blob: 02e0a9bbad16747c049f81b60f49d33df306b7e2 [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 Berg246ed352010-08-23 10:46:32 +0200320 if (!iwl_is_associated(priv, IWL_RXON_CTX_BSS) || !priv->ibss_beacon)
Zhu Yib481de92007-09-25 17:54:57 -0700321 return 0;
322
323 if (priv->ibss_beacon->len > left)
324 return 0;
325
326 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
327
328 return priv->ibss_beacon->len;
329}
330
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800331static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700332{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800333 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700334 unsigned int frame_size;
335 int rc;
336 u8 rate;
337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800338 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700339
340 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800341 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700342 "command.\n");
343 return -ENOMEM;
344 }
345
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800346 rate = iwl_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800348 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700349
Samuel Ortiz518099a2009-01-19 15:30:27 -0800350 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700351 &frame->u.cmd[0]);
352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800353 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700354
355 return rc;
356}
357
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800358static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700359{
Johannes Bergee525d12010-01-21 06:09:28 -0800360 if (priv->_3945.shared_virt)
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -0800361 dma_free_coherent(&priv->pci_dev->dev,
362 sizeof(struct iwl3945_shared),
Johannes Bergee525d12010-01-21 06:09:28 -0800363 priv->_3945.shared_virt,
364 priv->_3945.shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700365}
366
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800367static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +0200368 struct ieee80211_tx_info *info,
Johannes Bergc2acea82009-07-24 11:13:05 -0700369 struct iwl_device_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -0700370 struct sk_buff *skb_frag,
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -0800371 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700372{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700373 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Tomas Winklerc587de02009-06-03 11:44:07 -0700374 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -0700375
Johannes Berg97359d12010-08-10 09:46:38 +0200376 tx_cmd->sec_ctl = 0;
377
378 switch (keyinfo->cipher) {
379 case WLAN_CIPHER_SUITE_CCMP:
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700380 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
381 memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
Tomas Winklere1623442009-01-27 14:27:56 -0800382 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -0700383 break;
384
Johannes Berg97359d12010-08-10 09:46:38 +0200385 case WLAN_CIPHER_SUITE_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -0700386 break;
387
Johannes Berg97359d12010-08-10 09:46:38 +0200388 case WLAN_CIPHER_SUITE_WEP104:
389 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
390 /* fall through */
391 case WLAN_CIPHER_SUITE_WEP40:
392 tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +0200393 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -0700394
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700395 memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -0700396
Tomas Winklere1623442009-01-27 14:27:56 -0800397 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +0200398 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -0700399 break;
400
Zhu Yib481de92007-09-25 17:54:57 -0700401 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200402 IWL_ERR(priv, "Unknown encode cipher %x\n", keyinfo->cipher);
Zhu Yib481de92007-09-25 17:54:57 -0700403 break;
404 }
405}
406
407/*
408 * handle build REPLY_TX command notification.
409 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800410static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Johannes Bergc2acea82009-07-24 11:13:05 -0700411 struct iwl_device_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +0200412 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +0800413 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -0700414{
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700415 struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
416 __le32 tx_flags = tx_cmd->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700417 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700418
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700419 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +0200420 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700421 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700422 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700423 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700424 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -0700425 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
426 tx_flags |= TX_CMD_FLG_TSF_MSK;
427 } else {
428 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
429 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
430 }
431
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700432 tx_cmd->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700433 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -0700434 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
435
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700436 if (ieee80211_is_data_qos(fc)) {
437 u8 *qc = ieee80211_get_qos_ctl(hdr);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700438 tx_cmd->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -0700439 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800440 } else {
Zhu Yib481de92007-09-25 17:54:57 -0700441 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800442 }
Zhu Yib481de92007-09-25 17:54:57 -0700443
Johannes Berg94597ab2010-08-09 10:57:02 -0700444 priv->cfg->ops->utils->tx_cmd_protection(priv, info, fc, &tx_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700445
446 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700447 if (ieee80211_is_mgmt(fc)) {
448 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700449 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -0700450 else
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700451 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700452 } else {
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700453 tx_cmd->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700454 }
Zhu Yib481de92007-09-25 17:54:57 -0700455
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700456 tx_cmd->driver_txop = 0;
457 tx_cmd->tx_flags = tx_flags;
458 tx_cmd->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700459}
460
Zhu Yib481de92007-09-25 17:54:57 -0700461/*
462 * start REPLY_TX command process
463 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800464static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700465{
466 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200467 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Abhijeet Kolekar9744c912009-10-09 13:20:31 -0700468 struct iwl3945_tx_cmd *tx_cmd;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800469 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800470 struct iwl_queue *q = NULL;
Johannes Bergc2acea82009-07-24 11:13:05 -0700471 struct iwl_device_cmd *out_cmd;
472 struct iwl_cmd_meta *out_meta;
Zhu Yib481de92007-09-25 17:54:57 -0700473 dma_addr_t phys_addr;
474 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +0800475 int txq_id = skb_get_queue_mapping(skb);
Reinette Chatredf833b12009-04-21 10:55:48 -0700476 u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
Tomas Winkler54dbb522008-05-15 13:54:06 +0800477 u8 id;
478 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -0700479 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +0800480 u8 tid = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700481 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -0700482 u8 wait_write_ptr = 0;
483 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700484
485 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800486 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800487 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
Zhu Yib481de92007-09-25 17:54:57 -0700488 goto drop_unlock;
489 }
490
Johannes Berge039fa42008-05-15 12:55:29 +0200491 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800492 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700493 goto drop_unlock;
494 }
495
496 unicast = !is_multicast_ether_addr(hdr->addr1);
497 id = 0;
498
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700499 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -0700500
Samuel Ortizd08853a2009-01-23 13:45:17 -0800501#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700502 if (ieee80211_is_auth(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800503 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700504 else if (ieee80211_is_assoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800505 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -0700506 else if (ieee80211_is_reassoc_req(fc))
Tomas Winklere1623442009-01-27 14:27:56 -0800507 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
Zhu Yib481de92007-09-25 17:54:57 -0700508#endif
509
Zhu Yib481de92007-09-25 17:54:57 -0700510 spin_unlock_irqrestore(&priv->lock, flags);
511
Harvey Harrison7294ec92008-07-15 18:43:59 -0700512 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800513
Johannes Berg2a87c262010-04-30 11:30:45 -0700514 /* Find index into station table for destination station */
Johannes 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);
Johannes Berg246ed352010-08-23 10:46:32 +0200686 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -0700687
Johannes Berg246ed352010-08-23 10:46:32 +0200688 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700689 add_time = iwl_usecs_to_beacons(priv,
Johannes Berge99f168c2010-01-19 10:04:28 -0800690 le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
Johannes Berg246ed352010-08-23 10:46:32 +0200691 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700692
693 memset(&spectrum, 0, sizeof(spectrum));
694
695 spectrum.channel_count = cpu_to_le16(1);
696 spectrum.flags =
697 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
698 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
699 cmd.len = sizeof(spectrum);
700 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
701
Johannes Berg246ed352010-08-23 10:46:32 +0200702 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS))
Zhu Yib481de92007-09-25 17:54:57 -0700703 spectrum.start_time =
Wey-Yi Guya0ee74c2010-05-06 08:54:10 -0700704 iwl_add_beacon_time(priv,
705 priv->_3945.last_beacon_time, add_time,
Johannes Berg246ed352010-08-23 10:46:32 +0200706 le16_to_cpu(ctx->timing.beacon_interval));
Zhu Yib481de92007-09-25 17:54:57 -0700707 else
708 spectrum.start_time = 0;
709
710 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
711 spectrum.channels[0].channel = params->channel;
712 spectrum.channels[0].type = type;
Johannes Berg246ed352010-08-23 10:46:32 +0200713 if (ctx->active.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700714 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
715 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
716
Samuel Ortiz518099a2009-01-19 15:30:27 -0800717 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700718 if (rc)
719 return rc;
720
Zhu Yi2f301222009-10-09 17:19:45 +0800721 pkt = (struct iwl_rx_packet *)cmd.reply_page;
722 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800723 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700724 rc = -EIO;
725 }
726
Zhu Yi2f301222009-10-09 17:19:45 +0800727 spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
Zhu Yib481de92007-09-25 17:54:57 -0700728 switch (spectrum_resp_status) {
729 case 0: /* Command will be handled */
Zhu Yi2f301222009-10-09 17:19:45 +0800730 if (pkt->u.spectrum.id != 0xff) {
Tomas Winklere1623442009-01-27 14:27:56 -0800731 IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
Zhu Yi2f301222009-10-09 17:19:45 +0800732 pkt->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -0700733 priv->measurement_status &= ~MEASUREMENT_READY;
734 }
735 priv->measurement_status |= MEASUREMENT_ACTIVE;
736 rc = 0;
737 break;
738
739 case 1: /* Command will not be handled */
740 rc = -EAGAIN;
741 break;
742 }
743
Zhu Yi64a76b52009-12-10 14:37:21 -0800744 iwl_free_pages(priv, cmd.reply_page);
Zhu Yib481de92007-09-25 17:54:57 -0700745
746 return rc;
747}
Zhu Yib481de92007-09-25 17:54:57 -0700748
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800749static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800750 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700751{
Zhu Yi2f301222009-10-09 17:19:45 +0800752 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800753 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700754 struct delayed_work *pwork;
755
756 palive = &pkt->u.alive_frame;
757
Tomas Winklere1623442009-01-27 14:27:56 -0800758 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700759 "0x%01X 0x%01X\n",
760 palive->is_valid, palive->ver_type,
761 palive->ver_subtype);
762
763 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800764 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800765 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
766 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700767 pwork = &priv->init_alive_start;
768 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800769 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700770 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800771 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700772 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800773 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700774 }
775
776 /* We delay the ALIVE response by 5ms to
777 * give the HW RF Kill time to activate... */
778 if (palive->is_valid == UCODE_VALID_OK)
779 queue_delayed_work(priv->workqueue, pwork,
780 msecs_to_jiffies(5));
781 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800782 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700783}
784
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800785static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800786 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700787{
Helmut Schaac7e035a2009-01-19 13:02:15 +0100788#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800789 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Helmut Schaac7e035a2009-01-19 13:02:15 +0100790#endif
Zhu Yib481de92007-09-25 17:54:57 -0700791
Tomas Winklere1623442009-01-27 14:27:56 -0800792 IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
Zhu Yib481de92007-09-25 17:54:57 -0700793}
794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800795static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700796{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800797 struct iwl_priv *priv =
798 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700799 struct sk_buff *beacon;
800
801 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200802 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700803
804 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800805 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700806 return;
807 }
808
809 mutex_lock(&priv->mutex);
810 /* new beacon skb is allocated every time; dispose previous.*/
811 if (priv->ibss_beacon)
812 dev_kfree_skb(priv->ibss_beacon);
813
814 priv->ibss_beacon = beacon;
815 mutex_unlock(&priv->mutex);
816
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800817 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700818}
819
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800820static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800821 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700822{
Zhu Yi2f301222009-10-09 17:19:45 +0800823 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800824 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Johannes Berga85d7cc2010-07-31 08:34:10 -0700825#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -0700826 u8 rate = beacon->beacon_notify_hdr.rate;
827
Tomas Winklere1623442009-01-27 14:27:56 -0800828 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700829 "tsf %d %d rate %d\n",
830 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
831 beacon->beacon_notify_hdr.failure_frame,
832 le32_to_cpu(beacon->ibss_mgr_status),
833 le32_to_cpu(beacon->high_tsf),
834 le32_to_cpu(beacon->low_tsf), rate);
835#endif
836
Johannes Berga85d7cc2010-07-31 08:34:10 -0700837 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
838
Johannes Berg05c914f2008-09-11 00:01:58 +0200839 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700840 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
841 queue_work(priv->workqueue, &priv->beacon_update);
842}
843
Zhu Yib481de92007-09-25 17:54:57 -0700844/* Handle notification from uCode that card's power state is changing
845 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800846static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800847 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700848{
Zhu Yi2f301222009-10-09 17:19:45 +0800849 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700850 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
851 unsigned long status = priv->status;
852
Reinette Chatre4c423a22009-07-17 09:30:26 -0700853 IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700854 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
855 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
856
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +0800857 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700858 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
859
860 if (flags & HW_CARD_DISABLED)
861 set_bit(STATUS_RF_KILL_HW, &priv->status);
862 else
863 clear_bit(STATUS_RF_KILL_HW, &priv->status);
864
865
Winkler, Tomasaf0053d2009-01-19 15:30:23 -0800866 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700867
868 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200869 test_bit(STATUS_RF_KILL_HW, &priv->status)))
870 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
871 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700872 else
873 wake_up_interruptible(&priv->wait_command_queue);
874}
875
876/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800877 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700878 *
879 * Setup the RX handlers for each of the reply types sent from the uCode
880 * to the host.
881 *
882 * This function chains into the hardware specific files for them to setup
883 * any hardware specific handlers as well.
884 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800885static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700886{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800887 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
888 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
Abhijeet Kolekar261b9c32009-02-18 15:54:29 -0800889 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800890 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Reinette Chatre81963d62010-01-22 14:22:57 -0800891 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
892 iwl_rx_spectrum_measure_notif;
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800893 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700894 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Abhijeet Kolekar030f05e2009-02-18 15:54:28 -0800895 iwl_rx_pm_debug_statistics_notif;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800896 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700897
Ben Cahill9fbab512007-11-29 11:09:47 +0800898 /*
899 * The same handler is used for both the REPLY to a discrete
900 * statistics request from the host as well as for the periodic
901 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700902 */
Abhijeet Kolekar17f36fc2010-04-16 10:03:54 -0700903 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800904 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -0700905
Abhijeet Kolekarcade0eb2009-02-18 15:54:26 -0800906 iwl_setup_rx_scan_handlers(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800907 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700908
Ben Cahill9fbab512007-11-29 11:09:47 +0800909 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800910 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700911}
912
Zhu Yib481de92007-09-25 17:54:57 -0700913/************************** RX-FUNCTIONS ****************************/
914/*
915 * Rx theory of operation
916 *
917 * The host allocates 32 DMA target addresses and passes the host address
918 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
919 * 0 to 31
920 *
921 * Rx Queue Indexes
922 * The host/firmware share two index registers for managing the Rx buffers.
923 *
924 * The READ index maps to the first position that the firmware may be writing
925 * to -- the driver can read up to (but not including) this position and get
926 * good data.
927 * The READ index is managed by the firmware once the card is enabled.
928 *
929 * The WRITE index maps to the last position the driver has read from -- the
930 * position preceding WRITE is the last slot the firmware can place a packet.
931 *
932 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
933 * WRITE = READ.
934 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800935 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -0700936 * INDEX position, and WRITE to the last (READ - 1 wrapped)
937 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800938 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -0700939 * and fire the RX interrupt. The driver can then query the READ index and
940 * process as many packets as possible, moving the WRITE index forward as it
941 * resets the Rx queue buffers with new memory.
942 *
943 * The management in the driver is as follows:
944 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
945 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +0800946 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800947 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -0700948 * iwl->rxq is replenished and the READ INDEX is updated (updating the
949 * 'processed' and 'read' driver indexes as well)
950 * + A received packet is processed and handed to the kernel network stack,
951 * detached from the iwl->rxq. The driver 'processed' index is updated.
952 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
953 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
954 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
955 * were enough free buffers and RX_STALLED is set it is cleared.
956 *
957 *
958 * Driver sequence:
959 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800960 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800961 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +0800962 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -0700963 * queue, updates firmware pointers, and updates
964 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800965 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -0700966 *
967 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +0800968 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -0700969 * READ INDEX, detaching the SKB from the pool.
970 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800971 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -0700972 * slots.
973 * ...
974 *
975 */
976
977/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800978 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -0700979 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800980static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700981 dma_addr_t dma_addr)
982{
983 return cpu_to_le32((u32)dma_addr);
984}
985
986/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800987 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -0700988 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800989 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -0700990 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +0800991 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -0700992 *
993 * This moves the 'write' index forward to catch up with 'processed', and
994 * also updates the memory address in the firmware to reference the new
995 * target buffer.
996 */
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800997static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700998{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +0800999 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001000 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001001 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07001002 unsigned long flags;
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001003 int write;
Zhu Yib481de92007-09-25 17:54:57 -07001004
1005 spin_lock_irqsave(&rxq->lock, flags);
1006 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08001007 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001008 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07001009 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001010 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07001011 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001012
1013 /* Point to Rx buffer via next RBD in circular buffer */
Zhu Yi2f301222009-10-09 17:19:45 +08001014 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
Zhu Yib481de92007-09-25 17:54:57 -07001015 rxq->queue[rxq->write] = rxb;
1016 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1017 rxq->free_count--;
1018 }
1019 spin_unlock_irqrestore(&rxq->lock, flags);
1020 /* If the pre-allocated buffer pool is dropping low, schedule to
1021 * refill it */
1022 if (rxq->free_count <= RX_LOW_WATERMARK)
1023 queue_work(priv->workqueue, &priv->rx_replenish);
1024
1025
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001026 /* If we've added more space for the firmware to place data, tell it.
1027 * Increment device's write pointer in multiples of 8. */
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001028 if ((rxq->write_actual != (rxq->write & ~0x7))
Zhu Yib481de92007-09-25 17:54:57 -07001029 || (abs(rxq->write - rxq->read) > 7)) {
1030 spin_lock_irqsave(&rxq->lock, flags);
1031 rxq->need_update = 1;
1032 spin_unlock_irqrestore(&rxq->lock, flags);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001033 iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07001034 }
Zhu Yib481de92007-09-25 17:54:57 -07001035}
1036
1037/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001038 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07001039 *
1040 * When moving to rx_free an SKB is allocated for the slot.
1041 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001042 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08001043 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07001044 */
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001045static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
Zhu Yib481de92007-09-25 17:54:57 -07001046{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001047 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001048 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001049 struct iwl_rx_mem_buffer *rxb;
Zhu Yi2f301222009-10-09 17:19:45 +08001050 struct page *page;
Zhu Yib481de92007-09-25 17:54:57 -07001051 unsigned long flags;
Zhu Yi29b1b262009-10-23 13:42:25 -07001052 gfp_t gfp_mask = priority;
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001053
1054 while (1) {
1055 spin_lock_irqsave(&rxq->lock, flags);
1056
1057 if (list_empty(&rxq->rx_used)) {
1058 spin_unlock_irqrestore(&rxq->lock, flags);
1059 return;
1060 }
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001061 spin_unlock_irqrestore(&rxq->lock, flags);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001062
Reinette Chatref82a9242009-09-17 10:43:56 -07001063 if (rxq->free_count > RX_LOW_WATERMARK)
Zhu Yi29b1b262009-10-23 13:42:25 -07001064 gfp_mask |= __GFP_NOWARN;
Zhu Yi2f301222009-10-09 17:19:45 +08001065
1066 if (priv->hw_params.rx_page_order > 0)
Zhu Yi29b1b262009-10-23 13:42:25 -07001067 gfp_mask |= __GFP_COMP;
Zhu Yi2f301222009-10-09 17:19:45 +08001068
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001069 /* Alloc a new receive buffer */
Zhu Yi29b1b262009-10-23 13:42:25 -07001070 page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
Zhu Yi2f301222009-10-09 17:19:45 +08001071 if (!page) {
Zhu Yib481de92007-09-25 17:54:57 -07001072 if (net_ratelimit())
Reinette Chatref82a9242009-09-17 10:43:56 -07001073 IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
1074 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
1075 net_ratelimit())
1076 IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
1077 priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
1078 rxq->free_count);
Zhu Yib481de92007-09-25 17:54:57 -07001079 /* We don't reschedule replenish work here -- we will
1080 * call the restock method and if it still needs
1081 * more buffers it will schedule replenish */
1082 break;
1083 }
Zhu Yi12342c42007-12-20 11:27:32 +08001084
Reinette Chatrede0bd502009-09-11 10:38:12 -07001085 spin_lock_irqsave(&rxq->lock, flags);
1086 if (list_empty(&rxq->rx_used)) {
1087 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001088 __free_pages(page, priv->hw_params.rx_page_order);
Reinette Chatrede0bd502009-09-11 10:38:12 -07001089 return;
1090 }
1091 element = rxq->rx_used.next;
1092 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1093 list_del(element);
1094 spin_unlock_irqrestore(&rxq->lock, flags);
1095
Zhu Yi2f301222009-10-09 17:19:45 +08001096 rxb->page = page;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001097 /* Get physical address of RB/SKB */
Zhu Yi2f301222009-10-09 17:19:45 +08001098 rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
1099 PAGE_SIZE << priv->hw_params.rx_page_order,
1100 PCI_DMA_FROMDEVICE);
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001101
1102 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi2f301222009-10-09 17:19:45 +08001103
Zhu Yib481de92007-09-25 17:54:57 -07001104 list_add_tail(&rxb->list, &rxq->rx_free);
1105 rxq->free_count++;
Zhu Yi2f301222009-10-09 17:19:45 +08001106 priv->alloc_rxb_page++;
1107
Abhijeet Kolekar72240492009-04-30 13:56:26 -07001108 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07001109 }
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001110}
1111
Reinette Chatredf833b12009-04-21 10:55:48 -07001112void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1113{
1114 unsigned long flags;
1115 int i;
1116 spin_lock_irqsave(&rxq->lock, flags);
1117 INIT_LIST_HEAD(&rxq->rx_free);
1118 INIT_LIST_HEAD(&rxq->rx_used);
1119 /* Fill the rx_used queue with _all_ of the Rx buffers */
1120 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
1121 /* In the reset function, these buffers may have been allocated
1122 * to an SKB, so we need to unmap and free potential storage */
Zhu Yi2f301222009-10-09 17:19:45 +08001123 if (rxq->pool[i].page != NULL) {
1124 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1125 PAGE_SIZE << priv->hw_params.rx_page_order,
1126 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001127 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001128 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001129 }
1130 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
1131 }
1132
1133 /* Set us so that we have processed and used all buffers, but have
1134 * not restocked the Rx queue with fresh buffers */
1135 rxq->read = rxq->write = 0;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001136 rxq->write_actual = 0;
Zhu Yi2f301222009-10-09 17:19:45 +08001137 rxq->free_count = 0;
Reinette Chatredf833b12009-04-21 10:55:48 -07001138 spin_unlock_irqrestore(&rxq->lock, flags);
1139}
Reinette Chatredf833b12009-04-21 10:55:48 -07001140
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001141void iwl3945_rx_replenish(void *data)
1142{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001143 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001144 unsigned long flags;
1145
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001146 iwl3945_rx_allocate(priv, GFP_KERNEL);
Zhu Yib481de92007-09-25 17:54:57 -07001147
1148 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001149 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001150 spin_unlock_irqrestore(&priv->lock, flags);
1151}
1152
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001153static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
1154{
1155 iwl3945_rx_allocate(priv, GFP_ATOMIC);
1156
1157 iwl3945_rx_queue_restock(priv);
1158}
1159
1160
Reinette Chatredf833b12009-04-21 10:55:48 -07001161/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
1162 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
1163 * This free routine walks the list of POOL entries and if SKB is set to
1164 * non NULL it is unmapped and freed
1165 */
1166static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
1167{
1168 int i;
1169 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
Zhu Yi2f301222009-10-09 17:19:45 +08001170 if (rxq->pool[i].page != NULL) {
1171 pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
1172 PAGE_SIZE << priv->hw_params.rx_page_order,
1173 PCI_DMA_FROMDEVICE);
Zhu Yi64a76b52009-12-10 14:37:21 -08001174 __iwl_free_pages(priv, rxq->pool[i].page);
Zhu Yi2f301222009-10-09 17:19:45 +08001175 rxq->pool[i].page = NULL;
Reinette Chatredf833b12009-04-21 10:55:48 -07001176 }
1177 }
1178
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001179 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
Emmanuel Grumbachd5b25c92010-06-07 13:21:46 -07001180 rxq->bd_dma);
Stanislaw Gruszkaf36d04a2010-02-10 05:07:45 -08001181 dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
1182 rxq->rb_stts, rxq->rb_stts_dma);
Reinette Chatredf833b12009-04-21 10:55:48 -07001183 rxq->bd = NULL;
1184 rxq->rb_stts = NULL;
1185}
Reinette Chatredf833b12009-04-21 10:55:48 -07001186
1187
Zhu Yib481de92007-09-25 17:54:57 -07001188/* Convert linear signal-to-noise ratio into dB */
1189static u8 ratio2dB[100] = {
1190/* 0 1 2 3 4 5 6 7 8 9 */
1191 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1192 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1193 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1194 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1195 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1196 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1197 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1198 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1199 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1200 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
1201};
1202
1203/* Calculates a relative dB value from a ratio of linear
1204 * (i.e. not dB) signal levels.
1205 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001206int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07001207{
Adrian Bunk221c80c2008-02-02 23:19:01 +02001208 /* 1000:1 or higher just report as 60 dB */
1209 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07001210 return 60;
1211
Adrian Bunk221c80c2008-02-02 23:19:01 +02001212 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07001213 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02001214 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001215 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07001216
1217 /* We shouldn't see this */
1218 if (sig_ratio < 1)
1219 return 0;
1220
1221 /* Use table for ratios 1:1 - 99:1 */
1222 return (int)ratio2dB[sig_ratio];
1223}
1224
Zhu Yib481de92007-09-25 17:54:57 -07001225/**
Ben Cahill9fbab512007-11-29 11:09:47 +08001226 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07001227 *
1228 * Uses the priv->rx_handlers callback function array to invoke
1229 * the appropriate handlers, including command responses,
1230 * frame-received notifications, and other notifications.
1231 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001232static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001233{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001234 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001235 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08001236 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07001237 u32 r, i;
1238 int reclaim;
1239 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001240 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08001241 u32 count = 8;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001242 int total_empty = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001243
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001244 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1245 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08001246 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07001247 i = rxq->read;
1248
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001249 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -07001250 total_empty = r - rxq->write_actual;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001251 if (total_empty < 0)
1252 total_empty += RX_QUEUE_SIZE;
1253
1254 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001255 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001256 /* Rx interrupt, but nothing sent from uCode */
1257 if (i == r)
Reinette Chatreaf472a952009-08-28 09:58:50 -07001258 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07001259
1260 while (i != r) {
Johannes Bergf4989d92010-05-28 04:08:30 -07001261 int len;
1262
Zhu Yib481de92007-09-25 17:54:57 -07001263 rxb = rxq->queue[i];
1264
Ben Cahill9fbab512007-11-29 11:09:47 +08001265 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07001266 * then a bug has been introduced in the queue refilling
1267 * routines -- catch it here */
1268 BUG_ON(rxb == NULL);
1269
1270 rxq->queue[i] = NULL;
1271
Zhu Yi2f301222009-10-09 17:19:45 +08001272 pci_unmap_page(priv->pci_dev, rxb->page_dma,
1273 PAGE_SIZE << priv->hw_params.rx_page_order,
1274 PCI_DMA_FROMDEVICE);
1275 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001276
Johannes Bergf4989d92010-05-28 04:08:30 -07001277 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1278 len += sizeof(u32); /* account for status word */
1279 trace_iwlwifi_dev_rx(priv, pkt, len);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001280
Zhu Yib481de92007-09-25 17:54:57 -07001281 /* Reclaim a command buffer only if this packet is a response
1282 * to a (driver-originated) command.
1283 * If the packet (e.g. Rx frame) originated from uCode,
1284 * there is no command buffer to reclaim.
1285 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1286 * but apparently a few don't get set; catch them here. */
1287 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1288 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1289 (pkt->hdr.cmd != REPLY_TX);
1290
1291 /* Based on type of command response or notification,
1292 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001293 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07001294 if (priv->rx_handlers[pkt->hdr.cmd]) {
Reinette Chatreaf472a952009-08-28 09:58:50 -07001295 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
Zhu Yib481de92007-09-25 17:54:57 -07001296 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001297 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -07001298 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001299 } else {
1300 /* No handling needed */
Zhu Yi2f301222009-10-09 17:19:45 +08001301 IWL_DEBUG_RX(priv,
1302 "r %d i %d No handler needed for %s, 0x%02x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001303 r, i, get_cmd_string(pkt->hdr.cmd),
1304 pkt->hdr.cmd);
1305 }
1306
Zhu Yi29b1b262009-10-23 13:42:25 -07001307 /*
1308 * XXX: After here, we should always check rxb->page
1309 * against NULL before touching it or its virtual
1310 * memory (pkt). Because some rx_handler might have
1311 * already taken or freed the pages.
1312 */
1313
Zhu Yib481de92007-09-25 17:54:57 -07001314 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001315 /* Invoke any callbacks, transfer the buffer to caller,
1316 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001317 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001318 if (rxb->page)
Abhijeet Kolekar732587a2009-03-11 11:17:57 -07001319 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001320 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001321 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001322 }
1323
Zhu Yi73005152009-10-23 13:42:32 -07001324 /* Reuse the page if possible. For notification packets and
1325 * SKBs that fail to Rx correctly, add them back into the
1326 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001327 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001328 if (rxb->page != NULL) {
1329 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1330 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1331 PCI_DMA_FROMDEVICE);
1332 list_add_tail(&rxb->list, &rxq->rx_free);
1333 rxq->free_count++;
1334 } else
1335 list_add_tail(&rxb->list, &rxq->rx_used);
1336
Zhu Yib481de92007-09-25 17:54:57 -07001337 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001338
Zhu Yib481de92007-09-25 17:54:57 -07001339 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001340 /* If there are a lot of unused frames,
1341 * restock the Rx queue so ucode won't assert. */
1342 if (fill_rx) {
1343 count++;
1344 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001345 rxq->read = i;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001346 iwl3945_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001347 count = 0;
1348 }
1349 }
Zhu Yib481de92007-09-25 17:54:57 -07001350 }
1351
1352 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001353 rxq->read = i;
Abhijeet Kolekard14d44402009-06-03 11:44:08 -07001354 if (fill_rx)
1355 iwl3945_rx_replenish_now(priv);
1356 else
1357 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001358}
1359
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001360/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001361static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001362{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001363 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001364 synchronize_irq(priv->pci_dev->irq);
1365 tasklet_kill(&priv->irq_tasklet);
1366}
1367
Zhu Yib481de92007-09-25 17:54:57 -07001368static const char *desc_lookup(int i)
1369{
1370 switch (i) {
1371 case 1:
1372 return "FAIL";
1373 case 2:
1374 return "BAD_PARAM";
1375 case 3:
1376 return "BAD_CHECKSUM";
1377 case 4:
1378 return "NMI_INTERRUPT";
1379 case 5:
1380 return "SYSASSERT";
1381 case 6:
1382 return "FATAL_ERROR";
1383 }
1384
1385 return "UNKNOWN";
1386}
1387
1388#define ERROR_START_OFFSET (1 * sizeof(u32))
1389#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1390
Reinette Chatreb7a79402009-09-25 14:24:23 -07001391void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001392{
1393 u32 i;
1394 u32 desc, time, count, base, data1;
1395 u32 blink1, blink2, ilink1, ilink2;
Zhu Yib481de92007-09-25 17:54:57 -07001396
1397 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1398
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001399 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001400 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07001401 return;
1402 }
1403
Zhu Yib481de92007-09-25 17:54:57 -07001404
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001405 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07001406
1407 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001408 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1409 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1410 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07001411 }
1412
Winkler, Tomas15b16872008-12-19 10:37:33 +08001413 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07001414 "ilink1 nmiPC Line\n");
1415 for (i = ERROR_START_OFFSET;
1416 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1417 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001418 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07001419 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001420 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001421 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001422 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001423 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001424 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001425 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001426 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001427 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001428 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001429 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001430 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07001431
Winkler, Tomas15b16872008-12-19 10:37:33 +08001432 IWL_ERR(priv,
Jay Sternberg87563712010-06-14 14:40:40 -07001433 "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
Winkler, Tomas15b16872008-12-19 10:37:33 +08001434 desc_lookup(desc), desc, time, blink1, blink2,
1435 ilink1, ilink2, data1);
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001436 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, 0,
1437 0, blink1, blink2, ilink1, ilink2);
Zhu Yib481de92007-09-25 17:54:57 -07001438 }
Zhu Yib481de92007-09-25 17:54:57 -07001439}
1440
Ben Cahillf58177b2007-11-29 11:09:43 +08001441#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07001442
1443/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001444 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07001445 *
Zhu Yib481de92007-09-25 17:54:57 -07001446 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001447static int iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1448 u32 num_events, u32 mode,
1449 int pos, char **buf, size_t bufsz)
Zhu Yib481de92007-09-25 17:54:57 -07001450{
1451 u32 i;
1452 u32 base; /* SRAM byte address of event log header */
1453 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1454 u32 ptr; /* SRAM byte address of log data */
1455 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001456 unsigned long reg_flags;
Zhu Yib481de92007-09-25 17:54:57 -07001457
1458 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001459 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001460
1461 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1462
1463 if (mode == 0)
1464 event_size = 2 * sizeof(u32);
1465 else
1466 event_size = 3 * sizeof(u32);
1467
1468 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1469
Ben Cahille5854472009-11-06 14:52:58 -08001470 /* Make sure device is powered up for SRAM reads */
1471 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1472 iwl_grab_nic_access(priv);
1473
1474 /* Set starting address; reads will auto-increment */
1475 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1476 rmb();
1477
Zhu Yib481de92007-09-25 17:54:57 -07001478 /* "time" is actually "data" for mode 0 (no timestamp).
1479 * place event id # at far right for easier visual parsing. */
1480 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001481 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1482 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001483 if (mode == 0) {
1484 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001485 if (bufsz) {
1486 pos += scnprintf(*buf + pos, bufsz - pos,
1487 "0x%08x:%04u\n",
1488 time, ev);
1489 } else {
1490 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
1491 trace_iwlwifi_dev_ucode_event(priv, 0,
1492 time, ev);
1493 }
Winkler, Tomas15b16872008-12-19 10:37:33 +08001494 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001495 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001496 if (bufsz) {
1497 pos += scnprintf(*buf + pos, bufsz - pos,
1498 "%010u:0x%08x:%04u\n",
1499 time, data, ev);
1500 } else {
1501 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n",
1502 time, data, ev);
1503 trace_iwlwifi_dev_ucode_event(priv, time,
1504 data, ev);
1505 }
Zhu Yib481de92007-09-25 17:54:57 -07001506 }
1507 }
Ben Cahille5854472009-11-06 14:52:58 -08001508
1509 /* Allow device to power down */
1510 iwl_release_nic_access(priv);
1511 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001512 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001513}
1514
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001515/**
1516 * iwl3945_print_last_event_logs - Dump the newest # of event log to syslog
1517 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001518static int iwl3945_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001519 u32 num_wraps, u32 next_entry,
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001520 u32 size, u32 mode,
1521 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001522{
1523 /*
1524 * display the newest DEFAULT_LOG_ENTRIES entries
1525 * i.e the entries just before the next ont that uCode would fill.
1526 */
1527 if (num_wraps) {
1528 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001529 pos = iwl3945_print_event_log(priv,
1530 capacity - (size - next_entry),
1531 size - next_entry, mode,
1532 pos, buf, bufsz);
1533 pos = iwl3945_print_event_log(priv, 0,
1534 next_entry, mode,
1535 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001536 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001537 pos = iwl3945_print_event_log(priv, next_entry - size,
1538 size, mode,
1539 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001540 } else {
1541 if (next_entry < size)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001542 pos = iwl3945_print_event_log(priv, 0,
1543 next_entry, mode,
1544 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001545 else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001546 pos = iwl3945_print_event_log(priv, next_entry - size,
1547 size, mode,
1548 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001549 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001550 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001551}
1552
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001553#define DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES (20)
1554
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001555int iwl3945_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1556 char **buf, bool display)
Zhu Yib481de92007-09-25 17:54:57 -07001557{
Zhu Yib481de92007-09-25 17:54:57 -07001558 u32 base; /* SRAM byte address of event log header */
1559 u32 capacity; /* event log capacity in # entries */
1560 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1561 u32 num_wraps; /* # times uCode wrapped to top of log */
1562 u32 next_entry; /* index of next entry to be written by uCode */
1563 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001564 int pos = 0;
1565 size_t bufsz = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001566
1567 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001568 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001569 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001570 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001571 }
1572
Zhu Yib481de92007-09-25 17:54:57 -07001573 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001574 capacity = iwl_read_targ_mem(priv, base);
1575 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1576 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1577 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07001578
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001579 if (capacity > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001580 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001581 capacity, priv->cfg->max_event_log_size);
1582 capacity = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001583 }
1584
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001585 if (next_entry > priv->cfg->max_event_log_size) {
Ben Cahill84c40692009-11-06 14:52:57 -08001586 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
Wey-Yi Guy678b3852010-03-26 12:54:37 -07001587 next_entry, priv->cfg->max_event_log_size);
1588 next_entry = priv->cfg->max_event_log_size;
Ben Cahill84c40692009-11-06 14:52:57 -08001589 }
1590
Zhu Yib481de92007-09-25 17:54:57 -07001591 size = num_wraps ? capacity : next_entry;
1592
1593 /* bail out if nothing in log */
1594 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001595 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001596 return pos;
Zhu Yib481de92007-09-25 17:54:57 -07001597 }
1598
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001599#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001600 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001601 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1602 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1603#else
1604 size = (size > DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES)
1605 ? DEFAULT_IWL3945_DUMP_EVENT_LOG_ENTRIES : size;
1606#endif
1607
1608 IWL_ERR(priv, "Start IWL Event Log Dump: display last %d count\n",
1609 size);
Zhu Yib481de92007-09-25 17:54:57 -07001610
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001611#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001612 if (display) {
1613 if (full_log)
1614 bufsz = capacity * 48;
1615 else
1616 bufsz = size * 48;
1617 *buf = kmalloc(bufsz, GFP_KERNEL);
1618 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001619 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001620 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001621 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
1622 /* if uCode has wrapped back to top of log,
1623 * start at the oldest entry,
1624 * i.e the next one that uCode would fill.
1625 */
1626 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001627 pos = iwl3945_print_event_log(priv, next_entry,
1628 capacity - next_entry, mode,
1629 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001630
1631 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001632 pos = iwl3945_print_event_log(priv, 0, next_entry, mode,
1633 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001634 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001635 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1636 next_entry, size, mode,
1637 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001638#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001639 pos = iwl3945_print_last_event_logs(priv, capacity, num_wraps,
1640 next_entry, size, mode,
1641 pos, buf, bufsz);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001642#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001643 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001644}
1645
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001646static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001647{
1648 u32 inta, handled = 0;
1649 u32 inta_fh;
1650 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08001651#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001652 u32 inta_mask;
1653#endif
1654
1655 spin_lock_irqsave(&priv->lock, flags);
1656
1657 /* Ack/clear/reset pending uCode interrupts.
1658 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1659 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001660 inta = iwl_read32(priv, CSR_INT);
1661 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001662
1663 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1664 * Any new interrupts that happen after this, either while we're
1665 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001666 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1667 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001668
Samuel Ortizd08853a2009-01-23 13:45:17 -08001669#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001670 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001671 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001672 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001673 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001674 inta, inta_mask, inta_fh);
1675 }
1676#endif
1677
Zhu Yi2f301222009-10-09 17:19:45 +08001678 spin_unlock_irqrestore(&priv->lock, flags);
1679
Zhu Yib481de92007-09-25 17:54:57 -07001680 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1681 * atomic, make sure that inta covers all the interrupts that
1682 * we've discovered, even if FH interrupt came in just after
1683 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001684 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001685 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001686 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001687 inta |= CSR_INT_BIT_FH_TX;
1688
1689 /* Now service all interrupt bits discovered above. */
1690 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001691 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001692
1693 /* Tell the device to stop sending interrupts */
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001694 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001695
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001696 priv->isr_stats.hw++;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001697 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001698
1699 handled |= CSR_INT_BIT_HW_ERR;
1700
Zhu Yib481de92007-09-25 17:54:57 -07001701 return;
1702 }
1703
Samuel Ortizd08853a2009-01-23 13:45:17 -08001704#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001705 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001706 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001707 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001708 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001709 "the frame/frames.\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001710 priv->isr_stats.sch++;
1711 }
Zhu Yib481de92007-09-25 17:54:57 -07001712
1713 /* Alive notification via Rx interrupt will do the real work */
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001714 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001715 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001716 priv->isr_stats.alive++;
1717 }
Zhu Yib481de92007-09-25 17:54:57 -07001718 }
1719#endif
1720 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001721 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001722
Zhu Yib481de92007-09-25 17:54:57 -07001723 /* Error detected by uCode */
1724 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001725 IWL_ERR(priv, "Microcode SW error detected. "
1726 "Restarting 0x%X.\n", inta);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001727 priv->isr_stats.sw++;
1728 priv->isr_stats.sw_err = inta;
Samuel Ortiz8ccde882009-01-27 14:27:52 -08001729 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001730 handled |= CSR_INT_BIT_SW_ERR;
1731 }
1732
1733 /* uCode wakes up after power-down sleep */
1734 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001735 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08001736 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001737 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1738 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1739 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1740 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1741 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1742 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001743
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001744 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001745 handled |= CSR_INT_BIT_WAKEUP;
1746 }
1747
1748 /* All uCode command responses, including Tx command responses,
1749 * Rx "responses" (frame-received notification), and other
1750 * notifications from uCode come through here*/
1751 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001752 iwl3945_rx_handle(priv);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001753 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001754 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1755 }
1756
1757 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001758 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001759 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001760
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001761 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07001762 iwl_write_direct32(priv, FH39_TCSR_CREDIT
1763 (FH39_SRVC_CHNL), 0x0);
Zhu Yib481de92007-09-25 17:54:57 -07001764 handled |= CSR_INT_BIT_FH_TX;
1765 }
1766
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001767 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001768 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Abhijeet Kolekar86ddbf62009-04-20 14:36:59 -07001769 priv->isr_stats.unhandled++;
1770 }
Zhu Yib481de92007-09-25 17:54:57 -07001771
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001772 if (inta & ~priv->inta_mask) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001773 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001774 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001775 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001776 }
1777
1778 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001779 /* only Re-enable if disabled by irq */
1780 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08001781 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001782
Samuel Ortizd08853a2009-01-23 13:45:17 -08001783#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001784 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001785 inta = iwl_read32(priv, CSR_INT);
1786 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1787 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001788 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001789 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1790 }
1791#endif
Zhu Yib481de92007-09-25 17:54:57 -07001792}
1793
Abhijeet Kolekar14023642010-06-02 21:15:10 -07001794static int iwl3945_get_single_channel_for_scan(struct iwl_priv *priv,
1795 struct ieee80211_vif *vif,
1796 enum ieee80211_band band,
1797 struct iwl3945_scan_channel *scan_ch)
1798{
1799 const struct ieee80211_supported_band *sband;
1800 u16 passive_dwell = 0;
1801 u16 active_dwell = 0;
1802 int added = 0;
1803 u8 channel = 0;
1804
1805 sband = iwl_get_hw_mode(priv, band);
1806 if (!sband) {
1807 IWL_ERR(priv, "invalid band\n");
1808 return added;
1809 }
1810
1811 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
1812 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
1813
1814 if (passive_dwell <= active_dwell)
1815 passive_dwell = active_dwell + 1;
1816
1817
1818 channel = iwl_get_single_channel_number(priv, band);
1819
1820 if (channel) {
1821 scan_ch->channel = channel;
1822 scan_ch->type = 0; /* passive */
1823 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1824 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
1825 /* Set txpower levels to defaults */
1826 scan_ch->tpc.dsp_atten = 110;
1827 if (band == IEEE80211_BAND_5GHZ)
1828 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1829 else
1830 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1831 added++;
1832 } else
1833 IWL_ERR(priv, "no valid channel found\n");
1834 return added;
1835}
1836
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001837static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001838 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08001839 u8 is_active, u8 n_probes,
Johannes Berg1dda6d22010-04-29 04:43:06 -07001840 struct iwl3945_scan_channel *scan_ch,
1841 struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07001842{
Johannes Berg4e05c232009-06-19 13:52:44 -07001843 struct ieee80211_channel *chan;
Johannes Berg8318d782008-01-24 19:38:38 +01001844 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001845 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001846 u16 passive_dwell = 0;
1847 u16 active_dwell = 0;
1848 int added, i;
1849
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001850 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01001851 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07001852 return 0;
1853
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001854 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Johannes Berg1dda6d22010-04-29 04:43:06 -07001855 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Zhu Yib481de92007-09-25 17:54:57 -07001856
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08001857 if (passive_dwell <= active_dwell)
1858 passive_dwell = active_dwell + 1;
1859
Johannes Berg4e05c232009-06-19 13:52:44 -07001860 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
1861 chan = priv->scan_request->channels[i];
1862
1863 if (chan->band != band)
Johannes Berg182e2e62008-04-04 10:41:56 +02001864 continue;
1865
Johannes Berg4e05c232009-06-19 13:52:44 -07001866 scan_ch->channel = chan->hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07001867
Samuel Ortize6148912009-01-23 13:45:15 -08001868 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001869 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -08001870 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07001871 scan_ch->channel);
1872 continue;
1873 }
1874
Zhu Yib481de92007-09-25 17:54:57 -07001875 scan_ch->active_dwell = cpu_to_le16(active_dwell);
1876 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001877 /* If passive , set up for auto-switch
1878 * and use long active_dwell time.
1879 */
1880 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -07001881 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001882 scan_ch->type = 0; /* passive */
1883 if (IWL_UCODE_API(priv->ucode_ver) == 1)
1884 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
1885 } else {
1886 scan_ch->type = 1; /* active */
1887 }
1888
1889 /* Set direct probe bits. These may be used both for active
1890 * scan channels (probes gets sent right away),
1891 * or for passive channels (probes get se sent only after
1892 * hearing clear Rx packet).*/
1893 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
1894 if (n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001895 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001896 } else {
1897 /* uCode v1 does not allow setting direct probe bits on
1898 * passive channel. */
1899 if ((scan_ch->type & 1) && n_probes)
Winkler, Tomas0d210442009-01-23 13:45:21 -08001900 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08001901 }
Zhu Yib481de92007-09-25 17:54:57 -07001902
Ben Cahill9fbab512007-11-29 11:09:47 +08001903 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07001904 scan_ch->tpc.dsp_atten = 110;
1905 /* scan_pwr_info->tpc.dsp_atten; */
1906
1907 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01001908 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001909 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
1910 else {
1911 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
1912 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08001913 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08001914 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07001915 */
1916 }
1917
Tomas Winklere1623442009-01-27 14:27:56 -08001918 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
Zhu Yib481de92007-09-25 17:54:57 -07001919 scan_ch->channel,
1920 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
1921 (scan_ch->type & 1) ?
1922 active_dwell : passive_dwell);
1923
1924 scan_ch++;
1925 added++;
1926 }
1927
Frans Pop91dd6c22010-03-24 14:19:58 -07001928 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
Zhu Yib481de92007-09-25 17:54:57 -07001929 return added;
1930}
1931
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001932static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001933 struct ieee80211_rate *rates)
1934{
1935 int i;
1936
Dan Carpenter8e1a53c2010-03-28 14:55:00 +03001937 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001938 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
1939 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1940 rates[i].hw_value_short = i;
1941 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08001942 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07001943 /*
Johannes Berg8318d782008-01-24 19:38:38 +01001944 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07001945 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001946 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01001947 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07001948 }
Zhu Yib481de92007-09-25 17:54:57 -07001949 }
1950}
1951
Zhu Yib481de92007-09-25 17:54:57 -07001952/******************************************************************************
1953 *
1954 * uCode download functions
1955 *
1956 ******************************************************************************/
1957
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001958static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001959{
Tomas Winkler98c92212008-01-14 17:46:20 -08001960 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1961 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1962 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1963 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1964 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1965 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001966}
1967
1968/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001969 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07001970 * looking at all data.
1971 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001972static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07001973{
1974 u32 val;
1975 u32 save_len = len;
1976 int rc = 0;
1977 u32 errcnt;
1978
Tomas Winklere1623442009-01-27 14:27:56 -08001979 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001980
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001981 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08001982 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07001983
1984 errcnt = 0;
1985 for (; len > 0; len -= sizeof(u32), image++) {
1986 /* read data comes through single port, auto-incr addr */
1987 /* NOTE: Use the debugless read so we don't flood kernel log
1988 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001989 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07001990 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001991 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07001992 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1993 save_len - len, val, le32_to_cpu(*image));
1994 rc = -EIO;
1995 errcnt++;
1996 if (errcnt >= 20)
1997 break;
1998 }
1999 }
2000
Zhu Yib481de92007-09-25 17:54:57 -07002001
2002 if (!errcnt)
Tomas Winklere1623442009-01-27 14:27:56 -08002003 IWL_DEBUG_INFO(priv,
2004 "ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07002005
2006 return rc;
2007}
2008
2009
2010/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002011 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07002012 * using sample data 100 bytes apart. If these sample points are good,
2013 * it's a pretty good bet that everything between them is good, too.
2014 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002015static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07002016{
2017 u32 val;
2018 int rc = 0;
2019 u32 errcnt = 0;
2020 u32 i;
2021
Tomas Winklere1623442009-01-27 14:27:56 -08002022 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002023
Zhu Yib481de92007-09-25 17:54:57 -07002024 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2025 /* read data comes through single port, auto-incr addr */
2026 /* NOTE: Use the debugless read so we don't flood kernel log
2027 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002028 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002029 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002030 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07002031 if (val != le32_to_cpu(*image)) {
2032#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002033 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07002034 "offset 0x%x, is 0x%x, s/b 0x%x\n",
2035 i, val, *image);
2036#endif
2037 rc = -EIO;
2038 errcnt++;
2039 if (errcnt >= 3)
2040 break;
2041 }
2042 }
2043
Zhu Yib481de92007-09-25 17:54:57 -07002044 return rc;
2045}
2046
2047
2048/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002049 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07002050 * and verify its contents
2051 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002052static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002053{
2054 __le32 *image;
2055 u32 len;
2056 int rc = 0;
2057
2058 /* Try bootstrap */
2059 image = (__le32 *)priv->ucode_boot.v_addr;
2060 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002061 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002062 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002063 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002064 return 0;
2065 }
2066
2067 /* Try initialize */
2068 image = (__le32 *)priv->ucode_init.v_addr;
2069 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002070 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002071 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002072 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002073 return 0;
2074 }
2075
2076 /* Try runtime/protocol */
2077 image = (__le32 *)priv->ucode_code.v_addr;
2078 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002079 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002080 if (rc == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08002081 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
Zhu Yib481de92007-09-25 17:54:57 -07002082 return 0;
2083 }
2084
Winkler, Tomas15b16872008-12-19 10:37:33 +08002085 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07002086
Ben Cahill9fbab512007-11-29 11:09:47 +08002087 /* Since nothing seems to match, show first several data entries in
2088 * instruction SRAM, so maybe visual inspection will give a clue.
2089 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07002090 image = (__le32 *)priv->ucode_boot.v_addr;
2091 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002092 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07002093
2094 return rc;
2095}
2096
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002097static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002098{
2099 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002100 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002101}
2102
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002103#define IWL3945_UCODE_GET(item) \
2104static u32 iwl3945_ucode_get_##item(const struct iwl_ucode_header *ucode)\
2105{ \
2106 return le32_to_cpu(ucode->u.v1.item); \
2107}
2108
2109static u32 iwl3945_ucode_get_header_size(u32 api_ver)
2110{
Johannes Berg22adba22010-04-28 12:09:14 -07002111 return 24;
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002112}
2113
2114static u8 *iwl3945_ucode_get_data(const struct iwl_ucode_header *ucode)
2115{
2116 return (u8 *) ucode->u.v1.data;
2117}
2118
2119IWL3945_UCODE_GET(inst_size);
2120IWL3945_UCODE_GET(data_size);
2121IWL3945_UCODE_GET(init_size);
2122IWL3945_UCODE_GET(init_data_size);
2123IWL3945_UCODE_GET(boot_size);
2124
Zhu Yib481de92007-09-25 17:54:57 -07002125/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002126 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07002127 *
2128 * Copy into buffers for card to fetch via bus-mastering
2129 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002130static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002131{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002132 const struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002133 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07002134 const struct firmware *ucode_raw;
2135 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002136 const char *name_pre = priv->cfg->fw_name_pre;
2137 const unsigned int api_max = priv->cfg->ucode_api_max;
2138 const unsigned int api_min = priv->cfg->ucode_api_min;
2139 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07002140 u8 *src;
2141 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08002142 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07002143
2144 /* Ask kernel firmware_class module to get the boot firmware off disk.
2145 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002146 for (index = api_max; index >= api_min; index--) {
2147 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2148 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2149 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002150 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002151 buf, ret);
2152 if (ret == -ENOENT)
2153 continue;
2154 else
2155 goto error;
2156 } else {
2157 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002158 IWL_ERR(priv, "Loaded firmware %s, "
2159 "which is deprecated. "
2160 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002161 buf, api_max);
Tomas Winklere1623442009-01-27 14:27:56 -08002162 IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2163 "(%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002164 buf, ucode_raw->size);
2165 break;
2166 }
Zhu Yib481de92007-09-25 17:54:57 -07002167 }
2168
Reinette Chatrea0987a82008-12-02 12:14:06 -08002169 if (ret < 0)
2170 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07002171
2172 /* Make sure that we got at least our header! */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002173 if (ucode_raw->size < iwl3945_ucode_get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002174 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002175 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002176 goto err_release;
2177 }
2178
2179 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002180 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07002181
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08002182 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08002183 api_ver = IWL_UCODE_API(priv->ucode_ver);
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002184 inst_size = iwl3945_ucode_get_inst_size(ucode);
2185 data_size = iwl3945_ucode_get_data_size(ucode);
2186 init_size = iwl3945_ucode_get_init_size(ucode);
2187 init_data_size = iwl3945_ucode_get_init_data_size(ucode);
2188 boot_size = iwl3945_ucode_get_boot_size(ucode);
2189 src = iwl3945_ucode_get_data(ucode);
Zhu Yib481de92007-09-25 17:54:57 -07002190
Reinette Chatrea0987a82008-12-02 12:14:06 -08002191 /* api_ver should match the api version forming part of the
2192 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01002193 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08002194
2195 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002196 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002197 "Driver supports v%u, firmware is v%u.\n",
2198 api_max, api_ver);
2199 priv->ucode_ver = 0;
2200 ret = -EINVAL;
2201 goto err_release;
2202 }
2203 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08002204 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08002205 "got %u. New firmware can be obtained "
2206 "from http://www.intellinuxwireless.org.\n",
2207 api_max, api_ver);
2208
Tomas Winkler978785a2008-12-19 10:37:31 +08002209 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2210 IWL_UCODE_MAJOR(priv->ucode_ver),
2211 IWL_UCODE_MINOR(priv->ucode_ver),
2212 IWL_UCODE_API(priv->ucode_ver),
2213 IWL_UCODE_SERIAL(priv->ucode_ver));
2214
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07002215 snprintf(priv->hw->wiphy->fw_version,
2216 sizeof(priv->hw->wiphy->fw_version),
2217 "%u.%u.%u.%u",
2218 IWL_UCODE_MAJOR(priv->ucode_ver),
2219 IWL_UCODE_MINOR(priv->ucode_ver),
2220 IWL_UCODE_API(priv->ucode_ver),
2221 IWL_UCODE_SERIAL(priv->ucode_ver));
2222
Tomas Winklere1623442009-01-27 14:27:56 -08002223 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08002224 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08002225 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2226 inst_size);
2227 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2228 data_size);
2229 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2230 init_size);
2231 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2232 init_data_size);
2233 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2234 boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07002235
Reinette Chatrea0987a82008-12-02 12:14:06 -08002236
Zhu Yib481de92007-09-25 17:54:57 -07002237 /* Verify size of file vs. image size info in file's header */
Johannes Berg93b1a2f2010-04-28 12:09:13 -07002238 if (ucode_raw->size != iwl3945_ucode_get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07002239 inst_size + data_size + init_size +
2240 init_data_size + boot_size) {
2241
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002242 IWL_DEBUG_INFO(priv,
2243 "uCode file size %zd does not match expected size\n",
2244 ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002245 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002246 goto err_release;
2247 }
2248
2249 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002250 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002251 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002252 inst_size);
2253 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002254 goto err_release;
2255 }
2256
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002257 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002258 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002259 data_size);
2260 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002261 goto err_release;
2262 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002263 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002264 IWL_DEBUG_INFO(priv,
2265 "uCode init instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002266 init_size);
2267 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002268 goto err_release;
2269 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002270 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002271 IWL_DEBUG_INFO(priv,
2272 "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002273 init_data_size);
2274 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002275 goto err_release;
2276 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08002277 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winklere1623442009-01-27 14:27:56 -08002278 IWL_DEBUG_INFO(priv,
2279 "uCode boot instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08002280 boot_size);
2281 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002282 goto err_release;
2283 }
2284
2285 /* Allocate ucode buffers for card's bus-master loading ... */
2286
2287 /* Runtime instructions and 2 copies of data:
2288 * 1) unmodified from disk
2289 * 2) backup cache for save/restore during power-downs */
2290 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002291 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07002292
2293 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002294 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07002295
2296 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002297 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07002298
2299 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08002300 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07002301 goto err_pci_alloc;
2302
Tomas Winkler90e759d2007-11-29 11:09:41 +08002303 /* Initialization instructions and data */
2304 if (init_size && init_data_size) {
2305 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002306 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002307
2308 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002309 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002310
2311 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2312 goto err_pci_alloc;
2313 }
2314
2315 /* Bootstrap (instructions only, no data) */
2316 if (boot_size) {
2317 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08002318 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08002319
2320 if (!priv->ucode_boot.v_addr)
2321 goto err_pci_alloc;
2322 }
2323
Zhu Yib481de92007-09-25 17:54:57 -07002324 /* Copy images into buffers for card's bus-master reads ... */
2325
2326 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002327 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002328 IWL_DEBUG_INFO(priv,
2329 "Copying (but not loading) uCode instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002330 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002331 src += len;
2332
Tomas Winklere1623442009-01-27 14:27:56 -08002333 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07002334 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2335
2336 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002337 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002338 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002339 IWL_DEBUG_INFO(priv,
2340 "Copying (but not loading) uCode data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002341 memcpy(priv->ucode_data.v_addr, src, len);
2342 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002343 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002344
2345 /* Initialization instructions (3rd block) */
2346 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002347 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002348 IWL_DEBUG_INFO(priv,
2349 "Copying (but not loading) init instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002350 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002351 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002352 }
2353
2354 /* Initialization data (4th block) */
2355 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002356 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002357 IWL_DEBUG_INFO(priv,
2358 "Copying (but not loading) init data len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002359 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002360 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07002361 }
2362
2363 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07002364 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08002365 IWL_DEBUG_INFO(priv,
2366 "Copying (but not loading) boot instr len %zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07002367 memcpy(priv->ucode_boot.v_addr, src, len);
2368
2369 /* We have our copies now, allow OS release its copies */
2370 release_firmware(ucode_raw);
2371 return 0;
2372
2373 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002374 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08002375 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002376 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002377
2378 err_release:
2379 release_firmware(ucode_raw);
2380
2381 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08002382 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002383}
2384
2385
2386/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002387 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07002388 *
2389 * Tell initialization uCode where to find runtime uCode.
2390 *
2391 * BSM registers initially contain pointers to initialization uCode.
2392 * We need to replace them to load runtime uCode inst and data,
2393 * and to save runtime data when powering down.
2394 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002395static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002396{
2397 dma_addr_t pinst;
2398 dma_addr_t pdata;
Zhu Yib481de92007-09-25 17:54:57 -07002399
2400 /* bits 31:0 for 3945 */
2401 pinst = priv->ucode_code.p_addr;
2402 pdata = priv->ucode_data_backup.p_addr;
2403
Zhu Yib481de92007-09-25 17:54:57 -07002404 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002405 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2406 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2407 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002408 priv->ucode_data.len);
2409
Tomas Winklera96a27f2008-10-23 23:48:56 -07002410 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07002411 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002412 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07002413 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2414
Tomas Winklere1623442009-01-27 14:27:56 -08002415 IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002416
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07002417 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002418}
2419
2420/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002421 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002422 *
2423 * Called after REPLY_ALIVE notification received from "initialize" uCode.
2424 *
Zhu Yib481de92007-09-25 17:54:57 -07002425 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08002426 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002427static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002428{
2429 /* Check alive response for "valid" sign from uCode */
2430 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2431 /* We had an error bringing up the hardware, so take it
2432 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002433 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002434 goto restart;
2435 }
2436
2437 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2438 * This is a paranoid check, because we would not have gotten the
2439 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002440 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002441 /* Runtime instruction load was bad;
2442 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002443 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002444 goto restart;
2445 }
2446
2447 /* Send pointers to protocol/runtime uCode image ... init code will
2448 * load and launch runtime uCode, which will send us another "Alive"
2449 * notification. */
Tomas Winklere1623442009-01-27 14:27:56 -08002450 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002451 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002452 /* Runtime instruction load won't happen;
2453 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002454 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002455 goto restart;
2456 }
2457 return;
2458
2459 restart:
2460 queue_work(priv->workqueue, &priv->restart);
2461}
2462
Zhu Yib481de92007-09-25 17:54:57 -07002463/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002464 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002465 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002466 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002467 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002468static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002469{
Zhu Yib481de92007-09-25 17:54:57 -07002470 int thermal_spin = 0;
2471 u32 rfkill;
Johannes Berg246ed352010-08-23 10:46:32 +02002472 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07002473
Tomas Winklere1623442009-01-27 14:27:56 -08002474 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002475
2476 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2477 /* We had an error bringing up the hardware, so take it
2478 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002479 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002480 goto restart;
2481 }
2482
2483 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2484 * This is a paranoid check, because we would not have gotten the
2485 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002486 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002487 /* Runtime instruction load was bad;
2488 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002489 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002490 goto restart;
2491 }
2492
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002493 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Tomas Winklere1623442009-01-27 14:27:56 -08002494 IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
Zhu Yib481de92007-09-25 17:54:57 -07002495
2496 if (rfkill & 0x1) {
2497 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002498 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07002499 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002500 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07002501 thermal_spin++;
2502 udelay(10);
2503 }
2504
2505 if (thermal_spin)
Tomas Winklere1623442009-01-27 14:27:56 -08002506 IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
Zhu Yib481de92007-09-25 17:54:57 -07002507 thermal_spin * 10);
2508 } else
2509 set_bit(STATUS_RF_KILL_HW, &priv->status);
2510
Ben Cahill9fbab512007-11-29 11:09:47 +08002511 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002512 set_bit(STATUS_ALIVE, &priv->status);
2513
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08002514 if (priv->cfg->ops->lib->recover_from_tx_stall) {
2515 /* Enable timer to monitor the driver queues */
2516 mod_timer(&priv->monitor_recover,
2517 jiffies +
2518 msecs_to_jiffies(priv->cfg->monitor_recover_period));
2519 }
2520
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002521 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002522 return;
2523
Johannes Berg36d68252008-05-15 12:55:26 +02002524 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002525
Johannes Berg470ab2d2010-01-21 11:23:30 -08002526 priv->active_rate = IWL_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07002527
Ben Cahill4d6ccbf2009-11-13 11:56:29 -08002528 iwl_power_update_mode(priv, true);
Zhu Yib481de92007-09-25 17:54:57 -07002529
Johannes Berg246ed352010-08-23 10:46:32 +02002530 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002531 struct iwl3945_rxon_cmd *active_rxon =
Johannes Berg246ed352010-08-23 10:46:32 +02002532 (struct iwl3945_rxon_cmd *)(&ctx->active);
Zhu Yib481de92007-09-25 17:54:57 -07002533
Johannes Berg246ed352010-08-23 10:46:32 +02002534 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002535 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2536 } else {
2537 /* Initialize our rx_config data */
Johannes Berg1dda6d22010-04-29 04:43:06 -07002538 iwl_connection_init_rx_config(priv, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002539 }
2540
Ben Cahill9fbab512007-11-29 11:09:47 +08002541 /* Configure Bluetooth device coexistence support */
Johannes Berg65b52bd2010-04-13 01:04:31 -07002542 priv->cfg->ops->hcmd->send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002543
2544 /* Configure the adapter for unassociated operation */
Johannes Berg246ed352010-08-23 10:46:32 +02002545 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07002546
Zhu Yib481de92007-09-25 17:54:57 -07002547 iwl3945_reg_txpower_periodic(priv);
2548
Johannes Berge932a602009-10-02 13:44:03 -07002549 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002550
Tomas Winklere1623442009-01-27 14:27:56 -08002551 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002552 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002553 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002554
Zhu Yib481de92007-09-25 17:54:57 -07002555 return;
2556
2557 restart:
2558 queue_work(priv->workqueue, &priv->restart);
2559}
2560
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002561static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002562
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002563static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002564{
2565 unsigned long flags;
2566 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2567 struct ieee80211_conf *conf = NULL;
2568
Tomas Winklere1623442009-01-27 14:27:56 -08002569 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002570
2571 conf = ieee80211_get_hw_conf(priv->hw);
2572
2573 if (!exit_pending)
2574 set_bit(STATUS_EXIT_PENDING, &priv->status);
2575
Stanislaw Gruszkab62177a2010-08-20 15:23:47 +02002576 /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
2577 * to prevent rearm timer */
2578 if (priv->cfg->ops->lib->recover_from_tx_stall)
2579 del_timer_sync(&priv->monitor_recover);
2580
Reinette Chatre7e246192010-02-18 22:58:32 -08002581 /* Station information will now be cleared in device */
Johannes Berg2c810cc2010-04-29 00:53:29 -07002582 iwl_clear_ucode_stations(priv);
2583 iwl_dealloc_bcast_station(priv);
Johannes Bergdb125c72010-05-07 01:49:15 -07002584 iwl_clear_driver_stations(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002585
2586 /* Unblock any waiting calls */
2587 wake_up_interruptible_all(&priv->wait_command_queue);
2588
Zhu Yib481de92007-09-25 17:54:57 -07002589 /* Wipe out the EXIT_PENDING status bit if we are not actually
2590 * exiting the module */
2591 if (!exit_pending)
2592 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2593
2594 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002595 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002596
2597 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002598 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002599 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002600 spin_unlock_irqrestore(&priv->lock, flags);
2601 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002602
2603 if (priv->mac80211_registered)
2604 ieee80211_stop_queues(priv->hw);
2605
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002606 /* If we have not previously called iwl3945_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002607 * clear all bits but the RF Kill bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002608 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002609 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2610 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002611 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2612 STATUS_GEO_CONFIGURED |
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002613 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2614 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002615 goto exit;
2616 }
2617
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002618 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002619 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002620 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2621 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002622 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2623 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002624 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002625 STATUS_FW_ERROR |
2626 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2627 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002629 iwl3945_hw_txq_ctx_stop(priv);
2630 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002631
Ben Cahill309e7312009-11-06 14:53:03 -08002632 /* Power-down device's busmaster DMA clocks */
2633 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002634 udelay(5);
2635
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002636 /* Stop the device, and put it in low power state */
2637 priv->cfg->ops->lib->apm_ops.stop(priv);
Abbas, Mohamede9414b62009-01-20 21:33:55 -08002638
Zhu Yib481de92007-09-25 17:54:57 -07002639 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002640 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002641
2642 if (priv->ibss_beacon)
2643 dev_kfree_skb(priv->ibss_beacon);
2644 priv->ibss_beacon = NULL;
2645
2646 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002647 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002648}
2649
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002650static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002651{
2652 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002653 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002654 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002655
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002656 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002657}
2658
2659#define MAX_HW_RESTARTS 5
2660
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002661static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002662{
2663 int rc, i;
2664
Johannes Berg2c810cc2010-04-29 00:53:29 -07002665 rc = iwl_alloc_bcast_station(priv, false);
2666 if (rc)
2667 return rc;
2668
Zhu Yib481de92007-09-25 17:54:57 -07002669 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002670 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002671 return -EIO;
2672 }
2673
Reinette Chatree903fbd2008-01-30 22:05:15 -08002674 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002675 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002676 return -EIO;
2677 }
2678
Zhu Yie655b9f2008-01-24 02:19:38 -08002679 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002680 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08002681 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2682 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2683 else {
2684 set_bit(STATUS_RF_KILL_HW, &priv->status);
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002685 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
2686 return -ENODEV;
Zhu Yib481de92007-09-25 17:54:57 -07002687 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02002688
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002689 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002690
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002691 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002692 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002693 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07002694 return rc;
2695 }
2696
2697 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002698 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2699 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002700 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2701
2702 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002703 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08002704 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002705
2706 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002707 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2708 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002709
2710 /* Copy original ucode data image from disk into backup cache.
2711 * This will be used to initialize the on-board processor's
2712 * data SRAM for a clean start when the runtime program first loads. */
2713 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08002714 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002715
Zhu Yie655b9f2008-01-24 02:19:38 -08002716 /* We return success when we resume from suspend and rf_kill is on. */
2717 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
2718 return 0;
2719
Zhu Yib481de92007-09-25 17:54:57 -07002720 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2721
Zhu Yib481de92007-09-25 17:54:57 -07002722 /* load bootstrap state machine,
2723 * load bootstrap program into processor's memory,
2724 * prepare to load the "initialize" uCode */
Abhijeet Kolekar75a9a922010-02-26 15:17:01 -08002725 rc = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002726
2727 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002728 IWL_ERR(priv,
2729 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07002730 continue;
2731 }
2732
2733 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002734 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002735
Tomas Winklere1623442009-01-27 14:27:56 -08002736 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002737
2738 return 0;
2739 }
2740
2741 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002742 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08002743 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002744
2745 /* tried to restart and config the device for as long as our
2746 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002747 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002748 return -EIO;
2749}
2750
2751
2752/*****************************************************************************
2753 *
2754 * Workqueue callbacks
2755 *
2756 *****************************************************************************/
2757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002758static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002759{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002760 struct iwl_priv *priv =
2761 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002762
2763 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2764 return;
2765
2766 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002767 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002768 mutex_unlock(&priv->mutex);
2769}
2770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002771static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002772{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002773 struct iwl_priv *priv =
2774 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002775
2776 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2777 return;
2778
2779 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002780 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002781 mutex_unlock(&priv->mutex);
2782}
2783
Ben Cahill743cdf12009-10-09 13:20:26 -07002784/*
2785 * 3945 cannot interrupt driver when hardware rf kill switch toggles;
2786 * driver must poll CSR_GP_CNTRL_REG register for change. This register
2787 * *is* readable even when device has been SW_RESET into low power mode
2788 * (e.g. during RF KILL).
2789 */
Helmut Schaa26635162009-01-15 09:38:44 +01002790static void iwl3945_rfkill_poll(struct work_struct *data)
2791{
2792 struct iwl_priv *priv =
Johannes Bergee525d12010-01-21 06:09:28 -08002793 container_of(data, struct iwl_priv, _3945.rfkill_poll.work);
Ben Cahill743cdf12009-10-09 13:20:26 -07002794 bool old_rfkill = test_bit(STATUS_RF_KILL_HW, &priv->status);
2795 bool new_rfkill = !(iwl_read32(priv, CSR_GP_CNTRL)
2796 & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Helmut Schaa26635162009-01-15 09:38:44 +01002797
Ben Cahill743cdf12009-10-09 13:20:26 -07002798 if (new_rfkill != old_rfkill) {
2799 if (new_rfkill)
2800 set_bit(STATUS_RF_KILL_HW, &priv->status);
2801 else
2802 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Helmut Schaa26635162009-01-15 09:38:44 +01002803
Ben Cahill743cdf12009-10-09 13:20:26 -07002804 wiphy_rfkill_set_hw_state(priv->hw->wiphy, new_rfkill);
Helmut Schaa26635162009-01-15 09:38:44 +01002805
Ben Cahill743cdf12009-10-09 13:20:26 -07002806 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
2807 new_rfkill ? "disable radio" : "enable radio");
2808 }
2809
2810 /* Keep this running, even if radio now enabled. This will be
2811 * cancelled in mac_start() if system decides to start again */
Johannes Bergee525d12010-01-21 06:09:28 -08002812 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01002813 round_jiffies_relative(2 * HZ));
2814
2815}
2816
Johannes Berg1dda6d22010-04-29 04:43:06 -07002817void iwl3945_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07002818{
Winkler, Tomasc2d79b482008-12-19 10:37:34 +08002819 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002820 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002821 .len = sizeof(struct iwl3945_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -07002822 .flags = CMD_SIZE_HUGE,
Zhu Yib481de92007-09-25 17:54:57 -07002823 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002824 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07002825 struct ieee80211_conf *conf = NULL;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002826 u8 n_probes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002827 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002828 bool is_active = false;
Zhu Yib481de92007-09-25 17:54:57 -07002829
2830 conf = ieee80211_get_hw_conf(priv->hw);
2831
Reinette Chatrefbc9f972009-05-15 16:13:46 -07002832 cancel_delayed_work(&priv->scan_check);
2833
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002834 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002835 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002836 goto done;
2837 }
2838
Tomas Winklera96a27f2008-10-23 23:48:56 -07002839 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07002840 * was given the chance to run... */
2841 if (!test_bit(STATUS_SCANNING, &priv->status))
2842 goto done;
2843
2844 /* This should never be called or scheduled if there is currently
2845 * a scan active in the hardware. */
2846 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002847 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
2848 "Ignoring second request.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002849 goto done;
2850 }
2851
2852 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002853 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Zhu Yib481de92007-09-25 17:54:57 -07002854 goto done;
2855 }
2856
2857 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002858 IWL_DEBUG_HC(priv,
2859 "Scan request while abort pending. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002860 goto done;
2861 }
2862
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002863 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002864 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Zhu Yib481de92007-09-25 17:54:57 -07002865 goto done;
2866 }
2867
2868 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002869 IWL_DEBUG_HC(priv,
2870 "Scan request while uninitialized. Queuing.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002871 goto done;
2872 }
2873
Johannes Berg811ecc92010-04-06 04:12:41 -07002874 if (!priv->scan_cmd) {
2875 priv->scan_cmd = kmalloc(sizeof(struct iwl3945_scan_cmd) +
2876 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
2877 if (!priv->scan_cmd) {
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002878 IWL_DEBUG_SCAN(priv, "Fail to allocate scan memory\n");
Zhu Yib481de92007-09-25 17:54:57 -07002879 goto done;
2880 }
2881 }
Johannes Berg811ecc92010-04-06 04:12:41 -07002882 scan = priv->scan_cmd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002883 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07002884
2885 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
2886 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
2887
Johannes Berg246ed352010-08-23 10:46:32 +02002888 if (iwl_is_associated(priv, IWL_RXON_CTX_BSS)) {
Zhu Yib481de92007-09-25 17:54:57 -07002889 u16 interval = 0;
2890 u32 extra;
2891 u32 suspend_time = 100;
2892 u32 scan_suspend_time = 100;
2893 unsigned long flags;
2894
Tomas Winklere1623442009-01-27 14:27:56 -08002895 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Zhu Yib481de92007-09-25 17:54:57 -07002896
2897 spin_lock_irqsave(&priv->lock, flags);
John W. Linvillea6e492b2010-07-22 15:24:56 -04002898 if (priv->is_internal_short_scan)
2899 interval = 0;
2900 else
2901 interval = vif->bss_conf.beacon_int;
Zhu Yib481de92007-09-25 17:54:57 -07002902 spin_unlock_irqrestore(&priv->lock, flags);
2903
2904 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08002905 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07002906 if (!interval)
2907 interval = suspend_time;
2908 /*
2909 * suspend time format:
2910 * 0-19: beacon interval in usec (time before exec.)
2911 * 20-23: 0
2912 * 24-31: number of beacons (suspend between channels)
2913 */
2914
2915 extra = (suspend_time / interval) << 24;
2916 scan_suspend_time = 0xFF0FFFFF &
2917 (extra | ((suspend_time % interval) * 1024));
2918
2919 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -08002920 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002921 scan_suspend_time, interval);
2922 }
2923
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002924 if (priv->is_internal_short_scan) {
2925 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
2926 } else if (priv->scan_request->n_ssids) {
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002927 int i, p = 0;
2928 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
2929 for (i = 0; i < priv->scan_request->n_ssids; i++) {
2930 /* always does wildcard anyway */
2931 if (!priv->scan_request->ssids[i].ssid_len)
2932 continue;
2933 scan->direct_scan[p].id = WLAN_EID_SSID;
2934 scan->direct_scan[p].len =
2935 priv->scan_request->ssids[i].ssid_len;
2936 memcpy(scan->direct_scan[p].ssid,
2937 priv->scan_request->ssids[i].ssid,
2938 priv->scan_request->ssids[i].ssid_len);
2939 n_probes++;
2940 p++;
2941 }
2942 is_active = true;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08002943 } else
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002944 IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002945
2946 /* We don't build a direct scan probe request; the uCode will do
2947 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07002948 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002949 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002950 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2951
2952 /* flags + rate selection */
2953
Johannes Berg00700ee2010-04-06 04:12:37 -07002954 switch (priv->scan_band) {
2955 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002956 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
2957 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
2958 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002959 band = IEEE80211_BAND_2GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002960 break;
2961 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07002962 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
Johannes Bergb097ad22009-04-20 14:36:57 -07002963 /*
2964 * If active scaning is requested but a certain channel
2965 * is marked passive, we can do active scanning if we
2966 * detect transmissions.
2967 */
Johannes Berg96ff5642010-04-30 14:42:15 -07002968 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
2969 IWL_GOOD_CRC_TH_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +01002970 band = IEEE80211_BAND_5GHZ;
Johannes Berg00700ee2010-04-06 04:12:37 -07002971 break;
2972 default:
2973 IWL_WARN(priv, "Invalid scan band\n");
Zhu Yib481de92007-09-25 17:54:57 -07002974 goto done;
2975 }
2976
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002977 if (!priv->is_internal_short_scan) {
2978 scan->tx_cmd.len = cpu_to_le16(
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002979 iwl_fill_probe_req(priv,
2980 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002981 vif->addr,
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07002982 priv->scan_request->ie,
2983 priv->scan_request->ie_len,
2984 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002985 } else {
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002986 /* use bcast addr, will not be transmitted but must be valid */
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002987 scan->tx_cmd.len = cpu_to_le16(
2988 iwl_fill_probe_req(priv,
2989 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07002990 iwl_bcast_addr, NULL, 0,
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08002991 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
2992 }
Zhu Yib481de92007-09-25 17:54:57 -07002993 /* select Rx antennas */
2994 scan->flags |= iwl3945_get_antenna_flags(priv);
2995
Abhijeet Kolekar14023642010-06-02 21:15:10 -07002996 if (priv->is_internal_short_scan) {
2997 scan->channel_count =
2998 iwl3945_get_single_channel_for_scan(priv, vif, band,
2999 (void *)&scan->data[le16_to_cpu(
3000 scan->tx_cmd.len)]);
3001 } else {
3002 scan->channel_count =
3003 iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
3004 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)], vif);
3005 }
Zhu Yib481de92007-09-25 17:54:57 -07003006
Reinette Chatre14b54332008-11-04 12:21:35 -08003007 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -08003008 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Reinette Chatre14b54332008-11-04 12:21:35 -08003009 goto done;
3010 }
3011
Zhu Yib481de92007-09-25 17:54:57 -07003012 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003013 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07003014 cmd.data = scan;
3015 scan->len = cpu_to_le16(cmd.len);
3016
3017 set_bit(STATUS_SCAN_HW, &priv->status);
Wey-Yi Guy4f4d4082010-02-24 08:28:30 -08003018 if (iwl_send_cmd_sync(priv, &cmd))
Zhu Yib481de92007-09-25 17:54:57 -07003019 goto done;
3020
3021 queue_delayed_work(priv->workqueue, &priv->scan_check,
3022 IWL_SCAN_CHECK_WATCHDOG);
3023
Zhu Yib481de92007-09-25 17:54:57 -07003024 return;
3025
3026 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08003027 /* can not perform scan make sure we clear scanning
3028 * bits from status so next scan request can be performed.
3029 * if we dont clear scanning status bit here all next scan
3030 * will fail
3031 */
3032 clear_bit(STATUS_SCAN_HW, &priv->status);
3033 clear_bit(STATUS_SCANNING, &priv->status);
3034
Ian Schram01ebd062007-10-25 17:15:22 +08003035 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07003036 queue_work(priv->workqueue, &priv->scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07003037}
3038
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003039static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003040{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003041 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07003042
3043 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3044 return;
3045
Johannes Berg19cc1082009-05-08 13:44:36 -07003046 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
3047 mutex_lock(&priv->mutex);
3048 priv->vif = NULL;
3049 priv->is_open = 0;
3050 mutex_unlock(&priv->mutex);
3051 iwl3945_down(priv);
3052 ieee80211_restart_hw(priv->hw);
3053 } else {
3054 iwl3945_down(priv);
Johannes Berg80676512010-01-21 06:07:17 -08003055
3056 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3057 return;
3058
3059 mutex_lock(&priv->mutex);
3060 __iwl3945_up(priv);
3061 mutex_unlock(&priv->mutex);
Johannes Berg19cc1082009-05-08 13:44:36 -07003062 }
Zhu Yib481de92007-09-25 17:54:57 -07003063}
3064
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003065static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07003066{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003067 struct iwl_priv *priv =
3068 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07003069
3070 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3071 return;
3072
3073 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003074 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003075 mutex_unlock(&priv->mutex);
3076}
3077
Johannes Berg1dda6d22010-04-29 04:43:06 -07003078void iwl3945_post_associate(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003079{
Zhu Yib481de92007-09-25 17:54:57 -07003080 int rc = 0;
3081 struct ieee80211_conf *conf = NULL;
Johannes Berg246ed352010-08-23 10:46:32 +02003082 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003083
Johannes Berg1dda6d22010-04-29 04:43:06 -07003084 if (!vif || !priv->is_open)
3085 return;
3086
3087 if (vif->type == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003088 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07003089 return;
3090 }
3091
Tomas Winklere1623442009-01-27 14:27:56 -08003092 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003093 vif->bss_conf.aid, ctx->active.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003094
3095 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3096 return;
3097
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003098 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003099
Zhu Yib481de92007-09-25 17:54:57 -07003100 conf = ieee80211_get_hw_conf(priv->hw);
3101
Johannes Berg246ed352010-08-23 10:46:32 +02003102 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3103 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003104
Johannes Berg948f5a22010-07-29 07:07:51 -07003105 rc = iwl_send_rxon_timing(priv, vif);
Zhu Yib481de92007-09-25 17:54:57 -07003106 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003107 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003108 "Attempting to continue.\n");
3109
Johannes Berg246ed352010-08-23 10:46:32 +02003110 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003111
Johannes Berg246ed352010-08-23 10:46:32 +02003112 ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
Zhu Yib481de92007-09-25 17:54:57 -07003113
Tomas Winklere1623442009-01-27 14:27:56 -08003114 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Johannes Berg1dda6d22010-04-29 04:43:06 -07003115 vif->bss_conf.aid, vif->bss_conf.beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07003116
Johannes Bergc213d742010-05-06 12:21:40 -07003117 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003118 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003119 else
Johannes Berg246ed352010-08-23 10:46:32 +02003120 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003121
Johannes Berg246ed352010-08-23 10:46:32 +02003122 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003123 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003124 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003125 else
Johannes Berg246ed352010-08-23 10:46:32 +02003126 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003127 }
3128
Johannes Berg246ed352010-08-23 10:46:32 +02003129 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003130
Johannes Berg1dda6d22010-04-29 04:43:06 -07003131 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02003132 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003133 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07003134 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02003135 case NL80211_IFTYPE_ADHOC:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003136 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003137 break;
Zhu Yib481de92007-09-25 17:54:57 -07003138 default:
Johannes Berg1dda6d22010-04-29 04:43:06 -07003139 IWL_ERR(priv, "%s Should not be called in %d mode\n",
3140 __func__, vif->type);
Zhu Yib481de92007-09-25 17:54:57 -07003141 break;
3142 }
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08003143}
3144
Zhu Yib481de92007-09-25 17:54:57 -07003145/*****************************************************************************
3146 *
3147 * mac80211 entry point functions
3148 *
3149 *****************************************************************************/
3150
Zhu Yi5a66926a2008-01-14 17:46:18 -08003151#define UCODE_READY_TIMEOUT (2 * HZ)
3152
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003153static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003154{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003155 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003156 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003157
Tomas Winklere1623442009-01-27 14:27:56 -08003158 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003159
3160 /* we should be verifying the device is ready to be opened */
3161 mutex_lock(&priv->mutex);
3162
Zhu Yi5a66926a2008-01-14 17:46:18 -08003163 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3164 * ucode filename and max sizes are card-specific. */
3165
3166 if (!priv->ucode_code.len) {
3167 ret = iwl3945_read_ucode(priv);
3168 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003169 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003170 mutex_unlock(&priv->mutex);
3171 goto out_release_irq;
3172 }
3173 }
3174
Zhu Yie655b9f2008-01-24 02:19:38 -08003175 ret = __iwl3945_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003176
Zhu Yib481de92007-09-25 17:54:57 -07003177 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08003178
Zhu Yie655b9f2008-01-24 02:19:38 -08003179 if (ret)
3180 goto out_release_irq;
3181
Tomas Winklere1623442009-01-27 14:27:56 -08003182 IWL_DEBUG_INFO(priv, "Start UP work.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003183
Zhu Yi5a66926a2008-01-14 17:46:18 -08003184 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3185 * mac80211 will not be run successfully. */
3186 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3187 test_bit(STATUS_READY, &priv->status),
3188 UCODE_READY_TIMEOUT);
3189 if (!ret) {
3190 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003191 IWL_ERR(priv,
3192 "Wait for START_ALIVE timeout after %dms.\n",
3193 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a66926a2008-01-14 17:46:18 -08003194 ret = -ETIMEDOUT;
3195 goto out_release_irq;
3196 }
3197 }
3198
Helmut Schaa26635162009-01-15 09:38:44 +01003199 /* ucode is running and will send rfkill notifications,
3200 * no need to poll the killswitch state anymore */
Johannes Bergee525d12010-01-21 06:09:28 -08003201 cancel_delayed_work(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01003202
Johannes Berge932a602009-10-02 13:44:03 -07003203 iwl_led_start(priv);
3204
Zhu Yie655b9f2008-01-24 02:19:38 -08003205 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08003206 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003207 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003208
3209out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08003210 priv->is_open = 0;
Tomas Winklere1623442009-01-27 14:27:56 -08003211 IWL_DEBUG_MAC80211(priv, "leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08003212 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003213}
3214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003215static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07003216{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003217 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003218
Tomas Winklere1623442009-01-27 14:27:56 -08003219 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003220
Zhu Yie655b9f2008-01-24 02:19:38 -08003221 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08003222 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08003223 return;
3224 }
3225
Zhu Yib481de92007-09-25 17:54:57 -07003226 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08003227
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003228 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08003229 /* stop mac, cancel any scan request and clear
3230 * RXON_FILTER_ASSOC_MSK BIT
3231 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08003232 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003233 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003234 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08003235 }
3236
Zhu Yi5a66926a2008-01-14 17:46:18 -08003237 iwl3945_down(priv);
3238
3239 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01003240
3241 /* start polling the killswitch state again */
Johannes Bergee525d12010-01-21 06:09:28 -08003242 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01003243 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08003244
Tomas Winklere1623442009-01-27 14:27:56 -08003245 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003246}
3247
Johannes Berge039fa42008-05-15 12:55:29 +02003248static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07003249{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003250 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07003251
Tomas Winklere1623442009-01-27 14:27:56 -08003252 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003253
Tomas Winklere1623442009-01-27 14:27:56 -08003254 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02003255 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07003256
Johannes Berge039fa42008-05-15 12:55:29 +02003257 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07003258 dev_kfree_skb_any(skb);
3259
Tomas Winklere1623442009-01-27 14:27:56 -08003260 IWL_DEBUG_MAC80211(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08003261 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07003262}
3263
Johannes Berg1dda6d22010-04-29 04:43:06 -07003264void iwl3945_config_ap(struct iwl_priv *priv, struct ieee80211_vif *vif)
Zhu Yib481de92007-09-25 17:54:57 -07003265{
Johannes Berg246ed352010-08-23 10:46:32 +02003266 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003267 int rc = 0;
3268
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08003269 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07003270 return;
3271
3272 /* The following should be done only at AP bring up */
Johannes Berg246ed352010-08-23 10:46:32 +02003273 if (!(iwl_is_associated(priv, IWL_RXON_CTX_BSS))) {
Zhu Yib481de92007-09-25 17:54:57 -07003274
3275 /* RXON - unassoc (to set timing command) */
Johannes Berg246ed352010-08-23 10:46:32 +02003276 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3277 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003278
3279 /* RXON Timing */
Johannes Berg948f5a22010-07-29 07:07:51 -07003280 rc = iwl_send_rxon_timing(priv, vif);
Zhu Yib481de92007-09-25 17:54:57 -07003281 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003282 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07003283 "Attempting to continue.\n");
3284
Johannes Berg246ed352010-08-23 10:46:32 +02003285 ctx->staging.assoc_id = 0;
Johannes Berg1dda6d22010-04-29 04:43:06 -07003286
Johannes Bergc213d742010-05-06 12:21:40 -07003287 if (vif->bss_conf.use_short_preamble)
Johannes Berg246ed352010-08-23 10:46:32 +02003288 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003289 RXON_FLG_SHORT_PREAMBLE_MSK;
3290 else
Johannes Berg246ed352010-08-23 10:46:32 +02003291 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003292 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3293
Johannes Berg246ed352010-08-23 10:46:32 +02003294 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK) {
Johannes Bergc213d742010-05-06 12:21:40 -07003295 if (vif->bss_conf.use_short_slot)
Johannes Berg246ed352010-08-23 10:46:32 +02003296 ctx->staging.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07003297 RXON_FLG_SHORT_SLOT_MSK;
3298 else
Johannes Berg246ed352010-08-23 10:46:32 +02003299 ctx->staging.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07003300 ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07003301 }
3302 /* restore RXON assoc */
Johannes Berg246ed352010-08-23 10:46:32 +02003303 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
3304 iwlcore_commit_rxon(priv, ctx);
Zhu Yi556f8db2007-09-27 11:27:33 +08003305 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003306 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003307
3308 /* FIXME - we need to add code here to detect a totally new
3309 * configuration, reset the AP, unassoc, rxon timing, assoc,
3310 * clear sta table, add BCAST sta... */
3311}
3312
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003313static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003314 struct ieee80211_vif *vif,
3315 struct ieee80211_sta *sta,
3316 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07003317{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003318 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003319 int ret = 0;
3320 u8 sta_id = IWL_INVALID_STATION;
3321 u8 static_key;
Zhu Yib481de92007-09-25 17:54:57 -07003322
Tomas Winklere1623442009-01-27 14:27:56 -08003323 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003324
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08003325 if (iwl3945_mod_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08003326 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07003327 return -EOPNOTSUPP;
3328 }
3329
Johannes Berg246ed352010-08-23 10:46:32 +02003330 static_key = !iwl_is_associated(priv, IWL_RXON_CTX_BSS);
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003331
3332 if (!static_key) {
Johannes Berg0af8bca2010-04-30 14:08:00 -07003333 sta_id = iwl_sta_id_or_broadcast(priv, sta);
3334 if (sta_id == IWL_INVALID_STATION)
3335 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003336 }
3337
3338 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003339 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08003340
Zhu Yib481de92007-09-25 17:54:57 -07003341 switch (cmd) {
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003342 case SET_KEY:
3343 if (static_key)
3344 ret = iwl3945_set_static_key(priv, key);
3345 else
3346 ret = iwl3945_set_dynamic_key(priv, key, sta_id);
3347 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003348 break;
3349 case DISABLE_KEY:
Abhijeet Kolekar6e21f152009-02-27 16:21:21 -08003350 if (static_key)
3351 ret = iwl3945_remove_static_key(priv);
3352 else
3353 ret = iwl3945_clear_sta_key_info(priv, sta_id);
3354 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07003355 break;
3356 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08003357 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003358 }
3359
Johannes Berg72e15d72010-02-19 11:42:32 -08003360 mutex_unlock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08003361 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07003362
Winkler, Tomas42986792009-01-19 15:30:22 -08003363 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003364}
3365
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003366static int iwl3945_mac_sta_add(struct ieee80211_hw *hw,
3367 struct ieee80211_vif *vif,
3368 struct ieee80211_sta *sta)
3369{
3370 struct iwl_priv *priv = hw->priv;
Johannes Bergfd1af152010-04-30 11:30:43 -07003371 struct iwl3945_sta_priv *sta_priv = (void *)sta->drv_priv;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003372 int ret;
Johannes Bergfd1af152010-04-30 11:30:43 -07003373 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003374 u8 sta_id;
3375
3376 IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
3377 sta->addr);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003378 mutex_lock(&priv->mutex);
3379 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
3380 sta->addr);
3381 sta_priv->common.sta_id = IWL_INVALID_STATION;
3382
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003383
3384 ret = iwl_add_station_common(priv, sta->addr, is_ap, &sta->ht_cap,
3385 &sta_id);
3386 if (ret) {
3387 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
3388 sta->addr, ret);
3389 /* Should we return success if return code is EEXIST ? */
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003390 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003391 return ret;
3392 }
3393
Johannes Bergfd1af152010-04-30 11:30:43 -07003394 sta_priv->common.sta_id = sta_id;
3395
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003396 /* Initialize rate scaling */
Frans Pop91dd6c22010-03-24 14:19:58 -07003397 IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003398 sta->addr);
3399 iwl3945_rs_rate_init(priv, sta, sta_id);
Reinette Chatreda5ae1c2010-05-28 09:28:39 -07003400 mutex_unlock(&priv->mutex);
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003401
3402 return 0;
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003403}
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003404
3405static void iwl3945_configure_filter(struct ieee80211_hw *hw,
3406 unsigned int changed_flags,
3407 unsigned int *total_flags,
3408 u64 multicast)
3409{
3410 struct iwl_priv *priv = hw->priv;
3411 __le32 filter_or = 0, filter_nand = 0;
Johannes Berg246ed352010-08-23 10:46:32 +02003412 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003413
3414#define CHK(test, flag) do { \
3415 if (*total_flags & (test)) \
3416 filter_or |= (flag); \
3417 else \
3418 filter_nand |= (flag); \
3419 } while (0)
3420
3421 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
3422 changed_flags, *total_flags);
3423
3424 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
3425 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
3426 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
3427
3428#undef CHK
3429
3430 mutex_lock(&priv->mutex);
3431
Johannes Berg246ed352010-08-23 10:46:32 +02003432 ctx->staging.filter_flags &= ~filter_nand;
3433 ctx->staging.filter_flags |= filter_or;
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003434
3435 /*
3436 * Committing directly here breaks for some reason,
3437 * but we'll eventually commit the filter flags
3438 * change anyway.
3439 */
3440
3441 mutex_unlock(&priv->mutex);
3442
3443 /*
3444 * Receiving all multicast frames is always enabled by the
3445 * default flags setup in iwl_connection_init_rx_config()
3446 * since we currently do not support programming multicast
3447 * filters into the device.
3448 */
3449 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
3450 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
3451}
3452
3453
Zhu Yib481de92007-09-25 17:54:57 -07003454/*****************************************************************************
3455 *
3456 * sysfs attributes
3457 *
3458 *****************************************************************************/
3459
Samuel Ortizd08853a2009-01-23 13:45:17 -08003460#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003461
3462/*
3463 * The following adds a new attribute to the sysfs representation
3464 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3465 * used for controlling the debug level.
3466 *
3467 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003468 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003469 * The debug_level being managed using sysfs below is a per device debug
3470 * level that is used instead of the global debug level if it (the per
3471 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003472 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003473static ssize_t show_debug_level(struct device *d,
3474 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003475{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003476 struct iwl_priv *priv = dev_get_drvdata(d);
3477 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003478}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003479static ssize_t store_debug_level(struct device *d,
3480 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003481 const char *buf, size_t count)
3482{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003483 struct iwl_priv *priv = dev_get_drvdata(d);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003484 unsigned long val;
3485 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003486
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003487 ret = strict_strtoul(buf, 0, &val);
3488 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003489 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003490 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003491 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003492 if (iwl_alloc_traffic_mem(priv))
3493 IWL_ERR(priv,
3494 "Not enough memory to generate traffic log\n");
3495 }
Zhu Yib481de92007-09-25 17:54:57 -07003496 return strnlen(buf, count);
3497}
3498
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003499static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3500 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07003501
Samuel Ortizd08853a2009-01-23 13:45:17 -08003502#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003503
Zhu Yib481de92007-09-25 17:54:57 -07003504static ssize_t show_temperature(struct device *d,
3505 struct device_attribute *attr, char *buf)
3506{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003507 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003508
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003509 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003510 return -EAGAIN;
3511
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003512 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003513}
3514
3515static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3516
Zhu Yib481de92007-09-25 17:54:57 -07003517static ssize_t show_tx_power(struct device *d,
3518 struct device_attribute *attr, char *buf)
3519{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003520 struct iwl_priv *priv = dev_get_drvdata(d);
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003521 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003522}
3523
3524static ssize_t store_tx_power(struct device *d,
3525 struct device_attribute *attr,
3526 const char *buf, size_t count)
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 char *p = (char *)buf;
3530 u32 val;
3531
3532 val = simple_strtoul(p, &p, 10);
3533 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08003534 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07003535 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003536 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07003537
3538 return count;
3539}
3540
3541static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3542
3543static ssize_t show_flags(struct device *d,
3544 struct device_attribute *attr, char *buf)
3545{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003546 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003547 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003548
Johannes Berg246ed352010-08-23 10:46:32 +02003549 return sprintf(buf, "0x%04X\n", ctx->active.flags);
Zhu Yib481de92007-09-25 17:54:57 -07003550}
3551
3552static ssize_t store_flags(struct device *d,
3553 struct device_attribute *attr,
3554 const char *buf, size_t count)
3555{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003556 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003557 u32 flags = simple_strtoul(buf, NULL, 0);
Johannes Berg246ed352010-08-23 10:46:32 +02003558 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003559
3560 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003561 if (le32_to_cpu(ctx->staging.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003562 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003563 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003564 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003565 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003566 IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
Zhu Yib481de92007-09-25 17:54:57 -07003567 flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003568 ctx->staging.flags = cpu_to_le32(flags);
3569 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003570 }
3571 }
3572 mutex_unlock(&priv->mutex);
3573
3574 return count;
3575}
3576
3577static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3578
3579static ssize_t show_filter_flags(struct device *d,
3580 struct device_attribute *attr, char *buf)
3581{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003582 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003583 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003584
3585 return sprintf(buf, "0x%04X\n",
Johannes Berg246ed352010-08-23 10:46:32 +02003586 le32_to_cpu(ctx->active.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003587}
3588
3589static ssize_t store_filter_flags(struct device *d,
3590 struct device_attribute *attr,
3591 const char *buf, size_t count)
3592{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003593 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003594 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003595 u32 filter_flags = simple_strtoul(buf, NULL, 0);
3596
3597 mutex_lock(&priv->mutex);
Johannes Berg246ed352010-08-23 10:46:32 +02003598 if (le32_to_cpu(ctx->staging.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07003599 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08003600 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003601 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003602 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003603 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003604 "0x%04X\n", filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003605 ctx->staging.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07003606 cpu_to_le32(filter_flags);
Johannes Berg246ed352010-08-23 10:46:32 +02003607 iwlcore_commit_rxon(priv, ctx);
Zhu Yib481de92007-09-25 17:54:57 -07003608 }
3609 }
3610 mutex_unlock(&priv->mutex);
3611
3612 return count;
3613}
3614
3615static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3616 store_filter_flags);
3617
Zhu Yib481de92007-09-25 17:54:57 -07003618static ssize_t show_measurement(struct device *d,
3619 struct device_attribute *attr, char *buf)
3620{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003621 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003622 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003623 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003624 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07003625 unsigned long flags;
3626
3627 spin_lock_irqsave(&priv->lock, flags);
3628 if (!(priv->measurement_status & MEASUREMENT_READY)) {
3629 spin_unlock_irqrestore(&priv->lock, flags);
3630 return 0;
3631 }
3632 memcpy(&measure_report, &priv->measure_report, size);
3633 priv->measurement_status = 0;
3634 spin_unlock_irqrestore(&priv->lock, flags);
3635
3636 while (size && (PAGE_SIZE - len)) {
3637 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3638 PAGE_SIZE - len, 1);
3639 len = strlen(buf);
3640 if (PAGE_SIZE - len)
3641 buf[len++] = '\n';
3642
3643 ofs += 16;
3644 size -= min(size, 16U);
3645 }
3646
3647 return len;
3648}
3649
3650static ssize_t store_measurement(struct device *d,
3651 struct device_attribute *attr,
3652 const char *buf, size_t count)
3653{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003654 struct iwl_priv *priv = dev_get_drvdata(d);
Johannes Berg246ed352010-08-23 10:46:32 +02003655 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Zhu Yib481de92007-09-25 17:54:57 -07003656 struct ieee80211_measurement_params params = {
Johannes Berg246ed352010-08-23 10:46:32 +02003657 .channel = le16_to_cpu(ctx->active.channel),
Johannes Berge99f168c2010-01-19 10:04:28 -08003658 .start_time = cpu_to_le64(priv->_3945.last_tsf),
Zhu Yib481de92007-09-25 17:54:57 -07003659 .duration = cpu_to_le16(1),
3660 };
3661 u8 type = IWL_MEASURE_BASIC;
3662 u8 buffer[32];
3663 u8 channel;
3664
3665 if (count) {
3666 char *p = buffer;
3667 strncpy(buffer, buf, min(sizeof(buffer), count));
3668 channel = simple_strtoul(p, NULL, 0);
3669 if (channel)
3670 params.channel = channel;
3671
3672 p = buffer;
3673 while (*p && *p != ' ')
3674 p++;
3675 if (*p)
3676 type = simple_strtoul(p + 1, NULL, 0);
3677 }
3678
Tomas Winklere1623442009-01-27 14:27:56 -08003679 IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
Zhu Yib481de92007-09-25 17:54:57 -07003680 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003681 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07003682
3683 return count;
3684}
3685
3686static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3687 show_measurement, store_measurement);
Zhu Yib481de92007-09-25 17:54:57 -07003688
Zhu Yib481de92007-09-25 17:54:57 -07003689static ssize_t store_retry_rate(struct device *d,
3690 struct device_attribute *attr,
3691 const char *buf, size_t count)
3692{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003693 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003694
3695 priv->retry_rate = simple_strtoul(buf, NULL, 0);
3696 if (priv->retry_rate <= 0)
3697 priv->retry_rate = 1;
3698
3699 return count;
3700}
3701
3702static ssize_t show_retry_rate(struct device *d,
3703 struct device_attribute *attr, char *buf)
3704{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003705 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003706 return sprintf(buf, "%d", priv->retry_rate);
3707}
3708
3709static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3710 store_retry_rate);
3711
Winkler, Tomasd25aabb2009-01-27 14:27:58 -08003712
Zhu Yib481de92007-09-25 17:54:57 -07003713static ssize_t show_channels(struct device *d,
3714 struct device_attribute *attr, char *buf)
3715{
Johannes Berg8318d782008-01-24 19:38:38 +01003716 /* all this shit doesn't belong into sysfs anyway */
3717 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07003718}
3719
3720static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3721
Zhu Yib481de92007-09-25 17:54:57 -07003722static ssize_t show_antenna(struct device *d,
3723 struct device_attribute *attr, char *buf)
3724{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003725 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003726
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003727 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003728 return -EAGAIN;
3729
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003730 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07003731}
3732
3733static ssize_t store_antenna(struct device *d,
3734 struct device_attribute *attr,
3735 const char *buf, size_t count)
3736{
Winkler, Tomas7530f852009-01-27 14:27:53 -08003737 struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003738 int ant;
Zhu Yib481de92007-09-25 17:54:57 -07003739
3740 if (count == 0)
3741 return 0;
3742
3743 if (sscanf(buf, "%1i", &ant) != 1) {
Tomas Winklere1623442009-01-27 14:27:56 -08003744 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003745 return count;
3746 }
3747
3748 if ((ant >= 0) && (ant <= 2)) {
Tomas Winklere1623442009-01-27 14:27:56 -08003749 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08003750 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07003751 } else
Tomas Winklere1623442009-01-27 14:27:56 -08003752 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
Zhu Yib481de92007-09-25 17:54:57 -07003753
3754
3755 return count;
3756}
3757
3758static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
3759
3760static ssize_t show_status(struct device *d,
3761 struct device_attribute *attr, char *buf)
3762{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003763 struct iwl_priv *priv = dev_get_drvdata(d);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003764 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003765 return -EAGAIN;
3766 return sprintf(buf, "0x%08x\n", (int)priv->status);
3767}
3768
3769static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3770
3771static ssize_t dump_error_log(struct device *d,
3772 struct device_attribute *attr,
3773 const char *buf, size_t count)
3774{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003775 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003776 char *p = (char *)buf;
3777
3778 if (p[0] == '1')
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003779 iwl3945_dump_nic_error_log(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003780
3781 return strnlen(buf, count);
3782}
3783
3784static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
3785
Zhu Yib481de92007-09-25 17:54:57 -07003786/*****************************************************************************
3787 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07003788 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07003789 *
3790 *****************************************************************************/
3791
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003792static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003793{
Reinette Chatred21050c2009-02-13 11:51:18 -08003794 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003795
3796 init_waitqueue_head(&priv->wait_command_queue);
3797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798 INIT_WORK(&priv->restart, iwl3945_bg_restart);
3799 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003800 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003801 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
3802 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Johannes Bergee525d12010-01-21 06:09:28 -08003803 INIT_DELAYED_WORK(&priv->_3945.rfkill_poll, iwl3945_rfkill_poll);
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02003804
3805 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003807 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003808
Wey-Yi Guyb74e31a2010-03-01 17:23:50 -08003809 if (priv->cfg->ops->lib->recover_from_tx_stall) {
3810 init_timer(&priv->monitor_recover);
3811 priv->monitor_recover.data = (unsigned long)priv;
3812 priv->monitor_recover.function =
3813 priv->cfg->ops->lib->recover_from_tx_stall;
3814 }
3815
Zhu Yib481de92007-09-25 17:54:57 -07003816 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003817 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003818}
3819
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003820static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003821{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003822 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003823
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09003824 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003825 cancel_delayed_work(&priv->scan_check);
3826 cancel_delayed_work(&priv->alive_start);
Abhijeet Kolekar1e460532010-04-23 14:14:53 -07003827 cancel_work_sync(&priv->start_internal_scan);
Zhu Yib481de92007-09-25 17:54:57 -07003828 cancel_work_sync(&priv->beacon_update);
3829}
3830
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003831static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003832 &dev_attr_antenna.attr,
3833 &dev_attr_channels.attr,
3834 &dev_attr_dump_errors.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003835 &dev_attr_flags.attr,
3836 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003837 &dev_attr_measurement.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003838 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003839 &dev_attr_status.attr,
3840 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003841 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08003842#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003843 &dev_attr_debug_level.attr,
3844#endif
Zhu Yib481de92007-09-25 17:54:57 -07003845 NULL
3846};
3847
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003848static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003849 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003850 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003851};
3852
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003853static struct ieee80211_ops iwl3945_hw_ops = {
3854 .tx = iwl3945_mac_tx,
3855 .start = iwl3945_mac_start,
3856 .stop = iwl3945_mac_stop,
Abhijeet Kolekarcbb6ab92009-04-08 11:26:46 -07003857 .add_interface = iwl_mac_add_interface,
Abhijeet Kolekard8052312009-04-08 11:26:47 -07003858 .remove_interface = iwl_mac_remove_interface,
Abhijeet Kolekar48083682009-04-08 11:26:48 -07003859 .config = iwl_mac_config,
Johannes Berg8b8ab9d2010-08-17 11:24:01 +02003860 .configure_filter = iwl3945_configure_filter,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003861 .set_key = iwl3945_mac_set_key,
Abhijeet Kolekar488829f2009-03-26 10:14:10 -07003862 .conf_tx = iwl_mac_conf_tx,
Abhijeet Kolekarbd564262009-04-08 11:26:52 -07003863 .reset_tsf = iwl_mac_reset_tsf,
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07003864 .bss_info_changed = iwl_bss_info_changed,
Reinette Chatrefe6b23d2010-02-22 16:24:47 -08003865 .hw_scan = iwl_mac_hw_scan,
3866 .sta_add = iwl3945_mac_sta_add,
3867 .sta_remove = iwl_mac_sta_remove,
Johannes Berga85d7cc2010-07-31 08:34:10 -07003868 .tx_last_beacon = iwl_mac_tx_last_beacon,
Zhu Yib481de92007-09-25 17:54:57 -07003869};
3870
Winkler, Tomase52119c2008-12-22 11:31:19 +08003871static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003872{
3873 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08003874 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003875
3876 priv->retry_rate = 1;
3877 priv->ibss_beacon = NULL;
3878
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003879 spin_lock_init(&priv->sta_lock);
3880 spin_lock_init(&priv->hcmd_lock);
3881
3882 INIT_LIST_HEAD(&priv->free_frames);
3883
3884 mutex_init(&priv->mutex);
Reinette Chatred2dfe6d2010-02-18 22:03:04 -08003885 mutex_init(&priv->sync_cmd_mutex);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003886
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003887 priv->ieee_channels = NULL;
3888 priv->ieee_rates = NULL;
3889 priv->band = IEEE80211_BAND_2GHZ;
3890
3891 priv->iw_mode = NL80211_IFTYPE_STATION;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003892 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003893
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08003894 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003895
Samuel Ortize6148912009-01-23 13:45:15 -08003896 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
3897 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
3898 eeprom->version);
3899 ret = -EINVAL;
3900 goto err;
3901 }
3902 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003903 if (ret) {
3904 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3905 goto err;
3906 }
3907
Samuel Ortize6148912009-01-23 13:45:15 -08003908 /* Set up txpower settings in driver for all channels */
3909 if (iwl3945_txpower_set_from_eeprom(priv)) {
3910 ret = -EIO;
3911 goto err_free_channel_map;
3912 }
3913
Samuel Ortiz534166d2009-01-23 13:45:16 -08003914 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003915 if (ret) {
3916 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3917 goto err_free_channel_map;
3918 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08003919 iwl3945_init_hw_rates(priv, priv->ieee_rates);
3920
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003921 return 0;
3922
3923err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08003924 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003925err:
3926 return ret;
3927}
3928
Johannes Bergdd7a2502010-04-28 23:33:10 -07003929#define IWL3945_MAX_PROBE_REQUEST 200
3930
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003931static int iwl3945_setup_mac(struct iwl_priv *priv)
3932{
3933 int ret;
3934 struct ieee80211_hw *hw = priv->hw;
3935
3936 hw->rate_control_algorithm = "iwl-3945-rs";
3937 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
Johannes Bergfd1af152010-04-30 11:30:43 -07003938 hw->vif_data_size = sizeof(struct iwl_vif_priv);
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003939
3940 /* Tell mac80211 our characteristics */
3941 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Reinette Chatrebc45a672009-12-14 14:12:10 -08003942 IEEE80211_HW_SPECTRUM_MGMT;
3943
3944 if (!priv->cfg->broken_powersave)
3945 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
3946 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003947
3948 hw->wiphy->interface_modes =
3949 BIT(NL80211_IFTYPE_STATION) |
3950 BIT(NL80211_IFTYPE_ADHOC);
3951
Reinette Chatref6c8f152010-03-12 11:13:26 -08003952 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
Johannes Berg5be83de2009-11-19 00:56:28 +01003953 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Luis R. Rodriguez37184242009-07-30 17:43:48 -07003954
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07003955 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
3956 /* we create the 802.11 header and a zero-length SSID element */
Johannes Bergdd7a2502010-04-28 23:33:10 -07003957 hw->wiphy->max_scan_ie_len = IWL3945_MAX_PROBE_REQUEST - 24 - 2;
Johannes Bergd60cc912009-04-09 09:56:02 +02003958
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003959 /* Default value; 4 EDCA QOS priorities */
3960 hw->queues = 4;
3961
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08003962 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
3963 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3964 &priv->bands[IEEE80211_BAND_2GHZ];
3965
3966 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
3967 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
3968 &priv->bands[IEEE80211_BAND_5GHZ];
3969
3970 ret = ieee80211_register_hw(priv->hw);
3971 if (ret) {
3972 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
3973 return ret;
3974 }
3975 priv->mac80211_registered = 1;
3976
3977 return 0;
3978}
3979
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003980static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003981{
Johannes Berg246ed352010-08-23 10:46:32 +02003982 int err = 0, i;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003983 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003984 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08003985 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08003986 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003987 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003988
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08003989 /***********************
3990 * 1. Allocating HW data
3991 * ********************/
3992
Zhu Yib481de92007-09-25 17:54:57 -07003993 /* mac80211 allocates memory for this device instance, including
3994 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08003995 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07003996 if (hw == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003997 pr_err("Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07003998 err = -ENOMEM;
3999 goto out;
4000 }
Zhu Yib481de92007-09-25 17:54:57 -07004001 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004002 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004003
Johannes Berg13bb9482010-08-23 10:46:33 +02004004 priv->cmd_queue = IWL39_CMD_QUEUE_NUM;
4005
Johannes Berg246ed352010-08-23 10:46:32 +02004006 /* 3945 has only one valid context */
4007 priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
4008
4009 for (i = 0; i < NUM_IWL_RXON_CTX; i++)
4010 priv->contexts[i].ctxid = i;
4011
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004012 /*
4013 * Disabling hardware scan means that mac80211 will perform scans
4014 * "the hard way", rather than using device's scan.
4015 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004016 if (iwl3945_mod_params.disable_hw_scan) {
Tomas Winklere1623442009-01-27 14:27:56 -08004017 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004018 iwl3945_hw_ops.hw_scan = NULL;
4019 }
4020
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004021
Tomas Winklere1623442009-01-27 14:27:56 -08004022 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004023 priv->cfg = cfg;
4024 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07004025 priv->inta_mask = CSR_INI_SET_MASK;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004026
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004027 if (iwl_alloc_traffic_mem(priv))
4028 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07004029
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004030 /***************************
4031 * 2. Initializing PCI bus
4032 * *************************/
John W. Linville1a7123c2010-08-05 14:39:31 -04004033 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4034 PCIE_LINK_STATE_CLKPM);
4035
Zhu Yib481de92007-09-25 17:54:57 -07004036 if (pci_enable_device(pdev)) {
4037 err = -ENODEV;
4038 goto out_ieee80211_free_hw;
4039 }
4040
4041 pci_set_master(pdev);
4042
Yang Hongyang284901a2009-04-06 19:01:15 -07004043 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004044 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004045 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Zhu Yib481de92007-09-25 17:54:57 -07004046 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004047 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004048 goto out_pci_disable_device;
4049 }
4050
4051 pci_set_drvdata(pdev, priv);
4052 err = pci_request_regions(pdev, DRV_NAME);
4053 if (err)
4054 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004055
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004056 /***********************
4057 * 3. Read REV Register
4058 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07004059 priv->hw_base = pci_iomap(pdev, 0, 0);
4060 if (!priv->hw_base) {
4061 err = -ENODEV;
4062 goto out_pci_release_regions;
4063 }
4064
Tomas Winklere1623442009-01-27 14:27:56 -08004065 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07004066 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08004067 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Zhu Yib481de92007-09-25 17:54:57 -07004068
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004069 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4070 * PCI Tx retries from interfering with C3 CPU state */
4071 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07004072
Reinette Chatre731a29b2009-12-14 14:12:11 -08004073 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004074 * we should init now
4075 */
4076 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08004077 spin_lock_init(&priv->lock);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07004078
Reinette Chatre4843b5a2010-02-03 09:38:59 -08004079 /*
4080 * stop and reset the on-board processor just in case it is in a
4081 * strange state ... like being left stranded by a primary kernel
4082 * and this is now the kdump kernel trying to start up
4083 */
4084 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4085
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004086 /***********************
4087 * 4. Read EEPROM
4088 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004089
Zhu Yi5a66926a2008-01-14 17:46:18 -08004090 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08004091 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004092 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004093 IWL_ERR(priv, "Unable to init EEPROM\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004094 goto out_iounmap;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004095 }
4096 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08004097 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Johannes Berg30eabc12010-05-12 03:33:10 -07004098 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", eeprom->mac_address);
4099 SET_IEEE80211_PERM_ADDR(priv->hw, eeprom->mac_address);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004100
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004101 /***********************
4102 * 5. Setup HW Constants
4103 * ********************/
4104 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004105 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004106 IWL_ERR(priv, "failed to set hw settings\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004107 goto out_eeprom_free;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004108 }
4109
4110 /***********************
4111 * 6. Setup priv
4112 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004113
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004114 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004115 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004116 IWL_ERR(priv, "initializing driver failed\n");
Reinette Chatrec8f16132009-02-27 16:21:22 -08004117 goto out_unset_hw_params;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004118 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004119
Tomas Winkler978785a2008-12-19 10:37:31 +08004120 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
4121 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004122
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004123 /***********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004124 * 7. Setup Services
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004125 * ********************/
4126
4127 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004128 iwl_disable_interrupts(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004129 spin_unlock_irqrestore(&priv->lock, flags);
4130
Helmut Schaa26635162009-01-15 09:38:44 +01004131 pci_enable_msi(priv->pci_dev);
4132
Mohamed Abbasef850d72009-05-22 11:01:50 -07004133 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
4134 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004135 if (err) {
4136 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
4137 goto out_disable_msi;
4138 }
4139
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004140 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4141 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004142 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08004143 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004144 }
4145
Samuel Ortiz8ccde882009-01-27 14:27:52 -08004146 iwl_set_rxon_channel(priv,
Johannes Berg246ed352010-08-23 10:46:32 +02004147 &priv->bands[IEEE80211_BAND_2GHZ].channels[5],
4148 &priv->contexts[IWL_RXON_CTX_BSS]);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004149 iwl3945_setup_deferred_work(priv);
4150 iwl3945_setup_rx_handlers(priv);
Ben Cahill008a9e32009-10-09 13:20:27 -07004151 iwl_power_initialize(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004152
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004153 /*********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07004154 * 8. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004155 * *******************************/
4156
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004157 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004158
Abhijeet Kolekar2a4ddaa2009-02-27 16:21:23 -08004159 err = iwl3945_setup_mac(priv);
4160 if (err)
4161 goto out_remove_sysfs;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004162
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004163 err = iwl_dbgfs_register(priv, DRV_NAME);
4164 if (err)
4165 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
4166
Helmut Schaa26635162009-01-15 09:38:44 +01004167 /* Start monitoring the killswitch */
Johannes Bergee525d12010-01-21 06:09:28 -08004168 queue_delayed_work(priv->workqueue, &priv->_3945.rfkill_poll,
Helmut Schaa26635162009-01-15 09:38:44 +01004169 2 * HZ);
4170
Zhu Yib481de92007-09-25 17:54:57 -07004171 return 0;
4172
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08004173 out_remove_sysfs:
Zhu Yib481de92007-09-25 17:54:57 -07004174 destroy_workqueue(priv->workqueue);
4175 priv->workqueue = NULL;
Reinette Chatrec8f16132009-02-27 16:21:22 -08004176 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
4177 out_release_irq:
4178 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa26635162009-01-15 09:38:44 +01004179 out_disable_msi:
4180 pci_disable_msi(priv->pci_dev);
Reinette Chatrec8f16132009-02-27 16:21:22 -08004181 iwlcore_free_geos(priv);
4182 iwl_free_channel_map(priv);
4183 out_unset_hw_params:
4184 iwl3945_unset_hw_params(priv);
4185 out_eeprom_free:
4186 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004187 out_iounmap:
4188 pci_iounmap(pdev, priv->hw_base);
4189 out_pci_release_regions:
4190 pci_release_regions(pdev);
4191 out_pci_disable_device:
Zhu Yib481de92007-09-25 17:54:57 -07004192 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08004193 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07004194 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004195 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07004196 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004197 out:
4198 return err;
4199}
4200
Reinette Chatrec83dbf62008-03-21 13:53:41 -07004201static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07004202{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004203 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004204 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07004205
4206 if (!priv)
4207 return;
4208
Tomas Winklere1623442009-01-27 14:27:56 -08004209 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07004210
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07004211 iwl_dbgfs_unregister(priv);
4212
Zhu Yib481de92007-09-25 17:54:57 -07004213 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08004214
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08004215 if (priv->mac80211_registered) {
4216 ieee80211_unregister_hw(priv->hw);
4217 priv->mac80211_registered = 0;
4218 } else {
4219 iwl3945_down(priv);
4220 }
Zhu Yib481de92007-09-25 17:54:57 -07004221
Ben Cahillc166b252009-10-23 13:42:35 -07004222 /*
4223 * Make sure device is reset to low power before unloading driver.
4224 * This may be redundant with iwl_down(), but there are paths to
4225 * run iwl_down() without calling apm_ops.stop(), and there are
4226 * paths to avoid running iwl_down() at all before leaving driver.
4227 * This (inexpensive) call *makes sure* device is reset.
4228 */
4229 priv->cfg->ops->lib->apm_ops.stop(priv);
4230
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004231 /* make sure we flush any pending irq or
4232 * tasklet for the driver
4233 */
4234 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekared3b9322009-02-18 15:54:30 -08004235 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004236 spin_unlock_irqrestore(&priv->lock, flags);
4237
4238 iwl_synchronize_irq(priv);
4239
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004240 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07004241
Johannes Bergee525d12010-01-21 06:09:28 -08004242 cancel_delayed_work_sync(&priv->_3945.rfkill_poll);
Helmut Schaa26635162009-01-15 09:38:44 +01004243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004244 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004245
4246 if (priv->rxq.bd)
Reinette Chatredf833b12009-04-21 10:55:48 -07004247 iwl3945_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004248 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004249
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004250 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004251
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004252 /*netif_stop_queue(dev); */
4253 flush_workqueue(priv->workqueue);
4254
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004255 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07004256 * priv->workqueue... so we can't take down the workqueue
4257 * until now... */
4258 destroy_workqueue(priv->workqueue);
4259 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07004260 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004261
Helmut Schaa26635162009-01-15 09:38:44 +01004262 free_irq(pdev->irq, priv);
4263 pci_disable_msi(pdev);
4264
Zhu Yib481de92007-09-25 17:54:57 -07004265 pci_iounmap(pdev, priv->hw_base);
4266 pci_release_regions(pdev);
4267 pci_disable_device(pdev);
4268 pci_set_drvdata(pdev, NULL);
4269
Samuel Ortize6148912009-01-23 13:45:15 -08004270 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08004271 iwlcore_free_geos(priv);
Johannes Berg811ecc92010-04-06 04:12:41 -07004272 kfree(priv->scan_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07004273 if (priv->ibss_beacon)
4274 dev_kfree_skb(priv->ibss_beacon);
4275
4276 ieee80211_free_hw(priv->hw);
4277}
4278
Zhu Yib481de92007-09-25 17:54:57 -07004279
4280/*****************************************************************************
4281 *
4282 * driver and module entry point
4283 *
4284 *****************************************************************************/
4285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004286static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07004287 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004288 .id_table = iwl3945_hw_card_ids,
4289 .probe = iwl3945_pci_probe,
4290 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07004291#ifdef CONFIG_PM
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07004292 .suspend = iwl_pci_suspend,
4293 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07004294#endif
4295};
4296
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004297static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07004298{
4299
4300 int ret;
Joe Perchesc96c31e2010-07-26 14:39:58 -07004301 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
4302 pr_info(DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004303
4304 ret = iwl3945_rate_control_register();
4305 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004306 pr_err("Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004307 return ret;
4308 }
4309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004310 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07004311 if (ret) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004312 pr_err("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004313 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07004314 }
Zhu Yib481de92007-09-25 17:54:57 -07004315
4316 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004317
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004318error_register:
4319 iwl3945_rate_control_unregister();
4320 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004321}
4322
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004323static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07004324{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004325 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07004326 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07004327}
4328
Reinette Chatrea0987a82008-12-02 12:14:06 -08004329MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08004330
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004331module_param_named(antenna, iwl3945_mod_params.antenna, int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004332MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004333module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, S_IRUGO);
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08004334MODULE_PARM_DESC(swcrypto,
4335 "using software crypto (default 1 [software])\n");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004336#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004337module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Zhu Yib481de92007-09-25 17:54:57 -07004338MODULE_PARM_DESC(debug, "debug output mask");
Reinette Chatrea562a9d2009-07-17 09:30:24 -07004339#endif
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004340module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan,
4341 int, S_IRUGO);
Zhu Yib481de92007-09-25 17:54:57 -07004342MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07004343module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, S_IRUGO);
Samuel Ortizaf48d042009-01-23 13:45:19 -08004344MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
4345
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004346module_exit(iwl3945_exit);
4347module_init(iwl3945_init);