blob: f5f39cc281029559f1ee3883290ca117767a57e3 [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
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -0700591 priv->cfg->ops->smgmt->clear_station_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, Tomasc2d79b42008-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, Tomasc2d79b42008-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:
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -0700737 sta_id = priv->cfg->ops->smgmt->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 */
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -0700746 sta_id = priv->cfg->ops->smgmt->find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700747 if (sta_id != IWL_INVALID_STATION)
748 return sta_id;
749
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -0700750 sta_id = priv->cfg->ops->smgmt->add_station(priv,
751 hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -0700752
753 if (sta_id != IWL_INVALID_STATION)
754 return sta_id;
755
Tomas Winklere1623442009-01-27 14:27:56 -0800756 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -0700757 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -0700758 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +0800759 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800760 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -0700761 }
Stefanik Gábor914233d2008-06-30 17:23:30 +0800762 /* If we are in monitor mode, use BCAST. This is required for
763 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +0200764 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800765 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +0800766
Zhu Yib481de92007-09-25 17:54:57 -0700767 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800768 IWL_WARN(priv, "Unknown mode of operation: %d\n",
769 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800770 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700771 }
772}
773
774/*
775 * start REPLY_TX command process
776 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800777static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700778{
779 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200780 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomase52119c2008-12-22 11:31:19 +0800781 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800782 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800783 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800784 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700785 dma_addr_t phys_addr;
786 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800787 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800788 u16 len, idx, len_org, hdr_len;
789 u8 id;
790 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700791 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800792 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700793 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700794 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700795 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800796 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700797 unsigned long flags;
798 int rc;
799
800 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800801 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800802 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700803 goto drop_unlock;
804 }
805
Johannes Berge039fa42008-05-15 12:55:29 +0200806 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800807 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700808 goto drop_unlock;
809 }
810
811 unicast = !is_multicast_ether_addr(hdr->addr1);
812 id = 0;
813
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700814 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700815
Samuel Ortizd08853a2009-01-23 13:45:17 -0800816#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700817 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800818 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700819 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800820 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700821 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800822 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700823#endif
824
Mohamed Abbas7878a5a2007-11-29 11:10:13 +0800825 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +0800826 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200827 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800828 (!iwl_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200829 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Tomas Winklere1623442009-01-27 14:27:56 -0800830 IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -0700831 goto drop_unlock;
832 }
833
834 spin_unlock_irqrestore(&priv->lock, flags);
835
Harvey Harrison7294ec92008-07-15 18:43:59 -0700836 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800837
838 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -0700840 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800841 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700842 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700843 goto drop;
844 }
845
Tomas Winklere1623442009-01-27 14:27:56 -0800846 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700847
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700848 if (ieee80211_is_data_qos(fc)) {
849 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700850 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800851 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -0700852 IEEE80211_SCTL_SEQ;
853 hdr->seq_ctrl = cpu_to_le16(seq_number) |
854 (hdr->seq_ctrl &
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800855 cpu_to_le16(IEEE80211_SCTL_FRAG));
Zhu Yib481de92007-09-25 17:54:57 -0700856 seq_number += 0x10;
857 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800858
859 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800860 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700861 q = &txq->q;
862
863 spin_lock_irqsave(&priv->lock, flags);
864
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800865 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700866
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800867 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800868 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800869 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800870
871 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800872 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +0800873 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700874 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +0800875 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800876
877 /*
878 * Set up the Tx-command (not MAC!) header.
879 * Store the chosen Tx queue and TFD index within the sequence field;
880 * after Tx, uCode's Tx response will return this value so driver can
881 * locate the frame within the tx queue and do post-tx processing.
882 */
Zhu Yib481de92007-09-25 17:54:57 -0700883 out_cmd->hdr.cmd = REPLY_TX;
884 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800885 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800886
887 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +0800888 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700889
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800890 /*
891 * Use the first empty entry in this queue's command buffer array
892 * to contain the Tx command and MAC header concatenated together
893 * (payload data will be in another buffer).
894 * Size of this varies, due to varying MAC header length.
895 * If end is not dword aligned, we'll have 2 extra bytes at the end
896 * of the MAC header (device reads on dword boundaries).
897 * We'll tell device about this padding later.
898 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800899 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800900 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700901
902 len_org = len;
903 len = (len + 3) & ~3;
904
905 if (len_org != len)
906 len_org = 1;
907 else
908 len_org = 0;
909
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800910 /* Physical address of this Tx command's header (not MAC header!),
911 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800912 txcmd_phys = pci_map_single(priv->pci_dev,
913 out_cmd, sizeof(struct iwl_cmd),
914 PCI_DMA_TODEVICE);
915 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
916 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
917 /* Add buffer containing Tx command and MAC(!) header to TFD's
918 * first entry */
919 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -0700920
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800921 /* Add buffer containing Tx command and MAC(!) header to TFD's
922 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800923 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
924 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700925
Johannes Bergd0f09802008-07-29 11:32:07 +0200926 if (info->control.hw_key)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800927 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700928
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800929 /* Set up TFD's 2nd entry to point directly to remainder of skb,
930 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700931 len = skb->len - hdr_len;
932 if (len) {
933 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
934 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800935 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
936 phys_addr, len,
937 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700938 }
939
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800940 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -0700941 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800942 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -0700943
944 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +0800945 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700946
947 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +0200948 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700949
Winkler, Tomase52119c2008-12-22 11:31:19 +0800950 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
951 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700952
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700953 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -0700954 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300955 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800956 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -0700957 } else {
958 wait_write_ptr = 1;
959 txq->need_update = 0;
960 }
961
Winkler, Tomase52119c2008-12-22 11:31:19 +0800962 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -0700963
Winkler, Tomase52119c2008-12-22 11:31:19 +0800964 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -0700965 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -0700966
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800967 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800968 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800969 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700970 spin_unlock_irqrestore(&priv->lock, flags);
971
972 if (rc)
973 return rc;
974
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800975 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700976 && priv->mac80211_registered) {
977 if (wait_write_ptr) {
978 spin_lock_irqsave(&priv->lock, flags);
979 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800980 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700981 spin_unlock_irqrestore(&priv->lock, flags);
982 }
983
Johannes Berge4e72fb2009-03-23 17:28:42 +0100984 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700985 }
986
987 return 0;
988
989drop_unlock:
990 spin_unlock_irqrestore(&priv->lock, flags);
991drop:
992 return -1;
993}
994
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800995#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -0700996
997#include "iwl-spectrum.h"
998
999#define BEACON_TIME_MASK_LOW 0x00FFFFFF
1000#define BEACON_TIME_MASK_HIGH 0xFF000000
1001#define TIME_UNIT 1024
1002
1003/*
1004 * extended beacon time format
1005 * time in usec will be changed into a 32-bit value in 8:24 format
1006 * the high 1 byte is the beacon counts
1007 * the lower 3 bytes is the time in usec within one beacon interval
1008 */
1009
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001010static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001011{
1012 u32 quot;
1013 u32 rem;
1014 u32 interval = beacon_interval * 1024;
1015
1016 if (!interval || !usec)
1017 return 0;
1018
1019 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1020 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1021
1022 return (quot << 24) + rem;
1023}
1024
1025/* base is usually what we get from ucode with each received frame,
1026 * the same as HW timer counter counting down
1027 */
1028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001029static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001030{
1031 u32 base_low = base & BEACON_TIME_MASK_LOW;
1032 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1033 u32 interval = beacon_interval * TIME_UNIT;
1034 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1035 (addon & BEACON_TIME_MASK_HIGH);
1036
1037 if (base_low > addon_low)
1038 res += base_low - addon_low;
1039 else if (base_low < addon_low) {
1040 res += interval + base_low - addon_low;
1041 res += (1 << 24);
1042 } else
1043 res += (1 << 24);
1044
1045 return cpu_to_le32(res);
1046}
1047
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001048static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001049 struct ieee80211_measurement_params *params,
1050 u8 type)
1051{
Tomas Winkler600c0e12008-12-19 10:37:04 +08001052 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001053 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001054 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001055 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1056 .data = (void *)&spectrum,
1057 .meta.flags = CMD_WANT_SKB,
1058 };
1059 u32 add_time = le64_to_cpu(params->start_time);
1060 int rc;
1061 int spectrum_resp_status;
1062 int duration = le16_to_cpu(params->duration);
1063
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001064 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001065 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001066 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001067 le64_to_cpu(params->start_time) - priv->last_tsf,
1068 le16_to_cpu(priv->rxon_timing.beacon_interval));
1069
1070 memset(&spectrum, 0, sizeof(spectrum));
1071
1072 spectrum.channel_count = cpu_to_le16(1);
1073 spectrum.flags =
1074 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1075 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1076 cmd.len = sizeof(spectrum);
1077 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1078
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001079 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001080 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001081 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001082 add_time,
1083 le16_to_cpu(priv->rxon_timing.beacon_interval));
1084 else
1085 spectrum.start_time = 0;
1086
1087 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1088 spectrum.channels[0].channel = params->channel;
1089 spectrum.channels[0].type = type;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001090 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07001091 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1092 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1093
Samuel Ortiz518099a2009-01-19 15:30:27 -08001094 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001095 if (rc)
1096 return rc;
1097
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001098 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001099 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001100 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07001101 rc = -EIO;
1102 }
1103
1104 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1105 switch (spectrum_resp_status) {
1106 case 0: /* Command will be handled */
1107 if (res->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -08001108 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Ian Schrambc434dd2007-10-25 17:15:29 +08001109 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07001110 priv->measurement_status &= ~MEASUREMENT_READY;
1111 }
1112 priv->measurement_status |= MEASUREMENT_ACTIVE;
1113 rc = 0;
1114 break;
1115
1116 case 1: /* Command will not be handled */
1117 rc = -EAGAIN;
1118 break;
1119 }
1120
1121 dev_kfree_skb_any(cmd.meta.u.skb);
1122
1123 return rc;
1124}
1125#endif
1126
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001127static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001128 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001129{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001130 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1131 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001132 struct delayed_work *pwork;
1133
1134 palive = &pkt->u.alive_frame;
1135
Tomas Winklere1623442009-01-27 14:27:56 -08001136 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -07001137 "0x%01X 0x%01X\n",
1138 palive->is_valid, palive->ver_type,
1139 palive->ver_subtype);
1140
1141 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001142 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001143 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
1144 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001145 pwork = &priv->init_alive_start;
1146 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08001147 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001148 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001149 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001150 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001151 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001152 }
1153
1154 /* We delay the ALIVE response by 5ms to
1155 * give the HW RF Kill time to activate... */
1156 if (palive->is_valid == UCODE_VALID_OK)
1157 queue_delayed_work(priv->workqueue, pwork,
1158 msecs_to_jiffies(5));
1159 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001160 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001161}
1162
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001163static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001164 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001165{
Helmut Schaac7e035a2009-01-19 13:02:15 +01001166#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001167 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Helmut Schaac7e035a2009-01-19 13:02:15 +01001168#endif
Zhu Yib481de92007-09-25 17:54:57 -07001169
Tomas Winklere1623442009-01-27 14:27:56 -08001170 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -07001171 return;
1172}
1173
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001174static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07001175{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001176 struct iwl_priv *priv =
1177 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07001178 struct sk_buff *beacon;
1179
1180 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02001181 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07001182
1183 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001184 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001185 return;
1186 }
1187
1188 mutex_lock(&priv->mutex);
1189 /* new beacon skb is allocated every time; dispose previous.*/
1190 if (priv->ibss_beacon)
1191 dev_kfree_skb(priv->ibss_beacon);
1192
1193 priv->ibss_beacon = beacon;
1194 mutex_unlock(&priv->mutex);
1195
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001196 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001197}
1198
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001199static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001200 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001201{
Samuel Ortizd08853a2009-01-23 13:45:17 -08001202#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001203 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001204 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07001205 u8 rate = beacon->beacon_notify_hdr.rate;
1206
Tomas Winklere1623442009-01-27 14:27:56 -08001207 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -07001208 "tsf %d %d rate %d\n",
1209 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1210 beacon->beacon_notify_hdr.failure_frame,
1211 le32_to_cpu(beacon->ibss_mgr_status),
1212 le32_to_cpu(beacon->high_tsf),
1213 le32_to_cpu(beacon->low_tsf), rate);
1214#endif
1215
Johannes Berg05c914f2008-09-11 00:01:58 +02001216 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07001217 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1218 queue_work(priv->workqueue, &priv->beacon_update);
1219}
1220
Zhu Yib481de92007-09-25 17:54:57 -07001221/* Handle notification from uCode that card's power state is changing
1222 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001223static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001224 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001225{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001226 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001227 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1228 unsigned long status = priv->status;
1229
Tomas Winklere1623442009-01-27 14:27:56 -08001230 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001231 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1232 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1233
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001234 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001235 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1236
1237 if (flags & HW_CARD_DISABLED)
1238 set_bit(STATUS_RF_KILL_HW, &priv->status);
1239 else
1240 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1241
1242
1243 if (flags & SW_CARD_DISABLED)
1244 set_bit(STATUS_RF_KILL_SW, &priv->status);
1245 else
1246 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1247
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08001248 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001249
1250 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1251 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1252 (test_bit(STATUS_RF_KILL_SW, &status) !=
1253 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1254 queue_work(priv->workqueue, &priv->rf_kill);
1255 else
1256 wake_up_interruptible(&priv->wait_command_queue);
1257}
1258
1259/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001260 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07001261 *
1262 * Setup the RX handlers for each of the reply types sent from the uCode
1263 * to the host.
1264 *
1265 * This function chains into the hardware specific files for them to setup
1266 * any hardware specific handlers as well.
1267 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001268static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001269{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001270 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
1271 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -08001272 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001273 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -08001274 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001275 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -08001276 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001277 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001278
Ben Cahill9fbab512007-11-29 11:09:47 +08001279 /*
1280 * The same handler is used for both the REPLY to a discrete
1281 * statistics request from the host as well as for the periodic
1282 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07001283 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001284 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
1285 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07001286
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -08001287 iwl_setup_spectrum_handlers(priv);
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -08001288 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001289 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001290
Ben Cahill9fbab512007-11-29 11:09:47 +08001291 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001292 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001293}
1294
Zhu Yib481de92007-09-25 17:54:57 -07001295/************************** RX-FUNCTIONS ****************************/
1296/*
1297 * Rx theory of operation
1298 *
1299 * The host allocates 32 DMA target addresses and passes the host address
1300 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
1301 * 0 to 31
1302 *
1303 * Rx Queue Indexes
1304 * The host/firmware share two index registers for managing the Rx buffers.
1305 *
1306 * The READ index maps to the first position that the firmware may be writing
1307 * to -- the driver can read up to (but not including) this position and get
1308 * good data.
1309 * The READ index is managed by the firmware once the card is enabled.
1310 *
1311 * The WRITE index maps to the last position the driver has read from -- the
1312 * position preceding WRITE is the last slot the firmware can place a packet.
1313 *
1314 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
1315 * WRITE = READ.
1316 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001317 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07001318 * INDEX position, and WRITE to the last (READ - 1 wrapped)
1319 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001320 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07001321 * and fire the RX interrupt. The driver can then query the READ index and
1322 * process as many packets as possible, moving the WRITE index forward as it
1323 * resets the Rx queue buffers with new memory.
1324 *
1325 * The management in the driver is as follows:
1326 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
1327 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08001328 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001329 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07001330 * iwl->rxq is replenished and the READ INDEX is updated (updating the
1331 * 'processed' and 'read' driver indexes as well)
1332 * + A received packet is processed and handed to the kernel network stack,
1333 * detached from the iwl->rxq. The driver 'processed' index is updated.
1334 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1335 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1336 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
1337 * were enough free buffers and RX_STALLED is set it is cleared.
1338 *
1339 *
1340 * Driver sequence:
1341 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001342 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001343 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08001344 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07001345 * queue, updates firmware pointers, and updates
1346 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001347 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07001348 *
1349 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001350 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07001351 * READ INDEX, detaching the SKB from the pool.
1352 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001353 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07001354 * slots.
1355 * ...
1356 *
1357 */
1358
1359/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001360 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07001361 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001362static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001363 dma_addr_t dma_addr)
1364{
1365 return cpu_to_le32((u32)dma_addr);
1366}
1367
1368/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001369 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07001370 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001371 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07001372 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08001373 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07001374 *
1375 * This moves the 'write' index forward to catch up with 'processed', and
1376 * also updates the memory address in the firmware to reference the new
1377 * target buffer.
1378 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001379static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001380{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001381 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001382 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001383 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001384 unsigned long flags;
1385 int write, rc;
1386
1387 spin_lock_irqsave(&rxq->lock, flags);
1388 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001389 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001390 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001391 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001392 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001393 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001394
1395 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001396 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001397 rxq->queue[rxq->write] = rxb;
1398 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1399 rxq->free_count--;
1400 }
1401 spin_unlock_irqrestore(&rxq->lock, flags);
1402 /* If the pre-allocated buffer pool is dropping low, schedule to
1403 * refill it */
1404 if (rxq->free_count <= RX_LOW_WATERMARK)
1405 queue_work(priv->workqueue, &priv->rx_replenish);
1406
1407
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001408 /* If we've added more space for the firmware to place data, tell it.
1409 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07001410 if ((write != (rxq->write & ~0x7))
1411 || (abs(rxq->write - rxq->read) > 7)) {
1412 spin_lock_irqsave(&rxq->lock, flags);
1413 rxq->need_update = 1;
1414 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001415 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001416 if (rc)
1417 return rc;
1418 }
1419
1420 return 0;
1421}
1422
1423/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001424 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001425 *
1426 * When moving to rx_free an SKB is allocated for the slot.
1427 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001428 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001429 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001430 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001431static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001432{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001433 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001434 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001435 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001436 unsigned long flags;
1437 spin_lock_irqsave(&rxq->lock, flags);
1438 while (!list_empty(&rxq->rx_used)) {
1439 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001440 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001441
1442 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001443 rxb->skb =
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001444 alloc_skb(priv->hw_params.rx_buf_size,
1445 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07001446 if (!rxb->skb) {
1447 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08001448 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07001449 /* We don't reschedule replenish work here -- we will
1450 * call the restock method and if it still needs
1451 * more buffers it will schedule replenish */
1452 break;
1453 }
Zhu Yi12342c42007-12-20 11:27:32 +08001454
1455 /* If radiotap head is required, reserve some headroom here.
1456 * The physical head count is a variable rx_stats->phy_count.
1457 * We reserve 4 bytes here. Plus these extra bytes, the
1458 * headroom of the physical head should be enough for the
1459 * radiotap head that iwl3945 supported. See iwl3945_rt.
1460 */
1461 skb_reserve(rxb->skb, 4);
1462
Zhu Yib481de92007-09-25 17:54:57 -07001463 priv->alloc_rxb_skb++;
1464 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001465
1466 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001467 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
1468 rxb->skb->data,
1469 priv->hw_params.rx_buf_size,
1470 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001471 list_add_tail(&rxb->list, &rxq->rx_free);
1472 rxq->free_count++;
1473 }
1474 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001475}
1476
1477/*
1478 * this should be called while priv->lock is locked
1479 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02001480static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001481{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001482 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001483
1484 iwl3945_rx_allocate(priv);
1485 iwl3945_rx_queue_restock(priv);
1486}
1487
1488
1489void iwl3945_rx_replenish(void *data)
1490{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001491 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001492 unsigned long flags;
1493
1494 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001495
1496 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001497 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001498 spin_unlock_irqrestore(&priv->lock, flags);
1499}
1500
Zhu Yib481de92007-09-25 17:54:57 -07001501/* Convert linear signal-to-noise ratio into dB */
1502static u8 ratio2dB[100] = {
1503/* 0 1 2 3 4 5 6 7 8 9 */
1504 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1505 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1506 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1507 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1508 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1509 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1510 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1511 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1512 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1513 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1514};
1515
1516/* Calculates a relative dB value from a ratio of linear
1517 * (i.e. not dB) signal levels.
1518 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001519int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001520{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001521 /* 1000:1 or higher just report as 60 dB */
1522 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001523 return 60;
1524
Adrian Bunk221c80c2008-02-02 23:19:01 +02001525 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001526 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001527 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001528 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001529
1530 /* We shouldn't see this */
1531 if (sig_ratio < 1)
1532 return 0;
1533
1534 /* Use table for ratios 1:1 - 99:1 */
1535 return (int)ratio2dB[sig_ratio];
1536}
1537
1538#define PERFECT_RSSI (-20) /* dBm */
1539#define WORST_RSSI (-95) /* dBm */
1540#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1541
1542/* Calculate an indication of rx signal quality (a percentage, not dBm!).
1543 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1544 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001545int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07001546{
1547 int sig_qual;
1548 int degradation = PERFECT_RSSI - rssi_dbm;
1549
1550 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1551 * as indicator; formula is (signal dbm - noise dbm).
1552 * SNR at or above 40 is a great signal (100%).
1553 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1554 * Weakest usable signal is usually 10 - 15 dB SNR. */
1555 if (noise_dbm) {
1556 if (rssi_dbm - noise_dbm >= 40)
1557 return 100;
1558 else if (rssi_dbm < noise_dbm)
1559 return 0;
1560 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1561
1562 /* Else use just the signal level.
1563 * This formula is a least squares fit of data points collected and
1564 * compared with a reference system that had a percentage (%) display
1565 * for signal quality. */
1566 } else
1567 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1568 (15 * RSSI_RANGE + 62 * degradation)) /
1569 (RSSI_RANGE * RSSI_RANGE);
1570
1571 if (sig_qual > 100)
1572 sig_qual = 100;
1573 else if (sig_qual < 1)
1574 sig_qual = 0;
1575
1576 return sig_qual;
1577}
1578
1579/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001580 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001581 *
1582 * Uses the priv->rx_handlers callback function array to invoke
1583 * the appropriate handlers, including command responses,
1584 * frame-received notifications, and other notifications.
1585 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001586static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001587{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001588 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001589 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001590 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001591 u32 r, i;
1592 int reclaim;
1593 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001594 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001595 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07001596
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001597 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1598 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001599 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001600 i = rxq->read;
1601
Winkler, Tomas37d68312009-01-08 10:19:54 -08001602 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001603 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001604 /* Rx interrupt, but nothing sent from uCode */
1605 if (i == r)
Tomas Winklere1623442009-01-27 14:27:56 -08001606 IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001607
1608 while (i != r) {
1609 rxb = rxq->queue[i];
1610
Ben Cahill9fbab512007-11-29 11:09:47 +08001611 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001612 * then a bug has been introduced in the queue refilling
1613 * routines -- catch it here */
1614 BUG_ON(rxb == NULL);
1615
1616 rxq->queue[i] = NULL;
1617
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001618 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001619 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07001620 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001621 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001622
1623 /* Reclaim a command buffer only if this packet is a response
1624 * to a (driver-originated) command.
1625 * If the packet (e.g. Rx frame) originated from uCode,
1626 * there is no command buffer to reclaim.
1627 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1628 * but apparently a few don't get set; catch them here. */
1629 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1630 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1631 (pkt->hdr.cmd != REPLY_TX);
1632
1633 /* Based on type of command response or notification,
1634 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001635 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001636 if (priv->rx_handlers[pkt->hdr.cmd]) {
Tomas Winklere1623442009-01-27 14:27:56 -08001637 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07001638 "r = %d, i = %d, %s, 0x%02x\n", r, i,
1639 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1640 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1641 } else {
1642 /* No handling needed */
Tomas Winklere1623442009-01-27 14:27:56 -08001643 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07001644 "r %d i %d No handler needed for %s, 0x%02x\n",
1645 r, i, get_cmd_string(pkt->hdr.cmd),
1646 pkt->hdr.cmd);
1647 }
1648
1649 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001650 /* Invoke any callbacks, transfer the skb to caller, and
Samuel Ortiz518099a2009-01-19 15:30:27 -08001651 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001652 * as we reclaim the driver command queue */
1653 if (rxb && rxb->skb)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001654 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001655 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001656 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001657 }
1658
1659 /* For now we just don't re-use anything. We can tweak this
1660 * later to try and re-use notification packets and SKBs that
1661 * fail to Rx correctly */
1662 if (rxb->skb != NULL) {
1663 priv->alloc_rxb_skb--;
1664 dev_kfree_skb_any(rxb->skb);
1665 rxb->skb = NULL;
1666 }
1667
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001668 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001669 priv->hw_params.rx_buf_size,
1670 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001671 spin_lock_irqsave(&rxq->lock, flags);
1672 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1673 spin_unlock_irqrestore(&rxq->lock, flags);
1674 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001675 /* If there are a lot of unused frames,
1676 * restock the Rx queue so ucode won't assert. */
1677 if (fill_rx) {
1678 count++;
1679 if (count >= 8) {
1680 priv->rxq.read = i;
1681 __iwl3945_rx_replenish(priv);
1682 count = 0;
1683 }
1684 }
Zhu Yib481de92007-09-25 17:54:57 -07001685 }
1686
1687 /* Backtrack one entry */
1688 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001689 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001690}
1691
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001692/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001693static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001694{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001695 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001696 synchronize_irq(priv->pci_dev->irq);
1697 tasklet_kill(&priv->irq_tasklet);
1698}
1699
Zhu Yib481de92007-09-25 17:54:57 -07001700static const char *desc_lookup(int i)
1701{
1702 switch (i) {
1703 case 1:
1704 return "FAIL";
1705 case 2:
1706 return "BAD_PARAM";
1707 case 3:
1708 return "BAD_CHECKSUM";
1709 case 4:
1710 return "NMI_INTERRUPT";
1711 case 5:
1712 return "SYSASSERT";
1713 case 6:
1714 return "FATAL_ERROR";
1715 }
1716
1717 return "UNKNOWN";
1718}
1719
1720#define ERROR_START_OFFSET (1 * sizeof(u32))
1721#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1722
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001723static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001724{
1725 u32 i;
1726 u32 desc, time, count, base, data1;
1727 u32 blink1, blink2, ilink1, ilink2;
1728 int rc;
1729
1730 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1731
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001732 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001733 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001734 return;
1735 }
1736
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001737 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001738 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001739 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001740 return;
1741 }
1742
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001743 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001744
1745 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001746 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1747 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1748 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001749 }
1750
Winkler, Tomas15b16872008-12-19 10:37:33 +08001751 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001752 "ilink1 nmiPC Line\n");
1753 for (i = ERROR_START_OFFSET;
1754 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1755 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001756 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001757 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001758 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001759 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001760 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001761 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001762 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001763 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001764 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001765 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001766 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001767 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001768 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001769
Winkler, Tomas15b16872008-12-19 10:37:33 +08001770 IWL_ERR(priv,
1771 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1772 desc_lookup(desc), desc, time, blink1, blink2,
1773 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07001774 }
1775
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001776 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001777
1778}
1779
Ben Cahillf58177b2007-11-29 11:09:43 +08001780#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001781
1782/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001783 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001784 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001785 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07001786 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001787static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07001788 u32 num_events, u32 mode)
1789{
1790 u32 i;
1791 u32 base; /* SRAM byte address of event log header */
1792 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1793 u32 ptr; /* SRAM byte address of log data */
1794 u32 ev, time, data; /* event log data */
1795
1796 if (num_events == 0)
1797 return;
1798
1799 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1800
1801 if (mode == 0)
1802 event_size = 2 * sizeof(u32);
1803 else
1804 event_size = 3 * sizeof(u32);
1805
1806 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1807
1808 /* "time" is actually "data" for mode 0 (no timestamp).
1809 * place event id # at far right for easier visual parsing. */
1810 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001811 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001812 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001813 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001814 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001815 if (mode == 0) {
1816 /* data, ev */
1817 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1818 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001819 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001820 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001821 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07001822 }
1823 }
1824}
1825
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001826static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001827{
1828 int rc;
1829 u32 base; /* SRAM byte address of event log header */
1830 u32 capacity; /* event log capacity in # entries */
1831 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1832 u32 num_wraps; /* # times uCode wrapped to top of log */
1833 u32 next_entry; /* index of next entry to be written by uCode */
1834 u32 size; /* # entries that we'll print */
1835
1836 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001837 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001838 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001839 return;
1840 }
1841
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001842 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001843 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001844 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001845 return;
1846 }
1847
1848 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001849 capacity = iwl_read_targ_mem(priv, base);
1850 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1851 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1852 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001853
1854 size = num_wraps ? capacity : next_entry;
1855
1856 /* bail out if nothing in log */
1857 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001858 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001859 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001860 return;
1861 }
1862
Winkler, Tomas15b16872008-12-19 10:37:33 +08001863 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07001864 size, num_wraps);
1865
1866 /* if uCode has wrapped back to top of log, start at the oldest entry,
1867 * i.e the next one that uCode would fill. */
1868 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001869 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07001870 capacity - next_entry, mode);
1871
1872 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001873 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07001874
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001875 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001876}
1877
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001878static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001879{
1880 unsigned long flags;
1881
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001882 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1883 sizeof(priv->staging_rxon));
1884 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07001885 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001886
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07001887 priv->cfg->ops->smgmt->add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001888
1889 spin_lock_irqsave(&priv->lock, flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001890 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07001891 priv->error_recovering = 0;
1892 spin_unlock_irqrestore(&priv->lock, flags);
1893}
1894
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001895static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001896{
1897 u32 inta, handled = 0;
1898 u32 inta_fh;
1899 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001900#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001901 u32 inta_mask;
1902#endif
1903
1904 spin_lock_irqsave(&priv->lock, flags);
1905
1906 /* Ack/clear/reset pending uCode interrupts.
1907 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1908 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001909 inta = iwl_read32(priv, CSR_INT);
1910 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001911
1912 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1913 * Any new interrupts that happen after this, either while we're
1914 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001915 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1916 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001917
Samuel Ortizd08853a2009-01-23 13:45:17 -08001918#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08001919 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001920 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001921 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001922 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001923 inta, inta_mask, inta_fh);
1924 }
1925#endif
1926
1927 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1928 * atomic, make sure that inta covers all the interrupts that
1929 * we've discovered, even if FH interrupt came in just after
1930 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001931 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001932 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001933 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001934 inta |= CSR_INT_BIT_FH_TX;
1935
1936 /* Now service all interrupt bits discovered above. */
1937 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001938 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001939
1940 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001941 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001942
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001943 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001944
1945 handled |= CSR_INT_BIT_HW_ERR;
1946
1947 spin_unlock_irqrestore(&priv->lock, flags);
1948
1949 return;
1950 }
1951
Samuel Ortizd08853a2009-01-23 13:45:17 -08001952#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08001953 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001954 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001955 if (inta & CSR_INT_BIT_SCD)
Tomas Winklere1623442009-01-27 14:27:56 -08001956 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001957 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001958
1959 /* Alive notification via Rx interrupt will do the real work */
1960 if (inta & CSR_INT_BIT_ALIVE)
Tomas Winklere1623442009-01-27 14:27:56 -08001961 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Zhu Yib481de92007-09-25 17:54:57 -07001962 }
1963#endif
1964 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001965 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001966
Zhu Yib481de92007-09-25 17:54:57 -07001967 /* Error detected by uCode */
1968 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001969 IWL_ERR(priv, "Microcode SW error detected. "
1970 "Restarting 0x%X.\n", inta);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001971 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001972 handled |= CSR_INT_BIT_SW_ERR;
1973 }
1974
1975 /* uCode wakes up after power-down sleep */
1976 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001977 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001978 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001979 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1980 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1981 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1982 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1983 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1984 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001985
1986 handled |= CSR_INT_BIT_WAKEUP;
1987 }
1988
1989 /* All uCode command responses, including Tx command responses,
1990 * Rx "responses" (frame-received notification), and other
1991 * notifications from uCode come through here*/
1992 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001993 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001994 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1995 }
1996
1997 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001998 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Zhu Yib481de92007-09-25 17:54:57 -07001999
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002000 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
2001 if (!iwl_grab_nic_access(priv)) {
2002 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08002003 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002004 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002005 }
2006 handled |= CSR_INT_BIT_FH_TX;
2007 }
2008
2009 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002010 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07002011
2012 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002013 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07002014 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002015 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07002016 }
2017
2018 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002019 /* only Re-enable if disabled by irq */
2020 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002021 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002022
Samuel Ortizd08853a2009-01-23 13:45:17 -08002023#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002024 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002025 inta = iwl_read32(priv, CSR_INT);
2026 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2027 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08002028 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07002029 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2030 }
2031#endif
2032 spin_unlock_irqrestore(&priv->lock, flags);
2033}
2034
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002035static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002036 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002037 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002038 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07002039{
2040 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01002041 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002042 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002043 u16 passive_dwell = 0;
2044 u16 active_dwell = 0;
2045 int added, i;
2046
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002047 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01002048 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07002049 return 0;
2050
Johannes Berg8318d782008-01-24 19:38:38 +01002051 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07002052
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08002053 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
2054 passive_dwell = iwl_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07002055
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08002056 if (passive_dwell <= active_dwell)
2057 passive_dwell = active_dwell + 1;
2058
Johannes Berg8318d782008-01-24 19:38:38 +01002059 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02002060 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2061 continue;
2062
Johannes Berg8318d782008-01-24 19:38:38 +01002063 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07002064
Samuel Ortize6148912009-01-23 13:45:15 -08002065 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002066 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002067 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07002068 scan_ch->channel);
2069 continue;
2070 }
2071
Zhu Yib481de92007-09-25 17:54:57 -07002072 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2073 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002074 /* If passive , set up for auto-switch
2075 * and use long active_dwell time.
2076 */
2077 if (!is_active || is_channel_passive(ch_info) ||
2078 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
2079 scan_ch->type = 0; /* passive */
2080 if (IWL_UCODE_API(priv->ucode_ver) == 1)
2081 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
2082 } else {
2083 scan_ch->type = 1; /* active */
2084 }
2085
2086 /* Set direct probe bits. These may be used both for active
2087 * scan channels (probes gets sent right away),
2088 * or for passive channels (probes get se sent only after
2089 * hearing clear Rx packet).*/
2090 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
2091 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08002092 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002093 } else {
2094 /* uCode v1 does not allow setting direct probe bits on
2095 * passive channel. */
2096 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08002097 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002098 }
Zhu Yib481de92007-09-25 17:54:57 -07002099
Ben Cahill9fbab512007-11-29 11:09:47 +08002100 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07002101 scan_ch->tpc.dsp_atten = 110;
2102 /* scan_pwr_info->tpc.dsp_atten; */
2103
2104 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01002105 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07002106 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2107 else {
2108 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2109 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08002110 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08002111 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07002112 */
2113 }
2114
Tomas Winklere1623442009-01-27 14:27:56 -08002115 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07002116 scan_ch->channel,
2117 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2118 (scan_ch->type & 1) ?
2119 active_dwell : passive_dwell);
2120
2121 scan_ch++;
2122 added++;
2123 }
2124
Tomas Winklere1623442009-01-27 14:27:56 -08002125 IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
Zhu Yib481de92007-09-25 17:54:57 -07002126 return added;
2127}
2128
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002129static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002130 struct ieee80211_rate *rates)
2131{
2132 int i;
2133
2134 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01002135 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
2136 rates[i].hw_value = i; /* Rate scaling will work on indexes */
2137 rates[i].hw_value_short = i;
2138 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08002139 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07002140 /*
Johannes Berg8318d782008-01-24 19:38:38 +01002141 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07002142 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002143 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01002144 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07002145 }
Zhu Yib481de92007-09-25 17:54:57 -07002146 }
2147}
2148
Zhu Yib481de92007-09-25 17:54:57 -07002149/******************************************************************************
2150 *
2151 * uCode download functions
2152 *
2153 ******************************************************************************/
2154
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002155static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002156{
Tomas Winkler98c92212008-01-14 17:46:20 -08002157 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2158 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2159 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2160 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2161 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2162 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002163}
2164
2165/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002166 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002167 * looking at all data.
2168 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002169static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002170{
2171 u32 val;
2172 u32 save_len = len;
2173 int rc = 0;
2174 u32 errcnt;
2175
Tomas Winklere1623442009-01-27 14:27:56 -08002176 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002177
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002178 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002179 if (rc)
2180 return rc;
2181
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002182 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002183 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07002184
2185 errcnt = 0;
2186 for (; len > 0; len -= sizeof(u32), image++) {
2187 /* read data comes through single port, auto-incr addr */
2188 /* NOTE: Use the debugless read so we don't flood kernel log
2189 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002190 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002191 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002192 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002193 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2194 save_len - len, val, le32_to_cpu(*image));
2195 rc = -EIO;
2196 errcnt++;
2197 if (errcnt >= 20)
2198 break;
2199 }
2200 }
2201
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002202 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002203
2204 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002205 IWL_DEBUG_INFO(priv,
2206 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002207
2208 return rc;
2209}
2210
2211
2212/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002213 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002214 * using sample data 100 bytes apart. If these sample points are good,
2215 * it's a pretty good bet that everything between them is good, too.
2216 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002217static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002218{
2219 u32 val;
2220 int rc = 0;
2221 u32 errcnt = 0;
2222 u32 i;
2223
Tomas Winklere1623442009-01-27 14:27:56 -08002224 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002225
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002226 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002227 if (rc)
2228 return rc;
2229
2230 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2231 /* read data comes through single port, auto-incr addr */
2232 /* NOTE: Use the debugless read so we don't flood kernel log
2233 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002234 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002235 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002236 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002237 if (val != le32_to_cpu(*image)) {
2238#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002239 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002240 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2241 i, val, *image);
2242#endif
2243 rc = -EIO;
2244 errcnt++;
2245 if (errcnt >= 3)
2246 break;
2247 }
2248 }
2249
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002250 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002251
2252 return rc;
2253}
2254
2255
2256/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002257 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002258 * and verify its contents
2259 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002260static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002261{
2262 __le32 *image;
2263 u32 len;
2264 int rc = 0;
2265
2266 /* Try bootstrap */
2267 image = (__le32 *)priv->ucode_boot.v_addr;
2268 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002269 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002270 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002271 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002272 return 0;
2273 }
2274
2275 /* Try initialize */
2276 image = (__le32 *)priv->ucode_init.v_addr;
2277 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002278 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002279 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002280 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002281 return 0;
2282 }
2283
2284 /* Try runtime/protocol */
2285 image = (__le32 *)priv->ucode_code.v_addr;
2286 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002287 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002288 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002289 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002290 return 0;
2291 }
2292
Winkler, Tomas15b16872008-12-19 10:37:33 +08002293 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002294
Ben Cahill9fbab512007-11-29 11:09:47 +08002295 /* Since nothing seems to match, show first several data entries in
2296 * instruction SRAM, so maybe visual inspection will give a clue.
2297 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002298 image = (__le32 *)priv->ucode_boot.v_addr;
2299 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002300 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002301
2302 return rc;
2303}
2304
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002305static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002306{
2307 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002308 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002309}
2310
2311/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002312 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002313 *
2314 * Copy into buffers for card to fetch via bus-mastering
2315 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002316static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002317{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08002318 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002319 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002320 const struct firmware *ucode_raw;
2321 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002322 const char *name_pre = priv->cfg->fw_name_pre;
2323 const unsigned int api_max = priv->cfg->ucode_api_max;
2324 const unsigned int api_min = priv->cfg->ucode_api_min;
2325 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002326 u8 *src;
2327 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002328 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002329
2330 /* Ask kernel firmware_class module to get the boot firmware off disk.
2331 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002332 for (index = api_max; index >= api_min; index--) {
2333 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2334 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2335 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002336 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002337 buf, ret);
2338 if (ret == -ENOENT)
2339 continue;
2340 else
2341 goto error;
2342 } else {
2343 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002344 IWL_ERR(priv, "Loaded firmware %s, "
2345 "which is deprecated. "
2346 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002347 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002348 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2349 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002350 buf, ucode_raw->size);
2351 break;
2352 }
Zhu Yib481de92007-09-25 17:54:57 -07002353 }
2354
Reinette Chatrea0987a82008-12-02 12:14:06 -08002355 if (ret < 0)
2356 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002357
2358 /* Make sure that we got at least our header! */
2359 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002360 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002361 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002362 goto err_release;
2363 }
2364
2365 /* Data from ucode file: header followed by uCode images */
2366 ucode = (void *)ucode_raw->data;
2367
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002368 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002369 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07002370 inst_size = le32_to_cpu(ucode->inst_size);
2371 data_size = le32_to_cpu(ucode->data_size);
2372 init_size = le32_to_cpu(ucode->init_size);
2373 init_data_size = le32_to_cpu(ucode->init_data_size);
2374 boot_size = le32_to_cpu(ucode->boot_size);
2375
Reinette Chatrea0987a82008-12-02 12:14:06 -08002376 /* api_ver should match the api version forming part of the
2377 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002378 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002379
2380 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002381 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002382 "Driver supports v%u, firmware is v%u.\n",
2383 api_max, api_ver);
2384 priv->ucode_ver = 0;
2385 ret = -EINVAL;
2386 goto err_release;
2387 }
2388 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002389 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002390 "got %u. New firmware can be obtained "
2391 "from http://www.intellinuxwireless.org.\n",
2392 api_max, api_ver);
2393
Tomas Winkler978785a2008-12-19 10:37:31 +08002394 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2395 IWL_UCODE_MAJOR(priv->ucode_ver),
2396 IWL_UCODE_MINOR(priv->ucode_ver),
2397 IWL_UCODE_API(priv->ucode_ver),
2398 IWL_UCODE_SERIAL(priv->ucode_ver));
2399
Tomas Winklere1623442009-01-27 14:27:56 -08002400 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002401 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002402 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2403 inst_size);
2404 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2405 data_size);
2406 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2407 init_size);
2408 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2409 init_data_size);
2410 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2411 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002412
Reinette Chatrea0987a82008-12-02 12:14:06 -08002413
Zhu Yib481de92007-09-25 17:54:57 -07002414 /* Verify size of file vs. image size info in file's header */
2415 if (ucode_raw->size < sizeof(*ucode) +
2416 inst_size + data_size + init_size +
2417 init_data_size + boot_size) {
2418
Tomas Winklere1623442009-01-27 14:27:56 -08002419 IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
2420 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002421 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002422 goto err_release;
2423 }
2424
2425 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002426 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002427 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002428 inst_size);
2429 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002430 goto err_release;
2431 }
2432
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002433 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002434 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002435 data_size);
2436 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002437 goto err_release;
2438 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002439 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002440 IWL_DEBUG_INFO(priv,
2441 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002442 init_size);
2443 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002444 goto err_release;
2445 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002446 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002447 IWL_DEBUG_INFO(priv,
2448 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002449 init_data_size);
2450 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002451 goto err_release;
2452 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002453 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002454 IWL_DEBUG_INFO(priv,
2455 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002456 boot_size);
2457 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002458 goto err_release;
2459 }
2460
2461 /* Allocate ucode buffers for card's bus-master loading ... */
2462
2463 /* Runtime instructions and 2 copies of data:
2464 * 1) unmodified from disk
2465 * 2) backup cache for save/restore during power-downs */
2466 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002467 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002468
2469 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002470 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002471
2472 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002473 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002474
2475 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002476 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002477 goto err_pci_alloc;
2478
Tomas Winkler90e759d2007-11-29 11:09:41 +08002479 /* Initialization instructions and data */
2480 if (init_size && init_data_size) {
2481 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002482 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002483
2484 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002485 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002486
2487 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2488 goto err_pci_alloc;
2489 }
2490
2491 /* Bootstrap (instructions only, no data) */
2492 if (boot_size) {
2493 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002494 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002495
2496 if (!priv->ucode_boot.v_addr)
2497 goto err_pci_alloc;
2498 }
2499
Zhu Yib481de92007-09-25 17:54:57 -07002500 /* Copy images into buffers for card's bus-master reads ... */
2501
2502 /* Runtime instructions (first block of data in file) */
2503 src = &ucode->data[0];
2504 len = priv->ucode_code.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002505 IWL_DEBUG_INFO(priv,
2506 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002507 memcpy(priv->ucode_code.v_addr, src, len);
Tomas Winklere1623442009-01-27 14:27:56 -08002508 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002509 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2510
2511 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002512 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07002513 src = &ucode->data[inst_size];
2514 len = priv->ucode_data.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002515 IWL_DEBUG_INFO(priv,
2516 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002517 memcpy(priv->ucode_data.v_addr, src, len);
2518 memcpy(priv->ucode_data_backup.v_addr, src, len);
2519
2520 /* Initialization instructions (3rd block) */
2521 if (init_size) {
2522 src = &ucode->data[inst_size + data_size];
2523 len = priv->ucode_init.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002524 IWL_DEBUG_INFO(priv,
2525 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002526 memcpy(priv->ucode_init.v_addr, src, len);
2527 }
2528
2529 /* Initialization data (4th block) */
2530 if (init_data_size) {
2531 src = &ucode->data[inst_size + data_size + init_size];
2532 len = priv->ucode_init_data.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002533 IWL_DEBUG_INFO(priv,
2534 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002535 memcpy(priv->ucode_init_data.v_addr, src, len);
2536 }
2537
2538 /* Bootstrap instructions (5th block) */
2539 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2540 len = priv->ucode_boot.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002541 IWL_DEBUG_INFO(priv,
2542 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002543 memcpy(priv->ucode_boot.v_addr, src, len);
2544
2545 /* We have our copies now, allow OS release its copies */
2546 release_firmware(ucode_raw);
2547 return 0;
2548
2549 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002550 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002551 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002552 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002553
2554 err_release:
2555 release_firmware(ucode_raw);
2556
2557 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002558 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002559}
2560
2561
2562/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002563 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002564 *
2565 * Tell initialization uCode where to find runtime uCode.
2566 *
2567 * BSM registers initially contain pointers to initialization uCode.
2568 * We need to replace them to load runtime uCode inst and data,
2569 * and to save runtime data when powering down.
2570 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002571static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002572{
2573 dma_addr_t pinst;
2574 dma_addr_t pdata;
2575 int rc = 0;
2576 unsigned long flags;
2577
2578 /* bits 31:0 for 3945 */
2579 pinst = priv->ucode_code.p_addr;
2580 pdata = priv->ucode_data_backup.p_addr;
2581
2582 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002583 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002584 if (rc) {
2585 spin_unlock_irqrestore(&priv->lock, flags);
2586 return rc;
2587 }
2588
2589 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002590 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2591 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2592 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002593 priv->ucode_data.len);
2594
Tomas Winklera96a27f2008-10-23 23:48:56 -07002595 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002596 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002597 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002598 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2599
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002600 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002601
2602 spin_unlock_irqrestore(&priv->lock, flags);
2603
Tomas Winklere1623442009-01-27 14:27:56 -08002604 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002605
2606 return rc;
2607}
2608
2609/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002610 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002611 *
2612 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2613 *
Zhu Yib481de92007-09-25 17:54:57 -07002614 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002615 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002616static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002617{
2618 /* Check alive response for "valid" sign from uCode */
2619 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2620 /* We had an error bringing up the hardware, so take it
2621 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002622 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002623 goto restart;
2624 }
2625
2626 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2627 * This is a paranoid check, because we would not have gotten the
2628 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002629 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002630 /* Runtime instruction load was bad;
2631 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002632 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002633 goto restart;
2634 }
2635
2636 /* Send pointers to protocol/runtime uCode image ... init code will
2637 * load and launch runtime uCode, which will send us another "Alive"
2638 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002639 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002640 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002641 /* Runtime instruction load won't happen;
2642 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002643 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002644 goto restart;
2645 }
2646 return;
2647
2648 restart:
2649 queue_work(priv->workqueue, &priv->restart);
2650}
2651
Zhu Yib481de92007-09-25 17:54:57 -07002652/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002653 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002654 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002655 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002656 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002657static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002658{
2659 int rc = 0;
2660 int thermal_spin = 0;
2661 u32 rfkill;
2662
Tomas Winklere1623442009-01-27 14:27:56 -08002663 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002664
2665 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2666 /* We had an error bringing up the hardware, so take it
2667 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002668 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002669 goto restart;
2670 }
2671
2672 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2673 * This is a paranoid check, because we would not have gotten the
2674 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002675 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002676 /* Runtime instruction load was bad;
2677 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002678 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002679 goto restart;
2680 }
2681
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07002682 priv->cfg->ops->smgmt->clear_station_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002683
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002684 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002685 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002686 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002687 return;
2688 }
2689
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002690 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002691 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002692 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002693
2694 if (rfkill & 0x1) {
2695 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002696 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002697 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002698 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002699 thermal_spin++;
2700 udelay(10);
2701 }
2702
2703 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002704 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002705 thermal_spin * 10);
2706 } else
2707 set_bit(STATUS_RF_KILL_HW, &priv->status);
2708
Ben Cahill9fbab512007-11-29 11:09:47 +08002709 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002710 set_bit(STATUS_ALIVE, &priv->status);
2711
2712 /* Clear out the uCode error bit if it is set */
2713 clear_bit(STATUS_FW_ERROR, &priv->status);
2714
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002715 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002716 return;
2717
Johannes Berg36d68252008-05-15 12:55:26 +02002718 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002719
2720 priv->active_rate = priv->rates_mask;
2721 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2722
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08002723 iwl_power_update_mode(priv, false);
Zhu Yib481de92007-09-25 17:54:57 -07002724
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002725 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002726 struct iwl3945_rxon_cmd *active_rxon =
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002727 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07002728
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002729 memcpy(&priv->staging_rxon, &priv->active_rxon,
2730 sizeof(priv->staging_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002731 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2732 } else {
2733 /* Initialize our rx_config data */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002734 iwl_connection_init_rx_config(priv, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002735 }
2736
Ben Cahill9fbab512007-11-29 11:09:47 +08002737 /* Configure Bluetooth device coexistence support */
Samuel Ortiz17f841c2009-01-23 13:45:20 -08002738 iwl_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002739
2740 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002741 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002742
Zhu Yib481de92007-09-25 17:54:57 -07002743 iwl3945_reg_txpower_periodic(priv);
2744
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002745 iwl3945_led_register(priv);
2746
Tomas Winklere1623442009-01-27 14:27:56 -08002747 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002748 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002749 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002750
2751 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002753
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002754 /* reassociate for ADHOC mode */
2755 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2756 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2757 priv->vif);
2758 if (beacon)
Abhijeet Kolekar9944b932009-04-08 11:26:36 -07002759 iwl_mac_beacon_update(priv->hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002760 }
2761
Abhijeet Kolekarf45c2712009-04-08 11:26:38 -07002762 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
2763 iwl3945_set_mode(priv, priv->iw_mode);
2764
Zhu Yib481de92007-09-25 17:54:57 -07002765 return;
2766
2767 restart:
2768 queue_work(priv->workqueue, &priv->restart);
2769}
2770
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002771static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002772
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002773static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002774{
2775 unsigned long flags;
2776 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2777 struct ieee80211_conf *conf = NULL;
2778
Tomas Winklere1623442009-01-27 14:27:56 -08002779 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002780
2781 conf = ieee80211_get_hw_conf(priv->hw);
2782
2783 if (!exit_pending)
2784 set_bit(STATUS_EXIT_PENDING, &priv->status);
2785
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002786 iwl3945_led_unregister(priv);
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07002787 priv->cfg->ops->smgmt->clear_station_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002788
2789 /* Unblock any waiting calls */
2790 wake_up_interruptible_all(&priv->wait_command_queue);
2791
Zhu Yib481de92007-09-25 17:54:57 -07002792 /* Wipe out the EXIT_PENDING status bit if we are not actually
2793 * exiting the module */
2794 if (!exit_pending)
2795 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2796
2797 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002798 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002799
2800 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002801 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002802 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002803 spin_unlock_irqrestore(&priv->lock, flags);
2804 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002805
2806 if (priv->mac80211_registered)
2807 ieee80211_stop_queues(priv->hw);
2808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002809 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002810 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002811 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002812 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2813 STATUS_RF_KILL_HW |
2814 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2815 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002816 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2817 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002818 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2819 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002820 goto exit;
2821 }
2822
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002823 /* ...otherwise clear out all the status bits but the RF Kill
2824 * bits and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002825 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2826 STATUS_RF_KILL_HW |
2827 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2828 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002829 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2830 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002831 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002832 STATUS_FW_ERROR |
2833 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2834 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002835
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002836 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002837 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002838 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002839 spin_unlock_irqrestore(&priv->lock, flags);
2840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002841 iwl3945_hw_txq_ctx_stop(priv);
2842 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002843
2844 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002845 if (!iwl_grab_nic_access(priv)) {
2846 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002847 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002848 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002849 }
2850 spin_unlock_irqrestore(&priv->lock, flags);
2851
2852 udelay(5);
2853
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002854 if (exit_pending)
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002855 priv->cfg->ops->lib->apm_ops.stop(priv);
2856 else
2857 priv->cfg->ops->lib->apm_ops.reset(priv);
2858
Zhu Yib481de92007-09-25 17:54:57 -07002859 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002860 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002861
2862 if (priv->ibss_beacon)
2863 dev_kfree_skb(priv->ibss_beacon);
2864 priv->ibss_beacon = NULL;
2865
2866 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002867 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002868}
2869
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002870static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002871{
2872 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002873 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002874 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002875
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002876 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002877}
2878
2879#define MAX_HW_RESTARTS 5
2880
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002881static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002882{
2883 int rc, i;
2884
2885 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002886 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002887 return -EIO;
2888 }
2889
2890 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002891 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07002892 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08002893 return -ENODEV;
2894 }
2895
Reinette Chatree903fbd2008-01-30 22:05:15 -08002896 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002897 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002898 return -EIO;
2899 }
2900
Zhu Yie655b9f2008-01-24 02:19:38 -08002901 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002902 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002903 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2904 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2905 else {
2906 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002907 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2908 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002909 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002910
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002911 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002912
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002913 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002914 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002915 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002916 return rc;
2917 }
2918
2919 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002920 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2921 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002922 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2923
2924 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002925 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002926 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002927
2928 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002929 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2930 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002931
2932 /* Copy original ucode data image from disk into backup cache.
2933 * This will be used to initialize the on-board processor's
2934 * data SRAM for a clean start when the runtime program first loads. */
2935 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002936 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002937
Zhu Yie655b9f2008-01-24 02:19:38 -08002938 /* We return success when we resume from suspend and rf_kill is on. */
2939 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2940 return 0;
2941
Zhu Yib481de92007-09-25 17:54:57 -07002942 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2943
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07002944 priv->cfg->ops->smgmt->clear_station_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002945
2946 /* load bootstrap state machine,
2947 * load bootstrap program into processor's memory,
2948 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08002949 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002950
2951 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002952 IWL_ERR(priv,
2953 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002954 continue;
2955 }
2956
2957 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002958 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002959
Tomas Winklere1623442009-01-27 14:27:56 -08002960 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002961
2962 return 0;
2963 }
2964
2965 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002966 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002967 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002968
2969 /* tried to restart and config the device for as long as our
2970 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002971 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002972 return -EIO;
2973}
2974
2975
2976/*****************************************************************************
2977 *
2978 * Workqueue callbacks
2979 *
2980 *****************************************************************************/
2981
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002982static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002983{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002984 struct iwl_priv *priv =
2985 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002986
2987 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2988 return;
2989
2990 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002991 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002992 mutex_unlock(&priv->mutex);
2993}
2994
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002995static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002996{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002997 struct iwl_priv *priv =
2998 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002999
3000 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3001 return;
3002
3003 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003004 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003005 mutex_unlock(&priv->mutex);
3006}
3007
Helmut Schaa26635162009-01-15 09:38:44 +01003008static void iwl3945_rfkill_poll(struct work_struct *data)
3009{
3010 struct iwl_priv *priv =
3011 container_of(data, struct iwl_priv, rfkill_poll.work);
3012 unsigned long status = priv->status;
3013
3014 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3015 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3016 else
3017 set_bit(STATUS_RF_KILL_HW, &priv->status);
3018
3019 if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
3020 queue_work(priv->workqueue, &priv->rf_kill);
3021
3022 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3023 round_jiffies_relative(2 * HZ));
3024
3025}
3026
Zhu Yib481de92007-09-25 17:54:57 -07003027#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003028static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003029{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003030 struct iwl_priv *priv =
3031 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08003032 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003033 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003034 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07003035 .meta.flags = CMD_SIZE_HUGE,
3036 };
3037 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003038 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07003039 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003040 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01003041 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04003042 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07003043
3044 conf = ieee80211_get_hw_conf(priv->hw);
3045
3046 mutex_lock(&priv->mutex);
3047
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003048 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003049 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003050 goto done;
3051 }
3052
Tomas Winklera96a27f2008-10-23 23:48:56 -07003053 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07003054 * was given the chance to run... */
3055 if (!test_bit(STATUS_SCANNING, &priv->status))
3056 goto done;
3057
3058 /* This should never be called or scheduled if there is currently
3059 * a scan active in the hardware. */
3060 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003061 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
3062 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003063 rc = -EIO;
3064 goto done;
3065 }
3066
3067 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003068 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07003069 goto done;
3070 }
3071
3072 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003073 IWL_DEBUG_HC(priv,
3074 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003075 goto done;
3076 }
3077
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003078 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003079 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07003080 goto done;
3081 }
3082
3083 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003084 IWL_DEBUG_HC(priv,
3085 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003086 goto done;
3087 }
3088
3089 if (!priv->scan_bands) {
Tomas Winklere1623442009-01-27 14:27:56 -08003090 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
Zhu Yib481de92007-09-25 17:54:57 -07003091 goto done;
3092 }
3093
Winkler, Tomas805cee52009-01-19 15:30:28 -08003094 if (!priv->scan) {
3095 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07003096 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Winkler, Tomas805cee52009-01-19 15:30:28 -08003097 if (!priv->scan) {
Zhu Yib481de92007-09-25 17:54:57 -07003098 rc = -ENOMEM;
3099 goto done;
3100 }
3101 }
Winkler, Tomas805cee52009-01-19 15:30:28 -08003102 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003103 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07003104
3105 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3106 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3107
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003108 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003109 u16 interval = 0;
3110 u32 extra;
3111 u32 suspend_time = 100;
3112 u32 scan_suspend_time = 100;
3113 unsigned long flags;
3114
Tomas Winklere1623442009-01-27 14:27:56 -08003115 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07003116
3117 spin_lock_irqsave(&priv->lock, flags);
3118 interval = priv->beacon_int;
3119 spin_unlock_irqrestore(&priv->lock, flags);
3120
3121 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003122 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07003123 if (!interval)
3124 interval = suspend_time;
3125 /*
3126 * suspend time format:
3127 * 0-19: beacon interval in usec (time before exec.)
3128 * 20-23: 0
3129 * 24-31: number of beacons (suspend between channels)
3130 */
3131
3132 extra = (suspend_time / interval) << 24;
3133 scan_suspend_time = 0xFF0FFFFF &
3134 (extra | ((suspend_time % interval) * 1024));
3135
3136 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08003137 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003138 scan_suspend_time, interval);
3139 }
3140
3141 /* We should add the ability for user to lock to PASSIVE ONLY */
3142 if (priv->one_direct_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08003143 IWL_DEBUG_SCAN(priv, "Kicking off one direct scan for '%s'\n",
3144 print_ssid(ssid, priv->direct_ssid,
John W. Linville9387b7c2008-09-30 20:59:05 -04003145 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07003146 scan->direct_scan[0].id = WLAN_EID_SSID;
3147 scan->direct_scan[0].len = priv->direct_ssid_len;
3148 memcpy(scan->direct_scan[0].ssid,
3149 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003150 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003151 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003152 IWL_DEBUG_SCAN(priv, "Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003153
3154 /* We don't build a direct scan probe request; the uCode will do
3155 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07003156 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08003157 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07003158 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3159
3160 /* flags + rate selection */
3161
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003162 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07003163 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3164 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
3165 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003166 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003167 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07003168 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
3169 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01003170 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003171 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003172 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07003173 goto done;
3174 }
3175
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003176 scan->tx_cmd.len = cpu_to_le16(
3177 iwl_fill_probe_req(priv, band,
3178 (struct ieee80211_mgmt *)scan->data,
3179 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
3180
Zhu Yib481de92007-09-25 17:54:57 -07003181 /* select Rx antennas */
3182 scan->flags |= iwl3945_get_antenna_flags(priv);
3183
Johannes Berg05c914f2008-09-11 00:01:58 +02003184 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07003185 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3186
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003187 scan->channel_count =
3188 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
3189 n_probes,
3190 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07003191
Reinette Chatre14b54332008-11-04 12:21:35 -08003192 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08003193 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08003194 goto done;
3195 }
3196
Zhu Yib481de92007-09-25 17:54:57 -07003197 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003198 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003199 cmd.data = scan;
3200 scan->len = cpu_to_le16(cmd.len);
3201
3202 set_bit(STATUS_SCAN_HW, &priv->status);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003203 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003204 if (rc)
3205 goto done;
3206
3207 queue_delayed_work(priv->workqueue, &priv->scan_check,
3208 IWL_SCAN_CHECK_WATCHDOG);
3209
3210 mutex_unlock(&priv->mutex);
3211 return;
3212
3213 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003214 /* can not perform scan make sure we clear scanning
3215 * bits from status so next scan request can be performed.
3216 * if we dont clear scanning status bit here all next scan
3217 * will fail
3218 */
3219 clear_bit(STATUS_SCAN_HW, &priv->status);
3220 clear_bit(STATUS_SCANNING, &priv->status);
3221
Ian Schram01ebd062007-10-25 17:15:22 +08003222 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003223 queue_work(priv->workqueue, &priv->scan_completed);
3224 mutex_unlock(&priv->mutex);
3225}
3226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003227static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003228{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003229 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07003230
3231 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3232 return;
3233
3234 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003235 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003236 mutex_unlock(&priv->mutex);
Samuel Ortizc0af96a2009-01-21 18:27:54 +01003237 iwl_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003238}
3239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003240static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003241{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003242 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003243
3244 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3245 return;
3246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003247 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003248 queue_work(priv->workqueue, &priv->up);
3249}
3250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003251static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003252{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003253 struct iwl_priv *priv =
3254 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003255
3256 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3257 return;
3258
3259 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003260 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003261 mutex_unlock(&priv->mutex);
3262}
3263
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003264static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
3265
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003266#define IWL_DELAY_NEXT_SCAN (HZ*2)
3267
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003268void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003269{
Zhu Yib481de92007-09-25 17:54:57 -07003270 int rc = 0;
3271 struct ieee80211_conf *conf = NULL;
3272
Johannes Berg05c914f2008-09-11 00:01:58 +02003273 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003274 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003275 return;
3276 }
3277
3278
Tomas Winklere1623442009-01-27 14:27:56 -08003279 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003280 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003281
3282 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3283 return;
3284
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003285 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003286 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003287
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003288 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003289
Zhu Yib481de92007-09-25 17:54:57 -07003290 conf = ieee80211_get_hw_conf(priv->hw);
3291
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003292 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003293 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003294
Tomas Winkler28afaf92008-12-19 10:37:06 +08003295 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003296 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003297 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003298 sizeof(priv->rxon_timing), &priv->rxon_timing);
3299 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003300 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003301 "Attempting to continue.\n");
3302
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003303 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003304
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003305 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003306
Tomas Winklere1623442009-01-27 14:27:56 -08003307 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003308 priv->assoc_id, priv->beacon_int);
3309
3310 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003311 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003312 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003313 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003314
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003315 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003316 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003317 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003318 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003319 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003320
Johannes Berg05c914f2008-09-11 00:01:58 +02003321 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003322 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003323
3324 }
3325
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003326 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003327
3328 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003329 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003330 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003331 break;
3332
Johannes Berg05c914f2008-09-11 00:01:58 +02003333 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07003334
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08003335 priv->assoc_id = 1;
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07003336 priv->cfg->ops->smgmt->add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003337 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01003338 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003339 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
3340 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003341 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
3342 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003343
3344 break;
3345
3346 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08003347 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003348 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07003349 break;
3350 }
3351
Abhijeet Kolekar14d2aac2009-02-27 16:21:24 -08003352 iwl_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003353
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003354 /* we have just associated, don't start scan too early */
3355 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003356}
3357
Zhu Yib481de92007-09-25 17:54:57 -07003358/*****************************************************************************
3359 *
3360 * mac80211 entry point functions
3361 *
3362 *****************************************************************************/
3363
Zhu Yi5a669262008-01-14 17:46:18 -08003364#define UCODE_READY_TIMEOUT (2 * HZ)
3365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003366static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003367{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003368 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08003369 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003370
Tomas Winklere1623442009-01-27 14:27:56 -08003371 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003372
3373 /* we should be verifying the device is ready to be opened */
3374 mutex_lock(&priv->mutex);
3375
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003376 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
Zhu Yi5a669262008-01-14 17:46:18 -08003377 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3378 * ucode filename and max sizes are card-specific. */
3379
3380 if (!priv->ucode_code.len) {
3381 ret = iwl3945_read_ucode(priv);
3382 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003383 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08003384 mutex_unlock(&priv->mutex);
3385 goto out_release_irq;
3386 }
3387 }
3388
Zhu Yie655b9f2008-01-24 02:19:38 -08003389 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08003390
Zhu Yib481de92007-09-25 17:54:57 -07003391 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08003392
Samuel Ortizc0af96a2009-01-21 18:27:54 +01003393 iwl_rfkill_set_hw_state(priv);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02003394
Zhu Yie655b9f2008-01-24 02:19:38 -08003395 if (ret)
3396 goto out_release_irq;
3397
Tomas Winklere1623442009-01-27 14:27:56 -08003398 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003399
Zhu Yi5a669262008-01-14 17:46:18 -08003400 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3401 * mac80211 will not be run successfully. */
3402 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3403 test_bit(STATUS_READY, &priv->status),
3404 UCODE_READY_TIMEOUT);
3405 if (!ret) {
3406 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003407 IWL_ERR(priv,
3408 "Wait for START_ALIVE timeout after %dms.\n",
3409 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08003410 ret = -ETIMEDOUT;
3411 goto out_release_irq;
3412 }
3413 }
3414
Helmut Schaa26635162009-01-15 09:38:44 +01003415 /* ucode is running and will send rfkill notifications,
3416 * no need to poll the killswitch state anymore */
3417 cancel_delayed_work(&priv->rfkill_poll);
3418
Zhu Yie655b9f2008-01-24 02:19:38 -08003419 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003420 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003421 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003422
3423out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003424 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003425 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08003426 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003427}
3428
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003429static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003430{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003431 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003432
Tomas Winklere1623442009-01-27 14:27:56 -08003433 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003434
Zhu Yie655b9f2008-01-24 02:19:38 -08003435 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003436 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003437 return;
3438 }
3439
Zhu Yib481de92007-09-25 17:54:57 -07003440 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003441
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003442 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003443 /* stop mac, cancel any scan request and clear
3444 * RXON_FILTER_ASSOC_MSK BIT
3445 */
Zhu Yi5a669262008-01-14 17:46:18 -08003446 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003447 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003448 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003449 }
3450
Zhu Yi5a669262008-01-14 17:46:18 -08003451 iwl3945_down(priv);
3452
3453 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003454
3455 /* start polling the killswitch state again */
3456 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3457 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003458
Tomas Winklere1623442009-01-27 14:27:56 -08003459 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003460}
3461
Johannes Berge039fa42008-05-15 12:55:29 +02003462static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003463{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003464 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003465
Tomas Winklere1623442009-01-27 14:27:56 -08003466 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003467
Tomas Winklere1623442009-01-27 14:27:56 -08003468 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003469 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003470
Johannes Berge039fa42008-05-15 12:55:29 +02003471 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003472 dev_kfree_skb_any(skb);
3473
Tomas Winklere1623442009-01-27 14:27:56 -08003474 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003475 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003476}
3477
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003478static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003479 struct ieee80211_if_init_conf *conf)
3480{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003481 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003482 unsigned long flags;
3483
Tomas Winklere1623442009-01-27 14:27:56 -08003484 IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07003485
Johannes Berg32bfd352007-12-19 01:31:26 +01003486 if (priv->vif) {
Tomas Winklere1623442009-01-27 14:27:56 -08003487 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02003488 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07003489 }
3490
3491 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01003492 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07003493 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07003494
3495 spin_unlock_irqrestore(&priv->lock, flags);
3496
3497 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02003498
3499 if (conf->mac_addr) {
Tomas Winklere1623442009-01-27 14:27:56 -08003500 IWL_DEBUG_MAC80211(priv, "Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02003501 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3502 }
3503
Abhijeet Kolekarf45c2712009-04-08 11:26:38 -07003504 if (iwl3945_set_mode(priv, conf->type) == -EAGAIN)
3505 set_bit(STATUS_MODE_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003506
Zhu Yib481de92007-09-25 17:54:57 -07003507 mutex_unlock(&priv->mutex);
3508
Tomas Winklere1623442009-01-27 14:27:56 -08003509 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003510 return 0;
3511}
3512
3513/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003514 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07003515 *
3516 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3517 * be set inappropriately and the driver currently sets the hardware up to
3518 * use it whenever needed.
3519 */
Johannes Berge8975582008-10-09 12:18:51 +02003520static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07003521{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003522 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003523 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02003524 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07003525 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08003526 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003527
3528 mutex_lock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003529 IWL_DEBUG_MAC80211(priv, "enter to channel %d\n",
3530 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07003531
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003532 if (!iwl_is_ready(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003533 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003534 ret = -EIO;
3535 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003536 }
3537
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003538 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07003539 test_bit(STATUS_SCANNING, &priv->status))) {
Tomas Winklere1623442009-01-27 14:27:56 -08003540 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
Zhu Yia0646472007-12-20 14:10:01 +08003541 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003542 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08003543 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003544 }
3545
3546 spin_lock_irqsave(&priv->lock, flags);
3547
Samuel Ortize6148912009-01-23 13:45:15 -08003548 ch_info = iwl_get_channel_info(priv, conf->channel->band,
3549 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07003550 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003551 IWL_DEBUG_SCAN(priv,
3552 "Channel %d [%d] is INVALID for this band.\n",
3553 conf->channel->hw_value, conf->channel->band);
3554 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
Zhu Yib481de92007-09-25 17:54:57 -07003555 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003556 ret = -EINVAL;
3557 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003558 }
3559
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003560 iwl_set_rxon_channel(priv, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003561
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003562 iwl_set_flags_for_band(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07003563
3564 /* The list of supported rates and rate mask can be different
3565 * for each phymode; since the phymode may have changed, reset
3566 * the rate mask to what mac80211 lists */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003567 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003568
3569 spin_unlock_irqrestore(&priv->lock, flags);
3570
3571#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3572 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003573 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003574 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003575 }
3576#endif
3577
Mohamed Abbas37fec382009-03-17 21:51:42 -07003578 if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
3579 if (conf->radio_enabled &&
3580 iwl_radio_kill_sw_enable_radio(priv)) {
3581 IWL_DEBUG_MAC80211(priv, "leave - RF-KILL - "
3582 "waiting for uCode\n");
3583 goto out;
3584 }
Zhu Yib481de92007-09-25 17:54:57 -07003585
Mohamed Abbas37fec382009-03-17 21:51:42 -07003586 if (!conf->radio_enabled) {
3587 iwl_radio_kill_sw_disable_radio(priv);
3588 IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
3589 goto out;
3590 }
Zhu Yib481de92007-09-25 17:54:57 -07003591 }
3592
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003593 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003594 IWL_DEBUG_MAC80211(priv, "leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003595 ret = -EIO;
3596 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003597 }
3598
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003599 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003600
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003601 if (memcmp(&priv->active_rxon,
3602 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003603 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003604 else
Tomas Winklere1623442009-01-27 14:27:56 -08003605 IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration\n");
Zhu Yib481de92007-09-25 17:54:57 -07003606
Tomas Winklere1623442009-01-27 14:27:56 -08003607 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003608
Zhu Yi76bb77e2007-11-22 10:53:22 +08003609out:
Zhu Yia0646472007-12-20 14:10:01 +08003610 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003611 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003612 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003613}
3614
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003615static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003616{
3617 int rc = 0;
3618
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003619 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003620 return;
3621
3622 /* The following should be done only at AP bring up */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003623 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003624
3625 /* RXON - unassoc (to set timing command) */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003626 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003627 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003628
3629 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08003630 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003631 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003632 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3633 sizeof(priv->rxon_timing),
3634 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07003635 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003636 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003637 "Attempting to continue.\n");
3638
3639 /* FIXME: what should be the assoc_id for AP? */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003640 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003641 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003642 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003643 RXON_FLG_SHORT_PREAMBLE_MSK;
3644 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003645 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003646 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3647
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003648 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003649 if (priv->assoc_capability &
3650 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003651 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003652 RXON_FLG_SHORT_SLOT_MSK;
3653 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003654 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003655 ~RXON_FLG_SHORT_SLOT_MSK;
3656
Johannes Berg05c914f2008-09-11 00:01:58 +02003657 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003658 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003659 ~RXON_FLG_SHORT_SLOT_MSK;
3660 }
3661 /* restore RXON assoc */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003662 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003663 iwlcore_commit_rxon(priv);
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07003664 priv->cfg->ops->smgmt->add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08003665 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003666 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003667
3668 /* FIXME - we need to add code here to detect a totally new
3669 * configuration, reset the AP, unassoc, rxon timing, assoc,
3670 * clear sta table, add BCAST sta... */
3671}
3672
Johannes Berg32bfd352007-12-19 01:31:26 +01003673static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
3674 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003675 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07003676{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003677 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003678 int rc;
3679
3680 if (conf == NULL)
3681 return -EIO;
3682
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003683 if (priv->vif != vif) {
Tomas Winklere1623442009-01-27 14:27:56 -08003684 IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003685 return 0;
3686 }
3687
Johannes Berg9d139c82008-07-09 14:40:37 +02003688 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02003689 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02003690 conf->changed & IEEE80211_IFCC_BEACON) {
3691 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
3692 if (!beacon)
3693 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08003694 mutex_lock(&priv->mutex);
Abhijeet Kolekar9944b932009-04-08 11:26:36 -07003695 rc = iwl_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08003696 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02003697 if (rc)
3698 return rc;
3699 }
3700
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003701 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08003702 return -EAGAIN;
3703
Zhu Yib481de92007-09-25 17:54:57 -07003704 mutex_lock(&priv->mutex);
3705
Zhu Yib481de92007-09-25 17:54:57 -07003706 if (conf->bssid)
Tomas Winklere1623442009-01-27 14:27:56 -08003707 IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07003708
Johannes Berg4150c572007-09-17 01:29:23 -04003709/*
3710 * very dubious code was here; the probe filtering flag is never set:
3711 *
Zhu Yib481de92007-09-25 17:54:57 -07003712 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
3713 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04003714 */
Zhu Yib481de92007-09-25 17:54:57 -07003715
Johannes Berg05c914f2008-09-11 00:01:58 +02003716 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07003717 if (!conf->bssid) {
3718 conf->bssid = priv->mac_addr;
3719 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08003720 IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07003721 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07003722 }
3723 if (priv->ibss_beacon)
3724 dev_kfree_skb(priv->ibss_beacon);
3725
Johannes Berg9d139c82008-07-09 14:40:37 +02003726 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07003727 }
3728
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003729 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08003730 goto done;
3731
Zhu Yib481de92007-09-25 17:54:57 -07003732 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
3733 !is_multicast_ether_addr(conf->bssid)) {
3734 /* If there is currently a HW scan going on in the background
3735 * then we need to cancel it else the RXON below will fail. */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003736 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003737 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07003738 "after 100ms\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003739 IWL_DEBUG_MAC80211(priv, "leaving:scan abort failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07003740 mutex_unlock(&priv->mutex);
3741 return -EAGAIN;
3742 }
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003743 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07003744
3745 /* TODO: Audit driver for usage of these members and see
3746 * if mac80211 deprecates them (priv->bssid looks like it
3747 * shouldn't be there, but I haven't scanned the IBSS code
3748 * to verify) - jpk */
3749 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
3750
Johannes Berg05c914f2008-09-11 00:01:58 +02003751 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003752 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003753 else {
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003754 rc = iwlcore_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02003755 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07003756 priv->cfg->ops->smgmt->add_station(priv,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003757 priv->active_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003758 }
3759
3760 } else {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003761 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003762 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003763 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003764 }
3765
Mohamed Abbasfde35712007-11-29 11:10:15 +08003766 done:
Tomas Winklere1623442009-01-27 14:27:56 -08003767 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003768 mutex_unlock(&priv->mutex);
3769
3770 return 0;
3771}
3772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003773static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003774 struct ieee80211_if_init_conf *conf)
3775{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003776 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003777
Tomas Winklere1623442009-01-27 14:27:56 -08003778 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003779
3780 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003781
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003782 if (iwl_is_ready_rf(priv)) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003783 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003784 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003785 iwlcore_commit_rxon(priv);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003786 }
Johannes Berg32bfd352007-12-19 01:31:26 +01003787 if (priv->vif == conf->vif) {
3788 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003789 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07003790 }
3791 mutex_unlock(&priv->mutex);
3792
Tomas Winklere1623442009-01-27 14:27:56 -08003793 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003794}
3795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003796static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003797 struct ieee80211_vif *vif,
3798 struct ieee80211_sta *sta,
3799 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003800{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003801 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01003802 const u8 *addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003803 int ret = 0;
3804 u8 sta_id = IWL_INVALID_STATION;
3805 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003806
Tomas Winklere1623442009-01-27 14:27:56 -08003807 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003808
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003809 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003810 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003811 return -EOPNOTSUPP;
3812 }
3813
Winkler, Tomas42986792009-01-19 15:30:22 -08003814 addr = sta ? sta->addr : iwl_bcast_addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003815 static_key = !iwl_is_associated(priv);
3816
3817 if (!static_key) {
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07003818 sta_id = priv->cfg->ops->smgmt->find_station(priv, addr);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003819 if (sta_id == IWL_INVALID_STATION) {
Joe Perches12514392009-04-16 16:23:26 -07003820 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003821 addr);
3822 return -EINVAL;
3823 }
Zhu Yib481de92007-09-25 17:54:57 -07003824 }
3825
3826 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003827 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003828 mutex_unlock(&priv->mutex);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003829
Zhu Yib481de92007-09-25 17:54:57 -07003830 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003831 case SET_KEY:
3832 if (static_key)
3833 ret = iwl3945_set_static_key(priv, key);
3834 else
3835 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3836 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003837 break;
3838 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003839 if (static_key)
3840 ret = iwl3945_remove_static_key(priv);
3841 else
3842 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3843 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003844 break;
3845 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003846 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003847 }
3848
Tomas Winklere1623442009-01-27 14:27:56 -08003849 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003850
Winkler, Tomas42986792009-01-19 15:30:22 -08003851 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003852}
3853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003854static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003855 struct ieee80211_tx_queue_stats *stats)
3856{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003857 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003858 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003859 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003860 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07003861 unsigned long flags;
3862
Tomas Winklere1623442009-01-27 14:27:56 -08003863 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003864
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003865 if (!iwl_is_ready_rf(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003866 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07003867 return -EIO;
3868 }
3869
3870 spin_lock_irqsave(&priv->lock, flags);
3871
3872 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003873 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07003874 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003875 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07003876
Johannes Berg57ffc582008-04-29 17:18:59 +02003877 stats[i].len = q->n_window - avail;
3878 stats[i].limit = q->n_window - q->high_mark;
3879 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07003880
3881 }
3882 spin_unlock_irqrestore(&priv->lock, flags);
3883
Tomas Winklere1623442009-01-27 14:27:56 -08003884 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003885
3886 return 0;
3887}
3888
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003889static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003890{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003891 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003892 unsigned long flags;
3893
3894 mutex_lock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003895 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003896
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003897 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003898
Zhu Yib481de92007-09-25 17:54:57 -07003899 spin_lock_irqsave(&priv->lock, flags);
3900 priv->assoc_id = 0;
3901 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003902
3903 /* new association get rid of ibss beacon skb */
3904 if (priv->ibss_beacon)
3905 dev_kfree_skb(priv->ibss_beacon);
3906
3907 priv->ibss_beacon = NULL;
3908
3909 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08003910 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02003911 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07003912 priv->beacon_int = 0;
3913
3914 spin_unlock_irqrestore(&priv->lock, flags);
3915
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003916 if (!iwl_is_ready_rf(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003917 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08003918 mutex_unlock(&priv->mutex);
3919 return;
3920 }
3921
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003922 /* we are restarting association process
3923 * clear RXON_FILTER_ASSOC_MSK bit
3924 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003925 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003926 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003927 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003928 iwlcore_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003929 }
3930
Zhu Yib481de92007-09-25 17:54:57 -07003931 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02003932 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003933
Tomas Winklere1623442009-01-27 14:27:56 -08003934 IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07003935 mutex_unlock(&priv->mutex);
3936 return;
3937 }
3938
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003939 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003940
3941 mutex_unlock(&priv->mutex);
3942
Tomas Winklere1623442009-01-27 14:27:56 -08003943 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003944
3945}
3946
Zhu Yib481de92007-09-25 17:54:57 -07003947/*****************************************************************************
3948 *
3949 * sysfs attributes
3950 *
3951 *****************************************************************************/
3952
Samuel Ortizd08853a2009-01-23 13:45:17 -08003953#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003954
3955/*
3956 * The following adds a new attribute to the sysfs representation
3957 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3958 * used for controlling the debug level.
3959 *
3960 * See the level definitions in iwl for details.
3961 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003962static ssize_t show_debug_level(struct device *d,
3963 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003964{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003965 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003966
3967 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003968}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003969static ssize_t store_debug_level(struct device *d,
3970 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003971 const char *buf, size_t count)
3972{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003973 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003974 unsigned long val;
3975 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003976
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003977 ret = strict_strtoul(buf, 0, &val);
3978 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003979 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003980 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003981 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07003982
3983 return strnlen(buf, count);
3984}
3985
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003986static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3987 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003988
Samuel Ortizd08853a2009-01-23 13:45:17 -08003989#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003990
Zhu Yib481de92007-09-25 17:54:57 -07003991static ssize_t show_temperature(struct device *d,
3992 struct device_attribute *attr, char *buf)
3993{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003994 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003995
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003996 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003997 return -EAGAIN;
3998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003999 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07004000}
4001
4002static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
4003
Zhu Yib481de92007-09-25 17:54:57 -07004004static ssize_t show_tx_power(struct device *d,
4005 struct device_attribute *attr, char *buf)
4006{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004007 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08004008 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07004009}
4010
4011static ssize_t store_tx_power(struct device *d,
4012 struct device_attribute *attr,
4013 const char *buf, size_t count)
4014{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004015 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004016 char *p = (char *)buf;
4017 u32 val;
4018
4019 val = simple_strtoul(p, &p, 10);
4020 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08004021 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07004022 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004023 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07004024
4025 return count;
4026}
4027
4028static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4029
4030static ssize_t show_flags(struct device *d,
4031 struct device_attribute *attr, char *buf)
4032{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004033 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004034
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004035 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07004036}
4037
4038static ssize_t store_flags(struct device *d,
4039 struct device_attribute *attr,
4040 const char *buf, size_t count)
4041{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004042 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004043 u32 flags = simple_strtoul(buf, NULL, 0);
4044
4045 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004046 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07004047 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004048 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004049 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004050 else {
Tomas Winklere1623442009-01-27 14:27:56 -08004051 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07004052 flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004053 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07004054 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004055 }
4056 }
4057 mutex_unlock(&priv->mutex);
4058
4059 return count;
4060}
4061
4062static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4063
4064static ssize_t show_filter_flags(struct device *d,
4065 struct device_attribute *attr, char *buf)
4066{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004067 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004068
4069 return sprintf(buf, "0x%04X\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004070 le32_to_cpu(priv->active_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07004071}
4072
4073static ssize_t store_filter_flags(struct device *d,
4074 struct device_attribute *attr,
4075 const char *buf, size_t count)
4076{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004077 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004078 u32 filter_flags = simple_strtoul(buf, NULL, 0);
4079
4080 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004081 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07004082 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004083 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004084 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004085 else {
Tomas Winklere1623442009-01-27 14:27:56 -08004086 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07004087 "0x%04X\n", filter_flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004088 priv->staging_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07004089 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07004090 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004091 }
4092 }
4093 mutex_unlock(&priv->mutex);
4094
4095 return count;
4096}
4097
4098static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4099 store_filter_flags);
4100
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004101#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004102
4103static ssize_t show_measurement(struct device *d,
4104 struct device_attribute *attr, char *buf)
4105{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004106 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08004107 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07004108 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004109 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07004110 unsigned long flags;
4111
4112 spin_lock_irqsave(&priv->lock, flags);
4113 if (!(priv->measurement_status & MEASUREMENT_READY)) {
4114 spin_unlock_irqrestore(&priv->lock, flags);
4115 return 0;
4116 }
4117 memcpy(&measure_report, &priv->measure_report, size);
4118 priv->measurement_status = 0;
4119 spin_unlock_irqrestore(&priv->lock, flags);
4120
4121 while (size && (PAGE_SIZE - len)) {
4122 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4123 PAGE_SIZE - len, 1);
4124 len = strlen(buf);
4125 if (PAGE_SIZE - len)
4126 buf[len++] = '\n';
4127
4128 ofs += 16;
4129 size -= min(size, 16U);
4130 }
4131
4132 return len;
4133}
4134
4135static ssize_t store_measurement(struct device *d,
4136 struct device_attribute *attr,
4137 const char *buf, size_t count)
4138{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004139 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004140 struct ieee80211_measurement_params params = {
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004141 .channel = le16_to_cpu(priv->active_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07004142 .start_time = cpu_to_le64(priv->last_tsf),
4143 .duration = cpu_to_le16(1),
4144 };
4145 u8 type = IWL_MEASURE_BASIC;
4146 u8 buffer[32];
4147 u8 channel;
4148
4149 if (count) {
4150 char *p = buffer;
4151 strncpy(buffer, buf, min(sizeof(buffer), count));
4152 channel = simple_strtoul(p, NULL, 0);
4153 if (channel)
4154 params.channel = channel;
4155
4156 p = buffer;
4157 while (*p && *p != ' ')
4158 p++;
4159 if (*p)
4160 type = simple_strtoul(p + 1, NULL, 0);
4161 }
4162
Tomas Winklere1623442009-01-27 14:27:56 -08004163 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07004164 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004165 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07004166
4167 return count;
4168}
4169
4170static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4171 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004172#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07004173
Zhu Yib481de92007-09-25 17:54:57 -07004174static ssize_t store_retry_rate(struct device *d,
4175 struct device_attribute *attr,
4176 const char *buf, size_t count)
4177{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004178 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004179
4180 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4181 if (priv->retry_rate <= 0)
4182 priv->retry_rate = 1;
4183
4184 return count;
4185}
4186
4187static ssize_t show_retry_rate(struct device *d,
4188 struct device_attribute *attr, char *buf)
4189{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004190 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004191 return sprintf(buf, "%d", priv->retry_rate);
4192}
4193
4194static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4195 store_retry_rate);
4196
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004197
Zhu Yib481de92007-09-25 17:54:57 -07004198static ssize_t store_power_level(struct device *d,
4199 struct device_attribute *attr,
4200 const char *buf, size_t count)
4201{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004202 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004203 int ret;
4204 unsigned long mode;
Zhu Yib481de92007-09-25 17:54:57 -07004205
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004206
Zhu Yib481de92007-09-25 17:54:57 -07004207 mutex_lock(&priv->mutex);
4208
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004209 ret = strict_strtoul(buf, 10, &mode);
4210 if (ret)
4211 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004212
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004213 ret = iwl_power_set_user_mode(priv, mode);
4214 if (ret) {
4215 IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
4216 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004217 }
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004218 ret = count;
Zhu Yib481de92007-09-25 17:54:57 -07004219
4220 out:
4221 mutex_unlock(&priv->mutex);
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004222 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004223}
4224
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004225static ssize_t show_power_level(struct device *d,
4226 struct device_attribute *attr, char *buf)
4227{
4228 struct iwl_priv *priv = dev_get_drvdata(d);
4229 int mode = priv->power_data.user_power_setting;
4230 int system = priv->power_data.system_power_setting;
4231 int level = priv->power_data.power_mode;
4232 char *p = buf;
4233
4234 switch (system) {
4235 case IWL_POWER_SYS_AUTO:
4236 p += sprintf(p, "SYSTEM:auto");
4237 break;
4238 case IWL_POWER_SYS_AC:
4239 p += sprintf(p, "SYSTEM:ac");
4240 break;
4241 case IWL_POWER_SYS_BATTERY:
4242 p += sprintf(p, "SYSTEM:battery");
4243 break;
4244 }
4245
4246 p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
4247 "fixed" : "auto");
4248 p += sprintf(p, "\tINDEX:%d", level);
4249 p += sprintf(p, "\n");
4250 return p - buf + 1;
4251}
4252
4253static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
4254 show_power_level, store_power_level);
4255
Zhu Yib481de92007-09-25 17:54:57 -07004256#define MAX_WX_STRING 80
4257
4258/* Values are in microsecond */
4259static const s32 timeout_duration[] = {
4260 350000,
4261 250000,
4262 75000,
4263 37000,
4264 25000,
4265};
4266static const s32 period_duration[] = {
4267 400000,
4268 700000,
4269 1000000,
4270 1000000,
4271 1000000
4272};
4273
Zhu Yib481de92007-09-25 17:54:57 -07004274static ssize_t show_channels(struct device *d,
4275 struct device_attribute *attr, char *buf)
4276{
Johannes Berg8318d782008-01-24 19:38:38 +01004277 /* all this shit doesn't belong into sysfs anyway */
4278 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004279}
4280
4281static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4282
4283static ssize_t show_statistics(struct device *d,
4284 struct device_attribute *attr, char *buf)
4285{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004286 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004287 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07004288 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004289 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07004290 int rc = 0;
4291
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004292 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004293 return -EAGAIN;
4294
4295 mutex_lock(&priv->mutex);
Samuel Ortiz17f841c2009-01-23 13:45:20 -08004296 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004297 mutex_unlock(&priv->mutex);
4298
4299 if (rc) {
4300 len = sprintf(buf,
4301 "Error sending statistics request: 0x%08X\n", rc);
4302 return len;
4303 }
4304
4305 while (size && (PAGE_SIZE - len)) {
4306 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4307 PAGE_SIZE - len, 1);
4308 len = strlen(buf);
4309 if (PAGE_SIZE - len)
4310 buf[len++] = '\n';
4311
4312 ofs += 16;
4313 size -= min(size, 16U);
4314 }
4315
4316 return len;
4317}
4318
4319static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4320
4321static ssize_t show_antenna(struct device *d,
4322 struct device_attribute *attr, char *buf)
4323{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004324 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004325
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004326 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004327 return -EAGAIN;
4328
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08004329 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07004330}
4331
4332static ssize_t store_antenna(struct device *d,
4333 struct device_attribute *attr,
4334 const char *buf, size_t count)
4335{
Winkler, Tomas7530f852009-01-27 14:27:53 -08004336 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004337 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07004338
4339 if (count == 0)
4340 return 0;
4341
4342 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08004343 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004344 return count;
4345 }
4346
4347 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08004348 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08004349 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07004350 } else
Tomas Winklere1623442009-01-27 14:27:56 -08004351 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07004352
4353
4354 return count;
4355}
4356
4357static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
4358
4359static ssize_t show_status(struct device *d,
4360 struct device_attribute *attr, char *buf)
4361{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004362 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004363 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004364 return -EAGAIN;
4365 return sprintf(buf, "0x%08x\n", (int)priv->status);
4366}
4367
4368static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4369
4370static ssize_t dump_error_log(struct device *d,
4371 struct device_attribute *attr,
4372 const char *buf, size_t count)
4373{
4374 char *p = (char *)buf;
4375
4376 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004377 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07004378
4379 return strnlen(buf, count);
4380}
4381
4382static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
4383
4384static ssize_t dump_event_log(struct device *d,
4385 struct device_attribute *attr,
4386 const char *buf, size_t count)
4387{
4388 char *p = (char *)buf;
4389
4390 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004391 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07004392
4393 return strnlen(buf, count);
4394}
4395
4396static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
4397
4398/*****************************************************************************
4399 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07004400 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07004401 *
4402 *****************************************************************************/
4403
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004404static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004405{
Reinette Chatred21050c2009-02-13 11:51:18 -08004406 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07004407
4408 init_waitqueue_head(&priv->wait_command_queue);
4409
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004410 INIT_WORK(&priv->up, iwl3945_bg_up);
4411 INIT_WORK(&priv->restart, iwl3945_bg_restart);
4412 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004413 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004414 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004415 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
4416 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Helmut Schaa26635162009-01-15 09:38:44 +01004417 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08004418 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4419 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
4420 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
4421 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07004422
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004423 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004424
4425 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004426 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07004427}
4428
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004429static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004430{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004431 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004432
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09004433 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004434 cancel_delayed_work(&priv->scan_check);
4435 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004436 cancel_work_sync(&priv->beacon_update);
4437}
4438
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004439static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07004440 &dev_attr_antenna.attr,
4441 &dev_attr_channels.attr,
4442 &dev_attr_dump_errors.attr,
4443 &dev_attr_dump_events.attr,
4444 &dev_attr_flags.attr,
4445 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004446#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004447 &dev_attr_measurement.attr,
4448#endif
4449 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004450 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004451 &dev_attr_statistics.attr,
4452 &dev_attr_status.attr,
4453 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004454 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08004455#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004456 &dev_attr_debug_level.attr,
4457#endif
Zhu Yib481de92007-09-25 17:54:57 -07004458 NULL
4459};
4460
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004461static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07004462 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004463 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07004464};
4465
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004466static struct ieee80211_ops iwl3945_hw_ops = {
4467 .tx = iwl3945_mac_tx,
4468 .start = iwl3945_mac_start,
4469 .stop = iwl3945_mac_stop,
4470 .add_interface = iwl3945_mac_add_interface,
4471 .remove_interface = iwl3945_mac_remove_interface,
4472 .config = iwl3945_mac_config,
4473 .config_interface = iwl3945_mac_config_interface,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004474 .configure_filter = iwl_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004475 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004476 .get_tx_stats = iwl3945_mac_get_tx_stats,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07004477 .conf_tx = iwl_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004478 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07004479 .bss_info_changed = iwl_bss_info_changed,
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -08004480 .hw_scan = iwl_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07004481};
4482
Winkler, Tomase52119c2008-12-22 11:31:19 +08004483static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004484{
4485 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08004486 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004487
4488 priv->retry_rate = 1;
4489 priv->ibss_beacon = NULL;
4490
4491 spin_lock_init(&priv->lock);
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08004492 spin_lock_init(&priv->power_data.lock);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004493 spin_lock_init(&priv->sta_lock);
4494 spin_lock_init(&priv->hcmd_lock);
4495
4496 INIT_LIST_HEAD(&priv->free_frames);
4497
4498 mutex_init(&priv->mutex);
4499
4500 /* Clear the driver's (not device's) station table */
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07004501 priv->cfg->ops->smgmt->clear_station_table(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004502
4503 priv->data_retry_limit = -1;
4504 priv->ieee_channels = NULL;
4505 priv->ieee_rates = NULL;
4506 priv->band = IEEE80211_BAND_2GHZ;
4507
4508 priv->iw_mode = NL80211_IFTYPE_STATION;
4509
4510 iwl_reset_qos(priv);
4511
4512 priv->qos_data.qos_active = 0;
4513 priv->qos_data.qos_cap.val = 0;
4514
4515 priv->rates_mask = IWL_RATES_MASK;
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004516 /* If power management is turned on, default to CAM mode */
4517 priv->power_mode = IWL_POWER_MODE_CAM;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08004518 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004519
Samuel Ortize6148912009-01-23 13:45:15 -08004520 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
4521 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
4522 eeprom->version);
4523 ret = -EINVAL;
4524 goto err;
4525 }
4526 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004527 if (ret) {
4528 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
4529 goto err;
4530 }
4531
Samuel Ortize6148912009-01-23 13:45:15 -08004532 /* Set up txpower settings in driver for all channels */
4533 if (iwl3945_txpower_set_from_eeprom(priv)) {
4534 ret = -EIO;
4535 goto err_free_channel_map;
4536 }
4537
Samuel Ortiz534166d2009-01-23 13:45:16 -08004538 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004539 if (ret) {
4540 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
4541 goto err_free_channel_map;
4542 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08004543 iwl3945_init_hw_rates(priv, priv->ieee_rates);
4544
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004545 return 0;
4546
4547err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08004548 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004549err:
4550 return ret;
4551}
4552
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004553static int iwl3945_setup_mac(struct iwl_priv *priv)
4554{
4555 int ret;
4556 struct ieee80211_hw *hw = priv->hw;
4557
4558 hw->rate_control_algorithm = "iwl-3945-rs";
4559 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
4560
4561 /* Tell mac80211 our characteristics */
4562 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Mohamed Abbasb1c60192009-03-17 21:51:47 -07004563 IEEE80211_HW_NOISE_DBM |
4564 IEEE80211_HW_SPECTRUM_MGMT;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004565
4566 hw->wiphy->interface_modes =
4567 BIT(NL80211_IFTYPE_STATION) |
4568 BIT(NL80211_IFTYPE_ADHOC);
4569
4570 hw->wiphy->custom_regulatory = true;
4571
Johannes Bergd60cc912009-04-09 09:56:02 +02004572 hw->wiphy->max_scan_ssids = 1; /* WILL FIX */
4573
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004574 /* Default value; 4 EDCA QOS priorities */
4575 hw->queues = 4;
4576
4577 hw->conf.beacon_int = 100;
4578
4579 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4580 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4581 &priv->bands[IEEE80211_BAND_2GHZ];
4582
4583 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4584 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4585 &priv->bands[IEEE80211_BAND_5GHZ];
4586
4587 ret = ieee80211_register_hw(priv->hw);
4588 if (ret) {
4589 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
4590 return ret;
4591 }
4592 priv->mac80211_registered = 1;
4593
4594 return 0;
4595}
4596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004597static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07004598{
4599 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004600 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07004601 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08004602 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08004603 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004604 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004605
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004606 /***********************
4607 * 1. Allocating HW data
4608 * ********************/
4609
Zhu Yib481de92007-09-25 17:54:57 -07004610 /* mac80211 allocates memory for this device instance, including
4611 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004612 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07004613 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004614 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07004615 err = -ENOMEM;
4616 goto out;
4617 }
Zhu Yib481de92007-09-25 17:54:57 -07004618 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004619 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004620
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004621 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
4622 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004623 IWL_ERR(priv,
4624 "invalid queues_num, should be between %d and %d\n",
4625 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08004626 err = -EINVAL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004627 goto out_ieee80211_free_hw;
Samuel Ortiza3139c52008-12-19 10:37:09 +08004628 }
4629
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004630 /*
4631 * Disabling hardware scan means that mac80211 will perform scans
4632 * "the hard way", rather than using device's scan.
4633 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004634 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08004635 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004636 iwl3945_hw_ops.hw_scan = NULL;
4637 }
4638
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004639
Tomas Winklere1623442009-01-27 14:27:56 -08004640 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004641 priv->cfg = cfg;
4642 priv->pci_dev = pdev;
4643
Samuel Ortizd08853a2009-01-23 13:45:17 -08004644#ifdef CONFIG_IWLWIFI_DEBUG
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004645 priv->debug_level = iwl3945_mod_params.debug;
4646 atomic_set(&priv->restrict_refcnt, 0);
4647#endif
Zhu Yib481de92007-09-25 17:54:57 -07004648
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004649 /***************************
4650 * 2. Initializing PCI bus
4651 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07004652 if (pci_enable_device(pdev)) {
4653 err = -ENODEV;
4654 goto out_ieee80211_free_hw;
4655 }
4656
4657 pci_set_master(pdev);
4658
Yang Hongyang284901a2009-04-06 19:01:15 -07004659 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004660 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004661 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004662 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004663 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004664 goto out_pci_disable_device;
4665 }
4666
4667 pci_set_drvdata(pdev, priv);
4668 err = pci_request_regions(pdev, DRV_NAME);
4669 if (err)
4670 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004671
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004672 /***********************
4673 * 3. Read REV Register
4674 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004675 priv->hw_base = pci_iomap(pdev, 0, 0);
4676 if (!priv->hw_base) {
4677 err = -ENODEV;
4678 goto out_pci_release_regions;
4679 }
4680
Tomas Winklere1623442009-01-27 14:27:56 -08004681 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004682 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004683 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004684
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004685 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4686 * PCI Tx retries from interfering with C3 CPU state */
4687 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004688
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004689 /* amp init */
4690 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004691 if (err < 0) {
David S. Millerd5df2a12009-03-10 05:04:16 -07004692 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004693 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004694 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004695
4696 /***********************
4697 * 4. Read EEPROM
4698 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004699
Zhu Yi5a669262008-01-14 17:46:18 -08004700 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004701 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004702 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004703 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004704 goto out_iounmap;
Zhu Yi5a669262008-01-14 17:46:18 -08004705 }
4706 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004707 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4708 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08004709 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08004710 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4711
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004712 /***********************
4713 * 5. Setup HW Constants
4714 * ********************/
4715 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004716 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004717 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004718 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004719 }
4720
4721 /***********************
4722 * 6. Setup priv
4723 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004724
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004725 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004726 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004727 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004728 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004729 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004730
Tomas Winkler978785a2008-12-19 10:37:31 +08004731 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4732 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004733
4734 /***********************************
4735 * 7. Initialize Module Parameters
4736 * **********************************/
4737
4738 /* Initialize module parameter values here */
4739 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004740 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004741 set_bit(STATUS_RF_KILL_SW, &priv->status);
Tomas Winklere1623442009-01-27 14:27:56 -08004742 IWL_DEBUG_INFO(priv, "Radio disabled.\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004743 }
4744
4745
4746 /***********************
4747 * 8. Setup Services
4748 * ********************/
4749
4750 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004751 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004752 spin_unlock_irqrestore(&priv->lock, flags);
4753
Helmut Schaa26635162009-01-15 09:38:44 +01004754 pci_enable_msi(priv->pci_dev);
4755
Abhijeet Kolekarf17d08a2009-02-18 15:54:31 -08004756 err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
Helmut Schaa26635162009-01-15 09:38:44 +01004757 DRV_NAME, priv);
4758 if (err) {
4759 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4760 goto out_disable_msi;
4761 }
4762
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004763 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4764 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004765 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004766 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004767 }
4768
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004769 iwl_set_rxon_channel(priv,
4770 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004771 iwl3945_setup_deferred_work(priv);
4772 iwl3945_setup_rx_handlers(priv);
4773
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004774 /*********************************
Helmut Schaa26635162009-01-15 09:38:44 +01004775 * 9. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004776 * *******************************/
4777
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004778 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004779
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004780 err = iwl3945_setup_mac(priv);
4781 if (err)
4782 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004783
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004784 err = iwl_rfkill_init(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004785 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004786 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004787 "Ignoring error: %d\n", err);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004788 else
4789 iwl_rfkill_set_hw_state(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004790
Helmut Schaa26635162009-01-15 09:38:44 +01004791 /* Start monitoring the killswitch */
4792 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
4793 2 * HZ);
4794
Zhu Yib481de92007-09-25 17:54:57 -07004795 return 0;
4796
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004797 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004798 destroy_workqueue(priv->workqueue);
4799 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004800 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4801 out_release_irq:
4802 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004803 out_disable_msi:
4804 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004805 iwlcore_free_geos(priv);
4806 iwl_free_channel_map(priv);
4807 out_unset_hw_params:
4808 iwl3945_unset_hw_params(priv);
4809 out_eeprom_free:
4810 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004811 out_iounmap:
4812 pci_iounmap(pdev, priv->hw_base);
4813 out_pci_release_regions:
4814 pci_release_regions(pdev);
4815 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004816 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004817 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004818 out_ieee80211_free_hw:
4819 ieee80211_free_hw(priv->hw);
4820 out:
4821 return err;
4822}
4823
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004824static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004825{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004826 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004827 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004828
4829 if (!priv)
4830 return;
4831
Tomas Winklere1623442009-01-27 14:27:56 -08004832 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004833
Zhu Yib481de92007-09-25 17:54:57 -07004834 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004835
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004836 if (priv->mac80211_registered) {
4837 ieee80211_unregister_hw(priv->hw);
4838 priv->mac80211_registered = 0;
4839 } else {
4840 iwl3945_down(priv);
4841 }
Zhu Yib481de92007-09-25 17:54:57 -07004842
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004843 /* make sure we flush any pending irq or
4844 * tasklet for the driver
4845 */
4846 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004847 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004848 spin_unlock_irqrestore(&priv->lock, flags);
4849
4850 iwl_synchronize_irq(priv);
4851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004852 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004853
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004854 iwl_rfkill_unregister(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004855 cancel_delayed_work(&priv->rfkill_poll);
4856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004857 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004858
4859 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08004860 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004861 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004862
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004863 iwl3945_unset_hw_params(priv);
Abhijeet Kolekar40ace5b2009-04-08 11:26:40 -07004864 priv->cfg->ops->smgmt->clear_station_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004865
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004866 /*netif_stop_queue(dev); */
4867 flush_workqueue(priv->workqueue);
4868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004869 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004870 * priv->workqueue... so we can't take down the workqueue
4871 * until now... */
4872 destroy_workqueue(priv->workqueue);
4873 priv->workqueue = NULL;
4874
Helmut Schaa26635162009-01-15 09:38:44 +01004875 free_irq(pdev->irq, priv);
4876 pci_disable_msi(pdev);
4877
Zhu Yib481de92007-09-25 17:54:57 -07004878 pci_iounmap(pdev, priv->hw_base);
4879 pci_release_regions(pdev);
4880 pci_disable_device(pdev);
4881 pci_set_drvdata(pdev, NULL);
4882
Samuel Ortize6148912009-01-23 13:45:15 -08004883 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004884 iwlcore_free_geos(priv);
Winkler, Tomas805cee52009-01-19 15:30:28 -08004885 kfree(priv->scan);
Zhu Yib481de92007-09-25 17:54:57 -07004886 if (priv->ibss_beacon)
4887 dev_kfree_skb(priv->ibss_beacon);
4888
4889 ieee80211_free_hw(priv->hw);
4890}
4891
Zhu Yib481de92007-09-25 17:54:57 -07004892
4893/*****************************************************************************
4894 *
4895 * driver and module entry point
4896 *
4897 *****************************************************************************/
4898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004899static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004900 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004901 .id_table = iwl3945_hw_card_ids,
4902 .probe = iwl3945_pci_probe,
4903 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004904#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004905 .suspend = iwl_pci_suspend,
4906 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004907#endif
4908};
4909
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004910static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004911{
4912
4913 int ret;
4914 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4915 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004916
4917 ret = iwl3945_rate_control_register();
4918 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004919 printk(KERN_ERR DRV_NAME
4920 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004921 return ret;
4922 }
4923
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004924 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004925 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004926 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004927 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004928 }
Zhu Yib481de92007-09-25 17:54:57 -07004929
4930 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004931
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004932error_register:
4933 iwl3945_rate_control_unregister();
4934 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004935}
4936
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004937static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004938{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004939 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004940 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004941}
4942
Reinette Chatrea0987a82008-12-02 12:14:06 -08004943MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004944
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004945module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004946MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004947module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004948MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004949module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
4950MODULE_PARM_DESC(swcrypto,
4951 "using software crypto (default 1 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004952module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004953MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004954module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004955MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
4956
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004957module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004958MODULE_PARM_DESC(queues_num, "number of hw queues.");
4959
Samuel Ortizaf48d042009-01-23 13:45:19 -08004960module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
4961MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4962
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004963module_exit(iwl3945_exit);
4964module_init(iwl3945_init);