blob: 931c546367ea1901104dc4f2ced1ae57eb4c083c [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
Joe Perchesc96c31e2010-07-26 14:39:58 -070030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Zhu Yib481de92007-09-25 17:54:57 -070032#include <linux/kernel.h>
33#include <linux/module.h>
Zhu Yib481de92007-09-25 17:54:57 -070034#include <linux/init.h>
35#include <linux/pci.h>
John W. Linville1a7123c2010-08-05 14:39:31 -040036#include <linux/pci-aspm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Zhu Yib481de92007-09-25 17:54:57 -070038#include <linux/dma-mapping.h>
39#include <linux/delay.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040040#include <linux/sched.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/skbuff.h>
42#include <linux/netdevice.h>
43#include <linux/wireless.h>
44#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070045#include <linux/etherdevice.h>
46#include <linux/if_arp.h>
47
48#include <net/ieee80211_radiotap.h>
49#include <net/mac80211.h>
50
51#include <asm/div64.h>
52
Samuel Ortiza3139c52008-12-19 10:37:09 +080053#define DRV_NAME "iwl3945"
54
Winkler, Tomasdbb66542008-12-22 11:31:14 +080055#include "iwl-fh.h"
56#include "iwl-3945-fh.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080057#include "iwl-commands.h"
Samuel Ortiz17f841c2009-01-23 13:45:20 -080058#include "iwl-sta.h"
Zhu Yib481de92007-09-25 17:54:57 -070059#include "iwl-3945.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080060#include "iwl-core.h"
Reinette Chatre4a6547c2010-02-18 22:03:02 -080061#include "iwl-helpers.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080062#include "iwl-dev.h"
Reinette Chatre81963d62010-01-22 14:22:57 -080063#include "iwl-spectrum.h"
Johannes Berg2295c662010-10-23 09:15:41 -070064#include "iwl-legacy.h"
Zhu Yib481de92007-09-25 17:54:57 -070065
Zhu Yib481de92007-09-25 17:54:57 -070066/*
67 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070068 */
69
70#define DRV_DESCRIPTION \
71"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
72
Samuel Ortizd08853a2009-01-23 13:45:17 -080073#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070074#define VD "d"
75#else
76#define VD
77#endif
78
Reinette Chatre81963d62010-01-22 14:22:57 -080079/*
80 * add "s" to indicate spectrum measurement included.
81 * we add it here to be consistent with previous releases in which
82 * this was configurable.
83 */
84#define DRV_VERSION IWLWIFI_VERSION VD "s"
Reinette Chatre1f447802010-01-15 13:43:41 -080085#define DRV_COPYRIGHT "Copyright(c) 2003-2010 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080086#define DRV_AUTHOR "<ilw@linux.intel.com>"
Zhu Yib481de92007-09-25 17:54:57 -070087
88MODULE_DESCRIPTION(DRV_DESCRIPTION);
89MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080090MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070091MODULE_LICENSE("GPL");
92
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080093 /* module parameters */
94struct iwl_mod_params iwl3945_mod_params = {
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080095 .sw_crypto = 1,
Samuel Ortizaf48d042009-01-23 13:45:19 -080096 .restart_fw = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080097 /* the rest are 0 by default */
98};
99
Zhu Yib481de92007-09-25 17:54:57 -0700100/**
Samuel Ortiz7e4bca52009-01-23 13:45:18 -0800101 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
102 * @priv: eeprom and antenna fields are used to determine antenna flags
103 *
104 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
105 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
106 *
107 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
108 * IWL_ANTENNA_MAIN - Force MAIN antenna
109 * IWL_ANTENNA_AUX - Force AUX antenna
110 */
111__le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
112{
113 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
114
115 switch (iwl3945_mod_params.antenna) {
116 case IWL_ANTENNA_DIVERSITY:
117 return 0;
118
119 case IWL_ANTENNA_MAIN:
120 if (eeprom->antenna_switch_type)
121 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
122 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
123
124 case IWL_ANTENNA_AUX:
125 if (eeprom->antenna_switch_type)
126 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
127 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
128 }
129
130 /* bad antenna selector value */
131 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
132 iwl3945_mod_params.antenna);
133
134 return 0; /* "diversity" is default if error */
135}
136
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800137static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700138 struct ieee80211_key_conf *keyconf,
139 u8 sta_id)
140{
141 unsigned long flags;
142 __le16 key_flags = 0;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800143 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700144
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800145 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
146 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
147
Johannes Berga194e322010-08-27 08:53:46 -0700148 if (sta_id == priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800149 key_flags |= STA_KEY_MULTICAST_MSK;
150
151 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
152 keyconf->hw_key_idx = keyconf->keyidx;
153 key_flags &= ~STA_KEY_FLG_INVALID;
154
Zhu Yib481de92007-09-25 17:54:57 -0700155 spin_lock_irqsave(&priv->sta_lock, flags);
Johannes Berg97359d12010-08-10 09:46:38 +0200156 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
Tomas Winklerc587de02009-06-03 11:44:07 -0700157 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
158 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700159 keyconf->keylen);
160
Tomas Winklerc587de02009-06-03 11:44:07 -0700161 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700162 keyconf->keylen);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800163
Tomas Winklerc587de02009-06-03 11:44:07 -0700164 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800165 == STA_KEY_FLG_NO_ENC)
Tomas Winklerc587de02009-06-03 11:44:07 -0700166 priv->stations[sta_id].sta.key.key_offset =
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800167 iwl_get_free_ucode_key_index(priv);
168 /* else, we are overriding an existing key => no need to allocated room
169 * in uCode. */
170
Tomas Winklerc587de02009-06-03 11:44:07 -0700171 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800172 "no space for a new key");
173
Tomas Winklerc587de02009-06-03 11:44:07 -0700174 priv->stations[sta_id].sta.key.key_flags = key_flags;
175 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
176 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700177
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800178 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
179
Tomas Winklerc587de02009-06-03 11:44:07 -0700180 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800181
Zhu Yib481de92007-09-25 17:54:57 -0700182 spin_unlock_irqrestore(&priv->sta_lock, flags);
183
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800184 return ret;
185}
186
187static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
188 struct ieee80211_key_conf *keyconf,
189 u8 sta_id)
190{
191 return -EOPNOTSUPP;
192}
193
194static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
195 struct ieee80211_key_conf *keyconf,
196 u8 sta_id)
197{
198 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -0700199}
200
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800201static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700202{
203 unsigned long flags;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700204 struct iwl_addsta_cmd sta_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700205
206 spin_lock_irqsave(&priv->sta_lock, flags);
Tomas Winklerc587de02009-06-03 11:44:07 -0700207 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
208 memset(&priv->stations[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +0800209 sizeof(struct iwl4965_keyinfo));
Tomas Winklerc587de02009-06-03 11:44:07 -0700210 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
211 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
212 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700213 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700214 spin_unlock_irqrestore(&priv->sta_lock, flags);
215
Tomas Winklere1623442009-01-27 14:27:56 -0800216 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700217 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Zhu Yib481de92007-09-25 17:54:57 -0700218}
219
Abhijeet Kolekarfa11d522009-03-11 11:17:54 -0700220static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800221 struct ieee80211_key_conf *keyconf, u8 sta_id)
222{
223 int ret = 0;
224
225 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
226
Johannes Berg97359d12010-08-10 09:46:38 +0200227 switch (keyconf->cipher) {
228 case WLAN_CIPHER_SUITE_CCMP:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800229 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
230 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200231 case WLAN_CIPHER_SUITE_TKIP:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800232 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
233 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200234 case WLAN_CIPHER_SUITE_WEP40:
235 case WLAN_CIPHER_SUITE_WEP104:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800236 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
237 break;
238 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200239 IWL_ERR(priv, "Unknown alg: %s alg=%x\n", __func__,
240 keyconf->cipher);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800241 ret = -EINVAL;
242 }
243
Johannes Berg97359d12010-08-10 09:46:38 +0200244 IWL_DEBUG_WEP(priv, "Set dynamic key: alg=%x len=%d idx=%d sta=%d ret=%d\n",
245 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800246 sta_id, ret);
247
248 return ret;
249}
250
251static int iwl3945_remove_static_key(struct iwl_priv *priv)
252{
253 int ret = -EOPNOTSUPP;
254
255 return ret;
256}
257
258static int iwl3945_set_static_key(struct iwl_priv *priv,
259 struct ieee80211_key_conf *key)
260{
Johannes Berg97359d12010-08-10 09:46:38 +0200261 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
262 key->cipher == WLAN_CIPHER_SUITE_WEP104)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800263 return -EOPNOTSUPP;
264
Johannes Berg97359d12010-08-10 09:46:38 +0200265 IWL_ERR(priv, "Static key invalid: cipher %x\n", key->cipher);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800266 return -EINVAL;
267}
268
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800269static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700270{
271 struct list_head *element;
272
Tomas Winklere1623442009-01-27 14:27:56 -0800273 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700274 priv->frames_count);
275
276 while (!list_empty(&priv->free_frames)) {
277 element = priv->free_frames.next;
278 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800279 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700280 priv->frames_count--;
281 }
282
283 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800284 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700285 priv->frames_count);
286 priv->frames_count = 0;
287 }
288}
289
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800290static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700291{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800292 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700293 struct list_head *element;
294 if (list_empty(&priv->free_frames)) {
295 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
296 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800297 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700298 return NULL;
299 }
300
301 priv->frames_count++;
302 return frame;
303 }
304
305 element = priv->free_frames.next;
306 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800307 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700308}
309
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800310static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700311{
312 memset(frame, 0, sizeof(*frame));
313 list_add(&frame->list, &priv->free_frames);
314}
315
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800316unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700317 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200318 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700319{
320
Johannes Berg12e934d2010-10-04 05:50:06 -0700321 if (!iwl_is_associated(priv, IWL_RXON_CTX_BSS) || !priv->beacon_skb)
Zhu Yib481de92007-09-25 17:54:57 -0700322 return 0;
323
Johannes Berg12e934d2010-10-04 05:50:06 -0700324 if (priv->beacon_skb->len > left)
Zhu Yib481de92007-09-25 17:54:57 -0700325 return 0;
326
Johannes Berg12e934d2010-10-04 05:50:06 -0700327 memcpy(hdr, priv->beacon_skb->data, priv->beacon_skb->len);
Zhu Yib481de92007-09-25 17:54:57 -0700328
Johannes Berg12e934d2010-10-04 05:50:06 -0700329 return priv->beacon_skb->len;
Zhu Yib481de92007-09-25 17:54:57 -0700330}
331
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800332static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700333{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800334 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700335 unsigned int frame_size;
336 int rc;
337 u8 rate;
338
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800339 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700340
341 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800342 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700343 "command.\n");
344 return -ENOMEM;
345 }
346
Johannes Berg76d04812010-08-23 10:46:47 +0200347 rate = iwl_rate_get_lowest_plcp(priv,
348 &priv->contexts[IWL_RXON_CTX_BSS]);
Zhu Yib481de92007-09-25 17:54:57 -0700349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800350 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700351
Samuel Ortiz518099a2009-01-19 15:30:27 -0800352 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700353 &frame->u.cmd[0]);
354
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800355 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700356
357 return rc;
358}
359
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800360static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700361{
Johannes Bergee525d12010-01-21 06:09:28 -0800362 if (priv->_3945.shared_virt)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800363 dma_free_coherent(&priv->pci_dev->dev,
364 sizeof(struct iwl3945_shared),
Johannes Bergee525d12010-01-21 06:09:28 -0800365 priv->_3945.shared_virt,
366 priv->_3945.shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700367}
368
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800369static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200370 struct ieee80211_tx_info *info,
Johannes Bergc2acea82009-07-24 11:13:05 -0700371 struct iwl_device_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700372 struct sk_buff *skb_frag,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800373 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700374{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700375 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Tomas Winklerc587de02009-06-03 11:44:07 -0700376 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -0700377
Johannes Berg97359d12010-08-10 09:46:38 +0200378 tx_cmd->sec_ctl = 0;
379
380 switch (keyinfo->cipher) {
381 case WLAN_CIPHER_SUITE_CCMP:
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700382 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
383 memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
Tomas Winklere1623442009-01-27 14:27:56 -0800384 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -0700385 break;
386
Johannes Berg97359d12010-08-10 09:46:38 +0200387 case WLAN_CIPHER_SUITE_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -0700388 break;
389
Johannes Berg97359d12010-08-10 09:46:38 +0200390 case WLAN_CIPHER_SUITE_WEP104:
391 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
392 /* fall through */
393 case WLAN_CIPHER_SUITE_WEP40:
394 tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +0200395 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -0700396
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700397 memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -0700398
Tomas Winklere1623442009-01-27 14:27:56 -0800399 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +0200400 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -0700401 break;
402
Zhu Yib481de92007-09-25 17:54:57 -0700403 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200404 IWL_ERR(priv, "Unknown encode cipher %x\n", keyinfo->cipher);
Zhu Yib481de92007-09-25 17:54:57 -0700405 break;
406 }
407}
408
409/*
410 * handle build REPLY_TX command notification.
411 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800412static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Johannes Bergc2acea82009-07-24 11:13:05 -0700413 struct iwl_device_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200414 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +0800415 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -0700416{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700417 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
418 __le32 tx_flags = tx_cmd->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700419 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700420
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700421 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200422 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700423 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700424 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700425 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700426 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -0700427 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
428 tx_flags |= TX_CMD_FLG_TSF_MSK;
429 } else {
430 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
431 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
432 }
433
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700434 tx_cmd->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700435 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700436 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
437
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700438 if (ieee80211_is_data_qos(fc)) {
439 u8 *qc = ieee80211_get_qos_ctl(hdr);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700440 tx_cmd->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -0700441 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800442 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700443 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800444 }
Zhu Yib481de92007-09-25 17:54:57 -0700445
Johannes Berg94597ab2010-08-09 10:57:02 -0700446 priv->cfg->ops->utils->tx_cmd_protection(priv, info, fc, &tx_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700447
448 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700449 if (ieee80211_is_mgmt(fc)) {
450 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700451 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -0700452 else
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700453 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700454 } else {
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700455 tx_cmd->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700456 }
Zhu Yib481de92007-09-25 17:54:57 -0700457
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700458 tx_cmd->driver_txop = 0;
459 tx_cmd->tx_flags = tx_flags;
460 tx_cmd->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700461}
462
Zhu Yib481de92007-09-25 17:54:57 -0700463/*
464 * start REPLY_TX command process
465 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800466static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700467{
468 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200469 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700470 struct iwl3945_tx_cmd *tx_cmd;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800471 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800472 struct iwl_queue *q = NULL;
Johannes Bergc2acea82009-07-24 11:13:05 -0700473 struct iwl_device_cmd *out_cmd;
474 struct iwl_cmd_meta *out_meta;
Zhu Yib481de92007-09-25 17:54:57 -0700475 dma_addr_t phys_addr;
476 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800477 int txq_id = skb_get_queue_mapping(skb);
Reinette Chatredf833b12009-04-21 10:55:48 -0700478 u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
Tomas Winkler54dbb522008-05-15 13:54:06 +0800479 u8 id;
480 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700481 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800482 u8 tid = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700483 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700484 u8 wait_write_ptr = 0;
485 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700486
487 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800488 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800489 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700490 goto drop_unlock;
491 }
492
Johannes Berge039fa42008-05-15 12:55:29 +0200493 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800494 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700495 goto drop_unlock;
496 }
497
498 unicast = !is_multicast_ether_addr(hdr->addr1);
499 id = 0;
500
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700501 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700502
Samuel Ortizd08853a2009-01-23 13:45:17 -0800503#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700504 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800505 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700506 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800507 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700508 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800509 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700510#endif
511
Zhu Yib481de92007-09-25 17:54:57 -0700512 spin_unlock_irqrestore(&priv->lock, flags);
513
Harvey Harrison7294ec92008-07-15 18:43:59 -0700514 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800515
Johannes Berg2a87c262010-04-30 11:30:45 -0700516 /* Find index into station table for destination station */
Johannes Berga194e322010-08-27 08:53:46 -0700517 sta_id = iwl_sta_id_or_broadcast(
518 priv, &priv->contexts[IWL_RXON_CTX_BSS],
519 info->control.sta);
Zhu Yib481de92007-09-25 17:54:57 -0700520 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800521 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700522 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700523 goto drop;
524 }
525
Tomas Winklere1623442009-01-27 14:27:56 -0800526 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700527
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700528 if (ieee80211_is_data_qos(fc)) {
Johannes Bergf862a232010-05-03 01:20:52 -0700529 u8 *qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700530 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700531 if (unlikely(tid >= MAX_TID_COUNT))
532 goto drop;
Zhu Yib481de92007-09-25 17:54:57 -0700533 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800534
535 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800536 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700537 q = &txq->q;
538
Zhu Yidc57a302009-12-14 14:12:12 -0800539 if ((iwl_queue_space(q) < q->high_mark))
540 goto drop;
541
Zhu Yib481de92007-09-25 17:54:57 -0700542 spin_lock_irqsave(&priv->lock, flags);
543
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800544 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700545
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800546 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800547 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Johannes Bergff0d91c2010-05-17 02:37:34 -0700548 txq->txb[q->write_ptr].skb = skb;
Johannes Bergc90cbbb2010-08-23 10:46:39 +0200549 txq->txb[q->write_ptr].ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800550
551 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800552 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700553 out_meta = &txq->meta[idx];
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700554 tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700555 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700556 memset(tx_cmd, 0, sizeof(*tx_cmd));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800557
558 /*
559 * Set up the Tx-command (not MAC!) header.
560 * Store the chosen Tx queue and TFD index within the sequence field;
561 * after Tx, uCode's Tx response will return this value so driver can
562 * locate the frame within the tx queue and do post-tx processing.
563 */
Zhu Yib481de92007-09-25 17:54:57 -0700564 out_cmd->hdr.cmd = REPLY_TX;
565 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800566 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800567
568 /* Copy MAC header from skb into command buffer */
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700569 memcpy(tx_cmd->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700570
Reinette Chatredf833b12009-04-21 10:55:48 -0700571
572 if (info->control.hw_key)
573 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
574
575 /* TODO need this for burst mode later on */
576 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
577
578 /* set is_hcca to 0; it probably will never be implemented */
579 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
580
581 /* Total # bytes to be transmitted */
582 len = (u16)skb->len;
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700583 tx_cmd->len = cpu_to_le16(len);
Reinette Chatredf833b12009-04-21 10:55:48 -0700584
Wey-Yi Guy20594eb2009-08-07 15:41:39 -0700585 iwl_dbg_log_tx_data_frame(priv, len, hdr);
Wey-Yi Guy22fdf3c2009-08-07 15:41:40 -0700586 iwl_update_stats(priv, true, fc, len);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700587 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
588 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Reinette Chatredf833b12009-04-21 10:55:48 -0700589
590 if (!ieee80211_has_morefrags(hdr->frame_control)) {
591 txq->need_update = 1;
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*/
FUJITA Tomonori2e724442010-06-03 14:19:20 +0900630 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
631 dma_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
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800676static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700677 struct ieee80211_measurement_params *params,
678 u8 type)
679{
Tomas Winkler600c0e12008-12-19 10:37:04 +0800680 struct iwl_spectrum_cmd spectrum;
Zhu Yi2f301222009-10-09 17:19:45 +0800681 struct iwl_rx_packet *pkt;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800682 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700683 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
684 .data = (void *)&spectrum,
Johannes Bergc2acea82009-07-24 11:13:05 -0700685 .flags = CMD_WANT_SKB,
Zhu Yib481de92007-09-25 17:54:57 -0700686 };
687 u32 add_time = le64_to_cpu(params->start_time);
688 int rc;
689 int spectrum_resp_status;
690 int duration = le16_to_cpu(params->duration);
Johannes Berg246ed352010-08-23 10:46:32 +0200691 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -0700692
Johannes Berg246ed352010-08-23 10:46:32 +0200693 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700694 add_time = iwl_usecs_to_beacons(priv,
Johannes Berge99f1682010-01-19 10:04:28 -0800695 le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
Johannes Berg246ed352010-08-23 10:46:32 +0200696 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700697
698 memset(&spectrum, 0, sizeof(spectrum));
699
700 spectrum.channel_count = cpu_to_le16(1);
701 spectrum.flags =
702 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
703 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
704 cmd.len = sizeof(spectrum);
705 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
706
Johannes Berg246ed352010-08-23 10:46:32 +0200707 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Zhu Yib481de92007-09-25 17:54:57 -0700708 spectrum.start_time =
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700709 iwl_add_beacon_time(priv,
710 priv->_3945.last_beacon_time, add_time,
Johannes Berg246ed352010-08-23 10:46:32 +0200711 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700712 else
713 spectrum.start_time = 0;
714
715 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
716 spectrum.channels[0].channel = params->channel;
717 spectrum.channels[0].type = type;
Johannes Berg246ed352010-08-23 10:46:32 +0200718 if (ctx->active.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700719 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
720 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
721
Samuel Ortiz518099a2009-01-19 15:30:27 -0800722 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700723 if (rc)
724 return rc;
725
Zhu Yi2f301222009-10-09 17:19:45 +0800726 pkt = (struct iwl_rx_packet *)cmd.reply_page;
727 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800728 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700729 rc = -EIO;
730 }
731
Zhu Yi2f301222009-10-09 17:19:45 +0800732 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
Zhu Yib481de92007-09-25 17:54:57 -0700733 switch (spectrum_resp_status) {
734 case 0: /* Command will be handled */
Zhu Yi2f301222009-10-09 17:19:45 +0800735 if (pkt->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800736 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800737 pkt->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700738 priv->measurement_status &= ~MEASUREMENT_READY;
739 }
740 priv->measurement_status |= MEASUREMENT_ACTIVE;
741 rc = 0;
742 break;
743
744 case 1: /* Command will not be handled */
745 rc = -EAGAIN;
746 break;
747 }
748
Zhu Yi64a76b52009-12-10 14:37:21 -0800749 iwl_free_pages(priv, cmd.reply_page);
Zhu Yib481de92007-09-25 17:54:57 -0700750
751 return rc;
752}
Zhu Yib481de92007-09-25 17:54:57 -0700753
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800754static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800755 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700756{
Zhu Yi2f301222009-10-09 17:19:45 +0800757 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800758 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700759 struct delayed_work *pwork;
760
761 palive = &pkt->u.alive_frame;
762
Tomas Winklere1623442009-01-27 14:27:56 -0800763 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700764 "0x%01X 0x%01X\n",
765 palive->is_valid, palive->ver_type,
766 palive->ver_subtype);
767
768 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800769 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800770 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
771 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700772 pwork = &priv->init_alive_start;
773 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800774 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700775 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800776 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700777 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800778 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700779 }
780
781 /* We delay the ALIVE response by 5ms to
782 * give the HW RF Kill time to activate... */
783 if (palive->is_valid == UCODE_VALID_OK)
784 queue_delayed_work(priv->workqueue, pwork,
785 msecs_to_jiffies(5));
786 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800787 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700788}
789
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800790static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800791 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700792{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100793#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800794 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Helmut Schaac7e035a2009-01-19 13:02:15 +0100795#endif
Zhu Yib481de92007-09-25 17:54:57 -0700796
Tomas Winklere1623442009-01-27 14:27:56 -0800797 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700798}
799
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800800static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700801{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800802 struct iwl_priv *priv =
803 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700804 struct sk_buff *beacon;
805
806 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg8bd413e2010-08-23 10:46:40 +0200807 beacon = ieee80211_beacon_get(priv->hw,
808 priv->contexts[IWL_RXON_CTX_BSS].vif);
Zhu Yib481de92007-09-25 17:54:57 -0700809
810 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800811 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700812 return;
813 }
814
815 mutex_lock(&priv->mutex);
816 /* new beacon skb is allocated every time; dispose previous.*/
Johannes Berg12e934d2010-10-04 05:50:06 -0700817 if (priv->beacon_skb)
818 dev_kfree_skb(priv->beacon_skb);
Zhu Yib481de92007-09-25 17:54:57 -0700819
Johannes Berg12e934d2010-10-04 05:50:06 -0700820 priv->beacon_skb = beacon;
Zhu Yib481de92007-09-25 17:54:57 -0700821 mutex_unlock(&priv->mutex);
822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700824}
825
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800826static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800827 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700828{
Zhu Yi2f301222009-10-09 17:19:45 +0800829 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800830 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Johannes Berga85d7cc2010-07-31 08:34:10 -0700831#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700832 u8 rate = beacon->beacon_notify_hdr.rate;
833
Tomas Winklere1623442009-01-27 14:27:56 -0800834 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700835 "tsf %d %d rate %d\n",
836 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
837 beacon->beacon_notify_hdr.failure_frame,
838 le32_to_cpu(beacon->ibss_mgr_status),
839 le32_to_cpu(beacon->high_tsf),
840 le32_to_cpu(beacon->low_tsf), rate);
841#endif
842
Johannes Berga85d7cc2010-07-31 08:34:10 -0700843 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
844
Johannes Berg05c914f2008-09-11 00:01:58 +0200845 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700846 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
847 queue_work(priv->workqueue, &priv->beacon_update);
848}
849
Zhu Yib481de92007-09-25 17:54:57 -0700850/* Handle notification from uCode that card's power state is changing
851 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800852static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800853 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700854{
Zhu Yi2f301222009-10-09 17:19:45 +0800855 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700856 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
857 unsigned long status = priv->status;
858
Reinette Chatre4c423a22009-07-17 09:30:26 -0700859 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700860 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
861 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
862
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800863 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700864 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
865
866 if (flags & HW_CARD_DISABLED)
867 set_bit(STATUS_RF_KILL_HW, &priv->status);
868 else
869 clear_bit(STATUS_RF_KILL_HW, &priv->status);
870
871
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800872 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700873
874 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200875 test_bit(STATUS_RF_KILL_HW, &priv->status)))
876 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
877 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700878 else
879 wake_up_interruptible(&priv->wait_command_queue);
880}
881
882/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800883 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700884 *
885 * Setup the RX handlers for each of the reply types sent from the uCode
886 * to the host.
887 *
888 * This function chains into the hardware specific files for them to setup
889 * any hardware specific handlers as well.
890 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800891static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700892{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800893 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
894 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800895 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800896 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Reinette Chatre81963d62010-01-22 14:22:57 -0800897 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
898 iwl_rx_spectrum_measure_notif;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800899 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700900 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800901 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800902 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700903
Ben Cahill9fbab512007-11-29 11:09:47 +0800904 /*
905 * The same handler is used for both the REPLY to a discrete
906 * statistics request from the host as well as for the periodic
907 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700908 */
Abhijeet Kolekar17f36fc2010-04-16 10:03:54 -0700909 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800910 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700911
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800912 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800913 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700914
Ben Cahill9fbab512007-11-29 11:09:47 +0800915 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800916 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700917}
918
Zhu Yib481de92007-09-25 17:54:57 -0700919/************************** RX-FUNCTIONS ****************************/
920/*
921 * Rx theory of operation
922 *
923 * The host allocates 32 DMA target addresses and passes the host address
924 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
925 * 0 to 31
926 *
927 * Rx Queue Indexes
928 * The host/firmware share two index registers for managing the Rx buffers.
929 *
930 * The READ index maps to the first position that the firmware may be writing
931 * to -- the driver can read up to (but not including) this position and get
932 * good data.
933 * The READ index is managed by the firmware once the card is enabled.
934 *
935 * The WRITE index maps to the last position the driver has read from -- the
936 * position preceding WRITE is the last slot the firmware can place a packet.
937 *
938 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
939 * WRITE = READ.
940 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800941 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -0700942 * INDEX position, and WRITE to the last (READ - 1 wrapped)
943 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800944 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -0700945 * and fire the RX interrupt. The driver can then query the READ index and
946 * process as many packets as possible, moving the WRITE index forward as it
947 * resets the Rx queue buffers with new memory.
948 *
949 * The management in the driver is as follows:
950 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
951 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +0800952 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800953 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -0700954 * iwl->rxq is replenished and the READ INDEX is updated (updating the
955 * 'processed' and 'read' driver indexes as well)
956 * + A received packet is processed and handed to the kernel network stack,
957 * detached from the iwl->rxq. The driver 'processed' index is updated.
958 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
959 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
960 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
961 * were enough free buffers and RX_STALLED is set it is cleared.
962 *
963 *
964 * Driver sequence:
965 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800966 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800967 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +0800968 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -0700969 * queue, updates firmware pointers, and updates
970 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800971 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -0700972 *
973 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800974 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -0700975 * READ INDEX, detaching the SKB from the pool.
976 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800977 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -0700978 * slots.
979 * ...
980 *
981 */
982
983/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800984 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -0700985 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800986static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700987 dma_addr_t dma_addr)
988{
989 return cpu_to_le32((u32)dma_addr);
990}
991
992/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800993 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -0700994 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800995 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -0700996 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +0800997 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -0700998 *
999 * This moves the 'write' index forward to catch up with 'processed', and
1000 * also updates the memory address in the firmware to reference the new
1001 * target buffer.
1002 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001003static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001004{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001005 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001006 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001007 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001008 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001009 int write;
Zhu Yib481de92007-09-25 17:54:57 -07001010
1011 spin_lock_irqsave(&rxq->lock, flags);
1012 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001013 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001014 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001015 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001016 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001017 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001018
1019 /* Point to Rx buffer via next RBD in circular buffer */
Zhu Yi2f301222009-10-09 17:19:45 +08001020 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
Zhu Yib481de92007-09-25 17:54:57 -07001021 rxq->queue[rxq->write] = rxb;
1022 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1023 rxq->free_count--;
1024 }
1025 spin_unlock_irqrestore(&rxq->lock, flags);
1026 /* If the pre-allocated buffer pool is dropping low, schedule to
1027 * refill it */
1028 if (rxq->free_count <= RX_LOW_WATERMARK)
1029 queue_work(priv->workqueue, &priv->rx_replenish);
1030
1031
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001032 /* If we've added more space for the firmware to place data, tell it.
1033 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001034 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001035 || (abs(rxq->write - rxq->read) > 7)) {
1036 spin_lock_irqsave(&rxq->lock, flags);
1037 rxq->need_update = 1;
1038 spin_unlock_irqrestore(&rxq->lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001039 iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001040 }
Zhu Yib481de92007-09-25 17:54:57 -07001041}
1042
1043/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001044 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001045 *
1046 * When moving to rx_free an SKB is allocated for the slot.
1047 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001048 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001049 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001050 */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001051static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001052{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001053 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001054 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001055 struct iwl_rx_mem_buffer *rxb;
Zhu Yi2f301222009-10-09 17:19:45 +08001056 struct page *page;
Zhu Yib481de92007-09-25 17:54:57 -07001057 unsigned long flags;
Zhu Yi29b1b262009-10-23 13:42:25 -07001058 gfp_t gfp_mask = priority;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001059
1060 while (1) {
1061 spin_lock_irqsave(&rxq->lock, flags);
1062
1063 if (list_empty(&rxq->rx_used)) {
1064 spin_unlock_irqrestore(&rxq->lock, flags);
1065 return;
1066 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001067 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001068
Reinette Chatref82a9242009-09-17 10:43:56 -07001069 if (rxq->free_count > RX_LOW_WATERMARK)
Zhu Yi29b1b262009-10-23 13:42:25 -07001070 gfp_mask |= __GFP_NOWARN;
Zhu Yi2f301222009-10-09 17:19:45 +08001071
1072 if (priv->hw_params.rx_page_order > 0)
Zhu Yi29b1b262009-10-23 13:42:25 -07001073 gfp_mask |= __GFP_COMP;
Zhu Yi2f301222009-10-09 17:19:45 +08001074
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001075 /* Alloc a new receive buffer */
Zhu Yi29b1b262009-10-23 13:42:25 -07001076 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
Zhu Yi2f301222009-10-09 17:19:45 +08001077 if (!page) {
Zhu Yib481de92007-09-25 17:54:57 -07001078 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001079 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1080 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1081 net_ratelimit())
1082 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1083 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1084 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001085 /* We don't reschedule replenish work here -- we will
1086 * call the restock method and if it still needs
1087 * more buffers it will schedule replenish */
1088 break;
1089 }
Zhu Yi12342c42007-12-20 11:27:32 +08001090
Reinette Chatrede0bd502009-09-11 10:38:12 -07001091 spin_lock_irqsave(&rxq->lock, flags);
1092 if (list_empty(&rxq->rx_used)) {
1093 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001094 __free_pages(page, priv->hw_params.rx_page_order);
Reinette Chatrede0bd502009-09-11 10:38:12 -07001095 return;
1096 }
1097 element = rxq->rx_used.next;
1098 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1099 list_del(element);
1100 spin_unlock_irqrestore(&rxq->lock, flags);
1101
Zhu Yi2f301222009-10-09 17:19:45 +08001102 rxb->page = page;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001103 /* Get physical address of RB/SKB */
Zhu Yi2f301222009-10-09 17:19:45 +08001104 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1105 PAGE_SIZE << priv->hw_params.rx_page_order,
1106 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001107
1108 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001109
Zhu Yib481de92007-09-25 17:54:57 -07001110 list_add_tail(&rxb->list, &rxq->rx_free);
1111 rxq->free_count++;
Zhu Yi2f301222009-10-09 17:19:45 +08001112 priv->alloc_rxb_page++;
1113
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001114 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001115 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001116}
1117
Reinette Chatredf833b12009-04-21 10:55:48 -07001118void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1119{
1120 unsigned long flags;
1121 int i;
1122 spin_lock_irqsave(&rxq->lock, flags);
1123 INIT_LIST_HEAD(&rxq->rx_free);
1124 INIT_LIST_HEAD(&rxq->rx_used);
1125 /* Fill the rx_used queue with _all_ of the Rx buffers */
1126 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1127 /* In the reset function, these buffers may have been allocated
1128 * to an SKB, so we need to unmap and free potential storage */
Zhu Yi2f301222009-10-09 17:19:45 +08001129 if (rxq->pool[i].page != NULL) {
1130 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1131 PAGE_SIZE << priv->hw_params.rx_page_order,
1132 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001133 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001134 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001135 }
1136 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1137 }
1138
1139 /* Set us so that we have processed and used all buffers, but have
1140 * not restocked the Rx queue with fresh buffers */
1141 rxq->read = rxq->write = 0;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001142 rxq->write_actual = 0;
Zhu Yi2f301222009-10-09 17:19:45 +08001143 rxq->free_count = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001144 spin_unlock_irqrestore(&rxq->lock, flags);
1145}
Reinette Chatredf833b12009-04-21 10:55:48 -07001146
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001147void iwl3945_rx_replenish(void *data)
1148{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001149 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001150 unsigned long flags;
1151
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001152 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001153
1154 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001155 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001156 spin_unlock_irqrestore(&priv->lock, flags);
1157}
1158
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001159static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1160{
1161 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1162
1163 iwl3945_rx_queue_restock(priv);
1164}
1165
1166
Reinette Chatredf833b12009-04-21 10:55:48 -07001167/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1168 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1169 * This free routine walks the list of POOL entries and if SKB is set to
1170 * non NULL it is unmapped and freed
1171 */
1172static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1173{
1174 int i;
1175 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
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 }
1184
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001185 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
Emmanuel Grumbachd5b25c92010-06-07 13:21:46 -07001186 rxq->bd_dma);
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001187 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1188 rxq->rb_stts, rxq->rb_stts_dma);
Reinette Chatredf833b12009-04-21 10:55:48 -07001189 rxq->bd = NULL;
1190 rxq->rb_stts = NULL;
1191}
Reinette Chatredf833b12009-04-21 10:55:48 -07001192
1193
Zhu Yib481de92007-09-25 17:54:57 -07001194/* Convert linear signal-to-noise ratio into dB */
1195static u8 ratio2dB[100] = {
1196/* 0 1 2 3 4 5 6 7 8 9 */
1197 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1198 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1199 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1200 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1201 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1202 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1203 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1204 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1205 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1206 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1207};
1208
1209/* Calculates a relative dB value from a ratio of linear
1210 * (i.e. not dB) signal levels.
1211 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001212int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001213{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001214 /* 1000:1 or higher just report as 60 dB */
1215 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001216 return 60;
1217
Adrian Bunk221c80c2008-02-02 23:19:01 +02001218 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001219 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001220 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001221 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001222
1223 /* We shouldn't see this */
1224 if (sig_ratio < 1)
1225 return 0;
1226
1227 /* Use table for ratios 1:1 - 99:1 */
1228 return (int)ratio2dB[sig_ratio];
1229}
1230
Zhu Yib481de92007-09-25 17:54:57 -07001231/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001232 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001233 *
1234 * Uses the priv->rx_handlers callback function array to invoke
1235 * the appropriate handlers, including command responses,
1236 * frame-received notifications, and other notifications.
1237 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001238static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001239{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001240 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001241 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001242 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001243 u32 r, i;
1244 int reclaim;
1245 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001246 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001247 u32 count = 8;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001248 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001249
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001250 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1251 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001252 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001253 i = rxq->read;
1254
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001255 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -07001256 total_empty = r - rxq->write_actual;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001257 if (total_empty < 0)
1258 total_empty += RX_QUEUE_SIZE;
1259
1260 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001261 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001262 /* Rx interrupt, but nothing sent from uCode */
1263 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001264 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001265
1266 while (i != r) {
Johannes Bergf4989d92010-05-28 04:08:30 -07001267 int len;
1268
Zhu Yib481de92007-09-25 17:54:57 -07001269 rxb = rxq->queue[i];
1270
Ben Cahill9fbab512007-11-29 11:09:47 +08001271 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001272 * then a bug has been introduced in the queue refilling
1273 * routines -- catch it here */
1274 BUG_ON(rxb == NULL);
1275
1276 rxq->queue[i] = NULL;
1277
Zhu Yi2f301222009-10-09 17:19:45 +08001278 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1279 PAGE_SIZE << priv->hw_params.rx_page_order,
1280 PCI_DMA_FROMDEVICE);
1281 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001282
Johannes Bergf4989d92010-05-28 04:08:30 -07001283 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1284 len += sizeof(u32); /* account for status word */
1285 trace_iwlwifi_dev_rx(priv, pkt, len);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001286
Zhu Yib481de92007-09-25 17:54:57 -07001287 /* Reclaim a command buffer only if this packet is a response
1288 * to a (driver-originated) command.
1289 * If the packet (e.g. Rx frame) originated from uCode,
1290 * there is no command buffer to reclaim.
1291 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1292 * but apparently a few don't get set; catch them here. */
1293 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1294 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1295 (pkt->hdr.cmd != REPLY_TX);
1296
1297 /* Based on type of command response or notification,
1298 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001299 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001300 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001301 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001302 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001303 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -07001304 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001305 } else {
1306 /* No handling needed */
Zhu Yi2f301222009-10-09 17:19:45 +08001307 IWL_DEBUG_RX(priv,
1308 "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001309 r, i, get_cmd_string(pkt->hdr.cmd),
1310 pkt->hdr.cmd);
1311 }
1312
Zhu Yi29b1b262009-10-23 13:42:25 -07001313 /*
1314 * XXX: After here, we should always check rxb->page
1315 * against NULL before touching it or its virtual
1316 * memory (pkt). Because some rx_handler might have
1317 * already taken or freed the pages.
1318 */
1319
Zhu Yib481de92007-09-25 17:54:57 -07001320 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001321 /* Invoke any callbacks, transfer the buffer to caller,
1322 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001323 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001324 if (rxb->page)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001325 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001326 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001327 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001328 }
1329
Zhu Yi73005152009-10-23 13:42:32 -07001330 /* Reuse the page if possible. For notification packets and
1331 * SKBs that fail to Rx correctly, add them back into the
1332 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001333 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001334 if (rxb->page != NULL) {
1335 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1336 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1337 PCI_DMA_FROMDEVICE);
1338 list_add_tail(&rxb->list, &rxq->rx_free);
1339 rxq->free_count++;
1340 } else
1341 list_add_tail(&rxb->list, &rxq->rx_used);
1342
Zhu Yib481de92007-09-25 17:54:57 -07001343 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001344
Zhu Yib481de92007-09-25 17:54:57 -07001345 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001346 /* If there are a lot of unused frames,
1347 * restock the Rx queue so ucode won't assert. */
1348 if (fill_rx) {
1349 count++;
1350 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001351 rxq->read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001352 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001353 count = 0;
1354 }
1355 }
Zhu Yib481de92007-09-25 17:54:57 -07001356 }
1357
1358 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001359 rxq->read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001360 if (fill_rx)
1361 iwl3945_rx_replenish_now(priv);
1362 else
1363 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001364}
1365
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001366/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001367static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001368{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001369 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001370 synchronize_irq(priv->pci_dev->irq);
1371 tasklet_kill(&priv->irq_tasklet);
1372}
1373
Zhu Yib481de92007-09-25 17:54:57 -07001374static const char *desc_lookup(int i)
1375{
1376 switch (i) {
1377 case 1:
1378 return "FAIL";
1379 case 2:
1380 return "BAD_PARAM";
1381 case 3:
1382 return "BAD_CHECKSUM";
1383 case 4:
1384 return "NMI_INTERRUPT";
1385 case 5:
1386 return "SYSASSERT";
1387 case 6:
1388 return "FATAL_ERROR";
1389 }
1390
1391 return "UNKNOWN";
1392}
1393
1394#define ERROR_START_OFFSET (1 * sizeof(u32))
1395#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1396
Reinette Chatreb7a79402009-09-25 14:24:23 -07001397void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001398{
1399 u32 i;
1400 u32 desc, time, count, base, data1;
1401 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001402
1403 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001405 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001406 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001407 return;
1408 }
1409
Zhu Yib481de92007-09-25 17:54:57 -07001410
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001411 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001412
1413 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001414 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1415 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1416 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001417 }
1418
Winkler, Tomas15b16872008-12-19 10:37:33 +08001419 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001420 "ilink1 nmiPC Line\n");
1421 for (i = ERROR_START_OFFSET;
1422 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1423 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001424 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001425 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001426 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001427 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001428 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001429 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001430 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001431 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001432 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001433 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001434 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001435 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001436 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001437
Winkler, Tomas15b16872008-12-19 10:37:33 +08001438 IWL_ERR(priv,
Jay Sternberg87563712010-06-14 14:40:40 -07001439 "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +08001440 desc_lookup(desc), desc, time, blink1, blink2,
1441 ilink1, ilink2, data1);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001442 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1443 0, blink1, blink2, ilink1, ilink2);
Zhu Yib481de92007-09-25 17:54:57 -07001444 }
Zhu Yib481de92007-09-25 17:54:57 -07001445}
1446
Ben Cahillf58177b2007-11-29 11:09:43 +08001447#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001448
1449/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001450 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001451 *
Zhu Yib481de92007-09-25 17:54:57 -07001452 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001453static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1454 u32 num_events, u32 mode,
1455 int pos, char **buf, size_t bufsz)
Zhu Yib481de92007-09-25 17:54:57 -07001456{
1457 u32 i;
1458 u32 base; /* SRAM byte address of event log header */
1459 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1460 u32 ptr; /* SRAM byte address of log data */
1461 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001462 unsigned long reg_flags;
Zhu Yib481de92007-09-25 17:54:57 -07001463
1464 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001465 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001466
1467 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1468
1469 if (mode == 0)
1470 event_size = 2 * sizeof(u32);
1471 else
1472 event_size = 3 * sizeof(u32);
1473
1474 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1475
Ben Cahille5854472009-11-06 14:52:58 -08001476 /* Make sure device is powered up for SRAM reads */
1477 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1478 iwl_grab_nic_access(priv);
1479
1480 /* Set starting address; reads will auto-increment */
1481 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1482 rmb();
1483
Zhu Yib481de92007-09-25 17:54:57 -07001484 /* "time" is actually "data" for mode 0 (no timestamp).
1485 * place event id # at far right for easier visual parsing. */
1486 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001487 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1488 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001489 if (mode == 0) {
1490 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001491 if (bufsz) {
1492 pos += scnprintf(*buf + pos, bufsz - pos,
1493 "0x%08x:%04u\n",
1494 time, ev);
1495 } else {
1496 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1497 trace_iwlwifi_dev_ucode_event(priv, 0,
1498 time, ev);
1499 }
Winkler, Tomas15b16872008-12-19 10:37:33 +08001500 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001501 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001502 if (bufsz) {
1503 pos += scnprintf(*buf + pos, bufsz - pos,
1504 "%010u:0x%08x:%04u\n",
1505 time, data, ev);
1506 } else {
1507 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1508 time, data, ev);
1509 trace_iwlwifi_dev_ucode_event(priv, time,
1510 data, ev);
1511 }
Zhu Yib481de92007-09-25 17:54:57 -07001512 }
1513 }
Ben Cahille5854472009-11-06 14:52:58 -08001514
1515 /* Allow device to power down */
1516 iwl_release_nic_access(priv);
1517 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001518 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001519}
1520
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001521/**
1522 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1523 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001524static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001525 u32 num_wraps, u32 next_entry,
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001526 u32 size, u32 mode,
1527 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001528{
1529 /*
1530 * display the newest DEFAULT_LOG_ENTRIES entries
1531 * i.e the entries just before the next ont that uCode would fill.
1532 */
1533 if (num_wraps) {
1534 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001535 pos = iwl3945_print_event_log(priv,
1536 capacity - (size - next_entry),
1537 size - next_entry, mode,
1538 pos, buf, bufsz);
1539 pos = iwl3945_print_event_log(priv, 0,
1540 next_entry, mode,
1541 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001542 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001543 pos = iwl3945_print_event_log(priv, next_entry - size,
1544 size, mode,
1545 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001546 } else {
1547 if (next_entry < size)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001548 pos = iwl3945_print_event_log(priv, 0,
1549 next_entry, mode,
1550 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001551 else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001552 pos = iwl3945_print_event_log(priv, next_entry - size,
1553 size, mode,
1554 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001555 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001556 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001557}
1558
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001559#define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1560
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001561int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1562 char **buf, bool display)
Zhu Yib481de92007-09-25 17:54:57 -07001563{
Zhu Yib481de92007-09-25 17:54:57 -07001564 u32 base; /* SRAM byte address of event log header */
1565 u32 capacity; /* event log capacity in # entries */
1566 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1567 u32 num_wraps; /* # times uCode wrapped to top of log */
1568 u32 next_entry; /* index of next entry to be written by uCode */
1569 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001570 int pos = 0;
1571 size_t bufsz = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001572
1573 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001574 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001575 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001576 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001577 }
1578
Zhu Yib481de92007-09-25 17:54:57 -07001579 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001580 capacity = iwl_read_targ_mem(priv, base);
1581 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1582 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1583 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001584
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07001585 if (capacity > priv->cfg->base_params->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001586 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07001587 capacity, priv->cfg->base_params->max_event_log_size);
1588 capacity = priv->cfg->base_params->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001589 }
1590
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07001591 if (next_entry > priv->cfg->base_params->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001592 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07001593 next_entry, priv->cfg->base_params->max_event_log_size);
1594 next_entry = priv->cfg->base_params->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001595 }
1596
Zhu Yib481de92007-09-25 17:54:57 -07001597 size = num_wraps ? capacity : next_entry;
1598
1599 /* bail out if nothing in log */
1600 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001601 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001602 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001603 }
1604
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001605#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001606 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001607 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1608 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1609#else
1610 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1611 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1612#endif
1613
1614 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1615 size);
Zhu Yib481de92007-09-25 17:54:57 -07001616
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001617#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001618 if (display) {
1619 if (full_log)
1620 bufsz = capacity * 48;
1621 else
1622 bufsz = size * 48;
1623 *buf = kmalloc(bufsz, GFP_KERNEL);
1624 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001625 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001626 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001627 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1628 /* if uCode has wrapped back to top of log,
1629 * start at the oldest entry,
1630 * i.e the next one that uCode would fill.
1631 */
1632 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001633 pos = iwl3945_print_event_log(priv, next_entry,
1634 capacity - next_entry, mode,
1635 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001636
1637 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001638 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1639 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001640 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001641 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1642 next_entry, size, mode,
1643 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001644#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001645 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1646 next_entry, size, mode,
1647 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001648#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001649 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001650}
1651
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001652static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001653{
1654 u32 inta, handled = 0;
1655 u32 inta_fh;
1656 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001657#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001658 u32 inta_mask;
1659#endif
1660
1661 spin_lock_irqsave(&priv->lock, flags);
1662
1663 /* Ack/clear/reset pending uCode interrupts.
1664 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1665 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001666 inta = iwl_read32(priv, CSR_INT);
1667 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001668
1669 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1670 * Any new interrupts that happen after this, either while we're
1671 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001672 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1673 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001674
Samuel Ortizd08853a2009-01-23 13:45:17 -08001675#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001676 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001677 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001678 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001679 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001680 inta, inta_mask, inta_fh);
1681 }
1682#endif
1683
Zhu Yi2f301222009-10-09 17:19:45 +08001684 spin_unlock_irqrestore(&priv->lock, flags);
1685
Zhu Yib481de92007-09-25 17:54:57 -07001686 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1687 * atomic, make sure that inta covers all the interrupts that
1688 * we've discovered, even if FH interrupt came in just after
1689 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001690 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001691 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001692 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001693 inta |= CSR_INT_BIT_FH_TX;
1694
1695 /* Now service all interrupt bits discovered above. */
1696 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001697 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001698
1699 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001700 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001701
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001702 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001703 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001704
1705 handled |= CSR_INT_BIT_HW_ERR;
1706
Zhu Yib481de92007-09-25 17:54:57 -07001707 return;
1708 }
1709
Samuel Ortizd08853a2009-01-23 13:45:17 -08001710#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001711 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001712 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001713 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001714 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001715 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001716 priv->isr_stats.sch++;
1717 }
Zhu Yib481de92007-09-25 17:54:57 -07001718
1719 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001720 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001721 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001722 priv->isr_stats.alive++;
1723 }
Zhu Yib481de92007-09-25 17:54:57 -07001724 }
1725#endif
1726 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001727 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001728
Zhu Yib481de92007-09-25 17:54:57 -07001729 /* Error detected by uCode */
1730 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001731 IWL_ERR(priv, "Microcode SW error detected. "
1732 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001733 priv->isr_stats.sw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001734 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001735 handled |= CSR_INT_BIT_SW_ERR;
1736 }
1737
1738 /* uCode wakes up after power-down sleep */
1739 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001740 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001741 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001742 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1743 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1744 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1745 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1746 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1747 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001748
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001749 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001750 handled |= CSR_INT_BIT_WAKEUP;
1751 }
1752
1753 /* All uCode command responses, including Tx command responses,
1754 * Rx "responses" (frame-received notification), and other
1755 * notifications from uCode come through here*/
1756 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001757 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001758 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001759 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1760 }
1761
1762 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001763 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001764 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001765
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001766 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001767 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1768 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001769 handled |= CSR_INT_BIT_FH_TX;
1770 }
1771
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001772 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001773 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001774 priv->isr_stats.unhandled++;
1775 }
Zhu Yib481de92007-09-25 17:54:57 -07001776
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001777 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001778 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001779 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001780 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001781 }
1782
1783 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001784 /* only Re-enable if disabled by irq */
1785 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001786 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001787
Samuel Ortizd08853a2009-01-23 13:45:17 -08001788#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001789 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001790 inta = iwl_read32(priv, CSR_INT);
1791 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1792 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001793 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001794 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1795 }
1796#endif
Zhu Yib481de92007-09-25 17:54:57 -07001797}
1798
Abhijeet Kolekar14023642010-06-02 21:15:10 -07001799static int iwl3945_get_single_channel_for_scan(struct iwl_priv *priv,
1800 struct ieee80211_vif *vif,
1801 enum ieee80211_band band,
1802 struct iwl3945_scan_channel *scan_ch)
1803{
1804 const struct ieee80211_supported_band *sband;
1805 u16 passive_dwell = 0;
1806 u16 active_dwell = 0;
1807 int added = 0;
1808 u8 channel = 0;
1809
1810 sband = iwl_get_hw_mode(priv, band);
1811 if (!sband) {
1812 IWL_ERR(priv, "invalid band\n");
1813 return added;
1814 }
1815
1816 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
1817 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
1818
1819 if (passive_dwell <= active_dwell)
1820 passive_dwell = active_dwell + 1;
1821
1822
1823 channel = iwl_get_single_channel_number(priv, band);
1824
1825 if (channel) {
1826 scan_ch->channel = channel;
1827 scan_ch->type = 0; /* passive */
1828 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1829 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1830 /* Set txpower levels to defaults */
1831 scan_ch->tpc.dsp_atten = 110;
1832 if (band == IEEE80211_BAND_5GHZ)
1833 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1834 else
1835 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1836 added++;
1837 } else
1838 IWL_ERR(priv, "no valid channel found\n");
1839 return added;
1840}
1841
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001842static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001843 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001844 u8 is_active, u8 n_probes,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001845 struct iwl3945_scan_channel *scan_ch,
1846 struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07001847{
Johannes Berg4e05c232009-06-19 13:52:44 -07001848 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001849 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001850 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001851 u16 passive_dwell = 0;
1852 u16 active_dwell = 0;
1853 int added, i;
1854
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001855 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001856 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001857 return 0;
1858
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001859 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001860 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Zhu Yib481de92007-09-25 17:54:57 -07001861
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001862 if (passive_dwell <= active_dwell)
1863 passive_dwell = active_dwell + 1;
1864
Johannes Berg4e05c232009-06-19 13:52:44 -07001865 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1866 chan = priv->scan_request->channels[i];
1867
1868 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001869 continue;
1870
Johannes Berg4e05c232009-06-19 13:52:44 -07001871 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001872
Samuel Ortize6148912009-01-23 13:45:15 -08001873 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001874 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001875 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001876 scan_ch->channel);
1877 continue;
1878 }
1879
Zhu Yib481de92007-09-25 17:54:57 -07001880 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1881 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001882 /* If passive , set up for auto-switch
1883 * and use long active_dwell time.
1884 */
1885 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001886 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001887 scan_ch->type = 0; /* passive */
1888 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1889 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1890 } else {
1891 scan_ch->type = 1; /* active */
1892 }
1893
1894 /* Set direct probe bits. These may be used both for active
1895 * scan channels (probes gets sent right away),
1896 * or for passive channels (probes get se sent only after
1897 * hearing clear Rx packet).*/
1898 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1899 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001900 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001901 } else {
1902 /* uCode v1 does not allow setting direct probe bits on
1903 * passive channel. */
1904 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001905 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001906 }
Zhu Yib481de92007-09-25 17:54:57 -07001907
Ben Cahill9fbab512007-11-29 11:09:47 +08001908 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001909 scan_ch->tpc.dsp_atten = 110;
1910 /* scan_pwr_info->tpc.dsp_atten; */
1911
1912 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001913 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001914 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1915 else {
1916 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1917 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001918 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001919 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001920 */
1921 }
1922
Tomas Winklere1623442009-01-27 14:27:56 -08001923 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001924 scan_ch->channel,
1925 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1926 (scan_ch->type & 1) ?
1927 active_dwell : passive_dwell);
1928
1929 scan_ch++;
1930 added++;
1931 }
1932
Frans Pop91dd6c22010-03-24 14:19:58 -07001933 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001934 return added;
1935}
1936
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001937static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001938 struct ieee80211_rate *rates)
1939{
1940 int i;
1941
Dan Carpenter8e1a53c2010-03-28 14:55:00 +03001942 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001943 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1944 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1945 rates[i].hw_value_short = i;
1946 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001947 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001948 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001949 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001950 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001951 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001952 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001953 }
Zhu Yib481de92007-09-25 17:54:57 -07001954 }
1955}
1956
Zhu Yib481de92007-09-25 17:54:57 -07001957/******************************************************************************
1958 *
1959 * uCode download functions
1960 *
1961 ******************************************************************************/
1962
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001963static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001964{
Tomas Winkler98c92212008-01-14 17:46:20 -08001965 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1966 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1967 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1968 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1969 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1970 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001971}
1972
1973/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001974 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001975 * looking at all data.
1976 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001977static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001978{
1979 u32 val;
1980 u32 save_len = len;
1981 int rc = 0;
1982 u32 errcnt;
1983
Tomas Winklere1623442009-01-27 14:27:56 -08001984 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001985
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001986 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001987 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07001988
1989 errcnt = 0;
1990 for (; len > 0; len -= sizeof(u32), image++) {
1991 /* read data comes through single port, auto-incr addr */
1992 /* NOTE: Use the debugless read so we don't flood kernel log
1993 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001994 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001995 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001996 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001997 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1998 save_len - len, val, le32_to_cpu(*image));
1999 rc = -EIO;
2000 errcnt++;
2001 if (errcnt >= 20)
2002 break;
2003 }
2004 }
2005
Zhu Yib481de92007-09-25 17:54:57 -07002006
2007 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002008 IWL_DEBUG_INFO(priv,
2009 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002010
2011 return rc;
2012}
2013
2014
2015/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002016 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002017 * using sample data 100 bytes apart. If these sample points are good,
2018 * it's a pretty good bet that everything between them is good, too.
2019 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002020static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002021{
2022 u32 val;
2023 int rc = 0;
2024 u32 errcnt = 0;
2025 u32 i;
2026
Tomas Winklere1623442009-01-27 14:27:56 -08002027 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002028
Zhu Yib481de92007-09-25 17:54:57 -07002029 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2030 /* read data comes through single port, auto-incr addr */
2031 /* NOTE: Use the debugless read so we don't flood kernel log
2032 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002033 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002034 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002035 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002036 if (val != le32_to_cpu(*image)) {
2037#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002038 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002039 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2040 i, val, *image);
2041#endif
2042 rc = -EIO;
2043 errcnt++;
2044 if (errcnt >= 3)
2045 break;
2046 }
2047 }
2048
Zhu Yib481de92007-09-25 17:54:57 -07002049 return rc;
2050}
2051
2052
2053/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002054 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002055 * and verify its contents
2056 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002057static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002058{
2059 __le32 *image;
2060 u32 len;
2061 int rc = 0;
2062
2063 /* Try bootstrap */
2064 image = (__le32 *)priv->ucode_boot.v_addr;
2065 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002066 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002067 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002068 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002069 return 0;
2070 }
2071
2072 /* Try initialize */
2073 image = (__le32 *)priv->ucode_init.v_addr;
2074 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002075 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002076 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002077 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002078 return 0;
2079 }
2080
2081 /* Try runtime/protocol */
2082 image = (__le32 *)priv->ucode_code.v_addr;
2083 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002084 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002085 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002086 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002087 return 0;
2088 }
2089
Winkler, Tomas15b16872008-12-19 10:37:33 +08002090 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002091
Ben Cahill9fbab512007-11-29 11:09:47 +08002092 /* Since nothing seems to match, show first several data entries in
2093 * instruction SRAM, so maybe visual inspection will give a clue.
2094 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002095 image = (__le32 *)priv->ucode_boot.v_addr;
2096 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002097 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002098
2099 return rc;
2100}
2101
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002102static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002103{
2104 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002105 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002106}
2107
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002108#define IWL3945_UCODE_GET(item) \
2109static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
2110{ \
2111 return le32_to_cpu(ucode->u.v1.item); \
2112}
2113
2114static u32 iwl3945_ucode_get_header_size(u32 api_ver)
2115{
Johannes Berg22adba22010-04-28 12:09:14 -07002116 return 24;
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002117}
2118
2119static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
2120{
2121 return (u8 *) ucode->u.v1.data;
2122}
2123
2124IWL3945_UCODE_GET(inst_size);
2125IWL3945_UCODE_GET(data_size);
2126IWL3945_UCODE_GET(init_size);
2127IWL3945_UCODE_GET(init_data_size);
2128IWL3945_UCODE_GET(boot_size);
2129
Zhu Yib481de92007-09-25 17:54:57 -07002130/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002131 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002132 *
2133 * Copy into buffers for card to fetch via bus-mastering
2134 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002135static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002136{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002137 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002138 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002139 const struct firmware *ucode_raw;
2140 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002141 const char *name_pre = priv->cfg->fw_name_pre;
2142 const unsigned int api_max = priv->cfg->ucode_api_max;
2143 const unsigned int api_min = priv->cfg->ucode_api_min;
2144 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002145 u8 *src;
2146 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002147 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002148
2149 /* Ask kernel firmware_class module to get the boot firmware off disk.
2150 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002151 for (index = api_max; index >= api_min; index--) {
2152 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2153 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2154 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002155 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002156 buf, ret);
2157 if (ret == -ENOENT)
2158 continue;
2159 else
2160 goto error;
2161 } else {
2162 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002163 IWL_ERR(priv, "Loaded firmware %s, "
2164 "which is deprecated. "
2165 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002166 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002167 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2168 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002169 buf, ucode_raw->size);
2170 break;
2171 }
Zhu Yib481de92007-09-25 17:54:57 -07002172 }
2173
Reinette Chatrea0987a82008-12-02 12:14:06 -08002174 if (ret < 0)
2175 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002176
2177 /* Make sure that we got at least our header! */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002178 if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002179 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002180 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002181 goto err_release;
2182 }
2183
2184 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002185 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002186
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002187 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002188 api_ver = IWL_UCODE_API(priv->ucode_ver);
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002189 inst_size = iwl3945_ucode_get_inst_size(ucode);
2190 data_size = iwl3945_ucode_get_data_size(ucode);
2191 init_size = iwl3945_ucode_get_init_size(ucode);
2192 init_data_size = iwl3945_ucode_get_init_data_size(ucode);
2193 boot_size = iwl3945_ucode_get_boot_size(ucode);
2194 src = iwl3945_ucode_get_data(ucode);
Zhu Yib481de92007-09-25 17:54:57 -07002195
Reinette Chatrea0987a82008-12-02 12:14:06 -08002196 /* api_ver should match the api version forming part of the
2197 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002198 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002199
2200 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002201 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002202 "Driver supports v%u, firmware is v%u.\n",
2203 api_max, api_ver);
2204 priv->ucode_ver = 0;
2205 ret = -EINVAL;
2206 goto err_release;
2207 }
2208 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002209 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002210 "got %u. New firmware can be obtained "
2211 "from http://www.intellinuxwireless.org.\n",
2212 api_max, api_ver);
2213
Tomas Winkler978785a2008-12-19 10:37:31 +08002214 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2215 IWL_UCODE_MAJOR(priv->ucode_ver),
2216 IWL_UCODE_MINOR(priv->ucode_ver),
2217 IWL_UCODE_API(priv->ucode_ver),
2218 IWL_UCODE_SERIAL(priv->ucode_ver));
2219
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07002220 snprintf(priv->hw->wiphy->fw_version,
2221 sizeof(priv->hw->wiphy->fw_version),
2222 "%u.%u.%u.%u",
2223 IWL_UCODE_MAJOR(priv->ucode_ver),
2224 IWL_UCODE_MINOR(priv->ucode_ver),
2225 IWL_UCODE_API(priv->ucode_ver),
2226 IWL_UCODE_SERIAL(priv->ucode_ver));
2227
Tomas Winklere1623442009-01-27 14:27:56 -08002228 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002229 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002230 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2231 inst_size);
2232 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2233 data_size);
2234 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2235 init_size);
2236 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2237 init_data_size);
2238 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2239 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002240
Reinette Chatrea0987a82008-12-02 12:14:06 -08002241
Zhu Yib481de92007-09-25 17:54:57 -07002242 /* Verify size of file vs. image size info in file's header */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002243 if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002244 inst_size + data_size + init_size +
2245 init_data_size + boot_size) {
2246
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002247 IWL_DEBUG_INFO(priv,
2248 "uCode file size %zd does not match expected size\n",
2249 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002250 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002251 goto err_release;
2252 }
2253
2254 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002255 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002256 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002257 inst_size);
2258 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002259 goto err_release;
2260 }
2261
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002262 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002263 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002264 data_size);
2265 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002266 goto err_release;
2267 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002268 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002269 IWL_DEBUG_INFO(priv,
2270 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002271 init_size);
2272 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002273 goto err_release;
2274 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002275 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002276 IWL_DEBUG_INFO(priv,
2277 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002278 init_data_size);
2279 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002280 goto err_release;
2281 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002282 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002283 IWL_DEBUG_INFO(priv,
2284 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002285 boot_size);
2286 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002287 goto err_release;
2288 }
2289
2290 /* Allocate ucode buffers for card's bus-master loading ... */
2291
2292 /* Runtime instructions and 2 copies of data:
2293 * 1) unmodified from disk
2294 * 2) backup cache for save/restore during power-downs */
2295 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002296 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002297
2298 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002299 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002300
2301 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002302 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002303
2304 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002305 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002306 goto err_pci_alloc;
2307
Tomas Winkler90e759d2007-11-29 11:09:41 +08002308 /* Initialization instructions and data */
2309 if (init_size && init_data_size) {
2310 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002311 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002312
2313 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002314 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002315
2316 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2317 goto err_pci_alloc;
2318 }
2319
2320 /* Bootstrap (instructions only, no data) */
2321 if (boot_size) {
2322 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002323 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002324
2325 if (!priv->ucode_boot.v_addr)
2326 goto err_pci_alloc;
2327 }
2328
Zhu Yib481de92007-09-25 17:54:57 -07002329 /* Copy images into buffers for card's bus-master reads ... */
2330
2331 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002332 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002333 IWL_DEBUG_INFO(priv,
2334 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002335 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002336 src += len;
2337
Tomas Winklere1623442009-01-27 14:27:56 -08002338 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002339 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2340
2341 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002342 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002343 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002344 IWL_DEBUG_INFO(priv,
2345 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002346 memcpy(priv->ucode_data.v_addr, src, len);
2347 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002348 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002349
2350 /* Initialization instructions (3rd block) */
2351 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002352 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002353 IWL_DEBUG_INFO(priv,
2354 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002355 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002356 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002357 }
2358
2359 /* Initialization data (4th block) */
2360 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002361 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002362 IWL_DEBUG_INFO(priv,
2363 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002364 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002365 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002366 }
2367
2368 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002369 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002370 IWL_DEBUG_INFO(priv,
2371 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002372 memcpy(priv->ucode_boot.v_addr, src, len);
2373
2374 /* We have our copies now, allow OS release its copies */
2375 release_firmware(ucode_raw);
2376 return 0;
2377
2378 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002379 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002380 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002381 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002382
2383 err_release:
2384 release_firmware(ucode_raw);
2385
2386 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002387 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002388}
2389
2390
2391/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002392 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002393 *
2394 * Tell initialization uCode where to find runtime uCode.
2395 *
2396 * BSM registers initially contain pointers to initialization uCode.
2397 * We need to replace them to load runtime uCode inst and data,
2398 * and to save runtime data when powering down.
2399 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002400static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002401{
2402 dma_addr_t pinst;
2403 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002404
2405 /* bits 31:0 for 3945 */
2406 pinst = priv->ucode_code.p_addr;
2407 pdata = priv->ucode_data_backup.p_addr;
2408
Zhu Yib481de92007-09-25 17:54:57 -07002409 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002410 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2411 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2412 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002413 priv->ucode_data.len);
2414
Tomas Winklera96a27f2008-10-23 23:48:56 -07002415 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002416 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002417 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002418 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2419
Tomas Winklere1623442009-01-27 14:27:56 -08002420 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002421
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002422 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002423}
2424
2425/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002426 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002427 *
2428 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2429 *
Zhu Yib481de92007-09-25 17:54:57 -07002430 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002431 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002432static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002433{
2434 /* Check alive response for "valid" sign from uCode */
2435 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2436 /* We had an error bringing up the hardware, so take it
2437 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002438 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002439 goto restart;
2440 }
2441
2442 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2443 * This is a paranoid check, because we would not have gotten the
2444 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002445 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002446 /* Runtime instruction load was bad;
2447 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002448 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002449 goto restart;
2450 }
2451
2452 /* Send pointers to protocol/runtime uCode image ... init code will
2453 * load and launch runtime uCode, which will send us another "Alive"
2454 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002455 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002456 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002457 /* Runtime instruction load won't happen;
2458 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002459 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002460 goto restart;
2461 }
2462 return;
2463
2464 restart:
2465 queue_work(priv->workqueue, &priv->restart);
2466}
2467
Zhu Yib481de92007-09-25 17:54:57 -07002468/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002469 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002470 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002471 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002472 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002473static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002474{
Zhu Yib481de92007-09-25 17:54:57 -07002475 int thermal_spin = 0;
2476 u32 rfkill;
Johannes Berg246ed352010-08-23 10:46:32 +02002477 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07002478
Tomas Winklere1623442009-01-27 14:27:56 -08002479 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002480
2481 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2482 /* We had an error bringing up the hardware, so take it
2483 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002484 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002485 goto restart;
2486 }
2487
2488 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2489 * This is a paranoid check, because we would not have gotten the
2490 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002491 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002492 /* Runtime instruction load was bad;
2493 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002494 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002495 goto restart;
2496 }
2497
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002498 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002499 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002500
2501 if (rfkill & 0x1) {
2502 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002503 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002504 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002505 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002506 thermal_spin++;
2507 udelay(10);
2508 }
2509
2510 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002511 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002512 thermal_spin * 10);
2513 } else
2514 set_bit(STATUS_RF_KILL_HW, &priv->status);
2515
Ben Cahill9fbab512007-11-29 11:09:47 +08002516 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002517 set_bit(STATUS_ALIVE, &priv->status);
2518
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08002519 if (priv->cfg->ops->lib->recover_from_tx_stall) {
2520 /* Enable timer to monitor the driver queues */
2521 mod_timer(&priv->monitor_recover,
2522 jiffies +
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07002523 msecs_to_jiffies(
2524 priv->cfg->base_params->monitor_recover_period));
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08002525 }
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
Johannes Berg246ed352010-08-23 10:46:32 +02002536 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002537 struct iwl3945_rxon_cmd *active_rxon =
Johannes Berg246ed352010-08-23 10:46:32 +02002538 (struct iwl3945_rxon_cmd *)(&ctx->active);
Zhu Yib481de92007-09-25 17:54:57 -07002539
Johannes Berg246ed352010-08-23 10:46:32 +02002540 ctx->staging.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 Bergd0fe4782010-08-23 10:46:58 +02002544 iwl_connection_init_rx_config(priv, ctx);
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 */
Johannes Berg8289e072010-09-22 18:01:57 +02002551 iwl3945_commit_rxon(priv, ctx);
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 Yi5a669262008-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;
Stanislaw Gruszkad745d472010-09-13 14:46:35 +02002572 int exit_pending;
Zhu Yib481de92007-09-25 17:54:57 -07002573
Tomas Winklere1623442009-01-27 14:27:56 -08002574 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002575
Stanislaw Gruszkad745d472010-09-13 14:46:35 +02002576 iwl_scan_cancel_timeout(priv, 200);
2577
2578 exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002579
Stanislaw Gruszkab62177a2010-08-20 15:23:47 +02002580 /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
2581 * to prevent rearm timer */
2582 if (priv->cfg->ops->lib->recover_from_tx_stall)
2583 del_timer_sync(&priv->monitor_recover);
2584
Reinette Chatre7e246192010-02-18 22:58:32 -08002585 /* Station information will now be cleared in device */
Johannes Bergdcef7322010-08-27 08:55:52 -07002586 iwl_clear_ucode_stations(priv, NULL);
Johannes Berga194e322010-08-27 08:53:46 -07002587 iwl_dealloc_bcast_stations(priv);
Johannes Bergdb125c72010-05-07 01:49:15 -07002588 iwl_clear_driver_stations(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002589
2590 /* Unblock any waiting calls */
2591 wake_up_interruptible_all(&priv->wait_command_queue);
2592
Zhu Yib481de92007-09-25 17:54:57 -07002593 /* Wipe out the EXIT_PENDING status bit if we are not actually
2594 * exiting the module */
2595 if (!exit_pending)
2596 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2597
2598 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002599 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002600
2601 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002602 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002603 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002604 spin_unlock_irqrestore(&priv->lock, flags);
2605 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002606
2607 if (priv->mac80211_registered)
2608 ieee80211_stop_queues(priv->hw);
2609
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002610 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002611 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002612 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002613 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2614 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002615 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2616 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002617 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2618 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002619 goto exit;
2620 }
2621
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002622 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002623 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002624 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2625 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002626 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2627 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002628 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002629 STATUS_FW_ERROR |
2630 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2631 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002633 iwl3945_hw_txq_ctx_stop(priv);
2634 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002635
Ben Cahill309e7312009-11-06 14:53:03 -08002636 /* Power-down device's busmaster DMA clocks */
2637 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002638 udelay(5);
2639
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002640 /* Stop the device, and put it in low power state */
Johannes Berg14e8e4a2010-09-22 18:02:10 +02002641 iwl_apm_stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002642
Zhu Yib481de92007-09-25 17:54:57 -07002643 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002644 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002645
Johannes Berg12e934d2010-10-04 05:50:06 -07002646 if (priv->beacon_skb)
2647 dev_kfree_skb(priv->beacon_skb);
2648 priv->beacon_skb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002649
2650 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002651 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002652}
2653
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002654static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002655{
2656 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002657 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002658 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002659
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002660 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002661}
2662
2663#define MAX_HW_RESTARTS 5
2664
Johannes Berga30e3112010-09-22 18:02:01 +02002665static int iwl3945_alloc_bcast_station(struct iwl_priv *priv)
2666{
2667 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2668 unsigned long flags;
2669 u8 sta_id;
2670
2671 spin_lock_irqsave(&priv->sta_lock, flags);
2672 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
2673 if (sta_id == IWL_INVALID_STATION) {
2674 IWL_ERR(priv, "Unable to prepare broadcast station\n");
2675 spin_unlock_irqrestore(&priv->sta_lock, flags);
2676
2677 return -EINVAL;
2678 }
2679
2680 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
2681 priv->stations[sta_id].used |= IWL_STA_BCAST;
2682 spin_unlock_irqrestore(&priv->sta_lock, flags);
2683
2684 return 0;
2685}
2686
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002687static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002688{
2689 int rc, i;
2690
Johannes Berga30e3112010-09-22 18:02:01 +02002691 rc = iwl3945_alloc_bcast_station(priv);
Johannes Berg2c810cc2010-04-29 00:53:29 -07002692 if (rc)
2693 return rc;
2694
Zhu Yib481de92007-09-25 17:54:57 -07002695 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002696 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002697 return -EIO;
2698 }
2699
Reinette Chatree903fbd2008-01-30 22:05:15 -08002700 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002701 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002702 return -EIO;
2703 }
2704
Zhu Yie655b9f2008-01-24 02:19:38 -08002705 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002706 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002707 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2708 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2709 else {
2710 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002711 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2712 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002713 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002714
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002715 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002716
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002717 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002718 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002719 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002720 return rc;
2721 }
2722
2723 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002724 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2725 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002726 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2727
2728 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002729 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002730 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002731
2732 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002733 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2734 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002735
2736 /* Copy original ucode data image from disk into backup cache.
2737 * This will be used to initialize the on-board processor's
2738 * data SRAM for a clean start when the runtime program first loads. */
2739 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002740 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002741
Zhu Yie655b9f2008-01-24 02:19:38 -08002742 /* We return success when we resume from suspend and rf_kill is on. */
2743 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2744 return 0;
2745
Zhu Yib481de92007-09-25 17:54:57 -07002746 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2747
Zhu Yib481de92007-09-25 17:54:57 -07002748 /* load bootstrap state machine,
2749 * load bootstrap program into processor's memory,
2750 * prepare to load the "initialize" uCode */
Abhijeet Kolekar75a9a922010-02-26 15:17:01 -08002751 rc = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002752
2753 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002754 IWL_ERR(priv,
2755 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002756 continue;
2757 }
2758
2759 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002760 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002761
Tomas Winklere1623442009-01-27 14:27:56 -08002762 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002763
2764 return 0;
2765 }
2766
2767 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002768 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002769 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002770
2771 /* tried to restart and config the device for as long as our
2772 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002773 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002774 return -EIO;
2775}
2776
2777
2778/*****************************************************************************
2779 *
2780 * Workqueue callbacks
2781 *
2782 *****************************************************************************/
2783
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002784static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002785{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002786 struct iwl_priv *priv =
2787 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002788
2789 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2790 return;
2791
2792 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002793 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002794 mutex_unlock(&priv->mutex);
2795}
2796
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002797static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002798{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002799 struct iwl_priv *priv =
2800 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002801
2802 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2803 return;
2804
2805 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002806 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002807 mutex_unlock(&priv->mutex);
2808}
2809
Ben Cahill743cdf12009-10-09 13:20:26 -07002810/*
2811 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2812 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2813 * *is* readable even when device has been SW_RESET into low power mode
2814 * (e.g. during RF KILL).
2815 */
Helmut Schaa26635162009-01-15 09:38:44 +01002816static void iwl3945_rfkill_poll(struct work_struct *data)
2817{
2818 struct iwl_priv *priv =
Johannes Bergee525d12010-01-21 06:09:28 -08002819 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002820 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2821 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2822 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002823
Ben Cahill743cdf12009-10-09 13:20:26 -07002824 if (new_rfkill != old_rfkill) {
2825 if (new_rfkill)
2826 set_bit(STATUS_RF_KILL_HW, &priv->status);
2827 else
2828 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002829
Ben Cahill743cdf12009-10-09 13:20:26 -07002830 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002831
Ben Cahill743cdf12009-10-09 13:20:26 -07002832 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2833 new_rfkill ? "disable radio" : "enable radio");
2834 }
2835
2836 /* Keep this running, even if radio now enabled. This will be
2837 * cancelled in mac_start() if system decides to start again */
Johannes Bergee525d12010-01-21 06:09:28 -08002838 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01002839 round_jiffies_relative(2 * HZ));
2840
2841}
2842
Johannes Berg3eecce52010-09-13 14:46:33 +02002843int iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07002844{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002845 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002846 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002847 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002848 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002849 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002850 struct iwl3945_scan_cmd *scan;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002851 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002852 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002853 bool is_active = false;
Johannes Berg3eecce52010-09-13 14:46:33 +02002854 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002855
Johannes Berg3eecce52010-09-13 14:46:33 +02002856 lockdep_assert_held(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07002857
Johannes Berg811ecc92010-04-06 04:12:41 -07002858 if (!priv->scan_cmd) {
2859 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2860 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2861 if (!priv->scan_cmd) {
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002862 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02002863 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07002864 }
2865 }
Johannes Berg811ecc92010-04-06 04:12:41 -07002866 scan = priv->scan_cmd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002867 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002868
2869 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2870 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2871
Johannes Berg246ed352010-08-23 10:46:32 +02002872 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Zhu Yib481de92007-09-25 17:54:57 -07002873 u16 interval = 0;
2874 u32 extra;
2875 u32 suspend_time = 100;
2876 u32 scan_suspend_time = 100;
2877 unsigned long flags;
2878
Tomas Winklere1623442009-01-27 14:27:56 -08002879 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002880
2881 spin_lock_irqsave(&priv->lock, flags);
John W. Linvillea6e492b2010-07-22 15:24:56 -04002882 if (priv->is_internal_short_scan)
2883 interval = 0;
2884 else
2885 interval = vif->bss_conf.beacon_int;
Zhu Yib481de92007-09-25 17:54:57 -07002886 spin_unlock_irqrestore(&priv->lock, flags);
2887
2888 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002889 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002890 if (!interval)
2891 interval = suspend_time;
2892 /*
2893 * suspend time format:
2894 * 0-19: beacon interval in usec (time before exec.)
2895 * 20-23: 0
2896 * 24-31: number of beacons (suspend between channels)
2897 */
2898
2899 extra = (suspend_time / interval) << 24;
2900 scan_suspend_time = 0xFF0FFFFF &
2901 (extra | ((suspend_time % interval) * 1024));
2902
2903 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002904 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002905 scan_suspend_time, interval);
2906 }
2907
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002908 if (priv->is_internal_short_scan) {
2909 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2910 } else if (priv->scan_request->n_ssids) {
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002911 int i, p = 0;
2912 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2913 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2914 /* always does wildcard anyway */
2915 if (!priv->scan_request->ssids[i].ssid_len)
2916 continue;
2917 scan->direct_scan[p].id = WLAN_EID_SSID;
2918 scan->direct_scan[p].len =
2919 priv->scan_request->ssids[i].ssid_len;
2920 memcpy(scan->direct_scan[p].ssid,
2921 priv->scan_request->ssids[i].ssid,
2922 priv->scan_request->ssids[i].ssid_len);
2923 n_probes++;
2924 p++;
2925 }
2926 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002927 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002928 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002929
2930 /* We don't build a direct scan probe request; the uCode will do
2931 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002932 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Johannes Berga194e322010-08-27 08:53:46 -07002933 scan->tx_cmd.sta_id = priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002934 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2935
2936 /* flags + rate selection */
2937
Johannes Berg00700ee2010-04-06 04:12:37 -07002938 switch (priv->scan_band) {
2939 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002940 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2941 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
Johannes Berg8318d782008-01-24 19:38:38 +01002942 band = IEEE80211_BAND_2GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002943 break;
2944 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002945 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Berg8318d782008-01-24 19:38:38 +01002946 band = IEEE80211_BAND_5GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002947 break;
2948 default:
2949 IWL_WARN(priv, "Invalid scan band\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02002950 return -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07002951 }
2952
Johannes Berg085fbca2010-10-04 05:47:23 -07002953 /*
2954 * If active scaning is requested but a certain channel
2955 * is marked passive, we can do active scanning if we
2956 * detect transmissions.
2957 */
2958 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2959 IWL_GOOD_CRC_TH_DISABLED;
2960
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002961 if (!priv->is_internal_short_scan) {
2962 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002963 iwl_fill_probe_req(priv,
2964 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002965 vif->addr,
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002966 priv->scan_request->ie,
2967 priv->scan_request->ie_len,
2968 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002969 } else {
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002970 /* use bcast addr, will not be transmitted but must be valid */
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002971 scan->tx_cmd.len = cpu_to_le16(
2972 iwl_fill_probe_req(priv,
2973 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002974 iwl_bcast_addr, NULL, 0,
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002975 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2976 }
Zhu Yib481de92007-09-25 17:54:57 -07002977 /* select Rx antennas */
2978 scan->flags |= iwl3945_get_antenna_flags(priv);
2979
Abhijeet Kolekar14023642010-06-02 21:15:10 -07002980 if (priv->is_internal_short_scan) {
2981 scan->channel_count =
2982 iwl3945_get_single_channel_for_scan(priv, vif, band,
2983 (void *)&scan->data[le16_to_cpu(
2984 scan->tx_cmd.len)]);
2985 } else {
2986 scan->channel_count =
2987 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
2988 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
2989 }
Zhu Yib481de92007-09-25 17:54:57 -07002990
Reinette Chatre14b54332008-11-04 12:21:35 -08002991 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002992 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Johannes Berg3eecce52010-09-13 14:46:33 +02002993 return -EIO;
Reinette Chatre14b54332008-11-04 12:21:35 -08002994 }
2995
Zhu Yib481de92007-09-25 17:54:57 -07002996 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002997 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07002998 cmd.data = scan;
2999 scan->len = cpu_to_le16(cmd.len);
3000
3001 set_bit(STATUS_SCAN_HW, &priv->status);
Johannes Berg3eecce52010-09-13 14:46:33 +02003002 ret = iwl_send_cmd_sync(priv, &cmd);
3003 if (ret)
3004 clear_bit(STATUS_SCAN_HW, &priv->status);
3005 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003006}
3007
Johannes Berga77029e2010-09-22 18:01:56 +02003008void iwl3945_post_scan(struct iwl_priv *priv)
3009{
3010 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
3011
3012 /*
3013 * Since setting the RXON may have been deferred while
3014 * performing the scan, fire one off if needed
3015 */
3016 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
Johannes Berg8289e072010-09-22 18:01:57 +02003017 iwl3945_commit_rxon(priv, ctx);
Johannes Berga77029e2010-09-22 18:01:56 +02003018}
3019
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003020static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003021{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003022 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003023
3024 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3025 return;
3026
Johannes Berg19cc1082009-05-08 13:44:36 -07003027 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
Johannes Berg8bd413e2010-08-23 10:46:40 +02003028 struct iwl_rxon_context *ctx;
Johannes Berg19cc1082009-05-08 13:44:36 -07003029 mutex_lock(&priv->mutex);
Johannes Berg8bd413e2010-08-23 10:46:40 +02003030 for_each_context(priv, ctx)
3031 ctx->vif = NULL;
Johannes Berg19cc1082009-05-08 13:44:36 -07003032 priv->is_open = 0;
3033 mutex_unlock(&priv->mutex);
3034 iwl3945_down(priv);
3035 ieee80211_restart_hw(priv->hw);
3036 } else {
3037 iwl3945_down(priv);
Johannes Berg80676512010-01-21 06:07:17 -08003038
3039 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3040 return;
3041
3042 mutex_lock(&priv->mutex);
3043 __iwl3945_up(priv);
3044 mutex_unlock(&priv->mutex);
Johannes Berg19cc1082009-05-08 13:44:36 -07003045 }
Zhu Yib481de92007-09-25 17:54:57 -07003046}
3047
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003048static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003049{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003050 struct iwl_priv *priv =
3051 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003052
3053 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3054 return;
3055
3056 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003057 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003058 mutex_unlock(&priv->mutex);
3059}
3060
Johannes Berg2295c662010-10-23 09:15:41 -07003061void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003062{
Zhu Yib481de92007-09-25 17:54:57 -07003063 int rc = 0;
3064 struct ieee80211_conf *conf = NULL;
Johannes Berg246ed352010-08-23 10:46:32 +02003065 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003066
Johannes Berg2295c662010-10-23 09:15:41 -07003067 if (!ctx->vif || !priv->is_open)
Johannes Berg1dda6d22010-04-29 04:43:06 -07003068 return;
3069
Johannes Berg2295c662010-10-23 09:15:41 -07003070 if (ctx->vif->type == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003071 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003072 return;
3073 }
3074
Tomas Winklere1623442009-01-27 14:27:56 -08003075 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003076 ctx->vif->bss_conf.aid, ctx->active.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003077
3078 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3079 return;
3080
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003081 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003082
Zhu Yib481de92007-09-25 17:54:57 -07003083 conf = ieee80211_get_hw_conf(priv->hw);
3084
Johannes Berg246ed352010-08-23 10:46:32 +02003085 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003086 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003087
Johannes Berg47313e32010-08-23 10:46:55 +02003088 rc = iwl_send_rxon_timing(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003089 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003090 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003091 "Attempting to continue.\n");
3092
Johannes Berg246ed352010-08-23 10:46:32 +02003093 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003094
Johannes Berg2295c662010-10-23 09:15:41 -07003095 ctx->staging.assoc_id = cpu_to_le16(ctx->vif->bss_conf.aid);
Zhu Yib481de92007-09-25 17:54:57 -07003096
Tomas Winklere1623442009-01-27 14:27:56 -08003097 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003098 ctx->vif->bss_conf.aid, ctx->vif->bss_conf.beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07003099
Johannes Berg2295c662010-10-23 09:15:41 -07003100 if (ctx->vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003101 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003102 else
Johannes Berg246ed352010-08-23 10:46:32 +02003103 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003104
Johannes Berg246ed352010-08-23 10:46:32 +02003105 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Berg2295c662010-10-23 09:15:41 -07003106 if (ctx->vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003107 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003108 else
Johannes Berg246ed352010-08-23 10:46:32 +02003109 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003110 }
3111
Johannes Berg8289e072010-09-22 18:01:57 +02003112 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003113
Johannes Berg2295c662010-10-23 09:15:41 -07003114 switch (ctx->vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003115 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003116 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003117 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02003118 case NL80211_IFTYPE_ADHOC:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003119 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003120 break;
Zhu Yib481de92007-09-25 17:54:57 -07003121 default:
Johannes Berg1dda6d22010-04-29 04:43:06 -07003122 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003123 __func__, ctx->vif->type);
Zhu Yib481de92007-09-25 17:54:57 -07003124 break;
3125 }
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003126}
3127
Zhu Yib481de92007-09-25 17:54:57 -07003128/*****************************************************************************
3129 *
3130 * mac80211 entry point functions
3131 *
3132 *****************************************************************************/
3133
Zhu Yi5a669262008-01-14 17:46:18 -08003134#define UCODE_READY_TIMEOUT (2 * HZ)
3135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003136static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003137{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003138 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08003139 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003140
Tomas Winklere1623442009-01-27 14:27:56 -08003141 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003142
3143 /* we should be verifying the device is ready to be opened */
3144 mutex_lock(&priv->mutex);
3145
Zhu Yi5a669262008-01-14 17:46:18 -08003146 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3147 * ucode filename and max sizes are card-specific. */
3148
3149 if (!priv->ucode_code.len) {
3150 ret = iwl3945_read_ucode(priv);
3151 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003152 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08003153 mutex_unlock(&priv->mutex);
3154 goto out_release_irq;
3155 }
3156 }
3157
Zhu Yie655b9f2008-01-24 02:19:38 -08003158 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08003159
Zhu Yib481de92007-09-25 17:54:57 -07003160 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08003161
Zhu Yie655b9f2008-01-24 02:19:38 -08003162 if (ret)
3163 goto out_release_irq;
3164
Tomas Winklere1623442009-01-27 14:27:56 -08003165 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003166
Zhu Yi5a669262008-01-14 17:46:18 -08003167 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3168 * mac80211 will not be run successfully. */
3169 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3170 test_bit(STATUS_READY, &priv->status),
3171 UCODE_READY_TIMEOUT);
3172 if (!ret) {
3173 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003174 IWL_ERR(priv,
3175 "Wait for START_ALIVE timeout after %dms.\n",
3176 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08003177 ret = -ETIMEDOUT;
3178 goto out_release_irq;
3179 }
3180 }
3181
Helmut Schaa26635162009-01-15 09:38:44 +01003182 /* ucode is running and will send rfkill notifications,
3183 * no need to poll the killswitch state anymore */
Johannes Bergee525d12010-01-21 06:09:28 -08003184 cancel_delayed_work(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01003185
Johannes Berge932a602009-10-02 13:44:03 -07003186 iwl_led_start(priv);
3187
Zhu Yie655b9f2008-01-24 02:19:38 -08003188 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003189 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003190 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003191
3192out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003193 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003194 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08003195 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003196}
3197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003198static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003199{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003200 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003201
Tomas Winklere1623442009-01-27 14:27:56 -08003202 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003203
Zhu Yie655b9f2008-01-24 02:19:38 -08003204 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003205 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003206 return;
3207 }
3208
Zhu Yib481de92007-09-25 17:54:57 -07003209 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003210
Zhu Yi5a669262008-01-14 17:46:18 -08003211 iwl3945_down(priv);
3212
3213 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003214
3215 /* start polling the killswitch state again */
Johannes Bergee525d12010-01-21 06:09:28 -08003216 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01003217 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003218
Tomas Winklere1623442009-01-27 14:27:56 -08003219 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003220}
3221
Johannes Berge039fa42008-05-15 12:55:29 +02003222static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003223{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003224 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003225
Tomas Winklere1623442009-01-27 14:27:56 -08003226 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003227
Tomas Winklere1623442009-01-27 14:27:56 -08003228 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003229 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003230
Johannes Berge039fa42008-05-15 12:55:29 +02003231 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003232 dev_kfree_skb_any(skb);
3233
Tomas Winklere1623442009-01-27 14:27:56 -08003234 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003235 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003236}
3237
Johannes Berg2295c662010-10-23 09:15:41 -07003238void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003239{
Johannes Berg246ed352010-08-23 10:46:32 +02003240 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg2295c662010-10-23 09:15:41 -07003241 struct ieee80211_vif *vif = ctx->vif;
Zhu Yib481de92007-09-25 17:54:57 -07003242 int rc = 0;
3243
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003244 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003245 return;
3246
3247 /* The following should be done only at AP bring up */
Johannes Berg246ed352010-08-23 10:46:32 +02003248 if (!(iwl_is_associated(priv, IWL_RXON_CTX_BSS))) {
Zhu Yib481de92007-09-25 17:54:57 -07003249
3250 /* RXON - unassoc (to set timing command) */
Johannes Berg246ed352010-08-23 10:46:32 +02003251 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003252 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003253
3254 /* RXON Timing */
Johannes Berg47313e32010-08-23 10:46:55 +02003255 rc = iwl_send_rxon_timing(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003256 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003257 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003258 "Attempting to continue.\n");
3259
Johannes Berg246ed352010-08-23 10:46:32 +02003260 ctx->staging.assoc_id = 0;
Johannes Berg1dda6d22010-04-29 04:43:06 -07003261
Johannes Bergc213d742010-05-06 12:21:40 -07003262 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003263 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003264 RXON_FLG_SHORT_PREAMBLE_MSK;
3265 else
Johannes Berg246ed352010-08-23 10:46:32 +02003266 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003267 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3268
Johannes Berg246ed352010-08-23 10:46:32 +02003269 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003270 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003271 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003272 RXON_FLG_SHORT_SLOT_MSK;
3273 else
Johannes Berg246ed352010-08-23 10:46:32 +02003274 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003275 ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003276 }
3277 /* restore RXON assoc */
Johannes Berg246ed352010-08-23 10:46:32 +02003278 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003279 iwl3945_commit_rxon(priv, ctx);
Zhu Yi556f8db2007-09-27 11:27:33 +08003280 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003281 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003282
3283 /* FIXME - we need to add code here to detect a totally new
3284 * configuration, reset the AP, unassoc, rxon timing, assoc,
3285 * clear sta table, add BCAST sta... */
3286}
3287
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003288static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003289 struct ieee80211_vif *vif,
3290 struct ieee80211_sta *sta,
3291 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003292{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003293 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003294 int ret = 0;
3295 u8 sta_id = IWL_INVALID_STATION;
3296 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003297
Tomas Winklere1623442009-01-27 14:27:56 -08003298 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003299
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003300 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003301 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003302 return -EOPNOTSUPP;
3303 }
3304
Johannes Berg246ed352010-08-23 10:46:32 +02003305 static_key = !iwl_is_associated(priv, IWL_RXON_CTX_BSS);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003306
3307 if (!static_key) {
Johannes Berga194e322010-08-27 08:53:46 -07003308 sta_id = iwl_sta_id_or_broadcast(
3309 priv, &priv->contexts[IWL_RXON_CTX_BSS], sta);
Johannes Berg0af8bca2010-04-30 14:08:00 -07003310 if (sta_id == IWL_INVALID_STATION)
3311 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003312 }
3313
3314 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003315 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003316
Zhu Yib481de92007-09-25 17:54:57 -07003317 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003318 case SET_KEY:
3319 if (static_key)
3320 ret = iwl3945_set_static_key(priv, key);
3321 else
3322 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3323 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003324 break;
3325 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003326 if (static_key)
3327 ret = iwl3945_remove_static_key(priv);
3328 else
3329 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3330 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003331 break;
3332 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003333 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003334 }
3335
Johannes Berg72e15d72010-02-19 11:42:32 -08003336 mutex_unlock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003337 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003338
Winkler, Tomas42986792009-01-19 15:30:22 -08003339 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003340}
3341
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003342static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3343 struct ieee80211_vif *vif,
3344 struct ieee80211_sta *sta)
3345{
3346 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07003347 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003348 int ret;
Johannes Bergfd1af152010-04-30 11:30:43 -07003349 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003350 u8 sta_id;
3351
3352 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3353 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003354 mutex_lock(&priv->mutex);
3355 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
3356 sta->addr);
3357 sta_priv->common.sta_id = IWL_INVALID_STATION;
3358
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003359
Johannes Berga194e322010-08-27 08:53:46 -07003360 ret = iwl_add_station_common(priv, &priv->contexts[IWL_RXON_CTX_BSS],
Johannes Berg238d7812010-08-23 10:46:45 +02003361 sta->addr, is_ap, sta, &sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003362 if (ret) {
3363 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3364 sta->addr, ret);
3365 /* Should we return success if return code is EEXIST ? */
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003366 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003367 return ret;
3368 }
3369
Johannes Bergfd1af152010-04-30 11:30:43 -07003370 sta_priv->common.sta_id = sta_id;
3371
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003372 /* Initialize rate scaling */
Frans Pop91dd6c22010-03-24 14:19:58 -07003373 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003374 sta->addr);
3375 iwl3945_rs_rate_init(priv, sta, sta_id);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003376 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003377
3378 return 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003379}
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003380
3381static void iwl3945_configure_filter(struct ieee80211_hw *hw,
3382 unsigned int changed_flags,
3383 unsigned int *total_flags,
3384 u64 multicast)
3385{
3386 struct iwl_priv *priv = hw->priv;
3387 __le32 filter_or = 0, filter_nand = 0;
Johannes Berg246ed352010-08-23 10:46:32 +02003388 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003389
3390#define CHK(test, flag) do { \
3391 if (*total_flags & (test)) \
3392 filter_or |= (flag); \
3393 else \
3394 filter_nand |= (flag); \
3395 } while (0)
3396
3397 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
3398 changed_flags, *total_flags);
3399
3400 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
3401 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
3402 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
3403
3404#undef CHK
3405
3406 mutex_lock(&priv->mutex);
3407
Johannes Berg246ed352010-08-23 10:46:32 +02003408 ctx->staging.filter_flags &= ~filter_nand;
3409 ctx->staging.filter_flags |= filter_or;
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003410
3411 /*
Stanislaw Gruszka749ff4e2010-10-22 17:04:27 +02003412 * Not committing directly because hardware can perform a scan,
3413 * but even if hw is ready, committing here breaks for some reason,
3414 * we'll eventually commit the filter flags change anyway.
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003415 */
3416
3417 mutex_unlock(&priv->mutex);
3418
3419 /*
3420 * Receiving all multicast frames is always enabled by the
3421 * default flags setup in iwl_connection_init_rx_config()
3422 * since we currently do not support programming multicast
3423 * filters into the device.
3424 */
3425 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
3426 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
3427}
3428
3429
Zhu Yib481de92007-09-25 17:54:57 -07003430/*****************************************************************************
3431 *
3432 * sysfs attributes
3433 *
3434 *****************************************************************************/
3435
Samuel Ortizd08853a2009-01-23 13:45:17 -08003436#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003437
3438/*
3439 * The following adds a new attribute to the sysfs representation
3440 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3441 * used for controlling the debug level.
3442 *
3443 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003444 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003445 * The debug_level being managed using sysfs below is a per device debug
3446 * level that is used instead of the global debug level if it (the per
3447 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003448 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003449static ssize_t show_debug_level(struct device *d,
3450 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003451{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003452 struct iwl_priv *priv = dev_get_drvdata(d);
3453 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003454}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003455static ssize_t store_debug_level(struct device *d,
3456 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003457 const char *buf, size_t count)
3458{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003459 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003460 unsigned long val;
3461 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003462
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003463 ret = strict_strtoul(buf, 0, &val);
3464 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003465 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003466 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003467 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003468 if (iwl_alloc_traffic_mem(priv))
3469 IWL_ERR(priv,
3470 "Not enough memory to generate traffic log\n");
3471 }
Zhu Yib481de92007-09-25 17:54:57 -07003472 return strnlen(buf, count);
3473}
3474
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003475static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3476 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003477
Samuel Ortizd08853a2009-01-23 13:45:17 -08003478#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003479
Zhu Yib481de92007-09-25 17:54:57 -07003480static ssize_t show_temperature(struct device *d,
3481 struct device_attribute *attr, char *buf)
3482{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003483 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003484
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003485 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003486 return -EAGAIN;
3487
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003488 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003489}
3490
3491static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3492
Zhu Yib481de92007-09-25 17:54:57 -07003493static ssize_t show_tx_power(struct device *d,
3494 struct device_attribute *attr, char *buf)
3495{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003496 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003497 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003498}
3499
3500static ssize_t store_tx_power(struct device *d,
3501 struct device_attribute *attr,
3502 const char *buf, size_t count)
3503{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003504 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003505 char *p = (char *)buf;
3506 u32 val;
3507
3508 val = simple_strtoul(p, &p, 10);
3509 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003510 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003511 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003512 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003513
3514 return count;
3515}
3516
3517static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3518
3519static ssize_t show_flags(struct device *d,
3520 struct device_attribute *attr, char *buf)
3521{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003522 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003523 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003524
Johannes Berg246ed352010-08-23 10:46:32 +02003525 return sprintf(buf, "0x%04X\n", ctx->active.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003526}
3527
3528static ssize_t store_flags(struct device *d,
3529 struct device_attribute *attr,
3530 const char *buf, size_t count)
3531{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003532 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003533 u32 flags = simple_strtoul(buf, NULL, 0);
Johannes Berg246ed352010-08-23 10:46:32 +02003534 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003535
3536 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003537 if (le32_to_cpu(ctx->staging.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003538 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003539 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003540 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003541 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003542 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003543 flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003544 ctx->staging.flags = cpu_to_le32(flags);
Johannes Berg8289e072010-09-22 18:01:57 +02003545 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003546 }
3547 }
3548 mutex_unlock(&priv->mutex);
3549
3550 return count;
3551}
3552
3553static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3554
3555static ssize_t show_filter_flags(struct device *d,
3556 struct device_attribute *attr, char *buf)
3557{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003558 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003559 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003560
3561 return sprintf(buf, "0x%04X\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003562 le32_to_cpu(ctx->active.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003563}
3564
3565static ssize_t store_filter_flags(struct device *d,
3566 struct device_attribute *attr,
3567 const char *buf, size_t count)
3568{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003569 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003570 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003571 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3572
3573 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003574 if (le32_to_cpu(ctx->staging.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003575 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003576 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003577 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003578 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003579 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003580 "0x%04X\n", filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003581 ctx->staging.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003582 cpu_to_le32(filter_flags);
Johannes Berg8289e072010-09-22 18:01:57 +02003583 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003584 }
3585 }
3586 mutex_unlock(&priv->mutex);
3587
3588 return count;
3589}
3590
3591static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3592 store_filter_flags);
3593
Zhu Yib481de92007-09-25 17:54:57 -07003594static ssize_t show_measurement(struct device *d,
3595 struct device_attribute *attr, char *buf)
3596{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003597 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003598 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003599 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003600 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003601 unsigned long flags;
3602
3603 spin_lock_irqsave(&priv->lock, flags);
3604 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3605 spin_unlock_irqrestore(&priv->lock, flags);
3606 return 0;
3607 }
3608 memcpy(&measure_report, &priv->measure_report, size);
3609 priv->measurement_status = 0;
3610 spin_unlock_irqrestore(&priv->lock, flags);
3611
3612 while (size && (PAGE_SIZE - len)) {
3613 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3614 PAGE_SIZE - len, 1);
3615 len = strlen(buf);
3616 if (PAGE_SIZE - len)
3617 buf[len++] = '\n';
3618
3619 ofs += 16;
3620 size -= min(size, 16U);
3621 }
3622
3623 return len;
3624}
3625
3626static ssize_t store_measurement(struct device *d,
3627 struct device_attribute *attr,
3628 const char *buf, size_t count)
3629{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003630 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003631 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003632 struct ieee80211_measurement_params params = {
Johannes Berg246ed352010-08-23 10:46:32 +02003633 .channel = le16_to_cpu(ctx->active.channel),
Johannes Berge99f1682010-01-19 10:04:28 -08003634 .start_time = cpu_to_le64(priv->_3945.last_tsf),
Zhu Yib481de92007-09-25 17:54:57 -07003635 .duration = cpu_to_le16(1),
3636 };
3637 u8 type = IWL_MEASURE_BASIC;
3638 u8 buffer[32];
3639 u8 channel;
3640
3641 if (count) {
3642 char *p = buffer;
3643 strncpy(buffer, buf, min(sizeof(buffer), count));
3644 channel = simple_strtoul(p, NULL, 0);
3645 if (channel)
3646 params.channel = channel;
3647
3648 p = buffer;
3649 while (*p && *p != ' ')
3650 p++;
3651 if (*p)
3652 type = simple_strtoul(p + 1, NULL, 0);
3653 }
3654
Tomas Winklere1623442009-01-27 14:27:56 -08003655 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003656 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003657 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003658
3659 return count;
3660}
3661
3662static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3663 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003664
Zhu Yib481de92007-09-25 17:54:57 -07003665static ssize_t store_retry_rate(struct device *d,
3666 struct device_attribute *attr,
3667 const char *buf, size_t count)
3668{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003669 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003670
3671 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3672 if (priv->retry_rate <= 0)
3673 priv->retry_rate = 1;
3674
3675 return count;
3676}
3677
3678static ssize_t show_retry_rate(struct device *d,
3679 struct device_attribute *attr, char *buf)
3680{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003681 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003682 return sprintf(buf, "%d", priv->retry_rate);
3683}
3684
3685static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3686 store_retry_rate);
3687
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003688
Zhu Yib481de92007-09-25 17:54:57 -07003689static ssize_t show_channels(struct device *d,
3690 struct device_attribute *attr, char *buf)
3691{
Johannes Berg8318d782008-01-24 19:38:38 +01003692 /* all this shit doesn't belong into sysfs anyway */
3693 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003694}
3695
3696static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3697
Zhu Yib481de92007-09-25 17:54:57 -07003698static ssize_t show_antenna(struct device *d,
3699 struct device_attribute *attr, char *buf)
3700{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003701 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003702
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003703 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003704 return -EAGAIN;
3705
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003706 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003707}
3708
3709static ssize_t store_antenna(struct device *d,
3710 struct device_attribute *attr,
3711 const char *buf, size_t count)
3712{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003713 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003714 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003715
3716 if (count == 0)
3717 return 0;
3718
3719 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003720 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003721 return count;
3722 }
3723
3724 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003725 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003726 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003727 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003728 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003729
3730
3731 return count;
3732}
3733
3734static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3735
3736static ssize_t show_status(struct device *d,
3737 struct device_attribute *attr, char *buf)
3738{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003739 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003740 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003741 return -EAGAIN;
3742 return sprintf(buf, "0x%08x\n", (int)priv->status);
3743}
3744
3745static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3746
3747static ssize_t dump_error_log(struct device *d,
3748 struct device_attribute *attr,
3749 const char *buf, size_t count)
3750{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003751 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003752 char *p = (char *)buf;
3753
3754 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003755 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003756
3757 return strnlen(buf, count);
3758}
3759
3760static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3761
Zhu Yib481de92007-09-25 17:54:57 -07003762/*****************************************************************************
3763 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003764 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003765 *
3766 *****************************************************************************/
3767
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003768static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003769{
Reinette Chatred21050c2009-02-13 11:51:18 -08003770 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003771
3772 init_waitqueue_head(&priv->wait_command_queue);
3773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003774 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3775 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003776 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003777 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3778 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Johannes Bergee525d12010-01-21 06:09:28 -08003779 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02003780
3781 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003782
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003783 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003784
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003785 if (priv->cfg->ops->lib->recover_from_tx_stall) {
3786 init_timer(&priv->monitor_recover);
3787 priv->monitor_recover.data = (unsigned long)priv;
3788 priv->monitor_recover.function =
3789 priv->cfg->ops->lib->recover_from_tx_stall;
3790 }
3791
Zhu Yib481de92007-09-25 17:54:57 -07003792 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003793 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003794}
3795
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003796static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003797{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003799
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003800 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003801 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003802 cancel_work_sync(&priv->beacon_update);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02003803
3804 iwl_cancel_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003805}
3806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003808 &dev_attr_antenna.attr,
3809 &dev_attr_channels.attr,
3810 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003811 &dev_attr_flags.attr,
3812 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003813 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003814 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003815 &dev_attr_status.attr,
3816 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003817 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003818#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003819 &dev_attr_debug_level.attr,
3820#endif
Zhu Yib481de92007-09-25 17:54:57 -07003821 NULL
3822};
3823
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003824static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003825 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003826 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003827};
3828
Johannes Bergdc21b542010-10-23 09:15:39 -07003829struct ieee80211_ops iwl3945_hw_ops = {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003830 .tx = iwl3945_mac_tx,
3831 .start = iwl3945_mac_start,
3832 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003833 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003834 .remove_interface = iwl_mac_remove_interface,
Johannes Bergd4daaea2010-10-23 09:15:43 -07003835 .change_interface = iwl_mac_change_interface,
Johannes Berg2295c662010-10-23 09:15:41 -07003836 .config = iwl_legacy_mac_config,
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003837 .configure_filter = iwl3945_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003838 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003839 .conf_tx = iwl_mac_conf_tx,
Johannes Berg2295c662010-10-23 09:15:41 -07003840 .reset_tsf = iwl_legacy_mac_reset_tsf,
3841 .bss_info_changed = iwl_legacy_mac_bss_info_changed,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003842 .hw_scan = iwl_mac_hw_scan,
3843 .sta_add = iwl3945_mac_sta_add,
3844 .sta_remove = iwl_mac_sta_remove,
Johannes Berga85d7cc2010-07-31 08:34:10 -07003845 .tx_last_beacon = iwl_mac_tx_last_beacon,
Zhu Yib481de92007-09-25 17:54:57 -07003846};
3847
Winkler, Tomase52119c2008-12-22 11:31:19 +08003848static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003849{
3850 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003851 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003852
3853 priv->retry_rate = 1;
Johannes Berg12e934d2010-10-04 05:50:06 -07003854 priv->beacon_skb = NULL;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003855
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003856 spin_lock_init(&priv->sta_lock);
3857 spin_lock_init(&priv->hcmd_lock);
3858
3859 INIT_LIST_HEAD(&priv->free_frames);
3860
3861 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003862 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003863
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003864 priv->ieee_channels = NULL;
3865 priv->ieee_rates = NULL;
3866 priv->band = IEEE80211_BAND_2GHZ;
3867
3868 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003869 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003870
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003871 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Stanislaw Gruszkaa25a66a2010-10-22 17:04:26 +02003872 priv->tx_power_next = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003873
Samuel Ortize6148912009-01-23 13:45:15 -08003874 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3875 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3876 eeprom->version);
3877 ret = -EINVAL;
3878 goto err;
3879 }
3880 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003881 if (ret) {
3882 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3883 goto err;
3884 }
3885
Samuel Ortize6148912009-01-23 13:45:15 -08003886 /* Set up txpower settings in driver for all channels */
3887 if (iwl3945_txpower_set_from_eeprom(priv)) {
3888 ret = -EIO;
3889 goto err_free_channel_map;
3890 }
3891
Samuel Ortiz534166d2009-01-23 13:45:16 -08003892 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003893 if (ret) {
3894 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3895 goto err_free_channel_map;
3896 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003897 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3898
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003899 return 0;
3900
3901err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003902 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003903err:
3904 return ret;
3905}
3906
Johannes Bergdd7a2502010-04-28 23:33:10 -07003907#define IWL3945_MAX_PROBE_REQUEST 200
3908
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003909static int iwl3945_setup_mac(struct iwl_priv *priv)
3910{
3911 int ret;
3912 struct ieee80211_hw *hw = priv->hw;
3913
3914 hw->rate_control_algorithm = "iwl-3945-rs";
3915 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
Johannes Bergfd1af152010-04-30 11:30:43 -07003916 hw->vif_data_size = sizeof(struct iwl_vif_priv);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003917
3918 /* Tell mac80211 our characteristics */
3919 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003920 IEEE80211_HW_SPECTRUM_MGMT;
3921
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07003922 if (!priv->cfg->base_params->broken_powersave)
Reinette Chatrebc45a672009-12-14 14:12:10 -08003923 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3924 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003925
3926 hw->wiphy->interface_modes =
Johannes Bergd0fe4782010-08-23 10:46:58 +02003927 priv->contexts[IWL_RXON_CTX_BSS].interface_modes;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003928
Reinette Chatref6c8f152010-03-12 11:13:26 -08003929 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
Johannes Berg5be83de2009-11-19 00:56:28 +01003930 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003931
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003932 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3933 /* we create the 802.11 header and a zero-length SSID element */
Johannes Bergdd7a2502010-04-28 23:33:10 -07003934 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003935
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003936 /* Default value; 4 EDCA QOS priorities */
3937 hw->queues = 4;
3938
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003939 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3940 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3941 &priv->bands[IEEE80211_BAND_2GHZ];
3942
3943 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3944 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3945 &priv->bands[IEEE80211_BAND_5GHZ];
3946
3947 ret = ieee80211_register_hw(priv->hw);
3948 if (ret) {
3949 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3950 return ret;
3951 }
3952 priv->mac80211_registered = 1;
3953
3954 return 0;
3955}
3956
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003957static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003958{
Johannes Berg246ed352010-08-23 10:46:32 +02003959 int err = 0, i;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003960 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003961 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003962 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003963 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003964 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003965
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003966 /***********************
3967 * 1. Allocating HW data
3968 * ********************/
3969
Zhu Yib481de92007-09-25 17:54:57 -07003970 /* mac80211 allocates memory for this device instance, including
3971 * space for this driver's private structure */
Johannes Bergdc21b542010-10-23 09:15:39 -07003972 hw = iwl_alloc_all(cfg);
Zhu Yib481de92007-09-25 17:54:57 -07003973 if (hw == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003974 pr_err("Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07003975 err = -ENOMEM;
3976 goto out;
3977 }
Zhu Yib481de92007-09-25 17:54:57 -07003978 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003979 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003980
Johannes Berg13bb9482010-08-23 10:46:33 +02003981 priv->cmd_queue = IWL39_CMD_QUEUE_NUM;
3982
Johannes Berg246ed352010-08-23 10:46:32 +02003983 /* 3945 has only one valid context */
3984 priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
3985
3986 for (i = 0; i < NUM_IWL_RXON_CTX; i++)
3987 priv->contexts[i].ctxid = i;
3988
Johannes Berg8f2d3d22010-08-23 10:46:37 +02003989 priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
3990 priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
3991 priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
Johannes Berg8dfdb9d2010-08-23 10:46:38 +02003992 priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
Johannes Berg2995baf2010-08-23 10:46:42 +02003993 priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
Johannes Bergc10afb62010-08-23 10:46:43 +02003994 priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
Johannes Bergd0fe4782010-08-23 10:46:58 +02003995 priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
3996 BIT(NL80211_IFTYPE_STATION) |
3997 BIT(NL80211_IFTYPE_ADHOC);
3998 priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
3999 priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
4000 priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
Johannes Berg8f2d3d22010-08-23 10:46:37 +02004001
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004002 /*
4003 * Disabling hardware scan means that mac80211 will perform scans
4004 * "the hard way", rather than using device's scan.
4005 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004006 if (iwl3945_mod_params.disable_hw_scan) {
Wey-Yi Guy7379efe2010-11-08 18:45:21 -08004007 dev_printk(KERN_DEBUG, &(pdev->dev),
4008 "sw scan support is deprecated\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004009 iwl3945_hw_ops.hw_scan = NULL;
4010 }
4011
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004012
Tomas Winklere1623442009-01-27 14:27:56 -08004013 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004014 priv->cfg = cfg;
4015 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07004016 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004017
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004018 if (iwl_alloc_traffic_mem(priv))
4019 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07004020
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004021 /***************************
4022 * 2. Initializing PCI bus
4023 * *************************/
John W. Linville1a7123c2010-08-05 14:39:31 -04004024 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4025 PCIE_LINK_STATE_CLKPM);
4026
Zhu Yib481de92007-09-25 17:54:57 -07004027 if (pci_enable_device(pdev)) {
4028 err = -ENODEV;
4029 goto out_ieee80211_free_hw;
4030 }
4031
4032 pci_set_master(pdev);
4033
Yang Hongyang284901a2009-04-06 19:01:15 -07004034 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004035 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004036 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004037 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004038 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004039 goto out_pci_disable_device;
4040 }
4041
4042 pci_set_drvdata(pdev, priv);
4043 err = pci_request_regions(pdev, DRV_NAME);
4044 if (err)
4045 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004046
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004047 /***********************
4048 * 3. Read REV Register
4049 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004050 priv->hw_base = pci_iomap(pdev, 0, 0);
4051 if (!priv->hw_base) {
4052 err = -ENODEV;
4053 goto out_pci_release_regions;
4054 }
4055
Tomas Winklere1623442009-01-27 14:27:56 -08004056 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004057 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004058 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004059
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004060 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4061 * PCI Tx retries from interfering with C3 CPU state */
4062 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004063
Reinette Chatre731a29b2009-12-14 14:12:11 -08004064 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004065 * we should init now
4066 */
4067 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004068 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004069
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004070 /*
4071 * stop and reset the on-board processor just in case it is in a
4072 * strange state ... like being left stranded by a primary kernel
4073 * and this is now the kdump kernel trying to start up
4074 */
4075 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4076
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004077 /***********************
4078 * 4. Read EEPROM
4079 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004080
Zhu Yi5a669262008-01-14 17:46:18 -08004081 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004082 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004083 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004084 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004085 goto out_iounmap;
Zhu Yi5a669262008-01-14 17:46:18 -08004086 }
4087 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004088 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Johannes Berg30eabc12010-05-12 03:33:10 -07004089 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
4090 SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
Zhu Yi5a669262008-01-14 17:46:18 -08004091
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004092 /***********************
4093 * 5. Setup HW Constants
4094 * ********************/
4095 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004096 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004097 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004098 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004099 }
4100
4101 /***********************
4102 * 6. Setup priv
4103 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004104
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004105 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004106 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004107 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004108 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004109 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004110
Tomas Winkler978785a2008-12-19 10:37:31 +08004111 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4112 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004113
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004114 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004115 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004116 * ********************/
4117
4118 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004119 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004120 spin_unlock_irqrestore(&priv->lock, flags);
4121
Helmut Schaa26635162009-01-15 09:38:44 +01004122 pci_enable_msi(priv->pci_dev);
4123
Wey-Yi Guye39fdee2010-11-10 09:56:40 -08004124 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr_ops.isr,
Mohamed Abbasef850d72009-05-22 11:01:50 -07004125 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004126 if (err) {
4127 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4128 goto out_disable_msi;
4129 }
4130
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004131 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4132 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004133 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004134 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004135 }
4136
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004137 iwl_set_rxon_channel(priv,
Johannes Berg246ed352010-08-23 10:46:32 +02004138 &priv->bands[IEEE80211_BAND_2GHZ].channels[5],
4139 &priv->contexts[IWL_RXON_CTX_BSS]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004140 iwl3945_setup_deferred_work(priv);
4141 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004142 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004143
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004144 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004145 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004146 * *******************************/
4147
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004148 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004149
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004150 err = iwl3945_setup_mac(priv);
4151 if (err)
4152 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004153
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004154 err = iwl_dbgfs_register(priv, DRV_NAME);
4155 if (err)
4156 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4157
Helmut Schaa26635162009-01-15 09:38:44 +01004158 /* Start monitoring the killswitch */
Johannes Bergee525d12010-01-21 06:09:28 -08004159 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01004160 2 * HZ);
4161
Zhu Yib481de92007-09-25 17:54:57 -07004162 return 0;
4163
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004164 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004165 destroy_workqueue(priv->workqueue);
4166 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004167 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4168 out_release_irq:
4169 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004170 out_disable_msi:
4171 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004172 iwlcore_free_geos(priv);
4173 iwl_free_channel_map(priv);
4174 out_unset_hw_params:
4175 iwl3945_unset_hw_params(priv);
4176 out_eeprom_free:
4177 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004178 out_iounmap:
4179 pci_iounmap(pdev, priv->hw_base);
4180 out_pci_release_regions:
4181 pci_release_regions(pdev);
4182 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004183 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004184 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004185 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004186 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004187 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004188 out:
4189 return err;
4190}
4191
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004192static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004193{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004194 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004195 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004196
4197 if (!priv)
4198 return;
4199
Tomas Winklere1623442009-01-27 14:27:56 -08004200 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004201
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004202 iwl_dbgfs_unregister(priv);
4203
Zhu Yib481de92007-09-25 17:54:57 -07004204 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004205
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004206 if (priv->mac80211_registered) {
4207 ieee80211_unregister_hw(priv->hw);
4208 priv->mac80211_registered = 0;
4209 } else {
4210 iwl3945_down(priv);
4211 }
Zhu Yib481de92007-09-25 17:54:57 -07004212
Ben Cahillc166b252009-10-23 13:42:35 -07004213 /*
4214 * Make sure device is reset to low power before unloading driver.
4215 * This may be redundant with iwl_down(), but there are paths to
4216 * run iwl_down() without calling apm_ops.stop(), and there are
4217 * paths to avoid running iwl_down() at all before leaving driver.
4218 * This (inexpensive) call *makes sure* device is reset.
4219 */
Johannes Berg14e8e4a2010-09-22 18:02:10 +02004220 iwl_apm_stop(priv);
Ben Cahillc166b252009-10-23 13:42:35 -07004221
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004222 /* make sure we flush any pending irq or
4223 * tasklet for the driver
4224 */
4225 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004226 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004227 spin_unlock_irqrestore(&priv->lock, flags);
4228
4229 iwl_synchronize_irq(priv);
4230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004231 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004232
Johannes Bergee525d12010-01-21 06:09:28 -08004233 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004234
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004235 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004236
4237 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004238 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004239 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004240
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004241 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004242
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004243 /*netif_stop_queue(dev); */
4244 flush_workqueue(priv->workqueue);
4245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004246 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004247 * priv->workqueue... so we can't take down the workqueue
4248 * until now... */
4249 destroy_workqueue(priv->workqueue);
4250 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004251 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004252
Helmut Schaa26635162009-01-15 09:38:44 +01004253 free_irq(pdev->irq, priv);
4254 pci_disable_msi(pdev);
4255
Zhu Yib481de92007-09-25 17:54:57 -07004256 pci_iounmap(pdev, priv->hw_base);
4257 pci_release_regions(pdev);
4258 pci_disable_device(pdev);
4259 pci_set_drvdata(pdev, NULL);
4260
Samuel Ortize6148912009-01-23 13:45:15 -08004261 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004262 iwlcore_free_geos(priv);
Johannes Berg811ecc92010-04-06 04:12:41 -07004263 kfree(priv->scan_cmd);
Johannes Berg12e934d2010-10-04 05:50:06 -07004264 if (priv->beacon_skb)
4265 dev_kfree_skb(priv->beacon_skb);
Zhu Yib481de92007-09-25 17:54:57 -07004266
4267 ieee80211_free_hw(priv->hw);
4268}
4269
Zhu Yib481de92007-09-25 17:54:57 -07004270
4271/*****************************************************************************
4272 *
4273 * driver and module entry point
4274 *
4275 *****************************************************************************/
4276
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004277static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004278 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004279 .id_table = iwl3945_hw_card_ids,
4280 .probe = iwl3945_pci_probe,
4281 .remove = __devexit_p(iwl3945_pci_remove),
John W. Linvillef60dc012010-10-25 16:12:37 -04004282 .driver.pm = IWL_PM_OPS,
Zhu Yib481de92007-09-25 17:54:57 -07004283};
4284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004285static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004286{
4287
4288 int ret;
Joe Perchesc96c31e2010-07-26 14:39:58 -07004289 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
4290 pr_info(DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004291
4292 ret = iwl3945_rate_control_register();
4293 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004294 pr_err("Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004295 return ret;
4296 }
4297
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004298 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004299 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004300 pr_err("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004301 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004302 }
Zhu Yib481de92007-09-25 17:54:57 -07004303
4304 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004305
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004306error_register:
4307 iwl3945_rate_control_unregister();
4308 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004309}
4310
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004311static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004312{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004313 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004314 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004315}
4316
Reinette Chatrea0987a82008-12-02 12:14:06 -08004317MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004318
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004319module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004320MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004321module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004322MODULE_PARM_DESC(swcrypto,
4323 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004324#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004325module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004326MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004327#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004328module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4329 int, S_IRUGO);
Wey-Yi Guy72645ef2010-10-06 07:42:43 -07004330MODULE_PARM_DESC(disable_hw_scan,
4331 "disable hardware scanning (default 0) (deprecated)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004332module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004333MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4334
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004335module_exit(iwl3945_exit);
4336module_init(iwl3945_init);