blob: aef5f812c7ef5f88ae1d6f53b2289c668884694b [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"
Zhu Yib481de92007-09-25 17:54:57 -070064
Zhu Yib481de92007-09-25 17:54:57 -070065/*
66 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070067 */
68
69#define DRV_DESCRIPTION \
70"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
71
Samuel Ortizd08853a2009-01-23 13:45:17 -080072#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070073#define VD "d"
74#else
75#define VD
76#endif
77
Reinette Chatre81963d62010-01-22 14:22:57 -080078/*
79 * add "s" to indicate spectrum measurement included.
80 * we add it here to be consistent with previous releases in which
81 * this was configurable.
82 */
83#define DRV_VERSION IWLWIFI_VERSION VD "s"
Reinette Chatre1f447802010-01-15 13:43:41 -080084#define DRV_COPYRIGHT "Copyright(c) 2003-2010 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080085#define DRV_AUTHOR "<ilw@linux.intel.com>"
Zhu Yib481de92007-09-25 17:54:57 -070086
87MODULE_DESCRIPTION(DRV_DESCRIPTION);
88MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080089MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070090MODULE_LICENSE("GPL");
91
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080092 /* module parameters */
93struct iwl_mod_params iwl3945_mod_params = {
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080094 .sw_crypto = 1,
Samuel Ortizaf48d042009-01-23 13:45:19 -080095 .restart_fw = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080096 /* the rest are 0 by default */
97};
98
Zhu Yib481de92007-09-25 17:54:57 -070099/**
Samuel Ortiz7e4bca52009-01-23 13:45:18 -0800100 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
101 * @priv: eeprom and antenna fields are used to determine antenna flags
102 *
103 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
104 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
105 *
106 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
107 * IWL_ANTENNA_MAIN - Force MAIN antenna
108 * IWL_ANTENNA_AUX - Force AUX antenna
109 */
110__le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
111{
112 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
113
114 switch (iwl3945_mod_params.antenna) {
115 case IWL_ANTENNA_DIVERSITY:
116 return 0;
117
118 case IWL_ANTENNA_MAIN:
119 if (eeprom->antenna_switch_type)
120 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
121 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
122
123 case IWL_ANTENNA_AUX:
124 if (eeprom->antenna_switch_type)
125 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
126 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
127 }
128
129 /* bad antenna selector value */
130 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
131 iwl3945_mod_params.antenna);
132
133 return 0; /* "diversity" is default if error */
134}
135
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800136static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700137 struct ieee80211_key_conf *keyconf,
138 u8 sta_id)
139{
140 unsigned long flags;
141 __le16 key_flags = 0;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800142 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700143
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800144 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
145 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
146
Johannes Berga194e322010-08-27 08:53:46 -0700147 if (sta_id == priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800148 key_flags |= STA_KEY_MULTICAST_MSK;
149
150 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
151 keyconf->hw_key_idx = keyconf->keyidx;
152 key_flags &= ~STA_KEY_FLG_INVALID;
153
Zhu Yib481de92007-09-25 17:54:57 -0700154 spin_lock_irqsave(&priv->sta_lock, flags);
Johannes Berg97359d12010-08-10 09:46:38 +0200155 priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
Tomas Winklerc587de02009-06-03 11:44:07 -0700156 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
157 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700158 keyconf->keylen);
159
Tomas Winklerc587de02009-06-03 11:44:07 -0700160 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700161 keyconf->keylen);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800162
Tomas Winklerc587de02009-06-03 11:44:07 -0700163 if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800164 == STA_KEY_FLG_NO_ENC)
Tomas Winklerc587de02009-06-03 11:44:07 -0700165 priv->stations[sta_id].sta.key.key_offset =
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800166 iwl_get_free_ucode_key_index(priv);
167 /* else, we are overriding an existing key => no need to allocated room
168 * in uCode. */
169
Tomas Winklerc587de02009-06-03 11:44:07 -0700170 WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800171 "no space for a new key");
172
Tomas Winklerc587de02009-06-03 11:44:07 -0700173 priv->stations[sta_id].sta.key.key_flags = key_flags;
174 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
175 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700176
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800177 IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
178
Tomas Winklerc587de02009-06-03 11:44:07 -0700179 ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800180
Zhu Yib481de92007-09-25 17:54:57 -0700181 spin_unlock_irqrestore(&priv->sta_lock, flags);
182
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800183 return ret;
184}
185
186static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
187 struct ieee80211_key_conf *keyconf,
188 u8 sta_id)
189{
190 return -EOPNOTSUPP;
191}
192
193static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
194 struct ieee80211_key_conf *keyconf,
195 u8 sta_id)
196{
197 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -0700198}
199
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800200static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700201{
202 unsigned long flags;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700203 struct iwl_addsta_cmd sta_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700204
205 spin_lock_irqsave(&priv->sta_lock, flags);
Tomas Winklerc587de02009-06-03 11:44:07 -0700206 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
207 memset(&priv->stations[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +0800208 sizeof(struct iwl4965_keyinfo));
Tomas Winklerc587de02009-06-03 11:44:07 -0700209 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
210 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
211 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700212 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700213 spin_unlock_irqrestore(&priv->sta_lock, flags);
214
Tomas Winklere1623442009-01-27 14:27:56 -0800215 IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
Reinette Chatre9c5ac092010-05-05 02:26:06 -0700216 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
Zhu Yib481de92007-09-25 17:54:57 -0700217}
218
Abhijeet Kolekarfa11d522009-03-11 11:17:54 -0700219static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800220 struct ieee80211_key_conf *keyconf, u8 sta_id)
221{
222 int ret = 0;
223
224 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
225
Johannes Berg97359d12010-08-10 09:46:38 +0200226 switch (keyconf->cipher) {
227 case WLAN_CIPHER_SUITE_CCMP:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800228 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
229 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200230 case WLAN_CIPHER_SUITE_TKIP:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800231 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
232 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200233 case WLAN_CIPHER_SUITE_WEP40:
234 case WLAN_CIPHER_SUITE_WEP104:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800235 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
236 break;
237 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200238 IWL_ERR(priv, "Unknown alg: %s alg=%x\n", __func__,
239 keyconf->cipher);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800240 ret = -EINVAL;
241 }
242
Johannes Berg97359d12010-08-10 09:46:38 +0200243 IWL_DEBUG_WEP(priv, "Set dynamic key: alg=%x len=%d idx=%d sta=%d ret=%d\n",
244 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800245 sta_id, ret);
246
247 return ret;
248}
249
250static int iwl3945_remove_static_key(struct iwl_priv *priv)
251{
252 int ret = -EOPNOTSUPP;
253
254 return ret;
255}
256
257static int iwl3945_set_static_key(struct iwl_priv *priv,
258 struct ieee80211_key_conf *key)
259{
Johannes Berg97359d12010-08-10 09:46:38 +0200260 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
261 key->cipher == WLAN_CIPHER_SUITE_WEP104)
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800262 return -EOPNOTSUPP;
263
Johannes Berg97359d12010-08-10 09:46:38 +0200264 IWL_ERR(priv, "Static key invalid: cipher %x\n", key->cipher);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800265 return -EINVAL;
266}
267
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800268static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700269{
270 struct list_head *element;
271
Tomas Winklere1623442009-01-27 14:27:56 -0800272 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700273 priv->frames_count);
274
275 while (!list_empty(&priv->free_frames)) {
276 element = priv->free_frames.next;
277 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800278 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700279 priv->frames_count--;
280 }
281
282 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800283 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700284 priv->frames_count);
285 priv->frames_count = 0;
286 }
287}
288
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800289static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700290{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800291 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700292 struct list_head *element;
293 if (list_empty(&priv->free_frames)) {
294 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
295 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800296 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700297 return NULL;
298 }
299
300 priv->frames_count++;
301 return frame;
302 }
303
304 element = priv->free_frames.next;
305 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800306 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700307}
308
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800309static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700310{
311 memset(frame, 0, sizeof(*frame));
312 list_add(&frame->list, &priv->free_frames);
313}
314
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800315unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700316 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200317 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700318{
319
Johannes Berg246ed352010-08-23 10:46:32 +0200320 if (!iwl_is_associated(priv, IWL_RXON_CTX_BSS) || !priv->ibss_beacon)
Zhu Yib481de92007-09-25 17:54:57 -0700321 return 0;
322
323 if (priv->ibss_beacon->len > left)
324 return 0;
325
326 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
327
328 return priv->ibss_beacon->len;
329}
330
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800331static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700332{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800333 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700334 unsigned int frame_size;
335 int rc;
336 u8 rate;
337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800338 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700339
340 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800341 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700342 "command.\n");
343 return -ENOMEM;
344 }
345
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800346 rate = iwl_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800348 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700349
Samuel Ortiz518099a2009-01-19 15:30:27 -0800350 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700351 &frame->u.cmd[0]);
352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800353 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700354
355 return rc;
356}
357
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800358static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700359{
Johannes Bergee525d12010-01-21 06:09:28 -0800360 if (priv->_3945.shared_virt)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800361 dma_free_coherent(&priv->pci_dev->dev,
362 sizeof(struct iwl3945_shared),
Johannes Bergee525d12010-01-21 06:09:28 -0800363 priv->_3945.shared_virt,
364 priv->_3945.shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700365}
366
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800367static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200368 struct ieee80211_tx_info *info,
Johannes Bergc2acea82009-07-24 11:13:05 -0700369 struct iwl_device_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700370 struct sk_buff *skb_frag,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800371 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700372{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700373 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Tomas Winklerc587de02009-06-03 11:44:07 -0700374 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -0700375
Johannes Berg97359d12010-08-10 09:46:38 +0200376 tx_cmd->sec_ctl = 0;
377
378 switch (keyinfo->cipher) {
379 case WLAN_CIPHER_SUITE_CCMP:
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700380 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
381 memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
Tomas Winklere1623442009-01-27 14:27:56 -0800382 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -0700383 break;
384
Johannes Berg97359d12010-08-10 09:46:38 +0200385 case WLAN_CIPHER_SUITE_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -0700386 break;
387
Johannes Berg97359d12010-08-10 09:46:38 +0200388 case WLAN_CIPHER_SUITE_WEP104:
389 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
390 /* fall through */
391 case WLAN_CIPHER_SUITE_WEP40:
392 tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +0200393 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -0700394
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700395 memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -0700396
Tomas Winklere1623442009-01-27 14:27:56 -0800397 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +0200398 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -0700399 break;
400
Zhu Yib481de92007-09-25 17:54:57 -0700401 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200402 IWL_ERR(priv, "Unknown encode cipher %x\n", keyinfo->cipher);
Zhu Yib481de92007-09-25 17:54:57 -0700403 break;
404 }
405}
406
407/*
408 * handle build REPLY_TX command notification.
409 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800410static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Johannes Bergc2acea82009-07-24 11:13:05 -0700411 struct iwl_device_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200412 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +0800413 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -0700414{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700415 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
416 __le32 tx_flags = tx_cmd->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700417 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700418
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700419 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200420 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700421 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700422 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700423 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700424 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -0700425 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
426 tx_flags |= TX_CMD_FLG_TSF_MSK;
427 } else {
428 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
429 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
430 }
431
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700432 tx_cmd->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700433 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700434 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
435
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700436 if (ieee80211_is_data_qos(fc)) {
437 u8 *qc = ieee80211_get_qos_ctl(hdr);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700438 tx_cmd->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -0700439 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800440 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700441 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800442 }
Zhu Yib481de92007-09-25 17:54:57 -0700443
Johannes Berg94597ab2010-08-09 10:57:02 -0700444 priv->cfg->ops->utils->tx_cmd_protection(priv, info, fc, &tx_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700445
446 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700447 if (ieee80211_is_mgmt(fc)) {
448 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700449 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -0700450 else
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700451 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700452 } else {
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700453 tx_cmd->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700454 }
Zhu Yib481de92007-09-25 17:54:57 -0700455
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700456 tx_cmd->driver_txop = 0;
457 tx_cmd->tx_flags = tx_flags;
458 tx_cmd->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700459}
460
Zhu Yib481de92007-09-25 17:54:57 -0700461/*
462 * start REPLY_TX command process
463 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800464static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700465{
466 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200467 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700468 struct iwl3945_tx_cmd *tx_cmd;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800469 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800470 struct iwl_queue *q = NULL;
Johannes Bergc2acea82009-07-24 11:13:05 -0700471 struct iwl_device_cmd *out_cmd;
472 struct iwl_cmd_meta *out_meta;
Zhu Yib481de92007-09-25 17:54:57 -0700473 dma_addr_t phys_addr;
474 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800475 int txq_id = skb_get_queue_mapping(skb);
Reinette Chatredf833b12009-04-21 10:55:48 -0700476 u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
Tomas Winkler54dbb522008-05-15 13:54:06 +0800477 u8 id;
478 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700479 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800480 u8 tid = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700481 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700482 u8 wait_write_ptr = 0;
483 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700484
485 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800486 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800487 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700488 goto drop_unlock;
489 }
490
Johannes Berge039fa42008-05-15 12:55:29 +0200491 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800492 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700493 goto drop_unlock;
494 }
495
496 unicast = !is_multicast_ether_addr(hdr->addr1);
497 id = 0;
498
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700499 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700500
Samuel Ortizd08853a2009-01-23 13:45:17 -0800501#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700502 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800503 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700504 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800505 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700506 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800507 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700508#endif
509
Zhu Yib481de92007-09-25 17:54:57 -0700510 spin_unlock_irqrestore(&priv->lock, flags);
511
Harvey Harrison7294ec92008-07-15 18:43:59 -0700512 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800513
Johannes Berg2a87c262010-04-30 11:30:45 -0700514 /* Find index into station table for destination station */
Johannes Berga194e322010-08-27 08:53:46 -0700515 sta_id = iwl_sta_id_or_broadcast(
516 priv, &priv->contexts[IWL_RXON_CTX_BSS],
517 info->control.sta);
Zhu Yib481de92007-09-25 17:54:57 -0700518 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800519 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700520 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700521 goto drop;
522 }
523
Tomas Winklere1623442009-01-27 14:27:56 -0800524 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700525
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700526 if (ieee80211_is_data_qos(fc)) {
Johannes Bergf862a232010-05-03 01:20:52 -0700527 u8 *qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700528 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700529 if (unlikely(tid >= MAX_TID_COUNT))
530 goto drop;
Zhu Yib481de92007-09-25 17:54:57 -0700531 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800532
533 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800534 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700535 q = &txq->q;
536
Zhu Yidc57a302009-12-14 14:12:12 -0800537 if ((iwl_queue_space(q) < q->high_mark))
538 goto drop;
539
Zhu Yib481de92007-09-25 17:54:57 -0700540 spin_lock_irqsave(&priv->lock, flags);
541
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800542 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700543
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800544 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800545 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Johannes Bergff0d91c2010-05-17 02:37:34 -0700546 txq->txb[q->write_ptr].skb = skb;
Johannes Bergc90cbbb2010-08-23 10:46:39 +0200547 txq->txb[q->write_ptr].ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800548
549 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800550 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700551 out_meta = &txq->meta[idx];
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700552 tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700553 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700554 memset(tx_cmd, 0, sizeof(*tx_cmd));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800555
556 /*
557 * Set up the Tx-command (not MAC!) header.
558 * Store the chosen Tx queue and TFD index within the sequence field;
559 * after Tx, uCode's Tx response will return this value so driver can
560 * locate the frame within the tx queue and do post-tx processing.
561 */
Zhu Yib481de92007-09-25 17:54:57 -0700562 out_cmd->hdr.cmd = REPLY_TX;
563 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800564 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800565
566 /* Copy MAC header from skb into command buffer */
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700567 memcpy(tx_cmd->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700568
Reinette Chatredf833b12009-04-21 10:55:48 -0700569
570 if (info->control.hw_key)
571 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
572
573 /* TODO need this for burst mode later on */
574 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
575
576 /* set is_hcca to 0; it probably will never be implemented */
577 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
578
579 /* Total # bytes to be transmitted */
580 len = (u16)skb->len;
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700581 tx_cmd->len = cpu_to_le16(len);
Reinette Chatredf833b12009-04-21 10:55:48 -0700582
Wey-Yi Guy20594eb2009-08-07 15:41:39 -0700583 iwl_dbg_log_tx_data_frame(priv, len, hdr);
Wey-Yi Guy22fdf3c2009-08-07 15:41:40 -0700584 iwl_update_stats(priv, true, fc, len);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700585 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
586 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Reinette Chatredf833b12009-04-21 10:55:48 -0700587
588 if (!ieee80211_has_morefrags(hdr->frame_control)) {
589 txq->need_update = 1;
Reinette Chatredf833b12009-04-21 10:55:48 -0700590 } else {
591 wait_write_ptr = 1;
592 txq->need_update = 0;
593 }
594
Frans Pop91dd6c22010-03-24 14:19:58 -0700595 IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
Reinette Chatredf833b12009-04-21 10:55:48 -0700596 le16_to_cpu(out_cmd->hdr.sequence));
Frans Pop91dd6c22010-03-24 14:19:58 -0700597 IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700598 iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
599 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
Reinette Chatredf833b12009-04-21 10:55:48 -0700600 ieee80211_hdrlen(fc));
601
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800602 /*
603 * Use the first empty entry in this queue's command buffer array
604 * to contain the Tx command and MAC header concatenated together
605 * (payload data will be in another buffer).
606 * Size of this varies, due to varying MAC header length.
607 * If end is not dword aligned, we'll have 2 extra bytes at the end
608 * of the MAC header (device reads on dword boundaries).
609 * We'll tell device about this padding later.
610 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800611 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800612 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700613
614 len_org = len;
615 len = (len + 3) & ~3;
616
617 if (len_org != len)
618 len_org = 1;
619 else
620 len_org = 0;
621
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800622 /* Physical address of this Tx command's header (not MAC header!),
623 * within command buffer array. */
Reinette Chatredf833b12009-04-21 10:55:48 -0700624 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
625 len, PCI_DMA_TODEVICE);
626 /* we do not map meta data ... so we can safely access address to
627 * provide to unmap command*/
FUJITA Tomonori2e724442010-06-03 14:19:20 +0900628 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
629 dma_unmap_len_set(out_meta, len, len);
Zhu Yib481de92007-09-25 17:54:57 -0700630
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800631 /* Add buffer containing Tx command and MAC(!) header to TFD's
632 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800633 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
634 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700635
Zhu Yib481de92007-09-25 17:54:57 -0700636
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800637 /* Set up TFD's 2nd entry to point directly to remainder of skb,
638 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700639 len = skb->len - hdr_len;
640 if (len) {
641 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
642 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800643 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
644 phys_addr, len,
645 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700646 }
647
Zhu Yib481de92007-09-25 17:54:57 -0700648
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800649 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800650 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800651 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700652 spin_unlock_irqrestore(&priv->lock, flags);
653
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800654 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700655 && priv->mac80211_registered) {
656 if (wait_write_ptr) {
657 spin_lock_irqsave(&priv->lock, flags);
658 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800659 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700660 spin_unlock_irqrestore(&priv->lock, flags);
661 }
662
Johannes Berge4e72fb2009-03-23 17:28:42 +0100663 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700664 }
665
666 return 0;
667
668drop_unlock:
669 spin_unlock_irqrestore(&priv->lock, flags);
670drop:
671 return -1;
672}
673
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800674static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700675 struct ieee80211_measurement_params *params,
676 u8 type)
677{
Tomas Winkler600c0e12008-12-19 10:37:04 +0800678 struct iwl_spectrum_cmd spectrum;
Zhu Yi2f301222009-10-09 17:19:45 +0800679 struct iwl_rx_packet *pkt;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800680 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700681 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
682 .data = (void *)&spectrum,
Johannes Bergc2acea82009-07-24 11:13:05 -0700683 .flags = CMD_WANT_SKB,
Zhu Yib481de92007-09-25 17:54:57 -0700684 };
685 u32 add_time = le64_to_cpu(params->start_time);
686 int rc;
687 int spectrum_resp_status;
688 int duration = le16_to_cpu(params->duration);
Johannes Berg246ed352010-08-23 10:46:32 +0200689 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -0700690
Johannes Berg246ed352010-08-23 10:46:32 +0200691 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700692 add_time = iwl_usecs_to_beacons(priv,
Johannes Berge99f1682010-01-19 10:04:28 -0800693 le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
Johannes Berg246ed352010-08-23 10:46:32 +0200694 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700695
696 memset(&spectrum, 0, sizeof(spectrum));
697
698 spectrum.channel_count = cpu_to_le16(1);
699 spectrum.flags =
700 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
701 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
702 cmd.len = sizeof(spectrum);
703 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
704
Johannes Berg246ed352010-08-23 10:46:32 +0200705 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Zhu Yib481de92007-09-25 17:54:57 -0700706 spectrum.start_time =
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700707 iwl_add_beacon_time(priv,
708 priv->_3945.last_beacon_time, add_time,
Johannes Berg246ed352010-08-23 10:46:32 +0200709 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700710 else
711 spectrum.start_time = 0;
712
713 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
714 spectrum.channels[0].channel = params->channel;
715 spectrum.channels[0].type = type;
Johannes Berg246ed352010-08-23 10:46:32 +0200716 if (ctx->active.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700717 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
718 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
719
Samuel Ortiz518099a2009-01-19 15:30:27 -0800720 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700721 if (rc)
722 return rc;
723
Zhu Yi2f301222009-10-09 17:19:45 +0800724 pkt = (struct iwl_rx_packet *)cmd.reply_page;
725 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800726 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700727 rc = -EIO;
728 }
729
Zhu Yi2f301222009-10-09 17:19:45 +0800730 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
Zhu Yib481de92007-09-25 17:54:57 -0700731 switch (spectrum_resp_status) {
732 case 0: /* Command will be handled */
Zhu Yi2f301222009-10-09 17:19:45 +0800733 if (pkt->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800734 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800735 pkt->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700736 priv->measurement_status &= ~MEASUREMENT_READY;
737 }
738 priv->measurement_status |= MEASUREMENT_ACTIVE;
739 rc = 0;
740 break;
741
742 case 1: /* Command will not be handled */
743 rc = -EAGAIN;
744 break;
745 }
746
Zhu Yi64a76b52009-12-10 14:37:21 -0800747 iwl_free_pages(priv, cmd.reply_page);
Zhu Yib481de92007-09-25 17:54:57 -0700748
749 return rc;
750}
Zhu Yib481de92007-09-25 17:54:57 -0700751
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800752static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800753 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700754{
Zhu Yi2f301222009-10-09 17:19:45 +0800755 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800756 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700757 struct delayed_work *pwork;
758
759 palive = &pkt->u.alive_frame;
760
Tomas Winklere1623442009-01-27 14:27:56 -0800761 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700762 "0x%01X 0x%01X\n",
763 palive->is_valid, palive->ver_type,
764 palive->ver_subtype);
765
766 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800767 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800768 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
769 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700770 pwork = &priv->init_alive_start;
771 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800772 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700773 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800774 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700775 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800776 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700777 }
778
779 /* We delay the ALIVE response by 5ms to
780 * give the HW RF Kill time to activate... */
781 if (palive->is_valid == UCODE_VALID_OK)
782 queue_delayed_work(priv->workqueue, pwork,
783 msecs_to_jiffies(5));
784 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800785 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700786}
787
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800788static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800789 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700790{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100791#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800792 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Helmut Schaac7e035a2009-01-19 13:02:15 +0100793#endif
Zhu Yib481de92007-09-25 17:54:57 -0700794
Tomas Winklere1623442009-01-27 14:27:56 -0800795 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700796}
797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800798static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700799{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800800 struct iwl_priv *priv =
801 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700802 struct sk_buff *beacon;
803
804 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200805 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700806
807 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800808 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700809 return;
810 }
811
812 mutex_lock(&priv->mutex);
813 /* new beacon skb is allocated every time; dispose previous.*/
814 if (priv->ibss_beacon)
815 dev_kfree_skb(priv->ibss_beacon);
816
817 priv->ibss_beacon = beacon;
818 mutex_unlock(&priv->mutex);
819
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800820 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700821}
822
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800823static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800824 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700825{
Zhu Yi2f301222009-10-09 17:19:45 +0800826 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800827 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Johannes Berga85d7cc2010-07-31 08:34:10 -0700828#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700829 u8 rate = beacon->beacon_notify_hdr.rate;
830
Tomas Winklere1623442009-01-27 14:27:56 -0800831 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700832 "tsf %d %d rate %d\n",
833 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
834 beacon->beacon_notify_hdr.failure_frame,
835 le32_to_cpu(beacon->ibss_mgr_status),
836 le32_to_cpu(beacon->high_tsf),
837 le32_to_cpu(beacon->low_tsf), rate);
838#endif
839
Johannes Berga85d7cc2010-07-31 08:34:10 -0700840 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
841
Johannes Berg05c914f2008-09-11 00:01:58 +0200842 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700843 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
844 queue_work(priv->workqueue, &priv->beacon_update);
845}
846
Zhu Yib481de92007-09-25 17:54:57 -0700847/* Handle notification from uCode that card's power state is changing
848 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800849static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800850 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700851{
Zhu Yi2f301222009-10-09 17:19:45 +0800852 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700853 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
854 unsigned long status = priv->status;
855
Reinette Chatre4c423a22009-07-17 09:30:26 -0700856 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700857 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
858 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
859
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800860 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700861 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
862
863 if (flags & HW_CARD_DISABLED)
864 set_bit(STATUS_RF_KILL_HW, &priv->status);
865 else
866 clear_bit(STATUS_RF_KILL_HW, &priv->status);
867
868
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800869 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700870
871 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200872 test_bit(STATUS_RF_KILL_HW, &priv->status)))
873 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
874 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700875 else
876 wake_up_interruptible(&priv->wait_command_queue);
877}
878
879/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800880 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700881 *
882 * Setup the RX handlers for each of the reply types sent from the uCode
883 * to the host.
884 *
885 * This function chains into the hardware specific files for them to setup
886 * any hardware specific handlers as well.
887 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800888static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700889{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800890 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
891 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800892 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800893 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Reinette Chatre81963d62010-01-22 14:22:57 -0800894 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
895 iwl_rx_spectrum_measure_notif;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800896 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700897 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800898 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800899 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700900
Ben Cahill9fbab512007-11-29 11:09:47 +0800901 /*
902 * The same handler is used for both the REPLY to a discrete
903 * statistics request from the host as well as for the periodic
904 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700905 */
Abhijeet Kolekar17f36fc2010-04-16 10:03:54 -0700906 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800907 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700908
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800909 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800910 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700911
Ben Cahill9fbab512007-11-29 11:09:47 +0800912 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800913 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700914}
915
Zhu Yib481de92007-09-25 17:54:57 -0700916/************************** RX-FUNCTIONS ****************************/
917/*
918 * Rx theory of operation
919 *
920 * The host allocates 32 DMA target addresses and passes the host address
921 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
922 * 0 to 31
923 *
924 * Rx Queue Indexes
925 * The host/firmware share two index registers for managing the Rx buffers.
926 *
927 * The READ index maps to the first position that the firmware may be writing
928 * to -- the driver can read up to (but not including) this position and get
929 * good data.
930 * The READ index is managed by the firmware once the card is enabled.
931 *
932 * The WRITE index maps to the last position the driver has read from -- the
933 * position preceding WRITE is the last slot the firmware can place a packet.
934 *
935 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
936 * WRITE = READ.
937 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800938 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -0700939 * INDEX position, and WRITE to the last (READ - 1 wrapped)
940 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800941 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -0700942 * and fire the RX interrupt. The driver can then query the READ index and
943 * process as many packets as possible, moving the WRITE index forward as it
944 * resets the Rx queue buffers with new memory.
945 *
946 * The management in the driver is as follows:
947 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
948 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +0800949 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800950 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -0700951 * iwl->rxq is replenished and the READ INDEX is updated (updating the
952 * 'processed' and 'read' driver indexes as well)
953 * + A received packet is processed and handed to the kernel network stack,
954 * detached from the iwl->rxq. The driver 'processed' index is updated.
955 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
956 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
957 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
958 * were enough free buffers and RX_STALLED is set it is cleared.
959 *
960 *
961 * Driver sequence:
962 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800963 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800964 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +0800965 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -0700966 * queue, updates firmware pointers, and updates
967 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800968 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -0700969 *
970 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800971 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -0700972 * READ INDEX, detaching the SKB from the pool.
973 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800974 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -0700975 * slots.
976 * ...
977 *
978 */
979
980/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800981 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -0700982 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800983static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700984 dma_addr_t dma_addr)
985{
986 return cpu_to_le32((u32)dma_addr);
987}
988
989/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800990 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -0700991 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800992 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -0700993 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +0800994 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -0700995 *
996 * This moves the 'write' index forward to catch up with 'processed', and
997 * also updates the memory address in the firmware to reference the new
998 * target buffer.
999 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001000static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001001{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001002 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001003 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001004 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001005 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001006 int write;
Zhu Yib481de92007-09-25 17:54:57 -07001007
1008 spin_lock_irqsave(&rxq->lock, flags);
1009 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001010 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001011 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001012 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001013 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001014 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001015
1016 /* Point to Rx buffer via next RBD in circular buffer */
Zhu Yi2f301222009-10-09 17:19:45 +08001017 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
Zhu Yib481de92007-09-25 17:54:57 -07001018 rxq->queue[rxq->write] = rxb;
1019 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1020 rxq->free_count--;
1021 }
1022 spin_unlock_irqrestore(&rxq->lock, flags);
1023 /* If the pre-allocated buffer pool is dropping low, schedule to
1024 * refill it */
1025 if (rxq->free_count <= RX_LOW_WATERMARK)
1026 queue_work(priv->workqueue, &priv->rx_replenish);
1027
1028
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001029 /* If we've added more space for the firmware to place data, tell it.
1030 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001031 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001032 || (abs(rxq->write - rxq->read) > 7)) {
1033 spin_lock_irqsave(&rxq->lock, flags);
1034 rxq->need_update = 1;
1035 spin_unlock_irqrestore(&rxq->lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001036 iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001037 }
Zhu Yib481de92007-09-25 17:54:57 -07001038}
1039
1040/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001041 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001042 *
1043 * When moving to rx_free an SKB is allocated for the slot.
1044 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001045 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001046 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001047 */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001048static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001049{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001050 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001051 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001052 struct iwl_rx_mem_buffer *rxb;
Zhu Yi2f301222009-10-09 17:19:45 +08001053 struct page *page;
Zhu Yib481de92007-09-25 17:54:57 -07001054 unsigned long flags;
Zhu Yi29b1b262009-10-23 13:42:25 -07001055 gfp_t gfp_mask = priority;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001056
1057 while (1) {
1058 spin_lock_irqsave(&rxq->lock, flags);
1059
1060 if (list_empty(&rxq->rx_used)) {
1061 spin_unlock_irqrestore(&rxq->lock, flags);
1062 return;
1063 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001064 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001065
Reinette Chatref82a9242009-09-17 10:43:56 -07001066 if (rxq->free_count > RX_LOW_WATERMARK)
Zhu Yi29b1b262009-10-23 13:42:25 -07001067 gfp_mask |= __GFP_NOWARN;
Zhu Yi2f301222009-10-09 17:19:45 +08001068
1069 if (priv->hw_params.rx_page_order > 0)
Zhu Yi29b1b262009-10-23 13:42:25 -07001070 gfp_mask |= __GFP_COMP;
Zhu Yi2f301222009-10-09 17:19:45 +08001071
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001072 /* Alloc a new receive buffer */
Zhu Yi29b1b262009-10-23 13:42:25 -07001073 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
Zhu Yi2f301222009-10-09 17:19:45 +08001074 if (!page) {
Zhu Yib481de92007-09-25 17:54:57 -07001075 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001076 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1077 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1078 net_ratelimit())
1079 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1080 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1081 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001082 /* We don't reschedule replenish work here -- we will
1083 * call the restock method and if it still needs
1084 * more buffers it will schedule replenish */
1085 break;
1086 }
Zhu Yi12342c42007-12-20 11:27:32 +08001087
Reinette Chatrede0bd502009-09-11 10:38:12 -07001088 spin_lock_irqsave(&rxq->lock, flags);
1089 if (list_empty(&rxq->rx_used)) {
1090 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001091 __free_pages(page, priv->hw_params.rx_page_order);
Reinette Chatrede0bd502009-09-11 10:38:12 -07001092 return;
1093 }
1094 element = rxq->rx_used.next;
1095 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1096 list_del(element);
1097 spin_unlock_irqrestore(&rxq->lock, flags);
1098
Zhu Yi2f301222009-10-09 17:19:45 +08001099 rxb->page = page;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001100 /* Get physical address of RB/SKB */
Zhu Yi2f301222009-10-09 17:19:45 +08001101 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1102 PAGE_SIZE << priv->hw_params.rx_page_order,
1103 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001104
1105 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001106
Zhu Yib481de92007-09-25 17:54:57 -07001107 list_add_tail(&rxb->list, &rxq->rx_free);
1108 rxq->free_count++;
Zhu Yi2f301222009-10-09 17:19:45 +08001109 priv->alloc_rxb_page++;
1110
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001111 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001112 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001113}
1114
Reinette Chatredf833b12009-04-21 10:55:48 -07001115void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1116{
1117 unsigned long flags;
1118 int i;
1119 spin_lock_irqsave(&rxq->lock, flags);
1120 INIT_LIST_HEAD(&rxq->rx_free);
1121 INIT_LIST_HEAD(&rxq->rx_used);
1122 /* Fill the rx_used queue with _all_ of the Rx buffers */
1123 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1124 /* In the reset function, these buffers may have been allocated
1125 * to an SKB, so we need to unmap and free potential storage */
Zhu Yi2f301222009-10-09 17:19:45 +08001126 if (rxq->pool[i].page != NULL) {
1127 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1128 PAGE_SIZE << priv->hw_params.rx_page_order,
1129 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001130 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001131 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001132 }
1133 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1134 }
1135
1136 /* Set us so that we have processed and used all buffers, but have
1137 * not restocked the Rx queue with fresh buffers */
1138 rxq->read = rxq->write = 0;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001139 rxq->write_actual = 0;
Zhu Yi2f301222009-10-09 17:19:45 +08001140 rxq->free_count = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001141 spin_unlock_irqrestore(&rxq->lock, flags);
1142}
Reinette Chatredf833b12009-04-21 10:55:48 -07001143
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001144void iwl3945_rx_replenish(void *data)
1145{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001146 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001147 unsigned long flags;
1148
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001149 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001150
1151 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001152 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001153 spin_unlock_irqrestore(&priv->lock, flags);
1154}
1155
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001156static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1157{
1158 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1159
1160 iwl3945_rx_queue_restock(priv);
1161}
1162
1163
Reinette Chatredf833b12009-04-21 10:55:48 -07001164/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1165 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1166 * This free routine walks the list of POOL entries and if SKB is set to
1167 * non NULL it is unmapped and freed
1168 */
1169static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1170{
1171 int i;
1172 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
Zhu Yi2f301222009-10-09 17:19:45 +08001173 if (rxq->pool[i].page != NULL) {
1174 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1175 PAGE_SIZE << priv->hw_params.rx_page_order,
1176 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001177 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001178 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001179 }
1180 }
1181
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001182 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
Emmanuel Grumbachd5b25c92010-06-07 13:21:46 -07001183 rxq->bd_dma);
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001184 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1185 rxq->rb_stts, rxq->rb_stts_dma);
Reinette Chatredf833b12009-04-21 10:55:48 -07001186 rxq->bd = NULL;
1187 rxq->rb_stts = NULL;
1188}
Reinette Chatredf833b12009-04-21 10:55:48 -07001189
1190
Zhu Yib481de92007-09-25 17:54:57 -07001191/* Convert linear signal-to-noise ratio into dB */
1192static u8 ratio2dB[100] = {
1193/* 0 1 2 3 4 5 6 7 8 9 */
1194 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1195 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1196 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1197 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1198 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1199 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1200 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1201 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1202 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1203 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1204};
1205
1206/* Calculates a relative dB value from a ratio of linear
1207 * (i.e. not dB) signal levels.
1208 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001209int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001210{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001211 /* 1000:1 or higher just report as 60 dB */
1212 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001213 return 60;
1214
Adrian Bunk221c80c2008-02-02 23:19:01 +02001215 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001216 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001217 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001218 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001219
1220 /* We shouldn't see this */
1221 if (sig_ratio < 1)
1222 return 0;
1223
1224 /* Use table for ratios 1:1 - 99:1 */
1225 return (int)ratio2dB[sig_ratio];
1226}
1227
Zhu Yib481de92007-09-25 17:54:57 -07001228/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001229 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001230 *
1231 * Uses the priv->rx_handlers callback function array to invoke
1232 * the appropriate handlers, including command responses,
1233 * frame-received notifications, and other notifications.
1234 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001235static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001236{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001237 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001238 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001239 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001240 u32 r, i;
1241 int reclaim;
1242 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001243 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001244 u32 count = 8;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001245 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001246
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001247 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1248 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001249 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001250 i = rxq->read;
1251
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001252 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -07001253 total_empty = r - rxq->write_actual;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001254 if (total_empty < 0)
1255 total_empty += RX_QUEUE_SIZE;
1256
1257 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001258 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001259 /* Rx interrupt, but nothing sent from uCode */
1260 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001261 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001262
1263 while (i != r) {
Johannes Bergf4989d92010-05-28 04:08:30 -07001264 int len;
1265
Zhu Yib481de92007-09-25 17:54:57 -07001266 rxb = rxq->queue[i];
1267
Ben Cahill9fbab512007-11-29 11:09:47 +08001268 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001269 * then a bug has been introduced in the queue refilling
1270 * routines -- catch it here */
1271 BUG_ON(rxb == NULL);
1272
1273 rxq->queue[i] = NULL;
1274
Zhu Yi2f301222009-10-09 17:19:45 +08001275 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1276 PAGE_SIZE << priv->hw_params.rx_page_order,
1277 PCI_DMA_FROMDEVICE);
1278 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001279
Johannes Bergf4989d92010-05-28 04:08:30 -07001280 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1281 len += sizeof(u32); /* account for status word */
1282 trace_iwlwifi_dev_rx(priv, pkt, len);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001283
Zhu Yib481de92007-09-25 17:54:57 -07001284 /* Reclaim a command buffer only if this packet is a response
1285 * to a (driver-originated) command.
1286 * If the packet (e.g. Rx frame) originated from uCode,
1287 * there is no command buffer to reclaim.
1288 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1289 * but apparently a few don't get set; catch them here. */
1290 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1291 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1292 (pkt->hdr.cmd != REPLY_TX);
1293
1294 /* Based on type of command response or notification,
1295 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001296 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001297 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001298 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001299 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001300 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -07001301 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001302 } else {
1303 /* No handling needed */
Zhu Yi2f301222009-10-09 17:19:45 +08001304 IWL_DEBUG_RX(priv,
1305 "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001306 r, i, get_cmd_string(pkt->hdr.cmd),
1307 pkt->hdr.cmd);
1308 }
1309
Zhu Yi29b1b262009-10-23 13:42:25 -07001310 /*
1311 * XXX: After here, we should always check rxb->page
1312 * against NULL before touching it or its virtual
1313 * memory (pkt). Because some rx_handler might have
1314 * already taken or freed the pages.
1315 */
1316
Zhu Yib481de92007-09-25 17:54:57 -07001317 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001318 /* Invoke any callbacks, transfer the buffer to caller,
1319 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001320 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001321 if (rxb->page)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001322 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001323 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001324 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001325 }
1326
Zhu Yi73005152009-10-23 13:42:32 -07001327 /* Reuse the page if possible. For notification packets and
1328 * SKBs that fail to Rx correctly, add them back into the
1329 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001330 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001331 if (rxb->page != NULL) {
1332 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1333 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1334 PCI_DMA_FROMDEVICE);
1335 list_add_tail(&rxb->list, &rxq->rx_free);
1336 rxq->free_count++;
1337 } else
1338 list_add_tail(&rxb->list, &rxq->rx_used);
1339
Zhu Yib481de92007-09-25 17:54:57 -07001340 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001341
Zhu Yib481de92007-09-25 17:54:57 -07001342 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001343 /* If there are a lot of unused frames,
1344 * restock the Rx queue so ucode won't assert. */
1345 if (fill_rx) {
1346 count++;
1347 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001348 rxq->read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001349 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001350 count = 0;
1351 }
1352 }
Zhu Yib481de92007-09-25 17:54:57 -07001353 }
1354
1355 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001356 rxq->read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001357 if (fill_rx)
1358 iwl3945_rx_replenish_now(priv);
1359 else
1360 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001361}
1362
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001363/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001364static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001365{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001366 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001367 synchronize_irq(priv->pci_dev->irq);
1368 tasklet_kill(&priv->irq_tasklet);
1369}
1370
Zhu Yib481de92007-09-25 17:54:57 -07001371static const char *desc_lookup(int i)
1372{
1373 switch (i) {
1374 case 1:
1375 return "FAIL";
1376 case 2:
1377 return "BAD_PARAM";
1378 case 3:
1379 return "BAD_CHECKSUM";
1380 case 4:
1381 return "NMI_INTERRUPT";
1382 case 5:
1383 return "SYSASSERT";
1384 case 6:
1385 return "FATAL_ERROR";
1386 }
1387
1388 return "UNKNOWN";
1389}
1390
1391#define ERROR_START_OFFSET (1 * sizeof(u32))
1392#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1393
Reinette Chatreb7a79402009-09-25 14:24:23 -07001394void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001395{
1396 u32 i;
1397 u32 desc, time, count, base, data1;
1398 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001399
1400 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1401
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001402 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001403 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001404 return;
1405 }
1406
Zhu Yib481de92007-09-25 17:54:57 -07001407
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001408 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001409
1410 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001411 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1412 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1413 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001414 }
1415
Winkler, Tomas15b16872008-12-19 10:37:33 +08001416 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001417 "ilink1 nmiPC Line\n");
1418 for (i = ERROR_START_OFFSET;
1419 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1420 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001421 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001422 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001423 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001424 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001425 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001426 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001427 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001428 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001429 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001430 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001431 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001432 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001433 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001434
Winkler, Tomas15b16872008-12-19 10:37:33 +08001435 IWL_ERR(priv,
Jay Sternberg87563712010-06-14 14:40:40 -07001436 "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +08001437 desc_lookup(desc), desc, time, blink1, blink2,
1438 ilink1, ilink2, data1);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001439 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1440 0, blink1, blink2, ilink1, ilink2);
Zhu Yib481de92007-09-25 17:54:57 -07001441 }
Zhu Yib481de92007-09-25 17:54:57 -07001442}
1443
Ben Cahillf58177b2007-11-29 11:09:43 +08001444#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001445
1446/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001447 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001448 *
Zhu Yib481de92007-09-25 17:54:57 -07001449 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001450static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1451 u32 num_events, u32 mode,
1452 int pos, char **buf, size_t bufsz)
Zhu Yib481de92007-09-25 17:54:57 -07001453{
1454 u32 i;
1455 u32 base; /* SRAM byte address of event log header */
1456 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1457 u32 ptr; /* SRAM byte address of log data */
1458 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001459 unsigned long reg_flags;
Zhu Yib481de92007-09-25 17:54:57 -07001460
1461 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001462 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001463
1464 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1465
1466 if (mode == 0)
1467 event_size = 2 * sizeof(u32);
1468 else
1469 event_size = 3 * sizeof(u32);
1470
1471 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1472
Ben Cahille5854472009-11-06 14:52:58 -08001473 /* Make sure device is powered up for SRAM reads */
1474 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1475 iwl_grab_nic_access(priv);
1476
1477 /* Set starting address; reads will auto-increment */
1478 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1479 rmb();
1480
Zhu Yib481de92007-09-25 17:54:57 -07001481 /* "time" is actually "data" for mode 0 (no timestamp).
1482 * place event id # at far right for easier visual parsing. */
1483 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001484 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1485 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001486 if (mode == 0) {
1487 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001488 if (bufsz) {
1489 pos += scnprintf(*buf + pos, bufsz - pos,
1490 "0x%08x:%04u\n",
1491 time, ev);
1492 } else {
1493 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1494 trace_iwlwifi_dev_ucode_event(priv, 0,
1495 time, ev);
1496 }
Winkler, Tomas15b16872008-12-19 10:37:33 +08001497 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001498 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001499 if (bufsz) {
1500 pos += scnprintf(*buf + pos, bufsz - pos,
1501 "%010u:0x%08x:%04u\n",
1502 time, data, ev);
1503 } else {
1504 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1505 time, data, ev);
1506 trace_iwlwifi_dev_ucode_event(priv, time,
1507 data, ev);
1508 }
Zhu Yib481de92007-09-25 17:54:57 -07001509 }
1510 }
Ben Cahille5854472009-11-06 14:52:58 -08001511
1512 /* Allow device to power down */
1513 iwl_release_nic_access(priv);
1514 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001515 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001516}
1517
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001518/**
1519 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1520 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001521static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001522 u32 num_wraps, u32 next_entry,
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001523 u32 size, u32 mode,
1524 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001525{
1526 /*
1527 * display the newest DEFAULT_LOG_ENTRIES entries
1528 * i.e the entries just before the next ont that uCode would fill.
1529 */
1530 if (num_wraps) {
1531 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001532 pos = iwl3945_print_event_log(priv,
1533 capacity - (size - next_entry),
1534 size - next_entry, mode,
1535 pos, buf, bufsz);
1536 pos = iwl3945_print_event_log(priv, 0,
1537 next_entry, mode,
1538 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001539 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001540 pos = iwl3945_print_event_log(priv, next_entry - size,
1541 size, mode,
1542 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001543 } else {
1544 if (next_entry < size)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001545 pos = iwl3945_print_event_log(priv, 0,
1546 next_entry, mode,
1547 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001548 else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001549 pos = iwl3945_print_event_log(priv, next_entry - size,
1550 size, mode,
1551 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001552 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001553 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001554}
1555
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001556#define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1557
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001558int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1559 char **buf, bool display)
Zhu Yib481de92007-09-25 17:54:57 -07001560{
Zhu Yib481de92007-09-25 17:54:57 -07001561 u32 base; /* SRAM byte address of event log header */
1562 u32 capacity; /* event log capacity in # entries */
1563 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1564 u32 num_wraps; /* # times uCode wrapped to top of log */
1565 u32 next_entry; /* index of next entry to be written by uCode */
1566 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001567 int pos = 0;
1568 size_t bufsz = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001569
1570 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001571 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001572 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001573 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001574 }
1575
Zhu Yib481de92007-09-25 17:54:57 -07001576 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001577 capacity = iwl_read_targ_mem(priv, base);
1578 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1579 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1580 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001581
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001582 if (capacity > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001583 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001584 capacity, priv->cfg->max_event_log_size);
1585 capacity = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001586 }
1587
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001588 if (next_entry > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001589 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001590 next_entry, priv->cfg->max_event_log_size);
1591 next_entry = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001592 }
1593
Zhu Yib481de92007-09-25 17:54:57 -07001594 size = num_wraps ? capacity : next_entry;
1595
1596 /* bail out if nothing in log */
1597 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001598 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001599 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001600 }
1601
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001602#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001603 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001604 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1605 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1606#else
1607 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1608 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1609#endif
1610
1611 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1612 size);
Zhu Yib481de92007-09-25 17:54:57 -07001613
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001614#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001615 if (display) {
1616 if (full_log)
1617 bufsz = capacity * 48;
1618 else
1619 bufsz = size * 48;
1620 *buf = kmalloc(bufsz, GFP_KERNEL);
1621 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001622 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001623 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001624 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1625 /* if uCode has wrapped back to top of log,
1626 * start at the oldest entry,
1627 * i.e the next one that uCode would fill.
1628 */
1629 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001630 pos = iwl3945_print_event_log(priv, next_entry,
1631 capacity - next_entry, mode,
1632 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001633
1634 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001635 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1636 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001637 } 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#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001642 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1643 next_entry, size, mode,
1644 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001645#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001646 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001647}
1648
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001649static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001650{
1651 u32 inta, handled = 0;
1652 u32 inta_fh;
1653 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001654#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001655 u32 inta_mask;
1656#endif
1657
1658 spin_lock_irqsave(&priv->lock, flags);
1659
1660 /* Ack/clear/reset pending uCode interrupts.
1661 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1662 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001663 inta = iwl_read32(priv, CSR_INT);
1664 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001665
1666 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1667 * Any new interrupts that happen after this, either while we're
1668 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001669 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1670 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001671
Samuel Ortizd08853a2009-01-23 13:45:17 -08001672#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001673 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001674 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001675 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001676 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001677 inta, inta_mask, inta_fh);
1678 }
1679#endif
1680
Zhu Yi2f301222009-10-09 17:19:45 +08001681 spin_unlock_irqrestore(&priv->lock, flags);
1682
Zhu Yib481de92007-09-25 17:54:57 -07001683 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1684 * atomic, make sure that inta covers all the interrupts that
1685 * we've discovered, even if FH interrupt came in just after
1686 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001687 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001688 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001689 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001690 inta |= CSR_INT_BIT_FH_TX;
1691
1692 /* Now service all interrupt bits discovered above. */
1693 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001694 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001695
1696 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001697 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001698
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001699 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001700 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001701
1702 handled |= CSR_INT_BIT_HW_ERR;
1703
Zhu Yib481de92007-09-25 17:54:57 -07001704 return;
1705 }
1706
Samuel Ortizd08853a2009-01-23 13:45:17 -08001707#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001708 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001709 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001710 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001711 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001712 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001713 priv->isr_stats.sch++;
1714 }
Zhu Yib481de92007-09-25 17:54:57 -07001715
1716 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001717 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001718 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001719 priv->isr_stats.alive++;
1720 }
Zhu Yib481de92007-09-25 17:54:57 -07001721 }
1722#endif
1723 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001724 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001725
Zhu Yib481de92007-09-25 17:54:57 -07001726 /* Error detected by uCode */
1727 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001728 IWL_ERR(priv, "Microcode SW error detected. "
1729 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001730 priv->isr_stats.sw++;
1731 priv->isr_stats.sw_err = inta;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001732 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001733 handled |= CSR_INT_BIT_SW_ERR;
1734 }
1735
1736 /* uCode wakes up after power-down sleep */
1737 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001738 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001739 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001740 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1741 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1742 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1743 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1744 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1745 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001746
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001747 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001748 handled |= CSR_INT_BIT_WAKEUP;
1749 }
1750
1751 /* All uCode command responses, including Tx command responses,
1752 * Rx "responses" (frame-received notification), and other
1753 * notifications from uCode come through here*/
1754 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001755 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001756 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001757 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1758 }
1759
1760 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001761 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001762 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001763
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001764 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001765 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1766 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001767 handled |= CSR_INT_BIT_FH_TX;
1768 }
1769
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001770 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001771 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001772 priv->isr_stats.unhandled++;
1773 }
Zhu Yib481de92007-09-25 17:54:57 -07001774
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001775 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001776 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001777 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001778 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001779 }
1780
1781 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001782 /* only Re-enable if disabled by irq */
1783 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001784 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001785
Samuel Ortizd08853a2009-01-23 13:45:17 -08001786#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001787 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001788 inta = iwl_read32(priv, CSR_INT);
1789 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1790 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001791 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001792 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1793 }
1794#endif
Zhu Yib481de92007-09-25 17:54:57 -07001795}
1796
Abhijeet Kolekar14023642010-06-02 21:15:10 -07001797static int iwl3945_get_single_channel_for_scan(struct iwl_priv *priv,
1798 struct ieee80211_vif *vif,
1799 enum ieee80211_band band,
1800 struct iwl3945_scan_channel *scan_ch)
1801{
1802 const struct ieee80211_supported_band *sband;
1803 u16 passive_dwell = 0;
1804 u16 active_dwell = 0;
1805 int added = 0;
1806 u8 channel = 0;
1807
1808 sband = iwl_get_hw_mode(priv, band);
1809 if (!sband) {
1810 IWL_ERR(priv, "invalid band\n");
1811 return added;
1812 }
1813
1814 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
1815 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
1816
1817 if (passive_dwell <= active_dwell)
1818 passive_dwell = active_dwell + 1;
1819
1820
1821 channel = iwl_get_single_channel_number(priv, band);
1822
1823 if (channel) {
1824 scan_ch->channel = channel;
1825 scan_ch->type = 0; /* passive */
1826 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1827 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1828 /* Set txpower levels to defaults */
1829 scan_ch->tpc.dsp_atten = 110;
1830 if (band == IEEE80211_BAND_5GHZ)
1831 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1832 else
1833 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1834 added++;
1835 } else
1836 IWL_ERR(priv, "no valid channel found\n");
1837 return added;
1838}
1839
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001840static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001841 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001842 u8 is_active, u8 n_probes,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001843 struct iwl3945_scan_channel *scan_ch,
1844 struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07001845{
Johannes Berg4e05c232009-06-19 13:52:44 -07001846 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001847 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001848 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001849 u16 passive_dwell = 0;
1850 u16 active_dwell = 0;
1851 int added, i;
1852
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001853 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001854 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001855 return 0;
1856
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001857 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001858 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Zhu Yib481de92007-09-25 17:54:57 -07001859
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001860 if (passive_dwell <= active_dwell)
1861 passive_dwell = active_dwell + 1;
1862
Johannes Berg4e05c232009-06-19 13:52:44 -07001863 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1864 chan = priv->scan_request->channels[i];
1865
1866 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001867 continue;
1868
Johannes Berg4e05c232009-06-19 13:52:44 -07001869 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001870
Samuel Ortize6148912009-01-23 13:45:15 -08001871 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001872 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001873 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001874 scan_ch->channel);
1875 continue;
1876 }
1877
Zhu Yib481de92007-09-25 17:54:57 -07001878 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1879 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001880 /* If passive , set up for auto-switch
1881 * and use long active_dwell time.
1882 */
1883 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001884 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001885 scan_ch->type = 0; /* passive */
1886 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1887 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1888 } else {
1889 scan_ch->type = 1; /* active */
1890 }
1891
1892 /* Set direct probe bits. These may be used both for active
1893 * scan channels (probes gets sent right away),
1894 * or for passive channels (probes get se sent only after
1895 * hearing clear Rx packet).*/
1896 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1897 if (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 } else {
1900 /* uCode v1 does not allow setting direct probe bits on
1901 * passive channel. */
1902 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001903 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001904 }
Zhu Yib481de92007-09-25 17:54:57 -07001905
Ben Cahill9fbab512007-11-29 11:09:47 +08001906 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001907 scan_ch->tpc.dsp_atten = 110;
1908 /* scan_pwr_info->tpc.dsp_atten; */
1909
1910 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001911 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001912 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1913 else {
1914 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1915 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001916 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001917 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001918 */
1919 }
1920
Tomas Winklere1623442009-01-27 14:27:56 -08001921 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001922 scan_ch->channel,
1923 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1924 (scan_ch->type & 1) ?
1925 active_dwell : passive_dwell);
1926
1927 scan_ch++;
1928 added++;
1929 }
1930
Frans Pop91dd6c22010-03-24 14:19:58 -07001931 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001932 return added;
1933}
1934
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001935static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001936 struct ieee80211_rate *rates)
1937{
1938 int i;
1939
Dan Carpenter8e1a53c2010-03-28 14:55:00 +03001940 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001941 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1942 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1943 rates[i].hw_value_short = i;
1944 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001945 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001946 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001947 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001948 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001949 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001950 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001951 }
Zhu Yib481de92007-09-25 17:54:57 -07001952 }
1953}
1954
Zhu Yib481de92007-09-25 17:54:57 -07001955/******************************************************************************
1956 *
1957 * uCode download functions
1958 *
1959 ******************************************************************************/
1960
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001961static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001962{
Tomas Winkler98c92212008-01-14 17:46:20 -08001963 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1964 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1965 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1966 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1967 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1968 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001969}
1970
1971/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001972 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001973 * looking at all data.
1974 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001975static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001976{
1977 u32 val;
1978 u32 save_len = len;
1979 int rc = 0;
1980 u32 errcnt;
1981
Tomas Winklere1623442009-01-27 14:27:56 -08001982 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001983
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001984 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001985 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07001986
1987 errcnt = 0;
1988 for (; len > 0; len -= sizeof(u32), image++) {
1989 /* read data comes through single port, auto-incr addr */
1990 /* NOTE: Use the debugless read so we don't flood kernel log
1991 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001992 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001993 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001994 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001995 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1996 save_len - len, val, le32_to_cpu(*image));
1997 rc = -EIO;
1998 errcnt++;
1999 if (errcnt >= 20)
2000 break;
2001 }
2002 }
2003
Zhu Yib481de92007-09-25 17:54:57 -07002004
2005 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002006 IWL_DEBUG_INFO(priv,
2007 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002008
2009 return rc;
2010}
2011
2012
2013/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002014 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002015 * using sample data 100 bytes apart. If these sample points are good,
2016 * it's a pretty good bet that everything between them is good, too.
2017 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002018static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002019{
2020 u32 val;
2021 int rc = 0;
2022 u32 errcnt = 0;
2023 u32 i;
2024
Tomas Winklere1623442009-01-27 14:27:56 -08002025 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002026
Zhu Yib481de92007-09-25 17:54:57 -07002027 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2028 /* read data comes through single port, auto-incr addr */
2029 /* NOTE: Use the debugless read so we don't flood kernel log
2030 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002031 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002032 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002033 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002034 if (val != le32_to_cpu(*image)) {
2035#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002036 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002037 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2038 i, val, *image);
2039#endif
2040 rc = -EIO;
2041 errcnt++;
2042 if (errcnt >= 3)
2043 break;
2044 }
2045 }
2046
Zhu Yib481de92007-09-25 17:54:57 -07002047 return rc;
2048}
2049
2050
2051/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002052 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002053 * and verify its contents
2054 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002055static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002056{
2057 __le32 *image;
2058 u32 len;
2059 int rc = 0;
2060
2061 /* Try bootstrap */
2062 image = (__le32 *)priv->ucode_boot.v_addr;
2063 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002064 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002065 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002066 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002067 return 0;
2068 }
2069
2070 /* Try initialize */
2071 image = (__le32 *)priv->ucode_init.v_addr;
2072 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002073 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002074 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002075 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002076 return 0;
2077 }
2078
2079 /* Try runtime/protocol */
2080 image = (__le32 *)priv->ucode_code.v_addr;
2081 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002082 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002083 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002084 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002085 return 0;
2086 }
2087
Winkler, Tomas15b16872008-12-19 10:37:33 +08002088 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002089
Ben Cahill9fbab512007-11-29 11:09:47 +08002090 /* Since nothing seems to match, show first several data entries in
2091 * instruction SRAM, so maybe visual inspection will give a clue.
2092 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002093 image = (__le32 *)priv->ucode_boot.v_addr;
2094 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002095 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002096
2097 return rc;
2098}
2099
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002100static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002101{
2102 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002103 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002104}
2105
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002106#define IWL3945_UCODE_GET(item) \
2107static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
2108{ \
2109 return le32_to_cpu(ucode->u.v1.item); \
2110}
2111
2112static u32 iwl3945_ucode_get_header_size(u32 api_ver)
2113{
Johannes Berg22adba22010-04-28 12:09:14 -07002114 return 24;
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002115}
2116
2117static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
2118{
2119 return (u8 *) ucode->u.v1.data;
2120}
2121
2122IWL3945_UCODE_GET(inst_size);
2123IWL3945_UCODE_GET(data_size);
2124IWL3945_UCODE_GET(init_size);
2125IWL3945_UCODE_GET(init_data_size);
2126IWL3945_UCODE_GET(boot_size);
2127
Zhu Yib481de92007-09-25 17:54:57 -07002128/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002129 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002130 *
2131 * Copy into buffers for card to fetch via bus-mastering
2132 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002133static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002134{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002135 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002136 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002137 const struct firmware *ucode_raw;
2138 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002139 const char *name_pre = priv->cfg->fw_name_pre;
2140 const unsigned int api_max = priv->cfg->ucode_api_max;
2141 const unsigned int api_min = priv->cfg->ucode_api_min;
2142 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002143 u8 *src;
2144 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002145 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002146
2147 /* Ask kernel firmware_class module to get the boot firmware off disk.
2148 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002149 for (index = api_max; index >= api_min; index--) {
2150 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2151 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2152 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002153 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002154 buf, ret);
2155 if (ret == -ENOENT)
2156 continue;
2157 else
2158 goto error;
2159 } else {
2160 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002161 IWL_ERR(priv, "Loaded firmware %s, "
2162 "which is deprecated. "
2163 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002164 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002165 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2166 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002167 buf, ucode_raw->size);
2168 break;
2169 }
Zhu Yib481de92007-09-25 17:54:57 -07002170 }
2171
Reinette Chatrea0987a82008-12-02 12:14:06 -08002172 if (ret < 0)
2173 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002174
2175 /* Make sure that we got at least our header! */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002176 if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002177 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002178 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002179 goto err_release;
2180 }
2181
2182 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002183 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002184
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002185 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002186 api_ver = IWL_UCODE_API(priv->ucode_ver);
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002187 inst_size = iwl3945_ucode_get_inst_size(ucode);
2188 data_size = iwl3945_ucode_get_data_size(ucode);
2189 init_size = iwl3945_ucode_get_init_size(ucode);
2190 init_data_size = iwl3945_ucode_get_init_data_size(ucode);
2191 boot_size = iwl3945_ucode_get_boot_size(ucode);
2192 src = iwl3945_ucode_get_data(ucode);
Zhu Yib481de92007-09-25 17:54:57 -07002193
Reinette Chatrea0987a82008-12-02 12:14:06 -08002194 /* api_ver should match the api version forming part of the
2195 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002196 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002197
2198 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002199 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002200 "Driver supports v%u, firmware is v%u.\n",
2201 api_max, api_ver);
2202 priv->ucode_ver = 0;
2203 ret = -EINVAL;
2204 goto err_release;
2205 }
2206 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002207 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002208 "got %u. New firmware can be obtained "
2209 "from http://www.intellinuxwireless.org.\n",
2210 api_max, api_ver);
2211
Tomas Winkler978785a2008-12-19 10:37:31 +08002212 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2213 IWL_UCODE_MAJOR(priv->ucode_ver),
2214 IWL_UCODE_MINOR(priv->ucode_ver),
2215 IWL_UCODE_API(priv->ucode_ver),
2216 IWL_UCODE_SERIAL(priv->ucode_ver));
2217
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07002218 snprintf(priv->hw->wiphy->fw_version,
2219 sizeof(priv->hw->wiphy->fw_version),
2220 "%u.%u.%u.%u",
2221 IWL_UCODE_MAJOR(priv->ucode_ver),
2222 IWL_UCODE_MINOR(priv->ucode_ver),
2223 IWL_UCODE_API(priv->ucode_ver),
2224 IWL_UCODE_SERIAL(priv->ucode_ver));
2225
Tomas Winklere1623442009-01-27 14:27:56 -08002226 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002227 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002228 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2229 inst_size);
2230 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2231 data_size);
2232 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2233 init_size);
2234 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2235 init_data_size);
2236 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2237 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002238
Reinette Chatrea0987a82008-12-02 12:14:06 -08002239
Zhu Yib481de92007-09-25 17:54:57 -07002240 /* Verify size of file vs. image size info in file's header */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002241 if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002242 inst_size + data_size + init_size +
2243 init_data_size + boot_size) {
2244
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002245 IWL_DEBUG_INFO(priv,
2246 "uCode file size %zd does not match expected size\n",
2247 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002248 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002249 goto err_release;
2250 }
2251
2252 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002253 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002254 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002255 inst_size);
2256 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002257 goto err_release;
2258 }
2259
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002260 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002261 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002262 data_size);
2263 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002264 goto err_release;
2265 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002266 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002267 IWL_DEBUG_INFO(priv,
2268 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002269 init_size);
2270 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002271 goto err_release;
2272 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002273 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002274 IWL_DEBUG_INFO(priv,
2275 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002276 init_data_size);
2277 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002278 goto err_release;
2279 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002280 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002281 IWL_DEBUG_INFO(priv,
2282 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002283 boot_size);
2284 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002285 goto err_release;
2286 }
2287
2288 /* Allocate ucode buffers for card's bus-master loading ... */
2289
2290 /* Runtime instructions and 2 copies of data:
2291 * 1) unmodified from disk
2292 * 2) backup cache for save/restore during power-downs */
2293 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002294 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002295
2296 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002297 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002298
2299 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002300 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002301
2302 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002303 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002304 goto err_pci_alloc;
2305
Tomas Winkler90e759d2007-11-29 11:09:41 +08002306 /* Initialization instructions and data */
2307 if (init_size && init_data_size) {
2308 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002309 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002310
2311 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002312 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002313
2314 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2315 goto err_pci_alloc;
2316 }
2317
2318 /* Bootstrap (instructions only, no data) */
2319 if (boot_size) {
2320 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002321 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002322
2323 if (!priv->ucode_boot.v_addr)
2324 goto err_pci_alloc;
2325 }
2326
Zhu Yib481de92007-09-25 17:54:57 -07002327 /* Copy images into buffers for card's bus-master reads ... */
2328
2329 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002330 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002331 IWL_DEBUG_INFO(priv,
2332 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002333 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002334 src += len;
2335
Tomas Winklere1623442009-01-27 14:27:56 -08002336 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002337 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2338
2339 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002340 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002341 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002342 IWL_DEBUG_INFO(priv,
2343 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002344 memcpy(priv->ucode_data.v_addr, src, len);
2345 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002346 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002347
2348 /* Initialization instructions (3rd block) */
2349 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002350 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002351 IWL_DEBUG_INFO(priv,
2352 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002353 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002354 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002355 }
2356
2357 /* Initialization data (4th block) */
2358 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002359 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002360 IWL_DEBUG_INFO(priv,
2361 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002362 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002363 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002364 }
2365
2366 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002367 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002368 IWL_DEBUG_INFO(priv,
2369 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002370 memcpy(priv->ucode_boot.v_addr, src, len);
2371
2372 /* We have our copies now, allow OS release its copies */
2373 release_firmware(ucode_raw);
2374 return 0;
2375
2376 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002377 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002378 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002379 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002380
2381 err_release:
2382 release_firmware(ucode_raw);
2383
2384 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002385 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002386}
2387
2388
2389/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002390 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002391 *
2392 * Tell initialization uCode where to find runtime uCode.
2393 *
2394 * BSM registers initially contain pointers to initialization uCode.
2395 * We need to replace them to load runtime uCode inst and data,
2396 * and to save runtime data when powering down.
2397 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002398static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002399{
2400 dma_addr_t pinst;
2401 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002402
2403 /* bits 31:0 for 3945 */
2404 pinst = priv->ucode_code.p_addr;
2405 pdata = priv->ucode_data_backup.p_addr;
2406
Zhu Yib481de92007-09-25 17:54:57 -07002407 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002408 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2409 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2410 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002411 priv->ucode_data.len);
2412
Tomas Winklera96a27f2008-10-23 23:48:56 -07002413 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002414 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002415 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002416 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2417
Tomas Winklere1623442009-01-27 14:27:56 -08002418 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002419
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002420 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002421}
2422
2423/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002424 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002425 *
2426 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2427 *
Zhu Yib481de92007-09-25 17:54:57 -07002428 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002429 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002430static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002431{
2432 /* Check alive response for "valid" sign from uCode */
2433 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2434 /* We had an error bringing up the hardware, so take it
2435 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002436 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002437 goto restart;
2438 }
2439
2440 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2441 * This is a paranoid check, because we would not have gotten the
2442 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002443 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002444 /* Runtime instruction load was bad;
2445 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002446 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002447 goto restart;
2448 }
2449
2450 /* Send pointers to protocol/runtime uCode image ... init code will
2451 * load and launch runtime uCode, which will send us another "Alive"
2452 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002453 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002454 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002455 /* Runtime instruction load won't happen;
2456 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002457 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002458 goto restart;
2459 }
2460 return;
2461
2462 restart:
2463 queue_work(priv->workqueue, &priv->restart);
2464}
2465
Zhu Yib481de92007-09-25 17:54:57 -07002466/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002467 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002468 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002469 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002470 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002471static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002472{
Zhu Yib481de92007-09-25 17:54:57 -07002473 int thermal_spin = 0;
2474 u32 rfkill;
Johannes Berg246ed352010-08-23 10:46:32 +02002475 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07002476
Tomas Winklere1623442009-01-27 14:27:56 -08002477 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002478
2479 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2480 /* We had an error bringing up the hardware, so take it
2481 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002482 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002483 goto restart;
2484 }
2485
2486 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2487 * This is a paranoid check, because we would not have gotten the
2488 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002489 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002490 /* Runtime instruction load was bad;
2491 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002492 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002493 goto restart;
2494 }
2495
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002496 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002497 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002498
2499 if (rfkill & 0x1) {
2500 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002501 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002502 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002503 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002504 thermal_spin++;
2505 udelay(10);
2506 }
2507
2508 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002509 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002510 thermal_spin * 10);
2511 } else
2512 set_bit(STATUS_RF_KILL_HW, &priv->status);
2513
Ben Cahill9fbab512007-11-29 11:09:47 +08002514 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002515 set_bit(STATUS_ALIVE, &priv->status);
2516
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08002517 if (priv->cfg->ops->lib->recover_from_tx_stall) {
2518 /* Enable timer to monitor the driver queues */
2519 mod_timer(&priv->monitor_recover,
2520 jiffies +
2521 msecs_to_jiffies(priv->cfg->monitor_recover_period));
2522 }
2523
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002524 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002525 return;
2526
Johannes Berg36d68252008-05-15 12:55:26 +02002527 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002528
Johannes Berg470ab2d2010-01-21 11:23:30 -08002529 priv->active_rate = IWL_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07002530
Ben Cahill4d6ccbf2009-11-13 11:56:29 -08002531 iwl_power_update_mode(priv, true);
Zhu Yib481de92007-09-25 17:54:57 -07002532
Johannes Berg246ed352010-08-23 10:46:32 +02002533 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002534 struct iwl3945_rxon_cmd *active_rxon =
Johannes Berg246ed352010-08-23 10:46:32 +02002535 (struct iwl3945_rxon_cmd *)(&ctx->active);
Zhu Yib481de92007-09-25 17:54:57 -07002536
Johannes Berg246ed352010-08-23 10:46:32 +02002537 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002538 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2539 } else {
2540 /* Initialize our rx_config data */
Johannes Berg1dda6d22010-04-29 04:43:06 -07002541 iwl_connection_init_rx_config(priv, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002542 }
2543
Ben Cahill9fbab512007-11-29 11:09:47 +08002544 /* Configure Bluetooth device coexistence support */
Johannes Berg65b52bd2010-04-13 01:04:31 -07002545 priv->cfg->ops->hcmd->send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002546
2547 /* Configure the adapter for unassociated operation */
Johannes Berg246ed352010-08-23 10:46:32 +02002548 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07002549
Zhu Yib481de92007-09-25 17:54:57 -07002550 iwl3945_reg_txpower_periodic(priv);
2551
Johannes Berge932a602009-10-02 13:44:03 -07002552 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002553
Tomas Winklere1623442009-01-27 14:27:56 -08002554 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002555 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002556 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002557
Zhu Yib481de92007-09-25 17:54:57 -07002558 return;
2559
2560 restart:
2561 queue_work(priv->workqueue, &priv->restart);
2562}
2563
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002564static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002565
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002566static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002567{
2568 unsigned long flags;
2569 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2570 struct ieee80211_conf *conf = NULL;
2571
Tomas Winklere1623442009-01-27 14:27:56 -08002572 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002573
2574 conf = ieee80211_get_hw_conf(priv->hw);
2575
2576 if (!exit_pending)
2577 set_bit(STATUS_EXIT_PENDING, &priv->status);
2578
Stanislaw Gruszkab62177a2010-08-20 15:23:47 +02002579 /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
2580 * to prevent rearm timer */
2581 if (priv->cfg->ops->lib->recover_from_tx_stall)
2582 del_timer_sync(&priv->monitor_recover);
2583
Reinette Chatre7e246192010-02-18 22:58:32 -08002584 /* Station information will now be cleared in device */
Johannes Bergdcef7322010-08-27 08:55:52 -07002585 iwl_clear_ucode_stations(priv, NULL);
Johannes Berga194e322010-08-27 08:53:46 -07002586 iwl_dealloc_bcast_stations(priv);
Johannes Bergdb125c72010-05-07 01:49:15 -07002587 iwl_clear_driver_stations(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002588
2589 /* Unblock any waiting calls */
2590 wake_up_interruptible_all(&priv->wait_command_queue);
2591
Zhu Yib481de92007-09-25 17:54:57 -07002592 /* Wipe out the EXIT_PENDING status bit if we are not actually
2593 * exiting the module */
2594 if (!exit_pending)
2595 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2596
2597 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002598 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002599
2600 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002601 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002602 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002603 spin_unlock_irqrestore(&priv->lock, flags);
2604 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002605
2606 if (priv->mac80211_registered)
2607 ieee80211_stop_queues(priv->hw);
2608
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002609 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002610 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002611 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002612 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2613 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002614 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2615 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002616 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2617 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002618 goto exit;
2619 }
2620
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002621 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002622 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002623 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2624 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002625 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2626 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002627 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002628 STATUS_FW_ERROR |
2629 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2630 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002631
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002632 iwl3945_hw_txq_ctx_stop(priv);
2633 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002634
Ben Cahill309e7312009-11-06 14:53:03 -08002635 /* Power-down device's busmaster DMA clocks */
2636 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002637 udelay(5);
2638
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002639 /* Stop the device, and put it in low power state */
2640 priv->cfg->ops->lib->apm_ops.stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002641
Zhu Yib481de92007-09-25 17:54:57 -07002642 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002643 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002644
2645 if (priv->ibss_beacon)
2646 dev_kfree_skb(priv->ibss_beacon);
2647 priv->ibss_beacon = NULL;
2648
2649 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002650 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002651}
2652
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002653static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002654{
2655 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002656 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002657 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002658
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002659 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002660}
2661
2662#define MAX_HW_RESTARTS 5
2663
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002664static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002665{
2666 int rc, i;
2667
Johannes Berga194e322010-08-27 08:53:46 -07002668 rc = iwl_alloc_bcast_station(priv, &priv->contexts[IWL_RXON_CTX_BSS],
2669 false);
Johannes Berg2c810cc2010-04-29 00:53:29 -07002670 if (rc)
2671 return rc;
2672
Zhu Yib481de92007-09-25 17:54:57 -07002673 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002674 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002675 return -EIO;
2676 }
2677
Reinette Chatree903fbd2008-01-30 22:05:15 -08002678 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002679 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002680 return -EIO;
2681 }
2682
Zhu Yie655b9f2008-01-24 02:19:38 -08002683 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002684 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002685 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2686 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2687 else {
2688 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002689 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2690 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002691 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002692
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002693 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002695 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002696 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002697 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002698 return rc;
2699 }
2700
2701 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002702 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2703 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002704 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2705
2706 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002707 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002708 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002709
2710 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002711 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2712 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002713
2714 /* Copy original ucode data image from disk into backup cache.
2715 * This will be used to initialize the on-board processor's
2716 * data SRAM for a clean start when the runtime program first loads. */
2717 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002718 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002719
Zhu Yie655b9f2008-01-24 02:19:38 -08002720 /* We return success when we resume from suspend and rf_kill is on. */
2721 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2722 return 0;
2723
Zhu Yib481de92007-09-25 17:54:57 -07002724 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2725
Zhu Yib481de92007-09-25 17:54:57 -07002726 /* load bootstrap state machine,
2727 * load bootstrap program into processor's memory,
2728 * prepare to load the "initialize" uCode */
Abhijeet Kolekar75a9a922010-02-26 15:17:01 -08002729 rc = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002730
2731 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002732 IWL_ERR(priv,
2733 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002734 continue;
2735 }
2736
2737 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002738 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002739
Tomas Winklere1623442009-01-27 14:27:56 -08002740 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002741
2742 return 0;
2743 }
2744
2745 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002746 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002747 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002748
2749 /* tried to restart and config the device for as long as our
2750 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002751 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002752 return -EIO;
2753}
2754
2755
2756/*****************************************************************************
2757 *
2758 * Workqueue callbacks
2759 *
2760 *****************************************************************************/
2761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002762static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002763{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002764 struct iwl_priv *priv =
2765 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002766
2767 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2768 return;
2769
2770 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002771 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002772 mutex_unlock(&priv->mutex);
2773}
2774
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002775static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002776{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002777 struct iwl_priv *priv =
2778 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002779
2780 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2781 return;
2782
2783 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002784 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002785 mutex_unlock(&priv->mutex);
2786}
2787
Ben Cahill743cdf12009-10-09 13:20:26 -07002788/*
2789 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2790 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2791 * *is* readable even when device has been SW_RESET into low power mode
2792 * (e.g. during RF KILL).
2793 */
Helmut Schaa26635162009-01-15 09:38:44 +01002794static void iwl3945_rfkill_poll(struct work_struct *data)
2795{
2796 struct iwl_priv *priv =
Johannes Bergee525d12010-01-21 06:09:28 -08002797 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002798 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2799 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2800 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002801
Ben Cahill743cdf12009-10-09 13:20:26 -07002802 if (new_rfkill != old_rfkill) {
2803 if (new_rfkill)
2804 set_bit(STATUS_RF_KILL_HW, &priv->status);
2805 else
2806 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002807
Ben Cahill743cdf12009-10-09 13:20:26 -07002808 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002809
Ben Cahill743cdf12009-10-09 13:20:26 -07002810 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2811 new_rfkill ? "disable radio" : "enable radio");
2812 }
2813
2814 /* Keep this running, even if radio now enabled. This will be
2815 * cancelled in mac_start() if system decides to start again */
Johannes Bergee525d12010-01-21 06:09:28 -08002816 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01002817 round_jiffies_relative(2 * HZ));
2818
2819}
2820
Johannes Berg1dda6d22010-04-29 04:43:06 -07002821void iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07002822{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002823 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002824 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002825 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002826 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002827 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002828 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07002829 struct ieee80211_conf *conf = NULL;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002830 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002831 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002832 bool is_active = false;
Zhu Yib481de92007-09-25 17:54:57 -07002833
2834 conf = ieee80211_get_hw_conf(priv->hw);
2835
Reinette Chatrefbc9f972009-05-15 16:13:46 -07002836 cancel_delayed_work(&priv->scan_check);
2837
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002838 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002839 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002840 goto done;
2841 }
2842
Tomas Winklera96a27f2008-10-23 23:48:56 -07002843 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07002844 * was given the chance to run... */
2845 if (!test_bit(STATUS_SCANNING, &priv->status))
2846 goto done;
2847
2848 /* This should never be called or scheduled if there is currently
2849 * a scan active in the hardware. */
2850 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002851 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
2852 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002853 goto done;
2854 }
2855
2856 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002857 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07002858 goto done;
2859 }
2860
2861 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002862 IWL_DEBUG_HC(priv,
2863 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002864 goto done;
2865 }
2866
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002867 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002868 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07002869 goto done;
2870 }
2871
2872 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002873 IWL_DEBUG_HC(priv,
2874 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002875 goto done;
2876 }
2877
Johannes Berg811ecc92010-04-06 04:12:41 -07002878 if (!priv->scan_cmd) {
2879 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2880 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2881 if (!priv->scan_cmd) {
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002882 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
Zhu Yib481de92007-09-25 17:54:57 -07002883 goto done;
2884 }
2885 }
Johannes Berg811ecc92010-04-06 04:12:41 -07002886 scan = priv->scan_cmd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002887 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002888
2889 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2890 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2891
Johannes Berg246ed352010-08-23 10:46:32 +02002892 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Zhu Yib481de92007-09-25 17:54:57 -07002893 u16 interval = 0;
2894 u32 extra;
2895 u32 suspend_time = 100;
2896 u32 scan_suspend_time = 100;
2897 unsigned long flags;
2898
Tomas Winklere1623442009-01-27 14:27:56 -08002899 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002900
2901 spin_lock_irqsave(&priv->lock, flags);
John W. Linvillea6e492b2010-07-22 15:24:56 -04002902 if (priv->is_internal_short_scan)
2903 interval = 0;
2904 else
2905 interval = vif->bss_conf.beacon_int;
Zhu Yib481de92007-09-25 17:54:57 -07002906 spin_unlock_irqrestore(&priv->lock, flags);
2907
2908 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002909 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002910 if (!interval)
2911 interval = suspend_time;
2912 /*
2913 * suspend time format:
2914 * 0-19: beacon interval in usec (time before exec.)
2915 * 20-23: 0
2916 * 24-31: number of beacons (suspend between channels)
2917 */
2918
2919 extra = (suspend_time / interval) << 24;
2920 scan_suspend_time = 0xFF0FFFFF &
2921 (extra | ((suspend_time % interval) * 1024));
2922
2923 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002924 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002925 scan_suspend_time, interval);
2926 }
2927
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002928 if (priv->is_internal_short_scan) {
2929 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2930 } else if (priv->scan_request->n_ssids) {
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002931 int i, p = 0;
2932 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2933 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2934 /* always does wildcard anyway */
2935 if (!priv->scan_request->ssids[i].ssid_len)
2936 continue;
2937 scan->direct_scan[p].id = WLAN_EID_SSID;
2938 scan->direct_scan[p].len =
2939 priv->scan_request->ssids[i].ssid_len;
2940 memcpy(scan->direct_scan[p].ssid,
2941 priv->scan_request->ssids[i].ssid,
2942 priv->scan_request->ssids[i].ssid_len);
2943 n_probes++;
2944 p++;
2945 }
2946 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002947 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002948 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002949
2950 /* We don't build a direct scan probe request; the uCode will do
2951 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002952 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Johannes Berga194e322010-08-27 08:53:46 -07002953 scan->tx_cmd.sta_id = priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002954 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2955
2956 /* flags + rate selection */
2957
Johannes Berg00700ee2010-04-06 04:12:37 -07002958 switch (priv->scan_band) {
2959 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002960 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2961 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2962 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002963 band = IEEE80211_BAND_2GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002964 break;
2965 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002966 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Bergb097ad22009-04-20 14:36:57 -07002967 /*
2968 * If active scaning is requested but a certain channel
2969 * is marked passive, we can do active scanning if we
2970 * detect transmissions.
2971 */
Johannes Berg96ff5642010-04-30 14:42:15 -07002972 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2973 IWL_GOOD_CRC_TH_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +01002974 band = IEEE80211_BAND_5GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002975 break;
2976 default:
2977 IWL_WARN(priv, "Invalid scan band\n");
Zhu Yib481de92007-09-25 17:54:57 -07002978 goto done;
2979 }
2980
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002981 if (!priv->is_internal_short_scan) {
2982 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002983 iwl_fill_probe_req(priv,
2984 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002985 vif->addr,
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002986 priv->scan_request->ie,
2987 priv->scan_request->ie_len,
2988 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002989 } else {
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002990 /* use bcast addr, will not be transmitted but must be valid */
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002991 scan->tx_cmd.len = cpu_to_le16(
2992 iwl_fill_probe_req(priv,
2993 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002994 iwl_bcast_addr, NULL, 0,
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002995 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2996 }
Zhu Yib481de92007-09-25 17:54:57 -07002997 /* select Rx antennas */
2998 scan->flags |= iwl3945_get_antenna_flags(priv);
2999
Abhijeet Kolekar14023642010-06-02 21:15:10 -07003000 if (priv->is_internal_short_scan) {
3001 scan->channel_count =
3002 iwl3945_get_single_channel_for_scan(priv, vif, band,
3003 (void *)&scan->data[le16_to_cpu(
3004 scan->tx_cmd.len)]);
3005 } else {
3006 scan->channel_count =
3007 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
3008 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
3009 }
Zhu Yib481de92007-09-25 17:54:57 -07003010
Reinette Chatre14b54332008-11-04 12:21:35 -08003011 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08003012 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08003013 goto done;
3014 }
3015
Zhu Yib481de92007-09-25 17:54:57 -07003016 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003017 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003018 cmd.data = scan;
3019 scan->len = cpu_to_le16(cmd.len);
3020
3021 set_bit(STATUS_SCAN_HW, &priv->status);
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08003022 if (iwl_send_cmd_sync(priv, &cmd))
Zhu Yib481de92007-09-25 17:54:57 -07003023 goto done;
3024
3025 queue_delayed_work(priv->workqueue, &priv->scan_check,
3026 IWL_SCAN_CHECK_WATCHDOG);
3027
Zhu Yib481de92007-09-25 17:54:57 -07003028 return;
3029
3030 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003031 /* can not perform scan make sure we clear scanning
3032 * bits from status so next scan request can be performed.
3033 * if we dont clear scanning status bit here all next scan
3034 * will fail
3035 */
3036 clear_bit(STATUS_SCAN_HW, &priv->status);
3037 clear_bit(STATUS_SCANNING, &priv->status);
3038
Ian Schram01ebd062007-10-25 17:15:22 +08003039 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003040 queue_work(priv->workqueue, &priv->scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07003041}
3042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003043static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003044{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003045 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003046
3047 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3048 return;
3049
Johannes Berg19cc1082009-05-08 13:44:36 -07003050 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
3051 mutex_lock(&priv->mutex);
3052 priv->vif = NULL;
3053 priv->is_open = 0;
3054 mutex_unlock(&priv->mutex);
3055 iwl3945_down(priv);
3056 ieee80211_restart_hw(priv->hw);
3057 } else {
3058 iwl3945_down(priv);
Johannes Berg80676512010-01-21 06:07:17 -08003059
3060 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3061 return;
3062
3063 mutex_lock(&priv->mutex);
3064 __iwl3945_up(priv);
3065 mutex_unlock(&priv->mutex);
Johannes Berg19cc1082009-05-08 13:44:36 -07003066 }
Zhu Yib481de92007-09-25 17:54:57 -07003067}
3068
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003069static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003070{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003071 struct iwl_priv *priv =
3072 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003073
3074 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3075 return;
3076
3077 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003078 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003079 mutex_unlock(&priv->mutex);
3080}
3081
Johannes Berg1dda6d22010-04-29 04:43:06 -07003082void iwl3945_post_associate(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003083{
Zhu Yib481de92007-09-25 17:54:57 -07003084 int rc = 0;
3085 struct ieee80211_conf *conf = NULL;
Johannes Berg246ed352010-08-23 10:46:32 +02003086 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003087
Johannes Berg1dda6d22010-04-29 04:43:06 -07003088 if (!vif || !priv->is_open)
3089 return;
3090
3091 if (vif->type == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003092 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003093 return;
3094 }
3095
Tomas Winklere1623442009-01-27 14:27:56 -08003096 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003097 vif->bss_conf.aid, ctx->active.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003098
3099 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3100 return;
3101
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003102 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003103
Zhu Yib481de92007-09-25 17:54:57 -07003104 conf = ieee80211_get_hw_conf(priv->hw);
3105
Johannes Berg246ed352010-08-23 10:46:32 +02003106 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3107 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003108
Johannes Berg948f5a22010-07-29 07:07:51 -07003109 rc = iwl_send_rxon_timing(priv, vif);
Zhu Yib481de92007-09-25 17:54:57 -07003110 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003111 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003112 "Attempting to continue.\n");
3113
Johannes Berg246ed352010-08-23 10:46:32 +02003114 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003115
Johannes Berg246ed352010-08-23 10:46:32 +02003116 ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
Zhu Yib481de92007-09-25 17:54:57 -07003117
Tomas Winklere1623442009-01-27 14:27:56 -08003118 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Johannes Berg1dda6d22010-04-29 04:43:06 -07003119 vif->bss_conf.aid, vif->bss_conf.beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07003120
Johannes Bergc213d742010-05-06 12:21:40 -07003121 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003122 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003123 else
Johannes Berg246ed352010-08-23 10:46:32 +02003124 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003125
Johannes Berg246ed352010-08-23 10:46:32 +02003126 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003127 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003128 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003129 else
Johannes Berg246ed352010-08-23 10:46:32 +02003130 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003131 }
3132
Johannes Berg246ed352010-08-23 10:46:32 +02003133 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003134
Johannes Berg1dda6d22010-04-29 04:43:06 -07003135 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003136 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003137 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003138 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02003139 case NL80211_IFTYPE_ADHOC:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003140 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003141 break;
Zhu Yib481de92007-09-25 17:54:57 -07003142 default:
Johannes Berg1dda6d22010-04-29 04:43:06 -07003143 IWL_ERR(priv, "%s Should not be called in %d mode\n",
3144 __func__, vif->type);
Zhu Yib481de92007-09-25 17:54:57 -07003145 break;
3146 }
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003147}
3148
Zhu Yib481de92007-09-25 17:54:57 -07003149/*****************************************************************************
3150 *
3151 * mac80211 entry point functions
3152 *
3153 *****************************************************************************/
3154
Zhu Yi5a669262008-01-14 17:46:18 -08003155#define UCODE_READY_TIMEOUT (2 * HZ)
3156
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003157static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003158{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003159 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08003160 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003161
Tomas Winklere1623442009-01-27 14:27:56 -08003162 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003163
3164 /* we should be verifying the device is ready to be opened */
3165 mutex_lock(&priv->mutex);
3166
Zhu Yi5a669262008-01-14 17:46:18 -08003167 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3168 * ucode filename and max sizes are card-specific. */
3169
3170 if (!priv->ucode_code.len) {
3171 ret = iwl3945_read_ucode(priv);
3172 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003173 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08003174 mutex_unlock(&priv->mutex);
3175 goto out_release_irq;
3176 }
3177 }
3178
Zhu Yie655b9f2008-01-24 02:19:38 -08003179 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08003180
Zhu Yib481de92007-09-25 17:54:57 -07003181 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08003182
Zhu Yie655b9f2008-01-24 02:19:38 -08003183 if (ret)
3184 goto out_release_irq;
3185
Tomas Winklere1623442009-01-27 14:27:56 -08003186 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003187
Zhu Yi5a669262008-01-14 17:46:18 -08003188 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3189 * mac80211 will not be run successfully. */
3190 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3191 test_bit(STATUS_READY, &priv->status),
3192 UCODE_READY_TIMEOUT);
3193 if (!ret) {
3194 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003195 IWL_ERR(priv,
3196 "Wait for START_ALIVE timeout after %dms.\n",
3197 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08003198 ret = -ETIMEDOUT;
3199 goto out_release_irq;
3200 }
3201 }
3202
Helmut Schaa26635162009-01-15 09:38:44 +01003203 /* ucode is running and will send rfkill notifications,
3204 * no need to poll the killswitch state anymore */
Johannes Bergee525d12010-01-21 06:09:28 -08003205 cancel_delayed_work(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01003206
Johannes Berge932a602009-10-02 13:44:03 -07003207 iwl_led_start(priv);
3208
Zhu Yie655b9f2008-01-24 02:19:38 -08003209 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003210 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003211 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003212
3213out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003214 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003215 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08003216 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003217}
3218
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003219static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003220{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003221 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003222
Tomas Winklere1623442009-01-27 14:27:56 -08003223 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003224
Zhu Yie655b9f2008-01-24 02:19:38 -08003225 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003226 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003227 return;
3228 }
3229
Zhu Yib481de92007-09-25 17:54:57 -07003230 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003231
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003232 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003233 /* stop mac, cancel any scan request and clear
3234 * RXON_FILTER_ASSOC_MSK BIT
3235 */
Zhu Yi5a669262008-01-14 17:46:18 -08003236 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003237 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003238 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003239 }
3240
Zhu Yi5a669262008-01-14 17:46:18 -08003241 iwl3945_down(priv);
3242
3243 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003244
3245 /* start polling the killswitch state again */
Johannes Bergee525d12010-01-21 06:09:28 -08003246 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01003247 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003248
Tomas Winklere1623442009-01-27 14:27:56 -08003249 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003250}
3251
Johannes Berge039fa42008-05-15 12:55:29 +02003252static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003253{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003254 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003255
Tomas Winklere1623442009-01-27 14:27:56 -08003256 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003257
Tomas Winklere1623442009-01-27 14:27:56 -08003258 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003259 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003260
Johannes Berge039fa42008-05-15 12:55:29 +02003261 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003262 dev_kfree_skb_any(skb);
3263
Tomas Winklere1623442009-01-27 14:27:56 -08003264 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003265 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003266}
3267
Johannes Berg1dda6d22010-04-29 04:43:06 -07003268void iwl3945_config_ap(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003269{
Johannes Berg246ed352010-08-23 10:46:32 +02003270 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003271 int rc = 0;
3272
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003273 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003274 return;
3275
3276 /* The following should be done only at AP bring up */
Johannes Berg246ed352010-08-23 10:46:32 +02003277 if (!(iwl_is_associated(priv, IWL_RXON_CTX_BSS))) {
Zhu Yib481de92007-09-25 17:54:57 -07003278
3279 /* RXON - unassoc (to set timing command) */
Johannes Berg246ed352010-08-23 10:46:32 +02003280 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3281 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003282
3283 /* RXON Timing */
Johannes Berg948f5a22010-07-29 07:07:51 -07003284 rc = iwl_send_rxon_timing(priv, vif);
Zhu Yib481de92007-09-25 17:54:57 -07003285 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003286 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003287 "Attempting to continue.\n");
3288
Johannes Berg246ed352010-08-23 10:46:32 +02003289 ctx->staging.assoc_id = 0;
Johannes Berg1dda6d22010-04-29 04:43:06 -07003290
Johannes Bergc213d742010-05-06 12:21:40 -07003291 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003292 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003293 RXON_FLG_SHORT_PREAMBLE_MSK;
3294 else
Johannes Berg246ed352010-08-23 10:46:32 +02003295 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003296 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3297
Johannes Berg246ed352010-08-23 10:46:32 +02003298 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003299 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003300 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003301 RXON_FLG_SHORT_SLOT_MSK;
3302 else
Johannes Berg246ed352010-08-23 10:46:32 +02003303 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003304 ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003305 }
3306 /* restore RXON assoc */
Johannes Berg246ed352010-08-23 10:46:32 +02003307 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
3308 iwlcore_commit_rxon(priv, ctx);
Zhu Yi556f8db2007-09-27 11:27:33 +08003309 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003310 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003311
3312 /* FIXME - we need to add code here to detect a totally new
3313 * configuration, reset the AP, unassoc, rxon timing, assoc,
3314 * clear sta table, add BCAST sta... */
3315}
3316
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003317static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003318 struct ieee80211_vif *vif,
3319 struct ieee80211_sta *sta,
3320 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003321{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003322 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003323 int ret = 0;
3324 u8 sta_id = IWL_INVALID_STATION;
3325 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003326
Tomas Winklere1623442009-01-27 14:27:56 -08003327 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003328
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003329 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003330 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003331 return -EOPNOTSUPP;
3332 }
3333
Johannes Berg246ed352010-08-23 10:46:32 +02003334 static_key = !iwl_is_associated(priv, IWL_RXON_CTX_BSS);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003335
3336 if (!static_key) {
Johannes Berga194e322010-08-27 08:53:46 -07003337 sta_id = iwl_sta_id_or_broadcast(
3338 priv, &priv->contexts[IWL_RXON_CTX_BSS], sta);
Johannes Berg0af8bca2010-04-30 14:08:00 -07003339 if (sta_id == IWL_INVALID_STATION)
3340 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003341 }
3342
3343 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003344 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003345
Zhu Yib481de92007-09-25 17:54:57 -07003346 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003347 case SET_KEY:
3348 if (static_key)
3349 ret = iwl3945_set_static_key(priv, key);
3350 else
3351 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3352 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003353 break;
3354 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003355 if (static_key)
3356 ret = iwl3945_remove_static_key(priv);
3357 else
3358 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3359 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003360 break;
3361 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003362 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003363 }
3364
Johannes Berg72e15d72010-02-19 11:42:32 -08003365 mutex_unlock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003366 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003367
Winkler, Tomas42986792009-01-19 15:30:22 -08003368 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003369}
3370
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003371static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3372 struct ieee80211_vif *vif,
3373 struct ieee80211_sta *sta)
3374{
3375 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07003376 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003377 int ret;
Johannes Bergfd1af152010-04-30 11:30:43 -07003378 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003379 u8 sta_id;
3380
3381 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3382 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003383 mutex_lock(&priv->mutex);
3384 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
3385 sta->addr);
3386 sta_priv->common.sta_id = IWL_INVALID_STATION;
3387
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003388
Johannes Berga194e322010-08-27 08:53:46 -07003389 ret = iwl_add_station_common(priv, &priv->contexts[IWL_RXON_CTX_BSS],
3390 sta->addr, is_ap, &sta->ht_cap, &sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003391 if (ret) {
3392 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3393 sta->addr, ret);
3394 /* Should we return success if return code is EEXIST ? */
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003395 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003396 return ret;
3397 }
3398
Johannes Bergfd1af152010-04-30 11:30:43 -07003399 sta_priv->common.sta_id = sta_id;
3400
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003401 /* Initialize rate scaling */
Frans Pop91dd6c22010-03-24 14:19:58 -07003402 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003403 sta->addr);
3404 iwl3945_rs_rate_init(priv, sta, sta_id);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003405 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003406
3407 return 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003408}
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003409
3410static void iwl3945_configure_filter(struct ieee80211_hw *hw,
3411 unsigned int changed_flags,
3412 unsigned int *total_flags,
3413 u64 multicast)
3414{
3415 struct iwl_priv *priv = hw->priv;
3416 __le32 filter_or = 0, filter_nand = 0;
Johannes Berg246ed352010-08-23 10:46:32 +02003417 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003418
3419#define CHK(test, flag) do { \
3420 if (*total_flags & (test)) \
3421 filter_or |= (flag); \
3422 else \
3423 filter_nand |= (flag); \
3424 } while (0)
3425
3426 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
3427 changed_flags, *total_flags);
3428
3429 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
3430 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
3431 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
3432
3433#undef CHK
3434
3435 mutex_lock(&priv->mutex);
3436
Johannes Berg246ed352010-08-23 10:46:32 +02003437 ctx->staging.filter_flags &= ~filter_nand;
3438 ctx->staging.filter_flags |= filter_or;
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003439
3440 /*
3441 * Committing directly here breaks for some reason,
3442 * but we'll eventually commit the filter flags
3443 * change anyway.
3444 */
3445
3446 mutex_unlock(&priv->mutex);
3447
3448 /*
3449 * Receiving all multicast frames is always enabled by the
3450 * default flags setup in iwl_connection_init_rx_config()
3451 * since we currently do not support programming multicast
3452 * filters into the device.
3453 */
3454 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
3455 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
3456}
3457
3458
Zhu Yib481de92007-09-25 17:54:57 -07003459/*****************************************************************************
3460 *
3461 * sysfs attributes
3462 *
3463 *****************************************************************************/
3464
Samuel Ortizd08853a2009-01-23 13:45:17 -08003465#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003466
3467/*
3468 * The following adds a new attribute to the sysfs representation
3469 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3470 * used for controlling the debug level.
3471 *
3472 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003473 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003474 * The debug_level being managed using sysfs below is a per device debug
3475 * level that is used instead of the global debug level if it (the per
3476 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003477 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003478static ssize_t show_debug_level(struct device *d,
3479 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003480{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003481 struct iwl_priv *priv = dev_get_drvdata(d);
3482 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003483}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003484static ssize_t store_debug_level(struct device *d,
3485 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003486 const char *buf, size_t count)
3487{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003488 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003489 unsigned long val;
3490 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003491
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003492 ret = strict_strtoul(buf, 0, &val);
3493 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003494 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003495 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003496 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003497 if (iwl_alloc_traffic_mem(priv))
3498 IWL_ERR(priv,
3499 "Not enough memory to generate traffic log\n");
3500 }
Zhu Yib481de92007-09-25 17:54:57 -07003501 return strnlen(buf, count);
3502}
3503
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003504static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3505 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003506
Samuel Ortizd08853a2009-01-23 13:45:17 -08003507#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003508
Zhu Yib481de92007-09-25 17:54:57 -07003509static ssize_t show_temperature(struct device *d,
3510 struct device_attribute *attr, char *buf)
3511{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003512 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003513
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003514 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003515 return -EAGAIN;
3516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003517 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003518}
3519
3520static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3521
Zhu Yib481de92007-09-25 17:54:57 -07003522static ssize_t show_tx_power(struct device *d,
3523 struct device_attribute *attr, char *buf)
3524{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003525 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003526 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003527}
3528
3529static ssize_t store_tx_power(struct device *d,
3530 struct device_attribute *attr,
3531 const char *buf, size_t count)
3532{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003533 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003534 char *p = (char *)buf;
3535 u32 val;
3536
3537 val = simple_strtoul(p, &p, 10);
3538 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003539 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003540 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003542
3543 return count;
3544}
3545
3546static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3547
3548static ssize_t show_flags(struct device *d,
3549 struct device_attribute *attr, char *buf)
3550{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003551 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003552 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003553
Johannes Berg246ed352010-08-23 10:46:32 +02003554 return sprintf(buf, "0x%04X\n", ctx->active.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003555}
3556
3557static ssize_t store_flags(struct device *d,
3558 struct device_attribute *attr,
3559 const char *buf, size_t count)
3560{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003561 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003562 u32 flags = simple_strtoul(buf, NULL, 0);
Johannes Berg246ed352010-08-23 10:46:32 +02003563 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003564
3565 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003566 if (le32_to_cpu(ctx->staging.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003567 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003568 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003569 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003570 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003571 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003572 flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003573 ctx->staging.flags = cpu_to_le32(flags);
3574 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003575 }
3576 }
3577 mutex_unlock(&priv->mutex);
3578
3579 return count;
3580}
3581
3582static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3583
3584static ssize_t show_filter_flags(struct device *d,
3585 struct device_attribute *attr, char *buf)
3586{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003587 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003588 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003589
3590 return sprintf(buf, "0x%04X\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003591 le32_to_cpu(ctx->active.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003592}
3593
3594static ssize_t store_filter_flags(struct device *d,
3595 struct device_attribute *attr,
3596 const char *buf, size_t count)
3597{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003598 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003599 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003600 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3601
3602 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003603 if (le32_to_cpu(ctx->staging.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003604 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003605 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003606 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003607 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003608 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003609 "0x%04X\n", filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003610 ctx->staging.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003611 cpu_to_le32(filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003612 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003613 }
3614 }
3615 mutex_unlock(&priv->mutex);
3616
3617 return count;
3618}
3619
3620static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3621 store_filter_flags);
3622
Zhu Yib481de92007-09-25 17:54:57 -07003623static ssize_t show_measurement(struct device *d,
3624 struct device_attribute *attr, char *buf)
3625{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003626 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003627 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003628 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003629 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003630 unsigned long flags;
3631
3632 spin_lock_irqsave(&priv->lock, flags);
3633 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3634 spin_unlock_irqrestore(&priv->lock, flags);
3635 return 0;
3636 }
3637 memcpy(&measure_report, &priv->measure_report, size);
3638 priv->measurement_status = 0;
3639 spin_unlock_irqrestore(&priv->lock, flags);
3640
3641 while (size && (PAGE_SIZE - len)) {
3642 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3643 PAGE_SIZE - len, 1);
3644 len = strlen(buf);
3645 if (PAGE_SIZE - len)
3646 buf[len++] = '\n';
3647
3648 ofs += 16;
3649 size -= min(size, 16U);
3650 }
3651
3652 return len;
3653}
3654
3655static ssize_t store_measurement(struct device *d,
3656 struct device_attribute *attr,
3657 const char *buf, size_t count)
3658{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003659 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003660 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003661 struct ieee80211_measurement_params params = {
Johannes Berg246ed352010-08-23 10:46:32 +02003662 .channel = le16_to_cpu(ctx->active.channel),
Johannes Berge99f1682010-01-19 10:04:28 -08003663 .start_time = cpu_to_le64(priv->_3945.last_tsf),
Zhu Yib481de92007-09-25 17:54:57 -07003664 .duration = cpu_to_le16(1),
3665 };
3666 u8 type = IWL_MEASURE_BASIC;
3667 u8 buffer[32];
3668 u8 channel;
3669
3670 if (count) {
3671 char *p = buffer;
3672 strncpy(buffer, buf, min(sizeof(buffer), count));
3673 channel = simple_strtoul(p, NULL, 0);
3674 if (channel)
3675 params.channel = channel;
3676
3677 p = buffer;
3678 while (*p && *p != ' ')
3679 p++;
3680 if (*p)
3681 type = simple_strtoul(p + 1, NULL, 0);
3682 }
3683
Tomas Winklere1623442009-01-27 14:27:56 -08003684 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003685 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003686 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003687
3688 return count;
3689}
3690
3691static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3692 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003693
Zhu Yib481de92007-09-25 17:54:57 -07003694static ssize_t store_retry_rate(struct device *d,
3695 struct device_attribute *attr,
3696 const char *buf, size_t count)
3697{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003698 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003699
3700 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3701 if (priv->retry_rate <= 0)
3702 priv->retry_rate = 1;
3703
3704 return count;
3705}
3706
3707static ssize_t show_retry_rate(struct device *d,
3708 struct device_attribute *attr, char *buf)
3709{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003710 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003711 return sprintf(buf, "%d", priv->retry_rate);
3712}
3713
3714static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3715 store_retry_rate);
3716
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003717
Zhu Yib481de92007-09-25 17:54:57 -07003718static ssize_t show_channels(struct device *d,
3719 struct device_attribute *attr, char *buf)
3720{
Johannes Berg8318d782008-01-24 19:38:38 +01003721 /* all this shit doesn't belong into sysfs anyway */
3722 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003723}
3724
3725static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3726
Zhu Yib481de92007-09-25 17:54:57 -07003727static ssize_t show_antenna(struct device *d,
3728 struct device_attribute *attr, char *buf)
3729{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003730 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003731
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003732 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003733 return -EAGAIN;
3734
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003735 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003736}
3737
3738static ssize_t store_antenna(struct device *d,
3739 struct device_attribute *attr,
3740 const char *buf, size_t count)
3741{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003742 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003743 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003744
3745 if (count == 0)
3746 return 0;
3747
3748 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003749 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003750 return count;
3751 }
3752
3753 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003754 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003755 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003756 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003757 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003758
3759
3760 return count;
3761}
3762
3763static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3764
3765static ssize_t show_status(struct device *d,
3766 struct device_attribute *attr, char *buf)
3767{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003768 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003769 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003770 return -EAGAIN;
3771 return sprintf(buf, "0x%08x\n", (int)priv->status);
3772}
3773
3774static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3775
3776static ssize_t dump_error_log(struct device *d,
3777 struct device_attribute *attr,
3778 const char *buf, size_t count)
3779{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003780 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003781 char *p = (char *)buf;
3782
3783 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003784 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003785
3786 return strnlen(buf, count);
3787}
3788
3789static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3790
Zhu Yib481de92007-09-25 17:54:57 -07003791/*****************************************************************************
3792 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003793 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003794 *
3795 *****************************************************************************/
3796
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003797static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003798{
Reinette Chatred21050c2009-02-13 11:51:18 -08003799 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003800
3801 init_waitqueue_head(&priv->wait_command_queue);
3802
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003803 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3804 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003805 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003806 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3807 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Johannes Bergee525d12010-01-21 06:09:28 -08003808 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02003809
3810 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003812 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003813
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003814 if (priv->cfg->ops->lib->recover_from_tx_stall) {
3815 init_timer(&priv->monitor_recover);
3816 priv->monitor_recover.data = (unsigned long)priv;
3817 priv->monitor_recover.function =
3818 priv->cfg->ops->lib->recover_from_tx_stall;
3819 }
3820
Zhu Yib481de92007-09-25 17:54:57 -07003821 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003822 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003823}
3824
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003825static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003826{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003827 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003828
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003829 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003830 cancel_delayed_work(&priv->scan_check);
3831 cancel_delayed_work(&priv->alive_start);
Abhijeet Kolekar1e460532010-04-23 14:14:53 -07003832 cancel_work_sync(&priv->start_internal_scan);
Zhu Yib481de92007-09-25 17:54:57 -07003833 cancel_work_sync(&priv->beacon_update);
3834}
3835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003836static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003837 &dev_attr_antenna.attr,
3838 &dev_attr_channels.attr,
3839 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003840 &dev_attr_flags.attr,
3841 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003842 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003843 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003844 &dev_attr_status.attr,
3845 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003846 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003847#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003848 &dev_attr_debug_level.attr,
3849#endif
Zhu Yib481de92007-09-25 17:54:57 -07003850 NULL
3851};
3852
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003853static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003854 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003855 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003856};
3857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003858static struct ieee80211_ops iwl3945_hw_ops = {
3859 .tx = iwl3945_mac_tx,
3860 .start = iwl3945_mac_start,
3861 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003862 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003863 .remove_interface = iwl_mac_remove_interface,
Abhijeet Kolekar48083682009-04-08 11:26:48 -07003864 .config = iwl_mac_config,
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003865 .configure_filter = iwl3945_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003866 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003867 .conf_tx = iwl_mac_conf_tx,
Abhijeet Kolekarbd564262009-04-08 11:26:52 -07003868 .reset_tsf = iwl_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003869 .bss_info_changed = iwl_bss_info_changed,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003870 .hw_scan = iwl_mac_hw_scan,
3871 .sta_add = iwl3945_mac_sta_add,
3872 .sta_remove = iwl_mac_sta_remove,
Johannes Berga85d7cc2010-07-31 08:34:10 -07003873 .tx_last_beacon = iwl_mac_tx_last_beacon,
Zhu Yib481de92007-09-25 17:54:57 -07003874};
3875
Winkler, Tomase52119c2008-12-22 11:31:19 +08003876static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003877{
3878 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003879 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003880
3881 priv->retry_rate = 1;
3882 priv->ibss_beacon = NULL;
3883
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003884 spin_lock_init(&priv->sta_lock);
3885 spin_lock_init(&priv->hcmd_lock);
3886
3887 INIT_LIST_HEAD(&priv->free_frames);
3888
3889 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003890 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003891
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003892 priv->ieee_channels = NULL;
3893 priv->ieee_rates = NULL;
3894 priv->band = IEEE80211_BAND_2GHZ;
3895
3896 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003897 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003898
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003899 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003900
Samuel Ortize6148912009-01-23 13:45:15 -08003901 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3902 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3903 eeprom->version);
3904 ret = -EINVAL;
3905 goto err;
3906 }
3907 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003908 if (ret) {
3909 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3910 goto err;
3911 }
3912
Samuel Ortize6148912009-01-23 13:45:15 -08003913 /* Set up txpower settings in driver for all channels */
3914 if (iwl3945_txpower_set_from_eeprom(priv)) {
3915 ret = -EIO;
3916 goto err_free_channel_map;
3917 }
3918
Samuel Ortiz534166d2009-01-23 13:45:16 -08003919 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003920 if (ret) {
3921 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3922 goto err_free_channel_map;
3923 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003924 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3925
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003926 return 0;
3927
3928err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003929 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003930err:
3931 return ret;
3932}
3933
Johannes Bergdd7a2502010-04-28 23:33:10 -07003934#define IWL3945_MAX_PROBE_REQUEST 200
3935
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003936static int iwl3945_setup_mac(struct iwl_priv *priv)
3937{
3938 int ret;
3939 struct ieee80211_hw *hw = priv->hw;
3940
3941 hw->rate_control_algorithm = "iwl-3945-rs";
3942 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
Johannes Bergfd1af152010-04-30 11:30:43 -07003943 hw->vif_data_size = sizeof(struct iwl_vif_priv);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003944
3945 /* Tell mac80211 our characteristics */
3946 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003947 IEEE80211_HW_SPECTRUM_MGMT;
3948
3949 if (!priv->cfg->broken_powersave)
3950 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3951 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003952
3953 hw->wiphy->interface_modes =
3954 BIT(NL80211_IFTYPE_STATION) |
3955 BIT(NL80211_IFTYPE_ADHOC);
3956
Reinette Chatref6c8f152010-03-12 11:13:26 -08003957 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
Johannes Berg5be83de2009-11-19 00:56:28 +01003958 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003959
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003960 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3961 /* we create the 802.11 header and a zero-length SSID element */
Johannes Bergdd7a2502010-04-28 23:33:10 -07003962 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003963
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003964 /* Default value; 4 EDCA QOS priorities */
3965 hw->queues = 4;
3966
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003967 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3968 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3969 &priv->bands[IEEE80211_BAND_2GHZ];
3970
3971 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3972 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3973 &priv->bands[IEEE80211_BAND_5GHZ];
3974
3975 ret = ieee80211_register_hw(priv->hw);
3976 if (ret) {
3977 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3978 return ret;
3979 }
3980 priv->mac80211_registered = 1;
3981
3982 return 0;
3983}
3984
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003985static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003986{
Johannes Berg246ed352010-08-23 10:46:32 +02003987 int err = 0, i;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003988 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003989 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003990 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003991 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003992 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003993
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003994 /***********************
3995 * 1. Allocating HW data
3996 * ********************/
3997
Zhu Yib481de92007-09-25 17:54:57 -07003998 /* mac80211 allocates memory for this device instance, including
3999 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004000 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07004001 if (hw == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004002 pr_err("Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07004003 err = -ENOMEM;
4004 goto out;
4005 }
Zhu Yib481de92007-09-25 17:54:57 -07004006 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004007 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004008
Johannes Berg13bb9482010-08-23 10:46:33 +02004009 priv->cmd_queue = IWL39_CMD_QUEUE_NUM;
4010
Johannes Berg246ed352010-08-23 10:46:32 +02004011 /* 3945 has only one valid context */
4012 priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
4013
4014 for (i = 0; i < NUM_IWL_RXON_CTX; i++)
4015 priv->contexts[i].ctxid = i;
4016
Johannes Berg8f2d3d22010-08-23 10:46:37 +02004017 priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
4018 priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
4019 priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
Johannes Berg8dfdb9d2010-08-23 10:46:38 +02004020 priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
Johannes Berg8f2d3d22010-08-23 10:46:37 +02004021
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004022 /*
4023 * Disabling hardware scan means that mac80211 will perform scans
4024 * "the hard way", rather than using device's scan.
4025 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004026 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08004027 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004028 iwl3945_hw_ops.hw_scan = NULL;
4029 }
4030
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004031
Tomas Winklere1623442009-01-27 14:27:56 -08004032 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004033 priv->cfg = cfg;
4034 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07004035 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004036
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004037 if (iwl_alloc_traffic_mem(priv))
4038 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07004039
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004040 /***************************
4041 * 2. Initializing PCI bus
4042 * *************************/
John W. Linville1a7123c2010-08-05 14:39:31 -04004043 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4044 PCIE_LINK_STATE_CLKPM);
4045
Zhu Yib481de92007-09-25 17:54:57 -07004046 if (pci_enable_device(pdev)) {
4047 err = -ENODEV;
4048 goto out_ieee80211_free_hw;
4049 }
4050
4051 pci_set_master(pdev);
4052
Yang Hongyang284901a2009-04-06 19:01:15 -07004053 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004054 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004055 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004056 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004057 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004058 goto out_pci_disable_device;
4059 }
4060
4061 pci_set_drvdata(pdev, priv);
4062 err = pci_request_regions(pdev, DRV_NAME);
4063 if (err)
4064 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004065
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004066 /***********************
4067 * 3. Read REV Register
4068 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004069 priv->hw_base = pci_iomap(pdev, 0, 0);
4070 if (!priv->hw_base) {
4071 err = -ENODEV;
4072 goto out_pci_release_regions;
4073 }
4074
Tomas Winklere1623442009-01-27 14:27:56 -08004075 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004076 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004077 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004078
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004079 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4080 * PCI Tx retries from interfering with C3 CPU state */
4081 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004082
Reinette Chatre731a29b2009-12-14 14:12:11 -08004083 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004084 * we should init now
4085 */
4086 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004087 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004088
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004089 /*
4090 * stop and reset the on-board processor just in case it is in a
4091 * strange state ... like being left stranded by a primary kernel
4092 * and this is now the kdump kernel trying to start up
4093 */
4094 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4095
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004096 /***********************
4097 * 4. Read EEPROM
4098 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004099
Zhu Yi5a669262008-01-14 17:46:18 -08004100 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004101 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004102 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004103 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004104 goto out_iounmap;
Zhu Yi5a669262008-01-14 17:46:18 -08004105 }
4106 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004107 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Johannes Berg30eabc12010-05-12 03:33:10 -07004108 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
4109 SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
Zhu Yi5a669262008-01-14 17:46:18 -08004110
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004111 /***********************
4112 * 5. Setup HW Constants
4113 * ********************/
4114 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004115 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004116 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004117 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004118 }
4119
4120 /***********************
4121 * 6. Setup priv
4122 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004123
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004124 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004125 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004126 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004127 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004128 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004129
Tomas Winkler978785a2008-12-19 10:37:31 +08004130 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4131 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004132
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004133 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004134 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004135 * ********************/
4136
4137 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004138 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004139 spin_unlock_irqrestore(&priv->lock, flags);
4140
Helmut Schaa26635162009-01-15 09:38:44 +01004141 pci_enable_msi(priv->pci_dev);
4142
Mohamed Abbasef850d72009-05-22 11:01:50 -07004143 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
4144 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004145 if (err) {
4146 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4147 goto out_disable_msi;
4148 }
4149
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004150 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4151 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004152 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004153 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004154 }
4155
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004156 iwl_set_rxon_channel(priv,
Johannes Berg246ed352010-08-23 10:46:32 +02004157 &priv->bands[IEEE80211_BAND_2GHZ].channels[5],
4158 &priv->contexts[IWL_RXON_CTX_BSS]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004159 iwl3945_setup_deferred_work(priv);
4160 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004161 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004162
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004163 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004164 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004165 * *******************************/
4166
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004167 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004168
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004169 err = iwl3945_setup_mac(priv);
4170 if (err)
4171 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004172
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004173 err = iwl_dbgfs_register(priv, DRV_NAME);
4174 if (err)
4175 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4176
Helmut Schaa26635162009-01-15 09:38:44 +01004177 /* Start monitoring the killswitch */
Johannes Bergee525d12010-01-21 06:09:28 -08004178 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01004179 2 * HZ);
4180
Zhu Yib481de92007-09-25 17:54:57 -07004181 return 0;
4182
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004183 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004184 destroy_workqueue(priv->workqueue);
4185 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004186 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4187 out_release_irq:
4188 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004189 out_disable_msi:
4190 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004191 iwlcore_free_geos(priv);
4192 iwl_free_channel_map(priv);
4193 out_unset_hw_params:
4194 iwl3945_unset_hw_params(priv);
4195 out_eeprom_free:
4196 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004197 out_iounmap:
4198 pci_iounmap(pdev, priv->hw_base);
4199 out_pci_release_regions:
4200 pci_release_regions(pdev);
4201 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004202 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004203 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004204 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004205 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004206 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004207 out:
4208 return err;
4209}
4210
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004211static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004212{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004213 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004214 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004215
4216 if (!priv)
4217 return;
4218
Tomas Winklere1623442009-01-27 14:27:56 -08004219 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004220
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004221 iwl_dbgfs_unregister(priv);
4222
Zhu Yib481de92007-09-25 17:54:57 -07004223 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004224
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004225 if (priv->mac80211_registered) {
4226 ieee80211_unregister_hw(priv->hw);
4227 priv->mac80211_registered = 0;
4228 } else {
4229 iwl3945_down(priv);
4230 }
Zhu Yib481de92007-09-25 17:54:57 -07004231
Ben Cahillc166b252009-10-23 13:42:35 -07004232 /*
4233 * Make sure device is reset to low power before unloading driver.
4234 * This may be redundant with iwl_down(), but there are paths to
4235 * run iwl_down() without calling apm_ops.stop(), and there are
4236 * paths to avoid running iwl_down() at all before leaving driver.
4237 * This (inexpensive) call *makes sure* device is reset.
4238 */
4239 priv->cfg->ops->lib->apm_ops.stop(priv);
4240
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004241 /* make sure we flush any pending irq or
4242 * tasklet for the driver
4243 */
4244 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004245 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004246 spin_unlock_irqrestore(&priv->lock, flags);
4247
4248 iwl_synchronize_irq(priv);
4249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004250 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004251
Johannes Bergee525d12010-01-21 06:09:28 -08004252 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004253
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004254 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004255
4256 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004257 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004258 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004259
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004260 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004261
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004262 /*netif_stop_queue(dev); */
4263 flush_workqueue(priv->workqueue);
4264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004265 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004266 * priv->workqueue... so we can't take down the workqueue
4267 * until now... */
4268 destroy_workqueue(priv->workqueue);
4269 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004270 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004271
Helmut Schaa26635162009-01-15 09:38:44 +01004272 free_irq(pdev->irq, priv);
4273 pci_disable_msi(pdev);
4274
Zhu Yib481de92007-09-25 17:54:57 -07004275 pci_iounmap(pdev, priv->hw_base);
4276 pci_release_regions(pdev);
4277 pci_disable_device(pdev);
4278 pci_set_drvdata(pdev, NULL);
4279
Samuel Ortize6148912009-01-23 13:45:15 -08004280 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004281 iwlcore_free_geos(priv);
Johannes Berg811ecc92010-04-06 04:12:41 -07004282 kfree(priv->scan_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07004283 if (priv->ibss_beacon)
4284 dev_kfree_skb(priv->ibss_beacon);
4285
4286 ieee80211_free_hw(priv->hw);
4287}
4288
Zhu Yib481de92007-09-25 17:54:57 -07004289
4290/*****************************************************************************
4291 *
4292 * driver and module entry point
4293 *
4294 *****************************************************************************/
4295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004296static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004297 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004298 .id_table = iwl3945_hw_card_ids,
4299 .probe = iwl3945_pci_probe,
4300 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004301#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004302 .suspend = iwl_pci_suspend,
4303 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004304#endif
4305};
4306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004307static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004308{
4309
4310 int ret;
Joe Perchesc96c31e2010-07-26 14:39:58 -07004311 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
4312 pr_info(DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004313
4314 ret = iwl3945_rate_control_register();
4315 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004316 pr_err("Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004317 return ret;
4318 }
4319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004320 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004321 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004322 pr_err("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004323 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004324 }
Zhu Yib481de92007-09-25 17:54:57 -07004325
4326 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004327
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004328error_register:
4329 iwl3945_rate_control_unregister();
4330 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004331}
4332
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004333static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004334{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004335 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004336 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004337}
4338
Reinette Chatrea0987a82008-12-02 12:14:06 -08004339MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004340
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004341module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004342MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004343module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004344MODULE_PARM_DESC(swcrypto,
4345 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004346#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004347module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004348MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004349#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004350module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4351 int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004352MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004353module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004354MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4355
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004356module_exit(iwl3945_exit);
4357module_init(iwl3945_init);