blob: e3f872e3381d8f5c336b99adbeae328fc6ab9448 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070049#include "iwl-dev.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070050#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070051#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-helpers.h"
Emmanuel Grumbach6974e362008-04-14 21:16:06 -070053#include "iwl-sta.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070054#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070055
Tomas Winklerbabcebf2008-05-15 13:54:00 +080056static int iwl_txq_update_write_ptr(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +080057 struct iwl_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080058
Zhu Yib481de92007-09-25 17:54:57 -070059/******************************************************************************
60 *
61 * module boiler plate
62 *
63 ******************************************************************************/
64
Zhu Yib481de92007-09-25 17:54:57 -070065/*
66 * module name, copyright, version, etc.
67 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
68 */
69
70#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
71
Tomas Winkler0a6857e2008-03-12 16:58:49 -070072#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070073#define VD "d"
74#else
75#define VD
76#endif
77
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080078#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070079#define VS "s"
80#else
81#define VS
82#endif
83
Tomas Winklerdf48c322008-03-06 10:40:19 -080084#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070085
Zhu Yib481de92007-09-25 17:54:57 -070086
87MODULE_DESCRIPTION(DRV_DESCRIPTION);
88MODULE_VERSION(DRV_VERSION);
89MODULE_AUTHOR(DRV_COPYRIGHT);
90MODULE_LICENSE("GPL");
91
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -070092static const struct ieee80211_supported_band *iwl_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070093 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -070094{
Johannes Berg8318d782008-01-24 19:38:38 +010095 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -070096}
97
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080098static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -070099{
100 /* Single white space is for Linksys APs */
101 if (essid_len == 1 && essid[0] == ' ')
102 return 1;
103
104 /* Otherwise, if the entire essid is 0, we assume it is hidden */
105 while (essid_len) {
106 essid_len--;
107 if (essid[essid_len] != '\0')
108 return 0;
109 }
110
111 return 1;
112}
113
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800114static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700115{
116 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
117 const char *s = essid;
118 char *d = escaped;
119
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800120 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700121 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
122 return escaped;
123 }
124
125 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
126 while (essid_len--) {
127 if (*s == '\0') {
128 *d++ = '\\';
129 *d++ = '0';
130 s++;
131 } else
132 *d++ = *s++;
133 }
134 *d = '\0';
135 return escaped;
136}
137
Ester Kummerbf403db2008-05-05 10:22:40 +0800138
Zhu Yib481de92007-09-25 17:54:57 -0700139/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
140 * DMA services
141 *
142 * Theory of operation
143 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800144 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
145 * of buffer descriptors, each of which points to one or more data buffers for
146 * the device to read from or fill. Driver and device exchange status of each
147 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
148 * entries in each circular buffer, to protect against confusing empty and full
149 * queue states.
150 *
151 * The device reads or writes the data in the queues via the device's several
152 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700153 *
154 * For Tx queue, there are low mark and high mark limits. If, after queuing
155 * the packet for Tx, free space become < low mark, Tx queue stopped. When
156 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
157 * Tx queue resumed.
158 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800159 * The 4965 operates with up to 17 queues: One receive queue, one transmit
160 * queue (#4) for sending commands to the device firmware, and 15 other
161 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800162 *
163 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700164 ***************************************************/
165
Tomas Winkler443cfd42008-05-15 13:53:57 +0800166int iwl_queue_space(const struct iwl_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700167{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800168 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700169
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800170 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700171 s -= q->n_bd;
172
173 if (s <= 0)
174 s += q->n_window;
175 /* keep some reserve to not confuse empty and full situations */
176 s -= 2;
177 if (s < 0)
178 s = 0;
179 return s;
180}
181
Zhu Yib481de92007-09-25 17:54:57 -0700182
Tomas Winkler443cfd42008-05-15 13:53:57 +0800183static inline int iwl_queue_used(const struct iwl_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700184{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800185 return q->write_ptr > q->read_ptr ?
186 (i >= q->read_ptr && i < q->write_ptr) :
187 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700188}
189
Tomas Winkler443cfd42008-05-15 13:53:57 +0800190static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700191{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800192 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700193 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800194 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700195
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800196 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700197 return index & (q->n_window - 1);
198}
199
Zhu Yib481de92007-09-25 17:54:57 -0700200
201/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800202 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700203 * the functionality provided here
204 */
205
206/**************************************************************/
207
Ian Schram01ebd062007-10-25 17:15:22 +0800208#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800209/**
210 * iwl4965_remove_station - Remove driver's knowledge of station.
211 *
212 * NOTE: This does not remove station from device's station table.
213 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700214static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700215{
216 int index = IWL_INVALID_STATION;
217 int i;
218 unsigned long flags;
219
220 spin_lock_irqsave(&priv->sta_lock, flags);
221
222 if (is_ap)
223 index = IWL_AP_ID;
224 else if (is_broadcast_ether_addr(addr))
Tomas Winkler5425e492008-04-15 16:01:38 -0700225 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700226 else
Tomas Winkler5425e492008-04-15 16:01:38 -0700227 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Zhu Yib481de92007-09-25 17:54:57 -0700228 if (priv->stations[i].used &&
229 !compare_ether_addr(priv->stations[i].sta.sta.addr,
230 addr)) {
231 index = i;
232 break;
233 }
234
235 if (unlikely(index == IWL_INVALID_STATION))
236 goto out;
237
238 if (priv->stations[index].used) {
239 priv->stations[index].used = 0;
240 priv->num_stations--;
241 }
242
243 BUG_ON(priv->num_stations < 0);
244
245out:
246 spin_unlock_irqrestore(&priv->sta_lock, flags);
247 return 0;
248}
Zhu Yi556f8db2007-09-27 11:27:33 +0800249#endif
Zhu Yib481de92007-09-25 17:54:57 -0700250
Zhu Yib481de92007-09-25 17:54:57 -0700251
Zhu Yib481de92007-09-25 17:54:57 -0700252
253/*************** HOST COMMAND QUEUE FUNCTIONS *****/
254
Zhu Yib481de92007-09-25 17:54:57 -0700255/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800256 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700257 * @priv: device private data point
258 * @cmd: a point to the ucode command structure
259 *
260 * The function returns < 0 values to indicate the operation is
261 * failed. On success, it turns the index (> 0) of command in the
262 * command queue.
263 */
Tomas Winkler857485c2008-03-21 13:53:44 -0700264int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700265{
Ron Rindjunsky16466902008-05-05 10:22:50 +0800266 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Tomas Winkler443cfd42008-05-15 13:53:57 +0800267 struct iwl_queue *q = &txq->q;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800268 struct iwl_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700269 u32 *control_flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700270 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700271 u32 idx;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800272 u16 fix_size;
Zhu Yib481de92007-09-25 17:54:57 -0700273 dma_addr_t phys_addr;
274 int ret;
275 unsigned long flags;
276
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800277 cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
278 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
279
Zhu Yib481de92007-09-25 17:54:57 -0700280 /* If any of the command structures end up being larger than
281 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
282 * we will need to increase the size of the TFD entries */
283 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
284 !(cmd->meta.flags & CMD_SIZE_HUGE));
285
Tomas Winklerfee12472008-04-03 16:05:21 -0700286 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800287 IWL_DEBUG_INFO("Not sending command - RF KILL");
288 return -EIO;
289 }
290
Tomas Winkler443cfd42008-05-15 13:53:57 +0800291 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700292 IWL_ERROR("No space for Tx\n");
293 return -ENOSPC;
294 }
295
296 spin_lock_irqsave(&priv->hcmd_lock, flags);
297
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800298 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700299 memset(tfd, 0, sizeof(*tfd));
300
301 control_flags = (u32 *) tfd;
302
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800303 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700304 out_cmd = &txq->cmd[idx];
305
306 out_cmd->hdr.cmd = cmd->id;
307 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
308 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
309
310 /* At this point, the out_cmd now has all of the incoming cmd
311 * information */
312
313 out_cmd->hdr.flags = 0;
314 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800315 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700316 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
317 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
318
319 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Tomas Winkler857485c2008-03-21 13:53:44 -0700320 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800321 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700322
323 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
324 "%d bytes at %d[%d]:%d\n",
325 get_cmd_string(out_cmd->hdr.cmd),
326 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800327 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700328
329 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800330
331 /* Set up entry in queue's byte count circular buffer */
Tomas Winklere2a722e2008-04-14 21:16:10 -0700332 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800333
334 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800335 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklerbabcebf2008-05-15 13:54:00 +0800336 ret = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700337
338 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
339 return ret ? ret : idx;
340}
341
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700342static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
343{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800344 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -0700345
346 if (hw_decrypt)
347 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
348 else
349 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
350
351}
352
Zhu Yib481de92007-09-25 17:54:57 -0700353/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800354 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700355 *
356 * NOTE: This is really only useful during development and can eventually
357 * be #ifdef'd out once the driver is stable and folks aren't actively
358 * making changes
359 */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800360static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700361{
362 int error = 0;
363 int counter = 1;
364
365 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
366 error |= le32_to_cpu(rxon->flags &
367 (RXON_FLG_TGJ_NARROW_BAND_MSK |
368 RXON_FLG_RADAR_DETECT_MSK));
369 if (error)
370 IWL_WARNING("check 24G fields %d | %d\n",
371 counter++, error);
372 } else {
373 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
374 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
375 if (error)
376 IWL_WARNING("check 52 fields %d | %d\n",
377 counter++, error);
378 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
379 if (error)
380 IWL_WARNING("check 52 CCK %d | %d\n",
381 counter++, error);
382 }
383 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
384 if (error)
385 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
386
387 /* make sure basic rates 6Mbps and 1Mbps are supported */
388 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
389 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
390 if (error)
391 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
392
393 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
394 if (error)
395 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
396
397 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
398 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
399 if (error)
400 IWL_WARNING("check CCK and short slot %d | %d\n",
401 counter++, error);
402
403 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
404 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
405 if (error)
406 IWL_WARNING("check CCK & auto detect %d | %d\n",
407 counter++, error);
408
409 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
410 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
411 if (error)
412 IWL_WARNING("check TGG and auto detect %d | %d\n",
413 counter++, error);
414
415 if (error)
416 IWL_WARNING("Tuning to channel %d\n",
417 le16_to_cpu(rxon->channel));
418
419 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800420 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700421 return -1;
422 }
423 return 0;
424}
425
426/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800427 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800428 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700429 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800430 * If the RXON structure is changing enough to require a new tune,
431 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
432 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700433 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700434static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700435{
436
437 /* These items are only settable from the full RXON command */
438 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
439 compare_ether_addr(priv->staging_rxon.bssid_addr,
440 priv->active_rxon.bssid_addr) ||
441 compare_ether_addr(priv->staging_rxon.node_addr,
442 priv->active_rxon.node_addr) ||
443 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
444 priv->active_rxon.wlap_bssid_addr) ||
445 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
446 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
447 (priv->staging_rxon.air_propagation !=
448 priv->active_rxon.air_propagation) ||
449 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
450 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
451 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
452 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
453 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
454 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
455 return 1;
456
457 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
458 * be updated with the RXON_ASSOC command -- however only some
459 * flag transitions are allowed using RXON_ASSOC */
460
461 /* Check if we are not switching bands */
462 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
463 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
464 return 1;
465
466 /* Check if we are switching association toggle */
467 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
468 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
469 return 1;
470
471 return 0;
472}
473
Zhu Yib481de92007-09-25 17:54:57 -0700474/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800475 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700476 *
Ian Schram01ebd062007-10-25 17:15:22 +0800477 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700478 * the active_rxon structure is updated with the new data. This
479 * function correctly transitions out of the RXON_ASSOC_MSK state if
480 * a HW tune is required based on the RXON structure changes.
481 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700482static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700483{
484 /* cast away the const for active_rxon in this function */
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800485 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -0700486 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700487 int rc = 0;
488
Tomas Winklerfee12472008-04-03 16:05:21 -0700489 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700490 return -1;
491
492 /* always get timestamp with Rx frame */
493 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
494
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800495 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700496 if (rc) {
497 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
498 return -EINVAL;
499 }
500
501 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800502 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700503 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800504 if (!iwl4965_full_rxon_required(priv)) {
Tomas Winkler7e8c5192008-04-15 16:01:43 -0700505 rc = iwl_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700506 if (rc) {
507 IWL_ERROR("Error setting RXON_ASSOC "
508 "configuration (%d).\n", rc);
509 return rc;
510 }
511
512 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
513
514 return 0;
515 }
516
517 /* station table will be cleared */
518 priv->assoc_station_added = 0;
519
Zhu Yib481de92007-09-25 17:54:57 -0700520 /* If we are currently associated and the new config requires
521 * an RXON_ASSOC and the new config wants the associated mask enabled,
522 * we must clear the associated from the active configuration
523 * before we apply the new config */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700524 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700525 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
526 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
527 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
528
Tomas Winkler857485c2008-03-21 13:53:44 -0700529 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800530 sizeof(struct iwl_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -0700531 &priv->active_rxon);
532
533 /* If the mask clearing failed then we set
534 * active_rxon back to what it was previously */
535 if (rc) {
536 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
537 IWL_ERROR("Error clearing ASSOC_MSK on current "
538 "configuration (%d).\n", rc);
539 return rc;
540 }
Zhu Yib481de92007-09-25 17:54:57 -0700541 }
542
543 IWL_DEBUG_INFO("Sending RXON\n"
544 "* with%s RXON_FILTER_ASSOC_MSK\n"
545 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -0700546 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -0700547 ((priv->staging_rxon.filter_flags &
548 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
549 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -0700550 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -0700551
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700552 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
Zhu Yib481de92007-09-25 17:54:57 -0700553 /* Apply the new configuration */
Tomas Winkler857485c2008-03-21 13:53:44 -0700554 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +0800555 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700556 if (rc) {
557 IWL_ERROR("Error setting new configuration (%d).\n", rc);
558 return rc;
559 }
560
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700561 iwlcore_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800562
Zhu Yib481de92007-09-25 17:54:57 -0700563 if (!priv->error_recovering)
564 priv->start_calib = 0;
565
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700566 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700567
568 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
569
570 /* If we issue a new RXON command which required a tune then we must
571 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800572 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700573 if (rc) {
574 IWL_ERROR("Error setting Tx power (%d).\n", rc);
575 return rc;
576 }
577
578 /* Add the broadcast address so we can send broadcast frames */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800579 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700580 IWL_INVALID_STATION) {
581 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
582 return -EIO;
583 }
584
585 /* If we have set the ASSOC_MSK and we are in BSS mode then
586 * add the IWL_AP_ID to the station rate table */
Tomas Winkler3109ece2008-03-28 16:33:35 -0700587 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -0700588 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Tomas Winkler4f40e4d2008-05-15 13:54:04 +0800589 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -0700590 == IWL_INVALID_STATION) {
591 IWL_ERROR("Error adding AP address for transmit.\n");
592 return -EIO;
593 }
594 priv->assoc_station_added = 1;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -0700595 if (priv->default_wep_key &&
596 iwl_send_static_wepkey_cmd(priv, 0))
597 IWL_ERROR("Could not send WEP static key.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700598 }
599
600 return 0;
601}
602
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700603void iwl4965_update_chain_flags(struct iwl_priv *priv)
604{
605
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -0700606 iwl_set_rxon_chain(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700607 iwl4965_commit_rxon(priv);
608}
609
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700610static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700611{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800612 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700613 .flags = 3,
614 .lead_time = 0xAA,
615 .max_kill = 1,
616 .kill_ack_mask = 0,
617 .kill_cts_mask = 0,
618 };
619
Tomas Winkler857485c2008-03-21 13:53:44 -0700620 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800621 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700622}
623
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700624static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700625{
Tomas Winklerdb11d632008-05-05 10:22:33 +0800626 int ret = 0;
627 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -0700628 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700629 .id = REPLY_SCAN_ABORT_CMD,
630 .meta.flags = CMD_WANT_SKB,
631 };
632
633 /* If there isn't a scan actively going on in the hardware
634 * then we are in between scan bands and not actually
635 * actively scanning, so don't send the abort command */
636 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
637 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
638 return 0;
639 }
640
Tomas Winklerdb11d632008-05-05 10:22:33 +0800641 ret = iwl_send_cmd_sync(priv, &cmd);
642 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700643 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winklerdb11d632008-05-05 10:22:33 +0800644 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700645 }
646
Tomas Winklerdb11d632008-05-05 10:22:33 +0800647 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700648 if (res->u.status != CAN_ABORT_STATUS) {
649 /* The scan abort will return 1 for success or
650 * 2 for "failure". A failure condition can be
651 * due to simply not being in an active scan which
652 * can occur if we send the scan abort before we
653 * the microcode has notified us that a scan is
654 * completed. */
655 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
656 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
657 clear_bit(STATUS_SCAN_HW, &priv->status);
658 }
659
660 dev_kfree_skb_any(cmd.meta.u.skb);
661
Tomas Winklerdb11d632008-05-05 10:22:33 +0800662 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700663}
664
Zhu Yib481de92007-09-25 17:54:57 -0700665/*
666 * CARD_STATE_CMD
667 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800668 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -0700669 *
670 * When in the 'enable' state the card operates as normal.
671 * When in the 'disable' state, the card enters into a low power mode.
672 * When in the 'halt' state, the card is shut down and must be fully
673 * restarted to come back on.
674 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700675static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -0700676{
Tomas Winkler857485c2008-03-21 13:53:44 -0700677 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700678 .id = REPLY_CARD_STATE_CMD,
679 .len = sizeof(u32),
680 .data = &flags,
681 .meta.flags = meta_flag,
682 };
683
Tomas Winkler857485c2008-03-21 13:53:44 -0700684 return iwl_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700685}
686
Tomas Winklerfcab4232008-05-15 13:54:01 +0800687static void iwl_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700688{
689 struct list_head *element;
690
691 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
692 priv->frames_count);
693
694 while (!list_empty(&priv->free_frames)) {
695 element = priv->free_frames.next;
696 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800697 kfree(list_entry(element, struct iwl_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -0700698 priv->frames_count--;
699 }
700
701 if (priv->frames_count) {
702 IWL_WARNING("%d frames still in use. Did we lose one?\n",
703 priv->frames_count);
704 priv->frames_count = 0;
705 }
706}
707
Tomas Winklerfcab4232008-05-15 13:54:01 +0800708static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700709{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800710 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700711 struct list_head *element;
712 if (list_empty(&priv->free_frames)) {
713 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
714 if (!frame) {
715 IWL_ERROR("Could not allocate frame!\n");
716 return NULL;
717 }
718
719 priv->frames_count++;
720 return frame;
721 }
722
723 element = priv->free_frames.next;
724 list_del(element);
Tomas Winklerfcab4232008-05-15 13:54:01 +0800725 return list_entry(element, struct iwl_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -0700726}
727
Tomas Winklerfcab4232008-05-15 13:54:01 +0800728static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -0700729{
730 memset(frame, 0, sizeof(*frame));
731 list_add(&frame->list, &priv->free_frames);
732}
733
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700734unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700735 struct ieee80211_hdr *hdr,
736 const u8 *dest, int left)
737{
738
Tomas Winkler3109ece2008-03-28 16:33:35 -0700739 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -0700740 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
741 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
742 return 0;
743
744 if (priv->ibss_beacon->len > left)
745 return 0;
746
747 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
748
749 return priv->ibss_beacon->len;
750}
751
Guy Cohen39e88502008-04-23 17:14:57 -0700752static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700753{
Guy Cohen39e88502008-04-23 17:14:57 -0700754 int i;
755 int rate_mask;
Zhu Yib481de92007-09-25 17:54:57 -0700756
Guy Cohen39e88502008-04-23 17:14:57 -0700757 /* Set rate mask*/
758 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
759 rate_mask = priv->active_rate_basic & 0xF;
760 else
761 rate_mask = priv->active_rate_basic & 0xFF0;
762
763 /* Find lowest valid rate */
Zhu Yib481de92007-09-25 17:54:57 -0700764 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800765 i = iwl_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -0700766 if (rate_mask & (1 << i))
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800767 return iwl_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700768 }
769
Guy Cohen39e88502008-04-23 17:14:57 -0700770 /* No valid rate was found. Assign the lowest one */
771 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
772 return IWL_RATE_1M_PLCP;
773 else
774 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -0700775}
776
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700777static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700778{
Tomas Winklerfcab4232008-05-15 13:54:01 +0800779 struct iwl_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -0700780 unsigned int frame_size;
781 int rc;
782 u8 rate;
783
Tomas Winklerfcab4232008-05-15 13:54:01 +0800784 frame = iwl_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700785
786 if (!frame) {
787 IWL_ERROR("Could not obtain free frame buffer for beacon "
788 "command.\n");
789 return -ENOMEM;
790 }
791
Guy Cohen39e88502008-04-23 17:14:57 -0700792 rate = iwl4965_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700793
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800794 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -0700795
Tomas Winkler857485c2008-03-21 13:53:44 -0700796 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -0700797 &frame->u.cmd[0]);
798
Tomas Winklerfcab4232008-05-15 13:54:01 +0800799 iwl_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -0700800
801 return rc;
802}
803
804/******************************************************************************
805 *
Zhu Yib481de92007-09-25 17:54:57 -0700806 * Misc. internal state and helper functions
807 *
808 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -0700809
Zhu Yib481de92007-09-25 17:54:57 -0700810/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800811 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -0700812 *
813 * return : set the bit for each supported rate insert in ie
814 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800815static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +0200816 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -0700817{
818 u16 ret_rates = 0, bit;
819 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200820 u8 *cnt = ie;
821 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700822
823 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
824 if (bit & supported_rate) {
825 ret_rates |= bit;
Tomas Winkler1826dcc2008-05-15 13:54:02 +0800826 rates[*cnt] = iwl_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +0200827 ((bit & basic_rate) ? 0x80 : 0x00);
828 (*cnt)++;
829 (*left)--;
830 if ((*left <= 0) ||
831 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -0700832 break;
833 }
834 }
835
836 return ret_rates;
837}
838
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700839#ifdef CONFIG_IWL4965_HT
840static void iwl4965_ht_conf(struct iwl_priv *priv,
841 struct ieee80211_bss_conf *bss_conf)
842{
843 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
844 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
845 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
846
847 IWL_DEBUG_MAC80211("enter: \n");
848
849 iwl_conf->is_ht = bss_conf->assoc_ht;
850
851 if (!iwl_conf->is_ht)
852 return;
853
854 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
855
856 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
857 iwl_conf->sgf |= 0x1;
858 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
859 iwl_conf->sgf |= 0x2;
860
861 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
862 iwl_conf->max_amsdu_size =
863 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
864
865 iwl_conf->supported_chan_width =
866 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
867 iwl_conf->extension_chan_offset =
868 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
869 /* If no above or below channel supplied disable FAT channel */
870 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
871 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
872 iwl_conf->supported_chan_width = 0;
873
874 iwl_conf->tx_mimo_ps_mode =
875 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
876 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
877
878 iwl_conf->control_channel = ht_bss_conf->primary_channel;
879 iwl_conf->tx_chan_width =
880 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
881 iwl_conf->ht_protection =
882 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
883 iwl_conf->non_GF_STA_present =
884 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
885
886 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
887 IWL_DEBUG_MAC80211("leave\n");
888}
889
890static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
891 u8 *pos, int *left)
892{
893 struct ieee80211_ht_cap *ht_cap;
894
895 if (!sband || !sband->ht_info.ht_supported)
896 return;
897
898 if (*left < sizeof(struct ieee80211_ht_cap))
899 return;
900
901 *pos++ = sizeof(struct ieee80211_ht_cap);
902 ht_cap = (struct ieee80211_ht_cap *) pos;
903
904 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
905 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
906 ht_cap->ampdu_params_info =
907 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
908 ((sband->ht_info.ampdu_density << 2) &
909 IEEE80211_HT_CAP_AMPDU_DENSITY);
910 *left -= sizeof(struct ieee80211_ht_cap);
911}
912#else
913static inline void iwl4965_ht_conf(struct iwl_priv *priv,
914 struct ieee80211_bss_conf *bss_conf)
915{
916}
917static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
918 u8 *pos, int *left)
919{
920}
921#endif
922
923
Zhu Yib481de92007-09-25 17:54:57 -0700924/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800925 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -0700926 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700927static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +0200928 enum ieee80211_band band,
929 struct ieee80211_mgmt *frame,
930 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -0700931{
932 int len = 0;
933 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +0800934 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Tomas Winkler78330fd2008-02-06 02:37:18 +0200935 const struct ieee80211_supported_band *sband =
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -0700936 iwl_get_hw_mode(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -0700937
938 /* Make sure there is enough space for the probe request,
939 * two mandatory IEs and the data */
940 left -= 24;
941 if (left < 0)
942 return 0;
943 len += 24;
944
945 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800946 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -0700947 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Tomas Winkler57bd1be2008-05-15 13:54:03 +0800948 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -0700949 frame->seq_ctrl = 0;
950
951 /* fill in our indirect SSID IE */
952 /* ...next IE... */
953
954 left -= 2;
955 if (left < 0)
956 return 0;
957 len += 2;
958 pos = &(frame->u.probe_req.variable[0]);
959 *pos++ = WLAN_EID_SSID;
960 *pos++ = 0;
961
962 /* fill in our direct SSID IE... */
963 if (is_direct) {
964 /* ...next IE... */
965 left -= 2 + priv->essid_len;
966 if (left < 0)
967 return 0;
968 /* ... fill it in... */
969 *pos++ = WLAN_EID_SSID;
970 *pos++ = priv->essid_len;
971 memcpy(pos, priv->essid, priv->essid_len);
972 pos += priv->essid_len;
973 len += 2 + priv->essid_len;
974 }
975
976 /* fill in supported rate */
977 /* ...next IE... */
978 left -= 2;
979 if (left < 0)
980 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200981
Zhu Yib481de92007-09-25 17:54:57 -0700982 /* ... fill it in... */
983 *pos++ = WLAN_EID_SUPP_RATES;
984 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +0200985
mabbasbee488d2007-10-25 17:15:42 +0800986 /* exclude 60M rate */
987 active_rates = priv->rates_mask;
988 active_rates &= ~IWL_RATE_60M_MASK;
989
990 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -0700991
Tomas Winklerc7c46672007-10-18 02:04:15 +0200992 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800993 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +0800994 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +0200995 active_rates &= ~ret_rates;
996
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800997 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +0800998 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +0200999 active_rates &= ~ret_rates;
1000
Zhu Yib481de92007-09-25 17:54:57 -07001001 len += 2 + *pos;
1002 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001003 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001004 goto fill_end;
1005
1006 /* fill in supported extended rate */
1007 /* ...next IE... */
1008 left -= 2;
1009 if (left < 0)
1010 return 0;
1011 /* ... fill it in... */
1012 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1013 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001014 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001015 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001016 if (*pos > 0)
1017 len += 2 + *pos;
1018
Zhu Yib481de92007-09-25 17:54:57 -07001019 fill_end:
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001020 /* fill in HT IE */
1021 left -= 2;
1022 if (left < 0)
1023 return 0;
1024
1025 *pos++ = WLAN_EID_HT_CAPABILITY;
1026 *pos = 0;
1027
1028 iwl_ht_cap_to_ie(sband, pos, &left);
1029
1030 if (*pos > 0)
1031 len += 2 + *pos;
Zhu Yib481de92007-09-25 17:54:57 -07001032 return (u16)len;
1033}
1034
1035/*
1036 * QoS support
1037*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001038static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001039 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001040{
1041
Tomas Winkler857485c2008-03-21 13:53:44 -07001042 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001043 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001044}
1045
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001046static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001047{
1048 unsigned long flags;
1049
Zhu Yib481de92007-09-25 17:54:57 -07001050 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1051 return;
1052
1053 if (!priv->qos_data.qos_enable)
1054 return;
1055
1056 spin_lock_irqsave(&priv->lock, flags);
1057 priv->qos_data.def_qos_parm.qos_flags = 0;
1058
1059 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1060 !priv->qos_data.qos_cap.q_AP.txop_request)
1061 priv->qos_data.def_qos_parm.qos_flags |=
1062 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001063 if (priv->qos_data.qos_active)
1064 priv->qos_data.def_qos_parm.qos_flags |=
1065 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1066
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001067#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001068 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001069 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001070#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001071
Zhu Yib481de92007-09-25 17:54:57 -07001072 spin_unlock_irqrestore(&priv->lock, flags);
1073
Tomas Winkler3109ece2008-03-28 16:33:35 -07001074 if (force || iwl_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001075 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1076 priv->qos_data.qos_active,
1077 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001078
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001079 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001080 &(priv->qos_data.def_qos_parm));
1081 }
1082}
1083
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001084int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001085{
1086 /* Filter incoming packets to determine if they are targeted toward
1087 * this network, discarding packets coming from ourselves */
1088 switch (priv->iw_mode) {
1089 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1090 /* packets from our adapter are dropped (echo) */
1091 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1092 return 0;
1093 /* {broad,multi}cast packets to our IBSS go through */
1094 if (is_multicast_ether_addr(header->addr1))
1095 return !compare_ether_addr(header->addr3, priv->bssid);
1096 /* packets to our adapter go through */
1097 return !compare_ether_addr(header->addr1, priv->mac_addr);
1098 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1099 /* packets from our adapter are dropped (echo) */
1100 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1101 return 0;
1102 /* {broad,multi}cast packets to our BSS go through */
1103 if (is_multicast_ether_addr(header->addr1))
1104 return !compare_ether_addr(header->addr2, priv->bssid);
1105 /* packets to our adapter go through */
1106 return !compare_ether_addr(header->addr1, priv->mac_addr);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001107 default:
1108 break;
Zhu Yib481de92007-09-25 17:54:57 -07001109 }
1110
1111 return 1;
1112}
1113
1114#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1115
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001116static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07001117{
1118 switch (status & TX_STATUS_MSK) {
1119 case TX_STATUS_SUCCESS:
1120 return "SUCCESS";
1121 TX_STATUS_ENTRY(SHORT_LIMIT);
1122 TX_STATUS_ENTRY(LONG_LIMIT);
1123 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1124 TX_STATUS_ENTRY(MGMNT_ABORT);
1125 TX_STATUS_ENTRY(NEXT_FRAG);
1126 TX_STATUS_ENTRY(LIFE_EXPIRE);
1127 TX_STATUS_ENTRY(DEST_PS);
1128 TX_STATUS_ENTRY(ABORTED);
1129 TX_STATUS_ENTRY(BT_RETRY);
1130 TX_STATUS_ENTRY(STA_INVALID);
1131 TX_STATUS_ENTRY(FRAG_DROPPED);
1132 TX_STATUS_ENTRY(TID_DISABLE);
1133 TX_STATUS_ENTRY(FRAME_FLUSHED);
1134 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1135 TX_STATUS_ENTRY(TX_LOCKED);
1136 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1137 }
1138
1139 return "UNKNOWN";
1140}
1141
1142/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001143 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001144 *
1145 * NOTE: priv->mutex is not required before calling this function
1146 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001147static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001148{
1149 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1150 clear_bit(STATUS_SCANNING, &priv->status);
1151 return 0;
1152 }
1153
1154 if (test_bit(STATUS_SCANNING, &priv->status)) {
1155 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1156 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1157 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1158 queue_work(priv->workqueue, &priv->abort_scan);
1159
1160 } else
1161 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1162
1163 return test_bit(STATUS_SCANNING, &priv->status);
1164 }
1165
1166 return 0;
1167}
1168
1169/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001170 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001171 * @ms: amount of time to wait (in milliseconds) for scan to abort
1172 *
1173 * NOTE: priv->mutex must be held before calling this function
1174 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001175static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001176{
1177 unsigned long now = jiffies;
1178 int ret;
1179
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001180 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001181 if (ret && ms) {
1182 mutex_unlock(&priv->mutex);
1183 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1184 test_bit(STATUS_SCANNING, &priv->status))
1185 msleep(1);
1186 mutex_lock(&priv->mutex);
1187
1188 return test_bit(STATUS_SCANNING, &priv->status);
1189 }
1190
1191 return ret;
1192}
1193
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001194static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001195{
1196 /* Reset ieee stats */
1197
1198 /* We don't reset the net_device_stats (ieee->stats) on
1199 * re-association */
1200
1201 priv->last_seq_num = -1;
1202 priv->last_frag_num = -1;
1203 priv->last_packet_time = 0;
1204
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001205 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001206}
1207
1208#define MAX_UCODE_BEACON_INTERVAL 4096
1209#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1210
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001211static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001212{
1213 u16 new_val = 0;
1214 u16 beacon_factor = 0;
1215
1216 beacon_factor =
1217 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1218 / MAX_UCODE_BEACON_INTERVAL;
1219 new_val = beacon_val / beacon_factor;
1220
1221 return cpu_to_le16(new_val);
1222}
1223
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001224static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001225{
1226 u64 interval_tm_unit;
1227 u64 tsf, result;
1228 unsigned long flags;
1229 struct ieee80211_conf *conf = NULL;
1230 u16 beacon_int = 0;
1231
1232 conf = ieee80211_get_hw_conf(priv->hw);
1233
1234 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3109ece2008-03-28 16:33:35 -07001235 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1236 priv->rxon_timing.timestamp.dw[0] =
1237 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07001238
1239 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1240
Tomas Winkler3109ece2008-03-28 16:33:35 -07001241 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001242
1243 beacon_int = priv->beacon_int;
1244 spin_unlock_irqrestore(&priv->lock, flags);
1245
1246 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1247 if (beacon_int == 0) {
1248 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1249 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1250 } else {
1251 priv->rxon_timing.beacon_interval =
1252 cpu_to_le16(beacon_int);
1253 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001254 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001255 le16_to_cpu(priv->rxon_timing.beacon_interval));
1256 }
1257
1258 priv->rxon_timing.atim_window = 0;
1259 } else {
1260 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001261 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001262 /* TODO: we need to get atim_window from upper stack
1263 * for now we set to 0 */
1264 priv->rxon_timing.atim_window = 0;
1265 }
1266
1267 interval_tm_unit =
1268 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1269 result = do_div(tsf, interval_tm_unit);
1270 priv->rxon_timing.beacon_init_val =
1271 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1272
1273 IWL_DEBUG_ASSOC
1274 ("beacon interval %d beacon timer %d beacon tim %d\n",
1275 le16_to_cpu(priv->rxon_timing.beacon_interval),
1276 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1277 le16_to_cpu(priv->rxon_timing.atim_window));
1278}
1279
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001280static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001281{
1282 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1283 IWL_ERROR("APs don't scan.\n");
1284 return 0;
1285 }
1286
Tomas Winklerfee12472008-04-03 16:05:21 -07001287 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001288 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1289 return -EIO;
1290 }
1291
1292 if (test_bit(STATUS_SCANNING, &priv->status)) {
1293 IWL_DEBUG_SCAN("Scan already in progress.\n");
1294 return -EAGAIN;
1295 }
1296
1297 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1298 IWL_DEBUG_SCAN("Scan request while abort pending. "
1299 "Queuing.\n");
1300 return -EAGAIN;
1301 }
1302
1303 IWL_DEBUG_INFO("Starting scan...\n");
1304 priv->scan_bands = 2;
1305 set_bit(STATUS_SCANNING, &priv->status);
1306 priv->scan_start = jiffies;
1307 priv->scan_pass_start = priv->scan_start;
1308
1309 queue_work(priv->workqueue, &priv->request_scan);
1310
1311 return 0;
1312}
1313
Zhu Yib481de92007-09-25 17:54:57 -07001314
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001315static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001316 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001317{
Johannes Berg8318d782008-01-24 19:38:38 +01001318 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07001319 priv->staging_rxon.flags &=
1320 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1321 | RXON_FLG_CCK_MSK);
1322 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1323 } else {
Reinette Chatre508e32e2008-04-14 21:16:13 -07001324 /* Copied from iwl4965_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001325 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1326 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1327 else
1328 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1329
1330 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1331 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1332
1333 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1334 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1335 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1336 }
1337}
1338
1339/*
Ian Schram01ebd062007-10-25 17:15:22 +08001340 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001341 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001342static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001343{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001344 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001345
1346 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1347
1348 switch (priv->iw_mode) {
1349 case IEEE80211_IF_TYPE_AP:
1350 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1351 break;
1352
1353 case IEEE80211_IF_TYPE_STA:
1354 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1355 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1356 break;
1357
1358 case IEEE80211_IF_TYPE_IBSS:
1359 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1360 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1361 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1362 RXON_FILTER_ACCEPT_GRP_MSK;
1363 break;
1364
1365 case IEEE80211_IF_TYPE_MNTR:
1366 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1367 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1368 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1369 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001370 default:
1371 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1372 break;
Zhu Yib481de92007-09-25 17:54:57 -07001373 }
1374
1375#if 0
1376 /* TODO: Figure out when short_preamble would be set and cache from
1377 * that */
1378 if (!hw_to_local(priv->hw)->short_preamble)
1379 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1380 else
1381 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1382#endif
1383
Assaf Krauss8622e702008-03-21 13:53:43 -07001384 ch_info = iwl_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001385 le16_to_cpu(priv->staging_rxon.channel));
1386
1387 if (!ch_info)
1388 ch_info = &priv->channel_info[0];
1389
1390 /*
1391 * in some case A channels are all non IBSS
1392 * in this case force B/G channel
1393 */
1394 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1395 !(is_channel_ibss(ch_info)))
1396 ch_info = &priv->channel_info[0];
1397
1398 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01001399 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07001400
Johannes Berg8318d782008-01-24 19:38:38 +01001401 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001402
1403 priv->staging_rxon.ofdm_basic_rates =
1404 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1405 priv->staging_rxon.cck_basic_rates =
1406 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1407
1408 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1409 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1410 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1411 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1412 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1413 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07001414 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001415}
1416
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001417static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001418{
Zhu Yib481de92007-09-25 17:54:57 -07001419 if (mode == IEEE80211_IF_TYPE_IBSS) {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001420 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001421
Assaf Krauss8622e702008-03-21 13:53:43 -07001422 ch_info = iwl_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001423 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07001424 le16_to_cpu(priv->staging_rxon.channel));
1425
1426 if (!ch_info || !is_channel_ibss(ch_info)) {
1427 IWL_ERROR("channel %d not IBSS channel\n",
1428 le16_to_cpu(priv->staging_rxon.channel));
1429 return -EINVAL;
1430 }
1431 }
1432
Zhu Yib481de92007-09-25 17:54:57 -07001433 priv->iw_mode = mode;
1434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001435 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001436 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1437
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001438 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001439
Mohamed Abbasfde35712007-11-29 11:10:15 +08001440 /* dont commit rxon if rf-kill is on*/
Tomas Winklerfee12472008-04-03 16:05:21 -07001441 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08001442 return -EAGAIN;
1443
1444 cancel_delayed_work(&priv->scan_check);
1445 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1446 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1447 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1448 return -EAGAIN;
1449 }
1450
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001451 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001452
1453 return 0;
1454}
1455
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001456static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001457 struct ieee80211_tx_control *ctl,
Tomas Winkler857485c2008-03-21 13:53:44 -07001458 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001459 struct sk_buff *skb_frag,
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001460 int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001461{
Tomas Winkler6def9762008-05-05 10:22:31 +08001462 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001463 struct iwl_wep_key *wepkey;
1464 int keyidx = 0;
1465
Ivo van Doorn1c014422008-04-17 19:41:02 +02001466 BUG_ON(ctl->hw_key->hw_key_idx > 3);
Zhu Yib481de92007-09-25 17:54:57 -07001467
1468 switch (keyinfo->alg) {
1469 case ALG_CCMP:
1470 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1471 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07001472 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1473 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001474 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1475 break;
1476
1477 case ALG_TKIP:
Zhu Yib481de92007-09-25 17:54:57 -07001478 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
Emmanuel Grumbach2bc75082008-03-19 16:41:45 -07001479 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1480 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1481 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07001482 break;
1483
1484 case ALG_WEP:
Ivo van Doorn1c014422008-04-17 19:41:02 +02001485 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001486 cmd->cmd.tx.sec_ctl = 0;
1487 if (priv->default_wep_key) {
1488 /* the WEP key was sent as static */
Ivo van Doorn1c014422008-04-17 19:41:02 +02001489 keyidx = ctl->hw_key->hw_key_idx;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001490 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1491 wepkey->key_size);
1492 if (wepkey->key_size == WEP_KEY_LEN_128)
1493 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1494 } else {
Emmanuel Grumbach0211ddd2008-04-14 21:16:07 -07001495 /* the WEP key was sent as dynamic */
1496 keyidx = keyinfo->keyidx;
1497 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1498 keyinfo->keylen);
1499 if (keyinfo->keylen == WEP_KEY_LEN_128)
1500 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001501 }
Zhu Yib481de92007-09-25 17:54:57 -07001502
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001503 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1504 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
Zhu Yib481de92007-09-25 17:54:57 -07001505
1506 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07001507 "with key %d\n", keyidx);
Zhu Yib481de92007-09-25 17:54:57 -07001508 break;
1509
Zhu Yib481de92007-09-25 17:54:57 -07001510 default:
1511 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1512 break;
1513 }
1514}
1515
1516/*
1517 * handle build REPLY_TX command notification.
1518 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001519static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07001520 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001521 struct ieee80211_tx_control *ctrl,
1522 struct ieee80211_hdr *hdr,
1523 int is_unicast, u8 std_id)
1524{
Zhu Yib481de92007-09-25 17:54:57 -07001525 u16 fc = le16_to_cpu(hdr->frame_control);
1526 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1527
1528 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1529 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1530 tx_flags |= TX_CMD_FLG_ACK_MSK;
1531 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1532 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1533 if (ieee80211_is_probe_response(fc) &&
1534 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1535 tx_flags |= TX_CMD_FLG_TSF_MSK;
1536 } else {
1537 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1538 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1539 }
1540
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001541 if (ieee80211_is_back_request(fc))
1542 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1543
1544
Zhu Yib481de92007-09-25 17:54:57 -07001545 cmd->cmd.tx.sta_id = std_id;
1546 if (ieee80211_get_morefrag(hdr))
1547 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1548
Tomas Winkler54dbb522008-05-15 13:54:06 +08001549 if (ieee80211_is_qos_data(fc)) {
1550 u8 *qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
1551 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07001552 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001553 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001554 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001555 }
Zhu Yib481de92007-09-25 17:54:57 -07001556
1557 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1558 tx_flags |= TX_CMD_FLG_RTS_MSK;
1559 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1560 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1561 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1562 tx_flags |= TX_CMD_FLG_CTS_MSK;
1563 }
1564
1565 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1566 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1567
1568 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1569 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1570 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1571 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08001572 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07001573 else
Ian Schrambc434dd2007-10-25 17:15:29 +08001574 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001575 } else {
Zhu Yib481de92007-09-25 17:54:57 -07001576 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001577 }
Zhu Yib481de92007-09-25 17:54:57 -07001578
1579 cmd->cmd.tx.driver_txop = 0;
1580 cmd->cmd.tx.tx_flags = tx_flags;
1581 cmd->cmd.tx.next_frame_len = 0;
1582}
Tomas Winkler19758be2008-03-12 16:58:51 -07001583static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1584{
1585 /* 0 - mgmt, 1 - cnt, 2 - data */
1586 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1587 priv->tx_stats[idx].cnt++;
1588 priv->tx_stats[idx].bytes += len;
1589}
Zhu Yib481de92007-09-25 17:54:57 -07001590/*
1591 * start REPLY_TX command process
1592 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001593static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001594 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1595{
1596 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001597 struct iwl_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07001598 u32 *control_flags;
1599 int txq_id = ctl->queue;
Ron Rindjunsky16466902008-05-05 10:22:50 +08001600 struct iwl_tx_queue *txq = NULL;
Tomas Winkler443cfd42008-05-15 13:53:57 +08001601 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001602 dma_addr_t phys_addr;
1603 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001604 dma_addr_t scratch_phys;
Tomas Winkler857485c2008-03-21 13:53:44 -07001605 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001606 u16 len, idx, len_org;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001607 u8 hdr_len;
1608 u8 id;
1609 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07001610 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001611 u8 tid = 0;
1612 u8 wait_write_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001613 u16 seq_number = 0;
1614 u16 fc;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001615 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001616 unsigned long flags;
1617 int rc;
1618
1619 spin_lock_irqsave(&priv->lock, flags);
Tomas Winklerfee12472008-04-03 16:05:21 -07001620 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001621 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1622 goto drop_unlock;
1623 }
1624
Johannes Berg32bfd352007-12-19 01:31:26 +01001625 if (!priv->vif) {
1626 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07001627 goto drop_unlock;
1628 }
1629
Johannes Berg8318d782008-01-24 19:38:38 +01001630 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07001631 IWL_ERROR("ERROR: No TX rate available.\n");
1632 goto drop_unlock;
1633 }
1634
1635 unicast = !is_multicast_ether_addr(hdr->addr1);
1636 id = 0;
1637
1638 fc = le16_to_cpu(hdr->frame_control);
1639
Tomas Winkler0a6857e2008-03-12 16:58:49 -07001640#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001641 if (ieee80211_is_auth(fc))
1642 IWL_DEBUG_TX("Sending AUTH frame\n");
1643 else if (ieee80211_is_assoc_request(fc))
1644 IWL_DEBUG_TX("Sending ASSOC frame\n");
1645 else if (ieee80211_is_reassoc_request(fc))
1646 IWL_DEBUG_TX("Sending REASSOC frame\n");
1647#endif
1648
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08001649 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08001650 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Tomas Winkler3109ece2008-03-28 16:33:35 -07001651 (!iwl_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08001652 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08001653 !priv->assoc_station_added)) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07001654 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07001655 goto drop_unlock;
1656 }
1657
1658 spin_unlock_irqrestore(&priv->lock, flags);
1659
1660 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001661
1662 /* Find (or create) index into station table for destination station */
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08001663 sta_id = iwl_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001664 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07001665 DECLARE_MAC_BUF(mac);
1666
1667 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
1668 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07001669 goto drop;
1670 }
1671
Guy Cohen07bc28e2008-04-23 17:15:03 -07001672 IWL_DEBUG_TX("station Id %d\n", sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001673
Tomas Winkler54dbb522008-05-15 13:54:06 +08001674 if (ieee80211_is_qos_data(fc)) {
1675 qc = ieee80211_get_qos_ctrl(hdr, hdr_len);
1676 tid = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07001677 seq_number = priv->stations[sta_id].tid[tid].seq_number &
1678 IEEE80211_SCTL_SEQ;
1679 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1680 (hdr->seq_ctrl &
1681 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
1682 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001683#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001684 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001685 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07001686 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02001687 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001688#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001689 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001690
1691 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07001692 txq = &priv->txq[txq_id];
1693 q = &txq->q;
1694
1695 spin_lock_irqsave(&priv->lock, flags);
1696
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001697 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001698 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07001699 memset(tfd, 0, sizeof(*tfd));
1700 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001701 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001702
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001703 /* Set up driver data for this TFD */
Tomas Winkler8567c632008-05-15 13:53:58 +08001704 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001705 txq->txb[q->write_ptr].skb[0] = skb;
1706 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07001707 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001708
1709 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07001710 out_cmd = &txq->cmd[idx];
1711 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1712 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001713
1714 /*
1715 * Set up the Tx-command (not MAC!) header.
1716 * Store the chosen Tx queue and TFD index within the sequence field;
1717 * after Tx, uCode's Tx response will return this value so driver can
1718 * locate the frame within the tx queue and do post-tx processing.
1719 */
Zhu Yib481de92007-09-25 17:54:57 -07001720 out_cmd->hdr.cmd = REPLY_TX;
1721 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001722 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001723
1724 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001725 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
1726
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001727 /*
1728 * Use the first empty entry in this queue's command buffer array
1729 * to contain the Tx command and MAC header concatenated together
1730 * (payload data will be in another buffer).
1731 * Size of this varies, due to varying MAC header length.
1732 * If end is not dword aligned, we'll have 2 extra bytes at the end
1733 * of the MAC header (device reads on dword boundaries).
1734 * We'll tell device about this padding later.
1735 */
Tomas Winkler83d527d2008-05-15 13:54:05 +08001736 len = sizeof(struct iwl_tx_cmd) +
Tomas Winkler857485c2008-03-21 13:53:44 -07001737 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07001738
1739 len_org = len;
1740 len = (len + 3) & ~3;
1741
1742 if (len_org != len)
1743 len_org = 1;
1744 else
1745 len_org = 0;
1746
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001747 /* Physical address of this Tx command's header (not MAC header!),
1748 * within command buffer array. */
Tomas Winkler857485c2008-03-21 13:53:44 -07001749 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
1750 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07001751
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001752 /* Add buffer containing Tx command and MAC(!) header to TFD's
1753 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001754 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07001755
1756 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07001757 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001758
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001759 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1760 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07001761 len = skb->len - hdr_len;
1762 if (len) {
1763 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
1764 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001765 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07001766 }
1767
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001768 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07001769 if (len_org)
1770 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1771
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001772 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07001773 len = (u16)skb->len;
1774 out_cmd->cmd.tx.len = cpu_to_le16(len);
1775
1776 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001777 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07001778
1779 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001780 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001781
Tomas Winkler19758be2008-03-12 16:58:51 -07001782 iwl_update_tx_stats(priv, fc, len);
1783
Tomas Winkler857485c2008-03-21 13:53:44 -07001784 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
Tomas Winkler83d527d2008-05-15 13:54:05 +08001785 offsetof(struct iwl_tx_cmd, scratch);
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08001786 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
1787 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
1788
Zhu Yib481de92007-09-25 17:54:57 -07001789 if (!ieee80211_get_morefrag(hdr)) {
1790 txq->need_update = 1;
Tomas Winkler54dbb522008-05-15 13:54:06 +08001791 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07001792 priv->stations[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07001793 } else {
1794 wait_write_ptr = 1;
1795 txq->need_update = 0;
1796 }
1797
Ester Kummerbf403db2008-05-05 10:22:40 +08001798 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07001799 sizeof(out_cmd->cmd.tx));
1800
Ester Kummerbf403db2008-05-05 10:22:40 +08001801 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07001802 ieee80211_get_hdrlen(fc));
1803
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001804 /* Set up entry for this TFD in Tx byte-count array */
Tomas Winklere2a722e2008-04-14 21:16:10 -07001805 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
Zhu Yib481de92007-09-25 17:54:57 -07001806
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001807 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08001808 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08001809 rc = iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001810 spin_unlock_irqrestore(&priv->lock, flags);
1811
1812 if (rc)
1813 return rc;
1814
Tomas Winkler443cfd42008-05-15 13:53:57 +08001815 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07001816 && priv->mac80211_registered) {
1817 if (wait_write_ptr) {
1818 spin_lock_irqsave(&priv->lock, flags);
1819 txq->need_update = 1;
Tomas Winklerbabcebf2008-05-15 13:54:00 +08001820 iwl_txq_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07001821 spin_unlock_irqrestore(&priv->lock, flags);
1822 }
1823
1824 ieee80211_stop_queue(priv->hw, ctl->queue);
1825 }
1826
1827 return 0;
1828
1829drop_unlock:
1830 spin_unlock_irqrestore(&priv->lock, flags);
1831drop:
1832 return -1;
1833}
1834
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001835static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001836{
Johannes Berg8318d782008-01-24 19:38:38 +01001837 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001838 struct ieee80211_rate *rate;
1839 int i;
1840
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07001841 hw = iwl_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08001842 if (!hw) {
1843 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
1844 return;
1845 }
Zhu Yib481de92007-09-25 17:54:57 -07001846
1847 priv->active_rate = 0;
1848 priv->active_rate_basic = 0;
1849
Johannes Berg8318d782008-01-24 19:38:38 +01001850 for (i = 0; i < hw->n_bitrates; i++) {
1851 rate = &(hw->bitrates[i]);
1852 if (rate->hw_value < IWL_RATE_COUNT)
1853 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07001854 }
1855
1856 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
1857 priv->active_rate, priv->active_rate_basic);
1858
1859 /*
1860 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
1861 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
1862 * OFDM
1863 */
1864 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
1865 priv->staging_rxon.cck_basic_rates =
1866 ((priv->active_rate_basic &
1867 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
1868 else
1869 priv->staging_rxon.cck_basic_rates =
1870 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1871
1872 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
1873 priv->staging_rxon.ofdm_basic_rates =
1874 ((priv->active_rate_basic &
1875 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
1876 IWL_FIRST_OFDM_RATE) & 0xFF;
1877 else
1878 priv->staging_rxon.ofdm_basic_rates =
1879 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1880}
1881
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001882void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07001883{
1884 unsigned long flags;
1885
1886 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1887 return;
1888
1889 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
1890 disable_radio ? "OFF" : "ON");
1891
1892 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001893 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001894 /* FIXME: This is a workaround for AP */
1895 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
1896 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001897 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07001898 CSR_UCODE_SW_BIT_RFKILL);
1899 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001900 /* call the host command only if no hw rf-kill set */
Mohamed Abbas59003832008-04-15 16:01:46 -07001901 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
1902 iwl_is_ready(priv))
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001903 iwl4965_send_card_state(priv,
1904 CARD_STATE_CMD_DISABLE,
1905 0);
Zhu Yib481de92007-09-25 17:54:57 -07001906 set_bit(STATUS_RF_KILL_SW, &priv->status);
Mohamed Abbasad97edd2008-03-28 16:21:06 -07001907
1908 /* make sure mac80211 stop sending Tx frame */
1909 if (priv->mac80211_registered)
1910 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07001911 }
1912 return;
1913 }
1914
1915 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001916 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07001917
1918 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1919 spin_unlock_irqrestore(&priv->lock, flags);
1920
1921 /* wake up ucode */
1922 msleep(10);
1923
1924 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001925 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1926 if (!iwl_grab_nic_access(priv))
1927 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001928 spin_unlock_irqrestore(&priv->lock, flags);
1929
1930 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1931 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
1932 "disabled by HW switch\n");
1933 return;
1934 }
1935
1936 queue_work(priv->workqueue, &priv->restart);
1937 return;
1938}
1939
Zhu Yib481de92007-09-25 17:54:57 -07001940#define IWL_PACKET_RETRY_TIME HZ
1941
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001942int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07001943{
1944 u16 sc = le16_to_cpu(header->seq_ctrl);
1945 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1946 u16 frag = sc & IEEE80211_SCTL_FRAG;
1947 u16 *last_seq, *last_frag;
1948 unsigned long *last_time;
1949
1950 switch (priv->iw_mode) {
1951 case IEEE80211_IF_TYPE_IBSS:{
1952 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001953 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001954 u8 *mac = header->addr2;
1955 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
1956
1957 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001958 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07001959 if (!compare_ether_addr(entry->mac, mac))
1960 break;
1961 }
1962 if (p == &priv->ibss_mac_hash[index]) {
1963 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1964 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08001965 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07001966 return 0;
1967 }
1968 memcpy(entry->mac, mac, ETH_ALEN);
1969 entry->seq_num = seq;
1970 entry->frag_num = frag;
1971 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08001972 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07001973 return 0;
1974 }
1975 last_seq = &entry->seq_num;
1976 last_frag = &entry->frag_num;
1977 last_time = &entry->packet_time;
1978 break;
1979 }
1980 case IEEE80211_IF_TYPE_STA:
1981 last_seq = &priv->last_seq_num;
1982 last_frag = &priv->last_frag_num;
1983 last_time = &priv->last_packet_time;
1984 break;
1985 default:
1986 return 0;
1987 }
1988 if ((*last_seq == seq) &&
1989 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
1990 if (*last_frag == frag)
1991 goto drop;
1992 if (*last_frag + 1 != frag)
1993 /* out-of-order fragment */
1994 goto drop;
1995 } else
1996 *last_seq = seq;
1997
1998 *last_frag = frag;
1999 *last_time = jiffies;
2000 return 0;
2001
2002 drop:
2003 return 1;
2004}
2005
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002006#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002007
2008#include "iwl-spectrum.h"
2009
2010#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2011#define BEACON_TIME_MASK_HIGH 0xFF000000
2012#define TIME_UNIT 1024
2013
2014/*
2015 * extended beacon time format
2016 * time in usec will be changed into a 32-bit value in 8:24 format
2017 * the high 1 byte is the beacon counts
2018 * the lower 3 bytes is the time in usec within one beacon interval
2019 */
2020
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002021static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002022{
2023 u32 quot;
2024 u32 rem;
2025 u32 interval = beacon_interval * 1024;
2026
2027 if (!interval || !usec)
2028 return 0;
2029
2030 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2031 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2032
2033 return (quot << 24) + rem;
2034}
2035
2036/* base is usually what we get from ucode with each received frame,
2037 * the same as HW timer counter counting down
2038 */
2039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002040static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002041{
2042 u32 base_low = base & BEACON_TIME_MASK_LOW;
2043 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2044 u32 interval = beacon_interval * TIME_UNIT;
2045 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2046 (addon & BEACON_TIME_MASK_HIGH);
2047
2048 if (base_low > addon_low)
2049 res += base_low - addon_low;
2050 else if (base_low < addon_low) {
2051 res += interval + base_low - addon_low;
2052 res += (1 << 24);
2053 } else
2054 res += (1 << 24);
2055
2056 return cpu_to_le32(res);
2057}
2058
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002059static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002060 struct ieee80211_measurement_params *params,
2061 u8 type)
2062{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002063 struct iwl4965_spectrum_cmd spectrum;
Tomas Winklerdb11d632008-05-05 10:22:33 +08002064 struct iwl_rx_packet *res;
Tomas Winkler857485c2008-03-21 13:53:44 -07002065 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002066 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2067 .data = (void *)&spectrum,
2068 .meta.flags = CMD_WANT_SKB,
2069 };
2070 u32 add_time = le64_to_cpu(params->start_time);
2071 int rc;
2072 int spectrum_resp_status;
2073 int duration = le16_to_cpu(params->duration);
2074
Tomas Winkler3109ece2008-03-28 16:33:35 -07002075 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002076 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002077 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002078 le64_to_cpu(params->start_time) - priv->last_tsf,
2079 le16_to_cpu(priv->rxon_timing.beacon_interval));
2080
2081 memset(&spectrum, 0, sizeof(spectrum));
2082
2083 spectrum.channel_count = cpu_to_le16(1);
2084 spectrum.flags =
2085 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2086 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2087 cmd.len = sizeof(spectrum);
2088 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2089
Tomas Winkler3109ece2008-03-28 16:33:35 -07002090 if (iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002091 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002092 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002093 add_time,
2094 le16_to_cpu(priv->rxon_timing.beacon_interval));
2095 else
2096 spectrum.start_time = 0;
2097
2098 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2099 spectrum.channels[0].channel = params->channel;
2100 spectrum.channels[0].type = type;
2101 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2102 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2103 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2104
Tomas Winkler857485c2008-03-21 13:53:44 -07002105 rc = iwl_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002106 if (rc)
2107 return rc;
2108
Tomas Winklerdb11d632008-05-05 10:22:33 +08002109 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002110 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2111 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2112 rc = -EIO;
2113 }
2114
2115 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2116 switch (spectrum_resp_status) {
2117 case 0: /* Command will be handled */
2118 if (res->u.spectrum.id != 0xff) {
2119 IWL_DEBUG_INFO
2120 ("Replaced existing measurement: %d\n",
2121 res->u.spectrum.id);
2122 priv->measurement_status &= ~MEASUREMENT_READY;
2123 }
2124 priv->measurement_status |= MEASUREMENT_ACTIVE;
2125 rc = 0;
2126 break;
2127
2128 case 1: /* Command will not be handled */
2129 rc = -EAGAIN;
2130 break;
2131 }
2132
2133 dev_kfree_skb_any(cmd.meta.u.skb);
2134
2135 return rc;
2136}
2137#endif
2138
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002139static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Tomas Winkler8567c632008-05-15 13:53:58 +08002140 struct iwl_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002141{
2142
2143 tx_sta->status.ack_signal = 0;
2144 tx_sta->status.excessive_retries = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002145
2146 if (in_interrupt())
2147 ieee80211_tx_status_irqsafe(priv->hw,
2148 tx_sta->skb[0], &(tx_sta->status));
2149 else
2150 ieee80211_tx_status(priv->hw,
2151 tx_sta->skb[0], &(tx_sta->status));
2152
2153 tx_sta->skb[0] = NULL;
2154}
2155
2156/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002157 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07002158 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002159 * When FW advances 'R' index, all entries between old and new 'R' index
2160 * need to be reclaimed. As result, some free space forms. If there is
2161 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07002162 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002163int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07002164{
Ron Rindjunsky16466902008-05-05 10:22:50 +08002165 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Tomas Winkler443cfd42008-05-15 13:53:57 +08002166 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07002167 int nfreed = 0;
2168
Tomas Winkler443cfd42008-05-15 13:53:57 +08002169 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07002170 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2171 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002172 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002173 return 0;
2174 }
2175
Tomas Winklerc54b6792008-03-06 17:36:53 -08002176 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002177 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002178 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07002179 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002180 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002181 &(txq->txb[txq->q.read_ptr]));
Ron Rindjunsky1053d352008-05-05 10:22:43 +08002182 iwl_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002183 } else if (nfreed > 1) {
2184 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002185 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002186 queue_work(priv->workqueue, &priv->restart);
2187 }
2188 nfreed++;
2189 }
2190
Zhu Yib481de92007-09-25 17:54:57 -07002191 return nfreed;
2192}
2193
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002194static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002195{
2196 status &= TX_STATUS_MSK;
2197 return (status == TX_STATUS_SUCCESS)
2198 || (status == TX_STATUS_DIRECT_DONE);
2199}
2200
2201/******************************************************************************
2202 *
2203 * Generic RX handler implementations
2204 *
2205 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002206#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002207
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002208static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002209 struct ieee80211_hdr *hdr)
2210{
2211 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2212 return IWL_AP_ID;
2213 else {
2214 u8 *da = ieee80211_get_DA(hdr);
Tomas Winkler947b13a2008-04-16 16:34:48 -07002215 return iwl_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07002216 }
2217}
2218
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002219static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002220 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07002221{
2222 if (priv->txq[txq_id].txb[idx].skb[0])
2223 return (struct ieee80211_hdr *)priv->txq[txq_id].
2224 txb[idx].skb[0]->data;
2225 return NULL;
2226}
2227
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002228static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07002229{
2230 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2231 tx_resp->frame_count);
2232 return le32_to_cpu(*scd_ssn) & MAX_SN;
2233
2234}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002235
2236/**
2237 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2238 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002239static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Tomas Winkler6def9762008-05-05 10:22:31 +08002240 struct iwl_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002241 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07002242 u16 start_idx)
2243{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002244 u16 status;
2245 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07002246 struct ieee80211_tx_status *tx_status = NULL;
2247 struct ieee80211_hdr *hdr = NULL;
2248 int i, sh;
2249 int txq_id, idx;
2250 u16 seq;
2251
2252 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002253 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07002254
2255 agg->frame_count = tx_resp->frame_count;
2256 agg->start_idx = start_idx;
2257 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002258 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002259
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002260 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07002261 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002262 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002263 status = le16_to_cpu(frame_status[0].status);
2264 seq = le16_to_cpu(frame_status[0].sequence);
2265 idx = SEQ_TO_INDEX(seq);
2266 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07002267
Zhu Yib481de92007-09-25 17:54:57 -07002268 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002269 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2270 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002271
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002272 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07002273 tx_status->retry_count = tx_resp->failure_frame;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002274 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002275 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07002276 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002277 iwl4965_hwrate_to_tx_control(priv,
2278 le32_to_cpu(tx_resp->rate_n_flags),
2279 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002280 /* FIXME: code repetition end */
2281
2282 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2283 status & 0xff, tx_resp->failure_frame);
2284 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002285 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07002286
2287 agg->wait_for_ba = 0;
2288 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002289 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07002290 u64 bitmap = 0;
2291 int start = agg->start_idx;
2292
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002293 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07002294 for (i = 0; i < agg->frame_count; i++) {
2295 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002296 status = le16_to_cpu(frame_status[i].status);
2297 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07002298 idx = SEQ_TO_INDEX(seq);
2299 txq_id = SEQ_TO_QUEUE(seq);
2300
2301 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2302 AGG_TX_STATE_ABORT_MSK))
2303 continue;
2304
2305 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2306 agg->frame_count, txq_id, idx);
2307
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002308 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07002309
2310 sc = le16_to_cpu(hdr->seq_ctrl);
2311 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2312 IWL_ERROR("BUG_ON idx doesn't match seq control"
2313 " idx=%d, seq_idx=%d, seq=%d\n",
2314 idx, SEQ_TO_SN(sc),
2315 hdr->seq_ctrl);
2316 return -1;
2317 }
2318
2319 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2320 i, idx, SEQ_TO_SN(sc));
2321
2322 sh = idx - start;
2323 if (sh > 64) {
2324 sh = (start - idx) + 0xff;
2325 bitmap = bitmap << sh;
2326 sh = 0;
2327 start = idx;
2328 } else if (sh < -64)
2329 sh = 0xff - (start - idx);
2330 else if (sh < 0) {
2331 sh = start - idx;
2332 start = idx;
2333 bitmap = bitmap << sh;
2334 sh = 0;
2335 }
2336 bitmap |= (1 << sh);
2337 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2338 start, (u32)(bitmap & 0xFFFFFFFF));
2339 }
2340
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002341 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07002342 agg->start_idx = start;
2343 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002344 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07002345 agg->frame_count, agg->start_idx,
John W. Linville06501d22008-04-01 17:38:47 -04002346 (unsigned long long)agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07002347
2348 if (bitmap)
2349 agg->wait_for_ba = 1;
2350 }
2351 return 0;
2352}
2353#endif
Zhu Yib481de92007-09-25 17:54:57 -07002354
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002355/**
2356 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2357 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002358static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002359 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002360{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002361 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002362 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2363 int txq_id = SEQ_TO_QUEUE(sequence);
2364 int index = SEQ_TO_INDEX(sequence);
Ron Rindjunsky16466902008-05-05 10:22:50 +08002365 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002366 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002367 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07002368 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002369#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002370 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002371 u16 fc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002372 struct ieee80211_hdr *hdr;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002373 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002374#endif
2375
Tomas Winkler443cfd42008-05-15 13:53:57 +08002376 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
Zhu Yib481de92007-09-25 17:54:57 -07002377 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2378 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002379 index, txq->q.n_bd, txq->q.write_ptr,
2380 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07002381 return;
2382 }
2383
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002384#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002385 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002386 fc = le16_to_cpu(hdr->frame_control);
2387 if (ieee80211_is_qos_data(fc)) {
2388 qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
2389 tid = qc[0] & 0xf;
2390 }
Zhu Yib481de92007-09-25 17:54:57 -07002391
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002392 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2393 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2394 IWL_ERROR("Station not known\n");
2395 return;
2396 }
2397
2398 if (txq->sched_retry) {
2399 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Tomas Winkler6def9762008-05-05 10:22:31 +08002400 struct iwl_ht_agg *agg = NULL;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002401
2402 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07002403 return;
Zhu Yib481de92007-09-25 17:54:57 -07002404
2405 agg = &priv->stations[sta_id].tid[tid].agg;
2406
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002407 iwl4965_tx_status_reply_tx(priv, agg,
2408 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07002409
2410 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002411 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07002412 /* TODO: send BAR */
2413 }
2414
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002415 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002416 int freed, ampdu_q;
Tomas Winklerc54b6792008-03-06 17:36:53 -08002417 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07002418 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2419 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002420 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2421 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2422
Tomas Winkler443cfd42008-05-15 13:53:57 +08002423 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002424 txq_id >= 0 && priv->mac80211_registered &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002425 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
2426 /* calculate mac80211 ampdu sw queue to wake */
2427 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
2428 priv->hw->queues;
2429 if (agg->state == IWL_AGG_OFF)
2430 ieee80211_wake_queue(priv->hw, txq_id);
2431 else
2432 ieee80211_wake_queue(priv->hw, ampdu_q);
2433 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002434 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002435 }
2436 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002437#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002438 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07002439
2440 tx_status->retry_count = tx_resp->failure_frame;
Zhu Yib481de92007-09-25 17:54:57 -07002441 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002442 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08002443 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2444 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07002445
Zhu Yib481de92007-09-25 17:54:57 -07002446 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002447 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07002448 status, le32_to_cpu(tx_resp->rate_n_flags),
2449 tx_resp->failure_frame);
2450
2451 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Tomas Winkler47c51962008-05-05 10:22:41 +08002452#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002453 if (index != -1) {
2454 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002455 if (tid != MAX_TID_COUNT)
2456 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
Tomas Winkler443cfd42008-05-15 13:53:57 +08002457 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
Ron Rindjunsky0d0b2c12008-05-04 14:48:18 +03002458 (txq_id >= 0) && priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002459 ieee80211_wake_queue(priv->hw, txq_id);
2460 if (tid != MAX_TID_COUNT)
2461 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07002462 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002463 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002464#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002465
2466 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2467 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2468}
2469
2470
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002471static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002472 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002473{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002474 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002475 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002476 struct delayed_work *pwork;
2477
2478 palive = &pkt->u.alive_frame;
2479
2480 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2481 "0x%01X 0x%01X\n",
2482 palive->is_valid, palive->ver_type,
2483 palive->ver_subtype);
2484
2485 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2486 IWL_DEBUG_INFO("Initialization Alive received.\n");
2487 memcpy(&priv->card_alive_init,
2488 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002489 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002490 pwork = &priv->init_alive_start;
2491 } else {
2492 IWL_DEBUG_INFO("Runtime Alive received.\n");
2493 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002494 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002495 pwork = &priv->alive_start;
2496 }
2497
2498 /* We delay the ALIVE response by 5ms to
2499 * give the HW RF Kill time to activate... */
2500 if (palive->is_valid == UCODE_VALID_OK)
2501 queue_delayed_work(priv->workqueue, pwork,
2502 msecs_to_jiffies(5));
2503 else
2504 IWL_WARNING("uCode did not respond OK.\n");
2505}
2506
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002507static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002508 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002509{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002510 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002511
2512 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2513 return;
2514}
2515
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002516static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002517 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002518{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002519 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002520
2521 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2522 "seq 0x%04X ser 0x%08X\n",
2523 le32_to_cpu(pkt->u.err_resp.error_type),
2524 get_cmd_string(pkt->u.err_resp.cmd_id),
2525 pkt->u.err_resp.cmd_id,
2526 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2527 le32_to_cpu(pkt->u.err_resp.error_info));
2528}
2529
2530#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2531
Tomas Winklera55360e2008-05-05 10:22:28 +08002532static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002533{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002534 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08002535 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002536 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002537 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2538 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2539 rxon->channel = csa->channel;
2540 priv->staging_rxon.channel = csa->channel;
2541}
2542
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002543static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002544 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002545{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002546#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Tomas Winklerdb11d632008-05-05 10:22:33 +08002547 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002548 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002549
2550 if (!report->state) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002551 IWL_DEBUG(IWL_DL_11H,
2552 "Spectrum Measure Notification: Start\n");
Zhu Yib481de92007-09-25 17:54:57 -07002553 return;
2554 }
2555
2556 memcpy(&priv->measure_report, report, sizeof(*report));
2557 priv->measurement_status |= MEASUREMENT_READY;
2558#endif
2559}
2560
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002561static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002562 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002563{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002564#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002565 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002566 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002567 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2568 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2569#endif
2570}
2571
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002572static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002573 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002574{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002575 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002576 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2577 "notification for %s:\n",
2578 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Ester Kummerbf403db2008-05-05 10:22:40 +08002579 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002580}
2581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002582static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002583{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002584 struct iwl_priv *priv =
2585 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002586 struct sk_buff *beacon;
2587
2588 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01002589 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002590
2591 if (!beacon) {
2592 IWL_ERROR("update beacon failed\n");
2593 return;
2594 }
2595
2596 mutex_lock(&priv->mutex);
2597 /* new beacon skb is allocated every time; dispose previous.*/
2598 if (priv->ibss_beacon)
2599 dev_kfree_skb(priv->ibss_beacon);
2600
2601 priv->ibss_beacon = beacon;
2602 mutex_unlock(&priv->mutex);
2603
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002604 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002605}
2606
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002607static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002608 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002609{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002610#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002611 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002612 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2613 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002614
2615 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2616 "tsf %d %d rate %d\n",
2617 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2618 beacon->beacon_notify_hdr.failure_frame,
2619 le32_to_cpu(beacon->ibss_mgr_status),
2620 le32_to_cpu(beacon->high_tsf),
2621 le32_to_cpu(beacon->low_tsf), rate);
2622#endif
2623
2624 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2625 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2626 queue_work(priv->workqueue, &priv->beacon_update);
2627}
2628
2629/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002630static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002631 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002632{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002633#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winklerdb11d632008-05-05 10:22:33 +08002634 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002635 struct iwl4965_scanreq_notification *notif =
2636 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002637
2638 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2639#endif
2640}
2641
2642/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002643static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002644 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002645{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002646 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002647 struct iwl4965_scanstart_notification *notif =
2648 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002649 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2650 IWL_DEBUG_SCAN("Scan start: "
2651 "%d [802.11%s] "
2652 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2653 notif->channel,
2654 notif->band ? "bg" : "a",
2655 notif->tsf_high,
2656 notif->tsf_low, notif->status, notif->beacon_timer);
2657}
2658
2659/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002660static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002661 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002662{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002663 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002664 struct iwl4965_scanresults_notification *notif =
2665 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002666
2667 IWL_DEBUG_SCAN("Scan ch.res: "
2668 "%d [802.11%s] "
2669 "(TSF: 0x%08X:%08X) - %d "
2670 "elapsed=%lu usec (%dms since last)\n",
2671 notif->channel,
2672 notif->band ? "bg" : "a",
2673 le32_to_cpu(notif->tsf_high),
2674 le32_to_cpu(notif->tsf_low),
2675 le32_to_cpu(notif->statistics[0]),
2676 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2677 jiffies_to_msecs(elapsed_jiffies
2678 (priv->last_scan_jiffies, jiffies)));
2679
2680 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002681 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002682}
2683
2684/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002685static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002686 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002687{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002688 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002689 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002690
2691 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2692 scan_notif->scanned_channels,
2693 scan_notif->tsf_low,
2694 scan_notif->tsf_high, scan_notif->status);
2695
2696 /* The HW is no longer scanning */
2697 clear_bit(STATUS_SCAN_HW, &priv->status);
2698
2699 /* The scan completion notification came in, so kill that timer... */
2700 cancel_delayed_work(&priv->scan_check);
2701
2702 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
2703 (priv->scan_bands == 2) ? "2.4" : "5.2",
2704 jiffies_to_msecs(elapsed_jiffies
2705 (priv->scan_pass_start, jiffies)));
2706
2707 /* Remove this scanned band from the list
2708 * of pending bands to scan */
2709 priv->scan_bands--;
2710
2711 /* If a request to abort was given, or the scan did not succeed
2712 * then we reset the scan state machine and terminate,
2713 * re-queuing another scan if one has been requested */
2714 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2715 IWL_DEBUG_INFO("Aborted scan completed.\n");
2716 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2717 } else {
2718 /* If there are more bands on this scan pass reschedule */
2719 if (priv->scan_bands > 0)
2720 goto reschedule;
2721 }
2722
2723 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002724 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002725 IWL_DEBUG_INFO("Setting scan to off\n");
2726
2727 clear_bit(STATUS_SCANNING, &priv->status);
2728
2729 IWL_DEBUG_INFO("Scan took %dms\n",
2730 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2731
2732 queue_work(priv->workqueue, &priv->scan_completed);
2733
2734 return;
2735
2736reschedule:
2737 priv->scan_pass_start = jiffies;
2738 queue_work(priv->workqueue, &priv->request_scan);
2739}
2740
2741/* Handle notification from uCode that card's power state is changing
2742 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002743static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002744 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002745{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002746 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002747 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2748 unsigned long status = priv->status;
2749
2750 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2751 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2752 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2753
2754 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
2755 RF_CARD_DISABLED)) {
2756
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002757 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002758 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2759
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002760 if (!iwl_grab_nic_access(priv)) {
2761 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002762 priv, HBUS_TARG_MBX_C,
2763 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2764
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002765 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002766 }
2767
2768 if (!(flags & RXON_CARD_DISABLED)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002769 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07002770 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002771 if (!iwl_grab_nic_access(priv)) {
2772 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002773 priv, HBUS_TARG_MBX_C,
2774 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2775
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002776 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002777 }
2778 }
2779
2780 if (flags & RF_CARD_DISABLED) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002781 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002782 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002783 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2784 if (!iwl_grab_nic_access(priv))
2785 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002786 }
2787 }
2788
2789 if (flags & HW_CARD_DISABLED)
2790 set_bit(STATUS_RF_KILL_HW, &priv->status);
2791 else
2792 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2793
2794
2795 if (flags & SW_CARD_DISABLED)
2796 set_bit(STATUS_RF_KILL_SW, &priv->status);
2797 else
2798 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2799
2800 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002801 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002802
2803 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2804 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2805 (test_bit(STATUS_RF_KILL_SW, &status) !=
2806 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2807 queue_work(priv->workqueue, &priv->rf_kill);
2808 else
2809 wake_up_interruptible(&priv->wait_command_queue);
2810}
2811
2812/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002813 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07002814 *
2815 * Setup the RX handlers for each of the reply types sent from the uCode
2816 * to the host.
2817 *
2818 * This function chains into the hardware specific files for them to setup
2819 * any hardware specific handlers as well.
2820 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002821static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002822{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002823 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
2824 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
2825 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
2826 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07002827 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002828 iwl4965_rx_spectrum_measure_notif;
2829 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002830 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002831 iwl4965_rx_pm_debug_statistics_notif;
2832 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002833
Ben Cahill9fbab512007-11-29 11:09:47 +08002834 /*
2835 * The same handler is used for both the REPLY to a discrete
2836 * statistics request from the host as well as for the periodic
2837 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07002838 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002839 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
2840 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07002841
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002842 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
2843 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002844 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002845 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07002846 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002847 iwl4965_rx_scan_complete_notif;
2848 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
2849 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07002850
Ben Cahill9fbab512007-11-29 11:09:47 +08002851 /* Set up hardware specific Rx handlers */
Emmanuel Grumbachd4789ef2008-04-24 11:55:20 -07002852 priv->cfg->ops->lib->rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002853}
2854
2855/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002856 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07002857 * @rxb: Rx buffer to reclaim
2858 *
2859 * If an Rx buffer has an async callback associated with it the callback
2860 * will be executed. The attached skb (if present) will only be freed
2861 * if the callback returns 1
2862 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002863static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Tomas Winklera55360e2008-05-05 10:22:28 +08002864 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002865{
Tomas Winklerdb11d632008-05-05 10:22:33 +08002866 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002867 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2868 int txq_id = SEQ_TO_QUEUE(sequence);
2869 int index = SEQ_TO_INDEX(sequence);
2870 int huge = sequence & SEQ_HUGE_FRAME;
2871 int cmd_index;
Tomas Winkler857485c2008-03-21 13:53:44 -07002872 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002873
2874 /* If a Tx command is being handled and it isn't in the actual
2875 * command queue then there a command routing bug has been introduced
2876 * in the queue management code. */
2877 if (txq_id != IWL_CMD_QUEUE_NUM)
2878 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
2879 txq_id, pkt->hdr.cmd);
2880 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
2881
2882 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
2883 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
2884
2885 /* Input error checking is done when commands are added to queue. */
2886 if (cmd->meta.flags & CMD_WANT_SKB) {
2887 cmd->meta.source->u.skb = rxb->skb;
2888 rxb->skb = NULL;
2889 } else if (cmd->meta.u.callback &&
2890 !cmd->meta.u.callback(priv, cmd, rxb->skb))
2891 rxb->skb = NULL;
2892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002893 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07002894
2895 if (!(cmd->meta.flags & CMD_ASYNC)) {
2896 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2897 wake_up_interruptible(&priv->wait_command_queue);
2898 }
2899}
2900
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002901/*
2902 * this should be called while priv->lock is locked
2903*/
Tomas Winklera55360e2008-05-05 10:22:28 +08002904static void __iwl_rx_replenish(struct iwl_priv *priv)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002905{
Tomas Winklera55360e2008-05-05 10:22:28 +08002906 iwl_rx_allocate(priv);
2907 iwl_rx_queue_restock(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002908}
2909
2910
Zhu Yib481de92007-09-25 17:54:57 -07002911/**
Tomas Winklera55360e2008-05-05 10:22:28 +08002912 * iwl_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07002913 *
2914 * Uses the priv->rx_handlers callback function array to invoke
2915 * the appropriate handlers, including command responses,
2916 * frame-received notifications, and other notifications.
2917 */
Tomas Winklera55360e2008-05-05 10:22:28 +08002918void iwl_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002919{
Tomas Winklera55360e2008-05-05 10:22:28 +08002920 struct iwl_rx_mem_buffer *rxb;
Tomas Winklerdb11d632008-05-05 10:22:33 +08002921 struct iwl_rx_packet *pkt;
Tomas Winklera55360e2008-05-05 10:22:28 +08002922 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07002923 u32 r, i;
2924 int reclaim;
2925 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002926 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08002927 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07002928
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002929 /* uCode's read index (stored in shared DRAM) indicates the last Rx
2930 * buffer that the driver may process (last buffer filled by ucode). */
Ron Rindjunskyd67f5482008-05-05 10:22:49 +08002931 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002932 i = rxq->read;
2933
2934 /* Rx interrupt, but nothing sent from uCode */
2935 if (i == r)
Ester Kummerf3d67992008-05-06 11:05:12 +08002936 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
Zhu Yib481de92007-09-25 17:54:57 -07002937
Tomas Winklera55360e2008-05-05 10:22:28 +08002938 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08002939 fill_rx = 1;
2940
Zhu Yib481de92007-09-25 17:54:57 -07002941 while (i != r) {
2942 rxb = rxq->queue[i];
2943
Ben Cahill9fbab512007-11-29 11:09:47 +08002944 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07002945 * then a bug has been introduced in the queue refilling
2946 * routines -- catch it here */
2947 BUG_ON(rxb == NULL);
2948
2949 rxq->queue[i] = NULL;
2950
2951 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07002952 priv->hw_params.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07002953 PCI_DMA_FROMDEVICE);
Tomas Winklerdb11d632008-05-05 10:22:33 +08002954 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002955
2956 /* Reclaim a command buffer only if this packet is a response
2957 * to a (driver-originated) command.
2958 * If the packet (e.g. Rx frame) originated from uCode,
2959 * there is no command buffer to reclaim.
2960 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
2961 * but apparently a few don't get set; catch them here. */
2962 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
2963 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
Tomas Winkler857485c2008-03-21 13:53:44 -07002964 (pkt->hdr.cmd != REPLY_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08002965 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07002966 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
2967 (pkt->hdr.cmd != REPLY_TX);
2968
2969 /* Based on type of command response or notification,
2970 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002971 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07002972 if (priv->rx_handlers[pkt->hdr.cmd]) {
Ester Kummerf3d67992008-05-06 11:05:12 +08002973 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
2974 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002975 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2976 } else {
2977 /* No handling needed */
Ester Kummerf3d67992008-05-06 11:05:12 +08002978 IWL_DEBUG(IWL_DL_RX,
Zhu Yib481de92007-09-25 17:54:57 -07002979 "r %d i %d No handler needed for %s, 0x%02x\n",
2980 r, i, get_cmd_string(pkt->hdr.cmd),
2981 pkt->hdr.cmd);
2982 }
2983
2984 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08002985 /* Invoke any callbacks, transfer the skb to caller, and
Tomas Winkler857485c2008-03-21 13:53:44 -07002986 * fire off the (possibly) blocking iwl_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07002987 * as we reclaim the driver command queue */
2988 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002989 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07002990 else
2991 IWL_WARNING("Claim null rxb?\n");
2992 }
2993
2994 /* For now we just don't re-use anything. We can tweak this
2995 * later to try and re-use notification packets and SKBs that
2996 * fail to Rx correctly */
2997 if (rxb->skb != NULL) {
2998 priv->alloc_rxb_skb--;
2999 dev_kfree_skb_any(rxb->skb);
3000 rxb->skb = NULL;
3001 }
3002
3003 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Tomas Winkler5425e492008-04-15 16:01:38 -07003004 priv->hw_params.rx_buf_size,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003005 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07003006 spin_lock_irqsave(&rxq->lock, flags);
3007 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3008 spin_unlock_irqrestore(&rxq->lock, flags);
3009 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003010 /* If there are a lot of unused frames,
3011 * restock the Rx queue so ucode wont assert. */
3012 if (fill_rx) {
3013 count++;
3014 if (count >= 8) {
3015 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003016 __iwl_rx_replenish(priv);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003017 count = 0;
3018 }
3019 }
Zhu Yib481de92007-09-25 17:54:57 -07003020 }
3021
3022 /* Backtrack one entry */
3023 priv->rxq.read = i;
Tomas Winklera55360e2008-05-05 10:22:28 +08003024 iwl_rx_queue_restock(priv);
3025}
3026/* Convert linear signal-to-noise ratio into dB */
3027static u8 ratio2dB[100] = {
3028/* 0 1 2 3 4 5 6 7 8 9 */
3029 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3030 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3031 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3032 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3033 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3034 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3035 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3036 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3037 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3038 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3039};
3040
3041/* Calculates a relative dB value from a ratio of linear
3042 * (i.e. not dB) signal levels.
3043 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
3044int iwl4965_calc_db_from_ratio(int sig_ratio)
3045{
3046 /* 1000:1 or higher just report as 60 dB */
3047 if (sig_ratio >= 1000)
3048 return 60;
3049
3050 /* 100:1 or higher, divide by 10 and use table,
3051 * add 20 dB to make up for divide by 10 */
3052 if (sig_ratio >= 100)
3053 return (20 + (int)ratio2dB[sig_ratio/10]);
3054
3055 /* We shouldn't see this */
3056 if (sig_ratio < 1)
3057 return 0;
3058
3059 /* Use table for ratios 1:1 - 99:1 */
3060 return (int)ratio2dB[sig_ratio];
3061}
3062
3063#define PERFECT_RSSI (-20) /* dBm */
3064#define WORST_RSSI (-95) /* dBm */
3065#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3066
3067/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3068 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3069 * about formulas used below. */
3070int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
3071{
3072 int sig_qual;
3073 int degradation = PERFECT_RSSI - rssi_dbm;
3074
3075 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3076 * as indicator; formula is (signal dbm - noise dbm).
3077 * SNR at or above 40 is a great signal (100%).
3078 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3079 * Weakest usable signal is usually 10 - 15 dB SNR. */
3080 if (noise_dbm) {
3081 if (rssi_dbm - noise_dbm >= 40)
3082 return 100;
3083 else if (rssi_dbm < noise_dbm)
3084 return 0;
3085 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3086
3087 /* Else use just the signal level.
3088 * This formula is a least squares fit of data points collected and
3089 * compared with a reference system that had a percentage (%) display
3090 * for signal quality. */
3091 } else
3092 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3093 (15 * RSSI_RANGE + 62 * degradation)) /
3094 (RSSI_RANGE * RSSI_RANGE);
3095
3096 if (sig_qual > 100)
3097 sig_qual = 100;
3098 else if (sig_qual < 1)
3099 sig_qual = 0;
3100
3101 return sig_qual;
Zhu Yib481de92007-09-25 17:54:57 -07003102}
3103
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003104/**
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003105 * iwl_txq_update_write_ptr - Send new write index to hardware
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003106 */
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003107static int iwl_txq_update_write_ptr(struct iwl_priv *priv,
Ron Rindjunsky16466902008-05-05 10:22:50 +08003108 struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003109{
3110 u32 reg = 0;
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003111 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003112 int txq_id = txq->q.id;
3113
3114 if (txq->need_update == 0)
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003115 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003116
3117 /* if we're trying to save power */
3118 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3119 /* wake up nic if it's powered down ...
3120 * uCode will wake up, and interrupt us again, so next
3121 * time we'll skip this part. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003122 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003123
3124 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3125 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003126 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003127 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003128 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003129 }
3130
3131 /* restore this queue's parameters in nic hardware. */
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003132 ret = iwl_grab_nic_access(priv);
3133 if (ret)
3134 return ret;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003135 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003136 txq->q.write_ptr | (txq_id << 8));
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003137 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003138
3139 /* else not in power-save mode, uCode will never sleep when we're
3140 * trying to tx (during RFKILL, we're not trying to tx). */
3141 } else
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003142 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003143 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003144
3145 txq->need_update = 0;
3146
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003147 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003148}
3149
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003150#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003151static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003152{
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08003153 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07003154 DECLARE_MAC_BUF(mac);
3155
Zhu Yib481de92007-09-25 17:54:57 -07003156 IWL_DEBUG_RADIO("RX CONFIG:\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08003157 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003158 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3159 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3160 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3161 le32_to_cpu(rxon->filter_flags));
3162 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3163 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3164 rxon->ofdm_basic_rates);
3165 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07003166 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3167 print_mac(mac, rxon->node_addr));
3168 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3169 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07003170 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3171}
3172#endif
3173
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003174static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003175{
3176 IWL_DEBUG_ISR("Enabling interrupts\n");
3177 set_bit(STATUS_INT_ENABLED, &priv->status);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003178 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003179}
3180
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003181/* call this function to flush any scheduled tasklet */
3182static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3183{
3184 /* wait to make sure we flush pedding tasklet*/
3185 synchronize_irq(priv->pci_dev->irq);
3186 tasklet_kill(&priv->irq_tasklet);
3187}
3188
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003189static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003190{
3191 clear_bit(STATUS_INT_ENABLED, &priv->status);
3192
3193 /* disable interrupts from uCode/NIC to host */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003194 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003195
3196 /* acknowledge/clear/reset any interrupts still pending
3197 * from uCode or flow handler (Rx/Tx DMA) */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003198 iwl_write32(priv, CSR_INT, 0xffffffff);
3199 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003200 IWL_DEBUG_ISR("Disabled interrupts\n");
3201}
3202
3203static const char *desc_lookup(int i)
3204{
3205 switch (i) {
3206 case 1:
3207 return "FAIL";
3208 case 2:
3209 return "BAD_PARAM";
3210 case 3:
3211 return "BAD_CHECKSUM";
3212 case 4:
3213 return "NMI_INTERRUPT";
3214 case 5:
3215 return "SYSASSERT";
3216 case 6:
3217 return "FATAL_ERROR";
3218 }
3219
3220 return "UNKNOWN";
3221}
3222
3223#define ERROR_START_OFFSET (1 * sizeof(u32))
3224#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3225
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003226static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003227{
3228 u32 data2, line;
3229 u32 desc, time, count, base, data1;
3230 u32 blink1, blink2, ilink1, ilink2;
3231 int rc;
3232
3233 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3234
Tomas Winkler57aab752008-04-14 21:16:03 -07003235 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003236 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3237 return;
3238 }
3239
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003240 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003241 if (rc) {
3242 IWL_WARNING("Can not read from adapter at this time.\n");
3243 return;
3244 }
3245
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003246 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003247
3248 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3249 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02003250 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003251 }
3252
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003253 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3254 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3255 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3256 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3257 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3258 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3259 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3260 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3261 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003262
3263 IWL_ERROR("Desc Time "
3264 "data1 data2 line\n");
3265 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3266 desc_lookup(desc), desc, time, data1, data2, line);
3267 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3268 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3269 ilink1, ilink2);
3270
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003271 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003272}
3273
3274#define EVENT_START_OFFSET (4 * sizeof(u32))
3275
3276/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003277 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003278 *
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003279 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003280 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003281static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003282 u32 num_events, u32 mode)
3283{
3284 u32 i;
3285 u32 base; /* SRAM byte address of event log header */
3286 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3287 u32 ptr; /* SRAM byte address of log data */
3288 u32 ev, time, data; /* event log data */
3289
3290 if (num_events == 0)
3291 return;
3292
3293 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3294
3295 if (mode == 0)
3296 event_size = 2 * sizeof(u32);
3297 else
3298 event_size = 3 * sizeof(u32);
3299
3300 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3301
3302 /* "time" is actually "data" for mode 0 (no timestamp).
3303 * place event id # at far right for easier visual parsing. */
3304 for (i = 0; i < num_events; i++) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003305 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003306 ptr += sizeof(u32);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003307 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003308 ptr += sizeof(u32);
3309 if (mode == 0)
3310 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
3311 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003312 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003313 ptr += sizeof(u32);
3314 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
3315 }
3316 }
3317}
3318
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003319static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003320{
3321 int rc;
3322 u32 base; /* SRAM byte address of event log header */
3323 u32 capacity; /* event log capacity in # entries */
3324 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3325 u32 num_wraps; /* # times uCode wrapped to top of log */
3326 u32 next_entry; /* index of next entry to be written by uCode */
3327 u32 size; /* # entries that we'll print */
3328
3329 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Tomas Winkler57aab752008-04-14 21:16:03 -07003330 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07003331 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
3332 return;
3333 }
3334
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003335 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003336 if (rc) {
3337 IWL_WARNING("Can not read from adapter at this time.\n");
3338 return;
3339 }
3340
3341 /* event log header */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003342 capacity = iwl_read_targ_mem(priv, base);
3343 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3344 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3345 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003346
3347 size = num_wraps ? capacity : next_entry;
3348
3349 /* bail out if nothing in log */
3350 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08003351 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003352 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003353 return;
3354 }
3355
Zhu Yi583fab32007-09-27 11:27:30 +08003356 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003357 size, num_wraps);
3358
3359 /* if uCode has wrapped back to top of log, start at the oldest entry,
3360 * i.e the next one that uCode would fill. */
3361 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003362 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003363 capacity - next_entry, mode);
3364
3365 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003366 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003367
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003368 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003369}
3370
3371/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003372 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003373 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003374static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003375{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003376 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07003377 set_bit(STATUS_FW_ERROR, &priv->status);
3378
3379 /* Cancel currently queued command. */
3380 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3381
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003382#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003383 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003384 iwl4965_dump_nic_error_log(priv);
3385 iwl4965_dump_nic_event_log(priv);
Ester Kummerbf403db2008-05-05 10:22:40 +08003386 iwl4965_print_rx_config_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003387 }
3388#endif
3389
3390 wake_up_interruptible(&priv->wait_command_queue);
3391
3392 /* Keep the restart process from trying to send host
3393 * commands by clearing the INIT status bit */
3394 clear_bit(STATUS_READY, &priv->status);
3395
3396 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08003397 IWL_DEBUG(IWL_DL_FW_ERRORS,
Zhu Yib481de92007-09-25 17:54:57 -07003398 "Restarting adapter due to uCode error.\n");
3399
Tomas Winkler3109ece2008-03-28 16:33:35 -07003400 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003401 memcpy(&priv->recovery_rxon, &priv->active_rxon,
3402 sizeof(priv->recovery_rxon));
3403 priv->error_recovering = 1;
3404 }
Ester Kummer3a1081e2008-05-06 11:05:14 +08003405 if (priv->cfg->mod_params->restart_fw)
3406 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07003407 }
3408}
3409
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003410static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003411{
3412 unsigned long flags;
3413
3414 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
3415 sizeof(priv->staging_rxon));
3416 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003417 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003418
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08003419 iwl_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07003420
3421 spin_lock_irqsave(&priv->lock, flags);
3422 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
3423 priv->error_recovering = 0;
3424 spin_unlock_irqrestore(&priv->lock, flags);
3425}
3426
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003427static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003428{
3429 u32 inta, handled = 0;
3430 u32 inta_fh;
3431 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003432#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003433 u32 inta_mask;
3434#endif
3435
3436 spin_lock_irqsave(&priv->lock, flags);
3437
3438 /* Ack/clear/reset pending uCode interrupts.
3439 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3440 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003441 inta = iwl_read32(priv, CSR_INT);
3442 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003443
3444 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3445 * Any new interrupts that happen after this, either while we're
3446 * in this tasklet, or later, will show up in next ISR/tasklet. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003447 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3448 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003449
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003450#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003451 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003452 /* just for debug */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003453 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003454 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3455 inta, inta_mask, inta_fh);
3456 }
3457#endif
3458
3459 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3460 * atomic, make sure that inta covers all the interrupts that
3461 * we've discovered, even if FH interrupt came in just after
3462 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003463 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003464 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003465 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003466 inta |= CSR_INT_BIT_FH_TX;
3467
3468 /* Now service all interrupt bits discovered above. */
3469 if (inta & CSR_INT_BIT_HW_ERR) {
3470 IWL_ERROR("Microcode HW error detected. Restarting.\n");
3471
3472 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003473 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003474
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003475 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003476
3477 handled |= CSR_INT_BIT_HW_ERR;
3478
3479 spin_unlock_irqrestore(&priv->lock, flags);
3480
3481 return;
3482 }
3483
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003484#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003485 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003486 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003487 if (inta & CSR_INT_BIT_SCD)
3488 IWL_DEBUG_ISR("Scheduler finished to transmit "
3489 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003490
3491 /* Alive notification via Rx interrupt will do the real work */
3492 if (inta & CSR_INT_BIT_ALIVE)
3493 IWL_DEBUG_ISR("Alive interrupt\n");
3494 }
3495#endif
3496 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003497 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003498
Ben Cahill9fbab512007-11-29 11:09:47 +08003499 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07003500 if (inta & CSR_INT_BIT_RF_KILL) {
3501 int hw_rf_kill = 0;
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003502 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07003503 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
3504 hw_rf_kill = 1;
3505
Ester Kummerf3d67992008-05-06 11:05:12 +08003506 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
Zhu Yib481de92007-09-25 17:54:57 -07003507 hw_rf_kill ? "disable radio":"enable radio");
3508
3509 /* Queue restart only if RF_KILL switch was set to "kill"
3510 * when we loaded driver, and is now set to "enable".
3511 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08003512 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08003513 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
3514 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07003515 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08003516 }
Zhu Yib481de92007-09-25 17:54:57 -07003517
3518 handled |= CSR_INT_BIT_RF_KILL;
3519 }
3520
Ben Cahill9fbab512007-11-29 11:09:47 +08003521 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07003522 if (inta & CSR_INT_BIT_CT_KILL) {
3523 IWL_ERROR("Microcode CT kill error detected.\n");
3524 handled |= CSR_INT_BIT_CT_KILL;
3525 }
3526
3527 /* Error detected by uCode */
3528 if (inta & CSR_INT_BIT_SW_ERR) {
3529 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
3530 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003531 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003532 handled |= CSR_INT_BIT_SW_ERR;
3533 }
3534
3535 /* uCode wakes up after power-down sleep */
3536 if (inta & CSR_INT_BIT_WAKEUP) {
3537 IWL_DEBUG_ISR("Wakeup interrupt\n");
Tomas Winklera55360e2008-05-05 10:22:28 +08003538 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Tomas Winklerbabcebf2008-05-15 13:54:00 +08003539 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
3540 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
3541 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
3542 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
3543 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
3544 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003545
3546 handled |= CSR_INT_BIT_WAKEUP;
3547 }
3548
3549 /* All uCode command responses, including Tx command responses,
3550 * Rx "responses" (frame-received notification), and other
3551 * notifications from uCode come through here*/
3552 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Tomas Winklera55360e2008-05-05 10:22:28 +08003553 iwl_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003554 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3555 }
3556
3557 if (inta & CSR_INT_BIT_FH_TX) {
3558 IWL_DEBUG_ISR("Tx interrupt\n");
3559 handled |= CSR_INT_BIT_FH_TX;
3560 }
3561
3562 if (inta & ~handled)
3563 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
3564
3565 if (inta & ~CSR_INI_SET_MASK) {
3566 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
3567 inta & ~CSR_INI_SET_MASK);
3568 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
3569 }
3570
3571 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003572 /* only Re-enable if diabled by irq */
3573 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3574 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003575
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003576#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08003577 if (priv->debug_level & (IWL_DL_ISR)) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003578 inta = iwl_read32(priv, CSR_INT);
3579 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3580 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003581 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3582 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3583 }
3584#endif
3585 spin_unlock_irqrestore(&priv->lock, flags);
3586}
3587
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003588static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07003589{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003590 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07003591 u32 inta, inta_mask;
3592 u32 inta_fh;
3593 if (!priv)
3594 return IRQ_NONE;
3595
3596 spin_lock(&priv->lock);
3597
3598 /* Disable (but don't clear!) interrupts here to avoid
3599 * back-to-back ISRs and sporadic interrupts from our NIC.
3600 * If we have something to service, the tasklet will re-enable ints.
3601 * If we *don't* have something, we'll re-enable before leaving here. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003602 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3603 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003604
3605 /* Discover which interrupts are active/pending */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003606 inta = iwl_read32(priv, CSR_INT);
3607 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07003608
3609 /* Ignore interrupt if there's nothing in NIC to service.
3610 * This may be due to IRQ shared with another device,
3611 * or due to sporadic interrupts thrown from our NIC. */
3612 if (!inta && !inta_fh) {
3613 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3614 goto none;
3615 }
3616
3617 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08003618 /* Hardware disappeared. It might have already raised
3619 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07003620 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08003621 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07003622 }
3623
3624 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3625 inta, inta_mask, inta_fh);
3626
Joonwoo Park25c03d82008-01-23 10:15:20 -08003627 inta &= ~CSR_INT_BIT_SCD;
3628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003629 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003630 if (likely(inta || inta_fh))
3631 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07003632
Oliver Neukum66fbb542007-11-15 09:31:10 +08003633 unplugged:
3634 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07003635 return IRQ_HANDLED;
3636
3637 none:
3638 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003639 /* only Re-enable if diabled by irq */
3640 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3641 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003642 spin_unlock(&priv->lock);
3643 return IRQ_NONE;
3644}
3645
Zhu Yib481de92007-09-25 17:54:57 -07003646/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3647 * sending probe req. This should be set long enough to hear probe responses
3648 * from more than one AP. */
3649#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
3650#define IWL_ACTIVE_DWELL_TIME_52 (10)
3651
3652/* For faster active scanning, scan will move to the next channel if fewer than
3653 * PLCP_QUIET_THRESH packets are heard on this channel within
3654 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3655 * time if it's a quiet channel (nothing responded to our probe, and there's
3656 * no other traffic).
3657 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3658#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
3659#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
3660
3661/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3662 * Must be set longer than active dwell time.
3663 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
3664#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
3665#define IWL_PASSIVE_DWELL_TIME_52 (10)
3666#define IWL_PASSIVE_DWELL_BASE (100)
3667#define IWL_CHANNEL_TUNE_TIME 5
3668
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003669static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003670 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003671{
Johannes Berg8318d782008-01-24 19:38:38 +01003672 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003673 return IWL_ACTIVE_DWELL_TIME_52;
3674 else
3675 return IWL_ACTIVE_DWELL_TIME_24;
3676}
3677
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003678static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003679 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07003680{
Johannes Berg8318d782008-01-24 19:38:38 +01003681 u16 active = iwl4965_get_active_dwell_time(priv, band);
3682 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07003683 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
3684 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
3685
Tomas Winkler3109ece2008-03-28 16:33:35 -07003686 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07003687 /* If we're associated, we clamp the maximum passive
3688 * dwell time to be 98% of the beacon interval (minus
3689 * 2 * channel tune time) */
3690 passive = priv->beacon_int;
3691 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
3692 passive = IWL_PASSIVE_DWELL_BASE;
3693 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
3694 }
3695
3696 if (passive <= active)
3697 passive = active + 1;
3698
3699 return passive;
3700}
3701
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003702static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003703 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07003704 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003705 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07003706{
3707 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01003708 const struct ieee80211_supported_band *sband;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003709 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003710 u16 passive_dwell = 0;
3711 u16 active_dwell = 0;
3712 int added, i;
3713
Emmanuel Grumbachd1141df2008-04-21 15:42:00 -07003714 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01003715 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07003716 return 0;
3717
Johannes Berg8318d782008-01-24 19:38:38 +01003718 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07003719
Johannes Berg8318d782008-01-24 19:38:38 +01003720 active_dwell = iwl4965_get_active_dwell_time(priv, band);
3721 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07003722
Johannes Berg8318d782008-01-24 19:38:38 +01003723 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02003724 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3725 continue;
3726
Johannes Berg8318d782008-01-24 19:38:38 +01003727 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07003728
Assaf Krauss8622e702008-03-21 13:53:43 -07003729 ch_info = iwl_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08003730 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07003731 if (!is_channel_valid(ch_info)) {
3732 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
3733 scan_ch->channel);
3734 continue;
3735 }
3736
3737 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01003738 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07003739 scan_ch->type = 0; /* passive */
3740 else
3741 scan_ch->type = 1; /* active */
3742
3743 if (scan_ch->type & 1)
3744 scan_ch->type |= (direct_mask << 1);
3745
Zhu Yib481de92007-09-25 17:54:57 -07003746 scan_ch->active_dwell = cpu_to_le16(active_dwell);
3747 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
3748
Ben Cahill9fbab512007-11-29 11:09:47 +08003749 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07003750 scan_ch->tpc.dsp_atten = 110;
3751 /* scan_pwr_info->tpc.dsp_atten; */
3752
3753 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01003754 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003755 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
3756 else {
3757 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
3758 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08003759 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08003760 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07003761 */
3762 }
3763
3764 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
3765 scan_ch->channel,
3766 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
3767 (scan_ch->type & 1) ?
3768 active_dwell : passive_dwell);
3769
3770 scan_ch++;
3771 added++;
3772 }
3773
3774 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
3775 return added;
3776}
3777
Zhu Yib481de92007-09-25 17:54:57 -07003778/******************************************************************************
3779 *
3780 * uCode download functions
3781 *
3782 ******************************************************************************/
3783
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003784static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003785{
Tomas Winkler98c92212008-01-14 17:46:20 -08003786 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
3787 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
3788 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3789 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
3790 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3791 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07003792}
3793
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08003794static void iwl4965_nic_start(struct iwl_priv *priv)
3795{
3796 /* Remove all resets to allow NIC to operate */
3797 iwl_write32(priv, CSR_RESET, 0);
3798}
3799
3800
Zhu Yib481de92007-09-25 17:54:57 -07003801/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003802 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07003803 *
3804 * Copy into buffers for card to fetch via bus-mastering
3805 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003806static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003807{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003809 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07003810 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08003811 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07003812 u8 *src;
3813 size_t len;
3814 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
3815
3816 /* Ask kernel firmware_class module to get the boot firmware off disk.
3817 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003818 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
3819 if (ret < 0) {
3820 IWL_ERROR("%s firmware file req failed: Reason %d\n",
3821 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07003822 goto error;
3823 }
3824
3825 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3826 name, ucode_raw->size);
3827
3828 /* Make sure that we got at least our header! */
3829 if (ucode_raw->size < sizeof(*ucode)) {
3830 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08003831 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003832 goto err_release;
3833 }
3834
3835 /* Data from ucode file: header followed by uCode images */
3836 ucode = (void *)ucode_raw->data;
3837
3838 ver = le32_to_cpu(ucode->ver);
3839 inst_size = le32_to_cpu(ucode->inst_size);
3840 data_size = le32_to_cpu(ucode->data_size);
3841 init_size = le32_to_cpu(ucode->init_size);
3842 init_data_size = le32_to_cpu(ucode->init_data_size);
3843 boot_size = le32_to_cpu(ucode->boot_size);
3844
3845 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
3846 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
3847 inst_size);
3848 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
3849 data_size);
3850 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
3851 init_size);
3852 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
3853 init_data_size);
3854 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
3855 boot_size);
3856
3857 /* Verify size of file vs. image size info in file's header */
3858 if (ucode_raw->size < sizeof(*ucode) +
3859 inst_size + data_size + init_size +
3860 init_data_size + boot_size) {
3861
3862 IWL_DEBUG_INFO("uCode file size %d too small\n",
3863 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08003864 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003865 goto err_release;
3866 }
3867
3868 /* Verify that uCode images will fit in card's SRAM */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003869 if (inst_size > priv->hw_params.max_inst_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003870 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
3871 inst_size);
3872 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003873 goto err_release;
3874 }
3875
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003876 if (data_size > priv->hw_params.max_data_size) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08003877 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
3878 data_size);
3879 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003880 goto err_release;
3881 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003882 if (init_size > priv->hw_params.max_inst_size) {
Zhu Yib481de92007-09-25 17:54:57 -07003883 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08003884 ("uCode init instr len %d too large to fit in\n",
3885 init_size);
3886 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003887 goto err_release;
3888 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003889 if (init_data_size > priv->hw_params.max_data_size) {
Zhu Yib481de92007-09-25 17:54:57 -07003890 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08003891 ("uCode init data len %d too large to fit in\n",
3892 init_data_size);
3893 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003894 goto err_release;
3895 }
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003896 if (boot_size > priv->hw_params.max_bsm_size) {
Zhu Yib481de92007-09-25 17:54:57 -07003897 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08003898 ("uCode boot instr len %d too large to fit in\n",
3899 boot_size);
3900 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07003901 goto err_release;
3902 }
3903
3904 /* Allocate ucode buffers for card's bus-master loading ... */
3905
3906 /* Runtime instructions and 2 copies of data:
3907 * 1) unmodified from disk
3908 * 2) backup cache for save/restore during power-downs */
3909 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003910 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07003911
3912 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003913 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07003914
3915 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003916 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07003917
3918 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003919 if (init_size && init_data_size) {
3920 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003921 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07003922
Tomas Winkler90e759d2007-11-29 11:09:41 +08003923 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003924 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08003925
3926 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
3927 goto err_pci_alloc;
3928 }
Zhu Yib481de92007-09-25 17:54:57 -07003929
3930 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08003931 if (boot_size) {
3932 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08003933 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07003934
Tomas Winkler90e759d2007-11-29 11:09:41 +08003935 if (!priv->ucode_boot.v_addr)
3936 goto err_pci_alloc;
3937 }
Zhu Yib481de92007-09-25 17:54:57 -07003938
3939 /* Copy images into buffers for card's bus-master reads ... */
3940
3941 /* Runtime instructions (first block of data in file) */
3942 src = &ucode->data[0];
3943 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003944 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003945 memcpy(priv->ucode_code.v_addr, src, len);
3946 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
3947 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
3948
3949 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003950 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07003951 src = &ucode->data[inst_size];
3952 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003953 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003954 memcpy(priv->ucode_data.v_addr, src, len);
3955 memcpy(priv->ucode_data_backup.v_addr, src, len);
3956
3957 /* Initialization instructions (3rd block) */
3958 if (init_size) {
3959 src = &ucode->data[inst_size + data_size];
3960 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003961 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
3962 len);
Zhu Yib481de92007-09-25 17:54:57 -07003963 memcpy(priv->ucode_init.v_addr, src, len);
3964 }
3965
3966 /* Initialization data (4th block) */
3967 if (init_data_size) {
3968 src = &ucode->data[inst_size + data_size + init_size];
3969 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003970 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
3971 len);
Zhu Yib481de92007-09-25 17:54:57 -07003972 memcpy(priv->ucode_init_data.v_addr, src, len);
3973 }
3974
3975 /* Bootstrap instructions (5th block) */
3976 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
3977 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08003978 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003979 memcpy(priv->ucode_boot.v_addr, src, len);
3980
3981 /* We have our copies now, allow OS release its copies */
3982 release_firmware(ucode_raw);
3983 return 0;
3984
3985 err_pci_alloc:
3986 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08003987 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003988 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003989
3990 err_release:
3991 release_firmware(ucode_raw);
3992
3993 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08003994 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07003995}
3996
Zhu Yib481de92007-09-25 17:54:57 -07003997/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003998 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07003999 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004000 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07004001 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004002static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004003{
Tomas Winkler57aab752008-04-14 21:16:03 -07004004 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004005
4006 IWL_DEBUG_INFO("Runtime Alive received.\n");
4007
4008 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4009 /* We had an error bringing up the hardware, so take it
4010 * all the way back down so we can try again */
4011 IWL_DEBUG_INFO("Alive failed.\n");
4012 goto restart;
4013 }
4014
4015 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4016 * This is a paranoid check, because we would not have gotten the
4017 * "runtime" alive if code weren't properly loaded. */
Emmanuel Grumbachb0692f22008-04-24 11:55:18 -07004018 if (iwl_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004019 /* Runtime instruction load was bad;
4020 * take it all the way back down so we can try again */
4021 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4022 goto restart;
4023 }
4024
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004025 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004026
Tomas Winkler57aab752008-04-14 21:16:03 -07004027 ret = priv->cfg->ops->lib->alive_notify(priv);
4028 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07004029 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
Tomas Winkler57aab752008-04-14 21:16:03 -07004030 ret);
Zhu Yib481de92007-09-25 17:54:57 -07004031 goto restart;
4032 }
4033
Ben Cahill9fbab512007-11-29 11:09:47 +08004034 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07004035 set_bit(STATUS_ALIVE, &priv->status);
4036
4037 /* Clear out the uCode error bit if it is set */
4038 clear_bit(STATUS_FW_ERROR, &priv->status);
4039
Tomas Winklerfee12472008-04-03 16:05:21 -07004040 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004041 return;
4042
Zhu Yi5a66926a2008-01-14 17:46:18 -08004043 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004044
4045 priv->active_rate = priv->rates_mask;
4046 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
4047
Tomas Winkler3109ece2008-03-28 16:33:35 -07004048 if (iwl_is_associated(priv)) {
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08004049 struct iwl_rxon_cmd *active_rxon =
4050 (struct iwl_rxon_cmd *)&priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07004051
4052 memcpy(&priv->staging_rxon, &priv->active_rxon,
4053 sizeof(priv->staging_rxon));
4054 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4055 } else {
4056 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004057 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004058 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
4059 }
4060
Ben Cahill9fbab512007-11-29 11:09:47 +08004061 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004062 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004063
4064 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004065 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004066
4067 /* At this point, the NIC is initialized and operational */
4068 priv->notif_missed_beacons = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004069
4070 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004071
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07004072 iwl_leds_register(priv);
4073
Zhu Yib481de92007-09-25 17:54:57 -07004074 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07004075 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004076 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07004077
4078 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004079 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004080
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004081 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004082 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
Zhu Yib481de92007-09-25 17:54:57 -07004083 return;
4084
4085 restart:
4086 queue_work(priv->workqueue, &priv->restart);
4087}
4088
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004089static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07004090
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004091static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004092{
4093 unsigned long flags;
4094 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4095 struct ieee80211_conf *conf = NULL;
4096
4097 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4098
4099 conf = ieee80211_get_hw_conf(priv->hw);
4100
4101 if (!exit_pending)
4102 set_bit(STATUS_EXIT_PENDING, &priv->status);
4103
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07004104 iwl_leds_unregister(priv);
4105
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07004106 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
4107
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004108 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004109
4110 /* Unblock any waiting calls */
4111 wake_up_interruptible_all(&priv->wait_command_queue);
4112
Zhu Yib481de92007-09-25 17:54:57 -07004113 /* Wipe out the EXIT_PENDING status bit if we are not actually
4114 * exiting the module */
4115 if (!exit_pending)
4116 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4117
4118 /* stop and reset the on-board processor */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004119 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07004120
4121 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004122 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004123 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004124 spin_unlock_irqrestore(&priv->lock, flags);
4125 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004126
4127 if (priv->mac80211_registered)
4128 ieee80211_stop_queues(priv->hw);
4129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004130 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07004131 * clear all bits but the RF Kill and SUSPEND bits and return */
Tomas Winklerfee12472008-04-03 16:05:21 -07004132 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004133 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4134 STATUS_RF_KILL_HW |
4135 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4136 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004137 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4138 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004139 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4140 STATUS_IN_SUSPEND;
4141 goto exit;
4142 }
4143
4144 /* ...otherwise clear out all the status bits but the RF Kill and
4145 * SUSPEND bits and continue taking the NIC down. */
4146 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4147 STATUS_RF_KILL_HW |
4148 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4149 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08004150 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4151 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07004152 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4153 STATUS_IN_SUSPEND |
4154 test_bit(STATUS_FW_ERROR, &priv->status) <<
4155 STATUS_FW_ERROR;
4156
4157 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004158 iwl_clear_bit(priv, CSR_GP_CNTRL,
Ben Cahill9fbab512007-11-29 11:09:47 +08004159 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07004160 spin_unlock_irqrestore(&priv->lock, flags);
4161
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004162 iwl4965_hw_txq_ctx_stop(priv);
4163 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004164
4165 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004166 if (!iwl_grab_nic_access(priv)) {
4167 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07004168 APMG_CLK_VAL_DMA_CLK_RQT);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004169 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004170 }
4171 spin_unlock_irqrestore(&priv->lock, flags);
4172
4173 udelay(5);
4174
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004175 iwl4965_hw_nic_stop_master(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004176 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004177 iwl4965_hw_nic_reset(priv);
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004178 priv->cfg->ops->lib->free_shared_mem(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004179
4180 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004181 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07004182
4183 if (priv->ibss_beacon)
4184 dev_kfree_skb(priv->ibss_beacon);
4185 priv->ibss_beacon = NULL;
4186
4187 /* clear out any free frames */
Tomas Winklerfcab4232008-05-15 13:54:01 +08004188 iwl_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004189}
4190
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004191static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004192{
4193 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004194 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004195 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08004196
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004197 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004198}
4199
4200#define MAX_HW_RESTARTS 5
4201
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004202static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004203{
Tomas Winkler57aab752008-04-14 21:16:03 -07004204 int i;
4205 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004206
4207 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4208 IWL_WARNING("Exit pending; will not bring the NIC up\n");
4209 return -EIO;
4210 }
4211
4212 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
4213 IWL_WARNING("Radio disabled by SW RF kill (module "
4214 "parameter)\n");
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004215 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004216 return -ENODEV;
4217 }
4218
Reinette Chatree903fbd2008-01-30 22:05:15 -08004219 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
4220 IWL_ERROR("ucode not available for device bringup\n");
4221 return -EIO;
4222 }
4223
Zhu Yie655b9f2008-01-24 02:19:38 -08004224 /* If platform's RF_KILL switch is NOT set to KILL */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004225 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08004226 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4227 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4228 else {
4229 set_bit(STATUS_RF_KILL_HW, &priv->status);
4230 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004231 iwl_rfkill_set_hw_state(priv);
Zhu Yie655b9f2008-01-24 02:19:38 -08004232 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
4233 return -ENODEV;
4234 }
Zhu Yib481de92007-09-25 17:54:57 -07004235 }
4236
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004237 iwl_rfkill_set_hw_state(priv);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004238 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07004239
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004240 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
4241 if (ret) {
4242 IWL_ERROR("Unable to allocate shared memory\n");
4243 return ret;
4244 }
4245
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004246 ret = iwl_hw_nic_init(priv);
Tomas Winkler57aab752008-04-14 21:16:03 -07004247 if (ret) {
4248 IWL_ERROR("Unable to init nic\n");
4249 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004250 }
4251
4252 /* make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004253 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4254 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004255 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4256
4257 /* clear (again), then enable host interrupts */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004258 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004259 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004260
4261 /* really make sure rfkill handshake bits are cleared */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07004262 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4263 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07004264
4265 /* Copy original ucode data image from disk into backup cache.
4266 * This will be used to initialize the on-board processor's
4267 * data SRAM for a clean start when the runtime program first loads. */
4268 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08004269 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07004270
Zhu Yie655b9f2008-01-24 02:19:38 -08004271 /* We return success when we resume from suspend and rf_kill is on. */
4272 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07004273 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07004274
4275 for (i = 0; i < MAX_HW_RESTARTS; i++) {
4276
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004277 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004278
4279 /* load bootstrap state machine,
4280 * load bootstrap program into processor's memory,
4281 * prepare to load the "initialize" uCode */
Tomas Winkler57aab752008-04-14 21:16:03 -07004282 ret = priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004283
Tomas Winkler57aab752008-04-14 21:16:03 -07004284 if (ret) {
4285 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
Zhu Yib481de92007-09-25 17:54:57 -07004286 continue;
4287 }
4288
4289 /* start card; "initialize" will load runtime ucode */
Ron Rindjunskyedcdf8b2008-05-15 13:53:55 +08004290 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004291
Zhu Yib481de92007-09-25 17:54:57 -07004292 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
4293
4294 return 0;
4295 }
4296
4297 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004298 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004299
4300 /* tried to restart and config the device for as long as our
4301 * patience could withstand */
4302 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
4303 return -EIO;
4304}
4305
4306
4307/*****************************************************************************
4308 *
4309 * Workqueue callbacks
4310 *
4311 *****************************************************************************/
4312
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004313static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004314{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004315 struct iwl_priv *priv =
4316 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004317
4318 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4319 return;
4320
4321 mutex_lock(&priv->mutex);
Emmanuel Grumbachf3ccc082008-05-05 10:22:45 +08004322 priv->cfg->ops->lib->init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004323 mutex_unlock(&priv->mutex);
4324}
4325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004326static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004327{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004328 struct iwl_priv *priv =
4329 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07004330
4331 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4332 return;
4333
4334 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004335 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004336 mutex_unlock(&priv->mutex);
4337}
4338
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004339static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004340{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004341 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07004342
4343 wake_up_interruptible(&priv->wait_command_queue);
4344
4345 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4346 return;
4347
4348 mutex_lock(&priv->mutex);
4349
Tomas Winklerfee12472008-04-03 16:05:21 -07004350 if (!iwl_is_rfkill(priv)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08004351 IWL_DEBUG(IWL_DL_RF_KILL,
Zhu Yib481de92007-09-25 17:54:57 -07004352 "HW and/or SW RF Kill no longer active, restarting "
4353 "device\n");
4354 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4355 queue_work(priv->workqueue, &priv->restart);
4356 } else {
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004357 /* make sure mac80211 stop sending Tx frame */
4358 if (priv->mac80211_registered)
4359 ieee80211_stop_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07004360
4361 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
4362 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
4363 "disabled by SW switch\n");
4364 else
4365 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
4366 "Kill switch must be turned off for "
4367 "wireless networking to work.\n");
4368 }
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004369 iwl_rfkill_set_hw_state(priv);
4370
Zhu Yib481de92007-09-25 17:54:57 -07004371 mutex_unlock(&priv->mutex);
4372}
4373
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08004374static void iwl4965_bg_set_monitor(struct work_struct *work)
4375{
4376 struct iwl_priv *priv = container_of(work,
4377 struct iwl_priv, set_monitor);
4378
4379 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
4380
4381 mutex_lock(&priv->mutex);
4382
4383 if (!iwl_is_ready(priv))
4384 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
4385 else
4386 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
4387 IWL_ERROR("iwl4965_set_mode() failed\n");
4388
4389 mutex_unlock(&priv->mutex);
4390}
4391
Zhu Yib481de92007-09-25 17:54:57 -07004392#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
4393
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004394static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004395{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004396 struct iwl_priv *priv =
4397 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07004398
4399 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4400 return;
4401
4402 mutex_lock(&priv->mutex);
4403 if (test_bit(STATUS_SCANNING, &priv->status) ||
4404 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Ester Kummerf3d67992008-05-06 11:05:12 +08004405 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
4406 "adapter (%dms)\n",
4407 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08004408
Zhu Yib481de92007-09-25 17:54:57 -07004409 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004410 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004411 }
4412 mutex_unlock(&priv->mutex);
4413}
4414
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004415static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004416{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004417 struct iwl_priv *priv =
4418 container_of(data, struct iwl_priv, request_scan);
Tomas Winkler857485c2008-03-21 13:53:44 -07004419 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07004420 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004421 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07004422 .meta.flags = CMD_SIZE_HUGE,
4423 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004424 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07004425 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004426 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01004427 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02004428 u8 direct_mask;
Tomas Winkler857485c2008-03-21 13:53:44 -07004429 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004430
4431 conf = ieee80211_get_hw_conf(priv->hw);
4432
4433 mutex_lock(&priv->mutex);
4434
Tomas Winklerfee12472008-04-03 16:05:21 -07004435 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004436 IWL_WARNING("request scan called when driver not ready.\n");
4437 goto done;
4438 }
4439
4440 /* Make sure the scan wasn't cancelled before this queued work
4441 * was given the chance to run... */
4442 if (!test_bit(STATUS_SCANNING, &priv->status))
4443 goto done;
4444
4445 /* This should never be called or scheduled if there is currently
4446 * a scan active in the hardware. */
4447 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
4448 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
4449 "Ignoring second request.\n");
Tomas Winkler857485c2008-03-21 13:53:44 -07004450 ret = -EIO;
Zhu Yib481de92007-09-25 17:54:57 -07004451 goto done;
4452 }
4453
4454 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4455 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4456 goto done;
4457 }
4458
4459 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4460 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
4461 goto done;
4462 }
4463
Tomas Winklerfee12472008-04-03 16:05:21 -07004464 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004465 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4466 goto done;
4467 }
4468
4469 if (!test_bit(STATUS_READY, &priv->status)) {
4470 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
4471 goto done;
4472 }
4473
4474 if (!priv->scan_bands) {
4475 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
4476 goto done;
4477 }
4478
4479 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004480 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07004481 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
4482 if (!priv->scan) {
Tomas Winkler857485c2008-03-21 13:53:44 -07004483 ret = -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004484 goto done;
4485 }
4486 }
4487 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004488 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07004489
4490 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
4491 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
4492
Tomas Winkler3109ece2008-03-28 16:33:35 -07004493 if (iwl_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004494 u16 interval = 0;
4495 u32 extra;
4496 u32 suspend_time = 100;
4497 u32 scan_suspend_time = 100;
4498 unsigned long flags;
4499
4500 IWL_DEBUG_INFO("Scanning while associated...\n");
4501
4502 spin_lock_irqsave(&priv->lock, flags);
4503 interval = priv->beacon_int;
4504 spin_unlock_irqrestore(&priv->lock, flags);
4505
4506 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08004507 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07004508 if (!interval)
4509 interval = suspend_time;
4510
4511 extra = (suspend_time / interval) << 22;
4512 scan_suspend_time = (extra |
4513 ((suspend_time % interval) * 1024));
4514 scan->suspend_time = cpu_to_le32(scan_suspend_time);
4515 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
4516 scan_suspend_time, interval);
4517 }
4518
4519 /* We should add the ability for user to lock to PASSIVE ONLY */
4520 if (priv->one_direct_scan) {
4521 IWL_DEBUG_SCAN
4522 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004523 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07004524 priv->direct_ssid_len));
4525 scan->direct_scan[0].id = WLAN_EID_SSID;
4526 scan->direct_scan[0].len = priv->direct_ssid_len;
4527 memcpy(scan->direct_scan[0].ssid,
4528 priv->direct_ssid, priv->direct_ssid_len);
4529 direct_mask = 1;
Tomas Winkler3109ece2008-03-28 16:33:35 -07004530 } else if (!iwl_is_associated(priv) && priv->essid_len) {
Bill Moss786b4552008-04-17 16:03:40 -07004531 IWL_DEBUG_SCAN
4532 ("Kicking off one direct scan for '%s' when not associated\n",
4533 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07004534 scan->direct_scan[0].id = WLAN_EID_SSID;
4535 scan->direct_scan[0].len = priv->essid_len;
4536 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
4537 direct_mask = 1;
Tomas Winkler857485c2008-03-21 13:53:44 -07004538 } else {
Bill Moss786b4552008-04-17 16:03:40 -07004539 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004540 direct_mask = 0;
Tomas Winkler857485c2008-03-21 13:53:44 -07004541 }
Zhu Yib481de92007-09-25 17:54:57 -07004542
Zhu Yib481de92007-09-25 17:54:57 -07004543 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler5425e492008-04-15 16:01:38 -07004544 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07004545 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4546
Zhu Yib481de92007-09-25 17:54:57 -07004547
4548 switch (priv->scan_bands) {
4549 case 2:
4550 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
4551 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004552 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004553 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
4554
4555 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004556 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004557 break;
4558
4559 case 1:
4560 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004561 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07004562 RATE_MCS_ANT_B_MSK);
4563 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01004564 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004565 break;
4566
4567 default:
4568 IWL_WARNING("Invalid scan band count\n");
4569 goto done;
4570 }
4571
Tomas Winkler78330fd2008-02-06 02:37:18 +02004572 /* We don't build a direct scan probe request; the uCode will do
4573 * that based on the direct_mask added to each channel entry */
4574 cmd_len = iwl4965_fill_probe_req(priv, band,
4575 (struct ieee80211_mgmt *)scan->data,
4576 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
4577
4578 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07004579 /* select Rx chains */
4580
4581 /* Force use of chains B and C (0x6) for scan Rx.
4582 * Avoid A (0x1) because of its off-channel reception on A-band.
4583 * MIMO is not used here, but value is required to make uCode happy. */
4584 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
4585 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
4586 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
4587 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
4588
4589 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
4590 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
4591
Bill Moss786b4552008-04-17 16:03:40 -07004592 if (direct_mask)
Reinette Chatre26c0f032008-03-11 16:17:15 -07004593 scan->channel_count =
4594 iwl4965_get_channels_for_scan(
4595 priv, band, 1, /* active */
4596 direct_mask,
4597 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Bill Moss786b4552008-04-17 16:03:40 -07004598 else
Reinette Chatre26c0f032008-03-11 16:17:15 -07004599 scan->channel_count =
4600 iwl4965_get_channels_for_scan(
4601 priv, band, 0, /* passive */
4602 direct_mask,
4603 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07004604
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004605 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
4606 RXON_FILTER_BCON_AWARE_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07004607 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004608 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07004609 cmd.data = scan;
4610 scan->len = cpu_to_le16(cmd.len);
4611
4612 set_bit(STATUS_SCAN_HW, &priv->status);
Tomas Winkler857485c2008-03-21 13:53:44 -07004613 ret = iwl_send_cmd_sync(priv, &cmd);
4614 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004615 goto done;
4616
4617 queue_delayed_work(priv->workqueue, &priv->scan_check,
4618 IWL_SCAN_CHECK_WATCHDOG);
4619
4620 mutex_unlock(&priv->mutex);
4621 return;
4622
4623 done:
Ian Schram01ebd062007-10-25 17:15:22 +08004624 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07004625 queue_work(priv->workqueue, &priv->scan_completed);
4626 mutex_unlock(&priv->mutex);
4627}
4628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004629static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004630{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004631 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07004632
4633 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4634 return;
4635
4636 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004637 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004638 mutex_unlock(&priv->mutex);
4639}
4640
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004641static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004642{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004643 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07004644
4645 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4646 return;
4647
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004648 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004649 queue_work(priv->workqueue, &priv->up);
4650}
4651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004652static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07004653{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004654 struct iwl_priv *priv =
4655 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07004656
4657 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4658 return;
4659
4660 mutex_lock(&priv->mutex);
Tomas Winklera55360e2008-05-05 10:22:28 +08004661 iwl_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004662 mutex_unlock(&priv->mutex);
4663}
4664
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004665#define IWL_DELAY_NEXT_SCAN (HZ*2)
4666
Reinette Chatre508e32e2008-04-14 21:16:13 -07004667static void iwl4965_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004668{
Zhu Yib481de92007-09-25 17:54:57 -07004669 struct ieee80211_conf *conf = NULL;
Tomas Winkler857485c2008-03-21 13:53:44 -07004670 int ret = 0;
Joe Perches0795af52007-10-03 17:59:30 -07004671 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004672
4673 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4674 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
4675 return;
4676 }
4677
Joe Perches0795af52007-10-03 17:59:30 -07004678 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
4679 priv->assoc_id,
4680 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004681
4682
4683 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4684 return;
4685
Zhu Yib481de92007-09-25 17:54:57 -07004686
Reinette Chatre508e32e2008-04-14 21:16:13 -07004687 if (!priv->vif || !priv->is_open)
Mohamed Abbas948c1712007-10-25 17:15:45 +08004688 return;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004689
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004690 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08004691
Zhu Yib481de92007-09-25 17:54:57 -07004692 conf = ieee80211_get_hw_conf(priv->hw);
4693
4694 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004695 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004696
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004697 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4698 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07004699 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07004700 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07004701 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07004702 IWL_WARNING("REPLY_RXON_TIMING failed - "
4703 "Attempting to continue.\n");
4704
4705 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4706
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004707#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004708 if (priv->current_ht_config.is_ht)
Tomas Winkler47c51962008-05-05 10:22:41 +08004709 iwl_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004710#endif /* CONFIG_IWL4965_HT*/
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07004711 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004712 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4713
4714 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
4715 priv->assoc_id, priv->beacon_int);
4716
4717 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4718 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4719 else
4720 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4721
4722 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4723 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
4724 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
4725 else
4726 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4727
4728 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4729 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4730
4731 }
4732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004733 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004734
4735 switch (priv->iw_mode) {
4736 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004737 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07004738 break;
4739
4740 case IEEE80211_IF_TYPE_IBSS:
4741
4742 /* clear out the station table */
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004743 iwlcore_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004744
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08004745 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
4746 iwl_rxon_add_station(priv, priv->bssid, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004747 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
4748 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004749
4750 break;
4751
4752 default:
4753 IWL_ERROR("%s Should not be called in %d mode\n",
4754 __FUNCTION__, priv->iw_mode);
4755 break;
4756 }
4757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004758 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004759
Zhu Yib481de92007-09-25 17:54:57 -07004760 /* Enable Rx differential gain and sensitivity calibrations */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004761 iwl_chain_noise_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004762 priv->start_calib = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004763
4764 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4765 priv->assoc_station_added = 1;
4766
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004767 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08004768
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004769 iwl_power_update_mode(priv, 0);
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08004770 /* we have just associated, don't start scan too early */
4771 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Reinette Chatre508e32e2008-04-14 21:16:13 -07004772}
4773
4774
4775static void iwl4965_bg_post_associate(struct work_struct *data)
4776{
4777 struct iwl_priv *priv = container_of(data, struct iwl_priv,
4778 post_associate.work);
4779
4780 mutex_lock(&priv->mutex);
4781 iwl4965_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004782 mutex_unlock(&priv->mutex);
Reinette Chatre508e32e2008-04-14 21:16:13 -07004783
Zhu Yib481de92007-09-25 17:54:57 -07004784}
4785
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004786static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004787{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004788 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07004789
Tomas Winklerfee12472008-04-03 16:05:21 -07004790 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07004791 return;
4792
4793 mutex_lock(&priv->mutex);
4794
4795 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004796 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004797
4798 mutex_unlock(&priv->mutex);
4799}
4800
Zhu Yi76bb77e2007-11-22 10:53:22 +08004801static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
4802
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004803static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07004804{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004805 struct iwl_priv *priv =
4806 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07004807
Ester Kummerf3d67992008-05-06 11:05:12 +08004808 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
Zhu Yib481de92007-09-25 17:54:57 -07004809
4810 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4811 return;
4812
Zhu Yia0646472007-12-20 14:10:01 +08004813 if (test_bit(STATUS_CONF_PENDING, &priv->status))
4814 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08004815
Zhu Yib481de92007-09-25 17:54:57 -07004816 ieee80211_scan_completed(priv->hw);
4817
4818 /* Since setting the TXPOWER may have been deferred while
4819 * performing the scan, fire one off */
4820 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004821 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004822 mutex_unlock(&priv->mutex);
4823}
4824
4825/*****************************************************************************
4826 *
4827 * mac80211 entry point functions
4828 *
4829 *****************************************************************************/
4830
Zhu Yi5a66926a2008-01-14 17:46:18 -08004831#define UCODE_READY_TIMEOUT (2 * HZ)
4832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004833static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004834{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004835 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004836 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07004837
4838 IWL_DEBUG_MAC80211("enter\n");
4839
Zhu Yi5a66926a2008-01-14 17:46:18 -08004840 if (pci_enable_device(priv->pci_dev)) {
4841 IWL_ERROR("Fail to pci_enable_device\n");
4842 return -ENODEV;
4843 }
4844 pci_restore_state(priv->pci_dev);
4845 pci_enable_msi(priv->pci_dev);
4846
4847 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
4848 DRV_NAME, priv);
4849 if (ret) {
4850 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
4851 goto out_disable_msi;
4852 }
4853
Zhu Yib481de92007-09-25 17:54:57 -07004854 /* we should be verifying the device is ready to be opened */
4855 mutex_lock(&priv->mutex);
4856
Gregory Greenmanc1adf9f2008-05-15 13:53:59 +08004857 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
Zhu Yi5a66926a2008-01-14 17:46:18 -08004858 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
4859 * ucode filename and max sizes are card-specific. */
4860
4861 if (!priv->ucode_code.len) {
4862 ret = iwl4965_read_ucode(priv);
4863 if (ret) {
4864 IWL_ERROR("Could not read microcode: %d\n", ret);
4865 mutex_unlock(&priv->mutex);
4866 goto out_release_irq;
4867 }
4868 }
4869
Zhu Yie655b9f2008-01-24 02:19:38 -08004870 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004871
Zhu Yib481de92007-09-25 17:54:57 -07004872 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08004873
Zhu Yie655b9f2008-01-24 02:19:38 -08004874 if (ret)
4875 goto out_release_irq;
4876
4877 IWL_DEBUG_INFO("Start UP work done.\n");
4878
4879 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
4880 return 0;
4881
Zhu Yi5a66926a2008-01-14 17:46:18 -08004882 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
4883 * mac80211 will not be run successfully. */
4884 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
4885 test_bit(STATUS_READY, &priv->status),
4886 UCODE_READY_TIMEOUT);
4887 if (!ret) {
4888 if (!test_bit(STATUS_READY, &priv->status)) {
4889 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
4890 jiffies_to_msecs(UCODE_READY_TIMEOUT));
4891 ret = -ETIMEDOUT;
4892 goto out_release_irq;
4893 }
4894 }
4895
Zhu Yie655b9f2008-01-24 02:19:38 -08004896 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07004897 IWL_DEBUG_MAC80211("leave\n");
4898 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004899
4900out_release_irq:
4901 free_irq(priv->pci_dev->irq, priv);
4902out_disable_msi:
4903 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08004904 pci_disable_device(priv->pci_dev);
4905 priv->is_open = 0;
4906 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08004907 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004908}
4909
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004910static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07004911{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004912 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004913
4914 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08004915
Zhu Yie655b9f2008-01-24 02:19:38 -08004916 if (!priv->is_open) {
4917 IWL_DEBUG_MAC80211("leave - skip\n");
4918 return;
4919 }
4920
Zhu Yib481de92007-09-25 17:54:57 -07004921 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08004922
Tomas Winklerfee12472008-04-03 16:05:21 -07004923 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08004924 /* stop mac, cancel any scan request and clear
4925 * RXON_FILTER_ASSOC_MSK BIT
4926 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08004927 mutex_lock(&priv->mutex);
4928 iwl4965_scan_cancel_timeout(priv, 100);
4929 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08004930 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08004931 }
4932
Zhu Yi5a66926a2008-01-14 17:46:18 -08004933 iwl4965_down(priv);
4934
4935 flush_workqueue(priv->workqueue);
4936 free_irq(priv->pci_dev->irq, priv);
4937 pci_disable_msi(priv->pci_dev);
4938 pci_save_state(priv->pci_dev);
4939 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08004940
Zhu Yib481de92007-09-25 17:54:57 -07004941 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004942}
4943
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004944static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07004945 struct ieee80211_tx_control *ctl)
4946{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004947 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004948
4949 IWL_DEBUG_MAC80211("enter\n");
4950
4951 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
4952 IWL_DEBUG_MAC80211("leave - monitor\n");
4953 return -1;
4954 }
4955
4956 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01004957 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07004958
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004959 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07004960 dev_kfree_skb_any(skb);
4961
4962 IWL_DEBUG_MAC80211("leave\n");
4963 return 0;
4964}
4965
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004966static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07004967 struct ieee80211_if_init_conf *conf)
4968{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004969 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004970 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07004971 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004972
Johannes Berg32bfd352007-12-19 01:31:26 +01004973 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07004974
Johannes Berg32bfd352007-12-19 01:31:26 +01004975 if (priv->vif) {
4976 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08004977 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07004978 }
4979
4980 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01004981 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07004982
4983 spin_unlock_irqrestore(&priv->lock, flags);
4984
4985 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02004986
4987 if (conf->mac_addr) {
4988 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
4989 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
4990 }
Zhu Yib481de92007-09-25 17:54:57 -07004991
Tomas Winklerfee12472008-04-03 16:05:21 -07004992 if (iwl_is_ready(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08004993 iwl4965_set_mode(priv, conf->type);
4994
Zhu Yib481de92007-09-25 17:54:57 -07004995 mutex_unlock(&priv->mutex);
4996
Zhu Yi5a66926a2008-01-14 17:46:18 -08004997 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07004998 return 0;
4999}
5000
5001/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005002 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07005003 *
5004 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5005 * be set inappropriately and the driver currently sets the hardware up to
5006 * use it whenever needed.
5007 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005008static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07005009{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005010 struct iwl_priv *priv = hw->priv;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07005011 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005012 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08005013 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005014
5015 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01005016 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07005017
Zhu Yi12342c42007-12-20 11:27:32 +08005018 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
5019
Tomas Winklerfee12472008-04-03 16:05:21 -07005020 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005021 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005022 ret = -EIO;
5023 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005024 }
5025
Assaf Krauss1ea87392008-03-18 14:57:50 -07005026 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07005027 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08005028 IWL_DEBUG_MAC80211("leave - scanning\n");
5029 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005030 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08005031 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07005032 }
5033
5034 spin_lock_irqsave(&priv->lock, flags);
5035
Assaf Krauss8622e702008-03-21 13:53:43 -07005036 ch_info = iwl_get_channel_info(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005037 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005038 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07005039 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5040 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005041 ret = -EINVAL;
5042 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005043 }
5044
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005045#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02005046 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07005047 * from any ht related info since 2.4 does not
5048 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02005049 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07005050#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5051 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
5052#endif
5053 )
5054 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005055#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005056
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005057 iwl_set_rxon_channel(priv, conf->channel->band,
Johannes Berg8318d782008-01-24 19:38:38 +01005058 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07005059
Johannes Berg8318d782008-01-24 19:38:38 +01005060 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07005061
5062 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01005063 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07005064 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005065 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005066
5067 spin_unlock_irqrestore(&priv->lock, flags);
5068
5069#ifdef IEEE80211_CONF_CHANNEL_SWITCH
5070 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005071 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005072 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005073 }
5074#endif
5075
Mohamed Abbasad97edd2008-03-28 16:21:06 -07005076 if (priv->cfg->ops->lib->radio_kill_sw)
5077 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07005078
5079 if (!conf->radio_enabled) {
5080 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005081 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005082 }
5083
Tomas Winklerfee12472008-04-03 16:05:21 -07005084 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005085 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08005086 ret = -EIO;
5087 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07005088 }
5089
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005090 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005091
5092 if (memcmp(&priv->active_rxon,
5093 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005094 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005095 else
5096 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
5097
5098 IWL_DEBUG_MAC80211("leave\n");
5099
Zhu Yia0646472007-12-20 14:10:01 +08005100out:
5101 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08005102 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005103 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005104}
5105
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005106static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005107{
Tomas Winkler857485c2008-03-21 13:53:44 -07005108 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005109
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08005110 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07005111 return;
5112
5113 /* The following should be done only at AP bring up */
5114 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
5115
5116 /* RXON - unassoc (to set timing command) */
5117 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005118 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005119
5120 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005121 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5122 iwl4965_setup_rxon_timing(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07005123 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005124 sizeof(priv->rxon_timing), &priv->rxon_timing);
Tomas Winkler857485c2008-03-21 13:53:44 -07005125 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07005126 IWL_WARNING("REPLY_RXON_TIMING failed - "
5127 "Attempting to continue.\n");
5128
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005129 iwl_set_rxon_chain(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005130
5131 /* FIXME: what should be the assoc_id for AP? */
5132 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5133 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5134 priv->staging_rxon.flags |=
5135 RXON_FLG_SHORT_PREAMBLE_MSK;
5136 else
5137 priv->staging_rxon.flags &=
5138 ~RXON_FLG_SHORT_PREAMBLE_MSK;
5139
5140 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5141 if (priv->assoc_capability &
5142 WLAN_CAPABILITY_SHORT_SLOT_TIME)
5143 priv->staging_rxon.flags |=
5144 RXON_FLG_SHORT_SLOT_MSK;
5145 else
5146 priv->staging_rxon.flags &=
5147 ~RXON_FLG_SHORT_SLOT_MSK;
5148
5149 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5150 priv->staging_rxon.flags &=
5151 ~RXON_FLG_SHORT_SLOT_MSK;
5152 }
5153 /* restore RXON assoc */
5154 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005155 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005156 iwl4965_activate_qos(priv, 1);
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08005157 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08005158 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005159 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005160
5161 /* FIXME - we need to add code here to detect a totally new
5162 * configuration, reset the AP, unassoc, rxon timing, assoc,
5163 * clear sta table, add BCAST sta... */
5164}
5165
Johannes Berg32bfd352007-12-19 01:31:26 +01005166static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
5167 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07005168 struct ieee80211_if_conf *conf)
5169{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005170 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005171 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07005172 unsigned long flags;
5173 int rc;
5174
5175 if (conf == NULL)
5176 return -EIO;
5177
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005178 if (priv->vif != vif) {
5179 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08005180 return 0;
5181 }
5182
Zhu Yib481de92007-09-25 17:54:57 -07005183 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
5184 (!conf->beacon || !conf->ssid_len)) {
5185 IWL_DEBUG_MAC80211
5186 ("Leaving in AP mode because HostAPD is not ready.\n");
5187 return 0;
5188 }
5189
Tomas Winklerfee12472008-04-03 16:05:21 -07005190 if (!iwl_is_alive(priv))
Zhu Yi5a66926a2008-01-14 17:46:18 -08005191 return -EAGAIN;
5192
Zhu Yib481de92007-09-25 17:54:57 -07005193 mutex_lock(&priv->mutex);
5194
Zhu Yib481de92007-09-25 17:54:57 -07005195 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07005196 IWL_DEBUG_MAC80211("bssid: %s\n",
5197 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005198
Johannes Berg4150c572007-09-17 01:29:23 -04005199/*
5200 * very dubious code was here; the probe filtering flag is never set:
5201 *
Zhu Yib481de92007-09-25 17:54:57 -07005202 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
5203 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04005204 */
Zhu Yib481de92007-09-25 17:54:57 -07005205
5206 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5207 if (!conf->bssid) {
5208 conf->bssid = priv->mac_addr;
5209 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07005210 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
5211 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07005212 }
5213 if (priv->ibss_beacon)
5214 dev_kfree_skb(priv->ibss_beacon);
5215
5216 priv->ibss_beacon = conf->beacon;
5217 }
5218
Tomas Winklerfee12472008-04-03 16:05:21 -07005219 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08005220 goto done;
5221
Zhu Yib481de92007-09-25 17:54:57 -07005222 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
5223 !is_multicast_ether_addr(conf->bssid)) {
5224 /* If there is currently a HW scan going on in the background
5225 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005226 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07005227 IWL_WARNING("Aborted scan still in progress "
5228 "after 100ms\n");
5229 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5230 mutex_unlock(&priv->mutex);
5231 return -EAGAIN;
5232 }
5233 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
5234
5235 /* TODO: Audit driver for usage of these members and see
5236 * if mac80211 deprecates them (priv->bssid looks like it
5237 * shouldn't be there, but I haven't scanned the IBSS code
5238 * to verify) - jpk */
5239 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5240
5241 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005242 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005243 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005244 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005245 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Tomas Winkler4f40e4d2008-05-15 13:54:04 +08005246 iwl_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07005247 priv, priv->active_rxon.bssid_addr, 1);
5248 }
5249
5250 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005251 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07005252 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005253 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005254 }
5255
Mohamed Abbasfde35712007-11-29 11:10:15 +08005256 done:
Zhu Yib481de92007-09-25 17:54:57 -07005257 spin_lock_irqsave(&priv->lock, flags);
5258 if (!conf->ssid_len)
5259 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5260 else
5261 memcpy(priv->essid, conf->ssid, conf->ssid_len);
5262
5263 priv->essid_len = conf->ssid_len;
5264 spin_unlock_irqrestore(&priv->lock, flags);
5265
5266 IWL_DEBUG_MAC80211("leave\n");
5267 mutex_unlock(&priv->mutex);
5268
5269 return 0;
5270}
5271
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005272static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04005273 unsigned int changed_flags,
5274 unsigned int *total_flags,
5275 int mc_count, struct dev_addr_list *mc_list)
5276{
5277 /*
5278 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005279 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04005280 */
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08005281 struct iwl_priv *priv = hw->priv;
5282 int new_flags = 0;
5283 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5284 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5285 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
5286 IEEE80211_IF_TYPE_MNTR,
5287 changed_flags, *total_flags);
5288 /* queue work 'cuz mac80211 is holding a lock which
5289 * prevents us from issuing (synchronous) f/w cmds */
5290 queue_work(priv->workqueue, &priv->set_monitor);
5291 new_flags &= FIF_PROMISC_IN_BSS |
5292 FIF_OTHER_BSS |
5293 FIF_ALLMULTI;
5294 }
5295 }
5296 *total_flags = new_flags;
Johannes Berg4150c572007-09-17 01:29:23 -04005297}
5298
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005299static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005300 struct ieee80211_if_init_conf *conf)
5301{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005302 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005303
5304 IWL_DEBUG_MAC80211("enter\n");
5305
5306 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005307
Tomas Winklerfee12472008-04-03 16:05:21 -07005308 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005309 iwl4965_scan_cancel_timeout(priv, 100);
5310 cancel_delayed_work(&priv->post_associate);
5311 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5312 iwl4965_commit_rxon(priv);
5313 }
Johannes Berg32bfd352007-12-19 01:31:26 +01005314 if (priv->vif == conf->vif) {
5315 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005316 memset(priv->bssid, 0, ETH_ALEN);
5317 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5318 priv->essid_len = 0;
5319 }
5320 mutex_unlock(&priv->mutex);
5321
5322 IWL_DEBUG_MAC80211("leave\n");
5323
5324}
Johannes Berg471b3ef2007-12-28 14:32:58 +01005325
Tomas Winkler3109ece2008-03-28 16:33:35 -07005326#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
Johannes Berg471b3ef2007-12-28 14:32:58 +01005327static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
5328 struct ieee80211_vif *vif,
5329 struct ieee80211_bss_conf *bss_conf,
5330 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02005331{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005332 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02005333
Tomas Winkler3109ece2008-03-28 16:33:35 -07005334 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
5335
Johannes Berg471b3ef2007-12-28 14:32:58 +01005336 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005337 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
5338 bss_conf->use_short_preamble);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005339 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02005340 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5341 else
5342 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5343 }
5344
Johannes Berg471b3ef2007-12-28 14:32:58 +01005345 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005346 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
Johannes Berg8318d782008-01-24 19:38:38 +01005347 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02005348 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
5349 else
5350 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
5351 }
5352
Tomas Winkler98952d52008-03-28 16:33:33 -07005353 if (changes & BSS_CHANGED_HT) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005354 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
Tomas Winkler98952d52008-03-28 16:33:33 -07005355 iwl4965_ht_conf(priv, bss_conf);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005356 iwl_set_rxon_chain(priv);
Tomas Winkler98952d52008-03-28 16:33:33 -07005357 }
5358
Johannes Berg471b3ef2007-12-28 14:32:58 +01005359 if (changes & BSS_CHANGED_ASSOC) {
Tomas Winkler3109ece2008-03-28 16:33:35 -07005360 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
Reinette Chatre508e32e2008-04-14 21:16:13 -07005361 /* This should never happen as this function should
5362 * never be called from interrupt context. */
5363 if (WARN_ON_ONCE(in_interrupt()))
5364 return;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005365 if (bss_conf->assoc) {
5366 priv->assoc_id = bss_conf->aid;
5367 priv->beacon_int = bss_conf->beacon_int;
5368 priv->timestamp = bss_conf->timestamp;
5369 priv->assoc_capability = bss_conf->assoc_capability;
5370 priv->next_scan_jiffies = jiffies +
5371 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
Reinette Chatre508e32e2008-04-14 21:16:13 -07005372 mutex_lock(&priv->mutex);
5373 iwl4965_post_associate(priv);
5374 mutex_unlock(&priv->mutex);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005375 } else {
5376 priv->assoc_id = 0;
5377 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
5378 }
5379 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
5380 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
Tomas Winkler7e8c5192008-04-15 16:01:43 -07005381 iwl_send_rxon_assoc(priv);
Johannes Berg471b3ef2007-12-28 14:32:58 +01005382 }
5383
Tomas Winkler220173b2007-10-16 00:50:25 +02005384}
Zhu Yib481de92007-09-25 17:54:57 -07005385
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005386static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07005387{
5388 int rc = 0;
5389 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005390 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005391
5392 IWL_DEBUG_MAC80211("enter\n");
5393
mabbas052c4b92007-10-25 17:15:43 +08005394 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005395 spin_lock_irqsave(&priv->lock, flags);
5396
Tomas Winklerfee12472008-04-03 16:05:21 -07005397 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005398 rc = -EIO;
5399 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
5400 goto out_unlock;
5401 }
5402
5403 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
5404 rc = -EIO;
5405 IWL_ERROR("ERROR: APs don't scan\n");
5406 goto out_unlock;
5407 }
5408
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005409 /* we don't schedule scan within next_scan_jiffies period */
5410 if (priv->next_scan_jiffies &&
5411 time_after(priv->next_scan_jiffies, jiffies)) {
5412 rc = -EAGAIN;
5413 goto out_unlock;
5414 }
Zhu Yib481de92007-09-25 17:54:57 -07005415 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005416 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
5417 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07005418 rc = -EAGAIN;
5419 goto out_unlock;
5420 }
5421 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005422 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005423 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07005424
5425 priv->one_direct_scan = 1;
5426 priv->direct_ssid_len = (u8)
5427 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
5428 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08005429 } else
5430 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005432 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005433
5434 IWL_DEBUG_MAC80211("leave\n");
5435
5436out_unlock:
5437 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08005438 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07005439
5440 return rc;
5441}
5442
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005443static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
5444 struct ieee80211_key_conf *keyconf, const u8 *addr,
5445 u32 iv32, u16 *phase1key)
5446{
5447 struct iwl_priv *priv = hw->priv;
5448 u8 sta_id = IWL_INVALID_STATION;
5449 unsigned long flags;
5450 __le16 key_flags = 0;
5451 int i;
5452 DECLARE_MAC_BUF(mac);
5453
5454 IWL_DEBUG_MAC80211("enter\n");
5455
Tomas Winkler947b13a2008-04-16 16:34:48 -07005456 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005457 if (sta_id == IWL_INVALID_STATION) {
5458 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5459 print_mac(mac, addr));
5460 return;
5461 }
5462
5463 iwl4965_scan_cancel_timeout(priv, 100);
5464
5465 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
5466 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
5467 key_flags &= ~STA_KEY_FLG_INVALID;
5468
Tomas Winkler5425e492008-04-15 16:01:38 -07005469 if (sta_id == priv->hw_params.bcast_sta_id)
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005470 key_flags |= STA_KEY_MULTICAST_MSK;
5471
5472 spin_lock_irqsave(&priv->sta_lock, flags);
5473
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005474 priv->stations[sta_id].sta.key.key_flags = key_flags;
5475 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
5476
5477 for (i = 0; i < 5; i++)
5478 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
5479 cpu_to_le16(phase1key[i]);
5480
5481 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
5482 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
5483
Tomas Winkler133636d2008-05-05 10:22:34 +08005484 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02005485
5486 spin_unlock_irqrestore(&priv->sta_lock, flags);
5487
5488 IWL_DEBUG_MAC80211("leave\n");
5489}
5490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005491static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07005492 const u8 *local_addr, const u8 *addr,
5493 struct ieee80211_key_conf *key)
5494{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005495 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07005496 DECLARE_MAC_BUF(mac);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005497 int ret = 0;
5498 u8 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005499 u8 is_default_wep_key = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005500
5501 IWL_DEBUG_MAC80211("enter\n");
5502
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07005503 if (priv->hw_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07005504 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
5505 return -EOPNOTSUPP;
5506 }
5507
5508 if (is_zero_ether_addr(addr))
5509 /* only support pairwise keys */
5510 return -EOPNOTSUPP;
5511
Tomas Winkler947b13a2008-04-16 16:34:48 -07005512 sta_id = iwl_find_station(priv, addr);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005513 if (sta_id == IWL_INVALID_STATION) {
5514 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5515 print_mac(mac, addr));
5516 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005517
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005518 }
Zhu Yib481de92007-09-25 17:54:57 -07005519
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005520 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005521 iwl4965_scan_cancel_timeout(priv, 100);
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005522 mutex_unlock(&priv->mutex);
5523
5524 /* If we are getting WEP group key and we didn't receive any key mapping
5525 * so far, we are in legacy wep mode (group key only), otherwise we are
5526 * in 1X mode.
5527 * In legacy wep mode, we use another host command to the uCode */
Tomas Winkler5425e492008-04-15 16:01:38 -07005528 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005529 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
5530 if (cmd == SET_KEY)
5531 is_default_wep_key = !priv->key_mapping_key;
5532 else
5533 is_default_wep_key = priv->default_wep_key;
5534 }
mabbas052c4b92007-10-25 17:15:43 +08005535
Zhu Yib481de92007-09-25 17:54:57 -07005536 switch (cmd) {
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005537 case SET_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005538 if (is_default_wep_key)
5539 ret = iwl_set_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005540 else
Emmanuel Grumbach74805132008-04-14 21:16:09 -07005541 ret = iwl_set_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005542
5543 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005544 break;
5545 case DISABLE_KEY:
Emmanuel Grumbach6974e362008-04-14 21:16:06 -07005546 if (is_default_wep_key)
5547 ret = iwl_remove_default_wep_key(priv, key);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005548 else
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07005549 ret = iwl_remove_dynamic_key(priv, key, sta_id);
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005550
5551 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
Zhu Yib481de92007-09-25 17:54:57 -07005552 break;
5553 default:
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005554 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005555 }
5556
5557 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005558
Emmanuel Grumbachdeb09c42008-03-19 16:41:41 -07005559 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005560}
5561
Johannes Berge100bb62008-04-30 18:51:21 +02005562static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07005563 const struct ieee80211_tx_queue_params *params)
5564{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005565 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005566 unsigned long flags;
5567 int q;
Zhu Yib481de92007-09-25 17:54:57 -07005568
5569 IWL_DEBUG_MAC80211("enter\n");
5570
Tomas Winklerfee12472008-04-03 16:05:21 -07005571 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005572 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5573 return -EIO;
5574 }
5575
5576 if (queue >= AC_NUM) {
5577 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5578 return 0;
5579 }
5580
Zhu Yib481de92007-09-25 17:54:57 -07005581 if (!priv->qos_data.qos_enable) {
5582 priv->qos_data.qos_active = 0;
5583 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
5584 return 0;
5585 }
5586 q = AC_NUM - 1 - queue;
5587
5588 spin_lock_irqsave(&priv->lock, flags);
5589
5590 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5591 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5592 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5593 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01005594 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07005595
5596 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
5597 priv->qos_data.qos_active = 1;
5598
5599 spin_unlock_irqrestore(&priv->lock, flags);
5600
5601 mutex_lock(&priv->mutex);
5602 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005603 iwl4965_activate_qos(priv, 1);
Tomas Winkler3109ece2008-03-28 16:33:35 -07005604 else if (priv->assoc_id && iwl_is_associated(priv))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005605 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005606
5607 mutex_unlock(&priv->mutex);
5608
Zhu Yib481de92007-09-25 17:54:57 -07005609 IWL_DEBUG_MAC80211("leave\n");
5610 return 0;
5611}
5612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005613static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005614 struct ieee80211_tx_queue_stats *stats)
5615{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005616 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005617 int i, avail;
Ron Rindjunsky16466902008-05-05 10:22:50 +08005618 struct iwl_tx_queue *txq;
Tomas Winkler443cfd42008-05-15 13:53:57 +08005619 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07005620 unsigned long flags;
5621
5622 IWL_DEBUG_MAC80211("enter\n");
5623
Tomas Winklerfee12472008-04-03 16:05:21 -07005624 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005625 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5626 return -EIO;
5627 }
5628
5629 spin_lock_irqsave(&priv->lock, flags);
5630
5631 for (i = 0; i < AC_NUM; i++) {
5632 txq = &priv->txq[i];
5633 q = &txq->q;
Tomas Winkler443cfd42008-05-15 13:53:57 +08005634 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07005635
Johannes Berg57ffc582008-04-29 17:18:59 +02005636 stats[i].len = q->n_window - avail;
5637 stats[i].limit = q->n_window - q->high_mark;
5638 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07005639
5640 }
5641 spin_unlock_irqrestore(&priv->lock, flags);
5642
5643 IWL_DEBUG_MAC80211("leave\n");
5644
5645 return 0;
5646}
5647
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005648static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07005649 struct ieee80211_low_level_stats *stats)
5650{
Ester Kummerbf403db2008-05-05 10:22:40 +08005651 struct iwl_priv *priv = hw->priv;
5652
5653 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005654 IWL_DEBUG_MAC80211("enter\n");
5655 IWL_DEBUG_MAC80211("leave\n");
5656
5657 return 0;
5658}
5659
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005660static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005661{
Ester Kummerbf403db2008-05-05 10:22:40 +08005662 struct iwl_priv *priv;
5663
5664 priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005665 IWL_DEBUG_MAC80211("enter\n");
5666 IWL_DEBUG_MAC80211("leave\n");
5667
5668 return 0;
5669}
5670
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005671static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005672{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005673 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005674 unsigned long flags;
5675
5676 mutex_lock(&priv->mutex);
5677 IWL_DEBUG_MAC80211("enter\n");
5678
5679 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005680#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07005681 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02005682 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07005683 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005684#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07005685
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005686 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005687
5688 cancel_delayed_work(&priv->post_associate);
5689
5690 spin_lock_irqsave(&priv->lock, flags);
5691 priv->assoc_id = 0;
5692 priv->assoc_capability = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005693 priv->assoc_station_added = 0;
5694
5695 /* new association get rid of ibss beacon skb */
5696 if (priv->ibss_beacon)
5697 dev_kfree_skb(priv->ibss_beacon);
5698
5699 priv->ibss_beacon = NULL;
5700
5701 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler3109ece2008-03-28 16:33:35 -07005702 priv->timestamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005703 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
5704 priv->beacon_int = 0;
5705
5706 spin_unlock_irqrestore(&priv->lock, flags);
5707
Tomas Winklerfee12472008-04-03 16:05:21 -07005708 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08005709 IWL_DEBUG_MAC80211("leave - not ready\n");
5710 mutex_unlock(&priv->mutex);
5711 return;
5712 }
5713
mabbas052c4b92007-10-25 17:15:43 +08005714 /* we are restarting association process
5715 * clear RXON_FILTER_ASSOC_MSK bit
5716 */
5717 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005718 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08005719 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005720 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08005721 }
5722
Mohamed Abbas5da4b552008-04-21 15:41:51 -07005723 iwl_power_update_mode(priv, 0);
5724
Zhu Yib481de92007-09-25 17:54:57 -07005725 /* Per mac80211.h: This is only used in IBSS mode... */
5726 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08005727
Zhu Yib481de92007-09-25 17:54:57 -07005728 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
5729 mutex_unlock(&priv->mutex);
5730 return;
5731 }
5732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005733 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005734
5735 mutex_unlock(&priv->mutex);
5736
5737 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07005738}
5739
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005740static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07005741 struct ieee80211_tx_control *control)
5742{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005743 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005744 unsigned long flags;
5745
5746 mutex_lock(&priv->mutex);
5747 IWL_DEBUG_MAC80211("enter\n");
5748
Tomas Winklerfee12472008-04-03 16:05:21 -07005749 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005750 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5751 mutex_unlock(&priv->mutex);
5752 return -EIO;
5753 }
5754
5755 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
5756 IWL_DEBUG_MAC80211("leave - not IBSS\n");
5757 mutex_unlock(&priv->mutex);
5758 return -EIO;
5759 }
5760
5761 spin_lock_irqsave(&priv->lock, flags);
5762
5763 if (priv->ibss_beacon)
5764 dev_kfree_skb(priv->ibss_beacon);
5765
5766 priv->ibss_beacon = skb;
5767
5768 priv->assoc_id = 0;
5769
5770 IWL_DEBUG_MAC80211("leave\n");
5771 spin_unlock_irqrestore(&priv->lock, flags);
5772
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07005773 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005774
5775 queue_work(priv->workqueue, &priv->post_associate.work);
5776
5777 mutex_unlock(&priv->mutex);
5778
5779 return 0;
5780}
5781
Zhu Yib481de92007-09-25 17:54:57 -07005782/*****************************************************************************
5783 *
5784 * sysfs attributes
5785 *
5786 *****************************************************************************/
5787
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005788#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07005789
5790/*
5791 * The following adds a new attribute to the sysfs representation
5792 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
5793 * used for controlling the debug level.
5794 *
5795 * See the level definitions in iwl for details.
5796 */
5797
Ester Kummer8cf769c2008-05-06 11:05:11 +08005798static ssize_t show_debug_level(struct device *d,
5799 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07005800{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005801 struct iwl_priv *priv = d->driver_data;
5802
5803 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07005804}
Ester Kummer8cf769c2008-05-06 11:05:11 +08005805static ssize_t store_debug_level(struct device *d,
5806 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07005807 const char *buf, size_t count)
5808{
Ester Kummer8cf769c2008-05-06 11:05:11 +08005809 struct iwl_priv *priv = d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005810 char *p = (char *)buf;
5811 u32 val;
5812
5813 val = simple_strtoul(p, &p, 0);
5814 if (p == buf)
5815 printk(KERN_INFO DRV_NAME
5816 ": %s is not in hex or decimal form.\n", buf);
5817 else
Ester Kummer8cf769c2008-05-06 11:05:11 +08005818 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07005819
5820 return strnlen(buf, count);
5821}
5822
Ester Kummer8cf769c2008-05-06 11:05:11 +08005823static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5824 show_debug_level, store_debug_level);
5825
Zhu Yib481de92007-09-25 17:54:57 -07005826
Tomas Winkler0a6857e2008-03-12 16:58:49 -07005827#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07005828
Zhu Yib481de92007-09-25 17:54:57 -07005829
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08005830static ssize_t show_version(struct device *d,
5831 struct device_attribute *attr, char *buf)
5832{
5833 struct iwl_priv *priv = d->driver_data;
5834 struct iwl4965_alive_resp *palive = &priv->card_alive;
5835
5836 if (palive->is_valid)
5837 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
5838 "fw type: 0x%01X 0x%01X\n",
5839 palive->ucode_major, palive->ucode_minor,
5840 palive->sw_rev[0], palive->sw_rev[1],
5841 palive->ver_type, palive->ver_subtype);
5842
5843 else
5844 return sprintf(buf, "fw not loaded\n");
5845}
5846
5847static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
5848
Zhu Yib481de92007-09-25 17:54:57 -07005849static ssize_t show_temperature(struct device *d,
5850 struct device_attribute *attr, char *buf)
5851{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005852 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005853
Tomas Winklerfee12472008-04-03 16:05:21 -07005854 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005855 return -EAGAIN;
5856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005857 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07005858}
5859
5860static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
5861
5862static ssize_t show_rs_window(struct device *d,
5863 struct device_attribute *attr,
5864 char *buf)
5865{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005866 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005867 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005868}
5869static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
5870
5871static ssize_t show_tx_power(struct device *d,
5872 struct device_attribute *attr, char *buf)
5873{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005874 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005875 return sprintf(buf, "%d\n", priv->user_txpower_limit);
5876}
5877
5878static ssize_t store_tx_power(struct device *d,
5879 struct device_attribute *attr,
5880 const char *buf, size_t count)
5881{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005882 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005883 char *p = (char *)buf;
5884 u32 val;
5885
5886 val = simple_strtoul(p, &p, 10);
5887 if (p == buf)
5888 printk(KERN_INFO DRV_NAME
5889 ": %s is not in decimal form.\n", buf);
5890 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005891 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07005892
5893 return count;
5894}
5895
5896static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
5897
5898static ssize_t show_flags(struct device *d,
5899 struct device_attribute *attr, char *buf)
5900{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005901 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005902
5903 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
5904}
5905
5906static ssize_t store_flags(struct device *d,
5907 struct device_attribute *attr,
5908 const char *buf, size_t count)
5909{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005910 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005911 u32 flags = simple_strtoul(buf, NULL, 0);
5912
5913 mutex_lock(&priv->mutex);
5914 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
5915 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005916 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07005917 IWL_WARNING("Could not cancel scan.\n");
5918 else {
5919 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
5920 flags);
5921 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005922 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005923 }
5924 }
5925 mutex_unlock(&priv->mutex);
5926
5927 return count;
5928}
5929
5930static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
5931
5932static ssize_t show_filter_flags(struct device *d,
5933 struct device_attribute *attr, char *buf)
5934{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005935 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005936
5937 return sprintf(buf, "0x%04X\n",
5938 le32_to_cpu(priv->active_rxon.filter_flags));
5939}
5940
5941static ssize_t store_filter_flags(struct device *d,
5942 struct device_attribute *attr,
5943 const char *buf, size_t count)
5944{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005945 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07005946 u32 filter_flags = simple_strtoul(buf, NULL, 0);
5947
5948 mutex_lock(&priv->mutex);
5949 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
5950 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005951 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07005952 IWL_WARNING("Could not cancel scan.\n");
5953 else {
5954 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
5955 "0x%04X\n", filter_flags);
5956 priv->staging_rxon.filter_flags =
5957 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005958 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005959 }
5960 }
5961 mutex_unlock(&priv->mutex);
5962
5963 return count;
5964}
5965
5966static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
5967 store_filter_flags);
5968
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005969#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07005970
5971static ssize_t show_measurement(struct device *d,
5972 struct device_attribute *attr, char *buf)
5973{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005974 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005975 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07005976 u32 size = sizeof(measure_report), len = 0, ofs = 0;
5977 u8 *data = (u8 *) & measure_report;
5978 unsigned long flags;
5979
5980 spin_lock_irqsave(&priv->lock, flags);
5981 if (!(priv->measurement_status & MEASUREMENT_READY)) {
5982 spin_unlock_irqrestore(&priv->lock, flags);
5983 return 0;
5984 }
5985 memcpy(&measure_report, &priv->measure_report, size);
5986 priv->measurement_status = 0;
5987 spin_unlock_irqrestore(&priv->lock, flags);
5988
5989 while (size && (PAGE_SIZE - len)) {
5990 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5991 PAGE_SIZE - len, 1);
5992 len = strlen(buf);
5993 if (PAGE_SIZE - len)
5994 buf[len++] = '\n';
5995
5996 ofs += 16;
5997 size -= min(size, 16U);
5998 }
5999
6000 return len;
6001}
6002
6003static ssize_t store_measurement(struct device *d,
6004 struct device_attribute *attr,
6005 const char *buf, size_t count)
6006{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006007 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006008 struct ieee80211_measurement_params params = {
6009 .channel = le16_to_cpu(priv->active_rxon.channel),
6010 .start_time = cpu_to_le64(priv->last_tsf),
6011 .duration = cpu_to_le16(1),
6012 };
6013 u8 type = IWL_MEASURE_BASIC;
6014 u8 buffer[32];
6015 u8 channel;
6016
6017 if (count) {
6018 char *p = buffer;
6019 strncpy(buffer, buf, min(sizeof(buffer), count));
6020 channel = simple_strtoul(p, NULL, 0);
6021 if (channel)
6022 params.channel = channel;
6023
6024 p = buffer;
6025 while (*p && *p != ' ')
6026 p++;
6027 if (*p)
6028 type = simple_strtoul(p + 1, NULL, 0);
6029 }
6030
6031 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6032 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006033 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006034
6035 return count;
6036}
6037
6038static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6039 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006040#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006041
6042static ssize_t store_retry_rate(struct device *d,
6043 struct device_attribute *attr,
6044 const char *buf, size_t count)
6045{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006046 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006047
6048 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6049 if (priv->retry_rate <= 0)
6050 priv->retry_rate = 1;
6051
6052 return count;
6053}
6054
6055static ssize_t show_retry_rate(struct device *d,
6056 struct device_attribute *attr, char *buf)
6057{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006058 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006059 return sprintf(buf, "%d", priv->retry_rate);
6060}
6061
6062static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6063 store_retry_rate);
6064
6065static ssize_t store_power_level(struct device *d,
6066 struct device_attribute *attr,
6067 const char *buf, size_t count)
6068{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006069 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006070 int rc;
6071 int mode;
6072
6073 mode = simple_strtoul(buf, NULL, 0);
6074 mutex_lock(&priv->mutex);
6075
Tomas Winklerfee12472008-04-03 16:05:21 -07006076 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006077 rc = -EAGAIN;
6078 goto out;
6079 }
6080
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006081 rc = iwl_power_set_user_mode(priv, mode);
6082 if (rc) {
6083 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6084 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006085 }
Zhu Yib481de92007-09-25 17:54:57 -07006086 rc = count;
6087
6088 out:
6089 mutex_unlock(&priv->mutex);
6090 return rc;
6091}
6092
6093#define MAX_WX_STRING 80
6094
6095/* Values are in microsecond */
6096static const s32 timeout_duration[] = {
6097 350000,
6098 250000,
6099 75000,
6100 37000,
6101 25000,
6102};
6103static const s32 period_duration[] = {
6104 400000,
6105 700000,
6106 1000000,
6107 1000000,
6108 1000000
6109};
6110
6111static ssize_t show_power_level(struct device *d,
6112 struct device_attribute *attr, char *buf)
6113{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006114 struct iwl_priv *priv = dev_get_drvdata(d);
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006115 int level = priv->power_data.power_mode;
Zhu Yib481de92007-09-25 17:54:57 -07006116 char *p = buf;
6117
6118 p += sprintf(p, "%d ", level);
6119 switch (level) {
6120 case IWL_POWER_MODE_CAM:
6121 case IWL_POWER_AC:
6122 p += sprintf(p, "(AC)");
6123 break;
6124 case IWL_POWER_BATTERY:
6125 p += sprintf(p, "(BATTERY)");
6126 break;
6127 default:
6128 p += sprintf(p,
6129 "(Timeout %dms, Period %dms)",
6130 timeout_duration[level - 1] / 1000,
6131 period_duration[level - 1] / 1000);
6132 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006133/*
Zhu Yib481de92007-09-25 17:54:57 -07006134 if (!(priv->power_mode & IWL_POWER_ENABLED))
6135 p += sprintf(p, " OFF\n");
6136 else
6137 p += sprintf(p, " \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -07006138*/
6139 p += sprintf(p, " \n");
Zhu Yib481de92007-09-25 17:54:57 -07006140 return (p - buf + 1);
Zhu Yib481de92007-09-25 17:54:57 -07006141}
6142
6143static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6144 store_power_level);
6145
6146static ssize_t show_channels(struct device *d,
6147 struct device_attribute *attr, char *buf)
6148{
Johannes Berg8318d782008-01-24 19:38:38 +01006149 /* all this shit doesn't belong into sysfs anyway */
6150 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006151}
6152
6153static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6154
6155static ssize_t show_statistics(struct device *d,
6156 struct device_attribute *attr, char *buf)
6157{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006158 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006159 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07006160 u32 len = 0, ofs = 0;
6161 u8 *data = (u8 *) & priv->statistics;
6162 int rc = 0;
6163
Tomas Winklerfee12472008-04-03 16:05:21 -07006164 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006165 return -EAGAIN;
6166
6167 mutex_lock(&priv->mutex);
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07006168 rc = iwl_send_statistics_request(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006169 mutex_unlock(&priv->mutex);
6170
6171 if (rc) {
6172 len = sprintf(buf,
6173 "Error sending statistics request: 0x%08X\n", rc);
6174 return len;
6175 }
6176
6177 while (size && (PAGE_SIZE - len)) {
6178 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6179 PAGE_SIZE - len, 1);
6180 len = strlen(buf);
6181 if (PAGE_SIZE - len)
6182 buf[len++] = '\n';
6183
6184 ofs += 16;
6185 size -= min(size, 16U);
6186 }
6187
6188 return len;
6189}
6190
6191static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
6192
Zhu Yib481de92007-09-25 17:54:57 -07006193static ssize_t show_status(struct device *d,
6194 struct device_attribute *attr, char *buf)
6195{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006196 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Tomas Winklerfee12472008-04-03 16:05:21 -07006197 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006198 return -EAGAIN;
6199 return sprintf(buf, "0x%08x\n", (int)priv->status);
6200}
6201
6202static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6203
6204static ssize_t dump_error_log(struct device *d,
6205 struct device_attribute *attr,
6206 const char *buf, size_t count)
6207{
6208 char *p = (char *)buf;
6209
6210 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006211 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006212
6213 return strnlen(buf, count);
6214}
6215
6216static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
6217
6218static ssize_t dump_event_log(struct device *d,
6219 struct device_attribute *attr,
6220 const char *buf, size_t count)
6221{
6222 char *p = (char *)buf;
6223
6224 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006225 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07006226
6227 return strnlen(buf, count);
6228}
6229
6230static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
6231
6232/*****************************************************************************
6233 *
6234 * driver setup and teardown
6235 *
6236 *****************************************************************************/
6237
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006238static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006239{
6240 priv->workqueue = create_workqueue(DRV_NAME);
6241
6242 init_waitqueue_head(&priv->wait_command_queue);
6243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006244 INIT_WORK(&priv->up, iwl4965_bg_up);
6245 INIT_WORK(&priv->restart, iwl4965_bg_restart);
6246 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
6247 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
6248 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
6249 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
6250 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
6251 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
Abhijeet Kolekar4419e392008-05-05 10:22:47 +08006252 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006253 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
6254 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
6255 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
6256 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07006257
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006258 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006259
6260 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006261 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07006262}
6263
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006264static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006265{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006266 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006267
Joonwoo Park3ae6a052007-11-29 10:43:16 +09006268 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07006269 cancel_delayed_work(&priv->scan_check);
6270 cancel_delayed_work(&priv->alive_start);
6271 cancel_delayed_work(&priv->post_associate);
6272 cancel_work_sync(&priv->beacon_update);
6273}
6274
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006275static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07006276 &dev_attr_channels.attr,
6277 &dev_attr_dump_errors.attr,
6278 &dev_attr_dump_events.attr,
6279 &dev_attr_flags.attr,
6280 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006281#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006282 &dev_attr_measurement.attr,
6283#endif
6284 &dev_attr_power_level.attr,
6285 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006286 &dev_attr_rs_window.attr,
6287 &dev_attr_statistics.attr,
6288 &dev_attr_status.attr,
6289 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006290 &dev_attr_tx_power.attr,
Ester Kummer8cf769c2008-05-06 11:05:11 +08006291#ifdef CONFIG_IWLWIFI_DEBUG
6292 &dev_attr_debug_level.attr,
6293#endif
Tomas Winklerbc6f59b2008-05-06 11:05:13 +08006294 &dev_attr_version.attr,
Zhu Yib481de92007-09-25 17:54:57 -07006295
6296 NULL
6297};
6298
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006299static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07006300 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006301 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07006302};
6303
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006304static struct ieee80211_ops iwl4965_hw_ops = {
6305 .tx = iwl4965_mac_tx,
6306 .start = iwl4965_mac_start,
6307 .stop = iwl4965_mac_stop,
6308 .add_interface = iwl4965_mac_add_interface,
6309 .remove_interface = iwl4965_mac_remove_interface,
6310 .config = iwl4965_mac_config,
6311 .config_interface = iwl4965_mac_config_interface,
6312 .configure_filter = iwl4965_configure_filter,
6313 .set_key = iwl4965_mac_set_key,
Emmanuel Grumbachab885f82008-03-20 15:06:43 +02006314 .update_tkip_key = iwl4965_mac_update_tkip_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006315 .get_stats = iwl4965_mac_get_stats,
6316 .get_tx_stats = iwl4965_mac_get_tx_stats,
6317 .conf_tx = iwl4965_mac_conf_tx,
6318 .get_tsf = iwl4965_mac_get_tsf,
6319 .reset_tsf = iwl4965_mac_reset_tsf,
6320 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01006321 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006322#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02006323 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006324#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006325 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07006326};
6327
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006328static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07006329{
6330 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006331 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07006332 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08006333 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006334 unsigned long flags;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006335 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006336
Assaf Krauss316c30d2008-03-14 10:38:46 -07006337 /************************
6338 * 1. Allocating HW data
6339 ************************/
6340
Cahill, Ben M6440adb2007-11-29 11:09:55 +08006341 /* Disabling hardware scan means that mac80211 will perform scans
6342 * "the hard way", rather than using device's scan. */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006343 if (cfg->mod_params->disable_hw_scan) {
Ester Kummerbf403db2008-05-05 10:22:40 +08006344 if (cfg->mod_params->debug & IWL_DL_INFO)
6345 dev_printk(KERN_DEBUG, &(pdev->dev),
6346 "Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006347 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006348 }
6349
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006350 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
6351 if (!hw) {
Zhu Yib481de92007-09-25 17:54:57 -07006352 err = -ENOMEM;
6353 goto out;
6354 }
Assaf Krauss1d0a0822008-03-14 10:38:48 -07006355 priv = hw->priv;
6356 /* At this point both hw and priv are allocated. */
6357
Zhu Yib481de92007-09-25 17:54:57 -07006358 SET_IEEE80211_DEV(hw, &pdev->dev);
6359
6360 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Tomas Winkler82b9a122008-03-04 18:09:30 -08006361 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07006362 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006363
Tomas Winkler0a6857e2008-03-12 16:58:49 -07006364#ifdef CONFIG_IWLWIFI_DEBUG
Ester Kummerbf403db2008-05-05 10:22:40 +08006365 priv->debug_level = priv->cfg->mod_params->debug;
Zhu Yib481de92007-09-25 17:54:57 -07006366 atomic_set(&priv->restrict_refcnt, 0);
6367#endif
Zhu Yib481de92007-09-25 17:54:57 -07006368
Assaf Krauss316c30d2008-03-14 10:38:46 -07006369 /**************************
6370 * 2. Initializing PCI bus
6371 **************************/
6372 if (pci_enable_device(pdev)) {
6373 err = -ENODEV;
6374 goto out_ieee80211_free_hw;
6375 }
6376
6377 pci_set_master(pdev);
6378
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006379 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006380 if (!err)
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006381 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
6382 if (err) {
6383 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6384 if (!err)
6385 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
6386 /* both attempts failed: */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006387 if (err) {
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006388 printk(KERN_WARNING "%s: No suitable DMA available.\n",
6389 DRV_NAME);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006390 goto out_pci_disable_device;
Ron Rindjunskycc2a8ea2008-04-21 15:41:59 -07006391 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006392 }
6393
6394 err = pci_request_regions(pdev, DRV_NAME);
6395 if (err)
6396 goto out_pci_disable_device;
6397
6398 pci_set_drvdata(pdev, priv);
6399
6400 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6401 * PCI Tx retries from interfering with C3 CPU state */
6402 pci_write_config_byte(pdev, 0x41, 0x00);
6403
6404 /***********************
6405 * 3. Read REV register
6406 ***********************/
6407 priv->hw_base = pci_iomap(pdev, 0, 0);
6408 if (!priv->hw_base) {
6409 err = -ENODEV;
6410 goto out_pci_release_regions;
6411 }
6412
6413 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
6414 (unsigned long long) pci_resource_len(pdev, 0));
6415 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
6416
Tomas Winklerb661c812008-04-23 17:14:54 -07006417 iwl_hw_detect(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006418 printk(KERN_INFO DRV_NAME
Tomas Winklerb661c812008-04-23 17:14:54 -07006419 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
6420 priv->cfg->name, priv->hw_rev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006421
Tomas Winkler91238712008-04-23 17:14:53 -07006422 /* amp init */
6423 err = priv->cfg->ops->lib->apm_ops.init(priv);
6424 if (err < 0) {
6425 IWL_DEBUG_INFO("Failed to init APMG\n");
6426 goto out_iounmap;
6427 }
Assaf Krauss316c30d2008-03-14 10:38:46 -07006428 /*****************
6429 * 4. Read EEPROM
6430 *****************/
Assaf Krauss316c30d2008-03-14 10:38:46 -07006431 /* Read the EEPROM */
6432 err = iwl_eeprom_init(priv);
6433 if (err) {
6434 IWL_ERROR("Unable to init EEPROM\n");
6435 goto out_iounmap;
6436 }
Tomas Winkler8614f362008-04-23 17:14:55 -07006437 err = iwl_eeprom_check_version(priv);
6438 if (err)
6439 goto out_iounmap;
6440
Ron Rindjunsky02883012008-05-15 13:53:53 +08006441 /* extract MAC Address */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006442 iwl_eeprom_get_mac(priv, priv->mac_addr);
6443 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
6444 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6445
6446 /************************
6447 * 5. Setup HW constants
6448 ************************/
6449 /* Device-specific setup */
Tomas Winkler5425e492008-04-15 16:01:38 -07006450 if (priv->cfg->ops->lib->set_hw_params(priv)) {
6451 IWL_ERROR("failed to set hw parameters\n");
Tomas Winkler073d3f52008-04-21 15:41:52 -07006452 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006453 }
6454
6455 /*******************
6456 * 6. Setup hw/priv
6457 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07006458
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006459 err = iwl_setup(priv);
6460 if (err)
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006461 goto out_free_eeprom;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006462 /* At this point both hw and priv are initialized. */
Assaf Krauss316c30d2008-03-14 10:38:46 -07006463
6464 /**********************************
6465 * 7. Initialize module parameters
6466 **********************************/
6467
6468 /* Disable radio (SW RF KILL) via parameter when loading driver */
Assaf Krauss1ea87392008-03-18 14:57:50 -07006469 if (priv->cfg->mod_params->disable) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07006470 set_bit(STATUS_RF_KILL_SW, &priv->status);
6471 IWL_DEBUG_INFO("Radio disabled.\n");
6472 }
6473
Assaf Krauss1ea87392008-03-18 14:57:50 -07006474 if (priv->cfg->mod_params->enable_qos)
Assaf Krauss316c30d2008-03-14 10:38:46 -07006475 priv->qos_data.qos_enable = 1;
6476
6477 /********************
6478 * 8. Setup services
6479 ********************/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006480 spin_lock_irqsave(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006481 iwl4965_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006482 spin_unlock_irqrestore(&priv->lock, flags);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006483
6484 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6485 if (err) {
6486 IWL_ERROR("failed to create sysfs device attributes\n");
Ron Rindjunsky399f4902008-04-23 17:14:56 -07006487 goto out_free_eeprom;
Assaf Krauss316c30d2008-03-14 10:38:46 -07006488 }
6489
6490 err = iwl_dbgfs_register(priv, DRV_NAME);
6491 if (err) {
6492 IWL_ERROR("failed to create debugfs files\n");
6493 goto out_remove_sysfs;
6494 }
6495
6496 iwl4965_setup_deferred_work(priv);
6497 iwl4965_setup_rx_handlers(priv);
6498
6499 /********************
6500 * 9. Conclude
6501 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08006502 pci_save_state(pdev);
6503 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006504
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006505 /* notify iwlcore to init */
6506 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
Zhu Yib481de92007-09-25 17:54:57 -07006507 return 0;
6508
Assaf Krauss316c30d2008-03-14 10:38:46 -07006509 out_remove_sysfs:
6510 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006511 out_free_eeprom:
6512 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006513 out_iounmap:
6514 pci_iounmap(pdev, priv->hw_base);
6515 out_pci_release_regions:
6516 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07006517 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07006518 out_pci_disable_device:
6519 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006520 out_ieee80211_free_hw:
6521 ieee80211_free_hw(priv->hw);
6522 out:
6523 return err;
6524}
6525
Reinette Chatrec83dbf62008-03-21 13:53:41 -07006526static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006527{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006528 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006529 struct list_head *p, *q;
6530 int i;
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006531 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07006532
6533 if (!priv)
6534 return;
6535
6536 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
6537
Ron Rindjunskyc4f55232008-03-28 16:21:10 -07006538 if (priv->mac80211_registered) {
6539 ieee80211_unregister_hw(priv->hw);
6540 priv->mac80211_registered = 0;
6541 }
6542
Zhu Yib481de92007-09-25 17:54:57 -07006543 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08006544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006545 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006546
Mohamed Abbas0359fac2008-03-28 16:21:08 -07006547 /* make sure we flush any pending irq or
6548 * tasklet for the driver
6549 */
6550 spin_lock_irqsave(&priv->lock, flags);
6551 iwl4965_disable_interrupts(priv);
6552 spin_unlock_irqrestore(&priv->lock, flags);
6553
6554 iwl_synchronize_irq(priv);
6555
Zhu Yib481de92007-09-25 17:54:57 -07006556 /* Free MAC hash list for ADHOC */
6557 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
6558 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
6559 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006560 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07006561 }
6562 }
6563
Mohamed Abbasc8381fd2008-03-28 16:21:05 -07006564 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
Tomas Winkler712b6cf2008-03-12 16:58:52 -07006565 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006566 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07006567
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006568 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006569
6570 if (priv->rxq.bd)
Tomas Winklera55360e2008-05-05 10:22:28 +08006571 iwl_rx_queue_free(priv, &priv->rxq);
Ron Rindjunsky1053d352008-05-05 10:22:43 +08006572 iwl_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006573
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006574 iwlcore_clear_stations_table(priv);
Tomas Winkler073d3f52008-04-21 15:41:52 -07006575 iwl_eeprom_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006576
Zhu Yib481de92007-09-25 17:54:57 -07006577
Mohamed Abbas948c1712007-10-25 17:15:45 +08006578 /*netif_stop_queue(dev); */
6579 flush_workqueue(priv->workqueue);
6580
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006581 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07006582 * priv->workqueue... so we can't take down the workqueue
6583 * until now... */
6584 destroy_workqueue(priv->workqueue);
6585 priv->workqueue = NULL;
6586
Zhu Yib481de92007-09-25 17:54:57 -07006587 pci_iounmap(pdev, priv->hw_base);
6588 pci_release_regions(pdev);
6589 pci_disable_device(pdev);
6590 pci_set_drvdata(pdev, NULL);
6591
Assaf Kraussbf85ea42008-03-14 10:38:49 -07006592 iwl_free_channel_map(priv);
Ron Rindjunskyc7de35c2008-04-23 17:15:05 -07006593 iwlcore_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006594
6595 if (priv->ibss_beacon)
6596 dev_kfree_skb(priv->ibss_beacon);
6597
6598 ieee80211_free_hw(priv->hw);
6599}
6600
6601#ifdef CONFIG_PM
6602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006603static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07006604{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006605 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006606
Zhu Yie655b9f2008-01-24 02:19:38 -08006607 if (priv->is_open) {
6608 set_bit(STATUS_IN_SUSPEND, &priv->status);
6609 iwl4965_mac_stop(priv->hw);
6610 priv->is_open = 1;
6611 }
Zhu Yib481de92007-09-25 17:54:57 -07006612
Zhu Yib481de92007-09-25 17:54:57 -07006613 pci_set_power_state(pdev, PCI_D3hot);
6614
Zhu Yib481de92007-09-25 17:54:57 -07006615 return 0;
6616}
6617
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006618static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07006619{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006620 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07006621
Zhu Yib481de92007-09-25 17:54:57 -07006622 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07006623
Zhu Yie655b9f2008-01-24 02:19:38 -08006624 if (priv->is_open)
6625 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006626
Zhu Yie655b9f2008-01-24 02:19:38 -08006627 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006628 return 0;
6629}
6630
6631#endif /* CONFIG_PM */
6632
6633/*****************************************************************************
6634 *
6635 * driver and module entry point
6636 *
6637 *****************************************************************************/
6638
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006639/* Hardware specific file defines the PCI IDs table for that hardware module */
6640static struct pci_device_id iwl_hw_card_ids[] = {
6641 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
6642 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
Tomas Winkler5a6a2562008-04-24 11:55:23 -07006643#ifdef CONFIG_IWL5000
6644 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
6645 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
6646 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
6647#endif /* CONFIG_IWL5000 */
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006648 {0}
6649};
6650MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
6651
6652static struct pci_driver iwl_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07006653 .name = DRV_NAME,
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006654 .id_table = iwl_hw_card_ids,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006655 .probe = iwl4965_pci_probe,
6656 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07006657#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006658 .suspend = iwl4965_pci_suspend,
6659 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07006660#endif
6661};
6662
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006663static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07006664{
6665
6666 int ret;
6667 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
6668 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006669
6670 ret = iwl4965_rate_control_register();
6671 if (ret) {
6672 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
6673 return ret;
6674 }
6675
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006676 ret = pci_register_driver(&iwl_driver);
Zhu Yib481de92007-09-25 17:54:57 -07006677 if (ret) {
6678 IWL_ERROR("Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006679 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07006680 }
Zhu Yib481de92007-09-25 17:54:57 -07006681
6682 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006683
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006684error_register:
6685 iwl4965_rate_control_unregister();
6686 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006687}
6688
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006689static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07006690{
Ron Rindjunskyfed90172008-04-15 16:01:41 -07006691 pci_unregister_driver(&iwl_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07006692 iwl4965_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07006693}
6694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006695module_exit(iwl4965_exit);
6696module_init(iwl4965_init);