blob: 45b6b5234eefdff8d86eb5f9e4367824e462f43e [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 Winklerc79dd5b2008-03-12 16:58:50 -070056static int iwl4965_tx_queue_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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800210const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700211
212/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800213 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700214 * the functionality provided here
215 */
216
217/**************************************************************/
218
Ian Schram01ebd062007-10-25 17:15:22 +0800219#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800220/**
221 * iwl4965_remove_station - Remove driver's knowledge of station.
222 *
223 * NOTE: This does not remove station from device's station table.
224 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700225static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700226{
227 int index = IWL_INVALID_STATION;
228 int i;
229 unsigned long flags;
230
231 spin_lock_irqsave(&priv->sta_lock, flags);
232
233 if (is_ap)
234 index = IWL_AP_ID;
235 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700236 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700237 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700238 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Zhu Yib481de92007-09-25 17:54:57 -0700239 if (priv->stations[i].used &&
240 !compare_ether_addr(priv->stations[i].sta.sta.addr,
241 addr)) {
242 index = i;
243 break;
244 }
245
246 if (unlikely(index == IWL_INVALID_STATION))
247 goto out;
248
249 if (priv->stations[index].used) {
250 priv->stations[index].used = 0;
251 priv->num_stations--;
252 }
253
254 BUG_ON(priv->num_stations < 0);
255
256out:
257 spin_unlock_irqrestore(&priv->sta_lock, flags);
258 return 0;
259}
Zhu Yi556f8db2007-09-27 11:27:33 +0800260#endif
Zhu Yib481de92007-09-25 17:54:57 -0700261
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800262/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800263 * iwl4965_add_station_flags - Add station to tables in driver and device
264 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700265u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200266 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700267{
268 int i;
269 int index = IWL_INVALID_STATION;
Tomas Winkler6def9762008-05-05 10:22:31 +0800270 struct iwl_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700271 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700272 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700273
274 spin_lock_irqsave(&priv->sta_lock, flags_spin);
275 if (is_ap)
276 index = IWL_AP_ID;
277 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700278 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700279 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700280 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Zhu Yib481de92007-09-25 17:54:57 -0700281 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
282 addr)) {
283 index = i;
284 break;
285 }
286
287 if (!priv->stations[i].used &&
288 index == IWL_INVALID_STATION)
289 index = i;
290 }
291
292
Ben Cahill9fbab512007-11-29 11:09:47 +0800293 /* These two conditions have the same outcome, but keep them separate
294 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700295 if (unlikely(index == IWL_INVALID_STATION)) {
296 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
297 return index;
298 }
299
300 if (priv->stations[index].used &&
301 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
302 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
303 return index;
304 }
305
306
Joe Perches0795af52007-10-03 17:59:30 -0700307 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700308 station = &priv->stations[index];
309 station->used = 1;
310 priv->num_stations++;
311
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800312 /* Set up the REPLY_ADD_STA command to send to device */
Tomas Winkler133636d2008-05-05 10:22:34 +0800313 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700314 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
315 station->sta.mode = 0;
316 station->sta.sta.sta_id = index;
317 station->sta.station_flags = 0;
318
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800319#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700320 /* BCAST station and IBSS stations do not work in HT mode */
Tomas Winkler5425e492008-04-15 16:01:38 -0700321 if (index != priv->hw_params.bcast_sta_id &&
Zhu Yib481de92007-09-25 17:54:57 -0700322 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200323 iwl4965_set_ht_add_station(priv, index,
324 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800325#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700326
327 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800328
329 /* Add station to device's station table */
Tomas Winkler133636d2008-05-05 10:22:34 +0800330 iwl_send_add_sta(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700331 return index;
332
333}
334
Zhu Yib481de92007-09-25 17:54:57 -0700335
Zhu Yib481de92007-09-25 17:54:57 -0700336
337/*************** HOST COMMAND QUEUE FUNCTIONS *****/
338
Zhu Yib481de92007-09-25 17:54:57 -0700339/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800340 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700341 * @priv: device private data point
342 * @cmd: a point to the ucode command structure
343 *
344 * The function returns < 0 values to indicate the operation is
345 * failed. On success, it turns the index (> 0) of command in the
346 * command queue.
347 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700348int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700349{
Ron Rindjunsky16466902008-05-05 10:22:50 +0800350 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Tomas Winkler443cfd42008-05-15 13:53:57 +0800351 struct iwl_queue *q = &txq->q;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800352 struct iwl_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700353 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700354 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700355 u32 idx;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800356 u16 fix_size;
Zhu Yib481de92007-09-25 17:54:57 -0700357 dma_addr_t phys_addr;
358 int ret;
359 unsigned long flags;
360
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800361 cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
362 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
363
Zhu Yib481de92007-09-25 17:54:57 -0700364 /* If any of the command structures end up being larger than
365 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
366 * we will need to increase the size of the TFD entries */
367 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
368 !(cmd->meta.flags & CMD_SIZE_HUGE));
369
Tomas Winklerfee12472008-04-03 16:05:21 -0700370 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800371 IWL_DEBUG_INFO("Not sending command - RF KILL");
372 return -EIO;
373 }
374
Tomas Winkler443cfd42008-05-15 13:53:57 +0800375 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700376 IWL_ERROR("No space for Tx\n");
377 return -ENOSPC;
378 }
379
380 spin_lock_irqsave(&priv->hcmd_lock, flags);
381
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800382 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700383 memset(tfd, 0, sizeof(*tfd));
384
385 control_flags = (u32 *) tfd;
386
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800387 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700388 out_cmd = &txq->cmd[idx];
389
390 out_cmd->hdr.cmd = cmd->id;
391 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
392 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
393
394 /* At this point, the out_cmd now has all of the incoming cmd
395 * information */
396
397 out_cmd->hdr.flags = 0;
398 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800399 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700400 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
401 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
402
403 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700404 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800405 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700406
407 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
408 "%d bytes at %d[%d]:%d\n",
409 get_cmd_string(out_cmd->hdr.cmd),
410 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800411 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700412
413 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800414
415 /* Set up entry in queue's byte count circular buffer */
Tomas Winklere2a722e2008-04-14 21:16:10 -0700416 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800417
418 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800419 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklere2a722e2008-04-14 21:16:10 -0700420 ret = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700421
422 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
423 return ret ? ret : idx;
424}
425
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700426static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
427{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800428 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700429
430 if (hw_decrypt)
431 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
432 else
433 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
434
435}
436
Zhu Yib481de92007-09-25 17:54:57 -0700437/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800438 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700439 *
440 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800441 * NOTE: mutex must be held before calling this fnction
442 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700443static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700444 const u8 *addr, int is_ap)
445{
Zhu Yi556f8db2007-09-27 11:27:33 +0800446 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700447
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800448 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200449#ifdef CONFIG_IWL4965_HT
450 struct ieee80211_conf *conf = &priv->hw->conf;
451 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
452
453 if ((is_ap) &&
454 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
455 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
456 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
457 0, cur_ht_config);
458 else
459#endif /* CONFIG_IWL4965_HT */
460 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
461 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800462
463 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700464 iwl4965_add_station(priv, addr, is_ap);
465
Zhu Yi556f8db2007-09-27 11:27:33 +0800466 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700467}
468
469/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800470 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700471 *
472 * NOTE: This is really only useful during development and can eventually
473 * be #ifdef'd out once the driver is stable and folks aren't actively
474 * making changes
475 */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800476static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700477{
478 int error = 0;
479 int counter = 1;
480
481 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
482 error |= le32_to_cpu(rxon->flags &
483 (RXON_FLG_TGJ_NARROW_BAND_MSK |
484 RXON_FLG_RADAR_DETECT_MSK));
485 if (error)
486 IWL_WARNING("check 24G fields %d | %d\n",
487 counter++, error);
488 } else {
489 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
490 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
491 if (error)
492 IWL_WARNING("check 52 fields %d | %d\n",
493 counter++, error);
494 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
495 if (error)
496 IWL_WARNING("check 52 CCK %d | %d\n",
497 counter++, error);
498 }
499 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
500 if (error)
501 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
502
503 /* make sure basic rates 6Mbps and 1Mbps are supported */
504 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
505 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
506 if (error)
507 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
508
509 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
510 if (error)
511 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
512
513 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
514 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
515 if (error)
516 IWL_WARNING("check CCK and short slot %d | %d\n",
517 counter++, error);
518
519 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
520 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
521 if (error)
522 IWL_WARNING("check CCK & auto detect %d | %d\n",
523 counter++, error);
524
525 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
526 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
527 if (error)
528 IWL_WARNING("check TGG and auto detect %d | %d\n",
529 counter++, error);
530
531 if (error)
532 IWL_WARNING("Tuning to channel %d\n",
533 le16_to_cpu(rxon->channel));
534
535 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800536 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700537 return -1;
538 }
539 return 0;
540}
541
542/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800543 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800544 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700545 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800546 * If the RXON structure is changing enough to require a new tune,
547 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
548 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700549 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700550static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700551{
552
553 /* These items are only settable from the full RXON command */
554 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
555 compare_ether_addr(priv->staging_rxon.bssid_addr,
556 priv->active_rxon.bssid_addr) ||
557 compare_ether_addr(priv->staging_rxon.node_addr,
558 priv->active_rxon.node_addr) ||
559 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
560 priv->active_rxon.wlap_bssid_addr) ||
561 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
562 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
563 (priv->staging_rxon.air_propagation !=
564 priv->active_rxon.air_propagation) ||
565 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
566 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
567 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
568 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
569 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
570 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
571 return 1;
572
573 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
574 * be updated with the RXON_ASSOC command -- however only some
575 * flag transitions are allowed using RXON_ASSOC */
576
577 /* Check if we are not switching bands */
578 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
579 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
580 return 1;
581
582 /* Check if we are switching association toggle */
583 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
584 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
585 return 1;
586
587 return 0;
588}
589
Zhu Yib481de92007-09-25 17:54:57 -0700590/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800591 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700592 *
Ian Schram01ebd062007-10-25 17:15:22 +0800593 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700594 * the active_rxon structure is updated with the new data. This
595 * function correctly transitions out of the RXON_ASSOC_MSK state if
596 * a HW tune is required based on the RXON structure changes.
597 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700598static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700599{
600 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800601 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700602 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700603 int rc = 0;
604
Tomas Winklerfee12472008-04-03 16:05:21 -0700605 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700606 return -1;
607
608 /* always get timestamp with Rx frame */
609 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800611 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700612 if (rc) {
613 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
614 return -EINVAL;
615 }
616
617 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800618 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700619 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800620 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700621 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700622 if (rc) {
623 IWL_ERROR("Error setting RXON_ASSOC "
624 "configuration (%d).\n", rc);
625 return rc;
626 }
627
628 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
629
630 return 0;
631 }
632
633 /* station table will be cleared */
634 priv->assoc_station_added = 0;
635
Zhu Yib481de92007-09-25 17:54:57 -0700636 /* If we are currently associated and the new config requires
637 * an RXON_ASSOC and the new config wants the associated mask enabled,
638 * we must clear the associated from the active configuration
639 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700640 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700641 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
642 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
643 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
644
Tomas Winkler857485c2008-03-21 13:53:44 -0700645 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800646 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700647 &priv->active_rxon);
648
649 /* If the mask clearing failed then we set
650 * active_rxon back to what it was previously */
651 if (rc) {
652 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
653 IWL_ERROR("Error clearing ASSOC_MSK on current "
654 "configuration (%d).\n", rc);
655 return rc;
656 }
Zhu Yib481de92007-09-25 17:54:57 -0700657 }
658
659 IWL_DEBUG_INFO("Sending RXON\n"
660 "* with%s RXON_FILTER_ASSOC_MSK\n"
661 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700662 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700663 ((priv->staging_rxon.filter_flags &
664 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
665 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700666 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700667
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700668 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700669 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700670 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800671 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700672 if (rc) {
673 IWL_ERROR("Error setting new configuration (%d).\n", rc);
674 return rc;
675 }
676
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700677 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800678
Zhu Yib481de92007-09-25 17:54:57 -0700679 if (!priv->error_recovering)
680 priv->start_calib = 0;
681
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700682 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700683
684 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
685
686 /* If we issue a new RXON command which required a tune then we must
687 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800688 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700689 if (rc) {
690 IWL_ERROR("Error setting Tx power (%d).\n", rc);
691 return rc;
692 }
693
694 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800695 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700696 IWL_INVALID_STATION) {
697 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
698 return -EIO;
699 }
700
701 /* If we have set the ASSOC_MSK and we are in BSS mode then
702 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700703 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700704 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800705 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700706 == IWL_INVALID_STATION) {
707 IWL_ERROR("Error adding AP address for transmit.\n");
708 return -EIO;
709 }
710 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700711 if (priv->default_wep_key &&
712 iwl_send_static_wepkey_cmd(priv, 0))
713 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700714 }
715
716 return 0;
717}
718
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700719void iwl4965_update_chain_flags(struct iwl_priv *priv)
720{
721
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700722 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700723 iwl4965_commit_rxon(priv);
724}
725
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700726static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700727{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800728 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700729 .flags = 3,
730 .lead_time = 0xAA,
731 .max_kill = 1,
732 .kill_ack_mask = 0,
733 .kill_cts_mask = 0,
734 };
735
Tomas Winkler857485c2008-03-21 13:53:44 -0700736 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800737 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700738}
739
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700740static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700741{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800742 int ret = 0;
743 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700744 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700745 .id = REPLY_SCAN_ABORT_CMD,
746 .meta.flags = CMD_WANT_SKB,
747 };
748
749 /* If there isn't a scan actively going on in the hardware
750 * then we are in between scan bands and not actually
751 * actively scanning, so don't send the abort command */
752 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
753 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
754 return 0;
755 }
756
Tomas Winklerdb11d632008-05-05 10:22:33 +0800757 ret = iwl_send_cmd_sync(priv, &cmd);
758 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700759 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800760 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700761 }
762
Tomas Winklerdb11d632008-05-05 10:22:33 +0800763 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700764 if (res->u.status != CAN_ABORT_STATUS) {
765 /* The scan abort will return 1 for success or
766 * 2 for "failure". A failure condition can be
767 * due to simply not being in an active scan which
768 * can occur if we send the scan abort before we
769 * the microcode has notified us that a scan is
770 * completed. */
771 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
772 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
773 clear_bit(STATUS_SCAN_HW, &priv->status);
774 }
775
776 dev_kfree_skb_any(cmd.meta.u.skb);
777
Tomas Winklerdb11d632008-05-05 10:22:33 +0800778 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700779}
780
Zhu Yib481de92007-09-25 17:54:57 -0700781/*
782 * CARD_STATE_CMD
783 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800784 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700785 *
786 * When in the 'enable' state the card operates as normal.
787 * When in the 'disable' state, the card enters into a low power mode.
788 * When in the 'halt' state, the card is shut down and must be fully
789 * restarted to come back on.
790 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700791static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700792{
Tomas Winkler857485c2008-03-21 13:53:44 -0700793 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700794 .id = REPLY_CARD_STATE_CMD,
795 .len = sizeof(u32),
796 .data = &flags,
797 .meta.flags = meta_flag,
798 };
799
Tomas Winkler857485c2008-03-21 13:53:44 -0700800 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700801}
802
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700803static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700804{
805 struct list_head *element;
806
807 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
808 priv->frames_count);
809
810 while (!list_empty(&priv->free_frames)) {
811 element = priv->free_frames.next;
812 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800813 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700814 priv->frames_count--;
815 }
816
817 if (priv->frames_count) {
818 IWL_WARNING("%d frames still in use. Did we lose one?\n",
819 priv->frames_count);
820 priv->frames_count = 0;
821 }
822}
823
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700824static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700825{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800826 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700827 struct list_head *element;
828 if (list_empty(&priv->free_frames)) {
829 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
830 if (!frame) {
831 IWL_ERROR("Could not allocate frame!\n");
832 return NULL;
833 }
834
835 priv->frames_count++;
836 return frame;
837 }
838
839 element = priv->free_frames.next;
840 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800841 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700842}
843
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700844static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700845{
846 memset(frame, 0, sizeof(*frame));
847 list_add(&frame->list, &priv->free_frames);
848}
849
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700850unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700851 struct ieee80211_hdr *hdr,
852 const u8 *dest, int left)
853{
854
Tomas Winkler3109ece2008-03-28 16:33:35 -0700855 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700856 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
857 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
858 return 0;
859
860 if (priv->ibss_beacon->len > left)
861 return 0;
862
863 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
864
865 return priv->ibss_beacon->len;
866}
867
Guy Cohen39e88502008-04-23 17:14:57 -0700868static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700869{
Guy Cohen39e88502008-04-23 17:14:57 -0700870 int i;
871 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700872
Guy Cohen39e88502008-04-23 17:14:57 -0700873 /* Set rate mask*/
874 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
875 rate_mask = priv->active_rate_basic & 0xF;
876 else
877 rate_mask = priv->active_rate_basic & 0xFF0;
878
879 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700880 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Guy Cohen39e88502008-04-23 17:14:57 -0700881 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700882 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800883 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700884 }
885
Guy Cohen39e88502008-04-23 17:14:57 -0700886 /* No valid rate was found. Assign the lowest one */
887 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
888 return IWL_RATE_1M_PLCP;
889 else
890 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700891}
892
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700893static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700894{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800895 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700896 unsigned int frame_size;
897 int rc;
898 u8 rate;
899
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800900 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700901
902 if (!frame) {
903 IWL_ERROR("Could not obtain free frame buffer for beacon "
904 "command.\n");
905 return -ENOMEM;
906 }
907
Guy Cohen39e88502008-04-23 17:14:57 -0700908 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700909
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800910 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700911
Tomas Winkler857485c2008-03-21 13:53:44 -0700912 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700913 &frame->u.cmd[0]);
914
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800915 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700916
917 return rc;
918}
919
920/******************************************************************************
921 *
Zhu Yib481de92007-09-25 17:54:57 -0700922 * Misc. internal state and helper functions
923 *
924 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700925
Zhu Yib481de92007-09-25 17:54:57 -0700926/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800927 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -0700928 *
929 * return : set the bit for each supported rate insert in ie
930 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800931static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +0200932 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -0700933{
934 u16 ret_rates = 0, bit;
935 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200936 u8 *cnt = ie;
937 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700938
939 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
940 if (bit & supported_rate) {
941 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800942 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +0200943 ((bit & basic_rate) ? 0x80 : 0x00);
944 (*cnt)++;
945 (*left)--;
946 if ((*left <= 0) ||
947 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -0700948 break;
949 }
950 }
951
952 return ret_rates;
953}
954
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700955#ifdef CONFIG_IWL4965_HT
956static void iwl4965_ht_conf(struct iwl_priv *priv,
957 struct ieee80211_bss_conf *bss_conf)
958{
959 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
960 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
961 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
962
963 IWL_DEBUG_MAC80211("enter: \n");
964
965 iwl_conf->is_ht = bss_conf->assoc_ht;
966
967 if (!iwl_conf->is_ht)
968 return;
969
970 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
971
972 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
973 iwl_conf->sgf |= 0x1;
974 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
975 iwl_conf->sgf |= 0x2;
976
977 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
978 iwl_conf->max_amsdu_size =
979 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
980
981 iwl_conf->supported_chan_width =
982 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
983 iwl_conf->extension_chan_offset =
984 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
985 /* If no above or below channel supplied disable FAT channel */
986 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
987 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
988 iwl_conf->supported_chan_width = 0;
989
990 iwl_conf->tx_mimo_ps_mode =
991 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
992 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
993
994 iwl_conf->control_channel = ht_bss_conf->primary_channel;
995 iwl_conf->tx_chan_width =
996 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
997 iwl_conf->ht_protection =
998 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
999 iwl_conf->non_GF_STA_present =
1000 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
1001
1002 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
1003 IWL_DEBUG_MAC80211("leave\n");
1004}
1005
1006static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1007 u8 *pos, int *left)
1008{
1009 struct ieee80211_ht_cap *ht_cap;
1010
1011 if (!sband || !sband->ht_info.ht_supported)
1012 return;
1013
1014 if (*left < sizeof(struct ieee80211_ht_cap))
1015 return;
1016
1017 *pos++ = sizeof(struct ieee80211_ht_cap);
1018 ht_cap = (struct ieee80211_ht_cap *) pos;
1019
1020 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1021 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1022 ht_cap->ampdu_params_info =
1023 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
1024 ((sband->ht_info.ampdu_density << 2) &
1025 IEEE80211_HT_CAP_AMPDU_DENSITY);
1026 *left -= sizeof(struct ieee80211_ht_cap);
1027}
1028#else
1029static inline void iwl4965_ht_conf(struct iwl_priv *priv,
1030 struct ieee80211_bss_conf *bss_conf)
1031{
1032}
1033static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1034 u8 *pos, int *left)
1035{
1036}
1037#endif
1038
1039
Zhu Yib481de92007-09-25 17:54:57 -07001040/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001041 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001042 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001043static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001044 enum ieee80211_band band,
1045 struct ieee80211_mgmt *frame,
1046 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001047{
1048 int len = 0;
1049 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001050 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +02001051 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001052 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07001053
1054 /* Make sure there is enough space for the probe request,
1055 * two mandatory IEs and the data */
1056 left -= 24;
1057 if (left < 0)
1058 return 0;
1059 len += 24;
1060
1061 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001062 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001063 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001064 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001065 frame->seq_ctrl = 0;
1066
1067 /* fill in our indirect SSID IE */
1068 /* ...next IE... */
1069
1070 left -= 2;
1071 if (left < 0)
1072 return 0;
1073 len += 2;
1074 pos = &(frame->u.probe_req.variable[0]);
1075 *pos++ = WLAN_EID_SSID;
1076 *pos++ = 0;
1077
1078 /* fill in our direct SSID IE... */
1079 if (is_direct) {
1080 /* ...next IE... */
1081 left -= 2 + priv->essid_len;
1082 if (left < 0)
1083 return 0;
1084 /* ... fill it in... */
1085 *pos++ = WLAN_EID_SSID;
1086 *pos++ = priv->essid_len;
1087 memcpy(pos, priv->essid, priv->essid_len);
1088 pos += priv->essid_len;
1089 len += 2 + priv->essid_len;
1090 }
1091
1092 /* fill in supported rate */
1093 /* ...next IE... */
1094 left -= 2;
1095 if (left < 0)
1096 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001097
Zhu Yib481de92007-09-25 17:54:57 -07001098 /* ... fill it in... */
1099 *pos++ = WLAN_EID_SUPP_RATES;
1100 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001101
mabbasbee488d2007-10-25 17:15:42 +08001102 /* exclude 60M rate */
1103 active_rates = priv->rates_mask;
1104 active_rates &= ~IWL_RATE_60M_MASK;
1105
1106 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001107
Tomas Winklerc7c46672007-10-18 02:04:15 +02001108 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001109 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001110 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001111 active_rates &= ~ret_rates;
1112
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001113 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001114 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001115 active_rates &= ~ret_rates;
1116
Zhu Yib481de92007-09-25 17:54:57 -07001117 len += 2 + *pos;
1118 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001119 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001120 goto fill_end;
1121
1122 /* fill in supported extended rate */
1123 /* ...next IE... */
1124 left -= 2;
1125 if (left < 0)
1126 return 0;
1127 /* ... fill it in... */
1128 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1129 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001130 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001131 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001132 if (*pos > 0)
1133 len += 2 + *pos;
1134
Zhu Yib481de92007-09-25 17:54:57 -07001135 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001136 /* fill in HT IE */
1137 left -= 2;
1138 if (left < 0)
1139 return 0;
1140
1141 *pos++ = WLAN_EID_HT_CAPABILITY;
1142 *pos = 0;
1143
1144 iwl_ht_cap_to_ie(sband, pos, &left);
1145
1146 if (*pos > 0)
1147 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -07001148 return (u16)len;
1149}
1150
1151/*
1152 * QoS support
1153*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001154static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001155 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001156{
1157
Tomas Winkler857485c2008-03-21 13:53:44 -07001158 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001159 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001160}
1161
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001162static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001163{
1164 unsigned long flags;
1165
Zhu Yib481de92007-09-25 17:54:57 -07001166 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1167 return;
1168
1169 if (!priv->qos_data.qos_enable)
1170 return;
1171
1172 spin_lock_irqsave(&priv->lock, flags);
1173 priv->qos_data.def_qos_parm.qos_flags = 0;
1174
1175 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1176 !priv->qos_data.qos_cap.q_AP.txop_request)
1177 priv->qos_data.def_qos_parm.qos_flags |=
1178 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001179 if (priv->qos_data.qos_active)
1180 priv->qos_data.def_qos_parm.qos_flags |=
1181 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1182
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001183#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001184 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001185 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001186#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001187
Zhu Yib481de92007-09-25 17:54:57 -07001188 spin_unlock_irqrestore(&priv->lock, flags);
1189
Tomas Winkler3109ece2008-03-28 16:33:35 -07001190 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001191 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1192 priv->qos_data.qos_active,
1193 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001194
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001195 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001196 &(priv->qos_data.def_qos_parm));
1197 }
1198}
1199
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001200int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001201{
1202 /* Filter incoming packets to determine if they are targeted toward
1203 * this network, discarding packets coming from ourselves */
1204 switch (priv->iw_mode) {
1205 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1206 /* packets from our adapter are dropped (echo) */
1207 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1208 return 0;
1209 /* {broad,multi}cast packets to our IBSS go through */
1210 if (is_multicast_ether_addr(header->addr1))
1211 return !compare_ether_addr(header->addr3, priv->bssid);
1212 /* packets to our adapter go through */
1213 return !compare_ether_addr(header->addr1, priv->mac_addr);
1214 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1215 /* packets from our adapter are dropped (echo) */
1216 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1217 return 0;
1218 /* {broad,multi}cast packets to our BSS go through */
1219 if (is_multicast_ether_addr(header->addr1))
1220 return !compare_ether_addr(header->addr2, priv->bssid);
1221 /* packets to our adapter go through */
1222 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001223 default:
1224 break;
Zhu Yib481de92007-09-25 17:54:57 -07001225 }
1226
1227 return 1;
1228}
1229
1230#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1231
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001232static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001233{
1234 switch (status & TX_STATUS_MSK) {
1235 case TX_STATUS_SUCCESS:
1236 return "SUCCESS";
1237 TX_STATUS_ENTRY(SHORT_LIMIT);
1238 TX_STATUS_ENTRY(LONG_LIMIT);
1239 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1240 TX_STATUS_ENTRY(MGMNT_ABORT);
1241 TX_STATUS_ENTRY(NEXT_FRAG);
1242 TX_STATUS_ENTRY(LIFE_EXPIRE);
1243 TX_STATUS_ENTRY(DEST_PS);
1244 TX_STATUS_ENTRY(ABORTED);
1245 TX_STATUS_ENTRY(BT_RETRY);
1246 TX_STATUS_ENTRY(STA_INVALID);
1247 TX_STATUS_ENTRY(FRAG_DROPPED);
1248 TX_STATUS_ENTRY(TID_DISABLE);
1249 TX_STATUS_ENTRY(FRAME_FLUSHED);
1250 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1251 TX_STATUS_ENTRY(TX_LOCKED);
1252 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1253 }
1254
1255 return "UNKNOWN";
1256}
1257
1258/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001259 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001260 *
1261 * NOTE: priv->mutex is not required before calling this function
1262 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001263static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001264{
1265 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1266 clear_bit(STATUS_SCANNING, &priv->status);
1267 return 0;
1268 }
1269
1270 if (test_bit(STATUS_SCANNING, &priv->status)) {
1271 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1272 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1273 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1274 queue_work(priv->workqueue, &priv->abort_scan);
1275
1276 } else
1277 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1278
1279 return test_bit(STATUS_SCANNING, &priv->status);
1280 }
1281
1282 return 0;
1283}
1284
1285/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001286 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001287 * @ms: amount of time to wait (in milliseconds) for scan to abort
1288 *
1289 * NOTE: priv->mutex must be held before calling this function
1290 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001291static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001292{
1293 unsigned long now = jiffies;
1294 int ret;
1295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001296 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001297 if (ret && ms) {
1298 mutex_unlock(&priv->mutex);
1299 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1300 test_bit(STATUS_SCANNING, &priv->status))
1301 msleep(1);
1302 mutex_lock(&priv->mutex);
1303
1304 return test_bit(STATUS_SCANNING, &priv->status);
1305 }
1306
1307 return ret;
1308}
1309
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001310static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001311{
1312 /* Reset ieee stats */
1313
1314 /* We don't reset the net_device_stats (ieee->stats) on
1315 * re-association */
1316
1317 priv->last_seq_num = -1;
1318 priv->last_frag_num = -1;
1319 priv->last_packet_time = 0;
1320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001322}
1323
1324#define MAX_UCODE_BEACON_INTERVAL 4096
1325#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001327static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001328{
1329 u16 new_val = 0;
1330 u16 beacon_factor = 0;
1331
1332 beacon_factor =
1333 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1334 / MAX_UCODE_BEACON_INTERVAL;
1335 new_val = beacon_val / beacon_factor;
1336
1337 return cpu_to_le16(new_val);
1338}
1339
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001340static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001341{
1342 u64 interval_tm_unit;
1343 u64 tsf, result;
1344 unsigned long flags;
1345 struct ieee80211_conf *conf = NULL;
1346 u16 beacon_int = 0;
1347
1348 conf = ieee80211_get_hw_conf(priv->hw);
1349
1350 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001351 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1352 priv->rxon_timing.timestamp.dw[0] =
1353 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001354
1355 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1356
Tomas Winkler3109ece2008-03-28 16:33:35 -07001357 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001358
1359 beacon_int = priv->beacon_int;
1360 spin_unlock_irqrestore(&priv->lock, flags);
1361
1362 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1363 if (beacon_int == 0) {
1364 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1365 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1366 } else {
1367 priv->rxon_timing.beacon_interval =
1368 cpu_to_le16(beacon_int);
1369 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001370 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001371 le16_to_cpu(priv->rxon_timing.beacon_interval));
1372 }
1373
1374 priv->rxon_timing.atim_window = 0;
1375 } else {
1376 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001377 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001378 /* TODO: we need to get atim_window from upper stack
1379 * for now we set to 0 */
1380 priv->rxon_timing.atim_window = 0;
1381 }
1382
1383 interval_tm_unit =
1384 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1385 result = do_div(tsf, interval_tm_unit);
1386 priv->rxon_timing.beacon_init_val =
1387 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1388
1389 IWL_DEBUG_ASSOC
1390 ("beacon interval %d beacon timer %d beacon tim %d\n",
1391 le16_to_cpu(priv->rxon_timing.beacon_interval),
1392 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1393 le16_to_cpu(priv->rxon_timing.atim_window));
1394}
1395
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001396static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001397{
1398 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1399 IWL_ERROR("APs don't scan.\n");
1400 return 0;
1401 }
1402
Tomas Winklerfee12472008-04-03 16:05:21 -07001403 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001404 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1405 return -EIO;
1406 }
1407
1408 if (test_bit(STATUS_SCANNING, &priv->status)) {
1409 IWL_DEBUG_SCAN("Scan already in progress.\n");
1410 return -EAGAIN;
1411 }
1412
1413 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1414 IWL_DEBUG_SCAN("Scan request while abort pending. "
1415 "Queuing.\n");
1416 return -EAGAIN;
1417 }
1418
1419 IWL_DEBUG_INFO("Starting scan...\n");
1420 priv->scan_bands = 2;
1421 set_bit(STATUS_SCANNING, &priv->status);
1422 priv->scan_start = jiffies;
1423 priv->scan_pass_start = priv->scan_start;
1424
1425 queue_work(priv->workqueue, &priv->request_scan);
1426
1427 return 0;
1428}
1429
Zhu Yib481de92007-09-25 17:54:57 -07001430
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001431static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001432 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001433{
Johannes Berg8318d782008-01-24 19:38:38 +01001434 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001435 priv->staging_rxon.flags &=
1436 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1437 | RXON_FLG_CCK_MSK);
1438 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1439 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001440 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001441 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1442 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1443 else
1444 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1445
1446 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1447 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1448
1449 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1450 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1451 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1452 }
1453}
1454
1455/*
Ian Schram01ebd062007-10-25 17:15:22 +08001456 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001457 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001458static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001459{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001460 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001461
1462 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1463
1464 switch (priv->iw_mode) {
1465 case IEEE80211_IF_TYPE_AP:
1466 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1467 break;
1468
1469 case IEEE80211_IF_TYPE_STA:
1470 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1471 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1472 break;
1473
1474 case IEEE80211_IF_TYPE_IBSS:
1475 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1476 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1477 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1478 RXON_FILTER_ACCEPT_GRP_MSK;
1479 break;
1480
1481 case IEEE80211_IF_TYPE_MNTR:
1482 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1483 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1484 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1485 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001486 default:
1487 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1488 break;
Zhu Yib481de92007-09-25 17:54:57 -07001489 }
1490
1491#if 0
1492 /* TODO: Figure out when short_preamble would be set and cache from
1493 * that */
1494 if (!hw_to_local(priv->hw)->short_preamble)
1495 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1496 else
1497 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1498#endif
1499
Assaf Krauss8622e702008-03-21 13:53:43 -07001500 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001501 le16_to_cpu(priv->staging_rxon.channel));
1502
1503 if (!ch_info)
1504 ch_info = &priv->channel_info[0];
1505
1506 /*
1507 * in some case A channels are all non IBSS
1508 * in this case force B/G channel
1509 */
1510 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1511 !(is_channel_ibss(ch_info)))
1512 ch_info = &priv->channel_info[0];
1513
1514 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001515 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001516
Johannes Berg8318d782008-01-24 19:38:38 +01001517 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001518
1519 priv->staging_rxon.ofdm_basic_rates =
1520 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1521 priv->staging_rxon.cck_basic_rates =
1522 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1523
1524 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1525 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1526 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1527 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1528 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1529 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001530 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001531}
1532
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001533static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001534{
Zhu Yib481de92007-09-25 17:54:57 -07001535 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001536 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001537
Assaf Krauss8622e702008-03-21 13:53:43 -07001538 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001539 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001540 le16_to_cpu(priv->staging_rxon.channel));
1541
1542 if (!ch_info || !is_channel_ibss(ch_info)) {
1543 IWL_ERROR("channel %d not IBSS channel\n",
1544 le16_to_cpu(priv->staging_rxon.channel));
1545 return -EINVAL;
1546 }
1547 }
1548
Zhu Yib481de92007-09-25 17:54:57 -07001549 priv->iw_mode = mode;
1550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001551 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001552 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1553
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001554 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001555
Mohamed Abbasfde35712007-11-29 11:10:15 +08001556 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001557 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001558 return -EAGAIN;
1559
1560 cancel_delayed_work(&priv->scan_check);
1561 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1562 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1563 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1564 return -EAGAIN;
1565 }
1566
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001567 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001568
1569 return 0;
1570}
1571
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001572static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001573 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001574 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001575 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001576 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001577{
Tomas Winkler6def9762008-05-05 10:22:31 +08001578 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001579 struct iwl_wep_key *wepkey;
1580 int keyidx = 0;
1581
Ivo van Doorn1c014422008-04-17 19:41:02 +02001582 BUG_ON(ctl->hw_key->hw_key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001583
1584 switch (keyinfo->alg) {
1585 case ALG_CCMP:
1586 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1587 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001588 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1589 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001590 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1591 break;
1592
1593 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001594 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001595 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1596 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1597 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001598 break;
1599
1600 case ALG_WEP:
Ivo van Doorn1c014422008-04-17 19:41:02 +02001601 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001602 cmd->cmd.tx.sec_ctl = 0;
1603 if (priv->default_wep_key) {
1604 /* the WEP key was sent as static */
Ivo van Doorn1c014422008-04-17 19:41:02 +02001605 keyidx = ctl->hw_key->hw_key_idx;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001606 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1607 wepkey->key_size);
1608 if (wepkey->key_size == WEP_KEY_LEN_128)
1609 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1610 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001611 /* the WEP key was sent as dynamic */
1612 keyidx = keyinfo->keyidx;
1613 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1614 keyinfo->keylen);
1615 if (keyinfo->keylen == WEP_KEY_LEN_128)
1616 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001617 }
Zhu Yib481de92007-09-25 17:54:57 -07001618
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001619 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1620 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001621
1622 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001623 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001624 break;
1625
Zhu Yib481de92007-09-25 17:54:57 -07001626 default:
1627 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1628 break;
1629 }
1630}
1631
1632/*
1633 * handle build REPLY_TX command notification.
1634 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001635static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001636 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001637 struct ieee80211_tx_control *ctrl,
1638 struct ieee80211_hdr *hdr,
1639 int is_unicast, u8 std_id)
1640{
1641 __le16 *qc;
1642 u16 fc = le16_to_cpu(hdr->frame_control);
1643 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1644
1645 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1646 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1647 tx_flags |= TX_CMD_FLG_ACK_MSK;
1648 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1649 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1650 if (ieee80211_is_probe_response(fc) &&
1651 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1652 tx_flags |= TX_CMD_FLG_TSF_MSK;
1653 } else {
1654 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1655 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1656 }
1657
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001658 if (ieee80211_is_back_request(fc))
1659 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1660
1661
Zhu Yib481de92007-09-25 17:54:57 -07001662 cmd->cmd.tx.sta_id = std_id;
1663 if (ieee80211_get_morefrag(hdr))
1664 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1665
1666 qc = ieee80211_get_qos_ctrl(hdr);
1667 if (qc) {
1668 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
1669 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1670 } else
1671 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1672
1673 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1674 tx_flags |= TX_CMD_FLG_RTS_MSK;
1675 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1676 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1677 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1678 tx_flags |= TX_CMD_FLG_CTS_MSK;
1679 }
1680
1681 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1682 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1683
1684 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1685 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1686 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1687 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08001688 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001689 else
Ian Schrambc434dd2007-10-25 17:15:29 +08001690 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001691 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001692 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001693 }
Zhu Yib481de92007-09-25 17:54:57 -07001694
1695 cmd->cmd.tx.driver_txop = 0;
1696 cmd->cmd.tx.tx_flags = tx_flags;
1697 cmd->cmd.tx.next_frame_len = 0;
1698}
Tomas Winkler19758be2008-03-12 16:58:51 -07001699static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1700{
1701 /* 0 - mgmt, 1 - cnt, 2 - data */
1702 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1703 priv->tx_stats[idx].cnt++;
1704 priv->tx_stats[idx].bytes += len;
1705}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001706/**
1707 * iwl4965_get_sta_id - Find station's index within station table
1708 *
1709 * If new IBSS station, create new entry in station table
1710 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001711static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08001712 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001713{
1714 int sta_id;
1715 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07001716 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001717
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001718 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001719 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1720 is_multicast_ether_addr(hdr->addr1))
Tomas Winkler5425e492008-04-15 16:01:38 -07001721 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001722
1723 switch (priv->iw_mode) {
1724
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001725 /* If we are a client station in a BSS network, use the special
1726 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07001727 case IEEE80211_IF_TYPE_STA:
1728 return IWL_AP_ID;
1729
1730 /* If we are an AP, then find the station, or use BCAST */
1731 case IEEE80211_IF_TYPE_AP:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001732 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001733 if (sta_id != IWL_INVALID_STATION)
1734 return sta_id;
Tomas Winkler5425e492008-04-15 16:01:38 -07001735 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001736
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001737 /* If this frame is going out to an IBSS network, find the station,
1738 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07001739 case IEEE80211_IF_TYPE_IBSS:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001740 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001741 if (sta_id != IWL_INVALID_STATION)
1742 return sta_id;
1743
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001744 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02001745 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
1746 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07001747
1748 if (sta_id != IWL_INVALID_STATION)
1749 return sta_id;
1750
Joe Perches0795af52007-10-03 17:59:30 -07001751 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001752 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07001753 print_mac(mac, hdr->addr1));
Ester Kummerbf403db2008-05-05 10:22:40 +08001754 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler5425e492008-04-15 16:01:38 -07001755 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001756
1757 default:
Ian Schram01ebd062007-10-25 17:15:22 +08001758 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Tomas Winkler5425e492008-04-15 16:01:38 -07001759 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001760 }
1761}
1762
1763/*
1764 * start REPLY_TX command process
1765 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001766static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001767 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1768{
1769 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001770 struct iwl_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07001771 u32 *control_flags;
1772 int txq_id = ctl->queue;
Ron Rindjunsky16466902008-05-05 10:22:50 +08001773 struct iwl_tx_queue *txq = NULL;
Tomas Winkler443cfd42008-05-15 13:53:57 +08001774 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001775 dma_addr_t phys_addr;
1776 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001777 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07001778 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001779 u16 len, idx, len_org;
1780 u8 id, hdr_len, unicast;
1781 u8 sta_id;
1782 u16 seq_number = 0;
1783 u16 fc;
1784 __le16 *qc;
1785 u8 wait_write_ptr = 0;
1786 unsigned long flags;
1787 int rc;
1788
1789 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07001790 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001791 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1792 goto drop_unlock;
1793 }
1794
Johannes Berg32bfd352007-12-19 01:31:26 +01001795 if (!priv->vif) {
1796 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07001797 goto drop_unlock;
1798 }
1799
Johannes Berg8318d782008-01-24 19:38:38 +01001800 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07001801 IWL_ERROR("ERROR: No TX rate available.\n");
1802 goto drop_unlock;
1803 }
1804
1805 unicast = !is_multicast_ether_addr(hdr->addr1);
1806 id = 0;
1807
1808 fc = le16_to_cpu(hdr->frame_control);
1809
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001810#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001811 if (ieee80211_is_auth(fc))
1812 IWL_DEBUG_TX("Sending AUTH frame\n");
1813 else if (ieee80211_is_assoc_request(fc))
1814 IWL_DEBUG_TX("Sending ASSOC frame\n");
1815 else if (ieee80211_is_reassoc_request(fc))
1816 IWL_DEBUG_TX("Sending REASSOC frame\n");
1817#endif
1818
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001819 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08001820 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07001821 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08001822 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08001823 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07001824 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07001825 goto drop_unlock;
1826 }
1827
1828 spin_unlock_irqrestore(&priv->lock, flags);
1829
1830 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001831
1832 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001833 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001834 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07001835 DECLARE_MAC_BUF(mac);
1836
1837 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
1838 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07001839 goto drop;
1840 }
1841
Guy Cohen07bc28e2008-04-23 17:15:03 -07001842 IWL_DEBUG_TX("station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001843
1844 qc = ieee80211_get_qos_ctrl(hdr);
1845 if (qc) {
1846 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1847 seq_number = priv->stations[sta_id].tid[tid].seq_number &
1848 IEEE80211_SCTL_SEQ;
1849 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1850 (hdr->seq_ctrl &
1851 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
1852 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001853#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001854 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001855 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07001856 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001857 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001858#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001859 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001860
1861 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07001862 txq = &priv->txq[txq_id];
1863 q = &txq->q;
1864
1865 spin_lock_irqsave(&priv->lock, flags);
1866
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001867 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001868 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07001869 memset(tfd, 0, sizeof(*tfd));
1870 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001871 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001872
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001873 /* Set up driver data for this TFD */
Tomas Winkler8567c632008-05-15 13:53:58 +08001874 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001875 txq->txb[q->write_ptr].skb[0] = skb;
1876 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07001877 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001878
1879 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07001880 out_cmd = &txq->cmd[idx];
1881 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1882 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001883
1884 /*
1885 * Set up the Tx-command (not MAC!) header.
1886 * Store the chosen Tx queue and TFD index within the sequence field;
1887 * after Tx, uCode's Tx response will return this value so driver can
1888 * locate the frame within the tx queue and do post-tx processing.
1889 */
Zhu Yib481de92007-09-25 17:54:57 -07001890 out_cmd->hdr.cmd = REPLY_TX;
1891 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001892 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001893
1894 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001895 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
1896
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001897 /*
1898 * Use the first empty entry in this queue's command buffer array
1899 * to contain the Tx command and MAC header concatenated together
1900 * (payload data will be in another buffer).
1901 * Size of this varies, due to varying MAC header length.
1902 * If end is not dword aligned, we'll have 2 extra bytes at the end
1903 * of the MAC header (device reads on dword boundaries).
1904 * We'll tell device about this padding later.
1905 */
Tomas Winkler5425e492008-04-15 16:01:38 -07001906 len = priv->hw_params.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07001907 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07001908
1909 len_org = len;
1910 len = (len + 3) & ~3;
1911
1912 if (len_org != len)
1913 len_org = 1;
1914 else
1915 len_org = 0;
1916
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001917 /* Physical address of this Tx command's header (not MAC header!),
1918 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07001919 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
1920 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001921
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001922 /* Add buffer containing Tx command and MAC(!) header to TFD's
1923 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001924 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07001925
1926 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001927 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001928
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001929 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1930 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07001931 len = skb->len - hdr_len;
1932 if (len) {
1933 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
1934 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001935 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07001936 }
1937
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001938 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07001939 if (len_org)
1940 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1941
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001942 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07001943 len = (u16)skb->len;
1944 out_cmd->cmd.tx.len = cpu_to_le16(len);
1945
1946 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001947 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001948
1949 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001950 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001951
Tomas Winkler19758be2008-03-12 16:58:51 -07001952 iwl_update_tx_stats(priv, fc, len);
1953
Tomas Winkler857485c2008-03-21 13:53:44 -07001954 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001955 offsetof(struct iwl4965_tx_cmd, scratch);
1956 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
1957 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
1958
Zhu Yib481de92007-09-25 17:54:57 -07001959 if (!ieee80211_get_morefrag(hdr)) {
1960 txq->need_update = 1;
1961 if (qc) {
1962 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1963 priv->stations[sta_id].tid[tid].seq_number = seq_number;
1964 }
1965 } else {
1966 wait_write_ptr = 1;
1967 txq->need_update = 0;
1968 }
1969
Ester Kummerbf403db2008-05-05 10:22:40 +08001970 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07001971 sizeof(out_cmd->cmd.tx));
1972
Ester Kummerbf403db2008-05-05 10:22:40 +08001973 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07001974 ieee80211_get_hdrlen(fc));
1975
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001976 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07001977 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07001978
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001979 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08001980 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001981 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001982 spin_unlock_irqrestore(&priv->lock, flags);
1983
1984 if (rc)
1985 return rc;
1986
Tomas Winkler443cfd42008-05-15 13:53:57 +08001987 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07001988 && priv->mac80211_registered) {
1989 if (wait_write_ptr) {
1990 spin_lock_irqsave(&priv->lock, flags);
1991 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001992 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001993 spin_unlock_irqrestore(&priv->lock, flags);
1994 }
1995
1996 ieee80211_stop_queue(priv->hw, ctl->queue);
1997 }
1998
1999 return 0;
2000
2001drop_unlock:
2002 spin_unlock_irqrestore(&priv->lock, flags);
2003drop:
2004 return -1;
2005}
2006
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002007static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002008{
Johannes Berg8318d782008-01-24 19:38:38 +01002009 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002010 struct ieee80211_rate *rate;
2011 int i;
2012
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07002013 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002014 if (!hw) {
2015 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2016 return;
2017 }
Zhu Yib481de92007-09-25 17:54:57 -07002018
2019 priv->active_rate = 0;
2020 priv->active_rate_basic = 0;
2021
Johannes Berg8318d782008-01-24 19:38:38 +01002022 for (i = 0; i < hw->n_bitrates; i++) {
2023 rate = &(hw->bitrates[i]);
2024 if (rate->hw_value < IWL_RATE_COUNT)
2025 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002026 }
2027
2028 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2029 priv->active_rate, priv->active_rate_basic);
2030
2031 /*
2032 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2033 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2034 * OFDM
2035 */
2036 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2037 priv->staging_rxon.cck_basic_rates =
2038 ((priv->active_rate_basic &
2039 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2040 else
2041 priv->staging_rxon.cck_basic_rates =
2042 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2043
2044 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2045 priv->staging_rxon.ofdm_basic_rates =
2046 ((priv->active_rate_basic &
2047 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2048 IWL_FIRST_OFDM_RATE) & 0xFF;
2049 else
2050 priv->staging_rxon.ofdm_basic_rates =
2051 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2052}
2053
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002054void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002055{
2056 unsigned long flags;
2057
2058 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2059 return;
2060
2061 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2062 disable_radio ? "OFF" : "ON");
2063
2064 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002065 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002066 /* FIXME: This is a workaround for AP */
2067 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2068 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002069 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002070 CSR_UCODE_SW_BIT_RFKILL);
2071 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002072 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07002073 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2074 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002075 iwl4965_send_card_state(priv,
2076 CARD_STATE_CMD_DISABLE,
2077 0);
Zhu Yib481de92007-09-25 17:54:57 -07002078 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002079
2080 /* make sure mac80211 stop sending Tx frame */
2081 if (priv->mac80211_registered)
2082 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002083 }
2084 return;
2085 }
2086
2087 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002088 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002089
2090 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2091 spin_unlock_irqrestore(&priv->lock, flags);
2092
2093 /* wake up ucode */
2094 msleep(10);
2095
2096 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002097 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2098 if (!iwl_grab_nic_access(priv))
2099 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002100 spin_unlock_irqrestore(&priv->lock, flags);
2101
2102 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2103 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2104 "disabled by HW switch\n");
2105 return;
2106 }
2107
2108 queue_work(priv->workqueue, &priv->restart);
2109 return;
2110}
2111
Zhu Yib481de92007-09-25 17:54:57 -07002112#define IWL_PACKET_RETRY_TIME HZ
2113
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002114int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002115{
2116 u16 sc = le16_to_cpu(header->seq_ctrl);
2117 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2118 u16 frag = sc & IEEE80211_SCTL_FRAG;
2119 u16 *last_seq, *last_frag;
2120 unsigned long *last_time;
2121
2122 switch (priv->iw_mode) {
2123 case IEEE80211_IF_TYPE_IBSS:{
2124 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002125 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002126 u8 *mac = header->addr2;
2127 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2128
2129 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002130 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002131 if (!compare_ether_addr(entry->mac, mac))
2132 break;
2133 }
2134 if (p == &priv->ibss_mac_hash[index]) {
2135 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2136 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002137 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002138 return 0;
2139 }
2140 memcpy(entry->mac, mac, ETH_ALEN);
2141 entry->seq_num = seq;
2142 entry->frag_num = frag;
2143 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002144 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002145 return 0;
2146 }
2147 last_seq = &entry->seq_num;
2148 last_frag = &entry->frag_num;
2149 last_time = &entry->packet_time;
2150 break;
2151 }
2152 case IEEE80211_IF_TYPE_STA:
2153 last_seq = &priv->last_seq_num;
2154 last_frag = &priv->last_frag_num;
2155 last_time = &priv->last_packet_time;
2156 break;
2157 default:
2158 return 0;
2159 }
2160 if ((*last_seq == seq) &&
2161 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2162 if (*last_frag == frag)
2163 goto drop;
2164 if (*last_frag + 1 != frag)
2165 /* out-of-order fragment */
2166 goto drop;
2167 } else
2168 *last_seq = seq;
2169
2170 *last_frag = frag;
2171 *last_time = jiffies;
2172 return 0;
2173
2174 drop:
2175 return 1;
2176}
2177
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002178#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002179
2180#include "iwl-spectrum.h"
2181
2182#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2183#define BEACON_TIME_MASK_HIGH 0xFF000000
2184#define TIME_UNIT 1024
2185
2186/*
2187 * extended beacon time format
2188 * time in usec will be changed into a 32-bit value in 8:24 format
2189 * the high 1 byte is the beacon counts
2190 * the lower 3 bytes is the time in usec within one beacon interval
2191 */
2192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002193static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002194{
2195 u32 quot;
2196 u32 rem;
2197 u32 interval = beacon_interval * 1024;
2198
2199 if (!interval || !usec)
2200 return 0;
2201
2202 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2203 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2204
2205 return (quot << 24) + rem;
2206}
2207
2208/* base is usually what we get from ucode with each received frame,
2209 * the same as HW timer counter counting down
2210 */
2211
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002212static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002213{
2214 u32 base_low = base & BEACON_TIME_MASK_LOW;
2215 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2216 u32 interval = beacon_interval * TIME_UNIT;
2217 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2218 (addon & BEACON_TIME_MASK_HIGH);
2219
2220 if (base_low > addon_low)
2221 res += base_low - addon_low;
2222 else if (base_low < addon_low) {
2223 res += interval + base_low - addon_low;
2224 res += (1 << 24);
2225 } else
2226 res += (1 << 24);
2227
2228 return cpu_to_le32(res);
2229}
2230
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002231static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002232 struct ieee80211_measurement_params *params,
2233 u8 type)
2234{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002235 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08002236 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002237 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002238 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2239 .data = (void *)&spectrum,
2240 .meta.flags = CMD_WANT_SKB,
2241 };
2242 u32 add_time = le64_to_cpu(params->start_time);
2243 int rc;
2244 int spectrum_resp_status;
2245 int duration = le16_to_cpu(params->duration);
2246
Tomas Winkler3109ece2008-03-28 16:33:35 -07002247 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002248 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002249 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002250 le64_to_cpu(params->start_time) - priv->last_tsf,
2251 le16_to_cpu(priv->rxon_timing.beacon_interval));
2252
2253 memset(&spectrum, 0, sizeof(spectrum));
2254
2255 spectrum.channel_count = cpu_to_le16(1);
2256 spectrum.flags =
2257 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2258 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2259 cmd.len = sizeof(spectrum);
2260 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2261
Tomas Winkler3109ece2008-03-28 16:33:35 -07002262 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002263 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002264 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002265 add_time,
2266 le16_to_cpu(priv->rxon_timing.beacon_interval));
2267 else
2268 spectrum.start_time = 0;
2269
2270 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2271 spectrum.channels[0].channel = params->channel;
2272 spectrum.channels[0].type = type;
2273 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2274 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2275 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2276
Tomas Winkler857485c2008-03-21 13:53:44 -07002277 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002278 if (rc)
2279 return rc;
2280
Tomas Winklerdb11d632008-05-05 10:22:33 +08002281 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002282 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2283 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2284 rc = -EIO;
2285 }
2286
2287 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2288 switch (spectrum_resp_status) {
2289 case 0: /* Command will be handled */
2290 if (res->u.spectrum.id != 0xff) {
2291 IWL_DEBUG_INFO
2292 ("Replaced existing measurement: %d\n",
2293 res->u.spectrum.id);
2294 priv->measurement_status &= ~MEASUREMENT_READY;
2295 }
2296 priv->measurement_status |= MEASUREMENT_ACTIVE;
2297 rc = 0;
2298 break;
2299
2300 case 1: /* Command will not be handled */
2301 rc = -EAGAIN;
2302 break;
2303 }
2304
2305 dev_kfree_skb_any(cmd.meta.u.skb);
2306
2307 return rc;
2308}
2309#endif
2310
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002311static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Tomas Winkler8567c632008-05-15 13:53:58 +08002312 struct iwl_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002313{
2314
2315 tx_sta->status.ack_signal = 0;
2316 tx_sta->status.excessive_retries = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002317
2318 if (in_interrupt())
2319 ieee80211_tx_status_irqsafe(priv->hw,
2320 tx_sta->skb[0], &(tx_sta->status));
2321 else
2322 ieee80211_tx_status(priv->hw,
2323 tx_sta->skb[0], &(tx_sta->status));
2324
2325 tx_sta->skb[0] = NULL;
2326}
2327
2328/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002329 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002330 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002331 * When FW advances 'R' index, all entries between old and new 'R' index
2332 * need to be reclaimed. As result, some free space forms. If there is
2333 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002334 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002335int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002336{
Ron Rindjunsky16466902008-05-05 10:22:50 +08002337 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Tomas Winkler443cfd42008-05-15 13:53:57 +08002338 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002339 int nfreed = 0;
2340
Tomas Winkler443cfd42008-05-15 13:53:57 +08002341 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07002342 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2343 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002344 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002345 return 0;
2346 }
2347
Tomas Winklerc54b6792008-03-06 17:36:53 -08002348 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002349 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002350 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002351 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002352 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002353 &(txq->txb[txq->q.read_ptr]));
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002354 iwl_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002355 } else if (nfreed > 1) {
2356 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002357 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002358 queue_work(priv->workqueue, &priv->restart);
2359 }
2360 nfreed++;
2361 }
2362
Zhu Yib481de92007-09-25 17:54:57 -07002363 return nfreed;
2364}
2365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002366static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002367{
2368 status &= TX_STATUS_MSK;
2369 return (status == TX_STATUS_SUCCESS)
2370 || (status == TX_STATUS_DIRECT_DONE);
2371}
2372
2373/******************************************************************************
2374 *
2375 * Generic RX handler implementations
2376 *
2377 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002378#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002379
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002380static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002381 struct ieee80211_hdr *hdr)
2382{
2383 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2384 return IWL_AP_ID;
2385 else {
2386 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07002387 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002388 }
2389}
2390
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002391static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002392 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002393{
2394 if (priv->txq[txq_id].txb[idx].skb[0])
2395 return (struct ieee80211_hdr *)priv->txq[txq_id].
2396 txb[idx].skb[0]->data;
2397 return NULL;
2398}
2399
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002400static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002401{
2402 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2403 tx_resp->frame_count);
2404 return le32_to_cpu(*scd_ssn) & MAX_SN;
2405
2406}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002407
2408/**
2409 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2410 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002411static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Tomas Winkler6def9762008-05-05 10:22:31 +08002412 struct iwl_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002413 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002414 u16 start_idx)
2415{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002416 u16 status;
2417 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002418 struct ieee80211_tx_status *tx_status = NULL;
2419 struct ieee80211_hdr *hdr = NULL;
2420 int i, sh;
2421 int txq_id, idx;
2422 u16 seq;
2423
2424 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002425 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002426
2427 agg->frame_count = tx_resp->frame_count;
2428 agg->start_idx = start_idx;
2429 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002430 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002431
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002432 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002433 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002434 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002435 status = le16_to_cpu(frame_status[0].status);
2436 seq = le16_to_cpu(frame_status[0].sequence);
2437 idx = SEQ_TO_INDEX(seq);
2438 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002439
Zhu Yib481de92007-09-25 17:54:57 -07002440 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002441 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2442 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002443
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002444 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002445 tx_status->retry_count = tx_resp->failure_frame;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002446 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002447 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002448 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002449 iwl4965_hwrate_to_tx_control(priv,
2450 le32_to_cpu(tx_resp->rate_n_flags),
2451 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002452 /* FIXME: code repetition end */
2453
2454 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2455 status & 0xff, tx_resp->failure_frame);
2456 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002457 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002458
2459 agg->wait_for_ba = 0;
2460 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002461 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002462 u64 bitmap = 0;
2463 int start = agg->start_idx;
2464
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002465 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002466 for (i = 0; i < agg->frame_count; i++) {
2467 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002468 status = le16_to_cpu(frame_status[i].status);
2469 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002470 idx = SEQ_TO_INDEX(seq);
2471 txq_id = SEQ_TO_QUEUE(seq);
2472
2473 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2474 AGG_TX_STATE_ABORT_MSK))
2475 continue;
2476
2477 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2478 agg->frame_count, txq_id, idx);
2479
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002480 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002481
2482 sc = le16_to_cpu(hdr->seq_ctrl);
2483 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2484 IWL_ERROR("BUG_ON idx doesn't match seq control"
2485 " idx=%d, seq_idx=%d, seq=%d\n",
2486 idx, SEQ_TO_SN(sc),
2487 hdr->seq_ctrl);
2488 return -1;
2489 }
2490
2491 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2492 i, idx, SEQ_TO_SN(sc));
2493
2494 sh = idx - start;
2495 if (sh > 64) {
2496 sh = (start - idx) + 0xff;
2497 bitmap = bitmap << sh;
2498 sh = 0;
2499 start = idx;
2500 } else if (sh < -64)
2501 sh = 0xff - (start - idx);
2502 else if (sh < 0) {
2503 sh = start - idx;
2504 start = idx;
2505 bitmap = bitmap << sh;
2506 sh = 0;
2507 }
2508 bitmap |= (1 << sh);
2509 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2510 start, (u32)(bitmap & 0xFFFFFFFF));
2511 }
2512
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002513 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002514 agg->start_idx = start;
2515 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002516 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002517 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002518 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002519
2520 if (bitmap)
2521 agg->wait_for_ba = 1;
2522 }
2523 return 0;
2524}
2525#endif
Zhu Yib481de92007-09-25 17:54:57 -07002526
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002527/**
2528 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2529 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002530static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002531 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002532{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002533 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002534 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2535 int txq_id = SEQ_TO_QUEUE(sequence);
2536 int index = SEQ_TO_INDEX(sequence);
Ron Rindjunsky16466902008-05-05 10:22:50 +08002537 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002538 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002539 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002540 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002541#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002542 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2543 struct ieee80211_hdr *hdr;
2544 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002545#endif
2546
Tomas Winkler443cfd42008-05-15 13:53:57 +08002547 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07002548 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2549 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002550 index, txq->q.n_bd, txq->q.write_ptr,
2551 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002552 return;
2553 }
2554
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002555#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002556 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2557 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002558
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002559 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002560 tid = le16_to_cpu(*qc) & 0xf;
2561
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002562 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2563 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2564 IWL_ERROR("Station not known\n");
2565 return;
2566 }
2567
2568 if (txq->sched_retry) {
2569 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Tomas Winkler6def9762008-05-05 10:22:31 +08002570 struct iwl_ht_agg *agg = NULL;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002571
2572 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002573 return;
Zhu Yib481de92007-09-25 17:54:57 -07002574
2575 agg = &priv->stations[sta_id].tid[tid].agg;
2576
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002577 iwl4965_tx_status_reply_tx(priv, agg,
2578 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002579
2580 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002581 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002582 /* TODO: send BAR */
2583 }
2584
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002585 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002586 int freed, ampdu_q;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002587 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002588 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2589 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002590 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2591 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2592
Tomas Winkler443cfd42008-05-15 13:53:57 +08002593 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002594 txq_id >= 0 && priv->mac80211_registered &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002595 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
2596 /* calculate mac80211 ampdu sw queue to wake */
2597 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
2598 priv->hw->queues;
2599 if (agg->state == IWL_AGG_OFF)
2600 ieee80211_wake_queue(priv->hw, txq_id);
2601 else
2602 ieee80211_wake_queue(priv->hw, ampdu_q);
2603 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002604 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002605 }
2606 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002607#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002608 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07002609
2610 tx_status->retry_count = tx_resp->failure_frame;
Zhu Yib481de92007-09-25 17:54:57 -07002611 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002612 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002613 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2614 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002615
Zhu Yib481de92007-09-25 17:54:57 -07002616 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002617 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07002618 status, le32_to_cpu(tx_resp->rate_n_flags),
2619 tx_resp->failure_frame);
2620
2621 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Tomas Winkler47c51962008-05-05 10:22:41 +08002622#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002623 if (index != -1) {
2624 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002625 if (tid != MAX_TID_COUNT)
2626 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
Tomas Winkler443cfd42008-05-15 13:53:57 +08002627 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002628 (txq_id >= 0) && priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002629 ieee80211_wake_queue(priv->hw, txq_id);
2630 if (tid != MAX_TID_COUNT)
2631 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002632 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002633 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002634#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002635
2636 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2637 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2638}
2639
2640
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002641static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002642 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002643{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002644 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002645 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002646 struct delayed_work *pwork;
2647
2648 palive = &pkt->u.alive_frame;
2649
2650 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2651 "0x%01X 0x%01X\n",
2652 palive->is_valid, palive->ver_type,
2653 palive->ver_subtype);
2654
2655 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2656 IWL_DEBUG_INFO("Initialization Alive received.\n");
2657 memcpy(&priv->card_alive_init,
2658 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002659 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002660 pwork = &priv->init_alive_start;
2661 } else {
2662 IWL_DEBUG_INFO("Runtime Alive received.\n");
2663 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002664 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002665 pwork = &priv->alive_start;
2666 }
2667
2668 /* We delay the ALIVE response by 5ms to
2669 * give the HW RF Kill time to activate... */
2670 if (palive->is_valid == UCODE_VALID_OK)
2671 queue_delayed_work(priv->workqueue, pwork,
2672 msecs_to_jiffies(5));
2673 else
2674 IWL_WARNING("uCode did not respond OK.\n");
2675}
2676
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002677static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002678 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002679{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002680 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002681
2682 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2683 return;
2684}
2685
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002686static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002687 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002688{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002689 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002690
2691 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2692 "seq 0x%04X ser 0x%08X\n",
2693 le32_to_cpu(pkt->u.err_resp.error_type),
2694 get_cmd_string(pkt->u.err_resp.cmd_id),
2695 pkt->u.err_resp.cmd_id,
2696 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2697 le32_to_cpu(pkt->u.err_resp.error_info));
2698}
2699
2700#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2701
Tomas Winklera55360e2008-05-05 10:22:28 +08002702static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002703{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002704 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002705 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002706 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002707 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2708 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2709 rxon->channel = csa->channel;
2710 priv->staging_rxon.channel = csa->channel;
2711}
2712
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002713static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002714 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002715{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002716#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08002717 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002718 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002719
2720 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002721 IWL_DEBUG(IWL_DL_11H,
2722 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07002723 return;
2724 }
2725
2726 memcpy(&priv->measure_report, report, sizeof(*report));
2727 priv->measurement_status |= MEASUREMENT_READY;
2728#endif
2729}
2730
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002731static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002732 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002733{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002734#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002735 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002736 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002737 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2738 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2739#endif
2740}
2741
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002742static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002743 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002744{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002745 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002746 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2747 "notification for %s:\n",
2748 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08002749 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002750}
2751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002753{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002754 struct iwl_priv *priv =
2755 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002756 struct sk_buff *beacon;
2757
2758 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002759 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002760
2761 if (!beacon) {
2762 IWL_ERROR("update beacon failed\n");
2763 return;
2764 }
2765
2766 mutex_lock(&priv->mutex);
2767 /* new beacon skb is allocated every time; dispose previous.*/
2768 if (priv->ibss_beacon)
2769 dev_kfree_skb(priv->ibss_beacon);
2770
2771 priv->ibss_beacon = beacon;
2772 mutex_unlock(&priv->mutex);
2773
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002774 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002775}
2776
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002777static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002778 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002779{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002780#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002781 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002782 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2783 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002784
2785 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2786 "tsf %d %d rate %d\n",
2787 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2788 beacon->beacon_notify_hdr.failure_frame,
2789 le32_to_cpu(beacon->ibss_mgr_status),
2790 le32_to_cpu(beacon->high_tsf),
2791 le32_to_cpu(beacon->low_tsf), rate);
2792#endif
2793
2794 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2795 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2796 queue_work(priv->workqueue, &priv->beacon_update);
2797}
2798
2799/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002800static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002801 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002802{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002803#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002804 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002805 struct iwl4965_scanreq_notification *notif =
2806 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002807
2808 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2809#endif
2810}
2811
2812/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002813static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002814 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002815{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002816 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002817 struct iwl4965_scanstart_notification *notif =
2818 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002819 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2820 IWL_DEBUG_SCAN("Scan start: "
2821 "%d [802.11%s] "
2822 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2823 notif->channel,
2824 notif->band ? "bg" : "a",
2825 notif->tsf_high,
2826 notif->tsf_low, notif->status, notif->beacon_timer);
2827}
2828
2829/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002830static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002831 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002832{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002833 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002834 struct iwl4965_scanresults_notification *notif =
2835 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002836
2837 IWL_DEBUG_SCAN("Scan ch.res: "
2838 "%d [802.11%s] "
2839 "(TSF: 0x%08X:%08X) - %d "
2840 "elapsed=%lu usec (%dms since last)\n",
2841 notif->channel,
2842 notif->band ? "bg" : "a",
2843 le32_to_cpu(notif->tsf_high),
2844 le32_to_cpu(notif->tsf_low),
2845 le32_to_cpu(notif->statistics[0]),
2846 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2847 jiffies_to_msecs(elapsed_jiffies
2848 (priv->last_scan_jiffies, jiffies)));
2849
2850 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002851 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002852}
2853
2854/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002855static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002856 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002857{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002858 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002859 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002860
2861 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2862 scan_notif->scanned_channels,
2863 scan_notif->tsf_low,
2864 scan_notif->tsf_high, scan_notif->status);
2865
2866 /* The HW is no longer scanning */
2867 clear_bit(STATUS_SCAN_HW, &priv->status);
2868
2869 /* The scan completion notification came in, so kill that timer... */
2870 cancel_delayed_work(&priv->scan_check);
2871
2872 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
2873 (priv->scan_bands == 2) ? "2.4" : "5.2",
2874 jiffies_to_msecs(elapsed_jiffies
2875 (priv->scan_pass_start, jiffies)));
2876
2877 /* Remove this scanned band from the list
2878 * of pending bands to scan */
2879 priv->scan_bands--;
2880
2881 /* If a request to abort was given, or the scan did not succeed
2882 * then we reset the scan state machine and terminate,
2883 * re-queuing another scan if one has been requested */
2884 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2885 IWL_DEBUG_INFO("Aborted scan completed.\n");
2886 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2887 } else {
2888 /* If there are more bands on this scan pass reschedule */
2889 if (priv->scan_bands > 0)
2890 goto reschedule;
2891 }
2892
2893 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002894 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002895 IWL_DEBUG_INFO("Setting scan to off\n");
2896
2897 clear_bit(STATUS_SCANNING, &priv->status);
2898
2899 IWL_DEBUG_INFO("Scan took %dms\n",
2900 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2901
2902 queue_work(priv->workqueue, &priv->scan_completed);
2903
2904 return;
2905
2906reschedule:
2907 priv->scan_pass_start = jiffies;
2908 queue_work(priv->workqueue, &priv->request_scan);
2909}
2910
2911/* Handle notification from uCode that card's power state is changing
2912 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002913static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002914 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002915{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002916 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002917 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2918 unsigned long status = priv->status;
2919
2920 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2921 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2922 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2923
2924 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
2925 RF_CARD_DISABLED)) {
2926
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002927 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002928 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2929
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002930 if (!iwl_grab_nic_access(priv)) {
2931 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002932 priv, HBUS_TARG_MBX_C,
2933 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2934
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002935 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002936 }
2937
2938 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002939 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002940 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002941 if (!iwl_grab_nic_access(priv)) {
2942 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002943 priv, HBUS_TARG_MBX_C,
2944 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2945
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002946 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002947 }
2948 }
2949
2950 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002951 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002952 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002953 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2954 if (!iwl_grab_nic_access(priv))
2955 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002956 }
2957 }
2958
2959 if (flags & HW_CARD_DISABLED)
2960 set_bit(STATUS_RF_KILL_HW, &priv->status);
2961 else
2962 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2963
2964
2965 if (flags & SW_CARD_DISABLED)
2966 set_bit(STATUS_RF_KILL_SW, &priv->status);
2967 else
2968 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2969
2970 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002971 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002972
2973 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2974 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2975 (test_bit(STATUS_RF_KILL_SW, &status) !=
2976 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2977 queue_work(priv->workqueue, &priv->rf_kill);
2978 else
2979 wake_up_interruptible(&priv->wait_command_queue);
2980}
2981
2982/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002983 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07002984 *
2985 * Setup the RX handlers for each of the reply types sent from the uCode
2986 * to the host.
2987 *
2988 * This function chains into the hardware specific files for them to setup
2989 * any hardware specific handlers as well.
2990 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002991static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002992{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002993 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
2994 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
2995 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
2996 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07002997 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002998 iwl4965_rx_spectrum_measure_notif;
2999 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003000 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003001 iwl4965_rx_pm_debug_statistics_notif;
3002 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003003
Ben Cahill9fbab512007-11-29 11:09:47 +08003004 /*
3005 * The same handler is used for both the REPLY to a discrete
3006 * statistics request from the host as well as for the periodic
3007 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003008 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003009 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3010 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003011
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003012 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3013 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003014 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003015 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003016 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003017 iwl4965_rx_scan_complete_notif;
3018 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3019 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003020
Ben Cahill9fbab512007-11-29 11:09:47 +08003021 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07003022 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003023}
3024
3025/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003026 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003027 * @rxb: Rx buffer to reclaim
3028 *
3029 * If an Rx buffer has an async callback associated with it the callback
3030 * will be executed. The attached skb (if present) will only be freed
3031 * if the callback returns 1
3032 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003033static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08003034 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003035{
Tomas Winklerdb11d632008-05-05 10:22:33 +08003036 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003037 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3038 int txq_id = SEQ_TO_QUEUE(sequence);
3039 int index = SEQ_TO_INDEX(sequence);
3040 int huge = sequence & SEQ_HUGE_FRAME;
3041 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003042 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003043
3044 /* If a Tx command is being handled and it isn't in the actual
3045 * command queue then there a command routing bug has been introduced
3046 * in the queue management code. */
3047 if (txq_id != IWL_CMD_QUEUE_NUM)
3048 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3049 txq_id, pkt->hdr.cmd);
3050 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3051
3052 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3053 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3054
3055 /* Input error checking is done when commands are added to queue. */
3056 if (cmd->meta.flags & CMD_WANT_SKB) {
3057 cmd->meta.source->u.skb = rxb->skb;
3058 rxb->skb = NULL;
3059 } else if (cmd->meta.u.callback &&
3060 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3061 rxb->skb = NULL;
3062
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003063 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003064
3065 if (!(cmd->meta.flags & CMD_ASYNC)) {
3066 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3067 wake_up_interruptible(&priv->wait_command_queue);
3068 }
3069}
3070
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003071/*
3072 * this should be called while priv->lock is locked
3073*/
Tomas Winklera55360e2008-05-05 10:22:28 +08003074static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003075{
Tomas Winklera55360e2008-05-05 10:22:28 +08003076 iwl_rx_allocate(priv);
3077 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003078}
3079
3080
Zhu Yib481de92007-09-25 17:54:57 -07003081/**
Tomas Winklera55360e2008-05-05 10:22:28 +08003082 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003083 *
3084 * Uses the priv->rx_handlers callback function array to invoke
3085 * the appropriate handlers, including command responses,
3086 * frame-received notifications, and other notifications.
3087 */
Tomas Winklera55360e2008-05-05 10:22:28 +08003088void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003089{
Tomas Winklera55360e2008-05-05 10:22:28 +08003090 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08003091 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08003092 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003093 u32 r, i;
3094 int reclaim;
3095 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003096 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003097 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003098
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003099 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3100 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08003101 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003102 i = rxq->read;
3103
3104 /* Rx interrupt, but nothing sent from uCode */
3105 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08003106 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07003107
Tomas Winklera55360e2008-05-05 10:22:28 +08003108 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003109 fill_rx = 1;
3110
Zhu Yib481de92007-09-25 17:54:57 -07003111 while (i != r) {
3112 rxb = rxq->queue[i];
3113
Ben Cahill9fbab512007-11-29 11:09:47 +08003114 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003115 * then a bug has been introduced in the queue refilling
3116 * routines -- catch it here */
3117 BUG_ON(rxb == NULL);
3118
3119 rxq->queue[i] = NULL;
3120
3121 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003122 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003123 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08003124 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003125
3126 /* Reclaim a command buffer only if this packet is a response
3127 * to a (driver-originated) command.
3128 * If the packet (e.g. Rx frame) originated from uCode,
3129 * there is no command buffer to reclaim.
3130 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3131 * but apparently a few don't get set; catch them here. */
3132 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3133 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003134 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003135 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003136 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3137 (pkt->hdr.cmd != REPLY_TX);
3138
3139 /* Based on type of command response or notification,
3140 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003141 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003142 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003143 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
3144 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003145 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3146 } else {
3147 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08003148 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07003149 "r %d i %d No handler needed for %s, 0x%02x\n",
3150 r, i, get_cmd_string(pkt->hdr.cmd),
3151 pkt->hdr.cmd);
3152 }
3153
3154 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003155 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07003156 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003157 * as we reclaim the driver command queue */
3158 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003159 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003160 else
3161 IWL_WARNING("Claim null rxb?\n");
3162 }
3163
3164 /* For now we just don't re-use anything. We can tweak this
3165 * later to try and re-use notification packets and SKBs that
3166 * fail to Rx correctly */
3167 if (rxb->skb != NULL) {
3168 priv->alloc_rxb_skb--;
3169 dev_kfree_skb_any(rxb->skb);
3170 rxb->skb = NULL;
3171 }
3172
3173 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003174 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003175 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003176 spin_lock_irqsave(&rxq->lock, flags);
3177 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3178 spin_unlock_irqrestore(&rxq->lock, flags);
3179 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003180 /* If there are a lot of unused frames,
3181 * restock the Rx queue so ucode wont assert. */
3182 if (fill_rx) {
3183 count++;
3184 if (count >= 8) {
3185 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003186 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003187 count = 0;
3188 }
3189 }
Zhu Yib481de92007-09-25 17:54:57 -07003190 }
3191
3192 /* Backtrack one entry */
3193 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003194 iwl_rx_queue_restock(priv);
3195}
3196/* Convert linear signal-to-noise ratio into dB */
3197static u8 ratio2dB[100] = {
3198/* 0 1 2 3 4 5 6 7 8 9 */
3199 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3200 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3201 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3202 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3203 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3204 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3205 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3206 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3207 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3208 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3209};
3210
3211/* Calculates a relative dB value from a ratio of linear
3212 * (i.e. not dB) signal levels.
3213 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
3214int iwl4965_calc_db_from_ratio(int sig_ratio)
3215{
3216 /* 1000:1 or higher just report as 60 dB */
3217 if (sig_ratio >= 1000)
3218 return 60;
3219
3220 /* 100:1 or higher, divide by 10 and use table,
3221 * add 20 dB to make up for divide by 10 */
3222 if (sig_ratio >= 100)
3223 return (20 + (int)ratio2dB[sig_ratio/10]);
3224
3225 /* We shouldn't see this */
3226 if (sig_ratio < 1)
3227 return 0;
3228
3229 /* Use table for ratios 1:1 - 99:1 */
3230 return (int)ratio2dB[sig_ratio];
3231}
3232
3233#define PERFECT_RSSI (-20) /* dBm */
3234#define WORST_RSSI (-95) /* dBm */
3235#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3236
3237/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3238 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3239 * about formulas used below. */
3240int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
3241{
3242 int sig_qual;
3243 int degradation = PERFECT_RSSI - rssi_dbm;
3244
3245 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3246 * as indicator; formula is (signal dbm - noise dbm).
3247 * SNR at or above 40 is a great signal (100%).
3248 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3249 * Weakest usable signal is usually 10 - 15 dB SNR. */
3250 if (noise_dbm) {
3251 if (rssi_dbm - noise_dbm >= 40)
3252 return 100;
3253 else if (rssi_dbm < noise_dbm)
3254 return 0;
3255 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3256
3257 /* Else use just the signal level.
3258 * This formula is a least squares fit of data points collected and
3259 * compared with a reference system that had a percentage (%) display
3260 * for signal quality. */
3261 } else
3262 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3263 (15 * RSSI_RANGE + 62 * degradation)) /
3264 (RSSI_RANGE * RSSI_RANGE);
3265
3266 if (sig_qual > 100)
3267 sig_qual = 100;
3268 else if (sig_qual < 1)
3269 sig_qual = 0;
3270
3271 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07003272}
3273
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003274/**
3275 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
3276 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003277static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +08003278 struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003279{
3280 u32 reg = 0;
3281 int rc = 0;
3282 int txq_id = txq->q.id;
3283
3284 if (txq->need_update == 0)
3285 return rc;
3286
3287 /* if we're trying to save power */
3288 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3289 /* wake up nic if it's powered down ...
3290 * uCode will wake up, and interrupt us again, so next
3291 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003292 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003293
3294 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3295 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003296 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003297 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3298 return rc;
3299 }
3300
3301 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003302 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003303 if (rc)
3304 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003305 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003306 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003307 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003308
3309 /* else not in power-save mode, uCode will never sleep when we're
3310 * trying to tx (during RFKILL, we're not trying to tx). */
3311 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003312 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003313 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003314
3315 txq->need_update = 0;
3316
3317 return rc;
3318}
3319
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003320#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003321static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003322{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08003323 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07003324 DECLARE_MAC_BUF(mac);
3325
Zhu Yib481de92007-09-25 17:54:57 -07003326 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08003327 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003328 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3329 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3330 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3331 le32_to_cpu(rxon->filter_flags));
3332 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3333 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3334 rxon->ofdm_basic_rates);
3335 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07003336 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3337 print_mac(mac, rxon->node_addr));
3338 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3339 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003340 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3341}
3342#endif
3343
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003344static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003345{
3346 IWL_DEBUG_ISR("Enabling interrupts\n");
3347 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003348 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003349}
3350
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003351/* call this function to flush any scheduled tasklet */
3352static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3353{
3354 /* wait to make sure we flush pedding tasklet*/
3355 synchronize_irq(priv->pci_dev->irq);
3356 tasklet_kill(&priv->irq_tasklet);
3357}
3358
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003359static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003360{
3361 clear_bit(STATUS_INT_ENABLED, &priv->status);
3362
3363 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003364 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003365
3366 /* acknowledge/clear/reset any interrupts still pending
3367 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003368 iwl_write32(priv, CSR_INT, 0xffffffff);
3369 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003370 IWL_DEBUG_ISR("Disabled interrupts\n");
3371}
3372
3373static const char *desc_lookup(int i)
3374{
3375 switch (i) {
3376 case 1:
3377 return "FAIL";
3378 case 2:
3379 return "BAD_PARAM";
3380 case 3:
3381 return "BAD_CHECKSUM";
3382 case 4:
3383 return "NMI_INTERRUPT";
3384 case 5:
3385 return "SYSASSERT";
3386 case 6:
3387 return "FATAL_ERROR";
3388 }
3389
3390 return "UNKNOWN";
3391}
3392
3393#define ERROR_START_OFFSET (1 * sizeof(u32))
3394#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3395
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003396static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003397{
3398 u32 data2, line;
3399 u32 desc, time, count, base, data1;
3400 u32 blink1, blink2, ilink1, ilink2;
3401 int rc;
3402
3403 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3404
Tomas Winkler57aab752008-04-14 21:16:03 -07003405 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003406 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3407 return;
3408 }
3409
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003410 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003411 if (rc) {
3412 IWL_WARNING("Can not read from adapter at this time.\n");
3413 return;
3414 }
3415
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003416 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003417
3418 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3419 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003420 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003421 }
3422
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003423 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3424 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3425 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3426 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3427 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3428 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3429 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3430 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3431 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003432
3433 IWL_ERROR("Desc Time "
3434 "data1 data2 line\n");
3435 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3436 desc_lookup(desc), desc, time, data1, data2, line);
3437 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3438 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3439 ilink1, ilink2);
3440
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003441 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003442}
3443
3444#define EVENT_START_OFFSET (4 * sizeof(u32))
3445
3446/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003447 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003448 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003449 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003450 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003451static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003452 u32 num_events, u32 mode)
3453{
3454 u32 i;
3455 u32 base; /* SRAM byte address of event log header */
3456 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3457 u32 ptr; /* SRAM byte address of log data */
3458 u32 ev, time, data; /* event log data */
3459
3460 if (num_events == 0)
3461 return;
3462
3463 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3464
3465 if (mode == 0)
3466 event_size = 2 * sizeof(u32);
3467 else
3468 event_size = 3 * sizeof(u32);
3469
3470 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3471
3472 /* "time" is actually "data" for mode 0 (no timestamp).
3473 * place event id # at far right for easier visual parsing. */
3474 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003475 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003476 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003477 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003478 ptr += sizeof(u32);
3479 if (mode == 0)
3480 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
3481 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003482 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003483 ptr += sizeof(u32);
3484 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
3485 }
3486 }
3487}
3488
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003489static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003490{
3491 int rc;
3492 u32 base; /* SRAM byte address of event log header */
3493 u32 capacity; /* event log capacity in # entries */
3494 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3495 u32 num_wraps; /* # times uCode wrapped to top of log */
3496 u32 next_entry; /* index of next entry to be written by uCode */
3497 u32 size; /* # entries that we'll print */
3498
3499 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07003500 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003501 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
3502 return;
3503 }
3504
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003505 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003506 if (rc) {
3507 IWL_WARNING("Can not read from adapter at this time.\n");
3508 return;
3509 }
3510
3511 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003512 capacity = iwl_read_targ_mem(priv, base);
3513 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3514 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3515 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003516
3517 size = num_wraps ? capacity : next_entry;
3518
3519 /* bail out if nothing in log */
3520 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08003521 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003522 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003523 return;
3524 }
3525
Zhu Yi583fab32007-09-27 11:27:30 +08003526 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003527 size, num_wraps);
3528
3529 /* if uCode has wrapped back to top of log, start at the oldest entry,
3530 * i.e the next one that uCode would fill. */
3531 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003532 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003533 capacity - next_entry, mode);
3534
3535 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003536 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003537
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003538 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003539}
3540
3541/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003542 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003543 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003544static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003545{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003546 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07003547 set_bit(STATUS_FW_ERROR, &priv->status);
3548
3549 /* Cancel currently queued command. */
3550 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3551
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003552#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003553 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003554 iwl4965_dump_nic_error_log(priv);
3555 iwl4965_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08003556 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003557 }
3558#endif
3559
3560 wake_up_interruptible(&priv->wait_command_queue);
3561
3562 /* Keep the restart process from trying to send host
3563 * commands by clearing the INIT status bit */
3564 clear_bit(STATUS_READY, &priv->status);
3565
3566 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003567 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07003568 "Restarting adapter due to uCode error.\n");
3569
Tomas Winkler3109ece2008-03-28 16:33:35 -07003570 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003571 memcpy(&priv->recovery_rxon, &priv->active_rxon,
3572 sizeof(priv->recovery_rxon));
3573 priv->error_recovering = 1;
3574 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08003575 if (priv->cfg->mod_params->restart_fw)
3576 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07003577 }
3578}
3579
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003580static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003581{
3582 unsigned long flags;
3583
3584 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
3585 sizeof(priv->staging_rxon));
3586 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003587 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003588
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003589 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07003590
3591 spin_lock_irqsave(&priv->lock, flags);
3592 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
3593 priv->error_recovering = 0;
3594 spin_unlock_irqrestore(&priv->lock, flags);
3595}
3596
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003597static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003598{
3599 u32 inta, handled = 0;
3600 u32 inta_fh;
3601 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003602#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003603 u32 inta_mask;
3604#endif
3605
3606 spin_lock_irqsave(&priv->lock, flags);
3607
3608 /* Ack/clear/reset pending uCode interrupts.
3609 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3610 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003611 inta = iwl_read32(priv, CSR_INT);
3612 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003613
3614 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3615 * Any new interrupts that happen after this, either while we're
3616 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003617 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3618 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003619
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003620#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003621 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003622 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003623 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003624 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3625 inta, inta_mask, inta_fh);
3626 }
3627#endif
3628
3629 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3630 * atomic, make sure that inta covers all the interrupts that
3631 * we've discovered, even if FH interrupt came in just after
3632 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003633 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003634 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003635 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003636 inta |= CSR_INT_BIT_FH_TX;
3637
3638 /* Now service all interrupt bits discovered above. */
3639 if (inta & CSR_INT_BIT_HW_ERR) {
3640 IWL_ERROR("Microcode HW error detected. Restarting.\n");
3641
3642 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003643 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003644
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003645 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003646
3647 handled |= CSR_INT_BIT_HW_ERR;
3648
3649 spin_unlock_irqrestore(&priv->lock, flags);
3650
3651 return;
3652 }
3653
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003654#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003655 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003656 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003657 if (inta & CSR_INT_BIT_SCD)
3658 IWL_DEBUG_ISR("Scheduler finished to transmit "
3659 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003660
3661 /* Alive notification via Rx interrupt will do the real work */
3662 if (inta & CSR_INT_BIT_ALIVE)
3663 IWL_DEBUG_ISR("Alive interrupt\n");
3664 }
3665#endif
3666 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003667 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003668
Ben Cahill9fbab512007-11-29 11:09:47 +08003669 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07003670 if (inta & CSR_INT_BIT_RF_KILL) {
3671 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003672 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07003673 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
3674 hw_rf_kill = 1;
3675
Ester Kummerf3d67992008-05-06 11:05:12 +08003676 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07003677 hw_rf_kill ? "disable radio":"enable radio");
3678
3679 /* Queue restart only if RF_KILL switch was set to "kill"
3680 * when we loaded driver, and is now set to "enable".
3681 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08003682 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08003683 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
3684 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003685 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08003686 }
Zhu Yib481de92007-09-25 17:54:57 -07003687
3688 handled |= CSR_INT_BIT_RF_KILL;
3689 }
3690
Ben Cahill9fbab512007-11-29 11:09:47 +08003691 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07003692 if (inta & CSR_INT_BIT_CT_KILL) {
3693 IWL_ERROR("Microcode CT kill error detected.\n");
3694 handled |= CSR_INT_BIT_CT_KILL;
3695 }
3696
3697 /* Error detected by uCode */
3698 if (inta & CSR_INT_BIT_SW_ERR) {
3699 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
3700 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003701 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003702 handled |= CSR_INT_BIT_SW_ERR;
3703 }
3704
3705 /* uCode wakes up after power-down sleep */
3706 if (inta & CSR_INT_BIT_WAKEUP) {
3707 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08003708 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003709 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
3710 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
3711 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
3712 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
3713 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
3714 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003715
3716 handled |= CSR_INT_BIT_WAKEUP;
3717 }
3718
3719 /* All uCode command responses, including Tx command responses,
3720 * Rx "responses" (frame-received notification), and other
3721 * notifications from uCode come through here*/
3722 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08003723 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003724 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3725 }
3726
3727 if (inta & CSR_INT_BIT_FH_TX) {
3728 IWL_DEBUG_ISR("Tx interrupt\n");
3729 handled |= CSR_INT_BIT_FH_TX;
3730 }
3731
3732 if (inta & ~handled)
3733 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
3734
3735 if (inta & ~CSR_INI_SET_MASK) {
3736 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
3737 inta & ~CSR_INI_SET_MASK);
3738 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
3739 }
3740
3741 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003742 /* only Re-enable if diabled by irq */
3743 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3744 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003745
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003746#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003747 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003748 inta = iwl_read32(priv, CSR_INT);
3749 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3750 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003751 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3752 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3753 }
3754#endif
3755 spin_unlock_irqrestore(&priv->lock, flags);
3756}
3757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003758static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003759{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003760 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003761 u32 inta, inta_mask;
3762 u32 inta_fh;
3763 if (!priv)
3764 return IRQ_NONE;
3765
3766 spin_lock(&priv->lock);
3767
3768 /* Disable (but don't clear!) interrupts here to avoid
3769 * back-to-back ISRs and sporadic interrupts from our NIC.
3770 * If we have something to service, the tasklet will re-enable ints.
3771 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003772 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3773 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003774
3775 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003776 inta = iwl_read32(priv, CSR_INT);
3777 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003778
3779 /* Ignore interrupt if there's nothing in NIC to service.
3780 * This may be due to IRQ shared with another device,
3781 * or due to sporadic interrupts thrown from our NIC. */
3782 if (!inta && !inta_fh) {
3783 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3784 goto none;
3785 }
3786
3787 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08003788 /* Hardware disappeared. It might have already raised
3789 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07003790 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08003791 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003792 }
3793
3794 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3795 inta, inta_mask, inta_fh);
3796
Joonwoo Park25c03d82008-01-23 10:15:20 -08003797 inta &= ~CSR_INT_BIT_SCD;
3798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003799 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003800 if (likely(inta || inta_fh))
3801 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07003802
Oliver Neukum66fbb542007-11-15 09:31:10 +08003803 unplugged:
3804 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07003805 return IRQ_HANDLED;
3806
3807 none:
3808 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003809 /* only Re-enable if diabled by irq */
3810 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3811 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003812 spin_unlock(&priv->lock);
3813 return IRQ_NONE;
3814}
3815
Zhu Yib481de92007-09-25 17:54:57 -07003816/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3817 * sending probe req. This should be set long enough to hear probe responses
3818 * from more than one AP. */
3819#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
3820#define IWL_ACTIVE_DWELL_TIME_52 (10)
3821
3822/* For faster active scanning, scan will move to the next channel if fewer than
3823 * PLCP_QUIET_THRESH packets are heard on this channel within
3824 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3825 * time if it's a quiet channel (nothing responded to our probe, and there's
3826 * no other traffic).
3827 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3828#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
3829#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
3830
3831/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3832 * Must be set longer than active dwell time.
3833 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
3834#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
3835#define IWL_PASSIVE_DWELL_TIME_52 (10)
3836#define IWL_PASSIVE_DWELL_BASE (100)
3837#define IWL_CHANNEL_TUNE_TIME 5
3838
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003839static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003840 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003841{
Johannes Berg8318d782008-01-24 19:38:38 +01003842 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003843 return IWL_ACTIVE_DWELL_TIME_52;
3844 else
3845 return IWL_ACTIVE_DWELL_TIME_24;
3846}
3847
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003848static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003849 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003850{
Johannes Berg8318d782008-01-24 19:38:38 +01003851 u16 active = iwl4965_get_active_dwell_time(priv, band);
3852 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003853 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
3854 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
3855
Tomas Winkler3109ece2008-03-28 16:33:35 -07003856 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003857 /* If we're associated, we clamp the maximum passive
3858 * dwell time to be 98% of the beacon interval (minus
3859 * 2 * channel tune time) */
3860 passive = priv->beacon_int;
3861 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
3862 passive = IWL_PASSIVE_DWELL_BASE;
3863 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
3864 }
3865
3866 if (passive <= active)
3867 passive = active + 1;
3868
3869 return passive;
3870}
3871
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003872static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003873 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07003874 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003875 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07003876{
3877 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01003878 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003879 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003880 u16 passive_dwell = 0;
3881 u16 active_dwell = 0;
3882 int added, i;
3883
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07003884 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01003885 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07003886 return 0;
3887
Johannes Berg8318d782008-01-24 19:38:38 +01003888 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07003889
Johannes Berg8318d782008-01-24 19:38:38 +01003890 active_dwell = iwl4965_get_active_dwell_time(priv, band);
3891 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07003892
Johannes Berg8318d782008-01-24 19:38:38 +01003893 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02003894 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3895 continue;
3896
Johannes Berg8318d782008-01-24 19:38:38 +01003897 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07003898
Assaf Krauss8622e702008-03-21 13:53:43 -07003899 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08003900 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003901 if (!is_channel_valid(ch_info)) {
3902 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
3903 scan_ch->channel);
3904 continue;
3905 }
3906
3907 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01003908 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07003909 scan_ch->type = 0; /* passive */
3910 else
3911 scan_ch->type = 1; /* active */
3912
3913 if (scan_ch->type & 1)
3914 scan_ch->type |= (direct_mask << 1);
3915
Zhu Yib481de92007-09-25 17:54:57 -07003916 scan_ch->active_dwell = cpu_to_le16(active_dwell);
3917 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
3918
Ben Cahill9fbab512007-11-29 11:09:47 +08003919 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07003920 scan_ch->tpc.dsp_atten = 110;
3921 /* scan_pwr_info->tpc.dsp_atten; */
3922
3923 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01003924 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003925 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
3926 else {
3927 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
3928 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08003929 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08003930 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07003931 */
3932 }
3933
3934 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
3935 scan_ch->channel,
3936 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
3937 (scan_ch->type & 1) ?
3938 active_dwell : passive_dwell);
3939
3940 scan_ch++;
3941 added++;
3942 }
3943
3944 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
3945 return added;
3946}
3947
Zhu Yib481de92007-09-25 17:54:57 -07003948/******************************************************************************
3949 *
3950 * uCode download functions
3951 *
3952 ******************************************************************************/
3953
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003954static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003955{
Tomas Winkler98c92212008-01-14 17:46:20 -08003956 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
3957 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
3958 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3959 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
3960 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3961 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07003962}
3963
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08003964static void iwl4965_nic_start(struct iwl_priv *priv)
3965{
3966 /* Remove all resets to allow NIC to operate */
3967 iwl_write32(priv, CSR_RESET, 0);
3968}
3969
3970
Zhu Yib481de92007-09-25 17:54:57 -07003971/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003972 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07003973 *
3974 * Copy into buffers for card to fetch via bus-mastering
3975 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003976static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003977{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003978 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003979 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003980 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08003981 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07003982 u8 *src;
3983 size_t len;
3984 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
3985
3986 /* Ask kernel firmware_class module to get the boot firmware off disk.
3987 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003988 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
3989 if (ret < 0) {
3990 IWL_ERROR("%s firmware file req failed: Reason %d\n",
3991 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07003992 goto error;
3993 }
3994
3995 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3996 name, ucode_raw->size);
3997
3998 /* Make sure that we got at least our header! */
3999 if (ucode_raw->size < sizeof(*ucode)) {
4000 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004001 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004002 goto err_release;
4003 }
4004
4005 /* Data from ucode file: header followed by uCode images */
4006 ucode = (void *)ucode_raw->data;
4007
4008 ver = le32_to_cpu(ucode->ver);
4009 inst_size = le32_to_cpu(ucode->inst_size);
4010 data_size = le32_to_cpu(ucode->data_size);
4011 init_size = le32_to_cpu(ucode->init_size);
4012 init_data_size = le32_to_cpu(ucode->init_data_size);
4013 boot_size = le32_to_cpu(ucode->boot_size);
4014
4015 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
4016 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
4017 inst_size);
4018 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
4019 data_size);
4020 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
4021 init_size);
4022 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
4023 init_data_size);
4024 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
4025 boot_size);
4026
4027 /* Verify size of file vs. image size info in file's header */
4028 if (ucode_raw->size < sizeof(*ucode) +
4029 inst_size + data_size + init_size +
4030 init_data_size + boot_size) {
4031
4032 IWL_DEBUG_INFO("uCode file size %d too small\n",
4033 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004034 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004035 goto err_release;
4036 }
4037
4038 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004039 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004040 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4041 inst_size);
4042 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004043 goto err_release;
4044 }
4045
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004046 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004047 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4048 data_size);
4049 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004050 goto err_release;
4051 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004052 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004053 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004054 ("uCode init instr len %d too large to fit in\n",
4055 init_size);
4056 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004057 goto err_release;
4058 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004059 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004060 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004061 ("uCode init data len %d too large to fit in\n",
4062 init_data_size);
4063 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004064 goto err_release;
4065 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004066 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004067 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004068 ("uCode boot instr len %d too large to fit in\n",
4069 boot_size);
4070 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004071 goto err_release;
4072 }
4073
4074 /* Allocate ucode buffers for card's bus-master loading ... */
4075
4076 /* Runtime instructions and 2 copies of data:
4077 * 1) unmodified from disk
4078 * 2) backup cache for save/restore during power-downs */
4079 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004080 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004081
4082 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004083 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004084
4085 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004086 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004087
4088 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004089 if (init_size && init_data_size) {
4090 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004091 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07004092
Tomas Winkler90e759d2007-11-29 11:09:41 +08004093 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004094 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004095
4096 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4097 goto err_pci_alloc;
4098 }
Zhu Yib481de92007-09-25 17:54:57 -07004099
4100 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004101 if (boot_size) {
4102 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004103 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004104
Tomas Winkler90e759d2007-11-29 11:09:41 +08004105 if (!priv->ucode_boot.v_addr)
4106 goto err_pci_alloc;
4107 }
Zhu Yib481de92007-09-25 17:54:57 -07004108
4109 /* Copy images into buffers for card's bus-master reads ... */
4110
4111 /* Runtime instructions (first block of data in file) */
4112 src = &ucode->data[0];
4113 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004114 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004115 memcpy(priv->ucode_code.v_addr, src, len);
4116 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4117 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4118
4119 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004120 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004121 src = &ucode->data[inst_size];
4122 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004123 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004124 memcpy(priv->ucode_data.v_addr, src, len);
4125 memcpy(priv->ucode_data_backup.v_addr, src, len);
4126
4127 /* Initialization instructions (3rd block) */
4128 if (init_size) {
4129 src = &ucode->data[inst_size + data_size];
4130 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004131 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4132 len);
Zhu Yib481de92007-09-25 17:54:57 -07004133 memcpy(priv->ucode_init.v_addr, src, len);
4134 }
4135
4136 /* Initialization data (4th block) */
4137 if (init_data_size) {
4138 src = &ucode->data[inst_size + data_size + init_size];
4139 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004140 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
4141 len);
Zhu Yib481de92007-09-25 17:54:57 -07004142 memcpy(priv->ucode_init_data.v_addr, src, len);
4143 }
4144
4145 /* Bootstrap instructions (5th block) */
4146 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4147 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004148 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004149 memcpy(priv->ucode_boot.v_addr, src, len);
4150
4151 /* We have our copies now, allow OS release its copies */
4152 release_firmware(ucode_raw);
4153 return 0;
4154
4155 err_pci_alloc:
4156 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004157 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004158 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004159
4160 err_release:
4161 release_firmware(ucode_raw);
4162
4163 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08004164 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004165}
4166
Zhu Yib481de92007-09-25 17:54:57 -07004167/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004168 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004169 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004170 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07004171 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004172static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004173{
Tomas Winkler57aab752008-04-14 21:16:03 -07004174 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004175
4176 IWL_DEBUG_INFO("Runtime Alive received.\n");
4177
4178 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4179 /* We had an error bringing up the hardware, so take it
4180 * all the way back down so we can try again */
4181 IWL_DEBUG_INFO("Alive failed.\n");
4182 goto restart;
4183 }
4184
4185 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4186 * This is a paranoid check, because we would not have gotten the
4187 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07004188 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004189 /* Runtime instruction load was bad;
4190 * take it all the way back down so we can try again */
4191 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4192 goto restart;
4193 }
4194
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004195 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004196
Tomas Winkler57aab752008-04-14 21:16:03 -07004197 ret = priv->cfg->ops->lib->alive_notify(priv);
4198 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07004199 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07004200 ret);
Zhu Yib481de92007-09-25 17:54:57 -07004201 goto restart;
4202 }
4203
Ben Cahill9fbab512007-11-29 11:09:47 +08004204 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07004205 set_bit(STATUS_ALIVE, &priv->status);
4206
4207 /* Clear out the uCode error bit if it is set */
4208 clear_bit(STATUS_FW_ERROR, &priv->status);
4209
Tomas Winklerfee12472008-04-03 16:05:21 -07004210 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004211 return;
4212
Zhu Yi5a66926a2008-01-14 17:46:18 -08004213 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004214
4215 priv->active_rate = priv->rates_mask;
4216 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
4217
Tomas Winkler3109ece2008-03-28 16:33:35 -07004218 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08004219 struct iwl_rxon_cmd *active_rxon =
4220 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07004221
4222 memcpy(&priv->staging_rxon, &priv->active_rxon,
4223 sizeof(priv->staging_rxon));
4224 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4225 } else {
4226 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004227 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004228 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
4229 }
4230
Ben Cahill9fbab512007-11-29 11:09:47 +08004231 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004232 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004233
4234 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004235 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004236
4237 /* At this point, the NIC is initialized and operational */
4238 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004239
4240 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004241
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07004242 iwl_leds_register(priv);
4243
Zhu Yib481de92007-09-25 17:54:57 -07004244 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07004245 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004246 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07004247
4248 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004249 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004250
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004251 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004252 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07004253 return;
4254
4255 restart:
4256 queue_work(priv->workqueue, &priv->restart);
4257}
4258
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004259static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07004260
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004261static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004262{
4263 unsigned long flags;
4264 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4265 struct ieee80211_conf *conf = NULL;
4266
4267 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4268
4269 conf = ieee80211_get_hw_conf(priv->hw);
4270
4271 if (!exit_pending)
4272 set_bit(STATUS_EXIT_PENDING, &priv->status);
4273
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07004274 iwl_leds_unregister(priv);
4275
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004276 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
4277
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004278 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004279
4280 /* Unblock any waiting calls */
4281 wake_up_interruptible_all(&priv->wait_command_queue);
4282
Zhu Yib481de92007-09-25 17:54:57 -07004283 /* Wipe out the EXIT_PENDING status bit if we are not actually
4284 * exiting the module */
4285 if (!exit_pending)
4286 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4287
4288 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004289 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07004290
4291 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004292 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004293 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004294 spin_unlock_irqrestore(&priv->lock, flags);
4295 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004296
4297 if (priv->mac80211_registered)
4298 ieee80211_stop_queues(priv->hw);
4299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004300 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07004301 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07004302 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004303 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4304 STATUS_RF_KILL_HW |
4305 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4306 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004307 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4308 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004309 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4310 STATUS_IN_SUSPEND;
4311 goto exit;
4312 }
4313
4314 /* ...otherwise clear out all the status bits but the RF Kill and
4315 * SUSPEND bits and continue taking the NIC down. */
4316 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4317 STATUS_RF_KILL_HW |
4318 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4319 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004320 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4321 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004322 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4323 STATUS_IN_SUSPEND |
4324 test_bit(STATUS_FW_ERROR, &priv->status) <<
4325 STATUS_FW_ERROR;
4326
4327 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004328 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08004329 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07004330 spin_unlock_irqrestore(&priv->lock, flags);
4331
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004332 iwl4965_hw_txq_ctx_stop(priv);
4333 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004334
4335 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004336 if (!iwl_grab_nic_access(priv)) {
4337 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004338 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004339 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004340 }
4341 spin_unlock_irqrestore(&priv->lock, flags);
4342
4343 udelay(5);
4344
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004345 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004346 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004347 iwl4965_hw_nic_reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004348 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004349
4350 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004351 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07004352
4353 if (priv->ibss_beacon)
4354 dev_kfree_skb(priv->ibss_beacon);
4355 priv->ibss_beacon = NULL;
4356
4357 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004358 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004359}
4360
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004361static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004362{
4363 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004364 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004365 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08004366
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004367 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004368}
4369
4370#define MAX_HW_RESTARTS 5
4371
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004372static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004373{
Tomas Winkler57aab752008-04-14 21:16:03 -07004374 int i;
4375 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004376
4377 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4378 IWL_WARNING("Exit pending; will not bring the NIC up\n");
4379 return -EIO;
4380 }
4381
4382 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
4383 IWL_WARNING("Radio disabled by SW RF kill (module "
4384 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004385 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004386 return -ENODEV;
4387 }
4388
Reinette Chatree903fbd2008-01-30 22:05:15 -08004389 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
4390 IWL_ERROR("ucode not available for device bringup\n");
4391 return -EIO;
4392 }
4393
Zhu Yie655b9f2008-01-24 02:19:38 -08004394 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004395 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08004396 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4397 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4398 else {
4399 set_bit(STATUS_RF_KILL_HW, &priv->status);
4400 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004401 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004402 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
4403 return -ENODEV;
4404 }
Zhu Yib481de92007-09-25 17:54:57 -07004405 }
4406
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004407 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004408 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07004409
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004410 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
4411 if (ret) {
4412 IWL_ERROR("Unable to allocate shared memory\n");
4413 return ret;
4414 }
4415
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004416 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07004417 if (ret) {
4418 IWL_ERROR("Unable to init nic\n");
4419 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004420 }
4421
4422 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004423 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4424 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004425 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4426
4427 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004428 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004429 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004430
4431 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004432 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4433 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07004434
4435 /* Copy original ucode data image from disk into backup cache.
4436 * This will be used to initialize the on-board processor's
4437 * data SRAM for a clean start when the runtime program first loads. */
4438 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08004439 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07004440
Zhu Yie655b9f2008-01-24 02:19:38 -08004441 /* We return success when we resume from suspend and rf_kill is on. */
4442 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07004443 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004444
4445 for (i = 0; i < MAX_HW_RESTARTS; i++) {
4446
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004447 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004448
4449 /* load bootstrap state machine,
4450 * load bootstrap program into processor's memory,
4451 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07004452 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004453
Tomas Winkler57aab752008-04-14 21:16:03 -07004454 if (ret) {
4455 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07004456 continue;
4457 }
4458
4459 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08004460 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004461
Zhu Yib481de92007-09-25 17:54:57 -07004462 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
4463
4464 return 0;
4465 }
4466
4467 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004468 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004469
4470 /* tried to restart and config the device for as long as our
4471 * patience could withstand */
4472 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
4473 return -EIO;
4474}
4475
4476
4477/*****************************************************************************
4478 *
4479 * Workqueue callbacks
4480 *
4481 *****************************************************************************/
4482
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004483static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004484{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004485 struct iwl_priv *priv =
4486 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004487
4488 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4489 return;
4490
4491 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08004492 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004493 mutex_unlock(&priv->mutex);
4494}
4495
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004496static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004497{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004498 struct iwl_priv *priv =
4499 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004500
4501 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4502 return;
4503
4504 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004505 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004506 mutex_unlock(&priv->mutex);
4507}
4508
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004509static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004510{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004511 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07004512
4513 wake_up_interruptible(&priv->wait_command_queue);
4514
4515 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4516 return;
4517
4518 mutex_lock(&priv->mutex);
4519
Tomas Winklerfee12472008-04-03 16:05:21 -07004520 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08004521 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07004522 "HW and/or SW RF Kill no longer active, restarting "
4523 "device\n");
4524 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4525 queue_work(priv->workqueue, &priv->restart);
4526 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004527 /* make sure mac80211 stop sending Tx frame */
4528 if (priv->mac80211_registered)
4529 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004530
4531 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
4532 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
4533 "disabled by SW switch\n");
4534 else
4535 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
4536 "Kill switch must be turned off for "
4537 "wireless networking to work.\n");
4538 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004539 iwl_rfkill_set_hw_state(priv);
4540
Zhu Yib481de92007-09-25 17:54:57 -07004541 mutex_unlock(&priv->mutex);
4542}
4543
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004544static void iwl4965_bg_set_monitor(struct work_struct *work)
4545{
4546 struct iwl_priv *priv = container_of(work,
4547 struct iwl_priv, set_monitor);
4548
4549 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
4550
4551 mutex_lock(&priv->mutex);
4552
4553 if (!iwl_is_ready(priv))
4554 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
4555 else
4556 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
4557 IWL_ERROR("iwl4965_set_mode() failed\n");
4558
4559 mutex_unlock(&priv->mutex);
4560}
4561
Zhu Yib481de92007-09-25 17:54:57 -07004562#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
4563
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004564static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004565{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004566 struct iwl_priv *priv =
4567 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07004568
4569 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4570 return;
4571
4572 mutex_lock(&priv->mutex);
4573 if (test_bit(STATUS_SCANNING, &priv->status) ||
4574 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08004575 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
4576 "adapter (%dms)\n",
4577 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08004578
Zhu Yib481de92007-09-25 17:54:57 -07004579 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004580 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004581 }
4582 mutex_unlock(&priv->mutex);
4583}
4584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004585static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004586{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004587 struct iwl_priv *priv =
4588 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07004589 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07004590 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004591 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07004592 .meta.flags = CMD_SIZE_HUGE,
4593 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004594 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07004595 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004596 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01004597 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004598 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07004599 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004600
4601 conf = ieee80211_get_hw_conf(priv->hw);
4602
4603 mutex_lock(&priv->mutex);
4604
Tomas Winklerfee12472008-04-03 16:05:21 -07004605 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004606 IWL_WARNING("request scan called when driver not ready.\n");
4607 goto done;
4608 }
4609
4610 /* Make sure the scan wasn't cancelled before this queued work
4611 * was given the chance to run... */
4612 if (!test_bit(STATUS_SCANNING, &priv->status))
4613 goto done;
4614
4615 /* This should never be called or scheduled if there is currently
4616 * a scan active in the hardware. */
4617 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
4618 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
4619 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07004620 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07004621 goto done;
4622 }
4623
4624 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4625 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4626 goto done;
4627 }
4628
4629 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4630 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
4631 goto done;
4632 }
4633
Tomas Winklerfee12472008-04-03 16:05:21 -07004634 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004635 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4636 goto done;
4637 }
4638
4639 if (!test_bit(STATUS_READY, &priv->status)) {
4640 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
4641 goto done;
4642 }
4643
4644 if (!priv->scan_bands) {
4645 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
4646 goto done;
4647 }
4648
4649 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004650 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07004651 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
4652 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07004653 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004654 goto done;
4655 }
4656 }
4657 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004658 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07004659
4660 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
4661 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
4662
Tomas Winkler3109ece2008-03-28 16:33:35 -07004663 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004664 u16 interval = 0;
4665 u32 extra;
4666 u32 suspend_time = 100;
4667 u32 scan_suspend_time = 100;
4668 unsigned long flags;
4669
4670 IWL_DEBUG_INFO("Scanning while associated...\n");
4671
4672 spin_lock_irqsave(&priv->lock, flags);
4673 interval = priv->beacon_int;
4674 spin_unlock_irqrestore(&priv->lock, flags);
4675
4676 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08004677 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07004678 if (!interval)
4679 interval = suspend_time;
4680
4681 extra = (suspend_time / interval) << 22;
4682 scan_suspend_time = (extra |
4683 ((suspend_time % interval) * 1024));
4684 scan->suspend_time = cpu_to_le32(scan_suspend_time);
4685 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
4686 scan_suspend_time, interval);
4687 }
4688
4689 /* We should add the ability for user to lock to PASSIVE ONLY */
4690 if (priv->one_direct_scan) {
4691 IWL_DEBUG_SCAN
4692 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004693 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07004694 priv->direct_ssid_len));
4695 scan->direct_scan[0].id = WLAN_EID_SSID;
4696 scan->direct_scan[0].len = priv->direct_ssid_len;
4697 memcpy(scan->direct_scan[0].ssid,
4698 priv->direct_ssid, priv->direct_ssid_len);
4699 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004700 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07004701 IWL_DEBUG_SCAN
4702 ("Kicking off one direct scan for '%s' when not associated\n",
4703 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07004704 scan->direct_scan[0].id = WLAN_EID_SSID;
4705 scan->direct_scan[0].len = priv->essid_len;
4706 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
4707 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07004708 } else {
Bill Moss786b4552008-04-17 16:03:40 -07004709 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004710 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07004711 }
Zhu Yib481de92007-09-25 17:54:57 -07004712
Zhu Yib481de92007-09-25 17:54:57 -07004713 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07004714 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07004715 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4716
Zhu Yib481de92007-09-25 17:54:57 -07004717
4718 switch (priv->scan_bands) {
4719 case 2:
4720 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
4721 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004722 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004723 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
4724
4725 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004726 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004727 break;
4728
4729 case 1:
4730 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004731 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004732 RATE_MCS_ANT_B_MSK);
4733 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01004734 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004735 break;
4736
4737 default:
4738 IWL_WARNING("Invalid scan band count\n");
4739 goto done;
4740 }
4741
Tomas Winkler78330fd2008-02-06 02:37:18 +02004742 /* We don't build a direct scan probe request; the uCode will do
4743 * that based on the direct_mask added to each channel entry */
4744 cmd_len = iwl4965_fill_probe_req(priv, band,
4745 (struct ieee80211_mgmt *)scan->data,
4746 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
4747
4748 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07004749 /* select Rx chains */
4750
4751 /* Force use of chains B and C (0x6) for scan Rx.
4752 * Avoid A (0x1) because of its off-channel reception on A-band.
4753 * MIMO is not used here, but value is required to make uCode happy. */
4754 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
4755 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
4756 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
4757 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
4758
4759 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
4760 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
4761
Bill Moss786b4552008-04-17 16:03:40 -07004762 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07004763 scan->channel_count =
4764 iwl4965_get_channels_for_scan(
4765 priv, band, 1, /* active */
4766 direct_mask,
4767 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07004768 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07004769 scan->channel_count =
4770 iwl4965_get_channels_for_scan(
4771 priv, band, 0, /* passive */
4772 direct_mask,
4773 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07004774
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004775 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
4776 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07004777 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07004779 cmd.data = scan;
4780 scan->len = cpu_to_le16(cmd.len);
4781
4782 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07004783 ret = iwl_send_cmd_sync(priv, &cmd);
4784 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004785 goto done;
4786
4787 queue_delayed_work(priv->workqueue, &priv->scan_check,
4788 IWL_SCAN_CHECK_WATCHDOG);
4789
4790 mutex_unlock(&priv->mutex);
4791 return;
4792
4793 done:
Ian Schram01ebd062007-10-25 17:15:22 +08004794 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07004795 queue_work(priv->workqueue, &priv->scan_completed);
4796 mutex_unlock(&priv->mutex);
4797}
4798
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004799static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004800{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004801 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07004802
4803 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4804 return;
4805
4806 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004807 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004808 mutex_unlock(&priv->mutex);
4809}
4810
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004811static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004812{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004813 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07004814
4815 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4816 return;
4817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004818 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004819 queue_work(priv->workqueue, &priv->up);
4820}
4821
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004822static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004823{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004824 struct iwl_priv *priv =
4825 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07004826
4827 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4828 return;
4829
4830 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08004831 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004832 mutex_unlock(&priv->mutex);
4833}
4834
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004835#define IWL_DELAY_NEXT_SCAN (HZ*2)
4836
Reinette Chatre508e32e2008-04-14 21:16:13 -07004837static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004838{
Zhu Yib481de92007-09-25 17:54:57 -07004839 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07004840 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07004841 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004842
4843 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4844 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
4845 return;
4846 }
4847
Joe Perches0795af52007-10-03 17:59:30 -07004848 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
4849 priv->assoc_id,
4850 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004851
4852
4853 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4854 return;
4855
Zhu Yib481de92007-09-25 17:54:57 -07004856
Reinette Chatre508e32e2008-04-14 21:16:13 -07004857 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08004858 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004859
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004860 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08004861
Zhu Yib481de92007-09-25 17:54:57 -07004862 conf = ieee80211_get_hw_conf(priv->hw);
4863
4864 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004865 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004867 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4868 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07004869 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07004870 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07004871 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004872 IWL_WARNING("REPLY_RXON_TIMING failed - "
4873 "Attempting to continue.\n");
4874
4875 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4876
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004877#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004878 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08004879 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004880#endif /* CONFIG_IWL4965_HT*/
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004881 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004882 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4883
4884 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
4885 priv->assoc_id, priv->beacon_int);
4886
4887 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4888 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4889 else
4890 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4891
4892 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4893 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
4894 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
4895 else
4896 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4897
4898 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4899 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4900
4901 }
4902
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004903 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004904
4905 switch (priv->iw_mode) {
4906 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004907 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07004908 break;
4909
4910 case IEEE80211_IF_TYPE_IBSS:
4911
4912 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004913 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004914
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004915 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
4916 iwl4965_rxon_add_station(priv, priv->bssid, 0);
4917 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
4918 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004919
4920 break;
4921
4922 default:
4923 IWL_ERROR("%s Should not be called in %d mode\n",
4924 __FUNCTION__, priv->iw_mode);
4925 break;
4926 }
4927
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004928 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004929
Zhu Yib481de92007-09-25 17:54:57 -07004930 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004931 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004932 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004933
4934 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4935 priv->assoc_station_added = 1;
4936
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004937 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08004938
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004939 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004940 /* we have just associated, don't start scan too early */
4941 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004942}
4943
4944
4945static void iwl4965_bg_post_associate(struct work_struct *data)
4946{
4947 struct iwl_priv *priv = container_of(data, struct iwl_priv,
4948 post_associate.work);
4949
4950 mutex_lock(&priv->mutex);
4951 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004952 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07004953
Zhu Yib481de92007-09-25 17:54:57 -07004954}
4955
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004956static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004957{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004958 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07004959
Tomas Winklerfee12472008-04-03 16:05:21 -07004960 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004961 return;
4962
4963 mutex_lock(&priv->mutex);
4964
4965 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004966 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004967
4968 mutex_unlock(&priv->mutex);
4969}
4970
Zhu Yi76bb77e2007-11-22 10:53:22 +08004971static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
4972
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004973static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004974{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004975 struct iwl_priv *priv =
4976 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07004977
Ester Kummerf3d67992008-05-06 11:05:12 +08004978 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07004979
4980 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4981 return;
4982
Zhu Yia0646472007-12-20 14:10:01 +08004983 if (test_bit(STATUS_CONF_PENDING, &priv->status))
4984 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08004985
Zhu Yib481de92007-09-25 17:54:57 -07004986 ieee80211_scan_completed(priv->hw);
4987
4988 /* Since setting the TXPOWER may have been deferred while
4989 * performing the scan, fire one off */
4990 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004991 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004992 mutex_unlock(&priv->mutex);
4993}
4994
4995/*****************************************************************************
4996 *
4997 * mac80211 entry point functions
4998 *
4999 *****************************************************************************/
5000
Zhu Yi5a66926a2008-01-14 17:46:18 -08005001#define UCODE_READY_TIMEOUT (2 * HZ)
5002
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005003static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005004{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005005 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005006 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005007
5008 IWL_DEBUG_MAC80211("enter\n");
5009
Zhu Yi5a66926a2008-01-14 17:46:18 -08005010 if (pci_enable_device(priv->pci_dev)) {
5011 IWL_ERROR("Fail to pci_enable_device\n");
5012 return -ENODEV;
5013 }
5014 pci_restore_state(priv->pci_dev);
5015 pci_enable_msi(priv->pci_dev);
5016
5017 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
5018 DRV_NAME, priv);
5019 if (ret) {
5020 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
5021 goto out_disable_msi;
5022 }
5023
Zhu Yib481de92007-09-25 17:54:57 -07005024 /* we should be verifying the device is ready to be opened */
5025 mutex_lock(&priv->mutex);
5026
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08005027 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08005028 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5029 * ucode filename and max sizes are card-specific. */
5030
5031 if (!priv->ucode_code.len) {
5032 ret = iwl4965_read_ucode(priv);
5033 if (ret) {
5034 IWL_ERROR("Could not read microcode: %d\n", ret);
5035 mutex_unlock(&priv->mutex);
5036 goto out_release_irq;
5037 }
5038 }
5039
Zhu Yie655b9f2008-01-24 02:19:38 -08005040 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005041
Zhu Yib481de92007-09-25 17:54:57 -07005042 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005043
Zhu Yie655b9f2008-01-24 02:19:38 -08005044 if (ret)
5045 goto out_release_irq;
5046
5047 IWL_DEBUG_INFO("Start UP work done.\n");
5048
5049 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5050 return 0;
5051
Zhu Yi5a66926a2008-01-14 17:46:18 -08005052 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5053 * mac80211 will not be run successfully. */
5054 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5055 test_bit(STATUS_READY, &priv->status),
5056 UCODE_READY_TIMEOUT);
5057 if (!ret) {
5058 if (!test_bit(STATUS_READY, &priv->status)) {
5059 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
5060 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5061 ret = -ETIMEDOUT;
5062 goto out_release_irq;
5063 }
5064 }
5065
Zhu Yie655b9f2008-01-24 02:19:38 -08005066 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005067 IWL_DEBUG_MAC80211("leave\n");
5068 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005069
5070out_release_irq:
5071 free_irq(priv->pci_dev->irq, priv);
5072out_disable_msi:
5073 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005074 pci_disable_device(priv->pci_dev);
5075 priv->is_open = 0;
5076 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005077 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005078}
5079
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005080static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005081{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005082 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005083
5084 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08005085
Zhu Yie655b9f2008-01-24 02:19:38 -08005086 if (!priv->is_open) {
5087 IWL_DEBUG_MAC80211("leave - skip\n");
5088 return;
5089 }
5090
Zhu Yib481de92007-09-25 17:54:57 -07005091 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005092
Tomas Winklerfee12472008-04-03 16:05:21 -07005093 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005094 /* stop mac, cancel any scan request and clear
5095 * RXON_FILTER_ASSOC_MSK BIT
5096 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08005097 mutex_lock(&priv->mutex);
5098 iwl4965_scan_cancel_timeout(priv, 100);
5099 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005100 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005101 }
5102
Zhu Yi5a66926a2008-01-14 17:46:18 -08005103 iwl4965_down(priv);
5104
5105 flush_workqueue(priv->workqueue);
5106 free_irq(priv->pci_dev->irq, priv);
5107 pci_disable_msi(priv->pci_dev);
5108 pci_save_state(priv->pci_dev);
5109 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005110
Zhu Yib481de92007-09-25 17:54:57 -07005111 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005112}
5113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005114static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005115 struct ieee80211_tx_control *ctl)
5116{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005117 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005118
5119 IWL_DEBUG_MAC80211("enter\n");
5120
5121 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
5122 IWL_DEBUG_MAC80211("leave - monitor\n");
5123 return -1;
5124 }
5125
5126 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01005127 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07005128
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005129 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07005130 dev_kfree_skb_any(skb);
5131
5132 IWL_DEBUG_MAC80211("leave\n");
5133 return 0;
5134}
5135
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005136static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005137 struct ieee80211_if_init_conf *conf)
5138{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005139 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005140 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07005141 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005142
Johannes Berg32bfd352007-12-19 01:31:26 +01005143 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005144
Johannes Berg32bfd352007-12-19 01:31:26 +01005145 if (priv->vif) {
5146 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08005147 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07005148 }
5149
5150 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01005151 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07005152
5153 spin_unlock_irqrestore(&priv->lock, flags);
5154
5155 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02005156
5157 if (conf->mac_addr) {
5158 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
5159 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
5160 }
Zhu Yib481de92007-09-25 17:54:57 -07005161
Tomas Winklerfee12472008-04-03 16:05:21 -07005162 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005163 iwl4965_set_mode(priv, conf->type);
5164
Zhu Yib481de92007-09-25 17:54:57 -07005165 mutex_unlock(&priv->mutex);
5166
Zhu Yi5a66926a2008-01-14 17:46:18 -08005167 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005168 return 0;
5169}
5170
5171/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005172 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07005173 *
5174 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5175 * be set inappropriately and the driver currently sets the hardware up to
5176 * use it whenever needed.
5177 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005178static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07005179{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005180 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005181 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005182 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08005183 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005184
5185 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01005186 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005187
Zhu Yi12342c42007-12-20 11:27:32 +08005188 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
5189
Tomas Winklerfee12472008-04-03 16:05:21 -07005190 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005191 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005192 ret = -EIO;
5193 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005194 }
5195
Assaf Krauss1ea87392008-03-18 14:57:50 -07005196 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07005197 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08005198 IWL_DEBUG_MAC80211("leave - scanning\n");
5199 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005200 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08005201 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005202 }
5203
5204 spin_lock_irqsave(&priv->lock, flags);
5205
Assaf Krauss8622e702008-03-21 13:53:43 -07005206 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005207 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005208 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07005209 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5210 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005211 ret = -EINVAL;
5212 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005213 }
5214
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005215#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02005216 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07005217 * from any ht related info since 2.4 does not
5218 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02005219 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07005220#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5221 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
5222#endif
5223 )
5224 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005225#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005226
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005227 iwl_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005228 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005229
Johannes Berg8318d782008-01-24 19:38:38 +01005230 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07005231
5232 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01005233 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07005234 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005235 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005236
5237 spin_unlock_irqrestore(&priv->lock, flags);
5238
5239#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5240 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005241 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005242 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005243 }
5244#endif
5245
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005246 if (priv->cfg->ops->lib->radio_kill_sw)
5247 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07005248
5249 if (!conf->radio_enabled) {
5250 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005251 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005252 }
5253
Tomas Winklerfee12472008-04-03 16:05:21 -07005254 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005255 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005256 ret = -EIO;
5257 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005258 }
5259
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005260 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005261
5262 if (memcmp(&priv->active_rxon,
5263 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005264 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005265 else
5266 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
5267
5268 IWL_DEBUG_MAC80211("leave\n");
5269
Zhu Yia0646472007-12-20 14:10:01 +08005270out:
5271 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005272 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005273 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005274}
5275
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005276static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005277{
Tomas Winkler857485c2008-03-21 13:53:44 -07005278 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005279
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08005280 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005281 return;
5282
5283 /* The following should be done only at AP bring up */
5284 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
5285
5286 /* RXON - unassoc (to set timing command) */
5287 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005288 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005289
5290 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005291 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5292 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005293 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005294 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005295 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005296 IWL_WARNING("REPLY_RXON_TIMING failed - "
5297 "Attempting to continue.\n");
5298
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005299 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005300
5301 /* FIXME: what should be the assoc_id for AP? */
5302 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5303 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5304 priv->staging_rxon.flags |=
5305 RXON_FLG_SHORT_PREAMBLE_MSK;
5306 else
5307 priv->staging_rxon.flags &=
5308 ~RXON_FLG_SHORT_PREAMBLE_MSK;
5309
5310 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5311 if (priv->assoc_capability &
5312 WLAN_CAPABILITY_SHORT_SLOT_TIME)
5313 priv->staging_rxon.flags |=
5314 RXON_FLG_SHORT_SLOT_MSK;
5315 else
5316 priv->staging_rxon.flags &=
5317 ~RXON_FLG_SHORT_SLOT_MSK;
5318
5319 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5320 priv->staging_rxon.flags &=
5321 ~RXON_FLG_SHORT_SLOT_MSK;
5322 }
5323 /* restore RXON assoc */
5324 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005325 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005326 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005327 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08005328 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005329 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005330
5331 /* FIXME - we need to add code here to detect a totally new
5332 * configuration, reset the AP, unassoc, rxon timing, assoc,
5333 * clear sta table, add BCAST sta... */
5334}
5335
Johannes Berg32bfd352007-12-19 01:31:26 +01005336static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
5337 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07005338 struct ieee80211_if_conf *conf)
5339{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005340 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005341 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005342 unsigned long flags;
5343 int rc;
5344
5345 if (conf == NULL)
5346 return -EIO;
5347
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005348 if (priv->vif != vif) {
5349 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005350 return 0;
5351 }
5352
Zhu Yib481de92007-09-25 17:54:57 -07005353 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
5354 (!conf->beacon || !conf->ssid_len)) {
5355 IWL_DEBUG_MAC80211
5356 ("Leaving in AP mode because HostAPD is not ready.\n");
5357 return 0;
5358 }
5359
Tomas Winklerfee12472008-04-03 16:05:21 -07005360 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005361 return -EAGAIN;
5362
Zhu Yib481de92007-09-25 17:54:57 -07005363 mutex_lock(&priv->mutex);
5364
Zhu Yib481de92007-09-25 17:54:57 -07005365 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07005366 IWL_DEBUG_MAC80211("bssid: %s\n",
5367 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005368
Johannes Berg4150c572007-09-17 01:29:23 -04005369/*
5370 * very dubious code was here; the probe filtering flag is never set:
5371 *
Zhu Yib481de92007-09-25 17:54:57 -07005372 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
5373 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04005374 */
Zhu Yib481de92007-09-25 17:54:57 -07005375
5376 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5377 if (!conf->bssid) {
5378 conf->bssid = priv->mac_addr;
5379 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07005380 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
5381 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005382 }
5383 if (priv->ibss_beacon)
5384 dev_kfree_skb(priv->ibss_beacon);
5385
5386 priv->ibss_beacon = conf->beacon;
5387 }
5388
Tomas Winklerfee12472008-04-03 16:05:21 -07005389 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08005390 goto done;
5391
Zhu Yib481de92007-09-25 17:54:57 -07005392 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
5393 !is_multicast_ether_addr(conf->bssid)) {
5394 /* If there is currently a HW scan going on in the background
5395 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005396 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07005397 IWL_WARNING("Aborted scan still in progress "
5398 "after 100ms\n");
5399 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5400 mutex_unlock(&priv->mutex);
5401 return -EAGAIN;
5402 }
5403 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
5404
5405 /* TODO: Audit driver for usage of these members and see
5406 * if mac80211 deprecates them (priv->bssid looks like it
5407 * shouldn't be there, but I haven't scanned the IBSS code
5408 * to verify) - jpk */
5409 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5410
5411 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005412 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005413 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005414 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005415 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005416 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07005417 priv, priv->active_rxon.bssid_addr, 1);
5418 }
5419
5420 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005421 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07005422 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005423 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005424 }
5425
Mohamed Abbasfde35712007-11-29 11:10:15 +08005426 done:
Zhu Yib481de92007-09-25 17:54:57 -07005427 spin_lock_irqsave(&priv->lock, flags);
5428 if (!conf->ssid_len)
5429 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5430 else
5431 memcpy(priv->essid, conf->ssid, conf->ssid_len);
5432
5433 priv->essid_len = conf->ssid_len;
5434 spin_unlock_irqrestore(&priv->lock, flags);
5435
5436 IWL_DEBUG_MAC80211("leave\n");
5437 mutex_unlock(&priv->mutex);
5438
5439 return 0;
5440}
5441
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005442static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04005443 unsigned int changed_flags,
5444 unsigned int *total_flags,
5445 int mc_count, struct dev_addr_list *mc_list)
5446{
5447 /*
5448 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005449 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04005450 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08005451 struct iwl_priv *priv = hw->priv;
5452 int new_flags = 0;
5453 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5454 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5455 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
5456 IEEE80211_IF_TYPE_MNTR,
5457 changed_flags, *total_flags);
5458 /* queue work 'cuz mac80211 is holding a lock which
5459 * prevents us from issuing (synchronous) f/w cmds */
5460 queue_work(priv->workqueue, &priv->set_monitor);
5461 new_flags &= FIF_PROMISC_IN_BSS |
5462 FIF_OTHER_BSS |
5463 FIF_ALLMULTI;
5464 }
5465 }
5466 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04005467}
5468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005469static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005470 struct ieee80211_if_init_conf *conf)
5471{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005472 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005473
5474 IWL_DEBUG_MAC80211("enter\n");
5475
5476 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005477
Tomas Winklerfee12472008-04-03 16:05:21 -07005478 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005479 iwl4965_scan_cancel_timeout(priv, 100);
5480 cancel_delayed_work(&priv->post_associate);
5481 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5482 iwl4965_commit_rxon(priv);
5483 }
Johannes Berg32bfd352007-12-19 01:31:26 +01005484 if (priv->vif == conf->vif) {
5485 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005486 memset(priv->bssid, 0, ETH_ALEN);
5487 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5488 priv->essid_len = 0;
5489 }
5490 mutex_unlock(&priv->mutex);
5491
5492 IWL_DEBUG_MAC80211("leave\n");
5493
5494}
Johannes Berg471b3ef2007-12-28 14:32:58 +01005495
Tomas Winkler3109ece2008-03-28 16:33:35 -07005496#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01005497static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
5498 struct ieee80211_vif *vif,
5499 struct ieee80211_bss_conf *bss_conf,
5500 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02005501{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005502 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02005503
Tomas Winkler3109ece2008-03-28 16:33:35 -07005504 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
5505
Johannes Berg471b3ef2007-12-28 14:32:58 +01005506 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005507 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
5508 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005509 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02005510 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5511 else
5512 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5513 }
5514
Johannes Berg471b3ef2007-12-28 14:32:58 +01005515 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005516 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01005517 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02005518 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
5519 else
5520 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
5521 }
5522
Tomas Winkler98952d52008-03-28 16:33:33 -07005523 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005524 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07005525 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005526 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07005527 }
5528
Johannes Berg471b3ef2007-12-28 14:32:58 +01005529 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005530 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005531 /* This should never happen as this function should
5532 * never be called from interrupt context. */
5533 if (WARN_ON_ONCE(in_interrupt()))
5534 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005535 if (bss_conf->assoc) {
5536 priv->assoc_id = bss_conf->aid;
5537 priv->beacon_int = bss_conf->beacon_int;
5538 priv->timestamp = bss_conf->timestamp;
5539 priv->assoc_capability = bss_conf->assoc_capability;
5540 priv->next_scan_jiffies = jiffies +
5541 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005542 mutex_lock(&priv->mutex);
5543 iwl4965_post_associate(priv);
5544 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005545 } else {
5546 priv->assoc_id = 0;
5547 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
5548 }
5549 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
5550 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07005551 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005552 }
5553
Tomas Winkler220173b2007-10-16 00:50:25 +02005554}
Zhu Yib481de92007-09-25 17:54:57 -07005555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005556static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07005557{
5558 int rc = 0;
5559 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005560 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005561
5562 IWL_DEBUG_MAC80211("enter\n");
5563
mabbas052c4b92007-10-25 17:15:43 +08005564 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005565 spin_lock_irqsave(&priv->lock, flags);
5566
Tomas Winklerfee12472008-04-03 16:05:21 -07005567 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005568 rc = -EIO;
5569 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
5570 goto out_unlock;
5571 }
5572
5573 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
5574 rc = -EIO;
5575 IWL_ERROR("ERROR: APs don't scan\n");
5576 goto out_unlock;
5577 }
5578
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005579 /* we don't schedule scan within next_scan_jiffies period */
5580 if (priv->next_scan_jiffies &&
5581 time_after(priv->next_scan_jiffies, jiffies)) {
5582 rc = -EAGAIN;
5583 goto out_unlock;
5584 }
Zhu Yib481de92007-09-25 17:54:57 -07005585 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005586 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
5587 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07005588 rc = -EAGAIN;
5589 goto out_unlock;
5590 }
5591 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005592 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005593 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07005594
5595 priv->one_direct_scan = 1;
5596 priv->direct_ssid_len = (u8)
5597 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
5598 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005599 } else
5600 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005601
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005602 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005603
5604 IWL_DEBUG_MAC80211("leave\n");
5605
5606out_unlock:
5607 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08005608 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005609
5610 return rc;
5611}
5612
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005613static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
5614 struct ieee80211_key_conf *keyconf, const u8 *addr,
5615 u32 iv32, u16 *phase1key)
5616{
5617 struct iwl_priv *priv = hw->priv;
5618 u8 sta_id = IWL_INVALID_STATION;
5619 unsigned long flags;
5620 __le16 key_flags = 0;
5621 int i;
5622 DECLARE_MAC_BUF(mac);
5623
5624 IWL_DEBUG_MAC80211("enter\n");
5625
Tomas Winkler947b13a2008-04-16 16:34:48 -07005626 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005627 if (sta_id == IWL_INVALID_STATION) {
5628 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5629 print_mac(mac, addr));
5630 return;
5631 }
5632
5633 iwl4965_scan_cancel_timeout(priv, 100);
5634
5635 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
5636 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
5637 key_flags &= ~STA_KEY_FLG_INVALID;
5638
Tomas Winkler5425e492008-04-15 16:01:38 -07005639 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005640 key_flags |= STA_KEY_MULTICAST_MSK;
5641
5642 spin_lock_irqsave(&priv->sta_lock, flags);
5643
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005644 priv->stations[sta_id].sta.key.key_flags = key_flags;
5645 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
5646
5647 for (i = 0; i < 5; i++)
5648 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
5649 cpu_to_le16(phase1key[i]);
5650
5651 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
5652 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
5653
Tomas Winkler133636d2008-05-05 10:22:34 +08005654 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005655
5656 spin_unlock_irqrestore(&priv->sta_lock, flags);
5657
5658 IWL_DEBUG_MAC80211("leave\n");
5659}
5660
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005661static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07005662 const u8 *local_addr, const u8 *addr,
5663 struct ieee80211_key_conf *key)
5664{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005665 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005666 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005667 int ret = 0;
5668 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005669 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005670
5671 IWL_DEBUG_MAC80211("enter\n");
5672
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07005673 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07005674 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
5675 return -EOPNOTSUPP;
5676 }
5677
5678 if (is_zero_ether_addr(addr))
5679 /* only support pairwise keys */
5680 return -EOPNOTSUPP;
5681
Tomas Winkler947b13a2008-04-16 16:34:48 -07005682 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005683 if (sta_id == IWL_INVALID_STATION) {
5684 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5685 print_mac(mac, addr));
5686 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005687
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005688 }
Zhu Yib481de92007-09-25 17:54:57 -07005689
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005690 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005691 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005692 mutex_unlock(&priv->mutex);
5693
5694 /* If we are getting WEP group key and we didn't receive any key mapping
5695 * so far, we are in legacy wep mode (group key only), otherwise we are
5696 * in 1X mode.
5697 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07005698 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005699 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
5700 if (cmd == SET_KEY)
5701 is_default_wep_key = !priv->key_mapping_key;
5702 else
5703 is_default_wep_key = priv->default_wep_key;
5704 }
mabbas052c4b92007-10-25 17:15:43 +08005705
Zhu Yib481de92007-09-25 17:54:57 -07005706 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005707 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005708 if (is_default_wep_key)
5709 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005710 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07005711 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005712
5713 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005714 break;
5715 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005716 if (is_default_wep_key)
5717 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005718 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07005719 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005720
5721 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005722 break;
5723 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005724 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005725 }
5726
5727 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005728
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005729 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005730}
5731
Johannes Berge100bb62008-04-30 18:51:21 +02005732static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07005733 const struct ieee80211_tx_queue_params *params)
5734{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005735 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005736 unsigned long flags;
5737 int q;
Zhu Yib481de92007-09-25 17:54:57 -07005738
5739 IWL_DEBUG_MAC80211("enter\n");
5740
Tomas Winklerfee12472008-04-03 16:05:21 -07005741 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005742 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5743 return -EIO;
5744 }
5745
5746 if (queue >= AC_NUM) {
5747 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5748 return 0;
5749 }
5750
Zhu Yib481de92007-09-25 17:54:57 -07005751 if (!priv->qos_data.qos_enable) {
5752 priv->qos_data.qos_active = 0;
5753 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
5754 return 0;
5755 }
5756 q = AC_NUM - 1 - queue;
5757
5758 spin_lock_irqsave(&priv->lock, flags);
5759
5760 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5761 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5762 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5763 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01005764 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07005765
5766 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
5767 priv->qos_data.qos_active = 1;
5768
5769 spin_unlock_irqrestore(&priv->lock, flags);
5770
5771 mutex_lock(&priv->mutex);
5772 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005773 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005774 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005775 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005776
5777 mutex_unlock(&priv->mutex);
5778
Zhu Yib481de92007-09-25 17:54:57 -07005779 IWL_DEBUG_MAC80211("leave\n");
5780 return 0;
5781}
5782
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005783static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005784 struct ieee80211_tx_queue_stats *stats)
5785{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005786 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005787 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08005788 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08005789 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07005790 unsigned long flags;
5791
5792 IWL_DEBUG_MAC80211("enter\n");
5793
Tomas Winklerfee12472008-04-03 16:05:21 -07005794 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005795 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5796 return -EIO;
5797 }
5798
5799 spin_lock_irqsave(&priv->lock, flags);
5800
5801 for (i = 0; i < AC_NUM; i++) {
5802 txq = &priv->txq[i];
5803 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08005804 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07005805
Johannes Berg57ffc582008-04-29 17:18:59 +02005806 stats[i].len = q->n_window - avail;
5807 stats[i].limit = q->n_window - q->high_mark;
5808 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07005809
5810 }
5811 spin_unlock_irqrestore(&priv->lock, flags);
5812
5813 IWL_DEBUG_MAC80211("leave\n");
5814
5815 return 0;
5816}
5817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005818static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005819 struct ieee80211_low_level_stats *stats)
5820{
Ester Kummerbf403db2008-05-05 10:22:40 +08005821 struct iwl_priv *priv = hw->priv;
5822
5823 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005824 IWL_DEBUG_MAC80211("enter\n");
5825 IWL_DEBUG_MAC80211("leave\n");
5826
5827 return 0;
5828}
5829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005830static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005831{
Ester Kummerbf403db2008-05-05 10:22:40 +08005832 struct iwl_priv *priv;
5833
5834 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005835 IWL_DEBUG_MAC80211("enter\n");
5836 IWL_DEBUG_MAC80211("leave\n");
5837
5838 return 0;
5839}
5840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005841static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005842{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005843 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005844 unsigned long flags;
5845
5846 mutex_lock(&priv->mutex);
5847 IWL_DEBUG_MAC80211("enter\n");
5848
5849 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005850#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07005851 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005852 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07005853 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005854#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005855
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005856 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005857
5858 cancel_delayed_work(&priv->post_associate);
5859
5860 spin_lock_irqsave(&priv->lock, flags);
5861 priv->assoc_id = 0;
5862 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005863 priv->assoc_station_added = 0;
5864
5865 /* new association get rid of ibss beacon skb */
5866 if (priv->ibss_beacon)
5867 dev_kfree_skb(priv->ibss_beacon);
5868
5869 priv->ibss_beacon = NULL;
5870
5871 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005872 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005873 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
5874 priv->beacon_int = 0;
5875
5876 spin_unlock_irqrestore(&priv->lock, flags);
5877
Tomas Winklerfee12472008-04-03 16:05:21 -07005878 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005879 IWL_DEBUG_MAC80211("leave - not ready\n");
5880 mutex_unlock(&priv->mutex);
5881 return;
5882 }
5883
mabbas052c4b92007-10-25 17:15:43 +08005884 /* we are restarting association process
5885 * clear RXON_FILTER_ASSOC_MSK bit
5886 */
5887 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005888 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08005889 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005890 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08005891 }
5892
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005893 iwl_power_update_mode(priv, 0);
5894
Zhu Yib481de92007-09-25 17:54:57 -07005895 /* Per mac80211.h: This is only used in IBSS mode... */
5896 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08005897
Zhu Yib481de92007-09-25 17:54:57 -07005898 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
5899 mutex_unlock(&priv->mutex);
5900 return;
5901 }
5902
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005903 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005904
5905 mutex_unlock(&priv->mutex);
5906
5907 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005908}
5909
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005910static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005911 struct ieee80211_tx_control *control)
5912{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005913 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005914 unsigned long flags;
5915
5916 mutex_lock(&priv->mutex);
5917 IWL_DEBUG_MAC80211("enter\n");
5918
Tomas Winklerfee12472008-04-03 16:05:21 -07005919 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005920 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5921 mutex_unlock(&priv->mutex);
5922 return -EIO;
5923 }
5924
5925 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
5926 IWL_DEBUG_MAC80211("leave - not IBSS\n");
5927 mutex_unlock(&priv->mutex);
5928 return -EIO;
5929 }
5930
5931 spin_lock_irqsave(&priv->lock, flags);
5932
5933 if (priv->ibss_beacon)
5934 dev_kfree_skb(priv->ibss_beacon);
5935
5936 priv->ibss_beacon = skb;
5937
5938 priv->assoc_id = 0;
5939
5940 IWL_DEBUG_MAC80211("leave\n");
5941 spin_unlock_irqrestore(&priv->lock, flags);
5942
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005943 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005944
5945 queue_work(priv->workqueue, &priv->post_associate.work);
5946
5947 mutex_unlock(&priv->mutex);
5948
5949 return 0;
5950}
5951
Zhu Yib481de92007-09-25 17:54:57 -07005952/*****************************************************************************
5953 *
5954 * sysfs attributes
5955 *
5956 *****************************************************************************/
5957
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005958#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07005959
5960/*
5961 * The following adds a new attribute to the sysfs representation
5962 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
5963 * used for controlling the debug level.
5964 *
5965 * See the level definitions in iwl for details.
5966 */
5967
Ester Kummer8cf769c2008-05-06 11:05:11 +08005968static ssize_t show_debug_level(struct device *d,
5969 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07005970{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005971 struct iwl_priv *priv = d->driver_data;
5972
5973 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07005974}
Ester Kummer8cf769c2008-05-06 11:05:11 +08005975static ssize_t store_debug_level(struct device *d,
5976 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07005977 const char *buf, size_t count)
5978{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005979 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005980 char *p = (char *)buf;
5981 u32 val;
5982
5983 val = simple_strtoul(p, &p, 0);
5984 if (p == buf)
5985 printk(KERN_INFO DRV_NAME
5986 ": %s is not in hex or decimal form.\n", buf);
5987 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08005988 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07005989
5990 return strnlen(buf, count);
5991}
5992
Ester Kummer8cf769c2008-05-06 11:05:11 +08005993static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5994 show_debug_level, store_debug_level);
5995
Zhu Yib481de92007-09-25 17:54:57 -07005996
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005997#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07005998
Zhu Yib481de92007-09-25 17:54:57 -07005999
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08006000static ssize_t show_version(struct device *d,
6001 struct device_attribute *attr, char *buf)
6002{
6003 struct iwl_priv *priv = d->driver_data;
6004 struct iwl4965_alive_resp *palive = &priv->card_alive;
6005
6006 if (palive->is_valid)
6007 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
6008 "fw type: 0x%01X 0x%01X\n",
6009 palive->ucode_major, palive->ucode_minor,
6010 palive->sw_rev[0], palive->sw_rev[1],
6011 palive->ver_type, palive->ver_subtype);
6012
6013 else
6014 return sprintf(buf, "fw not loaded\n");
6015}
6016
6017static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
6018
Zhu Yib481de92007-09-25 17:54:57 -07006019static ssize_t show_temperature(struct device *d,
6020 struct device_attribute *attr, char *buf)
6021{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006022 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006023
Tomas Winklerfee12472008-04-03 16:05:21 -07006024 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006025 return -EAGAIN;
6026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006027 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006028}
6029
6030static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6031
6032static ssize_t show_rs_window(struct device *d,
6033 struct device_attribute *attr,
6034 char *buf)
6035{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006036 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006037 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006038}
6039static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
6040
6041static ssize_t show_tx_power(struct device *d,
6042 struct device_attribute *attr, char *buf)
6043{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006044 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006045 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6046}
6047
6048static ssize_t store_tx_power(struct device *d,
6049 struct device_attribute *attr,
6050 const char *buf, size_t count)
6051{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006052 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006053 char *p = (char *)buf;
6054 u32 val;
6055
6056 val = simple_strtoul(p, &p, 10);
6057 if (p == buf)
6058 printk(KERN_INFO DRV_NAME
6059 ": %s is not in decimal form.\n", buf);
6060 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006061 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006062
6063 return count;
6064}
6065
6066static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6067
6068static ssize_t show_flags(struct device *d,
6069 struct device_attribute *attr, char *buf)
6070{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006071 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006072
6073 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
6074}
6075
6076static ssize_t store_flags(struct device *d,
6077 struct device_attribute *attr,
6078 const char *buf, size_t count)
6079{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006080 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006081 u32 flags = simple_strtoul(buf, NULL, 0);
6082
6083 mutex_lock(&priv->mutex);
6084 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
6085 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006086 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006087 IWL_WARNING("Could not cancel scan.\n");
6088 else {
6089 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6090 flags);
6091 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006092 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006093 }
6094 }
6095 mutex_unlock(&priv->mutex);
6096
6097 return count;
6098}
6099
6100static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6101
6102static ssize_t show_filter_flags(struct device *d,
6103 struct device_attribute *attr, char *buf)
6104{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006105 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006106
6107 return sprintf(buf, "0x%04X\n",
6108 le32_to_cpu(priv->active_rxon.filter_flags));
6109}
6110
6111static ssize_t store_filter_flags(struct device *d,
6112 struct device_attribute *attr,
6113 const char *buf, size_t count)
6114{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006115 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006116 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6117
6118 mutex_lock(&priv->mutex);
6119 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
6120 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006121 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006122 IWL_WARNING("Could not cancel scan.\n");
6123 else {
6124 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6125 "0x%04X\n", filter_flags);
6126 priv->staging_rxon.filter_flags =
6127 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006128 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006129 }
6130 }
6131 mutex_unlock(&priv->mutex);
6132
6133 return count;
6134}
6135
6136static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6137 store_filter_flags);
6138
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006139#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006140
6141static ssize_t show_measurement(struct device *d,
6142 struct device_attribute *attr, char *buf)
6143{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006144 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006145 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006146 u32 size = sizeof(measure_report), len = 0, ofs = 0;
6147 u8 *data = (u8 *) & measure_report;
6148 unsigned long flags;
6149
6150 spin_lock_irqsave(&priv->lock, flags);
6151 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6152 spin_unlock_irqrestore(&priv->lock, flags);
6153 return 0;
6154 }
6155 memcpy(&measure_report, &priv->measure_report, size);
6156 priv->measurement_status = 0;
6157 spin_unlock_irqrestore(&priv->lock, flags);
6158
6159 while (size && (PAGE_SIZE - len)) {
6160 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6161 PAGE_SIZE - len, 1);
6162 len = strlen(buf);
6163 if (PAGE_SIZE - len)
6164 buf[len++] = '\n';
6165
6166 ofs += 16;
6167 size -= min(size, 16U);
6168 }
6169
6170 return len;
6171}
6172
6173static ssize_t store_measurement(struct device *d,
6174 struct device_attribute *attr,
6175 const char *buf, size_t count)
6176{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006177 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006178 struct ieee80211_measurement_params params = {
6179 .channel = le16_to_cpu(priv->active_rxon.channel),
6180 .start_time = cpu_to_le64(priv->last_tsf),
6181 .duration = cpu_to_le16(1),
6182 };
6183 u8 type = IWL_MEASURE_BASIC;
6184 u8 buffer[32];
6185 u8 channel;
6186
6187 if (count) {
6188 char *p = buffer;
6189 strncpy(buffer, buf, min(sizeof(buffer), count));
6190 channel = simple_strtoul(p, NULL, 0);
6191 if (channel)
6192 params.channel = channel;
6193
6194 p = buffer;
6195 while (*p && *p != ' ')
6196 p++;
6197 if (*p)
6198 type = simple_strtoul(p + 1, NULL, 0);
6199 }
6200
6201 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6202 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006203 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006204
6205 return count;
6206}
6207
6208static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6209 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006210#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006211
6212static ssize_t store_retry_rate(struct device *d,
6213 struct device_attribute *attr,
6214 const char *buf, size_t count)
6215{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006216 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006217
6218 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6219 if (priv->retry_rate <= 0)
6220 priv->retry_rate = 1;
6221
6222 return count;
6223}
6224
6225static ssize_t show_retry_rate(struct device *d,
6226 struct device_attribute *attr, char *buf)
6227{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006228 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006229 return sprintf(buf, "%d", priv->retry_rate);
6230}
6231
6232static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6233 store_retry_rate);
6234
6235static ssize_t store_power_level(struct device *d,
6236 struct device_attribute *attr,
6237 const char *buf, size_t count)
6238{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006239 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006240 int rc;
6241 int mode;
6242
6243 mode = simple_strtoul(buf, NULL, 0);
6244 mutex_lock(&priv->mutex);
6245
Tomas Winklerfee12472008-04-03 16:05:21 -07006246 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006247 rc = -EAGAIN;
6248 goto out;
6249 }
6250
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006251 rc = iwl_power_set_user_mode(priv, mode);
6252 if (rc) {
6253 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6254 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006255 }
Zhu Yib481de92007-09-25 17:54:57 -07006256 rc = count;
6257
6258 out:
6259 mutex_unlock(&priv->mutex);
6260 return rc;
6261}
6262
6263#define MAX_WX_STRING 80
6264
6265/* Values are in microsecond */
6266static const s32 timeout_duration[] = {
6267 350000,
6268 250000,
6269 75000,
6270 37000,
6271 25000,
6272};
6273static const s32 period_duration[] = {
6274 400000,
6275 700000,
6276 1000000,
6277 1000000,
6278 1000000
6279};
6280
6281static ssize_t show_power_level(struct device *d,
6282 struct device_attribute *attr, char *buf)
6283{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006284 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006285 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07006286 char *p = buf;
6287
6288 p += sprintf(p, "%d ", level);
6289 switch (level) {
6290 case IWL_POWER_MODE_CAM:
6291 case IWL_POWER_AC:
6292 p += sprintf(p, "(AC)");
6293 break;
6294 case IWL_POWER_BATTERY:
6295 p += sprintf(p, "(BATTERY)");
6296 break;
6297 default:
6298 p += sprintf(p,
6299 "(Timeout %dms, Period %dms)",
6300 timeout_duration[level - 1] / 1000,
6301 period_duration[level - 1] / 1000);
6302 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006303/*
Zhu Yib481de92007-09-25 17:54:57 -07006304 if (!(priv->power_mode & IWL_POWER_ENABLED))
6305 p += sprintf(p, " OFF\n");
6306 else
6307 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006308*/
6309 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07006310 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07006311}
6312
6313static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6314 store_power_level);
6315
6316static ssize_t show_channels(struct device *d,
6317 struct device_attribute *attr, char *buf)
6318{
Johannes Berg8318d782008-01-24 19:38:38 +01006319 /* all this shit doesn't belong into sysfs anyway */
6320 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006321}
6322
6323static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6324
6325static ssize_t show_statistics(struct device *d,
6326 struct device_attribute *attr, char *buf)
6327{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006328 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006329 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07006330 u32 len = 0, ofs = 0;
6331 u8 *data = (u8 *) & priv->statistics;
6332 int rc = 0;
6333
Tomas Winklerfee12472008-04-03 16:05:21 -07006334 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006335 return -EAGAIN;
6336
6337 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07006338 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006339 mutex_unlock(&priv->mutex);
6340
6341 if (rc) {
6342 len = sprintf(buf,
6343 "Error sending statistics request: 0x%08X\n", rc);
6344 return len;
6345 }
6346
6347 while (size && (PAGE_SIZE - len)) {
6348 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6349 PAGE_SIZE - len, 1);
6350 len = strlen(buf);
6351 if (PAGE_SIZE - len)
6352 buf[len++] = '\n';
6353
6354 ofs += 16;
6355 size -= min(size, 16U);
6356 }
6357
6358 return len;
6359}
6360
6361static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
6362
Zhu Yib481de92007-09-25 17:54:57 -07006363static ssize_t show_status(struct device *d,
6364 struct device_attribute *attr, char *buf)
6365{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006366 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07006367 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006368 return -EAGAIN;
6369 return sprintf(buf, "0x%08x\n", (int)priv->status);
6370}
6371
6372static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6373
6374static ssize_t dump_error_log(struct device *d,
6375 struct device_attribute *attr,
6376 const char *buf, size_t count)
6377{
6378 char *p = (char *)buf;
6379
6380 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006381 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006382
6383 return strnlen(buf, count);
6384}
6385
6386static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
6387
6388static ssize_t dump_event_log(struct device *d,
6389 struct device_attribute *attr,
6390 const char *buf, size_t count)
6391{
6392 char *p = (char *)buf;
6393
6394 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006395 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006396
6397 return strnlen(buf, count);
6398}
6399
6400static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
6401
6402/*****************************************************************************
6403 *
6404 * driver setup and teardown
6405 *
6406 *****************************************************************************/
6407
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006408static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006409{
6410 priv->workqueue = create_workqueue(DRV_NAME);
6411
6412 init_waitqueue_head(&priv->wait_command_queue);
6413
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006414 INIT_WORK(&priv->up, iwl4965_bg_up);
6415 INIT_WORK(&priv->restart, iwl4965_bg_restart);
6416 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
6417 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
6418 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
6419 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
6420 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
6421 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08006422 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006423 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
6424 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
6425 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
6426 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07006427
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006428 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006429
6430 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006431 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07006432}
6433
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006434static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006435{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006436 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006437
Joonwoo Park3ae6a052007-11-29 10:43:16 +09006438 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07006439 cancel_delayed_work(&priv->scan_check);
6440 cancel_delayed_work(&priv->alive_start);
6441 cancel_delayed_work(&priv->post_associate);
6442 cancel_work_sync(&priv->beacon_update);
6443}
6444
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006445static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07006446 &dev_attr_channels.attr,
6447 &dev_attr_dump_errors.attr,
6448 &dev_attr_dump_events.attr,
6449 &dev_attr_flags.attr,
6450 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006451#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006452 &dev_attr_measurement.attr,
6453#endif
6454 &dev_attr_power_level.attr,
6455 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006456 &dev_attr_rs_window.attr,
6457 &dev_attr_statistics.attr,
6458 &dev_attr_status.attr,
6459 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006460 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08006461#ifdef CONFIG_IWLWIFI_DEBUG
6462 &dev_attr_debug_level.attr,
6463#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08006464 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006465
6466 NULL
6467};
6468
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006469static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07006470 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006471 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07006472};
6473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006474static struct ieee80211_ops iwl4965_hw_ops = {
6475 .tx = iwl4965_mac_tx,
6476 .start = iwl4965_mac_start,
6477 .stop = iwl4965_mac_stop,
6478 .add_interface = iwl4965_mac_add_interface,
6479 .remove_interface = iwl4965_mac_remove_interface,
6480 .config = iwl4965_mac_config,
6481 .config_interface = iwl4965_mac_config_interface,
6482 .configure_filter = iwl4965_configure_filter,
6483 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006484 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006485 .get_stats = iwl4965_mac_get_stats,
6486 .get_tx_stats = iwl4965_mac_get_tx_stats,
6487 .conf_tx = iwl4965_mac_conf_tx,
6488 .get_tsf = iwl4965_mac_get_tsf,
6489 .reset_tsf = iwl4965_mac_reset_tsf,
6490 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01006491 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006492#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02006493 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006494#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006495 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07006496};
6497
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006498static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07006499{
6500 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006501 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07006502 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08006503 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006504 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006505 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006506
Assaf Krauss316c30d2008-03-14 10:38:46 -07006507 /************************
6508 * 1. Allocating HW data
6509 ************************/
6510
Cahill, Ben M6440adb2007-11-29 11:09:55 +08006511 /* Disabling hardware scan means that mac80211 will perform scans
6512 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006513 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08006514 if (cfg->mod_params->debug & IWL_DL_INFO)
6515 dev_printk(KERN_DEBUG, &(pdev->dev),
6516 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006517 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006518 }
6519
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006520 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
6521 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07006522 err = -ENOMEM;
6523 goto out;
6524 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006525 priv = hw->priv;
6526 /* At this point both hw and priv are allocated. */
6527
Zhu Yib481de92007-09-25 17:54:57 -07006528 SET_IEEE80211_DEV(hw, &pdev->dev);
6529
6530 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08006531 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07006532 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006533
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006534#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08006535 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07006536 atomic_set(&priv->restrict_refcnt, 0);
6537#endif
Zhu Yib481de92007-09-25 17:54:57 -07006538
Assaf Krauss316c30d2008-03-14 10:38:46 -07006539 /**************************
6540 * 2. Initializing PCI bus
6541 **************************/
6542 if (pci_enable_device(pdev)) {
6543 err = -ENODEV;
6544 goto out_ieee80211_free_hw;
6545 }
6546
6547 pci_set_master(pdev);
6548
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006549 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006550 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006551 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
6552 if (err) {
6553 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6554 if (!err)
6555 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
6556 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006557 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006558 printk(KERN_WARNING "%s: No suitable DMA available.\n",
6559 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006560 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006561 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006562 }
6563
6564 err = pci_request_regions(pdev, DRV_NAME);
6565 if (err)
6566 goto out_pci_disable_device;
6567
6568 pci_set_drvdata(pdev, priv);
6569
6570 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6571 * PCI Tx retries from interfering with C3 CPU state */
6572 pci_write_config_byte(pdev, 0x41, 0x00);
6573
6574 /***********************
6575 * 3. Read REV register
6576 ***********************/
6577 priv->hw_base = pci_iomap(pdev, 0, 0);
6578 if (!priv->hw_base) {
6579 err = -ENODEV;
6580 goto out_pci_release_regions;
6581 }
6582
6583 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
6584 (unsigned long long) pci_resource_len(pdev, 0));
6585 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
6586
Tomas Winklerb661c812008-04-23 17:14:54 -07006587 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006588 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07006589 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
6590 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006591
Tomas Winkler91238712008-04-23 17:14:53 -07006592 /* amp init */
6593 err = priv->cfg->ops->lib->apm_ops.init(priv);
6594 if (err < 0) {
6595 IWL_DEBUG_INFO("Failed to init APMG\n");
6596 goto out_iounmap;
6597 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006598 /*****************
6599 * 4. Read EEPROM
6600 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07006601 /* Read the EEPROM */
6602 err = iwl_eeprom_init(priv);
6603 if (err) {
6604 IWL_ERROR("Unable to init EEPROM\n");
6605 goto out_iounmap;
6606 }
Tomas Winkler8614f362008-04-23 17:14:55 -07006607 err = iwl_eeprom_check_version(priv);
6608 if (err)
6609 goto out_iounmap;
6610
Ron Rindjunsky02883012008-05-15 13:53:53 +08006611 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006612 iwl_eeprom_get_mac(priv, priv->mac_addr);
6613 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
6614 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6615
6616 /************************
6617 * 5. Setup HW constants
6618 ************************/
6619 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07006620 if (priv->cfg->ops->lib->set_hw_params(priv)) {
6621 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07006622 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006623 }
6624
6625 /*******************
6626 * 6. Setup hw/priv
6627 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07006628
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006629 err = iwl_setup(priv);
6630 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006631 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006632 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006633
6634 /**********************************
6635 * 7. Initialize module parameters
6636 **********************************/
6637
6638 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006639 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07006640 set_bit(STATUS_RF_KILL_SW, &priv->status);
6641 IWL_DEBUG_INFO("Radio disabled.\n");
6642 }
6643
Assaf Krauss1ea87392008-03-18 14:57:50 -07006644 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07006645 priv->qos_data.qos_enable = 1;
6646
6647 /********************
6648 * 8. Setup services
6649 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006650 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006651 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006652 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006653
6654 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6655 if (err) {
6656 IWL_ERROR("failed to create sysfs device attributes\n");
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006657 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006658 }
6659
6660 err = iwl_dbgfs_register(priv, DRV_NAME);
6661 if (err) {
6662 IWL_ERROR("failed to create debugfs files\n");
6663 goto out_remove_sysfs;
6664 }
6665
6666 iwl4965_setup_deferred_work(priv);
6667 iwl4965_setup_rx_handlers(priv);
6668
6669 /********************
6670 * 9. Conclude
6671 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08006672 pci_save_state(pdev);
6673 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006674
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006675 /* notify iwlcore to init */
6676 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07006677 return 0;
6678
Assaf Krauss316c30d2008-03-14 10:38:46 -07006679 out_remove_sysfs:
6680 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006681 out_free_eeprom:
6682 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006683 out_iounmap:
6684 pci_iounmap(pdev, priv->hw_base);
6685 out_pci_release_regions:
6686 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006687 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07006688 out_pci_disable_device:
6689 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006690 out_ieee80211_free_hw:
6691 ieee80211_free_hw(priv->hw);
6692 out:
6693 return err;
6694}
6695
Reinette Chatrec83dbf62008-03-21 13:53:41 -07006696static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006697{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006698 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006699 struct list_head *p, *q;
6700 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006701 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07006702
6703 if (!priv)
6704 return;
6705
6706 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
6707
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07006708 if (priv->mac80211_registered) {
6709 ieee80211_unregister_hw(priv->hw);
6710 priv->mac80211_registered = 0;
6711 }
6712
Zhu Yib481de92007-09-25 17:54:57 -07006713 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08006714
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006715 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006716
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006717 /* make sure we flush any pending irq or
6718 * tasklet for the driver
6719 */
6720 spin_lock_irqsave(&priv->lock, flags);
6721 iwl4965_disable_interrupts(priv);
6722 spin_unlock_irqrestore(&priv->lock, flags);
6723
6724 iwl_synchronize_irq(priv);
6725
Zhu Yib481de92007-09-25 17:54:57 -07006726 /* Free MAC hash list for ADHOC */
6727 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
6728 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
6729 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006730 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07006731 }
6732 }
6733
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006734 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07006735 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006736 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07006737
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006738 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006739
6740 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08006741 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08006742 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006743
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006744 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006745 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006746
Zhu Yib481de92007-09-25 17:54:57 -07006747
Mohamed Abbas948c1712007-10-25 17:15:45 +08006748 /*netif_stop_queue(dev); */
6749 flush_workqueue(priv->workqueue);
6750
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006751 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07006752 * priv->workqueue... so we can't take down the workqueue
6753 * until now... */
6754 destroy_workqueue(priv->workqueue);
6755 priv->workqueue = NULL;
6756
Zhu Yib481de92007-09-25 17:54:57 -07006757 pci_iounmap(pdev, priv->hw_base);
6758 pci_release_regions(pdev);
6759 pci_disable_device(pdev);
6760 pci_set_drvdata(pdev, NULL);
6761
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006762 iwl_free_channel_map(priv);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006763 iwlcore_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006764
6765 if (priv->ibss_beacon)
6766 dev_kfree_skb(priv->ibss_beacon);
6767
6768 ieee80211_free_hw(priv->hw);
6769}
6770
6771#ifdef CONFIG_PM
6772
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006773static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07006774{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006775 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006776
Zhu Yie655b9f2008-01-24 02:19:38 -08006777 if (priv->is_open) {
6778 set_bit(STATUS_IN_SUSPEND, &priv->status);
6779 iwl4965_mac_stop(priv->hw);
6780 priv->is_open = 1;
6781 }
Zhu Yib481de92007-09-25 17:54:57 -07006782
Zhu Yib481de92007-09-25 17:54:57 -07006783 pci_set_power_state(pdev, PCI_D3hot);
6784
Zhu Yib481de92007-09-25 17:54:57 -07006785 return 0;
6786}
6787
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006788static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006789{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006790 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006791
Zhu Yib481de92007-09-25 17:54:57 -07006792 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07006793
Zhu Yie655b9f2008-01-24 02:19:38 -08006794 if (priv->is_open)
6795 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006796
Zhu Yie655b9f2008-01-24 02:19:38 -08006797 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006798 return 0;
6799}
6800
6801#endif /* CONFIG_PM */
6802
6803/*****************************************************************************
6804 *
6805 * driver and module entry point
6806 *
6807 *****************************************************************************/
6808
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006809/* Hardware specific file defines the PCI IDs table for that hardware module */
6810static struct pci_device_id iwl_hw_card_ids[] = {
6811 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
6812 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07006813#ifdef CONFIG_IWL5000
6814 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
6815 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
6816 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
6817#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006818 {0}
6819};
6820MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
6821
6822static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07006823 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006824 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006825 .probe = iwl4965_pci_probe,
6826 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07006827#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006828 .suspend = iwl4965_pci_suspend,
6829 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07006830#endif
6831};
6832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006833static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07006834{
6835
6836 int ret;
6837 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
6838 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006839
6840 ret = iwl4965_rate_control_register();
6841 if (ret) {
6842 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
6843 return ret;
6844 }
6845
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006846 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07006847 if (ret) {
6848 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006849 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07006850 }
Zhu Yib481de92007-09-25 17:54:57 -07006851
6852 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006853
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006854error_register:
6855 iwl4965_rate_control_unregister();
6856 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006857}
6858
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006859static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07006860{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006861 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006862 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07006863}
6864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006865module_exit(iwl4965_exit);
6866module_init(iwl4965_init);