blob: e96a726dc5c94827c1bf44826875ca6a4663b4b8 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
Zhu Yib481de92007-09-25 17:54:57 -070032#include <linux/init.h>
33#include <linux/pci.h>
34#include <linux/dma-mapping.h>
35#include <linux/delay.h>
36#include <linux/skbuff.h>
37#include <linux/netdevice.h>
38#include <linux/wireless.h>
39#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070040#include <linux/etherdevice.h>
41#include <linux/if_arp.h>
42
43#include <net/ieee80211_radiotap.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040044#include <net/lib80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070045#include <net/mac80211.h>
46
47#include <asm/div64.h>
48
Samuel Ortiza3139c52008-12-19 10:37:09 +080049#define DRV_NAME "iwl3945"
50
Winkler, Tomasdbb66542008-12-22 11:31:14 +080051#include "iwl-fh.h"
52#include "iwl-3945-fh.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080053#include "iwl-commands.h"
Samuel Ortiz17f841c2009-01-23 13:45:20 -080054#include "iwl-sta.h"
Zhu Yib481de92007-09-25 17:54:57 -070055#include "iwl-3945.h"
56#include "iwl-helpers.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080057#include "iwl-core.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080058#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070059
Zhu Yib481de92007-09-25 17:54:57 -070060/*
61 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070062 */
63
64#define DRV_DESCRIPTION \
65"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
66
Samuel Ortizd08853a2009-01-23 13:45:17 -080067#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070068#define VD "d"
69#else
70#define VD
71#endif
72
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080073#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070074#define VS "s"
75#else
76#define VS
77#endif
78
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080079#define IWL39_VERSION "1.2.26k" VD VS
Reinette Chatre01f81622009-01-08 10:20:02 -080080#define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080081#define DRV_AUTHOR "<ilw@linux.intel.com>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080082#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070083
Zhu Yib481de92007-09-25 17:54:57 -070084
85MODULE_DESCRIPTION(DRV_DESCRIPTION);
86MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080087MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070088MODULE_LICENSE("GPL");
89
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080090 /* module parameters */
91struct iwl_mod_params iwl3945_mod_params = {
92 .num_of_queues = IWL39_MAX_NUM_QUEUES,
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080093 .sw_crypto = 1,
Samuel Ortizaf48d042009-01-23 13:45:19 -080094 .restart_fw = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080095 /* the rest are 0 by default */
96};
97
Zhu Yib481de92007-09-25 17:54:57 -070098/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +080099 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700100 * the functionality provided here
101 */
102
103/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800104#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800105/**
106 * iwl3945_remove_station - Remove driver's knowledge of station.
107 *
108 * NOTE: This does not remove station from device's station table.
109 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800110static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700111{
112 int index = IWL_INVALID_STATION;
113 int i;
114 unsigned long flags;
115
116 spin_lock_irqsave(&priv->sta_lock, flags);
117
118 if (is_ap)
119 index = IWL_AP_ID;
120 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800121 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700122 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800123 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800124 if (priv->stations_39[i].used &&
125 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700126 addr)) {
127 index = i;
128 break;
129 }
130
131 if (unlikely(index == IWL_INVALID_STATION))
132 goto out;
133
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800134 if (priv->stations_39[index].used) {
135 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700136 priv->num_stations--;
137 }
138
139 BUG_ON(priv->num_stations < 0);
140
141out:
142 spin_unlock_irqrestore(&priv->sta_lock, flags);
143 return 0;
144}
Zhu Yi556f8db2007-09-27 11:27:33 +0800145#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800146
147/**
148 * iwl3945_clear_stations_table - Clear the driver's station table
149 *
150 * NOTE: This does not clear or otherwise alter the device's station table.
151 */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700152void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700153{
154 unsigned long flags;
155
156 spin_lock_irqsave(&priv->sta_lock, flags);
157
158 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800159 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700160
161 spin_unlock_irqrestore(&priv->sta_lock, flags);
162}
163
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800164/**
165 * iwl3945_add_station - Add station to station tables in driver and device
166 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800167u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700168{
169 int i;
170 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800171 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700172 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800173 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700174
175 spin_lock_irqsave(&priv->sta_lock, flags_spin);
176 if (is_ap)
177 index = IWL_AP_ID;
178 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800179 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700180 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800181 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800182 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700183 addr)) {
184 index = i;
185 break;
186 }
187
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800188 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700189 index == IWL_INVALID_STATION)
190 index = i;
191 }
192
Ian Schram01ebd062007-10-25 17:15:22 +0800193 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700194 since they have different meaning */
195 if (unlikely(index == IWL_INVALID_STATION)) {
196 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
197 return index;
198 }
199
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800200 if (priv->stations_39[index].used &&
201 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700202 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
203 return index;
204 }
205
Tomas Winklere1623442009-01-27 14:27:56 -0800206 IWL_DEBUG_ASSOC(priv, "Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800207 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700208 station->used = 1;
209 priv->num_stations++;
210
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800211 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800212 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700213 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
214 station->sta.mode = 0;
215 station->sta.sta.sta_id = index;
216 station->sta.station_flags = 0;
217
Johannes Berg8318d782008-01-24 19:38:38 +0100218 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800219 rate = IWL_RATE_6M_PLCP;
220 else
221 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800222
223 /* Turn on both antennas for the station... */
224 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800225 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800226
Zhu Yib481de92007-09-25 17:54:57 -0700227 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800228
229 /* Add station to device's station table */
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800230 iwl_send_add_sta(priv,
231 (struct iwl_addsta_cmd *)&station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700232 return index;
233
234}
235
Zhu Yib481de92007-09-25 17:54:57 -0700236/**
Samuel Ortiz7e4bca52009-01-23 13:45:18 -0800237 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
238 * @priv: eeprom and antenna fields are used to determine antenna flags
239 *
240 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
241 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
242 *
243 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
244 * IWL_ANTENNA_MAIN - Force MAIN antenna
245 * IWL_ANTENNA_AUX - Force AUX antenna
246 */
247__le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
248{
249 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
250
251 switch (iwl3945_mod_params.antenna) {
252 case IWL_ANTENNA_DIVERSITY:
253 return 0;
254
255 case IWL_ANTENNA_MAIN:
256 if (eeprom->antenna_switch_type)
257 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
258 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
259
260 case IWL_ANTENNA_AUX:
261 if (eeprom->antenna_switch_type)
262 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
263 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
264 }
265
266 /* bad antenna selector value */
267 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
268 iwl3945_mod_params.antenna);
269
270 return 0; /* "diversity" is default if error */
271}
272
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800273static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700274 struct ieee80211_key_conf *keyconf,
275 u8 sta_id)
276{
277 unsigned long flags;
278 __le16 key_flags = 0;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800279 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700280
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800281 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
282 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
283
284 if (sta_id == priv->hw_params.bcast_sta_id)
285 key_flags |= STA_KEY_MULTICAST_MSK;
286
287 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
288 keyconf->hw_key_idx = keyconf->keyidx;
289 key_flags &= ~STA_KEY_FLG_INVALID;
290
Zhu Yib481de92007-09-25 17:54:57 -0700291 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800292 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
293 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
294 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700295 keyconf->keylen);
296
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800297 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700298 keyconf->keylen);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800299
Abhijeet Kolekar43da9192009-03-17 21:51:53 -0700300 if ((priv->stations_39[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800301 == STA_KEY_FLG_NO_ENC)
Abhijeet Kolekar43da9192009-03-17 21:51:53 -0700302 priv->stations_39[sta_id].sta.key.key_offset =
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800303 iwl_get_free_ucode_key_index(priv);
304 /* else, we are overriding an existing key => no need to allocated room
305 * in uCode. */
306
Abhijeet Kolekar43da9192009-03-17 21:51:53 -0700307 WARN(priv->stations_39[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800308 "no space for a new key");
309
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800310 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
311 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
312 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700313
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800314 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
315
316 ret = iwl_send_add_sta(priv,
317 (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, CMD_ASYNC);
318
Zhu Yib481de92007-09-25 17:54:57 -0700319 spin_unlock_irqrestore(&priv->sta_lock, flags);
320
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800321 return ret;
322}
323
324static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
325 struct ieee80211_key_conf *keyconf,
326 u8 sta_id)
327{
328 return -EOPNOTSUPP;
329}
330
331static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
332 struct ieee80211_key_conf *keyconf,
333 u8 sta_id)
334{
335 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -0700336}
337
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800338static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700339{
340 unsigned long flags;
341
342 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800343 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
344 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +0800345 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800346 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
347 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
348 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700349 spin_unlock_irqrestore(&priv->sta_lock, flags);
350
Tomas Winklere1623442009-01-27 14:27:56 -0800351 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
Samuel Ortiz17f841c2009-01-23 13:45:20 -0800352 iwl_send_add_sta(priv,
353 (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700354 return 0;
355}
356
Abhijeet Kolekarfa11d522009-03-11 11:17:54 -0700357static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800358 struct ieee80211_key_conf *keyconf, u8 sta_id)
359{
360 int ret = 0;
361
362 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
363
364 switch (keyconf->alg) {
365 case ALG_CCMP:
366 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
367 break;
368 case ALG_TKIP:
369 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
370 break;
371 case ALG_WEP:
372 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
373 break;
374 default:
Abhijeet Kolekar1e680232009-03-17 21:51:50 -0700375 IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800376 ret = -EINVAL;
377 }
378
379 IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
380 keyconf->alg, keyconf->keylen, keyconf->keyidx,
381 sta_id, ret);
382
383 return ret;
384}
385
386static int iwl3945_remove_static_key(struct iwl_priv *priv)
387{
388 int ret = -EOPNOTSUPP;
389
390 return ret;
391}
392
393static int iwl3945_set_static_key(struct iwl_priv *priv,
394 struct ieee80211_key_conf *key)
395{
396 if (key->alg == ALG_WEP)
397 return -EOPNOTSUPP;
398
399 IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
400 return -EINVAL;
401}
402
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800403static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700404{
405 struct list_head *element;
406
Tomas Winklere1623442009-01-27 14:27:56 -0800407 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700408 priv->frames_count);
409
410 while (!list_empty(&priv->free_frames)) {
411 element = priv->free_frames.next;
412 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800413 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700414 priv->frames_count--;
415 }
416
417 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800418 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700419 priv->frames_count);
420 priv->frames_count = 0;
421 }
422}
423
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800424static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700425{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800426 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700427 struct list_head *element;
428 if (list_empty(&priv->free_frames)) {
429 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
430 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800431 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700432 return NULL;
433 }
434
435 priv->frames_count++;
436 return frame;
437 }
438
439 element = priv->free_frames.next;
440 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800441 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700442}
443
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800444static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700445{
446 memset(frame, 0, sizeof(*frame));
447 list_add(&frame->list, &priv->free_frames);
448}
449
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800450unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700451 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200452 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700453{
454
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800455 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200456 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
457 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -0700458 return 0;
459
460 if (priv->ibss_beacon->len > left)
461 return 0;
462
463 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
464
465 return priv->ibss_beacon->len;
466}
467
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800468static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700469{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800470 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700471 unsigned int frame_size;
472 int rc;
473 u8 rate;
474
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800475 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700476
477 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800478 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700479 "command.\n");
480 return -ENOMEM;
481 }
482
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800483 rate = iwl_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800485 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700486
Samuel Ortiz518099a2009-01-19 15:30:27 -0800487 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700488 &frame->u.cmd[0]);
489
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800490 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700491
492 return rc;
493}
494
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800495static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700496{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800497 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -0700498 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800499 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800500 priv->shared_virt,
501 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700502}
503
Zhu Yib481de92007-09-25 17:54:57 -0700504#define MAX_UCODE_BEACON_INTERVAL 1024
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800505#define INTEL_CONN_LISTEN_INTERVAL cpu_to_le16(0xA)
Zhu Yib481de92007-09-25 17:54:57 -0700506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800507static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -0700508{
509 u16 new_val = 0;
510 u16 beacon_factor = 0;
511
512 beacon_factor =
513 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
514 / MAX_UCODE_BEACON_INTERVAL;
515 new_val = beacon_val / beacon_factor;
516
517 return cpu_to_le16(new_val);
518}
519
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800520static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700521{
522 u64 interval_tm_unit;
523 u64 tsf, result;
524 unsigned long flags;
525 struct ieee80211_conf *conf = NULL;
526 u16 beacon_int = 0;
527
528 conf = ieee80211_get_hw_conf(priv->hw);
529
530 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +0800531 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -0700532 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
533
Tomas Winkler28afaf92008-12-19 10:37:06 +0800534 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -0700535
536 beacon_int = priv->beacon_int;
537 spin_unlock_irqrestore(&priv->lock, flags);
538
Johannes Berg05c914f2008-09-11 00:01:58 +0200539 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -0700540 if (beacon_int == 0) {
541 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
542 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
543 } else {
544 priv->rxon_timing.beacon_interval =
545 cpu_to_le16(beacon_int);
546 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800547 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -0700548 le16_to_cpu(priv->rxon_timing.beacon_interval));
549 }
550
551 priv->rxon_timing.atim_window = 0;
552 } else {
553 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800554 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -0700555 /* TODO: we need to get atim_window from upper stack
556 * for now we set to 0 */
557 priv->rxon_timing.atim_window = 0;
558 }
559
560 interval_tm_unit =
561 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
562 result = do_div(tsf, interval_tm_unit);
563 priv->rxon_timing.beacon_init_val =
564 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
565
Tomas Winklere1623442009-01-27 14:27:56 -0800566 IWL_DEBUG_ASSOC(priv,
567 "beacon interval %d beacon timer %d beacon tim %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700568 le16_to_cpu(priv->rxon_timing.beacon_interval),
569 le32_to_cpu(priv->rxon_timing.beacon_init_val),
570 le16_to_cpu(priv->rxon_timing.atim_window));
571}
572
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800573static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700574{
Johannes Berg05c914f2008-09-11 00:01:58 +0200575 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800576 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -0700577
Samuel Ortize6148912009-01-23 13:45:15 -0800578 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100579 priv->band,
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800580 le16_to_cpu(priv->staging_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -0700581
582 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800583 IWL_ERR(priv, "channel %d not IBSS channel\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800584 le16_to_cpu(priv->staging_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -0700585 return -EINVAL;
586 }
587 }
588
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800589 iwl_connection_init_rx_config(priv, mode);
Zhu Yib481de92007-09-25 17:54:57 -0700590
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800591 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700592
Tomas Winklera96a27f2008-10-23 23:48:56 -0700593 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800594 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +0800595 return -EAGAIN;
596
597 cancel_delayed_work(&priv->scan_check);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800598 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800599 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Tomas Winklere1623442009-01-27 14:27:56 -0800600 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +0800601 return -EAGAIN;
602 }
603
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700604 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700605
606 return 0;
607}
608
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800609static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200610 struct ieee80211_tx_info *info,
Winkler, 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:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800737 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700738 if (sta_id != IWL_INVALID_STATION)
739 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800740 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700741
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800742 /* If this frame is going out to an IBSS network, find the station,
743 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +0200744 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800745 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800746 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700747 if (sta_id != IWL_INVALID_STATION)
748 return sta_id;
749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800750 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -0700751
752 if (sta_id != IWL_INVALID_STATION)
753 return sta_id;
754
Tomas Winklere1623442009-01-27 14:27:56 -0800755 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -0700756 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -0700757 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +0800758 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800759 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -0700760 }
Stefanik Gábor914233d2008-06-30 17:23:30 +0800761 /* If we are in monitor mode, use BCAST. This is required for
762 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +0200763 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800764 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +0800765
Zhu Yib481de92007-09-25 17:54:57 -0700766 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800767 IWL_WARN(priv, "Unknown mode of operation: %d\n",
768 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800769 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700770 }
771}
772
773/*
774 * start REPLY_TX command process
775 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800776static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700777{
778 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200779 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomase52119c2008-12-22 11:31:19 +0800780 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800781 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800782 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800783 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700784 dma_addr_t phys_addr;
785 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800786 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +0800787 u16 len, idx, len_org, hdr_len;
788 u8 id;
789 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700790 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800791 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700792 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700793 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700794 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800795 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700796 unsigned long flags;
797 int rc;
798
799 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800800 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800801 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700802 goto drop_unlock;
803 }
804
Johannes Berge039fa42008-05-15 12:55:29 +0200805 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800806 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700807 goto drop_unlock;
808 }
809
810 unicast = !is_multicast_ether_addr(hdr->addr1);
811 id = 0;
812
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700813 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700814
Samuel Ortizd08853a2009-01-23 13:45:17 -0800815#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700816 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800817 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700818 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800819 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700820 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800821 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700822#endif
823
Mohamed Abbas7878a5a2007-11-29 11:10:13 +0800824 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +0800825 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200826 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800827 (!iwl_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200828 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Tomas Winklere1623442009-01-27 14:27:56 -0800829 IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -0700830 goto drop_unlock;
831 }
832
833 spin_unlock_irqrestore(&priv->lock, flags);
834
Harvey Harrison7294ec92008-07-15 18:43:59 -0700835 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800836
837 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800838 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -0700839 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800840 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700841 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700842 goto drop;
843 }
844
Tomas Winklere1623442009-01-27 14:27:56 -0800845 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700846
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700847 if (ieee80211_is_data_qos(fc)) {
848 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700849 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800850 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -0700851 IEEE80211_SCTL_SEQ;
852 hdr->seq_ctrl = cpu_to_le16(seq_number) |
853 (hdr->seq_ctrl &
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800854 cpu_to_le16(IEEE80211_SCTL_FRAG));
Zhu Yib481de92007-09-25 17:54:57 -0700855 seq_number += 0x10;
856 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800857
858 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800859 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700860 q = &txq->q;
861
862 spin_lock_irqsave(&priv->lock, flags);
863
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800864 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700865
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800866 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800867 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800868 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800869
870 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800871 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +0800872 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700873 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +0800874 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800875
876 /*
877 * Set up the Tx-command (not MAC!) header.
878 * Store the chosen Tx queue and TFD index within the sequence field;
879 * after Tx, uCode's Tx response will return this value so driver can
880 * locate the frame within the tx queue and do post-tx processing.
881 */
Zhu Yib481de92007-09-25 17:54:57 -0700882 out_cmd->hdr.cmd = REPLY_TX;
883 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800884 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800885
886 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +0800887 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700888
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800889 /*
890 * Use the first empty entry in this queue's command buffer array
891 * to contain the Tx command and MAC header concatenated together
892 * (payload data will be in another buffer).
893 * Size of this varies, due to varying MAC header length.
894 * If end is not dword aligned, we'll have 2 extra bytes at the end
895 * of the MAC header (device reads on dword boundaries).
896 * We'll tell device about this padding later.
897 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800898 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800899 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700900
901 len_org = len;
902 len = (len + 3) & ~3;
903
904 if (len_org != len)
905 len_org = 1;
906 else
907 len_org = 0;
908
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800909 /* Physical address of this Tx command's header (not MAC header!),
910 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800911 txcmd_phys = pci_map_single(priv->pci_dev,
912 out_cmd, sizeof(struct iwl_cmd),
913 PCI_DMA_TODEVICE);
914 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
915 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
916 /* Add buffer containing Tx command and MAC(!) header to TFD's
917 * first entry */
918 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -0700919
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800920 /* Add buffer containing Tx command and MAC(!) header to TFD's
921 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800922 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
923 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700924
Johannes Bergd0f09802008-07-29 11:32:07 +0200925 if (info->control.hw_key)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800926 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700927
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800928 /* Set up TFD's 2nd entry to point directly to remainder of skb,
929 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700930 len = skb->len - hdr_len;
931 if (len) {
932 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
933 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800934 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
935 phys_addr, len,
936 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700937 }
938
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800939 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -0700940 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800941 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -0700942
943 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +0800944 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700945
946 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +0200947 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700948
Winkler, Tomase52119c2008-12-22 11:31:19 +0800949 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
950 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700951
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700952 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -0700953 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300954 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800955 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -0700956 } else {
957 wait_write_ptr = 1;
958 txq->need_update = 0;
959 }
960
Winkler, Tomase52119c2008-12-22 11:31:19 +0800961 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -0700962
Winkler, Tomase52119c2008-12-22 11:31:19 +0800963 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -0700964 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -0700965
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800966 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800967 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800968 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700969 spin_unlock_irqrestore(&priv->lock, flags);
970
971 if (rc)
972 return rc;
973
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800974 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700975 && priv->mac80211_registered) {
976 if (wait_write_ptr) {
977 spin_lock_irqsave(&priv->lock, flags);
978 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800979 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700980 spin_unlock_irqrestore(&priv->lock, flags);
981 }
982
Johannes Berge4e72fb2009-03-23 17:28:42 +0100983 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700984 }
985
986 return 0;
987
988drop_unlock:
989 spin_unlock_irqrestore(&priv->lock, flags);
990drop:
991 return -1;
992}
993
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800994#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -0700995
996#include "iwl-spectrum.h"
997
998#define BEACON_TIME_MASK_LOW 0x00FFFFFF
999#define BEACON_TIME_MASK_HIGH 0xFF000000
1000#define TIME_UNIT 1024
1001
1002/*
1003 * extended beacon time format
1004 * time in usec will be changed into a 32-bit value in 8:24 format
1005 * the high 1 byte is the beacon counts
1006 * the lower 3 bytes is the time in usec within one beacon interval
1007 */
1008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001009static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001010{
1011 u32 quot;
1012 u32 rem;
1013 u32 interval = beacon_interval * 1024;
1014
1015 if (!interval || !usec)
1016 return 0;
1017
1018 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1019 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1020
1021 return (quot << 24) + rem;
1022}
1023
1024/* base is usually what we get from ucode with each received frame,
1025 * the same as HW timer counter counting down
1026 */
1027
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001028static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001029{
1030 u32 base_low = base & BEACON_TIME_MASK_LOW;
1031 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1032 u32 interval = beacon_interval * TIME_UNIT;
1033 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1034 (addon & BEACON_TIME_MASK_HIGH);
1035
1036 if (base_low > addon_low)
1037 res += base_low - addon_low;
1038 else if (base_low < addon_low) {
1039 res += interval + base_low - addon_low;
1040 res += (1 << 24);
1041 } else
1042 res += (1 << 24);
1043
1044 return cpu_to_le32(res);
1045}
1046
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001047static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001048 struct ieee80211_measurement_params *params,
1049 u8 type)
1050{
Tomas Winkler600c0e12008-12-19 10:37:04 +08001051 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001052 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001053 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001054 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1055 .data = (void *)&spectrum,
1056 .meta.flags = CMD_WANT_SKB,
1057 };
1058 u32 add_time = le64_to_cpu(params->start_time);
1059 int rc;
1060 int spectrum_resp_status;
1061 int duration = le16_to_cpu(params->duration);
1062
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001063 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001064 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001065 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001066 le64_to_cpu(params->start_time) - priv->last_tsf,
1067 le16_to_cpu(priv->rxon_timing.beacon_interval));
1068
1069 memset(&spectrum, 0, sizeof(spectrum));
1070
1071 spectrum.channel_count = cpu_to_le16(1);
1072 spectrum.flags =
1073 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1074 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1075 cmd.len = sizeof(spectrum);
1076 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1077
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001078 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001079 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001080 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001081 add_time,
1082 le16_to_cpu(priv->rxon_timing.beacon_interval));
1083 else
1084 spectrum.start_time = 0;
1085
1086 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1087 spectrum.channels[0].channel = params->channel;
1088 spectrum.channels[0].type = type;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001089 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07001090 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1091 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1092
Samuel Ortiz518099a2009-01-19 15:30:27 -08001093 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001094 if (rc)
1095 return rc;
1096
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001097 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001098 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001099 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07001100 rc = -EIO;
1101 }
1102
1103 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1104 switch (spectrum_resp_status) {
1105 case 0: /* Command will be handled */
1106 if (res->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -08001107 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Ian Schrambc434dd2007-10-25 17:15:29 +08001108 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07001109 priv->measurement_status &= ~MEASUREMENT_READY;
1110 }
1111 priv->measurement_status |= MEASUREMENT_ACTIVE;
1112 rc = 0;
1113 break;
1114
1115 case 1: /* Command will not be handled */
1116 rc = -EAGAIN;
1117 break;
1118 }
1119
1120 dev_kfree_skb_any(cmd.meta.u.skb);
1121
1122 return rc;
1123}
1124#endif
1125
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001126static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001127 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001128{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001129 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1130 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001131 struct delayed_work *pwork;
1132
1133 palive = &pkt->u.alive_frame;
1134
Tomas Winklere1623442009-01-27 14:27:56 -08001135 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -07001136 "0x%01X 0x%01X\n",
1137 palive->is_valid, palive->ver_type,
1138 palive->ver_subtype);
1139
1140 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001141 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001142 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
1143 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001144 pwork = &priv->init_alive_start;
1145 } else {
Tomas Winklere1623442009-01-27 14:27:56 -08001146 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001147 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001148 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001149 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001150 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001151 }
1152
1153 /* We delay the ALIVE response by 5ms to
1154 * give the HW RF Kill time to activate... */
1155 if (palive->is_valid == UCODE_VALID_OK)
1156 queue_delayed_work(priv->workqueue, pwork,
1157 msecs_to_jiffies(5));
1158 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001159 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001160}
1161
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001162static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001163 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001164{
Helmut Schaac7e035a2009-01-19 13:02:15 +01001165#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001166 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Helmut Schaac7e035a2009-01-19 13:02:15 +01001167#endif
Zhu Yib481de92007-09-25 17:54:57 -07001168
Tomas Winklere1623442009-01-27 14:27:56 -08001169 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -07001170 return;
1171}
1172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001173static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07001174{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001175 struct iwl_priv *priv =
1176 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07001177 struct sk_buff *beacon;
1178
1179 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02001180 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07001181
1182 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001183 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001184 return;
1185 }
1186
1187 mutex_lock(&priv->mutex);
1188 /* new beacon skb is allocated every time; dispose previous.*/
1189 if (priv->ibss_beacon)
1190 dev_kfree_skb(priv->ibss_beacon);
1191
1192 priv->ibss_beacon = beacon;
1193 mutex_unlock(&priv->mutex);
1194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001195 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001196}
1197
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001198static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001199 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001200{
Samuel Ortizd08853a2009-01-23 13:45:17 -08001201#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001202 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001203 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07001204 u8 rate = beacon->beacon_notify_hdr.rate;
1205
Tomas Winklere1623442009-01-27 14:27:56 -08001206 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -07001207 "tsf %d %d rate %d\n",
1208 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1209 beacon->beacon_notify_hdr.failure_frame,
1210 le32_to_cpu(beacon->ibss_mgr_status),
1211 le32_to_cpu(beacon->high_tsf),
1212 le32_to_cpu(beacon->low_tsf), rate);
1213#endif
1214
Johannes Berg05c914f2008-09-11 00:01:58 +02001215 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07001216 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1217 queue_work(priv->workqueue, &priv->beacon_update);
1218}
1219
Zhu Yib481de92007-09-25 17:54:57 -07001220/* Handle notification from uCode that card's power state is changing
1221 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001222static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001223 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001224{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001225 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001226 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1227 unsigned long status = priv->status;
1228
Tomas Winklere1623442009-01-27 14:27:56 -08001229 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001230 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1231 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1232
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001233 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001234 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1235
1236 if (flags & HW_CARD_DISABLED)
1237 set_bit(STATUS_RF_KILL_HW, &priv->status);
1238 else
1239 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1240
1241
1242 if (flags & SW_CARD_DISABLED)
1243 set_bit(STATUS_RF_KILL_SW, &priv->status);
1244 else
1245 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1246
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08001247 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001248
1249 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1250 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1251 (test_bit(STATUS_RF_KILL_SW, &status) !=
1252 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1253 queue_work(priv->workqueue, &priv->rf_kill);
1254 else
1255 wake_up_interruptible(&priv->wait_command_queue);
1256}
1257
1258/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07001260 *
1261 * Setup the RX handlers for each of the reply types sent from the uCode
1262 * to the host.
1263 *
1264 * This function chains into the hardware specific files for them to setup
1265 * any hardware specific handlers as well.
1266 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001267static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001268{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001269 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
1270 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -08001271 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001272 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -08001273 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001274 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -08001275 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001276 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001277
Ben Cahill9fbab512007-11-29 11:09:47 +08001278 /*
1279 * The same handler is used for both the REPLY to a discrete
1280 * statistics request from the host as well as for the periodic
1281 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07001282 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001283 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
1284 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07001285
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -08001286 iwl_setup_spectrum_handlers(priv);
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -08001287 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001288 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07001289
Ben Cahill9fbab512007-11-29 11:09:47 +08001290 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001291 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001292}
1293
Zhu Yib481de92007-09-25 17:54:57 -07001294/************************** RX-FUNCTIONS ****************************/
1295/*
1296 * Rx theory of operation
1297 *
1298 * The host allocates 32 DMA target addresses and passes the host address
1299 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
1300 * 0 to 31
1301 *
1302 * Rx Queue Indexes
1303 * The host/firmware share two index registers for managing the Rx buffers.
1304 *
1305 * The READ index maps to the first position that the firmware may be writing
1306 * to -- the driver can read up to (but not including) this position and get
1307 * good data.
1308 * The READ index is managed by the firmware once the card is enabled.
1309 *
1310 * The WRITE index maps to the last position the driver has read from -- the
1311 * position preceding WRITE is the last slot the firmware can place a packet.
1312 *
1313 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
1314 * WRITE = READ.
1315 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001316 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07001317 * INDEX position, and WRITE to the last (READ - 1 wrapped)
1318 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001319 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07001320 * and fire the RX interrupt. The driver can then query the READ index and
1321 * process as many packets as possible, moving the WRITE index forward as it
1322 * resets the Rx queue buffers with new memory.
1323 *
1324 * The management in the driver is as follows:
1325 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
1326 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08001327 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001328 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07001329 * iwl->rxq is replenished and the READ INDEX is updated (updating the
1330 * 'processed' and 'read' driver indexes as well)
1331 * + A received packet is processed and handed to the kernel network stack,
1332 * detached from the iwl->rxq. The driver 'processed' index is updated.
1333 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1334 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1335 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
1336 * were enough free buffers and RX_STALLED is set it is cleared.
1337 *
1338 *
1339 * Driver sequence:
1340 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001341 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001342 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08001343 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07001344 * queue, updates firmware pointers, and updates
1345 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001346 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07001347 *
1348 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001349 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07001350 * READ INDEX, detaching the SKB from the pool.
1351 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001352 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07001353 * slots.
1354 * ...
1355 *
1356 */
1357
1358/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001359 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07001360 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001361static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001362 dma_addr_t dma_addr)
1363{
1364 return cpu_to_le32((u32)dma_addr);
1365}
1366
1367/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001368 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07001369 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001370 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07001371 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08001372 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07001373 *
1374 * This moves the 'write' index forward to catch up with 'processed', and
1375 * also updates the memory address in the firmware to reference the new
1376 * target buffer.
1377 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001378static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001379{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001380 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001381 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001382 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001383 unsigned long flags;
1384 int write, rc;
1385
1386 spin_lock_irqsave(&rxq->lock, flags);
1387 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001388 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001389 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001390 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001391 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001392 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001393
1394 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001395 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001396 rxq->queue[rxq->write] = rxb;
1397 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1398 rxq->free_count--;
1399 }
1400 spin_unlock_irqrestore(&rxq->lock, flags);
1401 /* If the pre-allocated buffer pool is dropping low, schedule to
1402 * refill it */
1403 if (rxq->free_count <= RX_LOW_WATERMARK)
1404 queue_work(priv->workqueue, &priv->rx_replenish);
1405
1406
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001407 /* If we've added more space for the firmware to place data, tell it.
1408 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07001409 if ((write != (rxq->write & ~0x7))
1410 || (abs(rxq->write - rxq->read) > 7)) {
1411 spin_lock_irqsave(&rxq->lock, flags);
1412 rxq->need_update = 1;
1413 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001414 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001415 if (rc)
1416 return rc;
1417 }
1418
1419 return 0;
1420}
1421
1422/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001423 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001424 *
1425 * When moving to rx_free an SKB is allocated for the slot.
1426 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001427 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001428 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001429 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001430static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001431{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001432 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001433 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001434 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001435 unsigned long flags;
1436 spin_lock_irqsave(&rxq->lock, flags);
1437 while (!list_empty(&rxq->rx_used)) {
1438 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001439 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001440
1441 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001442 rxb->skb =
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001443 alloc_skb(priv->hw_params.rx_buf_size,
1444 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07001445 if (!rxb->skb) {
1446 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08001447 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07001448 /* We don't reschedule replenish work here -- we will
1449 * call the restock method and if it still needs
1450 * more buffers it will schedule replenish */
1451 break;
1452 }
Zhu Yi12342c42007-12-20 11:27:32 +08001453
1454 /* If radiotap head is required, reserve some headroom here.
1455 * The physical head count is a variable rx_stats->phy_count.
1456 * We reserve 4 bytes here. Plus these extra bytes, the
1457 * headroom of the physical head should be enough for the
1458 * radiotap head that iwl3945 supported. See iwl3945_rt.
1459 */
1460 skb_reserve(rxb->skb, 4);
1461
Zhu Yib481de92007-09-25 17:54:57 -07001462 priv->alloc_rxb_skb++;
1463 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001464
1465 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001466 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
1467 rxb->skb->data,
1468 priv->hw_params.rx_buf_size,
1469 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001470 list_add_tail(&rxb->list, &rxq->rx_free);
1471 rxq->free_count++;
1472 }
1473 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001474}
1475
1476/*
1477 * this should be called while priv->lock is locked
1478 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02001479static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001480{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001481 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001482
1483 iwl3945_rx_allocate(priv);
1484 iwl3945_rx_queue_restock(priv);
1485}
1486
1487
1488void iwl3945_rx_replenish(void *data)
1489{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001490 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001491 unsigned long flags;
1492
1493 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001494
1495 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001496 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001497 spin_unlock_irqrestore(&priv->lock, flags);
1498}
1499
Zhu Yib481de92007-09-25 17:54:57 -07001500/* Convert linear signal-to-noise ratio into dB */
1501static u8 ratio2dB[100] = {
1502/* 0 1 2 3 4 5 6 7 8 9 */
1503 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1504 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1505 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1506 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1507 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1508 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1509 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1510 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1511 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1512 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1513};
1514
1515/* Calculates a relative dB value from a ratio of linear
1516 * (i.e. not dB) signal levels.
1517 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001518int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001519{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001520 /* 1000:1 or higher just report as 60 dB */
1521 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001522 return 60;
1523
Adrian Bunk221c80c2008-02-02 23:19:01 +02001524 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001525 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001526 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001527 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001528
1529 /* We shouldn't see this */
1530 if (sig_ratio < 1)
1531 return 0;
1532
1533 /* Use table for ratios 1:1 - 99:1 */
1534 return (int)ratio2dB[sig_ratio];
1535}
1536
1537#define PERFECT_RSSI (-20) /* dBm */
1538#define WORST_RSSI (-95) /* dBm */
1539#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1540
1541/* Calculate an indication of rx signal quality (a percentage, not dBm!).
1542 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1543 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001544int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07001545{
1546 int sig_qual;
1547 int degradation = PERFECT_RSSI - rssi_dbm;
1548
1549 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1550 * as indicator; formula is (signal dbm - noise dbm).
1551 * SNR at or above 40 is a great signal (100%).
1552 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1553 * Weakest usable signal is usually 10 - 15 dB SNR. */
1554 if (noise_dbm) {
1555 if (rssi_dbm - noise_dbm >= 40)
1556 return 100;
1557 else if (rssi_dbm < noise_dbm)
1558 return 0;
1559 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1560
1561 /* Else use just the signal level.
1562 * This formula is a least squares fit of data points collected and
1563 * compared with a reference system that had a percentage (%) display
1564 * for signal quality. */
1565 } else
1566 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1567 (15 * RSSI_RANGE + 62 * degradation)) /
1568 (RSSI_RANGE * RSSI_RANGE);
1569
1570 if (sig_qual > 100)
1571 sig_qual = 100;
1572 else if (sig_qual < 1)
1573 sig_qual = 0;
1574
1575 return sig_qual;
1576}
1577
1578/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001579 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001580 *
1581 * Uses the priv->rx_handlers callback function array to invoke
1582 * the appropriate handlers, including command responses,
1583 * frame-received notifications, and other notifications.
1584 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001585static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001586{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001587 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001588 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001589 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001590 u32 r, i;
1591 int reclaim;
1592 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001593 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001594 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07001595
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001596 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1597 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001598 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001599 i = rxq->read;
1600
Winkler, Tomas37d68312009-01-08 10:19:54 -08001601 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001602 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001603 /* Rx interrupt, but nothing sent from uCode */
1604 if (i == r)
Tomas Winklere1623442009-01-27 14:27:56 -08001605 IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001606
1607 while (i != r) {
1608 rxb = rxq->queue[i];
1609
Ben Cahill9fbab512007-11-29 11:09:47 +08001610 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001611 * then a bug has been introduced in the queue refilling
1612 * routines -- catch it here */
1613 BUG_ON(rxb == NULL);
1614
1615 rxq->queue[i] = NULL;
1616
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001617 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001618 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07001619 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001620 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001621
1622 /* Reclaim a command buffer only if this packet is a response
1623 * to a (driver-originated) command.
1624 * If the packet (e.g. Rx frame) originated from uCode,
1625 * there is no command buffer to reclaim.
1626 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1627 * but apparently a few don't get set; catch them here. */
1628 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1629 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1630 (pkt->hdr.cmd != REPLY_TX);
1631
1632 /* Based on type of command response or notification,
1633 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001634 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001635 if (priv->rx_handlers[pkt->hdr.cmd]) {
Tomas Winklere1623442009-01-27 14:27:56 -08001636 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07001637 "r = %d, i = %d, %s, 0x%02x\n", r, i,
1638 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1639 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1640 } else {
1641 /* No handling needed */
Tomas Winklere1623442009-01-27 14:27:56 -08001642 IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07001643 "r %d i %d No handler needed for %s, 0x%02x\n",
1644 r, i, get_cmd_string(pkt->hdr.cmd),
1645 pkt->hdr.cmd);
1646 }
1647
1648 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001649 /* Invoke any callbacks, transfer the skb to caller, and
Samuel Ortiz518099a2009-01-19 15:30:27 -08001650 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001651 * as we reclaim the driver command queue */
1652 if (rxb && rxb->skb)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001653 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001654 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001655 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001656 }
1657
1658 /* For now we just don't re-use anything. We can tweak this
1659 * later to try and re-use notification packets and SKBs that
1660 * fail to Rx correctly */
1661 if (rxb->skb != NULL) {
1662 priv->alloc_rxb_skb--;
1663 dev_kfree_skb_any(rxb->skb);
1664 rxb->skb = NULL;
1665 }
1666
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001667 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001668 priv->hw_params.rx_buf_size,
1669 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07001670 spin_lock_irqsave(&rxq->lock, flags);
1671 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1672 spin_unlock_irqrestore(&rxq->lock, flags);
1673 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001674 /* If there are a lot of unused frames,
1675 * restock the Rx queue so ucode won't assert. */
1676 if (fill_rx) {
1677 count++;
1678 if (count >= 8) {
1679 priv->rxq.read = i;
1680 __iwl3945_rx_replenish(priv);
1681 count = 0;
1682 }
1683 }
Zhu Yib481de92007-09-25 17:54:57 -07001684 }
1685
1686 /* Backtrack one entry */
1687 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001688 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001689}
1690
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001691/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001692static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001693{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001694 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001695 synchronize_irq(priv->pci_dev->irq);
1696 tasklet_kill(&priv->irq_tasklet);
1697}
1698
Zhu Yib481de92007-09-25 17:54:57 -07001699static const char *desc_lookup(int i)
1700{
1701 switch (i) {
1702 case 1:
1703 return "FAIL";
1704 case 2:
1705 return "BAD_PARAM";
1706 case 3:
1707 return "BAD_CHECKSUM";
1708 case 4:
1709 return "NMI_INTERRUPT";
1710 case 5:
1711 return "SYSASSERT";
1712 case 6:
1713 return "FATAL_ERROR";
1714 }
1715
1716 return "UNKNOWN";
1717}
1718
1719#define ERROR_START_OFFSET (1 * sizeof(u32))
1720#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1721
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001722static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001723{
1724 u32 i;
1725 u32 desc, time, count, base, data1;
1726 u32 blink1, blink2, ilink1, ilink2;
1727 int rc;
1728
1729 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1730
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001731 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001732 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001733 return;
1734 }
1735
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001736 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001737 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001738 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001739 return;
1740 }
1741
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001742 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001743
1744 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001745 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1746 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1747 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001748 }
1749
Winkler, Tomas15b16872008-12-19 10:37:33 +08001750 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001751 "ilink1 nmiPC Line\n");
1752 for (i = ERROR_START_OFFSET;
1753 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1754 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001755 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001756 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001757 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001758 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001759 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001760 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001761 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001762 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001763 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001764 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001765 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001766 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001767 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001768
Winkler, Tomas15b16872008-12-19 10:37:33 +08001769 IWL_ERR(priv,
1770 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1771 desc_lookup(desc), desc, time, blink1, blink2,
1772 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07001773 }
1774
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001775 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001776
1777}
1778
Ben Cahillf58177b2007-11-29 11:09:43 +08001779#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001780
1781/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001782 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001783 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001784 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07001785 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001786static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07001787 u32 num_events, u32 mode)
1788{
1789 u32 i;
1790 u32 base; /* SRAM byte address of event log header */
1791 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1792 u32 ptr; /* SRAM byte address of log data */
1793 u32 ev, time, data; /* event log data */
1794
1795 if (num_events == 0)
1796 return;
1797
1798 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1799
1800 if (mode == 0)
1801 event_size = 2 * sizeof(u32);
1802 else
1803 event_size = 3 * sizeof(u32);
1804
1805 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1806
1807 /* "time" is actually "data" for mode 0 (no timestamp).
1808 * place event id # at far right for easier visual parsing. */
1809 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001810 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001811 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001812 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001813 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001814 if (mode == 0) {
1815 /* data, ev */
1816 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1817 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001818 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001819 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001820 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07001821 }
1822 }
1823}
1824
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001825static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001826{
1827 int rc;
1828 u32 base; /* SRAM byte address of event log header */
1829 u32 capacity; /* event log capacity in # entries */
1830 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1831 u32 num_wraps; /* # times uCode wrapped to top of log */
1832 u32 next_entry; /* index of next entry to be written by uCode */
1833 u32 size; /* # entries that we'll print */
1834
1835 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001836 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001837 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001838 return;
1839 }
1840
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001841 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001842 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001843 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001844 return;
1845 }
1846
1847 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001848 capacity = iwl_read_targ_mem(priv, base);
1849 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1850 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1851 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001852
1853 size = num_wraps ? capacity : next_entry;
1854
1855 /* bail out if nothing in log */
1856 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001857 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001858 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001859 return;
1860 }
1861
Winkler, Tomas15b16872008-12-19 10:37:33 +08001862 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07001863 size, num_wraps);
1864
1865 /* if uCode has wrapped back to top of log, start at the oldest entry,
1866 * i.e the next one that uCode would fill. */
1867 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001868 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07001869 capacity - next_entry, mode);
1870
1871 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001872 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07001873
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001874 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001875}
1876
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001877static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001878{
1879 unsigned long flags;
1880
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001881 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1882 sizeof(priv->staging_rxon));
1883 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07001884 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001885
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001886 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001887
1888 spin_lock_irqsave(&priv->lock, flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001889 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07001890 priv->error_recovering = 0;
1891 spin_unlock_irqrestore(&priv->lock, flags);
1892}
1893
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001894static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001895{
1896 u32 inta, handled = 0;
1897 u32 inta_fh;
1898 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001899#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001900 u32 inta_mask;
1901#endif
1902
1903 spin_lock_irqsave(&priv->lock, flags);
1904
1905 /* Ack/clear/reset pending uCode interrupts.
1906 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1907 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001908 inta = iwl_read32(priv, CSR_INT);
1909 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001910
1911 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1912 * Any new interrupts that happen after this, either while we're
1913 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001914 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1915 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001916
Samuel Ortizd08853a2009-01-23 13:45:17 -08001917#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08001918 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001919 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001920 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001921 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001922 inta, inta_mask, inta_fh);
1923 }
1924#endif
1925
1926 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1927 * atomic, make sure that inta covers all the interrupts that
1928 * we've discovered, even if FH interrupt came in just after
1929 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001930 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001931 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001932 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001933 inta |= CSR_INT_BIT_FH_TX;
1934
1935 /* Now service all interrupt bits discovered above. */
1936 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001937 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001938
1939 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001940 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001941
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001942 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001943
1944 handled |= CSR_INT_BIT_HW_ERR;
1945
1946 spin_unlock_irqrestore(&priv->lock, flags);
1947
1948 return;
1949 }
1950
Samuel Ortizd08853a2009-01-23 13:45:17 -08001951#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08001952 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001953 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001954 if (inta & CSR_INT_BIT_SCD)
Tomas Winklere1623442009-01-27 14:27:56 -08001955 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001956 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001957
1958 /* Alive notification via Rx interrupt will do the real work */
1959 if (inta & CSR_INT_BIT_ALIVE)
Tomas Winklere1623442009-01-27 14:27:56 -08001960 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Zhu Yib481de92007-09-25 17:54:57 -07001961 }
1962#endif
1963 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001964 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001965
Zhu Yib481de92007-09-25 17:54:57 -07001966 /* Error detected by uCode */
1967 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001968 IWL_ERR(priv, "Microcode SW error detected. "
1969 "Restarting 0x%X.\n", inta);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001970 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001971 handled |= CSR_INT_BIT_SW_ERR;
1972 }
1973
1974 /* uCode wakes up after power-down sleep */
1975 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001976 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001977 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001978 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1979 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1980 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1981 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1982 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1983 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001984
1985 handled |= CSR_INT_BIT_WAKEUP;
1986 }
1987
1988 /* All uCode command responses, including Tx command responses,
1989 * Rx "responses" (frame-received notification), and other
1990 * notifications from uCode come through here*/
1991 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001992 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001993 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1994 }
1995
1996 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001997 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Zhu Yib481de92007-09-25 17:54:57 -07001998
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001999 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
2000 if (!iwl_grab_nic_access(priv)) {
2001 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08002002 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002003 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002004 }
2005 handled |= CSR_INT_BIT_FH_TX;
2006 }
2007
2008 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002009 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07002010
2011 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002012 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07002013 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002014 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07002015 }
2016
2017 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002018 /* only Re-enable if disabled by irq */
2019 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002020 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002021
Samuel Ortizd08853a2009-01-23 13:45:17 -08002022#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002023 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002024 inta = iwl_read32(priv, CSR_INT);
2025 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2026 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08002027 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07002028 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2029 }
2030#endif
2031 spin_unlock_irqrestore(&priv->lock, flags);
2032}
2033
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002034static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002035 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002036 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002037 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07002038{
2039 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01002040 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002041 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002042 u16 passive_dwell = 0;
2043 u16 active_dwell = 0;
2044 int added, i;
2045
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002046 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01002047 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07002048 return 0;
2049
Johannes Berg8318d782008-01-24 19:38:38 +01002050 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07002051
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08002052 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
2053 passive_dwell = iwl_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07002054
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08002055 if (passive_dwell <= active_dwell)
2056 passive_dwell = active_dwell + 1;
2057
Johannes Berg8318d782008-01-24 19:38:38 +01002058 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02002059 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2060 continue;
2061
Johannes Berg8318d782008-01-24 19:38:38 +01002062 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07002063
Samuel Ortize6148912009-01-23 13:45:15 -08002064 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002065 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002066 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07002067 scan_ch->channel);
2068 continue;
2069 }
2070
Zhu Yib481de92007-09-25 17:54:57 -07002071 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2072 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002073 /* If passive , set up for auto-switch
2074 * and use long active_dwell time.
2075 */
2076 if (!is_active || is_channel_passive(ch_info) ||
2077 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
2078 scan_ch->type = 0; /* passive */
2079 if (IWL_UCODE_API(priv->ucode_ver) == 1)
2080 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
2081 } else {
2082 scan_ch->type = 1; /* active */
2083 }
2084
2085 /* Set direct probe bits. These may be used both for active
2086 * scan channels (probes gets sent right away),
2087 * or for passive channels (probes get se sent only after
2088 * hearing clear Rx packet).*/
2089 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
2090 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08002091 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002092 } else {
2093 /* uCode v1 does not allow setting direct probe bits on
2094 * passive channel. */
2095 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08002096 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08002097 }
Zhu Yib481de92007-09-25 17:54:57 -07002098
Ben Cahill9fbab512007-11-29 11:09:47 +08002099 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07002100 scan_ch->tpc.dsp_atten = 110;
2101 /* scan_pwr_info->tpc.dsp_atten; */
2102
2103 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01002104 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07002105 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2106 else {
2107 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2108 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08002109 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08002110 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07002111 */
2112 }
2113
Tomas Winklere1623442009-01-27 14:27:56 -08002114 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07002115 scan_ch->channel,
2116 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2117 (scan_ch->type & 1) ?
2118 active_dwell : passive_dwell);
2119
2120 scan_ch++;
2121 added++;
2122 }
2123
Tomas Winklere1623442009-01-27 14:27:56 -08002124 IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
Zhu Yib481de92007-09-25 17:54:57 -07002125 return added;
2126}
2127
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002128static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002129 struct ieee80211_rate *rates)
2130{
2131 int i;
2132
2133 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01002134 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
2135 rates[i].hw_value = i; /* Rate scaling will work on indexes */
2136 rates[i].hw_value_short = i;
2137 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08002138 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07002139 /*
Johannes Berg8318d782008-01-24 19:38:38 +01002140 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07002141 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002142 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01002143 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07002144 }
Zhu Yib481de92007-09-25 17:54:57 -07002145 }
2146}
2147
Zhu Yib481de92007-09-25 17:54:57 -07002148/******************************************************************************
2149 *
2150 * uCode download functions
2151 *
2152 ******************************************************************************/
2153
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002154static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002155{
Tomas Winkler98c92212008-01-14 17:46:20 -08002156 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2157 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2158 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2159 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2160 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2161 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07002162}
2163
2164/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002165 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002166 * looking at all data.
2167 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002168static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002169{
2170 u32 val;
2171 u32 save_len = len;
2172 int rc = 0;
2173 u32 errcnt;
2174
Tomas Winklere1623442009-01-27 14:27:56 -08002175 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002176
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002177 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002178 if (rc)
2179 return rc;
2180
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002181 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002182 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07002183
2184 errcnt = 0;
2185 for (; len > 0; len -= sizeof(u32), image++) {
2186 /* read data comes through single port, auto-incr addr */
2187 /* NOTE: Use the debugless read so we don't flood kernel log
2188 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002189 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002190 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002191 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002192 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2193 save_len - len, val, le32_to_cpu(*image));
2194 rc = -EIO;
2195 errcnt++;
2196 if (errcnt >= 20)
2197 break;
2198 }
2199 }
2200
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002201 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002202
2203 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002204 IWL_DEBUG_INFO(priv,
2205 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002206
2207 return rc;
2208}
2209
2210
2211/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002212 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002213 * using sample data 100 bytes apart. If these sample points are good,
2214 * it's a pretty good bet that everything between them is good, too.
2215 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002216static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002217{
2218 u32 val;
2219 int rc = 0;
2220 u32 errcnt = 0;
2221 u32 i;
2222
Tomas Winklere1623442009-01-27 14:27:56 -08002223 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002224
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002225 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002226 if (rc)
2227 return rc;
2228
2229 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2230 /* read data comes through single port, auto-incr addr */
2231 /* NOTE: Use the debugless read so we don't flood kernel log
2232 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002233 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002234 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002235 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002236 if (val != le32_to_cpu(*image)) {
2237#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002238 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002239 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2240 i, val, *image);
2241#endif
2242 rc = -EIO;
2243 errcnt++;
2244 if (errcnt >= 3)
2245 break;
2246 }
2247 }
2248
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002249 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002250
2251 return rc;
2252}
2253
2254
2255/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002256 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002257 * and verify its contents
2258 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002259static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002260{
2261 __le32 *image;
2262 u32 len;
2263 int rc = 0;
2264
2265 /* Try bootstrap */
2266 image = (__le32 *)priv->ucode_boot.v_addr;
2267 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002268 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002269 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002270 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002271 return 0;
2272 }
2273
2274 /* Try initialize */
2275 image = (__le32 *)priv->ucode_init.v_addr;
2276 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002277 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002278 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002279 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002280 return 0;
2281 }
2282
2283 /* Try runtime/protocol */
2284 image = (__le32 *)priv->ucode_code.v_addr;
2285 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002286 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002287 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002288 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002289 return 0;
2290 }
2291
Winkler, Tomas15b16872008-12-19 10:37:33 +08002292 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002293
Ben Cahill9fbab512007-11-29 11:09:47 +08002294 /* Since nothing seems to match, show first several data entries in
2295 * instruction SRAM, so maybe visual inspection will give a clue.
2296 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002297 image = (__le32 *)priv->ucode_boot.v_addr;
2298 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002299 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002300
2301 return rc;
2302}
2303
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002304static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002305{
2306 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002307 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002308}
2309
2310/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002311 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002312 *
2313 * Copy into buffers for card to fetch via bus-mastering
2314 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002315static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002316{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08002317 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002318 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002319 const struct firmware *ucode_raw;
2320 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002321 const char *name_pre = priv->cfg->fw_name_pre;
2322 const unsigned int api_max = priv->cfg->ucode_api_max;
2323 const unsigned int api_min = priv->cfg->ucode_api_min;
2324 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002325 u8 *src;
2326 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002327 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002328
2329 /* Ask kernel firmware_class module to get the boot firmware off disk.
2330 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002331 for (index = api_max; index >= api_min; index--) {
2332 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2333 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2334 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002335 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002336 buf, ret);
2337 if (ret == -ENOENT)
2338 continue;
2339 else
2340 goto error;
2341 } else {
2342 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002343 IWL_ERR(priv, "Loaded firmware %s, "
2344 "which is deprecated. "
2345 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002346 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002347 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2348 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002349 buf, ucode_raw->size);
2350 break;
2351 }
Zhu Yib481de92007-09-25 17:54:57 -07002352 }
2353
Reinette Chatrea0987a82008-12-02 12:14:06 -08002354 if (ret < 0)
2355 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002356
2357 /* Make sure that we got at least our header! */
2358 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002359 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002360 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002361 goto err_release;
2362 }
2363
2364 /* Data from ucode file: header followed by uCode images */
2365 ucode = (void *)ucode_raw->data;
2366
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002367 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002368 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07002369 inst_size = le32_to_cpu(ucode->inst_size);
2370 data_size = le32_to_cpu(ucode->data_size);
2371 init_size = le32_to_cpu(ucode->init_size);
2372 init_data_size = le32_to_cpu(ucode->init_data_size);
2373 boot_size = le32_to_cpu(ucode->boot_size);
2374
Reinette Chatrea0987a82008-12-02 12:14:06 -08002375 /* api_ver should match the api version forming part of the
2376 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002377 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002378
2379 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002380 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002381 "Driver supports v%u, firmware is v%u.\n",
2382 api_max, api_ver);
2383 priv->ucode_ver = 0;
2384 ret = -EINVAL;
2385 goto err_release;
2386 }
2387 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002388 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002389 "got %u. New firmware can be obtained "
2390 "from http://www.intellinuxwireless.org.\n",
2391 api_max, api_ver);
2392
Tomas Winkler978785a2008-12-19 10:37:31 +08002393 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2394 IWL_UCODE_MAJOR(priv->ucode_ver),
2395 IWL_UCODE_MINOR(priv->ucode_ver),
2396 IWL_UCODE_API(priv->ucode_ver),
2397 IWL_UCODE_SERIAL(priv->ucode_ver));
2398
Tomas Winklere1623442009-01-27 14:27:56 -08002399 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002400 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002401 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2402 inst_size);
2403 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2404 data_size);
2405 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2406 init_size);
2407 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2408 init_data_size);
2409 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2410 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002411
Reinette Chatrea0987a82008-12-02 12:14:06 -08002412
Zhu Yib481de92007-09-25 17:54:57 -07002413 /* Verify size of file vs. image size info in file's header */
2414 if (ucode_raw->size < sizeof(*ucode) +
2415 inst_size + data_size + init_size +
2416 init_data_size + boot_size) {
2417
Tomas Winklere1623442009-01-27 14:27:56 -08002418 IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
2419 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002420 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002421 goto err_release;
2422 }
2423
2424 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002425 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002426 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002427 inst_size);
2428 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002429 goto err_release;
2430 }
2431
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002432 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002433 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002434 data_size);
2435 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002436 goto err_release;
2437 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002438 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002439 IWL_DEBUG_INFO(priv,
2440 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002441 init_size);
2442 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002443 goto err_release;
2444 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002445 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002446 IWL_DEBUG_INFO(priv,
2447 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002448 init_data_size);
2449 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002450 goto err_release;
2451 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002452 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002453 IWL_DEBUG_INFO(priv,
2454 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002455 boot_size);
2456 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002457 goto err_release;
2458 }
2459
2460 /* Allocate ucode buffers for card's bus-master loading ... */
2461
2462 /* Runtime instructions and 2 copies of data:
2463 * 1) unmodified from disk
2464 * 2) backup cache for save/restore during power-downs */
2465 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002466 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002467
2468 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002469 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002470
2471 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002472 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002473
2474 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002475 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002476 goto err_pci_alloc;
2477
Tomas Winkler90e759d2007-11-29 11:09:41 +08002478 /* Initialization instructions and data */
2479 if (init_size && init_data_size) {
2480 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002481 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002482
2483 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002484 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002485
2486 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2487 goto err_pci_alloc;
2488 }
2489
2490 /* Bootstrap (instructions only, no data) */
2491 if (boot_size) {
2492 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002493 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002494
2495 if (!priv->ucode_boot.v_addr)
2496 goto err_pci_alloc;
2497 }
2498
Zhu Yib481de92007-09-25 17:54:57 -07002499 /* Copy images into buffers for card's bus-master reads ... */
2500
2501 /* Runtime instructions (first block of data in file) */
2502 src = &ucode->data[0];
2503 len = priv->ucode_code.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002504 IWL_DEBUG_INFO(priv,
2505 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002506 memcpy(priv->ucode_code.v_addr, src, len);
Tomas Winklere1623442009-01-27 14:27:56 -08002507 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002508 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2509
2510 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002511 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07002512 src = &ucode->data[inst_size];
2513 len = priv->ucode_data.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002514 IWL_DEBUG_INFO(priv,
2515 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002516 memcpy(priv->ucode_data.v_addr, src, len);
2517 memcpy(priv->ucode_data_backup.v_addr, src, len);
2518
2519 /* Initialization instructions (3rd block) */
2520 if (init_size) {
2521 src = &ucode->data[inst_size + data_size];
2522 len = priv->ucode_init.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002523 IWL_DEBUG_INFO(priv,
2524 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002525 memcpy(priv->ucode_init.v_addr, src, len);
2526 }
2527
2528 /* Initialization data (4th block) */
2529 if (init_data_size) {
2530 src = &ucode->data[inst_size + data_size + init_size];
2531 len = priv->ucode_init_data.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002532 IWL_DEBUG_INFO(priv,
2533 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002534 memcpy(priv->ucode_init_data.v_addr, src, len);
2535 }
2536
2537 /* Bootstrap instructions (5th block) */
2538 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2539 len = priv->ucode_boot.len;
Tomas Winklere1623442009-01-27 14:27:56 -08002540 IWL_DEBUG_INFO(priv,
2541 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002542 memcpy(priv->ucode_boot.v_addr, src, len);
2543
2544 /* We have our copies now, allow OS release its copies */
2545 release_firmware(ucode_raw);
2546 return 0;
2547
2548 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002549 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002550 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002551 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002552
2553 err_release:
2554 release_firmware(ucode_raw);
2555
2556 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002557 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002558}
2559
2560
2561/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002562 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002563 *
2564 * Tell initialization uCode where to find runtime uCode.
2565 *
2566 * BSM registers initially contain pointers to initialization uCode.
2567 * We need to replace them to load runtime uCode inst and data,
2568 * and to save runtime data when powering down.
2569 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002570static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002571{
2572 dma_addr_t pinst;
2573 dma_addr_t pdata;
2574 int rc = 0;
2575 unsigned long flags;
2576
2577 /* bits 31:0 for 3945 */
2578 pinst = priv->ucode_code.p_addr;
2579 pdata = priv->ucode_data_backup.p_addr;
2580
2581 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002582 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002583 if (rc) {
2584 spin_unlock_irqrestore(&priv->lock, flags);
2585 return rc;
2586 }
2587
2588 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002589 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2590 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2591 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002592 priv->ucode_data.len);
2593
Tomas Winklera96a27f2008-10-23 23:48:56 -07002594 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002595 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002596 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002597 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2598
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002599 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002600
2601 spin_unlock_irqrestore(&priv->lock, flags);
2602
Tomas Winklere1623442009-01-27 14:27:56 -08002603 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002604
2605 return rc;
2606}
2607
2608/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002609 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002610 *
2611 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2612 *
Zhu Yib481de92007-09-25 17:54:57 -07002613 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002614 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002615static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002616{
2617 /* Check alive response for "valid" sign from uCode */
2618 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2619 /* We had an error bringing up the hardware, so take it
2620 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002621 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002622 goto restart;
2623 }
2624
2625 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2626 * This is a paranoid check, because we would not have gotten the
2627 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002628 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002629 /* Runtime instruction load was bad;
2630 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002631 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002632 goto restart;
2633 }
2634
2635 /* Send pointers to protocol/runtime uCode image ... init code will
2636 * load and launch runtime uCode, which will send us another "Alive"
2637 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002638 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002639 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002640 /* Runtime instruction load won't happen;
2641 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002642 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002643 goto restart;
2644 }
2645 return;
2646
2647 restart:
2648 queue_work(priv->workqueue, &priv->restart);
2649}
2650
Zhu Yib481de92007-09-25 17:54:57 -07002651/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002652 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002653 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002654 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002655 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002656static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002657{
2658 int rc = 0;
2659 int thermal_spin = 0;
2660 u32 rfkill;
2661
Tomas Winklere1623442009-01-27 14:27:56 -08002662 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002663
2664 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2665 /* We had an error bringing up the hardware, so take it
2666 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002667 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002668 goto restart;
2669 }
2670
2671 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2672 * This is a paranoid check, because we would not have gotten the
2673 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002674 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002675 /* Runtime instruction load was bad;
2676 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002677 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002678 goto restart;
2679 }
2680
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002681 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002682
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002683 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002684 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002685 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002686 return;
2687 }
2688
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002689 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002690 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002691 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002692
2693 if (rfkill & 0x1) {
2694 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002695 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002696 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002697 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002698 thermal_spin++;
2699 udelay(10);
2700 }
2701
2702 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002703 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002704 thermal_spin * 10);
2705 } else
2706 set_bit(STATUS_RF_KILL_HW, &priv->status);
2707
Ben Cahill9fbab512007-11-29 11:09:47 +08002708 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002709 set_bit(STATUS_ALIVE, &priv->status);
2710
2711 /* Clear out the uCode error bit if it is set */
2712 clear_bit(STATUS_FW_ERROR, &priv->status);
2713
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002714 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002715 return;
2716
Johannes Berg36d68252008-05-15 12:55:26 +02002717 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002718
2719 priv->active_rate = priv->rates_mask;
2720 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2721
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08002722 iwl_power_update_mode(priv, false);
Zhu Yib481de92007-09-25 17:54:57 -07002723
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002724 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002725 struct iwl3945_rxon_cmd *active_rxon =
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002726 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07002727
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002728 memcpy(&priv->staging_rxon, &priv->active_rxon,
2729 sizeof(priv->staging_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002730 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2731 } else {
2732 /* Initialize our rx_config data */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002733 iwl_connection_init_rx_config(priv, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002734 }
2735
Ben Cahill9fbab512007-11-29 11:09:47 +08002736 /* Configure Bluetooth device coexistence support */
Samuel Ortiz17f841c2009-01-23 13:45:20 -08002737 iwl_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002738
2739 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002740 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002741
Zhu Yib481de92007-09-25 17:54:57 -07002742 iwl3945_reg_txpower_periodic(priv);
2743
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002744 iwl3945_led_register(priv);
2745
Tomas Winklere1623442009-01-27 14:27:56 -08002746 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002747 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002748 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002749
2750 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002751 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002752
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002753 /* reassociate for ADHOC mode */
2754 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2755 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2756 priv->vif);
2757 if (beacon)
Abhijeet Kolekar9944b932009-04-08 11:26:36 -07002758 iwl_mac_beacon_update(priv->hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002759 }
2760
Zhu Yib481de92007-09-25 17:54:57 -07002761 return;
2762
2763 restart:
2764 queue_work(priv->workqueue, &priv->restart);
2765}
2766
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002767static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002768
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002769static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002770{
2771 unsigned long flags;
2772 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2773 struct ieee80211_conf *conf = NULL;
2774
Tomas Winklere1623442009-01-27 14:27:56 -08002775 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002776
2777 conf = ieee80211_get_hw_conf(priv->hw);
2778
2779 if (!exit_pending)
2780 set_bit(STATUS_EXIT_PENDING, &priv->status);
2781
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002782 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002783 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002784
2785 /* Unblock any waiting calls */
2786 wake_up_interruptible_all(&priv->wait_command_queue);
2787
Zhu Yib481de92007-09-25 17:54:57 -07002788 /* Wipe out the EXIT_PENDING status bit if we are not actually
2789 * exiting the module */
2790 if (!exit_pending)
2791 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2792
2793 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002794 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002795
2796 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002797 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002798 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002799 spin_unlock_irqrestore(&priv->lock, flags);
2800 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002801
2802 if (priv->mac80211_registered)
2803 ieee80211_stop_queues(priv->hw);
2804
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002805 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002806 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002807 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002808 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2809 STATUS_RF_KILL_HW |
2810 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2811 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002812 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2813 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002814 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2815 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002816 goto exit;
2817 }
2818
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002819 /* ...otherwise clear out all the status bits but the RF Kill
2820 * bits and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002821 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2822 STATUS_RF_KILL_HW |
2823 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2824 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08002825 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2826 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002827 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002828 STATUS_FW_ERROR |
2829 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2830 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002831
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002832 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002833 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002834 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002835 spin_unlock_irqrestore(&priv->lock, flags);
2836
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002837 iwl3945_hw_txq_ctx_stop(priv);
2838 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002839
2840 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002841 if (!iwl_grab_nic_access(priv)) {
2842 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002843 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002844 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002845 }
2846 spin_unlock_irqrestore(&priv->lock, flags);
2847
2848 udelay(5);
2849
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002850 if (exit_pending)
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002851 priv->cfg->ops->lib->apm_ops.stop(priv);
2852 else
2853 priv->cfg->ops->lib->apm_ops.reset(priv);
2854
Zhu Yib481de92007-09-25 17:54:57 -07002855 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002856 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002857
2858 if (priv->ibss_beacon)
2859 dev_kfree_skb(priv->ibss_beacon);
2860 priv->ibss_beacon = NULL;
2861
2862 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002863 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002864}
2865
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002866static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002867{
2868 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002869 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002870 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002871
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002872 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002873}
2874
2875#define MAX_HW_RESTARTS 5
2876
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002877static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002878{
2879 int rc, i;
2880
2881 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002882 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002883 return -EIO;
2884 }
2885
2886 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002887 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07002888 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08002889 return -ENODEV;
2890 }
2891
Reinette Chatree903fbd2008-01-30 22:05:15 -08002892 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002893 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002894 return -EIO;
2895 }
2896
Zhu Yie655b9f2008-01-24 02:19:38 -08002897 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002898 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002899 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2900 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2901 else {
2902 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002903 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2904 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002905 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002906
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002907 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002908
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002909 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002910 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002911 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002912 return rc;
2913 }
2914
2915 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002916 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2917 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002918 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2919
2920 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002921 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002922 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002923
2924 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002925 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2926 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002927
2928 /* Copy original ucode data image from disk into backup cache.
2929 * This will be used to initialize the on-board processor's
2930 * data SRAM for a clean start when the runtime program first loads. */
2931 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002932 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002933
Zhu Yie655b9f2008-01-24 02:19:38 -08002934 /* We return success when we resume from suspend and rf_kill is on. */
2935 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2936 return 0;
2937
Zhu Yib481de92007-09-25 17:54:57 -07002938 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2939
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002940 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002941
2942 /* load bootstrap state machine,
2943 * load bootstrap program into processor's memory,
2944 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08002945 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002946
2947 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002948 IWL_ERR(priv,
2949 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002950 continue;
2951 }
2952
2953 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002954 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002955
Tomas Winklere1623442009-01-27 14:27:56 -08002956 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002957
2958 return 0;
2959 }
2960
2961 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002962 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002963 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002964
2965 /* tried to restart and config the device for as long as our
2966 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002967 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002968 return -EIO;
2969}
2970
2971
2972/*****************************************************************************
2973 *
2974 * Workqueue callbacks
2975 *
2976 *****************************************************************************/
2977
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002978static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002979{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002980 struct iwl_priv *priv =
2981 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002982
2983 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2984 return;
2985
2986 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002987 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002988 mutex_unlock(&priv->mutex);
2989}
2990
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002991static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002992{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002993 struct iwl_priv *priv =
2994 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002995
2996 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2997 return;
2998
2999 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003000 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003001 mutex_unlock(&priv->mutex);
3002}
3003
Helmut Schaa26635162009-01-15 09:38:44 +01003004static void iwl3945_rfkill_poll(struct work_struct *data)
3005{
3006 struct iwl_priv *priv =
3007 container_of(data, struct iwl_priv, rfkill_poll.work);
3008 unsigned long status = priv->status;
3009
3010 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3011 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3012 else
3013 set_bit(STATUS_RF_KILL_HW, &priv->status);
3014
3015 if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
3016 queue_work(priv->workqueue, &priv->rf_kill);
3017
3018 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3019 round_jiffies_relative(2 * HZ));
3020
3021}
3022
Zhu Yib481de92007-09-25 17:54:57 -07003023#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003024static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003025{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003026 struct iwl_priv *priv =
3027 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08003028 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003029 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003030 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07003031 .meta.flags = CMD_SIZE_HUGE,
3032 };
3033 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003034 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07003035 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003036 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01003037 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04003038 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07003039
3040 conf = ieee80211_get_hw_conf(priv->hw);
3041
3042 mutex_lock(&priv->mutex);
3043
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003044 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003045 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003046 goto done;
3047 }
3048
Tomas Winklera96a27f2008-10-23 23:48:56 -07003049 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07003050 * was given the chance to run... */
3051 if (!test_bit(STATUS_SCANNING, &priv->status))
3052 goto done;
3053
3054 /* This should never be called or scheduled if there is currently
3055 * a scan active in the hardware. */
3056 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003057 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
3058 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003059 rc = -EIO;
3060 goto done;
3061 }
3062
3063 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003064 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07003065 goto done;
3066 }
3067
3068 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003069 IWL_DEBUG_HC(priv,
3070 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003071 goto done;
3072 }
3073
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003074 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003075 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07003076 goto done;
3077 }
3078
3079 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003080 IWL_DEBUG_HC(priv,
3081 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003082 goto done;
3083 }
3084
3085 if (!priv->scan_bands) {
Tomas Winklere1623442009-01-27 14:27:56 -08003086 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
Zhu Yib481de92007-09-25 17:54:57 -07003087 goto done;
3088 }
3089
Winkler, Tomas805cee52009-01-19 15:30:28 -08003090 if (!priv->scan) {
3091 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07003092 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Winkler, Tomas805cee52009-01-19 15:30:28 -08003093 if (!priv->scan) {
Zhu Yib481de92007-09-25 17:54:57 -07003094 rc = -ENOMEM;
3095 goto done;
3096 }
3097 }
Winkler, Tomas805cee52009-01-19 15:30:28 -08003098 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003099 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07003100
3101 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3102 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3103
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003104 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003105 u16 interval = 0;
3106 u32 extra;
3107 u32 suspend_time = 100;
3108 u32 scan_suspend_time = 100;
3109 unsigned long flags;
3110
Tomas Winklere1623442009-01-27 14:27:56 -08003111 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07003112
3113 spin_lock_irqsave(&priv->lock, flags);
3114 interval = priv->beacon_int;
3115 spin_unlock_irqrestore(&priv->lock, flags);
3116
3117 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003118 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07003119 if (!interval)
3120 interval = suspend_time;
3121 /*
3122 * suspend time format:
3123 * 0-19: beacon interval in usec (time before exec.)
3124 * 20-23: 0
3125 * 24-31: number of beacons (suspend between channels)
3126 */
3127
3128 extra = (suspend_time / interval) << 24;
3129 scan_suspend_time = 0xFF0FFFFF &
3130 (extra | ((suspend_time % interval) * 1024));
3131
3132 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08003133 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003134 scan_suspend_time, interval);
3135 }
3136
3137 /* We should add the ability for user to lock to PASSIVE ONLY */
3138 if (priv->one_direct_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08003139 IWL_DEBUG_SCAN(priv, "Kicking off one direct scan for '%s'\n",
3140 print_ssid(ssid, priv->direct_ssid,
John W. Linville9387b7c2008-09-30 20:59:05 -04003141 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07003142 scan->direct_scan[0].id = WLAN_EID_SSID;
3143 scan->direct_scan[0].len = priv->direct_ssid_len;
3144 memcpy(scan->direct_scan[0].ssid,
3145 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003146 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003147 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003148 IWL_DEBUG_SCAN(priv, "Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003149
3150 /* We don't build a direct scan probe request; the uCode will do
3151 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07003152 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08003153 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07003154 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3155
3156 /* flags + rate selection */
3157
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003158 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07003159 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3160 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
3161 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003162 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003163 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07003164 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
3165 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01003166 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003167 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003168 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07003169 goto done;
3170 }
3171
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003172 scan->tx_cmd.len = cpu_to_le16(
3173 iwl_fill_probe_req(priv, band,
3174 (struct ieee80211_mgmt *)scan->data,
3175 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
3176
Zhu Yib481de92007-09-25 17:54:57 -07003177 /* select Rx antennas */
3178 scan->flags |= iwl3945_get_antenna_flags(priv);
3179
Johannes Berg05c914f2008-09-11 00:01:58 +02003180 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07003181 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3182
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003183 scan->channel_count =
3184 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
3185 n_probes,
3186 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07003187
Reinette Chatre14b54332008-11-04 12:21:35 -08003188 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08003189 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08003190 goto done;
3191 }
3192
Zhu Yib481de92007-09-25 17:54:57 -07003193 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003194 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003195 cmd.data = scan;
3196 scan->len = cpu_to_le16(cmd.len);
3197
3198 set_bit(STATUS_SCAN_HW, &priv->status);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003199 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003200 if (rc)
3201 goto done;
3202
3203 queue_delayed_work(priv->workqueue, &priv->scan_check,
3204 IWL_SCAN_CHECK_WATCHDOG);
3205
3206 mutex_unlock(&priv->mutex);
3207 return;
3208
3209 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003210 /* can not perform scan make sure we clear scanning
3211 * bits from status so next scan request can be performed.
3212 * if we dont clear scanning status bit here all next scan
3213 * will fail
3214 */
3215 clear_bit(STATUS_SCAN_HW, &priv->status);
3216 clear_bit(STATUS_SCANNING, &priv->status);
3217
Ian Schram01ebd062007-10-25 17:15:22 +08003218 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003219 queue_work(priv->workqueue, &priv->scan_completed);
3220 mutex_unlock(&priv->mutex);
3221}
3222
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003223static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003224{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003225 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07003226
3227 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3228 return;
3229
3230 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003231 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003232 mutex_unlock(&priv->mutex);
Samuel Ortizc0af96a2009-01-21 18:27:54 +01003233 iwl_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003234}
3235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003236static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003237{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003238 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003239
3240 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3241 return;
3242
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003243 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003244 queue_work(priv->workqueue, &priv->up);
3245}
3246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003247static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003248{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003249 struct iwl_priv *priv =
3250 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003251
3252 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3253 return;
3254
3255 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003256 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003257 mutex_unlock(&priv->mutex);
3258}
3259
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003260static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
3261
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003262#define IWL_DELAY_NEXT_SCAN (HZ*2)
3263
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003264void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003265{
Zhu Yib481de92007-09-25 17:54:57 -07003266 int rc = 0;
3267 struct ieee80211_conf *conf = NULL;
3268
Johannes Berg05c914f2008-09-11 00:01:58 +02003269 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003270 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003271 return;
3272 }
3273
3274
Tomas Winklere1623442009-01-27 14:27:56 -08003275 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003276 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003277
3278 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3279 return;
3280
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003281 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003282 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003283
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003284 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003285
Zhu Yib481de92007-09-25 17:54:57 -07003286 conf = ieee80211_get_hw_conf(priv->hw);
3287
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003288 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003289 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003290
Tomas Winkler28afaf92008-12-19 10:37:06 +08003291 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003292 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003293 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003294 sizeof(priv->rxon_timing), &priv->rxon_timing);
3295 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003296 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003297 "Attempting to continue.\n");
3298
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003299 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003300
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003301 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003302
Tomas Winklere1623442009-01-27 14:27:56 -08003303 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003304 priv->assoc_id, priv->beacon_int);
3305
3306 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003307 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003308 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003309 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003310
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003311 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003312 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003313 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003314 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003315 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003316
Johannes Berg05c914f2008-09-11 00:01:58 +02003317 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003318 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003319
3320 }
3321
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003322 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003323
3324 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003325 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003326 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003327 break;
3328
Johannes Berg05c914f2008-09-11 00:01:58 +02003329 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07003330
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08003331 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003332 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003333 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01003334 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003335 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
3336 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003337 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
3338 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003339
3340 break;
3341
3342 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08003343 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003344 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07003345 break;
3346 }
3347
Abhijeet Kolekar14d2aac2009-02-27 16:21:24 -08003348 iwl_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003349
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003350 /* we have just associated, don't start scan too early */
3351 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003352}
3353
Zhu Yib481de92007-09-25 17:54:57 -07003354/*****************************************************************************
3355 *
3356 * mac80211 entry point functions
3357 *
3358 *****************************************************************************/
3359
Zhu Yi5a669262008-01-14 17:46:18 -08003360#define UCODE_READY_TIMEOUT (2 * HZ)
3361
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003362static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003363{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003364 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08003365 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003366
Tomas Winklere1623442009-01-27 14:27:56 -08003367 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003368
3369 /* we should be verifying the device is ready to be opened */
3370 mutex_lock(&priv->mutex);
3371
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003372 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
Zhu Yi5a669262008-01-14 17:46:18 -08003373 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3374 * ucode filename and max sizes are card-specific. */
3375
3376 if (!priv->ucode_code.len) {
3377 ret = iwl3945_read_ucode(priv);
3378 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003379 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08003380 mutex_unlock(&priv->mutex);
3381 goto out_release_irq;
3382 }
3383 }
3384
Zhu Yie655b9f2008-01-24 02:19:38 -08003385 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08003386
Zhu Yib481de92007-09-25 17:54:57 -07003387 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08003388
Samuel Ortizc0af96a2009-01-21 18:27:54 +01003389 iwl_rfkill_set_hw_state(priv);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02003390
Zhu Yie655b9f2008-01-24 02:19:38 -08003391 if (ret)
3392 goto out_release_irq;
3393
Tomas Winklere1623442009-01-27 14:27:56 -08003394 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003395
Zhu Yi5a669262008-01-14 17:46:18 -08003396 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3397 * mac80211 will not be run successfully. */
3398 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3399 test_bit(STATUS_READY, &priv->status),
3400 UCODE_READY_TIMEOUT);
3401 if (!ret) {
3402 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003403 IWL_ERR(priv,
3404 "Wait for START_ALIVE timeout after %dms.\n",
3405 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08003406 ret = -ETIMEDOUT;
3407 goto out_release_irq;
3408 }
3409 }
3410
Helmut Schaa26635162009-01-15 09:38:44 +01003411 /* ucode is running and will send rfkill notifications,
3412 * no need to poll the killswitch state anymore */
3413 cancel_delayed_work(&priv->rfkill_poll);
3414
Zhu Yie655b9f2008-01-24 02:19:38 -08003415 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003416 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003417 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003418
3419out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003420 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003421 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08003422 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003423}
3424
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003425static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003426{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003427 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003428
Tomas Winklere1623442009-01-27 14:27:56 -08003429 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003430
Zhu Yie655b9f2008-01-24 02:19:38 -08003431 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003432 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003433 return;
3434 }
3435
Zhu Yib481de92007-09-25 17:54:57 -07003436 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003437
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003438 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003439 /* stop mac, cancel any scan request and clear
3440 * RXON_FILTER_ASSOC_MSK BIT
3441 */
Zhu Yi5a669262008-01-14 17:46:18 -08003442 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003443 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003444 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003445 }
3446
Zhu Yi5a669262008-01-14 17:46:18 -08003447 iwl3945_down(priv);
3448
3449 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003450
3451 /* start polling the killswitch state again */
3452 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3453 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003454
Tomas Winklere1623442009-01-27 14:27:56 -08003455 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003456}
3457
Johannes Berge039fa42008-05-15 12:55:29 +02003458static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003459{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003460 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003461
Tomas Winklere1623442009-01-27 14:27:56 -08003462 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003463
Tomas Winklere1623442009-01-27 14:27:56 -08003464 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003465 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003466
Johannes Berge039fa42008-05-15 12:55:29 +02003467 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003468 dev_kfree_skb_any(skb);
3469
Tomas Winklere1623442009-01-27 14:27:56 -08003470 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003471 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003472}
3473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003474static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003475 struct ieee80211_if_init_conf *conf)
3476{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003477 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003478 unsigned long flags;
3479
Tomas Winklere1623442009-01-27 14:27:56 -08003480 IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07003481
Johannes Berg32bfd352007-12-19 01:31:26 +01003482 if (priv->vif) {
Tomas Winklere1623442009-01-27 14:27:56 -08003483 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02003484 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07003485 }
3486
3487 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01003488 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07003489 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07003490
3491 spin_unlock_irqrestore(&priv->lock, flags);
3492
3493 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02003494
3495 if (conf->mac_addr) {
Tomas Winklere1623442009-01-27 14:27:56 -08003496 IWL_DEBUG_MAC80211(priv, "Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02003497 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3498 }
3499
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003500 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08003501 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07003502
Zhu Yib481de92007-09-25 17:54:57 -07003503 mutex_unlock(&priv->mutex);
3504
Tomas Winklere1623442009-01-27 14:27:56 -08003505 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003506 return 0;
3507}
3508
3509/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003510 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07003511 *
3512 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3513 * be set inappropriately and the driver currently sets the hardware up to
3514 * use it whenever needed.
3515 */
Johannes Berge8975582008-10-09 12:18:51 +02003516static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07003517{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003518 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003519 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02003520 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07003521 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08003522 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003523
3524 mutex_lock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003525 IWL_DEBUG_MAC80211(priv, "enter to channel %d\n",
3526 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07003527
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003528 if (!iwl_is_ready(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003529 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003530 ret = -EIO;
3531 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003532 }
3533
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003534 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07003535 test_bit(STATUS_SCANNING, &priv->status))) {
Tomas Winklere1623442009-01-27 14:27:56 -08003536 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
Zhu Yia0646472007-12-20 14:10:01 +08003537 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003538 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08003539 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003540 }
3541
3542 spin_lock_irqsave(&priv->lock, flags);
3543
Samuel Ortize6148912009-01-23 13:45:15 -08003544 ch_info = iwl_get_channel_info(priv, conf->channel->band,
3545 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07003546 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003547 IWL_DEBUG_SCAN(priv,
3548 "Channel %d [%d] is INVALID for this band.\n",
3549 conf->channel->hw_value, conf->channel->band);
3550 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
Zhu Yib481de92007-09-25 17:54:57 -07003551 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003552 ret = -EINVAL;
3553 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003554 }
3555
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003556 iwl_set_rxon_channel(priv, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003557
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003558 iwl_set_flags_for_band(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07003559
3560 /* The list of supported rates and rate mask can be different
3561 * for each phymode; since the phymode may have changed, reset
3562 * the rate mask to what mac80211 lists */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003563 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003564
3565 spin_unlock_irqrestore(&priv->lock, flags);
3566
3567#ifdef IEEE80211_CONF_CHANNEL_SWITCH
3568 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003569 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003570 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003571 }
3572#endif
3573
Mohamed Abbas37fec382009-03-17 21:51:42 -07003574 if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
3575 if (conf->radio_enabled &&
3576 iwl_radio_kill_sw_enable_radio(priv)) {
3577 IWL_DEBUG_MAC80211(priv, "leave - RF-KILL - "
3578 "waiting for uCode\n");
3579 goto out;
3580 }
Zhu Yib481de92007-09-25 17:54:57 -07003581
Mohamed Abbas37fec382009-03-17 21:51:42 -07003582 if (!conf->radio_enabled) {
3583 iwl_radio_kill_sw_disable_radio(priv);
3584 IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
3585 goto out;
3586 }
Zhu Yib481de92007-09-25 17:54:57 -07003587 }
3588
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003589 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003590 IWL_DEBUG_MAC80211(priv, "leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08003591 ret = -EIO;
3592 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07003593 }
3594
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003595 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003596
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003597 if (memcmp(&priv->active_rxon,
3598 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003599 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003600 else
Tomas Winklere1623442009-01-27 14:27:56 -08003601 IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration\n");
Zhu Yib481de92007-09-25 17:54:57 -07003602
Tomas Winklere1623442009-01-27 14:27:56 -08003603 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003604
Zhu Yi76bb77e2007-11-22 10:53:22 +08003605out:
Zhu Yia0646472007-12-20 14:10:01 +08003606 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003607 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08003608 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003609}
3610
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003611static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003612{
3613 int rc = 0;
3614
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003615 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003616 return;
3617
3618 /* The following should be done only at AP bring up */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003619 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003620
3621 /* RXON - unassoc (to set timing command) */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003622 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003623 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003624
3625 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08003626 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003627 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003628 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3629 sizeof(priv->rxon_timing),
3630 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07003631 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003632 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003633 "Attempting to continue.\n");
3634
3635 /* FIXME: what should be the assoc_id for AP? */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003636 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003637 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003638 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003639 RXON_FLG_SHORT_PREAMBLE_MSK;
3640 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003641 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003642 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3643
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003644 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003645 if (priv->assoc_capability &
3646 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003647 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003648 RXON_FLG_SHORT_SLOT_MSK;
3649 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003650 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003651 ~RXON_FLG_SHORT_SLOT_MSK;
3652
Johannes Berg05c914f2008-09-11 00:01:58 +02003653 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
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 }
3657 /* restore RXON assoc */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003658 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003659 iwlcore_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08003660 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08003661 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003662 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003663
3664 /* FIXME - we need to add code here to detect a totally new
3665 * configuration, reset the AP, unassoc, rxon timing, assoc,
3666 * clear sta table, add BCAST sta... */
3667}
3668
Johannes Berg32bfd352007-12-19 01:31:26 +01003669static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
3670 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003671 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07003672{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003673 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003674 int rc;
3675
3676 if (conf == NULL)
3677 return -EIO;
3678
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003679 if (priv->vif != vif) {
Tomas Winklere1623442009-01-27 14:27:56 -08003680 IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08003681 return 0;
3682 }
3683
Johannes Berg9d139c82008-07-09 14:40:37 +02003684 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02003685 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02003686 conf->changed & IEEE80211_IFCC_BEACON) {
3687 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
3688 if (!beacon)
3689 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08003690 mutex_lock(&priv->mutex);
Abhijeet Kolekar9944b932009-04-08 11:26:36 -07003691 rc = iwl_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08003692 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02003693 if (rc)
3694 return rc;
3695 }
3696
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003697 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08003698 return -EAGAIN;
3699
Zhu Yib481de92007-09-25 17:54:57 -07003700 mutex_lock(&priv->mutex);
3701
Zhu Yib481de92007-09-25 17:54:57 -07003702 if (conf->bssid)
Tomas Winklere1623442009-01-27 14:27:56 -08003703 IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07003704
Johannes Berg4150c572007-09-17 01:29:23 -04003705/*
3706 * very dubious code was here; the probe filtering flag is never set:
3707 *
Zhu Yib481de92007-09-25 17:54:57 -07003708 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
3709 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04003710 */
Zhu Yib481de92007-09-25 17:54:57 -07003711
Johannes Berg05c914f2008-09-11 00:01:58 +02003712 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07003713 if (!conf->bssid) {
3714 conf->bssid = priv->mac_addr;
3715 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08003716 IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07003717 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07003718 }
3719 if (priv->ibss_beacon)
3720 dev_kfree_skb(priv->ibss_beacon);
3721
Johannes Berg9d139c82008-07-09 14:40:37 +02003722 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07003723 }
3724
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003725 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08003726 goto done;
3727
Zhu Yib481de92007-09-25 17:54:57 -07003728 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
3729 !is_multicast_ether_addr(conf->bssid)) {
3730 /* If there is currently a HW scan going on in the background
3731 * then we need to cancel it else the RXON below will fail. */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003732 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003733 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07003734 "after 100ms\n");
Tomas Winklere1623442009-01-27 14:27:56 -08003735 IWL_DEBUG_MAC80211(priv, "leaving:scan abort failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07003736 mutex_unlock(&priv->mutex);
3737 return -EAGAIN;
3738 }
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003739 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07003740
3741 /* TODO: Audit driver for usage of these members and see
3742 * if mac80211 deprecates them (priv->bssid looks like it
3743 * shouldn't be there, but I haven't scanned the IBSS code
3744 * to verify) - jpk */
3745 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
3746
Johannes Berg05c914f2008-09-11 00:01:58 +02003747 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003748 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003749 else {
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003750 rc = iwlcore_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02003751 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003752 iwl3945_add_station(priv,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003753 priv->active_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003754 }
3755
3756 } else {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003757 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003758 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003759 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003760 }
3761
Mohamed Abbasfde35712007-11-29 11:10:15 +08003762 done:
Tomas Winklere1623442009-01-27 14:27:56 -08003763 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003764 mutex_unlock(&priv->mutex);
3765
3766 return 0;
3767}
3768
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003769static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003770 struct ieee80211_if_init_conf *conf)
3771{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003772 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003773
Tomas Winklere1623442009-01-27 14:27:56 -08003774 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003775
3776 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003777
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003778 if (iwl_is_ready_rf(priv)) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003779 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003780 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003781 iwlcore_commit_rxon(priv);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003782 }
Johannes Berg32bfd352007-12-19 01:31:26 +01003783 if (priv->vif == conf->vif) {
3784 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003785 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07003786 }
3787 mutex_unlock(&priv->mutex);
3788
Tomas Winklere1623442009-01-27 14:27:56 -08003789 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003790}
3791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003792static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003793 struct ieee80211_vif *vif,
3794 struct ieee80211_sta *sta,
3795 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003796{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003797 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01003798 const u8 *addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003799 int ret = 0;
3800 u8 sta_id = IWL_INVALID_STATION;
3801 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003802
Tomas Winklere1623442009-01-27 14:27:56 -08003803 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003804
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003805 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003806 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003807 return -EOPNOTSUPP;
3808 }
3809
Winkler, Tomas42986792009-01-19 15:30:22 -08003810 addr = sta ? sta->addr : iwl_bcast_addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003811 static_key = !iwl_is_associated(priv);
3812
3813 if (!static_key) {
3814 sta_id = iwl3945_hw_find_station(priv, addr);
3815 if (sta_id == IWL_INVALID_STATION) {
Joe Perches12514392009-04-16 16:23:26 -07003816 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003817 addr);
3818 return -EINVAL;
3819 }
Zhu Yib481de92007-09-25 17:54:57 -07003820 }
3821
3822 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003823 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003824 mutex_unlock(&priv->mutex);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003825
Zhu Yib481de92007-09-25 17:54:57 -07003826 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003827 case SET_KEY:
3828 if (static_key)
3829 ret = iwl3945_set_static_key(priv, key);
3830 else
3831 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3832 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003833 break;
3834 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003835 if (static_key)
3836 ret = iwl3945_remove_static_key(priv);
3837 else
3838 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3839 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003840 break;
3841 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003842 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003843 }
3844
Tomas Winklere1623442009-01-27 14:27:56 -08003845 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003846
Winkler, Tomas42986792009-01-19 15:30:22 -08003847 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003848}
3849
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003850static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07003851 struct ieee80211_tx_queue_stats *stats)
3852{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003853 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003854 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003855 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003856 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07003857 unsigned long flags;
3858
Tomas Winklere1623442009-01-27 14:27:56 -08003859 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003860
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003861 if (!iwl_is_ready_rf(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003862 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07003863 return -EIO;
3864 }
3865
3866 spin_lock_irqsave(&priv->lock, flags);
3867
3868 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003869 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07003870 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003871 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07003872
Johannes Berg57ffc582008-04-29 17:18:59 +02003873 stats[i].len = q->n_window - avail;
3874 stats[i].limit = q->n_window - q->high_mark;
3875 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07003876
3877 }
3878 spin_unlock_irqrestore(&priv->lock, flags);
3879
Tomas Winklere1623442009-01-27 14:27:56 -08003880 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003881
3882 return 0;
3883}
3884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003885static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003886{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003887 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003888 unsigned long flags;
3889
3890 mutex_lock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003891 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003892
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003893 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003894
Zhu Yib481de92007-09-25 17:54:57 -07003895 spin_lock_irqsave(&priv->lock, flags);
3896 priv->assoc_id = 0;
3897 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003898
3899 /* new association get rid of ibss beacon skb */
3900 if (priv->ibss_beacon)
3901 dev_kfree_skb(priv->ibss_beacon);
3902
3903 priv->ibss_beacon = NULL;
3904
3905 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08003906 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02003907 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07003908 priv->beacon_int = 0;
3909
3910 spin_unlock_irqrestore(&priv->lock, flags);
3911
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003912 if (!iwl_is_ready_rf(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003913 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08003914 mutex_unlock(&priv->mutex);
3915 return;
3916 }
3917
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003918 /* we are restarting association process
3919 * clear RXON_FILTER_ASSOC_MSK bit
3920 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003921 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003922 iwl_scan_cancel_timeout(priv, 100);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003923 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003924 iwlcore_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003925 }
3926
Zhu Yib481de92007-09-25 17:54:57 -07003927 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02003928 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003929
Tomas Winklere1623442009-01-27 14:27:56 -08003930 IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07003931 mutex_unlock(&priv->mutex);
3932 return;
3933 }
3934
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003935 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003936
3937 mutex_unlock(&priv->mutex);
3938
Tomas Winklere1623442009-01-27 14:27:56 -08003939 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003940
3941}
3942
Zhu Yib481de92007-09-25 17:54:57 -07003943/*****************************************************************************
3944 *
3945 * sysfs attributes
3946 *
3947 *****************************************************************************/
3948
Samuel Ortizd08853a2009-01-23 13:45:17 -08003949#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003950
3951/*
3952 * The following adds a new attribute to the sysfs representation
3953 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3954 * used for controlling the debug level.
3955 *
3956 * See the level definitions in iwl for details.
3957 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003958static ssize_t show_debug_level(struct device *d,
3959 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003960{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003961 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003962
3963 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003964}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003965static ssize_t store_debug_level(struct device *d,
3966 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003967 const char *buf, size_t count)
3968{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003969 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003970 unsigned long val;
3971 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003972
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003973 ret = strict_strtoul(buf, 0, &val);
3974 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003975 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003976 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003977 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07003978
3979 return strnlen(buf, count);
3980}
3981
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003982static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3983 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003984
Samuel Ortizd08853a2009-01-23 13:45:17 -08003985#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003986
Zhu Yib481de92007-09-25 17:54:57 -07003987static ssize_t show_temperature(struct device *d,
3988 struct device_attribute *attr, char *buf)
3989{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003990 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07003991
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003992 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003993 return -EAGAIN;
3994
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003995 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003996}
3997
3998static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3999
Zhu Yib481de92007-09-25 17:54:57 -07004000static ssize_t show_tx_power(struct device *d,
4001 struct device_attribute *attr, char *buf)
4002{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004003 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08004004 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07004005}
4006
4007static ssize_t store_tx_power(struct device *d,
4008 struct device_attribute *attr,
4009 const char *buf, size_t count)
4010{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004011 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004012 char *p = (char *)buf;
4013 u32 val;
4014
4015 val = simple_strtoul(p, &p, 10);
4016 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08004017 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07004018 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004019 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07004020
4021 return count;
4022}
4023
4024static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4025
4026static ssize_t show_flags(struct device *d,
4027 struct device_attribute *attr, char *buf)
4028{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004029 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004030
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004031 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07004032}
4033
4034static ssize_t store_flags(struct device *d,
4035 struct device_attribute *attr,
4036 const char *buf, size_t count)
4037{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004038 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004039 u32 flags = simple_strtoul(buf, NULL, 0);
4040
4041 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004042 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07004043 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004044 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004045 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004046 else {
Tomas Winklere1623442009-01-27 14:27:56 -08004047 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07004048 flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004049 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07004050 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004051 }
4052 }
4053 mutex_unlock(&priv->mutex);
4054
4055 return count;
4056}
4057
4058static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4059
4060static ssize_t show_filter_flags(struct device *d,
4061 struct device_attribute *attr, char *buf)
4062{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004063 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004064
4065 return sprintf(buf, "0x%04X\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004066 le32_to_cpu(priv->active_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07004067}
4068
4069static ssize_t store_filter_flags(struct device *d,
4070 struct device_attribute *attr,
4071 const char *buf, size_t count)
4072{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004073 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07004074 u32 filter_flags = simple_strtoul(buf, NULL, 0);
4075
4076 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004077 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07004078 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004079 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004080 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004081 else {
Tomas Winklere1623442009-01-27 14:27:56 -08004082 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07004083 "0x%04X\n", filter_flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004084 priv->staging_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07004085 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07004086 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004087 }
4088 }
4089 mutex_unlock(&priv->mutex);
4090
4091 return count;
4092}
4093
4094static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4095 store_filter_flags);
4096
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004097#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004098
4099static ssize_t show_measurement(struct device *d,
4100 struct device_attribute *attr, char *buf)
4101{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004102 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08004103 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07004104 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004105 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07004106 unsigned long flags;
4107
4108 spin_lock_irqsave(&priv->lock, flags);
4109 if (!(priv->measurement_status & MEASUREMENT_READY)) {
4110 spin_unlock_irqrestore(&priv->lock, flags);
4111 return 0;
4112 }
4113 memcpy(&measure_report, &priv->measure_report, size);
4114 priv->measurement_status = 0;
4115 spin_unlock_irqrestore(&priv->lock, flags);
4116
4117 while (size && (PAGE_SIZE - len)) {
4118 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4119 PAGE_SIZE - len, 1);
4120 len = strlen(buf);
4121 if (PAGE_SIZE - len)
4122 buf[len++] = '\n';
4123
4124 ofs += 16;
4125 size -= min(size, 16U);
4126 }
4127
4128 return len;
4129}
4130
4131static ssize_t store_measurement(struct device *d,
4132 struct device_attribute *attr,
4133 const char *buf, size_t count)
4134{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004135 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004136 struct ieee80211_measurement_params params = {
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004137 .channel = le16_to_cpu(priv->active_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07004138 .start_time = cpu_to_le64(priv->last_tsf),
4139 .duration = cpu_to_le16(1),
4140 };
4141 u8 type = IWL_MEASURE_BASIC;
4142 u8 buffer[32];
4143 u8 channel;
4144
4145 if (count) {
4146 char *p = buffer;
4147 strncpy(buffer, buf, min(sizeof(buffer), count));
4148 channel = simple_strtoul(p, NULL, 0);
4149 if (channel)
4150 params.channel = channel;
4151
4152 p = buffer;
4153 while (*p && *p != ' ')
4154 p++;
4155 if (*p)
4156 type = simple_strtoul(p + 1, NULL, 0);
4157 }
4158
Tomas Winklere1623442009-01-27 14:27:56 -08004159 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07004160 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004161 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07004162
4163 return count;
4164}
4165
4166static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4167 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004168#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07004169
Zhu Yib481de92007-09-25 17:54:57 -07004170static ssize_t store_retry_rate(struct device *d,
4171 struct device_attribute *attr,
4172 const char *buf, size_t count)
4173{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004174 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004175
4176 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4177 if (priv->retry_rate <= 0)
4178 priv->retry_rate = 1;
4179
4180 return count;
4181}
4182
4183static ssize_t show_retry_rate(struct device *d,
4184 struct device_attribute *attr, char *buf)
4185{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004186 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004187 return sprintf(buf, "%d", priv->retry_rate);
4188}
4189
4190static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4191 store_retry_rate);
4192
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004193
Zhu Yib481de92007-09-25 17:54:57 -07004194static ssize_t store_power_level(struct device *d,
4195 struct device_attribute *attr,
4196 const char *buf, size_t count)
4197{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004198 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004199 int ret;
4200 unsigned long mode;
Zhu Yib481de92007-09-25 17:54:57 -07004201
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004202
Zhu Yib481de92007-09-25 17:54:57 -07004203 mutex_lock(&priv->mutex);
4204
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004205 ret = strict_strtoul(buf, 10, &mode);
4206 if (ret)
4207 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004208
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004209 ret = iwl_power_set_user_mode(priv, mode);
4210 if (ret) {
4211 IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
4212 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004213 }
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004214 ret = count;
Zhu Yib481de92007-09-25 17:54:57 -07004215
4216 out:
4217 mutex_unlock(&priv->mutex);
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004218 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004219}
4220
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004221static ssize_t show_power_level(struct device *d,
4222 struct device_attribute *attr, char *buf)
4223{
4224 struct iwl_priv *priv = dev_get_drvdata(d);
4225 int mode = priv->power_data.user_power_setting;
4226 int system = priv->power_data.system_power_setting;
4227 int level = priv->power_data.power_mode;
4228 char *p = buf;
4229
4230 switch (system) {
4231 case IWL_POWER_SYS_AUTO:
4232 p += sprintf(p, "SYSTEM:auto");
4233 break;
4234 case IWL_POWER_SYS_AC:
4235 p += sprintf(p, "SYSTEM:ac");
4236 break;
4237 case IWL_POWER_SYS_BATTERY:
4238 p += sprintf(p, "SYSTEM:battery");
4239 break;
4240 }
4241
4242 p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
4243 "fixed" : "auto");
4244 p += sprintf(p, "\tINDEX:%d", level);
4245 p += sprintf(p, "\n");
4246 return p - buf + 1;
4247}
4248
4249static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
4250 show_power_level, store_power_level);
4251
Zhu Yib481de92007-09-25 17:54:57 -07004252#define MAX_WX_STRING 80
4253
4254/* Values are in microsecond */
4255static const s32 timeout_duration[] = {
4256 350000,
4257 250000,
4258 75000,
4259 37000,
4260 25000,
4261};
4262static const s32 period_duration[] = {
4263 400000,
4264 700000,
4265 1000000,
4266 1000000,
4267 1000000
4268};
4269
Zhu Yib481de92007-09-25 17:54:57 -07004270static ssize_t show_channels(struct device *d,
4271 struct device_attribute *attr, char *buf)
4272{
Johannes Berg8318d782008-01-24 19:38:38 +01004273 /* all this shit doesn't belong into sysfs anyway */
4274 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004275}
4276
4277static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4278
4279static ssize_t show_statistics(struct device *d,
4280 struct device_attribute *attr, char *buf)
4281{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004282 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004283 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07004284 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004285 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07004286 int rc = 0;
4287
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004288 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004289 return -EAGAIN;
4290
4291 mutex_lock(&priv->mutex);
Samuel Ortiz17f841c2009-01-23 13:45:20 -08004292 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004293 mutex_unlock(&priv->mutex);
4294
4295 if (rc) {
4296 len = sprintf(buf,
4297 "Error sending statistics request: 0x%08X\n", rc);
4298 return len;
4299 }
4300
4301 while (size && (PAGE_SIZE - len)) {
4302 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4303 PAGE_SIZE - len, 1);
4304 len = strlen(buf);
4305 if (PAGE_SIZE - len)
4306 buf[len++] = '\n';
4307
4308 ofs += 16;
4309 size -= min(size, 16U);
4310 }
4311
4312 return len;
4313}
4314
4315static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4316
4317static ssize_t show_antenna(struct device *d,
4318 struct device_attribute *attr, char *buf)
4319{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004320 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004321
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004322 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004323 return -EAGAIN;
4324
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08004325 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07004326}
4327
4328static ssize_t store_antenna(struct device *d,
4329 struct device_attribute *attr,
4330 const char *buf, size_t count)
4331{
Winkler, Tomas7530f852009-01-27 14:27:53 -08004332 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07004333 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07004334
4335 if (count == 0)
4336 return 0;
4337
4338 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08004339 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004340 return count;
4341 }
4342
4343 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08004344 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08004345 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07004346 } else
Tomas Winklere1623442009-01-27 14:27:56 -08004347 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07004348
4349
4350 return count;
4351}
4352
4353static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
4354
4355static ssize_t show_status(struct device *d,
4356 struct device_attribute *attr, char *buf)
4357{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004358 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004359 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004360 return -EAGAIN;
4361 return sprintf(buf, "0x%08x\n", (int)priv->status);
4362}
4363
4364static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4365
4366static ssize_t dump_error_log(struct device *d,
4367 struct device_attribute *attr,
4368 const char *buf, size_t count)
4369{
4370 char *p = (char *)buf;
4371
4372 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004373 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07004374
4375 return strnlen(buf, count);
4376}
4377
4378static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
4379
4380static ssize_t dump_event_log(struct device *d,
4381 struct device_attribute *attr,
4382 const char *buf, size_t count)
4383{
4384 char *p = (char *)buf;
4385
4386 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004387 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07004388
4389 return strnlen(buf, count);
4390}
4391
4392static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
4393
4394/*****************************************************************************
4395 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07004396 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07004397 *
4398 *****************************************************************************/
4399
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004400static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004401{
Reinette Chatred21050c2009-02-13 11:51:18 -08004402 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07004403
4404 init_waitqueue_head(&priv->wait_command_queue);
4405
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004406 INIT_WORK(&priv->up, iwl3945_bg_up);
4407 INIT_WORK(&priv->restart, iwl3945_bg_restart);
4408 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004409 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004410 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004411 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
4412 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Helmut Schaa26635162009-01-15 09:38:44 +01004413 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08004414 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4415 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
4416 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
4417 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07004418
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004419 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004420
4421 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004422 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07004423}
4424
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004425static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004426{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004427 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004428
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09004429 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004430 cancel_delayed_work(&priv->scan_check);
4431 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07004432 cancel_work_sync(&priv->beacon_update);
4433}
4434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004435static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07004436 &dev_attr_antenna.attr,
4437 &dev_attr_channels.attr,
4438 &dev_attr_dump_errors.attr,
4439 &dev_attr_dump_events.attr,
4440 &dev_attr_flags.attr,
4441 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004442#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07004443 &dev_attr_measurement.attr,
4444#endif
4445 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004446 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004447 &dev_attr_statistics.attr,
4448 &dev_attr_status.attr,
4449 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07004450 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08004451#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004452 &dev_attr_debug_level.attr,
4453#endif
Zhu Yib481de92007-09-25 17:54:57 -07004454 NULL
4455};
4456
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004457static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07004458 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004459 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07004460};
4461
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004462static struct ieee80211_ops iwl3945_hw_ops = {
4463 .tx = iwl3945_mac_tx,
4464 .start = iwl3945_mac_start,
4465 .stop = iwl3945_mac_stop,
4466 .add_interface = iwl3945_mac_add_interface,
4467 .remove_interface = iwl3945_mac_remove_interface,
4468 .config = iwl3945_mac_config,
4469 .config_interface = iwl3945_mac_config_interface,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004470 .configure_filter = iwl_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004471 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004472 .get_tx_stats = iwl3945_mac_get_tx_stats,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07004473 .conf_tx = iwl_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004474 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07004475 .bss_info_changed = iwl_bss_info_changed,
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -08004476 .hw_scan = iwl_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07004477};
4478
Winkler, Tomase52119c2008-12-22 11:31:19 +08004479static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004480{
4481 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08004482 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004483
4484 priv->retry_rate = 1;
4485 priv->ibss_beacon = NULL;
4486
4487 spin_lock_init(&priv->lock);
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08004488 spin_lock_init(&priv->power_data.lock);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004489 spin_lock_init(&priv->sta_lock);
4490 spin_lock_init(&priv->hcmd_lock);
4491
4492 INIT_LIST_HEAD(&priv->free_frames);
4493
4494 mutex_init(&priv->mutex);
4495
4496 /* Clear the driver's (not device's) station table */
4497 iwl3945_clear_stations_table(priv);
4498
4499 priv->data_retry_limit = -1;
4500 priv->ieee_channels = NULL;
4501 priv->ieee_rates = NULL;
4502 priv->band = IEEE80211_BAND_2GHZ;
4503
4504 priv->iw_mode = NL80211_IFTYPE_STATION;
4505
4506 iwl_reset_qos(priv);
4507
4508 priv->qos_data.qos_active = 0;
4509 priv->qos_data.qos_cap.val = 0;
4510
4511 priv->rates_mask = IWL_RATES_MASK;
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08004512 /* If power management is turned on, default to CAM mode */
4513 priv->power_mode = IWL_POWER_MODE_CAM;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08004514 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004515
Samuel Ortize6148912009-01-23 13:45:15 -08004516 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
4517 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
4518 eeprom->version);
4519 ret = -EINVAL;
4520 goto err;
4521 }
4522 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004523 if (ret) {
4524 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
4525 goto err;
4526 }
4527
Samuel Ortize6148912009-01-23 13:45:15 -08004528 /* Set up txpower settings in driver for all channels */
4529 if (iwl3945_txpower_set_from_eeprom(priv)) {
4530 ret = -EIO;
4531 goto err_free_channel_map;
4532 }
4533
Samuel Ortiz534166d2009-01-23 13:45:16 -08004534 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004535 if (ret) {
4536 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
4537 goto err_free_channel_map;
4538 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08004539 iwl3945_init_hw_rates(priv, priv->ieee_rates);
4540
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004541 return 0;
4542
4543err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08004544 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004545err:
4546 return ret;
4547}
4548
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004549static int iwl3945_setup_mac(struct iwl_priv *priv)
4550{
4551 int ret;
4552 struct ieee80211_hw *hw = priv->hw;
4553
4554 hw->rate_control_algorithm = "iwl-3945-rs";
4555 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
4556
4557 /* Tell mac80211 our characteristics */
4558 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Mohamed Abbasb1c60192009-03-17 21:51:47 -07004559 IEEE80211_HW_NOISE_DBM |
4560 IEEE80211_HW_SPECTRUM_MGMT;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004561
4562 hw->wiphy->interface_modes =
4563 BIT(NL80211_IFTYPE_STATION) |
4564 BIT(NL80211_IFTYPE_ADHOC);
4565
4566 hw->wiphy->custom_regulatory = true;
4567
Johannes Bergd60cc912009-04-09 09:56:02 +02004568 hw->wiphy->max_scan_ssids = 1; /* WILL FIX */
4569
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004570 /* Default value; 4 EDCA QOS priorities */
4571 hw->queues = 4;
4572
4573 hw->conf.beacon_int = 100;
4574
4575 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4576 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4577 &priv->bands[IEEE80211_BAND_2GHZ];
4578
4579 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4580 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4581 &priv->bands[IEEE80211_BAND_5GHZ];
4582
4583 ret = ieee80211_register_hw(priv->hw);
4584 if (ret) {
4585 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
4586 return ret;
4587 }
4588 priv->mac80211_registered = 1;
4589
4590 return 0;
4591}
4592
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004593static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07004594{
4595 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004596 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07004597 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08004598 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08004599 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004600 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004601
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004602 /***********************
4603 * 1. Allocating HW data
4604 * ********************/
4605
Zhu Yib481de92007-09-25 17:54:57 -07004606 /* mac80211 allocates memory for this device instance, including
4607 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004608 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07004609 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004610 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07004611 err = -ENOMEM;
4612 goto out;
4613 }
Zhu Yib481de92007-09-25 17:54:57 -07004614 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004615 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004616
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004617 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
4618 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004619 IWL_ERR(priv,
4620 "invalid queues_num, should be between %d and %d\n",
4621 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08004622 err = -EINVAL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004623 goto out_ieee80211_free_hw;
Samuel Ortiza3139c52008-12-19 10:37:09 +08004624 }
4625
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004626 /*
4627 * Disabling hardware scan means that mac80211 will perform scans
4628 * "the hard way", rather than using device's scan.
4629 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004630 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08004631 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004632 iwl3945_hw_ops.hw_scan = NULL;
4633 }
4634
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004635
Tomas Winklere1623442009-01-27 14:27:56 -08004636 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004637 priv->cfg = cfg;
4638 priv->pci_dev = pdev;
4639
Samuel Ortizd08853a2009-01-23 13:45:17 -08004640#ifdef CONFIG_IWLWIFI_DEBUG
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004641 priv->debug_level = iwl3945_mod_params.debug;
4642 atomic_set(&priv->restrict_refcnt, 0);
4643#endif
Zhu Yib481de92007-09-25 17:54:57 -07004644
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004645 /***************************
4646 * 2. Initializing PCI bus
4647 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07004648 if (pci_enable_device(pdev)) {
4649 err = -ENODEV;
4650 goto out_ieee80211_free_hw;
4651 }
4652
4653 pci_set_master(pdev);
4654
Yang Hongyang284901a2009-04-06 19:01:15 -07004655 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004656 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004657 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004658 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004659 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004660 goto out_pci_disable_device;
4661 }
4662
4663 pci_set_drvdata(pdev, priv);
4664 err = pci_request_regions(pdev, DRV_NAME);
4665 if (err)
4666 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004667
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004668 /***********************
4669 * 3. Read REV Register
4670 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004671 priv->hw_base = pci_iomap(pdev, 0, 0);
4672 if (!priv->hw_base) {
4673 err = -ENODEV;
4674 goto out_pci_release_regions;
4675 }
4676
Tomas Winklere1623442009-01-27 14:27:56 -08004677 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004678 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004679 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004680
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004681 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4682 * PCI Tx retries from interfering with C3 CPU state */
4683 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004684
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004685 /* amp init */
4686 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004687 if (err < 0) {
David S. Millerd5df2a12009-03-10 05:04:16 -07004688 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004689 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004690 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004691
4692 /***********************
4693 * 4. Read EEPROM
4694 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004695
Zhu Yi5a669262008-01-14 17:46:18 -08004696 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004697 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004698 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004699 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004700 goto out_iounmap;
Zhu Yi5a669262008-01-14 17:46:18 -08004701 }
4702 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004703 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4704 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08004705 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08004706 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4707
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004708 /***********************
4709 * 5. Setup HW Constants
4710 * ********************/
4711 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004712 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004713 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004714 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004715 }
4716
4717 /***********************
4718 * 6. Setup priv
4719 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004720
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004721 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004722 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004723 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004724 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004725 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004726
Tomas Winkler978785a2008-12-19 10:37:31 +08004727 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4728 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004729
4730 /***********************************
4731 * 7. Initialize Module Parameters
4732 * **********************************/
4733
4734 /* Initialize module parameter values here */
4735 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004736 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004737 set_bit(STATUS_RF_KILL_SW, &priv->status);
Tomas Winklere1623442009-01-27 14:27:56 -08004738 IWL_DEBUG_INFO(priv, "Radio disabled.\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004739 }
4740
4741
4742 /***********************
4743 * 8. Setup Services
4744 * ********************/
4745
4746 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004747 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004748 spin_unlock_irqrestore(&priv->lock, flags);
4749
Helmut Schaa26635162009-01-15 09:38:44 +01004750 pci_enable_msi(priv->pci_dev);
4751
Abhijeet Kolekarf17d08a2009-02-18 15:54:31 -08004752 err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
Helmut Schaa26635162009-01-15 09:38:44 +01004753 DRV_NAME, priv);
4754 if (err) {
4755 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4756 goto out_disable_msi;
4757 }
4758
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004759 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4760 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004761 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004762 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004763 }
4764
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004765 iwl_set_rxon_channel(priv,
4766 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004767 iwl3945_setup_deferred_work(priv);
4768 iwl3945_setup_rx_handlers(priv);
4769
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004770 /*********************************
Helmut Schaa26635162009-01-15 09:38:44 +01004771 * 9. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004772 * *******************************/
4773
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004774 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004775
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004776 err = iwl3945_setup_mac(priv);
4777 if (err)
4778 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004779
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004780 err = iwl_rfkill_init(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004781 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004782 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004783 "Ignoring error: %d\n", err);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004784 else
4785 iwl_rfkill_set_hw_state(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004786
Helmut Schaa26635162009-01-15 09:38:44 +01004787 /* Start monitoring the killswitch */
4788 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
4789 2 * HZ);
4790
Zhu Yib481de92007-09-25 17:54:57 -07004791 return 0;
4792
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004793 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004794 destroy_workqueue(priv->workqueue);
4795 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004796 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4797 out_release_irq:
4798 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004799 out_disable_msi:
4800 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004801 iwlcore_free_geos(priv);
4802 iwl_free_channel_map(priv);
4803 out_unset_hw_params:
4804 iwl3945_unset_hw_params(priv);
4805 out_eeprom_free:
4806 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004807 out_iounmap:
4808 pci_iounmap(pdev, priv->hw_base);
4809 out_pci_release_regions:
4810 pci_release_regions(pdev);
4811 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004812 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004813 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004814 out_ieee80211_free_hw:
4815 ieee80211_free_hw(priv->hw);
4816 out:
4817 return err;
4818}
4819
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004820static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004821{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004822 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004823 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004824
4825 if (!priv)
4826 return;
4827
Tomas Winklere1623442009-01-27 14:27:56 -08004828 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004829
Zhu Yib481de92007-09-25 17:54:57 -07004830 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004831
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004832 if (priv->mac80211_registered) {
4833 ieee80211_unregister_hw(priv->hw);
4834 priv->mac80211_registered = 0;
4835 } else {
4836 iwl3945_down(priv);
4837 }
Zhu Yib481de92007-09-25 17:54:57 -07004838
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004839 /* make sure we flush any pending irq or
4840 * tasklet for the driver
4841 */
4842 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004843 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004844 spin_unlock_irqrestore(&priv->lock, flags);
4845
4846 iwl_synchronize_irq(priv);
4847
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004848 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004849
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004850 iwl_rfkill_unregister(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004851 cancel_delayed_work(&priv->rfkill_poll);
4852
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004853 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004854
4855 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08004856 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004857 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004858
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004859 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004860 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004861
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004862 /*netif_stop_queue(dev); */
4863 flush_workqueue(priv->workqueue);
4864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004865 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004866 * priv->workqueue... so we can't take down the workqueue
4867 * until now... */
4868 destroy_workqueue(priv->workqueue);
4869 priv->workqueue = NULL;
4870
Helmut Schaa26635162009-01-15 09:38:44 +01004871 free_irq(pdev->irq, priv);
4872 pci_disable_msi(pdev);
4873
Zhu Yib481de92007-09-25 17:54:57 -07004874 pci_iounmap(pdev, priv->hw_base);
4875 pci_release_regions(pdev);
4876 pci_disable_device(pdev);
4877 pci_set_drvdata(pdev, NULL);
4878
Samuel Ortize6148912009-01-23 13:45:15 -08004879 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004880 iwlcore_free_geos(priv);
Winkler, Tomas805cee52009-01-19 15:30:28 -08004881 kfree(priv->scan);
Zhu Yib481de92007-09-25 17:54:57 -07004882 if (priv->ibss_beacon)
4883 dev_kfree_skb(priv->ibss_beacon);
4884
4885 ieee80211_free_hw(priv->hw);
4886}
4887
Zhu Yib481de92007-09-25 17:54:57 -07004888
4889/*****************************************************************************
4890 *
4891 * driver and module entry point
4892 *
4893 *****************************************************************************/
4894
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004895static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004896 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004897 .id_table = iwl3945_hw_card_ids,
4898 .probe = iwl3945_pci_probe,
4899 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004900#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004901 .suspend = iwl_pci_suspend,
4902 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004903#endif
4904};
4905
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004906static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004907{
4908
4909 int ret;
4910 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4911 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004912
4913 ret = iwl3945_rate_control_register();
4914 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004915 printk(KERN_ERR DRV_NAME
4916 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004917 return ret;
4918 }
4919
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004920 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004921 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004922 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004923 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004924 }
Zhu Yib481de92007-09-25 17:54:57 -07004925
4926 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004927
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004928error_register:
4929 iwl3945_rate_control_unregister();
4930 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004931}
4932
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004933static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004934{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004935 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004936 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004937}
4938
Reinette Chatrea0987a82008-12-02 12:14:06 -08004939MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004940
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004941module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004942MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004943module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004944MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004945module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
4946MODULE_PARM_DESC(swcrypto,
4947 "using software crypto (default 1 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004948module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004949MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004950module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004951MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
4952
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004953module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07004954MODULE_PARM_DESC(queues_num, "number of hw queues.");
4955
Samuel Ortizaf48d042009-01-23 13:45:19 -08004956module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
4957MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4958
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004959module_exit(iwl3945_exit);
4960module_init(iwl3945_init);