1 /******************************************************************************
3 * Copyright(c) 2003 - 2008 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
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-eeprom.h"
52 #include "iwl-helpers.h"
54 #include "iwl-calib.h"
56 static int iwl_txq_update_write_ptr(struct iwl_priv *priv,
57 struct iwl_tx_queue *txq);
59 /******************************************************************************
63 ******************************************************************************/
66 * module name, copyright, version, etc.
67 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
70 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
72 #ifdef CONFIG_IWLWIFI_DEBUG
78 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
84 #define DRV_VERSION IWLWIFI_VERSION VD VS
87 MODULE_DESCRIPTION(DRV_DESCRIPTION);
88 MODULE_VERSION(DRV_VERSION);
89 MODULE_AUTHOR(DRV_COPYRIGHT);
90 MODULE_LICENSE("GPL");
92 static const struct ieee80211_supported_band *iwl_get_hw_mode(
93 struct iwl_priv *priv, enum ieee80211_band band)
95 return priv->hw->wiphy->bands[band];
98 static int iwl4965_is_empty_essid(const char *essid, int essid_len)
100 /* Single white space is for Linksys APs */
101 if (essid_len == 1 && essid[0] == ' ')
104 /* Otherwise, if the entire essid is 0, we assume it is hidden */
107 if (essid[essid_len] != '\0')
114 static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
116 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
117 const char *s = essid;
120 if (iwl4965_is_empty_essid(essid, essid_len)) {
121 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
125 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
126 while (essid_len--) {
139 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
142 * Theory of operation
144 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
145 * of buffer descriptors, each of which points to one or more data buffers for
146 * the device to read from or fill. Driver and device exchange status of each
147 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
148 * entries in each circular buffer, to protect against confusing empty and full
151 * The device reads or writes the data in the queues via the device's several
152 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
154 * For Tx queue, there are low mark and high mark limits. If, after queuing
155 * the packet for Tx, free space become < low mark, Tx queue stopped. When
156 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
159 * The 4965 operates with up to 17 queues: One receive queue, one transmit
160 * queue (#4) for sending commands to the device firmware, and 15 other
161 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
163 * See more detailed info in iwl-4965-hw.h.
164 ***************************************************/
166 int iwl_queue_space(const struct iwl_queue *q)
168 int s = q->read_ptr - q->write_ptr;
170 if (q->read_ptr > q->write_ptr)
175 /* keep some reserve to not confuse empty and full situations */
183 static inline int iwl_queue_used(const struct iwl_queue *q, int i)
185 return q->write_ptr > q->read_ptr ?
186 (i >= q->read_ptr && i < q->write_ptr) :
187 !(i < q->read_ptr && i >= q->write_ptr);
190 static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
192 /* This is for scan command, the big buffer at end of command array */
194 return q->n_window; /* must be power of 2 */
196 /* Otherwise, use normal size buffers */
197 return index & (q->n_window - 1);
201 /*************** STATION TABLE MANAGEMENT ****
202 * mac80211 should be examined to determine if sta_info is duplicating
203 * the functionality provided here
206 /**************************************************************/
208 #if 0 /* temporary disable till we add real remove station */
210 * iwl4965_remove_station - Remove driver's knowledge of station.
212 * NOTE: This does not remove station from device's station table.
214 static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
216 int index = IWL_INVALID_STATION;
220 spin_lock_irqsave(&priv->sta_lock, flags);
224 else if (is_broadcast_ether_addr(addr))
225 index = priv->hw_params.bcast_sta_id;
227 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
228 if (priv->stations[i].used &&
229 !compare_ether_addr(priv->stations[i].sta.sta.addr,
235 if (unlikely(index == IWL_INVALID_STATION))
238 if (priv->stations[index].used) {
239 priv->stations[index].used = 0;
240 priv->num_stations--;
243 BUG_ON(priv->num_stations < 0);
246 spin_unlock_irqrestore(&priv->sta_lock, flags);
253 /*************** HOST COMMAND QUEUE FUNCTIONS *****/
256 * iwl4965_enqueue_hcmd - enqueue a uCode command
257 * @priv: device private data point
258 * @cmd: a point to the ucode command structure
260 * The function returns < 0 values to indicate the operation is
261 * failed. On success, it turns the index (> 0) of command in the
264 int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
266 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
267 struct iwl_queue *q = &txq->q;
268 struct iwl_tfd_frame *tfd;
270 struct iwl_cmd *out_cmd;
273 dma_addr_t phys_addr;
277 cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
278 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
280 /* If any of the command structures end up being larger than
281 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
282 * we will need to increase the size of the TFD entries */
283 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
284 !(cmd->meta.flags & CMD_SIZE_HUGE));
286 if (iwl_is_rfkill(priv)) {
287 IWL_DEBUG_INFO("Not sending command - RF KILL");
291 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
292 IWL_ERROR("No space for Tx\n");
296 spin_lock_irqsave(&priv->hcmd_lock, flags);
298 tfd = &txq->bd[q->write_ptr];
299 memset(tfd, 0, sizeof(*tfd));
301 control_flags = (u32 *) tfd;
303 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
304 out_cmd = &txq->cmd[idx];
306 out_cmd->hdr.cmd = cmd->id;
307 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
308 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
310 /* At this point, the out_cmd now has all of the incoming cmd
313 out_cmd->hdr.flags = 0;
314 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
315 INDEX_TO_SEQ(q->write_ptr));
316 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
317 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
319 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
320 offsetof(struct iwl_cmd, hdr);
321 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
323 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
324 "%d bytes at %d[%d]:%d\n",
325 get_cmd_string(out_cmd->hdr.cmd),
326 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
327 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
329 txq->need_update = 1;
331 /* Set up entry in queue's byte count circular buffer */
332 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
334 /* Increment and update queue's write index */
335 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
336 ret = iwl_txq_update_write_ptr(priv, txq);
338 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
339 return ret ? ret : idx;
342 static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
344 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
347 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
349 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
354 * iwl4965_check_rxon_cmd - validate RXON structure is valid
356 * NOTE: This is really only useful during development and can eventually
357 * be #ifdef'd out once the driver is stable and folks aren't actively
360 static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
365 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
366 error |= le32_to_cpu(rxon->flags &
367 (RXON_FLG_TGJ_NARROW_BAND_MSK |
368 RXON_FLG_RADAR_DETECT_MSK));
370 IWL_WARNING("check 24G fields %d | %d\n",
373 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
374 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
376 IWL_WARNING("check 52 fields %d | %d\n",
378 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
380 IWL_WARNING("check 52 CCK %d | %d\n",
383 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
385 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
387 /* make sure basic rates 6Mbps and 1Mbps are supported */
388 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
389 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
391 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
393 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
395 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
397 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
398 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
400 IWL_WARNING("check CCK and short slot %d | %d\n",
403 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
404 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
406 IWL_WARNING("check CCK & auto detect %d | %d\n",
409 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
410 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
412 IWL_WARNING("check TGG and auto detect %d | %d\n",
416 IWL_WARNING("Tuning to channel %d\n",
417 le16_to_cpu(rxon->channel));
420 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
427 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
428 * @priv: staging_rxon is compared to active_rxon
430 * If the RXON structure is changing enough to require a new tune,
431 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
432 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
434 static int iwl4965_full_rxon_required(struct iwl_priv *priv)
437 /* These items are only settable from the full RXON command */
438 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
439 compare_ether_addr(priv->staging_rxon.bssid_addr,
440 priv->active_rxon.bssid_addr) ||
441 compare_ether_addr(priv->staging_rxon.node_addr,
442 priv->active_rxon.node_addr) ||
443 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
444 priv->active_rxon.wlap_bssid_addr) ||
445 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
446 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
447 (priv->staging_rxon.air_propagation !=
448 priv->active_rxon.air_propagation) ||
449 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
450 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
451 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
452 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
453 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
454 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
457 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
458 * be updated with the RXON_ASSOC command -- however only some
459 * flag transitions are allowed using RXON_ASSOC */
461 /* Check if we are not switching bands */
462 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
463 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
466 /* Check if we are switching association toggle */
467 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
468 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
475 * iwl4965_commit_rxon - commit staging_rxon to hardware
477 * The RXON command in staging_rxon is committed to the hardware and
478 * the active_rxon structure is updated with the new data. This
479 * function correctly transitions out of the RXON_ASSOC_MSK state if
480 * a HW tune is required based on the RXON structure changes.
482 static int iwl4965_commit_rxon(struct iwl_priv *priv)
484 /* cast away the const for active_rxon in this function */
485 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
486 DECLARE_MAC_BUF(mac);
489 if (!iwl_is_alive(priv))
492 /* always get timestamp with Rx frame */
493 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
495 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
497 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
501 /* If we don't need to send a full RXON, we can use
502 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
503 * and other flags for the current radio configuration. */
504 if (!iwl4965_full_rxon_required(priv)) {
505 rc = iwl_send_rxon_assoc(priv);
507 IWL_ERROR("Error setting RXON_ASSOC "
508 "configuration (%d).\n", rc);
512 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
517 /* station table will be cleared */
518 priv->assoc_station_added = 0;
520 /* If we are currently associated and the new config requires
521 * an RXON_ASSOC and the new config wants the associated mask enabled,
522 * we must clear the associated from the active configuration
523 * before we apply the new config */
524 if (iwl_is_associated(priv) &&
525 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
526 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
527 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
529 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
530 sizeof(struct iwl_rxon_cmd),
533 /* If the mask clearing failed then we set
534 * active_rxon back to what it was previously */
536 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
537 IWL_ERROR("Error clearing ASSOC_MSK on current "
538 "configuration (%d).\n", rc);
543 IWL_DEBUG_INFO("Sending RXON\n"
544 "* with%s RXON_FILTER_ASSOC_MSK\n"
547 ((priv->staging_rxon.filter_flags &
548 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
549 le16_to_cpu(priv->staging_rxon.channel),
550 print_mac(mac, priv->staging_rxon.bssid_addr));
552 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
553 /* Apply the new configuration */
554 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
555 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
557 IWL_ERROR("Error setting new configuration (%d).\n", rc);
561 iwlcore_clear_stations_table(priv);
563 if (!priv->error_recovering)
564 priv->start_calib = 0;
566 iwl_init_sensitivity(priv);
568 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
570 /* If we issue a new RXON command which required a tune then we must
571 * send a new TXPOWER command or we won't be able to Tx any frames */
572 rc = iwl4965_hw_reg_send_txpower(priv);
574 IWL_ERROR("Error setting Tx power (%d).\n", rc);
578 /* Add the broadcast address so we can send broadcast frames */
579 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
580 IWL_INVALID_STATION) {
581 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
585 /* If we have set the ASSOC_MSK and we are in BSS mode then
586 * add the IWL_AP_ID to the station rate table */
587 if (iwl_is_associated(priv) &&
588 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
589 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
590 == IWL_INVALID_STATION) {
591 IWL_ERROR("Error adding AP address for transmit.\n");
594 priv->assoc_station_added = 1;
595 if (priv->default_wep_key &&
596 iwl_send_static_wepkey_cmd(priv, 0))
597 IWL_ERROR("Could not send WEP static key.\n");
603 void iwl4965_update_chain_flags(struct iwl_priv *priv)
606 iwl_set_rxon_chain(priv);
607 iwl4965_commit_rxon(priv);
610 static int iwl4965_send_bt_config(struct iwl_priv *priv)
612 struct iwl4965_bt_cmd bt_cmd = {
620 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
621 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
624 static int iwl4965_send_scan_abort(struct iwl_priv *priv)
627 struct iwl_rx_packet *res;
628 struct iwl_host_cmd cmd = {
629 .id = REPLY_SCAN_ABORT_CMD,
630 .meta.flags = CMD_WANT_SKB,
633 /* If there isn't a scan actively going on in the hardware
634 * then we are in between scan bands and not actually
635 * actively scanning, so don't send the abort command */
636 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
637 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
641 ret = iwl_send_cmd_sync(priv, &cmd);
643 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
647 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
648 if (res->u.status != CAN_ABORT_STATUS) {
649 /* The scan abort will return 1 for success or
650 * 2 for "failure". A failure condition can be
651 * due to simply not being in an active scan which
652 * can occur if we send the scan abort before we
653 * the microcode has notified us that a scan is
655 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
656 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
657 clear_bit(STATUS_SCAN_HW, &priv->status);
660 dev_kfree_skb_any(cmd.meta.u.skb);
668 * Use: Sets the device's internal card state to enable, disable, or halt
670 * When in the 'enable' state the card operates as normal.
671 * When in the 'disable' state, the card enters into a low power mode.
672 * When in the 'halt' state, the card is shut down and must be fully
673 * restarted to come back on.
675 static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
677 struct iwl_host_cmd cmd = {
678 .id = REPLY_CARD_STATE_CMD,
681 .meta.flags = meta_flag,
684 return iwl_send_cmd(priv, &cmd);
687 static void iwl_clear_free_frames(struct iwl_priv *priv)
689 struct list_head *element;
691 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
694 while (!list_empty(&priv->free_frames)) {
695 element = priv->free_frames.next;
697 kfree(list_entry(element, struct iwl_frame, list));
698 priv->frames_count--;
701 if (priv->frames_count) {
702 IWL_WARNING("%d frames still in use. Did we lose one?\n",
704 priv->frames_count = 0;
708 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
710 struct iwl_frame *frame;
711 struct list_head *element;
712 if (list_empty(&priv->free_frames)) {
713 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
715 IWL_ERROR("Could not allocate frame!\n");
719 priv->frames_count++;
723 element = priv->free_frames.next;
725 return list_entry(element, struct iwl_frame, list);
728 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
730 memset(frame, 0, sizeof(*frame));
731 list_add(&frame->list, &priv->free_frames);
734 unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
735 struct ieee80211_hdr *hdr,
736 const u8 *dest, int left)
739 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
740 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
741 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
744 if (priv->ibss_beacon->len > left)
747 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
749 return priv->ibss_beacon->len;
752 static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
758 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
759 rate_mask = priv->active_rate_basic & 0xF;
761 rate_mask = priv->active_rate_basic & 0xFF0;
763 /* Find lowest valid rate */
764 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
765 i = iwl_rates[i].next_ieee) {
766 if (rate_mask & (1 << i))
767 return iwl_rates[i].plcp;
770 /* No valid rate was found. Assign the lowest one */
771 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
772 return IWL_RATE_1M_PLCP;
774 return IWL_RATE_6M_PLCP;
777 static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
779 struct iwl_frame *frame;
780 unsigned int frame_size;
784 frame = iwl_get_free_frame(priv);
787 IWL_ERROR("Could not obtain free frame buffer for beacon "
792 rate = iwl4965_rate_get_lowest_plcp(priv);
794 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
796 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
799 iwl_free_frame(priv, frame);
804 /******************************************************************************
806 * Misc. internal state and helper functions
808 ******************************************************************************/
811 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
813 * return : set the bit for each supported rate insert in ie
815 static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
816 u16 basic_rate, int *left)
818 u16 ret_rates = 0, bit;
823 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
824 if (bit & supported_rate) {
826 rates[*cnt] = iwl_rates[i].ieee |
827 ((bit & basic_rate) ? 0x80 : 0x00);
831 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
839 #ifdef CONFIG_IWL4965_HT
840 static void iwl4965_ht_conf(struct iwl_priv *priv,
841 struct ieee80211_bss_conf *bss_conf)
843 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
844 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
845 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
847 IWL_DEBUG_MAC80211("enter: \n");
849 iwl_conf->is_ht = bss_conf->assoc_ht;
851 if (!iwl_conf->is_ht)
854 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
856 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
857 iwl_conf->sgf |= 0x1;
858 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
859 iwl_conf->sgf |= 0x2;
861 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
862 iwl_conf->max_amsdu_size =
863 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
865 iwl_conf->supported_chan_width =
866 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
867 iwl_conf->extension_chan_offset =
868 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
869 /* If no above or below channel supplied disable FAT channel */
870 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
871 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
872 iwl_conf->supported_chan_width = 0;
874 iwl_conf->tx_mimo_ps_mode =
875 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
876 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
878 iwl_conf->control_channel = ht_bss_conf->primary_channel;
879 iwl_conf->tx_chan_width =
880 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
881 iwl_conf->ht_protection =
882 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
883 iwl_conf->non_GF_STA_present =
884 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
886 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
887 IWL_DEBUG_MAC80211("leave\n");
890 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
893 struct ieee80211_ht_cap *ht_cap;
895 if (!sband || !sband->ht_info.ht_supported)
898 if (*left < sizeof(struct ieee80211_ht_cap))
901 *pos++ = sizeof(struct ieee80211_ht_cap);
902 ht_cap = (struct ieee80211_ht_cap *) pos;
904 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
905 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
906 ht_cap->ampdu_params_info =
907 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
908 ((sband->ht_info.ampdu_density << 2) &
909 IEEE80211_HT_CAP_AMPDU_DENSITY);
910 *left -= sizeof(struct ieee80211_ht_cap);
913 static inline void iwl4965_ht_conf(struct iwl_priv *priv,
914 struct ieee80211_bss_conf *bss_conf)
917 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
925 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
927 static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
928 enum ieee80211_band band,
929 struct ieee80211_mgmt *frame,
930 int left, int is_direct)
934 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
935 const struct ieee80211_supported_band *sband =
936 iwl_get_hw_mode(priv, band);
938 /* Make sure there is enough space for the probe request,
939 * two mandatory IEs and the data */
945 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
946 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
947 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
948 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
951 /* fill in our indirect SSID IE */
958 pos = &(frame->u.probe_req.variable[0]);
959 *pos++ = WLAN_EID_SSID;
962 /* fill in our direct SSID IE... */
965 left -= 2 + priv->essid_len;
968 /* ... fill it in... */
969 *pos++ = WLAN_EID_SSID;
970 *pos++ = priv->essid_len;
971 memcpy(pos, priv->essid, priv->essid_len);
972 pos += priv->essid_len;
973 len += 2 + priv->essid_len;
976 /* fill in supported rate */
982 /* ... fill it in... */
983 *pos++ = WLAN_EID_SUPP_RATES;
986 /* exclude 60M rate */
987 active_rates = priv->rates_mask;
988 active_rates &= ~IWL_RATE_60M_MASK;
990 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
992 cck_rates = IWL_CCK_RATES_MASK & active_rates;
993 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
994 active_rate_basic, &left);
995 active_rates &= ~ret_rates;
997 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
998 active_rate_basic, &left);
999 active_rates &= ~ret_rates;
1003 if (active_rates == 0)
1006 /* fill in supported extended rate */
1011 /* ... fill it in... */
1012 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1014 iwl4965_supported_rate_to_ie(pos, active_rates,
1015 active_rate_basic, &left);
1025 *pos++ = WLAN_EID_HT_CAPABILITY;
1028 iwl_ht_cap_to_ie(sband, pos, &left);
1038 static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
1039 struct iwl4965_qosparam_cmd *qos)
1042 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1043 sizeof(struct iwl4965_qosparam_cmd), qos);
1046 static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
1048 unsigned long flags;
1050 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1053 if (!priv->qos_data.qos_enable)
1056 spin_lock_irqsave(&priv->lock, flags);
1057 priv->qos_data.def_qos_parm.qos_flags = 0;
1059 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1060 !priv->qos_data.qos_cap.q_AP.txop_request)
1061 priv->qos_data.def_qos_parm.qos_flags |=
1062 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1063 if (priv->qos_data.qos_active)
1064 priv->qos_data.def_qos_parm.qos_flags |=
1065 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1067 #ifdef CONFIG_IWL4965_HT
1068 if (priv->current_ht_config.is_ht)
1069 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
1070 #endif /* CONFIG_IWL4965_HT */
1072 spin_unlock_irqrestore(&priv->lock, flags);
1074 if (force || iwl_is_associated(priv)) {
1075 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1076 priv->qos_data.qos_active,
1077 priv->qos_data.def_qos_parm.qos_flags);
1079 iwl4965_send_qos_params_command(priv,
1080 &(priv->qos_data.def_qos_parm));
1084 int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
1086 /* Filter incoming packets to determine if they are targeted toward
1087 * this network, discarding packets coming from ourselves */
1088 switch (priv->iw_mode) {
1089 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1090 /* packets from our adapter are dropped (echo) */
1091 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1093 /* {broad,multi}cast packets to our IBSS go through */
1094 if (is_multicast_ether_addr(header->addr1))
1095 return !compare_ether_addr(header->addr3, priv->bssid);
1096 /* packets to our adapter go through */
1097 return !compare_ether_addr(header->addr1, priv->mac_addr);
1098 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1099 /* packets from our adapter are dropped (echo) */
1100 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1102 /* {broad,multi}cast packets to our BSS go through */
1103 if (is_multicast_ether_addr(header->addr1))
1104 return !compare_ether_addr(header->addr2, priv->bssid);
1105 /* packets to our adapter go through */
1106 return !compare_ether_addr(header->addr1, priv->mac_addr);
1114 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1116 static const char *iwl4965_get_tx_fail_reason(u32 status)
1118 switch (status & TX_STATUS_MSK) {
1119 case TX_STATUS_SUCCESS:
1121 TX_STATUS_ENTRY(SHORT_LIMIT);
1122 TX_STATUS_ENTRY(LONG_LIMIT);
1123 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1124 TX_STATUS_ENTRY(MGMNT_ABORT);
1125 TX_STATUS_ENTRY(NEXT_FRAG);
1126 TX_STATUS_ENTRY(LIFE_EXPIRE);
1127 TX_STATUS_ENTRY(DEST_PS);
1128 TX_STATUS_ENTRY(ABORTED);
1129 TX_STATUS_ENTRY(BT_RETRY);
1130 TX_STATUS_ENTRY(STA_INVALID);
1131 TX_STATUS_ENTRY(FRAG_DROPPED);
1132 TX_STATUS_ENTRY(TID_DISABLE);
1133 TX_STATUS_ENTRY(FRAME_FLUSHED);
1134 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1135 TX_STATUS_ENTRY(TX_LOCKED);
1136 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1143 * iwl4965_scan_cancel - Cancel any currently executing HW scan
1145 * NOTE: priv->mutex is not required before calling this function
1147 static int iwl4965_scan_cancel(struct iwl_priv *priv)
1149 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1150 clear_bit(STATUS_SCANNING, &priv->status);
1154 if (test_bit(STATUS_SCANNING, &priv->status)) {
1155 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1156 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1157 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1158 queue_work(priv->workqueue, &priv->abort_scan);
1161 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1163 return test_bit(STATUS_SCANNING, &priv->status);
1170 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
1171 * @ms: amount of time to wait (in milliseconds) for scan to abort
1173 * NOTE: priv->mutex must be held before calling this function
1175 static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
1177 unsigned long now = jiffies;
1180 ret = iwl4965_scan_cancel(priv);
1182 mutex_unlock(&priv->mutex);
1183 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1184 test_bit(STATUS_SCANNING, &priv->status))
1186 mutex_lock(&priv->mutex);
1188 return test_bit(STATUS_SCANNING, &priv->status);
1194 static void iwl4965_sequence_reset(struct iwl_priv *priv)
1196 /* Reset ieee stats */
1198 /* We don't reset the net_device_stats (ieee->stats) on
1201 priv->last_seq_num = -1;
1202 priv->last_frag_num = -1;
1203 priv->last_packet_time = 0;
1205 iwl4965_scan_cancel(priv);
1208 #define MAX_UCODE_BEACON_INTERVAL 4096
1209 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1211 static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
1214 u16 beacon_factor = 0;
1217 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1218 / MAX_UCODE_BEACON_INTERVAL;
1219 new_val = beacon_val / beacon_factor;
1221 return cpu_to_le16(new_val);
1224 static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
1226 u64 interval_tm_unit;
1228 unsigned long flags;
1229 struct ieee80211_conf *conf = NULL;
1232 conf = ieee80211_get_hw_conf(priv->hw);
1234 spin_lock_irqsave(&priv->lock, flags);
1235 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1236 priv->rxon_timing.timestamp.dw[0] =
1237 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
1239 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1241 tsf = priv->timestamp;
1243 beacon_int = priv->beacon_int;
1244 spin_unlock_irqrestore(&priv->lock, flags);
1246 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1247 if (beacon_int == 0) {
1248 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1249 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1251 priv->rxon_timing.beacon_interval =
1252 cpu_to_le16(beacon_int);
1253 priv->rxon_timing.beacon_interval =
1254 iwl4965_adjust_beacon_interval(
1255 le16_to_cpu(priv->rxon_timing.beacon_interval));
1258 priv->rxon_timing.atim_window = 0;
1260 priv->rxon_timing.beacon_interval =
1261 iwl4965_adjust_beacon_interval(conf->beacon_int);
1262 /* TODO: we need to get atim_window from upper stack
1263 * for now we set to 0 */
1264 priv->rxon_timing.atim_window = 0;
1268 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1269 result = do_div(tsf, interval_tm_unit);
1270 priv->rxon_timing.beacon_init_val =
1271 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1274 ("beacon interval %d beacon timer %d beacon tim %d\n",
1275 le16_to_cpu(priv->rxon_timing.beacon_interval),
1276 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1277 le16_to_cpu(priv->rxon_timing.atim_window));
1280 static int iwl4965_scan_initiate(struct iwl_priv *priv)
1282 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1283 IWL_ERROR("APs don't scan.\n");
1287 if (!iwl_is_ready_rf(priv)) {
1288 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1292 if (test_bit(STATUS_SCANNING, &priv->status)) {
1293 IWL_DEBUG_SCAN("Scan already in progress.\n");
1297 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1298 IWL_DEBUG_SCAN("Scan request while abort pending. "
1303 IWL_DEBUG_INFO("Starting scan...\n");
1304 priv->scan_bands = 2;
1305 set_bit(STATUS_SCANNING, &priv->status);
1306 priv->scan_start = jiffies;
1307 priv->scan_pass_start = priv->scan_start;
1309 queue_work(priv->workqueue, &priv->request_scan);
1315 static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
1316 enum ieee80211_band band)
1318 if (band == IEEE80211_BAND_5GHZ) {
1319 priv->staging_rxon.flags &=
1320 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1321 | RXON_FLG_CCK_MSK);
1322 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1324 /* Copied from iwl4965_post_associate() */
1325 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1326 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1328 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1330 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1331 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1333 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1334 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1335 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1340 * initialize rxon structure with default values from eeprom
1342 static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
1344 const struct iwl_channel_info *ch_info;
1346 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1348 switch (priv->iw_mode) {
1349 case IEEE80211_IF_TYPE_AP:
1350 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1353 case IEEE80211_IF_TYPE_STA:
1354 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1355 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1358 case IEEE80211_IF_TYPE_IBSS:
1359 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1360 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1361 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1362 RXON_FILTER_ACCEPT_GRP_MSK;
1365 case IEEE80211_IF_TYPE_MNTR:
1366 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1367 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1368 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1371 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1376 /* TODO: Figure out when short_preamble would be set and cache from
1378 if (!hw_to_local(priv->hw)->short_preamble)
1379 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1381 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1384 ch_info = iwl_get_channel_info(priv, priv->band,
1385 le16_to_cpu(priv->staging_rxon.channel));
1388 ch_info = &priv->channel_info[0];
1391 * in some case A channels are all non IBSS
1392 * in this case force B/G channel
1394 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1395 !(is_channel_ibss(ch_info)))
1396 ch_info = &priv->channel_info[0];
1398 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
1399 priv->band = ch_info->band;
1401 iwl4965_set_flags_for_phymode(priv, priv->band);
1403 priv->staging_rxon.ofdm_basic_rates =
1404 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1405 priv->staging_rxon.cck_basic_rates =
1406 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1408 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1409 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1410 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1411 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1412 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1413 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1414 iwl_set_rxon_chain(priv);
1417 static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
1419 if (mode == IEEE80211_IF_TYPE_IBSS) {
1420 const struct iwl_channel_info *ch_info;
1422 ch_info = iwl_get_channel_info(priv,
1424 le16_to_cpu(priv->staging_rxon.channel));
1426 if (!ch_info || !is_channel_ibss(ch_info)) {
1427 IWL_ERROR("channel %d not IBSS channel\n",
1428 le16_to_cpu(priv->staging_rxon.channel));
1433 priv->iw_mode = mode;
1435 iwl4965_connection_init_rx_config(priv);
1436 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1438 iwlcore_clear_stations_table(priv);
1440 /* dont commit rxon if rf-kill is on*/
1441 if (!iwl_is_ready_rf(priv))
1444 cancel_delayed_work(&priv->scan_check);
1445 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1446 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1447 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1451 iwl4965_commit_rxon(priv);
1456 static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
1457 struct ieee80211_tx_control *ctl,
1458 struct iwl_cmd *cmd,
1459 struct sk_buff *skb_frag,
1462 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
1463 struct iwl_wep_key *wepkey;
1466 BUG_ON(ctl->hw_key->hw_key_idx > 3);
1468 switch (keyinfo->alg) {
1470 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1471 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
1472 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1473 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1474 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1478 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
1479 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1480 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1481 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
1485 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
1486 cmd->cmd.tx.sec_ctl = 0;
1487 if (priv->default_wep_key) {
1488 /* the WEP key was sent as static */
1489 keyidx = ctl->hw_key->hw_key_idx;
1490 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1492 if (wepkey->key_size == WEP_KEY_LEN_128)
1493 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1495 /* the WEP key was sent as dynamic */
1496 keyidx = keyinfo->keyidx;
1497 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1499 if (keyinfo->keylen == WEP_KEY_LEN_128)
1500 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1503 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1504 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
1506 IWL_DEBUG_TX("Configuring packet for WEP encryption "
1507 "with key %d\n", keyidx);
1511 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1517 * handle build REPLY_TX command notification.
1519 static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
1520 struct iwl_cmd *cmd,
1521 struct ieee80211_tx_control *ctrl,
1522 struct ieee80211_hdr *hdr,
1523 int is_unicast, u8 std_id)
1525 u16 fc = le16_to_cpu(hdr->frame_control);
1526 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1528 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1529 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1530 tx_flags |= TX_CMD_FLG_ACK_MSK;
1531 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1532 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1533 if (ieee80211_is_probe_response(fc) &&
1534 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1535 tx_flags |= TX_CMD_FLG_TSF_MSK;
1537 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1538 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1541 if (ieee80211_is_back_request(fc))
1542 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1545 cmd->cmd.tx.sta_id = std_id;
1546 if (ieee80211_get_morefrag(hdr))
1547 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1549 if (ieee80211_is_qos_data(fc)) {
1550 u8 *qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
1551 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
1552 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1554 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1557 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1558 tx_flags |= TX_CMD_FLG_RTS_MSK;
1559 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1560 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1561 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1562 tx_flags |= TX_CMD_FLG_CTS_MSK;
1565 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1566 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1568 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1569 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1570 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1571 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
1572 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
1574 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
1576 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
1579 cmd->cmd.tx.driver_txop = 0;
1580 cmd->cmd.tx.tx_flags = tx_flags;
1581 cmd->cmd.tx.next_frame_len = 0;
1583 static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1585 /* 0 - mgmt, 1 - cnt, 2 - data */
1586 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1587 priv->tx_stats[idx].cnt++;
1588 priv->tx_stats[idx].bytes += len;
1591 * start REPLY_TX command process
1593 static int iwl4965_tx_skb(struct iwl_priv *priv,
1594 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1596 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1597 struct iwl_tfd_frame *tfd;
1599 int txq_id = ctl->queue;
1600 struct iwl_tx_queue *txq = NULL;
1601 struct iwl_queue *q = NULL;
1602 dma_addr_t phys_addr;
1603 dma_addr_t txcmd_phys;
1604 dma_addr_t scratch_phys;
1605 struct iwl_cmd *out_cmd = NULL;
1606 u16 len, idx, len_org;
1612 u8 wait_write_ptr = 0;
1616 unsigned long flags;
1619 spin_lock_irqsave(&priv->lock, flags);
1620 if (iwl_is_rfkill(priv)) {
1621 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1626 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
1630 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
1631 IWL_ERROR("ERROR: No TX rate available.\n");
1635 unicast = !is_multicast_ether_addr(hdr->addr1);
1638 fc = le16_to_cpu(hdr->frame_control);
1640 #ifdef CONFIG_IWLWIFI_DEBUG
1641 if (ieee80211_is_auth(fc))
1642 IWL_DEBUG_TX("Sending AUTH frame\n");
1643 else if (ieee80211_is_assoc_request(fc))
1644 IWL_DEBUG_TX("Sending ASSOC frame\n");
1645 else if (ieee80211_is_reassoc_request(fc))
1646 IWL_DEBUG_TX("Sending REASSOC frame\n");
1649 /* drop all data frame if we are not associated */
1650 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
1651 (!iwl_is_associated(priv) ||
1652 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
1653 !priv->assoc_station_added)) {
1654 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
1658 spin_unlock_irqrestore(&priv->lock, flags);
1660 hdr_len = ieee80211_get_hdrlen(fc);
1662 /* Find (or create) index into station table for destination station */
1663 sta_id = iwl_get_sta_id(priv, hdr);
1664 if (sta_id == IWL_INVALID_STATION) {
1665 DECLARE_MAC_BUF(mac);
1667 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
1668 print_mac(mac, hdr->addr1));
1672 IWL_DEBUG_TX("station Id %d\n", sta_id);
1674 if (ieee80211_is_qos_data(fc)) {
1675 qc = ieee80211_get_qos_ctrl(hdr, hdr_len);
1677 seq_number = priv->stations[sta_id].tid[tid].seq_number &
1679 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1681 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
1683 #ifdef CONFIG_IWL4965_HT
1684 /* aggregation is on for this <sta,tid> */
1685 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1686 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
1687 priv->stations[sta_id].tid[tid].tfds_in_queue++;
1688 #endif /* CONFIG_IWL4965_HT */
1691 /* Descriptor for chosen Tx queue */
1692 txq = &priv->txq[txq_id];
1695 spin_lock_irqsave(&priv->lock, flags);
1697 /* Set up first empty TFD within this queue's circular TFD buffer */
1698 tfd = &txq->bd[q->write_ptr];
1699 memset(tfd, 0, sizeof(*tfd));
1700 control_flags = (u32 *) tfd;
1701 idx = get_cmd_index(q, q->write_ptr, 0);
1703 /* Set up driver data for this TFD */
1704 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
1705 txq->txb[q->write_ptr].skb[0] = skb;
1706 memcpy(&(txq->txb[q->write_ptr].status.control),
1707 ctl, sizeof(struct ieee80211_tx_control));
1709 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1710 out_cmd = &txq->cmd[idx];
1711 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1712 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
1715 * Set up the Tx-command (not MAC!) header.
1716 * Store the chosen Tx queue and TFD index within the sequence field;
1717 * after Tx, uCode's Tx response will return this value so driver can
1718 * locate the frame within the tx queue and do post-tx processing.
1720 out_cmd->hdr.cmd = REPLY_TX;
1721 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1722 INDEX_TO_SEQ(q->write_ptr)));
1724 /* Copy MAC header from skb into command buffer */
1725 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
1728 * Use the first empty entry in this queue's command buffer array
1729 * to contain the Tx command and MAC header concatenated together
1730 * (payload data will be in another buffer).
1731 * Size of this varies, due to varying MAC header length.
1732 * If end is not dword aligned, we'll have 2 extra bytes at the end
1733 * of the MAC header (device reads on dword boundaries).
1734 * We'll tell device about this padding later.
1736 len = sizeof(struct iwl_tx_cmd) +
1737 sizeof(struct iwl_cmd_header) + hdr_len;
1740 len = (len + 3) & ~3;
1747 /* Physical address of this Tx command's header (not MAC header!),
1748 * within command buffer array. */
1749 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
1750 offsetof(struct iwl_cmd, hdr);
1752 /* Add buffer containing Tx command and MAC(!) header to TFD's
1754 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
1756 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
1757 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
1759 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1760 * if any (802.11 null frames have no payload). */
1761 len = skb->len - hdr_len;
1763 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
1764 len, PCI_DMA_TODEVICE);
1765 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
1768 /* Tell 4965 about any 2-byte padding after MAC header */
1770 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1772 /* Total # bytes to be transmitted */
1773 len = (u16)skb->len;
1774 out_cmd->cmd.tx.len = cpu_to_le16(len);
1776 /* TODO need this for burst mode later on */
1777 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
1779 /* set is_hcca to 0; it probably will never be implemented */
1780 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
1782 iwl_update_tx_stats(priv, fc, len);
1784 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
1785 offsetof(struct iwl_tx_cmd, scratch);
1786 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
1787 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
1789 if (!ieee80211_get_morefrag(hdr)) {
1790 txq->need_update = 1;
1792 priv->stations[sta_id].tid[tid].seq_number = seq_number;
1795 txq->need_update = 0;
1798 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
1799 sizeof(out_cmd->cmd.tx));
1801 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
1802 ieee80211_get_hdrlen(fc));
1804 /* Set up entry for this TFD in Tx byte-count array */
1805 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
1807 /* Tell device the write index *just past* this latest filled TFD */
1808 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
1809 rc = iwl_txq_update_write_ptr(priv, txq);
1810 spin_unlock_irqrestore(&priv->lock, flags);
1815 if ((iwl_queue_space(q) < q->high_mark)
1816 && priv->mac80211_registered) {
1817 if (wait_write_ptr) {
1818 spin_lock_irqsave(&priv->lock, flags);
1819 txq->need_update = 1;
1820 iwl_txq_update_write_ptr(priv, txq);
1821 spin_unlock_irqrestore(&priv->lock, flags);
1824 ieee80211_stop_queue(priv->hw, ctl->queue);
1830 spin_unlock_irqrestore(&priv->lock, flags);
1835 static void iwl4965_set_rate(struct iwl_priv *priv)
1837 const struct ieee80211_supported_band *hw = NULL;
1838 struct ieee80211_rate *rate;
1841 hw = iwl_get_hw_mode(priv, priv->band);
1843 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
1847 priv->active_rate = 0;
1848 priv->active_rate_basic = 0;
1850 for (i = 0; i < hw->n_bitrates; i++) {
1851 rate = &(hw->bitrates[i]);
1852 if (rate->hw_value < IWL_RATE_COUNT)
1853 priv->active_rate |= (1 << rate->hw_value);
1856 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
1857 priv->active_rate, priv->active_rate_basic);
1860 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
1861 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
1864 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
1865 priv->staging_rxon.cck_basic_rates =
1866 ((priv->active_rate_basic &
1867 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
1869 priv->staging_rxon.cck_basic_rates =
1870 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1872 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
1873 priv->staging_rxon.ofdm_basic_rates =
1874 ((priv->active_rate_basic &
1875 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
1876 IWL_FIRST_OFDM_RATE) & 0xFF;
1878 priv->staging_rxon.ofdm_basic_rates =
1879 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1882 void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
1884 unsigned long flags;
1886 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1889 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
1890 disable_radio ? "OFF" : "ON");
1892 if (disable_radio) {
1893 iwl4965_scan_cancel(priv);
1894 /* FIXME: This is a workaround for AP */
1895 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
1896 spin_lock_irqsave(&priv->lock, flags);
1897 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1898 CSR_UCODE_SW_BIT_RFKILL);
1899 spin_unlock_irqrestore(&priv->lock, flags);
1900 /* call the host command only if no hw rf-kill set */
1901 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
1903 iwl4965_send_card_state(priv,
1904 CARD_STATE_CMD_DISABLE,
1906 set_bit(STATUS_RF_KILL_SW, &priv->status);
1908 /* make sure mac80211 stop sending Tx frame */
1909 if (priv->mac80211_registered)
1910 ieee80211_stop_queues(priv->hw);
1915 spin_lock_irqsave(&priv->lock, flags);
1916 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1918 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1919 spin_unlock_irqrestore(&priv->lock, flags);
1924 spin_lock_irqsave(&priv->lock, flags);
1925 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1926 if (!iwl_grab_nic_access(priv))
1927 iwl_release_nic_access(priv);
1928 spin_unlock_irqrestore(&priv->lock, flags);
1930 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1931 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
1932 "disabled by HW switch\n");
1936 queue_work(priv->workqueue, &priv->restart);
1940 #define IWL_PACKET_RETRY_TIME HZ
1942 int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
1944 u16 sc = le16_to_cpu(header->seq_ctrl);
1945 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1946 u16 frag = sc & IEEE80211_SCTL_FRAG;
1947 u16 *last_seq, *last_frag;
1948 unsigned long *last_time;
1950 switch (priv->iw_mode) {
1951 case IEEE80211_IF_TYPE_IBSS:{
1952 struct list_head *p;
1953 struct iwl4965_ibss_seq *entry = NULL;
1954 u8 *mac = header->addr2;
1955 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
1957 __list_for_each(p, &priv->ibss_mac_hash[index]) {
1958 entry = list_entry(p, struct iwl4965_ibss_seq, list);
1959 if (!compare_ether_addr(entry->mac, mac))
1962 if (p == &priv->ibss_mac_hash[index]) {
1963 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1965 IWL_ERROR("Cannot malloc new mac entry\n");
1968 memcpy(entry->mac, mac, ETH_ALEN);
1969 entry->seq_num = seq;
1970 entry->frag_num = frag;
1971 entry->packet_time = jiffies;
1972 list_add(&entry->list, &priv->ibss_mac_hash[index]);
1975 last_seq = &entry->seq_num;
1976 last_frag = &entry->frag_num;
1977 last_time = &entry->packet_time;
1980 case IEEE80211_IF_TYPE_STA:
1981 last_seq = &priv->last_seq_num;
1982 last_frag = &priv->last_frag_num;
1983 last_time = &priv->last_packet_time;
1988 if ((*last_seq == seq) &&
1989 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
1990 if (*last_frag == frag)
1992 if (*last_frag + 1 != frag)
1993 /* out-of-order fragment */
1999 *last_time = jiffies;
2006 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
2008 #include "iwl-spectrum.h"
2010 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
2011 #define BEACON_TIME_MASK_HIGH 0xFF000000
2012 #define TIME_UNIT 1024
2015 * extended beacon time format
2016 * time in usec will be changed into a 32-bit value in 8:24 format
2017 * the high 1 byte is the beacon counts
2018 * the lower 3 bytes is the time in usec within one beacon interval
2021 static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
2025 u32 interval = beacon_interval * 1024;
2027 if (!interval || !usec)
2030 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2031 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2033 return (quot << 24) + rem;
2036 /* base is usually what we get from ucode with each received frame,
2037 * the same as HW timer counter counting down
2040 static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
2042 u32 base_low = base & BEACON_TIME_MASK_LOW;
2043 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2044 u32 interval = beacon_interval * TIME_UNIT;
2045 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2046 (addon & BEACON_TIME_MASK_HIGH);
2048 if (base_low > addon_low)
2049 res += base_low - addon_low;
2050 else if (base_low < addon_low) {
2051 res += interval + base_low - addon_low;
2056 return cpu_to_le32(res);
2059 static int iwl4965_get_measurement(struct iwl_priv *priv,
2060 struct ieee80211_measurement_params *params,
2063 struct iwl4965_spectrum_cmd spectrum;
2064 struct iwl_rx_packet *res;
2065 struct iwl_host_cmd cmd = {
2066 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2067 .data = (void *)&spectrum,
2068 .meta.flags = CMD_WANT_SKB,
2070 u32 add_time = le64_to_cpu(params->start_time);
2072 int spectrum_resp_status;
2073 int duration = le16_to_cpu(params->duration);
2075 if (iwl_is_associated(priv))
2077 iwl4965_usecs_to_beacons(
2078 le64_to_cpu(params->start_time) - priv->last_tsf,
2079 le16_to_cpu(priv->rxon_timing.beacon_interval));
2081 memset(&spectrum, 0, sizeof(spectrum));
2083 spectrum.channel_count = cpu_to_le16(1);
2085 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2086 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2087 cmd.len = sizeof(spectrum);
2088 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2090 if (iwl_is_associated(priv))
2091 spectrum.start_time =
2092 iwl4965_add_beacon_time(priv->last_beacon_time,
2094 le16_to_cpu(priv->rxon_timing.beacon_interval));
2096 spectrum.start_time = 0;
2098 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2099 spectrum.channels[0].channel = params->channel;
2100 spectrum.channels[0].type = type;
2101 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2102 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2103 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2105 rc = iwl_send_cmd_sync(priv, &cmd);
2109 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
2110 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2111 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2115 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2116 switch (spectrum_resp_status) {
2117 case 0: /* Command will be handled */
2118 if (res->u.spectrum.id != 0xff) {
2120 ("Replaced existing measurement: %d\n",
2121 res->u.spectrum.id);
2122 priv->measurement_status &= ~MEASUREMENT_READY;
2124 priv->measurement_status |= MEASUREMENT_ACTIVE;
2128 case 1: /* Command will not be handled */
2133 dev_kfree_skb_any(cmd.meta.u.skb);
2139 static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
2140 struct iwl_tx_info *tx_sta)
2143 tx_sta->status.ack_signal = 0;
2144 tx_sta->status.excessive_retries = 0;
2147 ieee80211_tx_status_irqsafe(priv->hw,
2148 tx_sta->skb[0], &(tx_sta->status));
2150 ieee80211_tx_status(priv->hw,
2151 tx_sta->skb[0], &(tx_sta->status));
2153 tx_sta->skb[0] = NULL;
2157 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
2159 * When FW advances 'R' index, all entries between old and new 'R' index
2160 * need to be reclaimed. As result, some free space forms. If there is
2161 * enough free space (> low mark), wake the stack that feeds us.
2163 int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
2165 struct iwl_tx_queue *txq = &priv->txq[txq_id];
2166 struct iwl_queue *q = &txq->q;
2169 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
2170 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2171 "is out of range [0-%d] %d %d.\n", txq_id,
2172 index, q->n_bd, q->write_ptr, q->read_ptr);
2176 for (index = iwl_queue_inc_wrap(index, q->n_bd);
2177 q->read_ptr != index;
2178 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2179 if (txq_id != IWL_CMD_QUEUE_NUM) {
2180 iwl4965_txstatus_to_ieee(priv,
2181 &(txq->txb[txq->q.read_ptr]));
2182 iwl_hw_txq_free_tfd(priv, txq);
2183 } else if (nfreed > 1) {
2184 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
2185 q->write_ptr, q->read_ptr);
2186 queue_work(priv->workqueue, &priv->restart);
2194 static int iwl4965_is_tx_success(u32 status)
2196 status &= TX_STATUS_MSK;
2197 return (status == TX_STATUS_SUCCESS)
2198 || (status == TX_STATUS_DIRECT_DONE);
2201 /******************************************************************************
2203 * Generic RX handler implementations
2205 ******************************************************************************/
2206 #ifdef CONFIG_IWL4965_HT
2208 static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
2209 struct ieee80211_hdr *hdr)
2211 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2214 u8 *da = ieee80211_get_DA(hdr);
2215 return iwl_find_station(priv, da);
2219 static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
2220 struct iwl_priv *priv, int txq_id, int idx)
2222 if (priv->txq[txq_id].txb[idx].skb[0])
2223 return (struct ieee80211_hdr *)priv->txq[txq_id].
2224 txb[idx].skb[0]->data;
2228 static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
2230 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2231 tx_resp->frame_count);
2232 return le32_to_cpu(*scd_ssn) & MAX_SN;
2237 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2239 static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
2240 struct iwl_ht_agg *agg,
2241 struct iwl4965_tx_resp_agg *tx_resp,
2245 struct agg_tx_status *frame_status = &tx_resp->status;
2246 struct ieee80211_tx_status *tx_status = NULL;
2247 struct ieee80211_hdr *hdr = NULL;
2252 if (agg->wait_for_ba)
2253 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
2255 agg->frame_count = tx_resp->frame_count;
2256 agg->start_idx = start_idx;
2257 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
2260 /* # frames attempted by Tx command */
2261 if (agg->frame_count == 1) {
2262 /* Only one frame was attempted; no block-ack will arrive */
2263 status = le16_to_cpu(frame_status[0].status);
2264 seq = le16_to_cpu(frame_status[0].sequence);
2265 idx = SEQ_TO_INDEX(seq);
2266 txq_id = SEQ_TO_QUEUE(seq);
2268 /* FIXME: code repetition */
2269 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2270 agg->frame_count, agg->start_idx, idx);
2272 tx_status = &(priv->txq[txq_id].txb[idx].status);
2273 tx_status->retry_count = tx_resp->failure_frame;
2274 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
2275 tx_status->flags = iwl4965_is_tx_success(status)?
2276 IEEE80211_TX_STATUS_ACK : 0;
2277 iwl4965_hwrate_to_tx_control(priv,
2278 le32_to_cpu(tx_resp->rate_n_flags),
2279 &tx_status->control);
2280 /* FIXME: code repetition end */
2282 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2283 status & 0xff, tx_resp->failure_frame);
2284 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
2285 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
2287 agg->wait_for_ba = 0;
2289 /* Two or more frames were attempted; expect block-ack */
2291 int start = agg->start_idx;
2293 /* Construct bit-map of pending frames within Tx window */
2294 for (i = 0; i < agg->frame_count; i++) {
2296 status = le16_to_cpu(frame_status[i].status);
2297 seq = le16_to_cpu(frame_status[i].sequence);
2298 idx = SEQ_TO_INDEX(seq);
2299 txq_id = SEQ_TO_QUEUE(seq);
2301 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2302 AGG_TX_STATE_ABORT_MSK))
2305 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2306 agg->frame_count, txq_id, idx);
2308 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
2310 sc = le16_to_cpu(hdr->seq_ctrl);
2311 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2312 IWL_ERROR("BUG_ON idx doesn't match seq control"
2313 " idx=%d, seq_idx=%d, seq=%d\n",
2319 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2320 i, idx, SEQ_TO_SN(sc));
2324 sh = (start - idx) + 0xff;
2325 bitmap = bitmap << sh;
2328 } else if (sh < -64)
2329 sh = 0xff - (start - idx);
2333 bitmap = bitmap << sh;
2336 bitmap |= (1 << sh);
2337 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2338 start, (u32)(bitmap & 0xFFFFFFFF));
2341 agg->bitmap = bitmap;
2342 agg->start_idx = start;
2343 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
2344 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
2345 agg->frame_count, agg->start_idx,
2346 (unsigned long long)agg->bitmap);
2349 agg->wait_for_ba = 1;
2356 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2358 static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
2359 struct iwl_rx_mem_buffer *rxb)
2361 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2362 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2363 int txq_id = SEQ_TO_QUEUE(sequence);
2364 int index = SEQ_TO_INDEX(sequence);
2365 struct iwl_tx_queue *txq = &priv->txq[txq_id];
2366 struct ieee80211_tx_status *tx_status;
2367 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
2368 u32 status = le32_to_cpu(tx_resp->status);
2369 #ifdef CONFIG_IWL4965_HT
2370 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2372 struct ieee80211_hdr *hdr;
2376 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
2377 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2378 "is out of range [0-%d] %d %d\n", txq_id,
2379 index, txq->q.n_bd, txq->q.write_ptr,
2384 #ifdef CONFIG_IWL4965_HT
2385 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2386 fc = le16_to_cpu(hdr->frame_control);
2387 if (ieee80211_is_qos_data(fc)) {
2388 qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
2392 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2393 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2394 IWL_ERROR("Station not known\n");
2398 if (txq->sched_retry) {
2399 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
2400 struct iwl_ht_agg *agg = NULL;
2405 agg = &priv->stations[sta_id].tid[tid].agg;
2407 iwl4965_tx_status_reply_tx(priv, agg,
2408 (struct iwl4965_tx_resp_agg *)tx_resp, index);
2410 if ((tx_resp->frame_count == 1) &&
2411 !iwl4965_is_tx_success(status)) {
2412 /* TODO: send BAR */
2415 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
2417 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
2418 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2419 "%d index %d\n", scd_ssn , index);
2420 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2421 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2423 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
2424 txq_id >= 0 && priv->mac80211_registered &&
2425 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
2426 /* calculate mac80211 ampdu sw queue to wake */
2427 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
2429 if (agg->state == IWL_AGG_OFF)
2430 ieee80211_wake_queue(priv->hw, txq_id);
2432 ieee80211_wake_queue(priv->hw, ampdu_q);
2434 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
2437 #endif /* CONFIG_IWL4965_HT */
2438 tx_status = &(txq->txb[txq->q.read_ptr].status);
2440 tx_status->retry_count = tx_resp->failure_frame;
2442 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
2443 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2444 &tx_status->control);
2446 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
2447 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
2448 status, le32_to_cpu(tx_resp->rate_n_flags),
2449 tx_resp->failure_frame);
2451 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
2452 #ifdef CONFIG_IWL4965_HT
2454 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2455 if (tid != MAX_TID_COUNT)
2456 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2457 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
2458 (txq_id >= 0) && priv->mac80211_registered)
2459 ieee80211_wake_queue(priv->hw, txq_id);
2460 if (tid != MAX_TID_COUNT)
2461 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
2464 #endif /* CONFIG_IWL4965_HT */
2466 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2467 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2471 static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
2472 struct iwl_rx_mem_buffer *rxb)
2474 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2475 struct iwl4965_alive_resp *palive;
2476 struct delayed_work *pwork;
2478 palive = &pkt->u.alive_frame;
2480 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2482 palive->is_valid, palive->ver_type,
2483 palive->ver_subtype);
2485 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2486 IWL_DEBUG_INFO("Initialization Alive received.\n");
2487 memcpy(&priv->card_alive_init,
2488 &pkt->u.alive_frame,
2489 sizeof(struct iwl4965_init_alive_resp));
2490 pwork = &priv->init_alive_start;
2492 IWL_DEBUG_INFO("Runtime Alive received.\n");
2493 memcpy(&priv->card_alive, &pkt->u.alive_frame,
2494 sizeof(struct iwl4965_alive_resp));
2495 pwork = &priv->alive_start;
2498 /* We delay the ALIVE response by 5ms to
2499 * give the HW RF Kill time to activate... */
2500 if (palive->is_valid == UCODE_VALID_OK)
2501 queue_delayed_work(priv->workqueue, pwork,
2502 msecs_to_jiffies(5));
2504 IWL_WARNING("uCode did not respond OK.\n");
2507 static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
2508 struct iwl_rx_mem_buffer *rxb)
2510 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2512 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2516 static void iwl4965_rx_reply_error(struct iwl_priv *priv,
2517 struct iwl_rx_mem_buffer *rxb)
2519 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2521 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2522 "seq 0x%04X ser 0x%08X\n",
2523 le32_to_cpu(pkt->u.err_resp.error_type),
2524 get_cmd_string(pkt->u.err_resp.cmd_id),
2525 pkt->u.err_resp.cmd_id,
2526 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2527 le32_to_cpu(pkt->u.err_resp.error_info));
2530 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2532 static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
2534 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2535 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
2536 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
2537 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2538 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2539 rxon->channel = csa->channel;
2540 priv->staging_rxon.channel = csa->channel;
2543 static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
2544 struct iwl_rx_mem_buffer *rxb)
2546 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
2547 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2548 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
2550 if (!report->state) {
2551 IWL_DEBUG(IWL_DL_11H,
2552 "Spectrum Measure Notification: Start\n");
2556 memcpy(&priv->measure_report, report, sizeof(*report));
2557 priv->measurement_status |= MEASUREMENT_READY;
2561 static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
2562 struct iwl_rx_mem_buffer *rxb)
2564 #ifdef CONFIG_IWLWIFI_DEBUG
2565 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2566 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
2567 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2568 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2572 static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
2573 struct iwl_rx_mem_buffer *rxb)
2575 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2576 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2577 "notification for %s:\n",
2578 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
2579 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
2582 static void iwl4965_bg_beacon_update(struct work_struct *work)
2584 struct iwl_priv *priv =
2585 container_of(work, struct iwl_priv, beacon_update);
2586 struct sk_buff *beacon;
2588 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
2589 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
2592 IWL_ERROR("update beacon failed\n");
2596 mutex_lock(&priv->mutex);
2597 /* new beacon skb is allocated every time; dispose previous.*/
2598 if (priv->ibss_beacon)
2599 dev_kfree_skb(priv->ibss_beacon);
2601 priv->ibss_beacon = beacon;
2602 mutex_unlock(&priv->mutex);
2604 iwl4965_send_beacon_cmd(priv);
2607 static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
2608 struct iwl_rx_mem_buffer *rxb)
2610 #ifdef CONFIG_IWLWIFI_DEBUG
2611 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2612 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2613 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
2615 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2616 "tsf %d %d rate %d\n",
2617 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2618 beacon->beacon_notify_hdr.failure_frame,
2619 le32_to_cpu(beacon->ibss_mgr_status),
2620 le32_to_cpu(beacon->high_tsf),
2621 le32_to_cpu(beacon->low_tsf), rate);
2624 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2625 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2626 queue_work(priv->workqueue, &priv->beacon_update);
2629 /* Service response to REPLY_SCAN_CMD (0x80) */
2630 static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
2631 struct iwl_rx_mem_buffer *rxb)
2633 #ifdef CONFIG_IWLWIFI_DEBUG
2634 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2635 struct iwl4965_scanreq_notification *notif =
2636 (struct iwl4965_scanreq_notification *)pkt->u.raw;
2638 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2642 /* Service SCAN_START_NOTIFICATION (0x82) */
2643 static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
2644 struct iwl_rx_mem_buffer *rxb)
2646 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2647 struct iwl4965_scanstart_notification *notif =
2648 (struct iwl4965_scanstart_notification *)pkt->u.raw;
2649 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2650 IWL_DEBUG_SCAN("Scan start: "
2652 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2654 notif->band ? "bg" : "a",
2656 notif->tsf_low, notif->status, notif->beacon_timer);
2659 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
2660 static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
2661 struct iwl_rx_mem_buffer *rxb)
2663 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2664 struct iwl4965_scanresults_notification *notif =
2665 (struct iwl4965_scanresults_notification *)pkt->u.raw;
2667 IWL_DEBUG_SCAN("Scan ch.res: "
2669 "(TSF: 0x%08X:%08X) - %d "
2670 "elapsed=%lu usec (%dms since last)\n",
2672 notif->band ? "bg" : "a",
2673 le32_to_cpu(notif->tsf_high),
2674 le32_to_cpu(notif->tsf_low),
2675 le32_to_cpu(notif->statistics[0]),
2676 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2677 jiffies_to_msecs(elapsed_jiffies
2678 (priv->last_scan_jiffies, jiffies)));
2680 priv->last_scan_jiffies = jiffies;
2681 priv->next_scan_jiffies = 0;
2684 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
2685 static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
2686 struct iwl_rx_mem_buffer *rxb)
2688 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2689 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
2691 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2692 scan_notif->scanned_channels,
2693 scan_notif->tsf_low,
2694 scan_notif->tsf_high, scan_notif->status);
2696 /* The HW is no longer scanning */
2697 clear_bit(STATUS_SCAN_HW, &priv->status);
2699 /* The scan completion notification came in, so kill that timer... */
2700 cancel_delayed_work(&priv->scan_check);
2702 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
2703 (priv->scan_bands == 2) ? "2.4" : "5.2",
2704 jiffies_to_msecs(elapsed_jiffies
2705 (priv->scan_pass_start, jiffies)));
2707 /* Remove this scanned band from the list
2708 * of pending bands to scan */
2711 /* If a request to abort was given, or the scan did not succeed
2712 * then we reset the scan state machine and terminate,
2713 * re-queuing another scan if one has been requested */
2714 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2715 IWL_DEBUG_INFO("Aborted scan completed.\n");
2716 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2718 /* If there are more bands on this scan pass reschedule */
2719 if (priv->scan_bands > 0)
2723 priv->last_scan_jiffies = jiffies;
2724 priv->next_scan_jiffies = 0;
2725 IWL_DEBUG_INFO("Setting scan to off\n");
2727 clear_bit(STATUS_SCANNING, &priv->status);
2729 IWL_DEBUG_INFO("Scan took %dms\n",
2730 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2732 queue_work(priv->workqueue, &priv->scan_completed);
2737 priv->scan_pass_start = jiffies;
2738 queue_work(priv->workqueue, &priv->request_scan);
2741 /* Handle notification from uCode that card's power state is changing
2742 * due to software, hardware, or critical temperature RFKILL */
2743 static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
2744 struct iwl_rx_mem_buffer *rxb)
2746 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2747 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2748 unsigned long status = priv->status;
2750 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2751 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2752 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2754 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
2755 RF_CARD_DISABLED)) {
2757 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2758 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2760 if (!iwl_grab_nic_access(priv)) {
2762 priv, HBUS_TARG_MBX_C,
2763 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2765 iwl_release_nic_access(priv);
2768 if (!(flags & RXON_CARD_DISABLED)) {
2769 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
2770 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2771 if (!iwl_grab_nic_access(priv)) {
2773 priv, HBUS_TARG_MBX_C,
2774 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2776 iwl_release_nic_access(priv);
2780 if (flags & RF_CARD_DISABLED) {
2781 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2782 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
2783 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2784 if (!iwl_grab_nic_access(priv))
2785 iwl_release_nic_access(priv);
2789 if (flags & HW_CARD_DISABLED)
2790 set_bit(STATUS_RF_KILL_HW, &priv->status);
2792 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2795 if (flags & SW_CARD_DISABLED)
2796 set_bit(STATUS_RF_KILL_SW, &priv->status);
2798 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2800 if (!(flags & RXON_CARD_DISABLED))
2801 iwl4965_scan_cancel(priv);
2803 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2804 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2805 (test_bit(STATUS_RF_KILL_SW, &status) !=
2806 test_bit(STATUS_RF_KILL_SW, &priv->status)))
2807 queue_work(priv->workqueue, &priv->rf_kill);
2809 wake_up_interruptible(&priv->wait_command_queue);
2813 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
2815 * Setup the RX handlers for each of the reply types sent from the uCode
2818 * This function chains into the hardware specific files for them to setup
2819 * any hardware specific handlers as well.
2821 static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
2823 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
2824 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
2825 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
2826 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
2827 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
2828 iwl4965_rx_spectrum_measure_notif;
2829 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
2830 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
2831 iwl4965_rx_pm_debug_statistics_notif;
2832 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
2835 * The same handler is used for both the REPLY to a discrete
2836 * statistics request from the host as well as for the periodic
2837 * statistics notifications (after received beacons) from the uCode.
2839 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
2840 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
2842 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
2843 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
2844 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
2845 iwl4965_rx_scan_results_notif;
2846 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
2847 iwl4965_rx_scan_complete_notif;
2848 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
2849 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
2851 /* Set up hardware specific Rx handlers */
2852 priv->cfg->ops->lib->rx_handler_setup(priv);
2856 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
2857 * @rxb: Rx buffer to reclaim
2859 * If an Rx buffer has an async callback associated with it the callback
2860 * will be executed. The attached skb (if present) will only be freed
2861 * if the callback returns 1
2863 static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
2864 struct iwl_rx_mem_buffer *rxb)
2866 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2867 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2868 int txq_id = SEQ_TO_QUEUE(sequence);
2869 int index = SEQ_TO_INDEX(sequence);
2870 int huge = sequence & SEQ_HUGE_FRAME;
2872 struct iwl_cmd *cmd;
2874 /* If a Tx command is being handled and it isn't in the actual
2875 * command queue then there a command routing bug has been introduced
2876 * in the queue management code. */
2877 if (txq_id != IWL_CMD_QUEUE_NUM)
2878 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
2879 txq_id, pkt->hdr.cmd);
2880 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
2882 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
2883 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
2885 /* Input error checking is done when commands are added to queue. */
2886 if (cmd->meta.flags & CMD_WANT_SKB) {
2887 cmd->meta.source->u.skb = rxb->skb;
2889 } else if (cmd->meta.u.callback &&
2890 !cmd->meta.u.callback(priv, cmd, rxb->skb))
2893 iwl4965_tx_queue_reclaim(priv, txq_id, index);
2895 if (!(cmd->meta.flags & CMD_ASYNC)) {
2896 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2897 wake_up_interruptible(&priv->wait_command_queue);
2902 * this should be called while priv->lock is locked
2904 static void __iwl_rx_replenish(struct iwl_priv *priv)
2906 iwl_rx_allocate(priv);
2907 iwl_rx_queue_restock(priv);
2912 * iwl_rx_handle - Main entry function for receiving responses from uCode
2914 * Uses the priv->rx_handlers callback function array to invoke
2915 * the appropriate handlers, including command responses,
2916 * frame-received notifications, and other notifications.
2918 void iwl_rx_handle(struct iwl_priv *priv)
2920 struct iwl_rx_mem_buffer *rxb;
2921 struct iwl_rx_packet *pkt;
2922 struct iwl_rx_queue *rxq = &priv->rxq;
2925 unsigned long flags;
2929 /* uCode's read index (stored in shared DRAM) indicates the last Rx
2930 * buffer that the driver may process (last buffer filled by ucode). */
2931 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
2934 /* Rx interrupt, but nothing sent from uCode */
2936 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
2938 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
2942 rxb = rxq->queue[i];
2944 /* If an RXB doesn't have a Rx queue slot associated with it,
2945 * then a bug has been introduced in the queue refilling
2946 * routines -- catch it here */
2947 BUG_ON(rxb == NULL);
2949 rxq->queue[i] = NULL;
2951 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
2952 priv->hw_params.rx_buf_size,
2953 PCI_DMA_FROMDEVICE);
2954 pkt = (struct iwl_rx_packet *)rxb->skb->data;
2956 /* Reclaim a command buffer only if this packet is a response
2957 * to a (driver-originated) command.
2958 * If the packet (e.g. Rx frame) originated from uCode,
2959 * there is no command buffer to reclaim.
2960 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
2961 * but apparently a few don't get set; catch them here. */
2962 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
2963 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
2964 (pkt->hdr.cmd != REPLY_RX) &&
2965 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
2966 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
2967 (pkt->hdr.cmd != REPLY_TX);
2969 /* Based on type of command response or notification,
2970 * handle those that need handling via function in
2971 * rx_handlers table. See iwl4965_setup_rx_handlers() */
2972 if (priv->rx_handlers[pkt->hdr.cmd]) {
2973 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
2974 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
2975 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2977 /* No handling needed */
2978 IWL_DEBUG(IWL_DL_RX,
2979 "r %d i %d No handler needed for %s, 0x%02x\n",
2980 r, i, get_cmd_string(pkt->hdr.cmd),
2985 /* Invoke any callbacks, transfer the skb to caller, and
2986 * fire off the (possibly) blocking iwl_send_cmd()
2987 * as we reclaim the driver command queue */
2988 if (rxb && rxb->skb)
2989 iwl4965_tx_cmd_complete(priv, rxb);
2991 IWL_WARNING("Claim null rxb?\n");
2994 /* For now we just don't re-use anything. We can tweak this
2995 * later to try and re-use notification packets and SKBs that
2996 * fail to Rx correctly */
2997 if (rxb->skb != NULL) {
2998 priv->alloc_rxb_skb--;
2999 dev_kfree_skb_any(rxb->skb);
3003 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
3004 priv->hw_params.rx_buf_size,
3005 PCI_DMA_FROMDEVICE);
3006 spin_lock_irqsave(&rxq->lock, flags);
3007 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3008 spin_unlock_irqrestore(&rxq->lock, flags);
3009 i = (i + 1) & RX_QUEUE_MASK;
3010 /* If there are a lot of unused frames,
3011 * restock the Rx queue so ucode wont assert. */
3016 __iwl_rx_replenish(priv);
3022 /* Backtrack one entry */
3024 iwl_rx_queue_restock(priv);
3026 /* Convert linear signal-to-noise ratio into dB */
3027 static u8 ratio2dB[100] = {
3028 /* 0 1 2 3 4 5 6 7 8 9 */
3029 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3030 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3031 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3032 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3033 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3034 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3035 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3036 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3037 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3038 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3041 /* Calculates a relative dB value from a ratio of linear
3042 * (i.e. not dB) signal levels.
3043 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
3044 int iwl4965_calc_db_from_ratio(int sig_ratio)
3046 /* 1000:1 or higher just report as 60 dB */
3047 if (sig_ratio >= 1000)
3050 /* 100:1 or higher, divide by 10 and use table,
3051 * add 20 dB to make up for divide by 10 */
3052 if (sig_ratio >= 100)
3053 return (20 + (int)ratio2dB[sig_ratio/10]);
3055 /* We shouldn't see this */
3059 /* Use table for ratios 1:1 - 99:1 */
3060 return (int)ratio2dB[sig_ratio];
3063 #define PERFECT_RSSI (-20) /* dBm */
3064 #define WORST_RSSI (-95) /* dBm */
3065 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3067 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
3068 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3069 * about formulas used below. */
3070 int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
3073 int degradation = PERFECT_RSSI - rssi_dbm;
3075 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3076 * as indicator; formula is (signal dbm - noise dbm).
3077 * SNR at or above 40 is a great signal (100%).
3078 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3079 * Weakest usable signal is usually 10 - 15 dB SNR. */
3081 if (rssi_dbm - noise_dbm >= 40)
3083 else if (rssi_dbm < noise_dbm)
3085 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3087 /* Else use just the signal level.
3088 * This formula is a least squares fit of data points collected and
3089 * compared with a reference system that had a percentage (%) display
3090 * for signal quality. */
3092 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3093 (15 * RSSI_RANGE + 62 * degradation)) /
3094 (RSSI_RANGE * RSSI_RANGE);
3098 else if (sig_qual < 1)
3105 * iwl_txq_update_write_ptr - Send new write index to hardware
3107 static int iwl_txq_update_write_ptr(struct iwl_priv *priv,
3108 struct iwl_tx_queue *txq)
3112 int txq_id = txq->q.id;
3114 if (txq->need_update == 0)
3117 /* if we're trying to save power */
3118 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3119 /* wake up nic if it's powered down ...
3120 * uCode will wake up, and interrupt us again, so next
3121 * time we'll skip this part. */
3122 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
3124 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3125 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
3126 iwl_set_bit(priv, CSR_GP_CNTRL,
3127 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3131 /* restore this queue's parameters in nic hardware. */
3132 ret = iwl_grab_nic_access(priv);
3135 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
3136 txq->q.write_ptr | (txq_id << 8));
3137 iwl_release_nic_access(priv);
3139 /* else not in power-save mode, uCode will never sleep when we're
3140 * trying to tx (during RFKILL, we're not trying to tx). */
3142 iwl_write32(priv, HBUS_TARG_WRPTR,
3143 txq->q.write_ptr | (txq_id << 8));
3145 txq->need_update = 0;
3150 #ifdef CONFIG_IWLWIFI_DEBUG
3151 static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
3153 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
3154 DECLARE_MAC_BUF(mac);
3156 IWL_DEBUG_RADIO("RX CONFIG:\n");
3157 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
3158 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3159 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3160 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3161 le32_to_cpu(rxon->filter_flags));
3162 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3163 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3164 rxon->ofdm_basic_rates);
3165 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
3166 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3167 print_mac(mac, rxon->node_addr));
3168 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3169 print_mac(mac, rxon->bssid_addr));
3170 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3174 static void iwl4965_enable_interrupts(struct iwl_priv *priv)
3176 IWL_DEBUG_ISR("Enabling interrupts\n");
3177 set_bit(STATUS_INT_ENABLED, &priv->status);
3178 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
3181 /* call this function to flush any scheduled tasklet */
3182 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3184 /* wait to make sure we flush pedding tasklet*/
3185 synchronize_irq(priv->pci_dev->irq);
3186 tasklet_kill(&priv->irq_tasklet);
3189 static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
3191 clear_bit(STATUS_INT_ENABLED, &priv->status);
3193 /* disable interrupts from uCode/NIC to host */
3194 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
3196 /* acknowledge/clear/reset any interrupts still pending
3197 * from uCode or flow handler (Rx/Tx DMA) */
3198 iwl_write32(priv, CSR_INT, 0xffffffff);
3199 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
3200 IWL_DEBUG_ISR("Disabled interrupts\n");
3203 static const char *desc_lookup(int i)
3211 return "BAD_CHECKSUM";
3213 return "NMI_INTERRUPT";
3217 return "FATAL_ERROR";
3223 #define ERROR_START_OFFSET (1 * sizeof(u32))
3224 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
3226 static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
3229 u32 desc, time, count, base, data1;
3230 u32 blink1, blink2, ilink1, ilink2;
3233 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3235 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
3236 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3240 rc = iwl_grab_nic_access(priv);
3242 IWL_WARNING("Can not read from adapter at this time.\n");
3246 count = iwl_read_targ_mem(priv, base);
3248 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3249 IWL_ERROR("Start IWL Error Log Dump:\n");
3250 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
3253 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3254 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3255 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3256 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3257 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3258 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3259 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3260 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3261 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
3263 IWL_ERROR("Desc Time "
3264 "data1 data2 line\n");
3265 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3266 desc_lookup(desc), desc, time, data1, data2, line);
3267 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3268 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3271 iwl_release_nic_access(priv);
3274 #define EVENT_START_OFFSET (4 * sizeof(u32))
3277 * iwl4965_print_event_log - Dump error event log to syslog
3279 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
3281 static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
3282 u32 num_events, u32 mode)
3285 u32 base; /* SRAM byte address of event log header */
3286 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3287 u32 ptr; /* SRAM byte address of log data */
3288 u32 ev, time, data; /* event log data */
3290 if (num_events == 0)
3293 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3296 event_size = 2 * sizeof(u32);
3298 event_size = 3 * sizeof(u32);
3300 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3302 /* "time" is actually "data" for mode 0 (no timestamp).
3303 * place event id # at far right for easier visual parsing. */
3304 for (i = 0; i < num_events; i++) {
3305 ev = iwl_read_targ_mem(priv, ptr);
3307 time = iwl_read_targ_mem(priv, ptr);
3310 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
3312 data = iwl_read_targ_mem(priv, ptr);
3314 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
3319 static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
3322 u32 base; /* SRAM byte address of event log header */
3323 u32 capacity; /* event log capacity in # entries */
3324 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3325 u32 num_wraps; /* # times uCode wrapped to top of log */
3326 u32 next_entry; /* index of next entry to be written by uCode */
3327 u32 size; /* # entries that we'll print */
3329 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3330 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
3331 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
3335 rc = iwl_grab_nic_access(priv);
3337 IWL_WARNING("Can not read from adapter at this time.\n");
3341 /* event log header */
3342 capacity = iwl_read_targ_mem(priv, base);
3343 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3344 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3345 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
3347 size = num_wraps ? capacity : next_entry;
3349 /* bail out if nothing in log */
3351 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
3352 iwl_release_nic_access(priv);
3356 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
3359 /* if uCode has wrapped back to top of log, start at the oldest entry,
3360 * i.e the next one that uCode would fill. */
3362 iwl4965_print_event_log(priv, next_entry,
3363 capacity - next_entry, mode);
3365 /* (then/else) start at top of log */
3366 iwl4965_print_event_log(priv, 0, next_entry, mode);
3368 iwl_release_nic_access(priv);
3372 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
3374 static void iwl4965_irq_handle_error(struct iwl_priv *priv)
3376 /* Set the FW error flag -- cleared on iwl4965_down */
3377 set_bit(STATUS_FW_ERROR, &priv->status);
3379 /* Cancel currently queued command. */
3380 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3382 #ifdef CONFIG_IWLWIFI_DEBUG
3383 if (priv->debug_level & IWL_DL_FW_ERRORS) {
3384 iwl4965_dump_nic_error_log(priv);
3385 iwl4965_dump_nic_event_log(priv);
3386 iwl4965_print_rx_config_cmd(priv);
3390 wake_up_interruptible(&priv->wait_command_queue);
3392 /* Keep the restart process from trying to send host
3393 * commands by clearing the INIT status bit */
3394 clear_bit(STATUS_READY, &priv->status);
3396 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3397 IWL_DEBUG(IWL_DL_FW_ERRORS,
3398 "Restarting adapter due to uCode error.\n");
3400 if (iwl_is_associated(priv)) {
3401 memcpy(&priv->recovery_rxon, &priv->active_rxon,
3402 sizeof(priv->recovery_rxon));
3403 priv->error_recovering = 1;
3405 if (priv->cfg->mod_params->restart_fw)
3406 queue_work(priv->workqueue, &priv->restart);
3410 static void iwl4965_error_recovery(struct iwl_priv *priv)
3412 unsigned long flags;
3414 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
3415 sizeof(priv->staging_rxon));
3416 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3417 iwl4965_commit_rxon(priv);
3419 iwl_rxon_add_station(priv, priv->bssid, 1);
3421 spin_lock_irqsave(&priv->lock, flags);
3422 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
3423 priv->error_recovering = 0;
3424 spin_unlock_irqrestore(&priv->lock, flags);
3427 static void iwl4965_irq_tasklet(struct iwl_priv *priv)
3429 u32 inta, handled = 0;
3431 unsigned long flags;
3432 #ifdef CONFIG_IWLWIFI_DEBUG
3436 spin_lock_irqsave(&priv->lock, flags);
3438 /* Ack/clear/reset pending uCode interrupts.
3439 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3440 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
3441 inta = iwl_read32(priv, CSR_INT);
3442 iwl_write32(priv, CSR_INT, inta);
3444 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3445 * Any new interrupts that happen after this, either while we're
3446 * in this tasklet, or later, will show up in next ISR/tasklet. */
3447 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3448 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
3450 #ifdef CONFIG_IWLWIFI_DEBUG
3451 if (priv->debug_level & IWL_DL_ISR) {
3452 /* just for debug */
3453 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3454 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3455 inta, inta_mask, inta_fh);
3459 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3460 * atomic, make sure that inta covers all the interrupts that
3461 * we've discovered, even if FH interrupt came in just after
3462 * reading CSR_INT. */
3463 if (inta_fh & CSR49_FH_INT_RX_MASK)
3464 inta |= CSR_INT_BIT_FH_RX;
3465 if (inta_fh & CSR49_FH_INT_TX_MASK)
3466 inta |= CSR_INT_BIT_FH_TX;
3468 /* Now service all interrupt bits discovered above. */
3469 if (inta & CSR_INT_BIT_HW_ERR) {
3470 IWL_ERROR("Microcode HW error detected. Restarting.\n");
3472 /* Tell the device to stop sending interrupts */
3473 iwl4965_disable_interrupts(priv);
3475 iwl4965_irq_handle_error(priv);
3477 handled |= CSR_INT_BIT_HW_ERR;
3479 spin_unlock_irqrestore(&priv->lock, flags);
3484 #ifdef CONFIG_IWLWIFI_DEBUG
3485 if (priv->debug_level & (IWL_DL_ISR)) {
3486 /* NIC fires this, but we don't use it, redundant with WAKEUP */
3487 if (inta & CSR_INT_BIT_SCD)
3488 IWL_DEBUG_ISR("Scheduler finished to transmit "
3489 "the frame/frames.\n");
3491 /* Alive notification via Rx interrupt will do the real work */
3492 if (inta & CSR_INT_BIT_ALIVE)
3493 IWL_DEBUG_ISR("Alive interrupt\n");
3496 /* Safely ignore these bits for debug checks below */
3497 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
3499 /* HW RF KILL switch toggled */
3500 if (inta & CSR_INT_BIT_RF_KILL) {
3502 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
3503 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
3506 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
3507 hw_rf_kill ? "disable radio":"enable radio");
3509 /* Queue restart only if RF_KILL switch was set to "kill"
3510 * when we loaded driver, and is now set to "enable".
3511 * After we're Alive, RF_KILL gets handled by
3512 * iwl4965_rx_card_state_notif() */
3513 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
3514 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3515 queue_work(priv->workqueue, &priv->restart);
3518 handled |= CSR_INT_BIT_RF_KILL;
3521 /* Chip got too hot and stopped itself */
3522 if (inta & CSR_INT_BIT_CT_KILL) {
3523 IWL_ERROR("Microcode CT kill error detected.\n");
3524 handled |= CSR_INT_BIT_CT_KILL;
3527 /* Error detected by uCode */
3528 if (inta & CSR_INT_BIT_SW_ERR) {
3529 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
3531 iwl4965_irq_handle_error(priv);
3532 handled |= CSR_INT_BIT_SW_ERR;
3535 /* uCode wakes up after power-down sleep */
3536 if (inta & CSR_INT_BIT_WAKEUP) {
3537 IWL_DEBUG_ISR("Wakeup interrupt\n");
3538 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
3539 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
3540 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
3541 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
3542 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
3543 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
3544 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
3546 handled |= CSR_INT_BIT_WAKEUP;
3549 /* All uCode command responses, including Tx command responses,
3550 * Rx "responses" (frame-received notification), and other
3551 * notifications from uCode come through here*/
3552 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
3553 iwl_rx_handle(priv);
3554 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3557 if (inta & CSR_INT_BIT_FH_TX) {
3558 IWL_DEBUG_ISR("Tx interrupt\n");
3559 handled |= CSR_INT_BIT_FH_TX;
3562 if (inta & ~handled)
3563 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
3565 if (inta & ~CSR_INI_SET_MASK) {
3566 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
3567 inta & ~CSR_INI_SET_MASK);
3568 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
3571 /* Re-enable all interrupts */
3572 /* only Re-enable if diabled by irq */
3573 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3574 iwl4965_enable_interrupts(priv);
3576 #ifdef CONFIG_IWLWIFI_DEBUG
3577 if (priv->debug_level & (IWL_DL_ISR)) {
3578 inta = iwl_read32(priv, CSR_INT);
3579 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3580 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3581 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3582 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3585 spin_unlock_irqrestore(&priv->lock, flags);
3588 static irqreturn_t iwl4965_isr(int irq, void *data)
3590 struct iwl_priv *priv = data;
3591 u32 inta, inta_mask;
3596 spin_lock(&priv->lock);
3598 /* Disable (but don't clear!) interrupts here to avoid
3599 * back-to-back ISRs and sporadic interrupts from our NIC.
3600 * If we have something to service, the tasklet will re-enable ints.
3601 * If we *don't* have something, we'll re-enable before leaving here. */
3602 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3603 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
3605 /* Discover which interrupts are active/pending */
3606 inta = iwl_read32(priv, CSR_INT);
3607 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3609 /* Ignore interrupt if there's nothing in NIC to service.
3610 * This may be due to IRQ shared with another device,
3611 * or due to sporadic interrupts thrown from our NIC. */
3612 if (!inta && !inta_fh) {
3613 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3617 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
3618 /* Hardware disappeared. It might have already raised
3620 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
3624 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3625 inta, inta_mask, inta_fh);
3627 inta &= ~CSR_INT_BIT_SCD;
3629 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
3630 if (likely(inta || inta_fh))
3631 tasklet_schedule(&priv->irq_tasklet);
3634 spin_unlock(&priv->lock);
3638 /* re-enable interrupts here since we don't have anything to service. */
3639 /* only Re-enable if diabled by irq */
3640 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3641 iwl4965_enable_interrupts(priv);
3642 spin_unlock(&priv->lock);
3646 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3647 * sending probe req. This should be set long enough to hear probe responses
3648 * from more than one AP. */
3649 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
3650 #define IWL_ACTIVE_DWELL_TIME_52 (10)
3652 /* For faster active scanning, scan will move to the next channel if fewer than
3653 * PLCP_QUIET_THRESH packets are heard on this channel within
3654 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3655 * time if it's a quiet channel (nothing responded to our probe, and there's
3656 * no other traffic).
3657 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3658 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
3659 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
3661 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3662 * Must be set longer than active dwell time.
3663 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
3664 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
3665 #define IWL_PASSIVE_DWELL_TIME_52 (10)
3666 #define IWL_PASSIVE_DWELL_BASE (100)
3667 #define IWL_CHANNEL_TUNE_TIME 5
3669 static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
3670 enum ieee80211_band band)
3672 if (band == IEEE80211_BAND_5GHZ)
3673 return IWL_ACTIVE_DWELL_TIME_52;
3675 return IWL_ACTIVE_DWELL_TIME_24;
3678 static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
3679 enum ieee80211_band band)
3681 u16 active = iwl4965_get_active_dwell_time(priv, band);
3682 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
3683 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
3684 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
3686 if (iwl_is_associated(priv)) {
3687 /* If we're associated, we clamp the maximum passive
3688 * dwell time to be 98% of the beacon interval (minus
3689 * 2 * channel tune time) */
3690 passive = priv->beacon_int;
3691 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
3692 passive = IWL_PASSIVE_DWELL_BASE;
3693 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
3696 if (passive <= active)
3697 passive = active + 1;
3702 static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
3703 enum ieee80211_band band,
3704 u8 is_active, u8 direct_mask,
3705 struct iwl4965_scan_channel *scan_ch)
3707 const struct ieee80211_channel *channels = NULL;
3708 const struct ieee80211_supported_band *sband;
3709 const struct iwl_channel_info *ch_info;
3710 u16 passive_dwell = 0;
3711 u16 active_dwell = 0;
3714 sband = iwl_get_hw_mode(priv, band);
3718 channels = sband->channels;
3720 active_dwell = iwl4965_get_active_dwell_time(priv, band);
3721 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
3723 for (i = 0, added = 0; i < sband->n_channels; i++) {
3724 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3727 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
3729 ch_info = iwl_get_channel_info(priv, band,
3731 if (!is_channel_valid(ch_info)) {
3732 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
3737 if (!is_active || is_channel_passive(ch_info) ||
3738 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
3739 scan_ch->type = 0; /* passive */
3741 scan_ch->type = 1; /* active */
3743 if (scan_ch->type & 1)
3744 scan_ch->type |= (direct_mask << 1);
3746 scan_ch->active_dwell = cpu_to_le16(active_dwell);
3747 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
3749 /* Set txpower levels to defaults */
3750 scan_ch->tpc.dsp_atten = 110;
3751 /* scan_pwr_info->tpc.dsp_atten; */
3753 /*scan_pwr_info->tpc.tx_gain; */
3754 if (band == IEEE80211_BAND_5GHZ)
3755 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
3757 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
3758 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
3760 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
3764 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
3766 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
3767 (scan_ch->type & 1) ?
3768 active_dwell : passive_dwell);
3774 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
3778 /******************************************************************************
3780 * uCode download functions
3782 ******************************************************************************/
3784 static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
3786 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
3787 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
3788 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3789 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
3790 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3791 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
3794 static void iwl4965_nic_start(struct iwl_priv *priv)
3796 /* Remove all resets to allow NIC to operate */
3797 iwl_write32(priv, CSR_RESET, 0);
3802 * iwl4965_read_ucode - Read uCode images from disk file.
3804 * Copy into buffers for card to fetch via bus-mastering
3806 static int iwl4965_read_ucode(struct iwl_priv *priv)
3808 struct iwl4965_ucode *ucode;
3810 const struct firmware *ucode_raw;
3811 const char *name = priv->cfg->fw_name;
3814 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
3816 /* Ask kernel firmware_class module to get the boot firmware off disk.
3817 * request_firmware() is synchronous, file is in memory on return. */
3818 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
3820 IWL_ERROR("%s firmware file req failed: Reason %d\n",
3825 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3826 name, ucode_raw->size);
3828 /* Make sure that we got at least our header! */
3829 if (ucode_raw->size < sizeof(*ucode)) {
3830 IWL_ERROR("File size way too small!\n");
3835 /* Data from ucode file: header followed by uCode images */
3836 ucode = (void *)ucode_raw->data;
3838 ver = le32_to_cpu(ucode->ver);
3839 inst_size = le32_to_cpu(ucode->inst_size);
3840 data_size = le32_to_cpu(ucode->data_size);
3841 init_size = le32_to_cpu(ucode->init_size);
3842 init_data_size = le32_to_cpu(ucode->init_data_size);
3843 boot_size = le32_to_cpu(ucode->boot_size);
3845 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
3846 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
3848 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
3850 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
3852 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
3854 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
3857 /* Verify size of file vs. image size info in file's header */
3858 if (ucode_raw->size < sizeof(*ucode) +
3859 inst_size + data_size + init_size +
3860 init_data_size + boot_size) {
3862 IWL_DEBUG_INFO("uCode file size %d too small\n",
3863 (int)ucode_raw->size);
3868 /* Verify that uCode images will fit in card's SRAM */
3869 if (inst_size > priv->hw_params.max_inst_size) {
3870 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
3876 if (data_size > priv->hw_params.max_data_size) {
3877 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
3882 if (init_size > priv->hw_params.max_inst_size) {
3884 ("uCode init instr len %d too large to fit in\n",
3889 if (init_data_size > priv->hw_params.max_data_size) {
3891 ("uCode init data len %d too large to fit in\n",
3896 if (boot_size > priv->hw_params.max_bsm_size) {
3898 ("uCode boot instr len %d too large to fit in\n",
3904 /* Allocate ucode buffers for card's bus-master loading ... */
3906 /* Runtime instructions and 2 copies of data:
3907 * 1) unmodified from disk
3908 * 2) backup cache for save/restore during power-downs */
3909 priv->ucode_code.len = inst_size;
3910 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
3912 priv->ucode_data.len = data_size;
3913 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
3915 priv->ucode_data_backup.len = data_size;
3916 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3918 /* Initialization instructions and data */
3919 if (init_size && init_data_size) {
3920 priv->ucode_init.len = init_size;
3921 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
3923 priv->ucode_init_data.len = init_data_size;
3924 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3926 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
3930 /* Bootstrap (instructions only, no data) */
3932 priv->ucode_boot.len = boot_size;
3933 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
3935 if (!priv->ucode_boot.v_addr)
3939 /* Copy images into buffers for card's bus-master reads ... */
3941 /* Runtime instructions (first block of data in file) */
3942 src = &ucode->data[0];
3943 len = priv->ucode_code.len;
3944 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
3945 memcpy(priv->ucode_code.v_addr, src, len);
3946 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
3947 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
3949 /* Runtime data (2nd block)
3950 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
3951 src = &ucode->data[inst_size];
3952 len = priv->ucode_data.len;
3953 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
3954 memcpy(priv->ucode_data.v_addr, src, len);
3955 memcpy(priv->ucode_data_backup.v_addr, src, len);
3957 /* Initialization instructions (3rd block) */
3959 src = &ucode->data[inst_size + data_size];
3960 len = priv->ucode_init.len;
3961 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
3963 memcpy(priv->ucode_init.v_addr, src, len);
3966 /* Initialization data (4th block) */
3967 if (init_data_size) {
3968 src = &ucode->data[inst_size + data_size + init_size];
3969 len = priv->ucode_init_data.len;
3970 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
3972 memcpy(priv->ucode_init_data.v_addr, src, len);
3975 /* Bootstrap instructions (5th block) */
3976 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
3977 len = priv->ucode_boot.len;
3978 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
3979 memcpy(priv->ucode_boot.v_addr, src, len);
3981 /* We have our copies now, allow OS release its copies */
3982 release_firmware(ucode_raw);
3986 IWL_ERROR("failed to allocate pci memory\n");
3988 iwl4965_dealloc_ucode_pci(priv);
3991 release_firmware(ucode_raw);
3998 * iwl4965_alive_start - called after REPLY_ALIVE notification received
3999 * from protocol/runtime uCode (initialization uCode's
4000 * Alive gets handled by iwl4965_init_alive_start()).
4002 static void iwl4965_alive_start(struct iwl_priv *priv)
4006 IWL_DEBUG_INFO("Runtime Alive received.\n");
4008 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4009 /* We had an error bringing up the hardware, so take it
4010 * all the way back down so we can try again */
4011 IWL_DEBUG_INFO("Alive failed.\n");
4015 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4016 * This is a paranoid check, because we would not have gotten the
4017 * "runtime" alive if code weren't properly loaded. */
4018 if (iwl_verify_ucode(priv)) {
4019 /* Runtime instruction load was bad;
4020 * take it all the way back down so we can try again */
4021 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4025 iwlcore_clear_stations_table(priv);
4027 ret = priv->cfg->ops->lib->alive_notify(priv);
4029 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
4034 /* After the ALIVE response, we can send host commands to 4965 uCode */
4035 set_bit(STATUS_ALIVE, &priv->status);
4037 /* Clear out the uCode error bit if it is set */
4038 clear_bit(STATUS_FW_ERROR, &priv->status);
4040 if (iwl_is_rfkill(priv))
4043 ieee80211_start_queues(priv->hw);
4045 priv->active_rate = priv->rates_mask;
4046 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
4048 if (iwl_is_associated(priv)) {
4049 struct iwl_rxon_cmd *active_rxon =
4050 (struct iwl_rxon_cmd *)&priv->active_rxon;
4052 memcpy(&priv->staging_rxon, &priv->active_rxon,
4053 sizeof(priv->staging_rxon));
4054 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4056 /* Initialize our rx_config data */
4057 iwl4965_connection_init_rx_config(priv);
4058 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
4061 /* Configure Bluetooth device coexistence support */
4062 iwl4965_send_bt_config(priv);
4064 /* Configure the adapter for unassociated operation */
4065 iwl4965_commit_rxon(priv);
4067 /* At this point, the NIC is initialized and operational */
4068 priv->notif_missed_beacons = 0;
4070 iwl4965_rf_kill_ct_config(priv);
4072 iwl_leds_register(priv);
4074 IWL_DEBUG_INFO("ALIVE processing complete.\n");
4075 set_bit(STATUS_READY, &priv->status);
4076 wake_up_interruptible(&priv->wait_command_queue);
4078 if (priv->error_recovering)
4079 iwl4965_error_recovery(priv);
4081 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
4082 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
4086 queue_work(priv->workqueue, &priv->restart);
4089 static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
4091 static void __iwl4965_down(struct iwl_priv *priv)
4093 unsigned long flags;
4094 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4095 struct ieee80211_conf *conf = NULL;
4097 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4099 conf = ieee80211_get_hw_conf(priv->hw);
4102 set_bit(STATUS_EXIT_PENDING, &priv->status);
4104 iwl_leds_unregister(priv);
4106 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
4108 iwlcore_clear_stations_table(priv);
4110 /* Unblock any waiting calls */
4111 wake_up_interruptible_all(&priv->wait_command_queue);
4113 /* Wipe out the EXIT_PENDING status bit if we are not actually
4114 * exiting the module */
4116 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4118 /* stop and reset the on-board processor */
4119 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4121 /* tell the device to stop sending interrupts */
4122 spin_lock_irqsave(&priv->lock, flags);
4123 iwl4965_disable_interrupts(priv);
4124 spin_unlock_irqrestore(&priv->lock, flags);
4125 iwl_synchronize_irq(priv);
4127 if (priv->mac80211_registered)
4128 ieee80211_stop_queues(priv->hw);
4130 /* If we have not previously called iwl4965_init() then
4131 * clear all bits but the RF Kill and SUSPEND bits and return */
4132 if (!iwl_is_init(priv)) {
4133 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4135 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4137 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4138 STATUS_GEO_CONFIGURED |
4139 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4144 /* ...otherwise clear out all the status bits but the RF Kill and
4145 * SUSPEND bits and continue taking the NIC down. */
4146 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4148 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4150 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4151 STATUS_GEO_CONFIGURED |
4152 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4154 test_bit(STATUS_FW_ERROR, &priv->status) <<
4157 spin_lock_irqsave(&priv->lock, flags);
4158 iwl_clear_bit(priv, CSR_GP_CNTRL,
4159 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4160 spin_unlock_irqrestore(&priv->lock, flags);
4162 iwl4965_hw_txq_ctx_stop(priv);
4163 iwl4965_hw_rxq_stop(priv);
4165 spin_lock_irqsave(&priv->lock, flags);
4166 if (!iwl_grab_nic_access(priv)) {
4167 iwl_write_prph(priv, APMG_CLK_DIS_REG,
4168 APMG_CLK_VAL_DMA_CLK_RQT);
4169 iwl_release_nic_access(priv);
4171 spin_unlock_irqrestore(&priv->lock, flags);
4175 iwl4965_hw_nic_stop_master(priv);
4176 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
4177 iwl4965_hw_nic_reset(priv);
4178 priv->cfg->ops->lib->free_shared_mem(priv);
4181 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
4183 if (priv->ibss_beacon)
4184 dev_kfree_skb(priv->ibss_beacon);
4185 priv->ibss_beacon = NULL;
4187 /* clear out any free frames */
4188 iwl_clear_free_frames(priv);
4191 static void iwl4965_down(struct iwl_priv *priv)
4193 mutex_lock(&priv->mutex);
4194 __iwl4965_down(priv);
4195 mutex_unlock(&priv->mutex);
4197 iwl4965_cancel_deferred_work(priv);
4200 #define MAX_HW_RESTARTS 5
4202 static int __iwl4965_up(struct iwl_priv *priv)
4207 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4208 IWL_WARNING("Exit pending; will not bring the NIC up\n");
4212 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
4213 IWL_WARNING("Radio disabled by SW RF kill (module "
4215 iwl_rfkill_set_hw_state(priv);
4219 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
4220 IWL_ERROR("ucode not available for device bringup\n");
4224 /* If platform's RF_KILL switch is NOT set to KILL */
4225 if (iwl_read32(priv, CSR_GP_CNTRL) &
4226 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4227 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4229 set_bit(STATUS_RF_KILL_HW, &priv->status);
4230 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
4231 iwl_rfkill_set_hw_state(priv);
4232 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
4237 iwl_rfkill_set_hw_state(priv);
4238 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
4240 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
4242 IWL_ERROR("Unable to allocate shared memory\n");
4246 ret = iwl_hw_nic_init(priv);
4248 IWL_ERROR("Unable to init nic\n");
4252 /* make sure rfkill handshake bits are cleared */
4253 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4254 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
4255 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4257 /* clear (again), then enable host interrupts */
4258 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
4259 iwl4965_enable_interrupts(priv);
4261 /* really make sure rfkill handshake bits are cleared */
4262 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4263 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4265 /* Copy original ucode data image from disk into backup cache.
4266 * This will be used to initialize the on-board processor's
4267 * data SRAM for a clean start when the runtime program first loads. */
4268 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
4269 priv->ucode_data.len);
4271 /* We return success when we resume from suspend and rf_kill is on. */
4272 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
4275 for (i = 0; i < MAX_HW_RESTARTS; i++) {
4277 iwlcore_clear_stations_table(priv);
4279 /* load bootstrap state machine,
4280 * load bootstrap program into processor's memory,
4281 * prepare to load the "initialize" uCode */
4282 ret = priv->cfg->ops->lib->load_ucode(priv);
4285 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
4289 /* start card; "initialize" will load runtime ucode */
4290 iwl4965_nic_start(priv);
4292 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
4297 set_bit(STATUS_EXIT_PENDING, &priv->status);
4298 __iwl4965_down(priv);
4300 /* tried to restart and config the device for as long as our
4301 * patience could withstand */
4302 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
4307 /*****************************************************************************
4309 * Workqueue callbacks
4311 *****************************************************************************/
4313 static void iwl4965_bg_init_alive_start(struct work_struct *data)
4315 struct iwl_priv *priv =
4316 container_of(data, struct iwl_priv, init_alive_start.work);
4318 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4321 mutex_lock(&priv->mutex);
4322 priv->cfg->ops->lib->init_alive_start(priv);
4323 mutex_unlock(&priv->mutex);
4326 static void iwl4965_bg_alive_start(struct work_struct *data)
4328 struct iwl_priv *priv =
4329 container_of(data, struct iwl_priv, alive_start.work);
4331 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4334 mutex_lock(&priv->mutex);
4335 iwl4965_alive_start(priv);
4336 mutex_unlock(&priv->mutex);
4339 static void iwl4965_bg_rf_kill(struct work_struct *work)
4341 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
4343 wake_up_interruptible(&priv->wait_command_queue);
4345 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4348 mutex_lock(&priv->mutex);
4350 if (!iwl_is_rfkill(priv)) {
4351 IWL_DEBUG(IWL_DL_RF_KILL,
4352 "HW and/or SW RF Kill no longer active, restarting "
4354 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4355 queue_work(priv->workqueue, &priv->restart);
4357 /* make sure mac80211 stop sending Tx frame */
4358 if (priv->mac80211_registered)
4359 ieee80211_stop_queues(priv->hw);
4361 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
4362 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
4363 "disabled by SW switch\n");
4365 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
4366 "Kill switch must be turned off for "
4367 "wireless networking to work.\n");
4369 iwl_rfkill_set_hw_state(priv);
4371 mutex_unlock(&priv->mutex);
4374 static void iwl4965_bg_set_monitor(struct work_struct *work)
4376 struct iwl_priv *priv = container_of(work,
4377 struct iwl_priv, set_monitor);
4379 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
4381 mutex_lock(&priv->mutex);
4383 if (!iwl_is_ready(priv))
4384 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
4386 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
4387 IWL_ERROR("iwl4965_set_mode() failed\n");
4389 mutex_unlock(&priv->mutex);
4392 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
4394 static void iwl4965_bg_scan_check(struct work_struct *data)
4396 struct iwl_priv *priv =
4397 container_of(data, struct iwl_priv, scan_check.work);
4399 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4402 mutex_lock(&priv->mutex);
4403 if (test_bit(STATUS_SCANNING, &priv->status) ||
4404 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4405 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
4407 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
4409 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4410 iwl4965_send_scan_abort(priv);
4412 mutex_unlock(&priv->mutex);
4415 static void iwl4965_bg_request_scan(struct work_struct *data)
4417 struct iwl_priv *priv =
4418 container_of(data, struct iwl_priv, request_scan);
4419 struct iwl_host_cmd cmd = {
4420 .id = REPLY_SCAN_CMD,
4421 .len = sizeof(struct iwl4965_scan_cmd),
4422 .meta.flags = CMD_SIZE_HUGE,
4424 struct iwl4965_scan_cmd *scan;
4425 struct ieee80211_conf *conf = NULL;
4427 enum ieee80211_band band;
4431 conf = ieee80211_get_hw_conf(priv->hw);
4433 mutex_lock(&priv->mutex);
4435 if (!iwl_is_ready(priv)) {
4436 IWL_WARNING("request scan called when driver not ready.\n");
4440 /* Make sure the scan wasn't cancelled before this queued work
4441 * was given the chance to run... */
4442 if (!test_bit(STATUS_SCANNING, &priv->status))
4445 /* This should never be called or scheduled if there is currently
4446 * a scan active in the hardware. */
4447 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
4448 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
4449 "Ignoring second request.\n");
4454 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4455 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4459 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4460 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
4464 if (iwl_is_rfkill(priv)) {
4465 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4469 if (!test_bit(STATUS_READY, &priv->status)) {
4470 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
4474 if (!priv->scan_bands) {
4475 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
4480 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
4481 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
4488 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
4490 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
4491 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
4493 if (iwl_is_associated(priv)) {
4496 u32 suspend_time = 100;
4497 u32 scan_suspend_time = 100;
4498 unsigned long flags;
4500 IWL_DEBUG_INFO("Scanning while associated...\n");
4502 spin_lock_irqsave(&priv->lock, flags);
4503 interval = priv->beacon_int;
4504 spin_unlock_irqrestore(&priv->lock, flags);
4506 scan->suspend_time = 0;
4507 scan->max_out_time = cpu_to_le32(200 * 1024);
4509 interval = suspend_time;
4511 extra = (suspend_time / interval) << 22;
4512 scan_suspend_time = (extra |
4513 ((suspend_time % interval) * 1024));
4514 scan->suspend_time = cpu_to_le32(scan_suspend_time);
4515 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
4516 scan_suspend_time, interval);
4519 /* We should add the ability for user to lock to PASSIVE ONLY */
4520 if (priv->one_direct_scan) {
4522 ("Kicking off one direct scan for '%s'\n",
4523 iwl4965_escape_essid(priv->direct_ssid,
4524 priv->direct_ssid_len));
4525 scan->direct_scan[0].id = WLAN_EID_SSID;
4526 scan->direct_scan[0].len = priv->direct_ssid_len;
4527 memcpy(scan->direct_scan[0].ssid,
4528 priv->direct_ssid, priv->direct_ssid_len);
4530 } else if (!iwl_is_associated(priv) && priv->essid_len) {
4532 ("Kicking off one direct scan for '%s' when not associated\n",
4533 iwl4965_escape_essid(priv->essid, priv->essid_len));
4534 scan->direct_scan[0].id = WLAN_EID_SSID;
4535 scan->direct_scan[0].len = priv->essid_len;
4536 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
4539 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
4543 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
4544 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
4545 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4548 switch (priv->scan_bands) {
4550 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
4551 scan->tx_cmd.rate_n_flags =
4552 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
4553 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
4555 scan->good_CRC_th = 0;
4556 band = IEEE80211_BAND_2GHZ;
4560 scan->tx_cmd.rate_n_flags =
4561 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
4562 RATE_MCS_ANT_B_MSK);
4563 scan->good_CRC_th = IWL_GOOD_CRC_TH;
4564 band = IEEE80211_BAND_5GHZ;
4568 IWL_WARNING("Invalid scan band count\n");
4572 /* We don't build a direct scan probe request; the uCode will do
4573 * that based on the direct_mask added to each channel entry */
4574 cmd_len = iwl4965_fill_probe_req(priv, band,
4575 (struct ieee80211_mgmt *)scan->data,
4576 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
4578 scan->tx_cmd.len = cpu_to_le16(cmd_len);
4579 /* select Rx chains */
4581 /* Force use of chains B and C (0x6) for scan Rx.
4582 * Avoid A (0x1) because of its off-channel reception on A-band.
4583 * MIMO is not used here, but value is required to make uCode happy. */
4584 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
4585 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
4586 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
4587 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
4589 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
4590 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
4593 scan->channel_count =
4594 iwl4965_get_channels_for_scan(
4595 priv, band, 1, /* active */
4597 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
4599 scan->channel_count =
4600 iwl4965_get_channels_for_scan(
4601 priv, band, 0, /* passive */
4603 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
4605 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
4606 RXON_FILTER_BCON_AWARE_MSK);
4607 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
4608 scan->channel_count * sizeof(struct iwl4965_scan_channel);
4610 scan->len = cpu_to_le16(cmd.len);
4612 set_bit(STATUS_SCAN_HW, &priv->status);
4613 ret = iwl_send_cmd_sync(priv, &cmd);
4617 queue_delayed_work(priv->workqueue, &priv->scan_check,
4618 IWL_SCAN_CHECK_WATCHDOG);
4620 mutex_unlock(&priv->mutex);
4624 /* inform mac80211 scan aborted */
4625 queue_work(priv->workqueue, &priv->scan_completed);
4626 mutex_unlock(&priv->mutex);
4629 static void iwl4965_bg_up(struct work_struct *data)
4631 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
4633 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4636 mutex_lock(&priv->mutex);
4638 mutex_unlock(&priv->mutex);
4641 static void iwl4965_bg_restart(struct work_struct *data)
4643 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
4645 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4649 queue_work(priv->workqueue, &priv->up);
4652 static void iwl4965_bg_rx_replenish(struct work_struct *data)
4654 struct iwl_priv *priv =
4655 container_of(data, struct iwl_priv, rx_replenish);
4657 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4660 mutex_lock(&priv->mutex);
4661 iwl_rx_replenish(priv);
4662 mutex_unlock(&priv->mutex);
4665 #define IWL_DELAY_NEXT_SCAN (HZ*2)
4667 static void iwl4965_post_associate(struct iwl_priv *priv)
4669 struct ieee80211_conf *conf = NULL;
4671 DECLARE_MAC_BUF(mac);
4673 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4674 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
4678 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
4680 print_mac(mac, priv->active_rxon.bssid_addr));
4683 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4687 if (!priv->vif || !priv->is_open)
4690 iwl4965_scan_cancel_timeout(priv, 200);
4692 conf = ieee80211_get_hw_conf(priv->hw);
4694 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4695 iwl4965_commit_rxon(priv);
4697 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4698 iwl4965_setup_rxon_timing(priv);
4699 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
4700 sizeof(priv->rxon_timing), &priv->rxon_timing);
4702 IWL_WARNING("REPLY_RXON_TIMING failed - "
4703 "Attempting to continue.\n");
4705 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4707 #ifdef CONFIG_IWL4965_HT
4708 if (priv->current_ht_config.is_ht)
4709 iwl_set_rxon_ht(priv, &priv->current_ht_config);
4710 #endif /* CONFIG_IWL4965_HT*/
4711 iwl_set_rxon_chain(priv);
4712 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4714 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
4715 priv->assoc_id, priv->beacon_int);
4717 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4718 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4720 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4722 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4723 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
4724 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
4726 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4728 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4729 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
4733 iwl4965_commit_rxon(priv);
4735 switch (priv->iw_mode) {
4736 case IEEE80211_IF_TYPE_STA:
4737 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
4740 case IEEE80211_IF_TYPE_IBSS:
4742 /* clear out the station table */
4743 iwlcore_clear_stations_table(priv);
4745 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
4746 iwl_rxon_add_station(priv, priv->bssid, 0);
4747 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
4748 iwl4965_send_beacon_cmd(priv);
4753 IWL_ERROR("%s Should not be called in %d mode\n",
4754 __FUNCTION__, priv->iw_mode);
4758 iwl4965_sequence_reset(priv);
4760 /* Enable Rx differential gain and sensitivity calibrations */
4761 iwl_chain_noise_reset(priv);
4762 priv->start_calib = 1;
4764 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4765 priv->assoc_station_added = 1;
4767 iwl4965_activate_qos(priv, 0);
4769 iwl_power_update_mode(priv, 0);
4770 /* we have just associated, don't start scan too early */
4771 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
4775 static void iwl4965_bg_post_associate(struct work_struct *data)
4777 struct iwl_priv *priv = container_of(data, struct iwl_priv,
4778 post_associate.work);
4780 mutex_lock(&priv->mutex);
4781 iwl4965_post_associate(priv);
4782 mutex_unlock(&priv->mutex);
4786 static void iwl4965_bg_abort_scan(struct work_struct *work)
4788 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
4790 if (!iwl_is_ready(priv))
4793 mutex_lock(&priv->mutex);
4795 set_bit(STATUS_SCAN_ABORTING, &priv->status);
4796 iwl4965_send_scan_abort(priv);
4798 mutex_unlock(&priv->mutex);
4801 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
4803 static void iwl4965_bg_scan_completed(struct work_struct *work)
4805 struct iwl_priv *priv =
4806 container_of(work, struct iwl_priv, scan_completed);
4808 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
4810 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4813 if (test_bit(STATUS_CONF_PENDING, &priv->status))
4814 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
4816 ieee80211_scan_completed(priv->hw);
4818 /* Since setting the TXPOWER may have been deferred while
4819 * performing the scan, fire one off */
4820 mutex_lock(&priv->mutex);
4821 iwl4965_hw_reg_send_txpower(priv);
4822 mutex_unlock(&priv->mutex);
4825 /*****************************************************************************
4827 * mac80211 entry point functions
4829 *****************************************************************************/
4831 #define UCODE_READY_TIMEOUT (2 * HZ)
4833 static int iwl4965_mac_start(struct ieee80211_hw *hw)
4835 struct iwl_priv *priv = hw->priv;
4838 IWL_DEBUG_MAC80211("enter\n");
4840 if (pci_enable_device(priv->pci_dev)) {
4841 IWL_ERROR("Fail to pci_enable_device\n");
4844 pci_restore_state(priv->pci_dev);
4845 pci_enable_msi(priv->pci_dev);
4847 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
4850 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
4851 goto out_disable_msi;
4854 /* we should be verifying the device is ready to be opened */
4855 mutex_lock(&priv->mutex);
4857 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
4858 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
4859 * ucode filename and max sizes are card-specific. */
4861 if (!priv->ucode_code.len) {
4862 ret = iwl4965_read_ucode(priv);
4864 IWL_ERROR("Could not read microcode: %d\n", ret);
4865 mutex_unlock(&priv->mutex);
4866 goto out_release_irq;
4870 ret = __iwl4965_up(priv);
4872 mutex_unlock(&priv->mutex);
4875 goto out_release_irq;
4877 IWL_DEBUG_INFO("Start UP work done.\n");
4879 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
4882 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
4883 * mac80211 will not be run successfully. */
4884 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
4885 test_bit(STATUS_READY, &priv->status),
4886 UCODE_READY_TIMEOUT);
4888 if (!test_bit(STATUS_READY, &priv->status)) {
4889 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
4890 jiffies_to_msecs(UCODE_READY_TIMEOUT));
4892 goto out_release_irq;
4897 IWL_DEBUG_MAC80211("leave\n");
4901 free_irq(priv->pci_dev->irq, priv);
4903 pci_disable_msi(priv->pci_dev);
4904 pci_disable_device(priv->pci_dev);
4906 IWL_DEBUG_MAC80211("leave - failed\n");
4910 static void iwl4965_mac_stop(struct ieee80211_hw *hw)
4912 struct iwl_priv *priv = hw->priv;
4914 IWL_DEBUG_MAC80211("enter\n");
4916 if (!priv->is_open) {
4917 IWL_DEBUG_MAC80211("leave - skip\n");
4923 if (iwl_is_ready_rf(priv)) {
4924 /* stop mac, cancel any scan request and clear
4925 * RXON_FILTER_ASSOC_MSK BIT
4927 mutex_lock(&priv->mutex);
4928 iwl4965_scan_cancel_timeout(priv, 100);
4929 cancel_delayed_work(&priv->post_associate);
4930 mutex_unlock(&priv->mutex);
4935 flush_workqueue(priv->workqueue);
4936 free_irq(priv->pci_dev->irq, priv);
4937 pci_disable_msi(priv->pci_dev);
4938 pci_save_state(priv->pci_dev);
4939 pci_disable_device(priv->pci_dev);
4941 IWL_DEBUG_MAC80211("leave\n");
4944 static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
4945 struct ieee80211_tx_control *ctl)
4947 struct iwl_priv *priv = hw->priv;
4949 IWL_DEBUG_MAC80211("enter\n");
4951 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
4952 IWL_DEBUG_MAC80211("leave - monitor\n");
4956 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
4957 ctl->tx_rate->bitrate);
4959 if (iwl4965_tx_skb(priv, skb, ctl))
4960 dev_kfree_skb_any(skb);
4962 IWL_DEBUG_MAC80211("leave\n");
4966 static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
4967 struct ieee80211_if_init_conf *conf)
4969 struct iwl_priv *priv = hw->priv;
4970 unsigned long flags;
4971 DECLARE_MAC_BUF(mac);
4973 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
4976 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
4980 spin_lock_irqsave(&priv->lock, flags);
4981 priv->vif = conf->vif;
4983 spin_unlock_irqrestore(&priv->lock, flags);
4985 mutex_lock(&priv->mutex);
4987 if (conf->mac_addr) {
4988 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
4989 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
4992 if (iwl_is_ready(priv))
4993 iwl4965_set_mode(priv, conf->type);
4995 mutex_unlock(&priv->mutex);
4997 IWL_DEBUG_MAC80211("leave\n");
5002 * iwl4965_mac_config - mac80211 config callback
5004 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5005 * be set inappropriately and the driver currently sets the hardware up to
5006 * use it whenever needed.
5008 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
5010 struct iwl_priv *priv = hw->priv;
5011 const struct iwl_channel_info *ch_info;
5012 unsigned long flags;
5015 mutex_lock(&priv->mutex);
5016 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
5018 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
5020 if (!iwl_is_ready(priv)) {
5021 IWL_DEBUG_MAC80211("leave - not ready\n");
5026 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
5027 test_bit(STATUS_SCANNING, &priv->status))) {
5028 IWL_DEBUG_MAC80211("leave - scanning\n");
5029 set_bit(STATUS_CONF_PENDING, &priv->status);
5030 mutex_unlock(&priv->mutex);
5034 spin_lock_irqsave(&priv->lock, flags);
5036 ch_info = iwl_get_channel_info(priv, conf->channel->band,
5037 ieee80211_frequency_to_channel(conf->channel->center_freq));
5038 if (!is_channel_valid(ch_info)) {
5039 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5040 spin_unlock_irqrestore(&priv->lock, flags);
5045 #ifdef CONFIG_IWL4965_HT
5046 /* if we are switching from ht to 2.4 clear flags
5047 * from any ht related info since 2.4 does not
5049 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
5050 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
5051 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
5054 priv->staging_rxon.flags = 0;
5055 #endif /* CONFIG_IWL4965_HT */
5057 iwl_set_rxon_channel(priv, conf->channel->band,
5058 ieee80211_frequency_to_channel(conf->channel->center_freq));
5060 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
5062 /* The list of supported rates and rate mask can be different
5063 * for each band; since the band may have changed, reset
5064 * the rate mask to what mac80211 lists */
5065 iwl4965_set_rate(priv);
5067 spin_unlock_irqrestore(&priv->lock, flags);
5069 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
5070 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
5071 iwl4965_hw_channel_switch(priv, conf->channel);
5076 if (priv->cfg->ops->lib->radio_kill_sw)
5077 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
5079 if (!conf->radio_enabled) {
5080 IWL_DEBUG_MAC80211("leave - radio disabled\n");
5084 if (iwl_is_rfkill(priv)) {
5085 IWL_DEBUG_MAC80211("leave - RF kill\n");
5090 iwl4965_set_rate(priv);
5092 if (memcmp(&priv->active_rxon,
5093 &priv->staging_rxon, sizeof(priv->staging_rxon)))
5094 iwl4965_commit_rxon(priv);
5096 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
5098 IWL_DEBUG_MAC80211("leave\n");
5101 clear_bit(STATUS_CONF_PENDING, &priv->status);
5102 mutex_unlock(&priv->mutex);
5106 static void iwl4965_config_ap(struct iwl_priv *priv)
5110 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5113 /* The following should be done only at AP bring up */
5114 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
5116 /* RXON - unassoc (to set timing command) */
5117 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5118 iwl4965_commit_rxon(priv);
5121 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5122 iwl4965_setup_rxon_timing(priv);
5123 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
5124 sizeof(priv->rxon_timing), &priv->rxon_timing);
5126 IWL_WARNING("REPLY_RXON_TIMING failed - "
5127 "Attempting to continue.\n");
5129 iwl_set_rxon_chain(priv);
5131 /* FIXME: what should be the assoc_id for AP? */
5132 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5133 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5134 priv->staging_rxon.flags |=
5135 RXON_FLG_SHORT_PREAMBLE_MSK;
5137 priv->staging_rxon.flags &=
5138 ~RXON_FLG_SHORT_PREAMBLE_MSK;
5140 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5141 if (priv->assoc_capability &
5142 WLAN_CAPABILITY_SHORT_SLOT_TIME)
5143 priv->staging_rxon.flags |=
5144 RXON_FLG_SHORT_SLOT_MSK;
5146 priv->staging_rxon.flags &=
5147 ~RXON_FLG_SHORT_SLOT_MSK;
5149 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5150 priv->staging_rxon.flags &=
5151 ~RXON_FLG_SHORT_SLOT_MSK;
5153 /* restore RXON assoc */
5154 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
5155 iwl4965_commit_rxon(priv);
5156 iwl4965_activate_qos(priv, 1);
5157 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
5159 iwl4965_send_beacon_cmd(priv);
5161 /* FIXME - we need to add code here to detect a totally new
5162 * configuration, reset the AP, unassoc, rxon timing, assoc,
5163 * clear sta table, add BCAST sta... */
5166 static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
5167 struct ieee80211_vif *vif,
5168 struct ieee80211_if_conf *conf)
5170 struct iwl_priv *priv = hw->priv;
5171 DECLARE_MAC_BUF(mac);
5172 unsigned long flags;
5178 if (priv->vif != vif) {
5179 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
5183 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
5184 (!conf->beacon || !conf->ssid_len)) {
5186 ("Leaving in AP mode because HostAPD is not ready.\n");
5190 if (!iwl_is_alive(priv))
5193 mutex_lock(&priv->mutex);
5196 IWL_DEBUG_MAC80211("bssid: %s\n",
5197 print_mac(mac, conf->bssid));
5200 * very dubious code was here; the probe filtering flag is never set:
5202 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
5203 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
5206 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5208 conf->bssid = priv->mac_addr;
5209 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
5210 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
5211 print_mac(mac, conf->bssid));
5213 if (priv->ibss_beacon)
5214 dev_kfree_skb(priv->ibss_beacon);
5216 priv->ibss_beacon = conf->beacon;
5219 if (iwl_is_rfkill(priv))
5222 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
5223 !is_multicast_ether_addr(conf->bssid)) {
5224 /* If there is currently a HW scan going on in the background
5225 * then we need to cancel it else the RXON below will fail. */
5226 if (iwl4965_scan_cancel_timeout(priv, 100)) {
5227 IWL_WARNING("Aborted scan still in progress "
5229 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5230 mutex_unlock(&priv->mutex);
5233 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
5235 /* TODO: Audit driver for usage of these members and see
5236 * if mac80211 deprecates them (priv->bssid looks like it
5237 * shouldn't be there, but I haven't scanned the IBSS code
5238 * to verify) - jpk */
5239 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5241 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
5242 iwl4965_config_ap(priv);
5244 rc = iwl4965_commit_rxon(priv);
5245 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
5246 iwl_rxon_add_station(
5247 priv, priv->active_rxon.bssid_addr, 1);
5251 iwl4965_scan_cancel_timeout(priv, 100);
5252 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5253 iwl4965_commit_rxon(priv);
5257 spin_lock_irqsave(&priv->lock, flags);
5258 if (!conf->ssid_len)
5259 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5261 memcpy(priv->essid, conf->ssid, conf->ssid_len);
5263 priv->essid_len = conf->ssid_len;
5264 spin_unlock_irqrestore(&priv->lock, flags);
5266 IWL_DEBUG_MAC80211("leave\n");
5267 mutex_unlock(&priv->mutex);
5272 static void iwl4965_configure_filter(struct ieee80211_hw *hw,
5273 unsigned int changed_flags,
5274 unsigned int *total_flags,
5275 int mc_count, struct dev_addr_list *mc_list)
5279 * see also iwl4965_connection_init_rx_config
5281 struct iwl_priv *priv = hw->priv;
5283 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5284 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
5285 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
5286 IEEE80211_IF_TYPE_MNTR,
5287 changed_flags, *total_flags);
5288 /* queue work 'cuz mac80211 is holding a lock which
5289 * prevents us from issuing (synchronous) f/w cmds */
5290 queue_work(priv->workqueue, &priv->set_monitor);
5291 new_flags &= FIF_PROMISC_IN_BSS |
5296 *total_flags = new_flags;
5299 static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
5300 struct ieee80211_if_init_conf *conf)
5302 struct iwl_priv *priv = hw->priv;
5304 IWL_DEBUG_MAC80211("enter\n");
5306 mutex_lock(&priv->mutex);
5308 if (iwl_is_ready_rf(priv)) {
5309 iwl4965_scan_cancel_timeout(priv, 100);
5310 cancel_delayed_work(&priv->post_associate);
5311 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5312 iwl4965_commit_rxon(priv);
5314 if (priv->vif == conf->vif) {
5316 memset(priv->bssid, 0, ETH_ALEN);
5317 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5318 priv->essid_len = 0;
5320 mutex_unlock(&priv->mutex);
5322 IWL_DEBUG_MAC80211("leave\n");
5326 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
5327 static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
5328 struct ieee80211_vif *vif,
5329 struct ieee80211_bss_conf *bss_conf,
5332 struct iwl_priv *priv = hw->priv;
5334 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
5336 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
5337 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
5338 bss_conf->use_short_preamble);
5339 if (bss_conf->use_short_preamble)
5340 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5342 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5345 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
5346 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
5347 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
5348 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
5350 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
5353 if (changes & BSS_CHANGED_HT) {
5354 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
5355 iwl4965_ht_conf(priv, bss_conf);
5356 iwl_set_rxon_chain(priv);
5359 if (changes & BSS_CHANGED_ASSOC) {
5360 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
5361 /* This should never happen as this function should
5362 * never be called from interrupt context. */
5363 if (WARN_ON_ONCE(in_interrupt()))
5365 if (bss_conf->assoc) {
5366 priv->assoc_id = bss_conf->aid;
5367 priv->beacon_int = bss_conf->beacon_int;
5368 priv->timestamp = bss_conf->timestamp;
5369 priv->assoc_capability = bss_conf->assoc_capability;
5370 priv->next_scan_jiffies = jiffies +
5371 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
5372 mutex_lock(&priv->mutex);
5373 iwl4965_post_associate(priv);
5374 mutex_unlock(&priv->mutex);
5377 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
5379 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
5380 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
5381 iwl_send_rxon_assoc(priv);
5386 static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
5389 unsigned long flags;
5390 struct iwl_priv *priv = hw->priv;
5392 IWL_DEBUG_MAC80211("enter\n");
5394 mutex_lock(&priv->mutex);
5395 spin_lock_irqsave(&priv->lock, flags);
5397 if (!iwl_is_ready_rf(priv)) {
5399 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
5403 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
5405 IWL_ERROR("ERROR: APs don't scan\n");
5409 /* we don't schedule scan within next_scan_jiffies period */
5410 if (priv->next_scan_jiffies &&
5411 time_after(priv->next_scan_jiffies, jiffies)) {
5415 /* if we just finished scan ask for delay */
5416 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
5417 IWL_DELAY_NEXT_SCAN, jiffies)) {
5422 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
5423 iwl4965_escape_essid(ssid, len), (int)len);
5425 priv->one_direct_scan = 1;
5426 priv->direct_ssid_len = (u8)
5427 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
5428 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
5430 priv->one_direct_scan = 0;
5432 rc = iwl4965_scan_initiate(priv);
5434 IWL_DEBUG_MAC80211("leave\n");
5437 spin_unlock_irqrestore(&priv->lock, flags);
5438 mutex_unlock(&priv->mutex);
5443 static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
5444 struct ieee80211_key_conf *keyconf, const u8 *addr,
5445 u32 iv32, u16 *phase1key)
5447 struct iwl_priv *priv = hw->priv;
5448 u8 sta_id = IWL_INVALID_STATION;
5449 unsigned long flags;
5450 __le16 key_flags = 0;
5452 DECLARE_MAC_BUF(mac);
5454 IWL_DEBUG_MAC80211("enter\n");
5456 sta_id = iwl_find_station(priv, addr);
5457 if (sta_id == IWL_INVALID_STATION) {
5458 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5459 print_mac(mac, addr));
5463 iwl4965_scan_cancel_timeout(priv, 100);
5465 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
5466 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
5467 key_flags &= ~STA_KEY_FLG_INVALID;
5469 if (sta_id == priv->hw_params.bcast_sta_id)
5470 key_flags |= STA_KEY_MULTICAST_MSK;
5472 spin_lock_irqsave(&priv->sta_lock, flags);
5474 priv->stations[sta_id].sta.key.key_flags = key_flags;
5475 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
5477 for (i = 0; i < 5; i++)
5478 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
5479 cpu_to_le16(phase1key[i]);
5481 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
5482 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
5484 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
5486 spin_unlock_irqrestore(&priv->sta_lock, flags);
5488 IWL_DEBUG_MAC80211("leave\n");
5491 static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5492 const u8 *local_addr, const u8 *addr,
5493 struct ieee80211_key_conf *key)
5495 struct iwl_priv *priv = hw->priv;
5496 DECLARE_MAC_BUF(mac);
5498 u8 sta_id = IWL_INVALID_STATION;
5499 u8 is_default_wep_key = 0;
5501 IWL_DEBUG_MAC80211("enter\n");
5503 if (priv->hw_params.sw_crypto) {
5504 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
5508 if (is_zero_ether_addr(addr))
5509 /* only support pairwise keys */
5512 sta_id = iwl_find_station(priv, addr);
5513 if (sta_id == IWL_INVALID_STATION) {
5514 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5515 print_mac(mac, addr));
5520 mutex_lock(&priv->mutex);
5521 iwl4965_scan_cancel_timeout(priv, 100);
5522 mutex_unlock(&priv->mutex);
5524 /* If we are getting WEP group key and we didn't receive any key mapping
5525 * so far, we are in legacy wep mode (group key only), otherwise we are
5527 * In legacy wep mode, we use another host command to the uCode */
5528 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
5529 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
5531 is_default_wep_key = !priv->key_mapping_key;
5533 is_default_wep_key = priv->default_wep_key;
5538 if (is_default_wep_key)
5539 ret = iwl_set_default_wep_key(priv, key);
5541 ret = iwl_set_dynamic_key(priv, key, sta_id);
5543 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
5546 if (is_default_wep_key)
5547 ret = iwl_remove_default_wep_key(priv, key);
5549 ret = iwl_remove_dynamic_key(priv, key, sta_id);
5551 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
5557 IWL_DEBUG_MAC80211("leave\n");
5562 static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
5563 const struct ieee80211_tx_queue_params *params)
5565 struct iwl_priv *priv = hw->priv;
5566 unsigned long flags;
5569 IWL_DEBUG_MAC80211("enter\n");
5571 if (!iwl_is_ready_rf(priv)) {
5572 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5576 if (queue >= AC_NUM) {
5577 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5581 if (!priv->qos_data.qos_enable) {
5582 priv->qos_data.qos_active = 0;
5583 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
5586 q = AC_NUM - 1 - queue;
5588 spin_lock_irqsave(&priv->lock, flags);
5590 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5591 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5592 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5593 priv->qos_data.def_qos_parm.ac[q].edca_txop =
5594 cpu_to_le16((params->txop * 32));
5596 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
5597 priv->qos_data.qos_active = 1;
5599 spin_unlock_irqrestore(&priv->lock, flags);
5601 mutex_lock(&priv->mutex);
5602 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
5603 iwl4965_activate_qos(priv, 1);
5604 else if (priv->assoc_id && iwl_is_associated(priv))
5605 iwl4965_activate_qos(priv, 0);
5607 mutex_unlock(&priv->mutex);
5609 IWL_DEBUG_MAC80211("leave\n");
5613 static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
5614 struct ieee80211_tx_queue_stats *stats)
5616 struct iwl_priv *priv = hw->priv;
5618 struct iwl_tx_queue *txq;
5619 struct iwl_queue *q;
5620 unsigned long flags;
5622 IWL_DEBUG_MAC80211("enter\n");
5624 if (!iwl_is_ready_rf(priv)) {
5625 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5629 spin_lock_irqsave(&priv->lock, flags);
5631 for (i = 0; i < AC_NUM; i++) {
5632 txq = &priv->txq[i];
5634 avail = iwl_queue_space(q);
5636 stats[i].len = q->n_window - avail;
5637 stats[i].limit = q->n_window - q->high_mark;
5638 stats[i].count = q->n_window;
5641 spin_unlock_irqrestore(&priv->lock, flags);
5643 IWL_DEBUG_MAC80211("leave\n");
5648 static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
5649 struct ieee80211_low_level_stats *stats)
5651 struct iwl_priv *priv = hw->priv;
5654 IWL_DEBUG_MAC80211("enter\n");
5655 IWL_DEBUG_MAC80211("leave\n");
5660 static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
5662 struct iwl_priv *priv;
5665 IWL_DEBUG_MAC80211("enter\n");
5666 IWL_DEBUG_MAC80211("leave\n");
5671 static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
5673 struct iwl_priv *priv = hw->priv;
5674 unsigned long flags;
5676 mutex_lock(&priv->mutex);
5677 IWL_DEBUG_MAC80211("enter\n");
5679 priv->lq_mngr.lq_ready = 0;
5680 #ifdef CONFIG_IWL4965_HT
5681 spin_lock_irqsave(&priv->lock, flags);
5682 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
5683 spin_unlock_irqrestore(&priv->lock, flags);
5684 #endif /* CONFIG_IWL4965_HT */
5686 iwl_reset_qos(priv);
5688 cancel_delayed_work(&priv->post_associate);
5690 spin_lock_irqsave(&priv->lock, flags);
5692 priv->assoc_capability = 0;
5693 priv->assoc_station_added = 0;
5695 /* new association get rid of ibss beacon skb */
5696 if (priv->ibss_beacon)
5697 dev_kfree_skb(priv->ibss_beacon);
5699 priv->ibss_beacon = NULL;
5701 priv->beacon_int = priv->hw->conf.beacon_int;
5702 priv->timestamp = 0;
5703 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
5704 priv->beacon_int = 0;
5706 spin_unlock_irqrestore(&priv->lock, flags);
5708 if (!iwl_is_ready_rf(priv)) {
5709 IWL_DEBUG_MAC80211("leave - not ready\n");
5710 mutex_unlock(&priv->mutex);
5714 /* we are restarting association process
5715 * clear RXON_FILTER_ASSOC_MSK bit
5717 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
5718 iwl4965_scan_cancel_timeout(priv, 100);
5719 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5720 iwl4965_commit_rxon(priv);
5723 iwl_power_update_mode(priv, 0);
5725 /* Per mac80211.h: This is only used in IBSS mode... */
5726 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
5728 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
5729 mutex_unlock(&priv->mutex);
5733 iwl4965_set_rate(priv);
5735 mutex_unlock(&priv->mutex);
5737 IWL_DEBUG_MAC80211("leave\n");
5740 static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
5741 struct ieee80211_tx_control *control)
5743 struct iwl_priv *priv = hw->priv;
5744 unsigned long flags;
5746 mutex_lock(&priv->mutex);
5747 IWL_DEBUG_MAC80211("enter\n");
5749 if (!iwl_is_ready_rf(priv)) {
5750 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5751 mutex_unlock(&priv->mutex);
5755 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
5756 IWL_DEBUG_MAC80211("leave - not IBSS\n");
5757 mutex_unlock(&priv->mutex);
5761 spin_lock_irqsave(&priv->lock, flags);
5763 if (priv->ibss_beacon)
5764 dev_kfree_skb(priv->ibss_beacon);
5766 priv->ibss_beacon = skb;
5770 IWL_DEBUG_MAC80211("leave\n");
5771 spin_unlock_irqrestore(&priv->lock, flags);
5773 iwl_reset_qos(priv);
5775 queue_work(priv->workqueue, &priv->post_associate.work);
5777 mutex_unlock(&priv->mutex);
5782 /*****************************************************************************
5786 *****************************************************************************/
5788 #ifdef CONFIG_IWLWIFI_DEBUG
5791 * The following adds a new attribute to the sysfs representation
5792 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
5793 * used for controlling the debug level.
5795 * See the level definitions in iwl for details.
5798 static ssize_t show_debug_level(struct device *d,
5799 struct device_attribute *attr, char *buf)
5801 struct iwl_priv *priv = d->driver_data;
5803 return sprintf(buf, "0x%08X\n", priv->debug_level);
5805 static ssize_t store_debug_level(struct device *d,
5806 struct device_attribute *attr,
5807 const char *buf, size_t count)
5809 struct iwl_priv *priv = d->driver_data;
5810 char *p = (char *)buf;
5813 val = simple_strtoul(p, &p, 0);
5815 printk(KERN_INFO DRV_NAME
5816 ": %s is not in hex or decimal form.\n", buf);
5818 priv->debug_level = val;
5820 return strnlen(buf, count);
5823 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5824 show_debug_level, store_debug_level);
5827 #endif /* CONFIG_IWLWIFI_DEBUG */
5830 static ssize_t show_version(struct device *d,
5831 struct device_attribute *attr, char *buf)
5833 struct iwl_priv *priv = d->driver_data;
5834 struct iwl4965_alive_resp *palive = &priv->card_alive;
5836 if (palive->is_valid)
5837 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
5838 "fw type: 0x%01X 0x%01X\n",
5839 palive->ucode_major, palive->ucode_minor,
5840 palive->sw_rev[0], palive->sw_rev[1],
5841 palive->ver_type, palive->ver_subtype);
5844 return sprintf(buf, "fw not loaded\n");
5847 static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
5849 static ssize_t show_temperature(struct device *d,
5850 struct device_attribute *attr, char *buf)
5852 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5854 if (!iwl_is_alive(priv))
5857 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
5860 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
5862 static ssize_t show_rs_window(struct device *d,
5863 struct device_attribute *attr,
5866 struct iwl_priv *priv = d->driver_data;
5867 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
5869 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
5871 static ssize_t show_tx_power(struct device *d,
5872 struct device_attribute *attr, char *buf)
5874 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5875 return sprintf(buf, "%d\n", priv->user_txpower_limit);
5878 static ssize_t store_tx_power(struct device *d,
5879 struct device_attribute *attr,
5880 const char *buf, size_t count)
5882 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5883 char *p = (char *)buf;
5886 val = simple_strtoul(p, &p, 10);
5888 printk(KERN_INFO DRV_NAME
5889 ": %s is not in decimal form.\n", buf);
5891 iwl4965_hw_reg_set_txpower(priv, val);
5896 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
5898 static ssize_t show_flags(struct device *d,
5899 struct device_attribute *attr, char *buf)
5901 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5903 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
5906 static ssize_t store_flags(struct device *d,
5907 struct device_attribute *attr,
5908 const char *buf, size_t count)
5910 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5911 u32 flags = simple_strtoul(buf, NULL, 0);
5913 mutex_lock(&priv->mutex);
5914 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
5915 /* Cancel any currently running scans... */
5916 if (iwl4965_scan_cancel_timeout(priv, 100))
5917 IWL_WARNING("Could not cancel scan.\n");
5919 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
5921 priv->staging_rxon.flags = cpu_to_le32(flags);
5922 iwl4965_commit_rxon(priv);
5925 mutex_unlock(&priv->mutex);
5930 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
5932 static ssize_t show_filter_flags(struct device *d,
5933 struct device_attribute *attr, char *buf)
5935 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5937 return sprintf(buf, "0x%04X\n",
5938 le32_to_cpu(priv->active_rxon.filter_flags));
5941 static ssize_t store_filter_flags(struct device *d,
5942 struct device_attribute *attr,
5943 const char *buf, size_t count)
5945 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5946 u32 filter_flags = simple_strtoul(buf, NULL, 0);
5948 mutex_lock(&priv->mutex);
5949 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
5950 /* Cancel any currently running scans... */
5951 if (iwl4965_scan_cancel_timeout(priv, 100))
5952 IWL_WARNING("Could not cancel scan.\n");
5954 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
5955 "0x%04X\n", filter_flags);
5956 priv->staging_rxon.filter_flags =
5957 cpu_to_le32(filter_flags);
5958 iwl4965_commit_rxon(priv);
5961 mutex_unlock(&priv->mutex);
5966 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
5967 store_filter_flags);
5969 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
5971 static ssize_t show_measurement(struct device *d,
5972 struct device_attribute *attr, char *buf)
5974 struct iwl_priv *priv = dev_get_drvdata(d);
5975 struct iwl4965_spectrum_notification measure_report;
5976 u32 size = sizeof(measure_report), len = 0, ofs = 0;
5977 u8 *data = (u8 *) & measure_report;
5978 unsigned long flags;
5980 spin_lock_irqsave(&priv->lock, flags);
5981 if (!(priv->measurement_status & MEASUREMENT_READY)) {
5982 spin_unlock_irqrestore(&priv->lock, flags);
5985 memcpy(&measure_report, &priv->measure_report, size);
5986 priv->measurement_status = 0;
5987 spin_unlock_irqrestore(&priv->lock, flags);
5989 while (size && (PAGE_SIZE - len)) {
5990 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5991 PAGE_SIZE - len, 1);
5993 if (PAGE_SIZE - len)
5997 size -= min(size, 16U);
6003 static ssize_t store_measurement(struct device *d,
6004 struct device_attribute *attr,
6005 const char *buf, size_t count)
6007 struct iwl_priv *priv = dev_get_drvdata(d);
6008 struct ieee80211_measurement_params params = {
6009 .channel = le16_to_cpu(priv->active_rxon.channel),
6010 .start_time = cpu_to_le64(priv->last_tsf),
6011 .duration = cpu_to_le16(1),
6013 u8 type = IWL_MEASURE_BASIC;
6019 strncpy(buffer, buf, min(sizeof(buffer), count));
6020 channel = simple_strtoul(p, NULL, 0);
6022 params.channel = channel;
6025 while (*p && *p != ' ')
6028 type = simple_strtoul(p + 1, NULL, 0);
6031 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6032 "channel %d (for '%s')\n", type, params.channel, buf);
6033 iwl4965_get_measurement(priv, ¶ms, type);
6038 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6039 show_measurement, store_measurement);
6040 #endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
6042 static ssize_t store_retry_rate(struct device *d,
6043 struct device_attribute *attr,
6044 const char *buf, size_t count)
6046 struct iwl_priv *priv = dev_get_drvdata(d);
6048 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6049 if (priv->retry_rate <= 0)
6050 priv->retry_rate = 1;
6055 static ssize_t show_retry_rate(struct device *d,
6056 struct device_attribute *attr, char *buf)
6058 struct iwl_priv *priv = dev_get_drvdata(d);
6059 return sprintf(buf, "%d", priv->retry_rate);
6062 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6065 static ssize_t store_power_level(struct device *d,
6066 struct device_attribute *attr,
6067 const char *buf, size_t count)
6069 struct iwl_priv *priv = dev_get_drvdata(d);
6073 mode = simple_strtoul(buf, NULL, 0);
6074 mutex_lock(&priv->mutex);
6076 if (!iwl_is_ready(priv)) {
6081 rc = iwl_power_set_user_mode(priv, mode);
6083 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6089 mutex_unlock(&priv->mutex);
6093 #define MAX_WX_STRING 80
6095 /* Values are in microsecond */
6096 static const s32 timeout_duration[] = {
6103 static const s32 period_duration[] = {
6111 static ssize_t show_power_level(struct device *d,
6112 struct device_attribute *attr, char *buf)
6114 struct iwl_priv *priv = dev_get_drvdata(d);
6115 int level = priv->power_data.power_mode;
6118 p += sprintf(p, "%d ", level);
6120 case IWL_POWER_MODE_CAM:
6122 p += sprintf(p, "(AC)");
6124 case IWL_POWER_BATTERY:
6125 p += sprintf(p, "(BATTERY)");
6129 "(Timeout %dms, Period %dms)",
6130 timeout_duration[level - 1] / 1000,
6131 period_duration[level - 1] / 1000);
6134 if (!(priv->power_mode & IWL_POWER_ENABLED))
6135 p += sprintf(p, " OFF\n");
6137 p += sprintf(p, " \n");
6139 p += sprintf(p, " \n");
6140 return (p - buf + 1);
6143 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6146 static ssize_t show_channels(struct device *d,
6147 struct device_attribute *attr, char *buf)
6149 /* all this shit doesn't belong into sysfs anyway */
6153 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6155 static ssize_t show_statistics(struct device *d,
6156 struct device_attribute *attr, char *buf)
6158 struct iwl_priv *priv = dev_get_drvdata(d);
6159 u32 size = sizeof(struct iwl4965_notif_statistics);
6160 u32 len = 0, ofs = 0;
6161 u8 *data = (u8 *) & priv->statistics;
6164 if (!iwl_is_alive(priv))
6167 mutex_lock(&priv->mutex);
6168 rc = iwl_send_statistics_request(priv, 0);
6169 mutex_unlock(&priv->mutex);
6173 "Error sending statistics request: 0x%08X\n", rc);
6177 while (size && (PAGE_SIZE - len)) {
6178 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6179 PAGE_SIZE - len, 1);
6181 if (PAGE_SIZE - len)
6185 size -= min(size, 16U);
6191 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
6193 static ssize_t show_status(struct device *d,
6194 struct device_attribute *attr, char *buf)
6196 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6197 if (!iwl_is_alive(priv))
6199 return sprintf(buf, "0x%08x\n", (int)priv->status);
6202 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6204 static ssize_t dump_error_log(struct device *d,
6205 struct device_attribute *attr,
6206 const char *buf, size_t count)
6208 char *p = (char *)buf;
6211 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
6213 return strnlen(buf, count);
6216 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
6218 static ssize_t dump_event_log(struct device *d,
6219 struct device_attribute *attr,
6220 const char *buf, size_t count)
6222 char *p = (char *)buf;
6225 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
6227 return strnlen(buf, count);
6230 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
6232 /*****************************************************************************
6234 * driver setup and teardown
6236 *****************************************************************************/
6238 static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
6240 priv->workqueue = create_workqueue(DRV_NAME);
6242 init_waitqueue_head(&priv->wait_command_queue);
6244 INIT_WORK(&priv->up, iwl4965_bg_up);
6245 INIT_WORK(&priv->restart, iwl4965_bg_restart);
6246 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
6247 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
6248 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
6249 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
6250 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
6251 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
6252 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
6253 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
6254 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
6255 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
6256 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
6258 iwl4965_hw_setup_deferred_work(priv);
6260 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
6261 iwl4965_irq_tasklet, (unsigned long)priv);
6264 static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
6266 iwl4965_hw_cancel_deferred_work(priv);
6268 cancel_delayed_work_sync(&priv->init_alive_start);
6269 cancel_delayed_work(&priv->scan_check);
6270 cancel_delayed_work(&priv->alive_start);
6271 cancel_delayed_work(&priv->post_associate);
6272 cancel_work_sync(&priv->beacon_update);
6275 static struct attribute *iwl4965_sysfs_entries[] = {
6276 &dev_attr_channels.attr,
6277 &dev_attr_dump_errors.attr,
6278 &dev_attr_dump_events.attr,
6279 &dev_attr_flags.attr,
6280 &dev_attr_filter_flags.attr,
6281 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
6282 &dev_attr_measurement.attr,
6284 &dev_attr_power_level.attr,
6285 &dev_attr_retry_rate.attr,
6286 &dev_attr_rs_window.attr,
6287 &dev_attr_statistics.attr,
6288 &dev_attr_status.attr,
6289 &dev_attr_temperature.attr,
6290 &dev_attr_tx_power.attr,
6291 #ifdef CONFIG_IWLWIFI_DEBUG
6292 &dev_attr_debug_level.attr,
6294 &dev_attr_version.attr,
6299 static struct attribute_group iwl4965_attribute_group = {
6300 .name = NULL, /* put in device directory */
6301 .attrs = iwl4965_sysfs_entries,
6304 static struct ieee80211_ops iwl4965_hw_ops = {
6305 .tx = iwl4965_mac_tx,
6306 .start = iwl4965_mac_start,
6307 .stop = iwl4965_mac_stop,
6308 .add_interface = iwl4965_mac_add_interface,
6309 .remove_interface = iwl4965_mac_remove_interface,
6310 .config = iwl4965_mac_config,
6311 .config_interface = iwl4965_mac_config_interface,
6312 .configure_filter = iwl4965_configure_filter,
6313 .set_key = iwl4965_mac_set_key,
6314 .update_tkip_key = iwl4965_mac_update_tkip_key,
6315 .get_stats = iwl4965_mac_get_stats,
6316 .get_tx_stats = iwl4965_mac_get_tx_stats,
6317 .conf_tx = iwl4965_mac_conf_tx,
6318 .get_tsf = iwl4965_mac_get_tsf,
6319 .reset_tsf = iwl4965_mac_reset_tsf,
6320 .beacon_update = iwl4965_mac_beacon_update,
6321 .bss_info_changed = iwl4965_bss_info_changed,
6322 #ifdef CONFIG_IWL4965_HT
6323 .ampdu_action = iwl4965_mac_ampdu_action,
6324 #endif /* CONFIG_IWL4965_HT */
6325 .hw_scan = iwl4965_mac_hw_scan
6328 static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
6331 struct iwl_priv *priv;
6332 struct ieee80211_hw *hw;
6333 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
6334 unsigned long flags;
6335 DECLARE_MAC_BUF(mac);
6337 /************************
6338 * 1. Allocating HW data
6339 ************************/
6341 /* Disabling hardware scan means that mac80211 will perform scans
6342 * "the hard way", rather than using device's scan. */
6343 if (cfg->mod_params->disable_hw_scan) {
6344 if (cfg->mod_params->debug & IWL_DL_INFO)
6345 dev_printk(KERN_DEBUG, &(pdev->dev),
6346 "Disabling hw_scan\n");
6347 iwl4965_hw_ops.hw_scan = NULL;
6350 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
6356 /* At this point both hw and priv are allocated. */
6358 SET_IEEE80211_DEV(hw, &pdev->dev);
6360 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
6362 priv->pci_dev = pdev;
6364 #ifdef CONFIG_IWLWIFI_DEBUG
6365 priv->debug_level = priv->cfg->mod_params->debug;
6366 atomic_set(&priv->restrict_refcnt, 0);
6369 /**************************
6370 * 2. Initializing PCI bus
6371 **************************/
6372 if (pci_enable_device(pdev)) {
6374 goto out_ieee80211_free_hw;
6377 pci_set_master(pdev);
6379 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
6381 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
6383 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6385 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
6386 /* both attempts failed: */
6388 printk(KERN_WARNING "%s: No suitable DMA available.\n",
6390 goto out_pci_disable_device;
6394 err = pci_request_regions(pdev, DRV_NAME);
6396 goto out_pci_disable_device;
6398 pci_set_drvdata(pdev, priv);
6400 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6401 * PCI Tx retries from interfering with C3 CPU state */
6402 pci_write_config_byte(pdev, 0x41, 0x00);
6404 /***********************
6405 * 3. Read REV register
6406 ***********************/
6407 priv->hw_base = pci_iomap(pdev, 0, 0);
6408 if (!priv->hw_base) {
6410 goto out_pci_release_regions;
6413 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
6414 (unsigned long long) pci_resource_len(pdev, 0));
6415 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
6417 iwl_hw_detect(priv);
6418 printk(KERN_INFO DRV_NAME
6419 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
6420 priv->cfg->name, priv->hw_rev);
6423 err = priv->cfg->ops->lib->apm_ops.init(priv);
6425 IWL_DEBUG_INFO("Failed to init APMG\n");
6431 /* Read the EEPROM */
6432 err = iwl_eeprom_init(priv);
6434 IWL_ERROR("Unable to init EEPROM\n");
6437 err = iwl_eeprom_check_version(priv);
6441 /* extract MAC Address */
6442 iwl_eeprom_get_mac(priv, priv->mac_addr);
6443 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
6444 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6446 /************************
6447 * 5. Setup HW constants
6448 ************************/
6449 /* Device-specific setup */
6450 if (priv->cfg->ops->lib->set_hw_params(priv)) {
6451 IWL_ERROR("failed to set hw parameters\n");
6452 goto out_free_eeprom;
6455 /*******************
6457 *******************/
6459 err = iwl_setup(priv);
6461 goto out_free_eeprom;
6462 /* At this point both hw and priv are initialized. */
6464 /**********************************
6465 * 7. Initialize module parameters
6466 **********************************/
6468 /* Disable radio (SW RF KILL) via parameter when loading driver */
6469 if (priv->cfg->mod_params->disable) {
6470 set_bit(STATUS_RF_KILL_SW, &priv->status);
6471 IWL_DEBUG_INFO("Radio disabled.\n");
6474 if (priv->cfg->mod_params->enable_qos)
6475 priv->qos_data.qos_enable = 1;
6477 /********************
6479 ********************/
6480 spin_lock_irqsave(&priv->lock, flags);
6481 iwl4965_disable_interrupts(priv);
6482 spin_unlock_irqrestore(&priv->lock, flags);
6484 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6486 IWL_ERROR("failed to create sysfs device attributes\n");
6487 goto out_free_eeprom;
6490 err = iwl_dbgfs_register(priv, DRV_NAME);
6492 IWL_ERROR("failed to create debugfs files\n");
6493 goto out_remove_sysfs;
6496 iwl4965_setup_deferred_work(priv);
6497 iwl4965_setup_rx_handlers(priv);
6499 /********************
6501 ********************/
6502 pci_save_state(pdev);
6503 pci_disable_device(pdev);
6505 /* notify iwlcore to init */
6506 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
6510 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6512 iwl_eeprom_free(priv);
6514 pci_iounmap(pdev, priv->hw_base);
6515 out_pci_release_regions:
6516 pci_release_regions(pdev);
6517 pci_set_drvdata(pdev, NULL);
6518 out_pci_disable_device:
6519 pci_disable_device(pdev);
6520 out_ieee80211_free_hw:
6521 ieee80211_free_hw(priv->hw);
6526 static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
6528 struct iwl_priv *priv = pci_get_drvdata(pdev);
6529 struct list_head *p, *q;
6531 unsigned long flags;
6536 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
6538 if (priv->mac80211_registered) {
6539 ieee80211_unregister_hw(priv->hw);
6540 priv->mac80211_registered = 0;
6543 set_bit(STATUS_EXIT_PENDING, &priv->status);
6547 /* make sure we flush any pending irq or
6548 * tasklet for the driver
6550 spin_lock_irqsave(&priv->lock, flags);
6551 iwl4965_disable_interrupts(priv);
6552 spin_unlock_irqrestore(&priv->lock, flags);
6554 iwl_synchronize_irq(priv);
6556 /* Free MAC hash list for ADHOC */
6557 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
6558 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
6560 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
6564 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
6565 iwl_dbgfs_unregister(priv);
6566 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6568 iwl4965_dealloc_ucode_pci(priv);
6571 iwl_rx_queue_free(priv, &priv->rxq);
6572 iwl_hw_txq_ctx_free(priv);
6574 iwlcore_clear_stations_table(priv);
6575 iwl_eeprom_free(priv);
6578 /*netif_stop_queue(dev); */
6579 flush_workqueue(priv->workqueue);
6581 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
6582 * priv->workqueue... so we can't take down the workqueue
6584 destroy_workqueue(priv->workqueue);
6585 priv->workqueue = NULL;
6587 pci_iounmap(pdev, priv->hw_base);
6588 pci_release_regions(pdev);
6589 pci_disable_device(pdev);
6590 pci_set_drvdata(pdev, NULL);
6592 iwl_free_channel_map(priv);
6593 iwlcore_free_geos(priv);
6595 if (priv->ibss_beacon)
6596 dev_kfree_skb(priv->ibss_beacon);
6598 ieee80211_free_hw(priv->hw);
6603 static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
6605 struct iwl_priv *priv = pci_get_drvdata(pdev);
6607 if (priv->is_open) {
6608 set_bit(STATUS_IN_SUSPEND, &priv->status);
6609 iwl4965_mac_stop(priv->hw);
6613 pci_set_power_state(pdev, PCI_D3hot);
6618 static int iwl4965_pci_resume(struct pci_dev *pdev)
6620 struct iwl_priv *priv = pci_get_drvdata(pdev);
6622 pci_set_power_state(pdev, PCI_D0);
6625 iwl4965_mac_start(priv->hw);
6627 clear_bit(STATUS_IN_SUSPEND, &priv->status);
6631 #endif /* CONFIG_PM */
6633 /*****************************************************************************
6635 * driver and module entry point
6637 *****************************************************************************/
6639 /* Hardware specific file defines the PCI IDs table for that hardware module */
6640 static struct pci_device_id iwl_hw_card_ids[] = {
6641 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
6642 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
6643 #ifdef CONFIG_IWL5000
6644 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
6645 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
6646 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
6647 #endif /* CONFIG_IWL5000 */
6650 MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
6652 static struct pci_driver iwl_driver = {
6654 .id_table = iwl_hw_card_ids,
6655 .probe = iwl4965_pci_probe,
6656 .remove = __devexit_p(iwl4965_pci_remove),
6658 .suspend = iwl4965_pci_suspend,
6659 .resume = iwl4965_pci_resume,
6663 static int __init iwl4965_init(void)
6667 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
6668 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
6670 ret = iwl4965_rate_control_register();
6672 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
6676 ret = pci_register_driver(&iwl_driver);
6678 IWL_ERROR("Unable to initialize PCI module\n");
6679 goto error_register;
6685 iwl4965_rate_control_unregister();
6689 static void __exit iwl4965_exit(void)
6691 pci_unregister_driver(&iwl_driver);
6692 iwl4965_rate_control_unregister();
6695 module_exit(iwl4965_exit);
6696 module_init(iwl4965_init);