blob: faa2e0037e1075a0a3c13e452e5071e81d313c8b [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
147 if (sta_id == priv->hw_params.bcast_sta_id)
148 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 Berg6abbe552010-07-15 05:59:07 -0700320 if (!iwl_is_associated(priv) || !priv->ibss_beacon)
Zhu Yib481de92007-09-25 17:54:57 -0700321 return 0;
322
323 if (priv->ibss_beacon->len > left)
324 return 0;
325
326 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
327
328 return priv->ibss_beacon->len;
329}
330
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800331static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700332{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800333 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700334 unsigned int frame_size;
335 int rc;
336 u8 rate;
337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800338 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700339
340 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800341 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700342 "command.\n");
343 return -ENOMEM;
344 }
345
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800346 rate = iwl_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800348 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700349
Samuel Ortiz518099a2009-01-19 15:30:27 -0800350 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700351 &frame->u.cmd[0]);
352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800353 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700354
355 return rc;
356}
357
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800358static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700359{
Johannes Bergee525d12010-01-21 06:09:28 -0800360 if (priv->_3945.shared_virt)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800361 dma_free_coherent(&priv->pci_dev->dev,
362 sizeof(struct iwl3945_shared),
Johannes Bergee525d12010-01-21 06:09:28 -0800363 priv->_3945.shared_virt,
364 priv->_3945.shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700365}
366
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800367static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200368 struct ieee80211_tx_info *info,
Johannes Bergc2acea82009-07-24 11:13:05 -0700369 struct iwl_device_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700370 struct sk_buff *skb_frag,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800371 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700372{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700373 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Tomas Winklerc587de02009-06-03 11:44:07 -0700374 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -0700375
Johannes Berg97359d12010-08-10 09:46:38 +0200376 tx_cmd->sec_ctl = 0;
377
378 switch (keyinfo->cipher) {
379 case WLAN_CIPHER_SUITE_CCMP:
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700380 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
381 memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
Tomas Winklere1623442009-01-27 14:27:56 -0800382 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -0700383 break;
384
Johannes Berg97359d12010-08-10 09:46:38 +0200385 case WLAN_CIPHER_SUITE_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -0700386 break;
387
Johannes Berg97359d12010-08-10 09:46:38 +0200388 case WLAN_CIPHER_SUITE_WEP104:
389 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
390 /* fall through */
391 case WLAN_CIPHER_SUITE_WEP40:
392 tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +0200393 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -0700394
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700395 memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -0700396
Tomas Winklere1623442009-01-27 14:27:56 -0800397 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +0200398 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -0700399 break;
400
Zhu Yib481de92007-09-25 17:54:57 -0700401 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200402 IWL_ERR(priv, "Unknown encode cipher %x\n", keyinfo->cipher);
Zhu Yib481de92007-09-25 17:54:57 -0700403 break;
404 }
405}
406
407/*
408 * handle build REPLY_TX command notification.
409 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800410static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Johannes Bergc2acea82009-07-24 11:13:05 -0700411 struct iwl_device_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200412 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +0800413 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -0700414{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700415 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
416 __le32 tx_flags = tx_cmd->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700417 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700418
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700419 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200420 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700421 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700422 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700423 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700424 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -0700425 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
426 tx_flags |= TX_CMD_FLG_TSF_MSK;
427 } else {
428 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
429 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
430 }
431
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700432 tx_cmd->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700433 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700434 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
435
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700436 if (ieee80211_is_data_qos(fc)) {
437 u8 *qc = ieee80211_get_qos_ctl(hdr);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700438 tx_cmd->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -0700439 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800440 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700441 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800442 }
Zhu Yib481de92007-09-25 17:54:57 -0700443
Johannes Berg94597ab2010-08-09 10:57:02 -0700444 priv->cfg->ops->utils->tx_cmd_protection(priv, info, fc, &tx_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700445
446 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700447 if (ieee80211_is_mgmt(fc)) {
448 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700449 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -0700450 else
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700451 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700452 } else {
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700453 tx_cmd->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700454 }
Zhu Yib481de92007-09-25 17:54:57 -0700455
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700456 tx_cmd->driver_txop = 0;
457 tx_cmd->tx_flags = tx_flags;
458 tx_cmd->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700459}
460
Zhu Yib481de92007-09-25 17:54:57 -0700461/*
462 * start REPLY_TX command process
463 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800464static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700465{
466 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200467 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700468 struct iwl3945_tx_cmd *tx_cmd;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800469 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800470 struct iwl_queue *q = NULL;
Johannes Bergc2acea82009-07-24 11:13:05 -0700471 struct iwl_device_cmd *out_cmd;
472 struct iwl_cmd_meta *out_meta;
Zhu Yib481de92007-09-25 17:54:57 -0700473 dma_addr_t phys_addr;
474 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800475 int txq_id = skb_get_queue_mapping(skb);
Reinette Chatredf833b12009-04-21 10:55:48 -0700476 u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
Tomas Winkler54dbb522008-05-15 13:54:06 +0800477 u8 id;
478 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700479 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800480 u8 tid = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700481 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700482 u8 wait_write_ptr = 0;
483 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700484
485 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800486 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800487 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700488 goto drop_unlock;
489 }
490
Johannes Berge039fa42008-05-15 12:55:29 +0200491 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800492 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700493 goto drop_unlock;
494 }
495
496 unicast = !is_multicast_ether_addr(hdr->addr1);
497 id = 0;
498
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700499 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700500
Samuel Ortizd08853a2009-01-23 13:45:17 -0800501#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700502 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800503 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700504 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800505 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700506 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800507 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700508#endif
509
Zhu Yib481de92007-09-25 17:54:57 -0700510 spin_unlock_irqrestore(&priv->lock, flags);
511
Harvey Harrison7294ec92008-07-15 18:43:59 -0700512 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800513
Johannes Berg2a87c262010-04-30 11:30:45 -0700514 /* Find index into station table for destination station */
Johannes Berg0af8bca2010-04-30 14:08:00 -0700515 sta_id = iwl_sta_id_or_broadcast(priv, info->control.sta);
Zhu Yib481de92007-09-25 17:54:57 -0700516 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -0800517 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -0700518 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700519 goto drop;
520 }
521
Tomas Winklere1623442009-01-27 14:27:56 -0800522 IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -0700523
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700524 if (ieee80211_is_data_qos(fc)) {
Johannes Bergf862a232010-05-03 01:20:52 -0700525 u8 *qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -0700526 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Reinette Chatree6a6cf42009-08-13 13:30:50 -0700527 if (unlikely(tid >= MAX_TID_COUNT))
528 goto drop;
Zhu Yib481de92007-09-25 17:54:57 -0700529 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800530
531 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800532 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -0700533 q = &txq->q;
534
Zhu Yidc57a302009-12-14 14:12:12 -0800535 if ((iwl_queue_space(q) < q->high_mark))
536 goto drop;
537
Zhu Yib481de92007-09-25 17:54:57 -0700538 spin_lock_irqsave(&priv->lock, flags);
539
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800540 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700541
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800542 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800543 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Johannes Bergff0d91c2010-05-17 02:37:34 -0700544 txq->txb[q->write_ptr].skb = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800545
546 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800547 out_cmd = txq->cmd[idx];
Johannes Bergc2acea82009-07-24 11:13:05 -0700548 out_meta = &txq->meta[idx];
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700549 tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -0700550 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700551 memset(tx_cmd, 0, sizeof(*tx_cmd));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800552
553 /*
554 * Set up the Tx-command (not MAC!) header.
555 * Store the chosen Tx queue and TFD index within the sequence field;
556 * after Tx, uCode's Tx response will return this value so driver can
557 * locate the frame within the tx queue and do post-tx processing.
558 */
Zhu Yib481de92007-09-25 17:54:57 -0700559 out_cmd->hdr.cmd = REPLY_TX;
560 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800561 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800562
563 /* Copy MAC header from skb into command buffer */
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700564 memcpy(tx_cmd->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -0700565
Reinette Chatredf833b12009-04-21 10:55:48 -0700566
567 if (info->control.hw_key)
568 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
569
570 /* TODO need this for burst mode later on */
571 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
572
573 /* set is_hcca to 0; it probably will never be implemented */
574 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
575
576 /* Total # bytes to be transmitted */
577 len = (u16)skb->len;
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700578 tx_cmd->len = cpu_to_le16(len);
Reinette Chatredf833b12009-04-21 10:55:48 -0700579
Wey-Yi Guy20594eb2009-08-07 15:41:39 -0700580 iwl_dbg_log_tx_data_frame(priv, len, hdr);
Wey-Yi Guy22fdf3c2009-08-07 15:41:40 -0700581 iwl_update_stats(priv, true, fc, len);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700582 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
583 tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Reinette Chatredf833b12009-04-21 10:55:48 -0700584
585 if (!ieee80211_has_morefrags(hdr->frame_control)) {
586 txq->need_update = 1;
Reinette Chatredf833b12009-04-21 10:55:48 -0700587 } else {
588 wait_write_ptr = 1;
589 txq->need_update = 0;
590 }
591
Frans Pop91dd6c22010-03-24 14:19:58 -0700592 IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
Reinette Chatredf833b12009-04-21 10:55:48 -0700593 le16_to_cpu(out_cmd->hdr.sequence));
Frans Pop91dd6c22010-03-24 14:19:58 -0700594 IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700595 iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
596 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
Reinette Chatredf833b12009-04-21 10:55:48 -0700597 ieee80211_hdrlen(fc));
598
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800599 /*
600 * Use the first empty entry in this queue's command buffer array
601 * to contain the Tx command and MAC header concatenated together
602 * (payload data will be in another buffer).
603 * Size of this varies, due to varying MAC header length.
604 * If end is not dword aligned, we'll have 2 extra bytes at the end
605 * of the MAC header (device reads on dword boundaries).
606 * We'll tell device about this padding later.
607 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800608 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +0800609 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -0700610
611 len_org = len;
612 len = (len + 3) & ~3;
613
614 if (len_org != len)
615 len_org = 1;
616 else
617 len_org = 0;
618
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800619 /* Physical address of this Tx command's header (not MAC header!),
620 * within command buffer array. */
Reinette Chatredf833b12009-04-21 10:55:48 -0700621 txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
622 len, PCI_DMA_TODEVICE);
623 /* we do not map meta data ... so we can safely access address to
624 * provide to unmap command*/
FUJITA Tomonori2e724442010-06-03 14:19:20 +0900625 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
626 dma_unmap_len_set(out_meta, len, len);
Zhu Yib481de92007-09-25 17:54:57 -0700627
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800628 /* Add buffer containing Tx command and MAC(!) header to TFD's
629 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800630 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
631 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700632
Zhu Yib481de92007-09-25 17:54:57 -0700633
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800634 /* Set up TFD's 2nd entry to point directly to remainder of skb,
635 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -0700636 len = skb->len - hdr_len;
637 if (len) {
638 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
639 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800640 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
641 phys_addr, len,
642 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -0700643 }
644
Zhu Yib481de92007-09-25 17:54:57 -0700645
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800646 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800647 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800648 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700649 spin_unlock_irqrestore(&priv->lock, flags);
650
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800651 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -0700652 && priv->mac80211_registered) {
653 if (wait_write_ptr) {
654 spin_lock_irqsave(&priv->lock, flags);
655 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -0800656 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700657 spin_unlock_irqrestore(&priv->lock, flags);
658 }
659
Johannes Berge4e72fb2009-03-23 17:28:42 +0100660 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -0700661 }
662
663 return 0;
664
665drop_unlock:
666 spin_unlock_irqrestore(&priv->lock, flags);
667drop:
668 return -1;
669}
670
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800671static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700672 struct ieee80211_measurement_params *params,
673 u8 type)
674{
Tomas Winkler600c0e12008-12-19 10:37:04 +0800675 struct iwl_spectrum_cmd spectrum;
Zhu Yi2f301222009-10-09 17:19:45 +0800676 struct iwl_rx_packet *pkt;
Winkler, Tomasc2d79b482008-12-19 10:37:34 +0800677 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700678 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
679 .data = (void *)&spectrum,
Johannes Bergc2acea82009-07-24 11:13:05 -0700680 .flags = CMD_WANT_SKB,
Zhu Yib481de92007-09-25 17:54:57 -0700681 };
682 u32 add_time = le64_to_cpu(params->start_time);
683 int rc;
684 int spectrum_resp_status;
685 int duration = le16_to_cpu(params->duration);
686
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800687 if (iwl_is_associated(priv))
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700688 add_time = iwl_usecs_to_beacons(priv,
Johannes Berge99f168c2010-01-19 10:04:28 -0800689 le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
Zhu Yib481de92007-09-25 17:54:57 -0700690 le16_to_cpu(priv->rxon_timing.beacon_interval));
691
692 memset(&spectrum, 0, sizeof(spectrum));
693
694 spectrum.channel_count = cpu_to_le16(1);
695 spectrum.flags =
696 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
697 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
698 cmd.len = sizeof(spectrum);
699 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
700
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800701 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700702 spectrum.start_time =
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700703 iwl_add_beacon_time(priv,
704 priv->_3945.last_beacon_time, add_time,
Zhu Yib481de92007-09-25 17:54:57 -0700705 le16_to_cpu(priv->rxon_timing.beacon_interval));
706 else
707 spectrum.start_time = 0;
708
709 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
710 spectrum.channels[0].channel = params->channel;
711 spectrum.channels[0].type = type;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800712 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700713 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
714 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
715
Samuel Ortiz518099a2009-01-19 15:30:27 -0800716 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700717 if (rc)
718 return rc;
719
Zhu Yi2f301222009-10-09 17:19:45 +0800720 pkt = (struct iwl_rx_packet *)cmd.reply_page;
721 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800722 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700723 rc = -EIO;
724 }
725
Zhu Yi2f301222009-10-09 17:19:45 +0800726 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
Zhu Yib481de92007-09-25 17:54:57 -0700727 switch (spectrum_resp_status) {
728 case 0: /* Command will be handled */
Zhu Yi2f301222009-10-09 17:19:45 +0800729 if (pkt->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800730 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800731 pkt->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700732 priv->measurement_status &= ~MEASUREMENT_READY;
733 }
734 priv->measurement_status |= MEASUREMENT_ACTIVE;
735 rc = 0;
736 break;
737
738 case 1: /* Command will not be handled */
739 rc = -EAGAIN;
740 break;
741 }
742
Zhu Yi64a76b52009-12-10 14:37:21 -0800743 iwl_free_pages(priv, cmd.reply_page);
Zhu Yib481de92007-09-25 17:54:57 -0700744
745 return rc;
746}
Zhu Yib481de92007-09-25 17:54:57 -0700747
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800748static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800749 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700750{
Zhu Yi2f301222009-10-09 17:19:45 +0800751 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800752 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700753 struct delayed_work *pwork;
754
755 palive = &pkt->u.alive_frame;
756
Tomas Winklere1623442009-01-27 14:27:56 -0800757 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700758 "0x%01X 0x%01X\n",
759 palive->is_valid, palive->ver_type,
760 palive->ver_subtype);
761
762 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800763 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800764 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
765 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700766 pwork = &priv->init_alive_start;
767 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800768 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700769 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800770 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700771 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800772 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700773 }
774
775 /* We delay the ALIVE response by 5ms to
776 * give the HW RF Kill time to activate... */
777 if (palive->is_valid == UCODE_VALID_OK)
778 queue_delayed_work(priv->workqueue, pwork,
779 msecs_to_jiffies(5));
780 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800781 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700782}
783
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800784static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800785 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700786{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100787#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800788 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Helmut Schaac7e035a2009-01-19 13:02:15 +0100789#endif
Zhu Yib481de92007-09-25 17:54:57 -0700790
Tomas Winklere1623442009-01-27 14:27:56 -0800791 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700792}
793
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800794static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700795{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800796 struct iwl_priv *priv =
797 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700798 struct sk_buff *beacon;
799
800 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200801 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700802
803 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800804 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700805 return;
806 }
807
808 mutex_lock(&priv->mutex);
809 /* new beacon skb is allocated every time; dispose previous.*/
810 if (priv->ibss_beacon)
811 dev_kfree_skb(priv->ibss_beacon);
812
813 priv->ibss_beacon = beacon;
814 mutex_unlock(&priv->mutex);
815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800816 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700817}
818
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800819static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800820 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700821{
Zhu Yi2f301222009-10-09 17:19:45 +0800822 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Johannes Berga85d7cc2010-07-31 08:34:10 -0700824#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700825 u8 rate = beacon->beacon_notify_hdr.rate;
826
Tomas Winklere1623442009-01-27 14:27:56 -0800827 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700828 "tsf %d %d rate %d\n",
829 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
830 beacon->beacon_notify_hdr.failure_frame,
831 le32_to_cpu(beacon->ibss_mgr_status),
832 le32_to_cpu(beacon->high_tsf),
833 le32_to_cpu(beacon->low_tsf), rate);
834#endif
835
Johannes Berga85d7cc2010-07-31 08:34:10 -0700836 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
837
Johannes Berg05c914f2008-09-11 00:01:58 +0200838 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700839 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
840 queue_work(priv->workqueue, &priv->beacon_update);
841}
842
Zhu Yib481de92007-09-25 17:54:57 -0700843/* Handle notification from uCode that card's power state is changing
844 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800845static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800846 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700847{
Zhu Yi2f301222009-10-09 17:19:45 +0800848 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700849 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
850 unsigned long status = priv->status;
851
Reinette Chatre4c423a22009-07-17 09:30:26 -0700852 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700853 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
854 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
855
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800856 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700857 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
858
859 if (flags & HW_CARD_DISABLED)
860 set_bit(STATUS_RF_KILL_HW, &priv->status);
861 else
862 clear_bit(STATUS_RF_KILL_HW, &priv->status);
863
864
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800865 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700866
867 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200868 test_bit(STATUS_RF_KILL_HW, &priv->status)))
869 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
870 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700871 else
872 wake_up_interruptible(&priv->wait_command_queue);
873}
874
875/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800876 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700877 *
878 * Setup the RX handlers for each of the reply types sent from the uCode
879 * to the host.
880 *
881 * This function chains into the hardware specific files for them to setup
882 * any hardware specific handlers as well.
883 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800884static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700885{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800886 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
887 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800888 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800889 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Reinette Chatre81963d62010-01-22 14:22:57 -0800890 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
891 iwl_rx_spectrum_measure_notif;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800892 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700893 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800894 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800895 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700896
Ben Cahill9fbab512007-11-29 11:09:47 +0800897 /*
898 * The same handler is used for both the REPLY to a discrete
899 * statistics request from the host as well as for the periodic
900 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700901 */
Abhijeet Kolekar17f36fc2010-04-16 10:03:54 -0700902 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800903 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700904
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800905 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800906 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700907
Ben Cahill9fbab512007-11-29 11:09:47 +0800908 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800909 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700910}
911
Zhu Yib481de92007-09-25 17:54:57 -0700912/************************** RX-FUNCTIONS ****************************/
913/*
914 * Rx theory of operation
915 *
916 * The host allocates 32 DMA target addresses and passes the host address
917 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
918 * 0 to 31
919 *
920 * Rx Queue Indexes
921 * The host/firmware share two index registers for managing the Rx buffers.
922 *
923 * The READ index maps to the first position that the firmware may be writing
924 * to -- the driver can read up to (but not including) this position and get
925 * good data.
926 * The READ index is managed by the firmware once the card is enabled.
927 *
928 * The WRITE index maps to the last position the driver has read from -- the
929 * position preceding WRITE is the last slot the firmware can place a packet.
930 *
931 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
932 * WRITE = READ.
933 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800934 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -0700935 * INDEX position, and WRITE to the last (READ - 1 wrapped)
936 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800937 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -0700938 * and fire the RX interrupt. The driver can then query the READ index and
939 * process as many packets as possible, moving the WRITE index forward as it
940 * resets the Rx queue buffers with new memory.
941 *
942 * The management in the driver is as follows:
943 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
944 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +0800945 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800946 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -0700947 * iwl->rxq is replenished and the READ INDEX is updated (updating the
948 * 'processed' and 'read' driver indexes as well)
949 * + A received packet is processed and handed to the kernel network stack,
950 * detached from the iwl->rxq. The driver 'processed' index is updated.
951 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
952 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
953 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
954 * were enough free buffers and RX_STALLED is set it is cleared.
955 *
956 *
957 * Driver sequence:
958 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800959 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800960 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +0800961 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -0700962 * queue, updates firmware pointers, and updates
963 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800964 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -0700965 *
966 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800967 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -0700968 * READ INDEX, detaching the SKB from the pool.
969 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800970 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -0700971 * slots.
972 * ...
973 *
974 */
975
976/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800977 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -0700978 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800979static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700980 dma_addr_t dma_addr)
981{
982 return cpu_to_le32((u32)dma_addr);
983}
984
985/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800986 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -0700987 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800988 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -0700989 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +0800990 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -0700991 *
992 * This moves the 'write' index forward to catch up with 'processed', and
993 * also updates the memory address in the firmware to reference the new
994 * target buffer.
995 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800996static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700997{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +0800998 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -0700999 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001000 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001001 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001002 int write;
Zhu Yib481de92007-09-25 17:54:57 -07001003
1004 spin_lock_irqsave(&rxq->lock, flags);
1005 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001006 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001007 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001008 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001009 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001010 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001011
1012 /* Point to Rx buffer via next RBD in circular buffer */
Zhu Yi2f301222009-10-09 17:19:45 +08001013 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
Zhu Yib481de92007-09-25 17:54:57 -07001014 rxq->queue[rxq->write] = rxb;
1015 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1016 rxq->free_count--;
1017 }
1018 spin_unlock_irqrestore(&rxq->lock, flags);
1019 /* If the pre-allocated buffer pool is dropping low, schedule to
1020 * refill it */
1021 if (rxq->free_count <= RX_LOW_WATERMARK)
1022 queue_work(priv->workqueue, &priv->rx_replenish);
1023
1024
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001025 /* If we've added more space for the firmware to place data, tell it.
1026 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001027 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001028 || (abs(rxq->write - rxq->read) > 7)) {
1029 spin_lock_irqsave(&rxq->lock, flags);
1030 rxq->need_update = 1;
1031 spin_unlock_irqrestore(&rxq->lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001032 iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001033 }
Zhu Yib481de92007-09-25 17:54:57 -07001034}
1035
1036/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001037 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001038 *
1039 * When moving to rx_free an SKB is allocated for the slot.
1040 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001041 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001042 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001043 */
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001044static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001045{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001046 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001047 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001048 struct iwl_rx_mem_buffer *rxb;
Zhu Yi2f301222009-10-09 17:19:45 +08001049 struct page *page;
Zhu Yib481de92007-09-25 17:54:57 -07001050 unsigned long flags;
Zhu Yi29b1b262009-10-23 13:42:25 -07001051 gfp_t gfp_mask = priority;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001052
1053 while (1) {
1054 spin_lock_irqsave(&rxq->lock, flags);
1055
1056 if (list_empty(&rxq->rx_used)) {
1057 spin_unlock_irqrestore(&rxq->lock, flags);
1058 return;
1059 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001060 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001061
Reinette Chatref82a9242009-09-17 10:43:56 -07001062 if (rxq->free_count > RX_LOW_WATERMARK)
Zhu Yi29b1b262009-10-23 13:42:25 -07001063 gfp_mask |= __GFP_NOWARN;
Zhu Yi2f301222009-10-09 17:19:45 +08001064
1065 if (priv->hw_params.rx_page_order > 0)
Zhu Yi29b1b262009-10-23 13:42:25 -07001066 gfp_mask |= __GFP_COMP;
Zhu Yi2f301222009-10-09 17:19:45 +08001067
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001068 /* Alloc a new receive buffer */
Zhu Yi29b1b262009-10-23 13:42:25 -07001069 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
Zhu Yi2f301222009-10-09 17:19:45 +08001070 if (!page) {
Zhu Yib481de92007-09-25 17:54:57 -07001071 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001072 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1073 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1074 net_ratelimit())
1075 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1076 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1077 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001078 /* We don't reschedule replenish work here -- we will
1079 * call the restock method and if it still needs
1080 * more buffers it will schedule replenish */
1081 break;
1082 }
Zhu Yi12342c42007-12-20 11:27:32 +08001083
Reinette Chatrede0bd502009-09-11 10:38:12 -07001084 spin_lock_irqsave(&rxq->lock, flags);
1085 if (list_empty(&rxq->rx_used)) {
1086 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001087 __free_pages(page, priv->hw_params.rx_page_order);
Reinette Chatrede0bd502009-09-11 10:38:12 -07001088 return;
1089 }
1090 element = rxq->rx_used.next;
1091 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1092 list_del(element);
1093 spin_unlock_irqrestore(&rxq->lock, flags);
1094
Zhu Yi2f301222009-10-09 17:19:45 +08001095 rxb->page = page;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001096 /* Get physical address of RB/SKB */
Zhu Yi2f301222009-10-09 17:19:45 +08001097 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1098 PAGE_SIZE << priv->hw_params.rx_page_order,
1099 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001100
1101 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001102
Zhu Yib481de92007-09-25 17:54:57 -07001103 list_add_tail(&rxb->list, &rxq->rx_free);
1104 rxq->free_count++;
Zhu Yi2f301222009-10-09 17:19:45 +08001105 priv->alloc_rxb_page++;
1106
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001107 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001108 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001109}
1110
Reinette Chatredf833b12009-04-21 10:55:48 -07001111void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1112{
1113 unsigned long flags;
1114 int i;
1115 spin_lock_irqsave(&rxq->lock, flags);
1116 INIT_LIST_HEAD(&rxq->rx_free);
1117 INIT_LIST_HEAD(&rxq->rx_used);
1118 /* Fill the rx_used queue with _all_ of the Rx buffers */
1119 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1120 /* In the reset function, these buffers may have been allocated
1121 * to an SKB, so we need to unmap and free potential storage */
Zhu Yi2f301222009-10-09 17:19:45 +08001122 if (rxq->pool[i].page != NULL) {
1123 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1124 PAGE_SIZE << priv->hw_params.rx_page_order,
1125 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001126 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001127 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001128 }
1129 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1130 }
1131
1132 /* Set us so that we have processed and used all buffers, but have
1133 * not restocked the Rx queue with fresh buffers */
1134 rxq->read = rxq->write = 0;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001135 rxq->write_actual = 0;
Zhu Yi2f301222009-10-09 17:19:45 +08001136 rxq->free_count = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001137 spin_unlock_irqrestore(&rxq->lock, flags);
1138}
Reinette Chatredf833b12009-04-21 10:55:48 -07001139
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001140void iwl3945_rx_replenish(void *data)
1141{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001142 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001143 unsigned long flags;
1144
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001145 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001146
1147 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001148 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001149 spin_unlock_irqrestore(&priv->lock, flags);
1150}
1151
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001152static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1153{
1154 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1155
1156 iwl3945_rx_queue_restock(priv);
1157}
1158
1159
Reinette Chatredf833b12009-04-21 10:55:48 -07001160/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1161 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1162 * This free routine walks the list of POOL entries and if SKB is set to
1163 * non NULL it is unmapped and freed
1164 */
1165static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1166{
1167 int i;
1168 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
Zhu Yi2f301222009-10-09 17:19:45 +08001169 if (rxq->pool[i].page != NULL) {
1170 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1171 PAGE_SIZE << priv->hw_params.rx_page_order,
1172 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001173 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001174 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001175 }
1176 }
1177
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001178 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
Emmanuel Grumbachd5b25c92010-06-07 13:21:46 -07001179 rxq->bd_dma);
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001180 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1181 rxq->rb_stts, rxq->rb_stts_dma);
Reinette Chatredf833b12009-04-21 10:55:48 -07001182 rxq->bd = NULL;
1183 rxq->rb_stts = NULL;
1184}
Reinette Chatredf833b12009-04-21 10:55:48 -07001185
1186
Zhu Yib481de92007-09-25 17:54:57 -07001187/* Convert linear signal-to-noise ratio into dB */
1188static u8 ratio2dB[100] = {
1189/* 0 1 2 3 4 5 6 7 8 9 */
1190 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1191 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1192 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1193 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1194 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1195 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1196 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1197 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1198 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1199 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1200};
1201
1202/* Calculates a relative dB value from a ratio of linear
1203 * (i.e. not dB) signal levels.
1204 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001205int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001206{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001207 /* 1000:1 or higher just report as 60 dB */
1208 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001209 return 60;
1210
Adrian Bunk221c80c2008-02-02 23:19:01 +02001211 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001212 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001213 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001214 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001215
1216 /* We shouldn't see this */
1217 if (sig_ratio < 1)
1218 return 0;
1219
1220 /* Use table for ratios 1:1 - 99:1 */
1221 return (int)ratio2dB[sig_ratio];
1222}
1223
Zhu Yib481de92007-09-25 17:54:57 -07001224/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001225 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001226 *
1227 * Uses the priv->rx_handlers callback function array to invoke
1228 * the appropriate handlers, including command responses,
1229 * frame-received notifications, and other notifications.
1230 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001231static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001232{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001233 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001234 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001235 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001236 u32 r, i;
1237 int reclaim;
1238 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001239 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001240 u32 count = 8;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001241 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001242
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001243 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1244 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001245 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001246 i = rxq->read;
1247
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001248 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -07001249 total_empty = r - rxq->write_actual;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001250 if (total_empty < 0)
1251 total_empty += RX_QUEUE_SIZE;
1252
1253 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001254 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001255 /* Rx interrupt, but nothing sent from uCode */
1256 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001257 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001258
1259 while (i != r) {
Johannes Bergf4989d92010-05-28 04:08:30 -07001260 int len;
1261
Zhu Yib481de92007-09-25 17:54:57 -07001262 rxb = rxq->queue[i];
1263
Ben Cahill9fbab512007-11-29 11:09:47 +08001264 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001265 * then a bug has been introduced in the queue refilling
1266 * routines -- catch it here */
1267 BUG_ON(rxb == NULL);
1268
1269 rxq->queue[i] = NULL;
1270
Zhu Yi2f301222009-10-09 17:19:45 +08001271 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1272 PAGE_SIZE << priv->hw_params.rx_page_order,
1273 PCI_DMA_FROMDEVICE);
1274 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001275
Johannes Bergf4989d92010-05-28 04:08:30 -07001276 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1277 len += sizeof(u32); /* account for status word */
1278 trace_iwlwifi_dev_rx(priv, pkt, len);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001279
Zhu Yib481de92007-09-25 17:54:57 -07001280 /* Reclaim a command buffer only if this packet is a response
1281 * to a (driver-originated) command.
1282 * If the packet (e.g. Rx frame) originated from uCode,
1283 * there is no command buffer to reclaim.
1284 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1285 * but apparently a few don't get set; catch them here. */
1286 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1287 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1288 (pkt->hdr.cmd != REPLY_TX);
1289
1290 /* Based on type of command response or notification,
1291 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001292 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001293 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001294 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001295 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001296 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -07001297 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001298 } else {
1299 /* No handling needed */
Zhu Yi2f301222009-10-09 17:19:45 +08001300 IWL_DEBUG_RX(priv,
1301 "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001302 r, i, get_cmd_string(pkt->hdr.cmd),
1303 pkt->hdr.cmd);
1304 }
1305
Zhu Yi29b1b262009-10-23 13:42:25 -07001306 /*
1307 * XXX: After here, we should always check rxb->page
1308 * against NULL before touching it or its virtual
1309 * memory (pkt). Because some rx_handler might have
1310 * already taken or freed the pages.
1311 */
1312
Zhu Yib481de92007-09-25 17:54:57 -07001313 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001314 /* Invoke any callbacks, transfer the buffer to caller,
1315 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001316 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001317 if (rxb->page)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001318 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001319 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001320 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001321 }
1322
Zhu Yi73005152009-10-23 13:42:32 -07001323 /* Reuse the page if possible. For notification packets and
1324 * SKBs that fail to Rx correctly, add them back into the
1325 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001326 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001327 if (rxb->page != NULL) {
1328 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1329 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1330 PCI_DMA_FROMDEVICE);
1331 list_add_tail(&rxb->list, &rxq->rx_free);
1332 rxq->free_count++;
1333 } else
1334 list_add_tail(&rxb->list, &rxq->rx_used);
1335
Zhu Yib481de92007-09-25 17:54:57 -07001336 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001337
Zhu Yib481de92007-09-25 17:54:57 -07001338 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001339 /* If there are a lot of unused frames,
1340 * restock the Rx queue so ucode won't assert. */
1341 if (fill_rx) {
1342 count++;
1343 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001344 rxq->read = i;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001345 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001346 count = 0;
1347 }
1348 }
Zhu Yib481de92007-09-25 17:54:57 -07001349 }
1350
1351 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001352 rxq->read = i;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001353 if (fill_rx)
1354 iwl3945_rx_replenish_now(priv);
1355 else
1356 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001357}
1358
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001359/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001360static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001361{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001362 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001363 synchronize_irq(priv->pci_dev->irq);
1364 tasklet_kill(&priv->irq_tasklet);
1365}
1366
Zhu Yib481de92007-09-25 17:54:57 -07001367static const char *desc_lookup(int i)
1368{
1369 switch (i) {
1370 case 1:
1371 return "FAIL";
1372 case 2:
1373 return "BAD_PARAM";
1374 case 3:
1375 return "BAD_CHECKSUM";
1376 case 4:
1377 return "NMI_INTERRUPT";
1378 case 5:
1379 return "SYSASSERT";
1380 case 6:
1381 return "FATAL_ERROR";
1382 }
1383
1384 return "UNKNOWN";
1385}
1386
1387#define ERROR_START_OFFSET (1 * sizeof(u32))
1388#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1389
Reinette Chatreb7a79402009-09-25 14:24:23 -07001390void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001391{
1392 u32 i;
1393 u32 desc, time, count, base, data1;
1394 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001395
1396 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1397
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001398 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001399 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001400 return;
1401 }
1402
Zhu Yib481de92007-09-25 17:54:57 -07001403
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001404 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001405
1406 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001407 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1408 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1409 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001410 }
1411
Winkler, Tomas15b16872008-12-19 10:37:33 +08001412 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001413 "ilink1 nmiPC Line\n");
1414 for (i = ERROR_START_OFFSET;
1415 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1416 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001417 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001418 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001419 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001420 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001421 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001422 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001423 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001424 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001425 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001426 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001427 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001428 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001429 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001430
Winkler, Tomas15b16872008-12-19 10:37:33 +08001431 IWL_ERR(priv,
Jay Sternberg87563712010-06-14 14:40:40 -07001432 "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +08001433 desc_lookup(desc), desc, time, blink1, blink2,
1434 ilink1, ilink2, data1);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001435 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1436 0, blink1, blink2, ilink1, ilink2);
Zhu Yib481de92007-09-25 17:54:57 -07001437 }
Zhu Yib481de92007-09-25 17:54:57 -07001438}
1439
Ben Cahillf58177b2007-11-29 11:09:43 +08001440#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001441
1442/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001443 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001444 *
Zhu Yib481de92007-09-25 17:54:57 -07001445 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001446static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1447 u32 num_events, u32 mode,
1448 int pos, char **buf, size_t bufsz)
Zhu Yib481de92007-09-25 17:54:57 -07001449{
1450 u32 i;
1451 u32 base; /* SRAM byte address of event log header */
1452 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1453 u32 ptr; /* SRAM byte address of log data */
1454 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001455 unsigned long reg_flags;
Zhu Yib481de92007-09-25 17:54:57 -07001456
1457 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001458 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001459
1460 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1461
1462 if (mode == 0)
1463 event_size = 2 * sizeof(u32);
1464 else
1465 event_size = 3 * sizeof(u32);
1466
1467 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1468
Ben Cahille5854472009-11-06 14:52:58 -08001469 /* Make sure device is powered up for SRAM reads */
1470 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1471 iwl_grab_nic_access(priv);
1472
1473 /* Set starting address; reads will auto-increment */
1474 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1475 rmb();
1476
Zhu Yib481de92007-09-25 17:54:57 -07001477 /* "time" is actually "data" for mode 0 (no timestamp).
1478 * place event id # at far right for easier visual parsing. */
1479 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001480 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1481 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001482 if (mode == 0) {
1483 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001484 if (bufsz) {
1485 pos += scnprintf(*buf + pos, bufsz - pos,
1486 "0x%08x:%04u\n",
1487 time, ev);
1488 } else {
1489 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1490 trace_iwlwifi_dev_ucode_event(priv, 0,
1491 time, ev);
1492 }
Winkler, Tomas15b16872008-12-19 10:37:33 +08001493 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001494 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001495 if (bufsz) {
1496 pos += scnprintf(*buf + pos, bufsz - pos,
1497 "%010u:0x%08x:%04u\n",
1498 time, data, ev);
1499 } else {
1500 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1501 time, data, ev);
1502 trace_iwlwifi_dev_ucode_event(priv, time,
1503 data, ev);
1504 }
Zhu Yib481de92007-09-25 17:54:57 -07001505 }
1506 }
Ben Cahille5854472009-11-06 14:52:58 -08001507
1508 /* Allow device to power down */
1509 iwl_release_nic_access(priv);
1510 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001511 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001512}
1513
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001514/**
1515 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1516 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001517static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001518 u32 num_wraps, u32 next_entry,
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001519 u32 size, u32 mode,
1520 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001521{
1522 /*
1523 * display the newest DEFAULT_LOG_ENTRIES entries
1524 * i.e the entries just before the next ont that uCode would fill.
1525 */
1526 if (num_wraps) {
1527 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001528 pos = iwl3945_print_event_log(priv,
1529 capacity - (size - next_entry),
1530 size - next_entry, mode,
1531 pos, buf, bufsz);
1532 pos = iwl3945_print_event_log(priv, 0,
1533 next_entry, mode,
1534 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001535 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001536 pos = iwl3945_print_event_log(priv, next_entry - size,
1537 size, mode,
1538 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001539 } else {
1540 if (next_entry < size)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001541 pos = iwl3945_print_event_log(priv, 0,
1542 next_entry, mode,
1543 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001544 else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001545 pos = iwl3945_print_event_log(priv, next_entry - size,
1546 size, mode,
1547 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001548 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001549 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001550}
1551
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001552#define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1553
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001554int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1555 char **buf, bool display)
Zhu Yib481de92007-09-25 17:54:57 -07001556{
Zhu Yib481de92007-09-25 17:54:57 -07001557 u32 base; /* SRAM byte address of event log header */
1558 u32 capacity; /* event log capacity in # entries */
1559 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1560 u32 num_wraps; /* # times uCode wrapped to top of log */
1561 u32 next_entry; /* index of next entry to be written by uCode */
1562 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001563 int pos = 0;
1564 size_t bufsz = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001565
1566 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001567 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001568 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001569 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001570 }
1571
Zhu Yib481de92007-09-25 17:54:57 -07001572 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001573 capacity = iwl_read_targ_mem(priv, base);
1574 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1575 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1576 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001577
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001578 if (capacity > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001579 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001580 capacity, priv->cfg->max_event_log_size);
1581 capacity = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001582 }
1583
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001584 if (next_entry > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001585 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001586 next_entry, priv->cfg->max_event_log_size);
1587 next_entry = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001588 }
1589
Zhu Yib481de92007-09-25 17:54:57 -07001590 size = num_wraps ? capacity : next_entry;
1591
1592 /* bail out if nothing in log */
1593 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001594 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001595 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001596 }
1597
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001598#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001599 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001600 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1601 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1602#else
1603 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1604 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1605#endif
1606
1607 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1608 size);
Zhu Yib481de92007-09-25 17:54:57 -07001609
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001610#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001611 if (display) {
1612 if (full_log)
1613 bufsz = capacity * 48;
1614 else
1615 bufsz = size * 48;
1616 *buf = kmalloc(bufsz, GFP_KERNEL);
1617 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001618 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001619 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001620 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1621 /* if uCode has wrapped back to top of log,
1622 * start at the oldest entry,
1623 * i.e the next one that uCode would fill.
1624 */
1625 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001626 pos = iwl3945_print_event_log(priv, next_entry,
1627 capacity - next_entry, mode,
1628 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001629
1630 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001631 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1632 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001633 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001634 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1635 next_entry, size, mode,
1636 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001637#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001638 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1639 next_entry, size, mode,
1640 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001641#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001642 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001643}
1644
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001645static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001646{
1647 u32 inta, handled = 0;
1648 u32 inta_fh;
1649 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001650#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001651 u32 inta_mask;
1652#endif
1653
1654 spin_lock_irqsave(&priv->lock, flags);
1655
1656 /* Ack/clear/reset pending uCode interrupts.
1657 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1658 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001659 inta = iwl_read32(priv, CSR_INT);
1660 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001661
1662 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1663 * Any new interrupts that happen after this, either while we're
1664 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001665 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1666 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001667
Samuel Ortizd08853a2009-01-23 13:45:17 -08001668#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001669 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001670 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001671 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001672 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001673 inta, inta_mask, inta_fh);
1674 }
1675#endif
1676
Zhu Yi2f301222009-10-09 17:19:45 +08001677 spin_unlock_irqrestore(&priv->lock, flags);
1678
Zhu Yib481de92007-09-25 17:54:57 -07001679 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1680 * atomic, make sure that inta covers all the interrupts that
1681 * we've discovered, even if FH interrupt came in just after
1682 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001683 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001684 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001685 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001686 inta |= CSR_INT_BIT_FH_TX;
1687
1688 /* Now service all interrupt bits discovered above. */
1689 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001690 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001691
1692 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001693 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001694
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001695 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001696 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001697
1698 handled |= CSR_INT_BIT_HW_ERR;
1699
Zhu Yib481de92007-09-25 17:54:57 -07001700 return;
1701 }
1702
Samuel Ortizd08853a2009-01-23 13:45:17 -08001703#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001704 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001705 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001706 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001707 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001708 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001709 priv->isr_stats.sch++;
1710 }
Zhu Yib481de92007-09-25 17:54:57 -07001711
1712 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001713 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001714 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001715 priv->isr_stats.alive++;
1716 }
Zhu Yib481de92007-09-25 17:54:57 -07001717 }
1718#endif
1719 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001720 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001721
Zhu Yib481de92007-09-25 17:54:57 -07001722 /* Error detected by uCode */
1723 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001724 IWL_ERR(priv, "Microcode SW error detected. "
1725 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001726 priv->isr_stats.sw++;
1727 priv->isr_stats.sw_err = inta;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001728 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001729 handled |= CSR_INT_BIT_SW_ERR;
1730 }
1731
1732 /* uCode wakes up after power-down sleep */
1733 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001734 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001735 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001736 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1737 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1738 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1739 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1740 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1741 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001742
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001743 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001744 handled |= CSR_INT_BIT_WAKEUP;
1745 }
1746
1747 /* All uCode command responses, including Tx command responses,
1748 * Rx "responses" (frame-received notification), and other
1749 * notifications from uCode come through here*/
1750 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001751 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001752 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001753 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1754 }
1755
1756 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001757 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001758 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001759
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001760 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001761 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1762 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001763 handled |= CSR_INT_BIT_FH_TX;
1764 }
1765
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001766 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001767 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001768 priv->isr_stats.unhandled++;
1769 }
Zhu Yib481de92007-09-25 17:54:57 -07001770
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001771 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001772 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001773 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001774 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001775 }
1776
1777 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001778 /* only Re-enable if disabled by irq */
1779 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001780 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001781
Samuel Ortizd08853a2009-01-23 13:45:17 -08001782#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001783 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001784 inta = iwl_read32(priv, CSR_INT);
1785 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1786 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001787 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001788 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1789 }
1790#endif
Zhu Yib481de92007-09-25 17:54:57 -07001791}
1792
Abhijeet Kolekar14023642010-06-02 21:15:10 -07001793static int iwl3945_get_single_channel_for_scan(struct iwl_priv *priv,
1794 struct ieee80211_vif *vif,
1795 enum ieee80211_band band,
1796 struct iwl3945_scan_channel *scan_ch)
1797{
1798 const struct ieee80211_supported_band *sband;
1799 u16 passive_dwell = 0;
1800 u16 active_dwell = 0;
1801 int added = 0;
1802 u8 channel = 0;
1803
1804 sband = iwl_get_hw_mode(priv, band);
1805 if (!sband) {
1806 IWL_ERR(priv, "invalid band\n");
1807 return added;
1808 }
1809
1810 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
1811 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
1812
1813 if (passive_dwell <= active_dwell)
1814 passive_dwell = active_dwell + 1;
1815
1816
1817 channel = iwl_get_single_channel_number(priv, band);
1818
1819 if (channel) {
1820 scan_ch->channel = channel;
1821 scan_ch->type = 0; /* passive */
1822 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1823 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1824 /* Set txpower levels to defaults */
1825 scan_ch->tpc.dsp_atten = 110;
1826 if (band == IEEE80211_BAND_5GHZ)
1827 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1828 else
1829 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1830 added++;
1831 } else
1832 IWL_ERR(priv, "no valid channel found\n");
1833 return added;
1834}
1835
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001836static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001837 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001838 u8 is_active, u8 n_probes,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001839 struct iwl3945_scan_channel *scan_ch,
1840 struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07001841{
Johannes Berg4e05c232009-06-19 13:52:44 -07001842 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001843 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001844 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001845 u16 passive_dwell = 0;
1846 u16 active_dwell = 0;
1847 int added, i;
1848
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001849 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001850 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001851 return 0;
1852
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001853 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001854 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Zhu Yib481de92007-09-25 17:54:57 -07001855
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001856 if (passive_dwell <= active_dwell)
1857 passive_dwell = active_dwell + 1;
1858
Johannes Berg4e05c232009-06-19 13:52:44 -07001859 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1860 chan = priv->scan_request->channels[i];
1861
1862 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001863 continue;
1864
Johannes Berg4e05c232009-06-19 13:52:44 -07001865 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001866
Samuel Ortize6148912009-01-23 13:45:15 -08001867 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001868 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001869 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001870 scan_ch->channel);
1871 continue;
1872 }
1873
Zhu Yib481de92007-09-25 17:54:57 -07001874 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1875 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001876 /* If passive , set up for auto-switch
1877 * and use long active_dwell time.
1878 */
1879 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001880 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001881 scan_ch->type = 0; /* passive */
1882 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1883 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1884 } else {
1885 scan_ch->type = 1; /* active */
1886 }
1887
1888 /* Set direct probe bits. These may be used both for active
1889 * scan channels (probes gets sent right away),
1890 * or for passive channels (probes get se sent only after
1891 * hearing clear Rx packet).*/
1892 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1893 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001894 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001895 } else {
1896 /* uCode v1 does not allow setting direct probe bits on
1897 * passive channel. */
1898 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001899 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001900 }
Zhu Yib481de92007-09-25 17:54:57 -07001901
Ben Cahill9fbab512007-11-29 11:09:47 +08001902 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001903 scan_ch->tpc.dsp_atten = 110;
1904 /* scan_pwr_info->tpc.dsp_atten; */
1905
1906 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001907 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001908 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1909 else {
1910 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1911 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001912 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001913 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001914 */
1915 }
1916
Tomas Winklere1623442009-01-27 14:27:56 -08001917 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001918 scan_ch->channel,
1919 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1920 (scan_ch->type & 1) ?
1921 active_dwell : passive_dwell);
1922
1923 scan_ch++;
1924 added++;
1925 }
1926
Frans Pop91dd6c22010-03-24 14:19:58 -07001927 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001928 return added;
1929}
1930
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001931static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001932 struct ieee80211_rate *rates)
1933{
1934 int i;
1935
Dan Carpenter8e1a53c2010-03-28 14:55:00 +03001936 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001937 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1938 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1939 rates[i].hw_value_short = i;
1940 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001941 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001942 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001943 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001944 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001945 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001946 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001947 }
Zhu Yib481de92007-09-25 17:54:57 -07001948 }
1949}
1950
Zhu Yib481de92007-09-25 17:54:57 -07001951/******************************************************************************
1952 *
1953 * uCode download functions
1954 *
1955 ******************************************************************************/
1956
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001957static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001958{
Tomas Winkler98c92212008-01-14 17:46:20 -08001959 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1960 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1961 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1962 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1963 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1964 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001965}
1966
1967/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001968 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001969 * looking at all data.
1970 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001971static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001972{
1973 u32 val;
1974 u32 save_len = len;
1975 int rc = 0;
1976 u32 errcnt;
1977
Tomas Winklere1623442009-01-27 14:27:56 -08001978 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001979
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001980 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001981 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07001982
1983 errcnt = 0;
1984 for (; len > 0; len -= sizeof(u32), image++) {
1985 /* read data comes through single port, auto-incr addr */
1986 /* NOTE: Use the debugless read so we don't flood kernel log
1987 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001988 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001989 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001990 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001991 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1992 save_len - len, val, le32_to_cpu(*image));
1993 rc = -EIO;
1994 errcnt++;
1995 if (errcnt >= 20)
1996 break;
1997 }
1998 }
1999
Zhu Yib481de92007-09-25 17:54:57 -07002000
2001 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002002 IWL_DEBUG_INFO(priv,
2003 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002004
2005 return rc;
2006}
2007
2008
2009/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002010 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002011 * using sample data 100 bytes apart. If these sample points are good,
2012 * it's a pretty good bet that everything between them is good, too.
2013 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002014static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002015{
2016 u32 val;
2017 int rc = 0;
2018 u32 errcnt = 0;
2019 u32 i;
2020
Tomas Winklere1623442009-01-27 14:27:56 -08002021 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002022
Zhu Yib481de92007-09-25 17:54:57 -07002023 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2024 /* read data comes through single port, auto-incr addr */
2025 /* NOTE: Use the debugless read so we don't flood kernel log
2026 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002027 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002028 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002029 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002030 if (val != le32_to_cpu(*image)) {
2031#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002032 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002033 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2034 i, val, *image);
2035#endif
2036 rc = -EIO;
2037 errcnt++;
2038 if (errcnt >= 3)
2039 break;
2040 }
2041 }
2042
Zhu Yib481de92007-09-25 17:54:57 -07002043 return rc;
2044}
2045
2046
2047/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002048 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002049 * and verify its contents
2050 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002051static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002052{
2053 __le32 *image;
2054 u32 len;
2055 int rc = 0;
2056
2057 /* Try bootstrap */
2058 image = (__le32 *)priv->ucode_boot.v_addr;
2059 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002060 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002061 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002062 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002063 return 0;
2064 }
2065
2066 /* Try initialize */
2067 image = (__le32 *)priv->ucode_init.v_addr;
2068 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002069 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002070 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002071 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002072 return 0;
2073 }
2074
2075 /* Try runtime/protocol */
2076 image = (__le32 *)priv->ucode_code.v_addr;
2077 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002078 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002079 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002080 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002081 return 0;
2082 }
2083
Winkler, Tomas15b16872008-12-19 10:37:33 +08002084 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002085
Ben Cahill9fbab512007-11-29 11:09:47 +08002086 /* Since nothing seems to match, show first several data entries in
2087 * instruction SRAM, so maybe visual inspection will give a clue.
2088 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002089 image = (__le32 *)priv->ucode_boot.v_addr;
2090 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002091 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002092
2093 return rc;
2094}
2095
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002096static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002097{
2098 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002099 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002100}
2101
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002102#define IWL3945_UCODE_GET(item) \
2103static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
2104{ \
2105 return le32_to_cpu(ucode->u.v1.item); \
2106}
2107
2108static u32 iwl3945_ucode_get_header_size(u32 api_ver)
2109{
Johannes Berg22adba22010-04-28 12:09:14 -07002110 return 24;
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002111}
2112
2113static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
2114{
2115 return (u8 *) ucode->u.v1.data;
2116}
2117
2118IWL3945_UCODE_GET(inst_size);
2119IWL3945_UCODE_GET(data_size);
2120IWL3945_UCODE_GET(init_size);
2121IWL3945_UCODE_GET(init_data_size);
2122IWL3945_UCODE_GET(boot_size);
2123
Zhu Yib481de92007-09-25 17:54:57 -07002124/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002125 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002126 *
2127 * Copy into buffers for card to fetch via bus-mastering
2128 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002129static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002130{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002131 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002132 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002133 const struct firmware *ucode_raw;
2134 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002135 const char *name_pre = priv->cfg->fw_name_pre;
2136 const unsigned int api_max = priv->cfg->ucode_api_max;
2137 const unsigned int api_min = priv->cfg->ucode_api_min;
2138 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002139 u8 *src;
2140 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002141 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002142
2143 /* Ask kernel firmware_class module to get the boot firmware off disk.
2144 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002145 for (index = api_max; index >= api_min; index--) {
2146 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2147 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2148 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002149 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002150 buf, ret);
2151 if (ret == -ENOENT)
2152 continue;
2153 else
2154 goto error;
2155 } else {
2156 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002157 IWL_ERR(priv, "Loaded firmware %s, "
2158 "which is deprecated. "
2159 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002160 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002161 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2162 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002163 buf, ucode_raw->size);
2164 break;
2165 }
Zhu Yib481de92007-09-25 17:54:57 -07002166 }
2167
Reinette Chatrea0987a82008-12-02 12:14:06 -08002168 if (ret < 0)
2169 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002170
2171 /* Make sure that we got at least our header! */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002172 if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002173 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002174 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002175 goto err_release;
2176 }
2177
2178 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002179 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002180
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002181 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002182 api_ver = IWL_UCODE_API(priv->ucode_ver);
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002183 inst_size = iwl3945_ucode_get_inst_size(ucode);
2184 data_size = iwl3945_ucode_get_data_size(ucode);
2185 init_size = iwl3945_ucode_get_init_size(ucode);
2186 init_data_size = iwl3945_ucode_get_init_data_size(ucode);
2187 boot_size = iwl3945_ucode_get_boot_size(ucode);
2188 src = iwl3945_ucode_get_data(ucode);
Zhu Yib481de92007-09-25 17:54:57 -07002189
Reinette Chatrea0987a82008-12-02 12:14:06 -08002190 /* api_ver should match the api version forming part of the
2191 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002192 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002193
2194 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002195 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002196 "Driver supports v%u, firmware is v%u.\n",
2197 api_max, api_ver);
2198 priv->ucode_ver = 0;
2199 ret = -EINVAL;
2200 goto err_release;
2201 }
2202 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002203 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002204 "got %u. New firmware can be obtained "
2205 "from http://www.intellinuxwireless.org.\n",
2206 api_max, api_ver);
2207
Tomas Winkler978785a2008-12-19 10:37:31 +08002208 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2209 IWL_UCODE_MAJOR(priv->ucode_ver),
2210 IWL_UCODE_MINOR(priv->ucode_ver),
2211 IWL_UCODE_API(priv->ucode_ver),
2212 IWL_UCODE_SERIAL(priv->ucode_ver));
2213
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07002214 snprintf(priv->hw->wiphy->fw_version,
2215 sizeof(priv->hw->wiphy->fw_version),
2216 "%u.%u.%u.%u",
2217 IWL_UCODE_MAJOR(priv->ucode_ver),
2218 IWL_UCODE_MINOR(priv->ucode_ver),
2219 IWL_UCODE_API(priv->ucode_ver),
2220 IWL_UCODE_SERIAL(priv->ucode_ver));
2221
Tomas Winklere1623442009-01-27 14:27:56 -08002222 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002223 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002224 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2225 inst_size);
2226 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2227 data_size);
2228 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2229 init_size);
2230 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2231 init_data_size);
2232 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2233 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002234
Reinette Chatrea0987a82008-12-02 12:14:06 -08002235
Zhu Yib481de92007-09-25 17:54:57 -07002236 /* Verify size of file vs. image size info in file's header */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002237 if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002238 inst_size + data_size + init_size +
2239 init_data_size + boot_size) {
2240
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002241 IWL_DEBUG_INFO(priv,
2242 "uCode file size %zd does not match expected size\n",
2243 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002244 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002245 goto err_release;
2246 }
2247
2248 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002249 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002250 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002251 inst_size);
2252 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002253 goto err_release;
2254 }
2255
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002256 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002257 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002258 data_size);
2259 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002260 goto err_release;
2261 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002262 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002263 IWL_DEBUG_INFO(priv,
2264 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002265 init_size);
2266 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002267 goto err_release;
2268 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002269 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002270 IWL_DEBUG_INFO(priv,
2271 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002272 init_data_size);
2273 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002274 goto err_release;
2275 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002276 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002277 IWL_DEBUG_INFO(priv,
2278 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002279 boot_size);
2280 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002281 goto err_release;
2282 }
2283
2284 /* Allocate ucode buffers for card's bus-master loading ... */
2285
2286 /* Runtime instructions and 2 copies of data:
2287 * 1) unmodified from disk
2288 * 2) backup cache for save/restore during power-downs */
2289 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002290 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002291
2292 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002293 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002294
2295 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002296 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002297
2298 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002299 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002300 goto err_pci_alloc;
2301
Tomas Winkler90e759d2007-11-29 11:09:41 +08002302 /* Initialization instructions and data */
2303 if (init_size && init_data_size) {
2304 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002305 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002306
2307 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002308 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002309
2310 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2311 goto err_pci_alloc;
2312 }
2313
2314 /* Bootstrap (instructions only, no data) */
2315 if (boot_size) {
2316 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002317 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002318
2319 if (!priv->ucode_boot.v_addr)
2320 goto err_pci_alloc;
2321 }
2322
Zhu Yib481de92007-09-25 17:54:57 -07002323 /* Copy images into buffers for card's bus-master reads ... */
2324
2325 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002326 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002327 IWL_DEBUG_INFO(priv,
2328 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002329 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002330 src += len;
2331
Tomas Winklere1623442009-01-27 14:27:56 -08002332 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002333 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2334
2335 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002336 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002337 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002338 IWL_DEBUG_INFO(priv,
2339 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002340 memcpy(priv->ucode_data.v_addr, src, len);
2341 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002342 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002343
2344 /* Initialization instructions (3rd block) */
2345 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002346 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002347 IWL_DEBUG_INFO(priv,
2348 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002349 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002350 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002351 }
2352
2353 /* Initialization data (4th block) */
2354 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002355 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002356 IWL_DEBUG_INFO(priv,
2357 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002358 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002359 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002360 }
2361
2362 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002363 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002364 IWL_DEBUG_INFO(priv,
2365 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002366 memcpy(priv->ucode_boot.v_addr, src, len);
2367
2368 /* We have our copies now, allow OS release its copies */
2369 release_firmware(ucode_raw);
2370 return 0;
2371
2372 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002373 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002374 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002375 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002376
2377 err_release:
2378 release_firmware(ucode_raw);
2379
2380 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002381 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002382}
2383
2384
2385/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002386 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002387 *
2388 * Tell initialization uCode where to find runtime uCode.
2389 *
2390 * BSM registers initially contain pointers to initialization uCode.
2391 * We need to replace them to load runtime uCode inst and data,
2392 * and to save runtime data when powering down.
2393 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002394static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002395{
2396 dma_addr_t pinst;
2397 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002398
2399 /* bits 31:0 for 3945 */
2400 pinst = priv->ucode_code.p_addr;
2401 pdata = priv->ucode_data_backup.p_addr;
2402
Zhu Yib481de92007-09-25 17:54:57 -07002403 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002404 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2405 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2406 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002407 priv->ucode_data.len);
2408
Tomas Winklera96a27f2008-10-23 23:48:56 -07002409 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002410 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002411 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002412 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2413
Tomas Winklere1623442009-01-27 14:27:56 -08002414 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002415
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002416 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002417}
2418
2419/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002420 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002421 *
2422 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2423 *
Zhu Yib481de92007-09-25 17:54:57 -07002424 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002425 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002426static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002427{
2428 /* Check alive response for "valid" sign from uCode */
2429 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2430 /* We had an error bringing up the hardware, so take it
2431 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002432 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002433 goto restart;
2434 }
2435
2436 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2437 * This is a paranoid check, because we would not have gotten the
2438 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002439 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002440 /* Runtime instruction load was bad;
2441 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002442 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002443 goto restart;
2444 }
2445
2446 /* Send pointers to protocol/runtime uCode image ... init code will
2447 * load and launch runtime uCode, which will send us another "Alive"
2448 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002449 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002450 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002451 /* Runtime instruction load won't happen;
2452 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002453 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002454 goto restart;
2455 }
2456 return;
2457
2458 restart:
2459 queue_work(priv->workqueue, &priv->restart);
2460}
2461
Zhu Yib481de92007-09-25 17:54:57 -07002462/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002463 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002464 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002465 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002466 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002467static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002468{
Zhu Yib481de92007-09-25 17:54:57 -07002469 int thermal_spin = 0;
2470 u32 rfkill;
2471
Tomas Winklere1623442009-01-27 14:27:56 -08002472 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002473
2474 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2475 /* We had an error bringing up the hardware, so take it
2476 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002477 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002478 goto restart;
2479 }
2480
2481 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2482 * This is a paranoid check, because we would not have gotten the
2483 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002484 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002485 /* Runtime instruction load was bad;
2486 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002487 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002488 goto restart;
2489 }
2490
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002491 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002492 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002493
2494 if (rfkill & 0x1) {
2495 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002496 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002497 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002498 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002499 thermal_spin++;
2500 udelay(10);
2501 }
2502
2503 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002504 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002505 thermal_spin * 10);
2506 } else
2507 set_bit(STATUS_RF_KILL_HW, &priv->status);
2508
Ben Cahill9fbab512007-11-29 11:09:47 +08002509 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002510 set_bit(STATUS_ALIVE, &priv->status);
2511
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08002512 if (priv->cfg->ops->lib->recover_from_tx_stall) {
2513 /* Enable timer to monitor the driver queues */
2514 mod_timer(&priv->monitor_recover,
2515 jiffies +
2516 msecs_to_jiffies(priv->cfg->monitor_recover_period));
2517 }
2518
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002519 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002520 return;
2521
Johannes Berg36d68252008-05-15 12:55:26 +02002522 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002523
Johannes Berg470ab2d2010-01-21 11:23:30 -08002524 priv->active_rate = IWL_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07002525
Ben Cahill4d6ccbf2009-11-13 11:56:29 -08002526 iwl_power_update_mode(priv, true);
Zhu Yib481de92007-09-25 17:54:57 -07002527
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002528 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002529 struct iwl3945_rxon_cmd *active_rxon =
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002530 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07002531
Abhijeet Kolekar8a9b9922009-06-12 13:22:52 -07002532 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002533 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2534 } else {
2535 /* Initialize our rx_config data */
Johannes Berg1dda6d22010-04-29 04:43:06 -07002536 iwl_connection_init_rx_config(priv, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002537 }
2538
Ben Cahill9fbab512007-11-29 11:09:47 +08002539 /* Configure Bluetooth device coexistence support */
Johannes Berg65b52bd2010-04-13 01:04:31 -07002540 priv->cfg->ops->hcmd->send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002541
2542 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002543 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002544
Zhu Yib481de92007-09-25 17:54:57 -07002545 iwl3945_reg_txpower_periodic(priv);
2546
Johannes Berge932a602009-10-02 13:44:03 -07002547 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002548
Tomas Winklere1623442009-01-27 14:27:56 -08002549 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002550 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002551 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002552
Zhu Yib481de92007-09-25 17:54:57 -07002553 return;
2554
2555 restart:
2556 queue_work(priv->workqueue, &priv->restart);
2557}
2558
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002559static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002560
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002561static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002562{
2563 unsigned long flags;
2564 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2565 struct ieee80211_conf *conf = NULL;
2566
Tomas Winklere1623442009-01-27 14:27:56 -08002567 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002568
2569 conf = ieee80211_get_hw_conf(priv->hw);
2570
2571 if (!exit_pending)
2572 set_bit(STATUS_EXIT_PENDING, &priv->status);
2573
Reinette Chatre7e246192010-02-18 22:58:32 -08002574 /* Station information will now be cleared in device */
Johannes Berg2c810cc2010-04-29 00:53:29 -07002575 iwl_clear_ucode_stations(priv);
2576 iwl_dealloc_bcast_station(priv);
Johannes Bergdb125c72010-05-07 01:49:15 -07002577 iwl_clear_driver_stations(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002578
2579 /* Unblock any waiting calls */
2580 wake_up_interruptible_all(&priv->wait_command_queue);
2581
Zhu Yib481de92007-09-25 17:54:57 -07002582 /* Wipe out the EXIT_PENDING status bit if we are not actually
2583 * exiting the module */
2584 if (!exit_pending)
2585 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2586
2587 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002588 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002589
2590 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002591 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002592 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002593 spin_unlock_irqrestore(&priv->lock, flags);
2594 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002595
2596 if (priv->mac80211_registered)
2597 ieee80211_stop_queues(priv->hw);
2598
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002599 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002600 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002601 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002602 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2603 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002604 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2605 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002606 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2607 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002608 goto exit;
2609 }
2610
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002611 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002612 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002613 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2614 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002615 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2616 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002617 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002618 STATUS_FW_ERROR |
2619 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2620 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002621
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002622 iwl3945_hw_txq_ctx_stop(priv);
2623 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002624
Ben Cahill309e7312009-11-06 14:53:03 -08002625 /* Power-down device's busmaster DMA clocks */
2626 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002627 udelay(5);
2628
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002629 /* Stop the device, and put it in low power state */
2630 priv->cfg->ops->lib->apm_ops.stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002631
Zhu Yib481de92007-09-25 17:54:57 -07002632 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002633 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002634
2635 if (priv->ibss_beacon)
2636 dev_kfree_skb(priv->ibss_beacon);
2637 priv->ibss_beacon = NULL;
2638
2639 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002640 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002641}
2642
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002643static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002644{
2645 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002646 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002647 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002649 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002650}
2651
2652#define MAX_HW_RESTARTS 5
2653
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002654static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002655{
2656 int rc, i;
2657
Johannes Berg2c810cc2010-04-29 00:53:29 -07002658 rc = iwl_alloc_bcast_station(priv, false);
2659 if (rc)
2660 return rc;
2661
Zhu Yib481de92007-09-25 17:54:57 -07002662 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002663 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002664 return -EIO;
2665 }
2666
Reinette Chatree903fbd2008-01-30 22:05:15 -08002667 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002668 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002669 return -EIO;
2670 }
2671
Zhu Yie655b9f2008-01-24 02:19:38 -08002672 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002673 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002674 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2675 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2676 else {
2677 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002678 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2679 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002680 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002681
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002682 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002683
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002684 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002685 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002686 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002687 return rc;
2688 }
2689
2690 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002691 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2692 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002693 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2694
2695 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002696 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002697 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002698
2699 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002700 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2701 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002702
2703 /* Copy original ucode data image from disk into backup cache.
2704 * This will be used to initialize the on-board processor's
2705 * data SRAM for a clean start when the runtime program first loads. */
2706 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08002707 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002708
Zhu Yie655b9f2008-01-24 02:19:38 -08002709 /* We return success when we resume from suspend and rf_kill is on. */
2710 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2711 return 0;
2712
Zhu Yib481de92007-09-25 17:54:57 -07002713 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2714
Zhu Yib481de92007-09-25 17:54:57 -07002715 /* load bootstrap state machine,
2716 * load bootstrap program into processor's memory,
2717 * prepare to load the "initialize" uCode */
Abhijeet Kolekar75a9a922010-02-26 15:17:01 -08002718 rc = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002719
2720 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002721 IWL_ERR(priv,
2722 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002723 continue;
2724 }
2725
2726 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002727 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002728
Tomas Winklere1623442009-01-27 14:27:56 -08002729 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002730
2731 return 0;
2732 }
2733
2734 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002735 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002736 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002737
2738 /* tried to restart and config the device for as long as our
2739 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002740 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002741 return -EIO;
2742}
2743
2744
2745/*****************************************************************************
2746 *
2747 * Workqueue callbacks
2748 *
2749 *****************************************************************************/
2750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002751static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002752{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002753 struct iwl_priv *priv =
2754 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002755
2756 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2757 return;
2758
2759 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002760 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002761 mutex_unlock(&priv->mutex);
2762}
2763
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002764static void iwl3945_bg_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, 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_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002774 mutex_unlock(&priv->mutex);
2775}
2776
Ben Cahill743cdf12009-10-09 13:20:26 -07002777/*
2778 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2779 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2780 * *is* readable even when device has been SW_RESET into low power mode
2781 * (e.g. during RF KILL).
2782 */
Helmut Schaa26635162009-01-15 09:38:44 +01002783static void iwl3945_rfkill_poll(struct work_struct *data)
2784{
2785 struct iwl_priv *priv =
Johannes Bergee525d12010-01-21 06:09:28 -08002786 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002787 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2788 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2789 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002790
Ben Cahill743cdf12009-10-09 13:20:26 -07002791 if (new_rfkill != old_rfkill) {
2792 if (new_rfkill)
2793 set_bit(STATUS_RF_KILL_HW, &priv->status);
2794 else
2795 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002796
Ben Cahill743cdf12009-10-09 13:20:26 -07002797 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002798
Ben Cahill743cdf12009-10-09 13:20:26 -07002799 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2800 new_rfkill ? "disable radio" : "enable radio");
2801 }
2802
2803 /* Keep this running, even if radio now enabled. This will be
2804 * cancelled in mac_start() if system decides to start again */
Johannes Bergee525d12010-01-21 06:09:28 -08002805 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01002806 round_jiffies_relative(2 * HZ));
2807
2808}
2809
Johannes Berg1dda6d22010-04-29 04:43:06 -07002810void iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07002811{
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002812 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002813 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002814 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002815 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002816 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002817 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07002818 struct ieee80211_conf *conf = NULL;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002819 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002820 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002821 bool is_active = false;
Zhu Yib481de92007-09-25 17:54:57 -07002822
2823 conf = ieee80211_get_hw_conf(priv->hw);
2824
Reinette Chatrefbc9f972009-05-15 16:13:46 -07002825 cancel_delayed_work(&priv->scan_check);
2826
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002827 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002828 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002829 goto done;
2830 }
2831
Tomas Winklera96a27f2008-10-23 23:48:56 -07002832 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07002833 * was given the chance to run... */
2834 if (!test_bit(STATUS_SCANNING, &priv->status))
2835 goto done;
2836
2837 /* This should never be called or scheduled if there is currently
2838 * a scan active in the hardware. */
2839 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002840 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
2841 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002842 goto done;
2843 }
2844
2845 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002846 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07002847 goto done;
2848 }
2849
2850 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002851 IWL_DEBUG_HC(priv,
2852 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002853 goto done;
2854 }
2855
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002856 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002857 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07002858 goto done;
2859 }
2860
2861 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002862 IWL_DEBUG_HC(priv,
2863 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002864 goto done;
2865 }
2866
Johannes Berg811ecc92010-04-06 04:12:41 -07002867 if (!priv->scan_cmd) {
2868 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2869 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2870 if (!priv->scan_cmd) {
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002871 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
Zhu Yib481de92007-09-25 17:54:57 -07002872 goto done;
2873 }
2874 }
Johannes Berg811ecc92010-04-06 04:12:41 -07002875 scan = priv->scan_cmd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002876 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002877
2878 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2879 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2880
Samuel Ortiz8ccde882009-01-27 14:27:52 -08002881 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002882 u16 interval = 0;
2883 u32 extra;
2884 u32 suspend_time = 100;
2885 u32 scan_suspend_time = 100;
2886 unsigned long flags;
2887
Tomas Winklere1623442009-01-27 14:27:56 -08002888 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002889
2890 spin_lock_irqsave(&priv->lock, flags);
John W. Linvillea6e492b2010-07-22 15:24:56 -04002891 if (priv->is_internal_short_scan)
2892 interval = 0;
2893 else
2894 interval = vif->bss_conf.beacon_int;
Zhu Yib481de92007-09-25 17:54:57 -07002895 spin_unlock_irqrestore(&priv->lock, flags);
2896
2897 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002898 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002899 if (!interval)
2900 interval = suspend_time;
2901 /*
2902 * suspend time format:
2903 * 0-19: beacon interval in usec (time before exec.)
2904 * 20-23: 0
2905 * 24-31: number of beacons (suspend between channels)
2906 */
2907
2908 extra = (suspend_time / interval) << 24;
2909 scan_suspend_time = 0xFF0FFFFF &
2910 (extra | ((suspend_time % interval) * 1024));
2911
2912 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002913 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002914 scan_suspend_time, interval);
2915 }
2916
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002917 if (priv->is_internal_short_scan) {
2918 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2919 } else if (priv->scan_request->n_ssids) {
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002920 int i, p = 0;
2921 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2922 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2923 /* always does wildcard anyway */
2924 if (!priv->scan_request->ssids[i].ssid_len)
2925 continue;
2926 scan->direct_scan[p].id = WLAN_EID_SSID;
2927 scan->direct_scan[p].len =
2928 priv->scan_request->ssids[i].ssid_len;
2929 memcpy(scan->direct_scan[p].ssid,
2930 priv->scan_request->ssids[i].ssid,
2931 priv->scan_request->ssids[i].ssid_len);
2932 n_probes++;
2933 p++;
2934 }
2935 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002936 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002937 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002938
2939 /* We don't build a direct scan probe request; the uCode will do
2940 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002941 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002942 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002943 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2944
2945 /* flags + rate selection */
2946
Johannes Berg00700ee2010-04-06 04:12:37 -07002947 switch (priv->scan_band) {
2948 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002949 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2950 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2951 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002952 band = IEEE80211_BAND_2GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002953 break;
2954 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002955 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Bergb097ad22009-04-20 14:36:57 -07002956 /*
2957 * If active scaning is requested but a certain channel
2958 * is marked passive, we can do active scanning if we
2959 * detect transmissions.
2960 */
Johannes Berg96ff5642010-04-30 14:42:15 -07002961 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2962 IWL_GOOD_CRC_TH_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +01002963 band = IEEE80211_BAND_5GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002964 break;
2965 default:
2966 IWL_WARN(priv, "Invalid scan band\n");
Zhu Yib481de92007-09-25 17:54:57 -07002967 goto done;
2968 }
2969
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002970 if (!priv->is_internal_short_scan) {
2971 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002972 iwl_fill_probe_req(priv,
2973 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002974 vif->addr,
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002975 priv->scan_request->ie,
2976 priv->scan_request->ie_len,
2977 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002978 } else {
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002979 /* use bcast addr, will not be transmitted but must be valid */
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002980 scan->tx_cmd.len = cpu_to_le16(
2981 iwl_fill_probe_req(priv,
2982 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002983 iwl_bcast_addr, NULL, 0,
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002984 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2985 }
Zhu Yib481de92007-09-25 17:54:57 -07002986 /* select Rx antennas */
2987 scan->flags |= iwl3945_get_antenna_flags(priv);
2988
Abhijeet Kolekar14023642010-06-02 21:15:10 -07002989 if (priv->is_internal_short_scan) {
2990 scan->channel_count =
2991 iwl3945_get_single_channel_for_scan(priv, vif, band,
2992 (void *)&scan->data[le16_to_cpu(
2993 scan->tx_cmd.len)]);
2994 } else {
2995 scan->channel_count =
2996 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
2997 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
2998 }
Zhu Yib481de92007-09-25 17:54:57 -07002999
Reinette Chatre14b54332008-11-04 12:21:35 -08003000 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08003001 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08003002 goto done;
3003 }
3004
Zhu Yib481de92007-09-25 17:54:57 -07003005 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003006 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003007 cmd.data = scan;
3008 scan->len = cpu_to_le16(cmd.len);
3009
3010 set_bit(STATUS_SCAN_HW, &priv->status);
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08003011 if (iwl_send_cmd_sync(priv, &cmd))
Zhu Yib481de92007-09-25 17:54:57 -07003012 goto done;
3013
3014 queue_delayed_work(priv->workqueue, &priv->scan_check,
3015 IWL_SCAN_CHECK_WATCHDOG);
3016
Zhu Yib481de92007-09-25 17:54:57 -07003017 return;
3018
3019 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003020 /* can not perform scan make sure we clear scanning
3021 * bits from status so next scan request can be performed.
3022 * if we dont clear scanning status bit here all next scan
3023 * will fail
3024 */
3025 clear_bit(STATUS_SCAN_HW, &priv->status);
3026 clear_bit(STATUS_SCANNING, &priv->status);
3027
Ian Schram01ebd062007-10-25 17:15:22 +08003028 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003029 queue_work(priv->workqueue, &priv->scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07003030}
3031
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003032static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003033{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003034 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003035
3036 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3037 return;
3038
Johannes Berg19cc1082009-05-08 13:44:36 -07003039 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
3040 mutex_lock(&priv->mutex);
3041 priv->vif = NULL;
3042 priv->is_open = 0;
3043 mutex_unlock(&priv->mutex);
3044 iwl3945_down(priv);
3045 ieee80211_restart_hw(priv->hw);
3046 } else {
3047 iwl3945_down(priv);
Johannes Berg80676512010-01-21 06:07:17 -08003048
3049 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3050 return;
3051
3052 mutex_lock(&priv->mutex);
3053 __iwl3945_up(priv);
3054 mutex_unlock(&priv->mutex);
Johannes Berg19cc1082009-05-08 13:44:36 -07003055 }
Zhu Yib481de92007-09-25 17:54:57 -07003056}
3057
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003058static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003059{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003060 struct iwl_priv *priv =
3061 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003062
3063 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3064 return;
3065
3066 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003067 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003068 mutex_unlock(&priv->mutex);
3069}
3070
Johannes Berg1dda6d22010-04-29 04:43:06 -07003071void iwl3945_post_associate(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003072{
Zhu Yib481de92007-09-25 17:54:57 -07003073 int rc = 0;
3074 struct ieee80211_conf *conf = NULL;
3075
Johannes Berg1dda6d22010-04-29 04:43:06 -07003076 if (!vif || !priv->is_open)
3077 return;
3078
3079 if (vif->type == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003080 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003081 return;
3082 }
3083
Tomas Winklere1623442009-01-27 14:27:56 -08003084 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berg1dda6d22010-04-29 04:43:06 -07003085 vif->bss_conf.aid, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003086
3087 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3088 return;
3089
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003090 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003091
Zhu Yib481de92007-09-25 17:54:57 -07003092 conf = ieee80211_get_hw_conf(priv->hw);
3093
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003094 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003095 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003096
Tomas Winkler28afaf92008-12-19 10:37:06 +08003097 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Johannes Berg1dda6d22010-04-29 04:43:06 -07003098 iwl_setup_rxon_timing(priv, vif);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003099 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07003100 sizeof(priv->rxon_timing), &priv->rxon_timing);
3101 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003102 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003103 "Attempting to continue.\n");
3104
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003105 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003106
Johannes Berg1dda6d22010-04-29 04:43:06 -07003107 priv->staging_rxon.assoc_id = cpu_to_le16(vif->bss_conf.aid);
Zhu Yib481de92007-09-25 17:54:57 -07003108
Tomas Winklere1623442009-01-27 14:27:56 -08003109 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Johannes Berg1dda6d22010-04-29 04:43:06 -07003110 vif->bss_conf.aid, vif->bss_conf.beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07003111
Johannes Bergc213d742010-05-06 12:21:40 -07003112 if (vif->bss_conf.use_short_preamble)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003113 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003114 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003115 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003116
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003117 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003118 if (vif->bss_conf.use_short_slot)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003119 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003120 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003121 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003122 }
3123
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003124 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003125
Johannes Berg1dda6d22010-04-29 04:43:06 -07003126 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003127 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003128 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003129 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02003130 case NL80211_IFTYPE_ADHOC:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003131 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003132 break;
Zhu Yib481de92007-09-25 17:54:57 -07003133 default:
Johannes Berg1dda6d22010-04-29 04:43:06 -07003134 IWL_ERR(priv, "%s Should not be called in %d mode\n",
3135 __func__, vif->type);
Zhu Yib481de92007-09-25 17:54:57 -07003136 break;
3137 }
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003138}
3139
Zhu Yib481de92007-09-25 17:54:57 -07003140/*****************************************************************************
3141 *
3142 * mac80211 entry point functions
3143 *
3144 *****************************************************************************/
3145
Zhu Yi5a66926a2008-01-14 17:46:18 -08003146#define UCODE_READY_TIMEOUT (2 * HZ)
3147
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003148static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003149{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003150 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003151 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003152
Tomas Winklere1623442009-01-27 14:27:56 -08003153 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003154
3155 /* we should be verifying the device is ready to be opened */
3156 mutex_lock(&priv->mutex);
3157
Zhu Yi5a66926a2008-01-14 17:46:18 -08003158 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3159 * ucode filename and max sizes are card-specific. */
3160
3161 if (!priv->ucode_code.len) {
3162 ret = iwl3945_read_ucode(priv);
3163 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003164 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003165 mutex_unlock(&priv->mutex);
3166 goto out_release_irq;
3167 }
3168 }
3169
Zhu Yie655b9f2008-01-24 02:19:38 -08003170 ret = __iwl3945_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003171
Zhu Yib481de92007-09-25 17:54:57 -07003172 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003173
Zhu Yie655b9f2008-01-24 02:19:38 -08003174 if (ret)
3175 goto out_release_irq;
3176
Tomas Winklere1623442009-01-27 14:27:56 -08003177 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003178
Zhu Yi5a66926a2008-01-14 17:46:18 -08003179 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3180 * mac80211 will not be run successfully. */
3181 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3182 test_bit(STATUS_READY, &priv->status),
3183 UCODE_READY_TIMEOUT);
3184 if (!ret) {
3185 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003186 IWL_ERR(priv,
3187 "Wait for START_ALIVE timeout after %dms.\n",
3188 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a66926a2008-01-14 17:46:18 -08003189 ret = -ETIMEDOUT;
3190 goto out_release_irq;
3191 }
3192 }
3193
Helmut Schaa26635162009-01-15 09:38:44 +01003194 /* ucode is running and will send rfkill notifications,
3195 * no need to poll the killswitch state anymore */
Johannes Bergee525d12010-01-21 06:09:28 -08003196 cancel_delayed_work(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01003197
Johannes Berge932a602009-10-02 13:44:03 -07003198 iwl_led_start(priv);
3199
Zhu Yie655b9f2008-01-24 02:19:38 -08003200 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003201 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003202 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003203
3204out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003205 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003206 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08003207 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003208}
3209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003210static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003211{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003212 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003213
Tomas Winklere1623442009-01-27 14:27:56 -08003214 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003215
Zhu Yie655b9f2008-01-24 02:19:38 -08003216 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003217 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003218 return;
3219 }
3220
Zhu Yib481de92007-09-25 17:54:57 -07003221 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003222
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003223 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003224 /* stop mac, cancel any scan request and clear
3225 * RXON_FILTER_ASSOC_MSK BIT
3226 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08003227 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003228 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003229 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003230 }
3231
Zhu Yi5a66926a2008-01-14 17:46:18 -08003232 iwl3945_down(priv);
3233
3234 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003235
3236 /* start polling the killswitch state again */
Johannes Bergee525d12010-01-21 06:09:28 -08003237 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01003238 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003239
Tomas Winklere1623442009-01-27 14:27:56 -08003240 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003241}
3242
Johannes Berge039fa42008-05-15 12:55:29 +02003243static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003244{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003245 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003246
Tomas Winklere1623442009-01-27 14:27:56 -08003247 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003248
Tomas Winklere1623442009-01-27 14:27:56 -08003249 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003250 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003251
Johannes Berge039fa42008-05-15 12:55:29 +02003252 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003253 dev_kfree_skb_any(skb);
3254
Tomas Winklere1623442009-01-27 14:27:56 -08003255 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003256 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003257}
3258
Johannes Berg1dda6d22010-04-29 04:43:06 -07003259void iwl3945_config_ap(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003260{
3261 int rc = 0;
3262
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003263 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003264 return;
3265
3266 /* The following should be done only at AP bring up */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003267 if (!(iwl_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07003268
3269 /* RXON - unassoc (to set timing command) */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003270 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003271 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003272
3273 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08003274 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Johannes Berg1dda6d22010-04-29 04:43:06 -07003275 iwl_setup_rxon_timing(priv, vif);
Samuel Ortiz518099a2009-01-19 15:30:27 -08003276 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3277 sizeof(priv->rxon_timing),
3278 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07003279 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003280 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003281 "Attempting to continue.\n");
3282
Johannes Berg1dda6d22010-04-29 04:43:06 -07003283 priv->staging_rxon.assoc_id = 0;
3284
Johannes Bergc213d742010-05-06 12:21:40 -07003285 if (vif->bss_conf.use_short_preamble)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003286 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003287 RXON_FLG_SHORT_PREAMBLE_MSK;
3288 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003289 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003290 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3291
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003292 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003293 if (vif->bss_conf.use_short_slot)
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003294 priv->staging_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003295 RXON_FLG_SHORT_SLOT_MSK;
3296 else
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003297 priv->staging_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003298 ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003299 }
3300 /* restore RXON assoc */
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003301 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003302 iwlcore_commit_rxon(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08003303 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003304 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003305
3306 /* FIXME - we need to add code here to detect a totally new
3307 * configuration, reset the AP, unassoc, rxon timing, assoc,
3308 * clear sta table, add BCAST sta... */
3309}
3310
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003311static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003312 struct ieee80211_vif *vif,
3313 struct ieee80211_sta *sta,
3314 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003315{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003316 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003317 int ret = 0;
3318 u8 sta_id = IWL_INVALID_STATION;
3319 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003320
Tomas Winklere1623442009-01-27 14:27:56 -08003321 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003322
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003323 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003324 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003325 return -EOPNOTSUPP;
3326 }
3327
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003328 static_key = !iwl_is_associated(priv);
3329
3330 if (!static_key) {
Johannes Berg0af8bca2010-04-30 14:08:00 -07003331 sta_id = iwl_sta_id_or_broadcast(priv, sta);
3332 if (sta_id == IWL_INVALID_STATION)
3333 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003334 }
3335
3336 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003337 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003338
Zhu Yib481de92007-09-25 17:54:57 -07003339 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003340 case SET_KEY:
3341 if (static_key)
3342 ret = iwl3945_set_static_key(priv, key);
3343 else
3344 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3345 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003346 break;
3347 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003348 if (static_key)
3349 ret = iwl3945_remove_static_key(priv);
3350 else
3351 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3352 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003353 break;
3354 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003355 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003356 }
3357
Johannes Berg72e15d72010-02-19 11:42:32 -08003358 mutex_unlock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003359 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003360
Winkler, Tomas42986792009-01-19 15:30:22 -08003361 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003362}
3363
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003364static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3365 struct ieee80211_vif *vif,
3366 struct ieee80211_sta *sta)
3367{
3368 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07003369 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003370 int ret;
Johannes Bergfd1af152010-04-30 11:30:43 -07003371 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003372 u8 sta_id;
3373
3374 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3375 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003376 mutex_lock(&priv->mutex);
3377 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
3378 sta->addr);
3379 sta_priv->common.sta_id = IWL_INVALID_STATION;
3380
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003381
3382 ret = iwl_add_station_common(priv, sta->addr, is_ap, &sta->ht_cap,
3383 &sta_id);
3384 if (ret) {
3385 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3386 sta->addr, ret);
3387 /* Should we return success if return code is EEXIST ? */
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003388 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003389 return ret;
3390 }
3391
Johannes Bergfd1af152010-04-30 11:30:43 -07003392 sta_priv->common.sta_id = sta_id;
3393
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003394 /* Initialize rate scaling */
Frans Pop91dd6c22010-03-24 14:19:58 -07003395 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003396 sta->addr);
3397 iwl3945_rs_rate_init(priv, sta, sta_id);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003398 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003399
3400 return 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003401}
Zhu Yib481de92007-09-25 17:54:57 -07003402/*****************************************************************************
3403 *
3404 * sysfs attributes
3405 *
3406 *****************************************************************************/
3407
Samuel Ortizd08853a2009-01-23 13:45:17 -08003408#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003409
3410/*
3411 * The following adds a new attribute to the sysfs representation
3412 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3413 * used for controlling the debug level.
3414 *
3415 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003416 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003417 * The debug_level being managed using sysfs below is a per device debug
3418 * level that is used instead of the global debug level if it (the per
3419 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003420 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003421static ssize_t show_debug_level(struct device *d,
3422 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003423{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003424 struct iwl_priv *priv = dev_get_drvdata(d);
3425 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003426}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003427static ssize_t store_debug_level(struct device *d,
3428 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003429 const char *buf, size_t count)
3430{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003431 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003432 unsigned long val;
3433 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003434
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003435 ret = strict_strtoul(buf, 0, &val);
3436 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003437 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003438 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003439 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003440 if (iwl_alloc_traffic_mem(priv))
3441 IWL_ERR(priv,
3442 "Not enough memory to generate traffic log\n");
3443 }
Zhu Yib481de92007-09-25 17:54:57 -07003444 return strnlen(buf, count);
3445}
3446
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003447static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3448 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003449
Samuel Ortizd08853a2009-01-23 13:45:17 -08003450#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003451
Zhu Yib481de92007-09-25 17:54:57 -07003452static ssize_t show_temperature(struct device *d,
3453 struct device_attribute *attr, char *buf)
3454{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003455 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003456
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003457 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003458 return -EAGAIN;
3459
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003460 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003461}
3462
3463static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3464
Zhu Yib481de92007-09-25 17:54:57 -07003465static ssize_t show_tx_power(struct device *d,
3466 struct device_attribute *attr, char *buf)
3467{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003468 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003469 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003470}
3471
3472static ssize_t store_tx_power(struct device *d,
3473 struct device_attribute *attr,
3474 const char *buf, size_t count)
3475{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003476 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003477 char *p = (char *)buf;
3478 u32 val;
3479
3480 val = simple_strtoul(p, &p, 10);
3481 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003482 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003483 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003484 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003485
3486 return count;
3487}
3488
3489static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3490
3491static ssize_t show_flags(struct device *d,
3492 struct device_attribute *attr, char *buf)
3493{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003494 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003495
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003496 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003497}
3498
3499static ssize_t store_flags(struct device *d,
3500 struct device_attribute *attr,
3501 const char *buf, size_t count)
3502{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003503 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003504 u32 flags = simple_strtoul(buf, NULL, 0);
3505
3506 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003507 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003508 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003509 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003510 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003511 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003512 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003513 flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003514 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003515 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003516 }
3517 }
3518 mutex_unlock(&priv->mutex);
3519
3520 return count;
3521}
3522
3523static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3524
3525static ssize_t show_filter_flags(struct device *d,
3526 struct device_attribute *attr, char *buf)
3527{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003528 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003529
3530 return sprintf(buf, "0x%04X\n",
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003531 le32_to_cpu(priv->active_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003532}
3533
3534static ssize_t store_filter_flags(struct device *d,
3535 struct device_attribute *attr,
3536 const char *buf, size_t count)
3537{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003538 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003539 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3540
3541 mutex_lock(&priv->mutex);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003542 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003543 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003544 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003545 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003546 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003547 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003548 "0x%04X\n", filter_flags);
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003549 priv->staging_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003550 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003551 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003552 }
3553 }
3554 mutex_unlock(&priv->mutex);
3555
3556 return count;
3557}
3558
3559static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3560 store_filter_flags);
3561
Zhu Yib481de92007-09-25 17:54:57 -07003562static ssize_t show_measurement(struct device *d,
3563 struct device_attribute *attr, char *buf)
3564{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003565 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003566 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003567 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003568 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003569 unsigned long flags;
3570
3571 spin_lock_irqsave(&priv->lock, flags);
3572 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3573 spin_unlock_irqrestore(&priv->lock, flags);
3574 return 0;
3575 }
3576 memcpy(&measure_report, &priv->measure_report, size);
3577 priv->measurement_status = 0;
3578 spin_unlock_irqrestore(&priv->lock, flags);
3579
3580 while (size && (PAGE_SIZE - len)) {
3581 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3582 PAGE_SIZE - len, 1);
3583 len = strlen(buf);
3584 if (PAGE_SIZE - len)
3585 buf[len++] = '\n';
3586
3587 ofs += 16;
3588 size -= min(size, 16U);
3589 }
3590
3591 return len;
3592}
3593
3594static ssize_t store_measurement(struct device *d,
3595 struct device_attribute *attr,
3596 const char *buf, size_t count)
3597{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003598 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003599 struct ieee80211_measurement_params params = {
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003600 .channel = le16_to_cpu(priv->active_rxon.channel),
Johannes Berge99f168c2010-01-19 10:04:28 -08003601 .start_time = cpu_to_le64(priv->_3945.last_tsf),
Zhu Yib481de92007-09-25 17:54:57 -07003602 .duration = cpu_to_le16(1),
3603 };
3604 u8 type = IWL_MEASURE_BASIC;
3605 u8 buffer[32];
3606 u8 channel;
3607
3608 if (count) {
3609 char *p = buffer;
3610 strncpy(buffer, buf, min(sizeof(buffer), count));
3611 channel = simple_strtoul(p, NULL, 0);
3612 if (channel)
3613 params.channel = channel;
3614
3615 p = buffer;
3616 while (*p && *p != ' ')
3617 p++;
3618 if (*p)
3619 type = simple_strtoul(p + 1, NULL, 0);
3620 }
3621
Tomas Winklere1623442009-01-27 14:27:56 -08003622 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003623 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003624 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003625
3626 return count;
3627}
3628
3629static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3630 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003631
Zhu Yib481de92007-09-25 17:54:57 -07003632static ssize_t store_retry_rate(struct device *d,
3633 struct device_attribute *attr,
3634 const char *buf, size_t count)
3635{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003636 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003637
3638 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3639 if (priv->retry_rate <= 0)
3640 priv->retry_rate = 1;
3641
3642 return count;
3643}
3644
3645static ssize_t show_retry_rate(struct device *d,
3646 struct device_attribute *attr, char *buf)
3647{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003648 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003649 return sprintf(buf, "%d", priv->retry_rate);
3650}
3651
3652static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3653 store_retry_rate);
3654
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003655
Zhu Yib481de92007-09-25 17:54:57 -07003656static ssize_t show_channels(struct device *d,
3657 struct device_attribute *attr, char *buf)
3658{
Johannes Berg8318d782008-01-24 19:38:38 +01003659 /* all this shit doesn't belong into sysfs anyway */
3660 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003661}
3662
3663static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3664
Zhu Yib481de92007-09-25 17:54:57 -07003665static ssize_t show_antenna(struct device *d,
3666 struct device_attribute *attr, char *buf)
3667{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003668 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003669
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003670 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003671 return -EAGAIN;
3672
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003673 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003674}
3675
3676static ssize_t store_antenna(struct device *d,
3677 struct device_attribute *attr,
3678 const char *buf, size_t count)
3679{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003680 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003681 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003682
3683 if (count == 0)
3684 return 0;
3685
3686 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003687 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003688 return count;
3689 }
3690
3691 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003692 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003693 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003694 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003695 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003696
3697
3698 return count;
3699}
3700
3701static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3702
3703static ssize_t show_status(struct device *d,
3704 struct device_attribute *attr, char *buf)
3705{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003706 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003707 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003708 return -EAGAIN;
3709 return sprintf(buf, "0x%08x\n", (int)priv->status);
3710}
3711
3712static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3713
3714static ssize_t dump_error_log(struct device *d,
3715 struct device_attribute *attr,
3716 const char *buf, size_t count)
3717{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003718 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003719 char *p = (char *)buf;
3720
3721 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003722 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003723
3724 return strnlen(buf, count);
3725}
3726
3727static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3728
Zhu Yib481de92007-09-25 17:54:57 -07003729/*****************************************************************************
3730 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003731 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003732 *
3733 *****************************************************************************/
3734
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003735static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003736{
Reinette Chatred21050c2009-02-13 11:51:18 -08003737 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003738
3739 init_waitqueue_head(&priv->wait_command_queue);
3740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003741 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3742 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003743 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003744 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3745 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Johannes Bergee525d12010-01-21 06:09:28 -08003746 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02003747
3748 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003750 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003751
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003752 if (priv->cfg->ops->lib->recover_from_tx_stall) {
3753 init_timer(&priv->monitor_recover);
3754 priv->monitor_recover.data = (unsigned long)priv;
3755 priv->monitor_recover.function =
3756 priv->cfg->ops->lib->recover_from_tx_stall;
3757 }
3758
Zhu Yib481de92007-09-25 17:54:57 -07003759 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003760 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003761}
3762
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003763static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003764{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003765 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003766
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003767 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003768 cancel_delayed_work(&priv->scan_check);
3769 cancel_delayed_work(&priv->alive_start);
Abhijeet Kolekar1e460532010-04-23 14:14:53 -07003770 cancel_work_sync(&priv->start_internal_scan);
Zhu Yib481de92007-09-25 17:54:57 -07003771 cancel_work_sync(&priv->beacon_update);
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003772 if (priv->cfg->ops->lib->recover_from_tx_stall)
3773 del_timer_sync(&priv->monitor_recover);
Zhu Yib481de92007-09-25 17:54:57 -07003774}
3775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003776static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003777 &dev_attr_antenna.attr,
3778 &dev_attr_channels.attr,
3779 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003780 &dev_attr_flags.attr,
3781 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003782 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003783 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003784 &dev_attr_status.attr,
3785 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003786 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003787#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003788 &dev_attr_debug_level.attr,
3789#endif
Zhu Yib481de92007-09-25 17:54:57 -07003790 NULL
3791};
3792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003793static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003794 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003795 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003796};
3797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798static struct ieee80211_ops iwl3945_hw_ops = {
3799 .tx = iwl3945_mac_tx,
3800 .start = iwl3945_mac_start,
3801 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003802 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003803 .remove_interface = iwl_mac_remove_interface,
Abhijeet Kolekar48083682009-04-08 11:26:48 -07003804 .config = iwl_mac_config,
Samuel Ortiz8ccde882009-01-27 14:27:52 -08003805 .configure_filter = iwl_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003806 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003807 .conf_tx = iwl_mac_conf_tx,
Abhijeet Kolekarbd564262009-04-08 11:26:52 -07003808 .reset_tsf = iwl_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003809 .bss_info_changed = iwl_bss_info_changed,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003810 .hw_scan = iwl_mac_hw_scan,
3811 .sta_add = iwl3945_mac_sta_add,
3812 .sta_remove = iwl_mac_sta_remove,
Johannes Berga85d7cc2010-07-31 08:34:10 -07003813 .tx_last_beacon = iwl_mac_tx_last_beacon,
Zhu Yib481de92007-09-25 17:54:57 -07003814};
3815
Winkler, Tomase52119c2008-12-22 11:31:19 +08003816static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003817{
3818 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003819 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003820
3821 priv->retry_rate = 1;
3822 priv->ibss_beacon = NULL;
3823
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003824 spin_lock_init(&priv->sta_lock);
3825 spin_lock_init(&priv->hcmd_lock);
3826
3827 INIT_LIST_HEAD(&priv->free_frames);
3828
3829 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003830 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003831
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003832 priv->ieee_channels = NULL;
3833 priv->ieee_rates = NULL;
3834 priv->band = IEEE80211_BAND_2GHZ;
3835
3836 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003837 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003838
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003839 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003840
Samuel Ortize6148912009-01-23 13:45:15 -08003841 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3842 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3843 eeprom->version);
3844 ret = -EINVAL;
3845 goto err;
3846 }
3847 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003848 if (ret) {
3849 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3850 goto err;
3851 }
3852
Samuel Ortize6148912009-01-23 13:45:15 -08003853 /* Set up txpower settings in driver for all channels */
3854 if (iwl3945_txpower_set_from_eeprom(priv)) {
3855 ret = -EIO;
3856 goto err_free_channel_map;
3857 }
3858
Samuel Ortiz534166d2009-01-23 13:45:16 -08003859 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003860 if (ret) {
3861 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3862 goto err_free_channel_map;
3863 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003864 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3865
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003866 return 0;
3867
3868err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003869 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003870err:
3871 return ret;
3872}
3873
Johannes Bergdd7a2502010-04-28 23:33:10 -07003874#define IWL3945_MAX_PROBE_REQUEST 200
3875
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003876static int iwl3945_setup_mac(struct iwl_priv *priv)
3877{
3878 int ret;
3879 struct ieee80211_hw *hw = priv->hw;
3880
3881 hw->rate_control_algorithm = "iwl-3945-rs";
3882 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
Johannes Bergfd1af152010-04-30 11:30:43 -07003883 hw->vif_data_size = sizeof(struct iwl_vif_priv);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003884
3885 /* Tell mac80211 our characteristics */
3886 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003887 IEEE80211_HW_SPECTRUM_MGMT;
3888
3889 if (!priv->cfg->broken_powersave)
3890 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3891 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003892
3893 hw->wiphy->interface_modes =
3894 BIT(NL80211_IFTYPE_STATION) |
3895 BIT(NL80211_IFTYPE_ADHOC);
3896
Reinette Chatref6c8f152010-03-12 11:13:26 -08003897 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
Johannes Berg5be83de2009-11-19 00:56:28 +01003898 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003899
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003900 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3901 /* we create the 802.11 header and a zero-length SSID element */
Johannes Bergdd7a2502010-04-28 23:33:10 -07003902 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003903
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003904 /* Default value; 4 EDCA QOS priorities */
3905 hw->queues = 4;
3906
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003907 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3908 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3909 &priv->bands[IEEE80211_BAND_2GHZ];
3910
3911 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3912 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3913 &priv->bands[IEEE80211_BAND_5GHZ];
3914
3915 ret = ieee80211_register_hw(priv->hw);
3916 if (ret) {
3917 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3918 return ret;
3919 }
3920 priv->mac80211_registered = 1;
3921
3922 return 0;
3923}
3924
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003925static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003926{
3927 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003928 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003929 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003930 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003931 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003932 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003933
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003934 /***********************
3935 * 1. Allocating HW data
3936 * ********************/
3937
Zhu Yib481de92007-09-25 17:54:57 -07003938 /* mac80211 allocates memory for this device instance, including
3939 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003940 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003941 if (hw == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003942 pr_err("Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07003943 err = -ENOMEM;
3944 goto out;
3945 }
Zhu Yib481de92007-09-25 17:54:57 -07003946 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003947 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003948
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003949 /*
3950 * Disabling hardware scan means that mac80211 will perform scans
3951 * "the hard way", rather than using device's scan.
3952 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003953 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08003954 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003955 iwl3945_hw_ops.hw_scan = NULL;
3956 }
3957
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003958
Tomas Winklere1623442009-01-27 14:27:56 -08003959 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003960 priv->cfg = cfg;
3961 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07003962 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003963
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003964 if (iwl_alloc_traffic_mem(priv))
3965 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07003966
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003967 /***************************
3968 * 2. Initializing PCI bus
3969 * *************************/
John W. Linville1a7123c2010-08-05 14:39:31 -04003970 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3971 PCIE_LINK_STATE_CLKPM);
3972
Zhu Yib481de92007-09-25 17:54:57 -07003973 if (pci_enable_device(pdev)) {
3974 err = -ENODEV;
3975 goto out_ieee80211_free_hw;
3976 }
3977
3978 pci_set_master(pdev);
3979
Yang Hongyang284901a2009-04-06 19:01:15 -07003980 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07003981 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07003982 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07003983 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08003984 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003985 goto out_pci_disable_device;
3986 }
3987
3988 pci_set_drvdata(pdev, priv);
3989 err = pci_request_regions(pdev, DRV_NAME);
3990 if (err)
3991 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003992
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003993 /***********************
3994 * 3. Read REV Register
3995 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07003996 priv->hw_base = pci_iomap(pdev, 0, 0);
3997 if (!priv->hw_base) {
3998 err = -ENODEV;
3999 goto out_pci_release_regions;
4000 }
4001
Tomas Winklere1623442009-01-27 14:27:56 -08004002 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004003 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004004 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004005
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004006 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4007 * PCI Tx retries from interfering with C3 CPU state */
4008 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004009
Reinette Chatre731a29b2009-12-14 14:12:11 -08004010 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004011 * we should init now
4012 */
4013 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004014 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004015
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004016 /*
4017 * stop and reset the on-board processor just in case it is in a
4018 * strange state ... like being left stranded by a primary kernel
4019 * and this is now the kdump kernel trying to start up
4020 */
4021 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4022
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004023 /***********************
4024 * 4. Read EEPROM
4025 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004026
Zhu Yi5a66926a2008-01-14 17:46:18 -08004027 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004028 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004029 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004030 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004031 goto out_iounmap;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004032 }
4033 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004034 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Johannes Berg30eabc12010-05-12 03:33:10 -07004035 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
4036 SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004037
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004038 /***********************
4039 * 5. Setup HW Constants
4040 * ********************/
4041 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004042 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004043 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004044 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004045 }
4046
4047 /***********************
4048 * 6. Setup priv
4049 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004050
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004051 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004052 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004053 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004054 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004055 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004056
Tomas Winkler978785a2008-12-19 10:37:31 +08004057 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4058 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004059
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004060 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004061 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004062 * ********************/
4063
4064 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004065 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004066 spin_unlock_irqrestore(&priv->lock, flags);
4067
Helmut Schaa26635162009-01-15 09:38:44 +01004068 pci_enable_msi(priv->pci_dev);
4069
Mohamed Abbasef850d72009-05-22 11:01:50 -07004070 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
4071 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004072 if (err) {
4073 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4074 goto out_disable_msi;
4075 }
4076
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004077 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4078 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004079 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004080 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004081 }
4082
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004083 iwl_set_rxon_channel(priv,
4084 &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004085 iwl3945_setup_deferred_work(priv);
4086 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004087 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004088
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004089 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004090 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004091 * *******************************/
4092
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004093 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004094
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004095 err = iwl3945_setup_mac(priv);
4096 if (err)
4097 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004098
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004099 err = iwl_dbgfs_register(priv, DRV_NAME);
4100 if (err)
4101 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4102
Helmut Schaa26635162009-01-15 09:38:44 +01004103 /* Start monitoring the killswitch */
Johannes Bergee525d12010-01-21 06:09:28 -08004104 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01004105 2 * HZ);
4106
Zhu Yib481de92007-09-25 17:54:57 -07004107 return 0;
4108
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004109 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004110 destroy_workqueue(priv->workqueue);
4111 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004112 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4113 out_release_irq:
4114 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004115 out_disable_msi:
4116 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004117 iwlcore_free_geos(priv);
4118 iwl_free_channel_map(priv);
4119 out_unset_hw_params:
4120 iwl3945_unset_hw_params(priv);
4121 out_eeprom_free:
4122 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004123 out_iounmap:
4124 pci_iounmap(pdev, priv->hw_base);
4125 out_pci_release_regions:
4126 pci_release_regions(pdev);
4127 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004128 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004129 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004130 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004131 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004132 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004133 out:
4134 return err;
4135}
4136
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004137static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004138{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004139 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004140 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004141
4142 if (!priv)
4143 return;
4144
Tomas Winklere1623442009-01-27 14:27:56 -08004145 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004146
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004147 iwl_dbgfs_unregister(priv);
4148
Zhu Yib481de92007-09-25 17:54:57 -07004149 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004150
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004151 if (priv->mac80211_registered) {
4152 ieee80211_unregister_hw(priv->hw);
4153 priv->mac80211_registered = 0;
4154 } else {
4155 iwl3945_down(priv);
4156 }
Zhu Yib481de92007-09-25 17:54:57 -07004157
Ben Cahillc166b252009-10-23 13:42:35 -07004158 /*
4159 * Make sure device is reset to low power before unloading driver.
4160 * This may be redundant with iwl_down(), but there are paths to
4161 * run iwl_down() without calling apm_ops.stop(), and there are
4162 * paths to avoid running iwl_down() at all before leaving driver.
4163 * This (inexpensive) call *makes sure* device is reset.
4164 */
4165 priv->cfg->ops->lib->apm_ops.stop(priv);
4166
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004167 /* make sure we flush any pending irq or
4168 * tasklet for the driver
4169 */
4170 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004171 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004172 spin_unlock_irqrestore(&priv->lock, flags);
4173
4174 iwl_synchronize_irq(priv);
4175
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004176 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004177
Johannes Bergee525d12010-01-21 06:09:28 -08004178 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004179
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004180 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004181
4182 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004183 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004184 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004185
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004186 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004187
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004188 /*netif_stop_queue(dev); */
4189 flush_workqueue(priv->workqueue);
4190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004191 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004192 * priv->workqueue... so we can't take down the workqueue
4193 * until now... */
4194 destroy_workqueue(priv->workqueue);
4195 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004196 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004197
Helmut Schaa26635162009-01-15 09:38:44 +01004198 free_irq(pdev->irq, priv);
4199 pci_disable_msi(pdev);
4200
Zhu Yib481de92007-09-25 17:54:57 -07004201 pci_iounmap(pdev, priv->hw_base);
4202 pci_release_regions(pdev);
4203 pci_disable_device(pdev);
4204 pci_set_drvdata(pdev, NULL);
4205
Samuel Ortize6148912009-01-23 13:45:15 -08004206 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004207 iwlcore_free_geos(priv);
Johannes Berg811ecc92010-04-06 04:12:41 -07004208 kfree(priv->scan_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07004209 if (priv->ibss_beacon)
4210 dev_kfree_skb(priv->ibss_beacon);
4211
4212 ieee80211_free_hw(priv->hw);
4213}
4214
Zhu Yib481de92007-09-25 17:54:57 -07004215
4216/*****************************************************************************
4217 *
4218 * driver and module entry point
4219 *
4220 *****************************************************************************/
4221
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004222static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004223 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004224 .id_table = iwl3945_hw_card_ids,
4225 .probe = iwl3945_pci_probe,
4226 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004227#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004228 .suspend = iwl_pci_suspend,
4229 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004230#endif
4231};
4232
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004233static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004234{
4235
4236 int ret;
Joe Perchesc96c31e2010-07-26 14:39:58 -07004237 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
4238 pr_info(DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004239
4240 ret = iwl3945_rate_control_register();
4241 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004242 pr_err("Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004243 return ret;
4244 }
4245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004246 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004247 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004248 pr_err("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004249 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004250 }
Zhu Yib481de92007-09-25 17:54:57 -07004251
4252 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004253
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004254error_register:
4255 iwl3945_rate_control_unregister();
4256 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004257}
4258
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004259static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004260{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004261 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004262 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004263}
4264
Reinette Chatrea0987a82008-12-02 12:14:06 -08004265MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004266
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004267module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004268MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004269module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004270MODULE_PARM_DESC(swcrypto,
4271 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004272#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004273module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004274MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004275#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004276module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4277 int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004278MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004279module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004280MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4281
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004282module_exit(iwl3945_exit);
4283module_init(iwl3945_init);