blob: d4500b5e0175380a7421c169e8795f62c34fdbed [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 Guy0924e512009-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 Chatre3459ab52010-01-22 14:22:49 -0800206 priv->cfg->ops->lib->add_bcast_station(priv);
207
Zhu Yib481de92007-09-25 17:54:57 -0700208
209 /* If we have set the ASSOC_MSK and we are in BSS mode then
210 * add the IWL_AP_ID to the station rate table */
Tomas Winkler91851592008-06-30 17:23:14 +0800211 if (new_assoc) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200212 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Tomas Winkler91851592008-06-30 17:23:14 +0800213 ret = iwl_rxon_add_station(priv,
214 priv->active_rxon.bssid_addr, 1);
215 if (ret == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800216 IWL_ERR(priv,
217 "Error adding AP address for TX.\n");
Tomas Winkler91851592008-06-30 17:23:14 +0800218 return -EIO;
219 }
220 priv->assoc_station_added = 1;
221 if (priv->default_wep_key &&
222 iwl_send_static_wepkey_cmd(priv, 0))
Winkler, Tomas15b16872008-12-19 10:37:33 +0800223 IWL_ERR(priv,
224 "Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700225 }
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800226
Wey-Yi Guy47eef9b2009-09-17 10:43:44 -0700227 /*
228 * allow CTS-to-self if possible for new association.
229 * this is relevant only for 5000 series and up,
230 * but will not damage 4965
231 */
232 priv->staging_rxon.flags |= RXON_FLG_SELF_CTS_EN;
233
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800234 /* Apply the new configuration
235 * RXON assoc doesn't clear the station table in uCode,
236 */
237 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
238 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
239 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800240 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
Emmanuel Grumbach43d59b32008-06-30 17:23:06 +0800241 return ret;
242 }
243 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700244 }
Wey-Yi Guya6435652009-11-06 14:52:46 -0800245 iwl_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700246
Zhu Yi36da7d72008-07-11 11:53:40 +0800247 iwl_init_sensitivity(priv);
248
249 /* If we issue a new RXON command which required a tune then we must
250 * send a new TXPOWER command or we won't be able to Tx any frames */
251 ret = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
252 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800253 IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
Zhu Yi36da7d72008-07-11 11:53:40 +0800254 return ret;
255 }
256
Zhu Yib481de92007-09-25 17:54:57 -0700257 return 0;
258}
259
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700260void iwl_update_chain_flags(struct iwl_priv *priv)
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700261{
262
Abhijeet Kolekar45823532009-04-08 11:26:44 -0700263 if (priv->cfg->ops->hcmd->set_rxon_chain)
264 priv->cfg->ops->hcmd->set_rxon_chain(priv);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -0700265 iwlcore_commit_rxon(priv);
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700266}
267
Tomas Winklerfcab4232008-05-15 13:54:01 +0800268static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700269{
270 struct list_head *element;
271
Tomas Winklere1623442009-01-27 14:27:56 -0800272 IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
Zhu Yib481de92007-09-25 17:54:57 -0700273 priv->frames_count);
274
275 while (!list_empty(&priv->free_frames)) {
276 element = priv->free_frames.next;
277 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800278 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700279 priv->frames_count--;
280 }
281
282 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800283 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -0700284 priv->frames_count);
285 priv->frames_count = 0;
286 }
287}
288
Tomas Winklerfcab4232008-05-15 13:54:01 +0800289static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700290{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800291 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700292 struct list_head *element;
293 if (list_empty(&priv->free_frames)) {
294 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
295 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800296 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -0700297 return NULL;
298 }
299
300 priv->frames_count++;
301 return frame;
302 }
303
304 element = priv->free_frames.next;
305 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800306 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700307}
308
Tomas Winklerfcab4232008-05-15 13:54:01 +0800309static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700310{
311 memset(frame, 0, sizeof(*frame));
312 list_add(&frame->list, &priv->free_frames);
313}
314
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800315static u32 iwl_fill_beacon_frame(struct iwl_priv *priv,
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800316 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +0200317 int left)
Zhu Yib481de92007-09-25 17:54:57 -0700318{
Tomas Winkler3109ece2008-03-28 16:33:35 -0700319 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200320 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
321 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -0700322 return 0;
323
324 if (priv->ibss_beacon->len > left)
325 return 0;
326
327 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
328
329 return priv->ibss_beacon->len;
330}
331
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800332/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
333static void iwl_set_beacon_tim(struct iwl_priv *priv,
334 struct iwl_tx_beacon_cmd *tx_beacon_cmd,
335 u8 *beacon, u32 frame_size)
336{
337 u16 tim_idx;
338 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
339
340 /*
341 * The index is relative to frame start but we start looking at the
342 * variable-length part of the beacon.
343 */
344 tim_idx = mgmt->u.beacon.variable - beacon;
345
346 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
347 while ((tim_idx < (frame_size - 2)) &&
348 (beacon[tim_idx] != WLAN_EID_TIM))
349 tim_idx += beacon[tim_idx+1] + 2;
350
351 /* If TIM field was found, set variables */
352 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
353 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
354 tx_beacon_cmd->tim_size = beacon[tim_idx+1];
355 } else
356 IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
357}
358
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700359static unsigned int iwl_hw_get_beacon_cmd(struct iwl_priv *priv,
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800360 struct iwl_frame *frame)
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800361{
362 struct iwl_tx_beacon_cmd *tx_beacon_cmd;
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800363 u32 frame_size;
364 u32 rate_flags;
365 u32 rate;
366 /*
367 * We have to set up the TX command, the TX Beacon command, and the
368 * beacon contents.
369 */
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800370
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800371 /* Initialize memory */
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800372 tx_beacon_cmd = &frame->u.beacon;
373 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
374
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800375 /* Set up TX beacon contents */
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800376 frame_size = iwl_fill_beacon_frame(priv, tx_beacon_cmd->frame,
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800377 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800378 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
379 return 0;
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800380
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800381 /* Set up TX command fields */
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800382 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800383 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
384 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800385 tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800386 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
387
388 /* Set up TX beacon command fields */
389 iwl_set_beacon_tim(priv, tx_beacon_cmd, (u8 *)tx_beacon_cmd->frame,
390 frame_size);
391
392 /* Set up packet rate and flags */
393 rate = iwl_rate_get_lowest_plcp(priv);
394 priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant);
395 rate_flags = iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
396 if ((rate >= IWL_FIRST_CCK_RATE) && (rate <= IWL_LAST_CCK_RATE))
397 rate_flags |= RATE_MCS_CCK_MSK;
398 tx_beacon_cmd->tx.rate_n_flags = iwl_hw_set_rate_n_flags(rate,
399 rate_flags);
Tomas Winkler4bf64ef2008-07-18 13:53:03 +0800400
401 return sizeof(*tx_beacon_cmd) + frame_size;
402}
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700403static int iwl_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700404{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800405 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700406 unsigned int frame_size;
407 int rc;
Zhu Yib481de92007-09-25 17:54:57 -0700408
Tomas Winklerfcab4232008-05-15 13:54:01 +0800409 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700410 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800411 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -0700412 "command.\n");
413 return -ENOMEM;
414 }
415
Daniel C Halperin47ff65c2009-11-13 11:56:33 -0800416 frame_size = iwl_hw_get_beacon_cmd(priv, frame);
417 if (!frame_size) {
418 IWL_ERR(priv, "Error configuring the beacon command\n");
419 iwl_free_frame(priv, frame);
420 return -EINVAL;
421 }
Zhu Yib481de92007-09-25 17:54:57 -0700422
Tomas Winkler857485c2008-03-21 13:53:44 -0700423 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700424 &frame->u.cmd[0]);
425
Tomas Winklerfcab4232008-05-15 13:54:01 +0800426 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700427
428 return rc;
429}
430
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800431static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
432{
433 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
434
435 dma_addr_t addr = get_unaligned_le32(&tb->lo);
436 if (sizeof(dma_addr_t) > sizeof(u32))
437 addr |=
438 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
439
440 return addr;
441}
442
443static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
444{
445 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
446
447 return le16_to_cpu(tb->hi_n_len) >> 4;
448}
449
450static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
451 dma_addr_t addr, u16 len)
452{
453 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
454 u16 hi_n_len = len << 4;
455
456 put_unaligned_le32(addr, &tb->lo);
457 if (sizeof(dma_addr_t) > sizeof(u32))
458 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
459
460 tb->hi_n_len = cpu_to_le16(hi_n_len);
461
462 tfd->num_tbs = idx + 1;
463}
464
465static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
466{
467 return tfd->num_tbs & 0x1f;
468}
469
470/**
471 * iwl_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
472 * @priv - driver private data
473 * @txq - tx queue
474 *
475 * Does NOT advance any TFD circular buffer read/write indexes
476 * Does NOT free the TFD itself (which is within circular buffer)
477 */
478void iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
479{
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800480 struct iwl_tfd *tfd_tmp = (struct iwl_tfd *)txq->tfds;
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800481 struct iwl_tfd *tfd;
482 struct pci_dev *dev = priv->pci_dev;
483 int index = txq->q.read_ptr;
484 int i;
485 int num_tbs;
486
487 tfd = &tfd_tmp[index];
488
489 /* Sanity check on number of chunks */
490 num_tbs = iwl_tfd_get_num_tbs(tfd);
491
492 if (num_tbs >= IWL_NUM_OF_TBS) {
493 IWL_ERR(priv, "Too many chunks: %i\n", num_tbs);
494 /* @todo issue fatal error, it is quite serious situation */
495 return;
496 }
497
498 /* Unmap tx_cmd */
499 if (num_tbs)
500 pci_unmap_single(dev,
Johannes Bergc2acea82009-07-24 11:13:05 -0700501 pci_unmap_addr(&txq->meta[index], mapping),
502 pci_unmap_len(&txq->meta[index], len),
Fenghua Yu96891ce2009-02-18 15:54:33 -0800503 PCI_DMA_BIDIRECTIONAL);
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800504
505 /* Unmap chunks, if any. */
506 for (i = 1; i < num_tbs; i++) {
507 pci_unmap_single(dev, iwl_tfd_tb_get_addr(tfd, i),
508 iwl_tfd_tb_get_len(tfd, i), PCI_DMA_TODEVICE);
509
510 if (txq->txb) {
511 dev_kfree_skb(txq->txb[txq->q.read_ptr].skb[i - 1]);
512 txq->txb[txq->q.read_ptr].skb[i - 1] = NULL;
513 }
514 }
515}
516
517int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv,
518 struct iwl_tx_queue *txq,
519 dma_addr_t addr, u16 len,
520 u8 reset, u8 pad)
521{
522 struct iwl_queue *q;
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800523 struct iwl_tfd *tfd, *tfd_tmp;
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800524 u32 num_tbs;
525
526 q = &txq->q;
Samuel Ortiz59606ff2009-01-23 13:45:13 -0800527 tfd_tmp = (struct iwl_tfd *)txq->tfds;
528 tfd = &tfd_tmp[q->write_ptr];
Samuel Ortiz7aaa1d72009-01-19 15:30:26 -0800529
530 if (reset)
531 memset(tfd, 0, sizeof(*tfd));
532
533 num_tbs = iwl_tfd_get_num_tbs(tfd);
534
535 /* Each TFD can point to a maximum 20 Tx buffers */
536 if (num_tbs >= IWL_NUM_OF_TBS) {
537 IWL_ERR(priv, "Error can not send more than %d chunks\n",
538 IWL_NUM_OF_TBS);
539 return -EINVAL;
540 }
541
542 BUG_ON(addr & ~DMA_BIT_MASK(36));
543 if (unlikely(addr & ~IWL_TX_DMA_MASK))
544 IWL_ERR(priv, "Unaligned address = %llx\n",
545 (unsigned long long)addr);
546
547 iwl_tfd_set_tb(tfd, num_tbs, addr, len);
548
549 return 0;
550}
551
Samuel Ortiza8e74e22009-01-23 13:45:14 -0800552/*
553 * Tell nic where to find circular buffer of Tx Frame Descriptors for
554 * given Tx queue, and enable the DMA channel used for that queue.
555 *
556 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
557 * channels supported in hardware.
558 */
559int iwl_hw_tx_queue_init(struct iwl_priv *priv,
560 struct iwl_tx_queue *txq)
561{
Samuel Ortiza8e74e22009-01-23 13:45:14 -0800562 int txq_id = txq->q.id;
563
Samuel Ortiza8e74e22009-01-23 13:45:14 -0800564 /* Circular buffer (TFD queue in DRAM) physical base address */
565 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
566 txq->q.dma_addr >> 8);
567
Samuel Ortiza8e74e22009-01-23 13:45:14 -0800568 return 0;
569}
570
Zhu Yib481de92007-09-25 17:54:57 -0700571/******************************************************************************
572 *
573 * Generic RX handler implementations
574 *
575 ******************************************************************************/
Tomas Winkler885ba202008-05-29 16:34:55 +0800576static void iwl_rx_reply_alive(struct iwl_priv *priv,
577 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700578{
Zhu Yi2f301222009-10-09 17:19:45 +0800579 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler885ba202008-05-29 16:34:55 +0800580 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -0700581 struct delayed_work *pwork;
582
583 palive = &pkt->u.alive_frame;
584
Tomas Winklere1623442009-01-27 14:27:56 -0800585 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
Zhu Yib481de92007-09-25 17:54:57 -0700586 "0x%01X 0x%01X\n",
587 palive->is_valid, palive->ver_type,
588 palive->ver_subtype);
589
590 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Tomas Winklere1623442009-01-27 14:27:56 -0800591 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700592 memcpy(&priv->card_alive_init,
593 &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +0800594 sizeof(struct iwl_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700595 pwork = &priv->init_alive_start;
596 } else {
Tomas Winklere1623442009-01-27 14:27:56 -0800597 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700598 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler885ba202008-05-29 16:34:55 +0800599 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -0700600 pwork = &priv->alive_start;
601 }
602
603 /* We delay the ALIVE response by 5ms to
604 * give the HW RF Kill time to activate... */
605 if (palive->is_valid == UCODE_VALID_OK)
606 queue_delayed_work(priv->workqueue, pwork,
607 msecs_to_jiffies(5));
608 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800609 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700610}
611
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700612static void iwl_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -0700613{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700614 struct iwl_priv *priv =
615 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -0700616 struct sk_buff *beacon;
617
618 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +0200619 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -0700620
621 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800622 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -0700623 return;
624 }
625
626 mutex_lock(&priv->mutex);
627 /* new beacon skb is allocated every time; dispose previous.*/
628 if (priv->ibss_beacon)
629 dev_kfree_skb(priv->ibss_beacon);
630
631 priv->ibss_beacon = beacon;
632 mutex_unlock(&priv->mutex);
633
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700634 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700635}
636
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800637/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700638 * iwl_bg_statistics_periodic - Timer callback to queue statistics
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800639 *
640 * This callback is provided in order to send a statistics request.
641 *
642 * This timer function is continually reset to execute within
643 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
644 * was received. We need to ensure we receive the statistics in order
645 * to update the temperature used for calibrating the TXPOWER.
646 */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700647static void iwl_bg_statistics_periodic(unsigned long data)
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800648{
649 struct iwl_priv *priv = (struct iwl_priv *)data;
650
651 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
652 return;
653
Mohamed Abbas61780ee2008-10-29 14:05:49 -0700654 /* dont send host command if rf-kill is on */
655 if (!iwl_is_ready_rf(priv))
656 return;
657
Wey-Yi Guyef8d5522009-11-13 11:56:28 -0800658 iwl_send_statistics_request(priv, CMD_ASYNC, false);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +0800659}
660
Wey-Yi Guya9e1cb62009-12-10 14:37:26 -0800661
662static void iwl_print_cont_event_trace(struct iwl_priv *priv, u32 base,
663 u32 start_idx, u32 num_events,
664 u32 mode)
665{
666 u32 i;
667 u32 ptr; /* SRAM byte address of log data */
668 u32 ev, time, data; /* event log data */
669 unsigned long reg_flags;
670
671 if (mode == 0)
672 ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
673 else
674 ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));
675
676 /* Make sure device is powered up for SRAM reads */
677 spin_lock_irqsave(&priv->reg_lock, reg_flags);
678 if (iwl_grab_nic_access(priv)) {
679 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
680 return;
681 }
682
683 /* Set starting address; reads will auto-increment */
684 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
685 rmb();
686
687 /*
688 * "time" is actually "data" for mode 0 (no timestamp).
689 * place event id # at far right for easier visual parsing.
690 */
691 for (i = 0; i < num_events; i++) {
692 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
693 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
694 if (mode == 0) {
695 trace_iwlwifi_dev_ucode_cont_event(priv,
696 0, time, ev);
697 } else {
698 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
699 trace_iwlwifi_dev_ucode_cont_event(priv,
700 time, data, ev);
701 }
702 }
703 /* Allow device to power down */
704 iwl_release_nic_access(priv);
705 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
706}
707
Johannes Berg875295f2010-01-22 14:22:55 -0800708static void iwl_continuous_event_trace(struct iwl_priv *priv)
Wey-Yi Guya9e1cb62009-12-10 14:37:26 -0800709{
710 u32 capacity; /* event log capacity in # entries */
711 u32 base; /* SRAM byte address of event log header */
712 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
713 u32 num_wraps; /* # times uCode wrapped to top of log */
714 u32 next_entry; /* index of next entry to be written by uCode */
715
716 if (priv->ucode_type == UCODE_INIT)
717 base = le32_to_cpu(priv->card_alive_init.error_event_table_ptr);
718 else
719 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
720 if (priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
721 capacity = iwl_read_targ_mem(priv, base);
722 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
723 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
724 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
725 } else
726 return;
727
728 if (num_wraps == priv->event_log.num_wraps) {
729 iwl_print_cont_event_trace(priv,
730 base, priv->event_log.next_entry,
731 next_entry - priv->event_log.next_entry,
732 mode);
733 priv->event_log.non_wraps_count++;
734 } else {
735 if ((num_wraps - priv->event_log.num_wraps) > 1)
736 priv->event_log.wraps_more_count++;
737 else
738 priv->event_log.wraps_once_count++;
739 trace_iwlwifi_dev_ucode_wrap_event(priv,
740 num_wraps - priv->event_log.num_wraps,
741 next_entry, priv->event_log.next_entry);
742 if (next_entry < priv->event_log.next_entry) {
743 iwl_print_cont_event_trace(priv, base,
744 priv->event_log.next_entry,
745 capacity - priv->event_log.next_entry,
746 mode);
747
748 iwl_print_cont_event_trace(priv, base, 0,
749 next_entry, mode);
750 } else {
751 iwl_print_cont_event_trace(priv, base,
752 next_entry, capacity - next_entry,
753 mode);
754
755 iwl_print_cont_event_trace(priv, base, 0,
756 next_entry, mode);
757 }
758 }
759 priv->event_log.num_wraps = num_wraps;
760 priv->event_log.next_entry = next_entry;
761}
762
763/**
764 * iwl_bg_ucode_trace - Timer callback to log ucode event
765 *
766 * The timer is continually set to execute every
767 * UCODE_TRACE_PERIOD milliseconds after the last timer expired
768 * this function is to perform continuous uCode event logging operation
769 * if enabled
770 */
771static void iwl_bg_ucode_trace(unsigned long data)
772{
773 struct iwl_priv *priv = (struct iwl_priv *)data;
774
775 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
776 return;
777
778 if (priv->event_log.ucode_trace) {
779 iwl_continuous_event_trace(priv);
780 /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
781 mod_timer(&priv->ucode_trace,
782 jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
783 }
784}
785
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700786static void iwl_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +0800787 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700788{
Tomas Winkler0a6857e2008-03-12 16:58:49 -0700789#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800790 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2aa6ab82008-12-11 10:33:40 -0800791 struct iwl4965_beacon_notif *beacon =
792 (struct iwl4965_beacon_notif *)pkt->u.raw;
Tomas Winklere7d326a2008-06-12 09:47:11 +0800793 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700794
Tomas Winklere1623442009-01-27 14:27:56 -0800795 IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
Zhu Yib481de92007-09-25 17:54:57 -0700796 "tsf %d %d rate %d\n",
Tomas Winkler25a65722008-06-12 09:47:07 +0800797 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
Zhu Yib481de92007-09-25 17:54:57 -0700798 beacon->beacon_notify_hdr.failure_frame,
799 le32_to_cpu(beacon->ibss_mgr_status),
800 le32_to_cpu(beacon->high_tsf),
801 le32_to_cpu(beacon->low_tsf), rate);
802#endif
803
Johannes Berg05c914f2008-09-11 00:01:58 +0200804 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -0700805 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
806 queue_work(priv->workqueue, &priv->beacon_update);
807}
808
Zhu Yib481de92007-09-25 17:54:57 -0700809/* Handle notification from uCode that card's power state is changing
810 * due to software, hardware, or critical temperature RFKILL */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700811static void iwl_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +0800812 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -0700813{
Zhu Yi2f301222009-10-09 17:19:45 +0800814 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700815 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
816 unsigned long status = priv->status;
817
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800818 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700819 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800820 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
821 (flags & CT_CARD_DISABLED) ?
822 "Reached" : "Not reached");
Zhu Yib481de92007-09-25 17:54:57 -0700823
824 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800825 CT_CARD_DISABLED)) {
Zhu Yib481de92007-09-25 17:54:57 -0700826
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700827 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -0700828 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
829
Mohamed Abbasa8b50a02009-05-22 11:01:47 -0700830 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
831 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Zhu Yib481de92007-09-25 17:54:57 -0700832
833 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700834 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -0700835 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Mohamed Abbasa8b50a02009-05-22 11:01:47 -0700836 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
Zhu Yib481de92007-09-25 17:54:57 -0700837 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Zhu Yib481de92007-09-25 17:54:57 -0700838 }
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800839 if (flags & CT_CARD_DISABLED)
Wey-Yi Guy39b73fb2009-07-24 11:13:02 -0700840 iwl_tt_enter_ct_kill(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700841 }
Wey-Yi Guy3a41bbd2009-12-10 14:37:24 -0800842 if (!(flags & CT_CARD_DISABLED))
Wey-Yi Guy39b73fb2009-07-24 11:13:02 -0700843 iwl_tt_exit_ct_kill(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700844
845 if (flags & HW_CARD_DISABLED)
846 set_bit(STATUS_RF_KILL_HW, &priv->status);
847 else
848 clear_bit(STATUS_RF_KILL_HW, &priv->status);
849
850
Zhu Yib481de92007-09-25 17:54:57 -0700851 if (!(flags & RXON_CARD_DISABLED))
Tomas Winkler2a421b92008-06-12 09:47:10 +0800852 iwl_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700853
854 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Johannes Berga60e77e2009-06-04 18:26:06 +0200855 test_bit(STATUS_RF_KILL_HW, &priv->status)))
856 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
857 test_bit(STATUS_RF_KILL_HW, &priv->status));
Zhu Yib481de92007-09-25 17:54:57 -0700858 else
859 wake_up_interruptible(&priv->wait_command_queue);
860}
861
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700862int iwl_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
Tomas Winklere2e3c572008-07-18 13:53:04 +0800863{
Tomas Winklere2e3c572008-07-18 13:53:04 +0800864 if (src == IWL_PWR_SRC_VAUX) {
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800865 if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
Tomas Winklere2e3c572008-07-18 13:53:04 +0800866 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
867 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
868 ~APMG_PS_CTRL_MSK_PWR_SRC);
869 } else {
870 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
871 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
872 ~APMG_PS_CTRL_MSK_PWR_SRC);
873 }
874
Mohamed Abbasa8b50a02009-05-22 11:01:47 -0700875 return 0;
Tomas Winklere2e3c572008-07-18 13:53:04 +0800876}
877
Zhu Yib481de92007-09-25 17:54:57 -0700878/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700879 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -0700880 *
881 * Setup the RX handlers for each of the reply types sent from the uCode
882 * to the host.
883 *
884 * This function chains into the hardware specific files for them to setup
885 * any hardware specific handlers as well.
886 */
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +0800887static void iwl_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700888{
Tomas Winkler885ba202008-05-29 16:34:55 +0800889 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700890 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
891 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700892 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700893 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700894 iwl_rx_pm_debug_statistics_notif;
895 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700896
Ben Cahill9fbab512007-11-29 11:09:47 +0800897 /*
898 * The same handler is used for both the REPLY to a discrete
899 * statistics request from the host as well as for the periodic
900 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -0700901 */
Wey-Yi Guyef8d5522009-11-13 11:56:28 -0800902 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_reply_statistics;
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +0800903 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800904
Tomas Winkler21c339b2008-11-07 09:58:41 -0800905 iwl_setup_spectrum_handlers(priv);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800906 iwl_setup_rx_scan_handlers(priv);
907
Ron Rindjunsky37a44212008-05-29 16:35:18 +0800908 /* status change handler */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700909 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -0700910
Tomas Winklerc1354752008-05-29 16:35:04 +0800911 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
912 iwl_rx_missed_beacon_notif;
Ron Rindjunsky37a44212008-05-29 16:35:18 +0800913 /* Rx handlers */
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800914 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
915 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +0800916 /* block ack */
917 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl_rx_reply_compressed_ba;
Ben Cahill9fbab512007-11-29 11:09:47 +0800918 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -0700919 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700920}
921
Zhu Yib481de92007-09-25 17:54:57 -0700922/**
Tomas Winklera55360e2008-05-05 10:22:28 +0800923 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -0700924 *
925 * Uses the priv->rx_handlers callback function array to invoke
926 * the appropriate handlers, including command responses,
927 * frame-received notifications, and other notifications.
928 */
Tomas Winklera55360e2008-05-05 10:22:28 +0800929void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700930{
Tomas Winklera55360e2008-05-05 10:22:28 +0800931 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +0800932 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +0800933 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -0700934 u32 r, i;
935 int reclaim;
936 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +0800937 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -0800938 u32 count = 8;
Mohamed Abbas4752c932009-05-22 11:01:51 -0700939 int total_empty;
Zhu Yib481de92007-09-25 17:54:57 -0700940
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800941 /* uCode's read index (stored in shared DRAM) indicates the last Rx
942 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8d864222008-11-07 09:58:39 -0800943 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -0700944 i = rxq->read;
945
946 /* Rx interrupt, but nothing sent from uCode */
947 if (i == r)
Tomas Winklere1623442009-01-27 14:27:56 -0800948 IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -0700949
Mohamed Abbas4752c932009-05-22 11:01:51 -0700950 /* calculate total frames need to be restock after handling RX */
Zhu Yi73005152009-10-23 13:42:32 -0700951 total_empty = r - rxq->write_actual;
Mohamed Abbas4752c932009-05-22 11:01:51 -0700952 if (total_empty < 0)
953 total_empty += RX_QUEUE_SIZE;
954
955 if (total_empty > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +0800956 fill_rx = 1;
957
Zhu Yib481de92007-09-25 17:54:57 -0700958 while (i != r) {
959 rxb = rxq->queue[i];
960
Ben Cahill9fbab512007-11-29 11:09:47 +0800961 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -0700962 * then a bug has been introduced in the queue refilling
963 * routines -- catch it here */
964 BUG_ON(rxb == NULL);
965
966 rxq->queue[i] = NULL;
967
Zhu Yi2f301222009-10-09 17:19:45 +0800968 pci_unmap_page(priv->pci_dev, rxb->page_dma,
969 PAGE_SIZE << priv->hw_params.rx_page_order,
970 PCI_DMA_FROMDEVICE);
971 pkt = rxb_addr(rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700972
Johannes Bergbe1a71a2009-10-02 13:44:02 -0700973 trace_iwlwifi_dev_rx(priv, pkt,
974 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK);
975
Zhu Yib481de92007-09-25 17:54:57 -0700976 /* Reclaim a command buffer only if this packet is a response
977 * to a (driver-originated) command.
978 * If the packet (e.g. Rx frame) originated from uCode,
979 * there is no command buffer to reclaim.
980 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
981 * but apparently a few don't get set; catch them here. */
982 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
983 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -0700984 (pkt->hdr.cmd != REPLY_RX) &&
Daniel Halperin7dddaf12008-10-23 23:48:58 -0700985 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
Zhu Yicfe01702007-09-27 11:27:31 +0800986 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -0700987 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
988 (pkt->hdr.cmd != REPLY_TX);
989
990 /* Based on type of command response or notification,
991 * handle those that need handling via function in
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -0700992 * rx_handlers table. See iwl_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -0700993 if (priv->rx_handlers[pkt->hdr.cmd]) {
Tomas Winklere1623442009-01-27 14:27:56 -0800994 IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r,
Ester Kummerf3d67992008-05-06 11:05:12 +0800995 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Wey-Yi Guya83b9142009-04-08 11:39:32 -0700996 priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
Zhu Yi29b1b262009-10-23 13:42:25 -0700997 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -0700998 } else {
999 /* No handling needed */
Tomas Winklere1623442009-01-27 14:27:56 -08001000 IWL_DEBUG_RX(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001001 "r %d i %d No handler needed for %s, 0x%02x\n",
1002 r, i, get_cmd_string(pkt->hdr.cmd),
1003 pkt->hdr.cmd);
1004 }
1005
Zhu Yi29b1b262009-10-23 13:42:25 -07001006 /*
1007 * XXX: After here, we should always check rxb->page
1008 * against NULL before touching it or its virtual
1009 * memory (pkt). Because some rx_handler might have
1010 * already taken or freed the pages.
1011 */
1012
Zhu Yib481de92007-09-25 17:54:57 -07001013 if (reclaim) {
Zhu Yi2f301222009-10-09 17:19:45 +08001014 /* Invoke any callbacks, transfer the buffer to caller,
1015 * and fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07001016 * as we reclaim the driver command queue */
Zhu Yi29b1b262009-10-23 13:42:25 -07001017 if (rxb->page)
Tomas Winkler17b88922008-05-29 16:35:12 +08001018 iwl_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07001019 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001020 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07001021 }
1022
Zhu Yi73005152009-10-23 13:42:32 -07001023 /* Reuse the page if possible. For notification packets and
1024 * SKBs that fail to Rx correctly, add them back into the
1025 * rx_free list for reuse later. */
Zhu Yib481de92007-09-25 17:54:57 -07001026 spin_lock_irqsave(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001027 if (rxb->page != NULL) {
1028 rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
1029 0, PAGE_SIZE << priv->hw_params.rx_page_order,
1030 PCI_DMA_FROMDEVICE);
1031 list_add_tail(&rxb->list, &rxq->rx_free);
1032 rxq->free_count++;
1033 } else
1034 list_add_tail(&rxb->list, &rxq->rx_used);
1035
Zhu Yib481de92007-09-25 17:54:57 -07001036 spin_unlock_irqrestore(&rxq->lock, flags);
Zhu Yi73005152009-10-23 13:42:32 -07001037
Zhu Yib481de92007-09-25 17:54:57 -07001038 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001039 /* If there are a lot of unused frames,
1040 * restock the Rx queue so ucode wont assert. */
1041 if (fill_rx) {
1042 count++;
1043 if (count >= 8) {
Zhu Yi73005152009-10-23 13:42:32 -07001044 rxq->read = i;
Mohamed Abbas4752c932009-05-22 11:01:51 -07001045 iwl_rx_replenish_now(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08001046 count = 0;
1047 }
1048 }
Zhu Yib481de92007-09-25 17:54:57 -07001049 }
1050
1051 /* Backtrack one entry */
Zhu Yi73005152009-10-23 13:42:32 -07001052 rxq->read = i;
Mohamed Abbas4752c932009-05-22 11:01:51 -07001053 if (fill_rx)
1054 iwl_rx_replenish_now(priv);
1055 else
1056 iwl_rx_queue_restock(priv);
Tomas Winklera55360e2008-05-05 10:22:28 +08001057}
Tomas Winklera55360e2008-05-05 10:22:28 +08001058
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001059/* call this function to flush any scheduled tasklet */
1060static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1061{
Tomas Winklera96a27f2008-10-23 23:48:56 -07001062 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001063 synchronize_irq(priv->pci_dev->irq);
1064 tasklet_kill(&priv->irq_tasklet);
1065}
1066
Mohamed Abbasef850d72009-05-22 11:01:50 -07001067static void iwl_irq_tasklet_legacy(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001068{
1069 u32 inta, handled = 0;
1070 u32 inta_fh;
1071 unsigned long flags;
Ben Cahillc2e61da2009-10-30 14:36:09 -07001072 u32 i;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001073#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001074 u32 inta_mask;
1075#endif
1076
1077 spin_lock_irqsave(&priv->lock, flags);
1078
1079 /* Ack/clear/reset pending uCode interrupts.
1080 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1081 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001082 inta = iwl_read32(priv, CSR_INT);
1083 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07001084
1085 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1086 * Any new interrupts that happen after this, either while we're
1087 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001088 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1089 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001090
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001091#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001092 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08001093 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001094 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Tomas Winklere1623442009-01-27 14:27:56 -08001095 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001096 inta, inta_mask, inta_fh);
1097 }
1098#endif
1099
Zhu Yi2f301222009-10-09 17:19:45 +08001100 spin_unlock_irqrestore(&priv->lock, flags);
1101
Zhu Yib481de92007-09-25 17:54:57 -07001102 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1103 * atomic, make sure that inta covers all the interrupts that
1104 * we've discovered, even if FH interrupt came in just after
1105 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001106 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001107 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08001108 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07001109 inta |= CSR_INT_BIT_FH_TX;
1110
1111 /* Now service all interrupt bits discovered above. */
1112 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001113 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001114
1115 /* Tell the device to stop sending interrupts */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001116 iwl_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001117
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001118 priv->isr_stats.hw++;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001119 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001120
1121 handled |= CSR_INT_BIT_HW_ERR;
1122
Zhu Yib481de92007-09-25 17:54:57 -07001123 return;
1124 }
1125
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001126#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001127 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07001128 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001129 if (inta & CSR_INT_BIT_SCD) {
Tomas Winklere1623442009-01-27 14:27:56 -08001130 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
Joonwoo Park25c03d82008-01-23 10:15:20 -08001131 "the frame/frames.\n");
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001132 priv->isr_stats.sch++;
1133 }
Zhu Yib481de92007-09-25 17:54:57 -07001134
1135 /* Alive notification via Rx interrupt will do the real work */
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001136 if (inta & CSR_INT_BIT_ALIVE) {
Tomas Winklere1623442009-01-27 14:27:56 -08001137 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001138 priv->isr_stats.alive++;
1139 }
Zhu Yib481de92007-09-25 17:54:57 -07001140 }
1141#endif
1142 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08001143 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07001144
Ben Cahill9fbab512007-11-29 11:09:47 +08001145 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07001146 if (inta & CSR_INT_BIT_RF_KILL) {
1147 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001148 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07001149 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1150 hw_rf_kill = 1;
1151
Reinette Chatre4c423a22009-07-17 09:30:26 -07001152 IWL_WARN(priv, "RF_KILL bit toggled to %s.\n",
Abhijeet Kolekarc3056062008-11-12 13:14:08 -08001153 hw_rf_kill ? "disable radio" : "enable radio");
Zhu Yib481de92007-09-25 17:54:57 -07001154
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001155 priv->isr_stats.rfkill++;
1156
Emmanuel Grumbacha9efa652008-06-30 17:23:25 +08001157 /* driver only loads ucode once setting the interface up.
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01001158 * the driver allows loading the ucode even if the radio
1159 * is killed. Hence update the killswitch state here. The
1160 * rfkill handler will care about restarting if needed.
Emmanuel Grumbacha9efa652008-06-30 17:23:25 +08001161 */
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01001162 if (!test_bit(STATUS_ALIVE, &priv->status)) {
1163 if (hw_rf_kill)
1164 set_bit(STATUS_RF_KILL_HW, &priv->status);
1165 else
1166 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Johannes Berga60e77e2009-06-04 18:26:06 +02001167 wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rf_kill);
Mohamed Abbasedb34222008-12-11 10:33:37 -08001168 }
Zhu Yib481de92007-09-25 17:54:57 -07001169
1170 handled |= CSR_INT_BIT_RF_KILL;
1171 }
1172
Ben Cahill9fbab512007-11-29 11:09:47 +08001173 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07001174 if (inta & CSR_INT_BIT_CT_KILL) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001175 IWL_ERR(priv, "Microcode CT kill error detected.\n");
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001176 priv->isr_stats.ctkill++;
Zhu Yib481de92007-09-25 17:54:57 -07001177 handled |= CSR_INT_BIT_CT_KILL;
1178 }
1179
1180 /* Error detected by uCode */
1181 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001182 IWL_ERR(priv, "Microcode SW error detected. "
1183 " Restarting 0x%X.\n", inta);
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001184 priv->isr_stats.sw++;
1185 priv->isr_stats.sw_err = inta;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001186 iwl_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001187 handled |= CSR_INT_BIT_SW_ERR;
1188 }
1189
Ben Cahillc2e61da2009-10-30 14:36:09 -07001190 /*
1191 * uCode wakes up after power-down sleep.
1192 * Tell device about any new tx or host commands enqueued,
1193 * and about any Rx buffers made available while asleep.
1194 */
Zhu Yib481de92007-09-25 17:54:57 -07001195 if (inta & CSR_INT_BIT_WAKEUP) {
Tomas Winklere1623442009-01-27 14:27:56 -08001196 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08001197 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Ben Cahillc2e61da2009-10-30 14:36:09 -07001198 for (i = 0; i < priv->hw_params.max_txq_num; i++)
1199 iwl_txq_update_write_ptr(priv, &priv->txq[i]);
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001200 priv->isr_stats.wakeup++;
Zhu Yib481de92007-09-25 17:54:57 -07001201 handled |= CSR_INT_BIT_WAKEUP;
1202 }
1203
1204 /* All uCode command responses, including Tx command responses,
1205 * Rx "responses" (frame-received notification), and other
1206 * notifications from uCode come through here*/
1207 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08001208 iwl_rx_handle(priv);
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001209 priv->isr_stats.rx++;
Zhu Yib481de92007-09-25 17:54:57 -07001210 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1211 }
1212
Ben Cahillc72cd192009-10-30 14:36:08 -07001213 /* This "Tx" DMA channel is used only for loading uCode */
Zhu Yib481de92007-09-25 17:54:57 -07001214 if (inta & CSR_INT_BIT_FH_TX) {
Ben Cahillc72cd192009-10-30 14:36:08 -07001215 IWL_DEBUG_ISR(priv, "uCode load interrupt\n");
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001216 priv->isr_stats.tx++;
Zhu Yib481de92007-09-25 17:54:57 -07001217 handled |= CSR_INT_BIT_FH_TX;
Ben Cahillc72cd192009-10-30 14:36:08 -07001218 /* Wake up uCode load routine, now that load is complete */
Ron Rindjunskydbb983b2008-05-15 13:54:12 +08001219 priv->ucode_write_complete = 1;
1220 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07001221 }
1222
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001223 if (inta & ~handled) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001224 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001225 priv->isr_stats.unhandled++;
1226 }
Zhu Yib481de92007-09-25 17:54:57 -07001227
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001228 if (inta & ~(priv->inta_mask)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001229 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001230 inta & ~priv->inta_mask);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001231 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07001232 }
1233
1234 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07001235 /* only Re-enable if diabled by irq */
1236 if (test_bit(STATUS_INT_ENABLED, &priv->status))
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001237 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001238
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001239#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001240 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001241 inta = iwl_read32(priv, CSR_INT);
1242 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1243 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Tomas Winklere1623442009-01-27 14:27:56 -08001244 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
Zhu Yib481de92007-09-25 17:54:57 -07001245 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1246 }
1247#endif
Zhu Yib481de92007-09-25 17:54:57 -07001248}
1249
Mohamed Abbasef850d72009-05-22 11:01:50 -07001250/* tasklet for iwlagn interrupt */
1251static void iwl_irq_tasklet(struct iwl_priv *priv)
1252{
1253 u32 inta = 0;
1254 u32 handled = 0;
1255 unsigned long flags;
Ben Cahill87569902009-11-06 14:53:01 -08001256 u32 i;
Mohamed Abbasef850d72009-05-22 11:01:50 -07001257#ifdef CONFIG_IWLWIFI_DEBUG
1258 u32 inta_mask;
1259#endif
1260
1261 spin_lock_irqsave(&priv->lock, flags);
1262
1263 /* Ack/clear/reset pending uCode interrupts.
1264 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1265 */
1266 iwl_write32(priv, CSR_INT, priv->inta);
1267
1268 inta = priv->inta;
1269
1270#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001271 if (iwl_get_debug_level(priv) & IWL_DL_ISR) {
Mohamed Abbasef850d72009-05-22 11:01:50 -07001272 /* just for debug */
1273 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1274 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x\n ",
1275 inta, inta_mask);
1276 }
1277#endif
Zhu Yi2f301222009-10-09 17:19:45 +08001278
1279 spin_unlock_irqrestore(&priv->lock, flags);
1280
Mohamed Abbasef850d72009-05-22 11:01:50 -07001281 /* saved interrupt in inta variable now we can reset priv->inta */
1282 priv->inta = 0;
1283
1284 /* Now service all interrupt bits discovered above. */
1285 if (inta & CSR_INT_BIT_HW_ERR) {
Reinette Chatre58dba722009-07-17 09:30:25 -07001286 IWL_ERR(priv, "Hardware error detected. Restarting.\n");
Mohamed Abbasef850d72009-05-22 11:01:50 -07001287
1288 /* Tell the device to stop sending interrupts */
1289 iwl_disable_interrupts(priv);
1290
1291 priv->isr_stats.hw++;
1292 iwl_irq_handle_error(priv);
1293
1294 handled |= CSR_INT_BIT_HW_ERR;
1295
Mohamed Abbasef850d72009-05-22 11:01:50 -07001296 return;
1297 }
1298
1299#ifdef CONFIG_IWLWIFI_DEBUG
Reinette Chatre3d816c72009-08-07 15:41:37 -07001300 if (iwl_get_debug_level(priv) & (IWL_DL_ISR)) {
Mohamed Abbasef850d72009-05-22 11:01:50 -07001301 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1302 if (inta & CSR_INT_BIT_SCD) {
1303 IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
1304 "the frame/frames.\n");
1305 priv->isr_stats.sch++;
1306 }
1307
1308 /* Alive notification via Rx interrupt will do the real work */
1309 if (inta & CSR_INT_BIT_ALIVE) {
1310 IWL_DEBUG_ISR(priv, "Alive interrupt\n");
1311 priv->isr_stats.alive++;
1312 }
1313 }
1314#endif
1315 /* Safely ignore these bits for debug checks below */
1316 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1317
1318 /* HW RF KILL switch toggled */
1319 if (inta & CSR_INT_BIT_RF_KILL) {
1320 int hw_rf_kill = 0;
1321 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
1322 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1323 hw_rf_kill = 1;
1324
Reinette Chatre4c423a22009-07-17 09:30:26 -07001325 IWL_WARN(priv, "RF_KILL bit toggled to %s.\n",
Mohamed Abbasef850d72009-05-22 11:01:50 -07001326 hw_rf_kill ? "disable radio" : "enable radio");
1327
1328 priv->isr_stats.rfkill++;
1329
1330 /* driver only loads ucode once setting the interface up.
1331 * the driver allows loading the ucode even if the radio
1332 * is killed. Hence update the killswitch state here. The
1333 * rfkill handler will care about restarting if needed.
1334 */
1335 if (!test_bit(STATUS_ALIVE, &priv->status)) {
1336 if (hw_rf_kill)
1337 set_bit(STATUS_RF_KILL_HW, &priv->status);
1338 else
1339 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Johannes Berga60e77e2009-06-04 18:26:06 +02001340 wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rf_kill);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001341 }
1342
1343 handled |= CSR_INT_BIT_RF_KILL;
1344 }
1345
1346 /* Chip got too hot and stopped itself */
1347 if (inta & CSR_INT_BIT_CT_KILL) {
1348 IWL_ERR(priv, "Microcode CT kill error detected.\n");
1349 priv->isr_stats.ctkill++;
1350 handled |= CSR_INT_BIT_CT_KILL;
1351 }
1352
1353 /* Error detected by uCode */
1354 if (inta & CSR_INT_BIT_SW_ERR) {
1355 IWL_ERR(priv, "Microcode SW error detected. "
1356 " Restarting 0x%X.\n", inta);
1357 priv->isr_stats.sw++;
1358 priv->isr_stats.sw_err = inta;
1359 iwl_irq_handle_error(priv);
1360 handled |= CSR_INT_BIT_SW_ERR;
1361 }
1362
1363 /* uCode wakes up after power-down sleep */
1364 if (inta & CSR_INT_BIT_WAKEUP) {
1365 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
1366 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Ben Cahill87569902009-11-06 14:53:01 -08001367 for (i = 0; i < priv->hw_params.max_txq_num; i++)
1368 iwl_txq_update_write_ptr(priv, &priv->txq[i]);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001369
1370 priv->isr_stats.wakeup++;
1371
1372 handled |= CSR_INT_BIT_WAKEUP;
1373 }
1374
1375 /* All uCode command responses, including Tx command responses,
1376 * Rx "responses" (frame-received notification), and other
1377 * notifications from uCode come through here*/
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001378 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1379 CSR_INT_BIT_RX_PERIODIC)) {
Mohamed Abbasef850d72009-05-22 11:01:50 -07001380 IWL_DEBUG_ISR(priv, "Rx interrupt\n");
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001381 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1382 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1383 iwl_write32(priv, CSR_FH_INT_STATUS,
1384 CSR49_FH_INT_RX_MASK);
1385 }
1386 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1387 handled |= CSR_INT_BIT_RX_PERIODIC;
1388 iwl_write32(priv, CSR_INT, CSR_INT_BIT_RX_PERIODIC);
1389 }
1390 /* Sending RX interrupt require many steps to be done in the
1391 * the device:
1392 * 1- write interrupt to current index in ICT table.
1393 * 2- dma RX frame.
1394 * 3- update RX shared data to indicate last write index.
1395 * 4- send interrupt.
1396 * This could lead to RX race, driver could receive RX interrupt
Ben Cahill74ba67e2009-11-20 12:04:53 -08001397 * but the shared data changes does not reflect this;
1398 * periodic interrupt will detect any dangling Rx activity.
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001399 */
Ben Cahill74ba67e2009-11-20 12:04:53 -08001400
1401 /* Disable periodic interrupt; we use it as just a one-shot. */
1402 iwl_write8(priv, CSR_INT_PERIODIC_REG,
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001403 CSR_INT_PERIODIC_DIS);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001404 iwl_rx_handle(priv);
Ben Cahill74ba67e2009-11-20 12:04:53 -08001405
1406 /*
1407 * Enable periodic interrupt in 8 msec only if we received
1408 * real RX interrupt (instead of just periodic int), to catch
1409 * any dangling Rx interrupt. If it was just the periodic
1410 * interrupt, there was no dangling Rx activity, and no need
1411 * to extend the periodic interrupt; one-shot is enough.
1412 */
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001413 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
Ben Cahill74ba67e2009-11-20 12:04:53 -08001414 iwl_write8(priv, CSR_INT_PERIODIC_REG,
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001415 CSR_INT_PERIODIC_ENA);
1416
Mohamed Abbasef850d72009-05-22 11:01:50 -07001417 priv->isr_stats.rx++;
Mohamed Abbasef850d72009-05-22 11:01:50 -07001418 }
1419
Ben Cahillc72cd192009-10-30 14:36:08 -07001420 /* This "Tx" DMA channel is used only for loading uCode */
Mohamed Abbasef850d72009-05-22 11:01:50 -07001421 if (inta & CSR_INT_BIT_FH_TX) {
1422 iwl_write32(priv, CSR_FH_INT_STATUS, CSR49_FH_INT_TX_MASK);
Ben Cahillc72cd192009-10-30 14:36:08 -07001423 IWL_DEBUG_ISR(priv, "uCode load interrupt\n");
Mohamed Abbasef850d72009-05-22 11:01:50 -07001424 priv->isr_stats.tx++;
1425 handled |= CSR_INT_BIT_FH_TX;
Ben Cahillc72cd192009-10-30 14:36:08 -07001426 /* Wake up uCode load routine, now that load is complete */
Mohamed Abbasef850d72009-05-22 11:01:50 -07001427 priv->ucode_write_complete = 1;
1428 wake_up_interruptible(&priv->wait_command_queue);
1429 }
1430
1431 if (inta & ~handled) {
1432 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
1433 priv->isr_stats.unhandled++;
1434 }
1435
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001436 if (inta & ~(priv->inta_mask)) {
Mohamed Abbasef850d72009-05-22 11:01:50 -07001437 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Mohamed Abbas40cefda2009-05-22 11:01:52 -07001438 inta & ~priv->inta_mask);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001439 }
1440
Mohamed Abbasef850d72009-05-22 11:01:50 -07001441 /* Re-enable all interrupts */
1442 /* only Re-enable if diabled by irq */
1443 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1444 iwl_enable_interrupts(priv);
Mohamed Abbasef850d72009-05-22 11:01:50 -07001445}
1446
Wey-Yi Guya83b9142009-04-08 11:39:32 -07001447
Zhu Yib481de92007-09-25 17:54:57 -07001448/******************************************************************************
1449 *
1450 * uCode download functions
1451 *
1452 ******************************************************************************/
1453
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001454static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001455{
Tomas Winkler98c92212008-01-14 17:46:20 -08001456 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1457 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1458 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1459 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1460 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1461 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001462}
1463
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001464static void iwl_nic_start(struct iwl_priv *priv)
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08001465{
1466 /* Remove all resets to allow NIC to operate */
1467 iwl_write32(priv, CSR_RESET, 0);
1468}
1469
1470
Zhu Yib481de92007-09-25 17:54:57 -07001471/**
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001472 * iwl_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07001473 *
1474 * Copy into buffers for card to fetch via bus-mastering
1475 */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001476static int iwl_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001477{
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001478 struct iwl_ucode_header *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08001479 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07001480 const struct firmware *ucode_raw;
Reinette Chatrea0987a82008-12-02 12:14:06 -08001481 const char *name_pre = priv->cfg->fw_name_pre;
1482 const unsigned int api_max = priv->cfg->ucode_api_max;
1483 const unsigned int api_min = priv->cfg->ucode_api_min;
1484 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07001485 u8 *src;
1486 size_t len;
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001487 u32 api_ver, build;
1488 u32 inst_size, data_size, init_size, init_data_size, boot_size;
Jay Sternbergabdc2d62009-07-31 14:28:09 -07001489 u16 eeprom_ver;
Zhu Yib481de92007-09-25 17:54:57 -07001490
1491 /* Ask kernel firmware_class module to get the boot firmware off disk.
1492 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08001493 for (index = api_max; index >= api_min; index--) {
1494 sprintf(buf, "%s%d%s", name_pre, index, ".ucode");
1495 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
1496 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001497 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001498 buf, ret);
1499 if (ret == -ENOENT)
1500 continue;
1501 else
1502 goto error;
1503 } else {
1504 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08001505 IWL_ERR(priv, "Loaded firmware %s, "
1506 "which is deprecated. "
1507 "Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001508 buf, api_max);
Winkler, Tomas15b16872008-12-19 10:37:33 +08001509
Tomas Winklere1623442009-01-27 14:27:56 -08001510 IWL_DEBUG_INFO(priv, "Got firmware '%s' file (%zd bytes) from disk\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001511 buf, ucode_raw->size);
1512 break;
1513 }
Zhu Yib481de92007-09-25 17:54:57 -07001514 }
1515
Reinette Chatrea0987a82008-12-02 12:14:06 -08001516 if (ret < 0)
1517 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07001518
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001519 /* Make sure that we got at least the v1 header! */
1520 if (ucode_raw->size < priv->cfg->ops->ucode->get_header_size(1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001521 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08001522 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001523 goto err_release;
1524 }
1525
1526 /* Data from ucode file: header followed by uCode images */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001527 ucode = (struct iwl_ucode_header *)ucode_raw->data;
Zhu Yib481de92007-09-25 17:54:57 -07001528
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08001529 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08001530 api_ver = IWL_UCODE_API(priv->ucode_ver);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001531 build = priv->cfg->ops->ucode->get_build(ucode, api_ver);
1532 inst_size = priv->cfg->ops->ucode->get_inst_size(ucode, api_ver);
1533 data_size = priv->cfg->ops->ucode->get_data_size(ucode, api_ver);
1534 init_size = priv->cfg->ops->ucode->get_init_size(ucode, api_ver);
1535 init_data_size =
1536 priv->cfg->ops->ucode->get_init_data_size(ucode, api_ver);
1537 boot_size = priv->cfg->ops->ucode->get_boot_size(ucode, api_ver);
1538 src = priv->cfg->ops->ucode->get_data(ucode, api_ver);
Zhu Yib481de92007-09-25 17:54:57 -07001539
Reinette Chatrea0987a82008-12-02 12:14:06 -08001540 /* api_ver should match the api version forming part of the
1541 * firmware filename ... but we don't check for that and only rely
Nick Andrew877d0312009-01-26 11:06:57 +01001542 * on the API version read from firmware header from here on forward */
Reinette Chatrea0987a82008-12-02 12:14:06 -08001543
1544 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001545 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08001546 "Driver supports v%u, firmware is v%u.\n",
1547 api_max, api_ver);
1548 priv->ucode_ver = 0;
1549 ret = -EINVAL;
1550 goto err_release;
1551 }
1552 if (api_ver != api_max)
Tomas Winkler978785a2008-12-19 10:37:31 +08001553 IWL_ERR(priv, "Firmware has old API version. Expected v%u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08001554 "got v%u. New firmware can be obtained "
1555 "from http://www.intellinuxwireless.org.\n",
1556 api_max, api_ver);
1557
Tomas Winkler978785a2008-12-19 10:37:31 +08001558 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
1559 IWL_UCODE_MAJOR(priv->ucode_ver),
1560 IWL_UCODE_MINOR(priv->ucode_ver),
1561 IWL_UCODE_API(priv->ucode_ver),
1562 IWL_UCODE_SERIAL(priv->ucode_ver));
Reinette Chatrea0987a82008-12-02 12:14:06 -08001563
Reinette Chatre5ebeb5a2009-10-30 14:36:04 -07001564 snprintf(priv->hw->wiphy->fw_version,
1565 sizeof(priv->hw->wiphy->fw_version),
1566 "%u.%u.%u.%u",
1567 IWL_UCODE_MAJOR(priv->ucode_ver),
1568 IWL_UCODE_MINOR(priv->ucode_ver),
1569 IWL_UCODE_API(priv->ucode_ver),
1570 IWL_UCODE_SERIAL(priv->ucode_ver));
1571
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001572 if (build)
1573 IWL_DEBUG_INFO(priv, "Build %u\n", build);
1574
Jay Sternbergabdc2d62009-07-31 14:28:09 -07001575 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
1576 IWL_DEBUG_INFO(priv, "NVM Type: %s, version: 0x%x\n",
1577 (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
1578 ? "OTP" : "EEPROM", eeprom_ver);
1579
Tomas Winklere1623442009-01-27 14:27:56 -08001580 IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08001581 priv->ucode_ver);
Tomas Winklere1623442009-01-27 14:27:56 -08001582 IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001583 inst_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001584 IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001585 data_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001586 IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001587 init_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001588 IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001589 init_data_size);
Tomas Winklere1623442009-01-27 14:27:56 -08001590 IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
Zhu Yib481de92007-09-25 17:54:57 -07001591 boot_size);
1592
1593 /* Verify size of file vs. image size info in file's header */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001594 if (ucode_raw->size !=
1595 priv->cfg->ops->ucode->get_header_size(api_ver) +
Zhu Yib481de92007-09-25 17:54:57 -07001596 inst_size + data_size + init_size +
1597 init_data_size + boot_size) {
1598
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001599 IWL_DEBUG_INFO(priv,
1600 "uCode file size %d does not match expected size\n",
1601 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001602 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001603 goto err_release;
1604 }
1605
1606 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001607 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001608 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001609 inst_size);
1610 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001611 goto err_release;
1612 }
1613
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001614 if (data_size > priv->hw_params.max_data_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001615 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001616 data_size);
1617 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001618 goto err_release;
1619 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001620 if (init_size > priv->hw_params.max_inst_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001621 IWL_INFO(priv, "uCode init instr len %d too large to fit in\n",
1622 init_size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001623 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001624 goto err_release;
1625 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001626 if (init_data_size > priv->hw_params.max_data_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001627 IWL_INFO(priv, "uCode init data len %d too large to fit in\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001628 init_data_size);
1629 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001630 goto err_release;
1631 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001632 if (boot_size > priv->hw_params.max_bsm_size) {
Tomas Winklere1623442009-01-27 14:27:56 -08001633 IWL_INFO(priv, "uCode boot instr len %d too large to fit in\n",
1634 boot_size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001635 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07001636 goto err_release;
1637 }
1638
1639 /* Allocate ucode buffers for card's bus-master loading ... */
1640
1641 /* Runtime instructions and 2 copies of data:
1642 * 1) unmodified from disk
1643 * 2) backup cache for save/restore during power-downs */
1644 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001645 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07001646
1647 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001648 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07001649
1650 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001651 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07001652
Zhu, Yi1f304e42009-01-23 13:45:22 -08001653 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
1654 !priv->ucode_data_backup.v_addr)
1655 goto err_pci_alloc;
1656
Zhu Yib481de92007-09-25 17:54:57 -07001657 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08001658 if (init_size && init_data_size) {
1659 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001660 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07001661
Tomas Winkler90e759d2007-11-29 11:09:41 +08001662 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001663 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08001664
1665 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
1666 goto err_pci_alloc;
1667 }
Zhu Yib481de92007-09-25 17:54:57 -07001668
1669 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08001670 if (boot_size) {
1671 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08001672 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07001673
Tomas Winkler90e759d2007-11-29 11:09:41 +08001674 if (!priv->ucode_boot.v_addr)
1675 goto err_pci_alloc;
1676 }
Zhu Yib481de92007-09-25 17:54:57 -07001677
1678 /* Copy images into buffers for card's bus-master reads ... */
1679
1680 /* Runtime instructions (first block of data in file) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001681 len = inst_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001682 IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001683 memcpy(priv->ucode_code.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001684 src += len;
1685
Tomas Winklere1623442009-01-27 14:27:56 -08001686 IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001687 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
1688
1689 /* Runtime data (2nd block)
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001690 * NOTE: Copy into backup buffer will be done in iwl_up() */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001691 len = data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001692 IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001693 memcpy(priv->ucode_data.v_addr, src, len);
1694 memcpy(priv->ucode_data_backup.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001695 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07001696
1697 /* Initialization instructions (3rd block) */
1698 if (init_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001699 len = init_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001700 IWL_DEBUG_INFO(priv, "Copying (but not loading) init instr len %Zd\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001701 len);
Zhu Yib481de92007-09-25 17:54:57 -07001702 memcpy(priv->ucode_init.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001703 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07001704 }
1705
1706 /* Initialization data (4th block) */
1707 if (init_data_size) {
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001708 len = init_data_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001709 IWL_DEBUG_INFO(priv, "Copying (but not loading) init data len %Zd\n",
Tomas Winkler90e759d2007-11-29 11:09:41 +08001710 len);
Zhu Yib481de92007-09-25 17:54:57 -07001711 memcpy(priv->ucode_init_data.v_addr, src, len);
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001712 src += len;
Zhu Yib481de92007-09-25 17:54:57 -07001713 }
1714
1715 /* Bootstrap instructions (5th block) */
Jay Sternbergcc0f5552009-07-17 09:30:16 -07001716 len = boot_size;
Tomas Winklere1623442009-01-27 14:27:56 -08001717 IWL_DEBUG_INFO(priv, "Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07001718 memcpy(priv->ucode_boot.v_addr, src, len);
1719
1720 /* We have our copies now, allow OS release its copies */
1721 release_firmware(ucode_raw);
1722 return 0;
1723
1724 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001725 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08001726 ret = -ENOMEM;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07001727 iwl_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001728
1729 err_release:
1730 release_firmware(ucode_raw);
1731
1732 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08001733 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001734}
1735
Reinette Chatreb7a79402009-09-25 14:24:23 -07001736static const char *desc_lookup_text[] = {
1737 "OK",
1738 "FAIL",
1739 "BAD_PARAM",
1740 "BAD_CHECKSUM",
1741 "NMI_INTERRUPT_WDG",
1742 "SYSASSERT",
1743 "FATAL_ERROR",
1744 "BAD_COMMAND",
1745 "HW_ERROR_TUNE_LOCK",
1746 "HW_ERROR_TEMPERATURE",
1747 "ILLEGAL_CHAN_FREQ",
1748 "VCC_NOT_STABLE",
1749 "FH_ERROR",
1750 "NMI_INTERRUPT_HOST",
1751 "NMI_INTERRUPT_ACTION_PT",
1752 "NMI_INTERRUPT_UNKNOWN",
1753 "UCODE_VERSION_MISMATCH",
1754 "HW_ERROR_ABS_LOCK",
1755 "HW_ERROR_CAL_LOCK_FAIL",
1756 "NMI_INTERRUPT_INST_ACTION_PT",
1757 "NMI_INTERRUPT_DATA_ACTION_PT",
1758 "NMI_TRM_HW_ER",
1759 "NMI_INTERRUPT_TRM",
1760 "NMI_INTERRUPT_BREAK_POINT"
1761 "DEBUG_0",
1762 "DEBUG_1",
1763 "DEBUG_2",
1764 "DEBUG_3",
1765 "UNKNOWN"
1766};
1767
1768static const char *desc_lookup(int i)
1769{
1770 int max = ARRAY_SIZE(desc_lookup_text) - 1;
1771
1772 if (i < 0 || i > max)
1773 i = max;
1774
1775 return desc_lookup_text[i];
1776}
1777
1778#define ERROR_START_OFFSET (1 * sizeof(u32))
1779#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1780
1781void iwl_dump_nic_error_log(struct iwl_priv *priv)
1782{
1783 u32 data2, line;
1784 u32 desc, time, count, base, data1;
1785 u32 blink1, blink2, ilink1, ilink2;
1786
1787 if (priv->ucode_type == UCODE_INIT)
1788 base = le32_to_cpu(priv->card_alive_init.error_event_table_ptr);
1789 else
1790 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1791
1792 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Wey-Yi Guy212fb572009-12-02 12:53:00 -08001793 IWL_ERR(priv,
1794 "Not valid error log pointer 0x%08X for %s uCode\n",
1795 base, (priv->ucode_type == UCODE_INIT) ? "Init" : "RT");
Reinette Chatreb7a79402009-09-25 14:24:23 -07001796 return;
1797 }
1798
1799 count = iwl_read_targ_mem(priv, base);
1800
1801 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
1802 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1803 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1804 priv->status, count);
1805 }
1806
1807 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
1808 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
1809 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
1810 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
1811 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
1812 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
1813 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
1814 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
1815 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
1816
Johannes Bergbe1a71a2009-10-02 13:44:02 -07001817 trace_iwlwifi_dev_ucode_error(priv, desc, time, data1, data2, line,
1818 blink1, blink2, ilink1, ilink2);
1819
Reinette Chatreb7a79402009-09-25 14:24:23 -07001820 IWL_ERR(priv, "Desc Time "
1821 "data1 data2 line\n");
1822 IWL_ERR(priv, "%-28s (#%02d) %010u 0x%08X 0x%08X %u\n",
1823 desc_lookup(desc), desc, time, data1, data2, line);
1824 IWL_ERR(priv, "blink1 blink2 ilink1 ilink2\n");
1825 IWL_ERR(priv, "0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
1826 ilink1, ilink2);
1827
1828}
1829
1830#define EVENT_START_OFFSET (4 * sizeof(u32))
1831
1832/**
1833 * iwl_print_event_log - Dump error event log to syslog
1834 *
1835 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001836static int iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
1837 u32 num_events, u32 mode,
1838 int pos, char **buf, size_t bufsz)
Reinette Chatreb7a79402009-09-25 14:24:23 -07001839{
1840 u32 i;
1841 u32 base; /* SRAM byte address of event log header */
1842 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1843 u32 ptr; /* SRAM byte address of log data */
1844 u32 ev, time, data; /* event log data */
Ben Cahille5854472009-11-06 14:52:58 -08001845 unsigned long reg_flags;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001846
1847 if (num_events == 0)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001848 return pos;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001849 if (priv->ucode_type == UCODE_INIT)
1850 base = le32_to_cpu(priv->card_alive_init.log_event_table_ptr);
1851 else
1852 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1853
1854 if (mode == 0)
1855 event_size = 2 * sizeof(u32);
1856 else
1857 event_size = 3 * sizeof(u32);
1858
1859 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1860
Ben Cahille5854472009-11-06 14:52:58 -08001861 /* Make sure device is powered up for SRAM reads */
1862 spin_lock_irqsave(&priv->reg_lock, reg_flags);
1863 iwl_grab_nic_access(priv);
1864
1865 /* Set starting address; reads will auto-increment */
1866 _iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
1867 rmb();
1868
Reinette Chatreb7a79402009-09-25 14:24:23 -07001869 /* "time" is actually "data" for mode 0 (no timestamp).
1870 * place event id # at far right for easier visual parsing. */
1871 for (i = 0; i < num_events; i++) {
Ben Cahille5854472009-11-06 14:52:58 -08001872 ev = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1873 time = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Reinette Chatreb7a79402009-09-25 14:24:23 -07001874 if (mode == 0) {
1875 /* data, ev */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001876 if (bufsz) {
1877 pos += scnprintf(*buf + pos, bufsz - pos,
1878 "EVT_LOG:0x%08x:%04u\n",
1879 time, ev);
1880 } else {
1881 trace_iwlwifi_dev_ucode_event(priv, 0,
1882 time, ev);
1883 IWL_ERR(priv, "EVT_LOG:0x%08x:%04u\n",
1884 time, ev);
1885 }
Reinette Chatreb7a79402009-09-25 14:24:23 -07001886 } else {
Ben Cahille5854472009-11-06 14:52:58 -08001887 data = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001888 if (bufsz) {
1889 pos += scnprintf(*buf + pos, bufsz - pos,
1890 "EVT_LOGT:%010u:0x%08x:%04u\n",
1891 time, data, ev);
1892 } else {
1893 IWL_ERR(priv, "EVT_LOGT:%010u:0x%08x:%04u\n",
Reinette Chatreb7a79402009-09-25 14:24:23 -07001894 time, data, ev);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001895 trace_iwlwifi_dev_ucode_event(priv, time,
1896 data, ev);
1897 }
Reinette Chatreb7a79402009-09-25 14:24:23 -07001898 }
1899 }
Ben Cahille5854472009-11-06 14:52:58 -08001900
1901 /* Allow device to power down */
1902 iwl_release_nic_access(priv);
1903 spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001904 return pos;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001905}
1906
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001907/**
1908 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
1909 */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001910static int iwl_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
1911 u32 num_wraps, u32 next_entry,
1912 u32 size, u32 mode,
1913 int pos, char **buf, size_t bufsz)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001914{
1915 /*
1916 * display the newest DEFAULT_LOG_ENTRIES entries
1917 * i.e the entries just before the next ont that uCode would fill.
1918 */
1919 if (num_wraps) {
1920 if (next_entry < size) {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001921 pos = iwl_print_event_log(priv,
1922 capacity - (size - next_entry),
1923 size - next_entry, mode,
1924 pos, buf, bufsz);
1925 pos = iwl_print_event_log(priv, 0,
1926 next_entry, mode,
1927 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001928 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001929 pos = iwl_print_event_log(priv, next_entry - size,
1930 size, mode, pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001931 } else {
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001932 if (next_entry < size) {
1933 pos = iwl_print_event_log(priv, 0, next_entry,
1934 mode, pos, buf, bufsz);
1935 } else {
1936 pos = iwl_print_event_log(priv, next_entry - size,
1937 size, mode, pos, buf, bufsz);
1938 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001939 }
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001940 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001941}
1942
Ben Cahill84c40692009-11-06 14:52:57 -08001943/* For sanity check only. Actual size is determined by uCode, typ. 512 */
1944#define MAX_EVENT_LOG_SIZE (512)
1945
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001946#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
1947
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001948int iwl_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1949 char **buf, bool display)
Reinette Chatreb7a79402009-09-25 14:24:23 -07001950{
1951 u32 base; /* SRAM byte address of event log header */
1952 u32 capacity; /* event log capacity in # entries */
1953 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
1954 u32 num_wraps; /* # times uCode wrapped to top of log */
1955 u32 next_entry; /* index of next entry to be written by uCode */
1956 u32 size; /* # entries that we'll print */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001957 int pos = 0;
1958 size_t bufsz = 0;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001959
1960 if (priv->ucode_type == UCODE_INIT)
1961 base = le32_to_cpu(priv->card_alive_init.log_event_table_ptr);
1962 else
1963 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1964
1965 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Wey-Yi Guy212fb572009-12-02 12:53:00 -08001966 IWL_ERR(priv,
1967 "Invalid event log pointer 0x%08X for %s uCode\n",
1968 base, (priv->ucode_type == UCODE_INIT) ? "Init" : "RT");
Wey-Yi Guy937c3972010-01-15 13:43:36 -08001969 return -EINVAL;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001970 }
1971
1972 /* event log header */
1973 capacity = iwl_read_targ_mem(priv, base);
1974 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
1975 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
1976 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
1977
Ben Cahill84c40692009-11-06 14:52:57 -08001978 if (capacity > MAX_EVENT_LOG_SIZE) {
1979 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d entries\n",
1980 capacity, MAX_EVENT_LOG_SIZE);
1981 capacity = MAX_EVENT_LOG_SIZE;
1982 }
1983
1984 if (next_entry > MAX_EVENT_LOG_SIZE) {
1985 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
1986 next_entry, MAX_EVENT_LOG_SIZE);
1987 next_entry = MAX_EVENT_LOG_SIZE;
1988 }
1989
Reinette Chatreb7a79402009-09-25 14:24:23 -07001990 size = num_wraps ? capacity : next_entry;
1991
1992 /* bail out if nothing in log */
1993 if (size == 0) {
1994 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08001995 return pos;
Reinette Chatreb7a79402009-09-25 14:24:23 -07001996 }
1997
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08001998#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy521d9bc2009-12-10 14:37:23 -08001999 if (!(iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002000 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
2001 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
2002#else
2003 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
2004 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
Reinette Chatreb7a79402009-09-25 14:24:23 -07002005#endif
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002006 IWL_ERR(priv, "Start IWL Event Log Dump: display last %u entries\n",
2007 size);
2008
2009#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002010 if (display) {
2011 if (full_log)
2012 bufsz = capacity * 48;
2013 else
2014 bufsz = size * 48;
2015 *buf = kmalloc(bufsz, GFP_KERNEL);
2016 if (!*buf)
Wey-Yi Guy937c3972010-01-15 13:43:36 -08002017 return -ENOMEM;
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002018 }
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002019 if ((iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
2020 /*
2021 * if uCode has wrapped back to top of log,
2022 * start at the oldest entry,
2023 * i.e the next one that uCode would fill.
2024 */
2025 if (num_wraps)
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002026 pos = iwl_print_event_log(priv, next_entry,
2027 capacity - next_entry, mode,
2028 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002029 /* (then/else) start at top of log */
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002030 pos = iwl_print_event_log(priv, 0,
2031 next_entry, mode, pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002032 } else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002033 pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
2034 next_entry, size, mode,
2035 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002036#else
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002037 pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
2038 next_entry, size, mode,
2039 pos, buf, bufsz);
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002040#endif
Wey-Yi Guyb03d7d02009-12-14 14:12:20 -08002041 return pos;
Wey-Yi Guyc341ddb2009-11-20 12:05:07 -08002042}
Reinette Chatreb7a79402009-09-25 14:24:23 -07002043
Zhu Yib481de92007-09-25 17:54:57 -07002044/**
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002045 * iwl_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07002046 * from protocol/runtime uCode (initialization uCode's
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002047 * Alive gets handled by iwl_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07002048 */
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002049static void iwl_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002050{
Tomas Winkler57aab752008-04-14 21:16:03 -07002051 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002052
Tomas Winklere1623442009-01-27 14:27:56 -08002053 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002054
2055 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2056 /* We had an error bringing up the hardware, so take it
2057 * all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002058 IWL_DEBUG_INFO(priv, "Alive failed.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002059 goto restart;
2060 }
2061
2062 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2063 * This is a paranoid check, because we would not have gotten the
2064 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07002065 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002066 /* Runtime instruction load was bad;
2067 * take it all the way back down so we can try again */
Tomas Winklere1623442009-01-27 14:27:56 -08002068 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002069 goto restart;
2070 }
2071
Tomas Winklerc587de02009-06-03 11:44:07 -07002072 iwl_clear_stations_table(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002073 ret = priv->cfg->ops->lib->alive_notify(priv);
2074 if (ret) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002075 IWL_WARN(priv,
2076 "Could not complete ALIVE transition [ntf]: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07002077 goto restart;
2078 }
2079
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002080 /* After the ALIVE response, we can send host commands to the uCode */
Zhu Yib481de92007-09-25 17:54:57 -07002081 set_bit(STATUS_ALIVE, &priv->status);
2082
Tomas Winklerfee12472008-04-03 16:05:21 -07002083 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002084 return;
2085
Johannes Berg36d68252008-05-15 12:55:26 +02002086 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002087
2088 priv->active_rate = priv->rates_mask;
2089 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2090
Wey-Yi Guy2f748de2009-09-17 10:43:51 -07002091 /* Configure Tx antenna selection based on H/W config */
2092 if (priv->cfg->ops->hcmd->set_tx_ant)
2093 priv->cfg->ops->hcmd->set_tx_ant(priv, priv->cfg->valid_tx_ant);
2094
Tomas Winkler3109ece2008-03-28 16:33:35 -07002095 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002096 struct iwl_rxon_cmd *active_rxon =
2097 (struct iwl_rxon_cmd *)&priv->active_rxon;
Mohamed Abbas019fb972009-03-17 21:59:18 -07002098 /* apply any changes in staging */
2099 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002100 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2101 } else {
2102 /* Initialize our rx_config data */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002103 iwl_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekar45823532009-04-08 11:26:44 -07002104
2105 if (priv->cfg->ops->hcmd->set_rxon_chain)
2106 priv->cfg->ops->hcmd->set_rxon_chain(priv);
2107
Zhu Yib481de92007-09-25 17:54:57 -07002108 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2109 }
2110
Ben Cahill9fbab512007-11-29 11:09:47 +08002111 /* Configure Bluetooth device coexistence support */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002112 iwl_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002113
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002114 iwl_reset_run_time_calib(priv);
2115
Zhu Yib481de92007-09-25 17:54:57 -07002116 /* Configure the adapter for unassociated operation */
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002117 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002118
2119 /* At this point, the NIC is initialized and operational */
Emmanuel Grumbach47f4a582008-06-12 09:47:13 +08002120 iwl_rf_kill_ct_config(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08002121
Johannes Berge932a602009-10-02 13:44:03 -07002122 iwl_leds_init(priv);
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07002123
Tomas Winklere1623442009-01-27 14:27:56 -08002124 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07002125 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08002126 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07002127
Johannes Berge312c242009-08-07 15:41:51 -07002128 iwl_power_update_mode(priv, true);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002129
Mohamed Abbasada17512008-11-07 09:58:34 -08002130 /* reassociate for ADHOC mode */
2131 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2132 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2133 priv->vif);
2134 if (beacon)
2135 iwl_mac_beacon_update(priv->hw, beacon);
2136 }
2137
2138
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002139 if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002140 iwl_set_mode(priv, priv->iw_mode);
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002141
Zhu Yib481de92007-09-25 17:54:57 -07002142 return;
2143
2144 restart:
2145 queue_work(priv->workqueue, &priv->restart);
2146}
2147
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002148static void iwl_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07002149
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002150static void __iwl_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002151{
2152 unsigned long flags;
2153 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002154
Tomas Winklere1623442009-01-27 14:27:56 -08002155 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
Zhu Yib481de92007-09-25 17:54:57 -07002156
Zhu Yib481de92007-09-25 17:54:57 -07002157 if (!exit_pending)
2158 set_bit(STATUS_EXIT_PENDING, &priv->status);
2159
Tomas Winklerc587de02009-06-03 11:44:07 -07002160 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002161
2162 /* Unblock any waiting calls */
2163 wake_up_interruptible_all(&priv->wait_command_queue);
2164
Zhu Yib481de92007-09-25 17:54:57 -07002165 /* Wipe out the EXIT_PENDING status bit if we are not actually
2166 * exiting the module */
2167 if (!exit_pending)
2168 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2169
2170 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002171 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07002172
2173 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002174 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002175 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07002176 spin_unlock_irqrestore(&priv->lock, flags);
2177 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002178
2179 if (priv->mac80211_registered)
2180 ieee80211_stop_queues(priv->hw);
2181
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002182 /* If we have not previously called iwl_init() then
Johannes Berga60e77e2009-06-04 18:26:06 +02002183 * clear all bits but the RF Kill bit and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07002184 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002185 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2186 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002187 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2188 STATUS_GEO_CONFIGURED |
Mohamed Abbas052ec3f2008-06-30 17:23:15 +08002189 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2190 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002191 goto exit;
2192 }
2193
Wey-Yi Guy6da3a132009-03-26 10:14:08 -07002194 /* ...otherwise clear out all the status bits but the RF Kill
Johannes Berga60e77e2009-06-04 18:26:06 +02002195 * bit and continue taking the NIC down. */
Zhu Yib481de92007-09-25 17:54:57 -07002196 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2197 STATUS_RF_KILL_HW |
Reinette Chatre97888642008-02-06 11:20:38 -08002198 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2199 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07002200 test_bit(STATUS_FW_ERROR, &priv->status) <<
Mohamed Abbas052ec3f2008-06-30 17:23:15 +08002201 STATUS_FW_ERROR |
2202 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2203 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07002204
Mohamed Abbasef850d72009-05-22 11:01:50 -07002205 /* device going down, Stop using ICT table */
2206 iwl_disable_ict(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002207
Tomas Winklerda1bc452008-05-29 16:35:00 +08002208 iwl_txq_ctx_stop(priv);
Tomas Winklerb3bbacb2008-05-29 16:35:01 +08002209 iwl_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002210
Ben Cahill309e7312009-11-06 14:53:03 -08002211 /* Power-down device's busmaster DMA clocks */
2212 iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -07002213 udelay(5);
2214
Ben Cahill309e7312009-11-06 14:53:03 -08002215 /* Make sure (redundant) we've released our request to stay awake */
2216 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2217
Ben Cahill4d2ccdb2009-10-09 13:20:20 -07002218 /* Stop the device, and put it in low power state */
2219 priv->cfg->ops->lib->apm_ops.stop(priv);
2220
Zhu Yib481de92007-09-25 17:54:57 -07002221 exit:
Tomas Winkler885ba202008-05-29 16:34:55 +08002222 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002223
2224 if (priv->ibss_beacon)
2225 dev_kfree_skb(priv->ibss_beacon);
2226 priv->ibss_beacon = NULL;
2227
2228 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08002229 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002230}
2231
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002232static void iwl_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002233{
2234 mutex_lock(&priv->mutex);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002235 __iwl_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002236 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08002237
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08002238 iwl_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002239}
2240
Mohamed Abbas086ed112009-05-22 11:01:54 -07002241#define HW_READY_TIMEOUT (50)
2242
2243static int iwl_set_hw_ready(struct iwl_priv *priv)
2244{
2245 int ret = 0;
2246
2247 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
2248 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
2249
2250 /* See if we got it */
2251 ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
2252 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
2253 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
2254 HW_READY_TIMEOUT);
2255 if (ret != -ETIMEDOUT)
2256 priv->hw_ready = true;
2257 else
2258 priv->hw_ready = false;
2259
2260 IWL_DEBUG_INFO(priv, "hardware %s\n",
2261 (priv->hw_ready == 1) ? "ready" : "not ready");
2262 return ret;
2263}
2264
2265static int iwl_prepare_card_hw(struct iwl_priv *priv)
2266{
2267 int ret = 0;
2268
2269 IWL_DEBUG_INFO(priv, "iwl_prepare_card_hw enter \n");
2270
Mohamed Abbas3354a0f2009-06-19 13:52:41 -07002271 ret = iwl_set_hw_ready(priv);
2272 if (priv->hw_ready)
2273 return ret;
2274
2275 /* If HW is not ready, prepare the conditions to check again */
Mohamed Abbas086ed112009-05-22 11:01:54 -07002276 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
2277 CSR_HW_IF_CONFIG_REG_PREPARE);
2278
2279 ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
2280 ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
2281 CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
2282
Mohamed Abbas3354a0f2009-06-19 13:52:41 -07002283 /* HW should be ready by now, check again. */
Mohamed Abbas086ed112009-05-22 11:01:54 -07002284 if (ret != -ETIMEDOUT)
2285 iwl_set_hw_ready(priv);
2286
2287 return ret;
2288}
2289
Zhu Yib481de92007-09-25 17:54:57 -07002290#define MAX_HW_RESTARTS 5
2291
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002292static int __iwl_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002293{
Tomas Winkler57aab752008-04-14 21:16:03 -07002294 int i;
2295 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002296
2297 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002298 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002299 return -EIO;
2300 }
2301
Reinette Chatree903fbd2008-01-30 22:05:15 -08002302 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002303 IWL_ERR(priv, "ucode not available for device bringup\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08002304 return -EIO;
2305 }
2306
Mohamed Abbas086ed112009-05-22 11:01:54 -07002307 iwl_prepare_card_hw(priv);
2308
2309 if (!priv->hw_ready) {
2310 IWL_WARN(priv, "Exit HW not ready\n");
2311 return -EIO;
2312 }
2313
Zhu Yie655b9f2008-01-24 02:19:38 -08002314 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winklerc1842d62008-08-04 16:00:43 +08002315 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Zhu Yie655b9f2008-01-24 02:19:38 -08002316 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Emmanuel Grumbach3bff19c2008-06-30 17:23:19 +08002317 else
Zhu Yie655b9f2008-01-24 02:19:38 -08002318 set_bit(STATUS_RF_KILL_HW, &priv->status);
Emmanuel Grumbach3bff19c2008-06-30 17:23:19 +08002319
Tomas Winklerc1842d62008-08-04 16:00:43 +08002320 if (iwl_is_rfkill(priv)) {
Johannes Berga60e77e2009-06-04 18:26:06 +02002321 wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
2322
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002323 iwl_enable_interrupts(priv);
Johannes Berga60e77e2009-06-04 18:26:06 +02002324 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Tomas Winklerc1842d62008-08-04 16:00:43 +08002325 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07002326 }
2327
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002328 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07002329
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002330 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07002331 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002332 IWL_ERR(priv, "Unable to init nic\n");
Tomas Winkler57aab752008-04-14 21:16:03 -07002333 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002334 }
2335
2336 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002337 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2338 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002339 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2340
2341 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002342 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002343 iwl_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002344
2345 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002346 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2347 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002348
2349 /* Copy original ucode data image from disk into backup cache.
2350 * This will be used to initialize the on-board processor's
2351 * data SRAM for a clean start when the runtime program first loads. */
2352 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08002353 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07002354
Zhu Yib481de92007-09-25 17:54:57 -07002355 for (i = 0; i < MAX_HW_RESTARTS; i++) {
2356
Tomas Winklerc587de02009-06-03 11:44:07 -07002357 iwl_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002358
2359 /* load bootstrap state machine,
2360 * load bootstrap program into processor's memory,
2361 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07002362 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002363
Tomas Winkler57aab752008-04-14 21:16:03 -07002364 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002365 IWL_ERR(priv, "Unable to set up bootstrap uCode: %d\n",
2366 ret);
Zhu Yib481de92007-09-25 17:54:57 -07002367 continue;
2368 }
2369
2370 /* start card; "initialize" will load runtime ucode */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002371 iwl_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002372
Tomas Winklere1623442009-01-27 14:27:56 -08002373 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
Zhu Yib481de92007-09-25 17:54:57 -07002374
2375 return 0;
2376 }
2377
2378 set_bit(STATUS_EXIT_PENDING, &priv->status);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002379 __iwl_down(priv);
Mohamed Abbas64e72c32008-06-12 09:47:03 +08002380 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07002381
2382 /* tried to restart and config the device for as long as our
2383 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08002384 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07002385 return -EIO;
2386}
2387
2388
2389/*****************************************************************************
2390 *
2391 * Workqueue callbacks
2392 *
2393 *****************************************************************************/
2394
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002395static void iwl_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002396{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002397 struct iwl_priv *priv =
2398 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002399
2400 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2401 return;
2402
2403 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08002404 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002405 mutex_unlock(&priv->mutex);
2406}
2407
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002408static void iwl_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002409{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002410 struct iwl_priv *priv =
2411 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07002412
2413 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2414 return;
2415
Mohamed Abbas258c44a2009-06-03 11:44:10 -07002416 /* enable dram interrupt */
2417 iwl_reset_ict(priv);
2418
Zhu Yib481de92007-09-25 17:54:57 -07002419 mutex_lock(&priv->mutex);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08002420 iwl_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002421 mutex_unlock(&priv->mutex);
2422}
2423
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08002424static void iwl_bg_run_time_calib_work(struct work_struct *work)
2425{
2426 struct iwl_priv *priv = container_of(work, struct iwl_priv,
2427 run_time_calib_work);
2428
2429 mutex_lock(&priv->mutex);
2430
2431 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
2432 test_bit(STATUS_SCANNING, &priv->status)) {
2433 mutex_unlock(&priv->mutex);
2434 return;
2435 }
2436
2437 if (priv->start_calib) {
2438 iwl_chain_noise_calibration(priv, &priv->statistics);
2439
2440 iwl_sensitivity_calibration(priv, &priv->statistics);
2441 }
2442
2443 mutex_unlock(&priv->mutex);
2444 return;
2445}
2446
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002447static void iwl_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002448{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002449 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07002450
2451 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2452 return;
2453
2454 mutex_lock(&priv->mutex);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002455 __iwl_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002456 mutex_unlock(&priv->mutex);
2457}
2458
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002459static void iwl_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002460{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002461 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07002462
2463 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2464 return;
2465
Johannes Berg19cc1082009-05-08 13:44:36 -07002466 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
2467 mutex_lock(&priv->mutex);
2468 priv->vif = NULL;
2469 priv->is_open = 0;
2470 mutex_unlock(&priv->mutex);
2471 iwl_down(priv);
2472 ieee80211_restart_hw(priv->hw);
2473 } else {
2474 iwl_down(priv);
2475 queue_work(priv->workqueue, &priv->up);
2476 }
Zhu Yib481de92007-09-25 17:54:57 -07002477}
2478
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002479static void iwl_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07002480{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002481 struct iwl_priv *priv =
2482 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07002483
2484 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2485 return;
2486
2487 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08002488 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002489 mutex_unlock(&priv->mutex);
2490}
2491
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002492#define IWL_DELAY_NEXT_SCAN (HZ*2)
2493
Abhijeet Kolekar5bbe2332009-04-08 11:26:35 -07002494void iwl_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002495{
Zhu Yib481de92007-09-25 17:54:57 -07002496 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07002497 int ret = 0;
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002498 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07002499
Johannes Berg05c914f2008-09-11 00:01:58 +02002500 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002501 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07002502 return;
2503 }
2504
Tomas Winklere1623442009-01-27 14:27:56 -08002505 IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
Johannes Berge1749612008-10-27 15:59:26 -07002506 priv->assoc_id, priv->active_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07002507
2508
2509 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2510 return;
2511
Zhu Yib481de92007-09-25 17:54:57 -07002512
Reinette Chatre508e32e2008-04-14 21:16:13 -07002513 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08002514 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07002515
Tomas Winkler2a421b92008-06-12 09:47:10 +08002516 iwl_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08002517
Zhu Yib481de92007-09-25 17:54:57 -07002518 conf = ieee80211_get_hw_conf(priv->hw);
2519
2520 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002521 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002522
Tomas Winkler3195c1f2008-10-08 09:37:30 +08002523 iwl_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07002524 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07002525 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07002526 if (ret)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002527 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07002528 "Attempting to continue.\n");
2529
2530 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2531
Emmanuel Grumbach42eb7c62008-09-17 10:10:05 +08002532 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Ron Rindjunsky4f85f5b2008-06-09 22:54:35 +03002533
Abhijeet Kolekar45823532009-04-08 11:26:44 -07002534 if (priv->cfg->ops->hcmd->set_rxon_chain)
2535 priv->cfg->ops->hcmd->set_rxon_chain(priv);
2536
Zhu Yib481de92007-09-25 17:54:57 -07002537 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2538
Tomas Winklere1623442009-01-27 14:27:56 -08002539 IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07002540 priv->assoc_id, priv->beacon_int);
2541
2542 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2543 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2544 else
2545 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2546
2547 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2548 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2549 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2550 else
2551 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2552
Johannes Berg05c914f2008-09-11 00:01:58 +02002553 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002554 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2555
2556 }
2557
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002558 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002559
2560 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002561 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002562 break;
2563
Johannes Berg05c914f2008-09-11 00:01:58 +02002564 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07002565
Assaf Kraussc46fbef2008-06-12 09:47:05 +08002566 /* assume default assoc id */
2567 priv->assoc_id = 1;
Zhu Yib481de92007-09-25 17:54:57 -07002568
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08002569 iwl_rxon_add_station(priv, priv->bssid, 0);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002570 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002571
2572 break;
2573
2574 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08002575 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002576 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002577 break;
2578 }
2579
Johannes Berg05c914f2008-09-11 00:01:58 +02002580 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002581 priv->assoc_station_added = 1;
2582
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002583 spin_lock_irqsave(&priv->lock, flags);
2584 iwl_activate_qos(priv, 0);
2585 spin_unlock_irqrestore(&priv->lock, flags);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08002586
Grumbach, Emmanuel04816442008-09-03 11:26:53 +08002587 /* the chain noise calibration will enabled PM upon completion
2588 * If chain noise has already been run, then we need to enable
2589 * power management here */
2590 if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
Johannes Berge312c242009-08-07 15:41:51 -07002591 iwl_power_update_mode(priv, false);
Emmanuel Grumbachc90a74ba2008-09-03 11:26:50 +08002592
2593 /* Enable Rx differential gain and sensitivity calibrations */
2594 iwl_chain_noise_reset(priv);
2595 priv->start_calib = 1;
2596
Reinette Chatre508e32e2008-04-14 21:16:13 -07002597}
2598
Zhu Yib481de92007-09-25 17:54:57 -07002599/*****************************************************************************
2600 *
2601 * mac80211 entry point functions
2602 *
2603 *****************************************************************************/
2604
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08002605#define UCODE_READY_TIMEOUT (4 * HZ)
Zhu Yi5a669262008-01-14 17:46:18 -08002606
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002607/*
2608 * Not a mac80211 entry point function, but it fits in with all the
2609 * other mac80211 functions grouped here.
2610 */
Johannes Berg158bea02010-01-22 14:22:53 -08002611static int iwl_mac_setup_register(struct iwl_priv *priv)
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002612{
2613 int ret;
2614 struct ieee80211_hw *hw = priv->hw;
2615 hw->rate_control_algorithm = "iwl-agn-rs";
2616
2617 /* Tell mac80211 our characteristics */
2618 hw->flags = IEEE80211_HW_SIGNAL_DBM |
2619 IEEE80211_HW_NOISE_DBM |
2620 IEEE80211_HW_AMPDU_AGGREGATION |
2621 IEEE80211_HW_SPECTRUM_MGMT;
2622
2623 if (!priv->cfg->broken_powersave)
2624 hw->flags |= IEEE80211_HW_SUPPORTS_PS |
2625 IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
2626
Johannes Bergba37a3d2009-12-10 14:37:27 -08002627 if (priv->cfg->sku & IWL_SKU_N)
2628 hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
2629 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
2630
Reinette Chatre8d9698b2009-10-16 14:25:55 -07002631 hw->sta_data_size = sizeof(struct iwl_station_priv);
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002632 hw->wiphy->interface_modes =
2633 BIT(NL80211_IFTYPE_STATION) |
2634 BIT(NL80211_IFTYPE_ADHOC);
2635
Johannes Berg5be83de2009-11-19 00:56:28 +01002636 hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY |
2637 WIPHY_FLAG_DISABLE_BEACON_HINTS;
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002638
2639 /*
2640 * For now, disable PS by default because it affects
2641 * RX performance significantly.
2642 */
Johannes Berg5be83de2009-11-19 00:56:28 +01002643 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
Reinette Chatref0b6e2e2009-10-16 14:25:53 -07002644
2645 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
2646 /* we create the 802.11 header and a zero-length SSID element */
2647 hw->wiphy->max_scan_ie_len = IWL_MAX_PROBE_REQUEST - 24 - 2;
2648
2649 /* Default value; 4 EDCA QOS priorities */
2650 hw->queues = 4;
2651
2652 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
2653
2654 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
2655 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
2656 &priv->bands[IEEE80211_BAND_2GHZ];
2657 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
2658 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
2659 &priv->bands[IEEE80211_BAND_5GHZ];
2660
2661 ret = ieee80211_register_hw(priv->hw);
2662 if (ret) {
2663 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
2664 return ret;
2665 }
2666 priv->mac80211_registered = 1;
2667
2668 return 0;
2669}
2670
2671
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002672static int iwl_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002673{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002674 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08002675 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002676
Tomas Winklere1623442009-01-27 14:27:56 -08002677 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002678
2679 /* we should be verifying the device is ready to be opened */
2680 mutex_lock(&priv->mutex);
2681
Zhu Yi5a669262008-01-14 17:46:18 -08002682 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
2683 * ucode filename and max sizes are card-specific. */
2684
2685 if (!priv->ucode_code.len) {
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002686 ret = iwl_read_ucode(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08002687 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002688 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08002689 mutex_unlock(&priv->mutex);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002690 return ret;
Zhu Yi5a669262008-01-14 17:46:18 -08002691 }
2692 }
2693
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002694 ret = __iwl_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08002695
Zhu Yib481de92007-09-25 17:54:57 -07002696 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08002697
Zhu Yie655b9f2008-01-24 02:19:38 -08002698 if (ret)
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002699 return ret;
Zhu Yie655b9f2008-01-24 02:19:38 -08002700
Tomas Winklerc1842d62008-08-04 16:00:43 +08002701 if (iwl_is_rfkill(priv))
2702 goto out;
2703
Tomas Winklere1623442009-01-27 14:27:56 -08002704 IWL_DEBUG_INFO(priv, "Start UP work done.\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08002705
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002706 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
Zhu Yi5a669262008-01-14 17:46:18 -08002707 * mac80211 will not be run successfully. */
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08002708 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2709 test_bit(STATUS_READY, &priv->status),
2710 UCODE_READY_TIMEOUT);
2711 if (!ret) {
2712 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002713 IWL_ERR(priv, "START_ALIVE timeout after %dms.\n",
Emmanuel Grumbach154b25c2008-06-30 17:23:24 +08002714 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002715 return -ETIMEDOUT;
Zhu Yi5a669262008-01-14 17:46:18 -08002716 }
Ron Rindjunskyfe9b6b72008-05-29 16:35:06 +08002717 }
Tomas Winkler0a078ff2008-06-30 17:23:26 +08002718
Johannes Berge932a602009-10-02 13:44:03 -07002719 iwl_led_start(priv);
2720
Tomas Winklerc1842d62008-08-04 16:00:43 +08002721out:
Tomas Winkler0a078ff2008-06-30 17:23:26 +08002722 priv->is_open = 1;
Tomas Winklere1623442009-01-27 14:27:56 -08002723 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002724 return 0;
2725}
2726
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002727static void iwl_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07002728{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002729 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002730
Tomas Winklere1623442009-01-27 14:27:56 -08002731 IWL_DEBUG_MAC80211(priv, "enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08002732
Johannes Berg19cc1082009-05-08 13:44:36 -07002733 if (!priv->is_open)
Zhu Yie655b9f2008-01-24 02:19:38 -08002734 return;
Zhu Yie655b9f2008-01-24 02:19:38 -08002735
Zhu Yib481de92007-09-25 17:54:57 -07002736 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08002737
Wey-Yi Guy5bddf542009-08-21 13:34:25 -07002738 if (iwl_is_ready_rf(priv) || test_bit(STATUS_SCAN_HW, &priv->status)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08002739 /* stop mac, cancel any scan request and clear
2740 * RXON_FILTER_ASSOC_MSK BIT
2741 */
Zhu Yi5a669262008-01-14 17:46:18 -08002742 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002743 iwl_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002744 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08002745 }
2746
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002747 iwl_down(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08002748
2749 flush_workqueue(priv->workqueue);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01002750
2751 /* enable interrupts again in order to receive rfkill changes */
2752 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2753 iwl_enable_interrupts(priv);
Mohamed Abbas948c1712007-10-25 17:15:45 +08002754
Tomas Winklere1623442009-01-27 14:27:56 -08002755 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002756}
2757
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002758static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002759{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002760 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002761
Tomas Winklere1623442009-01-27 14:27:56 -08002762 IWL_DEBUG_MACDUMP(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002763
Tomas Winklere1623442009-01-27 14:27:56 -08002764 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02002765 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07002766
Johannes Berge039fa42008-05-15 12:55:29 +02002767 if (iwl_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07002768 dev_kfree_skb_any(skb);
2769
Tomas Winklere1623442009-01-27 14:27:56 -08002770 IWL_DEBUG_MACDUMP(priv, "leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08002771 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07002772}
2773
Abhijeet Kolekar60690a62009-04-08 11:26:49 -07002774void iwl_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002775{
Tomas Winkler857485c2008-03-21 13:53:44 -07002776 int ret = 0;
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002777 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07002778
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08002779 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07002780 return;
2781
2782 /* The following should be done only at AP bring up */
Tomas Winkler3195c1f2008-10-08 09:37:30 +08002783 if (!iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002784
2785 /* RXON - unassoc (to set timing command) */
2786 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002787 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002788
2789 /* RXON Timing */
Tomas Winkler3195c1f2008-10-08 09:37:30 +08002790 iwl_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07002791 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07002792 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07002793 if (ret)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002794 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07002795 "Attempting to continue.\n");
2796
Daniel C Halperinf513dff2009-11-13 11:56:34 -08002797 /* AP has all antennas */
2798 priv->chain_noise_data.active_chains =
2799 priv->hw_params.valid_rx_ant;
2800 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Abhijeet Kolekar45823532009-04-08 11:26:44 -07002801 if (priv->cfg->ops->hcmd->set_rxon_chain)
2802 priv->cfg->ops->hcmd->set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002803
2804 /* FIXME: what should be the assoc_id for AP? */
2805 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2806 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2807 priv->staging_rxon.flags |=
2808 RXON_FLG_SHORT_PREAMBLE_MSK;
2809 else
2810 priv->staging_rxon.flags &=
2811 ~RXON_FLG_SHORT_PREAMBLE_MSK;
2812
2813 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2814 if (priv->assoc_capability &
2815 WLAN_CAPABILITY_SHORT_SLOT_TIME)
2816 priv->staging_rxon.flags |=
2817 RXON_FLG_SHORT_SLOT_MSK;
2818 else
2819 priv->staging_rxon.flags &=
2820 ~RXON_FLG_SHORT_SLOT_MSK;
2821
Johannes Berg05c914f2008-09-11 00:01:58 +02002822 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Zhu Yib481de92007-09-25 17:54:57 -07002823 priv->staging_rxon.flags &=
2824 ~RXON_FLG_SHORT_SLOT_MSK;
2825 }
2826 /* restore RXON assoc */
2827 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07002828 iwlcore_commit_rxon(priv);
Daniel C Halperinf513dff2009-11-13 11:56:34 -08002829 iwl_reset_qos(priv);
Emmanuel Grumbach1ff50bd2008-07-11 11:53:34 +08002830 spin_lock_irqsave(&priv->lock, flags);
2831 iwl_activate_qos(priv, 1);
2832 spin_unlock_irqrestore(&priv->lock, flags);
Reinette Chatre9a9ca652009-10-30 14:36:12 -07002833 iwl_add_bcast_station(priv);
Zhu Yie1493de2007-09-27 11:27:32 +08002834 }
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002835 iwl_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002836
2837 /* FIXME - we need to add code here to detect a totally new
2838 * configuration, reset the AP, unassoc, rxon timing, assoc,
2839 * clear sta table, add BCAST sta... */
2840}
2841
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002842static void iwl_mac_update_tkip_key(struct ieee80211_hw *hw,
Johannes Bergb3fbdcf2010-01-21 11:40:47 +01002843 struct ieee80211_vif *vif,
2844 struct ieee80211_key_conf *keyconf,
2845 struct ieee80211_sta *sta,
2846 u32 iv32, u16 *phase1key)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002847{
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002848
Tomas Winkler9f586712008-11-12 13:14:05 -08002849 struct iwl_priv *priv = hw->priv;
Tomas Winklere1623442009-01-27 14:27:56 -08002850 IWL_DEBUG_MAC80211(priv, "enter\n");
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002851
Johannes Bergb3fbdcf2010-01-21 11:40:47 +01002852 iwl_update_tkip_key(priv, keyconf,
2853 sta ? sta->addr : iwl_bcast_addr,
2854 iv32, phase1key);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002855
Tomas Winklere1623442009-01-27 14:27:56 -08002856 IWL_DEBUG_MAC80211(priv, "leave\n");
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02002857}
2858
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002859static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01002860 struct ieee80211_vif *vif,
2861 struct ieee80211_sta *sta,
Zhu Yib481de92007-09-25 17:54:57 -07002862 struct ieee80211_key_conf *key)
2863{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002864 struct iwl_priv *priv = hw->priv;
Winkler, Tomas42986792009-01-19 15:30:22 -08002865 const u8 *addr;
2866 int ret;
2867 u8 sta_id;
2868 bool is_default_wep_key = false;
Zhu Yib481de92007-09-25 17:54:57 -07002869
Tomas Winklere1623442009-01-27 14:27:56 -08002870 IWL_DEBUG_MAC80211(priv, "enter\n");
Zhu Yib481de92007-09-25 17:54:57 -07002871
Tomas Winkler90e8e422009-06-19 13:52:42 -07002872 if (priv->cfg->mod_params->sw_crypto) {
Tomas Winklere1623442009-01-27 14:27:56 -08002873 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
Zhu Yib481de92007-09-25 17:54:57 -07002874 return -EOPNOTSUPP;
2875 }
Winkler, Tomas42986792009-01-19 15:30:22 -08002876 addr = sta ? sta->addr : iwl_bcast_addr;
Tomas Winklerc587de02009-06-03 11:44:07 -07002877 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002878 if (sta_id == IWL_INVALID_STATION) {
Tomas Winklere1623442009-01-27 14:27:56 -08002879 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
Johannes Berge1749612008-10-27 15:59:26 -07002880 addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002881 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002882
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002883 }
Zhu Yib481de92007-09-25 17:54:57 -07002884
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002885 mutex_lock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08002886 iwl_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002887 mutex_unlock(&priv->mutex);
2888
2889 /* If we are getting WEP group key and we didn't receive any key mapping
2890 * so far, we are in legacy wep mode (group key only), otherwise we are
2891 * in 1X mode.
2892 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07002893 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002894 priv->iw_mode != NL80211_IFTYPE_AP) {
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002895 if (cmd == SET_KEY)
2896 is_default_wep_key = !priv->key_mapping_key;
2897 else
Emmanuel Grumbachccc038a2008-05-15 13:54:09 +08002898 is_default_wep_key =
2899 (key->hw_key_idx == HW_KEY_DEFAULT);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002900 }
mabbas052c4b92007-10-25 17:15:43 +08002901
Zhu Yib481de92007-09-25 17:54:57 -07002902 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002903 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002904 if (is_default_wep_key)
2905 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002906 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07002907 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002908
Tomas Winklere1623442009-01-27 14:27:56 -08002909 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07002910 break;
2911 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07002912 if (is_default_wep_key)
2913 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002914 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07002915 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002916
Tomas Winklere1623442009-01-27 14:27:56 -08002917 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07002918 break;
2919 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002920 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07002921 }
2922
Tomas Winklere1623442009-01-27 14:27:56 -08002923 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002924
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07002925 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002926}
2927
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002928static int iwl_mac_ampdu_action(struct ieee80211_hw *hw,
Johannes Bergc951ad32009-11-16 12:00:38 +01002929 struct ieee80211_vif *vif,
Tomas Winklerd783b062008-07-18 13:53:02 +08002930 enum ieee80211_ampdu_mlme_action action,
Johannes Berg17741cd2008-09-11 00:02:02 +02002931 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Tomas Winklerd783b062008-07-18 13:53:02 +08002932{
2933 struct iwl_priv *priv = hw->priv;
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002934 int ret;
Tomas Winklerd783b062008-07-18 13:53:02 +08002935
Tomas Winklere1623442009-01-27 14:27:56 -08002936 IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
Johannes Berge1749612008-10-27 15:59:26 -07002937 sta->addr, tid);
Tomas Winklerd783b062008-07-18 13:53:02 +08002938
2939 if (!(priv->cfg->sku & IWL_SKU_N))
2940 return -EACCES;
2941
2942 switch (action) {
2943 case IEEE80211_AMPDU_RX_START:
Tomas Winklere1623442009-01-27 14:27:56 -08002944 IWL_DEBUG_HT(priv, "start Rx\n");
Tomas Winkler9f586712008-11-12 13:14:05 -08002945 return iwl_sta_rx_agg_start(priv, sta->addr, tid, *ssn);
Tomas Winklerd783b062008-07-18 13:53:02 +08002946 case IEEE80211_AMPDU_RX_STOP:
Tomas Winklere1623442009-01-27 14:27:56 -08002947 IWL_DEBUG_HT(priv, "stop Rx\n");
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002948 ret = iwl_sta_rx_agg_stop(priv, sta->addr, tid);
2949 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2950 return 0;
2951 else
2952 return ret;
Tomas Winklerd783b062008-07-18 13:53:02 +08002953 case IEEE80211_AMPDU_TX_START:
Tomas Winklere1623442009-01-27 14:27:56 -08002954 IWL_DEBUG_HT(priv, "start Tx\n");
Johannes Berg17741cd2008-09-11 00:02:02 +02002955 return iwl_tx_agg_start(priv, sta->addr, tid, ssn);
Tomas Winklerd783b062008-07-18 13:53:02 +08002956 case IEEE80211_AMPDU_TX_STOP:
Tomas Winklere1623442009-01-27 14:27:56 -08002957 IWL_DEBUG_HT(priv, "stop Tx\n");
Wey-Yi Guy5c2207c2009-03-17 21:51:43 -07002958 ret = iwl_tx_agg_stop(priv, sta->addr, tid);
2959 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2960 return 0;
2961 else
2962 return ret;
Wey-Yi Guyf0527972010-01-08 10:04:41 -08002963 case IEEE80211_AMPDU_TX_OPERATIONAL:
2964 /* do nothing */
2965 return -EOPNOTSUPP;
Tomas Winklerd783b062008-07-18 13:53:02 +08002966 default:
Tomas Winklere1623442009-01-27 14:27:56 -08002967 IWL_DEBUG_HT(priv, "unknown\n");
Tomas Winklerd783b062008-07-18 13:53:02 +08002968 return -EINVAL;
2969 break;
2970 }
2971 return 0;
2972}
Tomas Winkler9f586712008-11-12 13:14:05 -08002973
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07002974static int iwl_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07002975 struct ieee80211_low_level_stats *stats)
2976{
Ester Kummerbf403db2008-05-05 10:22:40 +08002977 struct iwl_priv *priv = hw->priv;
2978
2979 priv = hw->priv;
Tomas Winklere1623442009-01-27 14:27:56 -08002980 IWL_DEBUG_MAC80211(priv, "enter\n");
2981 IWL_DEBUG_MAC80211(priv, "leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07002982
2983 return 0;
2984}
2985
Johannes Berg6ab10ff2009-11-13 11:56:37 -08002986static void iwl_mac_sta_notify(struct ieee80211_hw *hw,
2987 struct ieee80211_vif *vif,
2988 enum sta_notify_cmd cmd,
2989 struct ieee80211_sta *sta)
2990{
2991 struct iwl_priv *priv = hw->priv;
2992 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
2993 int sta_id;
2994
2995 /*
2996 * TODO: We really should use this callback to
2997 * actually maintain the station table in
2998 * the device.
2999 */
3000
3001 switch (cmd) {
3002 case STA_NOTIFY_ADD:
3003 atomic_set(&sta_priv->pending_frames, 0);
3004 if (vif->type == NL80211_IFTYPE_AP)
3005 sta_priv->client = true;
3006 break;
3007 case STA_NOTIFY_SLEEP:
3008 WARN_ON(!sta_priv->client);
3009 sta_priv->asleep = true;
3010 if (atomic_read(&sta_priv->pending_frames) > 0)
3011 ieee80211_sta_block_awake(hw, sta, true);
3012 break;
3013 case STA_NOTIFY_AWAKE:
3014 WARN_ON(!sta_priv->client);
3015 sta_priv->asleep = false;
3016 sta_id = iwl_find_station(priv, sta->addr);
3017 if (sta_id != IWL_INVALID_STATION)
3018 iwl_sta_modify_ps_wake(priv, sta_id);
3019 break;
3020 default:
3021 break;
3022 }
3023}
3024
Zhu Yib481de92007-09-25 17:54:57 -07003025/*****************************************************************************
3026 *
3027 * sysfs attributes
3028 *
3029 *****************************************************************************/
3030
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003031#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003032
3033/*
3034 * The following adds a new attribute to the sysfs representation
Wu, Fengguangc3a739f2008-12-17 16:52:29 +08003035 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
Zhu Yib481de92007-09-25 17:54:57 -07003036 * used for controlling the debug level.
3037 *
3038 * See the level definitions in iwl for details.
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003039 *
Reinette Chatre3d816c72009-08-07 15:41:37 -07003040 * The debug_level being managed using sysfs below is a per device debug
3041 * level that is used instead of the global debug level if it (the per
3042 * device debug level) is set.
Zhu Yib481de92007-09-25 17:54:57 -07003043 */
Ester Kummer8cf769c2008-05-06 11:05:11 +08003044static ssize_t show_debug_level(struct device *d,
3045 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07003046{
Reinette Chatre3d816c72009-08-07 15:41:37 -07003047 struct iwl_priv *priv = dev_get_drvdata(d);
3048 return sprintf(buf, "0x%08X\n", iwl_get_debug_level(priv));
Zhu Yib481de92007-09-25 17:54:57 -07003049}
Ester Kummer8cf769c2008-05-06 11:05:11 +08003050static ssize_t store_debug_level(struct device *d,
3051 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07003052 const char *buf, size_t count)
3053{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003054 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler9257746f2008-09-03 11:26:32 +08003055 unsigned long val;
3056 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003057
Tomas Winkler9257746f2008-09-03 11:26:32 +08003058 ret = strict_strtoul(buf, 0, &val);
3059 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003060 IWL_ERR(priv, "%s is not in hex or decimal form.\n", buf);
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003061 else {
Reinette Chatre3d816c72009-08-07 15:41:37 -07003062 priv->debug_level = val;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003063 if (iwl_alloc_traffic_mem(priv))
3064 IWL_ERR(priv,
3065 "Not enough memory to generate traffic log\n");
3066 }
Zhu Yib481de92007-09-25 17:54:57 -07003067 return strnlen(buf, count);
3068}
3069
Ester Kummer8cf769c2008-05-06 11:05:11 +08003070static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3071 show_debug_level, store_debug_level);
3072
Zhu Yib481de92007-09-25 17:54:57 -07003073
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003074#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07003075
Zhu Yib481de92007-09-25 17:54:57 -07003076
3077static ssize_t show_temperature(struct device *d,
3078 struct device_attribute *attr, char *buf)
3079{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003080 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003081
Tomas Winklerfee12472008-04-03 16:05:21 -07003082 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003083 return -EAGAIN;
3084
Emmanuel Grumbach91dbc5b2008-06-12 09:47:14 +08003085 return sprintf(buf, "%d\n", priv->temperature);
Zhu Yib481de92007-09-25 17:54:57 -07003086}
3087
3088static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3089
Zhu Yib481de92007-09-25 17:54:57 -07003090static ssize_t show_tx_power(struct device *d,
3091 struct device_attribute *attr, char *buf)
3092{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003093 struct iwl_priv *priv = dev_get_drvdata(d);
Jay Sternberg91f39e82008-12-17 16:52:34 +08003094
3095 if (!iwl_is_ready_rf(priv))
3096 return sprintf(buf, "off\n");
3097 else
3098 return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
Zhu Yib481de92007-09-25 17:54:57 -07003099}
3100
3101static ssize_t store_tx_power(struct device *d,
3102 struct device_attribute *attr,
3103 const char *buf, size_t count)
3104{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003105 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler9257746f2008-09-03 11:26:32 +08003106 unsigned long val;
3107 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003108
Tomas Winkler9257746f2008-09-03 11:26:32 +08003109 ret = strict_strtoul(buf, 10, &val);
3110 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08003111 IWL_INFO(priv, "%s is not in decimal form.\n", buf);
Wey-Yi Guy5eadd942009-08-21 13:34:17 -07003112 else {
3113 ret = iwl_set_tx_power(priv, val, false);
3114 if (ret)
3115 IWL_ERR(priv, "failed setting tx power (0x%d).\n",
3116 ret);
3117 else
3118 ret = count;
3119 }
3120 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003121}
3122
3123static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3124
3125static ssize_t show_flags(struct device *d,
3126 struct device_attribute *attr, char *buf)
3127{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003128 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003129
3130 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3131}
3132
3133static ssize_t store_flags(struct device *d,
3134 struct device_attribute *attr,
3135 const char *buf, size_t count)
3136{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003137 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler9257746f2008-09-03 11:26:32 +08003138 unsigned long val;
3139 u32 flags;
3140 int ret = strict_strtoul(buf, 0, &val);
Emmanuel Grumbach926f0b22008-09-03 11:26:39 +08003141 if (ret)
Tomas Winkler9257746f2008-09-03 11:26:32 +08003142 return ret;
3143 flags = (u32)val;
Zhu Yib481de92007-09-25 17:54:57 -07003144
3145 mutex_lock(&priv->mutex);
3146 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3147 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003148 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003149 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003150 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003151 IWL_DEBUG_INFO(priv, "Commit rxon.flags = 0x%04X\n", flags);
Zhu Yib481de92007-09-25 17:54:57 -07003152 priv->staging_rxon.flags = cpu_to_le32(flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003153 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003154 }
3155 }
3156 mutex_unlock(&priv->mutex);
3157
3158 return count;
3159}
3160
3161static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3162
3163static ssize_t show_filter_flags(struct device *d,
3164 struct device_attribute *attr, char *buf)
3165{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003166 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07003167
3168 return sprintf(buf, "0x%04X\n",
3169 le32_to_cpu(priv->active_rxon.filter_flags));
3170}
3171
3172static ssize_t store_filter_flags(struct device *d,
3173 struct device_attribute *attr,
3174 const char *buf, size_t count)
3175{
Greg Kroah-Hartman928841b2009-04-30 23:02:47 -07003176 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler9257746f2008-09-03 11:26:32 +08003177 unsigned long val;
3178 u32 filter_flags;
3179 int ret = strict_strtoul(buf, 0, &val);
Emmanuel Grumbach926f0b22008-09-03 11:26:39 +08003180 if (ret)
Tomas Winkler9257746f2008-09-03 11:26:32 +08003181 return ret;
3182 filter_flags = (u32)val;
Zhu Yib481de92007-09-25 17:54:57 -07003183
3184 mutex_lock(&priv->mutex);
3185 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3186 /* Cancel any currently running scans... */
Tomas Winkler2a421b92008-06-12 09:47:10 +08003187 if (iwl_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003188 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003189 else {
Tomas Winklere1623442009-01-27 14:27:56 -08003190 IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
Zhu Yib481de92007-09-25 17:54:57 -07003191 "0x%04X\n", filter_flags);
3192 priv->staging_rxon.filter_flags =
3193 cpu_to_le32(filter_flags);
Abhijeet Kolekare0158e62009-04-08 11:26:37 -07003194 iwlcore_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003195 }
3196 }
3197 mutex_unlock(&priv->mutex);
3198
3199 return count;
3200}
3201
3202static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3203 store_filter_flags);
3204
Zhu Yib481de92007-09-25 17:54:57 -07003205
3206static ssize_t show_statistics(struct device *d,
3207 struct device_attribute *attr, char *buf)
3208{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003209 struct iwl_priv *priv = dev_get_drvdata(d);
Emmanuel Grumbach8f91aec2008-06-30 17:23:07 +08003210 u32 size = sizeof(struct iwl_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07003211 u32 len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003212 u8 *data = (u8 *)&priv->statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003213 int rc = 0;
3214
Tomas Winklerfee12472008-04-03 16:05:21 -07003215 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003216 return -EAGAIN;
3217
3218 mutex_lock(&priv->mutex);
Wey-Yi Guyef8d5522009-11-13 11:56:28 -08003219 rc = iwl_send_statistics_request(priv, CMD_SYNC, false);
Zhu Yib481de92007-09-25 17:54:57 -07003220 mutex_unlock(&priv->mutex);
3221
3222 if (rc) {
3223 len = sprintf(buf,
3224 "Error sending statistics request: 0x%08X\n", rc);
3225 return len;
3226 }
3227
3228 while (size && (PAGE_SIZE - len)) {
3229 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3230 PAGE_SIZE - len, 1);
3231 len = strlen(buf);
3232 if (PAGE_SIZE - len)
3233 buf[len++] = '\n';
3234
3235 ofs += 16;
3236 size -= min(size, 16U);
3237 }
3238
3239 return len;
3240}
3241
3242static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
3243
Wey-Yi Guy01abfbb2009-09-17 10:43:45 -07003244static ssize_t show_rts_ht_protection(struct device *d,
3245 struct device_attribute *attr, char *buf)
3246{
3247 struct iwl_priv *priv = dev_get_drvdata(d);
3248
3249 return sprintf(buf, "%s\n",
3250 priv->cfg->use_rts_for_ht ? "RTS/CTS" : "CTS-to-self");
3251}
3252
3253static ssize_t store_rts_ht_protection(struct device *d,
3254 struct device_attribute *attr,
3255 const char *buf, size_t count)
3256{
3257 struct iwl_priv *priv = dev_get_drvdata(d);
3258 unsigned long val;
3259 int ret;
3260
3261 ret = strict_strtoul(buf, 10, &val);
3262 if (ret)
3263 IWL_INFO(priv, "Input is not in decimal form.\n");
3264 else {
3265 if (!iwl_is_associated(priv))
3266 priv->cfg->use_rts_for_ht = val ? true : false;
3267 else
3268 IWL_ERR(priv, "Sta associated with AP - "
3269 "Change protection mechanism is not allowed\n");
3270 ret = count;
3271 }
3272 return ret;
3273}
3274
3275static DEVICE_ATTR(rts_ht_protection, S_IWUSR | S_IRUGO,
3276 show_rts_ht_protection, store_rts_ht_protection);
3277
Zhu Yib481de92007-09-25 17:54:57 -07003278
Zhu Yib481de92007-09-25 17:54:57 -07003279/*****************************************************************************
3280 *
3281 * driver setup and teardown
3282 *
3283 *****************************************************************************/
3284
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003285static void iwl_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003286{
Reinette Chatred21050c2009-02-13 11:51:18 -08003287 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
Zhu Yib481de92007-09-25 17:54:57 -07003288
3289 init_waitqueue_head(&priv->wait_command_queue);
3290
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003291 INIT_WORK(&priv->up, iwl_bg_up);
3292 INIT_WORK(&priv->restart, iwl_bg_restart);
3293 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003294 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
Emmanuel Grumbach16e727e2008-06-12 09:46:52 +08003295 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
Tomas Winkler4a4a9e82008-05-29 16:34:54 +08003296 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
3297 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
Tomas Winkler2a421b92008-06-12 09:47:10 +08003298
Tomas Winkler2a421b92008-06-12 09:47:10 +08003299 iwl_setup_scan_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003300
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003301 if (priv->cfg->ops->lib->setup_deferred_work)
3302 priv->cfg->ops->lib->setup_deferred_work(priv);
3303
3304 init_timer(&priv->statistics_periodic);
3305 priv->statistics_periodic.data = (unsigned long)priv;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003306 priv->statistics_periodic.function = iwl_bg_statistics_periodic;
Zhu Yib481de92007-09-25 17:54:57 -07003307
Wey-Yi Guya9e1cb62009-12-10 14:37:26 -08003308 init_timer(&priv->ucode_trace);
3309 priv->ucode_trace.data = (unsigned long)priv;
3310 priv->ucode_trace.function = iwl_bg_ucode_trace;
3311
Mohamed Abbasef850d72009-05-22 11:01:50 -07003312 if (!priv->cfg->use_isr_legacy)
3313 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
3314 iwl_irq_tasklet, (unsigned long)priv);
3315 else
3316 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
3317 iwl_irq_tasklet_legacy, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07003318}
3319
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003320static void iwl_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003321{
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003322 if (priv->cfg->ops->lib->cancel_deferred_work)
3323 priv->cfg->ops->lib->cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003324
Joonwoo Park3ae6a052007-11-29 10:43:16 +09003325 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003326 cancel_delayed_work(&priv->scan_check);
3327 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07003328 cancel_work_sync(&priv->beacon_update);
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003329 del_timer_sync(&priv->statistics_periodic);
Wey-Yi Guya9e1cb62009-12-10 14:37:26 -08003330 del_timer_sync(&priv->ucode_trace);
Zhu Yib481de92007-09-25 17:54:57 -07003331}
3332
Reinette Chatre89f186a2009-10-30 14:36:11 -07003333static void iwl_init_hw_rates(struct iwl_priv *priv,
3334 struct ieee80211_rate *rates)
3335{
3336 int i;
3337
3338 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
3339 rates[i].bitrate = iwl_rates[i].ieee * 5;
3340 rates[i].hw_value = i; /* Rate scaling will work on indexes */
3341 rates[i].hw_value_short = i;
3342 rates[i].flags = 0;
3343 if ((i >= IWL_FIRST_CCK_RATE) && (i <= IWL_LAST_CCK_RATE)) {
3344 /*
3345 * If CCK != 1M then set short preamble rate flag.
3346 */
3347 rates[i].flags |=
3348 (iwl_rates[i].plcp == IWL_RATE_1M_PLCP) ?
3349 0 : IEEE80211_RATE_SHORT_PREAMBLE;
3350 }
3351 }
3352}
3353
3354static int iwl_init_drv(struct iwl_priv *priv)
3355{
3356 int ret;
3357
3358 priv->ibss_beacon = NULL;
3359
Reinette Chatre89f186a2009-10-30 14:36:11 -07003360 spin_lock_init(&priv->sta_lock);
3361 spin_lock_init(&priv->hcmd_lock);
3362
3363 INIT_LIST_HEAD(&priv->free_frames);
3364
3365 mutex_init(&priv->mutex);
3366
3367 /* Clear the driver's (not device's) station table */
3368 iwl_clear_stations_table(priv);
3369
3370 priv->ieee_channels = NULL;
3371 priv->ieee_rates = NULL;
3372 priv->band = IEEE80211_BAND_2GHZ;
3373
3374 priv->iw_mode = NL80211_IFTYPE_STATION;
Johannes Bergba37a3d2009-12-10 14:37:27 -08003375 priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
Wey-Yi Guya13d2762010-01-22 14:22:42 -08003376 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
Reinette Chatre89f186a2009-10-30 14:36:11 -07003377
3378 /* Choose which receivers/antennas to use */
3379 if (priv->cfg->ops->hcmd->set_rxon_chain)
3380 priv->cfg->ops->hcmd->set_rxon_chain(priv);
3381
3382 iwl_init_scan_params(priv);
3383
3384 iwl_reset_qos(priv);
3385
3386 priv->qos_data.qos_active = 0;
3387 priv->qos_data.qos_cap.val = 0;
3388
3389 priv->rates_mask = IWL_RATES_MASK;
3390 /* Set the tx_power_user_lmt to the lowest power level
3391 * this value will get overwritten by channel max power avg
3392 * from eeprom */
3393 priv->tx_power_user_lmt = IWL_TX_POWER_TARGET_POWER_MIN;
3394
3395 ret = iwl_init_channel_map(priv);
3396 if (ret) {
3397 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
3398 goto err;
3399 }
3400
3401 ret = iwlcore_init_geos(priv);
3402 if (ret) {
3403 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
3404 goto err_free_channel_map;
3405 }
3406 iwl_init_hw_rates(priv, priv->ieee_rates);
3407
3408 return 0;
3409
3410err_free_channel_map:
3411 iwl_free_channel_map(priv);
3412err:
3413 return ret;
3414}
3415
3416static void iwl_uninit_drv(struct iwl_priv *priv)
3417{
3418 iwl_calib_free_results(priv);
3419 iwlcore_free_geos(priv);
3420 iwl_free_channel_map(priv);
3421 kfree(priv->scan);
3422}
3423
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003424static struct attribute *iwl_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07003425 &dev_attr_flags.attr,
3426 &dev_attr_filter_flags.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003427 &dev_attr_statistics.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003428 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07003429 &dev_attr_tx_power.attr,
Wey-Yi Guy01abfbb2009-09-17 10:43:45 -07003430 &dev_attr_rts_ht_protection.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08003431#ifdef CONFIG_IWLWIFI_DEBUG
3432 &dev_attr_debug_level.attr,
3433#endif
Zhu Yib481de92007-09-25 17:54:57 -07003434 NULL
3435};
3436
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003437static struct attribute_group iwl_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07003438 .name = NULL, /* put in device directory */
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003439 .attrs = iwl_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07003440};
3441
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003442static struct ieee80211_ops iwl_hw_ops = {
3443 .tx = iwl_mac_tx,
3444 .start = iwl_mac_start,
3445 .stop = iwl_mac_stop,
3446 .add_interface = iwl_mac_add_interface,
3447 .remove_interface = iwl_mac_remove_interface,
3448 .config = iwl_mac_config,
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003449 .configure_filter = iwl_configure_filter,
3450 .set_key = iwl_mac_set_key,
3451 .update_tkip_key = iwl_mac_update_tkip_key,
3452 .get_stats = iwl_mac_get_stats,
3453 .get_tx_stats = iwl_mac_get_tx_stats,
3454 .conf_tx = iwl_mac_conf_tx,
3455 .reset_tsf = iwl_mac_reset_tsf,
3456 .bss_info_changed = iwl_bss_info_changed,
3457 .ampdu_action = iwl_mac_ampdu_action,
Johannes Berg6ab10ff2009-11-13 11:56:37 -08003458 .hw_scan = iwl_mac_hw_scan,
3459 .sta_notify = iwl_mac_sta_notify,
Zhu Yib481de92007-09-25 17:54:57 -07003460};
3461
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003462static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07003463{
3464 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003465 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07003466 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08003467 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003468 unsigned long flags;
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003469 u16 pci_cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003470
Assaf Krauss316c30d2008-03-14 10:38:46 -07003471 /************************
3472 * 1. Allocating HW data
3473 ************************/
3474
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003475 /* Disabling hardware scan means that mac80211 will perform scans
3476 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07003477 if (cfg->mod_params->disable_hw_scan) {
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003478 if (iwl_debug_level & IWL_DL_INFO)
Ester Kummerbf403db2008-05-05 10:22:40 +08003479 dev_printk(KERN_DEBUG, &(pdev->dev),
3480 "Disabling hw_scan\n");
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003481 iwl_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003482 }
3483
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003484 hw = iwl_alloc_all(cfg, &iwl_hw_ops);
Assaf Krauss1d0a0822008-03-14 10:38:48 -07003485 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07003486 err = -ENOMEM;
3487 goto out;
3488 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07003489 priv = hw->priv;
3490 /* At this point both hw and priv are allocated. */
3491
Zhu Yib481de92007-09-25 17:54:57 -07003492 SET_IEEE80211_DEV(hw, &pdev->dev);
3493
Tomas Winklere1623442009-01-27 14:27:56 -08003494 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08003495 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07003496 priv->pci_dev = pdev;
Mohamed Abbas40cefda2009-05-22 11:01:52 -07003497 priv->inta_mask = CSR_INI_SET_MASK;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003498
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003499#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003500 atomic_set(&priv->restrict_refcnt, 0);
3501#endif
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003502 if (iwl_alloc_traffic_mem(priv))
3503 IWL_ERR(priv, "Not enough memory to generate traffic log\n");
Zhu Yib481de92007-09-25 17:54:57 -07003504
Assaf Krauss316c30d2008-03-14 10:38:46 -07003505 /**************************
3506 * 2. Initializing PCI bus
3507 **************************/
3508 if (pci_enable_device(pdev)) {
3509 err = -ENODEV;
3510 goto out_ieee80211_free_hw;
3511 }
3512
3513 pci_set_master(pdev);
3514
Winkler, Tomas093d8742008-09-26 15:09:34 +08003515 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
Assaf Krauss316c30d2008-03-14 10:38:46 -07003516 if (!err)
Winkler, Tomas093d8742008-09-26 15:09:34 +08003517 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003518 if (err) {
Winkler, Tomas093d8742008-09-26 15:09:34 +08003519 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003520 if (!err)
Winkler, Tomas093d8742008-09-26 15:09:34 +08003521 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003522 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003523 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08003524 IWL_WARN(priv, "No suitable DMA available.\n");
Assaf Krauss316c30d2008-03-14 10:38:46 -07003525 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07003526 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07003527 }
3528
3529 err = pci_request_regions(pdev, DRV_NAME);
3530 if (err)
3531 goto out_pci_disable_device;
3532
3533 pci_set_drvdata(pdev, priv);
3534
Assaf Krauss316c30d2008-03-14 10:38:46 -07003535
3536 /***********************
3537 * 3. Read REV register
3538 ***********************/
3539 priv->hw_base = pci_iomap(pdev, 0, 0);
3540 if (!priv->hw_base) {
3541 err = -ENODEV;
3542 goto out_pci_release_regions;
3543 }
3544
Tomas Winklere1623442009-01-27 14:27:56 -08003545 IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
Assaf Krauss316c30d2008-03-14 10:38:46 -07003546 (unsigned long long) pci_resource_len(pdev, 0));
Tomas Winklere1623442009-01-27 14:27:56 -08003547 IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003548
Reinette Chatre731a29b2009-12-14 14:12:11 -08003549 /* these spin locks will be used in apm_ops.init and EEPROM access
Mohamed Abbasa8b50a02009-05-22 11:01:47 -07003550 * we should init now
3551 */
3552 spin_lock_init(&priv->reg_lock);
Reinette Chatre731a29b2009-12-14 14:12:11 -08003553 spin_lock_init(&priv->lock);
Tomas Winklerb661c812008-04-23 17:14:54 -07003554 iwl_hw_detect(priv);
Tomas Winkler978785a2008-12-19 10:37:31 +08003555 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s REV=0x%X\n",
Tomas Winklerb661c812008-04-23 17:14:54 -07003556 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003557
Tomas Winklere7b63582008-09-03 11:26:49 +08003558 /* We disable the RETRY_TIMEOUT register (0x41) to keep
3559 * PCI Tx retries from interfering with C3 CPU state */
3560 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
3561
Mohamed Abbas086ed112009-05-22 11:01:54 -07003562 iwl_prepare_card_hw(priv);
3563 if (!priv->hw_ready) {
3564 IWL_WARN(priv, "Failed, HW not ready\n");
3565 goto out_iounmap;
3566 }
3567
Assaf Krauss316c30d2008-03-14 10:38:46 -07003568 /*****************
3569 * 4. Read EEPROM
3570 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07003571 /* Read the EEPROM */
3572 err = iwl_eeprom_init(priv);
3573 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003574 IWL_ERR(priv, "Unable to init EEPROM\n");
Assaf Krauss316c30d2008-03-14 10:38:46 -07003575 goto out_iounmap;
3576 }
Tomas Winkler8614f362008-04-23 17:14:55 -07003577 err = iwl_eeprom_check_version(priv);
3578 if (err)
Reinette Chatrec8f16132009-02-27 16:21:22 -08003579 goto out_free_eeprom;
Tomas Winkler8614f362008-04-23 17:14:55 -07003580
Ron Rindjunsky02883012008-05-15 13:53:53 +08003581 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003582 iwl_eeprom_get_mac(priv, priv->mac_addr);
Tomas Winklere1623442009-01-27 14:27:56 -08003583 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003584 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
3585
3586 /************************
3587 * 5. Setup HW constants
3588 ************************/
Ron Rindjunskyda154e32008-06-30 17:23:20 +08003589 if (iwl_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003590 IWL_ERR(priv, "failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07003591 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003592 }
3593
3594 /*******************
Tomas Winkler6ba87952008-05-15 13:54:17 +08003595 * 6. Setup priv
Assaf Krauss316c30d2008-03-14 10:38:46 -07003596 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07003597
Tomas Winkler6ba87952008-05-15 13:54:17 +08003598 err = iwl_init_drv(priv);
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003599 if (err)
Ron Rindjunsky399f49002008-04-23 17:14:56 -07003600 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003601 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07003602
Assaf Krauss316c30d2008-03-14 10:38:46 -07003603 /********************
Abhijeet Kolekar09f9bf72009-04-20 14:37:03 -07003604 * 7. Setup services
Assaf Krauss316c30d2008-03-14 10:38:46 -07003605 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003606 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003607 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003608 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003609
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003610 pci_enable_msi(priv->pci_dev);
3611
Mohamed Abbasef850d72009-05-22 11:01:50 -07003612 iwl_alloc_isr_ict(priv);
3613 err = request_irq(priv->pci_dev->irq, priv->cfg->ops->lib->isr,
3614 IRQF_SHARED, DRV_NAME, priv);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003615 if (err) {
3616 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
3617 goto out_disable_msi;
3618 }
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003619 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003620 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003621 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Helmut Schaa795cc0a2009-02-12 18:51:03 +01003622 goto out_free_irq;
Assaf Krauss316c30d2008-03-14 10:38:46 -07003623 }
3624
Emmanuel Grumbach4e393172008-06-12 09:46:53 +08003625 iwl_setup_deferred_work(priv);
Emmanuel Grumbach653fa4a2008-06-30 17:23:11 +08003626 iwl_setup_rx_handlers(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07003627
Johannes Berg158bea02010-01-22 14:22:53 -08003628 /*********************************************
3629 * 8. Enable interrupts and read RFKILL state
3630 *********************************************/
Tomas Winkler6ba87952008-05-15 13:54:17 +08003631
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003632 /* enable interrupts if needed: hw bug w/a */
3633 pci_read_config_word(priv->pci_dev, PCI_COMMAND, &pci_cmd);
3634 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
3635 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
3636 pci_write_config_word(priv->pci_dev, PCI_COMMAND, pci_cmd);
3637 }
3638
3639 iwl_enable_interrupts(priv);
3640
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003641 /* If platform's RF_KILL switch is NOT set to KILL */
3642 if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3643 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3644 else
3645 set_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winkler6ba87952008-05-15 13:54:17 +08003646
Johannes Berga60e77e2009-06-04 18:26:06 +02003647 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
3648 test_bit(STATUS_RF_KILL_HW, &priv->status));
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003649
Emmanuel Grumbach58d0f362008-06-12 09:47:19 +08003650 iwl_power_initialize(priv);
Wey-Yi Guy39b73fb2009-07-24 11:13:02 -07003651 iwl_tt_initialize(priv);
Johannes Berg158bea02010-01-22 14:22:53 -08003652
3653 /**************************************************
3654 * 9. Setup and register with mac80211 and debugfs
3655 **************************************************/
3656 err = iwl_mac_setup_register(priv);
3657 if (err)
3658 goto out_remove_sysfs;
3659
3660 err = iwl_dbgfs_register(priv, DRV_NAME);
3661 if (err)
3662 IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
3663
Zhu Yib481de92007-09-25 17:54:57 -07003664 return 0;
3665
Assaf Krauss316c30d2008-03-14 10:38:46 -07003666 out_remove_sysfs:
Reinette Chatrec8f16132009-02-27 16:21:22 -08003667 destroy_workqueue(priv->workqueue);
3668 priv->workqueue = NULL;
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003669 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
Helmut Schaa795cc0a2009-02-12 18:51:03 +01003670 out_free_irq:
3671 free_irq(priv->pci_dev->irq, priv);
Mohamed Abbasef850d72009-05-22 11:01:50 -07003672 iwl_free_isr_ict(priv);
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003673 out_disable_msi:
3674 pci_disable_msi(priv->pci_dev);
Tomas Winkler6ba87952008-05-15 13:54:17 +08003675 iwl_uninit_drv(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07003676 out_free_eeprom:
3677 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003678 out_iounmap:
3679 pci_iounmap(pdev, priv->hw_base);
3680 out_pci_release_regions:
Assaf Krauss316c30d2008-03-14 10:38:46 -07003681 pci_set_drvdata(pdev, NULL);
Reinette Chatre623d5632009-03-03 11:37:04 -08003682 pci_release_regions(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07003683 out_pci_disable_device:
3684 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07003685 out_ieee80211_free_hw:
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003686 iwl_free_traffic_mem(priv);
Wey-Yi Guyd7c76f42009-10-09 13:20:17 -07003687 ieee80211_free_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07003688 out:
3689 return err;
3690}
3691
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003692static void __devexit iwl_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07003693{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003694 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003695 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07003696
3697 if (!priv)
3698 return;
3699
Tomas Winklere1623442009-01-27 14:27:56 -08003700 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07003701
Emmanuel Grumbach67249622008-05-29 16:35:26 +08003702 iwl_dbgfs_unregister(priv);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003703 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
Emmanuel Grumbach67249622008-05-29 16:35:26 +08003704
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003705 /* ieee80211_unregister_hw call wil cause iwl_mac_stop to
3706 * to be called and iwl_down since we are removing the device
Gregory Greenman0b124c32008-09-03 11:18:50 +08003707 * we need to set STATUS_EXIT_PENDING bit.
3708 */
3709 set_bit(STATUS_EXIT_PENDING, &priv->status);
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07003710 if (priv->mac80211_registered) {
3711 ieee80211_unregister_hw(priv->hw);
3712 priv->mac80211_registered = 0;
Gregory Greenman0b124c32008-09-03 11:18:50 +08003713 } else {
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003714 iwl_down(priv);
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07003715 }
3716
Ben Cahillc166b252009-10-23 13:42:35 -07003717 /*
3718 * Make sure device is reset to low power before unloading driver.
3719 * This may be redundant with iwl_down(), but there are paths to
3720 * run iwl_down() without calling apm_ops.stop(), and there are
3721 * paths to avoid running iwl_down() at all before leaving driver.
3722 * This (inexpensive) call *makes sure* device is reset.
3723 */
3724 priv->cfg->ops->lib->apm_ops.stop(priv);
3725
Wey-Yi Guy39b73fb2009-07-24 11:13:02 -07003726 iwl_tt_exit(priv);
3727
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003728 /* make sure we flush any pending irq or
3729 * tasklet for the driver
3730 */
3731 spin_lock_irqsave(&priv->lock, flags);
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003732 iwl_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003733 spin_unlock_irqrestore(&priv->lock, flags);
3734
3735 iwl_synchronize_irq(priv);
3736
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003737 iwl_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003738
3739 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08003740 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08003741 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003742
Tomas Winklerc587de02009-06-03 11:44:07 -07003743 iwl_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07003744 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003745
Zhu Yib481de92007-09-25 17:54:57 -07003746
Mohamed Abbas948c1712007-10-25 17:15:45 +08003747 /*netif_stop_queue(dev); */
3748 flush_workqueue(priv->workqueue);
3749
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003750 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07003751 * priv->workqueue... so we can't take down the workqueue
3752 * until now... */
3753 destroy_workqueue(priv->workqueue);
3754 priv->workqueue = NULL;
Wey-Yi Guy20594eb2009-08-07 15:41:39 -07003755 iwl_free_traffic_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003756
Helmut Schaa6cd0b1c2009-01-19 13:10:07 +01003757 free_irq(priv->pci_dev->irq, priv);
3758 pci_disable_msi(priv->pci_dev);
Zhu Yib481de92007-09-25 17:54:57 -07003759 pci_iounmap(pdev, priv->hw_base);
3760 pci_release_regions(pdev);
3761 pci_disable_device(pdev);
3762 pci_set_drvdata(pdev, NULL);
3763
Tomas Winkler6ba87952008-05-15 13:54:17 +08003764 iwl_uninit_drv(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003765
Mohamed Abbasef850d72009-05-22 11:01:50 -07003766 iwl_free_isr_ict(priv);
3767
Zhu Yib481de92007-09-25 17:54:57 -07003768 if (priv->ibss_beacon)
3769 dev_kfree_skb(priv->ibss_beacon);
3770
3771 ieee80211_free_hw(priv->hw);
3772}
3773
Zhu Yib481de92007-09-25 17:54:57 -07003774
3775/*****************************************************************************
3776 *
3777 * driver and module entry point
3778 *
3779 *****************************************************************************/
3780
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003781/* Hardware specific file defines the PCI IDs table for that hardware module */
3782static struct pci_device_id iwl_hw_card_ids[] = {
Tomas Winkler4fc22b22008-07-21 18:54:42 +03003783#ifdef CONFIG_IWL4965
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003784 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
3785 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler4fc22b22008-07-21 18:54:42 +03003786#endif /* CONFIG_IWL4965 */
Tomas Winkler5a6a2562008-04-24 11:55:23 -07003787#ifdef CONFIG_IWL5000
Wey-Yi Guyac592572009-11-20 12:05:03 -08003788/* 5100 Series WiFi */
3789 {IWL_PCI_DEVICE(0x4232, 0x1201, iwl5100_agn_cfg)}, /* Mini Card */
3790 {IWL_PCI_DEVICE(0x4232, 0x1301, iwl5100_agn_cfg)}, /* Half Mini Card */
3791 {IWL_PCI_DEVICE(0x4232, 0x1204, iwl5100_agn_cfg)}, /* Mini Card */
3792 {IWL_PCI_DEVICE(0x4232, 0x1304, iwl5100_agn_cfg)}, /* Half Mini Card */
3793 {IWL_PCI_DEVICE(0x4232, 0x1205, iwl5100_bgn_cfg)}, /* Mini Card */
3794 {IWL_PCI_DEVICE(0x4232, 0x1305, iwl5100_bgn_cfg)}, /* Half Mini Card */
3795 {IWL_PCI_DEVICE(0x4232, 0x1206, iwl5100_abg_cfg)}, /* Mini Card */
3796 {IWL_PCI_DEVICE(0x4232, 0x1306, iwl5100_abg_cfg)}, /* Half Mini Card */
3797 {IWL_PCI_DEVICE(0x4232, 0x1221, iwl5100_agn_cfg)}, /* Mini Card */
3798 {IWL_PCI_DEVICE(0x4232, 0x1321, iwl5100_agn_cfg)}, /* Half Mini Card */
3799 {IWL_PCI_DEVICE(0x4232, 0x1224, iwl5100_agn_cfg)}, /* Mini Card */
3800 {IWL_PCI_DEVICE(0x4232, 0x1324, iwl5100_agn_cfg)}, /* Half Mini Card */
3801 {IWL_PCI_DEVICE(0x4232, 0x1225, iwl5100_bgn_cfg)}, /* Mini Card */
3802 {IWL_PCI_DEVICE(0x4232, 0x1325, iwl5100_bgn_cfg)}, /* Half Mini Card */
3803 {IWL_PCI_DEVICE(0x4232, 0x1226, iwl5100_abg_cfg)}, /* Mini Card */
3804 {IWL_PCI_DEVICE(0x4232, 0x1326, iwl5100_abg_cfg)}, /* Half Mini Card */
3805 {IWL_PCI_DEVICE(0x4237, 0x1211, iwl5100_agn_cfg)}, /* Mini Card */
3806 {IWL_PCI_DEVICE(0x4237, 0x1311, iwl5100_agn_cfg)}, /* Half Mini Card */
3807 {IWL_PCI_DEVICE(0x4237, 0x1214, iwl5100_agn_cfg)}, /* Mini Card */
3808 {IWL_PCI_DEVICE(0x4237, 0x1314, iwl5100_agn_cfg)}, /* Half Mini Card */
3809 {IWL_PCI_DEVICE(0x4237, 0x1215, iwl5100_bgn_cfg)}, /* Mini Card */
3810 {IWL_PCI_DEVICE(0x4237, 0x1315, iwl5100_bgn_cfg)}, /* Half Mini Card */
3811 {IWL_PCI_DEVICE(0x4237, 0x1216, iwl5100_abg_cfg)}, /* Mini Card */
3812 {IWL_PCI_DEVICE(0x4237, 0x1316, iwl5100_abg_cfg)}, /* Half Mini Card */
3813
3814/* 5300 Series WiFi */
3815 {IWL_PCI_DEVICE(0x4235, 0x1021, iwl5300_agn_cfg)}, /* Mini Card */
3816 {IWL_PCI_DEVICE(0x4235, 0x1121, iwl5300_agn_cfg)}, /* Half Mini Card */
3817 {IWL_PCI_DEVICE(0x4235, 0x1024, iwl5300_agn_cfg)}, /* Mini Card */
3818 {IWL_PCI_DEVICE(0x4235, 0x1124, iwl5300_agn_cfg)}, /* Half Mini Card */
3819 {IWL_PCI_DEVICE(0x4235, 0x1001, iwl5300_agn_cfg)}, /* Mini Card */
3820 {IWL_PCI_DEVICE(0x4235, 0x1101, iwl5300_agn_cfg)}, /* Half Mini Card */
3821 {IWL_PCI_DEVICE(0x4235, 0x1004, iwl5300_agn_cfg)}, /* Mini Card */
3822 {IWL_PCI_DEVICE(0x4235, 0x1104, iwl5300_agn_cfg)}, /* Half Mini Card */
3823 {IWL_PCI_DEVICE(0x4236, 0x1011, iwl5300_agn_cfg)}, /* Mini Card */
3824 {IWL_PCI_DEVICE(0x4236, 0x1111, iwl5300_agn_cfg)}, /* Half Mini Card */
3825 {IWL_PCI_DEVICE(0x4236, 0x1014, iwl5300_agn_cfg)}, /* Mini Card */
3826 {IWL_PCI_DEVICE(0x4236, 0x1114, iwl5300_agn_cfg)}, /* Half Mini Card */
3827
3828/* 5350 Series WiFi/WiMax */
3829 {IWL_PCI_DEVICE(0x423A, 0x1001, iwl5350_agn_cfg)}, /* Mini Card */
3830 {IWL_PCI_DEVICE(0x423A, 0x1021, iwl5350_agn_cfg)}, /* Mini Card */
3831 {IWL_PCI_DEVICE(0x423B, 0x1011, iwl5350_agn_cfg)}, /* Mini Card */
3832
3833/* 5150 Series Wifi/WiMax */
3834 {IWL_PCI_DEVICE(0x423C, 0x1201, iwl5150_agn_cfg)}, /* Mini Card */
3835 {IWL_PCI_DEVICE(0x423C, 0x1301, iwl5150_agn_cfg)}, /* Half Mini Card */
3836 {IWL_PCI_DEVICE(0x423C, 0x1206, iwl5150_abg_cfg)}, /* Mini Card */
3837 {IWL_PCI_DEVICE(0x423C, 0x1306, iwl5150_abg_cfg)}, /* Half Mini Card */
3838 {IWL_PCI_DEVICE(0x423C, 0x1221, iwl5150_agn_cfg)}, /* Mini Card */
3839 {IWL_PCI_DEVICE(0x423C, 0x1321, iwl5150_agn_cfg)}, /* Half Mini Card */
3840
3841 {IWL_PCI_DEVICE(0x423D, 0x1211, iwl5150_agn_cfg)}, /* Mini Card */
3842 {IWL_PCI_DEVICE(0x423D, 0x1311, iwl5150_agn_cfg)}, /* Half Mini Card */
3843 {IWL_PCI_DEVICE(0x423D, 0x1216, iwl5150_abg_cfg)}, /* Mini Card */
3844 {IWL_PCI_DEVICE(0x423D, 0x1316, iwl5150_abg_cfg)}, /* Half Mini Card */
Wey-Yi Guy5953a622009-09-17 10:43:53 -07003845
3846/* 6x00 Series */
Wey-Yi Guy5953a622009-09-17 10:43:53 -07003847 {IWL_PCI_DEVICE(0x422B, 0x1101, iwl6000_3agn_cfg)},
3848 {IWL_PCI_DEVICE(0x422B, 0x1121, iwl6000_3agn_cfg)},
3849 {IWL_PCI_DEVICE(0x422C, 0x1301, iwl6000i_2agn_cfg)},
3850 {IWL_PCI_DEVICE(0x422C, 0x1306, iwl6000i_2abg_cfg)},
3851 {IWL_PCI_DEVICE(0x422C, 0x1307, iwl6000i_2bg_cfg)},
3852 {IWL_PCI_DEVICE(0x422C, 0x1321, iwl6000i_2agn_cfg)},
3853 {IWL_PCI_DEVICE(0x422C, 0x1326, iwl6000i_2abg_cfg)},
3854 {IWL_PCI_DEVICE(0x4238, 0x1111, iwl6000_3agn_cfg)},
3855 {IWL_PCI_DEVICE(0x4239, 0x1311, iwl6000i_2agn_cfg)},
3856 {IWL_PCI_DEVICE(0x4239, 0x1316, iwl6000i_2abg_cfg)},
3857
3858/* 6x50 WiFi/WiMax Series */
Wey-Yi Guy5953a622009-09-17 10:43:53 -07003859 {IWL_PCI_DEVICE(0x0087, 0x1301, iwl6050_2agn_cfg)},
3860 {IWL_PCI_DEVICE(0x0087, 0x1306, iwl6050_2abg_cfg)},
3861 {IWL_PCI_DEVICE(0x0087, 0x1321, iwl6050_2agn_cfg)},
3862 {IWL_PCI_DEVICE(0x0087, 0x1326, iwl6050_2abg_cfg)},
Wey-Yi Guy5953a622009-09-17 10:43:53 -07003863 {IWL_PCI_DEVICE(0x0089, 0x1311, iwl6050_2agn_cfg)},
3864 {IWL_PCI_DEVICE(0x0089, 0x1316, iwl6050_2abg_cfg)},
3865
Jay Sternberg77dcb6a2009-03-06 13:52:55 -08003866/* 1000 Series WiFi */
Wey-Yi Guy4bd09142009-09-17 10:43:52 -07003867 {IWL_PCI_DEVICE(0x0083, 0x1205, iwl1000_bgn_cfg)},
3868 {IWL_PCI_DEVICE(0x0083, 0x1305, iwl1000_bgn_cfg)},
3869 {IWL_PCI_DEVICE(0x0083, 0x1225, iwl1000_bgn_cfg)},
3870 {IWL_PCI_DEVICE(0x0083, 0x1325, iwl1000_bgn_cfg)},
3871 {IWL_PCI_DEVICE(0x0084, 0x1215, iwl1000_bgn_cfg)},
3872 {IWL_PCI_DEVICE(0x0084, 0x1315, iwl1000_bgn_cfg)},
3873 {IWL_PCI_DEVICE(0x0083, 0x1206, iwl1000_bg_cfg)},
3874 {IWL_PCI_DEVICE(0x0083, 0x1306, iwl1000_bg_cfg)},
3875 {IWL_PCI_DEVICE(0x0083, 0x1226, iwl1000_bg_cfg)},
3876 {IWL_PCI_DEVICE(0x0083, 0x1326, iwl1000_bg_cfg)},
3877 {IWL_PCI_DEVICE(0x0084, 0x1216, iwl1000_bg_cfg)},
3878 {IWL_PCI_DEVICE(0x0084, 0x1316, iwl1000_bg_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07003879#endif /* CONFIG_IWL5000 */
Tomas Winkler7100e922008-12-01 16:32:18 -08003880
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003881 {0}
3882};
3883MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
3884
3885static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07003886 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003887 .id_table = iwl_hw_card_ids,
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003888 .probe = iwl_pci_probe,
3889 .remove = __devexit_p(iwl_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07003890#ifdef CONFIG_PM
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003891 .suspend = iwl_pci_suspend,
3892 .resume = iwl_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07003893#endif
3894};
3895
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003896static int __init iwl_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07003897{
3898
3899 int ret;
3900 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
3901 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003902
Tomas Winklere227cea2008-07-18 13:53:05 +08003903 ret = iwlagn_rate_control_register();
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003904 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003905 printk(KERN_ERR DRV_NAME
3906 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003907 return ret;
3908 }
3909
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003910 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07003911 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08003912 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003913 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07003914 }
Zhu Yib481de92007-09-25 17:54:57 -07003915
3916 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003917
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003918error_register:
Tomas Winklere227cea2008-07-18 13:53:05 +08003919 iwlagn_rate_control_unregister();
Reinette Chatre897e1cf2008-03-28 16:21:09 -07003920 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003921}
3922
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003923static void __exit iwl_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07003924{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07003925 pci_unregister_driver(&iwl_driver);
Tomas Winklere227cea2008-07-18 13:53:05 +08003926 iwlagn_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07003927}
3928
Emmanuel Grumbach5b9f8cd2008-10-29 14:05:46 -07003929module_exit(iwl_exit);
3930module_init(iwl_init);
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003931
3932#ifdef CONFIG_IWLWIFI_DEBUG
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07003933module_param_named(debug50, iwl_debug_level, uint, S_IRUGO);
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003934MODULE_PARM_DESC(debug50, "50XX debug output mask (deprecated)");
Wey-Yi Guy4e30cb62009-09-17 10:43:47 -07003935module_param_named(debug, iwl_debug_level, uint, S_IRUGO | S_IWUSR);
Reinette Chatrea562a9d2009-07-17 09:30:24 -07003936MODULE_PARM_DESC(debug, "debug output mask");
3937#endif
3938