blob: 371abbf60eac4949090aa19eef4a59797ffadd3a [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);
Stanislaw Gruszka2cb78652010-11-12 08:47:05 +0100478 u16 len, idx, hdr_len;
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 len = (len + 3) & ~3;
616
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800617 /* Physical address of this Tx command's header (not MAC header!),
618 * within command buffer array. */
Reinette Chatredf833b12009-04-21 10:55:48 -0700619 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
620 len, PCI_DMA_TODEVICE);
621 /* we do not map meta data ... so we can safely access address to
622 * provide to unmap command*/
FUJITA Tomonori2e724442010-06-03 14:19:20 +0900623 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
624 dma_unmap_len_set(out_meta, len, len);
Zhu Yib481de92007-09-25 17:54:57 -0700625
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800626 /* Add buffer containing Tx command and MAC(!) header to TFD's
627 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800628 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
629 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700630
Zhu Yib481de92007-09-25 17:54:57 -0700631
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800632 /* Set up TFD's 2nd entry to point directly to remainder of skb,
633 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700634 len = skb->len - hdr_len;
635 if (len) {
636 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
637 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800638 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
639 phys_addr, len,
640 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700641 }
642
Zhu Yib481de92007-09-25 17:54:57 -0700643
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800644 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800645 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800646 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700647 spin_unlock_irqrestore(&priv->lock, flags);
648
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800649 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700650 && priv->mac80211_registered) {
651 if (wait_write_ptr) {
652 spin_lock_irqsave(&priv->lock, flags);
653 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800654 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700655 spin_unlock_irqrestore(&priv->lock, flags);
656 }
657
Johannes Berg549a04e2010-11-10 18:25:44 -0800658 iwl_stop_queue(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700659 }
660
661 return 0;
662
663drop_unlock:
664 spin_unlock_irqrestore(&priv->lock, flags);
665drop:
666 return -1;
667}
668
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800669static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700670 struct ieee80211_measurement_params *params,
671 u8 type)
672{
Tomas Winkler600c0e12008-12-19 10:37:04 +0800673 struct iwl_spectrum_cmd spectrum;
Zhu Yi2f301222009-10-09 17:19:45 +0800674 struct iwl_rx_packet *pkt;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800675 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700676 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
677 .data = (void *)&spectrum,
Johannes Bergc2acea82009-07-24 11:13:05 -0700678 .flags = CMD_WANT_SKB,
Zhu Yib481de92007-09-25 17:54:57 -0700679 };
680 u32 add_time = le64_to_cpu(params->start_time);
681 int rc;
682 int spectrum_resp_status;
683 int duration = le16_to_cpu(params->duration);
Johannes Berg246ed352010-08-23 10:46:32 +0200684 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -0700685
Johannes Berg246ed352010-08-23 10:46:32 +0200686 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700687 add_time = iwl_usecs_to_beacons(priv,
Johannes Berge99f1682010-01-19 10:04:28 -0800688 le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
Johannes Berg246ed352010-08-23 10:46:32 +0200689 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700690
691 memset(&spectrum, 0, sizeof(spectrum));
692
693 spectrum.channel_count = cpu_to_le16(1);
694 spectrum.flags =
695 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
696 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
697 cmd.len = sizeof(spectrum);
698 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
699
Johannes Berg246ed352010-08-23 10:46:32 +0200700 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Zhu Yib481de92007-09-25 17:54:57 -0700701 spectrum.start_time =
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700702 iwl_add_beacon_time(priv,
703 priv->_3945.last_beacon_time, add_time,
Johannes Berg246ed352010-08-23 10:46:32 +0200704 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700705 else
706 spectrum.start_time = 0;
707
708 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
709 spectrum.channels[0].channel = params->channel;
710 spectrum.channels[0].type = type;
Johannes Berg246ed352010-08-23 10:46:32 +0200711 if (ctx->active.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700712 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
713 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
714
Samuel Ortiz518099a2009-01-19 15:30:27 -0800715 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700716 if (rc)
717 return rc;
718
Zhu Yi2f301222009-10-09 17:19:45 +0800719 pkt = (struct iwl_rx_packet *)cmd.reply_page;
720 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800721 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700722 rc = -EIO;
723 }
724
Zhu Yi2f301222009-10-09 17:19:45 +0800725 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
Zhu Yib481de92007-09-25 17:54:57 -0700726 switch (spectrum_resp_status) {
727 case 0: /* Command will be handled */
Zhu Yi2f301222009-10-09 17:19:45 +0800728 if (pkt->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800729 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800730 pkt->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700731 priv->measurement_status &= ~MEASUREMENT_READY;
732 }
733 priv->measurement_status |= MEASUREMENT_ACTIVE;
734 rc = 0;
735 break;
736
737 case 1: /* Command will not be handled */
738 rc = -EAGAIN;
739 break;
740 }
741
Zhu Yi64a76b52009-12-10 14:37:21 -0800742 iwl_free_pages(priv, cmd.reply_page);
Zhu Yib481de92007-09-25 17:54:57 -0700743
744 return rc;
745}
Zhu Yib481de92007-09-25 17:54:57 -0700746
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800747static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800748 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700749{
Zhu Yi2f301222009-10-09 17:19:45 +0800750 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800751 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700752 struct delayed_work *pwork;
753
754 palive = &pkt->u.alive_frame;
755
Tomas Winklere1623442009-01-27 14:27:56 -0800756 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700757 "0x%01X 0x%01X\n",
758 palive->is_valid, palive->ver_type,
759 palive->ver_subtype);
760
761 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800762 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800763 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
764 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700765 pwork = &priv->init_alive_start;
766 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800767 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700768 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800769 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700770 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800771 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700772 }
773
774 /* We delay the ALIVE response by 5ms to
775 * give the HW RF Kill time to activate... */
776 if (palive->is_valid == UCODE_VALID_OK)
777 queue_delayed_work(priv->workqueue, pwork,
778 msecs_to_jiffies(5));
779 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800780 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700781}
782
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800783static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800784 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700785{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100786#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800787 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Helmut Schaac7e035a2009-01-19 13:02:15 +0100788#endif
Zhu Yib481de92007-09-25 17:54:57 -0700789
Tomas Winklere1623442009-01-27 14:27:56 -0800790 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700791}
792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800793static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700794{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800795 struct iwl_priv *priv =
796 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700797 struct sk_buff *beacon;
798
799 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg8bd413e2010-08-23 10:46:40 +0200800 beacon = ieee80211_beacon_get(priv->hw,
801 priv->contexts[IWL_RXON_CTX_BSS].vif);
Zhu Yib481de92007-09-25 17:54:57 -0700802
803 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800804 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700805 return;
806 }
807
808 mutex_lock(&priv->mutex);
809 /* new beacon skb is allocated every time; dispose previous.*/
Johannes Berg12e934d2010-10-04 05:50:06 -0700810 if (priv->beacon_skb)
811 dev_kfree_skb(priv->beacon_skb);
Zhu Yib481de92007-09-25 17:54:57 -0700812
Johannes Berg12e934d2010-10-04 05:50:06 -0700813 priv->beacon_skb = beacon;
Zhu Yib481de92007-09-25 17:54:57 -0700814 mutex_unlock(&priv->mutex);
815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800816 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700817}
818
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800819static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800820 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700821{
Zhu Yi2f301222009-10-09 17:19:45 +0800822 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Johannes Berga85d7cc2010-07-31 08:34:10 -0700824#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700825 u8 rate = beacon->beacon_notify_hdr.rate;
826
Tomas Winklere1623442009-01-27 14:27:56 -0800827 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700828 "tsf %d %d rate %d\n",
829 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
830 beacon->beacon_notify_hdr.failure_frame,
831 le32_to_cpu(beacon->ibss_mgr_status),
832 le32_to_cpu(beacon->high_tsf),
833 le32_to_cpu(beacon->low_tsf), rate);
834#endif
835
Johannes Berga85d7cc2010-07-31 08:34:10 -0700836 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
837
Johannes Berg05c914f2008-09-11 00:01:58 +0200838 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700839 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
840 queue_work(priv->workqueue, &priv->beacon_update);
841}
842
Zhu Yib481de92007-09-25 17:54:57 -0700843/* Handle notification from uCode that card's power state is changing
844 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800845static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800846 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700847{
Zhu Yi2f301222009-10-09 17:19:45 +0800848 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700849 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
850 unsigned long status = priv->status;
851
Reinette Chatre4c423a22009-07-17 09:30:26 -0700852 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700853 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
854 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
855
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800856 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700857 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
858
859 if (flags & HW_CARD_DISABLED)
860 set_bit(STATUS_RF_KILL_HW, &priv->status);
861 else
862 clear_bit(STATUS_RF_KILL_HW, &priv->status);
863
864
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800865 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700866
867 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200868 test_bit(STATUS_RF_KILL_HW, &priv->status)))
869 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
870 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700871 else
872 wake_up_interruptible(&priv->wait_command_queue);
873}
874
875/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800876 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700877 *
878 * Setup the RX handlers for each of the reply types sent from the uCode
879 * to the host.
880 *
881 * This function chains into the hardware specific files for them to setup
882 * any hardware specific handlers as well.
883 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800884static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700885{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800886 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
887 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800888 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800889 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Reinette Chatre81963d62010-01-22 14:22:57 -0800890 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
891 iwl_rx_spectrum_measure_notif;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800892 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700893 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800894 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800895 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700896
Ben Cahill9fbab512007-11-29 11:09:47 +0800897 /*
898 * The same handler is used for both the REPLY to a discrete
899 * statistics request from the host as well as for the periodic
900 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700901 */
Abhijeet Kolekar17f36fc2010-04-16 10:03:54 -0700902 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800903 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700904
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800905 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800906 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700907
Ben Cahill9fbab512007-11-29 11:09:47 +0800908 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800909 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700910}
911
Zhu Yib481de92007-09-25 17:54:57 -0700912/************************** RX-FUNCTIONS ****************************/
913/*
914 * Rx theory of operation
915 *
916 * The host allocates 32 DMA target addresses and passes the host address
917 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
918 * 0 to 31
919 *
920 * Rx Queue Indexes
921 * The host/firmware share two index registers for managing the Rx buffers.
922 *
923 * The READ index maps to the first position that the firmware may be writing
924 * to -- the driver can read up to (but not including) this position and get
925 * good data.
926 * The READ index is managed by the firmware once the card is enabled.
927 *
928 * The WRITE index maps to the last position the driver has read from -- the
929 * position preceding WRITE is the last slot the firmware can place a packet.
930 *
931 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
932 * WRITE = READ.
933 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800934 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -0700935 * INDEX position, and WRITE to the last (READ - 1 wrapped)
936 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800937 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -0700938 * and fire the RX interrupt. The driver can then query the READ index and
939 * process as many packets as possible, moving the WRITE index forward as it
940 * resets the Rx queue buffers with new memory.
941 *
942 * The management in the driver is as follows:
943 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
944 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +0800945 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800946 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -0700947 * iwl->rxq is replenished and the READ INDEX is updated (updating the
948 * 'processed' and 'read' driver indexes as well)
949 * + A received packet is processed and handed to the kernel network stack,
950 * detached from the iwl->rxq. The driver 'processed' index is updated.
951 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
952 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
953 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
954 * were enough free buffers and RX_STALLED is set it is cleared.
955 *
956 *
957 * Driver sequence:
958 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800959 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800960 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +0800961 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -0700962 * queue, updates firmware pointers, and updates
963 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800964 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -0700965 *
966 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800967 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -0700968 * READ INDEX, detaching the SKB from the pool.
969 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800970 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -0700971 * slots.
972 * ...
973 *
974 */
975
976/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800977 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -0700978 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800979static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700980 dma_addr_t dma_addr)
981{
982 return cpu_to_le32((u32)dma_addr);
983}
984
985/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800986 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -0700987 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800988 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -0700989 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +0800990 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -0700991 *
992 * This moves the 'write' index forward to catch up with 'processed', and
993 * also updates the memory address in the firmware to reference the new
994 * target buffer.
995 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800996static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700997{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +0800998 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -0700999 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001000 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001001 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001002 int write;
Zhu Yib481de92007-09-25 17:54:57 -07001003
1004 spin_lock_irqsave(&rxq->lock, flags);
1005 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001006 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001007 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001008 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001009 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001010 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001011
1012 /* Point to Rx buffer via next RBD in circular buffer */
Zhu Yi2f301222009-10-09 17:19:45 +08001013 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
Zhu Yib481de92007-09-25 17:54:57 -07001014 rxq->queue[rxq->write] = rxb;
1015 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1016 rxq->free_count--;
1017 }
1018 spin_unlock_irqrestore(&rxq->lock, flags);
1019 /* If the pre-allocated buffer pool is dropping low, schedule to
1020 * refill it */
1021 if (rxq->free_count <= RX_LOW_WATERMARK)
1022 queue_work(priv->workqueue, &priv->rx_replenish);
1023
1024
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001025 /* If we've added more space for the firmware to place data, tell it.
1026 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001027 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001028 || (abs(rxq->write - rxq->read) > 7)) {
1029 spin_lock_irqsave(&rxq->lock, flags);
1030 rxq->need_update = 1;
1031 spin_unlock_irqrestore(&rxq->lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001032 iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001033 }
Zhu Yib481de92007-09-25 17:54:57 -07001034}
1035
1036/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001037 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001038 *
1039 * When moving to rx_free an SKB is allocated for the slot.
1040 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001041 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001042 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001043 */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001044static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001045{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001046 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001047 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001048 struct iwl_rx_mem_buffer *rxb;
Zhu Yi2f301222009-10-09 17:19:45 +08001049 struct page *page;
Zhu Yib481de92007-09-25 17:54:57 -07001050 unsigned long flags;
Zhu Yi29b1b262009-10-23 13:42:25 -07001051 gfp_t gfp_mask = priority;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001052
1053 while (1) {
1054 spin_lock_irqsave(&rxq->lock, flags);
1055
1056 if (list_empty(&rxq->rx_used)) {
1057 spin_unlock_irqrestore(&rxq->lock, flags);
1058 return;
1059 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001060 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001061
Reinette Chatref82a9242009-09-17 10:43:56 -07001062 if (rxq->free_count > RX_LOW_WATERMARK)
Zhu Yi29b1b262009-10-23 13:42:25 -07001063 gfp_mask |= __GFP_NOWARN;
Zhu Yi2f301222009-10-09 17:19:45 +08001064
1065 if (priv->hw_params.rx_page_order > 0)
Zhu Yi29b1b262009-10-23 13:42:25 -07001066 gfp_mask |= __GFP_COMP;
Zhu Yi2f301222009-10-09 17:19:45 +08001067
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001068 /* Alloc a new receive buffer */
Zhu Yi29b1b262009-10-23 13:42:25 -07001069 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
Zhu Yi2f301222009-10-09 17:19:45 +08001070 if (!page) {
Zhu Yib481de92007-09-25 17:54:57 -07001071 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001072 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1073 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1074 net_ratelimit())
1075 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1076 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1077 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001078 /* We don't reschedule replenish work here -- we will
1079 * call the restock method and if it still needs
1080 * more buffers it will schedule replenish */
1081 break;
1082 }
Zhu Yi12342c42007-12-20 11:27:32 +08001083
Reinette Chatrede0bd502009-09-11 10:38:12 -07001084 spin_lock_irqsave(&rxq->lock, flags);
1085 if (list_empty(&rxq->rx_used)) {
1086 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001087 __free_pages(page, priv->hw_params.rx_page_order);
Reinette Chatrede0bd502009-09-11 10:38:12 -07001088 return;
1089 }
1090 element = rxq->rx_used.next;
1091 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1092 list_del(element);
1093 spin_unlock_irqrestore(&rxq->lock, flags);
1094
Zhu Yi2f301222009-10-09 17:19:45 +08001095 rxb->page = page;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001096 /* Get physical address of RB/SKB */
Zhu Yi2f301222009-10-09 17:19:45 +08001097 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1098 PAGE_SIZE << priv->hw_params.rx_page_order,
1099 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001100
1101 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001102
Zhu Yib481de92007-09-25 17:54:57 -07001103 list_add_tail(&rxb->list, &rxq->rx_free);
1104 rxq->free_count++;
Zhu Yi2f301222009-10-09 17:19:45 +08001105 priv->alloc_rxb_page++;
1106
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001107 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001108 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001109}
1110
Reinette Chatredf833b12009-04-21 10:55:48 -07001111void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1112{
1113 unsigned long flags;
1114 int i;
1115 spin_lock_irqsave(&rxq->lock, flags);
1116 INIT_LIST_HEAD(&rxq->rx_free);
1117 INIT_LIST_HEAD(&rxq->rx_used);
1118 /* Fill the rx_used queue with _all_ of the Rx buffers */
1119 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1120 /* In the reset function, these buffers may have been allocated
1121 * to an SKB, so we need to unmap and free potential storage */
Zhu Yi2f301222009-10-09 17:19:45 +08001122 if (rxq->pool[i].page != NULL) {
1123 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1124 PAGE_SIZE << priv->hw_params.rx_page_order,
1125 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001126 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001127 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001128 }
1129 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1130 }
1131
1132 /* Set us so that we have processed and used all buffers, but have
1133 * not restocked the Rx queue with fresh buffers */
1134 rxq->read = rxq->write = 0;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001135 rxq->write_actual = 0;
Zhu Yi2f301222009-10-09 17:19:45 +08001136 rxq->free_count = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001137 spin_unlock_irqrestore(&rxq->lock, flags);
1138}
Reinette Chatredf833b12009-04-21 10:55:48 -07001139
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001140void iwl3945_rx_replenish(void *data)
1141{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001142 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001143 unsigned long flags;
1144
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001145 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001146
1147 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001148 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001149 spin_unlock_irqrestore(&priv->lock, flags);
1150}
1151
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001152static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1153{
1154 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1155
1156 iwl3945_rx_queue_restock(priv);
1157}
1158
1159
Reinette Chatredf833b12009-04-21 10:55:48 -07001160/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1161 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1162 * This free routine walks the list of POOL entries and if SKB is set to
1163 * non NULL it is unmapped and freed
1164 */
1165static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1166{
1167 int i;
1168 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
Zhu Yi2f301222009-10-09 17:19:45 +08001169 if (rxq->pool[i].page != NULL) {
1170 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1171 PAGE_SIZE << priv->hw_params.rx_page_order,
1172 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001173 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001174 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001175 }
1176 }
1177
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001178 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
Emmanuel Grumbachd5b25c92010-06-07 13:21:46 -07001179 rxq->bd_dma);
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001180 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1181 rxq->rb_stts, rxq->rb_stts_dma);
Reinette Chatredf833b12009-04-21 10:55:48 -07001182 rxq->bd = NULL;
1183 rxq->rb_stts = NULL;
1184}
Reinette Chatredf833b12009-04-21 10:55:48 -07001185
1186
Zhu Yib481de92007-09-25 17:54:57 -07001187/* Convert linear signal-to-noise ratio into dB */
1188static u8 ratio2dB[100] = {
1189/* 0 1 2 3 4 5 6 7 8 9 */
1190 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1191 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1192 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1193 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1194 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1195 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1196 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1197 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1198 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1199 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1200};
1201
1202/* Calculates a relative dB value from a ratio of linear
1203 * (i.e. not dB) signal levels.
1204 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001205int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001206{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001207 /* 1000:1 or higher just report as 60 dB */
1208 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001209 return 60;
1210
Adrian Bunk221c80c2008-02-02 23:19:01 +02001211 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001212 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001213 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001214 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001215
1216 /* We shouldn't see this */
1217 if (sig_ratio < 1)
1218 return 0;
1219
1220 /* Use table for ratios 1:1 - 99:1 */
1221 return (int)ratio2dB[sig_ratio];
1222}
1223
Zhu Yib481de92007-09-25 17:54:57 -07001224/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001225 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001226 *
1227 * Uses the priv->rx_handlers callback function array to invoke
1228 * the appropriate handlers, including command responses,
1229 * frame-received notifications, and other notifications.
1230 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001231static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001232{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001233 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001234 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001235 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001236 u32 r, i;
1237 int reclaim;
1238 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001239 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001240 u32 count = 8;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001241 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001242
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001243 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1244 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001245 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001246 i = rxq->read;
1247
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001248 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -07001249 total_empty = r - rxq->write_actual;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001250 if (total_empty < 0)
1251 total_empty += RX_QUEUE_SIZE;
1252
1253 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001254 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001255 /* Rx interrupt, but nothing sent from uCode */
1256 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001257 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001258
1259 while (i != r) {
Johannes Bergf4989d92010-05-28 04:08:30 -07001260 int len;
1261
Zhu Yib481de92007-09-25 17:54:57 -07001262 rxb = rxq->queue[i];
1263
Ben Cahill9fbab512007-11-29 11:09:47 +08001264 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001265 * then a bug has been introduced in the queue refilling
1266 * routines -- catch it here */
1267 BUG_ON(rxb == NULL);
1268
1269 rxq->queue[i] = NULL;
1270
Zhu Yi2f301222009-10-09 17:19:45 +08001271 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1272 PAGE_SIZE << priv->hw_params.rx_page_order,
1273 PCI_DMA_FROMDEVICE);
1274 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001275
Johannes Bergf4989d92010-05-28 04:08:30 -07001276 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1277 len += sizeof(u32); /* account for status word */
1278 trace_iwlwifi_dev_rx(priv, pkt, len);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001279
Zhu Yib481de92007-09-25 17:54:57 -07001280 /* Reclaim a command buffer only if this packet is a response
1281 * to a (driver-originated) command.
1282 * If the packet (e.g. Rx frame) originated from uCode,
1283 * there is no command buffer to reclaim.
1284 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1285 * but apparently a few don't get set; catch them here. */
1286 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1287 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1288 (pkt->hdr.cmd != REPLY_TX);
1289
1290 /* Based on type of command response or notification,
1291 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001292 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001293 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001294 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001295 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001296 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -07001297 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001298 } else {
1299 /* No handling needed */
Zhu Yi2f301222009-10-09 17:19:45 +08001300 IWL_DEBUG_RX(priv,
1301 "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001302 r, i, get_cmd_string(pkt->hdr.cmd),
1303 pkt->hdr.cmd);
1304 }
1305
Zhu Yi29b1b262009-10-23 13:42:25 -07001306 /*
1307 * XXX: After here, we should always check rxb->page
1308 * against NULL before touching it or its virtual
1309 * memory (pkt). Because some rx_handler might have
1310 * already taken or freed the pages.
1311 */
1312
Zhu Yib481de92007-09-25 17:54:57 -07001313 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001314 /* Invoke any callbacks, transfer the buffer to caller,
1315 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001316 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001317 if (rxb->page)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001318 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001319 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001320 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001321 }
1322
Zhu Yi73005152009-10-23 13:42:32 -07001323 /* Reuse the page if possible. For notification packets and
1324 * SKBs that fail to Rx correctly, add them back into the
1325 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001326 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001327 if (rxb->page != NULL) {
1328 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1329 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1330 PCI_DMA_FROMDEVICE);
1331 list_add_tail(&rxb->list, &rxq->rx_free);
1332 rxq->free_count++;
1333 } else
1334 list_add_tail(&rxb->list, &rxq->rx_used);
1335
Zhu Yib481de92007-09-25 17:54:57 -07001336 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001337
Zhu Yib481de92007-09-25 17:54:57 -07001338 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001339 /* If there are a lot of unused frames,
1340 * restock the Rx queue so ucode won't assert. */
1341 if (fill_rx) {
1342 count++;
1343 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001344 rxq->read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001345 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001346 count = 0;
1347 }
1348 }
Zhu Yib481de92007-09-25 17:54:57 -07001349 }
1350
1351 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001352 rxq->read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001353 if (fill_rx)
1354 iwl3945_rx_replenish_now(priv);
1355 else
1356 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001357}
1358
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001359/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001360static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001361{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001362 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001363 synchronize_irq(priv->pci_dev->irq);
1364 tasklet_kill(&priv->irq_tasklet);
1365}
1366
Zhu Yib481de92007-09-25 17:54:57 -07001367static const char *desc_lookup(int i)
1368{
1369 switch (i) {
1370 case 1:
1371 return "FAIL";
1372 case 2:
1373 return "BAD_PARAM";
1374 case 3:
1375 return "BAD_CHECKSUM";
1376 case 4:
1377 return "NMI_INTERRUPT";
1378 case 5:
1379 return "SYSASSERT";
1380 case 6:
1381 return "FATAL_ERROR";
1382 }
1383
1384 return "UNKNOWN";
1385}
1386
1387#define ERROR_START_OFFSET (1 * sizeof(u32))
1388#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1389
Reinette Chatreb7a79402009-09-25 14:24:23 -07001390void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001391{
1392 u32 i;
1393 u32 desc, time, count, base, data1;
1394 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001395
1396 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1397
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001398 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001399 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001400 return;
1401 }
1402
Zhu Yib481de92007-09-25 17:54:57 -07001403
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001404 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001405
1406 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001407 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1408 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1409 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001410 }
1411
Winkler, Tomas15b16872008-12-19 10:37:33 +08001412 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001413 "ilink1 nmiPC Line\n");
1414 for (i = ERROR_START_OFFSET;
1415 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1416 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001417 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001418 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001419 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001420 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001421 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001422 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001423 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001424 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001425 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001426 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001427 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001428 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001429 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001430
Winkler, Tomas15b16872008-12-19 10:37:33 +08001431 IWL_ERR(priv,
Jay Sternberg87563712010-06-14 14:40:40 -07001432 "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +08001433 desc_lookup(desc), desc, time, blink1, blink2,
1434 ilink1, ilink2, data1);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001435 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1436 0, blink1, blink2, ilink1, ilink2);
Zhu Yib481de92007-09-25 17:54:57 -07001437 }
Zhu Yib481de92007-09-25 17:54:57 -07001438}
1439
Ben Cahillf58177b2007-11-29 11:09:43 +08001440#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001441
1442/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001443 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001444 *
Zhu Yib481de92007-09-25 17:54:57 -07001445 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001446static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1447 u32 num_events, u32 mode,
1448 int pos, char **buf, size_t bufsz)
Zhu Yib481de92007-09-25 17:54:57 -07001449{
1450 u32 i;
1451 u32 base; /* SRAM byte address of event log header */
1452 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1453 u32 ptr; /* SRAM byte address of log data */
1454 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001455 unsigned long reg_flags;
Zhu Yib481de92007-09-25 17:54:57 -07001456
1457 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001458 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001459
1460 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1461
1462 if (mode == 0)
1463 event_size = 2 * sizeof(u32);
1464 else
1465 event_size = 3 * sizeof(u32);
1466
1467 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1468
Ben Cahille5854472009-11-06 14:52:58 -08001469 /* Make sure device is powered up for SRAM reads */
1470 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1471 iwl_grab_nic_access(priv);
1472
1473 /* Set starting address; reads will auto-increment */
1474 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1475 rmb();
1476
Zhu Yib481de92007-09-25 17:54:57 -07001477 /* "time" is actually "data" for mode 0 (no timestamp).
1478 * place event id # at far right for easier visual parsing. */
1479 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001480 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1481 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001482 if (mode == 0) {
1483 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001484 if (bufsz) {
1485 pos += scnprintf(*buf + pos, bufsz - pos,
1486 "0x%08x:%04u\n",
1487 time, ev);
1488 } else {
1489 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1490 trace_iwlwifi_dev_ucode_event(priv, 0,
1491 time, ev);
1492 }
Winkler, Tomas15b16872008-12-19 10:37:33 +08001493 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001494 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001495 if (bufsz) {
1496 pos += scnprintf(*buf + pos, bufsz - pos,
1497 "%010u:0x%08x:%04u\n",
1498 time, data, ev);
1499 } else {
1500 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1501 time, data, ev);
1502 trace_iwlwifi_dev_ucode_event(priv, time,
1503 data, ev);
1504 }
Zhu Yib481de92007-09-25 17:54:57 -07001505 }
1506 }
Ben Cahille5854472009-11-06 14:52:58 -08001507
1508 /* Allow device to power down */
1509 iwl_release_nic_access(priv);
1510 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001511 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001512}
1513
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001514/**
1515 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1516 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001517static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001518 u32 num_wraps, u32 next_entry,
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001519 u32 size, u32 mode,
1520 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001521{
1522 /*
1523 * display the newest DEFAULT_LOG_ENTRIES entries
1524 * i.e the entries just before the next ont that uCode would fill.
1525 */
1526 if (num_wraps) {
1527 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001528 pos = iwl3945_print_event_log(priv,
1529 capacity - (size - next_entry),
1530 size - next_entry, mode,
1531 pos, buf, bufsz);
1532 pos = iwl3945_print_event_log(priv, 0,
1533 next_entry, mode,
1534 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001535 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001536 pos = iwl3945_print_event_log(priv, next_entry - size,
1537 size, mode,
1538 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001539 } else {
1540 if (next_entry < size)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001541 pos = iwl3945_print_event_log(priv, 0,
1542 next_entry, mode,
1543 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001544 else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001545 pos = iwl3945_print_event_log(priv, next_entry - size,
1546 size, mode,
1547 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001548 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001549 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001550}
1551
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001552#define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1553
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001554int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1555 char **buf, bool display)
Zhu Yib481de92007-09-25 17:54:57 -07001556{
Zhu Yib481de92007-09-25 17:54:57 -07001557 u32 base; /* SRAM byte address of event log header */
1558 u32 capacity; /* event log capacity in # entries */
1559 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1560 u32 num_wraps; /* # times uCode wrapped to top of log */
1561 u32 next_entry; /* index of next entry to be written by uCode */
1562 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001563 int pos = 0;
1564 size_t bufsz = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001565
1566 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001567 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001568 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001569 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001570 }
1571
Zhu Yib481de92007-09-25 17:54:57 -07001572 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001573 capacity = iwl_read_targ_mem(priv, base);
1574 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1575 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1576 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001577
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07001578 if (capacity > priv->cfg->base_params->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001579 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07001580 capacity, priv->cfg->base_params->max_event_log_size);
1581 capacity = priv->cfg->base_params->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001582 }
1583
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07001584 if (next_entry > priv->cfg->base_params->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001585 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07001586 next_entry, priv->cfg->base_params->max_event_log_size);
1587 next_entry = priv->cfg->base_params->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001588 }
1589
Zhu Yib481de92007-09-25 17:54:57 -07001590 size = num_wraps ? capacity : next_entry;
1591
1592 /* bail out if nothing in log */
1593 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001594 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001595 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001596 }
1597
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001598#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001599 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001600 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1601 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1602#else
1603 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1604 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1605#endif
1606
1607 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1608 size);
Zhu Yib481de92007-09-25 17:54:57 -07001609
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001610#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001611 if (display) {
1612 if (full_log)
1613 bufsz = capacity * 48;
1614 else
1615 bufsz = size * 48;
1616 *buf = kmalloc(bufsz, GFP_KERNEL);
1617 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001618 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001619 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001620 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1621 /* if uCode has wrapped back to top of log,
1622 * start at the oldest entry,
1623 * i.e the next one that uCode would fill.
1624 */
1625 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001626 pos = iwl3945_print_event_log(priv, next_entry,
1627 capacity - next_entry, mode,
1628 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001629
1630 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001631 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1632 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001633 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001634 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1635 next_entry, size, mode,
1636 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001637#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001638 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1639 next_entry, size, mode,
1640 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001641#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001642 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001643}
1644
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001645static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001646{
1647 u32 inta, handled = 0;
1648 u32 inta_fh;
1649 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001650#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001651 u32 inta_mask;
1652#endif
1653
1654 spin_lock_irqsave(&priv->lock, flags);
1655
1656 /* Ack/clear/reset pending uCode interrupts.
1657 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1658 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001659 inta = iwl_read32(priv, CSR_INT);
1660 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001661
1662 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1663 * Any new interrupts that happen after this, either while we're
1664 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001665 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1666 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001667
Samuel Ortizd08853a2009-01-23 13:45:17 -08001668#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001669 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001670 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001671 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001672 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001673 inta, inta_mask, inta_fh);
1674 }
1675#endif
1676
Zhu Yi2f301222009-10-09 17:19:45 +08001677 spin_unlock_irqrestore(&priv->lock, flags);
1678
Zhu Yib481de92007-09-25 17:54:57 -07001679 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1680 * atomic, make sure that inta covers all the interrupts that
1681 * we've discovered, even if FH interrupt came in just after
1682 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001683 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001684 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001685 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001686 inta |= CSR_INT_BIT_FH_TX;
1687
1688 /* Now service all interrupt bits discovered above. */
1689 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001690 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001691
1692 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001693 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001694
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001695 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001696 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001697
1698 handled |= CSR_INT_BIT_HW_ERR;
1699
Zhu Yib481de92007-09-25 17:54:57 -07001700 return;
1701 }
1702
Samuel Ortizd08853a2009-01-23 13:45:17 -08001703#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001704 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001705 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001706 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001707 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001708 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001709 priv->isr_stats.sch++;
1710 }
Zhu Yib481de92007-09-25 17:54:57 -07001711
1712 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001713 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001714 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001715 priv->isr_stats.alive++;
1716 }
Zhu Yib481de92007-09-25 17:54:57 -07001717 }
1718#endif
1719 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001720 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001721
Zhu Yib481de92007-09-25 17:54:57 -07001722 /* Error detected by uCode */
1723 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001724 IWL_ERR(priv, "Microcode SW error detected. "
1725 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001726 priv->isr_stats.sw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001727 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001728 handled |= CSR_INT_BIT_SW_ERR;
1729 }
1730
1731 /* uCode wakes up after power-down sleep */
1732 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001733 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001734 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001735 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1736 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1737 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1738 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1739 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1740 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001741
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001742 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001743 handled |= CSR_INT_BIT_WAKEUP;
1744 }
1745
1746 /* All uCode command responses, including Tx command responses,
1747 * Rx "responses" (frame-received notification), and other
1748 * notifications from uCode come through here*/
1749 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001750 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001751 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001752 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1753 }
1754
1755 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001756 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001757 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001758
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001759 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001760 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1761 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001762 handled |= CSR_INT_BIT_FH_TX;
1763 }
1764
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001765 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001766 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001767 priv->isr_stats.unhandled++;
1768 }
Zhu Yib481de92007-09-25 17:54:57 -07001769
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001770 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001771 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001772 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001773 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001774 }
1775
1776 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001777 /* only Re-enable if disabled by irq */
1778 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001779 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001780
Samuel Ortizd08853a2009-01-23 13:45:17 -08001781#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001782 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001783 inta = iwl_read32(priv, CSR_INT);
1784 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1785 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001786 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001787 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1788 }
1789#endif
Zhu Yib481de92007-09-25 17:54:57 -07001790}
1791
Abhijeet Kolekar14023642010-06-02 21:15:10 -07001792static int iwl3945_get_single_channel_for_scan(struct iwl_priv *priv,
1793 struct ieee80211_vif *vif,
1794 enum ieee80211_band band,
1795 struct iwl3945_scan_channel *scan_ch)
1796{
1797 const struct ieee80211_supported_band *sband;
1798 u16 passive_dwell = 0;
1799 u16 active_dwell = 0;
1800 int added = 0;
1801 u8 channel = 0;
1802
1803 sband = iwl_get_hw_mode(priv, band);
1804 if (!sband) {
1805 IWL_ERR(priv, "invalid band\n");
1806 return added;
1807 }
1808
1809 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
1810 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
1811
1812 if (passive_dwell <= active_dwell)
1813 passive_dwell = active_dwell + 1;
1814
1815
1816 channel = iwl_get_single_channel_number(priv, band);
1817
1818 if (channel) {
1819 scan_ch->channel = channel;
1820 scan_ch->type = 0; /* passive */
1821 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1822 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1823 /* Set txpower levels to defaults */
1824 scan_ch->tpc.dsp_atten = 110;
1825 if (band == IEEE80211_BAND_5GHZ)
1826 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1827 else
1828 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1829 added++;
1830 } else
1831 IWL_ERR(priv, "no valid channel found\n");
1832 return added;
1833}
1834
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001835static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001836 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001837 u8 is_active, u8 n_probes,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001838 struct iwl3945_scan_channel *scan_ch,
1839 struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07001840{
Johannes Berg4e05c232009-06-19 13:52:44 -07001841 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001842 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001843 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001844 u16 passive_dwell = 0;
1845 u16 active_dwell = 0;
1846 int added, i;
1847
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001848 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001849 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001850 return 0;
1851
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001852 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001853 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Zhu Yib481de92007-09-25 17:54:57 -07001854
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001855 if (passive_dwell <= active_dwell)
1856 passive_dwell = active_dwell + 1;
1857
Johannes Berg4e05c232009-06-19 13:52:44 -07001858 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1859 chan = priv->scan_request->channels[i];
1860
1861 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001862 continue;
1863
Johannes Berg4e05c232009-06-19 13:52:44 -07001864 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001865
Samuel Ortize6148912009-01-23 13:45:15 -08001866 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001867 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001868 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001869 scan_ch->channel);
1870 continue;
1871 }
1872
Zhu Yib481de92007-09-25 17:54:57 -07001873 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1874 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001875 /* If passive , set up for auto-switch
1876 * and use long active_dwell time.
1877 */
1878 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001879 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001880 scan_ch->type = 0; /* passive */
1881 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1882 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1883 } else {
1884 scan_ch->type = 1; /* active */
1885 }
1886
1887 /* Set direct probe bits. These may be used both for active
1888 * scan channels (probes gets sent right away),
1889 * or for passive channels (probes get se sent only after
1890 * hearing clear Rx packet).*/
1891 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1892 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001893 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001894 } else {
1895 /* uCode v1 does not allow setting direct probe bits on
1896 * passive channel. */
1897 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001898 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001899 }
Zhu Yib481de92007-09-25 17:54:57 -07001900
Ben Cahill9fbab512007-11-29 11:09:47 +08001901 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001902 scan_ch->tpc.dsp_atten = 110;
1903 /* scan_pwr_info->tpc.dsp_atten; */
1904
1905 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001906 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001907 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1908 else {
1909 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1910 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001911 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001912 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001913 */
1914 }
1915
Tomas Winklere1623442009-01-27 14:27:56 -08001916 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001917 scan_ch->channel,
1918 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1919 (scan_ch->type & 1) ?
1920 active_dwell : passive_dwell);
1921
1922 scan_ch++;
1923 added++;
1924 }
1925
Frans Pop91dd6c22010-03-24 14:19:58 -07001926 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001927 return added;
1928}
1929
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001930static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001931 struct ieee80211_rate *rates)
1932{
1933 int i;
1934
Dan Carpenter8e1a53c2010-03-28 14:55:00 +03001935 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001936 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1937 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1938 rates[i].hw_value_short = i;
1939 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001940 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001941 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001942 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001943 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001944 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001945 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001946 }
Zhu Yib481de92007-09-25 17:54:57 -07001947 }
1948}
1949
Zhu Yib481de92007-09-25 17:54:57 -07001950/******************************************************************************
1951 *
1952 * uCode download functions
1953 *
1954 ******************************************************************************/
1955
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001956static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001957{
Tomas Winkler98c92212008-01-14 17:46:20 -08001958 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1959 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1960 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1961 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1962 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1963 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001964}
1965
1966/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001967 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001968 * looking at all data.
1969 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001970static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001971{
1972 u32 val;
1973 u32 save_len = len;
1974 int rc = 0;
1975 u32 errcnt;
1976
Tomas Winklere1623442009-01-27 14:27:56 -08001977 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001978
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001979 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001980 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07001981
1982 errcnt = 0;
1983 for (; len > 0; len -= sizeof(u32), image++) {
1984 /* read data comes through single port, auto-incr addr */
1985 /* NOTE: Use the debugless read so we don't flood kernel log
1986 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001987 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001988 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001989 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001990 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1991 save_len - len, val, le32_to_cpu(*image));
1992 rc = -EIO;
1993 errcnt++;
1994 if (errcnt >= 20)
1995 break;
1996 }
1997 }
1998
Zhu Yib481de92007-09-25 17:54:57 -07001999
2000 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002001 IWL_DEBUG_INFO(priv,
2002 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002003
2004 return rc;
2005}
2006
2007
2008/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002009 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002010 * using sample data 100 bytes apart. If these sample points are good,
2011 * it's a pretty good bet that everything between them is good, too.
2012 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002013static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002014{
2015 u32 val;
2016 int rc = 0;
2017 u32 errcnt = 0;
2018 u32 i;
2019
Tomas Winklere1623442009-01-27 14:27:56 -08002020 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002021
Zhu Yib481de92007-09-25 17:54:57 -07002022 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2023 /* read data comes through single port, auto-incr addr */
2024 /* NOTE: Use the debugless read so we don't flood kernel log
2025 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002026 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002027 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002028 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002029 if (val != le32_to_cpu(*image)) {
2030#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002031 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002032 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2033 i, val, *image);
2034#endif
2035 rc = -EIO;
2036 errcnt++;
2037 if (errcnt >= 3)
2038 break;
2039 }
2040 }
2041
Zhu Yib481de92007-09-25 17:54:57 -07002042 return rc;
2043}
2044
2045
2046/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002047 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002048 * and verify its contents
2049 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002050static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002051{
2052 __le32 *image;
2053 u32 len;
2054 int rc = 0;
2055
2056 /* Try bootstrap */
2057 image = (__le32 *)priv->ucode_boot.v_addr;
2058 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002059 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002060 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002061 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002062 return 0;
2063 }
2064
2065 /* Try initialize */
2066 image = (__le32 *)priv->ucode_init.v_addr;
2067 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002068 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002069 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002070 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002071 return 0;
2072 }
2073
2074 /* Try runtime/protocol */
2075 image = (__le32 *)priv->ucode_code.v_addr;
2076 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002077 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002078 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002079 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002080 return 0;
2081 }
2082
Winkler, Tomas15b16872008-12-19 10:37:33 +08002083 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002084
Ben Cahill9fbab512007-11-29 11:09:47 +08002085 /* Since nothing seems to match, show first several data entries in
2086 * instruction SRAM, so maybe visual inspection will give a clue.
2087 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002088 image = (__le32 *)priv->ucode_boot.v_addr;
2089 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002090 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002091
2092 return rc;
2093}
2094
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002095static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002096{
2097 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002098 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002099}
2100
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002101#define IWL3945_UCODE_GET(item) \
2102static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
2103{ \
2104 return le32_to_cpu(ucode->u.v1.item); \
2105}
2106
2107static u32 iwl3945_ucode_get_header_size(u32 api_ver)
2108{
Johannes Berg22adba22010-04-28 12:09:14 -07002109 return 24;
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002110}
2111
2112static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
2113{
2114 return (u8 *) ucode->u.v1.data;
2115}
2116
2117IWL3945_UCODE_GET(inst_size);
2118IWL3945_UCODE_GET(data_size);
2119IWL3945_UCODE_GET(init_size);
2120IWL3945_UCODE_GET(init_data_size);
2121IWL3945_UCODE_GET(boot_size);
2122
Zhu Yib481de92007-09-25 17:54:57 -07002123/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002124 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002125 *
2126 * Copy into buffers for card to fetch via bus-mastering
2127 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002128static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002129{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002130 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002131 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002132 const struct firmware *ucode_raw;
2133 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002134 const char *name_pre = priv->cfg->fw_name_pre;
2135 const unsigned int api_max = priv->cfg->ucode_api_max;
2136 const unsigned int api_min = priv->cfg->ucode_api_min;
2137 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002138 u8 *src;
2139 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002140 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002141
2142 /* Ask kernel firmware_class module to get the boot firmware off disk.
2143 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002144 for (index = api_max; index >= api_min; index--) {
2145 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2146 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2147 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002148 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002149 buf, ret);
2150 if (ret == -ENOENT)
2151 continue;
2152 else
2153 goto error;
2154 } else {
2155 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002156 IWL_ERR(priv, "Loaded firmware %s, "
2157 "which is deprecated. "
2158 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002159 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002160 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2161 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002162 buf, ucode_raw->size);
2163 break;
2164 }
Zhu Yib481de92007-09-25 17:54:57 -07002165 }
2166
Reinette Chatrea0987a82008-12-02 12:14:06 -08002167 if (ret < 0)
2168 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002169
2170 /* Make sure that we got at least our header! */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002171 if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002172 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002173 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002174 goto err_release;
2175 }
2176
2177 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002178 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002179
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002180 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002181 api_ver = IWL_UCODE_API(priv->ucode_ver);
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002182 inst_size = iwl3945_ucode_get_inst_size(ucode);
2183 data_size = iwl3945_ucode_get_data_size(ucode);
2184 init_size = iwl3945_ucode_get_init_size(ucode);
2185 init_data_size = iwl3945_ucode_get_init_data_size(ucode);
2186 boot_size = iwl3945_ucode_get_boot_size(ucode);
2187 src = iwl3945_ucode_get_data(ucode);
Zhu Yib481de92007-09-25 17:54:57 -07002188
Reinette Chatrea0987a82008-12-02 12:14:06 -08002189 /* api_ver should match the api version forming part of the
2190 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002191 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002192
2193 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002194 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002195 "Driver supports v%u, firmware is v%u.\n",
2196 api_max, api_ver);
2197 priv->ucode_ver = 0;
2198 ret = -EINVAL;
2199 goto err_release;
2200 }
2201 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002202 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002203 "got %u. New firmware can be obtained "
2204 "from http://www.intellinuxwireless.org.\n",
2205 api_max, api_ver);
2206
Tomas Winkler978785a2008-12-19 10:37:31 +08002207 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2208 IWL_UCODE_MAJOR(priv->ucode_ver),
2209 IWL_UCODE_MINOR(priv->ucode_ver),
2210 IWL_UCODE_API(priv->ucode_ver),
2211 IWL_UCODE_SERIAL(priv->ucode_ver));
2212
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07002213 snprintf(priv->hw->wiphy->fw_version,
2214 sizeof(priv->hw->wiphy->fw_version),
2215 "%u.%u.%u.%u",
2216 IWL_UCODE_MAJOR(priv->ucode_ver),
2217 IWL_UCODE_MINOR(priv->ucode_ver),
2218 IWL_UCODE_API(priv->ucode_ver),
2219 IWL_UCODE_SERIAL(priv->ucode_ver));
2220
Tomas Winklere1623442009-01-27 14:27:56 -08002221 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002222 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002223 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2224 inst_size);
2225 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2226 data_size);
2227 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2228 init_size);
2229 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2230 init_data_size);
2231 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2232 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002233
Reinette Chatrea0987a82008-12-02 12:14:06 -08002234
Zhu Yib481de92007-09-25 17:54:57 -07002235 /* Verify size of file vs. image size info in file's header */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002236 if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002237 inst_size + data_size + init_size +
2238 init_data_size + boot_size) {
2239
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002240 IWL_DEBUG_INFO(priv,
2241 "uCode file size %zd does not match expected size\n",
2242 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002243 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002244 goto err_release;
2245 }
2246
2247 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002248 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002249 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002250 inst_size);
2251 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002252 goto err_release;
2253 }
2254
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002255 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002256 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002257 data_size);
2258 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002259 goto err_release;
2260 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002261 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002262 IWL_DEBUG_INFO(priv,
2263 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002264 init_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_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002269 IWL_DEBUG_INFO(priv,
2270 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002271 init_data_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 (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002276 IWL_DEBUG_INFO(priv,
2277 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002278 boot_size);
2279 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002280 goto err_release;
2281 }
2282
2283 /* Allocate ucode buffers for card's bus-master loading ... */
2284
2285 /* Runtime instructions and 2 copies of data:
2286 * 1) unmodified from disk
2287 * 2) backup cache for save/restore during power-downs */
2288 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002289 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002290
2291 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002292 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002293
2294 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002295 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002296
2297 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002298 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002299 goto err_pci_alloc;
2300
Tomas Winkler90e759d2007-11-29 11:09:41 +08002301 /* Initialization instructions and data */
2302 if (init_size && init_data_size) {
2303 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002304 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002305
2306 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002307 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002308
2309 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2310 goto err_pci_alloc;
2311 }
2312
2313 /* Bootstrap (instructions only, no data) */
2314 if (boot_size) {
2315 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002316 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002317
2318 if (!priv->ucode_boot.v_addr)
2319 goto err_pci_alloc;
2320 }
2321
Zhu Yib481de92007-09-25 17:54:57 -07002322 /* Copy images into buffers for card's bus-master reads ... */
2323
2324 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002325 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002326 IWL_DEBUG_INFO(priv,
2327 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002328 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002329 src += len;
2330
Tomas Winklere1623442009-01-27 14:27:56 -08002331 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002332 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2333
2334 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002335 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002336 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002337 IWL_DEBUG_INFO(priv,
2338 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002339 memcpy(priv->ucode_data.v_addr, src, len);
2340 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002341 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002342
2343 /* Initialization instructions (3rd block) */
2344 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002345 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002346 IWL_DEBUG_INFO(priv,
2347 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002348 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002349 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002350 }
2351
2352 /* Initialization data (4th block) */
2353 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002354 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002355 IWL_DEBUG_INFO(priv,
2356 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002357 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002358 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002359 }
2360
2361 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002362 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002363 IWL_DEBUG_INFO(priv,
2364 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002365 memcpy(priv->ucode_boot.v_addr, src, len);
2366
2367 /* We have our copies now, allow OS release its copies */
2368 release_firmware(ucode_raw);
2369 return 0;
2370
2371 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002372 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002373 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002374 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002375
2376 err_release:
2377 release_firmware(ucode_raw);
2378
2379 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002380 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002381}
2382
2383
2384/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002385 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002386 *
2387 * Tell initialization uCode where to find runtime uCode.
2388 *
2389 * BSM registers initially contain pointers to initialization uCode.
2390 * We need to replace them to load runtime uCode inst and data,
2391 * and to save runtime data when powering down.
2392 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002393static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002394{
2395 dma_addr_t pinst;
2396 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002397
2398 /* bits 31:0 for 3945 */
2399 pinst = priv->ucode_code.p_addr;
2400 pdata = priv->ucode_data_backup.p_addr;
2401
Zhu Yib481de92007-09-25 17:54:57 -07002402 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002403 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2404 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2405 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002406 priv->ucode_data.len);
2407
Tomas Winklera96a27f2008-10-23 23:48:56 -07002408 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002409 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002410 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002411 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2412
Tomas Winklere1623442009-01-27 14:27:56 -08002413 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002414
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002415 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002416}
2417
2418/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002419 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002420 *
2421 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2422 *
Zhu Yib481de92007-09-25 17:54:57 -07002423 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002424 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002425static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002426{
2427 /* Check alive response for "valid" sign from uCode */
2428 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2429 /* We had an error bringing up the hardware, so take it
2430 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002431 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002432 goto restart;
2433 }
2434
2435 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2436 * This is a paranoid check, because we would not have gotten the
2437 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002438 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002439 /* Runtime instruction load was bad;
2440 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002441 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002442 goto restart;
2443 }
2444
2445 /* Send pointers to protocol/runtime uCode image ... init code will
2446 * load and launch runtime uCode, which will send us another "Alive"
2447 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002448 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002449 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002450 /* Runtime instruction load won't happen;
2451 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002452 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002453 goto restart;
2454 }
2455 return;
2456
2457 restart:
2458 queue_work(priv->workqueue, &priv->restart);
2459}
2460
Zhu Yib481de92007-09-25 17:54:57 -07002461/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002462 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002463 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002464 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002465 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002466static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002467{
Zhu Yib481de92007-09-25 17:54:57 -07002468 int thermal_spin = 0;
2469 u32 rfkill;
Johannes Berg246ed352010-08-23 10:46:32 +02002470 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07002471
Tomas Winklere1623442009-01-27 14:27:56 -08002472 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002473
2474 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2475 /* We had an error bringing up the hardware, so take it
2476 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002477 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002478 goto restart;
2479 }
2480
2481 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2482 * This is a paranoid check, because we would not have gotten the
2483 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002484 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002485 /* Runtime instruction load was bad;
2486 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002487 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002488 goto restart;
2489 }
2490
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002491 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002492 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002493
2494 if (rfkill & 0x1) {
2495 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002496 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002497 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002498 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002499 thermal_spin++;
2500 udelay(10);
2501 }
2502
2503 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002504 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002505 thermal_spin * 10);
2506 } else
2507 set_bit(STATUS_RF_KILL_HW, &priv->status);
2508
Ben Cahill9fbab512007-11-29 11:09:47 +08002509 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002510 set_bit(STATUS_ALIVE, &priv->status);
2511
Stanislaw Gruszka22de94d2010-12-03 15:41:48 +01002512 /* Enable watchdog to monitor the driver tx queues */
2513 iwl_setup_watchdog(priv);
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08002514
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002515 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002516 return;
2517
Johannes Berg36d68252008-05-15 12:55:26 +02002518 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002519
Johannes Berg470ab2d2010-01-21 11:23:30 -08002520 priv->active_rate = IWL_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07002521
Ben Cahill4d6ccbf2009-11-13 11:56:29 -08002522 iwl_power_update_mode(priv, true);
Zhu Yib481de92007-09-25 17:54:57 -07002523
Johannes Berg246ed352010-08-23 10:46:32 +02002524 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002525 struct iwl3945_rxon_cmd *active_rxon =
Johannes Berg246ed352010-08-23 10:46:32 +02002526 (struct iwl3945_rxon_cmd *)(&ctx->active);
Zhu Yib481de92007-09-25 17:54:57 -07002527
Johannes Berg246ed352010-08-23 10:46:32 +02002528 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002529 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2530 } else {
2531 /* Initialize our rx_config data */
Johannes Bergd0fe4782010-08-23 10:46:58 +02002532 iwl_connection_init_rx_config(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07002533 }
2534
Ben Cahill9fbab512007-11-29 11:09:47 +08002535 /* Configure Bluetooth device coexistence support */
Johannes Berg65b52bd2010-04-13 01:04:31 -07002536 priv->cfg->ops->hcmd->send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002537
2538 /* Configure the adapter for unassociated operation */
Johannes Berg8289e072010-09-22 18:01:57 +02002539 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07002540
Zhu Yib481de92007-09-25 17:54:57 -07002541 iwl3945_reg_txpower_periodic(priv);
2542
Johannes Berge932a602009-10-02 13:44:03 -07002543 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002544
Tomas Winklere1623442009-01-27 14:27:56 -08002545 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002546 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002547 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002548
Zhu Yib481de92007-09-25 17:54:57 -07002549 return;
2550
2551 restart:
2552 queue_work(priv->workqueue, &priv->restart);
2553}
2554
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002555static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002556
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002557static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002558{
2559 unsigned long flags;
Stanislaw Gruszkad745d472010-09-13 14:46:35 +02002560 int exit_pending;
Zhu Yib481de92007-09-25 17:54:57 -07002561
Tomas Winklere1623442009-01-27 14:27:56 -08002562 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002563
Stanislaw Gruszkad745d472010-09-13 14:46:35 +02002564 iwl_scan_cancel_timeout(priv, 200);
2565
2566 exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002567
Stanislaw Gruszkab62177a2010-08-20 15:23:47 +02002568 /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
2569 * to prevent rearm timer */
Stanislaw Gruszka22de94d2010-12-03 15:41:48 +01002570 del_timer_sync(&priv->watchdog);
Stanislaw Gruszkab62177a2010-08-20 15:23:47 +02002571
Reinette Chatre7e246192010-02-18 22:58:32 -08002572 /* Station information will now be cleared in device */
Johannes Bergdcef7322010-08-27 08:55:52 -07002573 iwl_clear_ucode_stations(priv, NULL);
Johannes Berga194e322010-08-27 08:53:46 -07002574 iwl_dealloc_bcast_stations(priv);
Johannes Bergdb125c72010-05-07 01:49:15 -07002575 iwl_clear_driver_stations(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002576
2577 /* Unblock any waiting calls */
2578 wake_up_interruptible_all(&priv->wait_command_queue);
2579
Zhu Yib481de92007-09-25 17:54:57 -07002580 /* Wipe out the EXIT_PENDING status bit if we are not actually
2581 * exiting the module */
2582 if (!exit_pending)
2583 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2584
2585 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002586 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002587
2588 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002589 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002590 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002591 spin_unlock_irqrestore(&priv->lock, flags);
2592 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002593
2594 if (priv->mac80211_registered)
2595 ieee80211_stop_queues(priv->hw);
2596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002597 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002598 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002599 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002600 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2601 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002602 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2603 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002604 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2605 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002606 goto exit;
2607 }
2608
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002609 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002610 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002611 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2612 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002613 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2614 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002615 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002616 STATUS_FW_ERROR |
2617 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2618 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002619
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002620 iwl3945_hw_txq_ctx_stop(priv);
2621 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002622
Ben Cahill309e7312009-11-06 14:53:03 -08002623 /* Power-down device's busmaster DMA clocks */
2624 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002625 udelay(5);
2626
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002627 /* Stop the device, and put it in low power state */
Johannes Berg14e8e4a2010-09-22 18:02:10 +02002628 iwl_apm_stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002629
Zhu Yib481de92007-09-25 17:54:57 -07002630 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002631 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002632
Johannes Berg12e934d2010-10-04 05:50:06 -07002633 if (priv->beacon_skb)
2634 dev_kfree_skb(priv->beacon_skb);
2635 priv->beacon_skb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002636
2637 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002638 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002639}
2640
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002641static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002642{
2643 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002644 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002645 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002646
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002647 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002648}
2649
2650#define MAX_HW_RESTARTS 5
2651
Johannes Berga30e3112010-09-22 18:02:01 +02002652static int iwl3945_alloc_bcast_station(struct iwl_priv *priv)
2653{
2654 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2655 unsigned long flags;
2656 u8 sta_id;
2657
2658 spin_lock_irqsave(&priv->sta_lock, flags);
2659 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
2660 if (sta_id == IWL_INVALID_STATION) {
2661 IWL_ERR(priv, "Unable to prepare broadcast station\n");
2662 spin_unlock_irqrestore(&priv->sta_lock, flags);
2663
2664 return -EINVAL;
2665 }
2666
2667 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
2668 priv->stations[sta_id].used |= IWL_STA_BCAST;
2669 spin_unlock_irqrestore(&priv->sta_lock, flags);
2670
2671 return 0;
2672}
2673
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002674static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002675{
2676 int rc, i;
2677
Johannes Berga30e3112010-09-22 18:02:01 +02002678 rc = iwl3945_alloc_bcast_station(priv);
Johannes Berg2c810cc2010-04-29 00:53:29 -07002679 if (rc)
2680 return rc;
2681
Zhu Yib481de92007-09-25 17:54:57 -07002682 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002683 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002684 return -EIO;
2685 }
2686
Reinette Chatree903fbd2008-01-30 22:05:15 -08002687 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002688 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002689 return -EIO;
2690 }
2691
Zhu Yie655b9f2008-01-24 02:19:38 -08002692 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002693 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002694 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2695 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2696 else {
2697 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002698 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2699 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002700 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002701
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002702 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002703
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002704 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002705 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002706 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002707 return rc;
2708 }
2709
2710 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002711 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2712 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002713 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2714
2715 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002716 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002717 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002718
2719 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002720 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2721 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002722
2723 /* Copy original ucode data image from disk into backup cache.
2724 * This will be used to initialize the on-board processor's
2725 * data SRAM for a clean start when the runtime program first loads. */
2726 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002727 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002728
Zhu Yie655b9f2008-01-24 02:19:38 -08002729 /* We return success when we resume from suspend and rf_kill is on. */
2730 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2731 return 0;
2732
Zhu Yib481de92007-09-25 17:54:57 -07002733 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2734
Zhu Yib481de92007-09-25 17:54:57 -07002735 /* load bootstrap state machine,
2736 * load bootstrap program into processor's memory,
2737 * prepare to load the "initialize" uCode */
Abhijeet Kolekar75a9a922010-02-26 15:17:01 -08002738 rc = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002739
2740 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002741 IWL_ERR(priv,
2742 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002743 continue;
2744 }
2745
2746 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002747 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002748
Tomas Winklere1623442009-01-27 14:27:56 -08002749 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002750
2751 return 0;
2752 }
2753
2754 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002755 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002756 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002757
2758 /* tried to restart and config the device for as long as our
2759 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002760 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002761 return -EIO;
2762}
2763
2764
2765/*****************************************************************************
2766 *
2767 * Workqueue callbacks
2768 *
2769 *****************************************************************************/
2770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002771static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002772{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002773 struct iwl_priv *priv =
2774 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002775
2776 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2777 return;
2778
2779 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002780 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002781 mutex_unlock(&priv->mutex);
2782}
2783
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002784static void iwl3945_bg_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, 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_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002794 mutex_unlock(&priv->mutex);
2795}
2796
Ben Cahill743cdf12009-10-09 13:20:26 -07002797/*
2798 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2799 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2800 * *is* readable even when device has been SW_RESET into low power mode
2801 * (e.g. during RF KILL).
2802 */
Helmut Schaa26635162009-01-15 09:38:44 +01002803static void iwl3945_rfkill_poll(struct work_struct *data)
2804{
2805 struct iwl_priv *priv =
Johannes Bergee525d12010-01-21 06:09:28 -08002806 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002807 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2808 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2809 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002810
Ben Cahill743cdf12009-10-09 13:20:26 -07002811 if (new_rfkill != old_rfkill) {
2812 if (new_rfkill)
2813 set_bit(STATUS_RF_KILL_HW, &priv->status);
2814 else
2815 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002816
Ben Cahill743cdf12009-10-09 13:20:26 -07002817 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002818
Ben Cahill743cdf12009-10-09 13:20:26 -07002819 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2820 new_rfkill ? "disable radio" : "enable radio");
2821 }
2822
2823 /* Keep this running, even if radio now enabled. This will be
2824 * cancelled in mac_start() if system decides to start again */
Johannes Bergee525d12010-01-21 06:09:28 -08002825 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01002826 round_jiffies_relative(2 * HZ));
2827
2828}
2829
Johannes Berg3eecce52010-09-13 14:46:33 +02002830int iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07002831{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002832 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002833 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002834 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002835 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002836 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002837 struct iwl3945_scan_cmd *scan;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002838 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002839 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002840 bool is_active = false;
Johannes Berg3eecce52010-09-13 14:46:33 +02002841 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002842
Johannes Berg3eecce52010-09-13 14:46:33 +02002843 lockdep_assert_held(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07002844
Johannes Berg811ecc92010-04-06 04:12:41 -07002845 if (!priv->scan_cmd) {
2846 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2847 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2848 if (!priv->scan_cmd) {
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002849 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02002850 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07002851 }
2852 }
Johannes Berg811ecc92010-04-06 04:12:41 -07002853 scan = priv->scan_cmd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002854 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002855
2856 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2857 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2858
Johannes Berg246ed352010-08-23 10:46:32 +02002859 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Zhu Yib481de92007-09-25 17:54:57 -07002860 u16 interval = 0;
2861 u32 extra;
2862 u32 suspend_time = 100;
2863 u32 scan_suspend_time = 100;
2864 unsigned long flags;
2865
Tomas Winklere1623442009-01-27 14:27:56 -08002866 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002867
2868 spin_lock_irqsave(&priv->lock, flags);
John W. Linvillea6e492b2010-07-22 15:24:56 -04002869 if (priv->is_internal_short_scan)
2870 interval = 0;
2871 else
2872 interval = vif->bss_conf.beacon_int;
Zhu Yib481de92007-09-25 17:54:57 -07002873 spin_unlock_irqrestore(&priv->lock, flags);
2874
2875 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002876 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002877 if (!interval)
2878 interval = suspend_time;
2879 /*
2880 * suspend time format:
2881 * 0-19: beacon interval in usec (time before exec.)
2882 * 20-23: 0
2883 * 24-31: number of beacons (suspend between channels)
2884 */
2885
2886 extra = (suspend_time / interval) << 24;
2887 scan_suspend_time = 0xFF0FFFFF &
2888 (extra | ((suspend_time % interval) * 1024));
2889
2890 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002891 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002892 scan_suspend_time, interval);
2893 }
2894
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002895 if (priv->is_internal_short_scan) {
2896 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2897 } else if (priv->scan_request->n_ssids) {
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002898 int i, p = 0;
2899 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2900 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2901 /* always does wildcard anyway */
2902 if (!priv->scan_request->ssids[i].ssid_len)
2903 continue;
2904 scan->direct_scan[p].id = WLAN_EID_SSID;
2905 scan->direct_scan[p].len =
2906 priv->scan_request->ssids[i].ssid_len;
2907 memcpy(scan->direct_scan[p].ssid,
2908 priv->scan_request->ssids[i].ssid,
2909 priv->scan_request->ssids[i].ssid_len);
2910 n_probes++;
2911 p++;
2912 }
2913 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002914 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002915 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002916
2917 /* We don't build a direct scan probe request; the uCode will do
2918 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002919 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Johannes Berga194e322010-08-27 08:53:46 -07002920 scan->tx_cmd.sta_id = priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002921 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2922
2923 /* flags + rate selection */
2924
Johannes Berg00700ee2010-04-06 04:12:37 -07002925 switch (priv->scan_band) {
2926 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002927 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2928 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
Johannes Berg8318d782008-01-24 19:38:38 +01002929 band = IEEE80211_BAND_2GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002930 break;
2931 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002932 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Berg8318d782008-01-24 19:38:38 +01002933 band = IEEE80211_BAND_5GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002934 break;
2935 default:
2936 IWL_WARN(priv, "Invalid scan band\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02002937 return -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07002938 }
2939
Johannes Berg085fbca2010-10-04 05:47:23 -07002940 /*
2941 * If active scaning is requested but a certain channel
2942 * is marked passive, we can do active scanning if we
2943 * detect transmissions.
2944 */
2945 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2946 IWL_GOOD_CRC_TH_DISABLED;
2947
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002948 if (!priv->is_internal_short_scan) {
2949 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002950 iwl_fill_probe_req(priv,
2951 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002952 vif->addr,
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002953 priv->scan_request->ie,
2954 priv->scan_request->ie_len,
2955 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002956 } else {
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002957 /* use bcast addr, will not be transmitted but must be valid */
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002958 scan->tx_cmd.len = cpu_to_le16(
2959 iwl_fill_probe_req(priv,
2960 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002961 iwl_bcast_addr, NULL, 0,
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002962 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2963 }
Zhu Yib481de92007-09-25 17:54:57 -07002964 /* select Rx antennas */
2965 scan->flags |= iwl3945_get_antenna_flags(priv);
2966
Abhijeet Kolekar14023642010-06-02 21:15:10 -07002967 if (priv->is_internal_short_scan) {
2968 scan->channel_count =
2969 iwl3945_get_single_channel_for_scan(priv, vif, band,
2970 (void *)&scan->data[le16_to_cpu(
2971 scan->tx_cmd.len)]);
2972 } else {
2973 scan->channel_count =
2974 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
2975 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
2976 }
Zhu Yib481de92007-09-25 17:54:57 -07002977
Reinette Chatre14b54332008-11-04 12:21:35 -08002978 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002979 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Johannes Berg3eecce52010-09-13 14:46:33 +02002980 return -EIO;
Reinette Chatre14b54332008-11-04 12:21:35 -08002981 }
2982
Zhu Yib481de92007-09-25 17:54:57 -07002983 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002984 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07002985 cmd.data = scan;
2986 scan->len = cpu_to_le16(cmd.len);
2987
2988 set_bit(STATUS_SCAN_HW, &priv->status);
Johannes Berg3eecce52010-09-13 14:46:33 +02002989 ret = iwl_send_cmd_sync(priv, &cmd);
2990 if (ret)
2991 clear_bit(STATUS_SCAN_HW, &priv->status);
2992 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002993}
2994
Johannes Berga77029e2010-09-22 18:01:56 +02002995void iwl3945_post_scan(struct iwl_priv *priv)
2996{
2997 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2998
2999 /*
3000 * Since setting the RXON may have been deferred while
3001 * performing the scan, fire one off if needed
3002 */
3003 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
Johannes Berg8289e072010-09-22 18:01:57 +02003004 iwl3945_commit_rxon(priv, ctx);
Johannes Berga77029e2010-09-22 18:01:56 +02003005}
3006
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003007static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003008{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003009 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003010
3011 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3012 return;
3013
Johannes Berg19cc1082009-05-08 13:44:36 -07003014 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
Johannes Berg8bd413e2010-08-23 10:46:40 +02003015 struct iwl_rxon_context *ctx;
Johannes Berg19cc1082009-05-08 13:44:36 -07003016 mutex_lock(&priv->mutex);
Johannes Berg8bd413e2010-08-23 10:46:40 +02003017 for_each_context(priv, ctx)
3018 ctx->vif = NULL;
Johannes Berg19cc1082009-05-08 13:44:36 -07003019 priv->is_open = 0;
3020 mutex_unlock(&priv->mutex);
3021 iwl3945_down(priv);
3022 ieee80211_restart_hw(priv->hw);
3023 } else {
3024 iwl3945_down(priv);
Johannes Berg80676512010-01-21 06:07:17 -08003025
3026 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3027 return;
3028
3029 mutex_lock(&priv->mutex);
3030 __iwl3945_up(priv);
3031 mutex_unlock(&priv->mutex);
Johannes Berg19cc1082009-05-08 13:44:36 -07003032 }
Zhu Yib481de92007-09-25 17:54:57 -07003033}
3034
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003035static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003036{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003037 struct iwl_priv *priv =
3038 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003039
3040 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3041 return;
3042
3043 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003044 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003045 mutex_unlock(&priv->mutex);
3046}
3047
Johannes Berg2295c662010-10-23 09:15:41 -07003048void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003049{
Zhu Yib481de92007-09-25 17:54:57 -07003050 int rc = 0;
3051 struct ieee80211_conf *conf = NULL;
Johannes Berg246ed352010-08-23 10:46:32 +02003052 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003053
Johannes Berg2295c662010-10-23 09:15:41 -07003054 if (!ctx->vif || !priv->is_open)
Johannes Berg1dda6d22010-04-29 04:43:06 -07003055 return;
3056
Johannes Berg2295c662010-10-23 09:15:41 -07003057 if (ctx->vif->type == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003058 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003059 return;
3060 }
3061
Tomas Winklere1623442009-01-27 14:27:56 -08003062 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003063 ctx->vif->bss_conf.aid, ctx->active.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003064
3065 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3066 return;
3067
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003068 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003069
Zhu Yib481de92007-09-25 17:54:57 -07003070 conf = ieee80211_get_hw_conf(priv->hw);
3071
Johannes Berg246ed352010-08-23 10:46:32 +02003072 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003073 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003074
Johannes Berg47313e32010-08-23 10:46:55 +02003075 rc = iwl_send_rxon_timing(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003076 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003077 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003078 "Attempting to continue.\n");
3079
Johannes Berg246ed352010-08-23 10:46:32 +02003080 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003081
Johannes Berg2295c662010-10-23 09:15:41 -07003082 ctx->staging.assoc_id = cpu_to_le16(ctx->vif->bss_conf.aid);
Zhu Yib481de92007-09-25 17:54:57 -07003083
Tomas Winklere1623442009-01-27 14:27:56 -08003084 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003085 ctx->vif->bss_conf.aid, ctx->vif->bss_conf.beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07003086
Johannes Berg2295c662010-10-23 09:15:41 -07003087 if (ctx->vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003088 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003089 else
Johannes Berg246ed352010-08-23 10:46:32 +02003090 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003091
Johannes Berg246ed352010-08-23 10:46:32 +02003092 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Berg2295c662010-10-23 09:15:41 -07003093 if (ctx->vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003094 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003095 else
Johannes Berg246ed352010-08-23 10:46:32 +02003096 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003097 }
3098
Johannes Berg8289e072010-09-22 18:01:57 +02003099 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003100
Johannes Berg2295c662010-10-23 09:15:41 -07003101 switch (ctx->vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003102 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003103 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003104 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02003105 case NL80211_IFTYPE_ADHOC:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003106 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003107 break;
Zhu Yib481de92007-09-25 17:54:57 -07003108 default:
Johannes Berg1dda6d22010-04-29 04:43:06 -07003109 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003110 __func__, ctx->vif->type);
Zhu Yib481de92007-09-25 17:54:57 -07003111 break;
3112 }
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003113}
3114
Zhu Yib481de92007-09-25 17:54:57 -07003115/*****************************************************************************
3116 *
3117 * mac80211 entry point functions
3118 *
3119 *****************************************************************************/
3120
Zhu Yi5a669262008-01-14 17:46:18 -08003121#define UCODE_READY_TIMEOUT (2 * HZ)
3122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003123static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003124{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003125 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08003126 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003127
Tomas Winklere1623442009-01-27 14:27:56 -08003128 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003129
3130 /* we should be verifying the device is ready to be opened */
3131 mutex_lock(&priv->mutex);
3132
Zhu Yi5a669262008-01-14 17:46:18 -08003133 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3134 * ucode filename and max sizes are card-specific. */
3135
3136 if (!priv->ucode_code.len) {
3137 ret = iwl3945_read_ucode(priv);
3138 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003139 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08003140 mutex_unlock(&priv->mutex);
3141 goto out_release_irq;
3142 }
3143 }
3144
Zhu Yie655b9f2008-01-24 02:19:38 -08003145 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08003146
Zhu Yib481de92007-09-25 17:54:57 -07003147 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08003148
Zhu Yie655b9f2008-01-24 02:19:38 -08003149 if (ret)
3150 goto out_release_irq;
3151
Tomas Winklere1623442009-01-27 14:27:56 -08003152 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003153
Zhu Yi5a669262008-01-14 17:46:18 -08003154 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3155 * mac80211 will not be run successfully. */
3156 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3157 test_bit(STATUS_READY, &priv->status),
3158 UCODE_READY_TIMEOUT);
3159 if (!ret) {
3160 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003161 IWL_ERR(priv,
3162 "Wait for START_ALIVE timeout after %dms.\n",
3163 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08003164 ret = -ETIMEDOUT;
3165 goto out_release_irq;
3166 }
3167 }
3168
Helmut Schaa26635162009-01-15 09:38:44 +01003169 /* ucode is running and will send rfkill notifications,
3170 * no need to poll the killswitch state anymore */
Johannes Bergee525d12010-01-21 06:09:28 -08003171 cancel_delayed_work(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01003172
Johannes Berge932a602009-10-02 13:44:03 -07003173 iwl_led_start(priv);
3174
Zhu Yie655b9f2008-01-24 02:19:38 -08003175 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003176 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003177 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003178
3179out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003180 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003181 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08003182 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003183}
3184
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003185static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003186{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003187 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003188
Tomas Winklere1623442009-01-27 14:27:56 -08003189 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003190
Zhu Yie655b9f2008-01-24 02:19:38 -08003191 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003192 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003193 return;
3194 }
3195
Zhu Yib481de92007-09-25 17:54:57 -07003196 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003197
Zhu Yi5a669262008-01-14 17:46:18 -08003198 iwl3945_down(priv);
3199
3200 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003201
3202 /* start polling the killswitch state again */
Johannes Bergee525d12010-01-21 06:09:28 -08003203 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01003204 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003205
Tomas Winklere1623442009-01-27 14:27:56 -08003206 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003207}
3208
Johannes Berge039fa42008-05-15 12:55:29 +02003209static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003210{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003211 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003212
Tomas Winklere1623442009-01-27 14:27:56 -08003213 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003214
Tomas Winklere1623442009-01-27 14:27:56 -08003215 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003216 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003217
Johannes Berge039fa42008-05-15 12:55:29 +02003218 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003219 dev_kfree_skb_any(skb);
3220
Tomas Winklere1623442009-01-27 14:27:56 -08003221 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003222 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003223}
3224
Johannes Berg2295c662010-10-23 09:15:41 -07003225void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003226{
Johannes Berg246ed352010-08-23 10:46:32 +02003227 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg2295c662010-10-23 09:15:41 -07003228 struct ieee80211_vif *vif = ctx->vif;
Zhu Yib481de92007-09-25 17:54:57 -07003229 int rc = 0;
3230
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003231 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003232 return;
3233
3234 /* The following should be done only at AP bring up */
Johannes Berg246ed352010-08-23 10:46:32 +02003235 if (!(iwl_is_associated(priv, IWL_RXON_CTX_BSS))) {
Zhu Yib481de92007-09-25 17:54:57 -07003236
3237 /* RXON - unassoc (to set timing command) */
Johannes Berg246ed352010-08-23 10:46:32 +02003238 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003239 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003240
3241 /* RXON Timing */
Johannes Berg47313e32010-08-23 10:46:55 +02003242 rc = iwl_send_rxon_timing(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003243 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003244 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003245 "Attempting to continue.\n");
3246
Johannes Berg246ed352010-08-23 10:46:32 +02003247 ctx->staging.assoc_id = 0;
Johannes Berg1dda6d22010-04-29 04:43:06 -07003248
Johannes Bergc213d742010-05-06 12:21:40 -07003249 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003250 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003251 RXON_FLG_SHORT_PREAMBLE_MSK;
3252 else
Johannes Berg246ed352010-08-23 10:46:32 +02003253 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003254 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3255
Johannes Berg246ed352010-08-23 10:46:32 +02003256 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003257 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003258 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003259 RXON_FLG_SHORT_SLOT_MSK;
3260 else
Johannes Berg246ed352010-08-23 10:46:32 +02003261 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003262 ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003263 }
3264 /* restore RXON assoc */
Johannes Berg246ed352010-08-23 10:46:32 +02003265 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003266 iwl3945_commit_rxon(priv, ctx);
Zhu Yi556f8db2007-09-27 11:27:33 +08003267 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003268 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003269
3270 /* FIXME - we need to add code here to detect a totally new
3271 * configuration, reset the AP, unassoc, rxon timing, assoc,
3272 * clear sta table, add BCAST sta... */
3273}
3274
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003275static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003276 struct ieee80211_vif *vif,
3277 struct ieee80211_sta *sta,
3278 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003279{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003280 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003281 int ret = 0;
3282 u8 sta_id = IWL_INVALID_STATION;
3283 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003284
Tomas Winklere1623442009-01-27 14:27:56 -08003285 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003286
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003287 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003288 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003289 return -EOPNOTSUPP;
3290 }
3291
Johannes Berg246ed352010-08-23 10:46:32 +02003292 static_key = !iwl_is_associated(priv, IWL_RXON_CTX_BSS);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003293
3294 if (!static_key) {
Johannes Berga194e322010-08-27 08:53:46 -07003295 sta_id = iwl_sta_id_or_broadcast(
3296 priv, &priv->contexts[IWL_RXON_CTX_BSS], sta);
Johannes Berg0af8bca2010-04-30 14:08:00 -07003297 if (sta_id == IWL_INVALID_STATION)
3298 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003299 }
3300
3301 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003302 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003303
Zhu Yib481de92007-09-25 17:54:57 -07003304 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003305 case SET_KEY:
3306 if (static_key)
3307 ret = iwl3945_set_static_key(priv, key);
3308 else
3309 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3310 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003311 break;
3312 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003313 if (static_key)
3314 ret = iwl3945_remove_static_key(priv);
3315 else
3316 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3317 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003318 break;
3319 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003320 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003321 }
3322
Johannes Berg72e15d72010-02-19 11:42:32 -08003323 mutex_unlock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003324 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003325
Winkler, Tomas42986792009-01-19 15:30:22 -08003326 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003327}
3328
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003329static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3330 struct ieee80211_vif *vif,
3331 struct ieee80211_sta *sta)
3332{
3333 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07003334 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003335 int ret;
Johannes Bergfd1af152010-04-30 11:30:43 -07003336 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003337 u8 sta_id;
3338
3339 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3340 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003341 mutex_lock(&priv->mutex);
3342 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
3343 sta->addr);
3344 sta_priv->common.sta_id = IWL_INVALID_STATION;
3345
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003346
Johannes Berga194e322010-08-27 08:53:46 -07003347 ret = iwl_add_station_common(priv, &priv->contexts[IWL_RXON_CTX_BSS],
Johannes Berg238d7812010-08-23 10:46:45 +02003348 sta->addr, is_ap, sta, &sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003349 if (ret) {
3350 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3351 sta->addr, ret);
3352 /* Should we return success if return code is EEXIST ? */
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003353 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003354 return ret;
3355 }
3356
Johannes Bergfd1af152010-04-30 11:30:43 -07003357 sta_priv->common.sta_id = sta_id;
3358
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003359 /* Initialize rate scaling */
Frans Pop91dd6c22010-03-24 14:19:58 -07003360 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003361 sta->addr);
3362 iwl3945_rs_rate_init(priv, sta, sta_id);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003363 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003364
3365 return 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003366}
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003367
3368static void iwl3945_configure_filter(struct ieee80211_hw *hw,
3369 unsigned int changed_flags,
3370 unsigned int *total_flags,
3371 u64 multicast)
3372{
3373 struct iwl_priv *priv = hw->priv;
3374 __le32 filter_or = 0, filter_nand = 0;
Johannes Berg246ed352010-08-23 10:46:32 +02003375 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003376
3377#define CHK(test, flag) do { \
3378 if (*total_flags & (test)) \
3379 filter_or |= (flag); \
3380 else \
3381 filter_nand |= (flag); \
3382 } while (0)
3383
3384 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
3385 changed_flags, *total_flags);
3386
3387 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
3388 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
3389 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
3390
3391#undef CHK
3392
3393 mutex_lock(&priv->mutex);
3394
Johannes Berg246ed352010-08-23 10:46:32 +02003395 ctx->staging.filter_flags &= ~filter_nand;
3396 ctx->staging.filter_flags |= filter_or;
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003397
3398 /*
Stanislaw Gruszka749ff4e2010-10-22 17:04:27 +02003399 * Not committing directly because hardware can perform a scan,
3400 * but even if hw is ready, committing here breaks for some reason,
3401 * we'll eventually commit the filter flags change anyway.
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003402 */
3403
3404 mutex_unlock(&priv->mutex);
3405
3406 /*
3407 * Receiving all multicast frames is always enabled by the
3408 * default flags setup in iwl_connection_init_rx_config()
3409 * since we currently do not support programming multicast
3410 * filters into the device.
3411 */
3412 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
3413 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
3414}
3415
3416
Zhu Yib481de92007-09-25 17:54:57 -07003417/*****************************************************************************
3418 *
3419 * sysfs attributes
3420 *
3421 *****************************************************************************/
3422
Samuel Ortizd08853a2009-01-23 13:45:17 -08003423#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003424
3425/*
3426 * The following adds a new attribute to the sysfs representation
3427 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3428 * used for controlling the debug level.
3429 *
3430 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003431 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003432 * The debug_level being managed using sysfs below is a per device debug
3433 * level that is used instead of the global debug level if it (the per
3434 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003435 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003436static ssize_t show_debug_level(struct device *d,
3437 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003438{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003439 struct iwl_priv *priv = dev_get_drvdata(d);
3440 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003441}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003442static ssize_t store_debug_level(struct device *d,
3443 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003444 const char *buf, size_t count)
3445{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003446 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003447 unsigned long val;
3448 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003449
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003450 ret = strict_strtoul(buf, 0, &val);
3451 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003452 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003453 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003454 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003455 if (iwl_alloc_traffic_mem(priv))
3456 IWL_ERR(priv,
3457 "Not enough memory to generate traffic log\n");
3458 }
Zhu Yib481de92007-09-25 17:54:57 -07003459 return strnlen(buf, count);
3460}
3461
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003462static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3463 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003464
Samuel Ortizd08853a2009-01-23 13:45:17 -08003465#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003466
Zhu Yib481de92007-09-25 17:54:57 -07003467static ssize_t show_temperature(struct device *d,
3468 struct device_attribute *attr, char *buf)
3469{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003470 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003471
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003472 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003473 return -EAGAIN;
3474
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003475 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003476}
3477
3478static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3479
Zhu Yib481de92007-09-25 17:54:57 -07003480static ssize_t show_tx_power(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);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003484 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003485}
3486
3487static ssize_t store_tx_power(struct device *d,
3488 struct device_attribute *attr,
3489 const char *buf, size_t count)
3490{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003491 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003492 char *p = (char *)buf;
3493 u32 val;
3494
3495 val = simple_strtoul(p, &p, 10);
3496 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003497 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003498 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003499 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003500
3501 return count;
3502}
3503
3504static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3505
3506static ssize_t show_flags(struct device *d,
3507 struct device_attribute *attr, char *buf)
3508{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003509 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003510 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003511
Johannes Berg246ed352010-08-23 10:46:32 +02003512 return sprintf(buf, "0x%04X\n", ctx->active.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003513}
3514
3515static ssize_t store_flags(struct device *d,
3516 struct device_attribute *attr,
3517 const char *buf, size_t count)
3518{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003519 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003520 u32 flags = simple_strtoul(buf, NULL, 0);
Johannes Berg246ed352010-08-23 10:46:32 +02003521 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003522
3523 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003524 if (le32_to_cpu(ctx->staging.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003525 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003526 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003527 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003528 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003529 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003530 flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003531 ctx->staging.flags = cpu_to_le32(flags);
Johannes Berg8289e072010-09-22 18:01:57 +02003532 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003533 }
3534 }
3535 mutex_unlock(&priv->mutex);
3536
3537 return count;
3538}
3539
3540static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3541
3542static ssize_t show_filter_flags(struct device *d,
3543 struct device_attribute *attr, char *buf)
3544{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003545 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003546 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003547
3548 return sprintf(buf, "0x%04X\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003549 le32_to_cpu(ctx->active.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003550}
3551
3552static ssize_t store_filter_flags(struct device *d,
3553 struct device_attribute *attr,
3554 const char *buf, size_t count)
3555{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003556 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003557 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003558 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3559
3560 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003561 if (le32_to_cpu(ctx->staging.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003562 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003563 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003564 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003565 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003566 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003567 "0x%04X\n", filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003568 ctx->staging.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003569 cpu_to_le32(filter_flags);
Johannes Berg8289e072010-09-22 18:01:57 +02003570 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003571 }
3572 }
3573 mutex_unlock(&priv->mutex);
3574
3575 return count;
3576}
3577
3578static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3579 store_filter_flags);
3580
Zhu Yib481de92007-09-25 17:54:57 -07003581static ssize_t show_measurement(struct device *d,
3582 struct device_attribute *attr, char *buf)
3583{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003584 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003585 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003586 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003587 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003588 unsigned long flags;
3589
3590 spin_lock_irqsave(&priv->lock, flags);
3591 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3592 spin_unlock_irqrestore(&priv->lock, flags);
3593 return 0;
3594 }
3595 memcpy(&measure_report, &priv->measure_report, size);
3596 priv->measurement_status = 0;
3597 spin_unlock_irqrestore(&priv->lock, flags);
3598
3599 while (size && (PAGE_SIZE - len)) {
3600 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3601 PAGE_SIZE - len, 1);
3602 len = strlen(buf);
3603 if (PAGE_SIZE - len)
3604 buf[len++] = '\n';
3605
3606 ofs += 16;
3607 size -= min(size, 16U);
3608 }
3609
3610 return len;
3611}
3612
3613static ssize_t store_measurement(struct device *d,
3614 struct device_attribute *attr,
3615 const char *buf, size_t count)
3616{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003617 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003618 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003619 struct ieee80211_measurement_params params = {
Johannes Berg246ed352010-08-23 10:46:32 +02003620 .channel = le16_to_cpu(ctx->active.channel),
Johannes Berge99f1682010-01-19 10:04:28 -08003621 .start_time = cpu_to_le64(priv->_3945.last_tsf),
Zhu Yib481de92007-09-25 17:54:57 -07003622 .duration = cpu_to_le16(1),
3623 };
3624 u8 type = IWL_MEASURE_BASIC;
3625 u8 buffer[32];
3626 u8 channel;
3627
3628 if (count) {
3629 char *p = buffer;
3630 strncpy(buffer, buf, min(sizeof(buffer), count));
3631 channel = simple_strtoul(p, NULL, 0);
3632 if (channel)
3633 params.channel = channel;
3634
3635 p = buffer;
3636 while (*p && *p != ' ')
3637 p++;
3638 if (*p)
3639 type = simple_strtoul(p + 1, NULL, 0);
3640 }
3641
Tomas Winklere1623442009-01-27 14:27:56 -08003642 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003643 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003644 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003645
3646 return count;
3647}
3648
3649static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3650 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003651
Zhu Yib481de92007-09-25 17:54:57 -07003652static ssize_t store_retry_rate(struct device *d,
3653 struct device_attribute *attr,
3654 const char *buf, size_t count)
3655{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003656 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003657
3658 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3659 if (priv->retry_rate <= 0)
3660 priv->retry_rate = 1;
3661
3662 return count;
3663}
3664
3665static ssize_t show_retry_rate(struct device *d,
3666 struct device_attribute *attr, char *buf)
3667{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003668 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003669 return sprintf(buf, "%d", priv->retry_rate);
3670}
3671
3672static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3673 store_retry_rate);
3674
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003675
Zhu Yib481de92007-09-25 17:54:57 -07003676static ssize_t show_channels(struct device *d,
3677 struct device_attribute *attr, char *buf)
3678{
Johannes Berg8318d782008-01-24 19:38:38 +01003679 /* all this shit doesn't belong into sysfs anyway */
3680 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003681}
3682
3683static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3684
Zhu Yib481de92007-09-25 17:54:57 -07003685static ssize_t show_antenna(struct device *d,
3686 struct device_attribute *attr, char *buf)
3687{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003688 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003689
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003690 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003691 return -EAGAIN;
3692
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003693 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003694}
3695
3696static ssize_t store_antenna(struct device *d,
3697 struct device_attribute *attr,
3698 const char *buf, size_t count)
3699{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003700 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003701 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003702
3703 if (count == 0)
3704 return 0;
3705
3706 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003707 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003708 return count;
3709 }
3710
3711 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003712 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003713 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003714 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003715 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003716
3717
3718 return count;
3719}
3720
3721static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3722
3723static ssize_t show_status(struct device *d,
3724 struct device_attribute *attr, char *buf)
3725{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003726 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003727 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003728 return -EAGAIN;
3729 return sprintf(buf, "0x%08x\n", (int)priv->status);
3730}
3731
3732static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3733
3734static ssize_t dump_error_log(struct device *d,
3735 struct device_attribute *attr,
3736 const char *buf, size_t count)
3737{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003738 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003739 char *p = (char *)buf;
3740
3741 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003742 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003743
3744 return strnlen(buf, count);
3745}
3746
3747static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3748
Zhu Yib481de92007-09-25 17:54:57 -07003749/*****************************************************************************
3750 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003751 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003752 *
3753 *****************************************************************************/
3754
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003755static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003756{
Reinette Chatred21050c2009-02-13 11:51:18 -08003757 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003758
3759 init_waitqueue_head(&priv->wait_command_queue);
3760
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003761 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3762 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003763 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003764 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3765 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Johannes Bergee525d12010-01-21 06:09:28 -08003766 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02003767
3768 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003770 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003771
Stanislaw Gruszka22de94d2010-12-03 15:41:48 +01003772 init_timer(&priv->watchdog);
3773 priv->watchdog.data = (unsigned long)priv;
3774 priv->watchdog.function = iwl_bg_watchdog;
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003775
Zhu Yib481de92007-09-25 17:54:57 -07003776 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003777 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003778}
3779
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003780static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003781{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003782 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003783
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003784 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003785 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003786 cancel_work_sync(&priv->beacon_update);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02003787
3788 iwl_cancel_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003789}
3790
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003791static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003792 &dev_attr_antenna.attr,
3793 &dev_attr_channels.attr,
3794 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003795 &dev_attr_flags.attr,
3796 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003797 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003798 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003799 &dev_attr_status.attr,
3800 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003801 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003802#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003803 &dev_attr_debug_level.attr,
3804#endif
Zhu Yib481de92007-09-25 17:54:57 -07003805 NULL
3806};
3807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003809 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003810 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003811};
3812
Johannes Bergdc21b542010-10-23 09:15:39 -07003813struct ieee80211_ops iwl3945_hw_ops = {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003814 .tx = iwl3945_mac_tx,
3815 .start = iwl3945_mac_start,
3816 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003817 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003818 .remove_interface = iwl_mac_remove_interface,
Johannes Bergd4daaea2010-10-23 09:15:43 -07003819 .change_interface = iwl_mac_change_interface,
Johannes Berg2295c662010-10-23 09:15:41 -07003820 .config = iwl_legacy_mac_config,
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003821 .configure_filter = iwl3945_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003822 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003823 .conf_tx = iwl_mac_conf_tx,
Johannes Berg2295c662010-10-23 09:15:41 -07003824 .reset_tsf = iwl_legacy_mac_reset_tsf,
3825 .bss_info_changed = iwl_legacy_mac_bss_info_changed,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003826 .hw_scan = iwl_mac_hw_scan,
3827 .sta_add = iwl3945_mac_sta_add,
3828 .sta_remove = iwl_mac_sta_remove,
Johannes Berga85d7cc2010-07-31 08:34:10 -07003829 .tx_last_beacon = iwl_mac_tx_last_beacon,
Zhu Yib481de92007-09-25 17:54:57 -07003830};
3831
Winkler, Tomase52119c2008-12-22 11:31:19 +08003832static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003833{
3834 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003835 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003836
3837 priv->retry_rate = 1;
Johannes Berg12e934d2010-10-04 05:50:06 -07003838 priv->beacon_skb = NULL;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003839
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003840 spin_lock_init(&priv->sta_lock);
3841 spin_lock_init(&priv->hcmd_lock);
3842
3843 INIT_LIST_HEAD(&priv->free_frames);
3844
3845 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003846 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003847
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003848 priv->ieee_channels = NULL;
3849 priv->ieee_rates = NULL;
3850 priv->band = IEEE80211_BAND_2GHZ;
3851
3852 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003853 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003854
Stanislaw Gruszkaabc471d2010-12-03 15:41:47 +01003855 /* initialize force reset */
3856 priv->force_reset[IWL_RF_RESET].reset_duration =
3857 IWL_DELAY_NEXT_FORCE_RF_RESET;
3858 priv->force_reset[IWL_FW_RESET].reset_duration =
3859 IWL_DELAY_NEXT_FORCE_FW_RELOAD;
3860
3861
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003862 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Stanislaw Gruszkaa25a66a2010-10-22 17:04:26 +02003863 priv->tx_power_next = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003864
Samuel Ortize6148912009-01-23 13:45:15 -08003865 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3866 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3867 eeprom->version);
3868 ret = -EINVAL;
3869 goto err;
3870 }
3871 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003872 if (ret) {
3873 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3874 goto err;
3875 }
3876
Samuel Ortize6148912009-01-23 13:45:15 -08003877 /* Set up txpower settings in driver for all channels */
3878 if (iwl3945_txpower_set_from_eeprom(priv)) {
3879 ret = -EIO;
3880 goto err_free_channel_map;
3881 }
3882
Samuel Ortiz534166d2009-01-23 13:45:16 -08003883 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003884 if (ret) {
3885 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3886 goto err_free_channel_map;
3887 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003888 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3889
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003890 return 0;
3891
3892err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003893 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003894err:
3895 return ret;
3896}
3897
Johannes Bergdd7a2502010-04-28 23:33:10 -07003898#define IWL3945_MAX_PROBE_REQUEST 200
3899
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003900static int iwl3945_setup_mac(struct iwl_priv *priv)
3901{
3902 int ret;
3903 struct ieee80211_hw *hw = priv->hw;
3904
3905 hw->rate_control_algorithm = "iwl-3945-rs";
3906 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
Johannes Bergfd1af152010-04-30 11:30:43 -07003907 hw->vif_data_size = sizeof(struct iwl_vif_priv);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003908
3909 /* Tell mac80211 our characteristics */
3910 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003911 IEEE80211_HW_SPECTRUM_MGMT;
3912
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07003913 if (!priv->cfg->base_params->broken_powersave)
Reinette Chatrebc45a672009-12-14 14:12:10 -08003914 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3915 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003916
3917 hw->wiphy->interface_modes =
Johannes Bergd0fe4782010-08-23 10:46:58 +02003918 priv->contexts[IWL_RXON_CTX_BSS].interface_modes;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003919
Reinette Chatref6c8f152010-03-12 11:13:26 -08003920 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
Johannes Berg5be83de2009-11-19 00:56:28 +01003921 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003922
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003923 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3924 /* we create the 802.11 header and a zero-length SSID element */
Johannes Bergdd7a2502010-04-28 23:33:10 -07003925 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003926
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003927 /* Default value; 4 EDCA QOS priorities */
3928 hw->queues = 4;
3929
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003930 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3931 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3932 &priv->bands[IEEE80211_BAND_2GHZ];
3933
3934 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3935 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3936 &priv->bands[IEEE80211_BAND_5GHZ];
3937
3938 ret = ieee80211_register_hw(priv->hw);
3939 if (ret) {
3940 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3941 return ret;
3942 }
3943 priv->mac80211_registered = 1;
3944
3945 return 0;
3946}
3947
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003948static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003949{
Johannes Berg246ed352010-08-23 10:46:32 +02003950 int err = 0, i;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003951 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003952 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003953 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003954 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003955 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003956
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003957 /***********************
3958 * 1. Allocating HW data
3959 * ********************/
3960
Zhu Yib481de92007-09-25 17:54:57 -07003961 /* mac80211 allocates memory for this device instance, including
3962 * space for this driver's private structure */
Johannes Bergdc21b542010-10-23 09:15:39 -07003963 hw = iwl_alloc_all(cfg);
Zhu Yib481de92007-09-25 17:54:57 -07003964 if (hw == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003965 pr_err("Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07003966 err = -ENOMEM;
3967 goto out;
3968 }
Zhu Yib481de92007-09-25 17:54:57 -07003969 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003970 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003971
Johannes Berg13bb9482010-08-23 10:46:33 +02003972 priv->cmd_queue = IWL39_CMD_QUEUE_NUM;
3973
Johannes Berg246ed352010-08-23 10:46:32 +02003974 /* 3945 has only one valid context */
3975 priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
3976
3977 for (i = 0; i < NUM_IWL_RXON_CTX; i++)
3978 priv->contexts[i].ctxid = i;
3979
Johannes Berg8f2d3d22010-08-23 10:46:37 +02003980 priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
3981 priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
3982 priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
Johannes Berg8dfdb9d2010-08-23 10:46:38 +02003983 priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
Johannes Berg2995baf2010-08-23 10:46:42 +02003984 priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
Johannes Bergc10afb62010-08-23 10:46:43 +02003985 priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
Johannes Bergd0fe4782010-08-23 10:46:58 +02003986 priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
3987 BIT(NL80211_IFTYPE_STATION) |
3988 BIT(NL80211_IFTYPE_ADHOC);
3989 priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
3990 priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
3991 priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
Johannes Berg8f2d3d22010-08-23 10:46:37 +02003992
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003993 /*
3994 * Disabling hardware scan means that mac80211 will perform scans
3995 * "the hard way", rather than using device's scan.
3996 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003997 if (iwl3945_mod_params.disable_hw_scan) {
Wey-Yi Guy7379efe2010-11-08 18:45:21 -08003998 dev_printk(KERN_DEBUG, &(pdev->dev),
3999 "sw scan support is deprecated\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004000 iwl3945_hw_ops.hw_scan = NULL;
4001 }
4002
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004003
Tomas Winklere1623442009-01-27 14:27:56 -08004004 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004005 priv->cfg = cfg;
4006 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07004007 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004008
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004009 if (iwl_alloc_traffic_mem(priv))
4010 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07004011
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004012 /***************************
4013 * 2. Initializing PCI bus
4014 * *************************/
John W. Linville1a7123c2010-08-05 14:39:31 -04004015 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4016 PCIE_LINK_STATE_CLKPM);
4017
Zhu Yib481de92007-09-25 17:54:57 -07004018 if (pci_enable_device(pdev)) {
4019 err = -ENODEV;
4020 goto out_ieee80211_free_hw;
4021 }
4022
4023 pci_set_master(pdev);
4024
Yang Hongyang284901a2009-04-06 19:01:15 -07004025 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004026 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004027 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004028 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004029 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004030 goto out_pci_disable_device;
4031 }
4032
4033 pci_set_drvdata(pdev, priv);
4034 err = pci_request_regions(pdev, DRV_NAME);
4035 if (err)
4036 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004037
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004038 /***********************
4039 * 3. Read REV Register
4040 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004041 priv->hw_base = pci_iomap(pdev, 0, 0);
4042 if (!priv->hw_base) {
4043 err = -ENODEV;
4044 goto out_pci_release_regions;
4045 }
4046
Tomas Winklere1623442009-01-27 14:27:56 -08004047 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004048 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004049 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004050
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004051 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4052 * PCI Tx retries from interfering with C3 CPU state */
4053 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004054
Reinette Chatre731a29b2009-12-14 14:12:11 -08004055 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004056 * we should init now
4057 */
4058 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004059 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004060
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004061 /*
4062 * stop and reset the on-board processor just in case it is in a
4063 * strange state ... like being left stranded by a primary kernel
4064 * and this is now the kdump kernel trying to start up
4065 */
4066 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4067
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004068 /***********************
4069 * 4. Read EEPROM
4070 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004071
Zhu Yi5a669262008-01-14 17:46:18 -08004072 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004073 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004074 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004075 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004076 goto out_iounmap;
Zhu Yi5a669262008-01-14 17:46:18 -08004077 }
4078 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004079 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Johannes Berg30eabc12010-05-12 03:33:10 -07004080 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
4081 SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
Zhu Yi5a669262008-01-14 17:46:18 -08004082
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004083 /***********************
4084 * 5. Setup HW Constants
4085 * ********************/
4086 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004087 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004088 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004089 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004090 }
4091
4092 /***********************
4093 * 6. Setup priv
4094 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004095
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004096 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004097 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004098 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004099 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004100 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004101
Tomas Winkler978785a2008-12-19 10:37:31 +08004102 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4103 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004104
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004105 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004106 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004107 * ********************/
4108
4109 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004110 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004111 spin_unlock_irqrestore(&priv->lock, flags);
4112
Helmut Schaa26635162009-01-15 09:38:44 +01004113 pci_enable_msi(priv->pci_dev);
4114
Wey-Yi Guye39fdee2010-11-10 09:56:40 -08004115 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr_ops.isr,
Mohamed Abbasef850d72009-05-22 11:01:50 -07004116 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004117 if (err) {
4118 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4119 goto out_disable_msi;
4120 }
4121
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004122 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4123 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004124 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004125 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004126 }
4127
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004128 iwl_set_rxon_channel(priv,
Johannes Berg246ed352010-08-23 10:46:32 +02004129 &priv->bands[IEEE80211_BAND_2GHZ].channels[5],
4130 &priv->contexts[IWL_RXON_CTX_BSS]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004131 iwl3945_setup_deferred_work(priv);
4132 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004133 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004134
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004135 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004136 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004137 * *******************************/
4138
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004139 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004140
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004141 err = iwl3945_setup_mac(priv);
4142 if (err)
4143 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004144
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004145 err = iwl_dbgfs_register(priv, DRV_NAME);
4146 if (err)
4147 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4148
Helmut Schaa26635162009-01-15 09:38:44 +01004149 /* Start monitoring the killswitch */
Johannes Bergee525d12010-01-21 06:09:28 -08004150 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01004151 2 * HZ);
4152
Zhu Yib481de92007-09-25 17:54:57 -07004153 return 0;
4154
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004155 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004156 destroy_workqueue(priv->workqueue);
4157 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004158 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4159 out_release_irq:
4160 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004161 out_disable_msi:
4162 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004163 iwlcore_free_geos(priv);
4164 iwl_free_channel_map(priv);
4165 out_unset_hw_params:
4166 iwl3945_unset_hw_params(priv);
4167 out_eeprom_free:
4168 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004169 out_iounmap:
4170 pci_iounmap(pdev, priv->hw_base);
4171 out_pci_release_regions:
4172 pci_release_regions(pdev);
4173 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004174 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004175 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004176 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004177 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004178 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004179 out:
4180 return err;
4181}
4182
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004183static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004184{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004185 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004186 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004187
4188 if (!priv)
4189 return;
4190
Tomas Winklere1623442009-01-27 14:27:56 -08004191 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004192
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004193 iwl_dbgfs_unregister(priv);
4194
Zhu Yib481de92007-09-25 17:54:57 -07004195 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004196
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004197 if (priv->mac80211_registered) {
4198 ieee80211_unregister_hw(priv->hw);
4199 priv->mac80211_registered = 0;
4200 } else {
4201 iwl3945_down(priv);
4202 }
Zhu Yib481de92007-09-25 17:54:57 -07004203
Ben Cahillc166b252009-10-23 13:42:35 -07004204 /*
4205 * Make sure device is reset to low power before unloading driver.
4206 * This may be redundant with iwl_down(), but there are paths to
4207 * run iwl_down() without calling apm_ops.stop(), and there are
4208 * paths to avoid running iwl_down() at all before leaving driver.
4209 * This (inexpensive) call *makes sure* device is reset.
4210 */
Johannes Berg14e8e4a2010-09-22 18:02:10 +02004211 iwl_apm_stop(priv);
Ben Cahillc166b252009-10-23 13:42:35 -07004212
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004213 /* make sure we flush any pending irq or
4214 * tasklet for the driver
4215 */
4216 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004217 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004218 spin_unlock_irqrestore(&priv->lock, flags);
4219
4220 iwl_synchronize_irq(priv);
4221
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004222 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004223
Johannes Bergee525d12010-01-21 06:09:28 -08004224 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004225
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004226 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004227
4228 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004229 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004230 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004231
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004232 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004233
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004234 /*netif_stop_queue(dev); */
4235 flush_workqueue(priv->workqueue);
4236
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004237 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004238 * priv->workqueue... so we can't take down the workqueue
4239 * until now... */
4240 destroy_workqueue(priv->workqueue);
4241 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004242 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004243
Helmut Schaa26635162009-01-15 09:38:44 +01004244 free_irq(pdev->irq, priv);
4245 pci_disable_msi(pdev);
4246
Zhu Yib481de92007-09-25 17:54:57 -07004247 pci_iounmap(pdev, priv->hw_base);
4248 pci_release_regions(pdev);
4249 pci_disable_device(pdev);
4250 pci_set_drvdata(pdev, NULL);
4251
Samuel Ortize6148912009-01-23 13:45:15 -08004252 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004253 iwlcore_free_geos(priv);
Johannes Berg811ecc92010-04-06 04:12:41 -07004254 kfree(priv->scan_cmd);
Johannes Berg12e934d2010-10-04 05:50:06 -07004255 if (priv->beacon_skb)
4256 dev_kfree_skb(priv->beacon_skb);
Zhu Yib481de92007-09-25 17:54:57 -07004257
4258 ieee80211_free_hw(priv->hw);
4259}
4260
Zhu Yib481de92007-09-25 17:54:57 -07004261
4262/*****************************************************************************
4263 *
4264 * driver and module entry point
4265 *
4266 *****************************************************************************/
4267
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004268static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004269 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004270 .id_table = iwl3945_hw_card_ids,
4271 .probe = iwl3945_pci_probe,
4272 .remove = __devexit_p(iwl3945_pci_remove),
John W. Linvillef60dc012010-10-25 16:12:37 -04004273 .driver.pm = IWL_PM_OPS,
Zhu Yib481de92007-09-25 17:54:57 -07004274};
4275
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004276static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004277{
4278
4279 int ret;
Joe Perchesc96c31e2010-07-26 14:39:58 -07004280 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
4281 pr_info(DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004282
4283 ret = iwl3945_rate_control_register();
4284 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004285 pr_err("Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004286 return ret;
4287 }
4288
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004289 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004290 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004291 pr_err("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004292 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004293 }
Zhu Yib481de92007-09-25 17:54:57 -07004294
4295 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004296
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004297error_register:
4298 iwl3945_rate_control_unregister();
4299 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004300}
4301
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004302static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004303{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004304 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004305 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004306}
4307
Reinette Chatrea0987a82008-12-02 12:14:06 -08004308MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004309
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004310module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004311MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004312module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004313MODULE_PARM_DESC(swcrypto,
4314 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004315#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004316module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004317MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004318#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004319module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4320 int, S_IRUGO);
Wey-Yi Guy72645ef2010-10-06 07:42:43 -07004321MODULE_PARM_DESC(disable_hw_scan,
4322 "disable hardware scanning (default 0) (deprecated)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004323module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004324MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004326module_exit(iwl3945_exit);
4327module_init(iwl3945_init);