blob: 8aaa6bfd128dc4f8c942b983b2a68a67587211ac [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2003 - 2009 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
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
Zhu Yib481de92007-09-25 17:54:57 -070032#include <linux/init.h>
33#include <linux/pci.h>
34#include <linux/dma-mapping.h>
35#include <linux/delay.h>
36#include <linux/skbuff.h>
37#include <linux/netdevice.h>
38#include <linux/wireless.h>
39#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070040#include <linux/etherdevice.h>
41#include <linux/if_arp.h>
42
43#include <net/ieee80211_radiotap.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040044#include <net/lib80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070045#include <net/mac80211.h>
46
47#include <asm/div64.h>
48
Samuel Ortiza3139c52008-12-19 10:37:09 +080049#define DRV_NAME "iwl3945"
50
Winkler, Tomasdbb66542008-12-22 11:31:14 +080051#include "iwl-fh.h"
52#include "iwl-3945-fh.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080053#include "iwl-commands.h"
Zhu Yib481de92007-09-25 17:54:57 -070054#include "iwl-3945.h"
55#include "iwl-helpers.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080056#include "iwl-core.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080057#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070058
Zhu Yib481de92007-09-25 17:54:57 -070059/*
60 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070061 */
62
63#define DRV_DESCRIPTION \
64"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
65
Samuel Ortizd08853a2009-01-23 13:45:17 -080066#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070067#define VD "d"
68#else
69#define VD
70#endif
71
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080072#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070073#define VS "s"
74#else
75#define VS
76#endif
77
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080078#define IWL39_VERSION "1.2.26k" VD VS
Reinette Chatre01f81622009-01-08 10:20:02 -080079#define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080080#define DRV_AUTHOR "<ilw@linux.intel.com>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080081#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070082
Zhu Yib481de92007-09-25 17:54:57 -070083
84MODULE_DESCRIPTION(DRV_DESCRIPTION);
85MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080086MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070087MODULE_LICENSE("GPL");
88
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080089 /* module parameters */
90struct iwl_mod_params iwl3945_mod_params = {
91 .num_of_queues = IWL39_MAX_NUM_QUEUES,
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -080092 .sw_crypto = 1,
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080093 /* the rest are 0 by default */
94};
95
Zhu Yib481de92007-09-25 17:54:57 -070096/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +080097 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -070098 * the functionality provided here
99 */
100
101/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800102#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800103/**
104 * iwl3945_remove_station - Remove driver's knowledge of station.
105 *
106 * NOTE: This does not remove station from device's station table.
107 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800108static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700109{
110 int index = IWL_INVALID_STATION;
111 int i;
112 unsigned long flags;
113
114 spin_lock_irqsave(&priv->sta_lock, flags);
115
116 if (is_ap)
117 index = IWL_AP_ID;
118 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800119 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700120 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800121 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800122 if (priv->stations_39[i].used &&
123 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700124 addr)) {
125 index = i;
126 break;
127 }
128
129 if (unlikely(index == IWL_INVALID_STATION))
130 goto out;
131
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800132 if (priv->stations_39[index].used) {
133 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700134 priv->num_stations--;
135 }
136
137 BUG_ON(priv->num_stations < 0);
138
139out:
140 spin_unlock_irqrestore(&priv->sta_lock, flags);
141 return 0;
142}
Zhu Yi556f8db2007-09-27 11:27:33 +0800143#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800144
145/**
146 * iwl3945_clear_stations_table - Clear the driver's station table
147 *
148 * NOTE: This does not clear or otherwise alter the device's station table.
149 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800150static void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700151{
152 unsigned long flags;
153
154 spin_lock_irqsave(&priv->sta_lock, flags);
155
156 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800157 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700158
159 spin_unlock_irqrestore(&priv->sta_lock, flags);
160}
161
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800162/**
163 * iwl3945_add_station - Add station to station tables in driver and device
164 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800165u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700166{
167 int i;
168 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800169 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700170 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800171 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700172
173 spin_lock_irqsave(&priv->sta_lock, flags_spin);
174 if (is_ap)
175 index = IWL_AP_ID;
176 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800177 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700178 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800179 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800180 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700181 addr)) {
182 index = i;
183 break;
184 }
185
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800186 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700187 index == IWL_INVALID_STATION)
188 index = i;
189 }
190
Ian Schram01ebd062007-10-25 17:15:22 +0800191 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700192 since they have different meaning */
193 if (unlikely(index == IWL_INVALID_STATION)) {
194 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
195 return index;
196 }
197
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800198 if (priv->stations_39[index].used &&
199 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700200 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
201 return index;
202 }
203
Johannes Berge1749612008-10-27 15:59:26 -0700204 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800205 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700206 station->used = 1;
207 priv->num_stations++;
208
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800209 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800210 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700211 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
212 station->sta.mode = 0;
213 station->sta.sta.sta_id = index;
214 station->sta.station_flags = 0;
215
Johannes Berg8318d782008-01-24 19:38:38 +0100216 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800217 rate = IWL_RATE_6M_PLCP;
218 else
219 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800220
221 /* Turn on both antennas for the station... */
222 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800223 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800224
Zhu Yib481de92007-09-25 17:54:57 -0700225 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800226
227 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800228 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700229 return index;
230
231}
232
Samuel Ortiz518099a2009-01-19 15:30:27 -0800233int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700234{
Samuel Ortiz518099a2009-01-19 15:30:27 -0800235 u32 val = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700236
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800237 struct iwl_host_cmd cmd = {
Samuel Ortiz518099a2009-01-19 15:30:27 -0800238 .id = REPLY_STATISTICS_CMD,
Zhu Yib481de92007-09-25 17:54:57 -0700239 .len = sizeof(val),
240 .data = &val,
241 };
242
Samuel Ortiz518099a2009-01-19 15:30:27 -0800243 return iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700244}
245
246/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800247 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100248 * @band: 2.4 or 5 GHz band
249 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700250
Johannes Berg8318d782008-01-24 19:38:38 +0100251 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700252 *
253 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100254 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700255 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800256static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100257 enum ieee80211_band band,
258 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700259{
Samuel Ortize6148912009-01-23 13:45:15 -0800260 if (!iwl_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700261 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100262 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700263 return -EINVAL;
264 }
265
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800266 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100267 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700268 return 0;
269
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800270 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100271 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800272 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700273 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800274 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700275
Johannes Berg8318d782008-01-24 19:38:38 +0100276 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700277
Johannes Berg8318d782008-01-24 19:38:38 +0100278 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700279
280 return 0;
281}
282
283/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800284 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700285 *
286 * NOTE: This is really only useful during development and can eventually
287 * be #ifdef'd out once the driver is stable and folks aren't actively
288 * making changes
289 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800290static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700291{
292 int error = 0;
293 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800294 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700295
296 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
297 error |= le32_to_cpu(rxon->flags &
298 (RXON_FLG_TGJ_NARROW_BAND_MSK |
299 RXON_FLG_RADAR_DETECT_MSK));
300 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800301 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700302 counter++, error);
303 } else {
304 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
305 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
306 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800307 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700308 counter++, error);
309 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
310 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800311 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700312 counter++, error);
313 }
314 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
315 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800316 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700317
318 /* make sure basic rates 6Mbps and 1Mbps are supported */
319 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
320 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
321 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800322 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700323
324 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
325 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800326 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700327
328 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
329 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
330 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800331 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700332 counter++, error);
333
334 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
335 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
336 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800337 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700338 counter++, error);
339
340 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
341 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
342 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800343 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700344 counter++, error);
345
346 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
347 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
348 RXON_FLG_ANT_A_MSK)) == 0);
349 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800350 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700351
352 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800353 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700354 le16_to_cpu(rxon->channel));
355
356 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800357 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700358 return -1;
359 }
360 return 0;
361}
362
363/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800364 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800365 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700366 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800367 * If the RXON structure is changing enough to require a new tune,
368 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
369 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700370 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800371static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700372{
373
374 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800375 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800376 compare_ether_addr(priv->staging39_rxon.bssid_addr,
377 priv->active39_rxon.bssid_addr) ||
378 compare_ether_addr(priv->staging39_rxon.node_addr,
379 priv->active39_rxon.node_addr) ||
380 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
381 priv->active39_rxon.wlap_bssid_addr) ||
382 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
383 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
384 (priv->staging39_rxon.air_propagation !=
385 priv->active39_rxon.air_propagation) ||
386 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700387 return 1;
388
389 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
390 * be updated with the RXON_ASSOC command -- however only some
391 * flag transitions are allowed using RXON_ASSOC */
392
393 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800394 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
395 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700396 return 1;
397
398 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800399 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
400 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700401 return 1;
402
403 return 0;
404}
405
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800406static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700407{
408 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800409 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800410 struct iwl3945_rxon_assoc_cmd rxon_assoc;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800411 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700412 .id = REPLY_RXON_ASSOC,
413 .len = sizeof(rxon_assoc),
414 .meta.flags = CMD_WANT_SKB,
415 .data = &rxon_assoc,
416 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800417 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
418 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700419
420 if ((rxon1->flags == rxon2->flags) &&
421 (rxon1->filter_flags == rxon2->filter_flags) &&
422 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
423 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
424 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
425 return 0;
426 }
427
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800428 rxon_assoc.flags = priv->staging39_rxon.flags;
429 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
430 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
431 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700432 rxon_assoc.reserved = 0;
433
Samuel Ortiz518099a2009-01-19 15:30:27 -0800434 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700435 if (rc)
436 return rc;
437
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800438 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700439 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800440 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700441 rc = -EIO;
442 }
443
444 priv->alloc_rxb_skb--;
445 dev_kfree_skb_any(cmd.meta.u.skb);
446
447 return rc;
448}
449
450/**
Samuel Ortiz7e4bca52009-01-23 13:45:18 -0800451 * iwl3945_get_antenna_flags - Get antenna flags for RXON command
452 * @priv: eeprom and antenna fields are used to determine antenna flags
453 *
454 * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
455 * iwl3945_mod_params.antenna specifies the antenna diversity mode:
456 *
457 * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
458 * IWL_ANTENNA_MAIN - Force MAIN antenna
459 * IWL_ANTENNA_AUX - Force AUX antenna
460 */
461__le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
462{
463 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
464
465 switch (iwl3945_mod_params.antenna) {
466 case IWL_ANTENNA_DIVERSITY:
467 return 0;
468
469 case IWL_ANTENNA_MAIN:
470 if (eeprom->antenna_switch_type)
471 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
472 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
473
474 case IWL_ANTENNA_AUX:
475 if (eeprom->antenna_switch_type)
476 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
477 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
478 }
479
480 /* bad antenna selector value */
481 IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
482 iwl3945_mod_params.antenna);
483
484 return 0; /* "diversity" is default if error */
485}
486
487/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800488 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700489 *
Ian Schram01ebd062007-10-25 17:15:22 +0800490 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700491 * the active_rxon structure is updated with the new data. This
492 * function correctly transitions out of the RXON_ASSOC_MSK state if
493 * a HW tune is required based on the RXON structure changes.
494 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800495static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700496{
497 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800498 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700499 int rc = 0;
500
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800501 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700502 return -1;
503
504 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800505 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700506
507 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800508 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700509 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800510 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700511
Samuel Ortiza3139c52008-12-19 10:37:09 +0800512 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700513 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800514 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700515 return -EINVAL;
516 }
517
518 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800519 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700520 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800521 if (!iwl3945_full_rxon_required(priv)) {
522 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700523 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800524 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700525 "configuration (%d).\n", rc);
526 return rc;
527 }
528
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800529 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700530
531 return 0;
532 }
533
534 /* If we are currently associated and the new config requires
535 * an RXON_ASSOC and the new config wants the associated mask enabled,
536 * we must clear the associated from the active configuration
537 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800538 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800539 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700540 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
541 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
542
Samuel Ortiz518099a2009-01-19 15:30:27 -0800543 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800544 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800545 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700546
547 /* If the mask clearing failed then we set
548 * active_rxon back to what it was previously */
549 if (rc) {
550 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800551 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -0700552 "configuration (%d).\n", rc);
553 return rc;
554 }
Zhu Yib481de92007-09-25 17:54:57 -0700555 }
556
557 IWL_DEBUG_INFO("Sending RXON\n"
558 "* with%s RXON_FILTER_ASSOC_MSK\n"
559 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700560 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800561 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -0700562 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800563 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700564 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700565
566 /* Apply the new configuration */
Samuel Ortiz518099a2009-01-19 15:30:27 -0800567 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800568 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700569 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800570 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700571 return rc;
572 }
573
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800574 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700575
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800576 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800577
Zhu Yib481de92007-09-25 17:54:57 -0700578 /* If we issue a new RXON command which required a tune then we must
579 * send a new TXPOWER command or we won't be able to Tx any frames */
Samuel Ortiz75bcfae2009-01-23 13:45:11 -0800580 rc = priv->cfg->ops->lib->send_tx_power(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700581 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800582 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700583 return rc;
584 }
585
586 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +0800587 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700588 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800589 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700590 return -EIO;
591 }
592
593 /* If we have set the ASSOC_MSK and we are in BSS mode then
594 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800595 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200596 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800597 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -0700598 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800599 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -0700600 return -EIO;
601 }
602
Johannes Berg8318d782008-01-24 19:38:38 +0100603 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -0700604 rc = iwl3945_init_hw_rate_table(priv);
605 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800606 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700607 return -EIO;
608 }
609
610 return 0;
611}
612
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800613static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700614{
Tomas Winkler4c897252008-12-19 10:37:05 +0800615 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700616 .flags = 3,
617 .lead_time = 0xAA,
618 .max_kill = 1,
619 .kill_ack_mask = 0,
620 .kill_cts_mask = 0,
621 };
622
Samuel Ortiz518099a2009-01-19 15:30:27 -0800623 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +0800624 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700625}
626
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800627static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800628 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700629{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800630 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700631
632 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800633 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700634 return 1;
635 }
636
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800637 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700638 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800639 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -0700640 res->hdr.flags);
641 return 1;
642 }
643
644 switch (res->u.add_sta.status) {
645 case ADD_STA_SUCCESS_MSK:
646 break;
647 default:
648 break;
649 }
650
651 /* We didn't cache the SKB; let the caller free it */
652 return 1;
653}
654
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800655int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800656 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700657{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800658 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700659 int rc = 0;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800660 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700661 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800662 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700663 .meta.flags = flags,
664 .data = sta,
665 };
666
667 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800668 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -0700669 else
670 cmd.meta.flags |= CMD_WANT_SKB;
671
Samuel Ortiz518099a2009-01-19 15:30:27 -0800672 rc = iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700673
674 if (rc || (flags & CMD_ASYNC))
675 return rc;
676
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800677 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700678 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800679 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -0700680 res->hdr.flags);
681 rc = -EIO;
682 }
683
684 if (rc == 0) {
685 switch (res->u.add_sta.status) {
686 case ADD_STA_SUCCESS_MSK:
687 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
688 break;
689 default:
690 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800691 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700692 break;
693 }
694 }
695
696 priv->alloc_rxb_skb--;
697 dev_kfree_skb_any(cmd.meta.u.skb);
698
699 return rc;
700}
701
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800702static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700703 struct ieee80211_key_conf *keyconf,
704 u8 sta_id)
705{
706 unsigned long flags;
707 __le16 key_flags = 0;
708
709 switch (keyconf->alg) {
710 case ALG_CCMP:
711 key_flags |= STA_KEY_FLG_CCMP;
712 key_flags |= cpu_to_le16(
713 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
714 key_flags &= ~STA_KEY_FLG_INVALID;
715 break;
716 case ALG_TKIP:
717 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -0700718 default:
719 return -EINVAL;
720 }
721 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800722 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
723 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
724 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700725 keyconf->keylen);
726
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800727 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -0700728 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800729 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
730 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
731 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700732
733 spin_unlock_irqrestore(&priv->sta_lock, flags);
734
735 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800736 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700737 return 0;
738}
739
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800740static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700741{
742 unsigned long flags;
743
744 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800745 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
746 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +0800747 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800748 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
749 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
750 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700751 spin_unlock_irqrestore(&priv->sta_lock, flags);
752
753 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800754 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700755 return 0;
756}
757
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800758static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700759{
760 struct list_head *element;
761
762 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
763 priv->frames_count);
764
765 while (!list_empty(&priv->free_frames)) {
766 element = priv->free_frames.next;
767 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800768 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700769 priv->frames_count--;
770 }
771
772 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800773 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700774 priv->frames_count);
775 priv->frames_count = 0;
776 }
777}
778
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800779static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700780{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800781 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700782 struct list_head *element;
783 if (list_empty(&priv->free_frames)) {
784 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
785 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800786 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700787 return NULL;
788 }
789
790 priv->frames_count++;
791 return frame;
792 }
793
794 element = priv->free_frames.next;
795 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800796 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700797}
798
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800799static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700800{
801 memset(frame, 0, sizeof(*frame));
802 list_add(&frame->list, &priv->free_frames);
803}
804
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800805unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700806 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200807 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700808{
809
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800810 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200811 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
812 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -0700813 return 0;
814
815 if (priv->ibss_beacon->len > left)
816 return 0;
817
818 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
819
820 return priv->ibss_beacon->len;
821}
822
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800823static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700824{
825 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -0800826 int rate_mask;
827
828 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800829 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -0800830 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -0800831 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -0800832 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -0700833
834 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800835 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700836 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800837 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700838 }
839
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -0800840 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800841 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -0800842 return IWL_RATE_1M_PLCP;
843 else
844 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700845}
846
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800847static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700848{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800849 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700850 unsigned int frame_size;
851 int rc;
852 u8 rate;
853
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800854 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700855
856 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800857 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700858 "command.\n");
859 return -ENOMEM;
860 }
861
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -0800862 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700863
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800864 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700865
Samuel Ortiz518099a2009-01-19 15:30:27 -0800866 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700867 &frame->u.cmd[0]);
868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800869 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700870
871 return rc;
872}
873
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800874static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700875{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800876 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -0700877 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800878 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800879 priv->shared_virt,
880 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -0700881}
882
Zhu Yib481de92007-09-25 17:54:57 -0700883/*
884 * QoS support
885*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800886static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +0800887 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -0700888{
889
Samuel Ortiz518099a2009-01-19 15:30:27 -0800890 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +0800891 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -0700892}
893
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800894static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -0700895{
896 unsigned long flags;
897
Zhu Yib481de92007-09-25 17:54:57 -0700898 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
899 return;
900
Zhu Yib481de92007-09-25 17:54:57 -0700901 spin_lock_irqsave(&priv->lock, flags);
902 priv->qos_data.def_qos_parm.qos_flags = 0;
903
904 if (priv->qos_data.qos_cap.q_AP.queue_request &&
905 !priv->qos_data.qos_cap.q_AP.txop_request)
906 priv->qos_data.def_qos_parm.qos_flags |=
907 QOS_PARAM_FLG_TXOP_TYPE_MSK;
908
909 if (priv->qos_data.qos_active)
910 priv->qos_data.def_qos_parm.qos_flags |=
911 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
912
913 spin_unlock_irqrestore(&priv->lock, flags);
914
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800915 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -0700916 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -0700917 priv->qos_data.qos_active);
918
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800919 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700920 &(priv->qos_data.def_qos_parm));
921 }
922}
923
Zhu Yib481de92007-09-25 17:54:57 -0700924/*
925 * Power management (not Tx power!) functions
926 */
927#define MSEC_TO_USEC 1024
928
Tomas Winkler600c0e12008-12-19 10:37:04 +0800929
Zhu Yib481de92007-09-25 17:54:57 -0700930/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -0700931/* for TIM 0-10 */
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800932static struct iwl_power_vec_entry range_0[IWL_POWER_MAX] = {
933 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
934 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
935 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
936 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
937 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
938 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
Zhu Yib481de92007-09-25 17:54:57 -0700939};
940
Tomas Winklera96a27f2008-10-23 23:48:56 -0700941/* for TIM > 10 */
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800942static struct iwl_power_vec_entry range_1[IWL_POWER_MAX] = {
943 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
944 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
945 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
946 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
947 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
948 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
Zhu Yib481de92007-09-25 17:54:57 -0700949};
950
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800951int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700952{
953 int rc = 0, i;
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800954 struct iwl_power_mgr *pow_data;
955 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_MAX;
Zhu Yib481de92007-09-25 17:54:57 -0700956 u16 pci_pm;
957
958 IWL_DEBUG_POWER("Initialize power \n");
959
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800960 pow_data = &priv->power_data;
Zhu Yib481de92007-09-25 17:54:57 -0700961
962 memset(pow_data, 0, sizeof(*pow_data));
963
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800964 pow_data->dtim_period = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700965
966 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
967 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
968
969 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
970 if (rc != 0)
971 return 0;
972 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +0800973 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700974
975 IWL_DEBUG_POWER("adjust power command flags\n");
976
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800977 for (i = 0; i < IWL_POWER_MAX; i++) {
Zhu Yib481de92007-09-25 17:54:57 -0700978 cmd = &pow_data->pwr_range_0[i].cmd;
979
980 if (pci_pm & 0x1)
981 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
982 else
983 cmd->flags |= IWL_POWER_PCI_PM_MSK;
984 }
985 }
986 return rc;
987}
988
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800989static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +0800990 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -0700991{
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800992 struct iwl_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +0800993 struct iwl_power_vec_entry *range;
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800994 u32 max_sleep = 0;
995 int i;
Zhu Yib481de92007-09-25 17:54:57 -0700996 u8 period = 0;
Winkler, Tomas3dae0c42009-01-19 15:30:30 -0800997 bool skip;
Zhu Yib481de92007-09-25 17:54:57 -0700998
999 if (mode > IWL_POWER_INDEX_5) {
1000 IWL_DEBUG_POWER("Error invalid power mode \n");
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08001001 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001002 }
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08001003 pow_data = &priv->power_data;
Zhu Yib481de92007-09-25 17:54:57 -07001004
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08001005 if (pow_data->dtim_period < 10)
Zhu Yib481de92007-09-25 17:54:57 -07001006 range = &pow_data->pwr_range_0[0];
1007 else
1008 range = &pow_data->pwr_range_1[1];
1009
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001010 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001011
Zhu Yib481de92007-09-25 17:54:57 -07001012
1013 if (period == 0) {
1014 period = 1;
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08001015 skip = false;
1016 } else {
1017 skip = !!range[mode].no_dtim;
Zhu Yib481de92007-09-25 17:54:57 -07001018 }
1019
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08001020 if (skip) {
Zhu Yib481de92007-09-25 17:54:57 -07001021 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1022 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1023 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08001024 } else {
1025 max_sleep = period;
1026 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001027 }
1028
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08001029 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
Zhu Yib481de92007-09-25 17:54:57 -07001030 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1031 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
Zhu Yib481de92007-09-25 17:54:57 -07001032
1033 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1034 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1035 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1036 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1037 le32_to_cpu(cmd->sleep_interval[0]),
1038 le32_to_cpu(cmd->sleep_interval[1]),
1039 le32_to_cpu(cmd->sleep_interval[2]),
1040 le32_to_cpu(cmd->sleep_interval[3]),
1041 le32_to_cpu(cmd->sleep_interval[4]));
1042
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08001043 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001044}
1045
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001046static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001047{
John W. Linville9a62f732007-11-15 16:27:36 -05001048 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001049 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001050 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001051
1052 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001053 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001054 * else user level */
1055 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001056 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001057 final_mode = IWL_POWER_INDEX_3;
1058 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001059 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001060 final_mode = IWL_POWER_MODE_CAM;
1061 break;
1062 default:
1063 final_mode = mode;
1064 break;
1065 }
1066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001067 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001068
Tomas Winkler600c0e12008-12-19 10:37:04 +08001069 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
Samuel Ortiz518099a2009-01-19 15:30:27 -08001070 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
1071 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001072
1073 if (final_mode == IWL_POWER_MODE_CAM)
1074 clear_bit(STATUS_POWER_PMI, &priv->status);
1075 else
1076 set_bit(STATUS_POWER_PMI, &priv->status);
1077
1078 return rc;
1079}
1080
Zhu Yib481de92007-09-25 17:54:57 -07001081#define MAX_UCODE_BEACON_INTERVAL 1024
1082#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1083
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001084static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001085{
1086 u16 new_val = 0;
1087 u16 beacon_factor = 0;
1088
1089 beacon_factor =
1090 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1091 / MAX_UCODE_BEACON_INTERVAL;
1092 new_val = beacon_val / beacon_factor;
1093
1094 return cpu_to_le16(new_val);
1095}
1096
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001097static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001098{
1099 u64 interval_tm_unit;
1100 u64 tsf, result;
1101 unsigned long flags;
1102 struct ieee80211_conf *conf = NULL;
1103 u16 beacon_int = 0;
1104
1105 conf = ieee80211_get_hw_conf(priv->hw);
1106
1107 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001108 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001109 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1110
Tomas Winkler28afaf92008-12-19 10:37:06 +08001111 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001112
1113 beacon_int = priv->beacon_int;
1114 spin_unlock_irqrestore(&priv->lock, flags);
1115
Johannes Berg05c914f2008-09-11 00:01:58 +02001116 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001117 if (beacon_int == 0) {
1118 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1119 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1120 } else {
1121 priv->rxon_timing.beacon_interval =
1122 cpu_to_le16(beacon_int);
1123 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001124 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001125 le16_to_cpu(priv->rxon_timing.beacon_interval));
1126 }
1127
1128 priv->rxon_timing.atim_window = 0;
1129 } else {
1130 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001131 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001132 /* TODO: we need to get atim_window from upper stack
1133 * for now we set to 0 */
1134 priv->rxon_timing.atim_window = 0;
1135 }
1136
1137 interval_tm_unit =
1138 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1139 result = do_div(tsf, interval_tm_unit);
1140 priv->rxon_timing.beacon_init_val =
1141 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1142
1143 IWL_DEBUG_ASSOC
1144 ("beacon interval %d beacon timer %d beacon tim %d\n",
1145 le16_to_cpu(priv->rxon_timing.beacon_interval),
1146 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1147 le16_to_cpu(priv->rxon_timing.atim_window));
1148}
1149
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001150static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001151{
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001152 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001153 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1154 return -EIO;
1155 }
1156
1157 if (test_bit(STATUS_SCANNING, &priv->status)) {
1158 IWL_DEBUG_SCAN("Scan already in progress.\n");
1159 return -EAGAIN;
1160 }
1161
1162 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1163 IWL_DEBUG_SCAN("Scan request while abort pending. "
1164 "Queuing.\n");
1165 return -EAGAIN;
1166 }
1167
1168 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08001169 if (priv->cfg->sku & IWL_SKU_G)
1170 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
1171 if (priv->cfg->sku & IWL_SKU_A)
1172 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07001173 set_bit(STATUS_SCANNING, &priv->status);
1174 priv->scan_start = jiffies;
1175 priv->scan_pass_start = priv->scan_start;
1176
1177 queue_work(priv->workqueue, &priv->request_scan);
1178
1179 return 0;
1180}
1181
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001182static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07001183{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001184 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001185
1186 if (hw_decrypt)
1187 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
1188 else
1189 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
1190
1191 return 0;
1192}
1193
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001194static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001195 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001196{
Johannes Berg8318d782008-01-24 19:38:38 +01001197 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001198 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07001199 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1200 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001201 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001202 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001203 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001204 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001205 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001206 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001207 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001208
Johannes Berg05c914f2008-09-11 00:01:58 +02001209 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001210 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001211
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001212 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1213 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1214 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001215 }
1216}
1217
1218/*
Ian Schram01ebd062007-10-25 17:15:22 +08001219 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001220 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001221static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07001222 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001223{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001224 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001225
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001226 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001227
Zhu, Yi60294de2008-10-29 14:05:45 -07001228 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001229 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001230 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07001231 break;
1232
Johannes Berg05c914f2008-09-11 00:01:58 +02001233 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001234 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
1235 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001236 break;
1237
Johannes Berg05c914f2008-09-11 00:01:58 +02001238 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001239 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1240 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1241 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001242 RXON_FILTER_ACCEPT_GRP_MSK;
1243 break;
1244
Johannes Berg05c914f2008-09-11 00:01:58 +02001245 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001246 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1247 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001248 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1249 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001250 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001251 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001252 break;
Zhu Yib481de92007-09-25 17:54:57 -07001253 }
1254
1255#if 0
1256 /* TODO: Figure out when short_preamble would be set and cache from
1257 * that */
1258 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001259 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001260 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001261 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001262#endif
1263
Samuel Ortize6148912009-01-23 13:45:15 -08001264 ch_info = iwl_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001265 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001266
1267 if (!ch_info)
1268 ch_info = &priv->channel_info[0];
1269
1270 /*
1271 * in some case A channels are all non IBSS
1272 * in this case force B/G channel
1273 */
Zhu, Yi60294de2008-10-29 14:05:45 -07001274 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07001275 ch_info = &priv->channel_info[0];
1276
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001277 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001278 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01001279 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001280 else
Johannes Berg8318d782008-01-24 19:38:38 +01001281 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001282
Johannes Berg8318d782008-01-24 19:38:38 +01001283 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001284
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001285 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001286 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001287 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001288 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1289}
1290
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001291static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001292{
Johannes Berg05c914f2008-09-11 00:01:58 +02001293 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001294 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001295
Samuel Ortize6148912009-01-23 13:45:15 -08001296 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001297 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001298 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001299
1300 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001301 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001302 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001303 return -EINVAL;
1304 }
1305 }
1306
Zhu, Yi60294de2008-10-29 14:05:45 -07001307 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001308 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001309
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001310 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001311
Tomas Winklera96a27f2008-10-23 23:48:56 -07001312 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001313 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001314 return -EAGAIN;
1315
1316 cancel_delayed_work(&priv->scan_check);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08001317 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001318 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08001319 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1320 return -EAGAIN;
1321 }
1322
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001323 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001324
1325 return 0;
1326}
1327
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001328static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02001329 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001330 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001331 struct sk_buff *skb_frag,
1332 int last_frag)
1333{
Winkler, Tomase52119c2008-12-22 11:31:19 +08001334 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Ivo van Doorn1c014422008-04-17 19:41:02 +02001335 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001336 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07001337
1338 switch (keyinfo->alg) {
1339 case ALG_CCMP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08001340 tx->sec_ctl = TX_CMD_SEC_CCM;
1341 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07001342 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001343 break;
1344
1345 case ALG_TKIP:
1346#if 0
Winkler, Tomase52119c2008-12-22 11:31:19 +08001347 tx->sec_ctl = TX_CMD_SEC_TKIP;
Zhu Yib481de92007-09-25 17:54:57 -07001348
1349 if (last_frag)
Winkler, Tomase52119c2008-12-22 11:31:19 +08001350 memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
Zhu Yib481de92007-09-25 17:54:57 -07001351 8);
1352 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08001353 memset(tx->tkip_mic.byte, 0, 8);
Zhu Yib481de92007-09-25 17:54:57 -07001354#endif
1355 break;
1356
1357 case ALG_WEP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08001358 tx->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02001359 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07001360
1361 if (keyinfo->keylen == 13)
Winkler, Tomase52119c2008-12-22 11:31:19 +08001362 tx->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -07001363
Winkler, Tomase52119c2008-12-22 11:31:19 +08001364 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -07001365
1366 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02001367 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07001368 break;
1369
Zhu Yib481de92007-09-25 17:54:57 -07001370 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08001371 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07001372 break;
1373 }
1374}
1375
1376/*
1377 * handle build REPLY_TX command notification.
1378 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001379static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001380 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02001381 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +08001382 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -07001383{
Winkler, Tomase52119c2008-12-22 11:31:19 +08001384 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
1385 __le32 tx_flags = tx->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001386 __le16 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +02001387 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07001388
Winkler, Tomase52119c2008-12-22 11:31:19 +08001389 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02001390 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07001391 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001392 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07001393 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001394 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07001395 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1396 tx_flags |= TX_CMD_FLG_TSF_MSK;
1397 } else {
1398 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1399 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1400 }
1401
Winkler, Tomase52119c2008-12-22 11:31:19 +08001402 tx->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07001403 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07001404 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1405
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001406 if (ieee80211_is_data_qos(fc)) {
1407 u8 *qc = ieee80211_get_qos_ctl(hdr);
Winkler, Tomase52119c2008-12-22 11:31:19 +08001408 tx->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07001409 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001410 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001411 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001412 }
Zhu Yib481de92007-09-25 17:54:57 -07001413
Johannes Berge6a98542008-10-21 12:40:02 +02001414 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07001415 tx_flags |= TX_CMD_FLG_RTS_MSK;
1416 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02001417 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07001418 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1419 tx_flags |= TX_CMD_FLG_CTS_MSK;
1420 }
1421
1422 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1423 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1424
1425 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001426 if (ieee80211_is_mgmt(fc)) {
1427 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Winkler, Tomase52119c2008-12-22 11:31:19 +08001428 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001429 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08001430 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001431 } else {
Winkler, Tomase52119c2008-12-22 11:31:19 +08001432 tx->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001433#ifdef CONFIG_IWL3945_LEDS
1434 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
1435#endif
1436 }
Zhu Yib481de92007-09-25 17:54:57 -07001437
Winkler, Tomase52119c2008-12-22 11:31:19 +08001438 tx->driver_txop = 0;
1439 tx->tx_flags = tx_flags;
1440 tx->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001441}
1442
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001443/**
1444 * iwl3945_get_sta_id - Find station's index within station table
1445 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001446static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001447{
1448 int sta_id;
1449 u16 fc = le16_to_cpu(hdr->frame_control);
1450
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001451 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001452 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1453 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001454 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001455
1456 switch (priv->iw_mode) {
1457
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001458 /* If we are a client station in a BSS network, use the special
1459 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02001460 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07001461 return IWL_AP_ID;
1462
1463 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02001464 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001465 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001466 if (sta_id != IWL_INVALID_STATION)
1467 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001468 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001469
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001470 /* If this frame is going out to an IBSS network, find the station,
1471 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02001472 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001473 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001474 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001475 if (sta_id != IWL_INVALID_STATION)
1476 return sta_id;
1477
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001478 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001479
1480 if (sta_id != IWL_INVALID_STATION)
1481 return sta_id;
1482
Johannes Berge1749612008-10-27 15:59:26 -07001483 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001484 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07001485 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08001486 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001487 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07001488 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08001489 /* If we are in monitor mode, use BCAST. This is required for
1490 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02001491 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001492 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08001493
Zhu Yib481de92007-09-25 17:54:57 -07001494 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001495 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1496 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001497 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001498 }
1499}
1500
1501/*
1502 * start REPLY_TX command process
1503 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001504static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001505{
1506 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02001507 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomase52119c2008-12-22 11:31:19 +08001508 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08001509 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001510 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +08001511 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001512 dma_addr_t phys_addr;
1513 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +08001514 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +08001515 u16 len, idx, len_org, hdr_len;
1516 u8 id;
1517 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07001518 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001519 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001520 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001521 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07001522 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001523 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001524 unsigned long flags;
1525 int rc;
1526
1527 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001528 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001529 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1530 goto drop_unlock;
1531 }
1532
Johannes Berge039fa42008-05-15 12:55:29 +02001533 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001534 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001535 goto drop_unlock;
1536 }
1537
1538 unicast = !is_multicast_ether_addr(hdr->addr1);
1539 id = 0;
1540
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001541 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07001542
Samuel Ortizd08853a2009-01-23 13:45:17 -08001543#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001544 if (ieee80211_is_auth(fc))
1545 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001546 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07001547 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001548 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07001549 IWL_DEBUG_TX("Sending REASSOC frame\n");
1550#endif
1551
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001552 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08001553 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001554 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08001555 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001556 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001557 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07001558 goto drop_unlock;
1559 }
1560
1561 spin_unlock_irqrestore(&priv->lock, flags);
1562
Harvey Harrison7294ec92008-07-15 18:43:59 -07001563 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001564
1565 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001566 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001567 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07001568 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
1569 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001570 goto drop;
1571 }
1572
1573 IWL_DEBUG_RATE("station Id %d\n", sta_id);
1574
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07001575 if (ieee80211_is_data_qos(fc)) {
1576 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07001577 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001578 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07001579 IEEE80211_SCTL_SEQ;
1580 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1581 (hdr->seq_ctrl &
1582 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
1583 seq_number += 0x10;
1584 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001585
1586 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08001587 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07001588 q = &txq->q;
1589
1590 spin_lock_irqsave(&priv->lock, flags);
1591
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001592 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001593
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001594 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08001595 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001596 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001597
1598 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08001599 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +08001600 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -07001601 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +08001602 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001603
1604 /*
1605 * Set up the Tx-command (not MAC!) header.
1606 * Store the chosen Tx queue and TFD index within the sequence field;
1607 * after Tx, uCode's Tx response will return this value so driver can
1608 * locate the frame within the tx queue and do post-tx processing.
1609 */
Zhu Yib481de92007-09-25 17:54:57 -07001610 out_cmd->hdr.cmd = REPLY_TX;
1611 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001612 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001613
1614 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +08001615 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -07001616
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001617 /*
1618 * Use the first empty entry in this queue's command buffer array
1619 * to contain the Tx command and MAC header concatenated together
1620 * (payload data will be in another buffer).
1621 * Size of this varies, due to varying MAC header length.
1622 * If end is not dword aligned, we'll have 2 extra bytes at the end
1623 * of the MAC header (device reads on dword boundaries).
1624 * We'll tell device about this padding later.
1625 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001626 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08001627 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07001628
1629 len_org = len;
1630 len = (len + 3) & ~3;
1631
1632 if (len_org != len)
1633 len_org = 1;
1634 else
1635 len_org = 0;
1636
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001637 /* Physical address of this Tx command's header (not MAC header!),
1638 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08001639 txcmd_phys = pci_map_single(priv->pci_dev,
1640 out_cmd, sizeof(struct iwl_cmd),
1641 PCI_DMA_TODEVICE);
1642 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
1643 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
1644 /* Add buffer containing Tx command and MAC(!) header to TFD's
1645 * first entry */
1646 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001647
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001648 /* Add buffer containing Tx command and MAC(!) header to TFD's
1649 * first entry */
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -08001650 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
1651 txcmd_phys, len, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001652
Johannes Bergd0f09802008-07-29 11:32:07 +02001653 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02001654 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001655
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001656 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1657 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07001658 len = skb->len - hdr_len;
1659 if (len) {
1660 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
1661 len, PCI_DMA_TODEVICE);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -08001662 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
1663 phys_addr, len,
1664 0, U32_PAD(len));
Zhu Yib481de92007-09-25 17:54:57 -07001665 }
1666
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001667 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07001668 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +08001669 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -07001670
1671 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +08001672 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001673
1674 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02001675 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001676
Winkler, Tomase52119c2008-12-22 11:31:19 +08001677 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
1678 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001679
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07001680 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07001681 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001682 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001683 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07001684 } else {
1685 wait_write_ptr = 1;
1686 txq->need_update = 0;
1687 }
1688
Winkler, Tomase52119c2008-12-22 11:31:19 +08001689 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -07001690
Winkler, Tomase52119c2008-12-22 11:31:19 +08001691 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07001692 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07001693
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001694 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08001695 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001696 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001697 spin_unlock_irqrestore(&priv->lock, flags);
1698
1699 if (rc)
1700 return rc;
1701
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001702 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07001703 && priv->mac80211_registered) {
1704 if (wait_write_ptr) {
1705 spin_lock_irqsave(&priv->lock, flags);
1706 txq->need_update = 1;
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08001707 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001708 spin_unlock_irqrestore(&priv->lock, flags);
1709 }
1710
Johannes Berge2530082008-05-17 00:57:14 +02001711 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07001712 }
1713
1714 return 0;
1715
1716drop_unlock:
1717 spin_unlock_irqrestore(&priv->lock, flags);
1718drop:
1719 return -1;
1720}
1721
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001722static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001723{
Johannes Berg8318d782008-01-24 19:38:38 +01001724 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001725 struct ieee80211_rate *rate;
1726 int i;
1727
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08001728 sband = iwl_get_hw_mode(priv, priv->band);
Johannes Berg8318d782008-01-24 19:38:38 +01001729 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001730 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08001731 return;
1732 }
Zhu Yib481de92007-09-25 17:54:57 -07001733
1734 priv->active_rate = 0;
1735 priv->active_rate_basic = 0;
1736
Johannes Berg8318d782008-01-24 19:38:38 +01001737 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
1738 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07001739
Johannes Berg8318d782008-01-24 19:38:38 +01001740 for (i = 0; i < sband->n_bitrates; i++) {
1741 rate = &sband->bitrates[i];
1742 if ((rate->hw_value < IWL_RATE_COUNT) &&
1743 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
1744 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
1745 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
1746 priv->active_rate |= (1 << rate->hw_value);
1747 }
Zhu Yib481de92007-09-25 17:54:57 -07001748 }
1749
1750 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
1751 priv->active_rate, priv->active_rate_basic);
1752
1753 /*
1754 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
1755 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
1756 * OFDM
1757 */
1758 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001759 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001760 ((priv->active_rate_basic &
1761 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
1762 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001763 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001764 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1765
1766 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001767 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001768 ((priv->active_rate_basic &
1769 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
1770 IWL_FIRST_OFDM_RATE) & 0xFF;
1771 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001772 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07001773 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1774}
1775
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001776static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07001777{
1778 unsigned long flags;
1779
1780 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1781 return;
1782
1783 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
1784 disable_radio ? "OFF" : "ON");
1785
1786 if (disable_radio) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08001787 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001788 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02001789 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07001790 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001791 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001792 CSR_UCODE_SW_BIT_RFKILL);
1793 spin_unlock_irqrestore(&priv->lock, flags);
Samuel Ortizc4962942008-12-22 11:31:18 +08001794 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001795 set_bit(STATUS_RF_KILL_SW, &priv->status);
1796 }
1797 return;
1798 }
1799
1800 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001801 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07001802
1803 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1804 spin_unlock_irqrestore(&priv->lock, flags);
1805
1806 /* wake up ucode */
1807 msleep(10);
1808
1809 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001810 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1811 if (!iwl_grab_nic_access(priv))
1812 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001813 spin_unlock_irqrestore(&priv->lock, flags);
1814
1815 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1816 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
1817 "disabled by HW switch\n");
1818 return;
1819 }
1820
Zhu Yi808e72a2008-06-12 09:47:17 +08001821 if (priv->is_open)
1822 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07001823 return;
1824}
1825
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001826void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07001827 u32 decrypt_res, struct ieee80211_rx_status *stats)
1828{
1829 u16 fc =
1830 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
1831
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001832 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07001833 return;
1834
1835 if (!(fc & IEEE80211_FCTL_PROTECTED))
1836 return;
1837
1838 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
1839 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
1840 case RX_RES_STATUS_SEC_TYPE_TKIP:
1841 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
1842 RX_RES_STATUS_BAD_ICV_MIC)
1843 stats->flag |= RX_FLAG_MMIC_ERROR;
1844 case RX_RES_STATUS_SEC_TYPE_WEP:
1845 case RX_RES_STATUS_SEC_TYPE_CCMP:
1846 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
1847 RX_RES_STATUS_DECRYPT_OK) {
1848 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
1849 stats->flag |= RX_FLAG_DECRYPTED;
1850 }
1851 break;
1852
1853 default:
1854 break;
1855 }
1856}
1857
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001858#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07001859
1860#include "iwl-spectrum.h"
1861
1862#define BEACON_TIME_MASK_LOW 0x00FFFFFF
1863#define BEACON_TIME_MASK_HIGH 0xFF000000
1864#define TIME_UNIT 1024
1865
1866/*
1867 * extended beacon time format
1868 * time in usec will be changed into a 32-bit value in 8:24 format
1869 * the high 1 byte is the beacon counts
1870 * the lower 3 bytes is the time in usec within one beacon interval
1871 */
1872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001873static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001874{
1875 u32 quot;
1876 u32 rem;
1877 u32 interval = beacon_interval * 1024;
1878
1879 if (!interval || !usec)
1880 return 0;
1881
1882 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1883 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1884
1885 return (quot << 24) + rem;
1886}
1887
1888/* base is usually what we get from ucode with each received frame,
1889 * the same as HW timer counter counting down
1890 */
1891
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001892static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07001893{
1894 u32 base_low = base & BEACON_TIME_MASK_LOW;
1895 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1896 u32 interval = beacon_interval * TIME_UNIT;
1897 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1898 (addon & BEACON_TIME_MASK_HIGH);
1899
1900 if (base_low > addon_low)
1901 res += base_low - addon_low;
1902 else if (base_low < addon_low) {
1903 res += interval + base_low - addon_low;
1904 res += (1 << 24);
1905 } else
1906 res += (1 << 24);
1907
1908 return cpu_to_le32(res);
1909}
1910
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001911static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001912 struct ieee80211_measurement_params *params,
1913 u8 type)
1914{
Tomas Winkler600c0e12008-12-19 10:37:04 +08001915 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001916 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001917 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001918 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1919 .data = (void *)&spectrum,
1920 .meta.flags = CMD_WANT_SKB,
1921 };
1922 u32 add_time = le64_to_cpu(params->start_time);
1923 int rc;
1924 int spectrum_resp_status;
1925 int duration = le16_to_cpu(params->duration);
1926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001927 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001928 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001929 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07001930 le64_to_cpu(params->start_time) - priv->last_tsf,
1931 le16_to_cpu(priv->rxon_timing.beacon_interval));
1932
1933 memset(&spectrum, 0, sizeof(spectrum));
1934
1935 spectrum.channel_count = cpu_to_le16(1);
1936 spectrum.flags =
1937 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1938 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1939 cmd.len = sizeof(spectrum);
1940 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1941
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001942 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001943 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001944 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07001945 add_time,
1946 le16_to_cpu(priv->rxon_timing.beacon_interval));
1947 else
1948 spectrum.start_time = 0;
1949
1950 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1951 spectrum.channels[0].channel = params->channel;
1952 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001953 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07001954 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1955 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1956
Samuel Ortiz518099a2009-01-19 15:30:27 -08001957 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001958 if (rc)
1959 return rc;
1960
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001961 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001962 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001963 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07001964 rc = -EIO;
1965 }
1966
1967 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1968 switch (spectrum_resp_status) {
1969 case 0: /* Command will be handled */
1970 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08001971 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
1972 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07001973 priv->measurement_status &= ~MEASUREMENT_READY;
1974 }
1975 priv->measurement_status |= MEASUREMENT_ACTIVE;
1976 rc = 0;
1977 break;
1978
1979 case 1: /* Command will not be handled */
1980 rc = -EAGAIN;
1981 break;
1982 }
1983
1984 dev_kfree_skb_any(cmd.meta.u.skb);
1985
1986 return rc;
1987}
1988#endif
1989
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001990static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08001991 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07001992{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001993 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1994 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07001995 struct delayed_work *pwork;
1996
1997 palive = &pkt->u.alive_frame;
1998
1999 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2000 "0x%01X 0x%01X\n",
2001 palive->is_valid, palive->ver_type,
2002 palive->ver_subtype);
2003
2004 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2005 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002006 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2007 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002008 pwork = &priv->init_alive_start;
2009 } else {
2010 IWL_DEBUG_INFO("Runtime Alive received.\n");
2011 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002012 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002013 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002014 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002015 }
2016
2017 /* We delay the ALIVE response by 5ms to
2018 * give the HW RF Kill time to activate... */
2019 if (palive->is_valid == UCODE_VALID_OK)
2020 queue_delayed_work(priv->workqueue, pwork,
2021 msecs_to_jiffies(5));
2022 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002023 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002024}
2025
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002026static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002027 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002028{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002029#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002030 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002031#endif
Zhu Yib481de92007-09-25 17:54:57 -07002032
2033 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2034 return;
2035}
2036
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002037static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002038 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002039{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002040 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002041
Winkler, Tomas15b16872008-12-19 10:37:33 +08002042 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002043 "seq 0x%04X ser 0x%08X\n",
2044 le32_to_cpu(pkt->u.err_resp.error_type),
2045 get_cmd_string(pkt->u.err_resp.cmd_id),
2046 pkt->u.err_resp.cmd_id,
2047 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2048 le32_to_cpu(pkt->u.err_resp.error_info));
2049}
2050
2051#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2052
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002053static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002054{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002055 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002056 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002057 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002058 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2059 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2060 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002061 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002062}
2063
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002064static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002065 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002066{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002067#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002068 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002069 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002070
2071 if (!report->state) {
2072 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2073 "Spectrum Measure Notification: Start\n");
2074 return;
2075 }
2076
2077 memcpy(&priv->measure_report, report, sizeof(*report));
2078 priv->measurement_status |= MEASUREMENT_READY;
2079#endif
2080}
2081
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002082static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002083 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002084{
Samuel Ortizd08853a2009-01-23 13:45:17 -08002085#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002086 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002087 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002088 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2089 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2090#endif
2091}
2092
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002093static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002094 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002095{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002096 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002097 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2098 "notification for %s:\n",
2099 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002100 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2101 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002102}
2103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002104static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002105{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002106 struct iwl_priv *priv =
2107 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002108 struct sk_buff *beacon;
2109
2110 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002111 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002112
2113 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002114 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002115 return;
2116 }
2117
2118 mutex_lock(&priv->mutex);
2119 /* new beacon skb is allocated every time; dispose previous.*/
2120 if (priv->ibss_beacon)
2121 dev_kfree_skb(priv->ibss_beacon);
2122
2123 priv->ibss_beacon = beacon;
2124 mutex_unlock(&priv->mutex);
2125
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002126 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002127}
2128
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002129static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002130 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002131{
Samuel Ortizd08853a2009-01-23 13:45:17 -08002132#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002133 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002134 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07002135 u8 rate = beacon->beacon_notify_hdr.rate;
2136
2137 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2138 "tsf %d %d rate %d\n",
2139 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2140 beacon->beacon_notify_hdr.failure_frame,
2141 le32_to_cpu(beacon->ibss_mgr_status),
2142 le32_to_cpu(beacon->high_tsf),
2143 le32_to_cpu(beacon->low_tsf), rate);
2144#endif
2145
Johannes Berg05c914f2008-09-11 00:01:58 +02002146 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07002147 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2148 queue_work(priv->workqueue, &priv->beacon_update);
2149}
2150
2151/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002152static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002153 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002154{
Samuel Ortizd08853a2009-01-23 13:45:17 -08002155#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002156 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002157 struct iwl_scanreq_notification *notif =
2158 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002159
2160 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2161#endif
2162}
2163
2164/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002165static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002166 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002167{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002168 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002169 struct iwl_scanstart_notification *notif =
2170 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002171 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2172 IWL_DEBUG_SCAN("Scan start: "
2173 "%d [802.11%s] "
2174 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2175 notif->channel,
2176 notif->band ? "bg" : "a",
2177 notif->tsf_high,
2178 notif->tsf_low, notif->status, notif->beacon_timer);
2179}
2180
2181/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002182static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002183 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002184{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002185#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002186 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002187 struct iwl_scanresults_notification *notif =
2188 (struct iwl_scanresults_notification *)pkt->u.raw;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002189#endif
Zhu Yib481de92007-09-25 17:54:57 -07002190
2191 IWL_DEBUG_SCAN("Scan ch.res: "
2192 "%d [802.11%s] "
2193 "(TSF: 0x%08X:%08X) - %d "
2194 "elapsed=%lu usec (%dms since last)\n",
2195 notif->channel,
2196 notif->band ? "bg" : "a",
2197 le32_to_cpu(notif->tsf_high),
2198 le32_to_cpu(notif->tsf_low),
2199 le32_to_cpu(notif->statistics[0]),
2200 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2201 jiffies_to_msecs(elapsed_jiffies
2202 (priv->last_scan_jiffies, jiffies)));
2203
2204 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002205 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002206}
2207
2208/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002209static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002210 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002211{
Helmut Schaac7e035a2009-01-19 13:02:15 +01002212#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002213 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002214 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Helmut Schaac7e035a2009-01-19 13:02:15 +01002215#endif
Zhu Yib481de92007-09-25 17:54:57 -07002216
2217 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2218 scan_notif->scanned_channels,
2219 scan_notif->tsf_low,
2220 scan_notif->tsf_high, scan_notif->status);
2221
2222 /* The HW is no longer scanning */
2223 clear_bit(STATUS_SCAN_HW, &priv->status);
2224
2225 /* The scan completion notification came in, so kill that timer... */
2226 cancel_delayed_work(&priv->scan_check);
2227
2228 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002229 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
2230 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07002231 jiffies_to_msecs(elapsed_jiffies
2232 (priv->scan_pass_start, jiffies)));
2233
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002234 /* Remove this scanned band from the list of pending
2235 * bands to scan, band G precedes A in order of scanning
2236 * as seen in iwl3945_bg_request_scan */
2237 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
2238 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
2239 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
2240 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002241
2242 /* If a request to abort was given, or the scan did not succeed
2243 * then we reset the scan state machine and terminate,
2244 * re-queuing another scan if one has been requested */
2245 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2246 IWL_DEBUG_INFO("Aborted scan completed.\n");
2247 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2248 } else {
2249 /* If there are more bands on this scan pass reschedule */
2250 if (priv->scan_bands > 0)
2251 goto reschedule;
2252 }
2253
2254 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002255 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002256 IWL_DEBUG_INFO("Setting scan to off\n");
2257
2258 clear_bit(STATUS_SCANNING, &priv->status);
2259
2260 IWL_DEBUG_INFO("Scan took %dms\n",
2261 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2262
2263 queue_work(priv->workqueue, &priv->scan_completed);
2264
2265 return;
2266
2267reschedule:
2268 priv->scan_pass_start = jiffies;
2269 queue_work(priv->workqueue, &priv->request_scan);
2270}
2271
2272/* Handle notification from uCode that card's power state is changing
2273 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002274static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002275 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002276{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002277 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002278 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2279 unsigned long status = priv->status;
2280
2281 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2282 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2283 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2284
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002285 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002286 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2287
2288 if (flags & HW_CARD_DISABLED)
2289 set_bit(STATUS_RF_KILL_HW, &priv->status);
2290 else
2291 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2292
2293
2294 if (flags & SW_CARD_DISABLED)
2295 set_bit(STATUS_RF_KILL_SW, &priv->status);
2296 else
2297 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2298
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08002299 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002300
2301 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2302 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2303 (test_bit(STATUS_RF_KILL_SW, &status) !=
2304 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2305 queue_work(priv->workqueue, &priv->rf_kill);
2306 else
2307 wake_up_interruptible(&priv->wait_command_queue);
2308}
2309
2310/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002311 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07002312 *
2313 * Setup the RX handlers for each of the reply types sent from the uCode
2314 * to the host.
2315 *
2316 * This function chains into the hardware specific files for them to setup
2317 * any hardware specific handlers as well.
2318 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002319static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002320{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002321 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
2322 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
2323 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
2324 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07002325 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002326 iwl3945_rx_spectrum_measure_notif;
2327 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002328 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002329 iwl3945_rx_pm_debug_statistics_notif;
2330 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002331
Ben Cahill9fbab512007-11-29 11:09:47 +08002332 /*
2333 * The same handler is used for both the REPLY to a discrete
2334 * statistics request from the host as well as for the periodic
2335 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07002336 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002337 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
2338 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07002339
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002340 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
2341 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002342 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002343 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002344 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002345 iwl3945_rx_scan_complete_notif;
2346 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002347
Ben Cahill9fbab512007-11-29 11:09:47 +08002348 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002349 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002350}
2351
2352/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08002353 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
2354 * When FW advances 'R' index, all entries between old and new 'R' index
2355 * need to be reclaimed.
2356 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002357static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08002358 int txq_id, int index)
2359{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002360 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002361 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08002362 int nfreed = 0;
2363
Winkler, Tomas625a3812009-01-08 10:19:55 -08002364 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002365 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08002366 "is out of range [0-%d] %d %d.\n", txq_id,
2367 index, q->n_bd, q->write_ptr, q->read_ptr);
2368 return;
2369 }
2370
2371 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
2372 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2373 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002374 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08002375 q->write_ptr, q->read_ptr);
2376 queue_work(priv->workqueue, &priv->restart);
2377 break;
2378 }
2379 nfreed++;
2380 }
2381}
2382
2383
2384/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002385 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07002386 * @rxb: Rx buffer to reclaim
2387 *
2388 * If an Rx buffer has an async callback associated with it the callback
2389 * will be executed. The attached skb (if present) will only be freed
2390 * if the callback returns 1
2391 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002392static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002393 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002394{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002395 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002396 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2397 int txq_id = SEQ_TO_QUEUE(sequence);
2398 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08002399 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07002400 int cmd_index;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002401 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002402
Chatre, Reinette638d0eb2009-01-19 15:30:24 -08002403 if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
2404 "wrong command queue %d, sequence 0x%X readp=%d writep=%d\n",
2405 txq_id, sequence,
2406 priv->txq[IWL_CMD_QUEUE_NUM].q.read_ptr,
2407 priv->txq[IWL_CMD_QUEUE_NUM].q.write_ptr)) {
2408 iwl_print_hex_dump(priv, IWL_DL_INFO , rxb, 32);
2409 return;
2410 }
Zhu Yib481de92007-09-25 17:54:57 -07002411
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002412 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
2413 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07002414
2415 /* Input error checking is done when commands are added to queue. */
2416 if (cmd->meta.flags & CMD_WANT_SKB) {
2417 cmd->meta.source->u.skb = rxb->skb;
2418 rxb->skb = NULL;
2419 } else if (cmd->meta.u.callback &&
2420 !cmd->meta.u.callback(priv, cmd, rxb->skb))
2421 rxb->skb = NULL;
2422
Tomas Winkler91c066f2008-03-06 17:36:55 -08002423 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07002424
2425 if (!(cmd->meta.flags & CMD_ASYNC)) {
2426 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2427 wake_up_interruptible(&priv->wait_command_queue);
2428 }
2429}
2430
2431/************************** RX-FUNCTIONS ****************************/
2432/*
2433 * Rx theory of operation
2434 *
2435 * The host allocates 32 DMA target addresses and passes the host address
2436 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
2437 * 0 to 31
2438 *
2439 * Rx Queue Indexes
2440 * The host/firmware share two index registers for managing the Rx buffers.
2441 *
2442 * The READ index maps to the first position that the firmware may be writing
2443 * to -- the driver can read up to (but not including) this position and get
2444 * good data.
2445 * The READ index is managed by the firmware once the card is enabled.
2446 *
2447 * The WRITE index maps to the last position the driver has read from -- the
2448 * position preceding WRITE is the last slot the firmware can place a packet.
2449 *
2450 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
2451 * WRITE = READ.
2452 *
Ben Cahill9fbab512007-11-29 11:09:47 +08002453 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07002454 * INDEX position, and WRITE to the last (READ - 1 wrapped)
2455 *
Ben Cahill9fbab512007-11-29 11:09:47 +08002456 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07002457 * and fire the RX interrupt. The driver can then query the READ index and
2458 * process as many packets as possible, moving the WRITE index forward as it
2459 * resets the Rx queue buffers with new memory.
2460 *
2461 * The management in the driver is as follows:
2462 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
2463 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08002464 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002465 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07002466 * iwl->rxq is replenished and the READ INDEX is updated (updating the
2467 * 'processed' and 'read' driver indexes as well)
2468 * + A received packet is processed and handed to the kernel network stack,
2469 * detached from the iwl->rxq. The driver 'processed' index is updated.
2470 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
2471 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
2472 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
2473 * were enough free buffers and RX_STALLED is set it is cleared.
2474 *
2475 *
2476 * Driver sequence:
2477 *
Ben Cahill9fbab512007-11-29 11:09:47 +08002478 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002479 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08002480 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07002481 * queue, updates firmware pointers, and updates
2482 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002483 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07002484 *
2485 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002486 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07002487 * READ INDEX, detaching the SKB from the pool.
2488 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002489 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07002490 * slots.
2491 * ...
2492 *
2493 */
2494
2495/**
Ben Cahill9fbab512007-11-29 11:09:47 +08002496 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07002497 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002498static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002499 dma_addr_t dma_addr)
2500{
2501 return cpu_to_le32((u32)dma_addr);
2502}
2503
2504/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002505 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07002506 *
Ben Cahill9fbab512007-11-29 11:09:47 +08002507 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07002508 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08002509 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07002510 *
2511 * This moves the 'write' index forward to catch up with 'processed', and
2512 * also updates the memory address in the firmware to reference the new
2513 * target buffer.
2514 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002515static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002516{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08002517 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07002518 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002519 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07002520 unsigned long flags;
2521 int write, rc;
2522
2523 spin_lock_irqsave(&rxq->lock, flags);
2524 write = rxq->write & ~0x7;
Winkler, Tomas37d68312009-01-08 10:19:54 -08002525 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002526 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07002527 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002528 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07002529 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002530
2531 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002532 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07002533 rxq->queue[rxq->write] = rxb;
2534 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
2535 rxq->free_count--;
2536 }
2537 spin_unlock_irqrestore(&rxq->lock, flags);
2538 /* If the pre-allocated buffer pool is dropping low, schedule to
2539 * refill it */
2540 if (rxq->free_count <= RX_LOW_WATERMARK)
2541 queue_work(priv->workqueue, &priv->rx_replenish);
2542
2543
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002544 /* If we've added more space for the firmware to place data, tell it.
2545 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07002546 if ((write != (rxq->write & ~0x7))
2547 || (abs(rxq->write - rxq->read) > 7)) {
2548 spin_lock_irqsave(&rxq->lock, flags);
2549 rxq->need_update = 1;
2550 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08002551 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07002552 if (rc)
2553 return rc;
2554 }
2555
2556 return 0;
2557}
2558
2559/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002560 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07002561 *
2562 * When moving to rx_free an SKB is allocated for the slot.
2563 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002564 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08002565 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07002566 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002567static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002568{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08002569 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07002570 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002571 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07002572 unsigned long flags;
2573 spin_lock_irqsave(&rxq->lock, flags);
2574 while (!list_empty(&rxq->rx_used)) {
2575 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002576 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002577
2578 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002579 rxb->skb =
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08002580 alloc_skb(priv->hw_params.rx_buf_size,
2581 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07002582 if (!rxb->skb) {
2583 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08002584 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07002585 /* We don't reschedule replenish work here -- we will
2586 * call the restock method and if it still needs
2587 * more buffers it will schedule replenish */
2588 break;
2589 }
Zhu Yi12342c42007-12-20 11:27:32 +08002590
2591 /* If radiotap head is required, reserve some headroom here.
2592 * The physical head count is a variable rx_stats->phy_count.
2593 * We reserve 4 bytes here. Plus these extra bytes, the
2594 * headroom of the physical head should be enough for the
2595 * radiotap head that iwl3945 supported. See iwl3945_rt.
2596 */
2597 skb_reserve(rxb->skb, 4);
2598
Zhu Yib481de92007-09-25 17:54:57 -07002599 priv->alloc_rxb_skb++;
2600 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002601
2602 /* Get physical address of RB/SKB */
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08002603 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
2604 rxb->skb->data,
2605 priv->hw_params.rx_buf_size,
2606 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07002607 list_add_tail(&rxb->list, &rxq->rx_free);
2608 rxq->free_count++;
2609 }
2610 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002611}
2612
2613/*
2614 * this should be called while priv->lock is locked
2615 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02002616static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002617{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002618 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002619
2620 iwl3945_rx_allocate(priv);
2621 iwl3945_rx_queue_restock(priv);
2622}
2623
2624
2625void iwl3945_rx_replenish(void *data)
2626{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002627 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002628 unsigned long flags;
2629
2630 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002631
2632 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002633 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002634 spin_unlock_irqrestore(&priv->lock, flags);
2635}
2636
Zhu Yib481de92007-09-25 17:54:57 -07002637/* Convert linear signal-to-noise ratio into dB */
2638static u8 ratio2dB[100] = {
2639/* 0 1 2 3 4 5 6 7 8 9 */
2640 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
2641 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
2642 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
2643 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
2644 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
2645 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
2646 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
2647 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
2648 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
2649 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
2650};
2651
2652/* Calculates a relative dB value from a ratio of linear
2653 * (i.e. not dB) signal levels.
2654 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002655int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07002656{
Adrian Bunk221c80c2008-02-02 23:19:01 +02002657 /* 1000:1 or higher just report as 60 dB */
2658 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07002659 return 60;
2660
Adrian Bunk221c80c2008-02-02 23:19:01 +02002661 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07002662 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02002663 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002664 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07002665
2666 /* We shouldn't see this */
2667 if (sig_ratio < 1)
2668 return 0;
2669
2670 /* Use table for ratios 1:1 - 99:1 */
2671 return (int)ratio2dB[sig_ratio];
2672}
2673
2674#define PERFECT_RSSI (-20) /* dBm */
2675#define WORST_RSSI (-95) /* dBm */
2676#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
2677
2678/* Calculate an indication of rx signal quality (a percentage, not dBm!).
2679 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
2680 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002681int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07002682{
2683 int sig_qual;
2684 int degradation = PERFECT_RSSI - rssi_dbm;
2685
2686 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
2687 * as indicator; formula is (signal dbm - noise dbm).
2688 * SNR at or above 40 is a great signal (100%).
2689 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
2690 * Weakest usable signal is usually 10 - 15 dB SNR. */
2691 if (noise_dbm) {
2692 if (rssi_dbm - noise_dbm >= 40)
2693 return 100;
2694 else if (rssi_dbm < noise_dbm)
2695 return 0;
2696 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
2697
2698 /* Else use just the signal level.
2699 * This formula is a least squares fit of data points collected and
2700 * compared with a reference system that had a percentage (%) display
2701 * for signal quality. */
2702 } else
2703 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
2704 (15 * RSSI_RANGE + 62 * degradation)) /
2705 (RSSI_RANGE * RSSI_RANGE);
2706
2707 if (sig_qual > 100)
2708 sig_qual = 100;
2709 else if (sig_qual < 1)
2710 sig_qual = 0;
2711
2712 return sig_qual;
2713}
2714
2715/**
Ben Cahill9fbab512007-11-29 11:09:47 +08002716 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07002717 *
2718 * Uses the priv->rx_handlers callback function array to invoke
2719 * the appropriate handlers, including command responses,
2720 * frame-received notifications, and other notifications.
2721 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002722static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002723{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002724 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002725 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08002726 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07002727 u32 r, i;
2728 int reclaim;
2729 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002730 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08002731 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07002732
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002733 /* uCode's read index (stored in shared DRAM) indicates the last Rx
2734 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08002735 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07002736 i = rxq->read;
2737
Winkler, Tomas37d68312009-01-08 10:19:54 -08002738 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002739 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002740 /* Rx interrupt, but nothing sent from uCode */
2741 if (i == r)
2742 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
2743
2744 while (i != r) {
2745 rxb = rxq->queue[i];
2746
Ben Cahill9fbab512007-11-29 11:09:47 +08002747 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07002748 * then a bug has been introduced in the queue refilling
2749 * routines -- catch it here */
2750 BUG_ON(rxb == NULL);
2751
2752 rxq->queue[i] = NULL;
2753
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002754 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08002755 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07002756 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002757 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002758
2759 /* Reclaim a command buffer only if this packet is a response
2760 * to a (driver-originated) command.
2761 * If the packet (e.g. Rx frame) originated from uCode,
2762 * there is no command buffer to reclaim.
2763 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
2764 * but apparently a few don't get set; catch them here. */
2765 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
2766 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
2767 (pkt->hdr.cmd != REPLY_TX);
2768
2769 /* Based on type of command response or notification,
2770 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002771 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07002772 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002773 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07002774 "r = %d, i = %d, %s, 0x%02x\n", r, i,
2775 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
2776 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2777 } else {
2778 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002779 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07002780 "r %d i %d No handler needed for %s, 0x%02x\n",
2781 r, i, get_cmd_string(pkt->hdr.cmd),
2782 pkt->hdr.cmd);
2783 }
2784
2785 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08002786 /* Invoke any callbacks, transfer the skb to caller, and
Samuel Ortiz518099a2009-01-19 15:30:27 -08002787 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07002788 * as we reclaim the driver command queue */
2789 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002790 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07002791 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002792 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07002793 }
2794
2795 /* For now we just don't re-use anything. We can tweak this
2796 * later to try and re-use notification packets and SKBs that
2797 * fail to Rx correctly */
2798 if (rxb->skb != NULL) {
2799 priv->alloc_rxb_skb--;
2800 dev_kfree_skb_any(rxb->skb);
2801 rxb->skb = NULL;
2802 }
2803
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002804 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Winkler, Tomas1e33dc62009-01-08 10:19:57 -08002805 priv->hw_params.rx_buf_size,
2806 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07002807 spin_lock_irqsave(&rxq->lock, flags);
2808 list_add_tail(&rxb->list, &priv->rxq.rx_used);
2809 spin_unlock_irqrestore(&rxq->lock, flags);
2810 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002811 /* If there are a lot of unused frames,
2812 * restock the Rx queue so ucode won't assert. */
2813 if (fill_rx) {
2814 count++;
2815 if (count >= 8) {
2816 priv->rxq.read = i;
2817 __iwl3945_rx_replenish(priv);
2818 count = 0;
2819 }
2820 }
Zhu Yib481de92007-09-25 17:54:57 -07002821 }
2822
2823 /* Backtrack one entry */
2824 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002825 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002826}
2827
Samuel Ortizd08853a2009-01-23 13:45:17 -08002828#ifdef CONFIG_IWLWIFI_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002829static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002830 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07002831{
2832 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002833 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07002834 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
2835 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
2836 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
2837 le32_to_cpu(rxon->filter_flags));
2838 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
2839 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
2840 rxon->ofdm_basic_rates);
2841 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07002842 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
2843 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07002844 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
2845}
2846#endif
2847
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002848static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002849{
2850 IWL_DEBUG_ISR("Enabling interrupts\n");
2851 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002852 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07002853}
2854
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002855
2856/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002857static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002858{
Tomas Winklera96a27f2008-10-23 23:48:56 -07002859 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002860 synchronize_irq(priv->pci_dev->irq);
2861 tasklet_kill(&priv->irq_tasklet);
2862}
2863
2864
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002865static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002866{
2867 clear_bit(STATUS_INT_ENABLED, &priv->status);
2868
2869 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002870 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07002871
2872 /* acknowledge/clear/reset any interrupts still pending
2873 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002874 iwl_write32(priv, CSR_INT, 0xffffffff);
2875 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07002876 IWL_DEBUG_ISR("Disabled interrupts\n");
2877}
2878
2879static const char *desc_lookup(int i)
2880{
2881 switch (i) {
2882 case 1:
2883 return "FAIL";
2884 case 2:
2885 return "BAD_PARAM";
2886 case 3:
2887 return "BAD_CHECKSUM";
2888 case 4:
2889 return "NMI_INTERRUPT";
2890 case 5:
2891 return "SYSASSERT";
2892 case 6:
2893 return "FATAL_ERROR";
2894 }
2895
2896 return "UNKNOWN";
2897}
2898
2899#define ERROR_START_OFFSET (1 * sizeof(u32))
2900#define ERROR_ELEM_SIZE (7 * sizeof(u32))
2901
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002902static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002903{
2904 u32 i;
2905 u32 desc, time, count, base, data1;
2906 u32 blink1, blink2, ilink1, ilink2;
2907 int rc;
2908
2909 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
2910
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002911 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002912 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07002913 return;
2914 }
2915
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002916 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002917 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002918 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002919 return;
2920 }
2921
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002922 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07002923
2924 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002925 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
2926 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
2927 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07002928 }
2929
Winkler, Tomas15b16872008-12-19 10:37:33 +08002930 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07002931 "ilink1 nmiPC Line\n");
2932 for (i = ERROR_START_OFFSET;
2933 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
2934 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002935 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07002936 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002937 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07002938 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002939 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07002940 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002941 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07002942 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002943 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07002944 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002945 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07002946 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002947 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07002948
Winkler, Tomas15b16872008-12-19 10:37:33 +08002949 IWL_ERR(priv,
2950 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
2951 desc_lookup(desc), desc, time, blink1, blink2,
2952 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07002953 }
2954
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002955 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002956
2957}
2958
Ben Cahillf58177b2007-11-29 11:09:43 +08002959#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07002960
2961/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002962 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07002963 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002964 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07002965 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002966static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07002967 u32 num_events, u32 mode)
2968{
2969 u32 i;
2970 u32 base; /* SRAM byte address of event log header */
2971 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
2972 u32 ptr; /* SRAM byte address of log data */
2973 u32 ev, time, data; /* event log data */
2974
2975 if (num_events == 0)
2976 return;
2977
2978 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
2979
2980 if (mode == 0)
2981 event_size = 2 * sizeof(u32);
2982 else
2983 event_size = 3 * sizeof(u32);
2984
2985 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
2986
2987 /* "time" is actually "data" for mode 0 (no timestamp).
2988 * place event id # at far right for easier visual parsing. */
2989 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002990 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002991 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002992 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002993 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08002994 if (mode == 0) {
2995 /* data, ev */
2996 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
2997 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002998 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002999 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003000 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07003001 }
3002 }
3003}
3004
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003005static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003006{
3007 int rc;
3008 u32 base; /* SRAM byte address of event log header */
3009 u32 capacity; /* event log capacity in # entries */
3010 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3011 u32 num_wraps; /* # times uCode wrapped to top of log */
3012 u32 next_entry; /* index of next entry to be written by uCode */
3013 u32 size; /* # entries that we'll print */
3014
3015 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003016 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003017 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003018 return;
3019 }
3020
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003021 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003022 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003023 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003024 return;
3025 }
3026
3027 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003028 capacity = iwl_read_targ_mem(priv, base);
3029 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3030 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3031 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003032
3033 size = num_wraps ? capacity : next_entry;
3034
3035 /* bail out if nothing in log */
3036 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003037 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003038 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003039 return;
3040 }
3041
Winkler, Tomas15b16872008-12-19 10:37:33 +08003042 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003043 size, num_wraps);
3044
3045 /* if uCode has wrapped back to top of log, start at the oldest entry,
3046 * i.e the next one that uCode would fill. */
3047 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003048 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003049 capacity - next_entry, mode);
3050
3051 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003052 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003053
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003054 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003055}
3056
3057/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003058 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003059 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003060static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003061{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003062 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07003063 set_bit(STATUS_FW_ERROR, &priv->status);
3064
3065 /* Cancel currently queued command. */
3066 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3067
Samuel Ortizd08853a2009-01-23 13:45:17 -08003068#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003069 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003070 iwl3945_dump_nic_error_log(priv);
3071 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003072 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003073 }
3074#endif
3075
3076 wake_up_interruptible(&priv->wait_command_queue);
3077
3078 /* Keep the restart process from trying to send host
3079 * commands by clearing the INIT status bit */
3080 clear_bit(STATUS_READY, &priv->status);
3081
3082 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3083 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3084 "Restarting adapter due to uCode error.\n");
3085
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003086 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003087 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
3088 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003089 priv->error_recovering = 1;
3090 }
3091 queue_work(priv->workqueue, &priv->restart);
3092 }
3093}
3094
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003095static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003096{
3097 unsigned long flags;
3098
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003099 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
3100 sizeof(priv->staging39_rxon));
3101 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003102 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003104 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003105
3106 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003107 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003108 priv->error_recovering = 0;
3109 spin_unlock_irqrestore(&priv->lock, flags);
3110}
3111
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003112static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003113{
3114 u32 inta, handled = 0;
3115 u32 inta_fh;
3116 unsigned long flags;
Samuel Ortizd08853a2009-01-23 13:45:17 -08003117#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003118 u32 inta_mask;
3119#endif
3120
3121 spin_lock_irqsave(&priv->lock, flags);
3122
3123 /* Ack/clear/reset pending uCode interrupts.
3124 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3125 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003126 inta = iwl_read32(priv, CSR_INT);
3127 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003128
3129 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3130 * Any new interrupts that happen after this, either while we're
3131 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003132 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3133 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003134
Samuel Ortizd08853a2009-01-23 13:45:17 -08003135#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003136 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003137 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003138 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003139 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3140 inta, inta_mask, inta_fh);
3141 }
3142#endif
3143
3144 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3145 * atomic, make sure that inta covers all the interrupts that
3146 * we've discovered, even if FH interrupt came in just after
3147 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003148 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003149 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003150 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003151 inta |= CSR_INT_BIT_FH_TX;
3152
3153 /* Now service all interrupt bits discovered above. */
3154 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003155 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003156
3157 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003158 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003160 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003161
3162 handled |= CSR_INT_BIT_HW_ERR;
3163
3164 spin_unlock_irqrestore(&priv->lock, flags);
3165
3166 return;
3167 }
3168
Samuel Ortizd08853a2009-01-23 13:45:17 -08003169#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003170 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003171 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003172 if (inta & CSR_INT_BIT_SCD)
3173 IWL_DEBUG_ISR("Scheduler finished to transmit "
3174 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003175
3176 /* Alive notification via Rx interrupt will do the real work */
3177 if (inta & CSR_INT_BIT_ALIVE)
3178 IWL_DEBUG_ISR("Alive interrupt\n");
3179 }
3180#endif
3181 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003182 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003183
Zhu Yib481de92007-09-25 17:54:57 -07003184 /* Error detected by uCode */
3185 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003186 IWL_ERR(priv, "Microcode SW error detected. "
3187 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003188 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003189 handled |= CSR_INT_BIT_SW_ERR;
3190 }
3191
3192 /* uCode wakes up after power-down sleep */
3193 if (inta & CSR_INT_BIT_WAKEUP) {
3194 IWL_DEBUG_ISR("Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003195 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz4f3602c2009-01-19 15:30:25 -08003196 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
3197 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
3198 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
3199 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
3200 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
3201 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003202
3203 handled |= CSR_INT_BIT_WAKEUP;
3204 }
3205
3206 /* All uCode command responses, including Tx command responses,
3207 * Rx "responses" (frame-received notification), and other
3208 * notifications from uCode come through here*/
3209 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003210 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003211 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3212 }
3213
3214 if (inta & CSR_INT_BIT_FH_TX) {
3215 IWL_DEBUG_ISR("Tx interrupt\n");
3216
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003217 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
3218 if (!iwl_grab_nic_access(priv)) {
3219 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08003220 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003221 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003222 }
3223 handled |= CSR_INT_BIT_FH_TX;
3224 }
3225
3226 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003227 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07003228
3229 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003230 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07003231 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003232 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003233 }
3234
3235 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003236 /* only Re-enable if disabled by irq */
3237 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3238 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003239
Samuel Ortizd08853a2009-01-23 13:45:17 -08003240#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003241 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003242 inta = iwl_read32(priv, CSR_INT);
3243 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3244 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003245 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3246 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3247 }
3248#endif
3249 spin_unlock_irqrestore(&priv->lock, flags);
3250}
3251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003252static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003253{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003254 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003255 u32 inta, inta_mask;
3256 u32 inta_fh;
3257 if (!priv)
3258 return IRQ_NONE;
3259
3260 spin_lock(&priv->lock);
3261
3262 /* Disable (but don't clear!) interrupts here to avoid
3263 * back-to-back ISRs and sporadic interrupts from our NIC.
3264 * If we have something to service, the tasklet will re-enable ints.
3265 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003266 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3267 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003268
3269 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003270 inta = iwl_read32(priv, CSR_INT);
3271 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003272
3273 /* Ignore interrupt if there's nothing in NIC to service.
3274 * This may be due to IRQ shared with another device,
3275 * or due to sporadic interrupts thrown from our NIC. */
3276 if (!inta && !inta_fh) {
3277 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3278 goto none;
3279 }
3280
3281 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
3282 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003283 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08003284 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003285 }
3286
3287 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3288 inta, inta_mask, inta_fh);
3289
Joonwoo Park25c03d82008-01-23 10:15:20 -08003290 inta &= ~CSR_INT_BIT_SCD;
3291
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003292 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003293 if (likely(inta || inta_fh))
3294 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08003295unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07003296 spin_unlock(&priv->lock);
3297
3298 return IRQ_HANDLED;
3299
3300 none:
3301 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003302 /* only Re-enable if disabled by irq */
3303 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3304 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003305 spin_unlock(&priv->lock);
3306 return IRQ_NONE;
3307}
3308
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003309static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003310 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08003311 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003312 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07003313{
3314 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01003315 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003316 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003317 u16 passive_dwell = 0;
3318 u16 active_dwell = 0;
3319 int added, i;
3320
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08003321 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01003322 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07003323 return 0;
3324
Johannes Berg8318d782008-01-24 19:38:38 +01003325 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07003326
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08003327 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
3328 passive_dwell = iwl_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07003329
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08003330 if (passive_dwell <= active_dwell)
3331 passive_dwell = active_dwell + 1;
3332
Johannes Berg8318d782008-01-24 19:38:38 +01003333 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02003334 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3335 continue;
3336
Johannes Berg8318d782008-01-24 19:38:38 +01003337 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07003338
Samuel Ortize6148912009-01-23 13:45:15 -08003339 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003340 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08003341 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07003342 scan_ch->channel);
3343 continue;
3344 }
3345
Zhu Yib481de92007-09-25 17:54:57 -07003346 scan_ch->active_dwell = cpu_to_le16(active_dwell);
3347 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08003348 /* If passive , set up for auto-switch
3349 * and use long active_dwell time.
3350 */
3351 if (!is_active || is_channel_passive(ch_info) ||
3352 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
3353 scan_ch->type = 0; /* passive */
3354 if (IWL_UCODE_API(priv->ucode_ver) == 1)
3355 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
3356 } else {
3357 scan_ch->type = 1; /* active */
3358 }
3359
3360 /* Set direct probe bits. These may be used both for active
3361 * scan channels (probes gets sent right away),
3362 * or for passive channels (probes get se sent only after
3363 * hearing clear Rx packet).*/
3364 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
3365 if (n_probes)
3366 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
3367 } else {
3368 /* uCode v1 does not allow setting direct probe bits on
3369 * passive channel. */
3370 if ((scan_ch->type & 1) && n_probes)
3371 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
3372 }
Zhu Yib481de92007-09-25 17:54:57 -07003373
Ben Cahill9fbab512007-11-29 11:09:47 +08003374 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07003375 scan_ch->tpc.dsp_atten = 110;
3376 /* scan_pwr_info->tpc.dsp_atten; */
3377
3378 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01003379 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003380 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
3381 else {
3382 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
3383 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08003384 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08003385 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07003386 */
3387 }
3388
3389 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
3390 scan_ch->channel,
3391 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
3392 (scan_ch->type & 1) ?
3393 active_dwell : passive_dwell);
3394
3395 scan_ch++;
3396 added++;
3397 }
3398
3399 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
3400 return added;
3401}
3402
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003403static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003404 struct ieee80211_rate *rates)
3405{
3406 int i;
3407
3408 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01003409 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
3410 rates[i].hw_value = i; /* Rate scaling will work on indexes */
3411 rates[i].hw_value_short = i;
3412 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08003413 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07003414 /*
Johannes Berg8318d782008-01-24 19:38:38 +01003415 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07003416 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003417 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01003418 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07003419 }
Zhu Yib481de92007-09-25 17:54:57 -07003420 }
3421}
3422
Zhu Yib481de92007-09-25 17:54:57 -07003423/******************************************************************************
3424 *
3425 * uCode download functions
3426 *
3427 ******************************************************************************/
3428
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003429static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003430{
Tomas Winkler98c92212008-01-14 17:46:20 -08003431 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
3432 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
3433 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3434 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
3435 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3436 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07003437}
3438
3439/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003440 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07003441 * looking at all data.
3442 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003443static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07003444{
3445 u32 val;
3446 u32 save_len = len;
3447 int rc = 0;
3448 u32 errcnt;
3449
3450 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
3451
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003452 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003453 if (rc)
3454 return rc;
3455
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003456 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08003457 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07003458
3459 errcnt = 0;
3460 for (; len > 0; len -= sizeof(u32), image++) {
3461 /* read data comes through single port, auto-incr addr */
3462 /* NOTE: Use the debugless read so we don't flood kernel log
3463 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003464 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07003465 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003466 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07003467 "offset 0x%x, is 0x%x, s/b 0x%x\n",
3468 save_len - len, val, le32_to_cpu(*image));
3469 rc = -EIO;
3470 errcnt++;
3471 if (errcnt >= 20)
3472 break;
3473 }
3474 }
3475
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003476 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003477
3478 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08003479 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07003480
3481 return rc;
3482}
3483
3484
3485/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003486 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07003487 * using sample data 100 bytes apart. If these sample points are good,
3488 * it's a pretty good bet that everything between them is good, too.
3489 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003490static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07003491{
3492 u32 val;
3493 int rc = 0;
3494 u32 errcnt = 0;
3495 u32 i;
3496
3497 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
3498
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003499 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003500 if (rc)
3501 return rc;
3502
3503 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
3504 /* read data comes through single port, auto-incr addr */
3505 /* NOTE: Use the debugless read so we don't flood kernel log
3506 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003507 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08003508 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003509 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07003510 if (val != le32_to_cpu(*image)) {
3511#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08003512 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07003513 "offset 0x%x, is 0x%x, s/b 0x%x\n",
3514 i, val, *image);
3515#endif
3516 rc = -EIO;
3517 errcnt++;
3518 if (errcnt >= 3)
3519 break;
3520 }
3521 }
3522
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003523 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003524
3525 return rc;
3526}
3527
3528
3529/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003530 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07003531 * and verify its contents
3532 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003533static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003534{
3535 __le32 *image;
3536 u32 len;
3537 int rc = 0;
3538
3539 /* Try bootstrap */
3540 image = (__le32 *)priv->ucode_boot.v_addr;
3541 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003542 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07003543 if (rc == 0) {
3544 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
3545 return 0;
3546 }
3547
3548 /* Try initialize */
3549 image = (__le32 *)priv->ucode_init.v_addr;
3550 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003551 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07003552 if (rc == 0) {
3553 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
3554 return 0;
3555 }
3556
3557 /* Try runtime/protocol */
3558 image = (__le32 *)priv->ucode_code.v_addr;
3559 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003560 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07003561 if (rc == 0) {
3562 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
3563 return 0;
3564 }
3565
Winkler, Tomas15b16872008-12-19 10:37:33 +08003566 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07003567
Ben Cahill9fbab512007-11-29 11:09:47 +08003568 /* Since nothing seems to match, show first several data entries in
3569 * instruction SRAM, so maybe visual inspection will give a clue.
3570 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07003571 image = (__le32 *)priv->ucode_boot.v_addr;
3572 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003573 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07003574
3575 return rc;
3576}
3577
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003578static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003579{
3580 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003581 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003582}
3583
3584/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003585 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07003586 *
3587 * Copy into buffers for card to fetch via bus-mastering
3588 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003589static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003590{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08003591 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08003592 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07003593 const struct firmware *ucode_raw;
3594 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08003595 const char *name_pre = priv->cfg->fw_name_pre;
3596 const unsigned int api_max = priv->cfg->ucode_api_max;
3597 const unsigned int api_min = priv->cfg->ucode_api_min;
3598 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07003599 u8 *src;
3600 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08003601 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07003602
3603 /* Ask kernel firmware_class module to get the boot firmware off disk.
3604 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08003605 for (index = api_max; index >= api_min; index--) {
3606 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
3607 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
3608 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003609 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08003610 buf, ret);
3611 if (ret == -ENOENT)
3612 continue;
3613 else
3614 goto error;
3615 } else {
3616 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003617 IWL_ERR(priv, "Loaded firmware %s, "
3618 "which is deprecated. "
3619 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08003620 buf, api_max);
3621 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3622 buf, ucode_raw->size);
3623 break;
3624 }
Zhu Yib481de92007-09-25 17:54:57 -07003625 }
3626
Reinette Chatrea0987a82008-12-02 12:14:06 -08003627 if (ret < 0)
3628 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07003629
3630 /* Make sure that we got at least our header! */
3631 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003632 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08003633 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003634 goto err_release;
3635 }
3636
3637 /* Data from ucode file: header followed by uCode images */
3638 ucode = (void *)ucode_raw->data;
3639
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08003640 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08003641 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07003642 inst_size = le32_to_cpu(ucode->inst_size);
3643 data_size = le32_to_cpu(ucode->data_size);
3644 init_size = le32_to_cpu(ucode->init_size);
3645 init_data_size = le32_to_cpu(ucode->init_data_size);
3646 boot_size = le32_to_cpu(ucode->boot_size);
3647
Reinette Chatrea0987a82008-12-02 12:14:06 -08003648 /* api_ver should match the api version forming part of the
3649 * firmware filename ... but we don't check for that and only rely
3650 * on the API version read from firware header from here on forward */
3651
3652 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003653 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08003654 "Driver supports v%u, firmware is v%u.\n",
3655 api_max, api_ver);
3656 priv->ucode_ver = 0;
3657 ret = -EINVAL;
3658 goto err_release;
3659 }
3660 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003661 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08003662 "got %u. New firmware can be obtained "
3663 "from http://www.intellinuxwireless.org.\n",
3664 api_max, api_ver);
3665
Tomas Winkler978785a2008-12-19 10:37:31 +08003666 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
3667 IWL_UCODE_MAJOR(priv->ucode_ver),
3668 IWL_UCODE_MINOR(priv->ucode_ver),
3669 IWL_UCODE_API(priv->ucode_ver),
3670 IWL_UCODE_SERIAL(priv->ucode_ver));
3671
Reinette Chatrea0987a82008-12-02 12:14:06 -08003672 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
3673 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08003674 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
3675 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
3676 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
3677 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
3678 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07003679
Reinette Chatrea0987a82008-12-02 12:14:06 -08003680
Zhu Yib481de92007-09-25 17:54:57 -07003681 /* Verify size of file vs. image size info in file's header */
3682 if (ucode_raw->size < sizeof(*ucode) +
3683 inst_size + data_size + init_size +
3684 init_data_size + boot_size) {
3685
3686 IWL_DEBUG_INFO("uCode file size %d too small\n",
3687 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08003688 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003689 goto err_release;
3690 }
3691
3692 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08003693 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003694 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
3695 inst_size);
3696 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003697 goto err_release;
3698 }
3699
Samuel Ortiz250bdd22008-12-19 10:37:11 +08003700 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003701 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
3702 data_size);
3703 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003704 goto err_release;
3705 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08003706 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003707 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
3708 init_size);
3709 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003710 goto err_release;
3711 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08003712 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003713 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
3714 init_data_size);
3715 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003716 goto err_release;
3717 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08003718 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003719 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
3720 boot_size);
3721 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003722 goto err_release;
3723 }
3724
3725 /* Allocate ucode buffers for card's bus-master loading ... */
3726
3727 /* Runtime instructions and 2 copies of data:
3728 * 1) unmodified from disk
3729 * 2) backup cache for save/restore during power-downs */
3730 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003731 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07003732
3733 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003734 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07003735
3736 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003737 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07003738
3739 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08003740 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07003741 goto err_pci_alloc;
3742
Tomas Winkler90e759d2007-11-29 11:09:41 +08003743 /* Initialization instructions and data */
3744 if (init_size && init_data_size) {
3745 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003746 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08003747
3748 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003749 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08003750
3751 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
3752 goto err_pci_alloc;
3753 }
3754
3755 /* Bootstrap (instructions only, no data) */
3756 if (boot_size) {
3757 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003758 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08003759
3760 if (!priv->ucode_boot.v_addr)
3761 goto err_pci_alloc;
3762 }
3763
Zhu Yib481de92007-09-25 17:54:57 -07003764 /* Copy images into buffers for card's bus-master reads ... */
3765
3766 /* Runtime instructions (first block of data in file) */
3767 src = &ucode->data[0];
3768 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003769 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003770 memcpy(priv->ucode_code.v_addr, src, len);
3771 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
3772 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
3773
3774 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003775 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07003776 src = &ucode->data[inst_size];
3777 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003778 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003779 memcpy(priv->ucode_data.v_addr, src, len);
3780 memcpy(priv->ucode_data_backup.v_addr, src, len);
3781
3782 /* Initialization instructions (3rd block) */
3783 if (init_size) {
3784 src = &ucode->data[inst_size + data_size];
3785 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003786 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
3787 len);
Zhu Yib481de92007-09-25 17:54:57 -07003788 memcpy(priv->ucode_init.v_addr, src, len);
3789 }
3790
3791 /* Initialization data (4th block) */
3792 if (init_data_size) {
3793 src = &ucode->data[inst_size + data_size + init_size];
3794 len = priv->ucode_init_data.len;
3795 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
3796 (int)len);
3797 memcpy(priv->ucode_init_data.v_addr, src, len);
3798 }
3799
3800 /* Bootstrap instructions (5th block) */
3801 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
3802 len = priv->ucode_boot.len;
3803 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
3804 (int)len);
3805 memcpy(priv->ucode_boot.v_addr, src, len);
3806
3807 /* We have our copies now, allow OS release its copies */
3808 release_firmware(ucode_raw);
3809 return 0;
3810
3811 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08003812 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08003813 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003814 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003815
3816 err_release:
3817 release_firmware(ucode_raw);
3818
3819 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08003820 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003821}
3822
3823
3824/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003825 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07003826 *
3827 * Tell initialization uCode where to find runtime uCode.
3828 *
3829 * BSM registers initially contain pointers to initialization uCode.
3830 * We need to replace them to load runtime uCode inst and data,
3831 * and to save runtime data when powering down.
3832 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003833static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003834{
3835 dma_addr_t pinst;
3836 dma_addr_t pdata;
3837 int rc = 0;
3838 unsigned long flags;
3839
3840 /* bits 31:0 for 3945 */
3841 pinst = priv->ucode_code.p_addr;
3842 pdata = priv->ucode_data_backup.p_addr;
3843
3844 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003845 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003846 if (rc) {
3847 spin_unlock_irqrestore(&priv->lock, flags);
3848 return rc;
3849 }
3850
3851 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003852 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
3853 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
3854 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07003855 priv->ucode_data.len);
3856
Tomas Winklera96a27f2008-10-23 23:48:56 -07003857 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07003858 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003859 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07003860 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
3861
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003862 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003863
3864 spin_unlock_irqrestore(&priv->lock, flags);
3865
3866 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
3867
3868 return rc;
3869}
3870
3871/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003872 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07003873 *
3874 * Called after REPLY_ALIVE notification received from "initialize" uCode.
3875 *
Zhu Yib481de92007-09-25 17:54:57 -07003876 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08003877 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003878static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003879{
3880 /* Check alive response for "valid" sign from uCode */
3881 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
3882 /* We had an error bringing up the hardware, so take it
3883 * all the way back down so we can try again */
3884 IWL_DEBUG_INFO("Initialize Alive failed.\n");
3885 goto restart;
3886 }
3887
3888 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
3889 * This is a paranoid check, because we would not have gotten the
3890 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003891 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003892 /* Runtime instruction load was bad;
3893 * take it all the way back down so we can try again */
3894 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
3895 goto restart;
3896 }
3897
3898 /* Send pointers to protocol/runtime uCode image ... init code will
3899 * load and launch runtime uCode, which will send us another "Alive"
3900 * notification. */
3901 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003902 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003903 /* Runtime instruction load won't happen;
3904 * take it all the way back down so we can try again */
3905 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
3906 goto restart;
3907 }
3908 return;
3909
3910 restart:
3911 queue_work(priv->workqueue, &priv->restart);
3912}
3913
3914
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08003915/* temporary */
3916static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
3917 struct sk_buff *skb);
3918
Zhu Yib481de92007-09-25 17:54:57 -07003919/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003920 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07003921 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003922 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07003923 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003924static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003925{
3926 int rc = 0;
3927 int thermal_spin = 0;
3928 u32 rfkill;
3929
3930 IWL_DEBUG_INFO("Runtime Alive received.\n");
3931
3932 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
3933 /* We had an error bringing up the hardware, so take it
3934 * all the way back down so we can try again */
3935 IWL_DEBUG_INFO("Alive failed.\n");
3936 goto restart;
3937 }
3938
3939 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
3940 * This is a paranoid check, because we would not have gotten the
3941 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003942 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003943 /* Runtime instruction load was bad;
3944 * take it all the way back down so we can try again */
3945 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
3946 goto restart;
3947 }
3948
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003949 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003950
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003951 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003952 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003953 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07003954 return;
3955 }
3956
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003957 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07003958 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003959 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003960
3961 if (rfkill & 0x1) {
3962 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07003963 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07003964 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003965 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07003966 thermal_spin++;
3967 udelay(10);
3968 }
3969
3970 if (thermal_spin)
3971 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
3972 thermal_spin * 10);
3973 } else
3974 set_bit(STATUS_RF_KILL_HW, &priv->status);
3975
Ben Cahill9fbab512007-11-29 11:09:47 +08003976 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07003977 set_bit(STATUS_ALIVE, &priv->status);
3978
3979 /* Clear out the uCode error bit if it is set */
3980 clear_bit(STATUS_FW_ERROR, &priv->status);
3981
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08003982 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003983 return;
3984
Johannes Berg36d68252008-05-15 12:55:26 +02003985 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07003986
3987 priv->active_rate = priv->rates_mask;
3988 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
3989
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003990 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07003991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003992 if (iwl3945_is_associated(priv)) {
3993 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003994 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003995
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003996 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
3997 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003998 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3999 } else {
4000 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07004001 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004002 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07004003 }
4004
Ben Cahill9fbab512007-11-29 11:09:47 +08004005 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004006 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004007
4008 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004009 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004010
Zhu Yib481de92007-09-25 17:54:57 -07004011 iwl3945_reg_txpower_periodic(priv);
4012
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07004013 iwl3945_led_register(priv);
4014
Zhu Yib481de92007-09-25 17:54:57 -07004015 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07004016 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08004017 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07004018
4019 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004020 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004021
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08004022 /* reassociate for ADHOC mode */
4023 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
4024 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
4025 priv->vif);
4026 if (beacon)
4027 iwl3945_mac_beacon_update(priv->hw, beacon);
4028 }
4029
Zhu Yib481de92007-09-25 17:54:57 -07004030 return;
4031
4032 restart:
4033 queue_work(priv->workqueue, &priv->restart);
4034}
4035
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004036static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07004037
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004038static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004039{
4040 unsigned long flags;
4041 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4042 struct ieee80211_conf *conf = NULL;
4043
4044 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4045
4046 conf = ieee80211_get_hw_conf(priv->hw);
4047
4048 if (!exit_pending)
4049 set_bit(STATUS_EXIT_PENDING, &priv->status);
4050
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07004051 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004052 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004053
4054 /* Unblock any waiting calls */
4055 wake_up_interruptible_all(&priv->wait_command_queue);
4056
Zhu Yib481de92007-09-25 17:54:57 -07004057 /* Wipe out the EXIT_PENDING status bit if we are not actually
4058 * exiting the module */
4059 if (!exit_pending)
4060 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4061
4062 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004063 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07004064
4065 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004066 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004067 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004068 spin_unlock_irqrestore(&priv->lock, flags);
4069 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004070
4071 if (priv->mac80211_registered)
4072 ieee80211_stop_queues(priv->hw);
4073
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004074 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07004075 * clear all bits but the RF Kill and SUSPEND bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004076 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004077 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4078 STATUS_RF_KILL_HW |
4079 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4080 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004081 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4082 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004083 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004084 STATUS_IN_SUSPEND |
4085 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
4086 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07004087 goto exit;
4088 }
4089
4090 /* ...otherwise clear out all the status bits but the RF Kill and
4091 * SUSPEND bits and continue taking the NIC down. */
4092 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4093 STATUS_RF_KILL_HW |
4094 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4095 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004096 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4097 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004098 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4099 STATUS_IN_SUSPEND |
4100 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004101 STATUS_FW_ERROR |
4102 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
4103 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07004104
Abbas, Mohamede9414b62009-01-20 21:33:55 -08004105 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004106 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004107 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07004108 spin_unlock_irqrestore(&priv->lock, flags);
4109
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004110 iwl3945_hw_txq_ctx_stop(priv);
4111 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004112
4113 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004114 if (!iwl_grab_nic_access(priv)) {
4115 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004116 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004117 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004118 }
4119 spin_unlock_irqrestore(&priv->lock, flags);
4120
4121 udelay(5);
4122
Abbas, Mohamede9414b62009-01-20 21:33:55 -08004123 if (exit_pending || test_bit(STATUS_IN_SUSPEND, &priv->status))
4124 priv->cfg->ops->lib->apm_ops.stop(priv);
4125 else
4126 priv->cfg->ops->lib->apm_ops.reset(priv);
4127
Zhu Yib481de92007-09-25 17:54:57 -07004128 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08004129 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07004130
4131 if (priv->ibss_beacon)
4132 dev_kfree_skb(priv->ibss_beacon);
4133 priv->ibss_beacon = NULL;
4134
4135 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004136 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004137}
4138
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004139static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004140{
4141 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004142 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004143 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08004144
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004145 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004146}
4147
4148#define MAX_HW_RESTARTS 5
4149
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004150static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004151{
4152 int rc, i;
4153
4154 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004155 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07004156 return -EIO;
4157 }
4158
4159 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004160 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07004161 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08004162 return -ENODEV;
4163 }
4164
Reinette Chatree903fbd2008-01-30 22:05:15 -08004165 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004166 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08004167 return -EIO;
4168 }
4169
Zhu Yie655b9f2008-01-24 02:19:38 -08004170 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004171 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08004172 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4173 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4174 else {
4175 set_bit(STATUS_RF_KILL_HW, &priv->status);
4176 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004177 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08004178 return -ENODEV;
4179 }
Zhu Yib481de92007-09-25 17:54:57 -07004180 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02004181
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004182 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07004183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004184 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004185 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004186 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07004187 return rc;
4188 }
4189
4190 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004191 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4192 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004193 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4194
4195 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004196 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004197 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004198
4199 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004200 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4201 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07004202
4203 /* Copy original ucode data image from disk into backup cache.
4204 * This will be used to initialize the on-board processor's
4205 * data SRAM for a clean start when the runtime program first loads. */
4206 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08004207 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07004208
Zhu Yie655b9f2008-01-24 02:19:38 -08004209 /* We return success when we resume from suspend and rf_kill is on. */
4210 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
4211 return 0;
4212
Zhu Yib481de92007-09-25 17:54:57 -07004213 for (i = 0; i < MAX_HW_RESTARTS; i++) {
4214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004215 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004216
4217 /* load bootstrap state machine,
4218 * load bootstrap program into processor's memory,
4219 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08004220 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004221
4222 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004223 IWL_ERR(priv,
4224 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07004225 continue;
4226 }
4227
4228 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004229 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004230
Zhu Yib481de92007-09-25 17:54:57 -07004231 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
4232
4233 return 0;
4234 }
4235
4236 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004237 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08004238 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004239
4240 /* tried to restart and config the device for as long as our
4241 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08004242 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07004243 return -EIO;
4244}
4245
4246
4247/*****************************************************************************
4248 *
4249 * Workqueue callbacks
4250 *
4251 *****************************************************************************/
4252
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004253static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004254{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004255 struct iwl_priv *priv =
4256 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004257
4258 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4259 return;
4260
4261 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004262 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004263 mutex_unlock(&priv->mutex);
4264}
4265
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004266static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004267{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004268 struct iwl_priv *priv =
4269 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004270
4271 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4272 return;
4273
4274 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004275 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004276 mutex_unlock(&priv->mutex);
4277}
4278
Helmut Schaa26635162009-01-15 09:38:44 +01004279static void iwl3945_rfkill_poll(struct work_struct *data)
4280{
4281 struct iwl_priv *priv =
4282 container_of(data, struct iwl_priv, rfkill_poll.work);
4283 unsigned long status = priv->status;
4284
4285 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4286 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4287 else
4288 set_bit(STATUS_RF_KILL_HW, &priv->status);
4289
4290 if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
4291 queue_work(priv->workqueue, &priv->rf_kill);
4292
4293 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
4294 round_jiffies_relative(2 * HZ));
4295
4296}
4297
Zhu Yib481de92007-09-25 17:54:57 -07004298#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004299static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004300{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004301 struct iwl_priv *priv =
4302 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08004303 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07004304 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004305 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07004306 .meta.flags = CMD_SIZE_HUGE,
4307 };
4308 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004309 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07004310 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004311 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01004312 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04004313 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07004314
4315 conf = ieee80211_get_hw_conf(priv->hw);
4316
4317 mutex_lock(&priv->mutex);
4318
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004319 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004320 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004321 goto done;
4322 }
4323
Tomas Winklera96a27f2008-10-23 23:48:56 -07004324 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07004325 * was given the chance to run... */
4326 if (!test_bit(STATUS_SCANNING, &priv->status))
4327 goto done;
4328
4329 /* This should never be called or scheduled if there is currently
4330 * a scan active in the hardware. */
4331 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
4332 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
4333 "Ignoring second request.\n");
4334 rc = -EIO;
4335 goto done;
4336 }
4337
4338 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4339 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4340 goto done;
4341 }
4342
4343 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4344 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
4345 goto done;
4346 }
4347
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004348 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004349 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4350 goto done;
4351 }
4352
4353 if (!test_bit(STATUS_READY, &priv->status)) {
4354 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
4355 goto done;
4356 }
4357
4358 if (!priv->scan_bands) {
4359 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
4360 goto done;
4361 }
4362
Winkler, Tomas805cee52009-01-19 15:30:28 -08004363 if (!priv->scan) {
4364 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07004365 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Winkler, Tomas805cee52009-01-19 15:30:28 -08004366 if (!priv->scan) {
Zhu Yib481de92007-09-25 17:54:57 -07004367 rc = -ENOMEM;
4368 goto done;
4369 }
4370 }
Winkler, Tomas805cee52009-01-19 15:30:28 -08004371 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004372 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07004373
4374 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
4375 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
4376
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004377 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004378 u16 interval = 0;
4379 u32 extra;
4380 u32 suspend_time = 100;
4381 u32 scan_suspend_time = 100;
4382 unsigned long flags;
4383
4384 IWL_DEBUG_INFO("Scanning while associated...\n");
4385
4386 spin_lock_irqsave(&priv->lock, flags);
4387 interval = priv->beacon_int;
4388 spin_unlock_irqrestore(&priv->lock, flags);
4389
4390 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08004391 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07004392 if (!interval)
4393 interval = suspend_time;
4394 /*
4395 * suspend time format:
4396 * 0-19: beacon interval in usec (time before exec.)
4397 * 20-23: 0
4398 * 24-31: number of beacons (suspend between channels)
4399 */
4400
4401 extra = (suspend_time / interval) << 24;
4402 scan_suspend_time = 0xFF0FFFFF &
4403 (extra | ((suspend_time % interval) * 1024));
4404
4405 scan->suspend_time = cpu_to_le32(scan_suspend_time);
4406 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
4407 scan_suspend_time, interval);
4408 }
4409
4410 /* We should add the ability for user to lock to PASSIVE ONLY */
4411 if (priv->one_direct_scan) {
4412 IWL_DEBUG_SCAN
4413 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04004414 print_ssid(ssid, priv->direct_ssid,
4415 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07004416 scan->direct_scan[0].id = WLAN_EID_SSID;
4417 scan->direct_scan[0].len = priv->direct_ssid_len;
4418 memcpy(scan->direct_scan[0].ssid,
4419 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004420 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004421 } else
Bill Moss786b4552008-04-17 16:03:40 -07004422 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004423
4424 /* We don't build a direct scan probe request; the uCode will do
4425 * that based on the direct_mask added to each channel entry */
Zhu Yib481de92007-09-25 17:54:57 -07004426 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08004427 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07004428 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4429
4430 /* flags + rate selection */
4431
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004432 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07004433 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
4434 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
4435 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004436 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004437 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07004438 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
4439 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01004440 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004441 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004442 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07004443 goto done;
4444 }
4445
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08004446 scan->tx_cmd.len = cpu_to_le16(
4447 iwl_fill_probe_req(priv, band,
4448 (struct ieee80211_mgmt *)scan->data,
4449 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
4450
Zhu Yib481de92007-09-25 17:54:57 -07004451 /* select Rx antennas */
4452 scan->flags |= iwl3945_get_antenna_flags(priv);
4453
Johannes Berg05c914f2008-09-11 00:01:58 +02004454 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07004455 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
4456
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004457 scan->channel_count =
4458 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
4459 n_probes,
4460 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07004461
Reinette Chatre14b54332008-11-04 12:21:35 -08004462 if (scan->channel_count == 0) {
4463 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
4464 goto done;
4465 }
4466
Zhu Yib481de92007-09-25 17:54:57 -07004467 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004468 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07004469 cmd.data = scan;
4470 scan->len = cpu_to_le16(cmd.len);
4471
4472 set_bit(STATUS_SCAN_HW, &priv->status);
Samuel Ortiz518099a2009-01-19 15:30:27 -08004473 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07004474 if (rc)
4475 goto done;
4476
4477 queue_delayed_work(priv->workqueue, &priv->scan_check,
4478 IWL_SCAN_CHECK_WATCHDOG);
4479
4480 mutex_unlock(&priv->mutex);
4481 return;
4482
4483 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08004484 /* can not perform scan make sure we clear scanning
4485 * bits from status so next scan request can be performed.
4486 * if we dont clear scanning status bit here all next scan
4487 * will fail
4488 */
4489 clear_bit(STATUS_SCAN_HW, &priv->status);
4490 clear_bit(STATUS_SCANNING, &priv->status);
4491
Ian Schram01ebd062007-10-25 17:15:22 +08004492 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07004493 queue_work(priv->workqueue, &priv->scan_completed);
4494 mutex_unlock(&priv->mutex);
4495}
4496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004497static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004498{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004499 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07004500
4501 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4502 return;
4503
4504 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004505 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004506 mutex_unlock(&priv->mutex);
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004507 iwl_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004508}
4509
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004510static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004511{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004512 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07004513
4514 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4515 return;
4516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004517 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004518 queue_work(priv->workqueue, &priv->up);
4519}
4520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004521static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004522{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004523 struct iwl_priv *priv =
4524 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07004525
4526 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4527 return;
4528
4529 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004530 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004531 mutex_unlock(&priv->mutex);
4532}
4533
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004534#define IWL_DELAY_NEXT_SCAN (HZ*2)
4535
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004536static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004537{
Zhu Yib481de92007-09-25 17:54:57 -07004538 int rc = 0;
4539 struct ieee80211_conf *conf = NULL;
4540
Johannes Berg05c914f2008-09-11 00:01:58 +02004541 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004542 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07004543 return;
4544 }
4545
4546
Johannes Berge1749612008-10-27 15:59:26 -07004547 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004548 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004549
4550 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4551 return;
4552
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08004553 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004554 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08004555
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004556 iwl_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08004557
Zhu Yib481de92007-09-25 17:54:57 -07004558 conf = ieee80211_get_hw_conf(priv->hw);
4559
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004560 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004561 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004562
Tomas Winkler28afaf92008-12-19 10:37:06 +08004563 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004564 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08004565 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07004566 sizeof(priv->rxon_timing), &priv->rxon_timing);
4567 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004568 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07004569 "Attempting to continue.\n");
4570
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004571 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07004572
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004573 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07004574
4575 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
4576 priv->assoc_id, priv->beacon_int);
4577
4578 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004579 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07004580 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004581 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07004582
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004583 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07004584 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004585 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07004586 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004587 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07004588
Johannes Berg05c914f2008-09-11 00:01:58 +02004589 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004590 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07004591
4592 }
4593
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004594 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004595
4596 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02004597 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004598 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07004599 break;
4600
Johannes Berg05c914f2008-09-11 00:01:58 +02004601 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07004602
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08004603 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004604 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004605 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01004606 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004607 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
4608 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004609 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
4610 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004611
4612 break;
4613
4614 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08004615 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004616 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07004617 break;
4618 }
4619
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004620 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08004621
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004622 /* we have just associated, don't start scan too early */
4623 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08004624}
4625
Johannes Berge8975582008-10-09 12:18:51 +02004626static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08004627
Zhu Yib481de92007-09-25 17:54:57 -07004628/*****************************************************************************
4629 *
4630 * mac80211 entry point functions
4631 *
4632 *****************************************************************************/
4633
Zhu Yi5a669262008-01-14 17:46:18 -08004634#define UCODE_READY_TIMEOUT (2 * HZ)
4635
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004636static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004637{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004638 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08004639 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004640
4641 IWL_DEBUG_MAC80211("enter\n");
4642
4643 /* we should be verifying the device is ready to be opened */
4644 mutex_lock(&priv->mutex);
4645
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004646 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08004647 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
4648 * ucode filename and max sizes are card-specific. */
4649
4650 if (!priv->ucode_code.len) {
4651 ret = iwl3945_read_ucode(priv);
4652 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004653 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08004654 mutex_unlock(&priv->mutex);
4655 goto out_release_irq;
4656 }
4657 }
4658
Zhu Yie655b9f2008-01-24 02:19:38 -08004659 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08004660
Zhu Yib481de92007-09-25 17:54:57 -07004661 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08004662
Samuel Ortizc0af96a2009-01-21 18:27:54 +01004663 iwl_rfkill_set_hw_state(priv);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02004664
Zhu Yie655b9f2008-01-24 02:19:38 -08004665 if (ret)
4666 goto out_release_irq;
4667
4668 IWL_DEBUG_INFO("Start UP work.\n");
4669
4670 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
4671 return 0;
4672
Zhu Yi5a669262008-01-14 17:46:18 -08004673 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
4674 * mac80211 will not be run successfully. */
4675 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
4676 test_bit(STATUS_READY, &priv->status),
4677 UCODE_READY_TIMEOUT);
4678 if (!ret) {
4679 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004680 IWL_ERR(priv,
4681 "Wait for START_ALIVE timeout after %dms.\n",
4682 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08004683 ret = -ETIMEDOUT;
4684 goto out_release_irq;
4685 }
4686 }
4687
Helmut Schaa26635162009-01-15 09:38:44 +01004688 /* ucode is running and will send rfkill notifications,
4689 * no need to poll the killswitch state anymore */
4690 cancel_delayed_work(&priv->rfkill_poll);
4691
Zhu Yie655b9f2008-01-24 02:19:38 -08004692 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004693 IWL_DEBUG_MAC80211("leave\n");
4694 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08004695
4696out_release_irq:
Zhu Yie655b9f2008-01-24 02:19:38 -08004697 priv->is_open = 0;
4698 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08004699 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004700}
4701
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004702static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004703{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004704 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004705
4706 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004707
Zhu Yie655b9f2008-01-24 02:19:38 -08004708 if (!priv->is_open) {
4709 IWL_DEBUG_MAC80211("leave - skip\n");
4710 return;
4711 }
4712
Zhu Yib481de92007-09-25 17:54:57 -07004713 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08004714
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004715 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08004716 /* stop mac, cancel any scan request and clear
4717 * RXON_FILTER_ASSOC_MSK BIT
4718 */
Zhu Yi5a669262008-01-14 17:46:18 -08004719 mutex_lock(&priv->mutex);
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004720 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08004721 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08004722 }
4723
Zhu Yi5a669262008-01-14 17:46:18 -08004724 iwl3945_down(priv);
4725
4726 flush_workqueue(priv->workqueue);
Helmut Schaa26635162009-01-15 09:38:44 +01004727
4728 /* start polling the killswitch state again */
4729 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
4730 round_jiffies_relative(2 * HZ));
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08004731
Zhu Yib481de92007-09-25 17:54:57 -07004732 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004733}
4734
Johannes Berge039fa42008-05-15 12:55:29 +02004735static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07004736{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004737 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004738
4739 IWL_DEBUG_MAC80211("enter\n");
4740
Zhu Yib481de92007-09-25 17:54:57 -07004741 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02004742 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07004743
Johannes Berge039fa42008-05-15 12:55:29 +02004744 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07004745 dev_kfree_skb_any(skb);
4746
4747 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08004748 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07004749}
4750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004751static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004752 struct ieee80211_if_init_conf *conf)
4753{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004754 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004755 unsigned long flags;
4756
Johannes Berg32bfd352007-12-19 01:31:26 +01004757 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07004758
Johannes Berg32bfd352007-12-19 01:31:26 +01004759 if (priv->vif) {
4760 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02004761 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07004762 }
4763
4764 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01004765 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07004766 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07004767
4768 spin_unlock_irqrestore(&priv->lock, flags);
4769
4770 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02004771
4772 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07004773 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02004774 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
4775 }
4776
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004777 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08004778 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07004779
Zhu Yib481de92007-09-25 17:54:57 -07004780 mutex_unlock(&priv->mutex);
4781
Zhu Yi5a669262008-01-14 17:46:18 -08004782 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004783 return 0;
4784}
4785
4786/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004787 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07004788 *
4789 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
4790 * be set inappropriately and the driver currently sets the hardware up to
4791 * use it whenever needed.
4792 */
Johannes Berge8975582008-10-09 12:18:51 +02004793static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07004794{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004795 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004796 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02004797 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07004798 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08004799 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004800
4801 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01004802 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07004803
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004804 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004805 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08004806 ret = -EIO;
4807 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004808 }
4809
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08004810 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07004811 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08004812 IWL_DEBUG_MAC80211("leave - scanning\n");
4813 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004814 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08004815 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004816 }
4817
4818 spin_lock_irqsave(&priv->lock, flags);
4819
Samuel Ortize6148912009-01-23 13:45:15 -08004820 ch_info = iwl_get_channel_info(priv, conf->channel->band,
4821 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07004822 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004823 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004824 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07004825 IWL_DEBUG_MAC80211("leave - invalid channel\n");
4826 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08004827 ret = -EINVAL;
4828 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004829 }
4830
Johannes Berg8318d782008-01-24 19:38:38 +01004831 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07004832
Johannes Berg8318d782008-01-24 19:38:38 +01004833 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07004834
4835 /* The list of supported rates and rate mask can be different
4836 * for each phymode; since the phymode may have changed, reset
4837 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004838 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004839
4840 spin_unlock_irqrestore(&priv->lock, flags);
4841
4842#ifdef IEEE80211_CONF_CHANNEL_SWITCH
4843 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004844 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08004845 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004846 }
4847#endif
4848
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004849 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07004850
4851 if (!conf->radio_enabled) {
4852 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08004853 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004854 }
4855
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004856 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004857 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08004858 ret = -EIO;
4859 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07004860 }
4861
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004862 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004863
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004864 if (memcmp(&priv->active39_rxon,
4865 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004866 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004867 else
4868 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
4869
4870 IWL_DEBUG_MAC80211("leave\n");
4871
Zhu Yi76bb77e2007-11-22 10:53:22 +08004872out:
Zhu Yia0646472007-12-20 14:10:01 +08004873 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004874 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08004875 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004876}
4877
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004878static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004879{
4880 int rc = 0;
4881
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08004882 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07004883 return;
4884
4885 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08004886 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07004887
4888 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004889 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004890 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004891
4892 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08004893 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004894 iwl3945_setup_rxon_timing(priv);
Samuel Ortiz518099a2009-01-19 15:30:27 -08004895 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
4896 sizeof(priv->rxon_timing),
4897 &priv->rxon_timing);
Zhu Yib481de92007-09-25 17:54:57 -07004898 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004899 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07004900 "Attempting to continue.\n");
4901
4902 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004903 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07004904 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004905 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07004906 RXON_FLG_SHORT_PREAMBLE_MSK;
4907 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004908 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07004909 ~RXON_FLG_SHORT_PREAMBLE_MSK;
4910
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004911 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07004912 if (priv->assoc_capability &
4913 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004914 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07004915 RXON_FLG_SHORT_SLOT_MSK;
4916 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004917 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07004918 ~RXON_FLG_SHORT_SLOT_MSK;
4919
Johannes Berg05c914f2008-09-11 00:01:58 +02004920 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004921 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07004922 ~RXON_FLG_SHORT_SLOT_MSK;
4923 }
4924 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004925 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004926 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08004927 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08004928 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004929 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004930
4931 /* FIXME - we need to add code here to detect a totally new
4932 * configuration, reset the AP, unassoc, rxon timing, assoc,
4933 * clear sta table, add BCAST sta... */
4934}
4935
Johannes Berg32bfd352007-12-19 01:31:26 +01004936static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
4937 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004938 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07004939{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004940 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004941 int rc;
4942
4943 if (conf == NULL)
4944 return -EIO;
4945
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08004946 if (priv->vif != vif) {
4947 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08004948 return 0;
4949 }
4950
Johannes Berg9d139c82008-07-09 14:40:37 +02004951 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02004952 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02004953 conf->changed & IEEE80211_IFCC_BEACON) {
4954 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
4955 if (!beacon)
4956 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08004957 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02004958 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08004959 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02004960 if (rc)
4961 return rc;
4962 }
4963
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004964 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08004965 return -EAGAIN;
4966
Zhu Yib481de92007-09-25 17:54:57 -07004967 mutex_lock(&priv->mutex);
4968
Zhu Yib481de92007-09-25 17:54:57 -07004969 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07004970 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07004971
Johannes Berg4150c572007-09-17 01:29:23 -04004972/*
4973 * very dubious code was here; the probe filtering flag is never set:
4974 *
Zhu Yib481de92007-09-25 17:54:57 -07004975 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4976 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04004977 */
Zhu Yib481de92007-09-25 17:54:57 -07004978
Johannes Berg05c914f2008-09-11 00:01:58 +02004979 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07004980 if (!conf->bssid) {
4981 conf->bssid = priv->mac_addr;
4982 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07004983 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
4984 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07004985 }
4986 if (priv->ibss_beacon)
4987 dev_kfree_skb(priv->ibss_beacon);
4988
Johannes Berg9d139c82008-07-09 14:40:37 +02004989 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07004990 }
4991
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08004992 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08004993 goto done;
4994
Zhu Yib481de92007-09-25 17:54:57 -07004995 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4996 !is_multicast_ether_addr(conf->bssid)) {
4997 /* If there is currently a HW scan going on in the background
4998 * then we need to cancel it else the RXON below will fail. */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08004999 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005000 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07005001 "after 100ms\n");
5002 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5003 mutex_unlock(&priv->mutex);
5004 return -EAGAIN;
5005 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005006 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005007
5008 /* TODO: Audit driver for usage of these members and see
5009 * if mac80211 deprecates them (priv->bssid looks like it
5010 * shouldn't be there, but I haven't scanned the IBSS code
5011 * to verify) - jpk */
5012 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5013
Johannes Berg05c914f2008-09-11 00:01:58 +02005014 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005015 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005016 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005017 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02005018 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005019 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005020 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005021 }
5022
5023 } else {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005024 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005025 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005026 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005027 }
5028
Mohamed Abbasfde35712007-11-29 11:10:15 +08005029 done:
Zhu Yib481de92007-09-25 17:54:57 -07005030 IWL_DEBUG_MAC80211("leave\n");
5031 mutex_unlock(&priv->mutex);
5032
5033 return 0;
5034}
5035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005036static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04005037 unsigned int changed_flags,
5038 unsigned int *total_flags,
5039 int mc_count, struct dev_addr_list *mc_list)
5040{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005041 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005042 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08005043
Zhu, Yi352bc8d2008-11-12 13:14:09 -08005044 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
5045 changed_flags, *total_flags);
5046
5047 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
5048 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
5049 *filter_flags |= RXON_FILTER_PROMISC_MSK;
5050 else
5051 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08005052 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08005053 if (changed_flags & FIF_ALLMULTI) {
5054 if (*total_flags & FIF_ALLMULTI)
5055 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
5056 else
5057 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
5058 }
5059 if (changed_flags & FIF_CONTROL) {
5060 if (*total_flags & FIF_CONTROL)
5061 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
5062 else
5063 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
5064 }
5065 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
5066 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
5067 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
5068 else
5069 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
5070 }
5071
5072 /* We avoid iwl_commit_rxon here to commit the new filter flags
5073 * since mac80211 will call ieee80211_hw_config immediately.
5074 * (mc_list is not supported at this time). Otherwise, we need to
5075 * queue a background iwl_commit_rxon work.
5076 */
5077
5078 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08005079 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04005080}
5081
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005082static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005083 struct ieee80211_if_init_conf *conf)
5084{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005085 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005086
5087 IWL_DEBUG_MAC80211("enter\n");
5088
5089 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005090
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005091 if (iwl_is_ready_rf(priv)) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005092 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005093 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08005094 iwl3945_commit_rxon(priv);
5095 }
Johannes Berg32bfd352007-12-19 01:31:26 +01005096 if (priv->vif == conf->vif) {
5097 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005098 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005099 }
5100 mutex_unlock(&priv->mutex);
5101
5102 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005103}
5104
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005105#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
5106
5107static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
5108 struct ieee80211_vif *vif,
5109 struct ieee80211_bss_conf *bss_conf,
5110 u32 changes)
5111{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005112 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005113
5114 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
5115
5116 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
5117 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
5118 bss_conf->use_short_preamble);
5119 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005120 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005121 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005122 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005123 }
5124
5125 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
5126 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
5127 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005128 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005129 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005130 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005131 }
5132
5133 if (changes & BSS_CHANGED_ASSOC) {
5134 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
5135 /* This should never happen as this function should
5136 * never be called from interrupt context. */
5137 if (WARN_ON_ONCE(in_interrupt()))
5138 return;
5139 if (bss_conf->assoc) {
5140 priv->assoc_id = bss_conf->aid;
5141 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08005142 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005143 priv->assoc_capability = bss_conf->assoc_capability;
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08005144 priv->power_data.dtim_period = bss_conf->dtim_period;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005145 priv->next_scan_jiffies = jiffies +
5146 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
5147 mutex_lock(&priv->mutex);
5148 iwl3945_post_associate(priv);
5149 mutex_unlock(&priv->mutex);
5150 } else {
5151 priv->assoc_id = 0;
5152 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
5153 }
5154 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
5155 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
5156 iwl3945_send_rxon_assoc(priv);
5157 }
5158
5159}
5160
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005161static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07005162{
5163 int rc = 0;
5164 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005165 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04005166 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07005167
5168 IWL_DEBUG_MAC80211("enter\n");
5169
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005170 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005171 spin_lock_irqsave(&priv->lock, flags);
5172
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005173 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005174 rc = -EIO;
5175 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
5176 goto out_unlock;
5177 }
5178
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005179 /* we don't schedule scan within next_scan_jiffies period */
5180 if (priv->next_scan_jiffies &&
5181 time_after(priv->next_scan_jiffies, jiffies)) {
5182 rc = -EAGAIN;
5183 goto out_unlock;
5184 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08005185 /* if we just finished scan ask for delay for a broadcast scan */
5186 if ((len == 0) && priv->last_scan_jiffies &&
5187 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
5188 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07005189 rc = -EAGAIN;
5190 goto out_unlock;
5191 }
5192 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005193 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04005194 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07005195
5196 priv->one_direct_scan = 1;
5197 priv->direct_ssid_len = (u8)
5198 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
5199 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005200 } else
5201 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005202
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005203 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005204
5205 IWL_DEBUG_MAC80211("leave\n");
5206
5207out_unlock:
5208 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005209 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005210
5211 return rc;
5212}
5213
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005214static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01005215 struct ieee80211_vif *vif,
5216 struct ieee80211_sta *sta,
5217 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07005218{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005219 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01005220 const u8 *addr;
Winkler, Tomas42986792009-01-19 15:30:22 -08005221 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005222 u8 sta_id;
5223
5224 IWL_DEBUG_MAC80211("enter\n");
5225
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08005226 if (iwl3945_mod_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07005227 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
5228 return -EOPNOTSUPP;
5229 }
5230
Winkler, Tomas42986792009-01-19 15:30:22 -08005231 addr = sta ? sta->addr : iwl_bcast_addr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005232 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07005233 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07005234 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
5235 addr);
Zhu Yib481de92007-09-25 17:54:57 -07005236 return -EINVAL;
5237 }
5238
5239 mutex_lock(&priv->mutex);
5240
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005241 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005242
Zhu Yib481de92007-09-25 17:54:57 -07005243 switch (cmd) {
5244 case SET_KEY:
Winkler, Tomas42986792009-01-19 15:30:22 -08005245 ret = iwl3945_update_sta_key_info(priv, key, sta_id);
5246 if (!ret) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005247 iwl3945_set_rxon_hwcrypto(priv, 1);
5248 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005249 key->hw_key_idx = sta_id;
5250 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
5251 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
5252 }
5253 break;
5254 case DISABLE_KEY:
Winkler, Tomas42986792009-01-19 15:30:22 -08005255 ret = iwl3945_clear_sta_key_info(priv, sta_id);
5256 if (!ret) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005257 iwl3945_set_rxon_hwcrypto(priv, 0);
5258 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005259 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
5260 }
5261 break;
5262 default:
Winkler, Tomas42986792009-01-19 15:30:22 -08005263 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005264 }
5265
5266 IWL_DEBUG_MAC80211("leave\n");
5267 mutex_unlock(&priv->mutex);
5268
Winkler, Tomas42986792009-01-19 15:30:22 -08005269 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005270}
5271
Johannes Berge100bb62008-04-30 18:51:21 +02005272static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07005273 const struct ieee80211_tx_queue_params *params)
5274{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005275 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005276 unsigned long flags;
5277 int q;
Zhu Yib481de92007-09-25 17:54:57 -07005278
5279 IWL_DEBUG_MAC80211("enter\n");
5280
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005281 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005282 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5283 return -EIO;
5284 }
5285
5286 if (queue >= AC_NUM) {
5287 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5288 return 0;
5289 }
5290
Zhu Yib481de92007-09-25 17:54:57 -07005291 q = AC_NUM - 1 - queue;
5292
5293 spin_lock_irqsave(&priv->lock, flags);
5294
5295 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5296 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5297 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5298 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01005299 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07005300
5301 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
5302 priv->qos_data.qos_active = 1;
5303
5304 spin_unlock_irqrestore(&priv->lock, flags);
5305
5306 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02005307 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005308 iwl3945_activate_qos(priv, 1);
5309 else if (priv->assoc_id && iwl3945_is_associated(priv))
5310 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005311
5312 mutex_unlock(&priv->mutex);
5313
Zhu Yib481de92007-09-25 17:54:57 -07005314 IWL_DEBUG_MAC80211("leave\n");
5315 return 0;
5316}
5317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005318static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005319 struct ieee80211_tx_queue_stats *stats)
5320{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005321 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005322 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08005323 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08005324 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07005325 unsigned long flags;
5326
5327 IWL_DEBUG_MAC80211("enter\n");
5328
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005329 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005330 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5331 return -EIO;
5332 }
5333
5334 spin_lock_irqsave(&priv->lock, flags);
5335
5336 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08005337 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07005338 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08005339 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07005340
Johannes Berg57ffc582008-04-29 17:18:59 +02005341 stats[i].len = q->n_window - avail;
5342 stats[i].limit = q->n_window - q->high_mark;
5343 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07005344
5345 }
5346 spin_unlock_irqrestore(&priv->lock, flags);
5347
5348 IWL_DEBUG_MAC80211("leave\n");
5349
5350 return 0;
5351}
5352
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005353static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005354{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005355 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005356 unsigned long flags;
5357
5358 mutex_lock(&priv->mutex);
5359 IWL_DEBUG_MAC80211("enter\n");
5360
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005361 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005362
Zhu Yib481de92007-09-25 17:54:57 -07005363 spin_lock_irqsave(&priv->lock, flags);
5364 priv->assoc_id = 0;
5365 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005366
5367 /* new association get rid of ibss beacon skb */
5368 if (priv->ibss_beacon)
5369 dev_kfree_skb(priv->ibss_beacon);
5370
5371 priv->ibss_beacon = NULL;
5372
5373 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08005374 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02005375 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07005376 priv->beacon_int = 0;
5377
5378 spin_unlock_irqrestore(&priv->lock, flags);
5379
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005380 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005381 IWL_DEBUG_MAC80211("leave - not ready\n");
5382 mutex_unlock(&priv->mutex);
5383 return;
5384 }
5385
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005386 /* we are restarting association process
5387 * clear RXON_FILTER_ASSOC_MSK bit
5388 */
Johannes Berg05c914f2008-09-11 00:01:58 +02005389 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005390 iwl_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005391 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005392 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005393 }
5394
Zhu Yib481de92007-09-25 17:54:57 -07005395 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02005396 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005397
Zhu Yib481de92007-09-25 17:54:57 -07005398 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
5399 mutex_unlock(&priv->mutex);
5400 return;
5401 }
5402
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005403 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005404
5405 mutex_unlock(&priv->mutex);
5406
5407 IWL_DEBUG_MAC80211("leave\n");
5408
5409}
5410
Johannes Berge039fa42008-05-15 12:55:29 +02005411static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07005412{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005413 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005414 unsigned long flags;
5415
Zhu Yib481de92007-09-25 17:54:57 -07005416 IWL_DEBUG_MAC80211("enter\n");
5417
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005418 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005419 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07005420 return -EIO;
5421 }
5422
Johannes Berg05c914f2008-09-11 00:01:58 +02005423 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07005424 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07005425 return -EIO;
5426 }
5427
5428 spin_lock_irqsave(&priv->lock, flags);
5429
5430 if (priv->ibss_beacon)
5431 dev_kfree_skb(priv->ibss_beacon);
5432
5433 priv->ibss_beacon = skb;
5434
5435 priv->assoc_id = 0;
5436
5437 IWL_DEBUG_MAC80211("leave\n");
5438 spin_unlock_irqrestore(&priv->lock, flags);
5439
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005440 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005441
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08005442 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005443
Zhu Yib481de92007-09-25 17:54:57 -07005444
5445 return 0;
5446}
5447
5448/*****************************************************************************
5449 *
5450 * sysfs attributes
5451 *
5452 *****************************************************************************/
5453
Samuel Ortizd08853a2009-01-23 13:45:17 -08005454#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07005455
5456/*
5457 * The following adds a new attribute to the sysfs representation
5458 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
5459 * used for controlling the debug level.
5460 *
5461 * See the level definitions in iwl for details.
5462 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08005463static ssize_t show_debug_level(struct device *d,
5464 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07005465{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005466 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08005467
5468 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07005469}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08005470static ssize_t store_debug_level(struct device *d,
5471 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07005472 const char *buf, size_t count)
5473{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005474 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08005475 unsigned long val;
5476 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005477
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08005478 ret = strict_strtoul(buf, 0, &val);
5479 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08005480 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07005481 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08005482 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07005483
5484 return strnlen(buf, count);
5485}
5486
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08005487static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5488 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07005489
Samuel Ortizd08853a2009-01-23 13:45:17 -08005490#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07005491
Zhu Yib481de92007-09-25 17:54:57 -07005492static ssize_t show_temperature(struct device *d,
5493 struct device_attribute *attr, char *buf)
5494{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005495 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005496
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005497 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005498 return -EAGAIN;
5499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005500 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07005501}
5502
5503static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
5504
Zhu Yib481de92007-09-25 17:54:57 -07005505static ssize_t show_tx_power(struct device *d,
5506 struct device_attribute *attr, char *buf)
5507{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005508 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08005509 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07005510}
5511
5512static ssize_t store_tx_power(struct device *d,
5513 struct device_attribute *attr,
5514 const char *buf, size_t count)
5515{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005516 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005517 char *p = (char *)buf;
5518 u32 val;
5519
5520 val = simple_strtoul(p, &p, 10);
5521 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08005522 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07005523 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005524 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07005525
5526 return count;
5527}
5528
5529static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
5530
5531static ssize_t show_flags(struct device *d,
5532 struct device_attribute *attr, char *buf)
5533{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005534 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005535
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005536 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07005537}
5538
5539static ssize_t store_flags(struct device *d,
5540 struct device_attribute *attr,
5541 const char *buf, size_t count)
5542{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005543 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005544 u32 flags = simple_strtoul(buf, NULL, 0);
5545
5546 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005547 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07005548 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005549 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005550 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005551 else {
5552 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
5553 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005554 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005555 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005556 }
5557 }
5558 mutex_unlock(&priv->mutex);
5559
5560 return count;
5561}
5562
5563static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
5564
5565static ssize_t show_filter_flags(struct device *d,
5566 struct device_attribute *attr, char *buf)
5567{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005568 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005569
5570 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005571 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07005572}
5573
5574static ssize_t store_filter_flags(struct device *d,
5575 struct device_attribute *attr,
5576 const char *buf, size_t count)
5577{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005578 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005579 u32 filter_flags = simple_strtoul(buf, NULL, 0);
5580
5581 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005582 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07005583 /* Cancel any currently running scans... */
Winkler, Tomasaf0053d2009-01-19 15:30:23 -08005584 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005585 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005586 else {
5587 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
5588 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005589 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07005590 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005591 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005592 }
5593 }
5594 mutex_unlock(&priv->mutex);
5595
5596 return count;
5597}
5598
5599static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
5600 store_filter_flags);
5601
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005602#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07005603
5604static ssize_t show_measurement(struct device *d,
5605 struct device_attribute *attr, char *buf)
5606{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005607 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08005608 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07005609 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005610 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07005611 unsigned long flags;
5612
5613 spin_lock_irqsave(&priv->lock, flags);
5614 if (!(priv->measurement_status & MEASUREMENT_READY)) {
5615 spin_unlock_irqrestore(&priv->lock, flags);
5616 return 0;
5617 }
5618 memcpy(&measure_report, &priv->measure_report, size);
5619 priv->measurement_status = 0;
5620 spin_unlock_irqrestore(&priv->lock, flags);
5621
5622 while (size && (PAGE_SIZE - len)) {
5623 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5624 PAGE_SIZE - len, 1);
5625 len = strlen(buf);
5626 if (PAGE_SIZE - len)
5627 buf[len++] = '\n';
5628
5629 ofs += 16;
5630 size -= min(size, 16U);
5631 }
5632
5633 return len;
5634}
5635
5636static ssize_t store_measurement(struct device *d,
5637 struct device_attribute *attr,
5638 const char *buf, size_t count)
5639{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005640 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005641 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005642 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07005643 .start_time = cpu_to_le64(priv->last_tsf),
5644 .duration = cpu_to_le16(1),
5645 };
5646 u8 type = IWL_MEASURE_BASIC;
5647 u8 buffer[32];
5648 u8 channel;
5649
5650 if (count) {
5651 char *p = buffer;
5652 strncpy(buffer, buf, min(sizeof(buffer), count));
5653 channel = simple_strtoul(p, NULL, 0);
5654 if (channel)
5655 params.channel = channel;
5656
5657 p = buffer;
5658 while (*p && *p != ' ')
5659 p++;
5660 if (*p)
5661 type = simple_strtoul(p + 1, NULL, 0);
5662 }
5663
5664 IWL_DEBUG_INFO("Invoking measurement of type %d on "
5665 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005666 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07005667
5668 return count;
5669}
5670
5671static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
5672 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005673#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07005674
Zhu Yib481de92007-09-25 17:54:57 -07005675static ssize_t store_retry_rate(struct device *d,
5676 struct device_attribute *attr,
5677 const char *buf, size_t count)
5678{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005679 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005680
5681 priv->retry_rate = simple_strtoul(buf, NULL, 0);
5682 if (priv->retry_rate <= 0)
5683 priv->retry_rate = 1;
5684
5685 return count;
5686}
5687
5688static ssize_t show_retry_rate(struct device *d,
5689 struct device_attribute *attr, char *buf)
5690{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005691 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005692 return sprintf(buf, "%d", priv->retry_rate);
5693}
5694
5695static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
5696 store_retry_rate);
5697
5698static ssize_t store_power_level(struct device *d,
5699 struct device_attribute *attr,
5700 const char *buf, size_t count)
5701{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005702 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005703 int rc;
5704 int mode;
5705
5706 mode = simple_strtoul(buf, NULL, 0);
5707 mutex_lock(&priv->mutex);
5708
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005709 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005710 rc = -EAGAIN;
5711 goto out;
5712 }
5713
Samuel Ortiz1125eff2008-12-19 10:37:14 +08005714 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
5715 (mode == IWL39_POWER_AC))
5716 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07005717 else
5718 mode |= IWL_POWER_ENABLED;
5719
5720 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005721 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07005722 if (rc) {
5723 IWL_DEBUG_MAC80211("failed setting power mode.\n");
5724 goto out;
5725 }
5726 priv->power_mode = mode;
5727 }
5728
5729 rc = count;
5730
5731 out:
5732 mutex_unlock(&priv->mutex);
5733 return rc;
5734}
5735
5736#define MAX_WX_STRING 80
5737
5738/* Values are in microsecond */
5739static const s32 timeout_duration[] = {
5740 350000,
5741 250000,
5742 75000,
5743 37000,
5744 25000,
5745};
5746static const s32 period_duration[] = {
5747 400000,
5748 700000,
5749 1000000,
5750 1000000,
5751 1000000
5752};
5753
5754static ssize_t show_power_level(struct device *d,
5755 struct device_attribute *attr, char *buf)
5756{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005757 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005758 int level = IWL_POWER_LEVEL(priv->power_mode);
5759 char *p = buf;
5760
5761 p += sprintf(p, "%d ", level);
5762 switch (level) {
5763 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08005764 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07005765 p += sprintf(p, "(AC)");
5766 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08005767 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07005768 p += sprintf(p, "(BATTERY)");
5769 break;
5770 default:
5771 p += sprintf(p,
5772 "(Timeout %dms, Period %dms)",
5773 timeout_duration[level - 1] / 1000,
5774 period_duration[level - 1] / 1000);
5775 }
5776
5777 if (!(priv->power_mode & IWL_POWER_ENABLED))
5778 p += sprintf(p, " OFF\n");
5779 else
5780 p += sprintf(p, " \n");
5781
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005782 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07005783
5784}
5785
5786static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
5787 store_power_level);
5788
5789static ssize_t show_channels(struct device *d,
5790 struct device_attribute *attr, char *buf)
5791{
Johannes Berg8318d782008-01-24 19:38:38 +01005792 /* all this shit doesn't belong into sysfs anyway */
5793 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005794}
5795
5796static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
5797
5798static ssize_t show_statistics(struct device *d,
5799 struct device_attribute *attr, char *buf)
5800{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005801 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005802 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07005803 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005804 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07005805 int rc = 0;
5806
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005807 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005808 return -EAGAIN;
5809
5810 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005811 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005812 mutex_unlock(&priv->mutex);
5813
5814 if (rc) {
5815 len = sprintf(buf,
5816 "Error sending statistics request: 0x%08X\n", rc);
5817 return len;
5818 }
5819
5820 while (size && (PAGE_SIZE - len)) {
5821 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5822 PAGE_SIZE - len, 1);
5823 len = strlen(buf);
5824 if (PAGE_SIZE - len)
5825 buf[len++] = '\n';
5826
5827 ofs += 16;
5828 size -= min(size, 16U);
5829 }
5830
5831 return len;
5832}
5833
5834static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
5835
5836static ssize_t show_antenna(struct device *d,
5837 struct device_attribute *attr, char *buf)
5838{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005839 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005840
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005841 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005842 return -EAGAIN;
5843
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08005844 return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
Zhu Yib481de92007-09-25 17:54:57 -07005845}
5846
5847static ssize_t store_antenna(struct device *d,
5848 struct device_attribute *attr,
5849 const char *buf, size_t count)
5850{
5851 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005852 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07005853
5854 if (count == 0)
5855 return 0;
5856
5857 if (sscanf(buf, "%1i", &ant) != 1) {
5858 IWL_DEBUG_INFO("not in hex or decimal form.\n");
5859 return count;
5860 }
5861
5862 if ((ant >= 0) && (ant <= 2)) {
5863 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Samuel Ortiz7e4bca52009-01-23 13:45:18 -08005864 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07005865 } else
5866 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
5867
5868
5869 return count;
5870}
5871
5872static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
5873
5874static ssize_t show_status(struct device *d,
5875 struct device_attribute *attr, char *buf)
5876{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005877 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005878 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005879 return -EAGAIN;
5880 return sprintf(buf, "0x%08x\n", (int)priv->status);
5881}
5882
5883static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5884
5885static ssize_t dump_error_log(struct device *d,
5886 struct device_attribute *attr,
5887 const char *buf, size_t count)
5888{
5889 char *p = (char *)buf;
5890
5891 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005892 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07005893
5894 return strnlen(buf, count);
5895}
5896
5897static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
5898
5899static ssize_t dump_event_log(struct device *d,
5900 struct device_attribute *attr,
5901 const char *buf, size_t count)
5902{
5903 char *p = (char *)buf;
5904
5905 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005906 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07005907
5908 return strnlen(buf, count);
5909}
5910
5911static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
5912
5913/*****************************************************************************
5914 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07005915 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07005916 *
5917 *****************************************************************************/
5918
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005919static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005920{
5921 priv->workqueue = create_workqueue(DRV_NAME);
5922
5923 init_waitqueue_head(&priv->wait_command_queue);
5924
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005925 INIT_WORK(&priv->up, iwl3945_bg_up);
5926 INIT_WORK(&priv->restart, iwl3945_bg_restart);
5927 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
Samuel Ortizc0af96a2009-01-21 18:27:54 +01005928 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005929 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005930 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
5931 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
Helmut Schaa26635162009-01-15 09:38:44 +01005932 INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08005933 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
5934 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
5935 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
5936 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07005937
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005938 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005939
5940 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005941 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07005942}
5943
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005944static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005945{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005946 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005947
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09005948 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07005949 cancel_delayed_work(&priv->scan_check);
5950 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07005951 cancel_work_sync(&priv->beacon_update);
5952}
5953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005954static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07005955 &dev_attr_antenna.attr,
5956 &dev_attr_channels.attr,
5957 &dev_attr_dump_errors.attr,
5958 &dev_attr_dump_events.attr,
5959 &dev_attr_flags.attr,
5960 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005961#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07005962 &dev_attr_measurement.attr,
5963#endif
5964 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005965 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005966 &dev_attr_statistics.attr,
5967 &dev_attr_status.attr,
5968 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07005969 &dev_attr_tx_power.attr,
Samuel Ortizd08853a2009-01-23 13:45:17 -08005970#ifdef CONFIG_IWLWIFI_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08005971 &dev_attr_debug_level.attr,
5972#endif
Zhu Yib481de92007-09-25 17:54:57 -07005973 NULL
5974};
5975
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005976static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07005977 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005978 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07005979};
5980
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005981static struct ieee80211_ops iwl3945_hw_ops = {
5982 .tx = iwl3945_mac_tx,
5983 .start = iwl3945_mac_start,
5984 .stop = iwl3945_mac_stop,
5985 .add_interface = iwl3945_mac_add_interface,
5986 .remove_interface = iwl3945_mac_remove_interface,
5987 .config = iwl3945_mac_config,
5988 .config_interface = iwl3945_mac_config_interface,
5989 .configure_filter = iwl3945_configure_filter,
5990 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005991 .get_tx_stats = iwl3945_mac_get_tx_stats,
5992 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005993 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005994 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005995 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07005996};
5997
Winkler, Tomase52119c2008-12-22 11:31:19 +08005998static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08005999{
6000 int ret;
Samuel Ortize6148912009-01-23 13:45:15 -08006001 struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006002
6003 priv->retry_rate = 1;
6004 priv->ibss_beacon = NULL;
6005
6006 spin_lock_init(&priv->lock);
Winkler, Tomas3dae0c42009-01-19 15:30:30 -08006007 spin_lock_init(&priv->power_data.lock);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006008 spin_lock_init(&priv->sta_lock);
6009 spin_lock_init(&priv->hcmd_lock);
6010
6011 INIT_LIST_HEAD(&priv->free_frames);
6012
6013 mutex_init(&priv->mutex);
6014
6015 /* Clear the driver's (not device's) station table */
6016 iwl3945_clear_stations_table(priv);
6017
6018 priv->data_retry_limit = -1;
6019 priv->ieee_channels = NULL;
6020 priv->ieee_rates = NULL;
6021 priv->band = IEEE80211_BAND_2GHZ;
6022
6023 priv->iw_mode = NL80211_IFTYPE_STATION;
6024
6025 iwl_reset_qos(priv);
6026
6027 priv->qos_data.qos_active = 0;
6028 priv->qos_data.qos_cap.val = 0;
6029
6030 priv->rates_mask = IWL_RATES_MASK;
6031 /* If power management is turned on, default to AC mode */
Samuel Ortizc7a7c8e2009-01-08 10:20:01 -08006032 priv->power_mode = IWL39_POWER_AC;
Winkler, Tomas62ea9c52009-01-19 15:30:29 -08006033 priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006034
Samuel Ortize6148912009-01-23 13:45:15 -08006035 if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
6036 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
6037 eeprom->version);
6038 ret = -EINVAL;
6039 goto err;
6040 }
6041 ret = iwl_init_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006042 if (ret) {
6043 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
6044 goto err;
6045 }
6046
Samuel Ortize6148912009-01-23 13:45:15 -08006047 /* Set up txpower settings in driver for all channels */
6048 if (iwl3945_txpower_set_from_eeprom(priv)) {
6049 ret = -EIO;
6050 goto err_free_channel_map;
6051 }
6052
Samuel Ortiz534166d2009-01-23 13:45:16 -08006053 ret = iwlcore_init_geos(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006054 if (ret) {
6055 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
6056 goto err_free_channel_map;
6057 }
Samuel Ortiz534166d2009-01-23 13:45:16 -08006058 iwl3945_init_hw_rates(priv, priv->ieee_rates);
6059
6060 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
6061 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
6062 &priv->bands[IEEE80211_BAND_2GHZ];
6063 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
6064 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
6065 &priv->bands[IEEE80211_BAND_5GHZ];
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006066
6067 return 0;
6068
6069err_free_channel_map:
Samuel Ortize6148912009-01-23 13:45:15 -08006070 iwl_free_channel_map(priv);
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006071err:
6072 return ret;
6073}
6074
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006075static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07006076{
6077 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006078 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07006079 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08006080 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Samuel Ortize6148912009-01-23 13:45:15 -08006081 struct iwl3945_eeprom *eeprom;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006082 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07006083
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006084 /***********************
6085 * 1. Allocating HW data
6086 * ********************/
6087
Zhu Yib481de92007-09-25 17:54:57 -07006088 /* mac80211 allocates memory for this device instance, including
6089 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006090 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07006091 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08006092 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07006093 err = -ENOMEM;
6094 goto out;
6095 }
Zhu Yib481de92007-09-25 17:54:57 -07006096 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006097 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08006098
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006099 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
6100 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006101 IWL_ERR(priv,
6102 "invalid queues_num, should be between %d and %d\n",
6103 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08006104 err = -EINVAL;
6105 goto out;
6106 }
6107
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006108 /*
6109 * Disabling hardware scan means that mac80211 will perform scans
6110 * "the hard way", rather than using device's scan.
6111 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006112 if (iwl3945_mod_params.disable_hw_scan) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006113 IWL_DEBUG_INFO("Disabling hw_scan\n");
6114 iwl3945_hw_ops.hw_scan = NULL;
6115 }
6116
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006117
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006118 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006119 priv->cfg = cfg;
6120 priv->pci_dev = pdev;
6121
Samuel Ortizd08853a2009-01-23 13:45:17 -08006122#ifdef CONFIG_IWLWIFI_DEBUG
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006123 priv->debug_level = iwl3945_mod_params.debug;
6124 atomic_set(&priv->restrict_refcnt, 0);
6125#endif
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006126 hw->rate_control_algorithm = "iwl-3945-rs";
6127 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
6128
Bruno Randolf566bfe52008-05-08 19:15:40 +02006129 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02006130 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02006131 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07006132
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07006133 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07006134 BIT(NL80211_IFTYPE_STATION) |
6135 BIT(NL80211_IFTYPE_ADHOC);
6136
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -08006137 hw->wiphy->custom_regulatory = true;
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08006138
Cahill, Ben M6440adb2007-11-29 11:09:55 +08006139 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07006140 hw->queues = 4;
6141
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006142 /***************************
6143 * 2. Initializing PCI bus
6144 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07006145 if (pci_enable_device(pdev)) {
6146 err = -ENODEV;
6147 goto out_ieee80211_free_hw;
6148 }
6149
6150 pci_set_master(pdev);
6151
Zhu Yib481de92007-09-25 17:54:57 -07006152 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6153 if (!err)
6154 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
6155 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08006156 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006157 goto out_pci_disable_device;
6158 }
6159
6160 pci_set_drvdata(pdev, priv);
6161 err = pci_request_regions(pdev, DRV_NAME);
6162 if (err)
6163 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08006164
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006165 /***********************
6166 * 3. Read REV Register
6167 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07006168 priv->hw_base = pci_iomap(pdev, 0, 0);
6169 if (!priv->hw_base) {
6170 err = -ENODEV;
6171 goto out_pci_release_regions;
6172 }
6173
6174 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
6175 (unsigned long long) pci_resource_len(pdev, 0));
6176 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
6177
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006178 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6179 * PCI Tx retries from interfering with C3 CPU state */
6180 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07006181
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006182 /* amp init */
6183 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006184 if (err < 0) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006185 IWL_DEBUG_INFO("Failed to init APMG\n");
6186 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006187 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006188
6189 /***********************
6190 * 4. Read EEPROM
6191 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006192
Zhu Yi5a669262008-01-14 17:46:18 -08006193 /* Read the EEPROM */
Samuel Ortize6148912009-01-23 13:45:15 -08006194 err = iwl_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006195 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006196 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006197 goto out_remove_sysfs;
6198 }
6199 /* MAC Address location in EEPROM same for 3945/4965 */
Samuel Ortize6148912009-01-23 13:45:15 -08006200 eeprom = (struct iwl3945_eeprom *)priv->eeprom;
6201 memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006202 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08006203 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6204
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006205 /***********************
6206 * 5. Setup HW Constants
6207 * ********************/
6208 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08006209 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006210 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006211 goto out_iounmap;
6212 }
6213
6214 /***********************
6215 * 6. Setup priv
6216 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006217
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006218 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08006219 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006220 IWL_ERR(priv, "initializing driver failed\n");
6221 goto out_free_geos;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08006222 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08006223
Tomas Winkler978785a2008-12-19 10:37:31 +08006224 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
6225 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006226
6227 /***********************************
6228 * 7. Initialize Module Parameters
6229 * **********************************/
6230
6231 /* Initialize module parameter values here */
6232 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006233 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006234 set_bit(STATUS_RF_KILL_SW, &priv->status);
6235 IWL_DEBUG_INFO("Radio disabled.\n");
6236 }
6237
6238
6239 /***********************
6240 * 8. Setup Services
6241 * ********************/
6242
6243 spin_lock_irqsave(&priv->lock, flags);
6244 iwl3945_disable_interrupts(priv);
6245 spin_unlock_irqrestore(&priv->lock, flags);
6246
Helmut Schaa26635162009-01-15 09:38:44 +01006247 pci_enable_msi(priv->pci_dev);
6248
6249 err = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6250 DRV_NAME, priv);
6251 if (err) {
6252 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
6253 goto out_disable_msi;
6254 }
6255
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006256 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
6257 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006258 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08006259 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006260 }
6261
6262 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
6263 iwl3945_setup_deferred_work(priv);
6264 iwl3945_setup_rx_handlers(priv);
6265
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006266 /*********************************
Helmut Schaa26635162009-01-15 09:38:44 +01006267 * 9. Setup and Register mac80211
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006268 * *******************************/
6269
Zhu Yi5a669262008-01-14 17:46:18 -08006270 err = ieee80211_register_hw(priv->hw);
6271 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006272 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006273 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07006274 }
6275
Zhu Yi5a669262008-01-14 17:46:18 -08006276 priv->hw->conf.beacon_int = 100;
6277 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006278
Samuel Ortizc0af96a2009-01-21 18:27:54 +01006279 err = iwl_rfkill_init(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08006280 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08006281 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08006282 "Ignoring error: %d\n", err);
6283
Helmut Schaa26635162009-01-15 09:38:44 +01006284 /* Start monitoring the killswitch */
6285 queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
6286 2 * HZ);
6287
Zhu Yib481de92007-09-25 17:54:57 -07006288 return 0;
6289
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08006290 out_remove_sysfs:
6291 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08006292 out_free_geos:
Samuel Ortiz534166d2009-01-23 13:45:16 -08006293 iwlcore_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006294
6295 out_release_irq:
Helmut Schaa26635162009-01-15 09:38:44 +01006296 free_irq(priv->pci_dev->irq, priv);
Zhu Yib481de92007-09-25 17:54:57 -07006297 destroy_workqueue(priv->workqueue);
6298 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08006299 iwl3945_unset_hw_params(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01006300 out_disable_msi:
6301 pci_disable_msi(priv->pci_dev);
Zhu Yib481de92007-09-25 17:54:57 -07006302 out_iounmap:
6303 pci_iounmap(pdev, priv->hw_base);
6304 out_pci_release_regions:
6305 pci_release_regions(pdev);
6306 out_pci_disable_device:
6307 pci_disable_device(pdev);
6308 pci_set_drvdata(pdev, NULL);
6309 out_ieee80211_free_hw:
6310 ieee80211_free_hw(priv->hw);
6311 out:
6312 return err;
6313}
6314
Reinette Chatrec83dbf62008-03-21 13:53:41 -07006315static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006316{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006317 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006318 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07006319
6320 if (!priv)
6321 return;
6322
6323 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
6324
Zhu Yib481de92007-09-25 17:54:57 -07006325 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08006326
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08006327 if (priv->mac80211_registered) {
6328 ieee80211_unregister_hw(priv->hw);
6329 priv->mac80211_registered = 0;
6330 } else {
6331 iwl3945_down(priv);
6332 }
Zhu Yib481de92007-09-25 17:54:57 -07006333
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006334 /* make sure we flush any pending irq or
6335 * tasklet for the driver
6336 */
6337 spin_lock_irqsave(&priv->lock, flags);
6338 iwl3945_disable_interrupts(priv);
6339 spin_unlock_irqrestore(&priv->lock, flags);
6340
6341 iwl_synchronize_irq(priv);
6342
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006343 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07006344
Samuel Ortizc0af96a2009-01-21 18:27:54 +01006345 iwl_rfkill_unregister(priv);
Helmut Schaa26635162009-01-15 09:38:44 +01006346 cancel_delayed_work(&priv->rfkill_poll);
6347
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006348 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006349
6350 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08006351 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006352 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006353
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08006354 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006355 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006356
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006357 /*netif_stop_queue(dev); */
6358 flush_workqueue(priv->workqueue);
6359
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006360 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07006361 * priv->workqueue... so we can't take down the workqueue
6362 * until now... */
6363 destroy_workqueue(priv->workqueue);
6364 priv->workqueue = NULL;
6365
Helmut Schaa26635162009-01-15 09:38:44 +01006366 free_irq(pdev->irq, priv);
6367 pci_disable_msi(pdev);
6368
Zhu Yib481de92007-09-25 17:54:57 -07006369 pci_iounmap(pdev, priv->hw_base);
6370 pci_release_regions(pdev);
6371 pci_disable_device(pdev);
6372 pci_set_drvdata(pdev, NULL);
6373
Samuel Ortize6148912009-01-23 13:45:15 -08006374 iwl_free_channel_map(priv);
Samuel Ortiz534166d2009-01-23 13:45:16 -08006375 iwlcore_free_geos(priv);
Winkler, Tomas805cee52009-01-19 15:30:28 -08006376 kfree(priv->scan);
Zhu Yib481de92007-09-25 17:54:57 -07006377 if (priv->ibss_beacon)
6378 dev_kfree_skb(priv->ibss_beacon);
6379
6380 ieee80211_free_hw(priv->hw);
6381}
6382
6383#ifdef CONFIG_PM
6384
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006385static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07006386{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006387 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006388
Zhu Yie655b9f2008-01-24 02:19:38 -08006389 if (priv->is_open) {
6390 set_bit(STATUS_IN_SUSPEND, &priv->status);
6391 iwl3945_mac_stop(priv->hw);
6392 priv->is_open = 1;
6393 }
Helmut Schaa26635162009-01-15 09:38:44 +01006394 pci_save_state(pdev);
6395 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006396 pci_set_power_state(pdev, PCI_D3hot);
6397
Zhu Yib481de92007-09-25 17:54:57 -07006398 return 0;
6399}
6400
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006401static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006402{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006403 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006404
Zhu Yib481de92007-09-25 17:54:57 -07006405 pci_set_power_state(pdev, PCI_D0);
Helmut Schaa26635162009-01-15 09:38:44 +01006406 pci_enable_device(pdev);
6407 pci_restore_state(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006408
Zhu Yie655b9f2008-01-24 02:19:38 -08006409 if (priv->is_open)
6410 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006411
Zhu Yie655b9f2008-01-24 02:19:38 -08006412 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006413 return 0;
6414}
6415
6416#endif /* CONFIG_PM */
6417
6418/*****************************************************************************
6419 *
6420 * driver and module entry point
6421 *
6422 *****************************************************************************/
6423
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006424static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07006425 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006426 .id_table = iwl3945_hw_card_ids,
6427 .probe = iwl3945_pci_probe,
6428 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07006429#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006430 .suspend = iwl3945_pci_suspend,
6431 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07006432#endif
6433};
6434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006435static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07006436{
6437
6438 int ret;
6439 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
6440 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006441
6442 ret = iwl3945_rate_control_register();
6443 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08006444 printk(KERN_ERR DRV_NAME
6445 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006446 return ret;
6447 }
6448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006449 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07006450 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08006451 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006452 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07006453 }
Zhu Yib481de92007-09-25 17:54:57 -07006454
6455 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006456
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006457error_register:
6458 iwl3945_rate_control_unregister();
6459 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006460}
6461
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006462static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07006463{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006464 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006465 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07006466}
6467
Reinette Chatrea0987a82008-12-02 12:14:06 -08006468MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08006469
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006470module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07006471MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006472module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07006473MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Samuel Ortiz9c74d9f2009-01-08 10:19:59 -08006474module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
6475MODULE_PARM_DESC(swcrypto,
6476 "using software crypto (default 1 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006477module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07006478MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006479module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07006480MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
6481
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006482module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07006483MODULE_PARM_DESC(queues_num, "number of hw queues.");
6484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006485module_exit(iwl3945_exit);
6486module_init(iwl3945_init);