blob: bb820f35d6029275ae9b25eece4613d89ec75885 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 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:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * 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>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#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
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070049#include "iwl-dev.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070050#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070051#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070053#include "iwl-sta.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070054#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070055
Tomas Winklerbabcebf2008-05-15 13:54:00 +080056static int iwl_txq_update_write_ptr(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +080057 struct iwl_tx_queue *txq);
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.
67 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
68 */
69
70#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
71
Tomas Winkler0a6857e2008-03-12 16:58:49 -070072#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070073#define VD "d"
74#else
75#define VD
76#endif
77
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080078#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070079#define VS "s"
80#else
81#define VS
82#endif
83
Tomas Winklerdf48c322008-03-06 10:40:19 -080084#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070085
Zhu Yib481de92007-09-25 17:54:57 -070086
87MODULE_DESCRIPTION(DRV_DESCRIPTION);
88MODULE_VERSION(DRV_VERSION);
89MODULE_AUTHOR(DRV_COPYRIGHT);
90MODULE_LICENSE("GPL");
91
92__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
93{
94 u16 fc = le16_to_cpu(hdr->frame_control);
95 int hdr_len = ieee80211_get_hdrlen(fc);
96
97 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
98 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
99 return NULL;
100}
101
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700102static const struct ieee80211_supported_band *iwl_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700103 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700104{
Johannes Berg8318d782008-01-24 19:38:38 +0100105 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700106}
107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800108static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700109{
110 /* Single white space is for Linksys APs */
111 if (essid_len == 1 && essid[0] == ' ')
112 return 1;
113
114 /* Otherwise, if the entire essid is 0, we assume it is hidden */
115 while (essid_len) {
116 essid_len--;
117 if (essid[essid_len] != '\0')
118 return 0;
119 }
120
121 return 1;
122}
123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800124static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700125{
126 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
127 const char *s = essid;
128 char *d = escaped;
129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800130 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700131 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
132 return escaped;
133 }
134
135 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
136 while (essid_len--) {
137 if (*s == '\0') {
138 *d++ = '\\';
139 *d++ = '0';
140 s++;
141 } else
142 *d++ = *s++;
143 }
144 *d = '\0';
145 return escaped;
146}
147
Ester Kummerbf403db2008-05-05 10:22:40 +0800148
Zhu Yib481de92007-09-25 17:54:57 -0700149/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
150 * DMA services
151 *
152 * Theory of operation
153 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800154 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
155 * of buffer descriptors, each of which points to one or more data buffers for
156 * the device to read from or fill. Driver and device exchange status of each
157 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
158 * entries in each circular buffer, to protect against confusing empty and full
159 * queue states.
160 *
161 * The device reads or writes the data in the queues via the device's several
162 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700163 *
164 * For Tx queue, there are low mark and high mark limits. If, after queuing
165 * the packet for Tx, free space become < low mark, Tx queue stopped. When
166 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
167 * Tx queue resumed.
168 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800169 * The 4965 operates with up to 17 queues: One receive queue, one transmit
170 * queue (#4) for sending commands to the device firmware, and 15 other
171 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800172 *
173 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700174 ***************************************************/
175
Tomas Winkler443cfd42008-05-15 13:53:57 +0800176int iwl_queue_space(const struct iwl_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700177{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800178 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700179
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800180 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700181 s -= q->n_bd;
182
183 if (s <= 0)
184 s += q->n_window;
185 /* keep some reserve to not confuse empty and full situations */
186 s -= 2;
187 if (s < 0)
188 s = 0;
189 return s;
190}
191
Zhu Yib481de92007-09-25 17:54:57 -0700192
Tomas Winkler443cfd42008-05-15 13:53:57 +0800193static inline int iwl_queue_used(const struct iwl_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700194{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800195 return q->write_ptr > q->read_ptr ?
196 (i >= q->read_ptr && i < q->write_ptr) :
197 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700198}
199
Tomas Winkler443cfd42008-05-15 13:53:57 +0800200static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700201{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800202 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700203 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800204 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700205
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800206 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700207 return index & (q->n_window - 1);
208}
209
Zhu Yib481de92007-09-25 17:54:57 -0700210
211/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800212 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700213 * the functionality provided here
214 */
215
216/**************************************************************/
217
Ian Schram01ebd062007-10-25 17:15:22 +0800218#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800219/**
220 * iwl4965_remove_station - Remove driver's knowledge of station.
221 *
222 * NOTE: This does not remove station from device's station table.
223 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700224static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700225{
226 int index = IWL_INVALID_STATION;
227 int i;
228 unsigned long flags;
229
230 spin_lock_irqsave(&priv->sta_lock, flags);
231
232 if (is_ap)
233 index = IWL_AP_ID;
234 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700235 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700236 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700237 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Zhu Yib481de92007-09-25 17:54:57 -0700238 if (priv->stations[i].used &&
239 !compare_ether_addr(priv->stations[i].sta.sta.addr,
240 addr)) {
241 index = i;
242 break;
243 }
244
245 if (unlikely(index == IWL_INVALID_STATION))
246 goto out;
247
248 if (priv->stations[index].used) {
249 priv->stations[index].used = 0;
250 priv->num_stations--;
251 }
252
253 BUG_ON(priv->num_stations < 0);
254
255out:
256 spin_unlock_irqrestore(&priv->sta_lock, flags);
257 return 0;
258}
Zhu Yi556f8db2007-09-27 11:27:33 +0800259#endif
Zhu Yib481de92007-09-25 17:54:57 -0700260
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800261/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800262 * iwl4965_add_station_flags - Add station to tables in driver and device
263 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700264u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200265 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700266{
267 int i;
268 int index = IWL_INVALID_STATION;
Tomas Winkler6def9762008-05-05 10:22:31 +0800269 struct iwl_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700270 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700271 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700272
273 spin_lock_irqsave(&priv->sta_lock, flags_spin);
274 if (is_ap)
275 index = IWL_AP_ID;
276 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700277 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700278 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700279 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Zhu Yib481de92007-09-25 17:54:57 -0700280 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
281 addr)) {
282 index = i;
283 break;
284 }
285
286 if (!priv->stations[i].used &&
287 index == IWL_INVALID_STATION)
288 index = i;
289 }
290
291
Ben Cahill9fbab512007-11-29 11:09:47 +0800292 /* These two conditions have the same outcome, but keep them separate
293 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700294 if (unlikely(index == IWL_INVALID_STATION)) {
295 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
296 return index;
297 }
298
299 if (priv->stations[index].used &&
300 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
301 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
302 return index;
303 }
304
305
Joe Perches0795af52007-10-03 17:59:30 -0700306 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700307 station = &priv->stations[index];
308 station->used = 1;
309 priv->num_stations++;
310
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800311 /* Set up the REPLY_ADD_STA command to send to device */
Tomas Winkler133636d2008-05-05 10:22:34 +0800312 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700313 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
314 station->sta.mode = 0;
315 station->sta.sta.sta_id = index;
316 station->sta.station_flags = 0;
317
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800318#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700319 /* BCAST station and IBSS stations do not work in HT mode */
Tomas Winkler5425e492008-04-15 16:01:38 -0700320 if (index != priv->hw_params.bcast_sta_id &&
Zhu Yib481de92007-09-25 17:54:57 -0700321 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200322 iwl4965_set_ht_add_station(priv, index,
323 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800324#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700325
326 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800327
328 /* Add station to device's station table */
Tomas Winkler133636d2008-05-05 10:22:34 +0800329 iwl_send_add_sta(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700330 return index;
331
332}
333
Zhu Yib481de92007-09-25 17:54:57 -0700334
Zhu Yib481de92007-09-25 17:54:57 -0700335
336/*************** HOST COMMAND QUEUE FUNCTIONS *****/
337
Zhu Yib481de92007-09-25 17:54:57 -0700338/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800339 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700340 * @priv: device private data point
341 * @cmd: a point to the ucode command structure
342 *
343 * The function returns < 0 values to indicate the operation is
344 * failed. On success, it turns the index (> 0) of command in the
345 * command queue.
346 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700347int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700348{
Ron Rindjunsky16466902008-05-05 10:22:50 +0800349 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Tomas Winkler443cfd42008-05-15 13:53:57 +0800350 struct iwl_queue *q = &txq->q;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800351 struct iwl_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700352 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700353 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700354 u32 idx;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800355 u16 fix_size;
Zhu Yib481de92007-09-25 17:54:57 -0700356 dma_addr_t phys_addr;
357 int ret;
358 unsigned long flags;
359
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800360 cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
361 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
362
Zhu Yib481de92007-09-25 17:54:57 -0700363 /* If any of the command structures end up being larger than
364 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
365 * we will need to increase the size of the TFD entries */
366 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
367 !(cmd->meta.flags & CMD_SIZE_HUGE));
368
Tomas Winklerfee12472008-04-03 16:05:21 -0700369 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800370 IWL_DEBUG_INFO("Not sending command - RF KILL");
371 return -EIO;
372 }
373
Tomas Winkler443cfd42008-05-15 13:53:57 +0800374 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700375 IWL_ERROR("No space for Tx\n");
376 return -ENOSPC;
377 }
378
379 spin_lock_irqsave(&priv->hcmd_lock, flags);
380
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800381 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700382 memset(tfd, 0, sizeof(*tfd));
383
384 control_flags = (u32 *) tfd;
385
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800386 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700387 out_cmd = &txq->cmd[idx];
388
389 out_cmd->hdr.cmd = cmd->id;
390 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
391 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
392
393 /* At this point, the out_cmd now has all of the incoming cmd
394 * information */
395
396 out_cmd->hdr.flags = 0;
397 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800398 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700399 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
400 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
401
402 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700403 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800404 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700405
406 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
407 "%d bytes at %d[%d]:%d\n",
408 get_cmd_string(out_cmd->hdr.cmd),
409 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800410 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700411
412 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800413
414 /* Set up entry in queue's byte count circular buffer */
Tomas Winklere2a722e2008-04-14 21:16:10 -0700415 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800416
417 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800418 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklerbabcebf2008-05-15 13:54:00 +0800419 ret = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700420
421 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
422 return ret ? ret : idx;
423}
424
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700425static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
426{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800427 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700428
429 if (hw_decrypt)
430 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
431 else
432 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
433
434}
435
Zhu Yib481de92007-09-25 17:54:57 -0700436/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800437 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700438 *
439 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800440 * NOTE: mutex must be held before calling this fnction
441 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700442static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700443 const u8 *addr, int is_ap)
444{
Zhu Yi556f8db2007-09-27 11:27:33 +0800445 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700446
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800447 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200448#ifdef CONFIG_IWL4965_HT
449 struct ieee80211_conf *conf = &priv->hw->conf;
450 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
451
452 if ((is_ap) &&
453 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
454 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
455 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
456 0, cur_ht_config);
457 else
458#endif /* CONFIG_IWL4965_HT */
459 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
460 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800461
462 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700463 iwl4965_add_station(priv, addr, is_ap);
464
Zhu Yi556f8db2007-09-27 11:27:33 +0800465 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700466}
467
468/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800469 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700470 *
471 * NOTE: This is really only useful during development and can eventually
472 * be #ifdef'd out once the driver is stable and folks aren't actively
473 * making changes
474 */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800475static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700476{
477 int error = 0;
478 int counter = 1;
479
480 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
481 error |= le32_to_cpu(rxon->flags &
482 (RXON_FLG_TGJ_NARROW_BAND_MSK |
483 RXON_FLG_RADAR_DETECT_MSK));
484 if (error)
485 IWL_WARNING("check 24G fields %d | %d\n",
486 counter++, error);
487 } else {
488 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
489 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
490 if (error)
491 IWL_WARNING("check 52 fields %d | %d\n",
492 counter++, error);
493 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
494 if (error)
495 IWL_WARNING("check 52 CCK %d | %d\n",
496 counter++, error);
497 }
498 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
499 if (error)
500 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
501
502 /* make sure basic rates 6Mbps and 1Mbps are supported */
503 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
504 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
505 if (error)
506 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
507
508 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
509 if (error)
510 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
511
512 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
513 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
514 if (error)
515 IWL_WARNING("check CCK and short slot %d | %d\n",
516 counter++, error);
517
518 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
519 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
520 if (error)
521 IWL_WARNING("check CCK & auto detect %d | %d\n",
522 counter++, error);
523
524 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
525 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
526 if (error)
527 IWL_WARNING("check TGG and auto detect %d | %d\n",
528 counter++, error);
529
530 if (error)
531 IWL_WARNING("Tuning to channel %d\n",
532 le16_to_cpu(rxon->channel));
533
534 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800535 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700536 return -1;
537 }
538 return 0;
539}
540
541/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800542 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800543 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700544 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800545 * If the RXON structure is changing enough to require a new tune,
546 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
547 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700548 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700549static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700550{
551
552 /* These items are only settable from the full RXON command */
553 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
554 compare_ether_addr(priv->staging_rxon.bssid_addr,
555 priv->active_rxon.bssid_addr) ||
556 compare_ether_addr(priv->staging_rxon.node_addr,
557 priv->active_rxon.node_addr) ||
558 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
559 priv->active_rxon.wlap_bssid_addr) ||
560 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
561 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
562 (priv->staging_rxon.air_propagation !=
563 priv->active_rxon.air_propagation) ||
564 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
565 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
566 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
567 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
568 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
569 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
570 return 1;
571
572 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
573 * be updated with the RXON_ASSOC command -- however only some
574 * flag transitions are allowed using RXON_ASSOC */
575
576 /* Check if we are not switching bands */
577 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
578 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
579 return 1;
580
581 /* Check if we are switching association toggle */
582 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
583 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
584 return 1;
585
586 return 0;
587}
588
Zhu Yib481de92007-09-25 17:54:57 -0700589/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800590 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700591 *
Ian Schram01ebd062007-10-25 17:15:22 +0800592 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700593 * the active_rxon structure is updated with the new data. This
594 * function correctly transitions out of the RXON_ASSOC_MSK state if
595 * a HW tune is required based on the RXON structure changes.
596 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700597static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700598{
599 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800600 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700601 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700602 int rc = 0;
603
Tomas Winklerfee12472008-04-03 16:05:21 -0700604 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700605 return -1;
606
607 /* always get timestamp with Rx frame */
608 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
609
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800610 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700611 if (rc) {
612 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
613 return -EINVAL;
614 }
615
616 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800617 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700618 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800619 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700620 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700621 if (rc) {
622 IWL_ERROR("Error setting RXON_ASSOC "
623 "configuration (%d).\n", rc);
624 return rc;
625 }
626
627 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
628
629 return 0;
630 }
631
632 /* station table will be cleared */
633 priv->assoc_station_added = 0;
634
Zhu Yib481de92007-09-25 17:54:57 -0700635 /* If we are currently associated and the new config requires
636 * an RXON_ASSOC and the new config wants the associated mask enabled,
637 * we must clear the associated from the active configuration
638 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700639 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700640 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
641 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
642 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
643
Tomas Winkler857485c2008-03-21 13:53:44 -0700644 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800645 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700646 &priv->active_rxon);
647
648 /* If the mask clearing failed then we set
649 * active_rxon back to what it was previously */
650 if (rc) {
651 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
652 IWL_ERROR("Error clearing ASSOC_MSK on current "
653 "configuration (%d).\n", rc);
654 return rc;
655 }
Zhu Yib481de92007-09-25 17:54:57 -0700656 }
657
658 IWL_DEBUG_INFO("Sending RXON\n"
659 "* with%s RXON_FILTER_ASSOC_MSK\n"
660 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700661 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700662 ((priv->staging_rxon.filter_flags &
663 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
664 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700665 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700666
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700667 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700668 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700669 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800670 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700671 if (rc) {
672 IWL_ERROR("Error setting new configuration (%d).\n", rc);
673 return rc;
674 }
675
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700676 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800677
Zhu Yib481de92007-09-25 17:54:57 -0700678 if (!priv->error_recovering)
679 priv->start_calib = 0;
680
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700681 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700682
683 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
684
685 /* If we issue a new RXON command which required a tune then we must
686 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800687 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700688 if (rc) {
689 IWL_ERROR("Error setting Tx power (%d).\n", rc);
690 return rc;
691 }
692
693 /* Add the broadcast address so we can send broadcast frames */
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800694 if (iwl4965_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700695 IWL_INVALID_STATION) {
696 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
697 return -EIO;
698 }
699
700 /* If we have set the ASSOC_MSK and we are in BSS mode then
701 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700702 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700703 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800704 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700705 == IWL_INVALID_STATION) {
706 IWL_ERROR("Error adding AP address for transmit.\n");
707 return -EIO;
708 }
709 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700710 if (priv->default_wep_key &&
711 iwl_send_static_wepkey_cmd(priv, 0))
712 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700713 }
714
715 return 0;
716}
717
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700718void iwl4965_update_chain_flags(struct iwl_priv *priv)
719{
720
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700721 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700722 iwl4965_commit_rxon(priv);
723}
724
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700725static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700726{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800727 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700728 .flags = 3,
729 .lead_time = 0xAA,
730 .max_kill = 1,
731 .kill_ack_mask = 0,
732 .kill_cts_mask = 0,
733 };
734
Tomas Winkler857485c2008-03-21 13:53:44 -0700735 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800736 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700737}
738
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700739static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700740{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800741 int ret = 0;
742 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700743 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700744 .id = REPLY_SCAN_ABORT_CMD,
745 .meta.flags = CMD_WANT_SKB,
746 };
747
748 /* If there isn't a scan actively going on in the hardware
749 * then we are in between scan bands and not actually
750 * actively scanning, so don't send the abort command */
751 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
752 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
753 return 0;
754 }
755
Tomas Winklerdb11d632008-05-05 10:22:33 +0800756 ret = iwl_send_cmd_sync(priv, &cmd);
757 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700758 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800759 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700760 }
761
Tomas Winklerdb11d632008-05-05 10:22:33 +0800762 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700763 if (res->u.status != CAN_ABORT_STATUS) {
764 /* The scan abort will return 1 for success or
765 * 2 for "failure". A failure condition can be
766 * due to simply not being in an active scan which
767 * can occur if we send the scan abort before we
768 * the microcode has notified us that a scan is
769 * completed. */
770 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
771 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
772 clear_bit(STATUS_SCAN_HW, &priv->status);
773 }
774
775 dev_kfree_skb_any(cmd.meta.u.skb);
776
Tomas Winklerdb11d632008-05-05 10:22:33 +0800777 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700778}
779
Zhu Yib481de92007-09-25 17:54:57 -0700780/*
781 * CARD_STATE_CMD
782 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800783 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700784 *
785 * When in the 'enable' state the card operates as normal.
786 * When in the 'disable' state, the card enters into a low power mode.
787 * When in the 'halt' state, the card is shut down and must be fully
788 * restarted to come back on.
789 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700790static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700791{
Tomas Winkler857485c2008-03-21 13:53:44 -0700792 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700793 .id = REPLY_CARD_STATE_CMD,
794 .len = sizeof(u32),
795 .data = &flags,
796 .meta.flags = meta_flag,
797 };
798
Tomas Winkler857485c2008-03-21 13:53:44 -0700799 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700800}
801
Tomas Winklerfcab4232008-05-15 13:54:01 +0800802static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700803{
804 struct list_head *element;
805
806 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
807 priv->frames_count);
808
809 while (!list_empty(&priv->free_frames)) {
810 element = priv->free_frames.next;
811 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800812 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700813 priv->frames_count--;
814 }
815
816 if (priv->frames_count) {
817 IWL_WARNING("%d frames still in use. Did we lose one?\n",
818 priv->frames_count);
819 priv->frames_count = 0;
820 }
821}
822
Tomas Winklerfcab4232008-05-15 13:54:01 +0800823static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700824{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800825 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700826 struct list_head *element;
827 if (list_empty(&priv->free_frames)) {
828 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
829 if (!frame) {
830 IWL_ERROR("Could not allocate frame!\n");
831 return NULL;
832 }
833
834 priv->frames_count++;
835 return frame;
836 }
837
838 element = priv->free_frames.next;
839 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800840 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700841}
842
Tomas Winklerfcab4232008-05-15 13:54:01 +0800843static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700844{
845 memset(frame, 0, sizeof(*frame));
846 list_add(&frame->list, &priv->free_frames);
847}
848
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700849unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700850 struct ieee80211_hdr *hdr,
851 const u8 *dest, int left)
852{
853
Tomas Winkler3109ece2008-03-28 16:33:35 -0700854 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700855 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
856 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
857 return 0;
858
859 if (priv->ibss_beacon->len > left)
860 return 0;
861
862 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
863
864 return priv->ibss_beacon->len;
865}
866
Guy Cohen39e88502008-04-23 17:14:57 -0700867static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700868{
Guy Cohen39e88502008-04-23 17:14:57 -0700869 int i;
870 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700871
Guy Cohen39e88502008-04-23 17:14:57 -0700872 /* Set rate mask*/
873 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
874 rate_mask = priv->active_rate_basic & 0xF;
875 else
876 rate_mask = priv->active_rate_basic & 0xFF0;
877
878 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700879 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800880 i = iwl_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700881 if (rate_mask & (1 << i))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800882 return iwl_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700883 }
884
Guy Cohen39e88502008-04-23 17:14:57 -0700885 /* No valid rate was found. Assign the lowest one */
886 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
887 return IWL_RATE_1M_PLCP;
888 else
889 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700890}
891
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700892static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700893{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800894 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700895 unsigned int frame_size;
896 int rc;
897 u8 rate;
898
Tomas Winklerfcab4232008-05-15 13:54:01 +0800899 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700900
901 if (!frame) {
902 IWL_ERROR("Could not obtain free frame buffer for beacon "
903 "command.\n");
904 return -ENOMEM;
905 }
906
Guy Cohen39e88502008-04-23 17:14:57 -0700907 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700908
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800909 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700910
Tomas Winkler857485c2008-03-21 13:53:44 -0700911 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700912 &frame->u.cmd[0]);
913
Tomas Winklerfcab4232008-05-15 13:54:01 +0800914 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700915
916 return rc;
917}
918
919/******************************************************************************
920 *
Zhu Yib481de92007-09-25 17:54:57 -0700921 * Misc. internal state and helper functions
922 *
923 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700924
Zhu Yib481de92007-09-25 17:54:57 -0700925/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800926 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -0700927 *
928 * return : set the bit for each supported rate insert in ie
929 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800930static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +0200931 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -0700932{
933 u16 ret_rates = 0, bit;
934 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200935 u8 *cnt = ie;
936 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700937
938 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
939 if (bit & supported_rate) {
940 ret_rates |= bit;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800941 rates[*cnt] = iwl_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +0200942 ((bit & basic_rate) ? 0x80 : 0x00);
943 (*cnt)++;
944 (*left)--;
945 if ((*left <= 0) ||
946 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -0700947 break;
948 }
949 }
950
951 return ret_rates;
952}
953
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700954#ifdef CONFIG_IWL4965_HT
955static void iwl4965_ht_conf(struct iwl_priv *priv,
956 struct ieee80211_bss_conf *bss_conf)
957{
958 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
959 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
960 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
961
962 IWL_DEBUG_MAC80211("enter: \n");
963
964 iwl_conf->is_ht = bss_conf->assoc_ht;
965
966 if (!iwl_conf->is_ht)
967 return;
968
969 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
970
971 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
972 iwl_conf->sgf |= 0x1;
973 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
974 iwl_conf->sgf |= 0x2;
975
976 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
977 iwl_conf->max_amsdu_size =
978 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
979
980 iwl_conf->supported_chan_width =
981 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
982 iwl_conf->extension_chan_offset =
983 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
984 /* If no above or below channel supplied disable FAT channel */
985 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
986 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
987 iwl_conf->supported_chan_width = 0;
988
989 iwl_conf->tx_mimo_ps_mode =
990 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
991 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
992
993 iwl_conf->control_channel = ht_bss_conf->primary_channel;
994 iwl_conf->tx_chan_width =
995 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
996 iwl_conf->ht_protection =
997 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
998 iwl_conf->non_GF_STA_present =
999 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
1000
1001 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
1002 IWL_DEBUG_MAC80211("leave\n");
1003}
1004
1005static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1006 u8 *pos, int *left)
1007{
1008 struct ieee80211_ht_cap *ht_cap;
1009
1010 if (!sband || !sband->ht_info.ht_supported)
1011 return;
1012
1013 if (*left < sizeof(struct ieee80211_ht_cap))
1014 return;
1015
1016 *pos++ = sizeof(struct ieee80211_ht_cap);
1017 ht_cap = (struct ieee80211_ht_cap *) pos;
1018
1019 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1020 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1021 ht_cap->ampdu_params_info =
1022 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
1023 ((sband->ht_info.ampdu_density << 2) &
1024 IEEE80211_HT_CAP_AMPDU_DENSITY);
1025 *left -= sizeof(struct ieee80211_ht_cap);
1026}
1027#else
1028static inline void iwl4965_ht_conf(struct iwl_priv *priv,
1029 struct ieee80211_bss_conf *bss_conf)
1030{
1031}
1032static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1033 u8 *pos, int *left)
1034{
1035}
1036#endif
1037
1038
Zhu Yib481de92007-09-25 17:54:57 -07001039/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001040 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001041 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001042static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001043 enum ieee80211_band band,
1044 struct ieee80211_mgmt *frame,
1045 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001046{
1047 int len = 0;
1048 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001049 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +02001050 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001051 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07001052
1053 /* Make sure there is enough space for the probe request,
1054 * two mandatory IEs and the data */
1055 left -= 24;
1056 if (left < 0)
1057 return 0;
1058 len += 24;
1059
1060 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Tomas Winkler57bd1be2008-05-15 13:54:03 +08001061 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001062 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Tomas Winkler57bd1be2008-05-15 13:54:03 +08001063 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001064 frame->seq_ctrl = 0;
1065
1066 /* fill in our indirect SSID IE */
1067 /* ...next IE... */
1068
1069 left -= 2;
1070 if (left < 0)
1071 return 0;
1072 len += 2;
1073 pos = &(frame->u.probe_req.variable[0]);
1074 *pos++ = WLAN_EID_SSID;
1075 *pos++ = 0;
1076
1077 /* fill in our direct SSID IE... */
1078 if (is_direct) {
1079 /* ...next IE... */
1080 left -= 2 + priv->essid_len;
1081 if (left < 0)
1082 return 0;
1083 /* ... fill it in... */
1084 *pos++ = WLAN_EID_SSID;
1085 *pos++ = priv->essid_len;
1086 memcpy(pos, priv->essid, priv->essid_len);
1087 pos += priv->essid_len;
1088 len += 2 + priv->essid_len;
1089 }
1090
1091 /* fill in supported rate */
1092 /* ...next IE... */
1093 left -= 2;
1094 if (left < 0)
1095 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001096
Zhu Yib481de92007-09-25 17:54:57 -07001097 /* ... fill it in... */
1098 *pos++ = WLAN_EID_SUPP_RATES;
1099 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001100
mabbasbee488d2007-10-25 17:15:42 +08001101 /* exclude 60M rate */
1102 active_rates = priv->rates_mask;
1103 active_rates &= ~IWL_RATE_60M_MASK;
1104
1105 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001106
Tomas Winklerc7c46672007-10-18 02:04:15 +02001107 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001108 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001109 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001110 active_rates &= ~ret_rates;
1111
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001112 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001113 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001114 active_rates &= ~ret_rates;
1115
Zhu Yib481de92007-09-25 17:54:57 -07001116 len += 2 + *pos;
1117 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001118 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001119 goto fill_end;
1120
1121 /* fill in supported extended rate */
1122 /* ...next IE... */
1123 left -= 2;
1124 if (left < 0)
1125 return 0;
1126 /* ... fill it in... */
1127 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1128 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001129 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001130 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001131 if (*pos > 0)
1132 len += 2 + *pos;
1133
Zhu Yib481de92007-09-25 17:54:57 -07001134 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001135 /* fill in HT IE */
1136 left -= 2;
1137 if (left < 0)
1138 return 0;
1139
1140 *pos++ = WLAN_EID_HT_CAPABILITY;
1141 *pos = 0;
1142
1143 iwl_ht_cap_to_ie(sband, pos, &left);
1144
1145 if (*pos > 0)
1146 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -07001147 return (u16)len;
1148}
1149
1150/*
1151 * QoS support
1152*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001153static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001154 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001155{
1156
Tomas Winkler857485c2008-03-21 13:53:44 -07001157 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001158 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001159}
1160
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001161static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001162{
1163 unsigned long flags;
1164
Zhu Yib481de92007-09-25 17:54:57 -07001165 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1166 return;
1167
1168 if (!priv->qos_data.qos_enable)
1169 return;
1170
1171 spin_lock_irqsave(&priv->lock, flags);
1172 priv->qos_data.def_qos_parm.qos_flags = 0;
1173
1174 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1175 !priv->qos_data.qos_cap.q_AP.txop_request)
1176 priv->qos_data.def_qos_parm.qos_flags |=
1177 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001178 if (priv->qos_data.qos_active)
1179 priv->qos_data.def_qos_parm.qos_flags |=
1180 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1181
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001182#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001183 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001184 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001185#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001186
Zhu Yib481de92007-09-25 17:54:57 -07001187 spin_unlock_irqrestore(&priv->lock, flags);
1188
Tomas Winkler3109ece2008-03-28 16:33:35 -07001189 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001190 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1191 priv->qos_data.qos_active,
1192 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001193
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001194 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001195 &(priv->qos_data.def_qos_parm));
1196 }
1197}
1198
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001199int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001200{
1201 /* Filter incoming packets to determine if they are targeted toward
1202 * this network, discarding packets coming from ourselves */
1203 switch (priv->iw_mode) {
1204 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1205 /* packets from our adapter are dropped (echo) */
1206 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1207 return 0;
1208 /* {broad,multi}cast packets to our IBSS go through */
1209 if (is_multicast_ether_addr(header->addr1))
1210 return !compare_ether_addr(header->addr3, priv->bssid);
1211 /* packets to our adapter go through */
1212 return !compare_ether_addr(header->addr1, priv->mac_addr);
1213 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1214 /* packets from our adapter are dropped (echo) */
1215 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1216 return 0;
1217 /* {broad,multi}cast packets to our BSS go through */
1218 if (is_multicast_ether_addr(header->addr1))
1219 return !compare_ether_addr(header->addr2, priv->bssid);
1220 /* packets to our adapter go through */
1221 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001222 default:
1223 break;
Zhu Yib481de92007-09-25 17:54:57 -07001224 }
1225
1226 return 1;
1227}
1228
1229#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001231static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001232{
1233 switch (status & TX_STATUS_MSK) {
1234 case TX_STATUS_SUCCESS:
1235 return "SUCCESS";
1236 TX_STATUS_ENTRY(SHORT_LIMIT);
1237 TX_STATUS_ENTRY(LONG_LIMIT);
1238 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1239 TX_STATUS_ENTRY(MGMNT_ABORT);
1240 TX_STATUS_ENTRY(NEXT_FRAG);
1241 TX_STATUS_ENTRY(LIFE_EXPIRE);
1242 TX_STATUS_ENTRY(DEST_PS);
1243 TX_STATUS_ENTRY(ABORTED);
1244 TX_STATUS_ENTRY(BT_RETRY);
1245 TX_STATUS_ENTRY(STA_INVALID);
1246 TX_STATUS_ENTRY(FRAG_DROPPED);
1247 TX_STATUS_ENTRY(TID_DISABLE);
1248 TX_STATUS_ENTRY(FRAME_FLUSHED);
1249 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1250 TX_STATUS_ENTRY(TX_LOCKED);
1251 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1252 }
1253
1254 return "UNKNOWN";
1255}
1256
1257/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001258 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001259 *
1260 * NOTE: priv->mutex is not required before calling this function
1261 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001262static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001263{
1264 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1265 clear_bit(STATUS_SCANNING, &priv->status);
1266 return 0;
1267 }
1268
1269 if (test_bit(STATUS_SCANNING, &priv->status)) {
1270 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1271 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1272 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1273 queue_work(priv->workqueue, &priv->abort_scan);
1274
1275 } else
1276 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1277
1278 return test_bit(STATUS_SCANNING, &priv->status);
1279 }
1280
1281 return 0;
1282}
1283
1284/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001285 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001286 * @ms: amount of time to wait (in milliseconds) for scan to abort
1287 *
1288 * NOTE: priv->mutex must be held before calling this function
1289 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001290static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001291{
1292 unsigned long now = jiffies;
1293 int ret;
1294
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001295 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001296 if (ret && ms) {
1297 mutex_unlock(&priv->mutex);
1298 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1299 test_bit(STATUS_SCANNING, &priv->status))
1300 msleep(1);
1301 mutex_lock(&priv->mutex);
1302
1303 return test_bit(STATUS_SCANNING, &priv->status);
1304 }
1305
1306 return ret;
1307}
1308
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001309static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001310{
1311 /* Reset ieee stats */
1312
1313 /* We don't reset the net_device_stats (ieee->stats) on
1314 * re-association */
1315
1316 priv->last_seq_num = -1;
1317 priv->last_frag_num = -1;
1318 priv->last_packet_time = 0;
1319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001320 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001321}
1322
1323#define MAX_UCODE_BEACON_INTERVAL 4096
1324#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001326static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001327{
1328 u16 new_val = 0;
1329 u16 beacon_factor = 0;
1330
1331 beacon_factor =
1332 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1333 / MAX_UCODE_BEACON_INTERVAL;
1334 new_val = beacon_val / beacon_factor;
1335
1336 return cpu_to_le16(new_val);
1337}
1338
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001339static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001340{
1341 u64 interval_tm_unit;
1342 u64 tsf, result;
1343 unsigned long flags;
1344 struct ieee80211_conf *conf = NULL;
1345 u16 beacon_int = 0;
1346
1347 conf = ieee80211_get_hw_conf(priv->hw);
1348
1349 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001350 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1351 priv->rxon_timing.timestamp.dw[0] =
1352 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001353
1354 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1355
Tomas Winkler3109ece2008-03-28 16:33:35 -07001356 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001357
1358 beacon_int = priv->beacon_int;
1359 spin_unlock_irqrestore(&priv->lock, flags);
1360
1361 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1362 if (beacon_int == 0) {
1363 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1364 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1365 } else {
1366 priv->rxon_timing.beacon_interval =
1367 cpu_to_le16(beacon_int);
1368 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001369 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001370 le16_to_cpu(priv->rxon_timing.beacon_interval));
1371 }
1372
1373 priv->rxon_timing.atim_window = 0;
1374 } else {
1375 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001376 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001377 /* TODO: we need to get atim_window from upper stack
1378 * for now we set to 0 */
1379 priv->rxon_timing.atim_window = 0;
1380 }
1381
1382 interval_tm_unit =
1383 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1384 result = do_div(tsf, interval_tm_unit);
1385 priv->rxon_timing.beacon_init_val =
1386 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1387
1388 IWL_DEBUG_ASSOC
1389 ("beacon interval %d beacon timer %d beacon tim %d\n",
1390 le16_to_cpu(priv->rxon_timing.beacon_interval),
1391 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1392 le16_to_cpu(priv->rxon_timing.atim_window));
1393}
1394
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001395static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001396{
1397 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1398 IWL_ERROR("APs don't scan.\n");
1399 return 0;
1400 }
1401
Tomas Winklerfee12472008-04-03 16:05:21 -07001402 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001403 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1404 return -EIO;
1405 }
1406
1407 if (test_bit(STATUS_SCANNING, &priv->status)) {
1408 IWL_DEBUG_SCAN("Scan already in progress.\n");
1409 return -EAGAIN;
1410 }
1411
1412 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1413 IWL_DEBUG_SCAN("Scan request while abort pending. "
1414 "Queuing.\n");
1415 return -EAGAIN;
1416 }
1417
1418 IWL_DEBUG_INFO("Starting scan...\n");
1419 priv->scan_bands = 2;
1420 set_bit(STATUS_SCANNING, &priv->status);
1421 priv->scan_start = jiffies;
1422 priv->scan_pass_start = priv->scan_start;
1423
1424 queue_work(priv->workqueue, &priv->request_scan);
1425
1426 return 0;
1427}
1428
Zhu Yib481de92007-09-25 17:54:57 -07001429
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001430static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001431 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001432{
Johannes Berg8318d782008-01-24 19:38:38 +01001433 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001434 priv->staging_rxon.flags &=
1435 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1436 | RXON_FLG_CCK_MSK);
1437 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1438 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001439 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001440 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1441 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1442 else
1443 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1444
1445 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1446 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1447
1448 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1449 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1450 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1451 }
1452}
1453
1454/*
Ian Schram01ebd062007-10-25 17:15:22 +08001455 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001456 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001457static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001458{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001459 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001460
1461 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1462
1463 switch (priv->iw_mode) {
1464 case IEEE80211_IF_TYPE_AP:
1465 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1466 break;
1467
1468 case IEEE80211_IF_TYPE_STA:
1469 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1470 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1471 break;
1472
1473 case IEEE80211_IF_TYPE_IBSS:
1474 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1475 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1476 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1477 RXON_FILTER_ACCEPT_GRP_MSK;
1478 break;
1479
1480 case IEEE80211_IF_TYPE_MNTR:
1481 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1482 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1483 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1484 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001485 default:
1486 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1487 break;
Zhu Yib481de92007-09-25 17:54:57 -07001488 }
1489
1490#if 0
1491 /* TODO: Figure out when short_preamble would be set and cache from
1492 * that */
1493 if (!hw_to_local(priv->hw)->short_preamble)
1494 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1495 else
1496 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1497#endif
1498
Assaf Krauss8622e702008-03-21 13:53:43 -07001499 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001500 le16_to_cpu(priv->staging_rxon.channel));
1501
1502 if (!ch_info)
1503 ch_info = &priv->channel_info[0];
1504
1505 /*
1506 * in some case A channels are all non IBSS
1507 * in this case force B/G channel
1508 */
1509 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1510 !(is_channel_ibss(ch_info)))
1511 ch_info = &priv->channel_info[0];
1512
1513 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001514 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001515
Johannes Berg8318d782008-01-24 19:38:38 +01001516 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001517
1518 priv->staging_rxon.ofdm_basic_rates =
1519 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1520 priv->staging_rxon.cck_basic_rates =
1521 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1522
1523 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1524 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1525 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1526 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1527 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1528 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001529 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001530}
1531
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001532static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001533{
Zhu Yib481de92007-09-25 17:54:57 -07001534 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001535 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001536
Assaf Krauss8622e702008-03-21 13:53:43 -07001537 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001538 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001539 le16_to_cpu(priv->staging_rxon.channel));
1540
1541 if (!ch_info || !is_channel_ibss(ch_info)) {
1542 IWL_ERROR("channel %d not IBSS channel\n",
1543 le16_to_cpu(priv->staging_rxon.channel));
1544 return -EINVAL;
1545 }
1546 }
1547
Zhu Yib481de92007-09-25 17:54:57 -07001548 priv->iw_mode = mode;
1549
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001550 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001551 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1552
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001553 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001554
Mohamed Abbasfde35712007-11-29 11:10:15 +08001555 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001556 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001557 return -EAGAIN;
1558
1559 cancel_delayed_work(&priv->scan_check);
1560 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1561 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1562 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1563 return -EAGAIN;
1564 }
1565
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001566 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001567
1568 return 0;
1569}
1570
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001571static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001572 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001573 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001574 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001575 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001576{
Tomas Winkler6def9762008-05-05 10:22:31 +08001577 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001578 struct iwl_wep_key *wepkey;
1579 int keyidx = 0;
1580
Ivo van Doorn1c014422008-04-17 19:41:02 +02001581 BUG_ON(ctl->hw_key->hw_key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001582
1583 switch (keyinfo->alg) {
1584 case ALG_CCMP:
1585 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1586 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001587 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1588 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001589 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1590 break;
1591
1592 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001593 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001594 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1595 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1596 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001597 break;
1598
1599 case ALG_WEP:
Ivo van Doorn1c014422008-04-17 19:41:02 +02001600 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001601 cmd->cmd.tx.sec_ctl = 0;
1602 if (priv->default_wep_key) {
1603 /* the WEP key was sent as static */
Ivo van Doorn1c014422008-04-17 19:41:02 +02001604 keyidx = ctl->hw_key->hw_key_idx;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001605 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1606 wepkey->key_size);
1607 if (wepkey->key_size == WEP_KEY_LEN_128)
1608 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1609 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001610 /* the WEP key was sent as dynamic */
1611 keyidx = keyinfo->keyidx;
1612 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1613 keyinfo->keylen);
1614 if (keyinfo->keylen == WEP_KEY_LEN_128)
1615 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001616 }
Zhu Yib481de92007-09-25 17:54:57 -07001617
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001618 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1619 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001620
1621 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001622 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001623 break;
1624
Zhu Yib481de92007-09-25 17:54:57 -07001625 default:
1626 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1627 break;
1628 }
1629}
1630
1631/*
1632 * handle build REPLY_TX command notification.
1633 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001634static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001635 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001636 struct ieee80211_tx_control *ctrl,
1637 struct ieee80211_hdr *hdr,
1638 int is_unicast, u8 std_id)
1639{
1640 __le16 *qc;
1641 u16 fc = le16_to_cpu(hdr->frame_control);
1642 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1643
1644 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1645 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1646 tx_flags |= TX_CMD_FLG_ACK_MSK;
1647 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1648 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1649 if (ieee80211_is_probe_response(fc) &&
1650 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1651 tx_flags |= TX_CMD_FLG_TSF_MSK;
1652 } else {
1653 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1654 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1655 }
1656
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001657 if (ieee80211_is_back_request(fc))
1658 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1659
1660
Zhu Yib481de92007-09-25 17:54:57 -07001661 cmd->cmd.tx.sta_id = std_id;
1662 if (ieee80211_get_morefrag(hdr))
1663 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1664
1665 qc = ieee80211_get_qos_ctrl(hdr);
1666 if (qc) {
1667 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
1668 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1669 } else
1670 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1671
1672 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1673 tx_flags |= TX_CMD_FLG_RTS_MSK;
1674 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1675 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1676 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1677 tx_flags |= TX_CMD_FLG_CTS_MSK;
1678 }
1679
1680 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1681 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1682
1683 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1684 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1685 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1686 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08001687 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001688 else
Ian Schrambc434dd2007-10-25 17:15:29 +08001689 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001690 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001691 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001692 }
Zhu Yib481de92007-09-25 17:54:57 -07001693
1694 cmd->cmd.tx.driver_txop = 0;
1695 cmd->cmd.tx.tx_flags = tx_flags;
1696 cmd->cmd.tx.next_frame_len = 0;
1697}
Tomas Winkler19758be2008-03-12 16:58:51 -07001698static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1699{
1700 /* 0 - mgmt, 1 - cnt, 2 - data */
1701 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1702 priv->tx_stats[idx].cnt++;
1703 priv->tx_stats[idx].bytes += len;
1704}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001705/**
1706 * iwl4965_get_sta_id - Find station's index within station table
1707 *
1708 * If new IBSS station, create new entry in station table
1709 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001710static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08001711 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001712{
1713 int sta_id;
1714 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07001715 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001716
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001717 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001718 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1719 is_multicast_ether_addr(hdr->addr1))
Tomas Winkler5425e492008-04-15 16:01:38 -07001720 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001721
1722 switch (priv->iw_mode) {
1723
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001724 /* If we are a client station in a BSS network, use the special
1725 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07001726 case IEEE80211_IF_TYPE_STA:
1727 return IWL_AP_ID;
1728
1729 /* If we are an AP, then find the station, or use BCAST */
1730 case IEEE80211_IF_TYPE_AP:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001731 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001732 if (sta_id != IWL_INVALID_STATION)
1733 return sta_id;
Tomas Winkler5425e492008-04-15 16:01:38 -07001734 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001735
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001736 /* If this frame is going out to an IBSS network, find the station,
1737 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07001738 case IEEE80211_IF_TYPE_IBSS:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001739 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001740 if (sta_id != IWL_INVALID_STATION)
1741 return sta_id;
1742
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001743 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02001744 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
1745 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07001746
1747 if (sta_id != IWL_INVALID_STATION)
1748 return sta_id;
1749
Joe Perches0795af52007-10-03 17:59:30 -07001750 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001751 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07001752 print_mac(mac, hdr->addr1));
Ester Kummerbf403db2008-05-05 10:22:40 +08001753 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler5425e492008-04-15 16:01:38 -07001754 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001755
1756 default:
Ian Schram01ebd062007-10-25 17:15:22 +08001757 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Tomas Winkler5425e492008-04-15 16:01:38 -07001758 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001759 }
1760}
1761
1762/*
1763 * start REPLY_TX command process
1764 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001765static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001766 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1767{
1768 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001769 struct iwl_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07001770 u32 *control_flags;
1771 int txq_id = ctl->queue;
Ron Rindjunsky16466902008-05-05 10:22:50 +08001772 struct iwl_tx_queue *txq = NULL;
Tomas Winkler443cfd42008-05-15 13:53:57 +08001773 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001774 dma_addr_t phys_addr;
1775 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001776 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07001777 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001778 u16 len, idx, len_org;
1779 u8 id, hdr_len, unicast;
1780 u8 sta_id;
1781 u16 seq_number = 0;
1782 u16 fc;
1783 __le16 *qc;
1784 u8 wait_write_ptr = 0;
1785 unsigned long flags;
1786 int rc;
1787
1788 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07001789 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001790 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1791 goto drop_unlock;
1792 }
1793
Johannes Berg32bfd352007-12-19 01:31:26 +01001794 if (!priv->vif) {
1795 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07001796 goto drop_unlock;
1797 }
1798
Johannes Berg8318d782008-01-24 19:38:38 +01001799 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07001800 IWL_ERROR("ERROR: No TX rate available.\n");
1801 goto drop_unlock;
1802 }
1803
1804 unicast = !is_multicast_ether_addr(hdr->addr1);
1805 id = 0;
1806
1807 fc = le16_to_cpu(hdr->frame_control);
1808
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001809#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001810 if (ieee80211_is_auth(fc))
1811 IWL_DEBUG_TX("Sending AUTH frame\n");
1812 else if (ieee80211_is_assoc_request(fc))
1813 IWL_DEBUG_TX("Sending ASSOC frame\n");
1814 else if (ieee80211_is_reassoc_request(fc))
1815 IWL_DEBUG_TX("Sending REASSOC frame\n");
1816#endif
1817
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001818 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08001819 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07001820 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08001821 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08001822 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07001823 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07001824 goto drop_unlock;
1825 }
1826
1827 spin_unlock_irqrestore(&priv->lock, flags);
1828
1829 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001830
1831 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001832 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001833 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07001834 DECLARE_MAC_BUF(mac);
1835
1836 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
1837 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07001838 goto drop;
1839 }
1840
Guy Cohen07bc28e2008-04-23 17:15:03 -07001841 IWL_DEBUG_TX("station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001842
1843 qc = ieee80211_get_qos_ctrl(hdr);
1844 if (qc) {
1845 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1846 seq_number = priv->stations[sta_id].tid[tid].seq_number &
1847 IEEE80211_SCTL_SEQ;
1848 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1849 (hdr->seq_ctrl &
1850 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
1851 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001852#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001853 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001854 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07001855 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001856 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001857#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001858 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001859
1860 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07001861 txq = &priv->txq[txq_id];
1862 q = &txq->q;
1863
1864 spin_lock_irqsave(&priv->lock, flags);
1865
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001866 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001867 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07001868 memset(tfd, 0, sizeof(*tfd));
1869 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001870 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001871
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001872 /* Set up driver data for this TFD */
Tomas Winkler8567c632008-05-15 13:53:58 +08001873 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001874 txq->txb[q->write_ptr].skb[0] = skb;
1875 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07001876 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001877
1878 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07001879 out_cmd = &txq->cmd[idx];
1880 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1881 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001882
1883 /*
1884 * Set up the Tx-command (not MAC!) header.
1885 * Store the chosen Tx queue and TFD index within the sequence field;
1886 * after Tx, uCode's Tx response will return this value so driver can
1887 * locate the frame within the tx queue and do post-tx processing.
1888 */
Zhu Yib481de92007-09-25 17:54:57 -07001889 out_cmd->hdr.cmd = REPLY_TX;
1890 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001891 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001892
1893 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001894 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
1895
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001896 /*
1897 * Use the first empty entry in this queue's command buffer array
1898 * to contain the Tx command and MAC header concatenated together
1899 * (payload data will be in another buffer).
1900 * Size of this varies, due to varying MAC header length.
1901 * If end is not dword aligned, we'll have 2 extra bytes at the end
1902 * of the MAC header (device reads on dword boundaries).
1903 * We'll tell device about this padding later.
1904 */
Tomas Winkler5425e492008-04-15 16:01:38 -07001905 len = priv->hw_params.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07001906 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07001907
1908 len_org = len;
1909 len = (len + 3) & ~3;
1910
1911 if (len_org != len)
1912 len_org = 1;
1913 else
1914 len_org = 0;
1915
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001916 /* Physical address of this Tx command's header (not MAC header!),
1917 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07001918 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
1919 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001920
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001921 /* Add buffer containing Tx command and MAC(!) header to TFD's
1922 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001923 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07001924
1925 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001926 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001927
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001928 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1929 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07001930 len = skb->len - hdr_len;
1931 if (len) {
1932 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
1933 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001934 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07001935 }
1936
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001937 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07001938 if (len_org)
1939 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1940
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001941 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07001942 len = (u16)skb->len;
1943 out_cmd->cmd.tx.len = cpu_to_le16(len);
1944
1945 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001946 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001947
1948 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001949 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001950
Tomas Winkler19758be2008-03-12 16:58:51 -07001951 iwl_update_tx_stats(priv, fc, len);
1952
Tomas Winkler857485c2008-03-21 13:53:44 -07001953 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001954 offsetof(struct iwl4965_tx_cmd, scratch);
1955 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
1956 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
1957
Zhu Yib481de92007-09-25 17:54:57 -07001958 if (!ieee80211_get_morefrag(hdr)) {
1959 txq->need_update = 1;
1960 if (qc) {
1961 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1962 priv->stations[sta_id].tid[tid].seq_number = seq_number;
1963 }
1964 } else {
1965 wait_write_ptr = 1;
1966 txq->need_update = 0;
1967 }
1968
Ester Kummerbf403db2008-05-05 10:22:40 +08001969 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07001970 sizeof(out_cmd->cmd.tx));
1971
Ester Kummerbf403db2008-05-05 10:22:40 +08001972 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07001973 ieee80211_get_hdrlen(fc));
1974
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001975 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07001976 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07001977
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001978 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08001979 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08001980 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001981 spin_unlock_irqrestore(&priv->lock, flags);
1982
1983 if (rc)
1984 return rc;
1985
Tomas Winkler443cfd42008-05-15 13:53:57 +08001986 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07001987 && priv->mac80211_registered) {
1988 if (wait_write_ptr) {
1989 spin_lock_irqsave(&priv->lock, flags);
1990 txq->need_update = 1;
Tomas Winklerbabcebf2008-05-15 13:54:00 +08001991 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001992 spin_unlock_irqrestore(&priv->lock, flags);
1993 }
1994
1995 ieee80211_stop_queue(priv->hw, ctl->queue);
1996 }
1997
1998 return 0;
1999
2000drop_unlock:
2001 spin_unlock_irqrestore(&priv->lock, flags);
2002drop:
2003 return -1;
2004}
2005
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002006static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002007{
Johannes Berg8318d782008-01-24 19:38:38 +01002008 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002009 struct ieee80211_rate *rate;
2010 int i;
2011
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07002012 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002013 if (!hw) {
2014 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2015 return;
2016 }
Zhu Yib481de92007-09-25 17:54:57 -07002017
2018 priv->active_rate = 0;
2019 priv->active_rate_basic = 0;
2020
Johannes Berg8318d782008-01-24 19:38:38 +01002021 for (i = 0; i < hw->n_bitrates; i++) {
2022 rate = &(hw->bitrates[i]);
2023 if (rate->hw_value < IWL_RATE_COUNT)
2024 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002025 }
2026
2027 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2028 priv->active_rate, priv->active_rate_basic);
2029
2030 /*
2031 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2032 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2033 * OFDM
2034 */
2035 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2036 priv->staging_rxon.cck_basic_rates =
2037 ((priv->active_rate_basic &
2038 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2039 else
2040 priv->staging_rxon.cck_basic_rates =
2041 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2042
2043 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2044 priv->staging_rxon.ofdm_basic_rates =
2045 ((priv->active_rate_basic &
2046 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2047 IWL_FIRST_OFDM_RATE) & 0xFF;
2048 else
2049 priv->staging_rxon.ofdm_basic_rates =
2050 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2051}
2052
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002053void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002054{
2055 unsigned long flags;
2056
2057 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2058 return;
2059
2060 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2061 disable_radio ? "OFF" : "ON");
2062
2063 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002064 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002065 /* FIXME: This is a workaround for AP */
2066 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2067 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002068 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002069 CSR_UCODE_SW_BIT_RFKILL);
2070 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002071 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07002072 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2073 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002074 iwl4965_send_card_state(priv,
2075 CARD_STATE_CMD_DISABLE,
2076 0);
Zhu Yib481de92007-09-25 17:54:57 -07002077 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002078
2079 /* make sure mac80211 stop sending Tx frame */
2080 if (priv->mac80211_registered)
2081 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002082 }
2083 return;
2084 }
2085
2086 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002087 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002088
2089 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2090 spin_unlock_irqrestore(&priv->lock, flags);
2091
2092 /* wake up ucode */
2093 msleep(10);
2094
2095 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002096 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2097 if (!iwl_grab_nic_access(priv))
2098 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002099 spin_unlock_irqrestore(&priv->lock, flags);
2100
2101 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2102 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2103 "disabled by HW switch\n");
2104 return;
2105 }
2106
2107 queue_work(priv->workqueue, &priv->restart);
2108 return;
2109}
2110
Zhu Yib481de92007-09-25 17:54:57 -07002111#define IWL_PACKET_RETRY_TIME HZ
2112
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002113int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002114{
2115 u16 sc = le16_to_cpu(header->seq_ctrl);
2116 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2117 u16 frag = sc & IEEE80211_SCTL_FRAG;
2118 u16 *last_seq, *last_frag;
2119 unsigned long *last_time;
2120
2121 switch (priv->iw_mode) {
2122 case IEEE80211_IF_TYPE_IBSS:{
2123 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002124 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002125 u8 *mac = header->addr2;
2126 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2127
2128 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002129 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002130 if (!compare_ether_addr(entry->mac, mac))
2131 break;
2132 }
2133 if (p == &priv->ibss_mac_hash[index]) {
2134 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2135 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002136 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002137 return 0;
2138 }
2139 memcpy(entry->mac, mac, ETH_ALEN);
2140 entry->seq_num = seq;
2141 entry->frag_num = frag;
2142 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002143 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002144 return 0;
2145 }
2146 last_seq = &entry->seq_num;
2147 last_frag = &entry->frag_num;
2148 last_time = &entry->packet_time;
2149 break;
2150 }
2151 case IEEE80211_IF_TYPE_STA:
2152 last_seq = &priv->last_seq_num;
2153 last_frag = &priv->last_frag_num;
2154 last_time = &priv->last_packet_time;
2155 break;
2156 default:
2157 return 0;
2158 }
2159 if ((*last_seq == seq) &&
2160 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2161 if (*last_frag == frag)
2162 goto drop;
2163 if (*last_frag + 1 != frag)
2164 /* out-of-order fragment */
2165 goto drop;
2166 } else
2167 *last_seq = seq;
2168
2169 *last_frag = frag;
2170 *last_time = jiffies;
2171 return 0;
2172
2173 drop:
2174 return 1;
2175}
2176
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002177#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002178
2179#include "iwl-spectrum.h"
2180
2181#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2182#define BEACON_TIME_MASK_HIGH 0xFF000000
2183#define TIME_UNIT 1024
2184
2185/*
2186 * extended beacon time format
2187 * time in usec will be changed into a 32-bit value in 8:24 format
2188 * the high 1 byte is the beacon counts
2189 * the lower 3 bytes is the time in usec within one beacon interval
2190 */
2191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002192static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002193{
2194 u32 quot;
2195 u32 rem;
2196 u32 interval = beacon_interval * 1024;
2197
2198 if (!interval || !usec)
2199 return 0;
2200
2201 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2202 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2203
2204 return (quot << 24) + rem;
2205}
2206
2207/* base is usually what we get from ucode with each received frame,
2208 * the same as HW timer counter counting down
2209 */
2210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002211static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002212{
2213 u32 base_low = base & BEACON_TIME_MASK_LOW;
2214 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2215 u32 interval = beacon_interval * TIME_UNIT;
2216 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2217 (addon & BEACON_TIME_MASK_HIGH);
2218
2219 if (base_low > addon_low)
2220 res += base_low - addon_low;
2221 else if (base_low < addon_low) {
2222 res += interval + base_low - addon_low;
2223 res += (1 << 24);
2224 } else
2225 res += (1 << 24);
2226
2227 return cpu_to_le32(res);
2228}
2229
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002230static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002231 struct ieee80211_measurement_params *params,
2232 u8 type)
2233{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002234 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08002235 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002236 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002237 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2238 .data = (void *)&spectrum,
2239 .meta.flags = CMD_WANT_SKB,
2240 };
2241 u32 add_time = le64_to_cpu(params->start_time);
2242 int rc;
2243 int spectrum_resp_status;
2244 int duration = le16_to_cpu(params->duration);
2245
Tomas Winkler3109ece2008-03-28 16:33:35 -07002246 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002247 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002248 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002249 le64_to_cpu(params->start_time) - priv->last_tsf,
2250 le16_to_cpu(priv->rxon_timing.beacon_interval));
2251
2252 memset(&spectrum, 0, sizeof(spectrum));
2253
2254 spectrum.channel_count = cpu_to_le16(1);
2255 spectrum.flags =
2256 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2257 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2258 cmd.len = sizeof(spectrum);
2259 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2260
Tomas Winkler3109ece2008-03-28 16:33:35 -07002261 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002262 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002263 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002264 add_time,
2265 le16_to_cpu(priv->rxon_timing.beacon_interval));
2266 else
2267 spectrum.start_time = 0;
2268
2269 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2270 spectrum.channels[0].channel = params->channel;
2271 spectrum.channels[0].type = type;
2272 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2273 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2274 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2275
Tomas Winkler857485c2008-03-21 13:53:44 -07002276 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002277 if (rc)
2278 return rc;
2279
Tomas Winklerdb11d632008-05-05 10:22:33 +08002280 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002281 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2282 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2283 rc = -EIO;
2284 }
2285
2286 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2287 switch (spectrum_resp_status) {
2288 case 0: /* Command will be handled */
2289 if (res->u.spectrum.id != 0xff) {
2290 IWL_DEBUG_INFO
2291 ("Replaced existing measurement: %d\n",
2292 res->u.spectrum.id);
2293 priv->measurement_status &= ~MEASUREMENT_READY;
2294 }
2295 priv->measurement_status |= MEASUREMENT_ACTIVE;
2296 rc = 0;
2297 break;
2298
2299 case 1: /* Command will not be handled */
2300 rc = -EAGAIN;
2301 break;
2302 }
2303
2304 dev_kfree_skb_any(cmd.meta.u.skb);
2305
2306 return rc;
2307}
2308#endif
2309
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002310static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Tomas Winkler8567c632008-05-15 13:53:58 +08002311 struct iwl_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002312{
2313
2314 tx_sta->status.ack_signal = 0;
2315 tx_sta->status.excessive_retries = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002316
2317 if (in_interrupt())
2318 ieee80211_tx_status_irqsafe(priv->hw,
2319 tx_sta->skb[0], &(tx_sta->status));
2320 else
2321 ieee80211_tx_status(priv->hw,
2322 tx_sta->skb[0], &(tx_sta->status));
2323
2324 tx_sta->skb[0] = NULL;
2325}
2326
2327/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002328 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002329 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002330 * When FW advances 'R' index, all entries between old and new 'R' index
2331 * need to be reclaimed. As result, some free space forms. If there is
2332 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002333 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002334int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002335{
Ron Rindjunsky16466902008-05-05 10:22:50 +08002336 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Tomas Winkler443cfd42008-05-15 13:53:57 +08002337 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002338 int nfreed = 0;
2339
Tomas Winkler443cfd42008-05-15 13:53:57 +08002340 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07002341 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2342 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002343 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002344 return 0;
2345 }
2346
Tomas Winklerc54b6792008-03-06 17:36:53 -08002347 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002348 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002349 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002350 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002351 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002352 &(txq->txb[txq->q.read_ptr]));
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002353 iwl_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002354 } else if (nfreed > 1) {
2355 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002356 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002357 queue_work(priv->workqueue, &priv->restart);
2358 }
2359 nfreed++;
2360 }
2361
Zhu Yib481de92007-09-25 17:54:57 -07002362 return nfreed;
2363}
2364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002365static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002366{
2367 status &= TX_STATUS_MSK;
2368 return (status == TX_STATUS_SUCCESS)
2369 || (status == TX_STATUS_DIRECT_DONE);
2370}
2371
2372/******************************************************************************
2373 *
2374 * Generic RX handler implementations
2375 *
2376 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002377#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002378
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002379static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002380 struct ieee80211_hdr *hdr)
2381{
2382 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2383 return IWL_AP_ID;
2384 else {
2385 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07002386 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002387 }
2388}
2389
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002390static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002391 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002392{
2393 if (priv->txq[txq_id].txb[idx].skb[0])
2394 return (struct ieee80211_hdr *)priv->txq[txq_id].
2395 txb[idx].skb[0]->data;
2396 return NULL;
2397}
2398
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002399static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002400{
2401 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2402 tx_resp->frame_count);
2403 return le32_to_cpu(*scd_ssn) & MAX_SN;
2404
2405}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002406
2407/**
2408 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2409 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002410static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Tomas Winkler6def9762008-05-05 10:22:31 +08002411 struct iwl_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002412 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002413 u16 start_idx)
2414{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002415 u16 status;
2416 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002417 struct ieee80211_tx_status *tx_status = NULL;
2418 struct ieee80211_hdr *hdr = NULL;
2419 int i, sh;
2420 int txq_id, idx;
2421 u16 seq;
2422
2423 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002424 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002425
2426 agg->frame_count = tx_resp->frame_count;
2427 agg->start_idx = start_idx;
2428 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002429 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002430
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002431 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002432 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002433 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002434 status = le16_to_cpu(frame_status[0].status);
2435 seq = le16_to_cpu(frame_status[0].sequence);
2436 idx = SEQ_TO_INDEX(seq);
2437 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002438
Zhu Yib481de92007-09-25 17:54:57 -07002439 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002440 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2441 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002442
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002443 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002444 tx_status->retry_count = tx_resp->failure_frame;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002445 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002446 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002447 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002448 iwl4965_hwrate_to_tx_control(priv,
2449 le32_to_cpu(tx_resp->rate_n_flags),
2450 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002451 /* FIXME: code repetition end */
2452
2453 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2454 status & 0xff, tx_resp->failure_frame);
2455 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002456 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002457
2458 agg->wait_for_ba = 0;
2459 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002460 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002461 u64 bitmap = 0;
2462 int start = agg->start_idx;
2463
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002464 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002465 for (i = 0; i < agg->frame_count; i++) {
2466 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002467 status = le16_to_cpu(frame_status[i].status);
2468 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002469 idx = SEQ_TO_INDEX(seq);
2470 txq_id = SEQ_TO_QUEUE(seq);
2471
2472 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2473 AGG_TX_STATE_ABORT_MSK))
2474 continue;
2475
2476 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2477 agg->frame_count, txq_id, idx);
2478
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002479 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002480
2481 sc = le16_to_cpu(hdr->seq_ctrl);
2482 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2483 IWL_ERROR("BUG_ON idx doesn't match seq control"
2484 " idx=%d, seq_idx=%d, seq=%d\n",
2485 idx, SEQ_TO_SN(sc),
2486 hdr->seq_ctrl);
2487 return -1;
2488 }
2489
2490 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2491 i, idx, SEQ_TO_SN(sc));
2492
2493 sh = idx - start;
2494 if (sh > 64) {
2495 sh = (start - idx) + 0xff;
2496 bitmap = bitmap << sh;
2497 sh = 0;
2498 start = idx;
2499 } else if (sh < -64)
2500 sh = 0xff - (start - idx);
2501 else if (sh < 0) {
2502 sh = start - idx;
2503 start = idx;
2504 bitmap = bitmap << sh;
2505 sh = 0;
2506 }
2507 bitmap |= (1 << sh);
2508 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2509 start, (u32)(bitmap & 0xFFFFFFFF));
2510 }
2511
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002512 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002513 agg->start_idx = start;
2514 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002515 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002516 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002517 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002518
2519 if (bitmap)
2520 agg->wait_for_ba = 1;
2521 }
2522 return 0;
2523}
2524#endif
Zhu Yib481de92007-09-25 17:54:57 -07002525
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002526/**
2527 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2528 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002529static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002530 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002531{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002532 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002533 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2534 int txq_id = SEQ_TO_QUEUE(sequence);
2535 int index = SEQ_TO_INDEX(sequence);
Ron Rindjunsky16466902008-05-05 10:22:50 +08002536 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002537 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002538 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002539 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002540#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002541 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2542 struct ieee80211_hdr *hdr;
2543 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002544#endif
2545
Tomas Winkler443cfd42008-05-15 13:53:57 +08002546 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07002547 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2548 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002549 index, txq->q.n_bd, txq->q.write_ptr,
2550 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002551 return;
2552 }
2553
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002554#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002555 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2556 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002557
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002558 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002559 tid = le16_to_cpu(*qc) & 0xf;
2560
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002561 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2562 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2563 IWL_ERROR("Station not known\n");
2564 return;
2565 }
2566
2567 if (txq->sched_retry) {
2568 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Tomas Winkler6def9762008-05-05 10:22:31 +08002569 struct iwl_ht_agg *agg = NULL;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002570
2571 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002572 return;
Zhu Yib481de92007-09-25 17:54:57 -07002573
2574 agg = &priv->stations[sta_id].tid[tid].agg;
2575
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002576 iwl4965_tx_status_reply_tx(priv, agg,
2577 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002578
2579 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002580 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002581 /* TODO: send BAR */
2582 }
2583
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002584 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002585 int freed, ampdu_q;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002586 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002587 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2588 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002589 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2590 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2591
Tomas Winkler443cfd42008-05-15 13:53:57 +08002592 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002593 txq_id >= 0 && priv->mac80211_registered &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002594 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
2595 /* calculate mac80211 ampdu sw queue to wake */
2596 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
2597 priv->hw->queues;
2598 if (agg->state == IWL_AGG_OFF)
2599 ieee80211_wake_queue(priv->hw, txq_id);
2600 else
2601 ieee80211_wake_queue(priv->hw, ampdu_q);
2602 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002603 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002604 }
2605 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002606#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002607 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07002608
2609 tx_status->retry_count = tx_resp->failure_frame;
Zhu Yib481de92007-09-25 17:54:57 -07002610 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002611 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002612 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2613 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002614
Zhu Yib481de92007-09-25 17:54:57 -07002615 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002616 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07002617 status, le32_to_cpu(tx_resp->rate_n_flags),
2618 tx_resp->failure_frame);
2619
2620 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Tomas Winkler47c51962008-05-05 10:22:41 +08002621#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002622 if (index != -1) {
2623 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002624 if (tid != MAX_TID_COUNT)
2625 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
Tomas Winkler443cfd42008-05-15 13:53:57 +08002626 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002627 (txq_id >= 0) && priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002628 ieee80211_wake_queue(priv->hw, txq_id);
2629 if (tid != MAX_TID_COUNT)
2630 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002631 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002632 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002633#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002634
2635 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2636 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2637}
2638
2639
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002640static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002641 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002642{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002643 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002644 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002645 struct delayed_work *pwork;
2646
2647 palive = &pkt->u.alive_frame;
2648
2649 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2650 "0x%01X 0x%01X\n",
2651 palive->is_valid, palive->ver_type,
2652 palive->ver_subtype);
2653
2654 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2655 IWL_DEBUG_INFO("Initialization Alive received.\n");
2656 memcpy(&priv->card_alive_init,
2657 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002658 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002659 pwork = &priv->init_alive_start;
2660 } else {
2661 IWL_DEBUG_INFO("Runtime Alive received.\n");
2662 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002663 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002664 pwork = &priv->alive_start;
2665 }
2666
2667 /* We delay the ALIVE response by 5ms to
2668 * give the HW RF Kill time to activate... */
2669 if (palive->is_valid == UCODE_VALID_OK)
2670 queue_delayed_work(priv->workqueue, pwork,
2671 msecs_to_jiffies(5));
2672 else
2673 IWL_WARNING("uCode did not respond OK.\n");
2674}
2675
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002676static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002677 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002678{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002679 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002680
2681 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2682 return;
2683}
2684
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002685static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002686 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002687{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002688 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002689
2690 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2691 "seq 0x%04X ser 0x%08X\n",
2692 le32_to_cpu(pkt->u.err_resp.error_type),
2693 get_cmd_string(pkt->u.err_resp.cmd_id),
2694 pkt->u.err_resp.cmd_id,
2695 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2696 le32_to_cpu(pkt->u.err_resp.error_info));
2697}
2698
2699#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2700
Tomas Winklera55360e2008-05-05 10:22:28 +08002701static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002702{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002703 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002704 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002705 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002706 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2707 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2708 rxon->channel = csa->channel;
2709 priv->staging_rxon.channel = csa->channel;
2710}
2711
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002712static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002713 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002714{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002715#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08002716 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002717 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002718
2719 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002720 IWL_DEBUG(IWL_DL_11H,
2721 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07002722 return;
2723 }
2724
2725 memcpy(&priv->measure_report, report, sizeof(*report));
2726 priv->measurement_status |= MEASUREMENT_READY;
2727#endif
2728}
2729
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002730static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002731 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002732{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002733#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002734 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002735 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002736 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2737 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2738#endif
2739}
2740
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002741static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002742 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002743{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002744 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002745 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2746 "notification for %s:\n",
2747 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08002748 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002749}
2750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002751static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002752{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002753 struct iwl_priv *priv =
2754 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002755 struct sk_buff *beacon;
2756
2757 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002758 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002759
2760 if (!beacon) {
2761 IWL_ERROR("update beacon failed\n");
2762 return;
2763 }
2764
2765 mutex_lock(&priv->mutex);
2766 /* new beacon skb is allocated every time; dispose previous.*/
2767 if (priv->ibss_beacon)
2768 dev_kfree_skb(priv->ibss_beacon);
2769
2770 priv->ibss_beacon = beacon;
2771 mutex_unlock(&priv->mutex);
2772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002773 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002774}
2775
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002776static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002777 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002778{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002779#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002780 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002781 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2782 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002783
2784 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2785 "tsf %d %d rate %d\n",
2786 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2787 beacon->beacon_notify_hdr.failure_frame,
2788 le32_to_cpu(beacon->ibss_mgr_status),
2789 le32_to_cpu(beacon->high_tsf),
2790 le32_to_cpu(beacon->low_tsf), rate);
2791#endif
2792
2793 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2794 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2795 queue_work(priv->workqueue, &priv->beacon_update);
2796}
2797
2798/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002799static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002800 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002801{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002802#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002803 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002804 struct iwl4965_scanreq_notification *notif =
2805 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002806
2807 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2808#endif
2809}
2810
2811/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002812static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002813 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002814{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002815 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002816 struct iwl4965_scanstart_notification *notif =
2817 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002818 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2819 IWL_DEBUG_SCAN("Scan start: "
2820 "%d [802.11%s] "
2821 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2822 notif->channel,
2823 notif->band ? "bg" : "a",
2824 notif->tsf_high,
2825 notif->tsf_low, notif->status, notif->beacon_timer);
2826}
2827
2828/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002829static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002830 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002831{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002832 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002833 struct iwl4965_scanresults_notification *notif =
2834 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002835
2836 IWL_DEBUG_SCAN("Scan ch.res: "
2837 "%d [802.11%s] "
2838 "(TSF: 0x%08X:%08X) - %d "
2839 "elapsed=%lu usec (%dms since last)\n",
2840 notif->channel,
2841 notif->band ? "bg" : "a",
2842 le32_to_cpu(notif->tsf_high),
2843 le32_to_cpu(notif->tsf_low),
2844 le32_to_cpu(notif->statistics[0]),
2845 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2846 jiffies_to_msecs(elapsed_jiffies
2847 (priv->last_scan_jiffies, jiffies)));
2848
2849 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002850 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002851}
2852
2853/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002854static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002855 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002856{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002857 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002858 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002859
2860 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2861 scan_notif->scanned_channels,
2862 scan_notif->tsf_low,
2863 scan_notif->tsf_high, scan_notif->status);
2864
2865 /* The HW is no longer scanning */
2866 clear_bit(STATUS_SCAN_HW, &priv->status);
2867
2868 /* The scan completion notification came in, so kill that timer... */
2869 cancel_delayed_work(&priv->scan_check);
2870
2871 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
2872 (priv->scan_bands == 2) ? "2.4" : "5.2",
2873 jiffies_to_msecs(elapsed_jiffies
2874 (priv->scan_pass_start, jiffies)));
2875
2876 /* Remove this scanned band from the list
2877 * of pending bands to scan */
2878 priv->scan_bands--;
2879
2880 /* If a request to abort was given, or the scan did not succeed
2881 * then we reset the scan state machine and terminate,
2882 * re-queuing another scan if one has been requested */
2883 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2884 IWL_DEBUG_INFO("Aborted scan completed.\n");
2885 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2886 } else {
2887 /* If there are more bands on this scan pass reschedule */
2888 if (priv->scan_bands > 0)
2889 goto reschedule;
2890 }
2891
2892 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002893 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002894 IWL_DEBUG_INFO("Setting scan to off\n");
2895
2896 clear_bit(STATUS_SCANNING, &priv->status);
2897
2898 IWL_DEBUG_INFO("Scan took %dms\n",
2899 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2900
2901 queue_work(priv->workqueue, &priv->scan_completed);
2902
2903 return;
2904
2905reschedule:
2906 priv->scan_pass_start = jiffies;
2907 queue_work(priv->workqueue, &priv->request_scan);
2908}
2909
2910/* Handle notification from uCode that card's power state is changing
2911 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002912static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002913 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002914{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002915 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002916 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2917 unsigned long status = priv->status;
2918
2919 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2920 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2921 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2922
2923 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
2924 RF_CARD_DISABLED)) {
2925
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002926 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002927 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2928
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002929 if (!iwl_grab_nic_access(priv)) {
2930 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002931 priv, HBUS_TARG_MBX_C,
2932 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2933
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002934 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002935 }
2936
2937 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002938 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002939 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002940 if (!iwl_grab_nic_access(priv)) {
2941 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002942 priv, HBUS_TARG_MBX_C,
2943 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2944
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002945 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002946 }
2947 }
2948
2949 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002950 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002951 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002952 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2953 if (!iwl_grab_nic_access(priv))
2954 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002955 }
2956 }
2957
2958 if (flags & HW_CARD_DISABLED)
2959 set_bit(STATUS_RF_KILL_HW, &priv->status);
2960 else
2961 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2962
2963
2964 if (flags & SW_CARD_DISABLED)
2965 set_bit(STATUS_RF_KILL_SW, &priv->status);
2966 else
2967 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2968
2969 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002970 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002971
2972 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2973 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2974 (test_bit(STATUS_RF_KILL_SW, &status) !=
2975 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2976 queue_work(priv->workqueue, &priv->rf_kill);
2977 else
2978 wake_up_interruptible(&priv->wait_command_queue);
2979}
2980
2981/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002982 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07002983 *
2984 * Setup the RX handlers for each of the reply types sent from the uCode
2985 * to the host.
2986 *
2987 * This function chains into the hardware specific files for them to setup
2988 * any hardware specific handlers as well.
2989 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002990static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002991{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002992 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
2993 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
2994 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
2995 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07002996 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002997 iwl4965_rx_spectrum_measure_notif;
2998 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002999 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003000 iwl4965_rx_pm_debug_statistics_notif;
3001 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003002
Ben Cahill9fbab512007-11-29 11:09:47 +08003003 /*
3004 * The same handler is used for both the REPLY to a discrete
3005 * statistics request from the host as well as for the periodic
3006 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003007 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003008 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3009 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003010
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003011 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3012 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003013 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003014 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003015 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003016 iwl4965_rx_scan_complete_notif;
3017 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3018 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003019
Ben Cahill9fbab512007-11-29 11:09:47 +08003020 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07003021 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003022}
3023
3024/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003025 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003026 * @rxb: Rx buffer to reclaim
3027 *
3028 * If an Rx buffer has an async callback associated with it the callback
3029 * will be executed. The attached skb (if present) will only be freed
3030 * if the callback returns 1
3031 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003032static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08003033 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003034{
Tomas Winklerdb11d632008-05-05 10:22:33 +08003035 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003036 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3037 int txq_id = SEQ_TO_QUEUE(sequence);
3038 int index = SEQ_TO_INDEX(sequence);
3039 int huge = sequence & SEQ_HUGE_FRAME;
3040 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003041 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003042
3043 /* If a Tx command is being handled and it isn't in the actual
3044 * command queue then there a command routing bug has been introduced
3045 * in the queue management code. */
3046 if (txq_id != IWL_CMD_QUEUE_NUM)
3047 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3048 txq_id, pkt->hdr.cmd);
3049 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3050
3051 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3052 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3053
3054 /* Input error checking is done when commands are added to queue. */
3055 if (cmd->meta.flags & CMD_WANT_SKB) {
3056 cmd->meta.source->u.skb = rxb->skb;
3057 rxb->skb = NULL;
3058 } else if (cmd->meta.u.callback &&
3059 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3060 rxb->skb = NULL;
3061
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003062 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003063
3064 if (!(cmd->meta.flags & CMD_ASYNC)) {
3065 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3066 wake_up_interruptible(&priv->wait_command_queue);
3067 }
3068}
3069
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003070/*
3071 * this should be called while priv->lock is locked
3072*/
Tomas Winklera55360e2008-05-05 10:22:28 +08003073static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003074{
Tomas Winklera55360e2008-05-05 10:22:28 +08003075 iwl_rx_allocate(priv);
3076 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003077}
3078
3079
Zhu Yib481de92007-09-25 17:54:57 -07003080/**
Tomas Winklera55360e2008-05-05 10:22:28 +08003081 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003082 *
3083 * Uses the priv->rx_handlers callback function array to invoke
3084 * the appropriate handlers, including command responses,
3085 * frame-received notifications, and other notifications.
3086 */
Tomas Winklera55360e2008-05-05 10:22:28 +08003087void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003088{
Tomas Winklera55360e2008-05-05 10:22:28 +08003089 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08003090 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08003091 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003092 u32 r, i;
3093 int reclaim;
3094 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003095 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003096 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003097
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003098 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3099 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08003100 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003101 i = rxq->read;
3102
3103 /* Rx interrupt, but nothing sent from uCode */
3104 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08003105 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07003106
Tomas Winklera55360e2008-05-05 10:22:28 +08003107 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003108 fill_rx = 1;
3109
Zhu Yib481de92007-09-25 17:54:57 -07003110 while (i != r) {
3111 rxb = rxq->queue[i];
3112
Ben Cahill9fbab512007-11-29 11:09:47 +08003113 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003114 * then a bug has been introduced in the queue refilling
3115 * routines -- catch it here */
3116 BUG_ON(rxb == NULL);
3117
3118 rxq->queue[i] = NULL;
3119
3120 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003121 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003122 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08003123 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003124
3125 /* Reclaim a command buffer only if this packet is a response
3126 * to a (driver-originated) command.
3127 * If the packet (e.g. Rx frame) originated from uCode,
3128 * there is no command buffer to reclaim.
3129 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3130 * but apparently a few don't get set; catch them here. */
3131 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3132 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003133 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003134 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003135 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3136 (pkt->hdr.cmd != REPLY_TX);
3137
3138 /* Based on type of command response or notification,
3139 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003140 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003141 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003142 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
3143 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003144 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3145 } else {
3146 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08003147 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07003148 "r %d i %d No handler needed for %s, 0x%02x\n",
3149 r, i, get_cmd_string(pkt->hdr.cmd),
3150 pkt->hdr.cmd);
3151 }
3152
3153 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003154 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07003155 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003156 * as we reclaim the driver command queue */
3157 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003158 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003159 else
3160 IWL_WARNING("Claim null rxb?\n");
3161 }
3162
3163 /* For now we just don't re-use anything. We can tweak this
3164 * later to try and re-use notification packets and SKBs that
3165 * fail to Rx correctly */
3166 if (rxb->skb != NULL) {
3167 priv->alloc_rxb_skb--;
3168 dev_kfree_skb_any(rxb->skb);
3169 rxb->skb = NULL;
3170 }
3171
3172 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003173 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003174 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003175 spin_lock_irqsave(&rxq->lock, flags);
3176 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3177 spin_unlock_irqrestore(&rxq->lock, flags);
3178 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003179 /* If there are a lot of unused frames,
3180 * restock the Rx queue so ucode wont assert. */
3181 if (fill_rx) {
3182 count++;
3183 if (count >= 8) {
3184 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003185 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003186 count = 0;
3187 }
3188 }
Zhu Yib481de92007-09-25 17:54:57 -07003189 }
3190
3191 /* Backtrack one entry */
3192 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003193 iwl_rx_queue_restock(priv);
3194}
3195/* Convert linear signal-to-noise ratio into dB */
3196static u8 ratio2dB[100] = {
3197/* 0 1 2 3 4 5 6 7 8 9 */
3198 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3199 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3200 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3201 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3202 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3203 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3204 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3205 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3206 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3207 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3208};
3209
3210/* Calculates a relative dB value from a ratio of linear
3211 * (i.e. not dB) signal levels.
3212 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
3213int iwl4965_calc_db_from_ratio(int sig_ratio)
3214{
3215 /* 1000:1 or higher just report as 60 dB */
3216 if (sig_ratio >= 1000)
3217 return 60;
3218
3219 /* 100:1 or higher, divide by 10 and use table,
3220 * add 20 dB to make up for divide by 10 */
3221 if (sig_ratio >= 100)
3222 return (20 + (int)ratio2dB[sig_ratio/10]);
3223
3224 /* We shouldn't see this */
3225 if (sig_ratio < 1)
3226 return 0;
3227
3228 /* Use table for ratios 1:1 - 99:1 */
3229 return (int)ratio2dB[sig_ratio];
3230}
3231
3232#define PERFECT_RSSI (-20) /* dBm */
3233#define WORST_RSSI (-95) /* dBm */
3234#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3235
3236/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3237 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3238 * about formulas used below. */
3239int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
3240{
3241 int sig_qual;
3242 int degradation = PERFECT_RSSI - rssi_dbm;
3243
3244 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3245 * as indicator; formula is (signal dbm - noise dbm).
3246 * SNR at or above 40 is a great signal (100%).
3247 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3248 * Weakest usable signal is usually 10 - 15 dB SNR. */
3249 if (noise_dbm) {
3250 if (rssi_dbm - noise_dbm >= 40)
3251 return 100;
3252 else if (rssi_dbm < noise_dbm)
3253 return 0;
3254 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3255
3256 /* Else use just the signal level.
3257 * This formula is a least squares fit of data points collected and
3258 * compared with a reference system that had a percentage (%) display
3259 * for signal quality. */
3260 } else
3261 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3262 (15 * RSSI_RANGE + 62 * degradation)) /
3263 (RSSI_RANGE * RSSI_RANGE);
3264
3265 if (sig_qual > 100)
3266 sig_qual = 100;
3267 else if (sig_qual < 1)
3268 sig_qual = 0;
3269
3270 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07003271}
3272
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003273/**
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003274 * iwl_txq_update_write_ptr - Send new write index to hardware
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003275 */
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003276static int iwl_txq_update_write_ptr(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +08003277 struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003278{
3279 u32 reg = 0;
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003280 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003281 int txq_id = txq->q.id;
3282
3283 if (txq->need_update == 0)
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003284 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003285
3286 /* if we're trying to save power */
3287 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3288 /* wake up nic if it's powered down ...
3289 * uCode will wake up, and interrupt us again, so next
3290 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003291 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003292
3293 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3294 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003295 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003296 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003297 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003298 }
3299
3300 /* restore this queue's parameters in nic hardware. */
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003301 ret = iwl_grab_nic_access(priv);
3302 if (ret)
3303 return ret;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003304 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003305 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003306 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003307
3308 /* else not in power-save mode, uCode will never sleep when we're
3309 * trying to tx (during RFKILL, we're not trying to tx). */
3310 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003311 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003312 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003313
3314 txq->need_update = 0;
3315
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003316 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003317}
3318
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003319#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003320static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003321{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08003322 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07003323 DECLARE_MAC_BUF(mac);
3324
Zhu Yib481de92007-09-25 17:54:57 -07003325 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08003326 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003327 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3328 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3329 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3330 le32_to_cpu(rxon->filter_flags));
3331 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3332 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3333 rxon->ofdm_basic_rates);
3334 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07003335 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3336 print_mac(mac, rxon->node_addr));
3337 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3338 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003339 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3340}
3341#endif
3342
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003343static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003344{
3345 IWL_DEBUG_ISR("Enabling interrupts\n");
3346 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003347 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003348}
3349
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003350/* call this function to flush any scheduled tasklet */
3351static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3352{
3353 /* wait to make sure we flush pedding tasklet*/
3354 synchronize_irq(priv->pci_dev->irq);
3355 tasklet_kill(&priv->irq_tasklet);
3356}
3357
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003358static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003359{
3360 clear_bit(STATUS_INT_ENABLED, &priv->status);
3361
3362 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003363 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003364
3365 /* acknowledge/clear/reset any interrupts still pending
3366 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003367 iwl_write32(priv, CSR_INT, 0xffffffff);
3368 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003369 IWL_DEBUG_ISR("Disabled interrupts\n");
3370}
3371
3372static const char *desc_lookup(int i)
3373{
3374 switch (i) {
3375 case 1:
3376 return "FAIL";
3377 case 2:
3378 return "BAD_PARAM";
3379 case 3:
3380 return "BAD_CHECKSUM";
3381 case 4:
3382 return "NMI_INTERRUPT";
3383 case 5:
3384 return "SYSASSERT";
3385 case 6:
3386 return "FATAL_ERROR";
3387 }
3388
3389 return "UNKNOWN";
3390}
3391
3392#define ERROR_START_OFFSET (1 * sizeof(u32))
3393#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3394
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003395static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003396{
3397 u32 data2, line;
3398 u32 desc, time, count, base, data1;
3399 u32 blink1, blink2, ilink1, ilink2;
3400 int rc;
3401
3402 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3403
Tomas Winkler57aab752008-04-14 21:16:03 -07003404 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003405 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3406 return;
3407 }
3408
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003409 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003410 if (rc) {
3411 IWL_WARNING("Can not read from adapter at this time.\n");
3412 return;
3413 }
3414
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003415 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003416
3417 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3418 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003419 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003420 }
3421
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003422 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3423 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3424 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3425 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3426 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3427 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3428 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3429 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3430 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003431
3432 IWL_ERROR("Desc Time "
3433 "data1 data2 line\n");
3434 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3435 desc_lookup(desc), desc, time, data1, data2, line);
3436 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3437 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3438 ilink1, ilink2);
3439
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003440 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003441}
3442
3443#define EVENT_START_OFFSET (4 * sizeof(u32))
3444
3445/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003446 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003447 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003448 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003449 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003450static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003451 u32 num_events, u32 mode)
3452{
3453 u32 i;
3454 u32 base; /* SRAM byte address of event log header */
3455 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3456 u32 ptr; /* SRAM byte address of log data */
3457 u32 ev, time, data; /* event log data */
3458
3459 if (num_events == 0)
3460 return;
3461
3462 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3463
3464 if (mode == 0)
3465 event_size = 2 * sizeof(u32);
3466 else
3467 event_size = 3 * sizeof(u32);
3468
3469 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3470
3471 /* "time" is actually "data" for mode 0 (no timestamp).
3472 * place event id # at far right for easier visual parsing. */
3473 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003474 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003475 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003476 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003477 ptr += sizeof(u32);
3478 if (mode == 0)
3479 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
3480 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003481 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003482 ptr += sizeof(u32);
3483 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
3484 }
3485 }
3486}
3487
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003488static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003489{
3490 int rc;
3491 u32 base; /* SRAM byte address of event log header */
3492 u32 capacity; /* event log capacity in # entries */
3493 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3494 u32 num_wraps; /* # times uCode wrapped to top of log */
3495 u32 next_entry; /* index of next entry to be written by uCode */
3496 u32 size; /* # entries that we'll print */
3497
3498 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07003499 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003500 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
3501 return;
3502 }
3503
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003504 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003505 if (rc) {
3506 IWL_WARNING("Can not read from adapter at this time.\n");
3507 return;
3508 }
3509
3510 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003511 capacity = iwl_read_targ_mem(priv, base);
3512 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3513 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3514 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003515
3516 size = num_wraps ? capacity : next_entry;
3517
3518 /* bail out if nothing in log */
3519 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08003520 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003521 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003522 return;
3523 }
3524
Zhu Yi583fab32007-09-27 11:27:30 +08003525 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003526 size, num_wraps);
3527
3528 /* if uCode has wrapped back to top of log, start at the oldest entry,
3529 * i.e the next one that uCode would fill. */
3530 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003531 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003532 capacity - next_entry, mode);
3533
3534 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003535 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003536
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003537 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003538}
3539
3540/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003542 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003543static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003544{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003545 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07003546 set_bit(STATUS_FW_ERROR, &priv->status);
3547
3548 /* Cancel currently queued command. */
3549 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3550
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003551#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003552 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003553 iwl4965_dump_nic_error_log(priv);
3554 iwl4965_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08003555 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003556 }
3557#endif
3558
3559 wake_up_interruptible(&priv->wait_command_queue);
3560
3561 /* Keep the restart process from trying to send host
3562 * commands by clearing the INIT status bit */
3563 clear_bit(STATUS_READY, &priv->status);
3564
3565 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003566 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07003567 "Restarting adapter due to uCode error.\n");
3568
Tomas Winkler3109ece2008-03-28 16:33:35 -07003569 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003570 memcpy(&priv->recovery_rxon, &priv->active_rxon,
3571 sizeof(priv->recovery_rxon));
3572 priv->error_recovering = 1;
3573 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08003574 if (priv->cfg->mod_params->restart_fw)
3575 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07003576 }
3577}
3578
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003579static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003580{
3581 unsigned long flags;
3582
3583 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
3584 sizeof(priv->staging_rxon));
3585 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003586 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003587
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003588 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07003589
3590 spin_lock_irqsave(&priv->lock, flags);
3591 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
3592 priv->error_recovering = 0;
3593 spin_unlock_irqrestore(&priv->lock, flags);
3594}
3595
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003596static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003597{
3598 u32 inta, handled = 0;
3599 u32 inta_fh;
3600 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003601#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003602 u32 inta_mask;
3603#endif
3604
3605 spin_lock_irqsave(&priv->lock, flags);
3606
3607 /* Ack/clear/reset pending uCode interrupts.
3608 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3609 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003610 inta = iwl_read32(priv, CSR_INT);
3611 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003612
3613 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3614 * Any new interrupts that happen after this, either while we're
3615 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003616 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3617 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003618
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003619#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003620 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003621 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003622 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003623 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3624 inta, inta_mask, inta_fh);
3625 }
3626#endif
3627
3628 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3629 * atomic, make sure that inta covers all the interrupts that
3630 * we've discovered, even if FH interrupt came in just after
3631 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003632 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003633 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003634 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003635 inta |= CSR_INT_BIT_FH_TX;
3636
3637 /* Now service all interrupt bits discovered above. */
3638 if (inta & CSR_INT_BIT_HW_ERR) {
3639 IWL_ERROR("Microcode HW error detected. Restarting.\n");
3640
3641 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003642 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003644 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003645
3646 handled |= CSR_INT_BIT_HW_ERR;
3647
3648 spin_unlock_irqrestore(&priv->lock, flags);
3649
3650 return;
3651 }
3652
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003653#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003654 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003655 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003656 if (inta & CSR_INT_BIT_SCD)
3657 IWL_DEBUG_ISR("Scheduler finished to transmit "
3658 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003659
3660 /* Alive notification via Rx interrupt will do the real work */
3661 if (inta & CSR_INT_BIT_ALIVE)
3662 IWL_DEBUG_ISR("Alive interrupt\n");
3663 }
3664#endif
3665 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003666 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003667
Ben Cahill9fbab512007-11-29 11:09:47 +08003668 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07003669 if (inta & CSR_INT_BIT_RF_KILL) {
3670 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003671 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07003672 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
3673 hw_rf_kill = 1;
3674
Ester Kummerf3d67992008-05-06 11:05:12 +08003675 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07003676 hw_rf_kill ? "disable radio":"enable radio");
3677
3678 /* Queue restart only if RF_KILL switch was set to "kill"
3679 * when we loaded driver, and is now set to "enable".
3680 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08003681 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08003682 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
3683 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003684 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08003685 }
Zhu Yib481de92007-09-25 17:54:57 -07003686
3687 handled |= CSR_INT_BIT_RF_KILL;
3688 }
3689
Ben Cahill9fbab512007-11-29 11:09:47 +08003690 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07003691 if (inta & CSR_INT_BIT_CT_KILL) {
3692 IWL_ERROR("Microcode CT kill error detected.\n");
3693 handled |= CSR_INT_BIT_CT_KILL;
3694 }
3695
3696 /* Error detected by uCode */
3697 if (inta & CSR_INT_BIT_SW_ERR) {
3698 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
3699 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003700 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003701 handled |= CSR_INT_BIT_SW_ERR;
3702 }
3703
3704 /* uCode wakes up after power-down sleep */
3705 if (inta & CSR_INT_BIT_WAKEUP) {
3706 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08003707 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003708 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
3709 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
3710 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
3711 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
3712 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
3713 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003714
3715 handled |= CSR_INT_BIT_WAKEUP;
3716 }
3717
3718 /* All uCode command responses, including Tx command responses,
3719 * Rx "responses" (frame-received notification), and other
3720 * notifications from uCode come through here*/
3721 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08003722 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003723 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3724 }
3725
3726 if (inta & CSR_INT_BIT_FH_TX) {
3727 IWL_DEBUG_ISR("Tx interrupt\n");
3728 handled |= CSR_INT_BIT_FH_TX;
3729 }
3730
3731 if (inta & ~handled)
3732 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
3733
3734 if (inta & ~CSR_INI_SET_MASK) {
3735 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
3736 inta & ~CSR_INI_SET_MASK);
3737 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
3738 }
3739
3740 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003741 /* only Re-enable if diabled by irq */
3742 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3743 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003744
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003745#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003746 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003747 inta = iwl_read32(priv, CSR_INT);
3748 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3749 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003750 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3751 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3752 }
3753#endif
3754 spin_unlock_irqrestore(&priv->lock, flags);
3755}
3756
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003757static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003758{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003759 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003760 u32 inta, inta_mask;
3761 u32 inta_fh;
3762 if (!priv)
3763 return IRQ_NONE;
3764
3765 spin_lock(&priv->lock);
3766
3767 /* Disable (but don't clear!) interrupts here to avoid
3768 * back-to-back ISRs and sporadic interrupts from our NIC.
3769 * If we have something to service, the tasklet will re-enable ints.
3770 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003771 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3772 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003773
3774 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003775 inta = iwl_read32(priv, CSR_INT);
3776 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003777
3778 /* Ignore interrupt if there's nothing in NIC to service.
3779 * This may be due to IRQ shared with another device,
3780 * or due to sporadic interrupts thrown from our NIC. */
3781 if (!inta && !inta_fh) {
3782 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3783 goto none;
3784 }
3785
3786 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08003787 /* Hardware disappeared. It might have already raised
3788 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07003789 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08003790 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003791 }
3792
3793 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3794 inta, inta_mask, inta_fh);
3795
Joonwoo Park25c03d82008-01-23 10:15:20 -08003796 inta &= ~CSR_INT_BIT_SCD;
3797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003798 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003799 if (likely(inta || inta_fh))
3800 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07003801
Oliver Neukum66fbb542007-11-15 09:31:10 +08003802 unplugged:
3803 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07003804 return IRQ_HANDLED;
3805
3806 none:
3807 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003808 /* only Re-enable if diabled by irq */
3809 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3810 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003811 spin_unlock(&priv->lock);
3812 return IRQ_NONE;
3813}
3814
Zhu Yib481de92007-09-25 17:54:57 -07003815/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3816 * sending probe req. This should be set long enough to hear probe responses
3817 * from more than one AP. */
3818#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
3819#define IWL_ACTIVE_DWELL_TIME_52 (10)
3820
3821/* For faster active scanning, scan will move to the next channel if fewer than
3822 * PLCP_QUIET_THRESH packets are heard on this channel within
3823 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3824 * time if it's a quiet channel (nothing responded to our probe, and there's
3825 * no other traffic).
3826 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3827#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
3828#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
3829
3830/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3831 * Must be set longer than active dwell time.
3832 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
3833#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
3834#define IWL_PASSIVE_DWELL_TIME_52 (10)
3835#define IWL_PASSIVE_DWELL_BASE (100)
3836#define IWL_CHANNEL_TUNE_TIME 5
3837
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003838static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003839 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003840{
Johannes Berg8318d782008-01-24 19:38:38 +01003841 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003842 return IWL_ACTIVE_DWELL_TIME_52;
3843 else
3844 return IWL_ACTIVE_DWELL_TIME_24;
3845}
3846
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003847static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003848 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003849{
Johannes Berg8318d782008-01-24 19:38:38 +01003850 u16 active = iwl4965_get_active_dwell_time(priv, band);
3851 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003852 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
3853 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
3854
Tomas Winkler3109ece2008-03-28 16:33:35 -07003855 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003856 /* If we're associated, we clamp the maximum passive
3857 * dwell time to be 98% of the beacon interval (minus
3858 * 2 * channel tune time) */
3859 passive = priv->beacon_int;
3860 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
3861 passive = IWL_PASSIVE_DWELL_BASE;
3862 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
3863 }
3864
3865 if (passive <= active)
3866 passive = active + 1;
3867
3868 return passive;
3869}
3870
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003871static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003872 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07003873 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003874 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07003875{
3876 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01003877 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003878 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003879 u16 passive_dwell = 0;
3880 u16 active_dwell = 0;
3881 int added, i;
3882
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07003883 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01003884 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07003885 return 0;
3886
Johannes Berg8318d782008-01-24 19:38:38 +01003887 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07003888
Johannes Berg8318d782008-01-24 19:38:38 +01003889 active_dwell = iwl4965_get_active_dwell_time(priv, band);
3890 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07003891
Johannes Berg8318d782008-01-24 19:38:38 +01003892 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02003893 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3894 continue;
3895
Johannes Berg8318d782008-01-24 19:38:38 +01003896 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07003897
Assaf Krauss8622e702008-03-21 13:53:43 -07003898 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08003899 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003900 if (!is_channel_valid(ch_info)) {
3901 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
3902 scan_ch->channel);
3903 continue;
3904 }
3905
3906 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01003907 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07003908 scan_ch->type = 0; /* passive */
3909 else
3910 scan_ch->type = 1; /* active */
3911
3912 if (scan_ch->type & 1)
3913 scan_ch->type |= (direct_mask << 1);
3914
Zhu Yib481de92007-09-25 17:54:57 -07003915 scan_ch->active_dwell = cpu_to_le16(active_dwell);
3916 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
3917
Ben Cahill9fbab512007-11-29 11:09:47 +08003918 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07003919 scan_ch->tpc.dsp_atten = 110;
3920 /* scan_pwr_info->tpc.dsp_atten; */
3921
3922 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01003923 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003924 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
3925 else {
3926 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
3927 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08003928 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08003929 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07003930 */
3931 }
3932
3933 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
3934 scan_ch->channel,
3935 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
3936 (scan_ch->type & 1) ?
3937 active_dwell : passive_dwell);
3938
3939 scan_ch++;
3940 added++;
3941 }
3942
3943 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
3944 return added;
3945}
3946
Zhu Yib481de92007-09-25 17:54:57 -07003947/******************************************************************************
3948 *
3949 * uCode download functions
3950 *
3951 ******************************************************************************/
3952
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003953static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003954{
Tomas Winkler98c92212008-01-14 17:46:20 -08003955 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
3956 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
3957 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3958 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
3959 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3960 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07003961}
3962
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08003963static void iwl4965_nic_start(struct iwl_priv *priv)
3964{
3965 /* Remove all resets to allow NIC to operate */
3966 iwl_write32(priv, CSR_RESET, 0);
3967}
3968
3969
Zhu Yib481de92007-09-25 17:54:57 -07003970/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003971 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07003972 *
3973 * Copy into buffers for card to fetch via bus-mastering
3974 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003975static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003976{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003977 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003978 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003979 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08003980 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07003981 u8 *src;
3982 size_t len;
3983 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
3984
3985 /* Ask kernel firmware_class module to get the boot firmware off disk.
3986 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003987 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
3988 if (ret < 0) {
3989 IWL_ERROR("%s firmware file req failed: Reason %d\n",
3990 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07003991 goto error;
3992 }
3993
3994 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3995 name, ucode_raw->size);
3996
3997 /* Make sure that we got at least our header! */
3998 if (ucode_raw->size < sizeof(*ucode)) {
3999 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004000 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004001 goto err_release;
4002 }
4003
4004 /* Data from ucode file: header followed by uCode images */
4005 ucode = (void *)ucode_raw->data;
4006
4007 ver = le32_to_cpu(ucode->ver);
4008 inst_size = le32_to_cpu(ucode->inst_size);
4009 data_size = le32_to_cpu(ucode->data_size);
4010 init_size = le32_to_cpu(ucode->init_size);
4011 init_data_size = le32_to_cpu(ucode->init_data_size);
4012 boot_size = le32_to_cpu(ucode->boot_size);
4013
4014 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
4015 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
4016 inst_size);
4017 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
4018 data_size);
4019 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
4020 init_size);
4021 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
4022 init_data_size);
4023 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
4024 boot_size);
4025
4026 /* Verify size of file vs. image size info in file's header */
4027 if (ucode_raw->size < sizeof(*ucode) +
4028 inst_size + data_size + init_size +
4029 init_data_size + boot_size) {
4030
4031 IWL_DEBUG_INFO("uCode file size %d too small\n",
4032 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004033 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004034 goto err_release;
4035 }
4036
4037 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004038 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004039 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4040 inst_size);
4041 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004042 goto err_release;
4043 }
4044
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004045 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004046 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4047 data_size);
4048 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004049 goto err_release;
4050 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004051 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004052 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004053 ("uCode init instr len %d too large to fit in\n",
4054 init_size);
4055 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004056 goto err_release;
4057 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004058 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004059 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004060 ("uCode init data len %d too large to fit in\n",
4061 init_data_size);
4062 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004063 goto err_release;
4064 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004065 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004066 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004067 ("uCode boot instr len %d too large to fit in\n",
4068 boot_size);
4069 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004070 goto err_release;
4071 }
4072
4073 /* Allocate ucode buffers for card's bus-master loading ... */
4074
4075 /* Runtime instructions and 2 copies of data:
4076 * 1) unmodified from disk
4077 * 2) backup cache for save/restore during power-downs */
4078 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004079 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004080
4081 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004082 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004083
4084 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004085 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004086
4087 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004088 if (init_size && init_data_size) {
4089 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004090 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07004091
Tomas Winkler90e759d2007-11-29 11:09:41 +08004092 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004093 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004094
4095 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4096 goto err_pci_alloc;
4097 }
Zhu Yib481de92007-09-25 17:54:57 -07004098
4099 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004100 if (boot_size) {
4101 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004102 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004103
Tomas Winkler90e759d2007-11-29 11:09:41 +08004104 if (!priv->ucode_boot.v_addr)
4105 goto err_pci_alloc;
4106 }
Zhu Yib481de92007-09-25 17:54:57 -07004107
4108 /* Copy images into buffers for card's bus-master reads ... */
4109
4110 /* Runtime instructions (first block of data in file) */
4111 src = &ucode->data[0];
4112 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004113 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004114 memcpy(priv->ucode_code.v_addr, src, len);
4115 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4116 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4117
4118 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004119 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004120 src = &ucode->data[inst_size];
4121 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004122 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004123 memcpy(priv->ucode_data.v_addr, src, len);
4124 memcpy(priv->ucode_data_backup.v_addr, src, len);
4125
4126 /* Initialization instructions (3rd block) */
4127 if (init_size) {
4128 src = &ucode->data[inst_size + data_size];
4129 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004130 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4131 len);
Zhu Yib481de92007-09-25 17:54:57 -07004132 memcpy(priv->ucode_init.v_addr, src, len);
4133 }
4134
4135 /* Initialization data (4th block) */
4136 if (init_data_size) {
4137 src = &ucode->data[inst_size + data_size + init_size];
4138 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004139 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
4140 len);
Zhu Yib481de92007-09-25 17:54:57 -07004141 memcpy(priv->ucode_init_data.v_addr, src, len);
4142 }
4143
4144 /* Bootstrap instructions (5th block) */
4145 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4146 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004147 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004148 memcpy(priv->ucode_boot.v_addr, src, len);
4149
4150 /* We have our copies now, allow OS release its copies */
4151 release_firmware(ucode_raw);
4152 return 0;
4153
4154 err_pci_alloc:
4155 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004156 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004157 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004158
4159 err_release:
4160 release_firmware(ucode_raw);
4161
4162 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08004163 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004164}
4165
Zhu Yib481de92007-09-25 17:54:57 -07004166/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004167 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004168 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004169 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07004170 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004171static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004172{
Tomas Winkler57aab752008-04-14 21:16:03 -07004173 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004174
4175 IWL_DEBUG_INFO("Runtime Alive received.\n");
4176
4177 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4178 /* We had an error bringing up the hardware, so take it
4179 * all the way back down so we can try again */
4180 IWL_DEBUG_INFO("Alive failed.\n");
4181 goto restart;
4182 }
4183
4184 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4185 * This is a paranoid check, because we would not have gotten the
4186 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07004187 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004188 /* Runtime instruction load was bad;
4189 * take it all the way back down so we can try again */
4190 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4191 goto restart;
4192 }
4193
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004194 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004195
Tomas Winkler57aab752008-04-14 21:16:03 -07004196 ret = priv->cfg->ops->lib->alive_notify(priv);
4197 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07004198 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07004199 ret);
Zhu Yib481de92007-09-25 17:54:57 -07004200 goto restart;
4201 }
4202
Ben Cahill9fbab512007-11-29 11:09:47 +08004203 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07004204 set_bit(STATUS_ALIVE, &priv->status);
4205
4206 /* Clear out the uCode error bit if it is set */
4207 clear_bit(STATUS_FW_ERROR, &priv->status);
4208
Tomas Winklerfee12472008-04-03 16:05:21 -07004209 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004210 return;
4211
Zhu Yi5a66926a2008-01-14 17:46:18 -08004212 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004213
4214 priv->active_rate = priv->rates_mask;
4215 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
4216
Tomas Winkler3109ece2008-03-28 16:33:35 -07004217 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08004218 struct iwl_rxon_cmd *active_rxon =
4219 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07004220
4221 memcpy(&priv->staging_rxon, &priv->active_rxon,
4222 sizeof(priv->staging_rxon));
4223 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4224 } else {
4225 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004226 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004227 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
4228 }
4229
Ben Cahill9fbab512007-11-29 11:09:47 +08004230 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004231 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004232
4233 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004234 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004235
4236 /* At this point, the NIC is initialized and operational */
4237 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004238
4239 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004240
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07004241 iwl_leds_register(priv);
4242
Zhu Yib481de92007-09-25 17:54:57 -07004243 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07004244 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004245 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07004246
4247 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004248 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004249
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004250 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004251 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07004252 return;
4253
4254 restart:
4255 queue_work(priv->workqueue, &priv->restart);
4256}
4257
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004258static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07004259
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004260static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004261{
4262 unsigned long flags;
4263 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4264 struct ieee80211_conf *conf = NULL;
4265
4266 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4267
4268 conf = ieee80211_get_hw_conf(priv->hw);
4269
4270 if (!exit_pending)
4271 set_bit(STATUS_EXIT_PENDING, &priv->status);
4272
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07004273 iwl_leds_unregister(priv);
4274
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004275 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
4276
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004277 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004278
4279 /* Unblock any waiting calls */
4280 wake_up_interruptible_all(&priv->wait_command_queue);
4281
Zhu Yib481de92007-09-25 17:54:57 -07004282 /* Wipe out the EXIT_PENDING status bit if we are not actually
4283 * exiting the module */
4284 if (!exit_pending)
4285 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4286
4287 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004288 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07004289
4290 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004291 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004292 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004293 spin_unlock_irqrestore(&priv->lock, flags);
4294 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004295
4296 if (priv->mac80211_registered)
4297 ieee80211_stop_queues(priv->hw);
4298
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004299 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07004300 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07004301 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004302 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4303 STATUS_RF_KILL_HW |
4304 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4305 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004306 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4307 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004308 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4309 STATUS_IN_SUSPEND;
4310 goto exit;
4311 }
4312
4313 /* ...otherwise clear out all the status bits but the RF Kill and
4314 * SUSPEND bits and continue taking the NIC down. */
4315 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4316 STATUS_RF_KILL_HW |
4317 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4318 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004319 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4320 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004321 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4322 STATUS_IN_SUSPEND |
4323 test_bit(STATUS_FW_ERROR, &priv->status) <<
4324 STATUS_FW_ERROR;
4325
4326 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004327 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08004328 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07004329 spin_unlock_irqrestore(&priv->lock, flags);
4330
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004331 iwl4965_hw_txq_ctx_stop(priv);
4332 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004333
4334 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004335 if (!iwl_grab_nic_access(priv)) {
4336 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004337 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004338 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004339 }
4340 spin_unlock_irqrestore(&priv->lock, flags);
4341
4342 udelay(5);
4343
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004344 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004345 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004346 iwl4965_hw_nic_reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004347 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004348
4349 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004350 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07004351
4352 if (priv->ibss_beacon)
4353 dev_kfree_skb(priv->ibss_beacon);
4354 priv->ibss_beacon = NULL;
4355
4356 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08004357 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004358}
4359
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004360static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004361{
4362 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004363 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004364 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08004365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004366 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004367}
4368
4369#define MAX_HW_RESTARTS 5
4370
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004371static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004372{
Tomas Winkler57aab752008-04-14 21:16:03 -07004373 int i;
4374 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004375
4376 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4377 IWL_WARNING("Exit pending; will not bring the NIC up\n");
4378 return -EIO;
4379 }
4380
4381 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
4382 IWL_WARNING("Radio disabled by SW RF kill (module "
4383 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004384 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004385 return -ENODEV;
4386 }
4387
Reinette Chatree903fbd2008-01-30 22:05:15 -08004388 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
4389 IWL_ERROR("ucode not available for device bringup\n");
4390 return -EIO;
4391 }
4392
Zhu Yie655b9f2008-01-24 02:19:38 -08004393 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004394 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08004395 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4396 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4397 else {
4398 set_bit(STATUS_RF_KILL_HW, &priv->status);
4399 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004400 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004401 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
4402 return -ENODEV;
4403 }
Zhu Yib481de92007-09-25 17:54:57 -07004404 }
4405
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004406 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004407 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07004408
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004409 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
4410 if (ret) {
4411 IWL_ERROR("Unable to allocate shared memory\n");
4412 return ret;
4413 }
4414
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004415 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07004416 if (ret) {
4417 IWL_ERROR("Unable to init nic\n");
4418 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004419 }
4420
4421 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004422 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4423 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004424 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4425
4426 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004427 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004428 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004429
4430 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004431 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4432 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07004433
4434 /* Copy original ucode data image from disk into backup cache.
4435 * This will be used to initialize the on-board processor's
4436 * data SRAM for a clean start when the runtime program first loads. */
4437 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08004438 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07004439
Zhu Yie655b9f2008-01-24 02:19:38 -08004440 /* We return success when we resume from suspend and rf_kill is on. */
4441 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07004442 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004443
4444 for (i = 0; i < MAX_HW_RESTARTS; i++) {
4445
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004446 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004447
4448 /* load bootstrap state machine,
4449 * load bootstrap program into processor's memory,
4450 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07004451 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004452
Tomas Winkler57aab752008-04-14 21:16:03 -07004453 if (ret) {
4454 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07004455 continue;
4456 }
4457
4458 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08004459 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004460
Zhu Yib481de92007-09-25 17:54:57 -07004461 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
4462
4463 return 0;
4464 }
4465
4466 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004467 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004468
4469 /* tried to restart and config the device for as long as our
4470 * patience could withstand */
4471 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
4472 return -EIO;
4473}
4474
4475
4476/*****************************************************************************
4477 *
4478 * Workqueue callbacks
4479 *
4480 *****************************************************************************/
4481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004482static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004483{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004484 struct iwl_priv *priv =
4485 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004486
4487 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4488 return;
4489
4490 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08004491 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004492 mutex_unlock(&priv->mutex);
4493}
4494
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004495static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004496{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004497 struct iwl_priv *priv =
4498 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004499
4500 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4501 return;
4502
4503 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004504 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004505 mutex_unlock(&priv->mutex);
4506}
4507
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004508static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004509{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004510 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07004511
4512 wake_up_interruptible(&priv->wait_command_queue);
4513
4514 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4515 return;
4516
4517 mutex_lock(&priv->mutex);
4518
Tomas Winklerfee12472008-04-03 16:05:21 -07004519 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08004520 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07004521 "HW and/or SW RF Kill no longer active, restarting "
4522 "device\n");
4523 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4524 queue_work(priv->workqueue, &priv->restart);
4525 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004526 /* make sure mac80211 stop sending Tx frame */
4527 if (priv->mac80211_registered)
4528 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004529
4530 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
4531 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
4532 "disabled by SW switch\n");
4533 else
4534 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
4535 "Kill switch must be turned off for "
4536 "wireless networking to work.\n");
4537 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004538 iwl_rfkill_set_hw_state(priv);
4539
Zhu Yib481de92007-09-25 17:54:57 -07004540 mutex_unlock(&priv->mutex);
4541}
4542
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004543static void iwl4965_bg_set_monitor(struct work_struct *work)
4544{
4545 struct iwl_priv *priv = container_of(work,
4546 struct iwl_priv, set_monitor);
4547
4548 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
4549
4550 mutex_lock(&priv->mutex);
4551
4552 if (!iwl_is_ready(priv))
4553 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
4554 else
4555 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
4556 IWL_ERROR("iwl4965_set_mode() failed\n");
4557
4558 mutex_unlock(&priv->mutex);
4559}
4560
Zhu Yib481de92007-09-25 17:54:57 -07004561#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
4562
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004563static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004564{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004565 struct iwl_priv *priv =
4566 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07004567
4568 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4569 return;
4570
4571 mutex_lock(&priv->mutex);
4572 if (test_bit(STATUS_SCANNING, &priv->status) ||
4573 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08004574 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
4575 "adapter (%dms)\n",
4576 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08004577
Zhu Yib481de92007-09-25 17:54:57 -07004578 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004579 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004580 }
4581 mutex_unlock(&priv->mutex);
4582}
4583
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004584static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004585{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004586 struct iwl_priv *priv =
4587 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07004588 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07004589 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004590 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07004591 .meta.flags = CMD_SIZE_HUGE,
4592 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004593 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07004594 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004595 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01004596 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004597 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07004598 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004599
4600 conf = ieee80211_get_hw_conf(priv->hw);
4601
4602 mutex_lock(&priv->mutex);
4603
Tomas Winklerfee12472008-04-03 16:05:21 -07004604 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004605 IWL_WARNING("request scan called when driver not ready.\n");
4606 goto done;
4607 }
4608
4609 /* Make sure the scan wasn't cancelled before this queued work
4610 * was given the chance to run... */
4611 if (!test_bit(STATUS_SCANNING, &priv->status))
4612 goto done;
4613
4614 /* This should never be called or scheduled if there is currently
4615 * a scan active in the hardware. */
4616 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
4617 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
4618 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07004619 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07004620 goto done;
4621 }
4622
4623 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4624 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4625 goto done;
4626 }
4627
4628 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4629 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
4630 goto done;
4631 }
4632
Tomas Winklerfee12472008-04-03 16:05:21 -07004633 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004634 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4635 goto done;
4636 }
4637
4638 if (!test_bit(STATUS_READY, &priv->status)) {
4639 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
4640 goto done;
4641 }
4642
4643 if (!priv->scan_bands) {
4644 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
4645 goto done;
4646 }
4647
4648 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004649 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07004650 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
4651 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07004652 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004653 goto done;
4654 }
4655 }
4656 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004657 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07004658
4659 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
4660 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
4661
Tomas Winkler3109ece2008-03-28 16:33:35 -07004662 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004663 u16 interval = 0;
4664 u32 extra;
4665 u32 suspend_time = 100;
4666 u32 scan_suspend_time = 100;
4667 unsigned long flags;
4668
4669 IWL_DEBUG_INFO("Scanning while associated...\n");
4670
4671 spin_lock_irqsave(&priv->lock, flags);
4672 interval = priv->beacon_int;
4673 spin_unlock_irqrestore(&priv->lock, flags);
4674
4675 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08004676 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07004677 if (!interval)
4678 interval = suspend_time;
4679
4680 extra = (suspend_time / interval) << 22;
4681 scan_suspend_time = (extra |
4682 ((suspend_time % interval) * 1024));
4683 scan->suspend_time = cpu_to_le32(scan_suspend_time);
4684 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
4685 scan_suspend_time, interval);
4686 }
4687
4688 /* We should add the ability for user to lock to PASSIVE ONLY */
4689 if (priv->one_direct_scan) {
4690 IWL_DEBUG_SCAN
4691 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004692 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07004693 priv->direct_ssid_len));
4694 scan->direct_scan[0].id = WLAN_EID_SSID;
4695 scan->direct_scan[0].len = priv->direct_ssid_len;
4696 memcpy(scan->direct_scan[0].ssid,
4697 priv->direct_ssid, priv->direct_ssid_len);
4698 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004699 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07004700 IWL_DEBUG_SCAN
4701 ("Kicking off one direct scan for '%s' when not associated\n",
4702 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07004703 scan->direct_scan[0].id = WLAN_EID_SSID;
4704 scan->direct_scan[0].len = priv->essid_len;
4705 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
4706 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07004707 } else {
Bill Moss786b4552008-04-17 16:03:40 -07004708 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004709 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07004710 }
Zhu Yib481de92007-09-25 17:54:57 -07004711
Zhu Yib481de92007-09-25 17:54:57 -07004712 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07004713 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07004714 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4715
Zhu Yib481de92007-09-25 17:54:57 -07004716
4717 switch (priv->scan_bands) {
4718 case 2:
4719 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
4720 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004721 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004722 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
4723
4724 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004725 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004726 break;
4727
4728 case 1:
4729 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004730 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004731 RATE_MCS_ANT_B_MSK);
4732 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01004733 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004734 break;
4735
4736 default:
4737 IWL_WARNING("Invalid scan band count\n");
4738 goto done;
4739 }
4740
Tomas Winkler78330fd2008-02-06 02:37:18 +02004741 /* We don't build a direct scan probe request; the uCode will do
4742 * that based on the direct_mask added to each channel entry */
4743 cmd_len = iwl4965_fill_probe_req(priv, band,
4744 (struct ieee80211_mgmt *)scan->data,
4745 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
4746
4747 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07004748 /* select Rx chains */
4749
4750 /* Force use of chains B and C (0x6) for scan Rx.
4751 * Avoid A (0x1) because of its off-channel reception on A-band.
4752 * MIMO is not used here, but value is required to make uCode happy. */
4753 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
4754 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
4755 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
4756 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
4757
4758 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
4759 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
4760
Bill Moss786b4552008-04-17 16:03:40 -07004761 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07004762 scan->channel_count =
4763 iwl4965_get_channels_for_scan(
4764 priv, band, 1, /* active */
4765 direct_mask,
4766 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07004767 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07004768 scan->channel_count =
4769 iwl4965_get_channels_for_scan(
4770 priv, band, 0, /* passive */
4771 direct_mask,
4772 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07004773
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004774 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
4775 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07004776 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004777 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07004778 cmd.data = scan;
4779 scan->len = cpu_to_le16(cmd.len);
4780
4781 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07004782 ret = iwl_send_cmd_sync(priv, &cmd);
4783 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004784 goto done;
4785
4786 queue_delayed_work(priv->workqueue, &priv->scan_check,
4787 IWL_SCAN_CHECK_WATCHDOG);
4788
4789 mutex_unlock(&priv->mutex);
4790 return;
4791
4792 done:
Ian Schram01ebd062007-10-25 17:15:22 +08004793 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07004794 queue_work(priv->workqueue, &priv->scan_completed);
4795 mutex_unlock(&priv->mutex);
4796}
4797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004798static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004799{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004800 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07004801
4802 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4803 return;
4804
4805 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004806 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004807 mutex_unlock(&priv->mutex);
4808}
4809
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004810static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004811{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004812 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07004813
4814 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4815 return;
4816
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004817 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004818 queue_work(priv->workqueue, &priv->up);
4819}
4820
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004821static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004822{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004823 struct iwl_priv *priv =
4824 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07004825
4826 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4827 return;
4828
4829 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08004830 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004831 mutex_unlock(&priv->mutex);
4832}
4833
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004834#define IWL_DELAY_NEXT_SCAN (HZ*2)
4835
Reinette Chatre508e32e2008-04-14 21:16:13 -07004836static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004837{
Zhu Yib481de92007-09-25 17:54:57 -07004838 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07004839 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07004840 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004841
4842 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4843 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
4844 return;
4845 }
4846
Joe Perches0795af52007-10-03 17:59:30 -07004847 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
4848 priv->assoc_id,
4849 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004850
4851
4852 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4853 return;
4854
Zhu Yib481de92007-09-25 17:54:57 -07004855
Reinette Chatre508e32e2008-04-14 21:16:13 -07004856 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08004857 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004858
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004859 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08004860
Zhu Yib481de92007-09-25 17:54:57 -07004861 conf = ieee80211_get_hw_conf(priv->hw);
4862
4863 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004864 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004866 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4867 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07004868 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07004869 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07004870 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004871 IWL_WARNING("REPLY_RXON_TIMING failed - "
4872 "Attempting to continue.\n");
4873
4874 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4875
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004876#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004877 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08004878 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004879#endif /* CONFIG_IWL4965_HT*/
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004880 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004881 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4882
4883 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
4884 priv->assoc_id, priv->beacon_int);
4885
4886 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4887 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4888 else
4889 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4890
4891 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4892 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
4893 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
4894 else
4895 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4896
4897 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4898 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4899
4900 }
4901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004902 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004903
4904 switch (priv->iw_mode) {
4905 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004906 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07004907 break;
4908
4909 case IEEE80211_IF_TYPE_IBSS:
4910
4911 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004912 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004913
Tomas Winkler57bd1be2008-05-15 13:54:03 +08004914 iwl4965_rxon_add_station(priv, iwl_bcast_addr, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004915 iwl4965_rxon_add_station(priv, priv->bssid, 0);
4916 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
4917 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004918
4919 break;
4920
4921 default:
4922 IWL_ERROR("%s Should not be called in %d mode\n",
4923 __FUNCTION__, priv->iw_mode);
4924 break;
4925 }
4926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004927 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004928
Zhu Yib481de92007-09-25 17:54:57 -07004929 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004930 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004931 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004932
4933 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4934 priv->assoc_station_added = 1;
4935
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004936 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08004937
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004938 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004939 /* we have just associated, don't start scan too early */
4940 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004941}
4942
4943
4944static void iwl4965_bg_post_associate(struct work_struct *data)
4945{
4946 struct iwl_priv *priv = container_of(data, struct iwl_priv,
4947 post_associate.work);
4948
4949 mutex_lock(&priv->mutex);
4950 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004951 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07004952
Zhu Yib481de92007-09-25 17:54:57 -07004953}
4954
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004955static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004956{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004957 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07004958
Tomas Winklerfee12472008-04-03 16:05:21 -07004959 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004960 return;
4961
4962 mutex_lock(&priv->mutex);
4963
4964 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004965 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004966
4967 mutex_unlock(&priv->mutex);
4968}
4969
Zhu Yi76bb77e2007-11-22 10:53:22 +08004970static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
4971
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004972static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004973{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004974 struct iwl_priv *priv =
4975 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07004976
Ester Kummerf3d67992008-05-06 11:05:12 +08004977 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07004978
4979 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4980 return;
4981
Zhu Yia0646472007-12-20 14:10:01 +08004982 if (test_bit(STATUS_CONF_PENDING, &priv->status))
4983 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08004984
Zhu Yib481de92007-09-25 17:54:57 -07004985 ieee80211_scan_completed(priv->hw);
4986
4987 /* Since setting the TXPOWER may have been deferred while
4988 * performing the scan, fire one off */
4989 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004990 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004991 mutex_unlock(&priv->mutex);
4992}
4993
4994/*****************************************************************************
4995 *
4996 * mac80211 entry point functions
4997 *
4998 *****************************************************************************/
4999
Zhu Yi5a66926a2008-01-14 17:46:18 -08005000#define UCODE_READY_TIMEOUT (2 * HZ)
5001
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005002static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005003{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005004 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005005 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005006
5007 IWL_DEBUG_MAC80211("enter\n");
5008
Zhu Yi5a66926a2008-01-14 17:46:18 -08005009 if (pci_enable_device(priv->pci_dev)) {
5010 IWL_ERROR("Fail to pci_enable_device\n");
5011 return -ENODEV;
5012 }
5013 pci_restore_state(priv->pci_dev);
5014 pci_enable_msi(priv->pci_dev);
5015
5016 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
5017 DRV_NAME, priv);
5018 if (ret) {
5019 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
5020 goto out_disable_msi;
5021 }
5022
Zhu Yib481de92007-09-25 17:54:57 -07005023 /* we should be verifying the device is ready to be opened */
5024 mutex_lock(&priv->mutex);
5025
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08005026 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08005027 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5028 * ucode filename and max sizes are card-specific. */
5029
5030 if (!priv->ucode_code.len) {
5031 ret = iwl4965_read_ucode(priv);
5032 if (ret) {
5033 IWL_ERROR("Could not read microcode: %d\n", ret);
5034 mutex_unlock(&priv->mutex);
5035 goto out_release_irq;
5036 }
5037 }
5038
Zhu Yie655b9f2008-01-24 02:19:38 -08005039 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005040
Zhu Yib481de92007-09-25 17:54:57 -07005041 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005042
Zhu Yie655b9f2008-01-24 02:19:38 -08005043 if (ret)
5044 goto out_release_irq;
5045
5046 IWL_DEBUG_INFO("Start UP work done.\n");
5047
5048 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5049 return 0;
5050
Zhu Yi5a66926a2008-01-14 17:46:18 -08005051 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5052 * mac80211 will not be run successfully. */
5053 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5054 test_bit(STATUS_READY, &priv->status),
5055 UCODE_READY_TIMEOUT);
5056 if (!ret) {
5057 if (!test_bit(STATUS_READY, &priv->status)) {
5058 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
5059 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5060 ret = -ETIMEDOUT;
5061 goto out_release_irq;
5062 }
5063 }
5064
Zhu Yie655b9f2008-01-24 02:19:38 -08005065 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005066 IWL_DEBUG_MAC80211("leave\n");
5067 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005068
5069out_release_irq:
5070 free_irq(priv->pci_dev->irq, priv);
5071out_disable_msi:
5072 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005073 pci_disable_device(priv->pci_dev);
5074 priv->is_open = 0;
5075 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005076 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005077}
5078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005079static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005080{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005081 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005082
5083 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08005084
Zhu Yie655b9f2008-01-24 02:19:38 -08005085 if (!priv->is_open) {
5086 IWL_DEBUG_MAC80211("leave - skip\n");
5087 return;
5088 }
5089
Zhu Yib481de92007-09-25 17:54:57 -07005090 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005091
Tomas Winklerfee12472008-04-03 16:05:21 -07005092 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005093 /* stop mac, cancel any scan request and clear
5094 * RXON_FILTER_ASSOC_MSK BIT
5095 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08005096 mutex_lock(&priv->mutex);
5097 iwl4965_scan_cancel_timeout(priv, 100);
5098 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005099 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005100 }
5101
Zhu Yi5a66926a2008-01-14 17:46:18 -08005102 iwl4965_down(priv);
5103
5104 flush_workqueue(priv->workqueue);
5105 free_irq(priv->pci_dev->irq, priv);
5106 pci_disable_msi(priv->pci_dev);
5107 pci_save_state(priv->pci_dev);
5108 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005109
Zhu Yib481de92007-09-25 17:54:57 -07005110 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005111}
5112
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005113static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005114 struct ieee80211_tx_control *ctl)
5115{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005116 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005117
5118 IWL_DEBUG_MAC80211("enter\n");
5119
5120 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
5121 IWL_DEBUG_MAC80211("leave - monitor\n");
5122 return -1;
5123 }
5124
5125 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01005126 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07005127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005128 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07005129 dev_kfree_skb_any(skb);
5130
5131 IWL_DEBUG_MAC80211("leave\n");
5132 return 0;
5133}
5134
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005135static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005136 struct ieee80211_if_init_conf *conf)
5137{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005138 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005139 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07005140 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005141
Johannes Berg32bfd352007-12-19 01:31:26 +01005142 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005143
Johannes Berg32bfd352007-12-19 01:31:26 +01005144 if (priv->vif) {
5145 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08005146 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07005147 }
5148
5149 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01005150 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07005151
5152 spin_unlock_irqrestore(&priv->lock, flags);
5153
5154 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02005155
5156 if (conf->mac_addr) {
5157 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
5158 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
5159 }
Zhu Yib481de92007-09-25 17:54:57 -07005160
Tomas Winklerfee12472008-04-03 16:05:21 -07005161 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005162 iwl4965_set_mode(priv, conf->type);
5163
Zhu Yib481de92007-09-25 17:54:57 -07005164 mutex_unlock(&priv->mutex);
5165
Zhu Yi5a66926a2008-01-14 17:46:18 -08005166 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005167 return 0;
5168}
5169
5170/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005171 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07005172 *
5173 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5174 * be set inappropriately and the driver currently sets the hardware up to
5175 * use it whenever needed.
5176 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005177static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07005178{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005179 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005180 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005181 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08005182 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005183
5184 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01005185 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005186
Zhu Yi12342c42007-12-20 11:27:32 +08005187 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
5188
Tomas Winklerfee12472008-04-03 16:05:21 -07005189 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005190 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005191 ret = -EIO;
5192 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005193 }
5194
Assaf Krauss1ea87392008-03-18 14:57:50 -07005195 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07005196 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08005197 IWL_DEBUG_MAC80211("leave - scanning\n");
5198 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005199 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08005200 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005201 }
5202
5203 spin_lock_irqsave(&priv->lock, flags);
5204
Assaf Krauss8622e702008-03-21 13:53:43 -07005205 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005206 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005207 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07005208 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5209 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005210 ret = -EINVAL;
5211 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005212 }
5213
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005214#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02005215 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07005216 * from any ht related info since 2.4 does not
5217 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02005218 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07005219#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5220 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
5221#endif
5222 )
5223 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005224#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005225
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005226 iwl_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005227 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005228
Johannes Berg8318d782008-01-24 19:38:38 +01005229 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07005230
5231 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01005232 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07005233 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005234 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005235
5236 spin_unlock_irqrestore(&priv->lock, flags);
5237
5238#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5239 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005240 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005241 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005242 }
5243#endif
5244
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005245 if (priv->cfg->ops->lib->radio_kill_sw)
5246 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07005247
5248 if (!conf->radio_enabled) {
5249 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005250 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005251 }
5252
Tomas Winklerfee12472008-04-03 16:05:21 -07005253 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005254 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005255 ret = -EIO;
5256 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005257 }
5258
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005259 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005260
5261 if (memcmp(&priv->active_rxon,
5262 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005264 else
5265 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
5266
5267 IWL_DEBUG_MAC80211("leave\n");
5268
Zhu Yia0646472007-12-20 14:10:01 +08005269out:
5270 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005271 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005272 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005273}
5274
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005275static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005276{
Tomas Winkler857485c2008-03-21 13:53:44 -07005277 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005278
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08005279 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005280 return;
5281
5282 /* The following should be done only at AP bring up */
5283 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
5284
5285 /* RXON - unassoc (to set timing command) */
5286 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005287 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005288
5289 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005290 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5291 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005292 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005293 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005294 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005295 IWL_WARNING("REPLY_RXON_TIMING failed - "
5296 "Attempting to continue.\n");
5297
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005298 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005299
5300 /* FIXME: what should be the assoc_id for AP? */
5301 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5302 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5303 priv->staging_rxon.flags |=
5304 RXON_FLG_SHORT_PREAMBLE_MSK;
5305 else
5306 priv->staging_rxon.flags &=
5307 ~RXON_FLG_SHORT_PREAMBLE_MSK;
5308
5309 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5310 if (priv->assoc_capability &
5311 WLAN_CAPABILITY_SHORT_SLOT_TIME)
5312 priv->staging_rxon.flags |=
5313 RXON_FLG_SHORT_SLOT_MSK;
5314 else
5315 priv->staging_rxon.flags &=
5316 ~RXON_FLG_SHORT_SLOT_MSK;
5317
5318 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5319 priv->staging_rxon.flags &=
5320 ~RXON_FLG_SHORT_SLOT_MSK;
5321 }
5322 /* restore RXON assoc */
5323 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005324 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005325 iwl4965_activate_qos(priv, 1);
Tomas Winkler57bd1be2008-05-15 13:54:03 +08005326 iwl4965_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08005327 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005328 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005329
5330 /* FIXME - we need to add code here to detect a totally new
5331 * configuration, reset the AP, unassoc, rxon timing, assoc,
5332 * clear sta table, add BCAST sta... */
5333}
5334
Johannes Berg32bfd352007-12-19 01:31:26 +01005335static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
5336 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07005337 struct ieee80211_if_conf *conf)
5338{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005339 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005340 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005341 unsigned long flags;
5342 int rc;
5343
5344 if (conf == NULL)
5345 return -EIO;
5346
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005347 if (priv->vif != vif) {
5348 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005349 return 0;
5350 }
5351
Zhu Yib481de92007-09-25 17:54:57 -07005352 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
5353 (!conf->beacon || !conf->ssid_len)) {
5354 IWL_DEBUG_MAC80211
5355 ("Leaving in AP mode because HostAPD is not ready.\n");
5356 return 0;
5357 }
5358
Tomas Winklerfee12472008-04-03 16:05:21 -07005359 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005360 return -EAGAIN;
5361
Zhu Yib481de92007-09-25 17:54:57 -07005362 mutex_lock(&priv->mutex);
5363
Zhu Yib481de92007-09-25 17:54:57 -07005364 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07005365 IWL_DEBUG_MAC80211("bssid: %s\n",
5366 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005367
Johannes Berg4150c572007-09-17 01:29:23 -04005368/*
5369 * very dubious code was here; the probe filtering flag is never set:
5370 *
Zhu Yib481de92007-09-25 17:54:57 -07005371 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
5372 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04005373 */
Zhu Yib481de92007-09-25 17:54:57 -07005374
5375 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5376 if (!conf->bssid) {
5377 conf->bssid = priv->mac_addr;
5378 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07005379 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
5380 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005381 }
5382 if (priv->ibss_beacon)
5383 dev_kfree_skb(priv->ibss_beacon);
5384
5385 priv->ibss_beacon = conf->beacon;
5386 }
5387
Tomas Winklerfee12472008-04-03 16:05:21 -07005388 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08005389 goto done;
5390
Zhu Yib481de92007-09-25 17:54:57 -07005391 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
5392 !is_multicast_ether_addr(conf->bssid)) {
5393 /* If there is currently a HW scan going on in the background
5394 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005395 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07005396 IWL_WARNING("Aborted scan still in progress "
5397 "after 100ms\n");
5398 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5399 mutex_unlock(&priv->mutex);
5400 return -EAGAIN;
5401 }
5402 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
5403
5404 /* TODO: Audit driver for usage of these members and see
5405 * if mac80211 deprecates them (priv->bssid looks like it
5406 * shouldn't be there, but I haven't scanned the IBSS code
5407 * to verify) - jpk */
5408 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5409
5410 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005411 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005412 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005413 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005414 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005415 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07005416 priv, priv->active_rxon.bssid_addr, 1);
5417 }
5418
5419 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005420 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07005421 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005422 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005423 }
5424
Mohamed Abbasfde35712007-11-29 11:10:15 +08005425 done:
Zhu Yib481de92007-09-25 17:54:57 -07005426 spin_lock_irqsave(&priv->lock, flags);
5427 if (!conf->ssid_len)
5428 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5429 else
5430 memcpy(priv->essid, conf->ssid, conf->ssid_len);
5431
5432 priv->essid_len = conf->ssid_len;
5433 spin_unlock_irqrestore(&priv->lock, flags);
5434
5435 IWL_DEBUG_MAC80211("leave\n");
5436 mutex_unlock(&priv->mutex);
5437
5438 return 0;
5439}
5440
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005441static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04005442 unsigned int changed_flags,
5443 unsigned int *total_flags,
5444 int mc_count, struct dev_addr_list *mc_list)
5445{
5446 /*
5447 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005448 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04005449 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08005450 struct iwl_priv *priv = hw->priv;
5451 int new_flags = 0;
5452 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5453 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5454 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
5455 IEEE80211_IF_TYPE_MNTR,
5456 changed_flags, *total_flags);
5457 /* queue work 'cuz mac80211 is holding a lock which
5458 * prevents us from issuing (synchronous) f/w cmds */
5459 queue_work(priv->workqueue, &priv->set_monitor);
5460 new_flags &= FIF_PROMISC_IN_BSS |
5461 FIF_OTHER_BSS |
5462 FIF_ALLMULTI;
5463 }
5464 }
5465 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04005466}
5467
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005468static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005469 struct ieee80211_if_init_conf *conf)
5470{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005471 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005472
5473 IWL_DEBUG_MAC80211("enter\n");
5474
5475 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005476
Tomas Winklerfee12472008-04-03 16:05:21 -07005477 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005478 iwl4965_scan_cancel_timeout(priv, 100);
5479 cancel_delayed_work(&priv->post_associate);
5480 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5481 iwl4965_commit_rxon(priv);
5482 }
Johannes Berg32bfd352007-12-19 01:31:26 +01005483 if (priv->vif == conf->vif) {
5484 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005485 memset(priv->bssid, 0, ETH_ALEN);
5486 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5487 priv->essid_len = 0;
5488 }
5489 mutex_unlock(&priv->mutex);
5490
5491 IWL_DEBUG_MAC80211("leave\n");
5492
5493}
Johannes Berg471b3ef2007-12-28 14:32:58 +01005494
Tomas Winkler3109ece2008-03-28 16:33:35 -07005495#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01005496static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
5497 struct ieee80211_vif *vif,
5498 struct ieee80211_bss_conf *bss_conf,
5499 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02005500{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005501 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02005502
Tomas Winkler3109ece2008-03-28 16:33:35 -07005503 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
5504
Johannes Berg471b3ef2007-12-28 14:32:58 +01005505 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005506 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
5507 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005508 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02005509 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5510 else
5511 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5512 }
5513
Johannes Berg471b3ef2007-12-28 14:32:58 +01005514 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005515 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01005516 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02005517 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
5518 else
5519 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
5520 }
5521
Tomas Winkler98952d52008-03-28 16:33:33 -07005522 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005523 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07005524 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005525 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07005526 }
5527
Johannes Berg471b3ef2007-12-28 14:32:58 +01005528 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005529 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005530 /* This should never happen as this function should
5531 * never be called from interrupt context. */
5532 if (WARN_ON_ONCE(in_interrupt()))
5533 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005534 if (bss_conf->assoc) {
5535 priv->assoc_id = bss_conf->aid;
5536 priv->beacon_int = bss_conf->beacon_int;
5537 priv->timestamp = bss_conf->timestamp;
5538 priv->assoc_capability = bss_conf->assoc_capability;
5539 priv->next_scan_jiffies = jiffies +
5540 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005541 mutex_lock(&priv->mutex);
5542 iwl4965_post_associate(priv);
5543 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005544 } else {
5545 priv->assoc_id = 0;
5546 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
5547 }
5548 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
5549 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07005550 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005551 }
5552
Tomas Winkler220173b2007-10-16 00:50:25 +02005553}
Zhu Yib481de92007-09-25 17:54:57 -07005554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005555static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07005556{
5557 int rc = 0;
5558 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005559 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005560
5561 IWL_DEBUG_MAC80211("enter\n");
5562
mabbas052c4b92007-10-25 17:15:43 +08005563 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005564 spin_lock_irqsave(&priv->lock, flags);
5565
Tomas Winklerfee12472008-04-03 16:05:21 -07005566 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005567 rc = -EIO;
5568 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
5569 goto out_unlock;
5570 }
5571
5572 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
5573 rc = -EIO;
5574 IWL_ERROR("ERROR: APs don't scan\n");
5575 goto out_unlock;
5576 }
5577
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005578 /* we don't schedule scan within next_scan_jiffies period */
5579 if (priv->next_scan_jiffies &&
5580 time_after(priv->next_scan_jiffies, jiffies)) {
5581 rc = -EAGAIN;
5582 goto out_unlock;
5583 }
Zhu Yib481de92007-09-25 17:54:57 -07005584 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005585 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
5586 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07005587 rc = -EAGAIN;
5588 goto out_unlock;
5589 }
5590 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005591 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005592 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07005593
5594 priv->one_direct_scan = 1;
5595 priv->direct_ssid_len = (u8)
5596 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
5597 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005598 } else
5599 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005600
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005601 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005602
5603 IWL_DEBUG_MAC80211("leave\n");
5604
5605out_unlock:
5606 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08005607 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005608
5609 return rc;
5610}
5611
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005612static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
5613 struct ieee80211_key_conf *keyconf, const u8 *addr,
5614 u32 iv32, u16 *phase1key)
5615{
5616 struct iwl_priv *priv = hw->priv;
5617 u8 sta_id = IWL_INVALID_STATION;
5618 unsigned long flags;
5619 __le16 key_flags = 0;
5620 int i;
5621 DECLARE_MAC_BUF(mac);
5622
5623 IWL_DEBUG_MAC80211("enter\n");
5624
Tomas Winkler947b13a2008-04-16 16:34:48 -07005625 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005626 if (sta_id == IWL_INVALID_STATION) {
5627 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5628 print_mac(mac, addr));
5629 return;
5630 }
5631
5632 iwl4965_scan_cancel_timeout(priv, 100);
5633
5634 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
5635 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
5636 key_flags &= ~STA_KEY_FLG_INVALID;
5637
Tomas Winkler5425e492008-04-15 16:01:38 -07005638 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005639 key_flags |= STA_KEY_MULTICAST_MSK;
5640
5641 spin_lock_irqsave(&priv->sta_lock, flags);
5642
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005643 priv->stations[sta_id].sta.key.key_flags = key_flags;
5644 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
5645
5646 for (i = 0; i < 5; i++)
5647 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
5648 cpu_to_le16(phase1key[i]);
5649
5650 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
5651 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
5652
Tomas Winkler133636d2008-05-05 10:22:34 +08005653 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005654
5655 spin_unlock_irqrestore(&priv->sta_lock, flags);
5656
5657 IWL_DEBUG_MAC80211("leave\n");
5658}
5659
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005660static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07005661 const u8 *local_addr, const u8 *addr,
5662 struct ieee80211_key_conf *key)
5663{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005664 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005665 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005666 int ret = 0;
5667 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005668 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005669
5670 IWL_DEBUG_MAC80211("enter\n");
5671
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07005672 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07005673 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
5674 return -EOPNOTSUPP;
5675 }
5676
5677 if (is_zero_ether_addr(addr))
5678 /* only support pairwise keys */
5679 return -EOPNOTSUPP;
5680
Tomas Winkler947b13a2008-04-16 16:34:48 -07005681 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005682 if (sta_id == IWL_INVALID_STATION) {
5683 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5684 print_mac(mac, addr));
5685 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005686
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005687 }
Zhu Yib481de92007-09-25 17:54:57 -07005688
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005689 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005690 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005691 mutex_unlock(&priv->mutex);
5692
5693 /* If we are getting WEP group key and we didn't receive any key mapping
5694 * so far, we are in legacy wep mode (group key only), otherwise we are
5695 * in 1X mode.
5696 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07005697 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005698 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
5699 if (cmd == SET_KEY)
5700 is_default_wep_key = !priv->key_mapping_key;
5701 else
5702 is_default_wep_key = priv->default_wep_key;
5703 }
mabbas052c4b92007-10-25 17:15:43 +08005704
Zhu Yib481de92007-09-25 17:54:57 -07005705 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005706 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005707 if (is_default_wep_key)
5708 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005709 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07005710 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005711
5712 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005713 break;
5714 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005715 if (is_default_wep_key)
5716 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005717 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07005718 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005719
5720 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005721 break;
5722 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005723 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005724 }
5725
5726 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005727
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005728 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005729}
5730
Johannes Berge100bb62008-04-30 18:51:21 +02005731static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07005732 const struct ieee80211_tx_queue_params *params)
5733{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005734 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005735 unsigned long flags;
5736 int q;
Zhu Yib481de92007-09-25 17:54:57 -07005737
5738 IWL_DEBUG_MAC80211("enter\n");
5739
Tomas Winklerfee12472008-04-03 16:05:21 -07005740 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005741 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5742 return -EIO;
5743 }
5744
5745 if (queue >= AC_NUM) {
5746 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5747 return 0;
5748 }
5749
Zhu Yib481de92007-09-25 17:54:57 -07005750 if (!priv->qos_data.qos_enable) {
5751 priv->qos_data.qos_active = 0;
5752 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
5753 return 0;
5754 }
5755 q = AC_NUM - 1 - queue;
5756
5757 spin_lock_irqsave(&priv->lock, flags);
5758
5759 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5760 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5761 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5762 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01005763 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07005764
5765 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
5766 priv->qos_data.qos_active = 1;
5767
5768 spin_unlock_irqrestore(&priv->lock, flags);
5769
5770 mutex_lock(&priv->mutex);
5771 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005772 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005773 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005774 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005775
5776 mutex_unlock(&priv->mutex);
5777
Zhu Yib481de92007-09-25 17:54:57 -07005778 IWL_DEBUG_MAC80211("leave\n");
5779 return 0;
5780}
5781
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005782static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005783 struct ieee80211_tx_queue_stats *stats)
5784{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005785 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005786 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08005787 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08005788 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07005789 unsigned long flags;
5790
5791 IWL_DEBUG_MAC80211("enter\n");
5792
Tomas Winklerfee12472008-04-03 16:05:21 -07005793 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005794 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5795 return -EIO;
5796 }
5797
5798 spin_lock_irqsave(&priv->lock, flags);
5799
5800 for (i = 0; i < AC_NUM; i++) {
5801 txq = &priv->txq[i];
5802 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08005803 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07005804
Johannes Berg57ffc582008-04-29 17:18:59 +02005805 stats[i].len = q->n_window - avail;
5806 stats[i].limit = q->n_window - q->high_mark;
5807 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07005808
5809 }
5810 spin_unlock_irqrestore(&priv->lock, flags);
5811
5812 IWL_DEBUG_MAC80211("leave\n");
5813
5814 return 0;
5815}
5816
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005817static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005818 struct ieee80211_low_level_stats *stats)
5819{
Ester Kummerbf403db2008-05-05 10:22:40 +08005820 struct iwl_priv *priv = hw->priv;
5821
5822 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005823 IWL_DEBUG_MAC80211("enter\n");
5824 IWL_DEBUG_MAC80211("leave\n");
5825
5826 return 0;
5827}
5828
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005829static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005830{
Ester Kummerbf403db2008-05-05 10:22:40 +08005831 struct iwl_priv *priv;
5832
5833 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005834 IWL_DEBUG_MAC80211("enter\n");
5835 IWL_DEBUG_MAC80211("leave\n");
5836
5837 return 0;
5838}
5839
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005840static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005841{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005842 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005843 unsigned long flags;
5844
5845 mutex_lock(&priv->mutex);
5846 IWL_DEBUG_MAC80211("enter\n");
5847
5848 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005849#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07005850 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005851 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07005852 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005853#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005854
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005855 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005856
5857 cancel_delayed_work(&priv->post_associate);
5858
5859 spin_lock_irqsave(&priv->lock, flags);
5860 priv->assoc_id = 0;
5861 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005862 priv->assoc_station_added = 0;
5863
5864 /* new association get rid of ibss beacon skb */
5865 if (priv->ibss_beacon)
5866 dev_kfree_skb(priv->ibss_beacon);
5867
5868 priv->ibss_beacon = NULL;
5869
5870 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005871 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005872 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
5873 priv->beacon_int = 0;
5874
5875 spin_unlock_irqrestore(&priv->lock, flags);
5876
Tomas Winklerfee12472008-04-03 16:05:21 -07005877 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005878 IWL_DEBUG_MAC80211("leave - not ready\n");
5879 mutex_unlock(&priv->mutex);
5880 return;
5881 }
5882
mabbas052c4b92007-10-25 17:15:43 +08005883 /* we are restarting association process
5884 * clear RXON_FILTER_ASSOC_MSK bit
5885 */
5886 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005887 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08005888 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005889 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08005890 }
5891
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005892 iwl_power_update_mode(priv, 0);
5893
Zhu Yib481de92007-09-25 17:54:57 -07005894 /* Per mac80211.h: This is only used in IBSS mode... */
5895 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08005896
Zhu Yib481de92007-09-25 17:54:57 -07005897 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
5898 mutex_unlock(&priv->mutex);
5899 return;
5900 }
5901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005902 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005903
5904 mutex_unlock(&priv->mutex);
5905
5906 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005907}
5908
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005909static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005910 struct ieee80211_tx_control *control)
5911{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005912 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005913 unsigned long flags;
5914
5915 mutex_lock(&priv->mutex);
5916 IWL_DEBUG_MAC80211("enter\n");
5917
Tomas Winklerfee12472008-04-03 16:05:21 -07005918 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005919 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5920 mutex_unlock(&priv->mutex);
5921 return -EIO;
5922 }
5923
5924 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
5925 IWL_DEBUG_MAC80211("leave - not IBSS\n");
5926 mutex_unlock(&priv->mutex);
5927 return -EIO;
5928 }
5929
5930 spin_lock_irqsave(&priv->lock, flags);
5931
5932 if (priv->ibss_beacon)
5933 dev_kfree_skb(priv->ibss_beacon);
5934
5935 priv->ibss_beacon = skb;
5936
5937 priv->assoc_id = 0;
5938
5939 IWL_DEBUG_MAC80211("leave\n");
5940 spin_unlock_irqrestore(&priv->lock, flags);
5941
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005942 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005943
5944 queue_work(priv->workqueue, &priv->post_associate.work);
5945
5946 mutex_unlock(&priv->mutex);
5947
5948 return 0;
5949}
5950
Zhu Yib481de92007-09-25 17:54:57 -07005951/*****************************************************************************
5952 *
5953 * sysfs attributes
5954 *
5955 *****************************************************************************/
5956
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005957#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07005958
5959/*
5960 * The following adds a new attribute to the sysfs representation
5961 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
5962 * used for controlling the debug level.
5963 *
5964 * See the level definitions in iwl for details.
5965 */
5966
Ester Kummer8cf769c2008-05-06 11:05:11 +08005967static ssize_t show_debug_level(struct device *d,
5968 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07005969{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005970 struct iwl_priv *priv = d->driver_data;
5971
5972 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07005973}
Ester Kummer8cf769c2008-05-06 11:05:11 +08005974static ssize_t store_debug_level(struct device *d,
5975 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07005976 const char *buf, size_t count)
5977{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005978 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005979 char *p = (char *)buf;
5980 u32 val;
5981
5982 val = simple_strtoul(p, &p, 0);
5983 if (p == buf)
5984 printk(KERN_INFO DRV_NAME
5985 ": %s is not in hex or decimal form.\n", buf);
5986 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08005987 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07005988
5989 return strnlen(buf, count);
5990}
5991
Ester Kummer8cf769c2008-05-06 11:05:11 +08005992static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5993 show_debug_level, store_debug_level);
5994
Zhu Yib481de92007-09-25 17:54:57 -07005995
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005996#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07005997
Zhu Yib481de92007-09-25 17:54:57 -07005998
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08005999static ssize_t show_version(struct device *d,
6000 struct device_attribute *attr, char *buf)
6001{
6002 struct iwl_priv *priv = d->driver_data;
6003 struct iwl4965_alive_resp *palive = &priv->card_alive;
6004
6005 if (palive->is_valid)
6006 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
6007 "fw type: 0x%01X 0x%01X\n",
6008 palive->ucode_major, palive->ucode_minor,
6009 palive->sw_rev[0], palive->sw_rev[1],
6010 palive->ver_type, palive->ver_subtype);
6011
6012 else
6013 return sprintf(buf, "fw not loaded\n");
6014}
6015
6016static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
6017
Zhu Yib481de92007-09-25 17:54:57 -07006018static ssize_t show_temperature(struct device *d,
6019 struct device_attribute *attr, char *buf)
6020{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006021 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006022
Tomas Winklerfee12472008-04-03 16:05:21 -07006023 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006024 return -EAGAIN;
6025
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006026 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006027}
6028
6029static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6030
6031static ssize_t show_rs_window(struct device *d,
6032 struct device_attribute *attr,
6033 char *buf)
6034{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006035 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006036 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006037}
6038static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
6039
6040static ssize_t show_tx_power(struct device *d,
6041 struct device_attribute *attr, char *buf)
6042{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006043 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006044 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6045}
6046
6047static ssize_t store_tx_power(struct device *d,
6048 struct device_attribute *attr,
6049 const char *buf, size_t count)
6050{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006051 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006052 char *p = (char *)buf;
6053 u32 val;
6054
6055 val = simple_strtoul(p, &p, 10);
6056 if (p == buf)
6057 printk(KERN_INFO DRV_NAME
6058 ": %s is not in decimal form.\n", buf);
6059 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006060 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006061
6062 return count;
6063}
6064
6065static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6066
6067static ssize_t show_flags(struct device *d,
6068 struct device_attribute *attr, char *buf)
6069{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006070 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006071
6072 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
6073}
6074
6075static ssize_t store_flags(struct device *d,
6076 struct device_attribute *attr,
6077 const char *buf, size_t count)
6078{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006079 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006080 u32 flags = simple_strtoul(buf, NULL, 0);
6081
6082 mutex_lock(&priv->mutex);
6083 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
6084 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006085 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006086 IWL_WARNING("Could not cancel scan.\n");
6087 else {
6088 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6089 flags);
6090 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006091 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006092 }
6093 }
6094 mutex_unlock(&priv->mutex);
6095
6096 return count;
6097}
6098
6099static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6100
6101static ssize_t show_filter_flags(struct device *d,
6102 struct device_attribute *attr, char *buf)
6103{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006104 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006105
6106 return sprintf(buf, "0x%04X\n",
6107 le32_to_cpu(priv->active_rxon.filter_flags));
6108}
6109
6110static ssize_t store_filter_flags(struct device *d,
6111 struct device_attribute *attr,
6112 const char *buf, size_t count)
6113{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006114 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006115 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6116
6117 mutex_lock(&priv->mutex);
6118 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
6119 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006120 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006121 IWL_WARNING("Could not cancel scan.\n");
6122 else {
6123 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6124 "0x%04X\n", filter_flags);
6125 priv->staging_rxon.filter_flags =
6126 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006127 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006128 }
6129 }
6130 mutex_unlock(&priv->mutex);
6131
6132 return count;
6133}
6134
6135static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6136 store_filter_flags);
6137
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006138#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006139
6140static ssize_t show_measurement(struct device *d,
6141 struct device_attribute *attr, char *buf)
6142{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006143 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006144 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006145 u32 size = sizeof(measure_report), len = 0, ofs = 0;
6146 u8 *data = (u8 *) & measure_report;
6147 unsigned long flags;
6148
6149 spin_lock_irqsave(&priv->lock, flags);
6150 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6151 spin_unlock_irqrestore(&priv->lock, flags);
6152 return 0;
6153 }
6154 memcpy(&measure_report, &priv->measure_report, size);
6155 priv->measurement_status = 0;
6156 spin_unlock_irqrestore(&priv->lock, flags);
6157
6158 while (size && (PAGE_SIZE - len)) {
6159 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6160 PAGE_SIZE - len, 1);
6161 len = strlen(buf);
6162 if (PAGE_SIZE - len)
6163 buf[len++] = '\n';
6164
6165 ofs += 16;
6166 size -= min(size, 16U);
6167 }
6168
6169 return len;
6170}
6171
6172static ssize_t store_measurement(struct device *d,
6173 struct device_attribute *attr,
6174 const char *buf, size_t count)
6175{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006176 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006177 struct ieee80211_measurement_params params = {
6178 .channel = le16_to_cpu(priv->active_rxon.channel),
6179 .start_time = cpu_to_le64(priv->last_tsf),
6180 .duration = cpu_to_le16(1),
6181 };
6182 u8 type = IWL_MEASURE_BASIC;
6183 u8 buffer[32];
6184 u8 channel;
6185
6186 if (count) {
6187 char *p = buffer;
6188 strncpy(buffer, buf, min(sizeof(buffer), count));
6189 channel = simple_strtoul(p, NULL, 0);
6190 if (channel)
6191 params.channel = channel;
6192
6193 p = buffer;
6194 while (*p && *p != ' ')
6195 p++;
6196 if (*p)
6197 type = simple_strtoul(p + 1, NULL, 0);
6198 }
6199
6200 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6201 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006202 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006203
6204 return count;
6205}
6206
6207static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6208 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006209#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006210
6211static ssize_t store_retry_rate(struct device *d,
6212 struct device_attribute *attr,
6213 const char *buf, size_t count)
6214{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006215 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006216
6217 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6218 if (priv->retry_rate <= 0)
6219 priv->retry_rate = 1;
6220
6221 return count;
6222}
6223
6224static ssize_t show_retry_rate(struct device *d,
6225 struct device_attribute *attr, char *buf)
6226{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006227 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006228 return sprintf(buf, "%d", priv->retry_rate);
6229}
6230
6231static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6232 store_retry_rate);
6233
6234static ssize_t store_power_level(struct device *d,
6235 struct device_attribute *attr,
6236 const char *buf, size_t count)
6237{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006238 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006239 int rc;
6240 int mode;
6241
6242 mode = simple_strtoul(buf, NULL, 0);
6243 mutex_lock(&priv->mutex);
6244
Tomas Winklerfee12472008-04-03 16:05:21 -07006245 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006246 rc = -EAGAIN;
6247 goto out;
6248 }
6249
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006250 rc = iwl_power_set_user_mode(priv, mode);
6251 if (rc) {
6252 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6253 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006254 }
Zhu Yib481de92007-09-25 17:54:57 -07006255 rc = count;
6256
6257 out:
6258 mutex_unlock(&priv->mutex);
6259 return rc;
6260}
6261
6262#define MAX_WX_STRING 80
6263
6264/* Values are in microsecond */
6265static const s32 timeout_duration[] = {
6266 350000,
6267 250000,
6268 75000,
6269 37000,
6270 25000,
6271};
6272static const s32 period_duration[] = {
6273 400000,
6274 700000,
6275 1000000,
6276 1000000,
6277 1000000
6278};
6279
6280static ssize_t show_power_level(struct device *d,
6281 struct device_attribute *attr, char *buf)
6282{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006283 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006284 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07006285 char *p = buf;
6286
6287 p += sprintf(p, "%d ", level);
6288 switch (level) {
6289 case IWL_POWER_MODE_CAM:
6290 case IWL_POWER_AC:
6291 p += sprintf(p, "(AC)");
6292 break;
6293 case IWL_POWER_BATTERY:
6294 p += sprintf(p, "(BATTERY)");
6295 break;
6296 default:
6297 p += sprintf(p,
6298 "(Timeout %dms, Period %dms)",
6299 timeout_duration[level - 1] / 1000,
6300 period_duration[level - 1] / 1000);
6301 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006302/*
Zhu Yib481de92007-09-25 17:54:57 -07006303 if (!(priv->power_mode & IWL_POWER_ENABLED))
6304 p += sprintf(p, " OFF\n");
6305 else
6306 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006307*/
6308 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07006309 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07006310}
6311
6312static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6313 store_power_level);
6314
6315static ssize_t show_channels(struct device *d,
6316 struct device_attribute *attr, char *buf)
6317{
Johannes Berg8318d782008-01-24 19:38:38 +01006318 /* all this shit doesn't belong into sysfs anyway */
6319 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006320}
6321
6322static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6323
6324static ssize_t show_statistics(struct device *d,
6325 struct device_attribute *attr, char *buf)
6326{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006327 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006328 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07006329 u32 len = 0, ofs = 0;
6330 u8 *data = (u8 *) & priv->statistics;
6331 int rc = 0;
6332
Tomas Winklerfee12472008-04-03 16:05:21 -07006333 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006334 return -EAGAIN;
6335
6336 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07006337 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006338 mutex_unlock(&priv->mutex);
6339
6340 if (rc) {
6341 len = sprintf(buf,
6342 "Error sending statistics request: 0x%08X\n", rc);
6343 return len;
6344 }
6345
6346 while (size && (PAGE_SIZE - len)) {
6347 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6348 PAGE_SIZE - len, 1);
6349 len = strlen(buf);
6350 if (PAGE_SIZE - len)
6351 buf[len++] = '\n';
6352
6353 ofs += 16;
6354 size -= min(size, 16U);
6355 }
6356
6357 return len;
6358}
6359
6360static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
6361
Zhu Yib481de92007-09-25 17:54:57 -07006362static ssize_t show_status(struct device *d,
6363 struct device_attribute *attr, char *buf)
6364{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006365 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07006366 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006367 return -EAGAIN;
6368 return sprintf(buf, "0x%08x\n", (int)priv->status);
6369}
6370
6371static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6372
6373static ssize_t dump_error_log(struct device *d,
6374 struct device_attribute *attr,
6375 const char *buf, size_t count)
6376{
6377 char *p = (char *)buf;
6378
6379 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006380 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006381
6382 return strnlen(buf, count);
6383}
6384
6385static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
6386
6387static ssize_t dump_event_log(struct device *d,
6388 struct device_attribute *attr,
6389 const char *buf, size_t count)
6390{
6391 char *p = (char *)buf;
6392
6393 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006394 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006395
6396 return strnlen(buf, count);
6397}
6398
6399static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
6400
6401/*****************************************************************************
6402 *
6403 * driver setup and teardown
6404 *
6405 *****************************************************************************/
6406
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006407static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006408{
6409 priv->workqueue = create_workqueue(DRV_NAME);
6410
6411 init_waitqueue_head(&priv->wait_command_queue);
6412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006413 INIT_WORK(&priv->up, iwl4965_bg_up);
6414 INIT_WORK(&priv->restart, iwl4965_bg_restart);
6415 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
6416 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
6417 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
6418 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
6419 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
6420 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08006421 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006422 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
6423 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
6424 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
6425 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07006426
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006427 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006428
6429 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006430 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07006431}
6432
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006433static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006434{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006435 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006436
Joonwoo Park3ae6a052007-11-29 10:43:16 +09006437 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07006438 cancel_delayed_work(&priv->scan_check);
6439 cancel_delayed_work(&priv->alive_start);
6440 cancel_delayed_work(&priv->post_associate);
6441 cancel_work_sync(&priv->beacon_update);
6442}
6443
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006444static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07006445 &dev_attr_channels.attr,
6446 &dev_attr_dump_errors.attr,
6447 &dev_attr_dump_events.attr,
6448 &dev_attr_flags.attr,
6449 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006450#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006451 &dev_attr_measurement.attr,
6452#endif
6453 &dev_attr_power_level.attr,
6454 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006455 &dev_attr_rs_window.attr,
6456 &dev_attr_statistics.attr,
6457 &dev_attr_status.attr,
6458 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006459 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08006460#ifdef CONFIG_IWLWIFI_DEBUG
6461 &dev_attr_debug_level.attr,
6462#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08006463 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006464
6465 NULL
6466};
6467
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006468static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07006469 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006470 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07006471};
6472
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006473static struct ieee80211_ops iwl4965_hw_ops = {
6474 .tx = iwl4965_mac_tx,
6475 .start = iwl4965_mac_start,
6476 .stop = iwl4965_mac_stop,
6477 .add_interface = iwl4965_mac_add_interface,
6478 .remove_interface = iwl4965_mac_remove_interface,
6479 .config = iwl4965_mac_config,
6480 .config_interface = iwl4965_mac_config_interface,
6481 .configure_filter = iwl4965_configure_filter,
6482 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006483 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006484 .get_stats = iwl4965_mac_get_stats,
6485 .get_tx_stats = iwl4965_mac_get_tx_stats,
6486 .conf_tx = iwl4965_mac_conf_tx,
6487 .get_tsf = iwl4965_mac_get_tsf,
6488 .reset_tsf = iwl4965_mac_reset_tsf,
6489 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01006490 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006491#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02006492 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006493#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006494 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07006495};
6496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006497static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07006498{
6499 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006500 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07006501 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08006502 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006503 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006504 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006505
Assaf Krauss316c30d2008-03-14 10:38:46 -07006506 /************************
6507 * 1. Allocating HW data
6508 ************************/
6509
Cahill, Ben M6440adb2007-11-29 11:09:55 +08006510 /* Disabling hardware scan means that mac80211 will perform scans
6511 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006512 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08006513 if (cfg->mod_params->debug & IWL_DL_INFO)
6514 dev_printk(KERN_DEBUG, &(pdev->dev),
6515 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006516 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006517 }
6518
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006519 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
6520 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07006521 err = -ENOMEM;
6522 goto out;
6523 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006524 priv = hw->priv;
6525 /* At this point both hw and priv are allocated. */
6526
Zhu Yib481de92007-09-25 17:54:57 -07006527 SET_IEEE80211_DEV(hw, &pdev->dev);
6528
6529 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08006530 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07006531 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006532
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006533#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08006534 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07006535 atomic_set(&priv->restrict_refcnt, 0);
6536#endif
Zhu Yib481de92007-09-25 17:54:57 -07006537
Assaf Krauss316c30d2008-03-14 10:38:46 -07006538 /**************************
6539 * 2. Initializing PCI bus
6540 **************************/
6541 if (pci_enable_device(pdev)) {
6542 err = -ENODEV;
6543 goto out_ieee80211_free_hw;
6544 }
6545
6546 pci_set_master(pdev);
6547
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006548 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006549 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006550 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
6551 if (err) {
6552 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6553 if (!err)
6554 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
6555 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006556 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006557 printk(KERN_WARNING "%s: No suitable DMA available.\n",
6558 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006559 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006560 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006561 }
6562
6563 err = pci_request_regions(pdev, DRV_NAME);
6564 if (err)
6565 goto out_pci_disable_device;
6566
6567 pci_set_drvdata(pdev, priv);
6568
6569 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6570 * PCI Tx retries from interfering with C3 CPU state */
6571 pci_write_config_byte(pdev, 0x41, 0x00);
6572
6573 /***********************
6574 * 3. Read REV register
6575 ***********************/
6576 priv->hw_base = pci_iomap(pdev, 0, 0);
6577 if (!priv->hw_base) {
6578 err = -ENODEV;
6579 goto out_pci_release_regions;
6580 }
6581
6582 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
6583 (unsigned long long) pci_resource_len(pdev, 0));
6584 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
6585
Tomas Winklerb661c812008-04-23 17:14:54 -07006586 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006587 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07006588 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
6589 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006590
Tomas Winkler91238712008-04-23 17:14:53 -07006591 /* amp init */
6592 err = priv->cfg->ops->lib->apm_ops.init(priv);
6593 if (err < 0) {
6594 IWL_DEBUG_INFO("Failed to init APMG\n");
6595 goto out_iounmap;
6596 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006597 /*****************
6598 * 4. Read EEPROM
6599 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07006600 /* Read the EEPROM */
6601 err = iwl_eeprom_init(priv);
6602 if (err) {
6603 IWL_ERROR("Unable to init EEPROM\n");
6604 goto out_iounmap;
6605 }
Tomas Winkler8614f362008-04-23 17:14:55 -07006606 err = iwl_eeprom_check_version(priv);
6607 if (err)
6608 goto out_iounmap;
6609
Ron Rindjunsky02883012008-05-15 13:53:53 +08006610 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006611 iwl_eeprom_get_mac(priv, priv->mac_addr);
6612 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
6613 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6614
6615 /************************
6616 * 5. Setup HW constants
6617 ************************/
6618 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07006619 if (priv->cfg->ops->lib->set_hw_params(priv)) {
6620 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07006621 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006622 }
6623
6624 /*******************
6625 * 6. Setup hw/priv
6626 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07006627
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006628 err = iwl_setup(priv);
6629 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006630 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006631 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006632
6633 /**********************************
6634 * 7. Initialize module parameters
6635 **********************************/
6636
6637 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006638 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07006639 set_bit(STATUS_RF_KILL_SW, &priv->status);
6640 IWL_DEBUG_INFO("Radio disabled.\n");
6641 }
6642
Assaf Krauss1ea87392008-03-18 14:57:50 -07006643 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07006644 priv->qos_data.qos_enable = 1;
6645
6646 /********************
6647 * 8. Setup services
6648 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006649 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006650 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006651 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006652
6653 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6654 if (err) {
6655 IWL_ERROR("failed to create sysfs device attributes\n");
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006656 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006657 }
6658
6659 err = iwl_dbgfs_register(priv, DRV_NAME);
6660 if (err) {
6661 IWL_ERROR("failed to create debugfs files\n");
6662 goto out_remove_sysfs;
6663 }
6664
6665 iwl4965_setup_deferred_work(priv);
6666 iwl4965_setup_rx_handlers(priv);
6667
6668 /********************
6669 * 9. Conclude
6670 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08006671 pci_save_state(pdev);
6672 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006673
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006674 /* notify iwlcore to init */
6675 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07006676 return 0;
6677
Assaf Krauss316c30d2008-03-14 10:38:46 -07006678 out_remove_sysfs:
6679 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006680 out_free_eeprom:
6681 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006682 out_iounmap:
6683 pci_iounmap(pdev, priv->hw_base);
6684 out_pci_release_regions:
6685 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006686 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07006687 out_pci_disable_device:
6688 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006689 out_ieee80211_free_hw:
6690 ieee80211_free_hw(priv->hw);
6691 out:
6692 return err;
6693}
6694
Reinette Chatrec83dbf62008-03-21 13:53:41 -07006695static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006696{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006697 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006698 struct list_head *p, *q;
6699 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006700 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07006701
6702 if (!priv)
6703 return;
6704
6705 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
6706
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07006707 if (priv->mac80211_registered) {
6708 ieee80211_unregister_hw(priv->hw);
6709 priv->mac80211_registered = 0;
6710 }
6711
Zhu Yib481de92007-09-25 17:54:57 -07006712 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08006713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006714 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006715
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006716 /* make sure we flush any pending irq or
6717 * tasklet for the driver
6718 */
6719 spin_lock_irqsave(&priv->lock, flags);
6720 iwl4965_disable_interrupts(priv);
6721 spin_unlock_irqrestore(&priv->lock, flags);
6722
6723 iwl_synchronize_irq(priv);
6724
Zhu Yib481de92007-09-25 17:54:57 -07006725 /* Free MAC hash list for ADHOC */
6726 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
6727 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
6728 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006729 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07006730 }
6731 }
6732
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006733 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07006734 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006735 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07006736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006737 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006738
6739 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08006740 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08006741 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006742
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006743 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006744 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006745
Zhu Yib481de92007-09-25 17:54:57 -07006746
Mohamed Abbas948c1712007-10-25 17:15:45 +08006747 /*netif_stop_queue(dev); */
6748 flush_workqueue(priv->workqueue);
6749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006750 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07006751 * priv->workqueue... so we can't take down the workqueue
6752 * until now... */
6753 destroy_workqueue(priv->workqueue);
6754 priv->workqueue = NULL;
6755
Zhu Yib481de92007-09-25 17:54:57 -07006756 pci_iounmap(pdev, priv->hw_base);
6757 pci_release_regions(pdev);
6758 pci_disable_device(pdev);
6759 pci_set_drvdata(pdev, NULL);
6760
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006761 iwl_free_channel_map(priv);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006762 iwlcore_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006763
6764 if (priv->ibss_beacon)
6765 dev_kfree_skb(priv->ibss_beacon);
6766
6767 ieee80211_free_hw(priv->hw);
6768}
6769
6770#ifdef CONFIG_PM
6771
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006772static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07006773{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006774 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006775
Zhu Yie655b9f2008-01-24 02:19:38 -08006776 if (priv->is_open) {
6777 set_bit(STATUS_IN_SUSPEND, &priv->status);
6778 iwl4965_mac_stop(priv->hw);
6779 priv->is_open = 1;
6780 }
Zhu Yib481de92007-09-25 17:54:57 -07006781
Zhu Yib481de92007-09-25 17:54:57 -07006782 pci_set_power_state(pdev, PCI_D3hot);
6783
Zhu Yib481de92007-09-25 17:54:57 -07006784 return 0;
6785}
6786
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006787static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006788{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006789 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006790
Zhu Yib481de92007-09-25 17:54:57 -07006791 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07006792
Zhu Yie655b9f2008-01-24 02:19:38 -08006793 if (priv->is_open)
6794 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006795
Zhu Yie655b9f2008-01-24 02:19:38 -08006796 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006797 return 0;
6798}
6799
6800#endif /* CONFIG_PM */
6801
6802/*****************************************************************************
6803 *
6804 * driver and module entry point
6805 *
6806 *****************************************************************************/
6807
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006808/* Hardware specific file defines the PCI IDs table for that hardware module */
6809static struct pci_device_id iwl_hw_card_ids[] = {
6810 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
6811 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07006812#ifdef CONFIG_IWL5000
6813 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
6814 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
6815 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
6816#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006817 {0}
6818};
6819MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
6820
6821static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07006822 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006823 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006824 .probe = iwl4965_pci_probe,
6825 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07006826#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006827 .suspend = iwl4965_pci_suspend,
6828 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07006829#endif
6830};
6831
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006832static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07006833{
6834
6835 int ret;
6836 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
6837 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006838
6839 ret = iwl4965_rate_control_register();
6840 if (ret) {
6841 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
6842 return ret;
6843 }
6844
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006845 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07006846 if (ret) {
6847 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006848 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07006849 }
Zhu Yib481de92007-09-25 17:54:57 -07006850
6851 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006852
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006853error_register:
6854 iwl4965_rate_control_unregister();
6855 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006856}
6857
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006858static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07006859{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006860 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006861 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07006862}
6863
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006864module_exit(iwl4965_exit);
6865module_init(iwl4965_init);