blob: e9f786443d1f8db653f77aa1acb525747daf2b13 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040036#include <linux/sched.h>
Zhu Yib481de92007-09-25 17:54:57 -070037#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Samuel Ortiza3139c52008-12-19 10:37:09 +080048#define DRV_NAME "iwlagn"
49
Assaf Krauss6bc913b2008-03-11 16:17:18 -070050#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070051#include "iwl-dev.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070052#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070053#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070054#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070055#include "iwl-sta.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070056#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070057
Christoph Hellwig416e1432007-10-25 17:15:49 +080058
Zhu Yib481de92007-09-25 17:54:57 -070059/******************************************************************************
60 *
61 * module boiler plate
62 *
63 ******************************************************************************/
64
Zhu Yib481de92007-09-25 17:54:57 -070065/*
66 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070067 */
Tomas Winklerd783b062008-07-18 13:53:02 +080068#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link AGN driver for Linux"
Zhu Yib481de92007-09-25 17:54:57 -070069
Tomas Winkler0a6857e2008-03-12 16:58:49 -070070#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070071#define VD "d"
72#else
73#define VD
74#endif
75
Reinette Chatre80bc5392009-02-13 11:51:19 -080076#ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070077#define VS "s"
78#else
79#define VS
80#endif
81
Tomas Winklerdf48c322008-03-06 10:40:19 -080082#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070083
Zhu Yib481de92007-09-25 17:54:57 -070084
85MODULE_DESCRIPTION(DRV_DESCRIPTION);
86MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080087MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070088MODULE_LICENSE("GPL");
Tomas Winkler4fc22b22008-07-21 18:54:42 +030089MODULE_ALIAS("iwl4965");
Zhu Yib481de92007-09-25 17:54:57 -070090
Zhu Yib481de92007-09-25 17:54:57 -070091/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +080092 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -070093 * the functionality provided here
94 */
95
96/**************************************************************/
97
Zhu Yib481de92007-09-25 17:54:57 -070098/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -070099 * iwl_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700100 *
Ian Schram01ebd062007-10-25 17:15:22 +0800101 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700102 * the active_rxon structure is updated with the new data. This
103 * function correctly transitions out of the RXON_ASSOC_MSK state if
104 * a HW tune is required based on the RXON structure changes.
105 */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700106int iwl_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700107{
108 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800109 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800110 int ret;
111 bool new_assoc =
112 !!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700113
Tomas Winklerfee12472008-04-03 16:05:21 -0700114 if (!iwl_is_alive(priv))
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800115 return -EBUSY;
Zhu Yib481de92007-09-25 17:54:57 -0700116
117 /* always get timestamp with Rx frame */
118 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
119
Samuel Ortiz8ccde882009-01-27 14:27:52 -0800120 ret = iwl_check_rxon_cmd(priv);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800121 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800122 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700123 return -EINVAL;
124 }
125
Wey-Yi Guy0924e5192009-11-06 14:52:54 -0800126 /*
127 * receive commit_rxon request
128 * abort any previous channel switch if still in process
129 */
130 if (priv->switch_rxon.switch_in_progress &&
131 (priv->switch_rxon.channel != priv->staging_rxon.channel)) {
132 IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
133 le16_to_cpu(priv->switch_rxon.channel));
134 priv->switch_rxon.switch_in_progress = false;
135 }
136
Zhu Yib481de92007-09-25 17:54:57 -0700137 /* If we don't need to send a full RXON, we can use
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700138 * iwl_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700139 * and other flags for the current radio configuration. */
Mohamed Abbas54559702008-09-03 11:18:44 +0800140 if (!iwl_full_rxon_required(priv)) {
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800141 ret = iwl_send_rxon_assoc(priv);
142 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800143 IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800144 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700145 }
146
147 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Wey-Yi Guya6435652009-11-06 14:52:46 -0800148 iwl_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700149 return 0;
150 }
151
152 /* station table will be cleared */
153 priv->assoc_station_added = 0;
154
Zhu Yib481de92007-09-25 17:54:57 -0700155 /* If we are currently associated and the new config requires
156 * an RXON_ASSOC and the new config wants the associated mask enabled,
157 * we must clear the associated from the active configuration
158 * before we apply the new config */
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800159 if (iwl_is_associated(priv) && new_assoc) {
Tomas Winklere1623442009-01-27 14:27:56 -0800160 IWL_DEBUG_INFO(priv, "Toggling associated bit on current RXON\n");
Zhu Yib481de92007-09-25 17:54:57 -0700161 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
162
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800163 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800164 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700165 &priv->active_rxon);
166
167 /* If the mask clearing failed then we set
168 * active_rxon back to what it was previously */
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800169 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700170 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800171 IWL_ERR(priv, "Error clearing ASSOC_MSK (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800172 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700173 }
Zhu Yib481de92007-09-25 17:54:57 -0700174 }
175
Tomas Winklere1623442009-01-27 14:27:56 -0800176 IWL_DEBUG_INFO(priv, "Sending RXON\n"
Zhu Yib481de92007-09-25 17:54:57 -0700177 "* with%s RXON_FILTER_ASSOC_MSK\n"
178 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700179 "* bssid = %pM\n",
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800180 (new_assoc ? "" : "out"),
Zhu Yib481de92007-09-25 17:54:57 -0700181 le16_to_cpu(priv->staging_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700182 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700183
Tomas Winkler90e8e422009-06-19 13:52:42 -0700184 iwl_set_rxon_hwcrypto(priv, !priv->cfg->mod_params->sw_crypto);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800185
186 /* Apply the new configuration
187 * RXON unassoc clears the station table in uCode, send it before
188 * we add the bcast station. If assoc bit is set, we will send RXON
189 * after having added the bcast and bssid station.
190 */
191 if (!new_assoc) {
192 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800193 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800194 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800195 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800196 return ret;
197 }
198 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700199 }
200
Tomas Winklerc587de02009-06-03 11:44:07 -0700201 iwl_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800202
Johannes Berg19cc1082009-05-08 13:44:36 -0700203 priv->start_calib = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700204
Zhu Yib481de92007-09-25 17:54:57 -0700205 /* Add the broadcast address so we can send broadcast frames */
Reinette Chatre9a9ca652009-10-30 14:36:12 -0700206 iwl_add_bcast_station(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700207
208 /* If we have set the ASSOC_MSK and we are in BSS mode then
209 * add the IWL_AP_ID to the station rate table */
Tomas Winkler91851592008-06-30 17:23:14 +0800210 if (new_assoc) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200211 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Tomas Winkler91851592008-06-30 17:23:14 +0800212 ret = iwl_rxon_add_station(priv,
213 priv->active_rxon.bssid_addr, 1);
214 if (ret == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800215 IWL_ERR(priv,
216 "Error adding AP address for TX.\n");
Tomas Winkler91851592008-06-30 17:23:14 +0800217 return -EIO;
218 }
219 priv->assoc_station_added = 1;
220 if (priv->default_wep_key &&
221 iwl_send_static_wepkey_cmd(priv, 0))
Winkler, Tomas15b16872008-12-19 10:37:33 +0800222 IWL_ERR(priv,
223 "Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700224 }
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800225
Wey-Yi Guy47eef9b2009-09-17 10:43:44 -0700226 /*
227 * allow CTS-to-self if possible for new association.
228 * this is relevant only for 5000 series and up,
229 * but will not damage 4965
230 */
231 priv->staging_rxon.flags |= RXON_FLG_SELF_CTS_EN;
232
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800233 /* Apply the new configuration
234 * RXON assoc doesn't clear the station table in uCode,
235 */
236 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
237 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
238 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800239 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800240 return ret;
241 }
242 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700243 }
Wey-Yi Guya6435652009-11-06 14:52:46 -0800244 iwl_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700245
Zhu Yi36da7d72008-07-11 11:53:40 +0800246 iwl_init_sensitivity(priv);
247
248 /* If we issue a new RXON command which required a tune then we must
249 * send a new TXPOWER command or we won't be able to Tx any frames */
250 ret = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
251 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800252 IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
Zhu Yi36da7d72008-07-11 11:53:40 +0800253 return ret;
254 }
255
Zhu Yib481de92007-09-25 17:54:57 -0700256 return 0;
257}
258
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700259void iwl_update_chain_flags(struct iwl_priv *priv)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700260{
261
Abhijeet Kolekar45823532009-04-08 11:26:44 -0700262 if (priv->cfg->ops->hcmd->set_rxon_chain)
263 priv->cfg->ops->hcmd->set_rxon_chain(priv);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700264 iwlcore_commit_rxon(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700265}
266
Tomas Winklerfcab4232008-05-15 13:54:01 +0800267static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700268{
269 struct list_head *element;
270
Tomas Winklere1623442009-01-27 14:27:56 -0800271 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700272 priv->frames_count);
273
274 while (!list_empty(&priv->free_frames)) {
275 element = priv->free_frames.next;
276 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800277 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700278 priv->frames_count--;
279 }
280
281 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800282 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700283 priv->frames_count);
284 priv->frames_count = 0;
285 }
286}
287
Tomas Winklerfcab4232008-05-15 13:54:01 +0800288static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700289{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800290 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700291 struct list_head *element;
292 if (list_empty(&priv->free_frames)) {
293 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
294 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800295 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700296 return NULL;
297 }
298
299 priv->frames_count++;
300 return frame;
301 }
302
303 element = priv->free_frames.next;
304 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800305 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700306}
307
Tomas Winklerfcab4232008-05-15 13:54:01 +0800308static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700309{
310 memset(frame, 0, sizeof(*frame));
311 list_add(&frame->list, &priv->free_frames);
312}
313
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800314static u32 iwl_fill_beacon_frame(struct iwl_priv *priv,
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800315 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200316 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700317{
Tomas Winkler3109ece2008-03-28 16:33:35 -0700318 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200319 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
320 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -0700321 return 0;
322
323 if (priv->ibss_beacon->len > left)
324 return 0;
325
326 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
327
328 return priv->ibss_beacon->len;
329}
330
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800331/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
332static void iwl_set_beacon_tim(struct iwl_priv *priv,
333 struct iwl_tx_beacon_cmd *tx_beacon_cmd,
334 u8 *beacon, u32 frame_size)
335{
336 u16 tim_idx;
337 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
338
339 /*
340 * The index is relative to frame start but we start looking at the
341 * variable-length part of the beacon.
342 */
343 tim_idx = mgmt->u.beacon.variable - beacon;
344
345 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
346 while ((tim_idx < (frame_size - 2)) &&
347 (beacon[tim_idx] != WLAN_EID_TIM))
348 tim_idx += beacon[tim_idx+1] + 2;
349
350 /* If TIM field was found, set variables */
351 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
352 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
353 tx_beacon_cmd->tim_size = beacon[tim_idx+1];
354 } else
355 IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
356}
357
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700358static unsigned int iwl_hw_get_beacon_cmd(struct iwl_priv *priv,
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800359 struct iwl_frame *frame)
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800360{
361 struct iwl_tx_beacon_cmd *tx_beacon_cmd;
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800362 u32 frame_size;
363 u32 rate_flags;
364 u32 rate;
365 /*
366 * We have to set up the TX command, the TX Beacon command, and the
367 * beacon contents.
368 */
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800369
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800370 /* Initialize memory */
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800371 tx_beacon_cmd = &frame->u.beacon;
372 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
373
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800374 /* Set up TX beacon contents */
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800375 frame_size = iwl_fill_beacon_frame(priv, tx_beacon_cmd->frame,
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800376 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800377 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
378 return 0;
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800379
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800380 /* Set up TX command fields */
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800381 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800382 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
383 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800384 tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800385 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
386
387 /* Set up TX beacon command fields */
388 iwl_set_beacon_tim(priv, tx_beacon_cmd, (u8 *)tx_beacon_cmd->frame,
389 frame_size);
390
391 /* Set up packet rate and flags */
392 rate = iwl_rate_get_lowest_plcp(priv);
393 priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant);
394 rate_flags = iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
395 if ((rate >= IWL_FIRST_CCK_RATE) && (rate <= IWL_LAST_CCK_RATE))
396 rate_flags |= RATE_MCS_CCK_MSK;
397 tx_beacon_cmd->tx.rate_n_flags = iwl_hw_set_rate_n_flags(rate,
398 rate_flags);
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800399
400 return sizeof(*tx_beacon_cmd) + frame_size;
401}
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700402static int iwl_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700403{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800404 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700405 unsigned int frame_size;
406 int rc;
Zhu Yib481de92007-09-25 17:54:57 -0700407
Tomas Winklerfcab4232008-05-15 13:54:01 +0800408 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700409 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800410 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700411 "command.\n");
412 return -ENOMEM;
413 }
414
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800415 frame_size = iwl_hw_get_beacon_cmd(priv, frame);
416 if (!frame_size) {
417 IWL_ERR(priv, "Error configuring the beacon command\n");
418 iwl_free_frame(priv, frame);
419 return -EINVAL;
420 }
Zhu Yib481de92007-09-25 17:54:57 -0700421
Tomas Winkler857485c2008-03-21 13:53:44 -0700422 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700423 &frame->u.cmd[0]);
424
Tomas Winklerfcab4232008-05-15 13:54:01 +0800425 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700426
427 return rc;
428}
429
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800430static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
431{
432 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
433
434 dma_addr_t addr = get_unaligned_le32(&tb->lo);
435 if (sizeof(dma_addr_t) > sizeof(u32))
436 addr |=
437 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
438
439 return addr;
440}
441
442static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
443{
444 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
445
446 return le16_to_cpu(tb->hi_n_len) >> 4;
447}
448
449static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
450 dma_addr_t addr, u16 len)
451{
452 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
453 u16 hi_n_len = len << 4;
454
455 put_unaligned_le32(addr, &tb->lo);
456 if (sizeof(dma_addr_t) > sizeof(u32))
457 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
458
459 tb->hi_n_len = cpu_to_le16(hi_n_len);
460
461 tfd->num_tbs = idx + 1;
462}
463
464static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
465{
466 return tfd->num_tbs & 0x1f;
467}
468
469/**
470 * iwl_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
471 * @priv - driver private data
472 * @txq - tx queue
473 *
474 * Does NOT advance any TFD circular buffer read/write indexes
475 * Does NOT free the TFD itself (which is within circular buffer)
476 */
477void iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
478{
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800479 struct iwl_tfd *tfd_tmp = (struct iwl_tfd *)txq->tfds;
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800480 struct iwl_tfd *tfd;
481 struct pci_dev *dev = priv->pci_dev;
482 int index = txq->q.read_ptr;
483 int i;
484 int num_tbs;
485
486 tfd = &tfd_tmp[index];
487
488 /* Sanity check on number of chunks */
489 num_tbs = iwl_tfd_get_num_tbs(tfd);
490
491 if (num_tbs >= IWL_NUM_OF_TBS) {
492 IWL_ERR(priv, "Too many chunks: %i\n", num_tbs);
493 /* @todo issue fatal error, it is quite serious situation */
494 return;
495 }
496
497 /* Unmap tx_cmd */
498 if (num_tbs)
499 pci_unmap_single(dev,
Johannes Bergc2acea82009-07-24 11:13:05 -0700500 pci_unmap_addr(&txq->meta[index], mapping),
501 pci_unmap_len(&txq->meta[index], len),
Fenghua Yu96891ce2009-02-18 15:54:33 -0800502 PCI_DMA_BIDIRECTIONAL);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800503
504 /* Unmap chunks, if any. */
505 for (i = 1; i < num_tbs; i++) {
506 pci_unmap_single(dev, iwl_tfd_tb_get_addr(tfd, i),
507 iwl_tfd_tb_get_len(tfd, i), PCI_DMA_TODEVICE);
508
509 if (txq->txb) {
510 dev_kfree_skb(txq->txb[txq->q.read_ptr].skb[i - 1]);
511 txq->txb[txq->q.read_ptr].skb[i - 1] = NULL;
512 }
513 }
514}
515
516int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv,
517 struct iwl_tx_queue *txq,
518 dma_addr_t addr, u16 len,
519 u8 reset, u8 pad)
520{
521 struct iwl_queue *q;
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800522 struct iwl_tfd *tfd, *tfd_tmp;
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800523 u32 num_tbs;
524
525 q = &txq->q;
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800526 tfd_tmp = (struct iwl_tfd *)txq->tfds;
527 tfd = &tfd_tmp[q->write_ptr];
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800528
529 if (reset)
530 memset(tfd, 0, sizeof(*tfd));
531
532 num_tbs = iwl_tfd_get_num_tbs(tfd);
533
534 /* Each TFD can point to a maximum 20 Tx buffers */
535 if (num_tbs >= IWL_NUM_OF_TBS) {
536 IWL_ERR(priv, "Error can not send more than %d chunks\n",
537 IWL_NUM_OF_TBS);
538 return -EINVAL;
539 }
540
541 BUG_ON(addr & ~DMA_BIT_MASK(36));
542 if (unlikely(addr & ~IWL_TX_DMA_MASK))
543 IWL_ERR(priv, "Unaligned address = %llx\n",
544 (unsigned long long)addr);
545
546 iwl_tfd_set_tb(tfd, num_tbs, addr, len);
547
548 return 0;
549}
550
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800551/*
552 * Tell nic where to find circular buffer of Tx Frame Descriptors for
553 * given Tx queue, and enable the DMA channel used for that queue.
554 *
555 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
556 * channels supported in hardware.
557 */
558int iwl_hw_tx_queue_init(struct iwl_priv *priv,
559 struct iwl_tx_queue *txq)
560{
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800561 int txq_id = txq->q.id;
562
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800563 /* Circular buffer (TFD queue in DRAM) physical base address */
564 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
565 txq->q.dma_addr >> 8);
566
Samuel Ortiza8e74e272009-01-23 13:45:14 -0800567 return 0;
568}
569
Zhu Yib481de92007-09-25 17:54:57 -0700570/******************************************************************************
571 *
572 * Generic RX handler implementations
573 *
574 ******************************************************************************/
Tomas Winkler885ba202008-05-29 16:34:55 +0800575static void iwl_rx_reply_alive(struct iwl_priv *priv,
576 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700577{
Zhu Yi2f301222009-10-09 17:19:45 +0800578 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler885ba202008-05-29 16:34:55 +0800579 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700580 struct delayed_work *pwork;
581
582 palive = &pkt->u.alive_frame;
583
Tomas Winklere1623442009-01-27 14:27:56 -0800584 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700585 "0x%01X 0x%01X\n",
586 palive->is_valid, palive->ver_type,
587 palive->ver_subtype);
588
589 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800590 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700591 memcpy(&priv->card_alive_init,
592 &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +0800593 sizeof(struct iwl_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700594 pwork = &priv->init_alive_start;
595 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800596 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700597 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +0800598 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700599 pwork = &priv->alive_start;
600 }
601
602 /* We delay the ALIVE response by 5ms to
603 * give the HW RF Kill time to activate... */
604 if (palive->is_valid == UCODE_VALID_OK)
605 queue_delayed_work(priv->workqueue, pwork,
606 msecs_to_jiffies(5));
607 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800608 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700609}
610
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700611static void iwl_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700612{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700613 struct iwl_priv *priv =
614 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700615 struct sk_buff *beacon;
616
617 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200618 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700619
620 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800621 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700622 return;
623 }
624
625 mutex_lock(&priv->mutex);
626 /* new beacon skb is allocated every time; dispose previous.*/
627 if (priv->ibss_beacon)
628 dev_kfree_skb(priv->ibss_beacon);
629
630 priv->ibss_beacon = beacon;
631 mutex_unlock(&priv->mutex);
632
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700633 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700634}
635
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800636/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700637 * iwl_bg_statistics_periodic - Timer callback to queue statistics
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800638 *
639 * This callback is provided in order to send a statistics request.
640 *
641 * This timer function is continually reset to execute within
642 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
643 * was received. We need to ensure we receive the statistics in order
644 * to update the temperature used for calibrating the TXPOWER.
645 */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700646static void iwl_bg_statistics_periodic(unsigned long data)
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800647{
648 struct iwl_priv *priv = (struct iwl_priv *)data;
649
650 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
651 return;
652
Mohamed Abbas61780ee2008-10-29 14:05:49 -0700653 /* dont send host command if rf-kill is on */
654 if (!iwl_is_ready_rf(priv))
655 return;
656
Wey-Yi Guyef8d5522009-11-13 11:56:28 -0800657 iwl_send_statistics_request(priv, CMD_ASYNC, false);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800658}
659
Wey-Yi Guya9e1cb62009-12-10 14:37:26 -0800660
661static void iwl_print_cont_event_trace(struct iwl_priv *priv, u32 base,
662 u32 start_idx, u32 num_events,
663 u32 mode)
664{
665 u32 i;
666 u32 ptr; /* SRAM byte address of log data */
667 u32 ev, time, data; /* event log data */
668 unsigned long reg_flags;
669
670 if (mode == 0)
671 ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
672 else
673 ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));
674
675 /* Make sure device is powered up for SRAM reads */
676 spin_lock_irqsave(&priv->reg_lock, reg_flags);
677 if (iwl_grab_nic_access(priv)) {
678 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
679 return;
680 }
681
682 /* Set starting address; reads will auto-increment */
683 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
684 rmb();
685
686 /*
687 * "time" is actually "data" for mode 0 (no timestamp).
688 * place event id # at far right for easier visual parsing.
689 */
690 for (i = 0; i < num_events; i++) {
691 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
692 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
693 if (mode == 0) {
694 trace_iwlwifi_dev_ucode_cont_event(priv,
695 0, time, ev);
696 } else {
697 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
698 trace_iwlwifi_dev_ucode_cont_event(priv,
699 time, data, ev);
700 }
701 }
702 /* Allow device to power down */
703 iwl_release_nic_access(priv);
704 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
705}
706
707void iwl_continuous_event_trace(struct iwl_priv *priv)
708{
709 u32 capacity; /* event log capacity in # entries */
710 u32 base; /* SRAM byte address of event log header */
711 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
712 u32 num_wraps; /* # times uCode wrapped to top of log */
713 u32 next_entry; /* index of next entry to be written by uCode */
714
715 if (priv->ucode_type == UCODE_INIT)
716 base = le32_to_cpu(priv->card_alive_init.error_event_table_ptr);
717 else
718 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
719 if (priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
720 capacity = iwl_read_targ_mem(priv, base);
721 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
722 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
723 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
724 } else
725 return;
726
727 if (num_wraps == priv->event_log.num_wraps) {
728 iwl_print_cont_event_trace(priv,
729 base, priv->event_log.next_entry,
730 next_entry - priv->event_log.next_entry,
731 mode);
732 priv->event_log.non_wraps_count++;
733 } else {
734 if ((num_wraps - priv->event_log.num_wraps) > 1)
735 priv->event_log.wraps_more_count++;
736 else
737 priv->event_log.wraps_once_count++;
738 trace_iwlwifi_dev_ucode_wrap_event(priv,
739 num_wraps - priv->event_log.num_wraps,
740 next_entry, priv->event_log.next_entry);
741 if (next_entry < priv->event_log.next_entry) {
742 iwl_print_cont_event_trace(priv, base,
743 priv->event_log.next_entry,
744 capacity - priv->event_log.next_entry,
745 mode);
746
747 iwl_print_cont_event_trace(priv, base, 0,
748 next_entry, mode);
749 } else {
750 iwl_print_cont_event_trace(priv, base,
751 next_entry, capacity - next_entry,
752 mode);
753
754 iwl_print_cont_event_trace(priv, base, 0,
755 next_entry, mode);
756 }
757 }
758 priv->event_log.num_wraps = num_wraps;
759 priv->event_log.next_entry = next_entry;
760}
761
762/**
763 * iwl_bg_ucode_trace - Timer callback to log ucode event
764 *
765 * The timer is continually set to execute every
766 * UCODE_TRACE_PERIOD milliseconds after the last timer expired
767 * this function is to perform continuous uCode event logging operation
768 * if enabled
769 */
770static void iwl_bg_ucode_trace(unsigned long data)
771{
772 struct iwl_priv *priv = (struct iwl_priv *)data;
773
774 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
775 return;
776
777 if (priv->event_log.ucode_trace) {
778 iwl_continuous_event_trace(priv);
779 /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
780 mod_timer(&priv->ucode_trace,
781 jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
782 }
783}
784
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700785static void iwl_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +0800786 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700787{
Tomas Winkler0a6857e2008-03-12 16:58:49 -0700788#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800789 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2aa6ab82008-12-11 10:33:40 -0800790 struct iwl4965_beacon_notif *beacon =
791 (struct iwl4965_beacon_notif *)pkt->u.raw;
Tomas Winklere7d326a2008-06-12 09:47:11 +0800792 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700793
Tomas Winklere1623442009-01-27 14:27:56 -0800794 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700795 "tsf %d %d rate %d\n",
Tomas Winkler25a65722008-06-12 09:47:07 +0800796 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
Zhu Yib481de92007-09-25 17:54:57 -0700797 beacon->beacon_notify_hdr.failure_frame,
798 le32_to_cpu(beacon->ibss_mgr_status),
799 le32_to_cpu(beacon->high_tsf),
800 le32_to_cpu(beacon->low_tsf), rate);
801#endif
802
Johannes Berg05c914f2008-09-11 00:01:58 +0200803 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700804 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
805 queue_work(priv->workqueue, &priv->beacon_update);
806}
807
Zhu Yib481de92007-09-25 17:54:57 -0700808/* Handle notification from uCode that card's power state is changing
809 * due to software, hardware, or critical temperature RFKILL */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700810static void iwl_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +0800811 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700812{
Zhu Yi2f301222009-10-09 17:19:45 +0800813 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700814 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
815 unsigned long status = priv->status;
816
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800817 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700818 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800819 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
820 (flags & CT_CARD_DISABLED) ?
821 "Reached" : "Not reached");
Zhu Yib481de92007-09-25 17:54:57 -0700822
823 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800824 CT_CARD_DISABLED)) {
Zhu Yib481de92007-09-25 17:54:57 -0700825
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700826 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700827 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
828
Mohamed Abbasa8b50a02009-05-22 11:01:47 -0700829 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
830 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Zhu Yib481de92007-09-25 17:54:57 -0700831
832 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700833 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -0700834 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -0700835 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
Zhu Yib481de92007-09-25 17:54:57 -0700836 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Zhu Yib481de92007-09-25 17:54:57 -0700837 }
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800838 if (flags & CT_CARD_DISABLED)
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700839 iwl_tt_enter_ct_kill(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700840 }
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800841 if (!(flags & CT_CARD_DISABLED))
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700842 iwl_tt_exit_ct_kill(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700843
844 if (flags & HW_CARD_DISABLED)
845 set_bit(STATUS_RF_KILL_HW, &priv->status);
846 else
847 clear_bit(STATUS_RF_KILL_HW, &priv->status);
848
849
Zhu Yib481de92007-09-25 17:54:57 -0700850 if (!(flags & RXON_CARD_DISABLED))
Tomas Winkler2a421b92008-06-12 09:47:10 +0800851 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700852
853 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200854 test_bit(STATUS_RF_KILL_HW, &priv->status)))
855 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
856 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700857 else
858 wake_up_interruptible(&priv->wait_command_queue);
859}
860
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700861int iwl_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
Tomas Winklere2e3c572008-07-18 13:53:04 +0800862{
Tomas Winklere2e3c572008-07-18 13:53:04 +0800863 if (src == IWL_PWR_SRC_VAUX) {
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800864 if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
Tomas Winklere2e3c572008-07-18 13:53:04 +0800865 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
866 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
867 ~APMG_PS_CTRL_MSK_PWR_SRC);
868 } else {
869 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
870 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
871 ~APMG_PS_CTRL_MSK_PWR_SRC);
872 }
873
Mohamed Abbasa8b50a02009-05-22 11:01:47 -0700874 return 0;
Tomas Winklere2e3c572008-07-18 13:53:04 +0800875}
876
Zhu Yib481de92007-09-25 17:54:57 -0700877/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700878 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700879 *
880 * Setup the RX handlers for each of the reply types sent from the uCode
881 * to the host.
882 *
883 * This function chains into the hardware specific files for them to setup
884 * any hardware specific handlers as well.
885 */
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +0800886static void iwl_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700887{
Tomas Winkler885ba202008-05-29 16:34:55 +0800888 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700889 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
890 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700891 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700892 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700893 iwl_rx_pm_debug_statistics_notif;
894 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700895
Ben Cahill9fbab512007-11-29 11:09:47 +0800896 /*
897 * The same handler is used for both the REPLY to a discrete
898 * statistics request from the host as well as for the periodic
899 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700900 */
Wey-Yi Guyef8d5522009-11-13 11:56:28 -0800901 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_reply_statistics;
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +0800902 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800903
Tomas Winkler21c339b2008-11-07 09:58:41 -0800904 iwl_setup_spectrum_handlers(priv);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800905 iwl_setup_rx_scan_handlers(priv);
906
Ron Rindjunsky37a44212008-05-29 16:35:18 +0800907 /* status change handler */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700908 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700909
Tomas Winklerc1354752008-05-29 16:35:04 +0800910 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
911 iwl_rx_missed_beacon_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +0800912 /* Rx handlers */
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800913 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
914 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +0800915 /* block ack */
916 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl_rx_reply_compressed_ba;
Ben Cahill9fbab512007-11-29 11:09:47 +0800917 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -0700918 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700919}
920
Zhu Yib481de92007-09-25 17:54:57 -0700921/**
Tomas Winklera55360e2008-05-05 10:22:28 +0800922 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -0700923 *
924 * Uses the priv->rx_handlers callback function array to invoke
925 * the appropriate handlers, including command responses,
926 * frame-received notifications, and other notifications.
927 */
Tomas Winklera55360e2008-05-05 10:22:28 +0800928void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700929{
Tomas Winklera55360e2008-05-05 10:22:28 +0800930 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +0800931 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +0800932 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -0700933 u32 r, i;
934 int reclaim;
935 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +0800936 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -0800937 u32 count = 8;
Mohamed Abbas4752c932009-05-22 11:01:51 -0700938 int total_empty;
Zhu Yib481de92007-09-25 17:54:57 -0700939
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800940 /* uCode's read index (stored in shared DRAM) indicates the last Rx
941 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8d864222008-11-07 09:58:39 -0800942 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -0700943 i = rxq->read;
944
945 /* Rx interrupt, but nothing sent from uCode */
946 if (i == r)
Tomas Winklere1623442009-01-27 14:27:56 -0800947 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -0700948
Mohamed Abbas4752c932009-05-22 11:01:51 -0700949 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -0700950 total_empty = r - rxq->write_actual;
Mohamed Abbas4752c932009-05-22 11:01:51 -0700951 if (total_empty < 0)
952 total_empty += RX_QUEUE_SIZE;
953
954 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +0800955 fill_rx = 1;
956
Zhu Yib481de92007-09-25 17:54:57 -0700957 while (i != r) {
958 rxb = rxq->queue[i];
959
Ben Cahill9fbab512007-11-29 11:09:47 +0800960 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -0700961 * then a bug has been introduced in the queue refilling
962 * routines -- catch it here */
963 BUG_ON(rxb == NULL);
964
965 rxq->queue[i] = NULL;
966
Zhu Yi2f301222009-10-09 17:19:45 +0800967 pci_unmap_page(priv->pci_dev, rxb->page_dma,
968 PAGE_SIZE << priv->hw_params.rx_page_order,
969 PCI_DMA_FROMDEVICE);
970 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700971
Johannes Bergbe1a71a2009-10-02 13:44:02 -0700972 trace_iwlwifi_dev_rx(priv, pkt,
973 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK);
974
Zhu Yib481de92007-09-25 17:54:57 -0700975 /* Reclaim a command buffer only if this packet is a response
976 * to a (driver-originated) command.
977 * If the packet (e.g. Rx frame) originated from uCode,
978 * there is no command buffer to reclaim.
979 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
980 * but apparently a few don't get set; catch them here. */
981 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
982 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -0700983 (pkt->hdr.cmd != REPLY_RX) &&
Daniel Halperin7dddaf12008-10-23 23:48:58 -0700984 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
Zhu Yicfe01702007-09-27 11:27:31 +0800985 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -0700986 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
987 (pkt->hdr.cmd != REPLY_TX);
988
989 /* Based on type of command response or notification,
990 * handle those that need handling via function in
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700991 * rx_handlers table. See iwl_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -0700992 if (priv->rx_handlers[pkt->hdr.cmd]) {
Tomas Winklere1623442009-01-27 14:27:56 -0800993 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r,
Ester Kummerf3d67992008-05-06 11:05:12 +0800994 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Wey-Yi Guya83b9142009-04-08 11:39:32 -0700995 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -0700996 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700997 } else {
998 /* No handling needed */
Tomas Winklere1623442009-01-27 14:27:56 -0800999 IWL_DEBUG_RX(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001000 "r %d i %d No handler needed for %s, 0x%02x\n",
1001 r, i, get_cmd_string(pkt->hdr.cmd),
1002 pkt->hdr.cmd);
1003 }
1004
Zhu Yi29b1b262009-10-23 13:42:25 -07001005 /*
1006 * XXX: After here, we should always check rxb->page
1007 * against NULL before touching it or its virtual
1008 * memory (pkt). Because some rx_handler might have
1009 * already taken or freed the pages.
1010 */
1011
Zhu Yib481de92007-09-25 17:54:57 -07001012 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001013 /* Invoke any callbacks, transfer the buffer to caller,
1014 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001015 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001016 if (rxb->page)
Tomas Winkler17b88922008-05-29 16:35:12 +08001017 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001018 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001019 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001020 }
1021
Zhu Yi73005152009-10-23 13:42:32 -07001022 /* Reuse the page if possible. For notification packets and
1023 * SKBs that fail to Rx correctly, add them back into the
1024 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001025 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001026 if (rxb->page != NULL) {
1027 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1028 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1029 PCI_DMA_FROMDEVICE);
1030 list_add_tail(&rxb->list, &rxq->rx_free);
1031 rxq->free_count++;
1032 } else
1033 list_add_tail(&rxb->list, &rxq->rx_used);
1034
Zhu Yib481de92007-09-25 17:54:57 -07001035 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001036
Zhu Yib481de92007-09-25 17:54:57 -07001037 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001038 /* If there are a lot of unused frames,
1039 * restock the Rx queue so ucode wont assert. */
1040 if (fill_rx) {
1041 count++;
1042 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001043 rxq->read = i;
Mohamed Abbas4752c932009-05-22 11:01:51 -07001044 iwl_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001045 count = 0;
1046 }
1047 }
Zhu Yib481de92007-09-25 17:54:57 -07001048 }
1049
1050 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001051 rxq->read = i;
Mohamed Abbas4752c932009-05-22 11:01:51 -07001052 if (fill_rx)
1053 iwl_rx_replenish_now(priv);
1054 else
1055 iwl_rx_queue_restock(priv);
Tomas Winklera55360e2008-05-05 10:22:28 +08001056}
Tomas Winklera55360e2008-05-05 10:22:28 +08001057
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001058/* call this function to flush any scheduled tasklet */
1059static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1060{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001061 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001062 synchronize_irq(priv->pci_dev->irq);
1063 tasklet_kill(&priv->irq_tasklet);
1064}
1065
Mohamed Abbasef850d72009-05-22 11:01:50 -07001066static void iwl_irq_tasklet_legacy(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001067{
1068 u32 inta, handled = 0;
1069 u32 inta_fh;
1070 unsigned long flags;
Ben Cahillc2e61da2009-10-30 14:36:09 -07001071 u32 i;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001072#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001073 u32 inta_mask;
1074#endif
1075
1076 spin_lock_irqsave(&priv->lock, flags);
1077
1078 /* Ack/clear/reset pending uCode interrupts.
1079 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1080 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001081 inta = iwl_read32(priv, CSR_INT);
1082 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001083
1084 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1085 * Any new interrupts that happen after this, either while we're
1086 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001087 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1088 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001089
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001090#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001091 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001092 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001093 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001094 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001095 inta, inta_mask, inta_fh);
1096 }
1097#endif
1098
Zhu Yi2f301222009-10-09 17:19:45 +08001099 spin_unlock_irqrestore(&priv->lock, flags);
1100
Zhu Yib481de92007-09-25 17:54:57 -07001101 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1102 * atomic, make sure that inta covers all the interrupts that
1103 * we've discovered, even if FH interrupt came in just after
1104 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001105 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001106 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001107 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001108 inta |= CSR_INT_BIT_FH_TX;
1109
1110 /* Now service all interrupt bits discovered above. */
1111 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001112 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001113
1114 /* Tell the device to stop sending interrupts */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001115 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001116
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001117 priv->isr_stats.hw++;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001118 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001119
1120 handled |= CSR_INT_BIT_HW_ERR;
1121
Zhu Yib481de92007-09-25 17:54:57 -07001122 return;
1123 }
1124
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001125#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001126 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001127 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001128 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001129 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001130 "the frame/frames.\n");
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001131 priv->isr_stats.sch++;
1132 }
Zhu Yib481de92007-09-25 17:54:57 -07001133
1134 /* Alive notification via Rx interrupt will do the real work */
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001135 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001136 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001137 priv->isr_stats.alive++;
1138 }
Zhu Yib481de92007-09-25 17:54:57 -07001139 }
1140#endif
1141 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001142 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001143
Ben Cahill9fbab512007-11-29 11:09:47 +08001144 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07001145 if (inta & CSR_INT_BIT_RF_KILL) {
1146 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001147 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07001148 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1149 hw_rf_kill = 1;
1150
Reinette Chatre4c423a22009-07-17 09:30:26 -07001151 IWL_WARN(priv, "RF_KILL bit toggled to %s.\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08001152 hw_rf_kill ? "disable radio" : "enable radio");
Zhu Yib481de92007-09-25 17:54:57 -07001153
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001154 priv->isr_stats.rfkill++;
1155
Emmanuel Grumbacha9efa652008-06-30 17:23:25 +08001156 /* driver only loads ucode once setting the interface up.
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01001157 * the driver allows loading the ucode even if the radio
1158 * is killed. Hence update the killswitch state here. The
1159 * rfkill handler will care about restarting if needed.
Emmanuel Grumbacha9efa652008-06-30 17:23:25 +08001160 */
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01001161 if (!test_bit(STATUS_ALIVE, &priv->status)) {
1162 if (hw_rf_kill)
1163 set_bit(STATUS_RF_KILL_HW, &priv->status);
1164 else
1165 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Johannes Berga60e77e2009-06-04 18:26:06 +02001166 wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rf_kill);
Mohamed Abbasedb34222008-12-11 10:33:37 -08001167 }
Zhu Yib481de92007-09-25 17:54:57 -07001168
1169 handled |= CSR_INT_BIT_RF_KILL;
1170 }
1171
Ben Cahill9fbab512007-11-29 11:09:47 +08001172 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07001173 if (inta & CSR_INT_BIT_CT_KILL) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001174 IWL_ERR(priv, "Microcode CT kill error detected.\n");
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001175 priv->isr_stats.ctkill++;
Zhu Yib481de92007-09-25 17:54:57 -07001176 handled |= CSR_INT_BIT_CT_KILL;
1177 }
1178
1179 /* Error detected by uCode */
1180 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001181 IWL_ERR(priv, "Microcode SW error detected. "
1182 " Restarting 0x%X.\n", inta);
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001183 priv->isr_stats.sw++;
1184 priv->isr_stats.sw_err = inta;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001185 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001186 handled |= CSR_INT_BIT_SW_ERR;
1187 }
1188
Ben Cahillc2e61da2009-10-30 14:36:09 -07001189 /*
1190 * uCode wakes up after power-down sleep.
1191 * Tell device about any new tx or host commands enqueued,
1192 * and about any Rx buffers made available while asleep.
1193 */
Zhu Yib481de92007-09-25 17:54:57 -07001194 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001195 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08001196 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Ben Cahillc2e61da2009-10-30 14:36:09 -07001197 for (i = 0; i < priv->hw_params.max_txq_num; i++)
1198 iwl_txq_update_write_ptr(priv, &priv->txq[i]);
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001199 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001200 handled |= CSR_INT_BIT_WAKEUP;
1201 }
1202
1203 /* All uCode command responses, including Tx command responses,
1204 * Rx "responses" (frame-received notification), and other
1205 * notifications from uCode come through here*/
1206 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08001207 iwl_rx_handle(priv);
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001208 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001209 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1210 }
1211
Ben Cahillc72cd192009-10-30 14:36:08 -07001212 /* This "Tx" DMA channel is used only for loading uCode */
Zhu Yib481de92007-09-25 17:54:57 -07001213 if (inta & CSR_INT_BIT_FH_TX) {
Ben Cahillc72cd192009-10-30 14:36:08 -07001214 IWL_DEBUG_ISR(priv, "uCode load interrupt\n");
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001215 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001216 handled |= CSR_INT_BIT_FH_TX;
Ben Cahillc72cd192009-10-30 14:36:08 -07001217 /* Wake up uCode load routine, now that load is complete */
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08001218 priv->ucode_write_complete = 1;
1219 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07001220 }
1221
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001222 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001223 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001224 priv->isr_stats.unhandled++;
1225 }
Zhu Yib481de92007-09-25 17:54:57 -07001226
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001227 if (inta & ~(priv->inta_mask)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001228 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001229 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001230 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001231 }
1232
1233 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001234 /* only Re-enable if diabled by irq */
1235 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001236 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001237
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001238#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001239 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001240 inta = iwl_read32(priv, CSR_INT);
1241 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1242 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001243 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001244 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1245 }
1246#endif
Zhu Yib481de92007-09-25 17:54:57 -07001247}
1248
Mohamed Abbasef850d72009-05-22 11:01:50 -07001249/* tasklet for iwlagn interrupt */
1250static void iwl_irq_tasklet(struct iwl_priv *priv)
1251{
1252 u32 inta = 0;
1253 u32 handled = 0;
1254 unsigned long flags;
Ben Cahill87569902009-11-06 14:53:01 -08001255 u32 i;
Mohamed Abbasef850d72009-05-22 11:01:50 -07001256#ifdef CONFIG_IWLWIFI_DEBUG
1257 u32 inta_mask;
1258#endif
1259
1260 spin_lock_irqsave(&priv->lock, flags);
1261
1262 /* Ack/clear/reset pending uCode interrupts.
1263 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1264 */
1265 iwl_write32(priv, CSR_INT, priv->inta);
1266
1267 inta = priv->inta;
1268
1269#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001270 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Mohamed Abbasef850d72009-05-22 11:01:50 -07001271 /* just for debug */
1272 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1273 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x\n ",
1274 inta, inta_mask);
1275 }
1276#endif
Zhu Yi2f301222009-10-09 17:19:45 +08001277
1278 spin_unlock_irqrestore(&priv->lock, flags);
1279
Mohamed Abbasef850d72009-05-22 11:01:50 -07001280 /* saved interrupt in inta variable now we can reset priv->inta */
1281 priv->inta = 0;
1282
1283 /* Now service all interrupt bits discovered above. */
1284 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001285 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Mohamed Abbasef850d72009-05-22 11:01:50 -07001286
1287 /* Tell the device to stop sending interrupts */
1288 iwl_disable_interrupts(priv);
1289
1290 priv->isr_stats.hw++;
1291 iwl_irq_handle_error(priv);
1292
1293 handled |= CSR_INT_BIT_HW_ERR;
1294
Mohamed Abbasef850d72009-05-22 11:01:50 -07001295 return;
1296 }
1297
1298#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001299 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Mohamed Abbasef850d72009-05-22 11:01:50 -07001300 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1301 if (inta & CSR_INT_BIT_SCD) {
1302 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
1303 "the frame/frames.\n");
1304 priv->isr_stats.sch++;
1305 }
1306
1307 /* Alive notification via Rx interrupt will do the real work */
1308 if (inta & CSR_INT_BIT_ALIVE) {
1309 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
1310 priv->isr_stats.alive++;
1311 }
1312 }
1313#endif
1314 /* Safely ignore these bits for debug checks below */
1315 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1316
1317 /* HW RF KILL switch toggled */
1318 if (inta & CSR_INT_BIT_RF_KILL) {
1319 int hw_rf_kill = 0;
1320 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
1321 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1322 hw_rf_kill = 1;
1323
Reinette Chatre4c423a22009-07-17 09:30:26 -07001324 IWL_WARN(priv, "RF_KILL bit toggled to %s.\n",
Mohamed Abbasef850d72009-05-22 11:01:50 -07001325 hw_rf_kill ? "disable radio" : "enable radio");
1326
1327 priv->isr_stats.rfkill++;
1328
1329 /* driver only loads ucode once setting the interface up.
1330 * the driver allows loading the ucode even if the radio
1331 * is killed. Hence update the killswitch state here. The
1332 * rfkill handler will care about restarting if needed.
1333 */
1334 if (!test_bit(STATUS_ALIVE, &priv->status)) {
1335 if (hw_rf_kill)
1336 set_bit(STATUS_RF_KILL_HW, &priv->status);
1337 else
1338 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Johannes Berga60e77e2009-06-04 18:26:06 +02001339 wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rf_kill);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001340 }
1341
1342 handled |= CSR_INT_BIT_RF_KILL;
1343 }
1344
1345 /* Chip got too hot and stopped itself */
1346 if (inta & CSR_INT_BIT_CT_KILL) {
1347 IWL_ERR(priv, "Microcode CT kill error detected.\n");
1348 priv->isr_stats.ctkill++;
1349 handled |= CSR_INT_BIT_CT_KILL;
1350 }
1351
1352 /* Error detected by uCode */
1353 if (inta & CSR_INT_BIT_SW_ERR) {
1354 IWL_ERR(priv, "Microcode SW error detected. "
1355 " Restarting 0x%X.\n", inta);
1356 priv->isr_stats.sw++;
1357 priv->isr_stats.sw_err = inta;
1358 iwl_irq_handle_error(priv);
1359 handled |= CSR_INT_BIT_SW_ERR;
1360 }
1361
1362 /* uCode wakes up after power-down sleep */
1363 if (inta & CSR_INT_BIT_WAKEUP) {
1364 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
1365 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Ben Cahill87569902009-11-06 14:53:01 -08001366 for (i = 0; i < priv->hw_params.max_txq_num; i++)
1367 iwl_txq_update_write_ptr(priv, &priv->txq[i]);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001368
1369 priv->isr_stats.wakeup++;
1370
1371 handled |= CSR_INT_BIT_WAKEUP;
1372 }
1373
1374 /* All uCode command responses, including Tx command responses,
1375 * Rx "responses" (frame-received notification), and other
1376 * notifications from uCode come through here*/
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001377 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1378 CSR_INT_BIT_RX_PERIODIC)) {
Mohamed Abbasef850d72009-05-22 11:01:50 -07001379 IWL_DEBUG_ISR(priv, "Rx interrupt\n");
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001380 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1381 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1382 iwl_write32(priv, CSR_FH_INT_STATUS,
1383 CSR49_FH_INT_RX_MASK);
1384 }
1385 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1386 handled |= CSR_INT_BIT_RX_PERIODIC;
1387 iwl_write32(priv, CSR_INT, CSR_INT_BIT_RX_PERIODIC);
1388 }
1389 /* Sending RX interrupt require many steps to be done in the
1390 * the device:
1391 * 1- write interrupt to current index in ICT table.
1392 * 2- dma RX frame.
1393 * 3- update RX shared data to indicate last write index.
1394 * 4- send interrupt.
1395 * This could lead to RX race, driver could receive RX interrupt
Ben Cahill74ba67e2009-11-20 12:04:53 -08001396 * but the shared data changes does not reflect this;
1397 * periodic interrupt will detect any dangling Rx activity.
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001398 */
Ben Cahill74ba67e2009-11-20 12:04:53 -08001399
1400 /* Disable periodic interrupt; we use it as just a one-shot. */
1401 iwl_write8(priv, CSR_INT_PERIODIC_REG,
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001402 CSR_INT_PERIODIC_DIS);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001403 iwl_rx_handle(priv);
Ben Cahill74ba67e2009-11-20 12:04:53 -08001404
1405 /*
1406 * Enable periodic interrupt in 8 msec only if we received
1407 * real RX interrupt (instead of just periodic int), to catch
1408 * any dangling Rx interrupt. If it was just the periodic
1409 * interrupt, there was no dangling Rx activity, and no need
1410 * to extend the periodic interrupt; one-shot is enough.
1411 */
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001412 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
Ben Cahill74ba67e2009-11-20 12:04:53 -08001413 iwl_write8(priv, CSR_INT_PERIODIC_REG,
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001414 CSR_INT_PERIODIC_ENA);
1415
Mohamed Abbasef850d72009-05-22 11:01:50 -07001416 priv->isr_stats.rx++;
Mohamed Abbasef850d72009-05-22 11:01:50 -07001417 }
1418
Ben Cahillc72cd192009-10-30 14:36:08 -07001419 /* This "Tx" DMA channel is used only for loading uCode */
Mohamed Abbasef850d72009-05-22 11:01:50 -07001420 if (inta & CSR_INT_BIT_FH_TX) {
1421 iwl_write32(priv, CSR_FH_INT_STATUS, CSR49_FH_INT_TX_MASK);
Ben Cahillc72cd192009-10-30 14:36:08 -07001422 IWL_DEBUG_ISR(priv, "uCode load interrupt\n");
Mohamed Abbasef850d72009-05-22 11:01:50 -07001423 priv->isr_stats.tx++;
1424 handled |= CSR_INT_BIT_FH_TX;
Ben Cahillc72cd192009-10-30 14:36:08 -07001425 /* Wake up uCode load routine, now that load is complete */
Mohamed Abbasef850d72009-05-22 11:01:50 -07001426 priv->ucode_write_complete = 1;
1427 wake_up_interruptible(&priv->wait_command_queue);
1428 }
1429
1430 if (inta & ~handled) {
1431 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
1432 priv->isr_stats.unhandled++;
1433 }
1434
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001435 if (inta & ~(priv->inta_mask)) {
Mohamed Abbasef850d72009-05-22 11:01:50 -07001436 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001437 inta & ~priv->inta_mask);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001438 }
1439
Mohamed Abbasef850d72009-05-22 11:01:50 -07001440 /* Re-enable all interrupts */
1441 /* only Re-enable if diabled by irq */
1442 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1443 iwl_enable_interrupts(priv);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001444}
1445
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001446
Zhu Yib481de92007-09-25 17:54:57 -07001447/******************************************************************************
1448 *
1449 * uCode download functions
1450 *
1451 ******************************************************************************/
1452
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001453static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001454{
Tomas Winkler98c92212008-01-14 17:46:20 -08001455 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1456 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1457 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1458 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1459 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1460 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001461}
1462
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001463static void iwl_nic_start(struct iwl_priv *priv)
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08001464{
1465 /* Remove all resets to allow NIC to operate */
1466 iwl_write32(priv, CSR_RESET, 0);
1467}
1468
1469
Zhu Yib481de92007-09-25 17:54:57 -07001470/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001471 * iwl_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07001472 *
1473 * Copy into buffers for card to fetch via bus-mastering
1474 */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001475static int iwl_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001476{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001477 struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08001478 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07001479 const struct firmware *ucode_raw;
Reinette Chatrea0987a82008-12-02 12:14:06 -08001480 const char *name_pre = priv->cfg->fw_name_pre;
1481 const unsigned int api_max = priv->cfg->ucode_api_max;
1482 const unsigned int api_min = priv->cfg->ucode_api_min;
1483 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07001484 u8 *src;
1485 size_t len;
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001486 u32 api_ver, build;
1487 u32 inst_size, data_size, init_size, init_data_size, boot_size;
Jay Sternbergabdc2d62009-07-31 14:28:09 -07001488 u16 eeprom_ver;
Zhu Yib481de92007-09-25 17:54:57 -07001489
1490 /* Ask kernel firmware_class module to get the boot firmware off disk.
1491 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08001492 for (index = api_max; index >= api_min; index--) {
1493 sprintf(buf, "%s%d%s", name_pre, index, ".ucode");
1494 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
1495 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001496 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001497 buf, ret);
1498 if (ret == -ENOENT)
1499 continue;
1500 else
1501 goto error;
1502 } else {
1503 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08001504 IWL_ERR(priv, "Loaded firmware %s, "
1505 "which is deprecated. "
1506 "Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001507 buf, api_max);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001508
Tomas Winklere1623442009-01-27 14:27:56 -08001509 IWL_DEBUG_INFO(priv, "Got firmware '%s' file (%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001510 buf, ucode_raw->size);
1511 break;
1512 }
Zhu Yib481de92007-09-25 17:54:57 -07001513 }
1514
Reinette Chatrea0987a82008-12-02 12:14:06 -08001515 if (ret < 0)
1516 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07001517
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001518 /* Make sure that we got at least the v1 header! */
1519 if (ucode_raw->size < priv->cfg->ops->ucode->get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001520 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08001521 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001522 goto err_release;
1523 }
1524
1525 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001526 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07001527
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08001528 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08001529 api_ver = IWL_UCODE_API(priv->ucode_ver);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001530 build = priv->cfg->ops->ucode->get_build(ucode, api_ver);
1531 inst_size = priv->cfg->ops->ucode->get_inst_size(ucode, api_ver);
1532 data_size = priv->cfg->ops->ucode->get_data_size(ucode, api_ver);
1533 init_size = priv->cfg->ops->ucode->get_init_size(ucode, api_ver);
1534 init_data_size =
1535 priv->cfg->ops->ucode->get_init_data_size(ucode, api_ver);
1536 boot_size = priv->cfg->ops->ucode->get_boot_size(ucode, api_ver);
1537 src = priv->cfg->ops->ucode->get_data(ucode, api_ver);
Zhu Yib481de92007-09-25 17:54:57 -07001538
Reinette Chatrea0987a82008-12-02 12:14:06 -08001539 /* api_ver should match the api version forming part of the
1540 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01001541 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08001542
1543 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001544 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08001545 "Driver supports v%u, firmware is v%u.\n",
1546 api_max, api_ver);
1547 priv->ucode_ver = 0;
1548 ret = -EINVAL;
1549 goto err_release;
1550 }
1551 if (api_ver != api_max)
Tomas Winkler978785a2008-12-19 10:37:31 +08001552 IWL_ERR(priv, "Firmware has old API version. Expected v%u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08001553 "got v%u. New firmware can be obtained "
1554 "from http://www.intellinuxwireless.org.\n",
1555 api_max, api_ver);
1556
Tomas Winkler978785a2008-12-19 10:37:31 +08001557 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
1558 IWL_UCODE_MAJOR(priv->ucode_ver),
1559 IWL_UCODE_MINOR(priv->ucode_ver),
1560 IWL_UCODE_API(priv->ucode_ver),
1561 IWL_UCODE_SERIAL(priv->ucode_ver));
Reinette Chatrea0987a82008-12-02 12:14:06 -08001562
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07001563 snprintf(priv->hw->wiphy->fw_version,
1564 sizeof(priv->hw->wiphy->fw_version),
1565 "%u.%u.%u.%u",
1566 IWL_UCODE_MAJOR(priv->ucode_ver),
1567 IWL_UCODE_MINOR(priv->ucode_ver),
1568 IWL_UCODE_API(priv->ucode_ver),
1569 IWL_UCODE_SERIAL(priv->ucode_ver));
1570
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001571 if (build)
1572 IWL_DEBUG_INFO(priv, "Build %u\n", build);
1573
Jay Sternbergabdc2d62009-07-31 14:28:09 -07001574 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
1575 IWL_DEBUG_INFO(priv, "NVM Type: %s, version: 0x%x\n",
1576 (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
1577 ? "OTP" : "EEPROM", eeprom_ver);
1578
Tomas Winklere1623442009-01-27 14:27:56 -08001579 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001580 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08001581 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001582 inst_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001583 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001584 data_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001585 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001586 init_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001587 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001588 init_data_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001589 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001590 boot_size);
1591
1592 /* Verify size of file vs. image size info in file's header */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001593 if (ucode_raw->size !=
1594 priv->cfg->ops->ucode->get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07001595 inst_size + data_size + init_size +
1596 init_data_size + boot_size) {
1597
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001598 IWL_DEBUG_INFO(priv,
1599 "uCode file size %d does not match expected size\n",
1600 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001601 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001602 goto err_release;
1603 }
1604
1605 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001606 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001607 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001608 inst_size);
1609 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001610 goto err_release;
1611 }
1612
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001613 if (data_size > priv->hw_params.max_data_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001614 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001615 data_size);
1616 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001617 goto err_release;
1618 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001619 if (init_size > priv->hw_params.max_inst_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001620 IWL_INFO(priv, "uCode init instr len %d too large to fit in\n",
1621 init_size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001622 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001623 goto err_release;
1624 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001625 if (init_data_size > priv->hw_params.max_data_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001626 IWL_INFO(priv, "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001627 init_data_size);
1628 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001629 goto err_release;
1630 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001631 if (boot_size > priv->hw_params.max_bsm_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001632 IWL_INFO(priv, "uCode boot instr len %d too large to fit in\n",
1633 boot_size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001634 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001635 goto err_release;
1636 }
1637
1638 /* Allocate ucode buffers for card's bus-master loading ... */
1639
1640 /* Runtime instructions and 2 copies of data:
1641 * 1) unmodified from disk
1642 * 2) backup cache for save/restore during power-downs */
1643 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001644 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07001645
1646 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001647 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07001648
1649 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001650 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07001651
Zhu, Yi1f304e42009-01-23 13:45:22 -08001652 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
1653 !priv->ucode_data_backup.v_addr)
1654 goto err_pci_alloc;
1655
Zhu Yib481de92007-09-25 17:54:57 -07001656 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08001657 if (init_size && init_data_size) {
1658 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001659 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07001660
Tomas Winkler90e759d2007-11-29 11:09:41 +08001661 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001662 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001663
1664 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
1665 goto err_pci_alloc;
1666 }
Zhu Yib481de92007-09-25 17:54:57 -07001667
1668 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08001669 if (boot_size) {
1670 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001671 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001672
Tomas Winkler90e759d2007-11-29 11:09:41 +08001673 if (!priv->ucode_boot.v_addr)
1674 goto err_pci_alloc;
1675 }
Zhu Yib481de92007-09-25 17:54:57 -07001676
1677 /* Copy images into buffers for card's bus-master reads ... */
1678
1679 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001680 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001681 IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001682 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001683 src += len;
1684
Tomas Winklere1623442009-01-27 14:27:56 -08001685 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001686 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
1687
1688 /* Runtime data (2nd block)
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001689 * NOTE: Copy into backup buffer will be done in iwl_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001690 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001691 IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001692 memcpy(priv->ucode_data.v_addr, src, len);
1693 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001694 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07001695
1696 /* Initialization instructions (3rd block) */
1697 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001698 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001699 IWL_DEBUG_INFO(priv, "Copying (but not loading) init instr len %Zd\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001700 len);
Zhu Yib481de92007-09-25 17:54:57 -07001701 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001702 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07001703 }
1704
1705 /* Initialization data (4th block) */
1706 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001707 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001708 IWL_DEBUG_INFO(priv, "Copying (but not loading) init data len %Zd\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001709 len);
Zhu Yib481de92007-09-25 17:54:57 -07001710 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001711 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07001712 }
1713
1714 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001715 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001716 IWL_DEBUG_INFO(priv, "Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001717 memcpy(priv->ucode_boot.v_addr, src, len);
1718
1719 /* We have our copies now, allow OS release its copies */
1720 release_firmware(ucode_raw);
1721 return 0;
1722
1723 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001724 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08001725 ret = -ENOMEM;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001726 iwl_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001727
1728 err_release:
1729 release_firmware(ucode_raw);
1730
1731 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08001732 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001733}
1734
Reinette Chatreb7a79402009-09-25 14:24:23 -07001735static const char *desc_lookup_text[] = {
1736 "OK",
1737 "FAIL",
1738 "BAD_PARAM",
1739 "BAD_CHECKSUM",
1740 "NMI_INTERRUPT_WDG",
1741 "SYSASSERT",
1742 "FATAL_ERROR",
1743 "BAD_COMMAND",
1744 "HW_ERROR_TUNE_LOCK",
1745 "HW_ERROR_TEMPERATURE",
1746 "ILLEGAL_CHAN_FREQ",
1747 "VCC_NOT_STABLE",
1748 "FH_ERROR",
1749 "NMI_INTERRUPT_HOST",
1750 "NMI_INTERRUPT_ACTION_PT",
1751 "NMI_INTERRUPT_UNKNOWN",
1752 "UCODE_VERSION_MISMATCH",
1753 "HW_ERROR_ABS_LOCK",
1754 "HW_ERROR_CAL_LOCK_FAIL",
1755 "NMI_INTERRUPT_INST_ACTION_PT",
1756 "NMI_INTERRUPT_DATA_ACTION_PT",
1757 "NMI_TRM_HW_ER",
1758 "NMI_INTERRUPT_TRM",
1759 "NMI_INTERRUPT_BREAK_POINT"
1760 "DEBUG_0",
1761 "DEBUG_1",
1762 "DEBUG_2",
1763 "DEBUG_3",
1764 "UNKNOWN"
1765};
1766
1767static const char *desc_lookup(int i)
1768{
1769 int max = ARRAY_SIZE(desc_lookup_text) - 1;
1770
1771 if (i < 0 || i > max)
1772 i = max;
1773
1774 return desc_lookup_text[i];
1775}
1776
1777#define ERROR_START_OFFSET (1 * sizeof(u32))
1778#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1779
1780void iwl_dump_nic_error_log(struct iwl_priv *priv)
1781{
1782 u32 data2, line;
1783 u32 desc, time, count, base, data1;
1784 u32 blink1, blink2, ilink1, ilink2;
1785
1786 if (priv->ucode_type == UCODE_INIT)
1787 base = le32_to_cpu(priv->card_alive_init.error_event_table_ptr);
1788 else
1789 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1790
1791 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Wey-Yi Guy212fb572009-12-02 12:53:00 -08001792 IWL_ERR(priv,
1793 "Not valid error log pointer 0x%08X for %s uCode\n",
1794 base, (priv->ucode_type == UCODE_INIT) ? "Init" : "RT");
Reinette Chatreb7a79402009-09-25 14:24:23 -07001795 return;
1796 }
1797
1798 count = iwl_read_targ_mem(priv, base);
1799
1800 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
1801 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1802 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1803 priv->status, count);
1804 }
1805
1806 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
1807 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
1808 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
1809 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
1810 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
1811 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
1812 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
1813 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
1814 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
1815
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001816 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, data2, line,
1817 blink1, blink2, ilink1, ilink2);
1818
Reinette Chatreb7a79402009-09-25 14:24:23 -07001819 IWL_ERR(priv, "Desc Time "
1820 "data1 data2 line\n");
1821 IWL_ERR(priv, "%-28s (#%02d) %010u 0x%08X 0x%08X %u\n",
1822 desc_lookup(desc), desc, time, data1, data2, line);
1823 IWL_ERR(priv, "blink1 blink2 ilink1 ilink2\n");
1824 IWL_ERR(priv, "0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
1825 ilink1, ilink2);
1826
1827}
1828
1829#define EVENT_START_OFFSET (4 * sizeof(u32))
1830
1831/**
1832 * iwl_print_event_log - Dump error event log to syslog
1833 *
1834 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001835static int iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
1836 u32 num_events, u32 mode,
1837 int pos, char **buf, size_t bufsz)
Reinette Chatreb7a79402009-09-25 14:24:23 -07001838{
1839 u32 i;
1840 u32 base; /* SRAM byte address of event log header */
1841 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1842 u32 ptr; /* SRAM byte address of log data */
1843 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001844 unsigned long reg_flags;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001845
1846 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001847 return pos;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001848 if (priv->ucode_type == UCODE_INIT)
1849 base = le32_to_cpu(priv->card_alive_init.log_event_table_ptr);
1850 else
1851 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1852
1853 if (mode == 0)
1854 event_size = 2 * sizeof(u32);
1855 else
1856 event_size = 3 * sizeof(u32);
1857
1858 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1859
Ben Cahille5854472009-11-06 14:52:58 -08001860 /* Make sure device is powered up for SRAM reads */
1861 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1862 iwl_grab_nic_access(priv);
1863
1864 /* Set starting address; reads will auto-increment */
1865 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1866 rmb();
1867
Reinette Chatreb7a79402009-09-25 14:24:23 -07001868 /* "time" is actually "data" for mode 0 (no timestamp).
1869 * place event id # at far right for easier visual parsing. */
1870 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001871 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1872 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001873 if (mode == 0) {
1874 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001875 if (bufsz) {
1876 pos += scnprintf(*buf + pos, bufsz - pos,
1877 "EVT_LOG:0x%08x:%04u\n",
1878 time, ev);
1879 } else {
1880 trace_iwlwifi_dev_ucode_event(priv, 0,
1881 time, ev);
1882 IWL_ERR(priv, "EVT_LOG:0x%08x:%04u\n",
1883 time, ev);
1884 }
Reinette Chatreb7a79402009-09-25 14:24:23 -07001885 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001886 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001887 if (bufsz) {
1888 pos += scnprintf(*buf + pos, bufsz - pos,
1889 "EVT_LOGT:%010u:0x%08x:%04u\n",
1890 time, data, ev);
1891 } else {
1892 IWL_ERR(priv, "EVT_LOGT:%010u:0x%08x:%04u\n",
Reinette Chatreb7a79402009-09-25 14:24:23 -07001893 time, data, ev);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001894 trace_iwlwifi_dev_ucode_event(priv, time,
1895 data, ev);
1896 }
Reinette Chatreb7a79402009-09-25 14:24:23 -07001897 }
1898 }
Ben Cahille5854472009-11-06 14:52:58 -08001899
1900 /* Allow device to power down */
1901 iwl_release_nic_access(priv);
1902 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001903 return pos;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001904}
1905
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001906/**
1907 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
1908 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001909static int iwl_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
1910 u32 num_wraps, u32 next_entry,
1911 u32 size, u32 mode,
1912 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001913{
1914 /*
1915 * display the newest DEFAULT_LOG_ENTRIES entries
1916 * i.e the entries just before the next ont that uCode would fill.
1917 */
1918 if (num_wraps) {
1919 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001920 pos = iwl_print_event_log(priv,
1921 capacity - (size - next_entry),
1922 size - next_entry, mode,
1923 pos, buf, bufsz);
1924 pos = iwl_print_event_log(priv, 0,
1925 next_entry, mode,
1926 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001927 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001928 pos = iwl_print_event_log(priv, next_entry - size,
1929 size, mode, pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001930 } else {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001931 if (next_entry < size) {
1932 pos = iwl_print_event_log(priv, 0, next_entry,
1933 mode, pos, buf, bufsz);
1934 } else {
1935 pos = iwl_print_event_log(priv, next_entry - size,
1936 size, mode, pos, buf, bufsz);
1937 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001938 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001939 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001940}
1941
Ben Cahill84c40692009-11-06 14:52:57 -08001942/* For sanity check only. Actual size is determined by uCode, typ. 512 */
1943#define MAX_EVENT_LOG_SIZE (512)
1944
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001945#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
1946
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001947int iwl_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1948 char **buf, bool display)
Reinette Chatreb7a79402009-09-25 14:24:23 -07001949{
1950 u32 base; /* SRAM byte address of event log header */
1951 u32 capacity; /* event log capacity in # entries */
1952 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1953 u32 num_wraps; /* # times uCode wrapped to top of log */
1954 u32 next_entry; /* index of next entry to be written by uCode */
1955 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001956 int pos = 0;
1957 size_t bufsz = 0;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001958
1959 if (priv->ucode_type == UCODE_INIT)
1960 base = le32_to_cpu(priv->card_alive_init.log_event_table_ptr);
1961 else
1962 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1963
1964 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Wey-Yi Guy212fb572009-12-02 12:53:00 -08001965 IWL_ERR(priv,
1966 "Invalid event log pointer 0x%08X for %s uCode\n",
1967 base, (priv->ucode_type == UCODE_INIT) ? "Init" : "RT");
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001968 return -EINVAL;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001969 }
1970
1971 /* event log header */
1972 capacity = iwl_read_targ_mem(priv, base);
1973 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1974 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1975 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
1976
Ben Cahill84c40692009-11-06 14:52:57 -08001977 if (capacity > MAX_EVENT_LOG_SIZE) {
1978 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
1979 capacity, MAX_EVENT_LOG_SIZE);
1980 capacity = MAX_EVENT_LOG_SIZE;
1981 }
1982
1983 if (next_entry > MAX_EVENT_LOG_SIZE) {
1984 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
1985 next_entry, MAX_EVENT_LOG_SIZE);
1986 next_entry = MAX_EVENT_LOG_SIZE;
1987 }
1988
Reinette Chatreb7a79402009-09-25 14:24:23 -07001989 size = num_wraps ? capacity : next_entry;
1990
1991 /* bail out if nothing in log */
1992 if (size == 0) {
1993 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001994 return pos;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001995 }
1996
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001997#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001998 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001999 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
2000 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
2001#else
2002 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
2003 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
Reinette Chatreb7a79402009-09-25 14:24:23 -07002004#endif
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002005 IWL_ERR(priv, "Start IWL Event Log Dump: display last %u entries\n",
2006 size);
2007
2008#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002009 if (display) {
2010 if (full_log)
2011 bufsz = capacity * 48;
2012 else
2013 bufsz = size * 48;
2014 *buf = kmalloc(bufsz, GFP_KERNEL);
2015 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08002016 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002017 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002018 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
2019 /*
2020 * if uCode has wrapped back to top of log,
2021 * start at the oldest entry,
2022 * i.e the next one that uCode would fill.
2023 */
2024 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002025 pos = iwl_print_event_log(priv, next_entry,
2026 capacity - next_entry, mode,
2027 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002028 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002029 pos = iwl_print_event_log(priv, 0,
2030 next_entry, mode, pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002031 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002032 pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
2033 next_entry, size, mode,
2034 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002035#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002036 pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
2037 next_entry, size, mode,
2038 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002039#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002040 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002041}
Reinette Chatreb7a79402009-09-25 14:24:23 -07002042
Zhu Yib481de92007-09-25 17:54:57 -07002043/**
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002044 * iwl_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002045 * from protocol/runtime uCode (initialization uCode's
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002046 * Alive gets handled by iwl_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002047 */
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002048static void iwl_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002049{
Tomas Winkler57aab752008-04-14 21:16:03 -07002050 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002051
Tomas Winklere1623442009-01-27 14:27:56 -08002052 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002053
2054 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2055 /* We had an error bringing up the hardware, so take it
2056 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002057 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002058 goto restart;
2059 }
2060
2061 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2062 * This is a paranoid check, because we would not have gotten the
2063 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07002064 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002065 /* Runtime instruction load was bad;
2066 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002067 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002068 goto restart;
2069 }
2070
Tomas Winklerc587de02009-06-03 11:44:07 -07002071 iwl_clear_stations_table(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002072 ret = priv->cfg->ops->lib->alive_notify(priv);
2073 if (ret) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002074 IWL_WARN(priv,
2075 "Could not complete ALIVE transition [ntf]: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07002076 goto restart;
2077 }
2078
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002079 /* After the ALIVE response, we can send host commands to the uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002080 set_bit(STATUS_ALIVE, &priv->status);
2081
Tomas Winklerfee12472008-04-03 16:05:21 -07002082 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002083 return;
2084
Johannes Berg36d68252008-05-15 12:55:26 +02002085 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002086
2087 priv->active_rate = priv->rates_mask;
2088 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2089
Wey-Yi Guy2f748de2009-09-17 10:43:51 -07002090 /* Configure Tx antenna selection based on H/W config */
2091 if (priv->cfg->ops->hcmd->set_tx_ant)
2092 priv->cfg->ops->hcmd->set_tx_ant(priv, priv->cfg->valid_tx_ant);
2093
Tomas Winkler3109ece2008-03-28 16:33:35 -07002094 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002095 struct iwl_rxon_cmd *active_rxon =
2096 (struct iwl_rxon_cmd *)&priv->active_rxon;
Mohamed Abbas019fb972009-03-17 21:59:18 -07002097 /* apply any changes in staging */
2098 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002099 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2100 } else {
2101 /* Initialize our rx_config data */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002102 iwl_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekar45823532009-04-08 11:26:44 -07002103
2104 if (priv->cfg->ops->hcmd->set_rxon_chain)
2105 priv->cfg->ops->hcmd->set_rxon_chain(priv);
2106
Zhu Yib481de92007-09-25 17:54:57 -07002107 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2108 }
2109
Ben Cahill9fbab512007-11-29 11:09:47 +08002110 /* Configure Bluetooth device coexistence support */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002111 iwl_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002112
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002113 iwl_reset_run_time_calib(priv);
2114
Zhu Yib481de92007-09-25 17:54:57 -07002115 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002116 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002117
2118 /* At this point, the NIC is initialized and operational */
Emmanuel Grumbach47f4a582008-06-12 09:47:13 +08002119 iwl_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002120
Johannes Berge932a602009-10-02 13:44:03 -07002121 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002122
Tomas Winklere1623442009-01-27 14:27:56 -08002123 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002124 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002125 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002126
Johannes Berge312c242009-08-07 15:41:51 -07002127 iwl_power_update_mode(priv, true);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002128
Mohamed Abbasada17512008-11-07 09:58:34 -08002129 /* reassociate for ADHOC mode */
2130 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2131 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2132 priv->vif);
2133 if (beacon)
2134 iwl_mac_beacon_update(priv->hw, beacon);
2135 }
2136
2137
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002138 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002139 iwl_set_mode(priv, priv->iw_mode);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002140
Zhu Yib481de92007-09-25 17:54:57 -07002141 return;
2142
2143 restart:
2144 queue_work(priv->workqueue, &priv->restart);
2145}
2146
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002147static void iwl_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002148
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002149static void __iwl_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002150{
2151 unsigned long flags;
2152 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002153
Tomas Winklere1623442009-01-27 14:27:56 -08002154 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002155
Zhu Yib481de92007-09-25 17:54:57 -07002156 if (!exit_pending)
2157 set_bit(STATUS_EXIT_PENDING, &priv->status);
2158
Tomas Winklerc587de02009-06-03 11:44:07 -07002159 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002160
2161 /* Unblock any waiting calls */
2162 wake_up_interruptible_all(&priv->wait_command_queue);
2163
Zhu Yib481de92007-09-25 17:54:57 -07002164 /* Wipe out the EXIT_PENDING status bit if we are not actually
2165 * exiting the module */
2166 if (!exit_pending)
2167 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2168
2169 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002170 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002171
2172 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002173 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002174 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002175 spin_unlock_irqrestore(&priv->lock, flags);
2176 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002177
2178 if (priv->mac80211_registered)
2179 ieee80211_stop_queues(priv->hw);
2180
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002181 /* If we have not previously called iwl_init() then
Johannes Berga60e77e2009-06-04 18:26:06 +02002182 * clear all bits but the RF Kill bit and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07002183 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002184 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2185 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002186 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2187 STATUS_GEO_CONFIGURED |
Mohamed Abbas052ec3f2008-06-30 17:23:15 +08002188 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2189 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002190 goto exit;
2191 }
2192
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002193 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002194 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002195 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2196 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002197 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2198 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002199 test_bit(STATUS_FW_ERROR, &priv->status) <<
Mohamed Abbas052ec3f2008-06-30 17:23:15 +08002200 STATUS_FW_ERROR |
2201 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2202 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002203
Mohamed Abbasef850d72009-05-22 11:01:50 -07002204 /* device going down, Stop using ICT table */
2205 iwl_disable_ict(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002206
Tomas Winklerda1bc452008-05-29 16:35:00 +08002207 iwl_txq_ctx_stop(priv);
Tomas Winklerb3bbacb2008-05-29 16:35:01 +08002208 iwl_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002209
Ben Cahill309e7312009-11-06 14:53:03 -08002210 /* Power-down device's busmaster DMA clocks */
2211 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002212 udelay(5);
2213
Ben Cahill309e7312009-11-06 14:53:03 -08002214 /* Make sure (redundant) we've released our request to stay awake */
2215 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2216
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002217 /* Stop the device, and put it in low power state */
2218 priv->cfg->ops->lib->apm_ops.stop(priv);
2219
Zhu Yib481de92007-09-25 17:54:57 -07002220 exit:
Tomas Winkler885ba202008-05-29 16:34:55 +08002221 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002222
2223 if (priv->ibss_beacon)
2224 dev_kfree_skb(priv->ibss_beacon);
2225 priv->ibss_beacon = NULL;
2226
2227 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08002228 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002229}
2230
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002231static void iwl_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002232{
2233 mutex_lock(&priv->mutex);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002234 __iwl_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002235 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002236
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002237 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002238}
2239
Mohamed Abbas086ed112009-05-22 11:01:54 -07002240#define HW_READY_TIMEOUT (50)
2241
2242static int iwl_set_hw_ready(struct iwl_priv *priv)
2243{
2244 int ret = 0;
2245
2246 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
2247 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
2248
2249 /* See if we got it */
2250 ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
2251 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
2252 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
2253 HW_READY_TIMEOUT);
2254 if (ret != -ETIMEDOUT)
2255 priv->hw_ready = true;
2256 else
2257 priv->hw_ready = false;
2258
2259 IWL_DEBUG_INFO(priv, "hardware %s\n",
2260 (priv->hw_ready == 1) ? "ready" : "not ready");
2261 return ret;
2262}
2263
2264static int iwl_prepare_card_hw(struct iwl_priv *priv)
2265{
2266 int ret = 0;
2267
2268 IWL_DEBUG_INFO(priv, "iwl_prepare_card_hw enter \n");
2269
Mohamed Abbas3354a0f2009-06-19 13:52:41 -07002270 ret = iwl_set_hw_ready(priv);
2271 if (priv->hw_ready)
2272 return ret;
2273
2274 /* If HW is not ready, prepare the conditions to check again */
Mohamed Abbas086ed112009-05-22 11:01:54 -07002275 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
2276 CSR_HW_IF_CONFIG_REG_PREPARE);
2277
2278 ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
2279 ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
2280 CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
2281
Mohamed Abbas3354a0f2009-06-19 13:52:41 -07002282 /* HW should be ready by now, check again. */
Mohamed Abbas086ed112009-05-22 11:01:54 -07002283 if (ret != -ETIMEDOUT)
2284 iwl_set_hw_ready(priv);
2285
2286 return ret;
2287}
2288
Zhu Yib481de92007-09-25 17:54:57 -07002289#define MAX_HW_RESTARTS 5
2290
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002291static int __iwl_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002292{
Tomas Winkler57aab752008-04-14 21:16:03 -07002293 int i;
2294 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002295
2296 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002297 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002298 return -EIO;
2299 }
2300
Reinette Chatree903fbd2008-01-30 22:05:15 -08002301 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002302 IWL_ERR(priv, "ucode not available for device bringup\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002303 return -EIO;
2304 }
2305
Mohamed Abbas086ed112009-05-22 11:01:54 -07002306 iwl_prepare_card_hw(priv);
2307
2308 if (!priv->hw_ready) {
2309 IWL_WARN(priv, "Exit HW not ready\n");
2310 return -EIO;
2311 }
2312
Zhu Yie655b9f2008-01-24 02:19:38 -08002313 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winklerc1842d62008-08-04 16:00:43 +08002314 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Zhu Yie655b9f2008-01-24 02:19:38 -08002315 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Emmanuel Grumbach3bff19c2008-06-30 17:23:19 +08002316 else
Zhu Yie655b9f2008-01-24 02:19:38 -08002317 set_bit(STATUS_RF_KILL_HW, &priv->status);
Emmanuel Grumbach3bff19c2008-06-30 17:23:19 +08002318
Tomas Winklerc1842d62008-08-04 16:00:43 +08002319 if (iwl_is_rfkill(priv)) {
Johannes Berga60e77e2009-06-04 18:26:06 +02002320 wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
2321
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002322 iwl_enable_interrupts(priv);
Johannes Berga60e77e2009-06-04 18:26:06 +02002323 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Tomas Winklerc1842d62008-08-04 16:00:43 +08002324 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002325 }
2326
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002327 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002328
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002329 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002330 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002331 IWL_ERR(priv, "Unable to init nic\n");
Tomas Winkler57aab752008-04-14 21:16:03 -07002332 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002333 }
2334
2335 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002336 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2337 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002338 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2339
2340 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002341 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002342 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002343
2344 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002345 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2346 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002347
2348 /* Copy original ucode data image from disk into backup cache.
2349 * This will be used to initialize the on-board processor's
2350 * data SRAM for a clean start when the runtime program first loads. */
2351 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08002352 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002353
Zhu Yib481de92007-09-25 17:54:57 -07002354 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2355
Tomas Winklerc587de02009-06-03 11:44:07 -07002356 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002357
2358 /* load bootstrap state machine,
2359 * load bootstrap program into processor's memory,
2360 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07002361 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002362
Tomas Winkler57aab752008-04-14 21:16:03 -07002363 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002364 IWL_ERR(priv, "Unable to set up bootstrap uCode: %d\n",
2365 ret);
Zhu Yib481de92007-09-25 17:54:57 -07002366 continue;
2367 }
2368
2369 /* start card; "initialize" will load runtime ucode */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002370 iwl_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002371
Tomas Winklere1623442009-01-27 14:27:56 -08002372 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002373
2374 return 0;
2375 }
2376
2377 set_bit(STATUS_EXIT_PENDING, &priv->status);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002378 __iwl_down(priv);
Mohamed Abbas64e72c3e2008-06-12 09:47:03 +08002379 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002380
2381 /* tried to restart and config the device for as long as our
2382 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002383 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002384 return -EIO;
2385}
2386
2387
2388/*****************************************************************************
2389 *
2390 * Workqueue callbacks
2391 *
2392 *****************************************************************************/
2393
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002394static void iwl_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002395{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002396 struct iwl_priv *priv =
2397 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002398
2399 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2400 return;
2401
2402 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08002403 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002404 mutex_unlock(&priv->mutex);
2405}
2406
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002407static void iwl_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002408{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002409 struct iwl_priv *priv =
2410 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002411
2412 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2413 return;
2414
Mohamed Abbas258c44a2009-06-03 11:44:10 -07002415 /* enable dram interrupt */
2416 iwl_reset_ict(priv);
2417
Zhu Yib481de92007-09-25 17:54:57 -07002418 mutex_lock(&priv->mutex);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002419 iwl_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002420 mutex_unlock(&priv->mutex);
2421}
2422
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08002423static void iwl_bg_run_time_calib_work(struct work_struct *work)
2424{
2425 struct iwl_priv *priv = container_of(work, struct iwl_priv,
2426 run_time_calib_work);
2427
2428 mutex_lock(&priv->mutex);
2429
2430 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
2431 test_bit(STATUS_SCANNING, &priv->status)) {
2432 mutex_unlock(&priv->mutex);
2433 return;
2434 }
2435
2436 if (priv->start_calib) {
2437 iwl_chain_noise_calibration(priv, &priv->statistics);
2438
2439 iwl_sensitivity_calibration(priv, &priv->statistics);
2440 }
2441
2442 mutex_unlock(&priv->mutex);
2443 return;
2444}
2445
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002446static void iwl_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002447{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002448 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07002449
2450 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2451 return;
2452
2453 mutex_lock(&priv->mutex);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002454 __iwl_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002455 mutex_unlock(&priv->mutex);
2456}
2457
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002458static void iwl_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002459{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002460 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07002461
2462 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2463 return;
2464
Johannes Berg19cc1082009-05-08 13:44:36 -07002465 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
2466 mutex_lock(&priv->mutex);
2467 priv->vif = NULL;
2468 priv->is_open = 0;
2469 mutex_unlock(&priv->mutex);
2470 iwl_down(priv);
2471 ieee80211_restart_hw(priv->hw);
2472 } else {
2473 iwl_down(priv);
2474 queue_work(priv->workqueue, &priv->up);
2475 }
Zhu Yib481de92007-09-25 17:54:57 -07002476}
2477
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002478static void iwl_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002479{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002480 struct iwl_priv *priv =
2481 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07002482
2483 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2484 return;
2485
2486 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08002487 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002488 mutex_unlock(&priv->mutex);
2489}
2490
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002491#define IWL_DELAY_NEXT_SCAN (HZ*2)
2492
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07002493void iwl_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002494{
Zhu Yib481de92007-09-25 17:54:57 -07002495 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07002496 int ret = 0;
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002497 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07002498
Johannes Berg05c914f2008-09-11 00:01:58 +02002499 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002500 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07002501 return;
2502 }
2503
Tomas Winklere1623442009-01-27 14:27:56 -08002504 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002505 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07002506
2507
2508 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2509 return;
2510
Zhu Yib481de92007-09-25 17:54:57 -07002511
Reinette Chatre508e32e2008-04-14 21:16:13 -07002512 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08002513 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07002514
Tomas Winkler2a421b92008-06-12 09:47:10 +08002515 iwl_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08002516
Zhu Yib481de92007-09-25 17:54:57 -07002517 conf = ieee80211_get_hw_conf(priv->hw);
2518
2519 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002520 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002521
Tomas Winkler3195c1f2008-10-08 09:37:30 +08002522 iwl_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07002523 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07002524 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07002525 if (ret)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002526 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07002527 "Attempting to continue.\n");
2528
2529 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2530
Emmanuel Grumbach42eb7c62008-09-17 10:10:05 +08002531 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +03002532
Abhijeet Kolekar45823532009-04-08 11:26:44 -07002533 if (priv->cfg->ops->hcmd->set_rxon_chain)
2534 priv->cfg->ops->hcmd->set_rxon_chain(priv);
2535
Zhu Yib481de92007-09-25 17:54:57 -07002536 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2537
Tomas Winklere1623442009-01-27 14:27:56 -08002538 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002539 priv->assoc_id, priv->beacon_int);
2540
2541 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2542 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2543 else
2544 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2545
2546 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2547 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2548 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2549 else
2550 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2551
Johannes Berg05c914f2008-09-11 00:01:58 +02002552 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002553 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2554
2555 }
2556
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002557 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002558
2559 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002560 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002561 break;
2562
Johannes Berg05c914f2008-09-11 00:01:58 +02002563 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07002564
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002565 /* assume default assoc id */
2566 priv->assoc_id = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002567
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002568 iwl_rxon_add_station(priv, priv->bssid, 0);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002569 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002570
2571 break;
2572
2573 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002574 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002575 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002576 break;
2577 }
2578
Johannes Berg05c914f2008-09-11 00:01:58 +02002579 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002580 priv->assoc_station_added = 1;
2581
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002582 spin_lock_irqsave(&priv->lock, flags);
2583 iwl_activate_qos(priv, 0);
2584 spin_unlock_irqrestore(&priv->lock, flags);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08002585
Grumbach, Emmanuel04816442008-09-03 11:26:53 +08002586 /* the chain noise calibration will enabled PM upon completion
2587 * If chain noise has already been run, then we need to enable
2588 * power management here */
2589 if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
Johannes Berge312c242009-08-07 15:41:51 -07002590 iwl_power_update_mode(priv, false);
Emmanuel Grumbachc90a74ba2008-09-03 11:26:50 +08002591
2592 /* Enable Rx differential gain and sensitivity calibrations */
2593 iwl_chain_noise_reset(priv);
2594 priv->start_calib = 1;
2595
Reinette Chatre508e32e2008-04-14 21:16:13 -07002596}
2597
Zhu Yib481de92007-09-25 17:54:57 -07002598/*****************************************************************************
2599 *
2600 * mac80211 entry point functions
2601 *
2602 *****************************************************************************/
2603
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08002604#define UCODE_READY_TIMEOUT (4 * HZ)
Zhu Yi5a66926a2008-01-14 17:46:18 -08002605
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002606/*
2607 * Not a mac80211 entry point function, but it fits in with all the
2608 * other mac80211 functions grouped here.
2609 */
2610static int iwl_setup_mac(struct iwl_priv *priv)
2611{
2612 int ret;
2613 struct ieee80211_hw *hw = priv->hw;
2614 hw->rate_control_algorithm = "iwl-agn-rs";
2615
2616 /* Tell mac80211 our characteristics */
2617 hw->flags = IEEE80211_HW_SIGNAL_DBM |
2618 IEEE80211_HW_NOISE_DBM |
2619 IEEE80211_HW_AMPDU_AGGREGATION |
2620 IEEE80211_HW_SPECTRUM_MGMT;
2621
2622 if (!priv->cfg->broken_powersave)
2623 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
2624 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
2625
Johannes Bergba37a3d2009-12-10 14:37:27 -08002626 if (priv->cfg->sku & IWL_SKU_N)
2627 hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
2628 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
2629
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002630 hw->sta_data_size = sizeof(struct iwl_station_priv);
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002631 hw->wiphy->interface_modes =
2632 BIT(NL80211_IFTYPE_STATION) |
2633 BIT(NL80211_IFTYPE_ADHOC);
2634
Johannes Berg5be83de2009-11-19 00:56:28 +01002635 hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY |
2636 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002637
2638 /*
2639 * For now, disable PS by default because it affects
2640 * RX performance significantly.
2641 */
Johannes Berg5be83de2009-11-19 00:56:28 +01002642 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002643
2644 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
2645 /* we create the 802.11 header and a zero-length SSID element */
2646 hw->wiphy->max_scan_ie_len = IWL_MAX_PROBE_REQUEST - 24 - 2;
2647
2648 /* Default value; 4 EDCA QOS priorities */
2649 hw->queues = 4;
2650
2651 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
2652
2653 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
2654 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
2655 &priv->bands[IEEE80211_BAND_2GHZ];
2656 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
2657 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
2658 &priv->bands[IEEE80211_BAND_5GHZ];
2659
2660 ret = ieee80211_register_hw(priv->hw);
2661 if (ret) {
2662 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
2663 return ret;
2664 }
2665 priv->mac80211_registered = 1;
2666
2667 return 0;
2668}
2669
2670
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002671static int iwl_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002672{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002673 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002674 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002675
Tomas Winklere1623442009-01-27 14:27:56 -08002676 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002677
2678 /* we should be verifying the device is ready to be opened */
2679 mutex_lock(&priv->mutex);
2680
Zhu Yi5a66926a2008-01-14 17:46:18 -08002681 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
2682 * ucode filename and max sizes are card-specific. */
2683
2684 if (!priv->ucode_code.len) {
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002685 ret = iwl_read_ucode(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002686 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002687 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002688 mutex_unlock(&priv->mutex);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002689 return ret;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002690 }
2691 }
2692
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002693 ret = __iwl_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002694
Zhu Yib481de92007-09-25 17:54:57 -07002695 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002696
Zhu Yie655b9f2008-01-24 02:19:38 -08002697 if (ret)
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002698 return ret;
Zhu Yie655b9f2008-01-24 02:19:38 -08002699
Tomas Winklerc1842d62008-08-04 16:00:43 +08002700 if (iwl_is_rfkill(priv))
2701 goto out;
2702
Tomas Winklere1623442009-01-27 14:27:56 -08002703 IWL_DEBUG_INFO(priv, "Start UP work done.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08002704
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002705 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
Zhu Yi5a66926a2008-01-14 17:46:18 -08002706 * mac80211 will not be run successfully. */
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08002707 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2708 test_bit(STATUS_READY, &priv->status),
2709 UCODE_READY_TIMEOUT);
2710 if (!ret) {
2711 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002712 IWL_ERR(priv, "START_ALIVE timeout after %dms.\n",
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08002713 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002714 return -ETIMEDOUT;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002715 }
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002716 }
Tomas Winkler0a078ff2008-06-30 17:23:26 +08002717
Johannes Berge932a602009-10-02 13:44:03 -07002718 iwl_led_start(priv);
2719
Tomas Winklerc1842d62008-08-04 16:00:43 +08002720out:
Tomas Winkler0a078ff2008-06-30 17:23:26 +08002721 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08002722 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002723 return 0;
2724}
2725
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002726static void iwl_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002727{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002728 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002729
Tomas Winklere1623442009-01-27 14:27:56 -08002730 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08002731
Johannes Berg19cc1082009-05-08 13:44:36 -07002732 if (!priv->is_open)
Zhu Yie655b9f2008-01-24 02:19:38 -08002733 return;
Zhu Yie655b9f2008-01-24 02:19:38 -08002734
Zhu Yib481de92007-09-25 17:54:57 -07002735 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08002736
Wey-Yi Guy5bddf542009-08-21 13:34:25 -07002737 if (iwl_is_ready_rf(priv) || test_bit(STATUS_SCAN_HW, &priv->status)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08002738 /* stop mac, cancel any scan request and clear
2739 * RXON_FILTER_ASSOC_MSK BIT
2740 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08002741 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002742 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002743 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002744 }
2745
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002746 iwl_down(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08002747
2748 flush_workqueue(priv->workqueue);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002749
2750 /* enable interrupts again in order to receive rfkill changes */
2751 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2752 iwl_enable_interrupts(priv);
Mohamed Abbas948c1712007-10-25 17:15:45 +08002753
Tomas Winklere1623442009-01-27 14:27:56 -08002754 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002755}
2756
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002757static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002758{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002759 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002760
Tomas Winklere1623442009-01-27 14:27:56 -08002761 IWL_DEBUG_MACDUMP(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002762
Tomas Winklere1623442009-01-27 14:27:56 -08002763 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02002764 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07002765
Johannes Berge039fa42008-05-15 12:55:29 +02002766 if (iwl_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07002767 dev_kfree_skb_any(skb);
2768
Tomas Winklere1623442009-01-27 14:27:56 -08002769 IWL_DEBUG_MACDUMP(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08002770 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07002771}
2772
Abhijeet Kolekar60690a62009-04-08 11:26:49 -07002773void iwl_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002774{
Tomas Winkler857485c2008-03-21 13:53:44 -07002775 int ret = 0;
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002776 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07002777
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08002778 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07002779 return;
2780
2781 /* The following should be done only at AP bring up */
Tomas Winkler3195c1f2008-10-08 09:37:30 +08002782 if (!iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002783
2784 /* RXON - unassoc (to set timing command) */
2785 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002786 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002787
2788 /* RXON Timing */
Tomas Winkler3195c1f2008-10-08 09:37:30 +08002789 iwl_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07002790 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07002791 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07002792 if (ret)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002793 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07002794 "Attempting to continue.\n");
2795
Daniel C Halperinf513dff2009-11-13 11:56:34 -08002796 /* AP has all antennas */
2797 priv->chain_noise_data.active_chains =
2798 priv->hw_params.valid_rx_ant;
2799 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Abhijeet Kolekar45823532009-04-08 11:26:44 -07002800 if (priv->cfg->ops->hcmd->set_rxon_chain)
2801 priv->cfg->ops->hcmd->set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002802
2803 /* FIXME: what should be the assoc_id for AP? */
2804 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2805 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2806 priv->staging_rxon.flags |=
2807 RXON_FLG_SHORT_PREAMBLE_MSK;
2808 else
2809 priv->staging_rxon.flags &=
2810 ~RXON_FLG_SHORT_PREAMBLE_MSK;
2811
2812 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2813 if (priv->assoc_capability &
2814 WLAN_CAPABILITY_SHORT_SLOT_TIME)
2815 priv->staging_rxon.flags |=
2816 RXON_FLG_SHORT_SLOT_MSK;
2817 else
2818 priv->staging_rxon.flags &=
2819 ~RXON_FLG_SHORT_SLOT_MSK;
2820
Johannes Berg05c914f2008-09-11 00:01:58 +02002821 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002822 priv->staging_rxon.flags &=
2823 ~RXON_FLG_SHORT_SLOT_MSK;
2824 }
2825 /* restore RXON assoc */
2826 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002827 iwlcore_commit_rxon(priv);
Daniel C Halperinf513dff2009-11-13 11:56:34 -08002828 iwl_reset_qos(priv);
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002829 spin_lock_irqsave(&priv->lock, flags);
2830 iwl_activate_qos(priv, 1);
2831 spin_unlock_irqrestore(&priv->lock, flags);
Reinette Chatre9a9ca652009-10-30 14:36:12 -07002832 iwl_add_bcast_station(priv);
Zhu Yie1493de2007-09-27 11:27:32 +08002833 }
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002834 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002835
2836 /* FIXME - we need to add code here to detect a totally new
2837 * configuration, reset the AP, unassoc, rxon timing, assoc,
2838 * clear sta table, add BCAST sta... */
2839}
2840
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002841static void iwl_mac_update_tkip_key(struct ieee80211_hw *hw,
Johannes Bergb3fbdcf2010-01-21 11:40:47 +01002842 struct ieee80211_vif *vif,
2843 struct ieee80211_key_conf *keyconf,
2844 struct ieee80211_sta *sta,
2845 u32 iv32, u16 *phase1key)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002846{
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002847
Tomas Winkler9f586712008-11-12 13:14:05 -08002848 struct iwl_priv *priv = hw->priv;
Tomas Winklere1623442009-01-27 14:27:56 -08002849 IWL_DEBUG_MAC80211(priv, "enter\n");
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002850
Johannes Bergb3fbdcf2010-01-21 11:40:47 +01002851 iwl_update_tkip_key(priv, keyconf,
2852 sta ? sta->addr : iwl_bcast_addr,
2853 iv32, phase1key);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002854
Tomas Winklere1623442009-01-27 14:27:56 -08002855 IWL_DEBUG_MAC80211(priv, "leave\n");
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002856}
2857
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002858static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01002859 struct ieee80211_vif *vif,
2860 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07002861 struct ieee80211_key_conf *key)
2862{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002863 struct iwl_priv *priv = hw->priv;
Winkler, Tomas42986792009-01-19 15:30:22 -08002864 const u8 *addr;
2865 int ret;
2866 u8 sta_id;
2867 bool is_default_wep_key = false;
Zhu Yib481de92007-09-25 17:54:57 -07002868
Tomas Winklere1623442009-01-27 14:27:56 -08002869 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002870
Tomas Winkler90e8e422009-06-19 13:52:42 -07002871 if (priv->cfg->mod_params->sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08002872 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07002873 return -EOPNOTSUPP;
2874 }
Winkler, Tomas42986792009-01-19 15:30:22 -08002875 addr = sta ? sta->addr : iwl_bcast_addr;
Tomas Winklerc587de02009-06-03 11:44:07 -07002876 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002877 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002878 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Johannes Berge1749612008-10-27 15:59:26 -07002879 addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002880 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002881
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002882 }
Zhu Yib481de92007-09-25 17:54:57 -07002883
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002884 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002885 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002886 mutex_unlock(&priv->mutex);
2887
2888 /* If we are getting WEP group key and we didn't receive any key mapping
2889 * so far, we are in legacy wep mode (group key only), otherwise we are
2890 * in 1X mode.
2891 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07002892 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002893 priv->iw_mode != NL80211_IFTYPE_AP) {
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002894 if (cmd == SET_KEY)
2895 is_default_wep_key = !priv->key_mapping_key;
2896 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08002897 is_default_wep_key =
2898 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002899 }
mabbas052c4b92007-10-25 17:15:43 +08002900
Zhu Yib481de92007-09-25 17:54:57 -07002901 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002902 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002903 if (is_default_wep_key)
2904 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002905 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07002906 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002907
Tomas Winklere1623442009-01-27 14:27:56 -08002908 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07002909 break;
2910 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002911 if (is_default_wep_key)
2912 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002913 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07002914 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002915
Tomas Winklere1623442009-01-27 14:27:56 -08002916 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07002917 break;
2918 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002919 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002920 }
2921
Tomas Winklere1623442009-01-27 14:27:56 -08002922 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002923
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002924 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002925}
2926
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002927static int iwl_mac_ampdu_action(struct ieee80211_hw *hw,
Johannes Bergc951ad32009-11-16 12:00:38 +01002928 struct ieee80211_vif *vif,
Tomas Winklerd783b062008-07-18 13:53:02 +08002929 enum ieee80211_ampdu_mlme_action action,
Johannes Berg17741cd2008-09-11 00:02:02 +02002930 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Tomas Winklerd783b062008-07-18 13:53:02 +08002931{
2932 struct iwl_priv *priv = hw->priv;
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002933 int ret;
Tomas Winklerd783b062008-07-18 13:53:02 +08002934
Tomas Winklere1623442009-01-27 14:27:56 -08002935 IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
Johannes Berge1749612008-10-27 15:59:26 -07002936 sta->addr, tid);
Tomas Winklerd783b062008-07-18 13:53:02 +08002937
2938 if (!(priv->cfg->sku & IWL_SKU_N))
2939 return -EACCES;
2940
2941 switch (action) {
2942 case IEEE80211_AMPDU_RX_START:
Tomas Winklere1623442009-01-27 14:27:56 -08002943 IWL_DEBUG_HT(priv, "start Rx\n");
Tomas Winkler9f586712008-11-12 13:14:05 -08002944 return iwl_sta_rx_agg_start(priv, sta->addr, tid, *ssn);
Tomas Winklerd783b062008-07-18 13:53:02 +08002945 case IEEE80211_AMPDU_RX_STOP:
Tomas Winklere1623442009-01-27 14:27:56 -08002946 IWL_DEBUG_HT(priv, "stop Rx\n");
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002947 ret = iwl_sta_rx_agg_stop(priv, sta->addr, tid);
2948 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2949 return 0;
2950 else
2951 return ret;
Tomas Winklerd783b062008-07-18 13:53:02 +08002952 case IEEE80211_AMPDU_TX_START:
Tomas Winklere1623442009-01-27 14:27:56 -08002953 IWL_DEBUG_HT(priv, "start Tx\n");
Johannes Berg17741cd2008-09-11 00:02:02 +02002954 return iwl_tx_agg_start(priv, sta->addr, tid, ssn);
Tomas Winklerd783b062008-07-18 13:53:02 +08002955 case IEEE80211_AMPDU_TX_STOP:
Tomas Winklere1623442009-01-27 14:27:56 -08002956 IWL_DEBUG_HT(priv, "stop Tx\n");
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002957 ret = iwl_tx_agg_stop(priv, sta->addr, tid);
2958 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2959 return 0;
2960 else
2961 return ret;
Wey-Yi Guyf0527972010-01-08 10:04:41 -08002962 case IEEE80211_AMPDU_TX_OPERATIONAL:
2963 /* do nothing */
2964 return -EOPNOTSUPP;
Tomas Winklerd783b062008-07-18 13:53:02 +08002965 default:
Tomas Winklere1623442009-01-27 14:27:56 -08002966 IWL_DEBUG_HT(priv, "unknown\n");
Tomas Winklerd783b062008-07-18 13:53:02 +08002967 return -EINVAL;
2968 break;
2969 }
2970 return 0;
2971}
Tomas Winkler9f586712008-11-12 13:14:05 -08002972
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002973static int iwl_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002974 struct ieee80211_low_level_stats *stats)
2975{
Ester Kummerbf403db2008-05-05 10:22:40 +08002976 struct iwl_priv *priv = hw->priv;
2977
2978 priv = hw->priv;
Tomas Winklere1623442009-01-27 14:27:56 -08002979 IWL_DEBUG_MAC80211(priv, "enter\n");
2980 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002981
2982 return 0;
2983}
2984
Johannes Berg6ab10ff2009-11-13 11:56:37 -08002985static void iwl_mac_sta_notify(struct ieee80211_hw *hw,
2986 struct ieee80211_vif *vif,
2987 enum sta_notify_cmd cmd,
2988 struct ieee80211_sta *sta)
2989{
2990 struct iwl_priv *priv = hw->priv;
2991 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
2992 int sta_id;
2993
2994 /*
2995 * TODO: We really should use this callback to
2996 * actually maintain the station table in
2997 * the device.
2998 */
2999
3000 switch (cmd) {
3001 case STA_NOTIFY_ADD:
3002 atomic_set(&sta_priv->pending_frames, 0);
3003 if (vif->type == NL80211_IFTYPE_AP)
3004 sta_priv->client = true;
3005 break;
3006 case STA_NOTIFY_SLEEP:
3007 WARN_ON(!sta_priv->client);
3008 sta_priv->asleep = true;
3009 if (atomic_read(&sta_priv->pending_frames) > 0)
3010 ieee80211_sta_block_awake(hw, sta, true);
3011 break;
3012 case STA_NOTIFY_AWAKE:
3013 WARN_ON(!sta_priv->client);
3014 sta_priv->asleep = false;
3015 sta_id = iwl_find_station(priv, sta->addr);
3016 if (sta_id != IWL_INVALID_STATION)
3017 iwl_sta_modify_ps_wake(priv, sta_id);
3018 break;
3019 default:
3020 break;
3021 }
3022}
3023
Zhu Yib481de92007-09-25 17:54:57 -07003024/*****************************************************************************
3025 *
3026 * sysfs attributes
3027 *
3028 *****************************************************************************/
3029
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003030#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003031
3032/*
3033 * The following adds a new attribute to the sysfs representation
Wu, Fengguangc3a739f2008-12-17 16:52:29 +08003034 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
Zhu Yib481de92007-09-25 17:54:57 -07003035 * used for controlling the debug level.
3036 *
3037 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003038 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003039 * The debug_level being managed using sysfs below is a per device debug
3040 * level that is used instead of the global debug level if it (the per
3041 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003042 */
Ester Kummer8cf769c2008-05-06 11:05:11 +08003043static ssize_t show_debug_level(struct device *d,
3044 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003045{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003046 struct iwl_priv *priv = dev_get_drvdata(d);
3047 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003048}
Ester Kummer8cf769c2008-05-06 11:05:11 +08003049static ssize_t store_debug_level(struct device *d,
3050 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003051 const char *buf, size_t count)
3052{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003053 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler9257746f2008-09-03 11:26:32 +08003054 unsigned long val;
3055 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003056
Tomas Winkler9257746f2008-09-03 11:26:32 +08003057 ret = strict_strtoul(buf, 0, &val);
3058 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003059 IWL_ERR(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003060 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003061 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003062 if (iwl_alloc_traffic_mem(priv))
3063 IWL_ERR(priv,
3064 "Not enough memory to generate traffic log\n");
3065 }
Zhu Yib481de92007-09-25 17:54:57 -07003066 return strnlen(buf, count);
3067}
3068
Ester Kummer8cf769c2008-05-06 11:05:11 +08003069static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3070 show_debug_level, store_debug_level);
3071
Zhu Yib481de92007-09-25 17:54:57 -07003072
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003073#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003074
Zhu Yib481de92007-09-25 17:54:57 -07003075
3076static ssize_t show_temperature(struct device *d,
3077 struct device_attribute *attr, char *buf)
3078{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003079 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003080
Tomas Winklerfee12472008-04-03 16:05:21 -07003081 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003082 return -EAGAIN;
3083
Emmanuel Grumbach91dbc5b2008-06-12 09:47:14 +08003084 return sprintf(buf, "%d\n", priv->temperature);
Zhu Yib481de92007-09-25 17:54:57 -07003085}
3086
3087static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3088
Zhu Yib481de92007-09-25 17:54:57 -07003089static ssize_t show_tx_power(struct device *d,
3090 struct device_attribute *attr, char *buf)
3091{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003092 struct iwl_priv *priv = dev_get_drvdata(d);
Jay Sternberg91f39e82008-12-17 16:52:34 +08003093
3094 if (!iwl_is_ready_rf(priv))
3095 return sprintf(buf, "off\n");
3096 else
3097 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003098}
3099
3100static ssize_t store_tx_power(struct device *d,
3101 struct device_attribute *attr,
3102 const char *buf, size_t count)
3103{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003104 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler9257746f2008-09-03 11:26:32 +08003105 unsigned long val;
3106 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003107
Tomas Winkler9257746f2008-09-03 11:26:32 +08003108 ret = strict_strtoul(buf, 10, &val);
3109 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003110 IWL_INFO(priv, "%s is not in decimal form.\n", buf);
Wey-Yi Guy5eadd942009-08-21 13:34:17 -07003111 else {
3112 ret = iwl_set_tx_power(priv, val, false);
3113 if (ret)
3114 IWL_ERR(priv, "failed setting tx power (0x%d).\n",
3115 ret);
3116 else
3117 ret = count;
3118 }
3119 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003120}
3121
3122static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3123
3124static ssize_t show_flags(struct device *d,
3125 struct device_attribute *attr, char *buf)
3126{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003127 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003128
3129 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3130}
3131
3132static ssize_t store_flags(struct device *d,
3133 struct device_attribute *attr,
3134 const char *buf, size_t count)
3135{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003136 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler9257746f2008-09-03 11:26:32 +08003137 unsigned long val;
3138 u32 flags;
3139 int ret = strict_strtoul(buf, 0, &val);
Emmanuel Grumbach926f0b22008-09-03 11:26:39 +08003140 if (ret)
Tomas Winkler9257746f2008-09-03 11:26:32 +08003141 return ret;
3142 flags = (u32)val;
Zhu Yib481de92007-09-25 17:54:57 -07003143
3144 mutex_lock(&priv->mutex);
3145 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3146 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003147 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003148 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003149 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003150 IWL_DEBUG_INFO(priv, "Commit rxon.flags = 0x%04X\n", flags);
Zhu Yib481de92007-09-25 17:54:57 -07003151 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003152 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003153 }
3154 }
3155 mutex_unlock(&priv->mutex);
3156
3157 return count;
3158}
3159
3160static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3161
3162static ssize_t show_filter_flags(struct device *d,
3163 struct device_attribute *attr, char *buf)
3164{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003165 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003166
3167 return sprintf(buf, "0x%04X\n",
3168 le32_to_cpu(priv->active_rxon.filter_flags));
3169}
3170
3171static ssize_t store_filter_flags(struct device *d,
3172 struct device_attribute *attr,
3173 const char *buf, size_t count)
3174{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003175 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler9257746f2008-09-03 11:26:32 +08003176 unsigned long val;
3177 u32 filter_flags;
3178 int ret = strict_strtoul(buf, 0, &val);
Emmanuel Grumbach926f0b22008-09-03 11:26:39 +08003179 if (ret)
Tomas Winkler9257746f2008-09-03 11:26:32 +08003180 return ret;
3181 filter_flags = (u32)val;
Zhu Yib481de92007-09-25 17:54:57 -07003182
3183 mutex_lock(&priv->mutex);
3184 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3185 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003186 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003187 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003188 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003189 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003190 "0x%04X\n", filter_flags);
3191 priv->staging_rxon.filter_flags =
3192 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003193 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003194 }
3195 }
3196 mutex_unlock(&priv->mutex);
3197
3198 return count;
3199}
3200
3201static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3202 store_filter_flags);
3203
Zhu Yib481de92007-09-25 17:54:57 -07003204
3205static ssize_t show_statistics(struct device *d,
3206 struct device_attribute *attr, char *buf)
3207{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003208 struct iwl_priv *priv = dev_get_drvdata(d);
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +08003209 u32 size = sizeof(struct iwl_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07003210 u32 len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003211 u8 *data = (u8 *)&priv->statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003212 int rc = 0;
3213
Tomas Winklerfee12472008-04-03 16:05:21 -07003214 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003215 return -EAGAIN;
3216
3217 mutex_lock(&priv->mutex);
Wey-Yi Guyef8d5522009-11-13 11:56:28 -08003218 rc = iwl_send_statistics_request(priv, CMD_SYNC, false);
Zhu Yib481de92007-09-25 17:54:57 -07003219 mutex_unlock(&priv->mutex);
3220
3221 if (rc) {
3222 len = sprintf(buf,
3223 "Error sending statistics request: 0x%08X\n", rc);
3224 return len;
3225 }
3226
3227 while (size && (PAGE_SIZE - len)) {
3228 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3229 PAGE_SIZE - len, 1);
3230 len = strlen(buf);
3231 if (PAGE_SIZE - len)
3232 buf[len++] = '\n';
3233
3234 ofs += 16;
3235 size -= min(size, 16U);
3236 }
3237
3238 return len;
3239}
3240
3241static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
3242
Wey-Yi Guy01abfbb2009-09-17 10:43:45 -07003243static ssize_t show_rts_ht_protection(struct device *d,
3244 struct device_attribute *attr, char *buf)
3245{
3246 struct iwl_priv *priv = dev_get_drvdata(d);
3247
3248 return sprintf(buf, "%s\n",
3249 priv->cfg->use_rts_for_ht ? "RTS/CTS" : "CTS-to-self");
3250}
3251
3252static ssize_t store_rts_ht_protection(struct device *d,
3253 struct device_attribute *attr,
3254 const char *buf, size_t count)
3255{
3256 struct iwl_priv *priv = dev_get_drvdata(d);
3257 unsigned long val;
3258 int ret;
3259
3260 ret = strict_strtoul(buf, 10, &val);
3261 if (ret)
3262 IWL_INFO(priv, "Input is not in decimal form.\n");
3263 else {
3264 if (!iwl_is_associated(priv))
3265 priv->cfg->use_rts_for_ht = val ? true : false;
3266 else
3267 IWL_ERR(priv, "Sta associated with AP - "
3268 "Change protection mechanism is not allowed\n");
3269 ret = count;
3270 }
3271 return ret;
3272}
3273
3274static DEVICE_ATTR(rts_ht_protection, S_IWUSR | S_IRUGO,
3275 show_rts_ht_protection, store_rts_ht_protection);
3276
Zhu Yib481de92007-09-25 17:54:57 -07003277
Zhu Yib481de92007-09-25 17:54:57 -07003278/*****************************************************************************
3279 *
3280 * driver setup and teardown
3281 *
3282 *****************************************************************************/
3283
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003284static void iwl_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003285{
Reinette Chatred21050c2009-02-13 11:51:18 -08003286 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003287
3288 init_waitqueue_head(&priv->wait_command_queue);
3289
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003290 INIT_WORK(&priv->up, iwl_bg_up);
3291 INIT_WORK(&priv->restart, iwl_bg_restart);
3292 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003293 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08003294 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08003295 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
3296 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
Tomas Winkler2a421b92008-06-12 09:47:10 +08003297
Tomas Winkler2a421b92008-06-12 09:47:10 +08003298 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003299
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003300 if (priv->cfg->ops->lib->setup_deferred_work)
3301 priv->cfg->ops->lib->setup_deferred_work(priv);
3302
3303 init_timer(&priv->statistics_periodic);
3304 priv->statistics_periodic.data = (unsigned long)priv;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003305 priv->statistics_periodic.function = iwl_bg_statistics_periodic;
Zhu Yib481de92007-09-25 17:54:57 -07003306
Wey-Yi Guya9e1cb62009-12-10 14:37:26 -08003307 init_timer(&priv->ucode_trace);
3308 priv->ucode_trace.data = (unsigned long)priv;
3309 priv->ucode_trace.function = iwl_bg_ucode_trace;
3310
Mohamed Abbasef850d72009-05-22 11:01:50 -07003311 if (!priv->cfg->use_isr_legacy)
3312 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
3313 iwl_irq_tasklet, (unsigned long)priv);
3314 else
3315 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
3316 iwl_irq_tasklet_legacy, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003317}
3318
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003319static void iwl_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003320{
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003321 if (priv->cfg->ops->lib->cancel_deferred_work)
3322 priv->cfg->ops->lib->cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003323
Joonwoo Park3ae6a052007-11-29 10:43:16 +09003324 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003325 cancel_delayed_work(&priv->scan_check);
3326 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003327 cancel_work_sync(&priv->beacon_update);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003328 del_timer_sync(&priv->statistics_periodic);
Wey-Yi Guya9e1cb62009-12-10 14:37:26 -08003329 del_timer_sync(&priv->ucode_trace);
Zhu Yib481de92007-09-25 17:54:57 -07003330}
3331
Reinette Chatre89f186a2009-10-30 14:36:11 -07003332static void iwl_init_hw_rates(struct iwl_priv *priv,
3333 struct ieee80211_rate *rates)
3334{
3335 int i;
3336
3337 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
3338 rates[i].bitrate = iwl_rates[i].ieee * 5;
3339 rates[i].hw_value = i; /* Rate scaling will work on indexes */
3340 rates[i].hw_value_short = i;
3341 rates[i].flags = 0;
3342 if ((i >= IWL_FIRST_CCK_RATE) && (i <= IWL_LAST_CCK_RATE)) {
3343 /*
3344 * If CCK != 1M then set short preamble rate flag.
3345 */
3346 rates[i].flags |=
3347 (iwl_rates[i].plcp == IWL_RATE_1M_PLCP) ?
3348 0 : IEEE80211_RATE_SHORT_PREAMBLE;
3349 }
3350 }
3351}
3352
3353static int iwl_init_drv(struct iwl_priv *priv)
3354{
3355 int ret;
3356
3357 priv->ibss_beacon = NULL;
3358
Reinette Chatre89f186a2009-10-30 14:36:11 -07003359 spin_lock_init(&priv->sta_lock);
3360 spin_lock_init(&priv->hcmd_lock);
3361
3362 INIT_LIST_HEAD(&priv->free_frames);
3363
3364 mutex_init(&priv->mutex);
3365
3366 /* Clear the driver's (not device's) station table */
3367 iwl_clear_stations_table(priv);
3368
3369 priv->ieee_channels = NULL;
3370 priv->ieee_rates = NULL;
3371 priv->band = IEEE80211_BAND_2GHZ;
3372
3373 priv->iw_mode = NL80211_IFTYPE_STATION;
Johannes Bergba37a3d2009-12-10 14:37:27 -08003374 priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003375 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Reinette Chatre89f186a2009-10-30 14:36:11 -07003376
3377 /* Choose which receivers/antennas to use */
3378 if (priv->cfg->ops->hcmd->set_rxon_chain)
3379 priv->cfg->ops->hcmd->set_rxon_chain(priv);
3380
3381 iwl_init_scan_params(priv);
3382
3383 iwl_reset_qos(priv);
3384
3385 priv->qos_data.qos_active = 0;
3386 priv->qos_data.qos_cap.val = 0;
3387
3388 priv->rates_mask = IWL_RATES_MASK;
3389 /* Set the tx_power_user_lmt to the lowest power level
3390 * this value will get overwritten by channel max power avg
3391 * from eeprom */
3392 priv->tx_power_user_lmt = IWL_TX_POWER_TARGET_POWER_MIN;
3393
3394 ret = iwl_init_channel_map(priv);
3395 if (ret) {
3396 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3397 goto err;
3398 }
3399
3400 ret = iwlcore_init_geos(priv);
3401 if (ret) {
3402 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3403 goto err_free_channel_map;
3404 }
3405 iwl_init_hw_rates(priv, priv->ieee_rates);
3406
3407 return 0;
3408
3409err_free_channel_map:
3410 iwl_free_channel_map(priv);
3411err:
3412 return ret;
3413}
3414
3415static void iwl_uninit_drv(struct iwl_priv *priv)
3416{
3417 iwl_calib_free_results(priv);
3418 iwlcore_free_geos(priv);
3419 iwl_free_channel_map(priv);
3420 kfree(priv->scan);
3421}
3422
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003423static struct attribute *iwl_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003424 &dev_attr_flags.attr,
3425 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003426 &dev_attr_statistics.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003427 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003428 &dev_attr_tx_power.attr,
Wey-Yi Guy01abfbb2009-09-17 10:43:45 -07003429 &dev_attr_rts_ht_protection.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08003430#ifdef CONFIG_IWLWIFI_DEBUG
3431 &dev_attr_debug_level.attr,
3432#endif
Zhu Yib481de92007-09-25 17:54:57 -07003433 NULL
3434};
3435
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003436static struct attribute_group iwl_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003437 .name = NULL, /* put in device directory */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003438 .attrs = iwl_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003439};
3440
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003441static struct ieee80211_ops iwl_hw_ops = {
3442 .tx = iwl_mac_tx,
3443 .start = iwl_mac_start,
3444 .stop = iwl_mac_stop,
3445 .add_interface = iwl_mac_add_interface,
3446 .remove_interface = iwl_mac_remove_interface,
3447 .config = iwl_mac_config,
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003448 .configure_filter = iwl_configure_filter,
3449 .set_key = iwl_mac_set_key,
3450 .update_tkip_key = iwl_mac_update_tkip_key,
3451 .get_stats = iwl_mac_get_stats,
3452 .get_tx_stats = iwl_mac_get_tx_stats,
3453 .conf_tx = iwl_mac_conf_tx,
3454 .reset_tsf = iwl_mac_reset_tsf,
3455 .bss_info_changed = iwl_bss_info_changed,
3456 .ampdu_action = iwl_mac_ampdu_action,
Johannes Berg6ab10ff2009-11-13 11:56:37 -08003457 .hw_scan = iwl_mac_hw_scan,
3458 .sta_notify = iwl_mac_sta_notify,
Zhu Yib481de92007-09-25 17:54:57 -07003459};
3460
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003461static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003462{
3463 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003464 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003465 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08003466 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003467 unsigned long flags;
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003468 u16 pci_cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003469
Assaf Krauss316c30d2008-03-14 10:38:46 -07003470 /************************
3471 * 1. Allocating HW data
3472 ************************/
3473
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003474 /* Disabling hardware scan means that mac80211 will perform scans
3475 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07003476 if (cfg->mod_params->disable_hw_scan) {
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003477 if (iwl_debug_level & IWL_DL_INFO)
Ester Kummerbf403db2008-05-05 10:22:40 +08003478 dev_printk(KERN_DEBUG, &(pdev->dev),
3479 "Disabling hw_scan\n");
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003480 iwl_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003481 }
3482
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003483 hw = iwl_alloc_all(cfg, &iwl_hw_ops);
Assaf Krauss1d0a0822008-03-14 10:38:48 -07003484 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07003485 err = -ENOMEM;
3486 goto out;
3487 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07003488 priv = hw->priv;
3489 /* At this point both hw and priv are allocated. */
3490
Zhu Yib481de92007-09-25 17:54:57 -07003491 SET_IEEE80211_DEV(hw, &pdev->dev);
3492
Tomas Winklere1623442009-01-27 14:27:56 -08003493 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08003494 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07003495 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07003496 priv->inta_mask = CSR_INI_SET_MASK;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003497
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003498#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003499 atomic_set(&priv->restrict_refcnt, 0);
3500#endif
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003501 if (iwl_alloc_traffic_mem(priv))
3502 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07003503
Assaf Krauss316c30d2008-03-14 10:38:46 -07003504 /**************************
3505 * 2. Initializing PCI bus
3506 **************************/
3507 if (pci_enable_device(pdev)) {
3508 err = -ENODEV;
3509 goto out_ieee80211_free_hw;
3510 }
3511
3512 pci_set_master(pdev);
3513
Winkler, Tomas093d874c2008-09-26 15:09:34 +08003514 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
Assaf Krauss316c30d2008-03-14 10:38:46 -07003515 if (!err)
Winkler, Tomas093d874c2008-09-26 15:09:34 +08003516 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003517 if (err) {
Winkler, Tomas093d874c2008-09-26 15:09:34 +08003518 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003519 if (!err)
Winkler, Tomas093d874c2008-09-26 15:09:34 +08003520 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003521 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003522 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08003523 IWL_WARN(priv, "No suitable DMA available.\n");
Assaf Krauss316c30d2008-03-14 10:38:46 -07003524 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003525 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07003526 }
3527
3528 err = pci_request_regions(pdev, DRV_NAME);
3529 if (err)
3530 goto out_pci_disable_device;
3531
3532 pci_set_drvdata(pdev, priv);
3533
Assaf Krauss316c30d2008-03-14 10:38:46 -07003534
3535 /***********************
3536 * 3. Read REV register
3537 ***********************/
3538 priv->hw_base = pci_iomap(pdev, 0, 0);
3539 if (!priv->hw_base) {
3540 err = -ENODEV;
3541 goto out_pci_release_regions;
3542 }
3543
Tomas Winklere1623442009-01-27 14:27:56 -08003544 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Assaf Krauss316c30d2008-03-14 10:38:46 -07003545 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08003546 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003547
Reinette Chatre731a29b2009-12-14 14:12:11 -08003548 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07003549 * we should init now
3550 */
3551 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08003552 spin_lock_init(&priv->lock);
Tomas Winklerb661c812008-04-23 17:14:54 -07003553 iwl_hw_detect(priv);
Tomas Winkler978785a2008-12-19 10:37:31 +08003554 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s REV=0x%X\n",
Tomas Winklerb661c812008-04-23 17:14:54 -07003555 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003556
Tomas Winklere7b63582008-09-03 11:26:49 +08003557 /* We disable the RETRY_TIMEOUT register (0x41) to keep
3558 * PCI Tx retries from interfering with C3 CPU state */
3559 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
3560
Mohamed Abbas086ed112009-05-22 11:01:54 -07003561 iwl_prepare_card_hw(priv);
3562 if (!priv->hw_ready) {
3563 IWL_WARN(priv, "Failed, HW not ready\n");
3564 goto out_iounmap;
3565 }
3566
Assaf Krauss316c30d2008-03-14 10:38:46 -07003567 /*****************
3568 * 4. Read EEPROM
3569 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07003570 /* Read the EEPROM */
3571 err = iwl_eeprom_init(priv);
3572 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003573 IWL_ERR(priv, "Unable to init EEPROM\n");
Assaf Krauss316c30d2008-03-14 10:38:46 -07003574 goto out_iounmap;
3575 }
Tomas Winkler8614f362008-04-23 17:14:55 -07003576 err = iwl_eeprom_check_version(priv);
3577 if (err)
Reinette Chatrec8f16132009-02-27 16:21:22 -08003578 goto out_free_eeprom;
Tomas Winkler8614f362008-04-23 17:14:55 -07003579
Ron Rindjunsky02883012008-05-15 13:53:53 +08003580 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003581 iwl_eeprom_get_mac(priv, priv->mac_addr);
Tomas Winklere1623442009-01-27 14:27:56 -08003582 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003583 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
3584
3585 /************************
3586 * 5. Setup HW constants
3587 ************************/
Ron Rindjunskyda154e302008-06-30 17:23:20 +08003588 if (iwl_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003589 IWL_ERR(priv, "failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07003590 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003591 }
3592
3593 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08003594 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07003595 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07003596
Tomas Winkler6ba87952008-05-15 13:54:17 +08003597 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003598 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07003599 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003600 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003601
Assaf Krauss316c30d2008-03-14 10:38:46 -07003602 /********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07003603 * 7. Setup services
Assaf Krauss316c30d2008-03-14 10:38:46 -07003604 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003605 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003606 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003607 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003608
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003609 pci_enable_msi(priv->pci_dev);
3610
Mohamed Abbasef850d72009-05-22 11:01:50 -07003611 iwl_alloc_isr_ict(priv);
3612 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
3613 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003614 if (err) {
3615 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
3616 goto out_disable_msi;
3617 }
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003618 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003619 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003620 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Helmut Schaa795cc0a2009-02-12 18:51:03 +01003621 goto out_free_irq;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003622 }
3623
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003624 iwl_setup_deferred_work(priv);
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08003625 iwl_setup_rx_handlers(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003626
Tomas Winkler6ba87952008-05-15 13:54:17 +08003627 /**********************************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07003628 * 8. Setup and register mac80211
Tomas Winkler6ba87952008-05-15 13:54:17 +08003629 **********************************/
3630
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003631 /* enable interrupts if needed: hw bug w/a */
3632 pci_read_config_word(priv->pci_dev, PCI_COMMAND, &pci_cmd);
3633 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
3634 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
3635 pci_write_config_word(priv->pci_dev, PCI_COMMAND, pci_cmd);
3636 }
3637
3638 iwl_enable_interrupts(priv);
3639
Tomas Winkler6ba87952008-05-15 13:54:17 +08003640 err = iwl_setup_mac(priv);
3641 if (err)
3642 goto out_remove_sysfs;
3643
3644 err = iwl_dbgfs_register(priv, DRV_NAME);
3645 if (err)
Abhijeet Kolekara75fbe82009-04-20 14:36:58 -07003646 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
Tomas Winkler6ba87952008-05-15 13:54:17 +08003647
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003648 /* If platform's RF_KILL switch is NOT set to KILL */
3649 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3650 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3651 else
3652 set_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003653
Johannes Berga60e77e2009-06-04 18:26:06 +02003654 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
3655 test_bit(STATUS_RF_KILL_HW, &priv->status));
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003656
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08003657 iwl_power_initialize(priv);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -07003658 iwl_tt_initialize(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003659 return 0;
3660
Assaf Krauss316c30d2008-03-14 10:38:46 -07003661 out_remove_sysfs:
Reinette Chatrec8f16132009-02-27 16:21:22 -08003662 destroy_workqueue(priv->workqueue);
3663 priv->workqueue = NULL;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003664 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
Helmut Schaa795cc0a2009-02-12 18:51:03 +01003665 out_free_irq:
3666 free_irq(priv->pci_dev->irq, priv);
Mohamed Abbasef850d72009-05-22 11:01:50 -07003667 iwl_free_isr_ict(priv);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003668 out_disable_msi:
3669 pci_disable_msi(priv->pci_dev);
Tomas Winkler6ba87952008-05-15 13:54:17 +08003670 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07003671 out_free_eeprom:
3672 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003673 out_iounmap:
3674 pci_iounmap(pdev, priv->hw_base);
3675 out_pci_release_regions:
Assaf Krauss316c30d2008-03-14 10:38:46 -07003676 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08003677 pci_release_regions(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07003678 out_pci_disable_device:
3679 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07003680 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003681 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07003682 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07003683 out:
3684 return err;
3685}
3686
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003687static void __devexit iwl_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07003688{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003689 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003690 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003691
3692 if (!priv)
3693 return;
3694
Tomas Winklere1623442009-01-27 14:27:56 -08003695 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07003696
Emmanuel Grumbach67249622008-05-29 16:35:26 +08003697 iwl_dbgfs_unregister(priv);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003698 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
Emmanuel Grumbach67249622008-05-29 16:35:26 +08003699
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003700 /* ieee80211_unregister_hw call wil cause iwl_mac_stop to
3701 * to be called and iwl_down since we are removing the device
Gregory Greenman0b124c32008-09-03 11:18:50 +08003702 * we need to set STATUS_EXIT_PENDING bit.
3703 */
3704 set_bit(STATUS_EXIT_PENDING, &priv->status);
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07003705 if (priv->mac80211_registered) {
3706 ieee80211_unregister_hw(priv->hw);
3707 priv->mac80211_registered = 0;
Gregory Greenman0b124c32008-09-03 11:18:50 +08003708 } else {
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003709 iwl_down(priv);
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07003710 }
3711
Ben Cahillc166b252009-10-23 13:42:35 -07003712 /*
3713 * Make sure device is reset to low power before unloading driver.
3714 * This may be redundant with iwl_down(), but there are paths to
3715 * run iwl_down() without calling apm_ops.stop(), and there are
3716 * paths to avoid running iwl_down() at all before leaving driver.
3717 * This (inexpensive) call *makes sure* device is reset.
3718 */
3719 priv->cfg->ops->lib->apm_ops.stop(priv);
3720
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -07003721 iwl_tt_exit(priv);
3722
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003723 /* make sure we flush any pending irq or
3724 * tasklet for the driver
3725 */
3726 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003727 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003728 spin_unlock_irqrestore(&priv->lock, flags);
3729
3730 iwl_synchronize_irq(priv);
3731
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003732 iwl_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003733
3734 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08003735 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08003736 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003737
Tomas Winklerc587de02009-06-03 11:44:07 -07003738 iwl_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07003739 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003740
Zhu Yib481de92007-09-25 17:54:57 -07003741
Mohamed Abbas948c1712007-10-25 17:15:45 +08003742 /*netif_stop_queue(dev); */
3743 flush_workqueue(priv->workqueue);
3744
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003745 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07003746 * priv->workqueue... so we can't take down the workqueue
3747 * until now... */
3748 destroy_workqueue(priv->workqueue);
3749 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003750 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003751
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003752 free_irq(priv->pci_dev->irq, priv);
3753 pci_disable_msi(priv->pci_dev);
Zhu Yib481de92007-09-25 17:54:57 -07003754 pci_iounmap(pdev, priv->hw_base);
3755 pci_release_regions(pdev);
3756 pci_disable_device(pdev);
3757 pci_set_drvdata(pdev, NULL);
3758
Tomas Winkler6ba87952008-05-15 13:54:17 +08003759 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003760
Mohamed Abbasef850d72009-05-22 11:01:50 -07003761 iwl_free_isr_ict(priv);
3762
Zhu Yib481de92007-09-25 17:54:57 -07003763 if (priv->ibss_beacon)
3764 dev_kfree_skb(priv->ibss_beacon);
3765
3766 ieee80211_free_hw(priv->hw);
3767}
3768
Zhu Yib481de92007-09-25 17:54:57 -07003769
3770/*****************************************************************************
3771 *
3772 * driver and module entry point
3773 *
3774 *****************************************************************************/
3775
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003776/* Hardware specific file defines the PCI IDs table for that hardware module */
3777static struct pci_device_id iwl_hw_card_ids[] = {
Tomas Winkler4fc22b22008-07-21 18:54:42 +03003778#ifdef CONFIG_IWL4965
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003779 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
3780 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler4fc22b22008-07-21 18:54:42 +03003781#endif /* CONFIG_IWL4965 */
Tomas Winkler5a6a2562008-04-24 11:55:23 -07003782#ifdef CONFIG_IWL5000
Wey-Yi Guyac592572009-11-20 12:05:03 -08003783/* 5100 Series WiFi */
3784 {IWL_PCI_DEVICE(0x4232, 0x1201, iwl5100_agn_cfg)}, /* Mini Card */
3785 {IWL_PCI_DEVICE(0x4232, 0x1301, iwl5100_agn_cfg)}, /* Half Mini Card */
3786 {IWL_PCI_DEVICE(0x4232, 0x1204, iwl5100_agn_cfg)}, /* Mini Card */
3787 {IWL_PCI_DEVICE(0x4232, 0x1304, iwl5100_agn_cfg)}, /* Half Mini Card */
3788 {IWL_PCI_DEVICE(0x4232, 0x1205, iwl5100_bgn_cfg)}, /* Mini Card */
3789 {IWL_PCI_DEVICE(0x4232, 0x1305, iwl5100_bgn_cfg)}, /* Half Mini Card */
3790 {IWL_PCI_DEVICE(0x4232, 0x1206, iwl5100_abg_cfg)}, /* Mini Card */
3791 {IWL_PCI_DEVICE(0x4232, 0x1306, iwl5100_abg_cfg)}, /* Half Mini Card */
3792 {IWL_PCI_DEVICE(0x4232, 0x1221, iwl5100_agn_cfg)}, /* Mini Card */
3793 {IWL_PCI_DEVICE(0x4232, 0x1321, iwl5100_agn_cfg)}, /* Half Mini Card */
3794 {IWL_PCI_DEVICE(0x4232, 0x1224, iwl5100_agn_cfg)}, /* Mini Card */
3795 {IWL_PCI_DEVICE(0x4232, 0x1324, iwl5100_agn_cfg)}, /* Half Mini Card */
3796 {IWL_PCI_DEVICE(0x4232, 0x1225, iwl5100_bgn_cfg)}, /* Mini Card */
3797 {IWL_PCI_DEVICE(0x4232, 0x1325, iwl5100_bgn_cfg)}, /* Half Mini Card */
3798 {IWL_PCI_DEVICE(0x4232, 0x1226, iwl5100_abg_cfg)}, /* Mini Card */
3799 {IWL_PCI_DEVICE(0x4232, 0x1326, iwl5100_abg_cfg)}, /* Half Mini Card */
3800 {IWL_PCI_DEVICE(0x4237, 0x1211, iwl5100_agn_cfg)}, /* Mini Card */
3801 {IWL_PCI_DEVICE(0x4237, 0x1311, iwl5100_agn_cfg)}, /* Half Mini Card */
3802 {IWL_PCI_DEVICE(0x4237, 0x1214, iwl5100_agn_cfg)}, /* Mini Card */
3803 {IWL_PCI_DEVICE(0x4237, 0x1314, iwl5100_agn_cfg)}, /* Half Mini Card */
3804 {IWL_PCI_DEVICE(0x4237, 0x1215, iwl5100_bgn_cfg)}, /* Mini Card */
3805 {IWL_PCI_DEVICE(0x4237, 0x1315, iwl5100_bgn_cfg)}, /* Half Mini Card */
3806 {IWL_PCI_DEVICE(0x4237, 0x1216, iwl5100_abg_cfg)}, /* Mini Card */
3807 {IWL_PCI_DEVICE(0x4237, 0x1316, iwl5100_abg_cfg)}, /* Half Mini Card */
3808
3809/* 5300 Series WiFi */
3810 {IWL_PCI_DEVICE(0x4235, 0x1021, iwl5300_agn_cfg)}, /* Mini Card */
3811 {IWL_PCI_DEVICE(0x4235, 0x1121, iwl5300_agn_cfg)}, /* Half Mini Card */
3812 {IWL_PCI_DEVICE(0x4235, 0x1024, iwl5300_agn_cfg)}, /* Mini Card */
3813 {IWL_PCI_DEVICE(0x4235, 0x1124, iwl5300_agn_cfg)}, /* Half Mini Card */
3814 {IWL_PCI_DEVICE(0x4235, 0x1001, iwl5300_agn_cfg)}, /* Mini Card */
3815 {IWL_PCI_DEVICE(0x4235, 0x1101, iwl5300_agn_cfg)}, /* Half Mini Card */
3816 {IWL_PCI_DEVICE(0x4235, 0x1004, iwl5300_agn_cfg)}, /* Mini Card */
3817 {IWL_PCI_DEVICE(0x4235, 0x1104, iwl5300_agn_cfg)}, /* Half Mini Card */
3818 {IWL_PCI_DEVICE(0x4236, 0x1011, iwl5300_agn_cfg)}, /* Mini Card */
3819 {IWL_PCI_DEVICE(0x4236, 0x1111, iwl5300_agn_cfg)}, /* Half Mini Card */
3820 {IWL_PCI_DEVICE(0x4236, 0x1014, iwl5300_agn_cfg)}, /* Mini Card */
3821 {IWL_PCI_DEVICE(0x4236, 0x1114, iwl5300_agn_cfg)}, /* Half Mini Card */
3822
3823/* 5350 Series WiFi/WiMax */
3824 {IWL_PCI_DEVICE(0x423A, 0x1001, iwl5350_agn_cfg)}, /* Mini Card */
3825 {IWL_PCI_DEVICE(0x423A, 0x1021, iwl5350_agn_cfg)}, /* Mini Card */
3826 {IWL_PCI_DEVICE(0x423B, 0x1011, iwl5350_agn_cfg)}, /* Mini Card */
3827
3828/* 5150 Series Wifi/WiMax */
3829 {IWL_PCI_DEVICE(0x423C, 0x1201, iwl5150_agn_cfg)}, /* Mini Card */
3830 {IWL_PCI_DEVICE(0x423C, 0x1301, iwl5150_agn_cfg)}, /* Half Mini Card */
3831 {IWL_PCI_DEVICE(0x423C, 0x1206, iwl5150_abg_cfg)}, /* Mini Card */
3832 {IWL_PCI_DEVICE(0x423C, 0x1306, iwl5150_abg_cfg)}, /* Half Mini Card */
3833 {IWL_PCI_DEVICE(0x423C, 0x1221, iwl5150_agn_cfg)}, /* Mini Card */
3834 {IWL_PCI_DEVICE(0x423C, 0x1321, iwl5150_agn_cfg)}, /* Half Mini Card */
3835
3836 {IWL_PCI_DEVICE(0x423D, 0x1211, iwl5150_agn_cfg)}, /* Mini Card */
3837 {IWL_PCI_DEVICE(0x423D, 0x1311, iwl5150_agn_cfg)}, /* Half Mini Card */
3838 {IWL_PCI_DEVICE(0x423D, 0x1216, iwl5150_abg_cfg)}, /* Mini Card */
3839 {IWL_PCI_DEVICE(0x423D, 0x1316, iwl5150_abg_cfg)}, /* Half Mini Card */
Wey-Yi Guy5953a622009-09-17 10:43:53 -07003840
3841/* 6x00 Series */
Wey-Yi Guy5953a622009-09-17 10:43:53 -07003842 {IWL_PCI_DEVICE(0x422B, 0x1101, iwl6000_3agn_cfg)},
3843 {IWL_PCI_DEVICE(0x422B, 0x1121, iwl6000_3agn_cfg)},
3844 {IWL_PCI_DEVICE(0x422C, 0x1301, iwl6000i_2agn_cfg)},
3845 {IWL_PCI_DEVICE(0x422C, 0x1306, iwl6000i_2abg_cfg)},
3846 {IWL_PCI_DEVICE(0x422C, 0x1307, iwl6000i_2bg_cfg)},
3847 {IWL_PCI_DEVICE(0x422C, 0x1321, iwl6000i_2agn_cfg)},
3848 {IWL_PCI_DEVICE(0x422C, 0x1326, iwl6000i_2abg_cfg)},
3849 {IWL_PCI_DEVICE(0x4238, 0x1111, iwl6000_3agn_cfg)},
3850 {IWL_PCI_DEVICE(0x4239, 0x1311, iwl6000i_2agn_cfg)},
3851 {IWL_PCI_DEVICE(0x4239, 0x1316, iwl6000i_2abg_cfg)},
3852
3853/* 6x50 WiFi/WiMax Series */
Wey-Yi Guy5953a622009-09-17 10:43:53 -07003854 {IWL_PCI_DEVICE(0x0087, 0x1301, iwl6050_2agn_cfg)},
3855 {IWL_PCI_DEVICE(0x0087, 0x1306, iwl6050_2abg_cfg)},
3856 {IWL_PCI_DEVICE(0x0087, 0x1321, iwl6050_2agn_cfg)},
3857 {IWL_PCI_DEVICE(0x0087, 0x1326, iwl6050_2abg_cfg)},
Wey-Yi Guy5953a622009-09-17 10:43:53 -07003858 {IWL_PCI_DEVICE(0x0089, 0x1311, iwl6050_2agn_cfg)},
3859 {IWL_PCI_DEVICE(0x0089, 0x1316, iwl6050_2abg_cfg)},
3860
Jay Sternberg77dcb6a2009-03-06 13:52:55 -08003861/* 1000 Series WiFi */
Wey-Yi Guy4bd0914f2009-09-17 10:43:52 -07003862 {IWL_PCI_DEVICE(0x0083, 0x1205, iwl1000_bgn_cfg)},
3863 {IWL_PCI_DEVICE(0x0083, 0x1305, iwl1000_bgn_cfg)},
3864 {IWL_PCI_DEVICE(0x0083, 0x1225, iwl1000_bgn_cfg)},
3865 {IWL_PCI_DEVICE(0x0083, 0x1325, iwl1000_bgn_cfg)},
3866 {IWL_PCI_DEVICE(0x0084, 0x1215, iwl1000_bgn_cfg)},
3867 {IWL_PCI_DEVICE(0x0084, 0x1315, iwl1000_bgn_cfg)},
3868 {IWL_PCI_DEVICE(0x0083, 0x1206, iwl1000_bg_cfg)},
3869 {IWL_PCI_DEVICE(0x0083, 0x1306, iwl1000_bg_cfg)},
3870 {IWL_PCI_DEVICE(0x0083, 0x1226, iwl1000_bg_cfg)},
3871 {IWL_PCI_DEVICE(0x0083, 0x1326, iwl1000_bg_cfg)},
3872 {IWL_PCI_DEVICE(0x0084, 0x1216, iwl1000_bg_cfg)},
3873 {IWL_PCI_DEVICE(0x0084, 0x1316, iwl1000_bg_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07003874#endif /* CONFIG_IWL5000 */
Tomas Winkler7100e922008-12-01 16:32:18 -08003875
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003876 {0}
3877};
3878MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
3879
3880static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07003881 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003882 .id_table = iwl_hw_card_ids,
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003883 .probe = iwl_pci_probe,
3884 .remove = __devexit_p(iwl_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07003885#ifdef CONFIG_PM
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003886 .suspend = iwl_pci_suspend,
3887 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07003888#endif
3889};
3890
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003891static int __init iwl_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07003892{
3893
3894 int ret;
3895 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
3896 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003897
Tomas Winklere227cea2008-07-18 13:53:05 +08003898 ret = iwlagn_rate_control_register();
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003899 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003900 printk(KERN_ERR DRV_NAME
3901 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003902 return ret;
3903 }
3904
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003905 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07003906 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003907 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003908 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07003909 }
Zhu Yib481de92007-09-25 17:54:57 -07003910
3911 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003912
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003913error_register:
Tomas Winklere227cea2008-07-18 13:53:05 +08003914 iwlagn_rate_control_unregister();
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003915 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003916}
3917
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003918static void __exit iwl_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07003919{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003920 pci_unregister_driver(&iwl_driver);
Tomas Winklere227cea2008-07-18 13:53:05 +08003921 iwlagn_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07003922}
3923
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003924module_exit(iwl_exit);
3925module_init(iwl_init);
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003926
3927#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07003928module_param_named(debug50, iwl_debug_level, uint, S_IRUGO);
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003929MODULE_PARM_DESC(debug50, "50XX debug output mask (deprecated)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07003930module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003931MODULE_PARM_DESC(debug, "debug output mask");
3932#endif
3933