blob: 3575e7fbadc793f967e868ba47f059a7f273ec55 [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>
44#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Samuel Ortiza3139c52008-12-19 10:37:09 +080048#define DRV_NAME "iwl3945"
49
Winkler, Tomasdbb66542008-12-22 11:31:14 +080050#include "iwl-fh.h"
51#include "iwl-3945-fh.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080052#include "iwl-commands.h"
Samuel Ortiz17f841c2009-01-23 13:45:20 -080053#include "iwl-sta.h"
Zhu Yib481de92007-09-25 17:54:57 -070054#include "iwl-3945.h"
55#include "iwl-helpers.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080056#include "iwl-core.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080057#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070058
Zhu Yib481de92007-09-25 17:54:57 -070059/*
60 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070061 */
62
63#define DRV_DESCRIPTION \
64"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
65
Samuel Ortizd08853a2009-01-23 13:45:17 -080066#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070067#define VD "d"
68#else
69#define VD
70#endif
71
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080072#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070073#define VS "s"
74#else
75#define VS
76#endif
77
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080078#define IWL39_VERSION "1.2.26k" VD VS
Reinette Chatre01f81622009-01-08 10:20:02 -080079#define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080080#define DRV_AUTHOR "<ilw@linux.intel.com>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080081#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070082
Zhu Yib481de92007-09-25 17:54:57 -070083
84MODULE_DESCRIPTION(DRV_DESCRIPTION);
85MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080086MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070087MODULE_LICENSE("GPL");
88
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080089 /* module parameters */
90struct iwl_mod_params iwl3945_mod_params = {
Reinette Chatre5905a1a2009-07-09 10:33:40 -070091 .num_of_queues = IWL39_NUM_QUEUES, /* Not used */
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080092 .sw_crypto = 1,
Samuel Ortizaf48d042009-01-23 13:45:19 -080093 .restart_fw = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080094 /* the rest are 0 by default */
95};
96
Zhu Yib481de92007-09-25 17:54:57 -070097/**
Samuel Ortiz7e4bca52009-01-23 13:45:18 -080098 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
99 * @priv: eeprom and antenna fields are used to determine antenna flags
100 *
101 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
102 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
103 *
104 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
105 * IWL_ANTENNA_MAIN - Force MAIN antenna
106 * IWL_ANTENNA_AUX - Force AUX antenna
107 */
108__le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
109{
110 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
111
112 switch (iwl3945_mod_params.antenna) {
113 case IWL_ANTENNA_DIVERSITY:
114 return 0;
115
116 case IWL_ANTENNA_MAIN:
117 if (eeprom->antenna_switch_type)
118 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
119 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
120
121 case IWL_ANTENNA_AUX:
122 if (eeprom->antenna_switch_type)
123 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
124 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
125 }
126
127 /* bad antenna selector value */
128 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
129 iwl3945_mod_params.antenna);
130
131 return 0; /* "diversity" is default if error */
132}
133
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800134static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700135 struct ieee80211_key_conf *keyconf,
136 u8 sta_id)
137{
138 unsigned long flags;
139 __le16 key_flags = 0;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800140 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700141
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800142 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
143 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
144
145 if (sta_id == priv->hw_params.bcast_sta_id)
146 key_flags |= STA_KEY_MULTICAST_MSK;
147
148 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
149 keyconf->hw_key_idx = keyconf->keyidx;
150 key_flags &= ~STA_KEY_FLG_INVALID;
151
Zhu Yib481de92007-09-25 17:54:57 -0700152 spin_lock_irqsave(&priv->sta_lock, flags);
Tomas Winklerc587de02009-06-03 11:44:07 -0700153 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
154 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
155 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700156 keyconf->keylen);
157
Tomas Winklerc587de02009-06-03 11:44:07 -0700158 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700159 keyconf->keylen);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800160
Tomas Winklerc587de02009-06-03 11:44:07 -0700161 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800162 == STA_KEY_FLG_NO_ENC)
Tomas Winklerc587de02009-06-03 11:44:07 -0700163 priv->stations[sta_id].sta.key.key_offset =
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800164 iwl_get_free_ucode_key_index(priv);
165 /* else, we are overriding an existing key => no need to allocated room
166 * in uCode. */
167
Tomas Winklerc587de02009-06-03 11:44:07 -0700168 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800169 "no space for a new key");
170
Tomas Winklerc587de02009-06-03 11:44:07 -0700171 priv->stations[sta_id].sta.key.key_flags = key_flags;
172 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
173 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700174
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800175 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
176
Tomas Winklerc587de02009-06-03 11:44:07 -0700177 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800178
Zhu Yib481de92007-09-25 17:54:57 -0700179 spin_unlock_irqrestore(&priv->sta_lock, flags);
180
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800181 return ret;
182}
183
184static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
185 struct ieee80211_key_conf *keyconf,
186 u8 sta_id)
187{
188 return -EOPNOTSUPP;
189}
190
191static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
192 struct ieee80211_key_conf *keyconf,
193 u8 sta_id)
194{
195 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -0700196}
197
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800198static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700199{
200 unsigned long flags;
201
202 spin_lock_irqsave(&priv->sta_lock, flags);
Tomas Winklerc587de02009-06-03 11:44:07 -0700203 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
204 memset(&priv->stations[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +0800205 sizeof(struct iwl4965_keyinfo));
Tomas Winklerc587de02009-06-03 11:44:07 -0700206 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
207 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
208 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700209 spin_unlock_irqrestore(&priv->sta_lock, flags);
210
Tomas Winklere1623442009-01-27 14:27:56 -0800211 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
Tomas Winklerc587de02009-06-03 11:44:07 -0700212 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700213 return 0;
214}
215
Abhijeet Kolekarfa11d522009-03-11 11:17:54 -0700216static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800217 struct ieee80211_key_conf *keyconf, u8 sta_id)
218{
219 int ret = 0;
220
221 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
222
223 switch (keyconf->alg) {
224 case ALG_CCMP:
225 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
226 break;
227 case ALG_TKIP:
228 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
229 break;
230 case ALG_WEP:
231 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
232 break;
233 default:
Abhijeet Kolekar1e680232009-03-17 21:51:50 -0700234 IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800235 ret = -EINVAL;
236 }
237
238 IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
239 keyconf->alg, keyconf->keylen, keyconf->keyidx,
240 sta_id, ret);
241
242 return ret;
243}
244
245static int iwl3945_remove_static_key(struct iwl_priv *priv)
246{
247 int ret = -EOPNOTSUPP;
248
249 return ret;
250}
251
252static int iwl3945_set_static_key(struct iwl_priv *priv,
253 struct ieee80211_key_conf *key)
254{
255 if (key->alg == ALG_WEP)
256 return -EOPNOTSUPP;
257
258 IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
259 return -EINVAL;
260}
261
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800262static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700263{
264 struct list_head *element;
265
Tomas Winklere1623442009-01-27 14:27:56 -0800266 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700267 priv->frames_count);
268
269 while (!list_empty(&priv->free_frames)) {
270 element = priv->free_frames.next;
271 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800272 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700273 priv->frames_count--;
274 }
275
276 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800277 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700278 priv->frames_count);
279 priv->frames_count = 0;
280 }
281}
282
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800283static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700284{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800285 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700286 struct list_head *element;
287 if (list_empty(&priv->free_frames)) {
288 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
289 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800290 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700291 return NULL;
292 }
293
294 priv->frames_count++;
295 return frame;
296 }
297
298 element = priv->free_frames.next;
299 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800300 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700301}
302
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800303static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700304{
305 memset(frame, 0, sizeof(*frame));
306 list_add(&frame->list, &priv->free_frames);
307}
308
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800309unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700310 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200311 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700312{
313
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800314 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200315 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
316 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -0700317 return 0;
318
319 if (priv->ibss_beacon->len > left)
320 return 0;
321
322 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
323
324 return priv->ibss_beacon->len;
325}
326
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800327static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700328{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800329 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700330 unsigned int frame_size;
331 int rc;
332 u8 rate;
333
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800334 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700335
336 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800337 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700338 "command.\n");
339 return -ENOMEM;
340 }
341
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800342 rate = iwl_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800344 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700345
Samuel Ortiz518099a2009-01-19 15:30:27 -0800346 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700347 &frame->u.cmd[0]);
348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800349 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700350
351 return rc;
352}
353
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800354static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700355{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800356 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -0700357 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800358 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800359 priv->shared_virt,
360 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700361}
362
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800363static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200364 struct ieee80211_tx_info *info,
Johannes Bergc2acea82009-07-24 11:13:05 -0700365 struct iwl_device_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700366 struct sk_buff *skb_frag,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800367 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700368{
Winkler, Tomase52119c2008-12-22 11:31:19 +0800369 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Tomas Winklerc587de02009-06-03 11:44:07 -0700370 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -0700371
372 switch (keyinfo->alg) {
373 case ALG_CCMP:
Winkler, Tomase52119c2008-12-22 11:31:19 +0800374 tx->sec_ctl = TX_CMD_SEC_CCM;
375 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
Tomas Winklere1623442009-01-27 14:27:56 -0800376 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -0700377 break;
378
379 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -0700380 break;
381
382 case ALG_WEP:
Winkler, Tomase52119c2008-12-22 11:31:19 +0800383 tx->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +0200384 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -0700385
386 if (keyinfo->keylen == 13)
Winkler, Tomase52119c2008-12-22 11:31:19 +0800387 tx->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -0700388
Winkler, Tomase52119c2008-12-22 11:31:19 +0800389 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -0700390
Tomas Winklere1623442009-01-27 14:27:56 -0800391 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +0200392 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -0700393 break;
394
Zhu Yib481de92007-09-25 17:54:57 -0700395 default:
Tomas Winkler978785a2008-12-19 10:37:31 +0800396 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -0700397 break;
398 }
399}
400
401/*
402 * handle build REPLY_TX command notification.
403 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800404static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Johannes Bergc2acea82009-07-24 11:13:05 -0700405 struct iwl_device_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200406 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +0800407 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -0700408{
Winkler, Tomase52119c2008-12-22 11:31:19 +0800409 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
410 __le32 tx_flags = tx->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700411 __le16 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +0200412 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -0700413
Winkler, Tomase52119c2008-12-22 11:31:19 +0800414 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200415 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700416 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700417 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700418 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700419 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -0700420 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
421 tx_flags |= TX_CMD_FLG_TSF_MSK;
422 } else {
423 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
424 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
425 }
426
Winkler, Tomase52119c2008-12-22 11:31:19 +0800427 tx->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700428 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700429 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
430
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700431 if (ieee80211_is_data_qos(fc)) {
432 u8 *qc = ieee80211_get_qos_ctl(hdr);
Winkler, Tomase52119c2008-12-22 11:31:19 +0800433 tx->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -0700434 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800435 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700436 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800437 }
Zhu Yib481de92007-09-25 17:54:57 -0700438
Johannes Berge6a98542008-10-21 12:40:02 +0200439 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -0700440 tx_flags |= TX_CMD_FLG_RTS_MSK;
441 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +0200442 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -0700443 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
444 tx_flags |= TX_CMD_FLG_CTS_MSK;
445 }
446
447 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
448 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
449
450 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700451 if (ieee80211_is_mgmt(fc)) {
452 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Winkler, Tomase52119c2008-12-22 11:31:19 +0800453 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -0700454 else
Winkler, Tomase52119c2008-12-22 11:31:19 +0800455 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700456 } else {
Winkler, Tomase52119c2008-12-22 11:31:19 +0800457 tx->timeout.pm_frame_timeout = 0;
Abhijeet Kolekar5c8df2d2009-03-11 11:17:55 -0700458#ifdef CONFIG_IWLWIFI_LEDS
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700459 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
460#endif
461 }
Zhu Yib481de92007-09-25 17:54:57 -0700462
Winkler, Tomase52119c2008-12-22 11:31:19 +0800463 tx->driver_txop = 0;
464 tx->tx_flags = tx_flags;
465 tx->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700466}
467
Zhu Yib481de92007-09-25 17:54:57 -0700468/*
469 * start REPLY_TX command process
470 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800471static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700472{
473 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200474 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomase52119c2008-12-22 11:31:19 +0800475 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800476 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800477 struct iwl_queue *q = NULL;
Johannes Bergc2acea82009-07-24 11:13:05 -0700478 struct iwl_device_cmd *out_cmd;
479 struct iwl_cmd_meta *out_meta;
Zhu Yib481de92007-09-25 17:54:57 -0700480 dma_addr_t phys_addr;
481 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800482 int txq_id = skb_get_queue_mapping(skb);
Reinette Chatredf833b12009-04-21 10:55:48 -0700483 u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
Tomas Winkler54dbb522008-05-15 13:54:06 +0800484 u8 id;
485 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700486 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800487 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700488 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700489 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700490 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800491 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700492 unsigned long flags;
493 int rc;
494
495 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800496 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800497 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700498 goto drop_unlock;
499 }
500
Johannes Berge039fa42008-05-15 12:55:29 +0200501 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800502 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700503 goto drop_unlock;
504 }
505
506 unicast = !is_multicast_ether_addr(hdr->addr1);
507 id = 0;
508
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700509 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700510
Samuel Ortizd08853a2009-01-23 13:45:17 -0800511#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700512 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800513 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700514 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800515 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700516 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800517 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700518#endif
519
Gábor Stefanikaa065262009-08-21 20:44:09 +0200520 /* drop all non-injected data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +0800521 if (ieee80211_is_data(fc) &&
Gábor Stefanikaa065262009-08-21 20:44:09 +0200522 !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800523 (!iwl_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200524 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Tomas Winklere1623442009-01-27 14:27:56 -0800525 IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -0700526 goto drop_unlock;
527 }
528
529 spin_unlock_irqrestore(&priv->lock, flags);
530
Harvey Harrison7294ec92008-07-15 18:43:59 -0700531 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800532
533 /* Find (or create) index into station table for destination station */
Gábor Stefanikaa065262009-08-21 20:44:09 +0200534 if (info->flags & IEEE80211_TX_CTL_INJECTED)
535 sta_id = priv->hw_params.bcast_sta_id;
536 else
537 sta_id = iwl_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -0700538 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800539 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700540 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700541 goto drop;
542 }
543
Tomas Winklere1623442009-01-27 14:27:56 -0800544 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700545
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700546 if (ieee80211_is_data_qos(fc)) {
547 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700548 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700549 if (unlikely(tid >= MAX_TID_COUNT))
550 goto drop;
Tomas Winklerc587de02009-06-03 11:44:07 -0700551 seq_number = priv->stations[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -0700552 IEEE80211_SCTL_SEQ;
553 hdr->seq_ctrl = cpu_to_le16(seq_number) |
554 (hdr->seq_ctrl &
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800555 cpu_to_le16(IEEE80211_SCTL_FRAG));
Zhu Yib481de92007-09-25 17:54:57 -0700556 seq_number += 0x10;
557 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800558
559 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800560 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700561 q = &txq->q;
562
563 spin_lock_irqsave(&priv->lock, flags);
564
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800565 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700566
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800567 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800568 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800569 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800570
571 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800572 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700573 out_meta = &txq->meta[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +0800574 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700575 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +0800576 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800577
578 /*
579 * Set up the Tx-command (not MAC!) header.
580 * Store the chosen Tx queue and TFD index within the sequence field;
581 * after Tx, uCode's Tx response will return this value so driver can
582 * locate the frame within the tx queue and do post-tx processing.
583 */
Zhu Yib481de92007-09-25 17:54:57 -0700584 out_cmd->hdr.cmd = REPLY_TX;
585 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800586 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800587
588 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +0800589 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700590
Reinette Chatredf833b12009-04-21 10:55:48 -0700591
592 if (info->control.hw_key)
593 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
594
595 /* TODO need this for burst mode later on */
596 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
597
598 /* set is_hcca to 0; it probably will never be implemented */
599 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
600
601 /* Total # bytes to be transmitted */
602 len = (u16)skb->len;
603 tx->len = cpu_to_le16(len);
604
Wey-Yi Guy20594eb2009-08-07 15:41:39 -0700605 iwl_dbg_log_tx_data_frame(priv, len, hdr);
Wey-Yi Guy22fdf3c2009-08-07 15:41:40 -0700606 iwl_update_stats(priv, true, fc, len);
Reinette Chatredf833b12009-04-21 10:55:48 -0700607 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
608 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
609
610 if (!ieee80211_has_morefrags(hdr->frame_control)) {
611 txq->need_update = 1;
612 if (qc)
Tomas Winklerc587de02009-06-03 11:44:07 -0700613 priv->stations[sta_id].tid[tid].seq_number = seq_number;
Reinette Chatredf833b12009-04-21 10:55:48 -0700614 } else {
615 wait_write_ptr = 1;
616 txq->need_update = 0;
617 }
618
619 IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n",
620 le16_to_cpu(out_cmd->hdr.sequence));
621 IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx->tx_flags));
Reinette Chatre3d816c72009-08-07 15:41:37 -0700622 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
623 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Reinette Chatredf833b12009-04-21 10:55:48 -0700624 ieee80211_hdrlen(fc));
625
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800626 /*
627 * Use the first empty entry in this queue's command buffer array
628 * to contain the Tx command and MAC header concatenated together
629 * (payload data will be in another buffer).
630 * Size of this varies, due to varying MAC header length.
631 * If end is not dword aligned, we'll have 2 extra bytes at the end
632 * of the MAC header (device reads on dword boundaries).
633 * We'll tell device about this padding later.
634 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800635 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800636 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700637
638 len_org = len;
639 len = (len + 3) & ~3;
640
641 if (len_org != len)
642 len_org = 1;
643 else
644 len_org = 0;
645
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800646 /* Physical address of this Tx command's header (not MAC header!),
647 * within command buffer array. */
Reinette Chatredf833b12009-04-21 10:55:48 -0700648 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
649 len, PCI_DMA_TODEVICE);
650 /* we do not map meta data ... so we can safely access address to
651 * provide to unmap command*/
Johannes Bergc2acea82009-07-24 11:13:05 -0700652 pci_unmap_addr_set(out_meta, mapping, txcmd_phys);
653 pci_unmap_len_set(out_meta, len, len);
Zhu Yib481de92007-09-25 17:54:57 -0700654
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800655 /* Add buffer containing Tx command and MAC(!) header to TFD's
656 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800657 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
658 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700659
Zhu Yib481de92007-09-25 17:54:57 -0700660
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800661 /* Set up TFD's 2nd entry to point directly to remainder of skb,
662 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700663 len = skb->len - hdr_len;
664 if (len) {
665 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
666 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800667 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
668 phys_addr, len,
669 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700670 }
671
Zhu Yib481de92007-09-25 17:54:57 -0700672
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800673 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800674 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800675 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700676 spin_unlock_irqrestore(&priv->lock, flags);
677
678 if (rc)
679 return rc;
680
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800681 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700682 && priv->mac80211_registered) {
683 if (wait_write_ptr) {
684 spin_lock_irqsave(&priv->lock, flags);
685 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800686 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700687 spin_unlock_irqrestore(&priv->lock, flags);
688 }
689
Johannes Berge4e72fb2009-03-23 17:28:42 +0100690 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700691 }
692
693 return 0;
694
695drop_unlock:
696 spin_unlock_irqrestore(&priv->lock, flags);
697drop:
698 return -1;
699}
700
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800701#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -0700702
703#include "iwl-spectrum.h"
704
705#define BEACON_TIME_MASK_LOW 0x00FFFFFF
706#define BEACON_TIME_MASK_HIGH 0xFF000000
707#define TIME_UNIT 1024
708
709/*
710 * extended beacon time format
711 * time in usec will be changed into a 32-bit value in 8:24 format
712 * the high 1 byte is the beacon counts
713 * the lower 3 bytes is the time in usec within one beacon interval
714 */
715
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800716static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700717{
718 u32 quot;
719 u32 rem;
720 u32 interval = beacon_interval * 1024;
721
722 if (!interval || !usec)
723 return 0;
724
725 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
726 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
727
728 return (quot << 24) + rem;
729}
730
731/* base is usually what we get from ucode with each received frame,
732 * the same as HW timer counter counting down
733 */
734
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800735static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700736{
737 u32 base_low = base & BEACON_TIME_MASK_LOW;
738 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
739 u32 interval = beacon_interval * TIME_UNIT;
740 u32 res = (base & BEACON_TIME_MASK_HIGH) +
741 (addon & BEACON_TIME_MASK_HIGH);
742
743 if (base_low > addon_low)
744 res += base_low - addon_low;
745 else if (base_low < addon_low) {
746 res += interval + base_low - addon_low;
747 res += (1 << 24);
748 } else
749 res += (1 << 24);
750
751 return cpu_to_le32(res);
752}
753
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800754static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700755 struct ieee80211_measurement_params *params,
756 u8 type)
757{
Tomas Winkler600c0e12008-12-19 10:37:04 +0800758 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800759 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800760 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700761 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
762 .data = (void *)&spectrum,
Johannes Bergc2acea82009-07-24 11:13:05 -0700763 .flags = CMD_WANT_SKB,
Zhu Yib481de92007-09-25 17:54:57 -0700764 };
765 u32 add_time = le64_to_cpu(params->start_time);
766 int rc;
767 int spectrum_resp_status;
768 int duration = le16_to_cpu(params->duration);
769
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800770 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700771 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800772 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -0700773 le64_to_cpu(params->start_time) - priv->last_tsf,
774 le16_to_cpu(priv->rxon_timing.beacon_interval));
775
776 memset(&spectrum, 0, sizeof(spectrum));
777
778 spectrum.channel_count = cpu_to_le16(1);
779 spectrum.flags =
780 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
781 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
782 cmd.len = sizeof(spectrum);
783 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
784
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800785 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700786 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800787 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -0700788 add_time,
789 le16_to_cpu(priv->rxon_timing.beacon_interval));
790 else
791 spectrum.start_time = 0;
792
793 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
794 spectrum.channels[0].channel = params->channel;
795 spectrum.channels[0].type = type;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800796 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700797 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
798 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
799
Samuel Ortiz518099a2009-01-19 15:30:27 -0800800 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700801 if (rc)
802 return rc;
803
Johannes Bergc2acea82009-07-24 11:13:05 -0700804 res = (struct iwl_rx_packet *)cmd.reply_skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700805 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800806 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700807 rc = -EIO;
808 }
809
810 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
811 switch (spectrum_resp_status) {
812 case 0: /* Command will be handled */
813 if (res->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800814 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Ian Schrambc434dd2007-10-25 17:15:29 +0800815 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700816 priv->measurement_status &= ~MEASUREMENT_READY;
817 }
818 priv->measurement_status |= MEASUREMENT_ACTIVE;
819 rc = 0;
820 break;
821
822 case 1: /* Command will not be handled */
823 rc = -EAGAIN;
824 break;
825 }
826
Johannes Bergc2acea82009-07-24 11:13:05 -0700827 dev_kfree_skb_any(cmd.reply_skb);
Zhu Yib481de92007-09-25 17:54:57 -0700828
829 return rc;
830}
831#endif
832
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800833static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800834 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700835{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800836 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
837 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700838 struct delayed_work *pwork;
839
840 palive = &pkt->u.alive_frame;
841
Tomas Winklere1623442009-01-27 14:27:56 -0800842 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700843 "0x%01X 0x%01X\n",
844 palive->is_valid, palive->ver_type,
845 palive->ver_subtype);
846
847 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800848 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800849 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
850 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700851 pwork = &priv->init_alive_start;
852 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800853 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700854 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800855 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700856 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800857 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700858 }
859
860 /* We delay the ALIVE response by 5ms to
861 * give the HW RF Kill time to activate... */
862 if (palive->is_valid == UCODE_VALID_OK)
863 queue_delayed_work(priv->workqueue, pwork,
864 msecs_to_jiffies(5));
865 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800866 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700867}
868
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800869static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800870 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700871{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100872#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800873 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Helmut Schaac7e035a2009-01-19 13:02:15 +0100874#endif
Zhu Yib481de92007-09-25 17:54:57 -0700875
Tomas Winklere1623442009-01-27 14:27:56 -0800876 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700877 return;
878}
879
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800880static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700881{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800882 struct iwl_priv *priv =
883 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700884 struct sk_buff *beacon;
885
886 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200887 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700888
889 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800890 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700891 return;
892 }
893
894 mutex_lock(&priv->mutex);
895 /* new beacon skb is allocated every time; dispose previous.*/
896 if (priv->ibss_beacon)
897 dev_kfree_skb(priv->ibss_beacon);
898
899 priv->ibss_beacon = beacon;
900 mutex_unlock(&priv->mutex);
901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800902 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700903}
904
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800905static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800906 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700907{
Samuel Ortizd08853a2009-01-23 13:45:17 -0800908#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800909 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800910 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -0700911 u8 rate = beacon->beacon_notify_hdr.rate;
912
Tomas Winklere1623442009-01-27 14:27:56 -0800913 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700914 "tsf %d %d rate %d\n",
915 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
916 beacon->beacon_notify_hdr.failure_frame,
917 le32_to_cpu(beacon->ibss_mgr_status),
918 le32_to_cpu(beacon->high_tsf),
919 le32_to_cpu(beacon->low_tsf), rate);
920#endif
921
Johannes Berg05c914f2008-09-11 00:01:58 +0200922 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700923 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
924 queue_work(priv->workqueue, &priv->beacon_update);
925}
926
Zhu Yib481de92007-09-25 17:54:57 -0700927/* Handle notification from uCode that card's power state is changing
928 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800929static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800930 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700931{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800932 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700933 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
934 unsigned long status = priv->status;
935
Reinette Chatre4c423a22009-07-17 09:30:26 -0700936 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700937 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
938 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
939
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800940 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700941 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
942
943 if (flags & HW_CARD_DISABLED)
944 set_bit(STATUS_RF_KILL_HW, &priv->status);
945 else
946 clear_bit(STATUS_RF_KILL_HW, &priv->status);
947
948
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800949 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700950
951 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200952 test_bit(STATUS_RF_KILL_HW, &priv->status)))
953 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
954 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700955 else
956 wake_up_interruptible(&priv->wait_command_queue);
957}
958
959/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800960 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700961 *
962 * Setup the RX handlers for each of the reply types sent from the uCode
963 * to the host.
964 *
965 * This function chains into the hardware specific files for them to setup
966 * any hardware specific handlers as well.
967 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800968static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700969{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800970 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
971 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800972 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800973 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800974 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700975 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800976 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800977 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700978
Ben Cahill9fbab512007-11-29 11:09:47 +0800979 /*
980 * The same handler is used for both the REPLY to a discrete
981 * statistics request from the host as well as for the periodic
982 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700983 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800984 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
985 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700986
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800987 iwl_setup_spectrum_handlers(priv);
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800988 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800989 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700990
Ben Cahill9fbab512007-11-29 11:09:47 +0800991 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800992 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700993}
994
Zhu Yib481de92007-09-25 17:54:57 -0700995/************************** RX-FUNCTIONS ****************************/
996/*
997 * Rx theory of operation
998 *
999 * The host allocates 32 DMA target addresses and passes the host address
1000 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
1001 * 0 to 31
1002 *
1003 * Rx Queue Indexes
1004 * The host/firmware share two index registers for managing the Rx buffers.
1005 *
1006 * The READ index maps to the first position that the firmware may be writing
1007 * to -- the driver can read up to (but not including) this position and get
1008 * good data.
1009 * The READ index is managed by the firmware once the card is enabled.
1010 *
1011 * The WRITE index maps to the last position the driver has read from -- the
1012 * position preceding WRITE is the last slot the firmware can place a packet.
1013 *
1014 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
1015 * WRITE = READ.
1016 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001017 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07001018 * INDEX position, and WRITE to the last (READ - 1 wrapped)
1019 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001020 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07001021 * and fire the RX interrupt. The driver can then query the READ index and
1022 * process as many packets as possible, moving the WRITE index forward as it
1023 * resets the Rx queue buffers with new memory.
1024 *
1025 * The management in the driver is as follows:
1026 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
1027 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08001028 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001029 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07001030 * iwl->rxq is replenished and the READ INDEX is updated (updating the
1031 * 'processed' and 'read' driver indexes as well)
1032 * + A received packet is processed and handed to the kernel network stack,
1033 * detached from the iwl->rxq. The driver 'processed' index is updated.
1034 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1035 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1036 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
1037 * were enough free buffers and RX_STALLED is set it is cleared.
1038 *
1039 *
1040 * Driver sequence:
1041 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001042 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001043 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08001044 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07001045 * queue, updates firmware pointers, and updates
1046 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001047 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07001048 *
1049 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001050 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07001051 * READ INDEX, detaching the SKB from the pool.
1052 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001053 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07001054 * slots.
1055 * ...
1056 *
1057 */
1058
1059/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001060 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07001061 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001062static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001063 dma_addr_t dma_addr)
1064{
1065 return cpu_to_le32((u32)dma_addr);
1066}
1067
1068/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001069 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07001070 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001071 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07001072 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08001073 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07001074 *
1075 * This moves the 'write' index forward to catch up with 'processed', and
1076 * also updates the memory address in the firmware to reference the new
1077 * target buffer.
1078 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001079static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001080{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001081 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001082 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001083 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001084 unsigned long flags;
1085 int write, rc;
1086
1087 spin_lock_irqsave(&rxq->lock, flags);
1088 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001089 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001090 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001091 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001092 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001093 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001094
1095 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001096 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001097 rxq->queue[rxq->write] = rxb;
1098 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1099 rxq->free_count--;
1100 }
1101 spin_unlock_irqrestore(&rxq->lock, flags);
1102 /* If the pre-allocated buffer pool is dropping low, schedule to
1103 * refill it */
1104 if (rxq->free_count <= RX_LOW_WATERMARK)
1105 queue_work(priv->workqueue, &priv->rx_replenish);
1106
1107
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001108 /* If we've added more space for the firmware to place data, tell it.
1109 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001110 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001111 || (abs(rxq->write - rxq->read) > 7)) {
1112 spin_lock_irqsave(&rxq->lock, flags);
1113 rxq->need_update = 1;
1114 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001115 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001116 if (rc)
1117 return rc;
1118 }
1119
1120 return 0;
1121}
1122
1123/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001124 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001125 *
1126 * When moving to rx_free an SKB is allocated for the slot.
1127 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001128 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001129 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001130 */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001131static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001132{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001133 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001134 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001135 struct iwl_rx_mem_buffer *rxb;
Reinette Chatrede0bd502009-09-11 10:38:12 -07001136 struct sk_buff *skb;
Zhu Yib481de92007-09-25 17:54:57 -07001137 unsigned long flags;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001138
1139 while (1) {
1140 spin_lock_irqsave(&rxq->lock, flags);
1141
1142 if (list_empty(&rxq->rx_used)) {
1143 spin_unlock_irqrestore(&rxq->lock, flags);
1144 return;
1145 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001146 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001147
Reinette Chatref82a9242009-09-17 10:43:56 -07001148 if (rxq->free_count > RX_LOW_WATERMARK)
1149 priority |= __GFP_NOWARN;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001150 /* Alloc a new receive buffer */
Reinette Chatrede0bd502009-09-11 10:38:12 -07001151 skb = alloc_skb(priv->hw_params.rx_buf_size, priority);
1152 if (!skb) {
Zhu Yib481de92007-09-25 17:54:57 -07001153 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001154 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1155 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1156 net_ratelimit())
1157 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1158 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1159 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001160 /* We don't reschedule replenish work here -- we will
1161 * call the restock method and if it still needs
1162 * more buffers it will schedule replenish */
1163 break;
1164 }
Zhu Yi12342c42007-12-20 11:27:32 +08001165
Reinette Chatrede0bd502009-09-11 10:38:12 -07001166 spin_lock_irqsave(&rxq->lock, flags);
1167 if (list_empty(&rxq->rx_used)) {
1168 spin_unlock_irqrestore(&rxq->lock, flags);
1169 dev_kfree_skb_any(skb);
1170 return;
1171 }
1172 element = rxq->rx_used.next;
1173 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1174 list_del(element);
1175 spin_unlock_irqrestore(&rxq->lock, flags);
1176
1177 rxb->skb = skb;
1178
Zhu Yi12342c42007-12-20 11:27:32 +08001179 /* If radiotap head is required, reserve some headroom here.
1180 * The physical head count is a variable rx_stats->phy_count.
1181 * We reserve 4 bytes here. Plus these extra bytes, the
1182 * headroom of the physical head should be enough for the
1183 * radiotap head that iwl3945 supported. See iwl3945_rt.
1184 */
1185 skb_reserve(rxb->skb, 4);
1186
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001187 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08001188 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
1189 rxb->skb->data,
1190 priv->hw_params.rx_buf_size,
1191 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001192
1193 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001194 list_add_tail(&rxb->list, &rxq->rx_free);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001195 priv->alloc_rxb_skb++;
Zhu Yib481de92007-09-25 17:54:57 -07001196 rxq->free_count++;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001197 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001198 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001199}
1200
Reinette Chatredf833b12009-04-21 10:55:48 -07001201void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1202{
1203 unsigned long flags;
1204 int i;
1205 spin_lock_irqsave(&rxq->lock, flags);
1206 INIT_LIST_HEAD(&rxq->rx_free);
1207 INIT_LIST_HEAD(&rxq->rx_used);
1208 /* Fill the rx_used queue with _all_ of the Rx buffers */
1209 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1210 /* In the reset function, these buffers may have been allocated
1211 * to an SKB, so we need to unmap and free potential storage */
1212 if (rxq->pool[i].skb != NULL) {
1213 pci_unmap_single(priv->pci_dev,
1214 rxq->pool[i].real_dma_addr,
1215 priv->hw_params.rx_buf_size,
1216 PCI_DMA_FROMDEVICE);
1217 priv->alloc_rxb_skb--;
1218 dev_kfree_skb(rxq->pool[i].skb);
1219 rxq->pool[i].skb = NULL;
1220 }
1221 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1222 }
1223
1224 /* Set us so that we have processed and used all buffers, but have
1225 * not restocked the Rx queue with fresh buffers */
1226 rxq->read = rxq->write = 0;
1227 rxq->free_count = 0;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001228 rxq->write_actual = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001229 spin_unlock_irqrestore(&rxq->lock, flags);
1230}
Reinette Chatredf833b12009-04-21 10:55:48 -07001231
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001232void iwl3945_rx_replenish(void *data)
1233{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001234 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001235 unsigned long flags;
1236
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001237 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001238
1239 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001240 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001241 spin_unlock_irqrestore(&priv->lock, flags);
1242}
1243
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001244static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1245{
1246 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1247
1248 iwl3945_rx_queue_restock(priv);
1249}
1250
1251
Reinette Chatredf833b12009-04-21 10:55:48 -07001252/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1253 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1254 * This free routine walks the list of POOL entries and if SKB is set to
1255 * non NULL it is unmapped and freed
1256 */
1257static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1258{
1259 int i;
1260 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
1261 if (rxq->pool[i].skb != NULL) {
1262 pci_unmap_single(priv->pci_dev,
1263 rxq->pool[i].real_dma_addr,
1264 priv->hw_params.rx_buf_size,
1265 PCI_DMA_FROMDEVICE);
1266 dev_kfree_skb(rxq->pool[i].skb);
1267 }
1268 }
1269
1270 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
1271 rxq->dma_addr);
1272 pci_free_consistent(priv->pci_dev, sizeof(struct iwl_rb_status),
1273 rxq->rb_stts, rxq->rb_stts_dma);
1274 rxq->bd = NULL;
1275 rxq->rb_stts = NULL;
1276}
Reinette Chatredf833b12009-04-21 10:55:48 -07001277
1278
Zhu Yib481de92007-09-25 17:54:57 -07001279/* Convert linear signal-to-noise ratio into dB */
1280static u8 ratio2dB[100] = {
1281/* 0 1 2 3 4 5 6 7 8 9 */
1282 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1283 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1284 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1285 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1286 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1287 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1288 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1289 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1290 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1291 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1292};
1293
1294/* Calculates a relative dB value from a ratio of linear
1295 * (i.e. not dB) signal levels.
1296 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001297int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001298{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001299 /* 1000:1 or higher just report as 60 dB */
1300 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001301 return 60;
1302
Adrian Bunk221c80c2008-02-02 23:19:01 +02001303 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001304 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001305 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001306 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001307
1308 /* We shouldn't see this */
1309 if (sig_ratio < 1)
1310 return 0;
1311
1312 /* Use table for ratios 1:1 - 99:1 */
1313 return (int)ratio2dB[sig_ratio];
1314}
1315
1316#define PERFECT_RSSI (-20) /* dBm */
1317#define WORST_RSSI (-95) /* dBm */
1318#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1319
1320/* Calculate an indication of rx signal quality (a percentage, not dBm!).
1321 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1322 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001323int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07001324{
1325 int sig_qual;
1326 int degradation = PERFECT_RSSI - rssi_dbm;
1327
1328 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1329 * as indicator; formula is (signal dbm - noise dbm).
1330 * SNR at or above 40 is a great signal (100%).
1331 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1332 * Weakest usable signal is usually 10 - 15 dB SNR. */
1333 if (noise_dbm) {
1334 if (rssi_dbm - noise_dbm >= 40)
1335 return 100;
1336 else if (rssi_dbm < noise_dbm)
1337 return 0;
1338 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1339
1340 /* Else use just the signal level.
1341 * This formula is a least squares fit of data points collected and
1342 * compared with a reference system that had a percentage (%) display
1343 * for signal quality. */
1344 } else
1345 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1346 (15 * RSSI_RANGE + 62 * degradation)) /
1347 (RSSI_RANGE * RSSI_RANGE);
1348
1349 if (sig_qual > 100)
1350 sig_qual = 100;
1351 else if (sig_qual < 1)
1352 sig_qual = 0;
1353
1354 return sig_qual;
1355}
1356
1357/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001358 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001359 *
1360 * Uses the priv->rx_handlers callback function array to invoke
1361 * the appropriate handlers, including command responses,
1362 * frame-received notifications, and other notifications.
1363 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001364static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001365{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001366 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001367 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001368 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001369 u32 r, i;
1370 int reclaim;
1371 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001372 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001373 u32 count = 8;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001374 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001375
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001376 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1377 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001378 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001379 i = rxq->read;
1380
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001381 /* calculate total frames need to be restock after handling RX */
1382 total_empty = r - priv->rxq.write_actual;
1383 if (total_empty < 0)
1384 total_empty += RX_QUEUE_SIZE;
1385
1386 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001387 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001388 /* Rx interrupt, but nothing sent from uCode */
1389 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001390 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001391
1392 while (i != r) {
1393 rxb = rxq->queue[i];
1394
Ben Cahill9fbab512007-11-29 11:09:47 +08001395 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001396 * then a bug has been introduced in the queue refilling
1397 * routines -- catch it here */
1398 BUG_ON(rxb == NULL);
1399
1400 rxq->queue[i] = NULL;
1401
Reinette Chatredf833b12009-04-21 10:55:48 -07001402 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
1403 priv->hw_params.rx_buf_size,
1404 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001405 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001406
1407 /* Reclaim a command buffer only if this packet is a response
1408 * to a (driver-originated) command.
1409 * If the packet (e.g. Rx frame) originated from uCode,
1410 * there is no command buffer to reclaim.
1411 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1412 * but apparently a few don't get set; catch them here. */
1413 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1414 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1415 (pkt->hdr.cmd != REPLY_TX);
1416
1417 /* Based on type of command response or notification,
1418 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001419 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001420 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001421 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001422 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1423 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001424 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yib481de92007-09-25 17:54:57 -07001425 } else {
1426 /* No handling needed */
Reinette Chatreaf472a952009-08-28 09:58:50 -07001427 IWL_DEBUG_RX(priv, "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001428 r, i, get_cmd_string(pkt->hdr.cmd),
1429 pkt->hdr.cmd);
1430 }
1431
1432 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001433 /* Invoke any callbacks, transfer the skb to caller, and
Samuel Ortiz518099a2009-01-19 15:30:27 -08001434 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001435 * as we reclaim the driver command queue */
1436 if (rxb && rxb->skb)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001437 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001438 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001439 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001440 }
1441
1442 /* For now we just don't re-use anything. We can tweak this
1443 * later to try and re-use notification packets and SKBs that
1444 * fail to Rx correctly */
1445 if (rxb->skb != NULL) {
1446 priv->alloc_rxb_skb--;
1447 dev_kfree_skb_any(rxb->skb);
1448 rxb->skb = NULL;
1449 }
1450
Zhu Yib481de92007-09-25 17:54:57 -07001451 spin_lock_irqsave(&rxq->lock, flags);
1452 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1453 spin_unlock_irqrestore(&rxq->lock, flags);
1454 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001455 /* If there are a lot of unused frames,
1456 * restock the Rx queue so ucode won't assert. */
1457 if (fill_rx) {
1458 count++;
1459 if (count >= 8) {
1460 priv->rxq.read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001461 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001462 count = 0;
1463 }
1464 }
Zhu Yib481de92007-09-25 17:54:57 -07001465 }
1466
1467 /* Backtrack one entry */
1468 priv->rxq.read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001469 if (fill_rx)
1470 iwl3945_rx_replenish_now(priv);
1471 else
1472 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001473}
1474
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001475/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001476static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001477{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001478 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001479 synchronize_irq(priv->pci_dev->irq);
1480 tasklet_kill(&priv->irq_tasklet);
1481}
1482
Reinette Chatreb7a79402009-09-25 14:24:23 -07001483#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001484static const char *desc_lookup(int i)
1485{
1486 switch (i) {
1487 case 1:
1488 return "FAIL";
1489 case 2:
1490 return "BAD_PARAM";
1491 case 3:
1492 return "BAD_CHECKSUM";
1493 case 4:
1494 return "NMI_INTERRUPT";
1495 case 5:
1496 return "SYSASSERT";
1497 case 6:
1498 return "FATAL_ERROR";
1499 }
1500
1501 return "UNKNOWN";
1502}
1503
1504#define ERROR_START_OFFSET (1 * sizeof(u32))
1505#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1506
Reinette Chatreb7a79402009-09-25 14:24:23 -07001507void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001508{
1509 u32 i;
1510 u32 desc, time, count, base, data1;
1511 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001512
1513 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001516 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001517 return;
1518 }
1519
Zhu Yib481de92007-09-25 17:54:57 -07001520
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001521 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001522
1523 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001524 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1525 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1526 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001527 }
1528
Winkler, Tomas15b16872008-12-19 10:37:33 +08001529 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001530 "ilink1 nmiPC Line\n");
1531 for (i = ERROR_START_OFFSET;
1532 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1533 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001534 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001535 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001536 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001537 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001538 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001539 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001540 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001541 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001542 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001543 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001544 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001545 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001546 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001547
Winkler, Tomas15b16872008-12-19 10:37:33 +08001548 IWL_ERR(priv,
1549 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1550 desc_lookup(desc), desc, time, blink1, blink2,
1551 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07001552 }
1553
Zhu Yib481de92007-09-25 17:54:57 -07001554}
1555
Ben Cahillf58177b2007-11-29 11:09:43 +08001556#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001557
1558/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001559 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001560 *
Zhu Yib481de92007-09-25 17:54:57 -07001561 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001562static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07001563 u32 num_events, u32 mode)
1564{
1565 u32 i;
1566 u32 base; /* SRAM byte address of event log header */
1567 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1568 u32 ptr; /* SRAM byte address of log data */
1569 u32 ev, time, data; /* event log data */
1570
1571 if (num_events == 0)
1572 return;
1573
1574 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1575
1576 if (mode == 0)
1577 event_size = 2 * sizeof(u32);
1578 else
1579 event_size = 3 * sizeof(u32);
1580
1581 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1582
1583 /* "time" is actually "data" for mode 0 (no timestamp).
1584 * place event id # at far right for easier visual parsing. */
1585 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001586 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001587 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001588 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001589 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001590 if (mode == 0) {
1591 /* data, ev */
1592 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1593 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001594 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07001595 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001596 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07001597 }
1598 }
1599}
1600
Reinette Chatreb7a79402009-09-25 14:24:23 -07001601void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001602{
Zhu Yib481de92007-09-25 17:54:57 -07001603 u32 base; /* SRAM byte address of event log header */
1604 u32 capacity; /* event log capacity in # entries */
1605 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1606 u32 num_wraps; /* # times uCode wrapped to top of log */
1607 u32 next_entry; /* index of next entry to be written by uCode */
1608 u32 size; /* # entries that we'll print */
1609
1610 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001611 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001612 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001613 return;
1614 }
1615
Zhu Yib481de92007-09-25 17:54:57 -07001616 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001617 capacity = iwl_read_targ_mem(priv, base);
1618 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1619 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1620 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001621
1622 size = num_wraps ? capacity : next_entry;
1623
1624 /* bail out if nothing in log */
1625 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001626 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Zhu Yib481de92007-09-25 17:54:57 -07001627 return;
1628 }
1629
Winkler, Tomas15b16872008-12-19 10:37:33 +08001630 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07001631 size, num_wraps);
1632
1633 /* if uCode has wrapped back to top of log, start at the oldest entry,
1634 * i.e the next one that uCode would fill. */
1635 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001636 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07001637 capacity - next_entry, mode);
1638
1639 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001640 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07001641
Zhu Yib481de92007-09-25 17:54:57 -07001642}
Reinette Chatreb7a79402009-09-25 14:24:23 -07001643#else
1644void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
1645{
1646}
1647
1648void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
1649{
1650}
1651
1652#endif
Zhu Yib481de92007-09-25 17:54:57 -07001653
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001654static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001655{
1656 u32 inta, handled = 0;
1657 u32 inta_fh;
1658 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001659#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001660 u32 inta_mask;
1661#endif
1662
1663 spin_lock_irqsave(&priv->lock, flags);
1664
1665 /* Ack/clear/reset pending uCode interrupts.
1666 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1667 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001668 inta = iwl_read32(priv, CSR_INT);
1669 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001670
1671 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1672 * Any new interrupts that happen after this, either while we're
1673 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001674 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1675 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001676
Samuel Ortizd08853a2009-01-23 13:45:17 -08001677#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001678 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001679 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001680 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001681 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001682 inta, inta_mask, inta_fh);
1683 }
1684#endif
1685
1686 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1687 * atomic, make sure that inta covers all the interrupts that
1688 * we've discovered, even if FH interrupt came in just after
1689 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001690 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001691 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001692 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001693 inta |= CSR_INT_BIT_FH_TX;
1694
1695 /* Now service all interrupt bits discovered above. */
1696 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001697 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001698
1699 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001700 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001701
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001702 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001703 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001704
1705 handled |= CSR_INT_BIT_HW_ERR;
1706
1707 spin_unlock_irqrestore(&priv->lock, flags);
1708
1709 return;
1710 }
1711
Samuel Ortizd08853a2009-01-23 13:45:17 -08001712#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001713 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001714 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001715 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001716 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001717 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001718 priv->isr_stats.sch++;
1719 }
Zhu Yib481de92007-09-25 17:54:57 -07001720
1721 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001722 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001723 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001724 priv->isr_stats.alive++;
1725 }
Zhu Yib481de92007-09-25 17:54:57 -07001726 }
1727#endif
1728 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001729 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001730
Zhu Yib481de92007-09-25 17:54:57 -07001731 /* Error detected by uCode */
1732 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001733 IWL_ERR(priv, "Microcode SW error detected. "
1734 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001735 priv->isr_stats.sw++;
1736 priv->isr_stats.sw_err = inta;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001737 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001738 handled |= CSR_INT_BIT_SW_ERR;
1739 }
1740
1741 /* uCode wakes up after power-down sleep */
1742 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001743 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001744 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001745 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1746 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1747 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1748 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1749 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1750 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001751
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001752 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001753 handled |= CSR_INT_BIT_WAKEUP;
1754 }
1755
1756 /* All uCode command responses, including Tx command responses,
1757 * Rx "responses" (frame-received notification), and other
1758 * notifications from uCode come through here*/
1759 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001760 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001761 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001762 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1763 }
1764
1765 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001766 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001767 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001768
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001769 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001770 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1771 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001772 handled |= CSR_INT_BIT_FH_TX;
1773 }
1774
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001775 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001776 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001777 priv->isr_stats.unhandled++;
1778 }
Zhu Yib481de92007-09-25 17:54:57 -07001779
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001780 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001781 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001782 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001783 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001784 }
1785
1786 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001787 /* only Re-enable if disabled by irq */
1788 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001789 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001790
Samuel Ortizd08853a2009-01-23 13:45:17 -08001791#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001792 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001793 inta = iwl_read32(priv, CSR_INT);
1794 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1795 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001796 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001797 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1798 }
1799#endif
1800 spin_unlock_irqrestore(&priv->lock, flags);
1801}
1802
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001803static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001804 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001805 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001806 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07001807{
Johannes Berg4e05c232009-06-19 13:52:44 -07001808 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001809 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001810 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001811 u16 passive_dwell = 0;
1812 u16 active_dwell = 0;
1813 int added, i;
1814
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001815 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001816 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001817 return 0;
1818
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001819 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
1820 passive_dwell = iwl_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07001821
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001822 if (passive_dwell <= active_dwell)
1823 passive_dwell = active_dwell + 1;
1824
Johannes Berg4e05c232009-06-19 13:52:44 -07001825 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1826 chan = priv->scan_request->channels[i];
1827
1828 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001829 continue;
1830
Johannes Berg4e05c232009-06-19 13:52:44 -07001831 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001832
Samuel Ortize6148912009-01-23 13:45:15 -08001833 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001834 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001835 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001836 scan_ch->channel);
1837 continue;
1838 }
1839
Zhu Yib481de92007-09-25 17:54:57 -07001840 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1841 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001842 /* If passive , set up for auto-switch
1843 * and use long active_dwell time.
1844 */
1845 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001846 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001847 scan_ch->type = 0; /* passive */
1848 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1849 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1850 } else {
1851 scan_ch->type = 1; /* active */
1852 }
1853
1854 /* Set direct probe bits. These may be used both for active
1855 * scan channels (probes gets sent right away),
1856 * or for passive channels (probes get se sent only after
1857 * hearing clear Rx packet).*/
1858 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1859 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001860 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001861 } else {
1862 /* uCode v1 does not allow setting direct probe bits on
1863 * passive channel. */
1864 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001865 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001866 }
Zhu Yib481de92007-09-25 17:54:57 -07001867
Ben Cahill9fbab512007-11-29 11:09:47 +08001868 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001869 scan_ch->tpc.dsp_atten = 110;
1870 /* scan_pwr_info->tpc.dsp_atten; */
1871
1872 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001873 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001874 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1875 else {
1876 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1877 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001878 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001879 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001880 */
1881 }
1882
Tomas Winklere1623442009-01-27 14:27:56 -08001883 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001884 scan_ch->channel,
1885 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1886 (scan_ch->type & 1) ?
1887 active_dwell : passive_dwell);
1888
1889 scan_ch++;
1890 added++;
1891 }
1892
Tomas Winklere1623442009-01-27 14:27:56 -08001893 IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001894 return added;
1895}
1896
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001897static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001898 struct ieee80211_rate *rates)
1899{
1900 int i;
1901
1902 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001903 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1904 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1905 rates[i].hw_value_short = i;
1906 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001907 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001908 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001909 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001910 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001911 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001912 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001913 }
Zhu Yib481de92007-09-25 17:54:57 -07001914 }
1915}
1916
Zhu Yib481de92007-09-25 17:54:57 -07001917/******************************************************************************
1918 *
1919 * uCode download functions
1920 *
1921 ******************************************************************************/
1922
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001923static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001924{
Tomas Winkler98c92212008-01-14 17:46:20 -08001925 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1926 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1927 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1928 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1929 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1930 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001931}
1932
1933/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001934 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001935 * looking at all data.
1936 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001937static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001938{
1939 u32 val;
1940 u32 save_len = len;
1941 int rc = 0;
1942 u32 errcnt;
1943
Tomas Winklere1623442009-01-27 14:27:56 -08001944 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001945
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001946 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001947 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07001948
1949 errcnt = 0;
1950 for (; len > 0; len -= sizeof(u32), image++) {
1951 /* read data comes through single port, auto-incr addr */
1952 /* NOTE: Use the debugless read so we don't flood kernel log
1953 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001954 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001955 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001956 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001957 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1958 save_len - len, val, le32_to_cpu(*image));
1959 rc = -EIO;
1960 errcnt++;
1961 if (errcnt >= 20)
1962 break;
1963 }
1964 }
1965
Zhu Yib481de92007-09-25 17:54:57 -07001966
1967 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08001968 IWL_DEBUG_INFO(priv,
1969 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07001970
1971 return rc;
1972}
1973
1974
1975/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001976 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001977 * using sample data 100 bytes apart. If these sample points are good,
1978 * it's a pretty good bet that everything between them is good, too.
1979 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001980static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001981{
1982 u32 val;
1983 int rc = 0;
1984 u32 errcnt = 0;
1985 u32 i;
1986
Tomas Winklere1623442009-01-27 14:27:56 -08001987 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001988
Zhu Yib481de92007-09-25 17:54:57 -07001989 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
1990 /* read data comes through single port, auto-incr addr */
1991 /* NOTE: Use the debugless read so we don't flood kernel log
1992 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001993 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001994 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001995 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001996 if (val != le32_to_cpu(*image)) {
1997#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08001998 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001999 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2000 i, val, *image);
2001#endif
2002 rc = -EIO;
2003 errcnt++;
2004 if (errcnt >= 3)
2005 break;
2006 }
2007 }
2008
Zhu Yib481de92007-09-25 17:54:57 -07002009 return rc;
2010}
2011
2012
2013/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002014 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002015 * and verify its contents
2016 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002017static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002018{
2019 __le32 *image;
2020 u32 len;
2021 int rc = 0;
2022
2023 /* Try bootstrap */
2024 image = (__le32 *)priv->ucode_boot.v_addr;
2025 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002026 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002027 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002028 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002029 return 0;
2030 }
2031
2032 /* Try initialize */
2033 image = (__le32 *)priv->ucode_init.v_addr;
2034 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002035 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002036 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002037 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002038 return 0;
2039 }
2040
2041 /* Try runtime/protocol */
2042 image = (__le32 *)priv->ucode_code.v_addr;
2043 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002044 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002045 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002046 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002047 return 0;
2048 }
2049
Winkler, Tomas15b16872008-12-19 10:37:33 +08002050 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002051
Ben Cahill9fbab512007-11-29 11:09:47 +08002052 /* Since nothing seems to match, show first several data entries in
2053 * instruction SRAM, so maybe visual inspection will give a clue.
2054 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002055 image = (__le32 *)priv->ucode_boot.v_addr;
2056 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002057 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002058
2059 return rc;
2060}
2061
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002062static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002063{
2064 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002065 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002066}
2067
2068/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002069 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002070 *
2071 * Copy into buffers for card to fetch via bus-mastering
2072 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002073static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002074{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002075 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002076 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002077 const struct firmware *ucode_raw;
2078 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002079 const char *name_pre = priv->cfg->fw_name_pre;
2080 const unsigned int api_max = priv->cfg->ucode_api_max;
2081 const unsigned int api_min = priv->cfg->ucode_api_min;
2082 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002083 u8 *src;
2084 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002085 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002086
2087 /* Ask kernel firmware_class module to get the boot firmware off disk.
2088 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002089 for (index = api_max; index >= api_min; index--) {
2090 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2091 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2092 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002093 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002094 buf, ret);
2095 if (ret == -ENOENT)
2096 continue;
2097 else
2098 goto error;
2099 } else {
2100 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002101 IWL_ERR(priv, "Loaded firmware %s, "
2102 "which is deprecated. "
2103 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002104 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002105 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2106 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002107 buf, ucode_raw->size);
2108 break;
2109 }
Zhu Yib481de92007-09-25 17:54:57 -07002110 }
2111
Reinette Chatrea0987a82008-12-02 12:14:06 -08002112 if (ret < 0)
2113 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002114
2115 /* Make sure that we got at least our header! */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002116 if (ucode_raw->size < priv->cfg->ops->ucode->get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002117 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002118 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002119 goto err_release;
2120 }
2121
2122 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002123 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002124
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002125 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002126 api_ver = IWL_UCODE_API(priv->ucode_ver);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002127 inst_size = priv->cfg->ops->ucode->get_inst_size(ucode, api_ver);
2128 data_size = priv->cfg->ops->ucode->get_data_size(ucode, api_ver);
2129 init_size = priv->cfg->ops->ucode->get_init_size(ucode, api_ver);
2130 init_data_size =
2131 priv->cfg->ops->ucode->get_init_data_size(ucode, api_ver);
2132 boot_size = priv->cfg->ops->ucode->get_boot_size(ucode, api_ver);
2133 src = priv->cfg->ops->ucode->get_data(ucode, api_ver);
Zhu Yib481de92007-09-25 17:54:57 -07002134
Reinette Chatrea0987a82008-12-02 12:14:06 -08002135 /* api_ver should match the api version forming part of the
2136 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002137 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002138
2139 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002140 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002141 "Driver supports v%u, firmware is v%u.\n",
2142 api_max, api_ver);
2143 priv->ucode_ver = 0;
2144 ret = -EINVAL;
2145 goto err_release;
2146 }
2147 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002148 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002149 "got %u. New firmware can be obtained "
2150 "from http://www.intellinuxwireless.org.\n",
2151 api_max, api_ver);
2152
Tomas Winkler978785a2008-12-19 10:37:31 +08002153 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2154 IWL_UCODE_MAJOR(priv->ucode_ver),
2155 IWL_UCODE_MINOR(priv->ucode_ver),
2156 IWL_UCODE_API(priv->ucode_ver),
2157 IWL_UCODE_SERIAL(priv->ucode_ver));
2158
Tomas Winklere1623442009-01-27 14:27:56 -08002159 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002160 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002161 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2162 inst_size);
2163 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2164 data_size);
2165 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2166 init_size);
2167 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2168 init_data_size);
2169 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2170 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002171
Reinette Chatrea0987a82008-12-02 12:14:06 -08002172
Zhu Yib481de92007-09-25 17:54:57 -07002173 /* Verify size of file vs. image size info in file's header */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002174 if (ucode_raw->size != priv->cfg->ops->ucode->get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002175 inst_size + data_size + init_size +
2176 init_data_size + boot_size) {
2177
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002178 IWL_DEBUG_INFO(priv,
2179 "uCode file size %zd does not match expected size\n",
2180 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002181 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002182 goto err_release;
2183 }
2184
2185 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002186 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002187 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002188 inst_size);
2189 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002190 goto err_release;
2191 }
2192
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002193 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002194 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002195 data_size);
2196 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002197 goto err_release;
2198 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002199 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002200 IWL_DEBUG_INFO(priv,
2201 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002202 init_size);
2203 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002204 goto err_release;
2205 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002206 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002207 IWL_DEBUG_INFO(priv,
2208 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002209 init_data_size);
2210 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002211 goto err_release;
2212 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002213 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002214 IWL_DEBUG_INFO(priv,
2215 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002216 boot_size);
2217 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002218 goto err_release;
2219 }
2220
2221 /* Allocate ucode buffers for card's bus-master loading ... */
2222
2223 /* Runtime instructions and 2 copies of data:
2224 * 1) unmodified from disk
2225 * 2) backup cache for save/restore during power-downs */
2226 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002227 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002228
2229 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002230 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002231
2232 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002233 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002234
2235 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002236 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002237 goto err_pci_alloc;
2238
Tomas Winkler90e759d2007-11-29 11:09:41 +08002239 /* Initialization instructions and data */
2240 if (init_size && init_data_size) {
2241 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002242 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002243
2244 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002245 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002246
2247 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2248 goto err_pci_alloc;
2249 }
2250
2251 /* Bootstrap (instructions only, no data) */
2252 if (boot_size) {
2253 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002254 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002255
2256 if (!priv->ucode_boot.v_addr)
2257 goto err_pci_alloc;
2258 }
2259
Zhu Yib481de92007-09-25 17:54:57 -07002260 /* Copy images into buffers for card's bus-master reads ... */
2261
2262 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002263 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002264 IWL_DEBUG_INFO(priv,
2265 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002266 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002267 src += len;
2268
Tomas Winklere1623442009-01-27 14:27:56 -08002269 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002270 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2271
2272 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002273 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002274 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002275 IWL_DEBUG_INFO(priv,
2276 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002277 memcpy(priv->ucode_data.v_addr, src, len);
2278 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002279 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002280
2281 /* Initialization instructions (3rd block) */
2282 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002283 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002284 IWL_DEBUG_INFO(priv,
2285 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002286 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002287 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002288 }
2289
2290 /* Initialization data (4th block) */
2291 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002292 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002293 IWL_DEBUG_INFO(priv,
2294 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002295 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002296 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002297 }
2298
2299 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002300 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002301 IWL_DEBUG_INFO(priv,
2302 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002303 memcpy(priv->ucode_boot.v_addr, src, len);
2304
2305 /* We have our copies now, allow OS release its copies */
2306 release_firmware(ucode_raw);
2307 return 0;
2308
2309 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002310 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002311 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002312 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002313
2314 err_release:
2315 release_firmware(ucode_raw);
2316
2317 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002318 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002319}
2320
2321
2322/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002323 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002324 *
2325 * Tell initialization uCode where to find runtime uCode.
2326 *
2327 * BSM registers initially contain pointers to initialization uCode.
2328 * We need to replace them to load runtime uCode inst and data,
2329 * and to save runtime data when powering down.
2330 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002331static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002332{
2333 dma_addr_t pinst;
2334 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002335
2336 /* bits 31:0 for 3945 */
2337 pinst = priv->ucode_code.p_addr;
2338 pdata = priv->ucode_data_backup.p_addr;
2339
Zhu Yib481de92007-09-25 17:54:57 -07002340 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002341 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2342 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2343 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002344 priv->ucode_data.len);
2345
Tomas Winklera96a27f2008-10-23 23:48:56 -07002346 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002347 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002348 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002349 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2350
Tomas Winklere1623442009-01-27 14:27:56 -08002351 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002352
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002353 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002354}
2355
2356/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002357 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002358 *
2359 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2360 *
Zhu Yib481de92007-09-25 17:54:57 -07002361 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002362 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002363static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002364{
2365 /* Check alive response for "valid" sign from uCode */
2366 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2367 /* We had an error bringing up the hardware, so take it
2368 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002369 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002370 goto restart;
2371 }
2372
2373 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2374 * This is a paranoid check, because we would not have gotten the
2375 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002376 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002377 /* Runtime instruction load was bad;
2378 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002379 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002380 goto restart;
2381 }
2382
2383 /* Send pointers to protocol/runtime uCode image ... init code will
2384 * load and launch runtime uCode, which will send us another "Alive"
2385 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002386 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002387 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002388 /* Runtime instruction load won't happen;
2389 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002390 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002391 goto restart;
2392 }
2393 return;
2394
2395 restart:
2396 queue_work(priv->workqueue, &priv->restart);
2397}
2398
Zhu Yib481de92007-09-25 17:54:57 -07002399/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002400 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002401 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002402 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002403 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002404static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002405{
Zhu Yib481de92007-09-25 17:54:57 -07002406 int thermal_spin = 0;
2407 u32 rfkill;
2408
Tomas Winklere1623442009-01-27 14:27:56 -08002409 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002410
2411 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2412 /* We had an error bringing up the hardware, so take it
2413 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002414 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002415 goto restart;
2416 }
2417
2418 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2419 * This is a paranoid check, because we would not have gotten the
2420 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002421 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002422 /* Runtime instruction load was bad;
2423 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002424 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002425 goto restart;
2426 }
2427
Tomas Winklerc587de02009-06-03 11:44:07 -07002428 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002429
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002430 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002431 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002432
2433 if (rfkill & 0x1) {
2434 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002435 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002436 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002437 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002438 thermal_spin++;
2439 udelay(10);
2440 }
2441
2442 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002443 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002444 thermal_spin * 10);
2445 } else
2446 set_bit(STATUS_RF_KILL_HW, &priv->status);
2447
Ben Cahill9fbab512007-11-29 11:09:47 +08002448 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002449 set_bit(STATUS_ALIVE, &priv->status);
2450
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002451 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002452 return;
2453
Johannes Berg36d68252008-05-15 12:55:26 +02002454 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002455
2456 priv->active_rate = priv->rates_mask;
2457 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2458
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08002459 iwl_power_update_mode(priv, false);
Zhu Yib481de92007-09-25 17:54:57 -07002460
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002461 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002462 struct iwl3945_rxon_cmd *active_rxon =
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002463 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07002464
Abhijeet Kolekar8a9b9922009-06-12 13:22:52 -07002465 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002466 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2467 } else {
2468 /* Initialize our rx_config data */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002469 iwl_connection_init_rx_config(priv, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002470 }
2471
Ben Cahill9fbab512007-11-29 11:09:47 +08002472 /* Configure Bluetooth device coexistence support */
Samuel Ortiz17f841c2009-01-23 13:45:20 -08002473 iwl_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002474
2475 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002476 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002477
Zhu Yib481de92007-09-25 17:54:57 -07002478 iwl3945_reg_txpower_periodic(priv);
2479
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002480 iwl3945_led_register(priv);
2481
Tomas Winklere1623442009-01-27 14:27:56 -08002482 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002483 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002484 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002485
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002486 /* reassociate for ADHOC mode */
2487 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2488 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2489 priv->vif);
2490 if (beacon)
Abhijeet Kolekar9944b932009-04-08 11:26:36 -07002491 iwl_mac_beacon_update(priv->hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002492 }
2493
Abhijeet Kolekarf45c2712009-04-08 11:26:38 -07002494 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
Abhijeet Kolekar727882d2009-04-08 11:26:45 -07002495 iwl_set_mode(priv, priv->iw_mode);
Abhijeet Kolekarf45c2712009-04-08 11:26:38 -07002496
Zhu Yib481de92007-09-25 17:54:57 -07002497 return;
2498
2499 restart:
2500 queue_work(priv->workqueue, &priv->restart);
2501}
2502
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002503static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002504
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002505static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002506{
2507 unsigned long flags;
2508 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2509 struct ieee80211_conf *conf = NULL;
2510
Tomas Winklere1623442009-01-27 14:27:56 -08002511 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002512
2513 conf = ieee80211_get_hw_conf(priv->hw);
2514
2515 if (!exit_pending)
2516 set_bit(STATUS_EXIT_PENDING, &priv->status);
2517
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002518 iwl3945_led_unregister(priv);
Tomas Winklerc587de02009-06-03 11:44:07 -07002519 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002520
2521 /* Unblock any waiting calls */
2522 wake_up_interruptible_all(&priv->wait_command_queue);
2523
Zhu Yib481de92007-09-25 17:54:57 -07002524 /* Wipe out the EXIT_PENDING status bit if we are not actually
2525 * exiting the module */
2526 if (!exit_pending)
2527 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2528
2529 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002530 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002531
2532 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002533 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002534 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002535 spin_unlock_irqrestore(&priv->lock, flags);
2536 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002537
2538 if (priv->mac80211_registered)
2539 ieee80211_stop_queues(priv->hw);
2540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002541 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002542 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002543 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002544 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2545 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002546 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2547 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002548 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2549 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002550 goto exit;
2551 }
2552
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002553 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002554 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002555 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2556 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002557 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2558 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002559 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002560 STATUS_FW_ERROR |
2561 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2562 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002563
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002564 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002565 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002566 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07002567 spin_unlock_irqrestore(&priv->lock, flags);
2568
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002569 iwl3945_hw_txq_ctx_stop(priv);
2570 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002571
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002572 iwl_write_prph(priv, APMG_CLK_DIS_REG,
2573 APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002574
2575 udelay(5);
2576
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002577 if (exit_pending)
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002578 priv->cfg->ops->lib->apm_ops.stop(priv);
2579 else
2580 priv->cfg->ops->lib->apm_ops.reset(priv);
2581
Zhu Yib481de92007-09-25 17:54:57 -07002582 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002583 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002584
2585 if (priv->ibss_beacon)
2586 dev_kfree_skb(priv->ibss_beacon);
2587 priv->ibss_beacon = NULL;
2588
2589 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002590 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002591}
2592
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002593static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002594{
2595 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002596 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002597 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002598
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002599 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002600}
2601
2602#define MAX_HW_RESTARTS 5
2603
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002604static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002605{
2606 int rc, i;
2607
2608 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002609 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002610 return -EIO;
2611 }
2612
Reinette Chatree903fbd2008-01-30 22:05:15 -08002613 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002614 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002615 return -EIO;
2616 }
2617
Zhu Yie655b9f2008-01-24 02:19:38 -08002618 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002619 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002620 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2621 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2622 else {
2623 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002624 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2625 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002626 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002627
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002628 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002629
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002630 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002631 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002632 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002633 return rc;
2634 }
2635
2636 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002637 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2638 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002639 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2640
2641 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002642 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002643 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002644
2645 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002646 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2647 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002648
2649 /* Copy original ucode data image from disk into backup cache.
2650 * This will be used to initialize the on-board processor's
2651 * data SRAM for a clean start when the runtime program first loads. */
2652 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002653 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002654
Zhu Yie655b9f2008-01-24 02:19:38 -08002655 /* We return success when we resume from suspend and rf_kill is on. */
2656 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2657 return 0;
2658
Zhu Yib481de92007-09-25 17:54:57 -07002659 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2660
Tomas Winklerc587de02009-06-03 11:44:07 -07002661 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002662
2663 /* load bootstrap state machine,
2664 * load bootstrap program into processor's memory,
2665 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08002666 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002667
2668 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002669 IWL_ERR(priv,
2670 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002671 continue;
2672 }
2673
2674 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002675 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002676
Tomas Winklere1623442009-01-27 14:27:56 -08002677 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002678
2679 return 0;
2680 }
2681
2682 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002683 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002684 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002685
2686 /* tried to restart and config the device for as long as our
2687 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002688 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002689 return -EIO;
2690}
2691
2692
2693/*****************************************************************************
2694 *
2695 * Workqueue callbacks
2696 *
2697 *****************************************************************************/
2698
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002699static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002700{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002701 struct iwl_priv *priv =
2702 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002703
2704 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2705 return;
2706
2707 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002708 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002709 mutex_unlock(&priv->mutex);
2710}
2711
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002712static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002713{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002714 struct iwl_priv *priv =
2715 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002716
2717 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2718 return;
2719
2720 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002721 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002722 mutex_unlock(&priv->mutex);
2723}
2724
Helmut Schaa26635162009-01-15 09:38:44 +01002725static void iwl3945_rfkill_poll(struct work_struct *data)
2726{
2727 struct iwl_priv *priv =
2728 container_of(data, struct iwl_priv, rfkill_poll.work);
Helmut Schaa26635162009-01-15 09:38:44 +01002729
2730 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2731 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2732 else
2733 set_bit(STATUS_RF_KILL_HW, &priv->status);
2734
Johannes Berga60e77e2009-06-04 18:26:06 +02002735 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
2736 test_bit(STATUS_RF_KILL_HW, &priv->status));
Helmut Schaa26635162009-01-15 09:38:44 +01002737
2738 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
2739 round_jiffies_relative(2 * HZ));
2740
2741}
2742
Zhu Yib481de92007-09-25 17:54:57 -07002743#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002744static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002745{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002746 struct iwl_priv *priv =
2747 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002748 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002749 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002750 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002751 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002752 };
2753 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002754 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07002755 struct ieee80211_conf *conf = NULL;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002756 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002757 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002758 bool is_active = false;
Zhu Yib481de92007-09-25 17:54:57 -07002759
2760 conf = ieee80211_get_hw_conf(priv->hw);
2761
2762 mutex_lock(&priv->mutex);
2763
Reinette Chatrefbc9f972009-05-15 16:13:46 -07002764 cancel_delayed_work(&priv->scan_check);
2765
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002766 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002767 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002768 goto done;
2769 }
2770
Tomas Winklera96a27f2008-10-23 23:48:56 -07002771 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07002772 * was given the chance to run... */
2773 if (!test_bit(STATUS_SCANNING, &priv->status))
2774 goto done;
2775
2776 /* This should never be called or scheduled if there is currently
2777 * a scan active in the hardware. */
2778 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002779 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
2780 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002781 rc = -EIO;
2782 goto done;
2783 }
2784
2785 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002786 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07002787 goto done;
2788 }
2789
2790 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002791 IWL_DEBUG_HC(priv,
2792 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002793 goto done;
2794 }
2795
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002796 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002797 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07002798 goto done;
2799 }
2800
2801 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002802 IWL_DEBUG_HC(priv,
2803 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002804 goto done;
2805 }
2806
2807 if (!priv->scan_bands) {
Tomas Winklere1623442009-01-27 14:27:56 -08002808 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
Zhu Yib481de92007-09-25 17:54:57 -07002809 goto done;
2810 }
2811
Winkler, Tomas805cee52009-01-19 15:30:28 -08002812 if (!priv->scan) {
2813 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07002814 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Winkler, Tomas805cee52009-01-19 15:30:28 -08002815 if (!priv->scan) {
Zhu Yib481de92007-09-25 17:54:57 -07002816 rc = -ENOMEM;
2817 goto done;
2818 }
2819 }
Winkler, Tomas805cee52009-01-19 15:30:28 -08002820 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002821 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002822
2823 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2824 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2825
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002826 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002827 u16 interval = 0;
2828 u32 extra;
2829 u32 suspend_time = 100;
2830 u32 scan_suspend_time = 100;
2831 unsigned long flags;
2832
Tomas Winklere1623442009-01-27 14:27:56 -08002833 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002834
2835 spin_lock_irqsave(&priv->lock, flags);
2836 interval = priv->beacon_int;
2837 spin_unlock_irqrestore(&priv->lock, flags);
2838
2839 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002840 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002841 if (!interval)
2842 interval = suspend_time;
2843 /*
2844 * suspend time format:
2845 * 0-19: beacon interval in usec (time before exec.)
2846 * 20-23: 0
2847 * 24-31: number of beacons (suspend between channels)
2848 */
2849
2850 extra = (suspend_time / interval) << 24;
2851 scan_suspend_time = 0xFF0FFFFF &
2852 (extra | ((suspend_time % interval) * 1024));
2853
2854 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002855 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002856 scan_suspend_time, interval);
2857 }
2858
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002859 if (priv->scan_request->n_ssids) {
2860 int i, p = 0;
2861 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2862 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2863 /* always does wildcard anyway */
2864 if (!priv->scan_request->ssids[i].ssid_len)
2865 continue;
2866 scan->direct_scan[p].id = WLAN_EID_SSID;
2867 scan->direct_scan[p].len =
2868 priv->scan_request->ssids[i].ssid_len;
2869 memcpy(scan->direct_scan[p].ssid,
2870 priv->scan_request->ssids[i].ssid,
2871 priv->scan_request->ssids[i].ssid_len);
2872 n_probes++;
2873 p++;
2874 }
2875 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002876 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002877 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002878
2879 /* We don't build a direct scan probe request; the uCode will do
2880 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002881 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002882 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002883 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2884
2885 /* flags + rate selection */
2886
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002887 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07002888 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2889 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2890 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002891 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002892 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07002893 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Bergb097ad22009-04-20 14:36:57 -07002894 /*
2895 * If active scaning is requested but a certain channel
2896 * is marked passive, we can do active scanning if we
2897 * detect transmissions.
2898 */
2899 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002900 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002901 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002902 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07002903 goto done;
2904 }
2905
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08002906 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002907 iwl_fill_probe_req(priv,
2908 (struct ieee80211_mgmt *)scan->data,
2909 priv->scan_request->ie,
2910 priv->scan_request->ie_len,
2911 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08002912
Zhu Yib481de92007-09-25 17:54:57 -07002913 /* select Rx antennas */
2914 scan->flags |= iwl3945_get_antenna_flags(priv);
2915
Wey-Yi Guy279b05d2009-04-20 14:37:00 -07002916 if (iwl_is_monitor_mode(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002917 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
2918
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002919 scan->channel_count =
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002920 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002921 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07002922
Reinette Chatre14b54332008-11-04 12:21:35 -08002923 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002924 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08002925 goto done;
2926 }
2927
Zhu Yib481de92007-09-25 17:54:57 -07002928 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002929 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07002930 cmd.data = scan;
2931 scan->len = cpu_to_le16(cmd.len);
2932
2933 set_bit(STATUS_SCAN_HW, &priv->status);
Samuel Ortiz518099a2009-01-19 15:30:27 -08002934 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002935 if (rc)
2936 goto done;
2937
2938 queue_delayed_work(priv->workqueue, &priv->scan_check,
2939 IWL_SCAN_CHECK_WATCHDOG);
2940
2941 mutex_unlock(&priv->mutex);
2942 return;
2943
2944 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08002945 /* can not perform scan make sure we clear scanning
2946 * bits from status so next scan request can be performed.
2947 * if we dont clear scanning status bit here all next scan
2948 * will fail
2949 */
2950 clear_bit(STATUS_SCAN_HW, &priv->status);
2951 clear_bit(STATUS_SCANNING, &priv->status);
2952
Ian Schram01ebd062007-10-25 17:15:22 +08002953 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07002954 queue_work(priv->workqueue, &priv->scan_completed);
2955 mutex_unlock(&priv->mutex);
2956}
2957
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002958static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002959{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002960 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07002961
2962 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2963 return;
2964
2965 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002966 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002967 mutex_unlock(&priv->mutex);
2968}
2969
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002970static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002971{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002972 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07002973
2974 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2975 return;
2976
Johannes Berg19cc1082009-05-08 13:44:36 -07002977 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
2978 mutex_lock(&priv->mutex);
2979 priv->vif = NULL;
2980 priv->is_open = 0;
2981 mutex_unlock(&priv->mutex);
2982 iwl3945_down(priv);
2983 ieee80211_restart_hw(priv->hw);
2984 } else {
2985 iwl3945_down(priv);
2986 queue_work(priv->workqueue, &priv->up);
2987 }
Zhu Yib481de92007-09-25 17:54:57 -07002988}
2989
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002990static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002991{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002992 struct iwl_priv *priv =
2993 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07002994
2995 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2996 return;
2997
2998 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002999 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003000 mutex_unlock(&priv->mutex);
3001}
3002
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003003#define IWL_DELAY_NEXT_SCAN (HZ*2)
3004
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003005void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003006{
Zhu Yib481de92007-09-25 17:54:57 -07003007 int rc = 0;
3008 struct ieee80211_conf *conf = NULL;
3009
Johannes Berg05c914f2008-09-11 00:01:58 +02003010 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003011 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003012 return;
3013 }
3014
3015
Tomas Winklere1623442009-01-27 14:27:56 -08003016 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003017 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003018
3019 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3020 return;
3021
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003022 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003023 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003024
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003025 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003026
Zhu Yib481de92007-09-25 17:54:57 -07003027 conf = ieee80211_get_hw_conf(priv->hw);
3028
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003029 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003030 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003031
Tomas Winkler28afaf92008-12-19 10:37:06 +08003032 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Tomas Winkler2c2f3b32009-06-19 13:52:45 -07003033 iwl_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003034 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003035 sizeof(priv->rxon_timing), &priv->rxon_timing);
3036 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003037 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003038 "Attempting to continue.\n");
3039
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003040 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003041
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003042 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003043
Tomas Winklere1623442009-01-27 14:27:56 -08003044 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003045 priv->assoc_id, priv->beacon_int);
3046
3047 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003048 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003049 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003050 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003051
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003052 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003053 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003054 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003055 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003056 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003057
Johannes Berg05c914f2008-09-11 00:01:58 +02003058 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003059 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003060
3061 }
3062
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003063 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003064
3065 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003066 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003067 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003068 break;
3069
Johannes Berg05c914f2008-09-11 00:01:58 +02003070 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07003071
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08003072 priv->assoc_id = 1;
Tomas Winklerc587de02009-06-03 11:44:07 -07003073 iwl_add_station(priv, priv->bssid, 0, CMD_SYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003074 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01003075 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003076 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
3077 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003078 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
3079 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003080
3081 break;
3082
3083 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08003084 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003085 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07003086 break;
3087 }
3088
Abhijeet Kolekar14d2aac2009-02-27 16:21:24 -08003089 iwl_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003090
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003091 /* we have just associated, don't start scan too early */
3092 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003093}
3094
Zhu Yib481de92007-09-25 17:54:57 -07003095/*****************************************************************************
3096 *
3097 * mac80211 entry point functions
3098 *
3099 *****************************************************************************/
3100
Zhu Yi5a669262008-01-14 17:46:18 -08003101#define UCODE_READY_TIMEOUT (2 * HZ)
3102
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003103static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003104{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003105 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08003106 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003107
Tomas Winklere1623442009-01-27 14:27:56 -08003108 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003109
3110 /* we should be verifying the device is ready to be opened */
3111 mutex_lock(&priv->mutex);
3112
Zhu Yi5a669262008-01-14 17:46:18 -08003113 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3114 * ucode filename and max sizes are card-specific. */
3115
3116 if (!priv->ucode_code.len) {
3117 ret = iwl3945_read_ucode(priv);
3118 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003119 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08003120 mutex_unlock(&priv->mutex);
3121 goto out_release_irq;
3122 }
3123 }
3124
Zhu Yie655b9f2008-01-24 02:19:38 -08003125 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08003126
Zhu Yib481de92007-09-25 17:54:57 -07003127 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08003128
Zhu Yie655b9f2008-01-24 02:19:38 -08003129 if (ret)
3130 goto out_release_irq;
3131
Tomas Winklere1623442009-01-27 14:27:56 -08003132 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003133
Zhu Yi5a669262008-01-14 17:46:18 -08003134 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3135 * mac80211 will not be run successfully. */
3136 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3137 test_bit(STATUS_READY, &priv->status),
3138 UCODE_READY_TIMEOUT);
3139 if (!ret) {
3140 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003141 IWL_ERR(priv,
3142 "Wait for START_ALIVE timeout after %dms.\n",
3143 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08003144 ret = -ETIMEDOUT;
3145 goto out_release_irq;
3146 }
3147 }
3148
Helmut Schaa26635162009-01-15 09:38:44 +01003149 /* ucode is running and will send rfkill notifications,
3150 * no need to poll the killswitch state anymore */
3151 cancel_delayed_work(&priv->rfkill_poll);
3152
Zhu Yie655b9f2008-01-24 02:19:38 -08003153 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003154 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003155 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003156
3157out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003158 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003159 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08003160 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003161}
3162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003163static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003164{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003165 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003166
Tomas Winklere1623442009-01-27 14:27:56 -08003167 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003168
Zhu Yie655b9f2008-01-24 02:19:38 -08003169 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003170 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003171 return;
3172 }
3173
Zhu Yib481de92007-09-25 17:54:57 -07003174 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003175
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003176 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003177 /* stop mac, cancel any scan request and clear
3178 * RXON_FILTER_ASSOC_MSK BIT
3179 */
Zhu Yi5a669262008-01-14 17:46:18 -08003180 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003181 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003182 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003183 }
3184
Zhu Yi5a669262008-01-14 17:46:18 -08003185 iwl3945_down(priv);
3186
3187 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003188
3189 /* start polling the killswitch state again */
3190 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3191 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003192
Tomas Winklere1623442009-01-27 14:27:56 -08003193 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003194}
3195
Johannes Berge039fa42008-05-15 12:55:29 +02003196static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003197{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003198 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003199
Tomas Winklere1623442009-01-27 14:27:56 -08003200 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003201
Tomas Winklere1623442009-01-27 14:27:56 -08003202 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003203 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003204
Johannes Berge039fa42008-05-15 12:55:29 +02003205 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003206 dev_kfree_skb_any(skb);
3207
Tomas Winklere1623442009-01-27 14:27:56 -08003208 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003209 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003210}
3211
Abhijeet Kolekar60690a62009-04-08 11:26:49 -07003212void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003213{
3214 int rc = 0;
3215
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003216 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003217 return;
3218
3219 /* The following should be done only at AP bring up */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003220 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003221
3222 /* RXON - unassoc (to set timing command) */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003223 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003224 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003225
3226 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08003227 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Tomas Winkler2c2f3b32009-06-19 13:52:45 -07003228 iwl_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003229 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3230 sizeof(priv->rxon_timing),
3231 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07003232 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003233 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003234 "Attempting to continue.\n");
3235
3236 /* FIXME: what should be the assoc_id for AP? */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003237 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003238 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003239 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003240 RXON_FLG_SHORT_PREAMBLE_MSK;
3241 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003242 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003243 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3244
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003245 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003246 if (priv->assoc_capability &
3247 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003248 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003249 RXON_FLG_SHORT_SLOT_MSK;
3250 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003251 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003252 ~RXON_FLG_SHORT_SLOT_MSK;
3253
Johannes Berg05c914f2008-09-11 00:01:58 +02003254 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003255 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003256 ~RXON_FLG_SHORT_SLOT_MSK;
3257 }
3258 /* restore RXON assoc */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003259 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003260 iwlcore_commit_rxon(priv);
Tomas Winklerc587de02009-06-03 11:44:07 -07003261 iwl_add_station(priv, iwl_bcast_addr, 0, CMD_SYNC, NULL);
Zhu Yi556f8db2007-09-27 11:27:33 +08003262 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003263 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003264
3265 /* FIXME - we need to add code here to detect a totally new
3266 * configuration, reset the AP, unassoc, rxon timing, assoc,
3267 * clear sta table, add BCAST sta... */
3268}
3269
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003270static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003271 struct ieee80211_vif *vif,
3272 struct ieee80211_sta *sta,
3273 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003274{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003275 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01003276 const u8 *addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003277 int ret = 0;
3278 u8 sta_id = IWL_INVALID_STATION;
3279 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003280
Tomas Winklere1623442009-01-27 14:27:56 -08003281 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003282
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003283 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003284 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003285 return -EOPNOTSUPP;
3286 }
3287
Winkler, Tomas42986792009-01-19 15:30:22 -08003288 addr = sta ? sta->addr : iwl_bcast_addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003289 static_key = !iwl_is_associated(priv);
3290
3291 if (!static_key) {
Tomas Winklerc587de02009-06-03 11:44:07 -07003292 sta_id = iwl_find_station(priv, addr);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003293 if (sta_id == IWL_INVALID_STATION) {
Joe Perches12514392009-04-16 16:23:26 -07003294 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003295 addr);
3296 return -EINVAL;
3297 }
Zhu Yib481de92007-09-25 17:54:57 -07003298 }
3299
3300 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003301 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003302 mutex_unlock(&priv->mutex);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003303
Zhu Yib481de92007-09-25 17:54:57 -07003304 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003305 case SET_KEY:
3306 if (static_key)
3307 ret = iwl3945_set_static_key(priv, key);
3308 else
3309 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3310 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003311 break;
3312 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003313 if (static_key)
3314 ret = iwl3945_remove_static_key(priv);
3315 else
3316 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3317 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003318 break;
3319 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003320 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003321 }
3322
Tomas Winklere1623442009-01-27 14:27:56 -08003323 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003324
Winkler, Tomas42986792009-01-19 15:30:22 -08003325 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003326}
3327
Zhu Yib481de92007-09-25 17:54:57 -07003328/*****************************************************************************
3329 *
3330 * sysfs attributes
3331 *
3332 *****************************************************************************/
3333
Samuel Ortizd08853a2009-01-23 13:45:17 -08003334#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003335
3336/*
3337 * The following adds a new attribute to the sysfs representation
3338 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3339 * used for controlling the debug level.
3340 *
3341 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003342 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003343 * The debug_level being managed using sysfs below is a per device debug
3344 * level that is used instead of the global debug level if it (the per
3345 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003346 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003347static ssize_t show_debug_level(struct device *d,
3348 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003349{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003350 struct iwl_priv *priv = dev_get_drvdata(d);
3351 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003352}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003353static ssize_t store_debug_level(struct device *d,
3354 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003355 const char *buf, size_t count)
3356{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003357 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003358 unsigned long val;
3359 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003360
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003361 ret = strict_strtoul(buf, 0, &val);
3362 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003363 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003364 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003365 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003366 if (iwl_alloc_traffic_mem(priv))
3367 IWL_ERR(priv,
3368 "Not enough memory to generate traffic log\n");
3369 }
Zhu Yib481de92007-09-25 17:54:57 -07003370 return strnlen(buf, count);
3371}
3372
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003373static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3374 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003375
Samuel Ortizd08853a2009-01-23 13:45:17 -08003376#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003377
Zhu Yib481de92007-09-25 17:54:57 -07003378static ssize_t show_temperature(struct device *d,
3379 struct device_attribute *attr, char *buf)
3380{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003381 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003382
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003383 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003384 return -EAGAIN;
3385
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003386 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003387}
3388
3389static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3390
Zhu Yib481de92007-09-25 17:54:57 -07003391static ssize_t show_tx_power(struct device *d,
3392 struct device_attribute *attr, char *buf)
3393{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003394 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003395 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003396}
3397
3398static ssize_t store_tx_power(struct device *d,
3399 struct device_attribute *attr,
3400 const char *buf, size_t count)
3401{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003402 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003403 char *p = (char *)buf;
3404 u32 val;
3405
3406 val = simple_strtoul(p, &p, 10);
3407 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003408 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003409 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003410 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003411
3412 return count;
3413}
3414
3415static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3416
3417static ssize_t show_flags(struct device *d,
3418 struct device_attribute *attr, char *buf)
3419{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003420 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003421
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003422 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003423}
3424
3425static ssize_t store_flags(struct device *d,
3426 struct device_attribute *attr,
3427 const char *buf, size_t count)
3428{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003429 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003430 u32 flags = simple_strtoul(buf, NULL, 0);
3431
3432 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003433 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003434 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003435 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003436 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003437 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003438 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003439 flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003440 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003441 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003442 }
3443 }
3444 mutex_unlock(&priv->mutex);
3445
3446 return count;
3447}
3448
3449static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3450
3451static ssize_t show_filter_flags(struct device *d,
3452 struct device_attribute *attr, char *buf)
3453{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003454 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003455
3456 return sprintf(buf, "0x%04X\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003457 le32_to_cpu(priv->active_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003458}
3459
3460static ssize_t store_filter_flags(struct device *d,
3461 struct device_attribute *attr,
3462 const char *buf, size_t count)
3463{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003464 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003465 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3466
3467 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003468 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003469 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003470 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003471 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003472 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003473 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003474 "0x%04X\n", filter_flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003475 priv->staging_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003476 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003477 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003478 }
3479 }
3480 mutex_unlock(&priv->mutex);
3481
3482 return count;
3483}
3484
3485static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3486 store_filter_flags);
3487
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003488#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003489
3490static ssize_t show_measurement(struct device *d,
3491 struct device_attribute *attr, char *buf)
3492{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003493 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003494 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003495 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003496 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003497 unsigned long flags;
3498
3499 spin_lock_irqsave(&priv->lock, flags);
3500 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3501 spin_unlock_irqrestore(&priv->lock, flags);
3502 return 0;
3503 }
3504 memcpy(&measure_report, &priv->measure_report, size);
3505 priv->measurement_status = 0;
3506 spin_unlock_irqrestore(&priv->lock, flags);
3507
3508 while (size && (PAGE_SIZE - len)) {
3509 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3510 PAGE_SIZE - len, 1);
3511 len = strlen(buf);
3512 if (PAGE_SIZE - len)
3513 buf[len++] = '\n';
3514
3515 ofs += 16;
3516 size -= min(size, 16U);
3517 }
3518
3519 return len;
3520}
3521
3522static ssize_t store_measurement(struct device *d,
3523 struct device_attribute *attr,
3524 const char *buf, size_t count)
3525{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003526 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003527 struct ieee80211_measurement_params params = {
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003528 .channel = le16_to_cpu(priv->active_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07003529 .start_time = cpu_to_le64(priv->last_tsf),
3530 .duration = cpu_to_le16(1),
3531 };
3532 u8 type = IWL_MEASURE_BASIC;
3533 u8 buffer[32];
3534 u8 channel;
3535
3536 if (count) {
3537 char *p = buffer;
3538 strncpy(buffer, buf, min(sizeof(buffer), count));
3539 channel = simple_strtoul(p, NULL, 0);
3540 if (channel)
3541 params.channel = channel;
3542
3543 p = buffer;
3544 while (*p && *p != ' ')
3545 p++;
3546 if (*p)
3547 type = simple_strtoul(p + 1, NULL, 0);
3548 }
3549
Tomas Winklere1623442009-01-27 14:27:56 -08003550 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003551 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003552 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003553
3554 return count;
3555}
3556
3557static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3558 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003559#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07003560
Zhu Yib481de92007-09-25 17:54:57 -07003561static ssize_t store_retry_rate(struct device *d,
3562 struct device_attribute *attr,
3563 const char *buf, size_t count)
3564{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003565 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003566
3567 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3568 if (priv->retry_rate <= 0)
3569 priv->retry_rate = 1;
3570
3571 return count;
3572}
3573
3574static ssize_t show_retry_rate(struct device *d,
3575 struct device_attribute *attr, char *buf)
3576{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003577 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003578 return sprintf(buf, "%d", priv->retry_rate);
3579}
3580
3581static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3582 store_retry_rate);
3583
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003584
Zhu Yib481de92007-09-25 17:54:57 -07003585static ssize_t show_channels(struct device *d,
3586 struct device_attribute *attr, char *buf)
3587{
Johannes Berg8318d782008-01-24 19:38:38 +01003588 /* all this shit doesn't belong into sysfs anyway */
3589 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003590}
3591
3592static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3593
3594static ssize_t show_statistics(struct device *d,
3595 struct device_attribute *attr, char *buf)
3596{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003597 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003598 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07003599 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003600 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07003601 int rc = 0;
3602
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003603 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003604 return -EAGAIN;
3605
3606 mutex_lock(&priv->mutex);
Samuel Ortiz17f841c2009-01-23 13:45:20 -08003607 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003608 mutex_unlock(&priv->mutex);
3609
3610 if (rc) {
3611 len = sprintf(buf,
3612 "Error sending statistics request: 0x%08X\n", rc);
3613 return len;
3614 }
3615
3616 while (size && (PAGE_SIZE - len)) {
3617 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3618 PAGE_SIZE - len, 1);
3619 len = strlen(buf);
3620 if (PAGE_SIZE - len)
3621 buf[len++] = '\n';
3622
3623 ofs += 16;
3624 size -= min(size, 16U);
3625 }
3626
3627 return len;
3628}
3629
3630static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
3631
3632static ssize_t show_antenna(struct device *d,
3633 struct device_attribute *attr, char *buf)
3634{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003635 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003636
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003637 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003638 return -EAGAIN;
3639
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003640 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003641}
3642
3643static ssize_t store_antenna(struct device *d,
3644 struct device_attribute *attr,
3645 const char *buf, size_t count)
3646{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003647 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003648 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003649
3650 if (count == 0)
3651 return 0;
3652
3653 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003654 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003655 return count;
3656 }
3657
3658 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003659 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003660 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003661 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003662 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003663
3664
3665 return count;
3666}
3667
3668static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3669
3670static ssize_t show_status(struct device *d,
3671 struct device_attribute *attr, char *buf)
3672{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003673 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003674 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003675 return -EAGAIN;
3676 return sprintf(buf, "0x%08x\n", (int)priv->status);
3677}
3678
3679static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3680
3681static ssize_t dump_error_log(struct device *d,
3682 struct device_attribute *attr,
3683 const char *buf, size_t count)
3684{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003685 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003686 char *p = (char *)buf;
3687
3688 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003689 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003690
3691 return strnlen(buf, count);
3692}
3693
3694static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3695
Zhu Yib481de92007-09-25 17:54:57 -07003696/*****************************************************************************
3697 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003698 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003699 *
3700 *****************************************************************************/
3701
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003702static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003703{
Reinette Chatred21050c2009-02-13 11:51:18 -08003704 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003705
3706 init_waitqueue_head(&priv->wait_command_queue);
3707
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003708 INIT_WORK(&priv->up, iwl3945_bg_up);
3709 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3710 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003711 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003712 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3713 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Helmut Schaa26635162009-01-15 09:38:44 +01003714 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003715 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
3716 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
3717 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
3718 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07003719
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003720 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003721
3722 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003723 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003724}
3725
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003726static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003727{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003728 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003729
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003730 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003731 cancel_delayed_work(&priv->scan_check);
3732 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003733 cancel_work_sync(&priv->beacon_update);
3734}
3735
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003736static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003737 &dev_attr_antenna.attr,
3738 &dev_attr_channels.attr,
3739 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003740 &dev_attr_flags.attr,
3741 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003742#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003743 &dev_attr_measurement.attr,
3744#endif
Zhu Yib481de92007-09-25 17:54:57 -07003745 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003746 &dev_attr_statistics.attr,
3747 &dev_attr_status.attr,
3748 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003749 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003750#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003751 &dev_attr_debug_level.attr,
3752#endif
Zhu Yib481de92007-09-25 17:54:57 -07003753 NULL
3754};
3755
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003756static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003757 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003758 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003759};
3760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003761static struct ieee80211_ops iwl3945_hw_ops = {
3762 .tx = iwl3945_mac_tx,
3763 .start = iwl3945_mac_start,
3764 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003765 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003766 .remove_interface = iwl_mac_remove_interface,
Abhijeet Kolekar48083682009-04-08 11:26:48 -07003767 .config = iwl_mac_config,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003768 .configure_filter = iwl_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003769 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekaraa89f312009-04-08 11:26:51 -07003770 .get_tx_stats = iwl_mac_get_tx_stats,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003771 .conf_tx = iwl_mac_conf_tx,
Abhijeet Kolekarbd564262009-04-08 11:26:52 -07003772 .reset_tsf = iwl_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003773 .bss_info_changed = iwl_bss_info_changed,
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -08003774 .hw_scan = iwl_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07003775};
3776
Winkler, Tomase52119c2008-12-22 11:31:19 +08003777static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003778{
3779 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003780 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003781
3782 priv->retry_rate = 1;
3783 priv->ibss_beacon = NULL;
3784
3785 spin_lock_init(&priv->lock);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003786 spin_lock_init(&priv->sta_lock);
3787 spin_lock_init(&priv->hcmd_lock);
3788
3789 INIT_LIST_HEAD(&priv->free_frames);
3790
3791 mutex_init(&priv->mutex);
3792
3793 /* Clear the driver's (not device's) station table */
Tomas Winklerc587de02009-06-03 11:44:07 -07003794 iwl_clear_stations_table(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003795
3796 priv->data_retry_limit = -1;
3797 priv->ieee_channels = NULL;
3798 priv->ieee_rates = NULL;
3799 priv->band = IEEE80211_BAND_2GHZ;
3800
3801 priv->iw_mode = NL80211_IFTYPE_STATION;
3802
3803 iwl_reset_qos(priv);
3804
3805 priv->qos_data.qos_active = 0;
3806 priv->qos_data.qos_cap.val = 0;
3807
3808 priv->rates_mask = IWL_RATES_MASK;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003809 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003810
Samuel Ortize6148912009-01-23 13:45:15 -08003811 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3812 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3813 eeprom->version);
3814 ret = -EINVAL;
3815 goto err;
3816 }
3817 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003818 if (ret) {
3819 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3820 goto err;
3821 }
3822
Samuel Ortize6148912009-01-23 13:45:15 -08003823 /* Set up txpower settings in driver for all channels */
3824 if (iwl3945_txpower_set_from_eeprom(priv)) {
3825 ret = -EIO;
3826 goto err_free_channel_map;
3827 }
3828
Samuel Ortiz534166d2009-01-23 13:45:16 -08003829 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003830 if (ret) {
3831 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3832 goto err_free_channel_map;
3833 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003834 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3835
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003836 return 0;
3837
3838err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003839 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003840err:
3841 return ret;
3842}
3843
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003844static int iwl3945_setup_mac(struct iwl_priv *priv)
3845{
3846 int ret;
3847 struct ieee80211_hw *hw = priv->hw;
3848
3849 hw->rate_control_algorithm = "iwl-3945-rs";
3850 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
3851
3852 /* Tell mac80211 our characteristics */
3853 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Mohamed Abbasb1c60192009-03-17 21:51:47 -07003854 IEEE80211_HW_NOISE_DBM |
Johannes Berge312c242009-08-07 15:41:51 -07003855 IEEE80211_HW_SPECTRUM_MGMT |
3856 IEEE80211_HW_SUPPORTS_PS |
3857 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003858
3859 hw->wiphy->interface_modes =
3860 BIT(NL80211_IFTYPE_STATION) |
3861 BIT(NL80211_IFTYPE_ADHOC);
3862
3863 hw->wiphy->custom_regulatory = true;
3864
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003865 /* Firmware does not support this */
3866 hw->wiphy->disable_beacon_hints = true;
3867
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003868 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3869 /* we create the 802.11 header and a zero-length SSID element */
3870 hw->wiphy->max_scan_ie_len = IWL_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003871
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003872 /* Default value; 4 EDCA QOS priorities */
3873 hw->queues = 4;
3874
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003875 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3876 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3877 &priv->bands[IEEE80211_BAND_2GHZ];
3878
3879 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3880 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3881 &priv->bands[IEEE80211_BAND_5GHZ];
3882
3883 ret = ieee80211_register_hw(priv->hw);
3884 if (ret) {
3885 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3886 return ret;
3887 }
3888 priv->mac80211_registered = 1;
3889
3890 return 0;
3891}
3892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003893static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003894{
3895 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003896 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003897 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003898 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003899 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003900 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003901
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003902 /***********************
3903 * 1. Allocating HW data
3904 * ********************/
3905
Zhu Yib481de92007-09-25 17:54:57 -07003906 /* mac80211 allocates memory for this device instance, including
3907 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003908 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003909 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003910 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07003911 err = -ENOMEM;
3912 goto out;
3913 }
Zhu Yib481de92007-09-25 17:54:57 -07003914 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003915 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003916
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003917 /*
3918 * Disabling hardware scan means that mac80211 will perform scans
3919 * "the hard way", rather than using device's scan.
3920 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003921 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08003922 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003923 iwl3945_hw_ops.hw_scan = NULL;
3924 }
3925
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003926
Tomas Winklere1623442009-01-27 14:27:56 -08003927 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003928 priv->cfg = cfg;
3929 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07003930 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003931
Samuel Ortizd08853a2009-01-23 13:45:17 -08003932#ifdef CONFIG_IWLWIFI_DEBUG
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003933 atomic_set(&priv->restrict_refcnt, 0);
3934#endif
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003935 if (iwl_alloc_traffic_mem(priv))
3936 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07003937
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003938 /***************************
3939 * 2. Initializing PCI bus
3940 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07003941 if (pci_enable_device(pdev)) {
3942 err = -ENODEV;
3943 goto out_ieee80211_free_hw;
3944 }
3945
3946 pci_set_master(pdev);
3947
Yang Hongyang284901a2009-04-06 19:01:15 -07003948 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07003949 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07003950 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07003951 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08003952 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003953 goto out_pci_disable_device;
3954 }
3955
3956 pci_set_drvdata(pdev, priv);
3957 err = pci_request_regions(pdev, DRV_NAME);
3958 if (err)
3959 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003960
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003961 /***********************
3962 * 3. Read REV Register
3963 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07003964 priv->hw_base = pci_iomap(pdev, 0, 0);
3965 if (!priv->hw_base) {
3966 err = -ENODEV;
3967 goto out_pci_release_regions;
3968 }
3969
Tomas Winklere1623442009-01-27 14:27:56 -08003970 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003971 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08003972 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07003973
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003974 /* We disable the RETRY_TIMEOUT register (0x41) to keep
3975 * PCI Tx retries from interfering with C3 CPU state */
3976 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07003977
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07003978 /* this spin lock will be used in apm_ops.init and EEPROM access
3979 * we should init now
3980 */
3981 spin_lock_init(&priv->reg_lock);
3982
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003983 /* amp init */
3984 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003985 if (err < 0) {
David S. Millerd5df2a12009-03-10 05:04:16 -07003986 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003987 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003988 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003989
3990 /***********************
3991 * 4. Read EEPROM
3992 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003993
Zhu Yi5a669262008-01-14 17:46:18 -08003994 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08003995 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003996 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003997 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08003998 goto out_iounmap;
Zhu Yi5a669262008-01-14 17:46:18 -08003999 }
4000 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004001 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4002 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08004003 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08004004 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4005
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004006 /***********************
4007 * 5. Setup HW Constants
4008 * ********************/
4009 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004010 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004011 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004012 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004013 }
4014
4015 /***********************
4016 * 6. Setup priv
4017 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004018
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004019 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004020 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004021 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004022 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004023 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004024
Tomas Winkler978785a2008-12-19 10:37:31 +08004025 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4026 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004027
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004028 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004029 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004030 * ********************/
4031
4032 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004033 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004034 spin_unlock_irqrestore(&priv->lock, flags);
4035
Helmut Schaa26635162009-01-15 09:38:44 +01004036 pci_enable_msi(priv->pci_dev);
4037
Mohamed Abbasef850d72009-05-22 11:01:50 -07004038 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
4039 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004040 if (err) {
4041 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4042 goto out_disable_msi;
4043 }
4044
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004045 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4046 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004047 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004048 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004049 }
4050
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004051 iwl_set_rxon_channel(priv,
4052 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004053 iwl3945_setup_deferred_work(priv);
4054 iwl3945_setup_rx_handlers(priv);
4055
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004056 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004057 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004058 * *******************************/
4059
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004060 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004061
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004062 err = iwl3945_setup_mac(priv);
4063 if (err)
4064 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004065
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004066 err = iwl_dbgfs_register(priv, DRV_NAME);
4067 if (err)
4068 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4069
Helmut Schaa26635162009-01-15 09:38:44 +01004070 /* Start monitoring the killswitch */
4071 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
4072 2 * HZ);
4073
Zhu Yib481de92007-09-25 17:54:57 -07004074 return 0;
4075
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004076 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004077 destroy_workqueue(priv->workqueue);
4078 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004079 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4080 out_release_irq:
4081 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004082 out_disable_msi:
4083 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004084 iwlcore_free_geos(priv);
4085 iwl_free_channel_map(priv);
4086 out_unset_hw_params:
4087 iwl3945_unset_hw_params(priv);
4088 out_eeprom_free:
4089 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004090 out_iounmap:
4091 pci_iounmap(pdev, priv->hw_base);
4092 out_pci_release_regions:
4093 pci_release_regions(pdev);
4094 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004095 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004096 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004097 out_ieee80211_free_hw:
4098 ieee80211_free_hw(priv->hw);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004099 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004100 out:
4101 return err;
4102}
4103
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004104static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004105{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004106 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004107 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004108
4109 if (!priv)
4110 return;
4111
Tomas Winklere1623442009-01-27 14:27:56 -08004112 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004113
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004114 iwl_dbgfs_unregister(priv);
4115
Zhu Yib481de92007-09-25 17:54:57 -07004116 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004117
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004118 if (priv->mac80211_registered) {
4119 ieee80211_unregister_hw(priv->hw);
4120 priv->mac80211_registered = 0;
4121 } else {
4122 iwl3945_down(priv);
4123 }
Zhu Yib481de92007-09-25 17:54:57 -07004124
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004125 /* make sure we flush any pending irq or
4126 * tasklet for the driver
4127 */
4128 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004129 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004130 spin_unlock_irqrestore(&priv->lock, flags);
4131
4132 iwl_synchronize_irq(priv);
4133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004134 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004135
Reinette Chatre71d449b2009-04-20 14:37:01 -07004136 cancel_delayed_work_sync(&priv->rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004137
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004138 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004139
4140 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004141 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004142 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004143
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004144 iwl3945_unset_hw_params(priv);
Tomas Winklerc587de02009-06-03 11:44:07 -07004145 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004146
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004147 /*netif_stop_queue(dev); */
4148 flush_workqueue(priv->workqueue);
4149
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004150 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004151 * priv->workqueue... so we can't take down the workqueue
4152 * until now... */
4153 destroy_workqueue(priv->workqueue);
4154 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004155 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004156
Helmut Schaa26635162009-01-15 09:38:44 +01004157 free_irq(pdev->irq, priv);
4158 pci_disable_msi(pdev);
4159
Zhu Yib481de92007-09-25 17:54:57 -07004160 pci_iounmap(pdev, priv->hw_base);
4161 pci_release_regions(pdev);
4162 pci_disable_device(pdev);
4163 pci_set_drvdata(pdev, NULL);
4164
Samuel Ortize6148912009-01-23 13:45:15 -08004165 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004166 iwlcore_free_geos(priv);
Winkler, Tomas805cee52009-01-19 15:30:28 -08004167 kfree(priv->scan);
Zhu Yib481de92007-09-25 17:54:57 -07004168 if (priv->ibss_beacon)
4169 dev_kfree_skb(priv->ibss_beacon);
4170
4171 ieee80211_free_hw(priv->hw);
4172}
4173
Zhu Yib481de92007-09-25 17:54:57 -07004174
4175/*****************************************************************************
4176 *
4177 * driver and module entry point
4178 *
4179 *****************************************************************************/
4180
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004181static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004182 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004183 .id_table = iwl3945_hw_card_ids,
4184 .probe = iwl3945_pci_probe,
4185 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004186#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004187 .suspend = iwl_pci_suspend,
4188 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004189#endif
4190};
4191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004192static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004193{
4194
4195 int ret;
4196 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4197 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004198
4199 ret = iwl3945_rate_control_register();
4200 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004201 printk(KERN_ERR DRV_NAME
4202 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004203 return ret;
4204 }
4205
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004206 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004207 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004208 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004209 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004210 }
Zhu Yib481de92007-09-25 17:54:57 -07004211
4212 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004213
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004214error_register:
4215 iwl3945_rate_control_unregister();
4216 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004217}
4218
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004219static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004220{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004221 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004222 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004223}
4224
Reinette Chatrea0987a82008-12-02 12:14:06 -08004225MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004226
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004227module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004228MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004229module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004230MODULE_PARM_DESC(swcrypto,
4231 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004232#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004233module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004234MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004235#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004236module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4237 int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004238MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004239module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004240MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004242module_exit(iwl3945_exit);
4243module_init(iwl3945_init);