blob: 68e624afb9879d28554e522e8857137aea5ff212 [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
Johannes Berg76d04812010-08-23 10:46:47 +0200346 rate = iwl_rate_get_lowest_plcp(priv,
347 &priv->contexts[IWL_RXON_CTX_BSS]);
Zhu Yib481de92007-09-25 17:54:57 -0700348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800349 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700350
Samuel Ortiz518099a2009-01-19 15:30:27 -0800351 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700352 &frame->u.cmd[0]);
353
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800354 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700355
356 return rc;
357}
358
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800359static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700360{
Johannes Bergee525d12010-01-21 06:09:28 -0800361 if (priv->_3945.shared_virt)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800362 dma_free_coherent(&priv->pci_dev->dev,
363 sizeof(struct iwl3945_shared),
Johannes Bergee525d12010-01-21 06:09:28 -0800364 priv->_3945.shared_virt,
365 priv->_3945.shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700366}
367
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800368static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200369 struct ieee80211_tx_info *info,
Johannes Bergc2acea82009-07-24 11:13:05 -0700370 struct iwl_device_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700371 struct sk_buff *skb_frag,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800372 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700373{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700374 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Tomas Winklerc587de02009-06-03 11:44:07 -0700375 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -0700376
Johannes Berg97359d12010-08-10 09:46:38 +0200377 tx_cmd->sec_ctl = 0;
378
379 switch (keyinfo->cipher) {
380 case WLAN_CIPHER_SUITE_CCMP:
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700381 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
382 memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
Tomas Winklere1623442009-01-27 14:27:56 -0800383 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -0700384 break;
385
Johannes Berg97359d12010-08-10 09:46:38 +0200386 case WLAN_CIPHER_SUITE_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -0700387 break;
388
Johannes Berg97359d12010-08-10 09:46:38 +0200389 case WLAN_CIPHER_SUITE_WEP104:
390 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
391 /* fall through */
392 case WLAN_CIPHER_SUITE_WEP40:
393 tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +0200394 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -0700395
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700396 memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -0700397
Tomas Winklere1623442009-01-27 14:27:56 -0800398 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +0200399 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -0700400 break;
401
Zhu Yib481de92007-09-25 17:54:57 -0700402 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200403 IWL_ERR(priv, "Unknown encode cipher %x\n", keyinfo->cipher);
Zhu Yib481de92007-09-25 17:54:57 -0700404 break;
405 }
406}
407
408/*
409 * handle build REPLY_TX command notification.
410 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800411static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Johannes Bergc2acea82009-07-24 11:13:05 -0700412 struct iwl_device_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200413 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +0800414 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -0700415{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700416 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
417 __le32 tx_flags = tx_cmd->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700418 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700419
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700420 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200421 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700422 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700423 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700424 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700425 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -0700426 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
427 tx_flags |= TX_CMD_FLG_TSF_MSK;
428 } else {
429 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
430 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
431 }
432
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700433 tx_cmd->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700434 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700435 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
436
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700437 if (ieee80211_is_data_qos(fc)) {
438 u8 *qc = ieee80211_get_qos_ctl(hdr);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700439 tx_cmd->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -0700440 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800441 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700442 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800443 }
Zhu Yib481de92007-09-25 17:54:57 -0700444
Johannes Berg94597ab2010-08-09 10:57:02 -0700445 priv->cfg->ops->utils->tx_cmd_protection(priv, info, fc, &tx_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700446
447 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700448 if (ieee80211_is_mgmt(fc)) {
449 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700450 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -0700451 else
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700452 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700453 } else {
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700454 tx_cmd->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700455 }
Zhu Yib481de92007-09-25 17:54:57 -0700456
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700457 tx_cmd->driver_txop = 0;
458 tx_cmd->tx_flags = tx_flags;
459 tx_cmd->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700460}
461
Zhu Yib481de92007-09-25 17:54:57 -0700462/*
463 * start REPLY_TX command process
464 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800465static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700466{
467 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200468 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700469 struct iwl3945_tx_cmd *tx_cmd;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800470 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800471 struct iwl_queue *q = NULL;
Johannes Bergc2acea82009-07-24 11:13:05 -0700472 struct iwl_device_cmd *out_cmd;
473 struct iwl_cmd_meta *out_meta;
Zhu Yib481de92007-09-25 17:54:57 -0700474 dma_addr_t phys_addr;
475 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800476 int txq_id = skb_get_queue_mapping(skb);
Reinette Chatredf833b12009-04-21 10:55:48 -0700477 u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
Tomas Winkler54dbb522008-05-15 13:54:06 +0800478 u8 id;
479 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700480 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800481 u8 tid = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700482 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700483 u8 wait_write_ptr = 0;
484 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700485
486 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800487 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800488 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700489 goto drop_unlock;
490 }
491
Johannes Berge039fa42008-05-15 12:55:29 +0200492 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800493 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700494 goto drop_unlock;
495 }
496
497 unicast = !is_multicast_ether_addr(hdr->addr1);
498 id = 0;
499
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700500 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700501
Samuel Ortizd08853a2009-01-23 13:45:17 -0800502#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700503 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800504 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700505 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800506 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700507 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800508 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700509#endif
510
Zhu Yib481de92007-09-25 17:54:57 -0700511 spin_unlock_irqrestore(&priv->lock, flags);
512
Harvey Harrison7294ec92008-07-15 18:43:59 -0700513 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800514
Johannes Berg2a87c262010-04-30 11:30:45 -0700515 /* Find index into station table for destination station */
Johannes Berga194e322010-08-27 08:53:46 -0700516 sta_id = iwl_sta_id_or_broadcast(
517 priv, &priv->contexts[IWL_RXON_CTX_BSS],
518 info->control.sta);
Zhu Yib481de92007-09-25 17:54:57 -0700519 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800520 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700521 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700522 goto drop;
523 }
524
Tomas Winklere1623442009-01-27 14:27:56 -0800525 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700526
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700527 if (ieee80211_is_data_qos(fc)) {
Johannes Bergf862a232010-05-03 01:20:52 -0700528 u8 *qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700529 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700530 if (unlikely(tid >= MAX_TID_COUNT))
531 goto drop;
Zhu Yib481de92007-09-25 17:54:57 -0700532 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800533
534 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800535 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700536 q = &txq->q;
537
Zhu Yidc57a302009-12-14 14:12:12 -0800538 if ((iwl_queue_space(q) < q->high_mark))
539 goto drop;
540
Zhu Yib481de92007-09-25 17:54:57 -0700541 spin_lock_irqsave(&priv->lock, flags);
542
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800543 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700544
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800545 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800546 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Johannes Bergff0d91c2010-05-17 02:37:34 -0700547 txq->txb[q->write_ptr].skb = skb;
Johannes Bergc90cbbb2010-08-23 10:46:39 +0200548 txq->txb[q->write_ptr].ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800549
550 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800551 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700552 out_meta = &txq->meta[idx];
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700553 tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700554 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700555 memset(tx_cmd, 0, sizeof(*tx_cmd));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800556
557 /*
558 * Set up the Tx-command (not MAC!) header.
559 * Store the chosen Tx queue and TFD index within the sequence field;
560 * after Tx, uCode's Tx response will return this value so driver can
561 * locate the frame within the tx queue and do post-tx processing.
562 */
Zhu Yib481de92007-09-25 17:54:57 -0700563 out_cmd->hdr.cmd = REPLY_TX;
564 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800565 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800566
567 /* Copy MAC header from skb into command buffer */
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700568 memcpy(tx_cmd->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700569
Reinette Chatredf833b12009-04-21 10:55:48 -0700570
571 if (info->control.hw_key)
572 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
573
574 /* TODO need this for burst mode later on */
575 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
576
577 /* set is_hcca to 0; it probably will never be implemented */
578 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
579
580 /* Total # bytes to be transmitted */
581 len = (u16)skb->len;
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700582 tx_cmd->len = cpu_to_le16(len);
Reinette Chatredf833b12009-04-21 10:55:48 -0700583
Wey-Yi Guy20594eb2009-08-07 15:41:39 -0700584 iwl_dbg_log_tx_data_frame(priv, len, hdr);
Wey-Yi Guy22fdf3c2009-08-07 15:41:40 -0700585 iwl_update_stats(priv, true, fc, len);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700586 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
587 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Reinette Chatredf833b12009-04-21 10:55:48 -0700588
589 if (!ieee80211_has_morefrags(hdr->frame_control)) {
590 txq->need_update = 1;
Reinette Chatredf833b12009-04-21 10:55:48 -0700591 } else {
592 wait_write_ptr = 1;
593 txq->need_update = 0;
594 }
595
Frans Pop91dd6c22010-03-24 14:19:58 -0700596 IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
Reinette Chatredf833b12009-04-21 10:55:48 -0700597 le16_to_cpu(out_cmd->hdr.sequence));
Frans Pop91dd6c22010-03-24 14:19:58 -0700598 IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700599 iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
600 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
Reinette Chatredf833b12009-04-21 10:55:48 -0700601 ieee80211_hdrlen(fc));
602
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800603 /*
604 * Use the first empty entry in this queue's command buffer array
605 * to contain the Tx command and MAC header concatenated together
606 * (payload data will be in another buffer).
607 * Size of this varies, due to varying MAC header length.
608 * If end is not dword aligned, we'll have 2 extra bytes at the end
609 * of the MAC header (device reads on dword boundaries).
610 * We'll tell device about this padding later.
611 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800612 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800613 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700614
615 len_org = len;
616 len = (len + 3) & ~3;
617
618 if (len_org != len)
619 len_org = 1;
620 else
621 len_org = 0;
622
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800623 /* Physical address of this Tx command's header (not MAC header!),
624 * within command buffer array. */
Reinette Chatredf833b12009-04-21 10:55:48 -0700625 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
626 len, PCI_DMA_TODEVICE);
627 /* we do not map meta data ... so we can safely access address to
628 * provide to unmap command*/
FUJITA Tomonori2e724442010-06-03 14:19:20 +0900629 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
630 dma_unmap_len_set(out_meta, len, len);
Zhu Yib481de92007-09-25 17:54:57 -0700631
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800632 /* Add buffer containing Tx command and MAC(!) header to TFD's
633 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800634 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
635 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700636
Zhu Yib481de92007-09-25 17:54:57 -0700637
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800638 /* Set up TFD's 2nd entry to point directly to remainder of skb,
639 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700640 len = skb->len - hdr_len;
641 if (len) {
642 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
643 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800644 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
645 phys_addr, len,
646 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700647 }
648
Zhu Yib481de92007-09-25 17:54:57 -0700649
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800650 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800651 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800652 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700653 spin_unlock_irqrestore(&priv->lock, flags);
654
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800655 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700656 && priv->mac80211_registered) {
657 if (wait_write_ptr) {
658 spin_lock_irqsave(&priv->lock, flags);
659 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800660 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700661 spin_unlock_irqrestore(&priv->lock, flags);
662 }
663
Johannes Berge4e72fb2009-03-23 17:28:42 +0100664 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700665 }
666
667 return 0;
668
669drop_unlock:
670 spin_unlock_irqrestore(&priv->lock, flags);
671drop:
672 return -1;
673}
674
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800675static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700676 struct ieee80211_measurement_params *params,
677 u8 type)
678{
Tomas Winkler600c0e12008-12-19 10:37:04 +0800679 struct iwl_spectrum_cmd spectrum;
Zhu Yi2f301222009-10-09 17:19:45 +0800680 struct iwl_rx_packet *pkt;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800681 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700682 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
683 .data = (void *)&spectrum,
Johannes Bergc2acea82009-07-24 11:13:05 -0700684 .flags = CMD_WANT_SKB,
Zhu Yib481de92007-09-25 17:54:57 -0700685 };
686 u32 add_time = le64_to_cpu(params->start_time);
687 int rc;
688 int spectrum_resp_status;
689 int duration = le16_to_cpu(params->duration);
Johannes Berg246ed352010-08-23 10:46:32 +0200690 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -0700691
Johannes Berg246ed352010-08-23 10:46:32 +0200692 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700693 add_time = iwl_usecs_to_beacons(priv,
Johannes Berge99f1682010-01-19 10:04:28 -0800694 le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
Johannes Berg246ed352010-08-23 10:46:32 +0200695 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700696
697 memset(&spectrum, 0, sizeof(spectrum));
698
699 spectrum.channel_count = cpu_to_le16(1);
700 spectrum.flags =
701 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
702 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
703 cmd.len = sizeof(spectrum);
704 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
705
Johannes Berg246ed352010-08-23 10:46:32 +0200706 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Zhu Yib481de92007-09-25 17:54:57 -0700707 spectrum.start_time =
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700708 iwl_add_beacon_time(priv,
709 priv->_3945.last_beacon_time, add_time,
Johannes Berg246ed352010-08-23 10:46:32 +0200710 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700711 else
712 spectrum.start_time = 0;
713
714 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
715 spectrum.channels[0].channel = params->channel;
716 spectrum.channels[0].type = type;
Johannes Berg246ed352010-08-23 10:46:32 +0200717 if (ctx->active.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700718 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
719 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
720
Samuel Ortiz518099a2009-01-19 15:30:27 -0800721 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700722 if (rc)
723 return rc;
724
Zhu Yi2f301222009-10-09 17:19:45 +0800725 pkt = (struct iwl_rx_packet *)cmd.reply_page;
726 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800727 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700728 rc = -EIO;
729 }
730
Zhu Yi2f301222009-10-09 17:19:45 +0800731 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
Zhu Yib481de92007-09-25 17:54:57 -0700732 switch (spectrum_resp_status) {
733 case 0: /* Command will be handled */
Zhu Yi2f301222009-10-09 17:19:45 +0800734 if (pkt->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800735 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800736 pkt->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700737 priv->measurement_status &= ~MEASUREMENT_READY;
738 }
739 priv->measurement_status |= MEASUREMENT_ACTIVE;
740 rc = 0;
741 break;
742
743 case 1: /* Command will not be handled */
744 rc = -EAGAIN;
745 break;
746 }
747
Zhu Yi64a76b52009-12-10 14:37:21 -0800748 iwl_free_pages(priv, cmd.reply_page);
Zhu Yib481de92007-09-25 17:54:57 -0700749
750 return rc;
751}
Zhu Yib481de92007-09-25 17:54:57 -0700752
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800753static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800754 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700755{
Zhu Yi2f301222009-10-09 17:19:45 +0800756 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800757 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700758 struct delayed_work *pwork;
759
760 palive = &pkt->u.alive_frame;
761
Tomas Winklere1623442009-01-27 14:27:56 -0800762 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700763 "0x%01X 0x%01X\n",
764 palive->is_valid, palive->ver_type,
765 palive->ver_subtype);
766
767 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800768 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800769 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
770 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700771 pwork = &priv->init_alive_start;
772 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800773 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700774 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800775 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700776 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800777 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700778 }
779
780 /* We delay the ALIVE response by 5ms to
781 * give the HW RF Kill time to activate... */
782 if (palive->is_valid == UCODE_VALID_OK)
783 queue_delayed_work(priv->workqueue, pwork,
784 msecs_to_jiffies(5));
785 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800786 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700787}
788
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800789static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800790 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700791{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100792#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800793 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Helmut Schaac7e035a2009-01-19 13:02:15 +0100794#endif
Zhu Yib481de92007-09-25 17:54:57 -0700795
Tomas Winklere1623442009-01-27 14:27:56 -0800796 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700797}
798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800799static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700800{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800801 struct iwl_priv *priv =
802 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700803 struct sk_buff *beacon;
804
805 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg8bd413e2010-08-23 10:46:40 +0200806 beacon = ieee80211_beacon_get(priv->hw,
807 priv->contexts[IWL_RXON_CTX_BSS].vif);
Zhu Yib481de92007-09-25 17:54:57 -0700808
809 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800810 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700811 return;
812 }
813
814 mutex_lock(&priv->mutex);
815 /* new beacon skb is allocated every time; dispose previous.*/
816 if (priv->ibss_beacon)
817 dev_kfree_skb(priv->ibss_beacon);
818
819 priv->ibss_beacon = beacon;
820 mutex_unlock(&priv->mutex);
821
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800822 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700823}
824
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800825static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800826 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700827{
Zhu Yi2f301222009-10-09 17:19:45 +0800828 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800829 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Johannes Berga85d7cc2010-07-31 08:34:10 -0700830#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700831 u8 rate = beacon->beacon_notify_hdr.rate;
832
Tomas Winklere1623442009-01-27 14:27:56 -0800833 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700834 "tsf %d %d rate %d\n",
835 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
836 beacon->beacon_notify_hdr.failure_frame,
837 le32_to_cpu(beacon->ibss_mgr_status),
838 le32_to_cpu(beacon->high_tsf),
839 le32_to_cpu(beacon->low_tsf), rate);
840#endif
841
Johannes Berga85d7cc2010-07-31 08:34:10 -0700842 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
843
Johannes Berg05c914f2008-09-11 00:01:58 +0200844 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700845 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
846 queue_work(priv->workqueue, &priv->beacon_update);
847}
848
Zhu Yib481de92007-09-25 17:54:57 -0700849/* Handle notification from uCode that card's power state is changing
850 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800851static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800852 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700853{
Zhu Yi2f301222009-10-09 17:19:45 +0800854 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700855 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
856 unsigned long status = priv->status;
857
Reinette Chatre4c423a22009-07-17 09:30:26 -0700858 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700859 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
860 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
861
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800862 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700863 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
864
865 if (flags & HW_CARD_DISABLED)
866 set_bit(STATUS_RF_KILL_HW, &priv->status);
867 else
868 clear_bit(STATUS_RF_KILL_HW, &priv->status);
869
870
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800871 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700872
873 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200874 test_bit(STATUS_RF_KILL_HW, &priv->status)))
875 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
876 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700877 else
878 wake_up_interruptible(&priv->wait_command_queue);
879}
880
881/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800882 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700883 *
884 * Setup the RX handlers for each of the reply types sent from the uCode
885 * to the host.
886 *
887 * This function chains into the hardware specific files for them to setup
888 * any hardware specific handlers as well.
889 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800890static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700891{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800892 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
893 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800894 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800895 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Reinette Chatre81963d62010-01-22 14:22:57 -0800896 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
897 iwl_rx_spectrum_measure_notif;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800898 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700899 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800900 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800901 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700902
Ben Cahill9fbab512007-11-29 11:09:47 +0800903 /*
904 * The same handler is used for both the REPLY to a discrete
905 * statistics request from the host as well as for the periodic
906 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700907 */
Abhijeet Kolekar17f36fc2010-04-16 10:03:54 -0700908 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800909 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700910
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800911 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800912 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700913
Ben Cahill9fbab512007-11-29 11:09:47 +0800914 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800915 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700916}
917
Zhu Yib481de92007-09-25 17:54:57 -0700918/************************** RX-FUNCTIONS ****************************/
919/*
920 * Rx theory of operation
921 *
922 * The host allocates 32 DMA target addresses and passes the host address
923 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
924 * 0 to 31
925 *
926 * Rx Queue Indexes
927 * The host/firmware share two index registers for managing the Rx buffers.
928 *
929 * The READ index maps to the first position that the firmware may be writing
930 * to -- the driver can read up to (but not including) this position and get
931 * good data.
932 * The READ index is managed by the firmware once the card is enabled.
933 *
934 * The WRITE index maps to the last position the driver has read from -- the
935 * position preceding WRITE is the last slot the firmware can place a packet.
936 *
937 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
938 * WRITE = READ.
939 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800940 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -0700941 * INDEX position, and WRITE to the last (READ - 1 wrapped)
942 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800943 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -0700944 * and fire the RX interrupt. The driver can then query the READ index and
945 * process as many packets as possible, moving the WRITE index forward as it
946 * resets the Rx queue buffers with new memory.
947 *
948 * The management in the driver is as follows:
949 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
950 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +0800951 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800952 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -0700953 * iwl->rxq is replenished and the READ INDEX is updated (updating the
954 * 'processed' and 'read' driver indexes as well)
955 * + A received packet is processed and handed to the kernel network stack,
956 * detached from the iwl->rxq. The driver 'processed' index is updated.
957 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
958 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
959 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
960 * were enough free buffers and RX_STALLED is set it is cleared.
961 *
962 *
963 * Driver sequence:
964 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800965 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800966 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +0800967 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -0700968 * queue, updates firmware pointers, and updates
969 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800970 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -0700971 *
972 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800973 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -0700974 * READ INDEX, detaching the SKB from the pool.
975 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800976 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -0700977 * slots.
978 * ...
979 *
980 */
981
982/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800983 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -0700984 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800985static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700986 dma_addr_t dma_addr)
987{
988 return cpu_to_le32((u32)dma_addr);
989}
990
991/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800992 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -0700993 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800994 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -0700995 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +0800996 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -0700997 *
998 * This moves the 'write' index forward to catch up with 'processed', and
999 * also updates the memory address in the firmware to reference the new
1000 * target buffer.
1001 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001002static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001003{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001004 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001005 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001006 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001007 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001008 int write;
Zhu Yib481de92007-09-25 17:54:57 -07001009
1010 spin_lock_irqsave(&rxq->lock, flags);
1011 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001012 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001013 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001014 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001015 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001016 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001017
1018 /* Point to Rx buffer via next RBD in circular buffer */
Zhu Yi2f301222009-10-09 17:19:45 +08001019 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
Zhu Yib481de92007-09-25 17:54:57 -07001020 rxq->queue[rxq->write] = rxb;
1021 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1022 rxq->free_count--;
1023 }
1024 spin_unlock_irqrestore(&rxq->lock, flags);
1025 /* If the pre-allocated buffer pool is dropping low, schedule to
1026 * refill it */
1027 if (rxq->free_count <= RX_LOW_WATERMARK)
1028 queue_work(priv->workqueue, &priv->rx_replenish);
1029
1030
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001031 /* If we've added more space for the firmware to place data, tell it.
1032 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001033 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001034 || (abs(rxq->write - rxq->read) > 7)) {
1035 spin_lock_irqsave(&rxq->lock, flags);
1036 rxq->need_update = 1;
1037 spin_unlock_irqrestore(&rxq->lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001038 iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001039 }
Zhu Yib481de92007-09-25 17:54:57 -07001040}
1041
1042/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001043 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001044 *
1045 * When moving to rx_free an SKB is allocated for the slot.
1046 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001047 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001048 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001049 */
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001050static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001051{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001052 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001053 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001054 struct iwl_rx_mem_buffer *rxb;
Zhu Yi2f301222009-10-09 17:19:45 +08001055 struct page *page;
Zhu Yib481de92007-09-25 17:54:57 -07001056 unsigned long flags;
Zhu Yi29b1b262009-10-23 13:42:25 -07001057 gfp_t gfp_mask = priority;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001058
1059 while (1) {
1060 spin_lock_irqsave(&rxq->lock, flags);
1061
1062 if (list_empty(&rxq->rx_used)) {
1063 spin_unlock_irqrestore(&rxq->lock, flags);
1064 return;
1065 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001066 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001067
Reinette Chatref82a9242009-09-17 10:43:56 -07001068 if (rxq->free_count > RX_LOW_WATERMARK)
Zhu Yi29b1b262009-10-23 13:42:25 -07001069 gfp_mask |= __GFP_NOWARN;
Zhu Yi2f301222009-10-09 17:19:45 +08001070
1071 if (priv->hw_params.rx_page_order > 0)
Zhu Yi29b1b262009-10-23 13:42:25 -07001072 gfp_mask |= __GFP_COMP;
Zhu Yi2f301222009-10-09 17:19:45 +08001073
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001074 /* Alloc a new receive buffer */
Zhu Yi29b1b262009-10-23 13:42:25 -07001075 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
Zhu Yi2f301222009-10-09 17:19:45 +08001076 if (!page) {
Zhu Yib481de92007-09-25 17:54:57 -07001077 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001078 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1079 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1080 net_ratelimit())
1081 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1082 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1083 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001084 /* We don't reschedule replenish work here -- we will
1085 * call the restock method and if it still needs
1086 * more buffers it will schedule replenish */
1087 break;
1088 }
Zhu Yi12342c42007-12-20 11:27:32 +08001089
Reinette Chatrede0bd502009-09-11 10:38:12 -07001090 spin_lock_irqsave(&rxq->lock, flags);
1091 if (list_empty(&rxq->rx_used)) {
1092 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001093 __free_pages(page, priv->hw_params.rx_page_order);
Reinette Chatrede0bd502009-09-11 10:38:12 -07001094 return;
1095 }
1096 element = rxq->rx_used.next;
1097 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1098 list_del(element);
1099 spin_unlock_irqrestore(&rxq->lock, flags);
1100
Zhu Yi2f301222009-10-09 17:19:45 +08001101 rxb->page = page;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001102 /* Get physical address of RB/SKB */
Zhu Yi2f301222009-10-09 17:19:45 +08001103 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1104 PAGE_SIZE << priv->hw_params.rx_page_order,
1105 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001106
1107 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001108
Zhu Yib481de92007-09-25 17:54:57 -07001109 list_add_tail(&rxb->list, &rxq->rx_free);
1110 rxq->free_count++;
Zhu Yi2f301222009-10-09 17:19:45 +08001111 priv->alloc_rxb_page++;
1112
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001113 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001114 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001115}
1116
Reinette Chatredf833b12009-04-21 10:55:48 -07001117void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1118{
1119 unsigned long flags;
1120 int i;
1121 spin_lock_irqsave(&rxq->lock, flags);
1122 INIT_LIST_HEAD(&rxq->rx_free);
1123 INIT_LIST_HEAD(&rxq->rx_used);
1124 /* Fill the rx_used queue with _all_ of the Rx buffers */
1125 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1126 /* In the reset function, these buffers may have been allocated
1127 * to an SKB, so we need to unmap and free potential storage */
Zhu Yi2f301222009-10-09 17:19:45 +08001128 if (rxq->pool[i].page != NULL) {
1129 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1130 PAGE_SIZE << priv->hw_params.rx_page_order,
1131 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001132 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001133 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001134 }
1135 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1136 }
1137
1138 /* Set us so that we have processed and used all buffers, but have
1139 * not restocked the Rx queue with fresh buffers */
1140 rxq->read = rxq->write = 0;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001141 rxq->write_actual = 0;
Zhu Yi2f301222009-10-09 17:19:45 +08001142 rxq->free_count = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001143 spin_unlock_irqrestore(&rxq->lock, flags);
1144}
Reinette Chatredf833b12009-04-21 10:55:48 -07001145
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001146void iwl3945_rx_replenish(void *data)
1147{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001148 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001149 unsigned long flags;
1150
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001151 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001152
1153 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001154 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001155 spin_unlock_irqrestore(&priv->lock, flags);
1156}
1157
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001158static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1159{
1160 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1161
1162 iwl3945_rx_queue_restock(priv);
1163}
1164
1165
Reinette Chatredf833b12009-04-21 10:55:48 -07001166/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1167 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1168 * This free routine walks the list of POOL entries and if SKB is set to
1169 * non NULL it is unmapped and freed
1170 */
1171static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1172{
1173 int i;
1174 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
Zhu Yi2f301222009-10-09 17:19:45 +08001175 if (rxq->pool[i].page != NULL) {
1176 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1177 PAGE_SIZE << priv->hw_params.rx_page_order,
1178 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001179 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001180 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001181 }
1182 }
1183
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001184 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
Emmanuel Grumbachd5b25c92010-06-07 13:21:46 -07001185 rxq->bd_dma);
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001186 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1187 rxq->rb_stts, rxq->rb_stts_dma);
Reinette Chatredf833b12009-04-21 10:55:48 -07001188 rxq->bd = NULL;
1189 rxq->rb_stts = NULL;
1190}
Reinette Chatredf833b12009-04-21 10:55:48 -07001191
1192
Zhu Yib481de92007-09-25 17:54:57 -07001193/* Convert linear signal-to-noise ratio into dB */
1194static u8 ratio2dB[100] = {
1195/* 0 1 2 3 4 5 6 7 8 9 */
1196 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1197 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1198 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1199 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1200 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1201 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1202 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1203 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1204 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1205 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1206};
1207
1208/* Calculates a relative dB value from a ratio of linear
1209 * (i.e. not dB) signal levels.
1210 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001211int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001212{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001213 /* 1000:1 or higher just report as 60 dB */
1214 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001215 return 60;
1216
Adrian Bunk221c80c2008-02-02 23:19:01 +02001217 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001218 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001219 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001220 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001221
1222 /* We shouldn't see this */
1223 if (sig_ratio < 1)
1224 return 0;
1225
1226 /* Use table for ratios 1:1 - 99:1 */
1227 return (int)ratio2dB[sig_ratio];
1228}
1229
Zhu Yib481de92007-09-25 17:54:57 -07001230/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001231 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001232 *
1233 * Uses the priv->rx_handlers callback function array to invoke
1234 * the appropriate handlers, including command responses,
1235 * frame-received notifications, and other notifications.
1236 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001237static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001238{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001239 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001240 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001241 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001242 u32 r, i;
1243 int reclaim;
1244 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001245 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001246 u32 count = 8;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001247 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001248
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001249 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1250 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001251 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001252 i = rxq->read;
1253
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001254 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -07001255 total_empty = r - rxq->write_actual;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001256 if (total_empty < 0)
1257 total_empty += RX_QUEUE_SIZE;
1258
1259 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001260 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001261 /* Rx interrupt, but nothing sent from uCode */
1262 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001263 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001264
1265 while (i != r) {
Johannes Bergf4989d92010-05-28 04:08:30 -07001266 int len;
1267
Zhu Yib481de92007-09-25 17:54:57 -07001268 rxb = rxq->queue[i];
1269
Ben Cahill9fbab512007-11-29 11:09:47 +08001270 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001271 * then a bug has been introduced in the queue refilling
1272 * routines -- catch it here */
1273 BUG_ON(rxb == NULL);
1274
1275 rxq->queue[i] = NULL;
1276
Zhu Yi2f301222009-10-09 17:19:45 +08001277 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1278 PAGE_SIZE << priv->hw_params.rx_page_order,
1279 PCI_DMA_FROMDEVICE);
1280 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001281
Johannes Bergf4989d92010-05-28 04:08:30 -07001282 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1283 len += sizeof(u32); /* account for status word */
1284 trace_iwlwifi_dev_rx(priv, pkt, len);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001285
Zhu Yib481de92007-09-25 17:54:57 -07001286 /* Reclaim a command buffer only if this packet is a response
1287 * to a (driver-originated) command.
1288 * If the packet (e.g. Rx frame) originated from uCode,
1289 * there is no command buffer to reclaim.
1290 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1291 * but apparently a few don't get set; catch them here. */
1292 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1293 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1294 (pkt->hdr.cmd != REPLY_TX);
1295
1296 /* Based on type of command response or notification,
1297 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001298 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001299 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001300 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001301 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001302 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -07001303 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001304 } else {
1305 /* No handling needed */
Zhu Yi2f301222009-10-09 17:19:45 +08001306 IWL_DEBUG_RX(priv,
1307 "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001308 r, i, get_cmd_string(pkt->hdr.cmd),
1309 pkt->hdr.cmd);
1310 }
1311
Zhu Yi29b1b262009-10-23 13:42:25 -07001312 /*
1313 * XXX: After here, we should always check rxb->page
1314 * against NULL before touching it or its virtual
1315 * memory (pkt). Because some rx_handler might have
1316 * already taken or freed the pages.
1317 */
1318
Zhu Yib481de92007-09-25 17:54:57 -07001319 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001320 /* Invoke any callbacks, transfer the buffer to caller,
1321 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001322 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001323 if (rxb->page)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001324 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001325 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001326 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001327 }
1328
Zhu Yi73005152009-10-23 13:42:32 -07001329 /* Reuse the page if possible. For notification packets and
1330 * SKBs that fail to Rx correctly, add them back into the
1331 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001332 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001333 if (rxb->page != NULL) {
1334 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1335 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1336 PCI_DMA_FROMDEVICE);
1337 list_add_tail(&rxb->list, &rxq->rx_free);
1338 rxq->free_count++;
1339 } else
1340 list_add_tail(&rxb->list, &rxq->rx_used);
1341
Zhu Yib481de92007-09-25 17:54:57 -07001342 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001343
Zhu Yib481de92007-09-25 17:54:57 -07001344 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001345 /* If there are a lot of unused frames,
1346 * restock the Rx queue so ucode won't assert. */
1347 if (fill_rx) {
1348 count++;
1349 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001350 rxq->read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001351 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001352 count = 0;
1353 }
1354 }
Zhu Yib481de92007-09-25 17:54:57 -07001355 }
1356
1357 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001358 rxq->read = i;
Abhijeet Kolekard14d4442009-06-03 11:44:08 -07001359 if (fill_rx)
1360 iwl3945_rx_replenish_now(priv);
1361 else
1362 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001363}
1364
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001365/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001366static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001367{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001368 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001369 synchronize_irq(priv->pci_dev->irq);
1370 tasklet_kill(&priv->irq_tasklet);
1371}
1372
Zhu Yib481de92007-09-25 17:54:57 -07001373static const char *desc_lookup(int i)
1374{
1375 switch (i) {
1376 case 1:
1377 return "FAIL";
1378 case 2:
1379 return "BAD_PARAM";
1380 case 3:
1381 return "BAD_CHECKSUM";
1382 case 4:
1383 return "NMI_INTERRUPT";
1384 case 5:
1385 return "SYSASSERT";
1386 case 6:
1387 return "FATAL_ERROR";
1388 }
1389
1390 return "UNKNOWN";
1391}
1392
1393#define ERROR_START_OFFSET (1 * sizeof(u32))
1394#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1395
Reinette Chatreb7a79402009-09-25 14:24:23 -07001396void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001397{
1398 u32 i;
1399 u32 desc, time, count, base, data1;
1400 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001401
1402 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1403
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001404 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001405 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001406 return;
1407 }
1408
Zhu Yib481de92007-09-25 17:54:57 -07001409
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001410 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001411
1412 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001413 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1414 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1415 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001416 }
1417
Winkler, Tomas15b16872008-12-19 10:37:33 +08001418 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001419 "ilink1 nmiPC Line\n");
1420 for (i = ERROR_START_OFFSET;
1421 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1422 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001423 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001424 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001425 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001426 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001427 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001428 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001429 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001430 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001431 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001432 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001433 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001434 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001435 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001436
Winkler, Tomas15b16872008-12-19 10:37:33 +08001437 IWL_ERR(priv,
Jay Sternberg87563712010-06-14 14:40:40 -07001438 "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +08001439 desc_lookup(desc), desc, time, blink1, blink2,
1440 ilink1, ilink2, data1);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001441 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1442 0, blink1, blink2, ilink1, ilink2);
Zhu Yib481de92007-09-25 17:54:57 -07001443 }
Zhu Yib481de92007-09-25 17:54:57 -07001444}
1445
Ben Cahillf58177b2007-11-29 11:09:43 +08001446#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001447
1448/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001449 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001450 *
Zhu Yib481de92007-09-25 17:54:57 -07001451 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001452static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1453 u32 num_events, u32 mode,
1454 int pos, char **buf, size_t bufsz)
Zhu Yib481de92007-09-25 17:54:57 -07001455{
1456 u32 i;
1457 u32 base; /* SRAM byte address of event log header */
1458 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1459 u32 ptr; /* SRAM byte address of log data */
1460 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001461 unsigned long reg_flags;
Zhu Yib481de92007-09-25 17:54:57 -07001462
1463 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001464 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001465
1466 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1467
1468 if (mode == 0)
1469 event_size = 2 * sizeof(u32);
1470 else
1471 event_size = 3 * sizeof(u32);
1472
1473 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1474
Ben Cahille5854472009-11-06 14:52:58 -08001475 /* Make sure device is powered up for SRAM reads */
1476 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1477 iwl_grab_nic_access(priv);
1478
1479 /* Set starting address; reads will auto-increment */
1480 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1481 rmb();
1482
Zhu Yib481de92007-09-25 17:54:57 -07001483 /* "time" is actually "data" for mode 0 (no timestamp).
1484 * place event id # at far right for easier visual parsing. */
1485 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001486 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1487 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001488 if (mode == 0) {
1489 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001490 if (bufsz) {
1491 pos += scnprintf(*buf + pos, bufsz - pos,
1492 "0x%08x:%04u\n",
1493 time, ev);
1494 } else {
1495 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1496 trace_iwlwifi_dev_ucode_event(priv, 0,
1497 time, ev);
1498 }
Winkler, Tomas15b16872008-12-19 10:37:33 +08001499 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001500 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001501 if (bufsz) {
1502 pos += scnprintf(*buf + pos, bufsz - pos,
1503 "%010u:0x%08x:%04u\n",
1504 time, data, ev);
1505 } else {
1506 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1507 time, data, ev);
1508 trace_iwlwifi_dev_ucode_event(priv, time,
1509 data, ev);
1510 }
Zhu Yib481de92007-09-25 17:54:57 -07001511 }
1512 }
Ben Cahille5854472009-11-06 14:52:58 -08001513
1514 /* Allow device to power down */
1515 iwl_release_nic_access(priv);
1516 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001517 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001518}
1519
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001520/**
1521 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1522 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001523static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001524 u32 num_wraps, u32 next_entry,
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001525 u32 size, u32 mode,
1526 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001527{
1528 /*
1529 * display the newest DEFAULT_LOG_ENTRIES entries
1530 * i.e the entries just before the next ont that uCode would fill.
1531 */
1532 if (num_wraps) {
1533 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001534 pos = iwl3945_print_event_log(priv,
1535 capacity - (size - next_entry),
1536 size - next_entry, mode,
1537 pos, buf, bufsz);
1538 pos = iwl3945_print_event_log(priv, 0,
1539 next_entry, mode,
1540 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001541 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001542 pos = iwl3945_print_event_log(priv, next_entry - size,
1543 size, mode,
1544 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001545 } else {
1546 if (next_entry < size)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001547 pos = iwl3945_print_event_log(priv, 0,
1548 next_entry, mode,
1549 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001550 else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001551 pos = iwl3945_print_event_log(priv, next_entry - size,
1552 size, mode,
1553 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001554 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001555 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001556}
1557
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001558#define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1559
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001560int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1561 char **buf, bool display)
Zhu Yib481de92007-09-25 17:54:57 -07001562{
Zhu Yib481de92007-09-25 17:54:57 -07001563 u32 base; /* SRAM byte address of event log header */
1564 u32 capacity; /* event log capacity in # entries */
1565 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1566 u32 num_wraps; /* # times uCode wrapped to top of log */
1567 u32 next_entry; /* index of next entry to be written by uCode */
1568 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001569 int pos = 0;
1570 size_t bufsz = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001571
1572 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001573 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001574 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001575 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001576 }
1577
Zhu Yib481de92007-09-25 17:54:57 -07001578 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001579 capacity = iwl_read_targ_mem(priv, base);
1580 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1581 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1582 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001583
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001584 if (capacity > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001585 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001586 capacity, priv->cfg->max_event_log_size);
1587 capacity = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001588 }
1589
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001590 if (next_entry > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001591 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001592 next_entry, priv->cfg->max_event_log_size);
1593 next_entry = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001594 }
1595
Zhu Yib481de92007-09-25 17:54:57 -07001596 size = num_wraps ? capacity : next_entry;
1597
1598 /* bail out if nothing in log */
1599 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001600 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001601 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001602 }
1603
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001604#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001605 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001606 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1607 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1608#else
1609 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1610 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1611#endif
1612
1613 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1614 size);
Zhu Yib481de92007-09-25 17:54:57 -07001615
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001616#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001617 if (display) {
1618 if (full_log)
1619 bufsz = capacity * 48;
1620 else
1621 bufsz = size * 48;
1622 *buf = kmalloc(bufsz, GFP_KERNEL);
1623 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001624 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001625 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001626 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1627 /* if uCode has wrapped back to top of log,
1628 * start at the oldest entry,
1629 * i.e the next one that uCode would fill.
1630 */
1631 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001632 pos = iwl3945_print_event_log(priv, next_entry,
1633 capacity - next_entry, mode,
1634 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001635
1636 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001637 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1638 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001639 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001640 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1641 next_entry, size, mode,
1642 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001643#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001644 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1645 next_entry, size, mode,
1646 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001647#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001648 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001649}
1650
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001651static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001652{
1653 u32 inta, handled = 0;
1654 u32 inta_fh;
1655 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001656#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001657 u32 inta_mask;
1658#endif
1659
1660 spin_lock_irqsave(&priv->lock, flags);
1661
1662 /* Ack/clear/reset pending uCode interrupts.
1663 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1664 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001665 inta = iwl_read32(priv, CSR_INT);
1666 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001667
1668 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1669 * Any new interrupts that happen after this, either while we're
1670 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001671 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1672 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001673
Samuel Ortizd08853a2009-01-23 13:45:17 -08001674#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001675 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001676 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001677 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001678 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001679 inta, inta_mask, inta_fh);
1680 }
1681#endif
1682
Zhu Yi2f301222009-10-09 17:19:45 +08001683 spin_unlock_irqrestore(&priv->lock, flags);
1684
Zhu Yib481de92007-09-25 17:54:57 -07001685 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1686 * atomic, make sure that inta covers all the interrupts that
1687 * we've discovered, even if FH interrupt came in just after
1688 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001689 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001690 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001691 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001692 inta |= CSR_INT_BIT_FH_TX;
1693
1694 /* Now service all interrupt bits discovered above. */
1695 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001696 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001697
1698 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001699 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001700
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001701 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001702 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001703
1704 handled |= CSR_INT_BIT_HW_ERR;
1705
Zhu Yib481de92007-09-25 17:54:57 -07001706 return;
1707 }
1708
Samuel Ortizd08853a2009-01-23 13:45:17 -08001709#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001710 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001711 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001712 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001713 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001714 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001715 priv->isr_stats.sch++;
1716 }
Zhu Yib481de92007-09-25 17:54:57 -07001717
1718 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001719 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001720 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001721 priv->isr_stats.alive++;
1722 }
Zhu Yib481de92007-09-25 17:54:57 -07001723 }
1724#endif
1725 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001726 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001727
Zhu Yib481de92007-09-25 17:54:57 -07001728 /* Error detected by uCode */
1729 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001730 IWL_ERR(priv, "Microcode SW error detected. "
1731 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001732 priv->isr_stats.sw++;
1733 priv->isr_stats.sw_err = inta;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001734 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001735 handled |= CSR_INT_BIT_SW_ERR;
1736 }
1737
1738 /* uCode wakes up after power-down sleep */
1739 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001740 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001741 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001742 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1743 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1744 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1745 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1746 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1747 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001748
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001749 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001750 handled |= CSR_INT_BIT_WAKEUP;
1751 }
1752
1753 /* All uCode command responses, including Tx command responses,
1754 * Rx "responses" (frame-received notification), and other
1755 * notifications from uCode come through here*/
1756 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001757 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001758 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001759 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1760 }
1761
1762 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001763 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001764 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001765
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001766 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001767 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1768 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001769 handled |= CSR_INT_BIT_FH_TX;
1770 }
1771
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001772 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001773 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001774 priv->isr_stats.unhandled++;
1775 }
Zhu Yib481de92007-09-25 17:54:57 -07001776
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001777 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001778 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001779 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001780 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001781 }
1782
1783 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001784 /* only Re-enable if disabled by irq */
1785 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001786 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001787
Samuel Ortizd08853a2009-01-23 13:45:17 -08001788#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001789 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001790 inta = iwl_read32(priv, CSR_INT);
1791 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1792 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001793 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001794 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1795 }
1796#endif
Zhu Yib481de92007-09-25 17:54:57 -07001797}
1798
Abhijeet Kolekar14023642010-06-02 21:15:10 -07001799static int iwl3945_get_single_channel_for_scan(struct iwl_priv *priv,
1800 struct ieee80211_vif *vif,
1801 enum ieee80211_band band,
1802 struct iwl3945_scan_channel *scan_ch)
1803{
1804 const struct ieee80211_supported_band *sband;
1805 u16 passive_dwell = 0;
1806 u16 active_dwell = 0;
1807 int added = 0;
1808 u8 channel = 0;
1809
1810 sband = iwl_get_hw_mode(priv, band);
1811 if (!sband) {
1812 IWL_ERR(priv, "invalid band\n");
1813 return added;
1814 }
1815
1816 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
1817 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
1818
1819 if (passive_dwell <= active_dwell)
1820 passive_dwell = active_dwell + 1;
1821
1822
1823 channel = iwl_get_single_channel_number(priv, band);
1824
1825 if (channel) {
1826 scan_ch->channel = channel;
1827 scan_ch->type = 0; /* passive */
1828 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1829 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1830 /* Set txpower levels to defaults */
1831 scan_ch->tpc.dsp_atten = 110;
1832 if (band == IEEE80211_BAND_5GHZ)
1833 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1834 else
1835 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1836 added++;
1837 } else
1838 IWL_ERR(priv, "no valid channel found\n");
1839 return added;
1840}
1841
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001842static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001843 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001844 u8 is_active, u8 n_probes,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001845 struct iwl3945_scan_channel *scan_ch,
1846 struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07001847{
Johannes Berg4e05c232009-06-19 13:52:44 -07001848 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001849 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001850 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001851 u16 passive_dwell = 0;
1852 u16 active_dwell = 0;
1853 int added, i;
1854
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001855 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001856 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001857 return 0;
1858
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001859 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001860 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Zhu Yib481de92007-09-25 17:54:57 -07001861
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001862 if (passive_dwell <= active_dwell)
1863 passive_dwell = active_dwell + 1;
1864
Johannes Berg4e05c232009-06-19 13:52:44 -07001865 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1866 chan = priv->scan_request->channels[i];
1867
1868 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001869 continue;
1870
Johannes Berg4e05c232009-06-19 13:52:44 -07001871 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001872
Samuel Ortize6148912009-01-23 13:45:15 -08001873 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001874 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001875 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001876 scan_ch->channel);
1877 continue;
1878 }
1879
Zhu Yib481de92007-09-25 17:54:57 -07001880 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1881 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001882 /* If passive , set up for auto-switch
1883 * and use long active_dwell time.
1884 */
1885 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001886 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001887 scan_ch->type = 0; /* passive */
1888 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1889 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1890 } else {
1891 scan_ch->type = 1; /* active */
1892 }
1893
1894 /* Set direct probe bits. These may be used both for active
1895 * scan channels (probes gets sent right away),
1896 * or for passive channels (probes get se sent only after
1897 * hearing clear Rx packet).*/
1898 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1899 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001900 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001901 } else {
1902 /* uCode v1 does not allow setting direct probe bits on
1903 * passive channel. */
1904 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001905 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001906 }
Zhu Yib481de92007-09-25 17:54:57 -07001907
Ben Cahill9fbab512007-11-29 11:09:47 +08001908 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001909 scan_ch->tpc.dsp_atten = 110;
1910 /* scan_pwr_info->tpc.dsp_atten; */
1911
1912 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001913 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001914 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1915 else {
1916 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1917 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001918 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001919 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001920 */
1921 }
1922
Tomas Winklere1623442009-01-27 14:27:56 -08001923 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001924 scan_ch->channel,
1925 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1926 (scan_ch->type & 1) ?
1927 active_dwell : passive_dwell);
1928
1929 scan_ch++;
1930 added++;
1931 }
1932
Frans Pop91dd6c22010-03-24 14:19:58 -07001933 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001934 return added;
1935}
1936
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001937static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001938 struct ieee80211_rate *rates)
1939{
1940 int i;
1941
Dan Carpenter8e1a53c2010-03-28 14:55:00 +03001942 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001943 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1944 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1945 rates[i].hw_value_short = i;
1946 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001947 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001948 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001949 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001950 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001951 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001952 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001953 }
Zhu Yib481de92007-09-25 17:54:57 -07001954 }
1955}
1956
Zhu Yib481de92007-09-25 17:54:57 -07001957/******************************************************************************
1958 *
1959 * uCode download functions
1960 *
1961 ******************************************************************************/
1962
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001963static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001964{
Tomas Winkler98c92212008-01-14 17:46:20 -08001965 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1966 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1967 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1968 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1969 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1970 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001971}
1972
1973/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001974 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001975 * looking at all data.
1976 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001977static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001978{
1979 u32 val;
1980 u32 save_len = len;
1981 int rc = 0;
1982 u32 errcnt;
1983
Tomas Winklere1623442009-01-27 14:27:56 -08001984 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001985
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001986 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001987 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07001988
1989 errcnt = 0;
1990 for (; len > 0; len -= sizeof(u32), image++) {
1991 /* read data comes through single port, auto-incr addr */
1992 /* NOTE: Use the debugless read so we don't flood kernel log
1993 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001994 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001995 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001996 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001997 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1998 save_len - len, val, le32_to_cpu(*image));
1999 rc = -EIO;
2000 errcnt++;
2001 if (errcnt >= 20)
2002 break;
2003 }
2004 }
2005
Zhu Yib481de92007-09-25 17:54:57 -07002006
2007 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002008 IWL_DEBUG_INFO(priv,
2009 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002010
2011 return rc;
2012}
2013
2014
2015/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002016 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002017 * using sample data 100 bytes apart. If these sample points are good,
2018 * it's a pretty good bet that everything between them is good, too.
2019 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002020static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002021{
2022 u32 val;
2023 int rc = 0;
2024 u32 errcnt = 0;
2025 u32 i;
2026
Tomas Winklere1623442009-01-27 14:27:56 -08002027 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002028
Zhu Yib481de92007-09-25 17:54:57 -07002029 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2030 /* read data comes through single port, auto-incr addr */
2031 /* NOTE: Use the debugless read so we don't flood kernel log
2032 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002033 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002034 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002035 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002036 if (val != le32_to_cpu(*image)) {
2037#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002038 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002039 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2040 i, val, *image);
2041#endif
2042 rc = -EIO;
2043 errcnt++;
2044 if (errcnt >= 3)
2045 break;
2046 }
2047 }
2048
Zhu Yib481de92007-09-25 17:54:57 -07002049 return rc;
2050}
2051
2052
2053/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002054 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002055 * and verify its contents
2056 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002057static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002058{
2059 __le32 *image;
2060 u32 len;
2061 int rc = 0;
2062
2063 /* Try bootstrap */
2064 image = (__le32 *)priv->ucode_boot.v_addr;
2065 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002066 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002067 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002068 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002069 return 0;
2070 }
2071
2072 /* Try initialize */
2073 image = (__le32 *)priv->ucode_init.v_addr;
2074 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002075 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002076 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002077 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002078 return 0;
2079 }
2080
2081 /* Try runtime/protocol */
2082 image = (__le32 *)priv->ucode_code.v_addr;
2083 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002084 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002085 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002086 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002087 return 0;
2088 }
2089
Winkler, Tomas15b16872008-12-19 10:37:33 +08002090 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002091
Ben Cahill9fbab512007-11-29 11:09:47 +08002092 /* Since nothing seems to match, show first several data entries in
2093 * instruction SRAM, so maybe visual inspection will give a clue.
2094 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002095 image = (__le32 *)priv->ucode_boot.v_addr;
2096 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002097 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002098
2099 return rc;
2100}
2101
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002102static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002103{
2104 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002105 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002106}
2107
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002108#define IWL3945_UCODE_GET(item) \
2109static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
2110{ \
2111 return le32_to_cpu(ucode->u.v1.item); \
2112}
2113
2114static u32 iwl3945_ucode_get_header_size(u32 api_ver)
2115{
Johannes Berg22adba22010-04-28 12:09:14 -07002116 return 24;
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002117}
2118
2119static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
2120{
2121 return (u8 *) ucode->u.v1.data;
2122}
2123
2124IWL3945_UCODE_GET(inst_size);
2125IWL3945_UCODE_GET(data_size);
2126IWL3945_UCODE_GET(init_size);
2127IWL3945_UCODE_GET(init_data_size);
2128IWL3945_UCODE_GET(boot_size);
2129
Zhu Yib481de92007-09-25 17:54:57 -07002130/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002131 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002132 *
2133 * Copy into buffers for card to fetch via bus-mastering
2134 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002135static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002136{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002137 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002138 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002139 const struct firmware *ucode_raw;
2140 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002141 const char *name_pre = priv->cfg->fw_name_pre;
2142 const unsigned int api_max = priv->cfg->ucode_api_max;
2143 const unsigned int api_min = priv->cfg->ucode_api_min;
2144 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002145 u8 *src;
2146 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002147 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002148
2149 /* Ask kernel firmware_class module to get the boot firmware off disk.
2150 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002151 for (index = api_max; index >= api_min; index--) {
2152 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2153 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2154 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002155 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002156 buf, ret);
2157 if (ret == -ENOENT)
2158 continue;
2159 else
2160 goto error;
2161 } else {
2162 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002163 IWL_ERR(priv, "Loaded firmware %s, "
2164 "which is deprecated. "
2165 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002166 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002167 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2168 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002169 buf, ucode_raw->size);
2170 break;
2171 }
Zhu Yib481de92007-09-25 17:54:57 -07002172 }
2173
Reinette Chatrea0987a82008-12-02 12:14:06 -08002174 if (ret < 0)
2175 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002176
2177 /* Make sure that we got at least our header! */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002178 if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002179 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002180 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002181 goto err_release;
2182 }
2183
2184 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002185 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002186
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002187 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002188 api_ver = IWL_UCODE_API(priv->ucode_ver);
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002189 inst_size = iwl3945_ucode_get_inst_size(ucode);
2190 data_size = iwl3945_ucode_get_data_size(ucode);
2191 init_size = iwl3945_ucode_get_init_size(ucode);
2192 init_data_size = iwl3945_ucode_get_init_data_size(ucode);
2193 boot_size = iwl3945_ucode_get_boot_size(ucode);
2194 src = iwl3945_ucode_get_data(ucode);
Zhu Yib481de92007-09-25 17:54:57 -07002195
Reinette Chatrea0987a82008-12-02 12:14:06 -08002196 /* api_ver should match the api version forming part of the
2197 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002198 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002199
2200 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002201 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002202 "Driver supports v%u, firmware is v%u.\n",
2203 api_max, api_ver);
2204 priv->ucode_ver = 0;
2205 ret = -EINVAL;
2206 goto err_release;
2207 }
2208 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002209 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002210 "got %u. New firmware can be obtained "
2211 "from http://www.intellinuxwireless.org.\n",
2212 api_max, api_ver);
2213
Tomas Winkler978785a2008-12-19 10:37:31 +08002214 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2215 IWL_UCODE_MAJOR(priv->ucode_ver),
2216 IWL_UCODE_MINOR(priv->ucode_ver),
2217 IWL_UCODE_API(priv->ucode_ver),
2218 IWL_UCODE_SERIAL(priv->ucode_ver));
2219
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07002220 snprintf(priv->hw->wiphy->fw_version,
2221 sizeof(priv->hw->wiphy->fw_version),
2222 "%u.%u.%u.%u",
2223 IWL_UCODE_MAJOR(priv->ucode_ver),
2224 IWL_UCODE_MINOR(priv->ucode_ver),
2225 IWL_UCODE_API(priv->ucode_ver),
2226 IWL_UCODE_SERIAL(priv->ucode_ver));
2227
Tomas Winklere1623442009-01-27 14:27:56 -08002228 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002229 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002230 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2231 inst_size);
2232 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2233 data_size);
2234 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2235 init_size);
2236 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2237 init_data_size);
2238 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2239 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002240
Reinette Chatrea0987a82008-12-02 12:14:06 -08002241
Zhu Yib481de92007-09-25 17:54:57 -07002242 /* Verify size of file vs. image size info in file's header */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002243 if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002244 inst_size + data_size + init_size +
2245 init_data_size + boot_size) {
2246
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002247 IWL_DEBUG_INFO(priv,
2248 "uCode file size %zd does not match expected size\n",
2249 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002250 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002251 goto err_release;
2252 }
2253
2254 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002255 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002256 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002257 inst_size);
2258 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002259 goto err_release;
2260 }
2261
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002262 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002263 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002264 data_size);
2265 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002266 goto err_release;
2267 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002268 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002269 IWL_DEBUG_INFO(priv,
2270 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002271 init_size);
2272 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002273 goto err_release;
2274 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002275 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002276 IWL_DEBUG_INFO(priv,
2277 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002278 init_data_size);
2279 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002280 goto err_release;
2281 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002282 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002283 IWL_DEBUG_INFO(priv,
2284 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002285 boot_size);
2286 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002287 goto err_release;
2288 }
2289
2290 /* Allocate ucode buffers for card's bus-master loading ... */
2291
2292 /* Runtime instructions and 2 copies of data:
2293 * 1) unmodified from disk
2294 * 2) backup cache for save/restore during power-downs */
2295 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002296 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002297
2298 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002299 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002300
2301 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002302 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002303
2304 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002305 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002306 goto err_pci_alloc;
2307
Tomas Winkler90e759d2007-11-29 11:09:41 +08002308 /* Initialization instructions and data */
2309 if (init_size && init_data_size) {
2310 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002311 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002312
2313 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002314 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002315
2316 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2317 goto err_pci_alloc;
2318 }
2319
2320 /* Bootstrap (instructions only, no data) */
2321 if (boot_size) {
2322 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002323 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002324
2325 if (!priv->ucode_boot.v_addr)
2326 goto err_pci_alloc;
2327 }
2328
Zhu Yib481de92007-09-25 17:54:57 -07002329 /* Copy images into buffers for card's bus-master reads ... */
2330
2331 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002332 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002333 IWL_DEBUG_INFO(priv,
2334 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002335 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002336 src += len;
2337
Tomas Winklere1623442009-01-27 14:27:56 -08002338 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002339 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2340
2341 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002342 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002343 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002344 IWL_DEBUG_INFO(priv,
2345 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002346 memcpy(priv->ucode_data.v_addr, src, len);
2347 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002348 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002349
2350 /* Initialization instructions (3rd block) */
2351 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002352 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002353 IWL_DEBUG_INFO(priv,
2354 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002355 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002356 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002357 }
2358
2359 /* Initialization data (4th block) */
2360 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002361 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002362 IWL_DEBUG_INFO(priv,
2363 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002364 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002365 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002366 }
2367
2368 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002369 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002370 IWL_DEBUG_INFO(priv,
2371 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002372 memcpy(priv->ucode_boot.v_addr, src, len);
2373
2374 /* We have our copies now, allow OS release its copies */
2375 release_firmware(ucode_raw);
2376 return 0;
2377
2378 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002379 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002380 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002381 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002382
2383 err_release:
2384 release_firmware(ucode_raw);
2385
2386 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002387 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002388}
2389
2390
2391/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002392 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002393 *
2394 * Tell initialization uCode where to find runtime uCode.
2395 *
2396 * BSM registers initially contain pointers to initialization uCode.
2397 * We need to replace them to load runtime uCode inst and data,
2398 * and to save runtime data when powering down.
2399 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002400static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002401{
2402 dma_addr_t pinst;
2403 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002404
2405 /* bits 31:0 for 3945 */
2406 pinst = priv->ucode_code.p_addr;
2407 pdata = priv->ucode_data_backup.p_addr;
2408
Zhu Yib481de92007-09-25 17:54:57 -07002409 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002410 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2411 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2412 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002413 priv->ucode_data.len);
2414
Tomas Winklera96a27f2008-10-23 23:48:56 -07002415 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002416 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002417 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002418 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2419
Tomas Winklere1623442009-01-27 14:27:56 -08002420 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002421
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002422 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002423}
2424
2425/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002426 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002427 *
2428 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2429 *
Zhu Yib481de92007-09-25 17:54:57 -07002430 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002431 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002432static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002433{
2434 /* Check alive response for "valid" sign from uCode */
2435 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2436 /* We had an error bringing up the hardware, so take it
2437 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002438 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002439 goto restart;
2440 }
2441
2442 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2443 * This is a paranoid check, because we would not have gotten the
2444 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002445 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002446 /* Runtime instruction load was bad;
2447 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002448 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002449 goto restart;
2450 }
2451
2452 /* Send pointers to protocol/runtime uCode image ... init code will
2453 * load and launch runtime uCode, which will send us another "Alive"
2454 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002455 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002456 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002457 /* Runtime instruction load won't happen;
2458 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002459 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002460 goto restart;
2461 }
2462 return;
2463
2464 restart:
2465 queue_work(priv->workqueue, &priv->restart);
2466}
2467
Zhu Yib481de92007-09-25 17:54:57 -07002468/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002469 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002470 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002471 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002472 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002473static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002474{
Zhu Yib481de92007-09-25 17:54:57 -07002475 int thermal_spin = 0;
2476 u32 rfkill;
Johannes Berg246ed352010-08-23 10:46:32 +02002477 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07002478
Tomas Winklere1623442009-01-27 14:27:56 -08002479 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002480
2481 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2482 /* We had an error bringing up the hardware, so take it
2483 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002484 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002485 goto restart;
2486 }
2487
2488 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2489 * This is a paranoid check, because we would not have gotten the
2490 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002491 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002492 /* Runtime instruction load was bad;
2493 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002494 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002495 goto restart;
2496 }
2497
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002498 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002499 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002500
2501 if (rfkill & 0x1) {
2502 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002503 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002504 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002505 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002506 thermal_spin++;
2507 udelay(10);
2508 }
2509
2510 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002511 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002512 thermal_spin * 10);
2513 } else
2514 set_bit(STATUS_RF_KILL_HW, &priv->status);
2515
Ben Cahill9fbab512007-11-29 11:09:47 +08002516 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002517 set_bit(STATUS_ALIVE, &priv->status);
2518
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08002519 if (priv->cfg->ops->lib->recover_from_tx_stall) {
2520 /* Enable timer to monitor the driver queues */
2521 mod_timer(&priv->monitor_recover,
2522 jiffies +
2523 msecs_to_jiffies(priv->cfg->monitor_recover_period));
2524 }
2525
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002526 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002527 return;
2528
Johannes Berg36d68252008-05-15 12:55:26 +02002529 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002530
Johannes Berg470ab2d2010-01-21 11:23:30 -08002531 priv->active_rate = IWL_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07002532
Ben Cahill4d6ccbf2009-11-13 11:56:29 -08002533 iwl_power_update_mode(priv, true);
Zhu Yib481de92007-09-25 17:54:57 -07002534
Johannes Berg246ed352010-08-23 10:46:32 +02002535 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002536 struct iwl3945_rxon_cmd *active_rxon =
Johannes Berg246ed352010-08-23 10:46:32 +02002537 (struct iwl3945_rxon_cmd *)(&ctx->active);
Zhu Yib481de92007-09-25 17:54:57 -07002538
Johannes Berg246ed352010-08-23 10:46:32 +02002539 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002540 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2541 } else {
2542 /* Initialize our rx_config data */
Johannes Bergd0fe4782010-08-23 10:46:58 +02002543 iwl_connection_init_rx_config(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07002544 }
2545
Ben Cahill9fbab512007-11-29 11:09:47 +08002546 /* Configure Bluetooth device coexistence support */
Johannes Berg65b52bd2010-04-13 01:04:31 -07002547 priv->cfg->ops->hcmd->send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002548
2549 /* Configure the adapter for unassociated operation */
Johannes Berg246ed352010-08-23 10:46:32 +02002550 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07002551
Zhu Yib481de92007-09-25 17:54:57 -07002552 iwl3945_reg_txpower_periodic(priv);
2553
Johannes Berge932a602009-10-02 13:44:03 -07002554 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002555
Tomas Winklere1623442009-01-27 14:27:56 -08002556 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002557 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002558 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002559
Zhu Yib481de92007-09-25 17:54:57 -07002560 return;
2561
2562 restart:
2563 queue_work(priv->workqueue, &priv->restart);
2564}
2565
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002566static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002567
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002568static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002569{
2570 unsigned long flags;
2571 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2572 struct ieee80211_conf *conf = NULL;
2573
Tomas Winklere1623442009-01-27 14:27:56 -08002574 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002575
2576 conf = ieee80211_get_hw_conf(priv->hw);
2577
2578 if (!exit_pending)
2579 set_bit(STATUS_EXIT_PENDING, &priv->status);
2580
Stanislaw Gruszkab62177a2010-08-20 15:23:47 +02002581 /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
2582 * to prevent rearm timer */
2583 if (priv->cfg->ops->lib->recover_from_tx_stall)
2584 del_timer_sync(&priv->monitor_recover);
2585
Reinette Chatre7e246192010-02-18 22:58:32 -08002586 /* Station information will now be cleared in device */
Johannes Bergdcef7322010-08-27 08:55:52 -07002587 iwl_clear_ucode_stations(priv, NULL);
Johannes Berga194e322010-08-27 08:53:46 -07002588 iwl_dealloc_bcast_stations(priv);
Johannes Bergdb125c72010-05-07 01:49:15 -07002589 iwl_clear_driver_stations(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002590
2591 /* Unblock any waiting calls */
2592 wake_up_interruptible_all(&priv->wait_command_queue);
2593
Zhu Yib481de92007-09-25 17:54:57 -07002594 /* Wipe out the EXIT_PENDING status bit if we are not actually
2595 * exiting the module */
2596 if (!exit_pending)
2597 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2598
2599 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002600 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002601
2602 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002603 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002604 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002605 spin_unlock_irqrestore(&priv->lock, flags);
2606 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002607
2608 if (priv->mac80211_registered)
2609 ieee80211_stop_queues(priv->hw);
2610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002611 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002612 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002613 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002614 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2615 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002616 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2617 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002618 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2619 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002620 goto exit;
2621 }
2622
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002623 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002624 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002625 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2626 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002627 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2628 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002629 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002630 STATUS_FW_ERROR |
2631 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2632 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002634 iwl3945_hw_txq_ctx_stop(priv);
2635 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002636
Ben Cahill309e7312009-11-06 14:53:03 -08002637 /* Power-down device's busmaster DMA clocks */
2638 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002639 udelay(5);
2640
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002641 /* Stop the device, and put it in low power state */
2642 priv->cfg->ops->lib->apm_ops.stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002643
Zhu Yib481de92007-09-25 17:54:57 -07002644 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002645 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002646
2647 if (priv->ibss_beacon)
2648 dev_kfree_skb(priv->ibss_beacon);
2649 priv->ibss_beacon = NULL;
2650
2651 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002652 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002653}
2654
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002655static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002656{
2657 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002658 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002659 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002661 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002662}
2663
2664#define MAX_HW_RESTARTS 5
2665
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002666static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002667{
2668 int rc, i;
2669
Johannes Berga194e322010-08-27 08:53:46 -07002670 rc = iwl_alloc_bcast_station(priv, &priv->contexts[IWL_RXON_CTX_BSS],
2671 false);
Johannes Berg2c810cc2010-04-29 00:53:29 -07002672 if (rc)
2673 return rc;
2674
Zhu Yib481de92007-09-25 17:54:57 -07002675 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002676 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002677 return -EIO;
2678 }
2679
Reinette Chatree903fbd2008-01-30 22:05:15 -08002680 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002681 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002682 return -EIO;
2683 }
2684
Zhu Yie655b9f2008-01-24 02:19:38 -08002685 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002686 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002687 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2688 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2689 else {
2690 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002691 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2692 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002693 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002694
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002695 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002696
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002697 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002698 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002699 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002700 return rc;
2701 }
2702
2703 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002704 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2705 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002706 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2707
2708 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002709 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002710 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002711
2712 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002713 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2714 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002715
2716 /* Copy original ucode data image from disk into backup cache.
2717 * This will be used to initialize the on-board processor's
2718 * data SRAM for a clean start when the runtime program first loads. */
2719 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002720 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002721
Zhu Yie655b9f2008-01-24 02:19:38 -08002722 /* We return success when we resume from suspend and rf_kill is on. */
2723 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2724 return 0;
2725
Zhu Yib481de92007-09-25 17:54:57 -07002726 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2727
Zhu Yib481de92007-09-25 17:54:57 -07002728 /* load bootstrap state machine,
2729 * load bootstrap program into processor's memory,
2730 * prepare to load the "initialize" uCode */
Abhijeet Kolekar75a9a922010-02-26 15:17:01 -08002731 rc = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002732
2733 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002734 IWL_ERR(priv,
2735 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002736 continue;
2737 }
2738
2739 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002740 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002741
Tomas Winklere1623442009-01-27 14:27:56 -08002742 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002743
2744 return 0;
2745 }
2746
2747 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002748 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002749 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002750
2751 /* tried to restart and config the device for as long as our
2752 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002753 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002754 return -EIO;
2755}
2756
2757
2758/*****************************************************************************
2759 *
2760 * Workqueue callbacks
2761 *
2762 *****************************************************************************/
2763
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002764static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002765{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002766 struct iwl_priv *priv =
2767 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002768
2769 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2770 return;
2771
2772 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002773 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002774 mutex_unlock(&priv->mutex);
2775}
2776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002777static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002778{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002779 struct iwl_priv *priv =
2780 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002781
2782 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2783 return;
2784
2785 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002786 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002787 mutex_unlock(&priv->mutex);
2788}
2789
Ben Cahill743cdf12009-10-09 13:20:26 -07002790/*
2791 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2792 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2793 * *is* readable even when device has been SW_RESET into low power mode
2794 * (e.g. during RF KILL).
2795 */
Helmut Schaa26635162009-01-15 09:38:44 +01002796static void iwl3945_rfkill_poll(struct work_struct *data)
2797{
2798 struct iwl_priv *priv =
Johannes Bergee525d12010-01-21 06:09:28 -08002799 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002800 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2801 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2802 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002803
Ben Cahill743cdf12009-10-09 13:20:26 -07002804 if (new_rfkill != old_rfkill) {
2805 if (new_rfkill)
2806 set_bit(STATUS_RF_KILL_HW, &priv->status);
2807 else
2808 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002809
Ben Cahill743cdf12009-10-09 13:20:26 -07002810 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002811
Ben Cahill743cdf12009-10-09 13:20:26 -07002812 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2813 new_rfkill ? "disable radio" : "enable radio");
2814 }
2815
2816 /* Keep this running, even if radio now enabled. This will be
2817 * cancelled in mac_start() if system decides to start again */
Johannes Bergee525d12010-01-21 06:09:28 -08002818 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01002819 round_jiffies_relative(2 * HZ));
2820
2821}
2822
Johannes Berg1dda6d22010-04-29 04:43:06 -07002823void iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07002824{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002825 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002826 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002827 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002828 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002829 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002830 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07002831 struct ieee80211_conf *conf = NULL;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002832 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002833 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002834 bool is_active = false;
Zhu Yib481de92007-09-25 17:54:57 -07002835
2836 conf = ieee80211_get_hw_conf(priv->hw);
2837
Reinette Chatrefbc9f972009-05-15 16:13:46 -07002838 cancel_delayed_work(&priv->scan_check);
2839
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002840 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002841 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002842 goto done;
2843 }
2844
Tomas Winklera96a27f2008-10-23 23:48:56 -07002845 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07002846 * was given the chance to run... */
2847 if (!test_bit(STATUS_SCANNING, &priv->status))
2848 goto done;
2849
2850 /* This should never be called or scheduled if there is currently
2851 * a scan active in the hardware. */
2852 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002853 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
2854 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002855 goto done;
2856 }
2857
2858 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002859 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07002860 goto done;
2861 }
2862
2863 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002864 IWL_DEBUG_HC(priv,
2865 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002866 goto done;
2867 }
2868
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002869 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002870 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07002871 goto done;
2872 }
2873
2874 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002875 IWL_DEBUG_HC(priv,
2876 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002877 goto done;
2878 }
2879
Johannes Berg811ecc92010-04-06 04:12:41 -07002880 if (!priv->scan_cmd) {
2881 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2882 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2883 if (!priv->scan_cmd) {
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002884 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
Zhu Yib481de92007-09-25 17:54:57 -07002885 goto done;
2886 }
2887 }
Johannes Berg811ecc92010-04-06 04:12:41 -07002888 scan = priv->scan_cmd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002889 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002890
2891 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2892 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2893
Johannes Berg246ed352010-08-23 10:46:32 +02002894 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Zhu Yib481de92007-09-25 17:54:57 -07002895 u16 interval = 0;
2896 u32 extra;
2897 u32 suspend_time = 100;
2898 u32 scan_suspend_time = 100;
2899 unsigned long flags;
2900
Tomas Winklere1623442009-01-27 14:27:56 -08002901 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002902
2903 spin_lock_irqsave(&priv->lock, flags);
John W. Linvillea6e492b2010-07-22 15:24:56 -04002904 if (priv->is_internal_short_scan)
2905 interval = 0;
2906 else
2907 interval = vif->bss_conf.beacon_int;
Zhu Yib481de92007-09-25 17:54:57 -07002908 spin_unlock_irqrestore(&priv->lock, flags);
2909
2910 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002911 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002912 if (!interval)
2913 interval = suspend_time;
2914 /*
2915 * suspend time format:
2916 * 0-19: beacon interval in usec (time before exec.)
2917 * 20-23: 0
2918 * 24-31: number of beacons (suspend between channels)
2919 */
2920
2921 extra = (suspend_time / interval) << 24;
2922 scan_suspend_time = 0xFF0FFFFF &
2923 (extra | ((suspend_time % interval) * 1024));
2924
2925 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002926 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002927 scan_suspend_time, interval);
2928 }
2929
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002930 if (priv->is_internal_short_scan) {
2931 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2932 } else if (priv->scan_request->n_ssids) {
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002933 int i, p = 0;
2934 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2935 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2936 /* always does wildcard anyway */
2937 if (!priv->scan_request->ssids[i].ssid_len)
2938 continue;
2939 scan->direct_scan[p].id = WLAN_EID_SSID;
2940 scan->direct_scan[p].len =
2941 priv->scan_request->ssids[i].ssid_len;
2942 memcpy(scan->direct_scan[p].ssid,
2943 priv->scan_request->ssids[i].ssid,
2944 priv->scan_request->ssids[i].ssid_len);
2945 n_probes++;
2946 p++;
2947 }
2948 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002949 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002950 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002951
2952 /* We don't build a direct scan probe request; the uCode will do
2953 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002954 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Johannes Berga194e322010-08-27 08:53:46 -07002955 scan->tx_cmd.sta_id = priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002956 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2957
2958 /* flags + rate selection */
2959
Johannes Berg00700ee2010-04-06 04:12:37 -07002960 switch (priv->scan_band) {
2961 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002962 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2963 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2964 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002965 band = IEEE80211_BAND_2GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002966 break;
2967 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002968 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Bergb097ad22009-04-20 14:36:57 -07002969 /*
2970 * If active scaning is requested but a certain channel
2971 * is marked passive, we can do active scanning if we
2972 * detect transmissions.
2973 */
Johannes Berg96ff5642010-04-30 14:42:15 -07002974 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2975 IWL_GOOD_CRC_TH_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +01002976 band = IEEE80211_BAND_5GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002977 break;
2978 default:
2979 IWL_WARN(priv, "Invalid scan band\n");
Zhu Yib481de92007-09-25 17:54:57 -07002980 goto done;
2981 }
2982
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002983 if (!priv->is_internal_short_scan) {
2984 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002985 iwl_fill_probe_req(priv,
2986 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002987 vif->addr,
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002988 priv->scan_request->ie,
2989 priv->scan_request->ie_len,
2990 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002991 } else {
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002992 /* use bcast addr, will not be transmitted but must be valid */
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002993 scan->tx_cmd.len = cpu_to_le16(
2994 iwl_fill_probe_req(priv,
2995 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002996 iwl_bcast_addr, NULL, 0,
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002997 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2998 }
Zhu Yib481de92007-09-25 17:54:57 -07002999 /* select Rx antennas */
3000 scan->flags |= iwl3945_get_antenna_flags(priv);
3001
Abhijeet Kolekar14023642010-06-02 21:15:10 -07003002 if (priv->is_internal_short_scan) {
3003 scan->channel_count =
3004 iwl3945_get_single_channel_for_scan(priv, vif, band,
3005 (void *)&scan->data[le16_to_cpu(
3006 scan->tx_cmd.len)]);
3007 } else {
3008 scan->channel_count =
3009 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
3010 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
3011 }
Zhu Yib481de92007-09-25 17:54:57 -07003012
Reinette Chatre14b54332008-11-04 12:21:35 -08003013 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08003014 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08003015 goto done;
3016 }
3017
Zhu Yib481de92007-09-25 17:54:57 -07003018 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003019 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003020 cmd.data = scan;
3021 scan->len = cpu_to_le16(cmd.len);
3022
3023 set_bit(STATUS_SCAN_HW, &priv->status);
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08003024 if (iwl_send_cmd_sync(priv, &cmd))
Zhu Yib481de92007-09-25 17:54:57 -07003025 goto done;
3026
3027 queue_delayed_work(priv->workqueue, &priv->scan_check,
3028 IWL_SCAN_CHECK_WATCHDOG);
3029
Zhu Yib481de92007-09-25 17:54:57 -07003030 return;
3031
3032 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003033 /* can not perform scan make sure we clear scanning
3034 * bits from status so next scan request can be performed.
3035 * if we dont clear scanning status bit here all next scan
3036 * will fail
3037 */
3038 clear_bit(STATUS_SCAN_HW, &priv->status);
3039 clear_bit(STATUS_SCANNING, &priv->status);
3040
Ian Schram01ebd062007-10-25 17:15:22 +08003041 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003042 queue_work(priv->workqueue, &priv->scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07003043}
3044
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003045static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003046{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003047 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003048
3049 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3050 return;
3051
Johannes Berg19cc1082009-05-08 13:44:36 -07003052 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
Johannes Berg8bd413e2010-08-23 10:46:40 +02003053 struct iwl_rxon_context *ctx;
Johannes Berg19cc1082009-05-08 13:44:36 -07003054 mutex_lock(&priv->mutex);
Johannes Berg8bd413e2010-08-23 10:46:40 +02003055 for_each_context(priv, ctx)
3056 ctx->vif = NULL;
Johannes Berg19cc1082009-05-08 13:44:36 -07003057 priv->is_open = 0;
3058 mutex_unlock(&priv->mutex);
3059 iwl3945_down(priv);
3060 ieee80211_restart_hw(priv->hw);
3061 } else {
3062 iwl3945_down(priv);
Johannes Berg80676512010-01-21 06:07:17 -08003063
3064 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3065 return;
3066
3067 mutex_lock(&priv->mutex);
3068 __iwl3945_up(priv);
3069 mutex_unlock(&priv->mutex);
Johannes Berg19cc1082009-05-08 13:44:36 -07003070 }
Zhu Yib481de92007-09-25 17:54:57 -07003071}
3072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003073static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003074{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003075 struct iwl_priv *priv =
3076 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003077
3078 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3079 return;
3080
3081 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003082 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003083 mutex_unlock(&priv->mutex);
3084}
3085
Johannes Berg1dda6d22010-04-29 04:43:06 -07003086void iwl3945_post_associate(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003087{
Zhu Yib481de92007-09-25 17:54:57 -07003088 int rc = 0;
3089 struct ieee80211_conf *conf = NULL;
Johannes Berg246ed352010-08-23 10:46:32 +02003090 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003091
Johannes Berg1dda6d22010-04-29 04:43:06 -07003092 if (!vif || !priv->is_open)
3093 return;
3094
3095 if (vif->type == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003096 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003097 return;
3098 }
3099
Tomas Winklere1623442009-01-27 14:27:56 -08003100 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003101 vif->bss_conf.aid, ctx->active.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003102
3103 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3104 return;
3105
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003106 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003107
Zhu Yib481de92007-09-25 17:54:57 -07003108 conf = ieee80211_get_hw_conf(priv->hw);
3109
Johannes Berg246ed352010-08-23 10:46:32 +02003110 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3111 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003112
Johannes Berg47313e32010-08-23 10:46:55 +02003113 rc = iwl_send_rxon_timing(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003114 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003115 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003116 "Attempting to continue.\n");
3117
Johannes Berg246ed352010-08-23 10:46:32 +02003118 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003119
Johannes Berg246ed352010-08-23 10:46:32 +02003120 ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
Zhu Yib481de92007-09-25 17:54:57 -07003121
Tomas Winklere1623442009-01-27 14:27:56 -08003122 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Johannes Berg1dda6d22010-04-29 04:43:06 -07003123 vif->bss_conf.aid, vif->bss_conf.beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07003124
Johannes Bergc213d742010-05-06 12:21:40 -07003125 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003126 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003127 else
Johannes Berg246ed352010-08-23 10:46:32 +02003128 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003129
Johannes Berg246ed352010-08-23 10:46:32 +02003130 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003131 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003132 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003133 else
Johannes Berg246ed352010-08-23 10:46:32 +02003134 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003135 }
3136
Johannes Berg246ed352010-08-23 10:46:32 +02003137 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003138
Johannes Berg1dda6d22010-04-29 04:43:06 -07003139 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003140 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003141 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003142 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02003143 case NL80211_IFTYPE_ADHOC:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003144 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003145 break;
Zhu Yib481de92007-09-25 17:54:57 -07003146 default:
Johannes Berg1dda6d22010-04-29 04:43:06 -07003147 IWL_ERR(priv, "%s Should not be called in %d mode\n",
3148 __func__, vif->type);
Zhu Yib481de92007-09-25 17:54:57 -07003149 break;
3150 }
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003151}
3152
Zhu Yib481de92007-09-25 17:54:57 -07003153/*****************************************************************************
3154 *
3155 * mac80211 entry point functions
3156 *
3157 *****************************************************************************/
3158
Zhu Yi5a669262008-01-14 17:46:18 -08003159#define UCODE_READY_TIMEOUT (2 * HZ)
3160
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003161static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003162{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003163 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08003164 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003165
Tomas Winklere1623442009-01-27 14:27:56 -08003166 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003167
3168 /* we should be verifying the device is ready to be opened */
3169 mutex_lock(&priv->mutex);
3170
Zhu Yi5a669262008-01-14 17:46:18 -08003171 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3172 * ucode filename and max sizes are card-specific. */
3173
3174 if (!priv->ucode_code.len) {
3175 ret = iwl3945_read_ucode(priv);
3176 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003177 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08003178 mutex_unlock(&priv->mutex);
3179 goto out_release_irq;
3180 }
3181 }
3182
Zhu Yie655b9f2008-01-24 02:19:38 -08003183 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08003184
Zhu Yib481de92007-09-25 17:54:57 -07003185 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08003186
Zhu Yie655b9f2008-01-24 02:19:38 -08003187 if (ret)
3188 goto out_release_irq;
3189
Tomas Winklere1623442009-01-27 14:27:56 -08003190 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003191
Zhu Yi5a669262008-01-14 17:46:18 -08003192 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3193 * mac80211 will not be run successfully. */
3194 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3195 test_bit(STATUS_READY, &priv->status),
3196 UCODE_READY_TIMEOUT);
3197 if (!ret) {
3198 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003199 IWL_ERR(priv,
3200 "Wait for START_ALIVE timeout after %dms.\n",
3201 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08003202 ret = -ETIMEDOUT;
3203 goto out_release_irq;
3204 }
3205 }
3206
Helmut Schaa26635162009-01-15 09:38:44 +01003207 /* ucode is running and will send rfkill notifications,
3208 * no need to poll the killswitch state anymore */
Johannes Bergee525d12010-01-21 06:09:28 -08003209 cancel_delayed_work(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01003210
Johannes Berge932a602009-10-02 13:44:03 -07003211 iwl_led_start(priv);
3212
Zhu Yie655b9f2008-01-24 02:19:38 -08003213 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003214 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003215 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003216
3217out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003218 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003219 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08003220 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003221}
3222
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003223static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003224{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003225 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003226
Tomas Winklere1623442009-01-27 14:27:56 -08003227 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003228
Zhu Yie655b9f2008-01-24 02:19:38 -08003229 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003230 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003231 return;
3232 }
3233
Zhu Yib481de92007-09-25 17:54:57 -07003234 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08003235
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003236 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003237 /* stop mac, cancel any scan request and clear
3238 * RXON_FILTER_ASSOC_MSK BIT
3239 */
Zhu Yi5a669262008-01-14 17:46:18 -08003240 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003241 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003242 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003243 }
3244
Zhu Yi5a669262008-01-14 17:46:18 -08003245 iwl3945_down(priv);
3246
3247 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003248
3249 /* start polling the killswitch state again */
Johannes Bergee525d12010-01-21 06:09:28 -08003250 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01003251 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003252
Tomas Winklere1623442009-01-27 14:27:56 -08003253 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003254}
3255
Johannes Berge039fa42008-05-15 12:55:29 +02003256static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003257{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003258 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003259
Tomas Winklere1623442009-01-27 14:27:56 -08003260 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003261
Tomas Winklere1623442009-01-27 14:27:56 -08003262 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003263 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003264
Johannes Berge039fa42008-05-15 12:55:29 +02003265 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003266 dev_kfree_skb_any(skb);
3267
Tomas Winklere1623442009-01-27 14:27:56 -08003268 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003269 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003270}
3271
Johannes Berg1dda6d22010-04-29 04:43:06 -07003272void iwl3945_config_ap(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003273{
Johannes Berg246ed352010-08-23 10:46:32 +02003274 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003275 int rc = 0;
3276
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003277 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003278 return;
3279
3280 /* The following should be done only at AP bring up */
Johannes Berg246ed352010-08-23 10:46:32 +02003281 if (!(iwl_is_associated(priv, IWL_RXON_CTX_BSS))) {
Zhu Yib481de92007-09-25 17:54:57 -07003282
3283 /* RXON - unassoc (to set timing command) */
Johannes Berg246ed352010-08-23 10:46:32 +02003284 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3285 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003286
3287 /* RXON Timing */
Johannes Berg47313e32010-08-23 10:46:55 +02003288 rc = iwl_send_rxon_timing(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003289 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003290 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003291 "Attempting to continue.\n");
3292
Johannes Berg246ed352010-08-23 10:46:32 +02003293 ctx->staging.assoc_id = 0;
Johannes Berg1dda6d22010-04-29 04:43:06 -07003294
Johannes Bergc213d742010-05-06 12:21:40 -07003295 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003296 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003297 RXON_FLG_SHORT_PREAMBLE_MSK;
3298 else
Johannes Berg246ed352010-08-23 10:46:32 +02003299 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003300 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3301
Johannes Berg246ed352010-08-23 10:46:32 +02003302 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003303 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003304 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003305 RXON_FLG_SHORT_SLOT_MSK;
3306 else
Johannes Berg246ed352010-08-23 10:46:32 +02003307 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003308 ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003309 }
3310 /* restore RXON assoc */
Johannes Berg246ed352010-08-23 10:46:32 +02003311 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
3312 iwlcore_commit_rxon(priv, ctx);
Zhu Yi556f8db2007-09-27 11:27:33 +08003313 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003314 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003315
3316 /* FIXME - we need to add code here to detect a totally new
3317 * configuration, reset the AP, unassoc, rxon timing, assoc,
3318 * clear sta table, add BCAST sta... */
3319}
3320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003321static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003322 struct ieee80211_vif *vif,
3323 struct ieee80211_sta *sta,
3324 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003325{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003326 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003327 int ret = 0;
3328 u8 sta_id = IWL_INVALID_STATION;
3329 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003330
Tomas Winklere1623442009-01-27 14:27:56 -08003331 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003332
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003333 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003334 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003335 return -EOPNOTSUPP;
3336 }
3337
Johannes Berg246ed352010-08-23 10:46:32 +02003338 static_key = !iwl_is_associated(priv, IWL_RXON_CTX_BSS);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003339
3340 if (!static_key) {
Johannes Berga194e322010-08-27 08:53:46 -07003341 sta_id = iwl_sta_id_or_broadcast(
3342 priv, &priv->contexts[IWL_RXON_CTX_BSS], sta);
Johannes Berg0af8bca2010-04-30 14:08:00 -07003343 if (sta_id == IWL_INVALID_STATION)
3344 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003345 }
3346
3347 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003348 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003349
Zhu Yib481de92007-09-25 17:54:57 -07003350 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003351 case SET_KEY:
3352 if (static_key)
3353 ret = iwl3945_set_static_key(priv, key);
3354 else
3355 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3356 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003357 break;
3358 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003359 if (static_key)
3360 ret = iwl3945_remove_static_key(priv);
3361 else
3362 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3363 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003364 break;
3365 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003366 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003367 }
3368
Johannes Berg72e15d72010-02-19 11:42:32 -08003369 mutex_unlock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003370 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003371
Winkler, Tomas42986792009-01-19 15:30:22 -08003372 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003373}
3374
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003375static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3376 struct ieee80211_vif *vif,
3377 struct ieee80211_sta *sta)
3378{
3379 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07003380 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003381 int ret;
Johannes Bergfd1af152010-04-30 11:30:43 -07003382 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003383 u8 sta_id;
3384
3385 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3386 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003387 mutex_lock(&priv->mutex);
3388 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
3389 sta->addr);
3390 sta_priv->common.sta_id = IWL_INVALID_STATION;
3391
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003392
Johannes Berga194e322010-08-27 08:53:46 -07003393 ret = iwl_add_station_common(priv, &priv->contexts[IWL_RXON_CTX_BSS],
Johannes Berg238d7812010-08-23 10:46:45 +02003394 sta->addr, is_ap, sta, &sta_id);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003395 if (ret) {
3396 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3397 sta->addr, ret);
3398 /* Should we return success if return code is EEXIST ? */
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003399 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003400 return ret;
3401 }
3402
Johannes Bergfd1af152010-04-30 11:30:43 -07003403 sta_priv->common.sta_id = sta_id;
3404
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003405 /* Initialize rate scaling */
Frans Pop91dd6c22010-03-24 14:19:58 -07003406 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003407 sta->addr);
3408 iwl3945_rs_rate_init(priv, sta, sta_id);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003409 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003410
3411 return 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003412}
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003413
3414static void iwl3945_configure_filter(struct ieee80211_hw *hw,
3415 unsigned int changed_flags,
3416 unsigned int *total_flags,
3417 u64 multicast)
3418{
3419 struct iwl_priv *priv = hw->priv;
3420 __le32 filter_or = 0, filter_nand = 0;
Johannes Berg246ed352010-08-23 10:46:32 +02003421 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003422
3423#define CHK(test, flag) do { \
3424 if (*total_flags & (test)) \
3425 filter_or |= (flag); \
3426 else \
3427 filter_nand |= (flag); \
3428 } while (0)
3429
3430 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
3431 changed_flags, *total_flags);
3432
3433 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
3434 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
3435 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
3436
3437#undef CHK
3438
3439 mutex_lock(&priv->mutex);
3440
Johannes Berg246ed352010-08-23 10:46:32 +02003441 ctx->staging.filter_flags &= ~filter_nand;
3442 ctx->staging.filter_flags |= filter_or;
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003443
3444 /*
3445 * Committing directly here breaks for some reason,
3446 * but we'll eventually commit the filter flags
3447 * change anyway.
3448 */
3449
3450 mutex_unlock(&priv->mutex);
3451
3452 /*
3453 * Receiving all multicast frames is always enabled by the
3454 * default flags setup in iwl_connection_init_rx_config()
3455 * since we currently do not support programming multicast
3456 * filters into the device.
3457 */
3458 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
3459 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
3460}
3461
3462
Zhu Yib481de92007-09-25 17:54:57 -07003463/*****************************************************************************
3464 *
3465 * sysfs attributes
3466 *
3467 *****************************************************************************/
3468
Samuel Ortizd08853a2009-01-23 13:45:17 -08003469#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003470
3471/*
3472 * The following adds a new attribute to the sysfs representation
3473 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3474 * used for controlling the debug level.
3475 *
3476 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003477 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003478 * The debug_level being managed using sysfs below is a per device debug
3479 * level that is used instead of the global debug level if it (the per
3480 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003481 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003482static ssize_t show_debug_level(struct device *d,
3483 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003484{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003485 struct iwl_priv *priv = dev_get_drvdata(d);
3486 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003487}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003488static ssize_t store_debug_level(struct device *d,
3489 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003490 const char *buf, size_t count)
3491{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003492 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003493 unsigned long val;
3494 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003495
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003496 ret = strict_strtoul(buf, 0, &val);
3497 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003498 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003499 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003500 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003501 if (iwl_alloc_traffic_mem(priv))
3502 IWL_ERR(priv,
3503 "Not enough memory to generate traffic log\n");
3504 }
Zhu Yib481de92007-09-25 17:54:57 -07003505 return strnlen(buf, count);
3506}
3507
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003508static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3509 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003510
Samuel Ortizd08853a2009-01-23 13:45:17 -08003511#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003512
Zhu Yib481de92007-09-25 17:54:57 -07003513static ssize_t show_temperature(struct device *d,
3514 struct device_attribute *attr, char *buf)
3515{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003516 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003517
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003518 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003519 return -EAGAIN;
3520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003521 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003522}
3523
3524static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3525
Zhu Yib481de92007-09-25 17:54:57 -07003526static ssize_t show_tx_power(struct device *d,
3527 struct device_attribute *attr, char *buf)
3528{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003529 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003530 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003531}
3532
3533static ssize_t store_tx_power(struct device *d,
3534 struct device_attribute *attr,
3535 const char *buf, size_t count)
3536{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003537 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003538 char *p = (char *)buf;
3539 u32 val;
3540
3541 val = simple_strtoul(p, &p, 10);
3542 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003543 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003544 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003545 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003546
3547 return count;
3548}
3549
3550static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3551
3552static ssize_t show_flags(struct device *d,
3553 struct device_attribute *attr, char *buf)
3554{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003555 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003556 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003557
Johannes Berg246ed352010-08-23 10:46:32 +02003558 return sprintf(buf, "0x%04X\n", ctx->active.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003559}
3560
3561static ssize_t store_flags(struct device *d,
3562 struct device_attribute *attr,
3563 const char *buf, size_t count)
3564{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003565 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003566 u32 flags = simple_strtoul(buf, NULL, 0);
Johannes Berg246ed352010-08-23 10:46:32 +02003567 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003568
3569 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003570 if (le32_to_cpu(ctx->staging.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003571 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003572 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003573 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003574 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003575 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003576 flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003577 ctx->staging.flags = cpu_to_le32(flags);
3578 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003579 }
3580 }
3581 mutex_unlock(&priv->mutex);
3582
3583 return count;
3584}
3585
3586static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3587
3588static ssize_t show_filter_flags(struct device *d,
3589 struct device_attribute *attr, char *buf)
3590{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003591 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003592 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003593
3594 return sprintf(buf, "0x%04X\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003595 le32_to_cpu(ctx->active.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003596}
3597
3598static ssize_t store_filter_flags(struct device *d,
3599 struct device_attribute *attr,
3600 const char *buf, size_t count)
3601{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003602 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003603 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003604 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3605
3606 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003607 if (le32_to_cpu(ctx->staging.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003608 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003609 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003610 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003611 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003612 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003613 "0x%04X\n", filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003614 ctx->staging.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003615 cpu_to_le32(filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003616 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003617 }
3618 }
3619 mutex_unlock(&priv->mutex);
3620
3621 return count;
3622}
3623
3624static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3625 store_filter_flags);
3626
Zhu Yib481de92007-09-25 17:54:57 -07003627static ssize_t show_measurement(struct device *d,
3628 struct device_attribute *attr, char *buf)
3629{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003630 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003631 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003632 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003633 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003634 unsigned long flags;
3635
3636 spin_lock_irqsave(&priv->lock, flags);
3637 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3638 spin_unlock_irqrestore(&priv->lock, flags);
3639 return 0;
3640 }
3641 memcpy(&measure_report, &priv->measure_report, size);
3642 priv->measurement_status = 0;
3643 spin_unlock_irqrestore(&priv->lock, flags);
3644
3645 while (size && (PAGE_SIZE - len)) {
3646 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3647 PAGE_SIZE - len, 1);
3648 len = strlen(buf);
3649 if (PAGE_SIZE - len)
3650 buf[len++] = '\n';
3651
3652 ofs += 16;
3653 size -= min(size, 16U);
3654 }
3655
3656 return len;
3657}
3658
3659static ssize_t store_measurement(struct device *d,
3660 struct device_attribute *attr,
3661 const char *buf, size_t count)
3662{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003663 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003664 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003665 struct ieee80211_measurement_params params = {
Johannes Berg246ed352010-08-23 10:46:32 +02003666 .channel = le16_to_cpu(ctx->active.channel),
Johannes Berge99f1682010-01-19 10:04:28 -08003667 .start_time = cpu_to_le64(priv->_3945.last_tsf),
Zhu Yib481de92007-09-25 17:54:57 -07003668 .duration = cpu_to_le16(1),
3669 };
3670 u8 type = IWL_MEASURE_BASIC;
3671 u8 buffer[32];
3672 u8 channel;
3673
3674 if (count) {
3675 char *p = buffer;
3676 strncpy(buffer, buf, min(sizeof(buffer), count));
3677 channel = simple_strtoul(p, NULL, 0);
3678 if (channel)
3679 params.channel = channel;
3680
3681 p = buffer;
3682 while (*p && *p != ' ')
3683 p++;
3684 if (*p)
3685 type = simple_strtoul(p + 1, NULL, 0);
3686 }
3687
Tomas Winklere1623442009-01-27 14:27:56 -08003688 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003689 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003690 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003691
3692 return count;
3693}
3694
3695static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3696 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003697
Zhu Yib481de92007-09-25 17:54:57 -07003698static ssize_t store_retry_rate(struct device *d,
3699 struct device_attribute *attr,
3700 const char *buf, size_t count)
3701{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003702 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003703
3704 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3705 if (priv->retry_rate <= 0)
3706 priv->retry_rate = 1;
3707
3708 return count;
3709}
3710
3711static ssize_t show_retry_rate(struct device *d,
3712 struct device_attribute *attr, char *buf)
3713{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003714 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003715 return sprintf(buf, "%d", priv->retry_rate);
3716}
3717
3718static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3719 store_retry_rate);
3720
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003721
Zhu Yib481de92007-09-25 17:54:57 -07003722static ssize_t show_channels(struct device *d,
3723 struct device_attribute *attr, char *buf)
3724{
Johannes Berg8318d782008-01-24 19:38:38 +01003725 /* all this shit doesn't belong into sysfs anyway */
3726 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003727}
3728
3729static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3730
Zhu Yib481de92007-09-25 17:54:57 -07003731static ssize_t show_antenna(struct device *d,
3732 struct device_attribute *attr, char *buf)
3733{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003734 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003735
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003736 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003737 return -EAGAIN;
3738
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003739 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003740}
3741
3742static ssize_t store_antenna(struct device *d,
3743 struct device_attribute *attr,
3744 const char *buf, size_t count)
3745{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003746 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003747 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003748
3749 if (count == 0)
3750 return 0;
3751
3752 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003753 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003754 return count;
3755 }
3756
3757 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003758 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003759 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003760 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003761 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003762
3763
3764 return count;
3765}
3766
3767static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3768
3769static ssize_t show_status(struct device *d,
3770 struct device_attribute *attr, char *buf)
3771{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003772 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003773 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003774 return -EAGAIN;
3775 return sprintf(buf, "0x%08x\n", (int)priv->status);
3776}
3777
3778static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3779
3780static ssize_t dump_error_log(struct device *d,
3781 struct device_attribute *attr,
3782 const char *buf, size_t count)
3783{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003784 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003785 char *p = (char *)buf;
3786
3787 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003788 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003789
3790 return strnlen(buf, count);
3791}
3792
3793static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3794
Zhu Yib481de92007-09-25 17:54:57 -07003795/*****************************************************************************
3796 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003797 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003798 *
3799 *****************************************************************************/
3800
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003801static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003802{
Reinette Chatred21050c2009-02-13 11:51:18 -08003803 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003804
3805 init_waitqueue_head(&priv->wait_command_queue);
3806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3808 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003809 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003810 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3811 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Johannes Bergee525d12010-01-21 06:09:28 -08003812 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02003813
3814 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003816 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003817
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003818 if (priv->cfg->ops->lib->recover_from_tx_stall) {
3819 init_timer(&priv->monitor_recover);
3820 priv->monitor_recover.data = (unsigned long)priv;
3821 priv->monitor_recover.function =
3822 priv->cfg->ops->lib->recover_from_tx_stall;
3823 }
3824
Zhu Yib481de92007-09-25 17:54:57 -07003825 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003826 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003827}
3828
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003829static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003830{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003831 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003832
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003833 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003834 cancel_delayed_work(&priv->scan_check);
3835 cancel_delayed_work(&priv->alive_start);
Abhijeet Kolekar1e460532010-04-23 14:14:53 -07003836 cancel_work_sync(&priv->start_internal_scan);
Zhu Yib481de92007-09-25 17:54:57 -07003837 cancel_work_sync(&priv->beacon_update);
3838}
3839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003840static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003841 &dev_attr_antenna.attr,
3842 &dev_attr_channels.attr,
3843 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003844 &dev_attr_flags.attr,
3845 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003846 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003847 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003848 &dev_attr_status.attr,
3849 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003850 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003851#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003852 &dev_attr_debug_level.attr,
3853#endif
Zhu Yib481de92007-09-25 17:54:57 -07003854 NULL
3855};
3856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003857static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003858 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003859 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003860};
3861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003862static struct ieee80211_ops iwl3945_hw_ops = {
3863 .tx = iwl3945_mac_tx,
3864 .start = iwl3945_mac_start,
3865 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003866 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003867 .remove_interface = iwl_mac_remove_interface,
Abhijeet Kolekar48083682009-04-08 11:26:48 -07003868 .config = iwl_mac_config,
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003869 .configure_filter = iwl3945_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003870 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003871 .conf_tx = iwl_mac_conf_tx,
Abhijeet Kolekarbd564262009-04-08 11:26:52 -07003872 .reset_tsf = iwl_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003873 .bss_info_changed = iwl_bss_info_changed,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003874 .hw_scan = iwl_mac_hw_scan,
3875 .sta_add = iwl3945_mac_sta_add,
3876 .sta_remove = iwl_mac_sta_remove,
Johannes Berga85d7cc2010-07-31 08:34:10 -07003877 .tx_last_beacon = iwl_mac_tx_last_beacon,
Zhu Yib481de92007-09-25 17:54:57 -07003878};
3879
Winkler, Tomase52119c2008-12-22 11:31:19 +08003880static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003881{
3882 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003883 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003884
3885 priv->retry_rate = 1;
3886 priv->ibss_beacon = NULL;
3887
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003888 spin_lock_init(&priv->sta_lock);
3889 spin_lock_init(&priv->hcmd_lock);
3890
3891 INIT_LIST_HEAD(&priv->free_frames);
3892
3893 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003894 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003895
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003896 priv->ieee_channels = NULL;
3897 priv->ieee_rates = NULL;
3898 priv->band = IEEE80211_BAND_2GHZ;
3899
3900 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003901 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003902
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003903 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003904
Samuel Ortize6148912009-01-23 13:45:15 -08003905 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3906 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3907 eeprom->version);
3908 ret = -EINVAL;
3909 goto err;
3910 }
3911 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003912 if (ret) {
3913 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3914 goto err;
3915 }
3916
Samuel Ortize6148912009-01-23 13:45:15 -08003917 /* Set up txpower settings in driver for all channels */
3918 if (iwl3945_txpower_set_from_eeprom(priv)) {
3919 ret = -EIO;
3920 goto err_free_channel_map;
3921 }
3922
Samuel Ortiz534166d2009-01-23 13:45:16 -08003923 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003924 if (ret) {
3925 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3926 goto err_free_channel_map;
3927 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003928 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3929
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003930 return 0;
3931
3932err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003933 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003934err:
3935 return ret;
3936}
3937
Johannes Bergdd7a2502010-04-28 23:33:10 -07003938#define IWL3945_MAX_PROBE_REQUEST 200
3939
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003940static int iwl3945_setup_mac(struct iwl_priv *priv)
3941{
3942 int ret;
3943 struct ieee80211_hw *hw = priv->hw;
3944
3945 hw->rate_control_algorithm = "iwl-3945-rs";
3946 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
Johannes Bergfd1af152010-04-30 11:30:43 -07003947 hw->vif_data_size = sizeof(struct iwl_vif_priv);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003948
3949 /* Tell mac80211 our characteristics */
3950 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003951 IEEE80211_HW_SPECTRUM_MGMT;
3952
3953 if (!priv->cfg->broken_powersave)
3954 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3955 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003956
3957 hw->wiphy->interface_modes =
Johannes Bergd0fe4782010-08-23 10:46:58 +02003958 priv->contexts[IWL_RXON_CTX_BSS].interface_modes;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003959
Reinette Chatref6c8f152010-03-12 11:13:26 -08003960 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
Johannes Berg5be83de2009-11-19 00:56:28 +01003961 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003962
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003963 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3964 /* we create the 802.11 header and a zero-length SSID element */
Johannes Bergdd7a2502010-04-28 23:33:10 -07003965 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003966
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003967 /* Default value; 4 EDCA QOS priorities */
3968 hw->queues = 4;
3969
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003970 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3971 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3972 &priv->bands[IEEE80211_BAND_2GHZ];
3973
3974 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3975 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3976 &priv->bands[IEEE80211_BAND_5GHZ];
3977
3978 ret = ieee80211_register_hw(priv->hw);
3979 if (ret) {
3980 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3981 return ret;
3982 }
3983 priv->mac80211_registered = 1;
3984
3985 return 0;
3986}
3987
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003988static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003989{
Johannes Berg246ed352010-08-23 10:46:32 +02003990 int err = 0, i;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003991 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003992 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003993 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003994 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003995 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003996
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003997 /***********************
3998 * 1. Allocating HW data
3999 * ********************/
4000
Zhu Yib481de92007-09-25 17:54:57 -07004001 /* mac80211 allocates memory for this device instance, including
4002 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004003 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07004004 if (hw == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004005 pr_err("Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07004006 err = -ENOMEM;
4007 goto out;
4008 }
Zhu Yib481de92007-09-25 17:54:57 -07004009 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004010 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004011
Johannes Berg13bb9482010-08-23 10:46:33 +02004012 priv->cmd_queue = IWL39_CMD_QUEUE_NUM;
4013
Johannes Berg246ed352010-08-23 10:46:32 +02004014 /* 3945 has only one valid context */
4015 priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
4016
4017 for (i = 0; i < NUM_IWL_RXON_CTX; i++)
4018 priv->contexts[i].ctxid = i;
4019
Johannes Berg8f2d3d22010-08-23 10:46:37 +02004020 priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
4021 priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
4022 priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
Johannes Berg8dfdb9d2010-08-23 10:46:38 +02004023 priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
Johannes Berg2995baf2010-08-23 10:46:42 +02004024 priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
Johannes Bergc10afb62010-08-23 10:46:43 +02004025 priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
Johannes Bergd0fe4782010-08-23 10:46:58 +02004026 priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
4027 BIT(NL80211_IFTYPE_STATION) |
4028 BIT(NL80211_IFTYPE_ADHOC);
4029 priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
4030 priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
4031 priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
Johannes Berg8f2d3d22010-08-23 10:46:37 +02004032
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004033 /*
4034 * Disabling hardware scan means that mac80211 will perform scans
4035 * "the hard way", rather than using device's scan.
4036 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004037 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08004038 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004039 iwl3945_hw_ops.hw_scan = NULL;
4040 }
4041
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004042
Tomas Winklere1623442009-01-27 14:27:56 -08004043 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004044 priv->cfg = cfg;
4045 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07004046 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004047
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004048 if (iwl_alloc_traffic_mem(priv))
4049 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07004050
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004051 /***************************
4052 * 2. Initializing PCI bus
4053 * *************************/
John W. Linville1a7123c2010-08-05 14:39:31 -04004054 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4055 PCIE_LINK_STATE_CLKPM);
4056
Zhu Yib481de92007-09-25 17:54:57 -07004057 if (pci_enable_device(pdev)) {
4058 err = -ENODEV;
4059 goto out_ieee80211_free_hw;
4060 }
4061
4062 pci_set_master(pdev);
4063
Yang Hongyang284901a2009-04-06 19:01:15 -07004064 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004065 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004066 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004067 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004068 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004069 goto out_pci_disable_device;
4070 }
4071
4072 pci_set_drvdata(pdev, priv);
4073 err = pci_request_regions(pdev, DRV_NAME);
4074 if (err)
4075 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004076
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004077 /***********************
4078 * 3. Read REV Register
4079 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004080 priv->hw_base = pci_iomap(pdev, 0, 0);
4081 if (!priv->hw_base) {
4082 err = -ENODEV;
4083 goto out_pci_release_regions;
4084 }
4085
Tomas Winklere1623442009-01-27 14:27:56 -08004086 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004087 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004088 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004089
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004090 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4091 * PCI Tx retries from interfering with C3 CPU state */
4092 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004093
Reinette Chatre731a29b2009-12-14 14:12:11 -08004094 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004095 * we should init now
4096 */
4097 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004098 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004099
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004100 /*
4101 * stop and reset the on-board processor just in case it is in a
4102 * strange state ... like being left stranded by a primary kernel
4103 * and this is now the kdump kernel trying to start up
4104 */
4105 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4106
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004107 /***********************
4108 * 4. Read EEPROM
4109 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004110
Zhu Yi5a669262008-01-14 17:46:18 -08004111 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004112 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004113 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004114 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004115 goto out_iounmap;
Zhu Yi5a669262008-01-14 17:46:18 -08004116 }
4117 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004118 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Johannes Berg30eabc12010-05-12 03:33:10 -07004119 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
4120 SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
Zhu Yi5a669262008-01-14 17:46:18 -08004121
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004122 /***********************
4123 * 5. Setup HW Constants
4124 * ********************/
4125 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004126 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004127 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004128 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004129 }
4130
4131 /***********************
4132 * 6. Setup priv
4133 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004134
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004135 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004136 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004137 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004138 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004139 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004140
Tomas Winkler978785a2008-12-19 10:37:31 +08004141 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4142 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004143
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004144 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004145 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004146 * ********************/
4147
4148 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004149 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004150 spin_unlock_irqrestore(&priv->lock, flags);
4151
Helmut Schaa26635162009-01-15 09:38:44 +01004152 pci_enable_msi(priv->pci_dev);
4153
Mohamed Abbasef850d72009-05-22 11:01:50 -07004154 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
4155 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004156 if (err) {
4157 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4158 goto out_disable_msi;
4159 }
4160
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004161 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4162 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004163 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004164 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004165 }
4166
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004167 iwl_set_rxon_channel(priv,
Johannes Berg246ed352010-08-23 10:46:32 +02004168 &priv->bands[IEEE80211_BAND_2GHZ].channels[5],
4169 &priv->contexts[IWL_RXON_CTX_BSS]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004170 iwl3945_setup_deferred_work(priv);
4171 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004172 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004173
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004174 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004175 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004176 * *******************************/
4177
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004178 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004179
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004180 err = iwl3945_setup_mac(priv);
4181 if (err)
4182 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004183
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004184 err = iwl_dbgfs_register(priv, DRV_NAME);
4185 if (err)
4186 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4187
Helmut Schaa26635162009-01-15 09:38:44 +01004188 /* Start monitoring the killswitch */
Johannes Bergee525d12010-01-21 06:09:28 -08004189 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01004190 2 * HZ);
4191
Zhu Yib481de92007-09-25 17:54:57 -07004192 return 0;
4193
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004194 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004195 destroy_workqueue(priv->workqueue);
4196 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004197 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4198 out_release_irq:
4199 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004200 out_disable_msi:
4201 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004202 iwlcore_free_geos(priv);
4203 iwl_free_channel_map(priv);
4204 out_unset_hw_params:
4205 iwl3945_unset_hw_params(priv);
4206 out_eeprom_free:
4207 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004208 out_iounmap:
4209 pci_iounmap(pdev, priv->hw_base);
4210 out_pci_release_regions:
4211 pci_release_regions(pdev);
4212 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004213 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004214 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004215 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004216 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004217 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004218 out:
4219 return err;
4220}
4221
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004222static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004223{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004224 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004225 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004226
4227 if (!priv)
4228 return;
4229
Tomas Winklere1623442009-01-27 14:27:56 -08004230 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004231
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004232 iwl_dbgfs_unregister(priv);
4233
Zhu Yib481de92007-09-25 17:54:57 -07004234 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004235
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004236 if (priv->mac80211_registered) {
4237 ieee80211_unregister_hw(priv->hw);
4238 priv->mac80211_registered = 0;
4239 } else {
4240 iwl3945_down(priv);
4241 }
Zhu Yib481de92007-09-25 17:54:57 -07004242
Ben Cahillc166b252009-10-23 13:42:35 -07004243 /*
4244 * Make sure device is reset to low power before unloading driver.
4245 * This may be redundant with iwl_down(), but there are paths to
4246 * run iwl_down() without calling apm_ops.stop(), and there are
4247 * paths to avoid running iwl_down() at all before leaving driver.
4248 * This (inexpensive) call *makes sure* device is reset.
4249 */
4250 priv->cfg->ops->lib->apm_ops.stop(priv);
4251
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004252 /* make sure we flush any pending irq or
4253 * tasklet for the driver
4254 */
4255 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004256 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004257 spin_unlock_irqrestore(&priv->lock, flags);
4258
4259 iwl_synchronize_irq(priv);
4260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004261 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004262
Johannes Bergee525d12010-01-21 06:09:28 -08004263 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004265 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004266
4267 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004268 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004269 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004270
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004271 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004272
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004273 /*netif_stop_queue(dev); */
4274 flush_workqueue(priv->workqueue);
4275
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004276 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004277 * priv->workqueue... so we can't take down the workqueue
4278 * until now... */
4279 destroy_workqueue(priv->workqueue);
4280 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004281 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004282
Helmut Schaa26635162009-01-15 09:38:44 +01004283 free_irq(pdev->irq, priv);
4284 pci_disable_msi(pdev);
4285
Zhu Yib481de92007-09-25 17:54:57 -07004286 pci_iounmap(pdev, priv->hw_base);
4287 pci_release_regions(pdev);
4288 pci_disable_device(pdev);
4289 pci_set_drvdata(pdev, NULL);
4290
Samuel Ortize6148912009-01-23 13:45:15 -08004291 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004292 iwlcore_free_geos(priv);
Johannes Berg811ecc92010-04-06 04:12:41 -07004293 kfree(priv->scan_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07004294 if (priv->ibss_beacon)
4295 dev_kfree_skb(priv->ibss_beacon);
4296
4297 ieee80211_free_hw(priv->hw);
4298}
4299
Zhu Yib481de92007-09-25 17:54:57 -07004300
4301/*****************************************************************************
4302 *
4303 * driver and module entry point
4304 *
4305 *****************************************************************************/
4306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004307static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004308 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004309 .id_table = iwl3945_hw_card_ids,
4310 .probe = iwl3945_pci_probe,
4311 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004312#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004313 .suspend = iwl_pci_suspend,
4314 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004315#endif
4316};
4317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004318static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004319{
4320
4321 int ret;
Joe Perchesc96c31e2010-07-26 14:39:58 -07004322 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
4323 pr_info(DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004324
4325 ret = iwl3945_rate_control_register();
4326 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004327 pr_err("Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004328 return ret;
4329 }
4330
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004331 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004332 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004333 pr_err("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004334 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004335 }
Zhu Yib481de92007-09-25 17:54:57 -07004336
4337 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004338
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004339error_register:
4340 iwl3945_rate_control_unregister();
4341 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004342}
4343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004344static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004345{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004346 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004347 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004348}
4349
Reinette Chatrea0987a82008-12-02 12:14:06 -08004350MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004351
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004352module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004353MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004354module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004355MODULE_PARM_DESC(swcrypto,
4356 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004357#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004358module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004359MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004360#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004361module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4362 int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004363MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004364module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004365MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4366
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004367module_exit(iwl3945_exit);
4368module_init(iwl3945_init);