Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / net / wireless / iwlwifi / iwl4965-base.c
blobc9a0f774baf68f004b9f30776742db0966f14dad
1 /******************************************************************************
3 * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
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.
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.
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
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
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
28 *****************************************************************************/
30 #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>
41 #include <linux/etherdevice.h>
42 #include <linux/if_arp.h>
44 #include <net/mac80211.h>
46 #include <asm/div64.h>
48 #include "iwl-4965.h"
49 #include "iwl-helpers.h"
51 #ifdef CONFIG_IWL4965_DEBUG
52 u32 iwl4965_debug_level;
53 #endif
55 static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
56 struct iwl4965_tx_queue *txq);
58 /******************************************************************************
60 * module boiler plate
62 ******************************************************************************/
64 /* module parameters */
65 static int iwl4965_param_disable_hw_scan; /* def: 0 = use 4965's h/w scan */
66 static int iwl4965_param_debug; /* def: 0 = minimal debug log messages */
67 static int iwl4965_param_disable; /* def: enable radio */
68 static int iwl4965_param_antenna; /* def: 0 = both antennas (use diversity) */
69 int iwl4965_param_hwcrypto; /* def: using software encryption */
70 static int iwl4965_param_qos_enable = 1; /* def: 1 = use quality of service */
71 int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 16 Tx queues */
72 int iwl4965_param_amsdu_size_8K; /* def: enable 8K amsdu size */
75 * module name, copyright, version, etc.
76 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
79 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
81 #ifdef CONFIG_IWL4965_DEBUG
82 #define VD "d"
83 #else
84 #define VD
85 #endif
87 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
88 #define VS "s"
89 #else
90 #define VS
91 #endif
93 #define IWLWIFI_VERSION "1.2.23k" VD VS
94 #define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
95 #define DRV_VERSION IWLWIFI_VERSION
97 /* Change firmware file name, using "-" and incrementing number,
98 * *only* when uCode interface or architecture changes so that it
99 * is not compatible with earlier drivers.
100 * This number will also appear in << 8 position of 1st dword of uCode file */
101 #define IWL4965_UCODE_API "-1"
103 MODULE_DESCRIPTION(DRV_DESCRIPTION);
104 MODULE_VERSION(DRV_VERSION);
105 MODULE_AUTHOR(DRV_COPYRIGHT);
106 MODULE_LICENSE("GPL");
108 __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
110 u16 fc = le16_to_cpu(hdr->frame_control);
111 int hdr_len = ieee80211_get_hdrlen(fc);
113 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
114 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
115 return NULL;
118 static const struct ieee80211_hw_mode *iwl4965_get_hw_mode(
119 struct iwl4965_priv *priv, int mode)
121 int i;
123 for (i = 0; i < 3; i++)
124 if (priv->modes[i].mode == mode)
125 return &priv->modes[i];
127 return NULL;
130 static int iwl4965_is_empty_essid(const char *essid, int essid_len)
132 /* Single white space is for Linksys APs */
133 if (essid_len == 1 && essid[0] == ' ')
134 return 1;
136 /* Otherwise, if the entire essid is 0, we assume it is hidden */
137 while (essid_len) {
138 essid_len--;
139 if (essid[essid_len] != '\0')
140 return 0;
143 return 1;
146 static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
148 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
149 const char *s = essid;
150 char *d = escaped;
152 if (iwl4965_is_empty_essid(essid, essid_len)) {
153 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
154 return escaped;
157 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
158 while (essid_len--) {
159 if (*s == '\0') {
160 *d++ = '\\';
161 *d++ = '0';
162 s++;
163 } else
164 *d++ = *s++;
166 *d = '\0';
167 return escaped;
170 static void iwl4965_print_hex_dump(int level, void *p, u32 len)
172 #ifdef CONFIG_IWL4965_DEBUG
173 if (!(iwl4965_debug_level & level))
174 return;
176 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
177 p, len, 1);
178 #endif
181 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
182 * DMA services
184 * Theory of operation
186 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
187 * of buffer descriptors, each of which points to one or more data buffers for
188 * the device to read from or fill. Driver and device exchange status of each
189 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
190 * entries in each circular buffer, to protect against confusing empty and full
191 * queue states.
193 * The device reads or writes the data in the queues via the device's several
194 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
196 * For Tx queue, there are low mark and high mark limits. If, after queuing
197 * the packet for Tx, free space become < low mark, Tx queue stopped. When
198 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
199 * Tx queue resumed.
201 * The 4965 operates with up to 17 queues: One receive queue, one transmit
202 * queue (#4) for sending commands to the device firmware, and 15 other
203 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
205 * See more detailed info in iwl-4965-hw.h.
206 ***************************************************/
208 static int iwl4965_queue_space(const struct iwl4965_queue *q)
210 int s = q->read_ptr - q->write_ptr;
212 if (q->read_ptr > q->write_ptr)
213 s -= q->n_bd;
215 if (s <= 0)
216 s += q->n_window;
217 /* keep some reserve to not confuse empty and full situations */
218 s -= 2;
219 if (s < 0)
220 s = 0;
221 return s;
225 * iwl4965_queue_inc_wrap - increment queue index, wrap back to beginning
226 * @index -- current index
227 * @n_bd -- total number of entries in queue (must be power of 2)
229 static inline int iwl4965_queue_inc_wrap(int index, int n_bd)
231 return ++index & (n_bd - 1);
235 * iwl4965_queue_dec_wrap - decrement queue index, wrap back to end
236 * @index -- current index
237 * @n_bd -- total number of entries in queue (must be power of 2)
239 static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
241 return --index & (n_bd - 1);
244 static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
246 return q->write_ptr > q->read_ptr ?
247 (i >= q->read_ptr && i < q->write_ptr) :
248 !(i < q->read_ptr && i >= q->write_ptr);
251 static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
253 /* This is for scan command, the big buffer at end of command array */
254 if (is_huge)
255 return q->n_window; /* must be power of 2 */
257 /* Otherwise, use normal size buffers */
258 return index & (q->n_window - 1);
262 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
264 static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
265 int count, int slots_num, u32 id)
267 q->n_bd = count;
268 q->n_window = slots_num;
269 q->id = id;
271 /* count must be power-of-two size, otherwise iwl4965_queue_inc_wrap
272 * and iwl4965_queue_dec_wrap are broken. */
273 BUG_ON(!is_power_of_2(count));
275 /* slots_num must be power-of-two size, otherwise
276 * get_cmd_index is broken. */
277 BUG_ON(!is_power_of_2(slots_num));
279 q->low_mark = q->n_window / 4;
280 if (q->low_mark < 4)
281 q->low_mark = 4;
283 q->high_mark = q->n_window / 8;
284 if (q->high_mark < 2)
285 q->high_mark = 2;
287 q->write_ptr = q->read_ptr = 0;
289 return 0;
293 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
295 static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
296 struct iwl4965_tx_queue *txq, u32 id)
298 struct pci_dev *dev = priv->pci_dev;
300 /* Driver private data, only for Tx (not command) queues,
301 * not shared with device. */
302 if (id != IWL_CMD_QUEUE_NUM) {
303 txq->txb = kmalloc(sizeof(txq->txb[0]) *
304 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
305 if (!txq->txb) {
306 IWL_ERROR("kmalloc for auxiliary BD "
307 "structures failed\n");
308 goto error;
310 } else
311 txq->txb = NULL;
313 /* Circular buffer of transmit frame descriptors (TFDs),
314 * shared with device */
315 txq->bd = pci_alloc_consistent(dev,
316 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
317 &txq->q.dma_addr);
319 if (!txq->bd) {
320 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
321 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
322 goto error;
324 txq->q.id = id;
326 return 0;
328 error:
329 if (txq->txb) {
330 kfree(txq->txb);
331 txq->txb = NULL;
334 return -ENOMEM;
338 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
340 int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
341 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
343 struct pci_dev *dev = priv->pci_dev;
344 int len;
345 int rc = 0;
348 * Alloc buffer array for commands (Tx or other types of commands).
349 * For the command queue (#4), allocate command space + one big
350 * command for scan, since scan command is very huge; the system will
351 * not have two scans at the same time, so only one is needed.
352 * For normal Tx queues (all other queues), no super-size command
353 * space is needed.
355 len = sizeof(struct iwl4965_cmd) * slots_num;
356 if (txq_id == IWL_CMD_QUEUE_NUM)
357 len += IWL_MAX_SCAN_SIZE;
358 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
359 if (!txq->cmd)
360 return -ENOMEM;
362 /* Alloc driver data array and TFD circular buffer */
363 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
364 if (rc) {
365 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
367 return -ENOMEM;
369 txq->need_update = 0;
371 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
372 * iwl4965_queue_inc_wrap and iwl4965_queue_dec_wrap are broken. */
373 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
375 /* Initialize queue's high/low-water marks, and head/tail indexes */
376 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
378 /* Tell device where to find queue */
379 iwl4965_hw_tx_queue_init(priv, txq);
381 return 0;
385 * iwl4965_tx_queue_free - Deallocate DMA queue.
386 * @txq: Transmit queue to deallocate.
388 * Empty queue by removing and destroying all BD's.
389 * Free all buffers.
390 * 0-fill, but do not free "txq" descriptor structure.
392 void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
394 struct iwl4965_queue *q = &txq->q;
395 struct pci_dev *dev = priv->pci_dev;
396 int len;
398 if (q->n_bd == 0)
399 return;
401 /* first, empty all BD's */
402 for (; q->write_ptr != q->read_ptr;
403 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd))
404 iwl4965_hw_txq_free_tfd(priv, txq);
406 len = sizeof(struct iwl4965_cmd) * q->n_window;
407 if (q->id == IWL_CMD_QUEUE_NUM)
408 len += IWL_MAX_SCAN_SIZE;
410 /* De-alloc array of command/tx buffers */
411 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
413 /* De-alloc circular buffer of TFDs */
414 if (txq->q.n_bd)
415 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
416 txq->q.n_bd, txq->bd, txq->q.dma_addr);
418 /* De-alloc array of per-TFD driver data */
419 if (txq->txb) {
420 kfree(txq->txb);
421 txq->txb = NULL;
424 /* 0-fill queue descriptor structure */
425 memset(txq, 0, sizeof(*txq));
428 const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
430 /*************** STATION TABLE MANAGEMENT ****
431 * mac80211 should be examined to determine if sta_info is duplicating
432 * the functionality provided here
435 /**************************************************************/
437 #if 0 /* temporary disable till we add real remove station */
439 * iwl4965_remove_station - Remove driver's knowledge of station.
441 * NOTE: This does not remove station from device's station table.
443 static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
445 int index = IWL_INVALID_STATION;
446 int i;
447 unsigned long flags;
449 spin_lock_irqsave(&priv->sta_lock, flags);
451 if (is_ap)
452 index = IWL_AP_ID;
453 else if (is_broadcast_ether_addr(addr))
454 index = priv->hw_setting.bcast_sta_id;
455 else
456 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
457 if (priv->stations[i].used &&
458 !compare_ether_addr(priv->stations[i].sta.sta.addr,
459 addr)) {
460 index = i;
461 break;
464 if (unlikely(index == IWL_INVALID_STATION))
465 goto out;
467 if (priv->stations[index].used) {
468 priv->stations[index].used = 0;
469 priv->num_stations--;
472 BUG_ON(priv->num_stations < 0);
474 out:
475 spin_unlock_irqrestore(&priv->sta_lock, flags);
476 return 0;
478 #endif
481 * iwl4965_clear_stations_table - Clear the driver's station table
483 * NOTE: This does not clear or otherwise alter the device's station table.
485 static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
487 unsigned long flags;
489 spin_lock_irqsave(&priv->sta_lock, flags);
491 priv->num_stations = 0;
492 memset(priv->stations, 0, sizeof(priv->stations));
494 spin_unlock_irqrestore(&priv->sta_lock, flags);
498 * iwl4965_add_station_flags - Add station to tables in driver and device
500 u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr,
501 int is_ap, u8 flags, void *ht_data)
503 int i;
504 int index = IWL_INVALID_STATION;
505 struct iwl4965_station_entry *station;
506 unsigned long flags_spin;
507 DECLARE_MAC_BUF(mac);
509 spin_lock_irqsave(&priv->sta_lock, flags_spin);
510 if (is_ap)
511 index = IWL_AP_ID;
512 else if (is_broadcast_ether_addr(addr))
513 index = priv->hw_setting.bcast_sta_id;
514 else
515 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
516 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
517 addr)) {
518 index = i;
519 break;
522 if (!priv->stations[i].used &&
523 index == IWL_INVALID_STATION)
524 index = i;
528 /* These two conditions have the same outcome, but keep them separate
529 since they have different meanings */
530 if (unlikely(index == IWL_INVALID_STATION)) {
531 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
532 return index;
535 if (priv->stations[index].used &&
536 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
537 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
538 return index;
542 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
543 station = &priv->stations[index];
544 station->used = 1;
545 priv->num_stations++;
547 /* Set up the REPLY_ADD_STA command to send to device */
548 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
549 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
550 station->sta.mode = 0;
551 station->sta.sta.sta_id = index;
552 station->sta.station_flags = 0;
554 #ifdef CONFIG_IWL4965_HT
555 /* BCAST station and IBSS stations do not work in HT mode */
556 if (index != priv->hw_setting.bcast_sta_id &&
557 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
558 iwl4965_set_ht_add_station(priv, index,
559 (struct ieee80211_ht_info *) ht_data);
560 #endif /*CONFIG_IWL4965_HT*/
562 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
564 /* Add station to device's station table */
565 iwl4965_send_add_station(priv, &station->sta, flags);
566 return index;
570 /*************** DRIVER STATUS FUNCTIONS *****/
572 static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
574 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
575 * set but EXIT_PENDING is not */
576 return test_bit(STATUS_READY, &priv->status) &&
577 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
578 !test_bit(STATUS_EXIT_PENDING, &priv->status);
581 static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
583 return test_bit(STATUS_ALIVE, &priv->status);
586 static inline int iwl4965_is_init(struct iwl4965_priv *priv)
588 return test_bit(STATUS_INIT, &priv->status);
591 static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
593 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
594 test_bit(STATUS_RF_KILL_SW, &priv->status);
597 static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
600 if (iwl4965_is_rfkill(priv))
601 return 0;
603 return iwl4965_is_ready(priv);
606 /*************** HOST COMMAND QUEUE FUNCTIONS *****/
608 #define IWL_CMD(x) case x : return #x
610 static const char *get_cmd_string(u8 cmd)
612 switch (cmd) {
613 IWL_CMD(REPLY_ALIVE);
614 IWL_CMD(REPLY_ERROR);
615 IWL_CMD(REPLY_RXON);
616 IWL_CMD(REPLY_RXON_ASSOC);
617 IWL_CMD(REPLY_QOS_PARAM);
618 IWL_CMD(REPLY_RXON_TIMING);
619 IWL_CMD(REPLY_ADD_STA);
620 IWL_CMD(REPLY_REMOVE_STA);
621 IWL_CMD(REPLY_REMOVE_ALL_STA);
622 IWL_CMD(REPLY_TX);
623 IWL_CMD(REPLY_RATE_SCALE);
624 IWL_CMD(REPLY_LEDS_CMD);
625 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
626 IWL_CMD(RADAR_NOTIFICATION);
627 IWL_CMD(REPLY_QUIET_CMD);
628 IWL_CMD(REPLY_CHANNEL_SWITCH);
629 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
630 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
631 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
632 IWL_CMD(POWER_TABLE_CMD);
633 IWL_CMD(PM_SLEEP_NOTIFICATION);
634 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
635 IWL_CMD(REPLY_SCAN_CMD);
636 IWL_CMD(REPLY_SCAN_ABORT_CMD);
637 IWL_CMD(SCAN_START_NOTIFICATION);
638 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
639 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
640 IWL_CMD(BEACON_NOTIFICATION);
641 IWL_CMD(REPLY_TX_BEACON);
642 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
643 IWL_CMD(QUIET_NOTIFICATION);
644 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
645 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
646 IWL_CMD(REPLY_BT_CONFIG);
647 IWL_CMD(REPLY_STATISTICS_CMD);
648 IWL_CMD(STATISTICS_NOTIFICATION);
649 IWL_CMD(REPLY_CARD_STATE_CMD);
650 IWL_CMD(CARD_STATE_NOTIFICATION);
651 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
652 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
653 IWL_CMD(SENSITIVITY_CMD);
654 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
655 IWL_CMD(REPLY_RX_PHY_CMD);
656 IWL_CMD(REPLY_RX_MPDU_CMD);
657 IWL_CMD(REPLY_4965_RX);
658 IWL_CMD(REPLY_COMPRESSED_BA);
659 default:
660 return "UNKNOWN";
665 #define HOST_COMPLETE_TIMEOUT (HZ / 2)
668 * iwl4965_enqueue_hcmd - enqueue a uCode command
669 * @priv: device private data point
670 * @cmd: a point to the ucode command structure
672 * The function returns < 0 values to indicate the operation is
673 * failed. On success, it turns the index (> 0) of command in the
674 * command queue.
676 static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
678 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
679 struct iwl4965_queue *q = &txq->q;
680 struct iwl4965_tfd_frame *tfd;
681 u32 *control_flags;
682 struct iwl4965_cmd *out_cmd;
683 u32 idx;
684 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
685 dma_addr_t phys_addr;
686 int ret;
687 unsigned long flags;
689 /* If any of the command structures end up being larger than
690 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
691 * we will need to increase the size of the TFD entries */
692 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
693 !(cmd->meta.flags & CMD_SIZE_HUGE));
695 <<<<<<< HEAD:drivers/net/wireless/iwlwifi/iwl4965-base.c
696 =======
697 if (iwl4965_is_rfkill(priv)) {
698 IWL_DEBUG_INFO("Not sending command - RF KILL");
699 return -EIO;
702 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/iwlwifi/iwl4965-base.c
703 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
704 IWL_ERROR("No space for Tx\n");
705 return -ENOSPC;
708 spin_lock_irqsave(&priv->hcmd_lock, flags);
710 tfd = &txq->bd[q->write_ptr];
711 memset(tfd, 0, sizeof(*tfd));
713 control_flags = (u32 *) tfd;
715 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
716 out_cmd = &txq->cmd[idx];
718 out_cmd->hdr.cmd = cmd->id;
719 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
720 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
722 /* At this point, the out_cmd now has all of the incoming cmd
723 * information */
725 out_cmd->hdr.flags = 0;
726 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
727 INDEX_TO_SEQ(q->write_ptr));
728 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
729 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
731 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
732 offsetof(struct iwl4965_cmd, hdr);
733 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
735 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
736 "%d bytes at %d[%d]:%d\n",
737 get_cmd_string(out_cmd->hdr.cmd),
738 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
739 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
741 txq->need_update = 1;
743 /* Set up entry in queue's byte count circular buffer */
744 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
746 /* Increment and update queue's write index */
747 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
748 iwl4965_tx_queue_update_write_ptr(priv, txq);
750 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
751 return ret ? ret : idx;
754 static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
756 int ret;
758 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
760 /* An asynchronous command can not expect an SKB to be set. */
761 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
763 /* An asynchronous command MUST have a callback. */
764 BUG_ON(!cmd->meta.u.callback);
766 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
767 return -EBUSY;
769 ret = iwl4965_enqueue_hcmd(priv, cmd);
770 if (ret < 0) {
771 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
772 get_cmd_string(cmd->id), ret);
773 return ret;
775 return 0;
778 static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
780 int cmd_idx;
781 int ret;
782 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
784 BUG_ON(cmd->meta.flags & CMD_ASYNC);
786 /* A synchronous command can not have a callback set. */
787 BUG_ON(cmd->meta.u.callback != NULL);
789 if (atomic_xchg(&entry, 1)) {
790 IWL_ERROR("Error sending %s: Already sending a host command\n",
791 get_cmd_string(cmd->id));
792 return -EBUSY;
795 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
797 if (cmd->meta.flags & CMD_WANT_SKB)
798 cmd->meta.source = &cmd->meta;
800 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
801 if (cmd_idx < 0) {
802 ret = cmd_idx;
803 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
804 get_cmd_string(cmd->id), ret);
805 goto out;
808 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
809 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
810 HOST_COMPLETE_TIMEOUT);
811 if (!ret) {
812 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
813 IWL_ERROR("Error sending %s: time out after %dms.\n",
814 get_cmd_string(cmd->id),
815 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
817 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
818 ret = -ETIMEDOUT;
819 goto cancel;
823 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
824 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
825 get_cmd_string(cmd->id));
826 ret = -ECANCELED;
827 goto fail;
829 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
830 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
831 get_cmd_string(cmd->id));
832 ret = -EIO;
833 goto fail;
835 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
836 IWL_ERROR("Error: Response NULL in '%s'\n",
837 get_cmd_string(cmd->id));
838 ret = -EIO;
839 goto out;
842 ret = 0;
843 goto out;
845 cancel:
846 if (cmd->meta.flags & CMD_WANT_SKB) {
847 struct iwl4965_cmd *qcmd;
849 /* Cancel the CMD_WANT_SKB flag for the cmd in the
850 * TX cmd queue. Otherwise in case the cmd comes
851 * in later, it will possibly set an invalid
852 * address (cmd->meta.source). */
853 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
854 qcmd->meta.flags &= ~CMD_WANT_SKB;
856 fail:
857 if (cmd->meta.u.skb) {
858 dev_kfree_skb_any(cmd->meta.u.skb);
859 cmd->meta.u.skb = NULL;
861 out:
862 atomic_set(&entry, 0);
863 return ret;
866 int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
868 if (cmd->meta.flags & CMD_ASYNC)
869 return iwl4965_send_cmd_async(priv, cmd);
871 return iwl4965_send_cmd_sync(priv, cmd);
874 int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
876 struct iwl4965_host_cmd cmd = {
877 .id = id,
878 .len = len,
879 .data = data,
882 return iwl4965_send_cmd_sync(priv, &cmd);
885 static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
887 struct iwl4965_host_cmd cmd = {
888 .id = id,
889 .len = sizeof(val),
890 .data = &val,
893 return iwl4965_send_cmd_sync(priv, &cmd);
896 int iwl4965_send_statistics_request(struct iwl4965_priv *priv)
898 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
902 * iwl4965_rxon_add_station - add station into station table.
904 * there is only one AP station with id= IWL_AP_ID
905 * NOTE: mutex must be held before calling this fnction
907 static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
908 const u8 *addr, int is_ap)
910 u8 sta_id;
912 /* Add station to device's station table */
913 #ifdef CONFIG_IWL4965_HT
914 struct ieee80211_conf *conf = &priv->hw->conf;
915 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
917 if ((is_ap) &&
918 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
919 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
920 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
921 0, cur_ht_config);
922 else
923 #endif /* CONFIG_IWL4965_HT */
924 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
925 0, NULL);
927 /* Set up default rate scaling table in device's station table */
928 iwl4965_add_station(priv, addr, is_ap);
930 return sta_id;
934 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
935 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
936 * @channel: Any channel valid for the requested phymode
938 * In addition to setting the staging RXON, priv->phymode is also set.
940 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
941 * in the staging RXON flag structure based on the phymode
943 static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv, u8 phymode,
944 u16 channel)
946 if (!iwl4965_get_channel_info(priv, phymode, channel)) {
947 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
948 channel, phymode);
949 return -EINVAL;
952 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
953 (priv->phymode == phymode))
954 return 0;
956 priv->staging_rxon.channel = cpu_to_le16(channel);
957 if (phymode == MODE_IEEE80211A)
958 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
959 else
960 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
962 priv->phymode = phymode;
964 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
966 return 0;
970 * iwl4965_check_rxon_cmd - validate RXON structure is valid
972 * NOTE: This is really only useful during development and can eventually
973 * be #ifdef'd out once the driver is stable and folks aren't actively
974 * making changes
976 static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
978 int error = 0;
979 int counter = 1;
981 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
982 error |= le32_to_cpu(rxon->flags &
983 (RXON_FLG_TGJ_NARROW_BAND_MSK |
984 RXON_FLG_RADAR_DETECT_MSK));
985 if (error)
986 IWL_WARNING("check 24G fields %d | %d\n",
987 counter++, error);
988 } else {
989 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
990 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
991 if (error)
992 IWL_WARNING("check 52 fields %d | %d\n",
993 counter++, error);
994 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
995 if (error)
996 IWL_WARNING("check 52 CCK %d | %d\n",
997 counter++, error);
999 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
1000 if (error)
1001 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
1003 /* make sure basic rates 6Mbps and 1Mbps are supported */
1004 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
1005 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
1006 if (error)
1007 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
1009 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
1010 if (error)
1011 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
1013 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
1014 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
1015 if (error)
1016 IWL_WARNING("check CCK and short slot %d | %d\n",
1017 counter++, error);
1019 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
1020 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
1021 if (error)
1022 IWL_WARNING("check CCK & auto detect %d | %d\n",
1023 counter++, error);
1025 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
1026 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
1027 if (error)
1028 IWL_WARNING("check TGG and auto detect %d | %d\n",
1029 counter++, error);
1031 if (error)
1032 IWL_WARNING("Tuning to channel %d\n",
1033 le16_to_cpu(rxon->channel));
1035 if (error) {
1036 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
1037 return -1;
1039 return 0;
1043 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
1044 * @priv: staging_rxon is compared to active_rxon
1046 * If the RXON structure is changing enough to require a new tune,
1047 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
1048 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
1050 static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
1053 /* These items are only settable from the full RXON command */
1054 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1055 compare_ether_addr(priv->staging_rxon.bssid_addr,
1056 priv->active_rxon.bssid_addr) ||
1057 compare_ether_addr(priv->staging_rxon.node_addr,
1058 priv->active_rxon.node_addr) ||
1059 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1060 priv->active_rxon.wlap_bssid_addr) ||
1061 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1062 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1063 (priv->staging_rxon.air_propagation !=
1064 priv->active_rxon.air_propagation) ||
1065 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
1066 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
1067 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
1068 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1069 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1070 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1071 return 1;
1073 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1074 * be updated with the RXON_ASSOC command -- however only some
1075 * flag transitions are allowed using RXON_ASSOC */
1077 /* Check if we are not switching bands */
1078 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1079 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1080 return 1;
1082 /* Check if we are switching association toggle */
1083 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1084 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1085 return 1;
1087 return 0;
1090 static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
1092 int rc = 0;
1093 struct iwl4965_rx_packet *res = NULL;
1094 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1095 struct iwl4965_host_cmd cmd = {
1096 .id = REPLY_RXON_ASSOC,
1097 .len = sizeof(rxon_assoc),
1098 .meta.flags = CMD_WANT_SKB,
1099 .data = &rxon_assoc,
1101 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1102 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
1104 if ((rxon1->flags == rxon2->flags) &&
1105 (rxon1->filter_flags == rxon2->filter_flags) &&
1106 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1107 (rxon1->ofdm_ht_single_stream_basic_rates ==
1108 rxon2->ofdm_ht_single_stream_basic_rates) &&
1109 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1110 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1111 (rxon1->rx_chain == rxon2->rx_chain) &&
1112 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1113 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1114 return 0;
1117 rxon_assoc.flags = priv->staging_rxon.flags;
1118 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1119 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1120 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1121 rxon_assoc.reserved = 0;
1122 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1123 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1124 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1125 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1126 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1128 rc = iwl4965_send_cmd_sync(priv, &cmd);
1129 if (rc)
1130 return rc;
1132 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
1133 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1134 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1135 rc = -EIO;
1138 priv->alloc_rxb_skb--;
1139 dev_kfree_skb_any(cmd.meta.u.skb);
1141 return rc;
1145 * iwl4965_commit_rxon - commit staging_rxon to hardware
1147 * The RXON command in staging_rxon is committed to the hardware and
1148 * the active_rxon structure is updated with the new data. This
1149 * function correctly transitions out of the RXON_ASSOC_MSK state if
1150 * a HW tune is required based on the RXON structure changes.
1152 static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
1154 /* cast away the const for active_rxon in this function */
1155 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
1156 DECLARE_MAC_BUF(mac);
1157 int rc = 0;
1159 if (!iwl4965_is_alive(priv))
1160 return -1;
1162 /* always get timestamp with Rx frame */
1163 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1165 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
1166 if (rc) {
1167 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1168 return -EINVAL;
1171 /* If we don't need to send a full RXON, we can use
1172 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
1173 * and other flags for the current radio configuration. */
1174 if (!iwl4965_full_rxon_required(priv)) {
1175 rc = iwl4965_send_rxon_assoc(priv);
1176 if (rc) {
1177 IWL_ERROR("Error setting RXON_ASSOC "
1178 "configuration (%d).\n", rc);
1179 return rc;
1182 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1184 return 0;
1187 /* station table will be cleared */
1188 priv->assoc_station_added = 0;
1190 #ifdef CONFIG_IWL4965_SENSITIVITY
1191 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1192 if (!priv->error_recovering)
1193 priv->start_calib = 0;
1195 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1196 #endif /* CONFIG_IWL4965_SENSITIVITY */
1198 /* If we are currently associated and the new config requires
1199 * an RXON_ASSOC and the new config wants the associated mask enabled,
1200 * we must clear the associated from the active configuration
1201 * before we apply the new config */
1202 if (iwl4965_is_associated(priv) &&
1203 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1204 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1205 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1207 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1208 sizeof(struct iwl4965_rxon_cmd),
1209 &priv->active_rxon);
1211 /* If the mask clearing failed then we set
1212 * active_rxon back to what it was previously */
1213 if (rc) {
1214 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1215 IWL_ERROR("Error clearing ASSOC_MSK on current "
1216 "configuration (%d).\n", rc);
1217 return rc;
1221 IWL_DEBUG_INFO("Sending RXON\n"
1222 "* with%s RXON_FILTER_ASSOC_MSK\n"
1223 "* channel = %d\n"
1224 "* bssid = %s\n",
1225 ((priv->staging_rxon.filter_flags &
1226 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1227 le16_to_cpu(priv->staging_rxon.channel),
1228 print_mac(mac, priv->staging_rxon.bssid_addr));
1230 /* Apply the new configuration */
1231 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1232 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
1233 if (rc) {
1234 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1235 return rc;
1238 iwl4965_clear_stations_table(priv);
1240 #ifdef CONFIG_IWL4965_SENSITIVITY
1241 if (!priv->error_recovering)
1242 priv->start_calib = 0;
1244 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1245 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1246 #endif /* CONFIG_IWL4965_SENSITIVITY */
1248 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1250 /* If we issue a new RXON command which required a tune then we must
1251 * send a new TXPOWER command or we won't be able to Tx any frames */
1252 rc = iwl4965_hw_reg_send_txpower(priv);
1253 if (rc) {
1254 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1255 return rc;
1258 /* Add the broadcast address so we can send broadcast frames */
1259 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
1260 IWL_INVALID_STATION) {
1261 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1262 return -EIO;
1265 /* If we have set the ASSOC_MSK and we are in BSS mode then
1266 * add the IWL_AP_ID to the station rate table */
1267 if (iwl4965_is_associated(priv) &&
1268 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
1269 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
1270 == IWL_INVALID_STATION) {
1271 IWL_ERROR("Error adding AP address for transmit.\n");
1272 return -EIO;
1274 priv->assoc_station_added = 1;
1277 return 0;
1280 static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
1282 struct iwl4965_bt_cmd bt_cmd = {
1283 .flags = 3,
1284 .lead_time = 0xAA,
1285 .max_kill = 1,
1286 .kill_ack_mask = 0,
1287 .kill_cts_mask = 0,
1290 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1291 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
1294 static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
1296 int rc = 0;
1297 struct iwl4965_rx_packet *res;
1298 struct iwl4965_host_cmd cmd = {
1299 .id = REPLY_SCAN_ABORT_CMD,
1300 .meta.flags = CMD_WANT_SKB,
1303 /* If there isn't a scan actively going on in the hardware
1304 * then we are in between scan bands and not actually
1305 * actively scanning, so don't send the abort command */
1306 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1307 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1308 return 0;
1311 rc = iwl4965_send_cmd_sync(priv, &cmd);
1312 if (rc) {
1313 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1314 return rc;
1317 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
1318 if (res->u.status != CAN_ABORT_STATUS) {
1319 /* The scan abort will return 1 for success or
1320 * 2 for "failure". A failure condition can be
1321 * due to simply not being in an active scan which
1322 * can occur if we send the scan abort before we
1323 * the microcode has notified us that a scan is
1324 * completed. */
1325 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1326 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1327 clear_bit(STATUS_SCAN_HW, &priv->status);
1330 dev_kfree_skb_any(cmd.meta.u.skb);
1332 return rc;
1335 static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
1336 struct iwl4965_cmd *cmd,
1337 struct sk_buff *skb)
1339 return 1;
1343 * CARD_STATE_CMD
1345 * Use: Sets the device's internal card state to enable, disable, or halt
1347 * When in the 'enable' state the card operates as normal.
1348 * When in the 'disable' state, the card enters into a low power mode.
1349 * When in the 'halt' state, the card is shut down and must be fully
1350 * restarted to come back on.
1352 static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
1354 struct iwl4965_host_cmd cmd = {
1355 .id = REPLY_CARD_STATE_CMD,
1356 .len = sizeof(u32),
1357 .data = &flags,
1358 .meta.flags = meta_flag,
1361 if (meta_flag & CMD_ASYNC)
1362 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
1364 return iwl4965_send_cmd(priv, &cmd);
1367 static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
1368 struct iwl4965_cmd *cmd, struct sk_buff *skb)
1370 struct iwl4965_rx_packet *res = NULL;
1372 if (!skb) {
1373 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1374 return 1;
1377 res = (struct iwl4965_rx_packet *)skb->data;
1378 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1379 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1380 res->hdr.flags);
1381 return 1;
1384 switch (res->u.add_sta.status) {
1385 case ADD_STA_SUCCESS_MSK:
1386 break;
1387 default:
1388 break;
1391 /* We didn't cache the SKB; let the caller free it */
1392 return 1;
1395 int iwl4965_send_add_station(struct iwl4965_priv *priv,
1396 struct iwl4965_addsta_cmd *sta, u8 flags)
1398 struct iwl4965_rx_packet *res = NULL;
1399 int rc = 0;
1400 struct iwl4965_host_cmd cmd = {
1401 .id = REPLY_ADD_STA,
1402 .len = sizeof(struct iwl4965_addsta_cmd),
1403 .meta.flags = flags,
1404 .data = sta,
1407 if (flags & CMD_ASYNC)
1408 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
1409 else
1410 cmd.meta.flags |= CMD_WANT_SKB;
1412 rc = iwl4965_send_cmd(priv, &cmd);
1414 if (rc || (flags & CMD_ASYNC))
1415 return rc;
1417 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
1418 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1419 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1420 res->hdr.flags);
1421 rc = -EIO;
1424 if (rc == 0) {
1425 switch (res->u.add_sta.status) {
1426 case ADD_STA_SUCCESS_MSK:
1427 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1428 break;
1429 default:
1430 rc = -EIO;
1431 IWL_WARNING("REPLY_ADD_STA failed\n");
1432 break;
1436 priv->alloc_rxb_skb--;
1437 dev_kfree_skb_any(cmd.meta.u.skb);
1439 return rc;
1442 static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
1443 struct ieee80211_key_conf *keyconf,
1444 u8 sta_id)
1446 unsigned long flags;
1447 __le16 key_flags = 0;
1449 switch (keyconf->alg) {
1450 case ALG_CCMP:
1451 key_flags |= STA_KEY_FLG_CCMP;
1452 key_flags |= cpu_to_le16(
1453 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1454 key_flags &= ~STA_KEY_FLG_INVALID;
1455 break;
1456 case ALG_TKIP:
1457 case ALG_WEP:
1458 default:
1459 return -EINVAL;
1461 spin_lock_irqsave(&priv->sta_lock, flags);
1462 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1463 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1464 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1465 keyconf->keylen);
1467 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1468 keyconf->keylen);
1469 priv->stations[sta_id].sta.key.key_flags = key_flags;
1470 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1471 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1473 spin_unlock_irqrestore(&priv->sta_lock, flags);
1475 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
1476 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1477 return 0;
1480 static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
1482 unsigned long flags;
1484 spin_lock_irqsave(&priv->sta_lock, flags);
1485 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1486 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
1487 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1488 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1489 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1490 spin_unlock_irqrestore(&priv->sta_lock, flags);
1492 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
1493 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1494 return 0;
1497 static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
1499 struct list_head *element;
1501 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1502 priv->frames_count);
1504 while (!list_empty(&priv->free_frames)) {
1505 element = priv->free_frames.next;
1506 list_del(element);
1507 kfree(list_entry(element, struct iwl4965_frame, list));
1508 priv->frames_count--;
1511 if (priv->frames_count) {
1512 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1513 priv->frames_count);
1514 priv->frames_count = 0;
1518 static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
1520 struct iwl4965_frame *frame;
1521 struct list_head *element;
1522 if (list_empty(&priv->free_frames)) {
1523 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1524 if (!frame) {
1525 IWL_ERROR("Could not allocate frame!\n");
1526 return NULL;
1529 priv->frames_count++;
1530 return frame;
1533 element = priv->free_frames.next;
1534 list_del(element);
1535 return list_entry(element, struct iwl4965_frame, list);
1538 static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
1540 memset(frame, 0, sizeof(*frame));
1541 list_add(&frame->list, &priv->free_frames);
1544 unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
1545 struct ieee80211_hdr *hdr,
1546 const u8 *dest, int left)
1549 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
1550 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1551 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1552 return 0;
1554 if (priv->ibss_beacon->len > left)
1555 return 0;
1557 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1559 return priv->ibss_beacon->len;
1562 int iwl4965_rate_index_from_plcp(int plcp)
1564 int i = 0;
1566 /* 4965 HT rate format */
1567 if (plcp & RATE_MCS_HT_MSK) {
1568 i = (plcp & 0xff);
1570 if (i >= IWL_RATE_MIMO_6M_PLCP)
1571 i = i - IWL_RATE_MIMO_6M_PLCP;
1573 i += IWL_FIRST_OFDM_RATE;
1574 /* skip 9M not supported in ht*/
1575 if (i >= IWL_RATE_9M_INDEX)
1576 i += 1;
1577 if ((i >= IWL_FIRST_OFDM_RATE) &&
1578 (i <= IWL_LAST_OFDM_RATE))
1579 return i;
1581 /* 4965 legacy rate format, search for match in table */
1582 } else {
1583 for (i = 0; i < ARRAY_SIZE(iwl4965_rates); i++)
1584 if (iwl4965_rates[i].plcp == (plcp &0xFF))
1585 return i;
1587 return -1;
1590 static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
1592 u8 i;
1594 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1595 i = iwl4965_rates[i].next_ieee) {
1596 if (rate_mask & (1 << i))
1597 return iwl4965_rates[i].plcp;
1600 return IWL_RATE_INVALID;
1603 static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
1605 struct iwl4965_frame *frame;
1606 unsigned int frame_size;
1607 int rc;
1608 u8 rate;
1610 frame = iwl4965_get_free_frame(priv);
1612 if (!frame) {
1613 IWL_ERROR("Could not obtain free frame buffer for beacon "
1614 "command.\n");
1615 return -ENOMEM;
1618 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1619 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
1620 0xFF0);
1621 if (rate == IWL_INVALID_RATE)
1622 rate = IWL_RATE_6M_PLCP;
1623 } else {
1624 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
1625 if (rate == IWL_INVALID_RATE)
1626 rate = IWL_RATE_1M_PLCP;
1629 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
1631 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1632 &frame->u.cmd[0]);
1634 iwl4965_free_frame(priv, frame);
1636 return rc;
1639 /******************************************************************************
1641 * EEPROM related functions
1643 ******************************************************************************/
1645 static void get_eeprom_mac(struct iwl4965_priv *priv, u8 *mac)
1647 memcpy(mac, priv->eeprom.mac_address, 6);
1650 static inline void iwl4965_eeprom_release_semaphore(struct iwl4965_priv *priv)
1652 iwl4965_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
1653 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
1657 * iwl4965_eeprom_init - read EEPROM contents
1659 * Load the EEPROM contents from adapter into priv->eeprom
1661 * NOTE: This routine uses the non-debug IO access functions.
1663 int iwl4965_eeprom_init(struct iwl4965_priv *priv)
1665 <<<<<<< HEAD:drivers/net/wireless/iwlwifi/iwl4965-base.c
1666 __le16 *e = (__le16 *)&priv->eeprom;
1667 =======
1668 u16 *e = (u16 *)&priv->eeprom;
1669 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/iwlwifi/iwl4965-base.c
1670 u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
1671 u32 r;
1672 int sz = sizeof(priv->eeprom);
1673 int rc;
1674 int i;
1675 u16 addr;
1677 /* The EEPROM structure has several padding buffers within it
1678 * and when adding new EEPROM maps is subject to programmer errors
1679 * which may be very difficult to identify without explicitly
1680 * checking the resulting size of the eeprom map. */
1681 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1683 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1684 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1685 return -ENOENT;
1688 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
1689 rc = iwl4965_eeprom_acquire_semaphore(priv);
1690 if (rc < 0) {
1691 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
1692 return -ENOENT;
1695 /* eeprom is an array of 16bit values */
1696 for (addr = 0; addr < sz; addr += sizeof(u16)) {
1697 _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
1698 _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1700 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1701 i += IWL_EEPROM_ACCESS_DELAY) {
1702 r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
1703 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1704 break;
1705 udelay(IWL_EEPROM_ACCESS_DELAY);
1708 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1709 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1710 rc = -ETIMEDOUT;
1711 goto done;
1713 <<<<<<< HEAD:drivers/net/wireless/iwlwifi/iwl4965-base.c
1714 e[addr / 2] = cpu_to_le16(r >> 16);
1715 =======
1716 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
1717 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/iwlwifi/iwl4965-base.c
1719 rc = 0;
1721 done:
1722 iwl4965_eeprom_release_semaphore(priv);
1723 return rc;
1726 /******************************************************************************
1728 * Misc. internal state and helper functions
1730 ******************************************************************************/
1731 #ifdef CONFIG_IWL4965_DEBUG
1734 * iwl4965_report_frame - dump frame to syslog during debug sessions
1736 * You may hack this function to show different aspects of received frames,
1737 * including selective frame dumps.
1738 * group100 parameter selects whether to show 1 out of 100 good frames.
1740 * TODO: This was originally written for 3945, need to audit for
1741 * proper operation with 4965.
1743 void iwl4965_report_frame(struct iwl4965_priv *priv,
1744 struct iwl4965_rx_packet *pkt,
1745 struct ieee80211_hdr *header, int group100)
1747 u32 to_us;
1748 u32 print_summary = 0;
1749 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1750 u32 hundred = 0;
1751 u32 dataframe = 0;
1752 u16 fc;
1753 u16 seq_ctl;
1754 u16 channel;
1755 u16 phy_flags;
1756 int rate_sym;
1757 u16 length;
1758 u16 status;
1759 u16 bcn_tmr;
1760 u32 tsf_low;
1761 u64 tsf;
1762 u8 rssi;
1763 u8 agc;
1764 u16 sig_avg;
1765 u16 noise_diff;
1766 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1767 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1768 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
1769 u8 *data = IWL_RX_DATA(pkt);
1771 /* MAC header */
1772 fc = le16_to_cpu(header->frame_control);
1773 seq_ctl = le16_to_cpu(header->seq_ctrl);
1775 /* metadata */
1776 channel = le16_to_cpu(rx_hdr->channel);
1777 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1778 rate_sym = rx_hdr->rate;
1779 length = le16_to_cpu(rx_hdr->len);
1781 /* end-of-frame status and timestamp */
1782 status = le32_to_cpu(rx_end->status);
1783 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1784 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1785 tsf = le64_to_cpu(rx_end->timestamp);
1787 /* signal statistics */
1788 rssi = rx_stats->rssi;
1789 agc = rx_stats->agc;
1790 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1791 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1793 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1795 /* if data frame is to us and all is good,
1796 * (optionally) print summary for only 1 out of every 100 */
1797 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1798 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1799 dataframe = 1;
1800 if (!group100)
1801 print_summary = 1; /* print each frame */
1802 else if (priv->framecnt_to_us < 100) {
1803 priv->framecnt_to_us++;
1804 print_summary = 0;
1805 } else {
1806 priv->framecnt_to_us = 0;
1807 print_summary = 1;
1808 hundred = 1;
1810 } else {
1811 /* print summary for all other frames */
1812 print_summary = 1;
1815 if (print_summary) {
1816 char *title;
1817 u32 rate;
1819 if (hundred)
1820 title = "100Frames";
1821 else if (fc & IEEE80211_FCTL_RETRY)
1822 title = "Retry";
1823 else if (ieee80211_is_assoc_response(fc))
1824 title = "AscRsp";
1825 else if (ieee80211_is_reassoc_response(fc))
1826 title = "RasRsp";
1827 else if (ieee80211_is_probe_response(fc)) {
1828 title = "PrbRsp";
1829 print_dump = 1; /* dump frame contents */
1830 } else if (ieee80211_is_beacon(fc)) {
1831 title = "Beacon";
1832 print_dump = 1; /* dump frame contents */
1833 } else if (ieee80211_is_atim(fc))
1834 title = "ATIM";
1835 else if (ieee80211_is_auth(fc))
1836 title = "Auth";
1837 else if (ieee80211_is_deauth(fc))
1838 title = "DeAuth";
1839 else if (ieee80211_is_disassoc(fc))
1840 title = "DisAssoc";
1841 else
1842 title = "Frame";
1844 rate = iwl4965_rate_index_from_plcp(rate_sym);
1845 if (rate == -1)
1846 rate = 0;
1847 else
1848 rate = iwl4965_rates[rate].ieee / 2;
1850 /* print frame summary.
1851 * MAC addresses show just the last byte (for brevity),
1852 * but you can hack it to show more, if you'd like to. */
1853 if (dataframe)
1854 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1855 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1856 title, fc, header->addr1[5],
1857 length, rssi, channel, rate);
1858 else {
1859 /* src/dst addresses assume managed mode */
1860 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1861 "src=0x%02x, rssi=%u, tim=%lu usec, "
1862 "phy=0x%02x, chnl=%d\n",
1863 title, fc, header->addr1[5],
1864 header->addr3[5], rssi,
1865 tsf_low - priv->scan_start_tsf,
1866 phy_flags, channel);
1869 if (print_dump)
1870 iwl4965_print_hex_dump(IWL_DL_RX, data, length);
1872 #endif
1874 static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
1876 if (priv->hw_setting.shared_virt)
1877 pci_free_consistent(priv->pci_dev,
1878 sizeof(struct iwl4965_shared),
1879 priv->hw_setting.shared_virt,
1880 priv->hw_setting.shared_phys);
1884 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
1886 * return : set the bit for each supported rate insert in ie
1888 static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1889 u16 basic_rate, int *left)
1891 u16 ret_rates = 0, bit;
1892 int i;
1893 u8 *cnt = ie;
1894 u8 *rates = ie + 1;
1896 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1897 if (bit & supported_rate) {
1898 ret_rates |= bit;
1899 rates[*cnt] = iwl4965_rates[i].ieee |
1900 ((bit & basic_rate) ? 0x80 : 0x00);
1901 (*cnt)++;
1902 (*left)--;
1903 if ((*left <= 0) ||
1904 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1905 break;
1909 return ret_rates;
1912 #ifdef CONFIG_IWL4965_HT
1913 void static iwl4965_set_ht_capab(struct ieee80211_hw *hw,
1914 struct ieee80211_ht_cap *ht_cap,
1915 u8 use_current_config);
1916 #endif
1919 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
1921 static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
1922 struct ieee80211_mgmt *frame,
1923 int left, int is_direct)
1925 int len = 0;
1926 u8 *pos = NULL;
1927 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
1928 #ifdef CONFIG_IWL4965_HT
1929 struct ieee80211_hw_mode *mode;
1930 #endif /* CONFIG_IWL4965_HT */
1932 /* Make sure there is enough space for the probe request,
1933 * two mandatory IEs and the data */
1934 left -= 24;
1935 if (left < 0)
1936 return 0;
1937 len += 24;
1939 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1940 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
1941 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1942 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
1943 frame->seq_ctrl = 0;
1945 /* fill in our indirect SSID IE */
1946 /* ...next IE... */
1948 left -= 2;
1949 if (left < 0)
1950 return 0;
1951 len += 2;
1952 pos = &(frame->u.probe_req.variable[0]);
1953 *pos++ = WLAN_EID_SSID;
1954 *pos++ = 0;
1956 /* fill in our direct SSID IE... */
1957 if (is_direct) {
1958 /* ...next IE... */
1959 left -= 2 + priv->essid_len;
1960 if (left < 0)
1961 return 0;
1962 /* ... fill it in... */
1963 *pos++ = WLAN_EID_SSID;
1964 *pos++ = priv->essid_len;
1965 memcpy(pos, priv->essid, priv->essid_len);
1966 pos += priv->essid_len;
1967 len += 2 + priv->essid_len;
1970 /* fill in supported rate */
1971 /* ...next IE... */
1972 left -= 2;
1973 if (left < 0)
1974 return 0;
1976 /* ... fill it in... */
1977 *pos++ = WLAN_EID_SUPP_RATES;
1978 *pos = 0;
1980 /* exclude 60M rate */
1981 active_rates = priv->rates_mask;
1982 active_rates &= ~IWL_RATE_60M_MASK;
1984 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
1986 cck_rates = IWL_CCK_RATES_MASK & active_rates;
1987 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
1988 active_rate_basic, &left);
1989 active_rates &= ~ret_rates;
1991 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
1992 active_rate_basic, &left);
1993 active_rates &= ~ret_rates;
1995 len += 2 + *pos;
1996 pos += (*pos) + 1;
1997 if (active_rates == 0)
1998 goto fill_end;
2000 /* fill in supported extended rate */
2001 /* ...next IE... */
2002 left -= 2;
2003 if (left < 0)
2004 return 0;
2005 /* ... fill it in... */
2006 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2007 *pos = 0;
2008 iwl4965_supported_rate_to_ie(pos, active_rates,
2009 active_rate_basic, &left);
2010 if (*pos > 0)
2011 len += 2 + *pos;
2013 #ifdef CONFIG_IWL4965_HT
2014 mode = priv->hw->conf.mode;
2015 if (mode->ht_info.ht_supported) {
2016 pos += (*pos) + 1;
2017 *pos++ = WLAN_EID_HT_CAPABILITY;
2018 *pos++ = sizeof(struct ieee80211_ht_cap);
2019 iwl4965_set_ht_capab(priv->hw,
2020 (struct ieee80211_ht_cap *)pos, 0);
2021 len += 2 + sizeof(struct ieee80211_ht_cap);
2023 #endif /*CONFIG_IWL4965_HT */
2025 fill_end:
2026 return (u16)len;
2030 * QoS support
2032 #ifdef CONFIG_IWL4965_QOS
2033 static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
2034 struct iwl4965_qosparam_cmd *qos)
2037 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
2038 sizeof(struct iwl4965_qosparam_cmd), qos);
2041 static void iwl4965_reset_qos(struct iwl4965_priv *priv)
2043 u16 cw_min = 15;
2044 u16 cw_max = 1023;
2045 u8 aifs = 2;
2046 u8 is_legacy = 0;
2047 unsigned long flags;
2048 int i;
2050 spin_lock_irqsave(&priv->lock, flags);
2051 priv->qos_data.qos_active = 0;
2053 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
2054 if (priv->qos_data.qos_enable)
2055 priv->qos_data.qos_active = 1;
2056 if (!(priv->active_rate & 0xfff0)) {
2057 cw_min = 31;
2058 is_legacy = 1;
2060 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2061 if (priv->qos_data.qos_enable)
2062 priv->qos_data.qos_active = 1;
2063 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
2064 cw_min = 31;
2065 is_legacy = 1;
2068 if (priv->qos_data.qos_active)
2069 aifs = 3;
2071 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
2072 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
2073 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
2074 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
2075 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
2077 if (priv->qos_data.qos_active) {
2078 i = 1;
2079 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
2080 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
2081 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
2082 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2083 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2085 i = 2;
2086 priv->qos_data.def_qos_parm.ac[i].cw_min =
2087 cpu_to_le16((cw_min + 1) / 2 - 1);
2088 priv->qos_data.def_qos_parm.ac[i].cw_max =
2089 cpu_to_le16(cw_max);
2090 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2091 if (is_legacy)
2092 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2093 cpu_to_le16(6016);
2094 else
2095 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2096 cpu_to_le16(3008);
2097 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2099 i = 3;
2100 priv->qos_data.def_qos_parm.ac[i].cw_min =
2101 cpu_to_le16((cw_min + 1) / 4 - 1);
2102 priv->qos_data.def_qos_parm.ac[i].cw_max =
2103 cpu_to_le16((cw_max + 1) / 2 - 1);
2104 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2105 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2106 if (is_legacy)
2107 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2108 cpu_to_le16(3264);
2109 else
2110 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2111 cpu_to_le16(1504);
2112 } else {
2113 for (i = 1; i < 4; i++) {
2114 priv->qos_data.def_qos_parm.ac[i].cw_min =
2115 cpu_to_le16(cw_min);
2116 priv->qos_data.def_qos_parm.ac[i].cw_max =
2117 cpu_to_le16(cw_max);
2118 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2119 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2120 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2123 IWL_DEBUG_QOS("set QoS to default \n");
2125 spin_unlock_irqrestore(&priv->lock, flags);
2128 static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
2130 unsigned long flags;
2132 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2133 return;
2135 if (!priv->qos_data.qos_enable)
2136 return;
2138 spin_lock_irqsave(&priv->lock, flags);
2139 priv->qos_data.def_qos_parm.qos_flags = 0;
2141 if (priv->qos_data.qos_cap.q_AP.queue_request &&
2142 !priv->qos_data.qos_cap.q_AP.txop_request)
2143 priv->qos_data.def_qos_parm.qos_flags |=
2144 QOS_PARAM_FLG_TXOP_TYPE_MSK;
2145 if (priv->qos_data.qos_active)
2146 priv->qos_data.def_qos_parm.qos_flags |=
2147 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2149 #ifdef CONFIG_IWL4965_HT
2150 if (priv->current_ht_config.is_ht)
2151 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
2152 #endif /* CONFIG_IWL4965_HT */
2154 spin_unlock_irqrestore(&priv->lock, flags);
2156 if (force || iwl4965_is_associated(priv)) {
2157 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
2158 priv->qos_data.qos_active,
2159 priv->qos_data.def_qos_parm.qos_flags);
2161 iwl4965_send_qos_params_command(priv,
2162 &(priv->qos_data.def_qos_parm));
2166 #endif /* CONFIG_IWL4965_QOS */
2168 * Power management (not Tx power!) functions
2170 #define MSEC_TO_USEC 1024
2172 #define NOSLP __constant_cpu_to_le16(0), 0, 0
2173 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2174 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2175 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2176 __constant_cpu_to_le32(X1), \
2177 __constant_cpu_to_le32(X2), \
2178 __constant_cpu_to_le32(X3), \
2179 __constant_cpu_to_le32(X4)}
2182 /* default power management (not Tx power) table values */
2183 /* for tim 0-10 */
2184 static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
2185 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2186 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2187 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2188 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2189 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2190 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2193 /* for tim > 10 */
2194 static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
2195 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2196 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2197 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2198 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2199 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2200 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2201 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2202 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2203 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2204 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2207 int iwl4965_power_init_handle(struct iwl4965_priv *priv)
2209 int rc = 0, i;
2210 struct iwl4965_power_mgr *pow_data;
2211 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
2212 u16 pci_pm;
2214 IWL_DEBUG_POWER("Initialize power \n");
2216 pow_data = &(priv->power_data);
2218 memset(pow_data, 0, sizeof(*pow_data));
2220 pow_data->active_index = IWL_POWER_RANGE_0;
2221 pow_data->dtim_val = 0xffff;
2223 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2224 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2226 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2227 if (rc != 0)
2228 return 0;
2229 else {
2230 struct iwl4965_powertable_cmd *cmd;
2232 IWL_DEBUG_POWER("adjust power command flags\n");
2234 for (i = 0; i < IWL_POWER_AC; i++) {
2235 cmd = &pow_data->pwr_range_0[i].cmd;
2237 if (pci_pm & 0x1)
2238 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2239 else
2240 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2243 return rc;
2246 static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
2247 struct iwl4965_powertable_cmd *cmd, u32 mode)
2249 int rc = 0, i;
2250 u8 skip;
2251 u32 max_sleep = 0;
2252 struct iwl4965_power_vec_entry *range;
2253 u8 period = 0;
2254 struct iwl4965_power_mgr *pow_data;
2256 if (mode > IWL_POWER_INDEX_5) {
2257 IWL_DEBUG_POWER("Error invalid power mode \n");
2258 return -1;
2260 pow_data = &(priv->power_data);
2262 if (pow_data->active_index == IWL_POWER_RANGE_0)
2263 range = &pow_data->pwr_range_0[0];
2264 else
2265 range = &pow_data->pwr_range_1[1];
2267 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
2269 #ifdef IWL_MAC80211_DISABLE
2270 if (priv->assoc_network != NULL) {
2271 unsigned long flags;
2273 period = priv->assoc_network->tim.tim_period;
2275 #endif /*IWL_MAC80211_DISABLE */
2276 skip = range[mode].no_dtim;
2278 if (period == 0) {
2279 period = 1;
2280 skip = 0;
2283 if (skip == 0) {
2284 max_sleep = period;
2285 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2286 } else {
2287 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2288 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2289 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2292 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2293 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2294 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2297 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2298 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2299 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2300 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2301 le32_to_cpu(cmd->sleep_interval[0]),
2302 le32_to_cpu(cmd->sleep_interval[1]),
2303 le32_to_cpu(cmd->sleep_interval[2]),
2304 le32_to_cpu(cmd->sleep_interval[3]),
2305 le32_to_cpu(cmd->sleep_interval[4]));
2307 return rc;
2310 static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
2312 u32 uninitialized_var(final_mode);
2313 int rc;
2314 struct iwl4965_powertable_cmd cmd;
2316 /* If on battery, set to 3,
2317 * if plugged into AC power, set to CAM ("continuously aware mode"),
2318 * else user level */
2319 switch (mode) {
2320 case IWL_POWER_BATTERY:
2321 final_mode = IWL_POWER_INDEX_3;
2322 break;
2323 case IWL_POWER_AC:
2324 final_mode = IWL_POWER_MODE_CAM;
2325 break;
2326 default:
2327 final_mode = mode;
2328 break;
2331 cmd.keep_alive_beacons = 0;
2333 iwl4965_update_power_cmd(priv, &cmd, final_mode);
2335 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2337 if (final_mode == IWL_POWER_MODE_CAM)
2338 clear_bit(STATUS_POWER_PMI, &priv->status);
2339 else
2340 set_bit(STATUS_POWER_PMI, &priv->status);
2342 return rc;
2345 int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
2347 /* Filter incoming packets to determine if they are targeted toward
2348 * this network, discarding packets coming from ourselves */
2349 switch (priv->iw_mode) {
2350 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2351 /* packets from our adapter are dropped (echo) */
2352 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2353 return 0;
2354 /* {broad,multi}cast packets to our IBSS go through */
2355 if (is_multicast_ether_addr(header->addr1))
2356 return !compare_ether_addr(header->addr3, priv->bssid);
2357 /* packets to our adapter go through */
2358 return !compare_ether_addr(header->addr1, priv->mac_addr);
2359 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2360 /* packets from our adapter are dropped (echo) */
2361 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2362 return 0;
2363 /* {broad,multi}cast packets to our BSS go through */
2364 if (is_multicast_ether_addr(header->addr1))
2365 return !compare_ether_addr(header->addr2, priv->bssid);
2366 /* packets to our adapter go through */
2367 return !compare_ether_addr(header->addr1, priv->mac_addr);
2370 return 1;
2373 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2375 static const char *iwl4965_get_tx_fail_reason(u32 status)
2377 switch (status & TX_STATUS_MSK) {
2378 case TX_STATUS_SUCCESS:
2379 return "SUCCESS";
2380 TX_STATUS_ENTRY(SHORT_LIMIT);
2381 TX_STATUS_ENTRY(LONG_LIMIT);
2382 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2383 TX_STATUS_ENTRY(MGMNT_ABORT);
2384 TX_STATUS_ENTRY(NEXT_FRAG);
2385 TX_STATUS_ENTRY(LIFE_EXPIRE);
2386 TX_STATUS_ENTRY(DEST_PS);
2387 TX_STATUS_ENTRY(ABORTED);
2388 TX_STATUS_ENTRY(BT_RETRY);
2389 TX_STATUS_ENTRY(STA_INVALID);
2390 TX_STATUS_ENTRY(FRAG_DROPPED);
2391 TX_STATUS_ENTRY(TID_DISABLE);
2392 TX_STATUS_ENTRY(FRAME_FLUSHED);
2393 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2394 TX_STATUS_ENTRY(TX_LOCKED);
2395 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2398 return "UNKNOWN";
2402 * iwl4965_scan_cancel - Cancel any currently executing HW scan
2404 * NOTE: priv->mutex is not required before calling this function
2406 static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
2408 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2409 clear_bit(STATUS_SCANNING, &priv->status);
2410 return 0;
2413 if (test_bit(STATUS_SCANNING, &priv->status)) {
2414 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2415 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2416 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2417 queue_work(priv->workqueue, &priv->abort_scan);
2419 } else
2420 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2422 return test_bit(STATUS_SCANNING, &priv->status);
2425 return 0;
2429 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
2430 * @ms: amount of time to wait (in milliseconds) for scan to abort
2432 * NOTE: priv->mutex must be held before calling this function
2434 static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
2436 unsigned long now = jiffies;
2437 int ret;
2439 ret = iwl4965_scan_cancel(priv);
2440 if (ret && ms) {
2441 mutex_unlock(&priv->mutex);
2442 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2443 test_bit(STATUS_SCANNING, &priv->status))
2444 msleep(1);
2445 mutex_lock(&priv->mutex);
2447 return test_bit(STATUS_SCANNING, &priv->status);
2450 return ret;
2453 static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
2455 /* Reset ieee stats */
2457 /* We don't reset the net_device_stats (ieee->stats) on
2458 * re-association */
2460 priv->last_seq_num = -1;
2461 priv->last_frag_num = -1;
2462 priv->last_packet_time = 0;
2464 iwl4965_scan_cancel(priv);
2467 #define MAX_UCODE_BEACON_INTERVAL 4096
2468 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2470 static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
2472 u16 new_val = 0;
2473 u16 beacon_factor = 0;
2475 beacon_factor =
2476 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2477 / MAX_UCODE_BEACON_INTERVAL;
2478 new_val = beacon_val / beacon_factor;
2480 return cpu_to_le16(new_val);
2483 static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
2485 u64 interval_tm_unit;
2486 u64 tsf, result;
2487 unsigned long flags;
2488 struct ieee80211_conf *conf = NULL;
2489 u16 beacon_int = 0;
2491 conf = ieee80211_get_hw_conf(priv->hw);
2493 spin_lock_irqsave(&priv->lock, flags);
2494 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2495 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2497 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2499 tsf = priv->timestamp1;
2500 tsf = ((tsf << 32) | priv->timestamp0);
2502 beacon_int = priv->beacon_int;
2503 spin_unlock_irqrestore(&priv->lock, flags);
2505 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2506 if (beacon_int == 0) {
2507 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2508 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2509 } else {
2510 priv->rxon_timing.beacon_interval =
2511 cpu_to_le16(beacon_int);
2512 priv->rxon_timing.beacon_interval =
2513 iwl4965_adjust_beacon_interval(
2514 le16_to_cpu(priv->rxon_timing.beacon_interval));
2517 priv->rxon_timing.atim_window = 0;
2518 } else {
2519 priv->rxon_timing.beacon_interval =
2520 iwl4965_adjust_beacon_interval(conf->beacon_int);
2521 /* TODO: we need to get atim_window from upper stack
2522 * for now we set to 0 */
2523 priv->rxon_timing.atim_window = 0;
2526 interval_tm_unit =
2527 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2528 result = do_div(tsf, interval_tm_unit);
2529 priv->rxon_timing.beacon_init_val =
2530 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2532 IWL_DEBUG_ASSOC
2533 ("beacon interval %d beacon timer %d beacon tim %d\n",
2534 le16_to_cpu(priv->rxon_timing.beacon_interval),
2535 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2536 le16_to_cpu(priv->rxon_timing.atim_window));
2539 static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
2541 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2542 IWL_ERROR("APs don't scan.\n");
2543 return 0;
2546 if (!iwl4965_is_ready_rf(priv)) {
2547 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2548 return -EIO;
2551 if (test_bit(STATUS_SCANNING, &priv->status)) {
2552 IWL_DEBUG_SCAN("Scan already in progress.\n");
2553 return -EAGAIN;
2556 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2557 IWL_DEBUG_SCAN("Scan request while abort pending. "
2558 "Queuing.\n");
2559 return -EAGAIN;
2562 IWL_DEBUG_INFO("Starting scan...\n");
2563 priv->scan_bands = 2;
2564 set_bit(STATUS_SCANNING, &priv->status);
2565 priv->scan_start = jiffies;
2566 priv->scan_pass_start = priv->scan_start;
2568 queue_work(priv->workqueue, &priv->request_scan);
2570 return 0;
2573 static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
2575 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
2577 if (hw_decrypt)
2578 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2579 else
2580 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2582 return 0;
2585 static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode)
2587 if (phymode == MODE_IEEE80211A) {
2588 priv->staging_rxon.flags &=
2589 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2590 | RXON_FLG_CCK_MSK);
2591 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2592 } else {
2593 /* Copied from iwl4965_bg_post_associate() */
2594 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2595 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2596 else
2597 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2599 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2600 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2602 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2603 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2604 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2609 * initialize rxon structure with default values from eeprom
2611 static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
2613 const struct iwl4965_channel_info *ch_info;
2615 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2617 switch (priv->iw_mode) {
2618 case IEEE80211_IF_TYPE_AP:
2619 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2620 break;
2622 case IEEE80211_IF_TYPE_STA:
2623 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2624 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2625 break;
2627 case IEEE80211_IF_TYPE_IBSS:
2628 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2629 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2630 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2631 RXON_FILTER_ACCEPT_GRP_MSK;
2632 break;
2634 case IEEE80211_IF_TYPE_MNTR:
2635 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2636 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2637 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2638 break;
2641 #if 0
2642 /* TODO: Figure out when short_preamble would be set and cache from
2643 * that */
2644 if (!hw_to_local(priv->hw)->short_preamble)
2645 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2646 else
2647 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2648 #endif
2650 ch_info = iwl4965_get_channel_info(priv, priv->phymode,
2651 le16_to_cpu(priv->staging_rxon.channel));
2653 if (!ch_info)
2654 ch_info = &priv->channel_info[0];
2657 * in some case A channels are all non IBSS
2658 * in this case force B/G channel
2660 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2661 !(is_channel_ibss(ch_info)))
2662 ch_info = &priv->channel_info[0];
2664 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2665 if (is_channel_a_band(ch_info))
2666 priv->phymode = MODE_IEEE80211A;
2667 else
2668 priv->phymode = MODE_IEEE80211G;
2670 iwl4965_set_flags_for_phymode(priv, priv->phymode);
2672 priv->staging_rxon.ofdm_basic_rates =
2673 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2674 priv->staging_rxon.cck_basic_rates =
2675 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2677 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2678 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2679 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2680 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2681 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2682 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2683 iwl4965_set_rxon_chain(priv);
2686 static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
2688 if (mode == IEEE80211_IF_TYPE_IBSS) {
2689 const struct iwl4965_channel_info *ch_info;
2691 ch_info = iwl4965_get_channel_info(priv,
2692 priv->phymode,
2693 le16_to_cpu(priv->staging_rxon.channel));
2695 if (!ch_info || !is_channel_ibss(ch_info)) {
2696 IWL_ERROR("channel %d not IBSS channel\n",
2697 le16_to_cpu(priv->staging_rxon.channel));
2698 return -EINVAL;
2702 priv->iw_mode = mode;
2704 iwl4965_connection_init_rx_config(priv);
2705 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2707 iwl4965_clear_stations_table(priv);
2709 /* dont commit rxon if rf-kill is on*/
2710 if (!iwl4965_is_ready_rf(priv))
2711 return -EAGAIN;
2713 cancel_delayed_work(&priv->scan_check);
2714 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2715 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2716 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2717 return -EAGAIN;
2720 iwl4965_commit_rxon(priv);
2722 return 0;
2725 static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
2726 struct ieee80211_tx_control *ctl,
2727 struct iwl4965_cmd *cmd,
2728 struct sk_buff *skb_frag,
2729 int last_frag)
2731 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2733 switch (keyinfo->alg) {
2734 case ALG_CCMP:
2735 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2736 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2737 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2738 break;
2740 case ALG_TKIP:
2741 #if 0
2742 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2744 if (last_frag)
2745 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2747 else
2748 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2749 #endif
2750 break;
2752 case ALG_WEP:
2753 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2754 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2756 if (keyinfo->keylen == 13)
2757 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2759 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2761 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2762 "with key %d\n", ctl->key_idx);
2763 break;
2765 default:
2766 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2767 break;
2772 * handle build REPLY_TX command notification.
2774 static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2775 struct iwl4965_cmd *cmd,
2776 struct ieee80211_tx_control *ctrl,
2777 struct ieee80211_hdr *hdr,
2778 int is_unicast, u8 std_id)
2780 __le16 *qc;
2781 u16 fc = le16_to_cpu(hdr->frame_control);
2782 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2784 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2785 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2786 tx_flags |= TX_CMD_FLG_ACK_MSK;
2787 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2788 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2789 if (ieee80211_is_probe_response(fc) &&
2790 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2791 tx_flags |= TX_CMD_FLG_TSF_MSK;
2792 } else {
2793 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2794 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2797 if (ieee80211_is_back_request(fc))
2798 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2801 cmd->cmd.tx.sta_id = std_id;
2802 if (ieee80211_get_morefrag(hdr))
2803 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2805 qc = ieee80211_get_qos_ctrl(hdr);
2806 if (qc) {
2807 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2808 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2809 } else
2810 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2812 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2813 tx_flags |= TX_CMD_FLG_RTS_MSK;
2814 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2815 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2816 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2817 tx_flags |= TX_CMD_FLG_CTS_MSK;
2820 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2821 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2823 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2824 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2825 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2826 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2827 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
2828 else
2829 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
2830 } else
2831 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2833 cmd->cmd.tx.driver_txop = 0;
2834 cmd->cmd.tx.tx_flags = tx_flags;
2835 cmd->cmd.tx.next_frame_len = 0;
2839 * iwl4965_get_sta_id - Find station's index within station table
2841 * If new IBSS station, create new entry in station table
2843 static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
2844 struct ieee80211_hdr *hdr)
2846 int sta_id;
2847 u16 fc = le16_to_cpu(hdr->frame_control);
2848 DECLARE_MAC_BUF(mac);
2850 /* If this frame is broadcast or management, use broadcast station id */
2851 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2852 is_multicast_ether_addr(hdr->addr1))
2853 return priv->hw_setting.bcast_sta_id;
2855 switch (priv->iw_mode) {
2857 /* If we are a client station in a BSS network, use the special
2858 * AP station entry (that's the only station we communicate with) */
2859 case IEEE80211_IF_TYPE_STA:
2860 return IWL_AP_ID;
2862 /* If we are an AP, then find the station, or use BCAST */
2863 case IEEE80211_IF_TYPE_AP:
2864 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
2865 if (sta_id != IWL_INVALID_STATION)
2866 return sta_id;
2867 return priv->hw_setting.bcast_sta_id;
2869 /* If this frame is going out to an IBSS network, find the station,
2870 * or create a new station table entry */
2871 case IEEE80211_IF_TYPE_IBSS:
2872 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
2873 if (sta_id != IWL_INVALID_STATION)
2874 return sta_id;
2876 /* Create new station table entry */
2877 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2878 0, CMD_ASYNC, NULL);
2880 if (sta_id != IWL_INVALID_STATION)
2881 return sta_id;
2883 IWL_DEBUG_DROP("Station %s not in station map. "
2884 "Defaulting to broadcast...\n",
2885 print_mac(mac, hdr->addr1));
2886 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2887 return priv->hw_setting.bcast_sta_id;
2889 default:
2890 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
2891 return priv->hw_setting.bcast_sta_id;
2896 * start REPLY_TX command process
2898 static int iwl4965_tx_skb(struct iwl4965_priv *priv,
2899 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2901 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2902 struct iwl4965_tfd_frame *tfd;
2903 u32 *control_flags;
2904 int txq_id = ctl->queue;
2905 struct iwl4965_tx_queue *txq = NULL;
2906 struct iwl4965_queue *q = NULL;
2907 dma_addr_t phys_addr;
2908 dma_addr_t txcmd_phys;
2909 dma_addr_t scratch_phys;
2910 struct iwl4965_cmd *out_cmd = NULL;
2911 u16 len, idx, len_org;
2912 u8 id, hdr_len, unicast;
2913 u8 sta_id;
2914 u16 seq_number = 0;
2915 u16 fc;
2916 __le16 *qc;
2917 u8 wait_write_ptr = 0;
2918 unsigned long flags;
2919 int rc;
2921 spin_lock_irqsave(&priv->lock, flags);
2922 if (iwl4965_is_rfkill(priv)) {
2923 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2924 goto drop_unlock;
2927 if (!priv->vif) {
2928 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
2929 goto drop_unlock;
2932 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2933 IWL_ERROR("ERROR: No TX rate available.\n");
2934 goto drop_unlock;
2937 unicast = !is_multicast_ether_addr(hdr->addr1);
2938 id = 0;
2940 fc = le16_to_cpu(hdr->frame_control);
2942 #ifdef CONFIG_IWL4965_DEBUG
2943 if (ieee80211_is_auth(fc))
2944 IWL_DEBUG_TX("Sending AUTH frame\n");
2945 else if (ieee80211_is_assoc_request(fc))
2946 IWL_DEBUG_TX("Sending ASSOC frame\n");
2947 else if (ieee80211_is_reassoc_request(fc))
2948 IWL_DEBUG_TX("Sending REASSOC frame\n");
2949 #endif
2951 /* drop all data frame if we are not associated */
2952 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2953 (!iwl4965_is_associated(priv) ||
2954 <<<<<<< HEAD:drivers/net/wireless/iwlwifi/iwl4965-base.c
2955 !priv->assoc_id ||
2956 =======
2957 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
2958 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/iwlwifi/iwl4965-base.c
2959 !priv->assoc_station_added)) {
2960 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
2961 goto drop_unlock;
2964 spin_unlock_irqrestore(&priv->lock, flags);
2966 hdr_len = ieee80211_get_hdrlen(fc);
2968 /* Find (or create) index into station table for destination station */
2969 sta_id = iwl4965_get_sta_id(priv, hdr);
2970 if (sta_id == IWL_INVALID_STATION) {
2971 DECLARE_MAC_BUF(mac);
2973 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2974 print_mac(mac, hdr->addr1));
2975 goto drop;
2978 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2980 qc = ieee80211_get_qos_ctrl(hdr);
2981 if (qc) {
2982 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2983 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2984 IEEE80211_SCTL_SEQ;
2985 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2986 (hdr->seq_ctrl &
2987 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2988 seq_number += 0x10;
2989 #ifdef CONFIG_IWL4965_HT
2990 #ifdef CONFIG_IWL4965_HT_AGG
2991 /* aggregation is on for this <sta,tid> */
2992 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2993 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
2994 #endif /* CONFIG_IWL4965_HT_AGG */
2995 #endif /* CONFIG_IWL4965_HT */
2998 /* Descriptor for chosen Tx queue */
2999 txq = &priv->txq[txq_id];
3000 q = &txq->q;
3002 spin_lock_irqsave(&priv->lock, flags);
3004 /* Set up first empty TFD within this queue's circular TFD buffer */
3005 tfd = &txq->bd[q->write_ptr];
3006 memset(tfd, 0, sizeof(*tfd));
3007 control_flags = (u32 *) tfd;
3008 idx = get_cmd_index(q, q->write_ptr, 0);
3010 /* Set up driver data for this TFD */
3011 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
3012 txq->txb[q->write_ptr].skb[0] = skb;
3013 memcpy(&(txq->txb[q->write_ptr].status.control),
3014 ctl, sizeof(struct ieee80211_tx_control));
3016 /* Set up first empty entry in queue's array of Tx/cmd buffers */
3017 out_cmd = &txq->cmd[idx];
3018 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
3019 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
3022 * Set up the Tx-command (not MAC!) header.
3023 * Store the chosen Tx queue and TFD index within the sequence field;
3024 * after Tx, uCode's Tx response will return this value so driver can
3025 * locate the frame within the tx queue and do post-tx processing.
3027 out_cmd->hdr.cmd = REPLY_TX;
3028 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
3029 INDEX_TO_SEQ(q->write_ptr)));
3031 /* Copy MAC header from skb into command buffer */
3032 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
3035 * Use the first empty entry in this queue's command buffer array
3036 * to contain the Tx command and MAC header concatenated together
3037 * (payload data will be in another buffer).
3038 * Size of this varies, due to varying MAC header length.
3039 * If end is not dword aligned, we'll have 2 extra bytes at the end
3040 * of the MAC header (device reads on dword boundaries).
3041 * We'll tell device about this padding later.
3043 len = priv->hw_setting.tx_cmd_len +
3044 sizeof(struct iwl4965_cmd_header) + hdr_len;
3046 len_org = len;
3047 len = (len + 3) & ~3;
3049 if (len_org != len)
3050 len_org = 1;
3051 else
3052 len_org = 0;
3054 /* Physical address of this Tx command's header (not MAC header!),
3055 * within command buffer array. */
3056 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
3057 offsetof(struct iwl4965_cmd, hdr);
3059 /* Add buffer containing Tx command and MAC(!) header to TFD's
3060 * first entry */
3061 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
3063 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
3064 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
3066 /* Set up TFD's 2nd entry to point directly to remainder of skb,
3067 * if any (802.11 null frames have no payload). */
3068 len = skb->len - hdr_len;
3069 if (len) {
3070 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
3071 len, PCI_DMA_TODEVICE);
3072 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
3075 /* Tell 4965 about any 2-byte padding after MAC header */
3076 if (len_org)
3077 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
3079 /* Total # bytes to be transmitted */
3080 len = (u16)skb->len;
3081 out_cmd->cmd.tx.len = cpu_to_le16(len);
3083 /* TODO need this for burst mode later on */
3084 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
3086 /* set is_hcca to 0; it probably will never be implemented */
3087 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
3089 scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
3090 offsetof(struct iwl4965_tx_cmd, scratch);
3091 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
3092 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
3094 #ifdef CONFIG_IWL4965_HT_AGG
3095 #ifdef CONFIG_IWL4965_HT
3096 /* TODO: move this functionality to rate scaling */
3097 iwl4965_tl_get_stats(priv, hdr);
3098 #endif /* CONFIG_IWL4965_HT_AGG */
3099 #endif /*CONFIG_IWL4965_HT */
3102 if (!ieee80211_get_morefrag(hdr)) {
3103 txq->need_update = 1;
3104 if (qc) {
3105 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
3106 priv->stations[sta_id].tid[tid].seq_number = seq_number;
3108 } else {
3109 wait_write_ptr = 1;
3110 txq->need_update = 0;
3113 iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
3114 sizeof(out_cmd->cmd.tx));
3116 iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
3117 ieee80211_get_hdrlen(fc));
3119 /* Set up entry for this TFD in Tx byte-count array */
3120 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
3122 /* Tell device the write index *just past* this latest filled TFD */
3123 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
3124 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
3125 spin_unlock_irqrestore(&priv->lock, flags);
3127 if (rc)
3128 return rc;
3130 if ((iwl4965_queue_space(q) < q->high_mark)
3131 && priv->mac80211_registered) {
3132 if (wait_write_ptr) {
3133 spin_lock_irqsave(&priv->lock, flags);
3134 txq->need_update = 1;
3135 iwl4965_tx_queue_update_write_ptr(priv, txq);
3136 spin_unlock_irqrestore(&priv->lock, flags);
3139 ieee80211_stop_queue(priv->hw, ctl->queue);
3142 return 0;
3144 drop_unlock:
3145 spin_unlock_irqrestore(&priv->lock, flags);
3146 drop:
3147 return -1;
3150 static void iwl4965_set_rate(struct iwl4965_priv *priv)
3152 const struct ieee80211_hw_mode *hw = NULL;
3153 struct ieee80211_rate *rate;
3154 int i;
3156 hw = iwl4965_get_hw_mode(priv, priv->phymode);
3157 if (!hw) {
3158 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
3159 return;
3162 priv->active_rate = 0;
3163 priv->active_rate_basic = 0;
3165 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3166 hw->mode == MODE_IEEE80211A ?
3167 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3169 for (i = 0; i < hw->num_rates; i++) {
3170 rate = &(hw->rates[i]);
3171 if ((rate->val < IWL_RATE_COUNT) &&
3172 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3173 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
3174 rate->val, iwl4965_rates[rate->val].plcp,
3175 (rate->flags & IEEE80211_RATE_BASIC) ?
3176 "*" : "");
3177 priv->active_rate |= (1 << rate->val);
3178 if (rate->flags & IEEE80211_RATE_BASIC)
3179 priv->active_rate_basic |= (1 << rate->val);
3180 } else
3181 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
3182 rate->val, iwl4965_rates[rate->val].plcp);
3185 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3186 priv->active_rate, priv->active_rate_basic);
3189 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3190 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3191 * OFDM
3193 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3194 priv->staging_rxon.cck_basic_rates =
3195 ((priv->active_rate_basic &
3196 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3197 else
3198 priv->staging_rxon.cck_basic_rates =
3199 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3201 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3202 priv->staging_rxon.ofdm_basic_rates =
3203 ((priv->active_rate_basic &
3204 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3205 IWL_FIRST_OFDM_RATE) & 0xFF;
3206 else
3207 priv->staging_rxon.ofdm_basic_rates =
3208 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3211 static void iwl4965_radio_kill_sw(struct iwl4965_priv *priv, int disable_radio)
3213 unsigned long flags;
3215 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3216 return;
3218 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3219 disable_radio ? "OFF" : "ON");
3221 if (disable_radio) {
3222 iwl4965_scan_cancel(priv);
3223 /* FIXME: This is a workaround for AP */
3224 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3225 spin_lock_irqsave(&priv->lock, flags);
3226 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
3227 CSR_UCODE_SW_BIT_RFKILL);
3228 spin_unlock_irqrestore(&priv->lock, flags);
3229 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
3230 set_bit(STATUS_RF_KILL_SW, &priv->status);
3232 return;
3235 spin_lock_irqsave(&priv->lock, flags);
3236 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3238 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3239 spin_unlock_irqrestore(&priv->lock, flags);
3241 /* wake up ucode */
3242 msleep(10);
3244 spin_lock_irqsave(&priv->lock, flags);
3245 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3246 if (!iwl4965_grab_nic_access(priv))
3247 iwl4965_release_nic_access(priv);
3248 spin_unlock_irqrestore(&priv->lock, flags);
3250 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3251 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3252 "disabled by HW switch\n");
3253 return;
3256 queue_work(priv->workqueue, &priv->restart);
3257 return;
3260 void iwl4965_set_decrypted_flag(struct iwl4965_priv *priv, struct sk_buff *skb,
3261 u32 decrypt_res, struct ieee80211_rx_status *stats)
3263 u16 fc =
3264 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3266 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3267 return;
3269 if (!(fc & IEEE80211_FCTL_PROTECTED))
3270 return;
3272 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3273 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3274 case RX_RES_STATUS_SEC_TYPE_TKIP:
3275 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3276 RX_RES_STATUS_BAD_ICV_MIC)
3277 stats->flag |= RX_FLAG_MMIC_ERROR;
3278 case RX_RES_STATUS_SEC_TYPE_WEP:
3279 case RX_RES_STATUS_SEC_TYPE_CCMP:
3280 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3281 RX_RES_STATUS_DECRYPT_OK) {
3282 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3283 stats->flag |= RX_FLAG_DECRYPTED;
3285 break;
3287 default:
3288 break;
3293 #define IWL_PACKET_RETRY_TIME HZ
3295 int iwl4965_is_duplicate_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
3297 u16 sc = le16_to_cpu(header->seq_ctrl);
3298 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3299 u16 frag = sc & IEEE80211_SCTL_FRAG;
3300 u16 *last_seq, *last_frag;
3301 unsigned long *last_time;
3303 switch (priv->iw_mode) {
3304 case IEEE80211_IF_TYPE_IBSS:{
3305 struct list_head *p;
3306 struct iwl4965_ibss_seq *entry = NULL;
3307 u8 *mac = header->addr2;
3308 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3310 __list_for_each(p, &priv->ibss_mac_hash[index]) {
3311 entry = list_entry(p, struct iwl4965_ibss_seq, list);
3312 if (!compare_ether_addr(entry->mac, mac))
3313 break;
3315 if (p == &priv->ibss_mac_hash[index]) {
3316 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3317 if (!entry) {
3318 IWL_ERROR("Cannot malloc new mac entry\n");
3319 return 0;
3321 memcpy(entry->mac, mac, ETH_ALEN);
3322 entry->seq_num = seq;
3323 entry->frag_num = frag;
3324 entry->packet_time = jiffies;
3325 list_add(&entry->list, &priv->ibss_mac_hash[index]);
3326 return 0;
3328 last_seq = &entry->seq_num;
3329 last_frag = &entry->frag_num;
3330 last_time = &entry->packet_time;
3331 break;
3333 case IEEE80211_IF_TYPE_STA:
3334 last_seq = &priv->last_seq_num;
3335 last_frag = &priv->last_frag_num;
3336 last_time = &priv->last_packet_time;
3337 break;
3338 default:
3339 return 0;
3341 if ((*last_seq == seq) &&
3342 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3343 if (*last_frag == frag)
3344 goto drop;
3345 if (*last_frag + 1 != frag)
3346 /* out-of-order fragment */
3347 goto drop;
3348 } else
3349 *last_seq = seq;
3351 *last_frag = frag;
3352 *last_time = jiffies;
3353 return 0;
3355 drop:
3356 return 1;
3359 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
3361 #include "iwl-spectrum.h"
3363 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
3364 #define BEACON_TIME_MASK_HIGH 0xFF000000
3365 #define TIME_UNIT 1024
3368 * extended beacon time format
3369 * time in usec will be changed into a 32-bit value in 8:24 format
3370 * the high 1 byte is the beacon counts
3371 * the lower 3 bytes is the time in usec within one beacon interval
3374 static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
3376 u32 quot;
3377 u32 rem;
3378 u32 interval = beacon_interval * 1024;
3380 if (!interval || !usec)
3381 return 0;
3383 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3384 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3386 return (quot << 24) + rem;
3389 /* base is usually what we get from ucode with each received frame,
3390 * the same as HW timer counter counting down
3393 static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
3395 u32 base_low = base & BEACON_TIME_MASK_LOW;
3396 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3397 u32 interval = beacon_interval * TIME_UNIT;
3398 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3399 (addon & BEACON_TIME_MASK_HIGH);
3401 if (base_low > addon_low)
3402 res += base_low - addon_low;
3403 else if (base_low < addon_low) {
3404 res += interval + base_low - addon_low;
3405 res += (1 << 24);
3406 } else
3407 res += (1 << 24);
3409 return cpu_to_le32(res);
3412 static int iwl4965_get_measurement(struct iwl4965_priv *priv,
3413 struct ieee80211_measurement_params *params,
3414 u8 type)
3416 struct iwl4965_spectrum_cmd spectrum;
3417 struct iwl4965_rx_packet *res;
3418 struct iwl4965_host_cmd cmd = {
3419 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3420 .data = (void *)&spectrum,
3421 .meta.flags = CMD_WANT_SKB,
3423 u32 add_time = le64_to_cpu(params->start_time);
3424 int rc;
3425 int spectrum_resp_status;
3426 int duration = le16_to_cpu(params->duration);
3428 if (iwl4965_is_associated(priv))
3429 add_time =
3430 iwl4965_usecs_to_beacons(
3431 le64_to_cpu(params->start_time) - priv->last_tsf,
3432 le16_to_cpu(priv->rxon_timing.beacon_interval));
3434 memset(&spectrum, 0, sizeof(spectrum));
3436 spectrum.channel_count = cpu_to_le16(1);
3437 spectrum.flags =
3438 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3439 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3440 cmd.len = sizeof(spectrum);
3441 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3443 if (iwl4965_is_associated(priv))
3444 spectrum.start_time =
3445 iwl4965_add_beacon_time(priv->last_beacon_time,
3446 add_time,
3447 le16_to_cpu(priv->rxon_timing.beacon_interval));
3448 else
3449 spectrum.start_time = 0;
3451 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3452 spectrum.channels[0].channel = params->channel;
3453 spectrum.channels[0].type = type;
3454 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3455 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3456 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3458 rc = iwl4965_send_cmd_sync(priv, &cmd);
3459 if (rc)
3460 return rc;
3462 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
3463 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3464 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3465 rc = -EIO;
3468 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3469 switch (spectrum_resp_status) {
3470 case 0: /* Command will be handled */
3471 if (res->u.spectrum.id != 0xff) {
3472 IWL_DEBUG_INFO
3473 ("Replaced existing measurement: %d\n",
3474 res->u.spectrum.id);
3475 priv->measurement_status &= ~MEASUREMENT_READY;
3477 priv->measurement_status |= MEASUREMENT_ACTIVE;
3478 rc = 0;
3479 break;
3481 case 1: /* Command will not be handled */
3482 rc = -EAGAIN;
3483 break;
3486 dev_kfree_skb_any(cmd.meta.u.skb);
3488 return rc;
3490 #endif
3492 static void iwl4965_txstatus_to_ieee(struct iwl4965_priv *priv,
3493 struct iwl4965_tx_info *tx_sta)
3496 tx_sta->status.ack_signal = 0;
3497 tx_sta->status.excessive_retries = 0;
3498 tx_sta->status.queue_length = 0;
3499 tx_sta->status.queue_number = 0;
3501 if (in_interrupt())
3502 ieee80211_tx_status_irqsafe(priv->hw,
3503 tx_sta->skb[0], &(tx_sta->status));
3504 else
3505 ieee80211_tx_status(priv->hw,
3506 tx_sta->skb[0], &(tx_sta->status));
3508 tx_sta->skb[0] = NULL;
3512 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
3514 * When FW advances 'R' index, all entries between old and new 'R' index
3515 * need to be reclaimed. As result, some free space forms. If there is
3516 * enough free space (> low mark), wake the stack that feeds us.
3518 int iwl4965_tx_queue_reclaim(struct iwl4965_priv *priv, int txq_id, int index)
3520 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3521 struct iwl4965_queue *q = &txq->q;
3522 int nfreed = 0;
3524 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3525 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3526 "is out of range [0-%d] %d %d.\n", txq_id,
3527 index, q->n_bd, q->write_ptr, q->read_ptr);
3528 return 0;
3531 for (index = iwl4965_queue_inc_wrap(index, q->n_bd);
3532 q->read_ptr != index;
3533 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3534 if (txq_id != IWL_CMD_QUEUE_NUM) {
3535 iwl4965_txstatus_to_ieee(priv,
3536 &(txq->txb[txq->q.read_ptr]));
3537 iwl4965_hw_txq_free_tfd(priv, txq);
3538 } else if (nfreed > 1) {
3539 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3540 q->write_ptr, q->read_ptr);
3541 queue_work(priv->workqueue, &priv->restart);
3543 nfreed++;
3546 if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
3547 (txq_id != IWL_CMD_QUEUE_NUM) &&
3548 priv->mac80211_registered)
3549 ieee80211_wake_queue(priv->hw, txq_id);
3552 return nfreed;
3555 static int iwl4965_is_tx_success(u32 status)
3557 status &= TX_STATUS_MSK;
3558 return (status == TX_STATUS_SUCCESS)
3559 || (status == TX_STATUS_DIRECT_DONE);
3562 /******************************************************************************
3564 * Generic RX handler implementations
3566 ******************************************************************************/
3567 #ifdef CONFIG_IWL4965_HT
3568 #ifdef CONFIG_IWL4965_HT_AGG
3570 static inline int iwl4965_get_ra_sta_id(struct iwl4965_priv *priv,
3571 struct ieee80211_hdr *hdr)
3573 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3574 return IWL_AP_ID;
3575 else {
3576 u8 *da = ieee80211_get_DA(hdr);
3577 return iwl4965_hw_find_station(priv, da);
3581 static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
3582 struct iwl4965_priv *priv, int txq_id, int idx)
3584 if (priv->txq[txq_id].txb[idx].skb[0])
3585 return (struct ieee80211_hdr *)priv->txq[txq_id].
3586 txb[idx].skb[0]->data;
3587 return NULL;
3590 static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
3592 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3593 tx_resp->frame_count);
3594 return le32_to_cpu(*scd_ssn) & MAX_SN;
3599 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3601 static int iwl4965_tx_status_reply_tx(struct iwl4965_priv *priv,
3602 struct iwl4965_ht_agg *agg,
3603 struct iwl4965_tx_resp *tx_resp,
3604 u16 start_idx)
3606 u32 status;
3607 __le32 *frame_status = &tx_resp->status;
3608 struct ieee80211_tx_status *tx_status = NULL;
3609 struct ieee80211_hdr *hdr = NULL;
3610 int i, sh;
3611 int txq_id, idx;
3612 u16 seq;
3614 if (agg->wait_for_ba)
3615 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
3617 agg->frame_count = tx_resp->frame_count;
3618 agg->start_idx = start_idx;
3619 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3620 agg->bitmap0 = agg->bitmap1 = 0;
3622 /* # frames attempted by Tx command */
3623 if (agg->frame_count == 1) {
3624 /* Only one frame was attempted; no block-ack will arrive */
3625 struct iwl4965_tx_queue *txq ;
3626 status = le32_to_cpu(frame_status[0]);
3628 txq_id = agg->txq_id;
3629 txq = &priv->txq[txq_id];
3630 /* FIXME: code repetition */
3631 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
3632 agg->frame_count, agg->start_idx);
3634 tx_status = &(priv->txq[txq_id].txb[txq->q.read_ptr].status);
3635 tx_status->retry_count = tx_resp->failure_frame;
3636 tx_status->queue_number = status & 0xff;
3637 tx_status->queue_length = tx_resp->bt_kill_count;
3638 tx_status->queue_length |= tx_resp->failure_rts;
3640 tx_status->flags = iwl4965_is_tx_success(status)?
3641 IEEE80211_TX_STATUS_ACK : 0;
3642 tx_status->control.tx_rate =
3643 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3644 /* FIXME: code repetition end */
3646 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3647 status & 0xff, tx_resp->failure_frame);
3648 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
3649 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
3651 agg->wait_for_ba = 0;
3652 } else {
3653 /* Two or more frames were attempted; expect block-ack */
3654 u64 bitmap = 0;
3655 int start = agg->start_idx;
3657 /* Construct bit-map of pending frames within Tx window */
3658 for (i = 0; i < agg->frame_count; i++) {
3659 u16 sc;
3660 status = le32_to_cpu(frame_status[i]);
3661 seq = status >> 16;
3662 idx = SEQ_TO_INDEX(seq);
3663 txq_id = SEQ_TO_QUEUE(seq);
3665 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3666 AGG_TX_STATE_ABORT_MSK))
3667 continue;
3669 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3670 agg->frame_count, txq_id, idx);
3672 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
3674 sc = le16_to_cpu(hdr->seq_ctrl);
3675 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3676 IWL_ERROR("BUG_ON idx doesn't match seq control"
3677 " idx=%d, seq_idx=%d, seq=%d\n",
3678 idx, SEQ_TO_SN(sc),
3679 hdr->seq_ctrl);
3680 return -1;
3683 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3684 i, idx, SEQ_TO_SN(sc));
3686 sh = idx - start;
3687 if (sh > 64) {
3688 sh = (start - idx) + 0xff;
3689 bitmap = bitmap << sh;
3690 sh = 0;
3691 start = idx;
3692 } else if (sh < -64)
3693 sh = 0xff - (start - idx);
3694 else if (sh < 0) {
3695 sh = start - idx;
3696 start = idx;
3697 bitmap = bitmap << sh;
3698 sh = 0;
3700 bitmap |= (1 << sh);
3701 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3702 start, (u32)(bitmap & 0xFFFFFFFF));
3705 agg->bitmap0 = bitmap & 0xFFFFFFFF;
3706 agg->bitmap1 = bitmap >> 32;
3707 agg->start_idx = start;
3708 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3709 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
3710 agg->frame_count, agg->start_idx,
3711 agg->bitmap0);
3713 if (bitmap)
3714 agg->wait_for_ba = 1;
3716 return 0;
3718 #endif
3719 #endif
3722 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3724 static void iwl4965_rx_reply_tx(struct iwl4965_priv *priv,
3725 struct iwl4965_rx_mem_buffer *rxb)
3727 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3728 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3729 int txq_id = SEQ_TO_QUEUE(sequence);
3730 int index = SEQ_TO_INDEX(sequence);
3731 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3732 struct ieee80211_tx_status *tx_status;
3733 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3734 u32 status = le32_to_cpu(tx_resp->status);
3735 #ifdef CONFIG_IWL4965_HT
3736 #ifdef CONFIG_IWL4965_HT_AGG
3737 int tid, sta_id;
3738 #endif
3739 #endif
3741 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3742 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3743 "is out of range [0-%d] %d %d\n", txq_id,
3744 index, txq->q.n_bd, txq->q.write_ptr,
3745 txq->q.read_ptr);
3746 return;
3749 #ifdef CONFIG_IWL4965_HT
3750 #ifdef CONFIG_IWL4965_HT_AGG
3751 if (txq->sched_retry) {
3752 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3753 struct ieee80211_hdr *hdr =
3754 iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3755 struct iwl4965_ht_agg *agg = NULL;
3756 __le16 *qc = ieee80211_get_qos_ctrl(hdr);
3758 if (qc == NULL) {
3759 IWL_ERROR("BUG_ON qc is null!!!!\n");
3760 return;
3763 tid = le16_to_cpu(*qc) & 0xf;
3765 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3766 if (unlikely(sta_id == IWL_INVALID_STATION)) {
3767 IWL_ERROR("Station not known for\n");
3768 return;
3771 agg = &priv->stations[sta_id].tid[tid].agg;
3773 iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
3775 if ((tx_resp->frame_count == 1) &&
3776 !iwl4965_is_tx_success(status)) {
3777 /* TODO: send BAR */
3780 if ((txq->q.read_ptr != (scd_ssn & 0xff))) {
3781 index = iwl4965_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
3782 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3783 "%d index %d\n", scd_ssn , index);
3784 iwl4965_tx_queue_reclaim(priv, txq_id, index);
3786 } else {
3787 #endif /* CONFIG_IWL4965_HT_AGG */
3788 #endif /* CONFIG_IWL4965_HT */
3789 tx_status = &(txq->txb[txq->q.read_ptr].status);
3791 tx_status->retry_count = tx_resp->failure_frame;
3792 tx_status->queue_number = status;
3793 tx_status->queue_length = tx_resp->bt_kill_count;
3794 tx_status->queue_length |= tx_resp->failure_rts;
3796 tx_status->flags =
3797 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
3799 tx_status->control.tx_rate =
3800 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
3802 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
3803 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
3804 status, le32_to_cpu(tx_resp->rate_n_flags),
3805 tx_resp->failure_frame);
3807 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3808 if (index != -1)
3809 iwl4965_tx_queue_reclaim(priv, txq_id, index);
3810 #ifdef CONFIG_IWL4965_HT
3811 #ifdef CONFIG_IWL4965_HT_AGG
3813 #endif /* CONFIG_IWL4965_HT_AGG */
3814 #endif /* CONFIG_IWL4965_HT */
3816 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3817 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3821 static void iwl4965_rx_reply_alive(struct iwl4965_priv *priv,
3822 struct iwl4965_rx_mem_buffer *rxb)
3824 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3825 struct iwl4965_alive_resp *palive;
3826 struct delayed_work *pwork;
3828 palive = &pkt->u.alive_frame;
3830 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3831 "0x%01X 0x%01X\n",
3832 palive->is_valid, palive->ver_type,
3833 palive->ver_subtype);
3835 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3836 IWL_DEBUG_INFO("Initialization Alive received.\n");
3837 memcpy(&priv->card_alive_init,
3838 &pkt->u.alive_frame,
3839 sizeof(struct iwl4965_init_alive_resp));
3840 pwork = &priv->init_alive_start;
3841 } else {
3842 IWL_DEBUG_INFO("Runtime Alive received.\n");
3843 memcpy(&priv->card_alive, &pkt->u.alive_frame,
3844 sizeof(struct iwl4965_alive_resp));
3845 pwork = &priv->alive_start;
3848 /* We delay the ALIVE response by 5ms to
3849 * give the HW RF Kill time to activate... */
3850 if (palive->is_valid == UCODE_VALID_OK)
3851 queue_delayed_work(priv->workqueue, pwork,
3852 msecs_to_jiffies(5));
3853 else
3854 IWL_WARNING("uCode did not respond OK.\n");
3857 static void iwl4965_rx_reply_add_sta(struct iwl4965_priv *priv,
3858 struct iwl4965_rx_mem_buffer *rxb)
3860 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3862 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3863 return;
3866 static void iwl4965_rx_reply_error(struct iwl4965_priv *priv,
3867 struct iwl4965_rx_mem_buffer *rxb)
3869 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3871 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3872 "seq 0x%04X ser 0x%08X\n",
3873 le32_to_cpu(pkt->u.err_resp.error_type),
3874 get_cmd_string(pkt->u.err_resp.cmd_id),
3875 pkt->u.err_resp.cmd_id,
3876 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3877 le32_to_cpu(pkt->u.err_resp.error_info));
3880 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3882 static void iwl4965_rx_csa(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
3884 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3885 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3886 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
3887 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3888 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3889 rxon->channel = csa->channel;
3890 priv->staging_rxon.channel = csa->channel;
3893 static void iwl4965_rx_spectrum_measure_notif(struct iwl4965_priv *priv,
3894 struct iwl4965_rx_mem_buffer *rxb)
3896 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
3897 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3898 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
3900 if (!report->state) {
3901 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3902 "Spectrum Measure Notification: Start\n");
3903 return;
3906 memcpy(&priv->measure_report, report, sizeof(*report));
3907 priv->measurement_status |= MEASUREMENT_READY;
3908 #endif
3911 static void iwl4965_rx_pm_sleep_notif(struct iwl4965_priv *priv,
3912 struct iwl4965_rx_mem_buffer *rxb)
3914 #ifdef CONFIG_IWL4965_DEBUG
3915 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3916 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
3917 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3918 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3919 #endif
3922 static void iwl4965_rx_pm_debug_statistics_notif(struct iwl4965_priv *priv,
3923 struct iwl4965_rx_mem_buffer *rxb)
3925 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3926 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3927 "notification for %s:\n",
3928 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
3929 iwl4965_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
3932 static void iwl4965_bg_beacon_update(struct work_struct *work)
3934 struct iwl4965_priv *priv =
3935 container_of(work, struct iwl4965_priv, beacon_update);
3936 struct sk_buff *beacon;
3938 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3939 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
3941 if (!beacon) {
3942 IWL_ERROR("update beacon failed\n");
3943 return;
3946 mutex_lock(&priv->mutex);
3947 /* new beacon skb is allocated every time; dispose previous.*/
3948 if (priv->ibss_beacon)
3949 dev_kfree_skb(priv->ibss_beacon);
3951 priv->ibss_beacon = beacon;
3952 mutex_unlock(&priv->mutex);
3954 iwl4965_send_beacon_cmd(priv);
3957 static void iwl4965_rx_beacon_notif(struct iwl4965_priv *priv,
3958 struct iwl4965_rx_mem_buffer *rxb)
3960 #ifdef CONFIG_IWL4965_DEBUG
3961 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3962 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3963 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
3965 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3966 "tsf %d %d rate %d\n",
3967 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3968 beacon->beacon_notify_hdr.failure_frame,
3969 le32_to_cpu(beacon->ibss_mgr_status),
3970 le32_to_cpu(beacon->high_tsf),
3971 le32_to_cpu(beacon->low_tsf), rate);
3972 #endif
3974 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3975 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3976 queue_work(priv->workqueue, &priv->beacon_update);
3979 /* Service response to REPLY_SCAN_CMD (0x80) */
3980 static void iwl4965_rx_reply_scan(struct iwl4965_priv *priv,
3981 struct iwl4965_rx_mem_buffer *rxb)
3983 #ifdef CONFIG_IWL4965_DEBUG
3984 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3985 struct iwl4965_scanreq_notification *notif =
3986 (struct iwl4965_scanreq_notification *)pkt->u.raw;
3988 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3989 #endif
3992 /* Service SCAN_START_NOTIFICATION (0x82) */
3993 static void iwl4965_rx_scan_start_notif(struct iwl4965_priv *priv,
3994 struct iwl4965_rx_mem_buffer *rxb)
3996 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3997 struct iwl4965_scanstart_notification *notif =
3998 (struct iwl4965_scanstart_notification *)pkt->u.raw;
3999 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
4000 IWL_DEBUG_SCAN("Scan start: "
4001 "%d [802.11%s] "
4002 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
4003 notif->channel,
4004 notif->band ? "bg" : "a",
4005 notif->tsf_high,
4006 notif->tsf_low, notif->status, notif->beacon_timer);
4009 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
4010 static void iwl4965_rx_scan_results_notif(struct iwl4965_priv *priv,
4011 struct iwl4965_rx_mem_buffer *rxb)
4013 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
4014 struct iwl4965_scanresults_notification *notif =
4015 (struct iwl4965_scanresults_notification *)pkt->u.raw;
4017 IWL_DEBUG_SCAN("Scan ch.res: "
4018 "%d [802.11%s] "
4019 "(TSF: 0x%08X:%08X) - %d "
4020 "elapsed=%lu usec (%dms since last)\n",
4021 notif->channel,
4022 notif->band ? "bg" : "a",
4023 le32_to_cpu(notif->tsf_high),
4024 le32_to_cpu(notif->tsf_low),
4025 le32_to_cpu(notif->statistics[0]),
4026 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
4027 jiffies_to_msecs(elapsed_jiffies
4028 (priv->last_scan_jiffies, jiffies)));
4030 priv->last_scan_jiffies = jiffies;
4031 priv->next_scan_jiffies = 0;
4034 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
4035 static void iwl4965_rx_scan_complete_notif(struct iwl4965_priv *priv,
4036 struct iwl4965_rx_mem_buffer *rxb)
4038 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
4039 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
4041 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
4042 scan_notif->scanned_channels,
4043 scan_notif->tsf_low,
4044 scan_notif->tsf_high, scan_notif->status);
4046 /* The HW is no longer scanning */
4047 clear_bit(STATUS_SCAN_HW, &priv->status);
4049 /* The scan completion notification came in, so kill that timer... */
4050 cancel_delayed_work(&priv->scan_check);
4052 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
4053 (priv->scan_bands == 2) ? "2.4" : "5.2",
4054 jiffies_to_msecs(elapsed_jiffies
4055 (priv->scan_pass_start, jiffies)));
4057 /* Remove this scanned band from the list
4058 * of pending bands to scan */
4059 priv->scan_bands--;
4061 /* If a request to abort was given, or the scan did not succeed
4062 * then we reset the scan state machine and terminate,
4063 * re-queuing another scan if one has been requested */
4064 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4065 IWL_DEBUG_INFO("Aborted scan completed.\n");
4066 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
4067 } else {
4068 /* If there are more bands on this scan pass reschedule */
4069 if (priv->scan_bands > 0)
4070 goto reschedule;
4073 priv->last_scan_jiffies = jiffies;
4074 priv->next_scan_jiffies = 0;
4075 IWL_DEBUG_INFO("Setting scan to off\n");
4077 clear_bit(STATUS_SCANNING, &priv->status);
4079 IWL_DEBUG_INFO("Scan took %dms\n",
4080 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
4082 queue_work(priv->workqueue, &priv->scan_completed);
4084 return;
4086 reschedule:
4087 priv->scan_pass_start = jiffies;
4088 queue_work(priv->workqueue, &priv->request_scan);
4091 /* Handle notification from uCode that card's power state is changing
4092 * due to software, hardware, or critical temperature RFKILL */
4093 static void iwl4965_rx_card_state_notif(struct iwl4965_priv *priv,
4094 struct iwl4965_rx_mem_buffer *rxb)
4096 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
4097 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
4098 unsigned long status = priv->status;
4100 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
4101 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
4102 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
4104 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
4105 RF_CARD_DISABLED)) {
4107 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
4108 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4110 if (!iwl4965_grab_nic_access(priv)) {
4111 iwl4965_write_direct32(
4112 priv, HBUS_TARG_MBX_C,
4113 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4115 iwl4965_release_nic_access(priv);
4118 if (!(flags & RXON_CARD_DISABLED)) {
4119 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
4120 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4121 if (!iwl4965_grab_nic_access(priv)) {
4122 iwl4965_write_direct32(
4123 priv, HBUS_TARG_MBX_C,
4124 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4126 iwl4965_release_nic_access(priv);
4130 if (flags & RF_CARD_DISABLED) {
4131 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
4132 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
4133 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
4134 if (!iwl4965_grab_nic_access(priv))
4135 iwl4965_release_nic_access(priv);
4139 if (flags & HW_CARD_DISABLED)
4140 set_bit(STATUS_RF_KILL_HW, &priv->status);
4141 else
4142 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4145 if (flags & SW_CARD_DISABLED)
4146 set_bit(STATUS_RF_KILL_SW, &priv->status);
4147 else
4148 clear_bit(STATUS_RF_KILL_SW, &priv->status);
4150 if (!(flags & RXON_CARD_DISABLED))
4151 iwl4965_scan_cancel(priv);
4153 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
4154 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
4155 (test_bit(STATUS_RF_KILL_SW, &status) !=
4156 test_bit(STATUS_RF_KILL_SW, &priv->status)))
4157 queue_work(priv->workqueue, &priv->rf_kill);
4158 else
4159 wake_up_interruptible(&priv->wait_command_queue);
4163 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
4165 * Setup the RX handlers for each of the reply types sent from the uCode
4166 * to the host.
4168 * This function chains into the hardware specific files for them to setup
4169 * any hardware specific handlers as well.
4171 static void iwl4965_setup_rx_handlers(struct iwl4965_priv *priv)
4173 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
4174 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
4175 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
4176 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
4177 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
4178 iwl4965_rx_spectrum_measure_notif;
4179 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
4180 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
4181 iwl4965_rx_pm_debug_statistics_notif;
4182 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
4185 * The same handler is used for both the REPLY to a discrete
4186 * statistics request from the host as well as for the periodic
4187 * statistics notifications (after received beacons) from the uCode.
4189 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
4190 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
4192 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
4193 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
4194 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
4195 iwl4965_rx_scan_results_notif;
4196 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
4197 iwl4965_rx_scan_complete_notif;
4198 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
4199 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
4201 /* Set up hardware specific Rx handlers */
4202 iwl4965_hw_rx_handler_setup(priv);
4206 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
4207 * @rxb: Rx buffer to reclaim
4209 * If an Rx buffer has an async callback associated with it the callback
4210 * will be executed. The attached skb (if present) will only be freed
4211 * if the callback returns 1
4213 static void iwl4965_tx_cmd_complete(struct iwl4965_priv *priv,
4214 struct iwl4965_rx_mem_buffer *rxb)
4216 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
4217 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
4218 int txq_id = SEQ_TO_QUEUE(sequence);
4219 int index = SEQ_TO_INDEX(sequence);
4220 int huge = sequence & SEQ_HUGE_FRAME;
4221 int cmd_index;
4222 struct iwl4965_cmd *cmd;
4224 /* If a Tx command is being handled and it isn't in the actual
4225 * command queue then there a command routing bug has been introduced
4226 * in the queue management code. */
4227 if (txq_id != IWL_CMD_QUEUE_NUM)
4228 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4229 txq_id, pkt->hdr.cmd);
4230 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4232 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4233 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4235 /* Input error checking is done when commands are added to queue. */
4236 if (cmd->meta.flags & CMD_WANT_SKB) {
4237 cmd->meta.source->u.skb = rxb->skb;
4238 rxb->skb = NULL;
4239 } else if (cmd->meta.u.callback &&
4240 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4241 rxb->skb = NULL;
4243 iwl4965_tx_queue_reclaim(priv, txq_id, index);
4245 if (!(cmd->meta.flags & CMD_ASYNC)) {
4246 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4247 wake_up_interruptible(&priv->wait_command_queue);
4251 /************************** RX-FUNCTIONS ****************************/
4253 * Rx theory of operation
4255 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
4256 * each of which point to Receive Buffers to be filled by 4965. These get
4257 * used not only for Rx frames, but for any command response or notification
4258 * from the 4965. The driver and 4965 manage the Rx buffers by means
4259 * of indexes into the circular buffer.
4261 * Rx Queue Indexes
4262 * The host/firmware share two index registers for managing the Rx buffers.
4264 * The READ index maps to the first position that the firmware may be writing
4265 * to -- the driver can read up to (but not including) this position and get
4266 * good data.
4267 * The READ index is managed by the firmware once the card is enabled.
4269 * The WRITE index maps to the last position the driver has read from -- the
4270 * position preceding WRITE is the last slot the firmware can place a packet.
4272 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4273 * WRITE = READ.
4275 * During initialization, the host sets up the READ queue position to the first
4276 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4278 * When the firmware places a packet in a buffer, it will advance the READ index
4279 * and fire the RX interrupt. The driver can then query the READ index and
4280 * process as many packets as possible, moving the WRITE index forward as it
4281 * resets the Rx queue buffers with new memory.
4283 * The management in the driver is as follows:
4284 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4285 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
4286 * to replenish the iwl->rxq->rx_free.
4287 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
4288 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4289 * 'processed' and 'read' driver indexes as well)
4290 * + A received packet is processed and handed to the kernel network stack,
4291 * detached from the iwl->rxq. The driver 'processed' index is updated.
4292 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4293 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4294 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4295 * were enough free buffers and RX_STALLED is set it is cleared.
4298 * Driver sequence:
4300 * iwl4965_rx_queue_alloc() Allocates rx_free
4301 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
4302 * iwl4965_rx_queue_restock
4303 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
4304 * queue, updates firmware pointers, and updates
4305 * the WRITE index. If insufficient rx_free buffers
4306 * are available, schedules iwl4965_rx_replenish
4308 * -- enable interrupts --
4309 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
4310 * READ INDEX, detaching the SKB from the pool.
4311 * Moves the packet buffer from queue to rx_used.
4312 * Calls iwl4965_rx_queue_restock to refill any empty
4313 * slots.
4314 * ...
4319 * iwl4965_rx_queue_space - Return number of free slots available in queue.
4321 static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
4323 int s = q->read - q->write;
4324 if (s <= 0)
4325 s += RX_QUEUE_SIZE;
4326 /* keep some buffer to not confuse full and empty queue */
4327 s -= 2;
4328 if (s < 0)
4329 s = 0;
4330 return s;
4334 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
4336 int iwl4965_rx_queue_update_write_ptr(struct iwl4965_priv *priv, struct iwl4965_rx_queue *q)
4338 u32 reg = 0;
4339 int rc = 0;
4340 unsigned long flags;
4342 spin_lock_irqsave(&q->lock, flags);
4344 if (q->need_update == 0)
4345 goto exit_unlock;
4347 /* If power-saving is in use, make sure device is awake */
4348 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4349 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
4351 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4352 iwl4965_set_bit(priv, CSR_GP_CNTRL,
4353 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4354 goto exit_unlock;
4357 rc = iwl4965_grab_nic_access(priv);
4358 if (rc)
4359 goto exit_unlock;
4361 /* Device expects a multiple of 8 */
4362 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
4363 q->write & ~0x7);
4364 iwl4965_release_nic_access(priv);
4366 /* Else device is assumed to be awake */
4367 } else
4368 /* Device expects a multiple of 8 */
4369 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
4372 q->need_update = 0;
4374 exit_unlock:
4375 spin_unlock_irqrestore(&q->lock, flags);
4376 return rc;
4380 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
4382 static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl4965_priv *priv,
4383 dma_addr_t dma_addr)
4385 return cpu_to_le32((u32)(dma_addr >> 8));
4390 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
4392 * If there are slots in the RX queue that need to be restocked,
4393 * and we have free pre-allocated buffers, fill the ranks as much
4394 * as we can, pulling from rx_free.
4396 * This moves the 'write' index forward to catch up with 'processed', and
4397 * also updates the memory address in the firmware to reference the new
4398 * target buffer.
4400 static int iwl4965_rx_queue_restock(struct iwl4965_priv *priv)
4402 struct iwl4965_rx_queue *rxq = &priv->rxq;
4403 struct list_head *element;
4404 struct iwl4965_rx_mem_buffer *rxb;
4405 unsigned long flags;
4406 int write, rc;
4408 spin_lock_irqsave(&rxq->lock, flags);
4409 write = rxq->write & ~0x7;
4410 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
4411 /* Get next free Rx buffer, remove from free list */
4412 element = rxq->rx_free.next;
4413 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
4414 list_del(element);
4416 /* Point to Rx buffer via next RBD in circular buffer */
4417 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
4418 rxq->queue[rxq->write] = rxb;
4419 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4420 rxq->free_count--;
4422 spin_unlock_irqrestore(&rxq->lock, flags);
4423 /* If the pre-allocated buffer pool is dropping low, schedule to
4424 * refill it */
4425 if (rxq->free_count <= RX_LOW_WATERMARK)
4426 queue_work(priv->workqueue, &priv->rx_replenish);
4429 /* If we've added more space for the firmware to place data, tell it.
4430 * Increment device's write pointer in multiples of 8. */
4431 if ((write != (rxq->write & ~0x7))
4432 || (abs(rxq->write - rxq->read) > 7)) {
4433 spin_lock_irqsave(&rxq->lock, flags);
4434 rxq->need_update = 1;
4435 spin_unlock_irqrestore(&rxq->lock, flags);
4436 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
4437 if (rc)
4438 return rc;
4441 return 0;
4445 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
4447 * When moving to rx_free an SKB is allocated for the slot.
4449 * Also restock the Rx queue via iwl4965_rx_queue_restock.
4450 * This is called as a scheduled work item (except for during initialization)
4452 static void iwl4965_rx_allocate(struct iwl4965_priv *priv)
4454 struct iwl4965_rx_queue *rxq = &priv->rxq;
4455 struct list_head *element;
4456 struct iwl4965_rx_mem_buffer *rxb;
4457 unsigned long flags;
4458 spin_lock_irqsave(&rxq->lock, flags);
4459 while (!list_empty(&rxq->rx_used)) {
4460 element = rxq->rx_used.next;
4461 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
4463 /* Alloc a new receive buffer */
4464 rxb->skb =
4465 alloc_skb(priv->hw_setting.rx_buf_size,
4466 __GFP_NOWARN | GFP_ATOMIC);
4467 if (!rxb->skb) {
4468 if (net_ratelimit())
4469 printk(KERN_CRIT DRV_NAME
4470 ": Can not allocate SKB buffers\n");
4471 /* We don't reschedule replenish work here -- we will
4472 * call the restock method and if it still needs
4473 * more buffers it will schedule replenish */
4474 break;
4476 priv->alloc_rxb_skb++;
4477 list_del(element);
4479 /* Get physical address of RB/SKB */
4480 rxb->dma_addr =
4481 pci_map_single(priv->pci_dev, rxb->skb->data,
4482 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
4483 list_add_tail(&rxb->list, &rxq->rx_free);
4484 rxq->free_count++;
4486 spin_unlock_irqrestore(&rxq->lock, flags);
4490 * this should be called while priv->lock is locked
4492 static void __iwl4965_rx_replenish(void *data)
4494 struct iwl4965_priv *priv = data;
4496 iwl4965_rx_allocate(priv);
4497 iwl4965_rx_queue_restock(priv);
4501 void iwl4965_rx_replenish(void *data)
4503 struct iwl4965_priv *priv = data;
4504 unsigned long flags;
4506 iwl4965_rx_allocate(priv);
4508 spin_lock_irqsave(&priv->lock, flags);
4509 iwl4965_rx_queue_restock(priv);
4510 spin_unlock_irqrestore(&priv->lock, flags);
4513 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4514 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
4515 * This free routine walks the list of POOL entries and if SKB is set to
4516 * non NULL it is unmapped and freed
4518 static void iwl4965_rx_queue_free(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
4520 int i;
4521 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4522 if (rxq->pool[i].skb != NULL) {
4523 pci_unmap_single(priv->pci_dev,
4524 rxq->pool[i].dma_addr,
4525 priv->hw_setting.rx_buf_size,
4526 PCI_DMA_FROMDEVICE);
4527 dev_kfree_skb(rxq->pool[i].skb);
4531 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4532 rxq->dma_addr);
4533 rxq->bd = NULL;
4536 int iwl4965_rx_queue_alloc(struct iwl4965_priv *priv)
4538 struct iwl4965_rx_queue *rxq = &priv->rxq;
4539 struct pci_dev *dev = priv->pci_dev;
4540 int i;
4542 spin_lock_init(&rxq->lock);
4543 INIT_LIST_HEAD(&rxq->rx_free);
4544 INIT_LIST_HEAD(&rxq->rx_used);
4546 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
4547 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4548 if (!rxq->bd)
4549 return -ENOMEM;
4551 /* Fill the rx_used queue with _all_ of the Rx buffers */
4552 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4553 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4555 /* Set us so that we have processed and used all buffers, but have
4556 * not restocked the Rx queue with fresh buffers */
4557 rxq->read = rxq->write = 0;
4558 rxq->free_count = 0;
4559 rxq->need_update = 0;
4560 return 0;
4563 void iwl4965_rx_queue_reset(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
4565 unsigned long flags;
4566 int i;
4567 spin_lock_irqsave(&rxq->lock, flags);
4568 INIT_LIST_HEAD(&rxq->rx_free);
4569 INIT_LIST_HEAD(&rxq->rx_used);
4570 /* Fill the rx_used queue with _all_ of the Rx buffers */
4571 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4572 /* In the reset function, these buffers may have been allocated
4573 * to an SKB, so we need to unmap and free potential storage */
4574 if (rxq->pool[i].skb != NULL) {
4575 pci_unmap_single(priv->pci_dev,
4576 rxq->pool[i].dma_addr,
4577 priv->hw_setting.rx_buf_size,
4578 PCI_DMA_FROMDEVICE);
4579 priv->alloc_rxb_skb--;
4580 dev_kfree_skb(rxq->pool[i].skb);
4581 rxq->pool[i].skb = NULL;
4583 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4586 /* Set us so that we have processed and used all buffers, but have
4587 * not restocked the Rx queue with fresh buffers */
4588 rxq->read = rxq->write = 0;
4589 rxq->free_count = 0;
4590 spin_unlock_irqrestore(&rxq->lock, flags);
4593 /* Convert linear signal-to-noise ratio into dB */
4594 static u8 ratio2dB[100] = {
4595 /* 0 1 2 3 4 5 6 7 8 9 */
4596 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4597 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4598 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4599 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4600 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4601 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4602 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4603 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4604 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4605 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4608 /* Calculates a relative dB value from a ratio of linear
4609 * (i.e. not dB) signal levels.
4610 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
4611 int iwl4965_calc_db_from_ratio(int sig_ratio)
4613 /* 1000:1 or higher just report as 60 dB */
4614 if (sig_ratio >= 1000)
4615 return 60;
4617 /* 100:1 or higher, divide by 10 and use table,
4618 * add 20 dB to make up for divide by 10 */
4619 if (sig_ratio >= 100)
4620 return (20 + (int)ratio2dB[sig_ratio/10]);
4622 /* We shouldn't see this */
4623 if (sig_ratio < 1)
4624 return 0;
4626 /* Use table for ratios 1:1 - 99:1 */
4627 return (int)ratio2dB[sig_ratio];
4630 #define PERFECT_RSSI (-20) /* dBm */
4631 #define WORST_RSSI (-95) /* dBm */
4632 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4634 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
4635 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4636 * about formulas used below. */
4637 int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
4639 int sig_qual;
4640 int degradation = PERFECT_RSSI - rssi_dbm;
4642 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4643 * as indicator; formula is (signal dbm - noise dbm).
4644 * SNR at or above 40 is a great signal (100%).
4645 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4646 * Weakest usable signal is usually 10 - 15 dB SNR. */
4647 if (noise_dbm) {
4648 if (rssi_dbm - noise_dbm >= 40)
4649 return 100;
4650 else if (rssi_dbm < noise_dbm)
4651 return 0;
4652 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4654 /* Else use just the signal level.
4655 * This formula is a least squares fit of data points collected and
4656 * compared with a reference system that had a percentage (%) display
4657 * for signal quality. */
4658 } else
4659 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4660 (15 * RSSI_RANGE + 62 * degradation)) /
4661 (RSSI_RANGE * RSSI_RANGE);
4663 if (sig_qual > 100)
4664 sig_qual = 100;
4665 else if (sig_qual < 1)
4666 sig_qual = 0;
4668 return sig_qual;
4672 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
4674 * Uses the priv->rx_handlers callback function array to invoke
4675 * the appropriate handlers, including command responses,
4676 * frame-received notifications, and other notifications.
4678 static void iwl4965_rx_handle(struct iwl4965_priv *priv)
4680 struct iwl4965_rx_mem_buffer *rxb;
4681 struct iwl4965_rx_packet *pkt;
4682 struct iwl4965_rx_queue *rxq = &priv->rxq;
4683 u32 r, i;
4684 int reclaim;
4685 unsigned long flags;
4686 u8 fill_rx = 0;
4687 <<<<<<< HEAD:drivers/net/wireless/iwlwifi/iwl4965-base.c
4688 u32 count = 0;
4689 =======
4690 u32 count = 8;
4691 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/iwlwifi/iwl4965-base.c
4693 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4694 * buffer that the driver may process (last buffer filled by ucode). */
4695 r = iwl4965_hw_get_rx_read(priv);
4696 i = rxq->read;
4698 /* Rx interrupt, but nothing sent from uCode */
4699 if (i == r)
4700 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4702 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4703 fill_rx = 1;
4705 while (i != r) {
4706 rxb = rxq->queue[i];
4708 /* If an RXB doesn't have a Rx queue slot associated with it,
4709 * then a bug has been introduced in the queue refilling
4710 * routines -- catch it here */
4711 BUG_ON(rxb == NULL);
4713 rxq->queue[i] = NULL;
4715 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4716 priv->hw_setting.rx_buf_size,
4717 PCI_DMA_FROMDEVICE);
4718 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
4720 /* Reclaim a command buffer only if this packet is a response
4721 * to a (driver-originated) command.
4722 * If the packet (e.g. Rx frame) originated from uCode,
4723 * there is no command buffer to reclaim.
4724 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4725 * but apparently a few don't get set; catch them here. */
4726 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4727 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4728 (pkt->hdr.cmd != REPLY_4965_RX) &&
4729 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
4730 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4731 (pkt->hdr.cmd != REPLY_TX);
4733 /* Based on type of command response or notification,
4734 * handle those that need handling via function in
4735 * rx_handlers table. See iwl4965_setup_rx_handlers() */
4736 if (priv->rx_handlers[pkt->hdr.cmd]) {
4737 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4738 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4739 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4740 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4741 } else {
4742 /* No handling needed */
4743 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4744 "r %d i %d No handler needed for %s, 0x%02x\n",
4745 r, i, get_cmd_string(pkt->hdr.cmd),
4746 pkt->hdr.cmd);
4749 if (reclaim) {
4750 /* Invoke any callbacks, transfer the skb to caller, and
4751 * fire off the (possibly) blocking iwl4965_send_cmd()
4752 * as we reclaim the driver command queue */
4753 if (rxb && rxb->skb)
4754 iwl4965_tx_cmd_complete(priv, rxb);
4755 else
4756 IWL_WARNING("Claim null rxb?\n");
4759 /* For now we just don't re-use anything. We can tweak this
4760 * later to try and re-use notification packets and SKBs that
4761 * fail to Rx correctly */
4762 if (rxb->skb != NULL) {
4763 priv->alloc_rxb_skb--;
4764 dev_kfree_skb_any(rxb->skb);
4765 rxb->skb = NULL;
4768 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4769 priv->hw_setting.rx_buf_size,
4770 PCI_DMA_FROMDEVICE);
4771 spin_lock_irqsave(&rxq->lock, flags);
4772 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4773 spin_unlock_irqrestore(&rxq->lock, flags);
4774 i = (i + 1) & RX_QUEUE_MASK;
4775 /* If there are a lot of unused frames,
4776 * restock the Rx queue so ucode wont assert. */
4777 if (fill_rx) {
4778 count++;
4779 if (count >= 8) {
4780 priv->rxq.read = i;
4781 __iwl4965_rx_replenish(priv);
4782 count = 0;
4787 /* Backtrack one entry */
4788 priv->rxq.read = i;
4789 iwl4965_rx_queue_restock(priv);
4793 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4795 static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
4796 struct iwl4965_tx_queue *txq)
4798 u32 reg = 0;
4799 int rc = 0;
4800 int txq_id = txq->q.id;
4802 if (txq->need_update == 0)
4803 return rc;
4805 /* if we're trying to save power */
4806 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4807 /* wake up nic if it's powered down ...
4808 * uCode will wake up, and interrupt us again, so next
4809 * time we'll skip this part. */
4810 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
4812 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4813 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
4814 iwl4965_set_bit(priv, CSR_GP_CNTRL,
4815 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4816 return rc;
4819 /* restore this queue's parameters in nic hardware. */
4820 rc = iwl4965_grab_nic_access(priv);
4821 if (rc)
4822 return rc;
4823 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
4824 txq->q.write_ptr | (txq_id << 8));
4825 iwl4965_release_nic_access(priv);
4827 /* else not in power-save mode, uCode will never sleep when we're
4828 * trying to tx (during RFKILL, we're not trying to tx). */
4829 } else
4830 iwl4965_write32(priv, HBUS_TARG_WRPTR,
4831 txq->q.write_ptr | (txq_id << 8));
4833 txq->need_update = 0;
4835 return rc;
4838 #ifdef CONFIG_IWL4965_DEBUG
4839 static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
4841 DECLARE_MAC_BUF(mac);
4843 IWL_DEBUG_RADIO("RX CONFIG:\n");
4844 iwl4965_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
4845 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4846 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4847 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4848 le32_to_cpu(rxon->filter_flags));
4849 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4850 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4851 rxon->ofdm_basic_rates);
4852 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
4853 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4854 print_mac(mac, rxon->node_addr));
4855 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4856 print_mac(mac, rxon->bssid_addr));
4857 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4859 #endif
4861 static void iwl4965_enable_interrupts(struct iwl4965_priv *priv)
4863 IWL_DEBUG_ISR("Enabling interrupts\n");
4864 set_bit(STATUS_INT_ENABLED, &priv->status);
4865 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
4868 static inline void iwl4965_disable_interrupts(struct iwl4965_priv *priv)
4870 clear_bit(STATUS_INT_ENABLED, &priv->status);
4872 /* disable interrupts from uCode/NIC to host */
4873 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
4875 /* acknowledge/clear/reset any interrupts still pending
4876 * from uCode or flow handler (Rx/Tx DMA) */
4877 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4878 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
4879 IWL_DEBUG_ISR("Disabled interrupts\n");
4882 static const char *desc_lookup(int i)
4884 switch (i) {
4885 case 1:
4886 return "FAIL";
4887 case 2:
4888 return "BAD_PARAM";
4889 case 3:
4890 return "BAD_CHECKSUM";
4891 case 4:
4892 return "NMI_INTERRUPT";
4893 case 5:
4894 return "SYSASSERT";
4895 case 6:
4896 return "FATAL_ERROR";
4899 return "UNKNOWN";
4902 #define ERROR_START_OFFSET (1 * sizeof(u32))
4903 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
4905 static void iwl4965_dump_nic_error_log(struct iwl4965_priv *priv)
4907 u32 data2, line;
4908 u32 desc, time, count, base, data1;
4909 u32 blink1, blink2, ilink1, ilink2;
4910 int rc;
4912 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4914 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
4915 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4916 return;
4919 rc = iwl4965_grab_nic_access(priv);
4920 if (rc) {
4921 IWL_WARNING("Can not read from adapter at this time.\n");
4922 return;
4925 count = iwl4965_read_targ_mem(priv, base);
4927 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4928 IWL_ERROR("Start IWL Error Log Dump:\n");
4929 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4930 priv->status, priv->config, count);
4933 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4934 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4935 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4936 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4937 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4938 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4939 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4940 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4941 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
4943 IWL_ERROR("Desc Time "
4944 "data1 data2 line\n");
4945 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4946 desc_lookup(desc), desc, time, data1, data2, line);
4947 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4948 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4949 ilink1, ilink2);
4951 iwl4965_release_nic_access(priv);
4954 #define EVENT_START_OFFSET (4 * sizeof(u32))
4957 * iwl4965_print_event_log - Dump error event log to syslog
4959 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
4961 static void iwl4965_print_event_log(struct iwl4965_priv *priv, u32 start_idx,
4962 u32 num_events, u32 mode)
4964 u32 i;
4965 u32 base; /* SRAM byte address of event log header */
4966 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4967 u32 ptr; /* SRAM byte address of log data */
4968 u32 ev, time, data; /* event log data */
4970 if (num_events == 0)
4971 return;
4973 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4975 if (mode == 0)
4976 event_size = 2 * sizeof(u32);
4977 else
4978 event_size = 3 * sizeof(u32);
4980 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4982 /* "time" is actually "data" for mode 0 (no timestamp).
4983 * place event id # at far right for easier visual parsing. */
4984 for (i = 0; i < num_events; i++) {
4985 ev = iwl4965_read_targ_mem(priv, ptr);
4986 ptr += sizeof(u32);
4987 time = iwl4965_read_targ_mem(priv, ptr);
4988 ptr += sizeof(u32);
4989 if (mode == 0)
4990 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4991 else {
4992 data = iwl4965_read_targ_mem(priv, ptr);
4993 ptr += sizeof(u32);
4994 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4999 static void iwl4965_dump_nic_event_log(struct iwl4965_priv *priv)
5001 int rc;
5002 u32 base; /* SRAM byte address of event log header */
5003 u32 capacity; /* event log capacity in # entries */
5004 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
5005 u32 num_wraps; /* # times uCode wrapped to top of log */
5006 u32 next_entry; /* index of next entry to be written by uCode */
5007 u32 size; /* # entries that we'll print */
5009 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
5010 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
5011 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
5012 return;
5015 rc = iwl4965_grab_nic_access(priv);
5016 if (rc) {
5017 IWL_WARNING("Can not read from adapter at this time.\n");
5018 return;
5021 /* event log header */
5022 capacity = iwl4965_read_targ_mem(priv, base);
5023 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
5024 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
5025 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
5027 size = num_wraps ? capacity : next_entry;
5029 /* bail out if nothing in log */
5030 if (size == 0) {
5031 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
5032 iwl4965_release_nic_access(priv);
5033 return;
5036 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
5037 size, num_wraps);
5039 /* if uCode has wrapped back to top of log, start at the oldest entry,
5040 * i.e the next one that uCode would fill. */
5041 if (num_wraps)
5042 iwl4965_print_event_log(priv, next_entry,
5043 capacity - next_entry, mode);
5045 /* (then/else) start at top of log */
5046 iwl4965_print_event_log(priv, 0, next_entry, mode);
5048 iwl4965_release_nic_access(priv);
5052 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
5054 static void iwl4965_irq_handle_error(struct iwl4965_priv *priv)
5056 /* Set the FW error flag -- cleared on iwl4965_down */
5057 set_bit(STATUS_FW_ERROR, &priv->status);
5059 /* Cancel currently queued command. */
5060 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
5062 #ifdef CONFIG_IWL4965_DEBUG
5063 if (iwl4965_debug_level & IWL_DL_FW_ERRORS) {
5064 iwl4965_dump_nic_error_log(priv);
5065 iwl4965_dump_nic_event_log(priv);
5066 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
5068 #endif
5070 wake_up_interruptible(&priv->wait_command_queue);
5072 /* Keep the restart process from trying to send host
5073 * commands by clearing the INIT status bit */
5074 clear_bit(STATUS_READY, &priv->status);
5076 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5077 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
5078 "Restarting adapter due to uCode error.\n");
5080 if (iwl4965_is_associated(priv)) {
5081 memcpy(&priv->recovery_rxon, &priv->active_rxon,
5082 sizeof(priv->recovery_rxon));
5083 priv->error_recovering = 1;
5085 queue_work(priv->workqueue, &priv->restart);
5089 static void iwl4965_error_recovery(struct iwl4965_priv *priv)
5091 unsigned long flags;
5093 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
5094 sizeof(priv->staging_rxon));
5095 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5096 iwl4965_commit_rxon(priv);
5098 iwl4965_rxon_add_station(priv, priv->bssid, 1);
5100 spin_lock_irqsave(&priv->lock, flags);
5101 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
5102 priv->error_recovering = 0;
5103 spin_unlock_irqrestore(&priv->lock, flags);
5106 static void iwl4965_irq_tasklet(struct iwl4965_priv *priv)
5108 u32 inta, handled = 0;
5109 u32 inta_fh;
5110 unsigned long flags;
5111 #ifdef CONFIG_IWL4965_DEBUG
5112 u32 inta_mask;
5113 #endif
5115 spin_lock_irqsave(&priv->lock, flags);
5117 /* Ack/clear/reset pending uCode interrupts.
5118 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
5119 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
5120 inta = iwl4965_read32(priv, CSR_INT);
5121 iwl4965_write32(priv, CSR_INT, inta);
5123 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
5124 * Any new interrupts that happen after this, either while we're
5125 * in this tasklet, or later, will show up in next ISR/tasklet. */
5126 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
5127 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
5129 #ifdef CONFIG_IWL4965_DEBUG
5130 if (iwl4965_debug_level & IWL_DL_ISR) {
5131 /* just for debug */
5132 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
5133 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5134 inta, inta_mask, inta_fh);
5136 #endif
5138 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
5139 * atomic, make sure that inta covers all the interrupts that
5140 * we've discovered, even if FH interrupt came in just after
5141 * reading CSR_INT. */
5142 if (inta_fh & CSR_FH_INT_RX_MASK)
5143 inta |= CSR_INT_BIT_FH_RX;
5144 if (inta_fh & CSR_FH_INT_TX_MASK)
5145 inta |= CSR_INT_BIT_FH_TX;
5147 /* Now service all interrupt bits discovered above. */
5148 if (inta & CSR_INT_BIT_HW_ERR) {
5149 IWL_ERROR("Microcode HW error detected. Restarting.\n");
5151 /* Tell the device to stop sending interrupts */
5152 iwl4965_disable_interrupts(priv);
5154 iwl4965_irq_handle_error(priv);
5156 handled |= CSR_INT_BIT_HW_ERR;
5158 spin_unlock_irqrestore(&priv->lock, flags);
5160 return;
5163 #ifdef CONFIG_IWL4965_DEBUG
5164 if (iwl4965_debug_level & (IWL_DL_ISR)) {
5165 /* NIC fires this, but we don't use it, redundant with WAKEUP */
5166 if (inta & CSR_INT_BIT_SCD)
5167 IWL_DEBUG_ISR("Scheduler finished to transmit "
5168 "the frame/frames.\n");
5170 /* Alive notification via Rx interrupt will do the real work */
5171 if (inta & CSR_INT_BIT_ALIVE)
5172 IWL_DEBUG_ISR("Alive interrupt\n");
5174 #endif
5175 /* Safely ignore these bits for debug checks below */
5176 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
5178 /* HW RF KILL switch toggled */
5179 if (inta & CSR_INT_BIT_RF_KILL) {
5180 int hw_rf_kill = 0;
5181 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
5182 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5183 hw_rf_kill = 1;
5185 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
5186 "RF_KILL bit toggled to %s.\n",
5187 hw_rf_kill ? "disable radio":"enable radio");
5189 /* Queue restart only if RF_KILL switch was set to "kill"
5190 * when we loaded driver, and is now set to "enable".
5191 * After we're Alive, RF_KILL gets handled by
5192 * iwl_rx_card_state_notif() */
5193 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
5194 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5195 queue_work(priv->workqueue, &priv->restart);
5198 handled |= CSR_INT_BIT_RF_KILL;
5201 /* Chip got too hot and stopped itself */
5202 if (inta & CSR_INT_BIT_CT_KILL) {
5203 IWL_ERROR("Microcode CT kill error detected.\n");
5204 handled |= CSR_INT_BIT_CT_KILL;
5207 /* Error detected by uCode */
5208 if (inta & CSR_INT_BIT_SW_ERR) {
5209 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
5210 inta);
5211 iwl4965_irq_handle_error(priv);
5212 handled |= CSR_INT_BIT_SW_ERR;
5215 /* uCode wakes up after power-down sleep */
5216 if (inta & CSR_INT_BIT_WAKEUP) {
5217 IWL_DEBUG_ISR("Wakeup interrupt\n");
5218 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
5219 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
5220 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
5221 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
5222 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
5223 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
5224 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
5226 handled |= CSR_INT_BIT_WAKEUP;
5229 /* All uCode command responses, including Tx command responses,
5230 * Rx "responses" (frame-received notification), and other
5231 * notifications from uCode come through here*/
5232 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
5233 iwl4965_rx_handle(priv);
5234 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5237 if (inta & CSR_INT_BIT_FH_TX) {
5238 IWL_DEBUG_ISR("Tx interrupt\n");
5239 handled |= CSR_INT_BIT_FH_TX;
5242 if (inta & ~handled)
5243 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5245 if (inta & ~CSR_INI_SET_MASK) {
5246 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5247 inta & ~CSR_INI_SET_MASK);
5248 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5251 /* Re-enable all interrupts */
5252 iwl4965_enable_interrupts(priv);
5254 #ifdef CONFIG_IWL4965_DEBUG
5255 if (iwl4965_debug_level & (IWL_DL_ISR)) {
5256 inta = iwl4965_read32(priv, CSR_INT);
5257 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
5258 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
5259 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5260 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5262 #endif
5263 spin_unlock_irqrestore(&priv->lock, flags);
5266 static irqreturn_t iwl4965_isr(int irq, void *data)
5268 struct iwl4965_priv *priv = data;
5269 u32 inta, inta_mask;
5270 u32 inta_fh;
5271 if (!priv)
5272 return IRQ_NONE;
5274 spin_lock(&priv->lock);
5276 /* Disable (but don't clear!) interrupts here to avoid
5277 * back-to-back ISRs and sporadic interrupts from our NIC.
5278 * If we have something to service, the tasklet will re-enable ints.
5279 * If we *don't* have something, we'll re-enable before leaving here. */
5280 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
5281 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
5283 /* Discover which interrupts are active/pending */
5284 inta = iwl4965_read32(priv, CSR_INT);
5285 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
5287 /* Ignore interrupt if there's nothing in NIC to service.
5288 * This may be due to IRQ shared with another device,
5289 * or due to sporadic interrupts thrown from our NIC. */
5290 if (!inta && !inta_fh) {
5291 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5292 goto none;
5295 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
5296 /* Hardware disappeared. It might have already raised
5297 * an interrupt */
5298 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
5299 goto unplugged;
5302 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5303 inta, inta_mask, inta_fh);
5305 inta &= ~CSR_INT_BIT_SCD;
5307 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
5308 if (likely(inta || inta_fh))
5309 tasklet_schedule(&priv->irq_tasklet);
5311 unplugged:
5312 spin_unlock(&priv->lock);
5313 return IRQ_HANDLED;
5315 none:
5316 /* re-enable interrupts here since we don't have anything to service. */
5317 iwl4965_enable_interrupts(priv);
5318 spin_unlock(&priv->lock);
5319 return IRQ_NONE;
5322 /************************** EEPROM BANDS ****************************
5324 * The iwl4965_eeprom_band definitions below provide the mapping from the
5325 * EEPROM contents to the specific channel number supported for each
5326 * band.
5328 * For example, iwl4965_priv->eeprom.band_3_channels[4] from the band_3
5329 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5330 * The specific geography and calibration information for that channel
5331 * is contained in the eeprom map itself.
5333 * During init, we copy the eeprom information and channel map
5334 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5336 * channel_map_24/52 provides the index in the channel_info array for a
5337 * given channel. We have to have two separate maps as there is channel
5338 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5339 * band_2
5341 * A value of 0xff stored in the channel_map indicates that the channel
5342 * is not supported by the hardware at all.
5344 * A value of 0xfe in the channel_map indicates that the channel is not
5345 * valid for Tx with the current hardware. This means that
5346 * while the system can tune and receive on a given channel, it may not
5347 * be able to associate or transmit any frames on that
5348 * channel. There is no corresponding channel information for that
5349 * entry.
5351 *********************************************************************/
5353 /* 2.4 GHz */
5354 static const u8 iwl4965_eeprom_band_1[14] = {
5355 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5358 /* 5.2 GHz bands */
5359 static const u8 iwl4965_eeprom_band_2[] = { /* 4915-5080MHz */
5360 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5363 static const u8 iwl4965_eeprom_band_3[] = { /* 5170-5320MHz */
5364 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5367 static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
5368 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5371 static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
5372 145, 149, 153, 157, 161, 165
5375 static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
5376 1, 2, 3, 4, 5, 6, 7
5379 static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
5380 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5383 static void iwl4965_init_band_reference(const struct iwl4965_priv *priv,
5384 int band,
5385 int *eeprom_ch_count,
5386 const struct iwl4965_eeprom_channel
5387 **eeprom_ch_info,
5388 const u8 **eeprom_ch_index)
5390 switch (band) {
5391 case 1: /* 2.4GHz band */
5392 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
5393 *eeprom_ch_info = priv->eeprom.band_1_channels;
5394 *eeprom_ch_index = iwl4965_eeprom_band_1;
5395 break;
5396 case 2: /* 4.9GHz band */
5397 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
5398 *eeprom_ch_info = priv->eeprom.band_2_channels;
5399 *eeprom_ch_index = iwl4965_eeprom_band_2;
5400 break;
5401 case 3: /* 5.2GHz band */
5402 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
5403 *eeprom_ch_info = priv->eeprom.band_3_channels;
5404 *eeprom_ch_index = iwl4965_eeprom_band_3;
5405 break;
5406 case 4: /* 5.5GHz band */
5407 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
5408 *eeprom_ch_info = priv->eeprom.band_4_channels;
5409 *eeprom_ch_index = iwl4965_eeprom_band_4;
5410 break;
5411 case 5: /* 5.7GHz band */
5412 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
5413 *eeprom_ch_info = priv->eeprom.band_5_channels;
5414 *eeprom_ch_index = iwl4965_eeprom_band_5;
5415 break;
5416 case 6: /* 2.4GHz FAT channels */
5417 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
5418 *eeprom_ch_info = priv->eeprom.band_24_channels;
5419 *eeprom_ch_index = iwl4965_eeprom_band_6;
5420 break;
5421 case 7: /* 5 GHz FAT channels */
5422 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
5423 *eeprom_ch_info = priv->eeprom.band_52_channels;
5424 *eeprom_ch_index = iwl4965_eeprom_band_7;
5425 break;
5426 default:
5427 BUG();
5428 return;
5433 * iwl4965_get_channel_info - Find driver's private channel info
5435 * Based on band and channel number.
5437 const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl4965_priv *priv,
5438 int phymode, u16 channel)
5440 int i;
5442 switch (phymode) {
5443 case MODE_IEEE80211A:
5444 for (i = 14; i < priv->channel_count; i++) {
5445 if (priv->channel_info[i].channel == channel)
5446 return &priv->channel_info[i];
5448 break;
5450 case MODE_IEEE80211B:
5451 case MODE_IEEE80211G:
5452 if (channel >= 1 && channel <= 14)
5453 return &priv->channel_info[channel - 1];
5454 break;
5458 return NULL;
5461 #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5462 ? # x " " : "")
5465 * iwl4965_init_channel_map - Set up driver's info for all possible channels
5467 static int iwl4965_init_channel_map(struct iwl4965_priv *priv)
5469 int eeprom_ch_count = 0;
5470 const u8 *eeprom_ch_index = NULL;
5471 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
5472 int band, ch;
5473 struct iwl4965_channel_info *ch_info;
5475 if (priv->channel_count) {
5476 IWL_DEBUG_INFO("Channel map already initialized.\n");
5477 return 0;
5480 if (priv->eeprom.version < 0x2f) {
5481 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5482 priv->eeprom.version);
5483 return -EINVAL;
5486 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5488 priv->channel_count =
5489 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5490 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5491 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5492 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5493 ARRAY_SIZE(iwl4965_eeprom_band_5);
5495 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5497 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
5498 priv->channel_count, GFP_KERNEL);
5499 if (!priv->channel_info) {
5500 IWL_ERROR("Could not allocate channel_info\n");
5501 priv->channel_count = 0;
5502 return -ENOMEM;
5505 ch_info = priv->channel_info;
5507 /* Loop through the 5 EEPROM bands adding them in order to the
5508 * channel map we maintain (that contains additional information than
5509 * what just in the EEPROM) */
5510 for (band = 1; band <= 5; band++) {
5512 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
5513 &eeprom_ch_info, &eeprom_ch_index);
5515 /* Loop through each band adding each of the channels */
5516 for (ch = 0; ch < eeprom_ch_count; ch++) {
5517 ch_info->channel = eeprom_ch_index[ch];
5518 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5519 MODE_IEEE80211A;
5521 /* permanently store EEPROM's channel regulatory flags
5522 * and max power in channel info database. */
5523 ch_info->eeprom = eeprom_ch_info[ch];
5525 /* Copy the run-time flags so they are there even on
5526 * invalid channels */
5527 ch_info->flags = eeprom_ch_info[ch].flags;
5529 if (!(is_channel_valid(ch_info))) {
5530 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5531 "No traffic\n",
5532 ch_info->channel,
5533 ch_info->flags,
5534 is_channel_a_band(ch_info) ?
5535 "5.2" : "2.4");
5536 ch_info++;
5537 continue;
5540 /* Initialize regulatory-based run-time data */
5541 ch_info->max_power_avg = ch_info->curr_txpow =
5542 eeprom_ch_info[ch].max_power_avg;
5543 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5544 ch_info->min_power = 0;
5546 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5547 " %ddBm): Ad-Hoc %ssupported\n",
5548 ch_info->channel,
5549 is_channel_a_band(ch_info) ?
5550 "5.2" : "2.4",
5551 CHECK_AND_PRINT(IBSS),
5552 CHECK_AND_PRINT(ACTIVE),
5553 CHECK_AND_PRINT(RADAR),
5554 CHECK_AND_PRINT(WIDE),
5555 CHECK_AND_PRINT(NARROW),
5556 CHECK_AND_PRINT(DFS),
5557 eeprom_ch_info[ch].flags,
5558 eeprom_ch_info[ch].max_power_avg,
5559 ((eeprom_ch_info[ch].
5560 flags & EEPROM_CHANNEL_IBSS)
5561 && !(eeprom_ch_info[ch].
5562 flags & EEPROM_CHANNEL_RADAR))
5563 ? "" : "not ");
5565 /* Set the user_txpower_limit to the highest power
5566 * supported by any channel */
5567 if (eeprom_ch_info[ch].max_power_avg >
5568 priv->user_txpower_limit)
5569 priv->user_txpower_limit =
5570 eeprom_ch_info[ch].max_power_avg;
5572 ch_info++;
5576 /* Two additional EEPROM bands for 2.4 and 5 GHz FAT channels */
5577 for (band = 6; band <= 7; band++) {
5578 int phymode;
5579 u8 fat_extension_chan;
5581 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
5582 &eeprom_ch_info, &eeprom_ch_index);
5584 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
5585 phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
5587 /* Loop through each band adding each of the channels */
5588 for (ch = 0; ch < eeprom_ch_count; ch++) {
5590 if ((band == 6) &&
5591 ((eeprom_ch_index[ch] == 5) ||
5592 (eeprom_ch_index[ch] == 6) ||
5593 (eeprom_ch_index[ch] == 7)))
5594 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5595 else
5596 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5598 /* Set up driver's info for lower half */
5599 iwl4965_set_fat_chan_info(priv, phymode,
5600 eeprom_ch_index[ch],
5601 &(eeprom_ch_info[ch]),
5602 fat_extension_chan);
5604 /* Set up driver's info for upper half */
5605 iwl4965_set_fat_chan_info(priv, phymode,
5606 (eeprom_ch_index[ch] + 4),
5607 &(eeprom_ch_info[ch]),
5608 HT_IE_EXT_CHANNEL_BELOW);
5612 return 0;
5616 * iwl4965_free_channel_map - undo allocations in iwl4965_init_channel_map
5618 static void iwl4965_free_channel_map(struct iwl4965_priv *priv)
5620 kfree(priv->channel_info);
5621 priv->channel_count = 0;
5624 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5625 * sending probe req. This should be set long enough to hear probe responses
5626 * from more than one AP. */
5627 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5628 #define IWL_ACTIVE_DWELL_TIME_52 (10)
5630 /* For faster active scanning, scan will move to the next channel if fewer than
5631 * PLCP_QUIET_THRESH packets are heard on this channel within
5632 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5633 * time if it's a quiet channel (nothing responded to our probe, and there's
5634 * no other traffic).
5635 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5636 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5637 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5639 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5640 * Must be set longer than active dwell time.
5641 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5642 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5643 #define IWL_PASSIVE_DWELL_TIME_52 (10)
5644 #define IWL_PASSIVE_DWELL_BASE (100)
5645 #define IWL_CHANNEL_TUNE_TIME 5
5647 static inline u16 iwl4965_get_active_dwell_time(struct iwl4965_priv *priv, int phymode)
5649 if (phymode == MODE_IEEE80211A)
5650 return IWL_ACTIVE_DWELL_TIME_52;
5651 else
5652 return IWL_ACTIVE_DWELL_TIME_24;
5655 static u16 iwl4965_get_passive_dwell_time(struct iwl4965_priv *priv, int phymode)
5657 u16 active = iwl4965_get_active_dwell_time(priv, phymode);
5658 u16 passive = (phymode != MODE_IEEE80211A) ?
5659 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5660 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5662 if (iwl4965_is_associated(priv)) {
5663 /* If we're associated, we clamp the maximum passive
5664 * dwell time to be 98% of the beacon interval (minus
5665 * 2 * channel tune time) */
5666 passive = priv->beacon_int;
5667 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5668 passive = IWL_PASSIVE_DWELL_BASE;
5669 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5672 if (passive <= active)
5673 passive = active + 1;
5675 return passive;
5678 static int iwl4965_get_channels_for_scan(struct iwl4965_priv *priv, int phymode,
5679 u8 is_active, u8 direct_mask,
5680 struct iwl4965_scan_channel *scan_ch)
5682 const struct ieee80211_channel *channels = NULL;
5683 const struct ieee80211_hw_mode *hw_mode;
5684 const struct iwl4965_channel_info *ch_info;
5685 u16 passive_dwell = 0;
5686 u16 active_dwell = 0;
5687 int added, i;
5689 hw_mode = iwl4965_get_hw_mode(priv, phymode);
5690 if (!hw_mode)
5691 return 0;
5693 channels = hw_mode->channels;
5695 active_dwell = iwl4965_get_active_dwell_time(priv, phymode);
5696 passive_dwell = iwl4965_get_passive_dwell_time(priv, phymode);
5698 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5699 if (channels[i].chan ==
5700 le16_to_cpu(priv->active_rxon.channel)) {
5701 if (iwl4965_is_associated(priv)) {
5702 IWL_DEBUG_SCAN
5703 ("Skipping current channel %d\n",
5704 le16_to_cpu(priv->active_rxon.channel));
5705 continue;
5707 } else if (priv->only_active_channel)
5708 continue;
5710 scan_ch->channel = channels[i].chan;
5712 ch_info = iwl4965_get_channel_info(priv, phymode,
5713 scan_ch->channel);
5714 if (!is_channel_valid(ch_info)) {
5715 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5716 scan_ch->channel);
5717 continue;
5720 if (!is_active || is_channel_passive(ch_info) ||
5721 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5722 scan_ch->type = 0; /* passive */
5723 else
5724 scan_ch->type = 1; /* active */
5726 if (scan_ch->type & 1)
5727 scan_ch->type |= (direct_mask << 1);
5729 if (is_channel_narrow(ch_info))
5730 scan_ch->type |= (1 << 7);
5732 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5733 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5735 /* Set txpower levels to defaults */
5736 scan_ch->tpc.dsp_atten = 110;
5737 /* scan_pwr_info->tpc.dsp_atten; */
5739 /*scan_pwr_info->tpc.tx_gain; */
5740 if (phymode == MODE_IEEE80211A)
5741 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5742 else {
5743 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5744 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5745 * power level:
5746 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
5750 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5751 scan_ch->channel,
5752 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5753 (scan_ch->type & 1) ?
5754 active_dwell : passive_dwell);
5756 scan_ch++;
5757 added++;
5760 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5761 return added;
5764 static void iwl4965_reset_channel_flag(struct iwl4965_priv *priv)
5766 int i, j;
5767 for (i = 0; i < 3; i++) {
5768 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5769 for (j = 0; j < hw_mode->num_channels; j++)
5770 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5774 static void iwl4965_init_hw_rates(struct iwl4965_priv *priv,
5775 struct ieee80211_rate *rates)
5777 int i;
5779 for (i = 0; i < IWL_RATE_COUNT; i++) {
5780 rates[i].rate = iwl4965_rates[i].ieee * 5;
5781 rates[i].val = i; /* Rate scaling will work on indexes */
5782 rates[i].val2 = i;
5783 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5784 /* Only OFDM have the bits-per-symbol set */
5785 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5786 rates[i].flags |= IEEE80211_RATE_OFDM;
5787 else {
5789 * If CCK 1M then set rate flag to CCK else CCK_2
5790 * which is CCK | PREAMBLE2
5792 rates[i].flags |= (iwl4965_rates[i].plcp == 10) ?
5793 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5796 /* Set up which ones are basic rates... */
5797 if (IWL_BASIC_RATES_MASK & (1 << i))
5798 rates[i].flags |= IEEE80211_RATE_BASIC;
5803 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
5805 static int iwl4965_init_geos(struct iwl4965_priv *priv)
5807 struct iwl4965_channel_info *ch;
5808 struct ieee80211_hw_mode *modes;
5809 struct ieee80211_channel *channels;
5810 struct ieee80211_channel *geo_ch;
5811 struct ieee80211_rate *rates;
5812 int i = 0;
5813 enum {
5814 A = 0,
5815 B = 1,
5816 G = 2,
5818 int mode_count = 3;
5820 if (priv->modes) {
5821 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5822 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5823 return 0;
5826 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5827 GFP_KERNEL);
5828 if (!modes)
5829 return -ENOMEM;
5831 channels = kzalloc(sizeof(struct ieee80211_channel) *
5832 priv->channel_count, GFP_KERNEL);
5833 if (!channels) {
5834 kfree(modes);
5835 return -ENOMEM;
5838 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5839 GFP_KERNEL);
5840 if (!rates) {
5841 kfree(modes);
5842 kfree(channels);
5843 return -ENOMEM;
5846 /* 0 = 802.11a
5847 * 1 = 802.11b
5848 * 2 = 802.11g
5851 /* 5.2GHz channels start after the 2.4GHz channels */
5852 modes[A].mode = MODE_IEEE80211A;
5853 modes[A].channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
5854 modes[A].rates = rates;
5855 modes[A].num_rates = 8; /* just OFDM */
5856 modes[A].rates = &rates[4];
5857 modes[A].num_channels = 0;
5858 #ifdef CONFIG_IWL4965_HT
5859 iwl4965_init_ht_hw_capab(&modes[A].ht_info, MODE_IEEE80211A);
5860 #endif
5862 modes[B].mode = MODE_IEEE80211B;
5863 modes[B].channels = channels;
5864 modes[B].rates = rates;
5865 modes[B].num_rates = 4; /* just CCK */
5866 modes[B].num_channels = 0;
5868 modes[G].mode = MODE_IEEE80211G;
5869 modes[G].channels = channels;
5870 modes[G].rates = rates;
5871 modes[G].num_rates = 12; /* OFDM & CCK */
5872 modes[G].num_channels = 0;
5873 #ifdef CONFIG_IWL4965_HT
5874 iwl4965_init_ht_hw_capab(&modes[G].ht_info, MODE_IEEE80211G);
5875 #endif
5877 priv->ieee_channels = channels;
5878 priv->ieee_rates = rates;
5880 iwl4965_init_hw_rates(priv, rates);
5882 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5883 ch = &priv->channel_info[i];
5885 if (!is_channel_valid(ch)) {
5886 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5887 "skipping.\n",
5888 ch->channel, is_channel_a_band(ch) ?
5889 "5.2" : "2.4");
5890 continue;
5893 if (is_channel_a_band(ch)) {
5894 geo_ch = &modes[A].channels[modes[A].num_channels++];
5895 } else {
5896 geo_ch = &modes[B].channels[modes[B].num_channels++];
5897 modes[G].num_channels++;
5900 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5901 geo_ch->chan = ch->channel;
5902 geo_ch->power_level = ch->max_power_avg;
5903 geo_ch->antenna_max = 0xff;
5905 if (is_channel_valid(ch)) {
5906 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5907 if (ch->flags & EEPROM_CHANNEL_IBSS)
5908 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5910 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5911 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5913 if (ch->flags & EEPROM_CHANNEL_RADAR)
5914 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5916 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5917 priv->max_channel_txpower_limit =
5918 ch->max_power_avg;
5921 geo_ch->val = geo_ch->flag;
5924 if ((modes[A].num_channels == 0) && priv->is_abg) {
5925 printk(KERN_INFO DRV_NAME
5926 ": Incorrectly detected BG card as ABG. Please send "
5927 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5928 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5929 priv->is_abg = 0;
5932 printk(KERN_INFO DRV_NAME
5933 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5934 modes[G].num_channels, modes[A].num_channels);
5937 * NOTE: We register these in preference of order -- the
5938 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5939 * a phymode based on rates or AP capabilities but seems to
5940 * configure it purely on if the channel being configured
5941 * is supported by a mode -- and the first match is taken
5944 if (modes[G].num_channels)
5945 ieee80211_register_hwmode(priv->hw, &modes[G]);
5946 if (modes[B].num_channels)
5947 ieee80211_register_hwmode(priv->hw, &modes[B]);
5948 if (modes[A].num_channels)
5949 ieee80211_register_hwmode(priv->hw, &modes[A]);
5951 priv->modes = modes;
5952 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5954 return 0;
5958 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5960 static void iwl4965_free_geos(struct iwl4965_priv *priv)
5962 kfree(priv->modes);
5963 kfree(priv->ieee_channels);
5964 kfree(priv->ieee_rates);
5965 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5968 /******************************************************************************
5970 * uCode download functions
5972 ******************************************************************************/
5974 static void iwl4965_dealloc_ucode_pci(struct iwl4965_priv *priv)
5976 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5977 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5978 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5979 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5980 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5981 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
5985 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
5986 * looking at all data.
5988 static int iwl4965_verify_inst_full(struct iwl4965_priv *priv, __le32 *image,
5989 u32 len)
5991 u32 val;
5992 u32 save_len = len;
5993 int rc = 0;
5994 u32 errcnt;
5996 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5998 rc = iwl4965_grab_nic_access(priv);
5999 if (rc)
6000 return rc;
6002 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
6004 errcnt = 0;
6005 for (; len > 0; len -= sizeof(u32), image++) {
6006 /* read data comes through single port, auto-incr addr */
6007 /* NOTE: Use the debugless read so we don't flood kernel log
6008 * if IWL_DL_IO is set */
6009 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
6010 if (val != le32_to_cpu(*image)) {
6011 IWL_ERROR("uCode INST section is invalid at "
6012 "offset 0x%x, is 0x%x, s/b 0x%x\n",
6013 save_len - len, val, le32_to_cpu(*image));
6014 rc = -EIO;
6015 errcnt++;
6016 if (errcnt >= 20)
6017 break;
6021 iwl4965_release_nic_access(priv);
6023 if (!errcnt)
6024 IWL_DEBUG_INFO
6025 ("ucode image in INSTRUCTION memory is good\n");
6027 return rc;
6032 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
6033 * using sample data 100 bytes apart. If these sample points are good,
6034 * it's a pretty good bet that everything between them is good, too.
6036 static int iwl4965_verify_inst_sparse(struct iwl4965_priv *priv, __le32 *image, u32 len)
6038 u32 val;
6039 int rc = 0;
6040 u32 errcnt = 0;
6041 u32 i;
6043 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
6045 rc = iwl4965_grab_nic_access(priv);
6046 if (rc)
6047 return rc;
6049 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
6050 /* read data comes through single port, auto-incr addr */
6051 /* NOTE: Use the debugless read so we don't flood kernel log
6052 * if IWL_DL_IO is set */
6053 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
6054 i + RTC_INST_LOWER_BOUND);
6055 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
6056 if (val != le32_to_cpu(*image)) {
6057 #if 0 /* Enable this if you want to see details */
6058 IWL_ERROR("uCode INST section is invalid at "
6059 "offset 0x%x, is 0x%x, s/b 0x%x\n",
6060 i, val, *image);
6061 #endif
6062 rc = -EIO;
6063 errcnt++;
6064 if (errcnt >= 3)
6065 break;
6069 iwl4965_release_nic_access(priv);
6071 return rc;
6076 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
6077 * and verify its contents
6079 static int iwl4965_verify_ucode(struct iwl4965_priv *priv)
6081 __le32 *image;
6082 u32 len;
6083 int rc = 0;
6085 /* Try bootstrap */
6086 image = (__le32 *)priv->ucode_boot.v_addr;
6087 len = priv->ucode_boot.len;
6088 rc = iwl4965_verify_inst_sparse(priv, image, len);
6089 if (rc == 0) {
6090 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
6091 return 0;
6094 /* Try initialize */
6095 image = (__le32 *)priv->ucode_init.v_addr;
6096 len = priv->ucode_init.len;
6097 rc = iwl4965_verify_inst_sparse(priv, image, len);
6098 if (rc == 0) {
6099 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
6100 return 0;
6103 /* Try runtime/protocol */
6104 image = (__le32 *)priv->ucode_code.v_addr;
6105 len = priv->ucode_code.len;
6106 rc = iwl4965_verify_inst_sparse(priv, image, len);
6107 if (rc == 0) {
6108 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
6109 return 0;
6112 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
6114 /* Since nothing seems to match, show first several data entries in
6115 * instruction SRAM, so maybe visual inspection will give a clue.
6116 * Selection of bootstrap image (vs. other images) is arbitrary. */
6117 image = (__le32 *)priv->ucode_boot.v_addr;
6118 len = priv->ucode_boot.len;
6119 rc = iwl4965_verify_inst_full(priv, image, len);
6121 return rc;
6125 /* check contents of special bootstrap uCode SRAM */
6126 static int iwl4965_verify_bsm(struct iwl4965_priv *priv)
6128 __le32 *image = priv->ucode_boot.v_addr;
6129 u32 len = priv->ucode_boot.len;
6130 u32 reg;
6131 u32 val;
6133 IWL_DEBUG_INFO("Begin verify bsm\n");
6135 /* verify BSM SRAM contents */
6136 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
6137 for (reg = BSM_SRAM_LOWER_BOUND;
6138 reg < BSM_SRAM_LOWER_BOUND + len;
6139 reg += sizeof(u32), image ++) {
6140 val = iwl4965_read_prph(priv, reg);
6141 if (val != le32_to_cpu(*image)) {
6142 IWL_ERROR("BSM uCode verification failed at "
6143 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
6144 BSM_SRAM_LOWER_BOUND,
6145 reg - BSM_SRAM_LOWER_BOUND, len,
6146 val, le32_to_cpu(*image));
6147 return -EIO;
6151 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
6153 return 0;
6157 * iwl4965_load_bsm - Load bootstrap instructions
6159 * BSM operation:
6161 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
6162 * in special SRAM that does not power down during RFKILL. When powering back
6163 * up after power-saving sleeps (or during initial uCode load), the BSM loads
6164 * the bootstrap program into the on-board processor, and starts it.
6166 * The bootstrap program loads (via DMA) instructions and data for a new
6167 * program from host DRAM locations indicated by the host driver in the
6168 * BSM_DRAM_* registers. Once the new program is loaded, it starts
6169 * automatically.
6171 * When initializing the NIC, the host driver points the BSM to the
6172 * "initialize" uCode image. This uCode sets up some internal data, then
6173 * notifies host via "initialize alive" that it is complete.
6175 * The host then replaces the BSM_DRAM_* pointer values to point to the
6176 * normal runtime uCode instructions and a backup uCode data cache buffer
6177 * (filled initially with starting data values for the on-board processor),
6178 * then triggers the "initialize" uCode to load and launch the runtime uCode,
6179 * which begins normal operation.
6181 * When doing a power-save shutdown, runtime uCode saves data SRAM into
6182 * the backup data cache in DRAM before SRAM is powered down.
6184 * When powering back up, the BSM loads the bootstrap program. This reloads
6185 * the runtime uCode instructions and the backup data cache into SRAM,
6186 * and re-launches the runtime uCode from where it left off.
6188 static int iwl4965_load_bsm(struct iwl4965_priv *priv)
6190 __le32 *image = priv->ucode_boot.v_addr;
6191 u32 len = priv->ucode_boot.len;
6192 dma_addr_t pinst;
6193 dma_addr_t pdata;
6194 u32 inst_len;
6195 u32 data_len;
6196 int rc;
6197 int i;
6198 u32 done;
6199 u32 reg_offset;
6201 IWL_DEBUG_INFO("Begin load bsm\n");
6203 /* make sure bootstrap program is no larger than BSM's SRAM size */
6204 if (len > IWL_MAX_BSM_SIZE)
6205 return -EINVAL;
6207 /* Tell bootstrap uCode where to find the "Initialize" uCode
6208 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
6209 * NOTE: iwl4965_initialize_alive_start() will replace these values,
6210 * after the "initialize" uCode has run, to point to
6211 * runtime/protocol instructions and backup data cache. */
6212 pinst = priv->ucode_init.p_addr >> 4;
6213 pdata = priv->ucode_init_data.p_addr >> 4;
6214 inst_len = priv->ucode_init.len;
6215 data_len = priv->ucode_init_data.len;
6217 rc = iwl4965_grab_nic_access(priv);
6218 if (rc)
6219 return rc;
6221 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6222 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6223 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
6224 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
6226 /* Fill BSM memory with bootstrap instructions */
6227 for (reg_offset = BSM_SRAM_LOWER_BOUND;
6228 reg_offset < BSM_SRAM_LOWER_BOUND + len;
6229 reg_offset += sizeof(u32), image++)
6230 _iwl4965_write_prph(priv, reg_offset,
6231 le32_to_cpu(*image));
6233 rc = iwl4965_verify_bsm(priv);
6234 if (rc) {
6235 iwl4965_release_nic_access(priv);
6236 return rc;
6239 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
6240 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
6241 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
6242 RTC_INST_LOWER_BOUND);
6243 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
6245 /* Load bootstrap code into instruction SRAM now,
6246 * to prepare to load "initialize" uCode */
6247 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
6248 BSM_WR_CTRL_REG_BIT_START);
6250 /* Wait for load of bootstrap uCode to finish */
6251 for (i = 0; i < 100; i++) {
6252 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
6253 if (!(done & BSM_WR_CTRL_REG_BIT_START))
6254 break;
6255 udelay(10);
6257 if (i < 100)
6258 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
6259 else {
6260 IWL_ERROR("BSM write did not complete!\n");
6261 return -EIO;
6264 /* Enable future boot loads whenever power management unit triggers it
6265 * (e.g. when powering back up after power-save shutdown) */
6266 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
6267 BSM_WR_CTRL_REG_BIT_START_EN);
6269 iwl4965_release_nic_access(priv);
6271 return 0;
6274 static void iwl4965_nic_start(struct iwl4965_priv *priv)
6276 /* Remove all resets to allow NIC to operate */
6277 iwl4965_write32(priv, CSR_RESET, 0);
6282 * iwl4965_read_ucode - Read uCode images from disk file.
6284 * Copy into buffers for card to fetch via bus-mastering
6286 static int iwl4965_read_ucode(struct iwl4965_priv *priv)
6288 struct iwl4965_ucode *ucode;
6289 int ret;
6290 const struct firmware *ucode_raw;
6291 const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
6292 u8 *src;
6293 size_t len;
6294 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6296 /* Ask kernel firmware_class module to get the boot firmware off disk.
6297 * request_firmware() is synchronous, file is in memory on return. */
6298 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6299 if (ret < 0) {
6300 IWL_ERROR("%s firmware file req failed: Reason %d\n",
6301 name, ret);
6302 goto error;
6305 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6306 name, ucode_raw->size);
6308 /* Make sure that we got at least our header! */
6309 if (ucode_raw->size < sizeof(*ucode)) {
6310 IWL_ERROR("File size way too small!\n");
6311 ret = -EINVAL;
6312 goto err_release;
6315 /* Data from ucode file: header followed by uCode images */
6316 ucode = (void *)ucode_raw->data;
6318 ver = le32_to_cpu(ucode->ver);
6319 inst_size = le32_to_cpu(ucode->inst_size);
6320 data_size = le32_to_cpu(ucode->data_size);
6321 init_size = le32_to_cpu(ucode->init_size);
6322 init_data_size = le32_to_cpu(ucode->init_data_size);
6323 boot_size = le32_to_cpu(ucode->boot_size);
6325 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6326 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6327 inst_size);
6328 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6329 data_size);
6330 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6331 init_size);
6332 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6333 init_data_size);
6334 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6335 boot_size);
6337 /* Verify size of file vs. image size info in file's header */
6338 if (ucode_raw->size < sizeof(*ucode) +
6339 inst_size + data_size + init_size +
6340 init_data_size + boot_size) {
6342 IWL_DEBUG_INFO("uCode file size %d too small\n",
6343 (int)ucode_raw->size);
6344 ret = -EINVAL;
6345 goto err_release;
6348 /* Verify that uCode images will fit in card's SRAM */
6349 if (inst_size > IWL_MAX_INST_SIZE) {
6350 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
6351 inst_size);
6352 ret = -EINVAL;
6353 goto err_release;
6356 if (data_size > IWL_MAX_DATA_SIZE) {
6357 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
6358 data_size);
6359 ret = -EINVAL;
6360 goto err_release;
6362 if (init_size > IWL_MAX_INST_SIZE) {
6363 IWL_DEBUG_INFO
6364 ("uCode init instr len %d too large to fit in\n",
6365 init_size);
6366 ret = -EINVAL;
6367 goto err_release;
6369 if (init_data_size > IWL_MAX_DATA_SIZE) {
6370 IWL_DEBUG_INFO
6371 ("uCode init data len %d too large to fit in\n",
6372 init_data_size);
6373 ret = -EINVAL;
6374 goto err_release;
6376 if (boot_size > IWL_MAX_BSM_SIZE) {
6377 IWL_DEBUG_INFO
6378 ("uCode boot instr len %d too large to fit in\n",
6379 boot_size);
6380 ret = -EINVAL;
6381 goto err_release;
6384 /* Allocate ucode buffers for card's bus-master loading ... */
6386 /* Runtime instructions and 2 copies of data:
6387 * 1) unmodified from disk
6388 * 2) backup cache for save/restore during power-downs */
6389 priv->ucode_code.len = inst_size;
6390 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
6392 priv->ucode_data.len = data_size;
6393 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
6395 priv->ucode_data_backup.len = data_size;
6396 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
6398 /* Initialization instructions and data */
6399 if (init_size && init_data_size) {
6400 priv->ucode_init.len = init_size;
6401 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
6403 priv->ucode_init_data.len = init_data_size;
6404 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
6406 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6407 goto err_pci_alloc;
6410 /* Bootstrap (instructions only, no data) */
6411 if (boot_size) {
6412 priv->ucode_boot.len = boot_size;
6413 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
6415 if (!priv->ucode_boot.v_addr)
6416 goto err_pci_alloc;
6419 /* Copy images into buffers for card's bus-master reads ... */
6421 /* Runtime instructions (first block of data in file) */
6422 src = &ucode->data[0];
6423 len = priv->ucode_code.len;
6424 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
6425 memcpy(priv->ucode_code.v_addr, src, len);
6426 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6427 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6429 /* Runtime data (2nd block)
6430 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
6431 src = &ucode->data[inst_size];
6432 len = priv->ucode_data.len;
6433 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
6434 memcpy(priv->ucode_data.v_addr, src, len);
6435 memcpy(priv->ucode_data_backup.v_addr, src, len);
6437 /* Initialization instructions (3rd block) */
6438 if (init_size) {
6439 src = &ucode->data[inst_size + data_size];
6440 len = priv->ucode_init.len;
6441 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6442 len);
6443 memcpy(priv->ucode_init.v_addr, src, len);
6446 /* Initialization data (4th block) */
6447 if (init_data_size) {
6448 src = &ucode->data[inst_size + data_size + init_size];
6449 len = priv->ucode_init_data.len;
6450 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6451 len);
6452 memcpy(priv->ucode_init_data.v_addr, src, len);
6455 /* Bootstrap instructions (5th block) */
6456 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6457 len = priv->ucode_boot.len;
6458 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
6459 memcpy(priv->ucode_boot.v_addr, src, len);
6461 /* We have our copies now, allow OS release its copies */
6462 release_firmware(ucode_raw);
6463 return 0;
6465 err_pci_alloc:
6466 IWL_ERROR("failed to allocate pci memory\n");
6467 ret = -ENOMEM;
6468 iwl4965_dealloc_ucode_pci(priv);
6470 err_release:
6471 release_firmware(ucode_raw);
6473 error:
6474 return ret;
6479 * iwl4965_set_ucode_ptrs - Set uCode address location
6481 * Tell initialization uCode where to find runtime uCode.
6483 * BSM registers initially contain pointers to initialization uCode.
6484 * We need to replace them to load runtime uCode inst and data,
6485 * and to save runtime data when powering down.
6487 static int iwl4965_set_ucode_ptrs(struct iwl4965_priv *priv)
6489 dma_addr_t pinst;
6490 dma_addr_t pdata;
6491 int rc = 0;
6492 unsigned long flags;
6494 /* bits 35:4 for 4965 */
6495 pinst = priv->ucode_code.p_addr >> 4;
6496 pdata = priv->ucode_data_backup.p_addr >> 4;
6498 spin_lock_irqsave(&priv->lock, flags);
6499 rc = iwl4965_grab_nic_access(priv);
6500 if (rc) {
6501 spin_unlock_irqrestore(&priv->lock, flags);
6502 return rc;
6505 /* Tell bootstrap uCode where to find image to load */
6506 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6507 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6508 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
6509 priv->ucode_data.len);
6511 /* Inst bytecount must be last to set up, bit 31 signals uCode
6512 * that all new ptr/size info is in place */
6513 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
6514 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6516 iwl4965_release_nic_access(priv);
6518 spin_unlock_irqrestore(&priv->lock, flags);
6520 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6522 return rc;
6526 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
6528 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6530 * The 4965 "initialize" ALIVE reply contains calibration data for:
6531 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6532 * (3945 does not contain this data).
6534 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6536 static void iwl4965_init_alive_start(struct iwl4965_priv *priv)
6538 /* Check alive response for "valid" sign from uCode */
6539 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6540 /* We had an error bringing up the hardware, so take it
6541 * all the way back down so we can try again */
6542 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6543 goto restart;
6546 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6547 * This is a paranoid check, because we would not have gotten the
6548 * "initialize" alive if code weren't properly loaded. */
6549 if (iwl4965_verify_ucode(priv)) {
6550 /* Runtime instruction load was bad;
6551 * take it all the way back down so we can try again */
6552 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6553 goto restart;
6556 /* Calculate temperature */
6557 priv->temperature = iwl4965_get_temperature(priv);
6559 /* Send pointers to protocol/runtime uCode image ... init code will
6560 * load and launch runtime uCode, which will send us another "Alive"
6561 * notification. */
6562 IWL_DEBUG_INFO("Initialization Alive received.\n");
6563 if (iwl4965_set_ucode_ptrs(priv)) {
6564 /* Runtime instruction load won't happen;
6565 * take it all the way back down so we can try again */
6566 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6567 goto restart;
6569 return;
6571 restart:
6572 queue_work(priv->workqueue, &priv->restart);
6577 * iwl4965_alive_start - called after REPLY_ALIVE notification received
6578 * from protocol/runtime uCode (initialization uCode's
6579 * Alive gets handled by iwl4965_init_alive_start()).
6581 static void iwl4965_alive_start(struct iwl4965_priv *priv)
6583 int rc = 0;
6585 IWL_DEBUG_INFO("Runtime Alive received.\n");
6587 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6588 /* We had an error bringing up the hardware, so take it
6589 * all the way back down so we can try again */
6590 IWL_DEBUG_INFO("Alive failed.\n");
6591 goto restart;
6594 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6595 * This is a paranoid check, because we would not have gotten the
6596 * "runtime" alive if code weren't properly loaded. */
6597 if (iwl4965_verify_ucode(priv)) {
6598 /* Runtime instruction load was bad;
6599 * take it all the way back down so we can try again */
6600 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6601 goto restart;
6604 iwl4965_clear_stations_table(priv);
6606 rc = iwl4965_alive_notify(priv);
6607 if (rc) {
6608 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6609 rc);
6610 goto restart;
6613 /* After the ALIVE response, we can send host commands to 4965 uCode */
6614 set_bit(STATUS_ALIVE, &priv->status);
6616 /* Clear out the uCode error bit if it is set */
6617 clear_bit(STATUS_FW_ERROR, &priv->status);
6619 if (iwl4965_is_rfkill(priv))
6620 return;
6622 ieee80211_start_queues(priv->hw);
6624 priv->active_rate = priv->rates_mask;
6625 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6627 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
6629 if (iwl4965_is_associated(priv)) {
6630 struct iwl4965_rxon_cmd *active_rxon =
6631 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
6633 memcpy(&priv->staging_rxon, &priv->active_rxon,
6634 sizeof(priv->staging_rxon));
6635 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6636 } else {
6637 /* Initialize our rx_config data */
6638 iwl4965_connection_init_rx_config(priv);
6639 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6642 /* Configure Bluetooth device coexistence support */
6643 iwl4965_send_bt_config(priv);
6645 /* Configure the adapter for unassociated operation */
6646 iwl4965_commit_rxon(priv);
6648 /* At this point, the NIC is initialized and operational */
6649 priv->notif_missed_beacons = 0;
6650 set_bit(STATUS_READY, &priv->status);
6652 iwl4965_rf_kill_ct_config(priv);
6654 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6655 wake_up_interruptible(&priv->wait_command_queue);
6657 if (priv->error_recovering)
6658 iwl4965_error_recovery(priv);
6660 return;
6662 restart:
6663 queue_work(priv->workqueue, &priv->restart);
6666 static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv);
6668 static void __iwl4965_down(struct iwl4965_priv *priv)
6670 unsigned long flags;
6671 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6672 struct ieee80211_conf *conf = NULL;
6674 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6676 conf = ieee80211_get_hw_conf(priv->hw);
6678 if (!exit_pending)
6679 set_bit(STATUS_EXIT_PENDING, &priv->status);
6681 iwl4965_clear_stations_table(priv);
6683 /* Unblock any waiting calls */
6684 wake_up_interruptible_all(&priv->wait_command_queue);
6686 /* Wipe out the EXIT_PENDING status bit if we are not actually
6687 * exiting the module */
6688 if (!exit_pending)
6689 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6691 /* stop and reset the on-board processor */
6692 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
6694 /* tell the device to stop sending interrupts */
6695 iwl4965_disable_interrupts(priv);
6697 if (priv->mac80211_registered)
6698 ieee80211_stop_queues(priv->hw);
6700 /* If we have not previously called iwl4965_init() then
6701 * clear all bits but the RF Kill and SUSPEND bits and return */
6702 if (!iwl4965_is_init(priv)) {
6703 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6704 STATUS_RF_KILL_HW |
6705 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6706 STATUS_RF_KILL_SW |
6707 <<<<<<< HEAD:drivers/net/wireless/iwlwifi/iwl4965-base.c
6708 =======
6709 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6710 STATUS_GEO_CONFIGURED |
6711 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/iwlwifi/iwl4965-base.c
6712 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6713 STATUS_IN_SUSPEND;
6714 goto exit;
6717 /* ...otherwise clear out all the status bits but the RF Kill and
6718 * SUSPEND bits and continue taking the NIC down. */
6719 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6720 STATUS_RF_KILL_HW |
6721 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6722 STATUS_RF_KILL_SW |
6723 <<<<<<< HEAD:drivers/net/wireless/iwlwifi/iwl4965-base.c
6724 =======
6725 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6726 STATUS_GEO_CONFIGURED |
6727 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/wireless/iwlwifi/iwl4965-base.c
6728 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6729 STATUS_IN_SUSPEND |
6730 test_bit(STATUS_FW_ERROR, &priv->status) <<
6731 STATUS_FW_ERROR;
6733 spin_lock_irqsave(&priv->lock, flags);
6734 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
6735 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
6736 spin_unlock_irqrestore(&priv->lock, flags);
6738 iwl4965_hw_txq_ctx_stop(priv);
6739 iwl4965_hw_rxq_stop(priv);
6741 spin_lock_irqsave(&priv->lock, flags);
6742 if (!iwl4965_grab_nic_access(priv)) {
6743 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
6744 APMG_CLK_VAL_DMA_CLK_RQT);
6745 iwl4965_release_nic_access(priv);
6747 spin_unlock_irqrestore(&priv->lock, flags);
6749 udelay(5);
6751 iwl4965_hw_nic_stop_master(priv);
6752 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6753 iwl4965_hw_nic_reset(priv);
6755 exit:
6756 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
6758 if (priv->ibss_beacon)
6759 dev_kfree_skb(priv->ibss_beacon);
6760 priv->ibss_beacon = NULL;
6762 /* clear out any free frames */
6763 iwl4965_clear_free_frames(priv);
6766 static void iwl4965_down(struct iwl4965_priv *priv)
6768 mutex_lock(&priv->mutex);
6769 __iwl4965_down(priv);
6770 mutex_unlock(&priv->mutex);
6772 iwl4965_cancel_deferred_work(priv);
6775 #define MAX_HW_RESTARTS 5
6777 static int __iwl4965_up(struct iwl4965_priv *priv)
6779 int rc, i;
6781 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6782 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6783 return -EIO;
6786 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6787 IWL_WARNING("Radio disabled by SW RF kill (module "
6788 "parameter)\n");
6789 return -ENODEV;
6792 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6793 IWL_ERROR("ucode not available for device bringup\n");
6794 return -EIO;
6797 /* If platform's RF_KILL switch is NOT set to KILL */
6798 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
6799 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6800 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6801 else {
6802 set_bit(STATUS_RF_KILL_HW, &priv->status);
6803 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6804 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6805 return -ENODEV;
6809 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6811 rc = iwl4965_hw_nic_init(priv);
6812 if (rc) {
6813 IWL_ERROR("Unable to int nic\n");
6814 return rc;
6817 /* make sure rfkill handshake bits are cleared */
6818 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6819 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
6820 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6822 /* clear (again), then enable host interrupts */
6823 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6824 iwl4965_enable_interrupts(priv);
6826 /* really make sure rfkill handshake bits are cleared */
6827 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6828 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6830 /* Copy original ucode data image from disk into backup cache.
6831 * This will be used to initialize the on-board processor's
6832 * data SRAM for a clean start when the runtime program first loads. */
6833 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6834 priv->ucode_data.len);
6836 /* We return success when we resume from suspend and rf_kill is on. */
6837 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
6838 return 0;
6840 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6842 iwl4965_clear_stations_table(priv);
6844 /* load bootstrap state machine,
6845 * load bootstrap program into processor's memory,
6846 * prepare to load the "initialize" uCode */
6847 rc = iwl4965_load_bsm(priv);
6849 if (rc) {
6850 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6851 continue;
6854 /* start card; "initialize" will load runtime ucode */
6855 iwl4965_nic_start(priv);
6857 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6859 return 0;
6862 set_bit(STATUS_EXIT_PENDING, &priv->status);
6863 __iwl4965_down(priv);
6865 /* tried to restart and config the device for as long as our
6866 * patience could withstand */
6867 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6868 return -EIO;
6872 /*****************************************************************************
6874 * Workqueue callbacks
6876 *****************************************************************************/
6878 static void iwl4965_bg_init_alive_start(struct work_struct *data)
6880 struct iwl4965_priv *priv =
6881 container_of(data, struct iwl4965_priv, init_alive_start.work);
6883 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6884 return;
6886 mutex_lock(&priv->mutex);
6887 iwl4965_init_alive_start(priv);
6888 mutex_unlock(&priv->mutex);
6891 static void iwl4965_bg_alive_start(struct work_struct *data)
6893 struct iwl4965_priv *priv =
6894 container_of(data, struct iwl4965_priv, alive_start.work);
6896 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6897 return;
6899 mutex_lock(&priv->mutex);
6900 iwl4965_alive_start(priv);
6901 mutex_unlock(&priv->mutex);
6904 static void iwl4965_bg_rf_kill(struct work_struct *work)
6906 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, rf_kill);
6908 wake_up_interruptible(&priv->wait_command_queue);
6910 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6911 return;
6913 mutex_lock(&priv->mutex);
6915 if (!iwl4965_is_rfkill(priv)) {
6916 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6917 "HW and/or SW RF Kill no longer active, restarting "
6918 "device\n");
6919 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6920 queue_work(priv->workqueue, &priv->restart);
6921 } else {
6923 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6924 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6925 "disabled by SW switch\n");
6926 else
6927 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6928 "Kill switch must be turned off for "
6929 "wireless networking to work.\n");
6931 mutex_unlock(&priv->mutex);
6934 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6936 static void iwl4965_bg_scan_check(struct work_struct *data)
6938 struct iwl4965_priv *priv =
6939 container_of(data, struct iwl4965_priv, scan_check.work);
6941 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6942 return;
6944 mutex_lock(&priv->mutex);
6945 if (test_bit(STATUS_SCANNING, &priv->status) ||
6946 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6947 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6948 "Scan completion watchdog resetting adapter (%dms)\n",
6949 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
6951 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6952 iwl4965_send_scan_abort(priv);
6954 mutex_unlock(&priv->mutex);
6957 static void iwl4965_bg_request_scan(struct work_struct *data)
6959 struct iwl4965_priv *priv =
6960 container_of(data, struct iwl4965_priv, request_scan);
6961 struct iwl4965_host_cmd cmd = {
6962 .id = REPLY_SCAN_CMD,
6963 .len = sizeof(struct iwl4965_scan_cmd),
6964 .meta.flags = CMD_SIZE_HUGE,
6966 int rc = 0;
6967 struct iwl4965_scan_cmd *scan;
6968 struct ieee80211_conf *conf = NULL;
6969 u8 direct_mask;
6970 int phymode;
6972 conf = ieee80211_get_hw_conf(priv->hw);
6974 mutex_lock(&priv->mutex);
6976 if (!iwl4965_is_ready(priv)) {
6977 IWL_WARNING("request scan called when driver not ready.\n");
6978 goto done;
6981 /* Make sure the scan wasn't cancelled before this queued work
6982 * was given the chance to run... */
6983 if (!test_bit(STATUS_SCANNING, &priv->status))
6984 goto done;
6986 /* This should never be called or scheduled if there is currently
6987 * a scan active in the hardware. */
6988 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6989 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6990 "Ignoring second request.\n");
6991 rc = -EIO;
6992 goto done;
6995 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6996 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6997 goto done;
7000 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
7001 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
7002 goto done;
7005 if (iwl4965_is_rfkill(priv)) {
7006 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
7007 goto done;
7010 if (!test_bit(STATUS_READY, &priv->status)) {
7011 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
7012 goto done;
7015 if (!priv->scan_bands) {
7016 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
7017 goto done;
7020 if (!priv->scan) {
7021 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
7022 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
7023 if (!priv->scan) {
7024 rc = -ENOMEM;
7025 goto done;
7028 scan = priv->scan;
7029 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
7031 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
7032 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
7034 if (iwl4965_is_associated(priv)) {
7035 u16 interval = 0;
7036 u32 extra;
7037 u32 suspend_time = 100;
7038 u32 scan_suspend_time = 100;
7039 unsigned long flags;
7041 IWL_DEBUG_INFO("Scanning while associated...\n");
7043 spin_lock_irqsave(&priv->lock, flags);
7044 interval = priv->beacon_int;
7045 spin_unlock_irqrestore(&priv->lock, flags);
7047 scan->suspend_time = 0;
7048 scan->max_out_time = cpu_to_le32(200 * 1024);
7049 if (!interval)
7050 interval = suspend_time;
7052 extra = (suspend_time / interval) << 22;
7053 scan_suspend_time = (extra |
7054 ((suspend_time % interval) * 1024));
7055 scan->suspend_time = cpu_to_le32(scan_suspend_time);
7056 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
7057 scan_suspend_time, interval);
7060 /* We should add the ability for user to lock to PASSIVE ONLY */
7061 if (priv->one_direct_scan) {
7062 IWL_DEBUG_SCAN
7063 ("Kicking off one direct scan for '%s'\n",
7064 iwl4965_escape_essid(priv->direct_ssid,
7065 priv->direct_ssid_len));
7066 scan->direct_scan[0].id = WLAN_EID_SSID;
7067 scan->direct_scan[0].len = priv->direct_ssid_len;
7068 memcpy(scan->direct_scan[0].ssid,
7069 priv->direct_ssid, priv->direct_ssid_len);
7070 direct_mask = 1;
7071 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
7072 scan->direct_scan[0].id = WLAN_EID_SSID;
7073 scan->direct_scan[0].len = priv->essid_len;
7074 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
7075 direct_mask = 1;
7076 } else
7077 direct_mask = 0;
7079 /* We don't build a direct scan probe request; the uCode will do
7080 * that based on the direct_mask added to each channel entry */
7081 scan->tx_cmd.len = cpu_to_le16(
7082 iwl4965_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
7083 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0));
7084 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
7085 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
7086 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
7088 /* flags + rate selection */
7090 scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
7092 switch (priv->scan_bands) {
7093 case 2:
7094 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
7095 scan->tx_cmd.rate_n_flags =
7096 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
7097 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
7099 scan->good_CRC_th = 0;
7100 phymode = MODE_IEEE80211G;
7101 break;
7103 case 1:
7104 scan->tx_cmd.rate_n_flags =
7105 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
7106 RATE_MCS_ANT_B_MSK);
7107 scan->good_CRC_th = IWL_GOOD_CRC_TH;
7108 phymode = MODE_IEEE80211A;
7109 break;
7111 default:
7112 IWL_WARNING("Invalid scan band count\n");
7113 goto done;
7116 /* select Rx chains */
7118 /* Force use of chains B and C (0x6) for scan Rx.
7119 * Avoid A (0x1) because of its off-channel reception on A-band.
7120 * MIMO is not used here, but value is required to make uCode happy. */
7121 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
7122 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
7123 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
7124 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
7126 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
7127 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
7129 if (direct_mask)
7130 IWL_DEBUG_SCAN
7131 ("Initiating direct scan for %s.\n",
7132 iwl4965_escape_essid(priv->essid, priv->essid_len));
7133 else
7134 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
7136 scan->channel_count =
7137 iwl4965_get_channels_for_scan(
7138 priv, phymode, 1, /* active */
7139 direct_mask,
7140 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
7142 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
7143 scan->channel_count * sizeof(struct iwl4965_scan_channel);
7144 cmd.data = scan;
7145 scan->len = cpu_to_le16(cmd.len);
7147 set_bit(STATUS_SCAN_HW, &priv->status);
7148 rc = iwl4965_send_cmd_sync(priv, &cmd);
7149 if (rc)
7150 goto done;
7152 queue_delayed_work(priv->workqueue, &priv->scan_check,
7153 IWL_SCAN_CHECK_WATCHDOG);
7155 mutex_unlock(&priv->mutex);
7156 return;
7158 done:
7159 /* inform mac80211 scan aborted */
7160 queue_work(priv->workqueue, &priv->scan_completed);
7161 mutex_unlock(&priv->mutex);
7164 static void iwl4965_bg_up(struct work_struct *data)
7166 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, up);
7168 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7169 return;
7171 mutex_lock(&priv->mutex);
7172 __iwl4965_up(priv);
7173 mutex_unlock(&priv->mutex);
7176 static void iwl4965_bg_restart(struct work_struct *data)
7178 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, restart);
7180 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7181 return;
7183 iwl4965_down(priv);
7184 queue_work(priv->workqueue, &priv->up);
7187 static void iwl4965_bg_rx_replenish(struct work_struct *data)
7189 struct iwl4965_priv *priv =
7190 container_of(data, struct iwl4965_priv, rx_replenish);
7192 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7193 return;
7195 mutex_lock(&priv->mutex);
7196 iwl4965_rx_replenish(priv);
7197 mutex_unlock(&priv->mutex);
7200 #define IWL_DELAY_NEXT_SCAN (HZ*2)
7202 static void iwl4965_bg_post_associate(struct work_struct *data)
7204 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv,
7205 post_associate.work);
7207 int rc = 0;
7208 struct ieee80211_conf *conf = NULL;
7209 DECLARE_MAC_BUF(mac);
7211 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7212 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
7213 return;
7216 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
7217 priv->assoc_id,
7218 print_mac(mac, priv->active_rxon.bssid_addr));
7221 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7222 return;
7224 mutex_lock(&priv->mutex);
7226 if (!priv->vif || !priv->is_open) {
7227 mutex_unlock(&priv->mutex);
7228 return;
7230 iwl4965_scan_cancel_timeout(priv, 200);
7232 conf = ieee80211_get_hw_conf(priv->hw);
7234 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7235 iwl4965_commit_rxon(priv);
7237 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7238 iwl4965_setup_rxon_timing(priv);
7239 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7240 sizeof(priv->rxon_timing), &priv->rxon_timing);
7241 if (rc)
7242 IWL_WARNING("REPLY_RXON_TIMING failed - "
7243 "Attempting to continue.\n");
7245 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7247 #ifdef CONFIG_IWL4965_HT
7248 if (priv->current_ht_config.is_ht)
7249 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
7250 #endif /* CONFIG_IWL4965_HT*/
7251 iwl4965_set_rxon_chain(priv);
7252 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7254 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
7255 priv->assoc_id, priv->beacon_int);
7257 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7258 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7259 else
7260 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7262 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7263 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
7264 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
7265 else
7266 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7268 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7269 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7273 iwl4965_commit_rxon(priv);
7275 switch (priv->iw_mode) {
7276 case IEEE80211_IF_TYPE_STA:
7277 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
7278 break;
7280 case IEEE80211_IF_TYPE_IBSS:
7282 /* clear out the station table */
7283 iwl4965_clear_stations_table(priv);
7285 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
7286 iwl4965_rxon_add_station(priv, priv->bssid, 0);
7287 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
7288 iwl4965_send_beacon_cmd(priv);
7290 break;
7292 default:
7293 IWL_ERROR("%s Should not be called in %d mode\n",
7294 __FUNCTION__, priv->iw_mode);
7295 break;
7298 iwl4965_sequence_reset(priv);
7300 #ifdef CONFIG_IWL4965_SENSITIVITY
7301 /* Enable Rx differential gain and sensitivity calibrations */
7302 iwl4965_chain_noise_reset(priv);
7303 priv->start_calib = 1;
7304 #endif /* CONFIG_IWL4965_SENSITIVITY */
7306 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7307 priv->assoc_station_added = 1;
7309 #ifdef CONFIG_IWL4965_QOS
7310 iwl4965_activate_qos(priv, 0);
7311 #endif /* CONFIG_IWL4965_QOS */
7312 /* we have just associated, don't start scan too early */
7313 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
7314 mutex_unlock(&priv->mutex);
7317 static void iwl4965_bg_abort_scan(struct work_struct *work)
7319 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, abort_scan);
7321 if (!iwl4965_is_ready(priv))
7322 return;
7324 mutex_lock(&priv->mutex);
7326 set_bit(STATUS_SCAN_ABORTING, &priv->status);
7327 iwl4965_send_scan_abort(priv);
7329 mutex_unlock(&priv->mutex);
7332 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
7334 static void iwl4965_bg_scan_completed(struct work_struct *work)
7336 struct iwl4965_priv *priv =
7337 container_of(work, struct iwl4965_priv, scan_completed);
7339 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7341 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7342 return;
7344 if (test_bit(STATUS_CONF_PENDING, &priv->status))
7345 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
7347 ieee80211_scan_completed(priv->hw);
7349 /* Since setting the TXPOWER may have been deferred while
7350 * performing the scan, fire one off */
7351 mutex_lock(&priv->mutex);
7352 iwl4965_hw_reg_send_txpower(priv);
7353 mutex_unlock(&priv->mutex);
7356 /*****************************************************************************
7358 * mac80211 entry point functions
7360 *****************************************************************************/
7362 #define UCODE_READY_TIMEOUT (2 * HZ)
7364 static int iwl4965_mac_start(struct ieee80211_hw *hw)
7366 struct iwl4965_priv *priv = hw->priv;
7367 int ret;
7369 IWL_DEBUG_MAC80211("enter\n");
7371 if (pci_enable_device(priv->pci_dev)) {
7372 IWL_ERROR("Fail to pci_enable_device\n");
7373 return -ENODEV;
7375 pci_restore_state(priv->pci_dev);
7376 pci_enable_msi(priv->pci_dev);
7378 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
7379 DRV_NAME, priv);
7380 if (ret) {
7381 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
7382 goto out_disable_msi;
7385 /* we should be verifying the device is ready to be opened */
7386 mutex_lock(&priv->mutex);
7388 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
7389 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
7390 * ucode filename and max sizes are card-specific. */
7392 if (!priv->ucode_code.len) {
7393 ret = iwl4965_read_ucode(priv);
7394 if (ret) {
7395 IWL_ERROR("Could not read microcode: %d\n", ret);
7396 mutex_unlock(&priv->mutex);
7397 goto out_release_irq;
7401 ret = __iwl4965_up(priv);
7403 mutex_unlock(&priv->mutex);
7405 if (ret)
7406 goto out_release_irq;
7408 IWL_DEBUG_INFO("Start UP work done.\n");
7410 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
7411 return 0;
7413 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
7414 * mac80211 will not be run successfully. */
7415 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
7416 test_bit(STATUS_READY, &priv->status),
7417 UCODE_READY_TIMEOUT);
7418 if (!ret) {
7419 if (!test_bit(STATUS_READY, &priv->status)) {
7420 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
7421 jiffies_to_msecs(UCODE_READY_TIMEOUT));
7422 ret = -ETIMEDOUT;
7423 goto out_release_irq;
7427 priv->is_open = 1;
7428 IWL_DEBUG_MAC80211("leave\n");
7429 return 0;
7431 out_release_irq:
7432 free_irq(priv->pci_dev->irq, priv);
7433 out_disable_msi:
7434 pci_disable_msi(priv->pci_dev);
7435 pci_disable_device(priv->pci_dev);
7436 priv->is_open = 0;
7437 IWL_DEBUG_MAC80211("leave - failed\n");
7438 return ret;
7441 static void iwl4965_mac_stop(struct ieee80211_hw *hw)
7443 struct iwl4965_priv *priv = hw->priv;
7445 IWL_DEBUG_MAC80211("enter\n");
7447 if (!priv->is_open) {
7448 IWL_DEBUG_MAC80211("leave - skip\n");
7449 return;
7452 priv->is_open = 0;
7454 if (iwl4965_is_ready_rf(priv)) {
7455 /* stop mac, cancel any scan request and clear
7456 * RXON_FILTER_ASSOC_MSK BIT
7458 mutex_lock(&priv->mutex);
7459 iwl4965_scan_cancel_timeout(priv, 100);
7460 cancel_delayed_work(&priv->post_associate);
7461 mutex_unlock(&priv->mutex);
7464 iwl4965_down(priv);
7466 flush_workqueue(priv->workqueue);
7467 free_irq(priv->pci_dev->irq, priv);
7468 pci_disable_msi(priv->pci_dev);
7469 pci_save_state(priv->pci_dev);
7470 pci_disable_device(priv->pci_dev);
7472 IWL_DEBUG_MAC80211("leave\n");
7475 static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
7476 struct ieee80211_tx_control *ctl)
7478 struct iwl4965_priv *priv = hw->priv;
7480 IWL_DEBUG_MAC80211("enter\n");
7482 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7483 IWL_DEBUG_MAC80211("leave - monitor\n");
7484 return -1;
7487 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7488 ctl->tx_rate);
7490 if (iwl4965_tx_skb(priv, skb, ctl))
7491 dev_kfree_skb_any(skb);
7493 IWL_DEBUG_MAC80211("leave\n");
7494 return 0;
7497 static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
7498 struct ieee80211_if_init_conf *conf)
7500 struct iwl4965_priv *priv = hw->priv;
7501 unsigned long flags;
7502 DECLARE_MAC_BUF(mac);
7504 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
7506 if (priv->vif) {
7507 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
7508 return -EOPNOTSUPP;
7511 spin_lock_irqsave(&priv->lock, flags);
7512 priv->vif = conf->vif;
7514 spin_unlock_irqrestore(&priv->lock, flags);
7516 mutex_lock(&priv->mutex);
7518 if (conf->mac_addr) {
7519 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7520 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7523 if (iwl4965_is_ready(priv))
7524 iwl4965_set_mode(priv, conf->type);
7526 mutex_unlock(&priv->mutex);
7528 IWL_DEBUG_MAC80211("leave\n");
7529 return 0;
7533 * iwl4965_mac_config - mac80211 config callback
7535 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7536 * be set inappropriately and the driver currently sets the hardware up to
7537 * use it whenever needed.
7539 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
7541 struct iwl4965_priv *priv = hw->priv;
7542 const struct iwl4965_channel_info *ch_info;
7543 unsigned long flags;
7544 int ret = 0;
7546 mutex_lock(&priv->mutex);
7547 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7549 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
7551 if (!iwl4965_is_ready(priv)) {
7552 IWL_DEBUG_MAC80211("leave - not ready\n");
7553 ret = -EIO;
7554 goto out;
7557 if (unlikely(!iwl4965_param_disable_hw_scan &&
7558 test_bit(STATUS_SCANNING, &priv->status))) {
7559 IWL_DEBUG_MAC80211("leave - scanning\n");
7560 set_bit(STATUS_CONF_PENDING, &priv->status);
7561 mutex_unlock(&priv->mutex);
7562 return 0;
7565 spin_lock_irqsave(&priv->lock, flags);
7567 ch_info = iwl4965_get_channel_info(priv, conf->phymode, conf->channel);
7568 if (!is_channel_valid(ch_info)) {
7569 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7570 conf->channel, conf->phymode);
7571 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7572 spin_unlock_irqrestore(&priv->lock, flags);
7573 ret = -EINVAL;
7574 goto out;
7577 #ifdef CONFIG_IWL4965_HT
7578 /* if we are switching fron ht to 2.4 clear flags
7579 * from any ht related info since 2.4 does not
7580 * support ht */
7581 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
7582 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7583 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7584 #endif
7586 priv->staging_rxon.flags = 0;
7587 #endif /* CONFIG_IWL4965_HT */
7589 iwl4965_set_rxon_channel(priv, conf->phymode, conf->channel);
7591 iwl4965_set_flags_for_phymode(priv, conf->phymode);
7593 /* The list of supported rates and rate mask can be different
7594 * for each phymode; since the phymode may have changed, reset
7595 * the rate mask to what mac80211 lists */
7596 iwl4965_set_rate(priv);
7598 spin_unlock_irqrestore(&priv->lock, flags);
7600 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7601 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
7602 iwl4965_hw_channel_switch(priv, conf->channel);
7603 goto out;
7605 #endif
7607 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
7609 if (!conf->radio_enabled) {
7610 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7611 goto out;
7614 if (iwl4965_is_rfkill(priv)) {
7615 IWL_DEBUG_MAC80211("leave - RF kill\n");
7616 ret = -EIO;
7617 goto out;
7620 iwl4965_set_rate(priv);
7622 if (memcmp(&priv->active_rxon,
7623 &priv->staging_rxon, sizeof(priv->staging_rxon)))
7624 iwl4965_commit_rxon(priv);
7625 else
7626 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7628 IWL_DEBUG_MAC80211("leave\n");
7630 out:
7631 clear_bit(STATUS_CONF_PENDING, &priv->status);
7632 mutex_unlock(&priv->mutex);
7633 return ret;
7636 static void iwl4965_config_ap(struct iwl4965_priv *priv)
7638 int rc = 0;
7640 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7641 return;
7643 /* The following should be done only at AP bring up */
7644 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7646 /* RXON - unassoc (to set timing command) */
7647 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7648 iwl4965_commit_rxon(priv);
7650 /* RXON Timing */
7651 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7652 iwl4965_setup_rxon_timing(priv);
7653 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7654 sizeof(priv->rxon_timing), &priv->rxon_timing);
7655 if (rc)
7656 IWL_WARNING("REPLY_RXON_TIMING failed - "
7657 "Attempting to continue.\n");
7659 iwl4965_set_rxon_chain(priv);
7661 /* FIXME: what should be the assoc_id for AP? */
7662 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7663 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7664 priv->staging_rxon.flags |=
7665 RXON_FLG_SHORT_PREAMBLE_MSK;
7666 else
7667 priv->staging_rxon.flags &=
7668 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7670 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7671 if (priv->assoc_capability &
7672 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7673 priv->staging_rxon.flags |=
7674 RXON_FLG_SHORT_SLOT_MSK;
7675 else
7676 priv->staging_rxon.flags &=
7677 ~RXON_FLG_SHORT_SLOT_MSK;
7679 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7680 priv->staging_rxon.flags &=
7681 ~RXON_FLG_SHORT_SLOT_MSK;
7683 /* restore RXON assoc */
7684 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7685 iwl4965_commit_rxon(priv);
7686 #ifdef CONFIG_IWL4965_QOS
7687 iwl4965_activate_qos(priv, 1);
7688 #endif
7689 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
7691 iwl4965_send_beacon_cmd(priv);
7693 /* FIXME - we need to add code here to detect a totally new
7694 * configuration, reset the AP, unassoc, rxon timing, assoc,
7695 * clear sta table, add BCAST sta... */
7698 static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
7699 struct ieee80211_vif *vif,
7700 struct ieee80211_if_conf *conf)
7702 struct iwl4965_priv *priv = hw->priv;
7703 DECLARE_MAC_BUF(mac);
7704 unsigned long flags;
7705 int rc;
7707 if (conf == NULL)
7708 return -EIO;
7710 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7711 (!conf->beacon || !conf->ssid_len)) {
7712 IWL_DEBUG_MAC80211
7713 ("Leaving in AP mode because HostAPD is not ready.\n");
7714 return 0;
7717 if (!iwl4965_is_alive(priv))
7718 return -EAGAIN;
7720 mutex_lock(&priv->mutex);
7722 if (conf->bssid)
7723 IWL_DEBUG_MAC80211("bssid: %s\n",
7724 print_mac(mac, conf->bssid));
7727 * very dubious code was here; the probe filtering flag is never set:
7729 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7730 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
7732 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
7733 IWL_DEBUG_MAC80211("leave - scanning\n");
7734 mutex_unlock(&priv->mutex);
7735 return 0;
7738 if (priv->vif != vif) {
7739 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
7740 mutex_unlock(&priv->mutex);
7741 return 0;
7744 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7745 if (!conf->bssid) {
7746 conf->bssid = priv->mac_addr;
7747 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
7748 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7749 print_mac(mac, conf->bssid));
7751 if (priv->ibss_beacon)
7752 dev_kfree_skb(priv->ibss_beacon);
7754 priv->ibss_beacon = conf->beacon;
7757 if (iwl4965_is_rfkill(priv))
7758 goto done;
7760 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7761 !is_multicast_ether_addr(conf->bssid)) {
7762 /* If there is currently a HW scan going on in the background
7763 * then we need to cancel it else the RXON below will fail. */
7764 if (iwl4965_scan_cancel_timeout(priv, 100)) {
7765 IWL_WARNING("Aborted scan still in progress "
7766 "after 100ms\n");
7767 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7768 mutex_unlock(&priv->mutex);
7769 return -EAGAIN;
7771 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7773 /* TODO: Audit driver for usage of these members and see
7774 * if mac80211 deprecates them (priv->bssid looks like it
7775 * shouldn't be there, but I haven't scanned the IBSS code
7776 * to verify) - jpk */
7777 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7779 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7780 iwl4965_config_ap(priv);
7781 else {
7782 rc = iwl4965_commit_rxon(priv);
7783 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
7784 iwl4965_rxon_add_station(
7785 priv, priv->active_rxon.bssid_addr, 1);
7788 } else {
7789 iwl4965_scan_cancel_timeout(priv, 100);
7790 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7791 iwl4965_commit_rxon(priv);
7794 done:
7795 spin_lock_irqsave(&priv->lock, flags);
7796 if (!conf->ssid_len)
7797 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7798 else
7799 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7801 priv->essid_len = conf->ssid_len;
7802 spin_unlock_irqrestore(&priv->lock, flags);
7804 IWL_DEBUG_MAC80211("leave\n");
7805 mutex_unlock(&priv->mutex);
7807 return 0;
7810 static void iwl4965_configure_filter(struct ieee80211_hw *hw,
7811 unsigned int changed_flags,
7812 unsigned int *total_flags,
7813 int mc_count, struct dev_addr_list *mc_list)
7816 * XXX: dummy
7817 * see also iwl4965_connection_init_rx_config
7819 *total_flags = 0;
7822 static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
7823 struct ieee80211_if_init_conf *conf)
7825 struct iwl4965_priv *priv = hw->priv;
7827 IWL_DEBUG_MAC80211("enter\n");
7829 mutex_lock(&priv->mutex);
7831 if (iwl4965_is_ready_rf(priv)) {
7832 iwl4965_scan_cancel_timeout(priv, 100);
7833 cancel_delayed_work(&priv->post_associate);
7834 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7835 iwl4965_commit_rxon(priv);
7837 if (priv->vif == conf->vif) {
7838 priv->vif = NULL;
7839 memset(priv->bssid, 0, ETH_ALEN);
7840 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7841 priv->essid_len = 0;
7843 mutex_unlock(&priv->mutex);
7845 IWL_DEBUG_MAC80211("leave\n");
7849 static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
7850 struct ieee80211_vif *vif,
7851 struct ieee80211_bss_conf *bss_conf,
7852 u32 changes)
7854 struct iwl4965_priv *priv = hw->priv;
7856 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
7857 if (bss_conf->use_short_preamble)
7858 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7859 else
7860 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7863 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
7864 if (bss_conf->use_cts_prot && (priv->phymode != MODE_IEEE80211A))
7865 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7866 else
7867 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7870 if (changes & BSS_CHANGED_ASSOC) {
7872 * TODO:
7873 * do stuff instead of sniffing assoc resp
7877 if (iwl4965_is_associated(priv))
7878 iwl4965_send_rxon_assoc(priv);
7881 static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
7883 int rc = 0;
7884 unsigned long flags;
7885 struct iwl4965_priv *priv = hw->priv;
7887 IWL_DEBUG_MAC80211("enter\n");
7889 mutex_lock(&priv->mutex);
7890 spin_lock_irqsave(&priv->lock, flags);
7892 if (!iwl4965_is_ready_rf(priv)) {
7893 rc = -EIO;
7894 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7895 goto out_unlock;
7898 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7899 rc = -EIO;
7900 IWL_ERROR("ERROR: APs don't scan\n");
7901 goto out_unlock;
7904 /* we don't schedule scan within next_scan_jiffies period */
7905 if (priv->next_scan_jiffies &&
7906 time_after(priv->next_scan_jiffies, jiffies)) {
7907 rc = -EAGAIN;
7908 goto out_unlock;
7910 /* if we just finished scan ask for delay */
7911 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7912 IWL_DELAY_NEXT_SCAN, jiffies)) {
7913 rc = -EAGAIN;
7914 goto out_unlock;
7916 if (len) {
7917 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
7918 iwl4965_escape_essid(ssid, len), (int)len);
7920 priv->one_direct_scan = 1;
7921 priv->direct_ssid_len = (u8)
7922 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7923 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
7924 } else
7925 priv->one_direct_scan = 0;
7927 rc = iwl4965_scan_initiate(priv);
7929 IWL_DEBUG_MAC80211("leave\n");
7931 out_unlock:
7932 spin_unlock_irqrestore(&priv->lock, flags);
7933 mutex_unlock(&priv->mutex);
7935 return rc;
7938 static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
7939 const u8 *local_addr, const u8 *addr,
7940 struct ieee80211_key_conf *key)
7942 struct iwl4965_priv *priv = hw->priv;
7943 DECLARE_MAC_BUF(mac);
7944 int rc = 0;
7945 u8 sta_id;
7947 IWL_DEBUG_MAC80211("enter\n");
7949 if (!iwl4965_param_hwcrypto) {
7950 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7951 return -EOPNOTSUPP;
7954 if (is_zero_ether_addr(addr))
7955 /* only support pairwise keys */
7956 return -EOPNOTSUPP;
7958 sta_id = iwl4965_hw_find_station(priv, addr);
7959 if (sta_id == IWL_INVALID_STATION) {
7960 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7961 print_mac(mac, addr));
7962 return -EINVAL;
7965 mutex_lock(&priv->mutex);
7967 iwl4965_scan_cancel_timeout(priv, 100);
7969 switch (cmd) {
7970 case SET_KEY:
7971 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
7972 if (!rc) {
7973 iwl4965_set_rxon_hwcrypto(priv, 1);
7974 iwl4965_commit_rxon(priv);
7975 key->hw_key_idx = sta_id;
7976 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7977 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7979 break;
7980 case DISABLE_KEY:
7981 rc = iwl4965_clear_sta_key_info(priv, sta_id);
7982 if (!rc) {
7983 iwl4965_set_rxon_hwcrypto(priv, 0);
7984 iwl4965_commit_rxon(priv);
7985 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7987 break;
7988 default:
7989 rc = -EINVAL;
7992 IWL_DEBUG_MAC80211("leave\n");
7993 mutex_unlock(&priv->mutex);
7995 return rc;
7998 static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
7999 const struct ieee80211_tx_queue_params *params)
8001 struct iwl4965_priv *priv = hw->priv;
8002 #ifdef CONFIG_IWL4965_QOS
8003 unsigned long flags;
8004 int q;
8005 #endif /* CONFIG_IWL4965_QOS */
8007 IWL_DEBUG_MAC80211("enter\n");
8009 if (!iwl4965_is_ready_rf(priv)) {
8010 IWL_DEBUG_MAC80211("leave - RF not ready\n");
8011 return -EIO;
8014 if (queue >= AC_NUM) {
8015 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
8016 return 0;
8019 #ifdef CONFIG_IWL4965_QOS
8020 if (!priv->qos_data.qos_enable) {
8021 priv->qos_data.qos_active = 0;
8022 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
8023 return 0;
8025 q = AC_NUM - 1 - queue;
8027 spin_lock_irqsave(&priv->lock, flags);
8029 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
8030 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
8031 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
8032 priv->qos_data.def_qos_parm.ac[q].edca_txop =
8033 cpu_to_le16((params->burst_time * 100));
8035 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
8036 priv->qos_data.qos_active = 1;
8038 spin_unlock_irqrestore(&priv->lock, flags);
8040 mutex_lock(&priv->mutex);
8041 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
8042 iwl4965_activate_qos(priv, 1);
8043 else if (priv->assoc_id && iwl4965_is_associated(priv))
8044 iwl4965_activate_qos(priv, 0);
8046 mutex_unlock(&priv->mutex);
8048 #endif /*CONFIG_IWL4965_QOS */
8050 IWL_DEBUG_MAC80211("leave\n");
8051 return 0;
8054 static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
8055 struct ieee80211_tx_queue_stats *stats)
8057 struct iwl4965_priv *priv = hw->priv;
8058 int i, avail;
8059 struct iwl4965_tx_queue *txq;
8060 struct iwl4965_queue *q;
8061 unsigned long flags;
8063 IWL_DEBUG_MAC80211("enter\n");
8065 if (!iwl4965_is_ready_rf(priv)) {
8066 IWL_DEBUG_MAC80211("leave - RF not ready\n");
8067 return -EIO;
8070 spin_lock_irqsave(&priv->lock, flags);
8072 for (i = 0; i < AC_NUM; i++) {
8073 txq = &priv->txq[i];
8074 q = &txq->q;
8075 avail = iwl4965_queue_space(q);
8077 stats->data[i].len = q->n_window - avail;
8078 stats->data[i].limit = q->n_window - q->high_mark;
8079 stats->data[i].count = q->n_window;
8082 spin_unlock_irqrestore(&priv->lock, flags);
8084 IWL_DEBUG_MAC80211("leave\n");
8086 return 0;
8089 static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
8090 struct ieee80211_low_level_stats *stats)
8092 IWL_DEBUG_MAC80211("enter\n");
8093 IWL_DEBUG_MAC80211("leave\n");
8095 return 0;
8098 static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
8100 IWL_DEBUG_MAC80211("enter\n");
8101 IWL_DEBUG_MAC80211("leave\n");
8103 return 0;
8106 static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
8108 struct iwl4965_priv *priv = hw->priv;
8109 unsigned long flags;
8111 mutex_lock(&priv->mutex);
8112 IWL_DEBUG_MAC80211("enter\n");
8114 priv->lq_mngr.lq_ready = 0;
8115 #ifdef CONFIG_IWL4965_HT
8116 spin_lock_irqsave(&priv->lock, flags);
8117 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
8118 spin_unlock_irqrestore(&priv->lock, flags);
8119 #ifdef CONFIG_IWL4965_HT_AGG
8120 /* if (priv->lq_mngr.agg_ctrl.granted_ba)
8121 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
8123 memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl4965_agg_control));
8124 priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
8125 priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
8126 priv->lq_mngr.agg_ctrl.auto_agg = 1;
8128 if (priv->lq_mngr.agg_ctrl.auto_agg)
8129 priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
8130 #endif /*CONFIG_IWL4965_HT_AGG */
8131 #endif /* CONFIG_IWL4965_HT */
8133 #ifdef CONFIG_IWL4965_QOS
8134 iwl4965_reset_qos(priv);
8135 #endif
8137 cancel_delayed_work(&priv->post_associate);
8139 spin_lock_irqsave(&priv->lock, flags);
8140 priv->assoc_id = 0;
8141 priv->assoc_capability = 0;
8142 priv->call_post_assoc_from_beacon = 0;
8143 priv->assoc_station_added = 0;
8145 /* new association get rid of ibss beacon skb */
8146 if (priv->ibss_beacon)
8147 dev_kfree_skb(priv->ibss_beacon);
8149 priv->ibss_beacon = NULL;
8151 priv->beacon_int = priv->hw->conf.beacon_int;
8152 priv->timestamp1 = 0;
8153 priv->timestamp0 = 0;
8154 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
8155 priv->beacon_int = 0;
8157 spin_unlock_irqrestore(&priv->lock, flags);
8159 if (!iwl4965_is_ready_rf(priv)) {
8160 IWL_DEBUG_MAC80211("leave - not ready\n");
8161 mutex_unlock(&priv->mutex);
8162 return;
8165 /* we are restarting association process
8166 * clear RXON_FILTER_ASSOC_MSK bit
8168 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
8169 iwl4965_scan_cancel_timeout(priv, 100);
8170 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
8171 iwl4965_commit_rxon(priv);
8174 /* Per mac80211.h: This is only used in IBSS mode... */
8175 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
8177 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
8178 mutex_unlock(&priv->mutex);
8179 return;
8182 priv->only_active_channel = 0;
8184 iwl4965_set_rate(priv);
8186 mutex_unlock(&priv->mutex);
8188 IWL_DEBUG_MAC80211("leave\n");
8191 static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
8192 struct ieee80211_tx_control *control)
8194 struct iwl4965_priv *priv = hw->priv;
8195 unsigned long flags;
8197 mutex_lock(&priv->mutex);
8198 IWL_DEBUG_MAC80211("enter\n");
8200 if (!iwl4965_is_ready_rf(priv)) {
8201 IWL_DEBUG_MAC80211("leave - RF not ready\n");
8202 mutex_unlock(&priv->mutex);
8203 return -EIO;
8206 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
8207 IWL_DEBUG_MAC80211("leave - not IBSS\n");
8208 mutex_unlock(&priv->mutex);
8209 return -EIO;
8212 spin_lock_irqsave(&priv->lock, flags);
8214 if (priv->ibss_beacon)
8215 dev_kfree_skb(priv->ibss_beacon);
8217 priv->ibss_beacon = skb;
8219 priv->assoc_id = 0;
8221 IWL_DEBUG_MAC80211("leave\n");
8222 spin_unlock_irqrestore(&priv->lock, flags);
8224 #ifdef CONFIG_IWL4965_QOS
8225 iwl4965_reset_qos(priv);
8226 #endif
8228 queue_work(priv->workqueue, &priv->post_associate.work);
8230 mutex_unlock(&priv->mutex);
8232 return 0;
8235 #ifdef CONFIG_IWL4965_HT
8237 static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
8238 struct iwl4965_priv *priv)
8240 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
8241 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
8242 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
8244 IWL_DEBUG_MAC80211("enter: \n");
8246 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
8247 iwl_conf->is_ht = 0;
8248 return;
8251 iwl_conf->is_ht = 1;
8252 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
8254 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
8255 iwl_conf->sgf |= 0x1;
8256 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
8257 iwl_conf->sgf |= 0x2;
8259 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
8260 iwl_conf->max_amsdu_size =
8261 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
8262 iwl_conf->supported_chan_width =
8263 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
8264 iwl_conf->tx_mimo_ps_mode =
8265 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
8266 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
8268 iwl_conf->control_channel = ht_bss_conf->primary_channel;
8269 iwl_conf->extension_chan_offset =
8270 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
8271 iwl_conf->tx_chan_width =
8272 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
8273 iwl_conf->ht_protection =
8274 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
8275 iwl_conf->non_GF_STA_present =
8276 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
8278 IWL_DEBUG_MAC80211("control channel %d\n",
8279 iwl_conf->control_channel);
8280 IWL_DEBUG_MAC80211("leave\n");
8283 static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
8284 struct ieee80211_conf *conf)
8286 struct iwl4965_priv *priv = hw->priv;
8288 IWL_DEBUG_MAC80211("enter: \n");
8290 iwl4965_ht_info_fill(conf, priv);
8291 iwl4965_set_rxon_chain(priv);
8293 if (priv && priv->assoc_id &&
8294 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
8295 unsigned long flags;
8297 spin_lock_irqsave(&priv->lock, flags);
8298 if (priv->beacon_int)
8299 queue_work(priv->workqueue, &priv->post_associate.work);
8300 else
8301 priv->call_post_assoc_from_beacon = 1;
8302 spin_unlock_irqrestore(&priv->lock, flags);
8305 IWL_DEBUG_MAC80211("leave:\n");
8306 return 0;
8309 static void iwl4965_set_ht_capab(struct ieee80211_hw *hw,
8310 struct ieee80211_ht_cap *ht_cap,
8311 u8 use_current_config)
8313 struct ieee80211_conf *conf = &hw->conf;
8314 struct ieee80211_hw_mode *mode = conf->mode;
8316 if (use_current_config) {
8317 ht_cap->cap_info = cpu_to_le16(conf->ht_conf.cap);
8318 memcpy(ht_cap->supp_mcs_set,
8319 conf->ht_conf.supp_mcs_set, 16);
8320 } else {
8321 ht_cap->cap_info = cpu_to_le16(mode->ht_info.cap);
8322 memcpy(ht_cap->supp_mcs_set,
8323 mode->ht_info.supp_mcs_set, 16);
8325 ht_cap->ampdu_params_info =
8326 (mode->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
8327 ((mode->ht_info.ampdu_density << 2) &
8328 IEEE80211_HT_CAP_AMPDU_DENSITY);
8331 #endif /*CONFIG_IWL4965_HT*/
8333 /*****************************************************************************
8335 * sysfs attributes
8337 *****************************************************************************/
8339 #ifdef CONFIG_IWL4965_DEBUG
8342 * The following adds a new attribute to the sysfs representation
8343 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8344 * used for controlling the debug level.
8346 * See the level definitions in iwl for details.
8349 static ssize_t show_debug_level(struct device_driver *d, char *buf)
8351 return sprintf(buf, "0x%08X\n", iwl4965_debug_level);
8353 static ssize_t store_debug_level(struct device_driver *d,
8354 const char *buf, size_t count)
8356 char *p = (char *)buf;
8357 u32 val;
8359 val = simple_strtoul(p, &p, 0);
8360 if (p == buf)
8361 printk(KERN_INFO DRV_NAME
8362 ": %s is not in hex or decimal form.\n", buf);
8363 else
8364 iwl4965_debug_level = val;
8366 return strnlen(buf, count);
8369 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8370 show_debug_level, store_debug_level);
8372 #endif /* CONFIG_IWL4965_DEBUG */
8374 static ssize_t show_rf_kill(struct device *d,
8375 struct device_attribute *attr, char *buf)
8378 * 0 - RF kill not enabled
8379 * 1 - SW based RF kill active (sysfs)
8380 * 2 - HW based RF kill active
8381 * 3 - Both HW and SW based RF kill active
8383 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8384 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8385 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8387 return sprintf(buf, "%i\n", val);
8390 static ssize_t store_rf_kill(struct device *d,
8391 struct device_attribute *attr,
8392 const char *buf, size_t count)
8394 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8396 mutex_lock(&priv->mutex);
8397 iwl4965_radio_kill_sw(priv, buf[0] == '1');
8398 mutex_unlock(&priv->mutex);
8400 return count;
8403 static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8405 static ssize_t show_temperature(struct device *d,
8406 struct device_attribute *attr, char *buf)
8408 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8410 if (!iwl4965_is_alive(priv))
8411 return -EAGAIN;
8413 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
8416 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8418 static ssize_t show_rs_window(struct device *d,
8419 struct device_attribute *attr,
8420 char *buf)
8422 struct iwl4965_priv *priv = d->driver_data;
8423 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
8425 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8427 static ssize_t show_tx_power(struct device *d,
8428 struct device_attribute *attr, char *buf)
8430 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8431 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8434 static ssize_t store_tx_power(struct device *d,
8435 struct device_attribute *attr,
8436 const char *buf, size_t count)
8438 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8439 char *p = (char *)buf;
8440 u32 val;
8442 val = simple_strtoul(p, &p, 10);
8443 if (p == buf)
8444 printk(KERN_INFO DRV_NAME
8445 ": %s is not in decimal form.\n", buf);
8446 else
8447 iwl4965_hw_reg_set_txpower(priv, val);
8449 return count;
8452 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8454 static ssize_t show_flags(struct device *d,
8455 struct device_attribute *attr, char *buf)
8457 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8459 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8462 static ssize_t store_flags(struct device *d,
8463 struct device_attribute *attr,
8464 const char *buf, size_t count)
8466 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8467 u32 flags = simple_strtoul(buf, NULL, 0);
8469 mutex_lock(&priv->mutex);
8470 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8471 /* Cancel any currently running scans... */
8472 if (iwl4965_scan_cancel_timeout(priv, 100))
8473 IWL_WARNING("Could not cancel scan.\n");
8474 else {
8475 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8476 flags);
8477 priv->staging_rxon.flags = cpu_to_le32(flags);
8478 iwl4965_commit_rxon(priv);
8481 mutex_unlock(&priv->mutex);
8483 return count;
8486 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8488 static ssize_t show_filter_flags(struct device *d,
8489 struct device_attribute *attr, char *buf)
8491 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8493 return sprintf(buf, "0x%04X\n",
8494 le32_to_cpu(priv->active_rxon.filter_flags));
8497 static ssize_t store_filter_flags(struct device *d,
8498 struct device_attribute *attr,
8499 const char *buf, size_t count)
8501 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8502 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8504 mutex_lock(&priv->mutex);
8505 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8506 /* Cancel any currently running scans... */
8507 if (iwl4965_scan_cancel_timeout(priv, 100))
8508 IWL_WARNING("Could not cancel scan.\n");
8509 else {
8510 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8511 "0x%04X\n", filter_flags);
8512 priv->staging_rxon.filter_flags =
8513 cpu_to_le32(filter_flags);
8514 iwl4965_commit_rxon(priv);
8517 mutex_unlock(&priv->mutex);
8519 return count;
8522 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8523 store_filter_flags);
8525 static ssize_t show_tune(struct device *d,
8526 struct device_attribute *attr, char *buf)
8528 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8530 return sprintf(buf, "0x%04X\n",
8531 (priv->phymode << 8) |
8532 le16_to_cpu(priv->active_rxon.channel));
8535 static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode);
8537 static ssize_t store_tune(struct device *d,
8538 struct device_attribute *attr,
8539 const char *buf, size_t count)
8541 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8542 char *p = (char *)buf;
8543 u16 tune = simple_strtoul(p, &p, 0);
8544 u8 phymode = (tune >> 8) & 0xff;
8545 u16 channel = tune & 0xff;
8547 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
8549 mutex_lock(&priv->mutex);
8550 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
8551 (priv->phymode != phymode)) {
8552 const struct iwl4965_channel_info *ch_info;
8554 ch_info = iwl4965_get_channel_info(priv, phymode, channel);
8555 if (!ch_info) {
8556 IWL_WARNING("Requested invalid phymode/channel "
8557 "combination: %d %d\n", phymode, channel);
8558 mutex_unlock(&priv->mutex);
8559 return -EINVAL;
8562 /* Cancel any currently running scans... */
8563 if (iwl4965_scan_cancel_timeout(priv, 100))
8564 IWL_WARNING("Could not cancel scan.\n");
8565 else {
8566 IWL_DEBUG_INFO("Committing phymode and "
8567 "rxon.channel = %d %d\n",
8568 phymode, channel);
8570 iwl4965_set_rxon_channel(priv, phymode, channel);
8571 iwl4965_set_flags_for_phymode(priv, phymode);
8573 iwl4965_set_rate(priv);
8574 iwl4965_commit_rxon(priv);
8577 mutex_unlock(&priv->mutex);
8579 return count;
8582 static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
8584 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
8586 static ssize_t show_measurement(struct device *d,
8587 struct device_attribute *attr, char *buf)
8589 struct iwl4965_priv *priv = dev_get_drvdata(d);
8590 struct iwl4965_spectrum_notification measure_report;
8591 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8592 u8 *data = (u8 *) & measure_report;
8593 unsigned long flags;
8595 spin_lock_irqsave(&priv->lock, flags);
8596 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8597 spin_unlock_irqrestore(&priv->lock, flags);
8598 return 0;
8600 memcpy(&measure_report, &priv->measure_report, size);
8601 priv->measurement_status = 0;
8602 spin_unlock_irqrestore(&priv->lock, flags);
8604 while (size && (PAGE_SIZE - len)) {
8605 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8606 PAGE_SIZE - len, 1);
8607 len = strlen(buf);
8608 if (PAGE_SIZE - len)
8609 buf[len++] = '\n';
8611 ofs += 16;
8612 size -= min(size, 16U);
8615 return len;
8618 static ssize_t store_measurement(struct device *d,
8619 struct device_attribute *attr,
8620 const char *buf, size_t count)
8622 struct iwl4965_priv *priv = dev_get_drvdata(d);
8623 struct ieee80211_measurement_params params = {
8624 .channel = le16_to_cpu(priv->active_rxon.channel),
8625 .start_time = cpu_to_le64(priv->last_tsf),
8626 .duration = cpu_to_le16(1),
8628 u8 type = IWL_MEASURE_BASIC;
8629 u8 buffer[32];
8630 u8 channel;
8632 if (count) {
8633 char *p = buffer;
8634 strncpy(buffer, buf, min(sizeof(buffer), count));
8635 channel = simple_strtoul(p, NULL, 0);
8636 if (channel)
8637 params.channel = channel;
8639 p = buffer;
8640 while (*p && *p != ' ')
8641 p++;
8642 if (*p)
8643 type = simple_strtoul(p + 1, NULL, 0);
8646 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8647 "channel %d (for '%s')\n", type, params.channel, buf);
8648 iwl4965_get_measurement(priv, &params, type);
8650 return count;
8653 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8654 show_measurement, store_measurement);
8655 #endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
8657 static ssize_t store_retry_rate(struct device *d,
8658 struct device_attribute *attr,
8659 const char *buf, size_t count)
8661 struct iwl4965_priv *priv = dev_get_drvdata(d);
8663 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8664 if (priv->retry_rate <= 0)
8665 priv->retry_rate = 1;
8667 return count;
8670 static ssize_t show_retry_rate(struct device *d,
8671 struct device_attribute *attr, char *buf)
8673 struct iwl4965_priv *priv = dev_get_drvdata(d);
8674 return sprintf(buf, "%d", priv->retry_rate);
8677 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8678 store_retry_rate);
8680 static ssize_t store_power_level(struct device *d,
8681 struct device_attribute *attr,
8682 const char *buf, size_t count)
8684 struct iwl4965_priv *priv = dev_get_drvdata(d);
8685 int rc;
8686 int mode;
8688 mode = simple_strtoul(buf, NULL, 0);
8689 mutex_lock(&priv->mutex);
8691 if (!iwl4965_is_ready(priv)) {
8692 rc = -EAGAIN;
8693 goto out;
8696 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8697 mode = IWL_POWER_AC;
8698 else
8699 mode |= IWL_POWER_ENABLED;
8701 if (mode != priv->power_mode) {
8702 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
8703 if (rc) {
8704 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8705 goto out;
8707 priv->power_mode = mode;
8710 rc = count;
8712 out:
8713 mutex_unlock(&priv->mutex);
8714 return rc;
8717 #define MAX_WX_STRING 80
8719 /* Values are in microsecond */
8720 static const s32 timeout_duration[] = {
8721 350000,
8722 250000,
8723 75000,
8724 37000,
8725 25000,
8727 static const s32 period_duration[] = {
8728 400000,
8729 700000,
8730 1000000,
8731 1000000,
8732 1000000
8735 static ssize_t show_power_level(struct device *d,
8736 struct device_attribute *attr, char *buf)
8738 struct iwl4965_priv *priv = dev_get_drvdata(d);
8739 int level = IWL_POWER_LEVEL(priv->power_mode);
8740 char *p = buf;
8742 p += sprintf(p, "%d ", level);
8743 switch (level) {
8744 case IWL_POWER_MODE_CAM:
8745 case IWL_POWER_AC:
8746 p += sprintf(p, "(AC)");
8747 break;
8748 case IWL_POWER_BATTERY:
8749 p += sprintf(p, "(BATTERY)");
8750 break;
8751 default:
8752 p += sprintf(p,
8753 "(Timeout %dms, Period %dms)",
8754 timeout_duration[level - 1] / 1000,
8755 period_duration[level - 1] / 1000);
8758 if (!(priv->power_mode & IWL_POWER_ENABLED))
8759 p += sprintf(p, " OFF\n");
8760 else
8761 p += sprintf(p, " \n");
8763 return (p - buf + 1);
8767 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8768 store_power_level);
8770 static ssize_t show_channels(struct device *d,
8771 struct device_attribute *attr, char *buf)
8773 struct iwl4965_priv *priv = dev_get_drvdata(d);
8774 int len = 0, i;
8775 struct ieee80211_channel *channels = NULL;
8776 const struct ieee80211_hw_mode *hw_mode = NULL;
8777 int count = 0;
8779 if (!iwl4965_is_ready(priv))
8780 return -EAGAIN;
8782 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211G);
8783 if (!hw_mode)
8784 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211B);
8785 if (hw_mode) {
8786 channels = hw_mode->channels;
8787 count = hw_mode->num_channels;
8790 len +=
8791 sprintf(&buf[len],
8792 "Displaying %d channels in 2.4GHz band "
8793 "(802.11bg):\n", count);
8795 for (i = 0; i < count; i++)
8796 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8797 channels[i].chan,
8798 channels[i].power_level,
8799 channels[i].
8800 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8801 " (IEEE 802.11h required)" : "",
8802 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8803 || (channels[i].
8804 flag &
8805 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8806 ", IBSS",
8807 channels[i].
8808 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8809 "active/passive" : "passive only");
8811 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211A);
8812 if (hw_mode) {
8813 channels = hw_mode->channels;
8814 count = hw_mode->num_channels;
8815 } else {
8816 channels = NULL;
8817 count = 0;
8820 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8821 "(802.11a):\n", count);
8823 for (i = 0; i < count; i++)
8824 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8825 channels[i].chan,
8826 channels[i].power_level,
8827 channels[i].
8828 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8829 " (IEEE 802.11h required)" : "",
8830 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8831 || (channels[i].
8832 flag &
8833 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8834 ", IBSS",
8835 channels[i].
8836 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8837 "active/passive" : "passive only");
8839 return len;
8842 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8844 static ssize_t show_statistics(struct device *d,
8845 struct device_attribute *attr, char *buf)
8847 struct iwl4965_priv *priv = dev_get_drvdata(d);
8848 u32 size = sizeof(struct iwl4965_notif_statistics);
8849 u32 len = 0, ofs = 0;
8850 u8 *data = (u8 *) & priv->statistics;
8851 int rc = 0;
8853 if (!iwl4965_is_alive(priv))
8854 return -EAGAIN;
8856 mutex_lock(&priv->mutex);
8857 rc = iwl4965_send_statistics_request(priv);
8858 mutex_unlock(&priv->mutex);
8860 if (rc) {
8861 len = sprintf(buf,
8862 "Error sending statistics request: 0x%08X\n", rc);
8863 return len;
8866 while (size && (PAGE_SIZE - len)) {
8867 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8868 PAGE_SIZE - len, 1);
8869 len = strlen(buf);
8870 if (PAGE_SIZE - len)
8871 buf[len++] = '\n';
8873 ofs += 16;
8874 size -= min(size, 16U);
8877 return len;
8880 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8882 static ssize_t show_antenna(struct device *d,
8883 struct device_attribute *attr, char *buf)
8885 struct iwl4965_priv *priv = dev_get_drvdata(d);
8887 if (!iwl4965_is_alive(priv))
8888 return -EAGAIN;
8890 return sprintf(buf, "%d\n", priv->antenna);
8893 static ssize_t store_antenna(struct device *d,
8894 struct device_attribute *attr,
8895 const char *buf, size_t count)
8897 int ant;
8898 struct iwl4965_priv *priv = dev_get_drvdata(d);
8900 if (count == 0)
8901 return 0;
8903 if (sscanf(buf, "%1i", &ant) != 1) {
8904 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8905 return count;
8908 if ((ant >= 0) && (ant <= 2)) {
8909 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
8910 priv->antenna = (enum iwl4965_antenna)ant;
8911 } else
8912 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8915 return count;
8918 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8920 static ssize_t show_status(struct device *d,
8921 struct device_attribute *attr, char *buf)
8923 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8924 if (!iwl4965_is_alive(priv))
8925 return -EAGAIN;
8926 return sprintf(buf, "0x%08x\n", (int)priv->status);
8929 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8931 static ssize_t dump_error_log(struct device *d,
8932 struct device_attribute *attr,
8933 const char *buf, size_t count)
8935 char *p = (char *)buf;
8937 if (p[0] == '1')
8938 iwl4965_dump_nic_error_log((struct iwl4965_priv *)d->driver_data);
8940 return strnlen(buf, count);
8943 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8945 static ssize_t dump_event_log(struct device *d,
8946 struct device_attribute *attr,
8947 const char *buf, size_t count)
8949 char *p = (char *)buf;
8951 if (p[0] == '1')
8952 iwl4965_dump_nic_event_log((struct iwl4965_priv *)d->driver_data);
8954 return strnlen(buf, count);
8957 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8959 /*****************************************************************************
8961 * driver setup and teardown
8963 *****************************************************************************/
8965 static void iwl4965_setup_deferred_work(struct iwl4965_priv *priv)
8967 priv->workqueue = create_workqueue(DRV_NAME);
8969 init_waitqueue_head(&priv->wait_command_queue);
8971 INIT_WORK(&priv->up, iwl4965_bg_up);
8972 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8973 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8974 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8975 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8976 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8977 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8978 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8979 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8980 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8981 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8982 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
8984 iwl4965_hw_setup_deferred_work(priv);
8986 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
8987 iwl4965_irq_tasklet, (unsigned long)priv);
8990 static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv)
8992 iwl4965_hw_cancel_deferred_work(priv);
8994 cancel_delayed_work_sync(&priv->init_alive_start);
8995 cancel_delayed_work(&priv->scan_check);
8996 cancel_delayed_work(&priv->alive_start);
8997 cancel_delayed_work(&priv->post_associate);
8998 cancel_work_sync(&priv->beacon_update);
9001 static struct attribute *iwl4965_sysfs_entries[] = {
9002 &dev_attr_antenna.attr,
9003 &dev_attr_channels.attr,
9004 &dev_attr_dump_errors.attr,
9005 &dev_attr_dump_events.attr,
9006 &dev_attr_flags.attr,
9007 &dev_attr_filter_flags.attr,
9008 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
9009 &dev_attr_measurement.attr,
9010 #endif
9011 &dev_attr_power_level.attr,
9012 &dev_attr_retry_rate.attr,
9013 &dev_attr_rf_kill.attr,
9014 &dev_attr_rs_window.attr,
9015 &dev_attr_statistics.attr,
9016 &dev_attr_status.attr,
9017 &dev_attr_temperature.attr,
9018 &dev_attr_tune.attr,
9019 &dev_attr_tx_power.attr,
9021 NULL
9024 static struct attribute_group iwl4965_attribute_group = {
9025 .name = NULL, /* put in device directory */
9026 .attrs = iwl4965_sysfs_entries,
9029 static struct ieee80211_ops iwl4965_hw_ops = {
9030 .tx = iwl4965_mac_tx,
9031 .start = iwl4965_mac_start,
9032 .stop = iwl4965_mac_stop,
9033 .add_interface = iwl4965_mac_add_interface,
9034 .remove_interface = iwl4965_mac_remove_interface,
9035 .config = iwl4965_mac_config,
9036 .config_interface = iwl4965_mac_config_interface,
9037 .configure_filter = iwl4965_configure_filter,
9038 .set_key = iwl4965_mac_set_key,
9039 .get_stats = iwl4965_mac_get_stats,
9040 .get_tx_stats = iwl4965_mac_get_tx_stats,
9041 .conf_tx = iwl4965_mac_conf_tx,
9042 .get_tsf = iwl4965_mac_get_tsf,
9043 .reset_tsf = iwl4965_mac_reset_tsf,
9044 .beacon_update = iwl4965_mac_beacon_update,
9045 .bss_info_changed = iwl4965_bss_info_changed,
9046 #ifdef CONFIG_IWL4965_HT
9047 .conf_ht = iwl4965_mac_conf_ht,
9048 .ampdu_action = iwl4965_mac_ampdu_action,
9049 #ifdef CONFIG_IWL4965_HT_AGG
9050 .ht_tx_agg_start = iwl4965_mac_ht_tx_agg_start,
9051 .ht_tx_agg_stop = iwl4965_mac_ht_tx_agg_stop,
9052 #endif /* CONFIG_IWL4965_HT_AGG */
9053 #endif /* CONFIG_IWL4965_HT */
9054 .hw_scan = iwl4965_mac_hw_scan
9057 static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
9059 int err = 0;
9060 struct iwl4965_priv *priv;
9061 struct ieee80211_hw *hw;
9062 int i;
9063 DECLARE_MAC_BUF(mac);
9065 /* Disabling hardware scan means that mac80211 will perform scans
9066 * "the hard way", rather than using device's scan. */
9067 if (iwl4965_param_disable_hw_scan) {
9068 IWL_DEBUG_INFO("Disabling hw_scan\n");
9069 iwl4965_hw_ops.hw_scan = NULL;
9072 if ((iwl4965_param_queues_num > IWL_MAX_NUM_QUEUES) ||
9073 (iwl4965_param_queues_num < IWL_MIN_NUM_QUEUES)) {
9074 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
9075 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
9076 err = -EINVAL;
9077 goto out;
9080 /* mac80211 allocates memory for this device instance, including
9081 * space for this driver's private structure */
9082 hw = ieee80211_alloc_hw(sizeof(struct iwl4965_priv), &iwl4965_hw_ops);
9083 if (hw == NULL) {
9084 IWL_ERROR("Can not allocate network device\n");
9085 err = -ENOMEM;
9086 goto out;
9088 SET_IEEE80211_DEV(hw, &pdev->dev);
9090 hw->rate_control_algorithm = "iwl-4965-rs";
9092 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
9093 priv = hw->priv;
9094 priv->hw = hw;
9096 priv->pci_dev = pdev;
9097 priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
9098 #ifdef CONFIG_IWL4965_DEBUG
9099 iwl4965_debug_level = iwl4965_param_debug;
9100 atomic_set(&priv->restrict_refcnt, 0);
9101 #endif
9102 priv->retry_rate = 1;
9104 priv->ibss_beacon = NULL;
9106 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
9107 * the range of signal quality values that we'll provide.
9108 * Negative values for level/noise indicate that we'll provide dBm.
9109 * For WE, at least, non-0 values here *enable* display of values
9110 * in app (iwconfig). */
9111 hw->max_rssi = -20; /* signal level, negative indicates dBm */
9112 hw->max_noise = -20; /* noise level, negative indicates dBm */
9113 hw->max_signal = 100; /* link quality indication (%) */
9115 /* Tell mac80211 our Tx characteristics */
9116 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
9118 /* Default value; 4 EDCA QOS priorities */
9119 hw->queues = 4;
9120 #ifdef CONFIG_IWL4965_HT
9121 #ifdef CONFIG_IWL4965_HT_AGG
9122 /* Enhanced value; more queues, to support 11n aggregation */
9123 hw->queues = 16;
9124 #endif /* CONFIG_IWL4965_HT_AGG */
9125 #endif /* CONFIG_IWL4965_HT */
9127 spin_lock_init(&priv->lock);
9128 spin_lock_init(&priv->power_data.lock);
9129 spin_lock_init(&priv->sta_lock);
9130 spin_lock_init(&priv->hcmd_lock);
9131 spin_lock_init(&priv->lq_mngr.lock);
9133 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
9134 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
9136 INIT_LIST_HEAD(&priv->free_frames);
9138 mutex_init(&priv->mutex);
9139 if (pci_enable_device(pdev)) {
9140 err = -ENODEV;
9141 goto out_ieee80211_free_hw;
9144 pci_set_master(pdev);
9146 /* Clear the driver's (not device's) station table */
9147 iwl4965_clear_stations_table(priv);
9149 priv->data_retry_limit = -1;
9150 priv->ieee_channels = NULL;
9151 priv->ieee_rates = NULL;
9152 priv->phymode = -1;
9154 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
9155 if (!err)
9156 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
9157 if (err) {
9158 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
9159 goto out_pci_disable_device;
9162 pci_set_drvdata(pdev, priv);
9163 err = pci_request_regions(pdev, DRV_NAME);
9164 if (err)
9165 goto out_pci_disable_device;
9167 /* We disable the RETRY_TIMEOUT register (0x41) to keep
9168 * PCI Tx retries from interfering with C3 CPU state */
9169 pci_write_config_byte(pdev, 0x41, 0x00);
9171 priv->hw_base = pci_iomap(pdev, 0, 0);
9172 if (!priv->hw_base) {
9173 err = -ENODEV;
9174 goto out_pci_release_regions;
9177 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
9178 (unsigned long long) pci_resource_len(pdev, 0));
9179 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
9181 /* Initialize module parameter values here */
9183 /* Disable radio (SW RF KILL) via parameter when loading driver */
9184 if (iwl4965_param_disable) {
9185 set_bit(STATUS_RF_KILL_SW, &priv->status);
9186 IWL_DEBUG_INFO("Radio disabled.\n");
9189 priv->iw_mode = IEEE80211_IF_TYPE_STA;
9191 priv->ps_mode = 0;
9192 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
9193 priv->valid_antenna = 0x7; /* assume all 3 connected */
9194 priv->ps_mode = IWL_MIMO_PS_NONE;
9196 /* Choose which receivers/antennas to use */
9197 iwl4965_set_rxon_chain(priv);
9199 printk(KERN_INFO DRV_NAME
9200 ": Detected Intel Wireless WiFi Link 4965AGN\n");
9202 /* Device-specific setup */
9203 if (iwl4965_hw_set_hw_setting(priv)) {
9204 IWL_ERROR("failed to set hw settings\n");
9205 goto out_iounmap;
9208 #ifdef CONFIG_IWL4965_QOS
9209 if (iwl4965_param_qos_enable)
9210 priv->qos_data.qos_enable = 1;
9212 iwl4965_reset_qos(priv);
9214 priv->qos_data.qos_active = 0;
9215 priv->qos_data.qos_cap.val = 0;
9216 #endif /* CONFIG_IWL4965_QOS */
9218 iwl4965_set_rxon_channel(priv, MODE_IEEE80211G, 6);
9219 iwl4965_setup_deferred_work(priv);
9220 iwl4965_setup_rx_handlers(priv);
9222 priv->rates_mask = IWL_RATES_MASK;
9223 /* If power management is turned on, default to AC mode */
9224 priv->power_mode = IWL_POWER_AC;
9225 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
9227 iwl4965_disable_interrupts(priv);
9229 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
9230 if (err) {
9231 IWL_ERROR("failed to create sysfs device attributes\n");
9232 goto out_release_irq;
9235 /* nic init */
9236 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
9237 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
9239 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
9240 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
9241 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
9242 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
9243 if (err < 0) {
9244 IWL_DEBUG_INFO("Failed to init the card\n");
9245 goto out_remove_sysfs;
9247 /* Read the EEPROM */
9248 err = iwl4965_eeprom_init(priv);
9249 if (err) {
9250 IWL_ERROR("Unable to init EEPROM\n");
9251 goto out_remove_sysfs;
9253 /* MAC Address location in EEPROM same for 3945/4965 */
9254 get_eeprom_mac(priv, priv->mac_addr);
9255 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
9256 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
9258 err = iwl4965_init_channel_map(priv);
9259 if (err) {
9260 IWL_ERROR("initializing regulatory failed: %d\n", err);
9261 goto out_remove_sysfs;
9264 err = iwl4965_init_geos(priv);
9265 if (err) {
9266 IWL_ERROR("initializing geos failed: %d\n", err);
9267 goto out_free_channel_map;
9269 iwl4965_reset_channel_flag(priv);
9271 iwl4965_rate_control_register(priv->hw);
9272 err = ieee80211_register_hw(priv->hw);
9273 if (err) {
9274 IWL_ERROR("Failed to register network device (error %d)\n", err);
9275 goto out_free_geos;
9278 priv->hw->conf.beacon_int = 100;
9279 priv->mac80211_registered = 1;
9280 pci_save_state(pdev);
9281 pci_disable_device(pdev);
9283 return 0;
9285 out_free_geos:
9286 iwl4965_free_geos(priv);
9287 out_free_channel_map:
9288 iwl4965_free_channel_map(priv);
9289 out_remove_sysfs:
9290 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
9292 out_release_irq:
9293 destroy_workqueue(priv->workqueue);
9294 priv->workqueue = NULL;
9295 iwl4965_unset_hw_setting(priv);
9297 out_iounmap:
9298 pci_iounmap(pdev, priv->hw_base);
9299 out_pci_release_regions:
9300 pci_release_regions(pdev);
9301 out_pci_disable_device:
9302 pci_disable_device(pdev);
9303 pci_set_drvdata(pdev, NULL);
9304 out_ieee80211_free_hw:
9305 ieee80211_free_hw(priv->hw);
9306 out:
9307 return err;
9310 static void iwl4965_pci_remove(struct pci_dev *pdev)
9312 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
9313 struct list_head *p, *q;
9314 int i;
9316 if (!priv)
9317 return;
9319 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
9321 set_bit(STATUS_EXIT_PENDING, &priv->status);
9323 iwl4965_down(priv);
9325 /* Free MAC hash list for ADHOC */
9326 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
9327 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
9328 list_del(p);
9329 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
9333 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
9335 iwl4965_dealloc_ucode_pci(priv);
9337 if (priv->rxq.bd)
9338 iwl4965_rx_queue_free(priv, &priv->rxq);
9339 iwl4965_hw_txq_ctx_free(priv);
9341 iwl4965_unset_hw_setting(priv);
9342 iwl4965_clear_stations_table(priv);
9344 if (priv->mac80211_registered) {
9345 ieee80211_unregister_hw(priv->hw);
9346 iwl4965_rate_control_unregister(priv->hw);
9349 /*netif_stop_queue(dev); */
9350 flush_workqueue(priv->workqueue);
9352 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
9353 * priv->workqueue... so we can't take down the workqueue
9354 * until now... */
9355 destroy_workqueue(priv->workqueue);
9356 priv->workqueue = NULL;
9358 pci_iounmap(pdev, priv->hw_base);
9359 pci_release_regions(pdev);
9360 pci_disable_device(pdev);
9361 pci_set_drvdata(pdev, NULL);
9363 iwl4965_free_channel_map(priv);
9364 iwl4965_free_geos(priv);
9366 if (priv->ibss_beacon)
9367 dev_kfree_skb(priv->ibss_beacon);
9369 ieee80211_free_hw(priv->hw);
9372 #ifdef CONFIG_PM
9374 static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
9376 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
9378 if (priv->is_open) {
9379 set_bit(STATUS_IN_SUSPEND, &priv->status);
9380 iwl4965_mac_stop(priv->hw);
9381 priv->is_open = 1;
9384 pci_set_power_state(pdev, PCI_D3hot);
9386 return 0;
9389 static int iwl4965_pci_resume(struct pci_dev *pdev)
9391 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
9393 pci_set_power_state(pdev, PCI_D0);
9395 if (priv->is_open)
9396 iwl4965_mac_start(priv->hw);
9398 clear_bit(STATUS_IN_SUSPEND, &priv->status);
9399 return 0;
9402 #endif /* CONFIG_PM */
9404 /*****************************************************************************
9406 * driver and module entry point
9408 *****************************************************************************/
9410 static struct pci_driver iwl4965_driver = {
9411 .name = DRV_NAME,
9412 .id_table = iwl4965_hw_card_ids,
9413 .probe = iwl4965_pci_probe,
9414 .remove = __devexit_p(iwl4965_pci_remove),
9415 #ifdef CONFIG_PM
9416 .suspend = iwl4965_pci_suspend,
9417 .resume = iwl4965_pci_resume,
9418 #endif
9421 static int __init iwl4965_init(void)
9424 int ret;
9425 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
9426 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
9427 ret = pci_register_driver(&iwl4965_driver);
9428 if (ret) {
9429 IWL_ERROR("Unable to initialize PCI module\n");
9430 return ret;
9432 #ifdef CONFIG_IWL4965_DEBUG
9433 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
9434 if (ret) {
9435 IWL_ERROR("Unable to create driver sysfs file\n");
9436 pci_unregister_driver(&iwl4965_driver);
9437 return ret;
9439 #endif
9441 return ret;
9444 static void __exit iwl4965_exit(void)
9446 #ifdef CONFIG_IWL4965_DEBUG
9447 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
9448 #endif
9449 pci_unregister_driver(&iwl4965_driver);
9452 module_param_named(antenna, iwl4965_param_antenna, int, 0444);
9453 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
9454 module_param_named(disable, iwl4965_param_disable, int, 0444);
9455 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
9456 module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
9457 MODULE_PARM_DESC(hwcrypto,
9458 "using hardware crypto engine (default 0 [software])\n");
9459 module_param_named(debug, iwl4965_param_debug, int, 0444);
9460 MODULE_PARM_DESC(debug, "debug output mask");
9461 module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
9462 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9464 module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
9465 MODULE_PARM_DESC(queues_num, "number of hw queues.");
9467 /* QoS */
9468 module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
9469 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
9470 module_param_named(amsdu_size_8K, iwl4965_param_amsdu_size_8K, int, 0444);
9471 MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
9473 module_exit(iwl4965_exit);
9474 module_init(iwl4965_init);