blob: 901d9cf9ad78fc4d24c38a45552d5d1247bac2c5 [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
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200176int iwl4965_queue_space(const struct iwl4965_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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800193static inline int x2_queue_used(const struct iwl4965_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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800200static inline u8 get_cmd_index(struct iwl4965_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];
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800351 struct iwl4965_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;
356 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
357 dma_addr_t phys_addr;
358 int ret;
359 unsigned long flags;
360
361 /* If any of the command structures end up being larger than
362 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
363 * we will need to increase the size of the TFD entries */
364 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
365 !(cmd->meta.flags & CMD_SIZE_HUGE));
366
Tomas Winklerfee12472008-04-03 16:05:21 -0700367 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800368 IWL_DEBUG_INFO("Not sending command - RF KILL");
369 return -EIO;
370 }
371
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800372 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700373 IWL_ERROR("No space for Tx\n");
374 return -ENOSPC;
375 }
376
377 spin_lock_irqsave(&priv->hcmd_lock, flags);
378
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800379 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700380 memset(tfd, 0, sizeof(*tfd));
381
382 control_flags = (u32 *) tfd;
383
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800384 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700385 out_cmd = &txq->cmd[idx];
386
387 out_cmd->hdr.cmd = cmd->id;
388 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
389 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
390
391 /* At this point, the out_cmd now has all of the incoming cmd
392 * information */
393
394 out_cmd->hdr.flags = 0;
395 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800396 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700397 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
398 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
399
400 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700401 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800402 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700403
404 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
405 "%d bytes at %d[%d]:%d\n",
406 get_cmd_string(out_cmd->hdr.cmd),
407 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800408 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700409
410 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800411
412 /* Set up entry in queue's byte count circular buffer */
Tomas Winklere2a722e2008-04-14 21:16:10 -0700413 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800414
415 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800416 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklere2a722e2008-04-14 21:16:10 -0700417 ret = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700418
419 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
420 return ret ? ret : idx;
421}
422
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700423static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
424{
425 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
426
427 if (hw_decrypt)
428 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
429 else
430 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
431
432}
433
Zhu Yib481de92007-09-25 17:54:57 -0700434/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800435 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700436 *
437 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800438 * NOTE: mutex must be held before calling this fnction
439 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700440static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700441 const u8 *addr, int is_ap)
442{
Zhu Yi556f8db2007-09-27 11:27:33 +0800443 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700444
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800445 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200446#ifdef CONFIG_IWL4965_HT
447 struct ieee80211_conf *conf = &priv->hw->conf;
448 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
449
450 if ((is_ap) &&
451 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
452 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
453 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
454 0, cur_ht_config);
455 else
456#endif /* CONFIG_IWL4965_HT */
457 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
458 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800459
460 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700461 iwl4965_add_station(priv, addr, is_ap);
462
Zhu Yi556f8db2007-09-27 11:27:33 +0800463 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700464}
465
466/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800467 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700468 *
469 * NOTE: This is really only useful during development and can eventually
470 * be #ifdef'd out once the driver is stable and folks aren't actively
471 * making changes
472 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800473static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700474{
475 int error = 0;
476 int counter = 1;
477
478 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
479 error |= le32_to_cpu(rxon->flags &
480 (RXON_FLG_TGJ_NARROW_BAND_MSK |
481 RXON_FLG_RADAR_DETECT_MSK));
482 if (error)
483 IWL_WARNING("check 24G fields %d | %d\n",
484 counter++, error);
485 } else {
486 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
487 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
488 if (error)
489 IWL_WARNING("check 52 fields %d | %d\n",
490 counter++, error);
491 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
492 if (error)
493 IWL_WARNING("check 52 CCK %d | %d\n",
494 counter++, error);
495 }
496 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
497 if (error)
498 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
499
500 /* make sure basic rates 6Mbps and 1Mbps are supported */
501 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
502 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
503 if (error)
504 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
505
506 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
507 if (error)
508 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
509
510 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
511 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
512 if (error)
513 IWL_WARNING("check CCK and short slot %d | %d\n",
514 counter++, error);
515
516 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
517 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
518 if (error)
519 IWL_WARNING("check CCK & auto detect %d | %d\n",
520 counter++, error);
521
522 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
523 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
524 if (error)
525 IWL_WARNING("check TGG and auto detect %d | %d\n",
526 counter++, error);
527
528 if (error)
529 IWL_WARNING("Tuning to channel %d\n",
530 le16_to_cpu(rxon->channel));
531
532 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800533 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700534 return -1;
535 }
536 return 0;
537}
538
539/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800540 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800541 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700542 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800543 * If the RXON structure is changing enough to require a new tune,
544 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
545 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700546 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700547static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700548{
549
550 /* These items are only settable from the full RXON command */
551 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
552 compare_ether_addr(priv->staging_rxon.bssid_addr,
553 priv->active_rxon.bssid_addr) ||
554 compare_ether_addr(priv->staging_rxon.node_addr,
555 priv->active_rxon.node_addr) ||
556 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
557 priv->active_rxon.wlap_bssid_addr) ||
558 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
559 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
560 (priv->staging_rxon.air_propagation !=
561 priv->active_rxon.air_propagation) ||
562 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
563 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
564 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
565 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
566 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
567 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
568 return 1;
569
570 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
571 * be updated with the RXON_ASSOC command -- however only some
572 * flag transitions are allowed using RXON_ASSOC */
573
574 /* Check if we are not switching bands */
575 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
576 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
577 return 1;
578
579 /* Check if we are switching association toggle */
580 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
581 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
582 return 1;
583
584 return 0;
585}
586
Zhu Yib481de92007-09-25 17:54:57 -0700587/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800588 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700589 *
Ian Schram01ebd062007-10-25 17:15:22 +0800590 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700591 * the active_rxon structure is updated with the new data. This
592 * function correctly transitions out of the RXON_ASSOC_MSK state if
593 * a HW tune is required based on the RXON structure changes.
594 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700595static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700596{
597 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800598 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700599 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700600 int rc = 0;
601
Tomas Winklerfee12472008-04-03 16:05:21 -0700602 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700603 return -1;
604
605 /* always get timestamp with Rx frame */
606 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
607
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800608 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700609 if (rc) {
610 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
611 return -EINVAL;
612 }
613
614 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800615 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700616 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800617 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700618 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700619 if (rc) {
620 IWL_ERROR("Error setting RXON_ASSOC "
621 "configuration (%d).\n", rc);
622 return rc;
623 }
624
625 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
626
627 return 0;
628 }
629
630 /* station table will be cleared */
631 priv->assoc_station_added = 0;
632
Zhu Yib481de92007-09-25 17:54:57 -0700633 /* If we are currently associated and the new config requires
634 * an RXON_ASSOC and the new config wants the associated mask enabled,
635 * we must clear the associated from the active configuration
636 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700637 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700638 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
639 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
640 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
641
Tomas Winkler857485c2008-03-21 13:53:44 -0700642 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800643 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700644 &priv->active_rxon);
645
646 /* If the mask clearing failed then we set
647 * active_rxon back to what it was previously */
648 if (rc) {
649 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
650 IWL_ERROR("Error clearing ASSOC_MSK on current "
651 "configuration (%d).\n", rc);
652 return rc;
653 }
Zhu Yib481de92007-09-25 17:54:57 -0700654 }
655
656 IWL_DEBUG_INFO("Sending RXON\n"
657 "* with%s RXON_FILTER_ASSOC_MSK\n"
658 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700659 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700660 ((priv->staging_rxon.filter_flags &
661 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
662 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700663 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700664
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700665 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700666 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700667 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800668 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700669 if (rc) {
670 IWL_ERROR("Error setting new configuration (%d).\n", rc);
671 return rc;
672 }
673
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700674 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800675
Zhu Yib481de92007-09-25 17:54:57 -0700676 if (!priv->error_recovering)
677 priv->start_calib = 0;
678
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700679 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700680
681 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
682
683 /* If we issue a new RXON command which required a tune then we must
684 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800685 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700686 if (rc) {
687 IWL_ERROR("Error setting Tx power (%d).\n", rc);
688 return rc;
689 }
690
691 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800692 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700693 IWL_INVALID_STATION) {
694 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
695 return -EIO;
696 }
697
698 /* If we have set the ASSOC_MSK and we are in BSS mode then
699 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700700 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700701 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800702 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700703 == IWL_INVALID_STATION) {
704 IWL_ERROR("Error adding AP address for transmit.\n");
705 return -EIO;
706 }
707 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700708 if (priv->default_wep_key &&
709 iwl_send_static_wepkey_cmd(priv, 0))
710 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700711 }
712
713 return 0;
714}
715
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700716void iwl4965_update_chain_flags(struct iwl_priv *priv)
717{
718
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700719 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700720 iwl4965_commit_rxon(priv);
721}
722
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700723static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700724{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800725 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700726 .flags = 3,
727 .lead_time = 0xAA,
728 .max_kill = 1,
729 .kill_ack_mask = 0,
730 .kill_cts_mask = 0,
731 };
732
Tomas Winkler857485c2008-03-21 13:53:44 -0700733 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800734 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700735}
736
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700737static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700738{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800739 int ret = 0;
740 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700741 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700742 .id = REPLY_SCAN_ABORT_CMD,
743 .meta.flags = CMD_WANT_SKB,
744 };
745
746 /* If there isn't a scan actively going on in the hardware
747 * then we are in between scan bands and not actually
748 * actively scanning, so don't send the abort command */
749 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
750 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
751 return 0;
752 }
753
Tomas Winklerdb11d632008-05-05 10:22:33 +0800754 ret = iwl_send_cmd_sync(priv, &cmd);
755 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700756 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800757 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700758 }
759
Tomas Winklerdb11d632008-05-05 10:22:33 +0800760 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700761 if (res->u.status != CAN_ABORT_STATUS) {
762 /* The scan abort will return 1 for success or
763 * 2 for "failure". A failure condition can be
764 * due to simply not being in an active scan which
765 * can occur if we send the scan abort before we
766 * the microcode has notified us that a scan is
767 * completed. */
768 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
769 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
770 clear_bit(STATUS_SCAN_HW, &priv->status);
771 }
772
773 dev_kfree_skb_any(cmd.meta.u.skb);
774
Tomas Winklerdb11d632008-05-05 10:22:33 +0800775 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700776}
777
Zhu Yib481de92007-09-25 17:54:57 -0700778/*
779 * CARD_STATE_CMD
780 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800781 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700782 *
783 * When in the 'enable' state the card operates as normal.
784 * When in the 'disable' state, the card enters into a low power mode.
785 * When in the 'halt' state, the card is shut down and must be fully
786 * restarted to come back on.
787 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700788static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700789{
Tomas Winkler857485c2008-03-21 13:53:44 -0700790 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700791 .id = REPLY_CARD_STATE_CMD,
792 .len = sizeof(u32),
793 .data = &flags,
794 .meta.flags = meta_flag,
795 };
796
Tomas Winkler857485c2008-03-21 13:53:44 -0700797 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700798}
799
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700800static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700801{
802 struct list_head *element;
803
804 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
805 priv->frames_count);
806
807 while (!list_empty(&priv->free_frames)) {
808 element = priv->free_frames.next;
809 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800810 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700811 priv->frames_count--;
812 }
813
814 if (priv->frames_count) {
815 IWL_WARNING("%d frames still in use. Did we lose one?\n",
816 priv->frames_count);
817 priv->frames_count = 0;
818 }
819}
820
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700821static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700822{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700824 struct list_head *element;
825 if (list_empty(&priv->free_frames)) {
826 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
827 if (!frame) {
828 IWL_ERROR("Could not allocate frame!\n");
829 return NULL;
830 }
831
832 priv->frames_count++;
833 return frame;
834 }
835
836 element = priv->free_frames.next;
837 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800838 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700839}
840
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700841static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700842{
843 memset(frame, 0, sizeof(*frame));
844 list_add(&frame->list, &priv->free_frames);
845}
846
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700847unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700848 struct ieee80211_hdr *hdr,
849 const u8 *dest, int left)
850{
851
Tomas Winkler3109ece2008-03-28 16:33:35 -0700852 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700853 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
854 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
855 return 0;
856
857 if (priv->ibss_beacon->len > left)
858 return 0;
859
860 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
861
862 return priv->ibss_beacon->len;
863}
864
Guy Cohen39e88502008-04-23 17:14:57 -0700865static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700866{
Guy Cohen39e88502008-04-23 17:14:57 -0700867 int i;
868 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700869
Guy Cohen39e88502008-04-23 17:14:57 -0700870 /* Set rate mask*/
871 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
872 rate_mask = priv->active_rate_basic & 0xF;
873 else
874 rate_mask = priv->active_rate_basic & 0xFF0;
875
876 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700877 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Guy Cohen39e88502008-04-23 17:14:57 -0700878 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700879 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800880 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700881 }
882
Guy Cohen39e88502008-04-23 17:14:57 -0700883 /* No valid rate was found. Assign the lowest one */
884 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
885 return IWL_RATE_1M_PLCP;
886 else
887 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700888}
889
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700890static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700891{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800892 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700893 unsigned int frame_size;
894 int rc;
895 u8 rate;
896
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800897 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700898
899 if (!frame) {
900 IWL_ERROR("Could not obtain free frame buffer for beacon "
901 "command.\n");
902 return -ENOMEM;
903 }
904
Guy Cohen39e88502008-04-23 17:14:57 -0700905 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700906
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800907 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700908
Tomas Winkler857485c2008-03-21 13:53:44 -0700909 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700910 &frame->u.cmd[0]);
911
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800912 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700913
914 return rc;
915}
916
917/******************************************************************************
918 *
Zhu Yib481de92007-09-25 17:54:57 -0700919 * Misc. internal state and helper functions
920 *
921 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700922
Zhu Yib481de92007-09-25 17:54:57 -0700923/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800924 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -0700925 *
926 * return : set the bit for each supported rate insert in ie
927 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800928static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +0200929 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -0700930{
931 u16 ret_rates = 0, bit;
932 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200933 u8 *cnt = ie;
934 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700935
936 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
937 if (bit & supported_rate) {
938 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800939 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +0200940 ((bit & basic_rate) ? 0x80 : 0x00);
941 (*cnt)++;
942 (*left)--;
943 if ((*left <= 0) ||
944 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -0700945 break;
946 }
947 }
948
949 return ret_rates;
950}
951
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700952#ifdef CONFIG_IWL4965_HT
953static void iwl4965_ht_conf(struct iwl_priv *priv,
954 struct ieee80211_bss_conf *bss_conf)
955{
956 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
957 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
958 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
959
960 IWL_DEBUG_MAC80211("enter: \n");
961
962 iwl_conf->is_ht = bss_conf->assoc_ht;
963
964 if (!iwl_conf->is_ht)
965 return;
966
967 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
968
969 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
970 iwl_conf->sgf |= 0x1;
971 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
972 iwl_conf->sgf |= 0x2;
973
974 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
975 iwl_conf->max_amsdu_size =
976 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
977
978 iwl_conf->supported_chan_width =
979 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
980 iwl_conf->extension_chan_offset =
981 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
982 /* If no above or below channel supplied disable FAT channel */
983 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
984 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
985 iwl_conf->supported_chan_width = 0;
986
987 iwl_conf->tx_mimo_ps_mode =
988 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
989 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
990
991 iwl_conf->control_channel = ht_bss_conf->primary_channel;
992 iwl_conf->tx_chan_width =
993 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
994 iwl_conf->ht_protection =
995 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
996 iwl_conf->non_GF_STA_present =
997 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
998
999 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
1000 IWL_DEBUG_MAC80211("leave\n");
1001}
1002
1003static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1004 u8 *pos, int *left)
1005{
1006 struct ieee80211_ht_cap *ht_cap;
1007
1008 if (!sband || !sband->ht_info.ht_supported)
1009 return;
1010
1011 if (*left < sizeof(struct ieee80211_ht_cap))
1012 return;
1013
1014 *pos++ = sizeof(struct ieee80211_ht_cap);
1015 ht_cap = (struct ieee80211_ht_cap *) pos;
1016
1017 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1018 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1019 ht_cap->ampdu_params_info =
1020 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
1021 ((sband->ht_info.ampdu_density << 2) &
1022 IEEE80211_HT_CAP_AMPDU_DENSITY);
1023 *left -= sizeof(struct ieee80211_ht_cap);
1024}
1025#else
1026static inline void iwl4965_ht_conf(struct iwl_priv *priv,
1027 struct ieee80211_bss_conf *bss_conf)
1028{
1029}
1030static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1031 u8 *pos, int *left)
1032{
1033}
1034#endif
1035
1036
Zhu Yib481de92007-09-25 17:54:57 -07001037/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001038 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001039 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001040static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001041 enum ieee80211_band band,
1042 struct ieee80211_mgmt *frame,
1043 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001044{
1045 int len = 0;
1046 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001047 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +02001048 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001049 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07001050
1051 /* Make sure there is enough space for the probe request,
1052 * two mandatory IEs and the data */
1053 left -= 24;
1054 if (left < 0)
1055 return 0;
1056 len += 24;
1057
1058 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001059 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001060 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001061 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001062 frame->seq_ctrl = 0;
1063
1064 /* fill in our indirect SSID IE */
1065 /* ...next IE... */
1066
1067 left -= 2;
1068 if (left < 0)
1069 return 0;
1070 len += 2;
1071 pos = &(frame->u.probe_req.variable[0]);
1072 *pos++ = WLAN_EID_SSID;
1073 *pos++ = 0;
1074
1075 /* fill in our direct SSID IE... */
1076 if (is_direct) {
1077 /* ...next IE... */
1078 left -= 2 + priv->essid_len;
1079 if (left < 0)
1080 return 0;
1081 /* ... fill it in... */
1082 *pos++ = WLAN_EID_SSID;
1083 *pos++ = priv->essid_len;
1084 memcpy(pos, priv->essid, priv->essid_len);
1085 pos += priv->essid_len;
1086 len += 2 + priv->essid_len;
1087 }
1088
1089 /* fill in supported rate */
1090 /* ...next IE... */
1091 left -= 2;
1092 if (left < 0)
1093 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001094
Zhu Yib481de92007-09-25 17:54:57 -07001095 /* ... fill it in... */
1096 *pos++ = WLAN_EID_SUPP_RATES;
1097 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001098
mabbasbee488d2007-10-25 17:15:42 +08001099 /* exclude 60M rate */
1100 active_rates = priv->rates_mask;
1101 active_rates &= ~IWL_RATE_60M_MASK;
1102
1103 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001104
Tomas Winklerc7c46672007-10-18 02:04:15 +02001105 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001106 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001107 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001108 active_rates &= ~ret_rates;
1109
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001110 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001111 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001112 active_rates &= ~ret_rates;
1113
Zhu Yib481de92007-09-25 17:54:57 -07001114 len += 2 + *pos;
1115 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001116 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001117 goto fill_end;
1118
1119 /* fill in supported extended rate */
1120 /* ...next IE... */
1121 left -= 2;
1122 if (left < 0)
1123 return 0;
1124 /* ... fill it in... */
1125 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1126 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001127 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001128 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001129 if (*pos > 0)
1130 len += 2 + *pos;
1131
Zhu Yib481de92007-09-25 17:54:57 -07001132 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001133 /* fill in HT IE */
1134 left -= 2;
1135 if (left < 0)
1136 return 0;
1137
1138 *pos++ = WLAN_EID_HT_CAPABILITY;
1139 *pos = 0;
1140
1141 iwl_ht_cap_to_ie(sband, pos, &left);
1142
1143 if (*pos > 0)
1144 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -07001145 return (u16)len;
1146}
1147
1148/*
1149 * QoS support
1150*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001151static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001152 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001153{
1154
Tomas Winkler857485c2008-03-21 13:53:44 -07001155 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001156 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001157}
1158
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001159static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001160{
1161 unsigned long flags;
1162
Zhu Yib481de92007-09-25 17:54:57 -07001163 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1164 return;
1165
1166 if (!priv->qos_data.qos_enable)
1167 return;
1168
1169 spin_lock_irqsave(&priv->lock, flags);
1170 priv->qos_data.def_qos_parm.qos_flags = 0;
1171
1172 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1173 !priv->qos_data.qos_cap.q_AP.txop_request)
1174 priv->qos_data.def_qos_parm.qos_flags |=
1175 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001176 if (priv->qos_data.qos_active)
1177 priv->qos_data.def_qos_parm.qos_flags |=
1178 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1179
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001180#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001181 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001182 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001183#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001184
Zhu Yib481de92007-09-25 17:54:57 -07001185 spin_unlock_irqrestore(&priv->lock, flags);
1186
Tomas Winkler3109ece2008-03-28 16:33:35 -07001187 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001188 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1189 priv->qos_data.qos_active,
1190 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001192 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001193 &(priv->qos_data.def_qos_parm));
1194 }
1195}
1196
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001197int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001198{
1199 /* Filter incoming packets to determine if they are targeted toward
1200 * this network, discarding packets coming from ourselves */
1201 switch (priv->iw_mode) {
1202 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1203 /* packets from our adapter are dropped (echo) */
1204 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1205 return 0;
1206 /* {broad,multi}cast packets to our IBSS go through */
1207 if (is_multicast_ether_addr(header->addr1))
1208 return !compare_ether_addr(header->addr3, priv->bssid);
1209 /* packets to our adapter go through */
1210 return !compare_ether_addr(header->addr1, priv->mac_addr);
1211 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1212 /* packets from our adapter are dropped (echo) */
1213 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1214 return 0;
1215 /* {broad,multi}cast packets to our BSS go through */
1216 if (is_multicast_ether_addr(header->addr1))
1217 return !compare_ether_addr(header->addr2, priv->bssid);
1218 /* packets to our adapter go through */
1219 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001220 default:
1221 break;
Zhu Yib481de92007-09-25 17:54:57 -07001222 }
1223
1224 return 1;
1225}
1226
1227#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001229static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001230{
1231 switch (status & TX_STATUS_MSK) {
1232 case TX_STATUS_SUCCESS:
1233 return "SUCCESS";
1234 TX_STATUS_ENTRY(SHORT_LIMIT);
1235 TX_STATUS_ENTRY(LONG_LIMIT);
1236 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1237 TX_STATUS_ENTRY(MGMNT_ABORT);
1238 TX_STATUS_ENTRY(NEXT_FRAG);
1239 TX_STATUS_ENTRY(LIFE_EXPIRE);
1240 TX_STATUS_ENTRY(DEST_PS);
1241 TX_STATUS_ENTRY(ABORTED);
1242 TX_STATUS_ENTRY(BT_RETRY);
1243 TX_STATUS_ENTRY(STA_INVALID);
1244 TX_STATUS_ENTRY(FRAG_DROPPED);
1245 TX_STATUS_ENTRY(TID_DISABLE);
1246 TX_STATUS_ENTRY(FRAME_FLUSHED);
1247 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1248 TX_STATUS_ENTRY(TX_LOCKED);
1249 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1250 }
1251
1252 return "UNKNOWN";
1253}
1254
1255/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001256 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001257 *
1258 * NOTE: priv->mutex is not required before calling this function
1259 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001260static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001261{
1262 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1263 clear_bit(STATUS_SCANNING, &priv->status);
1264 return 0;
1265 }
1266
1267 if (test_bit(STATUS_SCANNING, &priv->status)) {
1268 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1269 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1270 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1271 queue_work(priv->workqueue, &priv->abort_scan);
1272
1273 } else
1274 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1275
1276 return test_bit(STATUS_SCANNING, &priv->status);
1277 }
1278
1279 return 0;
1280}
1281
1282/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001283 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001284 * @ms: amount of time to wait (in milliseconds) for scan to abort
1285 *
1286 * NOTE: priv->mutex must be held before calling this function
1287 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001288static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001289{
1290 unsigned long now = jiffies;
1291 int ret;
1292
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001293 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001294 if (ret && ms) {
1295 mutex_unlock(&priv->mutex);
1296 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1297 test_bit(STATUS_SCANNING, &priv->status))
1298 msleep(1);
1299 mutex_lock(&priv->mutex);
1300
1301 return test_bit(STATUS_SCANNING, &priv->status);
1302 }
1303
1304 return ret;
1305}
1306
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001307static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001308{
1309 /* Reset ieee stats */
1310
1311 /* We don't reset the net_device_stats (ieee->stats) on
1312 * re-association */
1313
1314 priv->last_seq_num = -1;
1315 priv->last_frag_num = -1;
1316 priv->last_packet_time = 0;
1317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001318 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001319}
1320
1321#define MAX_UCODE_BEACON_INTERVAL 4096
1322#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1323
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001324static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001325{
1326 u16 new_val = 0;
1327 u16 beacon_factor = 0;
1328
1329 beacon_factor =
1330 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1331 / MAX_UCODE_BEACON_INTERVAL;
1332 new_val = beacon_val / beacon_factor;
1333
1334 return cpu_to_le16(new_val);
1335}
1336
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001337static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001338{
1339 u64 interval_tm_unit;
1340 u64 tsf, result;
1341 unsigned long flags;
1342 struct ieee80211_conf *conf = NULL;
1343 u16 beacon_int = 0;
1344
1345 conf = ieee80211_get_hw_conf(priv->hw);
1346
1347 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001348 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1349 priv->rxon_timing.timestamp.dw[0] =
1350 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001351
1352 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1353
Tomas Winkler3109ece2008-03-28 16:33:35 -07001354 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001355
1356 beacon_int = priv->beacon_int;
1357 spin_unlock_irqrestore(&priv->lock, flags);
1358
1359 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1360 if (beacon_int == 0) {
1361 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1362 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1363 } else {
1364 priv->rxon_timing.beacon_interval =
1365 cpu_to_le16(beacon_int);
1366 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001367 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001368 le16_to_cpu(priv->rxon_timing.beacon_interval));
1369 }
1370
1371 priv->rxon_timing.atim_window = 0;
1372 } else {
1373 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001374 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001375 /* TODO: we need to get atim_window from upper stack
1376 * for now we set to 0 */
1377 priv->rxon_timing.atim_window = 0;
1378 }
1379
1380 interval_tm_unit =
1381 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1382 result = do_div(tsf, interval_tm_unit);
1383 priv->rxon_timing.beacon_init_val =
1384 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1385
1386 IWL_DEBUG_ASSOC
1387 ("beacon interval %d beacon timer %d beacon tim %d\n",
1388 le16_to_cpu(priv->rxon_timing.beacon_interval),
1389 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1390 le16_to_cpu(priv->rxon_timing.atim_window));
1391}
1392
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001393static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001394{
1395 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1396 IWL_ERROR("APs don't scan.\n");
1397 return 0;
1398 }
1399
Tomas Winklerfee12472008-04-03 16:05:21 -07001400 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001401 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1402 return -EIO;
1403 }
1404
1405 if (test_bit(STATUS_SCANNING, &priv->status)) {
1406 IWL_DEBUG_SCAN("Scan already in progress.\n");
1407 return -EAGAIN;
1408 }
1409
1410 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1411 IWL_DEBUG_SCAN("Scan request while abort pending. "
1412 "Queuing.\n");
1413 return -EAGAIN;
1414 }
1415
1416 IWL_DEBUG_INFO("Starting scan...\n");
1417 priv->scan_bands = 2;
1418 set_bit(STATUS_SCANNING, &priv->status);
1419 priv->scan_start = jiffies;
1420 priv->scan_pass_start = priv->scan_start;
1421
1422 queue_work(priv->workqueue, &priv->request_scan);
1423
1424 return 0;
1425}
1426
Zhu Yib481de92007-09-25 17:54:57 -07001427
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001428static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001429 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001430{
Johannes Berg8318d782008-01-24 19:38:38 +01001431 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001432 priv->staging_rxon.flags &=
1433 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1434 | RXON_FLG_CCK_MSK);
1435 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1436 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001437 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001438 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1439 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1440 else
1441 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1442
1443 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1444 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1445
1446 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1447 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1448 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1449 }
1450}
1451
1452/*
Ian Schram01ebd062007-10-25 17:15:22 +08001453 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001454 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001455static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001456{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001457 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001458
1459 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1460
1461 switch (priv->iw_mode) {
1462 case IEEE80211_IF_TYPE_AP:
1463 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1464 break;
1465
1466 case IEEE80211_IF_TYPE_STA:
1467 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1468 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1469 break;
1470
1471 case IEEE80211_IF_TYPE_IBSS:
1472 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1473 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1474 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1475 RXON_FILTER_ACCEPT_GRP_MSK;
1476 break;
1477
1478 case IEEE80211_IF_TYPE_MNTR:
1479 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1480 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1481 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1482 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001483 default:
1484 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1485 break;
Zhu Yib481de92007-09-25 17:54:57 -07001486 }
1487
1488#if 0
1489 /* TODO: Figure out when short_preamble would be set and cache from
1490 * that */
1491 if (!hw_to_local(priv->hw)->short_preamble)
1492 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1493 else
1494 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1495#endif
1496
Assaf Krauss8622e702008-03-21 13:53:43 -07001497 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001498 le16_to_cpu(priv->staging_rxon.channel));
1499
1500 if (!ch_info)
1501 ch_info = &priv->channel_info[0];
1502
1503 /*
1504 * in some case A channels are all non IBSS
1505 * in this case force B/G channel
1506 */
1507 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1508 !(is_channel_ibss(ch_info)))
1509 ch_info = &priv->channel_info[0];
1510
1511 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001512 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001513
Johannes Berg8318d782008-01-24 19:38:38 +01001514 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001515
1516 priv->staging_rxon.ofdm_basic_rates =
1517 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1518 priv->staging_rxon.cck_basic_rates =
1519 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1520
1521 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1522 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1523 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1524 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1525 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1526 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001527 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001528}
1529
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001530static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001531{
Zhu Yib481de92007-09-25 17:54:57 -07001532 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001533 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001534
Assaf Krauss8622e702008-03-21 13:53:43 -07001535 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001536 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001537 le16_to_cpu(priv->staging_rxon.channel));
1538
1539 if (!ch_info || !is_channel_ibss(ch_info)) {
1540 IWL_ERROR("channel %d not IBSS channel\n",
1541 le16_to_cpu(priv->staging_rxon.channel));
1542 return -EINVAL;
1543 }
1544 }
1545
Zhu Yib481de92007-09-25 17:54:57 -07001546 priv->iw_mode = mode;
1547
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001548 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001549 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1550
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001551 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001552
Mohamed Abbasfde35712007-11-29 11:10:15 +08001553 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001554 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001555 return -EAGAIN;
1556
1557 cancel_delayed_work(&priv->scan_check);
1558 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1559 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1560 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1561 return -EAGAIN;
1562 }
1563
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001564 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001565
1566 return 0;
1567}
1568
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001569static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001570 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001571 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001572 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001573 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001574{
Tomas Winkler6def9762008-05-05 10:22:31 +08001575 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001576 struct iwl_wep_key *wepkey;
1577 int keyidx = 0;
1578
Ivo van Doorn1c014422008-04-17 19:41:02 +02001579 BUG_ON(ctl->hw_key->hw_key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001580
1581 switch (keyinfo->alg) {
1582 case ALG_CCMP:
1583 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1584 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001585 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1586 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001587 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1588 break;
1589
1590 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001591 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001592 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1593 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1594 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001595 break;
1596
1597 case ALG_WEP:
Ivo van Doorn1c014422008-04-17 19:41:02 +02001598 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001599 cmd->cmd.tx.sec_ctl = 0;
1600 if (priv->default_wep_key) {
1601 /* the WEP key was sent as static */
Ivo van Doorn1c014422008-04-17 19:41:02 +02001602 keyidx = ctl->hw_key->hw_key_idx;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001603 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1604 wepkey->key_size);
1605 if (wepkey->key_size == WEP_KEY_LEN_128)
1606 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1607 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001608 /* the WEP key was sent as dynamic */
1609 keyidx = keyinfo->keyidx;
1610 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1611 keyinfo->keylen);
1612 if (keyinfo->keylen == WEP_KEY_LEN_128)
1613 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001614 }
Zhu Yib481de92007-09-25 17:54:57 -07001615
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001616 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1617 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001618
1619 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001620 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001621 break;
1622
Zhu Yib481de92007-09-25 17:54:57 -07001623 default:
1624 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1625 break;
1626 }
1627}
1628
1629/*
1630 * handle build REPLY_TX command notification.
1631 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001632static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001633 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001634 struct ieee80211_tx_control *ctrl,
1635 struct ieee80211_hdr *hdr,
1636 int is_unicast, u8 std_id)
1637{
1638 __le16 *qc;
1639 u16 fc = le16_to_cpu(hdr->frame_control);
1640 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1641
1642 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1643 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1644 tx_flags |= TX_CMD_FLG_ACK_MSK;
1645 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1646 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1647 if (ieee80211_is_probe_response(fc) &&
1648 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1649 tx_flags |= TX_CMD_FLG_TSF_MSK;
1650 } else {
1651 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1652 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1653 }
1654
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001655 if (ieee80211_is_back_request(fc))
1656 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1657
1658
Zhu Yib481de92007-09-25 17:54:57 -07001659 cmd->cmd.tx.sta_id = std_id;
1660 if (ieee80211_get_morefrag(hdr))
1661 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1662
1663 qc = ieee80211_get_qos_ctrl(hdr);
1664 if (qc) {
1665 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
1666 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1667 } else
1668 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1669
1670 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1671 tx_flags |= TX_CMD_FLG_RTS_MSK;
1672 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1673 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1674 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1675 tx_flags |= TX_CMD_FLG_CTS_MSK;
1676 }
1677
1678 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1679 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1680
1681 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1682 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1683 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1684 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08001685 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001686 else
Ian Schrambc434dd2007-10-25 17:15:29 +08001687 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001688 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001689 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001690 }
Zhu Yib481de92007-09-25 17:54:57 -07001691
1692 cmd->cmd.tx.driver_txop = 0;
1693 cmd->cmd.tx.tx_flags = tx_flags;
1694 cmd->cmd.tx.next_frame_len = 0;
1695}
Tomas Winkler19758be2008-03-12 16:58:51 -07001696static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1697{
1698 /* 0 - mgmt, 1 - cnt, 2 - data */
1699 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1700 priv->tx_stats[idx].cnt++;
1701 priv->tx_stats[idx].bytes += len;
1702}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001703/**
1704 * iwl4965_get_sta_id - Find station's index within station table
1705 *
1706 * If new IBSS station, create new entry in station table
1707 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001708static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08001709 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07001710{
1711 int sta_id;
1712 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07001713 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001714
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001715 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07001716 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1717 is_multicast_ether_addr(hdr->addr1))
Tomas Winkler5425e492008-04-15 16:01:38 -07001718 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001719
1720 switch (priv->iw_mode) {
1721
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001722 /* If we are a client station in a BSS network, use the special
1723 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07001724 case IEEE80211_IF_TYPE_STA:
1725 return IWL_AP_ID;
1726
1727 /* If we are an AP, then find the station, or use BCAST */
1728 case IEEE80211_IF_TYPE_AP:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001729 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001730 if (sta_id != IWL_INVALID_STATION)
1731 return sta_id;
Tomas Winkler5425e492008-04-15 16:01:38 -07001732 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001733
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001734 /* If this frame is going out to an IBSS network, find the station,
1735 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07001736 case IEEE80211_IF_TYPE_IBSS:
Tomas Winkler947b13a2008-04-16 16:34:48 -07001737 sta_id = iwl_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07001738 if (sta_id != IWL_INVALID_STATION)
1739 return sta_id;
1740
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001741 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02001742 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
1743 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07001744
1745 if (sta_id != IWL_INVALID_STATION)
1746 return sta_id;
1747
Joe Perches0795af52007-10-03 17:59:30 -07001748 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07001749 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07001750 print_mac(mac, hdr->addr1));
Ester Kummerbf403db2008-05-05 10:22:40 +08001751 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Tomas Winkler5425e492008-04-15 16:01:38 -07001752 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001753
1754 default:
Ian Schram01ebd062007-10-25 17:15:22 +08001755 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Tomas Winkler5425e492008-04-15 16:01:38 -07001756 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07001757 }
1758}
1759
1760/*
1761 * start REPLY_TX command process
1762 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001763static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001764 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1765{
1766 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001767 struct iwl_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07001768 u32 *control_flags;
1769 int txq_id = ctl->queue;
Ron Rindjunsky16466902008-05-05 10:22:50 +08001770 struct iwl_tx_queue *txq = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001771 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001772 dma_addr_t phys_addr;
1773 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001774 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07001775 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001776 u16 len, idx, len_org;
1777 u8 id, hdr_len, unicast;
1778 u8 sta_id;
1779 u16 seq_number = 0;
1780 u16 fc;
1781 __le16 *qc;
1782 u8 wait_write_ptr = 0;
1783 unsigned long flags;
1784 int rc;
1785
1786 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07001787 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001788 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1789 goto drop_unlock;
1790 }
1791
Johannes Berg32bfd352007-12-19 01:31:26 +01001792 if (!priv->vif) {
1793 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07001794 goto drop_unlock;
1795 }
1796
Johannes Berg8318d782008-01-24 19:38:38 +01001797 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07001798 IWL_ERROR("ERROR: No TX rate available.\n");
1799 goto drop_unlock;
1800 }
1801
1802 unicast = !is_multicast_ether_addr(hdr->addr1);
1803 id = 0;
1804
1805 fc = le16_to_cpu(hdr->frame_control);
1806
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001807#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001808 if (ieee80211_is_auth(fc))
1809 IWL_DEBUG_TX("Sending AUTH frame\n");
1810 else if (ieee80211_is_assoc_request(fc))
1811 IWL_DEBUG_TX("Sending ASSOC frame\n");
1812 else if (ieee80211_is_reassoc_request(fc))
1813 IWL_DEBUG_TX("Sending REASSOC frame\n");
1814#endif
1815
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001816 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08001817 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07001818 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08001819 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08001820 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07001821 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07001822 goto drop_unlock;
1823 }
1824
1825 spin_unlock_irqrestore(&priv->lock, flags);
1826
1827 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001828
1829 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001830 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001831 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07001832 DECLARE_MAC_BUF(mac);
1833
1834 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
1835 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07001836 goto drop;
1837 }
1838
Guy Cohen07bc28e2008-04-23 17:15:03 -07001839 IWL_DEBUG_TX("station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001840
1841 qc = ieee80211_get_qos_ctrl(hdr);
1842 if (qc) {
1843 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1844 seq_number = priv->stations[sta_id].tid[tid].seq_number &
1845 IEEE80211_SCTL_SEQ;
1846 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1847 (hdr->seq_ctrl &
1848 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
1849 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001850#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001851 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001852 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07001853 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001854 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001855#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001856 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001857
1858 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07001859 txq = &priv->txq[txq_id];
1860 q = &txq->q;
1861
1862 spin_lock_irqsave(&priv->lock, flags);
1863
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001864 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001865 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07001866 memset(tfd, 0, sizeof(*tfd));
1867 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001868 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001869
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001870 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001871 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001872 txq->txb[q->write_ptr].skb[0] = skb;
1873 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07001874 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001875
1876 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07001877 out_cmd = &txq->cmd[idx];
1878 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1879 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001880
1881 /*
1882 * Set up the Tx-command (not MAC!) header.
1883 * Store the chosen Tx queue and TFD index within the sequence field;
1884 * after Tx, uCode's Tx response will return this value so driver can
1885 * locate the frame within the tx queue and do post-tx processing.
1886 */
Zhu Yib481de92007-09-25 17:54:57 -07001887 out_cmd->hdr.cmd = REPLY_TX;
1888 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001889 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001890
1891 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001892 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
1893
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001894 /*
1895 * Use the first empty entry in this queue's command buffer array
1896 * to contain the Tx command and MAC header concatenated together
1897 * (payload data will be in another buffer).
1898 * Size of this varies, due to varying MAC header length.
1899 * If end is not dword aligned, we'll have 2 extra bytes at the end
1900 * of the MAC header (device reads on dword boundaries).
1901 * We'll tell device about this padding later.
1902 */
Tomas Winkler5425e492008-04-15 16:01:38 -07001903 len = priv->hw_params.tx_cmd_len +
Tomas Winkler857485c2008-03-21 13:53:44 -07001904 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07001905
1906 len_org = len;
1907 len = (len + 3) & ~3;
1908
1909 if (len_org != len)
1910 len_org = 1;
1911 else
1912 len_org = 0;
1913
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001914 /* Physical address of this Tx command's header (not MAC header!),
1915 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07001916 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
1917 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001918
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001919 /* Add buffer containing Tx command and MAC(!) header to TFD's
1920 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001921 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07001922
1923 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001924 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001925
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001926 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1927 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07001928 len = skb->len - hdr_len;
1929 if (len) {
1930 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
1931 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001932 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07001933 }
1934
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001935 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07001936 if (len_org)
1937 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1938
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001939 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07001940 len = (u16)skb->len;
1941 out_cmd->cmd.tx.len = cpu_to_le16(len);
1942
1943 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001944 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001945
1946 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001947 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001948
Tomas Winkler19758be2008-03-12 16:58:51 -07001949 iwl_update_tx_stats(priv, fc, len);
1950
Tomas Winkler857485c2008-03-21 13:53:44 -07001951 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001952 offsetof(struct iwl4965_tx_cmd, scratch);
1953 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
1954 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
1955
Zhu Yib481de92007-09-25 17:54:57 -07001956 if (!ieee80211_get_morefrag(hdr)) {
1957 txq->need_update = 1;
1958 if (qc) {
1959 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1960 priv->stations[sta_id].tid[tid].seq_number = seq_number;
1961 }
1962 } else {
1963 wait_write_ptr = 1;
1964 txq->need_update = 0;
1965 }
1966
Ester Kummerbf403db2008-05-05 10:22:40 +08001967 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07001968 sizeof(out_cmd->cmd.tx));
1969
Ester Kummerbf403db2008-05-05 10:22:40 +08001970 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07001971 ieee80211_get_hdrlen(fc));
1972
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001973 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07001974 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07001975
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001976 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08001977 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001978 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001979 spin_unlock_irqrestore(&priv->lock, flags);
1980
1981 if (rc)
1982 return rc;
1983
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001984 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07001985 && priv->mac80211_registered) {
1986 if (wait_write_ptr) {
1987 spin_lock_irqsave(&priv->lock, flags);
1988 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001989 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001990 spin_unlock_irqrestore(&priv->lock, flags);
1991 }
1992
1993 ieee80211_stop_queue(priv->hw, ctl->queue);
1994 }
1995
1996 return 0;
1997
1998drop_unlock:
1999 spin_unlock_irqrestore(&priv->lock, flags);
2000drop:
2001 return -1;
2002}
2003
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002004static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002005{
Johannes Berg8318d782008-01-24 19:38:38 +01002006 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002007 struct ieee80211_rate *rate;
2008 int i;
2009
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07002010 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002011 if (!hw) {
2012 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2013 return;
2014 }
Zhu Yib481de92007-09-25 17:54:57 -07002015
2016 priv->active_rate = 0;
2017 priv->active_rate_basic = 0;
2018
Johannes Berg8318d782008-01-24 19:38:38 +01002019 for (i = 0; i < hw->n_bitrates; i++) {
2020 rate = &(hw->bitrates[i]);
2021 if (rate->hw_value < IWL_RATE_COUNT)
2022 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002023 }
2024
2025 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2026 priv->active_rate, priv->active_rate_basic);
2027
2028 /*
2029 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2030 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2031 * OFDM
2032 */
2033 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2034 priv->staging_rxon.cck_basic_rates =
2035 ((priv->active_rate_basic &
2036 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2037 else
2038 priv->staging_rxon.cck_basic_rates =
2039 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2040
2041 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2042 priv->staging_rxon.ofdm_basic_rates =
2043 ((priv->active_rate_basic &
2044 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2045 IWL_FIRST_OFDM_RATE) & 0xFF;
2046 else
2047 priv->staging_rxon.ofdm_basic_rates =
2048 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2049}
2050
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002051void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002052{
2053 unsigned long flags;
2054
2055 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2056 return;
2057
2058 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2059 disable_radio ? "OFF" : "ON");
2060
2061 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002062 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002063 /* FIXME: This is a workaround for AP */
2064 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2065 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002066 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002067 CSR_UCODE_SW_BIT_RFKILL);
2068 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002069 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07002070 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2071 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002072 iwl4965_send_card_state(priv,
2073 CARD_STATE_CMD_DISABLE,
2074 0);
Zhu Yib481de92007-09-25 17:54:57 -07002075 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07002076
2077 /* make sure mac80211 stop sending Tx frame */
2078 if (priv->mac80211_registered)
2079 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07002080 }
2081 return;
2082 }
2083
2084 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002085 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002086
2087 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2088 spin_unlock_irqrestore(&priv->lock, flags);
2089
2090 /* wake up ucode */
2091 msleep(10);
2092
2093 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002094 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2095 if (!iwl_grab_nic_access(priv))
2096 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002097 spin_unlock_irqrestore(&priv->lock, flags);
2098
2099 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2100 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2101 "disabled by HW switch\n");
2102 return;
2103 }
2104
2105 queue_work(priv->workqueue, &priv->restart);
2106 return;
2107}
2108
Zhu Yib481de92007-09-25 17:54:57 -07002109#define IWL_PACKET_RETRY_TIME HZ
2110
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002111int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002112{
2113 u16 sc = le16_to_cpu(header->seq_ctrl);
2114 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2115 u16 frag = sc & IEEE80211_SCTL_FRAG;
2116 u16 *last_seq, *last_frag;
2117 unsigned long *last_time;
2118
2119 switch (priv->iw_mode) {
2120 case IEEE80211_IF_TYPE_IBSS:{
2121 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002122 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002123 u8 *mac = header->addr2;
2124 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2125
2126 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002127 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002128 if (!compare_ether_addr(entry->mac, mac))
2129 break;
2130 }
2131 if (p == &priv->ibss_mac_hash[index]) {
2132 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2133 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002134 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002135 return 0;
2136 }
2137 memcpy(entry->mac, mac, ETH_ALEN);
2138 entry->seq_num = seq;
2139 entry->frag_num = frag;
2140 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08002141 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07002142 return 0;
2143 }
2144 last_seq = &entry->seq_num;
2145 last_frag = &entry->frag_num;
2146 last_time = &entry->packet_time;
2147 break;
2148 }
2149 case IEEE80211_IF_TYPE_STA:
2150 last_seq = &priv->last_seq_num;
2151 last_frag = &priv->last_frag_num;
2152 last_time = &priv->last_packet_time;
2153 break;
2154 default:
2155 return 0;
2156 }
2157 if ((*last_seq == seq) &&
2158 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2159 if (*last_frag == frag)
2160 goto drop;
2161 if (*last_frag + 1 != frag)
2162 /* out-of-order fragment */
2163 goto drop;
2164 } else
2165 *last_seq = seq;
2166
2167 *last_frag = frag;
2168 *last_time = jiffies;
2169 return 0;
2170
2171 drop:
2172 return 1;
2173}
2174
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002175#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002176
2177#include "iwl-spectrum.h"
2178
2179#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2180#define BEACON_TIME_MASK_HIGH 0xFF000000
2181#define TIME_UNIT 1024
2182
2183/*
2184 * extended beacon time format
2185 * time in usec will be changed into a 32-bit value in 8:24 format
2186 * the high 1 byte is the beacon counts
2187 * the lower 3 bytes is the time in usec within one beacon interval
2188 */
2189
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002190static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002191{
2192 u32 quot;
2193 u32 rem;
2194 u32 interval = beacon_interval * 1024;
2195
2196 if (!interval || !usec)
2197 return 0;
2198
2199 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2200 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2201
2202 return (quot << 24) + rem;
2203}
2204
2205/* base is usually what we get from ucode with each received frame,
2206 * the same as HW timer counter counting down
2207 */
2208
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002209static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002210{
2211 u32 base_low = base & BEACON_TIME_MASK_LOW;
2212 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2213 u32 interval = beacon_interval * TIME_UNIT;
2214 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2215 (addon & BEACON_TIME_MASK_HIGH);
2216
2217 if (base_low > addon_low)
2218 res += base_low - addon_low;
2219 else if (base_low < addon_low) {
2220 res += interval + base_low - addon_low;
2221 res += (1 << 24);
2222 } else
2223 res += (1 << 24);
2224
2225 return cpu_to_le32(res);
2226}
2227
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002228static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002229 struct ieee80211_measurement_params *params,
2230 u8 type)
2231{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002232 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08002233 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002234 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002235 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2236 .data = (void *)&spectrum,
2237 .meta.flags = CMD_WANT_SKB,
2238 };
2239 u32 add_time = le64_to_cpu(params->start_time);
2240 int rc;
2241 int spectrum_resp_status;
2242 int duration = le16_to_cpu(params->duration);
2243
Tomas Winkler3109ece2008-03-28 16:33:35 -07002244 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002245 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002246 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002247 le64_to_cpu(params->start_time) - priv->last_tsf,
2248 le16_to_cpu(priv->rxon_timing.beacon_interval));
2249
2250 memset(&spectrum, 0, sizeof(spectrum));
2251
2252 spectrum.channel_count = cpu_to_le16(1);
2253 spectrum.flags =
2254 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2255 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2256 cmd.len = sizeof(spectrum);
2257 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2258
Tomas Winkler3109ece2008-03-28 16:33:35 -07002259 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002260 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002261 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002262 add_time,
2263 le16_to_cpu(priv->rxon_timing.beacon_interval));
2264 else
2265 spectrum.start_time = 0;
2266
2267 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2268 spectrum.channels[0].channel = params->channel;
2269 spectrum.channels[0].type = type;
2270 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2271 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2272 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2273
Tomas Winkler857485c2008-03-21 13:53:44 -07002274 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002275 if (rc)
2276 return rc;
2277
Tomas Winklerdb11d632008-05-05 10:22:33 +08002278 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002279 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2280 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2281 rc = -EIO;
2282 }
2283
2284 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2285 switch (spectrum_resp_status) {
2286 case 0: /* Command will be handled */
2287 if (res->u.spectrum.id != 0xff) {
2288 IWL_DEBUG_INFO
2289 ("Replaced existing measurement: %d\n",
2290 res->u.spectrum.id);
2291 priv->measurement_status &= ~MEASUREMENT_READY;
2292 }
2293 priv->measurement_status |= MEASUREMENT_ACTIVE;
2294 rc = 0;
2295 break;
2296
2297 case 1: /* Command will not be handled */
2298 rc = -EAGAIN;
2299 break;
2300 }
2301
2302 dev_kfree_skb_any(cmd.meta.u.skb);
2303
2304 return rc;
2305}
2306#endif
2307
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002308static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002309 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002310{
2311
2312 tx_sta->status.ack_signal = 0;
2313 tx_sta->status.excessive_retries = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002314
2315 if (in_interrupt())
2316 ieee80211_tx_status_irqsafe(priv->hw,
2317 tx_sta->skb[0], &(tx_sta->status));
2318 else
2319 ieee80211_tx_status(priv->hw,
2320 tx_sta->skb[0], &(tx_sta->status));
2321
2322 tx_sta->skb[0] = NULL;
2323}
2324
2325/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002326 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002327 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002328 * When FW advances 'R' index, all entries between old and new 'R' index
2329 * need to be reclaimed. As result, some free space forms. If there is
2330 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002331 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002332int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002333{
Ron Rindjunsky16466902008-05-05 10:22:50 +08002334 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002335 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002336 int nfreed = 0;
2337
2338 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2339 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2340 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002341 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002342 return 0;
2343 }
2344
Tomas Winklerc54b6792008-03-06 17:36:53 -08002345 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002346 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002347 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002348 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002349 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002350 &(txq->txb[txq->q.read_ptr]));
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002351 iwl_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002352 } else if (nfreed > 1) {
2353 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002354 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002355 queue_work(priv->workqueue, &priv->restart);
2356 }
2357 nfreed++;
2358 }
2359
Zhu Yib481de92007-09-25 17:54:57 -07002360 return nfreed;
2361}
2362
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002363static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002364{
2365 status &= TX_STATUS_MSK;
2366 return (status == TX_STATUS_SUCCESS)
2367 || (status == TX_STATUS_DIRECT_DONE);
2368}
2369
2370/******************************************************************************
2371 *
2372 * Generic RX handler implementations
2373 *
2374 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002375#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002376
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002377static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002378 struct ieee80211_hdr *hdr)
2379{
2380 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2381 return IWL_AP_ID;
2382 else {
2383 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07002384 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002385 }
2386}
2387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002388static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002389 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002390{
2391 if (priv->txq[txq_id].txb[idx].skb[0])
2392 return (struct ieee80211_hdr *)priv->txq[txq_id].
2393 txb[idx].skb[0]->data;
2394 return NULL;
2395}
2396
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002397static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002398{
2399 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2400 tx_resp->frame_count);
2401 return le32_to_cpu(*scd_ssn) & MAX_SN;
2402
2403}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002404
2405/**
2406 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2407 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002408static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Tomas Winkler6def9762008-05-05 10:22:31 +08002409 struct iwl_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002410 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002411 u16 start_idx)
2412{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002413 u16 status;
2414 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002415 struct ieee80211_tx_status *tx_status = NULL;
2416 struct ieee80211_hdr *hdr = NULL;
2417 int i, sh;
2418 int txq_id, idx;
2419 u16 seq;
2420
2421 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002422 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002423
2424 agg->frame_count = tx_resp->frame_count;
2425 agg->start_idx = start_idx;
2426 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002427 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002428
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002429 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002430 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002431 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002432 status = le16_to_cpu(frame_status[0].status);
2433 seq = le16_to_cpu(frame_status[0].sequence);
2434 idx = SEQ_TO_INDEX(seq);
2435 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002436
Zhu Yib481de92007-09-25 17:54:57 -07002437 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002438 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2439 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002440
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002441 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002442 tx_status->retry_count = tx_resp->failure_frame;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002443 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002444 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002445 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002446 iwl4965_hwrate_to_tx_control(priv,
2447 le32_to_cpu(tx_resp->rate_n_flags),
2448 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002449 /* FIXME: code repetition end */
2450
2451 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2452 status & 0xff, tx_resp->failure_frame);
2453 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002454 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002455
2456 agg->wait_for_ba = 0;
2457 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002458 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002459 u64 bitmap = 0;
2460 int start = agg->start_idx;
2461
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002462 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002463 for (i = 0; i < agg->frame_count; i++) {
2464 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002465 status = le16_to_cpu(frame_status[i].status);
2466 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002467 idx = SEQ_TO_INDEX(seq);
2468 txq_id = SEQ_TO_QUEUE(seq);
2469
2470 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2471 AGG_TX_STATE_ABORT_MSK))
2472 continue;
2473
2474 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2475 agg->frame_count, txq_id, idx);
2476
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002477 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002478
2479 sc = le16_to_cpu(hdr->seq_ctrl);
2480 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2481 IWL_ERROR("BUG_ON idx doesn't match seq control"
2482 " idx=%d, seq_idx=%d, seq=%d\n",
2483 idx, SEQ_TO_SN(sc),
2484 hdr->seq_ctrl);
2485 return -1;
2486 }
2487
2488 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2489 i, idx, SEQ_TO_SN(sc));
2490
2491 sh = idx - start;
2492 if (sh > 64) {
2493 sh = (start - idx) + 0xff;
2494 bitmap = bitmap << sh;
2495 sh = 0;
2496 start = idx;
2497 } else if (sh < -64)
2498 sh = 0xff - (start - idx);
2499 else if (sh < 0) {
2500 sh = start - idx;
2501 start = idx;
2502 bitmap = bitmap << sh;
2503 sh = 0;
2504 }
2505 bitmap |= (1 << sh);
2506 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2507 start, (u32)(bitmap & 0xFFFFFFFF));
2508 }
2509
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002510 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002511 agg->start_idx = start;
2512 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002513 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002514 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002515 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002516
2517 if (bitmap)
2518 agg->wait_for_ba = 1;
2519 }
2520 return 0;
2521}
2522#endif
Zhu Yib481de92007-09-25 17:54:57 -07002523
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002524/**
2525 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2526 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002527static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002528 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002529{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002530 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002531 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2532 int txq_id = SEQ_TO_QUEUE(sequence);
2533 int index = SEQ_TO_INDEX(sequence);
Ron Rindjunsky16466902008-05-05 10:22:50 +08002534 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002535 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002536 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002537 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002538#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002539 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2540 struct ieee80211_hdr *hdr;
2541 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07002542#endif
2543
2544 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
2545 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2546 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002547 index, txq->q.n_bd, txq->q.write_ptr,
2548 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002549 return;
2550 }
2551
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002552#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002553 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2554 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002555
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002556 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07002557 tid = le16_to_cpu(*qc) & 0xf;
2558
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002559 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2560 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2561 IWL_ERROR("Station not known\n");
2562 return;
2563 }
2564
2565 if (txq->sched_retry) {
2566 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Tomas Winkler6def9762008-05-05 10:22:31 +08002567 struct iwl_ht_agg *agg = NULL;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002568
2569 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002570 return;
Zhu Yib481de92007-09-25 17:54:57 -07002571
2572 agg = &priv->stations[sta_id].tid[tid].agg;
2573
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002574 iwl4965_tx_status_reply_tx(priv, agg,
2575 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002576
2577 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002578 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002579 /* TODO: send BAR */
2580 }
2581
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002582 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002583 int freed, ampdu_q;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002584 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002585 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2586 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002587 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2588 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2589
2590 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2591 txq_id >= 0 && priv->mac80211_registered &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002592 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
2593 /* calculate mac80211 ampdu sw queue to wake */
2594 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
2595 priv->hw->queues;
2596 if (agg->state == IWL_AGG_OFF)
2597 ieee80211_wake_queue(priv->hw, txq_id);
2598 else
2599 ieee80211_wake_queue(priv->hw, ampdu_q);
2600 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002601 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002602 }
2603 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002604#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002605 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07002606
2607 tx_status->retry_count = tx_resp->failure_frame;
Zhu Yib481de92007-09-25 17:54:57 -07002608 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002609 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002610 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2611 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002612
Zhu Yib481de92007-09-25 17:54:57 -07002613 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002614 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07002615 status, le32_to_cpu(tx_resp->rate_n_flags),
2616 tx_resp->failure_frame);
2617
2618 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Tomas Winkler47c51962008-05-05 10:22:41 +08002619#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002620 if (index != -1) {
2621 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002622 if (tid != MAX_TID_COUNT)
2623 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2624 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002625 (txq_id >= 0) && priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002626 ieee80211_wake_queue(priv->hw, txq_id);
2627 if (tid != MAX_TID_COUNT)
2628 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002629 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002630 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002631#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002632
2633 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2634 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2635}
2636
2637
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002638static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002639 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002640{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002641 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002642 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002643 struct delayed_work *pwork;
2644
2645 palive = &pkt->u.alive_frame;
2646
2647 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2648 "0x%01X 0x%01X\n",
2649 palive->is_valid, palive->ver_type,
2650 palive->ver_subtype);
2651
2652 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2653 IWL_DEBUG_INFO("Initialization Alive received.\n");
2654 memcpy(&priv->card_alive_init,
2655 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002656 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002657 pwork = &priv->init_alive_start;
2658 } else {
2659 IWL_DEBUG_INFO("Runtime Alive received.\n");
2660 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002661 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002662 pwork = &priv->alive_start;
2663 }
2664
2665 /* We delay the ALIVE response by 5ms to
2666 * give the HW RF Kill time to activate... */
2667 if (palive->is_valid == UCODE_VALID_OK)
2668 queue_delayed_work(priv->workqueue, pwork,
2669 msecs_to_jiffies(5));
2670 else
2671 IWL_WARNING("uCode did not respond OK.\n");
2672}
2673
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002674static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002675 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002676{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002677 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002678
2679 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2680 return;
2681}
2682
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002683static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002684 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002685{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002686 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002687
2688 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2689 "seq 0x%04X ser 0x%08X\n",
2690 le32_to_cpu(pkt->u.err_resp.error_type),
2691 get_cmd_string(pkt->u.err_resp.cmd_id),
2692 pkt->u.err_resp.cmd_id,
2693 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2694 le32_to_cpu(pkt->u.err_resp.error_info));
2695}
2696
2697#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2698
Tomas Winklera55360e2008-05-05 10:22:28 +08002699static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002700{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002701 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002702 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
2703 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002704 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2705 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2706 rxon->channel = csa->channel;
2707 priv->staging_rxon.channel = csa->channel;
2708}
2709
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002710static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002711 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002712{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002713#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08002714 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002715 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002716
2717 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002718 IWL_DEBUG(IWL_DL_11H,
2719 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07002720 return;
2721 }
2722
2723 memcpy(&priv->measure_report, report, sizeof(*report));
2724 priv->measurement_status |= MEASUREMENT_READY;
2725#endif
2726}
2727
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002728static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002729 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002730{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002731#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002732 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002733 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002734 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2735 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2736#endif
2737}
2738
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002739static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002740 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002741{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002742 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002743 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2744 "notification for %s:\n",
2745 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08002746 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002747}
2748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002749static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002750{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002751 struct iwl_priv *priv =
2752 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002753 struct sk_buff *beacon;
2754
2755 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002756 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002757
2758 if (!beacon) {
2759 IWL_ERROR("update beacon failed\n");
2760 return;
2761 }
2762
2763 mutex_lock(&priv->mutex);
2764 /* new beacon skb is allocated every time; dispose previous.*/
2765 if (priv->ibss_beacon)
2766 dev_kfree_skb(priv->ibss_beacon);
2767
2768 priv->ibss_beacon = beacon;
2769 mutex_unlock(&priv->mutex);
2770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002771 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002772}
2773
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002774static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002775 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002776{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002777#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002778 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002779 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2780 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002781
2782 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2783 "tsf %d %d rate %d\n",
2784 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2785 beacon->beacon_notify_hdr.failure_frame,
2786 le32_to_cpu(beacon->ibss_mgr_status),
2787 le32_to_cpu(beacon->high_tsf),
2788 le32_to_cpu(beacon->low_tsf), rate);
2789#endif
2790
2791 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2792 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2793 queue_work(priv->workqueue, &priv->beacon_update);
2794}
2795
2796/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002797static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002798 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002799{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002800#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002801 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002802 struct iwl4965_scanreq_notification *notif =
2803 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002804
2805 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2806#endif
2807}
2808
2809/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002810static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002811 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002812{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002813 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002814 struct iwl4965_scanstart_notification *notif =
2815 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002816 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2817 IWL_DEBUG_SCAN("Scan start: "
2818 "%d [802.11%s] "
2819 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2820 notif->channel,
2821 notif->band ? "bg" : "a",
2822 notif->tsf_high,
2823 notif->tsf_low, notif->status, notif->beacon_timer);
2824}
2825
2826/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002827static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002828 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002829{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002830 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002831 struct iwl4965_scanresults_notification *notif =
2832 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002833
2834 IWL_DEBUG_SCAN("Scan ch.res: "
2835 "%d [802.11%s] "
2836 "(TSF: 0x%08X:%08X) - %d "
2837 "elapsed=%lu usec (%dms since last)\n",
2838 notif->channel,
2839 notif->band ? "bg" : "a",
2840 le32_to_cpu(notif->tsf_high),
2841 le32_to_cpu(notif->tsf_low),
2842 le32_to_cpu(notif->statistics[0]),
2843 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2844 jiffies_to_msecs(elapsed_jiffies
2845 (priv->last_scan_jiffies, jiffies)));
2846
2847 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002848 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002849}
2850
2851/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002852static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002853 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002854{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002855 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002856 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002857
2858 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2859 scan_notif->scanned_channels,
2860 scan_notif->tsf_low,
2861 scan_notif->tsf_high, scan_notif->status);
2862
2863 /* The HW is no longer scanning */
2864 clear_bit(STATUS_SCAN_HW, &priv->status);
2865
2866 /* The scan completion notification came in, so kill that timer... */
2867 cancel_delayed_work(&priv->scan_check);
2868
2869 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
2870 (priv->scan_bands == 2) ? "2.4" : "5.2",
2871 jiffies_to_msecs(elapsed_jiffies
2872 (priv->scan_pass_start, jiffies)));
2873
2874 /* Remove this scanned band from the list
2875 * of pending bands to scan */
2876 priv->scan_bands--;
2877
2878 /* If a request to abort was given, or the scan did not succeed
2879 * then we reset the scan state machine and terminate,
2880 * re-queuing another scan if one has been requested */
2881 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2882 IWL_DEBUG_INFO("Aborted scan completed.\n");
2883 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2884 } else {
2885 /* If there are more bands on this scan pass reschedule */
2886 if (priv->scan_bands > 0)
2887 goto reschedule;
2888 }
2889
2890 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002891 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002892 IWL_DEBUG_INFO("Setting scan to off\n");
2893
2894 clear_bit(STATUS_SCANNING, &priv->status);
2895
2896 IWL_DEBUG_INFO("Scan took %dms\n",
2897 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2898
2899 queue_work(priv->workqueue, &priv->scan_completed);
2900
2901 return;
2902
2903reschedule:
2904 priv->scan_pass_start = jiffies;
2905 queue_work(priv->workqueue, &priv->request_scan);
2906}
2907
2908/* Handle notification from uCode that card's power state is changing
2909 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002910static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002911 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002912{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002913 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002914 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2915 unsigned long status = priv->status;
2916
2917 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2918 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2919 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2920
2921 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
2922 RF_CARD_DISABLED)) {
2923
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002924 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002925 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2926
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002927 if (!iwl_grab_nic_access(priv)) {
2928 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002929 priv, HBUS_TARG_MBX_C,
2930 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2931
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002932 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002933 }
2934
2935 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002936 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002937 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002938 if (!iwl_grab_nic_access(priv)) {
2939 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002940 priv, HBUS_TARG_MBX_C,
2941 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2942
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002943 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002944 }
2945 }
2946
2947 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002948 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002949 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002950 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2951 if (!iwl_grab_nic_access(priv))
2952 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002953 }
2954 }
2955
2956 if (flags & HW_CARD_DISABLED)
2957 set_bit(STATUS_RF_KILL_HW, &priv->status);
2958 else
2959 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2960
2961
2962 if (flags & SW_CARD_DISABLED)
2963 set_bit(STATUS_RF_KILL_SW, &priv->status);
2964 else
2965 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2966
2967 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002968 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002969
2970 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2971 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2972 (test_bit(STATUS_RF_KILL_SW, &status) !=
2973 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2974 queue_work(priv->workqueue, &priv->rf_kill);
2975 else
2976 wake_up_interruptible(&priv->wait_command_queue);
2977}
2978
2979/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002980 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07002981 *
2982 * Setup the RX handlers for each of the reply types sent from the uCode
2983 * to the host.
2984 *
2985 * This function chains into the hardware specific files for them to setup
2986 * any hardware specific handlers as well.
2987 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002988static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002989{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002990 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
2991 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
2992 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
2993 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07002994 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002995 iwl4965_rx_spectrum_measure_notif;
2996 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002997 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002998 iwl4965_rx_pm_debug_statistics_notif;
2999 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003000
Ben Cahill9fbab512007-11-29 11:09:47 +08003001 /*
3002 * The same handler is used for both the REPLY to a discrete
3003 * statistics request from the host as well as for the periodic
3004 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003005 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003006 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3007 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003008
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003009 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3010 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003011 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003012 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003013 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003014 iwl4965_rx_scan_complete_notif;
3015 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3016 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003017
Ben Cahill9fbab512007-11-29 11:09:47 +08003018 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07003019 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003020}
3021
3022/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003023 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003024 * @rxb: Rx buffer to reclaim
3025 *
3026 * If an Rx buffer has an async callback associated with it the callback
3027 * will be executed. The attached skb (if present) will only be freed
3028 * if the callback returns 1
3029 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003030static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08003031 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003032{
Tomas Winklerdb11d632008-05-05 10:22:33 +08003033 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003034 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3035 int txq_id = SEQ_TO_QUEUE(sequence);
3036 int index = SEQ_TO_INDEX(sequence);
3037 int huge = sequence & SEQ_HUGE_FRAME;
3038 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07003039 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003040
3041 /* If a Tx command is being handled and it isn't in the actual
3042 * command queue then there a command routing bug has been introduced
3043 * in the queue management code. */
3044 if (txq_id != IWL_CMD_QUEUE_NUM)
3045 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3046 txq_id, pkt->hdr.cmd);
3047 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3048
3049 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3050 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3051
3052 /* Input error checking is done when commands are added to queue. */
3053 if (cmd->meta.flags & CMD_WANT_SKB) {
3054 cmd->meta.source->u.skb = rxb->skb;
3055 rxb->skb = NULL;
3056 } else if (cmd->meta.u.callback &&
3057 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3058 rxb->skb = NULL;
3059
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003060 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003061
3062 if (!(cmd->meta.flags & CMD_ASYNC)) {
3063 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3064 wake_up_interruptible(&priv->wait_command_queue);
3065 }
3066}
3067
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003068/*
3069 * this should be called while priv->lock is locked
3070*/
Tomas Winklera55360e2008-05-05 10:22:28 +08003071static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003072{
Tomas Winklera55360e2008-05-05 10:22:28 +08003073 iwl_rx_allocate(priv);
3074 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003075}
3076
3077
Zhu Yib481de92007-09-25 17:54:57 -07003078/**
Tomas Winklera55360e2008-05-05 10:22:28 +08003079 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003080 *
3081 * Uses the priv->rx_handlers callback function array to invoke
3082 * the appropriate handlers, including command responses,
3083 * frame-received notifications, and other notifications.
3084 */
Tomas Winklera55360e2008-05-05 10:22:28 +08003085void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003086{
Tomas Winklera55360e2008-05-05 10:22:28 +08003087 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08003088 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08003089 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003090 u32 r, i;
3091 int reclaim;
3092 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003093 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003094 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003095
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003096 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3097 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08003098 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003099 i = rxq->read;
3100
3101 /* Rx interrupt, but nothing sent from uCode */
3102 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08003103 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07003104
Tomas Winklera55360e2008-05-05 10:22:28 +08003105 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003106 fill_rx = 1;
3107
Zhu Yib481de92007-09-25 17:54:57 -07003108 while (i != r) {
3109 rxb = rxq->queue[i];
3110
Ben Cahill9fbab512007-11-29 11:09:47 +08003111 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003112 * then a bug has been introduced in the queue refilling
3113 * routines -- catch it here */
3114 BUG_ON(rxb == NULL);
3115
3116 rxq->queue[i] = NULL;
3117
3118 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003119 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07003120 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08003121 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003122
3123 /* Reclaim a command buffer only if this packet is a response
3124 * to a (driver-originated) command.
3125 * If the packet (e.g. Rx frame) originated from uCode,
3126 * there is no command buffer to reclaim.
3127 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3128 * but apparently a few don't get set; catch them here. */
3129 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3130 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07003131 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08003132 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07003133 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3134 (pkt->hdr.cmd != REPLY_TX);
3135
3136 /* Based on type of command response or notification,
3137 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003138 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003139 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003140 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
3141 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003142 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3143 } else {
3144 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08003145 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07003146 "r %d i %d No handler needed for %s, 0x%02x\n",
3147 r, i, get_cmd_string(pkt->hdr.cmd),
3148 pkt->hdr.cmd);
3149 }
3150
3151 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003152 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07003153 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003154 * as we reclaim the driver command queue */
3155 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003156 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003157 else
3158 IWL_WARNING("Claim null rxb?\n");
3159 }
3160
3161 /* For now we just don't re-use anything. We can tweak this
3162 * later to try and re-use notification packets and SKBs that
3163 * fail to Rx correctly */
3164 if (rxb->skb != NULL) {
3165 priv->alloc_rxb_skb--;
3166 dev_kfree_skb_any(rxb->skb);
3167 rxb->skb = NULL;
3168 }
3169
3170 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003171 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003172 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003173 spin_lock_irqsave(&rxq->lock, flags);
3174 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3175 spin_unlock_irqrestore(&rxq->lock, flags);
3176 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003177 /* If there are a lot of unused frames,
3178 * restock the Rx queue so ucode wont assert. */
3179 if (fill_rx) {
3180 count++;
3181 if (count >= 8) {
3182 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003183 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003184 count = 0;
3185 }
3186 }
Zhu Yib481de92007-09-25 17:54:57 -07003187 }
3188
3189 /* Backtrack one entry */
3190 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003191 iwl_rx_queue_restock(priv);
3192}
3193/* Convert linear signal-to-noise ratio into dB */
3194static u8 ratio2dB[100] = {
3195/* 0 1 2 3 4 5 6 7 8 9 */
3196 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3197 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3198 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3199 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3200 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3201 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3202 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3203 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3204 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3205 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3206};
3207
3208/* Calculates a relative dB value from a ratio of linear
3209 * (i.e. not dB) signal levels.
3210 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
3211int iwl4965_calc_db_from_ratio(int sig_ratio)
3212{
3213 /* 1000:1 or higher just report as 60 dB */
3214 if (sig_ratio >= 1000)
3215 return 60;
3216
3217 /* 100:1 or higher, divide by 10 and use table,
3218 * add 20 dB to make up for divide by 10 */
3219 if (sig_ratio >= 100)
3220 return (20 + (int)ratio2dB[sig_ratio/10]);
3221
3222 /* We shouldn't see this */
3223 if (sig_ratio < 1)
3224 return 0;
3225
3226 /* Use table for ratios 1:1 - 99:1 */
3227 return (int)ratio2dB[sig_ratio];
3228}
3229
3230#define PERFECT_RSSI (-20) /* dBm */
3231#define WORST_RSSI (-95) /* dBm */
3232#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3233
3234/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3235 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3236 * about formulas used below. */
3237int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
3238{
3239 int sig_qual;
3240 int degradation = PERFECT_RSSI - rssi_dbm;
3241
3242 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3243 * as indicator; formula is (signal dbm - noise dbm).
3244 * SNR at or above 40 is a great signal (100%).
3245 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3246 * Weakest usable signal is usually 10 - 15 dB SNR. */
3247 if (noise_dbm) {
3248 if (rssi_dbm - noise_dbm >= 40)
3249 return 100;
3250 else if (rssi_dbm < noise_dbm)
3251 return 0;
3252 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3253
3254 /* Else use just the signal level.
3255 * This formula is a least squares fit of data points collected and
3256 * compared with a reference system that had a percentage (%) display
3257 * for signal quality. */
3258 } else
3259 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3260 (15 * RSSI_RANGE + 62 * degradation)) /
3261 (RSSI_RANGE * RSSI_RANGE);
3262
3263 if (sig_qual > 100)
3264 sig_qual = 100;
3265 else if (sig_qual < 1)
3266 sig_qual = 0;
3267
3268 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07003269}
3270
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003271/**
3272 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
3273 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003274static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +08003275 struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003276{
3277 u32 reg = 0;
3278 int rc = 0;
3279 int txq_id = txq->q.id;
3280
3281 if (txq->need_update == 0)
3282 return rc;
3283
3284 /* if we're trying to save power */
3285 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3286 /* wake up nic if it's powered down ...
3287 * uCode will wake up, and interrupt us again, so next
3288 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003289 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003290
3291 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3292 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003293 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003294 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3295 return rc;
3296 }
3297
3298 /* restore this queue's parameters in nic hardware. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003299 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003300 if (rc)
3301 return rc;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003302 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003303 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003304 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003305
3306 /* else not in power-save mode, uCode will never sleep when we're
3307 * trying to tx (during RFKILL, we're not trying to tx). */
3308 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003309 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003310 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003311
3312 txq->need_update = 0;
3313
3314 return rc;
3315}
3316
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003317#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003318static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003319{
Ester Kummerbf403db2008-05-05 10:22:40 +08003320 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07003321 DECLARE_MAC_BUF(mac);
3322
Zhu Yib481de92007-09-25 17:54:57 -07003323 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08003324 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003325 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3326 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3327 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3328 le32_to_cpu(rxon->filter_flags));
3329 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3330 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3331 rxon->ofdm_basic_rates);
3332 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07003333 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3334 print_mac(mac, rxon->node_addr));
3335 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3336 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003337 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3338}
3339#endif
3340
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003341static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003342{
3343 IWL_DEBUG_ISR("Enabling interrupts\n");
3344 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003345 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003346}
3347
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003348/* call this function to flush any scheduled tasklet */
3349static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3350{
3351 /* wait to make sure we flush pedding tasklet*/
3352 synchronize_irq(priv->pci_dev->irq);
3353 tasklet_kill(&priv->irq_tasklet);
3354}
3355
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003356static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003357{
3358 clear_bit(STATUS_INT_ENABLED, &priv->status);
3359
3360 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003361 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003362
3363 /* acknowledge/clear/reset any interrupts still pending
3364 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003365 iwl_write32(priv, CSR_INT, 0xffffffff);
3366 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003367 IWL_DEBUG_ISR("Disabled interrupts\n");
3368}
3369
3370static const char *desc_lookup(int i)
3371{
3372 switch (i) {
3373 case 1:
3374 return "FAIL";
3375 case 2:
3376 return "BAD_PARAM";
3377 case 3:
3378 return "BAD_CHECKSUM";
3379 case 4:
3380 return "NMI_INTERRUPT";
3381 case 5:
3382 return "SYSASSERT";
3383 case 6:
3384 return "FATAL_ERROR";
3385 }
3386
3387 return "UNKNOWN";
3388}
3389
3390#define ERROR_START_OFFSET (1 * sizeof(u32))
3391#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3392
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003393static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003394{
3395 u32 data2, line;
3396 u32 desc, time, count, base, data1;
3397 u32 blink1, blink2, ilink1, ilink2;
3398 int rc;
3399
3400 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3401
Tomas Winkler57aab752008-04-14 21:16:03 -07003402 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003403 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3404 return;
3405 }
3406
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003407 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003408 if (rc) {
3409 IWL_WARNING("Can not read from adapter at this time.\n");
3410 return;
3411 }
3412
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003413 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003414
3415 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3416 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003417 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003418 }
3419
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003420 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3421 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3422 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3423 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3424 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3425 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3426 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3427 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3428 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003429
3430 IWL_ERROR("Desc Time "
3431 "data1 data2 line\n");
3432 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3433 desc_lookup(desc), desc, time, data1, data2, line);
3434 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3435 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3436 ilink1, ilink2);
3437
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003438 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003439}
3440
3441#define EVENT_START_OFFSET (4 * sizeof(u32))
3442
3443/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003444 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003445 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003446 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003447 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003448static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003449 u32 num_events, u32 mode)
3450{
3451 u32 i;
3452 u32 base; /* SRAM byte address of event log header */
3453 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3454 u32 ptr; /* SRAM byte address of log data */
3455 u32 ev, time, data; /* event log data */
3456
3457 if (num_events == 0)
3458 return;
3459
3460 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3461
3462 if (mode == 0)
3463 event_size = 2 * sizeof(u32);
3464 else
3465 event_size = 3 * sizeof(u32);
3466
3467 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3468
3469 /* "time" is actually "data" for mode 0 (no timestamp).
3470 * place event id # at far right for easier visual parsing. */
3471 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003472 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003473 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003474 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003475 ptr += sizeof(u32);
3476 if (mode == 0)
3477 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
3478 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003479 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003480 ptr += sizeof(u32);
3481 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
3482 }
3483 }
3484}
3485
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003486static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003487{
3488 int rc;
3489 u32 base; /* SRAM byte address of event log header */
3490 u32 capacity; /* event log capacity in # entries */
3491 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3492 u32 num_wraps; /* # times uCode wrapped to top of log */
3493 u32 next_entry; /* index of next entry to be written by uCode */
3494 u32 size; /* # entries that we'll print */
3495
3496 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07003497 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003498 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
3499 return;
3500 }
3501
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003502 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003503 if (rc) {
3504 IWL_WARNING("Can not read from adapter at this time.\n");
3505 return;
3506 }
3507
3508 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003509 capacity = iwl_read_targ_mem(priv, base);
3510 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3511 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3512 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003513
3514 size = num_wraps ? capacity : next_entry;
3515
3516 /* bail out if nothing in log */
3517 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08003518 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003519 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003520 return;
3521 }
3522
Zhu Yi583fab32007-09-27 11:27:30 +08003523 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003524 size, num_wraps);
3525
3526 /* if uCode has wrapped back to top of log, start at the oldest entry,
3527 * i.e the next one that uCode would fill. */
3528 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003529 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003530 capacity - next_entry, mode);
3531
3532 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003533 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003534
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003535 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003536}
3537
3538/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003539 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003540 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003541static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003542{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003543 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07003544 set_bit(STATUS_FW_ERROR, &priv->status);
3545
3546 /* Cancel currently queued command. */
3547 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3548
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003549#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003550 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003551 iwl4965_dump_nic_error_log(priv);
3552 iwl4965_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08003553 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003554 }
3555#endif
3556
3557 wake_up_interruptible(&priv->wait_command_queue);
3558
3559 /* Keep the restart process from trying to send host
3560 * commands by clearing the INIT status bit */
3561 clear_bit(STATUS_READY, &priv->status);
3562
3563 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003564 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07003565 "Restarting adapter due to uCode error.\n");
3566
Tomas Winkler3109ece2008-03-28 16:33:35 -07003567 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003568 memcpy(&priv->recovery_rxon, &priv->active_rxon,
3569 sizeof(priv->recovery_rxon));
3570 priv->error_recovering = 1;
3571 }
3572 queue_work(priv->workqueue, &priv->restart);
3573 }
3574}
3575
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003576static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003577{
3578 unsigned long flags;
3579
3580 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
3581 sizeof(priv->staging_rxon));
3582 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003583 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003585 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07003586
3587 spin_lock_irqsave(&priv->lock, flags);
3588 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
3589 priv->error_recovering = 0;
3590 spin_unlock_irqrestore(&priv->lock, flags);
3591}
3592
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003593static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003594{
3595 u32 inta, handled = 0;
3596 u32 inta_fh;
3597 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003598#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003599 u32 inta_mask;
3600#endif
3601
3602 spin_lock_irqsave(&priv->lock, flags);
3603
3604 /* Ack/clear/reset pending uCode interrupts.
3605 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3606 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003607 inta = iwl_read32(priv, CSR_INT);
3608 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003609
3610 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3611 * Any new interrupts that happen after this, either while we're
3612 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003613 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3614 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003615
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003616#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003617 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003618 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003619 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003620 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3621 inta, inta_mask, inta_fh);
3622 }
3623#endif
3624
3625 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3626 * atomic, make sure that inta covers all the interrupts that
3627 * we've discovered, even if FH interrupt came in just after
3628 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003629 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003630 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003631 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003632 inta |= CSR_INT_BIT_FH_TX;
3633
3634 /* Now service all interrupt bits discovered above. */
3635 if (inta & CSR_INT_BIT_HW_ERR) {
3636 IWL_ERROR("Microcode HW error detected. Restarting.\n");
3637
3638 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003639 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003640
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003641 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003642
3643 handled |= CSR_INT_BIT_HW_ERR;
3644
3645 spin_unlock_irqrestore(&priv->lock, flags);
3646
3647 return;
3648 }
3649
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003650#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003651 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003652 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003653 if (inta & CSR_INT_BIT_SCD)
3654 IWL_DEBUG_ISR("Scheduler finished to transmit "
3655 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003656
3657 /* Alive notification via Rx interrupt will do the real work */
3658 if (inta & CSR_INT_BIT_ALIVE)
3659 IWL_DEBUG_ISR("Alive interrupt\n");
3660 }
3661#endif
3662 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003663 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003664
Ben Cahill9fbab512007-11-29 11:09:47 +08003665 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07003666 if (inta & CSR_INT_BIT_RF_KILL) {
3667 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003668 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07003669 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
3670 hw_rf_kill = 1;
3671
Ester Kummerf3d67992008-05-06 11:05:12 +08003672 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07003673 hw_rf_kill ? "disable radio":"enable radio");
3674
3675 /* Queue restart only if RF_KILL switch was set to "kill"
3676 * when we loaded driver, and is now set to "enable".
3677 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08003678 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08003679 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
3680 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003681 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08003682 }
Zhu Yib481de92007-09-25 17:54:57 -07003683
3684 handled |= CSR_INT_BIT_RF_KILL;
3685 }
3686
Ben Cahill9fbab512007-11-29 11:09:47 +08003687 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07003688 if (inta & CSR_INT_BIT_CT_KILL) {
3689 IWL_ERROR("Microcode CT kill error detected.\n");
3690 handled |= CSR_INT_BIT_CT_KILL;
3691 }
3692
3693 /* Error detected by uCode */
3694 if (inta & CSR_INT_BIT_SW_ERR) {
3695 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
3696 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003697 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003698 handled |= CSR_INT_BIT_SW_ERR;
3699 }
3700
3701 /* uCode wakes up after power-down sleep */
3702 if (inta & CSR_INT_BIT_WAKEUP) {
3703 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08003704 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003705 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
3706 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
3707 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
3708 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
3709 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
3710 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003711
3712 handled |= CSR_INT_BIT_WAKEUP;
3713 }
3714
3715 /* All uCode command responses, including Tx command responses,
3716 * Rx "responses" (frame-received notification), and other
3717 * notifications from uCode come through here*/
3718 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08003719 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003720 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3721 }
3722
3723 if (inta & CSR_INT_BIT_FH_TX) {
3724 IWL_DEBUG_ISR("Tx interrupt\n");
3725 handled |= CSR_INT_BIT_FH_TX;
3726 }
3727
3728 if (inta & ~handled)
3729 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
3730
3731 if (inta & ~CSR_INI_SET_MASK) {
3732 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
3733 inta & ~CSR_INI_SET_MASK);
3734 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
3735 }
3736
3737 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003738 /* only Re-enable if diabled by irq */
3739 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3740 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003741
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003742#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003743 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003744 inta = iwl_read32(priv, CSR_INT);
3745 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3746 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003747 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3748 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3749 }
3750#endif
3751 spin_unlock_irqrestore(&priv->lock, flags);
3752}
3753
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003754static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003755{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003756 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003757 u32 inta, inta_mask;
3758 u32 inta_fh;
3759 if (!priv)
3760 return IRQ_NONE;
3761
3762 spin_lock(&priv->lock);
3763
3764 /* Disable (but don't clear!) interrupts here to avoid
3765 * back-to-back ISRs and sporadic interrupts from our NIC.
3766 * If we have something to service, the tasklet will re-enable ints.
3767 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003768 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3769 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003770
3771 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003772 inta = iwl_read32(priv, CSR_INT);
3773 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003774
3775 /* Ignore interrupt if there's nothing in NIC to service.
3776 * This may be due to IRQ shared with another device,
3777 * or due to sporadic interrupts thrown from our NIC. */
3778 if (!inta && !inta_fh) {
3779 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3780 goto none;
3781 }
3782
3783 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08003784 /* Hardware disappeared. It might have already raised
3785 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07003786 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08003787 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003788 }
3789
3790 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3791 inta, inta_mask, inta_fh);
3792
Joonwoo Park25c03d82008-01-23 10:15:20 -08003793 inta &= ~CSR_INT_BIT_SCD;
3794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003795 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003796 if (likely(inta || inta_fh))
3797 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07003798
Oliver Neukum66fbb542007-11-15 09:31:10 +08003799 unplugged:
3800 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07003801 return IRQ_HANDLED;
3802
3803 none:
3804 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003805 /* only Re-enable if diabled by irq */
3806 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3807 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003808 spin_unlock(&priv->lock);
3809 return IRQ_NONE;
3810}
3811
Zhu Yib481de92007-09-25 17:54:57 -07003812/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3813 * sending probe req. This should be set long enough to hear probe responses
3814 * from more than one AP. */
3815#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
3816#define IWL_ACTIVE_DWELL_TIME_52 (10)
3817
3818/* For faster active scanning, scan will move to the next channel if fewer than
3819 * PLCP_QUIET_THRESH packets are heard on this channel within
3820 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3821 * time if it's a quiet channel (nothing responded to our probe, and there's
3822 * no other traffic).
3823 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3824#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
3825#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
3826
3827/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3828 * Must be set longer than active dwell time.
3829 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
3830#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
3831#define IWL_PASSIVE_DWELL_TIME_52 (10)
3832#define IWL_PASSIVE_DWELL_BASE (100)
3833#define IWL_CHANNEL_TUNE_TIME 5
3834
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003835static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003836 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003837{
Johannes Berg8318d782008-01-24 19:38:38 +01003838 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003839 return IWL_ACTIVE_DWELL_TIME_52;
3840 else
3841 return IWL_ACTIVE_DWELL_TIME_24;
3842}
3843
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003844static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003845 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003846{
Johannes Berg8318d782008-01-24 19:38:38 +01003847 u16 active = iwl4965_get_active_dwell_time(priv, band);
3848 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003849 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
3850 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
3851
Tomas Winkler3109ece2008-03-28 16:33:35 -07003852 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003853 /* If we're associated, we clamp the maximum passive
3854 * dwell time to be 98% of the beacon interval (minus
3855 * 2 * channel tune time) */
3856 passive = priv->beacon_int;
3857 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
3858 passive = IWL_PASSIVE_DWELL_BASE;
3859 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
3860 }
3861
3862 if (passive <= active)
3863 passive = active + 1;
3864
3865 return passive;
3866}
3867
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003868static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003869 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07003870 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003871 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07003872{
3873 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01003874 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003875 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003876 u16 passive_dwell = 0;
3877 u16 active_dwell = 0;
3878 int added, i;
3879
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07003880 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01003881 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07003882 return 0;
3883
Johannes Berg8318d782008-01-24 19:38:38 +01003884 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07003885
Johannes Berg8318d782008-01-24 19:38:38 +01003886 active_dwell = iwl4965_get_active_dwell_time(priv, band);
3887 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07003888
Johannes Berg8318d782008-01-24 19:38:38 +01003889 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02003890 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3891 continue;
3892
Johannes Berg8318d782008-01-24 19:38:38 +01003893 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07003894
Assaf Krauss8622e702008-03-21 13:53:43 -07003895 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08003896 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003897 if (!is_channel_valid(ch_info)) {
3898 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
3899 scan_ch->channel);
3900 continue;
3901 }
3902
3903 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01003904 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07003905 scan_ch->type = 0; /* passive */
3906 else
3907 scan_ch->type = 1; /* active */
3908
3909 if (scan_ch->type & 1)
3910 scan_ch->type |= (direct_mask << 1);
3911
Zhu Yib481de92007-09-25 17:54:57 -07003912 scan_ch->active_dwell = cpu_to_le16(active_dwell);
3913 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
3914
Ben Cahill9fbab512007-11-29 11:09:47 +08003915 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07003916 scan_ch->tpc.dsp_atten = 110;
3917 /* scan_pwr_info->tpc.dsp_atten; */
3918
3919 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01003920 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003921 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
3922 else {
3923 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
3924 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08003925 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08003926 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07003927 */
3928 }
3929
3930 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
3931 scan_ch->channel,
3932 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
3933 (scan_ch->type & 1) ?
3934 active_dwell : passive_dwell);
3935
3936 scan_ch++;
3937 added++;
3938 }
3939
3940 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
3941 return added;
3942}
3943
Zhu Yib481de92007-09-25 17:54:57 -07003944/******************************************************************************
3945 *
3946 * uCode download functions
3947 *
3948 ******************************************************************************/
3949
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003950static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003951{
Tomas Winkler98c92212008-01-14 17:46:20 -08003952 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
3953 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
3954 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3955 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
3956 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3957 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07003958}
3959
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003960static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003961{
3962 /* Remove all resets to allow NIC to operate */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003963 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003964}
3965
Tomas Winkler90e759d2007-11-29 11:09:41 +08003966
Zhu Yib481de92007-09-25 17:54:57 -07003967/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003968 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07003969 *
3970 * Copy into buffers for card to fetch via bus-mastering
3971 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003972static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003973{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003974 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003975 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003976 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08003977 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07003978 u8 *src;
3979 size_t len;
3980 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
3981
3982 /* Ask kernel firmware_class module to get the boot firmware off disk.
3983 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003984 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
3985 if (ret < 0) {
3986 IWL_ERROR("%s firmware file req failed: Reason %d\n",
3987 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07003988 goto error;
3989 }
3990
3991 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3992 name, ucode_raw->size);
3993
3994 /* Make sure that we got at least our header! */
3995 if (ucode_raw->size < sizeof(*ucode)) {
3996 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08003997 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003998 goto err_release;
3999 }
4000
4001 /* Data from ucode file: header followed by uCode images */
4002 ucode = (void *)ucode_raw->data;
4003
4004 ver = le32_to_cpu(ucode->ver);
4005 inst_size = le32_to_cpu(ucode->inst_size);
4006 data_size = le32_to_cpu(ucode->data_size);
4007 init_size = le32_to_cpu(ucode->init_size);
4008 init_data_size = le32_to_cpu(ucode->init_data_size);
4009 boot_size = le32_to_cpu(ucode->boot_size);
4010
4011 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
4012 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
4013 inst_size);
4014 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
4015 data_size);
4016 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
4017 init_size);
4018 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
4019 init_data_size);
4020 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
4021 boot_size);
4022
4023 /* Verify size of file vs. image size info in file's header */
4024 if (ucode_raw->size < sizeof(*ucode) +
4025 inst_size + data_size + init_size +
4026 init_data_size + boot_size) {
4027
4028 IWL_DEBUG_INFO("uCode file size %d too small\n",
4029 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004030 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004031 goto err_release;
4032 }
4033
4034 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004035 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004036 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4037 inst_size);
4038 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004039 goto err_release;
4040 }
4041
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004042 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004043 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4044 data_size);
4045 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004046 goto err_release;
4047 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004048 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004049 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004050 ("uCode init instr len %d too large to fit in\n",
4051 init_size);
4052 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004053 goto err_release;
4054 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004055 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004056 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004057 ("uCode init data len %d too large to fit in\n",
4058 init_data_size);
4059 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004060 goto err_release;
4061 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07004062 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07004063 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08004064 ("uCode boot instr len %d too large to fit in\n",
4065 boot_size);
4066 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004067 goto err_release;
4068 }
4069
4070 /* Allocate ucode buffers for card's bus-master loading ... */
4071
4072 /* Runtime instructions and 2 copies of data:
4073 * 1) unmodified from disk
4074 * 2) backup cache for save/restore during power-downs */
4075 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004076 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004077
4078 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004079 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004080
4081 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004082 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004083
4084 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004085 if (init_size && init_data_size) {
4086 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004087 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07004088
Tomas Winkler90e759d2007-11-29 11:09:41 +08004089 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004090 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004091
4092 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4093 goto err_pci_alloc;
4094 }
Zhu Yib481de92007-09-25 17:54:57 -07004095
4096 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08004097 if (boot_size) {
4098 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004099 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004100
Tomas Winkler90e759d2007-11-29 11:09:41 +08004101 if (!priv->ucode_boot.v_addr)
4102 goto err_pci_alloc;
4103 }
Zhu Yib481de92007-09-25 17:54:57 -07004104
4105 /* Copy images into buffers for card's bus-master reads ... */
4106
4107 /* Runtime instructions (first block of data in file) */
4108 src = &ucode->data[0];
4109 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004110 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004111 memcpy(priv->ucode_code.v_addr, src, len);
4112 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4113 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4114
4115 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004116 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004117 src = &ucode->data[inst_size];
4118 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004119 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004120 memcpy(priv->ucode_data.v_addr, src, len);
4121 memcpy(priv->ucode_data_backup.v_addr, src, len);
4122
4123 /* Initialization instructions (3rd block) */
4124 if (init_size) {
4125 src = &ucode->data[inst_size + data_size];
4126 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004127 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4128 len);
Zhu Yib481de92007-09-25 17:54:57 -07004129 memcpy(priv->ucode_init.v_addr, src, len);
4130 }
4131
4132 /* Initialization data (4th block) */
4133 if (init_data_size) {
4134 src = &ucode->data[inst_size + data_size + init_size];
4135 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004136 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
4137 len);
Zhu Yib481de92007-09-25 17:54:57 -07004138 memcpy(priv->ucode_init_data.v_addr, src, len);
4139 }
4140
4141 /* Bootstrap instructions (5th block) */
4142 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4143 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004144 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004145 memcpy(priv->ucode_boot.v_addr, src, len);
4146
4147 /* We have our copies now, allow OS release its copies */
4148 release_firmware(ucode_raw);
4149 return 0;
4150
4151 err_pci_alloc:
4152 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004153 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004154 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004155
4156 err_release:
4157 release_firmware(ucode_raw);
4158
4159 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08004160 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004161}
4162
Zhu Yib481de92007-09-25 17:54:57 -07004163/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004164 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07004165 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004166 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07004167 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004168static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004169{
Tomas Winkler57aab752008-04-14 21:16:03 -07004170 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004171
4172 IWL_DEBUG_INFO("Runtime Alive received.\n");
4173
4174 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4175 /* We had an error bringing up the hardware, so take it
4176 * all the way back down so we can try again */
4177 IWL_DEBUG_INFO("Alive failed.\n");
4178 goto restart;
4179 }
4180
4181 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4182 * This is a paranoid check, because we would not have gotten the
4183 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07004184 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004185 /* Runtime instruction load was bad;
4186 * take it all the way back down so we can try again */
4187 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4188 goto restart;
4189 }
4190
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004191 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004192
Tomas Winkler57aab752008-04-14 21:16:03 -07004193 ret = priv->cfg->ops->lib->alive_notify(priv);
4194 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07004195 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07004196 ret);
Zhu Yib481de92007-09-25 17:54:57 -07004197 goto restart;
4198 }
4199
Ben Cahill9fbab512007-11-29 11:09:47 +08004200 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07004201 set_bit(STATUS_ALIVE, &priv->status);
4202
4203 /* Clear out the uCode error bit if it is set */
4204 clear_bit(STATUS_FW_ERROR, &priv->status);
4205
Tomas Winklerfee12472008-04-03 16:05:21 -07004206 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004207 return;
4208
Zhu Yi5a66926a2008-01-14 17:46:18 -08004209 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004210
4211 priv->active_rate = priv->rates_mask;
4212 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
4213
Tomas Winkler3109ece2008-03-28 16:33:35 -07004214 if (iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004215 struct iwl4965_rxon_cmd *active_rxon =
4216 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004217
4218 memcpy(&priv->staging_rxon, &priv->active_rxon,
4219 sizeof(priv->staging_rxon));
4220 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4221 } else {
4222 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004223 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004224 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
4225 }
4226
Ben Cahill9fbab512007-11-29 11:09:47 +08004227 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004228 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004229
4230 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004231 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004232
4233 /* At this point, the NIC is initialized and operational */
4234 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004235
4236 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004237
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07004238 iwl_leds_register(priv);
4239
Zhu Yib481de92007-09-25 17:54:57 -07004240 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07004241 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004242 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07004243
4244 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004245 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004246
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004247 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004248 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07004249 return;
4250
4251 restart:
4252 queue_work(priv->workqueue, &priv->restart);
4253}
4254
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004255static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07004256
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004257static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004258{
4259 unsigned long flags;
4260 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4261 struct ieee80211_conf *conf = NULL;
4262
4263 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4264
4265 conf = ieee80211_get_hw_conf(priv->hw);
4266
4267 if (!exit_pending)
4268 set_bit(STATUS_EXIT_PENDING, &priv->status);
4269
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07004270 iwl_leds_unregister(priv);
4271
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004272 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
4273
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004274 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004275
4276 /* Unblock any waiting calls */
4277 wake_up_interruptible_all(&priv->wait_command_queue);
4278
Zhu Yib481de92007-09-25 17:54:57 -07004279 /* Wipe out the EXIT_PENDING status bit if we are not actually
4280 * exiting the module */
4281 if (!exit_pending)
4282 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4283
4284 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004285 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07004286
4287 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004288 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004289 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004290 spin_unlock_irqrestore(&priv->lock, flags);
4291 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004292
4293 if (priv->mac80211_registered)
4294 ieee80211_stop_queues(priv->hw);
4295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004296 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07004297 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07004298 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004299 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4300 STATUS_RF_KILL_HW |
4301 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4302 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004303 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4304 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004305 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4306 STATUS_IN_SUSPEND;
4307 goto exit;
4308 }
4309
4310 /* ...otherwise clear out all the status bits but the RF Kill and
4311 * SUSPEND bits and continue taking the NIC down. */
4312 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4313 STATUS_RF_KILL_HW |
4314 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4315 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004316 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4317 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004318 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4319 STATUS_IN_SUSPEND |
4320 test_bit(STATUS_FW_ERROR, &priv->status) <<
4321 STATUS_FW_ERROR;
4322
4323 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004324 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08004325 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07004326 spin_unlock_irqrestore(&priv->lock, flags);
4327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004328 iwl4965_hw_txq_ctx_stop(priv);
4329 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004330
4331 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004332 if (!iwl_grab_nic_access(priv)) {
4333 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004334 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004335 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004336 }
4337 spin_unlock_irqrestore(&priv->lock, flags);
4338
4339 udelay(5);
4340
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004341 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004342 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004343 iwl4965_hw_nic_reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004344 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004345
4346 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004347 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07004348
4349 if (priv->ibss_beacon)
4350 dev_kfree_skb(priv->ibss_beacon);
4351 priv->ibss_beacon = NULL;
4352
4353 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004354 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004355}
4356
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004357static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004358{
4359 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004360 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004361 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08004362
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004363 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004364}
4365
4366#define MAX_HW_RESTARTS 5
4367
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004368static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004369{
Tomas Winkler57aab752008-04-14 21:16:03 -07004370 int i;
4371 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004372
4373 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4374 IWL_WARNING("Exit pending; will not bring the NIC up\n");
4375 return -EIO;
4376 }
4377
4378 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
4379 IWL_WARNING("Radio disabled by SW RF kill (module "
4380 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004381 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004382 return -ENODEV;
4383 }
4384
Reinette Chatree903fbd2008-01-30 22:05:15 -08004385 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
4386 IWL_ERROR("ucode not available for device bringup\n");
4387 return -EIO;
4388 }
4389
Zhu Yie655b9f2008-01-24 02:19:38 -08004390 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004391 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08004392 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4393 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4394 else {
4395 set_bit(STATUS_RF_KILL_HW, &priv->status);
4396 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004397 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004398 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
4399 return -ENODEV;
4400 }
Zhu Yib481de92007-09-25 17:54:57 -07004401 }
4402
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004403 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004404 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07004405
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004406 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
4407 if (ret) {
4408 IWL_ERROR("Unable to allocate shared memory\n");
4409 return ret;
4410 }
4411
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004412 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07004413 if (ret) {
4414 IWL_ERROR("Unable to init nic\n");
4415 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004416 }
4417
4418 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004419 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4420 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004421 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4422
4423 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004424 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004425 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004426
4427 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004428 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4429 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07004430
4431 /* Copy original ucode data image from disk into backup cache.
4432 * This will be used to initialize the on-board processor's
4433 * data SRAM for a clean start when the runtime program first loads. */
4434 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08004435 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07004436
Zhu Yie655b9f2008-01-24 02:19:38 -08004437 /* We return success when we resume from suspend and rf_kill is on. */
4438 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07004439 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004440
4441 for (i = 0; i < MAX_HW_RESTARTS; i++) {
4442
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004443 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004444
4445 /* load bootstrap state machine,
4446 * load bootstrap program into processor's memory,
4447 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07004448 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004449
Tomas Winkler57aab752008-04-14 21:16:03 -07004450 if (ret) {
4451 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07004452 continue;
4453 }
4454
4455 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004456 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004457
Zhu Yib481de92007-09-25 17:54:57 -07004458 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
4459
4460 return 0;
4461 }
4462
4463 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004464 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004465
4466 /* tried to restart and config the device for as long as our
4467 * patience could withstand */
4468 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
4469 return -EIO;
4470}
4471
4472
4473/*****************************************************************************
4474 *
4475 * Workqueue callbacks
4476 *
4477 *****************************************************************************/
4478
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004479static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004480{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004481 struct iwl_priv *priv =
4482 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004483
4484 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4485 return;
4486
4487 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08004488 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004489 mutex_unlock(&priv->mutex);
4490}
4491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004492static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004493{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004494 struct iwl_priv *priv =
4495 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004496
4497 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4498 return;
4499
4500 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004501 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004502 mutex_unlock(&priv->mutex);
4503}
4504
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004505static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004506{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004507 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07004508
4509 wake_up_interruptible(&priv->wait_command_queue);
4510
4511 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4512 return;
4513
4514 mutex_lock(&priv->mutex);
4515
Tomas Winklerfee12472008-04-03 16:05:21 -07004516 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08004517 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07004518 "HW and/or SW RF Kill no longer active, restarting "
4519 "device\n");
4520 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4521 queue_work(priv->workqueue, &priv->restart);
4522 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004523 /* make sure mac80211 stop sending Tx frame */
4524 if (priv->mac80211_registered)
4525 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004526
4527 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
4528 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
4529 "disabled by SW switch\n");
4530 else
4531 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
4532 "Kill switch must be turned off for "
4533 "wireless networking to work.\n");
4534 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004535 iwl_rfkill_set_hw_state(priv);
4536
Zhu Yib481de92007-09-25 17:54:57 -07004537 mutex_unlock(&priv->mutex);
4538}
4539
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004540static void iwl4965_bg_set_monitor(struct work_struct *work)
4541{
4542 struct iwl_priv *priv = container_of(work,
4543 struct iwl_priv, set_monitor);
4544
4545 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
4546
4547 mutex_lock(&priv->mutex);
4548
4549 if (!iwl_is_ready(priv))
4550 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
4551 else
4552 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
4553 IWL_ERROR("iwl4965_set_mode() failed\n");
4554
4555 mutex_unlock(&priv->mutex);
4556}
4557
Zhu Yib481de92007-09-25 17:54:57 -07004558#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
4559
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004560static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004561{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004562 struct iwl_priv *priv =
4563 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07004564
4565 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4566 return;
4567
4568 mutex_lock(&priv->mutex);
4569 if (test_bit(STATUS_SCANNING, &priv->status) ||
4570 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08004571 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
4572 "adapter (%dms)\n",
4573 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08004574
Zhu Yib481de92007-09-25 17:54:57 -07004575 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004576 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004577 }
4578 mutex_unlock(&priv->mutex);
4579}
4580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004581static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004582{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004583 struct iwl_priv *priv =
4584 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07004585 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07004586 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004587 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07004588 .meta.flags = CMD_SIZE_HUGE,
4589 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004590 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07004591 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004592 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01004593 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004594 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07004595 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004596
4597 conf = ieee80211_get_hw_conf(priv->hw);
4598
4599 mutex_lock(&priv->mutex);
4600
Tomas Winklerfee12472008-04-03 16:05:21 -07004601 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004602 IWL_WARNING("request scan called when driver not ready.\n");
4603 goto done;
4604 }
4605
4606 /* Make sure the scan wasn't cancelled before this queued work
4607 * was given the chance to run... */
4608 if (!test_bit(STATUS_SCANNING, &priv->status))
4609 goto done;
4610
4611 /* This should never be called or scheduled if there is currently
4612 * a scan active in the hardware. */
4613 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
4614 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
4615 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07004616 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07004617 goto done;
4618 }
4619
4620 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4621 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4622 goto done;
4623 }
4624
4625 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4626 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
4627 goto done;
4628 }
4629
Tomas Winklerfee12472008-04-03 16:05:21 -07004630 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004631 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4632 goto done;
4633 }
4634
4635 if (!test_bit(STATUS_READY, &priv->status)) {
4636 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
4637 goto done;
4638 }
4639
4640 if (!priv->scan_bands) {
4641 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
4642 goto done;
4643 }
4644
4645 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004646 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07004647 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
4648 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07004649 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004650 goto done;
4651 }
4652 }
4653 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004654 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07004655
4656 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
4657 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
4658
Tomas Winkler3109ece2008-03-28 16:33:35 -07004659 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004660 u16 interval = 0;
4661 u32 extra;
4662 u32 suspend_time = 100;
4663 u32 scan_suspend_time = 100;
4664 unsigned long flags;
4665
4666 IWL_DEBUG_INFO("Scanning while associated...\n");
4667
4668 spin_lock_irqsave(&priv->lock, flags);
4669 interval = priv->beacon_int;
4670 spin_unlock_irqrestore(&priv->lock, flags);
4671
4672 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08004673 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07004674 if (!interval)
4675 interval = suspend_time;
4676
4677 extra = (suspend_time / interval) << 22;
4678 scan_suspend_time = (extra |
4679 ((suspend_time % interval) * 1024));
4680 scan->suspend_time = cpu_to_le32(scan_suspend_time);
4681 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
4682 scan_suspend_time, interval);
4683 }
4684
4685 /* We should add the ability for user to lock to PASSIVE ONLY */
4686 if (priv->one_direct_scan) {
4687 IWL_DEBUG_SCAN
4688 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004689 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07004690 priv->direct_ssid_len));
4691 scan->direct_scan[0].id = WLAN_EID_SSID;
4692 scan->direct_scan[0].len = priv->direct_ssid_len;
4693 memcpy(scan->direct_scan[0].ssid,
4694 priv->direct_ssid, priv->direct_ssid_len);
4695 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004696 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07004697 IWL_DEBUG_SCAN
4698 ("Kicking off one direct scan for '%s' when not associated\n",
4699 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07004700 scan->direct_scan[0].id = WLAN_EID_SSID;
4701 scan->direct_scan[0].len = priv->essid_len;
4702 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
4703 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07004704 } else {
Bill Moss786b4552008-04-17 16:03:40 -07004705 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004706 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07004707 }
Zhu Yib481de92007-09-25 17:54:57 -07004708
Zhu Yib481de92007-09-25 17:54:57 -07004709 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07004710 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07004711 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4712
Zhu Yib481de92007-09-25 17:54:57 -07004713
4714 switch (priv->scan_bands) {
4715 case 2:
4716 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
4717 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004718 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004719 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
4720
4721 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004722 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004723 break;
4724
4725 case 1:
4726 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004727 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004728 RATE_MCS_ANT_B_MSK);
4729 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01004730 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004731 break;
4732
4733 default:
4734 IWL_WARNING("Invalid scan band count\n");
4735 goto done;
4736 }
4737
Tomas Winkler78330fd2008-02-06 02:37:18 +02004738 /* We don't build a direct scan probe request; the uCode will do
4739 * that based on the direct_mask added to each channel entry */
4740 cmd_len = iwl4965_fill_probe_req(priv, band,
4741 (struct ieee80211_mgmt *)scan->data,
4742 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
4743
4744 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07004745 /* select Rx chains */
4746
4747 /* Force use of chains B and C (0x6) for scan Rx.
4748 * Avoid A (0x1) because of its off-channel reception on A-band.
4749 * MIMO is not used here, but value is required to make uCode happy. */
4750 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
4751 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
4752 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
4753 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
4754
4755 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
4756 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
4757
Bill Moss786b4552008-04-17 16:03:40 -07004758 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07004759 scan->channel_count =
4760 iwl4965_get_channels_for_scan(
4761 priv, band, 1, /* active */
4762 direct_mask,
4763 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07004764 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07004765 scan->channel_count =
4766 iwl4965_get_channels_for_scan(
4767 priv, band, 0, /* passive */
4768 direct_mask,
4769 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07004770
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004771 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
4772 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07004773 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004774 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07004775 cmd.data = scan;
4776 scan->len = cpu_to_le16(cmd.len);
4777
4778 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07004779 ret = iwl_send_cmd_sync(priv, &cmd);
4780 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004781 goto done;
4782
4783 queue_delayed_work(priv->workqueue, &priv->scan_check,
4784 IWL_SCAN_CHECK_WATCHDOG);
4785
4786 mutex_unlock(&priv->mutex);
4787 return;
4788
4789 done:
Ian Schram01ebd062007-10-25 17:15:22 +08004790 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07004791 queue_work(priv->workqueue, &priv->scan_completed);
4792 mutex_unlock(&priv->mutex);
4793}
4794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004795static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004796{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004797 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07004798
4799 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4800 return;
4801
4802 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004803 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004804 mutex_unlock(&priv->mutex);
4805}
4806
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004807static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004808{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004809 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07004810
4811 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4812 return;
4813
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004814 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004815 queue_work(priv->workqueue, &priv->up);
4816}
4817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004818static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004819{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004820 struct iwl_priv *priv =
4821 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07004822
4823 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4824 return;
4825
4826 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08004827 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004828 mutex_unlock(&priv->mutex);
4829}
4830
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004831#define IWL_DELAY_NEXT_SCAN (HZ*2)
4832
Reinette Chatre508e32e2008-04-14 21:16:13 -07004833static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004834{
Zhu Yib481de92007-09-25 17:54:57 -07004835 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07004836 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07004837 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004838
4839 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4840 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
4841 return;
4842 }
4843
Joe Perches0795af52007-10-03 17:59:30 -07004844 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
4845 priv->assoc_id,
4846 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004847
4848
4849 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4850 return;
4851
Zhu Yib481de92007-09-25 17:54:57 -07004852
Reinette Chatre508e32e2008-04-14 21:16:13 -07004853 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08004854 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004856 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08004857
Zhu Yib481de92007-09-25 17:54:57 -07004858 conf = ieee80211_get_hw_conf(priv->hw);
4859
4860 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004861 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004862
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004863 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4864 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07004865 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07004866 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07004867 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004868 IWL_WARNING("REPLY_RXON_TIMING failed - "
4869 "Attempting to continue.\n");
4870
4871 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4872
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004873#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004874 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08004875 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004876#endif /* CONFIG_IWL4965_HT*/
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004877 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004878 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4879
4880 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
4881 priv->assoc_id, priv->beacon_int);
4882
4883 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4884 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4885 else
4886 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4887
4888 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4889 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
4890 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
4891 else
4892 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4893
4894 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4895 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4896
4897 }
4898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004899 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004900
4901 switch (priv->iw_mode) {
4902 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004903 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07004904 break;
4905
4906 case IEEE80211_IF_TYPE_IBSS:
4907
4908 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004909 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004910
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004911 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
4912 iwl4965_rxon_add_station(priv, priv->bssid, 0);
4913 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
4914 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004915
4916 break;
4917
4918 default:
4919 IWL_ERROR("%s Should not be called in %d mode\n",
4920 __FUNCTION__, priv->iw_mode);
4921 break;
4922 }
4923
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004924 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004925
Zhu Yib481de92007-09-25 17:54:57 -07004926 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004927 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004928 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004929
4930 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4931 priv->assoc_station_added = 1;
4932
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004933 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08004934
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004935 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004936 /* we have just associated, don't start scan too early */
4937 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004938}
4939
4940
4941static void iwl4965_bg_post_associate(struct work_struct *data)
4942{
4943 struct iwl_priv *priv = container_of(data, struct iwl_priv,
4944 post_associate.work);
4945
4946 mutex_lock(&priv->mutex);
4947 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004948 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07004949
Zhu Yib481de92007-09-25 17:54:57 -07004950}
4951
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004952static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004953{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004954 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07004955
Tomas Winklerfee12472008-04-03 16:05:21 -07004956 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004957 return;
4958
4959 mutex_lock(&priv->mutex);
4960
4961 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004962 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004963
4964 mutex_unlock(&priv->mutex);
4965}
4966
Zhu Yi76bb77e2007-11-22 10:53:22 +08004967static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
4968
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004969static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004970{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004971 struct iwl_priv *priv =
4972 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07004973
Ester Kummerf3d67992008-05-06 11:05:12 +08004974 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07004975
4976 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4977 return;
4978
Zhu Yia0646472007-12-20 14:10:01 +08004979 if (test_bit(STATUS_CONF_PENDING, &priv->status))
4980 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08004981
Zhu Yib481de92007-09-25 17:54:57 -07004982 ieee80211_scan_completed(priv->hw);
4983
4984 /* Since setting the TXPOWER may have been deferred while
4985 * performing the scan, fire one off */
4986 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004987 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004988 mutex_unlock(&priv->mutex);
4989}
4990
4991/*****************************************************************************
4992 *
4993 * mac80211 entry point functions
4994 *
4995 *****************************************************************************/
4996
Zhu Yi5a66926a2008-01-14 17:46:18 -08004997#define UCODE_READY_TIMEOUT (2 * HZ)
4998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004999static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005000{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005001 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005002 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005003
5004 IWL_DEBUG_MAC80211("enter\n");
5005
Zhu Yi5a66926a2008-01-14 17:46:18 -08005006 if (pci_enable_device(priv->pci_dev)) {
5007 IWL_ERROR("Fail to pci_enable_device\n");
5008 return -ENODEV;
5009 }
5010 pci_restore_state(priv->pci_dev);
5011 pci_enable_msi(priv->pci_dev);
5012
5013 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
5014 DRV_NAME, priv);
5015 if (ret) {
5016 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
5017 goto out_disable_msi;
5018 }
5019
Zhu Yib481de92007-09-25 17:54:57 -07005020 /* we should be verifying the device is ready to be opened */
5021 mutex_lock(&priv->mutex);
5022
Zhu Yi5a66926a2008-01-14 17:46:18 -08005023 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
5024 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5025 * ucode filename and max sizes are card-specific. */
5026
5027 if (!priv->ucode_code.len) {
5028 ret = iwl4965_read_ucode(priv);
5029 if (ret) {
5030 IWL_ERROR("Could not read microcode: %d\n", ret);
5031 mutex_unlock(&priv->mutex);
5032 goto out_release_irq;
5033 }
5034 }
5035
Zhu Yie655b9f2008-01-24 02:19:38 -08005036 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005037
Zhu Yib481de92007-09-25 17:54:57 -07005038 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005039
Zhu Yie655b9f2008-01-24 02:19:38 -08005040 if (ret)
5041 goto out_release_irq;
5042
5043 IWL_DEBUG_INFO("Start UP work done.\n");
5044
5045 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5046 return 0;
5047
Zhu Yi5a66926a2008-01-14 17:46:18 -08005048 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5049 * mac80211 will not be run successfully. */
5050 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5051 test_bit(STATUS_READY, &priv->status),
5052 UCODE_READY_TIMEOUT);
5053 if (!ret) {
5054 if (!test_bit(STATUS_READY, &priv->status)) {
5055 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
5056 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5057 ret = -ETIMEDOUT;
5058 goto out_release_irq;
5059 }
5060 }
5061
Zhu Yie655b9f2008-01-24 02:19:38 -08005062 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005063 IWL_DEBUG_MAC80211("leave\n");
5064 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005065
5066out_release_irq:
5067 free_irq(priv->pci_dev->irq, priv);
5068out_disable_msi:
5069 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005070 pci_disable_device(priv->pci_dev);
5071 priv->is_open = 0;
5072 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08005073 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005074}
5075
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005076static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005077{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005078 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005079
5080 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08005081
Zhu Yie655b9f2008-01-24 02:19:38 -08005082 if (!priv->is_open) {
5083 IWL_DEBUG_MAC80211("leave - skip\n");
5084 return;
5085 }
5086
Zhu Yib481de92007-09-25 17:54:57 -07005087 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08005088
Tomas Winklerfee12472008-04-03 16:05:21 -07005089 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08005090 /* stop mac, cancel any scan request and clear
5091 * RXON_FILTER_ASSOC_MSK BIT
5092 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08005093 mutex_lock(&priv->mutex);
5094 iwl4965_scan_cancel_timeout(priv, 100);
5095 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005096 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08005097 }
5098
Zhu Yi5a66926a2008-01-14 17:46:18 -08005099 iwl4965_down(priv);
5100
5101 flush_workqueue(priv->workqueue);
5102 free_irq(priv->pci_dev->irq, priv);
5103 pci_disable_msi(priv->pci_dev);
5104 pci_save_state(priv->pci_dev);
5105 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005106
Zhu Yib481de92007-09-25 17:54:57 -07005107 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005108}
5109
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005110static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005111 struct ieee80211_tx_control *ctl)
5112{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005113 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005114
5115 IWL_DEBUG_MAC80211("enter\n");
5116
5117 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
5118 IWL_DEBUG_MAC80211("leave - monitor\n");
5119 return -1;
5120 }
5121
5122 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01005123 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07005124
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005125 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07005126 dev_kfree_skb_any(skb);
5127
5128 IWL_DEBUG_MAC80211("leave\n");
5129 return 0;
5130}
5131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005132static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005133 struct ieee80211_if_init_conf *conf)
5134{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005135 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005136 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07005137 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005138
Johannes Berg32bfd352007-12-19 01:31:26 +01005139 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07005140
Johannes Berg32bfd352007-12-19 01:31:26 +01005141 if (priv->vif) {
5142 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08005143 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07005144 }
5145
5146 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01005147 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07005148
5149 spin_unlock_irqrestore(&priv->lock, flags);
5150
5151 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02005152
5153 if (conf->mac_addr) {
5154 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
5155 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
5156 }
Zhu Yib481de92007-09-25 17:54:57 -07005157
Tomas Winklerfee12472008-04-03 16:05:21 -07005158 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005159 iwl4965_set_mode(priv, conf->type);
5160
Zhu Yib481de92007-09-25 17:54:57 -07005161 mutex_unlock(&priv->mutex);
5162
Zhu Yi5a66926a2008-01-14 17:46:18 -08005163 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005164 return 0;
5165}
5166
5167/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005168 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07005169 *
5170 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5171 * be set inappropriately and the driver currently sets the hardware up to
5172 * use it whenever needed.
5173 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005174static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07005175{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005176 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005177 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005178 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08005179 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005180
5181 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01005182 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005183
Zhu Yi12342c42007-12-20 11:27:32 +08005184 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
5185
Tomas Winklerfee12472008-04-03 16:05:21 -07005186 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005187 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005188 ret = -EIO;
5189 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005190 }
5191
Assaf Krauss1ea87392008-03-18 14:57:50 -07005192 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07005193 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08005194 IWL_DEBUG_MAC80211("leave - scanning\n");
5195 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005196 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08005197 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005198 }
5199
5200 spin_lock_irqsave(&priv->lock, flags);
5201
Assaf Krauss8622e702008-03-21 13:53:43 -07005202 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005203 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005204 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07005205 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5206 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005207 ret = -EINVAL;
5208 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005209 }
5210
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005211#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02005212 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07005213 * from any ht related info since 2.4 does not
5214 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02005215 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07005216#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5217 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
5218#endif
5219 )
5220 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005221#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005222
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005223 iwl_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005224 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005225
Johannes Berg8318d782008-01-24 19:38:38 +01005226 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07005227
5228 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01005229 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07005230 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005231 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005232
5233 spin_unlock_irqrestore(&priv->lock, flags);
5234
5235#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5236 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005237 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005238 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005239 }
5240#endif
5241
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005242 if (priv->cfg->ops->lib->radio_kill_sw)
5243 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07005244
5245 if (!conf->radio_enabled) {
5246 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005247 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005248 }
5249
Tomas Winklerfee12472008-04-03 16:05:21 -07005250 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005251 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005252 ret = -EIO;
5253 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005254 }
5255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005256 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005257
5258 if (memcmp(&priv->active_rxon,
5259 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005260 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005261 else
5262 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
5263
5264 IWL_DEBUG_MAC80211("leave\n");
5265
Zhu Yia0646472007-12-20 14:10:01 +08005266out:
5267 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005268 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005269 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005270}
5271
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005272static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005273{
Tomas Winkler857485c2008-03-21 13:53:44 -07005274 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005275
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08005276 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005277 return;
5278
5279 /* The following should be done only at AP bring up */
5280 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
5281
5282 /* RXON - unassoc (to set timing command) */
5283 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005284 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005285
5286 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005287 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5288 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005289 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005290 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005291 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005292 IWL_WARNING("REPLY_RXON_TIMING failed - "
5293 "Attempting to continue.\n");
5294
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005295 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005296
5297 /* FIXME: what should be the assoc_id for AP? */
5298 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5299 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5300 priv->staging_rxon.flags |=
5301 RXON_FLG_SHORT_PREAMBLE_MSK;
5302 else
5303 priv->staging_rxon.flags &=
5304 ~RXON_FLG_SHORT_PREAMBLE_MSK;
5305
5306 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5307 if (priv->assoc_capability &
5308 WLAN_CAPABILITY_SHORT_SLOT_TIME)
5309 priv->staging_rxon.flags |=
5310 RXON_FLG_SHORT_SLOT_MSK;
5311 else
5312 priv->staging_rxon.flags &=
5313 ~RXON_FLG_SHORT_SLOT_MSK;
5314
5315 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5316 priv->staging_rxon.flags &=
5317 ~RXON_FLG_SHORT_SLOT_MSK;
5318 }
5319 /* restore RXON assoc */
5320 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005321 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005322 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005323 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08005324 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005325 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005326
5327 /* FIXME - we need to add code here to detect a totally new
5328 * configuration, reset the AP, unassoc, rxon timing, assoc,
5329 * clear sta table, add BCAST sta... */
5330}
5331
Johannes Berg32bfd352007-12-19 01:31:26 +01005332static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
5333 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07005334 struct ieee80211_if_conf *conf)
5335{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005336 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005337 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005338 unsigned long flags;
5339 int rc;
5340
5341 if (conf == NULL)
5342 return -EIO;
5343
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005344 if (priv->vif != vif) {
5345 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005346 return 0;
5347 }
5348
Zhu Yib481de92007-09-25 17:54:57 -07005349 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
5350 (!conf->beacon || !conf->ssid_len)) {
5351 IWL_DEBUG_MAC80211
5352 ("Leaving in AP mode because HostAPD is not ready.\n");
5353 return 0;
5354 }
5355
Tomas Winklerfee12472008-04-03 16:05:21 -07005356 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005357 return -EAGAIN;
5358
Zhu Yib481de92007-09-25 17:54:57 -07005359 mutex_lock(&priv->mutex);
5360
Zhu Yib481de92007-09-25 17:54:57 -07005361 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07005362 IWL_DEBUG_MAC80211("bssid: %s\n",
5363 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005364
Johannes Berg4150c572007-09-17 01:29:23 -04005365/*
5366 * very dubious code was here; the probe filtering flag is never set:
5367 *
Zhu Yib481de92007-09-25 17:54:57 -07005368 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
5369 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04005370 */
Zhu Yib481de92007-09-25 17:54:57 -07005371
5372 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5373 if (!conf->bssid) {
5374 conf->bssid = priv->mac_addr;
5375 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07005376 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
5377 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005378 }
5379 if (priv->ibss_beacon)
5380 dev_kfree_skb(priv->ibss_beacon);
5381
5382 priv->ibss_beacon = conf->beacon;
5383 }
5384
Tomas Winklerfee12472008-04-03 16:05:21 -07005385 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08005386 goto done;
5387
Zhu Yib481de92007-09-25 17:54:57 -07005388 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
5389 !is_multicast_ether_addr(conf->bssid)) {
5390 /* If there is currently a HW scan going on in the background
5391 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005392 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07005393 IWL_WARNING("Aborted scan still in progress "
5394 "after 100ms\n");
5395 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5396 mutex_unlock(&priv->mutex);
5397 return -EAGAIN;
5398 }
5399 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
5400
5401 /* TODO: Audit driver for usage of these members and see
5402 * if mac80211 deprecates them (priv->bssid looks like it
5403 * shouldn't be there, but I haven't scanned the IBSS code
5404 * to verify) - jpk */
5405 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5406
5407 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005408 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005409 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005410 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005411 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005412 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07005413 priv, priv->active_rxon.bssid_addr, 1);
5414 }
5415
5416 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005417 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07005418 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005419 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005420 }
5421
Mohamed Abbasfde35712007-11-29 11:10:15 +08005422 done:
Zhu Yib481de92007-09-25 17:54:57 -07005423 spin_lock_irqsave(&priv->lock, flags);
5424 if (!conf->ssid_len)
5425 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5426 else
5427 memcpy(priv->essid, conf->ssid, conf->ssid_len);
5428
5429 priv->essid_len = conf->ssid_len;
5430 spin_unlock_irqrestore(&priv->lock, flags);
5431
5432 IWL_DEBUG_MAC80211("leave\n");
5433 mutex_unlock(&priv->mutex);
5434
5435 return 0;
5436}
5437
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005438static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04005439 unsigned int changed_flags,
5440 unsigned int *total_flags,
5441 int mc_count, struct dev_addr_list *mc_list)
5442{
5443 /*
5444 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005445 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04005446 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08005447 struct iwl_priv *priv = hw->priv;
5448 int new_flags = 0;
5449 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5450 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5451 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
5452 IEEE80211_IF_TYPE_MNTR,
5453 changed_flags, *total_flags);
5454 /* queue work 'cuz mac80211 is holding a lock which
5455 * prevents us from issuing (synchronous) f/w cmds */
5456 queue_work(priv->workqueue, &priv->set_monitor);
5457 new_flags &= FIF_PROMISC_IN_BSS |
5458 FIF_OTHER_BSS |
5459 FIF_ALLMULTI;
5460 }
5461 }
5462 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04005463}
5464
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005465static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005466 struct ieee80211_if_init_conf *conf)
5467{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005468 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005469
5470 IWL_DEBUG_MAC80211("enter\n");
5471
5472 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005473
Tomas Winklerfee12472008-04-03 16:05:21 -07005474 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005475 iwl4965_scan_cancel_timeout(priv, 100);
5476 cancel_delayed_work(&priv->post_associate);
5477 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5478 iwl4965_commit_rxon(priv);
5479 }
Johannes Berg32bfd352007-12-19 01:31:26 +01005480 if (priv->vif == conf->vif) {
5481 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005482 memset(priv->bssid, 0, ETH_ALEN);
5483 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5484 priv->essid_len = 0;
5485 }
5486 mutex_unlock(&priv->mutex);
5487
5488 IWL_DEBUG_MAC80211("leave\n");
5489
5490}
Johannes Berg471b3ef2007-12-28 14:32:58 +01005491
Tomas Winkler3109ece2008-03-28 16:33:35 -07005492#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01005493static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
5494 struct ieee80211_vif *vif,
5495 struct ieee80211_bss_conf *bss_conf,
5496 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02005497{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005498 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02005499
Tomas Winkler3109ece2008-03-28 16:33:35 -07005500 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
5501
Johannes Berg471b3ef2007-12-28 14:32:58 +01005502 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005503 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
5504 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005505 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02005506 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5507 else
5508 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5509 }
5510
Johannes Berg471b3ef2007-12-28 14:32:58 +01005511 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005512 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01005513 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02005514 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
5515 else
5516 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
5517 }
5518
Tomas Winkler98952d52008-03-28 16:33:33 -07005519 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005520 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07005521 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005522 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07005523 }
5524
Johannes Berg471b3ef2007-12-28 14:32:58 +01005525 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005526 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005527 /* This should never happen as this function should
5528 * never be called from interrupt context. */
5529 if (WARN_ON_ONCE(in_interrupt()))
5530 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005531 if (bss_conf->assoc) {
5532 priv->assoc_id = bss_conf->aid;
5533 priv->beacon_int = bss_conf->beacon_int;
5534 priv->timestamp = bss_conf->timestamp;
5535 priv->assoc_capability = bss_conf->assoc_capability;
5536 priv->next_scan_jiffies = jiffies +
5537 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005538 mutex_lock(&priv->mutex);
5539 iwl4965_post_associate(priv);
5540 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005541 } else {
5542 priv->assoc_id = 0;
5543 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
5544 }
5545 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
5546 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07005547 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005548 }
5549
Tomas Winkler220173b2007-10-16 00:50:25 +02005550}
Zhu Yib481de92007-09-25 17:54:57 -07005551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005552static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07005553{
5554 int rc = 0;
5555 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005556 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005557
5558 IWL_DEBUG_MAC80211("enter\n");
5559
mabbas052c4b92007-10-25 17:15:43 +08005560 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005561 spin_lock_irqsave(&priv->lock, flags);
5562
Tomas Winklerfee12472008-04-03 16:05:21 -07005563 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005564 rc = -EIO;
5565 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
5566 goto out_unlock;
5567 }
5568
5569 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
5570 rc = -EIO;
5571 IWL_ERROR("ERROR: APs don't scan\n");
5572 goto out_unlock;
5573 }
5574
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005575 /* we don't schedule scan within next_scan_jiffies period */
5576 if (priv->next_scan_jiffies &&
5577 time_after(priv->next_scan_jiffies, jiffies)) {
5578 rc = -EAGAIN;
5579 goto out_unlock;
5580 }
Zhu Yib481de92007-09-25 17:54:57 -07005581 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005582 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
5583 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07005584 rc = -EAGAIN;
5585 goto out_unlock;
5586 }
5587 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005588 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005589 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07005590
5591 priv->one_direct_scan = 1;
5592 priv->direct_ssid_len = (u8)
5593 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
5594 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005595 } else
5596 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005597
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005598 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005599
5600 IWL_DEBUG_MAC80211("leave\n");
5601
5602out_unlock:
5603 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08005604 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005605
5606 return rc;
5607}
5608
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005609static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
5610 struct ieee80211_key_conf *keyconf, const u8 *addr,
5611 u32 iv32, u16 *phase1key)
5612{
5613 struct iwl_priv *priv = hw->priv;
5614 u8 sta_id = IWL_INVALID_STATION;
5615 unsigned long flags;
5616 __le16 key_flags = 0;
5617 int i;
5618 DECLARE_MAC_BUF(mac);
5619
5620 IWL_DEBUG_MAC80211("enter\n");
5621
Tomas Winkler947b13a2008-04-16 16:34:48 -07005622 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005623 if (sta_id == IWL_INVALID_STATION) {
5624 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5625 print_mac(mac, addr));
5626 return;
5627 }
5628
5629 iwl4965_scan_cancel_timeout(priv, 100);
5630
5631 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
5632 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
5633 key_flags &= ~STA_KEY_FLG_INVALID;
5634
Tomas Winkler5425e492008-04-15 16:01:38 -07005635 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005636 key_flags |= STA_KEY_MULTICAST_MSK;
5637
5638 spin_lock_irqsave(&priv->sta_lock, flags);
5639
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005640 priv->stations[sta_id].sta.key.key_flags = key_flags;
5641 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
5642
5643 for (i = 0; i < 5; i++)
5644 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
5645 cpu_to_le16(phase1key[i]);
5646
5647 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
5648 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
5649
Tomas Winkler133636d2008-05-05 10:22:34 +08005650 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005651
5652 spin_unlock_irqrestore(&priv->sta_lock, flags);
5653
5654 IWL_DEBUG_MAC80211("leave\n");
5655}
5656
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005657static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07005658 const u8 *local_addr, const u8 *addr,
5659 struct ieee80211_key_conf *key)
5660{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005661 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005662 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005663 int ret = 0;
5664 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005665 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005666
5667 IWL_DEBUG_MAC80211("enter\n");
5668
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07005669 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07005670 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
5671 return -EOPNOTSUPP;
5672 }
5673
5674 if (is_zero_ether_addr(addr))
5675 /* only support pairwise keys */
5676 return -EOPNOTSUPP;
5677
Tomas Winkler947b13a2008-04-16 16:34:48 -07005678 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005679 if (sta_id == IWL_INVALID_STATION) {
5680 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5681 print_mac(mac, addr));
5682 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005683
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005684 }
Zhu Yib481de92007-09-25 17:54:57 -07005685
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005686 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005687 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005688 mutex_unlock(&priv->mutex);
5689
5690 /* If we are getting WEP group key and we didn't receive any key mapping
5691 * so far, we are in legacy wep mode (group key only), otherwise we are
5692 * in 1X mode.
5693 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07005694 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005695 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
5696 if (cmd == SET_KEY)
5697 is_default_wep_key = !priv->key_mapping_key;
5698 else
5699 is_default_wep_key = priv->default_wep_key;
5700 }
mabbas052c4b92007-10-25 17:15:43 +08005701
Zhu Yib481de92007-09-25 17:54:57 -07005702 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005703 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005704 if (is_default_wep_key)
5705 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005706 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07005707 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005708
5709 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005710 break;
5711 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005712 if (is_default_wep_key)
5713 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005714 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07005715 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005716
5717 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005718 break;
5719 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005720 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005721 }
5722
5723 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005724
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005725 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005726}
5727
Johannes Berge100bb62008-04-30 18:51:21 +02005728static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07005729 const struct ieee80211_tx_queue_params *params)
5730{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005731 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005732 unsigned long flags;
5733 int q;
Zhu Yib481de92007-09-25 17:54:57 -07005734
5735 IWL_DEBUG_MAC80211("enter\n");
5736
Tomas Winklerfee12472008-04-03 16:05:21 -07005737 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005738 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5739 return -EIO;
5740 }
5741
5742 if (queue >= AC_NUM) {
5743 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5744 return 0;
5745 }
5746
Zhu Yib481de92007-09-25 17:54:57 -07005747 if (!priv->qos_data.qos_enable) {
5748 priv->qos_data.qos_active = 0;
5749 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
5750 return 0;
5751 }
5752 q = AC_NUM - 1 - queue;
5753
5754 spin_lock_irqsave(&priv->lock, flags);
5755
5756 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5757 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5758 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5759 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01005760 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07005761
5762 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
5763 priv->qos_data.qos_active = 1;
5764
5765 spin_unlock_irqrestore(&priv->lock, flags);
5766
5767 mutex_lock(&priv->mutex);
5768 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005769 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005770 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005771 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005772
5773 mutex_unlock(&priv->mutex);
5774
Zhu Yib481de92007-09-25 17:54:57 -07005775 IWL_DEBUG_MAC80211("leave\n");
5776 return 0;
5777}
5778
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005779static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005780 struct ieee80211_tx_queue_stats *stats)
5781{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005782 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005783 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08005784 struct iwl_tx_queue *txq;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005785 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07005786 unsigned long flags;
5787
5788 IWL_DEBUG_MAC80211("enter\n");
5789
Tomas Winklerfee12472008-04-03 16:05:21 -07005790 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005791 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5792 return -EIO;
5793 }
5794
5795 spin_lock_irqsave(&priv->lock, flags);
5796
5797 for (i = 0; i < AC_NUM; i++) {
5798 txq = &priv->txq[i];
5799 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005800 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07005801
Johannes Berg57ffc582008-04-29 17:18:59 +02005802 stats[i].len = q->n_window - avail;
5803 stats[i].limit = q->n_window - q->high_mark;
5804 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07005805
5806 }
5807 spin_unlock_irqrestore(&priv->lock, flags);
5808
5809 IWL_DEBUG_MAC80211("leave\n");
5810
5811 return 0;
5812}
5813
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005814static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005815 struct ieee80211_low_level_stats *stats)
5816{
Ester Kummerbf403db2008-05-05 10:22:40 +08005817 struct iwl_priv *priv = hw->priv;
5818
5819 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005820 IWL_DEBUG_MAC80211("enter\n");
5821 IWL_DEBUG_MAC80211("leave\n");
5822
5823 return 0;
5824}
5825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005826static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005827{
Ester Kummerbf403db2008-05-05 10:22:40 +08005828 struct iwl_priv *priv;
5829
5830 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005831 IWL_DEBUG_MAC80211("enter\n");
5832 IWL_DEBUG_MAC80211("leave\n");
5833
5834 return 0;
5835}
5836
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005837static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005838{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005839 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005840 unsigned long flags;
5841
5842 mutex_lock(&priv->mutex);
5843 IWL_DEBUG_MAC80211("enter\n");
5844
5845 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005846#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07005847 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005848 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07005849 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005850#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005851
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005852 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005853
5854 cancel_delayed_work(&priv->post_associate);
5855
5856 spin_lock_irqsave(&priv->lock, flags);
5857 priv->assoc_id = 0;
5858 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005859 priv->assoc_station_added = 0;
5860
5861 /* new association get rid of ibss beacon skb */
5862 if (priv->ibss_beacon)
5863 dev_kfree_skb(priv->ibss_beacon);
5864
5865 priv->ibss_beacon = NULL;
5866
5867 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005868 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005869 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
5870 priv->beacon_int = 0;
5871
5872 spin_unlock_irqrestore(&priv->lock, flags);
5873
Tomas Winklerfee12472008-04-03 16:05:21 -07005874 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005875 IWL_DEBUG_MAC80211("leave - not ready\n");
5876 mutex_unlock(&priv->mutex);
5877 return;
5878 }
5879
mabbas052c4b92007-10-25 17:15:43 +08005880 /* we are restarting association process
5881 * clear RXON_FILTER_ASSOC_MSK bit
5882 */
5883 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005884 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08005885 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005886 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08005887 }
5888
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005889 iwl_power_update_mode(priv, 0);
5890
Zhu Yib481de92007-09-25 17:54:57 -07005891 /* Per mac80211.h: This is only used in IBSS mode... */
5892 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08005893
Zhu Yib481de92007-09-25 17:54:57 -07005894 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
5895 mutex_unlock(&priv->mutex);
5896 return;
5897 }
5898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005899 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005900
5901 mutex_unlock(&priv->mutex);
5902
5903 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005904}
5905
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005906static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005907 struct ieee80211_tx_control *control)
5908{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005909 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005910 unsigned long flags;
5911
5912 mutex_lock(&priv->mutex);
5913 IWL_DEBUG_MAC80211("enter\n");
5914
Tomas Winklerfee12472008-04-03 16:05:21 -07005915 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005916 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5917 mutex_unlock(&priv->mutex);
5918 return -EIO;
5919 }
5920
5921 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
5922 IWL_DEBUG_MAC80211("leave - not IBSS\n");
5923 mutex_unlock(&priv->mutex);
5924 return -EIO;
5925 }
5926
5927 spin_lock_irqsave(&priv->lock, flags);
5928
5929 if (priv->ibss_beacon)
5930 dev_kfree_skb(priv->ibss_beacon);
5931
5932 priv->ibss_beacon = skb;
5933
5934 priv->assoc_id = 0;
5935
5936 IWL_DEBUG_MAC80211("leave\n");
5937 spin_unlock_irqrestore(&priv->lock, flags);
5938
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005939 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005940
5941 queue_work(priv->workqueue, &priv->post_associate.work);
5942
5943 mutex_unlock(&priv->mutex);
5944
5945 return 0;
5946}
5947
Zhu Yib481de92007-09-25 17:54:57 -07005948/*****************************************************************************
5949 *
5950 * sysfs attributes
5951 *
5952 *****************************************************************************/
5953
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005954#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07005955
5956/*
5957 * The following adds a new attribute to the sysfs representation
5958 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
5959 * used for controlling the debug level.
5960 *
5961 * See the level definitions in iwl for details.
5962 */
5963
Ester Kummer8cf769c2008-05-06 11:05:11 +08005964static ssize_t show_debug_level(struct device *d,
5965 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07005966{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005967 struct iwl_priv *priv = d->driver_data;
5968
5969 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07005970}
Ester Kummer8cf769c2008-05-06 11:05:11 +08005971static ssize_t store_debug_level(struct device *d,
5972 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07005973 const char *buf, size_t count)
5974{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005975 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005976 char *p = (char *)buf;
5977 u32 val;
5978
5979 val = simple_strtoul(p, &p, 0);
5980 if (p == buf)
5981 printk(KERN_INFO DRV_NAME
5982 ": %s is not in hex or decimal form.\n", buf);
5983 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08005984 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07005985
5986 return strnlen(buf, count);
5987}
5988
Ester Kummer8cf769c2008-05-06 11:05:11 +08005989static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5990 show_debug_level, store_debug_level);
5991
Zhu Yib481de92007-09-25 17:54:57 -07005992
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005993#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07005994
Zhu Yib481de92007-09-25 17:54:57 -07005995
5996static ssize_t show_temperature(struct device *d,
5997 struct device_attribute *attr, char *buf)
5998{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005999 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006000
Tomas Winklerfee12472008-04-03 16:05:21 -07006001 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006002 return -EAGAIN;
6003
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006004 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006005}
6006
6007static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6008
6009static ssize_t show_rs_window(struct device *d,
6010 struct device_attribute *attr,
6011 char *buf)
6012{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006013 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006014 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006015}
6016static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
6017
6018static ssize_t show_tx_power(struct device *d,
6019 struct device_attribute *attr, char *buf)
6020{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006021 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006022 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6023}
6024
6025static ssize_t store_tx_power(struct device *d,
6026 struct device_attribute *attr,
6027 const char *buf, size_t count)
6028{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006029 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006030 char *p = (char *)buf;
6031 u32 val;
6032
6033 val = simple_strtoul(p, &p, 10);
6034 if (p == buf)
6035 printk(KERN_INFO DRV_NAME
6036 ": %s is not in decimal form.\n", buf);
6037 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006038 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006039
6040 return count;
6041}
6042
6043static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6044
6045static ssize_t show_flags(struct device *d,
6046 struct device_attribute *attr, char *buf)
6047{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006048 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006049
6050 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
6051}
6052
6053static ssize_t store_flags(struct device *d,
6054 struct device_attribute *attr,
6055 const char *buf, size_t count)
6056{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006057 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006058 u32 flags = simple_strtoul(buf, NULL, 0);
6059
6060 mutex_lock(&priv->mutex);
6061 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
6062 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006063 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006064 IWL_WARNING("Could not cancel scan.\n");
6065 else {
6066 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6067 flags);
6068 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006069 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006070 }
6071 }
6072 mutex_unlock(&priv->mutex);
6073
6074 return count;
6075}
6076
6077static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6078
6079static ssize_t show_filter_flags(struct device *d,
6080 struct device_attribute *attr, char *buf)
6081{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006082 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006083
6084 return sprintf(buf, "0x%04X\n",
6085 le32_to_cpu(priv->active_rxon.filter_flags));
6086}
6087
6088static ssize_t store_filter_flags(struct device *d,
6089 struct device_attribute *attr,
6090 const char *buf, size_t count)
6091{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006092 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006093 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6094
6095 mutex_lock(&priv->mutex);
6096 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
6097 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006098 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07006099 IWL_WARNING("Could not cancel scan.\n");
6100 else {
6101 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6102 "0x%04X\n", filter_flags);
6103 priv->staging_rxon.filter_flags =
6104 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006105 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006106 }
6107 }
6108 mutex_unlock(&priv->mutex);
6109
6110 return count;
6111}
6112
6113static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6114 store_filter_flags);
6115
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006116#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006117
6118static ssize_t show_measurement(struct device *d,
6119 struct device_attribute *attr, char *buf)
6120{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006121 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006122 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006123 u32 size = sizeof(measure_report), len = 0, ofs = 0;
6124 u8 *data = (u8 *) & measure_report;
6125 unsigned long flags;
6126
6127 spin_lock_irqsave(&priv->lock, flags);
6128 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6129 spin_unlock_irqrestore(&priv->lock, flags);
6130 return 0;
6131 }
6132 memcpy(&measure_report, &priv->measure_report, size);
6133 priv->measurement_status = 0;
6134 spin_unlock_irqrestore(&priv->lock, flags);
6135
6136 while (size && (PAGE_SIZE - len)) {
6137 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6138 PAGE_SIZE - len, 1);
6139 len = strlen(buf);
6140 if (PAGE_SIZE - len)
6141 buf[len++] = '\n';
6142
6143 ofs += 16;
6144 size -= min(size, 16U);
6145 }
6146
6147 return len;
6148}
6149
6150static ssize_t store_measurement(struct device *d,
6151 struct device_attribute *attr,
6152 const char *buf, size_t count)
6153{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006154 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006155 struct ieee80211_measurement_params params = {
6156 .channel = le16_to_cpu(priv->active_rxon.channel),
6157 .start_time = cpu_to_le64(priv->last_tsf),
6158 .duration = cpu_to_le16(1),
6159 };
6160 u8 type = IWL_MEASURE_BASIC;
6161 u8 buffer[32];
6162 u8 channel;
6163
6164 if (count) {
6165 char *p = buffer;
6166 strncpy(buffer, buf, min(sizeof(buffer), count));
6167 channel = simple_strtoul(p, NULL, 0);
6168 if (channel)
6169 params.channel = channel;
6170
6171 p = buffer;
6172 while (*p && *p != ' ')
6173 p++;
6174 if (*p)
6175 type = simple_strtoul(p + 1, NULL, 0);
6176 }
6177
6178 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6179 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006180 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006181
6182 return count;
6183}
6184
6185static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6186 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006187#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006188
6189static ssize_t store_retry_rate(struct device *d,
6190 struct device_attribute *attr,
6191 const char *buf, size_t count)
6192{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006193 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006194
6195 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6196 if (priv->retry_rate <= 0)
6197 priv->retry_rate = 1;
6198
6199 return count;
6200}
6201
6202static ssize_t show_retry_rate(struct device *d,
6203 struct device_attribute *attr, char *buf)
6204{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006205 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006206 return sprintf(buf, "%d", priv->retry_rate);
6207}
6208
6209static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6210 store_retry_rate);
6211
6212static ssize_t store_power_level(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 int rc;
6218 int mode;
6219
6220 mode = simple_strtoul(buf, NULL, 0);
6221 mutex_lock(&priv->mutex);
6222
Tomas Winklerfee12472008-04-03 16:05:21 -07006223 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006224 rc = -EAGAIN;
6225 goto out;
6226 }
6227
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006228 rc = iwl_power_set_user_mode(priv, mode);
6229 if (rc) {
6230 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6231 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006232 }
Zhu Yib481de92007-09-25 17:54:57 -07006233 rc = count;
6234
6235 out:
6236 mutex_unlock(&priv->mutex);
6237 return rc;
6238}
6239
6240#define MAX_WX_STRING 80
6241
6242/* Values are in microsecond */
6243static const s32 timeout_duration[] = {
6244 350000,
6245 250000,
6246 75000,
6247 37000,
6248 25000,
6249};
6250static const s32 period_duration[] = {
6251 400000,
6252 700000,
6253 1000000,
6254 1000000,
6255 1000000
6256};
6257
6258static ssize_t show_power_level(struct device *d,
6259 struct device_attribute *attr, char *buf)
6260{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006261 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006262 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07006263 char *p = buf;
6264
6265 p += sprintf(p, "%d ", level);
6266 switch (level) {
6267 case IWL_POWER_MODE_CAM:
6268 case IWL_POWER_AC:
6269 p += sprintf(p, "(AC)");
6270 break;
6271 case IWL_POWER_BATTERY:
6272 p += sprintf(p, "(BATTERY)");
6273 break;
6274 default:
6275 p += sprintf(p,
6276 "(Timeout %dms, Period %dms)",
6277 timeout_duration[level - 1] / 1000,
6278 period_duration[level - 1] / 1000);
6279 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006280/*
Zhu Yib481de92007-09-25 17:54:57 -07006281 if (!(priv->power_mode & IWL_POWER_ENABLED))
6282 p += sprintf(p, " OFF\n");
6283 else
6284 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006285*/
6286 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07006287 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07006288}
6289
6290static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6291 store_power_level);
6292
6293static ssize_t show_channels(struct device *d,
6294 struct device_attribute *attr, char *buf)
6295{
Johannes Berg8318d782008-01-24 19:38:38 +01006296 /* all this shit doesn't belong into sysfs anyway */
6297 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006298}
6299
6300static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6301
6302static ssize_t show_statistics(struct device *d,
6303 struct device_attribute *attr, char *buf)
6304{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006305 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006306 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07006307 u32 len = 0, ofs = 0;
6308 u8 *data = (u8 *) & priv->statistics;
6309 int rc = 0;
6310
Tomas Winklerfee12472008-04-03 16:05:21 -07006311 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006312 return -EAGAIN;
6313
6314 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07006315 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006316 mutex_unlock(&priv->mutex);
6317
6318 if (rc) {
6319 len = sprintf(buf,
6320 "Error sending statistics request: 0x%08X\n", rc);
6321 return len;
6322 }
6323
6324 while (size && (PAGE_SIZE - len)) {
6325 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6326 PAGE_SIZE - len, 1);
6327 len = strlen(buf);
6328 if (PAGE_SIZE - len)
6329 buf[len++] = '\n';
6330
6331 ofs += 16;
6332 size -= min(size, 16U);
6333 }
6334
6335 return len;
6336}
6337
6338static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
6339
Zhu Yib481de92007-09-25 17:54:57 -07006340static ssize_t show_status(struct device *d,
6341 struct device_attribute *attr, char *buf)
6342{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006343 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07006344 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006345 return -EAGAIN;
6346 return sprintf(buf, "0x%08x\n", (int)priv->status);
6347}
6348
6349static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6350
6351static ssize_t dump_error_log(struct device *d,
6352 struct device_attribute *attr,
6353 const char *buf, size_t count)
6354{
6355 char *p = (char *)buf;
6356
6357 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006358 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006359
6360 return strnlen(buf, count);
6361}
6362
6363static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
6364
6365static ssize_t dump_event_log(struct device *d,
6366 struct device_attribute *attr,
6367 const char *buf, size_t count)
6368{
6369 char *p = (char *)buf;
6370
6371 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006372 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006373
6374 return strnlen(buf, count);
6375}
6376
6377static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
6378
6379/*****************************************************************************
6380 *
6381 * driver setup and teardown
6382 *
6383 *****************************************************************************/
6384
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006385static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006386{
6387 priv->workqueue = create_workqueue(DRV_NAME);
6388
6389 init_waitqueue_head(&priv->wait_command_queue);
6390
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006391 INIT_WORK(&priv->up, iwl4965_bg_up);
6392 INIT_WORK(&priv->restart, iwl4965_bg_restart);
6393 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
6394 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
6395 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
6396 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
6397 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
6398 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08006399 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006400 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
6401 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
6402 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
6403 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07006404
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006405 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006406
6407 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006408 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07006409}
6410
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006411static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006412{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006413 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006414
Joonwoo Park3ae6a052007-11-29 10:43:16 +09006415 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07006416 cancel_delayed_work(&priv->scan_check);
6417 cancel_delayed_work(&priv->alive_start);
6418 cancel_delayed_work(&priv->post_associate);
6419 cancel_work_sync(&priv->beacon_update);
6420}
6421
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006422static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07006423 &dev_attr_channels.attr,
6424 &dev_attr_dump_errors.attr,
6425 &dev_attr_dump_events.attr,
6426 &dev_attr_flags.attr,
6427 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006428#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006429 &dev_attr_measurement.attr,
6430#endif
6431 &dev_attr_power_level.attr,
6432 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006433 &dev_attr_rs_window.attr,
6434 &dev_attr_statistics.attr,
6435 &dev_attr_status.attr,
6436 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006437 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08006438#ifdef CONFIG_IWLWIFI_DEBUG
6439 &dev_attr_debug_level.attr,
6440#endif
Zhu Yib481de92007-09-25 17:54:57 -07006441
6442 NULL
6443};
6444
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006445static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07006446 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006447 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07006448};
6449
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006450static struct ieee80211_ops iwl4965_hw_ops = {
6451 .tx = iwl4965_mac_tx,
6452 .start = iwl4965_mac_start,
6453 .stop = iwl4965_mac_stop,
6454 .add_interface = iwl4965_mac_add_interface,
6455 .remove_interface = iwl4965_mac_remove_interface,
6456 .config = iwl4965_mac_config,
6457 .config_interface = iwl4965_mac_config_interface,
6458 .configure_filter = iwl4965_configure_filter,
6459 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006460 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006461 .get_stats = iwl4965_mac_get_stats,
6462 .get_tx_stats = iwl4965_mac_get_tx_stats,
6463 .conf_tx = iwl4965_mac_conf_tx,
6464 .get_tsf = iwl4965_mac_get_tsf,
6465 .reset_tsf = iwl4965_mac_reset_tsf,
6466 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01006467 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006468#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02006469 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006470#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006471 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07006472};
6473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006474static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07006475{
6476 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006477 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07006478 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08006479 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006480 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006481 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006482
Assaf Krauss316c30d2008-03-14 10:38:46 -07006483 /************************
6484 * 1. Allocating HW data
6485 ************************/
6486
Cahill, Ben M6440adb2007-11-29 11:09:55 +08006487 /* Disabling hardware scan means that mac80211 will perform scans
6488 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006489 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08006490 if (cfg->mod_params->debug & IWL_DL_INFO)
6491 dev_printk(KERN_DEBUG, &(pdev->dev),
6492 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006493 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006494 }
6495
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006496 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
6497 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07006498 err = -ENOMEM;
6499 goto out;
6500 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006501 priv = hw->priv;
6502 /* At this point both hw and priv are allocated. */
6503
Zhu Yib481de92007-09-25 17:54:57 -07006504 SET_IEEE80211_DEV(hw, &pdev->dev);
6505
6506 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08006507 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07006508 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006509
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006510#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08006511 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07006512 atomic_set(&priv->restrict_refcnt, 0);
6513#endif
Zhu Yib481de92007-09-25 17:54:57 -07006514
Assaf Krauss316c30d2008-03-14 10:38:46 -07006515 /**************************
6516 * 2. Initializing PCI bus
6517 **************************/
6518 if (pci_enable_device(pdev)) {
6519 err = -ENODEV;
6520 goto out_ieee80211_free_hw;
6521 }
6522
6523 pci_set_master(pdev);
6524
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006525 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006526 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006527 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
6528 if (err) {
6529 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6530 if (!err)
6531 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
6532 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006533 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006534 printk(KERN_WARNING "%s: No suitable DMA available.\n",
6535 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006536 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006537 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006538 }
6539
6540 err = pci_request_regions(pdev, DRV_NAME);
6541 if (err)
6542 goto out_pci_disable_device;
6543
6544 pci_set_drvdata(pdev, priv);
6545
6546 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6547 * PCI Tx retries from interfering with C3 CPU state */
6548 pci_write_config_byte(pdev, 0x41, 0x00);
6549
6550 /***********************
6551 * 3. Read REV register
6552 ***********************/
6553 priv->hw_base = pci_iomap(pdev, 0, 0);
6554 if (!priv->hw_base) {
6555 err = -ENODEV;
6556 goto out_pci_release_regions;
6557 }
6558
6559 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
6560 (unsigned long long) pci_resource_len(pdev, 0));
6561 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
6562
Tomas Winklerb661c812008-04-23 17:14:54 -07006563 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006564 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07006565 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
6566 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006567
Tomas Winkler91238712008-04-23 17:14:53 -07006568 /* amp init */
6569 err = priv->cfg->ops->lib->apm_ops.init(priv);
6570 if (err < 0) {
6571 IWL_DEBUG_INFO("Failed to init APMG\n");
6572 goto out_iounmap;
6573 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006574 /*****************
6575 * 4. Read EEPROM
6576 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07006577 /* Read the EEPROM */
6578 err = iwl_eeprom_init(priv);
6579 if (err) {
6580 IWL_ERROR("Unable to init EEPROM\n");
6581 goto out_iounmap;
6582 }
Tomas Winkler8614f362008-04-23 17:14:55 -07006583 err = iwl_eeprom_check_version(priv);
6584 if (err)
6585 goto out_iounmap;
6586
Assaf Krauss316c30d2008-03-14 10:38:46 -07006587 /* MAC Address location in EEPROM same for 3945/4965 */
6588 iwl_eeprom_get_mac(priv, priv->mac_addr);
6589 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
6590 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6591
6592 /************************
6593 * 5. Setup HW constants
6594 ************************/
6595 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07006596 if (priv->cfg->ops->lib->set_hw_params(priv)) {
6597 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07006598 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006599 }
6600
6601 /*******************
6602 * 6. Setup hw/priv
6603 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07006604
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006605 err = iwl_setup(priv);
6606 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006607 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006608 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006609
6610 /**********************************
6611 * 7. Initialize module parameters
6612 **********************************/
6613
6614 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006615 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07006616 set_bit(STATUS_RF_KILL_SW, &priv->status);
6617 IWL_DEBUG_INFO("Radio disabled.\n");
6618 }
6619
Assaf Krauss1ea87392008-03-18 14:57:50 -07006620 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07006621 priv->qos_data.qos_enable = 1;
6622
6623 /********************
6624 * 8. Setup services
6625 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006626 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006627 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006628 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006629
6630 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6631 if (err) {
6632 IWL_ERROR("failed to create sysfs device attributes\n");
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006633 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006634 }
6635
6636 err = iwl_dbgfs_register(priv, DRV_NAME);
6637 if (err) {
6638 IWL_ERROR("failed to create debugfs files\n");
6639 goto out_remove_sysfs;
6640 }
6641
6642 iwl4965_setup_deferred_work(priv);
6643 iwl4965_setup_rx_handlers(priv);
6644
6645 /********************
6646 * 9. Conclude
6647 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08006648 pci_save_state(pdev);
6649 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006650
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006651 /* notify iwlcore to init */
6652 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07006653 return 0;
6654
Assaf Krauss316c30d2008-03-14 10:38:46 -07006655 out_remove_sysfs:
6656 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006657 out_free_eeprom:
6658 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006659 out_iounmap:
6660 pci_iounmap(pdev, priv->hw_base);
6661 out_pci_release_regions:
6662 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006663 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07006664 out_pci_disable_device:
6665 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006666 out_ieee80211_free_hw:
6667 ieee80211_free_hw(priv->hw);
6668 out:
6669 return err;
6670}
6671
Reinette Chatrec83dbf62008-03-21 13:53:41 -07006672static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006673{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006674 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006675 struct list_head *p, *q;
6676 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006677 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07006678
6679 if (!priv)
6680 return;
6681
6682 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
6683
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07006684 if (priv->mac80211_registered) {
6685 ieee80211_unregister_hw(priv->hw);
6686 priv->mac80211_registered = 0;
6687 }
6688
Zhu Yib481de92007-09-25 17:54:57 -07006689 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08006690
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006691 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006692
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006693 /* make sure we flush any pending irq or
6694 * tasklet for the driver
6695 */
6696 spin_lock_irqsave(&priv->lock, flags);
6697 iwl4965_disable_interrupts(priv);
6698 spin_unlock_irqrestore(&priv->lock, flags);
6699
6700 iwl_synchronize_irq(priv);
6701
Zhu Yib481de92007-09-25 17:54:57 -07006702 /* Free MAC hash list for ADHOC */
6703 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
6704 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
6705 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006706 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07006707 }
6708 }
6709
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006710 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07006711 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006712 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07006713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006714 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006715
6716 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08006717 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08006718 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006719
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006720 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006721 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006722
Zhu Yib481de92007-09-25 17:54:57 -07006723
Mohamed Abbas948c1712007-10-25 17:15:45 +08006724 /*netif_stop_queue(dev); */
6725 flush_workqueue(priv->workqueue);
6726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006727 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07006728 * priv->workqueue... so we can't take down the workqueue
6729 * until now... */
6730 destroy_workqueue(priv->workqueue);
6731 priv->workqueue = NULL;
6732
Zhu Yib481de92007-09-25 17:54:57 -07006733 pci_iounmap(pdev, priv->hw_base);
6734 pci_release_regions(pdev);
6735 pci_disable_device(pdev);
6736 pci_set_drvdata(pdev, NULL);
6737
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006738 iwl_free_channel_map(priv);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006739 iwlcore_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006740
6741 if (priv->ibss_beacon)
6742 dev_kfree_skb(priv->ibss_beacon);
6743
6744 ieee80211_free_hw(priv->hw);
6745}
6746
6747#ifdef CONFIG_PM
6748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006749static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07006750{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006751 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006752
Zhu Yie655b9f2008-01-24 02:19:38 -08006753 if (priv->is_open) {
6754 set_bit(STATUS_IN_SUSPEND, &priv->status);
6755 iwl4965_mac_stop(priv->hw);
6756 priv->is_open = 1;
6757 }
Zhu Yib481de92007-09-25 17:54:57 -07006758
Zhu Yib481de92007-09-25 17:54:57 -07006759 pci_set_power_state(pdev, PCI_D3hot);
6760
Zhu Yib481de92007-09-25 17:54:57 -07006761 return 0;
6762}
6763
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006764static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006765{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006766 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006767
Zhu Yib481de92007-09-25 17:54:57 -07006768 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07006769
Zhu Yie655b9f2008-01-24 02:19:38 -08006770 if (priv->is_open)
6771 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006772
Zhu Yie655b9f2008-01-24 02:19:38 -08006773 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006774 return 0;
6775}
6776
6777#endif /* CONFIG_PM */
6778
6779/*****************************************************************************
6780 *
6781 * driver and module entry point
6782 *
6783 *****************************************************************************/
6784
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006785/* Hardware specific file defines the PCI IDs table for that hardware module */
6786static struct pci_device_id iwl_hw_card_ids[] = {
6787 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
6788 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07006789#ifdef CONFIG_IWL5000
6790 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
6791 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
6792 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
6793#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006794 {0}
6795};
6796MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
6797
6798static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07006799 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006800 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006801 .probe = iwl4965_pci_probe,
6802 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07006803#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006804 .suspend = iwl4965_pci_suspend,
6805 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07006806#endif
6807};
6808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006809static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07006810{
6811
6812 int ret;
6813 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
6814 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006815
6816 ret = iwl4965_rate_control_register();
6817 if (ret) {
6818 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
6819 return ret;
6820 }
6821
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006822 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07006823 if (ret) {
6824 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006825 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07006826 }
Zhu Yib481de92007-09-25 17:54:57 -07006827
6828 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006829
6830#ifdef CONFIG_IWLWIFI_DEBUG
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006831 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006832#endif
6833error_register:
6834 iwl4965_rate_control_unregister();
6835 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006836}
6837
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006838static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07006839{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006840 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006841 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07006842}
6843
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006844module_exit(iwl4965_exit);
6845module_init(iwl4965_init);