blob: 68b8a1af3be7f23f4066549c8e1eab4d15b767c2 [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{
Johannes Bergee525d12010-01-21 06:09:28 -0800354 if (priv->_3945.shared_virt)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800355 dma_free_coherent(&priv->pci_dev->dev,
356 sizeof(struct iwl3945_shared),
Johannes Bergee525d12010-01-21 06:09:28 -0800357 priv->_3945.shared_virt,
358 priv->_3945.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
Zhu Yib481de92007-09-25 17:54:57 -0700507 spin_unlock_irqrestore(&priv->lock, flags);
508
Harvey Harrison7294ec92008-07-15 18:43:59 -0700509 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800510
Johannes Berg2a87c262010-04-30 11:30:45 -0700511 /* Find index into station table for destination station */
Johannes Berg0af8bca2010-04-30 14:08:00 -0700512 sta_id = iwl_sta_id_or_broadcast(priv, info->control.sta);
Zhu Yib481de92007-09-25 17:54:57 -0700513 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800514 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700515 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700516 goto drop;
517 }
518
Tomas Winklere1623442009-01-27 14:27:56 -0800519 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700520
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700521 if (ieee80211_is_data_qos(fc)) {
522 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700523 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700524 if (unlikely(tid >= MAX_TID_COUNT))
525 goto drop;
Tomas Winklerc587de02009-06-03 11:44:07 -0700526 seq_number = priv->stations[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -0700527 IEEE80211_SCTL_SEQ;
528 hdr->seq_ctrl = cpu_to_le16(seq_number) |
529 (hdr->seq_ctrl &
Harvey Harrisonc1b4aa32009-01-29 13:26:44 -0800530 cpu_to_le16(IEEE80211_SCTL_FRAG));
Zhu Yib481de92007-09-25 17:54:57 -0700531 seq_number += 0x10;
532 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800533
534 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800535 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700536 q = &txq->q;
537
Zhu Yidc57a302009-12-14 14:12:12 -0800538 if ((iwl_queue_space(q) < q->high_mark))
539 goto drop;
540
Zhu Yib481de92007-09-25 17:54:57 -0700541 spin_lock_irqsave(&priv->lock, flags);
542
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800543 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700544
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800545 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800546 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800547 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800548
549 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800550 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700551 out_meta = &txq->meta[idx];
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700552 tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700553 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700554 memset(tx_cmd, 0, sizeof(*tx_cmd));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800555
556 /*
557 * Set up the Tx-command (not MAC!) header.
558 * Store the chosen Tx queue and TFD index within the sequence field;
559 * after Tx, uCode's Tx response will return this value so driver can
560 * locate the frame within the tx queue and do post-tx processing.
561 */
Zhu Yib481de92007-09-25 17:54:57 -0700562 out_cmd->hdr.cmd = REPLY_TX;
563 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800564 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800565
566 /* Copy MAC header from skb into command buffer */
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700567 memcpy(tx_cmd->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700568
Reinette Chatredf833b12009-04-21 10:55:48 -0700569
570 if (info->control.hw_key)
571 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
572
573 /* TODO need this for burst mode later on */
574 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
575
576 /* set is_hcca to 0; it probably will never be implemented */
577 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
578
579 /* Total # bytes to be transmitted */
580 len = (u16)skb->len;
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700581 tx_cmd->len = cpu_to_le16(len);
Reinette Chatredf833b12009-04-21 10:55:48 -0700582
Wey-Yi Guy20594eb2009-08-07 15:41:39 -0700583 iwl_dbg_log_tx_data_frame(priv, len, hdr);
Wey-Yi Guy22fdf3c2009-08-07 15:41:40 -0700584 iwl_update_stats(priv, true, fc, len);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700585 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
586 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Reinette Chatredf833b12009-04-21 10:55:48 -0700587
588 if (!ieee80211_has_morefrags(hdr->frame_control)) {
589 txq->need_update = 1;
590 if (qc)
Tomas Winklerc587de02009-06-03 11:44:07 -0700591 priv->stations[sta_id].tid[tid].seq_number = seq_number;
Reinette Chatredf833b12009-04-21 10:55:48 -0700592 } else {
593 wait_write_ptr = 1;
594 txq->need_update = 0;
595 }
596
Frans Pop91dd6c22010-03-24 14:19:58 -0700597 IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
Reinette Chatredf833b12009-04-21 10:55:48 -0700598 le16_to_cpu(out_cmd->hdr.sequence));
Frans Pop91dd6c22010-03-24 14:19:58 -0700599 IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700600 iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
601 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
Reinette Chatredf833b12009-04-21 10:55:48 -0700602 ieee80211_hdrlen(fc));
603
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800604 /*
605 * Use the first empty entry in this queue's command buffer array
606 * to contain the Tx command and MAC header concatenated together
607 * (payload data will be in another buffer).
608 * Size of this varies, due to varying MAC header length.
609 * If end is not dword aligned, we'll have 2 extra bytes at the end
610 * of the MAC header (device reads on dword boundaries).
611 * We'll tell device about this padding later.
612 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800613 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800614 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700615
616 len_org = len;
617 len = (len + 3) & ~3;
618
619 if (len_org != len)
620 len_org = 1;
621 else
622 len_org = 0;
623
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800624 /* Physical address of this Tx command's header (not MAC header!),
625 * within command buffer array. */
Reinette Chatredf833b12009-04-21 10:55:48 -0700626 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
627 len, PCI_DMA_TODEVICE);
628 /* we do not map meta data ... so we can safely access address to
629 * provide to unmap command*/
Johannes Bergc2acea82009-07-24 11:13:05 -0700630 pci_unmap_addr_set(out_meta, mapping, txcmd_phys);
631 pci_unmap_len_set(out_meta, len, len);
Zhu Yib481de92007-09-25 17:54:57 -0700632
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800633 /* Add buffer containing Tx command and MAC(!) header to TFD's
634 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800635 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
636 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700637
Zhu Yib481de92007-09-25 17:54:57 -0700638
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800639 /* Set up TFD's 2nd entry to point directly to remainder of skb,
640 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700641 len = skb->len - hdr_len;
642 if (len) {
643 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
644 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800645 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
646 phys_addr, len,
647 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700648 }
649
Zhu Yib481de92007-09-25 17:54:57 -0700650
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800651 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800652 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800653 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700654 spin_unlock_irqrestore(&priv->lock, flags);
655
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800656 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700657 && priv->mac80211_registered) {
658 if (wait_write_ptr) {
659 spin_lock_irqsave(&priv->lock, flags);
660 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800661 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700662 spin_unlock_irqrestore(&priv->lock, flags);
663 }
664
Johannes Berge4e72fb2009-03-23 17:28:42 +0100665 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700666 }
667
668 return 0;
669
670drop_unlock:
671 spin_unlock_irqrestore(&priv->lock, flags);
672drop:
673 return -1;
674}
675
Zhu Yib481de92007-09-25 17:54:57 -0700676#define BEACON_TIME_MASK_LOW 0x00FFFFFF
677#define BEACON_TIME_MASK_HIGH 0xFF000000
678#define TIME_UNIT 1024
679
680/*
681 * extended beacon time format
682 * time in usec will be changed into a 32-bit value in 8:24 format
683 * the high 1 byte is the beacon counts
684 * the lower 3 bytes is the time in usec within one beacon interval
685 */
686
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800687static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700688{
689 u32 quot;
690 u32 rem;
691 u32 interval = beacon_interval * 1024;
692
693 if (!interval || !usec)
694 return 0;
695
696 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
697 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
698
699 return (quot << 24) + rem;
700}
701
702/* base is usually what we get from ucode with each received frame,
703 * the same as HW timer counter counting down
704 */
705
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800706static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -0700707{
708 u32 base_low = base & BEACON_TIME_MASK_LOW;
709 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
710 u32 interval = beacon_interval * TIME_UNIT;
711 u32 res = (base & BEACON_TIME_MASK_HIGH) +
712 (addon & BEACON_TIME_MASK_HIGH);
713
714 if (base_low > addon_low)
715 res += base_low - addon_low;
716 else if (base_low < addon_low) {
717 res += interval + base_low - addon_low;
718 res += (1 << 24);
719 } else
720 res += (1 << 24);
721
722 return cpu_to_le32(res);
723}
724
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800725static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700726 struct ieee80211_measurement_params *params,
727 u8 type)
728{
Tomas Winkler600c0e12008-12-19 10:37:04 +0800729 struct iwl_spectrum_cmd spectrum;
Zhu Yi2f301222009-10-09 17:19:45 +0800730 struct iwl_rx_packet *pkt;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800731 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700732 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
733 .data = (void *)&spectrum,
Johannes Bergc2acea82009-07-24 11:13:05 -0700734 .flags = CMD_WANT_SKB,
Zhu Yib481de92007-09-25 17:54:57 -0700735 };
736 u32 add_time = le64_to_cpu(params->start_time);
737 int rc;
738 int spectrum_resp_status;
739 int duration = le16_to_cpu(params->duration);
740
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800741 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700742 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800743 iwl3945_usecs_to_beacons(
Johannes Berge99f168c2010-01-19 10:04:28 -0800744 le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
Zhu Yib481de92007-09-25 17:54:57 -0700745 le16_to_cpu(priv->rxon_timing.beacon_interval));
746
747 memset(&spectrum, 0, sizeof(spectrum));
748
749 spectrum.channel_count = cpu_to_le16(1);
750 spectrum.flags =
751 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
752 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
753 cmd.len = sizeof(spectrum);
754 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
755
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800756 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700757 spectrum.start_time =
Johannes Berge99f168c2010-01-19 10:04:28 -0800758 iwl3945_add_beacon_time(priv->_3945.last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -0700759 add_time,
760 le16_to_cpu(priv->rxon_timing.beacon_interval));
761 else
762 spectrum.start_time = 0;
763
764 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
765 spectrum.channels[0].channel = params->channel;
766 spectrum.channels[0].type = type;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800767 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700768 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
769 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
770
Samuel Ortiz518099a2009-01-19 15:30:27 -0800771 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700772 if (rc)
773 return rc;
774
Zhu Yi2f301222009-10-09 17:19:45 +0800775 pkt = (struct iwl_rx_packet *)cmd.reply_page;
776 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800777 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700778 rc = -EIO;
779 }
780
Zhu Yi2f301222009-10-09 17:19:45 +0800781 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
Zhu Yib481de92007-09-25 17:54:57 -0700782 switch (spectrum_resp_status) {
783 case 0: /* Command will be handled */
Zhu Yi2f301222009-10-09 17:19:45 +0800784 if (pkt->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800785 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800786 pkt->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700787 priv->measurement_status &= ~MEASUREMENT_READY;
788 }
789 priv->measurement_status |= MEASUREMENT_ACTIVE;
790 rc = 0;
791 break;
792
793 case 1: /* Command will not be handled */
794 rc = -EAGAIN;
795 break;
796 }
797
Zhu Yi64a76b52009-12-10 14:37:21 -0800798 iwl_free_pages(priv, cmd.reply_page);
Zhu Yib481de92007-09-25 17:54:57 -0700799
800 return rc;
801}
Zhu Yib481de92007-09-25 17:54:57 -0700802
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800803static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800804 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700805{
Zhu Yi2f301222009-10-09 17:19:45 +0800806 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800807 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700808 struct delayed_work *pwork;
809
810 palive = &pkt->u.alive_frame;
811
Tomas Winklere1623442009-01-27 14:27:56 -0800812 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700813 "0x%01X 0x%01X\n",
814 palive->is_valid, palive->ver_type,
815 palive->ver_subtype);
816
817 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800818 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800819 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
820 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700821 pwork = &priv->init_alive_start;
822 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800823 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700824 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800825 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700826 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800827 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700828 }
829
830 /* We delay the ALIVE response by 5ms to
831 * give the HW RF Kill time to activate... */
832 if (palive->is_valid == UCODE_VALID_OK)
833 queue_delayed_work(priv->workqueue, pwork,
834 msecs_to_jiffies(5));
835 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800836 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700837}
838
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800839static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800840 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700841{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100842#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800843 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Helmut Schaac7e035a2009-01-19 13:02:15 +0100844#endif
Zhu Yib481de92007-09-25 17:54:57 -0700845
Tomas Winklere1623442009-01-27 14:27:56 -0800846 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700847 return;
848}
849
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800850static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700851{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800852 struct iwl_priv *priv =
853 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700854 struct sk_buff *beacon;
855
856 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200857 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700858
859 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800860 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700861 return;
862 }
863
864 mutex_lock(&priv->mutex);
865 /* new beacon skb is allocated every time; dispose previous.*/
866 if (priv->ibss_beacon)
867 dev_kfree_skb(priv->ibss_beacon);
868
869 priv->ibss_beacon = beacon;
870 mutex_unlock(&priv->mutex);
871
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800872 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700873}
874
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800875static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800876 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700877{
Samuel Ortizd08853a2009-01-23 13:45:17 -0800878#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800879 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800880 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -0700881 u8 rate = beacon->beacon_notify_hdr.rate;
882
Tomas Winklere1623442009-01-27 14:27:56 -0800883 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700884 "tsf %d %d rate %d\n",
885 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
886 beacon->beacon_notify_hdr.failure_frame,
887 le32_to_cpu(beacon->ibss_mgr_status),
888 le32_to_cpu(beacon->high_tsf),
889 le32_to_cpu(beacon->low_tsf), rate);
890#endif
891
Johannes Berg05c914f2008-09-11 00:01:58 +0200892 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700893 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
894 queue_work(priv->workqueue, &priv->beacon_update);
895}
896
Zhu Yib481de92007-09-25 17:54:57 -0700897/* Handle notification from uCode that card's power state is changing
898 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800899static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800900 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700901{
Zhu Yi2f301222009-10-09 17:19:45 +0800902 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700903 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
904 unsigned long status = priv->status;
905
Reinette Chatre4c423a22009-07-17 09:30:26 -0700906 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700907 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
908 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
909
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800910 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700911 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
912
913 if (flags & HW_CARD_DISABLED)
914 set_bit(STATUS_RF_KILL_HW, &priv->status);
915 else
916 clear_bit(STATUS_RF_KILL_HW, &priv->status);
917
918
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800919 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700920
921 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200922 test_bit(STATUS_RF_KILL_HW, &priv->status)))
923 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
924 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700925 else
926 wake_up_interruptible(&priv->wait_command_queue);
927}
928
929/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800930 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700931 *
932 * Setup the RX handlers for each of the reply types sent from the uCode
933 * to the host.
934 *
935 * This function chains into the hardware specific files for them to setup
936 * any hardware specific handlers as well.
937 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800938static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700939{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800940 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
941 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800942 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800943 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Reinette Chatre81963d62010-01-22 14:22:57 -0800944 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
945 iwl_rx_spectrum_measure_notif;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800946 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700947 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800948 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800949 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700950
Ben Cahill9fbab512007-11-29 11:09:47 +0800951 /*
952 * The same handler is used for both the REPLY to a discrete
953 * statistics request from the host as well as for the periodic
954 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700955 */
Abhijeet Kolekar17f36fc2010-04-16 10:03:54 -0700956 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800957 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700958
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800959 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800960 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700961
Ben Cahill9fbab512007-11-29 11:09:47 +0800962 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800963 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700964}
965
Zhu Yib481de92007-09-25 17:54:57 -0700966/************************** RX-FUNCTIONS ****************************/
967/*
968 * Rx theory of operation
969 *
970 * The host allocates 32 DMA target addresses and passes the host address
971 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
972 * 0 to 31
973 *
974 * Rx Queue Indexes
975 * The host/firmware share two index registers for managing the Rx buffers.
976 *
977 * The READ index maps to the first position that the firmware may be writing
978 * to -- the driver can read up to (but not including) this position and get
979 * good data.
980 * The READ index is managed by the firmware once the card is enabled.
981 *
982 * The WRITE index maps to the last position the driver has read from -- the
983 * position preceding WRITE is the last slot the firmware can place a packet.
984 *
985 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
986 * WRITE = READ.
987 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800988 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -0700989 * INDEX position, and WRITE to the last (READ - 1 wrapped)
990 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800991 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -0700992 * and fire the RX interrupt. The driver can then query the READ index and
993 * process as many packets as possible, moving the WRITE index forward as it
994 * resets the Rx queue buffers with new memory.
995 *
996 * The management in the driver is as follows:
997 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
998 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +0800999 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001000 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07001001 * iwl->rxq is replenished and the READ INDEX is updated (updating the
1002 * 'processed' and 'read' driver indexes as well)
1003 * + A received packet is processed and handed to the kernel network stack,
1004 * detached from the iwl->rxq. The driver 'processed' index is updated.
1005 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1006 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1007 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
1008 * were enough free buffers and RX_STALLED is set it is cleared.
1009 *
1010 *
1011 * Driver sequence:
1012 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001013 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001014 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08001015 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07001016 * queue, updates firmware pointers, and updates
1017 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001018 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07001019 *
1020 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001021 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07001022 * READ INDEX, detaching the SKB from the pool.
1023 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001024 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07001025 * slots.
1026 * ...
1027 *
1028 */
1029
1030/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001031 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07001032 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001033static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001034 dma_addr_t dma_addr)
1035{
1036 return cpu_to_le32((u32)dma_addr);
1037}
1038
1039/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001040 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07001041 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001042 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07001043 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08001044 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07001045 *
1046 * This moves the 'write' index forward to catch up with 'processed', and
1047 * also updates the memory address in the firmware to reference the new
1048 * target buffer.
1049 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001050static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001051{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001052 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001053 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001054 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001055 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001056 int write;
Zhu Yib481de92007-09-25 17:54:57 -07001057
1058 spin_lock_irqsave(&rxq->lock, flags);
1059 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001060 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001061 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001062 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001063 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001064 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001065
1066 /* Point to Rx buffer via next RBD in circular buffer */
Zhu Yi2f301222009-10-09 17:19:45 +08001067 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
Zhu Yib481de92007-09-25 17:54:57 -07001068 rxq->queue[rxq->write] = rxb;
1069 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1070 rxq->free_count--;
1071 }
1072 spin_unlock_irqrestore(&rxq->lock, flags);
1073 /* If the pre-allocated buffer pool is dropping low, schedule to
1074 * refill it */
1075 if (rxq->free_count <= RX_LOW_WATERMARK)
1076 queue_work(priv->workqueue, &priv->rx_replenish);
1077
1078
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001079 /* If we've added more space for the firmware to place data, tell it.
1080 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001081 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001082 || (abs(rxq->write - rxq->read) > 7)) {
1083 spin_lock_irqsave(&rxq->lock, flags);
1084 rxq->need_update = 1;
1085 spin_unlock_irqrestore(&rxq->lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001086 iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001087 }
Zhu Yib481de92007-09-25 17:54:57 -07001088}
1089
1090/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001091 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001092 *
1093 * When moving to rx_free an SKB is allocated for the slot.
1094 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001095 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001096 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001097 */
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001098static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001099{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001100 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001101 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001102 struct iwl_rx_mem_buffer *rxb;
Zhu Yi2f301222009-10-09 17:19:45 +08001103 struct page *page;
Zhu Yib481de92007-09-25 17:54:57 -07001104 unsigned long flags;
Zhu Yi29b1b262009-10-23 13:42:25 -07001105 gfp_t gfp_mask = priority;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001106
1107 while (1) {
1108 spin_lock_irqsave(&rxq->lock, flags);
1109
1110 if (list_empty(&rxq->rx_used)) {
1111 spin_unlock_irqrestore(&rxq->lock, flags);
1112 return;
1113 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001114 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001115
Reinette Chatref82a9242009-09-17 10:43:56 -07001116 if (rxq->free_count > RX_LOW_WATERMARK)
Zhu Yi29b1b262009-10-23 13:42:25 -07001117 gfp_mask |= __GFP_NOWARN;
Zhu Yi2f301222009-10-09 17:19:45 +08001118
1119 if (priv->hw_params.rx_page_order > 0)
Zhu Yi29b1b262009-10-23 13:42:25 -07001120 gfp_mask |= __GFP_COMP;
Zhu Yi2f301222009-10-09 17:19:45 +08001121
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001122 /* Alloc a new receive buffer */
Zhu Yi29b1b262009-10-23 13:42:25 -07001123 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
Zhu Yi2f301222009-10-09 17:19:45 +08001124 if (!page) {
Zhu Yib481de92007-09-25 17:54:57 -07001125 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001126 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1127 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1128 net_ratelimit())
1129 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1130 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1131 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001132 /* We don't reschedule replenish work here -- we will
1133 * call the restock method and if it still needs
1134 * more buffers it will schedule replenish */
1135 break;
1136 }
Zhu Yi12342c42007-12-20 11:27:32 +08001137
Reinette Chatrede0bd502009-09-11 10:38:12 -07001138 spin_lock_irqsave(&rxq->lock, flags);
1139 if (list_empty(&rxq->rx_used)) {
1140 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001141 __free_pages(page, priv->hw_params.rx_page_order);
Reinette Chatrede0bd502009-09-11 10:38:12 -07001142 return;
1143 }
1144 element = rxq->rx_used.next;
1145 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1146 list_del(element);
1147 spin_unlock_irqrestore(&rxq->lock, flags);
1148
Zhu Yi2f301222009-10-09 17:19:45 +08001149 rxb->page = page;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001150 /* Get physical address of RB/SKB */
Zhu Yi2f301222009-10-09 17:19:45 +08001151 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1152 PAGE_SIZE << priv->hw_params.rx_page_order,
1153 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001154
1155 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001156
Zhu Yib481de92007-09-25 17:54:57 -07001157 list_add_tail(&rxb->list, &rxq->rx_free);
1158 rxq->free_count++;
Zhu Yi2f301222009-10-09 17:19:45 +08001159 priv->alloc_rxb_page++;
1160
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001161 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001162 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001163}
1164
Reinette Chatredf833b12009-04-21 10:55:48 -07001165void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1166{
1167 unsigned long flags;
1168 int i;
1169 spin_lock_irqsave(&rxq->lock, flags);
1170 INIT_LIST_HEAD(&rxq->rx_free);
1171 INIT_LIST_HEAD(&rxq->rx_used);
1172 /* Fill the rx_used queue with _all_ of the Rx buffers */
1173 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1174 /* In the reset function, these buffers may have been allocated
1175 * to an SKB, so we need to unmap and free potential storage */
Zhu Yi2f301222009-10-09 17:19:45 +08001176 if (rxq->pool[i].page != NULL) {
1177 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1178 PAGE_SIZE << priv->hw_params.rx_page_order,
1179 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001180 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001181 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001182 }
1183 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1184 }
1185
1186 /* Set us so that we have processed and used all buffers, but have
1187 * not restocked the Rx queue with fresh buffers */
1188 rxq->read = rxq->write = 0;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001189 rxq->write_actual = 0;
Zhu Yi2f301222009-10-09 17:19:45 +08001190 rxq->free_count = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001191 spin_unlock_irqrestore(&rxq->lock, flags);
1192}
Reinette Chatredf833b12009-04-21 10:55:48 -07001193
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001194void iwl3945_rx_replenish(void *data)
1195{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001196 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001197 unsigned long flags;
1198
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001199 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001200
1201 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001202 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001203 spin_unlock_irqrestore(&priv->lock, flags);
1204}
1205
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001206static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1207{
1208 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1209
1210 iwl3945_rx_queue_restock(priv);
1211}
1212
1213
Reinette Chatredf833b12009-04-21 10:55:48 -07001214/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1215 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1216 * This free routine walks the list of POOL entries and if SKB is set to
1217 * non NULL it is unmapped and freed
1218 */
1219static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1220{
1221 int i;
1222 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
Zhu Yi2f301222009-10-09 17:19:45 +08001223 if (rxq->pool[i].page != NULL) {
1224 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1225 PAGE_SIZE << priv->hw_params.rx_page_order,
1226 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001227 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001228 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001229 }
1230 }
1231
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001232 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
1233 rxq->dma_addr);
1234 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1235 rxq->rb_stts, rxq->rb_stts_dma);
Reinette Chatredf833b12009-04-21 10:55:48 -07001236 rxq->bd = NULL;
1237 rxq->rb_stts = NULL;
1238}
Reinette Chatredf833b12009-04-21 10:55:48 -07001239
1240
Zhu Yib481de92007-09-25 17:54:57 -07001241/* Convert linear signal-to-noise ratio into dB */
1242static u8 ratio2dB[100] = {
1243/* 0 1 2 3 4 5 6 7 8 9 */
1244 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1245 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1246 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1247 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1248 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1249 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1250 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1251 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1252 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1253 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1254};
1255
1256/* Calculates a relative dB value from a ratio of linear
1257 * (i.e. not dB) signal levels.
1258 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001260{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001261 /* 1000:1 or higher just report as 60 dB */
1262 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001263 return 60;
1264
Adrian Bunk221c80c2008-02-02 23:19:01 +02001265 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001266 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001267 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001268 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001269
1270 /* We shouldn't see this */
1271 if (sig_ratio < 1)
1272 return 0;
1273
1274 /* Use table for ratios 1:1 - 99:1 */
1275 return (int)ratio2dB[sig_ratio];
1276}
1277
Zhu Yib481de92007-09-25 17:54:57 -07001278/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001279 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001280 *
1281 * Uses the priv->rx_handlers callback function array to invoke
1282 * the appropriate handlers, including command responses,
1283 * frame-received notifications, and other notifications.
1284 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001285static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001286{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001287 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001288 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001289 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001290 u32 r, i;
1291 int reclaim;
1292 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001293 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001294 u32 count = 8;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001295 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001296
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001297 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1298 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001299 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001300 i = rxq->read;
1301
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001302 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -07001303 total_empty = r - rxq->write_actual;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001304 if (total_empty < 0)
1305 total_empty += RX_QUEUE_SIZE;
1306
1307 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001308 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001309 /* Rx interrupt, but nothing sent from uCode */
1310 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001311 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001312
1313 while (i != r) {
1314 rxb = rxq->queue[i];
1315
Ben Cahill9fbab512007-11-29 11:09:47 +08001316 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001317 * then a bug has been introduced in the queue refilling
1318 * routines -- catch it here */
1319 BUG_ON(rxb == NULL);
1320
1321 rxq->queue[i] = NULL;
1322
Zhu Yi2f301222009-10-09 17:19:45 +08001323 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1324 PAGE_SIZE << priv->hw_params.rx_page_order,
1325 PCI_DMA_FROMDEVICE);
1326 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001327
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001328 trace_iwlwifi_dev_rx(priv, pkt,
1329 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK);
1330
Zhu Yib481de92007-09-25 17:54:57 -07001331 /* Reclaim a command buffer only if this packet is a response
1332 * to a (driver-originated) command.
1333 * If the packet (e.g. Rx frame) originated from uCode,
1334 * there is no command buffer to reclaim.
1335 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1336 * but apparently a few don't get set; catch them here. */
1337 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1338 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1339 (pkt->hdr.cmd != REPLY_TX);
1340
1341 /* Based on type of command response or notification,
1342 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001343 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001344 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001345 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001346 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001347 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -07001348 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001349 } else {
1350 /* No handling needed */
Zhu Yi2f301222009-10-09 17:19:45 +08001351 IWL_DEBUG_RX(priv,
1352 "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001353 r, i, get_cmd_string(pkt->hdr.cmd),
1354 pkt->hdr.cmd);
1355 }
1356
Zhu Yi29b1b262009-10-23 13:42:25 -07001357 /*
1358 * XXX: After here, we should always check rxb->page
1359 * against NULL before touching it or its virtual
1360 * memory (pkt). Because some rx_handler might have
1361 * already taken or freed the pages.
1362 */
1363
Zhu Yib481de92007-09-25 17:54:57 -07001364 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001365 /* Invoke any callbacks, transfer the buffer to caller,
1366 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001367 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001368 if (rxb->page)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001369 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001370 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001371 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001372 }
1373
Zhu Yi73005152009-10-23 13:42:32 -07001374 /* Reuse the page if possible. For notification packets and
1375 * SKBs that fail to Rx correctly, add them back into the
1376 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001377 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001378 if (rxb->page != NULL) {
1379 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1380 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1381 PCI_DMA_FROMDEVICE);
1382 list_add_tail(&rxb->list, &rxq->rx_free);
1383 rxq->free_count++;
1384 } else
1385 list_add_tail(&rxb->list, &rxq->rx_used);
1386
Zhu Yib481de92007-09-25 17:54:57 -07001387 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001388
Zhu Yib481de92007-09-25 17:54:57 -07001389 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001390 /* If there are a lot of unused frames,
1391 * restock the Rx queue so ucode won't assert. */
1392 if (fill_rx) {
1393 count++;
1394 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001395 rxq->read = i;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001396 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001397 count = 0;
1398 }
1399 }
Zhu Yib481de92007-09-25 17:54:57 -07001400 }
1401
1402 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001403 rxq->read = i;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001404 if (fill_rx)
1405 iwl3945_rx_replenish_now(priv);
1406 else
1407 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001408}
1409
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001410/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001411static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001412{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001413 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001414 synchronize_irq(priv->pci_dev->irq);
1415 tasklet_kill(&priv->irq_tasklet);
1416}
1417
Zhu Yib481de92007-09-25 17:54:57 -07001418static const char *desc_lookup(int i)
1419{
1420 switch (i) {
1421 case 1:
1422 return "FAIL";
1423 case 2:
1424 return "BAD_PARAM";
1425 case 3:
1426 return "BAD_CHECKSUM";
1427 case 4:
1428 return "NMI_INTERRUPT";
1429 case 5:
1430 return "SYSASSERT";
1431 case 6:
1432 return "FATAL_ERROR";
1433 }
1434
1435 return "UNKNOWN";
1436}
1437
1438#define ERROR_START_OFFSET (1 * sizeof(u32))
1439#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1440
Reinette Chatreb7a79402009-09-25 14:24:23 -07001441void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001442{
1443 u32 i;
1444 u32 desc, time, count, base, data1;
1445 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001446
1447 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001449 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001450 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001451 return;
1452 }
1453
Zhu Yib481de92007-09-25 17:54:57 -07001454
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001455 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001456
1457 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001458 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1459 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1460 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001461 }
1462
Winkler, Tomas15b16872008-12-19 10:37:33 +08001463 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001464 "ilink1 nmiPC Line\n");
1465 for (i = ERROR_START_OFFSET;
1466 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1467 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001468 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001469 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001470 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001471 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001472 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001473 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001474 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001475 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001476 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001477 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001478 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001479 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001480 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001481
Winkler, Tomas15b16872008-12-19 10:37:33 +08001482 IWL_ERR(priv,
1483 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1484 desc_lookup(desc), desc, time, blink1, blink2,
1485 ilink1, ilink2, data1);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001486 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1487 0, blink1, blink2, ilink1, ilink2);
Zhu Yib481de92007-09-25 17:54:57 -07001488 }
Zhu Yib481de92007-09-25 17:54:57 -07001489}
1490
Ben Cahillf58177b2007-11-29 11:09:43 +08001491#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001492
1493/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001494 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001495 *
Zhu Yib481de92007-09-25 17:54:57 -07001496 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001497static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1498 u32 num_events, u32 mode,
1499 int pos, char **buf, size_t bufsz)
Zhu Yib481de92007-09-25 17:54:57 -07001500{
1501 u32 i;
1502 u32 base; /* SRAM byte address of event log header */
1503 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1504 u32 ptr; /* SRAM byte address of log data */
1505 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001506 unsigned long reg_flags;
Zhu Yib481de92007-09-25 17:54:57 -07001507
1508 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001509 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001510
1511 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1512
1513 if (mode == 0)
1514 event_size = 2 * sizeof(u32);
1515 else
1516 event_size = 3 * sizeof(u32);
1517
1518 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1519
Ben Cahille5854472009-11-06 14:52:58 -08001520 /* Make sure device is powered up for SRAM reads */
1521 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1522 iwl_grab_nic_access(priv);
1523
1524 /* Set starting address; reads will auto-increment */
1525 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1526 rmb();
1527
Zhu Yib481de92007-09-25 17:54:57 -07001528 /* "time" is actually "data" for mode 0 (no timestamp).
1529 * place event id # at far right for easier visual parsing. */
1530 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001531 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1532 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001533 if (mode == 0) {
1534 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001535 if (bufsz) {
1536 pos += scnprintf(*buf + pos, bufsz - pos,
1537 "0x%08x:%04u\n",
1538 time, ev);
1539 } else {
1540 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1541 trace_iwlwifi_dev_ucode_event(priv, 0,
1542 time, ev);
1543 }
Winkler, Tomas15b16872008-12-19 10:37:33 +08001544 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001545 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001546 if (bufsz) {
1547 pos += scnprintf(*buf + pos, bufsz - pos,
1548 "%010u:0x%08x:%04u\n",
1549 time, data, ev);
1550 } else {
1551 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1552 time, data, ev);
1553 trace_iwlwifi_dev_ucode_event(priv, time,
1554 data, ev);
1555 }
Zhu Yib481de92007-09-25 17:54:57 -07001556 }
1557 }
Ben Cahille5854472009-11-06 14:52:58 -08001558
1559 /* Allow device to power down */
1560 iwl_release_nic_access(priv);
1561 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001562 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001563}
1564
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001565/**
1566 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1567 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001568static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001569 u32 num_wraps, u32 next_entry,
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001570 u32 size, u32 mode,
1571 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001572{
1573 /*
1574 * display the newest DEFAULT_LOG_ENTRIES entries
1575 * i.e the entries just before the next ont that uCode would fill.
1576 */
1577 if (num_wraps) {
1578 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001579 pos = iwl3945_print_event_log(priv,
1580 capacity - (size - next_entry),
1581 size - next_entry, mode,
1582 pos, buf, bufsz);
1583 pos = iwl3945_print_event_log(priv, 0,
1584 next_entry, mode,
1585 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001586 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001587 pos = iwl3945_print_event_log(priv, next_entry - size,
1588 size, mode,
1589 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001590 } else {
1591 if (next_entry < size)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001592 pos = iwl3945_print_event_log(priv, 0,
1593 next_entry, mode,
1594 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001595 else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001596 pos = iwl3945_print_event_log(priv, next_entry - size,
1597 size, mode,
1598 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001599 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001600 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001601}
1602
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001603#define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1604
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001605int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1606 char **buf, bool display)
Zhu Yib481de92007-09-25 17:54:57 -07001607{
Zhu Yib481de92007-09-25 17:54:57 -07001608 u32 base; /* SRAM byte address of event log header */
1609 u32 capacity; /* event log capacity in # entries */
1610 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1611 u32 num_wraps; /* # times uCode wrapped to top of log */
1612 u32 next_entry; /* index of next entry to be written by uCode */
1613 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001614 int pos = 0;
1615 size_t bufsz = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001616
1617 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001618 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001619 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001620 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001621 }
1622
Zhu Yib481de92007-09-25 17:54:57 -07001623 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001624 capacity = iwl_read_targ_mem(priv, base);
1625 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1626 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1627 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001628
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001629 if (capacity > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001630 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001631 capacity, priv->cfg->max_event_log_size);
1632 capacity = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001633 }
1634
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001635 if (next_entry > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001636 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001637 next_entry, priv->cfg->max_event_log_size);
1638 next_entry = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001639 }
1640
Zhu Yib481de92007-09-25 17:54:57 -07001641 size = num_wraps ? capacity : next_entry;
1642
1643 /* bail out if nothing in log */
1644 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001645 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001646 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001647 }
1648
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001649#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001650 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001651 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1652 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1653#else
1654 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1655 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1656#endif
1657
1658 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1659 size);
Zhu Yib481de92007-09-25 17:54:57 -07001660
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001661#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001662 if (display) {
1663 if (full_log)
1664 bufsz = capacity * 48;
1665 else
1666 bufsz = size * 48;
1667 *buf = kmalloc(bufsz, GFP_KERNEL);
1668 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001669 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001670 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001671 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1672 /* if uCode has wrapped back to top of log,
1673 * start at the oldest entry,
1674 * i.e the next one that uCode would fill.
1675 */
1676 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001677 pos = iwl3945_print_event_log(priv, next_entry,
1678 capacity - next_entry, mode,
1679 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001680
1681 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001682 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1683 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001684 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001685 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1686 next_entry, size, mode,
1687 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001688#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001689 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1690 next_entry, size, mode,
1691 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001692#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001693 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001694}
1695
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001696static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001697{
1698 u32 inta, handled = 0;
1699 u32 inta_fh;
1700 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001701#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001702 u32 inta_mask;
1703#endif
1704
1705 spin_lock_irqsave(&priv->lock, flags);
1706
1707 /* Ack/clear/reset pending uCode interrupts.
1708 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1709 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001710 inta = iwl_read32(priv, CSR_INT);
1711 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001712
1713 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1714 * Any new interrupts that happen after this, either while we're
1715 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001716 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1717 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001718
Samuel Ortizd08853a2009-01-23 13:45:17 -08001719#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001720 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001721 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001722 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001723 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001724 inta, inta_mask, inta_fh);
1725 }
1726#endif
1727
Zhu Yi2f301222009-10-09 17:19:45 +08001728 spin_unlock_irqrestore(&priv->lock, flags);
1729
Zhu Yib481de92007-09-25 17:54:57 -07001730 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1731 * atomic, make sure that inta covers all the interrupts that
1732 * we've discovered, even if FH interrupt came in just after
1733 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001734 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001735 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001736 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001737 inta |= CSR_INT_BIT_FH_TX;
1738
1739 /* Now service all interrupt bits discovered above. */
1740 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001741 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001742
1743 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001744 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001745
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001746 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001747 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001748
1749 handled |= CSR_INT_BIT_HW_ERR;
1750
Zhu Yib481de92007-09-25 17:54:57 -07001751 return;
1752 }
1753
Samuel Ortizd08853a2009-01-23 13:45:17 -08001754#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001755 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001756 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001757 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001758 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001759 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001760 priv->isr_stats.sch++;
1761 }
Zhu Yib481de92007-09-25 17:54:57 -07001762
1763 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001764 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001765 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001766 priv->isr_stats.alive++;
1767 }
Zhu Yib481de92007-09-25 17:54:57 -07001768 }
1769#endif
1770 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001771 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001772
Zhu Yib481de92007-09-25 17:54:57 -07001773 /* Error detected by uCode */
1774 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001775 IWL_ERR(priv, "Microcode SW error detected. "
1776 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001777 priv->isr_stats.sw++;
1778 priv->isr_stats.sw_err = inta;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001779 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001780 handled |= CSR_INT_BIT_SW_ERR;
1781 }
1782
1783 /* uCode wakes up after power-down sleep */
1784 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001785 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001786 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001787 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1788 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1789 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1790 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1791 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1792 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001793
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001794 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001795 handled |= CSR_INT_BIT_WAKEUP;
1796 }
1797
1798 /* All uCode command responses, including Tx command responses,
1799 * Rx "responses" (frame-received notification), and other
1800 * notifications from uCode come through here*/
1801 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001802 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001803 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001804 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1805 }
1806
1807 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001808 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001809 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001810
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001811 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001812 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1813 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001814 handled |= CSR_INT_BIT_FH_TX;
1815 }
1816
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001817 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001818 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001819 priv->isr_stats.unhandled++;
1820 }
Zhu Yib481de92007-09-25 17:54:57 -07001821
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001822 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001823 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001824 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001825 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001826 }
1827
1828 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001829 /* only Re-enable if disabled by irq */
1830 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001831 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001832
Samuel Ortizd08853a2009-01-23 13:45:17 -08001833#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001834 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001835 inta = iwl_read32(priv, CSR_INT);
1836 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1837 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001838 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001839 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1840 }
1841#endif
Zhu Yib481de92007-09-25 17:54:57 -07001842}
1843
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001844static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001845 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001846 u8 is_active, u8 n_probes,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001847 struct iwl3945_scan_channel *scan_ch,
1848 struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07001849{
Johannes Berg4e05c232009-06-19 13:52:44 -07001850 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001851 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001852 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001853 u16 passive_dwell = 0;
1854 u16 active_dwell = 0;
1855 int added, i;
1856
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001857 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001858 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001859 return 0;
1860
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001861 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001862 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Zhu Yib481de92007-09-25 17:54:57 -07001863
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001864 if (passive_dwell <= active_dwell)
1865 passive_dwell = active_dwell + 1;
1866
Johannes Berg4e05c232009-06-19 13:52:44 -07001867 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1868 chan = priv->scan_request->channels[i];
1869
1870 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001871 continue;
1872
Johannes Berg4e05c232009-06-19 13:52:44 -07001873 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001874
Samuel Ortize6148912009-01-23 13:45:15 -08001875 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001876 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001877 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001878 scan_ch->channel);
1879 continue;
1880 }
1881
Zhu Yib481de92007-09-25 17:54:57 -07001882 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1883 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001884 /* If passive , set up for auto-switch
1885 * and use long active_dwell time.
1886 */
1887 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001888 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001889 scan_ch->type = 0; /* passive */
1890 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1891 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1892 } else {
1893 scan_ch->type = 1; /* active */
1894 }
1895
1896 /* Set direct probe bits. These may be used both for active
1897 * scan channels (probes gets sent right away),
1898 * or for passive channels (probes get se sent only after
1899 * hearing clear Rx packet).*/
1900 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1901 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001902 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001903 } else {
1904 /* uCode v1 does not allow setting direct probe bits on
1905 * passive channel. */
1906 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001907 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001908 }
Zhu Yib481de92007-09-25 17:54:57 -07001909
Ben Cahill9fbab512007-11-29 11:09:47 +08001910 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001911 scan_ch->tpc.dsp_atten = 110;
1912 /* scan_pwr_info->tpc.dsp_atten; */
1913
1914 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001915 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001916 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1917 else {
1918 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1919 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001920 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001921 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001922 */
1923 }
1924
Tomas Winklere1623442009-01-27 14:27:56 -08001925 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001926 scan_ch->channel,
1927 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1928 (scan_ch->type & 1) ?
1929 active_dwell : passive_dwell);
1930
1931 scan_ch++;
1932 added++;
1933 }
1934
Frans Pop91dd6c22010-03-24 14:19:58 -07001935 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001936 return added;
1937}
1938
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001939static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001940 struct ieee80211_rate *rates)
1941{
1942 int i;
1943
Dan Carpenter8e1a53c2010-03-28 14:55:00 +03001944 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001945 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1946 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1947 rates[i].hw_value_short = i;
1948 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001949 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001950 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001951 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001952 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001953 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001954 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001955 }
Zhu Yib481de92007-09-25 17:54:57 -07001956 }
1957}
1958
Zhu Yib481de92007-09-25 17:54:57 -07001959/******************************************************************************
1960 *
1961 * uCode download functions
1962 *
1963 ******************************************************************************/
1964
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001965static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001966{
Tomas Winkler98c92212008-01-14 17:46:20 -08001967 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1968 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1969 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1970 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1971 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1972 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001973}
1974
1975/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001976 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001977 * looking at all data.
1978 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001979static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001980{
1981 u32 val;
1982 u32 save_len = len;
1983 int rc = 0;
1984 u32 errcnt;
1985
Tomas Winklere1623442009-01-27 14:27:56 -08001986 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001987
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001988 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001989 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07001990
1991 errcnt = 0;
1992 for (; len > 0; len -= sizeof(u32), image++) {
1993 /* read data comes through single port, auto-incr addr */
1994 /* NOTE: Use the debugless read so we don't flood kernel log
1995 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001996 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001997 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001998 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001999 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2000 save_len - len, val, le32_to_cpu(*image));
2001 rc = -EIO;
2002 errcnt++;
2003 if (errcnt >= 20)
2004 break;
2005 }
2006 }
2007
Zhu Yib481de92007-09-25 17:54:57 -07002008
2009 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002010 IWL_DEBUG_INFO(priv,
2011 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002012
2013 return rc;
2014}
2015
2016
2017/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002018 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002019 * using sample data 100 bytes apart. If these sample points are good,
2020 * it's a pretty good bet that everything between them is good, too.
2021 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002022static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002023{
2024 u32 val;
2025 int rc = 0;
2026 u32 errcnt = 0;
2027 u32 i;
2028
Tomas Winklere1623442009-01-27 14:27:56 -08002029 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002030
Zhu Yib481de92007-09-25 17:54:57 -07002031 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2032 /* read data comes through single port, auto-incr addr */
2033 /* NOTE: Use the debugless read so we don't flood kernel log
2034 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002035 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002036 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002037 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002038 if (val != le32_to_cpu(*image)) {
2039#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002040 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002041 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2042 i, val, *image);
2043#endif
2044 rc = -EIO;
2045 errcnt++;
2046 if (errcnt >= 3)
2047 break;
2048 }
2049 }
2050
Zhu Yib481de92007-09-25 17:54:57 -07002051 return rc;
2052}
2053
2054
2055/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002056 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002057 * and verify its contents
2058 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002059static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002060{
2061 __le32 *image;
2062 u32 len;
2063 int rc = 0;
2064
2065 /* Try bootstrap */
2066 image = (__le32 *)priv->ucode_boot.v_addr;
2067 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002068 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002069 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002070 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002071 return 0;
2072 }
2073
2074 /* Try initialize */
2075 image = (__le32 *)priv->ucode_init.v_addr;
2076 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002077 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002078 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002079 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002080 return 0;
2081 }
2082
2083 /* Try runtime/protocol */
2084 image = (__le32 *)priv->ucode_code.v_addr;
2085 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002086 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002087 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002088 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002089 return 0;
2090 }
2091
Winkler, Tomas15b16872008-12-19 10:37:33 +08002092 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002093
Ben Cahill9fbab512007-11-29 11:09:47 +08002094 /* Since nothing seems to match, show first several data entries in
2095 * instruction SRAM, so maybe visual inspection will give a clue.
2096 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002097 image = (__le32 *)priv->ucode_boot.v_addr;
2098 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002099 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002100
2101 return rc;
2102}
2103
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002104static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002105{
2106 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002107 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002108}
2109
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002110#define IWL3945_UCODE_GET(item) \
2111static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
2112{ \
2113 return le32_to_cpu(ucode->u.v1.item); \
2114}
2115
2116static u32 iwl3945_ucode_get_header_size(u32 api_ver)
2117{
Johannes Berg22adba22010-04-28 12:09:14 -07002118 return 24;
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002119}
2120
2121static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
2122{
2123 return (u8 *) ucode->u.v1.data;
2124}
2125
2126IWL3945_UCODE_GET(inst_size);
2127IWL3945_UCODE_GET(data_size);
2128IWL3945_UCODE_GET(init_size);
2129IWL3945_UCODE_GET(init_data_size);
2130IWL3945_UCODE_GET(boot_size);
2131
Zhu Yib481de92007-09-25 17:54:57 -07002132/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002133 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002134 *
2135 * Copy into buffers for card to fetch via bus-mastering
2136 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002137static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002138{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002139 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002140 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002141 const struct firmware *ucode_raw;
2142 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002143 const char *name_pre = priv->cfg->fw_name_pre;
2144 const unsigned int api_max = priv->cfg->ucode_api_max;
2145 const unsigned int api_min = priv->cfg->ucode_api_min;
2146 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002147 u8 *src;
2148 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002149 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002150
2151 /* Ask kernel firmware_class module to get the boot firmware off disk.
2152 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002153 for (index = api_max; index >= api_min; index--) {
2154 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2155 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2156 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002157 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002158 buf, ret);
2159 if (ret == -ENOENT)
2160 continue;
2161 else
2162 goto error;
2163 } else {
2164 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002165 IWL_ERR(priv, "Loaded firmware %s, "
2166 "which is deprecated. "
2167 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002168 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002169 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2170 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002171 buf, ucode_raw->size);
2172 break;
2173 }
Zhu Yib481de92007-09-25 17:54:57 -07002174 }
2175
Reinette Chatrea0987a82008-12-02 12:14:06 -08002176 if (ret < 0)
2177 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002178
2179 /* Make sure that we got at least our header! */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002180 if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002181 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002182 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002183 goto err_release;
2184 }
2185
2186 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002187 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002188
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002189 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002190 api_ver = IWL_UCODE_API(priv->ucode_ver);
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002191 inst_size = iwl3945_ucode_get_inst_size(ucode);
2192 data_size = iwl3945_ucode_get_data_size(ucode);
2193 init_size = iwl3945_ucode_get_init_size(ucode);
2194 init_data_size = iwl3945_ucode_get_init_data_size(ucode);
2195 boot_size = iwl3945_ucode_get_boot_size(ucode);
2196 src = iwl3945_ucode_get_data(ucode);
Zhu Yib481de92007-09-25 17:54:57 -07002197
Reinette Chatrea0987a82008-12-02 12:14:06 -08002198 /* api_ver should match the api version forming part of the
2199 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002200 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002201
2202 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002203 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002204 "Driver supports v%u, firmware is v%u.\n",
2205 api_max, api_ver);
2206 priv->ucode_ver = 0;
2207 ret = -EINVAL;
2208 goto err_release;
2209 }
2210 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002211 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002212 "got %u. New firmware can be obtained "
2213 "from http://www.intellinuxwireless.org.\n",
2214 api_max, api_ver);
2215
Tomas Winkler978785a2008-12-19 10:37:31 +08002216 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2217 IWL_UCODE_MAJOR(priv->ucode_ver),
2218 IWL_UCODE_MINOR(priv->ucode_ver),
2219 IWL_UCODE_API(priv->ucode_ver),
2220 IWL_UCODE_SERIAL(priv->ucode_ver));
2221
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07002222 snprintf(priv->hw->wiphy->fw_version,
2223 sizeof(priv->hw->wiphy->fw_version),
2224 "%u.%u.%u.%u",
2225 IWL_UCODE_MAJOR(priv->ucode_ver),
2226 IWL_UCODE_MINOR(priv->ucode_ver),
2227 IWL_UCODE_API(priv->ucode_ver),
2228 IWL_UCODE_SERIAL(priv->ucode_ver));
2229
Tomas Winklere1623442009-01-27 14:27:56 -08002230 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002231 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002232 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2233 inst_size);
2234 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2235 data_size);
2236 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2237 init_size);
2238 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2239 init_data_size);
2240 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2241 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002242
Reinette Chatrea0987a82008-12-02 12:14:06 -08002243
Zhu Yib481de92007-09-25 17:54:57 -07002244 /* Verify size of file vs. image size info in file's header */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002245 if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002246 inst_size + data_size + init_size +
2247 init_data_size + boot_size) {
2248
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002249 IWL_DEBUG_INFO(priv,
2250 "uCode file size %zd does not match expected size\n",
2251 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002252 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002253 goto err_release;
2254 }
2255
2256 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002257 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002258 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002259 inst_size);
2260 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002261 goto err_release;
2262 }
2263
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002264 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002265 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002266 data_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_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002271 IWL_DEBUG_INFO(priv,
2272 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002273 init_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 (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002278 IWL_DEBUG_INFO(priv,
2279 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002280 init_data_size);
2281 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002282 goto err_release;
2283 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002284 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002285 IWL_DEBUG_INFO(priv,
2286 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002287 boot_size);
2288 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002289 goto err_release;
2290 }
2291
2292 /* Allocate ucode buffers for card's bus-master loading ... */
2293
2294 /* Runtime instructions and 2 copies of data:
2295 * 1) unmodified from disk
2296 * 2) backup cache for save/restore during power-downs */
2297 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002298 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002299
2300 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002301 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002302
2303 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002304 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002305
2306 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002307 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002308 goto err_pci_alloc;
2309
Tomas Winkler90e759d2007-11-29 11:09:41 +08002310 /* Initialization instructions and data */
2311 if (init_size && init_data_size) {
2312 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002313 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002314
2315 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002316 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002317
2318 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2319 goto err_pci_alloc;
2320 }
2321
2322 /* Bootstrap (instructions only, no data) */
2323 if (boot_size) {
2324 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002325 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002326
2327 if (!priv->ucode_boot.v_addr)
2328 goto err_pci_alloc;
2329 }
2330
Zhu Yib481de92007-09-25 17:54:57 -07002331 /* Copy images into buffers for card's bus-master reads ... */
2332
2333 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002334 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002335 IWL_DEBUG_INFO(priv,
2336 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002337 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002338 src += len;
2339
Tomas Winklere1623442009-01-27 14:27:56 -08002340 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002341 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2342
2343 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002344 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002345 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002346 IWL_DEBUG_INFO(priv,
2347 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002348 memcpy(priv->ucode_data.v_addr, src, len);
2349 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002350 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002351
2352 /* Initialization instructions (3rd block) */
2353 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002354 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002355 IWL_DEBUG_INFO(priv,
2356 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002357 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002358 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002359 }
2360
2361 /* Initialization data (4th block) */
2362 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002363 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002364 IWL_DEBUG_INFO(priv,
2365 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002366 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002367 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002368 }
2369
2370 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002371 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002372 IWL_DEBUG_INFO(priv,
2373 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002374 memcpy(priv->ucode_boot.v_addr, src, len);
2375
2376 /* We have our copies now, allow OS release its copies */
2377 release_firmware(ucode_raw);
2378 return 0;
2379
2380 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002381 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002382 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002383 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002384
2385 err_release:
2386 release_firmware(ucode_raw);
2387
2388 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002389 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002390}
2391
2392
2393/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002394 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002395 *
2396 * Tell initialization uCode where to find runtime uCode.
2397 *
2398 * BSM registers initially contain pointers to initialization uCode.
2399 * We need to replace them to load runtime uCode inst and data,
2400 * and to save runtime data when powering down.
2401 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002402static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002403{
2404 dma_addr_t pinst;
2405 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002406
2407 /* bits 31:0 for 3945 */
2408 pinst = priv->ucode_code.p_addr;
2409 pdata = priv->ucode_data_backup.p_addr;
2410
Zhu Yib481de92007-09-25 17:54:57 -07002411 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002412 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2413 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2414 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002415 priv->ucode_data.len);
2416
Tomas Winklera96a27f2008-10-23 23:48:56 -07002417 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002418 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002419 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002420 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2421
Tomas Winklere1623442009-01-27 14:27:56 -08002422 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002423
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002424 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002425}
2426
2427/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002428 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002429 *
2430 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2431 *
Zhu Yib481de92007-09-25 17:54:57 -07002432 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002433 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002434static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002435{
2436 /* Check alive response for "valid" sign from uCode */
2437 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2438 /* We had an error bringing up the hardware, so take it
2439 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002440 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002441 goto restart;
2442 }
2443
2444 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2445 * This is a paranoid check, because we would not have gotten the
2446 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002447 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002448 /* Runtime instruction load was bad;
2449 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002450 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002451 goto restart;
2452 }
2453
2454 /* Send pointers to protocol/runtime uCode image ... init code will
2455 * load and launch runtime uCode, which will send us another "Alive"
2456 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002457 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002458 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002459 /* Runtime instruction load won't happen;
2460 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002461 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002462 goto restart;
2463 }
2464 return;
2465
2466 restart:
2467 queue_work(priv->workqueue, &priv->restart);
2468}
2469
Zhu Yib481de92007-09-25 17:54:57 -07002470/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002471 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002472 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002473 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002474 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002475static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002476{
Zhu Yib481de92007-09-25 17:54:57 -07002477 int thermal_spin = 0;
2478 u32 rfkill;
2479
Tomas Winklere1623442009-01-27 14:27:56 -08002480 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002481
2482 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2483 /* We had an error bringing up the hardware, so take it
2484 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002485 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002486 goto restart;
2487 }
2488
2489 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2490 * This is a paranoid check, because we would not have gotten the
2491 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002492 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002493 /* Runtime instruction load was bad;
2494 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002495 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002496 goto restart;
2497 }
2498
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002499 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002500 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002501
2502 if (rfkill & 0x1) {
2503 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002504 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002505 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002506 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002507 thermal_spin++;
2508 udelay(10);
2509 }
2510
2511 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002512 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002513 thermal_spin * 10);
2514 } else
2515 set_bit(STATUS_RF_KILL_HW, &priv->status);
2516
Ben Cahill9fbab512007-11-29 11:09:47 +08002517 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002518 set_bit(STATUS_ALIVE, &priv->status);
2519
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08002520 if (priv->cfg->ops->lib->recover_from_tx_stall) {
2521 /* Enable timer to monitor the driver queues */
2522 mod_timer(&priv->monitor_recover,
2523 jiffies +
2524 msecs_to_jiffies(priv->cfg->monitor_recover_period));
2525 }
2526
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002527 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002528 return;
2529
Johannes Berg36d68252008-05-15 12:55:26 +02002530 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002531
Johannes Berg470ab2d2010-01-21 11:23:30 -08002532 priv->active_rate = IWL_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07002533
Ben Cahill4d6ccbf2009-11-13 11:56:29 -08002534 iwl_power_update_mode(priv, true);
Zhu Yib481de92007-09-25 17:54:57 -07002535
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002536 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002537 struct iwl3945_rxon_cmd *active_rxon =
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002538 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07002539
Abhijeet Kolekar8a9b9922009-06-12 13:22:52 -07002540 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002541 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2542 } else {
2543 /* Initialize our rx_config data */
Johannes Berg1dda6d22010-04-29 04:43:06 -07002544 iwl_connection_init_rx_config(priv, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002545 }
2546
Ben Cahill9fbab512007-11-29 11:09:47 +08002547 /* Configure Bluetooth device coexistence support */
Johannes Berg65b52bd2010-04-13 01:04:31 -07002548 priv->cfg->ops->hcmd->send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002549
2550 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002551 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002552
Zhu Yib481de92007-09-25 17:54:57 -07002553 iwl3945_reg_txpower_periodic(priv);
2554
Johannes Berge932a602009-10-02 13:44:03 -07002555 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002556
Tomas Winklere1623442009-01-27 14:27:56 -08002557 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002558 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002559 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -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
Reinette Chatre7e246192010-02-18 22:58:32 -08002582 /* Station information will now be cleared in device */
Johannes Berg2c810cc2010-04-29 00:53:29 -07002583 iwl_clear_ucode_stations(priv);
2584 iwl_dealloc_bcast_station(priv);
Johannes Bergdb125c72010-05-07 01:49:15 -07002585 iwl_clear_driver_stations(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002586
2587 /* Unblock any waiting calls */
2588 wake_up_interruptible_all(&priv->wait_command_queue);
2589
Zhu Yib481de92007-09-25 17:54:57 -07002590 /* Wipe out the EXIT_PENDING status bit if we are not actually
2591 * exiting the module */
2592 if (!exit_pending)
2593 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2594
2595 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002596 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002597
2598 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002599 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002600 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002601 spin_unlock_irqrestore(&priv->lock, flags);
2602 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002603
2604 if (priv->mac80211_registered)
2605 ieee80211_stop_queues(priv->hw);
2606
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002607 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002608 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002609 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002610 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2611 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002612 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2613 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002614 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2615 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002616 goto exit;
2617 }
2618
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002619 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002620 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002621 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2622 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002623 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2624 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002625 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002626 STATUS_FW_ERROR |
2627 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2628 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002629
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002630 iwl3945_hw_txq_ctx_stop(priv);
2631 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002632
Ben Cahill309e7312009-11-06 14:53:03 -08002633 /* Power-down device's busmaster DMA clocks */
2634 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002635 udelay(5);
2636
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002637 /* Stop the device, and put it in low power state */
2638 priv->cfg->ops->lib->apm_ops.stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002639
Zhu Yib481de92007-09-25 17:54:57 -07002640 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002641 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002642
2643 if (priv->ibss_beacon)
2644 dev_kfree_skb(priv->ibss_beacon);
2645 priv->ibss_beacon = NULL;
2646
2647 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002648 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002649}
2650
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002651static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002652{
2653 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002654 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002655 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002656
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002657 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002658}
2659
2660#define MAX_HW_RESTARTS 5
2661
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002662static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002663{
2664 int rc, i;
2665
Johannes Berg2c810cc2010-04-29 00:53:29 -07002666 rc = iwl_alloc_bcast_station(priv, false);
2667 if (rc)
2668 return rc;
2669
Zhu Yib481de92007-09-25 17:54:57 -07002670 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002671 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002672 return -EIO;
2673 }
2674
Reinette Chatree903fbd2008-01-30 22:05:15 -08002675 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002676 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002677 return -EIO;
2678 }
2679
Zhu Yie655b9f2008-01-24 02:19:38 -08002680 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002681 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002682 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2683 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2684 else {
2685 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002686 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2687 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002688 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002689
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002690 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002691
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002692 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002693 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002694 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002695 return rc;
2696 }
2697
2698 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002699 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2700 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002701 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2702
2703 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002704 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002705 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002706
2707 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002708 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2709 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002710
2711 /* Copy original ucode data image from disk into backup cache.
2712 * This will be used to initialize the on-board processor's
2713 * data SRAM for a clean start when the runtime program first loads. */
2714 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08002715 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002716
Zhu Yie655b9f2008-01-24 02:19:38 -08002717 /* We return success when we resume from suspend and rf_kill is on. */
2718 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2719 return 0;
2720
Zhu Yib481de92007-09-25 17:54:57 -07002721 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2722
Zhu Yib481de92007-09-25 17:54:57 -07002723 /* load bootstrap state machine,
2724 * load bootstrap program into processor's memory,
2725 * prepare to load the "initialize" uCode */
Abhijeet Kolekar75a9a922010-02-26 15:17:01 -08002726 rc = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002727
2728 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002729 IWL_ERR(priv,
2730 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002731 continue;
2732 }
2733
2734 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002735 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002736
Tomas Winklere1623442009-01-27 14:27:56 -08002737 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002738
2739 return 0;
2740 }
2741
2742 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002743 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002744 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002745
2746 /* tried to restart and config the device for as long as our
2747 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002748 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002749 return -EIO;
2750}
2751
2752
2753/*****************************************************************************
2754 *
2755 * Workqueue callbacks
2756 *
2757 *****************************************************************************/
2758
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002759static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002760{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002761 struct iwl_priv *priv =
2762 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002763
2764 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2765 return;
2766
2767 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002768 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002769 mutex_unlock(&priv->mutex);
2770}
2771
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002772static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002773{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002774 struct iwl_priv *priv =
2775 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002776
2777 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2778 return;
2779
2780 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002781 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002782 mutex_unlock(&priv->mutex);
2783}
2784
Ben Cahill743cdf12009-10-09 13:20:26 -07002785/*
2786 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2787 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2788 * *is* readable even when device has been SW_RESET into low power mode
2789 * (e.g. during RF KILL).
2790 */
Helmut Schaa26635162009-01-15 09:38:44 +01002791static void iwl3945_rfkill_poll(struct work_struct *data)
2792{
2793 struct iwl_priv *priv =
Johannes Bergee525d12010-01-21 06:09:28 -08002794 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002795 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2796 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2797 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002798
Ben Cahill743cdf12009-10-09 13:20:26 -07002799 if (new_rfkill != old_rfkill) {
2800 if (new_rfkill)
2801 set_bit(STATUS_RF_KILL_HW, &priv->status);
2802 else
2803 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002804
Ben Cahill743cdf12009-10-09 13:20:26 -07002805 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002806
Ben Cahill743cdf12009-10-09 13:20:26 -07002807 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2808 new_rfkill ? "disable radio" : "enable radio");
2809 }
2810
2811 /* Keep this running, even if radio now enabled. This will be
2812 * cancelled in mac_start() if system decides to start again */
Johannes Bergee525d12010-01-21 06:09:28 -08002813 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01002814 round_jiffies_relative(2 * HZ));
2815
2816}
2817
Johannes Berg1dda6d22010-04-29 04:43:06 -07002818void iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07002819{
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002820 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002821 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002822 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002823 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002824 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002825 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07002826 struct ieee80211_conf *conf = NULL;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002827 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002828 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002829 bool is_active = false;
Zhu Yib481de92007-09-25 17:54:57 -07002830
2831 conf = ieee80211_get_hw_conf(priv->hw);
2832
Reinette Chatrefbc9f972009-05-15 16:13:46 -07002833 cancel_delayed_work(&priv->scan_check);
2834
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002835 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002836 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002837 goto done;
2838 }
2839
Tomas Winklera96a27f2008-10-23 23:48:56 -07002840 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07002841 * was given the chance to run... */
2842 if (!test_bit(STATUS_SCANNING, &priv->status))
2843 goto done;
2844
2845 /* This should never be called or scheduled if there is currently
2846 * a scan active in the hardware. */
2847 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002848 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
2849 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002850 goto done;
2851 }
2852
2853 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002854 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07002855 goto done;
2856 }
2857
2858 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002859 IWL_DEBUG_HC(priv,
2860 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002861 goto done;
2862 }
2863
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002864 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002865 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07002866 goto done;
2867 }
2868
2869 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002870 IWL_DEBUG_HC(priv,
2871 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002872 goto done;
2873 }
2874
Johannes Berg811ecc92010-04-06 04:12:41 -07002875 if (!priv->scan_cmd) {
2876 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2877 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2878 if (!priv->scan_cmd) {
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002879 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
Zhu Yib481de92007-09-25 17:54:57 -07002880 goto done;
2881 }
2882 }
Johannes Berg811ecc92010-04-06 04:12:41 -07002883 scan = priv->scan_cmd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002884 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002885
2886 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2887 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2888
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002889 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002890 u16 interval = 0;
2891 u32 extra;
2892 u32 suspend_time = 100;
2893 u32 scan_suspend_time = 100;
2894 unsigned long flags;
2895
Tomas Winklere1623442009-01-27 14:27:56 -08002896 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002897
2898 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg1dda6d22010-04-29 04:43:06 -07002899 interval = vif ? vif->bss_conf.beacon_int : 0;
Zhu Yib481de92007-09-25 17:54:57 -07002900 spin_unlock_irqrestore(&priv->lock, flags);
2901
2902 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002903 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002904 if (!interval)
2905 interval = suspend_time;
2906 /*
2907 * suspend time format:
2908 * 0-19: beacon interval in usec (time before exec.)
2909 * 20-23: 0
2910 * 24-31: number of beacons (suspend between channels)
2911 */
2912
2913 extra = (suspend_time / interval) << 24;
2914 scan_suspend_time = 0xFF0FFFFF &
2915 (extra | ((suspend_time % interval) * 1024));
2916
2917 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002918 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002919 scan_suspend_time, interval);
2920 }
2921
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002922 if (priv->is_internal_short_scan) {
2923 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2924 } else if (priv->scan_request->n_ssids) {
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002925 int i, p = 0;
2926 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2927 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2928 /* always does wildcard anyway */
2929 if (!priv->scan_request->ssids[i].ssid_len)
2930 continue;
2931 scan->direct_scan[p].id = WLAN_EID_SSID;
2932 scan->direct_scan[p].len =
2933 priv->scan_request->ssids[i].ssid_len;
2934 memcpy(scan->direct_scan[p].ssid,
2935 priv->scan_request->ssids[i].ssid,
2936 priv->scan_request->ssids[i].ssid_len);
2937 n_probes++;
2938 p++;
2939 }
2940 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002941 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002942 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002943
2944 /* We don't build a direct scan probe request; the uCode will do
2945 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002946 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002947 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002948 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2949
2950 /* flags + rate selection */
2951
Johannes Berg00700ee2010-04-06 04:12:37 -07002952 switch (priv->scan_band) {
2953 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002954 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2955 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2956 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002957 band = IEEE80211_BAND_2GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002958 break;
2959 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002960 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Bergb097ad22009-04-20 14:36:57 -07002961 /*
2962 * If active scaning is requested but a certain channel
2963 * is marked passive, we can do active scanning if we
2964 * detect transmissions.
2965 */
Johannes Berg96ff5642010-04-30 14:42:15 -07002966 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2967 IWL_GOOD_CRC_TH_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +01002968 band = IEEE80211_BAND_5GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002969 break;
2970 default:
2971 IWL_WARN(priv, "Invalid scan band\n");
Zhu Yib481de92007-09-25 17:54:57 -07002972 goto done;
2973 }
2974
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002975 if (!priv->is_internal_short_scan) {
2976 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)));
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002982 } else {
2983 scan->tx_cmd.len = cpu_to_le16(
2984 iwl_fill_probe_req(priv,
2985 (struct ieee80211_mgmt *)scan->data,
2986 NULL, 0,
2987 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2988 }
Zhu Yib481de92007-09-25 17:54:57 -07002989 /* select Rx antennas */
2990 scan->flags |= iwl3945_get_antenna_flags(priv);
2991
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002992 scan->channel_count =
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002993 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
Johannes Berg1dda6d22010-04-29 04:43:06 -07002994 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
Zhu Yib481de92007-09-25 17:54:57 -07002995
Reinette Chatre14b54332008-11-04 12:21:35 -08002996 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002997 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08002998 goto done;
2999 }
3000
Zhu Yib481de92007-09-25 17:54:57 -07003001 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003002 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003003 cmd.data = scan;
3004 scan->len = cpu_to_le16(cmd.len);
3005
3006 set_bit(STATUS_SCAN_HW, &priv->status);
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08003007 if (iwl_send_cmd_sync(priv, &cmd))
Zhu Yib481de92007-09-25 17:54:57 -07003008 goto done;
3009
3010 queue_delayed_work(priv->workqueue, &priv->scan_check,
3011 IWL_SCAN_CHECK_WATCHDOG);
3012
Zhu Yib481de92007-09-25 17:54:57 -07003013 return;
3014
3015 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003016 /* can not perform scan make sure we clear scanning
3017 * bits from status so next scan request can be performed.
3018 * if we dont clear scanning status bit here all next scan
3019 * will fail
3020 */
3021 clear_bit(STATUS_SCAN_HW, &priv->status);
3022 clear_bit(STATUS_SCANNING, &priv->status);
3023
Ian Schram01ebd062007-10-25 17:15:22 +08003024 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003025 queue_work(priv->workqueue, &priv->scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07003026}
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
Johannes Berg1dda6d22010-04-29 04:43:06 -07003067void iwl3945_post_associate(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003068{
Zhu Yib481de92007-09-25 17:54:57 -07003069 int rc = 0;
3070 struct ieee80211_conf *conf = NULL;
3071
Johannes Berg1dda6d22010-04-29 04:43:06 -07003072 if (!vif || !priv->is_open)
3073 return;
3074
3075 if (vif->type == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003076 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003077 return;
3078 }
3079
Tomas Winklere1623442009-01-27 14:27:56 -08003080 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berg1dda6d22010-04-29 04:43:06 -07003081 vif->bss_conf.aid, 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
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003086 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003087
Zhu Yib481de92007-09-25 17:54:57 -07003088 conf = ieee80211_get_hw_conf(priv->hw);
3089
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003090 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003091 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003092
Tomas Winkler28afaf92008-12-19 10:37:06 +08003093 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Johannes Berg1dda6d22010-04-29 04:43:06 -07003094 iwl_setup_rxon_timing(priv, vif);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003095 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003096 sizeof(priv->rxon_timing), &priv->rxon_timing);
3097 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003098 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003099 "Attempting to continue.\n");
3100
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003101 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003102
Johannes Berg1dda6d22010-04-29 04:43:06 -07003103 priv->staging_rxon.assoc_id = cpu_to_le16(vif->bss_conf.aid);
Zhu Yib481de92007-09-25 17:54:57 -07003104
Tomas Winklere1623442009-01-27 14:27:56 -08003105 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Johannes Berg1dda6d22010-04-29 04:43:06 -07003106 vif->bss_conf.aid, vif->bss_conf.beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07003107
Johannes Berg1dda6d22010-04-29 04:43:06 -07003108 if (vif->bss_conf.assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003109 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003110 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003111 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003112
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003113 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Berg1dda6d22010-04-29 04:43:06 -07003114 if (vif->bss_conf.assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003115 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003116 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003117 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003118
Johannes Berg1dda6d22010-04-29 04:43:06 -07003119 if (vif->type == NL80211_IFTYPE_ADHOC)
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 }
3122
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003123 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003124
Johannes Berg1dda6d22010-04-29 04:43:06 -07003125 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003126 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003127 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003128 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02003129 case NL80211_IFTYPE_ADHOC:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003130 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003131 break;
Zhu Yib481de92007-09-25 17:54:57 -07003132 default:
Johannes Berg1dda6d22010-04-29 04:43:06 -07003133 IWL_ERR(priv, "%s Should not be called in %d mode\n",
3134 __func__, vif->type);
Zhu Yib481de92007-09-25 17:54:57 -07003135 break;
3136 }
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003137}
3138
Zhu Yib481de92007-09-25 17:54:57 -07003139/*****************************************************************************
3140 *
3141 * mac80211 entry point functions
3142 *
3143 *****************************************************************************/
3144
Zhu Yi5a66926a2008-01-14 17:46:18 -08003145#define UCODE_READY_TIMEOUT (2 * HZ)
3146
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003147static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003148{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003149 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003150 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003151
Tomas Winklere1623442009-01-27 14:27:56 -08003152 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003153
3154 /* we should be verifying the device is ready to be opened */
3155 mutex_lock(&priv->mutex);
3156
Zhu Yi5a66926a2008-01-14 17:46:18 -08003157 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3158 * ucode filename and max sizes are card-specific. */
3159
3160 if (!priv->ucode_code.len) {
3161 ret = iwl3945_read_ucode(priv);
3162 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003163 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003164 mutex_unlock(&priv->mutex);
3165 goto out_release_irq;
3166 }
3167 }
3168
Zhu Yie655b9f2008-01-24 02:19:38 -08003169 ret = __iwl3945_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003170
Zhu Yib481de92007-09-25 17:54:57 -07003171 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003172
Zhu Yie655b9f2008-01-24 02:19:38 -08003173 if (ret)
3174 goto out_release_irq;
3175
Tomas Winklere1623442009-01-27 14:27:56 -08003176 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003177
Zhu Yi5a66926a2008-01-14 17:46:18 -08003178 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3179 * mac80211 will not be run successfully. */
3180 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3181 test_bit(STATUS_READY, &priv->status),
3182 UCODE_READY_TIMEOUT);
3183 if (!ret) {
3184 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003185 IWL_ERR(priv,
3186 "Wait for START_ALIVE timeout after %dms.\n",
3187 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a66926a2008-01-14 17:46:18 -08003188 ret = -ETIMEDOUT;
3189 goto out_release_irq;
3190 }
3191 }
3192
Helmut Schaa26635162009-01-15 09:38:44 +01003193 /* ucode is running and will send rfkill notifications,
3194 * no need to poll the killswitch state anymore */
Johannes Bergee525d12010-01-21 06:09:28 -08003195 cancel_delayed_work(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01003196
Johannes Berge932a602009-10-02 13:44:03 -07003197 iwl_led_start(priv);
3198
Zhu Yie655b9f2008-01-24 02:19:38 -08003199 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003200 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003201 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003202
3203out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003204 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003205 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08003206 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003207}
3208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003209static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003210{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003211 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003212
Tomas Winklere1623442009-01-27 14:27:56 -08003213 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003214
Zhu Yie655b9f2008-01-24 02:19:38 -08003215 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003216 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003217 return;
3218 }
3219
Zhu Yib481de92007-09-25 17:54:57 -07003220 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003221
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003222 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003223 /* stop mac, cancel any scan request and clear
3224 * RXON_FILTER_ASSOC_MSK BIT
3225 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08003226 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003227 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003228 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003229 }
3230
Zhu Yi5a66926a2008-01-14 17:46:18 -08003231 iwl3945_down(priv);
3232
3233 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003234
3235 /* start polling the killswitch state again */
Johannes Bergee525d12010-01-21 06:09:28 -08003236 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01003237 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003238
Tomas Winklere1623442009-01-27 14:27:56 -08003239 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003240}
3241
Johannes Berge039fa42008-05-15 12:55:29 +02003242static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003243{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003244 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003245
Tomas Winklere1623442009-01-27 14:27:56 -08003246 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003247
Tomas Winklere1623442009-01-27 14:27:56 -08003248 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003249 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003250
Johannes Berge039fa42008-05-15 12:55:29 +02003251 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003252 dev_kfree_skb_any(skb);
3253
Tomas Winklere1623442009-01-27 14:27:56 -08003254 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003255 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003256}
3257
Johannes Berg1dda6d22010-04-29 04:43:06 -07003258void iwl3945_config_ap(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003259{
3260 int rc = 0;
3261
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003262 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003263 return;
3264
3265 /* The following should be done only at AP bring up */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003266 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003267
3268 /* RXON - unassoc (to set timing command) */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003269 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003270 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003271
3272 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08003273 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Johannes Berg1dda6d22010-04-29 04:43:06 -07003274 iwl_setup_rxon_timing(priv, vif);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003275 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3276 sizeof(priv->rxon_timing),
3277 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07003278 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003279 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003280 "Attempting to continue.\n");
3281
Johannes Berg1dda6d22010-04-29 04:43:06 -07003282 priv->staging_rxon.assoc_id = 0;
3283
3284 if (vif->bss_conf.assoc_capability &
3285 WLAN_CAPABILITY_SHORT_PREAMBLE)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003286 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003287 RXON_FLG_SHORT_PREAMBLE_MSK;
3288 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003289 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003290 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3291
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003292 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Berg1dda6d22010-04-29 04:43:06 -07003293 if (vif->bss_conf.assoc_capability &
3294 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003295 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003296 RXON_FLG_SHORT_SLOT_MSK;
3297 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003298 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003299 ~RXON_FLG_SHORT_SLOT_MSK;
3300
Johannes Berg1dda6d22010-04-29 04:43:06 -07003301 if (vif->type == NL80211_IFTYPE_ADHOC)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003302 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003303 ~RXON_FLG_SHORT_SLOT_MSK;
3304 }
3305 /* restore RXON assoc */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003306 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003307 iwlcore_commit_rxon(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08003308 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003309 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003310
3311 /* FIXME - we need to add code here to detect a totally new
3312 * configuration, reset the AP, unassoc, rxon timing, assoc,
3313 * clear sta table, add BCAST sta... */
3314}
3315
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003316static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003317 struct ieee80211_vif *vif,
3318 struct ieee80211_sta *sta,
3319 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003320{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003321 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003322 int ret = 0;
3323 u8 sta_id = IWL_INVALID_STATION;
3324 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003325
Tomas Winklere1623442009-01-27 14:27:56 -08003326 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003327
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003328 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003329 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003330 return -EOPNOTSUPP;
3331 }
3332
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003333 static_key = !iwl_is_associated(priv);
3334
3335 if (!static_key) {
Johannes Berg0af8bca2010-04-30 14:08:00 -07003336 sta_id = iwl_sta_id_or_broadcast(priv, sta);
3337 if (sta_id == IWL_INVALID_STATION)
3338 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003339 }
3340
3341 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003342 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003343
Zhu Yib481de92007-09-25 17:54:57 -07003344 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003345 case SET_KEY:
3346 if (static_key)
3347 ret = iwl3945_set_static_key(priv, key);
3348 else
3349 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3350 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003351 break;
3352 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003353 if (static_key)
3354 ret = iwl3945_remove_static_key(priv);
3355 else
3356 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3357 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003358 break;
3359 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003360 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003361 }
3362
Johannes Berg72e15d72010-02-19 11:42:32 -08003363 mutex_unlock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003364 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003365
Winkler, Tomas42986792009-01-19 15:30:22 -08003366 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003367}
3368
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003369static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3370 struct ieee80211_vif *vif,
3371 struct ieee80211_sta *sta)
3372{
3373 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07003374 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003375 int ret;
Johannes Bergfd1af152010-04-30 11:30:43 -07003376 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003377 u8 sta_id;
3378
Johannes Bergfd1af152010-04-30 11:30:43 -07003379 sta_priv->common.sta_id = IWL_INVALID_STATION;
3380
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003381 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3382 sta->addr);
3383
3384 ret = iwl_add_station_common(priv, sta->addr, is_ap, &sta->ht_cap,
3385 &sta_id);
3386 if (ret) {
3387 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3388 sta->addr, ret);
3389 /* Should we return success if return code is EEXIST ? */
3390 return ret;
3391 }
3392
Johannes Bergfd1af152010-04-30 11:30:43 -07003393 sta_priv->common.sta_id = sta_id;
3394
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003395 /* Initialize rate scaling */
Frans Pop91dd6c22010-03-24 14:19:58 -07003396 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003397 sta->addr);
3398 iwl3945_rs_rate_init(priv, sta, sta_id);
3399
3400 return 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003401}
Zhu Yib481de92007-09-25 17:54:57 -07003402/*****************************************************************************
3403 *
3404 * sysfs attributes
3405 *
3406 *****************************************************************************/
3407
Samuel Ortizd08853a2009-01-23 13:45:17 -08003408#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003409
3410/*
3411 * The following adds a new attribute to the sysfs representation
3412 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3413 * used for controlling the debug level.
3414 *
3415 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003416 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003417 * The debug_level being managed using sysfs below is a per device debug
3418 * level that is used instead of the global debug level if it (the per
3419 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003420 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003421static ssize_t show_debug_level(struct device *d,
3422 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003423{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003424 struct iwl_priv *priv = dev_get_drvdata(d);
3425 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003426}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003427static ssize_t store_debug_level(struct device *d,
3428 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003429 const char *buf, size_t count)
3430{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003431 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003432 unsigned long val;
3433 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003434
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003435 ret = strict_strtoul(buf, 0, &val);
3436 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003437 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003438 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003439 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003440 if (iwl_alloc_traffic_mem(priv))
3441 IWL_ERR(priv,
3442 "Not enough memory to generate traffic log\n");
3443 }
Zhu Yib481de92007-09-25 17:54:57 -07003444 return strnlen(buf, count);
3445}
3446
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003447static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3448 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003449
Samuel Ortizd08853a2009-01-23 13:45:17 -08003450#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003451
Zhu Yib481de92007-09-25 17:54:57 -07003452static ssize_t show_temperature(struct device *d,
3453 struct device_attribute *attr, char *buf)
3454{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003455 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003456
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003457 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003458 return -EAGAIN;
3459
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003460 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003461}
3462
3463static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3464
Zhu Yib481de92007-09-25 17:54:57 -07003465static ssize_t show_tx_power(struct device *d,
3466 struct device_attribute *attr, char *buf)
3467{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003468 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003469 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003470}
3471
3472static ssize_t store_tx_power(struct device *d,
3473 struct device_attribute *attr,
3474 const char *buf, size_t count)
3475{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003476 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003477 char *p = (char *)buf;
3478 u32 val;
3479
3480 val = simple_strtoul(p, &p, 10);
3481 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003482 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003483 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003484 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003485
3486 return count;
3487}
3488
3489static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3490
3491static ssize_t show_flags(struct device *d,
3492 struct device_attribute *attr, char *buf)
3493{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003494 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003495
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003496 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003497}
3498
3499static ssize_t store_flags(struct device *d,
3500 struct device_attribute *attr,
3501 const char *buf, size_t count)
3502{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003503 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003504 u32 flags = simple_strtoul(buf, NULL, 0);
3505
3506 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003507 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003508 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003509 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003510 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003511 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003512 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003513 flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003514 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003515 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003516 }
3517 }
3518 mutex_unlock(&priv->mutex);
3519
3520 return count;
3521}
3522
3523static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3524
3525static ssize_t show_filter_flags(struct device *d,
3526 struct device_attribute *attr, char *buf)
3527{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003528 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003529
3530 return sprintf(buf, "0x%04X\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003531 le32_to_cpu(priv->active_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003532}
3533
3534static ssize_t store_filter_flags(struct device *d,
3535 struct device_attribute *attr,
3536 const char *buf, size_t count)
3537{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003538 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003539 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3540
3541 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003542 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003543 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003544 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003545 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003546 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003547 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003548 "0x%04X\n", filter_flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003549 priv->staging_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003550 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003551 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003552 }
3553 }
3554 mutex_unlock(&priv->mutex);
3555
3556 return count;
3557}
3558
3559static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3560 store_filter_flags);
3561
Zhu Yib481de92007-09-25 17:54:57 -07003562static ssize_t show_measurement(struct device *d,
3563 struct device_attribute *attr, char *buf)
3564{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003565 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003566 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003567 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003568 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003569 unsigned long flags;
3570
3571 spin_lock_irqsave(&priv->lock, flags);
3572 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3573 spin_unlock_irqrestore(&priv->lock, flags);
3574 return 0;
3575 }
3576 memcpy(&measure_report, &priv->measure_report, size);
3577 priv->measurement_status = 0;
3578 spin_unlock_irqrestore(&priv->lock, flags);
3579
3580 while (size && (PAGE_SIZE - len)) {
3581 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3582 PAGE_SIZE - len, 1);
3583 len = strlen(buf);
3584 if (PAGE_SIZE - len)
3585 buf[len++] = '\n';
3586
3587 ofs += 16;
3588 size -= min(size, 16U);
3589 }
3590
3591 return len;
3592}
3593
3594static ssize_t store_measurement(struct device *d,
3595 struct device_attribute *attr,
3596 const char *buf, size_t count)
3597{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003598 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003599 struct ieee80211_measurement_params params = {
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003600 .channel = le16_to_cpu(priv->active_rxon.channel),
Johannes Berge99f168c2010-01-19 10:04:28 -08003601 .start_time = cpu_to_le64(priv->_3945.last_tsf),
Zhu Yib481de92007-09-25 17:54:57 -07003602 .duration = cpu_to_le16(1),
3603 };
3604 u8 type = IWL_MEASURE_BASIC;
3605 u8 buffer[32];
3606 u8 channel;
3607
3608 if (count) {
3609 char *p = buffer;
3610 strncpy(buffer, buf, min(sizeof(buffer), count));
3611 channel = simple_strtoul(p, NULL, 0);
3612 if (channel)
3613 params.channel = channel;
3614
3615 p = buffer;
3616 while (*p && *p != ' ')
3617 p++;
3618 if (*p)
3619 type = simple_strtoul(p + 1, NULL, 0);
3620 }
3621
Tomas Winklere1623442009-01-27 14:27:56 -08003622 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003623 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003624 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003625
3626 return count;
3627}
3628
3629static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3630 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003631
Zhu Yib481de92007-09-25 17:54:57 -07003632static ssize_t store_retry_rate(struct device *d,
3633 struct device_attribute *attr,
3634 const char *buf, size_t count)
3635{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003636 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003637
3638 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3639 if (priv->retry_rate <= 0)
3640 priv->retry_rate = 1;
3641
3642 return count;
3643}
3644
3645static ssize_t show_retry_rate(struct device *d,
3646 struct device_attribute *attr, char *buf)
3647{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003648 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003649 return sprintf(buf, "%d", priv->retry_rate);
3650}
3651
3652static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3653 store_retry_rate);
3654
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003655
Zhu Yib481de92007-09-25 17:54:57 -07003656static ssize_t show_channels(struct device *d,
3657 struct device_attribute *attr, char *buf)
3658{
Johannes Berg8318d782008-01-24 19:38:38 +01003659 /* all this shit doesn't belong into sysfs anyway */
3660 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003661}
3662
3663static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3664
Zhu Yib481de92007-09-25 17:54:57 -07003665static ssize_t show_antenna(struct device *d,
3666 struct device_attribute *attr, char *buf)
3667{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003668 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003669
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003670 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003671 return -EAGAIN;
3672
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003673 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003674}
3675
3676static ssize_t store_antenna(struct device *d,
3677 struct device_attribute *attr,
3678 const char *buf, size_t count)
3679{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003680 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003681 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003682
3683 if (count == 0)
3684 return 0;
3685
3686 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003687 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003688 return count;
3689 }
3690
3691 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003692 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003693 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003694 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003695 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003696
3697
3698 return count;
3699}
3700
3701static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3702
3703static ssize_t show_status(struct device *d,
3704 struct device_attribute *attr, char *buf)
3705{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003706 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003707 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003708 return -EAGAIN;
3709 return sprintf(buf, "0x%08x\n", (int)priv->status);
3710}
3711
3712static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3713
3714static ssize_t dump_error_log(struct device *d,
3715 struct device_attribute *attr,
3716 const char *buf, size_t count)
3717{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003718 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003719 char *p = (char *)buf;
3720
3721 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003722 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003723
3724 return strnlen(buf, count);
3725}
3726
3727static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3728
Zhu Yib481de92007-09-25 17:54:57 -07003729/*****************************************************************************
3730 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003731 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003732 *
3733 *****************************************************************************/
3734
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003735static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003736{
Reinette Chatred21050c2009-02-13 11:51:18 -08003737 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003738
3739 init_waitqueue_head(&priv->wait_command_queue);
3740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003741 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3742 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003743 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003744 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3745 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Johannes Bergee525d12010-01-21 06:09:28 -08003746 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003747 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003748 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
Abhijeet Kolekar1e460532010-04-23 14:14:53 -07003749 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003750 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07003751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003752 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003753
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003754 if (priv->cfg->ops->lib->recover_from_tx_stall) {
3755 init_timer(&priv->monitor_recover);
3756 priv->monitor_recover.data = (unsigned long)priv;
3757 priv->monitor_recover.function =
3758 priv->cfg->ops->lib->recover_from_tx_stall;
3759 }
3760
Zhu Yib481de92007-09-25 17:54:57 -07003761 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003762 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003763}
3764
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003765static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003766{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003767 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003768
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003769 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003770 cancel_delayed_work(&priv->scan_check);
3771 cancel_delayed_work(&priv->alive_start);
Abhijeet Kolekar1e460532010-04-23 14:14:53 -07003772 cancel_work_sync(&priv->start_internal_scan);
Zhu Yib481de92007-09-25 17:54:57 -07003773 cancel_work_sync(&priv->beacon_update);
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003774 if (priv->cfg->ops->lib->recover_from_tx_stall)
3775 del_timer_sync(&priv->monitor_recover);
Zhu Yib481de92007-09-25 17:54:57 -07003776}
3777
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003778static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003779 &dev_attr_antenna.attr,
3780 &dev_attr_channels.attr,
3781 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003782 &dev_attr_flags.attr,
3783 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003784 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003785 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003786 &dev_attr_status.attr,
3787 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003788 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003789#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003790 &dev_attr_debug_level.attr,
3791#endif
Zhu Yib481de92007-09-25 17:54:57 -07003792 NULL
3793};
3794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003795static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003796 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003797 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003798};
3799
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003800static struct ieee80211_ops iwl3945_hw_ops = {
3801 .tx = iwl3945_mac_tx,
3802 .start = iwl3945_mac_start,
3803 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003804 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003805 .remove_interface = iwl_mac_remove_interface,
Abhijeet Kolekar48083682009-04-08 11:26:48 -07003806 .config = iwl_mac_config,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003807 .configure_filter = iwl_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003809 .conf_tx = iwl_mac_conf_tx,
Abhijeet Kolekarbd564262009-04-08 11:26:52 -07003810 .reset_tsf = iwl_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003811 .bss_info_changed = iwl_bss_info_changed,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003812 .hw_scan = iwl_mac_hw_scan,
3813 .sta_add = iwl3945_mac_sta_add,
3814 .sta_remove = iwl_mac_sta_remove,
Zhu Yib481de92007-09-25 17:54:57 -07003815};
3816
Winkler, Tomase52119c2008-12-22 11:31:19 +08003817static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003818{
3819 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003820 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003821
3822 priv->retry_rate = 1;
3823 priv->ibss_beacon = NULL;
3824
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003825 spin_lock_init(&priv->sta_lock);
3826 spin_lock_init(&priv->hcmd_lock);
3827
3828 INIT_LIST_HEAD(&priv->free_frames);
3829
3830 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003831 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003832
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003833 priv->ieee_channels = NULL;
3834 priv->ieee_rates = NULL;
3835 priv->band = IEEE80211_BAND_2GHZ;
3836
3837 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003838 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003839
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003840 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003841
Samuel Ortize6148912009-01-23 13:45:15 -08003842 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3843 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3844 eeprom->version);
3845 ret = -EINVAL;
3846 goto err;
3847 }
3848 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003849 if (ret) {
3850 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3851 goto err;
3852 }
3853
Samuel Ortize6148912009-01-23 13:45:15 -08003854 /* Set up txpower settings in driver for all channels */
3855 if (iwl3945_txpower_set_from_eeprom(priv)) {
3856 ret = -EIO;
3857 goto err_free_channel_map;
3858 }
3859
Samuel Ortiz534166d2009-01-23 13:45:16 -08003860 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003861 if (ret) {
3862 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3863 goto err_free_channel_map;
3864 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003865 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3866
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003867 return 0;
3868
3869err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003870 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003871err:
3872 return ret;
3873}
3874
Johannes Bergdd7a2502010-04-28 23:33:10 -07003875#define IWL3945_MAX_PROBE_REQUEST 200
3876
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003877static int iwl3945_setup_mac(struct iwl_priv *priv)
3878{
3879 int ret;
3880 struct ieee80211_hw *hw = priv->hw;
3881
3882 hw->rate_control_algorithm = "iwl-3945-rs";
3883 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
Johannes Bergfd1af152010-04-30 11:30:43 -07003884 hw->vif_data_size = sizeof(struct iwl_vif_priv);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003885
3886 /* Tell mac80211 our characteristics */
3887 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003888 IEEE80211_HW_SPECTRUM_MGMT;
3889
3890 if (!priv->cfg->broken_powersave)
3891 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3892 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003893
3894 hw->wiphy->interface_modes =
3895 BIT(NL80211_IFTYPE_STATION) |
3896 BIT(NL80211_IFTYPE_ADHOC);
3897
Reinette Chatref6c8f152010-03-12 11:13:26 -08003898 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
Johannes Berg5be83de2009-11-19 00:56:28 +01003899 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003900
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003901 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3902 /* we create the 802.11 header and a zero-length SSID element */
Johannes Bergdd7a2502010-04-28 23:33:10 -07003903 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003904
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003905 /* Default value; 4 EDCA QOS priorities */
3906 hw->queues = 4;
3907
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003908 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3909 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3910 &priv->bands[IEEE80211_BAND_2GHZ];
3911
3912 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3913 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3914 &priv->bands[IEEE80211_BAND_5GHZ];
3915
3916 ret = ieee80211_register_hw(priv->hw);
3917 if (ret) {
3918 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3919 return ret;
3920 }
3921 priv->mac80211_registered = 1;
3922
3923 return 0;
3924}
3925
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003926static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003927{
3928 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003929 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003930 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003931 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003932 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003933 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003934
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003935 /***********************
3936 * 1. Allocating HW data
3937 * ********************/
3938
Zhu Yib481de92007-09-25 17:54:57 -07003939 /* mac80211 allocates memory for this device instance, including
3940 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003941 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003942 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003943 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07003944 err = -ENOMEM;
3945 goto out;
3946 }
Zhu Yib481de92007-09-25 17:54:57 -07003947 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003948 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003949
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003950 /*
3951 * Disabling hardware scan means that mac80211 will perform scans
3952 * "the hard way", rather than using device's scan.
3953 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003954 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08003955 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003956 iwl3945_hw_ops.hw_scan = NULL;
3957 }
3958
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003959
Tomas Winklere1623442009-01-27 14:27:56 -08003960 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003961 priv->cfg = cfg;
3962 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07003963 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003964
Samuel Ortizd08853a2009-01-23 13:45:17 -08003965#ifdef CONFIG_IWLWIFI_DEBUG
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003966 atomic_set(&priv->restrict_refcnt, 0);
3967#endif
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003968 if (iwl_alloc_traffic_mem(priv))
3969 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07003970
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003971 /***************************
3972 * 2. Initializing PCI bus
3973 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07003974 if (pci_enable_device(pdev)) {
3975 err = -ENODEV;
3976 goto out_ieee80211_free_hw;
3977 }
3978
3979 pci_set_master(pdev);
3980
Yang Hongyang284901a2009-04-06 19:01:15 -07003981 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07003982 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07003983 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07003984 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08003985 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003986 goto out_pci_disable_device;
3987 }
3988
3989 pci_set_drvdata(pdev, priv);
3990 err = pci_request_regions(pdev, DRV_NAME);
3991 if (err)
3992 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003993
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003994 /***********************
3995 * 3. Read REV Register
3996 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07003997 priv->hw_base = pci_iomap(pdev, 0, 0);
3998 if (!priv->hw_base) {
3999 err = -ENODEV;
4000 goto out_pci_release_regions;
4001 }
4002
Tomas Winklere1623442009-01-27 14:27:56 -08004003 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004004 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004005 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004006
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004007 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4008 * PCI Tx retries from interfering with C3 CPU state */
4009 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004010
Reinette Chatre731a29b2009-12-14 14:12:11 -08004011 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004012 * we should init now
4013 */
4014 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004015 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004016
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004017 /*
4018 * stop and reset the on-board processor just in case it is in a
4019 * strange state ... like being left stranded by a primary kernel
4020 * and this is now the kdump kernel trying to start up
4021 */
4022 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4023
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004024 /***********************
4025 * 4. Read EEPROM
4026 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004027
Zhu Yi5a66926a2008-01-14 17:46:18 -08004028 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004029 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004030 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004031 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004032 goto out_iounmap;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004033 }
4034 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004035 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4036 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08004037 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004038 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4039
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004040 /***********************
4041 * 5. Setup HW Constants
4042 * ********************/
4043 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004044 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004045 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004046 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004047 }
4048
4049 /***********************
4050 * 6. Setup priv
4051 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004052
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004053 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004054 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004055 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004056 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004057 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004058
Tomas Winkler978785a2008-12-19 10:37:31 +08004059 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4060 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004061
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004062 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004063 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004064 * ********************/
4065
4066 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004067 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004068 spin_unlock_irqrestore(&priv->lock, flags);
4069
Helmut Schaa26635162009-01-15 09:38:44 +01004070 pci_enable_msi(priv->pci_dev);
4071
Mohamed Abbasef850d72009-05-22 11:01:50 -07004072 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
4073 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004074 if (err) {
4075 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4076 goto out_disable_msi;
4077 }
4078
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004079 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4080 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004081 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004082 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004083 }
4084
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004085 iwl_set_rxon_channel(priv,
4086 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004087 iwl3945_setup_deferred_work(priv);
4088 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004089 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004090
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004091 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004092 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004093 * *******************************/
4094
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004095 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004096
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004097 err = iwl3945_setup_mac(priv);
4098 if (err)
4099 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004100
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004101 err = iwl_dbgfs_register(priv, DRV_NAME);
4102 if (err)
4103 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4104
Helmut Schaa26635162009-01-15 09:38:44 +01004105 /* Start monitoring the killswitch */
Johannes Bergee525d12010-01-21 06:09:28 -08004106 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01004107 2 * HZ);
4108
Zhu Yib481de92007-09-25 17:54:57 -07004109 return 0;
4110
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004111 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004112 destroy_workqueue(priv->workqueue);
4113 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004114 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4115 out_release_irq:
4116 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004117 out_disable_msi:
4118 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004119 iwlcore_free_geos(priv);
4120 iwl_free_channel_map(priv);
4121 out_unset_hw_params:
4122 iwl3945_unset_hw_params(priv);
4123 out_eeprom_free:
4124 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004125 out_iounmap:
4126 pci_iounmap(pdev, priv->hw_base);
4127 out_pci_release_regions:
4128 pci_release_regions(pdev);
4129 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004130 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004131 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004132 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004133 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004134 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004135 out:
4136 return err;
4137}
4138
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004139static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004140{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004141 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004142 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004143
4144 if (!priv)
4145 return;
4146
Tomas Winklere1623442009-01-27 14:27:56 -08004147 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004148
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004149 iwl_dbgfs_unregister(priv);
4150
Zhu Yib481de92007-09-25 17:54:57 -07004151 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004152
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004153 if (priv->mac80211_registered) {
4154 ieee80211_unregister_hw(priv->hw);
4155 priv->mac80211_registered = 0;
4156 } else {
4157 iwl3945_down(priv);
4158 }
Zhu Yib481de92007-09-25 17:54:57 -07004159
Ben Cahillc166b252009-10-23 13:42:35 -07004160 /*
4161 * Make sure device is reset to low power before unloading driver.
4162 * This may be redundant with iwl_down(), but there are paths to
4163 * run iwl_down() without calling apm_ops.stop(), and there are
4164 * paths to avoid running iwl_down() at all before leaving driver.
4165 * This (inexpensive) call *makes sure* device is reset.
4166 */
4167 priv->cfg->ops->lib->apm_ops.stop(priv);
4168
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004169 /* make sure we flush any pending irq or
4170 * tasklet for the driver
4171 */
4172 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004173 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004174 spin_unlock_irqrestore(&priv->lock, flags);
4175
4176 iwl_synchronize_irq(priv);
4177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004178 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004179
Johannes Bergee525d12010-01-21 06:09:28 -08004180 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004182 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004183
4184 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004185 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004186 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004187
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004188 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004189
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004190 /*netif_stop_queue(dev); */
4191 flush_workqueue(priv->workqueue);
4192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004193 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004194 * priv->workqueue... so we can't take down the workqueue
4195 * until now... */
4196 destroy_workqueue(priv->workqueue);
4197 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004198 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004199
Helmut Schaa26635162009-01-15 09:38:44 +01004200 free_irq(pdev->irq, priv);
4201 pci_disable_msi(pdev);
4202
Zhu Yib481de92007-09-25 17:54:57 -07004203 pci_iounmap(pdev, priv->hw_base);
4204 pci_release_regions(pdev);
4205 pci_disable_device(pdev);
4206 pci_set_drvdata(pdev, NULL);
4207
Samuel Ortize6148912009-01-23 13:45:15 -08004208 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004209 iwlcore_free_geos(priv);
Johannes Berg811ecc92010-04-06 04:12:41 -07004210 kfree(priv->scan_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07004211 if (priv->ibss_beacon)
4212 dev_kfree_skb(priv->ibss_beacon);
4213
4214 ieee80211_free_hw(priv->hw);
4215}
4216
Zhu Yib481de92007-09-25 17:54:57 -07004217
4218/*****************************************************************************
4219 *
4220 * driver and module entry point
4221 *
4222 *****************************************************************************/
4223
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004224static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004225 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004226 .id_table = iwl3945_hw_card_ids,
4227 .probe = iwl3945_pci_probe,
4228 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004229#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004230 .suspend = iwl_pci_suspend,
4231 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004232#endif
4233};
4234
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004235static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004236{
4237
4238 int ret;
4239 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4240 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004241
4242 ret = iwl3945_rate_control_register();
4243 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004244 printk(KERN_ERR DRV_NAME
4245 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004246 return ret;
4247 }
4248
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004249 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004250 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08004251 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004252 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004253 }
Zhu Yib481de92007-09-25 17:54:57 -07004254
4255 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004256
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004257error_register:
4258 iwl3945_rate_control_unregister();
4259 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004260}
4261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004262static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004263{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004264 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004265 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004266}
4267
Reinette Chatrea0987a82008-12-02 12:14:06 -08004268MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004269
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004270module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004271MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004272module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004273MODULE_PARM_DESC(swcrypto,
4274 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004275#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004276module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004277MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004278#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004279module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4280 int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004281MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004282module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004283MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004285module_exit(iwl3945_exit);
4286module_init(iwl3945_init);