blob: 54daa38ecba3b1475bcff226be08b8be1375f9d1 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2003 - 2010 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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040036#include <linux/sched.h>
Zhu Yib481de92007-09-25 17:54:57 -070037#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
44#include <net/ieee80211_radiotap.h>
45#include <net/mac80211.h>
46
47#include <asm/div64.h>
48
Samuel Ortiza3139c52008-12-19 10:37:09 +080049#define DRV_NAME "iwl3945"
50
Winkler, Tomasdbb66542008-12-22 11:31:14 +080051#include "iwl-fh.h"
52#include "iwl-3945-fh.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080053#include "iwl-commands.h"
Samuel Ortiz17f841c2009-01-23 13:45:20 -080054#include "iwl-sta.h"
Zhu Yib481de92007-09-25 17:54:57 -070055#include "iwl-3945.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080056#include "iwl-core.h"
Reinette Chatre4a6547c2010-02-18 22:03:02 -080057#include "iwl-helpers.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080058#include "iwl-dev.h"
Reinette Chatre81963d62010-01-22 14:22:57 -080059#include "iwl-spectrum.h"
Zhu Yib481de92007-09-25 17:54:57 -070060
Zhu Yib481de92007-09-25 17:54:57 -070061/*
62 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070063 */
64
65#define DRV_DESCRIPTION \
66"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
67
Samuel Ortizd08853a2009-01-23 13:45:17 -080068#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070069#define VD "d"
70#else
71#define VD
72#endif
73
Reinette Chatre81963d62010-01-22 14:22:57 -080074/*
75 * add "s" to indicate spectrum measurement included.
76 * we add it here to be consistent with previous releases in which
77 * this was configurable.
78 */
79#define DRV_VERSION IWLWIFI_VERSION VD "s"
Reinette Chatre1f447802010-01-15 13:43:41 -080080#define DRV_COPYRIGHT "Copyright(c) 2003-2010 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080081#define DRV_AUTHOR "<ilw@linux.intel.com>"
Zhu Yib481de92007-09-25 17:54:57 -070082
83MODULE_DESCRIPTION(DRV_DESCRIPTION);
84MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080085MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070086MODULE_LICENSE("GPL");
87
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080088 /* module parameters */
89struct iwl_mod_params iwl3945_mod_params = {
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080090 .sw_crypto = 1,
Samuel Ortizaf48d042009-01-23 13:45:19 -080091 .restart_fw = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080092 /* the rest are 0 by default */
93};
94
Zhu Yib481de92007-09-25 17:54:57 -070095/**
Samuel Ortiz7e4bca52009-01-23 13:45:18 -080096 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
97 * @priv: eeprom and antenna fields are used to determine antenna flags
98 *
99 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
100 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
101 *
102 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
103 * IWL_ANTENNA_MAIN - Force MAIN antenna
104 * IWL_ANTENNA_AUX - Force AUX antenna
105 */
106__le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
107{
108 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
109
110 switch (iwl3945_mod_params.antenna) {
111 case IWL_ANTENNA_DIVERSITY:
112 return 0;
113
114 case IWL_ANTENNA_MAIN:
115 if (eeprom->antenna_switch_type)
116 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
117 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
118
119 case IWL_ANTENNA_AUX:
120 if (eeprom->antenna_switch_type)
121 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
122 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
123 }
124
125 /* bad antenna selector value */
126 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
127 iwl3945_mod_params.antenna);
128
129 return 0; /* "diversity" is default if error */
130}
131
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800132static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700133 struct ieee80211_key_conf *keyconf,
134 u8 sta_id)
135{
136 unsigned long flags;
137 __le16 key_flags = 0;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800138 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700139
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800140 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
141 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
142
143 if (sta_id == priv->hw_params.bcast_sta_id)
144 key_flags |= STA_KEY_MULTICAST_MSK;
145
146 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
147 keyconf->hw_key_idx = keyconf->keyidx;
148 key_flags &= ~STA_KEY_FLG_INVALID;
149
Zhu Yib481de92007-09-25 17:54:57 -0700150 spin_lock_irqsave(&priv->sta_lock, flags);
Tomas Winklerc587de02009-06-03 11:44:07 -0700151 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
152 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
153 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700154 keyconf->keylen);
155
Tomas Winklerc587de02009-06-03 11:44:07 -0700156 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700157 keyconf->keylen);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800158
Tomas Winklerc587de02009-06-03 11:44:07 -0700159 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800160 == STA_KEY_FLG_NO_ENC)
Tomas Winklerc587de02009-06-03 11:44:07 -0700161 priv->stations[sta_id].sta.key.key_offset =
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800162 iwl_get_free_ucode_key_index(priv);
163 /* else, we are overriding an existing key => no need to allocated room
164 * in uCode. */
165
Tomas Winklerc587de02009-06-03 11:44:07 -0700166 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800167 "no space for a new key");
168
Tomas Winklerc587de02009-06-03 11:44:07 -0700169 priv->stations[sta_id].sta.key.key_flags = key_flags;
170 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
171 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700172
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800173 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
174
Tomas Winklerc587de02009-06-03 11:44:07 -0700175 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800176
Zhu Yib481de92007-09-25 17:54:57 -0700177 spin_unlock_irqrestore(&priv->sta_lock, flags);
178
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800179 return ret;
180}
181
182static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
183 struct ieee80211_key_conf *keyconf,
184 u8 sta_id)
185{
186 return -EOPNOTSUPP;
187}
188
189static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
190 struct ieee80211_key_conf *keyconf,
191 u8 sta_id)
192{
193 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -0700194}
195
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800196static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700197{
198 unsigned long flags;
199
200 spin_lock_irqsave(&priv->sta_lock, flags);
Tomas Winklerc587de02009-06-03 11:44:07 -0700201 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
202 memset(&priv->stations[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +0800203 sizeof(struct iwl4965_keyinfo));
Tomas Winklerc587de02009-06-03 11:44:07 -0700204 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
205 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
206 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700207 spin_unlock_irqrestore(&priv->sta_lock, flags);
208
Tomas Winklere1623442009-01-27 14:27:56 -0800209 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
Tomas Winklerc587de02009-06-03 11:44:07 -0700210 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700211 return 0;
212}
213
Abhijeet Kolekarfa11d522009-03-11 11:17:54 -0700214static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800215 struct ieee80211_key_conf *keyconf, u8 sta_id)
216{
217 int ret = 0;
218
219 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
220
221 switch (keyconf->alg) {
222 case ALG_CCMP:
223 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
224 break;
225 case ALG_TKIP:
226 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
227 break;
228 case ALG_WEP:
229 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
230 break;
231 default:
Abhijeet Kolekar1e680232009-03-17 21:51:50 -0700232 IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800233 ret = -EINVAL;
234 }
235
236 IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
237 keyconf->alg, keyconf->keylen, keyconf->keyidx,
238 sta_id, ret);
239
240 return ret;
241}
242
243static int iwl3945_remove_static_key(struct iwl_priv *priv)
244{
245 int ret = -EOPNOTSUPP;
246
247 return ret;
248}
249
250static int iwl3945_set_static_key(struct iwl_priv *priv,
251 struct ieee80211_key_conf *key)
252{
253 if (key->alg == ALG_WEP)
254 return -EOPNOTSUPP;
255
256 IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
257 return -EINVAL;
258}
259
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800260static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700261{
262 struct list_head *element;
263
Tomas Winklere1623442009-01-27 14:27:56 -0800264 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700265 priv->frames_count);
266
267 while (!list_empty(&priv->free_frames)) {
268 element = priv->free_frames.next;
269 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800270 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700271 priv->frames_count--;
272 }
273
274 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800275 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700276 priv->frames_count);
277 priv->frames_count = 0;
278 }
279}
280
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800281static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700282{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800283 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700284 struct list_head *element;
285 if (list_empty(&priv->free_frames)) {
286 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
287 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800288 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700289 return NULL;
290 }
291
292 priv->frames_count++;
293 return frame;
294 }
295
296 element = priv->free_frames.next;
297 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800298 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700299}
300
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800301static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700302{
303 memset(frame, 0, sizeof(*frame));
304 list_add(&frame->list, &priv->free_frames);
305}
306
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800307unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700308 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200309 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700310{
311
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800312 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200313 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
314 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -0700315 return 0;
316
317 if (priv->ibss_beacon->len > left)
318 return 0;
319
320 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
321
322 return priv->ibss_beacon->len;
323}
324
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800325static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700326{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800327 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700328 unsigned int frame_size;
329 int rc;
330 u8 rate;
331
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800332 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700333
334 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800335 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700336 "command.\n");
337 return -ENOMEM;
338 }
339
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800340 rate = iwl_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700341
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800342 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700343
Samuel Ortiz518099a2009-01-19 15:30:27 -0800344 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700345 &frame->u.cmd[0]);
346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800347 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700348
349 return rc;
350}
351
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800352static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700353{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800354 if (priv->shared_virt)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800355 dma_free_coherent(&priv->pci_dev->dev,
356 sizeof(struct iwl3945_shared),
357 priv->shared_virt,
358 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700359}
360
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800361static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200362 struct ieee80211_tx_info *info,
Johannes Bergc2acea82009-07-24 11:13:05 -0700363 struct iwl_device_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700364 struct sk_buff *skb_frag,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800365 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700366{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700367 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Tomas Winklerc587de02009-06-03 11:44:07 -0700368 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -0700369
370 switch (keyinfo->alg) {
371 case ALG_CCMP:
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700372 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
373 memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
Tomas Winklere1623442009-01-27 14:27:56 -0800374 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -0700375 break;
376
377 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -0700378 break;
379
380 case ALG_WEP:
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700381 tx_cmd->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +0200382 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -0700383
384 if (keyinfo->keylen == 13)
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700385 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -0700386
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700387 memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -0700388
Tomas Winklere1623442009-01-27 14:27:56 -0800389 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +0200390 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -0700391 break;
392
Zhu Yib481de92007-09-25 17:54:57 -0700393 default:
Tomas Winkler978785a2008-12-19 10:37:31 +0800394 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -0700395 break;
396 }
397}
398
399/*
400 * handle build REPLY_TX command notification.
401 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800402static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Johannes Bergc2acea82009-07-24 11:13:05 -0700403 struct iwl_device_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200404 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +0800405 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -0700406{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700407 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
408 __le32 tx_flags = tx_cmd->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700409 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700410
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700411 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200412 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700413 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700414 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700415 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700416 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -0700417 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
418 tx_flags |= TX_CMD_FLG_TSF_MSK;
419 } else {
420 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
421 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
422 }
423
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700424 tx_cmd->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700425 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700426 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
427
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700428 if (ieee80211_is_data_qos(fc)) {
429 u8 *qc = ieee80211_get_qos_ctl(hdr);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700430 tx_cmd->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -0700431 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800432 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700433 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800434 }
Zhu Yib481de92007-09-25 17:54:57 -0700435
Abhijeet Kolekar37dc70f2009-10-09 13:20:30 -0700436 priv->cfg->ops->utils->rts_tx_cmd_flag(info, &tx_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700437
438 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
439 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
440
441 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700442 if (ieee80211_is_mgmt(fc)) {
443 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700444 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -0700445 else
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700446 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700447 } else {
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700448 tx_cmd->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700449 }
Zhu Yib481de92007-09-25 17:54:57 -0700450
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700451 tx_cmd->driver_txop = 0;
452 tx_cmd->tx_flags = tx_flags;
453 tx_cmd->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700454}
455
Zhu Yib481de92007-09-25 17:54:57 -0700456/*
457 * start REPLY_TX command process
458 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800459static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700460{
461 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200462 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700463 struct iwl3945_tx_cmd *tx_cmd;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800464 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800465 struct iwl_queue *q = NULL;
Johannes Bergc2acea82009-07-24 11:13:05 -0700466 struct iwl_device_cmd *out_cmd;
467 struct iwl_cmd_meta *out_meta;
Zhu Yib481de92007-09-25 17:54:57 -0700468 dma_addr_t phys_addr;
469 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800470 int txq_id = skb_get_queue_mapping(skb);
Reinette Chatredf833b12009-04-21 10:55:48 -0700471 u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
Tomas Winkler54dbb522008-05-15 13:54:06 +0800472 u8 id;
473 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700474 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800475 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700476 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700477 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700478 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800479 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700480 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700481
482 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800483 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800484 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700485 goto drop_unlock;
486 }
487
Johannes Berge039fa42008-05-15 12:55:29 +0200488 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800489 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700490 goto drop_unlock;
491 }
492
493 unicast = !is_multicast_ether_addr(hdr->addr1);
494 id = 0;
495
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700496 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700497
Samuel Ortizd08853a2009-01-23 13:45:17 -0800498#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700499 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800500 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700501 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800502 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700503 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800504 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700505#endif
506
Gábor Stefanikaa065262009-08-21 20:44:09 +0200507 /* drop all non-injected data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +0800508 if (ieee80211_is_data(fc) &&
Gábor Stefanikaa065262009-08-21 20:44:09 +0200509 !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800510 (!iwl_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200511 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Tomas Winklere1623442009-01-27 14:27:56 -0800512 IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -0700513 goto drop_unlock;
514 }
515
516 spin_unlock_irqrestore(&priv->lock, flags);
517
Harvey Harrison7294ec92008-07-15 18:43:59 -0700518 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800519
520 /* Find (or create) index into station table for destination station */
Gábor Stefanikaa065262009-08-21 20:44:09 +0200521 if (info->flags & IEEE80211_TX_CTL_INJECTED)
522 sta_id = priv->hw_params.bcast_sta_id;
523 else
524 sta_id = iwl_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -0700525 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800526 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700527 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700528 goto drop;
529 }
530
Tomas Winklere1623442009-01-27 14:27:56 -0800531 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700532
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700533 if (ieee80211_is_data_qos(fc)) {
534 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700535 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700536 if (unlikely(tid >= MAX_TID_COUNT))
537 goto drop;
Tomas Winklerc587de02009-06-03 11:44:07 -0700538 seq_number = priv->stations[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -0700539 IEEE80211_SCTL_SEQ;
540 hdr->seq_ctrl = cpu_to_le16(seq_number) |
541 (hdr->seq_ctrl &
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800542 cpu_to_le16(IEEE80211_SCTL_FRAG));
Zhu Yib481de92007-09-25 17:54:57 -0700543 seq_number += 0x10;
544 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800545
546 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800547 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700548 q = &txq->q;
549
Zhu Yidc57a302009-12-14 14:12:12 -0800550 if ((iwl_queue_space(q) < q->high_mark))
551 goto drop;
552
Zhu Yib481de92007-09-25 17:54:57 -0700553 spin_lock_irqsave(&priv->lock, flags);
554
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800555 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700556
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800557 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800558 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800559 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800560
561 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800562 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700563 out_meta = &txq->meta[idx];
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700564 tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700565 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700566 memset(tx_cmd, 0, sizeof(*tx_cmd));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800567
568 /*
569 * Set up the Tx-command (not MAC!) header.
570 * Store the chosen Tx queue and TFD index within the sequence field;
571 * after Tx, uCode's Tx response will return this value so driver can
572 * locate the frame within the tx queue and do post-tx processing.
573 */
Zhu Yib481de92007-09-25 17:54:57 -0700574 out_cmd->hdr.cmd = REPLY_TX;
575 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800576 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800577
578 /* Copy MAC header from skb into command buffer */
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700579 memcpy(tx_cmd->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700580
Reinette Chatredf833b12009-04-21 10:55:48 -0700581
582 if (info->control.hw_key)
583 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
584
585 /* TODO need this for burst mode later on */
586 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
587
588 /* set is_hcca to 0; it probably will never be implemented */
589 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
590
591 /* Total # bytes to be transmitted */
592 len = (u16)skb->len;
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700593 tx_cmd->len = cpu_to_le16(len);
Reinette Chatredf833b12009-04-21 10:55:48 -0700594
Wey-Yi Guy20594eb2009-08-07 15:41:39 -0700595 iwl_dbg_log_tx_data_frame(priv, len, hdr);
Wey-Yi Guy22fdf3c2009-08-07 15:41:40 -0700596 iwl_update_stats(priv, true, fc, len);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700597 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
598 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Reinette Chatredf833b12009-04-21 10:55:48 -0700599
600 if (!ieee80211_has_morefrags(hdr->frame_control)) {
601 txq->need_update = 1;
602 if (qc)
Tomas Winklerc587de02009-06-03 11:44:07 -0700603 priv->stations[sta_id].tid[tid].seq_number = seq_number;
Reinette Chatredf833b12009-04-21 10:55:48 -0700604 } else {
605 wait_write_ptr = 1;
606 txq->need_update = 0;
607 }
608
609 IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n",
610 le16_to_cpu(out_cmd->hdr.sequence));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700611 IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx_cmd->tx_flags));
612 iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
613 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
Reinette Chatredf833b12009-04-21 10:55:48 -0700614 ieee80211_hdrlen(fc));
615
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800616 /*
617 * Use the first empty entry in this queue's command buffer array
618 * to contain the Tx command and MAC header concatenated together
619 * (payload data will be in another buffer).
620 * Size of this varies, due to varying MAC header length.
621 * If end is not dword aligned, we'll have 2 extra bytes at the end
622 * of the MAC header (device reads on dword boundaries).
623 * We'll tell device about this padding later.
624 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800625 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800626 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700627
628 len_org = len;
629 len = (len + 3) & ~3;
630
631 if (len_org != len)
632 len_org = 1;
633 else
634 len_org = 0;
635
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800636 /* Physical address of this Tx command's header (not MAC header!),
637 * within command buffer array. */
Reinette Chatredf833b12009-04-21 10:55:48 -0700638 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
639 len, PCI_DMA_TODEVICE);
640 /* we do not map meta data ... so we can safely access address to
641 * provide to unmap command*/
Johannes Bergc2acea82009-07-24 11:13:05 -0700642 pci_unmap_addr_set(out_meta, mapping, txcmd_phys);
643 pci_unmap_len_set(out_meta, len, len);
Zhu Yib481de92007-09-25 17:54:57 -0700644
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800645 /* Add buffer containing Tx command and MAC(!) header to TFD's
646 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800647 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
648 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700649
Zhu Yib481de92007-09-25 17:54:57 -0700650
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800651 /* Set up TFD's 2nd entry to point directly to remainder of skb,
652 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700653 len = skb->len - hdr_len;
654 if (len) {
655 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
656 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800657 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
658 phys_addr, len,
659 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700660 }
661
Zhu Yib481de92007-09-25 17:54:57 -0700662
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800663 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800664 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800665 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700666 spin_unlock_irqrestore(&priv->lock, flags);
667
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800668 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700669 && priv->mac80211_registered) {
670 if (wait_write_ptr) {
671 spin_lock_irqsave(&priv->lock, flags);
672 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800673 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700674 spin_unlock_irqrestore(&priv->lock, flags);
675 }
676
Johannes Berge4e72fb2009-03-23 17:28:42 +0100677 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700678 }
679
680 return 0;
681
682drop_unlock:
683 spin_unlock_irqrestore(&priv->lock, flags);
684drop:
685 return -1;
686}
687
Zhu Yib481de92007-09-25 17:54:57 -0700688#define BEACON_TIME_MASK_LOW 0x00FFFFFF
689#define BEACON_TIME_MASK_HIGH 0xFF000000
690#define TIME_UNIT 1024
691
692/*
693 * extended beacon time format
694 * time in usec will be changed into a 32-bit value in 8:24 format
695 * the high 1 byte is the beacon counts
696 * the lower 3 bytes is the time in usec within one beacon interval
697 */
698
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800699static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700700{
701 u32 quot;
702 u32 rem;
703 u32 interval = beacon_interval * 1024;
704
705 if (!interval || !usec)
706 return 0;
707
708 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
709 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
710
711 return (quot << 24) + rem;
712}
713
714/* base is usually what we get from ucode with each received frame,
715 * the same as HW timer counter counting down
716 */
717
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800718static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700719{
720 u32 base_low = base & BEACON_TIME_MASK_LOW;
721 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
722 u32 interval = beacon_interval * TIME_UNIT;
723 u32 res = (base & BEACON_TIME_MASK_HIGH) +
724 (addon & BEACON_TIME_MASK_HIGH);
725
726 if (base_low > addon_low)
727 res += base_low - addon_low;
728 else if (base_low < addon_low) {
729 res += interval + base_low - addon_low;
730 res += (1 << 24);
731 } else
732 res += (1 << 24);
733
734 return cpu_to_le32(res);
735}
736
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800737static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700738 struct ieee80211_measurement_params *params,
739 u8 type)
740{
Tomas Winkler600c0e12008-12-19 10:37:04 +0800741 struct iwl_spectrum_cmd spectrum;
Zhu Yi2f301222009-10-09 17:19:45 +0800742 struct iwl_rx_packet *pkt;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800743 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700744 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
745 .data = (void *)&spectrum,
Johannes Bergc2acea82009-07-24 11:13:05 -0700746 .flags = CMD_WANT_SKB,
Zhu Yib481de92007-09-25 17:54:57 -0700747 };
748 u32 add_time = le64_to_cpu(params->start_time);
749 int rc;
750 int spectrum_resp_status;
751 int duration = le16_to_cpu(params->duration);
752
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800753 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700754 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800755 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -0700756 le64_to_cpu(params->start_time) - priv->last_tsf,
757 le16_to_cpu(priv->rxon_timing.beacon_interval));
758
759 memset(&spectrum, 0, sizeof(spectrum));
760
761 spectrum.channel_count = cpu_to_le16(1);
762 spectrum.flags =
763 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
764 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
765 cmd.len = sizeof(spectrum);
766 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
767
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800768 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700769 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800770 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -0700771 add_time,
772 le16_to_cpu(priv->rxon_timing.beacon_interval));
773 else
774 spectrum.start_time = 0;
775
776 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
777 spectrum.channels[0].channel = params->channel;
778 spectrum.channels[0].type = type;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800779 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700780 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
781 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
782
Samuel Ortiz518099a2009-01-19 15:30:27 -0800783 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700784 if (rc)
785 return rc;
786
Zhu Yi2f301222009-10-09 17:19:45 +0800787 pkt = (struct iwl_rx_packet *)cmd.reply_page;
788 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800789 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700790 rc = -EIO;
791 }
792
Zhu Yi2f301222009-10-09 17:19:45 +0800793 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
Zhu Yib481de92007-09-25 17:54:57 -0700794 switch (spectrum_resp_status) {
795 case 0: /* Command will be handled */
Zhu Yi2f301222009-10-09 17:19:45 +0800796 if (pkt->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800797 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800798 pkt->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700799 priv->measurement_status &= ~MEASUREMENT_READY;
800 }
801 priv->measurement_status |= MEASUREMENT_ACTIVE;
802 rc = 0;
803 break;
804
805 case 1: /* Command will not be handled */
806 rc = -EAGAIN;
807 break;
808 }
809
Zhu Yi64a76b52009-12-10 14:37:21 -0800810 iwl_free_pages(priv, cmd.reply_page);
Zhu Yib481de92007-09-25 17:54:57 -0700811
812 return rc;
813}
Zhu Yib481de92007-09-25 17:54:57 -0700814
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800815static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800816 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700817{
Zhu Yi2f301222009-10-09 17:19:45 +0800818 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800819 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700820 struct delayed_work *pwork;
821
822 palive = &pkt->u.alive_frame;
823
Tomas Winklere1623442009-01-27 14:27:56 -0800824 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700825 "0x%01X 0x%01X\n",
826 palive->is_valid, palive->ver_type,
827 palive->ver_subtype);
828
829 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800830 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800831 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
832 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700833 pwork = &priv->init_alive_start;
834 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800835 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700836 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800837 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700838 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700840 }
841
842 /* We delay the ALIVE response by 5ms to
843 * give the HW RF Kill time to activate... */
844 if (palive->is_valid == UCODE_VALID_OK)
845 queue_delayed_work(priv->workqueue, pwork,
846 msecs_to_jiffies(5));
847 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800848 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700849}
850
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800851static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800852 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700853{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100854#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800855 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Helmut Schaac7e035a2009-01-19 13:02:15 +0100856#endif
Zhu Yib481de92007-09-25 17:54:57 -0700857
Tomas Winklere1623442009-01-27 14:27:56 -0800858 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700859 return;
860}
861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800862static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700863{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800864 struct iwl_priv *priv =
865 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700866 struct sk_buff *beacon;
867
868 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200869 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700870
871 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800872 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700873 return;
874 }
875
876 mutex_lock(&priv->mutex);
877 /* new beacon skb is allocated every time; dispose previous.*/
878 if (priv->ibss_beacon)
879 dev_kfree_skb(priv->ibss_beacon);
880
881 priv->ibss_beacon = beacon;
882 mutex_unlock(&priv->mutex);
883
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800884 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700885}
886
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800887static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800888 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700889{
Samuel Ortizd08853a2009-01-23 13:45:17 -0800890#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800891 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800892 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -0700893 u8 rate = beacon->beacon_notify_hdr.rate;
894
Tomas Winklere1623442009-01-27 14:27:56 -0800895 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700896 "tsf %d %d rate %d\n",
897 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
898 beacon->beacon_notify_hdr.failure_frame,
899 le32_to_cpu(beacon->ibss_mgr_status),
900 le32_to_cpu(beacon->high_tsf),
901 le32_to_cpu(beacon->low_tsf), rate);
902#endif
903
Johannes Berg05c914f2008-09-11 00:01:58 +0200904 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700905 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
906 queue_work(priv->workqueue, &priv->beacon_update);
907}
908
Zhu Yib481de92007-09-25 17:54:57 -0700909/* Handle notification from uCode that card's power state is changing
910 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800911static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800912 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700913{
Zhu Yi2f301222009-10-09 17:19:45 +0800914 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700915 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
916 unsigned long status = priv->status;
917
Reinette Chatre4c423a22009-07-17 09:30:26 -0700918 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700919 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
920 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
921
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800922 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700923 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
924
925 if (flags & HW_CARD_DISABLED)
926 set_bit(STATUS_RF_KILL_HW, &priv->status);
927 else
928 clear_bit(STATUS_RF_KILL_HW, &priv->status);
929
930
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800931 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700932
933 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200934 test_bit(STATUS_RF_KILL_HW, &priv->status)))
935 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
936 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700937 else
938 wake_up_interruptible(&priv->wait_command_queue);
939}
940
941/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800942 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700943 *
944 * Setup the RX handlers for each of the reply types sent from the uCode
945 * to the host.
946 *
947 * This function chains into the hardware specific files for them to setup
948 * any hardware specific handlers as well.
949 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800950static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700951{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800952 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
953 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800954 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800955 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Reinette Chatre81963d62010-01-22 14:22:57 -0800956 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
957 iwl_rx_spectrum_measure_notif;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800958 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700959 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800960 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800961 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700962
Ben Cahill9fbab512007-11-29 11:09:47 +0800963 /*
964 * The same handler is used for both the REPLY to a discrete
965 * statistics request from the host as well as for the periodic
966 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700967 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800968 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
969 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700970
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800971 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800972 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700973
Ben Cahill9fbab512007-11-29 11:09:47 +0800974 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800975 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700976}
977
Zhu Yib481de92007-09-25 17:54:57 -0700978/************************** RX-FUNCTIONS ****************************/
979/*
980 * Rx theory of operation
981 *
982 * The host allocates 32 DMA target addresses and passes the host address
983 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
984 * 0 to 31
985 *
986 * Rx Queue Indexes
987 * The host/firmware share two index registers for managing the Rx buffers.
988 *
989 * The READ index maps to the first position that the firmware may be writing
990 * to -- the driver can read up to (but not including) this position and get
991 * good data.
992 * The READ index is managed by the firmware once the card is enabled.
993 *
994 * The WRITE index maps to the last position the driver has read from -- the
995 * position preceding WRITE is the last slot the firmware can place a packet.
996 *
997 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
998 * WRITE = READ.
999 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001000 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07001001 * INDEX position, and WRITE to the last (READ - 1 wrapped)
1002 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001003 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07001004 * and fire the RX interrupt. The driver can then query the READ index and
1005 * process as many packets as possible, moving the WRITE index forward as it
1006 * resets the Rx queue buffers with new memory.
1007 *
1008 * The management in the driver is as follows:
1009 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
1010 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08001011 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001012 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07001013 * iwl->rxq is replenished and the READ INDEX is updated (updating the
1014 * 'processed' and 'read' driver indexes as well)
1015 * + A received packet is processed and handed to the kernel network stack,
1016 * detached from the iwl->rxq. The driver 'processed' index is updated.
1017 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1018 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1019 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
1020 * were enough free buffers and RX_STALLED is set it is cleared.
1021 *
1022 *
1023 * Driver sequence:
1024 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001025 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001026 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08001027 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07001028 * queue, updates firmware pointers, and updates
1029 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001030 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07001031 *
1032 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001033 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07001034 * READ INDEX, detaching the SKB from the pool.
1035 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001036 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07001037 * slots.
1038 * ...
1039 *
1040 */
1041
1042/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001043 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07001044 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001045static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001046 dma_addr_t dma_addr)
1047{
1048 return cpu_to_le32((u32)dma_addr);
1049}
1050
1051/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001052 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07001053 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001054 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07001055 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08001056 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07001057 *
1058 * This moves the 'write' index forward to catch up with 'processed', and
1059 * also updates the memory address in the firmware to reference the new
1060 * target buffer.
1061 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001062static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001063{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001064 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001065 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001066 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001067 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001068 int write;
Zhu Yib481de92007-09-25 17:54:57 -07001069
1070 spin_lock_irqsave(&rxq->lock, flags);
1071 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001072 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001073 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001074 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001075 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001076 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001077
1078 /* Point to Rx buffer via next RBD in circular buffer */
Zhu Yi2f301222009-10-09 17:19:45 +08001079 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
Zhu Yib481de92007-09-25 17:54:57 -07001080 rxq->queue[rxq->write] = rxb;
1081 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1082 rxq->free_count--;
1083 }
1084 spin_unlock_irqrestore(&rxq->lock, flags);
1085 /* If the pre-allocated buffer pool is dropping low, schedule to
1086 * refill it */
1087 if (rxq->free_count <= RX_LOW_WATERMARK)
1088 queue_work(priv->workqueue, &priv->rx_replenish);
1089
1090
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001091 /* If we've added more space for the firmware to place data, tell it.
1092 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001093 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001094 || (abs(rxq->write - rxq->read) > 7)) {
1095 spin_lock_irqsave(&rxq->lock, flags);
1096 rxq->need_update = 1;
1097 spin_unlock_irqrestore(&rxq->lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001098 iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001099 }
Zhu Yib481de92007-09-25 17:54:57 -07001100}
1101
1102/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001103 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001104 *
1105 * When moving to rx_free an SKB is allocated for the slot.
1106 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001107 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001108 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001109 */
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001110static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001111{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001112 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001113 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001114 struct iwl_rx_mem_buffer *rxb;
Zhu Yi2f301222009-10-09 17:19:45 +08001115 struct page *page;
Zhu Yib481de92007-09-25 17:54:57 -07001116 unsigned long flags;
Zhu Yi29b1b262009-10-23 13:42:25 -07001117 gfp_t gfp_mask = priority;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001118
1119 while (1) {
1120 spin_lock_irqsave(&rxq->lock, flags);
1121
1122 if (list_empty(&rxq->rx_used)) {
1123 spin_unlock_irqrestore(&rxq->lock, flags);
1124 return;
1125 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001126 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001127
Reinette Chatref82a9242009-09-17 10:43:56 -07001128 if (rxq->free_count > RX_LOW_WATERMARK)
Zhu Yi29b1b262009-10-23 13:42:25 -07001129 gfp_mask |= __GFP_NOWARN;
Zhu Yi2f301222009-10-09 17:19:45 +08001130
1131 if (priv->hw_params.rx_page_order > 0)
Zhu Yi29b1b262009-10-23 13:42:25 -07001132 gfp_mask |= __GFP_COMP;
Zhu Yi2f301222009-10-09 17:19:45 +08001133
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001134 /* Alloc a new receive buffer */
Zhu Yi29b1b262009-10-23 13:42:25 -07001135 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
Zhu Yi2f301222009-10-09 17:19:45 +08001136 if (!page) {
Zhu Yib481de92007-09-25 17:54:57 -07001137 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001138 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1139 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1140 net_ratelimit())
1141 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1142 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1143 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001144 /* We don't reschedule replenish work here -- we will
1145 * call the restock method and if it still needs
1146 * more buffers it will schedule replenish */
1147 break;
1148 }
Zhu Yi12342c42007-12-20 11:27:32 +08001149
Reinette Chatrede0bd502009-09-11 10:38:12 -07001150 spin_lock_irqsave(&rxq->lock, flags);
1151 if (list_empty(&rxq->rx_used)) {
1152 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001153 __free_pages(page, priv->hw_params.rx_page_order);
Reinette Chatrede0bd502009-09-11 10:38:12 -07001154 return;
1155 }
1156 element = rxq->rx_used.next;
1157 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1158 list_del(element);
1159 spin_unlock_irqrestore(&rxq->lock, flags);
1160
Zhu Yi2f301222009-10-09 17:19:45 +08001161 rxb->page = page;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001162 /* Get physical address of RB/SKB */
Zhu Yi2f301222009-10-09 17:19:45 +08001163 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1164 PAGE_SIZE << priv->hw_params.rx_page_order,
1165 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001166
1167 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001168
Zhu Yib481de92007-09-25 17:54:57 -07001169 list_add_tail(&rxb->list, &rxq->rx_free);
1170 rxq->free_count++;
Zhu Yi2f301222009-10-09 17:19:45 +08001171 priv->alloc_rxb_page++;
1172
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001173 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001174 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001175}
1176
Reinette Chatredf833b12009-04-21 10:55:48 -07001177void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1178{
1179 unsigned long flags;
1180 int i;
1181 spin_lock_irqsave(&rxq->lock, flags);
1182 INIT_LIST_HEAD(&rxq->rx_free);
1183 INIT_LIST_HEAD(&rxq->rx_used);
1184 /* Fill the rx_used queue with _all_ of the Rx buffers */
1185 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1186 /* In the reset function, these buffers may have been allocated
1187 * to an SKB, so we need to unmap and free potential storage */
Zhu Yi2f301222009-10-09 17:19:45 +08001188 if (rxq->pool[i].page != NULL) {
1189 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1190 PAGE_SIZE << priv->hw_params.rx_page_order,
1191 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001192 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001193 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001194 }
1195 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1196 }
1197
1198 /* Set us so that we have processed and used all buffers, but have
1199 * not restocked the Rx queue with fresh buffers */
1200 rxq->read = rxq->write = 0;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001201 rxq->write_actual = 0;
Zhu Yi2f301222009-10-09 17:19:45 +08001202 rxq->free_count = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001203 spin_unlock_irqrestore(&rxq->lock, flags);
1204}
Reinette Chatredf833b12009-04-21 10:55:48 -07001205
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001206void iwl3945_rx_replenish(void *data)
1207{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001208 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001209 unsigned long flags;
1210
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001211 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001212
1213 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001214 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001215 spin_unlock_irqrestore(&priv->lock, flags);
1216}
1217
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001218static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1219{
1220 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1221
1222 iwl3945_rx_queue_restock(priv);
1223}
1224
1225
Reinette Chatredf833b12009-04-21 10:55:48 -07001226/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1227 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1228 * This free routine walks the list of POOL entries and if SKB is set to
1229 * non NULL it is unmapped and freed
1230 */
1231static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1232{
1233 int i;
1234 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
Zhu Yi2f301222009-10-09 17:19:45 +08001235 if (rxq->pool[i].page != NULL) {
1236 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1237 PAGE_SIZE << priv->hw_params.rx_page_order,
1238 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001239 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001240 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001241 }
1242 }
1243
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001244 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
1245 rxq->dma_addr);
1246 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1247 rxq->rb_stts, rxq->rb_stts_dma);
Reinette Chatredf833b12009-04-21 10:55:48 -07001248 rxq->bd = NULL;
1249 rxq->rb_stts = NULL;
1250}
Reinette Chatredf833b12009-04-21 10:55:48 -07001251
1252
Zhu Yib481de92007-09-25 17:54:57 -07001253/* Convert linear signal-to-noise ratio into dB */
1254static u8 ratio2dB[100] = {
1255/* 0 1 2 3 4 5 6 7 8 9 */
1256 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1257 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1258 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1259 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1260 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1261 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1262 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1263 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1264 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1265 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1266};
1267
1268/* Calculates a relative dB value from a ratio of linear
1269 * (i.e. not dB) signal levels.
1270 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001271int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001272{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001273 /* 1000:1 or higher just report as 60 dB */
1274 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001275 return 60;
1276
Adrian Bunk221c80c2008-02-02 23:19:01 +02001277 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001278 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001279 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001280 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001281
1282 /* We shouldn't see this */
1283 if (sig_ratio < 1)
1284 return 0;
1285
1286 /* Use table for ratios 1:1 - 99:1 */
1287 return (int)ratio2dB[sig_ratio];
1288}
1289
Zhu Yib481de92007-09-25 17:54:57 -07001290/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001291 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001292 *
1293 * Uses the priv->rx_handlers callback function array to invoke
1294 * the appropriate handlers, including command responses,
1295 * frame-received notifications, and other notifications.
1296 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001297static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001298{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001299 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001300 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001301 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001302 u32 r, i;
1303 int reclaim;
1304 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001305 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001306 u32 count = 8;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001307 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001308
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001309 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1310 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001311 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001312 i = rxq->read;
1313
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001314 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -07001315 total_empty = r - rxq->write_actual;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001316 if (total_empty < 0)
1317 total_empty += RX_QUEUE_SIZE;
1318
1319 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001320 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001321 /* Rx interrupt, but nothing sent from uCode */
1322 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001323 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001324
1325 while (i != r) {
1326 rxb = rxq->queue[i];
1327
Ben Cahill9fbab512007-11-29 11:09:47 +08001328 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001329 * then a bug has been introduced in the queue refilling
1330 * routines -- catch it here */
1331 BUG_ON(rxb == NULL);
1332
1333 rxq->queue[i] = NULL;
1334
Zhu Yi2f301222009-10-09 17:19:45 +08001335 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1336 PAGE_SIZE << priv->hw_params.rx_page_order,
1337 PCI_DMA_FROMDEVICE);
1338 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001339
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001340 trace_iwlwifi_dev_rx(priv, pkt,
1341 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK);
1342
Zhu Yib481de92007-09-25 17:54:57 -07001343 /* Reclaim a command buffer only if this packet is a response
1344 * to a (driver-originated) command.
1345 * If the packet (e.g. Rx frame) originated from uCode,
1346 * there is no command buffer to reclaim.
1347 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1348 * but apparently a few don't get set; catch them here. */
1349 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1350 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1351 (pkt->hdr.cmd != REPLY_TX);
1352
1353 /* Based on type of command response or notification,
1354 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001355 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001356 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001357 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001358 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001359 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -07001360 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001361 } else {
1362 /* No handling needed */
Zhu Yi2f301222009-10-09 17:19:45 +08001363 IWL_DEBUG_RX(priv,
1364 "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001365 r, i, get_cmd_string(pkt->hdr.cmd),
1366 pkt->hdr.cmd);
1367 }
1368
Zhu Yi29b1b262009-10-23 13:42:25 -07001369 /*
1370 * XXX: After here, we should always check rxb->page
1371 * against NULL before touching it or its virtual
1372 * memory (pkt). Because some rx_handler might have
1373 * already taken or freed the pages.
1374 */
1375
Zhu Yib481de92007-09-25 17:54:57 -07001376 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001377 /* Invoke any callbacks, transfer the buffer to caller,
1378 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001379 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001380 if (rxb->page)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001381 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001382 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001383 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001384 }
1385
Zhu Yi73005152009-10-23 13:42:32 -07001386 /* Reuse the page if possible. For notification packets and
1387 * SKBs that fail to Rx correctly, add them back into the
1388 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001389 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001390 if (rxb->page != NULL) {
1391 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1392 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1393 PCI_DMA_FROMDEVICE);
1394 list_add_tail(&rxb->list, &rxq->rx_free);
1395 rxq->free_count++;
1396 } else
1397 list_add_tail(&rxb->list, &rxq->rx_used);
1398
Zhu Yib481de92007-09-25 17:54:57 -07001399 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001400
Zhu Yib481de92007-09-25 17:54:57 -07001401 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001402 /* If there are a lot of unused frames,
1403 * restock the Rx queue so ucode won't assert. */
1404 if (fill_rx) {
1405 count++;
1406 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001407 rxq->read = i;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001408 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001409 count = 0;
1410 }
1411 }
Zhu Yib481de92007-09-25 17:54:57 -07001412 }
1413
1414 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001415 rxq->read = i;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001416 if (fill_rx)
1417 iwl3945_rx_replenish_now(priv);
1418 else
1419 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001420}
1421
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001422/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001423static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001424{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001425 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001426 synchronize_irq(priv->pci_dev->irq);
1427 tasklet_kill(&priv->irq_tasklet);
1428}
1429
Zhu Yib481de92007-09-25 17:54:57 -07001430static const char *desc_lookup(int i)
1431{
1432 switch (i) {
1433 case 1:
1434 return "FAIL";
1435 case 2:
1436 return "BAD_PARAM";
1437 case 3:
1438 return "BAD_CHECKSUM";
1439 case 4:
1440 return "NMI_INTERRUPT";
1441 case 5:
1442 return "SYSASSERT";
1443 case 6:
1444 return "FATAL_ERROR";
1445 }
1446
1447 return "UNKNOWN";
1448}
1449
1450#define ERROR_START_OFFSET (1 * sizeof(u32))
1451#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1452
Reinette Chatreb7a79402009-09-25 14:24:23 -07001453void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001454{
1455 u32 i;
1456 u32 desc, time, count, base, data1;
1457 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001458
1459 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1460
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001461 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001462 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001463 return;
1464 }
1465
Zhu Yib481de92007-09-25 17:54:57 -07001466
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001467 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001468
1469 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001470 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1471 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1472 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001473 }
1474
Winkler, Tomas15b16872008-12-19 10:37:33 +08001475 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001476 "ilink1 nmiPC Line\n");
1477 for (i = ERROR_START_OFFSET;
1478 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1479 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001480 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001481 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001482 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001483 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001484 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001485 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001486 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001487 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001488 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001489 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001490 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001491 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001492 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001493
Winkler, Tomas15b16872008-12-19 10:37:33 +08001494 IWL_ERR(priv,
1495 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1496 desc_lookup(desc), desc, time, blink1, blink2,
1497 ilink1, ilink2, data1);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001498 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1499 0, blink1, blink2, ilink1, ilink2);
Zhu Yib481de92007-09-25 17:54:57 -07001500 }
Zhu Yib481de92007-09-25 17:54:57 -07001501}
1502
Ben Cahillf58177b2007-11-29 11:09:43 +08001503#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001504
1505/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001506 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001507 *
Zhu Yib481de92007-09-25 17:54:57 -07001508 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001509static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1510 u32 num_events, u32 mode,
1511 int pos, char **buf, size_t bufsz)
Zhu Yib481de92007-09-25 17:54:57 -07001512{
1513 u32 i;
1514 u32 base; /* SRAM byte address of event log header */
1515 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1516 u32 ptr; /* SRAM byte address of log data */
1517 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001518 unsigned long reg_flags;
Zhu Yib481de92007-09-25 17:54:57 -07001519
1520 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001521 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001522
1523 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1524
1525 if (mode == 0)
1526 event_size = 2 * sizeof(u32);
1527 else
1528 event_size = 3 * sizeof(u32);
1529
1530 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1531
Ben Cahille5854472009-11-06 14:52:58 -08001532 /* Make sure device is powered up for SRAM reads */
1533 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1534 iwl_grab_nic_access(priv);
1535
1536 /* Set starting address; reads will auto-increment */
1537 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1538 rmb();
1539
Zhu Yib481de92007-09-25 17:54:57 -07001540 /* "time" is actually "data" for mode 0 (no timestamp).
1541 * place event id # at far right for easier visual parsing. */
1542 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001543 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1544 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001545 if (mode == 0) {
1546 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001547 if (bufsz) {
1548 pos += scnprintf(*buf + pos, bufsz - pos,
1549 "0x%08x:%04u\n",
1550 time, ev);
1551 } else {
1552 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1553 trace_iwlwifi_dev_ucode_event(priv, 0,
1554 time, ev);
1555 }
Winkler, Tomas15b16872008-12-19 10:37:33 +08001556 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001557 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001558 if (bufsz) {
1559 pos += scnprintf(*buf + pos, bufsz - pos,
1560 "%010u:0x%08x:%04u\n",
1561 time, data, ev);
1562 } else {
1563 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1564 time, data, ev);
1565 trace_iwlwifi_dev_ucode_event(priv, time,
1566 data, ev);
1567 }
Zhu Yib481de92007-09-25 17:54:57 -07001568 }
1569 }
Ben Cahille5854472009-11-06 14:52:58 -08001570
1571 /* Allow device to power down */
1572 iwl_release_nic_access(priv);
1573 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001574 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001575}
1576
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001577/**
1578 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1579 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001580static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001581 u32 num_wraps, u32 next_entry,
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001582 u32 size, u32 mode,
1583 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001584{
1585 /*
1586 * display the newest DEFAULT_LOG_ENTRIES entries
1587 * i.e the entries just before the next ont that uCode would fill.
1588 */
1589 if (num_wraps) {
1590 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001591 pos = iwl3945_print_event_log(priv,
1592 capacity - (size - next_entry),
1593 size - next_entry, mode,
1594 pos, buf, bufsz);
1595 pos = iwl3945_print_event_log(priv, 0,
1596 next_entry, mode,
1597 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001598 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001599 pos = iwl3945_print_event_log(priv, next_entry - size,
1600 size, mode,
1601 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001602 } else {
1603 if (next_entry < size)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001604 pos = iwl3945_print_event_log(priv, 0,
1605 next_entry, mode,
1606 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001607 else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001608 pos = iwl3945_print_event_log(priv, next_entry - size,
1609 size, mode,
1610 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001611 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001612 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001613}
1614
Ben Cahill84c40692009-11-06 14:52:57 -08001615/* For sanity check only. Actual size is determined by uCode, typ. 512 */
1616#define IWL3945_MAX_EVENT_LOG_SIZE (512)
1617
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001618#define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1619
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001620int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1621 char **buf, bool display)
Zhu Yib481de92007-09-25 17:54:57 -07001622{
Zhu Yib481de92007-09-25 17:54:57 -07001623 u32 base; /* SRAM byte address of event log header */
1624 u32 capacity; /* event log capacity in # entries */
1625 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1626 u32 num_wraps; /* # times uCode wrapped to top of log */
1627 u32 next_entry; /* index of next entry to be written by uCode */
1628 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001629 int pos = 0;
1630 size_t bufsz = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001631
1632 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001633 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001634 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001635 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001636 }
1637
Zhu Yib481de92007-09-25 17:54:57 -07001638 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001639 capacity = iwl_read_targ_mem(priv, base);
1640 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1641 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1642 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001643
Ben Cahill84c40692009-11-06 14:52:57 -08001644 if (capacity > IWL3945_MAX_EVENT_LOG_SIZE) {
1645 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
1646 capacity, IWL3945_MAX_EVENT_LOG_SIZE);
1647 capacity = IWL3945_MAX_EVENT_LOG_SIZE;
1648 }
1649
1650 if (next_entry > IWL3945_MAX_EVENT_LOG_SIZE) {
1651 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
1652 next_entry, IWL3945_MAX_EVENT_LOG_SIZE);
1653 next_entry = IWL3945_MAX_EVENT_LOG_SIZE;
1654 }
1655
Zhu Yib481de92007-09-25 17:54:57 -07001656 size = num_wraps ? capacity : next_entry;
1657
1658 /* bail out if nothing in log */
1659 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001660 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001661 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001662 }
1663
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001664#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001665 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001666 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1667 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1668#else
1669 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1670 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1671#endif
1672
1673 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1674 size);
Zhu Yib481de92007-09-25 17:54:57 -07001675
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001676#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001677 if (display) {
1678 if (full_log)
1679 bufsz = capacity * 48;
1680 else
1681 bufsz = size * 48;
1682 *buf = kmalloc(bufsz, GFP_KERNEL);
1683 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001684 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001685 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001686 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1687 /* if uCode has wrapped back to top of log,
1688 * start at the oldest entry,
1689 * i.e the next one that uCode would fill.
1690 */
1691 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001692 pos = iwl3945_print_event_log(priv, next_entry,
1693 capacity - next_entry, mode,
1694 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001695
1696 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001697 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1698 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001699 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001700 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1701 next_entry, size, mode,
1702 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001703#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001704 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1705 next_entry, size, mode,
1706 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001707#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001708 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001709}
1710
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001711static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001712{
1713 u32 inta, handled = 0;
1714 u32 inta_fh;
1715 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001716#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001717 u32 inta_mask;
1718#endif
1719
1720 spin_lock_irqsave(&priv->lock, flags);
1721
1722 /* Ack/clear/reset pending uCode interrupts.
1723 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1724 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001725 inta = iwl_read32(priv, CSR_INT);
1726 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001727
1728 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1729 * Any new interrupts that happen after this, either while we're
1730 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001731 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1732 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001733
Samuel Ortizd08853a2009-01-23 13:45:17 -08001734#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001735 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001736 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001737 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001738 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001739 inta, inta_mask, inta_fh);
1740 }
1741#endif
1742
Zhu Yi2f301222009-10-09 17:19:45 +08001743 spin_unlock_irqrestore(&priv->lock, flags);
1744
Zhu Yib481de92007-09-25 17:54:57 -07001745 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1746 * atomic, make sure that inta covers all the interrupts that
1747 * we've discovered, even if FH interrupt came in just after
1748 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001749 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001750 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001751 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001752 inta |= CSR_INT_BIT_FH_TX;
1753
1754 /* Now service all interrupt bits discovered above. */
1755 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001756 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001757
1758 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001759 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001760
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001761 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001762 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001763
1764 handled |= CSR_INT_BIT_HW_ERR;
1765
Zhu Yib481de92007-09-25 17:54:57 -07001766 return;
1767 }
1768
Samuel Ortizd08853a2009-01-23 13:45:17 -08001769#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001770 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001771 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001772 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001773 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001774 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001775 priv->isr_stats.sch++;
1776 }
Zhu Yib481de92007-09-25 17:54:57 -07001777
1778 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001779 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001780 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001781 priv->isr_stats.alive++;
1782 }
Zhu Yib481de92007-09-25 17:54:57 -07001783 }
1784#endif
1785 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001786 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001787
Zhu Yib481de92007-09-25 17:54:57 -07001788 /* Error detected by uCode */
1789 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001790 IWL_ERR(priv, "Microcode SW error detected. "
1791 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001792 priv->isr_stats.sw++;
1793 priv->isr_stats.sw_err = inta;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001794 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001795 handled |= CSR_INT_BIT_SW_ERR;
1796 }
1797
1798 /* uCode wakes up after power-down sleep */
1799 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001800 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001801 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001802 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1803 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1804 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1805 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1806 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1807 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001808
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001809 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001810 handled |= CSR_INT_BIT_WAKEUP;
1811 }
1812
1813 /* All uCode command responses, including Tx command responses,
1814 * Rx "responses" (frame-received notification), and other
1815 * notifications from uCode come through here*/
1816 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001817 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001818 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001819 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1820 }
1821
1822 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001823 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001824 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001825
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001826 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001827 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1828 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001829 handled |= CSR_INT_BIT_FH_TX;
1830 }
1831
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001832 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001833 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001834 priv->isr_stats.unhandled++;
1835 }
Zhu Yib481de92007-09-25 17:54:57 -07001836
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001837 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001838 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001839 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001840 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001841 }
1842
1843 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001844 /* only Re-enable if disabled by irq */
1845 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001846 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001847
Samuel Ortizd08853a2009-01-23 13:45:17 -08001848#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001849 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001850 inta = iwl_read32(priv, CSR_INT);
1851 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1852 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001853 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001854 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1855 }
1856#endif
Zhu Yib481de92007-09-25 17:54:57 -07001857}
1858
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001859static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001860 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001861 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001862 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07001863{
Johannes Berg4e05c232009-06-19 13:52:44 -07001864 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001865 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001866 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001867 u16 passive_dwell = 0;
1868 u16 active_dwell = 0;
1869 int added, i;
1870
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001871 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001872 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001873 return 0;
1874
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001875 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
1876 passive_dwell = iwl_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07001877
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001878 if (passive_dwell <= active_dwell)
1879 passive_dwell = active_dwell + 1;
1880
Johannes Berg4e05c232009-06-19 13:52:44 -07001881 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1882 chan = priv->scan_request->channels[i];
1883
1884 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001885 continue;
1886
Johannes Berg4e05c232009-06-19 13:52:44 -07001887 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001888
Samuel Ortize6148912009-01-23 13:45:15 -08001889 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001890 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001891 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001892 scan_ch->channel);
1893 continue;
1894 }
1895
Zhu Yib481de92007-09-25 17:54:57 -07001896 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1897 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001898 /* If passive , set up for auto-switch
1899 * and use long active_dwell time.
1900 */
1901 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001902 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001903 scan_ch->type = 0; /* passive */
1904 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1905 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1906 } else {
1907 scan_ch->type = 1; /* active */
1908 }
1909
1910 /* Set direct probe bits. These may be used both for active
1911 * scan channels (probes gets sent right away),
1912 * or for passive channels (probes get se sent only after
1913 * hearing clear Rx packet).*/
1914 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1915 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001916 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001917 } else {
1918 /* uCode v1 does not allow setting direct probe bits on
1919 * passive channel. */
1920 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001921 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001922 }
Zhu Yib481de92007-09-25 17:54:57 -07001923
Ben Cahill9fbab512007-11-29 11:09:47 +08001924 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001925 scan_ch->tpc.dsp_atten = 110;
1926 /* scan_pwr_info->tpc.dsp_atten; */
1927
1928 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001929 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001930 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1931 else {
1932 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1933 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001934 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001935 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001936 */
1937 }
1938
Tomas Winklere1623442009-01-27 14:27:56 -08001939 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001940 scan_ch->channel,
1941 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1942 (scan_ch->type & 1) ?
1943 active_dwell : passive_dwell);
1944
1945 scan_ch++;
1946 added++;
1947 }
1948
Tomas Winklere1623442009-01-27 14:27:56 -08001949 IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001950 return added;
1951}
1952
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001953static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001954 struct ieee80211_rate *rates)
1955{
1956 int i;
1957
1958 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001959 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1960 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1961 rates[i].hw_value_short = i;
1962 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001963 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001964 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001965 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001966 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001967 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001968 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001969 }
Zhu Yib481de92007-09-25 17:54:57 -07001970 }
1971}
1972
Zhu Yib481de92007-09-25 17:54:57 -07001973/******************************************************************************
1974 *
1975 * uCode download functions
1976 *
1977 ******************************************************************************/
1978
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001979static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001980{
Tomas Winkler98c92212008-01-14 17:46:20 -08001981 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1982 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1983 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1984 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1985 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1986 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001987}
1988
1989/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001990 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001991 * looking at all data.
1992 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001993static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001994{
1995 u32 val;
1996 u32 save_len = len;
1997 int rc = 0;
1998 u32 errcnt;
1999
Tomas Winklere1623442009-01-27 14:27:56 -08002000 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002001
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002002 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002003 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07002004
2005 errcnt = 0;
2006 for (; len > 0; len -= sizeof(u32), image++) {
2007 /* read data comes through single port, auto-incr addr */
2008 /* NOTE: Use the debugless read so we don't flood kernel log
2009 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002010 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002011 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002012 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002013 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2014 save_len - len, val, le32_to_cpu(*image));
2015 rc = -EIO;
2016 errcnt++;
2017 if (errcnt >= 20)
2018 break;
2019 }
2020 }
2021
Zhu Yib481de92007-09-25 17:54:57 -07002022
2023 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002024 IWL_DEBUG_INFO(priv,
2025 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002026
2027 return rc;
2028}
2029
2030
2031/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002032 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002033 * using sample data 100 bytes apart. If these sample points are good,
2034 * it's a pretty good bet that everything between them is good, too.
2035 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002036static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002037{
2038 u32 val;
2039 int rc = 0;
2040 u32 errcnt = 0;
2041 u32 i;
2042
Tomas Winklere1623442009-01-27 14:27:56 -08002043 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002044
Zhu Yib481de92007-09-25 17:54:57 -07002045 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2046 /* read data comes through single port, auto-incr addr */
2047 /* NOTE: Use the debugless read so we don't flood kernel log
2048 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002049 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002050 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002051 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002052 if (val != le32_to_cpu(*image)) {
2053#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002054 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002055 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2056 i, val, *image);
2057#endif
2058 rc = -EIO;
2059 errcnt++;
2060 if (errcnt >= 3)
2061 break;
2062 }
2063 }
2064
Zhu Yib481de92007-09-25 17:54:57 -07002065 return rc;
2066}
2067
2068
2069/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002070 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002071 * and verify its contents
2072 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002073static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002074{
2075 __le32 *image;
2076 u32 len;
2077 int rc = 0;
2078
2079 /* Try bootstrap */
2080 image = (__le32 *)priv->ucode_boot.v_addr;
2081 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002082 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002083 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002084 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002085 return 0;
2086 }
2087
2088 /* Try initialize */
2089 image = (__le32 *)priv->ucode_init.v_addr;
2090 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002091 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002092 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002093 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002094 return 0;
2095 }
2096
2097 /* Try runtime/protocol */
2098 image = (__le32 *)priv->ucode_code.v_addr;
2099 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002100 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002101 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002102 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002103 return 0;
2104 }
2105
Winkler, Tomas15b16872008-12-19 10:37:33 +08002106 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002107
Ben Cahill9fbab512007-11-29 11:09:47 +08002108 /* Since nothing seems to match, show first several data entries in
2109 * instruction SRAM, so maybe visual inspection will give a clue.
2110 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002111 image = (__le32 *)priv->ucode_boot.v_addr;
2112 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002113 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002114
2115 return rc;
2116}
2117
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002118static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002119{
2120 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002121 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002122}
2123
2124/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002125 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002126 *
2127 * Copy into buffers for card to fetch via bus-mastering
2128 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002129static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002130{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002131 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002132 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002133 const struct firmware *ucode_raw;
2134 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002135 const char *name_pre = priv->cfg->fw_name_pre;
2136 const unsigned int api_max = priv->cfg->ucode_api_max;
2137 const unsigned int api_min = priv->cfg->ucode_api_min;
2138 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002139 u8 *src;
2140 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002141 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002142
2143 /* Ask kernel firmware_class module to get the boot firmware off disk.
2144 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002145 for (index = api_max; index >= api_min; index--) {
2146 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2147 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2148 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002149 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002150 buf, ret);
2151 if (ret == -ENOENT)
2152 continue;
2153 else
2154 goto error;
2155 } else {
2156 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002157 IWL_ERR(priv, "Loaded firmware %s, "
2158 "which is deprecated. "
2159 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002160 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002161 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2162 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002163 buf, ucode_raw->size);
2164 break;
2165 }
Zhu Yib481de92007-09-25 17:54:57 -07002166 }
2167
Reinette Chatrea0987a82008-12-02 12:14:06 -08002168 if (ret < 0)
2169 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002170
2171 /* Make sure that we got at least our header! */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002172 if (ucode_raw->size < priv->cfg->ops->ucode->get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002173 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002174 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002175 goto err_release;
2176 }
2177
2178 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002179 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002180
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002181 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002182 api_ver = IWL_UCODE_API(priv->ucode_ver);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002183 inst_size = priv->cfg->ops->ucode->get_inst_size(ucode, api_ver);
2184 data_size = priv->cfg->ops->ucode->get_data_size(ucode, api_ver);
2185 init_size = priv->cfg->ops->ucode->get_init_size(ucode, api_ver);
2186 init_data_size =
2187 priv->cfg->ops->ucode->get_init_data_size(ucode, api_ver);
2188 boot_size = priv->cfg->ops->ucode->get_boot_size(ucode, api_ver);
2189 src = priv->cfg->ops->ucode->get_data(ucode, api_ver);
Zhu Yib481de92007-09-25 17:54:57 -07002190
Reinette Chatrea0987a82008-12-02 12:14:06 -08002191 /* api_ver should match the api version forming part of the
2192 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002193 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002194
2195 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002196 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002197 "Driver supports v%u, firmware is v%u.\n",
2198 api_max, api_ver);
2199 priv->ucode_ver = 0;
2200 ret = -EINVAL;
2201 goto err_release;
2202 }
2203 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002204 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002205 "got %u. New firmware can be obtained "
2206 "from http://www.intellinuxwireless.org.\n",
2207 api_max, api_ver);
2208
Tomas Winkler978785a2008-12-19 10:37:31 +08002209 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2210 IWL_UCODE_MAJOR(priv->ucode_ver),
2211 IWL_UCODE_MINOR(priv->ucode_ver),
2212 IWL_UCODE_API(priv->ucode_ver),
2213 IWL_UCODE_SERIAL(priv->ucode_ver));
2214
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07002215 snprintf(priv->hw->wiphy->fw_version,
2216 sizeof(priv->hw->wiphy->fw_version),
2217 "%u.%u.%u.%u",
2218 IWL_UCODE_MAJOR(priv->ucode_ver),
2219 IWL_UCODE_MINOR(priv->ucode_ver),
2220 IWL_UCODE_API(priv->ucode_ver),
2221 IWL_UCODE_SERIAL(priv->ucode_ver));
2222
Tomas Winklere1623442009-01-27 14:27:56 -08002223 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002224 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002225 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2226 inst_size);
2227 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2228 data_size);
2229 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2230 init_size);
2231 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2232 init_data_size);
2233 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2234 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002235
Reinette Chatrea0987a82008-12-02 12:14:06 -08002236
Zhu Yib481de92007-09-25 17:54:57 -07002237 /* Verify size of file vs. image size info in file's header */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002238 if (ucode_raw->size != priv->cfg->ops->ucode->get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002239 inst_size + data_size + init_size +
2240 init_data_size + boot_size) {
2241
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002242 IWL_DEBUG_INFO(priv,
2243 "uCode file size %zd does not match expected size\n",
2244 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002245 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002246 goto err_release;
2247 }
2248
2249 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002250 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002251 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002252 inst_size);
2253 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002254 goto err_release;
2255 }
2256
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002257 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002258 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002259 data_size);
2260 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002261 goto err_release;
2262 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002263 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002264 IWL_DEBUG_INFO(priv,
2265 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002266 init_size);
2267 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002268 goto err_release;
2269 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002270 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002271 IWL_DEBUG_INFO(priv,
2272 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002273 init_data_size);
2274 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002275 goto err_release;
2276 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002277 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002278 IWL_DEBUG_INFO(priv,
2279 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002280 boot_size);
2281 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002282 goto err_release;
2283 }
2284
2285 /* Allocate ucode buffers for card's bus-master loading ... */
2286
2287 /* Runtime instructions and 2 copies of data:
2288 * 1) unmodified from disk
2289 * 2) backup cache for save/restore during power-downs */
2290 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002291 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002292
2293 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002294 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002295
2296 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002297 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002298
2299 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002300 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002301 goto err_pci_alloc;
2302
Tomas Winkler90e759d2007-11-29 11:09:41 +08002303 /* Initialization instructions and data */
2304 if (init_size && init_data_size) {
2305 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002306 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002307
2308 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002309 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002310
2311 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2312 goto err_pci_alloc;
2313 }
2314
2315 /* Bootstrap (instructions only, no data) */
2316 if (boot_size) {
2317 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002318 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002319
2320 if (!priv->ucode_boot.v_addr)
2321 goto err_pci_alloc;
2322 }
2323
Zhu Yib481de92007-09-25 17:54:57 -07002324 /* Copy images into buffers for card's bus-master reads ... */
2325
2326 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002327 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002328 IWL_DEBUG_INFO(priv,
2329 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002330 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002331 src += len;
2332
Tomas Winklere1623442009-01-27 14:27:56 -08002333 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002334 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2335
2336 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002337 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002338 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002339 IWL_DEBUG_INFO(priv,
2340 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002341 memcpy(priv->ucode_data.v_addr, src, len);
2342 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002343 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002344
2345 /* Initialization instructions (3rd block) */
2346 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002347 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002348 IWL_DEBUG_INFO(priv,
2349 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002350 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002351 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002352 }
2353
2354 /* Initialization data (4th block) */
2355 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002356 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002357 IWL_DEBUG_INFO(priv,
2358 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002359 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002360 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002361 }
2362
2363 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002364 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002365 IWL_DEBUG_INFO(priv,
2366 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002367 memcpy(priv->ucode_boot.v_addr, src, len);
2368
2369 /* We have our copies now, allow OS release its copies */
2370 release_firmware(ucode_raw);
2371 return 0;
2372
2373 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002374 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002375 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002376 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002377
2378 err_release:
2379 release_firmware(ucode_raw);
2380
2381 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002382 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002383}
2384
2385
2386/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002387 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002388 *
2389 * Tell initialization uCode where to find runtime uCode.
2390 *
2391 * BSM registers initially contain pointers to initialization uCode.
2392 * We need to replace them to load runtime uCode inst and data,
2393 * and to save runtime data when powering down.
2394 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002395static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002396{
2397 dma_addr_t pinst;
2398 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002399
2400 /* bits 31:0 for 3945 */
2401 pinst = priv->ucode_code.p_addr;
2402 pdata = priv->ucode_data_backup.p_addr;
2403
Zhu Yib481de92007-09-25 17:54:57 -07002404 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002405 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2406 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2407 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002408 priv->ucode_data.len);
2409
Tomas Winklera96a27f2008-10-23 23:48:56 -07002410 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002411 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002412 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002413 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2414
Tomas Winklere1623442009-01-27 14:27:56 -08002415 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002416
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002417 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002418}
2419
2420/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002421 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002422 *
2423 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2424 *
Zhu Yib481de92007-09-25 17:54:57 -07002425 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002426 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002427static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002428{
2429 /* Check alive response for "valid" sign from uCode */
2430 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2431 /* We had an error bringing up the hardware, so take it
2432 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002433 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002434 goto restart;
2435 }
2436
2437 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2438 * This is a paranoid check, because we would not have gotten the
2439 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002440 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002441 /* Runtime instruction load was bad;
2442 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002443 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002444 goto restart;
2445 }
2446
2447 /* Send pointers to protocol/runtime uCode image ... init code will
2448 * load and launch runtime uCode, which will send us another "Alive"
2449 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002450 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002451 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002452 /* Runtime instruction load won't happen;
2453 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002454 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002455 goto restart;
2456 }
2457 return;
2458
2459 restart:
2460 queue_work(priv->workqueue, &priv->restart);
2461}
2462
Zhu Yib481de92007-09-25 17:54:57 -07002463/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002464 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002465 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002466 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002467 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002468static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002469{
Zhu Yib481de92007-09-25 17:54:57 -07002470 int thermal_spin = 0;
2471 u32 rfkill;
2472
Tomas Winklere1623442009-01-27 14:27:56 -08002473 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002474
2475 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2476 /* We had an error bringing up the hardware, so take it
2477 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002478 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002479 goto restart;
2480 }
2481
2482 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2483 * This is a paranoid check, because we would not have gotten the
2484 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002485 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002486 /* Runtime instruction load was bad;
2487 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002488 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002489 goto restart;
2490 }
2491
Tomas Winklerc587de02009-06-03 11:44:07 -07002492 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002493
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002494 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002495 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002496
2497 if (rfkill & 0x1) {
2498 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002499 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002500 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002501 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002502 thermal_spin++;
2503 udelay(10);
2504 }
2505
2506 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002507 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002508 thermal_spin * 10);
2509 } else
2510 set_bit(STATUS_RF_KILL_HW, &priv->status);
2511
Ben Cahill9fbab512007-11-29 11:09:47 +08002512 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002513 set_bit(STATUS_ALIVE, &priv->status);
2514
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002515 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002516 return;
2517
Johannes Berg36d68252008-05-15 12:55:26 +02002518 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002519
2520 priv->active_rate = priv->rates_mask;
2521 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2522
Ben Cahill4d6ccbf2009-11-13 11:56:29 -08002523 iwl_power_update_mode(priv, true);
Zhu Yib481de92007-09-25 17:54:57 -07002524
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002525 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002526 struct iwl3945_rxon_cmd *active_rxon =
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002527 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07002528
Abhijeet Kolekar8a9b9922009-06-12 13:22:52 -07002529 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002530 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2531 } else {
2532 /* Initialize our rx_config data */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002533 iwl_connection_init_rx_config(priv, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002534 }
2535
Ben Cahill9fbab512007-11-29 11:09:47 +08002536 /* Configure Bluetooth device coexistence support */
Samuel Ortiz17f841c2009-01-23 13:45:20 -08002537 iwl_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002538
2539 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002540 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002541
Zhu Yib481de92007-09-25 17:54:57 -07002542 iwl3945_reg_txpower_periodic(priv);
2543
Johannes Berge932a602009-10-02 13:44:03 -07002544 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002545
Tomas Winklere1623442009-01-27 14:27:56 -08002546 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002547 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002548 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002549
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002550 /* reassociate for ADHOC mode */
2551 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2552 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2553 priv->vif);
2554 if (beacon)
Abhijeet Kolekar9944b932009-04-08 11:26:36 -07002555 iwl_mac_beacon_update(priv->hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08002556 }
2557
Abhijeet Kolekarf45c2712009-04-08 11:26:38 -07002558 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
Abhijeet Kolekar727882d2009-04-08 11:26:45 -07002559 iwl_set_mode(priv, priv->iw_mode);
Abhijeet Kolekarf45c2712009-04-08 11:26:38 -07002560
Zhu Yib481de92007-09-25 17:54:57 -07002561 return;
2562
2563 restart:
2564 queue_work(priv->workqueue, &priv->restart);
2565}
2566
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002567static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002568
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002569static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002570{
2571 unsigned long flags;
2572 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2573 struct ieee80211_conf *conf = NULL;
2574
Tomas Winklere1623442009-01-27 14:27:56 -08002575 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002576
2577 conf = ieee80211_get_hw_conf(priv->hw);
2578
2579 if (!exit_pending)
2580 set_bit(STATUS_EXIT_PENDING, &priv->status);
2581
Tomas Winklerc587de02009-06-03 11:44:07 -07002582 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002583
2584 /* Unblock any waiting calls */
2585 wake_up_interruptible_all(&priv->wait_command_queue);
2586
Zhu Yib481de92007-09-25 17:54:57 -07002587 /* Wipe out the EXIT_PENDING status bit if we are not actually
2588 * exiting the module */
2589 if (!exit_pending)
2590 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2591
2592 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002593 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002594
2595 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002596 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002597 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002598 spin_unlock_irqrestore(&priv->lock, flags);
2599 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002600
2601 if (priv->mac80211_registered)
2602 ieee80211_stop_queues(priv->hw);
2603
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002604 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002605 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002606 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002607 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2608 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002609 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2610 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002611 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2612 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002613 goto exit;
2614 }
2615
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002616 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002617 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002618 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2619 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002620 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2621 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002622 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002623 STATUS_FW_ERROR |
2624 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2625 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002626
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002627 iwl3945_hw_txq_ctx_stop(priv);
2628 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002629
Ben Cahill309e7312009-11-06 14:53:03 -08002630 /* Power-down device's busmaster DMA clocks */
2631 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002632 udelay(5);
2633
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002634 /* Stop the device, and put it in low power state */
2635 priv->cfg->ops->lib->apm_ops.stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002636
Zhu Yib481de92007-09-25 17:54:57 -07002637 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002638 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002639
2640 if (priv->ibss_beacon)
2641 dev_kfree_skb(priv->ibss_beacon);
2642 priv->ibss_beacon = NULL;
2643
2644 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002645 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002646}
2647
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002648static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002649{
2650 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002651 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002652 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002653
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002654 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002655}
2656
2657#define MAX_HW_RESTARTS 5
2658
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002659static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002660{
2661 int rc, i;
2662
2663 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002664 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002665 return -EIO;
2666 }
2667
Reinette Chatree903fbd2008-01-30 22:05:15 -08002668 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002669 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002670 return -EIO;
2671 }
2672
Zhu Yie655b9f2008-01-24 02:19:38 -08002673 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002674 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002675 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2676 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2677 else {
2678 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002679 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2680 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002681 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002682
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002683 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002685 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002686 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002687 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002688 return rc;
2689 }
2690
2691 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002692 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2693 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002694 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2695
2696 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002697 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002698 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002699
2700 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002701 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2702 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002703
2704 /* Copy original ucode data image from disk into backup cache.
2705 * This will be used to initialize the on-board processor's
2706 * data SRAM for a clean start when the runtime program first loads. */
2707 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08002708 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002709
Zhu Yie655b9f2008-01-24 02:19:38 -08002710 /* We return success when we resume from suspend and rf_kill is on. */
2711 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2712 return 0;
2713
Zhu Yib481de92007-09-25 17:54:57 -07002714 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2715
Tomas Winklerc587de02009-06-03 11:44:07 -07002716 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002717
2718 /* load bootstrap state machine,
2719 * load bootstrap program into processor's memory,
2720 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08002721 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002722
2723 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002724 IWL_ERR(priv,
2725 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002726 continue;
2727 }
2728
2729 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002730 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002731
Tomas Winklere1623442009-01-27 14:27:56 -08002732 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002733
2734 return 0;
2735 }
2736
2737 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002738 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002739 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002740
2741 /* tried to restart and config the device for as long as our
2742 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002743 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002744 return -EIO;
2745}
2746
2747
2748/*****************************************************************************
2749 *
2750 * Workqueue callbacks
2751 *
2752 *****************************************************************************/
2753
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002754static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002755{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002756 struct iwl_priv *priv =
2757 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002758
2759 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2760 return;
2761
2762 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002763 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002764 mutex_unlock(&priv->mutex);
2765}
2766
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002767static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002768{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002769 struct iwl_priv *priv =
2770 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002771
2772 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2773 return;
2774
2775 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002776 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002777 mutex_unlock(&priv->mutex);
2778}
2779
Ben Cahill743cdf12009-10-09 13:20:26 -07002780/*
2781 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2782 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2783 * *is* readable even when device has been SW_RESET into low power mode
2784 * (e.g. during RF KILL).
2785 */
Helmut Schaa26635162009-01-15 09:38:44 +01002786static void iwl3945_rfkill_poll(struct work_struct *data)
2787{
2788 struct iwl_priv *priv =
2789 container_of(data, struct iwl_priv, rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002790 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2791 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2792 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002793
Ben Cahill743cdf12009-10-09 13:20:26 -07002794 if (new_rfkill != old_rfkill) {
2795 if (new_rfkill)
2796 set_bit(STATUS_RF_KILL_HW, &priv->status);
2797 else
2798 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002799
Ben Cahill743cdf12009-10-09 13:20:26 -07002800 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002801
Ben Cahill743cdf12009-10-09 13:20:26 -07002802 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2803 new_rfkill ? "disable radio" : "enable radio");
2804 }
2805
2806 /* Keep this running, even if radio now enabled. This will be
2807 * cancelled in mac_start() if system decides to start again */
Helmut Schaa26635162009-01-15 09:38:44 +01002808 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
2809 round_jiffies_relative(2 * HZ));
2810
2811}
2812
Zhu Yib481de92007-09-25 17:54:57 -07002813#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002814static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002815{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002816 struct iwl_priv *priv =
2817 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002818 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002819 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002820 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002821 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002822 };
2823 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002824 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07002825 struct ieee80211_conf *conf = NULL;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002826 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002827 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002828 bool is_active = false;
Zhu Yib481de92007-09-25 17:54:57 -07002829
2830 conf = ieee80211_get_hw_conf(priv->hw);
2831
2832 mutex_lock(&priv->mutex);
2833
Reinette Chatrefbc9f972009-05-15 16:13:46 -07002834 cancel_delayed_work(&priv->scan_check);
2835
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002836 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002837 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002838 goto done;
2839 }
2840
Tomas Winklera96a27f2008-10-23 23:48:56 -07002841 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07002842 * was given the chance to run... */
2843 if (!test_bit(STATUS_SCANNING, &priv->status))
2844 goto done;
2845
2846 /* This should never be called or scheduled if there is currently
2847 * a scan active in the hardware. */
2848 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002849 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
2850 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002851 rc = -EIO;
2852 goto done;
2853 }
2854
2855 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002856 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07002857 goto done;
2858 }
2859
2860 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002861 IWL_DEBUG_HC(priv,
2862 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002863 goto done;
2864 }
2865
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002866 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002867 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07002868 goto done;
2869 }
2870
2871 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002872 IWL_DEBUG_HC(priv,
2873 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002874 goto done;
2875 }
2876
2877 if (!priv->scan_bands) {
Tomas Winklere1623442009-01-27 14:27:56 -08002878 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
Zhu Yib481de92007-09-25 17:54:57 -07002879 goto done;
2880 }
2881
Winkler, Tomas805cee52009-01-19 15:30:28 -08002882 if (!priv->scan) {
2883 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07002884 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Winkler, Tomas805cee52009-01-19 15:30:28 -08002885 if (!priv->scan) {
Zhu Yib481de92007-09-25 17:54:57 -07002886 rc = -ENOMEM;
2887 goto done;
2888 }
2889 }
Winkler, Tomas805cee52009-01-19 15:30:28 -08002890 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002891 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002892
2893 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2894 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2895
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002896 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002897 u16 interval = 0;
2898 u32 extra;
2899 u32 suspend_time = 100;
2900 u32 scan_suspend_time = 100;
2901 unsigned long flags;
2902
Tomas Winklere1623442009-01-27 14:27:56 -08002903 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002904
2905 spin_lock_irqsave(&priv->lock, flags);
2906 interval = priv->beacon_int;
2907 spin_unlock_irqrestore(&priv->lock, flags);
2908
2909 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002910 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002911 if (!interval)
2912 interval = suspend_time;
2913 /*
2914 * suspend time format:
2915 * 0-19: beacon interval in usec (time before exec.)
2916 * 20-23: 0
2917 * 24-31: number of beacons (suspend between channels)
2918 */
2919
2920 extra = (suspend_time / interval) << 24;
2921 scan_suspend_time = 0xFF0FFFFF &
2922 (extra | ((suspend_time % interval) * 1024));
2923
2924 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002925 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002926 scan_suspend_time, interval);
2927 }
2928
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002929 if (priv->scan_request->n_ssids) {
2930 int i, p = 0;
2931 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2932 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2933 /* always does wildcard anyway */
2934 if (!priv->scan_request->ssids[i].ssid_len)
2935 continue;
2936 scan->direct_scan[p].id = WLAN_EID_SSID;
2937 scan->direct_scan[p].len =
2938 priv->scan_request->ssids[i].ssid_len;
2939 memcpy(scan->direct_scan[p].ssid,
2940 priv->scan_request->ssids[i].ssid,
2941 priv->scan_request->ssids[i].ssid_len);
2942 n_probes++;
2943 p++;
2944 }
2945 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002946 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002947 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002948
2949 /* We don't build a direct scan probe request; the uCode will do
2950 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002951 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002952 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002953 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2954
2955 /* flags + rate selection */
2956
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002957 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07002958 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2959 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2960 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002961 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002962 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07002963 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Bergb097ad22009-04-20 14:36:57 -07002964 /*
2965 * If active scaning is requested but a certain channel
2966 * is marked passive, we can do active scanning if we
2967 * detect transmissions.
2968 */
2969 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002970 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002971 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002972 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07002973 goto done;
2974 }
2975
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08002976 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002977 iwl_fill_probe_req(priv,
2978 (struct ieee80211_mgmt *)scan->data,
2979 priv->scan_request->ie,
2980 priv->scan_request->ie_len,
2981 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08002982
Zhu Yib481de92007-09-25 17:54:57 -07002983 /* select Rx antennas */
2984 scan->flags |= iwl3945_get_antenna_flags(priv);
2985
Wey-Yi Guy279b05d2009-04-20 14:37:00 -07002986 if (iwl_is_monitor_mode(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002987 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
2988
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002989 scan->channel_count =
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002990 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002991 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07002992
Reinette Chatre14b54332008-11-04 12:21:35 -08002993 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002994 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08002995 goto done;
2996 }
2997
Zhu Yib481de92007-09-25 17:54:57 -07002998 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002999 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003000 cmd.data = scan;
3001 scan->len = cpu_to_le16(cmd.len);
3002
3003 set_bit(STATUS_SCAN_HW, &priv->status);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003004 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003005 if (rc)
3006 goto done;
3007
3008 queue_delayed_work(priv->workqueue, &priv->scan_check,
3009 IWL_SCAN_CHECK_WATCHDOG);
3010
3011 mutex_unlock(&priv->mutex);
3012 return;
3013
3014 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003015 /* can not perform scan make sure we clear scanning
3016 * bits from status so next scan request can be performed.
3017 * if we dont clear scanning status bit here all next scan
3018 * will fail
3019 */
3020 clear_bit(STATUS_SCAN_HW, &priv->status);
3021 clear_bit(STATUS_SCANNING, &priv->status);
3022
Ian Schram01ebd062007-10-25 17:15:22 +08003023 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003024 queue_work(priv->workqueue, &priv->scan_completed);
3025 mutex_unlock(&priv->mutex);
3026}
3027
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003028static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003029{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003030 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003031
3032 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3033 return;
3034
Johannes Berg19cc1082009-05-08 13:44:36 -07003035 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
3036 mutex_lock(&priv->mutex);
3037 priv->vif = NULL;
3038 priv->is_open = 0;
3039 mutex_unlock(&priv->mutex);
3040 iwl3945_down(priv);
3041 ieee80211_restart_hw(priv->hw);
3042 } else {
3043 iwl3945_down(priv);
Johannes Berg80676512010-01-21 06:07:17 -08003044
3045 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3046 return;
3047
3048 mutex_lock(&priv->mutex);
3049 __iwl3945_up(priv);
3050 mutex_unlock(&priv->mutex);
Johannes Berg19cc1082009-05-08 13:44:36 -07003051 }
Zhu Yib481de92007-09-25 17:54:57 -07003052}
3053
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003054static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003055{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003056 struct iwl_priv *priv =
3057 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003058
3059 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3060 return;
3061
3062 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003063 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003064 mutex_unlock(&priv->mutex);
3065}
3066
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003067#define IWL_DELAY_NEXT_SCAN (HZ*2)
3068
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003069void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003070{
Zhu Yib481de92007-09-25 17:54:57 -07003071 int rc = 0;
3072 struct ieee80211_conf *conf = NULL;
3073
Johannes Berg05c914f2008-09-11 00:01:58 +02003074 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003075 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003076 return;
3077 }
3078
3079
Tomas Winklere1623442009-01-27 14:27:56 -08003080 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003081 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003082
3083 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3084 return;
3085
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003086 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003087 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08003088
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003089 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003090
Zhu Yib481de92007-09-25 17:54:57 -07003091 conf = ieee80211_get_hw_conf(priv->hw);
3092
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003093 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003094 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003095
Tomas Winkler28afaf92008-12-19 10:37:06 +08003096 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Tomas Winkler2c2f3b32009-06-19 13:52:45 -07003097 iwl_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003098 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003099 sizeof(priv->rxon_timing), &priv->rxon_timing);
3100 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003101 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003102 "Attempting to continue.\n");
3103
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003104 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003105
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003106 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003107
Tomas Winklere1623442009-01-27 14:27:56 -08003108 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003109 priv->assoc_id, priv->beacon_int);
3110
3111 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003112 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003113 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003114 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003115
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003116 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003117 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003118 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003119 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003120 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003121
Johannes Berg05c914f2008-09-11 00:01:58 +02003122 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003123 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003124
3125 }
3126
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003127 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003128
3129 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003130 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003131 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003132 break;
3133
Johannes Berg05c914f2008-09-11 00:01:58 +02003134 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07003135
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08003136 priv->assoc_id = 1;
Tomas Winklerc587de02009-06-03 11:44:07 -07003137 iwl_add_station(priv, priv->bssid, 0, CMD_SYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003138 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01003139 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003140 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
3141 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003142 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
3143 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003144
3145 break;
3146
3147 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08003148 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003149 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07003150 break;
3151 }
3152
Abhijeet Kolekar14d2aac2009-02-27 16:21:24 -08003153 iwl_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08003154
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003155 /* we have just associated, don't start scan too early */
3156 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003157}
3158
Zhu Yib481de92007-09-25 17:54:57 -07003159/*****************************************************************************
3160 *
3161 * mac80211 entry point functions
3162 *
3163 *****************************************************************************/
3164
Zhu Yi5a66926a2008-01-14 17:46:18 -08003165#define UCODE_READY_TIMEOUT (2 * HZ)
3166
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003167static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003168{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003169 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003170 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003171
Tomas Winklere1623442009-01-27 14:27:56 -08003172 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003173
3174 /* we should be verifying the device is ready to be opened */
3175 mutex_lock(&priv->mutex);
3176
Zhu Yi5a66926a2008-01-14 17:46:18 -08003177 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3178 * ucode filename and max sizes are card-specific. */
3179
3180 if (!priv->ucode_code.len) {
3181 ret = iwl3945_read_ucode(priv);
3182 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003183 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003184 mutex_unlock(&priv->mutex);
3185 goto out_release_irq;
3186 }
3187 }
3188
Zhu Yie655b9f2008-01-24 02:19:38 -08003189 ret = __iwl3945_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003190
Zhu Yib481de92007-09-25 17:54:57 -07003191 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003192
Zhu Yie655b9f2008-01-24 02:19:38 -08003193 if (ret)
3194 goto out_release_irq;
3195
Tomas Winklere1623442009-01-27 14:27:56 -08003196 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003197
Zhu Yi5a66926a2008-01-14 17:46:18 -08003198 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3199 * mac80211 will not be run successfully. */
3200 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3201 test_bit(STATUS_READY, &priv->status),
3202 UCODE_READY_TIMEOUT);
3203 if (!ret) {
3204 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003205 IWL_ERR(priv,
3206 "Wait for START_ALIVE timeout after %dms.\n",
3207 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a66926a2008-01-14 17:46:18 -08003208 ret = -ETIMEDOUT;
3209 goto out_release_irq;
3210 }
3211 }
3212
Helmut Schaa26635162009-01-15 09:38:44 +01003213 /* ucode is running and will send rfkill notifications,
3214 * no need to poll the killswitch state anymore */
3215 cancel_delayed_work(&priv->rfkill_poll);
3216
Johannes Berge932a602009-10-02 13:44:03 -07003217 iwl_led_start(priv);
3218
Zhu Yie655b9f2008-01-24 02:19:38 -08003219 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003220 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003221 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003222
3223out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003224 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003225 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08003226 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003227}
3228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003229static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003230{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003231 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003232
Tomas Winklere1623442009-01-27 14:27:56 -08003233 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003234
Zhu Yie655b9f2008-01-24 02:19:38 -08003235 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003236 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003237 return;
3238 }
3239
Zhu Yib481de92007-09-25 17:54:57 -07003240 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003241
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003242 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003243 /* stop mac, cancel any scan request and clear
3244 * RXON_FILTER_ASSOC_MSK BIT
3245 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08003246 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003247 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003248 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003249 }
3250
Zhu Yi5a66926a2008-01-14 17:46:18 -08003251 iwl3945_down(priv);
3252
3253 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003254
3255 /* start polling the killswitch state again */
3256 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3257 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003258
Tomas Winklere1623442009-01-27 14:27:56 -08003259 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003260}
3261
Johannes Berge039fa42008-05-15 12:55:29 +02003262static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003263{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003264 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003265
Tomas Winklere1623442009-01-27 14:27:56 -08003266 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003267
Tomas Winklere1623442009-01-27 14:27:56 -08003268 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003269 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003270
Johannes Berge039fa42008-05-15 12:55:29 +02003271 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003272 dev_kfree_skb_any(skb);
3273
Tomas Winklere1623442009-01-27 14:27:56 -08003274 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003275 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003276}
3277
Abhijeet Kolekar60690a62009-04-08 11:26:49 -07003278void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003279{
3280 int rc = 0;
3281
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003282 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003283 return;
3284
3285 /* The following should be done only at AP bring up */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003286 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003287
3288 /* RXON - unassoc (to set timing command) */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003289 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003290 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003291
3292 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08003293 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Tomas Winkler2c2f3b32009-06-19 13:52:45 -07003294 iwl_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003295 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3296 sizeof(priv->rxon_timing),
3297 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07003298 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003299 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003300 "Attempting to continue.\n");
3301
3302 /* FIXME: what should be the assoc_id for AP? */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003303 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003304 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003305 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003306 RXON_FLG_SHORT_PREAMBLE_MSK;
3307 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003308 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003309 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3310
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003311 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07003312 if (priv->assoc_capability &
3313 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003314 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003315 RXON_FLG_SHORT_SLOT_MSK;
3316 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003317 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003318 ~RXON_FLG_SHORT_SLOT_MSK;
3319
Johannes Berg05c914f2008-09-11 00:01:58 +02003320 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003321 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003322 ~RXON_FLG_SHORT_SLOT_MSK;
3323 }
3324 /* restore RXON assoc */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003325 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003326 iwlcore_commit_rxon(priv);
Tomas Winklerc587de02009-06-03 11:44:07 -07003327 iwl_add_station(priv, iwl_bcast_addr, 0, CMD_SYNC, NULL);
Zhu Yi556f8db2007-09-27 11:27:33 +08003328 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003329 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003330
3331 /* FIXME - we need to add code here to detect a totally new
3332 * configuration, reset the AP, unassoc, rxon timing, assoc,
3333 * clear sta table, add BCAST sta... */
3334}
3335
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003336static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003337 struct ieee80211_vif *vif,
3338 struct ieee80211_sta *sta,
3339 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003340{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003341 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01003342 const u8 *addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003343 int ret = 0;
3344 u8 sta_id = IWL_INVALID_STATION;
3345 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003346
Tomas Winklere1623442009-01-27 14:27:56 -08003347 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003348
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003349 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003350 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003351 return -EOPNOTSUPP;
3352 }
3353
Winkler, Tomas42986792009-01-19 15:30:22 -08003354 addr = sta ? sta->addr : iwl_bcast_addr;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003355 static_key = !iwl_is_associated(priv);
3356
3357 if (!static_key) {
Tomas Winklerc587de02009-06-03 11:44:07 -07003358 sta_id = iwl_find_station(priv, addr);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003359 if (sta_id == IWL_INVALID_STATION) {
Joe Perches12514392009-04-16 16:23:26 -07003360 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003361 addr);
3362 return -EINVAL;
3363 }
Zhu Yib481de92007-09-25 17:54:57 -07003364 }
3365
3366 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003367 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003368 mutex_unlock(&priv->mutex);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003369
Zhu Yib481de92007-09-25 17:54:57 -07003370 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003371 case SET_KEY:
3372 if (static_key)
3373 ret = iwl3945_set_static_key(priv, key);
3374 else
3375 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3376 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003377 break;
3378 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003379 if (static_key)
3380 ret = iwl3945_remove_static_key(priv);
3381 else
3382 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3383 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003384 break;
3385 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003386 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003387 }
3388
Tomas Winklere1623442009-01-27 14:27:56 -08003389 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003390
Winkler, Tomas42986792009-01-19 15:30:22 -08003391 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003392}
3393
Zhu Yib481de92007-09-25 17:54:57 -07003394/*****************************************************************************
3395 *
3396 * sysfs attributes
3397 *
3398 *****************************************************************************/
3399
Samuel Ortizd08853a2009-01-23 13:45:17 -08003400#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003401
3402/*
3403 * The following adds a new attribute to the sysfs representation
3404 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3405 * used for controlling the debug level.
3406 *
3407 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003408 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003409 * The debug_level being managed using sysfs below is a per device debug
3410 * level that is used instead of the global debug level if it (the per
3411 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003412 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003413static ssize_t show_debug_level(struct device *d,
3414 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003415{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003416 struct iwl_priv *priv = dev_get_drvdata(d);
3417 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003418}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003419static ssize_t store_debug_level(struct device *d,
3420 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003421 const char *buf, size_t count)
3422{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003423 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003424 unsigned long val;
3425 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003426
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003427 ret = strict_strtoul(buf, 0, &val);
3428 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003429 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003430 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003431 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003432 if (iwl_alloc_traffic_mem(priv))
3433 IWL_ERR(priv,
3434 "Not enough memory to generate traffic log\n");
3435 }
Zhu Yib481de92007-09-25 17:54:57 -07003436 return strnlen(buf, count);
3437}
3438
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003439static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3440 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003441
Samuel Ortizd08853a2009-01-23 13:45:17 -08003442#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003443
Zhu Yib481de92007-09-25 17:54:57 -07003444static ssize_t show_temperature(struct device *d,
3445 struct device_attribute *attr, char *buf)
3446{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003447 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003448
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003449 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003450 return -EAGAIN;
3451
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003452 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003453}
3454
3455static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3456
Zhu Yib481de92007-09-25 17:54:57 -07003457static ssize_t show_tx_power(struct device *d,
3458 struct device_attribute *attr, char *buf)
3459{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003460 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003461 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003462}
3463
3464static ssize_t store_tx_power(struct device *d,
3465 struct device_attribute *attr,
3466 const char *buf, size_t count)
3467{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003468 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003469 char *p = (char *)buf;
3470 u32 val;
3471
3472 val = simple_strtoul(p, &p, 10);
3473 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003474 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003475 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003476 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003477
3478 return count;
3479}
3480
3481static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3482
3483static ssize_t show_flags(struct device *d,
3484 struct device_attribute *attr, char *buf)
3485{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003486 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003487
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003488 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003489}
3490
3491static ssize_t store_flags(struct device *d,
3492 struct device_attribute *attr,
3493 const char *buf, size_t count)
3494{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003495 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003496 u32 flags = simple_strtoul(buf, NULL, 0);
3497
3498 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003499 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003500 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003501 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003502 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003503 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003504 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003505 flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003506 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003507 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003508 }
3509 }
3510 mutex_unlock(&priv->mutex);
3511
3512 return count;
3513}
3514
3515static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3516
3517static ssize_t show_filter_flags(struct device *d,
3518 struct device_attribute *attr, char *buf)
3519{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003520 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003521
3522 return sprintf(buf, "0x%04X\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003523 le32_to_cpu(priv->active_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003524}
3525
3526static ssize_t store_filter_flags(struct device *d,
3527 struct device_attribute *attr,
3528 const char *buf, size_t count)
3529{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003530 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003531 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3532
3533 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003534 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003535 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003536 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003537 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003538 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003539 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003540 "0x%04X\n", filter_flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003541 priv->staging_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003542 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003543 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003544 }
3545 }
3546 mutex_unlock(&priv->mutex);
3547
3548 return count;
3549}
3550
3551static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3552 store_filter_flags);
3553
Zhu Yib481de92007-09-25 17:54:57 -07003554static ssize_t show_measurement(struct device *d,
3555 struct device_attribute *attr, char *buf)
3556{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003557 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003558 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003559 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003560 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003561 unsigned long flags;
3562
3563 spin_lock_irqsave(&priv->lock, flags);
3564 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3565 spin_unlock_irqrestore(&priv->lock, flags);
3566 return 0;
3567 }
3568 memcpy(&measure_report, &priv->measure_report, size);
3569 priv->measurement_status = 0;
3570 spin_unlock_irqrestore(&priv->lock, flags);
3571
3572 while (size && (PAGE_SIZE - len)) {
3573 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3574 PAGE_SIZE - len, 1);
3575 len = strlen(buf);
3576 if (PAGE_SIZE - len)
3577 buf[len++] = '\n';
3578
3579 ofs += 16;
3580 size -= min(size, 16U);
3581 }
3582
3583 return len;
3584}
3585
3586static ssize_t store_measurement(struct device *d,
3587 struct device_attribute *attr,
3588 const char *buf, size_t count)
3589{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003590 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003591 struct ieee80211_measurement_params params = {
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003592 .channel = le16_to_cpu(priv->active_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07003593 .start_time = cpu_to_le64(priv->last_tsf),
3594 .duration = cpu_to_le16(1),
3595 };
3596 u8 type = IWL_MEASURE_BASIC;
3597 u8 buffer[32];
3598 u8 channel;
3599
3600 if (count) {
3601 char *p = buffer;
3602 strncpy(buffer, buf, min(sizeof(buffer), count));
3603 channel = simple_strtoul(p, NULL, 0);
3604 if (channel)
3605 params.channel = channel;
3606
3607 p = buffer;
3608 while (*p && *p != ' ')
3609 p++;
3610 if (*p)
3611 type = simple_strtoul(p + 1, NULL, 0);
3612 }
3613
Tomas Winklere1623442009-01-27 14:27:56 -08003614 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003615 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003616 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003617
3618 return count;
3619}
3620
3621static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3622 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003623
Zhu Yib481de92007-09-25 17:54:57 -07003624static ssize_t store_retry_rate(struct device *d,
3625 struct device_attribute *attr,
3626 const char *buf, size_t count)
3627{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003628 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003629
3630 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3631 if (priv->retry_rate <= 0)
3632 priv->retry_rate = 1;
3633
3634 return count;
3635}
3636
3637static ssize_t show_retry_rate(struct device *d,
3638 struct device_attribute *attr, char *buf)
3639{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003640 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003641 return sprintf(buf, "%d", priv->retry_rate);
3642}
3643
3644static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3645 store_retry_rate);
3646
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003647
Zhu Yib481de92007-09-25 17:54:57 -07003648static ssize_t show_channels(struct device *d,
3649 struct device_attribute *attr, char *buf)
3650{
Johannes Berg8318d782008-01-24 19:38:38 +01003651 /* all this shit doesn't belong into sysfs anyway */
3652 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003653}
3654
3655static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3656
3657static ssize_t show_statistics(struct device *d,
3658 struct device_attribute *attr, char *buf)
3659{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003660 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003661 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07003662 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003663 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07003664 int rc = 0;
3665
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003666 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003667 return -EAGAIN;
3668
3669 mutex_lock(&priv->mutex);
Wey-Yi Guyef8d5522009-11-13 11:56:28 -08003670 rc = iwl_send_statistics_request(priv, CMD_SYNC, false);
Zhu Yib481de92007-09-25 17:54:57 -07003671 mutex_unlock(&priv->mutex);
3672
3673 if (rc) {
3674 len = sprintf(buf,
3675 "Error sending statistics request: 0x%08X\n", rc);
3676 return len;
3677 }
3678
3679 while (size && (PAGE_SIZE - len)) {
3680 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3681 PAGE_SIZE - len, 1);
3682 len = strlen(buf);
3683 if (PAGE_SIZE - len)
3684 buf[len++] = '\n';
3685
3686 ofs += 16;
3687 size -= min(size, 16U);
3688 }
3689
3690 return len;
3691}
3692
3693static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
3694
3695static ssize_t show_antenna(struct device *d,
3696 struct device_attribute *attr, char *buf)
3697{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003698 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003699
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003700 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003701 return -EAGAIN;
3702
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003703 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003704}
3705
3706static ssize_t store_antenna(struct device *d,
3707 struct device_attribute *attr,
3708 const char *buf, size_t count)
3709{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003710 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003711 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003712
3713 if (count == 0)
3714 return 0;
3715
3716 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003717 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003718 return count;
3719 }
3720
3721 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003722 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003723 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003724 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003725 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003726
3727
3728 return count;
3729}
3730
3731static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3732
3733static ssize_t show_status(struct device *d,
3734 struct device_attribute *attr, char *buf)
3735{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003736 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003737 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003738 return -EAGAIN;
3739 return sprintf(buf, "0x%08x\n", (int)priv->status);
3740}
3741
3742static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3743
3744static ssize_t dump_error_log(struct device *d,
3745 struct device_attribute *attr,
3746 const char *buf, size_t count)
3747{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003748 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003749 char *p = (char *)buf;
3750
3751 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003752 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003753
3754 return strnlen(buf, count);
3755}
3756
3757static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3758
Zhu Yib481de92007-09-25 17:54:57 -07003759/*****************************************************************************
3760 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003761 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003762 *
3763 *****************************************************************************/
3764
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003765static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003766{
Reinette Chatred21050c2009-02-13 11:51:18 -08003767 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003768
3769 init_waitqueue_head(&priv->wait_command_queue);
3770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003771 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3772 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003773 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003774 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3775 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Helmut Schaa26635162009-01-15 09:38:44 +01003776 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003777 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
3778 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
3779 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
3780 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07003781
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003782 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003783
3784 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003785 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003786}
3787
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003788static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003789{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003790 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003791
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003792 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003793 cancel_delayed_work(&priv->scan_check);
3794 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003795 cancel_work_sync(&priv->beacon_update);
3796}
3797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003799 &dev_attr_antenna.attr,
3800 &dev_attr_channels.attr,
3801 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003802 &dev_attr_flags.attr,
3803 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003804 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003805 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003806 &dev_attr_statistics.attr,
3807 &dev_attr_status.attr,
3808 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003809 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003810#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003811 &dev_attr_debug_level.attr,
3812#endif
Zhu Yib481de92007-09-25 17:54:57 -07003813 NULL
3814};
3815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003816static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003817 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003819};
3820
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003821static struct ieee80211_ops iwl3945_hw_ops = {
3822 .tx = iwl3945_mac_tx,
3823 .start = iwl3945_mac_start,
3824 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003825 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003826 .remove_interface = iwl_mac_remove_interface,
Abhijeet Kolekar48083682009-04-08 11:26:48 -07003827 .config = iwl_mac_config,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003828 .configure_filter = iwl_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003829 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003830 .conf_tx = iwl_mac_conf_tx,
Abhijeet Kolekarbd564262009-04-08 11:26:52 -07003831 .reset_tsf = iwl_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003832 .bss_info_changed = iwl_bss_info_changed,
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -08003833 .hw_scan = iwl_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07003834};
3835
Winkler, Tomase52119c2008-12-22 11:31:19 +08003836static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003837{
3838 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003839 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003840
3841 priv->retry_rate = 1;
3842 priv->ibss_beacon = NULL;
3843
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003844 spin_lock_init(&priv->sta_lock);
3845 spin_lock_init(&priv->hcmd_lock);
3846
3847 INIT_LIST_HEAD(&priv->free_frames);
3848
3849 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003850 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003851
3852 /* Clear the driver's (not device's) station table */
Tomas Winklerc587de02009-06-03 11:44:07 -07003853 iwl_clear_stations_table(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003854
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003855 priv->ieee_channels = NULL;
3856 priv->ieee_rates = NULL;
3857 priv->band = IEEE80211_BAND_2GHZ;
3858
3859 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003860 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003861
3862 iwl_reset_qos(priv);
3863
3864 priv->qos_data.qos_active = 0;
3865 priv->qos_data.qos_cap.val = 0;
3866
3867 priv->rates_mask = IWL_RATES_MASK;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003868 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003869
Samuel Ortize6148912009-01-23 13:45:15 -08003870 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3871 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3872 eeprom->version);
3873 ret = -EINVAL;
3874 goto err;
3875 }
3876 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003877 if (ret) {
3878 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3879 goto err;
3880 }
3881
Samuel Ortize6148912009-01-23 13:45:15 -08003882 /* Set up txpower settings in driver for all channels */
3883 if (iwl3945_txpower_set_from_eeprom(priv)) {
3884 ret = -EIO;
3885 goto err_free_channel_map;
3886 }
3887
Samuel Ortiz534166d2009-01-23 13:45:16 -08003888 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003889 if (ret) {
3890 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3891 goto err_free_channel_map;
3892 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003893 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3894
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003895 return 0;
3896
3897err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003898 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003899err:
3900 return ret;
3901}
3902
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003903static int iwl3945_setup_mac(struct iwl_priv *priv)
3904{
3905 int ret;
3906 struct ieee80211_hw *hw = priv->hw;
3907
3908 hw->rate_control_algorithm = "iwl-3945-rs";
3909 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
3910
3911 /* Tell mac80211 our characteristics */
3912 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Mohamed Abbasb1c60192009-03-17 21:51:47 -07003913 IEEE80211_HW_NOISE_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003914 IEEE80211_HW_SPECTRUM_MGMT;
3915
3916 if (!priv->cfg->broken_powersave)
3917 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3918 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003919
3920 hw->wiphy->interface_modes =
3921 BIT(NL80211_IFTYPE_STATION) |
3922 BIT(NL80211_IFTYPE_ADHOC);
3923
Johannes Berg5be83de2009-11-19 00:56:28 +01003924 hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY |
3925 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003926
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003927 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3928 /* we create the 802.11 header and a zero-length SSID element */
3929 hw->wiphy->max_scan_ie_len = IWL_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc91a2009-04-09 09:56:02 +02003930
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003931 /* Default value; 4 EDCA QOS priorities */
3932 hw->queues = 4;
3933
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003934 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3935 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3936 &priv->bands[IEEE80211_BAND_2GHZ];
3937
3938 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3939 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3940 &priv->bands[IEEE80211_BAND_5GHZ];
3941
3942 ret = ieee80211_register_hw(priv->hw);
3943 if (ret) {
3944 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3945 return ret;
3946 }
3947 priv->mac80211_registered = 1;
3948
3949 return 0;
3950}
3951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003952static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003953{
3954 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003955 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003956 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003957 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003958 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003959 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003960
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003961 /***********************
3962 * 1. Allocating HW data
3963 * ********************/
3964
Zhu Yib481de92007-09-25 17:54:57 -07003965 /* mac80211 allocates memory for this device instance, including
3966 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003967 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003968 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003969 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07003970 err = -ENOMEM;
3971 goto out;
3972 }
Zhu Yib481de92007-09-25 17:54:57 -07003973 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003974 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003975
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003976 /*
3977 * Disabling hardware scan means that mac80211 will perform scans
3978 * "the hard way", rather than using device's scan.
3979 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003980 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08003981 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003982 iwl3945_hw_ops.hw_scan = NULL;
3983 }
3984
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003985
Tomas Winklere1623442009-01-27 14:27:56 -08003986 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003987 priv->cfg = cfg;
3988 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07003989 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003990
Samuel Ortizd08853a2009-01-23 13:45:17 -08003991#ifdef CONFIG_IWLWIFI_DEBUG
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003992 atomic_set(&priv->restrict_refcnt, 0);
3993#endif
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003994 if (iwl_alloc_traffic_mem(priv))
3995 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07003996
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003997 /***************************
3998 * 2. Initializing PCI bus
3999 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07004000 if (pci_enable_device(pdev)) {
4001 err = -ENODEV;
4002 goto out_ieee80211_free_hw;
4003 }
4004
4005 pci_set_master(pdev);
4006
Yang Hongyang284901a2009-04-06 19:01:15 -07004007 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004008 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004009 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004010 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004011 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004012 goto out_pci_disable_device;
4013 }
4014
4015 pci_set_drvdata(pdev, priv);
4016 err = pci_request_regions(pdev, DRV_NAME);
4017 if (err)
4018 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004019
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004020 /***********************
4021 * 3. Read REV Register
4022 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004023 priv->hw_base = pci_iomap(pdev, 0, 0);
4024 if (!priv->hw_base) {
4025 err = -ENODEV;
4026 goto out_pci_release_regions;
4027 }
4028
Tomas Winklere1623442009-01-27 14:27:56 -08004029 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004030 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004031 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004032
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004033 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4034 * PCI Tx retries from interfering with C3 CPU state */
4035 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004036
Reinette Chatre731a29b2009-12-14 14:12:11 -08004037 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004038 * we should init now
4039 */
4040 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004041 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004042
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004043 /*
4044 * stop and reset the on-board processor just in case it is in a
4045 * strange state ... like being left stranded by a primary kernel
4046 * and this is now the kdump kernel trying to start up
4047 */
4048 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4049
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004050 /***********************
4051 * 4. Read EEPROM
4052 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004053
Zhu Yi5a66926a2008-01-14 17:46:18 -08004054 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004055 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004056 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004057 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004058 goto out_iounmap;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004059 }
4060 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004061 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4062 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08004063 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004064 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4065
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004066 /***********************
4067 * 5. Setup HW Constants
4068 * ********************/
4069 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004070 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004071 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004072 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004073 }
4074
4075 /***********************
4076 * 6. Setup priv
4077 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004078
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004079 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004080 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004081 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004082 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004083 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004084
Tomas Winkler978785a2008-12-19 10:37:31 +08004085 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4086 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004087
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004088 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004089 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004090 * ********************/
4091
4092 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004093 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004094 spin_unlock_irqrestore(&priv->lock, flags);
4095
Helmut Schaa26635162009-01-15 09:38:44 +01004096 pci_enable_msi(priv->pci_dev);
4097
Mohamed Abbasef850d72009-05-22 11:01:50 -07004098 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
4099 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004100 if (err) {
4101 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4102 goto out_disable_msi;
4103 }
4104
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004105 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4106 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004107 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004108 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004109 }
4110
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004111 iwl_set_rxon_channel(priv,
4112 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004113 iwl3945_setup_deferred_work(priv);
4114 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004115 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004116
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004117 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004118 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004119 * *******************************/
4120
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004121 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004122
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004123 err = iwl3945_setup_mac(priv);
4124 if (err)
4125 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004126
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004127 err = iwl_dbgfs_register(priv, DRV_NAME);
4128 if (err)
4129 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4130
Helmut Schaa26635162009-01-15 09:38:44 +01004131 /* Start monitoring the killswitch */
4132 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
4133 2 * HZ);
4134
Zhu Yib481de92007-09-25 17:54:57 -07004135 return 0;
4136
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004137 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004138 destroy_workqueue(priv->workqueue);
4139 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004140 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4141 out_release_irq:
4142 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004143 out_disable_msi:
4144 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004145 iwlcore_free_geos(priv);
4146 iwl_free_channel_map(priv);
4147 out_unset_hw_params:
4148 iwl3945_unset_hw_params(priv);
4149 out_eeprom_free:
4150 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004151 out_iounmap:
4152 pci_iounmap(pdev, priv->hw_base);
4153 out_pci_release_regions:
4154 pci_release_regions(pdev);
4155 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004156 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004157 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004158 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004159 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004160 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004161 out:
4162 return err;
4163}
4164
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004165static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004166{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004167 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004168 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004169
4170 if (!priv)
4171 return;
4172
Tomas Winklere1623442009-01-27 14:27:56 -08004173 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004174
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004175 iwl_dbgfs_unregister(priv);
4176
Zhu Yib481de92007-09-25 17:54:57 -07004177 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004178
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004179 if (priv->mac80211_registered) {
4180 ieee80211_unregister_hw(priv->hw);
4181 priv->mac80211_registered = 0;
4182 } else {
4183 iwl3945_down(priv);
4184 }
Zhu Yib481de92007-09-25 17:54:57 -07004185
Ben Cahillc166b252009-10-23 13:42:35 -07004186 /*
4187 * Make sure device is reset to low power before unloading driver.
4188 * This may be redundant with iwl_down(), but there are paths to
4189 * run iwl_down() without calling apm_ops.stop(), and there are
4190 * paths to avoid running iwl_down() at all before leaving driver.
4191 * This (inexpensive) call *makes sure* device is reset.
4192 */
4193 priv->cfg->ops->lib->apm_ops.stop(priv);
4194
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004195 /* make sure we flush any pending irq or
4196 * tasklet for the driver
4197 */
4198 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004199 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004200 spin_unlock_irqrestore(&priv->lock, flags);
4201
4202 iwl_synchronize_irq(priv);
4203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004204 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004205
Reinette Chatre71d449b2009-04-20 14:37:01 -07004206 cancel_delayed_work_sync(&priv->rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004207
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004208 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004209
4210 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004211 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004212 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004213
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004214 iwl3945_unset_hw_params(priv);
Tomas Winklerc587de02009-06-03 11:44:07 -07004215 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004216
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004217 /*netif_stop_queue(dev); */
4218 flush_workqueue(priv->workqueue);
4219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004220 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004221 * priv->workqueue... so we can't take down the workqueue
4222 * until now... */
4223 destroy_workqueue(priv->workqueue);
4224 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004225 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004226
Helmut Schaa26635162009-01-15 09:38:44 +01004227 free_irq(pdev->irq, priv);
4228 pci_disable_msi(pdev);
4229
Zhu Yib481de92007-09-25 17:54:57 -07004230 pci_iounmap(pdev, priv->hw_base);
4231 pci_release_regions(pdev);
4232 pci_disable_device(pdev);
4233 pci_set_drvdata(pdev, NULL);
4234
Samuel Ortize6148912009-01-23 13:45:15 -08004235 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004236 iwlcore_free_geos(priv);
Winkler, Tomas805cee52009-01-19 15:30:28 -08004237 kfree(priv->scan);
Zhu Yib481de92007-09-25 17:54:57 -07004238 if (priv->ibss_beacon)
4239 dev_kfree_skb(priv->ibss_beacon);
4240
4241 ieee80211_free_hw(priv->hw);
4242}
4243
Zhu Yib481de92007-09-25 17:54:57 -07004244
4245/*****************************************************************************
4246 *
4247 * driver and module entry point
4248 *
4249 *****************************************************************************/
4250
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004251static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004252 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004253 .id_table = iwl3945_hw_card_ids,
4254 .probe = iwl3945_pci_probe,
4255 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004256#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004257 .suspend = iwl_pci_suspend,
4258 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004259#endif
4260};
4261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004262static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004263{
4264
4265 int ret;
4266 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4267 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004268
4269 ret = iwl3945_rate_control_register();
4270 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004271 printk(KERN_ERR DRV_NAME
4272 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004273 return ret;
4274 }
4275
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004276 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004277 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004278 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004279 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004280 }
Zhu Yib481de92007-09-25 17:54:57 -07004281
4282 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004283
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004284error_register:
4285 iwl3945_rate_control_unregister();
4286 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004287}
4288
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004289static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004290{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004291 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004292 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004293}
4294
Reinette Chatrea0987a82008-12-02 12:14:06 -08004295MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004296
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004297module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004298MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004299module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004300MODULE_PARM_DESC(swcrypto,
4301 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004302#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004303module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004304MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004305#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004306module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4307 int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004308MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004309module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004310MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4311
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004312module_exit(iwl3945_exit);
4313module_init(iwl3945_init);