blob: 2945acd955f0ca494dc54bdf33c38ce5879530d1 [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
Stanislaw Gruszkad828cd52011-01-28 16:47:49 +01002520 priv->active_rate = IWL_RATES_MASK_3945;
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
Stanislaw Gruszka5a5289d2011-01-28 16:47:45 +01002538 set_bit(STATUS_READY, &priv->status);
2539
Zhu Yib481de92007-09-25 17:54:57 -07002540 /* Configure the adapter for unassociated operation */
Johannes Berg8289e072010-09-22 18:01:57 +02002541 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07002542
Zhu Yib481de92007-09-25 17:54:57 -07002543 iwl3945_reg_txpower_periodic(priv);
2544
Tomas Winklere1623442009-01-27 14:27:56 -08002545 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Zhu Yi5a669262008-01-14 17:46:18 -08002546 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002547
Zhu Yib481de92007-09-25 17:54:57 -07002548 return;
2549
2550 restart:
2551 queue_work(priv->workqueue, &priv->restart);
2552}
2553
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002554static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002555
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002556static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002557{
2558 unsigned long flags;
Stanislaw Gruszkad745d472010-09-13 14:46:35 +02002559 int exit_pending;
Zhu Yib481de92007-09-25 17:54:57 -07002560
Tomas Winklere1623442009-01-27 14:27:56 -08002561 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002562
Stanislaw Gruszkad745d472010-09-13 14:46:35 +02002563 iwl_scan_cancel_timeout(priv, 200);
2564
2565 exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002566
Stanislaw Gruszkab62177a2010-08-20 15:23:47 +02002567 /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
2568 * to prevent rearm timer */
Stanislaw Gruszka22de94d2010-12-03 15:41:48 +01002569 del_timer_sync(&priv->watchdog);
Stanislaw Gruszkab62177a2010-08-20 15:23:47 +02002570
Reinette Chatre7e246192010-02-18 22:58:32 -08002571 /* Station information will now be cleared in device */
Johannes Bergdcef7322010-08-27 08:55:52 -07002572 iwl_clear_ucode_stations(priv, NULL);
Johannes Berga194e322010-08-27 08:53:46 -07002573 iwl_dealloc_bcast_stations(priv);
Johannes Bergdb125c72010-05-07 01:49:15 -07002574 iwl_clear_driver_stations(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002575
2576 /* Unblock any waiting calls */
2577 wake_up_interruptible_all(&priv->wait_command_queue);
2578
Zhu Yib481de92007-09-25 17:54:57 -07002579 /* Wipe out the EXIT_PENDING status bit if we are not actually
2580 * exiting the module */
2581 if (!exit_pending)
2582 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2583
2584 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002585 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002586
2587 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002588 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002589 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002590 spin_unlock_irqrestore(&priv->lock, flags);
2591 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002592
2593 if (priv->mac80211_registered)
2594 ieee80211_stop_queues(priv->hw);
2595
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002596 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002597 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002598 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002599 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2600 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002601 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2602 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002603 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2604 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002605 goto exit;
2606 }
2607
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002608 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002609 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002610 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2611 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002612 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2613 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002614 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002615 STATUS_FW_ERROR |
2616 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2617 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002618
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002619 iwl3945_hw_txq_ctx_stop(priv);
2620 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002621
Ben Cahill309e7312009-11-06 14:53:03 -08002622 /* Power-down device's busmaster DMA clocks */
2623 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002624 udelay(5);
2625
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002626 /* Stop the device, and put it in low power state */
Johannes Berg14e8e4a2010-09-22 18:02:10 +02002627 iwl_apm_stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002628
Zhu Yib481de92007-09-25 17:54:57 -07002629 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002630 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002631
Johannes Berg12e934d2010-10-04 05:50:06 -07002632 if (priv->beacon_skb)
2633 dev_kfree_skb(priv->beacon_skb);
2634 priv->beacon_skb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002635
2636 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002637 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002638}
2639
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002640static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002641{
2642 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002643 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002644 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002645
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002646 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002647}
2648
2649#define MAX_HW_RESTARTS 5
2650
Johannes Berga30e3112010-09-22 18:02:01 +02002651static int iwl3945_alloc_bcast_station(struct iwl_priv *priv)
2652{
2653 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2654 unsigned long flags;
2655 u8 sta_id;
2656
2657 spin_lock_irqsave(&priv->sta_lock, flags);
2658 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
2659 if (sta_id == IWL_INVALID_STATION) {
2660 IWL_ERR(priv, "Unable to prepare broadcast station\n");
2661 spin_unlock_irqrestore(&priv->sta_lock, flags);
2662
2663 return -EINVAL;
2664 }
2665
2666 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
2667 priv->stations[sta_id].used |= IWL_STA_BCAST;
2668 spin_unlock_irqrestore(&priv->sta_lock, flags);
2669
2670 return 0;
2671}
2672
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002673static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002674{
2675 int rc, i;
2676
Johannes Berga30e3112010-09-22 18:02:01 +02002677 rc = iwl3945_alloc_bcast_station(priv);
Johannes Berg2c810cc2010-04-29 00:53:29 -07002678 if (rc)
2679 return rc;
2680
Zhu Yib481de92007-09-25 17:54:57 -07002681 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002682 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002683 return -EIO;
2684 }
2685
Reinette Chatree903fbd2008-01-30 22:05:15 -08002686 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002687 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002688 return -EIO;
2689 }
2690
Zhu Yie655b9f2008-01-24 02:19:38 -08002691 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002692 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002693 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2694 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2695 else {
2696 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002697 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2698 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002699 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002700
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002701 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002702
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002703 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002704 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002705 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002706 return rc;
2707 }
2708
2709 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002710 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2711 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002712 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2713
2714 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002715 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002716 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002717
2718 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002719 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2720 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002721
2722 /* Copy original ucode data image from disk into backup cache.
2723 * This will be used to initialize the on-board processor's
2724 * data SRAM for a clean start when the runtime program first loads. */
2725 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002726 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002727
Zhu Yie655b9f2008-01-24 02:19:38 -08002728 /* We return success when we resume from suspend and rf_kill is on. */
2729 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2730 return 0;
2731
Zhu Yib481de92007-09-25 17:54:57 -07002732 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2733
Zhu Yib481de92007-09-25 17:54:57 -07002734 /* load bootstrap state machine,
2735 * load bootstrap program into processor's memory,
2736 * prepare to load the "initialize" uCode */
Abhijeet Kolekar75a9a922010-02-26 15:17:01 -08002737 rc = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002738
2739 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002740 IWL_ERR(priv,
2741 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002742 continue;
2743 }
2744
2745 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002746 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002747
Tomas Winklere1623442009-01-27 14:27:56 -08002748 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002749
2750 return 0;
2751 }
2752
2753 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002754 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002755 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002756
2757 /* tried to restart and config the device for as long as our
2758 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002759 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002760 return -EIO;
2761}
2762
2763
2764/*****************************************************************************
2765 *
2766 * Workqueue callbacks
2767 *
2768 *****************************************************************************/
2769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002770static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002771{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002772 struct iwl_priv *priv =
2773 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002774
2775 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2776 return;
2777
2778 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002779 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002780 mutex_unlock(&priv->mutex);
2781}
2782
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002783static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002784{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002785 struct iwl_priv *priv =
2786 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002787
2788 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2789 return;
2790
2791 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002792 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002793 mutex_unlock(&priv->mutex);
2794}
2795
Ben Cahill743cdf12009-10-09 13:20:26 -07002796/*
2797 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2798 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2799 * *is* readable even when device has been SW_RESET into low power mode
2800 * (e.g. during RF KILL).
2801 */
Helmut Schaa26635162009-01-15 09:38:44 +01002802static void iwl3945_rfkill_poll(struct work_struct *data)
2803{
2804 struct iwl_priv *priv =
Johannes Bergee525d12010-01-21 06:09:28 -08002805 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002806 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2807 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2808 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002809
Ben Cahill743cdf12009-10-09 13:20:26 -07002810 if (new_rfkill != old_rfkill) {
2811 if (new_rfkill)
2812 set_bit(STATUS_RF_KILL_HW, &priv->status);
2813 else
2814 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002815
Ben Cahill743cdf12009-10-09 13:20:26 -07002816 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002817
Ben Cahill743cdf12009-10-09 13:20:26 -07002818 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2819 new_rfkill ? "disable radio" : "enable radio");
2820 }
2821
2822 /* Keep this running, even if radio now enabled. This will be
2823 * cancelled in mac_start() if system decides to start again */
Johannes Bergee525d12010-01-21 06:09:28 -08002824 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01002825 round_jiffies_relative(2 * HZ));
2826
2827}
2828
Johannes Berg3eecce52010-09-13 14:46:33 +02002829int iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07002830{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002831 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002832 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002833 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002834 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002835 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002836 struct iwl3945_scan_cmd *scan;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002837 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002838 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002839 bool is_active = false;
Johannes Berg3eecce52010-09-13 14:46:33 +02002840 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002841
Johannes Berg3eecce52010-09-13 14:46:33 +02002842 lockdep_assert_held(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07002843
Johannes Berg811ecc92010-04-06 04:12:41 -07002844 if (!priv->scan_cmd) {
2845 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2846 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2847 if (!priv->scan_cmd) {
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002848 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02002849 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07002850 }
2851 }
Johannes Berg811ecc92010-04-06 04:12:41 -07002852 scan = priv->scan_cmd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002853 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002854
2855 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2856 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2857
Johannes Berg246ed352010-08-23 10:46:32 +02002858 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Zhu Yib481de92007-09-25 17:54:57 -07002859 u16 interval = 0;
2860 u32 extra;
2861 u32 suspend_time = 100;
2862 u32 scan_suspend_time = 100;
2863 unsigned long flags;
2864
Tomas Winklere1623442009-01-27 14:27:56 -08002865 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002866
2867 spin_lock_irqsave(&priv->lock, flags);
John W. Linvillea6e492b2010-07-22 15:24:56 -04002868 if (priv->is_internal_short_scan)
2869 interval = 0;
2870 else
2871 interval = vif->bss_conf.beacon_int;
Zhu Yib481de92007-09-25 17:54:57 -07002872 spin_unlock_irqrestore(&priv->lock, flags);
2873
2874 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002875 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002876 if (!interval)
2877 interval = suspend_time;
2878 /*
2879 * suspend time format:
2880 * 0-19: beacon interval in usec (time before exec.)
2881 * 20-23: 0
2882 * 24-31: number of beacons (suspend between channels)
2883 */
2884
2885 extra = (suspend_time / interval) << 24;
2886 scan_suspend_time = 0xFF0FFFFF &
2887 (extra | ((suspend_time % interval) * 1024));
2888
2889 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002890 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002891 scan_suspend_time, interval);
2892 }
2893
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002894 if (priv->is_internal_short_scan) {
2895 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2896 } else if (priv->scan_request->n_ssids) {
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002897 int i, p = 0;
2898 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2899 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2900 /* always does wildcard anyway */
2901 if (!priv->scan_request->ssids[i].ssid_len)
2902 continue;
2903 scan->direct_scan[p].id = WLAN_EID_SSID;
2904 scan->direct_scan[p].len =
2905 priv->scan_request->ssids[i].ssid_len;
2906 memcpy(scan->direct_scan[p].ssid,
2907 priv->scan_request->ssids[i].ssid,
2908 priv->scan_request->ssids[i].ssid_len);
2909 n_probes++;
2910 p++;
2911 }
2912 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002913 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002914 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002915
2916 /* We don't build a direct scan probe request; the uCode will do
2917 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002918 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Johannes Berga194e322010-08-27 08:53:46 -07002919 scan->tx_cmd.sta_id = priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002920 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2921
2922 /* flags + rate selection */
2923
Johannes Berg00700ee2010-04-06 04:12:37 -07002924 switch (priv->scan_band) {
2925 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002926 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2927 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
Johannes Berg8318d782008-01-24 19:38:38 +01002928 band = IEEE80211_BAND_2GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002929 break;
2930 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002931 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Berg8318d782008-01-24 19:38:38 +01002932 band = IEEE80211_BAND_5GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002933 break;
2934 default:
2935 IWL_WARN(priv, "Invalid scan band\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02002936 return -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07002937 }
2938
Johannes Berg085fbca2010-10-04 05:47:23 -07002939 /*
2940 * If active scaning is requested but a certain channel
2941 * is marked passive, we can do active scanning if we
2942 * detect transmissions.
2943 */
2944 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2945 IWL_GOOD_CRC_TH_DISABLED;
2946
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002947 if (!priv->is_internal_short_scan) {
2948 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002949 iwl_fill_probe_req(priv,
2950 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002951 vif->addr,
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002952 priv->scan_request->ie,
2953 priv->scan_request->ie_len,
2954 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002955 } else {
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002956 /* use bcast addr, will not be transmitted but must be valid */
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002957 scan->tx_cmd.len = cpu_to_le16(
2958 iwl_fill_probe_req(priv,
2959 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002960 iwl_bcast_addr, NULL, 0,
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002961 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2962 }
Zhu Yib481de92007-09-25 17:54:57 -07002963 /* select Rx antennas */
2964 scan->flags |= iwl3945_get_antenna_flags(priv);
2965
Abhijeet Kolekar14023642010-06-02 21:15:10 -07002966 if (priv->is_internal_short_scan) {
2967 scan->channel_count =
2968 iwl3945_get_single_channel_for_scan(priv, vif, band,
2969 (void *)&scan->data[le16_to_cpu(
2970 scan->tx_cmd.len)]);
2971 } else {
2972 scan->channel_count =
2973 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
2974 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
2975 }
Zhu Yib481de92007-09-25 17:54:57 -07002976
Reinette Chatre14b54332008-11-04 12:21:35 -08002977 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002978 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Johannes Berg3eecce52010-09-13 14:46:33 +02002979 return -EIO;
Reinette Chatre14b54332008-11-04 12:21:35 -08002980 }
2981
Zhu Yib481de92007-09-25 17:54:57 -07002982 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002983 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07002984 cmd.data = scan;
2985 scan->len = cpu_to_le16(cmd.len);
2986
2987 set_bit(STATUS_SCAN_HW, &priv->status);
Johannes Berg3eecce52010-09-13 14:46:33 +02002988 ret = iwl_send_cmd_sync(priv, &cmd);
2989 if (ret)
2990 clear_bit(STATUS_SCAN_HW, &priv->status);
2991 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002992}
2993
Johannes Berga77029e2010-09-22 18:01:56 +02002994void iwl3945_post_scan(struct iwl_priv *priv)
2995{
2996 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2997
2998 /*
2999 * Since setting the RXON may have been deferred while
3000 * performing the scan, fire one off if needed
3001 */
3002 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
Johannes Berg8289e072010-09-22 18:01:57 +02003003 iwl3945_commit_rxon(priv, ctx);
Johannes Berga77029e2010-09-22 18:01:56 +02003004}
3005
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003006static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003007{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003008 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003009
3010 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3011 return;
3012
Johannes Berg19cc1082009-05-08 13:44:36 -07003013 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
Johannes Berg8bd413e2010-08-23 10:46:40 +02003014 struct iwl_rxon_context *ctx;
Johannes Berg19cc1082009-05-08 13:44:36 -07003015 mutex_lock(&priv->mutex);
Johannes Berg8bd413e2010-08-23 10:46:40 +02003016 for_each_context(priv, ctx)
3017 ctx->vif = NULL;
Johannes Berg19cc1082009-05-08 13:44:36 -07003018 priv->is_open = 0;
3019 mutex_unlock(&priv->mutex);
3020 iwl3945_down(priv);
3021 ieee80211_restart_hw(priv->hw);
3022 } else {
3023 iwl3945_down(priv);
Johannes Berg80676512010-01-21 06:07:17 -08003024
3025 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3026 return;
3027
3028 mutex_lock(&priv->mutex);
3029 __iwl3945_up(priv);
3030 mutex_unlock(&priv->mutex);
Johannes Berg19cc1082009-05-08 13:44:36 -07003031 }
Zhu Yib481de92007-09-25 17:54:57 -07003032}
3033
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003034static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003035{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003036 struct iwl_priv *priv =
3037 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003038
3039 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3040 return;
3041
3042 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003043 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003044 mutex_unlock(&priv->mutex);
3045}
3046
Johannes Berg2295c662010-10-23 09:15:41 -07003047void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003048{
Zhu Yib481de92007-09-25 17:54:57 -07003049 int rc = 0;
3050 struct ieee80211_conf *conf = NULL;
Johannes Berg246ed352010-08-23 10:46:32 +02003051 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003052
Johannes Berg2295c662010-10-23 09:15:41 -07003053 if (!ctx->vif || !priv->is_open)
Johannes Berg1dda6d22010-04-29 04:43:06 -07003054 return;
3055
Johannes Berg2295c662010-10-23 09:15:41 -07003056 if (ctx->vif->type == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003057 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003058 return;
3059 }
3060
Tomas Winklere1623442009-01-27 14:27:56 -08003061 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003062 ctx->vif->bss_conf.aid, ctx->active.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003063
3064 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3065 return;
3066
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003067 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003068
Zhu Yib481de92007-09-25 17:54:57 -07003069 conf = ieee80211_get_hw_conf(priv->hw);
3070
Johannes Berg246ed352010-08-23 10:46:32 +02003071 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003072 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003073
Johannes Berg47313e32010-08-23 10:46:55 +02003074 rc = iwl_send_rxon_timing(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003075 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003076 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003077 "Attempting to continue.\n");
3078
Johannes Berg246ed352010-08-23 10:46:32 +02003079 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003080
Johannes Berg2295c662010-10-23 09:15:41 -07003081 ctx->staging.assoc_id = cpu_to_le16(ctx->vif->bss_conf.aid);
Zhu Yib481de92007-09-25 17:54:57 -07003082
Tomas Winklere1623442009-01-27 14:27:56 -08003083 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003084 ctx->vif->bss_conf.aid, ctx->vif->bss_conf.beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07003085
Johannes Berg2295c662010-10-23 09:15:41 -07003086 if (ctx->vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003087 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003088 else
Johannes Berg246ed352010-08-23 10:46:32 +02003089 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003090
Johannes Berg246ed352010-08-23 10:46:32 +02003091 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Berg2295c662010-10-23 09:15:41 -07003092 if (ctx->vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003093 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003094 else
Johannes Berg246ed352010-08-23 10:46:32 +02003095 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003096 }
3097
Johannes Berg8289e072010-09-22 18:01:57 +02003098 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003099
Johannes Berg2295c662010-10-23 09:15:41 -07003100 switch (ctx->vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003101 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003102 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003103 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02003104 case NL80211_IFTYPE_ADHOC:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003105 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003106 break;
Zhu Yib481de92007-09-25 17:54:57 -07003107 default:
Johannes Berg1dda6d22010-04-29 04:43:06 -07003108 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Johannes Berg2295c662010-10-23 09:15:41 -07003109 __func__, ctx->vif->type);
Zhu Yib481de92007-09-25 17:54:57 -07003110 break;
3111 }
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003112}
3113
Zhu Yib481de92007-09-25 17:54:57 -07003114/*****************************************************************************
3115 *
3116 * mac80211 entry point functions
3117 *
3118 *****************************************************************************/
3119
Zhu Yi5a669262008-01-14 17:46:18 -08003120#define UCODE_READY_TIMEOUT (2 * HZ)
3121
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003122static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003123{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003124 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08003125 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003126
Tomas Winklere1623442009-01-27 14:27:56 -08003127 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003128
3129 /* we should be verifying the device is ready to be opened */
3130 mutex_lock(&priv->mutex);
3131
Zhu Yi5a669262008-01-14 17:46:18 -08003132 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3133 * ucode filename and max sizes are card-specific. */
3134
3135 if (!priv->ucode_code.len) {
3136 ret = iwl3945_read_ucode(priv);
3137 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003138 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08003139 mutex_unlock(&priv->mutex);
3140 goto out_release_irq;
3141 }
3142 }
3143
Zhu Yie655b9f2008-01-24 02:19:38 -08003144 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08003145
Zhu Yib481de92007-09-25 17:54:57 -07003146 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08003147
Zhu Yie655b9f2008-01-24 02:19:38 -08003148 if (ret)
3149 goto out_release_irq;
3150
Tomas Winklere1623442009-01-27 14:27:56 -08003151 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003152
Zhu Yi5a669262008-01-14 17:46:18 -08003153 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3154 * mac80211 will not be run successfully. */
3155 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3156 test_bit(STATUS_READY, &priv->status),
3157 UCODE_READY_TIMEOUT);
3158 if (!ret) {
3159 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003160 IWL_ERR(priv,
3161 "Wait for START_ALIVE timeout after %dms.\n",
3162 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08003163 ret = -ETIMEDOUT;
3164 goto out_release_irq;
3165 }
3166 }
3167
Helmut Schaa26635162009-01-15 09:38:44 +01003168 /* ucode is running and will send rfkill notifications,
3169 * no need to poll the killswitch state anymore */
Johannes Bergee525d12010-01-21 06:09:28 -08003170 cancel_delayed_work(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01003171
Zhu Yie655b9f2008-01-24 02:19:38 -08003172 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003173 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003174 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003175
3176out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003177 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003178 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08003179 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003180}
3181
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003182static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003183{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003184 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003185
Tomas Winklere1623442009-01-27 14:27:56 -08003186 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003187
Zhu Yie655b9f2008-01-24 02:19:38 -08003188 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003189 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003190 return;
3191 }
3192
Zhu Yib481de92007-09-25 17:54:57 -07003193 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003194
Zhu Yi5a669262008-01-14 17:46:18 -08003195 iwl3945_down(priv);
3196
3197 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003198
3199 /* start polling the killswitch state again */
Johannes Bergee525d12010-01-21 06:09:28 -08003200 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01003201 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003202
Tomas Winklere1623442009-01-27 14:27:56 -08003203 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003204}
3205
Johannes Berge039fa42008-05-15 12:55:29 +02003206static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003207{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003208 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003209
Tomas Winklere1623442009-01-27 14:27:56 -08003210 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003211
Tomas Winklere1623442009-01-27 14:27:56 -08003212 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003213 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003214
Johannes Berge039fa42008-05-15 12:55:29 +02003215 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003216 dev_kfree_skb_any(skb);
3217
Tomas Winklere1623442009-01-27 14:27:56 -08003218 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003219 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003220}
3221
Johannes Berg2295c662010-10-23 09:15:41 -07003222void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003223{
Johannes Berg246ed352010-08-23 10:46:32 +02003224 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg2295c662010-10-23 09:15:41 -07003225 struct ieee80211_vif *vif = ctx->vif;
Zhu Yib481de92007-09-25 17:54:57 -07003226 int rc = 0;
3227
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003228 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003229 return;
3230
3231 /* The following should be done only at AP bring up */
Johannes Berg246ed352010-08-23 10:46:32 +02003232 if (!(iwl_is_associated(priv, IWL_RXON_CTX_BSS))) {
Zhu Yib481de92007-09-25 17:54:57 -07003233
3234 /* RXON - unassoc (to set timing command) */
Johannes Berg246ed352010-08-23 10:46:32 +02003235 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003236 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003237
3238 /* RXON Timing */
Johannes Berg47313e32010-08-23 10:46:55 +02003239 rc = iwl_send_rxon_timing(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003240 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003241 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003242 "Attempting to continue.\n");
3243
Johannes Berg246ed352010-08-23 10:46:32 +02003244 ctx->staging.assoc_id = 0;
Johannes Berg1dda6d22010-04-29 04:43:06 -07003245
Johannes Bergc213d742010-05-06 12:21:40 -07003246 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003247 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003248 RXON_FLG_SHORT_PREAMBLE_MSK;
3249 else
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
Johannes Berg246ed352010-08-23 10:46:32 +02003253 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003254 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003255 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003256 RXON_FLG_SHORT_SLOT_MSK;
3257 else
Johannes Berg246ed352010-08-23 10:46:32 +02003258 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003259 ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003260 }
3261 /* restore RXON assoc */
Johannes Berg246ed352010-08-23 10:46:32 +02003262 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Johannes Berg8289e072010-09-22 18:01:57 +02003263 iwl3945_commit_rxon(priv, ctx);
Zhu Yi556f8db2007-09-27 11:27:33 +08003264 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003265 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003266
3267 /* FIXME - we need to add code here to detect a totally new
3268 * configuration, reset the AP, unassoc, rxon timing, assoc,
3269 * clear sta table, add BCAST sta... */
3270}
3271
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003272static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003273 struct ieee80211_vif *vif,
3274 struct ieee80211_sta *sta,
3275 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003276{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003277 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003278 int ret = 0;
3279 u8 sta_id = IWL_INVALID_STATION;
3280 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003281
Tomas Winklere1623442009-01-27 14:27:56 -08003282 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003283
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003284 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003285 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003286 return -EOPNOTSUPP;
3287 }
3288
Johannes Berg246ed352010-08-23 10:46:32 +02003289 static_key = !iwl_is_associated(priv, IWL_RXON_CTX_BSS);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003290
3291 if (!static_key) {
Johannes Berga194e322010-08-27 08:53:46 -07003292 sta_id = iwl_sta_id_or_broadcast(
3293 priv, &priv->contexts[IWL_RXON_CTX_BSS], sta);
Johannes Berg0af8bca2010-04-30 14:08:00 -07003294 if (sta_id == IWL_INVALID_STATION)
3295 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003296 }
3297
3298 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003299 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003300
Zhu Yib481de92007-09-25 17:54:57 -07003301 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003302 case SET_KEY:
3303 if (static_key)
3304 ret = iwl3945_set_static_key(priv, key);
3305 else
3306 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3307 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003308 break;
3309 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003310 if (static_key)
3311 ret = iwl3945_remove_static_key(priv);
3312 else
3313 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3314 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003315 break;
3316 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003317 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003318 }
3319
Johannes Berg72e15d72010-02-19 11:42:32 -08003320 mutex_unlock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003321 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003322
Winkler, Tomas42986792009-01-19 15:30:22 -08003323 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003324}
3325
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003326static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3327 struct ieee80211_vif *vif,
3328 struct ieee80211_sta *sta)
3329{
3330 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07003331 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003332 int ret;
Johannes Bergfd1af152010-04-30 11:30:43 -07003333 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003334 u8 sta_id;
3335
3336 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3337 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003338 mutex_lock(&priv->mutex);
3339 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
3340 sta->addr);
3341 sta_priv->common.sta_id = IWL_INVALID_STATION;
3342
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003343
Johannes Berga194e322010-08-27 08:53:46 -07003344 ret = iwl_add_station_common(priv, &priv->contexts[IWL_RXON_CTX_BSS],
Johannes Berg238d7812010-08-23 10:46:45 +02003345 sta->addr, is_ap, sta, &sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003346 if (ret) {
3347 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3348 sta->addr, ret);
3349 /* Should we return success if return code is EEXIST ? */
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003350 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003351 return ret;
3352 }
3353
Johannes Bergfd1af152010-04-30 11:30:43 -07003354 sta_priv->common.sta_id = sta_id;
3355
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003356 /* Initialize rate scaling */
Frans Pop91dd6c22010-03-24 14:19:58 -07003357 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003358 sta->addr);
3359 iwl3945_rs_rate_init(priv, sta, sta_id);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003360 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003361
3362 return 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003363}
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003364
3365static void iwl3945_configure_filter(struct ieee80211_hw *hw,
3366 unsigned int changed_flags,
3367 unsigned int *total_flags,
3368 u64 multicast)
3369{
3370 struct iwl_priv *priv = hw->priv;
3371 __le32 filter_or = 0, filter_nand = 0;
Johannes Berg246ed352010-08-23 10:46:32 +02003372 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003373
3374#define CHK(test, flag) do { \
3375 if (*total_flags & (test)) \
3376 filter_or |= (flag); \
3377 else \
3378 filter_nand |= (flag); \
3379 } while (0)
3380
3381 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
3382 changed_flags, *total_flags);
3383
3384 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
3385 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
3386 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
3387
3388#undef CHK
3389
3390 mutex_lock(&priv->mutex);
3391
Johannes Berg246ed352010-08-23 10:46:32 +02003392 ctx->staging.filter_flags &= ~filter_nand;
3393 ctx->staging.filter_flags |= filter_or;
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003394
3395 /*
Stanislaw Gruszka749ff4e2010-10-22 17:04:27 +02003396 * Not committing directly because hardware can perform a scan,
3397 * but even if hw is ready, committing here breaks for some reason,
3398 * we'll eventually commit the filter flags change anyway.
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003399 */
3400
3401 mutex_unlock(&priv->mutex);
3402
3403 /*
3404 * Receiving all multicast frames is always enabled by the
3405 * default flags setup in iwl_connection_init_rx_config()
3406 * since we currently do not support programming multicast
3407 * filters into the device.
3408 */
3409 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
3410 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
3411}
3412
3413
Zhu Yib481de92007-09-25 17:54:57 -07003414/*****************************************************************************
3415 *
3416 * sysfs attributes
3417 *
3418 *****************************************************************************/
3419
Samuel Ortizd08853a2009-01-23 13:45:17 -08003420#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003421
3422/*
3423 * The following adds a new attribute to the sysfs representation
3424 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3425 * used for controlling the debug level.
3426 *
3427 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003428 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003429 * The debug_level being managed using sysfs below is a per device debug
3430 * level that is used instead of the global debug level if it (the per
3431 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003432 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003433static ssize_t show_debug_level(struct device *d,
3434 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003435{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003436 struct iwl_priv *priv = dev_get_drvdata(d);
3437 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003438}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003439static ssize_t store_debug_level(struct device *d,
3440 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003441 const char *buf, size_t count)
3442{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003443 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003444 unsigned long val;
3445 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003446
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003447 ret = strict_strtoul(buf, 0, &val);
3448 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003449 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003450 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003451 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003452 if (iwl_alloc_traffic_mem(priv))
3453 IWL_ERR(priv,
3454 "Not enough memory to generate traffic log\n");
3455 }
Zhu Yib481de92007-09-25 17:54:57 -07003456 return strnlen(buf, count);
3457}
3458
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003459static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3460 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003461
Samuel Ortizd08853a2009-01-23 13:45:17 -08003462#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003463
Zhu Yib481de92007-09-25 17:54:57 -07003464static ssize_t show_temperature(struct device *d,
3465 struct device_attribute *attr, char *buf)
3466{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003467 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003468
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003469 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003470 return -EAGAIN;
3471
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003472 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003473}
3474
3475static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3476
Zhu Yib481de92007-09-25 17:54:57 -07003477static ssize_t show_tx_power(struct device *d,
3478 struct device_attribute *attr, char *buf)
3479{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003480 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003481 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003482}
3483
3484static ssize_t store_tx_power(struct device *d,
3485 struct device_attribute *attr,
3486 const char *buf, size_t count)
3487{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003488 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003489 char *p = (char *)buf;
3490 u32 val;
3491
3492 val = simple_strtoul(p, &p, 10);
3493 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003494 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003495 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003496 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003497
3498 return count;
3499}
3500
3501static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3502
3503static ssize_t show_flags(struct device *d,
3504 struct device_attribute *attr, char *buf)
3505{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003506 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003507 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003508
Johannes Berg246ed352010-08-23 10:46:32 +02003509 return sprintf(buf, "0x%04X\n", ctx->active.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003510}
3511
3512static ssize_t store_flags(struct device *d,
3513 struct device_attribute *attr,
3514 const char *buf, size_t count)
3515{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003516 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003517 u32 flags = simple_strtoul(buf, NULL, 0);
Johannes Berg246ed352010-08-23 10:46:32 +02003518 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003519
3520 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003521 if (le32_to_cpu(ctx->staging.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003522 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003523 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003524 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003525 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003526 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003527 flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003528 ctx->staging.flags = cpu_to_le32(flags);
Johannes Berg8289e072010-09-22 18:01:57 +02003529 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003530 }
3531 }
3532 mutex_unlock(&priv->mutex);
3533
3534 return count;
3535}
3536
3537static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3538
3539static ssize_t show_filter_flags(struct device *d,
3540 struct device_attribute *attr, char *buf)
3541{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003542 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003543 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003544
3545 return sprintf(buf, "0x%04X\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003546 le32_to_cpu(ctx->active.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003547}
3548
3549static ssize_t store_filter_flags(struct device *d,
3550 struct device_attribute *attr,
3551 const char *buf, size_t count)
3552{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003553 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003554 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003555 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3556
3557 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003558 if (le32_to_cpu(ctx->staging.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003559 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003560 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003561 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003562 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003563 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003564 "0x%04X\n", filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003565 ctx->staging.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003566 cpu_to_le32(filter_flags);
Johannes Berg8289e072010-09-22 18:01:57 +02003567 iwl3945_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003568 }
3569 }
3570 mutex_unlock(&priv->mutex);
3571
3572 return count;
3573}
3574
3575static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3576 store_filter_flags);
3577
Zhu Yib481de92007-09-25 17:54:57 -07003578static ssize_t show_measurement(struct device *d,
3579 struct device_attribute *attr, char *buf)
3580{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003581 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003582 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003583 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003584 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003585 unsigned long flags;
3586
3587 spin_lock_irqsave(&priv->lock, flags);
3588 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3589 spin_unlock_irqrestore(&priv->lock, flags);
3590 return 0;
3591 }
3592 memcpy(&measure_report, &priv->measure_report, size);
3593 priv->measurement_status = 0;
3594 spin_unlock_irqrestore(&priv->lock, flags);
3595
3596 while (size && (PAGE_SIZE - len)) {
3597 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3598 PAGE_SIZE - len, 1);
3599 len = strlen(buf);
3600 if (PAGE_SIZE - len)
3601 buf[len++] = '\n';
3602
3603 ofs += 16;
3604 size -= min(size, 16U);
3605 }
3606
3607 return len;
3608}
3609
3610static ssize_t store_measurement(struct device *d,
3611 struct device_attribute *attr,
3612 const char *buf, size_t count)
3613{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003614 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003615 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003616 struct ieee80211_measurement_params params = {
Johannes Berg246ed352010-08-23 10:46:32 +02003617 .channel = le16_to_cpu(ctx->active.channel),
Johannes Berge99f1682010-01-19 10:04:28 -08003618 .start_time = cpu_to_le64(priv->_3945.last_tsf),
Zhu Yib481de92007-09-25 17:54:57 -07003619 .duration = cpu_to_le16(1),
3620 };
3621 u8 type = IWL_MEASURE_BASIC;
3622 u8 buffer[32];
3623 u8 channel;
3624
3625 if (count) {
3626 char *p = buffer;
3627 strncpy(buffer, buf, min(sizeof(buffer), count));
3628 channel = simple_strtoul(p, NULL, 0);
3629 if (channel)
3630 params.channel = channel;
3631
3632 p = buffer;
3633 while (*p && *p != ' ')
3634 p++;
3635 if (*p)
3636 type = simple_strtoul(p + 1, NULL, 0);
3637 }
3638
Tomas Winklere1623442009-01-27 14:27:56 -08003639 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003640 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003641 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003642
3643 return count;
3644}
3645
3646static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3647 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003648
Zhu Yib481de92007-09-25 17:54:57 -07003649static ssize_t store_retry_rate(struct device *d,
3650 struct device_attribute *attr,
3651 const char *buf, size_t count)
3652{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003653 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003654
3655 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3656 if (priv->retry_rate <= 0)
3657 priv->retry_rate = 1;
3658
3659 return count;
3660}
3661
3662static ssize_t show_retry_rate(struct device *d,
3663 struct device_attribute *attr, char *buf)
3664{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003665 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003666 return sprintf(buf, "%d", priv->retry_rate);
3667}
3668
3669static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3670 store_retry_rate);
3671
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003672
Zhu Yib481de92007-09-25 17:54:57 -07003673static ssize_t show_channels(struct device *d,
3674 struct device_attribute *attr, char *buf)
3675{
Johannes Berg8318d782008-01-24 19:38:38 +01003676 /* all this shit doesn't belong into sysfs anyway */
3677 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003678}
3679
3680static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3681
Zhu Yib481de92007-09-25 17:54:57 -07003682static ssize_t show_antenna(struct device *d,
3683 struct device_attribute *attr, char *buf)
3684{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003685 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003686
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003687 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003688 return -EAGAIN;
3689
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003690 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003691}
3692
3693static ssize_t store_antenna(struct device *d,
3694 struct device_attribute *attr,
3695 const char *buf, size_t count)
3696{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003697 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003698 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003699
3700 if (count == 0)
3701 return 0;
3702
3703 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003704 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003705 return count;
3706 }
3707
3708 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003709 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003710 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003711 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003712 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003713
3714
3715 return count;
3716}
3717
3718static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3719
3720static ssize_t show_status(struct device *d,
3721 struct device_attribute *attr, char *buf)
3722{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003723 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003724 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003725 return -EAGAIN;
3726 return sprintf(buf, "0x%08x\n", (int)priv->status);
3727}
3728
3729static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3730
3731static ssize_t dump_error_log(struct device *d,
3732 struct device_attribute *attr,
3733 const char *buf, size_t count)
3734{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003735 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003736 char *p = (char *)buf;
3737
3738 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003739 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003740
3741 return strnlen(buf, count);
3742}
3743
3744static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3745
Zhu Yib481de92007-09-25 17:54:57 -07003746/*****************************************************************************
3747 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003748 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003749 *
3750 *****************************************************************************/
3751
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003752static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003753{
Reinette Chatred21050c2009-02-13 11:51:18 -08003754 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003755
3756 init_waitqueue_head(&priv->wait_command_queue);
3757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003758 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3759 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003760 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003761 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3762 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Johannes Bergee525d12010-01-21 06:09:28 -08003763 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02003764
3765 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003766
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003767 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003768
Stanislaw Gruszka22de94d2010-12-03 15:41:48 +01003769 init_timer(&priv->watchdog);
3770 priv->watchdog.data = (unsigned long)priv;
3771 priv->watchdog.function = iwl_bg_watchdog;
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003772
Zhu Yib481de92007-09-25 17:54:57 -07003773 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003774 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003775}
3776
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003777static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003778{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003779 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003780
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003781 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003782 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003783 cancel_work_sync(&priv->beacon_update);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02003784
3785 iwl_cancel_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003786}
3787
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003788static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003789 &dev_attr_antenna.attr,
3790 &dev_attr_channels.attr,
3791 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003792 &dev_attr_flags.attr,
3793 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003794 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003795 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003796 &dev_attr_status.attr,
3797 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003798 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003799#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003800 &dev_attr_debug_level.attr,
3801#endif
Zhu Yib481de92007-09-25 17:54:57 -07003802 NULL
3803};
3804
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003805static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003806 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003808};
3809
Johannes Bergdc21b542010-10-23 09:15:39 -07003810struct ieee80211_ops iwl3945_hw_ops = {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003811 .tx = iwl3945_mac_tx,
3812 .start = iwl3945_mac_start,
3813 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003814 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003815 .remove_interface = iwl_mac_remove_interface,
Johannes Bergd4daaea2010-10-23 09:15:43 -07003816 .change_interface = iwl_mac_change_interface,
Johannes Berg2295c662010-10-23 09:15:41 -07003817 .config = iwl_legacy_mac_config,
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003818 .configure_filter = iwl3945_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003819 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003820 .conf_tx = iwl_mac_conf_tx,
Johannes Berg2295c662010-10-23 09:15:41 -07003821 .reset_tsf = iwl_legacy_mac_reset_tsf,
3822 .bss_info_changed = iwl_legacy_mac_bss_info_changed,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003823 .hw_scan = iwl_mac_hw_scan,
3824 .sta_add = iwl3945_mac_sta_add,
3825 .sta_remove = iwl_mac_sta_remove,
Johannes Berga85d7cc2010-07-31 08:34:10 -07003826 .tx_last_beacon = iwl_mac_tx_last_beacon,
Zhu Yib481de92007-09-25 17:54:57 -07003827};
3828
Winkler, Tomase52119c2008-12-22 11:31:19 +08003829static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003830{
3831 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003832 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003833
3834 priv->retry_rate = 1;
Johannes Berg12e934d2010-10-04 05:50:06 -07003835 priv->beacon_skb = NULL;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003836
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003837 spin_lock_init(&priv->sta_lock);
3838 spin_lock_init(&priv->hcmd_lock);
3839
3840 INIT_LIST_HEAD(&priv->free_frames);
3841
3842 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003843 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003844
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003845 priv->ieee_channels = NULL;
3846 priv->ieee_rates = NULL;
3847 priv->band = IEEE80211_BAND_2GHZ;
3848
3849 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003850 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003851
Stanislaw Gruszkaabc471d2010-12-03 15:41:47 +01003852 /* initialize force reset */
3853 priv->force_reset[IWL_RF_RESET].reset_duration =
3854 IWL_DELAY_NEXT_FORCE_RF_RESET;
3855 priv->force_reset[IWL_FW_RESET].reset_duration =
3856 IWL_DELAY_NEXT_FORCE_FW_RELOAD;
3857
3858
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003859 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Stanislaw Gruszkaa25a66a2010-10-22 17:04:26 +02003860 priv->tx_power_next = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003861
Samuel Ortize6148912009-01-23 13:45:15 -08003862 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3863 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3864 eeprom->version);
3865 ret = -EINVAL;
3866 goto err;
3867 }
3868 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003869 if (ret) {
3870 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3871 goto err;
3872 }
3873
Samuel Ortize6148912009-01-23 13:45:15 -08003874 /* Set up txpower settings in driver for all channels */
3875 if (iwl3945_txpower_set_from_eeprom(priv)) {
3876 ret = -EIO;
3877 goto err_free_channel_map;
3878 }
3879
Samuel Ortiz534166d2009-01-23 13:45:16 -08003880 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003881 if (ret) {
3882 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3883 goto err_free_channel_map;
3884 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003885 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3886
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003887 return 0;
3888
3889err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003890 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003891err:
3892 return ret;
3893}
3894
Johannes Bergdd7a2502010-04-28 23:33:10 -07003895#define IWL3945_MAX_PROBE_REQUEST 200
3896
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003897static int iwl3945_setup_mac(struct iwl_priv *priv)
3898{
3899 int ret;
3900 struct ieee80211_hw *hw = priv->hw;
3901
3902 hw->rate_control_algorithm = "iwl-3945-rs";
3903 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
Johannes Bergfd1af152010-04-30 11:30:43 -07003904 hw->vif_data_size = sizeof(struct iwl_vif_priv);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003905
3906 /* Tell mac80211 our characteristics */
3907 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003908 IEEE80211_HW_SPECTRUM_MGMT;
3909
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -07003910 if (!priv->cfg->base_params->broken_powersave)
Reinette Chatrebc45a672009-12-14 14:12:10 -08003911 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3912 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003913
3914 hw->wiphy->interface_modes =
Johannes Bergd0fe4782010-08-23 10:46:58 +02003915 priv->contexts[IWL_RXON_CTX_BSS].interface_modes;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003916
Reinette Chatref6c8f152010-03-12 11:13:26 -08003917 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
Johannes Berg5be83de2009-11-19 00:56:28 +01003918 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003919
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003920 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3921 /* we create the 802.11 header and a zero-length SSID element */
Johannes Bergdd7a2502010-04-28 23:33:10 -07003922 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003923
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003924 /* Default value; 4 EDCA QOS priorities */
3925 hw->queues = 4;
3926
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003927 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3928 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3929 &priv->bands[IEEE80211_BAND_2GHZ];
3930
3931 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3932 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3933 &priv->bands[IEEE80211_BAND_5GHZ];
3934
Wey-Yi Guy5ed540a2011-01-21 15:26:39 -08003935 iwl_leds_init(priv);
3936
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003937 ret = ieee80211_register_hw(priv->hw);
3938 if (ret) {
3939 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3940 return ret;
3941 }
3942 priv->mac80211_registered = 1;
3943
3944 return 0;
3945}
3946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003947static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003948{
Johannes Berg246ed352010-08-23 10:46:32 +02003949 int err = 0, i;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003950 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003951 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003952 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003953 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003954 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003955
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003956 /***********************
3957 * 1. Allocating HW data
3958 * ********************/
3959
Zhu Yib481de92007-09-25 17:54:57 -07003960 /* mac80211 allocates memory for this device instance, including
3961 * space for this driver's private structure */
Johannes Bergdc21b542010-10-23 09:15:39 -07003962 hw = iwl_alloc_all(cfg);
Zhu Yib481de92007-09-25 17:54:57 -07003963 if (hw == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003964 pr_err("Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07003965 err = -ENOMEM;
3966 goto out;
3967 }
Zhu Yib481de92007-09-25 17:54:57 -07003968 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003969 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003970
Johannes Berg13bb9482010-08-23 10:46:33 +02003971 priv->cmd_queue = IWL39_CMD_QUEUE_NUM;
3972
Johannes Berg246ed352010-08-23 10:46:32 +02003973 /* 3945 has only one valid context */
3974 priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
3975
3976 for (i = 0; i < NUM_IWL_RXON_CTX; i++)
3977 priv->contexts[i].ctxid = i;
3978
Johannes Berg8f2d3d22010-08-23 10:46:37 +02003979 priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
3980 priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
3981 priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
Johannes Berg8dfdb9d2010-08-23 10:46:38 +02003982 priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
Johannes Berg2995baf2010-08-23 10:46:42 +02003983 priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
Johannes Bergc10afb62010-08-23 10:46:43 +02003984 priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
Johannes Bergd0fe4782010-08-23 10:46:58 +02003985 priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
3986 BIT(NL80211_IFTYPE_STATION) |
3987 BIT(NL80211_IFTYPE_ADHOC);
3988 priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
3989 priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
3990 priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
Johannes Berg8f2d3d22010-08-23 10:46:37 +02003991
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003992 /*
3993 * Disabling hardware scan means that mac80211 will perform scans
3994 * "the hard way", rather than using device's scan.
3995 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003996 if (iwl3945_mod_params.disable_hw_scan) {
Wey-Yi Guy7379efe2010-11-08 18:45:21 -08003997 dev_printk(KERN_DEBUG, &(pdev->dev),
3998 "sw scan support is deprecated\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003999 iwl3945_hw_ops.hw_scan = NULL;
4000 }
4001
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004002
Tomas Winklere1623442009-01-27 14:27:56 -08004003 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004004 priv->cfg = cfg;
4005 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07004006 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004007
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004008 if (iwl_alloc_traffic_mem(priv))
4009 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07004010
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004011 /***************************
4012 * 2. Initializing PCI bus
4013 * *************************/
John W. Linville1a7123c2010-08-05 14:39:31 -04004014 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4015 PCIE_LINK_STATE_CLKPM);
4016
Zhu Yib481de92007-09-25 17:54:57 -07004017 if (pci_enable_device(pdev)) {
4018 err = -ENODEV;
4019 goto out_ieee80211_free_hw;
4020 }
4021
4022 pci_set_master(pdev);
4023
Yang Hongyang284901a2009-04-06 19:01:15 -07004024 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004025 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004026 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004027 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004028 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004029 goto out_pci_disable_device;
4030 }
4031
4032 pci_set_drvdata(pdev, priv);
4033 err = pci_request_regions(pdev, DRV_NAME);
4034 if (err)
4035 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004036
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004037 /***********************
4038 * 3. Read REV Register
4039 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004040 priv->hw_base = pci_iomap(pdev, 0, 0);
4041 if (!priv->hw_base) {
4042 err = -ENODEV;
4043 goto out_pci_release_regions;
4044 }
4045
Tomas Winklere1623442009-01-27 14:27:56 -08004046 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004047 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004048 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004049
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004050 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4051 * PCI Tx retries from interfering with C3 CPU state */
4052 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004053
Reinette Chatre731a29b2009-12-14 14:12:11 -08004054 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004055 * we should init now
4056 */
4057 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004058 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004059
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004060 /*
4061 * stop and reset the on-board processor just in case it is in a
4062 * strange state ... like being left stranded by a primary kernel
4063 * and this is now the kdump kernel trying to start up
4064 */
4065 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4066
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004067 /***********************
4068 * 4. Read EEPROM
4069 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004070
Zhu Yi5a669262008-01-14 17:46:18 -08004071 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004072 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004073 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004074 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004075 goto out_iounmap;
Zhu Yi5a669262008-01-14 17:46:18 -08004076 }
4077 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004078 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Johannes Berg30eabc12010-05-12 03:33:10 -07004079 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
4080 SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
Zhu Yi5a669262008-01-14 17:46:18 -08004081
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004082 /***********************
4083 * 5. Setup HW Constants
4084 * ********************/
4085 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004086 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004087 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004088 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004089 }
4090
4091 /***********************
4092 * 6. Setup priv
4093 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004094
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004095 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004096 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004097 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004098 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004099 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004100
Tomas Winkler978785a2008-12-19 10:37:31 +08004101 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4102 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004103
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004104 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004105 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004106 * ********************/
4107
4108 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004109 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004110 spin_unlock_irqrestore(&priv->lock, flags);
4111
Helmut Schaa26635162009-01-15 09:38:44 +01004112 pci_enable_msi(priv->pci_dev);
4113
Wey-Yi Guye39fdee2010-11-10 09:56:40 -08004114 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr_ops.isr,
Mohamed Abbasef850d72009-05-22 11:01:50 -07004115 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004116 if (err) {
4117 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4118 goto out_disable_msi;
4119 }
4120
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004121 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4122 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004123 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004124 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004125 }
4126
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004127 iwl_set_rxon_channel(priv,
Johannes Berg246ed352010-08-23 10:46:32 +02004128 &priv->bands[IEEE80211_BAND_2GHZ].channels[5],
4129 &priv->contexts[IWL_RXON_CTX_BSS]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004130 iwl3945_setup_deferred_work(priv);
4131 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004132 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004133
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004134 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004135 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004136 * *******************************/
4137
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004138 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004139
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004140 err = iwl3945_setup_mac(priv);
4141 if (err)
4142 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004143
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004144 err = iwl_dbgfs_register(priv, DRV_NAME);
4145 if (err)
4146 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4147
Helmut Schaa26635162009-01-15 09:38:44 +01004148 /* Start monitoring the killswitch */
Johannes Bergee525d12010-01-21 06:09:28 -08004149 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01004150 2 * HZ);
4151
Zhu Yib481de92007-09-25 17:54:57 -07004152 return 0;
4153
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004154 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004155 destroy_workqueue(priv->workqueue);
4156 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004157 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4158 out_release_irq:
4159 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004160 out_disable_msi:
4161 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004162 iwlcore_free_geos(priv);
4163 iwl_free_channel_map(priv);
4164 out_unset_hw_params:
4165 iwl3945_unset_hw_params(priv);
4166 out_eeprom_free:
4167 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004168 out_iounmap:
4169 pci_iounmap(pdev, priv->hw_base);
4170 out_pci_release_regions:
4171 pci_release_regions(pdev);
4172 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004173 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004174 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004175 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004176 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004177 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004178 out:
4179 return err;
4180}
4181
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004182static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004183{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004184 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004185 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004186
4187 if (!priv)
4188 return;
4189
Tomas Winklere1623442009-01-27 14:27:56 -08004190 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004191
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004192 iwl_dbgfs_unregister(priv);
4193
Zhu Yib481de92007-09-25 17:54:57 -07004194 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004195
Wey-Yi Guy5ed540a2011-01-21 15:26:39 -08004196 iwl_leds_exit(priv);
4197
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004198 if (priv->mac80211_registered) {
4199 ieee80211_unregister_hw(priv->hw);
4200 priv->mac80211_registered = 0;
4201 } else {
4202 iwl3945_down(priv);
4203 }
Zhu Yib481de92007-09-25 17:54:57 -07004204
Ben Cahillc166b252009-10-23 13:42:35 -07004205 /*
4206 * Make sure device is reset to low power before unloading driver.
4207 * This may be redundant with iwl_down(), but there are paths to
4208 * run iwl_down() without calling apm_ops.stop(), and there are
4209 * paths to avoid running iwl_down() at all before leaving driver.
4210 * This (inexpensive) call *makes sure* device is reset.
4211 */
Johannes Berg14e8e4a2010-09-22 18:02:10 +02004212 iwl_apm_stop(priv);
Ben Cahillc166b252009-10-23 13:42:35 -07004213
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004214 /* make sure we flush any pending irq or
4215 * tasklet for the driver
4216 */
4217 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004218 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004219 spin_unlock_irqrestore(&priv->lock, flags);
4220
4221 iwl_synchronize_irq(priv);
4222
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004223 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004224
Johannes Bergee525d12010-01-21 06:09:28 -08004225 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004227 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004228
4229 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004230 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004231 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004232
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004233 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004234
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004235 /*netif_stop_queue(dev); */
4236 flush_workqueue(priv->workqueue);
4237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004238 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004239 * priv->workqueue... so we can't take down the workqueue
4240 * until now... */
4241 destroy_workqueue(priv->workqueue);
4242 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004243 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004244
Helmut Schaa26635162009-01-15 09:38:44 +01004245 free_irq(pdev->irq, priv);
4246 pci_disable_msi(pdev);
4247
Zhu Yib481de92007-09-25 17:54:57 -07004248 pci_iounmap(pdev, priv->hw_base);
4249 pci_release_regions(pdev);
4250 pci_disable_device(pdev);
4251 pci_set_drvdata(pdev, NULL);
4252
Samuel Ortize6148912009-01-23 13:45:15 -08004253 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004254 iwlcore_free_geos(priv);
Johannes Berg811ecc92010-04-06 04:12:41 -07004255 kfree(priv->scan_cmd);
Johannes Berg12e934d2010-10-04 05:50:06 -07004256 if (priv->beacon_skb)
4257 dev_kfree_skb(priv->beacon_skb);
Zhu Yib481de92007-09-25 17:54:57 -07004258
4259 ieee80211_free_hw(priv->hw);
4260}
4261
Zhu Yib481de92007-09-25 17:54:57 -07004262
4263/*****************************************************************************
4264 *
4265 * driver and module entry point
4266 *
4267 *****************************************************************************/
4268
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004269static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004270 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004271 .id_table = iwl3945_hw_card_ids,
4272 .probe = iwl3945_pci_probe,
4273 .remove = __devexit_p(iwl3945_pci_remove),
John W. Linvillef60dc012010-10-25 16:12:37 -04004274 .driver.pm = IWL_PM_OPS,
Zhu Yib481de92007-09-25 17:54:57 -07004275};
4276
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004277static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004278{
4279
4280 int ret;
Joe Perchesc96c31e2010-07-26 14:39:58 -07004281 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
4282 pr_info(DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004283
4284 ret = iwl3945_rate_control_register();
4285 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004286 pr_err("Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004287 return ret;
4288 }
4289
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004290 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004291 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004292 pr_err("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004293 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004294 }
Zhu Yib481de92007-09-25 17:54:57 -07004295
4296 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004297
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004298error_register:
4299 iwl3945_rate_control_unregister();
4300 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004301}
4302
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004303static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004304{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004305 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004306 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004307}
4308
Reinette Chatrea0987a82008-12-02 12:14:06 -08004309MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004310
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004311module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004312MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004313module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004314MODULE_PARM_DESC(swcrypto,
4315 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004316#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004317module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004318MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004319#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004320module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4321 int, S_IRUGO);
Wey-Yi Guy72645ef2010-10-06 07:42:43 -07004322MODULE_PARM_DESC(disable_hw_scan,
4323 "disable hardware scanning (default 0) (deprecated)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004324module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004325MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004327module_exit(iwl3945_exit);
4328module_init(iwl3945_init);