blob: 11e17923ad01dd91f1a7fd923e43be12d8ad3b36 [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
Zhu Yib481de92007-09-25 17:54:57 -070043#include <net/mac80211.h>
44
45#include <asm/div64.h>
46
Samuel Ortiza3139c52008-12-19 10:37:09 +080047#define DRV_NAME "iwlagn"
48
Assaf Krauss6bc913b2008-03-11 16:17:18 -070049#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070050#include "iwl-dev.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070051#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070052#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070053#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070054#include "iwl-sta.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070055#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070056
Christoph Hellwig416e1432007-10-25 17:15:49 +080057
Zhu Yib481de92007-09-25 17:54:57 -070058/******************************************************************************
59 *
60 * module boiler plate
61 *
62 ******************************************************************************/
63
Zhu Yib481de92007-09-25 17:54:57 -070064/*
65 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070066 */
Tomas Winklerd783b062008-07-18 13:53:02 +080067#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link AGN driver for Linux"
Zhu Yib481de92007-09-25 17:54:57 -070068
Tomas Winkler0a6857e2008-03-12 16:58:49 -070069#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070070#define VD "d"
71#else
72#define VD
73#endif
74
Reinette Chatre80bc5392009-02-13 11:51:19 -080075#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070076#define VS "s"
77#else
78#define VS
79#endif
80
Tomas Winklerdf48c322008-03-06 10:40:19 -080081#define DRV_VERSION IWLWIFI_VERSION VD VS
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");
Tomas Winkler4fc22b22008-07-21 18:54:42 +030088MODULE_ALIAS("iwl4965");
Zhu Yib481de92007-09-25 17:54:57 -070089
Zhu Yib481de92007-09-25 17:54:57 -070090/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +080091 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -070092 * the functionality provided here
93 */
94
95/**************************************************************/
96
Zhu Yib481de92007-09-25 17:54:57 -070097/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -070098 * iwl_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -070099 *
Ian Schram01ebd062007-10-25 17:15:22 +0800100 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700101 * the active_rxon structure is updated with the new data. This
102 * function correctly transitions out of the RXON_ASSOC_MSK state if
103 * a HW tune is required based on the RXON structure changes.
104 */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700105int iwl_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700106{
107 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800108 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800109 int ret;
110 bool new_assoc =
111 !!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700112
Tomas Winklerfee12472008-04-03 16:05:21 -0700113 if (!iwl_is_alive(priv))
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800114 return -EBUSY;
Zhu Yib481de92007-09-25 17:54:57 -0700115
116 /* always get timestamp with Rx frame */
117 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Emmanuel Grumbacha326a5d2008-07-11 11:53:31 +0800118 /* allow CTS-to-self if possible. this is relevant only for
119 * 5000, but will not damage 4965 */
120 priv->staging_rxon.flags |= RXON_FLG_SELF_CTS_EN;
Zhu Yib481de92007-09-25 17:54:57 -0700121
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800122 ret = iwl_check_rxon_cmd(priv);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800123 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800124 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700125 return -EINVAL;
126 }
127
128 /* If we don't need to send a full RXON, we can use
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700129 * iwl_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700130 * and other flags for the current radio configuration. */
Mohamed Abbas54559702008-09-03 11:18:44 +0800131 if (!iwl_full_rxon_required(priv)) {
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800132 ret = iwl_send_rxon_assoc(priv);
133 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800134 IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800135 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700136 }
137
138 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700139 return 0;
140 }
141
142 /* station table will be cleared */
143 priv->assoc_station_added = 0;
144
Zhu Yib481de92007-09-25 17:54:57 -0700145 /* If we are currently associated and the new config requires
146 * an RXON_ASSOC and the new config wants the associated mask enabled,
147 * we must clear the associated from the active configuration
148 * before we apply the new config */
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800149 if (iwl_is_associated(priv) && new_assoc) {
Tomas Winklere1623442009-01-27 14:27:56 -0800150 IWL_DEBUG_INFO(priv, "Toggling associated bit on current RXON\n");
Zhu Yib481de92007-09-25 17:54:57 -0700151 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
152
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800153 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800154 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700155 &priv->active_rxon);
156
157 /* If the mask clearing failed then we set
158 * active_rxon back to what it was previously */
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800159 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700160 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800161 IWL_ERR(priv, "Error clearing ASSOC_MSK (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800162 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700163 }
Zhu Yib481de92007-09-25 17:54:57 -0700164 }
165
Tomas Winklere1623442009-01-27 14:27:56 -0800166 IWL_DEBUG_INFO(priv, "Sending RXON\n"
Zhu Yib481de92007-09-25 17:54:57 -0700167 "* with%s RXON_FILTER_ASSOC_MSK\n"
168 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700169 "* bssid = %pM\n",
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800170 (new_assoc ? "" : "out"),
Zhu Yib481de92007-09-25 17:54:57 -0700171 le16_to_cpu(priv->staging_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700172 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700173
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700174 iwl_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800175
176 /* Apply the new configuration
177 * RXON unassoc clears the station table in uCode, send it before
178 * we add the bcast station. If assoc bit is set, we will send RXON
179 * after having added the bcast and bssid station.
180 */
181 if (!new_assoc) {
182 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800183 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800184 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800185 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800186 return ret;
187 }
188 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700189 }
190
Abhijeet Kolekare11bc022009-04-08 11:26:41 -0700191 priv->cfg->ops->smgmt->clear_station_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800192
Zhu Yib481de92007-09-25 17:54:57 -0700193 if (!priv->error_recovering)
194 priv->start_calib = 0;
195
Zhu Yib481de92007-09-25 17:54:57 -0700196 /* Add the broadcast address so we can send broadcast frames */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800197 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800198 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800199 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700200 return -EIO;
201 }
202
203 /* If we have set the ASSOC_MSK and we are in BSS mode then
204 * add the IWL_AP_ID to the station rate table */
Tomas Winkler91851592008-06-30 17:23:14 +0800205 if (new_assoc) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200206 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Tomas Winkler91851592008-06-30 17:23:14 +0800207 ret = iwl_rxon_add_station(priv,
208 priv->active_rxon.bssid_addr, 1);
209 if (ret == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800210 IWL_ERR(priv,
211 "Error adding AP address for TX.\n");
Tomas Winkler91851592008-06-30 17:23:14 +0800212 return -EIO;
213 }
214 priv->assoc_station_added = 1;
215 if (priv->default_wep_key &&
216 iwl_send_static_wepkey_cmd(priv, 0))
Winkler, Tomas15b16872008-12-19 10:37:33 +0800217 IWL_ERR(priv,
218 "Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700219 }
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800220
221 /* Apply the new configuration
222 * RXON assoc doesn't clear the station table in uCode,
223 */
224 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
225 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
226 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800227 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800228 return ret;
229 }
230 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700231 }
232
Zhu Yi36da7d72008-07-11 11:53:40 +0800233 iwl_init_sensitivity(priv);
234
235 /* If we issue a new RXON command which required a tune then we must
236 * send a new TXPOWER command or we won't be able to Tx any frames */
237 ret = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
238 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800239 IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
Zhu Yi36da7d72008-07-11 11:53:40 +0800240 return ret;
241 }
242
Zhu Yib481de92007-09-25 17:54:57 -0700243 return 0;
244}
245
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700246void iwl_update_chain_flags(struct iwl_priv *priv)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700247{
248
Abhijeet Kolekar45823532009-04-08 11:26:44 -0700249 if (priv->cfg->ops->hcmd->set_rxon_chain)
250 priv->cfg->ops->hcmd->set_rxon_chain(priv);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700251 iwlcore_commit_rxon(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700252}
253
Tomas Winklerfcab4232008-05-15 13:54:01 +0800254static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700255{
256 struct list_head *element;
257
Tomas Winklere1623442009-01-27 14:27:56 -0800258 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700259 priv->frames_count);
260
261 while (!list_empty(&priv->free_frames)) {
262 element = priv->free_frames.next;
263 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800264 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700265 priv->frames_count--;
266 }
267
268 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800269 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700270 priv->frames_count);
271 priv->frames_count = 0;
272 }
273}
274
Tomas Winklerfcab4232008-05-15 13:54:01 +0800275static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700276{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800277 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700278 struct list_head *element;
279 if (list_empty(&priv->free_frames)) {
280 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
281 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800282 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700283 return NULL;
284 }
285
286 priv->frames_count++;
287 return frame;
288 }
289
290 element = priv->free_frames.next;
291 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800292 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700293}
294
Tomas Winklerfcab4232008-05-15 13:54:01 +0800295static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700296{
297 memset(frame, 0, sizeof(*frame));
298 list_add(&frame->list, &priv->free_frames);
299}
300
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800301static unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
302 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200303 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700304{
Tomas Winkler3109ece2008-03-28 16:33:35 -0700305 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200306 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
307 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -0700308 return 0;
309
310 if (priv->ibss_beacon->len > left)
311 return 0;
312
313 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
314
315 return priv->ibss_beacon->len;
316}
317
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700318static unsigned int iwl_hw_get_beacon_cmd(struct iwl_priv *priv,
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800319 struct iwl_frame *frame, u8 rate)
320{
321 struct iwl_tx_beacon_cmd *tx_beacon_cmd;
322 unsigned int frame_size;
323
324 tx_beacon_cmd = &frame->u.beacon;
325 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
326
327 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
328 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
329
330 frame_size = iwl_fill_beacon_frame(priv, tx_beacon_cmd->frame,
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800331 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
332
333 BUG_ON(frame_size > MAX_MPDU_SIZE);
334 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
335
336 if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
337 tx_beacon_cmd->tx.rate_n_flags =
338 iwl_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
339 else
340 tx_beacon_cmd->tx.rate_n_flags =
341 iwl_hw_set_rate_n_flags(rate, 0);
342
343 tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
344 TX_CMD_FLG_TSF_MSK |
345 TX_CMD_FLG_STA_RATE_MSK;
346
347 return sizeof(*tx_beacon_cmd) + frame_size;
348}
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700349static int iwl_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700350{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800351 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700352 unsigned int frame_size;
353 int rc;
354 u8 rate;
355
Tomas Winklerfcab4232008-05-15 13:54:01 +0800356 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700357
358 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800359 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700360 "command.\n");
361 return -ENOMEM;
362 }
363
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700364 rate = iwl_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700365
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700366 frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700367
Tomas Winkler857485c2008-03-21 13:53:44 -0700368 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700369 &frame->u.cmd[0]);
370
Tomas Winklerfcab4232008-05-15 13:54:01 +0800371 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700372
373 return rc;
374}
375
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800376static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
377{
378 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
379
380 dma_addr_t addr = get_unaligned_le32(&tb->lo);
381 if (sizeof(dma_addr_t) > sizeof(u32))
382 addr |=
383 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
384
385 return addr;
386}
387
388static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
389{
390 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
391
392 return le16_to_cpu(tb->hi_n_len) >> 4;
393}
394
395static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
396 dma_addr_t addr, u16 len)
397{
398 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
399 u16 hi_n_len = len << 4;
400
401 put_unaligned_le32(addr, &tb->lo);
402 if (sizeof(dma_addr_t) > sizeof(u32))
403 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
404
405 tb->hi_n_len = cpu_to_le16(hi_n_len);
406
407 tfd->num_tbs = idx + 1;
408}
409
410static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
411{
412 return tfd->num_tbs & 0x1f;
413}
414
415/**
416 * iwl_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
417 * @priv - driver private data
418 * @txq - tx queue
419 *
420 * Does NOT advance any TFD circular buffer read/write indexes
421 * Does NOT free the TFD itself (which is within circular buffer)
422 */
423void iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
424{
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800425 struct iwl_tfd *tfd_tmp = (struct iwl_tfd *)txq->tfds;
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800426 struct iwl_tfd *tfd;
427 struct pci_dev *dev = priv->pci_dev;
428 int index = txq->q.read_ptr;
429 int i;
430 int num_tbs;
431
432 tfd = &tfd_tmp[index];
433
434 /* Sanity check on number of chunks */
435 num_tbs = iwl_tfd_get_num_tbs(tfd);
436
437 if (num_tbs >= IWL_NUM_OF_TBS) {
438 IWL_ERR(priv, "Too many chunks: %i\n", num_tbs);
439 /* @todo issue fatal error, it is quite serious situation */
440 return;
441 }
442
443 /* Unmap tx_cmd */
444 if (num_tbs)
445 pci_unmap_single(dev,
446 pci_unmap_addr(&txq->cmd[index]->meta, mapping),
447 pci_unmap_len(&txq->cmd[index]->meta, len),
Fenghua Yu96891ce2009-02-18 15:54:33 -0800448 PCI_DMA_BIDIRECTIONAL);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800449
450 /* Unmap chunks, if any. */
451 for (i = 1; i < num_tbs; i++) {
452 pci_unmap_single(dev, iwl_tfd_tb_get_addr(tfd, i),
453 iwl_tfd_tb_get_len(tfd, i), PCI_DMA_TODEVICE);
454
455 if (txq->txb) {
456 dev_kfree_skb(txq->txb[txq->q.read_ptr].skb[i - 1]);
457 txq->txb[txq->q.read_ptr].skb[i - 1] = NULL;
458 }
459 }
460}
461
462int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv,
463 struct iwl_tx_queue *txq,
464 dma_addr_t addr, u16 len,
465 u8 reset, u8 pad)
466{
467 struct iwl_queue *q;
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800468 struct iwl_tfd *tfd, *tfd_tmp;
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800469 u32 num_tbs;
470
471 q = &txq->q;
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800472 tfd_tmp = (struct iwl_tfd *)txq->tfds;
473 tfd = &tfd_tmp[q->write_ptr];
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800474
475 if (reset)
476 memset(tfd, 0, sizeof(*tfd));
477
478 num_tbs = iwl_tfd_get_num_tbs(tfd);
479
480 /* Each TFD can point to a maximum 20 Tx buffers */
481 if (num_tbs >= IWL_NUM_OF_TBS) {
482 IWL_ERR(priv, "Error can not send more than %d chunks\n",
483 IWL_NUM_OF_TBS);
484 return -EINVAL;
485 }
486
487 BUG_ON(addr & ~DMA_BIT_MASK(36));
488 if (unlikely(addr & ~IWL_TX_DMA_MASK))
489 IWL_ERR(priv, "Unaligned address = %llx\n",
490 (unsigned long long)addr);
491
492 iwl_tfd_set_tb(tfd, num_tbs, addr, len);
493
494 return 0;
495}
496
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800497/*
498 * Tell nic where to find circular buffer of Tx Frame Descriptors for
499 * given Tx queue, and enable the DMA channel used for that queue.
500 *
501 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
502 * channels supported in hardware.
503 */
504int iwl_hw_tx_queue_init(struct iwl_priv *priv,
505 struct iwl_tx_queue *txq)
506{
507 int ret;
508 unsigned long flags;
509 int txq_id = txq->q.id;
510
511 spin_lock_irqsave(&priv->lock, flags);
512 ret = iwl_grab_nic_access(priv);
513 if (ret) {
514 spin_unlock_irqrestore(&priv->lock, flags);
515 return ret;
516 }
517
518 /* Circular buffer (TFD queue in DRAM) physical base address */
519 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
520 txq->q.dma_addr >> 8);
521
522 iwl_release_nic_access(priv);
523 spin_unlock_irqrestore(&priv->lock, flags);
524
525 return 0;
526}
527
528
Zhu Yib481de92007-09-25 17:54:57 -0700529/******************************************************************************
530 *
Zhu Yib481de92007-09-25 17:54:57 -0700531 * Misc. internal state and helper functions
532 *
533 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700534
Zhu Yib481de92007-09-25 17:54:57 -0700535#define MAX_UCODE_BEACON_INTERVAL 4096
Zhu Yib481de92007-09-25 17:54:57 -0700536
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800537static u16 iwl_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -0700538{
539 u16 new_val = 0;
540 u16 beacon_factor = 0;
541
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800542 beacon_factor = (beacon_val + MAX_UCODE_BEACON_INTERVAL)
543 / MAX_UCODE_BEACON_INTERVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700544 new_val = beacon_val / beacon_factor;
545
John W. Linville41d2f292009-02-20 19:03:31 -0500546 if (!new_val)
547 new_val = MAX_UCODE_BEACON_INTERVAL;
548
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800549 return new_val;
Zhu Yib481de92007-09-25 17:54:57 -0700550}
551
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800552static void iwl_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700553{
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800554 u64 tsf;
555 s32 interval_tm, rem;
Zhu Yib481de92007-09-25 17:54:57 -0700556 unsigned long flags;
557 struct ieee80211_conf *conf = NULL;
558 u16 beacon_int = 0;
559
560 conf = ieee80211_get_hw_conf(priv->hw);
561
562 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800563 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Tomas Winklerb5d7be52008-07-19 04:41:24 +0300564 priv->rxon_timing.listen_interval = cpu_to_le16(conf->listen_interval);
Zhu Yib481de92007-09-25 17:54:57 -0700565
Johannes Berg05c914f2008-09-11 00:01:58 +0200566 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800567 beacon_int = iwl_adjust_beacon_interval(priv->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -0700568 priv->rxon_timing.atim_window = 0;
569 } else {
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800570 beacon_int = iwl_adjust_beacon_interval(conf->beacon_int);
571
Zhu Yib481de92007-09-25 17:54:57 -0700572 /* TODO: we need to get atim_window from upper stack
573 * for now we set to 0 */
574 priv->rxon_timing.atim_window = 0;
575 }
576
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800577 priv->rxon_timing.beacon_interval = cpu_to_le16(beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -0700578
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800579 tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
580 interval_tm = beacon_int * 1024;
581 rem = do_div(tsf, interval_tm);
582 priv->rxon_timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
583
584 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winklere1623442009-01-27 14:27:56 -0800585 IWL_DEBUG_ASSOC(priv, "beacon interval %d beacon timer %d beacon tim %d\n",
Tomas Winkler3195c1f2008-10-08 09:37:30 +0800586 le16_to_cpu(priv->rxon_timing.beacon_interval),
587 le32_to_cpu(priv->rxon_timing.beacon_init_val),
588 le16_to_cpu(priv->rxon_timing.atim_window));
Zhu Yib481de92007-09-25 17:54:57 -0700589}
590
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700591static int iwl_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700592{
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700593 iwl_connection_init_rx_config(priv, mode);
Abhijeet Kolekar45823532009-04-08 11:26:44 -0700594
595 if (priv->cfg->ops->hcmd->set_rxon_chain)
596 priv->cfg->ops->hcmd->set_rxon_chain(priv);
597
Zhu Yib481de92007-09-25 17:54:57 -0700598 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
599
Abhijeet Kolekare11bc022009-04-08 11:26:41 -0700600 priv->cfg->ops->smgmt->clear_station_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700601
Mohamed Abbasfde35712007-11-29 11:10:15 +0800602 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -0700603 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +0800604 return -EAGAIN;
605
606 cancel_delayed_work(&priv->scan_check);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800607 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800608 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Tomas Winklere1623442009-01-27 14:27:56 -0800609 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +0800610 return -EAGAIN;
611 }
612
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700613 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700614
615 return 0;
616}
617
Zhu Yib481de92007-09-25 17:54:57 -0700618/******************************************************************************
619 *
620 * Generic RX handler implementations
621 *
622 ******************************************************************************/
Tomas Winkler885ba202008-05-29 16:34:55 +0800623static void iwl_rx_reply_alive(struct iwl_priv *priv,
624 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700625{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800626 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Tomas Winkler885ba202008-05-29 16:34:55 +0800627 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700628 struct delayed_work *pwork;
629
630 palive = &pkt->u.alive_frame;
631
Tomas Winklere1623442009-01-27 14:27:56 -0800632 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700633 "0x%01X 0x%01X\n",
634 palive->is_valid, palive->ver_type,
635 palive->ver_subtype);
636
637 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800638 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700639 memcpy(&priv->card_alive_init,
640 &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +0800641 sizeof(struct iwl_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700642 pwork = &priv->init_alive_start;
643 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800644 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700645 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +0800646 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700647 pwork = &priv->alive_start;
648 }
649
650 /* We delay the ALIVE response by 5ms to
651 * give the HW RF Kill time to activate... */
652 if (palive->is_valid == UCODE_VALID_OK)
653 queue_delayed_work(priv->workqueue, pwork,
654 msecs_to_jiffies(5));
655 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800656 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700657}
658
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700659static void iwl_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700660{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700661 struct iwl_priv *priv =
662 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700663 struct sk_buff *beacon;
664
665 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200666 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700667
668 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800669 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700670 return;
671 }
672
673 mutex_lock(&priv->mutex);
674 /* new beacon skb is allocated every time; dispose previous.*/
675 if (priv->ibss_beacon)
676 dev_kfree_skb(priv->ibss_beacon);
677
678 priv->ibss_beacon = beacon;
679 mutex_unlock(&priv->mutex);
680
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700681 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700682}
683
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800684/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700685 * iwl_bg_statistics_periodic - Timer callback to queue statistics
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800686 *
687 * This callback is provided in order to send a statistics request.
688 *
689 * This timer function is continually reset to execute within
690 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
691 * was received. We need to ensure we receive the statistics in order
692 * to update the temperature used for calibrating the TXPOWER.
693 */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700694static void iwl_bg_statistics_periodic(unsigned long data)
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800695{
696 struct iwl_priv *priv = (struct iwl_priv *)data;
697
698 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
699 return;
700
Mohamed Abbas61780ee2008-10-29 14:05:49 -0700701 /* dont send host command if rf-kill is on */
702 if (!iwl_is_ready_rf(priv))
703 return;
704
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800705 iwl_send_statistics_request(priv, CMD_ASYNC);
706}
707
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700708static void iwl_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +0800709 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700710{
Tomas Winkler0a6857e2008-03-12 16:58:49 -0700711#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +0800712 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Tomas Winkler2aa6ab82008-12-11 10:33:40 -0800713 struct iwl4965_beacon_notif *beacon =
714 (struct iwl4965_beacon_notif *)pkt->u.raw;
Tomas Winklere7d326a2008-06-12 09:47:11 +0800715 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700716
Tomas Winklere1623442009-01-27 14:27:56 -0800717 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700718 "tsf %d %d rate %d\n",
Tomas Winkler25a65722008-06-12 09:47:07 +0800719 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
Zhu Yib481de92007-09-25 17:54:57 -0700720 beacon->beacon_notify_hdr.failure_frame,
721 le32_to_cpu(beacon->ibss_mgr_status),
722 le32_to_cpu(beacon->high_tsf),
723 le32_to_cpu(beacon->low_tsf), rate);
724#endif
725
Johannes Berg05c914f2008-09-11 00:01:58 +0200726 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700727 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
728 queue_work(priv->workqueue, &priv->beacon_update);
729}
730
Zhu Yib481de92007-09-25 17:54:57 -0700731/* Handle notification from uCode that card's power state is changing
732 * due to software, hardware, or critical temperature RFKILL */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700733static void iwl_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +0800734 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700735{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800736 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700737 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
738 unsigned long status = priv->status;
739
Tomas Winklere1623442009-01-27 14:27:56 -0800740 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700741 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
742 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
743
744 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
745 RF_CARD_DISABLED)) {
746
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700747 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700748 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
749
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700750 if (!iwl_grab_nic_access(priv)) {
751 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -0700752 priv, HBUS_TARG_MBX_C,
753 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
754
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700755 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700756 }
757
758 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700759 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -0700760 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700761 if (!iwl_grab_nic_access(priv)) {
762 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -0700763 priv, HBUS_TARG_MBX_C,
764 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
765
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700766 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700767 }
768 }
769
770 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700771 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700772 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700773 iwl_read32(priv, CSR_UCODE_DRV_GP1);
774 if (!iwl_grab_nic_access(priv))
775 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700776 }
777 }
778
779 if (flags & HW_CARD_DISABLED)
780 set_bit(STATUS_RF_KILL_HW, &priv->status);
781 else
782 clear_bit(STATUS_RF_KILL_HW, &priv->status);
783
784
785 if (flags & SW_CARD_DISABLED)
786 set_bit(STATUS_RF_KILL_SW, &priv->status);
787 else
788 clear_bit(STATUS_RF_KILL_SW, &priv->status);
789
790 if (!(flags & RXON_CARD_DISABLED))
Tomas Winkler2a421b92008-06-12 09:47:10 +0800791 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700792
793 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
794 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
795 (test_bit(STATUS_RF_KILL_SW, &status) !=
796 test_bit(STATUS_RF_KILL_SW, &priv->status)))
797 queue_work(priv->workqueue, &priv->rf_kill);
798 else
799 wake_up_interruptible(&priv->wait_command_queue);
800}
801
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700802int iwl_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
Tomas Winklere2e3c572008-07-18 13:53:04 +0800803{
804 int ret;
805 unsigned long flags;
806
807 spin_lock_irqsave(&priv->lock, flags);
808 ret = iwl_grab_nic_access(priv);
809 if (ret)
810 goto err;
811
812 if (src == IWL_PWR_SRC_VAUX) {
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800813 if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
Tomas Winklere2e3c572008-07-18 13:53:04 +0800814 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
815 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
816 ~APMG_PS_CTRL_MSK_PWR_SRC);
817 } else {
818 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
819 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
820 ~APMG_PS_CTRL_MSK_PWR_SRC);
821 }
822
823 iwl_release_nic_access(priv);
824err:
825 spin_unlock_irqrestore(&priv->lock, flags);
826 return ret;
827}
828
Zhu Yib481de92007-09-25 17:54:57 -0700829/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700830 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700831 *
832 * Setup the RX handlers for each of the reply types sent from the uCode
833 * to the host.
834 *
835 * This function chains into the hardware specific files for them to setup
836 * any hardware specific handlers as well.
837 */
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +0800838static void iwl_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700839{
Tomas Winkler885ba202008-05-29 16:34:55 +0800840 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700841 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
842 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700843 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700844 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700845 iwl_rx_pm_debug_statistics_notif;
846 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700847
Ben Cahill9fbab512007-11-29 11:09:47 +0800848 /*
849 * The same handler is used for both the REPLY to a discrete
850 * statistics request from the host as well as for the periodic
851 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700852 */
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +0800853 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_rx_statistics;
854 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800855
Tomas Winkler21c339b2008-11-07 09:58:41 -0800856 iwl_setup_spectrum_handlers(priv);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800857 iwl_setup_rx_scan_handlers(priv);
858
Ron Rindjunsky37a44212008-05-29 16:35:18 +0800859 /* status change handler */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700860 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700861
Tomas Winklerc1354752008-05-29 16:35:04 +0800862 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
863 iwl_rx_missed_beacon_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +0800864 /* Rx handlers */
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800865 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
866 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +0800867 /* block ack */
868 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl_rx_reply_compressed_ba;
Ben Cahill9fbab512007-11-29 11:09:47 +0800869 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -0700870 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700871}
872
Zhu Yib481de92007-09-25 17:54:57 -0700873/**
Tomas Winklera55360e2008-05-05 10:22:28 +0800874 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -0700875 *
876 * Uses the priv->rx_handlers callback function array to invoke
877 * the appropriate handlers, including command responses,
878 * frame-received notifications, and other notifications.
879 */
Tomas Winklera55360e2008-05-05 10:22:28 +0800880void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700881{
Tomas Winklera55360e2008-05-05 10:22:28 +0800882 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +0800883 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +0800884 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -0700885 u32 r, i;
886 int reclaim;
887 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +0800888 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -0800889 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -0700890
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800891 /* uCode's read index (stored in shared DRAM) indicates the last Rx
892 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8d864222008-11-07 09:58:39 -0800893 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -0700894 i = rxq->read;
895
896 /* Rx interrupt, but nothing sent from uCode */
897 if (i == r)
Tomas Winklere1623442009-01-27 14:27:56 -0800898 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -0700899
Tomas Winklera55360e2008-05-05 10:22:28 +0800900 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +0800901 fill_rx = 1;
902
Zhu Yib481de92007-09-25 17:54:57 -0700903 while (i != r) {
904 rxb = rxq->queue[i];
905
Ben Cahill9fbab512007-11-29 11:09:47 +0800906 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -0700907 * then a bug has been introduced in the queue refilling
908 * routines -- catch it here */
909 BUG_ON(rxb == NULL);
910
911 rxq->queue[i] = NULL;
912
Johannes Berge91af0a2008-11-19 01:22:51 +0100913 dma_sync_single_range_for_cpu(
914 &priv->pci_dev->dev, rxb->real_dma_addr,
915 rxb->aligned_dma_addr - rxb->real_dma_addr,
916 priv->hw_params.rx_buf_size,
917 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800918 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700919
920 /* Reclaim a command buffer only if this packet is a response
921 * to a (driver-originated) command.
922 * If the packet (e.g. Rx frame) originated from uCode,
923 * there is no command buffer to reclaim.
924 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
925 * but apparently a few don't get set; catch them here. */
926 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
927 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -0700928 (pkt->hdr.cmd != REPLY_RX) &&
Daniel Halperin7dddaf12008-10-23 23:48:58 -0700929 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
Zhu Yicfe01702007-09-27 11:27:31 +0800930 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -0700931 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
932 (pkt->hdr.cmd != REPLY_TX);
933
934 /* Based on type of command response or notification,
935 * handle those that need handling via function in
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700936 * rx_handlers table. See iwl_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -0700937 if (priv->rx_handlers[pkt->hdr.cmd]) {
Tomas Winklere1623442009-01-27 14:27:56 -0800938 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r,
Ester Kummerf3d67992008-05-06 11:05:12 +0800939 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700940 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
941 } else {
942 /* No handling needed */
Tomas Winklere1623442009-01-27 14:27:56 -0800943 IWL_DEBUG_RX(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700944 "r %d i %d No handler needed for %s, 0x%02x\n",
945 r, i, get_cmd_string(pkt->hdr.cmd),
946 pkt->hdr.cmd);
947 }
948
949 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +0800950 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -0700951 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -0700952 * as we reclaim the driver command queue */
953 if (rxb && rxb->skb)
Tomas Winkler17b88922008-05-29 16:35:12 +0800954 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700955 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800956 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -0700957 }
958
959 /* For now we just don't re-use anything. We can tweak this
960 * later to try and re-use notification packets and SKBs that
961 * fail to Rx correctly */
962 if (rxb->skb != NULL) {
963 priv->alloc_rxb_skb--;
964 dev_kfree_skb_any(rxb->skb);
965 rxb->skb = NULL;
966 }
967
Johannes Berg40185172008-11-18 01:47:21 +0100968 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
969 priv->hw_params.rx_buf_size + 256,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +0200970 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -0700971 spin_lock_irqsave(&rxq->lock, flags);
972 list_add_tail(&rxb->list, &priv->rxq.rx_used);
973 spin_unlock_irqrestore(&rxq->lock, flags);
974 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +0800975 /* If there are a lot of unused frames,
976 * restock the Rx queue so ucode wont assert. */
977 if (fill_rx) {
978 count++;
979 if (count >= 8) {
980 priv->rxq.read = i;
Zhu Yif1bc4ac2008-12-17 16:52:33 +0800981 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +0800982 count = 0;
983 }
984 }
Zhu Yib481de92007-09-25 17:54:57 -0700985 }
986
987 /* Backtrack one entry */
988 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +0800989 iwl_rx_queue_restock(priv);
990}
Tomas Winklera55360e2008-05-05 10:22:28 +0800991
Mohamed Abbas0359fac2008-03-28 16:21:08 -0700992/* call this function to flush any scheduled tasklet */
993static inline void iwl_synchronize_irq(struct iwl_priv *priv)
994{
Tomas Winklera96a27f2008-10-23 23:48:56 -0700995 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -0700996 synchronize_irq(priv->pci_dev->irq);
997 tasklet_kill(&priv->irq_tasklet);
998}
999
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001000static void iwl_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001001{
1002 unsigned long flags;
1003
1004 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1005 sizeof(priv->staging_rxon));
1006 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07001007 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001008
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001009 iwl_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07001010
1011 spin_lock_irqsave(&priv->lock, flags);
1012 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
1013 priv->error_recovering = 0;
1014 spin_unlock_irqrestore(&priv->lock, flags);
1015}
1016
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001017static void iwl_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001018{
1019 u32 inta, handled = 0;
1020 u32 inta_fh;
1021 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001022#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001023 u32 inta_mask;
1024#endif
1025
1026 spin_lock_irqsave(&priv->lock, flags);
1027
1028 /* Ack/clear/reset pending uCode interrupts.
1029 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1030 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001031 inta = iwl_read32(priv, CSR_INT);
1032 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001033
1034 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1035 * Any new interrupts that happen after this, either while we're
1036 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001037 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1038 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001039
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001040#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001041 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001042 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001043 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001044 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001045 inta, inta_mask, inta_fh);
1046 }
1047#endif
1048
1049 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1050 * atomic, make sure that inta covers all the interrupts that
1051 * we've discovered, even if FH interrupt came in just after
1052 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001053 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001054 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001055 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001056 inta |= CSR_INT_BIT_FH_TX;
1057
1058 /* Now service all interrupt bits discovered above. */
1059 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001060 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001061
1062 /* Tell the device to stop sending interrupts */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001063 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001064
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001065 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001066
1067 handled |= CSR_INT_BIT_HW_ERR;
1068
1069 spin_unlock_irqrestore(&priv->lock, flags);
1070
1071 return;
1072 }
1073
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001074#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001075 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001076 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001077 if (inta & CSR_INT_BIT_SCD)
Tomas Winklere1623442009-01-27 14:27:56 -08001078 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001079 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001080
1081 /* Alive notification via Rx interrupt will do the real work */
1082 if (inta & CSR_INT_BIT_ALIVE)
Tomas Winklere1623442009-01-27 14:27:56 -08001083 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Zhu Yib481de92007-09-25 17:54:57 -07001084 }
1085#endif
1086 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001087 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001088
Ben Cahill9fbab512007-11-29 11:09:47 +08001089 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07001090 if (inta & CSR_INT_BIT_RF_KILL) {
1091 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001092 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07001093 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1094 hw_rf_kill = 1;
1095
Tomas Winklere1623442009-01-27 14:27:56 -08001096 IWL_DEBUG_RF_KILL(priv, "RF_KILL bit toggled to %s.\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08001097 hw_rf_kill ? "disable radio" : "enable radio");
Zhu Yib481de92007-09-25 17:54:57 -07001098
Emmanuel Grumbacha9efa652008-06-30 17:23:25 +08001099 /* driver only loads ucode once setting the interface up.
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01001100 * the driver allows loading the ucode even if the radio
1101 * is killed. Hence update the killswitch state here. The
1102 * rfkill handler will care about restarting if needed.
Emmanuel Grumbacha9efa652008-06-30 17:23:25 +08001103 */
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01001104 if (!test_bit(STATUS_ALIVE, &priv->status)) {
1105 if (hw_rf_kill)
1106 set_bit(STATUS_RF_KILL_HW, &priv->status);
1107 else
1108 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1109 queue_work(priv->workqueue, &priv->rf_kill);
Mohamed Abbasedb34222008-12-11 10:33:37 -08001110 }
Zhu Yib481de92007-09-25 17:54:57 -07001111
1112 handled |= CSR_INT_BIT_RF_KILL;
1113 }
1114
Ben Cahill9fbab512007-11-29 11:09:47 +08001115 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07001116 if (inta & CSR_INT_BIT_CT_KILL) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001117 IWL_ERR(priv, "Microcode CT kill error detected.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001118 handled |= CSR_INT_BIT_CT_KILL;
1119 }
1120
1121 /* Error detected by uCode */
1122 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001123 IWL_ERR(priv, "Microcode SW error detected. "
1124 " Restarting 0x%X.\n", inta);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001125 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001126 handled |= CSR_INT_BIT_SW_ERR;
1127 }
1128
1129 /* uCode wakes up after power-down sleep */
1130 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001131 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08001132 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08001133 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1134 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1135 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1136 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1137 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1138 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07001139
1140 handled |= CSR_INT_BIT_WAKEUP;
1141 }
1142
1143 /* All uCode command responses, including Tx command responses,
1144 * Rx "responses" (frame-received notification), and other
1145 * notifications from uCode come through here*/
1146 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08001147 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001148 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1149 }
1150
1151 if (inta & CSR_INT_BIT_FH_TX) {
Tomas Winklere1623442009-01-27 14:27:56 -08001152 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
Zhu Yib481de92007-09-25 17:54:57 -07001153 handled |= CSR_INT_BIT_FH_TX;
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08001154 /* FH finished to write, send event */
1155 priv->ucode_write_complete = 1;
1156 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07001157 }
1158
1159 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08001160 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07001161
1162 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001163 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07001164 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001165 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001166 }
1167
1168 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001169 /* only Re-enable if diabled by irq */
1170 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001171 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001172
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001173#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08001174 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001175 inta = iwl_read32(priv, CSR_INT);
1176 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1177 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001178 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001179 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1180 }
1181#endif
1182 spin_unlock_irqrestore(&priv->lock, flags);
1183}
1184
Zhu Yib481de92007-09-25 17:54:57 -07001185/******************************************************************************
1186 *
1187 * uCode download functions
1188 *
1189 ******************************************************************************/
1190
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001191static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001192{
Tomas Winkler98c92212008-01-14 17:46:20 -08001193 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1194 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1195 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1196 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1197 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1198 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001199}
1200
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001201static void iwl_nic_start(struct iwl_priv *priv)
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08001202{
1203 /* Remove all resets to allow NIC to operate */
1204 iwl_write32(priv, CSR_RESET, 0);
1205}
1206
1207
Zhu Yib481de92007-09-25 17:54:57 -07001208/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001209 * iwl_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07001210 *
1211 * Copy into buffers for card to fetch via bus-mastering
1212 */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001213static int iwl_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001214{
Ron Rindjunsky14b3d332008-06-13 15:44:54 +08001215 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08001216 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07001217 const struct firmware *ucode_raw;
Reinette Chatrea0987a82008-12-02 12:14:06 -08001218 const char *name_pre = priv->cfg->fw_name_pre;
1219 const unsigned int api_max = priv->cfg->ucode_api_max;
1220 const unsigned int api_min = priv->cfg->ucode_api_min;
1221 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07001222 u8 *src;
1223 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08001224 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07001225
1226 /* Ask kernel firmware_class module to get the boot firmware off disk.
1227 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08001228 for (index = api_max; index >= api_min; index--) {
1229 sprintf(buf, "%s%d%s", name_pre, index, ".ucode");
1230 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
1231 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001232 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001233 buf, ret);
1234 if (ret == -ENOENT)
1235 continue;
1236 else
1237 goto error;
1238 } else {
1239 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08001240 IWL_ERR(priv, "Loaded firmware %s, "
1241 "which is deprecated. "
1242 "Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001243 buf, api_max);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001244
Tomas Winklere1623442009-01-27 14:27:56 -08001245 IWL_DEBUG_INFO(priv, "Got firmware '%s' file (%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001246 buf, ucode_raw->size);
1247 break;
1248 }
Zhu Yib481de92007-09-25 17:54:57 -07001249 }
1250
Reinette Chatrea0987a82008-12-02 12:14:06 -08001251 if (ret < 0)
1252 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07001253
1254 /* Make sure that we got at least our header! */
1255 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001256 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08001257 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001258 goto err_release;
1259 }
1260
1261 /* Data from ucode file: header followed by uCode images */
1262 ucode = (void *)ucode_raw->data;
1263
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08001264 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08001265 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07001266 inst_size = le32_to_cpu(ucode->inst_size);
1267 data_size = le32_to_cpu(ucode->data_size);
1268 init_size = le32_to_cpu(ucode->init_size);
1269 init_data_size = le32_to_cpu(ucode->init_data_size);
1270 boot_size = le32_to_cpu(ucode->boot_size);
1271
Reinette Chatrea0987a82008-12-02 12:14:06 -08001272 /* api_ver should match the api version forming part of the
1273 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01001274 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08001275
1276 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001277 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08001278 "Driver supports v%u, firmware is v%u.\n",
1279 api_max, api_ver);
1280 priv->ucode_ver = 0;
1281 ret = -EINVAL;
1282 goto err_release;
1283 }
1284 if (api_ver != api_max)
Tomas Winkler978785a2008-12-19 10:37:31 +08001285 IWL_ERR(priv, "Firmware has old API version. Expected v%u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08001286 "got v%u. New firmware can be obtained "
1287 "from http://www.intellinuxwireless.org.\n",
1288 api_max, api_ver);
1289
Tomas Winkler978785a2008-12-19 10:37:31 +08001290 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
1291 IWL_UCODE_MAJOR(priv->ucode_ver),
1292 IWL_UCODE_MINOR(priv->ucode_ver),
1293 IWL_UCODE_API(priv->ucode_ver),
1294 IWL_UCODE_SERIAL(priv->ucode_ver));
Reinette Chatrea0987a82008-12-02 12:14:06 -08001295
Tomas Winklere1623442009-01-27 14:27:56 -08001296 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001297 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08001298 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001299 inst_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001300 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001301 data_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001302 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001303 init_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001304 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001305 init_data_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001306 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001307 boot_size);
1308
1309 /* Verify size of file vs. image size info in file's header */
1310 if (ucode_raw->size < sizeof(*ucode) +
1311 inst_size + data_size + init_size +
1312 init_data_size + boot_size) {
1313
Tomas Winklere1623442009-01-27 14:27:56 -08001314 IWL_DEBUG_INFO(priv, "uCode file size %d too small\n",
Zhu Yib481de92007-09-25 17:54:57 -07001315 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001316 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001317 goto err_release;
1318 }
1319
1320 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001321 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001322 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001323 inst_size);
1324 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001325 goto err_release;
1326 }
1327
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001328 if (data_size > priv->hw_params.max_data_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001329 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001330 data_size);
1331 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001332 goto err_release;
1333 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001334 if (init_size > priv->hw_params.max_inst_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001335 IWL_INFO(priv, "uCode init instr len %d too large to fit in\n",
1336 init_size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001337 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001338 goto err_release;
1339 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001340 if (init_data_size > priv->hw_params.max_data_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001341 IWL_INFO(priv, "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001342 init_data_size);
1343 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001344 goto err_release;
1345 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001346 if (boot_size > priv->hw_params.max_bsm_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001347 IWL_INFO(priv, "uCode boot instr len %d too large to fit in\n",
1348 boot_size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001349 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001350 goto err_release;
1351 }
1352
1353 /* Allocate ucode buffers for card's bus-master loading ... */
1354
1355 /* Runtime instructions and 2 copies of data:
1356 * 1) unmodified from disk
1357 * 2) backup cache for save/restore during power-downs */
1358 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001359 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07001360
1361 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001362 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07001363
1364 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001365 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07001366
Zhu, Yi1f304e42009-01-23 13:45:22 -08001367 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
1368 !priv->ucode_data_backup.v_addr)
1369 goto err_pci_alloc;
1370
Zhu Yib481de92007-09-25 17:54:57 -07001371 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08001372 if (init_size && init_data_size) {
1373 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001374 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07001375
Tomas Winkler90e759d2007-11-29 11:09:41 +08001376 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001377 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001378
1379 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
1380 goto err_pci_alloc;
1381 }
Zhu Yib481de92007-09-25 17:54:57 -07001382
1383 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08001384 if (boot_size) {
1385 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001386 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001387
Tomas Winkler90e759d2007-11-29 11:09:41 +08001388 if (!priv->ucode_boot.v_addr)
1389 goto err_pci_alloc;
1390 }
Zhu Yib481de92007-09-25 17:54:57 -07001391
1392 /* Copy images into buffers for card's bus-master reads ... */
1393
1394 /* Runtime instructions (first block of data in file) */
1395 src = &ucode->data[0];
1396 len = priv->ucode_code.len;
Tomas Winklere1623442009-01-27 14:27:56 -08001397 IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001398 memcpy(priv->ucode_code.v_addr, src, len);
Tomas Winklere1623442009-01-27 14:27:56 -08001399 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001400 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
1401
1402 /* Runtime data (2nd block)
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001403 * NOTE: Copy into backup buffer will be done in iwl_up() */
Zhu Yib481de92007-09-25 17:54:57 -07001404 src = &ucode->data[inst_size];
1405 len = priv->ucode_data.len;
Tomas Winklere1623442009-01-27 14:27:56 -08001406 IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001407 memcpy(priv->ucode_data.v_addr, src, len);
1408 memcpy(priv->ucode_data_backup.v_addr, src, len);
1409
1410 /* Initialization instructions (3rd block) */
1411 if (init_size) {
1412 src = &ucode->data[inst_size + data_size];
1413 len = priv->ucode_init.len;
Tomas Winklere1623442009-01-27 14:27:56 -08001414 IWL_DEBUG_INFO(priv, "Copying (but not loading) init instr len %Zd\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001415 len);
Zhu Yib481de92007-09-25 17:54:57 -07001416 memcpy(priv->ucode_init.v_addr, src, len);
1417 }
1418
1419 /* Initialization data (4th block) */
1420 if (init_data_size) {
1421 src = &ucode->data[inst_size + data_size + init_size];
1422 len = priv->ucode_init_data.len;
Tomas Winklere1623442009-01-27 14:27:56 -08001423 IWL_DEBUG_INFO(priv, "Copying (but not loading) init data len %Zd\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001424 len);
Zhu Yib481de92007-09-25 17:54:57 -07001425 memcpy(priv->ucode_init_data.v_addr, src, len);
1426 }
1427
1428 /* Bootstrap instructions (5th block) */
1429 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
1430 len = priv->ucode_boot.len;
Tomas Winklere1623442009-01-27 14:27:56 -08001431 IWL_DEBUG_INFO(priv, "Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001432 memcpy(priv->ucode_boot.v_addr, src, len);
1433
1434 /* We have our copies now, allow OS release its copies */
1435 release_firmware(ucode_raw);
1436 return 0;
1437
1438 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001439 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08001440 ret = -ENOMEM;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001441 iwl_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001442
1443 err_release:
1444 release_firmware(ucode_raw);
1445
1446 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08001447 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001448}
1449
Zhu Yib481de92007-09-25 17:54:57 -07001450/**
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001451 * iwl_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07001452 * from protocol/runtime uCode (initialization uCode's
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001453 * Alive gets handled by iwl_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07001454 */
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001455static void iwl_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001456{
Tomas Winkler57aab752008-04-14 21:16:03 -07001457 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001458
Tomas Winklere1623442009-01-27 14:27:56 -08001459 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001460
1461 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
1462 /* We had an error bringing up the hardware, so take it
1463 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08001464 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001465 goto restart;
1466 }
1467
1468 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
1469 * This is a paranoid check, because we would not have gotten the
1470 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07001471 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001472 /* Runtime instruction load was bad;
1473 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08001474 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001475 goto restart;
1476 }
1477
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07001478 priv->cfg->ops->smgmt->clear_station_table(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07001479 ret = priv->cfg->ops->lib->alive_notify(priv);
1480 if (ret) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001481 IWL_WARN(priv,
1482 "Could not complete ALIVE transition [ntf]: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07001483 goto restart;
1484 }
1485
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001486 /* After the ALIVE response, we can send host commands to the uCode */
Zhu Yib481de92007-09-25 17:54:57 -07001487 set_bit(STATUS_ALIVE, &priv->status);
1488
Tomas Winklerfee12472008-04-03 16:05:21 -07001489 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001490 return;
1491
Johannes Berg36d68252008-05-15 12:55:26 +02001492 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07001493
1494 priv->active_rate = priv->rates_mask;
1495 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1496
Tomas Winkler3109ece2008-03-28 16:33:35 -07001497 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001498 struct iwl_rxon_cmd *active_rxon =
1499 (struct iwl_rxon_cmd *)&priv->active_rxon;
Mohamed Abbas019fb972009-03-17 21:59:18 -07001500 /* apply any changes in staging */
1501 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001502 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1503 } else {
1504 /* Initialize our rx_config data */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001505 iwl_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekar45823532009-04-08 11:26:44 -07001506
1507 if (priv->cfg->ops->hcmd->set_rxon_chain)
1508 priv->cfg->ops->hcmd->set_rxon_chain(priv);
1509
Zhu Yib481de92007-09-25 17:54:57 -07001510 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1511 }
1512
Ben Cahill9fbab512007-11-29 11:09:47 +08001513 /* Configure Bluetooth device coexistence support */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001514 iwl_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001515
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001516 iwl_reset_run_time_calib(priv);
1517
Zhu Yib481de92007-09-25 17:54:57 -07001518 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07001519 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001520
1521 /* At this point, the NIC is initialized and operational */
Emmanuel Grumbach47f4a582008-06-12 09:47:13 +08001522 iwl_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08001523
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07001524 iwl_leds_register(priv);
1525
Tomas Winklere1623442009-01-27 14:27:56 -08001526 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07001527 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08001528 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07001529
1530 if (priv->error_recovering)
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001531 iwl_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001532
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08001533 iwl_power_update_mode(priv, 1);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08001534
Mohamed Abbasada17512008-11-07 09:58:34 -08001535 /* reassociate for ADHOC mode */
1536 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
1537 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
1538 priv->vif);
1539 if (beacon)
1540 iwl_mac_beacon_update(priv->hw, beacon);
1541 }
1542
1543
Assaf Kraussc46fbef2008-06-12 09:47:05 +08001544 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001545 iwl_set_mode(priv, priv->iw_mode);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08001546
Zhu Yib481de92007-09-25 17:54:57 -07001547 return;
1548
1549 restart:
1550 queue_work(priv->workqueue, &priv->restart);
1551}
1552
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08001553static void iwl_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07001554
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001555static void __iwl_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001556{
1557 unsigned long flags;
1558 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07001559
Tomas Winklere1623442009-01-27 14:27:56 -08001560 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07001561
Zhu Yib481de92007-09-25 17:54:57 -07001562 if (!exit_pending)
1563 set_bit(STATUS_EXIT_PENDING, &priv->status);
1564
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001565 iwl_leds_unregister(priv);
1566
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07001567 priv->cfg->ops->smgmt->clear_station_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001568
1569 /* Unblock any waiting calls */
1570 wake_up_interruptible_all(&priv->wait_command_queue);
1571
Zhu Yib481de92007-09-25 17:54:57 -07001572 /* Wipe out the EXIT_PENDING status bit if we are not actually
1573 * exiting the module */
1574 if (!exit_pending)
1575 clear_bit(STATUS_EXIT_PENDING, &priv->status);
1576
1577 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001578 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07001579
1580 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001581 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001582 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001583 spin_unlock_irqrestore(&priv->lock, flags);
1584 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001585
1586 if (priv->mac80211_registered)
1587 ieee80211_stop_queues(priv->hw);
1588
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001589 /* If we have not previously called iwl_init() then
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07001590 * clear all bits but the RF Kill bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07001591 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001592 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
1593 STATUS_RF_KILL_HW |
1594 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
1595 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08001596 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
1597 STATUS_GEO_CONFIGURED |
Mohamed Abbas052ec3f2008-06-30 17:23:15 +08001598 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
1599 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07001600 goto exit;
1601 }
1602
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07001603 /* ...otherwise clear out all the status bits but the RF Kill
1604 * bits and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07001605 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
1606 STATUS_RF_KILL_HW |
1607 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
1608 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08001609 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
1610 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07001611 test_bit(STATUS_FW_ERROR, &priv->status) <<
Mohamed Abbas052ec3f2008-06-30 17:23:15 +08001612 STATUS_FW_ERROR |
1613 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
1614 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07001615
1616 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001617 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08001618 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07001619 spin_unlock_irqrestore(&priv->lock, flags);
1620
Tomas Winklerda1bc452008-05-29 16:35:00 +08001621 iwl_txq_ctx_stop(priv);
Tomas Winklerb3bbacb2008-05-29 16:35:01 +08001622 iwl_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001623
1624 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001625 if (!iwl_grab_nic_access(priv)) {
1626 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07001627 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001628 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001629 }
1630 spin_unlock_irqrestore(&priv->lock, flags);
1631
1632 udelay(5);
1633
Tomas Winkler7f066102008-05-29 16:34:57 +08001634 /* FIXME: apm_ops.suspend(priv) */
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07001635 if (exit_pending)
Gregory Greenmand5353112008-09-03 11:18:49 +08001636 priv->cfg->ops->lib->apm_ops.stop(priv);
1637 else
1638 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001639 exit:
Tomas Winkler885ba202008-05-29 16:34:55 +08001640 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07001641
1642 if (priv->ibss_beacon)
1643 dev_kfree_skb(priv->ibss_beacon);
1644 priv->ibss_beacon = NULL;
1645
1646 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08001647 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001648}
1649
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001650static void iwl_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001651{
1652 mutex_lock(&priv->mutex);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001653 __iwl_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001654 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08001655
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08001656 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001657}
1658
1659#define MAX_HW_RESTARTS 5
1660
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001661static int __iwl_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001662{
Tomas Winkler57aab752008-04-14 21:16:03 -07001663 int i;
1664 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001665
1666 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001667 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07001668 return -EIO;
1669 }
1670
Reinette Chatree903fbd2008-01-30 22:05:15 -08001671 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001672 IWL_ERR(priv, "ucode not available for device bringup\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08001673 return -EIO;
1674 }
1675
Zhu Yie655b9f2008-01-24 02:19:38 -08001676 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winklerc1842d62008-08-04 16:00:43 +08001677 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Zhu Yie655b9f2008-01-24 02:19:38 -08001678 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Emmanuel Grumbach3bff19c2008-06-30 17:23:19 +08001679 else
Zhu Yie655b9f2008-01-24 02:19:38 -08001680 set_bit(STATUS_RF_KILL_HW, &priv->status);
Emmanuel Grumbach3bff19c2008-06-30 17:23:19 +08001681
Tomas Winklerc1842d62008-08-04 16:00:43 +08001682 if (iwl_is_rfkill(priv)) {
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001683 iwl_enable_interrupts(priv);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001684 IWL_WARN(priv, "Radio disabled by %s RF Kill switch\n",
Emmanuel Grumbach3bff19c2008-06-30 17:23:19 +08001685 test_bit(STATUS_RF_KILL_HW, &priv->status) ? "HW" : "SW");
Tomas Winklerc1842d62008-08-04 16:00:43 +08001686 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001687 }
1688
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001689 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001690
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001691 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07001692 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001693 IWL_ERR(priv, "Unable to init nic\n");
Tomas Winkler57aab752008-04-14 21:16:03 -07001694 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001695 }
1696
1697 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001698 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1699 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07001700 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1701
1702 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001703 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001704 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001705
1706 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001707 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1708 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07001709
1710 /* Copy original ucode data image from disk into backup cache.
1711 * This will be used to initialize the on-board processor's
1712 * data SRAM for a clean start when the runtime program first loads. */
1713 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08001714 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07001715
Zhu Yib481de92007-09-25 17:54:57 -07001716 for (i = 0; i < MAX_HW_RESTARTS; i++) {
1717
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07001718 priv->cfg->ops->smgmt->clear_station_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001719
1720 /* load bootstrap state machine,
1721 * load bootstrap program into processor's memory,
1722 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07001723 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001724
Tomas Winkler57aab752008-04-14 21:16:03 -07001725 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001726 IWL_ERR(priv, "Unable to set up bootstrap uCode: %d\n",
1727 ret);
Zhu Yib481de92007-09-25 17:54:57 -07001728 continue;
1729 }
1730
Emmanuel Grumbachf3d5b452008-06-12 09:47:04 +08001731 /* Clear out the uCode error bit if it is set */
1732 clear_bit(STATUS_FW_ERROR, &priv->status);
1733
Zhu Yib481de92007-09-25 17:54:57 -07001734 /* start card; "initialize" will load runtime ucode */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001735 iwl_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001736
Tomas Winklere1623442009-01-27 14:27:56 -08001737 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07001738
1739 return 0;
1740 }
1741
1742 set_bit(STATUS_EXIT_PENDING, &priv->status);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001743 __iwl_down(priv);
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +08001744 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07001745
1746 /* tried to restart and config the device for as long as our
1747 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08001748 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07001749 return -EIO;
1750}
1751
1752
1753/*****************************************************************************
1754 *
1755 * Workqueue callbacks
1756 *
1757 *****************************************************************************/
1758
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001759static void iwl_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07001760{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001761 struct iwl_priv *priv =
1762 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07001763
1764 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1765 return;
1766
1767 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08001768 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001769 mutex_unlock(&priv->mutex);
1770}
1771
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001772static void iwl_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07001773{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001774 struct iwl_priv *priv =
1775 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07001776
1777 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1778 return;
1779
1780 mutex_lock(&priv->mutex);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08001781 iwl_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001782 mutex_unlock(&priv->mutex);
1783}
1784
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08001785static void iwl_bg_run_time_calib_work(struct work_struct *work)
1786{
1787 struct iwl_priv *priv = container_of(work, struct iwl_priv,
1788 run_time_calib_work);
1789
1790 mutex_lock(&priv->mutex);
1791
1792 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1793 test_bit(STATUS_SCANNING, &priv->status)) {
1794 mutex_unlock(&priv->mutex);
1795 return;
1796 }
1797
1798 if (priv->start_calib) {
1799 iwl_chain_noise_calibration(priv, &priv->statistics);
1800
1801 iwl_sensitivity_calibration(priv, &priv->statistics);
1802 }
1803
1804 mutex_unlock(&priv->mutex);
1805 return;
1806}
1807
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001808static void iwl_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07001809{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001810 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07001811
1812 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1813 return;
1814
1815 mutex_lock(&priv->mutex);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001816 __iwl_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001817 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02001818 iwl_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001819}
1820
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001821static void iwl_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07001822{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001823 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07001824
1825 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1826 return;
1827
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001828 iwl_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001829 queue_work(priv->workqueue, &priv->up);
1830}
1831
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001832static void iwl_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07001833{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001834 struct iwl_priv *priv =
1835 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07001836
1837 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1838 return;
1839
1840 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08001841 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001842 mutex_unlock(&priv->mutex);
1843}
1844
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001845#define IWL_DELAY_NEXT_SCAN (HZ*2)
1846
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07001847void iwl_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001848{
Zhu Yib481de92007-09-25 17:54:57 -07001849 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07001850 int ret = 0;
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08001851 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07001852
Johannes Berg05c914f2008-09-11 00:01:58 +02001853 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001854 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07001855 return;
1856 }
1857
Tomas Winklere1623442009-01-27 14:27:56 -08001858 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07001859 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07001860
1861
1862 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1863 return;
1864
Zhu Yib481de92007-09-25 17:54:57 -07001865
Reinette Chatre508e32e2008-04-14 21:16:13 -07001866 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08001867 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07001868
Emmanuel Grumbachc90a74ba2008-09-03 11:26:50 +08001869 iwl_power_cancel_timeout(priv);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001870 iwl_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08001871
Zhu Yib481de92007-09-25 17:54:57 -07001872 conf = ieee80211_get_hw_conf(priv->hw);
1873
1874 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07001875 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001876
Tomas Winkler3195c1f2008-10-08 09:37:30 +08001877 iwl_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07001878 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07001879 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07001880 if (ret)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001881 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07001882 "Attempting to continue.\n");
1883
1884 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
1885
Emmanuel Grumbach42eb7c62008-09-17 10:10:05 +08001886 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +03001887
Abhijeet Kolekar45823532009-04-08 11:26:44 -07001888 if (priv->cfg->ops->hcmd->set_rxon_chain)
1889 priv->cfg->ops->hcmd->set_rxon_chain(priv);
1890
Zhu Yib481de92007-09-25 17:54:57 -07001891 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
1892
Tomas Winklere1623442009-01-27 14:27:56 -08001893 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07001894 priv->assoc_id, priv->beacon_int);
1895
1896 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1897 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1898 else
1899 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1900
1901 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
1902 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1903 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1904 else
1905 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1906
Johannes Berg05c914f2008-09-11 00:01:58 +02001907 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07001908 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1909
1910 }
1911
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07001912 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001913
1914 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001915 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07001916 break;
1917
Johannes Berg05c914f2008-09-11 00:01:58 +02001918 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07001919
Assaf Kraussc46fbef2008-06-12 09:47:05 +08001920 /* assume default assoc id */
1921 priv->assoc_id = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001922
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001923 iwl_rxon_add_station(priv, priv->bssid, 0);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001924 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001925
1926 break;
1927
1928 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001929 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03001930 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001931 break;
1932 }
1933
Johannes Berg05c914f2008-09-11 00:01:58 +02001934 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07001935 priv->assoc_station_added = 1;
1936
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08001937 spin_lock_irqsave(&priv->lock, flags);
1938 iwl_activate_qos(priv, 0);
1939 spin_unlock_irqrestore(&priv->lock, flags);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08001940
Grumbach, Emmanuel04816442008-09-03 11:26:53 +08001941 /* the chain noise calibration will enabled PM upon completion
1942 * If chain noise has already been run, then we need to enable
1943 * power management here */
1944 if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
1945 iwl_power_enable_management(priv);
Emmanuel Grumbachc90a74ba2008-09-03 11:26:50 +08001946
1947 /* Enable Rx differential gain and sensitivity calibrations */
1948 iwl_chain_noise_reset(priv);
1949 priv->start_calib = 1;
1950
Reinette Chatre508e32e2008-04-14 21:16:13 -07001951}
1952
Zhu Yib481de92007-09-25 17:54:57 -07001953/*****************************************************************************
1954 *
1955 * mac80211 entry point functions
1956 *
1957 *****************************************************************************/
1958
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08001959#define UCODE_READY_TIMEOUT (4 * HZ)
Zhu Yi5a66926a2008-01-14 17:46:18 -08001960
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001961static int iwl_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07001962{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001963 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08001964 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001965
Tomas Winklere1623442009-01-27 14:27:56 -08001966 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07001967
1968 /* we should be verifying the device is ready to be opened */
1969 mutex_lock(&priv->mutex);
1970
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08001971 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08001972 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
1973 * ucode filename and max sizes are card-specific. */
1974
1975 if (!priv->ucode_code.len) {
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001976 ret = iwl_read_ucode(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08001977 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001978 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a66926a2008-01-14 17:46:18 -08001979 mutex_unlock(&priv->mutex);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01001980 return ret;
Zhu Yi5a66926a2008-01-14 17:46:18 -08001981 }
1982 }
1983
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001984 ret = __iwl_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08001985
Zhu Yib481de92007-09-25 17:54:57 -07001986 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08001987
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02001988 iwl_rfkill_set_hw_state(priv);
1989
Zhu Yie655b9f2008-01-24 02:19:38 -08001990 if (ret)
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01001991 return ret;
Zhu Yie655b9f2008-01-24 02:19:38 -08001992
Tomas Winklerc1842d62008-08-04 16:00:43 +08001993 if (iwl_is_rfkill(priv))
1994 goto out;
1995
Tomas Winklere1623442009-01-27 14:27:56 -08001996 IWL_DEBUG_INFO(priv, "Start UP work done.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08001997
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08001998 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
Zhu Yi5a66926a2008-01-14 17:46:18 -08001999 * mac80211 will not be run successfully. */
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08002000 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2001 test_bit(STATUS_READY, &priv->status),
2002 UCODE_READY_TIMEOUT);
2003 if (!ret) {
2004 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002005 IWL_ERR(priv, "START_ALIVE timeout after %dms.\n",
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08002006 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002007 return -ETIMEDOUT;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002008 }
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002009 }
Tomas Winkler0a078ff2008-06-30 17:23:26 +08002010
Tomas Winklerc1842d62008-08-04 16:00:43 +08002011out:
Tomas Winkler0a078ff2008-06-30 17:23:26 +08002012 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08002013 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002014 return 0;
2015}
2016
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002017static void iwl_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002018{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002019 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002020
Tomas Winklere1623442009-01-27 14:27:56 -08002021 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08002022
Zhu Yie655b9f2008-01-24 02:19:38 -08002023 if (!priv->is_open) {
Tomas Winklere1623442009-01-27 14:27:56 -08002024 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08002025 return;
2026 }
2027
Zhu Yib481de92007-09-25 17:54:57 -07002028 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002029
Tomas Winklerfee12472008-04-03 16:05:21 -07002030 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08002031 /* stop mac, cancel any scan request and clear
2032 * RXON_FILTER_ASSOC_MSK BIT
2033 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08002034 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002035 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002036 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002037 }
2038
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002039 iwl_down(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002040
2041 flush_workqueue(priv->workqueue);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002042
2043 /* enable interrupts again in order to receive rfkill changes */
2044 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2045 iwl_enable_interrupts(priv);
Mohamed Abbas948c1712007-10-25 17:15:45 +08002046
Tomas Winklere1623442009-01-27 14:27:56 -08002047 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002048}
2049
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002050static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002051{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002052 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002053
Tomas Winklere1623442009-01-27 14:27:56 -08002054 IWL_DEBUG_MACDUMP(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002055
Tomas Winklere1623442009-01-27 14:27:56 -08002056 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02002057 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07002058
Johannes Berge039fa42008-05-15 12:55:29 +02002059 if (iwl_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07002060 dev_kfree_skb_any(skb);
2061
Tomas Winklere1623442009-01-27 14:27:56 -08002062 IWL_DEBUG_MACDUMP(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08002063 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07002064}
2065
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002066static int iwl_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002067 struct ieee80211_if_init_conf *conf)
2068{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002069 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002070 unsigned long flags;
2071
Tomas Winklere1623442009-01-27 14:27:56 -08002072 IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07002073
Johannes Berg32bfd352007-12-19 01:31:26 +01002074 if (priv->vif) {
Tomas Winklere1623442009-01-27 14:27:56 -08002075 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08002076 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07002077 }
2078
2079 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01002080 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07002081 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07002082
2083 spin_unlock_irqrestore(&priv->lock, flags);
2084
2085 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02002086
2087 if (conf->mac_addr) {
Tomas Winklere1623442009-01-27 14:27:56 -08002088 IWL_DEBUG_MAC80211(priv, "Set %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02002089 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
2090 }
Zhu Yib481de92007-09-25 17:54:57 -07002091
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002092 if (iwl_set_mode(priv, conf->type) == -EAGAIN)
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002093 /* we are not ready, will run again when ready */
2094 set_bit(STATUS_MODE_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002095
Zhu Yib481de92007-09-25 17:54:57 -07002096 mutex_unlock(&priv->mutex);
2097
Tomas Winklere1623442009-01-27 14:27:56 -08002098 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002099 return 0;
2100}
2101
2102/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002103 * iwl_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07002104 *
2105 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
2106 * be set inappropriately and the driver currently sets the hardware up to
2107 * use it whenever needed.
2108 */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002109static int iwl_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07002110{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002111 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002112 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02002113 struct ieee80211_conf *conf = &hw->conf;
Mohamed Abbas019fb972009-03-17 21:59:18 -07002114 unsigned long flags = 0;
Zhu Yi76bb77e2007-11-22 10:53:22 +08002115 int ret = 0;
Mohamed Abbas019fb972009-03-17 21:59:18 -07002116 u16 ch;
2117 int scan_active = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002118
2119 mutex_lock(&priv->mutex);
Mohamed Abbas019fb972009-03-17 21:59:18 -07002120 IWL_DEBUG_MAC80211(priv, "enter to channel %d changed 0x%X\n",
2121 conf->channel->hw_value, changed);
Zhu Yib481de92007-09-25 17:54:57 -07002122
Assaf Krauss1ea87392008-03-18 14:57:50 -07002123 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Mohamed Abbas019fb972009-03-17 21:59:18 -07002124 test_bit(STATUS_SCANNING, &priv->status))) {
2125 scan_active = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08002126 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
Zhu Yib481de92007-09-25 17:54:57 -07002127 }
2128
Mohamed Abbas019fb972009-03-17 21:59:18 -07002129
2130 /* during scanning mac80211 will delay channel setting until
2131 * scan finish with changed = 0
2132 */
2133 if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
2134 if (scan_active)
2135 goto set_ch_out;
2136
2137 ch = ieee80211_frequency_to_channel(conf->channel->center_freq);
2138 ch_info = iwl_get_channel_info(priv, conf->channel->band, ch);
2139 if (!is_channel_valid(ch_info)) {
2140 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
2141 ret = -EINVAL;
2142 goto set_ch_out;
2143 }
2144
2145 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
2146 !is_channel_ibss(ch_info)) {
2147 IWL_ERR(priv, "channel %d in band %d not "
2148 "IBSS channel\n",
2149 conf->channel->hw_value, conf->channel->band);
2150 ret = -EINVAL;
2151 goto set_ch_out;
2152 }
2153
2154 priv->current_ht_config.is_ht = conf_is_ht(conf);
2155
2156 spin_lock_irqsave(&priv->lock, flags);
2157
2158
2159 /* if we are switching from ht to 2.4 clear flags
2160 * from any ht related info since 2.4 does not
2161 * support ht */
2162 if ((le16_to_cpu(priv->staging_rxon.channel) != ch))
2163 priv->staging_rxon.flags = 0;
2164
2165 iwl_set_rxon_channel(priv, conf->channel);
2166
2167 iwl_set_flags_for_band(priv, conf->channel->band);
2168 spin_unlock_irqrestore(&priv->lock, flags);
2169 set_ch_out:
2170 /* The list of supported rates and rate mask can be different
2171 * for each band; since the band may have changed, reset
2172 * the rate mask to what mac80211 lists */
2173 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002174 }
2175
Mohamed Abbas019fb972009-03-17 21:59:18 -07002176 if (changed & IEEE80211_CONF_CHANGE_PS) {
2177 if (conf->flags & IEEE80211_CONF_PS)
2178 ret = iwl_power_set_user_mode(priv, IWL_POWER_INDEX_3);
2179 else
2180 ret = iwl_power_set_user_mode(priv, IWL_POWER_MODE_CAM);
2181 if (ret)
2182 IWL_DEBUG_MAC80211(priv, "Error setting power level\n");
2183
Assaf Krauss398f9e72008-06-12 09:47:06 +08002184 }
2185
Mohamed Abbas019fb972009-03-17 21:59:18 -07002186 if (changed & IEEE80211_CONF_CHANGE_POWER) {
2187 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
2188 priv->tx_power_user_lmt, conf->power_level);
Tomas Winkler82a66bb2008-05-29 16:35:28 +08002189
Mohamed Abbas019fb972009-03-17 21:59:18 -07002190 iwl_set_tx_power(priv, conf->power_level, false);
Zhu Yib481de92007-09-25 17:54:57 -07002191 }
Mohamed Abbas019fb972009-03-17 21:59:18 -07002192
2193 /* call to ensure that 4965 rx_chain is set properly in monitor mode */
Abhijeet Kolekar45823532009-04-08 11:26:44 -07002194 if (priv->cfg->ops->hcmd->set_rxon_chain)
2195 priv->cfg->ops->hcmd->set_rxon_chain(priv);
Mohamed Abbas019fb972009-03-17 21:59:18 -07002196
2197 if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
2198 if (conf->radio_enabled &&
2199 iwl_radio_kill_sw_enable_radio(priv)) {
2200 IWL_DEBUG_MAC80211(priv, "leave - RF-KILL - "
2201 "waiting for uCode\n");
2202 goto out;
2203 }
2204
2205 if (!conf->radio_enabled)
2206 iwl_radio_kill_sw_disable_radio(priv);
2207 }
Zhu Yib481de92007-09-25 17:54:57 -07002208
Zhu Yib481de92007-09-25 17:54:57 -07002209 if (!conf->radio_enabled) {
Tomas Winklere1623442009-01-27 14:27:56 -08002210 IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002211 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002212 }
2213
Mohamed Abbas019fb972009-03-17 21:59:18 -07002214 if (!iwl_is_ready(priv)) {
2215 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08002216 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002217 }
2218
Mohamed Abbas019fb972009-03-17 21:59:18 -07002219 if (scan_active)
2220 goto out;
Rick Farrington7b841722009-01-23 13:45:10 -08002221
Zhu Yib481de92007-09-25 17:54:57 -07002222 if (memcmp(&priv->active_rxon,
2223 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002224 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002225 else
Tomas Winklere1623442009-01-27 14:27:56 -08002226 IWL_DEBUG_INFO(priv, "No re-sending same RXON configuration.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002227
Zhu Yib481de92007-09-25 17:54:57 -07002228
Zhu Yia0646472007-12-20 14:10:01 +08002229out:
Mohamed Abbas019fb972009-03-17 21:59:18 -07002230 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08002231 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08002232 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002233}
2234
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002235static void iwl_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002236{
Tomas Winkler857485c2008-03-21 13:53:44 -07002237 int ret = 0;
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002238 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07002239
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08002240 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07002241 return;
2242
2243 /* The following should be done only at AP bring up */
Tomas Winkler3195c1f2008-10-08 09:37:30 +08002244 if (!iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002245
2246 /* RXON - unassoc (to set timing command) */
2247 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002248 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002249
2250 /* RXON Timing */
Tomas Winkler3195c1f2008-10-08 09:37:30 +08002251 iwl_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07002252 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07002253 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07002254 if (ret)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002255 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07002256 "Attempting to continue.\n");
2257
Abhijeet Kolekar45823532009-04-08 11:26:44 -07002258 if (priv->cfg->ops->hcmd->set_rxon_chain)
2259 priv->cfg->ops->hcmd->set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002260
2261 /* FIXME: what should be the assoc_id for AP? */
2262 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2263 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2264 priv->staging_rxon.flags |=
2265 RXON_FLG_SHORT_PREAMBLE_MSK;
2266 else
2267 priv->staging_rxon.flags &=
2268 ~RXON_FLG_SHORT_PREAMBLE_MSK;
2269
2270 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2271 if (priv->assoc_capability &
2272 WLAN_CAPABILITY_SHORT_SLOT_TIME)
2273 priv->staging_rxon.flags |=
2274 RXON_FLG_SHORT_SLOT_MSK;
2275 else
2276 priv->staging_rxon.flags &=
2277 ~RXON_FLG_SHORT_SLOT_MSK;
2278
Johannes Berg05c914f2008-09-11 00:01:58 +02002279 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002280 priv->staging_rxon.flags &=
2281 ~RXON_FLG_SHORT_SLOT_MSK;
2282 }
2283 /* restore RXON assoc */
2284 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002285 iwlcore_commit_rxon(priv);
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002286 spin_lock_irqsave(&priv->lock, flags);
2287 iwl_activate_qos(priv, 1);
2288 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002289 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08002290 }
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002291 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002292
2293 /* FIXME - we need to add code here to detect a totally new
2294 * configuration, reset the AP, unassoc, rxon timing, assoc,
2295 * clear sta table, add BCAST sta... */
2296}
2297
Johannes Berg9d139c82008-07-09 14:40:37 +02002298
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002299static int iwl_mac_config_interface(struct ieee80211_hw *hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01002300 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07002301 struct ieee80211_if_conf *conf)
2302{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002303 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002304 int rc;
2305
2306 if (conf == NULL)
2307 return -EIO;
2308
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08002309 if (priv->vif != vif) {
Tomas Winklere1623442009-01-27 14:27:56 -08002310 IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08002311 return 0;
2312 }
2313
Johannes Berg05c914f2008-09-11 00:01:58 +02002314 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02002315 conf->changed & IEEE80211_IFCC_BEACON) {
2316 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
2317 if (!beacon)
2318 return -ENOMEM;
Mohamed Abbasada17512008-11-07 09:58:34 -08002319 mutex_lock(&priv->mutex);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002320 rc = iwl_mac_beacon_update(hw, beacon);
Mohamed Abbasada17512008-11-07 09:58:34 -08002321 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02002322 if (rc)
2323 return rc;
2324 }
2325
Tomas Winklerfee12472008-04-03 16:05:21 -07002326 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08002327 return -EAGAIN;
2328
Zhu Yib481de92007-09-25 17:54:57 -07002329 mutex_lock(&priv->mutex);
2330
Zhu Yib481de92007-09-25 17:54:57 -07002331 if (conf->bssid)
Tomas Winklere1623442009-01-27 14:27:56 -08002332 IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07002333
Johannes Berg4150c572007-09-17 01:29:23 -04002334/*
2335 * very dubious code was here; the probe filtering flag is never set:
2336 *
Zhu Yib481de92007-09-25 17:54:57 -07002337 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
2338 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04002339 */
Zhu Yib481de92007-09-25 17:54:57 -07002340
Johannes Berg05c914f2008-09-11 00:01:58 +02002341 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002342 if (!conf->bssid) {
2343 conf->bssid = priv->mac_addr;
2344 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Tomas Winklere1623442009-01-27 14:27:56 -08002345 IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002346 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07002347 }
2348 if (priv->ibss_beacon)
2349 dev_kfree_skb(priv->ibss_beacon);
2350
Johannes Berg9d139c82008-07-09 14:40:37 +02002351 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07002352 }
2353
Tomas Winklerfee12472008-04-03 16:05:21 -07002354 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08002355 goto done;
2356
Zhu Yib481de92007-09-25 17:54:57 -07002357 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
2358 !is_multicast_ether_addr(conf->bssid)) {
2359 /* If there is currently a HW scan going on in the background
2360 * then we need to cancel it else the RXON below will fail. */
Tomas Winkler2a421b92008-06-12 09:47:10 +08002361 if (iwl_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002362 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07002363 "after 100ms\n");
Tomas Winklere1623442009-01-27 14:27:56 -08002364 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002365 mutex_unlock(&priv->mutex);
2366 return -EAGAIN;
2367 }
2368 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
2369
2370 /* TODO: Audit driver for usage of these members and see
2371 * if mac80211 deprecates them (priv->bssid looks like it
2372 * shouldn't be there, but I haven't scanned the IBSS code
2373 * to verify) - jpk */
2374 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
2375
Johannes Berg05c914f2008-09-11 00:01:58 +02002376 if (priv->iw_mode == NL80211_IFTYPE_AP)
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002377 iwl_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002378 else {
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002379 rc = iwlcore_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02002380 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002381 iwl_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07002382 priv, priv->active_rxon.bssid_addr, 1);
2383 }
2384
2385 } else {
Tomas Winkler2a421b92008-06-12 09:47:10 +08002386 iwl_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07002387 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002388 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002389 }
2390
Mohamed Abbasfde35712007-11-29 11:10:15 +08002391 done:
Tomas Winklere1623442009-01-27 14:27:56 -08002392 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002393 mutex_unlock(&priv->mutex);
2394
2395 return 0;
2396}
2397
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002398static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002399 struct ieee80211_if_init_conf *conf)
2400{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002401 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002402
Tomas Winklere1623442009-01-27 14:27:56 -08002403 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002404
2405 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08002406
Tomas Winklerfee12472008-04-03 16:05:21 -07002407 if (iwl_is_ready_rf(priv)) {
Tomas Winkler2a421b92008-06-12 09:47:10 +08002408 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002409 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002410 iwlcore_commit_rxon(priv);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002411 }
Johannes Berg32bfd352007-12-19 01:31:26 +01002412 if (priv->vif == conf->vif) {
2413 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002414 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07002415 }
2416 mutex_unlock(&priv->mutex);
2417
Tomas Winklere1623442009-01-27 14:27:56 -08002418 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002419
2420}
Johannes Berg471b3ef2007-12-28 14:32:58 +01002421
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002422static void iwl_mac_update_tkip_key(struct ieee80211_hw *hw,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002423 struct ieee80211_key_conf *keyconf, const u8 *addr,
2424 u32 iv32, u16 *phase1key)
2425{
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002426
Tomas Winkler9f586712008-11-12 13:14:05 -08002427 struct iwl_priv *priv = hw->priv;
Tomas Winklere1623442009-01-27 14:27:56 -08002428 IWL_DEBUG_MAC80211(priv, "enter\n");
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002429
Tomas Winkler9f586712008-11-12 13:14:05 -08002430 iwl_update_tkip_key(priv, keyconf, addr, iv32, phase1key);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002431
Tomas Winklere1623442009-01-27 14:27:56 -08002432 IWL_DEBUG_MAC80211(priv, "leave\n");
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002433}
2434
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002435static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01002436 struct ieee80211_vif *vif,
2437 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07002438 struct ieee80211_key_conf *key)
2439{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002440 struct iwl_priv *priv = hw->priv;
Winkler, Tomas42986792009-01-19 15:30:22 -08002441 const u8 *addr;
2442 int ret;
2443 u8 sta_id;
2444 bool is_default_wep_key = false;
Zhu Yib481de92007-09-25 17:54:57 -07002445
Tomas Winklere1623442009-01-27 14:27:56 -08002446 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002447
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07002448 if (priv->hw_params.sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08002449 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07002450 return -EOPNOTSUPP;
2451 }
Winkler, Tomas42986792009-01-19 15:30:22 -08002452 addr = sta ? sta->addr : iwl_bcast_addr;
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07002453 sta_id = priv->cfg->ops->smgmt->find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002454 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002455 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Johannes Berge1749612008-10-27 15:59:26 -07002456 addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002457 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002458
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002459 }
Zhu Yib481de92007-09-25 17:54:57 -07002460
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002461 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002462 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002463 mutex_unlock(&priv->mutex);
2464
2465 /* If we are getting WEP group key and we didn't receive any key mapping
2466 * so far, we are in legacy wep mode (group key only), otherwise we are
2467 * in 1X mode.
2468 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07002469 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002470 priv->iw_mode != NL80211_IFTYPE_AP) {
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002471 if (cmd == SET_KEY)
2472 is_default_wep_key = !priv->key_mapping_key;
2473 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08002474 is_default_wep_key =
2475 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002476 }
mabbas052c4b92007-10-25 17:15:43 +08002477
Zhu Yib481de92007-09-25 17:54:57 -07002478 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002479 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002480 if (is_default_wep_key)
2481 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002482 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07002483 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002484
Tomas Winklere1623442009-01-27 14:27:56 -08002485 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07002486 break;
2487 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002488 if (is_default_wep_key)
2489 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002490 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07002491 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002492
Tomas Winklere1623442009-01-27 14:27:56 -08002493 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07002494 break;
2495 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002496 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002497 }
2498
Tomas Winklere1623442009-01-27 14:27:56 -08002499 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002500
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002501 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002502}
2503
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002504static int iwl_mac_ampdu_action(struct ieee80211_hw *hw,
Tomas Winklerd783b062008-07-18 13:53:02 +08002505 enum ieee80211_ampdu_mlme_action action,
Johannes Berg17741cd2008-09-11 00:02:02 +02002506 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Tomas Winklerd783b062008-07-18 13:53:02 +08002507{
2508 struct iwl_priv *priv = hw->priv;
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002509 int ret;
Tomas Winklerd783b062008-07-18 13:53:02 +08002510
Tomas Winklere1623442009-01-27 14:27:56 -08002511 IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
Johannes Berge1749612008-10-27 15:59:26 -07002512 sta->addr, tid);
Tomas Winklerd783b062008-07-18 13:53:02 +08002513
2514 if (!(priv->cfg->sku & IWL_SKU_N))
2515 return -EACCES;
2516
2517 switch (action) {
2518 case IEEE80211_AMPDU_RX_START:
Tomas Winklere1623442009-01-27 14:27:56 -08002519 IWL_DEBUG_HT(priv, "start Rx\n");
Tomas Winkler9f586712008-11-12 13:14:05 -08002520 return iwl_sta_rx_agg_start(priv, sta->addr, tid, *ssn);
Tomas Winklerd783b062008-07-18 13:53:02 +08002521 case IEEE80211_AMPDU_RX_STOP:
Tomas Winklere1623442009-01-27 14:27:56 -08002522 IWL_DEBUG_HT(priv, "stop Rx\n");
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002523 ret = iwl_sta_rx_agg_stop(priv, sta->addr, tid);
2524 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2525 return 0;
2526 else
2527 return ret;
Tomas Winklerd783b062008-07-18 13:53:02 +08002528 case IEEE80211_AMPDU_TX_START:
Tomas Winklere1623442009-01-27 14:27:56 -08002529 IWL_DEBUG_HT(priv, "start Tx\n");
Johannes Berg17741cd2008-09-11 00:02:02 +02002530 return iwl_tx_agg_start(priv, sta->addr, tid, ssn);
Tomas Winklerd783b062008-07-18 13:53:02 +08002531 case IEEE80211_AMPDU_TX_STOP:
Tomas Winklere1623442009-01-27 14:27:56 -08002532 IWL_DEBUG_HT(priv, "stop Tx\n");
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002533 ret = iwl_tx_agg_stop(priv, sta->addr, tid);
2534 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2535 return 0;
2536 else
2537 return ret;
Tomas Winklerd783b062008-07-18 13:53:02 +08002538 default:
Tomas Winklere1623442009-01-27 14:27:56 -08002539 IWL_DEBUG_HT(priv, "unknown\n");
Tomas Winklerd783b062008-07-18 13:53:02 +08002540 return -EINVAL;
2541 break;
2542 }
2543 return 0;
2544}
Tomas Winkler9f586712008-11-12 13:14:05 -08002545
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002546static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002547 struct ieee80211_tx_queue_stats *stats)
2548{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002549 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002550 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08002551 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08002552 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07002553 unsigned long flags;
2554
Tomas Winklere1623442009-01-27 14:27:56 -08002555 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002556
Tomas Winklerfee12472008-04-03 16:05:21 -07002557 if (!iwl_is_ready_rf(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002558 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07002559 return -EIO;
2560 }
2561
2562 spin_lock_irqsave(&priv->lock, flags);
2563
2564 for (i = 0; i < AC_NUM; i++) {
2565 txq = &priv->txq[i];
2566 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08002567 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07002568
Johannes Berg57ffc582008-04-29 17:18:59 +02002569 stats[i].len = q->n_window - avail;
2570 stats[i].limit = q->n_window - q->high_mark;
2571 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07002572
2573 }
2574 spin_unlock_irqrestore(&priv->lock, flags);
2575
Tomas Winklere1623442009-01-27 14:27:56 -08002576 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002577
2578 return 0;
2579}
2580
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002581static int iwl_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002582 struct ieee80211_low_level_stats *stats)
2583{
Ester Kummerbf403db2008-05-05 10:22:40 +08002584 struct iwl_priv *priv = hw->priv;
2585
2586 priv = hw->priv;
Tomas Winklere1623442009-01-27 14:27:56 -08002587 IWL_DEBUG_MAC80211(priv, "enter\n");
2588 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002589
2590 return 0;
2591}
2592
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002593static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002594{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002595 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002596 unsigned long flags;
2597
2598 mutex_lock(&priv->mutex);
Tomas Winklere1623442009-01-27 14:27:56 -08002599 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002600
Zhu Yib481de92007-09-25 17:54:57 -07002601 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02002602 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07002603 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -07002604
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07002605 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002606
Zhu Yib481de92007-09-25 17:54:57 -07002607 spin_lock_irqsave(&priv->lock, flags);
2608 priv->assoc_id = 0;
2609 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002610 priv->assoc_station_added = 0;
2611
2612 /* new association get rid of ibss beacon skb */
2613 if (priv->ibss_beacon)
2614 dev_kfree_skb(priv->ibss_beacon);
2615
2616 priv->ibss_beacon = NULL;
2617
2618 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07002619 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02002620 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07002621 priv->beacon_int = 0;
2622
2623 spin_unlock_irqrestore(&priv->lock, flags);
2624
Tomas Winklerfee12472008-04-03 16:05:21 -07002625 if (!iwl_is_ready_rf(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -08002626 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08002627 mutex_unlock(&priv->mutex);
2628 return;
2629 }
2630
mabbas052c4b92007-10-25 17:15:43 +08002631 /* we are restarting association process
2632 * clear RXON_FILTER_ASSOC_MSK bit
2633 */
Johannes Berg05c914f2008-09-11 00:01:58 +02002634 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Tomas Winkler2a421b92008-06-12 09:47:10 +08002635 iwl_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08002636 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002637 iwlcore_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08002638 }
2639
Mohamed Abbas5da4b552008-04-21 15:41:51 -07002640 iwl_power_update_mode(priv, 0);
2641
Zhu Yib481de92007-09-25 17:54:57 -07002642 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02002643 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
mabbas052c4b92007-10-25 17:15:43 +08002644
Emmanuel Grumbachc90a74ba2008-09-03 11:26:50 +08002645 /* switch to CAM during association period.
2646 * the ucode will block any association/authentication
2647 * frome during assiciation period if it can not hear
2648 * the AP because of PM. the timer enable PM back is
2649 * association do not complete
2650 */
2651 if (priv->hw->conf.channel->flags & (IEEE80211_CHAN_PASSIVE_SCAN |
2652 IEEE80211_CHAN_RADAR))
2653 iwl_power_disable_management(priv, 3000);
2654
Tomas Winklere1623442009-01-27 14:27:56 -08002655 IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07002656 mutex_unlock(&priv->mutex);
2657 return;
2658 }
2659
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002660 iwl_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002661
2662 mutex_unlock(&priv->mutex);
2663
Tomas Winklere1623442009-01-27 14:27:56 -08002664 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002665}
2666
Zhu Yib481de92007-09-25 17:54:57 -07002667
Zhu Yib481de92007-09-25 17:54:57 -07002668/*****************************************************************************
2669 *
2670 * sysfs attributes
2671 *
2672 *****************************************************************************/
2673
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002674#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002675
2676/*
2677 * The following adds a new attribute to the sysfs representation
Wu, Fengguangc3a739f2008-12-17 16:52:29 +08002678 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
Zhu Yib481de92007-09-25 17:54:57 -07002679 * used for controlling the debug level.
2680 *
2681 * See the level definitions in iwl for details.
2682 */
2683
Ester Kummer8cf769c2008-05-06 11:05:11 +08002684static ssize_t show_debug_level(struct device *d,
2685 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07002686{
Ester Kummer8cf769c2008-05-06 11:05:11 +08002687 struct iwl_priv *priv = d->driver_data;
2688
2689 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07002690}
Ester Kummer8cf769c2008-05-06 11:05:11 +08002691static ssize_t store_debug_level(struct device *d,
2692 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07002693 const char *buf, size_t count)
2694{
Ester Kummer8cf769c2008-05-06 11:05:11 +08002695 struct iwl_priv *priv = d->driver_data;
Tomas Winkler9257746f2008-09-03 11:26:32 +08002696 unsigned long val;
2697 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002698
Tomas Winkler9257746f2008-09-03 11:26:32 +08002699 ret = strict_strtoul(buf, 0, &val);
2700 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08002701 IWL_ERR(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07002702 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08002703 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07002704
2705 return strnlen(buf, count);
2706}
2707
Ester Kummer8cf769c2008-05-06 11:05:11 +08002708static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
2709 show_debug_level, store_debug_level);
2710
Zhu Yib481de92007-09-25 17:54:57 -07002711
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002712#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07002713
Zhu Yib481de92007-09-25 17:54:57 -07002714
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08002715static ssize_t show_version(struct device *d,
2716 struct device_attribute *attr, char *buf)
2717{
2718 struct iwl_priv *priv = d->driver_data;
Tomas Winkler885ba202008-05-29 16:34:55 +08002719 struct iwl_alive_resp *palive = &priv->card_alive;
Tomas Winklerf236a262008-06-30 17:23:02 +08002720 ssize_t pos = 0;
2721 u16 eeprom_ver;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08002722
2723 if (palive->is_valid)
Tomas Winklerf236a262008-06-30 17:23:02 +08002724 pos += sprintf(buf + pos,
2725 "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
2726 "fw type: 0x%01X 0x%01X\n",
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08002727 palive->ucode_major, palive->ucode_minor,
2728 palive->sw_rev[0], palive->sw_rev[1],
2729 palive->ver_type, palive->ver_subtype);
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08002730 else
Tomas Winklerf236a262008-06-30 17:23:02 +08002731 pos += sprintf(buf + pos, "fw not loaded\n");
2732
2733 if (priv->eeprom) {
2734 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
2735 pos += sprintf(buf + pos, "EEPROM version: 0x%x\n",
2736 eeprom_ver);
2737 } else {
2738 pos += sprintf(buf + pos, "EEPROM not initialzed\n");
2739 }
2740
2741 return pos;
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08002742}
2743
2744static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
2745
Zhu Yib481de92007-09-25 17:54:57 -07002746static ssize_t show_temperature(struct device *d,
2747 struct device_attribute *attr, char *buf)
2748{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002749 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07002750
Tomas Winklerfee12472008-04-03 16:05:21 -07002751 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002752 return -EAGAIN;
2753
Emmanuel Grumbach91dbc5b2008-06-12 09:47:14 +08002754 return sprintf(buf, "%d\n", priv->temperature);
Zhu Yib481de92007-09-25 17:54:57 -07002755}
2756
2757static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
2758
Zhu Yib481de92007-09-25 17:54:57 -07002759static ssize_t show_tx_power(struct device *d,
2760 struct device_attribute *attr, char *buf)
2761{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002762 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Jay Sternberg91f39e82008-12-17 16:52:34 +08002763
2764 if (!iwl_is_ready_rf(priv))
2765 return sprintf(buf, "off\n");
2766 else
2767 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07002768}
2769
2770static ssize_t store_tx_power(struct device *d,
2771 struct device_attribute *attr,
2772 const char *buf, size_t count)
2773{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002774 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winkler9257746f2008-09-03 11:26:32 +08002775 unsigned long val;
2776 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002777
Tomas Winkler9257746f2008-09-03 11:26:32 +08002778 ret = strict_strtoul(buf, 10, &val);
2779 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08002780 IWL_INFO(priv, "%s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07002781 else
Tomas Winkler630fe9b2008-06-12 09:47:08 +08002782 iwl_set_tx_power(priv, val, false);
Zhu Yib481de92007-09-25 17:54:57 -07002783
2784 return count;
2785}
2786
2787static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
2788
2789static ssize_t show_flags(struct device *d,
2790 struct device_attribute *attr, char *buf)
2791{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002792 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07002793
2794 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
2795}
2796
2797static ssize_t store_flags(struct device *d,
2798 struct device_attribute *attr,
2799 const char *buf, size_t count)
2800{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002801 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winkler9257746f2008-09-03 11:26:32 +08002802 unsigned long val;
2803 u32 flags;
2804 int ret = strict_strtoul(buf, 0, &val);
Emmanuel Grumbach926f0b22008-09-03 11:26:39 +08002805 if (ret)
Tomas Winkler9257746f2008-09-03 11:26:32 +08002806 return ret;
2807 flags = (u32)val;
Zhu Yib481de92007-09-25 17:54:57 -07002808
2809 mutex_lock(&priv->mutex);
2810 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
2811 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08002812 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002813 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002814 else {
Tomas Winklere1623442009-01-27 14:27:56 -08002815 IWL_DEBUG_INFO(priv, "Commit rxon.flags = 0x%04X\n", flags);
Zhu Yib481de92007-09-25 17:54:57 -07002816 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002817 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002818 }
2819 }
2820 mutex_unlock(&priv->mutex);
2821
2822 return count;
2823}
2824
2825static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
2826
2827static ssize_t show_filter_flags(struct device *d,
2828 struct device_attribute *attr, char *buf)
2829{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002830 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07002831
2832 return sprintf(buf, "0x%04X\n",
2833 le32_to_cpu(priv->active_rxon.filter_flags));
2834}
2835
2836static ssize_t store_filter_flags(struct device *d,
2837 struct device_attribute *attr,
2838 const char *buf, size_t count)
2839{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002840 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winkler9257746f2008-09-03 11:26:32 +08002841 unsigned long val;
2842 u32 filter_flags;
2843 int ret = strict_strtoul(buf, 0, &val);
Emmanuel Grumbach926f0b22008-09-03 11:26:39 +08002844 if (ret)
Tomas Winkler9257746f2008-09-03 11:26:32 +08002845 return ret;
2846 filter_flags = (u32)val;
Zhu Yib481de92007-09-25 17:54:57 -07002847
2848 mutex_lock(&priv->mutex);
2849 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
2850 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08002851 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002852 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002853 else {
Tomas Winklere1623442009-01-27 14:27:56 -08002854 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07002855 "0x%04X\n", filter_flags);
2856 priv->staging_rxon.filter_flags =
2857 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002858 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002859 }
2860 }
2861 mutex_unlock(&priv->mutex);
2862
2863 return count;
2864}
2865
2866static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
2867 store_filter_flags);
2868
Zhu Yib481de92007-09-25 17:54:57 -07002869static ssize_t store_power_level(struct device *d,
2870 struct device_attribute *attr,
2871 const char *buf, size_t count)
2872{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002873 struct iwl_priv *priv = dev_get_drvdata(d);
Esti Kummer298df1f2008-07-18 13:52:58 +08002874 int ret;
Tomas Winkler9257746f2008-09-03 11:26:32 +08002875 unsigned long mode;
Zhu Yib481de92007-09-25 17:54:57 -07002876
Tomas Winkler9257746f2008-09-03 11:26:32 +08002877
Zhu Yib481de92007-09-25 17:54:57 -07002878 mutex_lock(&priv->mutex);
2879
Tomas Winkler9257746f2008-09-03 11:26:32 +08002880 ret = strict_strtoul(buf, 10, &mode);
Emmanuel Grumbach926f0b22008-09-03 11:26:39 +08002881 if (ret)
Tomas Winkler9257746f2008-09-03 11:26:32 +08002882 goto out;
2883
Esti Kummer298df1f2008-07-18 13:52:58 +08002884 ret = iwl_power_set_user_mode(priv, mode);
2885 if (ret) {
Tomas Winklere1623442009-01-27 14:27:56 -08002886 IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07002887 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002888 }
Esti Kummer298df1f2008-07-18 13:52:58 +08002889 ret = count;
Zhu Yib481de92007-09-25 17:54:57 -07002890
2891 out:
2892 mutex_unlock(&priv->mutex);
Esti Kummer298df1f2008-07-18 13:52:58 +08002893 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002894}
2895
Zhu Yib481de92007-09-25 17:54:57 -07002896static ssize_t show_power_level(struct device *d,
2897 struct device_attribute *attr, char *buf)
2898{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002899 struct iwl_priv *priv = dev_get_drvdata(d);
Esti Kummer298df1f2008-07-18 13:52:58 +08002900 int mode = priv->power_data.user_power_setting;
2901 int system = priv->power_data.system_power_setting;
Mohamed Abbas5da4b552008-04-21 15:41:51 -07002902 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07002903 char *p = buf;
2904
Esti Kummer298df1f2008-07-18 13:52:58 +08002905 switch (system) {
2906 case IWL_POWER_SYS_AUTO:
2907 p += sprintf(p, "SYSTEM:auto");
Zhu Yib481de92007-09-25 17:54:57 -07002908 break;
Esti Kummer298df1f2008-07-18 13:52:58 +08002909 case IWL_POWER_SYS_AC:
2910 p += sprintf(p, "SYSTEM:ac");
Zhu Yib481de92007-09-25 17:54:57 -07002911 break;
Esti Kummer298df1f2008-07-18 13:52:58 +08002912 case IWL_POWER_SYS_BATTERY:
2913 p += sprintf(p, "SYSTEM:battery");
2914 break;
Zhu Yib481de92007-09-25 17:54:57 -07002915 }
Esti Kummer298df1f2008-07-18 13:52:58 +08002916
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08002917 p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
2918 "fixed" : "auto");
Esti Kummer298df1f2008-07-18 13:52:58 +08002919 p += sprintf(p, "\tINDEX:%d", level);
2920 p += sprintf(p, "\n");
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002921 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07002922}
2923
2924static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
2925 store_power_level);
2926
Zhu Yib481de92007-09-25 17:54:57 -07002927
2928static ssize_t show_statistics(struct device *d,
2929 struct device_attribute *attr, char *buf)
2930{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002931 struct iwl_priv *priv = dev_get_drvdata(d);
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +08002932 u32 size = sizeof(struct iwl_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07002933 u32 len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002934 u8 *data = (u8 *)&priv->statistics;
Zhu Yib481de92007-09-25 17:54:57 -07002935 int rc = 0;
2936
Tomas Winklerfee12472008-04-03 16:05:21 -07002937 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002938 return -EAGAIN;
2939
2940 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07002941 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002942 mutex_unlock(&priv->mutex);
2943
2944 if (rc) {
2945 len = sprintf(buf,
2946 "Error sending statistics request: 0x%08X\n", rc);
2947 return len;
2948 }
2949
2950 while (size && (PAGE_SIZE - len)) {
2951 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
2952 PAGE_SIZE - len, 1);
2953 len = strlen(buf);
2954 if (PAGE_SIZE - len)
2955 buf[len++] = '\n';
2956
2957 ofs += 16;
2958 size -= min(size, 16U);
2959 }
2960
2961 return len;
2962}
2963
2964static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
2965
Zhu Yib481de92007-09-25 17:54:57 -07002966
Zhu Yib481de92007-09-25 17:54:57 -07002967/*****************************************************************************
2968 *
2969 * driver setup and teardown
2970 *
2971 *****************************************************************************/
2972
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002973static void iwl_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002974{
Reinette Chatred21050c2009-02-13 11:51:18 -08002975 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07002976
2977 init_waitqueue_head(&priv->wait_command_queue);
2978
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002979 INIT_WORK(&priv->up, iwl_bg_up);
2980 INIT_WORK(&priv->restart, iwl_bg_restart);
2981 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
2982 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
2983 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08002984 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002985 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
2986 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002987
Tomas Winkler2a421b92008-06-12 09:47:10 +08002988 iwl_setup_scan_deferred_work(priv);
Emmanuel Grumbachc90a74ba2008-09-03 11:26:50 +08002989 iwl_setup_power_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002990
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002991 if (priv->cfg->ops->lib->setup_deferred_work)
2992 priv->cfg->ops->lib->setup_deferred_work(priv);
2993
2994 init_timer(&priv->statistics_periodic);
2995 priv->statistics_periodic.data = (unsigned long)priv;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002996 priv->statistics_periodic.function = iwl_bg_statistics_periodic;
Zhu Yib481de92007-09-25 17:54:57 -07002997
2998 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002999 iwl_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003000}
3001
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003002static void iwl_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003003{
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003004 if (priv->cfg->ops->lib->cancel_deferred_work)
3005 priv->cfg->ops->lib->cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003006
Joonwoo Park3ae6a052007-11-29 10:43:16 +09003007 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003008 cancel_delayed_work(&priv->scan_check);
Emmanuel Grumbachc90a74ba2008-09-03 11:26:50 +08003009 cancel_delayed_work_sync(&priv->set_power_save);
Zhu Yib481de92007-09-25 17:54:57 -07003010 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003011 cancel_work_sync(&priv->beacon_update);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003012 del_timer_sync(&priv->statistics_periodic);
Zhu Yib481de92007-09-25 17:54:57 -07003013}
3014
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003015static struct attribute *iwl_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003016 &dev_attr_flags.attr,
3017 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003018 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003019 &dev_attr_statistics.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003020 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003021 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08003022#ifdef CONFIG_IWLWIFI_DEBUG
3023 &dev_attr_debug_level.attr,
3024#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08003025 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003026
3027 NULL
3028};
3029
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003030static struct attribute_group iwl_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003031 .name = NULL, /* put in device directory */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003032 .attrs = iwl_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003033};
3034
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003035static struct ieee80211_ops iwl_hw_ops = {
3036 .tx = iwl_mac_tx,
3037 .start = iwl_mac_start,
3038 .stop = iwl_mac_stop,
3039 .add_interface = iwl_mac_add_interface,
3040 .remove_interface = iwl_mac_remove_interface,
3041 .config = iwl_mac_config,
3042 .config_interface = iwl_mac_config_interface,
3043 .configure_filter = iwl_configure_filter,
3044 .set_key = iwl_mac_set_key,
3045 .update_tkip_key = iwl_mac_update_tkip_key,
3046 .get_stats = iwl_mac_get_stats,
3047 .get_tx_stats = iwl_mac_get_tx_stats,
3048 .conf_tx = iwl_mac_conf_tx,
3049 .reset_tsf = iwl_mac_reset_tsf,
3050 .bss_info_changed = iwl_bss_info_changed,
3051 .ampdu_action = iwl_mac_ampdu_action,
Tomas Winklercb43dc22008-09-03 11:26:23 +08003052 .hw_scan = iwl_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07003053};
3054
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003055static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003056{
3057 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003058 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003059 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08003060 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003061 unsigned long flags;
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003062 u16 pci_cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003063
Assaf Krauss316c30d2008-03-14 10:38:46 -07003064 /************************
3065 * 1. Allocating HW data
3066 ************************/
3067
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003068 /* Disabling hardware scan means that mac80211 will perform scans
3069 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07003070 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08003071 if (cfg->mod_params->debug & IWL_DL_INFO)
3072 dev_printk(KERN_DEBUG, &(pdev->dev),
3073 "Disabling hw_scan\n");
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003074 iwl_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003075 }
3076
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003077 hw = iwl_alloc_all(cfg, &iwl_hw_ops);
Assaf Krauss1d0a0822008-03-14 10:38:48 -07003078 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07003079 err = -ENOMEM;
3080 goto out;
3081 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07003082 priv = hw->priv;
3083 /* At this point both hw and priv are allocated. */
3084
Zhu Yib481de92007-09-25 17:54:57 -07003085 SET_IEEE80211_DEV(hw, &pdev->dev);
3086
Tomas Winklere1623442009-01-27 14:27:56 -08003087 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08003088 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07003089 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003090
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003091#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003092 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07003093 atomic_set(&priv->restrict_refcnt, 0);
3094#endif
Zhu Yib481de92007-09-25 17:54:57 -07003095
Assaf Krauss316c30d2008-03-14 10:38:46 -07003096 /**************************
3097 * 2. Initializing PCI bus
3098 **************************/
3099 if (pci_enable_device(pdev)) {
3100 err = -ENODEV;
3101 goto out_ieee80211_free_hw;
3102 }
3103
3104 pci_set_master(pdev);
3105
Winkler, Tomas093d874c2008-09-26 15:09:34 +08003106 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
Assaf Krauss316c30d2008-03-14 10:38:46 -07003107 if (!err)
Winkler, Tomas093d874c2008-09-26 15:09:34 +08003108 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003109 if (err) {
Winkler, Tomas093d874c2008-09-26 15:09:34 +08003110 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003111 if (!err)
Winkler, Tomas093d874c2008-09-26 15:09:34 +08003112 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003113 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003114 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08003115 IWL_WARN(priv, "No suitable DMA available.\n");
Assaf Krauss316c30d2008-03-14 10:38:46 -07003116 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003117 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07003118 }
3119
3120 err = pci_request_regions(pdev, DRV_NAME);
3121 if (err)
3122 goto out_pci_disable_device;
3123
3124 pci_set_drvdata(pdev, priv);
3125
Assaf Krauss316c30d2008-03-14 10:38:46 -07003126
3127 /***********************
3128 * 3. Read REV register
3129 ***********************/
3130 priv->hw_base = pci_iomap(pdev, 0, 0);
3131 if (!priv->hw_base) {
3132 err = -ENODEV;
3133 goto out_pci_release_regions;
3134 }
3135
Tomas Winklere1623442009-01-27 14:27:56 -08003136 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Assaf Krauss316c30d2008-03-14 10:38:46 -07003137 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08003138 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003139
Tomas Winklerb661c812008-04-23 17:14:54 -07003140 iwl_hw_detect(priv);
Tomas Winkler978785a2008-12-19 10:37:31 +08003141 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s REV=0x%X\n",
Tomas Winklerb661c812008-04-23 17:14:54 -07003142 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003143
Tomas Winklere7b63582008-09-03 11:26:49 +08003144 /* We disable the RETRY_TIMEOUT register (0x41) to keep
3145 * PCI Tx retries from interfering with C3 CPU state */
3146 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
3147
Tomas Winkler91238712008-04-23 17:14:53 -07003148 /* amp init */
3149 err = priv->cfg->ops->lib->apm_ops.init(priv);
3150 if (err < 0) {
Reinette Chatre808ff692009-03-11 11:18:01 -07003151 IWL_ERR(priv, "Failed to init APMG\n");
Tomas Winkler91238712008-04-23 17:14:53 -07003152 goto out_iounmap;
3153 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07003154 /*****************
3155 * 4. Read EEPROM
3156 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07003157 /* Read the EEPROM */
3158 err = iwl_eeprom_init(priv);
3159 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003160 IWL_ERR(priv, "Unable to init EEPROM\n");
Assaf Krauss316c30d2008-03-14 10:38:46 -07003161 goto out_iounmap;
3162 }
Tomas Winkler8614f362008-04-23 17:14:55 -07003163 err = iwl_eeprom_check_version(priv);
3164 if (err)
Reinette Chatrec8f16132009-02-27 16:21:22 -08003165 goto out_free_eeprom;
Tomas Winkler8614f362008-04-23 17:14:55 -07003166
Ron Rindjunsky02883012008-05-15 13:53:53 +08003167 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003168 iwl_eeprom_get_mac(priv, priv->mac_addr);
Tomas Winklere1623442009-01-27 14:27:56 -08003169 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003170 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
3171
3172 /************************
3173 * 5. Setup HW constants
3174 ************************/
Ron Rindjunskyda154e302008-06-30 17:23:20 +08003175 if (iwl_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003176 IWL_ERR(priv, "failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07003177 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003178 }
3179
3180 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08003181 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07003182 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07003183
Tomas Winkler6ba87952008-05-15 13:54:17 +08003184 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003185 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07003186 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003187 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003188
3189 /**********************************
3190 * 7. Initialize module parameters
3191 **********************************/
3192
3193 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07003194 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07003195 set_bit(STATUS_RF_KILL_SW, &priv->status);
Tomas Winklere1623442009-01-27 14:27:56 -08003196 IWL_DEBUG_INFO(priv, "Radio disabled.\n");
Assaf Krauss316c30d2008-03-14 10:38:46 -07003197 }
3198
Assaf Krauss316c30d2008-03-14 10:38:46 -07003199 /********************
3200 * 8. Setup services
3201 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003202 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003203 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003204 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003205
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003206 pci_enable_msi(priv->pci_dev);
3207
3208 err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
3209 DRV_NAME, priv);
3210 if (err) {
3211 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
3212 goto out_disable_msi;
3213 }
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003214 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003215 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003216 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Helmut Schaa795cc0a2009-02-12 18:51:03 +01003217 goto out_free_irq;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003218 }
3219
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003220 iwl_setup_deferred_work(priv);
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08003221 iwl_setup_rx_handlers(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003222
Tomas Winkler6ba87952008-05-15 13:54:17 +08003223 /**********************************
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003224 * 9. Setup and register mac80211
Tomas Winkler6ba87952008-05-15 13:54:17 +08003225 **********************************/
3226
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003227 /* enable interrupts if needed: hw bug w/a */
3228 pci_read_config_word(priv->pci_dev, PCI_COMMAND, &pci_cmd);
3229 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
3230 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
3231 pci_write_config_word(priv->pci_dev, PCI_COMMAND, pci_cmd);
3232 }
3233
3234 iwl_enable_interrupts(priv);
3235
Tomas Winkler6ba87952008-05-15 13:54:17 +08003236 err = iwl_setup_mac(priv);
3237 if (err)
3238 goto out_remove_sysfs;
3239
3240 err = iwl_dbgfs_register(priv, DRV_NAME);
3241 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003242 IWL_ERR(priv, "failed to create debugfs files\n");
Tomas Winkler6ba87952008-05-15 13:54:17 +08003243
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003244 /* If platform's RF_KILL switch is NOT set to KILL */
3245 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3246 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3247 else
3248 set_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003249
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08003250 err = iwl_rfkill_init(priv);
3251 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003252 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08003253 "Ignoring error: %d\n", err);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003254 else
3255 iwl_rfkill_set_hw_state(priv);
3256
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08003257 iwl_power_initialize(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003258 return 0;
3259
Assaf Krauss316c30d2008-03-14 10:38:46 -07003260 out_remove_sysfs:
Reinette Chatrec8f16132009-02-27 16:21:22 -08003261 destroy_workqueue(priv->workqueue);
3262 priv->workqueue = NULL;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003263 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
Helmut Schaa795cc0a2009-02-12 18:51:03 +01003264 out_free_irq:
3265 free_irq(priv->pci_dev->irq, priv);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003266 out_disable_msi:
3267 pci_disable_msi(priv->pci_dev);
Tomas Winkler6ba87952008-05-15 13:54:17 +08003268 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07003269 out_free_eeprom:
3270 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003271 out_iounmap:
3272 pci_iounmap(pdev, priv->hw_base);
3273 out_pci_release_regions:
Assaf Krauss316c30d2008-03-14 10:38:46 -07003274 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08003275 pci_release_regions(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07003276 out_pci_disable_device:
3277 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07003278 out_ieee80211_free_hw:
3279 ieee80211_free_hw(priv->hw);
3280 out:
3281 return err;
3282}
3283
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003284static void __devexit iwl_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07003285{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003286 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003287 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003288
3289 if (!priv)
3290 return;
3291
Tomas Winklere1623442009-01-27 14:27:56 -08003292 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07003293
Emmanuel Grumbach67249622008-05-29 16:35:26 +08003294 iwl_dbgfs_unregister(priv);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003295 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
Emmanuel Grumbach67249622008-05-29 16:35:26 +08003296
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003297 /* ieee80211_unregister_hw call wil cause iwl_mac_stop to
3298 * to be called and iwl_down since we are removing the device
Gregory Greenman0b124c32008-09-03 11:18:50 +08003299 * we need to set STATUS_EXIT_PENDING bit.
3300 */
3301 set_bit(STATUS_EXIT_PENDING, &priv->status);
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07003302 if (priv->mac80211_registered) {
3303 ieee80211_unregister_hw(priv->hw);
3304 priv->mac80211_registered = 0;
Gregory Greenman0b124c32008-09-03 11:18:50 +08003305 } else {
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003306 iwl_down(priv);
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07003307 }
3308
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003309 /* make sure we flush any pending irq or
3310 * tasklet for the driver
3311 */
3312 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003313 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003314 spin_unlock_irqrestore(&priv->lock, flags);
3315
3316 iwl_synchronize_irq(priv);
3317
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08003318 iwl_rfkill_unregister(priv);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003319 iwl_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003320
3321 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08003322 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08003323 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003324
Abhijeet Kolekare11bc022009-04-08 11:26:41 -07003325 priv->cfg->ops->smgmt->clear_station_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07003326 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003327
Zhu Yib481de92007-09-25 17:54:57 -07003328
Mohamed Abbas948c1712007-10-25 17:15:45 +08003329 /*netif_stop_queue(dev); */
3330 flush_workqueue(priv->workqueue);
3331
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003332 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07003333 * priv->workqueue... so we can't take down the workqueue
3334 * until now... */
3335 destroy_workqueue(priv->workqueue);
3336 priv->workqueue = NULL;
3337
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003338 free_irq(priv->pci_dev->irq, priv);
3339 pci_disable_msi(priv->pci_dev);
Zhu Yib481de92007-09-25 17:54:57 -07003340 pci_iounmap(pdev, priv->hw_base);
3341 pci_release_regions(pdev);
3342 pci_disable_device(pdev);
3343 pci_set_drvdata(pdev, NULL);
3344
Tomas Winkler6ba87952008-05-15 13:54:17 +08003345 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003346
3347 if (priv->ibss_beacon)
3348 dev_kfree_skb(priv->ibss_beacon);
3349
3350 ieee80211_free_hw(priv->hw);
3351}
3352
Zhu Yib481de92007-09-25 17:54:57 -07003353
3354/*****************************************************************************
3355 *
3356 * driver and module entry point
3357 *
3358 *****************************************************************************/
3359
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003360/* Hardware specific file defines the PCI IDs table for that hardware module */
3361static struct pci_device_id iwl_hw_card_ids[] = {
Tomas Winkler4fc22b22008-07-21 18:54:42 +03003362#ifdef CONFIG_IWL4965
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003363 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
3364 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler4fc22b22008-07-21 18:54:42 +03003365#endif /* CONFIG_IWL4965 */
Tomas Winkler5a6a2562008-04-24 11:55:23 -07003366#ifdef CONFIG_IWL5000
Esti Kummer47408632008-07-11 11:53:30 +08003367 {IWL_PCI_DEVICE(0x4232, 0x1205, iwl5100_bg_cfg)},
3368 {IWL_PCI_DEVICE(0x4232, 0x1305, iwl5100_bg_cfg)},
3369 {IWL_PCI_DEVICE(0x4232, 0x1206, iwl5100_abg_cfg)},
3370 {IWL_PCI_DEVICE(0x4232, 0x1306, iwl5100_abg_cfg)},
3371 {IWL_PCI_DEVICE(0x4232, 0x1326, iwl5100_abg_cfg)},
3372 {IWL_PCI_DEVICE(0x4237, 0x1216, iwl5100_abg_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07003373 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
Esti Kummer47408632008-07-11 11:53:30 +08003374 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
3375 {IWL_PCI_DEVICE(0x4236, PCI_ANY_ID, iwl5300_agn_cfg)},
3376 {IWL_PCI_DEVICE(0x4237, PCI_ANY_ID, iwl5100_agn_cfg)},
Tomas Winklere96a8492008-09-11 11:45:20 +08003377/* 5350 WiFi/WiMax */
3378 {IWL_PCI_DEVICE(0x423A, 0x1001, iwl5350_agn_cfg)},
3379 {IWL_PCI_DEVICE(0x423A, 0x1021, iwl5350_agn_cfg)},
3380 {IWL_PCI_DEVICE(0x423B, 0x1011, iwl5350_agn_cfg)},
Tomas Winkler7100e922008-12-01 16:32:18 -08003381/* 5150 Wifi/WiMax */
3382 {IWL_PCI_DEVICE(0x423C, PCI_ANY_ID, iwl5150_agn_cfg)},
3383 {IWL_PCI_DEVICE(0x423D, PCI_ANY_ID, iwl5150_agn_cfg)},
Jay Sternberge1228372009-01-19 15:30:34 -08003384/* 6000/6050 Series */
3385 {IWL_PCI_DEVICE(0x0082, 0x1102, iwl6000_2ag_cfg)},
3386 {IWL_PCI_DEVICE(0x0085, 0x1112, iwl6000_2ag_cfg)},
3387 {IWL_PCI_DEVICE(0x0082, 0x1122, iwl6000_2ag_cfg)},
3388 {IWL_PCI_DEVICE(0x422B, PCI_ANY_ID, iwl6000_3agn_cfg)},
3389 {IWL_PCI_DEVICE(0x4238, PCI_ANY_ID, iwl6000_3agn_cfg)},
3390 {IWL_PCI_DEVICE(0x0082, PCI_ANY_ID, iwl6000_2agn_cfg)},
3391 {IWL_PCI_DEVICE(0x0085, PCI_ANY_ID, iwl6000_3agn_cfg)},
3392 {IWL_PCI_DEVICE(0x0086, PCI_ANY_ID, iwl6050_3agn_cfg)},
3393 {IWL_PCI_DEVICE(0x0087, PCI_ANY_ID, iwl6050_2agn_cfg)},
3394 {IWL_PCI_DEVICE(0x0088, PCI_ANY_ID, iwl6050_3agn_cfg)},
3395 {IWL_PCI_DEVICE(0x0089, PCI_ANY_ID, iwl6050_2agn_cfg)},
Jay Sternberg77dcb6a2009-03-06 13:52:55 -08003396/* 1000 Series WiFi */
3397 {IWL_PCI_DEVICE(0x0083, PCI_ANY_ID, iwl1000_bgn_cfg)},
3398 {IWL_PCI_DEVICE(0x0084, PCI_ANY_ID, iwl1000_bgn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07003399#endif /* CONFIG_IWL5000 */
Tomas Winkler7100e922008-12-01 16:32:18 -08003400
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003401 {0}
3402};
3403MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
3404
3405static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07003406 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003407 .id_table = iwl_hw_card_ids,
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003408 .probe = iwl_pci_probe,
3409 .remove = __devexit_p(iwl_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07003410#ifdef CONFIG_PM
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003411 .suspend = iwl_pci_suspend,
3412 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07003413#endif
3414};
3415
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003416static int __init iwl_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07003417{
3418
3419 int ret;
3420 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
3421 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003422
Tomas Winklere227cea2008-07-18 13:53:05 +08003423 ret = iwlagn_rate_control_register();
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003424 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003425 printk(KERN_ERR DRV_NAME
3426 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003427 return ret;
3428 }
3429
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003430 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07003431 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003432 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003433 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07003434 }
Zhu Yib481de92007-09-25 17:54:57 -07003435
3436 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003437
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003438error_register:
Tomas Winklere227cea2008-07-18 13:53:05 +08003439 iwlagn_rate_control_unregister();
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003440 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003441}
3442
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003443static void __exit iwl_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07003444{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003445 pci_unregister_driver(&iwl_driver);
Tomas Winklere227cea2008-07-18 13:53:05 +08003446 iwlagn_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07003447}
3448
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003449module_exit(iwl_exit);
3450module_init(iwl_init);