aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/iwlwifi/dvm
diff options
context:
space:
mode:
authorJohannes Berg <johannes.berg@intel.com>2012-05-15 06:16:34 -0400
committerJohannes Berg <johannes.berg@intel.com>2012-06-06 07:21:14 -0400
commit1023fdc4858b6b8cb88ff28cafd425b77555be9f (patch)
tree16571db8e1a8c8be475b50513a3af33c3583bfc4 /drivers/net/wireless/iwlwifi/dvm
parent20041ea622fcb1992df536d253de5120808e64a5 (diff)
iwlwifi: move DVM code into subdirectory
Since we're working on another mode/driver inside iwlwifi, move the current one into a subdirectory to more cleanly separate the code. While at it, rename all the files. Reviewed-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com> Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Diffstat (limited to 'drivers/net/wireless/iwlwifi/dvm')
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/Makefile13
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/agn.h527
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/calib.c1113
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/calib.h74
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/commands.h3958
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/debugfs.c2433
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/dev.h1073
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/devices.c740
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/eeprom.c1146
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/eeprom.h269
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/led.c224
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/led.h43
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/lib.c1291
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/mac80211.c1631
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/main.c2371
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/power.c388
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/power.h47
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/rs.c3367
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/rs.h433
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/rx.c1167
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/rxon.c1577
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/scan.c1169
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/sta.c1479
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/testmode.c1113
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/testmode.h309
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/tt.c694
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/tt.h128
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/tx.c1385
-rw-r--r--drivers/net/wireless/iwlwifi/dvm/ucode.c533
29 files changed, 30695 insertions, 0 deletions
diff --git a/drivers/net/wireless/iwlwifi/dvm/Makefile b/drivers/net/wireless/iwlwifi/dvm/Makefile
new file mode 100644
index 000000000000..13f8282f4d0e
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/Makefile
@@ -0,0 +1,13 @@
1# DVM
2obj-$(CONFIG_IWLDVM) += iwldvm.o
3iwldvm-objs += main.o rs.o mac80211.o ucode.o tx.o
4iwldvm-objs += lib.o calib.o tt.o sta.o rx.o
5
6iwldvm-objs += eeprom.o power.o
7iwldvm-objs += scan.o led.o
8iwldvm-objs += rxon.o devices.o
9
10iwldvm-$(CONFIG_IWLWIFI_DEBUGFS) += debugfs.o
11iwldvm-$(CONFIG_IWLWIFI_DEVICE_TESTMODE) += testmode.o
12
13ccflags-y += -D__CHECK_ENDIAN__ -I$(src)/../
diff --git a/drivers/net/wireless/iwlwifi/dvm/agn.h b/drivers/net/wireless/iwlwifi/dvm/agn.h
new file mode 100644
index 000000000000..9a8dd1a579d0
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/agn.h
@@ -0,0 +1,527 @@
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62
63#ifndef __iwl_agn_h__
64#define __iwl_agn_h__
65
66#include "iwl-config.h"
67
68#include "dev.h"
69
70/* The first 11 queues (0-10) are used otherwise */
71#define IWLAGN_FIRST_AMPDU_QUEUE 11
72
73/* AUX (TX during scan dwell) queue */
74#define IWL_AUX_QUEUE 10
75
76/* device operations */
77extern struct iwl_lib_ops iwl1000_lib;
78extern struct iwl_lib_ops iwl2000_lib;
79extern struct iwl_lib_ops iwl2030_lib;
80extern struct iwl_lib_ops iwl5000_lib;
81extern struct iwl_lib_ops iwl5150_lib;
82extern struct iwl_lib_ops iwl6000_lib;
83extern struct iwl_lib_ops iwl6030_lib;
84
85
86#define TIME_UNIT 1024
87
88/*****************************************************
89* DRIVER STATUS FUNCTIONS
90******************************************************/
91#define STATUS_RF_KILL_HW 0
92#define STATUS_CT_KILL 1
93#define STATUS_ALIVE 2
94#define STATUS_READY 3
95#define STATUS_GEO_CONFIGURED 4
96#define STATUS_EXIT_PENDING 5
97#define STATUS_STATISTICS 6
98#define STATUS_SCANNING 7
99#define STATUS_SCAN_ABORTING 8
100#define STATUS_SCAN_HW 9
101#define STATUS_FW_ERROR 10
102#define STATUS_CHANNEL_SWITCH_PENDING 11
103#define STATUS_SCAN_COMPLETE 12
104#define STATUS_POWER_PMI 13
105#define STATUS_SCAN_ROC_EXPIRED 14
106
107struct iwl_ucode_capabilities;
108
109extern struct ieee80211_ops iwlagn_hw_ops;
110
111static inline void iwl_set_calib_hdr(struct iwl_calib_hdr *hdr, u8 cmd)
112{
113 hdr->op_code = cmd;
114 hdr->first_group = 0;
115 hdr->groups_num = 1;
116 hdr->data_valid = 1;
117}
118
119void iwl_down(struct iwl_priv *priv);
120void iwl_cancel_deferred_work(struct iwl_priv *priv);
121void iwlagn_prepare_restart(struct iwl_priv *priv);
122int __must_check iwl_rx_dispatch(struct iwl_op_mode *op_mode,
123 struct iwl_rx_cmd_buffer *rxb,
124 struct iwl_device_cmd *cmd);
125
126bool iwl_check_for_ct_kill(struct iwl_priv *priv);
127
128void iwlagn_lift_passive_no_rx(struct iwl_priv *priv);
129
130/* MAC80211 */
131struct ieee80211_hw *iwl_alloc_all(void);
132int iwlagn_mac_setup_register(struct iwl_priv *priv,
133 const struct iwl_ucode_capabilities *capa);
134void iwlagn_mac_unregister(struct iwl_priv *priv);
135
136/* commands */
137int iwl_dvm_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd);
138int iwl_dvm_send_cmd_pdu(struct iwl_priv *priv, u8 id,
139 u32 flags, u16 len, const void *data);
140
141/* RXON */
142void iwl_connection_init_rx_config(struct iwl_priv *priv,
143 struct iwl_rxon_context *ctx);
144int iwlagn_set_pan_params(struct iwl_priv *priv);
145int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx);
146void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx);
147int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed);
148void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
149 struct ieee80211_vif *vif,
150 struct ieee80211_bss_conf *bss_conf,
151 u32 changes);
152void iwlagn_config_ht40(struct ieee80211_conf *conf,
153 struct iwl_rxon_context *ctx);
154void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf);
155void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
156 struct iwl_rxon_context *ctx);
157void iwl_set_flags_for_band(struct iwl_priv *priv,
158 struct iwl_rxon_context *ctx,
159 enum ieee80211_band band,
160 struct ieee80211_vif *vif);
161
162/* uCode */
163int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type);
164void iwl_send_prio_tbl(struct iwl_priv *priv);
165int iwl_init_alive_start(struct iwl_priv *priv);
166int iwl_run_init_ucode(struct iwl_priv *priv);
167int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
168 enum iwl_ucode_type ucode_type);
169int iwl_send_calib_results(struct iwl_priv *priv);
170int iwl_calib_set(struct iwl_priv *priv,
171 const struct iwl_calib_hdr *cmd, int len);
172void iwl_calib_free_results(struct iwl_priv *priv);
173int iwl_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
174 char **buf, bool display);
175int iwlagn_hw_valid_rtc_data_addr(u32 addr);
176
177/* lib */
178int iwlagn_send_tx_power(struct iwl_priv *priv);
179void iwlagn_temperature(struct iwl_priv *priv);
180int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control);
181void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control);
182int iwlagn_send_beacon_cmd(struct iwl_priv *priv);
183int iwl_send_statistics_request(struct iwl_priv *priv,
184 u8 flags, bool clear);
185
186static inline const struct ieee80211_supported_band *iwl_get_hw_mode(
187 struct iwl_priv *priv, enum ieee80211_band band)
188{
189 return priv->hw->wiphy->bands[band];
190}
191
192#ifdef CONFIG_PM_SLEEP
193int iwlagn_send_patterns(struct iwl_priv *priv,
194 struct cfg80211_wowlan *wowlan);
195int iwlagn_suspend(struct iwl_priv *priv, struct cfg80211_wowlan *wowlan);
196#endif
197
198/* rx */
199int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band);
200void iwl_setup_rx_handlers(struct iwl_priv *priv);
201void iwl_chswitch_done(struct iwl_priv *priv, bool is_success);
202
203
204/* tx */
205int iwlagn_tx_skb(struct iwl_priv *priv, struct sk_buff *skb);
206int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
207 struct ieee80211_sta *sta, u16 tid, u16 *ssn);
208int iwlagn_tx_agg_oper(struct iwl_priv *priv, struct ieee80211_vif *vif,
209 struct ieee80211_sta *sta, u16 tid, u8 buf_size);
210int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
211 struct ieee80211_sta *sta, u16 tid);
212int iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
213 struct iwl_rx_cmd_buffer *rxb,
214 struct iwl_device_cmd *cmd);
215int iwlagn_rx_reply_tx(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
216 struct iwl_device_cmd *cmd);
217
218static inline u32 iwl_tx_status_to_mac80211(u32 status)
219{
220 status &= TX_STATUS_MSK;
221
222 switch (status) {
223 case TX_STATUS_SUCCESS:
224 case TX_STATUS_DIRECT_DONE:
225 return IEEE80211_TX_STAT_ACK;
226 case TX_STATUS_FAIL_DEST_PS:
227 case TX_STATUS_FAIL_PASSIVE_NO_RX:
228 return IEEE80211_TX_STAT_TX_FILTERED;
229 default:
230 return 0;
231 }
232}
233
234static inline bool iwl_is_tx_success(u32 status)
235{
236 status &= TX_STATUS_MSK;
237 return (status == TX_STATUS_SUCCESS) ||
238 (status == TX_STATUS_DIRECT_DONE);
239}
240
241u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant_idx, u8 valid);
242
243/* scan */
244void iwlagn_post_scan(struct iwl_priv *priv);
245void iwlagn_disable_roc(struct iwl_priv *priv);
246int iwl_force_rf_reset(struct iwl_priv *priv, bool external);
247void iwl_init_scan_params(struct iwl_priv *priv);
248int iwl_scan_cancel(struct iwl_priv *priv);
249void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms);
250void iwl_force_scan_end(struct iwl_priv *priv);
251void iwl_internal_short_hw_scan(struct iwl_priv *priv);
252void iwl_setup_rx_scan_handlers(struct iwl_priv *priv);
253void iwl_setup_scan_deferred_work(struct iwl_priv *priv);
254void iwl_cancel_scan_deferred_work(struct iwl_priv *priv);
255int __must_check iwl_scan_initiate(struct iwl_priv *priv,
256 struct ieee80211_vif *vif,
257 enum iwl_scan_type scan_type,
258 enum ieee80211_band band);
259
260void iwl_scan_roc_expired(struct iwl_priv *priv);
261void iwl_scan_offchannel_skb(struct iwl_priv *priv);
262void iwl_scan_offchannel_skb_status(struct iwl_priv *priv);
263
264/* For faster active scanning, scan will move to the next channel if fewer than
265 * PLCP_QUIET_THRESH packets are heard on this channel within
266 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
267 * time if it's a quiet channel (nothing responded to our probe, and there's
268 * no other traffic).
269 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
270#define IWL_ACTIVE_QUIET_TIME cpu_to_le16(10) /* msec */
271#define IWL_PLCP_QUIET_THRESH cpu_to_le16(1) /* packets */
272
273#define IWL_SCAN_CHECK_WATCHDOG (HZ * 7)
274
275
276/* bt coex */
277void iwlagn_send_advance_bt_config(struct iwl_priv *priv);
278int iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
279 struct iwl_rx_cmd_buffer *rxb,
280 struct iwl_device_cmd *cmd);
281void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv);
282void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv);
283void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv);
284void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv);
285void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena);
286
287static inline bool iwl_advanced_bt_coexist(struct iwl_priv *priv)
288{
289 return priv->cfg->bt_params &&
290 priv->cfg->bt_params->advanced_bt_coexist;
291}
292
293#ifdef CONFIG_IWLWIFI_DEBUG
294const char *iwl_get_tx_fail_reason(u32 status);
295const char *iwl_get_agg_tx_fail_reason(u16 status);
296#else
297static inline const char *iwl_get_tx_fail_reason(u32 status) { return ""; }
298static inline const char *iwl_get_agg_tx_fail_reason(u16 status) { return ""; }
299#endif
300
301
302/* station management */
303int iwlagn_manage_ibss_station(struct iwl_priv *priv,
304 struct ieee80211_vif *vif, bool add);
305#define IWL_STA_DRIVER_ACTIVE BIT(0) /* driver entry is active */
306#define IWL_STA_UCODE_ACTIVE BIT(1) /* ucode entry is active */
307#define IWL_STA_UCODE_INPROGRESS BIT(2) /* ucode entry is in process of
308 being activated */
309#define IWL_STA_LOCAL BIT(3) /* station state not directed by mac80211;
310 (this is for the IBSS BSSID stations) */
311#define IWL_STA_BCAST BIT(4) /* this station is the special bcast station */
312
313
314void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx);
315void iwl_clear_ucode_stations(struct iwl_priv *priv,
316 struct iwl_rxon_context *ctx);
317void iwl_dealloc_bcast_stations(struct iwl_priv *priv);
318int iwl_get_free_ucode_key_offset(struct iwl_priv *priv);
319int iwl_send_add_sta(struct iwl_priv *priv,
320 struct iwl_addsta_cmd *sta, u8 flags);
321int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
322 const u8 *addr, bool is_ap,
323 struct ieee80211_sta *sta, u8 *sta_id_r);
324int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
325 const u8 *addr);
326void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
327 const u8 *addr);
328u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
329 const u8 *addr, bool is_ap, struct ieee80211_sta *sta);
330
331int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
332 struct iwl_link_quality_cmd *lq, u8 flags, bool init);
333int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
334 struct iwl_device_cmd *cmd);
335int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
336 struct ieee80211_sta *sta);
337
338bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
339 struct iwl_rxon_context *ctx,
340 struct ieee80211_sta_ht_cap *ht_cap);
341
342static inline int iwl_sta_id(struct ieee80211_sta *sta)
343{
344 if (WARN_ON(!sta))
345 return IWL_INVALID_STATION;
346
347 return ((struct iwl_station_priv *)sta->drv_priv)->sta_id;
348}
349
350int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
351 struct iwl_rxon_context *ctx);
352int iwlagn_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
353 const u8 *addr, u8 *sta_id_r);
354int iwl_remove_default_wep_key(struct iwl_priv *priv,
355 struct iwl_rxon_context *ctx,
356 struct ieee80211_key_conf *key);
357int iwl_set_default_wep_key(struct iwl_priv *priv,
358 struct iwl_rxon_context *ctx,
359 struct ieee80211_key_conf *key);
360int iwl_restore_default_wep_keys(struct iwl_priv *priv,
361 struct iwl_rxon_context *ctx);
362int iwl_set_dynamic_key(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
363 struct ieee80211_key_conf *key,
364 struct ieee80211_sta *sta);
365int iwl_remove_dynamic_key(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
366 struct ieee80211_key_conf *key,
367 struct ieee80211_sta *sta);
368void iwl_update_tkip_key(struct iwl_priv *priv,
369 struct ieee80211_vif *vif,
370 struct ieee80211_key_conf *keyconf,
371 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key);
372int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid);
373int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
374 int tid, u16 ssn);
375int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
376 int tid);
377void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt);
378int iwl_update_bcast_station(struct iwl_priv *priv,
379 struct iwl_rxon_context *ctx);
380int iwl_update_bcast_stations(struct iwl_priv *priv);
381
382/* rate */
383static inline u32 iwl_ant_idx_to_flags(u8 ant_idx)
384{
385 return BIT(ant_idx) << RATE_MCS_ANT_POS;
386}
387
388static inline u8 iwl_hw_get_rate(__le32 rate_n_flags)
389{
390 return le32_to_cpu(rate_n_flags) & RATE_MCS_RATE_MSK;
391}
392
393static inline __le32 iwl_hw_set_rate_n_flags(u8 rate, u32 flags)
394{
395 return cpu_to_le32(flags|(u32)rate);
396}
397
398extern int iwl_alive_start(struct iwl_priv *priv);
399/* svtool */
400#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
401extern int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data,
402 int len);
403extern int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw,
404 struct sk_buff *skb,
405 struct netlink_callback *cb,
406 void *data, int len);
407extern void iwl_testmode_init(struct iwl_priv *priv);
408extern void iwl_testmode_cleanup(struct iwl_priv *priv);
409#else
410static inline
411int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
412{
413 return -ENOSYS;
414}
415static inline
416int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
417 struct netlink_callback *cb,
418 void *data, int len)
419{
420 return -ENOSYS;
421}
422static inline
423void iwl_testmode_init(struct iwl_priv *priv)
424{
425}
426static inline
427void iwl_testmode_cleanup(struct iwl_priv *priv)
428{
429}
430#endif
431
432#ifdef CONFIG_IWLWIFI_DEBUG
433void iwl_print_rx_config_cmd(struct iwl_priv *priv,
434 enum iwl_rxon_context_id ctxid);
435#else
436static inline void iwl_print_rx_config_cmd(struct iwl_priv *priv,
437 enum iwl_rxon_context_id ctxid)
438{
439}
440#endif
441
442/* status checks */
443
444static inline int iwl_is_ready(struct iwl_priv *priv)
445{
446 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
447 * set but EXIT_PENDING is not */
448 return test_bit(STATUS_READY, &priv->status) &&
449 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
450 !test_bit(STATUS_EXIT_PENDING, &priv->status);
451}
452
453static inline int iwl_is_alive(struct iwl_priv *priv)
454{
455 return test_bit(STATUS_ALIVE, &priv->status);
456}
457
458static inline int iwl_is_rfkill(struct iwl_priv *priv)
459{
460 return test_bit(STATUS_RF_KILL_HW, &priv->status);
461}
462
463static inline int iwl_is_ctkill(struct iwl_priv *priv)
464{
465 return test_bit(STATUS_CT_KILL, &priv->status);
466}
467
468static inline int iwl_is_ready_rf(struct iwl_priv *priv)
469{
470 if (iwl_is_rfkill(priv))
471 return 0;
472
473 return iwl_is_ready(priv);
474}
475
476static inline void iwl_dvm_set_pmi(struct iwl_priv *priv, bool state)
477{
478 if (state)
479 set_bit(STATUS_POWER_PMI, &priv->status);
480 else
481 clear_bit(STATUS_POWER_PMI, &priv->status);
482 iwl_trans_set_pmi(priv->trans, state);
483}
484
485#ifdef CONFIG_IWLWIFI_DEBUGFS
486int iwl_dbgfs_register(struct iwl_priv *priv, const char *name);
487void iwl_dbgfs_unregister(struct iwl_priv *priv);
488#else
489static inline int iwl_dbgfs_register(struct iwl_priv *priv, const char *name)
490{
491 return 0;
492}
493static inline void iwl_dbgfs_unregister(struct iwl_priv *priv)
494{
495}
496#endif /* CONFIG_IWLWIFI_DEBUGFS */
497
498#ifdef CONFIG_IWLWIFI_DEBUG
499#define IWL_DEBUG_QUIET_RFKILL(m, fmt, args...) \
500do { \
501 if (!iwl_is_rfkill((m))) \
502 IWL_ERR(m, fmt, ##args); \
503 else \
504 __iwl_err((m)->dev, true, \
505 !iwl_have_debug_level(IWL_DL_RADIO), \
506 fmt, ##args); \
507} while (0)
508#else
509#define IWL_DEBUG_QUIET_RFKILL(m, fmt, args...) \
510do { \
511 if (!iwl_is_rfkill((m))) \
512 IWL_ERR(m, fmt, ##args); \
513 else \
514 __iwl_err((m)->dev, true, true, fmt, ##args); \
515} while (0)
516#endif /* CONFIG_IWLWIFI_DEBUG */
517
518extern const char *iwl_dvm_cmd_strings[REPLY_MAX];
519
520static inline const char *iwl_dvm_get_cmd_string(u8 cmd)
521{
522 const char *s = iwl_dvm_cmd_strings[cmd];
523 if (s)
524 return s;
525 return "UNKNOWN";
526}
527#endif /* __iwl_agn_h__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/calib.c b/drivers/net/wireless/iwlwifi/dvm/calib.c
new file mode 100644
index 000000000000..7eb3fa3df7c4
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/calib.c
@@ -0,0 +1,1113 @@
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62
63#include <linux/slab.h>
64#include <net/mac80211.h>
65
66#include "iwl-trans.h"
67
68#include "dev.h"
69#include "calib.h"
70#include "agn.h"
71
72/*****************************************************************************
73 * INIT calibrations framework
74 *****************************************************************************/
75
76/* Opaque calibration results */
77struct iwl_calib_result {
78 struct list_head list;
79 size_t cmd_len;
80 struct iwl_calib_hdr hdr;
81 /* data follows */
82};
83
84struct statistics_general_data {
85 u32 beacon_silence_rssi_a;
86 u32 beacon_silence_rssi_b;
87 u32 beacon_silence_rssi_c;
88 u32 beacon_energy_a;
89 u32 beacon_energy_b;
90 u32 beacon_energy_c;
91};
92
93int iwl_send_calib_results(struct iwl_priv *priv)
94{
95 struct iwl_host_cmd hcmd = {
96 .id = REPLY_PHY_CALIBRATION_CMD,
97 .flags = CMD_SYNC,
98 };
99 struct iwl_calib_result *res;
100
101 list_for_each_entry(res, &priv->calib_results, list) {
102 int ret;
103
104 hcmd.len[0] = res->cmd_len;
105 hcmd.data[0] = &res->hdr;
106 hcmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
107 ret = iwl_dvm_send_cmd(priv, &hcmd);
108 if (ret) {
109 IWL_ERR(priv, "Error %d on calib cmd %d\n",
110 ret, res->hdr.op_code);
111 return ret;
112 }
113 }
114
115 return 0;
116}
117
118int iwl_calib_set(struct iwl_priv *priv,
119 const struct iwl_calib_hdr *cmd, int len)
120{
121 struct iwl_calib_result *res, *tmp;
122
123 res = kmalloc(sizeof(*res) + len - sizeof(struct iwl_calib_hdr),
124 GFP_ATOMIC);
125 if (!res)
126 return -ENOMEM;
127 memcpy(&res->hdr, cmd, len);
128 res->cmd_len = len;
129
130 list_for_each_entry(tmp, &priv->calib_results, list) {
131 if (tmp->hdr.op_code == res->hdr.op_code) {
132 list_replace(&tmp->list, &res->list);
133 kfree(tmp);
134 return 0;
135 }
136 }
137
138 /* wasn't in list already */
139 list_add_tail(&res->list, &priv->calib_results);
140
141 return 0;
142}
143
144void iwl_calib_free_results(struct iwl_priv *priv)
145{
146 struct iwl_calib_result *res, *tmp;
147
148 list_for_each_entry_safe(res, tmp, &priv->calib_results, list) {
149 list_del(&res->list);
150 kfree(res);
151 }
152}
153
154/*****************************************************************************
155 * RUNTIME calibrations framework
156 *****************************************************************************/
157
158/* "false alarms" are signals that our DSP tries to lock onto,
159 * but then determines that they are either noise, or transmissions
160 * from a distant wireless network (also "noise", really) that get
161 * "stepped on" by stronger transmissions within our own network.
162 * This algorithm attempts to set a sensitivity level that is high
163 * enough to receive all of our own network traffic, but not so
164 * high that our DSP gets too busy trying to lock onto non-network
165 * activity/noise. */
166static int iwl_sens_energy_cck(struct iwl_priv *priv,
167 u32 norm_fa,
168 u32 rx_enable_time,
169 struct statistics_general_data *rx_info)
170{
171 u32 max_nrg_cck = 0;
172 int i = 0;
173 u8 max_silence_rssi = 0;
174 u32 silence_ref = 0;
175 u8 silence_rssi_a = 0;
176 u8 silence_rssi_b = 0;
177 u8 silence_rssi_c = 0;
178 u32 val;
179
180 /* "false_alarms" values below are cross-multiplications to assess the
181 * numbers of false alarms within the measured period of actual Rx
182 * (Rx is off when we're txing), vs the min/max expected false alarms
183 * (some should be expected if rx is sensitive enough) in a
184 * hypothetical listening period of 200 time units (TU), 204.8 msec:
185 *
186 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
187 *
188 * */
189 u32 false_alarms = norm_fa * 200 * 1024;
190 u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
191 u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
192 struct iwl_sensitivity_data *data = NULL;
193 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
194
195 data = &(priv->sensitivity_data);
196
197 data->nrg_auto_corr_silence_diff = 0;
198
199 /* Find max silence rssi among all 3 receivers.
200 * This is background noise, which may include transmissions from other
201 * networks, measured during silence before our network's beacon */
202 silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
203 ALL_BAND_FILTER) >> 8);
204 silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
205 ALL_BAND_FILTER) >> 8);
206 silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
207 ALL_BAND_FILTER) >> 8);
208
209 val = max(silence_rssi_b, silence_rssi_c);
210 max_silence_rssi = max(silence_rssi_a, (u8) val);
211
212 /* Store silence rssi in 20-beacon history table */
213 data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
214 data->nrg_silence_idx++;
215 if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
216 data->nrg_silence_idx = 0;
217
218 /* Find max silence rssi across 20 beacon history */
219 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
220 val = data->nrg_silence_rssi[i];
221 silence_ref = max(silence_ref, val);
222 }
223 IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n",
224 silence_rssi_a, silence_rssi_b, silence_rssi_c,
225 silence_ref);
226
227 /* Find max rx energy (min value!) among all 3 receivers,
228 * measured during beacon frame.
229 * Save it in 10-beacon history table. */
230 i = data->nrg_energy_idx;
231 val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
232 data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
233
234 data->nrg_energy_idx++;
235 if (data->nrg_energy_idx >= 10)
236 data->nrg_energy_idx = 0;
237
238 /* Find min rx energy (max value) across 10 beacon history.
239 * This is the minimum signal level that we want to receive well.
240 * Add backoff (margin so we don't miss slightly lower energy frames).
241 * This establishes an upper bound (min value) for energy threshold. */
242 max_nrg_cck = data->nrg_value[0];
243 for (i = 1; i < 10; i++)
244 max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
245 max_nrg_cck += 6;
246
247 IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
248 rx_info->beacon_energy_a, rx_info->beacon_energy_b,
249 rx_info->beacon_energy_c, max_nrg_cck - 6);
250
251 /* Count number of consecutive beacons with fewer-than-desired
252 * false alarms. */
253 if (false_alarms < min_false_alarms)
254 data->num_in_cck_no_fa++;
255 else
256 data->num_in_cck_no_fa = 0;
257 IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n",
258 data->num_in_cck_no_fa);
259
260 /* If we got too many false alarms this time, reduce sensitivity */
261 if ((false_alarms > max_false_alarms) &&
262 (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) {
263 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n",
264 false_alarms, max_false_alarms);
265 IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n");
266 data->nrg_curr_state = IWL_FA_TOO_MANY;
267 /* Store for "fewer than desired" on later beacon */
268 data->nrg_silence_ref = silence_ref;
269
270 /* increase energy threshold (reduce nrg value)
271 * to decrease sensitivity */
272 data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK;
273 /* Else if we got fewer than desired, increase sensitivity */
274 } else if (false_alarms < min_false_alarms) {
275 data->nrg_curr_state = IWL_FA_TOO_FEW;
276
277 /* Compare silence level with silence level for most recent
278 * healthy number or too many false alarms */
279 data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
280 (s32)silence_ref;
281
282 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n",
283 false_alarms, min_false_alarms,
284 data->nrg_auto_corr_silence_diff);
285
286 /* Increase value to increase sensitivity, but only if:
287 * 1a) previous beacon did *not* have *too many* false alarms
288 * 1b) AND there's a significant difference in Rx levels
289 * from a previous beacon with too many, or healthy # FAs
290 * OR 2) We've seen a lot of beacons (100) with too few
291 * false alarms */
292 if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
293 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
294 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
295
296 IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n");
297 /* Increase nrg value to increase sensitivity */
298 val = data->nrg_th_cck + NRG_STEP_CCK;
299 data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val);
300 } else {
301 IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n");
302 }
303
304 /* Else we got a healthy number of false alarms, keep status quo */
305 } else {
306 IWL_DEBUG_CALIB(priv, " FA in safe zone\n");
307 data->nrg_curr_state = IWL_FA_GOOD_RANGE;
308
309 /* Store for use in "fewer than desired" with later beacon */
310 data->nrg_silence_ref = silence_ref;
311
312 /* If previous beacon had too many false alarms,
313 * give it some extra margin by reducing sensitivity again
314 * (but don't go below measured energy of desired Rx) */
315 if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
316 IWL_DEBUG_CALIB(priv, "... increasing margin\n");
317 if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN))
318 data->nrg_th_cck -= NRG_MARGIN;
319 else
320 data->nrg_th_cck = max_nrg_cck;
321 }
322 }
323
324 /* Make sure the energy threshold does not go above the measured
325 * energy of the desired Rx signals (reduced by backoff margin),
326 * or else we might start missing Rx frames.
327 * Lower value is higher energy, so we use max()!
328 */
329 data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
330 IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck);
331
332 data->nrg_prev_state = data->nrg_curr_state;
333
334 /* Auto-correlation CCK algorithm */
335 if (false_alarms > min_false_alarms) {
336
337 /* increase auto_corr values to decrease sensitivity
338 * so the DSP won't be disturbed by the noise
339 */
340 if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
341 data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
342 else {
343 val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
344 data->auto_corr_cck =
345 min((u32)ranges->auto_corr_max_cck, val);
346 }
347 val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
348 data->auto_corr_cck_mrc =
349 min((u32)ranges->auto_corr_max_cck_mrc, val);
350 } else if ((false_alarms < min_false_alarms) &&
351 ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
352 (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
353
354 /* Decrease auto_corr values to increase sensitivity */
355 val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
356 data->auto_corr_cck =
357 max((u32)ranges->auto_corr_min_cck, val);
358 val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
359 data->auto_corr_cck_mrc =
360 max((u32)ranges->auto_corr_min_cck_mrc, val);
361 }
362
363 return 0;
364}
365
366
367static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv,
368 u32 norm_fa,
369 u32 rx_enable_time)
370{
371 u32 val;
372 u32 false_alarms = norm_fa * 200 * 1024;
373 u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
374 u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
375 struct iwl_sensitivity_data *data = NULL;
376 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
377
378 data = &(priv->sensitivity_data);
379
380 /* If we got too many false alarms this time, reduce sensitivity */
381 if (false_alarms > max_false_alarms) {
382
383 IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n",
384 false_alarms, max_false_alarms);
385
386 val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
387 data->auto_corr_ofdm =
388 min((u32)ranges->auto_corr_max_ofdm, val);
389
390 val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
391 data->auto_corr_ofdm_mrc =
392 min((u32)ranges->auto_corr_max_ofdm_mrc, val);
393
394 val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
395 data->auto_corr_ofdm_x1 =
396 min((u32)ranges->auto_corr_max_ofdm_x1, val);
397
398 val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
399 data->auto_corr_ofdm_mrc_x1 =
400 min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val);
401 }
402
403 /* Else if we got fewer than desired, increase sensitivity */
404 else if (false_alarms < min_false_alarms) {
405
406 IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n",
407 false_alarms, min_false_alarms);
408
409 val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
410 data->auto_corr_ofdm =
411 max((u32)ranges->auto_corr_min_ofdm, val);
412
413 val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
414 data->auto_corr_ofdm_mrc =
415 max((u32)ranges->auto_corr_min_ofdm_mrc, val);
416
417 val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
418 data->auto_corr_ofdm_x1 =
419 max((u32)ranges->auto_corr_min_ofdm_x1, val);
420
421 val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
422 data->auto_corr_ofdm_mrc_x1 =
423 max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val);
424 } else {
425 IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n",
426 min_false_alarms, false_alarms, max_false_alarms);
427 }
428 return 0;
429}
430
431static void iwl_prepare_legacy_sensitivity_tbl(struct iwl_priv *priv,
432 struct iwl_sensitivity_data *data,
433 __le16 *tbl)
434{
435 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
436 cpu_to_le16((u16)data->auto_corr_ofdm);
437 tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
438 cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
439 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
440 cpu_to_le16((u16)data->auto_corr_ofdm_x1);
441 tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
442 cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
443
444 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
445 cpu_to_le16((u16)data->auto_corr_cck);
446 tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
447 cpu_to_le16((u16)data->auto_corr_cck_mrc);
448
449 tbl[HD_MIN_ENERGY_CCK_DET_INDEX] =
450 cpu_to_le16((u16)data->nrg_th_cck);
451 tbl[HD_MIN_ENERGY_OFDM_DET_INDEX] =
452 cpu_to_le16((u16)data->nrg_th_ofdm);
453
454 tbl[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
455 cpu_to_le16(data->barker_corr_th_min);
456 tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
457 cpu_to_le16(data->barker_corr_th_min_mrc);
458 tbl[HD_OFDM_ENERGY_TH_IN_INDEX] =
459 cpu_to_le16(data->nrg_th_cca);
460
461 IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
462 data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
463 data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
464 data->nrg_th_ofdm);
465
466 IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n",
467 data->auto_corr_cck, data->auto_corr_cck_mrc,
468 data->nrg_th_cck);
469}
470
471/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
472static int iwl_sensitivity_write(struct iwl_priv *priv)
473{
474 struct iwl_sensitivity_cmd cmd;
475 struct iwl_sensitivity_data *data = NULL;
476 struct iwl_host_cmd cmd_out = {
477 .id = SENSITIVITY_CMD,
478 .len = { sizeof(struct iwl_sensitivity_cmd), },
479 .flags = CMD_ASYNC,
480 .data = { &cmd, },
481 };
482
483 data = &(priv->sensitivity_data);
484
485 memset(&cmd, 0, sizeof(cmd));
486
487 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.table[0]);
488
489 /* Update uCode's "work" table, and copy it to DSP */
490 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
491
492 /* Don't send command to uCode if nothing has changed */
493 if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
494 sizeof(u16)*HD_TABLE_SIZE)) {
495 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
496 return 0;
497 }
498
499 /* Copy table for comparison next time */
500 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
501 sizeof(u16)*HD_TABLE_SIZE);
502
503 return iwl_dvm_send_cmd(priv, &cmd_out);
504}
505
506/* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
507static int iwl_enhance_sensitivity_write(struct iwl_priv *priv)
508{
509 struct iwl_enhance_sensitivity_cmd cmd;
510 struct iwl_sensitivity_data *data = NULL;
511 struct iwl_host_cmd cmd_out = {
512 .id = SENSITIVITY_CMD,
513 .len = { sizeof(struct iwl_enhance_sensitivity_cmd), },
514 .flags = CMD_ASYNC,
515 .data = { &cmd, },
516 };
517
518 data = &(priv->sensitivity_data);
519
520 memset(&cmd, 0, sizeof(cmd));
521
522 iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.enhance_table[0]);
523
524 if (priv->cfg->base_params->hd_v2) {
525 cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
526 HD_INA_NON_SQUARE_DET_OFDM_DATA_V2;
527 cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
528 HD_INA_NON_SQUARE_DET_CCK_DATA_V2;
529 cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
530 HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V2;
531 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
532 HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V2;
533 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
534 HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2;
535 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
536 HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V2;
537 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
538 HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V2;
539 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
540 HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V2;
541 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
542 HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2;
543 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
544 HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V2;
545 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
546 HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V2;
547 } else {
548 cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] =
549 HD_INA_NON_SQUARE_DET_OFDM_DATA_V1;
550 cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] =
551 HD_INA_NON_SQUARE_DET_CCK_DATA_V1;
552 cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] =
553 HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V1;
554 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
555 HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V1;
556 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
557 HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1;
558 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] =
559 HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V1;
560 cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] =
561 HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V1;
562 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] =
563 HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V1;
564 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] =
565 HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1;
566 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] =
567 HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V1;
568 cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] =
569 HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V1;
570 }
571
572 /* Update uCode's "work" table, and copy it to DSP */
573 cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
574
575 /* Don't send command to uCode if nothing has changed */
576 if (!memcmp(&cmd.enhance_table[0], &(priv->sensitivity_tbl[0]),
577 sizeof(u16)*HD_TABLE_SIZE) &&
578 !memcmp(&cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX],
579 &(priv->enhance_sensitivity_tbl[0]),
580 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES)) {
581 IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n");
582 return 0;
583 }
584
585 /* Copy table for comparison next time */
586 memcpy(&(priv->sensitivity_tbl[0]), &(cmd.enhance_table[0]),
587 sizeof(u16)*HD_TABLE_SIZE);
588 memcpy(&(priv->enhance_sensitivity_tbl[0]),
589 &(cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX]),
590 sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES);
591
592 return iwl_dvm_send_cmd(priv, &cmd_out);
593}
594
595void iwl_init_sensitivity(struct iwl_priv *priv)
596{
597 int ret = 0;
598 int i;
599 struct iwl_sensitivity_data *data = NULL;
600 const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens;
601
602 if (priv->calib_disabled & IWL_SENSITIVITY_CALIB_DISABLED)
603 return;
604
605 IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n");
606
607 /* Clear driver's sensitivity algo data */
608 data = &(priv->sensitivity_data);
609
610 if (ranges == NULL)
611 return;
612
613 memset(data, 0, sizeof(struct iwl_sensitivity_data));
614
615 data->num_in_cck_no_fa = 0;
616 data->nrg_curr_state = IWL_FA_TOO_MANY;
617 data->nrg_prev_state = IWL_FA_TOO_MANY;
618 data->nrg_silence_ref = 0;
619 data->nrg_silence_idx = 0;
620 data->nrg_energy_idx = 0;
621
622 for (i = 0; i < 10; i++)
623 data->nrg_value[i] = 0;
624
625 for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
626 data->nrg_silence_rssi[i] = 0;
627
628 data->auto_corr_ofdm = ranges->auto_corr_min_ofdm;
629 data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc;
630 data->auto_corr_ofdm_x1 = ranges->auto_corr_min_ofdm_x1;
631 data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1;
632 data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
633 data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc;
634 data->nrg_th_cck = ranges->nrg_th_cck;
635 data->nrg_th_ofdm = ranges->nrg_th_ofdm;
636 data->barker_corr_th_min = ranges->barker_corr_th_min;
637 data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc;
638 data->nrg_th_cca = ranges->nrg_th_cca;
639
640 data->last_bad_plcp_cnt_ofdm = 0;
641 data->last_fa_cnt_ofdm = 0;
642 data->last_bad_plcp_cnt_cck = 0;
643 data->last_fa_cnt_cck = 0;
644
645 if (priv->fw->enhance_sensitivity_table)
646 ret |= iwl_enhance_sensitivity_write(priv);
647 else
648 ret |= iwl_sensitivity_write(priv);
649 IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret);
650}
651
652void iwl_sensitivity_calibration(struct iwl_priv *priv)
653{
654 u32 rx_enable_time;
655 u32 fa_cck;
656 u32 fa_ofdm;
657 u32 bad_plcp_cck;
658 u32 bad_plcp_ofdm;
659 u32 norm_fa_ofdm;
660 u32 norm_fa_cck;
661 struct iwl_sensitivity_data *data = NULL;
662 struct statistics_rx_non_phy *rx_info;
663 struct statistics_rx_phy *ofdm, *cck;
664 struct statistics_general_data statis;
665
666 if (priv->calib_disabled & IWL_SENSITIVITY_CALIB_DISABLED)
667 return;
668
669 data = &(priv->sensitivity_data);
670
671 if (!iwl_is_any_associated(priv)) {
672 IWL_DEBUG_CALIB(priv, "<< - not associated\n");
673 return;
674 }
675
676 spin_lock_bh(&priv->statistics.lock);
677 rx_info = &priv->statistics.rx_non_phy;
678 ofdm = &priv->statistics.rx_ofdm;
679 cck = &priv->statistics.rx_cck;
680 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
681 IWL_DEBUG_CALIB(priv, "<< invalid data.\n");
682 spin_unlock_bh(&priv->statistics.lock);
683 return;
684 }
685
686 /* Extract Statistics: */
687 rx_enable_time = le32_to_cpu(rx_info->channel_load);
688 fa_cck = le32_to_cpu(cck->false_alarm_cnt);
689 fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt);
690 bad_plcp_cck = le32_to_cpu(cck->plcp_err);
691 bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err);
692
693 statis.beacon_silence_rssi_a =
694 le32_to_cpu(rx_info->beacon_silence_rssi_a);
695 statis.beacon_silence_rssi_b =
696 le32_to_cpu(rx_info->beacon_silence_rssi_b);
697 statis.beacon_silence_rssi_c =
698 le32_to_cpu(rx_info->beacon_silence_rssi_c);
699 statis.beacon_energy_a =
700 le32_to_cpu(rx_info->beacon_energy_a);
701 statis.beacon_energy_b =
702 le32_to_cpu(rx_info->beacon_energy_b);
703 statis.beacon_energy_c =
704 le32_to_cpu(rx_info->beacon_energy_c);
705
706 spin_unlock_bh(&priv->statistics.lock);
707
708 IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time);
709
710 if (!rx_enable_time) {
711 IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n");
712 return;
713 }
714
715 /* These statistics increase monotonically, and do not reset
716 * at each beacon. Calculate difference from last value, or just
717 * use the new statistics value if it has reset or wrapped around. */
718 if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
719 data->last_bad_plcp_cnt_cck = bad_plcp_cck;
720 else {
721 bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
722 data->last_bad_plcp_cnt_cck += bad_plcp_cck;
723 }
724
725 if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
726 data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
727 else {
728 bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
729 data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
730 }
731
732 if (data->last_fa_cnt_ofdm > fa_ofdm)
733 data->last_fa_cnt_ofdm = fa_ofdm;
734 else {
735 fa_ofdm -= data->last_fa_cnt_ofdm;
736 data->last_fa_cnt_ofdm += fa_ofdm;
737 }
738
739 if (data->last_fa_cnt_cck > fa_cck)
740 data->last_fa_cnt_cck = fa_cck;
741 else {
742 fa_cck -= data->last_fa_cnt_cck;
743 data->last_fa_cnt_cck += fa_cck;
744 }
745
746 /* Total aborted signal locks */
747 norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
748 norm_fa_cck = fa_cck + bad_plcp_cck;
749
750 IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
751 bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
752
753 iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
754 iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
755 if (priv->fw->enhance_sensitivity_table)
756 iwl_enhance_sensitivity_write(priv);
757 else
758 iwl_sensitivity_write(priv);
759}
760
761static inline u8 find_first_chain(u8 mask)
762{
763 if (mask & ANT_A)
764 return CHAIN_A;
765 if (mask & ANT_B)
766 return CHAIN_B;
767 return CHAIN_C;
768}
769
770/**
771 * Run disconnected antenna algorithm to find out which antennas are
772 * disconnected.
773 */
774static void iwl_find_disconn_antenna(struct iwl_priv *priv, u32* average_sig,
775 struct iwl_chain_noise_data *data)
776{
777 u32 active_chains = 0;
778 u32 max_average_sig;
779 u16 max_average_sig_antenna_i;
780 u8 num_tx_chains;
781 u8 first_chain;
782 u16 i = 0;
783
784 average_sig[0] = data->chain_signal_a / IWL_CAL_NUM_BEACONS;
785 average_sig[1] = data->chain_signal_b / IWL_CAL_NUM_BEACONS;
786 average_sig[2] = data->chain_signal_c / IWL_CAL_NUM_BEACONS;
787
788 if (average_sig[0] >= average_sig[1]) {
789 max_average_sig = average_sig[0];
790 max_average_sig_antenna_i = 0;
791 active_chains = (1 << max_average_sig_antenna_i);
792 } else {
793 max_average_sig = average_sig[1];
794 max_average_sig_antenna_i = 1;
795 active_chains = (1 << max_average_sig_antenna_i);
796 }
797
798 if (average_sig[2] >= max_average_sig) {
799 max_average_sig = average_sig[2];
800 max_average_sig_antenna_i = 2;
801 active_chains = (1 << max_average_sig_antenna_i);
802 }
803
804 IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n",
805 average_sig[0], average_sig[1], average_sig[2]);
806 IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n",
807 max_average_sig, max_average_sig_antenna_i);
808
809 /* Compare signal strengths for all 3 receivers. */
810 for (i = 0; i < NUM_RX_CHAINS; i++) {
811 if (i != max_average_sig_antenna_i) {
812 s32 rssi_delta = (max_average_sig - average_sig[i]);
813
814 /* If signal is very weak, compared with
815 * strongest, mark it as disconnected. */
816 if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
817 data->disconn_array[i] = 1;
818 else
819 active_chains |= (1 << i);
820 IWL_DEBUG_CALIB(priv, "i = %d rssiDelta = %d "
821 "disconn_array[i] = %d\n",
822 i, rssi_delta, data->disconn_array[i]);
823 }
824 }
825
826 /*
827 * The above algorithm sometimes fails when the ucode
828 * reports 0 for all chains. It's not clear why that
829 * happens to start with, but it is then causing trouble
830 * because this can make us enable more chains than the
831 * hardware really has.
832 *
833 * To be safe, simply mask out any chains that we know
834 * are not on the device.
835 */
836 active_chains &= priv->hw_params.valid_rx_ant;
837
838 num_tx_chains = 0;
839 for (i = 0; i < NUM_RX_CHAINS; i++) {
840 /* loops on all the bits of
841 * priv->hw_setting.valid_tx_ant */
842 u8 ant_msk = (1 << i);
843 if (!(priv->hw_params.valid_tx_ant & ant_msk))
844 continue;
845
846 num_tx_chains++;
847 if (data->disconn_array[i] == 0)
848 /* there is a Tx antenna connected */
849 break;
850 if (num_tx_chains == priv->hw_params.tx_chains_num &&
851 data->disconn_array[i]) {
852 /*
853 * If all chains are disconnected
854 * connect the first valid tx chain
855 */
856 first_chain =
857 find_first_chain(priv->hw_params.valid_tx_ant);
858 data->disconn_array[first_chain] = 0;
859 active_chains |= BIT(first_chain);
860 IWL_DEBUG_CALIB(priv,
861 "All Tx chains are disconnected W/A - declare %d as connected\n",
862 first_chain);
863 break;
864 }
865 }
866
867 if (active_chains != priv->hw_params.valid_rx_ant &&
868 active_chains != priv->chain_noise_data.active_chains)
869 IWL_DEBUG_CALIB(priv,
870 "Detected that not all antennas are connected! "
871 "Connected: %#x, valid: %#x.\n",
872 active_chains,
873 priv->hw_params.valid_rx_ant);
874
875 /* Save for use within RXON, TX, SCAN commands, etc. */
876 data->active_chains = active_chains;
877 IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n",
878 active_chains);
879}
880
881static void iwlagn_gain_computation(struct iwl_priv *priv,
882 u32 average_noise[NUM_RX_CHAINS],
883 u8 default_chain)
884{
885 int i;
886 s32 delta_g;
887 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
888
889 /*
890 * Find Gain Code for the chains based on "default chain"
891 */
892 for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
893 if ((data->disconn_array[i])) {
894 data->delta_gain_code[i] = 0;
895 continue;
896 }
897
898 delta_g = (priv->cfg->base_params->chain_noise_scale *
899 ((s32)average_noise[default_chain] -
900 (s32)average_noise[i])) / 1500;
901
902 /* bound gain by 2 bits value max, 3rd bit is sign */
903 data->delta_gain_code[i] =
904 min(abs(delta_g),
905 (long) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
906
907 if (delta_g < 0)
908 /*
909 * set negative sign ...
910 * note to Intel developers: This is uCode API format,
911 * not the format of any internal device registers.
912 * Do not change this format for e.g. 6050 or similar
913 * devices. Change format only if more resolution
914 * (i.e. more than 2 bits magnitude) is needed.
915 */
916 data->delta_gain_code[i] |= (1 << 2);
917 }
918
919 IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
920 data->delta_gain_code[1], data->delta_gain_code[2]);
921
922 if (!data->radio_write) {
923 struct iwl_calib_chain_noise_gain_cmd cmd;
924
925 memset(&cmd, 0, sizeof(cmd));
926
927 iwl_set_calib_hdr(&cmd.hdr,
928 priv->phy_calib_chain_noise_gain_cmd);
929 cmd.delta_gain_1 = data->delta_gain_code[1];
930 cmd.delta_gain_2 = data->delta_gain_code[2];
931 iwl_dvm_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
932 CMD_ASYNC, sizeof(cmd), &cmd);
933
934 data->radio_write = 1;
935 data->state = IWL_CHAIN_NOISE_CALIBRATED;
936 }
937}
938
939/*
940 * Accumulate 16 beacons of signal and noise statistics for each of
941 * 3 receivers/antennas/rx-chains, then figure out:
942 * 1) Which antennas are connected.
943 * 2) Differential rx gain settings to balance the 3 receivers.
944 */
945void iwl_chain_noise_calibration(struct iwl_priv *priv)
946{
947 struct iwl_chain_noise_data *data = NULL;
948
949 u32 chain_noise_a;
950 u32 chain_noise_b;
951 u32 chain_noise_c;
952 u32 chain_sig_a;
953 u32 chain_sig_b;
954 u32 chain_sig_c;
955 u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
956 u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
957 u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
958 u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
959 u16 i = 0;
960 u16 rxon_chnum = INITIALIZATION_VALUE;
961 u16 stat_chnum = INITIALIZATION_VALUE;
962 u8 rxon_band24;
963 u8 stat_band24;
964 struct statistics_rx_non_phy *rx_info;
965
966 /*
967 * MULTI-FIXME:
968 * When we support multiple interfaces on different channels,
969 * this must be modified/fixed.
970 */
971 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
972
973 if (priv->calib_disabled & IWL_CHAIN_NOISE_CALIB_DISABLED)
974 return;
975
976 data = &(priv->chain_noise_data);
977
978 /*
979 * Accumulate just the first "chain_noise_num_beacons" after
980 * the first association, then we're done forever.
981 */
982 if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
983 if (data->state == IWL_CHAIN_NOISE_ALIVE)
984 IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n");
985 return;
986 }
987
988 spin_lock_bh(&priv->statistics.lock);
989
990 rx_info = &priv->statistics.rx_non_phy;
991
992 if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
993 IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n");
994 spin_unlock_bh(&priv->statistics.lock);
995 return;
996 }
997
998 rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK);
999 rxon_chnum = le16_to_cpu(ctx->staging.channel);
1000 stat_band24 =
1001 !!(priv->statistics.flag & STATISTICS_REPLY_FLG_BAND_24G_MSK);
1002 stat_chnum = le32_to_cpu(priv->statistics.flag) >> 16;
1003
1004 /* Make sure we accumulate data for just the associated channel
1005 * (even if scanning). */
1006 if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) {
1007 IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n",
1008 rxon_chnum, rxon_band24);
1009 spin_unlock_bh(&priv->statistics.lock);
1010 return;
1011 }
1012
1013 /*
1014 * Accumulate beacon statistics values across
1015 * "chain_noise_num_beacons"
1016 */
1017 chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
1018 IN_BAND_FILTER;
1019 chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
1020 IN_BAND_FILTER;
1021 chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
1022 IN_BAND_FILTER;
1023
1024 chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
1025 chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
1026 chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
1027
1028 spin_unlock_bh(&priv->statistics.lock);
1029
1030 data->beacon_count++;
1031
1032 data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
1033 data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
1034 data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
1035
1036 data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
1037 data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
1038 data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
1039
1040 IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n",
1041 rxon_chnum, rxon_band24, data->beacon_count);
1042 IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n",
1043 chain_sig_a, chain_sig_b, chain_sig_c);
1044 IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n",
1045 chain_noise_a, chain_noise_b, chain_noise_c);
1046
1047 /* If this is the "chain_noise_num_beacons", determine:
1048 * 1) Disconnected antennas (using signal strengths)
1049 * 2) Differential gain (using silence noise) to balance receivers */
1050 if (data->beacon_count != IWL_CAL_NUM_BEACONS)
1051 return;
1052
1053 /* Analyze signal for disconnected antenna */
1054 if (priv->cfg->bt_params &&
1055 priv->cfg->bt_params->advanced_bt_coexist) {
1056 /* Disable disconnected antenna algorithm for advanced
1057 bt coex, assuming valid antennas are connected */
1058 data->active_chains = priv->hw_params.valid_rx_ant;
1059 for (i = 0; i < NUM_RX_CHAINS; i++)
1060 if (!(data->active_chains & (1<<i)))
1061 data->disconn_array[i] = 1;
1062 } else
1063 iwl_find_disconn_antenna(priv, average_sig, data);
1064
1065 /* Analyze noise for rx balance */
1066 average_noise[0] = data->chain_noise_a / IWL_CAL_NUM_BEACONS;
1067 average_noise[1] = data->chain_noise_b / IWL_CAL_NUM_BEACONS;
1068 average_noise[2] = data->chain_noise_c / IWL_CAL_NUM_BEACONS;
1069
1070 for (i = 0; i < NUM_RX_CHAINS; i++) {
1071 if (!(data->disconn_array[i]) &&
1072 (average_noise[i] <= min_average_noise)) {
1073 /* This means that chain i is active and has
1074 * lower noise values so far: */
1075 min_average_noise = average_noise[i];
1076 min_average_noise_antenna_i = i;
1077 }
1078 }
1079
1080 IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n",
1081 average_noise[0], average_noise[1],
1082 average_noise[2]);
1083
1084 IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n",
1085 min_average_noise, min_average_noise_antenna_i);
1086
1087 iwlagn_gain_computation(priv, average_noise,
1088 find_first_chain(priv->hw_params.valid_rx_ant));
1089
1090 /* Some power changes may have been made during the calibration.
1091 * Update and commit the RXON
1092 */
1093 iwl_update_chain_flags(priv);
1094
1095 data->state = IWL_CHAIN_NOISE_DONE;
1096 iwl_power_update_mode(priv, false);
1097}
1098
1099void iwl_reset_run_time_calib(struct iwl_priv *priv)
1100{
1101 int i;
1102 memset(&(priv->sensitivity_data), 0,
1103 sizeof(struct iwl_sensitivity_data));
1104 memset(&(priv->chain_noise_data), 0,
1105 sizeof(struct iwl_chain_noise_data));
1106 for (i = 0; i < NUM_RX_CHAINS; i++)
1107 priv->chain_noise_data.delta_gain_code[i] =
1108 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
1109
1110 /* Ask for statistics now, the uCode will send notification
1111 * periodically after association */
1112 iwl_send_statistics_request(priv, CMD_ASYNC, true);
1113}
diff --git a/drivers/net/wireless/iwlwifi/dvm/calib.h b/drivers/net/wireless/iwlwifi/dvm/calib.h
new file mode 100644
index 000000000000..2349f393cc42
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/calib.h
@@ -0,0 +1,74 @@
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62#ifndef __iwl_calib_h__
63#define __iwl_calib_h__
64
65#include "dev.h"
66#include "commands.h"
67
68void iwl_chain_noise_calibration(struct iwl_priv *priv);
69void iwl_sensitivity_calibration(struct iwl_priv *priv);
70
71void iwl_init_sensitivity(struct iwl_priv *priv);
72void iwl_reset_run_time_calib(struct iwl_priv *priv);
73
74#endif /* __iwl_calib_h__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/commands.h b/drivers/net/wireless/iwlwifi/dvm/commands.h
new file mode 100644
index 000000000000..64811cd91635
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/commands.h
@@ -0,0 +1,3958 @@
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63/*
64 * Please use this file (commands.h) only for uCode API definitions.
65 * Please use iwl-xxxx-hw.h for hardware-related definitions.
66 * Please use dev.h for driver implementation definitions.
67 */
68
69#ifndef __iwl_commands_h__
70#define __iwl_commands_h__
71
72#include <linux/ieee80211.h>
73#include <linux/types.h>
74
75
76enum {
77 REPLY_ALIVE = 0x1,
78 REPLY_ERROR = 0x2,
79 REPLY_ECHO = 0x3, /* test command */
80
81 /* RXON and QOS commands */
82 REPLY_RXON = 0x10,
83 REPLY_RXON_ASSOC = 0x11,
84 REPLY_QOS_PARAM = 0x13,
85 REPLY_RXON_TIMING = 0x14,
86
87 /* Multi-Station support */
88 REPLY_ADD_STA = 0x18,
89 REPLY_REMOVE_STA = 0x19,
90 REPLY_REMOVE_ALL_STA = 0x1a, /* not used */
91 REPLY_TXFIFO_FLUSH = 0x1e,
92
93 /* Security */
94 REPLY_WEPKEY = 0x20,
95
96 /* RX, TX, LEDs */
97 REPLY_TX = 0x1c,
98 REPLY_LEDS_CMD = 0x48,
99 REPLY_TX_LINK_QUALITY_CMD = 0x4e,
100
101 /* WiMAX coexistence */
102 COEX_PRIORITY_TABLE_CMD = 0x5a,
103 COEX_MEDIUM_NOTIFICATION = 0x5b,
104 COEX_EVENT_CMD = 0x5c,
105
106 /* Calibration */
107 TEMPERATURE_NOTIFICATION = 0x62,
108 CALIBRATION_CFG_CMD = 0x65,
109 CALIBRATION_RES_NOTIFICATION = 0x66,
110 CALIBRATION_COMPLETE_NOTIFICATION = 0x67,
111
112 /* 802.11h related */
113 REPLY_QUIET_CMD = 0x71, /* not used */
114 REPLY_CHANNEL_SWITCH = 0x72,
115 CHANNEL_SWITCH_NOTIFICATION = 0x73,
116 REPLY_SPECTRUM_MEASUREMENT_CMD = 0x74,
117 SPECTRUM_MEASURE_NOTIFICATION = 0x75,
118
119 /* Power Management */
120 POWER_TABLE_CMD = 0x77,
121 PM_SLEEP_NOTIFICATION = 0x7A,
122 PM_DEBUG_STATISTIC_NOTIFIC = 0x7B,
123
124 /* Scan commands and notifications */
125 REPLY_SCAN_CMD = 0x80,
126 REPLY_SCAN_ABORT_CMD = 0x81,
127 SCAN_START_NOTIFICATION = 0x82,
128 SCAN_RESULTS_NOTIFICATION = 0x83,
129 SCAN_COMPLETE_NOTIFICATION = 0x84,
130
131 /* IBSS/AP commands */
132 BEACON_NOTIFICATION = 0x90,
133 REPLY_TX_BEACON = 0x91,
134 WHO_IS_AWAKE_NOTIFICATION = 0x94, /* not used */
135
136 /* Miscellaneous commands */
137 REPLY_TX_POWER_DBM_CMD = 0x95,
138 QUIET_NOTIFICATION = 0x96, /* not used */
139 REPLY_TX_PWR_TABLE_CMD = 0x97,
140 REPLY_TX_POWER_DBM_CMD_V1 = 0x98, /* old version of API */
141 TX_ANT_CONFIGURATION_CMD = 0x98,
142 MEASURE_ABORT_NOTIFICATION = 0x99, /* not used */
143
144 /* Bluetooth device coexistence config command */
145 REPLY_BT_CONFIG = 0x9b,
146
147 /* Statistics */
148 REPLY_STATISTICS_CMD = 0x9c,
149 STATISTICS_NOTIFICATION = 0x9d,
150
151 /* RF-KILL commands and notifications */
152 REPLY_CARD_STATE_CMD = 0xa0,
153 CARD_STATE_NOTIFICATION = 0xa1,
154
155 /* Missed beacons notification */
156 MISSED_BEACONS_NOTIFICATION = 0xa2,
157
158 REPLY_CT_KILL_CONFIG_CMD = 0xa4,
159 SENSITIVITY_CMD = 0xa8,
160 REPLY_PHY_CALIBRATION_CMD = 0xb0,
161 REPLY_RX_PHY_CMD = 0xc0,
162 REPLY_RX_MPDU_CMD = 0xc1,
163 REPLY_RX = 0xc3,
164 REPLY_COMPRESSED_BA = 0xc5,
165
166 /* BT Coex */
167 REPLY_BT_COEX_PRIO_TABLE = 0xcc,
168 REPLY_BT_COEX_PROT_ENV = 0xcd,
169 REPLY_BT_COEX_PROFILE_NOTIF = 0xce,
170
171 /* PAN commands */
172 REPLY_WIPAN_PARAMS = 0xb2,
173 REPLY_WIPAN_RXON = 0xb3, /* use REPLY_RXON structure */
174 REPLY_WIPAN_RXON_TIMING = 0xb4, /* use REPLY_RXON_TIMING structure */
175 REPLY_WIPAN_RXON_ASSOC = 0xb6, /* use REPLY_RXON_ASSOC structure */
176 REPLY_WIPAN_QOS_PARAM = 0xb7, /* use REPLY_QOS_PARAM structure */
177 REPLY_WIPAN_WEPKEY = 0xb8, /* use REPLY_WEPKEY structure */
178 REPLY_WIPAN_P2P_CHANNEL_SWITCH = 0xb9,
179 REPLY_WIPAN_NOA_NOTIFICATION = 0xbc,
180 REPLY_WIPAN_DEACTIVATION_COMPLETE = 0xbd,
181
182 REPLY_WOWLAN_PATTERNS = 0xe0,
183 REPLY_WOWLAN_WAKEUP_FILTER = 0xe1,
184 REPLY_WOWLAN_TSC_RSC_PARAMS = 0xe2,
185 REPLY_WOWLAN_TKIP_PARAMS = 0xe3,
186 REPLY_WOWLAN_KEK_KCK_MATERIAL = 0xe4,
187 REPLY_WOWLAN_GET_STATUS = 0xe5,
188 REPLY_D3_CONFIG = 0xd3,
189
190 REPLY_MAX = 0xff
191};
192
193/******************************************************************************
194 * (0)
195 * Commonly used structures and definitions:
196 * Command header, rate_n_flags, txpower
197 *
198 *****************************************************************************/
199
200/**
201 * iwlagn rate_n_flags bit fields
202 *
203 * rate_n_flags format is used in following iwlagn commands:
204 * REPLY_RX (response only)
205 * REPLY_RX_MPDU (response only)
206 * REPLY_TX (both command and response)
207 * REPLY_TX_LINK_QUALITY_CMD
208 *
209 * High-throughput (HT) rate format for bits 7:0 (bit 8 must be "1"):
210 * 2-0: 0) 6 Mbps
211 * 1) 12 Mbps
212 * 2) 18 Mbps
213 * 3) 24 Mbps
214 * 4) 36 Mbps
215 * 5) 48 Mbps
216 * 6) 54 Mbps
217 * 7) 60 Mbps
218 *
219 * 4-3: 0) Single stream (SISO)
220 * 1) Dual stream (MIMO)
221 * 2) Triple stream (MIMO)
222 *
223 * 5: Value of 0x20 in bits 7:0 indicates 6 Mbps HT40 duplicate data
224 *
225 * Legacy OFDM rate format for bits 7:0 (bit 8 must be "0", bit 9 "0"):
226 * 3-0: 0xD) 6 Mbps
227 * 0xF) 9 Mbps
228 * 0x5) 12 Mbps
229 * 0x7) 18 Mbps
230 * 0x9) 24 Mbps
231 * 0xB) 36 Mbps
232 * 0x1) 48 Mbps
233 * 0x3) 54 Mbps
234 *
235 * Legacy CCK rate format for bits 7:0 (bit 8 must be "0", bit 9 "1"):
236 * 6-0: 10) 1 Mbps
237 * 20) 2 Mbps
238 * 55) 5.5 Mbps
239 * 110) 11 Mbps
240 */
241#define RATE_MCS_CODE_MSK 0x7
242#define RATE_MCS_SPATIAL_POS 3
243#define RATE_MCS_SPATIAL_MSK 0x18
244#define RATE_MCS_HT_DUP_POS 5
245#define RATE_MCS_HT_DUP_MSK 0x20
246/* Both legacy and HT use bits 7:0 as the CCK/OFDM rate or HT MCS */
247#define RATE_MCS_RATE_MSK 0xff
248
249/* Bit 8: (1) HT format, (0) legacy format in bits 7:0 */
250#define RATE_MCS_FLAGS_POS 8
251#define RATE_MCS_HT_POS 8
252#define RATE_MCS_HT_MSK 0x100
253
254/* Bit 9: (1) CCK, (0) OFDM. HT (bit 8) must be "0" for this bit to be valid */
255#define RATE_MCS_CCK_POS 9
256#define RATE_MCS_CCK_MSK 0x200
257
258/* Bit 10: (1) Use Green Field preamble */
259#define RATE_MCS_GF_POS 10
260#define RATE_MCS_GF_MSK 0x400
261
262/* Bit 11: (1) Use 40Mhz HT40 chnl width, (0) use 20 MHz legacy chnl width */
263#define RATE_MCS_HT40_POS 11
264#define RATE_MCS_HT40_MSK 0x800
265
266/* Bit 12: (1) Duplicate data on both 20MHz chnls. HT40 (bit 11) must be set. */
267#define RATE_MCS_DUP_POS 12
268#define RATE_MCS_DUP_MSK 0x1000
269
270/* Bit 13: (1) Short guard interval (0.4 usec), (0) normal GI (0.8 usec) */
271#define RATE_MCS_SGI_POS 13
272#define RATE_MCS_SGI_MSK 0x2000
273
274/**
275 * rate_n_flags Tx antenna masks
276 * 4965 has 2 transmitters
277 * 5100 has 1 transmitter B
278 * 5150 has 1 transmitter A
279 * 5300 has 3 transmitters
280 * 5350 has 3 transmitters
281 * bit14:16
282 */
283#define RATE_MCS_ANT_POS 14
284#define RATE_MCS_ANT_A_MSK 0x04000
285#define RATE_MCS_ANT_B_MSK 0x08000
286#define RATE_MCS_ANT_C_MSK 0x10000
287#define RATE_MCS_ANT_AB_MSK (RATE_MCS_ANT_A_MSK | RATE_MCS_ANT_B_MSK)
288#define RATE_MCS_ANT_ABC_MSK (RATE_MCS_ANT_AB_MSK | RATE_MCS_ANT_C_MSK)
289#define RATE_ANT_NUM 3
290
291#define POWER_TABLE_NUM_ENTRIES 33
292#define POWER_TABLE_NUM_HT_OFDM_ENTRIES 32
293#define POWER_TABLE_CCK_ENTRY 32
294
295#define IWL_PWR_NUM_HT_OFDM_ENTRIES 24
296#define IWL_PWR_CCK_ENTRIES 2
297
298/**
299 * struct tx_power_dual_stream
300 *
301 * Table entries in REPLY_TX_PWR_TABLE_CMD, REPLY_CHANNEL_SWITCH
302 *
303 * Same format as iwl_tx_power_dual_stream, but __le32
304 */
305struct tx_power_dual_stream {
306 __le32 dw;
307} __packed;
308
309/**
310 * Command REPLY_TX_POWER_DBM_CMD = 0x98
311 * struct iwlagn_tx_power_dbm_cmd
312 */
313#define IWLAGN_TX_POWER_AUTO 0x7f
314#define IWLAGN_TX_POWER_NO_CLOSED (0x1 << 6)
315
316struct iwlagn_tx_power_dbm_cmd {
317 s8 global_lmt; /*in half-dBm (e.g. 30 = 15 dBm) */
318 u8 flags;
319 s8 srv_chan_lmt; /*in half-dBm (e.g. 30 = 15 dBm) */
320 u8 reserved;
321} __packed;
322
323/**
324 * Command TX_ANT_CONFIGURATION_CMD = 0x98
325 * This command is used to configure valid Tx antenna.
326 * By default uCode concludes the valid antenna according to the radio flavor.
327 * This command enables the driver to override/modify this conclusion.
328 */
329struct iwl_tx_ant_config_cmd {
330 __le32 valid;
331} __packed;
332
333/******************************************************************************
334 * (0a)
335 * Alive and Error Commands & Responses:
336 *
337 *****************************************************************************/
338
339#define UCODE_VALID_OK cpu_to_le32(0x1)
340
341/**
342 * REPLY_ALIVE = 0x1 (response only, not a command)
343 *
344 * uCode issues this "alive" notification once the runtime image is ready
345 * to receive commands from the driver. This is the *second* "alive"
346 * notification that the driver will receive after rebooting uCode;
347 * this "alive" is indicated by subtype field != 9.
348 *
349 * See comments documenting "BSM" (bootstrap state machine).
350 *
351 * This response includes two pointers to structures within the device's
352 * data SRAM (access via HBUS_TARG_MEM_* regs) that are useful for debugging:
353 *
354 * 1) log_event_table_ptr indicates base of the event log. This traces
355 * a 256-entry history of uCode execution within a circular buffer.
356 * Its header format is:
357 *
358 * __le32 log_size; log capacity (in number of entries)
359 * __le32 type; (1) timestamp with each entry, (0) no timestamp
360 * __le32 wraps; # times uCode has wrapped to top of circular buffer
361 * __le32 write_index; next circular buffer entry that uCode would fill
362 *
363 * The header is followed by the circular buffer of log entries. Entries
364 * with timestamps have the following format:
365 *
366 * __le32 event_id; range 0 - 1500
367 * __le32 timestamp; low 32 bits of TSF (of network, if associated)
368 * __le32 data; event_id-specific data value
369 *
370 * Entries without timestamps contain only event_id and data.
371 *
372 *
373 * 2) error_event_table_ptr indicates base of the error log. This contains
374 * information about any uCode error that occurs. For agn, the format
375 * of the error log is defined by struct iwl_error_event_table.
376 *
377 * The Linux driver can print both logs to the system log when a uCode error
378 * occurs.
379 */
380
381/*
382 * Note: This structure is read from the device with IO accesses,
383 * and the reading already does the endian conversion. As it is
384 * read with u32-sized accesses, any members with a different size
385 * need to be ordered correctly though!
386 */
387struct iwl_error_event_table {
388 u32 valid; /* (nonzero) valid, (0) log is empty */
389 u32 error_id; /* type of error */
390 u32 pc; /* program counter */
391 u32 blink1; /* branch link */
392 u32 blink2; /* branch link */
393 u32 ilink1; /* interrupt link */
394 u32 ilink2; /* interrupt link */
395 u32 data1; /* error-specific data */
396 u32 data2; /* error-specific data */
397 u32 line; /* source code line of error */
398 u32 bcon_time; /* beacon timer */
399 u32 tsf_low; /* network timestamp function timer */
400 u32 tsf_hi; /* network timestamp function timer */
401 u32 gp1; /* GP1 timer register */
402 u32 gp2; /* GP2 timer register */
403 u32 gp3; /* GP3 timer register */
404 u32 ucode_ver; /* uCode version */
405 u32 hw_ver; /* HW Silicon version */
406 u32 brd_ver; /* HW board version */
407 u32 log_pc; /* log program counter */
408 u32 frame_ptr; /* frame pointer */
409 u32 stack_ptr; /* stack pointer */
410 u32 hcmd; /* last host command header */
411 u32 isr0; /* isr status register LMPM_NIC_ISR0:
412 * rxtx_flag */
413 u32 isr1; /* isr status register LMPM_NIC_ISR1:
414 * host_flag */
415 u32 isr2; /* isr status register LMPM_NIC_ISR2:
416 * enc_flag */
417 u32 isr3; /* isr status register LMPM_NIC_ISR3:
418 * time_flag */
419 u32 isr4; /* isr status register LMPM_NIC_ISR4:
420 * wico interrupt */
421 u32 isr_pref; /* isr status register LMPM_NIC_PREF_STAT */
422 u32 wait_event; /* wait event() caller address */
423 u32 l2p_control; /* L2pControlField */
424 u32 l2p_duration; /* L2pDurationField */
425 u32 l2p_mhvalid; /* L2pMhValidBits */
426 u32 l2p_addr_match; /* L2pAddrMatchStat */
427 u32 lmpm_pmg_sel; /* indicate which clocks are turned on
428 * (LMPM_PMG_SEL) */
429 u32 u_timestamp; /* indicate when the date and time of the
430 * compilation */
431 u32 flow_handler; /* FH read/write pointers, RX credit */
432} __packed;
433
434struct iwl_alive_resp {
435 u8 ucode_minor;
436 u8 ucode_major;
437 __le16 reserved1;
438 u8 sw_rev[8];
439 u8 ver_type;
440 u8 ver_subtype; /* not "9" for runtime alive */
441 __le16 reserved2;
442 __le32 log_event_table_ptr; /* SRAM address for event log */
443 __le32 error_event_table_ptr; /* SRAM address for error log */
444 __le32 timestamp;
445 __le32 is_valid;
446} __packed;
447
448/*
449 * REPLY_ERROR = 0x2 (response only, not a command)
450 */
451struct iwl_error_resp {
452 __le32 error_type;
453 u8 cmd_id;
454 u8 reserved1;
455 __le16 bad_cmd_seq_num;
456 __le32 error_info;
457 __le64 timestamp;
458} __packed;
459
460/******************************************************************************
461 * (1)
462 * RXON Commands & Responses:
463 *
464 *****************************************************************************/
465
466/*
467 * Rx config defines & structure
468 */
469/* rx_config device types */
470enum {
471 RXON_DEV_TYPE_AP = 1,
472 RXON_DEV_TYPE_ESS = 3,
473 RXON_DEV_TYPE_IBSS = 4,
474 RXON_DEV_TYPE_SNIFFER = 6,
475 RXON_DEV_TYPE_CP = 7,
476 RXON_DEV_TYPE_2STA = 8,
477 RXON_DEV_TYPE_P2P = 9,
478};
479
480
481#define RXON_RX_CHAIN_DRIVER_FORCE_MSK cpu_to_le16(0x1 << 0)
482#define RXON_RX_CHAIN_DRIVER_FORCE_POS (0)
483#define RXON_RX_CHAIN_VALID_MSK cpu_to_le16(0x7 << 1)
484#define RXON_RX_CHAIN_VALID_POS (1)
485#define RXON_RX_CHAIN_FORCE_SEL_MSK cpu_to_le16(0x7 << 4)
486#define RXON_RX_CHAIN_FORCE_SEL_POS (4)
487#define RXON_RX_CHAIN_FORCE_MIMO_SEL_MSK cpu_to_le16(0x7 << 7)
488#define RXON_RX_CHAIN_FORCE_MIMO_SEL_POS (7)
489#define RXON_RX_CHAIN_CNT_MSK cpu_to_le16(0x3 << 10)
490#define RXON_RX_CHAIN_CNT_POS (10)
491#define RXON_RX_CHAIN_MIMO_CNT_MSK cpu_to_le16(0x3 << 12)
492#define RXON_RX_CHAIN_MIMO_CNT_POS (12)
493#define RXON_RX_CHAIN_MIMO_FORCE_MSK cpu_to_le16(0x1 << 14)
494#define RXON_RX_CHAIN_MIMO_FORCE_POS (14)
495
496/* rx_config flags */
497/* band & modulation selection */
498#define RXON_FLG_BAND_24G_MSK cpu_to_le32(1 << 0)
499#define RXON_FLG_CCK_MSK cpu_to_le32(1 << 1)
500/* auto detection enable */
501#define RXON_FLG_AUTO_DETECT_MSK cpu_to_le32(1 << 2)
502/* TGg protection when tx */
503#define RXON_FLG_TGG_PROTECT_MSK cpu_to_le32(1 << 3)
504/* cck short slot & preamble */
505#define RXON_FLG_SHORT_SLOT_MSK cpu_to_le32(1 << 4)
506#define RXON_FLG_SHORT_PREAMBLE_MSK cpu_to_le32(1 << 5)
507/* antenna selection */
508#define RXON_FLG_DIS_DIV_MSK cpu_to_le32(1 << 7)
509#define RXON_FLG_ANT_SEL_MSK cpu_to_le32(0x0f00)
510#define RXON_FLG_ANT_A_MSK cpu_to_le32(1 << 8)
511#define RXON_FLG_ANT_B_MSK cpu_to_le32(1 << 9)
512/* radar detection enable */
513#define RXON_FLG_RADAR_DETECT_MSK cpu_to_le32(1 << 12)
514#define RXON_FLG_TGJ_NARROW_BAND_MSK cpu_to_le32(1 << 13)
515/* rx response to host with 8-byte TSF
516* (according to ON_AIR deassertion) */
517#define RXON_FLG_TSF2HOST_MSK cpu_to_le32(1 << 15)
518
519
520/* HT flags */
521#define RXON_FLG_CTRL_CHANNEL_LOC_POS (22)
522#define RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK cpu_to_le32(0x1 << 22)
523
524#define RXON_FLG_HT_OPERATING_MODE_POS (23)
525
526#define RXON_FLG_HT_PROT_MSK cpu_to_le32(0x1 << 23)
527#define RXON_FLG_HT40_PROT_MSK cpu_to_le32(0x2 << 23)
528
529#define RXON_FLG_CHANNEL_MODE_POS (25)
530#define RXON_FLG_CHANNEL_MODE_MSK cpu_to_le32(0x3 << 25)
531
532/* channel mode */
533enum {
534 CHANNEL_MODE_LEGACY = 0,
535 CHANNEL_MODE_PURE_40 = 1,
536 CHANNEL_MODE_MIXED = 2,
537 CHANNEL_MODE_RESERVED = 3,
538};
539#define RXON_FLG_CHANNEL_MODE_LEGACY cpu_to_le32(CHANNEL_MODE_LEGACY << RXON_FLG_CHANNEL_MODE_POS)
540#define RXON_FLG_CHANNEL_MODE_PURE_40 cpu_to_le32(CHANNEL_MODE_PURE_40 << RXON_FLG_CHANNEL_MODE_POS)
541#define RXON_FLG_CHANNEL_MODE_MIXED cpu_to_le32(CHANNEL_MODE_MIXED << RXON_FLG_CHANNEL_MODE_POS)
542
543/* CTS to self (if spec allows) flag */
544#define RXON_FLG_SELF_CTS_EN cpu_to_le32(0x1<<30)
545
546/* rx_config filter flags */
547/* accept all data frames */
548#define RXON_FILTER_PROMISC_MSK cpu_to_le32(1 << 0)
549/* pass control & management to host */
550#define RXON_FILTER_CTL2HOST_MSK cpu_to_le32(1 << 1)
551/* accept multi-cast */
552#define RXON_FILTER_ACCEPT_GRP_MSK cpu_to_le32(1 << 2)
553/* don't decrypt uni-cast frames */
554#define RXON_FILTER_DIS_DECRYPT_MSK cpu_to_le32(1 << 3)
555/* don't decrypt multi-cast frames */
556#define RXON_FILTER_DIS_GRP_DECRYPT_MSK cpu_to_le32(1 << 4)
557/* STA is associated */
558#define RXON_FILTER_ASSOC_MSK cpu_to_le32(1 << 5)
559/* transfer to host non bssid beacons in associated state */
560#define RXON_FILTER_BCON_AWARE_MSK cpu_to_le32(1 << 6)
561
562/**
563 * REPLY_RXON = 0x10 (command, has simple generic response)
564 *
565 * RXON tunes the radio tuner to a service channel, and sets up a number
566 * of parameters that are used primarily for Rx, but also for Tx operations.
567 *
568 * NOTE: When tuning to a new channel, driver must set the
569 * RXON_FILTER_ASSOC_MSK to 0. This will clear station-dependent
570 * info within the device, including the station tables, tx retry
571 * rate tables, and txpower tables. Driver must build a new station
572 * table and txpower table before transmitting anything on the RXON
573 * channel.
574 *
575 * NOTE: All RXONs wipe clean the internal txpower table. Driver must
576 * issue a new REPLY_TX_PWR_TABLE_CMD after each REPLY_RXON (0x10),
577 * regardless of whether RXON_FILTER_ASSOC_MSK is set.
578 */
579
580struct iwl_rxon_cmd {
581 u8 node_addr[6];
582 __le16 reserved1;
583 u8 bssid_addr[6];
584 __le16 reserved2;
585 u8 wlap_bssid_addr[6];
586 __le16 reserved3;
587 u8 dev_type;
588 u8 air_propagation;
589 __le16 rx_chain;
590 u8 ofdm_basic_rates;
591 u8 cck_basic_rates;
592 __le16 assoc_id;
593 __le32 flags;
594 __le32 filter_flags;
595 __le16 channel;
596 u8 ofdm_ht_single_stream_basic_rates;
597 u8 ofdm_ht_dual_stream_basic_rates;
598 u8 ofdm_ht_triple_stream_basic_rates;
599 u8 reserved5;
600 __le16 acquisition_data;
601 __le16 reserved6;
602} __packed;
603
604/*
605 * REPLY_RXON_ASSOC = 0x11 (command, has simple generic response)
606 */
607struct iwl_rxon_assoc_cmd {
608 __le32 flags;
609 __le32 filter_flags;
610 u8 ofdm_basic_rates;
611 u8 cck_basic_rates;
612 __le16 reserved1;
613 u8 ofdm_ht_single_stream_basic_rates;
614 u8 ofdm_ht_dual_stream_basic_rates;
615 u8 ofdm_ht_triple_stream_basic_rates;
616 u8 reserved2;
617 __le16 rx_chain_select_flags;
618 __le16 acquisition_data;
619 __le32 reserved3;
620} __packed;
621
622#define IWL_CONN_MAX_LISTEN_INTERVAL 10
623#define IWL_MAX_UCODE_BEACON_INTERVAL 4 /* 4096 */
624
625/*
626 * REPLY_RXON_TIMING = 0x14 (command, has simple generic response)
627 */
628struct iwl_rxon_time_cmd {
629 __le64 timestamp;
630 __le16 beacon_interval;
631 __le16 atim_window;
632 __le32 beacon_init_val;
633 __le16 listen_interval;
634 u8 dtim_period;
635 u8 delta_cp_bss_tbtts;
636} __packed;
637
638/*
639 * REPLY_CHANNEL_SWITCH = 0x72 (command, has simple generic response)
640 */
641/**
642 * struct iwl5000_channel_switch_cmd
643 * @band: 0- 5.2GHz, 1- 2.4GHz
644 * @expect_beacon: 0- resume transmits after channel switch
645 * 1- wait for beacon to resume transmits
646 * @channel: new channel number
647 * @rxon_flags: Rx on flags
648 * @rxon_filter_flags: filtering parameters
649 * @switch_time: switch time in extended beacon format
650 * @reserved: reserved bytes
651 */
652struct iwl5000_channel_switch_cmd {
653 u8 band;
654 u8 expect_beacon;
655 __le16 channel;
656 __le32 rxon_flags;
657 __le32 rxon_filter_flags;
658 __le32 switch_time;
659 __le32 reserved[2][IWL_PWR_NUM_HT_OFDM_ENTRIES + IWL_PWR_CCK_ENTRIES];
660} __packed;
661
662/**
663 * struct iwl6000_channel_switch_cmd
664 * @band: 0- 5.2GHz, 1- 2.4GHz
665 * @expect_beacon: 0- resume transmits after channel switch
666 * 1- wait for beacon to resume transmits
667 * @channel: new channel number
668 * @rxon_flags: Rx on flags
669 * @rxon_filter_flags: filtering parameters
670 * @switch_time: switch time in extended beacon format
671 * @reserved: reserved bytes
672 */
673struct iwl6000_channel_switch_cmd {
674 u8 band;
675 u8 expect_beacon;
676 __le16 channel;
677 __le32 rxon_flags;
678 __le32 rxon_filter_flags;
679 __le32 switch_time;
680 __le32 reserved[3][IWL_PWR_NUM_HT_OFDM_ENTRIES + IWL_PWR_CCK_ENTRIES];
681} __packed;
682
683/*
684 * CHANNEL_SWITCH_NOTIFICATION = 0x73 (notification only, not a command)
685 */
686struct iwl_csa_notification {
687 __le16 band;
688 __le16 channel;
689 __le32 status; /* 0 - OK, 1 - fail */
690} __packed;
691
692/******************************************************************************
693 * (2)
694 * Quality-of-Service (QOS) Commands & Responses:
695 *
696 *****************************************************************************/
697
698/**
699 * struct iwl_ac_qos -- QOS timing params for REPLY_QOS_PARAM
700 * One for each of 4 EDCA access categories in struct iwl_qosparam_cmd
701 *
702 * @cw_min: Contention window, start value in numbers of slots.
703 * Should be a power-of-2, minus 1. Device's default is 0x0f.
704 * @cw_max: Contention window, max value in numbers of slots.
705 * Should be a power-of-2, minus 1. Device's default is 0x3f.
706 * @aifsn: Number of slots in Arbitration Interframe Space (before
707 * performing random backoff timing prior to Tx). Device default 1.
708 * @edca_txop: Length of Tx opportunity, in uSecs. Device default is 0.
709 *
710 * Device will automatically increase contention window by (2*CW) + 1 for each
711 * transmission retry. Device uses cw_max as a bit mask, ANDed with new CW
712 * value, to cap the CW value.
713 */
714struct iwl_ac_qos {
715 __le16 cw_min;
716 __le16 cw_max;
717 u8 aifsn;
718 u8 reserved1;
719 __le16 edca_txop;
720} __packed;
721
722/* QoS flags defines */
723#define QOS_PARAM_FLG_UPDATE_EDCA_MSK cpu_to_le32(0x01)
724#define QOS_PARAM_FLG_TGN_MSK cpu_to_le32(0x02)
725#define QOS_PARAM_FLG_TXOP_TYPE_MSK cpu_to_le32(0x10)
726
727/* Number of Access Categories (AC) (EDCA), queues 0..3 */
728#define AC_NUM 4
729
730/*
731 * REPLY_QOS_PARAM = 0x13 (command, has simple generic response)
732 *
733 * This command sets up timings for each of the 4 prioritized EDCA Tx FIFOs
734 * 0: Background, 1: Best Effort, 2: Video, 3: Voice.
735 */
736struct iwl_qosparam_cmd {
737 __le32 qos_flags;
738 struct iwl_ac_qos ac[AC_NUM];
739} __packed;
740
741/******************************************************************************
742 * (3)
743 * Add/Modify Stations Commands & Responses:
744 *
745 *****************************************************************************/
746/*
747 * Multi station support
748 */
749
750/* Special, dedicated locations within device's station table */
751#define IWL_AP_ID 0
752#define IWL_AP_ID_PAN 1
753#define IWL_STA_ID 2
754#define IWLAGN_PAN_BCAST_ID 14
755#define IWLAGN_BROADCAST_ID 15
756#define IWLAGN_STATION_COUNT 16
757
758#define IWL_INVALID_STATION 255
759#define IWL_MAX_TID_COUNT 8
760#define IWL_TID_NON_QOS IWL_MAX_TID_COUNT
761
762#define STA_FLG_TX_RATE_MSK cpu_to_le32(1 << 2)
763#define STA_FLG_PWR_SAVE_MSK cpu_to_le32(1 << 8)
764#define STA_FLG_PAN_STATION cpu_to_le32(1 << 13)
765#define STA_FLG_RTS_MIMO_PROT_MSK cpu_to_le32(1 << 17)
766#define STA_FLG_AGG_MPDU_8US_MSK cpu_to_le32(1 << 18)
767#define STA_FLG_MAX_AGG_SIZE_POS (19)
768#define STA_FLG_MAX_AGG_SIZE_MSK cpu_to_le32(3 << 19)
769#define STA_FLG_HT40_EN_MSK cpu_to_le32(1 << 21)
770#define STA_FLG_MIMO_DIS_MSK cpu_to_le32(1 << 22)
771#define STA_FLG_AGG_MPDU_DENSITY_POS (23)
772#define STA_FLG_AGG_MPDU_DENSITY_MSK cpu_to_le32(7 << 23)
773
774/* Use in mode field. 1: modify existing entry, 0: add new station entry */
775#define STA_CONTROL_MODIFY_MSK 0x01
776
777/* key flags __le16*/
778#define STA_KEY_FLG_ENCRYPT_MSK cpu_to_le16(0x0007)
779#define STA_KEY_FLG_NO_ENC cpu_to_le16(0x0000)
780#define STA_KEY_FLG_WEP cpu_to_le16(0x0001)
781#define STA_KEY_FLG_CCMP cpu_to_le16(0x0002)
782#define STA_KEY_FLG_TKIP cpu_to_le16(0x0003)
783
784#define STA_KEY_FLG_KEYID_POS 8
785#define STA_KEY_FLG_INVALID cpu_to_le16(0x0800)
786/* wep key is either from global key (0) or from station info array (1) */
787#define STA_KEY_FLG_MAP_KEY_MSK cpu_to_le16(0x0008)
788
789/* wep key in STA: 5-bytes (0) or 13-bytes (1) */
790#define STA_KEY_FLG_KEY_SIZE_MSK cpu_to_le16(0x1000)
791#define STA_KEY_MULTICAST_MSK cpu_to_le16(0x4000)
792#define STA_KEY_MAX_NUM 8
793#define STA_KEY_MAX_NUM_PAN 16
794/* must not match WEP_INVALID_OFFSET */
795#define IWLAGN_HW_KEY_DEFAULT 0xfe
796
797/* Flags indicate whether to modify vs. don't change various station params */
798#define STA_MODIFY_KEY_MASK 0x01
799#define STA_MODIFY_TID_DISABLE_TX 0x02
800#define STA_MODIFY_TX_RATE_MSK 0x04
801#define STA_MODIFY_ADDBA_TID_MSK 0x08
802#define STA_MODIFY_DELBA_TID_MSK 0x10
803#define STA_MODIFY_SLEEP_TX_COUNT_MSK 0x20
804
805/* Receiver address (actually, Rx station's index into station table),
806 * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
807#define BUILD_RAxTID(sta_id, tid) (((sta_id) << 4) + (tid))
808
809/* agn */
810struct iwl_keyinfo {
811 __le16 key_flags;
812 u8 tkip_rx_tsc_byte2; /* TSC[2] for key mix ph1 detection */
813 u8 reserved1;
814 __le16 tkip_rx_ttak[5]; /* 10-byte unicast TKIP TTAK */
815 u8 key_offset;
816 u8 reserved2;
817 u8 key[16]; /* 16-byte unicast decryption key */
818 __le64 tx_secur_seq_cnt;
819 __le64 hw_tkip_mic_rx_key;
820 __le64 hw_tkip_mic_tx_key;
821} __packed;
822
823/**
824 * struct sta_id_modify
825 * @addr[ETH_ALEN]: station's MAC address
826 * @sta_id: index of station in uCode's station table
827 * @modify_mask: STA_MODIFY_*, 1: modify, 0: don't change
828 *
829 * Driver selects unused table index when adding new station,
830 * or the index to a pre-existing station entry when modifying that station.
831 * Some indexes have special purposes (IWL_AP_ID, index 0, is for AP).
832 *
833 * modify_mask flags select which parameters to modify vs. leave alone.
834 */
835struct sta_id_modify {
836 u8 addr[ETH_ALEN];
837 __le16 reserved1;
838 u8 sta_id;
839 u8 modify_mask;
840 __le16 reserved2;
841} __packed;
842
843/*
844 * REPLY_ADD_STA = 0x18 (command)
845 *
846 * The device contains an internal table of per-station information,
847 * with info on security keys, aggregation parameters, and Tx rates for
848 * initial Tx attempt and any retries (agn devices uses
849 * REPLY_TX_LINK_QUALITY_CMD,
850 *
851 * REPLY_ADD_STA sets up the table entry for one station, either creating
852 * a new entry, or modifying a pre-existing one.
853 *
854 * NOTE: RXON command (without "associated" bit set) wipes the station table
855 * clean. Moving into RF_KILL state does this also. Driver must set up
856 * new station table before transmitting anything on the RXON channel
857 * (except active scans or active measurements; those commands carry
858 * their own txpower/rate setup data).
859 *
860 * When getting started on a new channel, driver must set up the
861 * IWL_BROADCAST_ID entry (last entry in the table). For a client
862 * station in a BSS, once an AP is selected, driver sets up the AP STA
863 * in the IWL_AP_ID entry (1st entry in the table). BROADCAST and AP
864 * are all that are needed for a BSS client station. If the device is
865 * used as AP, or in an IBSS network, driver must set up station table
866 * entries for all STAs in network, starting with index IWL_STA_ID.
867 */
868
869struct iwl_addsta_cmd {
870 u8 mode; /* 1: modify existing, 0: add new station */
871 u8 reserved[3];
872 struct sta_id_modify sta;
873 struct iwl_keyinfo key;
874 __le32 station_flags; /* STA_FLG_* */
875 __le32 station_flags_msk; /* STA_FLG_* */
876
877 /* bit field to disable (1) or enable (0) Tx for Traffic ID (TID)
878 * corresponding to bit (e.g. bit 5 controls TID 5).
879 * Set modify_mask bit STA_MODIFY_TID_DISABLE_TX to use this field. */
880 __le16 tid_disable_tx;
881 __le16 legacy_reserved;
882
883 /* TID for which to add block-ack support.
884 * Set modify_mask bit STA_MODIFY_ADDBA_TID_MSK to use this field. */
885 u8 add_immediate_ba_tid;
886
887 /* TID for which to remove block-ack support.
888 * Set modify_mask bit STA_MODIFY_DELBA_TID_MSK to use this field. */
889 u8 remove_immediate_ba_tid;
890
891 /* Starting Sequence Number for added block-ack support.
892 * Set modify_mask bit STA_MODIFY_ADDBA_TID_MSK to use this field. */
893 __le16 add_immediate_ba_ssn;
894
895 /*
896 * Number of packets OK to transmit to station even though
897 * it is asleep -- used to synchronise PS-poll and u-APSD
898 * responses while ucode keeps track of STA sleep state.
899 */
900 __le16 sleep_tx_count;
901
902 __le16 reserved2;
903} __packed;
904
905
906#define ADD_STA_SUCCESS_MSK 0x1
907#define ADD_STA_NO_ROOM_IN_TABLE 0x2
908#define ADD_STA_NO_BLOCK_ACK_RESOURCE 0x4
909#define ADD_STA_MODIFY_NON_EXIST_STA 0x8
910/*
911 * REPLY_ADD_STA = 0x18 (response)
912 */
913struct iwl_add_sta_resp {
914 u8 status; /* ADD_STA_* */
915} __packed;
916
917#define REM_STA_SUCCESS_MSK 0x1
918/*
919 * REPLY_REM_STA = 0x19 (response)
920 */
921struct iwl_rem_sta_resp {
922 u8 status;
923} __packed;
924
925/*
926 * REPLY_REM_STA = 0x19 (command)
927 */
928struct iwl_rem_sta_cmd {
929 u8 num_sta; /* number of removed stations */
930 u8 reserved[3];
931 u8 addr[ETH_ALEN]; /* MAC addr of the first station */
932 u8 reserved2[2];
933} __packed;
934
935
936/* WiFi queues mask */
937#define IWL_SCD_BK_MSK cpu_to_le32(BIT(0))
938#define IWL_SCD_BE_MSK cpu_to_le32(BIT(1))
939#define IWL_SCD_VI_MSK cpu_to_le32(BIT(2))
940#define IWL_SCD_VO_MSK cpu_to_le32(BIT(3))
941#define IWL_SCD_MGMT_MSK cpu_to_le32(BIT(3))
942
943/* PAN queues mask */
944#define IWL_PAN_SCD_BK_MSK cpu_to_le32(BIT(4))
945#define IWL_PAN_SCD_BE_MSK cpu_to_le32(BIT(5))
946#define IWL_PAN_SCD_VI_MSK cpu_to_le32(BIT(6))
947#define IWL_PAN_SCD_VO_MSK cpu_to_le32(BIT(7))
948#define IWL_PAN_SCD_MGMT_MSK cpu_to_le32(BIT(7))
949#define IWL_PAN_SCD_MULTICAST_MSK cpu_to_le32(BIT(8))
950
951#define IWL_AGG_TX_QUEUE_MSK cpu_to_le32(0xffc00)
952
953#define IWL_DROP_SINGLE 0
954#define IWL_DROP_ALL (BIT(IWL_RXON_CTX_BSS) | BIT(IWL_RXON_CTX_PAN))
955
956/*
957 * REPLY_TXFIFO_FLUSH = 0x1e(command and response)
958 *
959 * When using full FIFO flush this command checks the scheduler HW block WR/RD
960 * pointers to check if all the frames were transferred by DMA into the
961 * relevant TX FIFO queue. Only when the DMA is finished and the queue is
962 * empty the command can finish.
963 * This command is used to flush the TXFIFO from transmit commands, it may
964 * operate on single or multiple queues, the command queue can't be flushed by
965 * this command. The command response is returned when all the queue flush
966 * operations are done. Each TX command flushed return response with the FLUSH
967 * status set in the TX response status. When FIFO flush operation is used,
968 * the flush operation ends when both the scheduler DMA done and TXFIFO empty
969 * are set.
970 *
971 * @fifo_control: bit mask for which queues to flush
972 * @flush_control: flush controls
973 * 0: Dump single MSDU
974 * 1: Dump multiple MSDU according to PS, INVALID STA, TTL, TID disable.
975 * 2: Dump all FIFO
976 */
977struct iwl_txfifo_flush_cmd {
978 __le32 fifo_control;
979 __le16 flush_control;
980 __le16 reserved;
981} __packed;
982
983/*
984 * REPLY_WEP_KEY = 0x20
985 */
986struct iwl_wep_key {
987 u8 key_index;
988 u8 key_offset;
989 u8 reserved1[2];
990 u8 key_size;
991 u8 reserved2[3];
992 u8 key[16];
993} __packed;
994
995struct iwl_wep_cmd {
996 u8 num_keys;
997 u8 global_key_type;
998 u8 flags;
999 u8 reserved;
1000 struct iwl_wep_key key[0];
1001} __packed;
1002
1003#define WEP_KEY_WEP_TYPE 1
1004#define WEP_KEYS_MAX 4
1005#define WEP_INVALID_OFFSET 0xff
1006#define WEP_KEY_LEN_64 5
1007#define WEP_KEY_LEN_128 13
1008
1009/******************************************************************************
1010 * (4)
1011 * Rx Responses:
1012 *
1013 *****************************************************************************/
1014
1015#define RX_RES_STATUS_NO_CRC32_ERROR cpu_to_le32(1 << 0)
1016#define RX_RES_STATUS_NO_RXE_OVERFLOW cpu_to_le32(1 << 1)
1017
1018#define RX_RES_PHY_FLAGS_BAND_24_MSK cpu_to_le16(1 << 0)
1019#define RX_RES_PHY_FLAGS_MOD_CCK_MSK cpu_to_le16(1 << 1)
1020#define RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK cpu_to_le16(1 << 2)
1021#define RX_RES_PHY_FLAGS_NARROW_BAND_MSK cpu_to_le16(1 << 3)
1022#define RX_RES_PHY_FLAGS_ANTENNA_MSK 0xf0
1023#define RX_RES_PHY_FLAGS_ANTENNA_POS 4
1024
1025#define RX_RES_STATUS_SEC_TYPE_MSK (0x7 << 8)
1026#define RX_RES_STATUS_SEC_TYPE_NONE (0x0 << 8)
1027#define RX_RES_STATUS_SEC_TYPE_WEP (0x1 << 8)
1028#define RX_RES_STATUS_SEC_TYPE_CCMP (0x2 << 8)
1029#define RX_RES_STATUS_SEC_TYPE_TKIP (0x3 << 8)
1030#define RX_RES_STATUS_SEC_TYPE_ERR (0x7 << 8)
1031
1032#define RX_RES_STATUS_STATION_FOUND (1<<6)
1033#define RX_RES_STATUS_NO_STATION_INFO_MISMATCH (1<<7)
1034
1035#define RX_RES_STATUS_DECRYPT_TYPE_MSK (0x3 << 11)
1036#define RX_RES_STATUS_NOT_DECRYPT (0x0 << 11)
1037#define RX_RES_STATUS_DECRYPT_OK (0x3 << 11)
1038#define RX_RES_STATUS_BAD_ICV_MIC (0x1 << 11)
1039#define RX_RES_STATUS_BAD_KEY_TTAK (0x2 << 11)
1040
1041#define RX_MPDU_RES_STATUS_ICV_OK (0x20)
1042#define RX_MPDU_RES_STATUS_MIC_OK (0x40)
1043#define RX_MPDU_RES_STATUS_TTAK_OK (1 << 7)
1044#define RX_MPDU_RES_STATUS_DEC_DONE_MSK (0x800)
1045
1046
1047#define IWLAGN_RX_RES_PHY_CNT 8
1048#define IWLAGN_RX_RES_AGC_IDX 1
1049#define IWLAGN_RX_RES_RSSI_AB_IDX 2
1050#define IWLAGN_RX_RES_RSSI_C_IDX 3
1051#define IWLAGN_OFDM_AGC_MSK 0xfe00
1052#define IWLAGN_OFDM_AGC_BIT_POS 9
1053#define IWLAGN_OFDM_RSSI_INBAND_A_BITMSK 0x00ff
1054#define IWLAGN_OFDM_RSSI_ALLBAND_A_BITMSK 0xff00
1055#define IWLAGN_OFDM_RSSI_A_BIT_POS 0
1056#define IWLAGN_OFDM_RSSI_INBAND_B_BITMSK 0xff0000
1057#define IWLAGN_OFDM_RSSI_ALLBAND_B_BITMSK 0xff000000
1058#define IWLAGN_OFDM_RSSI_B_BIT_POS 16
1059#define IWLAGN_OFDM_RSSI_INBAND_C_BITMSK 0x00ff
1060#define IWLAGN_OFDM_RSSI_ALLBAND_C_BITMSK 0xff00
1061#define IWLAGN_OFDM_RSSI_C_BIT_POS 0
1062
1063struct iwlagn_non_cfg_phy {
1064 __le32 non_cfg_phy[IWLAGN_RX_RES_PHY_CNT]; /* up to 8 phy entries */
1065} __packed;
1066
1067
1068/*
1069 * REPLY_RX = 0xc3 (response only, not a command)
1070 * Used only for legacy (non 11n) frames.
1071 */
1072struct iwl_rx_phy_res {
1073 u8 non_cfg_phy_cnt; /* non configurable DSP phy data byte count */
1074 u8 cfg_phy_cnt; /* configurable DSP phy data byte count */
1075 u8 stat_id; /* configurable DSP phy data set ID */
1076 u8 reserved1;
1077 __le64 timestamp; /* TSF at on air rise */
1078 __le32 beacon_time_stamp; /* beacon at on-air rise */
1079 __le16 phy_flags; /* general phy flags: band, modulation, ... */
1080 __le16 channel; /* channel number */
1081 u8 non_cfg_phy_buf[32]; /* for various implementations of non_cfg_phy */
1082 __le32 rate_n_flags; /* RATE_MCS_* */
1083 __le16 byte_count; /* frame's byte-count */
1084 __le16 frame_time; /* frame's time on the air */
1085} __packed;
1086
1087struct iwl_rx_mpdu_res_start {
1088 __le16 byte_count;
1089 __le16 reserved;
1090} __packed;
1091
1092
1093/******************************************************************************
1094 * (5)
1095 * Tx Commands & Responses:
1096 *
1097 * Driver must place each REPLY_TX command into one of the prioritized Tx
1098 * queues in host DRAM, shared between driver and device (see comments for
1099 * SCD registers and Tx/Rx Queues). When the device's Tx scheduler and uCode
1100 * are preparing to transmit, the device pulls the Tx command over the PCI
1101 * bus via one of the device's Tx DMA channels, to fill an internal FIFO
1102 * from which data will be transmitted.
1103 *
1104 * uCode handles all timing and protocol related to control frames
1105 * (RTS/CTS/ACK), based on flags in the Tx command. uCode and Tx scheduler
1106 * handle reception of block-acks; uCode updates the host driver via
1107 * REPLY_COMPRESSED_BA.
1108 *
1109 * uCode handles retrying Tx when an ACK is expected but not received.
1110 * This includes trying lower data rates than the one requested in the Tx
1111 * command, as set up by the REPLY_TX_LINK_QUALITY_CMD (agn).
1112 *
1113 * Driver sets up transmit power for various rates via REPLY_TX_PWR_TABLE_CMD.
1114 * This command must be executed after every RXON command, before Tx can occur.
1115 *****************************************************************************/
1116
1117/* REPLY_TX Tx flags field */
1118
1119/*
1120 * 1: Use RTS/CTS protocol or CTS-to-self if spec allows it
1121 * before this frame. if CTS-to-self required check
1122 * RXON_FLG_SELF_CTS_EN status.
1123 */
1124#define TX_CMD_FLG_PROT_REQUIRE_MSK cpu_to_le32(1 << 0)
1125
1126/* 1: Expect ACK from receiving station
1127 * 0: Don't expect ACK (MAC header's duration field s/b 0)
1128 * Set this for unicast frames, but not broadcast/multicast. */
1129#define TX_CMD_FLG_ACK_MSK cpu_to_le32(1 << 3)
1130
1131/* For agn devices:
1132 * 1: Use rate scale table (see REPLY_TX_LINK_QUALITY_CMD).
1133 * Tx command's initial_rate_index indicates first rate to try;
1134 * uCode walks through table for additional Tx attempts.
1135 * 0: Use Tx rate/MCS from Tx command's rate_n_flags field.
1136 * This rate will be used for all Tx attempts; it will not be scaled. */
1137#define TX_CMD_FLG_STA_RATE_MSK cpu_to_le32(1 << 4)
1138
1139/* 1: Expect immediate block-ack.
1140 * Set when Txing a block-ack request frame. Also set TX_CMD_FLG_ACK_MSK. */
1141#define TX_CMD_FLG_IMM_BA_RSP_MASK cpu_to_le32(1 << 6)
1142
1143/* Tx antenna selection field; reserved (0) for agn devices. */
1144#define TX_CMD_FLG_ANT_SEL_MSK cpu_to_le32(0xf00)
1145
1146/* 1: Ignore Bluetooth priority for this frame.
1147 * 0: Delay Tx until Bluetooth device is done (normal usage). */
1148#define TX_CMD_FLG_IGNORE_BT cpu_to_le32(1 << 12)
1149
1150/* 1: uCode overrides sequence control field in MAC header.
1151 * 0: Driver provides sequence control field in MAC header.
1152 * Set this for management frames, non-QOS data frames, non-unicast frames,
1153 * and also in Tx command embedded in REPLY_SCAN_CMD for active scans. */
1154#define TX_CMD_FLG_SEQ_CTL_MSK cpu_to_le32(1 << 13)
1155
1156/* 1: This frame is non-last MPDU; more fragments are coming.
1157 * 0: Last fragment, or not using fragmentation. */
1158#define TX_CMD_FLG_MORE_FRAG_MSK cpu_to_le32(1 << 14)
1159
1160/* 1: uCode calculates and inserts Timestamp Function (TSF) in outgoing frame.
1161 * 0: No TSF required in outgoing frame.
1162 * Set this for transmitting beacons and probe responses. */
1163#define TX_CMD_FLG_TSF_MSK cpu_to_le32(1 << 16)
1164
1165/* 1: Driver inserted 2 bytes pad after the MAC header, for (required) dword
1166 * alignment of frame's payload data field.
1167 * 0: No pad
1168 * Set this for MAC headers with 26 or 30 bytes, i.e. those with QOS or ADDR4
1169 * field (but not both). Driver must align frame data (i.e. data following
1170 * MAC header) to DWORD boundary. */
1171#define TX_CMD_FLG_MH_PAD_MSK cpu_to_le32(1 << 20)
1172
1173/* accelerate aggregation support
1174 * 0 - no CCMP encryption; 1 - CCMP encryption */
1175#define TX_CMD_FLG_AGG_CCMP_MSK cpu_to_le32(1 << 22)
1176
1177/* HCCA-AP - disable duration overwriting. */
1178#define TX_CMD_FLG_DUR_MSK cpu_to_le32(1 << 25)
1179
1180
1181/*
1182 * TX command security control
1183 */
1184#define TX_CMD_SEC_WEP 0x01
1185#define TX_CMD_SEC_CCM 0x02
1186#define TX_CMD_SEC_TKIP 0x03
1187#define TX_CMD_SEC_MSK 0x03
1188#define TX_CMD_SEC_SHIFT 6
1189#define TX_CMD_SEC_KEY128 0x08
1190
1191/*
1192 * security overhead sizes
1193 */
1194#define WEP_IV_LEN 4
1195#define WEP_ICV_LEN 4
1196#define CCMP_MIC_LEN 8
1197#define TKIP_ICV_LEN 4
1198
1199/*
1200 * REPLY_TX = 0x1c (command)
1201 */
1202
1203/*
1204 * 4965 uCode updates these Tx attempt count values in host DRAM.
1205 * Used for managing Tx retries when expecting block-acks.
1206 * Driver should set these fields to 0.
1207 */
1208struct iwl_dram_scratch {
1209 u8 try_cnt; /* Tx attempts */
1210 u8 bt_kill_cnt; /* Tx attempts blocked by Bluetooth device */
1211 __le16 reserved;
1212} __packed;
1213
1214struct iwl_tx_cmd {
1215 /*
1216 * MPDU byte count:
1217 * MAC header (24/26/30/32 bytes) + 2 bytes pad if 26/30 header size,
1218 * + 8 byte IV for CCM or TKIP (not used for WEP)
1219 * + Data payload
1220 * + 8-byte MIC (not used for CCM/WEP)
1221 * NOTE: Does not include Tx command bytes, post-MAC pad bytes,
1222 * MIC (CCM) 8 bytes, ICV (WEP/TKIP/CKIP) 4 bytes, CRC 4 bytes.i
1223 * Range: 14-2342 bytes.
1224 */
1225 __le16 len;
1226
1227 /*
1228 * MPDU or MSDU byte count for next frame.
1229 * Used for fragmentation and bursting, but not 11n aggregation.
1230 * Same as "len", but for next frame. Set to 0 if not applicable.
1231 */
1232 __le16 next_frame_len;
1233
1234 __le32 tx_flags; /* TX_CMD_FLG_* */
1235
1236 /* uCode may modify this field of the Tx command (in host DRAM!).
1237 * Driver must also set dram_lsb_ptr and dram_msb_ptr in this cmd. */
1238 struct iwl_dram_scratch scratch;
1239
1240 /* Rate for *all* Tx attempts, if TX_CMD_FLG_STA_RATE_MSK is cleared. */
1241 __le32 rate_n_flags; /* RATE_MCS_* */
1242
1243 /* Index of destination station in uCode's station table */
1244 u8 sta_id;
1245
1246 /* Type of security encryption: CCM or TKIP */
1247 u8 sec_ctl; /* TX_CMD_SEC_* */
1248
1249 /*
1250 * Index into rate table (see REPLY_TX_LINK_QUALITY_CMD) for initial
1251 * Tx attempt, if TX_CMD_FLG_STA_RATE_MSK is set. Normally "0" for
1252 * data frames, this field may be used to selectively reduce initial
1253 * rate (via non-0 value) for special frames (e.g. management), while
1254 * still supporting rate scaling for all frames.
1255 */
1256 u8 initial_rate_index;
1257 u8 reserved;
1258 u8 key[16];
1259 __le16 next_frame_flags;
1260 __le16 reserved2;
1261 union {
1262 __le32 life_time;
1263 __le32 attempt;
1264 } stop_time;
1265
1266 /* Host DRAM physical address pointer to "scratch" in this command.
1267 * Must be dword aligned. "0" in dram_lsb_ptr disables usage. */
1268 __le32 dram_lsb_ptr;
1269 u8 dram_msb_ptr;
1270
1271 u8 rts_retry_limit; /*byte 50 */
1272 u8 data_retry_limit; /*byte 51 */
1273 u8 tid_tspec;
1274 union {
1275 __le16 pm_frame_timeout;
1276 __le16 attempt_duration;
1277 } timeout;
1278
1279 /*
1280 * Duration of EDCA burst Tx Opportunity, in 32-usec units.
1281 * Set this if txop time is not specified by HCCA protocol (e.g. by AP).
1282 */
1283 __le16 driver_txop;
1284
1285 /*
1286 * MAC header goes here, followed by 2 bytes padding if MAC header
1287 * length is 26 or 30 bytes, followed by payload data
1288 */
1289 u8 payload[0];
1290 struct ieee80211_hdr hdr[0];
1291} __packed;
1292
1293/*
1294 * TX command response is sent after *agn* transmission attempts.
1295 *
1296 * both postpone and abort status are expected behavior from uCode. there is
1297 * no special operation required from driver; except for RFKILL_FLUSH,
1298 * which required tx flush host command to flush all the tx frames in queues
1299 */
1300enum {
1301 TX_STATUS_SUCCESS = 0x01,
1302 TX_STATUS_DIRECT_DONE = 0x02,
1303 /* postpone TX */
1304 TX_STATUS_POSTPONE_DELAY = 0x40,
1305 TX_STATUS_POSTPONE_FEW_BYTES = 0x41,
1306 TX_STATUS_POSTPONE_BT_PRIO = 0x42,
1307 TX_STATUS_POSTPONE_QUIET_PERIOD = 0x43,
1308 TX_STATUS_POSTPONE_CALC_TTAK = 0x44,
1309 /* abort TX */
1310 TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY = 0x81,
1311 TX_STATUS_FAIL_SHORT_LIMIT = 0x82,
1312 TX_STATUS_FAIL_LONG_LIMIT = 0x83,
1313 TX_STATUS_FAIL_FIFO_UNDERRUN = 0x84,
1314 TX_STATUS_FAIL_DRAIN_FLOW = 0x85,
1315 TX_STATUS_FAIL_RFKILL_FLUSH = 0x86,
1316 TX_STATUS_FAIL_LIFE_EXPIRE = 0x87,
1317 TX_STATUS_FAIL_DEST_PS = 0x88,
1318 TX_STATUS_FAIL_HOST_ABORTED = 0x89,
1319 TX_STATUS_FAIL_BT_RETRY = 0x8a,
1320 TX_STATUS_FAIL_STA_INVALID = 0x8b,
1321 TX_STATUS_FAIL_FRAG_DROPPED = 0x8c,
1322 TX_STATUS_FAIL_TID_DISABLE = 0x8d,
1323 TX_STATUS_FAIL_FIFO_FLUSHED = 0x8e,
1324 TX_STATUS_FAIL_INSUFFICIENT_CF_POLL = 0x8f,
1325 TX_STATUS_FAIL_PASSIVE_NO_RX = 0x90,
1326 TX_STATUS_FAIL_NO_BEACON_ON_RADAR = 0x91,
1327};
1328
1329#define TX_PACKET_MODE_REGULAR 0x0000
1330#define TX_PACKET_MODE_BURST_SEQ 0x0100
1331#define TX_PACKET_MODE_BURST_FIRST 0x0200
1332
1333enum {
1334 TX_POWER_PA_NOT_ACTIVE = 0x0,
1335};
1336
1337enum {
1338 TX_STATUS_MSK = 0x000000ff, /* bits 0:7 */
1339 TX_STATUS_DELAY_MSK = 0x00000040,
1340 TX_STATUS_ABORT_MSK = 0x00000080,
1341 TX_PACKET_MODE_MSK = 0x0000ff00, /* bits 8:15 */
1342 TX_FIFO_NUMBER_MSK = 0x00070000, /* bits 16:18 */
1343 TX_RESERVED = 0x00780000, /* bits 19:22 */
1344 TX_POWER_PA_DETECT_MSK = 0x7f800000, /* bits 23:30 */
1345 TX_ABORT_REQUIRED_MSK = 0x80000000, /* bits 31:31 */
1346};
1347
1348/* *******************************
1349 * TX aggregation status
1350 ******************************* */
1351
1352enum {
1353 AGG_TX_STATE_TRANSMITTED = 0x00,
1354 AGG_TX_STATE_UNDERRUN_MSK = 0x01,
1355 AGG_TX_STATE_BT_PRIO_MSK = 0x02,
1356 AGG_TX_STATE_FEW_BYTES_MSK = 0x04,
1357 AGG_TX_STATE_ABORT_MSK = 0x08,
1358 AGG_TX_STATE_LAST_SENT_TTL_MSK = 0x10,
1359 AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK = 0x20,
1360 AGG_TX_STATE_LAST_SENT_BT_KILL_MSK = 0x40,
1361 AGG_TX_STATE_SCD_QUERY_MSK = 0x80,
1362 AGG_TX_STATE_TEST_BAD_CRC32_MSK = 0x100,
1363 AGG_TX_STATE_RESPONSE_MSK = 0x1ff,
1364 AGG_TX_STATE_DUMP_TX_MSK = 0x200,
1365 AGG_TX_STATE_DELAY_TX_MSK = 0x400
1366};
1367
1368#define AGG_TX_STATUS_MSK 0x00000fff /* bits 0:11 */
1369#define AGG_TX_TRY_MSK 0x0000f000 /* bits 12:15 */
1370
1371#define AGG_TX_STATE_LAST_SENT_MSK (AGG_TX_STATE_LAST_SENT_TTL_MSK | \
1372 AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK | \
1373 AGG_TX_STATE_LAST_SENT_BT_KILL_MSK)
1374
1375/* # tx attempts for first frame in aggregation */
1376#define AGG_TX_STATE_TRY_CNT_POS 12
1377#define AGG_TX_STATE_TRY_CNT_MSK 0xf000
1378
1379/* Command ID and sequence number of Tx command for this frame */
1380#define AGG_TX_STATE_SEQ_NUM_POS 16
1381#define AGG_TX_STATE_SEQ_NUM_MSK 0xffff0000
1382
1383/*
1384 * REPLY_TX = 0x1c (response)
1385 *
1386 * This response may be in one of two slightly different formats, indicated
1387 * by the frame_count field:
1388 *
1389 * 1) No aggregation (frame_count == 1). This reports Tx results for
1390 * a single frame. Multiple attempts, at various bit rates, may have
1391 * been made for this frame.
1392 *
1393 * 2) Aggregation (frame_count > 1). This reports Tx results for
1394 * 2 or more frames that used block-acknowledge. All frames were
1395 * transmitted at same rate. Rate scaling may have been used if first
1396 * frame in this new agg block failed in previous agg block(s).
1397 *
1398 * Note that, for aggregation, ACK (block-ack) status is not delivered here;
1399 * block-ack has not been received by the time the agn device records
1400 * this status.
1401 * This status relates to reasons the tx might have been blocked or aborted
1402 * within the sending station (this agn device), rather than whether it was
1403 * received successfully by the destination station.
1404 */
1405struct agg_tx_status {
1406 __le16 status;
1407 __le16 sequence;
1408} __packed;
1409
1410/*
1411 * definitions for initial rate index field
1412 * bits [3:0] initial rate index
1413 * bits [6:4] rate table color, used for the initial rate
1414 * bit-7 invalid rate indication
1415 * i.e. rate was not chosen from rate table
1416 * or rate table color was changed during frame retries
1417 * refer tlc rate info
1418 */
1419
1420#define IWL50_TX_RES_INIT_RATE_INDEX_POS 0
1421#define IWL50_TX_RES_INIT_RATE_INDEX_MSK 0x0f
1422#define IWL50_TX_RES_RATE_TABLE_COLOR_POS 4
1423#define IWL50_TX_RES_RATE_TABLE_COLOR_MSK 0x70
1424#define IWL50_TX_RES_INV_RATE_INDEX_MSK 0x80
1425
1426/* refer to ra_tid */
1427#define IWLAGN_TX_RES_TID_POS 0
1428#define IWLAGN_TX_RES_TID_MSK 0x0f
1429#define IWLAGN_TX_RES_RA_POS 4
1430#define IWLAGN_TX_RES_RA_MSK 0xf0
1431
1432struct iwlagn_tx_resp {
1433 u8 frame_count; /* 1 no aggregation, >1 aggregation */
1434 u8 bt_kill_count; /* # blocked by bluetooth (unused for agg) */
1435 u8 failure_rts; /* # failures due to unsuccessful RTS */
1436 u8 failure_frame; /* # failures due to no ACK (unused for agg) */
1437
1438 /* For non-agg: Rate at which frame was successful.
1439 * For agg: Rate at which all frames were transmitted. */
1440 __le32 rate_n_flags; /* RATE_MCS_* */
1441
1442 /* For non-agg: RTS + CTS + frame tx attempts time + ACK.
1443 * For agg: RTS + CTS + aggregation tx time + block-ack time. */
1444 __le16 wireless_media_time; /* uSecs */
1445
1446 u8 pa_status; /* RF power amplifier measurement (not used) */
1447 u8 pa_integ_res_a[3];
1448 u8 pa_integ_res_b[3];
1449 u8 pa_integ_res_C[3];
1450
1451 __le32 tfd_info;
1452 __le16 seq_ctl;
1453 __le16 byte_cnt;
1454 u8 tlc_info;
1455 u8 ra_tid; /* tid (0:3), sta_id (4:7) */
1456 __le16 frame_ctrl;
1457 /*
1458 * For non-agg: frame status TX_STATUS_*
1459 * For agg: status of 1st frame, AGG_TX_STATE_*; other frame status
1460 * fields follow this one, up to frame_count.
1461 * Bit fields:
1462 * 11- 0: AGG_TX_STATE_* status code
1463 * 15-12: Retry count for 1st frame in aggregation (retries
1464 * occur if tx failed for this frame when it was a
1465 * member of a previous aggregation block). If rate
1466 * scaling is used, retry count indicates the rate
1467 * table entry used for all frames in the new agg.
1468 * 31-16: Sequence # for this frame's Tx cmd (not SSN!)
1469 */
1470 struct agg_tx_status status; /* TX status (in aggregation -
1471 * status of 1st frame) */
1472} __packed;
1473/*
1474 * REPLY_COMPRESSED_BA = 0xc5 (response only, not a command)
1475 *
1476 * Reports Block-Acknowledge from recipient station
1477 */
1478struct iwl_compressed_ba_resp {
1479 __le32 sta_addr_lo32;
1480 __le16 sta_addr_hi16;
1481 __le16 reserved;
1482
1483 /* Index of recipient (BA-sending) station in uCode's station table */
1484 u8 sta_id;
1485 u8 tid;
1486 __le16 seq_ctl;
1487 __le64 bitmap;
1488 __le16 scd_flow;
1489 __le16 scd_ssn;
1490 u8 txed; /* number of frames sent */
1491 u8 txed_2_done; /* number of frames acked */
1492} __packed;
1493
1494/*
1495 * REPLY_TX_PWR_TABLE_CMD = 0x97 (command, has simple generic response)
1496 *
1497 */
1498
1499/*RS_NEW_API: only TLC_RTS remains and moved to bit 0 */
1500#define LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK (1 << 0)
1501
1502/* # of EDCA prioritized tx fifos */
1503#define LINK_QUAL_AC_NUM AC_NUM
1504
1505/* # entries in rate scale table to support Tx retries */
1506#define LINK_QUAL_MAX_RETRY_NUM 16
1507
1508/* Tx antenna selection values */
1509#define LINK_QUAL_ANT_A_MSK (1 << 0)
1510#define LINK_QUAL_ANT_B_MSK (1 << 1)
1511#define LINK_QUAL_ANT_MSK (LINK_QUAL_ANT_A_MSK|LINK_QUAL_ANT_B_MSK)
1512
1513
1514/**
1515 * struct iwl_link_qual_general_params
1516 *
1517 * Used in REPLY_TX_LINK_QUALITY_CMD
1518 */
1519struct iwl_link_qual_general_params {
1520 u8 flags;
1521
1522 /* No entries at or above this (driver chosen) index contain MIMO */
1523 u8 mimo_delimiter;
1524
1525 /* Best single antenna to use for single stream (legacy, SISO). */
1526 u8 single_stream_ant_msk; /* LINK_QUAL_ANT_* */
1527
1528 /* Best antennas to use for MIMO (unused for 4965, assumes both). */
1529 u8 dual_stream_ant_msk; /* LINK_QUAL_ANT_* */
1530
1531 /*
1532 * If driver needs to use different initial rates for different
1533 * EDCA QOS access categories (as implemented by tx fifos 0-3),
1534 * this table will set that up, by indicating the indexes in the
1535 * rs_table[LINK_QUAL_MAX_RETRY_NUM] rate table at which to start.
1536 * Otherwise, driver should set all entries to 0.
1537 *
1538 * Entry usage:
1539 * 0 = Background, 1 = Best Effort (normal), 2 = Video, 3 = Voice
1540 * TX FIFOs above 3 use same value (typically 0) as TX FIFO 3.
1541 */
1542 u8 start_rate_index[LINK_QUAL_AC_NUM];
1543} __packed;
1544
1545#define LINK_QUAL_AGG_TIME_LIMIT_DEF (4000) /* 4 milliseconds */
1546#define LINK_QUAL_AGG_TIME_LIMIT_MAX (8000)
1547#define LINK_QUAL_AGG_TIME_LIMIT_MIN (100)
1548
1549#define LINK_QUAL_AGG_DISABLE_START_DEF (3)
1550#define LINK_QUAL_AGG_DISABLE_START_MAX (255)
1551#define LINK_QUAL_AGG_DISABLE_START_MIN (0)
1552
1553#define LINK_QUAL_AGG_FRAME_LIMIT_DEF (63)
1554#define LINK_QUAL_AGG_FRAME_LIMIT_MAX (63)
1555#define LINK_QUAL_AGG_FRAME_LIMIT_MIN (0)
1556
1557/**
1558 * struct iwl_link_qual_agg_params
1559 *
1560 * Used in REPLY_TX_LINK_QUALITY_CMD
1561 */
1562struct iwl_link_qual_agg_params {
1563
1564 /*
1565 *Maximum number of uSec in aggregation.
1566 * default set to 4000 (4 milliseconds) if not configured in .cfg
1567 */
1568 __le16 agg_time_limit;
1569
1570 /*
1571 * Number of Tx retries allowed for a frame, before that frame will
1572 * no longer be considered for the start of an aggregation sequence
1573 * (scheduler will then try to tx it as single frame).
1574 * Driver should set this to 3.
1575 */
1576 u8 agg_dis_start_th;
1577
1578 /*
1579 * Maximum number of frames in aggregation.
1580 * 0 = no limit (default). 1 = no aggregation.
1581 * Other values = max # frames in aggregation.
1582 */
1583 u8 agg_frame_cnt_limit;
1584
1585 __le32 reserved;
1586} __packed;
1587
1588/*
1589 * REPLY_TX_LINK_QUALITY_CMD = 0x4e (command, has simple generic response)
1590 *
1591 * For agn devices
1592 *
1593 * Each station in the agn device's internal station table has its own table
1594 * of 16
1595 * Tx rates and modulation modes (e.g. legacy/SISO/MIMO) for retrying Tx when
1596 * an ACK is not received. This command replaces the entire table for
1597 * one station.
1598 *
1599 * NOTE: Station must already be in agn device's station table.
1600 * Use REPLY_ADD_STA.
1601 *
1602 * The rate scaling procedures described below work well. Of course, other
1603 * procedures are possible, and may work better for particular environments.
1604 *
1605 *
1606 * FILLING THE RATE TABLE
1607 *
1608 * Given a particular initial rate and mode, as determined by the rate
1609 * scaling algorithm described below, the Linux driver uses the following
1610 * formula to fill the rs_table[LINK_QUAL_MAX_RETRY_NUM] rate table in the
1611 * Link Quality command:
1612 *
1613 *
1614 * 1) If using High-throughput (HT) (SISO or MIMO) initial rate:
1615 * a) Use this same initial rate for first 3 entries.
1616 * b) Find next lower available rate using same mode (SISO or MIMO),
1617 * use for next 3 entries. If no lower rate available, switch to
1618 * legacy mode (no HT40 channel, no MIMO, no short guard interval).
1619 * c) If using MIMO, set command's mimo_delimiter to number of entries
1620 * using MIMO (3 or 6).
1621 * d) After trying 2 HT rates, switch to legacy mode (no HT40 channel,
1622 * no MIMO, no short guard interval), at the next lower bit rate
1623 * (e.g. if second HT bit rate was 54, try 48 legacy), and follow
1624 * legacy procedure for remaining table entries.
1625 *
1626 * 2) If using legacy initial rate:
1627 * a) Use the initial rate for only one entry.
1628 * b) For each following entry, reduce the rate to next lower available
1629 * rate, until reaching the lowest available rate.
1630 * c) When reducing rate, also switch antenna selection.
1631 * d) Once lowest available rate is reached, repeat this rate until
1632 * rate table is filled (16 entries), switching antenna each entry.
1633 *
1634 *
1635 * ACCUMULATING HISTORY
1636 *
1637 * The rate scaling algorithm for agn devices, as implemented in Linux driver,
1638 * uses two sets of frame Tx success history: One for the current/active
1639 * modulation mode, and one for a speculative/search mode that is being
1640 * attempted. If the speculative mode turns out to be more effective (i.e.
1641 * actual transfer rate is better), then the driver continues to use the
1642 * speculative mode as the new current active mode.
1643 *
1644 * Each history set contains, separately for each possible rate, data for a
1645 * sliding window of the 62 most recent tx attempts at that rate. The data
1646 * includes a shifting bitmap of success(1)/failure(0), and sums of successful
1647 * and attempted frames, from which the driver can additionally calculate a
1648 * success ratio (success / attempted) and number of failures
1649 * (attempted - success), and control the size of the window (attempted).
1650 * The driver uses the bit map to remove successes from the success sum, as
1651 * the oldest tx attempts fall out of the window.
1652 *
1653 * When the agn device makes multiple tx attempts for a given frame, each
1654 * attempt might be at a different rate, and have different modulation
1655 * characteristics (e.g. antenna, fat channel, short guard interval), as set
1656 * up in the rate scaling table in the Link Quality command. The driver must
1657 * determine which rate table entry was used for each tx attempt, to determine
1658 * which rate-specific history to update, and record only those attempts that
1659 * match the modulation characteristics of the history set.
1660 *
1661 * When using block-ack (aggregation), all frames are transmitted at the same
1662 * rate, since there is no per-attempt acknowledgment from the destination
1663 * station. The Tx response struct iwl_tx_resp indicates the Tx rate in
1664 * rate_n_flags field. After receiving a block-ack, the driver can update
1665 * history for the entire block all at once.
1666 *
1667 *
1668 * FINDING BEST STARTING RATE:
1669 *
1670 * When working with a selected initial modulation mode (see below), the
1671 * driver attempts to find a best initial rate. The initial rate is the
1672 * first entry in the Link Quality command's rate table.
1673 *
1674 * 1) Calculate actual throughput (success ratio * expected throughput, see
1675 * table below) for current initial rate. Do this only if enough frames
1676 * have been attempted to make the value meaningful: at least 6 failed
1677 * tx attempts, or at least 8 successes. If not enough, don't try rate
1678 * scaling yet.
1679 *
1680 * 2) Find available rates adjacent to current initial rate. Available means:
1681 * a) supported by hardware &&
1682 * b) supported by association &&
1683 * c) within any constraints selected by user
1684 *
1685 * 3) Gather measured throughputs for adjacent rates. These might not have
1686 * enough history to calculate a throughput. That's okay, we might try
1687 * using one of them anyway!
1688 *
1689 * 4) Try decreasing rate if, for current rate:
1690 * a) success ratio is < 15% ||
1691 * b) lower adjacent rate has better measured throughput ||
1692 * c) higher adjacent rate has worse throughput, and lower is unmeasured
1693 *
1694 * As a sanity check, if decrease was determined above, leave rate
1695 * unchanged if:
1696 * a) lower rate unavailable
1697 * b) success ratio at current rate > 85% (very good)
1698 * c) current measured throughput is better than expected throughput
1699 * of lower rate (under perfect 100% tx conditions, see table below)
1700 *
1701 * 5) Try increasing rate if, for current rate:
1702 * a) success ratio is < 15% ||
1703 * b) both adjacent rates' throughputs are unmeasured (try it!) ||
1704 * b) higher adjacent rate has better measured throughput ||
1705 * c) lower adjacent rate has worse throughput, and higher is unmeasured
1706 *
1707 * As a sanity check, if increase was determined above, leave rate
1708 * unchanged if:
1709 * a) success ratio at current rate < 70%. This is not particularly
1710 * good performance; higher rate is sure to have poorer success.
1711 *
1712 * 6) Re-evaluate the rate after each tx frame. If working with block-
1713 * acknowledge, history and statistics may be calculated for the entire
1714 * block (including prior history that fits within the history windows),
1715 * before re-evaluation.
1716 *
1717 * FINDING BEST STARTING MODULATION MODE:
1718 *
1719 * After working with a modulation mode for a "while" (and doing rate scaling),
1720 * the driver searches for a new initial mode in an attempt to improve
1721 * throughput. The "while" is measured by numbers of attempted frames:
1722 *
1723 * For legacy mode, search for new mode after:
1724 * 480 successful frames, or 160 failed frames
1725 * For high-throughput modes (SISO or MIMO), search for new mode after:
1726 * 4500 successful frames, or 400 failed frames
1727 *
1728 * Mode switch possibilities are (3 for each mode):
1729 *
1730 * For legacy:
1731 * Change antenna, try SISO (if HT association), try MIMO (if HT association)
1732 * For SISO:
1733 * Change antenna, try MIMO, try shortened guard interval (SGI)
1734 * For MIMO:
1735 * Try SISO antenna A, SISO antenna B, try shortened guard interval (SGI)
1736 *
1737 * When trying a new mode, use the same bit rate as the old/current mode when
1738 * trying antenna switches and shortened guard interval. When switching to
1739 * SISO from MIMO or legacy, or to MIMO from SISO or legacy, use a rate
1740 * for which the expected throughput (under perfect conditions) is about the
1741 * same or slightly better than the actual measured throughput delivered by
1742 * the old/current mode.
1743 *
1744 * Actual throughput can be estimated by multiplying the expected throughput
1745 * by the success ratio (successful / attempted tx frames). Frame size is
1746 * not considered in this calculation; it assumes that frame size will average
1747 * out to be fairly consistent over several samples. The following are
1748 * metric values for expected throughput assuming 100% success ratio.
1749 * Only G band has support for CCK rates:
1750 *
1751 * RATE: 1 2 5 11 6 9 12 18 24 36 48 54 60
1752 *
1753 * G: 7 13 35 58 40 57 72 98 121 154 177 186 186
1754 * A: 0 0 0 0 40 57 72 98 121 154 177 186 186
1755 * SISO 20MHz: 0 0 0 0 42 42 76 102 124 159 183 193 202
1756 * SGI SISO 20MHz: 0 0 0 0 46 46 82 110 132 168 192 202 211
1757 * MIMO 20MHz: 0 0 0 0 74 74 123 155 179 214 236 244 251
1758 * SGI MIMO 20MHz: 0 0 0 0 81 81 131 164 188 222 243 251 257
1759 * SISO 40MHz: 0 0 0 0 77 77 127 160 184 220 242 250 257
1760 * SGI SISO 40MHz: 0 0 0 0 83 83 135 169 193 229 250 257 264
1761 * MIMO 40MHz: 0 0 0 0 123 123 182 214 235 264 279 285 289
1762 * SGI MIMO 40MHz: 0 0 0 0 131 131 191 222 242 270 284 289 293
1763 *
1764 * After the new mode has been tried for a short while (minimum of 6 failed
1765 * frames or 8 successful frames), compare success ratio and actual throughput
1766 * estimate of the new mode with the old. If either is better with the new
1767 * mode, continue to use the new mode.
1768 *
1769 * Continue comparing modes until all 3 possibilities have been tried.
1770 * If moving from legacy to HT, try all 3 possibilities from the new HT
1771 * mode. After trying all 3, a best mode is found. Continue to use this mode
1772 * for the longer "while" described above (e.g. 480 successful frames for
1773 * legacy), and then repeat the search process.
1774 *
1775 */
1776struct iwl_link_quality_cmd {
1777
1778 /* Index of destination/recipient station in uCode's station table */
1779 u8 sta_id;
1780 u8 reserved1;
1781 __le16 control; /* not used */
1782 struct iwl_link_qual_general_params general_params;
1783 struct iwl_link_qual_agg_params agg_params;
1784
1785 /*
1786 * Rate info; when using rate-scaling, Tx command's initial_rate_index
1787 * specifies 1st Tx rate attempted, via index into this table.
1788 * agn devices works its way through table when retrying Tx.
1789 */
1790 struct {
1791 __le32 rate_n_flags; /* RATE_MCS_*, IWL_RATE_* */
1792 } rs_table[LINK_QUAL_MAX_RETRY_NUM];
1793 __le32 reserved2;
1794} __packed;
1795
1796/*
1797 * BT configuration enable flags:
1798 * bit 0 - 1: BT channel announcement enabled
1799 * 0: disable
1800 * bit 1 - 1: priority of BT device enabled
1801 * 0: disable
1802 * bit 2 - 1: BT 2 wire support enabled
1803 * 0: disable
1804 */
1805#define BT_COEX_DISABLE (0x0)
1806#define BT_ENABLE_CHANNEL_ANNOUNCE BIT(0)
1807#define BT_ENABLE_PRIORITY BIT(1)
1808#define BT_ENABLE_2_WIRE BIT(2)
1809
1810#define BT_COEX_DISABLE (0x0)
1811#define BT_COEX_ENABLE (BT_ENABLE_CHANNEL_ANNOUNCE | BT_ENABLE_PRIORITY)
1812
1813#define BT_LEAD_TIME_MIN (0x0)
1814#define BT_LEAD_TIME_DEF (0x1E)
1815#define BT_LEAD_TIME_MAX (0xFF)
1816
1817#define BT_MAX_KILL_MIN (0x1)
1818#define BT_MAX_KILL_DEF (0x5)
1819#define BT_MAX_KILL_MAX (0xFF)
1820
1821#define BT_DURATION_LIMIT_DEF 625
1822#define BT_DURATION_LIMIT_MAX 1250
1823#define BT_DURATION_LIMIT_MIN 625
1824
1825#define BT_ON_THRESHOLD_DEF 4
1826#define BT_ON_THRESHOLD_MAX 1000
1827#define BT_ON_THRESHOLD_MIN 1
1828
1829#define BT_FRAG_THRESHOLD_DEF 0
1830#define BT_FRAG_THRESHOLD_MAX 0
1831#define BT_FRAG_THRESHOLD_MIN 0
1832
1833#define BT_AGG_THRESHOLD_DEF 1200
1834#define BT_AGG_THRESHOLD_MAX 8000
1835#define BT_AGG_THRESHOLD_MIN 400
1836
1837/*
1838 * REPLY_BT_CONFIG = 0x9b (command, has simple generic response)
1839 *
1840 * agn devices support hardware handshake with Bluetooth device on
1841 * same platform. Bluetooth device alerts wireless device when it will Tx;
1842 * wireless device can delay or kill its own Tx to accommodate.
1843 */
1844struct iwl_bt_cmd {
1845 u8 flags;
1846 u8 lead_time;
1847 u8 max_kill;
1848 u8 reserved;
1849 __le32 kill_ack_mask;
1850 __le32 kill_cts_mask;
1851} __packed;
1852
1853#define IWLAGN_BT_FLAG_CHANNEL_INHIBITION BIT(0)
1854
1855#define IWLAGN_BT_FLAG_COEX_MODE_MASK (BIT(3)|BIT(4)|BIT(5))
1856#define IWLAGN_BT_FLAG_COEX_MODE_SHIFT 3
1857#define IWLAGN_BT_FLAG_COEX_MODE_DISABLED 0
1858#define IWLAGN_BT_FLAG_COEX_MODE_LEGACY_2W 1
1859#define IWLAGN_BT_FLAG_COEX_MODE_3W 2
1860#define IWLAGN_BT_FLAG_COEX_MODE_4W 3
1861
1862#define IWLAGN_BT_FLAG_UCODE_DEFAULT BIT(6)
1863/* Disable Sync PSPoll on SCO/eSCO */
1864#define IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE BIT(7)
1865
1866#define IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD -75 /* dBm */
1867#define IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD -65 /* dBm */
1868
1869#define IWLAGN_BT_PRIO_BOOST_MAX 0xFF
1870#define IWLAGN_BT_PRIO_BOOST_MIN 0x00
1871#define IWLAGN_BT_PRIO_BOOST_DEFAULT 0xF0
1872
1873#define IWLAGN_BT_MAX_KILL_DEFAULT 5
1874
1875#define IWLAGN_BT3_T7_DEFAULT 1
1876
1877enum iwl_bt_kill_idx {
1878 IWL_BT_KILL_DEFAULT = 0,
1879 IWL_BT_KILL_OVERRIDE = 1,
1880 IWL_BT_KILL_REDUCE = 2,
1881};
1882
1883#define IWLAGN_BT_KILL_ACK_MASK_DEFAULT cpu_to_le32(0xffff0000)
1884#define IWLAGN_BT_KILL_CTS_MASK_DEFAULT cpu_to_le32(0xffff0000)
1885#define IWLAGN_BT_KILL_ACK_CTS_MASK_SCO cpu_to_le32(0xffffffff)
1886#define IWLAGN_BT_KILL_ACK_CTS_MASK_REDUCE cpu_to_le32(0)
1887
1888#define IWLAGN_BT3_PRIO_SAMPLE_DEFAULT 2
1889
1890#define IWLAGN_BT3_T2_DEFAULT 0xc
1891
1892#define IWLAGN_BT_VALID_ENABLE_FLAGS cpu_to_le16(BIT(0))
1893#define IWLAGN_BT_VALID_BOOST cpu_to_le16(BIT(1))
1894#define IWLAGN_BT_VALID_MAX_KILL cpu_to_le16(BIT(2))
1895#define IWLAGN_BT_VALID_3W_TIMERS cpu_to_le16(BIT(3))
1896#define IWLAGN_BT_VALID_KILL_ACK_MASK cpu_to_le16(BIT(4))
1897#define IWLAGN_BT_VALID_KILL_CTS_MASK cpu_to_le16(BIT(5))
1898#define IWLAGN_BT_VALID_REDUCED_TX_PWR cpu_to_le16(BIT(6))
1899#define IWLAGN_BT_VALID_3W_LUT cpu_to_le16(BIT(7))
1900
1901#define IWLAGN_BT_ALL_VALID_MSK (IWLAGN_BT_VALID_ENABLE_FLAGS | \
1902 IWLAGN_BT_VALID_BOOST | \
1903 IWLAGN_BT_VALID_MAX_KILL | \
1904 IWLAGN_BT_VALID_3W_TIMERS | \
1905 IWLAGN_BT_VALID_KILL_ACK_MASK | \
1906 IWLAGN_BT_VALID_KILL_CTS_MASK | \
1907 IWLAGN_BT_VALID_REDUCED_TX_PWR | \
1908 IWLAGN_BT_VALID_3W_LUT)
1909
1910#define IWLAGN_BT_REDUCED_TX_PWR BIT(0)
1911
1912#define IWLAGN_BT_DECISION_LUT_SIZE 12
1913
1914struct iwl_basic_bt_cmd {
1915 u8 flags;
1916 u8 ledtime; /* unused */
1917 u8 max_kill;
1918 u8 bt3_timer_t7_value;
1919 __le32 kill_ack_mask;
1920 __le32 kill_cts_mask;
1921 u8 bt3_prio_sample_time;
1922 u8 bt3_timer_t2_value;
1923 __le16 bt4_reaction_time; /* unused */
1924 __le32 bt3_lookup_table[IWLAGN_BT_DECISION_LUT_SIZE];
1925 /*
1926 * bit 0: use reduced tx power for control frame
1927 * bit 1 - 7: reserved
1928 */
1929 u8 reduce_txpower;
1930 u8 reserved;
1931 __le16 valid;
1932};
1933
1934struct iwl_bt_cmd_v1 {
1935 struct iwl_basic_bt_cmd basic;
1936 u8 prio_boost;
1937 /*
1938 * set IWLAGN_BT_VALID_BOOST to "1" in "valid" bitmask
1939 * if configure the following patterns
1940 */
1941 u8 tx_prio_boost; /* SW boost of WiFi tx priority */
1942 __le16 rx_prio_boost; /* SW boost of WiFi rx priority */
1943};
1944
1945struct iwl_bt_cmd_v2 {
1946 struct iwl_basic_bt_cmd basic;
1947 __le32 prio_boost;
1948 /*
1949 * set IWLAGN_BT_VALID_BOOST to "1" in "valid" bitmask
1950 * if configure the following patterns
1951 */
1952 u8 reserved;
1953 u8 tx_prio_boost; /* SW boost of WiFi tx priority */
1954 __le16 rx_prio_boost; /* SW boost of WiFi rx priority */
1955};
1956
1957#define IWLAGN_BT_SCO_ACTIVE cpu_to_le32(BIT(0))
1958
1959struct iwlagn_bt_sco_cmd {
1960 __le32 flags;
1961};
1962
1963/******************************************************************************
1964 * (6)
1965 * Spectrum Management (802.11h) Commands, Responses, Notifications:
1966 *
1967 *****************************************************************************/
1968
1969/*
1970 * Spectrum Management
1971 */
1972#define MEASUREMENT_FILTER_FLAG (RXON_FILTER_PROMISC_MSK | \
1973 RXON_FILTER_CTL2HOST_MSK | \
1974 RXON_FILTER_ACCEPT_GRP_MSK | \
1975 RXON_FILTER_DIS_DECRYPT_MSK | \
1976 RXON_FILTER_DIS_GRP_DECRYPT_MSK | \
1977 RXON_FILTER_ASSOC_MSK | \
1978 RXON_FILTER_BCON_AWARE_MSK)
1979
1980struct iwl_measure_channel {
1981 __le32 duration; /* measurement duration in extended beacon
1982 * format */
1983 u8 channel; /* channel to measure */
1984 u8 type; /* see enum iwl_measure_type */
1985 __le16 reserved;
1986} __packed;
1987
1988/*
1989 * REPLY_SPECTRUM_MEASUREMENT_CMD = 0x74 (command)
1990 */
1991struct iwl_spectrum_cmd {
1992 __le16 len; /* number of bytes starting from token */
1993 u8 token; /* token id */
1994 u8 id; /* measurement id -- 0 or 1 */
1995 u8 origin; /* 0 = TGh, 1 = other, 2 = TGk */
1996 u8 periodic; /* 1 = periodic */
1997 __le16 path_loss_timeout;
1998 __le32 start_time; /* start time in extended beacon format */
1999 __le32 reserved2;
2000 __le32 flags; /* rxon flags */
2001 __le32 filter_flags; /* rxon filter flags */
2002 __le16 channel_count; /* minimum 1, maximum 10 */
2003 __le16 reserved3;
2004 struct iwl_measure_channel channels[10];
2005} __packed;
2006
2007/*
2008 * REPLY_SPECTRUM_MEASUREMENT_CMD = 0x74 (response)
2009 */
2010struct iwl_spectrum_resp {
2011 u8 token;
2012 u8 id; /* id of the prior command replaced, or 0xff */
2013 __le16 status; /* 0 - command will be handled
2014 * 1 - cannot handle (conflicts with another
2015 * measurement) */
2016} __packed;
2017
2018enum iwl_measurement_state {
2019 IWL_MEASUREMENT_START = 0,
2020 IWL_MEASUREMENT_STOP = 1,
2021};
2022
2023enum iwl_measurement_status {
2024 IWL_MEASUREMENT_OK = 0,
2025 IWL_MEASUREMENT_CONCURRENT = 1,
2026 IWL_MEASUREMENT_CSA_CONFLICT = 2,
2027 IWL_MEASUREMENT_TGH_CONFLICT = 3,
2028 /* 4-5 reserved */
2029 IWL_MEASUREMENT_STOPPED = 6,
2030 IWL_MEASUREMENT_TIMEOUT = 7,
2031 IWL_MEASUREMENT_PERIODIC_FAILED = 8,
2032};
2033
2034#define NUM_ELEMENTS_IN_HISTOGRAM 8
2035
2036struct iwl_measurement_histogram {
2037 __le32 ofdm[NUM_ELEMENTS_IN_HISTOGRAM]; /* in 0.8usec counts */
2038 __le32 cck[NUM_ELEMENTS_IN_HISTOGRAM]; /* in 1usec counts */
2039} __packed;
2040
2041/* clear channel availability counters */
2042struct iwl_measurement_cca_counters {
2043 __le32 ofdm;
2044 __le32 cck;
2045} __packed;
2046
2047enum iwl_measure_type {
2048 IWL_MEASURE_BASIC = (1 << 0),
2049 IWL_MEASURE_CHANNEL_LOAD = (1 << 1),
2050 IWL_MEASURE_HISTOGRAM_RPI = (1 << 2),
2051 IWL_MEASURE_HISTOGRAM_NOISE = (1 << 3),
2052 IWL_MEASURE_FRAME = (1 << 4),
2053 /* bits 5:6 are reserved */
2054 IWL_MEASURE_IDLE = (1 << 7),
2055};
2056
2057/*
2058 * SPECTRUM_MEASURE_NOTIFICATION = 0x75 (notification only, not a command)
2059 */
2060struct iwl_spectrum_notification {
2061 u8 id; /* measurement id -- 0 or 1 */
2062 u8 token;
2063 u8 channel_index; /* index in measurement channel list */
2064 u8 state; /* 0 - start, 1 - stop */
2065 __le32 start_time; /* lower 32-bits of TSF */
2066 u8 band; /* 0 - 5.2GHz, 1 - 2.4GHz */
2067 u8 channel;
2068 u8 type; /* see enum iwl_measurement_type */
2069 u8 reserved1;
2070 /* NOTE: cca_ofdm, cca_cck, basic_type, and histogram are only only
2071 * valid if applicable for measurement type requested. */
2072 __le32 cca_ofdm; /* cca fraction time in 40Mhz clock periods */
2073 __le32 cca_cck; /* cca fraction time in 44Mhz clock periods */
2074 __le32 cca_time; /* channel load time in usecs */
2075 u8 basic_type; /* 0 - bss, 1 - ofdm preamble, 2 -
2076 * unidentified */
2077 u8 reserved2[3];
2078 struct iwl_measurement_histogram histogram;
2079 __le32 stop_time; /* lower 32-bits of TSF */
2080 __le32 status; /* see iwl_measurement_status */
2081} __packed;
2082
2083/******************************************************************************
2084 * (7)
2085 * Power Management Commands, Responses, Notifications:
2086 *
2087 *****************************************************************************/
2088
2089/**
2090 * struct iwl_powertable_cmd - Power Table Command
2091 * @flags: See below:
2092 *
2093 * POWER_TABLE_CMD = 0x77 (command, has simple generic response)
2094 *
2095 * PM allow:
2096 * bit 0 - '0' Driver not allow power management
2097 * '1' Driver allow PM (use rest of parameters)
2098 *
2099 * uCode send sleep notifications:
2100 * bit 1 - '0' Don't send sleep notification
2101 * '1' send sleep notification (SEND_PM_NOTIFICATION)
2102 *
2103 * Sleep over DTIM
2104 * bit 2 - '0' PM have to walk up every DTIM
2105 * '1' PM could sleep over DTIM till listen Interval.
2106 *
2107 * PCI power managed
2108 * bit 3 - '0' (PCI_CFG_LINK_CTRL & 0x1)
2109 * '1' !(PCI_CFG_LINK_CTRL & 0x1)
2110 *
2111 * Fast PD
2112 * bit 4 - '1' Put radio to sleep when receiving frame for others
2113 *
2114 * Force sleep Modes
2115 * bit 31/30- '00' use both mac/xtal sleeps
2116 * '01' force Mac sleep
2117 * '10' force xtal sleep
2118 * '11' Illegal set
2119 *
2120 * NOTE: if sleep_interval[SLEEP_INTRVL_TABLE_SIZE-1] > DTIM period then
2121 * ucode assume sleep over DTIM is allowed and we don't need to wake up
2122 * for every DTIM.
2123 */
2124#define IWL_POWER_VEC_SIZE 5
2125
2126#define IWL_POWER_DRIVER_ALLOW_SLEEP_MSK cpu_to_le16(BIT(0))
2127#define IWL_POWER_POWER_SAVE_ENA_MSK cpu_to_le16(BIT(0))
2128#define IWL_POWER_POWER_MANAGEMENT_ENA_MSK cpu_to_le16(BIT(1))
2129#define IWL_POWER_SLEEP_OVER_DTIM_MSK cpu_to_le16(BIT(2))
2130#define IWL_POWER_PCI_PM_MSK cpu_to_le16(BIT(3))
2131#define IWL_POWER_FAST_PD cpu_to_le16(BIT(4))
2132#define IWL_POWER_BEACON_FILTERING cpu_to_le16(BIT(5))
2133#define IWL_POWER_SHADOW_REG_ENA cpu_to_le16(BIT(6))
2134#define IWL_POWER_CT_KILL_SET cpu_to_le16(BIT(7))
2135#define IWL_POWER_BT_SCO_ENA cpu_to_le16(BIT(8))
2136#define IWL_POWER_ADVANCE_PM_ENA_MSK cpu_to_le16(BIT(9))
2137
2138struct iwl_powertable_cmd {
2139 __le16 flags;
2140 u8 keep_alive_seconds;
2141 u8 debug_flags;
2142 __le32 rx_data_timeout;
2143 __le32 tx_data_timeout;
2144 __le32 sleep_interval[IWL_POWER_VEC_SIZE];
2145 __le32 keep_alive_beacons;
2146} __packed;
2147
2148/*
2149 * PM_SLEEP_NOTIFICATION = 0x7A (notification only, not a command)
2150 * all devices identical.
2151 */
2152struct iwl_sleep_notification {
2153 u8 pm_sleep_mode;
2154 u8 pm_wakeup_src;
2155 __le16 reserved;
2156 __le32 sleep_time;
2157 __le32 tsf_low;
2158 __le32 bcon_timer;
2159} __packed;
2160
2161/* Sleep states. all devices identical. */
2162enum {
2163 IWL_PM_NO_SLEEP = 0,
2164 IWL_PM_SLP_MAC = 1,
2165 IWL_PM_SLP_FULL_MAC_UNASSOCIATE = 2,
2166 IWL_PM_SLP_FULL_MAC_CARD_STATE = 3,
2167 IWL_PM_SLP_PHY = 4,
2168 IWL_PM_SLP_REPENT = 5,
2169 IWL_PM_WAKEUP_BY_TIMER = 6,
2170 IWL_PM_WAKEUP_BY_DRIVER = 7,
2171 IWL_PM_WAKEUP_BY_RFKILL = 8,
2172 /* 3 reserved */
2173 IWL_PM_NUM_OF_MODES = 12,
2174};
2175
2176/*
2177 * REPLY_CARD_STATE_CMD = 0xa0 (command, has simple generic response)
2178 */
2179#define CARD_STATE_CMD_DISABLE 0x00 /* Put card to sleep */
2180#define CARD_STATE_CMD_ENABLE 0x01 /* Wake up card */
2181#define CARD_STATE_CMD_HALT 0x02 /* Power down permanently */
2182struct iwl_card_state_cmd {
2183 __le32 status; /* CARD_STATE_CMD_* request new power state */
2184} __packed;
2185
2186/*
2187 * CARD_STATE_NOTIFICATION = 0xa1 (notification only, not a command)
2188 */
2189struct iwl_card_state_notif {
2190 __le32 flags;
2191} __packed;
2192
2193#define HW_CARD_DISABLED 0x01
2194#define SW_CARD_DISABLED 0x02
2195#define CT_CARD_DISABLED 0x04
2196#define RXON_CARD_DISABLED 0x10
2197
2198struct iwl_ct_kill_config {
2199 __le32 reserved;
2200 __le32 critical_temperature_M;
2201 __le32 critical_temperature_R;
2202} __packed;
2203
2204/* 1000, and 6x00 */
2205struct iwl_ct_kill_throttling_config {
2206 __le32 critical_temperature_exit;
2207 __le32 reserved;
2208 __le32 critical_temperature_enter;
2209} __packed;
2210
2211/******************************************************************************
2212 * (8)
2213 * Scan Commands, Responses, Notifications:
2214 *
2215 *****************************************************************************/
2216
2217#define SCAN_CHANNEL_TYPE_PASSIVE cpu_to_le32(0)
2218#define SCAN_CHANNEL_TYPE_ACTIVE cpu_to_le32(1)
2219
2220/**
2221 * struct iwl_scan_channel - entry in REPLY_SCAN_CMD channel table
2222 *
2223 * One for each channel in the scan list.
2224 * Each channel can independently select:
2225 * 1) SSID for directed active scans
2226 * 2) Txpower setting (for rate specified within Tx command)
2227 * 3) How long to stay on-channel (behavior may be modified by quiet_time,
2228 * quiet_plcp_th, good_CRC_th)
2229 *
2230 * To avoid uCode errors, make sure the following are true (see comments
2231 * under struct iwl_scan_cmd about max_out_time and quiet_time):
2232 * 1) If using passive_dwell (i.e. passive_dwell != 0):
2233 * active_dwell <= passive_dwell (< max_out_time if max_out_time != 0)
2234 * 2) quiet_time <= active_dwell
2235 * 3) If restricting off-channel time (i.e. max_out_time !=0):
2236 * passive_dwell < max_out_time
2237 * active_dwell < max_out_time
2238 */
2239
2240struct iwl_scan_channel {
2241 /*
2242 * type is defined as:
2243 * 0:0 1 = active, 0 = passive
2244 * 1:20 SSID direct bit map; if a bit is set, then corresponding
2245 * SSID IE is transmitted in probe request.
2246 * 21:31 reserved
2247 */
2248 __le32 type;
2249 __le16 channel; /* band is selected by iwl_scan_cmd "flags" field */
2250 u8 tx_gain; /* gain for analog radio */
2251 u8 dsp_atten; /* gain for DSP */
2252 __le16 active_dwell; /* in 1024-uSec TU (time units), typ 5-50 */
2253 __le16 passive_dwell; /* in 1024-uSec TU (time units), typ 20-500 */
2254} __packed;
2255
2256/* set number of direct probes __le32 type */
2257#define IWL_SCAN_PROBE_MASK(n) cpu_to_le32((BIT(n) | (BIT(n) - BIT(1))))
2258
2259/**
2260 * struct iwl_ssid_ie - directed scan network information element
2261 *
2262 * Up to 20 of these may appear in REPLY_SCAN_CMD,
2263 * selected by "type" bit field in struct iwl_scan_channel;
2264 * each channel may select different ssids from among the 20 entries.
2265 * SSID IEs get transmitted in reverse order of entry.
2266 */
2267struct iwl_ssid_ie {
2268 u8 id;
2269 u8 len;
2270 u8 ssid[32];
2271} __packed;
2272
2273#define PROBE_OPTION_MAX 20
2274#define TX_CMD_LIFE_TIME_INFINITE cpu_to_le32(0xFFFFFFFF)
2275#define IWL_GOOD_CRC_TH_DISABLED 0
2276#define IWL_GOOD_CRC_TH_DEFAULT cpu_to_le16(1)
2277#define IWL_GOOD_CRC_TH_NEVER cpu_to_le16(0xffff)
2278#define IWL_MAX_CMD_SIZE 4096
2279
2280/*
2281 * REPLY_SCAN_CMD = 0x80 (command)
2282 *
2283 * The hardware scan command is very powerful; the driver can set it up to
2284 * maintain (relatively) normal network traffic while doing a scan in the
2285 * background. The max_out_time and suspend_time control the ratio of how
2286 * long the device stays on an associated network channel ("service channel")
2287 * vs. how long it's away from the service channel, i.e. tuned to other channels
2288 * for scanning.
2289 *
2290 * max_out_time is the max time off-channel (in usec), and suspend_time
2291 * is how long (in "extended beacon" format) that the scan is "suspended"
2292 * after returning to the service channel. That is, suspend_time is the
2293 * time that we stay on the service channel, doing normal work, between
2294 * scan segments. The driver may set these parameters differently to support
2295 * scanning when associated vs. not associated, and light vs. heavy traffic
2296 * loads when associated.
2297 *
2298 * After receiving this command, the device's scan engine does the following;
2299 *
2300 * 1) Sends SCAN_START notification to driver
2301 * 2) Checks to see if it has time to do scan for one channel
2302 * 3) Sends NULL packet, with power-save (PS) bit set to 1,
2303 * to tell AP that we're going off-channel
2304 * 4) Tunes to first channel in scan list, does active or passive scan
2305 * 5) Sends SCAN_RESULT notification to driver
2306 * 6) Checks to see if it has time to do scan on *next* channel in list
2307 * 7) Repeats 4-6 until it no longer has time to scan the next channel
2308 * before max_out_time expires
2309 * 8) Returns to service channel
2310 * 9) Sends NULL packet with PS=0 to tell AP that we're back
2311 * 10) Stays on service channel until suspend_time expires
2312 * 11) Repeats entire process 2-10 until list is complete
2313 * 12) Sends SCAN_COMPLETE notification
2314 *
2315 * For fast, efficient scans, the scan command also has support for staying on
2316 * a channel for just a short time, if doing active scanning and getting no
2317 * responses to the transmitted probe request. This time is controlled by
2318 * quiet_time, and the number of received packets below which a channel is
2319 * considered "quiet" is controlled by quiet_plcp_threshold.
2320 *
2321 * For active scanning on channels that have regulatory restrictions against
2322 * blindly transmitting, the scan can listen before transmitting, to make sure
2323 * that there is already legitimate activity on the channel. If enough
2324 * packets are cleanly received on the channel (controlled by good_CRC_th,
2325 * typical value 1), the scan engine starts transmitting probe requests.
2326 *
2327 * Driver must use separate scan commands for 2.4 vs. 5 GHz bands.
2328 *
2329 * To avoid uCode errors, see timing restrictions described under
2330 * struct iwl_scan_channel.
2331 */
2332
2333enum iwl_scan_flags {
2334 /* BIT(0) currently unused */
2335 IWL_SCAN_FLAGS_ACTION_FRAME_TX = BIT(1),
2336 /* bits 2-7 reserved */
2337};
2338
2339struct iwl_scan_cmd {
2340 __le16 len;
2341 u8 scan_flags; /* scan flags: see enum iwl_scan_flags */
2342 u8 channel_count; /* # channels in channel list */
2343 __le16 quiet_time; /* dwell only this # millisecs on quiet channel
2344 * (only for active scan) */
2345 __le16 quiet_plcp_th; /* quiet chnl is < this # pkts (typ. 1) */
2346 __le16 good_CRC_th; /* passive -> active promotion threshold */
2347 __le16 rx_chain; /* RXON_RX_CHAIN_* */
2348 __le32 max_out_time; /* max usec to be away from associated (service)
2349 * channel */
2350 __le32 suspend_time; /* pause scan this long (in "extended beacon
2351 * format") when returning to service chnl:
2352 */
2353 __le32 flags; /* RXON_FLG_* */
2354 __le32 filter_flags; /* RXON_FILTER_* */
2355
2356 /* For active scans (set to all-0s for passive scans).
2357 * Does not include payload. Must specify Tx rate; no rate scaling. */
2358 struct iwl_tx_cmd tx_cmd;
2359
2360 /* For directed active scans (set to all-0s otherwise) */
2361 struct iwl_ssid_ie direct_scan[PROBE_OPTION_MAX];
2362
2363 /*
2364 * Probe request frame, followed by channel list.
2365 *
2366 * Size of probe request frame is specified by byte count in tx_cmd.
2367 * Channel list follows immediately after probe request frame.
2368 * Number of channels in list is specified by channel_count.
2369 * Each channel in list is of type:
2370 *
2371 * struct iwl_scan_channel channels[0];
2372 *
2373 * NOTE: Only one band of channels can be scanned per pass. You
2374 * must not mix 2.4GHz channels and 5.2GHz channels, and you must wait
2375 * for one scan to complete (i.e. receive SCAN_COMPLETE_NOTIFICATION)
2376 * before requesting another scan.
2377 */
2378 u8 data[0];
2379} __packed;
2380
2381/* Can abort will notify by complete notification with abort status. */
2382#define CAN_ABORT_STATUS cpu_to_le32(0x1)
2383/* complete notification statuses */
2384#define ABORT_STATUS 0x2
2385
2386/*
2387 * REPLY_SCAN_CMD = 0x80 (response)
2388 */
2389struct iwl_scanreq_notification {
2390 __le32 status; /* 1: okay, 2: cannot fulfill request */
2391} __packed;
2392
2393/*
2394 * SCAN_START_NOTIFICATION = 0x82 (notification only, not a command)
2395 */
2396struct iwl_scanstart_notification {
2397 __le32 tsf_low;
2398 __le32 tsf_high;
2399 __le32 beacon_timer;
2400 u8 channel;
2401 u8 band;
2402 u8 reserved[2];
2403 __le32 status;
2404} __packed;
2405
2406#define SCAN_OWNER_STATUS 0x1
2407#define MEASURE_OWNER_STATUS 0x2
2408
2409#define IWL_PROBE_STATUS_OK 0
2410#define IWL_PROBE_STATUS_TX_FAILED BIT(0)
2411/* error statuses combined with TX_FAILED */
2412#define IWL_PROBE_STATUS_FAIL_TTL BIT(1)
2413#define IWL_PROBE_STATUS_FAIL_BT BIT(2)
2414
2415#define NUMBER_OF_STATISTICS 1 /* first __le32 is good CRC */
2416/*
2417 * SCAN_RESULTS_NOTIFICATION = 0x83 (notification only, not a command)
2418 */
2419struct iwl_scanresults_notification {
2420 u8 channel;
2421 u8 band;
2422 u8 probe_status;
2423 u8 num_probe_not_sent; /* not enough time to send */
2424 __le32 tsf_low;
2425 __le32 tsf_high;
2426 __le32 statistics[NUMBER_OF_STATISTICS];
2427} __packed;
2428
2429/*
2430 * SCAN_COMPLETE_NOTIFICATION = 0x84 (notification only, not a command)
2431 */
2432struct iwl_scancomplete_notification {
2433 u8 scanned_channels;
2434 u8 status;
2435 u8 bt_status; /* BT On/Off status */
2436 u8 last_channel;
2437 __le32 tsf_low;
2438 __le32 tsf_high;
2439} __packed;
2440
2441
2442/******************************************************************************
2443 * (9)
2444 * IBSS/AP Commands and Notifications:
2445 *
2446 *****************************************************************************/
2447
2448enum iwl_ibss_manager {
2449 IWL_NOT_IBSS_MANAGER = 0,
2450 IWL_IBSS_MANAGER = 1,
2451};
2452
2453/*
2454 * BEACON_NOTIFICATION = 0x90 (notification only, not a command)
2455 */
2456
2457struct iwlagn_beacon_notif {
2458 struct iwlagn_tx_resp beacon_notify_hdr;
2459 __le32 low_tsf;
2460 __le32 high_tsf;
2461 __le32 ibss_mgr_status;
2462} __packed;
2463
2464/*
2465 * REPLY_TX_BEACON = 0x91 (command, has simple generic response)
2466 */
2467
2468struct iwl_tx_beacon_cmd {
2469 struct iwl_tx_cmd tx;
2470 __le16 tim_idx;
2471 u8 tim_size;
2472 u8 reserved1;
2473 struct ieee80211_hdr frame[0]; /* beacon frame */
2474} __packed;
2475
2476/******************************************************************************
2477 * (10)
2478 * Statistics Commands and Notifications:
2479 *
2480 *****************************************************************************/
2481
2482#define IWL_TEMP_CONVERT 260
2483
2484#define SUP_RATE_11A_MAX_NUM_CHANNELS 8
2485#define SUP_RATE_11B_MAX_NUM_CHANNELS 4
2486#define SUP_RATE_11G_MAX_NUM_CHANNELS 12
2487
2488/* Used for passing to driver number of successes and failures per rate */
2489struct rate_histogram {
2490 union {
2491 __le32 a[SUP_RATE_11A_MAX_NUM_CHANNELS];
2492 __le32 b[SUP_RATE_11B_MAX_NUM_CHANNELS];
2493 __le32 g[SUP_RATE_11G_MAX_NUM_CHANNELS];
2494 } success;
2495 union {
2496 __le32 a[SUP_RATE_11A_MAX_NUM_CHANNELS];
2497 __le32 b[SUP_RATE_11B_MAX_NUM_CHANNELS];
2498 __le32 g[SUP_RATE_11G_MAX_NUM_CHANNELS];
2499 } failed;
2500} __packed;
2501
2502/* statistics command response */
2503
2504struct statistics_dbg {
2505 __le32 burst_check;
2506 __le32 burst_count;
2507 __le32 wait_for_silence_timeout_cnt;
2508 __le32 reserved[3];
2509} __packed;
2510
2511struct statistics_rx_phy {
2512 __le32 ina_cnt;
2513 __le32 fina_cnt;
2514 __le32 plcp_err;
2515 __le32 crc32_err;
2516 __le32 overrun_err;
2517 __le32 early_overrun_err;
2518 __le32 crc32_good;
2519 __le32 false_alarm_cnt;
2520 __le32 fina_sync_err_cnt;
2521 __le32 sfd_timeout;
2522 __le32 fina_timeout;
2523 __le32 unresponded_rts;
2524 __le32 rxe_frame_limit_overrun;
2525 __le32 sent_ack_cnt;
2526 __le32 sent_cts_cnt;
2527 __le32 sent_ba_rsp_cnt;
2528 __le32 dsp_self_kill;
2529 __le32 mh_format_err;
2530 __le32 re_acq_main_rssi_sum;
2531 __le32 reserved3;
2532} __packed;
2533
2534struct statistics_rx_ht_phy {
2535 __le32 plcp_err;
2536 __le32 overrun_err;
2537 __le32 early_overrun_err;
2538 __le32 crc32_good;
2539 __le32 crc32_err;
2540 __le32 mh_format_err;
2541 __le32 agg_crc32_good;
2542 __le32 agg_mpdu_cnt;
2543 __le32 agg_cnt;
2544 __le32 unsupport_mcs;
2545} __packed;
2546
2547#define INTERFERENCE_DATA_AVAILABLE cpu_to_le32(1)
2548
2549struct statistics_rx_non_phy {
2550 __le32 bogus_cts; /* CTS received when not expecting CTS */
2551 __le32 bogus_ack; /* ACK received when not expecting ACK */
2552 __le32 non_bssid_frames; /* number of frames with BSSID that
2553 * doesn't belong to the STA BSSID */
2554 __le32 filtered_frames; /* count frames that were dumped in the
2555 * filtering process */
2556 __le32 non_channel_beacons; /* beacons with our bss id but not on
2557 * our serving channel */
2558 __le32 channel_beacons; /* beacons with our bss id and in our
2559 * serving channel */
2560 __le32 num_missed_bcon; /* number of missed beacons */
2561 __le32 adc_rx_saturation_time; /* count in 0.8us units the time the
2562 * ADC was in saturation */
2563 __le32 ina_detection_search_time;/* total time (in 0.8us) searched
2564 * for INA */
2565 __le32 beacon_silence_rssi_a; /* RSSI silence after beacon frame */
2566 __le32 beacon_silence_rssi_b; /* RSSI silence after beacon frame */
2567 __le32 beacon_silence_rssi_c; /* RSSI silence after beacon frame */
2568 __le32 interference_data_flag; /* flag for interference data
2569 * availability. 1 when data is
2570 * available. */
2571 __le32 channel_load; /* counts RX Enable time in uSec */
2572 __le32 dsp_false_alarms; /* DSP false alarm (both OFDM
2573 * and CCK) counter */
2574 __le32 beacon_rssi_a;
2575 __le32 beacon_rssi_b;
2576 __le32 beacon_rssi_c;
2577 __le32 beacon_energy_a;
2578 __le32 beacon_energy_b;
2579 __le32 beacon_energy_c;
2580} __packed;
2581
2582struct statistics_rx_non_phy_bt {
2583 struct statistics_rx_non_phy common;
2584 /* additional stats for bt */
2585 __le32 num_bt_kills;
2586 __le32 reserved[2];
2587} __packed;
2588
2589struct statistics_rx {
2590 struct statistics_rx_phy ofdm;
2591 struct statistics_rx_phy cck;
2592 struct statistics_rx_non_phy general;
2593 struct statistics_rx_ht_phy ofdm_ht;
2594} __packed;
2595
2596struct statistics_rx_bt {
2597 struct statistics_rx_phy ofdm;
2598 struct statistics_rx_phy cck;
2599 struct statistics_rx_non_phy_bt general;
2600 struct statistics_rx_ht_phy ofdm_ht;
2601} __packed;
2602
2603/**
2604 * struct statistics_tx_power - current tx power
2605 *
2606 * @ant_a: current tx power on chain a in 1/2 dB step
2607 * @ant_b: current tx power on chain b in 1/2 dB step
2608 * @ant_c: current tx power on chain c in 1/2 dB step
2609 */
2610struct statistics_tx_power {
2611 u8 ant_a;
2612 u8 ant_b;
2613 u8 ant_c;
2614 u8 reserved;
2615} __packed;
2616
2617struct statistics_tx_non_phy_agg {
2618 __le32 ba_timeout;
2619 __le32 ba_reschedule_frames;
2620 __le32 scd_query_agg_frame_cnt;
2621 __le32 scd_query_no_agg;
2622 __le32 scd_query_agg;
2623 __le32 scd_query_mismatch;
2624 __le32 frame_not_ready;
2625 __le32 underrun;
2626 __le32 bt_prio_kill;
2627 __le32 rx_ba_rsp_cnt;
2628} __packed;
2629
2630struct statistics_tx {
2631 __le32 preamble_cnt;
2632 __le32 rx_detected_cnt;
2633 __le32 bt_prio_defer_cnt;
2634 __le32 bt_prio_kill_cnt;
2635 __le32 few_bytes_cnt;
2636 __le32 cts_timeout;
2637 __le32 ack_timeout;
2638 __le32 expected_ack_cnt;
2639 __le32 actual_ack_cnt;
2640 __le32 dump_msdu_cnt;
2641 __le32 burst_abort_next_frame_mismatch_cnt;
2642 __le32 burst_abort_missing_next_frame_cnt;
2643 __le32 cts_timeout_collision;
2644 __le32 ack_or_ba_timeout_collision;
2645 struct statistics_tx_non_phy_agg agg;
2646 /*
2647 * "tx_power" are optional parameters provided by uCode,
2648 * 6000 series is the only device provide the information,
2649 * Those are reserved fields for all the other devices
2650 */
2651 struct statistics_tx_power tx_power;
2652 __le32 reserved1;
2653} __packed;
2654
2655
2656struct statistics_div {
2657 __le32 tx_on_a;
2658 __le32 tx_on_b;
2659 __le32 exec_time;
2660 __le32 probe_time;
2661 __le32 reserved1;
2662 __le32 reserved2;
2663} __packed;
2664
2665struct statistics_general_common {
2666 __le32 temperature; /* radio temperature */
2667 __le32 temperature_m; /* radio voltage */
2668 struct statistics_dbg dbg;
2669 __le32 sleep_time;
2670 __le32 slots_out;
2671 __le32 slots_idle;
2672 __le32 ttl_timestamp;
2673 struct statistics_div div;
2674 __le32 rx_enable_counter;
2675 /*
2676 * num_of_sos_states:
2677 * count the number of times we have to re-tune
2678 * in order to get out of bad PHY status
2679 */
2680 __le32 num_of_sos_states;
2681} __packed;
2682
2683struct statistics_bt_activity {
2684 /* Tx statistics */
2685 __le32 hi_priority_tx_req_cnt;
2686 __le32 hi_priority_tx_denied_cnt;
2687 __le32 lo_priority_tx_req_cnt;
2688 __le32 lo_priority_tx_denied_cnt;
2689 /* Rx statistics */
2690 __le32 hi_priority_rx_req_cnt;
2691 __le32 hi_priority_rx_denied_cnt;
2692 __le32 lo_priority_rx_req_cnt;
2693 __le32 lo_priority_rx_denied_cnt;
2694} __packed;
2695
2696struct statistics_general {
2697 struct statistics_general_common common;
2698 __le32 reserved2;
2699 __le32 reserved3;
2700} __packed;
2701
2702struct statistics_general_bt {
2703 struct statistics_general_common common;
2704 struct statistics_bt_activity activity;
2705 __le32 reserved2;
2706 __le32 reserved3;
2707} __packed;
2708
2709#define UCODE_STATISTICS_CLEAR_MSK (0x1 << 0)
2710#define UCODE_STATISTICS_FREQUENCY_MSK (0x1 << 1)
2711#define UCODE_STATISTICS_NARROW_BAND_MSK (0x1 << 2)
2712
2713/*
2714 * REPLY_STATISTICS_CMD = 0x9c,
2715 * all devices identical.
2716 *
2717 * This command triggers an immediate response containing uCode statistics.
2718 * The response is in the same format as STATISTICS_NOTIFICATION 0x9d, below.
2719 *
2720 * If the CLEAR_STATS configuration flag is set, uCode will clear its
2721 * internal copy of the statistics (counters) after issuing the response.
2722 * This flag does not affect STATISTICS_NOTIFICATIONs after beacons (see below).
2723 *
2724 * If the DISABLE_NOTIF configuration flag is set, uCode will not issue
2725 * STATISTICS_NOTIFICATIONs after received beacons (see below). This flag
2726 * does not affect the response to the REPLY_STATISTICS_CMD 0x9c itself.
2727 */
2728#define IWL_STATS_CONF_CLEAR_STATS cpu_to_le32(0x1) /* see above */
2729#define IWL_STATS_CONF_DISABLE_NOTIF cpu_to_le32(0x2)/* see above */
2730struct iwl_statistics_cmd {
2731 __le32 configuration_flags; /* IWL_STATS_CONF_* */
2732} __packed;
2733
2734/*
2735 * STATISTICS_NOTIFICATION = 0x9d (notification only, not a command)
2736 *
2737 * By default, uCode issues this notification after receiving a beacon
2738 * while associated. To disable this behavior, set DISABLE_NOTIF flag in the
2739 * REPLY_STATISTICS_CMD 0x9c, above.
2740 *
2741 * Statistics counters continue to increment beacon after beacon, but are
2742 * cleared when changing channels or when driver issues REPLY_STATISTICS_CMD
2743 * 0x9c with CLEAR_STATS bit set (see above).
2744 *
2745 * uCode also issues this notification during scans. uCode clears statistics
2746 * appropriately so that each notification contains statistics for only the
2747 * one channel that has just been scanned.
2748 */
2749#define STATISTICS_REPLY_FLG_BAND_24G_MSK cpu_to_le32(0x2)
2750#define STATISTICS_REPLY_FLG_HT40_MODE_MSK cpu_to_le32(0x8)
2751
2752struct iwl_notif_statistics {
2753 __le32 flag;
2754 struct statistics_rx rx;
2755 struct statistics_tx tx;
2756 struct statistics_general general;
2757} __packed;
2758
2759struct iwl_bt_notif_statistics {
2760 __le32 flag;
2761 struct statistics_rx_bt rx;
2762 struct statistics_tx tx;
2763 struct statistics_general_bt general;
2764} __packed;
2765
2766/*
2767 * MISSED_BEACONS_NOTIFICATION = 0xa2 (notification only, not a command)
2768 *
2769 * uCode send MISSED_BEACONS_NOTIFICATION to driver when detect beacon missed
2770 * in regardless of how many missed beacons, which mean when driver receive the
2771 * notification, inside the command, it can find all the beacons information
2772 * which include number of total missed beacons, number of consecutive missed
2773 * beacons, number of beacons received and number of beacons expected to
2774 * receive.
2775 *
2776 * If uCode detected consecutive_missed_beacons > 5, it will reset the radio
2777 * in order to bring the radio/PHY back to working state; which has no relation
2778 * to when driver will perform sensitivity calibration.
2779 *
2780 * Driver should set it own missed_beacon_threshold to decide when to perform
2781 * sensitivity calibration based on number of consecutive missed beacons in
2782 * order to improve overall performance, especially in noisy environment.
2783 *
2784 */
2785
2786#define IWL_MISSED_BEACON_THRESHOLD_MIN (1)
2787#define IWL_MISSED_BEACON_THRESHOLD_DEF (5)
2788#define IWL_MISSED_BEACON_THRESHOLD_MAX IWL_MISSED_BEACON_THRESHOLD_DEF
2789
2790struct iwl_missed_beacon_notif {
2791 __le32 consecutive_missed_beacons;
2792 __le32 total_missed_becons;
2793 __le32 num_expected_beacons;
2794 __le32 num_recvd_beacons;
2795} __packed;
2796
2797
2798/******************************************************************************
2799 * (11)
2800 * Rx Calibration Commands:
2801 *
2802 * With the uCode used for open source drivers, most Tx calibration (except
2803 * for Tx Power) and most Rx calibration is done by uCode during the
2804 * "initialize" phase of uCode boot. Driver must calibrate only:
2805 *
2806 * 1) Tx power (depends on temperature), described elsewhere
2807 * 2) Receiver gain balance (optimize MIMO, and detect disconnected antennas)
2808 * 3) Receiver sensitivity (to optimize signal detection)
2809 *
2810 *****************************************************************************/
2811
2812/**
2813 * SENSITIVITY_CMD = 0xa8 (command, has simple generic response)
2814 *
2815 * This command sets up the Rx signal detector for a sensitivity level that
2816 * is high enough to lock onto all signals within the associated network,
2817 * but low enough to ignore signals that are below a certain threshold, so as
2818 * not to have too many "false alarms". False alarms are signals that the
2819 * Rx DSP tries to lock onto, but then discards after determining that they
2820 * are noise.
2821 *
2822 * The optimum number of false alarms is between 5 and 50 per 200 TUs
2823 * (200 * 1024 uSecs, i.e. 204.8 milliseconds) of actual Rx time (i.e.
2824 * time listening, not transmitting). Driver must adjust sensitivity so that
2825 * the ratio of actual false alarms to actual Rx time falls within this range.
2826 *
2827 * While associated, uCode delivers STATISTICS_NOTIFICATIONs after each
2828 * received beacon. These provide information to the driver to analyze the
2829 * sensitivity. Don't analyze statistics that come in from scanning, or any
2830 * other non-associated-network source. Pertinent statistics include:
2831 *
2832 * From "general" statistics (struct statistics_rx_non_phy):
2833 *
2834 * (beacon_energy_[abc] & 0x0FF00) >> 8 (unsigned, higher value is lower level)
2835 * Measure of energy of desired signal. Used for establishing a level
2836 * below which the device does not detect signals.
2837 *
2838 * (beacon_silence_rssi_[abc] & 0x0FF00) >> 8 (unsigned, units in dB)
2839 * Measure of background noise in silent period after beacon.
2840 *
2841 * channel_load
2842 * uSecs of actual Rx time during beacon period (varies according to
2843 * how much time was spent transmitting).
2844 *
2845 * From "cck" and "ofdm" statistics (struct statistics_rx_phy), separately:
2846 *
2847 * false_alarm_cnt
2848 * Signal locks abandoned early (before phy-level header).
2849 *
2850 * plcp_err
2851 * Signal locks abandoned late (during phy-level header).
2852 *
2853 * NOTE: Both false_alarm_cnt and plcp_err increment monotonically from
2854 * beacon to beacon, i.e. each value is an accumulation of all errors
2855 * before and including the latest beacon. Values will wrap around to 0
2856 * after counting up to 2^32 - 1. Driver must differentiate vs.
2857 * previous beacon's values to determine # false alarms in the current
2858 * beacon period.
2859 *
2860 * Total number of false alarms = false_alarms + plcp_errs
2861 *
2862 * For OFDM, adjust the following table entries in struct iwl_sensitivity_cmd
2863 * (notice that the start points for OFDM are at or close to settings for
2864 * maximum sensitivity):
2865 *
2866 * START / MIN / MAX
2867 * HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX 90 / 85 / 120
2868 * HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX 170 / 170 / 210
2869 * HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX 105 / 105 / 140
2870 * HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX 220 / 220 / 270
2871 *
2872 * If actual rate of OFDM false alarms (+ plcp_errors) is too high
2873 * (greater than 50 for each 204.8 msecs listening), reduce sensitivity
2874 * by *adding* 1 to all 4 of the table entries above, up to the max for
2875 * each entry. Conversely, if false alarm rate is too low (less than 5
2876 * for each 204.8 msecs listening), *subtract* 1 from each entry to
2877 * increase sensitivity.
2878 *
2879 * For CCK sensitivity, keep track of the following:
2880 *
2881 * 1). 20-beacon history of maximum background noise, indicated by
2882 * (beacon_silence_rssi_[abc] & 0x0FF00), units in dB, across the
2883 * 3 receivers. For any given beacon, the "silence reference" is
2884 * the maximum of last 60 samples (20 beacons * 3 receivers).
2885 *
2886 * 2). 10-beacon history of strongest signal level, as indicated
2887 * by (beacon_energy_[abc] & 0x0FF00) >> 8, across the 3 receivers,
2888 * i.e. the strength of the signal through the best receiver at the
2889 * moment. These measurements are "upside down", with lower values
2890 * for stronger signals, so max energy will be *minimum* value.
2891 *
2892 * Then for any given beacon, the driver must determine the *weakest*
2893 * of the strongest signals; this is the minimum level that needs to be
2894 * successfully detected, when using the best receiver at the moment.
2895 * "Max cck energy" is the maximum (higher value means lower energy!)
2896 * of the last 10 minima. Once this is determined, driver must add
2897 * a little margin by adding "6" to it.
2898 *
2899 * 3). Number of consecutive beacon periods with too few false alarms.
2900 * Reset this to 0 at the first beacon period that falls within the
2901 * "good" range (5 to 50 false alarms per 204.8 milliseconds rx).
2902 *
2903 * Then, adjust the following CCK table entries in struct iwl_sensitivity_cmd
2904 * (notice that the start points for CCK are at maximum sensitivity):
2905 *
2906 * START / MIN / MAX
2907 * HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX 125 / 125 / 200
2908 * HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX 200 / 200 / 400
2909 * HD_MIN_ENERGY_CCK_DET_INDEX 100 / 0 / 100
2910 *
2911 * If actual rate of CCK false alarms (+ plcp_errors) is too high
2912 * (greater than 50 for each 204.8 msecs listening), method for reducing
2913 * sensitivity is:
2914 *
2915 * 1) *Add* 3 to value in HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX,
2916 * up to max 400.
2917 *
2918 * 2) If current value in HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX is < 160,
2919 * sensitivity has been reduced a significant amount; bring it up to
2920 * a moderate 161. Otherwise, *add* 3, up to max 200.
2921 *
2922 * 3) a) If current value in HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX is > 160,
2923 * sensitivity has been reduced only a moderate or small amount;
2924 * *subtract* 2 from value in HD_MIN_ENERGY_CCK_DET_INDEX,
2925 * down to min 0. Otherwise (if gain has been significantly reduced),
2926 * don't change the HD_MIN_ENERGY_CCK_DET_INDEX value.
2927 *
2928 * b) Save a snapshot of the "silence reference".
2929 *
2930 * If actual rate of CCK false alarms (+ plcp_errors) is too low
2931 * (less than 5 for each 204.8 msecs listening), method for increasing
2932 * sensitivity is used only if:
2933 *
2934 * 1a) Previous beacon did not have too many false alarms
2935 * 1b) AND difference between previous "silence reference" and current
2936 * "silence reference" (prev - current) is 2 or more,
2937 * OR 2) 100 or more consecutive beacon periods have had rate of
2938 * less than 5 false alarms per 204.8 milliseconds rx time.
2939 *
2940 * Method for increasing sensitivity:
2941 *
2942 * 1) *Subtract* 3 from value in HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX,
2943 * down to min 125.
2944 *
2945 * 2) *Subtract* 3 from value in HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX,
2946 * down to min 200.
2947 *
2948 * 3) *Add* 2 to value in HD_MIN_ENERGY_CCK_DET_INDEX, up to max 100.
2949 *
2950 * If actual rate of CCK false alarms (+ plcp_errors) is within good range
2951 * (between 5 and 50 for each 204.8 msecs listening):
2952 *
2953 * 1) Save a snapshot of the silence reference.
2954 *
2955 * 2) If previous beacon had too many CCK false alarms (+ plcp_errors),
2956 * give some extra margin to energy threshold by *subtracting* 8
2957 * from value in HD_MIN_ENERGY_CCK_DET_INDEX.
2958 *
2959 * For all cases (too few, too many, good range), make sure that the CCK
2960 * detection threshold (energy) is below the energy level for robust
2961 * detection over the past 10 beacon periods, the "Max cck energy".
2962 * Lower values mean higher energy; this means making sure that the value
2963 * in HD_MIN_ENERGY_CCK_DET_INDEX is at or *above* "Max cck energy".
2964 *
2965 */
2966
2967/*
2968 * Table entries in SENSITIVITY_CMD (struct iwl_sensitivity_cmd)
2969 */
2970#define HD_TABLE_SIZE (11) /* number of entries */
2971#define HD_MIN_ENERGY_CCK_DET_INDEX (0) /* table indexes */
2972#define HD_MIN_ENERGY_OFDM_DET_INDEX (1)
2973#define HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX (2)
2974#define HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX (3)
2975#define HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX (4)
2976#define HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX (5)
2977#define HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX (6)
2978#define HD_BARKER_CORR_TH_ADD_MIN_INDEX (7)
2979#define HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX (8)
2980#define HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX (9)
2981#define HD_OFDM_ENERGY_TH_IN_INDEX (10)
2982
2983/*
2984 * Additional table entries in enhance SENSITIVITY_CMD
2985 */
2986#define HD_INA_NON_SQUARE_DET_OFDM_INDEX (11)
2987#define HD_INA_NON_SQUARE_DET_CCK_INDEX (12)
2988#define HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX (13)
2989#define HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX (14)
2990#define HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX (15)
2991#define HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX (16)
2992#define HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX (17)
2993#define HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX (18)
2994#define HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX (19)
2995#define HD_CCK_NON_SQUARE_DET_SLOPE_INDEX (20)
2996#define HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX (21)
2997#define HD_RESERVED (22)
2998
2999/* number of entries for enhanced tbl */
3000#define ENHANCE_HD_TABLE_SIZE (23)
3001
3002/* number of additional entries for enhanced tbl */
3003#define ENHANCE_HD_TABLE_ENTRIES (ENHANCE_HD_TABLE_SIZE - HD_TABLE_SIZE)
3004
3005#define HD_INA_NON_SQUARE_DET_OFDM_DATA_V1 cpu_to_le16(0)
3006#define HD_INA_NON_SQUARE_DET_CCK_DATA_V1 cpu_to_le16(0)
3007#define HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V1 cpu_to_le16(0)
3008#define HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V1 cpu_to_le16(668)
3009#define HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1 cpu_to_le16(4)
3010#define HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V1 cpu_to_le16(486)
3011#define HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V1 cpu_to_le16(37)
3012#define HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V1 cpu_to_le16(853)
3013#define HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V1 cpu_to_le16(4)
3014#define HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V1 cpu_to_le16(476)
3015#define HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V1 cpu_to_le16(99)
3016
3017#define HD_INA_NON_SQUARE_DET_OFDM_DATA_V2 cpu_to_le16(1)
3018#define HD_INA_NON_SQUARE_DET_CCK_DATA_V2 cpu_to_le16(1)
3019#define HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA_V2 cpu_to_le16(1)
3020#define HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA_V2 cpu_to_le16(600)
3021#define HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2 cpu_to_le16(40)
3022#define HD_OFDM_NON_SQUARE_DET_SLOPE_DATA_V2 cpu_to_le16(486)
3023#define HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA_V2 cpu_to_le16(45)
3024#define HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA_V2 cpu_to_le16(853)
3025#define HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA_V2 cpu_to_le16(60)
3026#define HD_CCK_NON_SQUARE_DET_SLOPE_DATA_V2 cpu_to_le16(476)
3027#define HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA_V2 cpu_to_le16(99)
3028
3029
3030/* Control field in struct iwl_sensitivity_cmd */
3031#define SENSITIVITY_CMD_CONTROL_DEFAULT_TABLE cpu_to_le16(0)
3032#define SENSITIVITY_CMD_CONTROL_WORK_TABLE cpu_to_le16(1)
3033
3034/**
3035 * struct iwl_sensitivity_cmd
3036 * @control: (1) updates working table, (0) updates default table
3037 * @table: energy threshold values, use HD_* as index into table
3038 *
3039 * Always use "1" in "control" to update uCode's working table and DSP.
3040 */
3041struct iwl_sensitivity_cmd {
3042 __le16 control; /* always use "1" */
3043 __le16 table[HD_TABLE_SIZE]; /* use HD_* as index */
3044} __packed;
3045
3046/*
3047 *
3048 */
3049struct iwl_enhance_sensitivity_cmd {
3050 __le16 control; /* always use "1" */
3051 __le16 enhance_table[ENHANCE_HD_TABLE_SIZE]; /* use HD_* as index */
3052} __packed;
3053
3054
3055/**
3056 * REPLY_PHY_CALIBRATION_CMD = 0xb0 (command, has simple generic response)
3057 *
3058 * This command sets the relative gains of agn device's 3 radio receiver chains.
3059 *
3060 * After the first association, driver should accumulate signal and noise
3061 * statistics from the STATISTICS_NOTIFICATIONs that follow the first 20
3062 * beacons from the associated network (don't collect statistics that come
3063 * in from scanning, or any other non-network source).
3064 *
3065 * DISCONNECTED ANTENNA:
3066 *
3067 * Driver should determine which antennas are actually connected, by comparing
3068 * average beacon signal levels for the 3 Rx chains. Accumulate (add) the
3069 * following values over 20 beacons, one accumulator for each of the chains
3070 * a/b/c, from struct statistics_rx_non_phy:
3071 *
3072 * beacon_rssi_[abc] & 0x0FF (unsigned, units in dB)
3073 *
3074 * Find the strongest signal from among a/b/c. Compare the other two to the
3075 * strongest. If any signal is more than 15 dB (times 20, unless you
3076 * divide the accumulated values by 20) below the strongest, the driver
3077 * considers that antenna to be disconnected, and should not try to use that
3078 * antenna/chain for Rx or Tx. If both A and B seem to be disconnected,
3079 * driver should declare the stronger one as connected, and attempt to use it
3080 * (A and B are the only 2 Tx chains!).
3081 *
3082 *
3083 * RX BALANCE:
3084 *
3085 * Driver should balance the 3 receivers (but just the ones that are connected
3086 * to antennas, see above) for gain, by comparing the average signal levels
3087 * detected during the silence after each beacon (background noise).
3088 * Accumulate (add) the following values over 20 beacons, one accumulator for
3089 * each of the chains a/b/c, from struct statistics_rx_non_phy:
3090 *
3091 * beacon_silence_rssi_[abc] & 0x0FF (unsigned, units in dB)
3092 *
3093 * Find the weakest background noise level from among a/b/c. This Rx chain
3094 * will be the reference, with 0 gain adjustment. Attenuate other channels by
3095 * finding noise difference:
3096 *
3097 * (accum_noise[i] - accum_noise[reference]) / 30
3098 *
3099 * The "30" adjusts the dB in the 20 accumulated samples to units of 1.5 dB.
3100 * For use in diff_gain_[abc] fields of struct iwl_calibration_cmd, the
3101 * driver should limit the difference results to a range of 0-3 (0-4.5 dB),
3102 * and set bit 2 to indicate "reduce gain". The value for the reference
3103 * (weakest) chain should be "0".
3104 *
3105 * diff_gain_[abc] bit fields:
3106 * 2: (1) reduce gain, (0) increase gain
3107 * 1-0: amount of gain, units of 1.5 dB
3108 */
3109
3110/* Phy calibration command for series */
3111enum {
3112 IWL_PHY_CALIBRATE_DC_CMD = 8,
3113 IWL_PHY_CALIBRATE_LO_CMD = 9,
3114 IWL_PHY_CALIBRATE_TX_IQ_CMD = 11,
3115 IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD = 15,
3116 IWL_PHY_CALIBRATE_BASE_BAND_CMD = 16,
3117 IWL_PHY_CALIBRATE_TX_IQ_PERD_CMD = 17,
3118 IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD = 18,
3119};
3120
3121/* This enum defines the bitmap of various calibrations to enable in both
3122 * init ucode and runtime ucode through CALIBRATION_CFG_CMD.
3123 */
3124enum iwl_ucode_calib_cfg {
3125 IWL_CALIB_CFG_RX_BB_IDX = BIT(0),
3126 IWL_CALIB_CFG_DC_IDX = BIT(1),
3127 IWL_CALIB_CFG_LO_IDX = BIT(2),
3128 IWL_CALIB_CFG_TX_IQ_IDX = BIT(3),
3129 IWL_CALIB_CFG_RX_IQ_IDX = BIT(4),
3130 IWL_CALIB_CFG_NOISE_IDX = BIT(5),
3131 IWL_CALIB_CFG_CRYSTAL_IDX = BIT(6),
3132 IWL_CALIB_CFG_TEMPERATURE_IDX = BIT(7),
3133 IWL_CALIB_CFG_PAPD_IDX = BIT(8),
3134 IWL_CALIB_CFG_SENSITIVITY_IDX = BIT(9),
3135 IWL_CALIB_CFG_TX_PWR_IDX = BIT(10),
3136};
3137
3138#define IWL_CALIB_INIT_CFG_ALL cpu_to_le32(IWL_CALIB_CFG_RX_BB_IDX | \
3139 IWL_CALIB_CFG_DC_IDX | \
3140 IWL_CALIB_CFG_LO_IDX | \
3141 IWL_CALIB_CFG_TX_IQ_IDX | \
3142 IWL_CALIB_CFG_RX_IQ_IDX | \
3143 IWL_CALIB_CFG_CRYSTAL_IDX)
3144
3145#define IWL_CALIB_RT_CFG_ALL cpu_to_le32(IWL_CALIB_CFG_RX_BB_IDX | \
3146 IWL_CALIB_CFG_DC_IDX | \
3147 IWL_CALIB_CFG_LO_IDX | \
3148 IWL_CALIB_CFG_TX_IQ_IDX | \
3149 IWL_CALIB_CFG_RX_IQ_IDX | \
3150 IWL_CALIB_CFG_TEMPERATURE_IDX | \
3151 IWL_CALIB_CFG_PAPD_IDX | \
3152 IWL_CALIB_CFG_TX_PWR_IDX | \
3153 IWL_CALIB_CFG_CRYSTAL_IDX)
3154
3155#define IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK cpu_to_le32(BIT(0))
3156
3157struct iwl_calib_cfg_elmnt_s {
3158 __le32 is_enable;
3159 __le32 start;
3160 __le32 send_res;
3161 __le32 apply_res;
3162 __le32 reserved;
3163} __packed;
3164
3165struct iwl_calib_cfg_status_s {
3166 struct iwl_calib_cfg_elmnt_s once;
3167 struct iwl_calib_cfg_elmnt_s perd;
3168 __le32 flags;
3169} __packed;
3170
3171struct iwl_calib_cfg_cmd {
3172 struct iwl_calib_cfg_status_s ucd_calib_cfg;
3173 struct iwl_calib_cfg_status_s drv_calib_cfg;
3174 __le32 reserved1;
3175} __packed;
3176
3177struct iwl_calib_hdr {
3178 u8 op_code;
3179 u8 first_group;
3180 u8 groups_num;
3181 u8 data_valid;
3182} __packed;
3183
3184struct iwl_calib_cmd {
3185 struct iwl_calib_hdr hdr;
3186 u8 data[0];
3187} __packed;
3188
3189struct iwl_calib_xtal_freq_cmd {
3190 struct iwl_calib_hdr hdr;
3191 u8 cap_pin1;
3192 u8 cap_pin2;
3193 u8 pad[2];
3194} __packed;
3195
3196#define DEFAULT_RADIO_SENSOR_OFFSET cpu_to_le16(2700)
3197struct iwl_calib_temperature_offset_cmd {
3198 struct iwl_calib_hdr hdr;
3199 __le16 radio_sensor_offset;
3200 __le16 reserved;
3201} __packed;
3202
3203struct iwl_calib_temperature_offset_v2_cmd {
3204 struct iwl_calib_hdr hdr;
3205 __le16 radio_sensor_offset_high;
3206 __le16 radio_sensor_offset_low;
3207 __le16 burntVoltageRef;
3208 __le16 reserved;
3209} __packed;
3210
3211/* IWL_PHY_CALIBRATE_CHAIN_NOISE_RESET_CMD */
3212struct iwl_calib_chain_noise_reset_cmd {
3213 struct iwl_calib_hdr hdr;
3214 u8 data[0];
3215};
3216
3217/* IWL_PHY_CALIBRATE_CHAIN_NOISE_GAIN_CMD */
3218struct iwl_calib_chain_noise_gain_cmd {
3219 struct iwl_calib_hdr hdr;
3220 u8 delta_gain_1;
3221 u8 delta_gain_2;
3222 u8 pad[2];
3223} __packed;
3224
3225/******************************************************************************
3226 * (12)
3227 * Miscellaneous Commands:
3228 *
3229 *****************************************************************************/
3230
3231/*
3232 * LEDs Command & Response
3233 * REPLY_LEDS_CMD = 0x48 (command, has simple generic response)
3234 *
3235 * For each of 3 possible LEDs (Activity/Link/Tech, selected by "id" field),
3236 * this command turns it on or off, or sets up a periodic blinking cycle.
3237 */
3238struct iwl_led_cmd {
3239 __le32 interval; /* "interval" in uSec */
3240 u8 id; /* 1: Activity, 2: Link, 3: Tech */
3241 u8 off; /* # intervals off while blinking;
3242 * "0", with >0 "on" value, turns LED on */
3243 u8 on; /* # intervals on while blinking;
3244 * "0", regardless of "off", turns LED off */
3245 u8 reserved;
3246} __packed;
3247
3248/*
3249 * station priority table entries
3250 * also used as potential "events" value for both
3251 * COEX_MEDIUM_NOTIFICATION and COEX_EVENT_CMD
3252 */
3253
3254/*
3255 * COEX events entry flag masks
3256 * RP - Requested Priority
3257 * WP - Win Medium Priority: priority assigned when the contention has been won
3258 */
3259#define COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG (0x1)
3260#define COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG (0x2)
3261#define COEX_EVT_FLAG_DELAY_MEDIUM_FREE_NTFY_FLG (0x4)
3262
3263#define COEX_CU_UNASSOC_IDLE_RP 4
3264#define COEX_CU_UNASSOC_MANUAL_SCAN_RP 4
3265#define COEX_CU_UNASSOC_AUTO_SCAN_RP 4
3266#define COEX_CU_CALIBRATION_RP 4
3267#define COEX_CU_PERIODIC_CALIBRATION_RP 4
3268#define COEX_CU_CONNECTION_ESTAB_RP 4
3269#define COEX_CU_ASSOCIATED_IDLE_RP 4
3270#define COEX_CU_ASSOC_MANUAL_SCAN_RP 4
3271#define COEX_CU_ASSOC_AUTO_SCAN_RP 4
3272#define COEX_CU_ASSOC_ACTIVE_LEVEL_RP 4
3273#define COEX_CU_RF_ON_RP 6
3274#define COEX_CU_RF_OFF_RP 4
3275#define COEX_CU_STAND_ALONE_DEBUG_RP 6
3276#define COEX_CU_IPAN_ASSOC_LEVEL_RP 4
3277#define COEX_CU_RSRVD1_RP 4
3278#define COEX_CU_RSRVD2_RP 4
3279
3280#define COEX_CU_UNASSOC_IDLE_WP 3
3281#define COEX_CU_UNASSOC_MANUAL_SCAN_WP 3
3282#define COEX_CU_UNASSOC_AUTO_SCAN_WP 3
3283#define COEX_CU_CALIBRATION_WP 3
3284#define COEX_CU_PERIODIC_CALIBRATION_WP 3
3285#define COEX_CU_CONNECTION_ESTAB_WP 3
3286#define COEX_CU_ASSOCIATED_IDLE_WP 3
3287#define COEX_CU_ASSOC_MANUAL_SCAN_WP 3
3288#define COEX_CU_ASSOC_AUTO_SCAN_WP 3
3289#define COEX_CU_ASSOC_ACTIVE_LEVEL_WP 3
3290#define COEX_CU_RF_ON_WP 3
3291#define COEX_CU_RF_OFF_WP 3
3292#define COEX_CU_STAND_ALONE_DEBUG_WP 6
3293#define COEX_CU_IPAN_ASSOC_LEVEL_WP 3
3294#define COEX_CU_RSRVD1_WP 3
3295#define COEX_CU_RSRVD2_WP 3
3296
3297#define COEX_UNASSOC_IDLE_FLAGS 0
3298#define COEX_UNASSOC_MANUAL_SCAN_FLAGS \
3299 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3300 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG)
3301#define COEX_UNASSOC_AUTO_SCAN_FLAGS \
3302 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3303 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG)
3304#define COEX_CALIBRATION_FLAGS \
3305 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3306 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG)
3307#define COEX_PERIODIC_CALIBRATION_FLAGS 0
3308/*
3309 * COEX_CONNECTION_ESTAB:
3310 * we need DELAY_MEDIUM_FREE_NTFY to let WiMAX disconnect from network.
3311 */
3312#define COEX_CONNECTION_ESTAB_FLAGS \
3313 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3314 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG | \
3315 COEX_EVT_FLAG_DELAY_MEDIUM_FREE_NTFY_FLG)
3316#define COEX_ASSOCIATED_IDLE_FLAGS 0
3317#define COEX_ASSOC_MANUAL_SCAN_FLAGS \
3318 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3319 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG)
3320#define COEX_ASSOC_AUTO_SCAN_FLAGS \
3321 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3322 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG)
3323#define COEX_ASSOC_ACTIVE_LEVEL_FLAGS 0
3324#define COEX_RF_ON_FLAGS 0
3325#define COEX_RF_OFF_FLAGS 0
3326#define COEX_STAND_ALONE_DEBUG_FLAGS \
3327 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3328 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG)
3329#define COEX_IPAN_ASSOC_LEVEL_FLAGS \
3330 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3331 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG | \
3332 COEX_EVT_FLAG_DELAY_MEDIUM_FREE_NTFY_FLG)
3333#define COEX_RSRVD1_FLAGS 0
3334#define COEX_RSRVD2_FLAGS 0
3335/*
3336 * COEX_CU_RF_ON is the event wrapping all radio ownership.
3337 * We need DELAY_MEDIUM_FREE_NTFY to let WiMAX disconnect from network.
3338 */
3339#define COEX_CU_RF_ON_FLAGS \
3340 (COEX_EVT_FLAG_MEDIUM_FREE_NTFY_FLG | \
3341 COEX_EVT_FLAG_MEDIUM_ACTV_NTFY_FLG | \
3342 COEX_EVT_FLAG_DELAY_MEDIUM_FREE_NTFY_FLG)
3343
3344
3345enum {
3346 /* un-association part */
3347 COEX_UNASSOC_IDLE = 0,
3348 COEX_UNASSOC_MANUAL_SCAN = 1,
3349 COEX_UNASSOC_AUTO_SCAN = 2,
3350 /* calibration */
3351 COEX_CALIBRATION = 3,
3352 COEX_PERIODIC_CALIBRATION = 4,
3353 /* connection */
3354 COEX_CONNECTION_ESTAB = 5,
3355 /* association part */
3356 COEX_ASSOCIATED_IDLE = 6,
3357 COEX_ASSOC_MANUAL_SCAN = 7,
3358 COEX_ASSOC_AUTO_SCAN = 8,
3359 COEX_ASSOC_ACTIVE_LEVEL = 9,
3360 /* RF ON/OFF */
3361 COEX_RF_ON = 10,
3362 COEX_RF_OFF = 11,
3363 COEX_STAND_ALONE_DEBUG = 12,
3364 /* IPAN */
3365 COEX_IPAN_ASSOC_LEVEL = 13,
3366 /* reserved */
3367 COEX_RSRVD1 = 14,
3368 COEX_RSRVD2 = 15,
3369 COEX_NUM_OF_EVENTS = 16
3370};
3371
3372/*
3373 * Coexistence WIFI/WIMAX Command
3374 * COEX_PRIORITY_TABLE_CMD = 0x5a
3375 *
3376 */
3377struct iwl_wimax_coex_event_entry {
3378 u8 request_prio;
3379 u8 win_medium_prio;
3380 u8 reserved;
3381 u8 flags;
3382} __packed;
3383
3384/* COEX flag masks */
3385
3386/* Station table is valid */
3387#define COEX_FLAGS_STA_TABLE_VALID_MSK (0x1)
3388/* UnMask wake up src at unassociated sleep */
3389#define COEX_FLAGS_UNASSOC_WA_UNMASK_MSK (0x4)
3390/* UnMask wake up src at associated sleep */
3391#define COEX_FLAGS_ASSOC_WA_UNMASK_MSK (0x8)
3392/* Enable CoEx feature. */
3393#define COEX_FLAGS_COEX_ENABLE_MSK (0x80)
3394
3395struct iwl_wimax_coex_cmd {
3396 u8 flags;
3397 u8 reserved[3];
3398 struct iwl_wimax_coex_event_entry sta_prio[COEX_NUM_OF_EVENTS];
3399} __packed;
3400
3401/*
3402 * Coexistence MEDIUM NOTIFICATION
3403 * COEX_MEDIUM_NOTIFICATION = 0x5b
3404 *
3405 * notification from uCode to host to indicate medium changes
3406 *
3407 */
3408/*
3409 * status field
3410 * bit 0 - 2: medium status
3411 * bit 3: medium change indication
3412 * bit 4 - 31: reserved
3413 */
3414/* status option values, (0 - 2 bits) */
3415#define COEX_MEDIUM_BUSY (0x0) /* radio belongs to WiMAX */
3416#define COEX_MEDIUM_ACTIVE (0x1) /* radio belongs to WiFi */
3417#define COEX_MEDIUM_PRE_RELEASE (0x2) /* received radio release */
3418#define COEX_MEDIUM_MSK (0x7)
3419
3420/* send notification status (1 bit) */
3421#define COEX_MEDIUM_CHANGED (0x8)
3422#define COEX_MEDIUM_CHANGED_MSK (0x8)
3423#define COEX_MEDIUM_SHIFT (3)
3424
3425struct iwl_coex_medium_notification {
3426 __le32 status;
3427 __le32 events;
3428} __packed;
3429
3430/*
3431 * Coexistence EVENT Command
3432 * COEX_EVENT_CMD = 0x5c
3433 *
3434 * send from host to uCode for coex event request.
3435 */
3436/* flags options */
3437#define COEX_EVENT_REQUEST_MSK (0x1)
3438
3439struct iwl_coex_event_cmd {
3440 u8 flags;
3441 u8 event;
3442 __le16 reserved;
3443} __packed;
3444
3445struct iwl_coex_event_resp {
3446 __le32 status;
3447} __packed;
3448
3449
3450/******************************************************************************
3451 * Bluetooth Coexistence commands
3452 *
3453 *****************************************************************************/
3454
3455/*
3456 * BT Status notification
3457 * REPLY_BT_COEX_PROFILE_NOTIF = 0xce
3458 */
3459enum iwl_bt_coex_profile_traffic_load {
3460 IWL_BT_COEX_TRAFFIC_LOAD_NONE = 0,
3461 IWL_BT_COEX_TRAFFIC_LOAD_LOW = 1,
3462 IWL_BT_COEX_TRAFFIC_LOAD_HIGH = 2,
3463 IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS = 3,
3464/*
3465 * There are no more even though below is a u8, the
3466 * indication from the BT device only has two bits.
3467 */
3468};
3469
3470#define BT_SESSION_ACTIVITY_1_UART_MSG 0x1
3471#define BT_SESSION_ACTIVITY_2_UART_MSG 0x2
3472
3473/* BT UART message - Share Part (BT -> WiFi) */
3474#define BT_UART_MSG_FRAME1MSGTYPE_POS (0)
3475#define BT_UART_MSG_FRAME1MSGTYPE_MSK \
3476 (0x7 << BT_UART_MSG_FRAME1MSGTYPE_POS)
3477#define BT_UART_MSG_FRAME1SSN_POS (3)
3478#define BT_UART_MSG_FRAME1SSN_MSK \
3479 (0x3 << BT_UART_MSG_FRAME1SSN_POS)
3480#define BT_UART_MSG_FRAME1UPDATEREQ_POS (5)
3481#define BT_UART_MSG_FRAME1UPDATEREQ_MSK \
3482 (0x1 << BT_UART_MSG_FRAME1UPDATEREQ_POS)
3483#define BT_UART_MSG_FRAME1RESERVED_POS (6)
3484#define BT_UART_MSG_FRAME1RESERVED_MSK \
3485 (0x3 << BT_UART_MSG_FRAME1RESERVED_POS)
3486
3487#define BT_UART_MSG_FRAME2OPENCONNECTIONS_POS (0)
3488#define BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK \
3489 (0x3 << BT_UART_MSG_FRAME2OPENCONNECTIONS_POS)
3490#define BT_UART_MSG_FRAME2TRAFFICLOAD_POS (2)
3491#define BT_UART_MSG_FRAME2TRAFFICLOAD_MSK \
3492 (0x3 << BT_UART_MSG_FRAME2TRAFFICLOAD_POS)
3493#define BT_UART_MSG_FRAME2CHLSEQN_POS (4)
3494#define BT_UART_MSG_FRAME2CHLSEQN_MSK \
3495 (0x1 << BT_UART_MSG_FRAME2CHLSEQN_POS)
3496#define BT_UART_MSG_FRAME2INBAND_POS (5)
3497#define BT_UART_MSG_FRAME2INBAND_MSK \
3498 (0x1 << BT_UART_MSG_FRAME2INBAND_POS)
3499#define BT_UART_MSG_FRAME2RESERVED_POS (6)
3500#define BT_UART_MSG_FRAME2RESERVED_MSK \
3501 (0x3 << BT_UART_MSG_FRAME2RESERVED_POS)
3502
3503#define BT_UART_MSG_FRAME3SCOESCO_POS (0)
3504#define BT_UART_MSG_FRAME3SCOESCO_MSK \
3505 (0x1 << BT_UART_MSG_FRAME3SCOESCO_POS)
3506#define BT_UART_MSG_FRAME3SNIFF_POS (1)
3507#define BT_UART_MSG_FRAME3SNIFF_MSK \
3508 (0x1 << BT_UART_MSG_FRAME3SNIFF_POS)
3509#define BT_UART_MSG_FRAME3A2DP_POS (2)
3510#define BT_UART_MSG_FRAME3A2DP_MSK \
3511 (0x1 << BT_UART_MSG_FRAME3A2DP_POS)
3512#define BT_UART_MSG_FRAME3ACL_POS (3)
3513#define BT_UART_MSG_FRAME3ACL_MSK \
3514 (0x1 << BT_UART_MSG_FRAME3ACL_POS)
3515#define BT_UART_MSG_FRAME3MASTER_POS (4)
3516#define BT_UART_MSG_FRAME3MASTER_MSK \
3517 (0x1 << BT_UART_MSG_FRAME3MASTER_POS)
3518#define BT_UART_MSG_FRAME3OBEX_POS (5)
3519#define BT_UART_MSG_FRAME3OBEX_MSK \
3520 (0x1 << BT_UART_MSG_FRAME3OBEX_POS)
3521#define BT_UART_MSG_FRAME3RESERVED_POS (6)
3522#define BT_UART_MSG_FRAME3RESERVED_MSK \
3523 (0x3 << BT_UART_MSG_FRAME3RESERVED_POS)
3524
3525#define BT_UART_MSG_FRAME4IDLEDURATION_POS (0)
3526#define BT_UART_MSG_FRAME4IDLEDURATION_MSK \
3527 (0x3F << BT_UART_MSG_FRAME4IDLEDURATION_POS)
3528#define BT_UART_MSG_FRAME4RESERVED_POS (6)
3529#define BT_UART_MSG_FRAME4RESERVED_MSK \
3530 (0x3 << BT_UART_MSG_FRAME4RESERVED_POS)
3531
3532#define BT_UART_MSG_FRAME5TXACTIVITY_POS (0)
3533#define BT_UART_MSG_FRAME5TXACTIVITY_MSK \
3534 (0x3 << BT_UART_MSG_FRAME5TXACTIVITY_POS)
3535#define BT_UART_MSG_FRAME5RXACTIVITY_POS (2)
3536#define BT_UART_MSG_FRAME5RXACTIVITY_MSK \
3537 (0x3 << BT_UART_MSG_FRAME5RXACTIVITY_POS)
3538#define BT_UART_MSG_FRAME5ESCORETRANSMIT_POS (4)
3539#define BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK \
3540 (0x3 << BT_UART_MSG_FRAME5ESCORETRANSMIT_POS)
3541#define BT_UART_MSG_FRAME5RESERVED_POS (6)
3542#define BT_UART_MSG_FRAME5RESERVED_MSK \
3543 (0x3 << BT_UART_MSG_FRAME5RESERVED_POS)
3544
3545#define BT_UART_MSG_FRAME6SNIFFINTERVAL_POS (0)
3546#define BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK \
3547 (0x1F << BT_UART_MSG_FRAME6SNIFFINTERVAL_POS)
3548#define BT_UART_MSG_FRAME6DISCOVERABLE_POS (5)
3549#define BT_UART_MSG_FRAME6DISCOVERABLE_MSK \
3550 (0x1 << BT_UART_MSG_FRAME6DISCOVERABLE_POS)
3551#define BT_UART_MSG_FRAME6RESERVED_POS (6)
3552#define BT_UART_MSG_FRAME6RESERVED_MSK \
3553 (0x3 << BT_UART_MSG_FRAME6RESERVED_POS)
3554
3555#define BT_UART_MSG_FRAME7SNIFFACTIVITY_POS (0)
3556#define BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK \
3557 (0x7 << BT_UART_MSG_FRAME7SNIFFACTIVITY_POS)
3558#define BT_UART_MSG_FRAME7PAGE_POS (3)
3559#define BT_UART_MSG_FRAME7PAGE_MSK \
3560 (0x1 << BT_UART_MSG_FRAME7PAGE_POS)
3561#define BT_UART_MSG_FRAME7INQUIRY_POS (4)
3562#define BT_UART_MSG_FRAME7INQUIRY_MSK \
3563 (0x1 << BT_UART_MSG_FRAME7INQUIRY_POS)
3564#define BT_UART_MSG_FRAME7CONNECTABLE_POS (5)
3565#define BT_UART_MSG_FRAME7CONNECTABLE_MSK \
3566 (0x1 << BT_UART_MSG_FRAME7CONNECTABLE_POS)
3567#define BT_UART_MSG_FRAME7RESERVED_POS (6)
3568#define BT_UART_MSG_FRAME7RESERVED_MSK \
3569 (0x3 << BT_UART_MSG_FRAME7RESERVED_POS)
3570
3571/* BT Session Activity 2 UART message (BT -> WiFi) */
3572#define BT_UART_MSG_2_FRAME1RESERVED1_POS (5)
3573#define BT_UART_MSG_2_FRAME1RESERVED1_MSK \
3574 (0x1<<BT_UART_MSG_2_FRAME1RESERVED1_POS)
3575#define BT_UART_MSG_2_FRAME1RESERVED2_POS (6)
3576#define BT_UART_MSG_2_FRAME1RESERVED2_MSK \
3577 (0x3<<BT_UART_MSG_2_FRAME1RESERVED2_POS)
3578
3579#define BT_UART_MSG_2_FRAME2AGGTRAFFICLOAD_POS (0)
3580#define BT_UART_MSG_2_FRAME2AGGTRAFFICLOAD_MSK \
3581 (0x3F<<BT_UART_MSG_2_FRAME2AGGTRAFFICLOAD_POS)
3582#define BT_UART_MSG_2_FRAME2RESERVED_POS (6)
3583#define BT_UART_MSG_2_FRAME2RESERVED_MSK \
3584 (0x3<<BT_UART_MSG_2_FRAME2RESERVED_POS)
3585
3586#define BT_UART_MSG_2_FRAME3BRLASTTXPOWER_POS (0)
3587#define BT_UART_MSG_2_FRAME3BRLASTTXPOWER_MSK \
3588 (0xF<<BT_UART_MSG_2_FRAME3BRLASTTXPOWER_POS)
3589#define BT_UART_MSG_2_FRAME3INQPAGESRMODE_POS (4)
3590#define BT_UART_MSG_2_FRAME3INQPAGESRMODE_MSK \
3591 (0x1<<BT_UART_MSG_2_FRAME3INQPAGESRMODE_POS)
3592#define BT_UART_MSG_2_FRAME3LEMASTER_POS (5)
3593#define BT_UART_MSG_2_FRAME3LEMASTER_MSK \
3594 (0x1<<BT_UART_MSG_2_FRAME3LEMASTER_POS)
3595#define BT_UART_MSG_2_FRAME3RESERVED_POS (6)
3596#define BT_UART_MSG_2_FRAME3RESERVED_MSK \
3597 (0x3<<BT_UART_MSG_2_FRAME3RESERVED_POS)
3598
3599#define BT_UART_MSG_2_FRAME4LELASTTXPOWER_POS (0)
3600#define BT_UART_MSG_2_FRAME4LELASTTXPOWER_MSK \
3601 (0xF<<BT_UART_MSG_2_FRAME4LELASTTXPOWER_POS)
3602#define BT_UART_MSG_2_FRAME4NUMLECONN_POS (4)
3603#define BT_UART_MSG_2_FRAME4NUMLECONN_MSK \
3604 (0x3<<BT_UART_MSG_2_FRAME4NUMLECONN_POS)
3605#define BT_UART_MSG_2_FRAME4RESERVED_POS (6)
3606#define BT_UART_MSG_2_FRAME4RESERVED_MSK \
3607 (0x3<<BT_UART_MSG_2_FRAME4RESERVED_POS)
3608
3609#define BT_UART_MSG_2_FRAME5BTMINRSSI_POS (0)
3610#define BT_UART_MSG_2_FRAME5BTMINRSSI_MSK \
3611 (0xF<<BT_UART_MSG_2_FRAME5BTMINRSSI_POS)
3612#define BT_UART_MSG_2_FRAME5LESCANINITMODE_POS (4)
3613#define BT_UART_MSG_2_FRAME5LESCANINITMODE_MSK \
3614 (0x1<<BT_UART_MSG_2_FRAME5LESCANINITMODE_POS)
3615#define BT_UART_MSG_2_FRAME5LEADVERMODE_POS (5)
3616#define BT_UART_MSG_2_FRAME5LEADVERMODE_MSK \
3617 (0x1<<BT_UART_MSG_2_FRAME5LEADVERMODE_POS)
3618#define BT_UART_MSG_2_FRAME5RESERVED_POS (6)
3619#define BT_UART_MSG_2_FRAME5RESERVED_MSK \
3620 (0x3<<BT_UART_MSG_2_FRAME5RESERVED_POS)
3621
3622#define BT_UART_MSG_2_FRAME6LECONNINTERVAL_POS (0)
3623#define BT_UART_MSG_2_FRAME6LECONNINTERVAL_MSK \
3624 (0x1F<<BT_UART_MSG_2_FRAME6LECONNINTERVAL_POS)
3625#define BT_UART_MSG_2_FRAME6RFU_POS (5)
3626#define BT_UART_MSG_2_FRAME6RFU_MSK \
3627 (0x1<<BT_UART_MSG_2_FRAME6RFU_POS)
3628#define BT_UART_MSG_2_FRAME6RESERVED_POS (6)
3629#define BT_UART_MSG_2_FRAME6RESERVED_MSK \
3630 (0x3<<BT_UART_MSG_2_FRAME6RESERVED_POS)
3631
3632#define BT_UART_MSG_2_FRAME7LECONNSLAVELAT_POS (0)
3633#define BT_UART_MSG_2_FRAME7LECONNSLAVELAT_MSK \
3634 (0x7<<BT_UART_MSG_2_FRAME7LECONNSLAVELAT_POS)
3635#define BT_UART_MSG_2_FRAME7LEPROFILE1_POS (3)
3636#define BT_UART_MSG_2_FRAME7LEPROFILE1_MSK \
3637 (0x1<<BT_UART_MSG_2_FRAME7LEPROFILE1_POS)
3638#define BT_UART_MSG_2_FRAME7LEPROFILE2_POS (4)
3639#define BT_UART_MSG_2_FRAME7LEPROFILE2_MSK \
3640 (0x1<<BT_UART_MSG_2_FRAME7LEPROFILE2_POS)
3641#define BT_UART_MSG_2_FRAME7LEPROFILEOTHER_POS (5)
3642#define BT_UART_MSG_2_FRAME7LEPROFILEOTHER_MSK \
3643 (0x1<<BT_UART_MSG_2_FRAME7LEPROFILEOTHER_POS)
3644#define BT_UART_MSG_2_FRAME7RESERVED_POS (6)
3645#define BT_UART_MSG_2_FRAME7RESERVED_MSK \
3646 (0x3<<BT_UART_MSG_2_FRAME7RESERVED_POS)
3647
3648
3649#define BT_ENABLE_REDUCED_TXPOWER_THRESHOLD (-62)
3650#define BT_DISABLE_REDUCED_TXPOWER_THRESHOLD (-65)
3651
3652struct iwl_bt_uart_msg {
3653 u8 header;
3654 u8 frame1;
3655 u8 frame2;
3656 u8 frame3;
3657 u8 frame4;
3658 u8 frame5;
3659 u8 frame6;
3660 u8 frame7;
3661} __attribute__((packed));
3662
3663struct iwl_bt_coex_profile_notif {
3664 struct iwl_bt_uart_msg last_bt_uart_msg;
3665 u8 bt_status; /* 0 - off, 1 - on */
3666 u8 bt_traffic_load; /* 0 .. 3? */
3667 u8 bt_ci_compliance; /* 0 - not complied, 1 - complied */
3668 u8 reserved;
3669} __attribute__((packed));
3670
3671#define IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS 0
3672#define IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_MSK 0x1
3673#define IWL_BT_COEX_PRIO_TBL_PRIO_POS 1
3674#define IWL_BT_COEX_PRIO_TBL_PRIO_MASK 0x0e
3675#define IWL_BT_COEX_PRIO_TBL_RESERVED_POS 4
3676#define IWL_BT_COEX_PRIO_TBL_RESERVED_MASK 0xf0
3677#define IWL_BT_COEX_PRIO_TBL_PRIO_SHIFT 1
3678
3679/*
3680 * BT Coexistence Priority table
3681 * REPLY_BT_COEX_PRIO_TABLE = 0xcc
3682 */
3683enum bt_coex_prio_table_events {
3684 BT_COEX_PRIO_TBL_EVT_INIT_CALIB1 = 0,
3685 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2 = 1,
3686 BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW1 = 2,
3687 BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW2 = 3, /* DC calib */
3688 BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH1 = 4,
3689 BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH2 = 5,
3690 BT_COEX_PRIO_TBL_EVT_DTIM = 6,
3691 BT_COEX_PRIO_TBL_EVT_SCAN52 = 7,
3692 BT_COEX_PRIO_TBL_EVT_SCAN24 = 8,
3693 BT_COEX_PRIO_TBL_EVT_RESERVED0 = 9,
3694 BT_COEX_PRIO_TBL_EVT_RESERVED1 = 10,
3695 BT_COEX_PRIO_TBL_EVT_RESERVED2 = 11,
3696 BT_COEX_PRIO_TBL_EVT_RESERVED3 = 12,
3697 BT_COEX_PRIO_TBL_EVT_RESERVED4 = 13,
3698 BT_COEX_PRIO_TBL_EVT_RESERVED5 = 14,
3699 BT_COEX_PRIO_TBL_EVT_RESERVED6 = 15,
3700 /* BT_COEX_PRIO_TBL_EVT_MAX should always be last */
3701 BT_COEX_PRIO_TBL_EVT_MAX,
3702};
3703
3704enum bt_coex_prio_table_priorities {
3705 BT_COEX_PRIO_TBL_DISABLED = 0,
3706 BT_COEX_PRIO_TBL_PRIO_LOW = 1,
3707 BT_COEX_PRIO_TBL_PRIO_HIGH = 2,
3708 BT_COEX_PRIO_TBL_PRIO_BYPASS = 3,
3709 BT_COEX_PRIO_TBL_PRIO_COEX_OFF = 4,
3710 BT_COEX_PRIO_TBL_PRIO_COEX_ON = 5,
3711 BT_COEX_PRIO_TBL_PRIO_RSRVD1 = 6,
3712 BT_COEX_PRIO_TBL_PRIO_RSRVD2 = 7,
3713 BT_COEX_PRIO_TBL_MAX,
3714};
3715
3716struct iwl_bt_coex_prio_table_cmd {
3717 u8 prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX];
3718} __attribute__((packed));
3719
3720#define IWL_BT_COEX_ENV_CLOSE 0
3721#define IWL_BT_COEX_ENV_OPEN 1
3722/*
3723 * BT Protection Envelope
3724 * REPLY_BT_COEX_PROT_ENV = 0xcd
3725 */
3726struct iwl_bt_coex_prot_env_cmd {
3727 u8 action; /* 0 = closed, 1 = open */
3728 u8 type; /* 0 .. 15 */
3729 u8 reserved[2];
3730} __attribute__((packed));
3731
3732/*
3733 * REPLY_D3_CONFIG
3734 */
3735enum iwlagn_d3_wakeup_filters {
3736 IWLAGN_D3_WAKEUP_RFKILL = BIT(0),
3737 IWLAGN_D3_WAKEUP_SYSASSERT = BIT(1),
3738};
3739
3740struct iwlagn_d3_config_cmd {
3741 __le32 min_sleep_time;
3742 __le32 wakeup_flags;
3743} __packed;
3744
3745/*
3746 * REPLY_WOWLAN_PATTERNS
3747 */
3748#define IWLAGN_WOWLAN_MIN_PATTERN_LEN 16
3749#define IWLAGN_WOWLAN_MAX_PATTERN_LEN 128
3750
3751struct iwlagn_wowlan_pattern {
3752 u8 mask[IWLAGN_WOWLAN_MAX_PATTERN_LEN / 8];
3753 u8 pattern[IWLAGN_WOWLAN_MAX_PATTERN_LEN];
3754 u8 mask_size;
3755 u8 pattern_size;
3756 __le16 reserved;
3757} __packed;
3758
3759#define IWLAGN_WOWLAN_MAX_PATTERNS 20
3760
3761struct iwlagn_wowlan_patterns_cmd {
3762 __le32 n_patterns;
3763 struct iwlagn_wowlan_pattern patterns[];
3764} __packed;
3765
3766/*
3767 * REPLY_WOWLAN_WAKEUP_FILTER
3768 */
3769enum iwlagn_wowlan_wakeup_filters {
3770 IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET = BIT(0),
3771 IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH = BIT(1),
3772 IWLAGN_WOWLAN_WAKEUP_BEACON_MISS = BIT(2),
3773 IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE = BIT(3),
3774 IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL = BIT(4),
3775 IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ = BIT(5),
3776 IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE = BIT(6),
3777 IWLAGN_WOWLAN_WAKEUP_ALWAYS = BIT(7),
3778 IWLAGN_WOWLAN_WAKEUP_ENABLE_NET_DETECT = BIT(8),
3779};
3780
3781struct iwlagn_wowlan_wakeup_filter_cmd {
3782 __le32 enabled;
3783 __le16 non_qos_seq;
3784 __le16 reserved;
3785 __le16 qos_seq[8];
3786};
3787
3788/*
3789 * REPLY_WOWLAN_TSC_RSC_PARAMS
3790 */
3791#define IWLAGN_NUM_RSC 16
3792
3793struct tkip_sc {
3794 __le16 iv16;
3795 __le16 pad;
3796 __le32 iv32;
3797} __packed;
3798
3799struct iwlagn_tkip_rsc_tsc {
3800 struct tkip_sc unicast_rsc[IWLAGN_NUM_RSC];
3801 struct tkip_sc multicast_rsc[IWLAGN_NUM_RSC];
3802 struct tkip_sc tsc;
3803} __packed;
3804
3805struct aes_sc {
3806 __le64 pn;
3807} __packed;
3808
3809struct iwlagn_aes_rsc_tsc {
3810 struct aes_sc unicast_rsc[IWLAGN_NUM_RSC];
3811 struct aes_sc multicast_rsc[IWLAGN_NUM_RSC];
3812 struct aes_sc tsc;
3813} __packed;
3814
3815union iwlagn_all_tsc_rsc {
3816 struct iwlagn_tkip_rsc_tsc tkip;
3817 struct iwlagn_aes_rsc_tsc aes;
3818};
3819
3820struct iwlagn_wowlan_rsc_tsc_params_cmd {
3821 union iwlagn_all_tsc_rsc all_tsc_rsc;
3822} __packed;
3823
3824/*
3825 * REPLY_WOWLAN_TKIP_PARAMS
3826 */
3827#define IWLAGN_MIC_KEY_SIZE 8
3828#define IWLAGN_P1K_SIZE 5
3829struct iwlagn_mic_keys {
3830 u8 tx[IWLAGN_MIC_KEY_SIZE];
3831 u8 rx_unicast[IWLAGN_MIC_KEY_SIZE];
3832 u8 rx_mcast[IWLAGN_MIC_KEY_SIZE];
3833} __packed;
3834
3835struct iwlagn_p1k_cache {
3836 __le16 p1k[IWLAGN_P1K_SIZE];
3837} __packed;
3838
3839#define IWLAGN_NUM_RX_P1K_CACHE 2
3840
3841struct iwlagn_wowlan_tkip_params_cmd {
3842 struct iwlagn_mic_keys mic_keys;
3843 struct iwlagn_p1k_cache tx;
3844 struct iwlagn_p1k_cache rx_uni[IWLAGN_NUM_RX_P1K_CACHE];
3845 struct iwlagn_p1k_cache rx_multi[IWLAGN_NUM_RX_P1K_CACHE];
3846} __packed;
3847
3848/*
3849 * REPLY_WOWLAN_KEK_KCK_MATERIAL
3850 */
3851
3852#define IWLAGN_KCK_MAX_SIZE 32
3853#define IWLAGN_KEK_MAX_SIZE 32
3854
3855struct iwlagn_wowlan_kek_kck_material_cmd {
3856 u8 kck[IWLAGN_KCK_MAX_SIZE];
3857 u8 kek[IWLAGN_KEK_MAX_SIZE];
3858 __le16 kck_len;
3859 __le16 kek_len;
3860 __le64 replay_ctr;
3861} __packed;
3862
3863/*
3864 * REPLY_WIPAN_PARAMS = 0xb2 (Commands and Notification)
3865 */
3866
3867/*
3868 * Minimum slot time in TU
3869 */
3870#define IWL_MIN_SLOT_TIME 20
3871
3872/**
3873 * struct iwl_wipan_slot
3874 * @width: Time in TU
3875 * @type:
3876 * 0 - BSS
3877 * 1 - PAN
3878 */
3879struct iwl_wipan_slot {
3880 __le16 width;
3881 u8 type;
3882 u8 reserved;
3883} __packed;
3884
3885#define IWL_WIPAN_PARAMS_FLG_LEAVE_CHANNEL_CTS BIT(1) /* reserved */
3886#define IWL_WIPAN_PARAMS_FLG_LEAVE_CHANNEL_QUIET BIT(2) /* reserved */
3887#define IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE BIT(3) /* reserved */
3888#define IWL_WIPAN_PARAMS_FLG_FILTER_BEACON_NOTIF BIT(4)
3889#define IWL_WIPAN_PARAMS_FLG_FULL_SLOTTED_MODE BIT(5)
3890
3891/**
3892 * struct iwl_wipan_params_cmd
3893 * @flags:
3894 * bit0: reserved
3895 * bit1: CP leave channel with CTS
3896 * bit2: CP leave channel qith Quiet
3897 * bit3: slotted mode
3898 * 1 - work in slotted mode
3899 * 0 - work in non slotted mode
3900 * bit4: filter beacon notification
3901 * bit5: full tx slotted mode. if this flag is set,
3902 * uCode will perform leaving channel methods in context switch
3903 * also when working in same channel mode
3904 * @num_slots: 1 - 10
3905 */
3906struct iwl_wipan_params_cmd {
3907 __le16 flags;
3908 u8 reserved;
3909 u8 num_slots;
3910 struct iwl_wipan_slot slots[10];
3911} __packed;
3912
3913/*
3914 * REPLY_WIPAN_P2P_CHANNEL_SWITCH = 0xb9
3915 *
3916 * TODO: Figure out what this is used for,
3917 * it can only switch between 2.4 GHz
3918 * channels!!
3919 */
3920
3921struct iwl_wipan_p2p_channel_switch_cmd {
3922 __le16 channel;
3923 __le16 reserved;
3924};
3925
3926/*
3927 * REPLY_WIPAN_NOA_NOTIFICATION = 0xbc
3928 *
3929 * This is used by the device to notify us of the
3930 * NoA schedule it determined so we can forward it
3931 * to userspace for inclusion in probe responses.
3932 *
3933 * In beacons, the NoA schedule is simply appended
3934 * to the frame we give the device.
3935 */
3936
3937struct iwl_wipan_noa_descriptor {
3938 u8 count;
3939 __le32 duration;
3940 __le32 interval;
3941 __le32 starttime;
3942} __packed;
3943
3944struct iwl_wipan_noa_attribute {
3945 u8 id;
3946 __le16 length;
3947 u8 index;
3948 u8 ct_window;
3949 struct iwl_wipan_noa_descriptor descr0, descr1;
3950 u8 reserved;
3951} __packed;
3952
3953struct iwl_wipan_noa_notification {
3954 u32 noa_active;
3955 struct iwl_wipan_noa_attribute noa_attribute;
3956} __packed;
3957
3958#endif /* __iwl_commands_h__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/debugfs.c b/drivers/net/wireless/iwlwifi/dvm/debugfs.c
new file mode 100644
index 000000000000..2268adddf4c0
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/debugfs.c
@@ -0,0 +1,2433 @@
1/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29#include <linux/slab.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/debugfs.h>
33#include <linux/ieee80211.h>
34#include <net/mac80211.h>
35#include "iwl-debug.h"
36#include "iwl-io.h"
37#include "dev.h"
38#include "agn.h"
39
40/* create and remove of files */
41#define DEBUGFS_ADD_FILE(name, parent, mode) do { \
42 if (!debugfs_create_file(#name, mode, parent, priv, \
43 &iwl_dbgfs_##name##_ops)) \
44 goto err; \
45} while (0)
46
47#define DEBUGFS_ADD_BOOL(name, parent, ptr) do { \
48 struct dentry *__tmp; \
49 __tmp = debugfs_create_bool(#name, S_IWUSR | S_IRUSR, \
50 parent, ptr); \
51 if (IS_ERR(__tmp) || !__tmp) \
52 goto err; \
53} while (0)
54
55#define DEBUGFS_ADD_X32(name, parent, ptr) do { \
56 struct dentry *__tmp; \
57 __tmp = debugfs_create_x32(#name, S_IWUSR | S_IRUSR, \
58 parent, ptr); \
59 if (IS_ERR(__tmp) || !__tmp) \
60 goto err; \
61} while (0)
62
63#define DEBUGFS_ADD_U32(name, parent, ptr, mode) do { \
64 struct dentry *__tmp; \
65 __tmp = debugfs_create_u32(#name, mode, \
66 parent, ptr); \
67 if (IS_ERR(__tmp) || !__tmp) \
68 goto err; \
69} while (0)
70
71/* file operation */
72#define DEBUGFS_READ_FUNC(name) \
73static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
74 char __user *user_buf, \
75 size_t count, loff_t *ppos);
76
77#define DEBUGFS_WRITE_FUNC(name) \
78static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
79 const char __user *user_buf, \
80 size_t count, loff_t *ppos);
81
82
83#define DEBUGFS_READ_FILE_OPS(name) \
84 DEBUGFS_READ_FUNC(name); \
85static const struct file_operations iwl_dbgfs_##name##_ops = { \
86 .read = iwl_dbgfs_##name##_read, \
87 .open = simple_open, \
88 .llseek = generic_file_llseek, \
89};
90
91#define DEBUGFS_WRITE_FILE_OPS(name) \
92 DEBUGFS_WRITE_FUNC(name); \
93static const struct file_operations iwl_dbgfs_##name##_ops = { \
94 .write = iwl_dbgfs_##name##_write, \
95 .open = simple_open, \
96 .llseek = generic_file_llseek, \
97};
98
99
100#define DEBUGFS_READ_WRITE_FILE_OPS(name) \
101 DEBUGFS_READ_FUNC(name); \
102 DEBUGFS_WRITE_FUNC(name); \
103static const struct file_operations iwl_dbgfs_##name##_ops = { \
104 .write = iwl_dbgfs_##name##_write, \
105 .read = iwl_dbgfs_##name##_read, \
106 .open = simple_open, \
107 .llseek = generic_file_llseek, \
108};
109
110static ssize_t iwl_dbgfs_sram_read(struct file *file,
111 char __user *user_buf,
112 size_t count, loff_t *ppos)
113{
114 u32 val = 0;
115 char *buf;
116 ssize_t ret;
117 int i = 0;
118 bool device_format = false;
119 int offset = 0;
120 int len = 0;
121 int pos = 0;
122 int sram;
123 struct iwl_priv *priv = file->private_data;
124 const struct fw_img *img;
125 size_t bufsz;
126
127 /* default is to dump the entire data segment */
128 if (!priv->dbgfs_sram_offset && !priv->dbgfs_sram_len) {
129 priv->dbgfs_sram_offset = 0x800000;
130 if (!priv->ucode_loaded)
131 return -EINVAL;
132 img = &priv->fw->img[priv->cur_ucode];
133 priv->dbgfs_sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
134 }
135 len = priv->dbgfs_sram_len;
136
137 if (len == -4) {
138 device_format = true;
139 len = 4;
140 }
141
142 bufsz = 50 + len * 4;
143 buf = kmalloc(bufsz, GFP_KERNEL);
144 if (!buf)
145 return -ENOMEM;
146
147 pos += scnprintf(buf + pos, bufsz - pos, "sram_len: 0x%x\n",
148 len);
149 pos += scnprintf(buf + pos, bufsz - pos, "sram_offset: 0x%x\n",
150 priv->dbgfs_sram_offset);
151
152 /* adjust sram address since reads are only on even u32 boundaries */
153 offset = priv->dbgfs_sram_offset & 0x3;
154 sram = priv->dbgfs_sram_offset & ~0x3;
155
156 /* read the first u32 from sram */
157 val = iwl_read_targ_mem(priv->trans, sram);
158
159 for (; len; len--) {
160 /* put the address at the start of every line */
161 if (i == 0)
162 pos += scnprintf(buf + pos, bufsz - pos,
163 "%08X: ", sram + offset);
164
165 if (device_format)
166 pos += scnprintf(buf + pos, bufsz - pos,
167 "%02x", (val >> (8 * (3 - offset))) & 0xff);
168 else
169 pos += scnprintf(buf + pos, bufsz - pos,
170 "%02x ", (val >> (8 * offset)) & 0xff);
171
172 /* if all bytes processed, read the next u32 from sram */
173 if (++offset == 4) {
174 sram += 4;
175 offset = 0;
176 val = iwl_read_targ_mem(priv->trans, sram);
177 }
178
179 /* put in extra spaces and split lines for human readability */
180 if (++i == 16) {
181 i = 0;
182 pos += scnprintf(buf + pos, bufsz - pos, "\n");
183 } else if (!(i & 7)) {
184 pos += scnprintf(buf + pos, bufsz - pos, " ");
185 } else if (!(i & 3)) {
186 pos += scnprintf(buf + pos, bufsz - pos, " ");
187 }
188 }
189 if (i)
190 pos += scnprintf(buf + pos, bufsz - pos, "\n");
191
192 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
193 kfree(buf);
194 return ret;
195}
196
197static ssize_t iwl_dbgfs_sram_write(struct file *file,
198 const char __user *user_buf,
199 size_t count, loff_t *ppos)
200{
201 struct iwl_priv *priv = file->private_data;
202 char buf[64];
203 int buf_size;
204 u32 offset, len;
205
206 memset(buf, 0, sizeof(buf));
207 buf_size = min(count, sizeof(buf) - 1);
208 if (copy_from_user(buf, user_buf, buf_size))
209 return -EFAULT;
210
211 if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
212 priv->dbgfs_sram_offset = offset;
213 priv->dbgfs_sram_len = len;
214 } else if (sscanf(buf, "%x", &offset) == 1) {
215 priv->dbgfs_sram_offset = offset;
216 priv->dbgfs_sram_len = -4;
217 } else {
218 priv->dbgfs_sram_offset = 0;
219 priv->dbgfs_sram_len = 0;
220 }
221
222 return count;
223}
224
225static ssize_t iwl_dbgfs_wowlan_sram_read(struct file *file,
226 char __user *user_buf,
227 size_t count, loff_t *ppos)
228{
229 struct iwl_priv *priv = file->private_data;
230 const struct fw_img *img = &priv->fw->img[IWL_UCODE_WOWLAN];
231
232 if (!priv->wowlan_sram)
233 return -ENODATA;
234
235 return simple_read_from_buffer(user_buf, count, ppos,
236 priv->wowlan_sram,
237 img->sec[IWL_UCODE_SECTION_DATA].len);
238}
239static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
240 size_t count, loff_t *ppos)
241{
242 struct iwl_priv *priv = file->private_data;
243 struct iwl_station_entry *station;
244 struct iwl_tid_data *tid_data;
245 char *buf;
246 int i, j, pos = 0;
247 ssize_t ret;
248 /* Add 30 for initial string */
249 const size_t bufsz = 30 + sizeof(char) * 500 * (priv->num_stations);
250
251 buf = kmalloc(bufsz, GFP_KERNEL);
252 if (!buf)
253 return -ENOMEM;
254
255 pos += scnprintf(buf + pos, bufsz - pos, "num of stations: %d\n\n",
256 priv->num_stations);
257
258 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
259 station = &priv->stations[i];
260 if (!station->used)
261 continue;
262 pos += scnprintf(buf + pos, bufsz - pos,
263 "station %d - addr: %pM, flags: %#x\n",
264 i, station->sta.sta.addr,
265 station->sta.station_flags_msk);
266 pos += scnprintf(buf + pos, bufsz - pos,
267 "TID seqno next_rclmd "
268 "rate_n_flags state txq\n");
269
270 for (j = 0; j < IWL_MAX_TID_COUNT; j++) {
271 tid_data = &priv->tid_data[i][j];
272 pos += scnprintf(buf + pos, bufsz - pos,
273 "%d: 0x%.4x 0x%.4x 0x%.8x "
274 "%d %.2d",
275 j, tid_data->seq_number,
276 tid_data->next_reclaimed,
277 tid_data->agg.rate_n_flags,
278 tid_data->agg.state,
279 tid_data->agg.txq_id);
280
281 if (tid_data->agg.wait_for_ba)
282 pos += scnprintf(buf + pos, bufsz - pos,
283 " - waitforba");
284 pos += scnprintf(buf + pos, bufsz - pos, "\n");
285 }
286
287 pos += scnprintf(buf + pos, bufsz - pos, "\n");
288 }
289
290 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
291 kfree(buf);
292 return ret;
293}
294
295static ssize_t iwl_dbgfs_nvm_read(struct file *file,
296 char __user *user_buf,
297 size_t count,
298 loff_t *ppos)
299{
300 ssize_t ret;
301 struct iwl_priv *priv = file->private_data;
302 int pos = 0, ofs = 0, buf_size = 0;
303 const u8 *ptr;
304 char *buf;
305 u16 eeprom_ver;
306 size_t eeprom_len = priv->cfg->base_params->eeprom_size;
307 buf_size = 4 * eeprom_len + 256;
308
309 if (eeprom_len % 16)
310 return -ENODATA;
311
312 ptr = priv->eeprom;
313 if (!ptr)
314 return -ENOMEM;
315
316 /* 4 characters for byte 0xYY */
317 buf = kzalloc(buf_size, GFP_KERNEL);
318 if (!buf)
319 return -ENOMEM;
320
321 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
322 pos += scnprintf(buf + pos, buf_size - pos, "NVM Type: %s, "
323 "version: 0x%x\n",
324 (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
325 ? "OTP" : "EEPROM", eeprom_ver);
326 for (ofs = 0 ; ofs < eeprom_len ; ofs += 16) {
327 pos += scnprintf(buf + pos, buf_size - pos, "0x%.4x ", ofs);
328 hex_dump_to_buffer(ptr + ofs, 16 , 16, 2, buf + pos,
329 buf_size - pos, 0);
330 pos += strlen(buf + pos);
331 if (buf_size - pos > 0)
332 buf[pos++] = '\n';
333 }
334
335 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
336 kfree(buf);
337 return ret;
338}
339
340static ssize_t iwl_dbgfs_channels_read(struct file *file, char __user *user_buf,
341 size_t count, loff_t *ppos)
342{
343 struct iwl_priv *priv = file->private_data;
344 struct ieee80211_channel *channels = NULL;
345 const struct ieee80211_supported_band *supp_band = NULL;
346 int pos = 0, i, bufsz = PAGE_SIZE;
347 char *buf;
348 ssize_t ret;
349
350 if (!test_bit(STATUS_GEO_CONFIGURED, &priv->status))
351 return -EAGAIN;
352
353 buf = kzalloc(bufsz, GFP_KERNEL);
354 if (!buf)
355 return -ENOMEM;
356
357 supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_2GHZ);
358 if (supp_band) {
359 channels = supp_band->channels;
360
361 pos += scnprintf(buf + pos, bufsz - pos,
362 "Displaying %d channels in 2.4GHz band 802.11bg):\n",
363 supp_band->n_channels);
364
365 for (i = 0; i < supp_band->n_channels; i++)
366 pos += scnprintf(buf + pos, bufsz - pos,
367 "%d: %ddBm: BSS%s%s, %s.\n",
368 channels[i].hw_value,
369 channels[i].max_power,
370 channels[i].flags & IEEE80211_CHAN_RADAR ?
371 " (IEEE 802.11h required)" : "",
372 ((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
373 || (channels[i].flags &
374 IEEE80211_CHAN_RADAR)) ? "" :
375 ", IBSS",
376 channels[i].flags &
377 IEEE80211_CHAN_PASSIVE_SCAN ?
378 "passive only" : "active/passive");
379 }
380 supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_5GHZ);
381 if (supp_band) {
382 channels = supp_band->channels;
383
384 pos += scnprintf(buf + pos, bufsz - pos,
385 "Displaying %d channels in 5.2GHz band (802.11a)\n",
386 supp_band->n_channels);
387
388 for (i = 0; i < supp_band->n_channels; i++)
389 pos += scnprintf(buf + pos, bufsz - pos,
390 "%d: %ddBm: BSS%s%s, %s.\n",
391 channels[i].hw_value,
392 channels[i].max_power,
393 channels[i].flags & IEEE80211_CHAN_RADAR ?
394 " (IEEE 802.11h required)" : "",
395 ((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
396 || (channels[i].flags &
397 IEEE80211_CHAN_RADAR)) ? "" :
398 ", IBSS",
399 channels[i].flags &
400 IEEE80211_CHAN_PASSIVE_SCAN ?
401 "passive only" : "active/passive");
402 }
403 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
404 kfree(buf);
405 return ret;
406}
407
408static ssize_t iwl_dbgfs_status_read(struct file *file,
409 char __user *user_buf,
410 size_t count, loff_t *ppos) {
411
412 struct iwl_priv *priv = file->private_data;
413 char buf[512];
414 int pos = 0;
415 const size_t bufsz = sizeof(buf);
416
417 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_RF_KILL_HW:\t %d\n",
418 test_bit(STATUS_RF_KILL_HW, &priv->status));
419 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_CT_KILL:\t\t %d\n",
420 test_bit(STATUS_CT_KILL, &priv->status));
421 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_ALIVE:\t\t %d\n",
422 test_bit(STATUS_ALIVE, &priv->status));
423 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_READY:\t\t %d\n",
424 test_bit(STATUS_READY, &priv->status));
425 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_GEO_CONFIGURED:\t %d\n",
426 test_bit(STATUS_GEO_CONFIGURED, &priv->status));
427 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_EXIT_PENDING:\t %d\n",
428 test_bit(STATUS_EXIT_PENDING, &priv->status));
429 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_STATISTICS:\t %d\n",
430 test_bit(STATUS_STATISTICS, &priv->status));
431 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCANNING:\t %d\n",
432 test_bit(STATUS_SCANNING, &priv->status));
433 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCAN_ABORTING:\t %d\n",
434 test_bit(STATUS_SCAN_ABORTING, &priv->status));
435 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_SCAN_HW:\t\t %d\n",
436 test_bit(STATUS_SCAN_HW, &priv->status));
437 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_POWER_PMI:\t %d\n",
438 test_bit(STATUS_POWER_PMI, &priv->status));
439 pos += scnprintf(buf + pos, bufsz - pos, "STATUS_FW_ERROR:\t %d\n",
440 test_bit(STATUS_FW_ERROR, &priv->status));
441 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
442}
443
444static ssize_t iwl_dbgfs_rx_handlers_read(struct file *file,
445 char __user *user_buf,
446 size_t count, loff_t *ppos) {
447
448 struct iwl_priv *priv = file->private_data;
449
450 int pos = 0;
451 int cnt = 0;
452 char *buf;
453 int bufsz = 24 * 64; /* 24 items * 64 char per item */
454 ssize_t ret;
455
456 buf = kzalloc(bufsz, GFP_KERNEL);
457 if (!buf)
458 return -ENOMEM;
459
460 for (cnt = 0; cnt < REPLY_MAX; cnt++) {
461 if (priv->rx_handlers_stats[cnt] > 0)
462 pos += scnprintf(buf + pos, bufsz - pos,
463 "\tRx handler[%36s]:\t\t %u\n",
464 iwl_dvm_get_cmd_string(cnt),
465 priv->rx_handlers_stats[cnt]);
466 }
467
468 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
469 kfree(buf);
470 return ret;
471}
472
473static ssize_t iwl_dbgfs_rx_handlers_write(struct file *file,
474 const char __user *user_buf,
475 size_t count, loff_t *ppos)
476{
477 struct iwl_priv *priv = file->private_data;
478
479 char buf[8];
480 int buf_size;
481 u32 reset_flag;
482
483 memset(buf, 0, sizeof(buf));
484 buf_size = min(count, sizeof(buf) - 1);
485 if (copy_from_user(buf, user_buf, buf_size))
486 return -EFAULT;
487 if (sscanf(buf, "%x", &reset_flag) != 1)
488 return -EFAULT;
489 if (reset_flag == 0)
490 memset(&priv->rx_handlers_stats[0], 0,
491 sizeof(priv->rx_handlers_stats));
492
493 return count;
494}
495
496static ssize_t iwl_dbgfs_qos_read(struct file *file, char __user *user_buf,
497 size_t count, loff_t *ppos)
498{
499 struct iwl_priv *priv = file->private_data;
500 struct iwl_rxon_context *ctx;
501 int pos = 0, i;
502 char buf[256 * NUM_IWL_RXON_CTX];
503 const size_t bufsz = sizeof(buf);
504
505 for_each_context(priv, ctx) {
506 pos += scnprintf(buf + pos, bufsz - pos, "context %d:\n",
507 ctx->ctxid);
508 for (i = 0; i < AC_NUM; i++) {
509 pos += scnprintf(buf + pos, bufsz - pos,
510 "\tcw_min\tcw_max\taifsn\ttxop\n");
511 pos += scnprintf(buf + pos, bufsz - pos,
512 "AC[%d]\t%u\t%u\t%u\t%u\n", i,
513 ctx->qos_data.def_qos_parm.ac[i].cw_min,
514 ctx->qos_data.def_qos_parm.ac[i].cw_max,
515 ctx->qos_data.def_qos_parm.ac[i].aifsn,
516 ctx->qos_data.def_qos_parm.ac[i].edca_txop);
517 }
518 pos += scnprintf(buf + pos, bufsz - pos, "\n");
519 }
520 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
521}
522
523static ssize_t iwl_dbgfs_thermal_throttling_read(struct file *file,
524 char __user *user_buf,
525 size_t count, loff_t *ppos)
526{
527 struct iwl_priv *priv = file->private_data;
528 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
529 struct iwl_tt_restriction *restriction;
530 char buf[100];
531 int pos = 0;
532 const size_t bufsz = sizeof(buf);
533
534 pos += scnprintf(buf + pos, bufsz - pos,
535 "Thermal Throttling Mode: %s\n",
536 tt->advanced_tt ? "Advance" : "Legacy");
537 pos += scnprintf(buf + pos, bufsz - pos,
538 "Thermal Throttling State: %d\n",
539 tt->state);
540 if (tt->advanced_tt) {
541 restriction = tt->restriction + tt->state;
542 pos += scnprintf(buf + pos, bufsz - pos,
543 "Tx mode: %d\n",
544 restriction->tx_stream);
545 pos += scnprintf(buf + pos, bufsz - pos,
546 "Rx mode: %d\n",
547 restriction->rx_stream);
548 pos += scnprintf(buf + pos, bufsz - pos,
549 "HT mode: %d\n",
550 restriction->is_ht);
551 }
552 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
553}
554
555static ssize_t iwl_dbgfs_disable_ht40_write(struct file *file,
556 const char __user *user_buf,
557 size_t count, loff_t *ppos)
558{
559 struct iwl_priv *priv = file->private_data;
560 char buf[8];
561 int buf_size;
562 int ht40;
563
564 memset(buf, 0, sizeof(buf));
565 buf_size = min(count, sizeof(buf) - 1);
566 if (copy_from_user(buf, user_buf, buf_size))
567 return -EFAULT;
568 if (sscanf(buf, "%d", &ht40) != 1)
569 return -EFAULT;
570 if (!iwl_is_any_associated(priv))
571 priv->disable_ht40 = ht40 ? true : false;
572 else
573 return -EINVAL;
574
575 return count;
576}
577
578static ssize_t iwl_dbgfs_disable_ht40_read(struct file *file,
579 char __user *user_buf,
580 size_t count, loff_t *ppos)
581{
582 struct iwl_priv *priv = file->private_data;
583 char buf[100];
584 int pos = 0;
585 const size_t bufsz = sizeof(buf);
586
587 pos += scnprintf(buf + pos, bufsz - pos,
588 "11n 40MHz Mode: %s\n",
589 priv->disable_ht40 ? "Disabled" : "Enabled");
590 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
591}
592
593static ssize_t iwl_dbgfs_temperature_read(struct file *file,
594 char __user *user_buf,
595 size_t count, loff_t *ppos)
596{
597 struct iwl_priv *priv = file->private_data;
598 char buf[8];
599 int pos = 0;
600 const size_t bufsz = sizeof(buf);
601
602 pos += scnprintf(buf + pos, bufsz - pos, "%d\n", priv->temperature);
603 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
604}
605
606
607static ssize_t iwl_dbgfs_sleep_level_override_write(struct file *file,
608 const char __user *user_buf,
609 size_t count, loff_t *ppos)
610{
611 struct iwl_priv *priv = file->private_data;
612 char buf[8];
613 int buf_size;
614 int value;
615
616 memset(buf, 0, sizeof(buf));
617 buf_size = min(count, sizeof(buf) - 1);
618 if (copy_from_user(buf, user_buf, buf_size))
619 return -EFAULT;
620
621 if (sscanf(buf, "%d", &value) != 1)
622 return -EINVAL;
623
624 /*
625 * Our users expect 0 to be "CAM", but 0 isn't actually
626 * valid here. However, let's not confuse them and present
627 * IWL_POWER_INDEX_1 as "1", not "0".
628 */
629 if (value == 0)
630 return -EINVAL;
631 else if (value > 0)
632 value -= 1;
633
634 if (value != -1 && (value < 0 || value >= IWL_POWER_NUM))
635 return -EINVAL;
636
637 if (!iwl_is_ready_rf(priv))
638 return -EAGAIN;
639
640 priv->power_data.debug_sleep_level_override = value;
641
642 mutex_lock(&priv->mutex);
643 iwl_power_update_mode(priv, true);
644 mutex_unlock(&priv->mutex);
645
646 return count;
647}
648
649static ssize_t iwl_dbgfs_sleep_level_override_read(struct file *file,
650 char __user *user_buf,
651 size_t count, loff_t *ppos)
652{
653 struct iwl_priv *priv = file->private_data;
654 char buf[10];
655 int pos, value;
656 const size_t bufsz = sizeof(buf);
657
658 /* see the write function */
659 value = priv->power_data.debug_sleep_level_override;
660 if (value >= 0)
661 value += 1;
662
663 pos = scnprintf(buf, bufsz, "%d\n", value);
664 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
665}
666
667static ssize_t iwl_dbgfs_current_sleep_command_read(struct file *file,
668 char __user *user_buf,
669 size_t count, loff_t *ppos)
670{
671 struct iwl_priv *priv = file->private_data;
672 char buf[200];
673 int pos = 0, i;
674 const size_t bufsz = sizeof(buf);
675 struct iwl_powertable_cmd *cmd = &priv->power_data.sleep_cmd;
676
677 pos += scnprintf(buf + pos, bufsz - pos,
678 "flags: %#.2x\n", le16_to_cpu(cmd->flags));
679 pos += scnprintf(buf + pos, bufsz - pos,
680 "RX/TX timeout: %d/%d usec\n",
681 le32_to_cpu(cmd->rx_data_timeout),
682 le32_to_cpu(cmd->tx_data_timeout));
683 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
684 pos += scnprintf(buf + pos, bufsz - pos,
685 "sleep_interval[%d]: %d\n", i,
686 le32_to_cpu(cmd->sleep_interval[i]));
687
688 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
689}
690
691DEBUGFS_READ_WRITE_FILE_OPS(sram);
692DEBUGFS_READ_FILE_OPS(wowlan_sram);
693DEBUGFS_READ_FILE_OPS(nvm);
694DEBUGFS_READ_FILE_OPS(stations);
695DEBUGFS_READ_FILE_OPS(channels);
696DEBUGFS_READ_FILE_OPS(status);
697DEBUGFS_READ_WRITE_FILE_OPS(rx_handlers);
698DEBUGFS_READ_FILE_OPS(qos);
699DEBUGFS_READ_FILE_OPS(thermal_throttling);
700DEBUGFS_READ_WRITE_FILE_OPS(disable_ht40);
701DEBUGFS_READ_FILE_OPS(temperature);
702DEBUGFS_READ_WRITE_FILE_OPS(sleep_level_override);
703DEBUGFS_READ_FILE_OPS(current_sleep_command);
704
705static const char *fmt_value = " %-30s %10u\n";
706static const char *fmt_hex = " %-30s 0x%02X\n";
707static const char *fmt_table = " %-30s %10u %10u %10u %10u\n";
708static const char *fmt_header =
709 "%-32s current cumulative delta max\n";
710
711static int iwl_statistics_flag(struct iwl_priv *priv, char *buf, int bufsz)
712{
713 int p = 0;
714 u32 flag;
715
716 lockdep_assert_held(&priv->statistics.lock);
717
718 flag = le32_to_cpu(priv->statistics.flag);
719
720 p += scnprintf(buf + p, bufsz - p, "Statistics Flag(0x%X):\n", flag);
721 if (flag & UCODE_STATISTICS_CLEAR_MSK)
722 p += scnprintf(buf + p, bufsz - p,
723 "\tStatistics have been cleared\n");
724 p += scnprintf(buf + p, bufsz - p, "\tOperational Frequency: %s\n",
725 (flag & UCODE_STATISTICS_FREQUENCY_MSK)
726 ? "2.4 GHz" : "5.2 GHz");
727 p += scnprintf(buf + p, bufsz - p, "\tTGj Narrow Band: %s\n",
728 (flag & UCODE_STATISTICS_NARROW_BAND_MSK)
729 ? "enabled" : "disabled");
730
731 return p;
732}
733
734static ssize_t iwl_dbgfs_ucode_rx_stats_read(struct file *file,
735 char __user *user_buf,
736 size_t count, loff_t *ppos)
737{
738 struct iwl_priv *priv = file->private_data;
739 int pos = 0;
740 char *buf;
741 int bufsz = sizeof(struct statistics_rx_phy) * 40 +
742 sizeof(struct statistics_rx_non_phy) * 40 +
743 sizeof(struct statistics_rx_ht_phy) * 40 + 400;
744 ssize_t ret;
745 struct statistics_rx_phy *ofdm, *accum_ofdm, *delta_ofdm, *max_ofdm;
746 struct statistics_rx_phy *cck, *accum_cck, *delta_cck, *max_cck;
747 struct statistics_rx_non_phy *general, *accum_general;
748 struct statistics_rx_non_phy *delta_general, *max_general;
749 struct statistics_rx_ht_phy *ht, *accum_ht, *delta_ht, *max_ht;
750
751 if (!iwl_is_alive(priv))
752 return -EAGAIN;
753
754 buf = kzalloc(bufsz, GFP_KERNEL);
755 if (!buf)
756 return -ENOMEM;
757
758 /*
759 * the statistic information display here is based on
760 * the last statistics notification from uCode
761 * might not reflect the current uCode activity
762 */
763 spin_lock_bh(&priv->statistics.lock);
764 ofdm = &priv->statistics.rx_ofdm;
765 cck = &priv->statistics.rx_cck;
766 general = &priv->statistics.rx_non_phy;
767 ht = &priv->statistics.rx_ofdm_ht;
768 accum_ofdm = &priv->accum_stats.rx_ofdm;
769 accum_cck = &priv->accum_stats.rx_cck;
770 accum_general = &priv->accum_stats.rx_non_phy;
771 accum_ht = &priv->accum_stats.rx_ofdm_ht;
772 delta_ofdm = &priv->delta_stats.rx_ofdm;
773 delta_cck = &priv->delta_stats.rx_cck;
774 delta_general = &priv->delta_stats.rx_non_phy;
775 delta_ht = &priv->delta_stats.rx_ofdm_ht;
776 max_ofdm = &priv->max_delta_stats.rx_ofdm;
777 max_cck = &priv->max_delta_stats.rx_cck;
778 max_general = &priv->max_delta_stats.rx_non_phy;
779 max_ht = &priv->max_delta_stats.rx_ofdm_ht;
780
781 pos += iwl_statistics_flag(priv, buf, bufsz);
782 pos += scnprintf(buf + pos, bufsz - pos,
783 fmt_header, "Statistics_Rx - OFDM:");
784 pos += scnprintf(buf + pos, bufsz - pos,
785 fmt_table, "ina_cnt:",
786 le32_to_cpu(ofdm->ina_cnt),
787 accum_ofdm->ina_cnt,
788 delta_ofdm->ina_cnt, max_ofdm->ina_cnt);
789 pos += scnprintf(buf + pos, bufsz - pos,
790 fmt_table, "fina_cnt:",
791 le32_to_cpu(ofdm->fina_cnt), accum_ofdm->fina_cnt,
792 delta_ofdm->fina_cnt, max_ofdm->fina_cnt);
793 pos += scnprintf(buf + pos, bufsz - pos,
794 fmt_table, "plcp_err:",
795 le32_to_cpu(ofdm->plcp_err), accum_ofdm->plcp_err,
796 delta_ofdm->plcp_err, max_ofdm->plcp_err);
797 pos += scnprintf(buf + pos, bufsz - pos,
798 fmt_table, "crc32_err:",
799 le32_to_cpu(ofdm->crc32_err), accum_ofdm->crc32_err,
800 delta_ofdm->crc32_err, max_ofdm->crc32_err);
801 pos += scnprintf(buf + pos, bufsz - pos,
802 fmt_table, "overrun_err:",
803 le32_to_cpu(ofdm->overrun_err),
804 accum_ofdm->overrun_err, delta_ofdm->overrun_err,
805 max_ofdm->overrun_err);
806 pos += scnprintf(buf + pos, bufsz - pos,
807 fmt_table, "early_overrun_err:",
808 le32_to_cpu(ofdm->early_overrun_err),
809 accum_ofdm->early_overrun_err,
810 delta_ofdm->early_overrun_err,
811 max_ofdm->early_overrun_err);
812 pos += scnprintf(buf + pos, bufsz - pos,
813 fmt_table, "crc32_good:",
814 le32_to_cpu(ofdm->crc32_good),
815 accum_ofdm->crc32_good, delta_ofdm->crc32_good,
816 max_ofdm->crc32_good);
817 pos += scnprintf(buf + pos, bufsz - pos,
818 fmt_table, "false_alarm_cnt:",
819 le32_to_cpu(ofdm->false_alarm_cnt),
820 accum_ofdm->false_alarm_cnt,
821 delta_ofdm->false_alarm_cnt,
822 max_ofdm->false_alarm_cnt);
823 pos += scnprintf(buf + pos, bufsz - pos,
824 fmt_table, "fina_sync_err_cnt:",
825 le32_to_cpu(ofdm->fina_sync_err_cnt),
826 accum_ofdm->fina_sync_err_cnt,
827 delta_ofdm->fina_sync_err_cnt,
828 max_ofdm->fina_sync_err_cnt);
829 pos += scnprintf(buf + pos, bufsz - pos,
830 fmt_table, "sfd_timeout:",
831 le32_to_cpu(ofdm->sfd_timeout),
832 accum_ofdm->sfd_timeout, delta_ofdm->sfd_timeout,
833 max_ofdm->sfd_timeout);
834 pos += scnprintf(buf + pos, bufsz - pos,
835 fmt_table, "fina_timeout:",
836 le32_to_cpu(ofdm->fina_timeout),
837 accum_ofdm->fina_timeout, delta_ofdm->fina_timeout,
838 max_ofdm->fina_timeout);
839 pos += scnprintf(buf + pos, bufsz - pos,
840 fmt_table, "unresponded_rts:",
841 le32_to_cpu(ofdm->unresponded_rts),
842 accum_ofdm->unresponded_rts,
843 delta_ofdm->unresponded_rts,
844 max_ofdm->unresponded_rts);
845 pos += scnprintf(buf + pos, bufsz - pos,
846 fmt_table, "rxe_frame_lmt_ovrun:",
847 le32_to_cpu(ofdm->rxe_frame_limit_overrun),
848 accum_ofdm->rxe_frame_limit_overrun,
849 delta_ofdm->rxe_frame_limit_overrun,
850 max_ofdm->rxe_frame_limit_overrun);
851 pos += scnprintf(buf + pos, bufsz - pos,
852 fmt_table, "sent_ack_cnt:",
853 le32_to_cpu(ofdm->sent_ack_cnt),
854 accum_ofdm->sent_ack_cnt, delta_ofdm->sent_ack_cnt,
855 max_ofdm->sent_ack_cnt);
856 pos += scnprintf(buf + pos, bufsz - pos,
857 fmt_table, "sent_cts_cnt:",
858 le32_to_cpu(ofdm->sent_cts_cnt),
859 accum_ofdm->sent_cts_cnt, delta_ofdm->sent_cts_cnt,
860 max_ofdm->sent_cts_cnt);
861 pos += scnprintf(buf + pos, bufsz - pos,
862 fmt_table, "sent_ba_rsp_cnt:",
863 le32_to_cpu(ofdm->sent_ba_rsp_cnt),
864 accum_ofdm->sent_ba_rsp_cnt,
865 delta_ofdm->sent_ba_rsp_cnt,
866 max_ofdm->sent_ba_rsp_cnt);
867 pos += scnprintf(buf + pos, bufsz - pos,
868 fmt_table, "dsp_self_kill:",
869 le32_to_cpu(ofdm->dsp_self_kill),
870 accum_ofdm->dsp_self_kill,
871 delta_ofdm->dsp_self_kill,
872 max_ofdm->dsp_self_kill);
873 pos += scnprintf(buf + pos, bufsz - pos,
874 fmt_table, "mh_format_err:",
875 le32_to_cpu(ofdm->mh_format_err),
876 accum_ofdm->mh_format_err,
877 delta_ofdm->mh_format_err,
878 max_ofdm->mh_format_err);
879 pos += scnprintf(buf + pos, bufsz - pos,
880 fmt_table, "re_acq_main_rssi_sum:",
881 le32_to_cpu(ofdm->re_acq_main_rssi_sum),
882 accum_ofdm->re_acq_main_rssi_sum,
883 delta_ofdm->re_acq_main_rssi_sum,
884 max_ofdm->re_acq_main_rssi_sum);
885
886 pos += scnprintf(buf + pos, bufsz - pos,
887 fmt_header, "Statistics_Rx - CCK:");
888 pos += scnprintf(buf + pos, bufsz - pos,
889 fmt_table, "ina_cnt:",
890 le32_to_cpu(cck->ina_cnt), accum_cck->ina_cnt,
891 delta_cck->ina_cnt, max_cck->ina_cnt);
892 pos += scnprintf(buf + pos, bufsz - pos,
893 fmt_table, "fina_cnt:",
894 le32_to_cpu(cck->fina_cnt), accum_cck->fina_cnt,
895 delta_cck->fina_cnt, max_cck->fina_cnt);
896 pos += scnprintf(buf + pos, bufsz - pos,
897 fmt_table, "plcp_err:",
898 le32_to_cpu(cck->plcp_err), accum_cck->plcp_err,
899 delta_cck->plcp_err, max_cck->plcp_err);
900 pos += scnprintf(buf + pos, bufsz - pos,
901 fmt_table, "crc32_err:",
902 le32_to_cpu(cck->crc32_err), accum_cck->crc32_err,
903 delta_cck->crc32_err, max_cck->crc32_err);
904 pos += scnprintf(buf + pos, bufsz - pos,
905 fmt_table, "overrun_err:",
906 le32_to_cpu(cck->overrun_err),
907 accum_cck->overrun_err, delta_cck->overrun_err,
908 max_cck->overrun_err);
909 pos += scnprintf(buf + pos, bufsz - pos,
910 fmt_table, "early_overrun_err:",
911 le32_to_cpu(cck->early_overrun_err),
912 accum_cck->early_overrun_err,
913 delta_cck->early_overrun_err,
914 max_cck->early_overrun_err);
915 pos += scnprintf(buf + pos, bufsz - pos,
916 fmt_table, "crc32_good:",
917 le32_to_cpu(cck->crc32_good), accum_cck->crc32_good,
918 delta_cck->crc32_good, max_cck->crc32_good);
919 pos += scnprintf(buf + pos, bufsz - pos,
920 fmt_table, "false_alarm_cnt:",
921 le32_to_cpu(cck->false_alarm_cnt),
922 accum_cck->false_alarm_cnt,
923 delta_cck->false_alarm_cnt, max_cck->false_alarm_cnt);
924 pos += scnprintf(buf + pos, bufsz - pos,
925 fmt_table, "fina_sync_err_cnt:",
926 le32_to_cpu(cck->fina_sync_err_cnt),
927 accum_cck->fina_sync_err_cnt,
928 delta_cck->fina_sync_err_cnt,
929 max_cck->fina_sync_err_cnt);
930 pos += scnprintf(buf + pos, bufsz - pos,
931 fmt_table, "sfd_timeout:",
932 le32_to_cpu(cck->sfd_timeout),
933 accum_cck->sfd_timeout, delta_cck->sfd_timeout,
934 max_cck->sfd_timeout);
935 pos += scnprintf(buf + pos, bufsz - pos,
936 fmt_table, "fina_timeout:",
937 le32_to_cpu(cck->fina_timeout),
938 accum_cck->fina_timeout, delta_cck->fina_timeout,
939 max_cck->fina_timeout);
940 pos += scnprintf(buf + pos, bufsz - pos,
941 fmt_table, "unresponded_rts:",
942 le32_to_cpu(cck->unresponded_rts),
943 accum_cck->unresponded_rts, delta_cck->unresponded_rts,
944 max_cck->unresponded_rts);
945 pos += scnprintf(buf + pos, bufsz - pos,
946 fmt_table, "rxe_frame_lmt_ovrun:",
947 le32_to_cpu(cck->rxe_frame_limit_overrun),
948 accum_cck->rxe_frame_limit_overrun,
949 delta_cck->rxe_frame_limit_overrun,
950 max_cck->rxe_frame_limit_overrun);
951 pos += scnprintf(buf + pos, bufsz - pos,
952 fmt_table, "sent_ack_cnt:",
953 le32_to_cpu(cck->sent_ack_cnt),
954 accum_cck->sent_ack_cnt, delta_cck->sent_ack_cnt,
955 max_cck->sent_ack_cnt);
956 pos += scnprintf(buf + pos, bufsz - pos,
957 fmt_table, "sent_cts_cnt:",
958 le32_to_cpu(cck->sent_cts_cnt),
959 accum_cck->sent_cts_cnt, delta_cck->sent_cts_cnt,
960 max_cck->sent_cts_cnt);
961 pos += scnprintf(buf + pos, bufsz - pos,
962 fmt_table, "sent_ba_rsp_cnt:",
963 le32_to_cpu(cck->sent_ba_rsp_cnt),
964 accum_cck->sent_ba_rsp_cnt,
965 delta_cck->sent_ba_rsp_cnt,
966 max_cck->sent_ba_rsp_cnt);
967 pos += scnprintf(buf + pos, bufsz - pos,
968 fmt_table, "dsp_self_kill:",
969 le32_to_cpu(cck->dsp_self_kill),
970 accum_cck->dsp_self_kill, delta_cck->dsp_self_kill,
971 max_cck->dsp_self_kill);
972 pos += scnprintf(buf + pos, bufsz - pos,
973 fmt_table, "mh_format_err:",
974 le32_to_cpu(cck->mh_format_err),
975 accum_cck->mh_format_err, delta_cck->mh_format_err,
976 max_cck->mh_format_err);
977 pos += scnprintf(buf + pos, bufsz - pos,
978 fmt_table, "re_acq_main_rssi_sum:",
979 le32_to_cpu(cck->re_acq_main_rssi_sum),
980 accum_cck->re_acq_main_rssi_sum,
981 delta_cck->re_acq_main_rssi_sum,
982 max_cck->re_acq_main_rssi_sum);
983
984 pos += scnprintf(buf + pos, bufsz - pos,
985 fmt_header, "Statistics_Rx - GENERAL:");
986 pos += scnprintf(buf + pos, bufsz - pos,
987 fmt_table, "bogus_cts:",
988 le32_to_cpu(general->bogus_cts),
989 accum_general->bogus_cts, delta_general->bogus_cts,
990 max_general->bogus_cts);
991 pos += scnprintf(buf + pos, bufsz - pos,
992 fmt_table, "bogus_ack:",
993 le32_to_cpu(general->bogus_ack),
994 accum_general->bogus_ack, delta_general->bogus_ack,
995 max_general->bogus_ack);
996 pos += scnprintf(buf + pos, bufsz - pos,
997 fmt_table, "non_bssid_frames:",
998 le32_to_cpu(general->non_bssid_frames),
999 accum_general->non_bssid_frames,
1000 delta_general->non_bssid_frames,
1001 max_general->non_bssid_frames);
1002 pos += scnprintf(buf + pos, bufsz - pos,
1003 fmt_table, "filtered_frames:",
1004 le32_to_cpu(general->filtered_frames),
1005 accum_general->filtered_frames,
1006 delta_general->filtered_frames,
1007 max_general->filtered_frames);
1008 pos += scnprintf(buf + pos, bufsz - pos,
1009 fmt_table, "non_channel_beacons:",
1010 le32_to_cpu(general->non_channel_beacons),
1011 accum_general->non_channel_beacons,
1012 delta_general->non_channel_beacons,
1013 max_general->non_channel_beacons);
1014 pos += scnprintf(buf + pos, bufsz - pos,
1015 fmt_table, "channel_beacons:",
1016 le32_to_cpu(general->channel_beacons),
1017 accum_general->channel_beacons,
1018 delta_general->channel_beacons,
1019 max_general->channel_beacons);
1020 pos += scnprintf(buf + pos, bufsz - pos,
1021 fmt_table, "num_missed_bcon:",
1022 le32_to_cpu(general->num_missed_bcon),
1023 accum_general->num_missed_bcon,
1024 delta_general->num_missed_bcon,
1025 max_general->num_missed_bcon);
1026 pos += scnprintf(buf + pos, bufsz - pos,
1027 fmt_table, "adc_rx_saturation_time:",
1028 le32_to_cpu(general->adc_rx_saturation_time),
1029 accum_general->adc_rx_saturation_time,
1030 delta_general->adc_rx_saturation_time,
1031 max_general->adc_rx_saturation_time);
1032 pos += scnprintf(buf + pos, bufsz - pos,
1033 fmt_table, "ina_detect_search_tm:",
1034 le32_to_cpu(general->ina_detection_search_time),
1035 accum_general->ina_detection_search_time,
1036 delta_general->ina_detection_search_time,
1037 max_general->ina_detection_search_time);
1038 pos += scnprintf(buf + pos, bufsz - pos,
1039 fmt_table, "beacon_silence_rssi_a:",
1040 le32_to_cpu(general->beacon_silence_rssi_a),
1041 accum_general->beacon_silence_rssi_a,
1042 delta_general->beacon_silence_rssi_a,
1043 max_general->beacon_silence_rssi_a);
1044 pos += scnprintf(buf + pos, bufsz - pos,
1045 fmt_table, "beacon_silence_rssi_b:",
1046 le32_to_cpu(general->beacon_silence_rssi_b),
1047 accum_general->beacon_silence_rssi_b,
1048 delta_general->beacon_silence_rssi_b,
1049 max_general->beacon_silence_rssi_b);
1050 pos += scnprintf(buf + pos, bufsz - pos,
1051 fmt_table, "beacon_silence_rssi_c:",
1052 le32_to_cpu(general->beacon_silence_rssi_c),
1053 accum_general->beacon_silence_rssi_c,
1054 delta_general->beacon_silence_rssi_c,
1055 max_general->beacon_silence_rssi_c);
1056 pos += scnprintf(buf + pos, bufsz - pos,
1057 fmt_table, "interference_data_flag:",
1058 le32_to_cpu(general->interference_data_flag),
1059 accum_general->interference_data_flag,
1060 delta_general->interference_data_flag,
1061 max_general->interference_data_flag);
1062 pos += scnprintf(buf + pos, bufsz - pos,
1063 fmt_table, "channel_load:",
1064 le32_to_cpu(general->channel_load),
1065 accum_general->channel_load,
1066 delta_general->channel_load,
1067 max_general->channel_load);
1068 pos += scnprintf(buf + pos, bufsz - pos,
1069 fmt_table, "dsp_false_alarms:",
1070 le32_to_cpu(general->dsp_false_alarms),
1071 accum_general->dsp_false_alarms,
1072 delta_general->dsp_false_alarms,
1073 max_general->dsp_false_alarms);
1074 pos += scnprintf(buf + pos, bufsz - pos,
1075 fmt_table, "beacon_rssi_a:",
1076 le32_to_cpu(general->beacon_rssi_a),
1077 accum_general->beacon_rssi_a,
1078 delta_general->beacon_rssi_a,
1079 max_general->beacon_rssi_a);
1080 pos += scnprintf(buf + pos, bufsz - pos,
1081 fmt_table, "beacon_rssi_b:",
1082 le32_to_cpu(general->beacon_rssi_b),
1083 accum_general->beacon_rssi_b,
1084 delta_general->beacon_rssi_b,
1085 max_general->beacon_rssi_b);
1086 pos += scnprintf(buf + pos, bufsz - pos,
1087 fmt_table, "beacon_rssi_c:",
1088 le32_to_cpu(general->beacon_rssi_c),
1089 accum_general->beacon_rssi_c,
1090 delta_general->beacon_rssi_c,
1091 max_general->beacon_rssi_c);
1092 pos += scnprintf(buf + pos, bufsz - pos,
1093 fmt_table, "beacon_energy_a:",
1094 le32_to_cpu(general->beacon_energy_a),
1095 accum_general->beacon_energy_a,
1096 delta_general->beacon_energy_a,
1097 max_general->beacon_energy_a);
1098 pos += scnprintf(buf + pos, bufsz - pos,
1099 fmt_table, "beacon_energy_b:",
1100 le32_to_cpu(general->beacon_energy_b),
1101 accum_general->beacon_energy_b,
1102 delta_general->beacon_energy_b,
1103 max_general->beacon_energy_b);
1104 pos += scnprintf(buf + pos, bufsz - pos,
1105 fmt_table, "beacon_energy_c:",
1106 le32_to_cpu(general->beacon_energy_c),
1107 accum_general->beacon_energy_c,
1108 delta_general->beacon_energy_c,
1109 max_general->beacon_energy_c);
1110
1111 pos += scnprintf(buf + pos, bufsz - pos,
1112 fmt_header, "Statistics_Rx - OFDM_HT:");
1113 pos += scnprintf(buf + pos, bufsz - pos,
1114 fmt_table, "plcp_err:",
1115 le32_to_cpu(ht->plcp_err), accum_ht->plcp_err,
1116 delta_ht->plcp_err, max_ht->plcp_err);
1117 pos += scnprintf(buf + pos, bufsz - pos,
1118 fmt_table, "overrun_err:",
1119 le32_to_cpu(ht->overrun_err), accum_ht->overrun_err,
1120 delta_ht->overrun_err, max_ht->overrun_err);
1121 pos += scnprintf(buf + pos, bufsz - pos,
1122 fmt_table, "early_overrun_err:",
1123 le32_to_cpu(ht->early_overrun_err),
1124 accum_ht->early_overrun_err,
1125 delta_ht->early_overrun_err,
1126 max_ht->early_overrun_err);
1127 pos += scnprintf(buf + pos, bufsz - pos,
1128 fmt_table, "crc32_good:",
1129 le32_to_cpu(ht->crc32_good), accum_ht->crc32_good,
1130 delta_ht->crc32_good, max_ht->crc32_good);
1131 pos += scnprintf(buf + pos, bufsz - pos,
1132 fmt_table, "crc32_err:",
1133 le32_to_cpu(ht->crc32_err), accum_ht->crc32_err,
1134 delta_ht->crc32_err, max_ht->crc32_err);
1135 pos += scnprintf(buf + pos, bufsz - pos,
1136 fmt_table, "mh_format_err:",
1137 le32_to_cpu(ht->mh_format_err),
1138 accum_ht->mh_format_err,
1139 delta_ht->mh_format_err, max_ht->mh_format_err);
1140 pos += scnprintf(buf + pos, bufsz - pos,
1141 fmt_table, "agg_crc32_good:",
1142 le32_to_cpu(ht->agg_crc32_good),
1143 accum_ht->agg_crc32_good,
1144 delta_ht->agg_crc32_good, max_ht->agg_crc32_good);
1145 pos += scnprintf(buf + pos, bufsz - pos,
1146 fmt_table, "agg_mpdu_cnt:",
1147 le32_to_cpu(ht->agg_mpdu_cnt),
1148 accum_ht->agg_mpdu_cnt,
1149 delta_ht->agg_mpdu_cnt, max_ht->agg_mpdu_cnt);
1150 pos += scnprintf(buf + pos, bufsz - pos,
1151 fmt_table, "agg_cnt:",
1152 le32_to_cpu(ht->agg_cnt), accum_ht->agg_cnt,
1153 delta_ht->agg_cnt, max_ht->agg_cnt);
1154 pos += scnprintf(buf + pos, bufsz - pos,
1155 fmt_table, "unsupport_mcs:",
1156 le32_to_cpu(ht->unsupport_mcs),
1157 accum_ht->unsupport_mcs,
1158 delta_ht->unsupport_mcs, max_ht->unsupport_mcs);
1159
1160 spin_unlock_bh(&priv->statistics.lock);
1161
1162 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1163 kfree(buf);
1164 return ret;
1165}
1166
1167static ssize_t iwl_dbgfs_ucode_tx_stats_read(struct file *file,
1168 char __user *user_buf,
1169 size_t count, loff_t *ppos)
1170{
1171 struct iwl_priv *priv = file->private_data;
1172 int pos = 0;
1173 char *buf;
1174 int bufsz = (sizeof(struct statistics_tx) * 48) + 250;
1175 ssize_t ret;
1176 struct statistics_tx *tx, *accum_tx, *delta_tx, *max_tx;
1177
1178 if (!iwl_is_alive(priv))
1179 return -EAGAIN;
1180
1181 buf = kzalloc(bufsz, GFP_KERNEL);
1182 if (!buf)
1183 return -ENOMEM;
1184
1185 /* the statistic information display here is based on
1186 * the last statistics notification from uCode
1187 * might not reflect the current uCode activity
1188 */
1189 spin_lock_bh(&priv->statistics.lock);
1190
1191 tx = &priv->statistics.tx;
1192 accum_tx = &priv->accum_stats.tx;
1193 delta_tx = &priv->delta_stats.tx;
1194 max_tx = &priv->max_delta_stats.tx;
1195
1196 pos += iwl_statistics_flag(priv, buf, bufsz);
1197 pos += scnprintf(buf + pos, bufsz - pos,
1198 fmt_header, "Statistics_Tx:");
1199 pos += scnprintf(buf + pos, bufsz - pos,
1200 fmt_table, "preamble:",
1201 le32_to_cpu(tx->preamble_cnt),
1202 accum_tx->preamble_cnt,
1203 delta_tx->preamble_cnt, max_tx->preamble_cnt);
1204 pos += scnprintf(buf + pos, bufsz - pos,
1205 fmt_table, "rx_detected_cnt:",
1206 le32_to_cpu(tx->rx_detected_cnt),
1207 accum_tx->rx_detected_cnt,
1208 delta_tx->rx_detected_cnt, max_tx->rx_detected_cnt);
1209 pos += scnprintf(buf + pos, bufsz - pos,
1210 fmt_table, "bt_prio_defer_cnt:",
1211 le32_to_cpu(tx->bt_prio_defer_cnt),
1212 accum_tx->bt_prio_defer_cnt,
1213 delta_tx->bt_prio_defer_cnt,
1214 max_tx->bt_prio_defer_cnt);
1215 pos += scnprintf(buf + pos, bufsz - pos,
1216 fmt_table, "bt_prio_kill_cnt:",
1217 le32_to_cpu(tx->bt_prio_kill_cnt),
1218 accum_tx->bt_prio_kill_cnt,
1219 delta_tx->bt_prio_kill_cnt,
1220 max_tx->bt_prio_kill_cnt);
1221 pos += scnprintf(buf + pos, bufsz - pos,
1222 fmt_table, "few_bytes_cnt:",
1223 le32_to_cpu(tx->few_bytes_cnt),
1224 accum_tx->few_bytes_cnt,
1225 delta_tx->few_bytes_cnt, max_tx->few_bytes_cnt);
1226 pos += scnprintf(buf + pos, bufsz - pos,
1227 fmt_table, "cts_timeout:",
1228 le32_to_cpu(tx->cts_timeout), accum_tx->cts_timeout,
1229 delta_tx->cts_timeout, max_tx->cts_timeout);
1230 pos += scnprintf(buf + pos, bufsz - pos,
1231 fmt_table, "ack_timeout:",
1232 le32_to_cpu(tx->ack_timeout),
1233 accum_tx->ack_timeout,
1234 delta_tx->ack_timeout, max_tx->ack_timeout);
1235 pos += scnprintf(buf + pos, bufsz - pos,
1236 fmt_table, "expected_ack_cnt:",
1237 le32_to_cpu(tx->expected_ack_cnt),
1238 accum_tx->expected_ack_cnt,
1239 delta_tx->expected_ack_cnt,
1240 max_tx->expected_ack_cnt);
1241 pos += scnprintf(buf + pos, bufsz - pos,
1242 fmt_table, "actual_ack_cnt:",
1243 le32_to_cpu(tx->actual_ack_cnt),
1244 accum_tx->actual_ack_cnt,
1245 delta_tx->actual_ack_cnt,
1246 max_tx->actual_ack_cnt);
1247 pos += scnprintf(buf + pos, bufsz - pos,
1248 fmt_table, "dump_msdu_cnt:",
1249 le32_to_cpu(tx->dump_msdu_cnt),
1250 accum_tx->dump_msdu_cnt,
1251 delta_tx->dump_msdu_cnt,
1252 max_tx->dump_msdu_cnt);
1253 pos += scnprintf(buf + pos, bufsz - pos,
1254 fmt_table, "abort_nxt_frame_mismatch:",
1255 le32_to_cpu(tx->burst_abort_next_frame_mismatch_cnt),
1256 accum_tx->burst_abort_next_frame_mismatch_cnt,
1257 delta_tx->burst_abort_next_frame_mismatch_cnt,
1258 max_tx->burst_abort_next_frame_mismatch_cnt);
1259 pos += scnprintf(buf + pos, bufsz - pos,
1260 fmt_table, "abort_missing_nxt_frame:",
1261 le32_to_cpu(tx->burst_abort_missing_next_frame_cnt),
1262 accum_tx->burst_abort_missing_next_frame_cnt,
1263 delta_tx->burst_abort_missing_next_frame_cnt,
1264 max_tx->burst_abort_missing_next_frame_cnt);
1265 pos += scnprintf(buf + pos, bufsz - pos,
1266 fmt_table, "cts_timeout_collision:",
1267 le32_to_cpu(tx->cts_timeout_collision),
1268 accum_tx->cts_timeout_collision,
1269 delta_tx->cts_timeout_collision,
1270 max_tx->cts_timeout_collision);
1271 pos += scnprintf(buf + pos, bufsz - pos,
1272 fmt_table, "ack_ba_timeout_collision:",
1273 le32_to_cpu(tx->ack_or_ba_timeout_collision),
1274 accum_tx->ack_or_ba_timeout_collision,
1275 delta_tx->ack_or_ba_timeout_collision,
1276 max_tx->ack_or_ba_timeout_collision);
1277 pos += scnprintf(buf + pos, bufsz - pos,
1278 fmt_table, "agg ba_timeout:",
1279 le32_to_cpu(tx->agg.ba_timeout),
1280 accum_tx->agg.ba_timeout,
1281 delta_tx->agg.ba_timeout,
1282 max_tx->agg.ba_timeout);
1283 pos += scnprintf(buf + pos, bufsz - pos,
1284 fmt_table, "agg ba_resched_frames:",
1285 le32_to_cpu(tx->agg.ba_reschedule_frames),
1286 accum_tx->agg.ba_reschedule_frames,
1287 delta_tx->agg.ba_reschedule_frames,
1288 max_tx->agg.ba_reschedule_frames);
1289 pos += scnprintf(buf + pos, bufsz - pos,
1290 fmt_table, "agg scd_query_agg_frame:",
1291 le32_to_cpu(tx->agg.scd_query_agg_frame_cnt),
1292 accum_tx->agg.scd_query_agg_frame_cnt,
1293 delta_tx->agg.scd_query_agg_frame_cnt,
1294 max_tx->agg.scd_query_agg_frame_cnt);
1295 pos += scnprintf(buf + pos, bufsz - pos,
1296 fmt_table, "agg scd_query_no_agg:",
1297 le32_to_cpu(tx->agg.scd_query_no_agg),
1298 accum_tx->agg.scd_query_no_agg,
1299 delta_tx->agg.scd_query_no_agg,
1300 max_tx->agg.scd_query_no_agg);
1301 pos += scnprintf(buf + pos, bufsz - pos,
1302 fmt_table, "agg scd_query_agg:",
1303 le32_to_cpu(tx->agg.scd_query_agg),
1304 accum_tx->agg.scd_query_agg,
1305 delta_tx->agg.scd_query_agg,
1306 max_tx->agg.scd_query_agg);
1307 pos += scnprintf(buf + pos, bufsz - pos,
1308 fmt_table, "agg scd_query_mismatch:",
1309 le32_to_cpu(tx->agg.scd_query_mismatch),
1310 accum_tx->agg.scd_query_mismatch,
1311 delta_tx->agg.scd_query_mismatch,
1312 max_tx->agg.scd_query_mismatch);
1313 pos += scnprintf(buf + pos, bufsz - pos,
1314 fmt_table, "agg frame_not_ready:",
1315 le32_to_cpu(tx->agg.frame_not_ready),
1316 accum_tx->agg.frame_not_ready,
1317 delta_tx->agg.frame_not_ready,
1318 max_tx->agg.frame_not_ready);
1319 pos += scnprintf(buf + pos, bufsz - pos,
1320 fmt_table, "agg underrun:",
1321 le32_to_cpu(tx->agg.underrun),
1322 accum_tx->agg.underrun,
1323 delta_tx->agg.underrun, max_tx->agg.underrun);
1324 pos += scnprintf(buf + pos, bufsz - pos,
1325 fmt_table, "agg bt_prio_kill:",
1326 le32_to_cpu(tx->agg.bt_prio_kill),
1327 accum_tx->agg.bt_prio_kill,
1328 delta_tx->agg.bt_prio_kill,
1329 max_tx->agg.bt_prio_kill);
1330 pos += scnprintf(buf + pos, bufsz - pos,
1331 fmt_table, "agg rx_ba_rsp_cnt:",
1332 le32_to_cpu(tx->agg.rx_ba_rsp_cnt),
1333 accum_tx->agg.rx_ba_rsp_cnt,
1334 delta_tx->agg.rx_ba_rsp_cnt,
1335 max_tx->agg.rx_ba_rsp_cnt);
1336
1337 if (tx->tx_power.ant_a || tx->tx_power.ant_b || tx->tx_power.ant_c) {
1338 pos += scnprintf(buf + pos, bufsz - pos,
1339 "tx power: (1/2 dB step)\n");
1340 if ((priv->hw_params.valid_tx_ant & ANT_A) &&
1341 tx->tx_power.ant_a)
1342 pos += scnprintf(buf + pos, bufsz - pos,
1343 fmt_hex, "antenna A:",
1344 tx->tx_power.ant_a);
1345 if ((priv->hw_params.valid_tx_ant & ANT_B) &&
1346 tx->tx_power.ant_b)
1347 pos += scnprintf(buf + pos, bufsz - pos,
1348 fmt_hex, "antenna B:",
1349 tx->tx_power.ant_b);
1350 if ((priv->hw_params.valid_tx_ant & ANT_C) &&
1351 tx->tx_power.ant_c)
1352 pos += scnprintf(buf + pos, bufsz - pos,
1353 fmt_hex, "antenna C:",
1354 tx->tx_power.ant_c);
1355 }
1356
1357 spin_unlock_bh(&priv->statistics.lock);
1358
1359 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1360 kfree(buf);
1361 return ret;
1362}
1363
1364static ssize_t iwl_dbgfs_ucode_general_stats_read(struct file *file,
1365 char __user *user_buf,
1366 size_t count, loff_t *ppos)
1367{
1368 struct iwl_priv *priv = file->private_data;
1369 int pos = 0;
1370 char *buf;
1371 int bufsz = sizeof(struct statistics_general) * 10 + 300;
1372 ssize_t ret;
1373 struct statistics_general_common *general, *accum_general;
1374 struct statistics_general_common *delta_general, *max_general;
1375 struct statistics_dbg *dbg, *accum_dbg, *delta_dbg, *max_dbg;
1376 struct statistics_div *div, *accum_div, *delta_div, *max_div;
1377
1378 if (!iwl_is_alive(priv))
1379 return -EAGAIN;
1380
1381 buf = kzalloc(bufsz, GFP_KERNEL);
1382 if (!buf)
1383 return -ENOMEM;
1384
1385 /* the statistic information display here is based on
1386 * the last statistics notification from uCode
1387 * might not reflect the current uCode activity
1388 */
1389
1390 spin_lock_bh(&priv->statistics.lock);
1391
1392 general = &priv->statistics.common;
1393 dbg = &priv->statistics.common.dbg;
1394 div = &priv->statistics.common.div;
1395 accum_general = &priv->accum_stats.common;
1396 accum_dbg = &priv->accum_stats.common.dbg;
1397 accum_div = &priv->accum_stats.common.div;
1398 delta_general = &priv->delta_stats.common;
1399 max_general = &priv->max_delta_stats.common;
1400 delta_dbg = &priv->delta_stats.common.dbg;
1401 max_dbg = &priv->max_delta_stats.common.dbg;
1402 delta_div = &priv->delta_stats.common.div;
1403 max_div = &priv->max_delta_stats.common.div;
1404
1405 pos += iwl_statistics_flag(priv, buf, bufsz);
1406 pos += scnprintf(buf + pos, bufsz - pos,
1407 fmt_header, "Statistics_General:");
1408 pos += scnprintf(buf + pos, bufsz - pos,
1409 fmt_value, "temperature:",
1410 le32_to_cpu(general->temperature));
1411 pos += scnprintf(buf + pos, bufsz - pos,
1412 fmt_value, "temperature_m:",
1413 le32_to_cpu(general->temperature_m));
1414 pos += scnprintf(buf + pos, bufsz - pos,
1415 fmt_value, "ttl_timestamp:",
1416 le32_to_cpu(general->ttl_timestamp));
1417 pos += scnprintf(buf + pos, bufsz - pos,
1418 fmt_table, "burst_check:",
1419 le32_to_cpu(dbg->burst_check),
1420 accum_dbg->burst_check,
1421 delta_dbg->burst_check, max_dbg->burst_check);
1422 pos += scnprintf(buf + pos, bufsz - pos,
1423 fmt_table, "burst_count:",
1424 le32_to_cpu(dbg->burst_count),
1425 accum_dbg->burst_count,
1426 delta_dbg->burst_count, max_dbg->burst_count);
1427 pos += scnprintf(buf + pos, bufsz - pos,
1428 fmt_table, "wait_for_silence_timeout_count:",
1429 le32_to_cpu(dbg->wait_for_silence_timeout_cnt),
1430 accum_dbg->wait_for_silence_timeout_cnt,
1431 delta_dbg->wait_for_silence_timeout_cnt,
1432 max_dbg->wait_for_silence_timeout_cnt);
1433 pos += scnprintf(buf + pos, bufsz - pos,
1434 fmt_table, "sleep_time:",
1435 le32_to_cpu(general->sleep_time),
1436 accum_general->sleep_time,
1437 delta_general->sleep_time, max_general->sleep_time);
1438 pos += scnprintf(buf + pos, bufsz - pos,
1439 fmt_table, "slots_out:",
1440 le32_to_cpu(general->slots_out),
1441 accum_general->slots_out,
1442 delta_general->slots_out, max_general->slots_out);
1443 pos += scnprintf(buf + pos, bufsz - pos,
1444 fmt_table, "slots_idle:",
1445 le32_to_cpu(general->slots_idle),
1446 accum_general->slots_idle,
1447 delta_general->slots_idle, max_general->slots_idle);
1448 pos += scnprintf(buf + pos, bufsz - pos,
1449 fmt_table, "tx_on_a:",
1450 le32_to_cpu(div->tx_on_a), accum_div->tx_on_a,
1451 delta_div->tx_on_a, max_div->tx_on_a);
1452 pos += scnprintf(buf + pos, bufsz - pos,
1453 fmt_table, "tx_on_b:",
1454 le32_to_cpu(div->tx_on_b), accum_div->tx_on_b,
1455 delta_div->tx_on_b, max_div->tx_on_b);
1456 pos += scnprintf(buf + pos, bufsz - pos,
1457 fmt_table, "exec_time:",
1458 le32_to_cpu(div->exec_time), accum_div->exec_time,
1459 delta_div->exec_time, max_div->exec_time);
1460 pos += scnprintf(buf + pos, bufsz - pos,
1461 fmt_table, "probe_time:",
1462 le32_to_cpu(div->probe_time), accum_div->probe_time,
1463 delta_div->probe_time, max_div->probe_time);
1464 pos += scnprintf(buf + pos, bufsz - pos,
1465 fmt_table, "rx_enable_counter:",
1466 le32_to_cpu(general->rx_enable_counter),
1467 accum_general->rx_enable_counter,
1468 delta_general->rx_enable_counter,
1469 max_general->rx_enable_counter);
1470 pos += scnprintf(buf + pos, bufsz - pos,
1471 fmt_table, "num_of_sos_states:",
1472 le32_to_cpu(general->num_of_sos_states),
1473 accum_general->num_of_sos_states,
1474 delta_general->num_of_sos_states,
1475 max_general->num_of_sos_states);
1476
1477 spin_unlock_bh(&priv->statistics.lock);
1478
1479 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1480 kfree(buf);
1481 return ret;
1482}
1483
1484static ssize_t iwl_dbgfs_ucode_bt_stats_read(struct file *file,
1485 char __user *user_buf,
1486 size_t count, loff_t *ppos)
1487{
1488 struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
1489 int pos = 0;
1490 char *buf;
1491 int bufsz = (sizeof(struct statistics_bt_activity) * 24) + 200;
1492 ssize_t ret;
1493 struct statistics_bt_activity *bt, *accum_bt;
1494
1495 if (!iwl_is_alive(priv))
1496 return -EAGAIN;
1497
1498 if (!priv->bt_enable_flag)
1499 return -EINVAL;
1500
1501 /* make request to uCode to retrieve statistics information */
1502 mutex_lock(&priv->mutex);
1503 ret = iwl_send_statistics_request(priv, CMD_SYNC, false);
1504 mutex_unlock(&priv->mutex);
1505
1506 if (ret)
1507 return -EAGAIN;
1508 buf = kzalloc(bufsz, GFP_KERNEL);
1509 if (!buf)
1510 return -ENOMEM;
1511
1512 /*
1513 * the statistic information display here is based on
1514 * the last statistics notification from uCode
1515 * might not reflect the current uCode activity
1516 */
1517
1518 spin_lock_bh(&priv->statistics.lock);
1519
1520 bt = &priv->statistics.bt_activity;
1521 accum_bt = &priv->accum_stats.bt_activity;
1522
1523 pos += iwl_statistics_flag(priv, buf, bufsz);
1524 pos += scnprintf(buf + pos, bufsz - pos, "Statistics_BT:\n");
1525 pos += scnprintf(buf + pos, bufsz - pos,
1526 "\t\t\tcurrent\t\t\taccumulative\n");
1527 pos += scnprintf(buf + pos, bufsz - pos,
1528 "hi_priority_tx_req_cnt:\t\t%u\t\t\t%u\n",
1529 le32_to_cpu(bt->hi_priority_tx_req_cnt),
1530 accum_bt->hi_priority_tx_req_cnt);
1531 pos += scnprintf(buf + pos, bufsz - pos,
1532 "hi_priority_tx_denied_cnt:\t%u\t\t\t%u\n",
1533 le32_to_cpu(bt->hi_priority_tx_denied_cnt),
1534 accum_bt->hi_priority_tx_denied_cnt);
1535 pos += scnprintf(buf + pos, bufsz - pos,
1536 "lo_priority_tx_req_cnt:\t\t%u\t\t\t%u\n",
1537 le32_to_cpu(bt->lo_priority_tx_req_cnt),
1538 accum_bt->lo_priority_tx_req_cnt);
1539 pos += scnprintf(buf + pos, bufsz - pos,
1540 "lo_priority_tx_denied_cnt:\t%u\t\t\t%u\n",
1541 le32_to_cpu(bt->lo_priority_tx_denied_cnt),
1542 accum_bt->lo_priority_tx_denied_cnt);
1543 pos += scnprintf(buf + pos, bufsz - pos,
1544 "hi_priority_rx_req_cnt:\t\t%u\t\t\t%u\n",
1545 le32_to_cpu(bt->hi_priority_rx_req_cnt),
1546 accum_bt->hi_priority_rx_req_cnt);
1547 pos += scnprintf(buf + pos, bufsz - pos,
1548 "hi_priority_rx_denied_cnt:\t%u\t\t\t%u\n",
1549 le32_to_cpu(bt->hi_priority_rx_denied_cnt),
1550 accum_bt->hi_priority_rx_denied_cnt);
1551 pos += scnprintf(buf + pos, bufsz - pos,
1552 "lo_priority_rx_req_cnt:\t\t%u\t\t\t%u\n",
1553 le32_to_cpu(bt->lo_priority_rx_req_cnt),
1554 accum_bt->lo_priority_rx_req_cnt);
1555 pos += scnprintf(buf + pos, bufsz - pos,
1556 "lo_priority_rx_denied_cnt:\t%u\t\t\t%u\n",
1557 le32_to_cpu(bt->lo_priority_rx_denied_cnt),
1558 accum_bt->lo_priority_rx_denied_cnt);
1559
1560 pos += scnprintf(buf + pos, bufsz - pos,
1561 "(rx)num_bt_kills:\t\t%u\t\t\t%u\n",
1562 le32_to_cpu(priv->statistics.num_bt_kills),
1563 priv->statistics.accum_num_bt_kills);
1564
1565 spin_unlock_bh(&priv->statistics.lock);
1566
1567 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1568 kfree(buf);
1569 return ret;
1570}
1571
1572static ssize_t iwl_dbgfs_reply_tx_error_read(struct file *file,
1573 char __user *user_buf,
1574 size_t count, loff_t *ppos)
1575{
1576 struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
1577 int pos = 0;
1578 char *buf;
1579 int bufsz = (sizeof(struct reply_tx_error_statistics) * 24) +
1580 (sizeof(struct reply_agg_tx_error_statistics) * 24) + 200;
1581 ssize_t ret;
1582
1583 if (!iwl_is_alive(priv))
1584 return -EAGAIN;
1585
1586 buf = kzalloc(bufsz, GFP_KERNEL);
1587 if (!buf)
1588 return -ENOMEM;
1589
1590 pos += scnprintf(buf + pos, bufsz - pos, "Statistics_TX_Error:\n");
1591 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t\t%u\n",
1592 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_DELAY),
1593 priv->reply_tx_stats.pp_delay);
1594 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1595 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_FEW_BYTES),
1596 priv->reply_tx_stats.pp_few_bytes);
1597 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1598 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_BT_PRIO),
1599 priv->reply_tx_stats.pp_bt_prio);
1600 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1601 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_QUIET_PERIOD),
1602 priv->reply_tx_stats.pp_quiet_period);
1603 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1604 iwl_get_tx_fail_reason(TX_STATUS_POSTPONE_CALC_TTAK),
1605 priv->reply_tx_stats.pp_calc_ttak);
1606 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
1607 iwl_get_tx_fail_reason(
1608 TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY),
1609 priv->reply_tx_stats.int_crossed_retry);
1610 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1611 iwl_get_tx_fail_reason(TX_STATUS_FAIL_SHORT_LIMIT),
1612 priv->reply_tx_stats.short_limit);
1613 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1614 iwl_get_tx_fail_reason(TX_STATUS_FAIL_LONG_LIMIT),
1615 priv->reply_tx_stats.long_limit);
1616 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1617 iwl_get_tx_fail_reason(TX_STATUS_FAIL_FIFO_UNDERRUN),
1618 priv->reply_tx_stats.fifo_underrun);
1619 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1620 iwl_get_tx_fail_reason(TX_STATUS_FAIL_DRAIN_FLOW),
1621 priv->reply_tx_stats.drain_flow);
1622 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1623 iwl_get_tx_fail_reason(TX_STATUS_FAIL_RFKILL_FLUSH),
1624 priv->reply_tx_stats.rfkill_flush);
1625 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1626 iwl_get_tx_fail_reason(TX_STATUS_FAIL_LIFE_EXPIRE),
1627 priv->reply_tx_stats.life_expire);
1628 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1629 iwl_get_tx_fail_reason(TX_STATUS_FAIL_DEST_PS),
1630 priv->reply_tx_stats.dest_ps);
1631 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1632 iwl_get_tx_fail_reason(TX_STATUS_FAIL_HOST_ABORTED),
1633 priv->reply_tx_stats.host_abort);
1634 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1635 iwl_get_tx_fail_reason(TX_STATUS_FAIL_BT_RETRY),
1636 priv->reply_tx_stats.pp_delay);
1637 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1638 iwl_get_tx_fail_reason(TX_STATUS_FAIL_STA_INVALID),
1639 priv->reply_tx_stats.sta_invalid);
1640 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1641 iwl_get_tx_fail_reason(TX_STATUS_FAIL_FRAG_DROPPED),
1642 priv->reply_tx_stats.frag_drop);
1643 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1644 iwl_get_tx_fail_reason(TX_STATUS_FAIL_TID_DISABLE),
1645 priv->reply_tx_stats.tid_disable);
1646 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1647 iwl_get_tx_fail_reason(TX_STATUS_FAIL_FIFO_FLUSHED),
1648 priv->reply_tx_stats.fifo_flush);
1649 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
1650 iwl_get_tx_fail_reason(
1651 TX_STATUS_FAIL_INSUFFICIENT_CF_POLL),
1652 priv->reply_tx_stats.insuff_cf_poll);
1653 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1654 iwl_get_tx_fail_reason(TX_STATUS_FAIL_PASSIVE_NO_RX),
1655 priv->reply_tx_stats.fail_hw_drop);
1656 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
1657 iwl_get_tx_fail_reason(
1658 TX_STATUS_FAIL_NO_BEACON_ON_RADAR),
1659 priv->reply_tx_stats.sta_color_mismatch);
1660 pos += scnprintf(buf + pos, bufsz - pos, "UNKNOWN:\t\t\t%u\n",
1661 priv->reply_tx_stats.unknown);
1662
1663 pos += scnprintf(buf + pos, bufsz - pos,
1664 "\nStatistics_Agg_TX_Error:\n");
1665
1666 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1667 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_UNDERRUN_MSK),
1668 priv->reply_agg_tx_stats.underrun);
1669 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1670 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_BT_PRIO_MSK),
1671 priv->reply_agg_tx_stats.bt_prio);
1672 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1673 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_FEW_BYTES_MSK),
1674 priv->reply_agg_tx_stats.few_bytes);
1675 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1676 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_ABORT_MSK),
1677 priv->reply_agg_tx_stats.abort);
1678 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
1679 iwl_get_agg_tx_fail_reason(
1680 AGG_TX_STATE_LAST_SENT_TTL_MSK),
1681 priv->reply_agg_tx_stats.last_sent_ttl);
1682 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
1683 iwl_get_agg_tx_fail_reason(
1684 AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK),
1685 priv->reply_agg_tx_stats.last_sent_try);
1686 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
1687 iwl_get_agg_tx_fail_reason(
1688 AGG_TX_STATE_LAST_SENT_BT_KILL_MSK),
1689 priv->reply_agg_tx_stats.last_sent_bt_kill);
1690 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1691 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_SCD_QUERY_MSK),
1692 priv->reply_agg_tx_stats.scd_query);
1693 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t%u\n",
1694 iwl_get_agg_tx_fail_reason(
1695 AGG_TX_STATE_TEST_BAD_CRC32_MSK),
1696 priv->reply_agg_tx_stats.bad_crc32);
1697 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1698 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_RESPONSE_MSK),
1699 priv->reply_agg_tx_stats.response);
1700 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1701 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_DUMP_TX_MSK),
1702 priv->reply_agg_tx_stats.dump_tx);
1703 pos += scnprintf(buf + pos, bufsz - pos, "%s:\t\t\t%u\n",
1704 iwl_get_agg_tx_fail_reason(AGG_TX_STATE_DELAY_TX_MSK),
1705 priv->reply_agg_tx_stats.delay_tx);
1706 pos += scnprintf(buf + pos, bufsz - pos, "UNKNOWN:\t\t\t%u\n",
1707 priv->reply_agg_tx_stats.unknown);
1708
1709 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1710 kfree(buf);
1711 return ret;
1712}
1713
1714static ssize_t iwl_dbgfs_sensitivity_read(struct file *file,
1715 char __user *user_buf,
1716 size_t count, loff_t *ppos) {
1717
1718 struct iwl_priv *priv = file->private_data;
1719 int pos = 0;
1720 int cnt = 0;
1721 char *buf;
1722 int bufsz = sizeof(struct iwl_sensitivity_data) * 4 + 100;
1723 ssize_t ret;
1724 struct iwl_sensitivity_data *data;
1725
1726 data = &priv->sensitivity_data;
1727 buf = kzalloc(bufsz, GFP_KERNEL);
1728 if (!buf)
1729 return -ENOMEM;
1730
1731 pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_ofdm:\t\t\t %u\n",
1732 data->auto_corr_ofdm);
1733 pos += scnprintf(buf + pos, bufsz - pos,
1734 "auto_corr_ofdm_mrc:\t\t %u\n",
1735 data->auto_corr_ofdm_mrc);
1736 pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_ofdm_x1:\t\t %u\n",
1737 data->auto_corr_ofdm_x1);
1738 pos += scnprintf(buf + pos, bufsz - pos,
1739 "auto_corr_ofdm_mrc_x1:\t\t %u\n",
1740 data->auto_corr_ofdm_mrc_x1);
1741 pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_cck:\t\t\t %u\n",
1742 data->auto_corr_cck);
1743 pos += scnprintf(buf + pos, bufsz - pos, "auto_corr_cck_mrc:\t\t %u\n",
1744 data->auto_corr_cck_mrc);
1745 pos += scnprintf(buf + pos, bufsz - pos,
1746 "last_bad_plcp_cnt_ofdm:\t\t %u\n",
1747 data->last_bad_plcp_cnt_ofdm);
1748 pos += scnprintf(buf + pos, bufsz - pos, "last_fa_cnt_ofdm:\t\t %u\n",
1749 data->last_fa_cnt_ofdm);
1750 pos += scnprintf(buf + pos, bufsz - pos,
1751 "last_bad_plcp_cnt_cck:\t\t %u\n",
1752 data->last_bad_plcp_cnt_cck);
1753 pos += scnprintf(buf + pos, bufsz - pos, "last_fa_cnt_cck:\t\t %u\n",
1754 data->last_fa_cnt_cck);
1755 pos += scnprintf(buf + pos, bufsz - pos, "nrg_curr_state:\t\t\t %u\n",
1756 data->nrg_curr_state);
1757 pos += scnprintf(buf + pos, bufsz - pos, "nrg_prev_state:\t\t\t %u\n",
1758 data->nrg_prev_state);
1759 pos += scnprintf(buf + pos, bufsz - pos, "nrg_value:\t\t\t");
1760 for (cnt = 0; cnt < 10; cnt++) {
1761 pos += scnprintf(buf + pos, bufsz - pos, " %u",
1762 data->nrg_value[cnt]);
1763 }
1764 pos += scnprintf(buf + pos, bufsz - pos, "\n");
1765 pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_rssi:\t\t");
1766 for (cnt = 0; cnt < NRG_NUM_PREV_STAT_L; cnt++) {
1767 pos += scnprintf(buf + pos, bufsz - pos, " %u",
1768 data->nrg_silence_rssi[cnt]);
1769 }
1770 pos += scnprintf(buf + pos, bufsz - pos, "\n");
1771 pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_ref:\t\t %u\n",
1772 data->nrg_silence_ref);
1773 pos += scnprintf(buf + pos, bufsz - pos, "nrg_energy_idx:\t\t\t %u\n",
1774 data->nrg_energy_idx);
1775 pos += scnprintf(buf + pos, bufsz - pos, "nrg_silence_idx:\t\t %u\n",
1776 data->nrg_silence_idx);
1777 pos += scnprintf(buf + pos, bufsz - pos, "nrg_th_cck:\t\t\t %u\n",
1778 data->nrg_th_cck);
1779 pos += scnprintf(buf + pos, bufsz - pos,
1780 "nrg_auto_corr_silence_diff:\t %u\n",
1781 data->nrg_auto_corr_silence_diff);
1782 pos += scnprintf(buf + pos, bufsz - pos, "num_in_cck_no_fa:\t\t %u\n",
1783 data->num_in_cck_no_fa);
1784 pos += scnprintf(buf + pos, bufsz - pos, "nrg_th_ofdm:\t\t\t %u\n",
1785 data->nrg_th_ofdm);
1786
1787 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1788 kfree(buf);
1789 return ret;
1790}
1791
1792
1793static ssize_t iwl_dbgfs_chain_noise_read(struct file *file,
1794 char __user *user_buf,
1795 size_t count, loff_t *ppos) {
1796
1797 struct iwl_priv *priv = file->private_data;
1798 int pos = 0;
1799 int cnt = 0;
1800 char *buf;
1801 int bufsz = sizeof(struct iwl_chain_noise_data) * 4 + 100;
1802 ssize_t ret;
1803 struct iwl_chain_noise_data *data;
1804
1805 data = &priv->chain_noise_data;
1806 buf = kzalloc(bufsz, GFP_KERNEL);
1807 if (!buf)
1808 return -ENOMEM;
1809
1810 pos += scnprintf(buf + pos, bufsz - pos, "active_chains:\t\t\t %u\n",
1811 data->active_chains);
1812 pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_a:\t\t\t %u\n",
1813 data->chain_noise_a);
1814 pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_b:\t\t\t %u\n",
1815 data->chain_noise_b);
1816 pos += scnprintf(buf + pos, bufsz - pos, "chain_noise_c:\t\t\t %u\n",
1817 data->chain_noise_c);
1818 pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_a:\t\t\t %u\n",
1819 data->chain_signal_a);
1820 pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_b:\t\t\t %u\n",
1821 data->chain_signal_b);
1822 pos += scnprintf(buf + pos, bufsz - pos, "chain_signal_c:\t\t\t %u\n",
1823 data->chain_signal_c);
1824 pos += scnprintf(buf + pos, bufsz - pos, "beacon_count:\t\t\t %u\n",
1825 data->beacon_count);
1826
1827 pos += scnprintf(buf + pos, bufsz - pos, "disconn_array:\t\t\t");
1828 for (cnt = 0; cnt < NUM_RX_CHAINS; cnt++) {
1829 pos += scnprintf(buf + pos, bufsz - pos, " %u",
1830 data->disconn_array[cnt]);
1831 }
1832 pos += scnprintf(buf + pos, bufsz - pos, "\n");
1833 pos += scnprintf(buf + pos, bufsz - pos, "delta_gain_code:\t\t");
1834 for (cnt = 0; cnt < NUM_RX_CHAINS; cnt++) {
1835 pos += scnprintf(buf + pos, bufsz - pos, " %u",
1836 data->delta_gain_code[cnt]);
1837 }
1838 pos += scnprintf(buf + pos, bufsz - pos, "\n");
1839 pos += scnprintf(buf + pos, bufsz - pos, "radio_write:\t\t\t %u\n",
1840 data->radio_write);
1841 pos += scnprintf(buf + pos, bufsz - pos, "state:\t\t\t\t %u\n",
1842 data->state);
1843
1844 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1845 kfree(buf);
1846 return ret;
1847}
1848
1849static ssize_t iwl_dbgfs_power_save_status_read(struct file *file,
1850 char __user *user_buf,
1851 size_t count, loff_t *ppos)
1852{
1853 struct iwl_priv *priv = file->private_data;
1854 char buf[60];
1855 int pos = 0;
1856 const size_t bufsz = sizeof(buf);
1857 u32 pwrsave_status;
1858
1859 pwrsave_status = iwl_read32(priv->trans, CSR_GP_CNTRL) &
1860 CSR_GP_REG_POWER_SAVE_STATUS_MSK;
1861
1862 pos += scnprintf(buf + pos, bufsz - pos, "Power Save Status: ");
1863 pos += scnprintf(buf + pos, bufsz - pos, "%s\n",
1864 (pwrsave_status == CSR_GP_REG_NO_POWER_SAVE) ? "none" :
1865 (pwrsave_status == CSR_GP_REG_MAC_POWER_SAVE) ? "MAC" :
1866 (pwrsave_status == CSR_GP_REG_PHY_POWER_SAVE) ? "PHY" :
1867 "error");
1868
1869 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1870}
1871
1872static ssize_t iwl_dbgfs_clear_ucode_statistics_write(struct file *file,
1873 const char __user *user_buf,
1874 size_t count, loff_t *ppos)
1875{
1876 struct iwl_priv *priv = file->private_data;
1877 char buf[8];
1878 int buf_size;
1879 int clear;
1880
1881 memset(buf, 0, sizeof(buf));
1882 buf_size = min(count, sizeof(buf) - 1);
1883 if (copy_from_user(buf, user_buf, buf_size))
1884 return -EFAULT;
1885 if (sscanf(buf, "%d", &clear) != 1)
1886 return -EFAULT;
1887
1888 /* make request to uCode to retrieve statistics information */
1889 mutex_lock(&priv->mutex);
1890 iwl_send_statistics_request(priv, CMD_SYNC, true);
1891 mutex_unlock(&priv->mutex);
1892
1893 return count;
1894}
1895
1896static ssize_t iwl_dbgfs_ucode_tracing_read(struct file *file,
1897 char __user *user_buf,
1898 size_t count, loff_t *ppos) {
1899
1900 struct iwl_priv *priv = file->private_data;
1901 int pos = 0;
1902 char buf[128];
1903 const size_t bufsz = sizeof(buf);
1904
1905 pos += scnprintf(buf + pos, bufsz - pos, "ucode trace timer is %s\n",
1906 priv->event_log.ucode_trace ? "On" : "Off");
1907 pos += scnprintf(buf + pos, bufsz - pos, "non_wraps_count:\t\t %u\n",
1908 priv->event_log.non_wraps_count);
1909 pos += scnprintf(buf + pos, bufsz - pos, "wraps_once_count:\t\t %u\n",
1910 priv->event_log.wraps_once_count);
1911 pos += scnprintf(buf + pos, bufsz - pos, "wraps_more_count:\t\t %u\n",
1912 priv->event_log.wraps_more_count);
1913
1914 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1915}
1916
1917static ssize_t iwl_dbgfs_ucode_tracing_write(struct file *file,
1918 const char __user *user_buf,
1919 size_t count, loff_t *ppos)
1920{
1921 struct iwl_priv *priv = file->private_data;
1922 char buf[8];
1923 int buf_size;
1924 int trace;
1925
1926 memset(buf, 0, sizeof(buf));
1927 buf_size = min(count, sizeof(buf) - 1);
1928 if (copy_from_user(buf, user_buf, buf_size))
1929 return -EFAULT;
1930 if (sscanf(buf, "%d", &trace) != 1)
1931 return -EFAULT;
1932
1933 if (trace) {
1934 priv->event_log.ucode_trace = true;
1935 if (iwl_is_alive(priv)) {
1936 /* start collecting data now */
1937 mod_timer(&priv->ucode_trace, jiffies);
1938 }
1939 } else {
1940 priv->event_log.ucode_trace = false;
1941 del_timer_sync(&priv->ucode_trace);
1942 }
1943
1944 return count;
1945}
1946
1947static ssize_t iwl_dbgfs_rxon_flags_read(struct file *file,
1948 char __user *user_buf,
1949 size_t count, loff_t *ppos) {
1950
1951 struct iwl_priv *priv = file->private_data;
1952 int len = 0;
1953 char buf[20];
1954
1955 len = sprintf(buf, "0x%04X\n",
1956 le32_to_cpu(priv->contexts[IWL_RXON_CTX_BSS].active.flags));
1957 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1958}
1959
1960static ssize_t iwl_dbgfs_rxon_filter_flags_read(struct file *file,
1961 char __user *user_buf,
1962 size_t count, loff_t *ppos) {
1963
1964 struct iwl_priv *priv = file->private_data;
1965 int len = 0;
1966 char buf[20];
1967
1968 len = sprintf(buf, "0x%04X\n",
1969 le32_to_cpu(priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags));
1970 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1971}
1972
1973static ssize_t iwl_dbgfs_missed_beacon_read(struct file *file,
1974 char __user *user_buf,
1975 size_t count, loff_t *ppos) {
1976
1977 struct iwl_priv *priv = file->private_data;
1978 int pos = 0;
1979 char buf[12];
1980 const size_t bufsz = sizeof(buf);
1981
1982 pos += scnprintf(buf + pos, bufsz - pos, "%d\n",
1983 priv->missed_beacon_threshold);
1984
1985 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1986}
1987
1988static ssize_t iwl_dbgfs_missed_beacon_write(struct file *file,
1989 const char __user *user_buf,
1990 size_t count, loff_t *ppos)
1991{
1992 struct iwl_priv *priv = file->private_data;
1993 char buf[8];
1994 int buf_size;
1995 int missed;
1996
1997 memset(buf, 0, sizeof(buf));
1998 buf_size = min(count, sizeof(buf) - 1);
1999 if (copy_from_user(buf, user_buf, buf_size))
2000 return -EFAULT;
2001 if (sscanf(buf, "%d", &missed) != 1)
2002 return -EINVAL;
2003
2004 if (missed < IWL_MISSED_BEACON_THRESHOLD_MIN ||
2005 missed > IWL_MISSED_BEACON_THRESHOLD_MAX)
2006 priv->missed_beacon_threshold =
2007 IWL_MISSED_BEACON_THRESHOLD_DEF;
2008 else
2009 priv->missed_beacon_threshold = missed;
2010
2011 return count;
2012}
2013
2014static ssize_t iwl_dbgfs_plcp_delta_read(struct file *file,
2015 char __user *user_buf,
2016 size_t count, loff_t *ppos) {
2017
2018 struct iwl_priv *priv = file->private_data;
2019 int pos = 0;
2020 char buf[12];
2021 const size_t bufsz = sizeof(buf);
2022
2023 pos += scnprintf(buf + pos, bufsz - pos, "%u\n",
2024 priv->plcp_delta_threshold);
2025
2026 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2027}
2028
2029static ssize_t iwl_dbgfs_plcp_delta_write(struct file *file,
2030 const char __user *user_buf,
2031 size_t count, loff_t *ppos) {
2032
2033 struct iwl_priv *priv = file->private_data;
2034 char buf[8];
2035 int buf_size;
2036 int plcp;
2037
2038 memset(buf, 0, sizeof(buf));
2039 buf_size = min(count, sizeof(buf) - 1);
2040 if (copy_from_user(buf, user_buf, buf_size))
2041 return -EFAULT;
2042 if (sscanf(buf, "%d", &plcp) != 1)
2043 return -EINVAL;
2044 if ((plcp < IWL_MAX_PLCP_ERR_THRESHOLD_MIN) ||
2045 (plcp > IWL_MAX_PLCP_ERR_THRESHOLD_MAX))
2046 priv->plcp_delta_threshold =
2047 IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE;
2048 else
2049 priv->plcp_delta_threshold = plcp;
2050 return count;
2051}
2052
2053static ssize_t iwl_dbgfs_rf_reset_read(struct file *file,
2054 char __user *user_buf,
2055 size_t count, loff_t *ppos)
2056{
2057 struct iwl_priv *priv = file->private_data;
2058 int pos = 0;
2059 char buf[300];
2060 const size_t bufsz = sizeof(buf);
2061 struct iwl_rf_reset *rf_reset = &priv->rf_reset;
2062
2063 pos += scnprintf(buf + pos, bufsz - pos,
2064 "RF reset statistics\n");
2065 pos += scnprintf(buf + pos, bufsz - pos,
2066 "\tnumber of reset request: %d\n",
2067 rf_reset->reset_request_count);
2068 pos += scnprintf(buf + pos, bufsz - pos,
2069 "\tnumber of reset request success: %d\n",
2070 rf_reset->reset_success_count);
2071 pos += scnprintf(buf + pos, bufsz - pos,
2072 "\tnumber of reset request reject: %d\n",
2073 rf_reset->reset_reject_count);
2074
2075 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2076}
2077
2078static ssize_t iwl_dbgfs_rf_reset_write(struct file *file,
2079 const char __user *user_buf,
2080 size_t count, loff_t *ppos) {
2081
2082 struct iwl_priv *priv = file->private_data;
2083 int ret;
2084
2085 ret = iwl_force_rf_reset(priv, true);
2086 return ret ? ret : count;
2087}
2088
2089static ssize_t iwl_dbgfs_txfifo_flush_write(struct file *file,
2090 const char __user *user_buf,
2091 size_t count, loff_t *ppos) {
2092
2093 struct iwl_priv *priv = file->private_data;
2094 char buf[8];
2095 int buf_size;
2096 int flush;
2097
2098 memset(buf, 0, sizeof(buf));
2099 buf_size = min(count, sizeof(buf) - 1);
2100 if (copy_from_user(buf, user_buf, buf_size))
2101 return -EFAULT;
2102 if (sscanf(buf, "%d", &flush) != 1)
2103 return -EINVAL;
2104
2105 if (iwl_is_rfkill(priv))
2106 return -EFAULT;
2107
2108 iwlagn_dev_txfifo_flush(priv, IWL_DROP_ALL);
2109
2110 return count;
2111}
2112
2113static ssize_t iwl_dbgfs_bt_traffic_read(struct file *file,
2114 char __user *user_buf,
2115 size_t count, loff_t *ppos) {
2116
2117 struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
2118 int pos = 0;
2119 char buf[200];
2120 const size_t bufsz = sizeof(buf);
2121
2122 if (!priv->bt_enable_flag) {
2123 pos += scnprintf(buf + pos, bufsz - pos, "BT coex disabled\n");
2124 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2125 }
2126 pos += scnprintf(buf + pos, bufsz - pos, "BT enable flag: 0x%x\n",
2127 priv->bt_enable_flag);
2128 pos += scnprintf(buf + pos, bufsz - pos, "BT in %s mode\n",
2129 priv->bt_full_concurrent ? "full concurrency" : "3-wire");
2130 pos += scnprintf(buf + pos, bufsz - pos, "BT status: %s, "
2131 "last traffic notif: %d\n",
2132 priv->bt_status ? "On" : "Off", priv->last_bt_traffic_load);
2133 pos += scnprintf(buf + pos, bufsz - pos, "ch_announcement: %d, "
2134 "kill_ack_mask: %x, kill_cts_mask: %x\n",
2135 priv->bt_ch_announce, priv->kill_ack_mask,
2136 priv->kill_cts_mask);
2137
2138 pos += scnprintf(buf + pos, bufsz - pos, "bluetooth traffic load: ");
2139 switch (priv->bt_traffic_load) {
2140 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
2141 pos += scnprintf(buf + pos, bufsz - pos, "Continuous\n");
2142 break;
2143 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
2144 pos += scnprintf(buf + pos, bufsz - pos, "High\n");
2145 break;
2146 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
2147 pos += scnprintf(buf + pos, bufsz - pos, "Low\n");
2148 break;
2149 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
2150 default:
2151 pos += scnprintf(buf + pos, bufsz - pos, "None\n");
2152 break;
2153 }
2154
2155 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2156}
2157
2158static ssize_t iwl_dbgfs_protection_mode_read(struct file *file,
2159 char __user *user_buf,
2160 size_t count, loff_t *ppos)
2161{
2162 struct iwl_priv *priv = (struct iwl_priv *)file->private_data;
2163
2164 int pos = 0;
2165 char buf[40];
2166 const size_t bufsz = sizeof(buf);
2167
2168 if (priv->cfg->ht_params)
2169 pos += scnprintf(buf + pos, bufsz - pos,
2170 "use %s for aggregation\n",
2171 (priv->hw_params.use_rts_for_aggregation) ?
2172 "rts/cts" : "cts-to-self");
2173 else
2174 pos += scnprintf(buf + pos, bufsz - pos, "N/A");
2175
2176 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2177}
2178
2179static ssize_t iwl_dbgfs_protection_mode_write(struct file *file,
2180 const char __user *user_buf,
2181 size_t count, loff_t *ppos) {
2182
2183 struct iwl_priv *priv = file->private_data;
2184 char buf[8];
2185 int buf_size;
2186 int rts;
2187
2188 if (!priv->cfg->ht_params)
2189 return -EINVAL;
2190
2191 memset(buf, 0, sizeof(buf));
2192 buf_size = min(count, sizeof(buf) - 1);
2193 if (copy_from_user(buf, user_buf, buf_size))
2194 return -EFAULT;
2195 if (sscanf(buf, "%d", &rts) != 1)
2196 return -EINVAL;
2197 if (rts)
2198 priv->hw_params.use_rts_for_aggregation = true;
2199 else
2200 priv->hw_params.use_rts_for_aggregation = false;
2201 return count;
2202}
2203
2204static int iwl_cmd_echo_test(struct iwl_priv *priv)
2205{
2206 int ret;
2207 struct iwl_host_cmd cmd = {
2208 .id = REPLY_ECHO,
2209 .len = { 0 },
2210 .flags = CMD_SYNC,
2211 };
2212
2213 ret = iwl_dvm_send_cmd(priv, &cmd);
2214 if (ret)
2215 IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
2216 else
2217 IWL_DEBUG_INFO(priv, "echo testing pass\n");
2218 return ret;
2219}
2220
2221static ssize_t iwl_dbgfs_echo_test_write(struct file *file,
2222 const char __user *user_buf,
2223 size_t count, loff_t *ppos)
2224{
2225 struct iwl_priv *priv = file->private_data;
2226 char buf[8];
2227 int buf_size;
2228
2229 memset(buf, 0, sizeof(buf));
2230 buf_size = min(count, sizeof(buf) - 1);
2231 if (copy_from_user(buf, user_buf, buf_size))
2232 return -EFAULT;
2233
2234 iwl_cmd_echo_test(priv);
2235 return count;
2236}
2237
2238static ssize_t iwl_dbgfs_log_event_read(struct file *file,
2239 char __user *user_buf,
2240 size_t count, loff_t *ppos)
2241{
2242 struct iwl_priv *priv = file->private_data;
2243 char *buf;
2244 int pos = 0;
2245 ssize_t ret = -ENOMEM;
2246
2247 ret = pos = iwl_dump_nic_event_log(priv, true, &buf, true);
2248 if (buf) {
2249 ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2250 kfree(buf);
2251 }
2252 return ret;
2253}
2254
2255static ssize_t iwl_dbgfs_log_event_write(struct file *file,
2256 const char __user *user_buf,
2257 size_t count, loff_t *ppos)
2258{
2259 struct iwl_priv *priv = file->private_data;
2260 u32 event_log_flag;
2261 char buf[8];
2262 int buf_size;
2263
2264 memset(buf, 0, sizeof(buf));
2265 buf_size = min(count, sizeof(buf) - 1);
2266 if (copy_from_user(buf, user_buf, buf_size))
2267 return -EFAULT;
2268 if (sscanf(buf, "%d", &event_log_flag) != 1)
2269 return -EFAULT;
2270 if (event_log_flag == 1)
2271 iwl_dump_nic_event_log(priv, true, NULL, false);
2272
2273 return count;
2274}
2275
2276static ssize_t iwl_dbgfs_calib_disabled_read(struct file *file,
2277 char __user *user_buf,
2278 size_t count, loff_t *ppos)
2279{
2280 struct iwl_priv *priv = file->private_data;
2281 char buf[120];
2282 int pos = 0;
2283 const size_t bufsz = sizeof(buf);
2284
2285 pos += scnprintf(buf + pos, bufsz - pos,
2286 "Sensitivity calibrations %s\n",
2287 (priv->calib_disabled &
2288 IWL_SENSITIVITY_CALIB_DISABLED) ?
2289 "DISABLED" : "ENABLED");
2290 pos += scnprintf(buf + pos, bufsz - pos,
2291 "Chain noise calibrations %s\n",
2292 (priv->calib_disabled &
2293 IWL_CHAIN_NOISE_CALIB_DISABLED) ?
2294 "DISABLED" : "ENABLED");
2295 pos += scnprintf(buf + pos, bufsz - pos,
2296 "Tx power calibrations %s\n",
2297 (priv->calib_disabled &
2298 IWL_TX_POWER_CALIB_DISABLED) ?
2299 "DISABLED" : "ENABLED");
2300
2301 return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2302}
2303
2304static ssize_t iwl_dbgfs_calib_disabled_write(struct file *file,
2305 const char __user *user_buf,
2306 size_t count, loff_t *ppos)
2307{
2308 struct iwl_priv *priv = file->private_data;
2309 char buf[8];
2310 u32 calib_disabled;
2311 int buf_size;
2312
2313 memset(buf, 0, sizeof(buf));
2314 buf_size = min(count, sizeof(buf) - 1);
2315 if (copy_from_user(buf, user_buf, buf_size))
2316 return -EFAULT;
2317 if (sscanf(buf, "%x", &calib_disabled) != 1)
2318 return -EFAULT;
2319
2320 priv->calib_disabled = calib_disabled;
2321
2322 return count;
2323}
2324
2325DEBUGFS_READ_FILE_OPS(ucode_rx_stats);
2326DEBUGFS_READ_FILE_OPS(ucode_tx_stats);
2327DEBUGFS_READ_FILE_OPS(ucode_general_stats);
2328DEBUGFS_READ_FILE_OPS(sensitivity);
2329DEBUGFS_READ_FILE_OPS(chain_noise);
2330DEBUGFS_READ_FILE_OPS(power_save_status);
2331DEBUGFS_WRITE_FILE_OPS(clear_ucode_statistics);
2332DEBUGFS_READ_WRITE_FILE_OPS(ucode_tracing);
2333DEBUGFS_READ_WRITE_FILE_OPS(missed_beacon);
2334DEBUGFS_READ_WRITE_FILE_OPS(plcp_delta);
2335DEBUGFS_READ_WRITE_FILE_OPS(rf_reset);
2336DEBUGFS_READ_FILE_OPS(rxon_flags);
2337DEBUGFS_READ_FILE_OPS(rxon_filter_flags);
2338DEBUGFS_WRITE_FILE_OPS(txfifo_flush);
2339DEBUGFS_READ_FILE_OPS(ucode_bt_stats);
2340DEBUGFS_READ_FILE_OPS(bt_traffic);
2341DEBUGFS_READ_WRITE_FILE_OPS(protection_mode);
2342DEBUGFS_READ_FILE_OPS(reply_tx_error);
2343DEBUGFS_WRITE_FILE_OPS(echo_test);
2344DEBUGFS_READ_WRITE_FILE_OPS(log_event);
2345DEBUGFS_READ_WRITE_FILE_OPS(calib_disabled);
2346
2347/*
2348 * Create the debugfs files and directories
2349 *
2350 */
2351int iwl_dbgfs_register(struct iwl_priv *priv, const char *name)
2352{
2353 struct dentry *phyd = priv->hw->wiphy->debugfsdir;
2354 struct dentry *dir_drv, *dir_data, *dir_rf, *dir_debug;
2355
2356 dir_drv = debugfs_create_dir(name, phyd);
2357 if (!dir_drv)
2358 return -ENOMEM;
2359
2360 priv->debugfs_dir = dir_drv;
2361
2362 dir_data = debugfs_create_dir("data", dir_drv);
2363 if (!dir_data)
2364 goto err;
2365 dir_rf = debugfs_create_dir("rf", dir_drv);
2366 if (!dir_rf)
2367 goto err;
2368 dir_debug = debugfs_create_dir("debug", dir_drv);
2369 if (!dir_debug)
2370 goto err;
2371
2372 DEBUGFS_ADD_FILE(nvm, dir_data, S_IRUSR);
2373 DEBUGFS_ADD_FILE(sram, dir_data, S_IWUSR | S_IRUSR);
2374 DEBUGFS_ADD_FILE(wowlan_sram, dir_data, S_IRUSR);
2375 DEBUGFS_ADD_FILE(stations, dir_data, S_IRUSR);
2376 DEBUGFS_ADD_FILE(channels, dir_data, S_IRUSR);
2377 DEBUGFS_ADD_FILE(status, dir_data, S_IRUSR);
2378 DEBUGFS_ADD_FILE(rx_handlers, dir_data, S_IWUSR | S_IRUSR);
2379 DEBUGFS_ADD_FILE(qos, dir_data, S_IRUSR);
2380 DEBUGFS_ADD_FILE(sleep_level_override, dir_data, S_IWUSR | S_IRUSR);
2381 DEBUGFS_ADD_FILE(current_sleep_command, dir_data, S_IRUSR);
2382 DEBUGFS_ADD_FILE(thermal_throttling, dir_data, S_IRUSR);
2383 DEBUGFS_ADD_FILE(disable_ht40, dir_data, S_IWUSR | S_IRUSR);
2384 DEBUGFS_ADD_FILE(temperature, dir_data, S_IRUSR);
2385
2386 DEBUGFS_ADD_FILE(power_save_status, dir_debug, S_IRUSR);
2387 DEBUGFS_ADD_FILE(clear_ucode_statistics, dir_debug, S_IWUSR);
2388 DEBUGFS_ADD_FILE(missed_beacon, dir_debug, S_IWUSR);
2389 DEBUGFS_ADD_FILE(plcp_delta, dir_debug, S_IWUSR | S_IRUSR);
2390 DEBUGFS_ADD_FILE(rf_reset, dir_debug, S_IWUSR | S_IRUSR);
2391 DEBUGFS_ADD_FILE(ucode_rx_stats, dir_debug, S_IRUSR);
2392 DEBUGFS_ADD_FILE(ucode_tx_stats, dir_debug, S_IRUSR);
2393 DEBUGFS_ADD_FILE(ucode_general_stats, dir_debug, S_IRUSR);
2394 DEBUGFS_ADD_FILE(txfifo_flush, dir_debug, S_IWUSR);
2395 DEBUGFS_ADD_FILE(protection_mode, dir_debug, S_IWUSR | S_IRUSR);
2396 DEBUGFS_ADD_FILE(sensitivity, dir_debug, S_IRUSR);
2397 DEBUGFS_ADD_FILE(chain_noise, dir_debug, S_IRUSR);
2398 DEBUGFS_ADD_FILE(ucode_tracing, dir_debug, S_IWUSR | S_IRUSR);
2399 DEBUGFS_ADD_FILE(ucode_bt_stats, dir_debug, S_IRUSR);
2400 DEBUGFS_ADD_FILE(reply_tx_error, dir_debug, S_IRUSR);
2401 DEBUGFS_ADD_FILE(rxon_flags, dir_debug, S_IWUSR);
2402 DEBUGFS_ADD_FILE(rxon_filter_flags, dir_debug, S_IWUSR);
2403 DEBUGFS_ADD_FILE(echo_test, dir_debug, S_IWUSR);
2404 DEBUGFS_ADD_FILE(log_event, dir_debug, S_IWUSR | S_IRUSR);
2405
2406 if (iwl_advanced_bt_coexist(priv))
2407 DEBUGFS_ADD_FILE(bt_traffic, dir_debug, S_IRUSR);
2408
2409 /* Calibrations disabled/enabled status*/
2410 DEBUGFS_ADD_FILE(calib_disabled, dir_rf, S_IWUSR | S_IRUSR);
2411
2412 if (iwl_trans_dbgfs_register(priv->trans, dir_debug))
2413 goto err;
2414 return 0;
2415
2416err:
2417 IWL_ERR(priv, "Can't create the debugfs directory\n");
2418 iwl_dbgfs_unregister(priv);
2419 return -ENOMEM;
2420}
2421
2422/**
2423 * Remove the debugfs files and directories
2424 *
2425 */
2426void iwl_dbgfs_unregister(struct iwl_priv *priv)
2427{
2428 if (!priv->debugfs_dir)
2429 return;
2430
2431 debugfs_remove_recursive(priv->debugfs_dir);
2432 priv->debugfs_dir = NULL;
2433}
diff --git a/drivers/net/wireless/iwlwifi/dvm/dev.h b/drivers/net/wireless/iwlwifi/dvm/dev.h
new file mode 100644
index 000000000000..2880a0a3da68
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/dev.h
@@ -0,0 +1,1073 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26/*
27 * Please use this file (dev.h) for driver implementation definitions.
28 * Please use commands.h for uCode API definitions.
29 */
30
31#ifndef __iwl_dev_h__
32#define __iwl_dev_h__
33
34#include <linux/interrupt.h>
35#include <linux/kernel.h>
36#include <linux/wait.h>
37#include <linux/leds.h>
38#include <linux/slab.h>
39#include <linux/mutex.h>
40
41#include "iwl-fw.h"
42#include "iwl-csr.h"
43#include "iwl-debug.h"
44#include "iwl-agn-hw.h"
45#include "iwl-op-mode.h"
46#include "iwl-notif-wait.h"
47#include "iwl-trans.h"
48
49#include "eeprom.h"
50#include "led.h"
51#include "power.h"
52#include "rs.h"
53#include "tt.h"
54
55/* CT-KILL constants */
56#define CT_KILL_THRESHOLD_LEGACY 110 /* in Celsius */
57#define CT_KILL_THRESHOLD 114 /* in Celsius */
58#define CT_KILL_EXIT_THRESHOLD 95 /* in Celsius */
59
60/* Default noise level to report when noise measurement is not available.
61 * This may be because we're:
62 * 1) Not associated no beacon statistics being sent to driver)
63 * 2) Scanning (noise measurement does not apply to associated channel)
64 * Use default noise value of -127 ... this is below the range of measurable
65 * Rx dBm for all agn devices, so it can indicate "unmeasurable" to user.
66 * Also, -127 works better than 0 when averaging frames with/without
67 * noise info (e.g. averaging might be done in app); measured dBm values are
68 * always negative ... using a negative value as the default keeps all
69 * averages within an s8's (used in some apps) range of negative values. */
70#define IWL_NOISE_MEAS_NOT_AVAILABLE (-127)
71
72/*
73 * RTS threshold here is total size [2347] minus 4 FCS bytes
74 * Per spec:
75 * a value of 0 means RTS on all data/management packets
76 * a value > max MSDU size means no RTS
77 * else RTS for data/management frames where MPDU is larger
78 * than RTS value.
79 */
80#define DEFAULT_RTS_THRESHOLD 2347U
81#define MIN_RTS_THRESHOLD 0U
82#define MAX_RTS_THRESHOLD 2347U
83#define MAX_MSDU_SIZE 2304U
84#define MAX_MPDU_SIZE 2346U
85#define DEFAULT_BEACON_INTERVAL 200U
86#define DEFAULT_SHORT_RETRY_LIMIT 7U
87#define DEFAULT_LONG_RETRY_LIMIT 4U
88
89#define IWL_NUM_SCAN_RATES (2)
90
91/*
92 * One for each channel, holds all channel setup data
93 * Some of the fields (e.g. eeprom and flags/max_power_avg) are redundant
94 * with one another!
95 */
96struct iwl_channel_info {
97 struct iwl_eeprom_channel eeprom; /* EEPROM regulatory limit */
98 struct iwl_eeprom_channel ht40_eeprom; /* EEPROM regulatory limit for
99 * HT40 channel */
100
101 u8 channel; /* channel number */
102 u8 flags; /* flags copied from EEPROM */
103 s8 max_power_avg; /* (dBm) regul. eeprom, normal Tx, any rate */
104 s8 curr_txpow; /* (dBm) regulatory/spectrum/user (not h/w) limit */
105 s8 min_power; /* always 0 */
106 s8 scan_power; /* (dBm) regul. eeprom, direct scans, any rate */
107
108 u8 group_index; /* 0-4, maps channel to group1/2/3/4/5 */
109 u8 band_index; /* 0-4, maps channel to band1/2/3/4/5 */
110 enum ieee80211_band band;
111
112 /* HT40 channel info */
113 s8 ht40_max_power_avg; /* (dBm) regul. eeprom, normal Tx, any rate */
114 u8 ht40_flags; /* flags copied from EEPROM */
115 u8 ht40_extension_channel; /* HT_IE_EXT_CHANNEL_* */
116};
117
118/*
119 * Minimum number of queues. MAX_NUM is defined in hw specific files.
120 * Set the minimum to accommodate
121 * - 4 standard TX queues
122 * - the command queue
123 * - 4 PAN TX queues
124 * - the PAN multicast queue, and
125 * - the AUX (TX during scan dwell) queue.
126 */
127#define IWL_MIN_NUM_QUEUES 11
128
129/*
130 * Command queue depends on iPAN support.
131 */
132#define IWL_DEFAULT_CMD_QUEUE_NUM 4
133#define IWL_IPAN_CMD_QUEUE_NUM 9
134
135#define IEEE80211_DATA_LEN 2304
136#define IEEE80211_4ADDR_LEN 30
137#define IEEE80211_HLEN (IEEE80211_4ADDR_LEN)
138#define IEEE80211_FRAME_LEN (IEEE80211_DATA_LEN + IEEE80211_HLEN)
139
140#define SUP_RATE_11A_MAX_NUM_CHANNELS 8
141#define SUP_RATE_11B_MAX_NUM_CHANNELS 4
142#define SUP_RATE_11G_MAX_NUM_CHANNELS 12
143
144#define IWL_SUPPORTED_RATES_IE_LEN 8
145
146#define IWL_INVALID_RATE 0xFF
147#define IWL_INVALID_VALUE -1
148
149union iwl_ht_rate_supp {
150 u16 rates;
151 struct {
152 u8 siso_rate;
153 u8 mimo_rate;
154 };
155};
156
157#define CFG_HT_RX_AMPDU_FACTOR_8K (0x0)
158#define CFG_HT_RX_AMPDU_FACTOR_16K (0x1)
159#define CFG_HT_RX_AMPDU_FACTOR_32K (0x2)
160#define CFG_HT_RX_AMPDU_FACTOR_64K (0x3)
161#define CFG_HT_RX_AMPDU_FACTOR_DEF CFG_HT_RX_AMPDU_FACTOR_64K
162#define CFG_HT_RX_AMPDU_FACTOR_MAX CFG_HT_RX_AMPDU_FACTOR_64K
163#define CFG_HT_RX_AMPDU_FACTOR_MIN CFG_HT_RX_AMPDU_FACTOR_8K
164
165/*
166 * Maximal MPDU density for TX aggregation
167 * 4 - 2us density
168 * 5 - 4us density
169 * 6 - 8us density
170 * 7 - 16us density
171 */
172#define CFG_HT_MPDU_DENSITY_2USEC (0x4)
173#define CFG_HT_MPDU_DENSITY_4USEC (0x5)
174#define CFG_HT_MPDU_DENSITY_8USEC (0x6)
175#define CFG_HT_MPDU_DENSITY_16USEC (0x7)
176#define CFG_HT_MPDU_DENSITY_DEF CFG_HT_MPDU_DENSITY_4USEC
177#define CFG_HT_MPDU_DENSITY_MAX CFG_HT_MPDU_DENSITY_16USEC
178#define CFG_HT_MPDU_DENSITY_MIN (0x1)
179
180struct iwl_ht_config {
181 bool single_chain_sufficient;
182 enum ieee80211_smps_mode smps; /* current smps mode */
183};
184
185/* QoS structures */
186struct iwl_qos_info {
187 int qos_active;
188 struct iwl_qosparam_cmd def_qos_parm;
189};
190
191/**
192 * enum iwl_agg_state
193 *
194 * The state machine of the BA agreement establishment / tear down.
195 * These states relate to a specific RA / TID.
196 *
197 * @IWL_AGG_OFF: aggregation is not used
198 * @IWL_AGG_STARTING: aggregation are starting (between start and oper)
199 * @IWL_AGG_ON: aggregation session is up
200 * @IWL_EMPTYING_HW_QUEUE_ADDBA: establishing a BA session - waiting for the
201 * HW queue to be empty from packets for this RA /TID.
202 * @IWL_EMPTYING_HW_QUEUE_DELBA: tearing down a BA session - waiting for the
203 * HW queue to be empty from packets for this RA /TID.
204 */
205enum iwl_agg_state {
206 IWL_AGG_OFF = 0,
207 IWL_AGG_STARTING,
208 IWL_AGG_ON,
209 IWL_EMPTYING_HW_QUEUE_ADDBA,
210 IWL_EMPTYING_HW_QUEUE_DELBA,
211};
212
213/**
214 * struct iwl_ht_agg - aggregation state machine
215
216 * This structs holds the states for the BA agreement establishment and tear
217 * down. It also holds the state during the BA session itself. This struct is
218 * duplicated for each RA / TID.
219
220 * @rate_n_flags: Rate at which Tx was attempted. Holds the data between the
221 * Tx response (REPLY_TX), and the block ack notification
222 * (REPLY_COMPRESSED_BA).
223 * @state: state of the BA agreement establishment / tear down.
224 * @txq_id: Tx queue used by the BA session
225 * @ssn: the first packet to be sent in AGG HW queue in Tx AGG start flow, or
226 * the first packet to be sent in legacy HW queue in Tx AGG stop flow.
227 * Basically when next_reclaimed reaches ssn, we can tell mac80211 that
228 * we are ready to finish the Tx AGG stop / start flow.
229 * @wait_for_ba: Expect block-ack before next Tx reply
230 */
231struct iwl_ht_agg {
232 u32 rate_n_flags;
233 enum iwl_agg_state state;
234 u16 txq_id;
235 u16 ssn;
236 bool wait_for_ba;
237};
238
239/**
240 * struct iwl_tid_data - one for each RA / TID
241
242 * This structs holds the states for each RA / TID.
243
244 * @seq_number: the next WiFi sequence number to use
245 * @next_reclaimed: the WiFi sequence number of the next packet to be acked.
246 * This is basically (last acked packet++).
247 * @agg: aggregation state machine
248 */
249struct iwl_tid_data {
250 u16 seq_number;
251 u16 next_reclaimed;
252 struct iwl_ht_agg agg;
253};
254
255/*
256 * Structure should be accessed with sta_lock held. When station addition
257 * is in progress (IWL_STA_UCODE_INPROGRESS) it is possible to access only
258 * the commands (iwl_addsta_cmd and iwl_link_quality_cmd) without sta_lock
259 * held.
260 */
261struct iwl_station_entry {
262 struct iwl_addsta_cmd sta;
263 u8 used, ctxid;
264 struct iwl_link_quality_cmd *lq;
265};
266
267/*
268 * iwl_station_priv: Driver's private station information
269 *
270 * When mac80211 creates a station it reserves some space (hw->sta_data_size)
271 * in the structure for use by driver. This structure is places in that
272 * space.
273 */
274struct iwl_station_priv {
275 struct iwl_rxon_context *ctx;
276 struct iwl_lq_sta lq_sta;
277 atomic_t pending_frames;
278 bool client;
279 bool asleep;
280 u8 max_agg_bufsize;
281 u8 sta_id;
282};
283
284/**
285 * struct iwl_vif_priv - driver's private per-interface information
286 *
287 * When mac80211 allocates a virtual interface, it can allocate
288 * space for us to put data into.
289 */
290struct iwl_vif_priv {
291 struct iwl_rxon_context *ctx;
292 u8 ibss_bssid_sta_id;
293};
294
295struct iwl_sensitivity_ranges {
296 u16 min_nrg_cck;
297
298 u16 nrg_th_cck;
299 u16 nrg_th_ofdm;
300
301 u16 auto_corr_min_ofdm;
302 u16 auto_corr_min_ofdm_mrc;
303 u16 auto_corr_min_ofdm_x1;
304 u16 auto_corr_min_ofdm_mrc_x1;
305
306 u16 auto_corr_max_ofdm;
307 u16 auto_corr_max_ofdm_mrc;
308 u16 auto_corr_max_ofdm_x1;
309 u16 auto_corr_max_ofdm_mrc_x1;
310
311 u16 auto_corr_max_cck;
312 u16 auto_corr_max_cck_mrc;
313 u16 auto_corr_min_cck;
314 u16 auto_corr_min_cck_mrc;
315
316 u16 barker_corr_th_min;
317 u16 barker_corr_th_min_mrc;
318 u16 nrg_th_cca;
319};
320
321
322#define KELVIN_TO_CELSIUS(x) ((x)-273)
323#define CELSIUS_TO_KELVIN(x) ((x)+273)
324
325
326/******************************************************************************
327 *
328 * Functions implemented in core module which are forward declared here
329 * for use by iwl-[4-5].c
330 *
331 * NOTE: The implementation of these functions are not hardware specific
332 * which is why they are in the core module files.
333 *
334 * Naming convention --
335 * iwl_ <-- Is part of iwlwifi
336 * iwlXXXX_ <-- Hardware specific (implemented in iwl-XXXX.c for XXXX)
337 *
338 ****************************************************************************/
339extern void iwl_update_chain_flags(struct iwl_priv *priv);
340extern const u8 iwl_bcast_addr[ETH_ALEN];
341
342#define IWL_OPERATION_MODE_AUTO 0
343#define IWL_OPERATION_MODE_HT_ONLY 1
344#define IWL_OPERATION_MODE_MIXED 2
345#define IWL_OPERATION_MODE_20MHZ 3
346
347#define TX_POWER_IWL_ILLEGAL_VOLTAGE -10000
348
349/* Sensitivity and chain noise calibration */
350#define INITIALIZATION_VALUE 0xFFFF
351#define IWL_CAL_NUM_BEACONS 16
352#define MAXIMUM_ALLOWED_PATHLOSS 15
353
354#define CHAIN_NOISE_MAX_DELTA_GAIN_CODE 3
355
356#define MAX_FA_OFDM 50
357#define MIN_FA_OFDM 5
358#define MAX_FA_CCK 50
359#define MIN_FA_CCK 5
360
361#define AUTO_CORR_STEP_OFDM 1
362
363#define AUTO_CORR_STEP_CCK 3
364#define AUTO_CORR_MAX_TH_CCK 160
365
366#define NRG_DIFF 2
367#define NRG_STEP_CCK 2
368#define NRG_MARGIN 8
369#define MAX_NUMBER_CCK_NO_FA 100
370
371#define AUTO_CORR_CCK_MIN_VAL_DEF (125)
372
373#define CHAIN_A 0
374#define CHAIN_B 1
375#define CHAIN_C 2
376#define CHAIN_NOISE_DELTA_GAIN_INIT_VAL 4
377#define ALL_BAND_FILTER 0xFF00
378#define IN_BAND_FILTER 0xFF
379#define MIN_AVERAGE_NOISE_MAX_VALUE 0xFFFFFFFF
380
381#define NRG_NUM_PREV_STAT_L 20
382#define NUM_RX_CHAINS 3
383
384enum iwlagn_false_alarm_state {
385 IWL_FA_TOO_MANY = 0,
386 IWL_FA_TOO_FEW = 1,
387 IWL_FA_GOOD_RANGE = 2,
388};
389
390enum iwlagn_chain_noise_state {
391 IWL_CHAIN_NOISE_ALIVE = 0, /* must be 0 */
392 IWL_CHAIN_NOISE_ACCUMULATE,
393 IWL_CHAIN_NOISE_CALIBRATED,
394 IWL_CHAIN_NOISE_DONE,
395};
396
397/* Sensitivity calib data */
398struct iwl_sensitivity_data {
399 u32 auto_corr_ofdm;
400 u32 auto_corr_ofdm_mrc;
401 u32 auto_corr_ofdm_x1;
402 u32 auto_corr_ofdm_mrc_x1;
403 u32 auto_corr_cck;
404 u32 auto_corr_cck_mrc;
405
406 u32 last_bad_plcp_cnt_ofdm;
407 u32 last_fa_cnt_ofdm;
408 u32 last_bad_plcp_cnt_cck;
409 u32 last_fa_cnt_cck;
410
411 u32 nrg_curr_state;
412 u32 nrg_prev_state;
413 u32 nrg_value[10];
414 u8 nrg_silence_rssi[NRG_NUM_PREV_STAT_L];
415 u32 nrg_silence_ref;
416 u32 nrg_energy_idx;
417 u32 nrg_silence_idx;
418 u32 nrg_th_cck;
419 s32 nrg_auto_corr_silence_diff;
420 u32 num_in_cck_no_fa;
421 u32 nrg_th_ofdm;
422
423 u16 barker_corr_th_min;
424 u16 barker_corr_th_min_mrc;
425 u16 nrg_th_cca;
426};
427
428/* Chain noise (differential Rx gain) calib data */
429struct iwl_chain_noise_data {
430 u32 active_chains;
431 u32 chain_noise_a;
432 u32 chain_noise_b;
433 u32 chain_noise_c;
434 u32 chain_signal_a;
435 u32 chain_signal_b;
436 u32 chain_signal_c;
437 u16 beacon_count;
438 u8 disconn_array[NUM_RX_CHAINS];
439 u8 delta_gain_code[NUM_RX_CHAINS];
440 u8 radio_write;
441 u8 state;
442};
443
444enum {
445 MEASUREMENT_READY = (1 << 0),
446 MEASUREMENT_ACTIVE = (1 << 1),
447};
448
449enum iwl_nvm_type {
450 NVM_DEVICE_TYPE_EEPROM = 0,
451 NVM_DEVICE_TYPE_OTP,
452};
453
454/*
455 * Two types of OTP memory access modes
456 * IWL_OTP_ACCESS_ABSOLUTE - absolute address mode,
457 * based on physical memory addressing
458 * IWL_OTP_ACCESS_RELATIVE - relative address mode,
459 * based on logical memory addressing
460 */
461enum iwl_access_mode {
462 IWL_OTP_ACCESS_ABSOLUTE,
463 IWL_OTP_ACCESS_RELATIVE,
464};
465
466/* reply_tx_statistics (for _agn devices) */
467struct reply_tx_error_statistics {
468 u32 pp_delay;
469 u32 pp_few_bytes;
470 u32 pp_bt_prio;
471 u32 pp_quiet_period;
472 u32 pp_calc_ttak;
473 u32 int_crossed_retry;
474 u32 short_limit;
475 u32 long_limit;
476 u32 fifo_underrun;
477 u32 drain_flow;
478 u32 rfkill_flush;
479 u32 life_expire;
480 u32 dest_ps;
481 u32 host_abort;
482 u32 bt_retry;
483 u32 sta_invalid;
484 u32 frag_drop;
485 u32 tid_disable;
486 u32 fifo_flush;
487 u32 insuff_cf_poll;
488 u32 fail_hw_drop;
489 u32 sta_color_mismatch;
490 u32 unknown;
491};
492
493/* reply_agg_tx_statistics (for _agn devices) */
494struct reply_agg_tx_error_statistics {
495 u32 underrun;
496 u32 bt_prio;
497 u32 few_bytes;
498 u32 abort;
499 u32 last_sent_ttl;
500 u32 last_sent_try;
501 u32 last_sent_bt_kill;
502 u32 scd_query;
503 u32 bad_crc32;
504 u32 response;
505 u32 dump_tx;
506 u32 delay_tx;
507 u32 unknown;
508};
509
510/*
511 * schedule the timer to wake up every UCODE_TRACE_PERIOD milliseconds
512 * to perform continuous uCode event logging operation if enabled
513 */
514#define UCODE_TRACE_PERIOD (10)
515
516/*
517 * iwl_event_log: current uCode event log position
518 *
519 * @ucode_trace: enable/disable ucode continuous trace timer
520 * @num_wraps: how many times the event buffer wraps
521 * @next_entry: the entry just before the next one that uCode would fill
522 * @non_wraps_count: counter for no wrap detected when dump ucode events
523 * @wraps_once_count: counter for wrap once detected when dump ucode events
524 * @wraps_more_count: counter for wrap more than once detected
525 * when dump ucode events
526 */
527struct iwl_event_log {
528 bool ucode_trace;
529 u32 num_wraps;
530 u32 next_entry;
531 int non_wraps_count;
532 int wraps_once_count;
533 int wraps_more_count;
534};
535
536#define IWL_DELAY_NEXT_FORCE_RF_RESET (HZ*3)
537
538/* BT Antenna Coupling Threshold (dB) */
539#define IWL_BT_ANTENNA_COUPLING_THRESHOLD (35)
540
541/* Firmware reload counter and Timestamp */
542#define IWL_MIN_RELOAD_DURATION 1000 /* 1000 ms */
543#define IWL_MAX_CONTINUE_RELOAD_CNT 4
544
545
546struct iwl_rf_reset {
547 int reset_request_count;
548 int reset_success_count;
549 int reset_reject_count;
550 unsigned long last_reset_jiffies;
551};
552
553enum iwl_rxon_context_id {
554 IWL_RXON_CTX_BSS,
555 IWL_RXON_CTX_PAN,
556
557 NUM_IWL_RXON_CTX
558};
559
560/* extend beacon time format bit shifting */
561/*
562 * for _agn devices
563 * bits 31:22 - extended
564 * bits 21:0 - interval
565 */
566#define IWLAGN_EXT_BEACON_TIME_POS 22
567
568struct iwl_rxon_context {
569 struct ieee80211_vif *vif;
570
571 u8 mcast_queue;
572 u8 ac_to_queue[IEEE80211_NUM_ACS];
573 u8 ac_to_fifo[IEEE80211_NUM_ACS];
574
575 /*
576 * We could use the vif to indicate active, but we
577 * also need it to be active during disabling when
578 * we already removed the vif for type setting.
579 */
580 bool always_active, is_active;
581
582 bool ht_need_multiple_chains;
583
584 enum iwl_rxon_context_id ctxid;
585
586 u32 interface_modes, exclusive_interface_modes;
587 u8 unused_devtype, ap_devtype, ibss_devtype, station_devtype;
588
589 /*
590 * We declare this const so it can only be
591 * changed via explicit cast within the
592 * routines that actually update the physical
593 * hardware.
594 */
595 const struct iwl_rxon_cmd active;
596 struct iwl_rxon_cmd staging;
597
598 struct iwl_rxon_time_cmd timing;
599
600 struct iwl_qos_info qos_data;
601
602 u8 bcast_sta_id, ap_sta_id;
603
604 u8 rxon_cmd, rxon_assoc_cmd, rxon_timing_cmd;
605 u8 qos_cmd;
606 u8 wep_key_cmd;
607
608 struct iwl_wep_key wep_keys[WEP_KEYS_MAX];
609 u8 key_mapping_keys;
610
611 __le32 station_flags;
612
613 int beacon_int;
614
615 struct {
616 bool non_gf_sta_present;
617 u8 protection;
618 bool enabled, is_40mhz;
619 u8 extension_chan_offset;
620 } ht;
621};
622
623enum iwl_scan_type {
624 IWL_SCAN_NORMAL,
625 IWL_SCAN_RADIO_RESET,
626 IWL_SCAN_ROC,
627};
628
629/**
630 * struct iwl_hw_params
631 *
632 * Holds the module parameters
633 *
634 * @tx_chains_num: Number of TX chains
635 * @rx_chains_num: Number of RX chains
636 * @valid_tx_ant: usable antennas for TX
637 * @valid_rx_ant: usable antennas for RX
638 * @ht40_channel: is 40MHz width possible: BIT(IEEE80211_BAND_XXX)
639 * @sku: sku read from EEPROM
640 * @ct_kill_threshold: temperature threshold - in hw dependent unit
641 * @ct_kill_exit_threshold: when to reeable the device - in hw dependent unit
642 * relevant for 1000, 6000 and up
643 * @struct iwl_sensitivity_ranges: range of sensitivity values
644 * @use_rts_for_aggregation: use rts/cts protection for HT traffic
645 */
646struct iwl_hw_params {
647 u8 tx_chains_num;
648 u8 rx_chains_num;
649 u8 valid_tx_ant;
650 u8 valid_rx_ant;
651 u8 ht40_channel;
652 bool use_rts_for_aggregation;
653 u16 sku;
654 u32 ct_kill_threshold;
655 u32 ct_kill_exit_threshold;
656
657 const struct iwl_sensitivity_ranges *sens;
658};
659
660struct iwl_lib_ops {
661 /* set hw dependent parameters */
662 void (*set_hw_params)(struct iwl_priv *priv);
663 int (*set_channel_switch)(struct iwl_priv *priv,
664 struct ieee80211_channel_switch *ch_switch);
665 /* device specific configuration */
666 void (*nic_config)(struct iwl_priv *priv);
667
668 /* eeprom operations (as defined in eeprom.h) */
669 struct iwl_eeprom_ops eeprom_ops;
670
671 /* temperature */
672 void (*temperature)(struct iwl_priv *priv);
673};
674
675#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
676struct iwl_testmode_trace {
677 u32 buff_size;
678 u32 total_size;
679 u32 num_chunks;
680 u8 *cpu_addr;
681 u8 *trace_addr;
682 dma_addr_t dma_addr;
683 bool trace_enabled;
684};
685struct iwl_testmode_mem {
686 u32 buff_size;
687 u32 num_chunks;
688 u8 *buff_addr;
689 bool read_in_progress;
690};
691#endif
692
693struct iwl_wipan_noa_data {
694 struct rcu_head rcu_head;
695 u32 length;
696 u8 data[];
697};
698
699/* Calibration disabling bit mask */
700enum {
701 IWL_CALIB_ENABLE_ALL = 0,
702
703 IWL_SENSITIVITY_CALIB_DISABLED = BIT(0),
704 IWL_CHAIN_NOISE_CALIB_DISABLED = BIT(1),
705 IWL_TX_POWER_CALIB_DISABLED = BIT(2),
706
707 IWL_CALIB_DISABLE_ALL = 0xFFFFFFFF,
708};
709
710#define IWL_OP_MODE_GET_DVM(_iwl_op_mode) \
711 ((struct iwl_priv *) ((_iwl_op_mode)->op_mode_specific))
712
713#define IWL_MAC80211_GET_DVM(_hw) \
714 ((struct iwl_priv *) ((struct iwl_op_mode *) \
715 (_hw)->priv)->op_mode_specific)
716
717struct iwl_priv {
718
719 struct iwl_trans *trans;
720 struct device *dev; /* for debug prints only */
721 const struct iwl_cfg *cfg;
722 const struct iwl_fw *fw;
723 const struct iwl_lib_ops *lib;
724 unsigned long status;
725
726 spinlock_t sta_lock;
727 struct mutex mutex;
728
729 unsigned long transport_queue_stop;
730 bool passive_no_rx;
731#define IWL_INVALID_MAC80211_QUEUE 0xff
732 u8 queue_to_mac80211[IWL_MAX_HW_QUEUES];
733 atomic_t queue_stop_count[IWL_MAX_HW_QUEUES];
734
735 unsigned long agg_q_alloc[BITS_TO_LONGS(IWL_MAX_HW_QUEUES)];
736
737 /* ieee device used by generic ieee processing code */
738 struct ieee80211_hw *hw;
739 struct ieee80211_channel *ieee_channels;
740 struct ieee80211_rate *ieee_rates;
741
742 struct list_head calib_results;
743
744 struct workqueue_struct *workqueue;
745
746 struct iwl_hw_params hw_params;
747
748 enum ieee80211_band band;
749 u8 valid_contexts;
750
751 void (*pre_rx_handler)(struct iwl_priv *priv,
752 struct iwl_rx_cmd_buffer *rxb);
753 int (*rx_handlers[REPLY_MAX])(struct iwl_priv *priv,
754 struct iwl_rx_cmd_buffer *rxb,
755 struct iwl_device_cmd *cmd);
756
757 struct iwl_notif_wait_data notif_wait;
758
759 struct ieee80211_supported_band bands[IEEE80211_NUM_BANDS];
760
761 /* spectrum measurement report caching */
762 struct iwl_spectrum_notification measure_report;
763 u8 measurement_status;
764
765#define IWL_OWNERSHIP_DRIVER 0
766#define IWL_OWNERSHIP_TM 1
767 u8 ucode_owner;
768
769 /* ucode beacon time */
770 u32 ucode_beacon_time;
771 int missed_beacon_threshold;
772
773 /* track IBSS manager (last beacon) status */
774 u32 ibss_manager;
775
776 /* jiffies when last recovery from statistics was performed */
777 unsigned long rx_statistics_jiffies;
778
779 /*counters */
780 u32 rx_handlers_stats[REPLY_MAX];
781
782 /* rf reset */
783 struct iwl_rf_reset rf_reset;
784
785 /* firmware reload counter and timestamp */
786 unsigned long reload_jiffies;
787 int reload_count;
788 bool ucode_loaded;
789 bool init_ucode_run; /* Don't run init uCode again */
790
791 /* we allocate array of iwl_channel_info for NIC's valid channels.
792 * Access via channel # using indirect index array */
793 struct iwl_channel_info *channel_info; /* channel info array */
794 u8 channel_count; /* # of channels */
795
796 u8 plcp_delta_threshold;
797
798 /* thermal calibration */
799 s32 temperature; /* Celsius */
800 s32 last_temperature;
801
802 struct iwl_wipan_noa_data __rcu *noa_data;
803
804 /* Scan related variables */
805 unsigned long scan_start;
806 unsigned long scan_start_tsf;
807 void *scan_cmd;
808 enum ieee80211_band scan_band;
809 struct cfg80211_scan_request *scan_request;
810 struct ieee80211_vif *scan_vif;
811 enum iwl_scan_type scan_type;
812 u8 scan_tx_ant[IEEE80211_NUM_BANDS];
813 u8 mgmt_tx_ant;
814
815 /* max number of station keys */
816 u8 sta_key_max_num;
817
818 bool new_scan_threshold_behaviour;
819
820 bool wowlan;
821
822 /* EEPROM MAC addresses */
823 struct mac_address addresses[2];
824
825 struct iwl_rxon_context contexts[NUM_IWL_RXON_CTX];
826
827 __le16 switch_channel;
828
829 u8 start_calib;
830 struct iwl_sensitivity_data sensitivity_data;
831 struct iwl_chain_noise_data chain_noise_data;
832 __le16 sensitivity_tbl[HD_TABLE_SIZE];
833 __le16 enhance_sensitivity_tbl[ENHANCE_HD_TABLE_ENTRIES];
834
835 struct iwl_ht_config current_ht_config;
836
837 /* Rate scaling data */
838 u8 retry_rate;
839
840 int activity_timer_active;
841
842 struct iwl_power_mgr power_data;
843 struct iwl_tt_mgmt thermal_throttle;
844
845 /* station table variables */
846 int num_stations;
847 struct iwl_station_entry stations[IWLAGN_STATION_COUNT];
848 unsigned long ucode_key_table;
849 struct iwl_tid_data tid_data[IWLAGN_STATION_COUNT][IWL_MAX_TID_COUNT];
850 atomic_t num_aux_in_flight;
851
852 u8 mac80211_registered;
853
854 /* Indication if ieee80211_ops->open has been called */
855 u8 is_open;
856
857 enum nl80211_iftype iw_mode;
858
859 /* Last Rx'd beacon timestamp */
860 u64 timestamp;
861
862 struct {
863 __le32 flag;
864 struct statistics_general_common common;
865 struct statistics_rx_non_phy rx_non_phy;
866 struct statistics_rx_phy rx_ofdm;
867 struct statistics_rx_ht_phy rx_ofdm_ht;
868 struct statistics_rx_phy rx_cck;
869 struct statistics_tx tx;
870#ifdef CONFIG_IWLWIFI_DEBUGFS
871 struct statistics_bt_activity bt_activity;
872 __le32 num_bt_kills, accum_num_bt_kills;
873#endif
874 spinlock_t lock;
875 } statistics;
876#ifdef CONFIG_IWLWIFI_DEBUGFS
877 struct {
878 struct statistics_general_common common;
879 struct statistics_rx_non_phy rx_non_phy;
880 struct statistics_rx_phy rx_ofdm;
881 struct statistics_rx_ht_phy rx_ofdm_ht;
882 struct statistics_rx_phy rx_cck;
883 struct statistics_tx tx;
884 struct statistics_bt_activity bt_activity;
885 } accum_stats, delta_stats, max_delta_stats;
886#endif
887
888 /*
889 * reporting the number of tids has AGG on. 0 means
890 * no AGGREGATION
891 */
892 u8 agg_tids_count;
893
894 struct iwl_rx_phy_res last_phy_res;
895 bool last_phy_res_valid;
896
897 /*
898 * chain noise reset and gain commands are the
899 * two extra calibration commands follows the standard
900 * phy calibration commands
901 */
902 u8 phy_calib_chain_noise_reset_cmd;
903 u8 phy_calib_chain_noise_gain_cmd;
904
905 /* counts reply_tx error */
906 struct reply_tx_error_statistics reply_tx_stats;
907 struct reply_agg_tx_error_statistics reply_agg_tx_stats;
908
909 /* remain-on-channel offload support */
910 struct ieee80211_channel *hw_roc_channel;
911 struct delayed_work hw_roc_disable_work;
912 enum nl80211_channel_type hw_roc_chantype;
913 int hw_roc_duration;
914 bool hw_roc_setup, hw_roc_start_notified;
915
916 /* bt coex */
917 u8 bt_enable_flag;
918 u8 bt_status;
919 u8 bt_traffic_load, last_bt_traffic_load;
920 bool bt_ch_announce;
921 bool bt_full_concurrent;
922 bool bt_ant_couple_ok;
923 __le32 kill_ack_mask;
924 __le32 kill_cts_mask;
925 __le16 bt_valid;
926 bool reduced_txpower;
927 u16 bt_on_thresh;
928 u16 bt_duration;
929 u16 dynamic_frag_thresh;
930 u8 bt_ci_compliance;
931 struct work_struct bt_traffic_change_work;
932 bool bt_enable_pspoll;
933 struct iwl_rxon_context *cur_rssi_ctx;
934 bool bt_is_sco;
935
936 struct work_struct restart;
937 struct work_struct scan_completed;
938 struct work_struct abort_scan;
939
940 struct work_struct beacon_update;
941 struct iwl_rxon_context *beacon_ctx;
942 struct sk_buff *beacon_skb;
943 void *beacon_cmd;
944
945 struct work_struct tt_work;
946 struct work_struct ct_enter;
947 struct work_struct ct_exit;
948 struct work_struct start_internal_scan;
949 struct work_struct tx_flush;
950 struct work_struct bt_full_concurrency;
951 struct work_struct bt_runtime_config;
952
953 struct delayed_work scan_check;
954
955 /* TX Power */
956 s8 tx_power_user_lmt;
957 s8 tx_power_device_lmt;
958 s8 tx_power_lmt_in_half_dbm; /* max tx power in half-dBm format */
959 s8 tx_power_next;
960
961#ifdef CONFIG_IWLWIFI_DEBUGFS
962 /* debugfs */
963 struct dentry *debugfs_dir;
964 u32 dbgfs_sram_offset, dbgfs_sram_len;
965 bool disable_ht40;
966 void *wowlan_sram;
967#endif /* CONFIG_IWLWIFI_DEBUGFS */
968
969 /* eeprom -- this is in the card's little endian byte order */
970 u8 *eeprom;
971 enum iwl_nvm_type nvm_device_type;
972
973 struct work_struct txpower_work;
974 u32 calib_disabled;
975 struct work_struct run_time_calib_work;
976 struct timer_list statistics_periodic;
977 struct timer_list ucode_trace;
978
979 struct iwl_event_log event_log;
980
981 struct led_classdev led;
982 unsigned long blink_on, blink_off;
983 bool led_registered;
984#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
985 struct iwl_testmode_trace testmode_trace;
986 struct iwl_testmode_mem testmode_mem;
987 u32 tm_fixed_rate;
988#endif
989
990 /* WoWLAN GTK rekey data */
991 u8 kck[NL80211_KCK_LEN], kek[NL80211_KEK_LEN];
992 __le64 replay_ctr;
993 __le16 last_seq_ctl;
994 bool have_rekey_data;
995
996 /* device_pointers: pointers to ucode event tables */
997 struct {
998 u32 error_event_table;
999 u32 log_event_table;
1000 } device_pointers;
1001
1002 /* indicator of loaded ucode image */
1003 enum iwl_ucode_type cur_ucode;
1004}; /*iwl_priv */
1005
1006extern struct kmem_cache *iwl_tx_cmd_pool;
1007
1008static inline struct iwl_rxon_context *
1009iwl_rxon_ctx_from_vif(struct ieee80211_vif *vif)
1010{
1011 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1012
1013 return vif_priv->ctx;
1014}
1015
1016#define for_each_context(priv, ctx) \
1017 for (ctx = &priv->contexts[IWL_RXON_CTX_BSS]; \
1018 ctx < &priv->contexts[NUM_IWL_RXON_CTX]; ctx++) \
1019 if (priv->valid_contexts & BIT(ctx->ctxid))
1020
1021static inline int iwl_is_associated_ctx(struct iwl_rxon_context *ctx)
1022{
1023 return (ctx->active.filter_flags & RXON_FILTER_ASSOC_MSK) ? 1 : 0;
1024}
1025
1026static inline int iwl_is_associated(struct iwl_priv *priv,
1027 enum iwl_rxon_context_id ctxid)
1028{
1029 return iwl_is_associated_ctx(&priv->contexts[ctxid]);
1030}
1031
1032static inline int iwl_is_any_associated(struct iwl_priv *priv)
1033{
1034 struct iwl_rxon_context *ctx;
1035 for_each_context(priv, ctx)
1036 if (iwl_is_associated_ctx(ctx))
1037 return true;
1038 return false;
1039}
1040
1041static inline int is_channel_valid(const struct iwl_channel_info *ch_info)
1042{
1043 if (ch_info == NULL)
1044 return 0;
1045 return (ch_info->flags & EEPROM_CHANNEL_VALID) ? 1 : 0;
1046}
1047
1048static inline int is_channel_radar(const struct iwl_channel_info *ch_info)
1049{
1050 return (ch_info->flags & EEPROM_CHANNEL_RADAR) ? 1 : 0;
1051}
1052
1053static inline u8 is_channel_a_band(const struct iwl_channel_info *ch_info)
1054{
1055 return ch_info->band == IEEE80211_BAND_5GHZ;
1056}
1057
1058static inline u8 is_channel_bg_band(const struct iwl_channel_info *ch_info)
1059{
1060 return ch_info->band == IEEE80211_BAND_2GHZ;
1061}
1062
1063static inline int is_channel_passive(const struct iwl_channel_info *ch)
1064{
1065 return (!(ch->flags & EEPROM_CHANNEL_ACTIVE)) ? 1 : 0;
1066}
1067
1068static inline int is_channel_ibss(const struct iwl_channel_info *ch)
1069{
1070 return ((ch->flags & EEPROM_CHANNEL_IBSS)) ? 1 : 0;
1071}
1072
1073#endif /* __iwl_dev_h__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/devices.c b/drivers/net/wireless/iwlwifi/dvm/devices.c
new file mode 100644
index 000000000000..ac5675d86ec3
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/devices.c
@@ -0,0 +1,740 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26
27/*
28 * DVM device-specific data & functions
29 */
30#include "iwl-io.h"
31#include "iwl-prph.h"
32
33#include "agn.h"
34#include "dev.h"
35#include "commands.h"
36
37/*
38 * 1000 series
39 * ===========
40 */
41
42/*
43 * For 1000, use advance thermal throttling critical temperature threshold,
44 * but legacy thermal management implementation for now.
45 * This is for the reason of 1000 uCode using advance thermal throttling API
46 * but not implement ct_kill_exit based on ct_kill exit temperature
47 * so the thermal throttling will still based on legacy thermal throttling
48 * management.
49 * The code here need to be modified once 1000 uCode has the advanced thermal
50 * throttling algorithm in place
51 */
52static void iwl1000_set_ct_threshold(struct iwl_priv *priv)
53{
54 /* want Celsius */
55 priv->hw_params.ct_kill_threshold = CT_KILL_THRESHOLD_LEGACY;
56 priv->hw_params.ct_kill_exit_threshold = CT_KILL_EXIT_THRESHOLD;
57}
58
59/* NIC configuration for 1000 series */
60static void iwl1000_nic_config(struct iwl_priv *priv)
61{
62 /* set CSR_HW_CONFIG_REG for uCode use */
63 iwl_set_bit(priv->trans, CSR_HW_IF_CONFIG_REG,
64 CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
65 CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
66
67 /* Setting digital SVR for 1000 card to 1.32V */
68 /* locking is acquired in iwl_set_bits_mask_prph() function */
69 iwl_set_bits_mask_prph(priv->trans, APMG_DIGITAL_SVR_REG,
70 APMG_SVR_DIGITAL_VOLTAGE_1_32,
71 ~APMG_SVR_VOLTAGE_CONFIG_BIT_MSK);
72}
73
74/**
75 * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
76 * @priv -- pointer to iwl_priv data structure
77 * @tsf_bits -- number of bits need to shift for masking)
78 */
79static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
80 u16 tsf_bits)
81{
82 return (1 << tsf_bits) - 1;
83}
84
85/**
86 * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
87 * @priv -- pointer to iwl_priv data structure
88 * @tsf_bits -- number of bits need to shift for masking)
89 */
90static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
91 u16 tsf_bits)
92{
93 return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
94}
95
96/*
97 * extended beacon time format
98 * time in usec will be changed into a 32-bit value in extended:internal format
99 * the extended part is the beacon counts
100 * the internal part is the time in usec within one beacon interval
101 */
102static u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec,
103 u32 beacon_interval)
104{
105 u32 quot;
106 u32 rem;
107 u32 interval = beacon_interval * TIME_UNIT;
108
109 if (!interval || !usec)
110 return 0;
111
112 quot = (usec / interval) &
113 (iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
114 IWLAGN_EXT_BEACON_TIME_POS);
115 rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
116 IWLAGN_EXT_BEACON_TIME_POS);
117
118 return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
119}
120
121/* base is usually what we get from ucode with each received frame,
122 * the same as HW timer counter counting down
123 */
124static __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
125 u32 addon, u32 beacon_interval)
126{
127 u32 base_low = base & iwl_beacon_time_mask_low(priv,
128 IWLAGN_EXT_BEACON_TIME_POS);
129 u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
130 IWLAGN_EXT_BEACON_TIME_POS);
131 u32 interval = beacon_interval * TIME_UNIT;
132 u32 res = (base & iwl_beacon_time_mask_high(priv,
133 IWLAGN_EXT_BEACON_TIME_POS)) +
134 (addon & iwl_beacon_time_mask_high(priv,
135 IWLAGN_EXT_BEACON_TIME_POS));
136
137 if (base_low > addon_low)
138 res += base_low - addon_low;
139 else if (base_low < addon_low) {
140 res += interval + base_low - addon_low;
141 res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
142 } else
143 res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
144
145 return cpu_to_le32(res);
146}
147
148static const struct iwl_sensitivity_ranges iwl1000_sensitivity = {
149 .min_nrg_cck = 95,
150 .auto_corr_min_ofdm = 90,
151 .auto_corr_min_ofdm_mrc = 170,
152 .auto_corr_min_ofdm_x1 = 120,
153 .auto_corr_min_ofdm_mrc_x1 = 240,
154
155 .auto_corr_max_ofdm = 120,
156 .auto_corr_max_ofdm_mrc = 210,
157 .auto_corr_max_ofdm_x1 = 155,
158 .auto_corr_max_ofdm_mrc_x1 = 290,
159
160 .auto_corr_min_cck = 125,
161 .auto_corr_max_cck = 200,
162 .auto_corr_min_cck_mrc = 170,
163 .auto_corr_max_cck_mrc = 400,
164 .nrg_th_cck = 95,
165 .nrg_th_ofdm = 95,
166
167 .barker_corr_th_min = 190,
168 .barker_corr_th_min_mrc = 390,
169 .nrg_th_cca = 62,
170};
171
172static void iwl1000_hw_set_hw_params(struct iwl_priv *priv)
173{
174 priv->hw_params.ht40_channel = BIT(IEEE80211_BAND_2GHZ);
175
176 priv->hw_params.tx_chains_num =
177 num_of_ant(priv->hw_params.valid_tx_ant);
178 if (priv->cfg->rx_with_siso_diversity)
179 priv->hw_params.rx_chains_num = 1;
180 else
181 priv->hw_params.rx_chains_num =
182 num_of_ant(priv->hw_params.valid_rx_ant);
183
184 iwl1000_set_ct_threshold(priv);
185
186 /* Set initial sensitivity parameters */
187 priv->hw_params.sens = &iwl1000_sensitivity;
188}
189
190struct iwl_lib_ops iwl1000_lib = {
191 .set_hw_params = iwl1000_hw_set_hw_params,
192 .nic_config = iwl1000_nic_config,
193 .eeprom_ops = {
194 .regulatory_bands = {
195 EEPROM_REG_BAND_1_CHANNELS,
196 EEPROM_REG_BAND_2_CHANNELS,
197 EEPROM_REG_BAND_3_CHANNELS,
198 EEPROM_REG_BAND_4_CHANNELS,
199 EEPROM_REG_BAND_5_CHANNELS,
200 EEPROM_REG_BAND_24_HT40_CHANNELS,
201 EEPROM_REGULATORY_BAND_NO_HT40,
202 },
203 },
204 .temperature = iwlagn_temperature,
205};
206
207
208/*
209 * 2000 series
210 * ===========
211 */
212
213static void iwl2000_set_ct_threshold(struct iwl_priv *priv)
214{
215 /* want Celsius */
216 priv->hw_params.ct_kill_threshold = CT_KILL_THRESHOLD;
217 priv->hw_params.ct_kill_exit_threshold = CT_KILL_EXIT_THRESHOLD;
218}
219
220/* NIC configuration for 2000 series */
221static void iwl2000_nic_config(struct iwl_priv *priv)
222{
223 iwl_rf_config(priv);
224
225 iwl_set_bit(priv->trans, CSR_GP_DRIVER_REG,
226 CSR_GP_DRIVER_REG_BIT_RADIO_IQ_INVER);
227}
228
229static const struct iwl_sensitivity_ranges iwl2000_sensitivity = {
230 .min_nrg_cck = 97,
231 .auto_corr_min_ofdm = 80,
232 .auto_corr_min_ofdm_mrc = 128,
233 .auto_corr_min_ofdm_x1 = 105,
234 .auto_corr_min_ofdm_mrc_x1 = 192,
235
236 .auto_corr_max_ofdm = 145,
237 .auto_corr_max_ofdm_mrc = 232,
238 .auto_corr_max_ofdm_x1 = 110,
239 .auto_corr_max_ofdm_mrc_x1 = 232,
240
241 .auto_corr_min_cck = 125,
242 .auto_corr_max_cck = 175,
243 .auto_corr_min_cck_mrc = 160,
244 .auto_corr_max_cck_mrc = 310,
245 .nrg_th_cck = 97,
246 .nrg_th_ofdm = 100,
247
248 .barker_corr_th_min = 190,
249 .barker_corr_th_min_mrc = 390,
250 .nrg_th_cca = 62,
251};
252
253static void iwl2000_hw_set_hw_params(struct iwl_priv *priv)
254{
255 priv->hw_params.ht40_channel = BIT(IEEE80211_BAND_2GHZ);
256
257 priv->hw_params.tx_chains_num =
258 num_of_ant(priv->hw_params.valid_tx_ant);
259 if (priv->cfg->rx_with_siso_diversity)
260 priv->hw_params.rx_chains_num = 1;
261 else
262 priv->hw_params.rx_chains_num =
263 num_of_ant(priv->hw_params.valid_rx_ant);
264
265 iwl2000_set_ct_threshold(priv);
266
267 /* Set initial sensitivity parameters */
268 priv->hw_params.sens = &iwl2000_sensitivity;
269}
270
271struct iwl_lib_ops iwl2000_lib = {
272 .set_hw_params = iwl2000_hw_set_hw_params,
273 .nic_config = iwl2000_nic_config,
274 .eeprom_ops = {
275 .regulatory_bands = {
276 EEPROM_REG_BAND_1_CHANNELS,
277 EEPROM_REG_BAND_2_CHANNELS,
278 EEPROM_REG_BAND_3_CHANNELS,
279 EEPROM_REG_BAND_4_CHANNELS,
280 EEPROM_REG_BAND_5_CHANNELS,
281 EEPROM_6000_REG_BAND_24_HT40_CHANNELS,
282 EEPROM_REGULATORY_BAND_NO_HT40,
283 },
284 .enhanced_txpower = true,
285 },
286 .temperature = iwlagn_temperature,
287};
288
289struct iwl_lib_ops iwl2030_lib = {
290 .set_hw_params = iwl2000_hw_set_hw_params,
291 .nic_config = iwl2000_nic_config,
292 .eeprom_ops = {
293 .regulatory_bands = {
294 EEPROM_REG_BAND_1_CHANNELS,
295 EEPROM_REG_BAND_2_CHANNELS,
296 EEPROM_REG_BAND_3_CHANNELS,
297 EEPROM_REG_BAND_4_CHANNELS,
298 EEPROM_REG_BAND_5_CHANNELS,
299 EEPROM_6000_REG_BAND_24_HT40_CHANNELS,
300 EEPROM_REGULATORY_BAND_NO_HT40,
301 },
302 .enhanced_txpower = true,
303 },
304 .temperature = iwlagn_temperature,
305};
306
307/*
308 * 5000 series
309 * ===========
310 */
311
312/* NIC configuration for 5000 series */
313static void iwl5000_nic_config(struct iwl_priv *priv)
314{
315 iwl_rf_config(priv);
316
317 /* W/A : NIC is stuck in a reset state after Early PCIe power off
318 * (PCIe power is lost before PERST# is asserted),
319 * causing ME FW to lose ownership and not being able to obtain it back.
320 */
321 iwl_set_bits_mask_prph(priv->trans, APMG_PS_CTRL_REG,
322 APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
323 ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
324}
325
326static const struct iwl_sensitivity_ranges iwl5000_sensitivity = {
327 .min_nrg_cck = 100,
328 .auto_corr_min_ofdm = 90,
329 .auto_corr_min_ofdm_mrc = 170,
330 .auto_corr_min_ofdm_x1 = 105,
331 .auto_corr_min_ofdm_mrc_x1 = 220,
332
333 .auto_corr_max_ofdm = 120,
334 .auto_corr_max_ofdm_mrc = 210,
335 .auto_corr_max_ofdm_x1 = 120,
336 .auto_corr_max_ofdm_mrc_x1 = 240,
337
338 .auto_corr_min_cck = 125,
339 .auto_corr_max_cck = 200,
340 .auto_corr_min_cck_mrc = 200,
341 .auto_corr_max_cck_mrc = 400,
342 .nrg_th_cck = 100,
343 .nrg_th_ofdm = 100,
344
345 .barker_corr_th_min = 190,
346 .barker_corr_th_min_mrc = 390,
347 .nrg_th_cca = 62,
348};
349
350static struct iwl_sensitivity_ranges iwl5150_sensitivity = {
351 .min_nrg_cck = 95,
352 .auto_corr_min_ofdm = 90,
353 .auto_corr_min_ofdm_mrc = 170,
354 .auto_corr_min_ofdm_x1 = 105,
355 .auto_corr_min_ofdm_mrc_x1 = 220,
356
357 .auto_corr_max_ofdm = 120,
358 .auto_corr_max_ofdm_mrc = 210,
359 /* max = min for performance bug in 5150 DSP */
360 .auto_corr_max_ofdm_x1 = 105,
361 .auto_corr_max_ofdm_mrc_x1 = 220,
362
363 .auto_corr_min_cck = 125,
364 .auto_corr_max_cck = 200,
365 .auto_corr_min_cck_mrc = 170,
366 .auto_corr_max_cck_mrc = 400,
367 .nrg_th_cck = 95,
368 .nrg_th_ofdm = 95,
369
370 .barker_corr_th_min = 190,
371 .barker_corr_th_min_mrc = 390,
372 .nrg_th_cca = 62,
373};
374
375#define IWL_5150_VOLTAGE_TO_TEMPERATURE_COEFF (-5)
376
377static s32 iwl_temp_calib_to_offset(struct iwl_priv *priv)
378{
379 u16 temperature, voltage;
380 __le16 *temp_calib = (__le16 *)iwl_eeprom_query_addr(priv,
381 EEPROM_KELVIN_TEMPERATURE);
382
383 temperature = le16_to_cpu(temp_calib[0]);
384 voltage = le16_to_cpu(temp_calib[1]);
385
386 /* offset = temp - volt / coeff */
387 return (s32)(temperature -
388 voltage / IWL_5150_VOLTAGE_TO_TEMPERATURE_COEFF);
389}
390
391static void iwl5150_set_ct_threshold(struct iwl_priv *priv)
392{
393 const s32 volt2temp_coef = IWL_5150_VOLTAGE_TO_TEMPERATURE_COEFF;
394 s32 threshold = (s32)CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD_LEGACY) -
395 iwl_temp_calib_to_offset(priv);
396
397 priv->hw_params.ct_kill_threshold = threshold * volt2temp_coef;
398}
399
400static void iwl5000_set_ct_threshold(struct iwl_priv *priv)
401{
402 /* want Celsius */
403 priv->hw_params.ct_kill_threshold = CT_KILL_THRESHOLD_LEGACY;
404}
405
406static void iwl5000_hw_set_hw_params(struct iwl_priv *priv)
407{
408 priv->hw_params.ht40_channel = BIT(IEEE80211_BAND_2GHZ) |
409 BIT(IEEE80211_BAND_5GHZ);
410
411 priv->hw_params.tx_chains_num =
412 num_of_ant(priv->hw_params.valid_tx_ant);
413 priv->hw_params.rx_chains_num =
414 num_of_ant(priv->hw_params.valid_rx_ant);
415
416 iwl5000_set_ct_threshold(priv);
417
418 /* Set initial sensitivity parameters */
419 priv->hw_params.sens = &iwl5000_sensitivity;
420}
421
422static void iwl5150_hw_set_hw_params(struct iwl_priv *priv)
423{
424 priv->hw_params.ht40_channel = BIT(IEEE80211_BAND_2GHZ) |
425 BIT(IEEE80211_BAND_5GHZ);
426
427 priv->hw_params.tx_chains_num =
428 num_of_ant(priv->hw_params.valid_tx_ant);
429 priv->hw_params.rx_chains_num =
430 num_of_ant(priv->hw_params.valid_rx_ant);
431
432 iwl5150_set_ct_threshold(priv);
433
434 /* Set initial sensitivity parameters */
435 priv->hw_params.sens = &iwl5150_sensitivity;
436}
437
438static void iwl5150_temperature(struct iwl_priv *priv)
439{
440 u32 vt = 0;
441 s32 offset = iwl_temp_calib_to_offset(priv);
442
443 vt = le32_to_cpu(priv->statistics.common.temperature);
444 vt = vt / IWL_5150_VOLTAGE_TO_TEMPERATURE_COEFF + offset;
445 /* now vt hold the temperature in Kelvin */
446 priv->temperature = KELVIN_TO_CELSIUS(vt);
447 iwl_tt_handler(priv);
448}
449
450static int iwl5000_hw_channel_switch(struct iwl_priv *priv,
451 struct ieee80211_channel_switch *ch_switch)
452{
453 /*
454 * MULTI-FIXME
455 * See iwlagn_mac_channel_switch.
456 */
457 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
458 struct iwl5000_channel_switch_cmd cmd;
459 u32 switch_time_in_usec, ucode_switch_time;
460 u16 ch;
461 u32 tsf_low;
462 u8 switch_count;
463 u16 beacon_interval = le16_to_cpu(ctx->timing.beacon_interval);
464 struct ieee80211_vif *vif = ctx->vif;
465 struct iwl_host_cmd hcmd = {
466 .id = REPLY_CHANNEL_SWITCH,
467 .len = { sizeof(cmd), },
468 .flags = CMD_SYNC,
469 .data = { &cmd, },
470 };
471
472 cmd.band = priv->band == IEEE80211_BAND_2GHZ;
473 ch = ch_switch->channel->hw_value;
474 IWL_DEBUG_11H(priv, "channel switch from %d to %d\n",
475 ctx->active.channel, ch);
476 cmd.channel = cpu_to_le16(ch);
477 cmd.rxon_flags = ctx->staging.flags;
478 cmd.rxon_filter_flags = ctx->staging.filter_flags;
479 switch_count = ch_switch->count;
480 tsf_low = ch_switch->timestamp & 0x0ffffffff;
481 /*
482 * calculate the ucode channel switch time
483 * adding TSF as one of the factor for when to switch
484 */
485 if ((priv->ucode_beacon_time > tsf_low) && beacon_interval) {
486 if (switch_count > ((priv->ucode_beacon_time - tsf_low) /
487 beacon_interval)) {
488 switch_count -= (priv->ucode_beacon_time -
489 tsf_low) / beacon_interval;
490 } else
491 switch_count = 0;
492 }
493 if (switch_count <= 1)
494 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
495 else {
496 switch_time_in_usec =
497 vif->bss_conf.beacon_int * switch_count * TIME_UNIT;
498 ucode_switch_time = iwl_usecs_to_beacons(priv,
499 switch_time_in_usec,
500 beacon_interval);
501 cmd.switch_time = iwl_add_beacon_time(priv,
502 priv->ucode_beacon_time,
503 ucode_switch_time,
504 beacon_interval);
505 }
506 IWL_DEBUG_11H(priv, "uCode time for the switch is 0x%x\n",
507 cmd.switch_time);
508 cmd.expect_beacon = ch_switch->channel->flags & IEEE80211_CHAN_RADAR;
509
510 return iwl_dvm_send_cmd(priv, &hcmd);
511}
512
513struct iwl_lib_ops iwl5000_lib = {
514 .set_hw_params = iwl5000_hw_set_hw_params,
515 .set_channel_switch = iwl5000_hw_channel_switch,
516 .nic_config = iwl5000_nic_config,
517 .eeprom_ops = {
518 .regulatory_bands = {
519 EEPROM_REG_BAND_1_CHANNELS,
520 EEPROM_REG_BAND_2_CHANNELS,
521 EEPROM_REG_BAND_3_CHANNELS,
522 EEPROM_REG_BAND_4_CHANNELS,
523 EEPROM_REG_BAND_5_CHANNELS,
524 EEPROM_REG_BAND_24_HT40_CHANNELS,
525 EEPROM_REG_BAND_52_HT40_CHANNELS
526 },
527 },
528 .temperature = iwlagn_temperature,
529};
530
531struct iwl_lib_ops iwl5150_lib = {
532 .set_hw_params = iwl5150_hw_set_hw_params,
533 .set_channel_switch = iwl5000_hw_channel_switch,
534 .nic_config = iwl5000_nic_config,
535 .eeprom_ops = {
536 .regulatory_bands = {
537 EEPROM_REG_BAND_1_CHANNELS,
538 EEPROM_REG_BAND_2_CHANNELS,
539 EEPROM_REG_BAND_3_CHANNELS,
540 EEPROM_REG_BAND_4_CHANNELS,
541 EEPROM_REG_BAND_5_CHANNELS,
542 EEPROM_REG_BAND_24_HT40_CHANNELS,
543 EEPROM_REG_BAND_52_HT40_CHANNELS
544 },
545 },
546 .temperature = iwl5150_temperature,
547};
548
549
550
551/*
552 * 6000 series
553 * ===========
554 */
555
556static void iwl6000_set_ct_threshold(struct iwl_priv *priv)
557{
558 /* want Celsius */
559 priv->hw_params.ct_kill_threshold = CT_KILL_THRESHOLD;
560 priv->hw_params.ct_kill_exit_threshold = CT_KILL_EXIT_THRESHOLD;
561}
562
563/* NIC configuration for 6000 series */
564static void iwl6000_nic_config(struct iwl_priv *priv)
565{
566 iwl_rf_config(priv);
567
568 switch (priv->cfg->device_family) {
569 case IWL_DEVICE_FAMILY_6005:
570 case IWL_DEVICE_FAMILY_6030:
571 case IWL_DEVICE_FAMILY_6000:
572 break;
573 case IWL_DEVICE_FAMILY_6000i:
574 /* 2x2 IPA phy type */
575 iwl_write32(priv->trans, CSR_GP_DRIVER_REG,
576 CSR_GP_DRIVER_REG_BIT_RADIO_SKU_2x2_IPA);
577 break;
578 case IWL_DEVICE_FAMILY_6050:
579 /* Indicate calibration version to uCode. */
580 if (iwl_eeprom_calib_version(priv) >= 6)
581 iwl_set_bit(priv->trans, CSR_GP_DRIVER_REG,
582 CSR_GP_DRIVER_REG_BIT_CALIB_VERSION6);
583 break;
584 case IWL_DEVICE_FAMILY_6150:
585 /* Indicate calibration version to uCode. */
586 if (iwl_eeprom_calib_version(priv) >= 6)
587 iwl_set_bit(priv->trans, CSR_GP_DRIVER_REG,
588 CSR_GP_DRIVER_REG_BIT_CALIB_VERSION6);
589 iwl_set_bit(priv->trans, CSR_GP_DRIVER_REG,
590 CSR_GP_DRIVER_REG_BIT_6050_1x2);
591 break;
592 default:
593 WARN_ON(1);
594 }
595}
596
597static const struct iwl_sensitivity_ranges iwl6000_sensitivity = {
598 .min_nrg_cck = 110,
599 .auto_corr_min_ofdm = 80,
600 .auto_corr_min_ofdm_mrc = 128,
601 .auto_corr_min_ofdm_x1 = 105,
602 .auto_corr_min_ofdm_mrc_x1 = 192,
603
604 .auto_corr_max_ofdm = 145,
605 .auto_corr_max_ofdm_mrc = 232,
606 .auto_corr_max_ofdm_x1 = 110,
607 .auto_corr_max_ofdm_mrc_x1 = 232,
608
609 .auto_corr_min_cck = 125,
610 .auto_corr_max_cck = 175,
611 .auto_corr_min_cck_mrc = 160,
612 .auto_corr_max_cck_mrc = 310,
613 .nrg_th_cck = 110,
614 .nrg_th_ofdm = 110,
615
616 .barker_corr_th_min = 190,
617 .barker_corr_th_min_mrc = 336,
618 .nrg_th_cca = 62,
619};
620
621static void iwl6000_hw_set_hw_params(struct iwl_priv *priv)
622{
623 priv->hw_params.ht40_channel = BIT(IEEE80211_BAND_2GHZ) |
624 BIT(IEEE80211_BAND_5GHZ);
625
626 priv->hw_params.tx_chains_num =
627 num_of_ant(priv->hw_params.valid_tx_ant);
628 if (priv->cfg->rx_with_siso_diversity)
629 priv->hw_params.rx_chains_num = 1;
630 else
631 priv->hw_params.rx_chains_num =
632 num_of_ant(priv->hw_params.valid_rx_ant);
633
634 iwl6000_set_ct_threshold(priv);
635
636 /* Set initial sensitivity parameters */
637 priv->hw_params.sens = &iwl6000_sensitivity;
638
639}
640
641static int iwl6000_hw_channel_switch(struct iwl_priv *priv,
642 struct ieee80211_channel_switch *ch_switch)
643{
644 /*
645 * MULTI-FIXME
646 * See iwlagn_mac_channel_switch.
647 */
648 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
649 struct iwl6000_channel_switch_cmd cmd;
650 u32 switch_time_in_usec, ucode_switch_time;
651 u16 ch;
652 u32 tsf_low;
653 u8 switch_count;
654 u16 beacon_interval = le16_to_cpu(ctx->timing.beacon_interval);
655 struct ieee80211_vif *vif = ctx->vif;
656 struct iwl_host_cmd hcmd = {
657 .id = REPLY_CHANNEL_SWITCH,
658 .len = { sizeof(cmd), },
659 .flags = CMD_SYNC,
660 .data = { &cmd, },
661 };
662
663 cmd.band = priv->band == IEEE80211_BAND_2GHZ;
664 ch = ch_switch->channel->hw_value;
665 IWL_DEBUG_11H(priv, "channel switch from %u to %u\n",
666 ctx->active.channel, ch);
667 cmd.channel = cpu_to_le16(ch);
668 cmd.rxon_flags = ctx->staging.flags;
669 cmd.rxon_filter_flags = ctx->staging.filter_flags;
670 switch_count = ch_switch->count;
671 tsf_low = ch_switch->timestamp & 0x0ffffffff;
672 /*
673 * calculate the ucode channel switch time
674 * adding TSF as one of the factor for when to switch
675 */
676 if ((priv->ucode_beacon_time > tsf_low) && beacon_interval) {
677 if (switch_count > ((priv->ucode_beacon_time - tsf_low) /
678 beacon_interval)) {
679 switch_count -= (priv->ucode_beacon_time -
680 tsf_low) / beacon_interval;
681 } else
682 switch_count = 0;
683 }
684 if (switch_count <= 1)
685 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
686 else {
687 switch_time_in_usec =
688 vif->bss_conf.beacon_int * switch_count * TIME_UNIT;
689 ucode_switch_time = iwl_usecs_to_beacons(priv,
690 switch_time_in_usec,
691 beacon_interval);
692 cmd.switch_time = iwl_add_beacon_time(priv,
693 priv->ucode_beacon_time,
694 ucode_switch_time,
695 beacon_interval);
696 }
697 IWL_DEBUG_11H(priv, "uCode time for the switch is 0x%x\n",
698 cmd.switch_time);
699 cmd.expect_beacon = ch_switch->channel->flags & IEEE80211_CHAN_RADAR;
700
701 return iwl_dvm_send_cmd(priv, &hcmd);
702}
703
704struct iwl_lib_ops iwl6000_lib = {
705 .set_hw_params = iwl6000_hw_set_hw_params,
706 .set_channel_switch = iwl6000_hw_channel_switch,
707 .nic_config = iwl6000_nic_config,
708 .eeprom_ops = {
709 .regulatory_bands = {
710 EEPROM_REG_BAND_1_CHANNELS,
711 EEPROM_REG_BAND_2_CHANNELS,
712 EEPROM_REG_BAND_3_CHANNELS,
713 EEPROM_REG_BAND_4_CHANNELS,
714 EEPROM_REG_BAND_5_CHANNELS,
715 EEPROM_6000_REG_BAND_24_HT40_CHANNELS,
716 EEPROM_REG_BAND_52_HT40_CHANNELS
717 },
718 .enhanced_txpower = true,
719 },
720 .temperature = iwlagn_temperature,
721};
722
723struct iwl_lib_ops iwl6030_lib = {
724 .set_hw_params = iwl6000_hw_set_hw_params,
725 .set_channel_switch = iwl6000_hw_channel_switch,
726 .nic_config = iwl6000_nic_config,
727 .eeprom_ops = {
728 .regulatory_bands = {
729 EEPROM_REG_BAND_1_CHANNELS,
730 EEPROM_REG_BAND_2_CHANNELS,
731 EEPROM_REG_BAND_3_CHANNELS,
732 EEPROM_REG_BAND_4_CHANNELS,
733 EEPROM_REG_BAND_5_CHANNELS,
734 EEPROM_6000_REG_BAND_24_HT40_CHANNELS,
735 EEPROM_REG_BAND_52_HT40_CHANNELS
736 },
737 .enhanced_txpower = true,
738 },
739 .temperature = iwlagn_temperature,
740};
diff --git a/drivers/net/wireless/iwlwifi/dvm/eeprom.c b/drivers/net/wireless/iwlwifi/dvm/eeprom.c
new file mode 100644
index 000000000000..69d280f8db33
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/eeprom.c
@@ -0,0 +1,1146 @@
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62
63
64#include <linux/kernel.h>
65#include <linux/module.h>
66#include <linux/slab.h>
67#include <linux/init.h>
68#include <net/mac80211.h>
69#include "iwl-io.h"
70#include "iwl-prph.h"
71#include "iwl-debug.h"
72#include "dev.h"
73#include "agn.h"
74#include "eeprom.h"
75
76/************************** EEPROM BANDS ****************************
77 *
78 * The iwl_eeprom_band definitions below provide the mapping from the
79 * EEPROM contents to the specific channel number supported for each
80 * band.
81 *
82 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
83 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
84 * The specific geography and calibration information for that channel
85 * is contained in the eeprom map itself.
86 *
87 * During init, we copy the eeprom information and channel map
88 * information into priv->channel_info_24/52 and priv->channel_map_24/52
89 *
90 * channel_map_24/52 provides the index in the channel_info array for a
91 * given channel. We have to have two separate maps as there is channel
92 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
93 * band_2
94 *
95 * A value of 0xff stored in the channel_map indicates that the channel
96 * is not supported by the hardware at all.
97 *
98 * A value of 0xfe in the channel_map indicates that the channel is not
99 * valid for Tx with the current hardware. This means that
100 * while the system can tune and receive on a given channel, it may not
101 * be able to associate or transmit any frames on that
102 * channel. There is no corresponding channel information for that
103 * entry.
104 *
105 *********************************************************************/
106
107/* 2.4 GHz */
108const u8 iwl_eeprom_band_1[14] = {
109 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
110};
111
112/* 5.2 GHz bands */
113static const u8 iwl_eeprom_band_2[] = { /* 4915-5080MHz */
114 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
115};
116
117static const u8 iwl_eeprom_band_3[] = { /* 5170-5320MHz */
118 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
119};
120
121static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
122 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
123};
124
125static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
126 145, 149, 153, 157, 161, 165
127};
128
129static const u8 iwl_eeprom_band_6[] = { /* 2.4 ht40 channel */
130 1, 2, 3, 4, 5, 6, 7
131};
132
133static const u8 iwl_eeprom_band_7[] = { /* 5.2 ht40 channel */
134 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
135};
136
137/******************************************************************************
138 *
139 * generic NVM functions
140 *
141******************************************************************************/
142
143/*
144 * The device's EEPROM semaphore prevents conflicts between driver and uCode
145 * when accessing the EEPROM; each access is a series of pulses to/from the
146 * EEPROM chip, not a single event, so even reads could conflict if they
147 * weren't arbitrated by the semaphore.
148 */
149
150#define EEPROM_SEM_TIMEOUT 10 /* milliseconds */
151#define EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
152
153static int iwl_eeprom_acquire_semaphore(struct iwl_trans *trans)
154{
155 u16 count;
156 int ret;
157
158 for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
159 /* Request semaphore */
160 iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
161 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
162
163 /* See if we got it */
164 ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
165 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
166 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
167 EEPROM_SEM_TIMEOUT);
168 if (ret >= 0) {
169 IWL_DEBUG_EEPROM(trans,
170 "Acquired semaphore after %d tries.\n",
171 count+1);
172 return ret;
173 }
174 }
175
176 return ret;
177}
178
179static void iwl_eeprom_release_semaphore(struct iwl_trans *trans)
180{
181 iwl_clear_bit(trans, CSR_HW_IF_CONFIG_REG,
182 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
183
184}
185
186static int iwl_eeprom_verify_signature(struct iwl_priv *priv)
187{
188 u32 gp = iwl_read32(priv->trans, CSR_EEPROM_GP) &
189 CSR_EEPROM_GP_VALID_MSK;
190 int ret = 0;
191
192 IWL_DEBUG_EEPROM(priv, "EEPROM signature=0x%08x\n", gp);
193 switch (gp) {
194 case CSR_EEPROM_GP_BAD_SIG_EEP_GOOD_SIG_OTP:
195 if (priv->nvm_device_type != NVM_DEVICE_TYPE_OTP) {
196 IWL_ERR(priv, "EEPROM with bad signature: 0x%08x\n",
197 gp);
198 ret = -ENOENT;
199 }
200 break;
201 case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
202 case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
203 if (priv->nvm_device_type != NVM_DEVICE_TYPE_EEPROM) {
204 IWL_ERR(priv, "OTP with bad signature: 0x%08x\n", gp);
205 ret = -ENOENT;
206 }
207 break;
208 case CSR_EEPROM_GP_BAD_SIGNATURE_BOTH_EEP_AND_OTP:
209 default:
210 IWL_ERR(priv, "bad EEPROM/OTP signature, type=%s, "
211 "EEPROM_GP=0x%08x\n",
212 (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
213 ? "OTP" : "EEPROM", gp);
214 ret = -ENOENT;
215 break;
216 }
217 return ret;
218}
219
220u16 iwl_eeprom_query16(struct iwl_priv *priv, size_t offset)
221{
222 if (!priv->eeprom)
223 return 0;
224 return (u16)priv->eeprom[offset] | ((u16)priv->eeprom[offset + 1] << 8);
225}
226
227int iwl_eeprom_check_version(struct iwl_priv *priv)
228{
229 u16 eeprom_ver;
230 u16 calib_ver;
231
232 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
233 calib_ver = iwl_eeprom_calib_version(priv);
234
235 if (eeprom_ver < priv->cfg->eeprom_ver ||
236 calib_ver < priv->cfg->eeprom_calib_ver)
237 goto err;
238
239 IWL_INFO(priv, "device EEPROM VER=0x%x, CALIB=0x%x\n",
240 eeprom_ver, calib_ver);
241
242 return 0;
243err:
244 IWL_ERR(priv, "Unsupported (too old) EEPROM VER=0x%x < 0x%x "
245 "CALIB=0x%x < 0x%x\n",
246 eeprom_ver, priv->cfg->eeprom_ver,
247 calib_ver, priv->cfg->eeprom_calib_ver);
248 return -EINVAL;
249
250}
251
252int iwl_eeprom_init_hw_params(struct iwl_priv *priv)
253{
254 u16 radio_cfg;
255
256 priv->hw_params.sku = iwl_eeprom_query16(priv, EEPROM_SKU_CAP);
257 if (priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE &&
258 !priv->cfg->ht_params) {
259 IWL_ERR(priv, "Invalid 11n configuration\n");
260 return -EINVAL;
261 }
262
263 if (!priv->hw_params.sku) {
264 IWL_ERR(priv, "Invalid device sku\n");
265 return -EINVAL;
266 }
267
268 IWL_INFO(priv, "Device SKU: 0x%X\n", priv->hw_params.sku);
269
270 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
271
272 priv->hw_params.valid_tx_ant = EEPROM_RF_CFG_TX_ANT_MSK(radio_cfg);
273 priv->hw_params.valid_rx_ant = EEPROM_RF_CFG_RX_ANT_MSK(radio_cfg);
274
275 /* check overrides (some devices have wrong EEPROM) */
276 if (priv->cfg->valid_tx_ant)
277 priv->hw_params.valid_tx_ant = priv->cfg->valid_tx_ant;
278 if (priv->cfg->valid_rx_ant)
279 priv->hw_params.valid_rx_ant = priv->cfg->valid_rx_ant;
280
281 if (!priv->hw_params.valid_tx_ant || !priv->hw_params.valid_rx_ant) {
282 IWL_ERR(priv, "Invalid chain (0x%X, 0x%X)\n",
283 priv->hw_params.valid_tx_ant,
284 priv->hw_params.valid_rx_ant);
285 return -EINVAL;
286 }
287
288 IWL_INFO(priv, "Valid Tx ant: 0x%X, Valid Rx ant: 0x%X\n",
289 priv->hw_params.valid_tx_ant, priv->hw_params.valid_rx_ant);
290
291 return 0;
292}
293
294u16 iwl_eeprom_calib_version(struct iwl_priv *priv)
295{
296 struct iwl_eeprom_calib_hdr *hdr;
297
298 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
299 EEPROM_CALIB_ALL);
300 return hdr->version;
301}
302
303static u32 eeprom_indirect_address(struct iwl_priv *priv, u32 address)
304{
305 u16 offset = 0;
306
307 if ((address & INDIRECT_ADDRESS) == 0)
308 return address;
309
310 switch (address & INDIRECT_TYPE_MSK) {
311 case INDIRECT_HOST:
312 offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
313 break;
314 case INDIRECT_GENERAL:
315 offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
316 break;
317 case INDIRECT_REGULATORY:
318 offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
319 break;
320 case INDIRECT_TXP_LIMIT:
321 offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT);
322 break;
323 case INDIRECT_TXP_LIMIT_SIZE:
324 offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT_SIZE);
325 break;
326 case INDIRECT_CALIBRATION:
327 offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
328 break;
329 case INDIRECT_PROCESS_ADJST:
330 offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
331 break;
332 case INDIRECT_OTHERS:
333 offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
334 break;
335 default:
336 IWL_ERR(priv, "illegal indirect type: 0x%X\n",
337 address & INDIRECT_TYPE_MSK);
338 break;
339 }
340
341 /* translate the offset from words to byte */
342 return (address & ADDRESS_MSK) + (offset << 1);
343}
344
345const u8 *iwl_eeprom_query_addr(struct iwl_priv *priv, size_t offset)
346{
347 u32 address = eeprom_indirect_address(priv, offset);
348 BUG_ON(address >= priv->cfg->base_params->eeprom_size);
349 return &priv->eeprom[address];
350}
351
352void iwl_eeprom_get_mac(struct iwl_priv *priv, u8 *mac)
353{
354 const u8 *addr = iwl_eeprom_query_addr(priv,
355 EEPROM_MAC_ADDRESS);
356 memcpy(mac, addr, ETH_ALEN);
357}
358
359/******************************************************************************
360 *
361 * OTP related functions
362 *
363******************************************************************************/
364
365static void iwl_set_otp_access(struct iwl_trans *trans,
366 enum iwl_access_mode mode)
367{
368 iwl_read32(trans, CSR_OTP_GP_REG);
369
370 if (mode == IWL_OTP_ACCESS_ABSOLUTE)
371 iwl_clear_bit(trans, CSR_OTP_GP_REG,
372 CSR_OTP_GP_REG_OTP_ACCESS_MODE);
373 else
374 iwl_set_bit(trans, CSR_OTP_GP_REG,
375 CSR_OTP_GP_REG_OTP_ACCESS_MODE);
376}
377
378static int iwl_get_nvm_type(struct iwl_trans *trans, u32 hw_rev)
379{
380 u32 otpgp;
381 int nvm_type;
382
383 /* OTP only valid for CP/PP and after */
384 switch (hw_rev & CSR_HW_REV_TYPE_MSK) {
385 case CSR_HW_REV_TYPE_NONE:
386 IWL_ERR(trans, "Unknown hardware type\n");
387 return -ENOENT;
388 case CSR_HW_REV_TYPE_5300:
389 case CSR_HW_REV_TYPE_5350:
390 case CSR_HW_REV_TYPE_5100:
391 case CSR_HW_REV_TYPE_5150:
392 nvm_type = NVM_DEVICE_TYPE_EEPROM;
393 break;
394 default:
395 otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
396 if (otpgp & CSR_OTP_GP_REG_DEVICE_SELECT)
397 nvm_type = NVM_DEVICE_TYPE_OTP;
398 else
399 nvm_type = NVM_DEVICE_TYPE_EEPROM;
400 break;
401 }
402 return nvm_type;
403}
404
405static int iwl_init_otp_access(struct iwl_trans *trans)
406{
407 int ret;
408
409 /* Enable 40MHz radio clock */
410 iwl_write32(trans, CSR_GP_CNTRL,
411 iwl_read32(trans, CSR_GP_CNTRL) |
412 CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
413
414 /* wait for clock to be ready */
415 ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
416 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
417 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
418 25000);
419 if (ret < 0)
420 IWL_ERR(trans, "Time out access OTP\n");
421 else {
422 iwl_set_bits_prph(trans, APMG_PS_CTRL_REG,
423 APMG_PS_CTRL_VAL_RESET_REQ);
424 udelay(5);
425 iwl_clear_bits_prph(trans, APMG_PS_CTRL_REG,
426 APMG_PS_CTRL_VAL_RESET_REQ);
427
428 /*
429 * CSR auto clock gate disable bit -
430 * this is only applicable for HW with OTP shadow RAM
431 */
432 if (trans->cfg->base_params->shadow_ram_support)
433 iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
434 CSR_RESET_LINK_PWR_MGMT_DISABLED);
435 }
436 return ret;
437}
438
439static int iwl_read_otp_word(struct iwl_trans *trans, u16 addr,
440 __le16 *eeprom_data)
441{
442 int ret = 0;
443 u32 r;
444 u32 otpgp;
445
446 iwl_write32(trans, CSR_EEPROM_REG,
447 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
448 ret = iwl_poll_bit(trans, CSR_EEPROM_REG,
449 CSR_EEPROM_REG_READ_VALID_MSK,
450 CSR_EEPROM_REG_READ_VALID_MSK,
451 IWL_EEPROM_ACCESS_TIMEOUT);
452 if (ret < 0) {
453 IWL_ERR(trans, "Time out reading OTP[%d]\n", addr);
454 return ret;
455 }
456 r = iwl_read32(trans, CSR_EEPROM_REG);
457 /* check for ECC errors: */
458 otpgp = iwl_read32(trans, CSR_OTP_GP_REG);
459 if (otpgp & CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK) {
460 /* stop in this case */
461 /* set the uncorrectable OTP ECC bit for acknowledgement */
462 iwl_set_bit(trans, CSR_OTP_GP_REG,
463 CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
464 IWL_ERR(trans, "Uncorrectable OTP ECC error, abort OTP read\n");
465 return -EINVAL;
466 }
467 if (otpgp & CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK) {
468 /* continue in this case */
469 /* set the correctable OTP ECC bit for acknowledgement */
470 iwl_set_bit(trans, CSR_OTP_GP_REG,
471 CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK);
472 IWL_ERR(trans, "Correctable OTP ECC error, continue read\n");
473 }
474 *eeprom_data = cpu_to_le16(r >> 16);
475 return 0;
476}
477
478/*
479 * iwl_is_otp_empty: check for empty OTP
480 */
481static bool iwl_is_otp_empty(struct iwl_trans *trans)
482{
483 u16 next_link_addr = 0;
484 __le16 link_value;
485 bool is_empty = false;
486
487 /* locate the beginning of OTP link list */
488 if (!iwl_read_otp_word(trans, next_link_addr, &link_value)) {
489 if (!link_value) {
490 IWL_ERR(trans, "OTP is empty\n");
491 is_empty = true;
492 }
493 } else {
494 IWL_ERR(trans, "Unable to read first block of OTP list.\n");
495 is_empty = true;
496 }
497
498 return is_empty;
499}
500
501
502/*
503 * iwl_find_otp_image: find EEPROM image in OTP
504 * finding the OTP block that contains the EEPROM image.
505 * the last valid block on the link list (the block _before_ the last block)
506 * is the block we should read and used to configure the device.
507 * If all the available OTP blocks are full, the last block will be the block
508 * we should read and used to configure the device.
509 * only perform this operation if shadow RAM is disabled
510 */
511static int iwl_find_otp_image(struct iwl_trans *trans,
512 u16 *validblockaddr)
513{
514 u16 next_link_addr = 0, valid_addr;
515 __le16 link_value = 0;
516 int usedblocks = 0;
517
518 /* set addressing mode to absolute to traverse the link list */
519 iwl_set_otp_access(trans, IWL_OTP_ACCESS_ABSOLUTE);
520
521 /* checking for empty OTP or error */
522 if (iwl_is_otp_empty(trans))
523 return -EINVAL;
524
525 /*
526 * start traverse link list
527 * until reach the max number of OTP blocks
528 * different devices have different number of OTP blocks
529 */
530 do {
531 /* save current valid block address
532 * check for more block on the link list
533 */
534 valid_addr = next_link_addr;
535 next_link_addr = le16_to_cpu(link_value) * sizeof(u16);
536 IWL_DEBUG_EEPROM(trans, "OTP blocks %d addr 0x%x\n",
537 usedblocks, next_link_addr);
538 if (iwl_read_otp_word(trans, next_link_addr, &link_value))
539 return -EINVAL;
540 if (!link_value) {
541 /*
542 * reach the end of link list, return success and
543 * set address point to the starting address
544 * of the image
545 */
546 *validblockaddr = valid_addr;
547 /* skip first 2 bytes (link list pointer) */
548 *validblockaddr += 2;
549 return 0;
550 }
551 /* more in the link list, continue */
552 usedblocks++;
553 } while (usedblocks <= trans->cfg->base_params->max_ll_items);
554
555 /* OTP has no valid blocks */
556 IWL_DEBUG_EEPROM(trans, "OTP has no valid blocks\n");
557 return -EINVAL;
558}
559
560/******************************************************************************
561 *
562 * Tx Power related functions
563 *
564******************************************************************************/
565/**
566 * iwl_get_max_txpower_avg - get the highest tx power from all chains.
567 * find the highest tx power from all chains for the channel
568 */
569static s8 iwl_get_max_txpower_avg(const struct iwl_cfg *cfg,
570 struct iwl_eeprom_enhanced_txpwr *enhanced_txpower,
571 int element, s8 *max_txpower_in_half_dbm)
572{
573 s8 max_txpower_avg = 0; /* (dBm) */
574
575 /* Take the highest tx power from any valid chains */
576 if ((cfg->valid_tx_ant & ANT_A) &&
577 (enhanced_txpower[element].chain_a_max > max_txpower_avg))
578 max_txpower_avg = enhanced_txpower[element].chain_a_max;
579 if ((cfg->valid_tx_ant & ANT_B) &&
580 (enhanced_txpower[element].chain_b_max > max_txpower_avg))
581 max_txpower_avg = enhanced_txpower[element].chain_b_max;
582 if ((cfg->valid_tx_ant & ANT_C) &&
583 (enhanced_txpower[element].chain_c_max > max_txpower_avg))
584 max_txpower_avg = enhanced_txpower[element].chain_c_max;
585 if (((cfg->valid_tx_ant == ANT_AB) |
586 (cfg->valid_tx_ant == ANT_BC) |
587 (cfg->valid_tx_ant == ANT_AC)) &&
588 (enhanced_txpower[element].mimo2_max > max_txpower_avg))
589 max_txpower_avg = enhanced_txpower[element].mimo2_max;
590 if ((cfg->valid_tx_ant == ANT_ABC) &&
591 (enhanced_txpower[element].mimo3_max > max_txpower_avg))
592 max_txpower_avg = enhanced_txpower[element].mimo3_max;
593
594 /*
595 * max. tx power in EEPROM is in 1/2 dBm format
596 * convert from 1/2 dBm to dBm (round-up convert)
597 * but we also do not want to loss 1/2 dBm resolution which
598 * will impact performance
599 */
600 *max_txpower_in_half_dbm = max_txpower_avg;
601 return (max_txpower_avg & 0x01) + (max_txpower_avg >> 1);
602}
603
604static void
605iwl_eeprom_enh_txp_read_element(struct iwl_priv *priv,
606 struct iwl_eeprom_enhanced_txpwr *txp,
607 s8 max_txpower_avg)
608{
609 int ch_idx;
610 bool is_ht40 = txp->flags & IWL_EEPROM_ENH_TXP_FL_40MHZ;
611 enum ieee80211_band band;
612
613 band = txp->flags & IWL_EEPROM_ENH_TXP_FL_BAND_52G ?
614 IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
615
616 for (ch_idx = 0; ch_idx < priv->channel_count; ch_idx++) {
617 struct iwl_channel_info *ch_info = &priv->channel_info[ch_idx];
618
619 /* update matching channel or from common data only */
620 if (txp->channel != 0 && ch_info->channel != txp->channel)
621 continue;
622
623 /* update matching band only */
624 if (band != ch_info->band)
625 continue;
626
627 if (ch_info->max_power_avg < max_txpower_avg && !is_ht40) {
628 ch_info->max_power_avg = max_txpower_avg;
629 ch_info->curr_txpow = max_txpower_avg;
630 ch_info->scan_power = max_txpower_avg;
631 }
632
633 if (is_ht40 && ch_info->ht40_max_power_avg < max_txpower_avg)
634 ch_info->ht40_max_power_avg = max_txpower_avg;
635 }
636}
637
638#define EEPROM_TXP_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT)
639#define EEPROM_TXP_ENTRY_LEN sizeof(struct iwl_eeprom_enhanced_txpwr)
640#define EEPROM_TXP_SZ_OFFS (0x00 | INDIRECT_ADDRESS | INDIRECT_TXP_LIMIT_SIZE)
641
642#define TXP_CHECK_AND_PRINT(x) ((txp->flags & IWL_EEPROM_ENH_TXP_FL_##x) \
643 ? # x " " : "")
644
645static void iwl_eeprom_enhanced_txpower(struct iwl_priv *priv)
646{
647 struct iwl_eeprom_enhanced_txpwr *txp_array, *txp;
648 int idx, entries;
649 __le16 *txp_len;
650 s8 max_txp_avg, max_txp_avg_halfdbm;
651
652 BUILD_BUG_ON(sizeof(struct iwl_eeprom_enhanced_txpwr) != 8);
653
654 /* the length is in 16-bit words, but we want entries */
655 txp_len = (__le16 *) iwl_eeprom_query_addr(priv, EEPROM_TXP_SZ_OFFS);
656 entries = le16_to_cpup(txp_len) * 2 / EEPROM_TXP_ENTRY_LEN;
657
658 txp_array = (void *) iwl_eeprom_query_addr(priv, EEPROM_TXP_OFFS);
659
660 for (idx = 0; idx < entries; idx++) {
661 txp = &txp_array[idx];
662 /* skip invalid entries */
663 if (!(txp->flags & IWL_EEPROM_ENH_TXP_FL_VALID))
664 continue;
665
666 IWL_DEBUG_EEPROM(priv, "%s %d:\t %s%s%s%s%s%s%s%s (0x%02x)\n",
667 (txp->channel && (txp->flags &
668 IWL_EEPROM_ENH_TXP_FL_COMMON_TYPE)) ?
669 "Common " : (txp->channel) ?
670 "Channel" : "Common",
671 (txp->channel),
672 TXP_CHECK_AND_PRINT(VALID),
673 TXP_CHECK_AND_PRINT(BAND_52G),
674 TXP_CHECK_AND_PRINT(OFDM),
675 TXP_CHECK_AND_PRINT(40MHZ),
676 TXP_CHECK_AND_PRINT(HT_AP),
677 TXP_CHECK_AND_PRINT(RES1),
678 TXP_CHECK_AND_PRINT(RES2),
679 TXP_CHECK_AND_PRINT(COMMON_TYPE),
680 txp->flags);
681 IWL_DEBUG_EEPROM(priv, "\t\t chain_A: 0x%02x "
682 "chain_B: 0X%02x chain_C: 0X%02x\n",
683 txp->chain_a_max, txp->chain_b_max,
684 txp->chain_c_max);
685 IWL_DEBUG_EEPROM(priv, "\t\t MIMO2: 0x%02x "
686 "MIMO3: 0x%02x High 20_on_40: 0x%02x "
687 "Low 20_on_40: 0x%02x\n",
688 txp->mimo2_max, txp->mimo3_max,
689 ((txp->delta_20_in_40 & 0xf0) >> 4),
690 (txp->delta_20_in_40 & 0x0f));
691
692 max_txp_avg = iwl_get_max_txpower_avg(priv->cfg, txp_array, idx,
693 &max_txp_avg_halfdbm);
694
695 /*
696 * Update the user limit values values to the highest
697 * power supported by any channel
698 */
699 if (max_txp_avg > priv->tx_power_user_lmt)
700 priv->tx_power_user_lmt = max_txp_avg;
701 if (max_txp_avg_halfdbm > priv->tx_power_lmt_in_half_dbm)
702 priv->tx_power_lmt_in_half_dbm = max_txp_avg_halfdbm;
703
704 iwl_eeprom_enh_txp_read_element(priv, txp, max_txp_avg);
705 }
706}
707
708/**
709 * iwl_eeprom_init - read EEPROM contents
710 *
711 * Load the EEPROM contents from adapter into priv->eeprom
712 *
713 * NOTE: This routine uses the non-debug IO access functions.
714 */
715int iwl_eeprom_init(struct iwl_priv *priv, u32 hw_rev)
716{
717 __le16 *e;
718 u32 gp = iwl_read32(priv->trans, CSR_EEPROM_GP);
719 int sz;
720 int ret;
721 u16 addr;
722 u16 validblockaddr = 0;
723 u16 cache_addr = 0;
724
725 priv->nvm_device_type = iwl_get_nvm_type(priv->trans, hw_rev);
726 if (priv->nvm_device_type == -ENOENT)
727 return -ENOENT;
728 /* allocate eeprom */
729 sz = priv->cfg->base_params->eeprom_size;
730 IWL_DEBUG_EEPROM(priv, "NVM size = %d\n", sz);
731 priv->eeprom = kzalloc(sz, GFP_KERNEL);
732 if (!priv->eeprom) {
733 ret = -ENOMEM;
734 goto alloc_err;
735 }
736 e = (__le16 *)priv->eeprom;
737
738 ret = iwl_eeprom_verify_signature(priv);
739 if (ret < 0) {
740 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
741 ret = -ENOENT;
742 goto err;
743 }
744
745 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
746 ret = iwl_eeprom_acquire_semaphore(priv->trans);
747 if (ret < 0) {
748 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
749 ret = -ENOENT;
750 goto err;
751 }
752
753 if (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP) {
754
755 ret = iwl_init_otp_access(priv->trans);
756 if (ret) {
757 IWL_ERR(priv, "Failed to initialize OTP access.\n");
758 ret = -ENOENT;
759 goto done;
760 }
761 iwl_write32(priv->trans, CSR_EEPROM_GP,
762 iwl_read32(priv->trans, CSR_EEPROM_GP) &
763 ~CSR_EEPROM_GP_IF_OWNER_MSK);
764
765 iwl_set_bit(priv->trans, CSR_OTP_GP_REG,
766 CSR_OTP_GP_REG_ECC_CORR_STATUS_MSK |
767 CSR_OTP_GP_REG_ECC_UNCORR_STATUS_MSK);
768 /* traversing the linked list if no shadow ram supported */
769 if (!priv->cfg->base_params->shadow_ram_support) {
770 if (iwl_find_otp_image(priv->trans, &validblockaddr)) {
771 ret = -ENOENT;
772 goto done;
773 }
774 }
775 for (addr = validblockaddr; addr < validblockaddr + sz;
776 addr += sizeof(u16)) {
777 __le16 eeprom_data;
778
779 ret = iwl_read_otp_word(priv->trans, addr,
780 &eeprom_data);
781 if (ret)
782 goto done;
783 e[cache_addr / 2] = eeprom_data;
784 cache_addr += sizeof(u16);
785 }
786 } else {
787 /* eeprom is an array of 16bit values */
788 for (addr = 0; addr < sz; addr += sizeof(u16)) {
789 u32 r;
790
791 iwl_write32(priv->trans, CSR_EEPROM_REG,
792 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
793
794 ret = iwl_poll_bit(priv->trans, CSR_EEPROM_REG,
795 CSR_EEPROM_REG_READ_VALID_MSK,
796 CSR_EEPROM_REG_READ_VALID_MSK,
797 IWL_EEPROM_ACCESS_TIMEOUT);
798 if (ret < 0) {
799 IWL_ERR(priv,
800 "Time out reading EEPROM[%d]\n", addr);
801 goto done;
802 }
803 r = iwl_read32(priv->trans, CSR_EEPROM_REG);
804 e[addr / 2] = cpu_to_le16(r >> 16);
805 }
806 }
807
808 IWL_DEBUG_EEPROM(priv, "NVM Type: %s, version: 0x%x\n",
809 (priv->nvm_device_type == NVM_DEVICE_TYPE_OTP)
810 ? "OTP" : "EEPROM",
811 iwl_eeprom_query16(priv, EEPROM_VERSION));
812
813 ret = 0;
814done:
815 iwl_eeprom_release_semaphore(priv->trans);
816
817err:
818 if (ret)
819 iwl_eeprom_free(priv);
820alloc_err:
821 return ret;
822}
823
824void iwl_eeprom_free(struct iwl_priv *priv)
825{
826 kfree(priv->eeprom);
827 priv->eeprom = NULL;
828}
829
830static void iwl_init_band_reference(struct iwl_priv *priv,
831 int eep_band, int *eeprom_ch_count,
832 const struct iwl_eeprom_channel **eeprom_ch_info,
833 const u8 **eeprom_ch_index)
834{
835 u32 offset = priv->lib->
836 eeprom_ops.regulatory_bands[eep_band - 1];
837 switch (eep_band) {
838 case 1: /* 2.4GHz band */
839 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
840 *eeprom_ch_info = (struct iwl_eeprom_channel *)
841 iwl_eeprom_query_addr(priv, offset);
842 *eeprom_ch_index = iwl_eeprom_band_1;
843 break;
844 case 2: /* 4.9GHz band */
845 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
846 *eeprom_ch_info = (struct iwl_eeprom_channel *)
847 iwl_eeprom_query_addr(priv, offset);
848 *eeprom_ch_index = iwl_eeprom_band_2;
849 break;
850 case 3: /* 5.2GHz band */
851 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
852 *eeprom_ch_info = (struct iwl_eeprom_channel *)
853 iwl_eeprom_query_addr(priv, offset);
854 *eeprom_ch_index = iwl_eeprom_band_3;
855 break;
856 case 4: /* 5.5GHz band */
857 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
858 *eeprom_ch_info = (struct iwl_eeprom_channel *)
859 iwl_eeprom_query_addr(priv, offset);
860 *eeprom_ch_index = iwl_eeprom_band_4;
861 break;
862 case 5: /* 5.7GHz band */
863 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
864 *eeprom_ch_info = (struct iwl_eeprom_channel *)
865 iwl_eeprom_query_addr(priv, offset);
866 *eeprom_ch_index = iwl_eeprom_band_5;
867 break;
868 case 6: /* 2.4GHz ht40 channels */
869 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_6);
870 *eeprom_ch_info = (struct iwl_eeprom_channel *)
871 iwl_eeprom_query_addr(priv, offset);
872 *eeprom_ch_index = iwl_eeprom_band_6;
873 break;
874 case 7: /* 5 GHz ht40 channels */
875 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_7);
876 *eeprom_ch_info = (struct iwl_eeprom_channel *)
877 iwl_eeprom_query_addr(priv, offset);
878 *eeprom_ch_index = iwl_eeprom_band_7;
879 break;
880 default:
881 BUG();
882 return;
883 }
884}
885
886#define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
887 ? # x " " : "")
888/**
889 * iwl_mod_ht40_chan_info - Copy ht40 channel info into driver's priv.
890 *
891 * Does not set up a command, or touch hardware.
892 */
893static int iwl_mod_ht40_chan_info(struct iwl_priv *priv,
894 enum ieee80211_band band, u16 channel,
895 const struct iwl_eeprom_channel *eeprom_ch,
896 u8 clear_ht40_extension_channel)
897{
898 struct iwl_channel_info *ch_info;
899
900 ch_info = (struct iwl_channel_info *)
901 iwl_get_channel_info(priv, band, channel);
902
903 if (!is_channel_valid(ch_info))
904 return -1;
905
906 IWL_DEBUG_EEPROM(priv, "HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm):"
907 " Ad-Hoc %ssupported\n",
908 ch_info->channel,
909 is_channel_a_band(ch_info) ?
910 "5.2" : "2.4",
911 CHECK_AND_PRINT(IBSS),
912 CHECK_AND_PRINT(ACTIVE),
913 CHECK_AND_PRINT(RADAR),
914 CHECK_AND_PRINT(WIDE),
915 CHECK_AND_PRINT(DFS),
916 eeprom_ch->flags,
917 eeprom_ch->max_power_avg,
918 ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS)
919 && !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ?
920 "" : "not ");
921
922 ch_info->ht40_eeprom = *eeprom_ch;
923 ch_info->ht40_max_power_avg = eeprom_ch->max_power_avg;
924 ch_info->ht40_flags = eeprom_ch->flags;
925 if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
926 ch_info->ht40_extension_channel &= ~clear_ht40_extension_channel;
927
928 return 0;
929}
930
931#define CHECK_AND_PRINT_I(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
932 ? # x " " : "")
933
934/**
935 * iwl_init_channel_map - Set up driver's info for all possible channels
936 */
937int iwl_init_channel_map(struct iwl_priv *priv)
938{
939 int eeprom_ch_count = 0;
940 const u8 *eeprom_ch_index = NULL;
941 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
942 int band, ch;
943 struct iwl_channel_info *ch_info;
944
945 if (priv->channel_count) {
946 IWL_DEBUG_EEPROM(priv, "Channel map already initialized.\n");
947 return 0;
948 }
949
950 IWL_DEBUG_EEPROM(priv, "Initializing regulatory info from EEPROM\n");
951
952 priv->channel_count =
953 ARRAY_SIZE(iwl_eeprom_band_1) +
954 ARRAY_SIZE(iwl_eeprom_band_2) +
955 ARRAY_SIZE(iwl_eeprom_band_3) +
956 ARRAY_SIZE(iwl_eeprom_band_4) +
957 ARRAY_SIZE(iwl_eeprom_band_5);
958
959 IWL_DEBUG_EEPROM(priv, "Parsing data for %d channels.\n",
960 priv->channel_count);
961
962 priv->channel_info = kcalloc(priv->channel_count,
963 sizeof(struct iwl_channel_info),
964 GFP_KERNEL);
965 if (!priv->channel_info) {
966 IWL_ERR(priv, "Could not allocate channel_info\n");
967 priv->channel_count = 0;
968 return -ENOMEM;
969 }
970
971 ch_info = priv->channel_info;
972
973 /* Loop through the 5 EEPROM bands adding them in order to the
974 * channel map we maintain (that contains additional information than
975 * what just in the EEPROM) */
976 for (band = 1; band <= 5; band++) {
977
978 iwl_init_band_reference(priv, band, &eeprom_ch_count,
979 &eeprom_ch_info, &eeprom_ch_index);
980
981 /* Loop through each band adding each of the channels */
982 for (ch = 0; ch < eeprom_ch_count; ch++) {
983 ch_info->channel = eeprom_ch_index[ch];
984 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
985 IEEE80211_BAND_5GHZ;
986
987 /* permanently store EEPROM's channel regulatory flags
988 * and max power in channel info database. */
989 ch_info->eeprom = eeprom_ch_info[ch];
990
991 /* Copy the run-time flags so they are there even on
992 * invalid channels */
993 ch_info->flags = eeprom_ch_info[ch].flags;
994 /* First write that ht40 is not enabled, and then enable
995 * one by one */
996 ch_info->ht40_extension_channel =
997 IEEE80211_CHAN_NO_HT40;
998
999 if (!(is_channel_valid(ch_info))) {
1000 IWL_DEBUG_EEPROM(priv,
1001 "Ch. %d Flags %x [%sGHz] - "
1002 "No traffic\n",
1003 ch_info->channel,
1004 ch_info->flags,
1005 is_channel_a_band(ch_info) ?
1006 "5.2" : "2.4");
1007 ch_info++;
1008 continue;
1009 }
1010
1011 /* Initialize regulatory-based run-time data */
1012 ch_info->max_power_avg = ch_info->curr_txpow =
1013 eeprom_ch_info[ch].max_power_avg;
1014 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
1015 ch_info->min_power = 0;
1016
1017 IWL_DEBUG_EEPROM(priv, "Ch. %d [%sGHz] "
1018 "%s%s%s%s%s%s(0x%02x %ddBm):"
1019 " Ad-Hoc %ssupported\n",
1020 ch_info->channel,
1021 is_channel_a_band(ch_info) ?
1022 "5.2" : "2.4",
1023 CHECK_AND_PRINT_I(VALID),
1024 CHECK_AND_PRINT_I(IBSS),
1025 CHECK_AND_PRINT_I(ACTIVE),
1026 CHECK_AND_PRINT_I(RADAR),
1027 CHECK_AND_PRINT_I(WIDE),
1028 CHECK_AND_PRINT_I(DFS),
1029 eeprom_ch_info[ch].flags,
1030 eeprom_ch_info[ch].max_power_avg,
1031 ((eeprom_ch_info[ch].
1032 flags & EEPROM_CHANNEL_IBSS)
1033 && !(eeprom_ch_info[ch].
1034 flags & EEPROM_CHANNEL_RADAR))
1035 ? "" : "not ");
1036
1037 ch_info++;
1038 }
1039 }
1040
1041 /* Check if we do have HT40 channels */
1042 if (priv->lib->eeprom_ops.regulatory_bands[5] ==
1043 EEPROM_REGULATORY_BAND_NO_HT40 &&
1044 priv->lib->eeprom_ops.regulatory_bands[6] ==
1045 EEPROM_REGULATORY_BAND_NO_HT40)
1046 return 0;
1047
1048 /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
1049 for (band = 6; band <= 7; band++) {
1050 enum ieee80211_band ieeeband;
1051
1052 iwl_init_band_reference(priv, band, &eeprom_ch_count,
1053 &eeprom_ch_info, &eeprom_ch_index);
1054
1055 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
1056 ieeeband =
1057 (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
1058
1059 /* Loop through each band adding each of the channels */
1060 for (ch = 0; ch < eeprom_ch_count; ch++) {
1061 /* Set up driver's info for lower half */
1062 iwl_mod_ht40_chan_info(priv, ieeeband,
1063 eeprom_ch_index[ch],
1064 &eeprom_ch_info[ch],
1065 IEEE80211_CHAN_NO_HT40PLUS);
1066
1067 /* Set up driver's info for upper half */
1068 iwl_mod_ht40_chan_info(priv, ieeeband,
1069 eeprom_ch_index[ch] + 4,
1070 &eeprom_ch_info[ch],
1071 IEEE80211_CHAN_NO_HT40MINUS);
1072 }
1073 }
1074
1075 /* for newer device (6000 series and up)
1076 * EEPROM contain enhanced tx power information
1077 * driver need to process addition information
1078 * to determine the max channel tx power limits
1079 */
1080 if (priv->lib->eeprom_ops.enhanced_txpower)
1081 iwl_eeprom_enhanced_txpower(priv);
1082
1083 return 0;
1084}
1085
1086/*
1087 * iwl_free_channel_map - undo allocations in iwl_init_channel_map
1088 */
1089void iwl_free_channel_map(struct iwl_priv *priv)
1090{
1091 kfree(priv->channel_info);
1092 priv->channel_count = 0;
1093}
1094
1095/**
1096 * iwl_get_channel_info - Find driver's private channel info
1097 *
1098 * Based on band and channel number.
1099 */
1100const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
1101 enum ieee80211_band band, u16 channel)
1102{
1103 int i;
1104
1105 switch (band) {
1106 case IEEE80211_BAND_5GHZ:
1107 for (i = 14; i < priv->channel_count; i++) {
1108 if (priv->channel_info[i].channel == channel)
1109 return &priv->channel_info[i];
1110 }
1111 break;
1112 case IEEE80211_BAND_2GHZ:
1113 if (channel >= 1 && channel <= 14)
1114 return &priv->channel_info[channel - 1];
1115 break;
1116 default:
1117 BUG();
1118 }
1119
1120 return NULL;
1121}
1122
1123void iwl_rf_config(struct iwl_priv *priv)
1124{
1125 u16 radio_cfg;
1126
1127 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
1128
1129 /* write radio config values to register */
1130 if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) <= EEPROM_RF_CONFIG_TYPE_MAX) {
1131 iwl_set_bit(priv->trans, CSR_HW_IF_CONFIG_REG,
1132 EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
1133 EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
1134 EEPROM_RF_CFG_DASH_MSK(radio_cfg));
1135 IWL_INFO(priv, "Radio type=0x%x-0x%x-0x%x\n",
1136 EEPROM_RF_CFG_TYPE_MSK(radio_cfg),
1137 EEPROM_RF_CFG_STEP_MSK(radio_cfg),
1138 EEPROM_RF_CFG_DASH_MSK(radio_cfg));
1139 } else
1140 WARN_ON(1);
1141
1142 /* set CSR_HW_CONFIG_REG for uCode use */
1143 iwl_set_bit(priv->trans, CSR_HW_IF_CONFIG_REG,
1144 CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
1145 CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
1146}
diff --git a/drivers/net/wireless/iwlwifi/dvm/eeprom.h b/drivers/net/wireless/iwlwifi/dvm/eeprom.h
new file mode 100644
index 000000000000..64bfd947caeb
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/eeprom.h
@@ -0,0 +1,269 @@
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62
63#ifndef __iwl_eeprom_h__
64#define __iwl_eeprom_h__
65
66#include <net/mac80211.h>
67
68struct iwl_priv;
69
70/*
71 * EEPROM access time values:
72 *
73 * Driver initiates EEPROM read by writing byte address << 1 to CSR_EEPROM_REG.
74 * Driver then polls CSR_EEPROM_REG for CSR_EEPROM_REG_READ_VALID_MSK (0x1).
75 * When polling, wait 10 uSec between polling loops, up to a maximum 5000 uSec.
76 * Driver reads 16-bit value from bits 31-16 of CSR_EEPROM_REG.
77 */
78#define IWL_EEPROM_ACCESS_TIMEOUT 5000 /* uSec */
79
80#define IWL_EEPROM_SEM_TIMEOUT 10 /* microseconds */
81#define IWL_EEPROM_SEM_RETRY_LIMIT 1000 /* number of attempts (not time) */
82
83
84/*
85 * Regulatory channel usage flags in EEPROM struct iwl4965_eeprom_channel.flags.
86 *
87 * IBSS and/or AP operation is allowed *only* on those channels with
88 * (VALID && IBSS && ACTIVE && !RADAR). This restriction is in place because
89 * RADAR detection is not supported by the 4965 driver, but is a
90 * requirement for establishing a new network for legal operation on channels
91 * requiring RADAR detection or restricting ACTIVE scanning.
92 *
93 * NOTE: "WIDE" flag does not indicate anything about "HT40" 40 MHz channels.
94 * It only indicates that 20 MHz channel use is supported; HT40 channel
95 * usage is indicated by a separate set of regulatory flags for each
96 * HT40 channel pair.
97 *
98 * NOTE: Using a channel inappropriately will result in a uCode error!
99 */
100#define IWL_NUM_TX_CALIB_GROUPS 5
101enum {
102 EEPROM_CHANNEL_VALID = (1 << 0), /* usable for this SKU/geo */
103 EEPROM_CHANNEL_IBSS = (1 << 1), /* usable as an IBSS channel */
104 /* Bit 2 Reserved */
105 EEPROM_CHANNEL_ACTIVE = (1 << 3), /* active scanning allowed */
106 EEPROM_CHANNEL_RADAR = (1 << 4), /* radar detection required */
107 EEPROM_CHANNEL_WIDE = (1 << 5), /* 20 MHz channel okay */
108 /* Bit 6 Reserved (was Narrow Channel) */
109 EEPROM_CHANNEL_DFS = (1 << 7), /* dynamic freq selection candidate */
110};
111
112/* SKU Capabilities */
113#define EEPROM_SKU_CAP_BAND_24GHZ (1 << 4)
114#define EEPROM_SKU_CAP_BAND_52GHZ (1 << 5)
115#define EEPROM_SKU_CAP_11N_ENABLE (1 << 6)
116#define EEPROM_SKU_CAP_AMT_ENABLE (1 << 7)
117#define EEPROM_SKU_CAP_IPAN_ENABLE (1 << 8)
118
119/* *regulatory* channel data format in eeprom, one for each channel.
120 * There are separate entries for HT40 (40 MHz) vs. normal (20 MHz) channels. */
121struct iwl_eeprom_channel {
122 u8 flags; /* EEPROM_CHANNEL_* flags copied from EEPROM */
123 s8 max_power_avg; /* max power (dBm) on this chnl, limit 31 */
124} __packed;
125
126enum iwl_eeprom_enhanced_txpwr_flags {
127 IWL_EEPROM_ENH_TXP_FL_VALID = BIT(0),
128 IWL_EEPROM_ENH_TXP_FL_BAND_52G = BIT(1),
129 IWL_EEPROM_ENH_TXP_FL_OFDM = BIT(2),
130 IWL_EEPROM_ENH_TXP_FL_40MHZ = BIT(3),
131 IWL_EEPROM_ENH_TXP_FL_HT_AP = BIT(4),
132 IWL_EEPROM_ENH_TXP_FL_RES1 = BIT(5),
133 IWL_EEPROM_ENH_TXP_FL_RES2 = BIT(6),
134 IWL_EEPROM_ENH_TXP_FL_COMMON_TYPE = BIT(7),
135};
136
137/**
138 * iwl_eeprom_enhanced_txpwr structure
139 * This structure presents the enhanced regulatory tx power limit layout
140 * in eeprom image
141 * Enhanced regulatory tx power portion of eeprom image can be broken down
142 * into individual structures; each one is 8 bytes in size and contain the
143 * following information
144 * @flags: entry flags
145 * @channel: channel number
146 * @chain_a_max_pwr: chain a max power in 1/2 dBm
147 * @chain_b_max_pwr: chain b max power in 1/2 dBm
148 * @chain_c_max_pwr: chain c max power in 1/2 dBm
149 * @delta_20_in_40: 20-in-40 deltas (hi/lo)
150 * @mimo2_max_pwr: mimo2 max power in 1/2 dBm
151 * @mimo3_max_pwr: mimo3 max power in 1/2 dBm
152 *
153 */
154struct iwl_eeprom_enhanced_txpwr {
155 u8 flags;
156 u8 channel;
157 s8 chain_a_max;
158 s8 chain_b_max;
159 s8 chain_c_max;
160 u8 delta_20_in_40;
161 s8 mimo2_max;
162 s8 mimo3_max;
163} __packed;
164
165/* calibration */
166struct iwl_eeprom_calib_hdr {
167 u8 version;
168 u8 pa_type;
169 __le16 voltage;
170} __packed;
171
172#define EEPROM_CALIB_ALL (INDIRECT_ADDRESS | INDIRECT_CALIBRATION)
173#define EEPROM_XTAL ((2*0x128) | EEPROM_CALIB_ALL)
174
175/* temperature */
176#define EEPROM_KELVIN_TEMPERATURE ((2*0x12A) | EEPROM_CALIB_ALL)
177#define EEPROM_RAW_TEMPERATURE ((2*0x12B) | EEPROM_CALIB_ALL)
178
179
180/* agn links */
181#define EEPROM_LINK_HOST (2*0x64)
182#define EEPROM_LINK_GENERAL (2*0x65)
183#define EEPROM_LINK_REGULATORY (2*0x66)
184#define EEPROM_LINK_CALIBRATION (2*0x67)
185#define EEPROM_LINK_PROCESS_ADJST (2*0x68)
186#define EEPROM_LINK_OTHERS (2*0x69)
187#define EEPROM_LINK_TXP_LIMIT (2*0x6a)
188#define EEPROM_LINK_TXP_LIMIT_SIZE (2*0x6b)
189
190/* agn regulatory - indirect access */
191#define EEPROM_REG_BAND_1_CHANNELS ((0x08)\
192 | INDIRECT_ADDRESS | INDIRECT_REGULATORY) /* 28 bytes */
193#define EEPROM_REG_BAND_2_CHANNELS ((0x26)\
194 | INDIRECT_ADDRESS | INDIRECT_REGULATORY) /* 26 bytes */
195#define EEPROM_REG_BAND_3_CHANNELS ((0x42)\
196 | INDIRECT_ADDRESS | INDIRECT_REGULATORY) /* 24 bytes */
197#define EEPROM_REG_BAND_4_CHANNELS ((0x5C)\
198 | INDIRECT_ADDRESS | INDIRECT_REGULATORY) /* 22 bytes */
199#define EEPROM_REG_BAND_5_CHANNELS ((0x74)\
200 | INDIRECT_ADDRESS | INDIRECT_REGULATORY) /* 12 bytes */
201#define EEPROM_REG_BAND_24_HT40_CHANNELS ((0x82)\
202 | INDIRECT_ADDRESS | INDIRECT_REGULATORY) /* 14 bytes */
203#define EEPROM_REG_BAND_52_HT40_CHANNELS ((0x92)\
204 | INDIRECT_ADDRESS | INDIRECT_REGULATORY) /* 22 bytes */
205
206/* 6000 regulatory - indirect access */
207#define EEPROM_6000_REG_BAND_24_HT40_CHANNELS ((0x80)\
208 | INDIRECT_ADDRESS | INDIRECT_REGULATORY) /* 14 bytes */
209/* 2.4 GHz */
210extern const u8 iwl_eeprom_band_1[14];
211
212#define ADDRESS_MSK 0x0000FFFF
213#define INDIRECT_TYPE_MSK 0x000F0000
214#define INDIRECT_HOST 0x00010000
215#define INDIRECT_GENERAL 0x00020000
216#define INDIRECT_REGULATORY 0x00030000
217#define INDIRECT_CALIBRATION 0x00040000
218#define INDIRECT_PROCESS_ADJST 0x00050000
219#define INDIRECT_OTHERS 0x00060000
220#define INDIRECT_TXP_LIMIT 0x00070000
221#define INDIRECT_TXP_LIMIT_SIZE 0x00080000
222#define INDIRECT_ADDRESS 0x00100000
223
224/* General */
225#define EEPROM_DEVICE_ID (2*0x08) /* 2 bytes */
226#define EEPROM_SUBSYSTEM_ID (2*0x0A) /* 2 bytes */
227#define EEPROM_MAC_ADDRESS (2*0x15) /* 6 bytes */
228#define EEPROM_BOARD_REVISION (2*0x35) /* 2 bytes */
229#define EEPROM_BOARD_PBA_NUMBER (2*0x3B+1) /* 9 bytes */
230#define EEPROM_VERSION (2*0x44) /* 2 bytes */
231#define EEPROM_SKU_CAP (2*0x45) /* 2 bytes */
232#define EEPROM_OEM_MODE (2*0x46) /* 2 bytes */
233#define EEPROM_RADIO_CONFIG (2*0x48) /* 2 bytes */
234#define EEPROM_NUM_MAC_ADDRESS (2*0x4C) /* 2 bytes */
235
236/* The following masks are to be applied on EEPROM_RADIO_CONFIG */
237#define EEPROM_RF_CFG_TYPE_MSK(x) (x & 0x3) /* bits 0-1 */
238#define EEPROM_RF_CFG_STEP_MSK(x) ((x >> 2) & 0x3) /* bits 2-3 */
239#define EEPROM_RF_CFG_DASH_MSK(x) ((x >> 4) & 0x3) /* bits 4-5 */
240#define EEPROM_RF_CFG_PNUM_MSK(x) ((x >> 6) & 0x3) /* bits 6-7 */
241#define EEPROM_RF_CFG_TX_ANT_MSK(x) ((x >> 8) & 0xF) /* bits 8-11 */
242#define EEPROM_RF_CFG_RX_ANT_MSK(x) ((x >> 12) & 0xF) /* bits 12-15 */
243
244#define EEPROM_RF_CONFIG_TYPE_MAX 0x3
245
246#define EEPROM_REGULATORY_BAND_NO_HT40 (0)
247
248struct iwl_eeprom_ops {
249 const u32 regulatory_bands[7];
250 bool enhanced_txpower;
251};
252
253
254int iwl_eeprom_init(struct iwl_priv *priv, u32 hw_rev);
255void iwl_eeprom_free(struct iwl_priv *priv);
256int iwl_eeprom_check_version(struct iwl_priv *priv);
257int iwl_eeprom_init_hw_params(struct iwl_priv *priv);
258u16 iwl_eeprom_calib_version(struct iwl_priv *priv);
259const u8 *iwl_eeprom_query_addr(struct iwl_priv *priv, size_t offset);
260u16 iwl_eeprom_query16(struct iwl_priv *priv, size_t offset);
261void iwl_eeprom_get_mac(struct iwl_priv *priv, u8 *mac);
262int iwl_init_channel_map(struct iwl_priv *priv);
263void iwl_free_channel_map(struct iwl_priv *priv);
264const struct iwl_channel_info *iwl_get_channel_info(
265 const struct iwl_priv *priv,
266 enum ieee80211_band band, u16 channel);
267void iwl_rf_config(struct iwl_priv *priv);
268
269#endif /* __iwl_eeprom_h__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/led.c b/drivers/net/wireless/iwlwifi/dvm/led.c
new file mode 100644
index 000000000000..bf479f709091
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/led.c
@@ -0,0 +1,224 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/delay.h>
32#include <linux/skbuff.h>
33#include <linux/netdevice.h>
34#include <net/mac80211.h>
35#include <linux/etherdevice.h>
36#include <asm/unaligned.h>
37#include "iwl-io.h"
38#include "iwl-trans.h"
39#include "iwl-modparams.h"
40#include "dev.h"
41#include "agn.h"
42
43/* Throughput OFF time(ms) ON time (ms)
44 * >300 25 25
45 * >200 to 300 40 40
46 * >100 to 200 55 55
47 * >70 to 100 65 65
48 * >50 to 70 75 75
49 * >20 to 50 85 85
50 * >10 to 20 95 95
51 * >5 to 10 110 110
52 * >1 to 5 130 130
53 * >0 to 1 167 167
54 * <=0 SOLID ON
55 */
56static const struct ieee80211_tpt_blink iwl_blink[] = {
57 { .throughput = 0, .blink_time = 334 },
58 { .throughput = 1 * 1024 - 1, .blink_time = 260 },
59 { .throughput = 5 * 1024 - 1, .blink_time = 220 },
60 { .throughput = 10 * 1024 - 1, .blink_time = 190 },
61 { .throughput = 20 * 1024 - 1, .blink_time = 170 },
62 { .throughput = 50 * 1024 - 1, .blink_time = 150 },
63 { .throughput = 70 * 1024 - 1, .blink_time = 130 },
64 { .throughput = 100 * 1024 - 1, .blink_time = 110 },
65 { .throughput = 200 * 1024 - 1, .blink_time = 80 },
66 { .throughput = 300 * 1024 - 1, .blink_time = 50 },
67};
68
69/* Set led register off */
70void iwlagn_led_enable(struct iwl_priv *priv)
71{
72 iwl_write32(priv->trans, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
73}
74
75/*
76 * Adjust led blink rate to compensate on a MAC Clock difference on every HW
77 * Led blink rate analysis showed an average deviation of 20% on 5000 series
78 * and up.
79 * Need to compensate on the led on/off time per HW according to the deviation
80 * to achieve the desired led frequency
81 * The calculation is: (100-averageDeviation)/100 * blinkTime
82 * For code efficiency the calculation will be:
83 * compensation = (100 - averageDeviation) * 64 / 100
84 * NewBlinkTime = (compensation * BlinkTime) / 64
85 */
86static inline u8 iwl_blink_compensation(struct iwl_priv *priv,
87 u8 time, u16 compensation)
88{
89 if (!compensation) {
90 IWL_ERR(priv, "undefined blink compensation: "
91 "use pre-defined blinking time\n");
92 return time;
93 }
94
95 return (u8)((time * compensation) >> 6);
96}
97
98static int iwl_send_led_cmd(struct iwl_priv *priv, struct iwl_led_cmd *led_cmd)
99{
100 struct iwl_host_cmd cmd = {
101 .id = REPLY_LEDS_CMD,
102 .len = { sizeof(struct iwl_led_cmd), },
103 .data = { led_cmd, },
104 .flags = CMD_ASYNC,
105 };
106 u32 reg;
107
108 reg = iwl_read32(priv->trans, CSR_LED_REG);
109 if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
110 iwl_write32(priv->trans, CSR_LED_REG,
111 reg & CSR_LED_BSM_CTRL_MSK);
112
113 return iwl_dvm_send_cmd(priv, &cmd);
114}
115
116/* Set led pattern command */
117static int iwl_led_cmd(struct iwl_priv *priv,
118 unsigned long on,
119 unsigned long off)
120{
121 struct iwl_led_cmd led_cmd = {
122 .id = IWL_LED_LINK,
123 .interval = IWL_DEF_LED_INTRVL
124 };
125 int ret;
126
127 if (!test_bit(STATUS_READY, &priv->status))
128 return -EBUSY;
129
130 if (priv->blink_on == on && priv->blink_off == off)
131 return 0;
132
133 if (off == 0) {
134 /* led is SOLID_ON */
135 on = IWL_LED_SOLID;
136 }
137
138 IWL_DEBUG_LED(priv, "Led blink time compensation=%u\n",
139 priv->cfg->base_params->led_compensation);
140 led_cmd.on = iwl_blink_compensation(priv, on,
141 priv->cfg->base_params->led_compensation);
142 led_cmd.off = iwl_blink_compensation(priv, off,
143 priv->cfg->base_params->led_compensation);
144
145 ret = iwl_send_led_cmd(priv, &led_cmd);
146 if (!ret) {
147 priv->blink_on = on;
148 priv->blink_off = off;
149 }
150 return ret;
151}
152
153static void iwl_led_brightness_set(struct led_classdev *led_cdev,
154 enum led_brightness brightness)
155{
156 struct iwl_priv *priv = container_of(led_cdev, struct iwl_priv, led);
157 unsigned long on = 0;
158
159 if (brightness > 0)
160 on = IWL_LED_SOLID;
161
162 iwl_led_cmd(priv, on, 0);
163}
164
165static int iwl_led_blink_set(struct led_classdev *led_cdev,
166 unsigned long *delay_on,
167 unsigned long *delay_off)
168{
169 struct iwl_priv *priv = container_of(led_cdev, struct iwl_priv, led);
170
171 return iwl_led_cmd(priv, *delay_on, *delay_off);
172}
173
174void iwl_leds_init(struct iwl_priv *priv)
175{
176 int mode = iwlwifi_mod_params.led_mode;
177 int ret;
178
179 if (mode == IWL_LED_DISABLE) {
180 IWL_INFO(priv, "Led disabled\n");
181 return;
182 }
183 if (mode == IWL_LED_DEFAULT)
184 mode = priv->cfg->led_mode;
185
186 priv->led.name = kasprintf(GFP_KERNEL, "%s-led",
187 wiphy_name(priv->hw->wiphy));
188 priv->led.brightness_set = iwl_led_brightness_set;
189 priv->led.blink_set = iwl_led_blink_set;
190 priv->led.max_brightness = 1;
191
192 switch (mode) {
193 case IWL_LED_DEFAULT:
194 WARN_ON(1);
195 break;
196 case IWL_LED_BLINK:
197 priv->led.default_trigger =
198 ieee80211_create_tpt_led_trigger(priv->hw,
199 IEEE80211_TPT_LEDTRIG_FL_CONNECTED,
200 iwl_blink, ARRAY_SIZE(iwl_blink));
201 break;
202 case IWL_LED_RF_STATE:
203 priv->led.default_trigger =
204 ieee80211_get_radio_led_name(priv->hw);
205 break;
206 }
207
208 ret = led_classdev_register(priv->trans->dev, &priv->led);
209 if (ret) {
210 kfree(priv->led.name);
211 return;
212 }
213
214 priv->led_registered = true;
215}
216
217void iwl_leds_exit(struct iwl_priv *priv)
218{
219 if (!priv->led_registered)
220 return;
221
222 led_classdev_unregister(&priv->led);
223 kfree(priv->led.name);
224}
diff --git a/drivers/net/wireless/iwlwifi/dvm/led.h b/drivers/net/wireless/iwlwifi/dvm/led.h
new file mode 100644
index 000000000000..b02a853103d3
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/led.h
@@ -0,0 +1,43 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26
27#ifndef __iwl_leds_h__
28#define __iwl_leds_h__
29
30
31struct iwl_priv;
32
33#define IWL_LED_SOLID 11
34#define IWL_DEF_LED_INTRVL cpu_to_le32(1000)
35
36#define IWL_LED_ACTIVITY (0<<1)
37#define IWL_LED_LINK (1<<1)
38
39void iwlagn_led_enable(struct iwl_priv *priv);
40void iwl_leds_init(struct iwl_priv *priv);
41void iwl_leds_exit(struct iwl_priv *priv);
42
43#endif /* __iwl_leds_h__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/lib.c b/drivers/net/wireless/iwlwifi/dvm/lib.c
new file mode 100644
index 000000000000..4ac9af7eb853
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/lib.c
@@ -0,0 +1,1291 @@
1/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29#include <linux/etherdevice.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/sched.h>
34#include <net/mac80211.h>
35
36#include "iwl-io.h"
37#include "iwl-agn-hw.h"
38#include "iwl-trans.h"
39#include "iwl-modparams.h"
40
41#include "dev.h"
42#include "agn.h"
43
44int iwlagn_hw_valid_rtc_data_addr(u32 addr)
45{
46 return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
47 (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
48}
49
50int iwlagn_send_tx_power(struct iwl_priv *priv)
51{
52 struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
53 u8 tx_ant_cfg_cmd;
54
55 if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->status),
56 "TX Power requested while scanning!\n"))
57 return -EAGAIN;
58
59 /* half dBm need to multiply */
60 tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
61
62 if (priv->tx_power_lmt_in_half_dbm &&
63 priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
64 /*
65 * For the newer devices which using enhanced/extend tx power
66 * table in EEPROM, the format is in half dBm. driver need to
67 * convert to dBm format before report to mac80211.
68 * By doing so, there is a possibility of 1/2 dBm resolution
69 * lost. driver will perform "round-up" operation before
70 * reporting, but it will cause 1/2 dBm tx power over the
71 * regulatory limit. Perform the checking here, if the
72 * "tx_power_user_lmt" is higher than EEPROM value (in
73 * half-dBm format), lower the tx power based on EEPROM
74 */
75 tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
76 }
77 tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
78 tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
79
80 if (IWL_UCODE_API(priv->fw->ucode_ver) == 1)
81 tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
82 else
83 tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
84
85 return iwl_dvm_send_cmd_pdu(priv, tx_ant_cfg_cmd, CMD_SYNC,
86 sizeof(tx_power_cmd), &tx_power_cmd);
87}
88
89void iwlagn_temperature(struct iwl_priv *priv)
90{
91 lockdep_assert_held(&priv->statistics.lock);
92
93 /* store temperature from correct statistics (in Celsius) */
94 priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
95 iwl_tt_handler(priv);
96}
97
98int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
99{
100 int idx = 0;
101 int band_offset = 0;
102
103 /* HT rate format: mac80211 wants an MCS number, which is just LSB */
104 if (rate_n_flags & RATE_MCS_HT_MSK) {
105 idx = (rate_n_flags & 0xff);
106 return idx;
107 /* Legacy rate format, search for match in table */
108 } else {
109 if (band == IEEE80211_BAND_5GHZ)
110 band_offset = IWL_FIRST_OFDM_RATE;
111 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
112 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
113 return idx - band_offset;
114 }
115
116 return -1;
117}
118
119int iwlagn_manage_ibss_station(struct iwl_priv *priv,
120 struct ieee80211_vif *vif, bool add)
121{
122 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
123
124 if (add)
125 return iwlagn_add_bssid_station(priv, vif_priv->ctx,
126 vif->bss_conf.bssid,
127 &vif_priv->ibss_bssid_sta_id);
128 return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
129 vif->bss_conf.bssid);
130}
131
132/**
133 * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
134 *
135 * pre-requirements:
136 * 1. acquire mutex before calling
137 * 2. make sure rf is on and not in exit state
138 */
139int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
140{
141 struct iwl_txfifo_flush_cmd flush_cmd;
142 struct iwl_host_cmd cmd = {
143 .id = REPLY_TXFIFO_FLUSH,
144 .len = { sizeof(struct iwl_txfifo_flush_cmd), },
145 .flags = CMD_SYNC,
146 .data = { &flush_cmd, },
147 };
148
149 might_sleep();
150
151 memset(&flush_cmd, 0, sizeof(flush_cmd));
152 if (flush_control & BIT(IWL_RXON_CTX_BSS))
153 flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
154 IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
155 IWL_SCD_MGMT_MSK;
156 if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
157 (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
158 flush_cmd.fifo_control |= IWL_PAN_SCD_VO_MSK |
159 IWL_PAN_SCD_VI_MSK | IWL_PAN_SCD_BE_MSK |
160 IWL_PAN_SCD_BK_MSK | IWL_PAN_SCD_MGMT_MSK |
161 IWL_PAN_SCD_MULTICAST_MSK;
162
163 if (priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE)
164 flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
165
166 IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
167 flush_cmd.fifo_control);
168 flush_cmd.flush_control = cpu_to_le16(flush_control);
169
170 return iwl_dvm_send_cmd(priv, &cmd);
171}
172
173void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
174{
175 mutex_lock(&priv->mutex);
176 ieee80211_stop_queues(priv->hw);
177 if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
178 IWL_ERR(priv, "flush request fail\n");
179 goto done;
180 }
181 IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
182 iwl_trans_wait_tx_queue_empty(priv->trans);
183done:
184 ieee80211_wake_queues(priv->hw);
185 mutex_unlock(&priv->mutex);
186}
187
188/*
189 * BT coex
190 */
191/* Notmal TDM */
192static const __le32 iwlagn_def_3w_lookup[IWLAGN_BT_DECISION_LUT_SIZE] = {
193 cpu_to_le32(0xaaaaaaaa),
194 cpu_to_le32(0xaaaaaaaa),
195 cpu_to_le32(0xaeaaaaaa),
196 cpu_to_le32(0xaaaaaaaa),
197 cpu_to_le32(0xcc00ff28),
198 cpu_to_le32(0x0000aaaa),
199 cpu_to_le32(0xcc00aaaa),
200 cpu_to_le32(0x0000aaaa),
201 cpu_to_le32(0xc0004000),
202 cpu_to_le32(0x00004000),
203 cpu_to_le32(0xf0005000),
204 cpu_to_le32(0xf0005000),
205};
206
207
208/* Loose Coex */
209static const __le32 iwlagn_loose_lookup[IWLAGN_BT_DECISION_LUT_SIZE] = {
210 cpu_to_le32(0xaaaaaaaa),
211 cpu_to_le32(0xaaaaaaaa),
212 cpu_to_le32(0xaeaaaaaa),
213 cpu_to_le32(0xaaaaaaaa),
214 cpu_to_le32(0xcc00ff28),
215 cpu_to_le32(0x0000aaaa),
216 cpu_to_le32(0xcc00aaaa),
217 cpu_to_le32(0x0000aaaa),
218 cpu_to_le32(0x00000000),
219 cpu_to_le32(0x00000000),
220 cpu_to_le32(0xf0005000),
221 cpu_to_le32(0xf0005000),
222};
223
224/* Full concurrency */
225static const __le32 iwlagn_concurrent_lookup[IWLAGN_BT_DECISION_LUT_SIZE] = {
226 cpu_to_le32(0xaaaaaaaa),
227 cpu_to_le32(0xaaaaaaaa),
228 cpu_to_le32(0xaaaaaaaa),
229 cpu_to_le32(0xaaaaaaaa),
230 cpu_to_le32(0xaaaaaaaa),
231 cpu_to_le32(0xaaaaaaaa),
232 cpu_to_le32(0xaaaaaaaa),
233 cpu_to_le32(0xaaaaaaaa),
234 cpu_to_le32(0x00000000),
235 cpu_to_le32(0x00000000),
236 cpu_to_le32(0x00000000),
237 cpu_to_le32(0x00000000),
238};
239
240void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
241{
242 struct iwl_basic_bt_cmd basic = {
243 .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
244 .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
245 .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
246 .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
247 };
248 struct iwl_bt_cmd_v1 bt_cmd_v1;
249 struct iwl_bt_cmd_v2 bt_cmd_v2;
250 int ret;
251
252 BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
253 sizeof(basic.bt3_lookup_table));
254
255 if (priv->cfg->bt_params) {
256 /*
257 * newer generation of devices (2000 series and newer)
258 * use the version 2 of the bt command
259 * we need to make sure sending the host command
260 * with correct data structure to avoid uCode assert
261 */
262 if (priv->cfg->bt_params->bt_session_2) {
263 bt_cmd_v2.prio_boost = cpu_to_le32(
264 priv->cfg->bt_params->bt_prio_boost);
265 bt_cmd_v2.tx_prio_boost = 0;
266 bt_cmd_v2.rx_prio_boost = 0;
267 } else {
268 bt_cmd_v1.prio_boost =
269 priv->cfg->bt_params->bt_prio_boost;
270 bt_cmd_v1.tx_prio_boost = 0;
271 bt_cmd_v1.rx_prio_boost = 0;
272 }
273 } else {
274 IWL_ERR(priv, "failed to construct BT Coex Config\n");
275 return;
276 }
277
278 /*
279 * Possible situations when BT needs to take over for receive,
280 * at the same time where STA needs to response to AP's frame(s),
281 * reduce the tx power of the required response frames, by that,
282 * allow the concurrent BT receive & WiFi transmit
283 * (BT - ANT A, WiFi -ANT B), without interference to one another
284 *
285 * Reduced tx power apply to control frames only (ACK/Back/CTS)
286 * when indicated by the BT config command
287 */
288 basic.kill_ack_mask = priv->kill_ack_mask;
289 basic.kill_cts_mask = priv->kill_cts_mask;
290 if (priv->reduced_txpower)
291 basic.reduce_txpower = IWLAGN_BT_REDUCED_TX_PWR;
292 basic.valid = priv->bt_valid;
293
294 /*
295 * Configure BT coex mode to "no coexistence" when the
296 * user disabled BT coexistence, we have no interface
297 * (might be in monitor mode), or the interface is in
298 * IBSS mode (no proper uCode support for coex then).
299 */
300 if (!iwlwifi_mod_params.bt_coex_active ||
301 priv->iw_mode == NL80211_IFTYPE_ADHOC) {
302 basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
303 } else {
304 basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
305 IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
306
307 if (!priv->bt_enable_pspoll)
308 basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
309 else
310 basic.flags &= ~IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
311
312 if (priv->bt_ch_announce)
313 basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
314 IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
315 }
316 priv->bt_enable_flag = basic.flags;
317 if (priv->bt_full_concurrent)
318 memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
319 sizeof(iwlagn_concurrent_lookup));
320 else
321 memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
322 sizeof(iwlagn_def_3w_lookup));
323
324 IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
325 basic.flags ? "active" : "disabled",
326 priv->bt_full_concurrent ?
327 "full concurrency" : "3-wire");
328
329 if (priv->cfg->bt_params->bt_session_2) {
330 memcpy(&bt_cmd_v2.basic, &basic,
331 sizeof(basic));
332 ret = iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
333 CMD_SYNC, sizeof(bt_cmd_v2), &bt_cmd_v2);
334 } else {
335 memcpy(&bt_cmd_v1.basic, &basic,
336 sizeof(basic));
337 ret = iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
338 CMD_SYNC, sizeof(bt_cmd_v1), &bt_cmd_v1);
339 }
340 if (ret)
341 IWL_ERR(priv, "failed to send BT Coex Config\n");
342
343}
344
345void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena)
346{
347 struct iwl_rxon_context *ctx, *found_ctx = NULL;
348 bool found_ap = false;
349
350 lockdep_assert_held(&priv->mutex);
351
352 /* Check whether AP or GO mode is active. */
353 if (rssi_ena) {
354 for_each_context(priv, ctx) {
355 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_AP &&
356 iwl_is_associated_ctx(ctx)) {
357 found_ap = true;
358 break;
359 }
360 }
361 }
362
363 /*
364 * If disable was received or If GO/AP mode, disable RSSI
365 * measurements.
366 */
367 if (!rssi_ena || found_ap) {
368 if (priv->cur_rssi_ctx) {
369 ctx = priv->cur_rssi_ctx;
370 ieee80211_disable_rssi_reports(ctx->vif);
371 priv->cur_rssi_ctx = NULL;
372 }
373 return;
374 }
375
376 /*
377 * If rssi measurements need to be enabled, consider all cases now.
378 * Figure out how many contexts are active.
379 */
380 for_each_context(priv, ctx) {
381 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
382 iwl_is_associated_ctx(ctx)) {
383 found_ctx = ctx;
384 break;
385 }
386 }
387
388 /*
389 * rssi monitor already enabled for the correct interface...nothing
390 * to do.
391 */
392 if (found_ctx == priv->cur_rssi_ctx)
393 return;
394
395 /*
396 * Figure out if rssi monitor is currently enabled, and needs
397 * to be changed. If rssi monitor is already enabled, disable
398 * it first else just enable rssi measurements on the
399 * interface found above.
400 */
401 if (priv->cur_rssi_ctx) {
402 ctx = priv->cur_rssi_ctx;
403 if (ctx->vif)
404 ieee80211_disable_rssi_reports(ctx->vif);
405 }
406
407 priv->cur_rssi_ctx = found_ctx;
408
409 if (!found_ctx)
410 return;
411
412 ieee80211_enable_rssi_reports(found_ctx->vif,
413 IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD,
414 IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD);
415}
416
417static bool iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg *uart_msg)
418{
419 return BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3 >>
420 BT_UART_MSG_FRAME3SCOESCO_POS;
421}
422
423static void iwlagn_bt_traffic_change_work(struct work_struct *work)
424{
425 struct iwl_priv *priv =
426 container_of(work, struct iwl_priv, bt_traffic_change_work);
427 struct iwl_rxon_context *ctx;
428 int smps_request = -1;
429
430 if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
431 /* bt coex disabled */
432 return;
433 }
434
435 /*
436 * Note: bt_traffic_load can be overridden by scan complete and
437 * coex profile notifications. Ignore that since only bad consequence
438 * can be not matching debug print with actual state.
439 */
440 IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
441 priv->bt_traffic_load);
442
443 switch (priv->bt_traffic_load) {
444 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
445 if (priv->bt_status)
446 smps_request = IEEE80211_SMPS_DYNAMIC;
447 else
448 smps_request = IEEE80211_SMPS_AUTOMATIC;
449 break;
450 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
451 smps_request = IEEE80211_SMPS_DYNAMIC;
452 break;
453 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
454 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
455 smps_request = IEEE80211_SMPS_STATIC;
456 break;
457 default:
458 IWL_ERR(priv, "Invalid BT traffic load: %d\n",
459 priv->bt_traffic_load);
460 break;
461 }
462
463 mutex_lock(&priv->mutex);
464
465 /*
466 * We can not send command to firmware while scanning. When the scan
467 * complete we will schedule this work again. We do check with mutex
468 * locked to prevent new scan request to arrive. We do not check
469 * STATUS_SCANNING to avoid race when queue_work two times from
470 * different notifications, but quit and not perform any work at all.
471 */
472 if (test_bit(STATUS_SCAN_HW, &priv->status))
473 goto out;
474
475 iwl_update_chain_flags(priv);
476
477 if (smps_request != -1) {
478 priv->current_ht_config.smps = smps_request;
479 for_each_context(priv, ctx) {
480 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
481 ieee80211_request_smps(ctx->vif, smps_request);
482 }
483 }
484
485 /*
486 * Dynamic PS poll related functionality. Adjust RSSI measurements if
487 * necessary.
488 */
489 iwlagn_bt_coex_rssi_monitor(priv);
490out:
491 mutex_unlock(&priv->mutex);
492}
493
494/*
495 * If BT sco traffic, and RSSI monitor is enabled, move measurements to the
496 * correct interface or disable it if this is the last interface to be
497 * removed.
498 */
499void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv)
500{
501 if (priv->bt_is_sco &&
502 priv->bt_traffic_load == IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS)
503 iwlagn_bt_adjust_rssi_monitor(priv, true);
504 else
505 iwlagn_bt_adjust_rssi_monitor(priv, false);
506}
507
508static void iwlagn_print_uartmsg(struct iwl_priv *priv,
509 struct iwl_bt_uart_msg *uart_msg)
510{
511 IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
512 "Update Req = 0x%X\n",
513 (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
514 BT_UART_MSG_FRAME1MSGTYPE_POS,
515 (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
516 BT_UART_MSG_FRAME1SSN_POS,
517 (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
518 BT_UART_MSG_FRAME1UPDATEREQ_POS);
519
520 IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
521 "Chl_SeqN = 0x%X, In band = 0x%X\n",
522 (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
523 BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
524 (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
525 BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
526 (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
527 BT_UART_MSG_FRAME2CHLSEQN_POS,
528 (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
529 BT_UART_MSG_FRAME2INBAND_POS);
530
531 IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
532 "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X\n",
533 (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
534 BT_UART_MSG_FRAME3SCOESCO_POS,
535 (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
536 BT_UART_MSG_FRAME3SNIFF_POS,
537 (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
538 BT_UART_MSG_FRAME3A2DP_POS,
539 (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
540 BT_UART_MSG_FRAME3ACL_POS,
541 (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
542 BT_UART_MSG_FRAME3MASTER_POS,
543 (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
544 BT_UART_MSG_FRAME3OBEX_POS);
545
546 IWL_DEBUG_COEX(priv, "Idle duration = 0x%X\n",
547 (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
548 BT_UART_MSG_FRAME4IDLEDURATION_POS);
549
550 IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
551 "eSCO Retransmissions = 0x%X\n",
552 (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
553 BT_UART_MSG_FRAME5TXACTIVITY_POS,
554 (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
555 BT_UART_MSG_FRAME5RXACTIVITY_POS,
556 (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
557 BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
558
559 IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X\n",
560 (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
561 BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
562 (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
563 BT_UART_MSG_FRAME6DISCOVERABLE_POS);
564
565 IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
566 "0x%X, Inquiry = 0x%X, Connectable = 0x%X\n",
567 (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
568 BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
569 (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
570 BT_UART_MSG_FRAME7PAGE_POS,
571 (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
572 BT_UART_MSG_FRAME7INQUIRY_POS,
573 (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
574 BT_UART_MSG_FRAME7CONNECTABLE_POS);
575}
576
577static bool iwlagn_set_kill_msk(struct iwl_priv *priv,
578 struct iwl_bt_uart_msg *uart_msg)
579{
580 bool need_update = false;
581 u8 kill_msk = IWL_BT_KILL_REDUCE;
582 static const __le32 bt_kill_ack_msg[3] = {
583 IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
584 IWLAGN_BT_KILL_ACK_CTS_MASK_SCO,
585 IWLAGN_BT_KILL_ACK_CTS_MASK_REDUCE};
586 static const __le32 bt_kill_cts_msg[3] = {
587 IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
588 IWLAGN_BT_KILL_ACK_CTS_MASK_SCO,
589 IWLAGN_BT_KILL_ACK_CTS_MASK_REDUCE};
590
591 if (!priv->reduced_txpower)
592 kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
593 ? IWL_BT_KILL_OVERRIDE : IWL_BT_KILL_DEFAULT;
594 if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
595 priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
596 priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
597 priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
598 priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
599 priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
600 need_update = true;
601 }
602 return need_update;
603}
604
605/*
606 * Upon RSSI changes, sends a bt config command with following changes
607 * 1. enable/disable "reduced control frames tx power
608 * 2. update the "kill)ack_mask" and "kill_cts_mask"
609 *
610 * If "reduced tx power" is enabled, uCode shall
611 * 1. ACK/Back/CTS rate shall reduced to 6Mbps
612 * 2. not use duplciate 20/40MHz mode
613 */
614static bool iwlagn_fill_txpower_mode(struct iwl_priv *priv,
615 struct iwl_bt_uart_msg *uart_msg)
616{
617 bool need_update = false;
618 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
619 int ave_rssi;
620
621 if (!ctx->vif || (ctx->vif->type != NL80211_IFTYPE_STATION)) {
622 IWL_DEBUG_INFO(priv, "BSS ctx not active or not in sta mode\n");
623 return false;
624 }
625
626 ave_rssi = ieee80211_ave_rssi(ctx->vif);
627 if (!ave_rssi) {
628 /* no rssi data, no changes to reduce tx power */
629 IWL_DEBUG_COEX(priv, "no rssi data available\n");
630 return need_update;
631 }
632 if (!priv->reduced_txpower &&
633 !iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
634 (ave_rssi > BT_ENABLE_REDUCED_TXPOWER_THRESHOLD) &&
635 (uart_msg->frame3 & (BT_UART_MSG_FRAME3ACL_MSK |
636 BT_UART_MSG_FRAME3OBEX_MSK)) &&
637 !(uart_msg->frame3 & (BT_UART_MSG_FRAME3SCOESCO_MSK |
638 BT_UART_MSG_FRAME3SNIFF_MSK | BT_UART_MSG_FRAME3A2DP_MSK))) {
639 /* enabling reduced tx power */
640 priv->reduced_txpower = true;
641 priv->bt_valid |= IWLAGN_BT_VALID_REDUCED_TX_PWR;
642 need_update = true;
643 } else if (priv->reduced_txpower &&
644 (iwl_is_associated(priv, IWL_RXON_CTX_PAN) ||
645 (ave_rssi < BT_DISABLE_REDUCED_TXPOWER_THRESHOLD) ||
646 (uart_msg->frame3 & (BT_UART_MSG_FRAME3SCOESCO_MSK |
647 BT_UART_MSG_FRAME3SNIFF_MSK | BT_UART_MSG_FRAME3A2DP_MSK)) ||
648 !(uart_msg->frame3 & (BT_UART_MSG_FRAME3ACL_MSK |
649 BT_UART_MSG_FRAME3OBEX_MSK)))) {
650 /* disable reduced tx power */
651 priv->reduced_txpower = false;
652 priv->bt_valid |= IWLAGN_BT_VALID_REDUCED_TX_PWR;
653 need_update = true;
654 }
655
656 return need_update;
657}
658
659int iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
660 struct iwl_rx_cmd_buffer *rxb,
661 struct iwl_device_cmd *cmd)
662{
663 struct iwl_rx_packet *pkt = rxb_addr(rxb);
664 struct iwl_bt_coex_profile_notif *coex = (void *)pkt->data;
665 struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
666
667 if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
668 /* bt coex disabled */
669 return 0;
670 }
671
672 IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
673 IWL_DEBUG_COEX(priv, " status: %d\n", coex->bt_status);
674 IWL_DEBUG_COEX(priv, " traffic load: %d\n", coex->bt_traffic_load);
675 IWL_DEBUG_COEX(priv, " CI compliance: %d\n",
676 coex->bt_ci_compliance);
677 iwlagn_print_uartmsg(priv, uart_msg);
678
679 priv->last_bt_traffic_load = priv->bt_traffic_load;
680 priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
681
682 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
683 if (priv->bt_status != coex->bt_status ||
684 priv->last_bt_traffic_load != coex->bt_traffic_load) {
685 if (coex->bt_status) {
686 /* BT on */
687 if (!priv->bt_ch_announce)
688 priv->bt_traffic_load =
689 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
690 else
691 priv->bt_traffic_load =
692 coex->bt_traffic_load;
693 } else {
694 /* BT off */
695 priv->bt_traffic_load =
696 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
697 }
698 priv->bt_status = coex->bt_status;
699 queue_work(priv->workqueue,
700 &priv->bt_traffic_change_work);
701 }
702 }
703
704 /* schedule to send runtime bt_config */
705 /* check reduce power before change ack/cts kill mask */
706 if (iwlagn_fill_txpower_mode(priv, uart_msg) ||
707 iwlagn_set_kill_msk(priv, uart_msg))
708 queue_work(priv->workqueue, &priv->bt_runtime_config);
709
710
711 /* FIXME: based on notification, adjust the prio_boost */
712
713 priv->bt_ci_compliance = coex->bt_ci_compliance;
714 return 0;
715}
716
717void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
718{
719 priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
720 iwlagn_bt_coex_profile_notif;
721}
722
723void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
724{
725 INIT_WORK(&priv->bt_traffic_change_work,
726 iwlagn_bt_traffic_change_work);
727}
728
729void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
730{
731 cancel_work_sync(&priv->bt_traffic_change_work);
732}
733
734static bool is_single_rx_stream(struct iwl_priv *priv)
735{
736 return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
737 priv->current_ht_config.single_chain_sufficient;
738}
739
740#define IWL_NUM_RX_CHAINS_MULTIPLE 3
741#define IWL_NUM_RX_CHAINS_SINGLE 2
742#define IWL_NUM_IDLE_CHAINS_DUAL 2
743#define IWL_NUM_IDLE_CHAINS_SINGLE 1
744
745/*
746 * Determine how many receiver/antenna chains to use.
747 *
748 * More provides better reception via diversity. Fewer saves power
749 * at the expense of throughput, but only when not in powersave to
750 * start with.
751 *
752 * MIMO (dual stream) requires at least 2, but works better with 3.
753 * This does not determine *which* chains to use, just how many.
754 */
755static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
756{
757 if (priv->cfg->bt_params &&
758 priv->cfg->bt_params->advanced_bt_coexist &&
759 (priv->bt_full_concurrent ||
760 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
761 /*
762 * only use chain 'A' in bt high traffic load or
763 * full concurrency mode
764 */
765 return IWL_NUM_RX_CHAINS_SINGLE;
766 }
767 /* # of Rx chains to use when expecting MIMO. */
768 if (is_single_rx_stream(priv))
769 return IWL_NUM_RX_CHAINS_SINGLE;
770 else
771 return IWL_NUM_RX_CHAINS_MULTIPLE;
772}
773
774/*
775 * When we are in power saving mode, unless device support spatial
776 * multiplexing power save, use the active count for rx chain count.
777 */
778static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
779{
780 /* # Rx chains when idling, depending on SMPS mode */
781 switch (priv->current_ht_config.smps) {
782 case IEEE80211_SMPS_STATIC:
783 case IEEE80211_SMPS_DYNAMIC:
784 return IWL_NUM_IDLE_CHAINS_SINGLE;
785 case IEEE80211_SMPS_AUTOMATIC:
786 case IEEE80211_SMPS_OFF:
787 return active_cnt;
788 default:
789 WARN(1, "invalid SMPS mode %d",
790 priv->current_ht_config.smps);
791 return active_cnt;
792 }
793}
794
795/* up to 4 chains */
796static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
797{
798 u8 res;
799 res = (chain_bitmap & BIT(0)) >> 0;
800 res += (chain_bitmap & BIT(1)) >> 1;
801 res += (chain_bitmap & BIT(2)) >> 2;
802 res += (chain_bitmap & BIT(3)) >> 3;
803 return res;
804}
805
806/**
807 * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
808 *
809 * Selects how many and which Rx receivers/antennas/chains to use.
810 * This should not be used for scan command ... it puts data in wrong place.
811 */
812void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
813{
814 bool is_single = is_single_rx_stream(priv);
815 bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
816 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
817 u32 active_chains;
818 u16 rx_chain;
819
820 /* Tell uCode which antennas are actually connected.
821 * Before first association, we assume all antennas are connected.
822 * Just after first association, iwl_chain_noise_calibration()
823 * checks which antennas actually *are* connected. */
824 if (priv->chain_noise_data.active_chains)
825 active_chains = priv->chain_noise_data.active_chains;
826 else
827 active_chains = priv->hw_params.valid_rx_ant;
828
829 if (priv->cfg->bt_params &&
830 priv->cfg->bt_params->advanced_bt_coexist &&
831 (priv->bt_full_concurrent ||
832 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
833 /*
834 * only use chain 'A' in bt high traffic load or
835 * full concurrency mode
836 */
837 active_chains = first_antenna(active_chains);
838 }
839
840 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
841
842 /* How many receivers should we use? */
843 active_rx_cnt = iwl_get_active_rx_chain_count(priv);
844 idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
845
846
847 /* correct rx chain count according hw settings
848 * and chain noise calibration
849 */
850 valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
851 if (valid_rx_cnt < active_rx_cnt)
852 active_rx_cnt = valid_rx_cnt;
853
854 if (valid_rx_cnt < idle_rx_cnt)
855 idle_rx_cnt = valid_rx_cnt;
856
857 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
858 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
859
860 ctx->staging.rx_chain = cpu_to_le16(rx_chain);
861
862 if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
863 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
864 else
865 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
866
867 IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
868 ctx->staging.rx_chain,
869 active_rx_cnt, idle_rx_cnt);
870
871 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
872 active_rx_cnt < idle_rx_cnt);
873}
874
875u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
876{
877 int i;
878 u8 ind = ant;
879
880 if (priv->band == IEEE80211_BAND_2GHZ &&
881 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
882 return 0;
883
884 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
885 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
886 if (valid & BIT(ind))
887 return ind;
888 }
889 return ant;
890}
891
892#ifdef CONFIG_PM_SLEEP
893static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
894{
895 int i;
896
897 for (i = 0; i < IWLAGN_P1K_SIZE; i++)
898 out[i] = cpu_to_le16(p1k[i]);
899}
900
901struct wowlan_key_data {
902 struct iwl_rxon_context *ctx;
903 struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
904 struct iwlagn_wowlan_tkip_params_cmd *tkip;
905 const u8 *bssid;
906 bool error, use_rsc_tsc, use_tkip;
907};
908
909
910static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
911 struct ieee80211_vif *vif,
912 struct ieee80211_sta *sta,
913 struct ieee80211_key_conf *key,
914 void *_data)
915{
916 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
917 struct wowlan_key_data *data = _data;
918 struct iwl_rxon_context *ctx = data->ctx;
919 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
920 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
921 struct iwlagn_p1k_cache *rx_p1ks;
922 u8 *rx_mic_key;
923 struct ieee80211_key_seq seq;
924 u32 cur_rx_iv32 = 0;
925 u16 p1k[IWLAGN_P1K_SIZE];
926 int ret, i;
927
928 mutex_lock(&priv->mutex);
929
930 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
931 key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
932 !sta && !ctx->key_mapping_keys)
933 ret = iwl_set_default_wep_key(priv, ctx, key);
934 else
935 ret = iwl_set_dynamic_key(priv, ctx, key, sta);
936
937 if (ret) {
938 IWL_ERR(priv, "Error setting key during suspend!\n");
939 data->error = true;
940 }
941
942 switch (key->cipher) {
943 case WLAN_CIPHER_SUITE_TKIP:
944 if (sta) {
945 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
946 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
947
948 rx_p1ks = data->tkip->rx_uni;
949
950 ieee80211_get_key_tx_seq(key, &seq);
951 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
952 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
953
954 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
955 iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
956
957 memcpy(data->tkip->mic_keys.tx,
958 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
959 IWLAGN_MIC_KEY_SIZE);
960
961 rx_mic_key = data->tkip->mic_keys.rx_unicast;
962 } else {
963 tkip_sc =
964 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
965 rx_p1ks = data->tkip->rx_multi;
966 rx_mic_key = data->tkip->mic_keys.rx_mcast;
967 }
968
969 /*
970 * For non-QoS this relies on the fact that both the uCode and
971 * mac80211 use TID 0 (as they need to to avoid replay attacks)
972 * for checking the IV in the frames.
973 */
974 for (i = 0; i < IWLAGN_NUM_RSC; i++) {
975 ieee80211_get_key_rx_seq(key, i, &seq);
976 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
977 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
978 /* wrapping isn't allowed, AP must rekey */
979 if (seq.tkip.iv32 > cur_rx_iv32)
980 cur_rx_iv32 = seq.tkip.iv32;
981 }
982
983 ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
984 iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
985 ieee80211_get_tkip_rx_p1k(key, data->bssid,
986 cur_rx_iv32 + 1, p1k);
987 iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
988
989 memcpy(rx_mic_key,
990 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
991 IWLAGN_MIC_KEY_SIZE);
992
993 data->use_tkip = true;
994 data->use_rsc_tsc = true;
995 break;
996 case WLAN_CIPHER_SUITE_CCMP:
997 if (sta) {
998 u8 *pn = seq.ccmp.pn;
999
1000 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
1001 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
1002
1003 ieee80211_get_key_tx_seq(key, &seq);
1004 aes_tx_sc->pn = cpu_to_le64(
1005 (u64)pn[5] |
1006 ((u64)pn[4] << 8) |
1007 ((u64)pn[3] << 16) |
1008 ((u64)pn[2] << 24) |
1009 ((u64)pn[1] << 32) |
1010 ((u64)pn[0] << 40));
1011 } else
1012 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
1013
1014 /*
1015 * For non-QoS this relies on the fact that both the uCode and
1016 * mac80211 use TID 0 for checking the IV in the frames.
1017 */
1018 for (i = 0; i < IWLAGN_NUM_RSC; i++) {
1019 u8 *pn = seq.ccmp.pn;
1020
1021 ieee80211_get_key_rx_seq(key, i, &seq);
1022 aes_sc->pn = cpu_to_le64(
1023 (u64)pn[5] |
1024 ((u64)pn[4] << 8) |
1025 ((u64)pn[3] << 16) |
1026 ((u64)pn[2] << 24) |
1027 ((u64)pn[1] << 32) |
1028 ((u64)pn[0] << 40));
1029 }
1030 data->use_rsc_tsc = true;
1031 break;
1032 }
1033
1034 mutex_unlock(&priv->mutex);
1035}
1036
1037int iwlagn_send_patterns(struct iwl_priv *priv,
1038 struct cfg80211_wowlan *wowlan)
1039{
1040 struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
1041 struct iwl_host_cmd cmd = {
1042 .id = REPLY_WOWLAN_PATTERNS,
1043 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1044 .flags = CMD_SYNC,
1045 };
1046 int i, err;
1047
1048 if (!wowlan->n_patterns)
1049 return 0;
1050
1051 cmd.len[0] = sizeof(*pattern_cmd) +
1052 wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
1053
1054 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
1055 if (!pattern_cmd)
1056 return -ENOMEM;
1057
1058 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
1059
1060 for (i = 0; i < wowlan->n_patterns; i++) {
1061 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
1062
1063 memcpy(&pattern_cmd->patterns[i].mask,
1064 wowlan->patterns[i].mask, mask_len);
1065 memcpy(&pattern_cmd->patterns[i].pattern,
1066 wowlan->patterns[i].pattern,
1067 wowlan->patterns[i].pattern_len);
1068 pattern_cmd->patterns[i].mask_size = mask_len;
1069 pattern_cmd->patterns[i].pattern_size =
1070 wowlan->patterns[i].pattern_len;
1071 }
1072
1073 cmd.data[0] = pattern_cmd;
1074 err = iwl_dvm_send_cmd(priv, &cmd);
1075 kfree(pattern_cmd);
1076 return err;
1077}
1078
1079int iwlagn_suspend(struct iwl_priv *priv, struct cfg80211_wowlan *wowlan)
1080{
1081 struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
1082 struct iwl_rxon_cmd rxon;
1083 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1084 struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
1085 struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
1086 struct iwlagn_d3_config_cmd d3_cfg_cmd = {};
1087 struct wowlan_key_data key_data = {
1088 .ctx = ctx,
1089 .bssid = ctx->active.bssid_addr,
1090 .use_rsc_tsc = false,
1091 .tkip = &tkip_cmd,
1092 .use_tkip = false,
1093 };
1094 int ret, i;
1095 u16 seq;
1096
1097 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
1098 if (!key_data.rsc_tsc)
1099 return -ENOMEM;
1100
1101 memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
1102
1103 /*
1104 * We know the last used seqno, and the uCode expects to know that
1105 * one, it will increment before TX.
1106 */
1107 seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
1108 wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
1109
1110 /*
1111 * For QoS counters, we store the one to use next, so subtract 0x10
1112 * since the uCode will add 0x10 before using the value.
1113 */
1114 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1115 seq = priv->tid_data[IWL_AP_ID][i].seq_number;
1116 seq -= 0x10;
1117 wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
1118 }
1119
1120 if (wowlan->disconnect)
1121 wakeup_filter_cmd.enabled |=
1122 cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
1123 IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
1124 if (wowlan->magic_pkt)
1125 wakeup_filter_cmd.enabled |=
1126 cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
1127 if (wowlan->gtk_rekey_failure)
1128 wakeup_filter_cmd.enabled |=
1129 cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
1130 if (wowlan->eap_identity_req)
1131 wakeup_filter_cmd.enabled |=
1132 cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
1133 if (wowlan->four_way_handshake)
1134 wakeup_filter_cmd.enabled |=
1135 cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
1136 if (wowlan->n_patterns)
1137 wakeup_filter_cmd.enabled |=
1138 cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
1139
1140 if (wowlan->rfkill_release)
1141 d3_cfg_cmd.wakeup_flags |=
1142 cpu_to_le32(IWLAGN_D3_WAKEUP_RFKILL);
1143
1144 iwl_scan_cancel_timeout(priv, 200);
1145
1146 memcpy(&rxon, &ctx->active, sizeof(rxon));
1147
1148 priv->ucode_loaded = false;
1149 iwl_trans_stop_device(priv->trans);
1150
1151 priv->wowlan = true;
1152
1153 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
1154 if (ret)
1155 goto out;
1156
1157 /* now configure WoWLAN ucode */
1158 ret = iwl_alive_start(priv);
1159 if (ret)
1160 goto out;
1161
1162 memcpy(&ctx->staging, &rxon, sizeof(rxon));
1163 ret = iwlagn_commit_rxon(priv, ctx);
1164 if (ret)
1165 goto out;
1166
1167 ret = iwl_power_update_mode(priv, true);
1168 if (ret)
1169 goto out;
1170
1171 if (!iwlwifi_mod_params.sw_crypto) {
1172 /* mark all keys clear */
1173 priv->ucode_key_table = 0;
1174 ctx->key_mapping_keys = 0;
1175
1176 /*
1177 * This needs to be unlocked due to lock ordering
1178 * constraints. Since we're in the suspend path
1179 * that isn't really a problem though.
1180 */
1181 mutex_unlock(&priv->mutex);
1182 ieee80211_iter_keys(priv->hw, ctx->vif,
1183 iwlagn_wowlan_program_keys,
1184 &key_data);
1185 mutex_lock(&priv->mutex);
1186 if (key_data.error) {
1187 ret = -EIO;
1188 goto out;
1189 }
1190
1191 if (key_data.use_rsc_tsc) {
1192 struct iwl_host_cmd rsc_tsc_cmd = {
1193 .id = REPLY_WOWLAN_TSC_RSC_PARAMS,
1194 .flags = CMD_SYNC,
1195 .data[0] = key_data.rsc_tsc,
1196 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1197 .len[0] = sizeof(*key_data.rsc_tsc),
1198 };
1199
1200 ret = iwl_dvm_send_cmd(priv, &rsc_tsc_cmd);
1201 if (ret)
1202 goto out;
1203 }
1204
1205 if (key_data.use_tkip) {
1206 ret = iwl_dvm_send_cmd_pdu(priv,
1207 REPLY_WOWLAN_TKIP_PARAMS,
1208 CMD_SYNC, sizeof(tkip_cmd),
1209 &tkip_cmd);
1210 if (ret)
1211 goto out;
1212 }
1213
1214 if (priv->have_rekey_data) {
1215 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1216 memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
1217 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1218 memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
1219 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1220 kek_kck_cmd.replay_ctr = priv->replay_ctr;
1221
1222 ret = iwl_dvm_send_cmd_pdu(priv,
1223 REPLY_WOWLAN_KEK_KCK_MATERIAL,
1224 CMD_SYNC, sizeof(kek_kck_cmd),
1225 &kek_kck_cmd);
1226 if (ret)
1227 goto out;
1228 }
1229 }
1230
1231 ret = iwl_dvm_send_cmd_pdu(priv, REPLY_D3_CONFIG, CMD_SYNC,
1232 sizeof(d3_cfg_cmd), &d3_cfg_cmd);
1233 if (ret)
1234 goto out;
1235
1236 ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WOWLAN_WAKEUP_FILTER,
1237 CMD_SYNC, sizeof(wakeup_filter_cmd),
1238 &wakeup_filter_cmd);
1239 if (ret)
1240 goto out;
1241
1242 ret = iwlagn_send_patterns(priv, wowlan);
1243 out:
1244 kfree(key_data.rsc_tsc);
1245 return ret;
1246}
1247#endif
1248
1249int iwl_dvm_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
1250{
1251 if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
1252 IWL_WARN(priv, "Not sending command - %s KILL\n",
1253 iwl_is_rfkill(priv) ? "RF" : "CT");
1254 return -EIO;
1255 }
1256
1257 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
1258 IWL_ERR(priv, "Command %s failed: FW Error\n",
1259 iwl_dvm_get_cmd_string(cmd->id));
1260 return -EIO;
1261 }
1262
1263 /*
1264 * Synchronous commands from this op-mode must hold
1265 * the mutex, this ensures we don't try to send two
1266 * (or more) synchronous commands at a time.
1267 */
1268 if (cmd->flags & CMD_SYNC)
1269 lockdep_assert_held(&priv->mutex);
1270
1271 if (priv->ucode_owner == IWL_OWNERSHIP_TM &&
1272 !(cmd->flags & CMD_ON_DEMAND)) {
1273 IWL_DEBUG_HC(priv, "tm own the uCode, no regular hcmd send\n");
1274 return -EIO;
1275 }
1276
1277 return iwl_trans_send_cmd(priv->trans, cmd);
1278}
1279
1280int iwl_dvm_send_cmd_pdu(struct iwl_priv *priv, u8 id,
1281 u32 flags, u16 len, const void *data)
1282{
1283 struct iwl_host_cmd cmd = {
1284 .id = id,
1285 .len = { len, },
1286 .data = { data, },
1287 .flags = flags,
1288 };
1289
1290 return iwl_dvm_send_cmd(priv, &cmd);
1291}
diff --git a/drivers/net/wireless/iwlwifi/dvm/mac80211.c b/drivers/net/wireless/iwlwifi/dvm/mac80211.c
new file mode 100644
index 000000000000..33603c5a24a8
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/mac80211.c
@@ -0,0 +1,1631 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/dma-mapping.h>
34#include <linux/delay.h>
35#include <linux/sched.h>
36#include <linux/skbuff.h>
37#include <linux/netdevice.h>
38#include <linux/etherdevice.h>
39#include <linux/if_arp.h>
40
41#include <net/ieee80211_radiotap.h>
42#include <net/mac80211.h>
43
44#include <asm/div64.h>
45
46#include "iwl-io.h"
47#include "iwl-trans.h"
48#include "iwl-op-mode.h"
49#include "iwl-modparams.h"
50
51#include "eeprom.h"
52#include "dev.h"
53#include "calib.h"
54#include "agn.h"
55
56/*****************************************************************************
57 *
58 * mac80211 entry point functions
59 *
60 *****************************************************************************/
61
62static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
63 {
64 .max = 1,
65 .types = BIT(NL80211_IFTYPE_STATION),
66 },
67 {
68 .max = 1,
69 .types = BIT(NL80211_IFTYPE_AP),
70 },
71};
72
73static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
74 {
75 .max = 2,
76 .types = BIT(NL80211_IFTYPE_STATION),
77 },
78};
79
80static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
81 {
82 .max = 1,
83 .types = BIT(NL80211_IFTYPE_STATION),
84 },
85 {
86 .max = 1,
87 .types = BIT(NL80211_IFTYPE_P2P_GO) |
88 BIT(NL80211_IFTYPE_AP),
89 },
90};
91
92static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
93 {
94 .max = 2,
95 .types = BIT(NL80211_IFTYPE_STATION),
96 },
97 {
98 .max = 1,
99 .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
100 },
101};
102
103static const struct ieee80211_iface_combination
104iwlagn_iface_combinations_dualmode[] = {
105 { .num_different_channels = 1,
106 .max_interfaces = 2,
107 .beacon_int_infra_match = true,
108 .limits = iwlagn_sta_ap_limits,
109 .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
110 },
111 { .num_different_channels = 1,
112 .max_interfaces = 2,
113 .limits = iwlagn_2sta_limits,
114 .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
115 },
116};
117
118static const struct ieee80211_iface_combination
119iwlagn_iface_combinations_p2p[] = {
120 { .num_different_channels = 1,
121 .max_interfaces = 2,
122 .beacon_int_infra_match = true,
123 .limits = iwlagn_p2p_sta_go_limits,
124 .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
125 },
126 { .num_different_channels = 1,
127 .max_interfaces = 2,
128 .limits = iwlagn_p2p_2sta_limits,
129 .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
130 },
131};
132
133/*
134 * Not a mac80211 entry point function, but it fits in with all the
135 * other mac80211 functions grouped here.
136 */
137int iwlagn_mac_setup_register(struct iwl_priv *priv,
138 const struct iwl_ucode_capabilities *capa)
139{
140 int ret;
141 struct ieee80211_hw *hw = priv->hw;
142 struct iwl_rxon_context *ctx;
143
144 hw->rate_control_algorithm = "iwl-agn-rs";
145
146 /* Tell mac80211 our characteristics */
147 hw->flags = IEEE80211_HW_SIGNAL_DBM |
148 IEEE80211_HW_AMPDU_AGGREGATION |
149 IEEE80211_HW_NEED_DTIM_PERIOD |
150 IEEE80211_HW_SPECTRUM_MGMT |
151 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
152 IEEE80211_HW_QUEUE_CONTROL |
153 IEEE80211_HW_SUPPORTS_PS |
154 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
155 IEEE80211_HW_WANT_MONITOR_VIF |
156 IEEE80211_HW_SCAN_WHILE_IDLE;
157
158 hw->offchannel_tx_hw_queue = IWL_AUX_QUEUE;
159 hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
160
161 /*
162 * Including the following line will crash some AP's. This
163 * workaround removes the stimulus which causes the crash until
164 * the AP software can be fixed.
165 hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
166 */
167
168 if (priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE)
169 hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
170 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
171
172#ifndef CONFIG_IWLWIFI_EXPERIMENTAL_MFP
173 /* enable 11w if the uCode advertise */
174 if (capa->flags & IWL_UCODE_TLV_FLAGS_MFP)
175#endif /* !CONFIG_IWLWIFI_EXPERIMENTAL_MFP */
176 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
177
178 hw->sta_data_size = sizeof(struct iwl_station_priv);
179 hw->vif_data_size = sizeof(struct iwl_vif_priv);
180
181 for_each_context(priv, ctx) {
182 hw->wiphy->interface_modes |= ctx->interface_modes;
183 hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
184 }
185
186 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
187
188 if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
189 hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
190 hw->wiphy->n_iface_combinations =
191 ARRAY_SIZE(iwlagn_iface_combinations_p2p);
192 } else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
193 hw->wiphy->iface_combinations =
194 iwlagn_iface_combinations_dualmode;
195 hw->wiphy->n_iface_combinations =
196 ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
197 }
198
199 hw->wiphy->max_remain_on_channel_duration = 1000;
200
201 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
202 WIPHY_FLAG_DISABLE_BEACON_HINTS |
203 WIPHY_FLAG_IBSS_RSN;
204
205 if (priv->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
206 priv->trans->ops->wowlan_suspend &&
207 device_can_wakeup(priv->trans->dev)) {
208 hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
209 WIPHY_WOWLAN_DISCONNECT |
210 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
211 WIPHY_WOWLAN_RFKILL_RELEASE;
212 if (!iwlwifi_mod_params.sw_crypto)
213 hw->wiphy->wowlan.flags |=
214 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
215 WIPHY_WOWLAN_GTK_REKEY_FAILURE;
216
217 hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
218 hw->wiphy->wowlan.pattern_min_len =
219 IWLAGN_WOWLAN_MIN_PATTERN_LEN;
220 hw->wiphy->wowlan.pattern_max_len =
221 IWLAGN_WOWLAN_MAX_PATTERN_LEN;
222 }
223
224 if (iwlwifi_mod_params.power_save)
225 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
226 else
227 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
228
229 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
230 /* we create the 802.11 header and a max-length SSID element */
231 hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 34;
232
233 /*
234 * We don't use all queues: 4 and 9 are unused and any
235 * aggregation queue gets mapped down to the AC queue.
236 */
237 hw->queues = IWLAGN_FIRST_AMPDU_QUEUE;
238
239 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
240
241 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
242 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
243 &priv->bands[IEEE80211_BAND_2GHZ];
244 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
245 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
246 &priv->bands[IEEE80211_BAND_5GHZ];
247
248 hw->wiphy->hw_version = priv->trans->hw_id;
249
250 iwl_leds_init(priv);
251
252 ret = ieee80211_register_hw(priv->hw);
253 if (ret) {
254 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
255 return ret;
256 }
257 priv->mac80211_registered = 1;
258
259 return 0;
260}
261
262void iwlagn_mac_unregister(struct iwl_priv *priv)
263{
264 if (!priv->mac80211_registered)
265 return;
266 iwl_leds_exit(priv);
267 ieee80211_unregister_hw(priv->hw);
268 priv->mac80211_registered = 0;
269}
270
271static int __iwl_up(struct iwl_priv *priv)
272{
273 struct iwl_rxon_context *ctx;
274 int ret;
275
276 lockdep_assert_held(&priv->mutex);
277
278 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
279 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
280 return -EIO;
281 }
282
283 for_each_context(priv, ctx) {
284 ret = iwlagn_alloc_bcast_station(priv, ctx);
285 if (ret) {
286 iwl_dealloc_bcast_stations(priv);
287 return ret;
288 }
289 }
290
291 ret = iwl_run_init_ucode(priv);
292 if (ret) {
293 IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
294 goto error;
295 }
296
297 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
298 if (ret) {
299 IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
300 goto error;
301 }
302
303 ret = iwl_alive_start(priv);
304 if (ret)
305 goto error;
306 return 0;
307
308 error:
309 set_bit(STATUS_EXIT_PENDING, &priv->status);
310 iwl_down(priv);
311 clear_bit(STATUS_EXIT_PENDING, &priv->status);
312
313 IWL_ERR(priv, "Unable to initialize device.\n");
314 return ret;
315}
316
317static int iwlagn_mac_start(struct ieee80211_hw *hw)
318{
319 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
320 int ret;
321
322 IWL_DEBUG_MAC80211(priv, "enter\n");
323
324 /* we should be verifying the device is ready to be opened */
325 mutex_lock(&priv->mutex);
326 ret = __iwl_up(priv);
327 mutex_unlock(&priv->mutex);
328 if (ret)
329 return ret;
330
331 IWL_DEBUG_INFO(priv, "Start UP work done.\n");
332
333 /* Now we should be done, and the READY bit should be set. */
334 if (WARN_ON(!test_bit(STATUS_READY, &priv->status)))
335 ret = -EIO;
336
337 iwlagn_led_enable(priv);
338
339 priv->is_open = 1;
340 IWL_DEBUG_MAC80211(priv, "leave\n");
341 return 0;
342}
343
344static void iwlagn_mac_stop(struct ieee80211_hw *hw)
345{
346 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
347
348 IWL_DEBUG_MAC80211(priv, "enter\n");
349
350 if (!priv->is_open)
351 return;
352
353 priv->is_open = 0;
354
355 mutex_lock(&priv->mutex);
356 iwl_down(priv);
357 mutex_unlock(&priv->mutex);
358
359 iwl_cancel_deferred_work(priv);
360
361 flush_workqueue(priv->workqueue);
362
363 /* User space software may expect getting rfkill changes
364 * even if interface is down, trans->down will leave the RF
365 * kill interrupt enabled
366 */
367 iwl_trans_stop_hw(priv->trans, false);
368
369 IWL_DEBUG_MAC80211(priv, "leave\n");
370}
371
372static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
373 struct ieee80211_vif *vif,
374 struct cfg80211_gtk_rekey_data *data)
375{
376 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
377
378 if (iwlwifi_mod_params.sw_crypto)
379 return;
380
381 IWL_DEBUG_MAC80211(priv, "enter\n");
382 mutex_lock(&priv->mutex);
383
384 if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
385 goto out;
386
387 memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
388 memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
389 priv->replay_ctr =
390 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
391 priv->have_rekey_data = true;
392
393 out:
394 mutex_unlock(&priv->mutex);
395 IWL_DEBUG_MAC80211(priv, "leave\n");
396}
397
398#ifdef CONFIG_PM_SLEEP
399
400static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
401 struct cfg80211_wowlan *wowlan)
402{
403 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
404 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
405 int ret;
406
407 if (WARN_ON(!wowlan))
408 return -EINVAL;
409
410 IWL_DEBUG_MAC80211(priv, "enter\n");
411 mutex_lock(&priv->mutex);
412
413 /* Don't attempt WoWLAN when not associated, tear down instead. */
414 if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
415 !iwl_is_associated_ctx(ctx)) {
416 ret = 1;
417 goto out;
418 }
419
420 ret = iwlagn_suspend(priv, wowlan);
421 if (ret)
422 goto error;
423
424 iwl_trans_wowlan_suspend(priv->trans);
425
426 goto out;
427
428 error:
429 priv->wowlan = false;
430 iwlagn_prepare_restart(priv);
431 ieee80211_restart_hw(priv->hw);
432 out:
433 mutex_unlock(&priv->mutex);
434 IWL_DEBUG_MAC80211(priv, "leave\n");
435
436 return ret;
437}
438
439static int iwlagn_mac_resume(struct ieee80211_hw *hw)
440{
441 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
442 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
443 struct ieee80211_vif *vif;
444 unsigned long flags;
445 u32 base, status = 0xffffffff;
446 int ret = -EIO;
447
448 IWL_DEBUG_MAC80211(priv, "enter\n");
449 mutex_lock(&priv->mutex);
450
451 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
452 CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
453
454 base = priv->device_pointers.error_event_table;
455 if (iwlagn_hw_valid_rtc_data_addr(base)) {
456 spin_lock_irqsave(&priv->trans->reg_lock, flags);
457 ret = iwl_grab_nic_access_silent(priv->trans);
458 if (likely(ret == 0)) {
459 iwl_write32(priv->trans, HBUS_TARG_MEM_RADDR, base);
460 status = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
461 iwl_release_nic_access(priv->trans);
462 }
463 spin_unlock_irqrestore(&priv->trans->reg_lock, flags);
464
465#ifdef CONFIG_IWLWIFI_DEBUGFS
466 if (ret == 0) {
467 const struct fw_img *img;
468
469 img = &(priv->fw->img[IWL_UCODE_WOWLAN]);
470 if (!priv->wowlan_sram) {
471 priv->wowlan_sram =
472 kzalloc(img->sec[IWL_UCODE_SECTION_DATA].len,
473 GFP_KERNEL);
474 }
475
476 if (priv->wowlan_sram)
477 _iwl_read_targ_mem_words(
478 priv->trans, 0x800000,
479 priv->wowlan_sram,
480 img->sec[IWL_UCODE_SECTION_DATA].len / 4);
481 }
482#endif
483 }
484
485 /* we'll clear ctx->vif during iwlagn_prepare_restart() */
486 vif = ctx->vif;
487
488 priv->wowlan = false;
489
490 iwlagn_prepare_restart(priv);
491
492 memset((void *)&ctx->active, 0, sizeof(ctx->active));
493 iwl_connection_init_rx_config(priv, ctx);
494 iwlagn_set_rxon_chain(priv, ctx);
495
496 mutex_unlock(&priv->mutex);
497 IWL_DEBUG_MAC80211(priv, "leave\n");
498
499 ieee80211_resume_disconnect(vif);
500
501 return 1;
502}
503
504static void iwlagn_mac_set_wakeup(struct ieee80211_hw *hw, bool enabled)
505{
506 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
507
508 device_set_wakeup_enable(priv->trans->dev, enabled);
509}
510#endif
511
512static void iwlagn_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
513{
514 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
515
516 IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
517 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
518
519 if (iwlagn_tx_skb(priv, skb))
520 dev_kfree_skb_any(skb);
521}
522
523static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
524 struct ieee80211_vif *vif,
525 struct ieee80211_key_conf *keyconf,
526 struct ieee80211_sta *sta,
527 u32 iv32, u16 *phase1key)
528{
529 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
530
531 iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
532}
533
534static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
535 struct ieee80211_vif *vif,
536 struct ieee80211_sta *sta,
537 struct ieee80211_key_conf *key)
538{
539 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
540 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
541 struct iwl_rxon_context *ctx = vif_priv->ctx;
542 int ret;
543 bool is_default_wep_key = false;
544
545 IWL_DEBUG_MAC80211(priv, "enter\n");
546
547 if (iwlwifi_mod_params.sw_crypto) {
548 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
549 return -EOPNOTSUPP;
550 }
551
552 switch (key->cipher) {
553 case WLAN_CIPHER_SUITE_TKIP:
554 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
555 /* fall through */
556 case WLAN_CIPHER_SUITE_CCMP:
557 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
558 break;
559 default:
560 break;
561 }
562
563 /*
564 * We could program these keys into the hardware as well, but we
565 * don't expect much multicast traffic in IBSS and having keys
566 * for more stations is probably more useful.
567 *
568 * Mark key TX-only and return 0.
569 */
570 if (vif->type == NL80211_IFTYPE_ADHOC &&
571 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
572 key->hw_key_idx = WEP_INVALID_OFFSET;
573 return 0;
574 }
575
576 /* If they key was TX-only, accept deletion */
577 if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
578 return 0;
579
580 mutex_lock(&priv->mutex);
581 iwl_scan_cancel_timeout(priv, 100);
582
583 BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
584
585 /*
586 * If we are getting WEP group key and we didn't receive any key mapping
587 * so far, we are in legacy wep mode (group key only), otherwise we are
588 * in 1X mode.
589 * In legacy wep mode, we use another host command to the uCode.
590 */
591 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
592 key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
593 if (cmd == SET_KEY)
594 is_default_wep_key = !ctx->key_mapping_keys;
595 else
596 is_default_wep_key =
597 key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
598 }
599
600
601 switch (cmd) {
602 case SET_KEY:
603 if (is_default_wep_key) {
604 ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
605 break;
606 }
607 ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
608 if (ret) {
609 /*
610 * can't add key for RX, but we don't need it
611 * in the device for TX so still return 0
612 */
613 ret = 0;
614 key->hw_key_idx = WEP_INVALID_OFFSET;
615 }
616
617 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
618 break;
619 case DISABLE_KEY:
620 if (is_default_wep_key)
621 ret = iwl_remove_default_wep_key(priv, ctx, key);
622 else
623 ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
624
625 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
626 break;
627 default:
628 ret = -EINVAL;
629 }
630
631 mutex_unlock(&priv->mutex);
632 IWL_DEBUG_MAC80211(priv, "leave\n");
633
634 return ret;
635}
636
637static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
638 struct ieee80211_vif *vif,
639 enum ieee80211_ampdu_mlme_action action,
640 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
641 u8 buf_size)
642{
643 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
644 int ret = -EINVAL;
645 struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
646
647 IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
648 sta->addr, tid);
649
650 if (!(priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE))
651 return -EACCES;
652
653 IWL_DEBUG_MAC80211(priv, "enter\n");
654 mutex_lock(&priv->mutex);
655
656 switch (action) {
657 case IEEE80211_AMPDU_RX_START:
658 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
659 break;
660 IWL_DEBUG_HT(priv, "start Rx\n");
661 ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
662 break;
663 case IEEE80211_AMPDU_RX_STOP:
664 IWL_DEBUG_HT(priv, "stop Rx\n");
665 ret = iwl_sta_rx_agg_stop(priv, sta, tid);
666 break;
667 case IEEE80211_AMPDU_TX_START:
668 if (!priv->trans->ops->tx_agg_setup)
669 break;
670 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
671 break;
672 IWL_DEBUG_HT(priv, "start Tx\n");
673 ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
674 break;
675 case IEEE80211_AMPDU_TX_STOP:
676 IWL_DEBUG_HT(priv, "stop Tx\n");
677 ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
678 if ((ret == 0) && (priv->agg_tids_count > 0)) {
679 priv->agg_tids_count--;
680 IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
681 priv->agg_tids_count);
682 }
683 if (!priv->agg_tids_count &&
684 priv->hw_params.use_rts_for_aggregation) {
685 /*
686 * switch off RTS/CTS if it was previously enabled
687 */
688 sta_priv->lq_sta.lq.general_params.flags &=
689 ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
690 iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
691 &sta_priv->lq_sta.lq, CMD_ASYNC, false);
692 }
693 break;
694 case IEEE80211_AMPDU_TX_OPERATIONAL:
695 ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
696 break;
697 }
698 mutex_unlock(&priv->mutex);
699 IWL_DEBUG_MAC80211(priv, "leave\n");
700 return ret;
701}
702
703static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
704 struct ieee80211_vif *vif,
705 struct ieee80211_sta *sta)
706{
707 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
708 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
709 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
710 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
711 int ret;
712 u8 sta_id;
713
714 IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
715 sta->addr);
716 sta_priv->sta_id = IWL_INVALID_STATION;
717
718 atomic_set(&sta_priv->pending_frames, 0);
719 if (vif->type == NL80211_IFTYPE_AP)
720 sta_priv->client = true;
721
722 ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
723 is_ap, sta, &sta_id);
724 if (ret) {
725 IWL_ERR(priv, "Unable to add station %pM (%d)\n",
726 sta->addr, ret);
727 /* Should we return success if return code is EEXIST ? */
728 return ret;
729 }
730
731 sta_priv->sta_id = sta_id;
732
733 return 0;
734}
735
736static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
737 struct ieee80211_vif *vif,
738 struct ieee80211_sta *sta)
739{
740 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
741 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
742 int ret;
743
744 IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n", sta->addr);
745
746 if (vif->type == NL80211_IFTYPE_STATION) {
747 /*
748 * Station will be removed from device when the RXON
749 * is set to unassociated -- just deactivate it here
750 * to avoid re-programming it.
751 */
752 ret = 0;
753 iwl_deactivate_station(priv, sta_priv->sta_id, sta->addr);
754 } else {
755 ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
756 if (ret)
757 IWL_DEBUG_QUIET_RFKILL(priv,
758 "Error removing station %pM\n", sta->addr);
759 }
760 return ret;
761}
762
763static int iwlagn_mac_sta_state(struct ieee80211_hw *hw,
764 struct ieee80211_vif *vif,
765 struct ieee80211_sta *sta,
766 enum ieee80211_sta_state old_state,
767 enum ieee80211_sta_state new_state)
768{
769 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
770 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
771 enum {
772 NONE, ADD, REMOVE, HT_RATE_INIT, ADD_RATE_INIT,
773 } op = NONE;
774 int ret;
775
776 IWL_DEBUG_MAC80211(priv, "station %pM state change %d->%d\n",
777 sta->addr, old_state, new_state);
778
779 mutex_lock(&priv->mutex);
780 if (vif->type == NL80211_IFTYPE_STATION) {
781 if (old_state == IEEE80211_STA_NOTEXIST &&
782 new_state == IEEE80211_STA_NONE)
783 op = ADD;
784 else if (old_state == IEEE80211_STA_NONE &&
785 new_state == IEEE80211_STA_NOTEXIST)
786 op = REMOVE;
787 else if (old_state == IEEE80211_STA_AUTH &&
788 new_state == IEEE80211_STA_ASSOC)
789 op = HT_RATE_INIT;
790 } else {
791 if (old_state == IEEE80211_STA_AUTH &&
792 new_state == IEEE80211_STA_ASSOC)
793 op = ADD_RATE_INIT;
794 else if (old_state == IEEE80211_STA_ASSOC &&
795 new_state == IEEE80211_STA_AUTH)
796 op = REMOVE;
797 }
798
799 switch (op) {
800 case ADD:
801 ret = iwlagn_mac_sta_add(hw, vif, sta);
802 break;
803 case REMOVE:
804 ret = iwlagn_mac_sta_remove(hw, vif, sta);
805 break;
806 case ADD_RATE_INIT:
807 ret = iwlagn_mac_sta_add(hw, vif, sta);
808 if (ret)
809 break;
810 /* Initialize rate scaling */
811 IWL_DEBUG_INFO(priv,
812 "Initializing rate scaling for station %pM\n",
813 sta->addr);
814 iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
815 ret = 0;
816 break;
817 case HT_RATE_INIT:
818 /* Initialize rate scaling */
819 ret = iwl_sta_update_ht(priv, vif_priv->ctx, sta);
820 if (ret)
821 break;
822 IWL_DEBUG_INFO(priv,
823 "Initializing rate scaling for station %pM\n",
824 sta->addr);
825 iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
826 ret = 0;
827 break;
828 default:
829 ret = 0;
830 break;
831 }
832
833 /*
834 * mac80211 might WARN if we fail, but due the way we
835 * (badly) handle hard rfkill, we might fail here
836 */
837 if (iwl_is_rfkill(priv))
838 ret = 0;
839
840 mutex_unlock(&priv->mutex);
841 IWL_DEBUG_MAC80211(priv, "leave\n");
842
843 return ret;
844}
845
846static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
847 struct ieee80211_channel_switch *ch_switch)
848{
849 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
850 struct ieee80211_conf *conf = &hw->conf;
851 struct ieee80211_channel *channel = ch_switch->channel;
852 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
853 /*
854 * MULTI-FIXME
855 * When we add support for multiple interfaces, we need to
856 * revisit this. The channel switch command in the device
857 * only affects the BSS context, but what does that really
858 * mean? And what if we get a CSA on the second interface?
859 * This needs a lot of work.
860 */
861 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
862 u16 ch;
863
864 IWL_DEBUG_MAC80211(priv, "enter\n");
865
866 mutex_lock(&priv->mutex);
867
868 if (iwl_is_rfkill(priv))
869 goto out;
870
871 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
872 test_bit(STATUS_SCANNING, &priv->status) ||
873 test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
874 goto out;
875
876 if (!iwl_is_associated_ctx(ctx))
877 goto out;
878
879 if (!priv->lib->set_channel_switch)
880 goto out;
881
882 ch = channel->hw_value;
883 if (le16_to_cpu(ctx->active.channel) == ch)
884 goto out;
885
886 priv->current_ht_config.smps = conf->smps_mode;
887
888 /* Configure HT40 channels */
889 ctx->ht.enabled = conf_is_ht(conf);
890 if (ctx->ht.enabled)
891 iwlagn_config_ht40(conf, ctx);
892 else
893 ctx->ht.is_40mhz = false;
894
895 if ((le16_to_cpu(ctx->staging.channel) != ch))
896 ctx->staging.flags = 0;
897
898 iwl_set_rxon_channel(priv, channel, ctx);
899 iwl_set_rxon_ht(priv, ht_conf);
900 iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
901
902 /*
903 * at this point, staging_rxon has the
904 * configuration for channel switch
905 */
906 set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
907 priv->switch_channel = cpu_to_le16(ch);
908 if (priv->lib->set_channel_switch(priv, ch_switch)) {
909 clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
910 priv->switch_channel = 0;
911 ieee80211_chswitch_done(ctx->vif, false);
912 }
913
914out:
915 mutex_unlock(&priv->mutex);
916 IWL_DEBUG_MAC80211(priv, "leave\n");
917}
918
919void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
920{
921 /*
922 * MULTI-FIXME
923 * See iwlagn_mac_channel_switch.
924 */
925 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
926
927 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
928 return;
929
930 if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
931 ieee80211_chswitch_done(ctx->vif, is_success);
932}
933
934static void iwlagn_configure_filter(struct ieee80211_hw *hw,
935 unsigned int changed_flags,
936 unsigned int *total_flags,
937 u64 multicast)
938{
939 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
940 __le32 filter_or = 0, filter_nand = 0;
941 struct iwl_rxon_context *ctx;
942
943#define CHK(test, flag) do { \
944 if (*total_flags & (test)) \
945 filter_or |= (flag); \
946 else \
947 filter_nand |= (flag); \
948 } while (0)
949
950 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
951 changed_flags, *total_flags);
952
953 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
954 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
955 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
956 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
957
958#undef CHK
959
960 mutex_lock(&priv->mutex);
961
962 for_each_context(priv, ctx) {
963 ctx->staging.filter_flags &= ~filter_nand;
964 ctx->staging.filter_flags |= filter_or;
965
966 /*
967 * Not committing directly because hardware can perform a scan,
968 * but we'll eventually commit the filter flags change anyway.
969 */
970 }
971
972 mutex_unlock(&priv->mutex);
973
974 /*
975 * Receiving all multicast frames is always enabled by the
976 * default flags setup in iwl_connection_init_rx_config()
977 * since we currently do not support programming multicast
978 * filters into the device.
979 */
980 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
981 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
982}
983
984static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
985{
986 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
987
988 mutex_lock(&priv->mutex);
989 IWL_DEBUG_MAC80211(priv, "enter\n");
990
991 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
992 IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
993 goto done;
994 }
995 if (iwl_is_rfkill(priv)) {
996 IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
997 goto done;
998 }
999
1000 /*
1001 * mac80211 will not push any more frames for transmit
1002 * until the flush is completed
1003 */
1004 if (drop) {
1005 IWL_DEBUG_MAC80211(priv, "send flush command\n");
1006 if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
1007 IWL_ERR(priv, "flush request fail\n");
1008 goto done;
1009 }
1010 }
1011 IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
1012 iwl_trans_wait_tx_queue_empty(priv->trans);
1013done:
1014 mutex_unlock(&priv->mutex);
1015 IWL_DEBUG_MAC80211(priv, "leave\n");
1016}
1017
1018static int iwlagn_mac_remain_on_channel(struct ieee80211_hw *hw,
1019 struct ieee80211_channel *channel,
1020 enum nl80211_channel_type channel_type,
1021 int duration)
1022{
1023 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1024 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
1025 int err = 0;
1026
1027 if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
1028 return -EOPNOTSUPP;
1029
1030 if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
1031 return -EOPNOTSUPP;
1032
1033 IWL_DEBUG_MAC80211(priv, "enter\n");
1034 mutex_lock(&priv->mutex);
1035
1036 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1037 err = -EBUSY;
1038 goto out;
1039 }
1040
1041 priv->hw_roc_channel = channel;
1042 priv->hw_roc_chantype = channel_type;
1043 /* convert from ms to TU */
1044 priv->hw_roc_duration = DIV_ROUND_UP(1000 * duration, 1024);
1045 priv->hw_roc_start_notified = false;
1046 cancel_delayed_work(&priv->hw_roc_disable_work);
1047
1048 if (!ctx->is_active) {
1049 static const struct iwl_qos_info default_qos_data = {
1050 .def_qos_parm = {
1051 .ac[0] = {
1052 .cw_min = cpu_to_le16(3),
1053 .cw_max = cpu_to_le16(7),
1054 .aifsn = 2,
1055 .edca_txop = cpu_to_le16(1504),
1056 },
1057 .ac[1] = {
1058 .cw_min = cpu_to_le16(7),
1059 .cw_max = cpu_to_le16(15),
1060 .aifsn = 2,
1061 .edca_txop = cpu_to_le16(3008),
1062 },
1063 .ac[2] = {
1064 .cw_min = cpu_to_le16(15),
1065 .cw_max = cpu_to_le16(1023),
1066 .aifsn = 3,
1067 },
1068 .ac[3] = {
1069 .cw_min = cpu_to_le16(15),
1070 .cw_max = cpu_to_le16(1023),
1071 .aifsn = 7,
1072 },
1073 },
1074 };
1075
1076 ctx->is_active = true;
1077 ctx->qos_data = default_qos_data;
1078 ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
1079 memcpy(ctx->staging.node_addr,
1080 priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
1081 ETH_ALEN);
1082 memcpy(ctx->staging.bssid_addr,
1083 priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
1084 ETH_ALEN);
1085 err = iwlagn_commit_rxon(priv, ctx);
1086 if (err)
1087 goto out;
1088 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
1089 RXON_FILTER_PROMISC_MSK |
1090 RXON_FILTER_CTL2HOST_MSK;
1091
1092 err = iwlagn_commit_rxon(priv, ctx);
1093 if (err) {
1094 iwlagn_disable_roc(priv);
1095 goto out;
1096 }
1097 priv->hw_roc_setup = true;
1098 }
1099
1100 err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
1101 if (err)
1102 iwlagn_disable_roc(priv);
1103
1104 out:
1105 mutex_unlock(&priv->mutex);
1106 IWL_DEBUG_MAC80211(priv, "leave\n");
1107
1108 return err;
1109}
1110
1111static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
1112{
1113 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1114
1115 if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
1116 return -EOPNOTSUPP;
1117
1118 IWL_DEBUG_MAC80211(priv, "enter\n");
1119 mutex_lock(&priv->mutex);
1120 iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
1121 iwlagn_disable_roc(priv);
1122 mutex_unlock(&priv->mutex);
1123 IWL_DEBUG_MAC80211(priv, "leave\n");
1124
1125 return 0;
1126}
1127
1128static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
1129 enum ieee80211_rssi_event rssi_event)
1130{
1131 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1132
1133 IWL_DEBUG_MAC80211(priv, "enter\n");
1134 mutex_lock(&priv->mutex);
1135
1136 if (priv->cfg->bt_params &&
1137 priv->cfg->bt_params->advanced_bt_coexist) {
1138 if (rssi_event == RSSI_EVENT_LOW)
1139 priv->bt_enable_pspoll = true;
1140 else if (rssi_event == RSSI_EVENT_HIGH)
1141 priv->bt_enable_pspoll = false;
1142
1143 iwlagn_send_advance_bt_config(priv);
1144 } else {
1145 IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
1146 "ignoring RSSI callback\n");
1147 }
1148
1149 mutex_unlock(&priv->mutex);
1150 IWL_DEBUG_MAC80211(priv, "leave\n");
1151}
1152
1153static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
1154 struct ieee80211_sta *sta, bool set)
1155{
1156 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1157
1158 queue_work(priv->workqueue, &priv->beacon_update);
1159
1160 return 0;
1161}
1162
1163static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
1164 struct ieee80211_vif *vif, u16 queue,
1165 const struct ieee80211_tx_queue_params *params)
1166{
1167 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1168 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1169 struct iwl_rxon_context *ctx = vif_priv->ctx;
1170 int q;
1171
1172 if (WARN_ON(!ctx))
1173 return -EINVAL;
1174
1175 IWL_DEBUG_MAC80211(priv, "enter\n");
1176
1177 if (!iwl_is_ready_rf(priv)) {
1178 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1179 return -EIO;
1180 }
1181
1182 if (queue >= AC_NUM) {
1183 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1184 return 0;
1185 }
1186
1187 q = AC_NUM - 1 - queue;
1188
1189 mutex_lock(&priv->mutex);
1190
1191 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1192 cpu_to_le16(params->cw_min);
1193 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1194 cpu_to_le16(params->cw_max);
1195 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1196 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1197 cpu_to_le16((params->txop * 32));
1198
1199 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1200
1201 mutex_unlock(&priv->mutex);
1202
1203 IWL_DEBUG_MAC80211(priv, "leave\n");
1204 return 0;
1205}
1206
1207static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
1208{
1209 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1210
1211 return priv->ibss_manager == IWL_IBSS_MANAGER;
1212}
1213
1214static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1215{
1216 iwl_connection_init_rx_config(priv, ctx);
1217
1218 iwlagn_set_rxon_chain(priv, ctx);
1219
1220 return iwlagn_commit_rxon(priv, ctx);
1221}
1222
1223static int iwl_setup_interface(struct iwl_priv *priv,
1224 struct iwl_rxon_context *ctx)
1225{
1226 struct ieee80211_vif *vif = ctx->vif;
1227 int err, ac;
1228
1229 lockdep_assert_held(&priv->mutex);
1230
1231 /*
1232 * This variable will be correct only when there's just
1233 * a single context, but all code using it is for hardware
1234 * that supports only one context.
1235 */
1236 priv->iw_mode = vif->type;
1237
1238 ctx->is_active = true;
1239
1240 err = iwl_set_mode(priv, ctx);
1241 if (err) {
1242 if (!ctx->always_active)
1243 ctx->is_active = false;
1244 return err;
1245 }
1246
1247 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1248 vif->type == NL80211_IFTYPE_ADHOC) {
1249 /*
1250 * pretend to have high BT traffic as long as we
1251 * are operating in IBSS mode, as this will cause
1252 * the rate scaling etc. to behave as intended.
1253 */
1254 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1255 }
1256
1257 /* set up queue mappings */
1258 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1259 vif->hw_queue[ac] = ctx->ac_to_queue[ac];
1260
1261 if (vif->type == NL80211_IFTYPE_AP)
1262 vif->cab_queue = ctx->mcast_queue;
1263 else
1264 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
1265
1266 return 0;
1267}
1268
1269static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
1270 struct ieee80211_vif *vif)
1271{
1272 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1273 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1274 struct iwl_rxon_context *tmp, *ctx = NULL;
1275 int err;
1276 enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1277 bool reset = false;
1278
1279 IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1280 viftype, vif->addr);
1281
1282 cancel_delayed_work_sync(&priv->hw_roc_disable_work);
1283
1284 mutex_lock(&priv->mutex);
1285
1286 iwlagn_disable_roc(priv);
1287
1288 if (!iwl_is_ready_rf(priv)) {
1289 IWL_WARN(priv, "Try to add interface when device not ready\n");
1290 err = -EINVAL;
1291 goto out;
1292 }
1293
1294 for_each_context(priv, tmp) {
1295 u32 possible_modes =
1296 tmp->interface_modes | tmp->exclusive_interface_modes;
1297
1298 if (tmp->vif) {
1299 /* On reset we need to add the same interface again */
1300 if (tmp->vif == vif) {
1301 reset = true;
1302 ctx = tmp;
1303 break;
1304 }
1305
1306 /* check if this busy context is exclusive */
1307 if (tmp->exclusive_interface_modes &
1308 BIT(tmp->vif->type)) {
1309 err = -EINVAL;
1310 goto out;
1311 }
1312 continue;
1313 }
1314
1315 if (!(possible_modes & BIT(viftype)))
1316 continue;
1317
1318 /* have maybe usable context w/o interface */
1319 ctx = tmp;
1320 break;
1321 }
1322
1323 if (!ctx) {
1324 err = -EOPNOTSUPP;
1325 goto out;
1326 }
1327
1328 vif_priv->ctx = ctx;
1329 ctx->vif = vif;
1330
1331 err = iwl_setup_interface(priv, ctx);
1332 if (!err || reset)
1333 goto out;
1334
1335 ctx->vif = NULL;
1336 priv->iw_mode = NL80211_IFTYPE_STATION;
1337 out:
1338 mutex_unlock(&priv->mutex);
1339
1340 IWL_DEBUG_MAC80211(priv, "leave\n");
1341 return err;
1342}
1343
1344static void iwl_teardown_interface(struct iwl_priv *priv,
1345 struct ieee80211_vif *vif,
1346 bool mode_change)
1347{
1348 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1349
1350 lockdep_assert_held(&priv->mutex);
1351
1352 if (priv->scan_vif == vif) {
1353 iwl_scan_cancel_timeout(priv, 200);
1354 iwl_force_scan_end(priv);
1355 }
1356
1357 if (!mode_change) {
1358 iwl_set_mode(priv, ctx);
1359 if (!ctx->always_active)
1360 ctx->is_active = false;
1361 }
1362
1363 /*
1364 * When removing the IBSS interface, overwrite the
1365 * BT traffic load with the stored one from the last
1366 * notification, if any. If this is a device that
1367 * doesn't implement this, this has no effect since
1368 * both values are the same and zero.
1369 */
1370 if (vif->type == NL80211_IFTYPE_ADHOC)
1371 priv->bt_traffic_load = priv->last_bt_traffic_load;
1372}
1373
1374static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
1375 struct ieee80211_vif *vif)
1376{
1377 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1378 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1379
1380 IWL_DEBUG_MAC80211(priv, "enter\n");
1381
1382 mutex_lock(&priv->mutex);
1383
1384 if (WARN_ON(ctx->vif != vif)) {
1385 struct iwl_rxon_context *tmp;
1386 IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
1387 for_each_context(priv, tmp)
1388 IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
1389 tmp->ctxid, tmp, tmp->vif);
1390 }
1391 ctx->vif = NULL;
1392
1393 iwl_teardown_interface(priv, vif, false);
1394
1395 mutex_unlock(&priv->mutex);
1396
1397 IWL_DEBUG_MAC80211(priv, "leave\n");
1398
1399}
1400
1401static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
1402 struct ieee80211_vif *vif,
1403 enum nl80211_iftype newtype, bool newp2p)
1404{
1405 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1406 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1407 struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1408 struct iwl_rxon_context *tmp;
1409 enum nl80211_iftype newviftype = newtype;
1410 u32 interface_modes;
1411 int err;
1412
1413 IWL_DEBUG_MAC80211(priv, "enter\n");
1414
1415 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1416
1417 mutex_lock(&priv->mutex);
1418
1419 if (!ctx->vif || !iwl_is_ready_rf(priv)) {
1420 /*
1421 * Huh? But wait ... this can maybe happen when
1422 * we're in the middle of a firmware restart!
1423 */
1424 err = -EBUSY;
1425 goto out;
1426 }
1427
1428 interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1429
1430 if (!(interface_modes & BIT(newtype))) {
1431 err = -EBUSY;
1432 goto out;
1433 }
1434
1435 /*
1436 * Refuse a change that should be done by moving from the PAN
1437 * context to the BSS context instead, if the BSS context is
1438 * available and can support the new interface type.
1439 */
1440 if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
1441 (bss_ctx->interface_modes & BIT(newtype) ||
1442 bss_ctx->exclusive_interface_modes & BIT(newtype))) {
1443 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1444 err = -EBUSY;
1445 goto out;
1446 }
1447
1448 if (ctx->exclusive_interface_modes & BIT(newtype)) {
1449 for_each_context(priv, tmp) {
1450 if (ctx == tmp)
1451 continue;
1452
1453 if (!tmp->vif)
1454 continue;
1455
1456 /*
1457 * The current mode switch would be exclusive, but
1458 * another context is active ... refuse the switch.
1459 */
1460 err = -EBUSY;
1461 goto out;
1462 }
1463 }
1464
1465 /* success */
1466 iwl_teardown_interface(priv, vif, true);
1467 vif->type = newviftype;
1468 vif->p2p = newp2p;
1469 err = iwl_setup_interface(priv, ctx);
1470 WARN_ON(err);
1471 /*
1472 * We've switched internally, but submitting to the
1473 * device may have failed for some reason. Mask this
1474 * error, because otherwise mac80211 will not switch
1475 * (and set the interface type back) and we'll be
1476 * out of sync with it.
1477 */
1478 err = 0;
1479
1480 out:
1481 mutex_unlock(&priv->mutex);
1482 IWL_DEBUG_MAC80211(priv, "leave\n");
1483
1484 return err;
1485}
1486
1487static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
1488 struct ieee80211_vif *vif,
1489 struct cfg80211_scan_request *req)
1490{
1491 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1492 int ret;
1493
1494 IWL_DEBUG_MAC80211(priv, "enter\n");
1495
1496 if (req->n_channels == 0)
1497 return -EINVAL;
1498
1499 mutex_lock(&priv->mutex);
1500
1501 /*
1502 * If an internal scan is in progress, just set
1503 * up the scan_request as per above.
1504 */
1505 if (priv->scan_type != IWL_SCAN_NORMAL) {
1506 IWL_DEBUG_SCAN(priv,
1507 "SCAN request during internal scan - defer\n");
1508 priv->scan_request = req;
1509 priv->scan_vif = vif;
1510 ret = 0;
1511 } else {
1512 priv->scan_request = req;
1513 priv->scan_vif = vif;
1514 /*
1515 * mac80211 will only ask for one band at a time
1516 * so using channels[0] here is ok
1517 */
1518 ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
1519 req->channels[0]->band);
1520 if (ret) {
1521 priv->scan_request = NULL;
1522 priv->scan_vif = NULL;
1523 }
1524 }
1525
1526 IWL_DEBUG_MAC80211(priv, "leave\n");
1527
1528 mutex_unlock(&priv->mutex);
1529
1530 return ret;
1531}
1532
1533static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1534{
1535 struct iwl_addsta_cmd cmd = {
1536 .mode = STA_CONTROL_MODIFY_MSK,
1537 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1538 .sta.sta_id = sta_id,
1539 };
1540
1541 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1542}
1543
1544static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
1545 struct ieee80211_vif *vif,
1546 enum sta_notify_cmd cmd,
1547 struct ieee80211_sta *sta)
1548{
1549 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1550 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1551 int sta_id;
1552
1553 IWL_DEBUG_MAC80211(priv, "enter\n");
1554
1555 switch (cmd) {
1556 case STA_NOTIFY_SLEEP:
1557 WARN_ON(!sta_priv->client);
1558 sta_priv->asleep = true;
1559 if (atomic_read(&sta_priv->pending_frames) > 0)
1560 ieee80211_sta_block_awake(hw, sta, true);
1561 break;
1562 case STA_NOTIFY_AWAKE:
1563 WARN_ON(!sta_priv->client);
1564 if (!sta_priv->asleep)
1565 break;
1566 sta_priv->asleep = false;
1567 sta_id = iwl_sta_id(sta);
1568 if (sta_id != IWL_INVALID_STATION)
1569 iwl_sta_modify_ps_wake(priv, sta_id);
1570 break;
1571 default:
1572 break;
1573 }
1574 IWL_DEBUG_MAC80211(priv, "leave\n");
1575}
1576
1577struct ieee80211_ops iwlagn_hw_ops = {
1578 .tx = iwlagn_mac_tx,
1579 .start = iwlagn_mac_start,
1580 .stop = iwlagn_mac_stop,
1581#ifdef CONFIG_PM_SLEEP
1582 .suspend = iwlagn_mac_suspend,
1583 .resume = iwlagn_mac_resume,
1584 .set_wakeup = iwlagn_mac_set_wakeup,
1585#endif
1586 .add_interface = iwlagn_mac_add_interface,
1587 .remove_interface = iwlagn_mac_remove_interface,
1588 .change_interface = iwlagn_mac_change_interface,
1589 .config = iwlagn_mac_config,
1590 .configure_filter = iwlagn_configure_filter,
1591 .set_key = iwlagn_mac_set_key,
1592 .update_tkip_key = iwlagn_mac_update_tkip_key,
1593 .set_rekey_data = iwlagn_mac_set_rekey_data,
1594 .conf_tx = iwlagn_mac_conf_tx,
1595 .bss_info_changed = iwlagn_bss_info_changed,
1596 .ampdu_action = iwlagn_mac_ampdu_action,
1597 .hw_scan = iwlagn_mac_hw_scan,
1598 .sta_notify = iwlagn_mac_sta_notify,
1599 .sta_state = iwlagn_mac_sta_state,
1600 .channel_switch = iwlagn_mac_channel_switch,
1601 .flush = iwlagn_mac_flush,
1602 .tx_last_beacon = iwlagn_mac_tx_last_beacon,
1603 .remain_on_channel = iwlagn_mac_remain_on_channel,
1604 .cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
1605 .rssi_callback = iwlagn_mac_rssi_callback,
1606 CFG80211_TESTMODE_CMD(iwlagn_mac_testmode_cmd)
1607 CFG80211_TESTMODE_DUMP(iwlagn_mac_testmode_dump)
1608 .set_tim = iwlagn_mac_set_tim,
1609};
1610
1611/* This function both allocates and initializes hw and priv. */
1612struct ieee80211_hw *iwl_alloc_all(void)
1613{
1614 struct iwl_priv *priv;
1615 struct iwl_op_mode *op_mode;
1616 /* mac80211 allocates memory for this device instance, including
1617 * space for this driver's private structure */
1618 struct ieee80211_hw *hw;
1619
1620 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
1621 sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
1622 if (!hw)
1623 goto out;
1624
1625 op_mode = hw->priv;
1626 priv = IWL_OP_MODE_GET_DVM(op_mode);
1627 priv->hw = hw;
1628
1629out:
1630 return hw;
1631}
diff --git a/drivers/net/wireless/iwlwifi/dvm/main.c b/drivers/net/wireless/iwlwifi/dvm/main.c
new file mode 100644
index 000000000000..d67ae99c00b9
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/main.c
@@ -0,0 +1,2371 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/slab.h>
36#include <linux/delay.h>
37#include <linux/sched.h>
38#include <linux/skbuff.h>
39#include <linux/netdevice.h>
40#include <linux/etherdevice.h>
41#include <linux/if_arp.h>
42
43#include <net/mac80211.h>
44
45#include <asm/div64.h>
46
47#include "iwl-io.h"
48#include "iwl-trans.h"
49#include "iwl-op-mode.h"
50#include "iwl-drv.h"
51#include "iwl-modparams.h"
52
53#include "eeprom.h"
54#include "dev.h"
55#include "calib.h"
56#include "agn.h"
57
58/******************************************************************************
59 *
60 * module boiler plate
61 *
62 ******************************************************************************/
63
64/*
65 * module name, copyright, version, etc.
66 */
67#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link AGN driver for Linux"
68
69#ifdef CONFIG_IWLWIFI_DEBUG
70#define VD "d"
71#else
72#define VD
73#endif
74
75#define DRV_VERSION IWLWIFI_VERSION VD
76
77
78MODULE_DESCRIPTION(DRV_DESCRIPTION);
79MODULE_VERSION(DRV_VERSION);
80MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
81MODULE_LICENSE("GPL");
82
83void iwl_update_chain_flags(struct iwl_priv *priv)
84{
85 struct iwl_rxon_context *ctx;
86
87 for_each_context(priv, ctx) {
88 iwlagn_set_rxon_chain(priv, ctx);
89 if (ctx->active.rx_chain != ctx->staging.rx_chain)
90 iwlagn_commit_rxon(priv, ctx);
91 }
92}
93
94/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
95static void iwl_set_beacon_tim(struct iwl_priv *priv,
96 struct iwl_tx_beacon_cmd *tx_beacon_cmd,
97 u8 *beacon, u32 frame_size)
98{
99 u16 tim_idx;
100 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
101
102 /*
103 * The index is relative to frame start but we start looking at the
104 * variable-length part of the beacon.
105 */
106 tim_idx = mgmt->u.beacon.variable - beacon;
107
108 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
109 while ((tim_idx < (frame_size - 2)) &&
110 (beacon[tim_idx] != WLAN_EID_TIM))
111 tim_idx += beacon[tim_idx+1] + 2;
112
113 /* If TIM field was found, set variables */
114 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
115 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
116 tx_beacon_cmd->tim_size = beacon[tim_idx+1];
117 } else
118 IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
119}
120
121int iwlagn_send_beacon_cmd(struct iwl_priv *priv)
122{
123 struct iwl_tx_beacon_cmd *tx_beacon_cmd;
124 struct iwl_host_cmd cmd = {
125 .id = REPLY_TX_BEACON,
126 .flags = CMD_SYNC,
127 };
128 struct ieee80211_tx_info *info;
129 u32 frame_size;
130 u32 rate_flags;
131 u32 rate;
132
133 /*
134 * We have to set up the TX command, the TX Beacon command, and the
135 * beacon contents.
136 */
137
138 lockdep_assert_held(&priv->mutex);
139
140 if (!priv->beacon_ctx) {
141 IWL_ERR(priv, "trying to build beacon w/o beacon context!\n");
142 return 0;
143 }
144
145 if (WARN_ON(!priv->beacon_skb))
146 return -EINVAL;
147
148 /* Allocate beacon command */
149 if (!priv->beacon_cmd)
150 priv->beacon_cmd = kzalloc(sizeof(*tx_beacon_cmd), GFP_KERNEL);
151 tx_beacon_cmd = priv->beacon_cmd;
152 if (!tx_beacon_cmd)
153 return -ENOMEM;
154
155 frame_size = priv->beacon_skb->len;
156
157 /* Set up TX command fields */
158 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
159 tx_beacon_cmd->tx.sta_id = priv->beacon_ctx->bcast_sta_id;
160 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
161 tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
162 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
163
164 /* Set up TX beacon command fields */
165 iwl_set_beacon_tim(priv, tx_beacon_cmd, priv->beacon_skb->data,
166 frame_size);
167
168 /* Set up packet rate and flags */
169 info = IEEE80211_SKB_CB(priv->beacon_skb);
170
171 /*
172 * Let's set up the rate at least somewhat correctly;
173 * it will currently not actually be used by the uCode,
174 * it uses the broadcast station's rate instead.
175 */
176 if (info->control.rates[0].idx < 0 ||
177 info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
178 rate = 0;
179 else
180 rate = info->control.rates[0].idx;
181
182 priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
183 priv->hw_params.valid_tx_ant);
184 rate_flags = iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
185
186 /* In mac80211, rates for 5 GHz start at 0 */
187 if (info->band == IEEE80211_BAND_5GHZ)
188 rate += IWL_FIRST_OFDM_RATE;
189 else if (rate >= IWL_FIRST_CCK_RATE && rate <= IWL_LAST_CCK_RATE)
190 rate_flags |= RATE_MCS_CCK_MSK;
191
192 tx_beacon_cmd->tx.rate_n_flags =
193 iwl_hw_set_rate_n_flags(rate, rate_flags);
194
195 /* Submit command */
196 cmd.len[0] = sizeof(*tx_beacon_cmd);
197 cmd.data[0] = tx_beacon_cmd;
198 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
199 cmd.len[1] = frame_size;
200 cmd.data[1] = priv->beacon_skb->data;
201 cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
202
203 return iwl_dvm_send_cmd(priv, &cmd);
204}
205
206static void iwl_bg_beacon_update(struct work_struct *work)
207{
208 struct iwl_priv *priv =
209 container_of(work, struct iwl_priv, beacon_update);
210 struct sk_buff *beacon;
211
212 mutex_lock(&priv->mutex);
213 if (!priv->beacon_ctx) {
214 IWL_ERR(priv, "updating beacon w/o beacon context!\n");
215 goto out;
216 }
217
218 if (priv->beacon_ctx->vif->type != NL80211_IFTYPE_AP) {
219 /*
220 * The ucode will send beacon notifications even in
221 * IBSS mode, but we don't want to process them. But
222 * we need to defer the type check to here due to
223 * requiring locking around the beacon_ctx access.
224 */
225 goto out;
226 }
227
228 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
229 beacon = ieee80211_beacon_get(priv->hw, priv->beacon_ctx->vif);
230 if (!beacon) {
231 IWL_ERR(priv, "update beacon failed -- keeping old\n");
232 goto out;
233 }
234
235 /* new beacon skb is allocated every time; dispose previous.*/
236 dev_kfree_skb(priv->beacon_skb);
237
238 priv->beacon_skb = beacon;
239
240 iwlagn_send_beacon_cmd(priv);
241 out:
242 mutex_unlock(&priv->mutex);
243}
244
245static void iwl_bg_bt_runtime_config(struct work_struct *work)
246{
247 struct iwl_priv *priv =
248 container_of(work, struct iwl_priv, bt_runtime_config);
249
250 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
251 return;
252
253 /* dont send host command if rf-kill is on */
254 if (!iwl_is_ready_rf(priv))
255 return;
256 iwlagn_send_advance_bt_config(priv);
257}
258
259static void iwl_bg_bt_full_concurrency(struct work_struct *work)
260{
261 struct iwl_priv *priv =
262 container_of(work, struct iwl_priv, bt_full_concurrency);
263 struct iwl_rxon_context *ctx;
264
265 mutex_lock(&priv->mutex);
266
267 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
268 goto out;
269
270 /* dont send host command if rf-kill is on */
271 if (!iwl_is_ready_rf(priv))
272 goto out;
273
274 IWL_DEBUG_INFO(priv, "BT coex in %s mode\n",
275 priv->bt_full_concurrent ?
276 "full concurrency" : "3-wire");
277
278 /*
279 * LQ & RXON updated cmds must be sent before BT Config cmd
280 * to avoid 3-wire collisions
281 */
282 for_each_context(priv, ctx) {
283 iwlagn_set_rxon_chain(priv, ctx);
284 iwlagn_commit_rxon(priv, ctx);
285 }
286
287 iwlagn_send_advance_bt_config(priv);
288out:
289 mutex_unlock(&priv->mutex);
290}
291
292int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
293{
294 struct iwl_statistics_cmd statistics_cmd = {
295 .configuration_flags =
296 clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
297 };
298
299 if (flags & CMD_ASYNC)
300 return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
301 CMD_ASYNC,
302 sizeof(struct iwl_statistics_cmd),
303 &statistics_cmd);
304 else
305 return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
306 CMD_SYNC,
307 sizeof(struct iwl_statistics_cmd),
308 &statistics_cmd);
309}
310
311/**
312 * iwl_bg_statistics_periodic - Timer callback to queue statistics
313 *
314 * This callback is provided in order to send a statistics request.
315 *
316 * This timer function is continually reset to execute within
317 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
318 * was received. We need to ensure we receive the statistics in order
319 * to update the temperature used for calibrating the TXPOWER.
320 */
321static void iwl_bg_statistics_periodic(unsigned long data)
322{
323 struct iwl_priv *priv = (struct iwl_priv *)data;
324
325 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
326 return;
327
328 /* dont send host command if rf-kill is on */
329 if (!iwl_is_ready_rf(priv))
330 return;
331
332 iwl_send_statistics_request(priv, CMD_ASYNC, false);
333}
334
335
336static void iwl_print_cont_event_trace(struct iwl_priv *priv, u32 base,
337 u32 start_idx, u32 num_events,
338 u32 capacity, u32 mode)
339{
340 u32 i;
341 u32 ptr; /* SRAM byte address of log data */
342 u32 ev, time, data; /* event log data */
343 unsigned long reg_flags;
344
345 if (mode == 0)
346 ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
347 else
348 ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));
349
350 /* Make sure device is powered up for SRAM reads */
351 spin_lock_irqsave(&priv->trans->reg_lock, reg_flags);
352 if (unlikely(!iwl_grab_nic_access(priv->trans))) {
353 spin_unlock_irqrestore(&priv->trans->reg_lock, reg_flags);
354 return;
355 }
356
357 /* Set starting address; reads will auto-increment */
358 iwl_write32(priv->trans, HBUS_TARG_MEM_RADDR, ptr);
359
360 /*
361 * Refuse to read more than would have fit into the log from
362 * the current start_idx. This used to happen due to the race
363 * described below, but now WARN because the code below should
364 * prevent it from happening here.
365 */
366 if (WARN_ON(num_events > capacity - start_idx))
367 num_events = capacity - start_idx;
368
369 /*
370 * "time" is actually "data" for mode 0 (no timestamp).
371 * place event id # at far right for easier visual parsing.
372 */
373 for (i = 0; i < num_events; i++) {
374 ev = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
375 time = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
376 if (mode == 0) {
377 trace_iwlwifi_dev_ucode_cont_event(
378 priv->trans->dev, 0, time, ev);
379 } else {
380 data = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
381 trace_iwlwifi_dev_ucode_cont_event(
382 priv->trans->dev, time, data, ev);
383 }
384 }
385 /* Allow device to power down */
386 iwl_release_nic_access(priv->trans);
387 spin_unlock_irqrestore(&priv->trans->reg_lock, reg_flags);
388}
389
390static void iwl_continuous_event_trace(struct iwl_priv *priv)
391{
392 u32 capacity; /* event log capacity in # entries */
393 struct {
394 u32 capacity;
395 u32 mode;
396 u32 wrap_counter;
397 u32 write_counter;
398 } __packed read;
399 u32 base; /* SRAM byte address of event log header */
400 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
401 u32 num_wraps; /* # times uCode wrapped to top of log */
402 u32 next_entry; /* index of next entry to be written by uCode */
403
404 base = priv->device_pointers.log_event_table;
405 if (iwlagn_hw_valid_rtc_data_addr(base)) {
406 iwl_read_targ_mem_words(priv->trans, base, &read, sizeof(read));
407 capacity = read.capacity;
408 mode = read.mode;
409 num_wraps = read.wrap_counter;
410 next_entry = read.write_counter;
411 } else
412 return;
413
414 /*
415 * Unfortunately, the uCode doesn't use temporary variables.
416 * Therefore, it can happen that we read next_entry == capacity,
417 * which really means next_entry == 0.
418 */
419 if (unlikely(next_entry == capacity))
420 next_entry = 0;
421 /*
422 * Additionally, the uCode increases the write pointer before
423 * the wraps counter, so if the write pointer is smaller than
424 * the old write pointer (wrap occurred) but we read that no
425 * wrap occurred, we actually read between the next_entry and
426 * num_wraps update (this does happen in practice!!) -- take
427 * that into account by increasing num_wraps.
428 */
429 if (unlikely(next_entry < priv->event_log.next_entry &&
430 num_wraps == priv->event_log.num_wraps))
431 num_wraps++;
432
433 if (num_wraps == priv->event_log.num_wraps) {
434 iwl_print_cont_event_trace(
435 priv, base, priv->event_log.next_entry,
436 next_entry - priv->event_log.next_entry,
437 capacity, mode);
438
439 priv->event_log.non_wraps_count++;
440 } else {
441 if (num_wraps - priv->event_log.num_wraps > 1)
442 priv->event_log.wraps_more_count++;
443 else
444 priv->event_log.wraps_once_count++;
445
446 trace_iwlwifi_dev_ucode_wrap_event(priv->trans->dev,
447 num_wraps - priv->event_log.num_wraps,
448 next_entry, priv->event_log.next_entry);
449
450 if (next_entry < priv->event_log.next_entry) {
451 iwl_print_cont_event_trace(
452 priv, base, priv->event_log.next_entry,
453 capacity - priv->event_log.next_entry,
454 capacity, mode);
455
456 iwl_print_cont_event_trace(
457 priv, base, 0, next_entry, capacity, mode);
458 } else {
459 iwl_print_cont_event_trace(
460 priv, base, next_entry,
461 capacity - next_entry,
462 capacity, mode);
463
464 iwl_print_cont_event_trace(
465 priv, base, 0, next_entry, capacity, mode);
466 }
467 }
468
469 priv->event_log.num_wraps = num_wraps;
470 priv->event_log.next_entry = next_entry;
471}
472
473/**
474 * iwl_bg_ucode_trace - Timer callback to log ucode event
475 *
476 * The timer is continually set to execute every
477 * UCODE_TRACE_PERIOD milliseconds after the last timer expired
478 * this function is to perform continuous uCode event logging operation
479 * if enabled
480 */
481static void iwl_bg_ucode_trace(unsigned long data)
482{
483 struct iwl_priv *priv = (struct iwl_priv *)data;
484
485 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
486 return;
487
488 if (priv->event_log.ucode_trace) {
489 iwl_continuous_event_trace(priv);
490 /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
491 mod_timer(&priv->ucode_trace,
492 jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
493 }
494}
495
496static void iwl_bg_tx_flush(struct work_struct *work)
497{
498 struct iwl_priv *priv =
499 container_of(work, struct iwl_priv, tx_flush);
500
501 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
502 return;
503
504 /* do nothing if rf-kill is on */
505 if (!iwl_is_ready_rf(priv))
506 return;
507
508 IWL_DEBUG_INFO(priv, "device request: flush all tx frames\n");
509 iwlagn_dev_txfifo_flush(priv, IWL_DROP_ALL);
510}
511
512/*
513 * queue/FIFO/AC mapping definitions
514 */
515
516#define IWL_TX_FIFO_BK 0 /* shared */
517#define IWL_TX_FIFO_BE 1
518#define IWL_TX_FIFO_VI 2 /* shared */
519#define IWL_TX_FIFO_VO 3
520#define IWL_TX_FIFO_BK_IPAN IWL_TX_FIFO_BK
521#define IWL_TX_FIFO_BE_IPAN 4
522#define IWL_TX_FIFO_VI_IPAN IWL_TX_FIFO_VI
523#define IWL_TX_FIFO_VO_IPAN 5
524/* re-uses the VO FIFO, uCode will properly flush/schedule */
525#define IWL_TX_FIFO_AUX 5
526#define IWL_TX_FIFO_UNUSED -1
527
528#define IWLAGN_CMD_FIFO_NUM 7
529
530/*
531 * This queue number is required for proper operation
532 * because the ucode will stop/start the scheduler as
533 * required.
534 */
535#define IWL_IPAN_MCAST_QUEUE 8
536
537static const u8 iwlagn_default_queue_to_tx_fifo[] = {
538 IWL_TX_FIFO_VO,
539 IWL_TX_FIFO_VI,
540 IWL_TX_FIFO_BE,
541 IWL_TX_FIFO_BK,
542 IWLAGN_CMD_FIFO_NUM,
543};
544
545static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
546 IWL_TX_FIFO_VO,
547 IWL_TX_FIFO_VI,
548 IWL_TX_FIFO_BE,
549 IWL_TX_FIFO_BK,
550 IWL_TX_FIFO_BK_IPAN,
551 IWL_TX_FIFO_BE_IPAN,
552 IWL_TX_FIFO_VI_IPAN,
553 IWL_TX_FIFO_VO_IPAN,
554 IWL_TX_FIFO_BE_IPAN,
555 IWLAGN_CMD_FIFO_NUM,
556 IWL_TX_FIFO_AUX,
557};
558
559static const u8 iwlagn_bss_ac_to_fifo[] = {
560 IWL_TX_FIFO_VO,
561 IWL_TX_FIFO_VI,
562 IWL_TX_FIFO_BE,
563 IWL_TX_FIFO_BK,
564};
565
566static const u8 iwlagn_bss_ac_to_queue[] = {
567 0, 1, 2, 3,
568};
569
570static const u8 iwlagn_pan_ac_to_fifo[] = {
571 IWL_TX_FIFO_VO_IPAN,
572 IWL_TX_FIFO_VI_IPAN,
573 IWL_TX_FIFO_BE_IPAN,
574 IWL_TX_FIFO_BK_IPAN,
575};
576
577static const u8 iwlagn_pan_ac_to_queue[] = {
578 7, 6, 5, 4,
579};
580
581static void iwl_init_context(struct iwl_priv *priv, u32 ucode_flags)
582{
583 int i;
584
585 /*
586 * The default context is always valid,
587 * the PAN context depends on uCode.
588 */
589 priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
590 if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN)
591 priv->valid_contexts |= BIT(IWL_RXON_CTX_PAN);
592
593 for (i = 0; i < NUM_IWL_RXON_CTX; i++)
594 priv->contexts[i].ctxid = i;
595
596 priv->contexts[IWL_RXON_CTX_BSS].always_active = true;
597 priv->contexts[IWL_RXON_CTX_BSS].is_active = true;
598 priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
599 priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
600 priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
601 priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
602 priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
603 priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
604 priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id = IWLAGN_BROADCAST_ID;
605 priv->contexts[IWL_RXON_CTX_BSS].exclusive_interface_modes =
606 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_MONITOR);
607 priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
608 BIT(NL80211_IFTYPE_STATION);
609 priv->contexts[IWL_RXON_CTX_BSS].ap_devtype = RXON_DEV_TYPE_AP;
610 priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
611 priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
612 priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
613 memcpy(priv->contexts[IWL_RXON_CTX_BSS].ac_to_queue,
614 iwlagn_bss_ac_to_queue, sizeof(iwlagn_bss_ac_to_queue));
615 memcpy(priv->contexts[IWL_RXON_CTX_BSS].ac_to_fifo,
616 iwlagn_bss_ac_to_fifo, sizeof(iwlagn_bss_ac_to_fifo));
617
618 priv->contexts[IWL_RXON_CTX_PAN].rxon_cmd = REPLY_WIPAN_RXON;
619 priv->contexts[IWL_RXON_CTX_PAN].rxon_timing_cmd =
620 REPLY_WIPAN_RXON_TIMING;
621 priv->contexts[IWL_RXON_CTX_PAN].rxon_assoc_cmd =
622 REPLY_WIPAN_RXON_ASSOC;
623 priv->contexts[IWL_RXON_CTX_PAN].qos_cmd = REPLY_WIPAN_QOS_PARAM;
624 priv->contexts[IWL_RXON_CTX_PAN].ap_sta_id = IWL_AP_ID_PAN;
625 priv->contexts[IWL_RXON_CTX_PAN].wep_key_cmd = REPLY_WIPAN_WEPKEY;
626 priv->contexts[IWL_RXON_CTX_PAN].bcast_sta_id = IWLAGN_PAN_BCAST_ID;
627 priv->contexts[IWL_RXON_CTX_PAN].station_flags = STA_FLG_PAN_STATION;
628 priv->contexts[IWL_RXON_CTX_PAN].interface_modes =
629 BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP);
630
631 if (ucode_flags & IWL_UCODE_TLV_FLAGS_P2P)
632 priv->contexts[IWL_RXON_CTX_PAN].interface_modes |=
633 BIT(NL80211_IFTYPE_P2P_CLIENT) |
634 BIT(NL80211_IFTYPE_P2P_GO);
635
636 priv->contexts[IWL_RXON_CTX_PAN].ap_devtype = RXON_DEV_TYPE_CP;
637 priv->contexts[IWL_RXON_CTX_PAN].station_devtype = RXON_DEV_TYPE_2STA;
638 priv->contexts[IWL_RXON_CTX_PAN].unused_devtype = RXON_DEV_TYPE_P2P;
639 memcpy(priv->contexts[IWL_RXON_CTX_PAN].ac_to_queue,
640 iwlagn_pan_ac_to_queue, sizeof(iwlagn_pan_ac_to_queue));
641 memcpy(priv->contexts[IWL_RXON_CTX_PAN].ac_to_fifo,
642 iwlagn_pan_ac_to_fifo, sizeof(iwlagn_pan_ac_to_fifo));
643 priv->contexts[IWL_RXON_CTX_PAN].mcast_queue = IWL_IPAN_MCAST_QUEUE;
644
645 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
646}
647
648static void iwl_rf_kill_ct_config(struct iwl_priv *priv)
649{
650 struct iwl_ct_kill_config cmd;
651 struct iwl_ct_kill_throttling_config adv_cmd;
652 int ret = 0;
653
654 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
655 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
656
657 priv->thermal_throttle.ct_kill_toggle = false;
658
659 if (priv->cfg->base_params->support_ct_kill_exit) {
660 adv_cmd.critical_temperature_enter =
661 cpu_to_le32(priv->hw_params.ct_kill_threshold);
662 adv_cmd.critical_temperature_exit =
663 cpu_to_le32(priv->hw_params.ct_kill_exit_threshold);
664
665 ret = iwl_dvm_send_cmd_pdu(priv,
666 REPLY_CT_KILL_CONFIG_CMD,
667 CMD_SYNC, sizeof(adv_cmd), &adv_cmd);
668 if (ret)
669 IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
670 else
671 IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
672 "succeeded, critical temperature enter is %d,"
673 "exit is %d\n",
674 priv->hw_params.ct_kill_threshold,
675 priv->hw_params.ct_kill_exit_threshold);
676 } else {
677 cmd.critical_temperature_R =
678 cpu_to_le32(priv->hw_params.ct_kill_threshold);
679
680 ret = iwl_dvm_send_cmd_pdu(priv,
681 REPLY_CT_KILL_CONFIG_CMD,
682 CMD_SYNC, sizeof(cmd), &cmd);
683 if (ret)
684 IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
685 else
686 IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
687 "succeeded, "
688 "critical temperature is %d\n",
689 priv->hw_params.ct_kill_threshold);
690 }
691}
692
693static int iwlagn_send_calib_cfg_rt(struct iwl_priv *priv, u32 cfg)
694{
695 struct iwl_calib_cfg_cmd calib_cfg_cmd;
696 struct iwl_host_cmd cmd = {
697 .id = CALIBRATION_CFG_CMD,
698 .len = { sizeof(struct iwl_calib_cfg_cmd), },
699 .data = { &calib_cfg_cmd, },
700 };
701
702 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
703 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_RT_CFG_ALL;
704 calib_cfg_cmd.ucd_calib_cfg.once.start = cpu_to_le32(cfg);
705
706 return iwl_dvm_send_cmd(priv, &cmd);
707}
708
709
710static int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
711{
712 struct iwl_tx_ant_config_cmd tx_ant_cmd = {
713 .valid = cpu_to_le32(valid_tx_ant),
714 };
715
716 if (IWL_UCODE_API(priv->fw->ucode_ver) > 1) {
717 IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
718 return iwl_dvm_send_cmd_pdu(priv,
719 TX_ANT_CONFIGURATION_CMD,
720 CMD_SYNC,
721 sizeof(struct iwl_tx_ant_config_cmd),
722 &tx_ant_cmd);
723 } else {
724 IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
725 return -EOPNOTSUPP;
726 }
727}
728
729static void iwl_send_bt_config(struct iwl_priv *priv)
730{
731 struct iwl_bt_cmd bt_cmd = {
732 .lead_time = BT_LEAD_TIME_DEF,
733 .max_kill = BT_MAX_KILL_DEF,
734 .kill_ack_mask = 0,
735 .kill_cts_mask = 0,
736 };
737
738 if (!iwlwifi_mod_params.bt_coex_active)
739 bt_cmd.flags = BT_COEX_DISABLE;
740 else
741 bt_cmd.flags = BT_COEX_ENABLE;
742
743 priv->bt_enable_flag = bt_cmd.flags;
744 IWL_DEBUG_INFO(priv, "BT coex %s\n",
745 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
746
747 if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
748 CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
749 IWL_ERR(priv, "failed to send BT Coex Config\n");
750}
751
752/**
753 * iwl_alive_start - called after REPLY_ALIVE notification received
754 * from protocol/runtime uCode (initialization uCode's
755 * Alive gets handled by iwl_init_alive_start()).
756 */
757int iwl_alive_start(struct iwl_priv *priv)
758{
759 int ret = 0;
760 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
761
762 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
763
764 /* After the ALIVE response, we can send host commands to the uCode */
765 set_bit(STATUS_ALIVE, &priv->status);
766
767 if (iwl_is_rfkill(priv))
768 return -ERFKILL;
769
770 if (priv->event_log.ucode_trace) {
771 /* start collecting data now */
772 mod_timer(&priv->ucode_trace, jiffies);
773 }
774
775 /* download priority table before any calibration request */
776 if (priv->cfg->bt_params &&
777 priv->cfg->bt_params->advanced_bt_coexist) {
778 /* Configure Bluetooth device coexistence support */
779 if (priv->cfg->bt_params->bt_sco_disable)
780 priv->bt_enable_pspoll = false;
781 else
782 priv->bt_enable_pspoll = true;
783
784 priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
785 priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
786 priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
787 iwlagn_send_advance_bt_config(priv);
788 priv->bt_valid = IWLAGN_BT_VALID_ENABLE_FLAGS;
789 priv->cur_rssi_ctx = NULL;
790
791 iwl_send_prio_tbl(priv);
792
793 /* FIXME: w/a to force change uCode BT state machine */
794 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
795 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
796 if (ret)
797 return ret;
798 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_CLOSE,
799 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
800 if (ret)
801 return ret;
802 } else {
803 /*
804 * default is 2-wire BT coexexistence support
805 */
806 iwl_send_bt_config(priv);
807 }
808
809 /*
810 * Perform runtime calibrations, including DC calibration.
811 */
812 iwlagn_send_calib_cfg_rt(priv, IWL_CALIB_CFG_DC_IDX);
813
814 ieee80211_wake_queues(priv->hw);
815
816 /* Configure Tx antenna selection based on H/W config */
817 iwlagn_send_tx_ant_config(priv, priv->hw_params.valid_tx_ant);
818
819 if (iwl_is_associated_ctx(ctx) && !priv->wowlan) {
820 struct iwl_rxon_cmd *active_rxon =
821 (struct iwl_rxon_cmd *)&ctx->active;
822 /* apply any changes in staging */
823 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
824 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
825 } else {
826 struct iwl_rxon_context *tmp;
827 /* Initialize our rx_config data */
828 for_each_context(priv, tmp)
829 iwl_connection_init_rx_config(priv, tmp);
830
831 iwlagn_set_rxon_chain(priv, ctx);
832 }
833
834 if (!priv->wowlan) {
835 /* WoWLAN ucode will not reply in the same way, skip it */
836 iwl_reset_run_time_calib(priv);
837 }
838
839 set_bit(STATUS_READY, &priv->status);
840
841 /* Configure the adapter for unassociated operation */
842 ret = iwlagn_commit_rxon(priv, ctx);
843 if (ret)
844 return ret;
845
846 /* At this point, the NIC is initialized and operational */
847 iwl_rf_kill_ct_config(priv);
848
849 IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
850
851 return iwl_power_update_mode(priv, true);
852}
853
854/**
855 * iwl_clear_driver_stations - clear knowledge of all stations from driver
856 * @priv: iwl priv struct
857 *
858 * This is called during iwl_down() to make sure that in the case
859 * we're coming there from a hardware restart mac80211 will be
860 * able to reconfigure stations -- if we're getting there in the
861 * normal down flow then the stations will already be cleared.
862 */
863static void iwl_clear_driver_stations(struct iwl_priv *priv)
864{
865 struct iwl_rxon_context *ctx;
866
867 spin_lock_bh(&priv->sta_lock);
868 memset(priv->stations, 0, sizeof(priv->stations));
869 priv->num_stations = 0;
870
871 priv->ucode_key_table = 0;
872
873 for_each_context(priv, ctx) {
874 /*
875 * Remove all key information that is not stored as part
876 * of station information since mac80211 may not have had
877 * a chance to remove all the keys. When device is
878 * reconfigured by mac80211 after an error all keys will
879 * be reconfigured.
880 */
881 memset(ctx->wep_keys, 0, sizeof(ctx->wep_keys));
882 ctx->key_mapping_keys = 0;
883 }
884
885 spin_unlock_bh(&priv->sta_lock);
886}
887
888void iwl_down(struct iwl_priv *priv)
889{
890 int exit_pending;
891
892 IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
893
894 lockdep_assert_held(&priv->mutex);
895
896 iwl_scan_cancel_timeout(priv, 200);
897
898 /*
899 * If active, scanning won't cancel it, so say it expired.
900 * No race since we hold the mutex here and a new one
901 * can't come in at this time.
902 */
903 ieee80211_remain_on_channel_expired(priv->hw);
904
905 exit_pending =
906 test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
907
908 iwl_clear_ucode_stations(priv, NULL);
909 iwl_dealloc_bcast_stations(priv);
910 iwl_clear_driver_stations(priv);
911
912 /* reset BT coex data */
913 priv->bt_status = 0;
914 priv->cur_rssi_ctx = NULL;
915 priv->bt_is_sco = 0;
916 if (priv->cfg->bt_params)
917 priv->bt_traffic_load =
918 priv->cfg->bt_params->bt_init_traffic_load;
919 else
920 priv->bt_traffic_load = 0;
921 priv->bt_full_concurrent = false;
922 priv->bt_ci_compliance = 0;
923
924 /* Wipe out the EXIT_PENDING status bit if we are not actually
925 * exiting the module */
926 if (!exit_pending)
927 clear_bit(STATUS_EXIT_PENDING, &priv->status);
928
929 if (priv->mac80211_registered)
930 ieee80211_stop_queues(priv->hw);
931
932 priv->ucode_loaded = false;
933 iwl_trans_stop_device(priv->trans);
934
935 /* Set num_aux_in_flight must be done after the transport is stopped */
936 atomic_set(&priv->num_aux_in_flight, 0);
937
938 /* Clear out all status bits but a few that are stable across reset */
939 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
940 STATUS_RF_KILL_HW |
941 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
942 STATUS_GEO_CONFIGURED |
943 test_bit(STATUS_FW_ERROR, &priv->status) <<
944 STATUS_FW_ERROR |
945 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
946 STATUS_EXIT_PENDING;
947
948 dev_kfree_skb(priv->beacon_skb);
949 priv->beacon_skb = NULL;
950}
951
952/*****************************************************************************
953 *
954 * Workqueue callbacks
955 *
956 *****************************************************************************/
957
958static void iwl_bg_run_time_calib_work(struct work_struct *work)
959{
960 struct iwl_priv *priv = container_of(work, struct iwl_priv,
961 run_time_calib_work);
962
963 mutex_lock(&priv->mutex);
964
965 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
966 test_bit(STATUS_SCANNING, &priv->status)) {
967 mutex_unlock(&priv->mutex);
968 return;
969 }
970
971 if (priv->start_calib) {
972 iwl_chain_noise_calibration(priv);
973 iwl_sensitivity_calibration(priv);
974 }
975
976 mutex_unlock(&priv->mutex);
977}
978
979void iwlagn_prepare_restart(struct iwl_priv *priv)
980{
981 bool bt_full_concurrent;
982 u8 bt_ci_compliance;
983 u8 bt_load;
984 u8 bt_status;
985 bool bt_is_sco;
986 int i;
987
988 lockdep_assert_held(&priv->mutex);
989
990 priv->is_open = 0;
991
992 /*
993 * __iwl_down() will clear the BT status variables,
994 * which is correct, but when we restart we really
995 * want to keep them so restore them afterwards.
996 *
997 * The restart process will later pick them up and
998 * re-configure the hw when we reconfigure the BT
999 * command.
1000 */
1001 bt_full_concurrent = priv->bt_full_concurrent;
1002 bt_ci_compliance = priv->bt_ci_compliance;
1003 bt_load = priv->bt_traffic_load;
1004 bt_status = priv->bt_status;
1005 bt_is_sco = priv->bt_is_sco;
1006
1007 iwl_down(priv);
1008
1009 priv->bt_full_concurrent = bt_full_concurrent;
1010 priv->bt_ci_compliance = bt_ci_compliance;
1011 priv->bt_traffic_load = bt_load;
1012 priv->bt_status = bt_status;
1013 priv->bt_is_sco = bt_is_sco;
1014
1015 /* reset aggregation queues */
1016 for (i = IWLAGN_FIRST_AMPDU_QUEUE; i < IWL_MAX_HW_QUEUES; i++)
1017 priv->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
1018 /* and stop counts */
1019 for (i = 0; i < IWL_MAX_HW_QUEUES; i++)
1020 atomic_set(&priv->queue_stop_count[i], 0);
1021
1022 memset(priv->agg_q_alloc, 0, sizeof(priv->agg_q_alloc));
1023}
1024
1025static void iwl_bg_restart(struct work_struct *data)
1026{
1027 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
1028
1029 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1030 return;
1031
1032 if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
1033 mutex_lock(&priv->mutex);
1034 iwlagn_prepare_restart(priv);
1035 mutex_unlock(&priv->mutex);
1036 iwl_cancel_deferred_work(priv);
1037 ieee80211_restart_hw(priv->hw);
1038 } else {
1039 WARN_ON(1);
1040 }
1041}
1042
1043
1044
1045
1046void iwlagn_disable_roc(struct iwl_priv *priv)
1047{
1048 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
1049
1050 lockdep_assert_held(&priv->mutex);
1051
1052 if (!priv->hw_roc_setup)
1053 return;
1054
1055 ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
1056 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1057
1058 priv->hw_roc_channel = NULL;
1059
1060 memset(ctx->staging.node_addr, 0, ETH_ALEN);
1061
1062 iwlagn_commit_rxon(priv, ctx);
1063
1064 ctx->is_active = false;
1065 priv->hw_roc_setup = false;
1066}
1067
1068static void iwlagn_disable_roc_work(struct work_struct *work)
1069{
1070 struct iwl_priv *priv = container_of(work, struct iwl_priv,
1071 hw_roc_disable_work.work);
1072
1073 mutex_lock(&priv->mutex);
1074 iwlagn_disable_roc(priv);
1075 mutex_unlock(&priv->mutex);
1076}
1077
1078/*****************************************************************************
1079 *
1080 * driver setup and teardown
1081 *
1082 *****************************************************************************/
1083
1084static void iwl_setup_deferred_work(struct iwl_priv *priv)
1085{
1086 priv->workqueue = create_singlethread_workqueue(DRV_NAME);
1087
1088 INIT_WORK(&priv->restart, iwl_bg_restart);
1089 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
1090 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
1091 INIT_WORK(&priv->tx_flush, iwl_bg_tx_flush);
1092 INIT_WORK(&priv->bt_full_concurrency, iwl_bg_bt_full_concurrency);
1093 INIT_WORK(&priv->bt_runtime_config, iwl_bg_bt_runtime_config);
1094 INIT_DELAYED_WORK(&priv->hw_roc_disable_work,
1095 iwlagn_disable_roc_work);
1096
1097 iwl_setup_scan_deferred_work(priv);
1098
1099 if (priv->cfg->bt_params)
1100 iwlagn_bt_setup_deferred_work(priv);
1101
1102 init_timer(&priv->statistics_periodic);
1103 priv->statistics_periodic.data = (unsigned long)priv;
1104 priv->statistics_periodic.function = iwl_bg_statistics_periodic;
1105
1106 init_timer(&priv->ucode_trace);
1107 priv->ucode_trace.data = (unsigned long)priv;
1108 priv->ucode_trace.function = iwl_bg_ucode_trace;
1109}
1110
1111void iwl_cancel_deferred_work(struct iwl_priv *priv)
1112{
1113 if (priv->cfg->bt_params)
1114 iwlagn_bt_cancel_deferred_work(priv);
1115
1116 cancel_work_sync(&priv->run_time_calib_work);
1117 cancel_work_sync(&priv->beacon_update);
1118
1119 iwl_cancel_scan_deferred_work(priv);
1120
1121 cancel_work_sync(&priv->bt_full_concurrency);
1122 cancel_work_sync(&priv->bt_runtime_config);
1123 cancel_delayed_work_sync(&priv->hw_roc_disable_work);
1124
1125 del_timer_sync(&priv->statistics_periodic);
1126 del_timer_sync(&priv->ucode_trace);
1127}
1128
1129static void iwl_init_hw_rates(struct ieee80211_rate *rates)
1130{
1131 int i;
1132
1133 for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
1134 rates[i].bitrate = iwl_rates[i].ieee * 5;
1135 rates[i].hw_value = i; /* Rate scaling will work on indexes */
1136 rates[i].hw_value_short = i;
1137 rates[i].flags = 0;
1138 if ((i >= IWL_FIRST_CCK_RATE) && (i <= IWL_LAST_CCK_RATE)) {
1139 /*
1140 * If CCK != 1M then set short preamble rate flag.
1141 */
1142 rates[i].flags |=
1143 (iwl_rates[i].plcp == IWL_RATE_1M_PLCP) ?
1144 0 : IEEE80211_RATE_SHORT_PREAMBLE;
1145 }
1146 }
1147}
1148
1149#define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
1150#define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
1151static void iwl_init_ht_hw_capab(const struct iwl_priv *priv,
1152 struct ieee80211_sta_ht_cap *ht_info,
1153 enum ieee80211_band band)
1154{
1155 u16 max_bit_rate = 0;
1156 u8 rx_chains_num = priv->hw_params.rx_chains_num;
1157 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1158
1159 ht_info->cap = 0;
1160 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
1161
1162 ht_info->ht_supported = true;
1163
1164 if (priv->cfg->ht_params &&
1165 priv->cfg->ht_params->ht_greenfield_support)
1166 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
1167 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
1168 max_bit_rate = MAX_BIT_RATE_20_MHZ;
1169 if (priv->hw_params.ht40_channel & BIT(band)) {
1170 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1171 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
1172 ht_info->mcs.rx_mask[4] = 0x01;
1173 max_bit_rate = MAX_BIT_RATE_40_MHZ;
1174 }
1175
1176 if (iwlwifi_mod_params.amsdu_size_8K)
1177 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
1178
1179 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
1180 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
1181
1182 ht_info->mcs.rx_mask[0] = 0xFF;
1183 if (rx_chains_num >= 2)
1184 ht_info->mcs.rx_mask[1] = 0xFF;
1185 if (rx_chains_num >= 3)
1186 ht_info->mcs.rx_mask[2] = 0xFF;
1187
1188 /* Highest supported Rx data rate */
1189 max_bit_rate *= rx_chains_num;
1190 WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
1191 ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
1192
1193 /* Tx MCS capabilities */
1194 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
1195 if (tx_chains_num != rx_chains_num) {
1196 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1197 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
1198 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
1199 }
1200}
1201
1202/**
1203 * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
1204 */
1205static int iwl_init_geos(struct iwl_priv *priv)
1206{
1207 struct iwl_channel_info *ch;
1208 struct ieee80211_supported_band *sband;
1209 struct ieee80211_channel *channels;
1210 struct ieee80211_channel *geo_ch;
1211 struct ieee80211_rate *rates;
1212 int i = 0;
1213 s8 max_tx_power = IWLAGN_TX_POWER_TARGET_POWER_MIN;
1214
1215 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
1216 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
1217 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
1218 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
1219 return 0;
1220 }
1221
1222 channels = kcalloc(priv->channel_count,
1223 sizeof(struct ieee80211_channel), GFP_KERNEL);
1224 if (!channels)
1225 return -ENOMEM;
1226
1227 rates = kcalloc(IWL_RATE_COUNT_LEGACY, sizeof(struct ieee80211_rate),
1228 GFP_KERNEL);
1229 if (!rates) {
1230 kfree(channels);
1231 return -ENOMEM;
1232 }
1233
1234 /* 5.2GHz channels start after the 2.4GHz channels */
1235 sband = &priv->bands[IEEE80211_BAND_5GHZ];
1236 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
1237 /* just OFDM */
1238 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
1239 sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
1240
1241 if (priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE)
1242 iwl_init_ht_hw_capab(priv, &sband->ht_cap,
1243 IEEE80211_BAND_5GHZ);
1244
1245 sband = &priv->bands[IEEE80211_BAND_2GHZ];
1246 sband->channels = channels;
1247 /* OFDM & CCK */
1248 sband->bitrates = rates;
1249 sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
1250
1251 if (priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE)
1252 iwl_init_ht_hw_capab(priv, &sband->ht_cap,
1253 IEEE80211_BAND_2GHZ);
1254
1255 priv->ieee_channels = channels;
1256 priv->ieee_rates = rates;
1257
1258 for (i = 0; i < priv->channel_count; i++) {
1259 ch = &priv->channel_info[i];
1260
1261 /* FIXME: might be removed if scan is OK */
1262 if (!is_channel_valid(ch))
1263 continue;
1264
1265 sband = &priv->bands[ch->band];
1266
1267 geo_ch = &sband->channels[sband->n_channels++];
1268
1269 geo_ch->center_freq =
1270 ieee80211_channel_to_frequency(ch->channel, ch->band);
1271 geo_ch->max_power = ch->max_power_avg;
1272 geo_ch->max_antenna_gain = 0xff;
1273 geo_ch->hw_value = ch->channel;
1274
1275 if (is_channel_valid(ch)) {
1276 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
1277 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
1278
1279 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
1280 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1281
1282 if (ch->flags & EEPROM_CHANNEL_RADAR)
1283 geo_ch->flags |= IEEE80211_CHAN_RADAR;
1284
1285 geo_ch->flags |= ch->ht40_extension_channel;
1286
1287 if (ch->max_power_avg > max_tx_power)
1288 max_tx_power = ch->max_power_avg;
1289 } else {
1290 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
1291 }
1292
1293 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
1294 ch->channel, geo_ch->center_freq,
1295 is_channel_a_band(ch) ? "5.2" : "2.4",
1296 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
1297 "restricted" : "valid",
1298 geo_ch->flags);
1299 }
1300
1301 priv->tx_power_device_lmt = max_tx_power;
1302 priv->tx_power_user_lmt = max_tx_power;
1303 priv->tx_power_next = max_tx_power;
1304
1305 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
1306 priv->hw_params.sku & EEPROM_SKU_CAP_BAND_52GHZ) {
1307 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
1308 "Please send your %s to maintainer.\n",
1309 priv->trans->hw_id_str);
1310 priv->hw_params.sku &= ~EEPROM_SKU_CAP_BAND_52GHZ;
1311 }
1312
1313 if (iwlwifi_mod_params.disable_5ghz)
1314 priv->bands[IEEE80211_BAND_5GHZ].n_channels = 0;
1315
1316 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
1317 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
1318 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
1319
1320 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
1321
1322 return 0;
1323}
1324
1325/*
1326 * iwl_free_geos - undo allocations in iwl_init_geos
1327 */
1328static void iwl_free_geos(struct iwl_priv *priv)
1329{
1330 kfree(priv->ieee_channels);
1331 kfree(priv->ieee_rates);
1332 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
1333}
1334
1335static int iwl_init_drv(struct iwl_priv *priv)
1336{
1337 int ret;
1338
1339 spin_lock_init(&priv->sta_lock);
1340
1341 mutex_init(&priv->mutex);
1342
1343 INIT_LIST_HEAD(&priv->calib_results);
1344
1345 priv->ieee_channels = NULL;
1346 priv->ieee_rates = NULL;
1347 priv->band = IEEE80211_BAND_2GHZ;
1348
1349 priv->plcp_delta_threshold =
1350 priv->cfg->base_params->plcp_delta_threshold;
1351
1352 priv->iw_mode = NL80211_IFTYPE_STATION;
1353 priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
1354 priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
1355 priv->agg_tids_count = 0;
1356
1357 priv->ucode_owner = IWL_OWNERSHIP_DRIVER;
1358
1359 priv->rx_statistics_jiffies = jiffies;
1360
1361 /* Choose which receivers/antennas to use */
1362 iwlagn_set_rxon_chain(priv, &priv->contexts[IWL_RXON_CTX_BSS]);
1363
1364 iwl_init_scan_params(priv);
1365
1366 /* init bt coex */
1367 if (priv->cfg->bt_params &&
1368 priv->cfg->bt_params->advanced_bt_coexist) {
1369 priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
1370 priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
1371 priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
1372 priv->bt_on_thresh = BT_ON_THRESHOLD_DEF;
1373 priv->bt_duration = BT_DURATION_LIMIT_DEF;
1374 priv->dynamic_frag_thresh = BT_FRAG_THRESHOLD_DEF;
1375 }
1376
1377 ret = iwl_init_channel_map(priv);
1378 if (ret) {
1379 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
1380 goto err;
1381 }
1382
1383 ret = iwl_init_geos(priv);
1384 if (ret) {
1385 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
1386 goto err_free_channel_map;
1387 }
1388 iwl_init_hw_rates(priv->ieee_rates);
1389
1390 return 0;
1391
1392err_free_channel_map:
1393 iwl_free_channel_map(priv);
1394err:
1395 return ret;
1396}
1397
1398static void iwl_uninit_drv(struct iwl_priv *priv)
1399{
1400 iwl_free_geos(priv);
1401 iwl_free_channel_map(priv);
1402 kfree(priv->scan_cmd);
1403 kfree(priv->beacon_cmd);
1404 kfree(rcu_dereference_raw(priv->noa_data));
1405 iwl_calib_free_results(priv);
1406#ifdef CONFIG_IWLWIFI_DEBUGFS
1407 kfree(priv->wowlan_sram);
1408#endif
1409}
1410
1411static void iwl_set_hw_params(struct iwl_priv *priv)
1412{
1413 if (priv->cfg->ht_params)
1414 priv->hw_params.use_rts_for_aggregation =
1415 priv->cfg->ht_params->use_rts_for_aggregation;
1416
1417 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_ALL)
1418 priv->hw_params.sku &= ~EEPROM_SKU_CAP_11N_ENABLE;
1419
1420 /* Device-specific setup */
1421 priv->lib->set_hw_params(priv);
1422}
1423
1424
1425
1426/* show what optional capabilities we have */
1427static void iwl_option_config(struct iwl_priv *priv)
1428{
1429#ifdef CONFIG_IWLWIFI_DEBUG
1430 IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUG enabled\n");
1431#else
1432 IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUG disabled\n");
1433#endif
1434
1435#ifdef CONFIG_IWLWIFI_DEBUGFS
1436 IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUGFS enabled\n");
1437#else
1438 IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUGFS disabled\n");
1439#endif
1440
1441#ifdef CONFIG_IWLWIFI_DEVICE_TRACING
1442 IWL_INFO(priv, "CONFIG_IWLWIFI_DEVICE_TRACING enabled\n");
1443#else
1444 IWL_INFO(priv, "CONFIG_IWLWIFI_DEVICE_TRACING disabled\n");
1445#endif
1446
1447#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
1448 IWL_INFO(priv, "CONFIG_IWLWIFI_DEVICE_TESTMODE enabled\n");
1449#else
1450 IWL_INFO(priv, "CONFIG_IWLWIFI_DEVICE_TESTMODE disabled\n");
1451#endif
1452
1453#ifdef CONFIG_IWLWIFI_P2P
1454 IWL_INFO(priv, "CONFIG_IWLWIFI_P2P enabled\n");
1455#else
1456 IWL_INFO(priv, "CONFIG_IWLWIFI_P2P disabled\n");
1457#endif
1458}
1459
1460static struct iwl_op_mode *iwl_op_mode_dvm_start(struct iwl_trans *trans,
1461 const struct iwl_cfg *cfg,
1462 const struct iwl_fw *fw)
1463{
1464 struct iwl_priv *priv;
1465 struct ieee80211_hw *hw;
1466 struct iwl_op_mode *op_mode;
1467 u16 num_mac;
1468 u32 ucode_flags;
1469 struct iwl_trans_config trans_cfg;
1470 static const u8 no_reclaim_cmds[] = {
1471 REPLY_RX_PHY_CMD,
1472 REPLY_RX,
1473 REPLY_RX_MPDU_CMD,
1474 REPLY_COMPRESSED_BA,
1475 STATISTICS_NOTIFICATION,
1476 REPLY_TX,
1477 };
1478 int i;
1479
1480 /************************
1481 * 1. Allocating HW data
1482 ************************/
1483 hw = iwl_alloc_all();
1484 if (!hw) {
1485 pr_err("%s: Cannot allocate network device\n", cfg->name);
1486 goto out;
1487 }
1488
1489 op_mode = hw->priv;
1490 op_mode->ops = &iwl_dvm_ops;
1491 priv = IWL_OP_MODE_GET_DVM(op_mode);
1492 priv->trans = trans;
1493 priv->dev = trans->dev;
1494 priv->cfg = cfg;
1495 priv->fw = fw;
1496
1497 switch (priv->cfg->device_family) {
1498 case IWL_DEVICE_FAMILY_1000:
1499 case IWL_DEVICE_FAMILY_100:
1500 priv->lib = &iwl1000_lib;
1501 break;
1502 case IWL_DEVICE_FAMILY_2000:
1503 case IWL_DEVICE_FAMILY_105:
1504 priv->lib = &iwl2000_lib;
1505 break;
1506 case IWL_DEVICE_FAMILY_2030:
1507 case IWL_DEVICE_FAMILY_135:
1508 priv->lib = &iwl2030_lib;
1509 break;
1510 case IWL_DEVICE_FAMILY_5000:
1511 priv->lib = &iwl5000_lib;
1512 break;
1513 case IWL_DEVICE_FAMILY_5150:
1514 priv->lib = &iwl5150_lib;
1515 break;
1516 case IWL_DEVICE_FAMILY_6000:
1517 case IWL_DEVICE_FAMILY_6005:
1518 case IWL_DEVICE_FAMILY_6000i:
1519 case IWL_DEVICE_FAMILY_6050:
1520 case IWL_DEVICE_FAMILY_6150:
1521 priv->lib = &iwl6000_lib;
1522 break;
1523 case IWL_DEVICE_FAMILY_6030:
1524 priv->lib = &iwl6030_lib;
1525 break;
1526 default:
1527 break;
1528 }
1529
1530 if (WARN_ON(!priv->lib))
1531 goto out_free_hw;
1532
1533 /*
1534 * Populate the state variables that the transport layer needs
1535 * to know about.
1536 */
1537 trans_cfg.op_mode = op_mode;
1538 trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
1539 trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1540 trans_cfg.rx_buf_size_8k = iwlwifi_mod_params.amsdu_size_8K;
1541 if (!iwlwifi_mod_params.wd_disable)
1542 trans_cfg.queue_watchdog_timeout =
1543 priv->cfg->base_params->wd_timeout;
1544 else
1545 trans_cfg.queue_watchdog_timeout = IWL_WATCHHDOG_DISABLED;
1546 trans_cfg.command_names = iwl_dvm_cmd_strings;
1547
1548 ucode_flags = fw->ucode_capa.flags;
1549
1550#ifndef CONFIG_IWLWIFI_P2P
1551 ucode_flags &= ~IWL_UCODE_TLV_FLAGS_P2P;
1552#endif
1553
1554 if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN) {
1555 priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
1556 trans_cfg.cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
1557 trans_cfg.queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
1558 trans_cfg.n_queue_to_fifo =
1559 ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
1560 } else {
1561 priv->sta_key_max_num = STA_KEY_MAX_NUM;
1562 trans_cfg.cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
1563 trans_cfg.queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
1564 trans_cfg.n_queue_to_fifo =
1565 ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
1566 }
1567
1568 /* Configure transport layer */
1569 iwl_trans_configure(priv->trans, &trans_cfg);
1570
1571 /* At this point both hw and priv are allocated. */
1572
1573 SET_IEEE80211_DEV(priv->hw, priv->trans->dev);
1574
1575 iwl_option_config(priv);
1576
1577 IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
1578
1579 /* is antenna coupling more than 35dB ? */
1580 priv->bt_ant_couple_ok =
1581 (iwlwifi_mod_params.ant_coupling >
1582 IWL_BT_ANTENNA_COUPLING_THRESHOLD) ?
1583 true : false;
1584
1585 /* enable/disable bt channel inhibition */
1586 priv->bt_ch_announce = iwlwifi_mod_params.bt_ch_announce;
1587 IWL_DEBUG_INFO(priv, "BT channel inhibition is %s\n",
1588 (priv->bt_ch_announce) ? "On" : "Off");
1589
1590 /* these spin locks will be used in apm_ops.init and EEPROM access
1591 * we should init now
1592 */
1593 spin_lock_init(&priv->statistics.lock);
1594
1595 /***********************
1596 * 2. Read REV register
1597 ***********************/
1598 IWL_INFO(priv, "Detected %s, REV=0x%X\n",
1599 priv->cfg->name, priv->trans->hw_rev);
1600
1601 if (iwl_trans_start_hw(priv->trans))
1602 goto out_free_hw;
1603
1604 /* Read the EEPROM */
1605 if (iwl_eeprom_init(priv, priv->trans->hw_rev)) {
1606 IWL_ERR(priv, "Unable to init EEPROM\n");
1607 goto out_free_hw;
1608 }
1609 /* Reset chip to save power until we load uCode during "up". */
1610 iwl_trans_stop_hw(priv->trans, false);
1611
1612 if (iwl_eeprom_check_version(priv))
1613 goto out_free_eeprom;
1614
1615 if (iwl_eeprom_init_hw_params(priv))
1616 goto out_free_eeprom;
1617
1618 /* extract MAC Address */
1619 iwl_eeprom_get_mac(priv, priv->addresses[0].addr);
1620 IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->addresses[0].addr);
1621 priv->hw->wiphy->addresses = priv->addresses;
1622 priv->hw->wiphy->n_addresses = 1;
1623 num_mac = iwl_eeprom_query16(priv, EEPROM_NUM_MAC_ADDRESS);
1624 if (num_mac > 1) {
1625 memcpy(priv->addresses[1].addr, priv->addresses[0].addr,
1626 ETH_ALEN);
1627 priv->addresses[1].addr[5]++;
1628 priv->hw->wiphy->n_addresses++;
1629 }
1630
1631 /************************
1632 * 4. Setup HW constants
1633 ************************/
1634 iwl_set_hw_params(priv);
1635
1636 if (!(priv->hw_params.sku & EEPROM_SKU_CAP_IPAN_ENABLE)) {
1637 IWL_DEBUG_INFO(priv, "Your EEPROM disabled PAN");
1638 ucode_flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
1639 /*
1640 * if not PAN, then don't support P2P -- might be a uCode
1641 * packaging bug or due to the eeprom check above
1642 */
1643 ucode_flags &= ~IWL_UCODE_TLV_FLAGS_P2P;
1644 priv->sta_key_max_num = STA_KEY_MAX_NUM;
1645 trans_cfg.cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
1646 trans_cfg.queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
1647 trans_cfg.n_queue_to_fifo =
1648 ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
1649
1650 /* Configure transport layer again*/
1651 iwl_trans_configure(priv->trans, &trans_cfg);
1652 }
1653
1654 /*******************
1655 * 5. Setup priv
1656 *******************/
1657 for (i = 0; i < IWL_MAX_HW_QUEUES; i++) {
1658 priv->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
1659 if (i < IWLAGN_FIRST_AMPDU_QUEUE &&
1660 i != IWL_DEFAULT_CMD_QUEUE_NUM &&
1661 i != IWL_IPAN_CMD_QUEUE_NUM)
1662 priv->queue_to_mac80211[i] = i;
1663 atomic_set(&priv->queue_stop_count[i], 0);
1664 }
1665
1666 WARN_ON(trans_cfg.queue_to_fifo[trans_cfg.cmd_queue] !=
1667 IWLAGN_CMD_FIFO_NUM);
1668
1669 if (iwl_init_drv(priv))
1670 goto out_free_eeprom;
1671
1672 /* At this point both hw and priv are initialized. */
1673
1674 /********************
1675 * 6. Setup services
1676 ********************/
1677 iwl_setup_deferred_work(priv);
1678 iwl_setup_rx_handlers(priv);
1679 iwl_testmode_init(priv);
1680
1681 iwl_power_initialize(priv);
1682 iwl_tt_initialize(priv);
1683
1684 snprintf(priv->hw->wiphy->fw_version,
1685 sizeof(priv->hw->wiphy->fw_version),
1686 "%s", fw->fw_version);
1687
1688 priv->new_scan_threshold_behaviour =
1689 !!(ucode_flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
1690
1691 priv->phy_calib_chain_noise_reset_cmd =
1692 fw->ucode_capa.standard_phy_calibration_size;
1693 priv->phy_calib_chain_noise_gain_cmd =
1694 fw->ucode_capa.standard_phy_calibration_size + 1;
1695
1696 /* initialize all valid contexts */
1697 iwl_init_context(priv, ucode_flags);
1698
1699 /**************************************************
1700 * This is still part of probe() in a sense...
1701 *
1702 * 7. Setup and register with mac80211 and debugfs
1703 **************************************************/
1704 if (iwlagn_mac_setup_register(priv, &fw->ucode_capa))
1705 goto out_destroy_workqueue;
1706
1707 if (iwl_dbgfs_register(priv, DRV_NAME))
1708 IWL_ERR(priv,
1709 "failed to create debugfs files. Ignoring error\n");
1710
1711 return op_mode;
1712
1713out_destroy_workqueue:
1714 destroy_workqueue(priv->workqueue);
1715 priv->workqueue = NULL;
1716 iwl_uninit_drv(priv);
1717out_free_eeprom:
1718 iwl_eeprom_free(priv);
1719out_free_hw:
1720 ieee80211_free_hw(priv->hw);
1721out:
1722 op_mode = NULL;
1723 return op_mode;
1724}
1725
1726static void iwl_op_mode_dvm_stop(struct iwl_op_mode *op_mode)
1727{
1728 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1729
1730 IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
1731
1732 iwl_dbgfs_unregister(priv);
1733
1734 iwl_testmode_cleanup(priv);
1735 iwlagn_mac_unregister(priv);
1736
1737 iwl_tt_exit(priv);
1738
1739 /*This will stop the queues, move the device to low power state */
1740 priv->ucode_loaded = false;
1741 iwl_trans_stop_device(priv->trans);
1742
1743 iwl_eeprom_free(priv);
1744
1745 /*netif_stop_queue(dev); */
1746 flush_workqueue(priv->workqueue);
1747
1748 /* ieee80211_unregister_hw calls iwlagn_mac_stop, which flushes
1749 * priv->workqueue... so we can't take down the workqueue
1750 * until now... */
1751 destroy_workqueue(priv->workqueue);
1752 priv->workqueue = NULL;
1753
1754 iwl_uninit_drv(priv);
1755
1756 dev_kfree_skb(priv->beacon_skb);
1757
1758 iwl_trans_stop_hw(priv->trans, true);
1759 ieee80211_free_hw(priv->hw);
1760}
1761
1762static const char * const desc_lookup_text[] = {
1763 "OK",
1764 "FAIL",
1765 "BAD_PARAM",
1766 "BAD_CHECKSUM",
1767 "NMI_INTERRUPT_WDG",
1768 "SYSASSERT",
1769 "FATAL_ERROR",
1770 "BAD_COMMAND",
1771 "HW_ERROR_TUNE_LOCK",
1772 "HW_ERROR_TEMPERATURE",
1773 "ILLEGAL_CHAN_FREQ",
1774 "VCC_NOT_STABLE",
1775 "FH_ERROR",
1776 "NMI_INTERRUPT_HOST",
1777 "NMI_INTERRUPT_ACTION_PT",
1778 "NMI_INTERRUPT_UNKNOWN",
1779 "UCODE_VERSION_MISMATCH",
1780 "HW_ERROR_ABS_LOCK",
1781 "HW_ERROR_CAL_LOCK_FAIL",
1782 "NMI_INTERRUPT_INST_ACTION_PT",
1783 "NMI_INTERRUPT_DATA_ACTION_PT",
1784 "NMI_TRM_HW_ER",
1785 "NMI_INTERRUPT_TRM",
1786 "NMI_INTERRUPT_BREAK_POINT",
1787 "DEBUG_0",
1788 "DEBUG_1",
1789 "DEBUG_2",
1790 "DEBUG_3",
1791};
1792
1793static struct { char *name; u8 num; } advanced_lookup[] = {
1794 { "NMI_INTERRUPT_WDG", 0x34 },
1795 { "SYSASSERT", 0x35 },
1796 { "UCODE_VERSION_MISMATCH", 0x37 },
1797 { "BAD_COMMAND", 0x38 },
1798 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
1799 { "FATAL_ERROR", 0x3D },
1800 { "NMI_TRM_HW_ERR", 0x46 },
1801 { "NMI_INTERRUPT_TRM", 0x4C },
1802 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
1803 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
1804 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
1805 { "NMI_INTERRUPT_HOST", 0x66 },
1806 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
1807 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
1808 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
1809 { "ADVANCED_SYSASSERT", 0 },
1810};
1811
1812static const char *desc_lookup(u32 num)
1813{
1814 int i;
1815 int max = ARRAY_SIZE(desc_lookup_text);
1816
1817 if (num < max)
1818 return desc_lookup_text[num];
1819
1820 max = ARRAY_SIZE(advanced_lookup) - 1;
1821 for (i = 0; i < max; i++) {
1822 if (advanced_lookup[i].num == num)
1823 break;
1824 }
1825 return advanced_lookup[i].name;
1826}
1827
1828#define ERROR_START_OFFSET (1 * sizeof(u32))
1829#define ERROR_ELEM_SIZE (7 * sizeof(u32))
1830
1831static void iwl_dump_nic_error_log(struct iwl_priv *priv)
1832{
1833 struct iwl_trans *trans = priv->trans;
1834 u32 base;
1835 struct iwl_error_event_table table;
1836
1837 base = priv->device_pointers.error_event_table;
1838 if (priv->cur_ucode == IWL_UCODE_INIT) {
1839 if (!base)
1840 base = priv->fw->init_errlog_ptr;
1841 } else {
1842 if (!base)
1843 base = priv->fw->inst_errlog_ptr;
1844 }
1845
1846 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
1847 IWL_ERR(priv,
1848 "Not valid error log pointer 0x%08X for %s uCode\n",
1849 base,
1850 (priv->cur_ucode == IWL_UCODE_INIT)
1851 ? "Init" : "RT");
1852 return;
1853 }
1854
1855 /*TODO: Update dbgfs with ISR error stats obtained below */
1856 iwl_read_targ_mem_words(trans, base, &table, sizeof(table));
1857
1858 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
1859 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
1860 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
1861 priv->status, table.valid);
1862 }
1863
1864 trace_iwlwifi_dev_ucode_error(trans->dev, table.error_id, table.tsf_low,
1865 table.data1, table.data2, table.line,
1866 table.blink1, table.blink2, table.ilink1,
1867 table.ilink2, table.bcon_time, table.gp1,
1868 table.gp2, table.gp3, table.ucode_ver,
1869 table.hw_ver, table.brd_ver);
1870 IWL_ERR(priv, "0x%08X | %-28s\n", table.error_id,
1871 desc_lookup(table.error_id));
1872 IWL_ERR(priv, "0x%08X | uPc\n", table.pc);
1873 IWL_ERR(priv, "0x%08X | branchlink1\n", table.blink1);
1874 IWL_ERR(priv, "0x%08X | branchlink2\n", table.blink2);
1875 IWL_ERR(priv, "0x%08X | interruptlink1\n", table.ilink1);
1876 IWL_ERR(priv, "0x%08X | interruptlink2\n", table.ilink2);
1877 IWL_ERR(priv, "0x%08X | data1\n", table.data1);
1878 IWL_ERR(priv, "0x%08X | data2\n", table.data2);
1879 IWL_ERR(priv, "0x%08X | line\n", table.line);
1880 IWL_ERR(priv, "0x%08X | beacon time\n", table.bcon_time);
1881 IWL_ERR(priv, "0x%08X | tsf low\n", table.tsf_low);
1882 IWL_ERR(priv, "0x%08X | tsf hi\n", table.tsf_hi);
1883 IWL_ERR(priv, "0x%08X | time gp1\n", table.gp1);
1884 IWL_ERR(priv, "0x%08X | time gp2\n", table.gp2);
1885 IWL_ERR(priv, "0x%08X | time gp3\n", table.gp3);
1886 IWL_ERR(priv, "0x%08X | uCode version\n", table.ucode_ver);
1887 IWL_ERR(priv, "0x%08X | hw version\n", table.hw_ver);
1888 IWL_ERR(priv, "0x%08X | board version\n", table.brd_ver);
1889 IWL_ERR(priv, "0x%08X | hcmd\n", table.hcmd);
1890 IWL_ERR(priv, "0x%08X | isr0\n", table.isr0);
1891 IWL_ERR(priv, "0x%08X | isr1\n", table.isr1);
1892 IWL_ERR(priv, "0x%08X | isr2\n", table.isr2);
1893 IWL_ERR(priv, "0x%08X | isr3\n", table.isr3);
1894 IWL_ERR(priv, "0x%08X | isr4\n", table.isr4);
1895 IWL_ERR(priv, "0x%08X | isr_pref\n", table.isr_pref);
1896 IWL_ERR(priv, "0x%08X | wait_event\n", table.wait_event);
1897 IWL_ERR(priv, "0x%08X | l2p_control\n", table.l2p_control);
1898 IWL_ERR(priv, "0x%08X | l2p_duration\n", table.l2p_duration);
1899 IWL_ERR(priv, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
1900 IWL_ERR(priv, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
1901 IWL_ERR(priv, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
1902 IWL_ERR(priv, "0x%08X | timestamp\n", table.u_timestamp);
1903 IWL_ERR(priv, "0x%08X | flow_handler\n", table.flow_handler);
1904}
1905
1906#define EVENT_START_OFFSET (4 * sizeof(u32))
1907
1908/**
1909 * iwl_print_event_log - Dump error event log to syslog
1910 *
1911 */
1912static int iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
1913 u32 num_events, u32 mode,
1914 int pos, char **buf, size_t bufsz)
1915{
1916 u32 i;
1917 u32 base; /* SRAM byte address of event log header */
1918 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1919 u32 ptr; /* SRAM byte address of log data */
1920 u32 ev, time, data; /* event log data */
1921 unsigned long reg_flags;
1922
1923 struct iwl_trans *trans = priv->trans;
1924
1925 if (num_events == 0)
1926 return pos;
1927
1928 base = priv->device_pointers.log_event_table;
1929 if (priv->cur_ucode == IWL_UCODE_INIT) {
1930 if (!base)
1931 base = priv->fw->init_evtlog_ptr;
1932 } else {
1933 if (!base)
1934 base = priv->fw->inst_evtlog_ptr;
1935 }
1936
1937 if (mode == 0)
1938 event_size = 2 * sizeof(u32);
1939 else
1940 event_size = 3 * sizeof(u32);
1941
1942 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1943
1944 /* Make sure device is powered up for SRAM reads */
1945 spin_lock_irqsave(&trans->reg_lock, reg_flags);
1946 if (unlikely(!iwl_grab_nic_access(trans)))
1947 goto out_unlock;
1948
1949 /* Set starting address; reads will auto-increment */
1950 iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
1951
1952 /* "time" is actually "data" for mode 0 (no timestamp).
1953 * place event id # at far right for easier visual parsing. */
1954 for (i = 0; i < num_events; i++) {
1955 ev = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1956 time = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1957 if (mode == 0) {
1958 /* data, ev */
1959 if (bufsz) {
1960 pos += scnprintf(*buf + pos, bufsz - pos,
1961 "EVT_LOG:0x%08x:%04u\n",
1962 time, ev);
1963 } else {
1964 trace_iwlwifi_dev_ucode_event(trans->dev, 0,
1965 time, ev);
1966 IWL_ERR(priv, "EVT_LOG:0x%08x:%04u\n",
1967 time, ev);
1968 }
1969 } else {
1970 data = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1971 if (bufsz) {
1972 pos += scnprintf(*buf + pos, bufsz - pos,
1973 "EVT_LOGT:%010u:0x%08x:%04u\n",
1974 time, data, ev);
1975 } else {
1976 IWL_ERR(priv, "EVT_LOGT:%010u:0x%08x:%04u\n",
1977 time, data, ev);
1978 trace_iwlwifi_dev_ucode_event(trans->dev, time,
1979 data, ev);
1980 }
1981 }
1982 }
1983
1984 /* Allow device to power down */
1985 iwl_release_nic_access(trans);
1986out_unlock:
1987 spin_unlock_irqrestore(&trans->reg_lock, reg_flags);
1988 return pos;
1989}
1990
1991/**
1992 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
1993 */
1994static int iwl_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
1995 u32 num_wraps, u32 next_entry,
1996 u32 size, u32 mode,
1997 int pos, char **buf, size_t bufsz)
1998{
1999 /*
2000 * display the newest DEFAULT_LOG_ENTRIES entries
2001 * i.e the entries just before the next ont that uCode would fill.
2002 */
2003 if (num_wraps) {
2004 if (next_entry < size) {
2005 pos = iwl_print_event_log(priv,
2006 capacity - (size - next_entry),
2007 size - next_entry, mode,
2008 pos, buf, bufsz);
2009 pos = iwl_print_event_log(priv, 0,
2010 next_entry, mode,
2011 pos, buf, bufsz);
2012 } else
2013 pos = iwl_print_event_log(priv, next_entry - size,
2014 size, mode, pos, buf, bufsz);
2015 } else {
2016 if (next_entry < size) {
2017 pos = iwl_print_event_log(priv, 0, next_entry,
2018 mode, pos, buf, bufsz);
2019 } else {
2020 pos = iwl_print_event_log(priv, next_entry - size,
2021 size, mode, pos, buf, bufsz);
2022 }
2023 }
2024 return pos;
2025}
2026
2027#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
2028
2029int iwl_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
2030 char **buf, bool display)
2031{
2032 u32 base; /* SRAM byte address of event log header */
2033 u32 capacity; /* event log capacity in # entries */
2034 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
2035 u32 num_wraps; /* # times uCode wrapped to top of log */
2036 u32 next_entry; /* index of next entry to be written by uCode */
2037 u32 size; /* # entries that we'll print */
2038 u32 logsize;
2039 int pos = 0;
2040 size_t bufsz = 0;
2041 struct iwl_trans *trans = priv->trans;
2042
2043 base = priv->device_pointers.log_event_table;
2044 if (priv->cur_ucode == IWL_UCODE_INIT) {
2045 logsize = priv->fw->init_evtlog_size;
2046 if (!base)
2047 base = priv->fw->init_evtlog_ptr;
2048 } else {
2049 logsize = priv->fw->inst_evtlog_size;
2050 if (!base)
2051 base = priv->fw->inst_evtlog_ptr;
2052 }
2053
2054 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
2055 IWL_ERR(priv,
2056 "Invalid event log pointer 0x%08X for %s uCode\n",
2057 base,
2058 (priv->cur_ucode == IWL_UCODE_INIT)
2059 ? "Init" : "RT");
2060 return -EINVAL;
2061 }
2062
2063 /* event log header */
2064 capacity = iwl_read_targ_mem(trans, base);
2065 mode = iwl_read_targ_mem(trans, base + (1 * sizeof(u32)));
2066 num_wraps = iwl_read_targ_mem(trans, base + (2 * sizeof(u32)));
2067 next_entry = iwl_read_targ_mem(trans, base + (3 * sizeof(u32)));
2068
2069 if (capacity > logsize) {
2070 IWL_ERR(priv, "Log capacity %d is bogus, limit to %d "
2071 "entries\n", capacity, logsize);
2072 capacity = logsize;
2073 }
2074
2075 if (next_entry > logsize) {
2076 IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
2077 next_entry, logsize);
2078 next_entry = logsize;
2079 }
2080
2081 size = num_wraps ? capacity : next_entry;
2082
2083 /* bail out if nothing in log */
2084 if (size == 0) {
2085 IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
2086 return pos;
2087 }
2088
2089#ifdef CONFIG_IWLWIFI_DEBUG
2090 if (!(iwl_have_debug_level(IWL_DL_FW_ERRORS)) && !full_log)
2091 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
2092 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
2093#else
2094 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
2095 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
2096#endif
2097 IWL_ERR(priv, "Start IWL Event Log Dump: display last %u entries\n",
2098 size);
2099
2100#ifdef CONFIG_IWLWIFI_DEBUG
2101 if (display) {
2102 if (full_log)
2103 bufsz = capacity * 48;
2104 else
2105 bufsz = size * 48;
2106 *buf = kmalloc(bufsz, GFP_KERNEL);
2107 if (!*buf)
2108 return -ENOMEM;
2109 }
2110 if (iwl_have_debug_level(IWL_DL_FW_ERRORS) || full_log) {
2111 /*
2112 * if uCode has wrapped back to top of log,
2113 * start at the oldest entry,
2114 * i.e the next one that uCode would fill.
2115 */
2116 if (num_wraps)
2117 pos = iwl_print_event_log(priv, next_entry,
2118 capacity - next_entry, mode,
2119 pos, buf, bufsz);
2120 /* (then/else) start at top of log */
2121 pos = iwl_print_event_log(priv, 0,
2122 next_entry, mode, pos, buf, bufsz);
2123 } else
2124 pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
2125 next_entry, size, mode,
2126 pos, buf, bufsz);
2127#else
2128 pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
2129 next_entry, size, mode,
2130 pos, buf, bufsz);
2131#endif
2132 return pos;
2133}
2134
2135static void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
2136{
2137 unsigned int reload_msec;
2138 unsigned long reload_jiffies;
2139
2140#ifdef CONFIG_IWLWIFI_DEBUG
2141 if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
2142 iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
2143#endif
2144
2145 /* uCode is no longer loaded. */
2146 priv->ucode_loaded = false;
2147
2148 /* Set the FW error flag -- cleared on iwl_down */
2149 set_bit(STATUS_FW_ERROR, &priv->status);
2150
2151 iwl_abort_notification_waits(&priv->notif_wait);
2152
2153 /* Keep the restart process from trying to send host
2154 * commands by clearing the ready bit */
2155 clear_bit(STATUS_READY, &priv->status);
2156
2157 wake_up(&priv->trans->wait_command_queue);
2158
2159 if (!ondemand) {
2160 /*
2161 * If firmware keep reloading, then it indicate something
2162 * serious wrong and firmware having problem to recover
2163 * from it. Instead of keep trying which will fill the syslog
2164 * and hang the system, let's just stop it
2165 */
2166 reload_jiffies = jiffies;
2167 reload_msec = jiffies_to_msecs((long) reload_jiffies -
2168 (long) priv->reload_jiffies);
2169 priv->reload_jiffies = reload_jiffies;
2170 if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
2171 priv->reload_count++;
2172 if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
2173 IWL_ERR(priv, "BUG_ON, Stop restarting\n");
2174 return;
2175 }
2176 } else
2177 priv->reload_count = 0;
2178 }
2179
2180 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2181 if (iwlwifi_mod_params.restart_fw) {
2182 IWL_DEBUG_FW_ERRORS(priv,
2183 "Restarting adapter due to uCode error.\n");
2184 queue_work(priv->workqueue, &priv->restart);
2185 } else
2186 IWL_DEBUG_FW_ERRORS(priv,
2187 "Detected FW error, but not restarting\n");
2188 }
2189}
2190
2191static void iwl_nic_error(struct iwl_op_mode *op_mode)
2192{
2193 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2194
2195 IWL_ERR(priv, "Loaded firmware version: %s\n",
2196 priv->fw->fw_version);
2197
2198 iwl_dump_nic_error_log(priv);
2199 iwl_dump_nic_event_log(priv, false, NULL, false);
2200
2201 iwlagn_fw_error(priv, false);
2202}
2203
2204static void iwl_cmd_queue_full(struct iwl_op_mode *op_mode)
2205{
2206 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2207
2208 if (!iwl_check_for_ct_kill(priv)) {
2209 IWL_ERR(priv, "Restarting adapter queue is full\n");
2210 iwlagn_fw_error(priv, false);
2211 }
2212}
2213
2214static void iwl_nic_config(struct iwl_op_mode *op_mode)
2215{
2216 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2217
2218 priv->lib->nic_config(priv);
2219}
2220
2221static void iwl_wimax_active(struct iwl_op_mode *op_mode)
2222{
2223 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2224
2225 clear_bit(STATUS_READY, &priv->status);
2226 IWL_ERR(priv, "RF is used by WiMAX\n");
2227}
2228
2229static void iwl_stop_sw_queue(struct iwl_op_mode *op_mode, int queue)
2230{
2231 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2232 int mq = priv->queue_to_mac80211[queue];
2233
2234 if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
2235 return;
2236
2237 if (atomic_inc_return(&priv->queue_stop_count[mq]) > 1) {
2238 IWL_DEBUG_TX_QUEUES(priv,
2239 "queue %d (mac80211 %d) already stopped\n",
2240 queue, mq);
2241 return;
2242 }
2243
2244 set_bit(mq, &priv->transport_queue_stop);
2245 ieee80211_stop_queue(priv->hw, mq);
2246}
2247
2248static void iwl_wake_sw_queue(struct iwl_op_mode *op_mode, int queue)
2249{
2250 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2251 int mq = priv->queue_to_mac80211[queue];
2252
2253 if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
2254 return;
2255
2256 if (atomic_dec_return(&priv->queue_stop_count[mq]) > 0) {
2257 IWL_DEBUG_TX_QUEUES(priv,
2258 "queue %d (mac80211 %d) already awake\n",
2259 queue, mq);
2260 return;
2261 }
2262
2263 clear_bit(mq, &priv->transport_queue_stop);
2264
2265 if (!priv->passive_no_rx)
2266 ieee80211_wake_queue(priv->hw, mq);
2267}
2268
2269void iwlagn_lift_passive_no_rx(struct iwl_priv *priv)
2270{
2271 int mq;
2272
2273 if (!priv->passive_no_rx)
2274 return;
2275
2276 for (mq = 0; mq < IWLAGN_FIRST_AMPDU_QUEUE; mq++) {
2277 if (!test_bit(mq, &priv->transport_queue_stop)) {
2278 IWL_DEBUG_TX_QUEUES(priv, "Wake queue %d", mq);
2279 ieee80211_wake_queue(priv->hw, mq);
2280 } else {
2281 IWL_DEBUG_TX_QUEUES(priv, "Don't wake queue %d", mq);
2282 }
2283 }
2284
2285 priv->passive_no_rx = false;
2286}
2287
2288static void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
2289{
2290 struct ieee80211_tx_info *info;
2291
2292 info = IEEE80211_SKB_CB(skb);
2293 kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
2294 dev_kfree_skb_any(skb);
2295}
2296
2297static void iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
2298{
2299 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2300
2301 if (state)
2302 set_bit(STATUS_RF_KILL_HW, &priv->status);
2303 else
2304 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2305
2306 wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
2307}
2308
2309const struct iwl_op_mode_ops iwl_dvm_ops = {
2310 .start = iwl_op_mode_dvm_start,
2311 .stop = iwl_op_mode_dvm_stop,
2312 .rx = iwl_rx_dispatch,
2313 .queue_full = iwl_stop_sw_queue,
2314 .queue_not_full = iwl_wake_sw_queue,
2315 .hw_rf_kill = iwl_set_hw_rfkill_state,
2316 .free_skb = iwl_free_skb,
2317 .nic_error = iwl_nic_error,
2318 .cmd_queue_full = iwl_cmd_queue_full,
2319 .nic_config = iwl_nic_config,
2320 .wimax_active = iwl_wimax_active,
2321};
2322
2323/*****************************************************************************
2324 *
2325 * driver and module entry point
2326 *
2327 *****************************************************************************/
2328
2329struct kmem_cache *iwl_tx_cmd_pool;
2330
2331static int __init iwl_init(void)
2332{
2333
2334 int ret;
2335 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
2336 pr_info(DRV_COPYRIGHT "\n");
2337
2338 iwl_tx_cmd_pool = kmem_cache_create("iwl_dev_cmd",
2339 sizeof(struct iwl_device_cmd),
2340 sizeof(void *), 0, NULL);
2341 if (!iwl_tx_cmd_pool)
2342 return -ENOMEM;
2343
2344 ret = iwlagn_rate_control_register();
2345 if (ret) {
2346 pr_err("Unable to register rate control algorithm: %d\n", ret);
2347 goto error_rc_register;
2348 }
2349
2350 ret = iwl_opmode_register("iwldvm", &iwl_dvm_ops);
2351 if (ret) {
2352 pr_err("Unable to register op_mode: %d\n", ret);
2353 goto error_opmode_register;
2354 }
2355 return ret;
2356
2357error_opmode_register:
2358 iwlagn_rate_control_unregister();
2359error_rc_register:
2360 kmem_cache_destroy(iwl_tx_cmd_pool);
2361 return ret;
2362}
2363module_init(iwl_init);
2364
2365static void __exit iwl_exit(void)
2366{
2367 iwl_opmode_deregister("iwldvm");
2368 iwlagn_rate_control_unregister();
2369 kmem_cache_destroy(iwl_tx_cmd_pool);
2370}
2371module_exit(iwl_exit);
diff --git a/drivers/net/wireless/iwlwifi/dvm/power.c b/drivers/net/wireless/iwlwifi/dvm/power.c
new file mode 100644
index 000000000000..72c940759ac7
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/power.c
@@ -0,0 +1,388 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <net/mac80211.h>
35#include "iwl-io.h"
36#include "iwl-debug.h"
37#include "iwl-trans.h"
38#include "iwl-modparams.h"
39#include "eeprom.h"
40#include "dev.h"
41#include "agn.h"
42#include "commands.h"
43#include "power.h"
44
45/*
46 * Setting power level allows the card to go to sleep when not busy.
47 *
48 * We calculate a sleep command based on the required latency, which
49 * we get from mac80211. In order to handle thermal throttling, we can
50 * also use pre-defined power levels.
51 */
52
53/*
54 * This defines the old power levels. They are still used by default
55 * (level 1) and for thermal throttle (levels 3 through 5)
56 */
57
58struct iwl_power_vec_entry {
59 struct iwl_powertable_cmd cmd;
60 u8 no_dtim; /* number of skip dtim */
61};
62
63#define IWL_DTIM_RANGE_0_MAX 2
64#define IWL_DTIM_RANGE_1_MAX 10
65
66#define NOSLP cpu_to_le16(0), 0, 0
67#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
68#define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK | \
69 IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \
70 IWL_POWER_ADVANCE_PM_ENA_MSK)
71#define ASLP_TOUT(T) cpu_to_le32(T)
72#define TU_TO_USEC 1024
73#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
74#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
75 cpu_to_le32(X1), \
76 cpu_to_le32(X2), \
77 cpu_to_le32(X3), \
78 cpu_to_le32(X4)}
79/* default power management (not Tx power) table values */
80/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
81/* DTIM 0 - 2 */
82static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
83 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
84 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
85 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
86 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
87 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
88};
89
90
91/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
92/* DTIM 3 - 10 */
93static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
94 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
95 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
96 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
97 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
98 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
99};
100
101/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
102/* DTIM 11 - */
103static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
104 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
105 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
106 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
107 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
108 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
109};
110
111/* advance power management */
112/* DTIM 0 - 2 */
113static const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = {
114 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
115 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
116 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
117 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
118 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
119 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
120 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
121 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
122 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
123 SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
124};
125
126
127/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
128/* DTIM 3 - 10 */
129static const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = {
130 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
131 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
132 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
133 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
134 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
135 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
136 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
137 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
138 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
139 SLP_VEC(1, 2, 6, 8, 0xFF), 0}, 2}
140};
141
142/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
143/* DTIM 11 - */
144static const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = {
145 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
146 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
147 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
148 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
149 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
150 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
151 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
152 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
153 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
154 SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
155};
156
157static void iwl_static_sleep_cmd(struct iwl_priv *priv,
158 struct iwl_powertable_cmd *cmd,
159 enum iwl_power_level lvl, int period)
160{
161 const struct iwl_power_vec_entry *table;
162 int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
163 int i;
164 u8 skip;
165 u32 slp_itrvl;
166
167 if (priv->cfg->adv_pm) {
168 table = apm_range_2;
169 if (period <= IWL_DTIM_RANGE_1_MAX)
170 table = apm_range_1;
171 if (period <= IWL_DTIM_RANGE_0_MAX)
172 table = apm_range_0;
173 } else {
174 table = range_2;
175 if (period <= IWL_DTIM_RANGE_1_MAX)
176 table = range_1;
177 if (period <= IWL_DTIM_RANGE_0_MAX)
178 table = range_0;
179 }
180
181 if (WARN_ON(lvl < 0 || lvl >= IWL_POWER_NUM))
182 memset(cmd, 0, sizeof(*cmd));
183 else
184 *cmd = table[lvl].cmd;
185
186 if (period == 0) {
187 skip = 0;
188 period = 1;
189 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
190 max_sleep[i] = 1;
191
192 } else {
193 skip = table[lvl].no_dtim;
194 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
195 max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
196 max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
197 }
198
199 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
200 /* figure out the listen interval based on dtim period and skip */
201 if (slp_itrvl == 0xFF)
202 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
203 cpu_to_le32(period * (skip + 1));
204
205 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
206 if (slp_itrvl > period)
207 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
208 cpu_to_le32((slp_itrvl / period) * period);
209
210 if (skip)
211 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
212 else
213 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
214
215 if (priv->cfg->base_params->shadow_reg_enable)
216 cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
217 else
218 cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
219
220 if (iwl_advanced_bt_coexist(priv)) {
221 if (!priv->cfg->bt_params->bt_sco_disable)
222 cmd->flags |= IWL_POWER_BT_SCO_ENA;
223 else
224 cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
225 }
226
227
228 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
229 if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
230 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
231 cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
232
233 /* enforce max sleep interval */
234 for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
235 if (le32_to_cpu(cmd->sleep_interval[i]) >
236 (max_sleep[i] * period))
237 cmd->sleep_interval[i] =
238 cpu_to_le32(max_sleep[i] * period);
239 if (i != (IWL_POWER_VEC_SIZE - 1)) {
240 if (le32_to_cpu(cmd->sleep_interval[i]) >
241 le32_to_cpu(cmd->sleep_interval[i+1]))
242 cmd->sleep_interval[i] =
243 cmd->sleep_interval[i+1];
244 }
245 }
246
247 if (priv->power_data.bus_pm)
248 cmd->flags |= IWL_POWER_PCI_PM_MSK;
249 else
250 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
251
252 IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
253 skip, period);
254 /* The power level here is 0-4 (used as array index), but user expects
255 to see 1-5 (according to spec). */
256 IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
257}
258
259static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
260 struct iwl_powertable_cmd *cmd)
261{
262 memset(cmd, 0, sizeof(*cmd));
263
264 if (priv->power_data.bus_pm)
265 cmd->flags |= IWL_POWER_PCI_PM_MSK;
266
267 IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
268}
269
270static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
271{
272 IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
273 IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
274 IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
275 IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
276 IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
277 le32_to_cpu(cmd->sleep_interval[0]),
278 le32_to_cpu(cmd->sleep_interval[1]),
279 le32_to_cpu(cmd->sleep_interval[2]),
280 le32_to_cpu(cmd->sleep_interval[3]),
281 le32_to_cpu(cmd->sleep_interval[4]));
282
283 return iwl_dvm_send_cmd_pdu(priv, POWER_TABLE_CMD, CMD_SYNC,
284 sizeof(struct iwl_powertable_cmd), cmd);
285}
286
287static void iwl_power_build_cmd(struct iwl_priv *priv,
288 struct iwl_powertable_cmd *cmd)
289{
290 bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
291 int dtimper;
292
293 dtimper = priv->hw->conf.ps_dtim_period ?: 1;
294
295 if (priv->wowlan)
296 iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, dtimper);
297 else if (!priv->cfg->base_params->no_idle_support &&
298 priv->hw->conf.flags & IEEE80211_CONF_IDLE)
299 iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
300 else if (iwl_tt_is_low_power_state(priv)) {
301 /* in thermal throttling low power state */
302 iwl_static_sleep_cmd(priv, cmd,
303 iwl_tt_current_power_mode(priv), dtimper);
304 } else if (!enabled)
305 iwl_power_sleep_cam_cmd(priv, cmd);
306 else if (priv->power_data.debug_sleep_level_override >= 0)
307 iwl_static_sleep_cmd(priv, cmd,
308 priv->power_data.debug_sleep_level_override,
309 dtimper);
310 else {
311 /* Note that the user parameter is 1-5 (according to spec),
312 but we pass 0-4 because it acts as an array index. */
313 if (iwlwifi_mod_params.power_level > IWL_POWER_INDEX_1 &&
314 iwlwifi_mod_params.power_level <= IWL_POWER_NUM)
315 iwl_static_sleep_cmd(priv, cmd,
316 iwlwifi_mod_params.power_level - 1, dtimper);
317 else
318 iwl_static_sleep_cmd(priv, cmd,
319 IWL_POWER_INDEX_1, dtimper);
320 }
321}
322
323int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
324 bool force)
325{
326 int ret;
327 bool update_chains;
328
329 lockdep_assert_held(&priv->mutex);
330
331 /* Don't update the RX chain when chain noise calibration is running */
332 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
333 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
334
335 if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
336 return 0;
337
338 if (!iwl_is_ready_rf(priv))
339 return -EIO;
340
341 /* scan complete use sleep_power_next, need to be updated */
342 memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
343 if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
344 IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
345 return 0;
346 }
347
348 if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
349 iwl_dvm_set_pmi(priv, true);
350
351 ret = iwl_set_power(priv, cmd);
352 if (!ret) {
353 if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
354 iwl_dvm_set_pmi(priv, false);
355
356 if (update_chains)
357 iwl_update_chain_flags(priv);
358 else
359 IWL_DEBUG_POWER(priv,
360 "Cannot update the power, chain noise "
361 "calibration running: %d\n",
362 priv->chain_noise_data.state);
363
364 memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
365 } else
366 IWL_ERR(priv, "set power fail, ret = %d", ret);
367
368 return ret;
369}
370
371int iwl_power_update_mode(struct iwl_priv *priv, bool force)
372{
373 struct iwl_powertable_cmd cmd;
374
375 iwl_power_build_cmd(priv, &cmd);
376 return iwl_power_set_mode(priv, &cmd, force);
377}
378
379/* initialize to default */
380void iwl_power_initialize(struct iwl_priv *priv)
381{
382 priv->power_data.bus_pm = priv->trans->pm_support;
383
384 priv->power_data.debug_sleep_level_override = -1;
385
386 memset(&priv->power_data.sleep_cmd, 0,
387 sizeof(priv->power_data.sleep_cmd));
388}
diff --git a/drivers/net/wireless/iwlwifi/dvm/power.h b/drivers/net/wireless/iwlwifi/dvm/power.h
new file mode 100644
index 000000000000..a2cee7f04848
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/power.h
@@ -0,0 +1,47 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28#ifndef __iwl_power_setting_h__
29#define __iwl_power_setting_h__
30
31#include "commands.h"
32
33struct iwl_power_mgr {
34 struct iwl_powertable_cmd sleep_cmd;
35 struct iwl_powertable_cmd sleep_cmd_next;
36 int debug_sleep_level_override;
37 bool bus_pm;
38};
39
40int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
41 bool force);
42int iwl_power_update_mode(struct iwl_priv *priv, bool force);
43void iwl_power_initialize(struct iwl_priv *priv);
44
45extern bool no_sleep_autoadjust;
46
47#endif /* __iwl_power_setting_h__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/rs.c b/drivers/net/wireless/iwlwifi/dvm/rs.c
new file mode 100644
index 000000000000..af102910e689
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/rs.c
@@ -0,0 +1,3367 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
29#include <linux/slab.h>
30#include <net/mac80211.h>
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
38#include "dev.h"
39#include "agn.h"
40
41#define RS_NAME "iwl-agn-rs"
42
43#define NUM_TRY_BEFORE_ANT_TOGGLE 1
44#define IWL_NUMBER_TRY 1
45#define IWL_HT_NUMBER_TRY 3
46
47#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
48#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
49#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
50
51/* max allowed rate miss before sync LQ cmd */
52#define IWL_MISSED_RATE_MAX 15
53/* max time to accum history 2 seconds */
54#define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
55
56static u8 rs_ht_to_legacy[] = {
57 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
61 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
62 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
63 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
64};
65
66static const u8 ant_toggle_lookup[] = {
67 /*ANT_NONE -> */ ANT_NONE,
68 /*ANT_A -> */ ANT_B,
69 /*ANT_B -> */ ANT_C,
70 /*ANT_AB -> */ ANT_BC,
71 /*ANT_C -> */ ANT_A,
72 /*ANT_AC -> */ ANT_AB,
73 /*ANT_BC -> */ ANT_AC,
74 /*ANT_ABC -> */ ANT_ABC,
75};
76
77#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
78 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
79 IWL_RATE_SISO_##s##M_PLCP, \
80 IWL_RATE_MIMO2_##s##M_PLCP,\
81 IWL_RATE_MIMO3_##s##M_PLCP,\
82 IWL_RATE_##r##M_IEEE, \
83 IWL_RATE_##ip##M_INDEX, \
84 IWL_RATE_##in##M_INDEX, \
85 IWL_RATE_##rp##M_INDEX, \
86 IWL_RATE_##rn##M_INDEX, \
87 IWL_RATE_##pp##M_INDEX, \
88 IWL_RATE_##np##M_INDEX }
89
90/*
91 * Parameter order:
92 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
93 *
94 * If there isn't a valid next or previous rate then INV is used which
95 * maps to IWL_RATE_INVALID
96 *
97 */
98const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
99 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
100 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
101 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
102 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
103 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
104 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
105 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
106 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
107 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
108 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
109 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
110 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
111 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
112 /* FIXME:RS: ^^ should be INV (legacy) */
113};
114
115static inline u8 rs_extract_rate(u32 rate_n_flags)
116{
117 return (u8)(rate_n_flags & RATE_MCS_RATE_MSK);
118}
119
120static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
121{
122 int idx = 0;
123
124 /* HT rate format */
125 if (rate_n_flags & RATE_MCS_HT_MSK) {
126 idx = rs_extract_rate(rate_n_flags);
127
128 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
129 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
130 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
131 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
132
133 idx += IWL_FIRST_OFDM_RATE;
134 /* skip 9M not supported in ht*/
135 if (idx >= IWL_RATE_9M_INDEX)
136 idx += 1;
137 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
138 return idx;
139
140 /* legacy rate format, search for match in table */
141 } else {
142 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
143 if (iwl_rates[idx].plcp ==
144 rs_extract_rate(rate_n_flags))
145 return idx;
146 }
147
148 return -1;
149}
150
151static void rs_rate_scale_perform(struct iwl_priv *priv,
152 struct sk_buff *skb,
153 struct ieee80211_sta *sta,
154 struct iwl_lq_sta *lq_sta);
155static void rs_fill_link_cmd(struct iwl_priv *priv,
156 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
157static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
158
159
160#ifdef CONFIG_MAC80211_DEBUGFS
161static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
162 u32 *rate_n_flags, int index);
163#else
164static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
165 u32 *rate_n_flags, int index)
166{}
167#endif
168
169/**
170 * The following tables contain the expected throughput metrics for all rates
171 *
172 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
173 *
174 * where invalid entries are zeros.
175 *
176 * CCK rates are only valid in legacy table and will only be used in G
177 * (2.4 GHz) band.
178 */
179
180static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
181 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
182};
183
184static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
185 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
186 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
187 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
188 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
189};
190
191static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
192 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
193 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
194 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
195 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
196};
197
198static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
199 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
200 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
201 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
202 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
203};
204
205static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
206 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
207 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
208 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
209 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
210};
211
212static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
213 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
214 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
215 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
216 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
217};
218
219static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
220 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
221 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
222 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
223 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
224};
225
226/* mbps, mcs */
227static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
228 { "1", "BPSK DSSS"},
229 { "2", "QPSK DSSS"},
230 {"5.5", "BPSK CCK"},
231 { "11", "QPSK CCK"},
232 { "6", "BPSK 1/2"},
233 { "9", "BPSK 1/2"},
234 { "12", "QPSK 1/2"},
235 { "18", "QPSK 3/4"},
236 { "24", "16QAM 1/2"},
237 { "36", "16QAM 3/4"},
238 { "48", "64QAM 2/3"},
239 { "54", "64QAM 3/4"},
240 { "60", "64QAM 5/6"},
241};
242
243#define MCS_INDEX_PER_STREAM (8)
244
245static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
246{
247 window->data = 0;
248 window->success_counter = 0;
249 window->success_ratio = IWL_INVALID_VALUE;
250 window->counter = 0;
251 window->average_tpt = IWL_INVALID_VALUE;
252 window->stamp = 0;
253}
254
255static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
256{
257 return (ant_type & valid_antenna) == ant_type;
258}
259
260/*
261 * removes the old data from the statistics. All data that is older than
262 * TID_MAX_TIME_DIFF, will be deleted.
263 */
264static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
265{
266 /* The oldest age we want to keep */
267 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
268
269 while (tl->queue_count &&
270 (tl->time_stamp < oldest_time)) {
271 tl->total -= tl->packet_count[tl->head];
272 tl->packet_count[tl->head] = 0;
273 tl->time_stamp += TID_QUEUE_CELL_SPACING;
274 tl->queue_count--;
275 tl->head++;
276 if (tl->head >= TID_QUEUE_MAX_SIZE)
277 tl->head = 0;
278 }
279}
280
281/*
282 * increment traffic load value for tid and also remove
283 * any old values if passed the certain time period
284 */
285static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
286 struct ieee80211_hdr *hdr)
287{
288 u32 curr_time = jiffies_to_msecs(jiffies);
289 u32 time_diff;
290 s32 index;
291 struct iwl_traffic_load *tl = NULL;
292 u8 tid;
293
294 if (ieee80211_is_data_qos(hdr->frame_control)) {
295 u8 *qc = ieee80211_get_qos_ctl(hdr);
296 tid = qc[0] & 0xf;
297 } else
298 return IWL_MAX_TID_COUNT;
299
300 if (unlikely(tid >= IWL_MAX_TID_COUNT))
301 return IWL_MAX_TID_COUNT;
302
303 tl = &lq_data->load[tid];
304
305 curr_time -= curr_time % TID_ROUND_VALUE;
306
307 /* Happens only for the first packet. Initialize the data */
308 if (!(tl->queue_count)) {
309 tl->total = 1;
310 tl->time_stamp = curr_time;
311 tl->queue_count = 1;
312 tl->head = 0;
313 tl->packet_count[0] = 1;
314 return IWL_MAX_TID_COUNT;
315 }
316
317 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
318 index = time_diff / TID_QUEUE_CELL_SPACING;
319
320 /* The history is too long: remove data that is older than */
321 /* TID_MAX_TIME_DIFF */
322 if (index >= TID_QUEUE_MAX_SIZE)
323 rs_tl_rm_old_stats(tl, curr_time);
324
325 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
326 tl->packet_count[index] = tl->packet_count[index] + 1;
327 tl->total = tl->total + 1;
328
329 if ((index + 1) > tl->queue_count)
330 tl->queue_count = index + 1;
331
332 return tid;
333}
334
335#ifdef CONFIG_MAC80211_DEBUGFS
336/**
337 * Program the device to use fixed rate for frame transmit
338 * This is for debugging/testing only
339 * once the device start use fixed rate, we need to reload the module
340 * to being back the normal operation.
341 */
342static void rs_program_fix_rate(struct iwl_priv *priv,
343 struct iwl_lq_sta *lq_sta)
344{
345 struct iwl_station_priv *sta_priv =
346 container_of(lq_sta, struct iwl_station_priv, lq_sta);
347 struct iwl_rxon_context *ctx = sta_priv->ctx;
348
349 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
350 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
351 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
352 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
353
354#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
355 /* testmode has higher priority to overwirte the fixed rate */
356 if (priv->tm_fixed_rate)
357 lq_sta->dbg_fixed_rate = priv->tm_fixed_rate;
358#endif
359
360 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
361 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
362
363 if (lq_sta->dbg_fixed_rate) {
364 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
365 iwl_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
366 false);
367 }
368}
369#endif
370
371/*
372 get the traffic load value for tid
373*/
374static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
375{
376 u32 curr_time = jiffies_to_msecs(jiffies);
377 u32 time_diff;
378 s32 index;
379 struct iwl_traffic_load *tl = NULL;
380
381 if (tid >= IWL_MAX_TID_COUNT)
382 return 0;
383
384 tl = &(lq_data->load[tid]);
385
386 curr_time -= curr_time % TID_ROUND_VALUE;
387
388 if (!(tl->queue_count))
389 return 0;
390
391 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
392 index = time_diff / TID_QUEUE_CELL_SPACING;
393
394 /* The history is too long: remove data that is older than */
395 /* TID_MAX_TIME_DIFF */
396 if (index >= TID_QUEUE_MAX_SIZE)
397 rs_tl_rm_old_stats(tl, curr_time);
398
399 return tl->total;
400}
401
402static int rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
403 struct iwl_lq_sta *lq_data, u8 tid,
404 struct ieee80211_sta *sta)
405{
406 int ret = -EAGAIN;
407 u32 load;
408
409 /*
410 * Don't create TX aggregation sessions when in high
411 * BT traffic, as they would just be disrupted by BT.
412 */
413 if (priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) {
414 IWL_ERR(priv, "BT traffic (%d), no aggregation allowed\n",
415 priv->bt_traffic_load);
416 return ret;
417 }
418
419 load = rs_tl_get_load(lq_data, tid);
420
421 if ((iwlwifi_mod_params.auto_agg) || (load > IWL_AGG_LOAD_THRESHOLD)) {
422 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
423 sta->addr, tid);
424 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
425 if (ret == -EAGAIN) {
426 /*
427 * driver and mac80211 is out of sync
428 * this might be cause by reloading firmware
429 * stop the tx ba session here
430 */
431 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
432 tid);
433 ieee80211_stop_tx_ba_session(sta, tid);
434 }
435 } else {
436 IWL_DEBUG_HT(priv, "Aggregation not enabled for tid %d "
437 "because load = %u\n", tid, load);
438 }
439 return ret;
440}
441
442static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
443 struct iwl_lq_sta *lq_data,
444 struct ieee80211_sta *sta)
445{
446 if (tid < IWL_MAX_TID_COUNT)
447 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
448 else
449 IWL_ERR(priv, "tid exceeds max TID count: %d/%d\n",
450 tid, IWL_MAX_TID_COUNT);
451}
452
453static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
454{
455 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
456 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
457 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
458}
459
460/*
461 * Static function to get the expected throughput from an iwl_scale_tbl_info
462 * that wraps a NULL pointer check
463 */
464static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
465{
466 if (tbl->expected_tpt)
467 return tbl->expected_tpt[rs_index];
468 return 0;
469}
470
471/**
472 * rs_collect_tx_data - Update the success/failure sliding window
473 *
474 * We keep a sliding window of the last 62 packets transmitted
475 * at this rate. window->data contains the bitmask of successful
476 * packets.
477 */
478static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
479 int scale_index, int attempts, int successes)
480{
481 struct iwl_rate_scale_data *window = NULL;
482 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
483 s32 fail_count, tpt;
484
485 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
486 return -EINVAL;
487
488 /* Select window for current tx bit rate */
489 window = &(tbl->win[scale_index]);
490
491 /* Get expected throughput */
492 tpt = get_expected_tpt(tbl, scale_index);
493
494 /*
495 * Keep track of only the latest 62 tx frame attempts in this rate's
496 * history window; anything older isn't really relevant any more.
497 * If we have filled up the sliding window, drop the oldest attempt;
498 * if the oldest attempt (highest bit in bitmap) shows "success",
499 * subtract "1" from the success counter (this is the main reason
500 * we keep these bitmaps!).
501 */
502 while (attempts > 0) {
503 if (window->counter >= IWL_RATE_MAX_WINDOW) {
504
505 /* remove earliest */
506 window->counter = IWL_RATE_MAX_WINDOW - 1;
507
508 if (window->data & mask) {
509 window->data &= ~mask;
510 window->success_counter--;
511 }
512 }
513
514 /* Increment frames-attempted counter */
515 window->counter++;
516
517 /* Shift bitmap by one frame to throw away oldest history */
518 window->data <<= 1;
519
520 /* Mark the most recent #successes attempts as successful */
521 if (successes > 0) {
522 window->success_counter++;
523 window->data |= 0x1;
524 successes--;
525 }
526
527 attempts--;
528 }
529
530 /* Calculate current success ratio, avoid divide-by-0! */
531 if (window->counter > 0)
532 window->success_ratio = 128 * (100 * window->success_counter)
533 / window->counter;
534 else
535 window->success_ratio = IWL_INVALID_VALUE;
536
537 fail_count = window->counter - window->success_counter;
538
539 /* Calculate average throughput, if we have enough history. */
540 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
541 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
542 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
543 else
544 window->average_tpt = IWL_INVALID_VALUE;
545
546 /* Tag this window as having been updated */
547 window->stamp = jiffies;
548
549 return 0;
550}
551
552/*
553 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
554 */
555/* FIXME:RS:remove this function and put the flags statically in the table */
556static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
557 struct iwl_scale_tbl_info *tbl,
558 int index, u8 use_green)
559{
560 u32 rate_n_flags = 0;
561
562 if (is_legacy(tbl->lq_type)) {
563 rate_n_flags = iwl_rates[index].plcp;
564 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
565 rate_n_flags |= RATE_MCS_CCK_MSK;
566
567 } else if (is_Ht(tbl->lq_type)) {
568 if (index > IWL_LAST_OFDM_RATE) {
569 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
570 index = IWL_LAST_OFDM_RATE;
571 }
572 rate_n_flags = RATE_MCS_HT_MSK;
573
574 if (is_siso(tbl->lq_type))
575 rate_n_flags |= iwl_rates[index].plcp_siso;
576 else if (is_mimo2(tbl->lq_type))
577 rate_n_flags |= iwl_rates[index].plcp_mimo2;
578 else
579 rate_n_flags |= iwl_rates[index].plcp_mimo3;
580 } else {
581 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
582 }
583
584 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
585 RATE_MCS_ANT_ABC_MSK);
586
587 if (is_Ht(tbl->lq_type)) {
588 if (tbl->is_ht40) {
589 if (tbl->is_dup)
590 rate_n_flags |= RATE_MCS_DUP_MSK;
591 else
592 rate_n_flags |= RATE_MCS_HT40_MSK;
593 }
594 if (tbl->is_SGI)
595 rate_n_flags |= RATE_MCS_SGI_MSK;
596
597 if (use_green) {
598 rate_n_flags |= RATE_MCS_GF_MSK;
599 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
600 rate_n_flags &= ~RATE_MCS_SGI_MSK;
601 IWL_ERR(priv, "GF was set with SGI:SISO\n");
602 }
603 }
604 }
605 return rate_n_flags;
606}
607
608/*
609 * Interpret uCode API's rate_n_flags format,
610 * fill "search" or "active" tx mode table.
611 */
612static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
613 enum ieee80211_band band,
614 struct iwl_scale_tbl_info *tbl,
615 int *rate_idx)
616{
617 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
618 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
619 u8 mcs;
620
621 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
622 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
623
624 if (*rate_idx == IWL_RATE_INVALID) {
625 *rate_idx = -1;
626 return -EINVAL;
627 }
628 tbl->is_SGI = 0; /* default legacy setup */
629 tbl->is_ht40 = 0;
630 tbl->is_dup = 0;
631 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
632 tbl->lq_type = LQ_NONE;
633 tbl->max_search = IWL_MAX_SEARCH;
634
635 /* legacy rate format */
636 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
637 if (num_of_ant == 1) {
638 if (band == IEEE80211_BAND_5GHZ)
639 tbl->lq_type = LQ_A;
640 else
641 tbl->lq_type = LQ_G;
642 }
643 /* HT rate format */
644 } else {
645 if (rate_n_flags & RATE_MCS_SGI_MSK)
646 tbl->is_SGI = 1;
647
648 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
649 (rate_n_flags & RATE_MCS_DUP_MSK))
650 tbl->is_ht40 = 1;
651
652 if (rate_n_flags & RATE_MCS_DUP_MSK)
653 tbl->is_dup = 1;
654
655 mcs = rs_extract_rate(rate_n_flags);
656
657 /* SISO */
658 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
659 if (num_of_ant == 1)
660 tbl->lq_type = LQ_SISO; /*else NONE*/
661 /* MIMO2 */
662 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
663 if (num_of_ant == 2)
664 tbl->lq_type = LQ_MIMO2;
665 /* MIMO3 */
666 } else {
667 if (num_of_ant == 3) {
668 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
669 tbl->lq_type = LQ_MIMO3;
670 }
671 }
672 }
673 return 0;
674}
675
676/* switch to another antenna/antennas and return 1 */
677/* if no other valid antenna found, return 0 */
678static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
679 struct iwl_scale_tbl_info *tbl)
680{
681 u8 new_ant_type;
682
683 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
684 return 0;
685
686 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
687 return 0;
688
689 new_ant_type = ant_toggle_lookup[tbl->ant_type];
690
691 while ((new_ant_type != tbl->ant_type) &&
692 !rs_is_valid_ant(valid_ant, new_ant_type))
693 new_ant_type = ant_toggle_lookup[new_ant_type];
694
695 if (new_ant_type == tbl->ant_type)
696 return 0;
697
698 tbl->ant_type = new_ant_type;
699 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
700 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
701 return 1;
702}
703
704/**
705 * Green-field mode is valid if the station supports it and
706 * there are no non-GF stations present in the BSS.
707 */
708static bool rs_use_green(struct ieee80211_sta *sta)
709{
710 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
711 struct iwl_rxon_context *ctx = sta_priv->ctx;
712
713 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
714 !(ctx->ht.non_gf_sta_present);
715}
716
717/**
718 * rs_get_supported_rates - get the available rates
719 *
720 * if management frame or broadcast frame only return
721 * basic available rates.
722 *
723 */
724static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
725 struct ieee80211_hdr *hdr,
726 enum iwl_table_type rate_type)
727{
728 if (is_legacy(rate_type)) {
729 return lq_sta->active_legacy_rate;
730 } else {
731 if (is_siso(rate_type))
732 return lq_sta->active_siso_rate;
733 else if (is_mimo2(rate_type))
734 return lq_sta->active_mimo2_rate;
735 else
736 return lq_sta->active_mimo3_rate;
737 }
738}
739
740static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
741 int rate_type)
742{
743 u8 high = IWL_RATE_INVALID;
744 u8 low = IWL_RATE_INVALID;
745
746 /* 802.11A or ht walks to the next literal adjacent rate in
747 * the rate table */
748 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
749 int i;
750 u32 mask;
751
752 /* Find the previous rate that is in the rate mask */
753 i = index - 1;
754 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
755 if (rate_mask & mask) {
756 low = i;
757 break;
758 }
759 }
760
761 /* Find the next rate that is in the rate mask */
762 i = index + 1;
763 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
764 if (rate_mask & mask) {
765 high = i;
766 break;
767 }
768 }
769
770 return (high << 8) | low;
771 }
772
773 low = index;
774 while (low != IWL_RATE_INVALID) {
775 low = iwl_rates[low].prev_rs;
776 if (low == IWL_RATE_INVALID)
777 break;
778 if (rate_mask & (1 << low))
779 break;
780 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
781 }
782
783 high = index;
784 while (high != IWL_RATE_INVALID) {
785 high = iwl_rates[high].next_rs;
786 if (high == IWL_RATE_INVALID)
787 break;
788 if (rate_mask & (1 << high))
789 break;
790 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
791 }
792
793 return (high << 8) | low;
794}
795
796static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
797 struct iwl_scale_tbl_info *tbl,
798 u8 scale_index, u8 ht_possible)
799{
800 s32 low;
801 u16 rate_mask;
802 u16 high_low;
803 u8 switch_to_legacy = 0;
804 u8 is_green = lq_sta->is_green;
805 struct iwl_priv *priv = lq_sta->drv;
806
807 /* check if we need to switch from HT to legacy rates.
808 * assumption is that mandatory rates (1Mbps or 6Mbps)
809 * are always supported (spec demand) */
810 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
811 switch_to_legacy = 1;
812 scale_index = rs_ht_to_legacy[scale_index];
813 if (lq_sta->band == IEEE80211_BAND_5GHZ)
814 tbl->lq_type = LQ_A;
815 else
816 tbl->lq_type = LQ_G;
817
818 if (num_of_ant(tbl->ant_type) > 1)
819 tbl->ant_type =
820 first_antenna(priv->hw_params.valid_tx_ant);
821
822 tbl->is_ht40 = 0;
823 tbl->is_SGI = 0;
824 tbl->max_search = IWL_MAX_SEARCH;
825 }
826
827 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
828
829 /* Mask with station rate restriction */
830 if (is_legacy(tbl->lq_type)) {
831 /* supp_rates has no CCK bits in A mode */
832 if (lq_sta->band == IEEE80211_BAND_5GHZ)
833 rate_mask = (u16)(rate_mask &
834 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
835 else
836 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
837 }
838
839 /* If we switched from HT to legacy, check current rate */
840 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
841 low = scale_index;
842 goto out;
843 }
844
845 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
846 tbl->lq_type);
847 low = high_low & 0xff;
848
849 if (low == IWL_RATE_INVALID)
850 low = scale_index;
851
852out:
853 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
854}
855
856/*
857 * Simple function to compare two rate scale table types
858 */
859static bool table_type_matches(struct iwl_scale_tbl_info *a,
860 struct iwl_scale_tbl_info *b)
861{
862 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
863 (a->is_SGI == b->is_SGI);
864}
865
866static void rs_bt_update_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
867 struct iwl_lq_sta *lq_sta)
868{
869 struct iwl_scale_tbl_info *tbl;
870 bool full_concurrent = priv->bt_full_concurrent;
871
872 if (priv->bt_ant_couple_ok) {
873 /*
874 * Is there a need to switch between
875 * full concurrency and 3-wire?
876 */
877 if (priv->bt_ci_compliance && priv->bt_ant_couple_ok)
878 full_concurrent = true;
879 else
880 full_concurrent = false;
881 }
882 if ((priv->bt_traffic_load != priv->last_bt_traffic_load) ||
883 (priv->bt_full_concurrent != full_concurrent)) {
884 priv->bt_full_concurrent = full_concurrent;
885 priv->last_bt_traffic_load = priv->bt_traffic_load;
886
887 /* Update uCode's rate table. */
888 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
889 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
890 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
891
892 queue_work(priv->workqueue, &priv->bt_full_concurrency);
893 }
894}
895
896/*
897 * mac80211 sends us Tx status
898 */
899static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
900 struct ieee80211_sta *sta, void *priv_sta,
901 struct sk_buff *skb)
902{
903 int legacy_success;
904 int retries;
905 int rs_index, mac_index, i;
906 struct iwl_lq_sta *lq_sta = priv_sta;
907 struct iwl_link_quality_cmd *table;
908 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
909 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)priv_r;
910 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
911 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
912 enum mac80211_rate_control_flags mac_flags;
913 u32 tx_rate;
914 struct iwl_scale_tbl_info tbl_type;
915 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
916 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
917 struct iwl_rxon_context *ctx = sta_priv->ctx;
918
919 IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
920
921 /* Treat uninitialized rate scaling data same as non-existing. */
922 if (!lq_sta) {
923 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
924 return;
925 } else if (!lq_sta->drv) {
926 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
927 return;
928 }
929
930 if (!ieee80211_is_data(hdr->frame_control) ||
931 info->flags & IEEE80211_TX_CTL_NO_ACK)
932 return;
933
934 /* This packet was aggregated but doesn't carry status info */
935 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
936 !(info->flags & IEEE80211_TX_STAT_AMPDU))
937 return;
938
939 /*
940 * Ignore this Tx frame response if its initial rate doesn't match
941 * that of latest Link Quality command. There may be stragglers
942 * from a previous Link Quality command, but we're no longer interested
943 * in those; they're either from the "active" mode while we're trying
944 * to check "search" mode, or a prior "search" mode after we've moved
945 * to a new "search" mode (which might become the new "active" mode).
946 */
947 table = &lq_sta->lq;
948 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
949 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
950 if (priv->band == IEEE80211_BAND_5GHZ)
951 rs_index -= IWL_FIRST_OFDM_RATE;
952 mac_flags = info->status.rates[0].flags;
953 mac_index = info->status.rates[0].idx;
954 /* For HT packets, map MCS to PLCP */
955 if (mac_flags & IEEE80211_TX_RC_MCS) {
956 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
957 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
958 mac_index++;
959 /*
960 * mac80211 HT index is always zero-indexed; we need to move
961 * HT OFDM rates after CCK rates in 2.4 GHz band
962 */
963 if (priv->band == IEEE80211_BAND_2GHZ)
964 mac_index += IWL_FIRST_OFDM_RATE;
965 }
966 /* Here we actually compare this rate to the latest LQ command */
967 if ((mac_index < 0) ||
968 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
969 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
970 (tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
971 (tbl_type.ant_type != info->status.antenna) ||
972 (!!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
973 (!!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
974 (rs_index != mac_index)) {
975 IWL_DEBUG_RATE(priv, "initial rate %d does not match %d (0x%x)\n", mac_index, rs_index, tx_rate);
976 /*
977 * Since rates mis-match, the last LQ command may have failed.
978 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
979 * ... driver.
980 */
981 lq_sta->missed_rate_counter++;
982 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
983 lq_sta->missed_rate_counter = 0;
984 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
985 }
986 /* Regardless, ignore this status info for outdated rate */
987 return;
988 } else
989 /* Rate did match, so reset the missed_rate_counter */
990 lq_sta->missed_rate_counter = 0;
991
992 /* Figure out if rate scale algorithm is in active or search table */
993 if (table_type_matches(&tbl_type,
994 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
995 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
996 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
997 } else if (table_type_matches(&tbl_type,
998 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
999 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1000 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1001 } else {
1002 IWL_DEBUG_RATE(priv, "Neither active nor search matches tx rate\n");
1003 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1004 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
1005 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1006 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
1007 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
1008 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
1009 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
1010 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
1011 /*
1012 * no matching table found, let's by-pass the data collection
1013 * and continue to perform rate scale to find the rate table
1014 */
1015 rs_stay_in_table(lq_sta, true);
1016 goto done;
1017 }
1018
1019 /*
1020 * Updating the frame history depends on whether packets were
1021 * aggregated.
1022 *
1023 * For aggregation, all packets were transmitted at the same rate, the
1024 * first index into rate scale table.
1025 */
1026 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
1027 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
1028 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
1029 &rs_index);
1030 rs_collect_tx_data(curr_tbl, rs_index,
1031 info->status.ampdu_len,
1032 info->status.ampdu_ack_len);
1033
1034 /* Update success/fail counts if not searching for new mode */
1035 if (lq_sta->stay_in_tbl) {
1036 lq_sta->total_success += info->status.ampdu_ack_len;
1037 lq_sta->total_failed += (info->status.ampdu_len -
1038 info->status.ampdu_ack_len);
1039 }
1040 } else {
1041 /*
1042 * For legacy, update frame history with for each Tx retry.
1043 */
1044 retries = info->status.rates[0].count - 1;
1045 /* HW doesn't send more than 15 retries */
1046 retries = min(retries, 15);
1047
1048 /* The last transmission may have been successful */
1049 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1050 /* Collect data for each rate used during failed TX attempts */
1051 for (i = 0; i <= retries; ++i) {
1052 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
1053 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
1054 &tbl_type, &rs_index);
1055 /*
1056 * Only collect stats if retried rate is in the same RS
1057 * table as active/search.
1058 */
1059 if (table_type_matches(&tbl_type, curr_tbl))
1060 tmp_tbl = curr_tbl;
1061 else if (table_type_matches(&tbl_type, other_tbl))
1062 tmp_tbl = other_tbl;
1063 else
1064 continue;
1065 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1066 i < retries ? 0 : legacy_success);
1067 }
1068
1069 /* Update success/fail counts if not searching for new mode */
1070 if (lq_sta->stay_in_tbl) {
1071 lq_sta->total_success += legacy_success;
1072 lq_sta->total_failed += retries + (1 - legacy_success);
1073 }
1074 }
1075 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1076 lq_sta->last_rate_n_flags = tx_rate;
1077done:
1078 /* See if there's a better rate or modulation mode to try. */
1079 if (sta && sta->supp_rates[sband->band])
1080 rs_rate_scale_perform(priv, skb, sta, lq_sta);
1081
1082#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_IWLWIFI_DEVICE_TESTMODE)
1083 if ((priv->tm_fixed_rate) &&
1084 (priv->tm_fixed_rate != lq_sta->dbg_fixed_rate))
1085 rs_program_fix_rate(priv, lq_sta);
1086#endif
1087 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist)
1088 rs_bt_update_lq(priv, ctx, lq_sta);
1089}
1090
1091/*
1092 * Begin a period of staying with a selected modulation mode.
1093 * Set "stay_in_tbl" flag to prevent any mode switches.
1094 * Set frame tx success limits according to legacy vs. high-throughput,
1095 * and reset overall (spanning all rates) tx success history statistics.
1096 * These control how long we stay using same modulation mode before
1097 * searching for a new mode.
1098 */
1099static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1100 struct iwl_lq_sta *lq_sta)
1101{
1102 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1103 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1104 if (is_legacy) {
1105 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1106 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1107 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1108 } else {
1109 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1110 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1111 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1112 }
1113 lq_sta->table_count = 0;
1114 lq_sta->total_failed = 0;
1115 lq_sta->total_success = 0;
1116 lq_sta->flush_timer = jiffies;
1117 lq_sta->action_counter = 0;
1118}
1119
1120/*
1121 * Find correct throughput table for given mode of modulation
1122 */
1123static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1124 struct iwl_scale_tbl_info *tbl)
1125{
1126 /* Used to choose among HT tables */
1127 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1128
1129 /* Check for invalid LQ type */
1130 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1131 tbl->expected_tpt = expected_tpt_legacy;
1132 return;
1133 }
1134
1135 /* Legacy rates have only one table */
1136 if (is_legacy(tbl->lq_type)) {
1137 tbl->expected_tpt = expected_tpt_legacy;
1138 return;
1139 }
1140
1141 /* Choose among many HT tables depending on number of streams
1142 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1143 * status */
1144 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1145 ht_tbl_pointer = expected_tpt_siso20MHz;
1146 else if (is_siso(tbl->lq_type))
1147 ht_tbl_pointer = expected_tpt_siso40MHz;
1148 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1149 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1150 else if (is_mimo2(tbl->lq_type))
1151 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1152 else if (is_mimo3(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1153 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1154 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1155 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1156
1157 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1158 tbl->expected_tpt = ht_tbl_pointer[0];
1159 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1160 tbl->expected_tpt = ht_tbl_pointer[1];
1161 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1162 tbl->expected_tpt = ht_tbl_pointer[2];
1163 else /* AGG+SGI */
1164 tbl->expected_tpt = ht_tbl_pointer[3];
1165}
1166
1167/*
1168 * Find starting rate for new "search" high-throughput mode of modulation.
1169 * Goal is to find lowest expected rate (under perfect conditions) that is
1170 * above the current measured throughput of "active" mode, to give new mode
1171 * a fair chance to prove itself without too many challenges.
1172 *
1173 * This gets called when transitioning to more aggressive modulation
1174 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1175 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1176 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1177 * bit rate will typically need to increase, but not if performance was bad.
1178 */
1179static s32 rs_get_best_rate(struct iwl_priv *priv,
1180 struct iwl_lq_sta *lq_sta,
1181 struct iwl_scale_tbl_info *tbl, /* "search" */
1182 u16 rate_mask, s8 index)
1183{
1184 /* "active" values */
1185 struct iwl_scale_tbl_info *active_tbl =
1186 &(lq_sta->lq_info[lq_sta->active_tbl]);
1187 s32 active_sr = active_tbl->win[index].success_ratio;
1188 s32 active_tpt = active_tbl->expected_tpt[index];
1189
1190 /* expected "search" throughput */
1191 s32 *tpt_tbl = tbl->expected_tpt;
1192
1193 s32 new_rate, high, low, start_hi;
1194 u16 high_low;
1195 s8 rate = index;
1196
1197 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1198
1199 for (; ;) {
1200 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1201 tbl->lq_type);
1202
1203 low = high_low & 0xff;
1204 high = (high_low >> 8) & 0xff;
1205
1206 /*
1207 * Lower the "search" bit rate, to give new "search" mode
1208 * approximately the same throughput as "active" if:
1209 *
1210 * 1) "Active" mode has been working modestly well (but not
1211 * great), and expected "search" throughput (under perfect
1212 * conditions) at candidate rate is above the actual
1213 * measured "active" throughput (but less than expected
1214 * "active" throughput under perfect conditions).
1215 * OR
1216 * 2) "Active" mode has been working perfectly or very well
1217 * and expected "search" throughput (under perfect
1218 * conditions) at candidate rate is above expected
1219 * "active" throughput (under perfect conditions).
1220 */
1221 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1222 ((active_sr > IWL_RATE_DECREASE_TH) &&
1223 (active_sr <= IWL_RATE_HIGH_TH) &&
1224 (tpt_tbl[rate] <= active_tpt))) ||
1225 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1226 (tpt_tbl[rate] > active_tpt))) {
1227
1228 /* (2nd or later pass)
1229 * If we've already tried to raise the rate, and are
1230 * now trying to lower it, use the higher rate. */
1231 if (start_hi != IWL_RATE_INVALID) {
1232 new_rate = start_hi;
1233 break;
1234 }
1235
1236 new_rate = rate;
1237
1238 /* Loop again with lower rate */
1239 if (low != IWL_RATE_INVALID)
1240 rate = low;
1241
1242 /* Lower rate not available, use the original */
1243 else
1244 break;
1245
1246 /* Else try to raise the "search" rate to match "active" */
1247 } else {
1248 /* (2nd or later pass)
1249 * If we've already tried to lower the rate, and are
1250 * now trying to raise it, use the lower rate. */
1251 if (new_rate != IWL_RATE_INVALID)
1252 break;
1253
1254 /* Loop again with higher rate */
1255 else if (high != IWL_RATE_INVALID) {
1256 start_hi = high;
1257 rate = high;
1258
1259 /* Higher rate not available, use the original */
1260 } else {
1261 new_rate = rate;
1262 break;
1263 }
1264 }
1265 }
1266
1267 return new_rate;
1268}
1269
1270/*
1271 * Set up search table for MIMO2
1272 */
1273static int rs_switch_to_mimo2(struct iwl_priv *priv,
1274 struct iwl_lq_sta *lq_sta,
1275 struct ieee80211_conf *conf,
1276 struct ieee80211_sta *sta,
1277 struct iwl_scale_tbl_info *tbl, int index)
1278{
1279 u16 rate_mask;
1280 s32 rate;
1281 s8 is_green = lq_sta->is_green;
1282 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1283 struct iwl_rxon_context *ctx = sta_priv->ctx;
1284
1285 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1286 return -1;
1287
1288 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1289 == WLAN_HT_CAP_SM_PS_STATIC)
1290 return -1;
1291
1292 /* Need both Tx chains/antennas to support MIMO */
1293 if (priv->hw_params.tx_chains_num < 2)
1294 return -1;
1295
1296 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1297
1298 tbl->lq_type = LQ_MIMO2;
1299 tbl->is_dup = lq_sta->is_dup;
1300 tbl->action = 0;
1301 tbl->max_search = IWL_MAX_SEARCH;
1302 rate_mask = lq_sta->active_mimo2_rate;
1303
1304 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
1305 tbl->is_ht40 = 1;
1306 else
1307 tbl->is_ht40 = 0;
1308
1309 rs_set_expected_tpt_table(lq_sta, tbl);
1310
1311 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1312
1313 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1314 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1315 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1316 rate, rate_mask);
1317 return -1;
1318 }
1319 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1320
1321 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1322 tbl->current_rate, is_green);
1323 return 0;
1324}
1325
1326/*
1327 * Set up search table for MIMO3
1328 */
1329static int rs_switch_to_mimo3(struct iwl_priv *priv,
1330 struct iwl_lq_sta *lq_sta,
1331 struct ieee80211_conf *conf,
1332 struct ieee80211_sta *sta,
1333 struct iwl_scale_tbl_info *tbl, int index)
1334{
1335 u16 rate_mask;
1336 s32 rate;
1337 s8 is_green = lq_sta->is_green;
1338 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1339 struct iwl_rxon_context *ctx = sta_priv->ctx;
1340
1341 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1342 return -1;
1343
1344 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1345 == WLAN_HT_CAP_SM_PS_STATIC)
1346 return -1;
1347
1348 /* Need both Tx chains/antennas to support MIMO */
1349 if (priv->hw_params.tx_chains_num < 3)
1350 return -1;
1351
1352 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
1353
1354 tbl->lq_type = LQ_MIMO3;
1355 tbl->is_dup = lq_sta->is_dup;
1356 tbl->action = 0;
1357 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1358 rate_mask = lq_sta->active_mimo3_rate;
1359
1360 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
1361 tbl->is_ht40 = 1;
1362 else
1363 tbl->is_ht40 = 0;
1364
1365 rs_set_expected_tpt_table(lq_sta, tbl);
1366
1367 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1368
1369 IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
1370 rate, rate_mask);
1371 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1372 IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
1373 rate, rate_mask);
1374 return -1;
1375 }
1376 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1377
1378 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1379 tbl->current_rate, is_green);
1380 return 0;
1381}
1382
1383/*
1384 * Set up search table for SISO
1385 */
1386static int rs_switch_to_siso(struct iwl_priv *priv,
1387 struct iwl_lq_sta *lq_sta,
1388 struct ieee80211_conf *conf,
1389 struct ieee80211_sta *sta,
1390 struct iwl_scale_tbl_info *tbl, int index)
1391{
1392 u16 rate_mask;
1393 u8 is_green = lq_sta->is_green;
1394 s32 rate;
1395 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1396 struct iwl_rxon_context *ctx = sta_priv->ctx;
1397
1398 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1399 return -1;
1400
1401 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1402
1403 tbl->is_dup = lq_sta->is_dup;
1404 tbl->lq_type = LQ_SISO;
1405 tbl->action = 0;
1406 tbl->max_search = IWL_MAX_SEARCH;
1407 rate_mask = lq_sta->active_siso_rate;
1408
1409 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
1410 tbl->is_ht40 = 1;
1411 else
1412 tbl->is_ht40 = 0;
1413
1414 if (is_green)
1415 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1416
1417 rs_set_expected_tpt_table(lq_sta, tbl);
1418 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1419
1420 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1421 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1422 IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
1423 rate, rate_mask);
1424 return -1;
1425 }
1426 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
1427 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1428 tbl->current_rate, is_green);
1429 return 0;
1430}
1431
1432/*
1433 * Try to switch to new modulation mode from legacy
1434 */
1435static int rs_move_legacy_other(struct iwl_priv *priv,
1436 struct iwl_lq_sta *lq_sta,
1437 struct ieee80211_conf *conf,
1438 struct ieee80211_sta *sta,
1439 int index)
1440{
1441 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1442 struct iwl_scale_tbl_info *search_tbl =
1443 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1444 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1445 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1446 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1447 u8 start_action;
1448 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1449 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1450 int ret = 0;
1451 u8 update_search_tbl_counter = 0;
1452
1453 switch (priv->bt_traffic_load) {
1454 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1455 /* nothing */
1456 break;
1457 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1458 /* avoid antenna B unless MIMO */
1459 if (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2)
1460 tbl->action = IWL_LEGACY_SWITCH_SISO;
1461 break;
1462 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1463 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1464 /* avoid antenna B and MIMO */
1465 valid_tx_ant =
1466 first_antenna(priv->hw_params.valid_tx_ant);
1467 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2 &&
1468 tbl->action != IWL_LEGACY_SWITCH_SISO)
1469 tbl->action = IWL_LEGACY_SWITCH_SISO;
1470 break;
1471 default:
1472 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1473 break;
1474 }
1475
1476 if (!iwl_ht_enabled(priv))
1477 /* stay in Legacy */
1478 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1479 else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1480 tbl->action > IWL_LEGACY_SWITCH_SISO)
1481 tbl->action = IWL_LEGACY_SWITCH_SISO;
1482
1483 /* configure as 1x1 if bt full concurrency */
1484 if (priv->bt_full_concurrent) {
1485 if (!iwl_ht_enabled(priv))
1486 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1487 else if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1488 tbl->action = IWL_LEGACY_SWITCH_SISO;
1489 valid_tx_ant =
1490 first_antenna(priv->hw_params.valid_tx_ant);
1491 }
1492
1493 start_action = tbl->action;
1494 for (; ;) {
1495 lq_sta->action_counter++;
1496 switch (tbl->action) {
1497 case IWL_LEGACY_SWITCH_ANTENNA1:
1498 case IWL_LEGACY_SWITCH_ANTENNA2:
1499 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1500
1501 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1502 tx_chains_num <= 1) ||
1503 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1504 tx_chains_num <= 2))
1505 break;
1506
1507 /* Don't change antenna if success has been great */
1508 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1509 !priv->bt_full_concurrent &&
1510 priv->bt_traffic_load ==
1511 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
1512 break;
1513
1514 /* Set up search table to try other antenna */
1515 memcpy(search_tbl, tbl, sz);
1516
1517 if (rs_toggle_antenna(valid_tx_ant,
1518 &search_tbl->current_rate, search_tbl)) {
1519 update_search_tbl_counter = 1;
1520 rs_set_expected_tpt_table(lq_sta, search_tbl);
1521 goto out;
1522 }
1523 break;
1524 case IWL_LEGACY_SWITCH_SISO:
1525 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1526
1527 /* Set up search table to try SISO */
1528 memcpy(search_tbl, tbl, sz);
1529 search_tbl->is_SGI = 0;
1530 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1531 search_tbl, index);
1532 if (!ret) {
1533 lq_sta->action_counter = 0;
1534 goto out;
1535 }
1536
1537 break;
1538 case IWL_LEGACY_SWITCH_MIMO2_AB:
1539 case IWL_LEGACY_SWITCH_MIMO2_AC:
1540 case IWL_LEGACY_SWITCH_MIMO2_BC:
1541 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1542
1543 /* Set up search table to try MIMO */
1544 memcpy(search_tbl, tbl, sz);
1545 search_tbl->is_SGI = 0;
1546
1547 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1548 search_tbl->ant_type = ANT_AB;
1549 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1550 search_tbl->ant_type = ANT_AC;
1551 else
1552 search_tbl->ant_type = ANT_BC;
1553
1554 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1555 break;
1556
1557 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1558 search_tbl, index);
1559 if (!ret) {
1560 lq_sta->action_counter = 0;
1561 goto out;
1562 }
1563 break;
1564
1565 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1566 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
1567
1568 /* Set up search table to try MIMO3 */
1569 memcpy(search_tbl, tbl, sz);
1570 search_tbl->is_SGI = 0;
1571
1572 search_tbl->ant_type = ANT_ABC;
1573
1574 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1575 break;
1576
1577 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1578 search_tbl, index);
1579 if (!ret) {
1580 lq_sta->action_counter = 0;
1581 goto out;
1582 }
1583 break;
1584 }
1585 tbl->action++;
1586 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1587 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1588
1589 if (tbl->action == start_action)
1590 break;
1591
1592 }
1593 search_tbl->lq_type = LQ_NONE;
1594 return 0;
1595
1596out:
1597 lq_sta->search_better_tbl = 1;
1598 tbl->action++;
1599 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1600 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1601 if (update_search_tbl_counter)
1602 search_tbl->action = tbl->action;
1603 return 0;
1604
1605}
1606
1607/*
1608 * Try to switch to new modulation mode from SISO
1609 */
1610static int rs_move_siso_to_other(struct iwl_priv *priv,
1611 struct iwl_lq_sta *lq_sta,
1612 struct ieee80211_conf *conf,
1613 struct ieee80211_sta *sta, int index)
1614{
1615 u8 is_green = lq_sta->is_green;
1616 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1617 struct iwl_scale_tbl_info *search_tbl =
1618 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1619 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1620 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1621 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1622 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1623 u8 start_action;
1624 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1625 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1626 u8 update_search_tbl_counter = 0;
1627 int ret;
1628
1629 switch (priv->bt_traffic_load) {
1630 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1631 /* nothing */
1632 break;
1633 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1634 /* avoid antenna B unless MIMO */
1635 if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
1636 tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
1637 break;
1638 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1639 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1640 /* avoid antenna B and MIMO */
1641 valid_tx_ant =
1642 first_antenna(priv->hw_params.valid_tx_ant);
1643 if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
1644 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1645 break;
1646 default:
1647 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1648 break;
1649 }
1650
1651 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
1652 tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
1653 /* stay in SISO */
1654 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1655 }
1656
1657 /* configure as 1x1 if bt full concurrency */
1658 if (priv->bt_full_concurrent) {
1659 valid_tx_ant =
1660 first_antenna(priv->hw_params.valid_tx_ant);
1661 if (tbl->action >= IWL_LEGACY_SWITCH_ANTENNA2)
1662 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1663 }
1664
1665 start_action = tbl->action;
1666 for (;;) {
1667 lq_sta->action_counter++;
1668 switch (tbl->action) {
1669 case IWL_SISO_SWITCH_ANTENNA1:
1670 case IWL_SISO_SWITCH_ANTENNA2:
1671 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1672 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1673 tx_chains_num <= 1) ||
1674 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1675 tx_chains_num <= 2))
1676 break;
1677
1678 if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
1679 !priv->bt_full_concurrent &&
1680 priv->bt_traffic_load ==
1681 IWL_BT_COEX_TRAFFIC_LOAD_NONE)
1682 break;
1683
1684 memcpy(search_tbl, tbl, sz);
1685 if (rs_toggle_antenna(valid_tx_ant,
1686 &search_tbl->current_rate, search_tbl)) {
1687 update_search_tbl_counter = 1;
1688 goto out;
1689 }
1690 break;
1691 case IWL_SISO_SWITCH_MIMO2_AB:
1692 case IWL_SISO_SWITCH_MIMO2_AC:
1693 case IWL_SISO_SWITCH_MIMO2_BC:
1694 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1695 memcpy(search_tbl, tbl, sz);
1696 search_tbl->is_SGI = 0;
1697
1698 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1699 search_tbl->ant_type = ANT_AB;
1700 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1701 search_tbl->ant_type = ANT_AC;
1702 else
1703 search_tbl->ant_type = ANT_BC;
1704
1705 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1706 break;
1707
1708 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1709 search_tbl, index);
1710 if (!ret)
1711 goto out;
1712 break;
1713 case IWL_SISO_SWITCH_GI:
1714 if (!tbl->is_ht40 && !(ht_cap->cap &
1715 IEEE80211_HT_CAP_SGI_20))
1716 break;
1717 if (tbl->is_ht40 && !(ht_cap->cap &
1718 IEEE80211_HT_CAP_SGI_40))
1719 break;
1720
1721 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1722
1723 memcpy(search_tbl, tbl, sz);
1724 if (is_green) {
1725 if (!tbl->is_SGI)
1726 break;
1727 else
1728 IWL_ERR(priv,
1729 "SGI was set in GF+SISO\n");
1730 }
1731 search_tbl->is_SGI = !tbl->is_SGI;
1732 rs_set_expected_tpt_table(lq_sta, search_tbl);
1733 if (tbl->is_SGI) {
1734 s32 tpt = lq_sta->last_tpt / 100;
1735 if (tpt >= search_tbl->expected_tpt[index])
1736 break;
1737 }
1738 search_tbl->current_rate =
1739 rate_n_flags_from_tbl(priv, search_tbl,
1740 index, is_green);
1741 update_search_tbl_counter = 1;
1742 goto out;
1743 case IWL_SISO_SWITCH_MIMO3_ABC:
1744 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
1745 memcpy(search_tbl, tbl, sz);
1746 search_tbl->is_SGI = 0;
1747 search_tbl->ant_type = ANT_ABC;
1748
1749 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1750 break;
1751
1752 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1753 search_tbl, index);
1754 if (!ret)
1755 goto out;
1756 break;
1757 }
1758 tbl->action++;
1759 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1760 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1761
1762 if (tbl->action == start_action)
1763 break;
1764 }
1765 search_tbl->lq_type = LQ_NONE;
1766 return 0;
1767
1768 out:
1769 lq_sta->search_better_tbl = 1;
1770 tbl->action++;
1771 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1772 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1773 if (update_search_tbl_counter)
1774 search_tbl->action = tbl->action;
1775
1776 return 0;
1777}
1778
1779/*
1780 * Try to switch to new modulation mode from MIMO2
1781 */
1782static int rs_move_mimo2_to_other(struct iwl_priv *priv,
1783 struct iwl_lq_sta *lq_sta,
1784 struct ieee80211_conf *conf,
1785 struct ieee80211_sta *sta, int index)
1786{
1787 s8 is_green = lq_sta->is_green;
1788 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1789 struct iwl_scale_tbl_info *search_tbl =
1790 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1791 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1792 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1793 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1794 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1795 u8 start_action;
1796 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1797 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1798 u8 update_search_tbl_counter = 0;
1799 int ret;
1800
1801 switch (priv->bt_traffic_load) {
1802 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1803 /* nothing */
1804 break;
1805 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1806 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1807 /* avoid antenna B and MIMO */
1808 if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
1809 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1810 break;
1811 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1812 /* avoid antenna B unless MIMO */
1813 if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
1814 tbl->action == IWL_MIMO2_SWITCH_SISO_C)
1815 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1816 break;
1817 default:
1818 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1819 break;
1820 }
1821
1822 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1823 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1824 tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
1825 /* switch in SISO */
1826 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1827 }
1828
1829 /* configure as 1x1 if bt full concurrency */
1830 if (priv->bt_full_concurrent &&
1831 (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
1832 tbl->action > IWL_MIMO2_SWITCH_SISO_C))
1833 tbl->action = IWL_MIMO2_SWITCH_SISO_A;
1834
1835 start_action = tbl->action;
1836 for (;;) {
1837 lq_sta->action_counter++;
1838 switch (tbl->action) {
1839 case IWL_MIMO2_SWITCH_ANTENNA1:
1840 case IWL_MIMO2_SWITCH_ANTENNA2:
1841 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1842
1843 if (tx_chains_num <= 2)
1844 break;
1845
1846 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1847 break;
1848
1849 memcpy(search_tbl, tbl, sz);
1850 if (rs_toggle_antenna(valid_tx_ant,
1851 &search_tbl->current_rate, search_tbl)) {
1852 update_search_tbl_counter = 1;
1853 goto out;
1854 }
1855 break;
1856 case IWL_MIMO2_SWITCH_SISO_A:
1857 case IWL_MIMO2_SWITCH_SISO_B:
1858 case IWL_MIMO2_SWITCH_SISO_C:
1859 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1860
1861 /* Set up new search table for SISO */
1862 memcpy(search_tbl, tbl, sz);
1863
1864 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1865 search_tbl->ant_type = ANT_A;
1866 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1867 search_tbl->ant_type = ANT_B;
1868 else
1869 search_tbl->ant_type = ANT_C;
1870
1871 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1872 break;
1873
1874 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1875 search_tbl, index);
1876 if (!ret)
1877 goto out;
1878
1879 break;
1880
1881 case IWL_MIMO2_SWITCH_GI:
1882 if (!tbl->is_ht40 && !(ht_cap->cap &
1883 IEEE80211_HT_CAP_SGI_20))
1884 break;
1885 if (tbl->is_ht40 && !(ht_cap->cap &
1886 IEEE80211_HT_CAP_SGI_40))
1887 break;
1888
1889 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1890
1891 /* Set up new search table for MIMO2 */
1892 memcpy(search_tbl, tbl, sz);
1893 search_tbl->is_SGI = !tbl->is_SGI;
1894 rs_set_expected_tpt_table(lq_sta, search_tbl);
1895 /*
1896 * If active table already uses the fastest possible
1897 * modulation (dual stream with short guard interval),
1898 * and it's working well, there's no need to look
1899 * for a better type of modulation!
1900 */
1901 if (tbl->is_SGI) {
1902 s32 tpt = lq_sta->last_tpt / 100;
1903 if (tpt >= search_tbl->expected_tpt[index])
1904 break;
1905 }
1906 search_tbl->current_rate =
1907 rate_n_flags_from_tbl(priv, search_tbl,
1908 index, is_green);
1909 update_search_tbl_counter = 1;
1910 goto out;
1911
1912 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1913 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
1914 memcpy(search_tbl, tbl, sz);
1915 search_tbl->is_SGI = 0;
1916 search_tbl->ant_type = ANT_ABC;
1917
1918 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
1919 break;
1920
1921 ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
1922 search_tbl, index);
1923 if (!ret)
1924 goto out;
1925
1926 break;
1927 }
1928 tbl->action++;
1929 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1930 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1931
1932 if (tbl->action == start_action)
1933 break;
1934 }
1935 search_tbl->lq_type = LQ_NONE;
1936 return 0;
1937 out:
1938 lq_sta->search_better_tbl = 1;
1939 tbl->action++;
1940 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1941 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1942 if (update_search_tbl_counter)
1943 search_tbl->action = tbl->action;
1944
1945 return 0;
1946
1947}
1948
1949/*
1950 * Try to switch to new modulation mode from MIMO3
1951 */
1952static int rs_move_mimo3_to_other(struct iwl_priv *priv,
1953 struct iwl_lq_sta *lq_sta,
1954 struct ieee80211_conf *conf,
1955 struct ieee80211_sta *sta, int index)
1956{
1957 s8 is_green = lq_sta->is_green;
1958 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1959 struct iwl_scale_tbl_info *search_tbl =
1960 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1961 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1962 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1963 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1964 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1965 u8 start_action;
1966 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1967 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1968 int ret;
1969 u8 update_search_tbl_counter = 0;
1970
1971 switch (priv->bt_traffic_load) {
1972 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
1973 /* nothing */
1974 break;
1975 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1976 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1977 /* avoid antenna B and MIMO */
1978 if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
1979 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1980 break;
1981 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1982 /* avoid antenna B unless MIMO */
1983 if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
1984 tbl->action == IWL_MIMO3_SWITCH_SISO_C)
1985 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1986 break;
1987 default:
1988 IWL_ERR(priv, "Invalid BT load %d", priv->bt_traffic_load);
1989 break;
1990 }
1991
1992 if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
1993 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
1994 tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
1995 /* switch in SISO */
1996 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
1997 }
1998
1999 /* configure as 1x1 if bt full concurrency */
2000 if (priv->bt_full_concurrent &&
2001 (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
2002 tbl->action > IWL_MIMO3_SWITCH_SISO_C))
2003 tbl->action = IWL_MIMO3_SWITCH_SISO_A;
2004
2005 start_action = tbl->action;
2006 for (;;) {
2007 lq_sta->action_counter++;
2008 switch (tbl->action) {
2009 case IWL_MIMO3_SWITCH_ANTENNA1:
2010 case IWL_MIMO3_SWITCH_ANTENNA2:
2011 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
2012
2013 if (tx_chains_num <= 3)
2014 break;
2015
2016 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
2017 break;
2018
2019 memcpy(search_tbl, tbl, sz);
2020 if (rs_toggle_antenna(valid_tx_ant,
2021 &search_tbl->current_rate, search_tbl))
2022 goto out;
2023 break;
2024 case IWL_MIMO3_SWITCH_SISO_A:
2025 case IWL_MIMO3_SWITCH_SISO_B:
2026 case IWL_MIMO3_SWITCH_SISO_C:
2027 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
2028
2029 /* Set up new search table for SISO */
2030 memcpy(search_tbl, tbl, sz);
2031
2032 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
2033 search_tbl->ant_type = ANT_A;
2034 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
2035 search_tbl->ant_type = ANT_B;
2036 else
2037 search_tbl->ant_type = ANT_C;
2038
2039 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2040 break;
2041
2042 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
2043 search_tbl, index);
2044 if (!ret)
2045 goto out;
2046
2047 break;
2048
2049 case IWL_MIMO3_SWITCH_MIMO2_AB:
2050 case IWL_MIMO3_SWITCH_MIMO2_AC:
2051 case IWL_MIMO3_SWITCH_MIMO2_BC:
2052 IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
2053
2054 memcpy(search_tbl, tbl, sz);
2055 search_tbl->is_SGI = 0;
2056 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
2057 search_tbl->ant_type = ANT_AB;
2058 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
2059 search_tbl->ant_type = ANT_AC;
2060 else
2061 search_tbl->ant_type = ANT_BC;
2062
2063 if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
2064 break;
2065
2066 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
2067 search_tbl, index);
2068 if (!ret)
2069 goto out;
2070
2071 break;
2072
2073 case IWL_MIMO3_SWITCH_GI:
2074 if (!tbl->is_ht40 && !(ht_cap->cap &
2075 IEEE80211_HT_CAP_SGI_20))
2076 break;
2077 if (tbl->is_ht40 && !(ht_cap->cap &
2078 IEEE80211_HT_CAP_SGI_40))
2079 break;
2080
2081 IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
2082
2083 /* Set up new search table for MIMO */
2084 memcpy(search_tbl, tbl, sz);
2085 search_tbl->is_SGI = !tbl->is_SGI;
2086 rs_set_expected_tpt_table(lq_sta, search_tbl);
2087 /*
2088 * If active table already uses the fastest possible
2089 * modulation (dual stream with short guard interval),
2090 * and it's working well, there's no need to look
2091 * for a better type of modulation!
2092 */
2093 if (tbl->is_SGI) {
2094 s32 tpt = lq_sta->last_tpt / 100;
2095 if (tpt >= search_tbl->expected_tpt[index])
2096 break;
2097 }
2098 search_tbl->current_rate =
2099 rate_n_flags_from_tbl(priv, search_tbl,
2100 index, is_green);
2101 update_search_tbl_counter = 1;
2102 goto out;
2103 }
2104 tbl->action++;
2105 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2106 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
2107
2108 if (tbl->action == start_action)
2109 break;
2110 }
2111 search_tbl->lq_type = LQ_NONE;
2112 return 0;
2113 out:
2114 lq_sta->search_better_tbl = 1;
2115 tbl->action++;
2116 if (tbl->action > IWL_MIMO3_SWITCH_GI)
2117 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
2118 if (update_search_tbl_counter)
2119 search_tbl->action = tbl->action;
2120
2121 return 0;
2122
2123}
2124
2125/*
2126 * Check whether we should continue using same modulation mode, or
2127 * begin search for a new mode, based on:
2128 * 1) # tx successes or failures while using this mode
2129 * 2) # times calling this function
2130 * 3) elapsed time in this mode (not used, for now)
2131 */
2132static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
2133{
2134 struct iwl_scale_tbl_info *tbl;
2135 int i;
2136 int active_tbl;
2137 int flush_interval_passed = 0;
2138 struct iwl_priv *priv;
2139
2140 priv = lq_sta->drv;
2141 active_tbl = lq_sta->active_tbl;
2142
2143 tbl = &(lq_sta->lq_info[active_tbl]);
2144
2145 /* If we've been disallowing search, see if we should now allow it */
2146 if (lq_sta->stay_in_tbl) {
2147
2148 /* Elapsed time using current modulation mode */
2149 if (lq_sta->flush_timer)
2150 flush_interval_passed =
2151 time_after(jiffies,
2152 (unsigned long)(lq_sta->flush_timer +
2153 IWL_RATE_SCALE_FLUSH_INTVL));
2154
2155 /*
2156 * Check if we should allow search for new modulation mode.
2157 * If many frames have failed or succeeded, or we've used
2158 * this same modulation for a long time, allow search, and
2159 * reset history stats that keep track of whether we should
2160 * allow a new search. Also (below) reset all bitmaps and
2161 * stats in active history.
2162 */
2163 if (force_search ||
2164 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
2165 (lq_sta->total_success > lq_sta->max_success_limit) ||
2166 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
2167 && (flush_interval_passed))) {
2168 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n",
2169 lq_sta->total_failed,
2170 lq_sta->total_success,
2171 flush_interval_passed);
2172
2173 /* Allow search for new mode */
2174 lq_sta->stay_in_tbl = 0; /* only place reset */
2175 lq_sta->total_failed = 0;
2176 lq_sta->total_success = 0;
2177 lq_sta->flush_timer = 0;
2178
2179 /*
2180 * Else if we've used this modulation mode enough repetitions
2181 * (regardless of elapsed time or success/failure), reset
2182 * history bitmaps and rate-specific stats for all rates in
2183 * active table.
2184 */
2185 } else {
2186 lq_sta->table_count++;
2187 if (lq_sta->table_count >=
2188 lq_sta->table_count_limit) {
2189 lq_sta->table_count = 0;
2190
2191 IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
2192 for (i = 0; i < IWL_RATE_COUNT; i++)
2193 rs_rate_scale_clear_window(
2194 &(tbl->win[i]));
2195 }
2196 }
2197
2198 /* If transitioning to allow "search", reset all history
2199 * bitmaps and stats in active table (this will become the new
2200 * "search" table). */
2201 if (!lq_sta->stay_in_tbl) {
2202 for (i = 0; i < IWL_RATE_COUNT; i++)
2203 rs_rate_scale_clear_window(&(tbl->win[i]));
2204 }
2205 }
2206}
2207
2208/*
2209 * setup rate table in uCode
2210 */
2211static void rs_update_rate_tbl(struct iwl_priv *priv,
2212 struct iwl_rxon_context *ctx,
2213 struct iwl_lq_sta *lq_sta,
2214 struct iwl_scale_tbl_info *tbl,
2215 int index, u8 is_green)
2216{
2217 u32 rate;
2218
2219 /* Update uCode's rate table. */
2220 rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2221 rs_fill_link_cmd(priv, lq_sta, rate);
2222 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
2223}
2224
2225/*
2226 * Do rate scaling and search for new modulation mode.
2227 */
2228static void rs_rate_scale_perform(struct iwl_priv *priv,
2229 struct sk_buff *skb,
2230 struct ieee80211_sta *sta,
2231 struct iwl_lq_sta *lq_sta)
2232{
2233 struct ieee80211_hw *hw = priv->hw;
2234 struct ieee80211_conf *conf = &hw->conf;
2235 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2236 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2237 int low = IWL_RATE_INVALID;
2238 int high = IWL_RATE_INVALID;
2239 int index;
2240 int i;
2241 struct iwl_rate_scale_data *window = NULL;
2242 int current_tpt = IWL_INVALID_VALUE;
2243 int low_tpt = IWL_INVALID_VALUE;
2244 int high_tpt = IWL_INVALID_VALUE;
2245 u32 fail_count;
2246 s8 scale_action = 0;
2247 u16 rate_mask;
2248 u8 update_lq = 0;
2249 struct iwl_scale_tbl_info *tbl, *tbl1;
2250 u16 rate_scale_index_msk = 0;
2251 u8 is_green = 0;
2252 u8 active_tbl = 0;
2253 u8 done_search = 0;
2254 u16 high_low;
2255 s32 sr;
2256 u8 tid = IWL_MAX_TID_COUNT;
2257 struct iwl_tid_data *tid_data;
2258 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
2259 struct iwl_rxon_context *ctx = sta_priv->ctx;
2260
2261 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
2262
2263 /* Send management frames and NO_ACK data using lowest rate. */
2264 /* TODO: this could probably be improved.. */
2265 if (!ieee80211_is_data(hdr->frame_control) ||
2266 info->flags & IEEE80211_TX_CTL_NO_ACK)
2267 return;
2268
2269 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2270
2271 tid = rs_tl_add_packet(lq_sta, hdr);
2272 if ((tid != IWL_MAX_TID_COUNT) &&
2273 (lq_sta->tx_agg_tid_en & (1 << tid))) {
2274 tid_data = &priv->tid_data[lq_sta->lq.sta_id][tid];
2275 if (tid_data->agg.state == IWL_AGG_OFF)
2276 lq_sta->is_agg = 0;
2277 else
2278 lq_sta->is_agg = 1;
2279 } else
2280 lq_sta->is_agg = 0;
2281
2282 /*
2283 * Select rate-scale / modulation-mode table to work with in
2284 * the rest of this function: "search" if searching for better
2285 * modulation mode, or "active" if doing rate scaling within a mode.
2286 */
2287 if (!lq_sta->search_better_tbl)
2288 active_tbl = lq_sta->active_tbl;
2289 else
2290 active_tbl = 1 - lq_sta->active_tbl;
2291
2292 tbl = &(lq_sta->lq_info[active_tbl]);
2293 if (is_legacy(tbl->lq_type))
2294 lq_sta->is_green = 0;
2295 else
2296 lq_sta->is_green = rs_use_green(sta);
2297 is_green = lq_sta->is_green;
2298
2299 /* current tx rate */
2300 index = lq_sta->last_txrate_idx;
2301
2302 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
2303 tbl->lq_type);
2304
2305 /* rates available for this association, and for modulation mode */
2306 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2307
2308 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
2309
2310 /* mask with station rate restriction */
2311 if (is_legacy(tbl->lq_type)) {
2312 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2313 /* supp_rates has no CCK bits in A mode */
2314 rate_scale_index_msk = (u16) (rate_mask &
2315 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2316 else
2317 rate_scale_index_msk = (u16) (rate_mask &
2318 lq_sta->supp_rates);
2319
2320 } else
2321 rate_scale_index_msk = rate_mask;
2322
2323 if (!rate_scale_index_msk)
2324 rate_scale_index_msk = rate_mask;
2325
2326 if (!((1 << index) & rate_scale_index_msk)) {
2327 IWL_ERR(priv, "Current Rate is not valid\n");
2328 if (lq_sta->search_better_tbl) {
2329 /* revert to active table if search table is not valid*/
2330 tbl->lq_type = LQ_NONE;
2331 lq_sta->search_better_tbl = 0;
2332 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2333 /* get "active" rate info */
2334 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2335 rs_update_rate_tbl(priv, ctx, lq_sta, tbl,
2336 index, is_green);
2337 }
2338 return;
2339 }
2340
2341 /* Get expected throughput table and history window for current rate */
2342 if (!tbl->expected_tpt) {
2343 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
2344 return;
2345 }
2346
2347 /* force user max rate if set by user */
2348 if ((lq_sta->max_rate_idx != -1) &&
2349 (lq_sta->max_rate_idx < index)) {
2350 index = lq_sta->max_rate_idx;
2351 update_lq = 1;
2352 window = &(tbl->win[index]);
2353 goto lq_update;
2354 }
2355
2356 window = &(tbl->win[index]);
2357
2358 /*
2359 * If there is not enough history to calculate actual average
2360 * throughput, keep analyzing results of more tx frames, without
2361 * changing rate or mode (bypass most of the rest of this function).
2362 * Set up new rate table in uCode only if old rate is not supported
2363 * in current association (use new rate found above).
2364 */
2365 fail_count = window->counter - window->success_counter;
2366 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2367 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2368 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
2369 "for index %d\n",
2370 window->success_counter, window->counter, index);
2371
2372 /* Can't calculate this yet; not enough history */
2373 window->average_tpt = IWL_INVALID_VALUE;
2374
2375 /* Should we stay with this modulation mode,
2376 * or search for a new one? */
2377 rs_stay_in_table(lq_sta, false);
2378
2379 goto out;
2380 }
2381 /* Else we have enough samples; calculate estimate of
2382 * actual average throughput */
2383 if (window->average_tpt != ((window->success_ratio *
2384 tbl->expected_tpt[index] + 64) / 128)) {
2385 IWL_ERR(priv, "expected_tpt should have been calculated by now\n");
2386 window->average_tpt = ((window->success_ratio *
2387 tbl->expected_tpt[index] + 64) / 128);
2388 }
2389
2390 /* If we are searching for better modulation mode, check success. */
2391 if (lq_sta->search_better_tbl &&
2392 (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
2393 /* If good success, continue using the "search" mode;
2394 * no need to send new link quality command, since we're
2395 * continuing to use the setup that we've been trying. */
2396 if (window->average_tpt > lq_sta->last_tpt) {
2397
2398 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
2399 "suc=%d cur-tpt=%d old-tpt=%d\n",
2400 window->success_ratio,
2401 window->average_tpt,
2402 lq_sta->last_tpt);
2403
2404 if (!is_legacy(tbl->lq_type))
2405 lq_sta->enable_counter = 1;
2406
2407 /* Swap tables; "search" becomes "active" */
2408 lq_sta->active_tbl = active_tbl;
2409 current_tpt = window->average_tpt;
2410
2411 /* Else poor success; go back to mode in "active" table */
2412 } else {
2413
2414 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
2415 "suc=%d cur-tpt=%d old-tpt=%d\n",
2416 window->success_ratio,
2417 window->average_tpt,
2418 lq_sta->last_tpt);
2419
2420 /* Nullify "search" table */
2421 tbl->lq_type = LQ_NONE;
2422
2423 /* Revert to "active" table */
2424 active_tbl = lq_sta->active_tbl;
2425 tbl = &(lq_sta->lq_info[active_tbl]);
2426
2427 /* Revert to "active" rate and throughput info */
2428 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2429 current_tpt = lq_sta->last_tpt;
2430
2431 /* Need to set up a new rate table in uCode */
2432 update_lq = 1;
2433 }
2434
2435 /* Either way, we've made a decision; modulation mode
2436 * search is done, allow rate adjustment next time. */
2437 lq_sta->search_better_tbl = 0;
2438 done_search = 1; /* Don't switch modes below! */
2439 goto lq_update;
2440 }
2441
2442 /* (Else) not in search of better modulation mode, try for better
2443 * starting rate, while staying in this mode. */
2444 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
2445 tbl->lq_type);
2446 low = high_low & 0xff;
2447 high = (high_low >> 8) & 0xff;
2448
2449 /* If user set max rate, dont allow higher than user constrain */
2450 if ((lq_sta->max_rate_idx != -1) &&
2451 (lq_sta->max_rate_idx < high))
2452 high = IWL_RATE_INVALID;
2453
2454 sr = window->success_ratio;
2455
2456 /* Collect measured throughputs for current and adjacent rates */
2457 current_tpt = window->average_tpt;
2458 if (low != IWL_RATE_INVALID)
2459 low_tpt = tbl->win[low].average_tpt;
2460 if (high != IWL_RATE_INVALID)
2461 high_tpt = tbl->win[high].average_tpt;
2462
2463 scale_action = 0;
2464
2465 /* Too many failures, decrease rate */
2466 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2467 IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
2468 scale_action = -1;
2469
2470 /* No throughput measured yet for adjacent rates; try increase. */
2471 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2472 (high_tpt == IWL_INVALID_VALUE)) {
2473
2474 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2475 scale_action = 1;
2476 else if (low != IWL_RATE_INVALID)
2477 scale_action = 0;
2478 }
2479
2480 /* Both adjacent throughputs are measured, but neither one has better
2481 * throughput; we're using the best rate, don't change it! */
2482 else if ((low_tpt != IWL_INVALID_VALUE) &&
2483 (high_tpt != IWL_INVALID_VALUE) &&
2484 (low_tpt < current_tpt) &&
2485 (high_tpt < current_tpt))
2486 scale_action = 0;
2487
2488 /* At least one adjacent rate's throughput is measured,
2489 * and may have better performance. */
2490 else {
2491 /* Higher adjacent rate's throughput is measured */
2492 if (high_tpt != IWL_INVALID_VALUE) {
2493 /* Higher rate has better throughput */
2494 if (high_tpt > current_tpt &&
2495 sr >= IWL_RATE_INCREASE_TH) {
2496 scale_action = 1;
2497 } else {
2498 scale_action = 0;
2499 }
2500
2501 /* Lower adjacent rate's throughput is measured */
2502 } else if (low_tpt != IWL_INVALID_VALUE) {
2503 /* Lower rate has better throughput */
2504 if (low_tpt > current_tpt) {
2505 IWL_DEBUG_RATE(priv,
2506 "decrease rate because of low tpt\n");
2507 scale_action = -1;
2508 } else if (sr >= IWL_RATE_INCREASE_TH) {
2509 scale_action = 1;
2510 }
2511 }
2512 }
2513
2514 /* Sanity check; asked for decrease, but success rate or throughput
2515 * has been good at old rate. Don't change it. */
2516 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2517 ((sr > IWL_RATE_HIGH_TH) ||
2518 (current_tpt > (100 * tbl->expected_tpt[low]))))
2519 scale_action = 0;
2520 if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
2521 scale_action = -1;
2522 if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
2523 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
2524 scale_action = -1;
2525
2526 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2527 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2528 if (lq_sta->last_bt_traffic > priv->bt_traffic_load) {
2529 /*
2530 * don't set scale_action, don't want to scale up if
2531 * the rate scale doesn't otherwise think that is a
2532 * good idea.
2533 */
2534 } else if (lq_sta->last_bt_traffic <= priv->bt_traffic_load) {
2535 scale_action = -1;
2536 }
2537 }
2538 lq_sta->last_bt_traffic = priv->bt_traffic_load;
2539
2540 if ((priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
2541 (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
2542 /* search for a new modulation */
2543 rs_stay_in_table(lq_sta, true);
2544 goto lq_update;
2545 }
2546
2547 switch (scale_action) {
2548 case -1:
2549 /* Decrease starting rate, update uCode's rate table */
2550 if (low != IWL_RATE_INVALID) {
2551 update_lq = 1;
2552 index = low;
2553 }
2554
2555 break;
2556 case 1:
2557 /* Increase starting rate, update uCode's rate table */
2558 if (high != IWL_RATE_INVALID) {
2559 update_lq = 1;
2560 index = high;
2561 }
2562
2563 break;
2564 case 0:
2565 /* No change */
2566 default:
2567 break;
2568 }
2569
2570 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2571 "high %d type %d\n",
2572 index, scale_action, low, high, tbl->lq_type);
2573
2574lq_update:
2575 /* Replace uCode's rate table for the destination station. */
2576 if (update_lq)
2577 rs_update_rate_tbl(priv, ctx, lq_sta, tbl, index, is_green);
2578
2579 if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
2580 /* Should we stay with this modulation mode,
2581 * or search for a new one? */
2582 rs_stay_in_table(lq_sta, false);
2583 }
2584 /*
2585 * Search for new modulation mode if we're:
2586 * 1) Not changing rates right now
2587 * 2) Not just finishing up a search
2588 * 3) Allowing a new search
2589 */
2590 if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
2591 /* Save current throughput to compare with "search" throughput*/
2592 lq_sta->last_tpt = current_tpt;
2593
2594 /* Select a new "search" modulation mode to try.
2595 * If one is found, set up the new "search" table. */
2596 if (is_legacy(tbl->lq_type))
2597 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
2598 else if (is_siso(tbl->lq_type))
2599 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
2600 else if (is_mimo2(tbl->lq_type))
2601 rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
2602 else
2603 rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
2604
2605 /* If new "search" mode was selected, set up in uCode table */
2606 if (lq_sta->search_better_tbl) {
2607 /* Access the "search" table, clear its history. */
2608 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2609 for (i = 0; i < IWL_RATE_COUNT; i++)
2610 rs_rate_scale_clear_window(&(tbl->win[i]));
2611
2612 /* Use new "search" start rate */
2613 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2614
2615 IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
2616 tbl->current_rate, index);
2617 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2618 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
2619 } else
2620 done_search = 1;
2621 }
2622
2623 if (done_search && !lq_sta->stay_in_tbl) {
2624 /* If the "active" (non-search) mode was legacy,
2625 * and we've tried switching antennas,
2626 * but we haven't been able to try HT modes (not available),
2627 * stay with best antenna legacy modulation for a while
2628 * before next round of mode comparisons. */
2629 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2630 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2631 lq_sta->action_counter > tbl1->max_search) {
2632 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2633 rs_set_stay_in_table(priv, 1, lq_sta);
2634 }
2635
2636 /* If we're in an HT mode, and all 3 mode switch actions
2637 * have been tried and compared, stay in this best modulation
2638 * mode for a while before next round of mode comparisons. */
2639 if (lq_sta->enable_counter &&
2640 (lq_sta->action_counter >= tbl1->max_search) &&
2641 iwl_ht_enabled(priv)) {
2642 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2643 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2644 (tid != IWL_MAX_TID_COUNT)) {
2645 u8 sta_id = lq_sta->lq.sta_id;
2646 tid_data = &priv->tid_data[sta_id][tid];
2647 if (tid_data->agg.state == IWL_AGG_OFF) {
2648 IWL_DEBUG_RATE(priv,
2649 "try to aggregate tid %d\n",
2650 tid);
2651 rs_tl_turn_on_agg(priv, tid,
2652 lq_sta, sta);
2653 }
2654 }
2655 rs_set_stay_in_table(priv, 0, lq_sta);
2656 }
2657 }
2658
2659out:
2660 tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
2661 lq_sta->last_txrate_idx = index;
2662}
2663
2664/**
2665 * rs_initialize_lq - Initialize a station's hardware rate table
2666 *
2667 * The uCode's station table contains a table of fallback rates
2668 * for automatic fallback during transmission.
2669 *
2670 * NOTE: This sets up a default set of values. These will be replaced later
2671 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2672 * rc80211_simple.
2673 *
2674 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2675 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2676 * which requires station table entry to exist).
2677 */
2678static void rs_initialize_lq(struct iwl_priv *priv,
2679 struct ieee80211_sta *sta,
2680 struct iwl_lq_sta *lq_sta)
2681{
2682 struct iwl_scale_tbl_info *tbl;
2683 int rate_idx;
2684 int i;
2685 u32 rate;
2686 u8 use_green = rs_use_green(sta);
2687 u8 active_tbl = 0;
2688 u8 valid_tx_ant;
2689 struct iwl_station_priv *sta_priv;
2690 struct iwl_rxon_context *ctx;
2691
2692 if (!sta || !lq_sta)
2693 return;
2694
2695 sta_priv = (void *)sta->drv_priv;
2696 ctx = sta_priv->ctx;
2697
2698 i = lq_sta->last_txrate_idx;
2699
2700 valid_tx_ant = priv->hw_params.valid_tx_ant;
2701
2702 if (!lq_sta->search_better_tbl)
2703 active_tbl = lq_sta->active_tbl;
2704 else
2705 active_tbl = 1 - lq_sta->active_tbl;
2706
2707 tbl = &(lq_sta->lq_info[active_tbl]);
2708
2709 if ((i < 0) || (i >= IWL_RATE_COUNT))
2710 i = 0;
2711
2712 rate = iwl_rates[i].plcp;
2713 tbl->ant_type = first_antenna(valid_tx_ant);
2714 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2715
2716 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2717 rate |= RATE_MCS_CCK_MSK;
2718
2719 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2720 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2721 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2722
2723 rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2724 tbl->current_rate = rate;
2725 rs_set_expected_tpt_table(lq_sta, tbl);
2726 rs_fill_link_cmd(NULL, lq_sta, rate);
2727 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
2728 iwl_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
2729}
2730
2731static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2732 struct ieee80211_tx_rate_control *txrc)
2733{
2734
2735 struct sk_buff *skb = txrc->skb;
2736 struct ieee80211_supported_band *sband = txrc->sband;
2737 struct iwl_op_mode *op_mode __maybe_unused =
2738 (struct iwl_op_mode *)priv_r;
2739 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
2740 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2741 struct iwl_lq_sta *lq_sta = priv_sta;
2742 int rate_idx;
2743
2744 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2745
2746 /* Get max rate if user set max rate */
2747 if (lq_sta) {
2748 lq_sta->max_rate_idx = txrc->max_rate_idx;
2749 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2750 (lq_sta->max_rate_idx != -1))
2751 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2752 if ((lq_sta->max_rate_idx < 0) ||
2753 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2754 lq_sta->max_rate_idx = -1;
2755 }
2756
2757 /* Treat uninitialized rate scaling data same as non-existing. */
2758 if (lq_sta && !lq_sta->drv) {
2759 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2760 priv_sta = NULL;
2761 }
2762
2763 /* Send management frames and NO_ACK data using lowest rate. */
2764 if (rate_control_send_low(sta, priv_sta, txrc))
2765 return;
2766
2767 rate_idx = lq_sta->last_txrate_idx;
2768
2769 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2770 rate_idx -= IWL_FIRST_OFDM_RATE;
2771 /* 6M and 9M shared same MCS index */
2772 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2773 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2774 IWL_RATE_MIMO3_6M_PLCP)
2775 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2776 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2777 IWL_RATE_MIMO2_6M_PLCP)
2778 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2779 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2780 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2781 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2782 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2783 info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
2784 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2785 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2786 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2787 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2788 } else {
2789 /* Check for invalid rates */
2790 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2791 ((sband->band == IEEE80211_BAND_5GHZ) &&
2792 (rate_idx < IWL_FIRST_OFDM_RATE)))
2793 rate_idx = rate_lowest_index(sband, sta);
2794 /* On valid 5 GHz rate, adjust index */
2795 else if (sband->band == IEEE80211_BAND_5GHZ)
2796 rate_idx -= IWL_FIRST_OFDM_RATE;
2797 info->control.rates[0].flags = 0;
2798 }
2799 info->control.rates[0].idx = rate_idx;
2800
2801}
2802
2803static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2804 gfp_t gfp)
2805{
2806 struct iwl_station_priv *sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2807 struct iwl_op_mode *op_mode __maybe_unused =
2808 (struct iwl_op_mode *)priv_rate;
2809 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
2810
2811 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2812
2813 return &sta_priv->lq_sta;
2814}
2815
2816/*
2817 * Called after adding a new station to initialize rate scaling
2818 */
2819void iwl_rs_rate_init(struct iwl_priv *priv, struct ieee80211_sta *sta, u8 sta_id)
2820{
2821 int i, j;
2822 struct ieee80211_hw *hw = priv->hw;
2823 struct ieee80211_conf *conf = &priv->hw->conf;
2824 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2825 struct iwl_station_priv *sta_priv;
2826 struct iwl_lq_sta *lq_sta;
2827 struct ieee80211_supported_band *sband;
2828 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2829
2830 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2831 lq_sta = &sta_priv->lq_sta;
2832 sband = hw->wiphy->bands[conf->channel->band];
2833
2834
2835 lq_sta->lq.sta_id = sta_id;
2836
2837 for (j = 0; j < LQ_SIZE; j++)
2838 for (i = 0; i < IWL_RATE_COUNT; i++)
2839 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2840
2841 lq_sta->flush_timer = 0;
2842 lq_sta->supp_rates = sta->supp_rates[sband->band];
2843 for (j = 0; j < LQ_SIZE; j++)
2844 for (i = 0; i < IWL_RATE_COUNT; i++)
2845 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2846
2847 IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init for station %d ***\n",
2848 sta_id);
2849 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2850 * the lowest or the highest rate.. Could consider using RSSI from
2851 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2852 * after assoc.. */
2853
2854 lq_sta->is_dup = 0;
2855 lq_sta->max_rate_idx = -1;
2856 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2857 lq_sta->is_green = rs_use_green(sta);
2858 lq_sta->band = sband->band;
2859 /*
2860 * active legacy rates as per supported rates bitmap
2861 */
2862 supp = sta->supp_rates[sband->band];
2863 lq_sta->active_legacy_rate = 0;
2864 for_each_set_bit(i, &supp, BITS_PER_LONG)
2865 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2866
2867 /*
2868 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2869 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2870 */
2871 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2872 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2873 lq_sta->active_siso_rate &= ~((u16)0x2);
2874 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2875
2876 /* Same here */
2877 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2878 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2879 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2880 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2881
2882 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2883 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
2884 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2885 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2886
2887 IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2888 lq_sta->active_siso_rate,
2889 lq_sta->active_mimo2_rate,
2890 lq_sta->active_mimo3_rate);
2891
2892 /* These values will be overridden later */
2893 lq_sta->lq.general_params.single_stream_ant_msk =
2894 first_antenna(priv->hw_params.valid_tx_ant);
2895 lq_sta->lq.general_params.dual_stream_ant_msk =
2896 priv->hw_params.valid_tx_ant &
2897 ~first_antenna(priv->hw_params.valid_tx_ant);
2898 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2899 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2900 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
2901 lq_sta->lq.general_params.dual_stream_ant_msk =
2902 priv->hw_params.valid_tx_ant;
2903 }
2904
2905 /* as default allow aggregation for all tids */
2906 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2907 lq_sta->drv = priv;
2908
2909 /* Set last_txrate_idx to lowest rate */
2910 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2911 if (sband->band == IEEE80211_BAND_5GHZ)
2912 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2913 lq_sta->is_agg = 0;
2914#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
2915 priv->tm_fixed_rate = 0;
2916#endif
2917#ifdef CONFIG_MAC80211_DEBUGFS
2918 lq_sta->dbg_fixed_rate = 0;
2919#endif
2920
2921 rs_initialize_lq(priv, sta, lq_sta);
2922}
2923
2924static void rs_fill_link_cmd(struct iwl_priv *priv,
2925 struct iwl_lq_sta *lq_sta, u32 new_rate)
2926{
2927 struct iwl_scale_tbl_info tbl_type;
2928 int index = 0;
2929 int rate_idx;
2930 int repeat_rate = 0;
2931 u8 ant_toggle_cnt = 0;
2932 u8 use_ht_possible = 1;
2933 u8 valid_tx_ant = 0;
2934 struct iwl_station_priv *sta_priv =
2935 container_of(lq_sta, struct iwl_station_priv, lq_sta);
2936 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2937
2938 /* Override starting rate (index 0) if needed for debug purposes */
2939 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2940
2941 /* Interpret new_rate (rate_n_flags) */
2942 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2943 &tbl_type, &rate_idx);
2944
2945 if (priv && priv->bt_full_concurrent) {
2946 /* 1x1 only */
2947 tbl_type.ant_type =
2948 first_antenna(priv->hw_params.valid_tx_ant);
2949 }
2950
2951 /* How many times should we repeat the initial rate? */
2952 if (is_legacy(tbl_type.lq_type)) {
2953 ant_toggle_cnt = 1;
2954 repeat_rate = IWL_NUMBER_TRY;
2955 } else {
2956 repeat_rate = min(IWL_HT_NUMBER_TRY,
2957 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2958 }
2959
2960 lq_cmd->general_params.mimo_delimiter =
2961 is_mimo(tbl_type.lq_type) ? 1 : 0;
2962
2963 /* Fill 1st table entry (index 0) */
2964 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2965
2966 if (num_of_ant(tbl_type.ant_type) == 1) {
2967 lq_cmd->general_params.single_stream_ant_msk =
2968 tbl_type.ant_type;
2969 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2970 lq_cmd->general_params.dual_stream_ant_msk =
2971 tbl_type.ant_type;
2972 } /* otherwise we don't modify the existing value */
2973
2974 index++;
2975 repeat_rate--;
2976 if (priv) {
2977 if (priv->bt_full_concurrent)
2978 valid_tx_ant = ANT_A;
2979 else
2980 valid_tx_ant = priv->hw_params.valid_tx_ant;
2981 }
2982
2983 /* Fill rest of rate table */
2984 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2985 /* Repeat initial/next rate.
2986 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2987 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2988 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2989 if (is_legacy(tbl_type.lq_type)) {
2990 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2991 ant_toggle_cnt++;
2992 else if (priv &&
2993 rs_toggle_antenna(valid_tx_ant,
2994 &new_rate, &tbl_type))
2995 ant_toggle_cnt = 1;
2996 }
2997
2998 /* Override next rate if needed for debug purposes */
2999 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
3000
3001 /* Fill next table entry */
3002 lq_cmd->rs_table[index].rate_n_flags =
3003 cpu_to_le32(new_rate);
3004 repeat_rate--;
3005 index++;
3006 }
3007
3008 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
3009 &rate_idx);
3010
3011 if (priv && priv->bt_full_concurrent) {
3012 /* 1x1 only */
3013 tbl_type.ant_type =
3014 first_antenna(priv->hw_params.valid_tx_ant);
3015 }
3016
3017 /* Indicate to uCode which entries might be MIMO.
3018 * If initial rate was MIMO, this will finally end up
3019 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
3020 if (is_mimo(tbl_type.lq_type))
3021 lq_cmd->general_params.mimo_delimiter = index;
3022
3023 /* Get next rate */
3024 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
3025 use_ht_possible);
3026
3027 /* How many times should we repeat the next rate? */
3028 if (is_legacy(tbl_type.lq_type)) {
3029 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
3030 ant_toggle_cnt++;
3031 else if (priv &&
3032 rs_toggle_antenna(valid_tx_ant,
3033 &new_rate, &tbl_type))
3034 ant_toggle_cnt = 1;
3035
3036 repeat_rate = IWL_NUMBER_TRY;
3037 } else {
3038 repeat_rate = IWL_HT_NUMBER_TRY;
3039 }
3040
3041 /* Don't allow HT rates after next pass.
3042 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
3043 use_ht_possible = 0;
3044
3045 /* Override next rate if needed for debug purposes */
3046 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
3047
3048 /* Fill next table entry */
3049 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
3050
3051 index++;
3052 repeat_rate--;
3053 }
3054
3055 lq_cmd->agg_params.agg_frame_cnt_limit =
3056 sta_priv->max_agg_bufsize ?: LINK_QUAL_AGG_FRAME_LIMIT_DEF;
3057 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
3058
3059 lq_cmd->agg_params.agg_time_limit =
3060 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
3061 /*
3062 * overwrite if needed, pass aggregation time limit
3063 * to uCode in uSec
3064 */
3065 if (priv && priv->cfg->bt_params &&
3066 priv->cfg->bt_params->agg_time_limit &&
3067 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
3068 lq_cmd->agg_params.agg_time_limit =
3069 cpu_to_le16(priv->cfg->bt_params->agg_time_limit);
3070}
3071
3072static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
3073{
3074 return hw->priv;
3075}
3076/* rate scale requires free function to be implemented */
3077static void rs_free(void *priv_rate)
3078{
3079 return;
3080}
3081
3082static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
3083 void *priv_sta)
3084{
3085 struct iwl_op_mode *op_mode __maybe_unused = priv_r;
3086 struct iwl_priv *priv __maybe_unused = IWL_OP_MODE_GET_DVM(op_mode);
3087
3088 IWL_DEBUG_RATE(priv, "enter\n");
3089 IWL_DEBUG_RATE(priv, "leave\n");
3090}
3091
3092#ifdef CONFIG_MAC80211_DEBUGFS
3093static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
3094 u32 *rate_n_flags, int index)
3095{
3096 struct iwl_priv *priv;
3097 u8 valid_tx_ant;
3098 u8 ant_sel_tx;
3099
3100 priv = lq_sta->drv;
3101 valid_tx_ant = priv->hw_params.valid_tx_ant;
3102 if (lq_sta->dbg_fixed_rate) {
3103 ant_sel_tx =
3104 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
3105 >> RATE_MCS_ANT_POS);
3106 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
3107 *rate_n_flags = lq_sta->dbg_fixed_rate;
3108 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
3109 } else {
3110 lq_sta->dbg_fixed_rate = 0;
3111 IWL_ERR(priv,
3112 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
3113 ant_sel_tx, valid_tx_ant);
3114 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
3115 }
3116 } else {
3117 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
3118 }
3119}
3120
3121static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
3122 const char __user *user_buf, size_t count, loff_t *ppos)
3123{
3124 struct iwl_lq_sta *lq_sta = file->private_data;
3125 struct iwl_priv *priv;
3126 char buf[64];
3127 size_t buf_size;
3128 u32 parsed_rate;
3129
3130
3131 priv = lq_sta->drv;
3132 memset(buf, 0, sizeof(buf));
3133 buf_size = min(count, sizeof(buf) - 1);
3134 if (copy_from_user(buf, user_buf, buf_size))
3135 return -EFAULT;
3136
3137 if (sscanf(buf, "%x", &parsed_rate) == 1)
3138 lq_sta->dbg_fixed_rate = parsed_rate;
3139 else
3140 lq_sta->dbg_fixed_rate = 0;
3141
3142 rs_program_fix_rate(priv, lq_sta);
3143
3144 return count;
3145}
3146
3147static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
3148 char __user *user_buf, size_t count, loff_t *ppos)
3149{
3150 char *buff;
3151 int desc = 0;
3152 int i = 0;
3153 int index = 0;
3154 ssize_t ret;
3155
3156 struct iwl_lq_sta *lq_sta = file->private_data;
3157 struct iwl_priv *priv;
3158 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
3159
3160 priv = lq_sta->drv;
3161 buff = kmalloc(1024, GFP_KERNEL);
3162 if (!buff)
3163 return -ENOMEM;
3164
3165 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
3166 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
3167 lq_sta->total_failed, lq_sta->total_success,
3168 lq_sta->active_legacy_rate);
3169 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
3170 lq_sta->dbg_fixed_rate);
3171 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
3172 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
3173 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
3174 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
3175 desc += sprintf(buff+desc, "lq type %s\n",
3176 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
3177 if (is_Ht(tbl->lq_type)) {
3178 desc += sprintf(buff+desc, " %s",
3179 (is_siso(tbl->lq_type)) ? "SISO" :
3180 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
3181 desc += sprintf(buff+desc, " %s",
3182 (tbl->is_ht40) ? "40MHz" : "20MHz");
3183 desc += sprintf(buff+desc, " %s %s %s\n", (tbl->is_SGI) ? "SGI" : "",
3184 (lq_sta->is_green) ? "GF enabled" : "",
3185 (lq_sta->is_agg) ? "AGG on" : "");
3186 }
3187 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
3188 lq_sta->last_rate_n_flags);
3189 desc += sprintf(buff+desc, "general:"
3190 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
3191 lq_sta->lq.general_params.flags,
3192 lq_sta->lq.general_params.mimo_delimiter,
3193 lq_sta->lq.general_params.single_stream_ant_msk,
3194 lq_sta->lq.general_params.dual_stream_ant_msk);
3195
3196 desc += sprintf(buff+desc, "agg:"
3197 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
3198 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
3199 lq_sta->lq.agg_params.agg_dis_start_th,
3200 lq_sta->lq.agg_params.agg_frame_cnt_limit);
3201
3202 desc += sprintf(buff+desc,
3203 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
3204 lq_sta->lq.general_params.start_rate_index[0],
3205 lq_sta->lq.general_params.start_rate_index[1],
3206 lq_sta->lq.general_params.start_rate_index[2],
3207 lq_sta->lq.general_params.start_rate_index[3]);
3208
3209 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3210 index = iwl_hwrate_to_plcp_idx(
3211 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
3212 if (is_legacy(tbl->lq_type)) {
3213 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
3214 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3215 iwl_rate_mcs[index].mbps);
3216 } else {
3217 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
3218 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
3219 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
3220 }
3221 }
3222
3223 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3224 kfree(buff);
3225 return ret;
3226}
3227
3228static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
3229 .write = rs_sta_dbgfs_scale_table_write,
3230 .read = rs_sta_dbgfs_scale_table_read,
3231 .open = simple_open,
3232 .llseek = default_llseek,
3233};
3234static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
3235 char __user *user_buf, size_t count, loff_t *ppos)
3236{
3237 char *buff;
3238 int desc = 0;
3239 int i, j;
3240 ssize_t ret;
3241
3242 struct iwl_lq_sta *lq_sta = file->private_data;
3243
3244 buff = kmalloc(1024, GFP_KERNEL);
3245 if (!buff)
3246 return -ENOMEM;
3247
3248 for (i = 0; i < LQ_SIZE; i++) {
3249 desc += sprintf(buff+desc,
3250 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
3251 "rate=0x%X\n",
3252 lq_sta->active_tbl == i ? "*" : "x",
3253 lq_sta->lq_info[i].lq_type,
3254 lq_sta->lq_info[i].is_SGI,
3255 lq_sta->lq_info[i].is_ht40,
3256 lq_sta->lq_info[i].is_dup,
3257 lq_sta->is_green,
3258 lq_sta->lq_info[i].current_rate);
3259 for (j = 0; j < IWL_RATE_COUNT; j++) {
3260 desc += sprintf(buff+desc,
3261 "counter=%d success=%d %%=%d\n",
3262 lq_sta->lq_info[i].win[j].counter,
3263 lq_sta->lq_info[i].win[j].success_counter,
3264 lq_sta->lq_info[i].win[j].success_ratio);
3265 }
3266 }
3267 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3268 kfree(buff);
3269 return ret;
3270}
3271
3272static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
3273 .read = rs_sta_dbgfs_stats_table_read,
3274 .open = simple_open,
3275 .llseek = default_llseek,
3276};
3277
3278static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
3279 char __user *user_buf, size_t count, loff_t *ppos)
3280{
3281 struct iwl_lq_sta *lq_sta = file->private_data;
3282 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3283 char buff[120];
3284 int desc = 0;
3285
3286 if (is_Ht(tbl->lq_type))
3287 desc += sprintf(buff+desc,
3288 "Bit Rate= %d Mb/s\n",
3289 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3290 else
3291 desc += sprintf(buff+desc,
3292 "Bit Rate= %d Mb/s\n",
3293 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3294
3295 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3296}
3297
3298static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3299 .read = rs_sta_dbgfs_rate_scale_data_read,
3300 .open = simple_open,
3301 .llseek = default_llseek,
3302};
3303
3304static void rs_add_debugfs(void *priv, void *priv_sta,
3305 struct dentry *dir)
3306{
3307 struct iwl_lq_sta *lq_sta = priv_sta;
3308 lq_sta->rs_sta_dbgfs_scale_table_file =
3309 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
3310 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3311 lq_sta->rs_sta_dbgfs_stats_table_file =
3312 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
3313 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3314 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3315 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
3316 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3317 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3318 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
3319 &lq_sta->tx_agg_tid_en);
3320
3321}
3322
3323static void rs_remove_debugfs(void *priv, void *priv_sta)
3324{
3325 struct iwl_lq_sta *lq_sta = priv_sta;
3326 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3327 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3328 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3329 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3330}
3331#endif
3332
3333/*
3334 * Initialization of rate scaling information is done by driver after
3335 * the station is added. Since mac80211 calls this function before a
3336 * station is added we ignore it.
3337 */
3338static void rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
3339 struct ieee80211_sta *sta, void *priv_sta)
3340{
3341}
3342static struct rate_control_ops rs_ops = {
3343 .module = NULL,
3344 .name = RS_NAME,
3345 .tx_status = rs_tx_status,
3346 .get_rate = rs_get_rate,
3347 .rate_init = rs_rate_init_stub,
3348 .alloc = rs_alloc,
3349 .free = rs_free,
3350 .alloc_sta = rs_alloc_sta,
3351 .free_sta = rs_free_sta,
3352#ifdef CONFIG_MAC80211_DEBUGFS
3353 .add_sta_debugfs = rs_add_debugfs,
3354 .remove_sta_debugfs = rs_remove_debugfs,
3355#endif
3356};
3357
3358int iwlagn_rate_control_register(void)
3359{
3360 return ieee80211_rate_control_register(&rs_ops);
3361}
3362
3363void iwlagn_rate_control_unregister(void)
3364{
3365 ieee80211_rate_control_unregister(&rs_ops);
3366}
3367
diff --git a/drivers/net/wireless/iwlwifi/dvm/rs.h b/drivers/net/wireless/iwlwifi/dvm/rs.h
new file mode 100644
index 000000000000..ad3aea8f626a
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/rs.h
@@ -0,0 +1,433 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26
27#ifndef __iwl_agn_rs_h__
28#define __iwl_agn_rs_h__
29
30#include <net/mac80211.h>
31
32#include "iwl-config.h"
33
34#include "commands.h"
35
36struct iwl_rate_info {
37 u8 plcp; /* uCode API: IWL_RATE_6M_PLCP, etc. */
38 u8 plcp_siso; /* uCode API: IWL_RATE_SISO_6M_PLCP, etc. */
39 u8 plcp_mimo2; /* uCode API: IWL_RATE_MIMO2_6M_PLCP, etc. */
40 u8 plcp_mimo3; /* uCode API: IWL_RATE_MIMO3_6M_PLCP, etc. */
41 u8 ieee; /* MAC header: IWL_RATE_6M_IEEE, etc. */
42 u8 prev_ieee; /* previous rate in IEEE speeds */
43 u8 next_ieee; /* next rate in IEEE speeds */
44 u8 prev_rs; /* previous rate used in rs algo */
45 u8 next_rs; /* next rate used in rs algo */
46 u8 prev_rs_tgg; /* previous rate used in TGG rs algo */
47 u8 next_rs_tgg; /* next rate used in TGG rs algo */
48};
49
50/*
51 * These serve as indexes into
52 * struct iwl_rate_info iwl_rates[IWL_RATE_COUNT];
53 */
54enum {
55 IWL_RATE_1M_INDEX = 0,
56 IWL_RATE_2M_INDEX,
57 IWL_RATE_5M_INDEX,
58 IWL_RATE_11M_INDEX,
59 IWL_RATE_6M_INDEX,
60 IWL_RATE_9M_INDEX,
61 IWL_RATE_12M_INDEX,
62 IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX,
64 IWL_RATE_36M_INDEX,
65 IWL_RATE_48M_INDEX,
66 IWL_RATE_54M_INDEX,
67 IWL_RATE_60M_INDEX,
68 IWL_RATE_COUNT, /*FIXME:RS:change to IWL_RATE_INDEX_COUNT,*/
69 IWL_RATE_COUNT_LEGACY = IWL_RATE_COUNT - 1, /* Excluding 60M */
70 IWL_RATE_INVM_INDEX = IWL_RATE_COUNT,
71 IWL_RATE_INVALID = IWL_RATE_COUNT,
72};
73
74enum {
75 IWL_RATE_6M_INDEX_TABLE = 0,
76 IWL_RATE_9M_INDEX_TABLE,
77 IWL_RATE_12M_INDEX_TABLE,
78 IWL_RATE_18M_INDEX_TABLE,
79 IWL_RATE_24M_INDEX_TABLE,
80 IWL_RATE_36M_INDEX_TABLE,
81 IWL_RATE_48M_INDEX_TABLE,
82 IWL_RATE_54M_INDEX_TABLE,
83 IWL_RATE_1M_INDEX_TABLE,
84 IWL_RATE_2M_INDEX_TABLE,
85 IWL_RATE_5M_INDEX_TABLE,
86 IWL_RATE_11M_INDEX_TABLE,
87 IWL_RATE_INVM_INDEX_TABLE = IWL_RATE_INVM_INDEX - 1,
88};
89
90enum {
91 IWL_FIRST_OFDM_RATE = IWL_RATE_6M_INDEX,
92 IWL_LAST_OFDM_RATE = IWL_RATE_60M_INDEX,
93 IWL_FIRST_CCK_RATE = IWL_RATE_1M_INDEX,
94 IWL_LAST_CCK_RATE = IWL_RATE_11M_INDEX,
95};
96
97/* #define vs. enum to keep from defaulting to 'large integer' */
98#define IWL_RATE_6M_MASK (1 << IWL_RATE_6M_INDEX)
99#define IWL_RATE_9M_MASK (1 << IWL_RATE_9M_INDEX)
100#define IWL_RATE_12M_MASK (1 << IWL_RATE_12M_INDEX)
101#define IWL_RATE_18M_MASK (1 << IWL_RATE_18M_INDEX)
102#define IWL_RATE_24M_MASK (1 << IWL_RATE_24M_INDEX)
103#define IWL_RATE_36M_MASK (1 << IWL_RATE_36M_INDEX)
104#define IWL_RATE_48M_MASK (1 << IWL_RATE_48M_INDEX)
105#define IWL_RATE_54M_MASK (1 << IWL_RATE_54M_INDEX)
106#define IWL_RATE_60M_MASK (1 << IWL_RATE_60M_INDEX)
107#define IWL_RATE_1M_MASK (1 << IWL_RATE_1M_INDEX)
108#define IWL_RATE_2M_MASK (1 << IWL_RATE_2M_INDEX)
109#define IWL_RATE_5M_MASK (1 << IWL_RATE_5M_INDEX)
110#define IWL_RATE_11M_MASK (1 << IWL_RATE_11M_INDEX)
111
112/* uCode API values for legacy bit rates, both OFDM and CCK */
113enum {
114 IWL_RATE_6M_PLCP = 13,
115 IWL_RATE_9M_PLCP = 15,
116 IWL_RATE_12M_PLCP = 5,
117 IWL_RATE_18M_PLCP = 7,
118 IWL_RATE_24M_PLCP = 9,
119 IWL_RATE_36M_PLCP = 11,
120 IWL_RATE_48M_PLCP = 1,
121 IWL_RATE_54M_PLCP = 3,
122 IWL_RATE_60M_PLCP = 3,/*FIXME:RS:should be removed*/
123 IWL_RATE_1M_PLCP = 10,
124 IWL_RATE_2M_PLCP = 20,
125 IWL_RATE_5M_PLCP = 55,
126 IWL_RATE_11M_PLCP = 110,
127 /*FIXME:RS:change to IWL_RATE_LEGACY_??M_PLCP */
128 /*FIXME:RS:add IWL_RATE_LEGACY_INVM_PLCP = 0,*/
129};
130
131/* uCode API values for OFDM high-throughput (HT) bit rates */
132enum {
133 IWL_RATE_SISO_6M_PLCP = 0,
134 IWL_RATE_SISO_12M_PLCP = 1,
135 IWL_RATE_SISO_18M_PLCP = 2,
136 IWL_RATE_SISO_24M_PLCP = 3,
137 IWL_RATE_SISO_36M_PLCP = 4,
138 IWL_RATE_SISO_48M_PLCP = 5,
139 IWL_RATE_SISO_54M_PLCP = 6,
140 IWL_RATE_SISO_60M_PLCP = 7,
141 IWL_RATE_MIMO2_6M_PLCP = 0x8,
142 IWL_RATE_MIMO2_12M_PLCP = 0x9,
143 IWL_RATE_MIMO2_18M_PLCP = 0xa,
144 IWL_RATE_MIMO2_24M_PLCP = 0xb,
145 IWL_RATE_MIMO2_36M_PLCP = 0xc,
146 IWL_RATE_MIMO2_48M_PLCP = 0xd,
147 IWL_RATE_MIMO2_54M_PLCP = 0xe,
148 IWL_RATE_MIMO2_60M_PLCP = 0xf,
149 IWL_RATE_MIMO3_6M_PLCP = 0x10,
150 IWL_RATE_MIMO3_12M_PLCP = 0x11,
151 IWL_RATE_MIMO3_18M_PLCP = 0x12,
152 IWL_RATE_MIMO3_24M_PLCP = 0x13,
153 IWL_RATE_MIMO3_36M_PLCP = 0x14,
154 IWL_RATE_MIMO3_48M_PLCP = 0x15,
155 IWL_RATE_MIMO3_54M_PLCP = 0x16,
156 IWL_RATE_MIMO3_60M_PLCP = 0x17,
157 IWL_RATE_SISO_INVM_PLCP,
158 IWL_RATE_MIMO2_INVM_PLCP = IWL_RATE_SISO_INVM_PLCP,
159 IWL_RATE_MIMO3_INVM_PLCP = IWL_RATE_SISO_INVM_PLCP,
160};
161
162/* MAC header values for bit rates */
163enum {
164 IWL_RATE_6M_IEEE = 12,
165 IWL_RATE_9M_IEEE = 18,
166 IWL_RATE_12M_IEEE = 24,
167 IWL_RATE_18M_IEEE = 36,
168 IWL_RATE_24M_IEEE = 48,
169 IWL_RATE_36M_IEEE = 72,
170 IWL_RATE_48M_IEEE = 96,
171 IWL_RATE_54M_IEEE = 108,
172 IWL_RATE_60M_IEEE = 120,
173 IWL_RATE_1M_IEEE = 2,
174 IWL_RATE_2M_IEEE = 4,
175 IWL_RATE_5M_IEEE = 11,
176 IWL_RATE_11M_IEEE = 22,
177};
178
179#define IWL_RATES_MASK ((1 << IWL_RATE_COUNT) - 1)
180
181#define IWL_INVALID_VALUE -1
182
183#define IWL_MIN_RSSI_VAL -100
184#define IWL_MAX_RSSI_VAL 0
185
186/* These values specify how many Tx frame attempts before
187 * searching for a new modulation mode */
188#define IWL_LEGACY_FAILURE_LIMIT 160
189#define IWL_LEGACY_SUCCESS_LIMIT 480
190#define IWL_LEGACY_TABLE_COUNT 160
191
192#define IWL_NONE_LEGACY_FAILURE_LIMIT 400
193#define IWL_NONE_LEGACY_SUCCESS_LIMIT 4500
194#define IWL_NONE_LEGACY_TABLE_COUNT 1500
195
196/* Success ratio (ACKed / attempted tx frames) values (perfect is 128 * 100) */
197#define IWL_RS_GOOD_RATIO 12800 /* 100% */
198#define IWL_RATE_SCALE_SWITCH 10880 /* 85% */
199#define IWL_RATE_HIGH_TH 10880 /* 85% */
200#define IWL_RATE_INCREASE_TH 6400 /* 50% */
201#define IWL_RATE_DECREASE_TH 1920 /* 15% */
202
203/* possible actions when in legacy mode */
204#define IWL_LEGACY_SWITCH_ANTENNA1 0
205#define IWL_LEGACY_SWITCH_ANTENNA2 1
206#define IWL_LEGACY_SWITCH_SISO 2
207#define IWL_LEGACY_SWITCH_MIMO2_AB 3
208#define IWL_LEGACY_SWITCH_MIMO2_AC 4
209#define IWL_LEGACY_SWITCH_MIMO2_BC 5
210#define IWL_LEGACY_SWITCH_MIMO3_ABC 6
211
212/* possible actions when in siso mode */
213#define IWL_SISO_SWITCH_ANTENNA1 0
214#define IWL_SISO_SWITCH_ANTENNA2 1
215#define IWL_SISO_SWITCH_MIMO2_AB 2
216#define IWL_SISO_SWITCH_MIMO2_AC 3
217#define IWL_SISO_SWITCH_MIMO2_BC 4
218#define IWL_SISO_SWITCH_GI 5
219#define IWL_SISO_SWITCH_MIMO3_ABC 6
220
221
222/* possible actions when in mimo mode */
223#define IWL_MIMO2_SWITCH_ANTENNA1 0
224#define IWL_MIMO2_SWITCH_ANTENNA2 1
225#define IWL_MIMO2_SWITCH_SISO_A 2
226#define IWL_MIMO2_SWITCH_SISO_B 3
227#define IWL_MIMO2_SWITCH_SISO_C 4
228#define IWL_MIMO2_SWITCH_GI 5
229#define IWL_MIMO2_SWITCH_MIMO3_ABC 6
230
231
232/* possible actions when in mimo3 mode */
233#define IWL_MIMO3_SWITCH_ANTENNA1 0
234#define IWL_MIMO3_SWITCH_ANTENNA2 1
235#define IWL_MIMO3_SWITCH_SISO_A 2
236#define IWL_MIMO3_SWITCH_SISO_B 3
237#define IWL_MIMO3_SWITCH_SISO_C 4
238#define IWL_MIMO3_SWITCH_MIMO2_AB 5
239#define IWL_MIMO3_SWITCH_MIMO2_AC 6
240#define IWL_MIMO3_SWITCH_MIMO2_BC 7
241#define IWL_MIMO3_SWITCH_GI 8
242
243
244#define IWL_MAX_11N_MIMO3_SEARCH IWL_MIMO3_SWITCH_GI
245#define IWL_MAX_SEARCH IWL_MIMO2_SWITCH_MIMO3_ABC
246
247/*FIXME:RS:add possible actions for MIMO3*/
248
249#define IWL_ACTION_LIMIT 3 /* # possible actions */
250
251#define LQ_SIZE 2 /* 2 mode tables: "Active" and "Search" */
252
253/* load per tid defines for A-MPDU activation */
254#define IWL_AGG_TPT_THREHOLD 0
255#define IWL_AGG_LOAD_THRESHOLD 10
256#define IWL_AGG_ALL_TID 0xff
257#define TID_QUEUE_CELL_SPACING 50 /*mS */
258#define TID_QUEUE_MAX_SIZE 20
259#define TID_ROUND_VALUE 5 /* mS */
260
261#define TID_MAX_TIME_DIFF ((TID_QUEUE_MAX_SIZE - 1) * TID_QUEUE_CELL_SPACING)
262#define TIME_WRAP_AROUND(x, y) (((y) > (x)) ? (y) - (x) : (0-(x)) + (y))
263
264extern const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT];
265
266enum iwl_table_type {
267 LQ_NONE,
268 LQ_G, /* legacy types */
269 LQ_A,
270 LQ_SISO, /* high-throughput types */
271 LQ_MIMO2,
272 LQ_MIMO3,
273 LQ_MAX,
274};
275
276#define is_legacy(tbl) (((tbl) == LQ_G) || ((tbl) == LQ_A))
277#define is_siso(tbl) ((tbl) == LQ_SISO)
278#define is_mimo2(tbl) ((tbl) == LQ_MIMO2)
279#define is_mimo3(tbl) ((tbl) == LQ_MIMO3)
280#define is_mimo(tbl) (is_mimo2(tbl) || is_mimo3(tbl))
281#define is_Ht(tbl) (is_siso(tbl) || is_mimo(tbl))
282#define is_a_band(tbl) ((tbl) == LQ_A)
283#define is_g_and(tbl) ((tbl) == LQ_G)
284
285#define IWL_MAX_MCS_DISPLAY_SIZE 12
286
287struct iwl_rate_mcs_info {
288 char mbps[IWL_MAX_MCS_DISPLAY_SIZE];
289 char mcs[IWL_MAX_MCS_DISPLAY_SIZE];
290};
291
292/**
293 * struct iwl_rate_scale_data -- tx success history for one rate
294 */
295struct iwl_rate_scale_data {
296 u64 data; /* bitmap of successful frames */
297 s32 success_counter; /* number of frames successful */
298 s32 success_ratio; /* per-cent * 128 */
299 s32 counter; /* number of frames attempted */
300 s32 average_tpt; /* success ratio * expected throughput */
301 unsigned long stamp;
302};
303
304/**
305 * struct iwl_scale_tbl_info -- tx params and success history for all rates
306 *
307 * There are two of these in struct iwl_lq_sta,
308 * one for "active", and one for "search".
309 */
310struct iwl_scale_tbl_info {
311 enum iwl_table_type lq_type;
312 u8 ant_type;
313 u8 is_SGI; /* 1 = short guard interval */
314 u8 is_ht40; /* 1 = 40 MHz channel width */
315 u8 is_dup; /* 1 = duplicated data streams */
316 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
317 u8 max_search; /* maximun number of tables we can search */
318 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
319 u32 current_rate; /* rate_n_flags, uCode API format */
320 struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
321};
322
323struct iwl_traffic_load {
324 unsigned long time_stamp; /* age of the oldest statistics */
325 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
326 * slice */
327 u32 total; /* total num of packets during the
328 * last TID_MAX_TIME_DIFF */
329 u8 queue_count; /* number of queues that has
330 * been used since the last cleanup */
331 u8 head; /* start of the circular buffer */
332};
333
334/**
335 * struct iwl_lq_sta -- driver's rate scaling private structure
336 *
337 * Pointer to this gets passed back and forth between driver and mac80211.
338 */
339struct iwl_lq_sta {
340 u8 active_tbl; /* index of active table, range 0-1 */
341 u8 enable_counter; /* indicates HT mode */
342 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
343 u8 search_better_tbl; /* 1: currently trying alternate mode */
344 s32 last_tpt;
345
346 /* The following determine when to search for a new mode */
347 u32 table_count_limit;
348 u32 max_failure_limit; /* # failed frames before new search */
349 u32 max_success_limit; /* # successful frames before new search */
350 u32 table_count;
351 u32 total_failed; /* total failed frames, any/all rates */
352 u32 total_success; /* total successful frames, any/all rates */
353 u64 flush_timer; /* time staying in mode before new search */
354
355 u8 action_counter; /* # mode-switch actions tried */
356 u8 is_green;
357 u8 is_dup;
358 enum ieee80211_band band;
359
360 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
361 u32 supp_rates;
362 u16 active_legacy_rate;
363 u16 active_siso_rate;
364 u16 active_mimo2_rate;
365 u16 active_mimo3_rate;
366 s8 max_rate_idx; /* Max rate set by user */
367 u8 missed_rate_counter;
368
369 struct iwl_link_quality_cmd lq;
370 struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
371 struct iwl_traffic_load load[IWL_MAX_TID_COUNT];
372 u8 tx_agg_tid_en;
373#ifdef CONFIG_MAC80211_DEBUGFS
374 struct dentry *rs_sta_dbgfs_scale_table_file;
375 struct dentry *rs_sta_dbgfs_stats_table_file;
376 struct dentry *rs_sta_dbgfs_rate_scale_data_file;
377 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
378 u32 dbg_fixed_rate;
379#endif
380 struct iwl_priv *drv;
381
382 /* used to be in sta_info */
383 int last_txrate_idx;
384 /* last tx rate_n_flags */
385 u32 last_rate_n_flags;
386 /* packets destined for this STA are aggregated */
387 u8 is_agg;
388 /* BT traffic this sta was last updated in */
389 u8 last_bt_traffic;
390};
391
392static inline u8 num_of_ant(u8 mask)
393{
394 return !!((mask) & ANT_A) +
395 !!((mask) & ANT_B) +
396 !!((mask) & ANT_C);
397}
398
399static inline u8 first_antenna(u8 mask)
400{
401 if (mask & ANT_A)
402 return ANT_A;
403 if (mask & ANT_B)
404 return ANT_B;
405 return ANT_C;
406}
407
408
409/* Initialize station's rate scaling information after adding station */
410extern void iwl_rs_rate_init(struct iwl_priv *priv,
411 struct ieee80211_sta *sta, u8 sta_id);
412
413/**
414 * iwl_rate_control_register - Register the rate control algorithm callbacks
415 *
416 * Since the rate control algorithm is hardware specific, there is no need
417 * or reason to place it as a stand alone module. The driver can call
418 * iwl_rate_control_register in order to register the rate control callbacks
419 * with the mac80211 subsystem. This should be performed prior to calling
420 * ieee80211_register_hw
421 *
422 */
423extern int iwlagn_rate_control_register(void);
424
425/**
426 * iwl_rate_control_unregister - Unregister the rate control callbacks
427 *
428 * This should be called after calling ieee80211_unregister_hw, but before
429 * the driver is unloaded.
430 */
431extern void iwlagn_rate_control_unregister(void);
432
433#endif /* __iwl_agn__rs__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/rx.c b/drivers/net/wireless/iwlwifi/dvm/rx.c
new file mode 100644
index 000000000000..d61980248408
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/rx.c
@@ -0,0 +1,1167 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portionhelp of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <linux/etherdevice.h>
31#include <linux/slab.h>
32#include <linux/sched.h>
33#include <net/mac80211.h>
34#include <asm/unaligned.h>
35#include "iwl-io.h"
36#include "eeprom.h"
37#include "dev.h"
38#include "calib.h"
39#include "agn.h"
40
41#define IWL_CMD_ENTRY(x) [x] = #x
42
43const char *iwl_dvm_cmd_strings[REPLY_MAX] = {
44 IWL_CMD_ENTRY(REPLY_ALIVE),
45 IWL_CMD_ENTRY(REPLY_ERROR),
46 IWL_CMD_ENTRY(REPLY_ECHO),
47 IWL_CMD_ENTRY(REPLY_RXON),
48 IWL_CMD_ENTRY(REPLY_RXON_ASSOC),
49 IWL_CMD_ENTRY(REPLY_QOS_PARAM),
50 IWL_CMD_ENTRY(REPLY_RXON_TIMING),
51 IWL_CMD_ENTRY(REPLY_ADD_STA),
52 IWL_CMD_ENTRY(REPLY_REMOVE_STA),
53 IWL_CMD_ENTRY(REPLY_REMOVE_ALL_STA),
54 IWL_CMD_ENTRY(REPLY_TXFIFO_FLUSH),
55 IWL_CMD_ENTRY(REPLY_WEPKEY),
56 IWL_CMD_ENTRY(REPLY_TX),
57 IWL_CMD_ENTRY(REPLY_LEDS_CMD),
58 IWL_CMD_ENTRY(REPLY_TX_LINK_QUALITY_CMD),
59 IWL_CMD_ENTRY(COEX_PRIORITY_TABLE_CMD),
60 IWL_CMD_ENTRY(COEX_MEDIUM_NOTIFICATION),
61 IWL_CMD_ENTRY(COEX_EVENT_CMD),
62 IWL_CMD_ENTRY(REPLY_QUIET_CMD),
63 IWL_CMD_ENTRY(REPLY_CHANNEL_SWITCH),
64 IWL_CMD_ENTRY(CHANNEL_SWITCH_NOTIFICATION),
65 IWL_CMD_ENTRY(REPLY_SPECTRUM_MEASUREMENT_CMD),
66 IWL_CMD_ENTRY(SPECTRUM_MEASURE_NOTIFICATION),
67 IWL_CMD_ENTRY(POWER_TABLE_CMD),
68 IWL_CMD_ENTRY(PM_SLEEP_NOTIFICATION),
69 IWL_CMD_ENTRY(PM_DEBUG_STATISTIC_NOTIFIC),
70 IWL_CMD_ENTRY(REPLY_SCAN_CMD),
71 IWL_CMD_ENTRY(REPLY_SCAN_ABORT_CMD),
72 IWL_CMD_ENTRY(SCAN_START_NOTIFICATION),
73 IWL_CMD_ENTRY(SCAN_RESULTS_NOTIFICATION),
74 IWL_CMD_ENTRY(SCAN_COMPLETE_NOTIFICATION),
75 IWL_CMD_ENTRY(BEACON_NOTIFICATION),
76 IWL_CMD_ENTRY(REPLY_TX_BEACON),
77 IWL_CMD_ENTRY(WHO_IS_AWAKE_NOTIFICATION),
78 IWL_CMD_ENTRY(QUIET_NOTIFICATION),
79 IWL_CMD_ENTRY(REPLY_TX_PWR_TABLE_CMD),
80 IWL_CMD_ENTRY(MEASURE_ABORT_NOTIFICATION),
81 IWL_CMD_ENTRY(REPLY_BT_CONFIG),
82 IWL_CMD_ENTRY(REPLY_STATISTICS_CMD),
83 IWL_CMD_ENTRY(STATISTICS_NOTIFICATION),
84 IWL_CMD_ENTRY(REPLY_CARD_STATE_CMD),
85 IWL_CMD_ENTRY(CARD_STATE_NOTIFICATION),
86 IWL_CMD_ENTRY(MISSED_BEACONS_NOTIFICATION),
87 IWL_CMD_ENTRY(REPLY_CT_KILL_CONFIG_CMD),
88 IWL_CMD_ENTRY(SENSITIVITY_CMD),
89 IWL_CMD_ENTRY(REPLY_PHY_CALIBRATION_CMD),
90 IWL_CMD_ENTRY(REPLY_RX_PHY_CMD),
91 IWL_CMD_ENTRY(REPLY_RX_MPDU_CMD),
92 IWL_CMD_ENTRY(REPLY_RX),
93 IWL_CMD_ENTRY(REPLY_COMPRESSED_BA),
94 IWL_CMD_ENTRY(CALIBRATION_CFG_CMD),
95 IWL_CMD_ENTRY(CALIBRATION_RES_NOTIFICATION),
96 IWL_CMD_ENTRY(CALIBRATION_COMPLETE_NOTIFICATION),
97 IWL_CMD_ENTRY(REPLY_TX_POWER_DBM_CMD),
98 IWL_CMD_ENTRY(TEMPERATURE_NOTIFICATION),
99 IWL_CMD_ENTRY(TX_ANT_CONFIGURATION_CMD),
100 IWL_CMD_ENTRY(REPLY_BT_COEX_PROFILE_NOTIF),
101 IWL_CMD_ENTRY(REPLY_BT_COEX_PRIO_TABLE),
102 IWL_CMD_ENTRY(REPLY_BT_COEX_PROT_ENV),
103 IWL_CMD_ENTRY(REPLY_WIPAN_PARAMS),
104 IWL_CMD_ENTRY(REPLY_WIPAN_RXON),
105 IWL_CMD_ENTRY(REPLY_WIPAN_RXON_TIMING),
106 IWL_CMD_ENTRY(REPLY_WIPAN_RXON_ASSOC),
107 IWL_CMD_ENTRY(REPLY_WIPAN_QOS_PARAM),
108 IWL_CMD_ENTRY(REPLY_WIPAN_WEPKEY),
109 IWL_CMD_ENTRY(REPLY_WIPAN_P2P_CHANNEL_SWITCH),
110 IWL_CMD_ENTRY(REPLY_WIPAN_NOA_NOTIFICATION),
111 IWL_CMD_ENTRY(REPLY_WIPAN_DEACTIVATION_COMPLETE),
112 IWL_CMD_ENTRY(REPLY_WOWLAN_PATTERNS),
113 IWL_CMD_ENTRY(REPLY_WOWLAN_WAKEUP_FILTER),
114 IWL_CMD_ENTRY(REPLY_WOWLAN_TSC_RSC_PARAMS),
115 IWL_CMD_ENTRY(REPLY_WOWLAN_TKIP_PARAMS),
116 IWL_CMD_ENTRY(REPLY_WOWLAN_KEK_KCK_MATERIAL),
117 IWL_CMD_ENTRY(REPLY_WOWLAN_GET_STATUS),
118 IWL_CMD_ENTRY(REPLY_D3_CONFIG),
119};
120#undef IWL_CMD_ENTRY
121
122/******************************************************************************
123 *
124 * Generic RX handler implementations
125 *
126 ******************************************************************************/
127
128static int iwlagn_rx_reply_error(struct iwl_priv *priv,
129 struct iwl_rx_cmd_buffer *rxb,
130 struct iwl_device_cmd *cmd)
131{
132 struct iwl_rx_packet *pkt = rxb_addr(rxb);
133 struct iwl_error_resp *err_resp = (void *)pkt->data;
134
135 IWL_ERR(priv, "Error Reply type 0x%08X cmd REPLY_ERROR (0x%02X) "
136 "seq 0x%04X ser 0x%08X\n",
137 le32_to_cpu(err_resp->error_type),
138 err_resp->cmd_id,
139 le16_to_cpu(err_resp->bad_cmd_seq_num),
140 le32_to_cpu(err_resp->error_info));
141 return 0;
142}
143
144static int iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
145 struct iwl_device_cmd *cmd)
146{
147 struct iwl_rx_packet *pkt = rxb_addr(rxb);
148 struct iwl_csa_notification *csa = (void *)pkt->data;
149 /*
150 * MULTI-FIXME
151 * See iwlagn_mac_channel_switch.
152 */
153 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
154 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
155
156 if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
157 return 0;
158
159 if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
160 rxon->channel = csa->channel;
161 ctx->staging.channel = csa->channel;
162 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
163 le16_to_cpu(csa->channel));
164 iwl_chswitch_done(priv, true);
165 } else {
166 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
167 le16_to_cpu(csa->channel));
168 iwl_chswitch_done(priv, false);
169 }
170 return 0;
171}
172
173
174static int iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
175 struct iwl_rx_cmd_buffer *rxb,
176 struct iwl_device_cmd *cmd)
177{
178 struct iwl_rx_packet *pkt = rxb_addr(rxb);
179 struct iwl_spectrum_notification *report = (void *)pkt->data;
180
181 if (!report->state) {
182 IWL_DEBUG_11H(priv,
183 "Spectrum Measure Notification: Start\n");
184 return 0;
185 }
186
187 memcpy(&priv->measure_report, report, sizeof(*report));
188 priv->measurement_status |= MEASUREMENT_READY;
189 return 0;
190}
191
192static int iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
193 struct iwl_rx_cmd_buffer *rxb,
194 struct iwl_device_cmd *cmd)
195{
196#ifdef CONFIG_IWLWIFI_DEBUG
197 struct iwl_rx_packet *pkt = rxb_addr(rxb);
198 struct iwl_sleep_notification *sleep = (void *)pkt->data;
199 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
200 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
201#endif
202 return 0;
203}
204
205static int iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
206 struct iwl_rx_cmd_buffer *rxb,
207 struct iwl_device_cmd *cmd)
208{
209 struct iwl_rx_packet *pkt = rxb_addr(rxb);
210 u32 __maybe_unused len =
211 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
212 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
213 "notification for PM_DEBUG_STATISTIC_NOTIFIC:\n", len);
214 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->data, len);
215 return 0;
216}
217
218static int iwlagn_rx_beacon_notif(struct iwl_priv *priv,
219 struct iwl_rx_cmd_buffer *rxb,
220 struct iwl_device_cmd *cmd)
221{
222 struct iwl_rx_packet *pkt = rxb_addr(rxb);
223 struct iwlagn_beacon_notif *beacon = (void *)pkt->data;
224#ifdef CONFIG_IWLWIFI_DEBUG
225 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
226 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
227
228 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
229 "tsf:0x%.8x%.8x rate:%d\n",
230 status & TX_STATUS_MSK,
231 beacon->beacon_notify_hdr.failure_frame,
232 le32_to_cpu(beacon->ibss_mgr_status),
233 le32_to_cpu(beacon->high_tsf),
234 le32_to_cpu(beacon->low_tsf), rate);
235#endif
236
237 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
238
239 return 0;
240}
241
242/**
243 * iwl_good_plcp_health - checks for plcp error.
244 *
245 * When the plcp error is exceeding the thresholds, reset the radio
246 * to improve the throughput.
247 */
248static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
249 struct statistics_rx_phy *cur_ofdm,
250 struct statistics_rx_ht_phy *cur_ofdm_ht,
251 unsigned int msecs)
252{
253 int delta;
254 int threshold = priv->plcp_delta_threshold;
255
256 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
257 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
258 return true;
259 }
260
261 delta = le32_to_cpu(cur_ofdm->plcp_err) -
262 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
263 le32_to_cpu(cur_ofdm_ht->plcp_err) -
264 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
265
266 /* Can be negative if firmware reset statistics */
267 if (delta <= 0)
268 return true;
269
270 if ((delta * 100 / msecs) > threshold) {
271 IWL_DEBUG_RADIO(priv,
272 "plcp health threshold %u delta %d msecs %u\n",
273 threshold, delta, msecs);
274 return false;
275 }
276
277 return true;
278}
279
280int iwl_force_rf_reset(struct iwl_priv *priv, bool external)
281{
282 struct iwl_rf_reset *rf_reset;
283
284 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
285 return -EAGAIN;
286
287 if (!iwl_is_any_associated(priv)) {
288 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
289 return -ENOLINK;
290 }
291
292 rf_reset = &priv->rf_reset;
293 rf_reset->reset_request_count++;
294 if (!external && rf_reset->last_reset_jiffies &&
295 time_after(rf_reset->last_reset_jiffies +
296 IWL_DELAY_NEXT_FORCE_RF_RESET, jiffies)) {
297 IWL_DEBUG_INFO(priv, "RF reset rejected\n");
298 rf_reset->reset_reject_count++;
299 return -EAGAIN;
300 }
301 rf_reset->reset_success_count++;
302 rf_reset->last_reset_jiffies = jiffies;
303
304 /*
305 * There is no easy and better way to force reset the radio,
306 * the only known method is switching channel which will force to
307 * reset and tune the radio.
308 * Use internal short scan (single channel) operation to should
309 * achieve this objective.
310 * Driver should reset the radio when number of consecutive missed
311 * beacon, or any other uCode error condition detected.
312 */
313 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
314 iwl_internal_short_hw_scan(priv);
315 return 0;
316}
317
318
319static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
320 struct statistics_rx_phy *cur_ofdm,
321 struct statistics_rx_ht_phy *cur_ofdm_ht,
322 struct statistics_tx *tx,
323 unsigned long stamp)
324{
325 unsigned int msecs;
326
327 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
328 return;
329
330 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
331
332 /* Only gather statistics and update time stamp when not associated */
333 if (!iwl_is_any_associated(priv))
334 return;
335
336 /* Do not check/recover when do not have enough statistics data */
337 if (msecs < 99)
338 return;
339
340 if (iwlwifi_mod_params.plcp_check &&
341 !iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
342 iwl_force_rf_reset(priv, false);
343}
344
345/* Calculate noise level, based on measurements during network silence just
346 * before arriving beacon. This measurement can be done only if we know
347 * exactly when to expect beacons, therefore only when we're associated. */
348static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
349{
350 struct statistics_rx_non_phy *rx_info;
351 int num_active_rx = 0;
352 int total_silence = 0;
353 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
354 int last_rx_noise;
355
356 rx_info = &priv->statistics.rx_non_phy;
357
358 bcn_silence_a =
359 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
360 bcn_silence_b =
361 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
362 bcn_silence_c =
363 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
364
365 if (bcn_silence_a) {
366 total_silence += bcn_silence_a;
367 num_active_rx++;
368 }
369 if (bcn_silence_b) {
370 total_silence += bcn_silence_b;
371 num_active_rx++;
372 }
373 if (bcn_silence_c) {
374 total_silence += bcn_silence_c;
375 num_active_rx++;
376 }
377
378 /* Average among active antennas */
379 if (num_active_rx)
380 last_rx_noise = (total_silence / num_active_rx) - 107;
381 else
382 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
383
384 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
385 bcn_silence_a, bcn_silence_b, bcn_silence_c,
386 last_rx_noise);
387}
388
389#ifdef CONFIG_IWLWIFI_DEBUGFS
390/*
391 * based on the assumption of all statistics counter are in DWORD
392 * FIXME: This function is for debugging, do not deal with
393 * the case of counters roll-over.
394 */
395static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
396 __le32 *max_delta, __le32 *accum, int size)
397{
398 int i;
399
400 for (i = 0;
401 i < size / sizeof(__le32);
402 i++, prev++, cur++, delta++, max_delta++, accum++) {
403 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
404 *delta = cpu_to_le32(
405 le32_to_cpu(*cur) - le32_to_cpu(*prev));
406 le32_add_cpu(accum, le32_to_cpu(*delta));
407 if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
408 *max_delta = *delta;
409 }
410 }
411}
412
413static void
414iwlagn_accumulative_statistics(struct iwl_priv *priv,
415 struct statistics_general_common *common,
416 struct statistics_rx_non_phy *rx_non_phy,
417 struct statistics_rx_phy *rx_ofdm,
418 struct statistics_rx_ht_phy *rx_ofdm_ht,
419 struct statistics_rx_phy *rx_cck,
420 struct statistics_tx *tx,
421 struct statistics_bt_activity *bt_activity)
422{
423#define ACCUM(_name) \
424 accum_stats((__le32 *)&priv->statistics._name, \
425 (__le32 *)_name, \
426 (__le32 *)&priv->delta_stats._name, \
427 (__le32 *)&priv->max_delta_stats._name, \
428 (__le32 *)&priv->accum_stats._name, \
429 sizeof(*_name));
430
431 ACCUM(common);
432 ACCUM(rx_non_phy);
433 ACCUM(rx_ofdm);
434 ACCUM(rx_ofdm_ht);
435 ACCUM(rx_cck);
436 ACCUM(tx);
437 if (bt_activity)
438 ACCUM(bt_activity);
439#undef ACCUM
440}
441#else
442static inline void
443iwlagn_accumulative_statistics(struct iwl_priv *priv,
444 struct statistics_general_common *common,
445 struct statistics_rx_non_phy *rx_non_phy,
446 struct statistics_rx_phy *rx_ofdm,
447 struct statistics_rx_ht_phy *rx_ofdm_ht,
448 struct statistics_rx_phy *rx_cck,
449 struct statistics_tx *tx,
450 struct statistics_bt_activity *bt_activity)
451{
452}
453#endif
454
455static int iwlagn_rx_statistics(struct iwl_priv *priv,
456 struct iwl_rx_cmd_buffer *rxb,
457 struct iwl_device_cmd *cmd)
458{
459 unsigned long stamp = jiffies;
460 const int reg_recalib_period = 60;
461 int change;
462 struct iwl_rx_packet *pkt = rxb_addr(rxb);
463 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
464 __le32 *flag;
465 struct statistics_general_common *common;
466 struct statistics_rx_non_phy *rx_non_phy;
467 struct statistics_rx_phy *rx_ofdm;
468 struct statistics_rx_ht_phy *rx_ofdm_ht;
469 struct statistics_rx_phy *rx_cck;
470 struct statistics_tx *tx;
471 struct statistics_bt_activity *bt_activity;
472
473 len -= sizeof(struct iwl_cmd_header); /* skip header */
474
475 IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
476 len);
477
478 spin_lock(&priv->statistics.lock);
479
480 if (len == sizeof(struct iwl_bt_notif_statistics)) {
481 struct iwl_bt_notif_statistics *stats;
482 stats = (void *)&pkt->data;
483 flag = &stats->flag;
484 common = &stats->general.common;
485 rx_non_phy = &stats->rx.general.common;
486 rx_ofdm = &stats->rx.ofdm;
487 rx_ofdm_ht = &stats->rx.ofdm_ht;
488 rx_cck = &stats->rx.cck;
489 tx = &stats->tx;
490 bt_activity = &stats->general.activity;
491
492#ifdef CONFIG_IWLWIFI_DEBUGFS
493 /* handle this exception directly */
494 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
495 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
496 le32_to_cpu(stats->rx.general.num_bt_kills));
497#endif
498 } else if (len == sizeof(struct iwl_notif_statistics)) {
499 struct iwl_notif_statistics *stats;
500 stats = (void *)&pkt->data;
501 flag = &stats->flag;
502 common = &stats->general.common;
503 rx_non_phy = &stats->rx.general;
504 rx_ofdm = &stats->rx.ofdm;
505 rx_ofdm_ht = &stats->rx.ofdm_ht;
506 rx_cck = &stats->rx.cck;
507 tx = &stats->tx;
508 bt_activity = NULL;
509 } else {
510 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
511 len, sizeof(struct iwl_bt_notif_statistics),
512 sizeof(struct iwl_notif_statistics));
513 spin_unlock(&priv->statistics.lock);
514 return 0;
515 }
516
517 change = common->temperature != priv->statistics.common.temperature ||
518 (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
519 (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
520
521 iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
522 rx_ofdm_ht, rx_cck, tx, bt_activity);
523
524 iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
525
526 priv->statistics.flag = *flag;
527 memcpy(&priv->statistics.common, common, sizeof(*common));
528 memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
529 memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
530 memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
531 memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
532 memcpy(&priv->statistics.tx, tx, sizeof(*tx));
533#ifdef CONFIG_IWLWIFI_DEBUGFS
534 if (bt_activity)
535 memcpy(&priv->statistics.bt_activity, bt_activity,
536 sizeof(*bt_activity));
537#endif
538
539 priv->rx_statistics_jiffies = stamp;
540
541 set_bit(STATUS_STATISTICS, &priv->status);
542
543 /* Reschedule the statistics timer to occur in
544 * reg_recalib_period seconds to ensure we get a
545 * thermal update even if the uCode doesn't give
546 * us one */
547 mod_timer(&priv->statistics_periodic, jiffies +
548 msecs_to_jiffies(reg_recalib_period * 1000));
549
550 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
551 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
552 iwlagn_rx_calc_noise(priv);
553 queue_work(priv->workqueue, &priv->run_time_calib_work);
554 }
555 if (priv->lib->temperature && change)
556 priv->lib->temperature(priv);
557
558 spin_unlock(&priv->statistics.lock);
559
560 return 0;
561}
562
563static int iwlagn_rx_reply_statistics(struct iwl_priv *priv,
564 struct iwl_rx_cmd_buffer *rxb,
565 struct iwl_device_cmd *cmd)
566{
567 struct iwl_rx_packet *pkt = rxb_addr(rxb);
568 struct iwl_notif_statistics *stats = (void *)pkt->data;
569
570 if (le32_to_cpu(stats->flag) & UCODE_STATISTICS_CLEAR_MSK) {
571#ifdef CONFIG_IWLWIFI_DEBUGFS
572 memset(&priv->accum_stats, 0,
573 sizeof(priv->accum_stats));
574 memset(&priv->delta_stats, 0,
575 sizeof(priv->delta_stats));
576 memset(&priv->max_delta_stats, 0,
577 sizeof(priv->max_delta_stats));
578#endif
579 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
580 }
581 iwlagn_rx_statistics(priv, rxb, cmd);
582 return 0;
583}
584
585/* Handle notification from uCode that card's power state is changing
586 * due to software, hardware, or critical temperature RFKILL */
587static int iwlagn_rx_card_state_notif(struct iwl_priv *priv,
588 struct iwl_rx_cmd_buffer *rxb,
589 struct iwl_device_cmd *cmd)
590{
591 struct iwl_rx_packet *pkt = rxb_addr(rxb);
592 struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
593 u32 flags = le32_to_cpu(card_state_notif->flags);
594 unsigned long status = priv->status;
595
596 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
597 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
598 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
599 (flags & CT_CARD_DISABLED) ?
600 "Reached" : "Not reached");
601
602 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
603 CT_CARD_DISABLED)) {
604
605 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
606 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
607
608 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
609 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
610
611 if (!(flags & RXON_CARD_DISABLED)) {
612 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
613 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
614 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
615 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
616 }
617 if (flags & CT_CARD_DISABLED)
618 iwl_tt_enter_ct_kill(priv);
619 }
620 if (!(flags & CT_CARD_DISABLED))
621 iwl_tt_exit_ct_kill(priv);
622
623 if (flags & HW_CARD_DISABLED)
624 set_bit(STATUS_RF_KILL_HW, &priv->status);
625 else
626 clear_bit(STATUS_RF_KILL_HW, &priv->status);
627
628
629 if (!(flags & RXON_CARD_DISABLED))
630 iwl_scan_cancel(priv);
631
632 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
633 test_bit(STATUS_RF_KILL_HW, &priv->status)))
634 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
635 test_bit(STATUS_RF_KILL_HW, &priv->status));
636 else
637 wake_up(&priv->trans->wait_command_queue);
638 return 0;
639}
640
641static int iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
642 struct iwl_rx_cmd_buffer *rxb,
643 struct iwl_device_cmd *cmd)
644
645{
646 struct iwl_rx_packet *pkt = rxb_addr(rxb);
647 struct iwl_missed_beacon_notif *missed_beacon = (void *)pkt->data;
648
649 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
650 priv->missed_beacon_threshold) {
651 IWL_DEBUG_CALIB(priv,
652 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
653 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
654 le32_to_cpu(missed_beacon->total_missed_becons),
655 le32_to_cpu(missed_beacon->num_recvd_beacons),
656 le32_to_cpu(missed_beacon->num_expected_beacons));
657 if (!test_bit(STATUS_SCANNING, &priv->status))
658 iwl_init_sensitivity(priv);
659 }
660 return 0;
661}
662
663/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
664 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
665static int iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
666 struct iwl_rx_cmd_buffer *rxb,
667 struct iwl_device_cmd *cmd)
668{
669 struct iwl_rx_packet *pkt = rxb_addr(rxb);
670
671 priv->last_phy_res_valid = true;
672 memcpy(&priv->last_phy_res, pkt->data,
673 sizeof(struct iwl_rx_phy_res));
674 return 0;
675}
676
677/*
678 * returns non-zero if packet should be dropped
679 */
680static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
681 struct ieee80211_hdr *hdr,
682 u32 decrypt_res,
683 struct ieee80211_rx_status *stats)
684{
685 u16 fc = le16_to_cpu(hdr->frame_control);
686
687 /*
688 * All contexts have the same setting here due to it being
689 * a module parameter, so OK to check any context.
690 */
691 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
692 RXON_FILTER_DIS_DECRYPT_MSK)
693 return 0;
694
695 if (!(fc & IEEE80211_FCTL_PROTECTED))
696 return 0;
697
698 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
699 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
700 case RX_RES_STATUS_SEC_TYPE_TKIP:
701 /* The uCode has got a bad phase 1 Key, pushes the packet.
702 * Decryption will be done in SW. */
703 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
704 RX_RES_STATUS_BAD_KEY_TTAK)
705 break;
706
707 case RX_RES_STATUS_SEC_TYPE_WEP:
708 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
709 RX_RES_STATUS_BAD_ICV_MIC) {
710 /* bad ICV, the packet is destroyed since the
711 * decryption is inplace, drop it */
712 IWL_DEBUG_RX(priv, "Packet destroyed\n");
713 return -1;
714 }
715 case RX_RES_STATUS_SEC_TYPE_CCMP:
716 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
717 RX_RES_STATUS_DECRYPT_OK) {
718 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
719 stats->flag |= RX_FLAG_DECRYPTED;
720 }
721 break;
722
723 default:
724 break;
725 }
726 return 0;
727}
728
729static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
730 struct ieee80211_hdr *hdr,
731 u16 len,
732 u32 ampdu_status,
733 struct iwl_rx_cmd_buffer *rxb,
734 struct ieee80211_rx_status *stats)
735{
736 struct sk_buff *skb;
737 __le16 fc = hdr->frame_control;
738 struct iwl_rxon_context *ctx;
739 unsigned int hdrlen, fraglen;
740
741 /* We only process data packets if the interface is open */
742 if (unlikely(!priv->is_open)) {
743 IWL_DEBUG_DROP_LIMIT(priv,
744 "Dropping packet while interface is not open.\n");
745 return;
746 }
747
748 /* In case of HW accelerated crypto and bad decryption, drop */
749 if (!iwlwifi_mod_params.sw_crypto &&
750 iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
751 return;
752
753 /* Dont use dev_alloc_skb(), we'll have enough headroom once
754 * ieee80211_hdr pulled.
755 */
756 skb = alloc_skb(128, GFP_ATOMIC);
757 if (!skb) {
758 IWL_ERR(priv, "alloc_skb failed\n");
759 return;
760 }
761 /* If frame is small enough to fit in skb->head, pull it completely.
762 * If not, only pull ieee80211_hdr so that splice() or TCP coalesce
763 * are more efficient.
764 */
765 hdrlen = (len <= skb_tailroom(skb)) ? len : sizeof(*hdr);
766
767 memcpy(skb_put(skb, hdrlen), hdr, hdrlen);
768 fraglen = len - hdrlen;
769
770 if (fraglen) {
771 int offset = (void *)hdr + hdrlen -
772 rxb_addr(rxb) + rxb_offset(rxb);
773
774 skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
775 fraglen, rxb->truesize);
776 }
777
778 /*
779 * Wake any queues that were stopped due to a passive channel tx
780 * failure. This can happen because the regulatory enforcement in
781 * the device waits for a beacon before allowing transmission,
782 * sometimes even after already having transmitted frames for the
783 * association because the new RXON may reset the information.
784 */
785 if (unlikely(ieee80211_is_beacon(fc) && priv->passive_no_rx)) {
786 for_each_context(priv, ctx) {
787 if (!ether_addr_equal(hdr->addr3,
788 ctx->active.bssid_addr))
789 continue;
790 iwlagn_lift_passive_no_rx(priv);
791 }
792 }
793
794 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
795
796 ieee80211_rx(priv->hw, skb);
797}
798
799static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
800{
801 u32 decrypt_out = 0;
802
803 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
804 RX_RES_STATUS_STATION_FOUND)
805 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
806 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
807
808 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
809
810 /* packet was not encrypted */
811 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
812 RX_RES_STATUS_SEC_TYPE_NONE)
813 return decrypt_out;
814
815 /* packet was encrypted with unknown alg */
816 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
817 RX_RES_STATUS_SEC_TYPE_ERR)
818 return decrypt_out;
819
820 /* decryption was not done in HW */
821 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
822 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
823 return decrypt_out;
824
825 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
826
827 case RX_RES_STATUS_SEC_TYPE_CCMP:
828 /* alg is CCM: check MIC only */
829 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
830 /* Bad MIC */
831 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
832 else
833 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
834
835 break;
836
837 case RX_RES_STATUS_SEC_TYPE_TKIP:
838 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
839 /* Bad TTAK */
840 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
841 break;
842 }
843 /* fall through if TTAK OK */
844 default:
845 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
846 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
847 else
848 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
849 break;
850 }
851
852 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
853 decrypt_in, decrypt_out);
854
855 return decrypt_out;
856}
857
858/* Calc max signal level (dBm) among 3 possible receivers */
859static int iwlagn_calc_rssi(struct iwl_priv *priv,
860 struct iwl_rx_phy_res *rx_resp)
861{
862 /* data from PHY/DSP regarding signal strength, etc.,
863 * contents are always there, not configurable by host
864 */
865 struct iwlagn_non_cfg_phy *ncphy =
866 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
867 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
868 u8 agc;
869
870 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
871 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
872
873 /* Find max rssi among 3 possible receivers.
874 * These values are measured by the digital signal processor (DSP).
875 * They should stay fairly constant even as the signal strength varies,
876 * if the radio's automatic gain control (AGC) is working right.
877 * AGC value (see below) will provide the "interesting" info.
878 */
879 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
880 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
881 IWLAGN_OFDM_RSSI_A_BIT_POS;
882 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
883 IWLAGN_OFDM_RSSI_B_BIT_POS;
884 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
885 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
886 IWLAGN_OFDM_RSSI_C_BIT_POS;
887
888 max_rssi = max_t(u32, rssi_a, rssi_b);
889 max_rssi = max_t(u32, max_rssi, rssi_c);
890
891 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
892 rssi_a, rssi_b, rssi_c, max_rssi, agc);
893
894 /* dBm = max_rssi dB - agc dB - constant.
895 * Higher AGC (higher radio gain) means lower signal. */
896 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
897}
898
899/* Called for REPLY_RX (legacy ABG frames), or
900 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
901static int iwlagn_rx_reply_rx(struct iwl_priv *priv,
902 struct iwl_rx_cmd_buffer *rxb,
903 struct iwl_device_cmd *cmd)
904{
905 struct ieee80211_hdr *header;
906 struct ieee80211_rx_status rx_status;
907 struct iwl_rx_packet *pkt = rxb_addr(rxb);
908 struct iwl_rx_phy_res *phy_res;
909 __le32 rx_pkt_status;
910 struct iwl_rx_mpdu_res_start *amsdu;
911 u32 len;
912 u32 ampdu_status;
913 u32 rate_n_flags;
914
915 /**
916 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
917 * REPLY_RX: physical layer info is in this buffer
918 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
919 * command and cached in priv->last_phy_res
920 *
921 * Here we set up local variables depending on which command is
922 * received.
923 */
924 if (pkt->hdr.cmd == REPLY_RX) {
925 phy_res = (struct iwl_rx_phy_res *)pkt->data;
926 header = (struct ieee80211_hdr *)(pkt->data + sizeof(*phy_res)
927 + phy_res->cfg_phy_cnt);
928
929 len = le16_to_cpu(phy_res->byte_count);
930 rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*phy_res) +
931 phy_res->cfg_phy_cnt + len);
932 ampdu_status = le32_to_cpu(rx_pkt_status);
933 } else {
934 if (!priv->last_phy_res_valid) {
935 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
936 return 0;
937 }
938 phy_res = &priv->last_phy_res;
939 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->data;
940 header = (struct ieee80211_hdr *)(pkt->data + sizeof(*amsdu));
941 len = le16_to_cpu(amsdu->byte_count);
942 rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*amsdu) + len);
943 ampdu_status = iwlagn_translate_rx_status(priv,
944 le32_to_cpu(rx_pkt_status));
945 }
946
947 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
948 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d\n",
949 phy_res->cfg_phy_cnt);
950 return 0;
951 }
952
953 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
954 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
955 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
956 le32_to_cpu(rx_pkt_status));
957 return 0;
958 }
959
960 /* This will be used in several places later */
961 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
962
963 /* rx_status carries information about the packet to mac80211 */
964 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
965 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
966 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
967 rx_status.freq =
968 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
969 rx_status.band);
970 rx_status.rate_idx =
971 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
972 rx_status.flag = 0;
973
974 /* TSF isn't reliable. In order to allow smooth user experience,
975 * this W/A doesn't propagate it to the mac80211 */
976 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
977
978 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
979
980 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
981 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
982
983 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
984 rx_status.signal, (unsigned long long)rx_status.mactime);
985
986 /*
987 * "antenna number"
988 *
989 * It seems that the antenna field in the phy flags value
990 * is actually a bit field. This is undefined by radiotap,
991 * it wants an actual antenna number but I always get "7"
992 * for most legacy frames I receive indicating that the
993 * same frame was received on all three RX chains.
994 *
995 * I think this field should be removed in favor of a
996 * new 802.11n radiotap field "RX chains" that is defined
997 * as a bitmask.
998 */
999 rx_status.antenna =
1000 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1001 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1002
1003 /* set the preamble flag if appropriate */
1004 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1005 rx_status.flag |= RX_FLAG_SHORTPRE;
1006
1007 /* Set up the HT phy flags */
1008 if (rate_n_flags & RATE_MCS_HT_MSK)
1009 rx_status.flag |= RX_FLAG_HT;
1010 if (rate_n_flags & RATE_MCS_HT40_MSK)
1011 rx_status.flag |= RX_FLAG_40MHZ;
1012 if (rate_n_flags & RATE_MCS_SGI_MSK)
1013 rx_status.flag |= RX_FLAG_SHORT_GI;
1014 if (rate_n_flags & RATE_MCS_GF_MSK)
1015 rx_status.flag |= RX_FLAG_HT_GF;
1016
1017 iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1018 rxb, &rx_status);
1019 return 0;
1020}
1021
1022static int iwlagn_rx_noa_notification(struct iwl_priv *priv,
1023 struct iwl_rx_cmd_buffer *rxb,
1024 struct iwl_device_cmd *cmd)
1025{
1026 struct iwl_wipan_noa_data *new_data, *old_data;
1027 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1028 struct iwl_wipan_noa_notification *noa_notif = (void *)pkt->data;
1029
1030 /* no condition -- we're in softirq */
1031 old_data = rcu_dereference_protected(priv->noa_data, true);
1032
1033 if (noa_notif->noa_active) {
1034 u32 len = le16_to_cpu(noa_notif->noa_attribute.length);
1035 u32 copylen = len;
1036
1037 /* EID, len, OUI, subtype */
1038 len += 1 + 1 + 3 + 1;
1039 /* P2P id, P2P length */
1040 len += 1 + 2;
1041 copylen += 1 + 2;
1042
1043 new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
1044 if (new_data) {
1045 new_data->length = len;
1046 new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
1047 new_data->data[1] = len - 2; /* not counting EID, len */
1048 new_data->data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
1049 new_data->data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
1050 new_data->data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
1051 new_data->data[5] = WLAN_OUI_TYPE_WFA_P2P;
1052 memcpy(&new_data->data[6], &noa_notif->noa_attribute,
1053 copylen);
1054 }
1055 } else
1056 new_data = NULL;
1057
1058 rcu_assign_pointer(priv->noa_data, new_data);
1059
1060 if (old_data)
1061 kfree_rcu(old_data, rcu_head);
1062
1063 return 0;
1064}
1065
1066/**
1067 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1068 *
1069 * Setup the RX handlers for each of the reply types sent from the uCode
1070 * to the host.
1071 */
1072void iwl_setup_rx_handlers(struct iwl_priv *priv)
1073{
1074 int (**handlers)(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
1075 struct iwl_device_cmd *cmd);
1076
1077 handlers = priv->rx_handlers;
1078
1079 handlers[REPLY_ERROR] = iwlagn_rx_reply_error;
1080 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwlagn_rx_csa;
1081 handlers[SPECTRUM_MEASURE_NOTIFICATION] =
1082 iwlagn_rx_spectrum_measure_notif;
1083 handlers[PM_SLEEP_NOTIFICATION] = iwlagn_rx_pm_sleep_notif;
1084 handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1085 iwlagn_rx_pm_debug_statistics_notif;
1086 handlers[BEACON_NOTIFICATION] = iwlagn_rx_beacon_notif;
1087 handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
1088
1089 handlers[REPLY_WIPAN_NOA_NOTIFICATION] = iwlagn_rx_noa_notification;
1090
1091 /*
1092 * The same handler is used for both the REPLY to a discrete
1093 * statistics request from the host as well as for the periodic
1094 * statistics notifications (after received beacons) from the uCode.
1095 */
1096 handlers[REPLY_STATISTICS_CMD] = iwlagn_rx_reply_statistics;
1097 handlers[STATISTICS_NOTIFICATION] = iwlagn_rx_statistics;
1098
1099 iwl_setup_rx_scan_handlers(priv);
1100
1101 handlers[CARD_STATE_NOTIFICATION] = iwlagn_rx_card_state_notif;
1102 handlers[MISSED_BEACONS_NOTIFICATION] =
1103 iwlagn_rx_missed_beacon_notif;
1104
1105 /* Rx handlers */
1106 handlers[REPLY_RX_PHY_CMD] = iwlagn_rx_reply_rx_phy;
1107 handlers[REPLY_RX_MPDU_CMD] = iwlagn_rx_reply_rx;
1108
1109 /* block ack */
1110 handlers[REPLY_COMPRESSED_BA] =
1111 iwlagn_rx_reply_compressed_ba;
1112
1113 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
1114
1115 /* set up notification wait support */
1116 iwl_notification_wait_init(&priv->notif_wait);
1117
1118 /* Set up BT Rx handlers */
1119 if (priv->cfg->bt_params)
1120 iwlagn_bt_rx_handler_setup(priv);
1121}
1122
1123int iwl_rx_dispatch(struct iwl_op_mode *op_mode, struct iwl_rx_cmd_buffer *rxb,
1124 struct iwl_device_cmd *cmd)
1125{
1126 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1127 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1128 void (*pre_rx_handler)(struct iwl_priv *,
1129 struct iwl_rx_cmd_buffer *);
1130 int err = 0;
1131
1132 /*
1133 * Do the notification wait before RX handlers so
1134 * even if the RX handler consumes the RXB we have
1135 * access to it in the notification wait entry.
1136 */
1137 iwl_notification_wait_notify(&priv->notif_wait, pkt);
1138
1139 /* RX data may be forwarded to userspace (using pre_rx_handler) in one
1140 * of two cases: the first, that the user owns the uCode through
1141 * testmode - in such case the pre_rx_handler is set and no further
1142 * processing takes place. The other case is when the user want to
1143 * monitor the rx w/o affecting the regular flow - the pre_rx_handler
1144 * will be set but the ownership flag != IWL_OWNERSHIP_TM and the flow
1145 * continues.
1146 * We need to use ACCESS_ONCE to prevent a case where the handler
1147 * changes between the check and the call.
1148 */
1149 pre_rx_handler = ACCESS_ONCE(priv->pre_rx_handler);
1150 if (pre_rx_handler)
1151 pre_rx_handler(priv, rxb);
1152 if (priv->ucode_owner != IWL_OWNERSHIP_TM) {
1153 /* Based on type of command response or notification,
1154 * handle those that need handling via function in
1155 * rx_handlers table. See iwl_setup_rx_handlers() */
1156 if (priv->rx_handlers[pkt->hdr.cmd]) {
1157 priv->rx_handlers_stats[pkt->hdr.cmd]++;
1158 err = priv->rx_handlers[pkt->hdr.cmd] (priv, rxb, cmd);
1159 } else {
1160 /* No handling needed */
1161 IWL_DEBUG_RX(priv, "No handler needed for %s, 0x%02x\n",
1162 iwl_dvm_get_cmd_string(pkt->hdr.cmd),
1163 pkt->hdr.cmd);
1164 }
1165 }
1166 return err;
1167}
diff --git a/drivers/net/wireless/iwlwifi/dvm/rxon.c b/drivers/net/wireless/iwlwifi/dvm/rxon.c
new file mode 100644
index 000000000000..4f55689ac6a7
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/rxon.c
@@ -0,0 +1,1577 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26
27#include <linux/etherdevice.h>
28#include "iwl-trans.h"
29#include "iwl-modparams.h"
30#include "dev.h"
31#include "agn.h"
32#include "calib.h"
33
34/*
35 * initialize rxon structure with default values from eeprom
36 */
37void iwl_connection_init_rx_config(struct iwl_priv *priv,
38 struct iwl_rxon_context *ctx)
39{
40 memset(&ctx->staging, 0, sizeof(ctx->staging));
41
42 if (!ctx->vif) {
43 ctx->staging.dev_type = ctx->unused_devtype;
44 } else
45 switch (ctx->vif->type) {
46 case NL80211_IFTYPE_AP:
47 ctx->staging.dev_type = ctx->ap_devtype;
48 break;
49
50 case NL80211_IFTYPE_STATION:
51 ctx->staging.dev_type = ctx->station_devtype;
52 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
53 break;
54
55 case NL80211_IFTYPE_ADHOC:
56 ctx->staging.dev_type = ctx->ibss_devtype;
57 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
58 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
59 RXON_FILTER_ACCEPT_GRP_MSK;
60 break;
61
62 case NL80211_IFTYPE_MONITOR:
63 ctx->staging.dev_type = RXON_DEV_TYPE_SNIFFER;
64 break;
65
66 default:
67 IWL_ERR(priv, "Unsupported interface type %d\n",
68 ctx->vif->type);
69 break;
70 }
71
72#if 0
73 /* TODO: Figure out when short_preamble would be set and cache from
74 * that */
75 if (!hw_to_local(priv->hw)->short_preamble)
76 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
77 else
78 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
79#endif
80
81 ctx->staging.channel = cpu_to_le16(priv->hw->conf.channel->hw_value);
82 priv->band = priv->hw->conf.channel->band;
83
84 iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
85
86 /* clear both MIX and PURE40 mode flag */
87 ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
88 RXON_FLG_CHANNEL_MODE_PURE_40);
89 if (ctx->vif)
90 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
91
92 ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
93 ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
94 ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
95}
96
97static int iwlagn_disable_bss(struct iwl_priv *priv,
98 struct iwl_rxon_context *ctx,
99 struct iwl_rxon_cmd *send)
100{
101 __le32 old_filter = send->filter_flags;
102 int ret;
103
104 send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
105 ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
106 CMD_SYNC, sizeof(*send), send);
107
108 send->filter_flags = old_filter;
109
110 if (ret)
111 IWL_DEBUG_QUIET_RFKILL(priv,
112 "Error clearing ASSOC_MSK on BSS (%d)\n", ret);
113
114 return ret;
115}
116
117static int iwlagn_disable_pan(struct iwl_priv *priv,
118 struct iwl_rxon_context *ctx,
119 struct iwl_rxon_cmd *send)
120{
121 struct iwl_notification_wait disable_wait;
122 __le32 old_filter = send->filter_flags;
123 u8 old_dev_type = send->dev_type;
124 int ret;
125 static const u8 deactivate_cmd[] = {
126 REPLY_WIPAN_DEACTIVATION_COMPLETE
127 };
128
129 iwl_init_notification_wait(&priv->notif_wait, &disable_wait,
130 deactivate_cmd, ARRAY_SIZE(deactivate_cmd),
131 NULL, NULL);
132
133 send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
134 send->dev_type = RXON_DEV_TYPE_P2P;
135 ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
136 CMD_SYNC, sizeof(*send), send);
137
138 send->filter_flags = old_filter;
139 send->dev_type = old_dev_type;
140
141 if (ret) {
142 IWL_ERR(priv, "Error disabling PAN (%d)\n", ret);
143 iwl_remove_notification(&priv->notif_wait, &disable_wait);
144 } else {
145 ret = iwl_wait_notification(&priv->notif_wait,
146 &disable_wait, HZ);
147 if (ret)
148 IWL_ERR(priv, "Timed out waiting for PAN disable\n");
149 }
150
151 return ret;
152}
153
154static int iwlagn_disconn_pan(struct iwl_priv *priv,
155 struct iwl_rxon_context *ctx,
156 struct iwl_rxon_cmd *send)
157{
158 __le32 old_filter = send->filter_flags;
159 int ret;
160
161 send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
162 ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, CMD_SYNC,
163 sizeof(*send), send);
164
165 send->filter_flags = old_filter;
166
167 return ret;
168}
169
170static void iwlagn_update_qos(struct iwl_priv *priv,
171 struct iwl_rxon_context *ctx)
172{
173 int ret;
174
175 if (!ctx->is_active)
176 return;
177
178 ctx->qos_data.def_qos_parm.qos_flags = 0;
179
180 if (ctx->qos_data.qos_active)
181 ctx->qos_data.def_qos_parm.qos_flags |=
182 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
183
184 if (ctx->ht.enabled)
185 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
186
187 IWL_DEBUG_INFO(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
188 ctx->qos_data.qos_active,
189 ctx->qos_data.def_qos_parm.qos_flags);
190
191 ret = iwl_dvm_send_cmd_pdu(priv, ctx->qos_cmd, CMD_SYNC,
192 sizeof(struct iwl_qosparam_cmd),
193 &ctx->qos_data.def_qos_parm);
194 if (ret)
195 IWL_DEBUG_QUIET_RFKILL(priv, "Failed to update QoS\n");
196}
197
198static int iwlagn_update_beacon(struct iwl_priv *priv,
199 struct ieee80211_vif *vif)
200{
201 lockdep_assert_held(&priv->mutex);
202
203 dev_kfree_skb(priv->beacon_skb);
204 priv->beacon_skb = ieee80211_beacon_get(priv->hw, vif);
205 if (!priv->beacon_skb)
206 return -ENOMEM;
207 return iwlagn_send_beacon_cmd(priv);
208}
209
210static int iwlagn_send_rxon_assoc(struct iwl_priv *priv,
211 struct iwl_rxon_context *ctx)
212{
213 int ret = 0;
214 struct iwl_rxon_assoc_cmd rxon_assoc;
215 const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
216 const struct iwl_rxon_cmd *rxon2 = &ctx->active;
217
218 if ((rxon1->flags == rxon2->flags) &&
219 (rxon1->filter_flags == rxon2->filter_flags) &&
220 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
221 (rxon1->ofdm_ht_single_stream_basic_rates ==
222 rxon2->ofdm_ht_single_stream_basic_rates) &&
223 (rxon1->ofdm_ht_dual_stream_basic_rates ==
224 rxon2->ofdm_ht_dual_stream_basic_rates) &&
225 (rxon1->ofdm_ht_triple_stream_basic_rates ==
226 rxon2->ofdm_ht_triple_stream_basic_rates) &&
227 (rxon1->acquisition_data == rxon2->acquisition_data) &&
228 (rxon1->rx_chain == rxon2->rx_chain) &&
229 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
230 IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC. Not resending.\n");
231 return 0;
232 }
233
234 rxon_assoc.flags = ctx->staging.flags;
235 rxon_assoc.filter_flags = ctx->staging.filter_flags;
236 rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
237 rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
238 rxon_assoc.reserved1 = 0;
239 rxon_assoc.reserved2 = 0;
240 rxon_assoc.reserved3 = 0;
241 rxon_assoc.ofdm_ht_single_stream_basic_rates =
242 ctx->staging.ofdm_ht_single_stream_basic_rates;
243 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
244 ctx->staging.ofdm_ht_dual_stream_basic_rates;
245 rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
246 rxon_assoc.ofdm_ht_triple_stream_basic_rates =
247 ctx->staging.ofdm_ht_triple_stream_basic_rates;
248 rxon_assoc.acquisition_data = ctx->staging.acquisition_data;
249
250 ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_assoc_cmd,
251 CMD_ASYNC, sizeof(rxon_assoc), &rxon_assoc);
252 return ret;
253}
254
255static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
256{
257 u16 new_val;
258 u16 beacon_factor;
259
260 /*
261 * If mac80211 hasn't given us a beacon interval, program
262 * the default into the device (not checking this here
263 * would cause the adjustment below to return the maximum
264 * value, which may break PAN.)
265 */
266 if (!beacon_val)
267 return DEFAULT_BEACON_INTERVAL;
268
269 /*
270 * If the beacon interval we obtained from the peer
271 * is too large, we'll have to wake up more often
272 * (and in IBSS case, we'll beacon too much)
273 *
274 * For example, if max_beacon_val is 4096, and the
275 * requested beacon interval is 7000, we'll have to
276 * use 3500 to be able to wake up on the beacons.
277 *
278 * This could badly influence beacon detection stats.
279 */
280
281 beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
282 new_val = beacon_val / beacon_factor;
283
284 if (!new_val)
285 new_val = max_beacon_val;
286
287 return new_val;
288}
289
290static int iwl_send_rxon_timing(struct iwl_priv *priv,
291 struct iwl_rxon_context *ctx)
292{
293 u64 tsf;
294 s32 interval_tm, rem;
295 struct ieee80211_conf *conf = NULL;
296 u16 beacon_int;
297 struct ieee80211_vif *vif = ctx->vif;
298
299 conf = &priv->hw->conf;
300
301 lockdep_assert_held(&priv->mutex);
302
303 memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
304
305 ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
306 ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
307
308 beacon_int = vif ? vif->bss_conf.beacon_int : 0;
309
310 /*
311 * TODO: For IBSS we need to get atim_window from mac80211,
312 * for now just always use 0
313 */
314 ctx->timing.atim_window = 0;
315
316 if (ctx->ctxid == IWL_RXON_CTX_PAN &&
317 (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
318 iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
319 priv->contexts[IWL_RXON_CTX_BSS].vif &&
320 priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
321 ctx->timing.beacon_interval =
322 priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
323 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
324 } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
325 iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
326 priv->contexts[IWL_RXON_CTX_PAN].vif &&
327 priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
328 (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
329 !ctx->vif->bss_conf.beacon_int)) {
330 ctx->timing.beacon_interval =
331 priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
332 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
333 } else {
334 beacon_int = iwl_adjust_beacon_interval(beacon_int,
335 IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
336 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
337 }
338
339 ctx->beacon_int = beacon_int;
340
341 tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
342 interval_tm = beacon_int * TIME_UNIT;
343 rem = do_div(tsf, interval_tm);
344 ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
345
346 ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
347
348 IWL_DEBUG_ASSOC(priv,
349 "beacon interval %d beacon timer %d beacon tim %d\n",
350 le16_to_cpu(ctx->timing.beacon_interval),
351 le32_to_cpu(ctx->timing.beacon_init_val),
352 le16_to_cpu(ctx->timing.atim_window));
353
354 return iwl_dvm_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
355 CMD_SYNC, sizeof(ctx->timing), &ctx->timing);
356}
357
358static int iwlagn_rxon_disconn(struct iwl_priv *priv,
359 struct iwl_rxon_context *ctx)
360{
361 int ret;
362 struct iwl_rxon_cmd *active = (void *)&ctx->active;
363
364 if (ctx->ctxid == IWL_RXON_CTX_BSS) {
365 ret = iwlagn_disable_bss(priv, ctx, &ctx->staging);
366 } else {
367 ret = iwlagn_disable_pan(priv, ctx, &ctx->staging);
368 if (ret)
369 return ret;
370 if (ctx->vif) {
371 ret = iwl_send_rxon_timing(priv, ctx);
372 if (ret) {
373 IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
374 return ret;
375 }
376 ret = iwlagn_disconn_pan(priv, ctx, &ctx->staging);
377 }
378 }
379 if (ret)
380 return ret;
381
382 /*
383 * Un-assoc RXON clears the station table and WEP
384 * keys, so we have to restore those afterwards.
385 */
386 iwl_clear_ucode_stations(priv, ctx);
387 /* update -- might need P2P now */
388 iwl_update_bcast_station(priv, ctx);
389 iwl_restore_stations(priv, ctx);
390 ret = iwl_restore_default_wep_keys(priv, ctx);
391 if (ret) {
392 IWL_ERR(priv, "Failed to restore WEP keys (%d)\n", ret);
393 return ret;
394 }
395
396 memcpy(active, &ctx->staging, sizeof(*active));
397 return 0;
398}
399
400static int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
401{
402 int ret;
403 s8 prev_tx_power;
404 bool defer;
405 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
406
407 if (priv->calib_disabled & IWL_TX_POWER_CALIB_DISABLED)
408 return 0;
409
410 lockdep_assert_held(&priv->mutex);
411
412 if (priv->tx_power_user_lmt == tx_power && !force)
413 return 0;
414
415 if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
416 IWL_WARN(priv,
417 "Requested user TXPOWER %d below lower limit %d.\n",
418 tx_power,
419 IWLAGN_TX_POWER_TARGET_POWER_MIN);
420 return -EINVAL;
421 }
422
423 if (tx_power > priv->tx_power_device_lmt) {
424 IWL_WARN(priv,
425 "Requested user TXPOWER %d above upper limit %d.\n",
426 tx_power, priv->tx_power_device_lmt);
427 return -EINVAL;
428 }
429
430 if (!iwl_is_ready_rf(priv))
431 return -EIO;
432
433 /* scan complete and commit_rxon use tx_power_next value,
434 * it always need to be updated for newest request */
435 priv->tx_power_next = tx_power;
436
437 /* do not set tx power when scanning or channel changing */
438 defer = test_bit(STATUS_SCANNING, &priv->status) ||
439 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
440 if (defer && !force) {
441 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
442 return 0;
443 }
444
445 prev_tx_power = priv->tx_power_user_lmt;
446 priv->tx_power_user_lmt = tx_power;
447
448 ret = iwlagn_send_tx_power(priv);
449
450 /* if fail to set tx_power, restore the orig. tx power */
451 if (ret) {
452 priv->tx_power_user_lmt = prev_tx_power;
453 priv->tx_power_next = prev_tx_power;
454 }
455 return ret;
456}
457
458static int iwlagn_rxon_connect(struct iwl_priv *priv,
459 struct iwl_rxon_context *ctx)
460{
461 int ret;
462 struct iwl_rxon_cmd *active = (void *)&ctx->active;
463
464 /* RXON timing must be before associated RXON */
465 if (ctx->ctxid == IWL_RXON_CTX_BSS) {
466 ret = iwl_send_rxon_timing(priv, ctx);
467 if (ret) {
468 IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
469 return ret;
470 }
471 }
472 /* QoS info may be cleared by previous un-assoc RXON */
473 iwlagn_update_qos(priv, ctx);
474
475 /*
476 * We'll run into this code path when beaconing is
477 * enabled, but then we also need to send the beacon
478 * to the device.
479 */
480 if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_AP)) {
481 ret = iwlagn_update_beacon(priv, ctx->vif);
482 if (ret) {
483 IWL_ERR(priv,
484 "Error sending required beacon (%d)!\n",
485 ret);
486 return ret;
487 }
488 }
489
490 priv->start_calib = 0;
491 /*
492 * Apply the new configuration.
493 *
494 * Associated RXON doesn't clear the station table in uCode,
495 * so we don't need to restore stations etc. after this.
496 */
497 ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, CMD_SYNC,
498 sizeof(struct iwl_rxon_cmd), &ctx->staging);
499 if (ret) {
500 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
501 return ret;
502 }
503 memcpy(active, &ctx->staging, sizeof(*active));
504
505 /* IBSS beacon needs to be sent after setting assoc */
506 if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_ADHOC))
507 if (iwlagn_update_beacon(priv, ctx->vif))
508 IWL_ERR(priv, "Error sending IBSS beacon\n");
509 iwl_init_sensitivity(priv);
510
511 /*
512 * If we issue a new RXON command which required a tune then
513 * we must send a new TXPOWER command or we won't be able to
514 * Tx any frames.
515 *
516 * It's expected we set power here if channel is changing.
517 */
518 ret = iwl_set_tx_power(priv, priv->tx_power_next, true);
519 if (ret) {
520 IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
521 return ret;
522 }
523
524 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
525 priv->cfg->ht_params && priv->cfg->ht_params->smps_mode)
526 ieee80211_request_smps(ctx->vif,
527 priv->cfg->ht_params->smps_mode);
528
529 return 0;
530}
531
532int iwlagn_set_pan_params(struct iwl_priv *priv)
533{
534 struct iwl_wipan_params_cmd cmd;
535 struct iwl_rxon_context *ctx_bss, *ctx_pan;
536 int slot0 = 300, slot1 = 0;
537 int ret;
538
539 if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
540 return 0;
541
542 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
543
544 lockdep_assert_held(&priv->mutex);
545
546 ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
547 ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
548
549 /*
550 * If the PAN context is inactive, then we don't need
551 * to update the PAN parameters, the last thing we'll
552 * have done before it goes inactive is making the PAN
553 * parameters be WLAN-only.
554 */
555 if (!ctx_pan->is_active)
556 return 0;
557
558 memset(&cmd, 0, sizeof(cmd));
559
560 /* only 2 slots are currently allowed */
561 cmd.num_slots = 2;
562
563 cmd.slots[0].type = 0; /* BSS */
564 cmd.slots[1].type = 1; /* PAN */
565
566 if (priv->hw_roc_setup) {
567 /* both contexts must be used for this to happen */
568 slot1 = IWL_MIN_SLOT_TIME;
569 slot0 = 3000;
570 } else if (ctx_bss->vif && ctx_pan->vif) {
571 int bcnint = ctx_pan->beacon_int;
572 int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
573
574 /* should be set, but seems unused?? */
575 cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
576
577 if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
578 bcnint &&
579 bcnint != ctx_bss->beacon_int) {
580 IWL_ERR(priv,
581 "beacon intervals don't match (%d, %d)\n",
582 ctx_bss->beacon_int, ctx_pan->beacon_int);
583 } else
584 bcnint = max_t(int, bcnint,
585 ctx_bss->beacon_int);
586 if (!bcnint)
587 bcnint = DEFAULT_BEACON_INTERVAL;
588 slot0 = bcnint / 2;
589 slot1 = bcnint - slot0;
590
591 if (test_bit(STATUS_SCAN_HW, &priv->status) ||
592 (!ctx_bss->vif->bss_conf.idle &&
593 !ctx_bss->vif->bss_conf.assoc)) {
594 slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
595 slot1 = IWL_MIN_SLOT_TIME;
596 } else if (!ctx_pan->vif->bss_conf.idle &&
597 !ctx_pan->vif->bss_conf.assoc) {
598 slot1 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
599 slot0 = IWL_MIN_SLOT_TIME;
600 }
601 } else if (ctx_pan->vif) {
602 slot0 = 0;
603 slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
604 ctx_pan->beacon_int;
605 slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
606
607 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
608 slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
609 slot1 = IWL_MIN_SLOT_TIME;
610 }
611 }
612
613 cmd.slots[0].width = cpu_to_le16(slot0);
614 cmd.slots[1].width = cpu_to_le16(slot1);
615
616 ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, CMD_SYNC,
617 sizeof(cmd), &cmd);
618 if (ret)
619 IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
620
621 return ret;
622}
623
624static void _iwl_set_rxon_ht(struct iwl_priv *priv,
625 struct iwl_ht_config *ht_conf,
626 struct iwl_rxon_context *ctx)
627{
628 struct iwl_rxon_cmd *rxon = &ctx->staging;
629
630 if (!ctx->ht.enabled) {
631 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
632 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
633 RXON_FLG_HT40_PROT_MSK |
634 RXON_FLG_HT_PROT_MSK);
635 return;
636 }
637
638 /* FIXME: if the definition of ht.protection changed, the "translation"
639 * will be needed for rxon->flags
640 */
641 rxon->flags |= cpu_to_le32(ctx->ht.protection <<
642 RXON_FLG_HT_OPERATING_MODE_POS);
643
644 /* Set up channel bandwidth:
645 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
646 /* clear the HT channel mode before set the mode */
647 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
648 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
649 if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
650 /* pure ht40 */
651 if (ctx->ht.protection ==
652 IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
653 rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
654 /*
655 * Note: control channel is opposite of extension
656 * channel
657 */
658 switch (ctx->ht.extension_chan_offset) {
659 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
660 rxon->flags &=
661 ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
662 break;
663 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
664 rxon->flags |=
665 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
666 break;
667 }
668 } else {
669 /*
670 * Note: control channel is opposite of extension
671 * channel
672 */
673 switch (ctx->ht.extension_chan_offset) {
674 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
675 rxon->flags &=
676 ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
677 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
678 break;
679 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
680 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
681 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
682 break;
683 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
684 default:
685 /*
686 * channel location only valid if in Mixed
687 * mode
688 */
689 IWL_ERR(priv,
690 "invalid extension channel offset\n");
691 break;
692 }
693 }
694 } else {
695 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
696 }
697
698 iwlagn_set_rxon_chain(priv, ctx);
699
700 IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
701 "extension channel offset 0x%x\n",
702 le32_to_cpu(rxon->flags), ctx->ht.protection,
703 ctx->ht.extension_chan_offset);
704}
705
706void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
707{
708 struct iwl_rxon_context *ctx;
709
710 for_each_context(priv, ctx)
711 _iwl_set_rxon_ht(priv, ht_conf, ctx);
712}
713
714/**
715 * iwl_set_rxon_channel - Set the band and channel values in staging RXON
716 * @ch: requested channel as a pointer to struct ieee80211_channel
717
718 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
719 * in the staging RXON flag structure based on the ch->band
720 */
721void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
722 struct iwl_rxon_context *ctx)
723{
724 enum ieee80211_band band = ch->band;
725 u16 channel = ch->hw_value;
726
727 if ((le16_to_cpu(ctx->staging.channel) == channel) &&
728 (priv->band == band))
729 return;
730
731 ctx->staging.channel = cpu_to_le16(channel);
732 if (band == IEEE80211_BAND_5GHZ)
733 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
734 else
735 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
736
737 priv->band = band;
738
739 IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
740
741}
742
743void iwl_set_flags_for_band(struct iwl_priv *priv,
744 struct iwl_rxon_context *ctx,
745 enum ieee80211_band band,
746 struct ieee80211_vif *vif)
747{
748 if (band == IEEE80211_BAND_5GHZ) {
749 ctx->staging.flags &=
750 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
751 | RXON_FLG_CCK_MSK);
752 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
753 } else {
754 /* Copied from iwl_post_associate() */
755 if (vif && vif->bss_conf.use_short_slot)
756 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
757 else
758 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
759
760 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
761 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
762 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
763 }
764}
765
766static void iwl_set_rxon_hwcrypto(struct iwl_priv *priv,
767 struct iwl_rxon_context *ctx, int hw_decrypt)
768{
769 struct iwl_rxon_cmd *rxon = &ctx->staging;
770
771 if (hw_decrypt)
772 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
773 else
774 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
775
776}
777
778/* validate RXON structure is valid */
779static int iwl_check_rxon_cmd(struct iwl_priv *priv,
780 struct iwl_rxon_context *ctx)
781{
782 struct iwl_rxon_cmd *rxon = &ctx->staging;
783 u32 errors = 0;
784
785 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
786 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
787 IWL_WARN(priv, "check 2.4G: wrong narrow\n");
788 errors |= BIT(0);
789 }
790 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
791 IWL_WARN(priv, "check 2.4G: wrong radar\n");
792 errors |= BIT(1);
793 }
794 } else {
795 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
796 IWL_WARN(priv, "check 5.2G: not short slot!\n");
797 errors |= BIT(2);
798 }
799 if (rxon->flags & RXON_FLG_CCK_MSK) {
800 IWL_WARN(priv, "check 5.2G: CCK!\n");
801 errors |= BIT(3);
802 }
803 }
804 if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
805 IWL_WARN(priv, "mac/bssid mcast!\n");
806 errors |= BIT(4);
807 }
808
809 /* make sure basic rates 6Mbps and 1Mbps are supported */
810 if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
811 (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
812 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
813 errors |= BIT(5);
814 }
815
816 if (le16_to_cpu(rxon->assoc_id) > 2007) {
817 IWL_WARN(priv, "aid > 2007\n");
818 errors |= BIT(6);
819 }
820
821 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
822 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
823 IWL_WARN(priv, "CCK and short slot\n");
824 errors |= BIT(7);
825 }
826
827 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
828 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
829 IWL_WARN(priv, "CCK and auto detect");
830 errors |= BIT(8);
831 }
832
833 if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
834 RXON_FLG_TGG_PROTECT_MSK)) ==
835 RXON_FLG_TGG_PROTECT_MSK) {
836 IWL_WARN(priv, "TGg but no auto-detect\n");
837 errors |= BIT(9);
838 }
839
840 if (rxon->channel == 0) {
841 IWL_WARN(priv, "zero channel is invalid\n");
842 errors |= BIT(10);
843 }
844
845 WARN(errors, "Invalid RXON (%#x), channel %d",
846 errors, le16_to_cpu(rxon->channel));
847
848 return errors ? -EINVAL : 0;
849}
850
851/**
852 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
853 * @priv: staging_rxon is compared to active_rxon
854 *
855 * If the RXON structure is changing enough to require a new tune,
856 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
857 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
858 */
859static int iwl_full_rxon_required(struct iwl_priv *priv,
860 struct iwl_rxon_context *ctx)
861{
862 const struct iwl_rxon_cmd *staging = &ctx->staging;
863 const struct iwl_rxon_cmd *active = &ctx->active;
864
865#define CHK(cond) \
866 if ((cond)) { \
867 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
868 return 1; \
869 }
870
871#define CHK_NEQ(c1, c2) \
872 if ((c1) != (c2)) { \
873 IWL_DEBUG_INFO(priv, "need full RXON - " \
874 #c1 " != " #c2 " - %d != %d\n", \
875 (c1), (c2)); \
876 return 1; \
877 }
878
879 /* These items are only settable from the full RXON command */
880 CHK(!iwl_is_associated_ctx(ctx));
881 CHK(!ether_addr_equal(staging->bssid_addr, active->bssid_addr));
882 CHK(!ether_addr_equal(staging->node_addr, active->node_addr));
883 CHK(!ether_addr_equal(staging->wlap_bssid_addr,
884 active->wlap_bssid_addr));
885 CHK_NEQ(staging->dev_type, active->dev_type);
886 CHK_NEQ(staging->channel, active->channel);
887 CHK_NEQ(staging->air_propagation, active->air_propagation);
888 CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
889 active->ofdm_ht_single_stream_basic_rates);
890 CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
891 active->ofdm_ht_dual_stream_basic_rates);
892 CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
893 active->ofdm_ht_triple_stream_basic_rates);
894 CHK_NEQ(staging->assoc_id, active->assoc_id);
895
896 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
897 * be updated with the RXON_ASSOC command -- however only some
898 * flag transitions are allowed using RXON_ASSOC */
899
900 /* Check if we are not switching bands */
901 CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
902 active->flags & RXON_FLG_BAND_24G_MSK);
903
904 /* Check if we are switching association toggle */
905 CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
906 active->filter_flags & RXON_FILTER_ASSOC_MSK);
907
908#undef CHK
909#undef CHK_NEQ
910
911 return 0;
912}
913
914#ifdef CONFIG_IWLWIFI_DEBUG
915void iwl_print_rx_config_cmd(struct iwl_priv *priv,
916 enum iwl_rxon_context_id ctxid)
917{
918 struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
919 struct iwl_rxon_cmd *rxon = &ctx->staging;
920
921 IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
922 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
923 IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n",
924 le16_to_cpu(rxon->channel));
925 IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n",
926 le32_to_cpu(rxon->flags));
927 IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
928 le32_to_cpu(rxon->filter_flags));
929 IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
930 IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
931 rxon->ofdm_basic_rates);
932 IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n",
933 rxon->cck_basic_rates);
934 IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
935 IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
936 IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n",
937 le16_to_cpu(rxon->assoc_id));
938}
939#endif
940
941static void iwl_calc_basic_rates(struct iwl_priv *priv,
942 struct iwl_rxon_context *ctx)
943{
944 int lowest_present_ofdm = 100;
945 int lowest_present_cck = 100;
946 u8 cck = 0;
947 u8 ofdm = 0;
948
949 if (ctx->vif) {
950 struct ieee80211_supported_band *sband;
951 unsigned long basic = ctx->vif->bss_conf.basic_rates;
952 int i;
953
954 sband = priv->hw->wiphy->bands[priv->hw->conf.channel->band];
955
956 for_each_set_bit(i, &basic, BITS_PER_LONG) {
957 int hw = sband->bitrates[i].hw_value;
958 if (hw >= IWL_FIRST_OFDM_RATE) {
959 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
960 if (lowest_present_ofdm > hw)
961 lowest_present_ofdm = hw;
962 } else {
963 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
964
965 cck |= BIT(hw);
966 if (lowest_present_cck > hw)
967 lowest_present_cck = hw;
968 }
969 }
970 }
971
972 /*
973 * Now we've got the basic rates as bitmaps in the ofdm and cck
974 * variables. This isn't sufficient though, as there might not
975 * be all the right rates in the bitmap. E.g. if the only basic
976 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
977 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
978 *
979 * [...] a STA responding to a received frame shall transmit
980 * its Control Response frame [...] at the highest rate in the
981 * BSSBasicRateSet parameter that is less than or equal to the
982 * rate of the immediately previous frame in the frame exchange
983 * sequence ([...]) and that is of the same modulation class
984 * ([...]) as the received frame. If no rate contained in the
985 * BSSBasicRateSet parameter meets these conditions, then the
986 * control frame sent in response to a received frame shall be
987 * transmitted at the highest mandatory rate of the PHY that is
988 * less than or equal to the rate of the received frame, and
989 * that is of the same modulation class as the received frame.
990 *
991 * As a consequence, we need to add all mandatory rates that are
992 * lower than all of the basic rates to these bitmaps.
993 */
994
995 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
996 ofdm |= IWL_RATE_24M_MASK >> IWL_FIRST_OFDM_RATE;
997 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
998 ofdm |= IWL_RATE_12M_MASK >> IWL_FIRST_OFDM_RATE;
999 /* 6M already there or needed so always add */
1000 ofdm |= IWL_RATE_6M_MASK >> IWL_FIRST_OFDM_RATE;
1001
1002 /*
1003 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
1004 * Note, however:
1005 * - if no CCK rates are basic, it must be ERP since there must
1006 * be some basic rates at all, so they're OFDM => ERP PHY
1007 * (or we're in 5 GHz, and the cck bitmap will never be used)
1008 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
1009 * - if 5.5M is basic, 1M and 2M are mandatory
1010 * - if 2M is basic, 1M is mandatory
1011 * - if 1M is basic, that's the only valid ACK rate.
1012 * As a consequence, it's not as complicated as it sounds, just add
1013 * any lower rates to the ACK rate bitmap.
1014 */
1015 if (IWL_RATE_11M_INDEX < lowest_present_ofdm)
1016 ofdm |= IWL_RATE_11M_MASK >> IWL_FIRST_CCK_RATE;
1017 if (IWL_RATE_5M_INDEX < lowest_present_ofdm)
1018 ofdm |= IWL_RATE_5M_MASK >> IWL_FIRST_CCK_RATE;
1019 if (IWL_RATE_2M_INDEX < lowest_present_ofdm)
1020 ofdm |= IWL_RATE_2M_MASK >> IWL_FIRST_CCK_RATE;
1021 /* 1M already there or needed so always add */
1022 cck |= IWL_RATE_1M_MASK >> IWL_FIRST_CCK_RATE;
1023
1024 IWL_DEBUG_RATE(priv, "Set basic rates cck:0x%.2x ofdm:0x%.2x\n",
1025 cck, ofdm);
1026
1027 /* "basic_rates" is a misnomer here -- should be called ACK rates */
1028 ctx->staging.cck_basic_rates = cck;
1029 ctx->staging.ofdm_basic_rates = ofdm;
1030}
1031
1032/**
1033 * iwlagn_commit_rxon - commit staging_rxon to hardware
1034 *
1035 * The RXON command in staging_rxon is committed to the hardware and
1036 * the active_rxon structure is updated with the new data. This
1037 * function correctly transitions out of the RXON_ASSOC_MSK state if
1038 * a HW tune is required based on the RXON structure changes.
1039 *
1040 * The connect/disconnect flow should be as the following:
1041 *
1042 * 1. make sure send RXON command with association bit unset if not connect
1043 * this should include the channel and the band for the candidate
1044 * to be connected to
1045 * 2. Add Station before RXON association with the AP
1046 * 3. RXON_timing has to send before RXON for connection
1047 * 4. full RXON command - associated bit set
1048 * 5. use RXON_ASSOC command to update any flags changes
1049 */
1050int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1051{
1052 /* cast away the const for active_rxon in this function */
1053 struct iwl_rxon_cmd *active = (void *)&ctx->active;
1054 bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
1055 int ret;
1056
1057 lockdep_assert_held(&priv->mutex);
1058
1059 if (!iwl_is_alive(priv))
1060 return -EBUSY;
1061
1062 /* This function hardcodes a bunch of dual-mode assumptions */
1063 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1064
1065 if (!ctx->is_active)
1066 return 0;
1067
1068 /* always get timestamp with Rx frame */
1069 ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
1070
1071 /* recalculate basic rates */
1072 iwl_calc_basic_rates(priv, ctx);
1073
1074 /*
1075 * force CTS-to-self frames protection if RTS-CTS is not preferred
1076 * one aggregation protection method
1077 */
1078 if (!priv->hw_params.use_rts_for_aggregation)
1079 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1080
1081 if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
1082 !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
1083 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
1084 else
1085 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1086
1087 iwl_print_rx_config_cmd(priv, ctx->ctxid);
1088 ret = iwl_check_rxon_cmd(priv, ctx);
1089 if (ret) {
1090 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
1091 return -EINVAL;
1092 }
1093
1094 /*
1095 * receive commit_rxon request
1096 * abort any previous channel switch if still in process
1097 */
1098 if (test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status) &&
1099 (priv->switch_channel != ctx->staging.channel)) {
1100 IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
1101 le16_to_cpu(priv->switch_channel));
1102 iwl_chswitch_done(priv, false);
1103 }
1104
1105 /*
1106 * If we don't need to send a full RXON, we can use
1107 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1108 * and other flags for the current radio configuration.
1109 */
1110 if (!iwl_full_rxon_required(priv, ctx)) {
1111 ret = iwlagn_send_rxon_assoc(priv, ctx);
1112 if (ret) {
1113 IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
1114 return ret;
1115 }
1116
1117 memcpy(active, &ctx->staging, sizeof(*active));
1118 /*
1119 * We do not commit tx power settings while channel changing,
1120 * do it now if after settings changed.
1121 */
1122 iwl_set_tx_power(priv, priv->tx_power_next, false);
1123
1124 /* make sure we are in the right PS state */
1125 iwl_power_update_mode(priv, true);
1126
1127 return 0;
1128 }
1129
1130 iwl_set_rxon_hwcrypto(priv, ctx, !iwlwifi_mod_params.sw_crypto);
1131
1132 IWL_DEBUG_INFO(priv,
1133 "Going to commit RXON\n"
1134 " * with%s RXON_FILTER_ASSOC_MSK\n"
1135 " * channel = %d\n"
1136 " * bssid = %pM\n",
1137 (new_assoc ? "" : "out"),
1138 le16_to_cpu(ctx->staging.channel),
1139 ctx->staging.bssid_addr);
1140
1141 /*
1142 * Always clear associated first, but with the correct config.
1143 * This is required as for example station addition for the
1144 * AP station must be done after the BSSID is set to correctly
1145 * set up filters in the device.
1146 */
1147 ret = iwlagn_rxon_disconn(priv, ctx);
1148 if (ret)
1149 return ret;
1150
1151 ret = iwlagn_set_pan_params(priv);
1152 if (ret)
1153 return ret;
1154
1155 if (new_assoc)
1156 return iwlagn_rxon_connect(priv, ctx);
1157
1158 return 0;
1159}
1160
1161void iwlagn_config_ht40(struct ieee80211_conf *conf,
1162 struct iwl_rxon_context *ctx)
1163{
1164 if (conf_is_ht40_minus(conf)) {
1165 ctx->ht.extension_chan_offset =
1166 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1167 ctx->ht.is_40mhz = true;
1168 } else if (conf_is_ht40_plus(conf)) {
1169 ctx->ht.extension_chan_offset =
1170 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1171 ctx->ht.is_40mhz = true;
1172 } else {
1173 ctx->ht.extension_chan_offset =
1174 IEEE80211_HT_PARAM_CHA_SEC_NONE;
1175 ctx->ht.is_40mhz = false;
1176 }
1177}
1178
1179int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
1180{
1181 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1182 struct iwl_rxon_context *ctx;
1183 struct ieee80211_conf *conf = &hw->conf;
1184 struct ieee80211_channel *channel = conf->channel;
1185 int ret = 0;
1186
1187 IWL_DEBUG_MAC80211(priv, "enter: changed %#x\n", changed);
1188
1189 mutex_lock(&priv->mutex);
1190
1191 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
1192 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
1193 goto out;
1194 }
1195
1196 if (!iwl_is_ready(priv)) {
1197 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
1198 goto out;
1199 }
1200
1201 if (changed & (IEEE80211_CONF_CHANGE_SMPS |
1202 IEEE80211_CONF_CHANGE_CHANNEL)) {
1203 /* mac80211 uses static for non-HT which is what we want */
1204 priv->current_ht_config.smps = conf->smps_mode;
1205
1206 /*
1207 * Recalculate chain counts.
1208 *
1209 * If monitor mode is enabled then mac80211 will
1210 * set up the SM PS mode to OFF if an HT channel is
1211 * configured.
1212 */
1213 for_each_context(priv, ctx)
1214 iwlagn_set_rxon_chain(priv, ctx);
1215 }
1216
1217 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1218 for_each_context(priv, ctx) {
1219 /* Configure HT40 channels */
1220 if (ctx->ht.enabled != conf_is_ht(conf))
1221 ctx->ht.enabled = conf_is_ht(conf);
1222
1223 if (ctx->ht.enabled) {
1224 /* if HT40 is used, it should not change
1225 * after associated except channel switch */
1226 if (!ctx->ht.is_40mhz ||
1227 !iwl_is_associated_ctx(ctx))
1228 iwlagn_config_ht40(conf, ctx);
1229 } else
1230 ctx->ht.is_40mhz = false;
1231
1232 /*
1233 * Default to no protection. Protection mode will
1234 * later be set from BSS config in iwl_ht_conf
1235 */
1236 ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
1237
1238 /* if we are switching from ht to 2.4 clear flags
1239 * from any ht related info since 2.4 does not
1240 * support ht */
1241 if (le16_to_cpu(ctx->staging.channel) !=
1242 channel->hw_value)
1243 ctx->staging.flags = 0;
1244
1245 iwl_set_rxon_channel(priv, channel, ctx);
1246 iwl_set_rxon_ht(priv, &priv->current_ht_config);
1247
1248 iwl_set_flags_for_band(priv, ctx, channel->band,
1249 ctx->vif);
1250 }
1251
1252 iwl_update_bcast_stations(priv);
1253 }
1254
1255 if (changed & (IEEE80211_CONF_CHANGE_PS |
1256 IEEE80211_CONF_CHANGE_IDLE)) {
1257 ret = iwl_power_update_mode(priv, false);
1258 if (ret)
1259 IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
1260 }
1261
1262 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1263 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
1264 priv->tx_power_user_lmt, conf->power_level);
1265
1266 iwl_set_tx_power(priv, conf->power_level, false);
1267 }
1268
1269 for_each_context(priv, ctx) {
1270 if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1271 continue;
1272 iwlagn_commit_rxon(priv, ctx);
1273 }
1274 out:
1275 mutex_unlock(&priv->mutex);
1276 IWL_DEBUG_MAC80211(priv, "leave\n");
1277
1278 return ret;
1279}
1280
1281static void iwlagn_check_needed_chains(struct iwl_priv *priv,
1282 struct iwl_rxon_context *ctx,
1283 struct ieee80211_bss_conf *bss_conf)
1284{
1285 struct ieee80211_vif *vif = ctx->vif;
1286 struct iwl_rxon_context *tmp;
1287 struct ieee80211_sta *sta;
1288 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1289 struct ieee80211_sta_ht_cap *ht_cap;
1290 bool need_multiple;
1291
1292 lockdep_assert_held(&priv->mutex);
1293
1294 switch (vif->type) {
1295 case NL80211_IFTYPE_STATION:
1296 rcu_read_lock();
1297 sta = ieee80211_find_sta(vif, bss_conf->bssid);
1298 if (!sta) {
1299 /*
1300 * If at all, this can only happen through a race
1301 * when the AP disconnects us while we're still
1302 * setting up the connection, in that case mac80211
1303 * will soon tell us about that.
1304 */
1305 need_multiple = false;
1306 rcu_read_unlock();
1307 break;
1308 }
1309
1310 ht_cap = &sta->ht_cap;
1311
1312 need_multiple = true;
1313
1314 /*
1315 * If the peer advertises no support for receiving 2 and 3
1316 * stream MCS rates, it can't be transmitting them either.
1317 */
1318 if (ht_cap->mcs.rx_mask[1] == 0 &&
1319 ht_cap->mcs.rx_mask[2] == 0) {
1320 need_multiple = false;
1321 } else if (!(ht_cap->mcs.tx_params &
1322 IEEE80211_HT_MCS_TX_DEFINED)) {
1323 /* If it can't TX MCS at all ... */
1324 need_multiple = false;
1325 } else if (ht_cap->mcs.tx_params &
1326 IEEE80211_HT_MCS_TX_RX_DIFF) {
1327 int maxstreams;
1328
1329 /*
1330 * But if it can receive them, it might still not
1331 * be able to transmit them, which is what we need
1332 * to check here -- so check the number of streams
1333 * it advertises for TX (if different from RX).
1334 */
1335
1336 maxstreams = (ht_cap->mcs.tx_params &
1337 IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK);
1338 maxstreams >>=
1339 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1340 maxstreams += 1;
1341
1342 if (maxstreams <= 1)
1343 need_multiple = false;
1344 }
1345
1346 rcu_read_unlock();
1347 break;
1348 case NL80211_IFTYPE_ADHOC:
1349 /* currently */
1350 need_multiple = false;
1351 break;
1352 default:
1353 /* only AP really */
1354 need_multiple = true;
1355 break;
1356 }
1357
1358 ctx->ht_need_multiple_chains = need_multiple;
1359
1360 if (!need_multiple) {
1361 /* check all contexts */
1362 for_each_context(priv, tmp) {
1363 if (!tmp->vif)
1364 continue;
1365 if (tmp->ht_need_multiple_chains) {
1366 need_multiple = true;
1367 break;
1368 }
1369 }
1370 }
1371
1372 ht_conf->single_chain_sufficient = !need_multiple;
1373}
1374
1375static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
1376{
1377 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
1378 int ret;
1379
1380 if (!(priv->calib_disabled & IWL_CHAIN_NOISE_CALIB_DISABLED))
1381 return;
1382
1383 if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
1384 iwl_is_any_associated(priv)) {
1385 struct iwl_calib_chain_noise_reset_cmd cmd;
1386
1387 /* clear data for chain noise calibration algorithm */
1388 data->chain_noise_a = 0;
1389 data->chain_noise_b = 0;
1390 data->chain_noise_c = 0;
1391 data->chain_signal_a = 0;
1392 data->chain_signal_b = 0;
1393 data->chain_signal_c = 0;
1394 data->beacon_count = 0;
1395
1396 memset(&cmd, 0, sizeof(cmd));
1397 iwl_set_calib_hdr(&cmd.hdr,
1398 priv->phy_calib_chain_noise_reset_cmd);
1399 ret = iwl_dvm_send_cmd_pdu(priv,
1400 REPLY_PHY_CALIBRATION_CMD,
1401 CMD_SYNC, sizeof(cmd), &cmd);
1402 if (ret)
1403 IWL_ERR(priv,
1404 "Could not send REPLY_PHY_CALIBRATION_CMD\n");
1405 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
1406 IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
1407 }
1408}
1409
1410void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
1411 struct ieee80211_vif *vif,
1412 struct ieee80211_bss_conf *bss_conf,
1413 u32 changes)
1414{
1415 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1416 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1417 int ret;
1418 bool force = false;
1419
1420 mutex_lock(&priv->mutex);
1421
1422 if (unlikely(!iwl_is_ready(priv))) {
1423 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
1424 mutex_unlock(&priv->mutex);
1425 return;
1426 }
1427
1428 if (unlikely(!ctx->vif)) {
1429 IWL_DEBUG_MAC80211(priv, "leave - vif is NULL\n");
1430 mutex_unlock(&priv->mutex);
1431 return;
1432 }
1433
1434 if (changes & BSS_CHANGED_BEACON_INT)
1435 force = true;
1436
1437 if (changes & BSS_CHANGED_QOS) {
1438 ctx->qos_data.qos_active = bss_conf->qos;
1439 iwlagn_update_qos(priv, ctx);
1440 }
1441
1442 ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
1443 if (vif->bss_conf.use_short_preamble)
1444 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1445 else
1446 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1447
1448 if (changes & BSS_CHANGED_ASSOC) {
1449 if (bss_conf->assoc) {
1450 priv->timestamp = bss_conf->last_tsf;
1451 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
1452 } else {
1453 /*
1454 * If we disassociate while there are pending
1455 * frames, just wake up the queues and let the
1456 * frames "escape" ... This shouldn't really
1457 * be happening to start with, but we should
1458 * not get stuck in this case either since it
1459 * can happen if userspace gets confused.
1460 */
1461 iwlagn_lift_passive_no_rx(priv);
1462
1463 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1464
1465 if (ctx->ctxid == IWL_RXON_CTX_BSS)
1466 priv->have_rekey_data = false;
1467 }
1468
1469 iwlagn_bt_coex_rssi_monitor(priv);
1470 }
1471
1472 if (ctx->ht.enabled) {
1473 ctx->ht.protection = bss_conf->ht_operation_mode &
1474 IEEE80211_HT_OP_MODE_PROTECTION;
1475 ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
1476 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1477 iwlagn_check_needed_chains(priv, ctx, bss_conf);
1478 iwl_set_rxon_ht(priv, &priv->current_ht_config);
1479 }
1480
1481 iwlagn_set_rxon_chain(priv, ctx);
1482
1483 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
1484 ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
1485 else
1486 ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
1487
1488 if (bss_conf->use_cts_prot)
1489 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1490 else
1491 ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
1492
1493 memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
1494
1495 if (vif->type == NL80211_IFTYPE_AP ||
1496 vif->type == NL80211_IFTYPE_ADHOC) {
1497 if (vif->bss_conf.enable_beacon) {
1498 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
1499 priv->beacon_ctx = ctx;
1500 } else {
1501 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1502 priv->beacon_ctx = NULL;
1503 }
1504 }
1505
1506 /*
1507 * If the ucode decides to do beacon filtering before
1508 * association, it will lose beacons that are needed
1509 * before sending frames out on passive channels. This
1510 * causes association failures on those channels. Enable
1511 * receiving beacons in such cases.
1512 */
1513
1514 if (vif->type == NL80211_IFTYPE_STATION) {
1515 if (!bss_conf->assoc)
1516 ctx->staging.filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
1517 else
1518 ctx->staging.filter_flags &=
1519 ~RXON_FILTER_BCON_AWARE_MSK;
1520 }
1521
1522 if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1523 iwlagn_commit_rxon(priv, ctx);
1524
1525 if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
1526 /*
1527 * The chain noise calibration will enable PM upon
1528 * completion. If calibration has already been run
1529 * then we need to enable power management here.
1530 */
1531 if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
1532 iwl_power_update_mode(priv, false);
1533
1534 /* Enable RX differential gain and sensitivity calibrations */
1535 iwlagn_chain_noise_reset(priv);
1536 priv->start_calib = 1;
1537 }
1538
1539 if (changes & BSS_CHANGED_IBSS) {
1540 ret = iwlagn_manage_ibss_station(priv, vif,
1541 bss_conf->ibss_joined);
1542 if (ret)
1543 IWL_ERR(priv, "failed to %s IBSS station %pM\n",
1544 bss_conf->ibss_joined ? "add" : "remove",
1545 bss_conf->bssid);
1546 }
1547
1548 if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_ADHOC &&
1549 priv->beacon_ctx) {
1550 if (iwlagn_update_beacon(priv, vif))
1551 IWL_ERR(priv, "Error sending IBSS beacon\n");
1552 }
1553
1554 mutex_unlock(&priv->mutex);
1555}
1556
1557void iwlagn_post_scan(struct iwl_priv *priv)
1558{
1559 struct iwl_rxon_context *ctx;
1560
1561 /*
1562 * We do not commit power settings while scan is pending,
1563 * do it now if the settings changed.
1564 */
1565 iwl_power_set_mode(priv, &priv->power_data.sleep_cmd_next, false);
1566 iwl_set_tx_power(priv, priv->tx_power_next, false);
1567
1568 /*
1569 * Since setting the RXON may have been deferred while
1570 * performing the scan, fire one off if needed
1571 */
1572 for_each_context(priv, ctx)
1573 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1574 iwlagn_commit_rxon(priv, ctx);
1575
1576 iwlagn_set_pan_params(priv);
1577}
diff --git a/drivers/net/wireless/iwlwifi/dvm/scan.c b/drivers/net/wireless/iwlwifi/dvm/scan.c
new file mode 100644
index 000000000000..7ee7f32b0fab
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/scan.c
@@ -0,0 +1,1169 @@
1/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28#include <linux/slab.h>
29#include <linux/types.h>
30#include <linux/etherdevice.h>
31#include <net/mac80211.h>
32
33#include "eeprom.h"
34#include "dev.h"
35#include "agn.h"
36
37/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
38 * sending probe req. This should be set long enough to hear probe responses
39 * from more than one AP. */
40#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
41#define IWL_ACTIVE_DWELL_TIME_52 (20)
42
43#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
44#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
45
46/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
47 * Must be set longer than active dwell time.
48 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
49#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
50#define IWL_PASSIVE_DWELL_TIME_52 (10)
51#define IWL_PASSIVE_DWELL_BASE (100)
52#define IWL_CHANNEL_TUNE_TIME 5
53#define MAX_SCAN_CHANNEL 50
54
55static int iwl_send_scan_abort(struct iwl_priv *priv)
56{
57 int ret;
58 struct iwl_host_cmd cmd = {
59 .id = REPLY_SCAN_ABORT_CMD,
60 .flags = CMD_SYNC | CMD_WANT_SKB,
61 };
62 __le32 *status;
63
64 /* Exit instantly with error when device is not ready
65 * to receive scan abort command or it does not perform
66 * hardware scan currently */
67 if (!test_bit(STATUS_READY, &priv->status) ||
68 !test_bit(STATUS_GEO_CONFIGURED, &priv->status) ||
69 !test_bit(STATUS_SCAN_HW, &priv->status) ||
70 test_bit(STATUS_FW_ERROR, &priv->status))
71 return -EIO;
72
73 ret = iwl_dvm_send_cmd(priv, &cmd);
74 if (ret)
75 return ret;
76
77 status = (void *)cmd.resp_pkt->data;
78 if (*status != CAN_ABORT_STATUS) {
79 /* The scan abort will return 1 for success or
80 * 2 for "failure". A failure condition can be
81 * due to simply not being in an active scan which
82 * can occur if we send the scan abort before we
83 * the microcode has notified us that a scan is
84 * completed. */
85 IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n",
86 le32_to_cpu(*status));
87 ret = -EIO;
88 }
89
90 iwl_free_resp(&cmd);
91 return ret;
92}
93
94static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
95{
96 /* check if scan was requested from mac80211 */
97 if (priv->scan_request) {
98 IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
99 ieee80211_scan_completed(priv->hw, aborted);
100 }
101
102 if (priv->scan_type == IWL_SCAN_ROC)
103 iwl_scan_roc_expired(priv);
104
105 priv->scan_type = IWL_SCAN_NORMAL;
106 priv->scan_vif = NULL;
107 priv->scan_request = NULL;
108}
109
110static void iwl_process_scan_complete(struct iwl_priv *priv)
111{
112 bool aborted;
113
114 lockdep_assert_held(&priv->mutex);
115
116 if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->status))
117 return;
118
119 IWL_DEBUG_SCAN(priv, "Completed scan.\n");
120
121 cancel_delayed_work(&priv->scan_check);
122
123 aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status);
124 if (aborted)
125 IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
126
127 if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) {
128 IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
129 goto out_settings;
130 }
131
132 if (priv->scan_type == IWL_SCAN_ROC)
133 iwl_scan_roc_expired(priv);
134
135 if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
136 int err;
137
138 /* Check if mac80211 requested scan during our internal scan */
139 if (priv->scan_request == NULL)
140 goto out_complete;
141
142 /* If so request a new scan */
143 err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
144 priv->scan_request->channels[0]->band);
145 if (err) {
146 IWL_DEBUG_SCAN(priv,
147 "failed to initiate pending scan: %d\n", err);
148 aborted = true;
149 goto out_complete;
150 }
151
152 return;
153 }
154
155out_complete:
156 iwl_complete_scan(priv, aborted);
157
158out_settings:
159 /* Can we still talk to firmware ? */
160 if (!iwl_is_ready_rf(priv))
161 return;
162
163 iwlagn_post_scan(priv);
164}
165
166void iwl_force_scan_end(struct iwl_priv *priv)
167{
168 lockdep_assert_held(&priv->mutex);
169
170 if (!test_bit(STATUS_SCANNING, &priv->status)) {
171 IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
172 return;
173 }
174
175 IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
176 clear_bit(STATUS_SCANNING, &priv->status);
177 clear_bit(STATUS_SCAN_HW, &priv->status);
178 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
179 clear_bit(STATUS_SCAN_COMPLETE, &priv->status);
180 iwl_complete_scan(priv, true);
181}
182
183static void iwl_do_scan_abort(struct iwl_priv *priv)
184{
185 int ret;
186
187 lockdep_assert_held(&priv->mutex);
188
189 if (!test_bit(STATUS_SCANNING, &priv->status)) {
190 IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
191 return;
192 }
193
194 if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
195 IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
196 return;
197 }
198
199 ret = iwl_send_scan_abort(priv);
200 if (ret) {
201 IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
202 iwl_force_scan_end(priv);
203 } else
204 IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
205}
206
207/**
208 * iwl_scan_cancel - Cancel any currently executing HW scan
209 */
210int iwl_scan_cancel(struct iwl_priv *priv)
211{
212 IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
213 queue_work(priv->workqueue, &priv->abort_scan);
214 return 0;
215}
216
217/**
218 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
219 * @ms: amount of time to wait (in milliseconds) for scan to abort
220 *
221 */
222void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
223{
224 unsigned long timeout = jiffies + msecs_to_jiffies(ms);
225
226 lockdep_assert_held(&priv->mutex);
227
228 IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
229
230 iwl_do_scan_abort(priv);
231
232 while (time_before_eq(jiffies, timeout)) {
233 if (!test_bit(STATUS_SCAN_HW, &priv->status))
234 goto finished;
235 msleep(20);
236 }
237
238 return;
239
240 finished:
241 /*
242 * Now STATUS_SCAN_HW is clear. This means that the
243 * device finished, but the background work is going
244 * to execute at best as soon as we release the mutex.
245 * Since we need to be able to issue a new scan right
246 * after this function returns, run the complete here.
247 * The STATUS_SCAN_COMPLETE bit will then be cleared
248 * and prevent the background work from "completing"
249 * a possible new scan.
250 */
251 iwl_process_scan_complete(priv);
252}
253
254/* Service response to REPLY_SCAN_CMD (0x80) */
255static int iwl_rx_reply_scan(struct iwl_priv *priv,
256 struct iwl_rx_cmd_buffer *rxb,
257 struct iwl_device_cmd *cmd)
258{
259#ifdef CONFIG_IWLWIFI_DEBUG
260 struct iwl_rx_packet *pkt = rxb_addr(rxb);
261 struct iwl_scanreq_notification *notif = (void *)pkt->data;
262
263 IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
264#endif
265 return 0;
266}
267
268/* Service SCAN_START_NOTIFICATION (0x82) */
269static int iwl_rx_scan_start_notif(struct iwl_priv *priv,
270 struct iwl_rx_cmd_buffer *rxb,
271 struct iwl_device_cmd *cmd)
272{
273 struct iwl_rx_packet *pkt = rxb_addr(rxb);
274 struct iwl_scanstart_notification *notif = (void *)pkt->data;
275
276 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
277 IWL_DEBUG_SCAN(priv, "Scan start: "
278 "%d [802.11%s] "
279 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
280 notif->channel,
281 notif->band ? "bg" : "a",
282 le32_to_cpu(notif->tsf_high),
283 le32_to_cpu(notif->tsf_low),
284 notif->status, notif->beacon_timer);
285
286 if (priv->scan_type == IWL_SCAN_ROC &&
287 !priv->hw_roc_start_notified) {
288 ieee80211_ready_on_channel(priv->hw);
289 priv->hw_roc_start_notified = true;
290 }
291
292 return 0;
293}
294
295/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
296static int iwl_rx_scan_results_notif(struct iwl_priv *priv,
297 struct iwl_rx_cmd_buffer *rxb,
298 struct iwl_device_cmd *cmd)
299{
300#ifdef CONFIG_IWLWIFI_DEBUG
301 struct iwl_rx_packet *pkt = rxb_addr(rxb);
302 struct iwl_scanresults_notification *notif = (void *)pkt->data;
303
304 IWL_DEBUG_SCAN(priv, "Scan ch.res: "
305 "%d [802.11%s] "
306 "probe status: %u:%u "
307 "(TSF: 0x%08X:%08X) - %d "
308 "elapsed=%lu usec\n",
309 notif->channel,
310 notif->band ? "bg" : "a",
311 notif->probe_status, notif->num_probe_not_sent,
312 le32_to_cpu(notif->tsf_high),
313 le32_to_cpu(notif->tsf_low),
314 le32_to_cpu(notif->statistics[0]),
315 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
316#endif
317 return 0;
318}
319
320/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
321static int iwl_rx_scan_complete_notif(struct iwl_priv *priv,
322 struct iwl_rx_cmd_buffer *rxb,
323 struct iwl_device_cmd *cmd)
324{
325 struct iwl_rx_packet *pkt = rxb_addr(rxb);
326 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->data;
327
328 IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
329 scan_notif->scanned_channels,
330 scan_notif->tsf_low,
331 scan_notif->tsf_high, scan_notif->status);
332
333 IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
334 (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
335 jiffies_to_msecs(jiffies - priv->scan_start));
336
337 /*
338 * When aborting, we run the scan completed background work inline
339 * and the background work must then do nothing. The SCAN_COMPLETE
340 * bit helps implement that logic and thus needs to be set before
341 * queueing the work. Also, since the scan abort waits for SCAN_HW
342 * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW
343 * to avoid a race there.
344 */
345 set_bit(STATUS_SCAN_COMPLETE, &priv->status);
346 clear_bit(STATUS_SCAN_HW, &priv->status);
347 queue_work(priv->workqueue, &priv->scan_completed);
348
349 if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
350 iwl_advanced_bt_coexist(priv) &&
351 priv->bt_status != scan_notif->bt_status) {
352 if (scan_notif->bt_status) {
353 /* BT on */
354 if (!priv->bt_ch_announce)
355 priv->bt_traffic_load =
356 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
357 /*
358 * otherwise, no traffic load information provided
359 * no changes made
360 */
361 } else {
362 /* BT off */
363 priv->bt_traffic_load =
364 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
365 }
366 priv->bt_status = scan_notif->bt_status;
367 queue_work(priv->workqueue,
368 &priv->bt_traffic_change_work);
369 }
370 return 0;
371}
372
373void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
374{
375 /* scan handlers */
376 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
377 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
378 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
379 iwl_rx_scan_results_notif;
380 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
381 iwl_rx_scan_complete_notif;
382}
383
384static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
385 enum ieee80211_band band, u8 n_probes)
386{
387 if (band == IEEE80211_BAND_5GHZ)
388 return IWL_ACTIVE_DWELL_TIME_52 +
389 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
390 else
391 return IWL_ACTIVE_DWELL_TIME_24 +
392 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
393}
394
395static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
396{
397 struct iwl_rxon_context *ctx;
398
399 /*
400 * If we're associated, we clamp the dwell time 98%
401 * of the smallest beacon interval (minus 2 * channel
402 * tune time)
403 */
404 for_each_context(priv, ctx) {
405 u16 value;
406
407 switch (ctx->staging.dev_type) {
408 case RXON_DEV_TYPE_P2P:
409 /* no timing constraints */
410 continue;
411 case RXON_DEV_TYPE_ESS:
412 default:
413 /* timing constraints if associated */
414 if (!iwl_is_associated_ctx(ctx))
415 continue;
416 break;
417 case RXON_DEV_TYPE_CP:
418 case RXON_DEV_TYPE_2STA:
419 /*
420 * These seem to always have timers for TBTT
421 * active in uCode even when not associated yet.
422 */
423 break;
424 }
425
426 value = ctx->beacon_int;
427 if (!value)
428 value = IWL_PASSIVE_DWELL_BASE;
429 value = (value * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
430 dwell_time = min(value, dwell_time);
431 }
432
433 return dwell_time;
434}
435
436static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
437 enum ieee80211_band band)
438{
439 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
440 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
441 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
442
443 return iwl_limit_dwell(priv, passive);
444}
445
446/* Return valid, unused, channel for a passive scan to reset the RF */
447static u8 iwl_get_single_channel_number(struct iwl_priv *priv,
448 enum ieee80211_band band)
449{
450 struct ieee80211_supported_band *sband = priv->hw->wiphy->bands[band];
451 struct iwl_rxon_context *ctx;
452 int i;
453
454 for (i = 0; i < sband->n_channels; i++) {
455 bool busy = false;
456
457 for_each_context(priv, ctx) {
458 busy = sband->channels[i].hw_value ==
459 le16_to_cpu(ctx->staging.channel);
460 if (busy)
461 break;
462 }
463
464 if (busy)
465 continue;
466
467 if (!(sband->channels[i].flags & IEEE80211_CHAN_DISABLED))
468 return sband->channels[i].hw_value;
469 }
470
471 return 0;
472}
473
474static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
475 struct ieee80211_vif *vif,
476 enum ieee80211_band band,
477 struct iwl_scan_channel *scan_ch)
478{
479 const struct ieee80211_supported_band *sband;
480 u16 passive_dwell = 0;
481 u16 active_dwell = 0;
482 int added = 0;
483 u16 channel = 0;
484
485 sband = iwl_get_hw_mode(priv, band);
486 if (!sband) {
487 IWL_ERR(priv, "invalid band\n");
488 return added;
489 }
490
491 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
492 passive_dwell = iwl_get_passive_dwell_time(priv, band);
493
494 if (passive_dwell <= active_dwell)
495 passive_dwell = active_dwell + 1;
496
497 channel = iwl_get_single_channel_number(priv, band);
498 if (channel) {
499 scan_ch->channel = cpu_to_le16(channel);
500 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
501 scan_ch->active_dwell = cpu_to_le16(active_dwell);
502 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
503 /* Set txpower levels to defaults */
504 scan_ch->dsp_atten = 110;
505 if (band == IEEE80211_BAND_5GHZ)
506 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
507 else
508 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
509 added++;
510 } else
511 IWL_ERR(priv, "no valid channel found\n");
512 return added;
513}
514
515static int iwl_get_channels_for_scan(struct iwl_priv *priv,
516 struct ieee80211_vif *vif,
517 enum ieee80211_band band,
518 u8 is_active, u8 n_probes,
519 struct iwl_scan_channel *scan_ch)
520{
521 struct ieee80211_channel *chan;
522 const struct ieee80211_supported_band *sband;
523 u16 passive_dwell = 0;
524 u16 active_dwell = 0;
525 int added, i;
526 u16 channel;
527
528 sband = iwl_get_hw_mode(priv, band);
529 if (!sband)
530 return 0;
531
532 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
533 passive_dwell = iwl_get_passive_dwell_time(priv, band);
534
535 if (passive_dwell <= active_dwell)
536 passive_dwell = active_dwell + 1;
537
538 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
539 chan = priv->scan_request->channels[i];
540
541 if (chan->band != band)
542 continue;
543
544 channel = chan->hw_value;
545 scan_ch->channel = cpu_to_le16(channel);
546
547 if (!is_active || (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
548 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
549 else
550 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
551
552 if (n_probes)
553 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
554
555 scan_ch->active_dwell = cpu_to_le16(active_dwell);
556 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
557
558 /* Set txpower levels to defaults */
559 scan_ch->dsp_atten = 110;
560
561 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
562 * power level:
563 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
564 */
565 if (band == IEEE80211_BAND_5GHZ)
566 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
567 else
568 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
569
570 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
571 channel, le32_to_cpu(scan_ch->type),
572 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
573 "ACTIVE" : "PASSIVE",
574 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
575 active_dwell : passive_dwell);
576
577 scan_ch++;
578 added++;
579 }
580
581 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
582 return added;
583}
584
585/**
586 * iwl_fill_probe_req - fill in all required fields and IE for probe request
587 */
588
589static u16 iwl_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
590 const u8 *ies, int ie_len, const u8 *ssid,
591 u8 ssid_len, int left)
592{
593 int len = 0;
594 u8 *pos = NULL;
595
596 /* Make sure there is enough space for the probe request,
597 * two mandatory IEs and the data */
598 left -= 24;
599 if (left < 0)
600 return 0;
601
602 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
603 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
604 memcpy(frame->sa, ta, ETH_ALEN);
605 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
606 frame->seq_ctrl = 0;
607
608 len += 24;
609
610 /* ...next IE... */
611 pos = &frame->u.probe_req.variable[0];
612
613 /* fill in our SSID IE */
614 left -= ssid_len + 2;
615 if (left < 0)
616 return 0;
617 *pos++ = WLAN_EID_SSID;
618 *pos++ = ssid_len;
619 if (ssid && ssid_len) {
620 memcpy(pos, ssid, ssid_len);
621 pos += ssid_len;
622 }
623
624 len += ssid_len + 2;
625
626 if (WARN_ON(left < ie_len))
627 return len;
628
629 if (ies && ie_len) {
630 memcpy(pos, ies, ie_len);
631 len += ie_len;
632 }
633
634 return (u16)len;
635}
636
637static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
638{
639 struct iwl_host_cmd cmd = {
640 .id = REPLY_SCAN_CMD,
641 .len = { sizeof(struct iwl_scan_cmd), },
642 .flags = CMD_SYNC,
643 };
644 struct iwl_scan_cmd *scan;
645 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
646 u32 rate_flags = 0;
647 u16 cmd_len = 0;
648 u16 rx_chain = 0;
649 enum ieee80211_band band;
650 u8 n_probes = 0;
651 u8 rx_ant = priv->hw_params.valid_rx_ant;
652 u8 rate;
653 bool is_active = false;
654 int chan_mod;
655 u8 active_chains;
656 u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
657 int ret;
658 int scan_cmd_size = sizeof(struct iwl_scan_cmd) +
659 MAX_SCAN_CHANNEL * sizeof(struct iwl_scan_channel) +
660 priv->fw->ucode_capa.max_probe_length;
661 const u8 *ssid = NULL;
662 u8 ssid_len = 0;
663
664 if (WARN_ON_ONCE(priv->scan_request &&
665 priv->scan_request->n_channels > MAX_SCAN_CHANNEL))
666 return -EINVAL;
667
668 lockdep_assert_held(&priv->mutex);
669
670 if (vif)
671 ctx = iwl_rxon_ctx_from_vif(vif);
672
673 if (!priv->scan_cmd) {
674 priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL);
675 if (!priv->scan_cmd) {
676 IWL_DEBUG_SCAN(priv,
677 "fail to allocate memory for scan\n");
678 return -ENOMEM;
679 }
680 }
681 scan = priv->scan_cmd;
682 memset(scan, 0, scan_cmd_size);
683
684 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
685 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
686
687 if (priv->scan_type != IWL_SCAN_ROC &&
688 iwl_is_any_associated(priv)) {
689 u16 interval = 0;
690 u32 extra;
691 u32 suspend_time = 100;
692 u32 scan_suspend_time = 100;
693
694 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
695 switch (priv->scan_type) {
696 case IWL_SCAN_ROC:
697 WARN_ON(1);
698 break;
699 case IWL_SCAN_RADIO_RESET:
700 interval = 0;
701 break;
702 case IWL_SCAN_NORMAL:
703 interval = vif->bss_conf.beacon_int;
704 break;
705 }
706
707 scan->suspend_time = 0;
708 scan->max_out_time = cpu_to_le32(200 * 1024);
709 if (!interval)
710 interval = suspend_time;
711
712 extra = (suspend_time / interval) << 22;
713 scan_suspend_time = (extra |
714 ((suspend_time % interval) * 1024));
715 scan->suspend_time = cpu_to_le32(scan_suspend_time);
716 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
717 scan_suspend_time, interval);
718 } else if (priv->scan_type == IWL_SCAN_ROC) {
719 scan->suspend_time = 0;
720 scan->max_out_time = 0;
721 scan->quiet_time = 0;
722 scan->quiet_plcp_th = 0;
723 }
724
725 switch (priv->scan_type) {
726 case IWL_SCAN_RADIO_RESET:
727 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
728 break;
729 case IWL_SCAN_NORMAL:
730 if (priv->scan_request->n_ssids) {
731 int i, p = 0;
732 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
733 /*
734 * The highest priority SSID is inserted to the
735 * probe request template.
736 */
737 ssid_len = priv->scan_request->ssids[0].ssid_len;
738 ssid = priv->scan_request->ssids[0].ssid;
739
740 /*
741 * Invert the order of ssids, the firmware will invert
742 * it back.
743 */
744 for (i = priv->scan_request->n_ssids - 1; i >= 1; i--) {
745 scan->direct_scan[p].id = WLAN_EID_SSID;
746 scan->direct_scan[p].len =
747 priv->scan_request->ssids[i].ssid_len;
748 memcpy(scan->direct_scan[p].ssid,
749 priv->scan_request->ssids[i].ssid,
750 priv->scan_request->ssids[i].ssid_len);
751 n_probes++;
752 p++;
753 }
754 is_active = true;
755 } else
756 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
757 break;
758 case IWL_SCAN_ROC:
759 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
760 break;
761 }
762
763 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
764 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
765 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
766
767 switch (priv->scan_band) {
768 case IEEE80211_BAND_2GHZ:
769 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
770 chan_mod = le32_to_cpu(
771 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
772 RXON_FLG_CHANNEL_MODE_MSK)
773 >> RXON_FLG_CHANNEL_MODE_POS;
774 if ((priv->scan_request && priv->scan_request->no_cck) ||
775 chan_mod == CHANNEL_MODE_PURE_40) {
776 rate = IWL_RATE_6M_PLCP;
777 } else {
778 rate = IWL_RATE_1M_PLCP;
779 rate_flags = RATE_MCS_CCK_MSK;
780 }
781 /*
782 * Internal scans are passive, so we can indiscriminately set
783 * the BT ignore flag on 2.4 GHz since it applies to TX only.
784 */
785 if (priv->cfg->bt_params &&
786 priv->cfg->bt_params->advanced_bt_coexist)
787 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
788 break;
789 case IEEE80211_BAND_5GHZ:
790 rate = IWL_RATE_6M_PLCP;
791 break;
792 default:
793 IWL_WARN(priv, "Invalid scan band\n");
794 return -EIO;
795 }
796
797 /*
798 * If active scanning is requested but a certain channel is
799 * marked passive, we can do active scanning if we detect
800 * transmissions.
801 *
802 * There is an issue with some firmware versions that triggers
803 * a sysassert on a "good CRC threshold" of zero (== disabled),
804 * on a radar channel even though this means that we should NOT
805 * send probes.
806 *
807 * The "good CRC threshold" is the number of frames that we
808 * need to receive during our dwell time on a channel before
809 * sending out probes -- setting this to a huge value will
810 * mean we never reach it, but at the same time work around
811 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
812 * here instead of IWL_GOOD_CRC_TH_DISABLED.
813 *
814 * This was fixed in later versions along with some other
815 * scan changes, and the threshold behaves as a flag in those
816 * versions.
817 */
818 if (priv->new_scan_threshold_behaviour)
819 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
820 IWL_GOOD_CRC_TH_DISABLED;
821 else
822 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
823 IWL_GOOD_CRC_TH_NEVER;
824
825 band = priv->scan_band;
826
827 if (band == IEEE80211_BAND_2GHZ &&
828 priv->cfg->bt_params &&
829 priv->cfg->bt_params->advanced_bt_coexist) {
830 /* transmit 2.4 GHz probes only on first antenna */
831 scan_tx_antennas = first_antenna(scan_tx_antennas);
832 }
833
834 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
835 priv->scan_tx_ant[band],
836 scan_tx_antennas);
837 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
838 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
839
840 /*
841 * In power save mode while associated use one chain,
842 * otherwise use all chains
843 */
844 if (test_bit(STATUS_POWER_PMI, &priv->status) &&
845 !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) {
846 /* rx_ant has been set to all valid chains previously */
847 active_chains = rx_ant &
848 ((u8)(priv->chain_noise_data.active_chains));
849 if (!active_chains)
850 active_chains = rx_ant;
851
852 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
853 priv->chain_noise_data.active_chains);
854
855 rx_ant = first_antenna(active_chains);
856 }
857 if (priv->cfg->bt_params &&
858 priv->cfg->bt_params->advanced_bt_coexist &&
859 priv->bt_full_concurrent) {
860 /* operated as 1x1 in full concurrency mode */
861 rx_ant = first_antenna(rx_ant);
862 }
863
864 /* MIMO is not used here, but value is required */
865 rx_chain |=
866 priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
867 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
868 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
869 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
870 scan->rx_chain = cpu_to_le16(rx_chain);
871 switch (priv->scan_type) {
872 case IWL_SCAN_NORMAL:
873 cmd_len = iwl_fill_probe_req(
874 (struct ieee80211_mgmt *)scan->data,
875 vif->addr,
876 priv->scan_request->ie,
877 priv->scan_request->ie_len,
878 ssid, ssid_len,
879 scan_cmd_size - sizeof(*scan));
880 break;
881 case IWL_SCAN_RADIO_RESET:
882 case IWL_SCAN_ROC:
883 /* use bcast addr, will not be transmitted but must be valid */
884 cmd_len = iwl_fill_probe_req(
885 (struct ieee80211_mgmt *)scan->data,
886 iwl_bcast_addr, NULL, 0,
887 NULL, 0,
888 scan_cmd_size - sizeof(*scan));
889 break;
890 default:
891 BUG();
892 }
893 scan->tx_cmd.len = cpu_to_le16(cmd_len);
894
895 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
896 RXON_FILTER_BCON_AWARE_MSK);
897
898 switch (priv->scan_type) {
899 case IWL_SCAN_RADIO_RESET:
900 scan->channel_count =
901 iwl_get_single_channel_for_scan(priv, vif, band,
902 (void *)&scan->data[cmd_len]);
903 break;
904 case IWL_SCAN_NORMAL:
905 scan->channel_count =
906 iwl_get_channels_for_scan(priv, vif, band,
907 is_active, n_probes,
908 (void *)&scan->data[cmd_len]);
909 break;
910 case IWL_SCAN_ROC: {
911 struct iwl_scan_channel *scan_ch;
912 int n_chan, i;
913 u16 dwell;
914
915 dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
916 n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
917
918 scan->channel_count = n_chan;
919
920 scan_ch = (void *)&scan->data[cmd_len];
921
922 for (i = 0; i < n_chan; i++) {
923 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
924 scan_ch->channel =
925 cpu_to_le16(priv->hw_roc_channel->hw_value);
926
927 if (i == n_chan - 1)
928 dwell = priv->hw_roc_duration - i * dwell;
929
930 scan_ch->active_dwell =
931 scan_ch->passive_dwell = cpu_to_le16(dwell);
932
933 /* Set txpower levels to defaults */
934 scan_ch->dsp_atten = 110;
935
936 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
937 * power level:
938 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
939 */
940 if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
941 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
942 else
943 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
944
945 scan_ch++;
946 }
947 }
948
949 break;
950 }
951
952 if (scan->channel_count == 0) {
953 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
954 return -EIO;
955 }
956
957 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
958 scan->channel_count * sizeof(struct iwl_scan_channel);
959 cmd.data[0] = scan;
960 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
961 scan->len = cpu_to_le16(cmd.len[0]);
962
963 /* set scan bit here for PAN params */
964 set_bit(STATUS_SCAN_HW, &priv->status);
965
966 ret = iwlagn_set_pan_params(priv);
967 if (ret) {
968 clear_bit(STATUS_SCAN_HW, &priv->status);
969 return ret;
970 }
971
972 ret = iwl_dvm_send_cmd(priv, &cmd);
973 if (ret) {
974 clear_bit(STATUS_SCAN_HW, &priv->status);
975 iwlagn_set_pan_params(priv);
976 }
977
978 return ret;
979}
980
981void iwl_init_scan_params(struct iwl_priv *priv)
982{
983 u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1;
984 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
985 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
986 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
987 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
988}
989
990int __must_check iwl_scan_initiate(struct iwl_priv *priv,
991 struct ieee80211_vif *vif,
992 enum iwl_scan_type scan_type,
993 enum ieee80211_band band)
994{
995 int ret;
996
997 lockdep_assert_held(&priv->mutex);
998
999 cancel_delayed_work(&priv->scan_check);
1000
1001 if (!iwl_is_ready_rf(priv)) {
1002 IWL_WARN(priv, "Request scan called when driver not ready.\n");
1003 return -EIO;
1004 }
1005
1006 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1007 IWL_DEBUG_SCAN(priv,
1008 "Multiple concurrent scan requests in parallel.\n");
1009 return -EBUSY;
1010 }
1011
1012 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1013 IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
1014 return -EBUSY;
1015 }
1016
1017 IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
1018 scan_type == IWL_SCAN_NORMAL ? "" :
1019 scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
1020 "internal short ");
1021
1022 set_bit(STATUS_SCANNING, &priv->status);
1023 priv->scan_type = scan_type;
1024 priv->scan_start = jiffies;
1025 priv->scan_band = band;
1026
1027 ret = iwlagn_request_scan(priv, vif);
1028 if (ret) {
1029 clear_bit(STATUS_SCANNING, &priv->status);
1030 priv->scan_type = IWL_SCAN_NORMAL;
1031 return ret;
1032 }
1033
1034 queue_delayed_work(priv->workqueue, &priv->scan_check,
1035 IWL_SCAN_CHECK_WATCHDOG);
1036
1037 return 0;
1038}
1039
1040
1041/*
1042 * internal short scan, this function should only been called while associated.
1043 * It will reset and tune the radio to prevent possible RF related problem
1044 */
1045void iwl_internal_short_hw_scan(struct iwl_priv *priv)
1046{
1047 queue_work(priv->workqueue, &priv->start_internal_scan);
1048}
1049
1050static void iwl_bg_start_internal_scan(struct work_struct *work)
1051{
1052 struct iwl_priv *priv =
1053 container_of(work, struct iwl_priv, start_internal_scan);
1054
1055 IWL_DEBUG_SCAN(priv, "Start internal scan\n");
1056
1057 mutex_lock(&priv->mutex);
1058
1059 if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
1060 IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
1061 goto unlock;
1062 }
1063
1064 if (test_bit(STATUS_SCANNING, &priv->status)) {
1065 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
1066 goto unlock;
1067 }
1068
1069 if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
1070 IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
1071 unlock:
1072 mutex_unlock(&priv->mutex);
1073}
1074
1075static void iwl_bg_scan_check(struct work_struct *data)
1076{
1077 struct iwl_priv *priv =
1078 container_of(data, struct iwl_priv, scan_check.work);
1079
1080 IWL_DEBUG_SCAN(priv, "Scan check work\n");
1081
1082 /* Since we are here firmware does not finish scan and
1083 * most likely is in bad shape, so we don't bother to
1084 * send abort command, just force scan complete to mac80211 */
1085 mutex_lock(&priv->mutex);
1086 iwl_force_scan_end(priv);
1087 mutex_unlock(&priv->mutex);
1088}
1089
1090static void iwl_bg_abort_scan(struct work_struct *work)
1091{
1092 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
1093
1094 IWL_DEBUG_SCAN(priv, "Abort scan work\n");
1095
1096 /* We keep scan_check work queued in case when firmware will not
1097 * report back scan completed notification */
1098 mutex_lock(&priv->mutex);
1099 iwl_scan_cancel_timeout(priv, 200);
1100 mutex_unlock(&priv->mutex);
1101}
1102
1103static void iwl_bg_scan_completed(struct work_struct *work)
1104{
1105 struct iwl_priv *priv =
1106 container_of(work, struct iwl_priv, scan_completed);
1107
1108 mutex_lock(&priv->mutex);
1109 iwl_process_scan_complete(priv);
1110 mutex_unlock(&priv->mutex);
1111}
1112
1113void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
1114{
1115 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
1116 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
1117 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
1118 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
1119}
1120
1121void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
1122{
1123 cancel_work_sync(&priv->start_internal_scan);
1124 cancel_work_sync(&priv->abort_scan);
1125 cancel_work_sync(&priv->scan_completed);
1126
1127 if (cancel_delayed_work_sync(&priv->scan_check)) {
1128 mutex_lock(&priv->mutex);
1129 iwl_force_scan_end(priv);
1130 mutex_unlock(&priv->mutex);
1131 }
1132}
1133
1134void iwl_scan_roc_expired(struct iwl_priv *priv)
1135{
1136 /*
1137 * The status bit should be set here, to prevent a race
1138 * where the atomic_read returns 1, but before the execution continues
1139 * iwl_scan_offchannel_skb_status() checks if the status bit is set
1140 */
1141 set_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status);
1142
1143 if (atomic_read(&priv->num_aux_in_flight) == 0) {
1144 ieee80211_remain_on_channel_expired(priv->hw);
1145 priv->hw_roc_channel = NULL;
1146 schedule_delayed_work(&priv->hw_roc_disable_work,
1147 10 * HZ);
1148
1149 clear_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status);
1150 } else {
1151 IWL_DEBUG_SCAN(priv, "ROC done with %d frames in aux\n",
1152 atomic_read(&priv->num_aux_in_flight));
1153 }
1154}
1155
1156void iwl_scan_offchannel_skb(struct iwl_priv *priv)
1157{
1158 WARN_ON(!priv->hw_roc_start_notified);
1159 atomic_inc(&priv->num_aux_in_flight);
1160}
1161
1162void iwl_scan_offchannel_skb_status(struct iwl_priv *priv)
1163{
1164 if (atomic_dec_return(&priv->num_aux_in_flight) == 0 &&
1165 test_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status)) {
1166 IWL_DEBUG_SCAN(priv, "0 aux frames. Calling ROC expired\n");
1167 iwl_scan_roc_expired(priv);
1168 }
1169}
diff --git a/drivers/net/wireless/iwlwifi/dvm/sta.c b/drivers/net/wireless/iwlwifi/dvm/sta.c
new file mode 100644
index 000000000000..c6b343fdf5f9
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/sta.c
@@ -0,0 +1,1479 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29#include <linux/etherdevice.h>
30#include <net/mac80211.h>
31#include "iwl-trans.h"
32#include "dev.h"
33#include "agn.h"
34
35const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
36
37static int iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
38{
39 lockdep_assert_held(&priv->sta_lock);
40
41 if (sta_id >= IWLAGN_STATION_COUNT) {
42 IWL_ERR(priv, "invalid sta_id %u", sta_id);
43 return -EINVAL;
44 }
45 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
46 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
47 "addr %pM\n",
48 sta_id, priv->stations[sta_id].sta.sta.addr);
49
50 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
51 IWL_DEBUG_ASSOC(priv,
52 "STA id %u addr %pM already present in uCode "
53 "(according to driver)\n",
54 sta_id, priv->stations[sta_id].sta.sta.addr);
55 } else {
56 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
57 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
58 sta_id, priv->stations[sta_id].sta.sta.addr);
59 }
60 return 0;
61}
62
63static int iwl_process_add_sta_resp(struct iwl_priv *priv,
64 struct iwl_addsta_cmd *addsta,
65 struct iwl_rx_packet *pkt)
66{
67 struct iwl_add_sta_resp *add_sta_resp = (void *)pkt->data;
68 u8 sta_id = addsta->sta.sta_id;
69 int ret = -EIO;
70
71 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
72 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
73 pkt->hdr.flags);
74 return ret;
75 }
76
77 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
78 sta_id);
79
80 spin_lock(&priv->sta_lock);
81
82 switch (add_sta_resp->status) {
83 case ADD_STA_SUCCESS_MSK:
84 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
85 ret = iwl_sta_ucode_activate(priv, sta_id);
86 break;
87 case ADD_STA_NO_ROOM_IN_TABLE:
88 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
89 sta_id);
90 break;
91 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
92 IWL_ERR(priv, "Adding station %d failed, no block ack "
93 "resource.\n", sta_id);
94 break;
95 case ADD_STA_MODIFY_NON_EXIST_STA:
96 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
97 sta_id);
98 break;
99 default:
100 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
101 add_sta_resp->status);
102 break;
103 }
104
105 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
106 priv->stations[sta_id].sta.mode ==
107 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
108 sta_id, priv->stations[sta_id].sta.sta.addr);
109
110 /*
111 * XXX: The MAC address in the command buffer is often changed from
112 * the original sent to the device. That is, the MAC address
113 * written to the command buffer often is not the same MAC address
114 * read from the command buffer when the command returns. This
115 * issue has not yet been resolved and this debugging is left to
116 * observe the problem.
117 */
118 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
119 priv->stations[sta_id].sta.mode ==
120 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
121 addsta->sta.addr);
122 spin_unlock(&priv->sta_lock);
123
124 return ret;
125}
126
127int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
128 struct iwl_device_cmd *cmd)
129{
130 struct iwl_rx_packet *pkt = rxb_addr(rxb);
131 struct iwl_addsta_cmd *addsta =
132 (struct iwl_addsta_cmd *) cmd->payload;
133
134 return iwl_process_add_sta_resp(priv, addsta, pkt);
135}
136
137int iwl_send_add_sta(struct iwl_priv *priv,
138 struct iwl_addsta_cmd *sta, u8 flags)
139{
140 int ret = 0;
141 struct iwl_host_cmd cmd = {
142 .id = REPLY_ADD_STA,
143 .flags = flags,
144 .data = { sta, },
145 .len = { sizeof(*sta), },
146 };
147 u8 sta_id __maybe_unused = sta->sta.sta_id;
148
149 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
150 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
151
152 if (!(flags & CMD_ASYNC)) {
153 cmd.flags |= CMD_WANT_SKB;
154 might_sleep();
155 }
156
157 ret = iwl_dvm_send_cmd(priv, &cmd);
158
159 if (ret || (flags & CMD_ASYNC))
160 return ret;
161 /*else the command was successfully sent in SYNC mode, need to free
162 * the reply page */
163
164 iwl_free_resp(&cmd);
165
166 if (cmd.handler_status)
167 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
168 cmd.handler_status);
169
170 return cmd.handler_status;
171}
172
173bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
174 struct iwl_rxon_context *ctx,
175 struct ieee80211_sta_ht_cap *ht_cap)
176{
177 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
178 return false;
179
180 /*
181 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
182 * the bit will not set if it is pure 40MHz case
183 */
184 if (ht_cap && !ht_cap->ht_supported)
185 return false;
186
187#ifdef CONFIG_IWLWIFI_DEBUGFS
188 if (priv->disable_ht40)
189 return false;
190#endif
191
192 return true;
193}
194
195static void iwl_sta_calc_ht_flags(struct iwl_priv *priv,
196 struct ieee80211_sta *sta,
197 struct iwl_rxon_context *ctx,
198 __le32 *flags, __le32 *mask)
199{
200 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
201 u8 mimo_ps_mode;
202
203 *mask = STA_FLG_RTS_MIMO_PROT_MSK |
204 STA_FLG_MIMO_DIS_MSK |
205 STA_FLG_HT40_EN_MSK |
206 STA_FLG_MAX_AGG_SIZE_MSK |
207 STA_FLG_AGG_MPDU_DENSITY_MSK;
208 *flags = 0;
209
210 if (!sta || !sta_ht_inf->ht_supported)
211 return;
212
213 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
214
215 IWL_DEBUG_INFO(priv, "STA %pM SM PS mode: %s\n",
216 sta->addr,
217 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
218 "static" :
219 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
220 "dynamic" : "disabled");
221
222 switch (mimo_ps_mode) {
223 case WLAN_HT_CAP_SM_PS_STATIC:
224 *flags |= STA_FLG_MIMO_DIS_MSK;
225 break;
226 case WLAN_HT_CAP_SM_PS_DYNAMIC:
227 *flags |= STA_FLG_RTS_MIMO_PROT_MSK;
228 break;
229 case WLAN_HT_CAP_SM_PS_DISABLED:
230 break;
231 default:
232 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
233 break;
234 }
235
236 *flags |= cpu_to_le32(
237 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
238
239 *flags |= cpu_to_le32(
240 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
241
242 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
243 *flags |= STA_FLG_HT40_EN_MSK;
244}
245
246int iwl_sta_update_ht(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
247 struct ieee80211_sta *sta)
248{
249 u8 sta_id = iwl_sta_id(sta);
250 __le32 flags, mask;
251 struct iwl_addsta_cmd cmd;
252
253 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
254 return -EINVAL;
255
256 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
257
258 spin_lock_bh(&priv->sta_lock);
259 priv->stations[sta_id].sta.station_flags &= ~mask;
260 priv->stations[sta_id].sta.station_flags |= flags;
261 spin_unlock_bh(&priv->sta_lock);
262
263 memset(&cmd, 0, sizeof(cmd));
264 cmd.mode = STA_CONTROL_MODIFY_MSK;
265 cmd.station_flags_msk = mask;
266 cmd.station_flags = flags;
267 cmd.sta.sta_id = sta_id;
268
269 return iwl_send_add_sta(priv, &cmd, CMD_SYNC);
270}
271
272static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
273 struct ieee80211_sta *sta,
274 struct iwl_rxon_context *ctx)
275{
276 __le32 flags, mask;
277
278 iwl_sta_calc_ht_flags(priv, sta, ctx, &flags, &mask);
279
280 lockdep_assert_held(&priv->sta_lock);
281 priv->stations[index].sta.station_flags &= ~mask;
282 priv->stations[index].sta.station_flags |= flags;
283}
284
285/**
286 * iwl_prep_station - Prepare station information for addition
287 *
288 * should be called with sta_lock held
289 */
290u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
291 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
292{
293 struct iwl_station_entry *station;
294 int i;
295 u8 sta_id = IWL_INVALID_STATION;
296
297 if (is_ap)
298 sta_id = ctx->ap_sta_id;
299 else if (is_broadcast_ether_addr(addr))
300 sta_id = ctx->bcast_sta_id;
301 else
302 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
303 if (ether_addr_equal(priv->stations[i].sta.sta.addr,
304 addr)) {
305 sta_id = i;
306 break;
307 }
308
309 if (!priv->stations[i].used &&
310 sta_id == IWL_INVALID_STATION)
311 sta_id = i;
312 }
313
314 /*
315 * These two conditions have the same outcome, but keep them
316 * separate
317 */
318 if (unlikely(sta_id == IWL_INVALID_STATION))
319 return sta_id;
320
321 /*
322 * uCode is not able to deal with multiple requests to add a
323 * station. Keep track if one is in progress so that we do not send
324 * another.
325 */
326 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
327 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
328 "added.\n", sta_id);
329 return sta_id;
330 }
331
332 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
333 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
334 ether_addr_equal(priv->stations[sta_id].sta.sta.addr, addr)) {
335 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
336 "adding again.\n", sta_id, addr);
337 return sta_id;
338 }
339
340 station = &priv->stations[sta_id];
341 station->used = IWL_STA_DRIVER_ACTIVE;
342 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
343 sta_id, addr);
344 priv->num_stations++;
345
346 /* Set up the REPLY_ADD_STA command to send to device */
347 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
348 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
349 station->sta.mode = 0;
350 station->sta.sta.sta_id = sta_id;
351 station->sta.station_flags = ctx->station_flags;
352 station->ctxid = ctx->ctxid;
353
354 if (sta) {
355 struct iwl_station_priv *sta_priv;
356
357 sta_priv = (void *)sta->drv_priv;
358 sta_priv->ctx = ctx;
359 }
360
361 /*
362 * OK to call unconditionally, since local stations (IBSS BSSID
363 * STA and broadcast STA) pass in a NULL sta, and mac80211
364 * doesn't allow HT IBSS.
365 */
366 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
367
368 return sta_id;
369
370}
371
372#define STA_WAIT_TIMEOUT (HZ/2)
373
374/**
375 * iwl_add_station_common -
376 */
377int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
378 const u8 *addr, bool is_ap,
379 struct ieee80211_sta *sta, u8 *sta_id_r)
380{
381 int ret = 0;
382 u8 sta_id;
383 struct iwl_addsta_cmd sta_cmd;
384
385 *sta_id_r = 0;
386 spin_lock_bh(&priv->sta_lock);
387 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
388 if (sta_id == IWL_INVALID_STATION) {
389 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
390 addr);
391 spin_unlock_bh(&priv->sta_lock);
392 return -EINVAL;
393 }
394
395 /*
396 * uCode is not able to deal with multiple requests to add a
397 * station. Keep track if one is in progress so that we do not send
398 * another.
399 */
400 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
401 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
402 "added.\n", sta_id);
403 spin_unlock_bh(&priv->sta_lock);
404 return -EEXIST;
405 }
406
407 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
408 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
409 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
410 "adding again.\n", sta_id, addr);
411 spin_unlock_bh(&priv->sta_lock);
412 return -EEXIST;
413 }
414
415 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
416 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
417 sizeof(struct iwl_addsta_cmd));
418 spin_unlock_bh(&priv->sta_lock);
419
420 /* Add station to device's station table */
421 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
422 if (ret) {
423 spin_lock_bh(&priv->sta_lock);
424 IWL_ERR(priv, "Adding station %pM failed.\n",
425 priv->stations[sta_id].sta.sta.addr);
426 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
427 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
428 spin_unlock_bh(&priv->sta_lock);
429 }
430 *sta_id_r = sta_id;
431 return ret;
432}
433
434/**
435 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
436 */
437static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
438{
439 lockdep_assert_held(&priv->sta_lock);
440
441 /* Ucode must be active and driver must be non active */
442 if ((priv->stations[sta_id].used &
443 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
444 IWL_STA_UCODE_ACTIVE)
445 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
446
447 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
448
449 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
450 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
451}
452
453static int iwl_send_remove_station(struct iwl_priv *priv,
454 const u8 *addr, int sta_id,
455 bool temporary)
456{
457 struct iwl_rx_packet *pkt;
458 int ret;
459 struct iwl_rem_sta_cmd rm_sta_cmd;
460
461 struct iwl_host_cmd cmd = {
462 .id = REPLY_REMOVE_STA,
463 .len = { sizeof(struct iwl_rem_sta_cmd), },
464 .flags = CMD_SYNC,
465 .data = { &rm_sta_cmd, },
466 };
467
468 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
469 rm_sta_cmd.num_sta = 1;
470 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
471
472 cmd.flags |= CMD_WANT_SKB;
473
474 ret = iwl_dvm_send_cmd(priv, &cmd);
475
476 if (ret)
477 return ret;
478
479 pkt = cmd.resp_pkt;
480 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
481 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
482 pkt->hdr.flags);
483 ret = -EIO;
484 }
485
486 if (!ret) {
487 struct iwl_rem_sta_resp *rem_sta_resp = (void *)pkt->data;
488 switch (rem_sta_resp->status) {
489 case REM_STA_SUCCESS_MSK:
490 if (!temporary) {
491 spin_lock_bh(&priv->sta_lock);
492 iwl_sta_ucode_deactivate(priv, sta_id);
493 spin_unlock_bh(&priv->sta_lock);
494 }
495 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
496 break;
497 default:
498 ret = -EIO;
499 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
500 break;
501 }
502 }
503 iwl_free_resp(&cmd);
504
505 return ret;
506}
507
508/**
509 * iwl_remove_station - Remove driver's knowledge of station.
510 */
511int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
512 const u8 *addr)
513{
514 u8 tid;
515
516 if (!iwl_is_ready(priv)) {
517 IWL_DEBUG_INFO(priv,
518 "Unable to remove station %pM, device not ready.\n",
519 addr);
520 /*
521 * It is typical for stations to be removed when we are
522 * going down. Return success since device will be down
523 * soon anyway
524 */
525 return 0;
526 }
527
528 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
529 sta_id, addr);
530
531 if (WARN_ON(sta_id == IWL_INVALID_STATION))
532 return -EINVAL;
533
534 spin_lock_bh(&priv->sta_lock);
535
536 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
537 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
538 addr);
539 goto out_err;
540 }
541
542 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
543 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
544 addr);
545 goto out_err;
546 }
547
548 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
549 kfree(priv->stations[sta_id].lq);
550 priv->stations[sta_id].lq = NULL;
551 }
552
553 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
554 memset(&priv->tid_data[sta_id][tid], 0,
555 sizeof(priv->tid_data[sta_id][tid]));
556
557 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
558
559 priv->num_stations--;
560
561 if (WARN_ON(priv->num_stations < 0))
562 priv->num_stations = 0;
563
564 spin_unlock_bh(&priv->sta_lock);
565
566 return iwl_send_remove_station(priv, addr, sta_id, false);
567out_err:
568 spin_unlock_bh(&priv->sta_lock);
569 return -EINVAL;
570}
571
572void iwl_deactivate_station(struct iwl_priv *priv, const u8 sta_id,
573 const u8 *addr)
574{
575 u8 tid;
576
577 if (!iwl_is_ready(priv)) {
578 IWL_DEBUG_INFO(priv,
579 "Unable to remove station %pM, device not ready.\n",
580 addr);
581 return;
582 }
583
584 IWL_DEBUG_ASSOC(priv, "Deactivating STA: %pM (%d)\n", addr, sta_id);
585
586 if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
587 return;
588
589 spin_lock_bh(&priv->sta_lock);
590
591 WARN_ON_ONCE(!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE));
592
593 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
594 memset(&priv->tid_data[sta_id][tid], 0,
595 sizeof(priv->tid_data[sta_id][tid]));
596
597 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
598
599 priv->num_stations--;
600
601 if (WARN_ON_ONCE(priv->num_stations < 0))
602 priv->num_stations = 0;
603
604 spin_unlock_bh(&priv->sta_lock);
605}
606
607static void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
608 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
609{
610 int i, r;
611 u32 rate_flags = 0;
612 __le32 rate_n_flags;
613
614 lockdep_assert_held(&priv->mutex);
615
616 memset(link_cmd, 0, sizeof(*link_cmd));
617
618 /* Set up the rate scaling to start at selected rate, fall back
619 * all the way down to 1M in IEEE order, and then spin on 1M */
620 if (priv->band == IEEE80211_BAND_5GHZ)
621 r = IWL_RATE_6M_INDEX;
622 else if (ctx && ctx->vif && ctx->vif->p2p)
623 r = IWL_RATE_6M_INDEX;
624 else
625 r = IWL_RATE_1M_INDEX;
626
627 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
628 rate_flags |= RATE_MCS_CCK_MSK;
629
630 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
631 RATE_MCS_ANT_POS;
632 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
633 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
634 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
635
636 link_cmd->general_params.single_stream_ant_msk =
637 first_antenna(priv->hw_params.valid_tx_ant);
638
639 link_cmd->general_params.dual_stream_ant_msk =
640 priv->hw_params.valid_tx_ant &
641 ~first_antenna(priv->hw_params.valid_tx_ant);
642 if (!link_cmd->general_params.dual_stream_ant_msk) {
643 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
644 } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
645 link_cmd->general_params.dual_stream_ant_msk =
646 priv->hw_params.valid_tx_ant;
647 }
648
649 link_cmd->agg_params.agg_dis_start_th =
650 LINK_QUAL_AGG_DISABLE_START_DEF;
651 link_cmd->agg_params.agg_time_limit =
652 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
653
654 link_cmd->sta_id = sta_id;
655}
656
657/**
658 * iwl_clear_ucode_stations - clear ucode station table bits
659 *
660 * This function clears all the bits in the driver indicating
661 * which stations are active in the ucode. Call when something
662 * other than explicit station management would cause this in
663 * the ucode, e.g. unassociated RXON.
664 */
665void iwl_clear_ucode_stations(struct iwl_priv *priv,
666 struct iwl_rxon_context *ctx)
667{
668 int i;
669 bool cleared = false;
670
671 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
672
673 spin_lock_bh(&priv->sta_lock);
674 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
675 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
676 continue;
677
678 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
679 IWL_DEBUG_INFO(priv,
680 "Clearing ucode active for station %d\n", i);
681 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
682 cleared = true;
683 }
684 }
685 spin_unlock_bh(&priv->sta_lock);
686
687 if (!cleared)
688 IWL_DEBUG_INFO(priv,
689 "No active stations found to be cleared\n");
690}
691
692/**
693 * iwl_restore_stations() - Restore driver known stations to device
694 *
695 * All stations considered active by driver, but not present in ucode, is
696 * restored.
697 *
698 * Function sleeps.
699 */
700void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
701{
702 struct iwl_addsta_cmd sta_cmd;
703 struct iwl_link_quality_cmd lq;
704 int i;
705 bool found = false;
706 int ret;
707 bool send_lq;
708
709 if (!iwl_is_ready(priv)) {
710 IWL_DEBUG_INFO(priv,
711 "Not ready yet, not restoring any stations.\n");
712 return;
713 }
714
715 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
716 spin_lock_bh(&priv->sta_lock);
717 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
718 if (ctx->ctxid != priv->stations[i].ctxid)
719 continue;
720 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
721 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
722 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
723 priv->stations[i].sta.sta.addr);
724 priv->stations[i].sta.mode = 0;
725 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
726 found = true;
727 }
728 }
729
730 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
731 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
732 memcpy(&sta_cmd, &priv->stations[i].sta,
733 sizeof(struct iwl_addsta_cmd));
734 send_lq = false;
735 if (priv->stations[i].lq) {
736 if (priv->wowlan)
737 iwl_sta_fill_lq(priv, ctx, i, &lq);
738 else
739 memcpy(&lq, priv->stations[i].lq,
740 sizeof(struct iwl_link_quality_cmd));
741 send_lq = true;
742 }
743 spin_unlock_bh(&priv->sta_lock);
744 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
745 if (ret) {
746 spin_lock_bh(&priv->sta_lock);
747 IWL_ERR(priv, "Adding station %pM failed.\n",
748 priv->stations[i].sta.sta.addr);
749 priv->stations[i].used &=
750 ~IWL_STA_DRIVER_ACTIVE;
751 priv->stations[i].used &=
752 ~IWL_STA_UCODE_INPROGRESS;
753 continue;
754 }
755 /*
756 * Rate scaling has already been initialized, send
757 * current LQ command
758 */
759 if (send_lq)
760 iwl_send_lq_cmd(priv, ctx, &lq,
761 CMD_SYNC, true);
762 spin_lock_bh(&priv->sta_lock);
763 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
764 }
765 }
766
767 spin_unlock_bh(&priv->sta_lock);
768 if (!found)
769 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
770 "no stations to be restored.\n");
771 else
772 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
773 "complete.\n");
774}
775
776int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
777{
778 int i;
779
780 for (i = 0; i < priv->sta_key_max_num; i++)
781 if (!test_and_set_bit(i, &priv->ucode_key_table))
782 return i;
783
784 return WEP_INVALID_OFFSET;
785}
786
787void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
788{
789 int i;
790
791 spin_lock_bh(&priv->sta_lock);
792 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
793 if (!(priv->stations[i].used & IWL_STA_BCAST))
794 continue;
795
796 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
797 priv->num_stations--;
798 if (WARN_ON(priv->num_stations < 0))
799 priv->num_stations = 0;
800 kfree(priv->stations[i].lq);
801 priv->stations[i].lq = NULL;
802 }
803 spin_unlock_bh(&priv->sta_lock);
804}
805
806#ifdef CONFIG_IWLWIFI_DEBUG
807static void iwl_dump_lq_cmd(struct iwl_priv *priv,
808 struct iwl_link_quality_cmd *lq)
809{
810 int i;
811 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
812 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
813 lq->general_params.single_stream_ant_msk,
814 lq->general_params.dual_stream_ant_msk);
815
816 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
817 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
818 i, lq->rs_table[i].rate_n_flags);
819}
820#else
821static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
822 struct iwl_link_quality_cmd *lq)
823{
824}
825#endif
826
827/**
828 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
829 *
830 * It sometimes happens when a HT rate has been in use and we
831 * loose connectivity with AP then mac80211 will first tell us that the
832 * current channel is not HT anymore before removing the station. In such a
833 * scenario the RXON flags will be updated to indicate we are not
834 * communicating HT anymore, but the LQ command may still contain HT rates.
835 * Test for this to prevent driver from sending LQ command between the time
836 * RXON flags are updated and when LQ command is updated.
837 */
838static bool is_lq_table_valid(struct iwl_priv *priv,
839 struct iwl_rxon_context *ctx,
840 struct iwl_link_quality_cmd *lq)
841{
842 int i;
843
844 if (ctx->ht.enabled)
845 return true;
846
847 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
848 ctx->active.channel);
849 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
850 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
851 RATE_MCS_HT_MSK) {
852 IWL_DEBUG_INFO(priv,
853 "index %d of LQ expects HT channel\n",
854 i);
855 return false;
856 }
857 }
858 return true;
859}
860
861/**
862 * iwl_send_lq_cmd() - Send link quality command
863 * @init: This command is sent as part of station initialization right
864 * after station has been added.
865 *
866 * The link quality command is sent as the last step of station creation.
867 * This is the special case in which init is set and we call a callback in
868 * this case to clear the state indicating that station creation is in
869 * progress.
870 */
871int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
872 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
873{
874 int ret = 0;
875 struct iwl_host_cmd cmd = {
876 .id = REPLY_TX_LINK_QUALITY_CMD,
877 .len = { sizeof(struct iwl_link_quality_cmd), },
878 .flags = flags,
879 .data = { lq, },
880 };
881
882 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
883 return -EINVAL;
884
885
886 spin_lock_bh(&priv->sta_lock);
887 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
888 spin_unlock_bh(&priv->sta_lock);
889 return -EINVAL;
890 }
891 spin_unlock_bh(&priv->sta_lock);
892
893 iwl_dump_lq_cmd(priv, lq);
894 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
895 return -EINVAL;
896
897 if (is_lq_table_valid(priv, ctx, lq))
898 ret = iwl_dvm_send_cmd(priv, &cmd);
899 else
900 ret = -EINVAL;
901
902 if (cmd.flags & CMD_ASYNC)
903 return ret;
904
905 if (init) {
906 IWL_DEBUG_INFO(priv, "init LQ command complete, "
907 "clearing sta addition status for sta %d\n",
908 lq->sta_id);
909 spin_lock_bh(&priv->sta_lock);
910 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
911 spin_unlock_bh(&priv->sta_lock);
912 }
913 return ret;
914}
915
916
917static struct iwl_link_quality_cmd *
918iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
919 u8 sta_id)
920{
921 struct iwl_link_quality_cmd *link_cmd;
922
923 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
924 if (!link_cmd) {
925 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
926 return NULL;
927 }
928
929 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
930
931 return link_cmd;
932}
933
934/*
935 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
936 *
937 * Function sleeps.
938 */
939int iwlagn_add_bssid_station(struct iwl_priv *priv,
940 struct iwl_rxon_context *ctx,
941 const u8 *addr, u8 *sta_id_r)
942{
943 int ret;
944 u8 sta_id;
945 struct iwl_link_quality_cmd *link_cmd;
946
947 if (sta_id_r)
948 *sta_id_r = IWL_INVALID_STATION;
949
950 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
951 if (ret) {
952 IWL_ERR(priv, "Unable to add station %pM\n", addr);
953 return ret;
954 }
955
956 if (sta_id_r)
957 *sta_id_r = sta_id;
958
959 spin_lock_bh(&priv->sta_lock);
960 priv->stations[sta_id].used |= IWL_STA_LOCAL;
961 spin_unlock_bh(&priv->sta_lock);
962
963 /* Set up default rate scaling table in device's station table */
964 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
965 if (!link_cmd) {
966 IWL_ERR(priv,
967 "Unable to initialize rate scaling for station %pM.\n",
968 addr);
969 return -ENOMEM;
970 }
971
972 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
973 if (ret)
974 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
975
976 spin_lock_bh(&priv->sta_lock);
977 priv->stations[sta_id].lq = link_cmd;
978 spin_unlock_bh(&priv->sta_lock);
979
980 return 0;
981}
982
983/*
984 * static WEP keys
985 *
986 * For each context, the device has a table of 4 static WEP keys
987 * (one for each key index) that is updated with the following
988 * commands.
989 */
990
991static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
992 struct iwl_rxon_context *ctx,
993 bool send_if_empty)
994{
995 int i, not_empty = 0;
996 u8 buff[sizeof(struct iwl_wep_cmd) +
997 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
998 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
999 size_t cmd_size = sizeof(struct iwl_wep_cmd);
1000 struct iwl_host_cmd cmd = {
1001 .id = ctx->wep_key_cmd,
1002 .data = { wep_cmd, },
1003 .flags = CMD_SYNC,
1004 };
1005
1006 might_sleep();
1007
1008 memset(wep_cmd, 0, cmd_size +
1009 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
1010
1011 for (i = 0; i < WEP_KEYS_MAX ; i++) {
1012 wep_cmd->key[i].key_index = i;
1013 if (ctx->wep_keys[i].key_size) {
1014 wep_cmd->key[i].key_offset = i;
1015 not_empty = 1;
1016 } else {
1017 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
1018 }
1019
1020 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1021 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1022 ctx->wep_keys[i].key_size);
1023 }
1024
1025 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1026 wep_cmd->num_keys = WEP_KEYS_MAX;
1027
1028 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1029
1030 cmd.len[0] = cmd_size;
1031
1032 if (not_empty || send_if_empty)
1033 return iwl_dvm_send_cmd(priv, &cmd);
1034 else
1035 return 0;
1036}
1037
1038int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1039 struct iwl_rxon_context *ctx)
1040{
1041 lockdep_assert_held(&priv->mutex);
1042
1043 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1044}
1045
1046int iwl_remove_default_wep_key(struct iwl_priv *priv,
1047 struct iwl_rxon_context *ctx,
1048 struct ieee80211_key_conf *keyconf)
1049{
1050 int ret;
1051
1052 lockdep_assert_held(&priv->mutex);
1053
1054 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1055 keyconf->keyidx);
1056
1057 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
1058 if (iwl_is_rfkill(priv)) {
1059 IWL_DEBUG_WEP(priv,
1060 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
1061 /* but keys in device are clear anyway so return success */
1062 return 0;
1063 }
1064 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1065 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1066 keyconf->keyidx, ret);
1067
1068 return ret;
1069}
1070
1071int iwl_set_default_wep_key(struct iwl_priv *priv,
1072 struct iwl_rxon_context *ctx,
1073 struct ieee80211_key_conf *keyconf)
1074{
1075 int ret;
1076
1077 lockdep_assert_held(&priv->mutex);
1078
1079 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1080 keyconf->keylen != WEP_KEY_LEN_64) {
1081 IWL_DEBUG_WEP(priv,
1082 "Bad WEP key length %d\n", keyconf->keylen);
1083 return -EINVAL;
1084 }
1085
1086 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
1087
1088 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1089 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1090 keyconf->keylen);
1091
1092 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1093 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1094 keyconf->keylen, keyconf->keyidx, ret);
1095
1096 return ret;
1097}
1098
1099/*
1100 * dynamic (per-station) keys
1101 *
1102 * The dynamic keys are a little more complicated. The device has
1103 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1104 * These are linked to stations by a table that contains an index
1105 * into the key table for each station/key index/{mcast,unicast},
1106 * i.e. it's basically an array of pointers like this:
1107 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1108 * (it really works differently, but you can think of it as such)
1109 *
1110 * The key uploading and linking happens in the same command, the
1111 * add station command with STA_MODIFY_KEY_MASK.
1112 */
1113
1114static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1115 struct ieee80211_vif *vif,
1116 struct ieee80211_sta *sta)
1117{
1118 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1119
1120 if (sta)
1121 return iwl_sta_id(sta);
1122
1123 /*
1124 * The device expects GTKs for station interfaces to be
1125 * installed as GTKs for the AP station. If we have no
1126 * station ID, then use the ap_sta_id in that case.
1127 */
1128 if (vif->type == NL80211_IFTYPE_STATION && vif_priv->ctx)
1129 return vif_priv->ctx->ap_sta_id;
1130
1131 return IWL_INVALID_STATION;
1132}
1133
1134static int iwlagn_send_sta_key(struct iwl_priv *priv,
1135 struct ieee80211_key_conf *keyconf,
1136 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1137 u32 cmd_flags)
1138{
1139 __le16 key_flags;
1140 struct iwl_addsta_cmd sta_cmd;
1141 int i;
1142
1143 spin_lock_bh(&priv->sta_lock);
1144 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1145 spin_unlock_bh(&priv->sta_lock);
1146
1147 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1148 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
1149
1150 switch (keyconf->cipher) {
1151 case WLAN_CIPHER_SUITE_CCMP:
1152 key_flags |= STA_KEY_FLG_CCMP;
1153 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1154 break;
1155 case WLAN_CIPHER_SUITE_TKIP:
1156 key_flags |= STA_KEY_FLG_TKIP;
1157 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1158 for (i = 0; i < 5; i++)
1159 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1160 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1161 break;
1162 case WLAN_CIPHER_SUITE_WEP104:
1163 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
1164 /* fall through */
1165 case WLAN_CIPHER_SUITE_WEP40:
1166 key_flags |= STA_KEY_FLG_WEP;
1167 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1168 break;
1169 default:
1170 WARN_ON(1);
1171 return -EINVAL;
1172 }
1173
1174 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1175 key_flags |= STA_KEY_MULTICAST_MSK;
1176
1177 /* key pointer (offset) */
1178 sta_cmd.key.key_offset = keyconf->hw_key_idx;
1179
1180 sta_cmd.key.key_flags = key_flags;
1181 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1182 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1183
1184 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
1185}
1186
1187void iwl_update_tkip_key(struct iwl_priv *priv,
1188 struct ieee80211_vif *vif,
1189 struct ieee80211_key_conf *keyconf,
1190 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1191{
1192 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1193
1194 if (sta_id == IWL_INVALID_STATION)
1195 return;
1196
1197 if (iwl_scan_cancel(priv)) {
1198 /* cancel scan failed, just live w/ bad key and rely
1199 briefly on SW decryption */
1200 return;
1201 }
1202
1203 iwlagn_send_sta_key(priv, keyconf, sta_id,
1204 iv32, phase1key, CMD_ASYNC);
1205}
1206
1207int iwl_remove_dynamic_key(struct iwl_priv *priv,
1208 struct iwl_rxon_context *ctx,
1209 struct ieee80211_key_conf *keyconf,
1210 struct ieee80211_sta *sta)
1211{
1212 struct iwl_addsta_cmd sta_cmd;
1213 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1214 __le16 key_flags;
1215
1216 /* if station isn't there, neither is the key */
1217 if (sta_id == IWL_INVALID_STATION)
1218 return -ENOENT;
1219
1220 spin_lock_bh(&priv->sta_lock);
1221 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1222 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1223 sta_id = IWL_INVALID_STATION;
1224 spin_unlock_bh(&priv->sta_lock);
1225
1226 if (sta_id == IWL_INVALID_STATION)
1227 return 0;
1228
1229 lockdep_assert_held(&priv->mutex);
1230
1231 ctx->key_mapping_keys--;
1232
1233 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1234 keyconf->keyidx, sta_id);
1235
1236 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1237 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1238 keyconf->hw_key_idx);
1239
1240 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1241 key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
1242 STA_KEY_FLG_INVALID;
1243
1244 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1245 key_flags |= STA_KEY_MULTICAST_MSK;
1246
1247 sta_cmd.key.key_flags = key_flags;
1248 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1249 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1250 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1251
1252 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1253}
1254
1255int iwl_set_dynamic_key(struct iwl_priv *priv,
1256 struct iwl_rxon_context *ctx,
1257 struct ieee80211_key_conf *keyconf,
1258 struct ieee80211_sta *sta)
1259{
1260 struct ieee80211_key_seq seq;
1261 u16 p1k[5];
1262 int ret;
1263 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1264 const u8 *addr;
1265
1266 if (sta_id == IWL_INVALID_STATION)
1267 return -EINVAL;
1268
1269 lockdep_assert_held(&priv->mutex);
1270
1271 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1272 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1273 return -ENOSPC;
1274
1275 ctx->key_mapping_keys++;
1276
1277 switch (keyconf->cipher) {
1278 case WLAN_CIPHER_SUITE_TKIP:
1279 if (sta)
1280 addr = sta->addr;
1281 else /* station mode case only */
1282 addr = ctx->active.bssid_addr;
1283
1284 /* pre-fill phase 1 key into device cache */
1285 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1286 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1287 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1288 seq.tkip.iv32, p1k, CMD_SYNC);
1289 break;
1290 case WLAN_CIPHER_SUITE_CCMP:
1291 case WLAN_CIPHER_SUITE_WEP40:
1292 case WLAN_CIPHER_SUITE_WEP104:
1293 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1294 0, NULL, CMD_SYNC);
1295 break;
1296 default:
1297 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
1298 ret = -EINVAL;
1299 }
1300
1301 if (ret) {
1302 ctx->key_mapping_keys--;
1303 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1304 }
1305
1306 IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1307 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1308 sta ? sta->addr : NULL, ret);
1309
1310 return ret;
1311}
1312
1313/**
1314 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1315 *
1316 * This adds the broadcast station into the driver's station table
1317 * and marks it driver active, so that it will be restored to the
1318 * device at the next best time.
1319 */
1320int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1321 struct iwl_rxon_context *ctx)
1322{
1323 struct iwl_link_quality_cmd *link_cmd;
1324 u8 sta_id;
1325
1326 spin_lock_bh(&priv->sta_lock);
1327 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1328 if (sta_id == IWL_INVALID_STATION) {
1329 IWL_ERR(priv, "Unable to prepare broadcast station\n");
1330 spin_unlock_bh(&priv->sta_lock);
1331
1332 return -EINVAL;
1333 }
1334
1335 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1336 priv->stations[sta_id].used |= IWL_STA_BCAST;
1337 spin_unlock_bh(&priv->sta_lock);
1338
1339 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1340 if (!link_cmd) {
1341 IWL_ERR(priv,
1342 "Unable to initialize rate scaling for bcast station.\n");
1343 return -ENOMEM;
1344 }
1345
1346 spin_lock_bh(&priv->sta_lock);
1347 priv->stations[sta_id].lq = link_cmd;
1348 spin_unlock_bh(&priv->sta_lock);
1349
1350 return 0;
1351}
1352
1353/**
1354 * iwl_update_bcast_station - update broadcast station's LQ command
1355 *
1356 * Only used by iwlagn. Placed here to have all bcast station management
1357 * code together.
1358 */
1359int iwl_update_bcast_station(struct iwl_priv *priv,
1360 struct iwl_rxon_context *ctx)
1361{
1362 struct iwl_link_quality_cmd *link_cmd;
1363 u8 sta_id = ctx->bcast_sta_id;
1364
1365 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
1366 if (!link_cmd) {
1367 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1368 return -ENOMEM;
1369 }
1370
1371 spin_lock_bh(&priv->sta_lock);
1372 if (priv->stations[sta_id].lq)
1373 kfree(priv->stations[sta_id].lq);
1374 else
1375 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1376 priv->stations[sta_id].lq = link_cmd;
1377 spin_unlock_bh(&priv->sta_lock);
1378
1379 return 0;
1380}
1381
1382int iwl_update_bcast_stations(struct iwl_priv *priv)
1383{
1384 struct iwl_rxon_context *ctx;
1385 int ret = 0;
1386
1387 for_each_context(priv, ctx) {
1388 ret = iwl_update_bcast_station(priv, ctx);
1389 if (ret)
1390 break;
1391 }
1392
1393 return ret;
1394}
1395
1396/**
1397 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1398 */
1399int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1400{
1401 struct iwl_addsta_cmd sta_cmd;
1402
1403 lockdep_assert_held(&priv->mutex);
1404
1405 /* Remove "disable" flag, to enable Tx for this TID */
1406 spin_lock_bh(&priv->sta_lock);
1407 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1408 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1409 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1410 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1411 spin_unlock_bh(&priv->sta_lock);
1412
1413 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1414}
1415
1416int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1417 int tid, u16 ssn)
1418{
1419 int sta_id;
1420 struct iwl_addsta_cmd sta_cmd;
1421
1422 lockdep_assert_held(&priv->mutex);
1423
1424 sta_id = iwl_sta_id(sta);
1425 if (sta_id == IWL_INVALID_STATION)
1426 return -ENXIO;
1427
1428 spin_lock_bh(&priv->sta_lock);
1429 priv->stations[sta_id].sta.station_flags_msk = 0;
1430 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1431 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1432 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1433 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1434 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1435 spin_unlock_bh(&priv->sta_lock);
1436
1437 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1438}
1439
1440int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1441 int tid)
1442{
1443 int sta_id;
1444 struct iwl_addsta_cmd sta_cmd;
1445
1446 lockdep_assert_held(&priv->mutex);
1447
1448 sta_id = iwl_sta_id(sta);
1449 if (sta_id == IWL_INVALID_STATION) {
1450 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1451 return -ENXIO;
1452 }
1453
1454 spin_lock_bh(&priv->sta_lock);
1455 priv->stations[sta_id].sta.station_flags_msk = 0;
1456 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1457 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1458 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1459 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
1460 spin_unlock_bh(&priv->sta_lock);
1461
1462 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1463}
1464
1465
1466
1467void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1468{
1469 struct iwl_addsta_cmd cmd = {
1470 .mode = STA_CONTROL_MODIFY_MSK,
1471 .station_flags = STA_FLG_PWR_SAVE_MSK,
1472 .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1473 .sta.sta_id = sta_id,
1474 .sta.modify_mask = STA_MODIFY_SLEEP_TX_COUNT_MSK,
1475 .sleep_tx_count = cpu_to_le16(cnt),
1476 };
1477
1478 iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1479}
diff --git a/drivers/net/wireless/iwlwifi/dvm/testmode.c b/drivers/net/wireless/iwlwifi/dvm/testmode.c
new file mode 100644
index 000000000000..59cfe9a8eea5
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/testmode.c
@@ -0,0 +1,1113 @@
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2010 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2010 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63#include <linux/init.h>
64#include <linux/kernel.h>
65#include <linux/module.h>
66#include <linux/dma-mapping.h>
67#include <net/net_namespace.h>
68#include <linux/netdevice.h>
69#include <net/cfg80211.h>
70#include <net/mac80211.h>
71#include <net/netlink.h>
72#include "iwl-debug.h"
73#include "iwl-io.h"
74#include "iwl-trans.h"
75#include "iwl-fh.h"
76#include "iwl-prph.h"
77#include "dev.h"
78#include "agn.h"
79#include "testmode.h"
80
81
82/* Periphery registers absolute lower bound. This is used in order to
83 * differentiate registery access through HBUS_TARG_PRPH_* and
84 * HBUS_TARG_MEM_* accesses.
85 */
86#define IWL_TM_ABS_PRPH_START (0xA00000)
87
88/* The TLVs used in the gnl message policy between the kernel module and
89 * user space application. iwl_testmode_gnl_msg_policy is to be carried
90 * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
91 * See testmode.h
92 */
93static
94struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
95 [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
96
97 [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
98 [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
99
100 [IWL_TM_ATTR_REG_OFFSET] = { .type = NLA_U32, },
101 [IWL_TM_ATTR_REG_VALUE8] = { .type = NLA_U8, },
102 [IWL_TM_ATTR_REG_VALUE32] = { .type = NLA_U32, },
103
104 [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
105 [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
106
107 [IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },
108
109 [IWL_TM_ATTR_TRACE_ADDR] = { .type = NLA_UNSPEC, },
110 [IWL_TM_ATTR_TRACE_DUMP] = { .type = NLA_UNSPEC, },
111 [IWL_TM_ATTR_TRACE_SIZE] = { .type = NLA_U32, },
112
113 [IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
114
115 [IWL_TM_ATTR_UCODE_OWNER] = { .type = NLA_U8, },
116
117 [IWL_TM_ATTR_MEM_ADDR] = { .type = NLA_U32, },
118 [IWL_TM_ATTR_BUFFER_SIZE] = { .type = NLA_U32, },
119 [IWL_TM_ATTR_BUFFER_DUMP] = { .type = NLA_UNSPEC, },
120
121 [IWL_TM_ATTR_FW_VERSION] = { .type = NLA_U32, },
122 [IWL_TM_ATTR_DEVICE_ID] = { .type = NLA_U32, },
123 [IWL_TM_ATTR_FW_TYPE] = { .type = NLA_U32, },
124 [IWL_TM_ATTR_FW_INST_SIZE] = { .type = NLA_U32, },
125 [IWL_TM_ATTR_FW_DATA_SIZE] = { .type = NLA_U32, },
126
127 [IWL_TM_ATTR_ENABLE_NOTIFICATION] = {.type = NLA_FLAG, },
128};
129
130/*
131 * See the struct iwl_rx_packet in commands.h for the format of the
132 * received events from the device
133 */
134static inline int get_event_length(struct iwl_rx_cmd_buffer *rxb)
135{
136 struct iwl_rx_packet *pkt = rxb_addr(rxb);
137 if (pkt)
138 return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
139 else
140 return 0;
141}
142
143
144/*
145 * This function multicasts the spontaneous messages from the device to the
146 * user space. It is invoked whenever there is a received messages
147 * from the device. This function is called within the ISR of the rx handlers
148 * in iwlagn driver.
149 *
150 * The parsing of the message content is left to the user space application,
151 * The message content is treated as unattacked raw data and is encapsulated
152 * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
153 *
154 * @priv: the instance of iwlwifi device
155 * @rxb: pointer to rx data content received by the ISR
156 *
157 * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
158 * For the messages multicasting to the user application, the mandatory
159 * TLV fields are :
160 * IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
161 * IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
162 */
163
164static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
165 struct iwl_rx_cmd_buffer *rxb)
166{
167 struct ieee80211_hw *hw = priv->hw;
168 struct sk_buff *skb;
169 void *data;
170 int length;
171
172 data = (void *)rxb_addr(rxb);
173 length = get_event_length(rxb);
174
175 if (!data || length == 0)
176 return;
177
178 skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
179 GFP_ATOMIC);
180 if (skb == NULL) {
181 IWL_ERR(priv,
182 "Run out of memory for messages to user space ?\n");
183 return;
184 }
185 if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT) ||
186 /* the length doesn't include len_n_flags field, so add it manually */
187 nla_put(skb, IWL_TM_ATTR_UCODE_RX_PKT, length + sizeof(__le32), data))
188 goto nla_put_failure;
189 cfg80211_testmode_event(skb, GFP_ATOMIC);
190 return;
191
192nla_put_failure:
193 kfree_skb(skb);
194 IWL_ERR(priv, "Ouch, overran buffer, check allocation!\n");
195}
196
197void iwl_testmode_init(struct iwl_priv *priv)
198{
199 priv->pre_rx_handler = NULL;
200 priv->testmode_trace.trace_enabled = false;
201 priv->testmode_mem.read_in_progress = false;
202}
203
204static void iwl_mem_cleanup(struct iwl_priv *priv)
205{
206 if (priv->testmode_mem.read_in_progress) {
207 kfree(priv->testmode_mem.buff_addr);
208 priv->testmode_mem.buff_addr = NULL;
209 priv->testmode_mem.buff_size = 0;
210 priv->testmode_mem.num_chunks = 0;
211 priv->testmode_mem.read_in_progress = false;
212 }
213}
214
215static void iwl_trace_cleanup(struct iwl_priv *priv)
216{
217 if (priv->testmode_trace.trace_enabled) {
218 if (priv->testmode_trace.cpu_addr &&
219 priv->testmode_trace.dma_addr)
220 dma_free_coherent(priv->trans->dev,
221 priv->testmode_trace.total_size,
222 priv->testmode_trace.cpu_addr,
223 priv->testmode_trace.dma_addr);
224 priv->testmode_trace.trace_enabled = false;
225 priv->testmode_trace.cpu_addr = NULL;
226 priv->testmode_trace.trace_addr = NULL;
227 priv->testmode_trace.dma_addr = 0;
228 priv->testmode_trace.buff_size = 0;
229 priv->testmode_trace.total_size = 0;
230 }
231}
232
233
234void iwl_testmode_cleanup(struct iwl_priv *priv)
235{
236 iwl_trace_cleanup(priv);
237 iwl_mem_cleanup(priv);
238}
239
240
241/*
242 * This function handles the user application commands to the ucode.
243 *
244 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
245 * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
246 * host command to the ucode.
247 *
248 * If any mandatory field is missing, -ENOMSG is replied to the user space
249 * application; otherwise, waits for the host command to be sent and checks
250 * the return code. In case or error, it is returned, otherwise a reply is
251 * allocated and the reply RX packet
252 * is returned.
253 *
254 * @hw: ieee80211_hw object that represents the device
255 * @tb: gnl message fields from the user space
256 */
257static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
258{
259 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
260 struct iwl_host_cmd cmd;
261 struct iwl_rx_packet *pkt;
262 struct sk_buff *skb;
263 void *reply_buf;
264 u32 reply_len;
265 int ret;
266 bool cmd_want_skb;
267
268 memset(&cmd, 0, sizeof(struct iwl_host_cmd));
269
270 if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
271 !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
272 IWL_ERR(priv, "Missing ucode command mandatory fields\n");
273 return -ENOMSG;
274 }
275
276 cmd.flags = CMD_ON_DEMAND | CMD_SYNC;
277 cmd_want_skb = nla_get_flag(tb[IWL_TM_ATTR_UCODE_CMD_SKB]);
278 if (cmd_want_skb)
279 cmd.flags |= CMD_WANT_SKB;
280
281 cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
282 cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
283 cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
284 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
285 IWL_DEBUG_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
286 " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
287
288 ret = iwl_dvm_send_cmd(priv, &cmd);
289 if (ret) {
290 IWL_ERR(priv, "Failed to send hcmd\n");
291 return ret;
292 }
293 if (!cmd_want_skb)
294 return ret;
295
296 /* Handling return of SKB to the user */
297 pkt = cmd.resp_pkt;
298 if (!pkt) {
299 IWL_ERR(priv, "HCMD received a null response packet\n");
300 return ret;
301 }
302
303 reply_len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
304 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, reply_len + 20);
305 reply_buf = kmalloc(reply_len, GFP_KERNEL);
306 if (!skb || !reply_buf) {
307 kfree_skb(skb);
308 kfree(reply_buf);
309 return -ENOMEM;
310 }
311
312 /* The reply is in a page, that we cannot send to user space. */
313 memcpy(reply_buf, &(pkt->hdr), reply_len);
314 iwl_free_resp(&cmd);
315
316 if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT) ||
317 nla_put(skb, IWL_TM_ATTR_UCODE_RX_PKT, reply_len, reply_buf))
318 goto nla_put_failure;
319 return cfg80211_testmode_reply(skb);
320
321nla_put_failure:
322 IWL_DEBUG_INFO(priv, "Failed creating NL attributes\n");
323 return -ENOMSG;
324}
325
326
327/*
328 * This function handles the user application commands for register access.
329 *
330 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
331 * handlers respectively.
332 *
333 * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
334 * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
335 * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
336 * the success of the command execution.
337 *
338 * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
339 * value is returned with IWL_TM_ATTR_REG_VALUE32.
340 *
341 * @hw: ieee80211_hw object that represents the device
342 * @tb: gnl message fields from the user space
343 */
344static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
345{
346 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
347 u32 ofs, val32, cmd;
348 u8 val8;
349 struct sk_buff *skb;
350 int status = 0;
351
352 if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
353 IWL_ERR(priv, "Missing register offset\n");
354 return -ENOMSG;
355 }
356 ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
357 IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
358
359 /* Allow access only to FH/CSR/HBUS in direct mode.
360 Since we don't have the upper bounds for the CSR and HBUS segments,
361 we will use only the upper bound of FH for sanity check. */
362 cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
363 if ((cmd == IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32 ||
364 cmd == IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32 ||
365 cmd == IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8) &&
366 (ofs >= FH_MEM_UPPER_BOUND)) {
367 IWL_ERR(priv, "offset out of segment (0x0 - 0x%x)\n",
368 FH_MEM_UPPER_BOUND);
369 return -EINVAL;
370 }
371
372 switch (cmd) {
373 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
374 val32 = iwl_read_direct32(priv->trans, ofs);
375 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
376
377 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
378 if (!skb) {
379 IWL_ERR(priv, "Memory allocation fail\n");
380 return -ENOMEM;
381 }
382 if (nla_put_u32(skb, IWL_TM_ATTR_REG_VALUE32, val32))
383 goto nla_put_failure;
384 status = cfg80211_testmode_reply(skb);
385 if (status < 0)
386 IWL_ERR(priv, "Error sending msg : %d\n", status);
387 break;
388 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
389 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
390 IWL_ERR(priv, "Missing value to write\n");
391 return -ENOMSG;
392 } else {
393 val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
394 IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
395 iwl_write_direct32(priv->trans, ofs, val32);
396 }
397 break;
398 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
399 if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
400 IWL_ERR(priv, "Missing value to write\n");
401 return -ENOMSG;
402 } else {
403 val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
404 IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
405 iwl_write8(priv->trans, ofs, val8);
406 }
407 break;
408 default:
409 IWL_ERR(priv, "Unknown testmode register command ID\n");
410 return -ENOSYS;
411 }
412
413 return status;
414
415nla_put_failure:
416 kfree_skb(skb);
417 return -EMSGSIZE;
418}
419
420
421static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
422{
423 struct iwl_notification_wait calib_wait;
424 static const u8 calib_complete[] = {
425 CALIBRATION_COMPLETE_NOTIFICATION
426 };
427 int ret;
428
429 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
430 calib_complete, ARRAY_SIZE(calib_complete),
431 NULL, NULL);
432 ret = iwl_init_alive_start(priv);
433 if (ret) {
434 IWL_ERR(priv, "Fail init calibration: %d\n", ret);
435 goto cfg_init_calib_error;
436 }
437
438 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait, 2 * HZ);
439 if (ret)
440 IWL_ERR(priv, "Error detecting"
441 " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
442 return ret;
443
444cfg_init_calib_error:
445 iwl_remove_notification(&priv->notif_wait, &calib_wait);
446 return ret;
447}
448
449/*
450 * This function handles the user application commands for driver.
451 *
452 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
453 * handlers respectively.
454 *
455 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
456 * value of the actual command execution is replied to the user application.
457 *
458 * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
459 * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
460 * IWL_TM_CMD_DEV2APP_SYNC_RSP.
461 *
462 * @hw: ieee80211_hw object that represents the device
463 * @tb: gnl message fields from the user space
464 */
465static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
466{
467 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
468 struct iwl_trans *trans = priv->trans;
469 struct sk_buff *skb;
470 unsigned char *rsp_data_ptr = NULL;
471 int status = 0, rsp_data_len = 0;
472 u32 devid, inst_size = 0, data_size = 0;
473 const struct fw_img *img;
474
475 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
476 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
477 rsp_data_ptr = (unsigned char *)priv->cfg->name;
478 rsp_data_len = strlen(priv->cfg->name);
479 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
480 rsp_data_len + 20);
481 if (!skb) {
482 IWL_ERR(priv, "Memory allocation fail\n");
483 return -ENOMEM;
484 }
485 if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND,
486 IWL_TM_CMD_DEV2APP_SYNC_RSP) ||
487 nla_put(skb, IWL_TM_ATTR_SYNC_RSP,
488 rsp_data_len, rsp_data_ptr))
489 goto nla_put_failure;
490 status = cfg80211_testmode_reply(skb);
491 if (status < 0)
492 IWL_ERR(priv, "Error sending msg : %d\n", status);
493 break;
494
495 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
496 status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
497 if (status)
498 IWL_ERR(priv, "Error loading init ucode: %d\n", status);
499 break;
500
501 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
502 iwl_testmode_cfg_init_calib(priv);
503 priv->ucode_loaded = false;
504 iwl_trans_stop_device(trans);
505 break;
506
507 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
508 status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
509 if (status) {
510 IWL_ERR(priv,
511 "Error loading runtime ucode: %d\n", status);
512 break;
513 }
514 status = iwl_alive_start(priv);
515 if (status)
516 IWL_ERR(priv,
517 "Error starting the device: %d\n", status);
518 break;
519
520 case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
521 iwl_scan_cancel_timeout(priv, 200);
522 priv->ucode_loaded = false;
523 iwl_trans_stop_device(trans);
524 status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
525 if (status) {
526 IWL_ERR(priv,
527 "Error loading WOWLAN ucode: %d\n", status);
528 break;
529 }
530 status = iwl_alive_start(priv);
531 if (status)
532 IWL_ERR(priv,
533 "Error starting the device: %d\n", status);
534 break;
535
536 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
537 if (priv->eeprom) {
538 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
539 priv->cfg->base_params->eeprom_size + 20);
540 if (!skb) {
541 IWL_ERR(priv, "Memory allocation fail\n");
542 return -ENOMEM;
543 }
544 if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND,
545 IWL_TM_CMD_DEV2APP_EEPROM_RSP) ||
546 nla_put(skb, IWL_TM_ATTR_EEPROM,
547 priv->cfg->base_params->eeprom_size,
548 priv->eeprom))
549 goto nla_put_failure;
550 status = cfg80211_testmode_reply(skb);
551 if (status < 0)
552 IWL_ERR(priv, "Error sending msg : %d\n",
553 status);
554 } else
555 return -EFAULT;
556 break;
557
558 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
559 if (!tb[IWL_TM_ATTR_FIXRATE]) {
560 IWL_ERR(priv, "Missing fixrate setting\n");
561 return -ENOMSG;
562 }
563 priv->tm_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
564 break;
565
566 case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
567 IWL_INFO(priv, "uCode version raw: 0x%x\n",
568 priv->fw->ucode_ver);
569
570 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
571 if (!skb) {
572 IWL_ERR(priv, "Memory allocation fail\n");
573 return -ENOMEM;
574 }
575 if (nla_put_u32(skb, IWL_TM_ATTR_FW_VERSION,
576 priv->fw->ucode_ver))
577 goto nla_put_failure;
578 status = cfg80211_testmode_reply(skb);
579 if (status < 0)
580 IWL_ERR(priv, "Error sending msg : %d\n", status);
581 break;
582
583 case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
584 devid = priv->trans->hw_id;
585 IWL_INFO(priv, "hw version: 0x%x\n", devid);
586
587 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
588 if (!skb) {
589 IWL_ERR(priv, "Memory allocation fail\n");
590 return -ENOMEM;
591 }
592 if (nla_put_u32(skb, IWL_TM_ATTR_DEVICE_ID, devid))
593 goto nla_put_failure;
594 status = cfg80211_testmode_reply(skb);
595 if (status < 0)
596 IWL_ERR(priv, "Error sending msg : %d\n", status);
597 break;
598
599 case IWL_TM_CMD_APP2DEV_GET_FW_INFO:
600 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20 + 8);
601 if (!skb) {
602 IWL_ERR(priv, "Memory allocation fail\n");
603 return -ENOMEM;
604 }
605 if (!priv->ucode_loaded) {
606 IWL_ERR(priv, "No uCode has not been loaded\n");
607 return -EINVAL;
608 } else {
609 img = &priv->fw->img[priv->cur_ucode];
610 inst_size = img->sec[IWL_UCODE_SECTION_INST].len;
611 data_size = img->sec[IWL_UCODE_SECTION_DATA].len;
612 }
613 if (nla_put_u32(skb, IWL_TM_ATTR_FW_TYPE, priv->cur_ucode) ||
614 nla_put_u32(skb, IWL_TM_ATTR_FW_INST_SIZE, inst_size) ||
615 nla_put_u32(skb, IWL_TM_ATTR_FW_DATA_SIZE, data_size))
616 goto nla_put_failure;
617 status = cfg80211_testmode_reply(skb);
618 if (status < 0)
619 IWL_ERR(priv, "Error sending msg : %d\n", status);
620 break;
621
622 default:
623 IWL_ERR(priv, "Unknown testmode driver command ID\n");
624 return -ENOSYS;
625 }
626 return status;
627
628nla_put_failure:
629 kfree_skb(skb);
630 return -EMSGSIZE;
631}
632
633
634/*
635 * This function handles the user application commands for uCode trace
636 *
637 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
638 * handlers respectively.
639 *
640 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
641 * value of the actual command execution is replied to the user application.
642 *
643 * @hw: ieee80211_hw object that represents the device
644 * @tb: gnl message fields from the user space
645 */
646static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
647{
648 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
649 struct sk_buff *skb;
650 int status = 0;
651 struct device *dev = priv->trans->dev;
652
653 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
654 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
655 if (priv->testmode_trace.trace_enabled)
656 return -EBUSY;
657
658 if (!tb[IWL_TM_ATTR_TRACE_SIZE])
659 priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
660 else
661 priv->testmode_trace.buff_size =
662 nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
663 if (!priv->testmode_trace.buff_size)
664 return -EINVAL;
665 if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
666 priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
667 return -EINVAL;
668
669 priv->testmode_trace.total_size =
670 priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
671 priv->testmode_trace.cpu_addr =
672 dma_alloc_coherent(dev,
673 priv->testmode_trace.total_size,
674 &priv->testmode_trace.dma_addr,
675 GFP_KERNEL);
676 if (!priv->testmode_trace.cpu_addr)
677 return -ENOMEM;
678 priv->testmode_trace.trace_enabled = true;
679 priv->testmode_trace.trace_addr = (u8 *)PTR_ALIGN(
680 priv->testmode_trace.cpu_addr, 0x100);
681 memset(priv->testmode_trace.trace_addr, 0x03B,
682 priv->testmode_trace.buff_size);
683 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
684 sizeof(priv->testmode_trace.dma_addr) + 20);
685 if (!skb) {
686 IWL_ERR(priv, "Memory allocation fail\n");
687 iwl_trace_cleanup(priv);
688 return -ENOMEM;
689 }
690 if (nla_put(skb, IWL_TM_ATTR_TRACE_ADDR,
691 sizeof(priv->testmode_trace.dma_addr),
692 (u64 *)&priv->testmode_trace.dma_addr))
693 goto nla_put_failure;
694 status = cfg80211_testmode_reply(skb);
695 if (status < 0) {
696 IWL_ERR(priv, "Error sending msg : %d\n", status);
697 }
698 priv->testmode_trace.num_chunks =
699 DIV_ROUND_UP(priv->testmode_trace.buff_size,
700 DUMP_CHUNK_SIZE);
701 break;
702
703 case IWL_TM_CMD_APP2DEV_END_TRACE:
704 iwl_trace_cleanup(priv);
705 break;
706 default:
707 IWL_ERR(priv, "Unknown testmode mem command ID\n");
708 return -ENOSYS;
709 }
710 return status;
711
712nla_put_failure:
713 kfree_skb(skb);
714 if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
715 IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
716 iwl_trace_cleanup(priv);
717 return -EMSGSIZE;
718}
719
720static int iwl_testmode_trace_dump(struct ieee80211_hw *hw,
721 struct sk_buff *skb,
722 struct netlink_callback *cb)
723{
724 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
725 int idx, length;
726
727 if (priv->testmode_trace.trace_enabled &&
728 priv->testmode_trace.trace_addr) {
729 idx = cb->args[4];
730 if (idx >= priv->testmode_trace.num_chunks)
731 return -ENOENT;
732 length = DUMP_CHUNK_SIZE;
733 if (((idx + 1) == priv->testmode_trace.num_chunks) &&
734 (priv->testmode_trace.buff_size % DUMP_CHUNK_SIZE))
735 length = priv->testmode_trace.buff_size %
736 DUMP_CHUNK_SIZE;
737
738 if (nla_put(skb, IWL_TM_ATTR_TRACE_DUMP, length,
739 priv->testmode_trace.trace_addr +
740 (DUMP_CHUNK_SIZE * idx)))
741 goto nla_put_failure;
742 idx++;
743 cb->args[4] = idx;
744 return 0;
745 } else
746 return -EFAULT;
747
748 nla_put_failure:
749 return -ENOBUFS;
750}
751
752/*
753 * This function handles the user application switch ucode ownership.
754 *
755 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_OWNER and
756 * decide who the current owner of the uCode
757 *
758 * If the current owner is OWNERSHIP_TM, then the only host command
759 * can deliver to uCode is from testmode, all the other host commands
760 * will dropped.
761 *
762 * default driver is the owner of uCode in normal operational mode
763 *
764 * @hw: ieee80211_hw object that represents the device
765 * @tb: gnl message fields from the user space
766 */
767static int iwl_testmode_ownership(struct ieee80211_hw *hw, struct nlattr **tb)
768{
769 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
770 u8 owner;
771
772 if (!tb[IWL_TM_ATTR_UCODE_OWNER]) {
773 IWL_ERR(priv, "Missing ucode owner\n");
774 return -ENOMSG;
775 }
776
777 owner = nla_get_u8(tb[IWL_TM_ATTR_UCODE_OWNER]);
778 if (owner == IWL_OWNERSHIP_DRIVER) {
779 priv->ucode_owner = owner;
780 priv->pre_rx_handler = NULL;
781 } else if (owner == IWL_OWNERSHIP_TM) {
782 priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
783 priv->ucode_owner = owner;
784 } else {
785 IWL_ERR(priv, "Invalid owner\n");
786 return -EINVAL;
787 }
788 return 0;
789}
790
791static int iwl_testmode_indirect_read(struct iwl_priv *priv, u32 addr, u32 size)
792{
793 struct iwl_trans *trans = priv->trans;
794 unsigned long flags;
795 int i;
796
797 if (size & 0x3)
798 return -EINVAL;
799 priv->testmode_mem.buff_size = size;
800 priv->testmode_mem.buff_addr =
801 kmalloc(priv->testmode_mem.buff_size, GFP_KERNEL);
802 if (priv->testmode_mem.buff_addr == NULL)
803 return -ENOMEM;
804
805 /* Hard-coded periphery absolute address */
806 if (IWL_TM_ABS_PRPH_START <= addr &&
807 addr < IWL_TM_ABS_PRPH_START + PRPH_END) {
808 spin_lock_irqsave(&trans->reg_lock, flags);
809 iwl_grab_nic_access(trans);
810 iwl_write32(trans, HBUS_TARG_PRPH_RADDR,
811 addr | (3 << 24));
812 for (i = 0; i < size; i += 4)
813 *(u32 *)(priv->testmode_mem.buff_addr + i) =
814 iwl_read32(trans, HBUS_TARG_PRPH_RDAT);
815 iwl_release_nic_access(trans);
816 spin_unlock_irqrestore(&trans->reg_lock, flags);
817 } else { /* target memory (SRAM) */
818 _iwl_read_targ_mem_words(trans, addr,
819 priv->testmode_mem.buff_addr,
820 priv->testmode_mem.buff_size / 4);
821 }
822
823 priv->testmode_mem.num_chunks =
824 DIV_ROUND_UP(priv->testmode_mem.buff_size, DUMP_CHUNK_SIZE);
825 priv->testmode_mem.read_in_progress = true;
826 return 0;
827
828}
829
830static int iwl_testmode_indirect_write(struct iwl_priv *priv, u32 addr,
831 u32 size, unsigned char *buf)
832{
833 struct iwl_trans *trans = priv->trans;
834 u32 val, i;
835 unsigned long flags;
836
837 if (IWL_TM_ABS_PRPH_START <= addr &&
838 addr < IWL_TM_ABS_PRPH_START + PRPH_END) {
839 /* Periphery writes can be 1-3 bytes long, or DWORDs */
840 if (size < 4) {
841 memcpy(&val, buf, size);
842 spin_lock_irqsave(&trans->reg_lock, flags);
843 iwl_grab_nic_access(trans);
844 iwl_write32(trans, HBUS_TARG_PRPH_WADDR,
845 (addr & 0x0000FFFF) |
846 ((size - 1) << 24));
847 iwl_write32(trans, HBUS_TARG_PRPH_WDAT, val);
848 iwl_release_nic_access(trans);
849 /* needed after consecutive writes w/o read */
850 mmiowb();
851 spin_unlock_irqrestore(&trans->reg_lock, flags);
852 } else {
853 if (size % 4)
854 return -EINVAL;
855 for (i = 0; i < size; i += 4)
856 iwl_write_prph(trans, addr+i,
857 *(u32 *)(buf+i));
858 }
859 } else if (iwlagn_hw_valid_rtc_data_addr(addr) ||
860 (IWLAGN_RTC_INST_LOWER_BOUND <= addr &&
861 addr < IWLAGN_RTC_INST_UPPER_BOUND)) {
862 _iwl_write_targ_mem_words(trans, addr, buf, size/4);
863 } else
864 return -EINVAL;
865 return 0;
866}
867
868/*
869 * This function handles the user application commands for SRAM data dump
870 *
871 * It retrieves the mandatory fields IWL_TM_ATTR_SRAM_ADDR and
872 * IWL_TM_ATTR_SRAM_SIZE to decide the memory area for SRAM data reading
873 *
874 * Several error will be retured, -EBUSY if the SRAM data retrieved by
875 * previous command has not been delivered to userspace, or -ENOMSG if
876 * the mandatory fields (IWL_TM_ATTR_SRAM_ADDR,IWL_TM_ATTR_SRAM_SIZE)
877 * are missing, or -ENOMEM if the buffer allocation fails.
878 *
879 * Otherwise 0 is replied indicating the success of the SRAM reading.
880 *
881 * @hw: ieee80211_hw object that represents the device
882 * @tb: gnl message fields from the user space
883 */
884static int iwl_testmode_indirect_mem(struct ieee80211_hw *hw,
885 struct nlattr **tb)
886{
887 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
888 u32 addr, size, cmd;
889 unsigned char *buf;
890
891 /* Both read and write should be blocked, for atomicity */
892 if (priv->testmode_mem.read_in_progress)
893 return -EBUSY;
894
895 cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
896 if (!tb[IWL_TM_ATTR_MEM_ADDR]) {
897 IWL_ERR(priv, "Error finding memory offset address\n");
898 return -ENOMSG;
899 }
900 addr = nla_get_u32(tb[IWL_TM_ATTR_MEM_ADDR]);
901 if (!tb[IWL_TM_ATTR_BUFFER_SIZE]) {
902 IWL_ERR(priv, "Error finding size for memory reading\n");
903 return -ENOMSG;
904 }
905 size = nla_get_u32(tb[IWL_TM_ATTR_BUFFER_SIZE]);
906
907 if (cmd == IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_READ)
908 return iwl_testmode_indirect_read(priv, addr, size);
909 else {
910 if (!tb[IWL_TM_ATTR_BUFFER_DUMP])
911 return -EINVAL;
912 buf = (unsigned char *) nla_data(tb[IWL_TM_ATTR_BUFFER_DUMP]);
913 return iwl_testmode_indirect_write(priv, addr, size, buf);
914 }
915}
916
917static int iwl_testmode_buffer_dump(struct ieee80211_hw *hw,
918 struct sk_buff *skb,
919 struct netlink_callback *cb)
920{
921 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
922 int idx, length;
923
924 if (priv->testmode_mem.read_in_progress) {
925 idx = cb->args[4];
926 if (idx >= priv->testmode_mem.num_chunks) {
927 iwl_mem_cleanup(priv);
928 return -ENOENT;
929 }
930 length = DUMP_CHUNK_SIZE;
931 if (((idx + 1) == priv->testmode_mem.num_chunks) &&
932 (priv->testmode_mem.buff_size % DUMP_CHUNK_SIZE))
933 length = priv->testmode_mem.buff_size %
934 DUMP_CHUNK_SIZE;
935
936 if (nla_put(skb, IWL_TM_ATTR_BUFFER_DUMP, length,
937 priv->testmode_mem.buff_addr +
938 (DUMP_CHUNK_SIZE * idx)))
939 goto nla_put_failure;
940 idx++;
941 cb->args[4] = idx;
942 return 0;
943 } else
944 return -EFAULT;
945
946 nla_put_failure:
947 return -ENOBUFS;
948}
949
950static int iwl_testmode_notifications(struct ieee80211_hw *hw,
951 struct nlattr **tb)
952{
953 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
954 bool enable;
955
956 enable = nla_get_flag(tb[IWL_TM_ATTR_ENABLE_NOTIFICATION]);
957 if (enable)
958 priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
959 else
960 priv->pre_rx_handler = NULL;
961 return 0;
962}
963
964
965/* The testmode gnl message handler that takes the gnl message from the
966 * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
967 * invoke the corresponding handlers.
968 *
969 * This function is invoked when there is user space application sending
970 * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
971 * by nl80211.
972 *
973 * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
974 * dispatching it to the corresponding handler.
975 *
976 * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
977 * -ENOSYS is replied to the user application if the command is unknown;
978 * Otherwise, the command is dispatched to the respective handler.
979 *
980 * @hw: ieee80211_hw object that represents the device
981 * @data: pointer to user space message
982 * @len: length in byte of @data
983 */
984int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
985{
986 struct nlattr *tb[IWL_TM_ATTR_MAX];
987 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
988 int result;
989
990 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
991 iwl_testmode_gnl_msg_policy);
992 if (result != 0) {
993 IWL_ERR(priv, "Error parsing the gnl message : %d\n", result);
994 return result;
995 }
996
997 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
998 if (!tb[IWL_TM_ATTR_COMMAND]) {
999 IWL_ERR(priv, "Missing testmode command type\n");
1000 return -ENOMSG;
1001 }
1002 /* in case multiple accesses to the device happens */
1003 mutex_lock(&priv->mutex);
1004
1005 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
1006 case IWL_TM_CMD_APP2DEV_UCODE:
1007 IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
1008 result = iwl_testmode_ucode(hw, tb);
1009 break;
1010 case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
1011 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
1012 case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
1013 IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
1014 result = iwl_testmode_reg(hw, tb);
1015 break;
1016 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
1017 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
1018 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
1019 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
1020 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
1021 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
1022 case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
1023 case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
1024 case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
1025 case IWL_TM_CMD_APP2DEV_GET_FW_INFO:
1026 IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
1027 result = iwl_testmode_driver(hw, tb);
1028 break;
1029
1030 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
1031 case IWL_TM_CMD_APP2DEV_END_TRACE:
1032 case IWL_TM_CMD_APP2DEV_READ_TRACE:
1033 IWL_DEBUG_INFO(priv, "testmode uCode trace cmd to driver\n");
1034 result = iwl_testmode_trace(hw, tb);
1035 break;
1036
1037 case IWL_TM_CMD_APP2DEV_OWNERSHIP:
1038 IWL_DEBUG_INFO(priv, "testmode change uCode ownership\n");
1039 result = iwl_testmode_ownership(hw, tb);
1040 break;
1041
1042 case IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_READ:
1043 case IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_WRITE:
1044 IWL_DEBUG_INFO(priv, "testmode indirect memory cmd "
1045 "to driver\n");
1046 result = iwl_testmode_indirect_mem(hw, tb);
1047 break;
1048
1049 case IWL_TM_CMD_APP2DEV_NOTIFICATIONS:
1050 IWL_DEBUG_INFO(priv, "testmode notifications cmd "
1051 "to driver\n");
1052 result = iwl_testmode_notifications(hw, tb);
1053 break;
1054
1055 default:
1056 IWL_ERR(priv, "Unknown testmode command\n");
1057 result = -ENOSYS;
1058 break;
1059 }
1060
1061 mutex_unlock(&priv->mutex);
1062 return result;
1063}
1064
1065int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
1066 struct netlink_callback *cb,
1067 void *data, int len)
1068{
1069 struct nlattr *tb[IWL_TM_ATTR_MAX];
1070 struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1071 int result;
1072 u32 cmd;
1073
1074 if (cb->args[3]) {
1075 /* offset by 1 since commands start at 0 */
1076 cmd = cb->args[3] - 1;
1077 } else {
1078 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
1079 iwl_testmode_gnl_msg_policy);
1080 if (result) {
1081 IWL_ERR(priv,
1082 "Error parsing the gnl message : %d\n", result);
1083 return result;
1084 }
1085
1086 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
1087 if (!tb[IWL_TM_ATTR_COMMAND]) {
1088 IWL_ERR(priv, "Missing testmode command type\n");
1089 return -ENOMSG;
1090 }
1091 cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
1092 cb->args[3] = cmd + 1;
1093 }
1094
1095 /* in case multiple accesses to the device happens */
1096 mutex_lock(&priv->mutex);
1097 switch (cmd) {
1098 case IWL_TM_CMD_APP2DEV_READ_TRACE:
1099 IWL_DEBUG_INFO(priv, "uCode trace cmd to driver\n");
1100 result = iwl_testmode_trace_dump(hw, skb, cb);
1101 break;
1102 case IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_DUMP:
1103 IWL_DEBUG_INFO(priv, "testmode sram dump cmd to driver\n");
1104 result = iwl_testmode_buffer_dump(hw, skb, cb);
1105 break;
1106 default:
1107 result = -EINVAL;
1108 break;
1109 }
1110
1111 mutex_unlock(&priv->mutex);
1112 return result;
1113}
diff --git a/drivers/net/wireless/iwlwifi/dvm/testmode.h b/drivers/net/wireless/iwlwifi/dvm/testmode.h
new file mode 100644
index 000000000000..6ba211b09426
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/testmode.h
@@ -0,0 +1,309 @@
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2010 - 2012 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2010 - 2012 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63#ifndef __IWL_TESTMODE_H__
64#define __IWL_TESTMODE_H__
65
66#include <linux/types.h>
67
68
69/*
70 * Commands from user space to kernel space(IWL_TM_CMD_ID_APP2DEV_XX) and
71 * from and kernel space to user space(IWL_TM_CMD_ID_DEV2APP_XX).
72 * The command ID is carried with IWL_TM_ATTR_COMMAND.
73 *
74 * @IWL_TM_CMD_APP2DEV_UCODE:
75 * commands from user application to the uCode,
76 * the actual uCode host command ID is carried with
77 * IWL_TM_ATTR_UCODE_CMD_ID
78 *
79 * @IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
80 * @IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
81 * @IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
82 * commands from user applicaiton to access register
83 *
84 * @IWL_TM_CMD_APP2DEV_GET_DEVICENAME: retrieve device name
85 * @IWL_TM_CMD_APP2DEV_LOAD_INIT_FW: load initial uCode image
86 * @IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB: perform calibration
87 * @IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW: load runtime uCode image
88 * @IWL_TM_CMD_APP2DEV_GET_EEPROM: request EEPROM data
89 * @IWL_TM_CMD_APP2DEV_FIXRATE_REQ: set fix MCS
90 * commands fom user space for pure driver level operations
91 *
92 * @IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
93 * @IWL_TM_CMD_APP2DEV_END_TRACE:
94 * @IWL_TM_CMD_APP2DEV_READ_TRACE:
95 * commands fom user space for uCode trace operations
96 *
97 * @IWL_TM_CMD_DEV2APP_SYNC_RSP:
98 * commands from kernel space to carry the synchronous response
99 * to user application
100 * @IWL_TM_CMD_DEV2APP_UCODE_RX_PKT:
101 * commands from kernel space to multicast the spontaneous messages
102 * to user application, or reply of host commands
103 * @IWL_TM_CMD_DEV2APP_EEPROM_RSP:
104 * commands from kernel space to carry the eeprom response
105 * to user application
106 *
107 * @IWL_TM_CMD_APP2DEV_OWNERSHIP:
108 * commands from user application to own change the ownership of the uCode
109 * if application has the ownership, the only host command from
110 * testmode will deliver to uCode. Default owner is driver
111 *
112 * @IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW: load Wake On Wireless LAN uCode image
113 * @IWL_TM_CMD_APP2DEV_GET_FW_VERSION: retrieve uCode version
114 * @IWL_TM_CMD_APP2DEV_GET_DEVICE_ID: retrieve ID information in device
115 * @IWL_TM_CMD_APP2DEV_GET_FW_INFO:
116 * retrieve information of existing loaded uCode image
117 *
118 * @IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_READ:
119 * @IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_DUMP:
120 * @IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_WRITE:
121 * Commands to read/write data from periphery or SRAM memory ranges.
122 * Fore reading, a READ command is sent from the userspace and the data
123 * is returned when the user calls a DUMP command.
124 * For writing, only a WRITE command is used.
125 * @IWL_TM_CMD_APP2DEV_NOTIFICATIONS:
126 * Command to enable/disable notifications (currently RX packets) from the
127 * driver to userspace.
128 */
129enum iwl_tm_cmd_t {
130 IWL_TM_CMD_APP2DEV_UCODE = 1,
131 IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32 = 2,
132 IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32 = 3,
133 IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8 = 4,
134 IWL_TM_CMD_APP2DEV_GET_DEVICENAME = 5,
135 IWL_TM_CMD_APP2DEV_LOAD_INIT_FW = 6,
136 IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB = 7,
137 IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW = 8,
138 IWL_TM_CMD_APP2DEV_GET_EEPROM = 9,
139 IWL_TM_CMD_APP2DEV_FIXRATE_REQ = 10,
140 IWL_TM_CMD_APP2DEV_BEGIN_TRACE = 11,
141 IWL_TM_CMD_APP2DEV_END_TRACE = 12,
142 IWL_TM_CMD_APP2DEV_READ_TRACE = 13,
143 IWL_TM_CMD_DEV2APP_SYNC_RSP = 14,
144 IWL_TM_CMD_DEV2APP_UCODE_RX_PKT = 15,
145 IWL_TM_CMD_DEV2APP_EEPROM_RSP = 16,
146 IWL_TM_CMD_APP2DEV_OWNERSHIP = 17,
147 RESERVED_18 = 18,
148 RESERVED_19 = 19,
149 RESERVED_20 = 20,
150 RESERVED_21 = 21,
151 IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW = 22,
152 IWL_TM_CMD_APP2DEV_GET_FW_VERSION = 23,
153 IWL_TM_CMD_APP2DEV_GET_DEVICE_ID = 24,
154 IWL_TM_CMD_APP2DEV_GET_FW_INFO = 25,
155 IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_READ = 26,
156 IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_DUMP = 27,
157 IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_WRITE = 28,
158 IWL_TM_CMD_APP2DEV_NOTIFICATIONS = 29,
159 IWL_TM_CMD_MAX = 30,
160};
161
162/*
163 * Atrribute filed in testmode command
164 * See enum iwl_tm_cmd_t.
165 *
166 * @IWL_TM_ATTR_NOT_APPLICABLE:
167 * The attribute is not applicable or invalid
168 * @IWL_TM_ATTR_COMMAND:
169 * From user space to kernel space:
170 * the command either destines to ucode, driver, or register;
171 * From kernel space to user space:
172 * the command either carries synchronous response,
173 * or the spontaneous message multicast from the device;
174 *
175 * @IWL_TM_ATTR_UCODE_CMD_ID:
176 * @IWL_TM_ATTR_UCODE_CMD_DATA:
177 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_UCODE,
178 * The mandatory fields are :
179 * IWL_TM_ATTR_UCODE_CMD_ID for recognizable command ID;
180 * IWL_TM_ATTR_UCODE_CMD_DATA for the actual command payload
181 * to the ucode
182 *
183 * @IWL_TM_ATTR_REG_OFFSET:
184 * @IWL_TM_ATTR_REG_VALUE8:
185 * @IWL_TM_ATTR_REG_VALUE32:
186 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_XXX,
187 * The mandatory fields are:
188 * IWL_TM_ATTR_REG_OFFSET for the offset of the target register;
189 * IWL_TM_ATTR_REG_VALUE8 or IWL_TM_ATTR_REG_VALUE32 for value
190 *
191 * @IWL_TM_ATTR_SYNC_RSP:
192 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_DEV2APP_SYNC_RSP,
193 * The mandatory fields are:
194 * IWL_TM_ATTR_SYNC_RSP for the data content responding to the user
195 * application command
196 *
197 * @IWL_TM_ATTR_UCODE_RX_PKT:
198 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_DEV2APP_UCODE_RX_PKT,
199 * The mandatory fields are:
200 * IWL_TM_ATTR_UCODE_RX_PKT for the data content multicast to the user
201 * application
202 *
203 * @IWL_TM_ATTR_EEPROM:
204 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_DEV2APP_EEPROM,
205 * The mandatory fields are:
206 * IWL_TM_ATTR_EEPROM for the data content responging to the user
207 * application
208 *
209 * @IWL_TM_ATTR_TRACE_ADDR:
210 * @IWL_TM_ATTR_TRACE_SIZE:
211 * @IWL_TM_ATTR_TRACE_DUMP:
212 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_XXX_TRACE,
213 * The mandatory fields are:
214 * IWL_TM_ATTR_MEM_TRACE_ADDR for the trace address
215 * IWL_TM_ATTR_MEM_TRACE_SIZE for the trace buffer size
216 * IWL_TM_ATTR_MEM_TRACE_DUMP for the trace dump
217 *
218 * @IWL_TM_ATTR_FIXRATE:
219 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_FIXRATE_REQ,
220 * The mandatory fields are:
221 * IWL_TM_ATTR_FIXRATE for the fixed rate
222 *
223 * @IWL_TM_ATTR_UCODE_OWNER:
224 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_OWNERSHIP,
225 * The mandatory fields are:
226 * IWL_TM_ATTR_UCODE_OWNER for the new owner
227 *
228 * @IWL_TM_ATTR_MEM_ADDR:
229 * @IWL_TM_ATTR_BUFFER_SIZE:
230 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_READ
231 * or IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_WRITE.
232 * The mandatory fields are:
233 * IWL_TM_ATTR_MEM_ADDR for the address in SRAM/periphery to read/write
234 * IWL_TM_ATTR_BUFFER_SIZE for the buffer size of data to read/write.
235 *
236 * @IWL_TM_ATTR_BUFFER_DUMP:
237 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_DUMP,
238 * IWL_TM_ATTR_BUFFER_DUMP is used for the data that was read.
239 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_WRITE,
240 * this attribute contains the data to write.
241 *
242 * @IWL_TM_ATTR_FW_VERSION:
243 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_GET_FW_VERSION,
244 * IWL_TM_ATTR_FW_VERSION for the uCode version
245 *
246 * @IWL_TM_ATTR_DEVICE_ID:
247 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_GET_DEVICE_ID,
248 * IWL_TM_ATTR_DEVICE_ID for the device ID information
249 *
250 * @IWL_TM_ATTR_FW_TYPE:
251 * @IWL_TM_ATTR_FW_INST_SIZE:
252 * @IWL_TM_ATTR_FW_DATA_SIZE:
253 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_GET_FW_INFO,
254 * The mandatory fields are:
255 * IWL_TM_ATTR_FW_TYPE for the uCode type (INIT/RUNTIME/...)
256 * IWL_TM_ATTR_FW_INST_SIZE for the size of instruction section
257 * IWL_TM_ATTR_FW_DATA_SIZE for the size of data section
258 *
259 * @IWL_TM_ATTR_UCODE_CMD_SKB:
260 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_UCODE this flag
261 * indicates that the user wants to receive the response of the command
262 * in a reply SKB. If it's not present, the response is not returned.
263 * @IWL_TM_ATTR_ENABLE_NOTIFICATIONS:
264 * When IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_NOTIFICATIONS, this
265 * flag enables (if present) or disables (if not) the forwarding
266 * to userspace.
267 */
268enum iwl_tm_attr_t {
269 IWL_TM_ATTR_NOT_APPLICABLE = 0,
270 IWL_TM_ATTR_COMMAND = 1,
271 IWL_TM_ATTR_UCODE_CMD_ID = 2,
272 IWL_TM_ATTR_UCODE_CMD_DATA = 3,
273 IWL_TM_ATTR_REG_OFFSET = 4,
274 IWL_TM_ATTR_REG_VALUE8 = 5,
275 IWL_TM_ATTR_REG_VALUE32 = 6,
276 IWL_TM_ATTR_SYNC_RSP = 7,
277 IWL_TM_ATTR_UCODE_RX_PKT = 8,
278 IWL_TM_ATTR_EEPROM = 9,
279 IWL_TM_ATTR_TRACE_ADDR = 10,
280 IWL_TM_ATTR_TRACE_SIZE = 11,
281 IWL_TM_ATTR_TRACE_DUMP = 12,
282 IWL_TM_ATTR_FIXRATE = 13,
283 IWL_TM_ATTR_UCODE_OWNER = 14,
284 IWL_TM_ATTR_MEM_ADDR = 15,
285 IWL_TM_ATTR_BUFFER_SIZE = 16,
286 IWL_TM_ATTR_BUFFER_DUMP = 17,
287 IWL_TM_ATTR_FW_VERSION = 18,
288 IWL_TM_ATTR_DEVICE_ID = 19,
289 IWL_TM_ATTR_FW_TYPE = 20,
290 IWL_TM_ATTR_FW_INST_SIZE = 21,
291 IWL_TM_ATTR_FW_DATA_SIZE = 22,
292 IWL_TM_ATTR_UCODE_CMD_SKB = 23,
293 IWL_TM_ATTR_ENABLE_NOTIFICATION = 24,
294 IWL_TM_ATTR_MAX = 25,
295};
296
297/* uCode trace buffer */
298#define TRACE_BUFF_SIZE_MAX 0x200000
299#define TRACE_BUFF_SIZE_MIN 0x20000
300#define TRACE_BUFF_SIZE_DEF TRACE_BUFF_SIZE_MIN
301#define TRACE_BUFF_PADD 0x2000
302
303/* Maximum data size of each dump it packet */
304#define DUMP_CHUNK_SIZE (PAGE_SIZE - 1024)
305
306/* Address offset of data segment in SRAM */
307#define SRAM_DATA_SEG_OFFSET 0x800000
308
309#endif
diff --git a/drivers/net/wireless/iwlwifi/dvm/tt.c b/drivers/net/wireless/iwlwifi/dvm/tt.c
new file mode 100644
index 000000000000..55418899bc4e
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/tt.c
@@ -0,0 +1,694 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <net/mac80211.h>
35#include "iwl-io.h"
36#include "iwl-modparams.h"
37#include "iwl-debug.h"
38#include "agn.h"
39#include "eeprom.h"
40#include "dev.h"
41#include "commands.h"
42#include "tt.h"
43
44/* default Thermal Throttling transaction table
45 * Current state | Throttling Down | Throttling Up
46 *=============================================================================
47 * Condition Nxt State Condition Nxt State Condition Nxt State
48 *-----------------------------------------------------------------------------
49 * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
50 * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
51 * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
52 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
53 *=============================================================================
54 */
55static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
56 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
57 {IWL_TI_1, 105, CT_KILL_THRESHOLD - 1},
58 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
59};
60static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
61 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
62 {IWL_TI_2, 110, CT_KILL_THRESHOLD - 1},
63 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
64};
65static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
66 {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
67 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX},
68 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
69};
70static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
71 {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
72 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
73 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
74};
75
76/* Advance Thermal Throttling default restriction table */
77static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
78 {IWL_ANT_OK_MULTI, IWL_ANT_OK_MULTI, true },
79 {IWL_ANT_OK_SINGLE, IWL_ANT_OK_MULTI, true },
80 {IWL_ANT_OK_SINGLE, IWL_ANT_OK_SINGLE, false },
81 {IWL_ANT_OK_NONE, IWL_ANT_OK_NONE, false }
82};
83
84bool iwl_tt_is_low_power_state(struct iwl_priv *priv)
85{
86 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
87
88 if (tt->state >= IWL_TI_1)
89 return true;
90 return false;
91}
92
93u8 iwl_tt_current_power_mode(struct iwl_priv *priv)
94{
95 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
96
97 return tt->tt_power_mode;
98}
99
100bool iwl_ht_enabled(struct iwl_priv *priv)
101{
102 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
103 struct iwl_tt_restriction *restriction;
104
105 if (!priv->thermal_throttle.advanced_tt)
106 return true;
107 restriction = tt->restriction + tt->state;
108 return restriction->is_ht;
109}
110
111static bool iwl_within_ct_kill_margin(struct iwl_priv *priv)
112{
113 s32 temp = priv->temperature; /* degrees CELSIUS except specified */
114 bool within_margin = false;
115
116 if (!priv->thermal_throttle.advanced_tt)
117 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
118 CT_KILL_THRESHOLD_LEGACY) ? true : false;
119 else
120 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
121 CT_KILL_THRESHOLD) ? true : false;
122 return within_margin;
123}
124
125bool iwl_check_for_ct_kill(struct iwl_priv *priv)
126{
127 bool is_ct_kill = false;
128
129 if (iwl_within_ct_kill_margin(priv)) {
130 iwl_tt_enter_ct_kill(priv);
131 is_ct_kill = true;
132 }
133 return is_ct_kill;
134}
135
136enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
137{
138 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
139 struct iwl_tt_restriction *restriction;
140
141 if (!priv->thermal_throttle.advanced_tt)
142 return IWL_ANT_OK_MULTI;
143 restriction = tt->restriction + tt->state;
144 return restriction->tx_stream;
145}
146
147enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
148{
149 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
150 struct iwl_tt_restriction *restriction;
151
152 if (!priv->thermal_throttle.advanced_tt)
153 return IWL_ANT_OK_MULTI;
154 restriction = tt->restriction + tt->state;
155 return restriction->rx_stream;
156}
157
158#define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
159#define CT_KILL_WAITING_DURATION (300) /* 300ms duration */
160
161/*
162 * toggle the bit to wake up uCode and check the temperature
163 * if the temperature is below CT, uCode will stay awake and send card
164 * state notification with CT_KILL bit clear to inform Thermal Throttling
165 * Management to change state. Otherwise, uCode will go back to sleep
166 * without doing anything, driver should continue the 5 seconds timer
167 * to wake up uCode for temperature check until temperature drop below CT
168 */
169static void iwl_tt_check_exit_ct_kill(unsigned long data)
170{
171 struct iwl_priv *priv = (struct iwl_priv *)data;
172 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
173 unsigned long flags;
174
175 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
176 return;
177
178 if (tt->state == IWL_TI_CT_KILL) {
179 if (priv->thermal_throttle.ct_kill_toggle) {
180 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
181 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
182 priv->thermal_throttle.ct_kill_toggle = false;
183 } else {
184 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
185 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
186 priv->thermal_throttle.ct_kill_toggle = true;
187 }
188 iwl_read32(priv->trans, CSR_UCODE_DRV_GP1);
189 spin_lock_irqsave(&priv->trans->reg_lock, flags);
190 if (likely(iwl_grab_nic_access(priv->trans)))
191 iwl_release_nic_access(priv->trans);
192 spin_unlock_irqrestore(&priv->trans->reg_lock, flags);
193
194 /* Reschedule the ct_kill timer to occur in
195 * CT_KILL_EXIT_DURATION seconds to ensure we get a
196 * thermal update */
197 IWL_DEBUG_TEMP(priv, "schedule ct_kill exit timer\n");
198 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
199 jiffies + CT_KILL_EXIT_DURATION * HZ);
200 }
201}
202
203static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
204 bool stop)
205{
206 if (stop) {
207 IWL_DEBUG_TEMP(priv, "Stop all queues\n");
208 if (priv->mac80211_registered)
209 ieee80211_stop_queues(priv->hw);
210 IWL_DEBUG_TEMP(priv,
211 "Schedule 5 seconds CT_KILL Timer\n");
212 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
213 jiffies + CT_KILL_EXIT_DURATION * HZ);
214 } else {
215 IWL_DEBUG_TEMP(priv, "Wake all queues\n");
216 if (priv->mac80211_registered)
217 ieee80211_wake_queues(priv->hw);
218 }
219}
220
221static void iwl_tt_ready_for_ct_kill(unsigned long data)
222{
223 struct iwl_priv *priv = (struct iwl_priv *)data;
224 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
225
226 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
227 return;
228
229 /* temperature timer expired, ready to go into CT_KILL state */
230 if (tt->state != IWL_TI_CT_KILL) {
231 IWL_DEBUG_TEMP(priv, "entering CT_KILL state when "
232 "temperature timer expired\n");
233 tt->state = IWL_TI_CT_KILL;
234 set_bit(STATUS_CT_KILL, &priv->status);
235 iwl_perform_ct_kill_task(priv, true);
236 }
237}
238
239static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
240{
241 IWL_DEBUG_TEMP(priv, "Prepare to enter IWL_TI_CT_KILL\n");
242 /* make request to retrieve statistics information */
243 iwl_send_statistics_request(priv, CMD_SYNC, false);
244 /* Reschedule the ct_kill wait timer */
245 mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
246 jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
247}
248
249#define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
250#define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
251#define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
252
253/*
254 * Legacy thermal throttling
255 * 1) Avoid NIC destruction due to high temperatures
256 * Chip will identify dangerously high temperatures that can
257 * harm the device and will power down
258 * 2) Avoid the NIC power down due to high temperature
259 * Throttle early enough to lower the power consumption before
260 * drastic steps are needed
261 */
262static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
263{
264 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
265 enum iwl_tt_state old_state;
266
267#ifdef CONFIG_IWLWIFI_DEBUG
268 if ((tt->tt_previous_temp) &&
269 (temp > tt->tt_previous_temp) &&
270 ((temp - tt->tt_previous_temp) >
271 IWL_TT_INCREASE_MARGIN)) {
272 IWL_DEBUG_TEMP(priv,
273 "Temperature increase %d degree Celsius\n",
274 (temp - tt->tt_previous_temp));
275 }
276#endif
277 old_state = tt->state;
278 /* in Celsius */
279 if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
280 tt->state = IWL_TI_CT_KILL;
281 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
282 tt->state = IWL_TI_2;
283 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
284 tt->state = IWL_TI_1;
285 else
286 tt->state = IWL_TI_0;
287
288#ifdef CONFIG_IWLWIFI_DEBUG
289 tt->tt_previous_temp = temp;
290#endif
291 /* stop ct_kill_waiting_tm timer */
292 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
293 if (tt->state != old_state) {
294 switch (tt->state) {
295 case IWL_TI_0:
296 /*
297 * When the system is ready to go back to IWL_TI_0
298 * we only have to call iwl_power_update_mode() to
299 * do so.
300 */
301 break;
302 case IWL_TI_1:
303 tt->tt_power_mode = IWL_POWER_INDEX_3;
304 break;
305 case IWL_TI_2:
306 tt->tt_power_mode = IWL_POWER_INDEX_4;
307 break;
308 default:
309 tt->tt_power_mode = IWL_POWER_INDEX_5;
310 break;
311 }
312 mutex_lock(&priv->mutex);
313 if (old_state == IWL_TI_CT_KILL)
314 clear_bit(STATUS_CT_KILL, &priv->status);
315 if (tt->state != IWL_TI_CT_KILL &&
316 iwl_power_update_mode(priv, true)) {
317 /* TT state not updated
318 * try again during next temperature read
319 */
320 if (old_state == IWL_TI_CT_KILL)
321 set_bit(STATUS_CT_KILL, &priv->status);
322 tt->state = old_state;
323 IWL_ERR(priv, "Cannot update power mode, "
324 "TT state not updated\n");
325 } else {
326 if (tt->state == IWL_TI_CT_KILL) {
327 if (force) {
328 set_bit(STATUS_CT_KILL, &priv->status);
329 iwl_perform_ct_kill_task(priv, true);
330 } else {
331 iwl_prepare_ct_kill_task(priv);
332 tt->state = old_state;
333 }
334 } else if (old_state == IWL_TI_CT_KILL &&
335 tt->state != IWL_TI_CT_KILL)
336 iwl_perform_ct_kill_task(priv, false);
337 IWL_DEBUG_TEMP(priv, "Temperature state changed %u\n",
338 tt->state);
339 IWL_DEBUG_TEMP(priv, "Power Index change to %u\n",
340 tt->tt_power_mode);
341 }
342 mutex_unlock(&priv->mutex);
343 }
344}
345
346/*
347 * Advance thermal throttling
348 * 1) Avoid NIC destruction due to high temperatures
349 * Chip will identify dangerously high temperatures that can
350 * harm the device and will power down
351 * 2) Avoid the NIC power down due to high temperature
352 * Throttle early enough to lower the power consumption before
353 * drastic steps are needed
354 * Actions include relaxing the power down sleep thresholds and
355 * decreasing the number of TX streams
356 * 3) Avoid throughput performance impact as much as possible
357 *
358 *=============================================================================
359 * Condition Nxt State Condition Nxt State Condition Nxt State
360 *-----------------------------------------------------------------------------
361 * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
362 * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
363 * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
364 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
365 *=============================================================================
366 */
367static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
368{
369 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
370 int i;
371 bool changed = false;
372 enum iwl_tt_state old_state;
373 struct iwl_tt_trans *transaction;
374
375 old_state = tt->state;
376 for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
377 /* based on the current TT state,
378 * find the curresponding transaction table
379 * each table has (IWL_TI_STATE_MAX - 1) entries
380 * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
381 * will advance to the correct table.
382 * then based on the current temperature
383 * find the next state need to transaction to
384 * go through all the possible (IWL_TI_STATE_MAX - 1) entries
385 * in the current table to see if transaction is needed
386 */
387 transaction = tt->transaction +
388 ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
389 if (temp >= transaction->tt_low &&
390 temp <= transaction->tt_high) {
391#ifdef CONFIG_IWLWIFI_DEBUG
392 if ((tt->tt_previous_temp) &&
393 (temp > tt->tt_previous_temp) &&
394 ((temp - tt->tt_previous_temp) >
395 IWL_TT_INCREASE_MARGIN)) {
396 IWL_DEBUG_TEMP(priv,
397 "Temperature increase %d "
398 "degree Celsius\n",
399 (temp - tt->tt_previous_temp));
400 }
401 tt->tt_previous_temp = temp;
402#endif
403 if (old_state !=
404 transaction->next_state) {
405 changed = true;
406 tt->state =
407 transaction->next_state;
408 }
409 break;
410 }
411 }
412 /* stop ct_kill_waiting_tm timer */
413 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
414 if (changed) {
415 if (tt->state >= IWL_TI_1) {
416 /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
417 tt->tt_power_mode = IWL_POWER_INDEX_5;
418
419 if (!iwl_ht_enabled(priv)) {
420 struct iwl_rxon_context *ctx;
421
422 for_each_context(priv, ctx) {
423 struct iwl_rxon_cmd *rxon;
424
425 rxon = &ctx->staging;
426
427 /* disable HT */
428 rxon->flags &= ~(
429 RXON_FLG_CHANNEL_MODE_MSK |
430 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
431 RXON_FLG_HT40_PROT_MSK |
432 RXON_FLG_HT_PROT_MSK);
433 }
434 } else {
435 /* check HT capability and set
436 * according to the system HT capability
437 * in case get disabled before */
438 iwl_set_rxon_ht(priv, &priv->current_ht_config);
439 }
440
441 } else {
442 /*
443 * restore system power setting -- it will be
444 * recalculated automatically.
445 */
446
447 /* check HT capability and set
448 * according to the system HT capability
449 * in case get disabled before */
450 iwl_set_rxon_ht(priv, &priv->current_ht_config);
451 }
452 mutex_lock(&priv->mutex);
453 if (old_state == IWL_TI_CT_KILL)
454 clear_bit(STATUS_CT_KILL, &priv->status);
455 if (tt->state != IWL_TI_CT_KILL &&
456 iwl_power_update_mode(priv, true)) {
457 /* TT state not updated
458 * try again during next temperature read
459 */
460 IWL_ERR(priv, "Cannot update power mode, "
461 "TT state not updated\n");
462 if (old_state == IWL_TI_CT_KILL)
463 set_bit(STATUS_CT_KILL, &priv->status);
464 tt->state = old_state;
465 } else {
466 IWL_DEBUG_TEMP(priv,
467 "Thermal Throttling to new state: %u\n",
468 tt->state);
469 if (old_state != IWL_TI_CT_KILL &&
470 tt->state == IWL_TI_CT_KILL) {
471 if (force) {
472 IWL_DEBUG_TEMP(priv,
473 "Enter IWL_TI_CT_KILL\n");
474 set_bit(STATUS_CT_KILL, &priv->status);
475 iwl_perform_ct_kill_task(priv, true);
476 } else {
477 iwl_prepare_ct_kill_task(priv);
478 tt->state = old_state;
479 }
480 } else if (old_state == IWL_TI_CT_KILL &&
481 tt->state != IWL_TI_CT_KILL) {
482 IWL_DEBUG_TEMP(priv, "Exit IWL_TI_CT_KILL\n");
483 iwl_perform_ct_kill_task(priv, false);
484 }
485 }
486 mutex_unlock(&priv->mutex);
487 }
488}
489
490/* Card State Notification indicated reach critical temperature
491 * if PSP not enable, no Thermal Throttling function will be performed
492 * just set the GP1 bit to acknowledge the event
493 * otherwise, go into IWL_TI_CT_KILL state
494 * since Card State Notification will not provide any temperature reading
495 * for Legacy mode
496 * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
497 * for advance mode
498 * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
499 */
500static void iwl_bg_ct_enter(struct work_struct *work)
501{
502 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
503 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
504
505 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
506 return;
507
508 if (!iwl_is_ready(priv))
509 return;
510
511 if (tt->state != IWL_TI_CT_KILL) {
512 IWL_ERR(priv, "Device reached critical temperature "
513 "- ucode going to sleep!\n");
514 if (!priv->thermal_throttle.advanced_tt)
515 iwl_legacy_tt_handler(priv,
516 IWL_MINIMAL_POWER_THRESHOLD,
517 true);
518 else
519 iwl_advance_tt_handler(priv,
520 CT_KILL_THRESHOLD + 1, true);
521 }
522}
523
524/* Card State Notification indicated out of critical temperature
525 * since Card State Notification will not provide any temperature reading
526 * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
527 * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
528 */
529static void iwl_bg_ct_exit(struct work_struct *work)
530{
531 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
532 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
533
534 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
535 return;
536
537 if (!iwl_is_ready(priv))
538 return;
539
540 /* stop ct_kill_exit_tm timer */
541 del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
542
543 if (tt->state == IWL_TI_CT_KILL) {
544 IWL_ERR(priv,
545 "Device temperature below critical"
546 "- ucode awake!\n");
547 /*
548 * exit from CT_KILL state
549 * reset the current temperature reading
550 */
551 priv->temperature = 0;
552 if (!priv->thermal_throttle.advanced_tt)
553 iwl_legacy_tt_handler(priv,
554 IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
555 true);
556 else
557 iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
558 true);
559 }
560}
561
562void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
563{
564 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
565 return;
566
567 IWL_DEBUG_TEMP(priv, "Queueing critical temperature enter.\n");
568 queue_work(priv->workqueue, &priv->ct_enter);
569}
570
571void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
572{
573 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
574 return;
575
576 IWL_DEBUG_TEMP(priv, "Queueing critical temperature exit.\n");
577 queue_work(priv->workqueue, &priv->ct_exit);
578}
579
580static void iwl_bg_tt_work(struct work_struct *work)
581{
582 struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
583 s32 temp = priv->temperature; /* degrees CELSIUS except specified */
584
585 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
586 return;
587
588 if (!priv->thermal_throttle.advanced_tt)
589 iwl_legacy_tt_handler(priv, temp, false);
590 else
591 iwl_advance_tt_handler(priv, temp, false);
592}
593
594void iwl_tt_handler(struct iwl_priv *priv)
595{
596 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
597 return;
598
599 IWL_DEBUG_TEMP(priv, "Queueing thermal throttling work.\n");
600 queue_work(priv->workqueue, &priv->tt_work);
601}
602
603/* Thermal throttling initialization
604 * For advance thermal throttling:
605 * Initialize Thermal Index and temperature threshold table
606 * Initialize thermal throttling restriction table
607 */
608void iwl_tt_initialize(struct iwl_priv *priv)
609{
610 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
611 int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
612 struct iwl_tt_trans *transaction;
613
614 IWL_DEBUG_TEMP(priv, "Initialize Thermal Throttling\n");
615
616 memset(tt, 0, sizeof(struct iwl_tt_mgmt));
617
618 tt->state = IWL_TI_0;
619 init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
620 priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
621 priv->thermal_throttle.ct_kill_exit_tm.function =
622 iwl_tt_check_exit_ct_kill;
623 init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
624 priv->thermal_throttle.ct_kill_waiting_tm.data =
625 (unsigned long)priv;
626 priv->thermal_throttle.ct_kill_waiting_tm.function =
627 iwl_tt_ready_for_ct_kill;
628 /* setup deferred ct kill work */
629 INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
630 INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
631 INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
632
633 if (priv->cfg->base_params->adv_thermal_throttle) {
634 IWL_DEBUG_TEMP(priv, "Advanced Thermal Throttling\n");
635 tt->restriction = kcalloc(IWL_TI_STATE_MAX,
636 sizeof(struct iwl_tt_restriction),
637 GFP_KERNEL);
638 tt->transaction = kcalloc(IWL_TI_STATE_MAX *
639 (IWL_TI_STATE_MAX - 1),
640 sizeof(struct iwl_tt_trans),
641 GFP_KERNEL);
642 if (!tt->restriction || !tt->transaction) {
643 IWL_ERR(priv, "Fallback to Legacy Throttling\n");
644 priv->thermal_throttle.advanced_tt = false;
645 kfree(tt->restriction);
646 tt->restriction = NULL;
647 kfree(tt->transaction);
648 tt->transaction = NULL;
649 } else {
650 transaction = tt->transaction +
651 (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
652 memcpy(transaction, &tt_range_0[0], size);
653 transaction = tt->transaction +
654 (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
655 memcpy(transaction, &tt_range_1[0], size);
656 transaction = tt->transaction +
657 (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
658 memcpy(transaction, &tt_range_2[0], size);
659 transaction = tt->transaction +
660 (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
661 memcpy(transaction, &tt_range_3[0], size);
662 size = sizeof(struct iwl_tt_restriction) *
663 IWL_TI_STATE_MAX;
664 memcpy(tt->restriction,
665 &restriction_range[0], size);
666 priv->thermal_throttle.advanced_tt = true;
667 }
668 } else {
669 IWL_DEBUG_TEMP(priv, "Legacy Thermal Throttling\n");
670 priv->thermal_throttle.advanced_tt = false;
671 }
672}
673
674/* cleanup thermal throttling management related memory and timer */
675void iwl_tt_exit(struct iwl_priv *priv)
676{
677 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
678
679 /* stop ct_kill_exit_tm timer if activated */
680 del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
681 /* stop ct_kill_waiting_tm timer if activated */
682 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
683 cancel_work_sync(&priv->tt_work);
684 cancel_work_sync(&priv->ct_enter);
685 cancel_work_sync(&priv->ct_exit);
686
687 if (priv->thermal_throttle.advanced_tt) {
688 /* free advance thermal throttling memory */
689 kfree(tt->restriction);
690 tt->restriction = NULL;
691 kfree(tt->transaction);
692 tt->transaction = NULL;
693 }
694}
diff --git a/drivers/net/wireless/iwlwifi/dvm/tt.h b/drivers/net/wireless/iwlwifi/dvm/tt.h
new file mode 100644
index 000000000000..44c7c8f30a2d
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/tt.h
@@ -0,0 +1,128 @@
1/******************************************************************************
2 *
3 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28#ifndef __iwl_tt_setting_h__
29#define __iwl_tt_setting_h__
30
31#include "commands.h"
32
33#define IWL_ABSOLUTE_ZERO 0
34#define IWL_ABSOLUTE_MAX 0xFFFFFFFF
35#define IWL_TT_INCREASE_MARGIN 5
36#define IWL_TT_CT_KILL_MARGIN 3
37
38enum iwl_antenna_ok {
39 IWL_ANT_OK_NONE,
40 IWL_ANT_OK_SINGLE,
41 IWL_ANT_OK_MULTI,
42};
43
44/* Thermal Throttling State Machine states */
45enum iwl_tt_state {
46 IWL_TI_0, /* normal temperature, system power state */
47 IWL_TI_1, /* high temperature detect, low power state */
48 IWL_TI_2, /* higher temperature detected, lower power state */
49 IWL_TI_CT_KILL, /* critical temperature detected, lowest power state */
50 IWL_TI_STATE_MAX
51};
52
53/**
54 * struct iwl_tt_restriction - Thermal Throttling restriction table
55 * @tx_stream: number of tx stream allowed
56 * @is_ht: ht enable/disable
57 * @rx_stream: number of rx stream allowed
58 *
59 * This table is used by advance thermal throttling management
60 * based on the current thermal throttling state, and determines
61 * the number of tx/rx streams and the status of HT operation.
62 */
63struct iwl_tt_restriction {
64 enum iwl_antenna_ok tx_stream;
65 enum iwl_antenna_ok rx_stream;
66 bool is_ht;
67};
68
69/**
70 * struct iwl_tt_trans - Thermal Throttling transaction table
71 * @next_state: next thermal throttling mode
72 * @tt_low: low temperature threshold to change state
73 * @tt_high: high temperature threshold to change state
74 *
75 * This is used by the advanced thermal throttling algorithm
76 * to determine the next thermal state to go based on the
77 * current temperature.
78 */
79struct iwl_tt_trans {
80 enum iwl_tt_state next_state;
81 u32 tt_low;
82 u32 tt_high;
83};
84
85/**
86 * struct iwl_tt_mgnt - Thermal Throttling Management structure
87 * @advanced_tt: advanced thermal throttle required
88 * @state: current Thermal Throttling state
89 * @tt_power_mode: Thermal Throttling power mode index
90 * being used to set power level when
91 * when thermal throttling state != IWL_TI_0
92 * the tt_power_mode should set to different
93 * power mode based on the current tt state
94 * @tt_previous_temperature: last measured temperature
95 * @iwl_tt_restriction: ptr to restriction tbl, used by advance
96 * thermal throttling to determine how many tx/rx streams
97 * should be used in tt state; and can HT be enabled or not
98 * @iwl_tt_trans: ptr to adv trans table, used by advance thermal throttling
99 * state transaction
100 * @ct_kill_toggle: used to toggle the CSR bit when checking uCode temperature
101 * @ct_kill_exit_tm: timer to exit thermal kill
102 */
103struct iwl_tt_mgmt {
104 enum iwl_tt_state state;
105 bool advanced_tt;
106 u8 tt_power_mode;
107 bool ct_kill_toggle;
108#ifdef CONFIG_IWLWIFI_DEBUG
109 s32 tt_previous_temp;
110#endif
111 struct iwl_tt_restriction *restriction;
112 struct iwl_tt_trans *transaction;
113 struct timer_list ct_kill_exit_tm;
114 struct timer_list ct_kill_waiting_tm;
115};
116
117u8 iwl_tt_current_power_mode(struct iwl_priv *priv);
118bool iwl_tt_is_low_power_state(struct iwl_priv *priv);
119bool iwl_ht_enabled(struct iwl_priv *priv);
120enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv);
121enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv);
122void iwl_tt_enter_ct_kill(struct iwl_priv *priv);
123void iwl_tt_exit_ct_kill(struct iwl_priv *priv);
124void iwl_tt_handler(struct iwl_priv *priv);
125void iwl_tt_initialize(struct iwl_priv *priv);
126void iwl_tt_exit(struct iwl_priv *priv);
127
128#endif /* __iwl_tt_setting_h__ */
diff --git a/drivers/net/wireless/iwlwifi/dvm/tx.c b/drivers/net/wireless/iwlwifi/dvm/tx.c
new file mode 100644
index 000000000000..52f2cae080c3
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/tx.c
@@ -0,0 +1,1385 @@
1/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/sched.h>
34#include <linux/ieee80211.h>
35#include "iwl-io.h"
36#include "iwl-trans.h"
37#include "iwl-agn-hw.h"
38#include "dev.h"
39#include "agn.h"
40
41static const u8 tid_to_ac[] = {
42 IEEE80211_AC_BE,
43 IEEE80211_AC_BK,
44 IEEE80211_AC_BK,
45 IEEE80211_AC_BE,
46 IEEE80211_AC_VI,
47 IEEE80211_AC_VI,
48 IEEE80211_AC_VO,
49 IEEE80211_AC_VO,
50};
51
52static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
53 struct ieee80211_tx_info *info,
54 __le16 fc, __le32 *tx_flags)
55{
56 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
57 info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT ||
58 info->flags & IEEE80211_TX_CTL_AMPDU)
59 *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
60}
61
62/*
63 * handle build REPLY_TX command notification.
64 */
65static void iwlagn_tx_cmd_build_basic(struct iwl_priv *priv,
66 struct sk_buff *skb,
67 struct iwl_tx_cmd *tx_cmd,
68 struct ieee80211_tx_info *info,
69 struct ieee80211_hdr *hdr, u8 sta_id)
70{
71 __le16 fc = hdr->frame_control;
72 __le32 tx_flags = tx_cmd->tx_flags;
73
74 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
75
76 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
77 tx_flags |= TX_CMD_FLG_ACK_MSK;
78 else
79 tx_flags &= ~TX_CMD_FLG_ACK_MSK;
80
81 if (ieee80211_is_probe_resp(fc))
82 tx_flags |= TX_CMD_FLG_TSF_MSK;
83 else if (ieee80211_is_back_req(fc))
84 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
85 else if (info->band == IEEE80211_BAND_2GHZ &&
86 priv->cfg->bt_params &&
87 priv->cfg->bt_params->advanced_bt_coexist &&
88 (ieee80211_is_auth(fc) || ieee80211_is_assoc_req(fc) ||
89 ieee80211_is_reassoc_req(fc) ||
90 skb->protocol == cpu_to_be16(ETH_P_PAE)))
91 tx_flags |= TX_CMD_FLG_IGNORE_BT;
92
93
94 tx_cmd->sta_id = sta_id;
95 if (ieee80211_has_morefrags(fc))
96 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
97
98 if (ieee80211_is_data_qos(fc)) {
99 u8 *qc = ieee80211_get_qos_ctl(hdr);
100 tx_cmd->tid_tspec = qc[0] & 0xf;
101 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
102 } else {
103 tx_cmd->tid_tspec = IWL_TID_NON_QOS;
104 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
105 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
106 else
107 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
108 }
109
110 iwlagn_tx_cmd_protection(priv, info, fc, &tx_flags);
111
112 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
113 if (ieee80211_is_mgmt(fc)) {
114 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
115 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
116 else
117 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
118 } else {
119 tx_cmd->timeout.pm_frame_timeout = 0;
120 }
121
122 tx_cmd->driver_txop = 0;
123 tx_cmd->tx_flags = tx_flags;
124 tx_cmd->next_frame_len = 0;
125}
126
127static void iwlagn_tx_cmd_build_rate(struct iwl_priv *priv,
128 struct iwl_tx_cmd *tx_cmd,
129 struct ieee80211_tx_info *info,
130 __le16 fc)
131{
132 u32 rate_flags;
133 int rate_idx;
134 u8 rts_retry_limit;
135 u8 data_retry_limit;
136 u8 rate_plcp;
137
138 if (priv->wowlan) {
139 rts_retry_limit = IWLAGN_LOW_RETRY_LIMIT;
140 data_retry_limit = IWLAGN_LOW_RETRY_LIMIT;
141 } else {
142 /* Set retry limit on RTS packets */
143 rts_retry_limit = IWLAGN_RTS_DFAULT_RETRY_LIMIT;
144
145 /* Set retry limit on DATA packets and Probe Responses*/
146 if (ieee80211_is_probe_resp(fc)) {
147 data_retry_limit = IWLAGN_MGMT_DFAULT_RETRY_LIMIT;
148 rts_retry_limit =
149 min(data_retry_limit, rts_retry_limit);
150 } else if (ieee80211_is_back_req(fc))
151 data_retry_limit = IWLAGN_BAR_DFAULT_RETRY_LIMIT;
152 else
153 data_retry_limit = IWLAGN_DEFAULT_TX_RETRY;
154 }
155
156 tx_cmd->data_retry_limit = data_retry_limit;
157 tx_cmd->rts_retry_limit = rts_retry_limit;
158
159 /* DATA packets will use the uCode station table for rate/antenna
160 * selection */
161 if (ieee80211_is_data(fc)) {
162 tx_cmd->initial_rate_index = 0;
163 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
164#ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
165 if (priv->tm_fixed_rate) {
166 /*
167 * rate overwrite by testmode
168 * we not only send lq command to change rate
169 * we also re-enforce per data pkt base.
170 */
171 tx_cmd->tx_flags &= ~TX_CMD_FLG_STA_RATE_MSK;
172 memcpy(&tx_cmd->rate_n_flags, &priv->tm_fixed_rate,
173 sizeof(tx_cmd->rate_n_flags));
174 }
175#endif
176 return;
177 } else if (ieee80211_is_back_req(fc))
178 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
179
180 /**
181 * If the current TX rate stored in mac80211 has the MCS bit set, it's
182 * not really a TX rate. Thus, we use the lowest supported rate for
183 * this band. Also use the lowest supported rate if the stored rate
184 * index is invalid.
185 */
186 rate_idx = info->control.rates[0].idx;
187 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS ||
188 (rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY))
189 rate_idx = rate_lowest_index(&priv->bands[info->band],
190 info->control.sta);
191 /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
192 if (info->band == IEEE80211_BAND_5GHZ)
193 rate_idx += IWL_FIRST_OFDM_RATE;
194 /* Get PLCP rate for tx_cmd->rate_n_flags */
195 rate_plcp = iwl_rates[rate_idx].plcp;
196 /* Zero out flags for this packet */
197 rate_flags = 0;
198
199 /* Set CCK flag as needed */
200 if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
201 rate_flags |= RATE_MCS_CCK_MSK;
202
203 /* Set up antennas */
204 if (priv->cfg->bt_params &&
205 priv->cfg->bt_params->advanced_bt_coexist &&
206 priv->bt_full_concurrent) {
207 /* operated as 1x1 in full concurrency mode */
208 priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
209 first_antenna(priv->hw_params.valid_tx_ant));
210 } else
211 priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
212 priv->hw_params.valid_tx_ant);
213 rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
214
215 /* Set the rate in the TX cmd */
216 tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
217}
218
219static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
220 struct ieee80211_tx_info *info,
221 struct iwl_tx_cmd *tx_cmd,
222 struct sk_buff *skb_frag)
223{
224 struct ieee80211_key_conf *keyconf = info->control.hw_key;
225
226 switch (keyconf->cipher) {
227 case WLAN_CIPHER_SUITE_CCMP:
228 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
229 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
230 if (info->flags & IEEE80211_TX_CTL_AMPDU)
231 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
232 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
233 break;
234
235 case WLAN_CIPHER_SUITE_TKIP:
236 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
237 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
238 IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n");
239 break;
240
241 case WLAN_CIPHER_SUITE_WEP104:
242 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
243 /* fall through */
244 case WLAN_CIPHER_SUITE_WEP40:
245 tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
246 (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
247
248 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
249
250 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
251 "with key %d\n", keyconf->keyidx);
252 break;
253
254 default:
255 IWL_ERR(priv, "Unknown encode cipher %x\n", keyconf->cipher);
256 break;
257 }
258}
259
260/**
261 * iwl_sta_id_or_broadcast - return sta_id or broadcast sta
262 * @context: the current context
263 * @sta: mac80211 station
264 *
265 * In certain circumstances mac80211 passes a station pointer
266 * that may be %NULL, for example during TX or key setup. In
267 * that case, we need to use the broadcast station, so this
268 * inline wraps that pattern.
269 */
270static int iwl_sta_id_or_broadcast(struct iwl_rxon_context *context,
271 struct ieee80211_sta *sta)
272{
273 int sta_id;
274
275 if (!sta)
276 return context->bcast_sta_id;
277
278 sta_id = iwl_sta_id(sta);
279
280 /*
281 * mac80211 should not be passing a partially
282 * initialised station!
283 */
284 WARN_ON(sta_id == IWL_INVALID_STATION);
285
286 return sta_id;
287}
288
289/*
290 * start REPLY_TX command process
291 */
292int iwlagn_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
293{
294 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
295 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
296 struct iwl_station_priv *sta_priv = NULL;
297 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
298 struct iwl_device_cmd *dev_cmd = NULL;
299 struct iwl_tx_cmd *tx_cmd;
300 __le16 fc;
301 u8 hdr_len;
302 u16 len, seq_number = 0;
303 u8 sta_id, tid = IWL_MAX_TID_COUNT;
304 bool is_agg = false;
305 int txq_id;
306
307 if (info->control.vif)
308 ctx = iwl_rxon_ctx_from_vif(info->control.vif);
309
310 if (iwl_is_rfkill(priv)) {
311 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
312 goto drop_unlock_priv;
313 }
314
315 fc = hdr->frame_control;
316
317#ifdef CONFIG_IWLWIFI_DEBUG
318 if (ieee80211_is_auth(fc))
319 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
320 else if (ieee80211_is_assoc_req(fc))
321 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
322 else if (ieee80211_is_reassoc_req(fc))
323 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
324#endif
325
326 if (unlikely(ieee80211_is_probe_resp(fc))) {
327 struct iwl_wipan_noa_data *noa_data =
328 rcu_dereference(priv->noa_data);
329
330 if (noa_data &&
331 pskb_expand_head(skb, 0, noa_data->length,
332 GFP_ATOMIC) == 0) {
333 memcpy(skb_put(skb, noa_data->length),
334 noa_data->data, noa_data->length);
335 hdr = (struct ieee80211_hdr *)skb->data;
336 }
337 }
338
339 hdr_len = ieee80211_hdrlen(fc);
340
341 /* For management frames use broadcast id to do not break aggregation */
342 if (!ieee80211_is_data(fc))
343 sta_id = ctx->bcast_sta_id;
344 else {
345 /* Find index into station table for destination station */
346 sta_id = iwl_sta_id_or_broadcast(ctx, info->control.sta);
347 if (sta_id == IWL_INVALID_STATION) {
348 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
349 hdr->addr1);
350 goto drop_unlock_priv;
351 }
352 }
353
354 IWL_DEBUG_TX(priv, "station Id %d\n", sta_id);
355
356 if (info->control.sta)
357 sta_priv = (void *)info->control.sta->drv_priv;
358
359 if (sta_priv && sta_priv->asleep &&
360 (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)) {
361 /*
362 * This sends an asynchronous command to the device,
363 * but we can rely on it being processed before the
364 * next frame is processed -- and the next frame to
365 * this station is the one that will consume this
366 * counter.
367 * For now set the counter to just 1 since we do not
368 * support uAPSD yet.
369 *
370 * FIXME: If we get two non-bufferable frames one
371 * after the other, we might only send out one of
372 * them because this is racy.
373 */
374 iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
375 }
376
377 if (info->flags & IEEE80211_TX_CTL_AMPDU)
378 is_agg = true;
379
380 dev_cmd = kmem_cache_alloc(iwl_tx_cmd_pool, GFP_ATOMIC);
381
382 if (unlikely(!dev_cmd))
383 goto drop_unlock_priv;
384
385 memset(dev_cmd, 0, sizeof(*dev_cmd));
386 tx_cmd = (struct iwl_tx_cmd *) dev_cmd->payload;
387
388 /* Total # bytes to be transmitted */
389 len = (u16)skb->len;
390 tx_cmd->len = cpu_to_le16(len);
391
392 if (info->control.hw_key)
393 iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb);
394
395 /* TODO need this for burst mode later on */
396 iwlagn_tx_cmd_build_basic(priv, skb, tx_cmd, info, hdr, sta_id);
397
398 iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, fc);
399
400 memset(&info->status, 0, sizeof(info->status));
401
402 info->driver_data[0] = ctx;
403 info->driver_data[1] = dev_cmd;
404
405 spin_lock(&priv->sta_lock);
406
407 if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
408 u8 *qc = NULL;
409 struct iwl_tid_data *tid_data;
410 qc = ieee80211_get_qos_ctl(hdr);
411 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
412 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
413 goto drop_unlock_sta;
414 tid_data = &priv->tid_data[sta_id][tid];
415
416 /* aggregation is on for this <sta,tid> */
417 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
418 tid_data->agg.state != IWL_AGG_ON) {
419 IWL_ERR(priv, "TX_CTL_AMPDU while not in AGG:"
420 " Tx flags = 0x%08x, agg.state = %d",
421 info->flags, tid_data->agg.state);
422 IWL_ERR(priv, "sta_id = %d, tid = %d seq_num = %d",
423 sta_id, tid, SEQ_TO_SN(tid_data->seq_number));
424 goto drop_unlock_sta;
425 }
426
427 /* We can receive packets from the stack in IWL_AGG_{ON,OFF}
428 * only. Check this here.
429 */
430 if (WARN_ONCE(tid_data->agg.state != IWL_AGG_ON &&
431 tid_data->agg.state != IWL_AGG_OFF,
432 "Tx while agg.state = %d", tid_data->agg.state))
433 goto drop_unlock_sta;
434
435 seq_number = tid_data->seq_number;
436 seq_number &= IEEE80211_SCTL_SEQ;
437 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
438 hdr->seq_ctrl |= cpu_to_le16(seq_number);
439 seq_number += 0x10;
440 }
441
442 /* Copy MAC header from skb into command buffer */
443 memcpy(tx_cmd->hdr, hdr, hdr_len);
444
445 if (is_agg)
446 txq_id = priv->tid_data[sta_id][tid].agg.txq_id;
447 else if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
448 /*
449 * Send this frame after DTIM -- there's a special queue
450 * reserved for this for contexts that support AP mode.
451 */
452 txq_id = ctx->mcast_queue;
453
454 /*
455 * The microcode will clear the more data
456 * bit in the last frame it transmits.
457 */
458 hdr->frame_control |=
459 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
460 } else if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
461 txq_id = IWL_AUX_QUEUE;
462 else
463 txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
464
465 WARN_ON_ONCE(!is_agg && txq_id != info->hw_queue);
466 WARN_ON_ONCE(is_agg &&
467 priv->queue_to_mac80211[txq_id] != info->hw_queue);
468
469 if (iwl_trans_tx(priv->trans, skb, dev_cmd, txq_id))
470 goto drop_unlock_sta;
471
472 if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc) &&
473 !ieee80211_has_morefrags(fc))
474 priv->tid_data[sta_id][tid].seq_number = seq_number;
475
476 spin_unlock(&priv->sta_lock);
477
478 /*
479 * Avoid atomic ops if it isn't an associated client.
480 * Also, if this is a packet for aggregation, don't
481 * increase the counter because the ucode will stop
482 * aggregation queues when their respective station
483 * goes to sleep.
484 */
485 if (sta_priv && sta_priv->client && !is_agg)
486 atomic_inc(&sta_priv->pending_frames);
487
488 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
489 iwl_scan_offchannel_skb(priv);
490
491 return 0;
492
493drop_unlock_sta:
494 if (dev_cmd)
495 kmem_cache_free(iwl_tx_cmd_pool, dev_cmd);
496 spin_unlock(&priv->sta_lock);
497drop_unlock_priv:
498 return -1;
499}
500
501static int iwlagn_alloc_agg_txq(struct iwl_priv *priv, int mq)
502{
503 int q;
504
505 for (q = IWLAGN_FIRST_AMPDU_QUEUE;
506 q < priv->cfg->base_params->num_of_queues; q++) {
507 if (!test_and_set_bit(q, priv->agg_q_alloc)) {
508 priv->queue_to_mac80211[q] = mq;
509 return q;
510 }
511 }
512
513 return -ENOSPC;
514}
515
516static void iwlagn_dealloc_agg_txq(struct iwl_priv *priv, int q)
517{
518 clear_bit(q, priv->agg_q_alloc);
519 priv->queue_to_mac80211[q] = IWL_INVALID_MAC80211_QUEUE;
520}
521
522int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
523 struct ieee80211_sta *sta, u16 tid)
524{
525 struct iwl_tid_data *tid_data;
526 int sta_id, txq_id;
527 enum iwl_agg_state agg_state;
528
529 sta_id = iwl_sta_id(sta);
530
531 if (sta_id == IWL_INVALID_STATION) {
532 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
533 return -ENXIO;
534 }
535
536 spin_lock_bh(&priv->sta_lock);
537
538 tid_data = &priv->tid_data[sta_id][tid];
539 txq_id = priv->tid_data[sta_id][tid].agg.txq_id;
540
541 switch (priv->tid_data[sta_id][tid].agg.state) {
542 case IWL_EMPTYING_HW_QUEUE_ADDBA:
543 /*
544 * This can happen if the peer stops aggregation
545 * again before we've had a chance to drain the
546 * queue we selected previously, i.e. before the
547 * session was really started completely.
548 */
549 IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
550 goto turn_off;
551 case IWL_AGG_STARTING:
552 /*
553 * This can happen when the session is stopped before
554 * we receive ADDBA response
555 */
556 IWL_DEBUG_HT(priv, "AGG stop before AGG became operational\n");
557 goto turn_off;
558 case IWL_AGG_ON:
559 break;
560 default:
561 IWL_WARN(priv, "Stopping AGG while state not ON "
562 "or starting for %d on %d (%d)\n", sta_id, tid,
563 priv->tid_data[sta_id][tid].agg.state);
564 spin_unlock_bh(&priv->sta_lock);
565 return 0;
566 }
567
568 tid_data->agg.ssn = SEQ_TO_SN(tid_data->seq_number);
569
570 /* There are still packets for this RA / TID in the HW */
571 if (!test_bit(txq_id, priv->agg_q_alloc)) {
572 IWL_DEBUG_TX_QUEUES(priv,
573 "stopping AGG on STA/TID %d/%d but hwq %d not used\n",
574 sta_id, tid, txq_id);
575 } else if (tid_data->agg.ssn != tid_data->next_reclaimed) {
576 IWL_DEBUG_TX_QUEUES(priv, "Can't proceed: ssn %d, "
577 "next_recl = %d\n",
578 tid_data->agg.ssn,
579 tid_data->next_reclaimed);
580 priv->tid_data[sta_id][tid].agg.state =
581 IWL_EMPTYING_HW_QUEUE_DELBA;
582 spin_unlock_bh(&priv->sta_lock);
583 return 0;
584 }
585
586 IWL_DEBUG_TX_QUEUES(priv, "Can proceed: ssn = next_recl = %d\n",
587 tid_data->agg.ssn);
588turn_off:
589 agg_state = priv->tid_data[sta_id][tid].agg.state;
590 priv->tid_data[sta_id][tid].agg.state = IWL_AGG_OFF;
591
592 spin_unlock_bh(&priv->sta_lock);
593
594 if (test_bit(txq_id, priv->agg_q_alloc)) {
595 /*
596 * If the transport didn't know that we wanted to start
597 * agreggation, don't tell it that we want to stop them.
598 * This can happen when we don't get the addBA response on
599 * time, or we hadn't time to drain the AC queues.
600 */
601 if (agg_state == IWL_AGG_ON)
602 iwl_trans_tx_agg_disable(priv->trans, txq_id);
603 else
604 IWL_DEBUG_TX_QUEUES(priv, "Don't disable tx agg: %d\n",
605 agg_state);
606 iwlagn_dealloc_agg_txq(priv, txq_id);
607 }
608
609 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
610
611 return 0;
612}
613
614int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
615 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
616{
617 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
618 struct iwl_tid_data *tid_data;
619 int sta_id, txq_id, ret;
620
621 IWL_DEBUG_HT(priv, "TX AGG request on ra = %pM tid = %d\n",
622 sta->addr, tid);
623
624 sta_id = iwl_sta_id(sta);
625 if (sta_id == IWL_INVALID_STATION) {
626 IWL_ERR(priv, "Start AGG on invalid station\n");
627 return -ENXIO;
628 }
629 if (unlikely(tid >= IWL_MAX_TID_COUNT))
630 return -EINVAL;
631
632 if (priv->tid_data[sta_id][tid].agg.state != IWL_AGG_OFF) {
633 IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
634 return -ENXIO;
635 }
636
637 txq_id = iwlagn_alloc_agg_txq(priv, ctx->ac_to_queue[tid_to_ac[tid]]);
638 if (txq_id < 0) {
639 IWL_DEBUG_TX_QUEUES(priv,
640 "No free aggregation queue for %pM/%d\n",
641 sta->addr, tid);
642 return txq_id;
643 }
644
645 ret = iwl_sta_tx_modify_enable_tid(priv, sta_id, tid);
646 if (ret)
647 return ret;
648
649 spin_lock_bh(&priv->sta_lock);
650 tid_data = &priv->tid_data[sta_id][tid];
651 tid_data->agg.ssn = SEQ_TO_SN(tid_data->seq_number);
652 tid_data->agg.txq_id = txq_id;
653
654 *ssn = tid_data->agg.ssn;
655
656 if (*ssn == tid_data->next_reclaimed) {
657 IWL_DEBUG_TX_QUEUES(priv, "Can proceed: ssn = next_recl = %d\n",
658 tid_data->agg.ssn);
659 tid_data->agg.state = IWL_AGG_STARTING;
660 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
661 } else {
662 IWL_DEBUG_TX_QUEUES(priv, "Can't proceed: ssn %d, "
663 "next_reclaimed = %d\n",
664 tid_data->agg.ssn,
665 tid_data->next_reclaimed);
666 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
667 }
668 spin_unlock_bh(&priv->sta_lock);
669
670 return ret;
671}
672
673int iwlagn_tx_agg_oper(struct iwl_priv *priv, struct ieee80211_vif *vif,
674 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
675{
676 struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
677 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
678 int q, fifo;
679 u16 ssn;
680
681 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
682
683 spin_lock_bh(&priv->sta_lock);
684 ssn = priv->tid_data[sta_priv->sta_id][tid].agg.ssn;
685 q = priv->tid_data[sta_priv->sta_id][tid].agg.txq_id;
686 priv->tid_data[sta_priv->sta_id][tid].agg.state = IWL_AGG_ON;
687 spin_unlock_bh(&priv->sta_lock);
688
689 fifo = ctx->ac_to_fifo[tid_to_ac[tid]];
690
691 iwl_trans_tx_agg_setup(priv->trans, q, fifo,
692 sta_priv->sta_id, tid,
693 buf_size, ssn);
694
695 /*
696 * If the limit is 0, then it wasn't initialised yet,
697 * use the default. We can do that since we take the
698 * minimum below, and we don't want to go above our
699 * default due to hardware restrictions.
700 */
701 if (sta_priv->max_agg_bufsize == 0)
702 sta_priv->max_agg_bufsize =
703 LINK_QUAL_AGG_FRAME_LIMIT_DEF;
704
705 /*
706 * Even though in theory the peer could have different
707 * aggregation reorder buffer sizes for different sessions,
708 * our ucode doesn't allow for that and has a global limit
709 * for each station. Therefore, use the minimum of all the
710 * aggregation sessions and our default value.
711 */
712 sta_priv->max_agg_bufsize =
713 min(sta_priv->max_agg_bufsize, buf_size);
714
715 if (priv->hw_params.use_rts_for_aggregation) {
716 /*
717 * switch to RTS/CTS if it is the prefer protection
718 * method for HT traffic
719 */
720
721 sta_priv->lq_sta.lq.general_params.flags |=
722 LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
723 }
724 priv->agg_tids_count++;
725 IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
726 priv->agg_tids_count);
727
728 sta_priv->lq_sta.lq.agg_params.agg_frame_cnt_limit =
729 sta_priv->max_agg_bufsize;
730
731 IWL_DEBUG_HT(priv, "Tx aggregation enabled on ra = %pM tid = %d\n",
732 sta->addr, tid);
733
734 return iwl_send_lq_cmd(priv, ctx,
735 &sta_priv->lq_sta.lq, CMD_ASYNC, false);
736}
737
738static void iwlagn_check_ratid_empty(struct iwl_priv *priv, int sta_id, u8 tid)
739{
740 struct iwl_tid_data *tid_data = &priv->tid_data[sta_id][tid];
741 enum iwl_rxon_context_id ctx;
742 struct ieee80211_vif *vif;
743 u8 *addr;
744
745 lockdep_assert_held(&priv->sta_lock);
746
747 addr = priv->stations[sta_id].sta.sta.addr;
748 ctx = priv->stations[sta_id].ctxid;
749 vif = priv->contexts[ctx].vif;
750
751 switch (priv->tid_data[sta_id][tid].agg.state) {
752 case IWL_EMPTYING_HW_QUEUE_DELBA:
753 /* There are no packets for this RA / TID in the HW any more */
754 if (tid_data->agg.ssn == tid_data->next_reclaimed) {
755 IWL_DEBUG_TX_QUEUES(priv,
756 "Can continue DELBA flow ssn = next_recl ="
757 " %d", tid_data->next_reclaimed);
758 iwl_trans_tx_agg_disable(priv->trans,
759 tid_data->agg.txq_id);
760 iwlagn_dealloc_agg_txq(priv, tid_data->agg.txq_id);
761 tid_data->agg.state = IWL_AGG_OFF;
762 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
763 }
764 break;
765 case IWL_EMPTYING_HW_QUEUE_ADDBA:
766 /* There are no packets for this RA / TID in the HW any more */
767 if (tid_data->agg.ssn == tid_data->next_reclaimed) {
768 IWL_DEBUG_TX_QUEUES(priv,
769 "Can continue ADDBA flow ssn = next_recl ="
770 " %d", tid_data->next_reclaimed);
771 tid_data->agg.state = IWL_AGG_STARTING;
772 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
773 }
774 break;
775 default:
776 break;
777 }
778}
779
780static void iwlagn_non_agg_tx_status(struct iwl_priv *priv,
781 struct iwl_rxon_context *ctx,
782 const u8 *addr1)
783{
784 struct ieee80211_sta *sta;
785 struct iwl_station_priv *sta_priv;
786
787 rcu_read_lock();
788 sta = ieee80211_find_sta(ctx->vif, addr1);
789 if (sta) {
790 sta_priv = (void *)sta->drv_priv;
791 /* avoid atomic ops if this isn't a client */
792 if (sta_priv->client &&
793 atomic_dec_return(&sta_priv->pending_frames) == 0)
794 ieee80211_sta_block_awake(priv->hw, sta, false);
795 }
796 rcu_read_unlock();
797}
798
799/**
800 * translate ucode response to mac80211 tx status control values
801 */
802static void iwlagn_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
803 struct ieee80211_tx_info *info)
804{
805 struct ieee80211_tx_rate *r = &info->status.rates[0];
806
807 info->status.antenna =
808 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
809 if (rate_n_flags & RATE_MCS_HT_MSK)
810 r->flags |= IEEE80211_TX_RC_MCS;
811 if (rate_n_flags & RATE_MCS_GF_MSK)
812 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
813 if (rate_n_flags & RATE_MCS_HT40_MSK)
814 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
815 if (rate_n_flags & RATE_MCS_DUP_MSK)
816 r->flags |= IEEE80211_TX_RC_DUP_DATA;
817 if (rate_n_flags & RATE_MCS_SGI_MSK)
818 r->flags |= IEEE80211_TX_RC_SHORT_GI;
819 r->idx = iwlagn_hwrate_to_mac80211_idx(rate_n_flags, info->band);
820}
821
822#ifdef CONFIG_IWLWIFI_DEBUG
823const char *iwl_get_tx_fail_reason(u32 status)
824{
825#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
826#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
827
828 switch (status & TX_STATUS_MSK) {
829 case TX_STATUS_SUCCESS:
830 return "SUCCESS";
831 TX_STATUS_POSTPONE(DELAY);
832 TX_STATUS_POSTPONE(FEW_BYTES);
833 TX_STATUS_POSTPONE(BT_PRIO);
834 TX_STATUS_POSTPONE(QUIET_PERIOD);
835 TX_STATUS_POSTPONE(CALC_TTAK);
836 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
837 TX_STATUS_FAIL(SHORT_LIMIT);
838 TX_STATUS_FAIL(LONG_LIMIT);
839 TX_STATUS_FAIL(FIFO_UNDERRUN);
840 TX_STATUS_FAIL(DRAIN_FLOW);
841 TX_STATUS_FAIL(RFKILL_FLUSH);
842 TX_STATUS_FAIL(LIFE_EXPIRE);
843 TX_STATUS_FAIL(DEST_PS);
844 TX_STATUS_FAIL(HOST_ABORTED);
845 TX_STATUS_FAIL(BT_RETRY);
846 TX_STATUS_FAIL(STA_INVALID);
847 TX_STATUS_FAIL(FRAG_DROPPED);
848 TX_STATUS_FAIL(TID_DISABLE);
849 TX_STATUS_FAIL(FIFO_FLUSHED);
850 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
851 TX_STATUS_FAIL(PASSIVE_NO_RX);
852 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
853 }
854
855 return "UNKNOWN";
856
857#undef TX_STATUS_FAIL
858#undef TX_STATUS_POSTPONE
859}
860#endif /* CONFIG_IWLWIFI_DEBUG */
861
862static void iwlagn_count_agg_tx_err_status(struct iwl_priv *priv, u16 status)
863{
864 status &= AGG_TX_STATUS_MSK;
865
866 switch (status) {
867 case AGG_TX_STATE_UNDERRUN_MSK:
868 priv->reply_agg_tx_stats.underrun++;
869 break;
870 case AGG_TX_STATE_BT_PRIO_MSK:
871 priv->reply_agg_tx_stats.bt_prio++;
872 break;
873 case AGG_TX_STATE_FEW_BYTES_MSK:
874 priv->reply_agg_tx_stats.few_bytes++;
875 break;
876 case AGG_TX_STATE_ABORT_MSK:
877 priv->reply_agg_tx_stats.abort++;
878 break;
879 case AGG_TX_STATE_LAST_SENT_TTL_MSK:
880 priv->reply_agg_tx_stats.last_sent_ttl++;
881 break;
882 case AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK:
883 priv->reply_agg_tx_stats.last_sent_try++;
884 break;
885 case AGG_TX_STATE_LAST_SENT_BT_KILL_MSK:
886 priv->reply_agg_tx_stats.last_sent_bt_kill++;
887 break;
888 case AGG_TX_STATE_SCD_QUERY_MSK:
889 priv->reply_agg_tx_stats.scd_query++;
890 break;
891 case AGG_TX_STATE_TEST_BAD_CRC32_MSK:
892 priv->reply_agg_tx_stats.bad_crc32++;
893 break;
894 case AGG_TX_STATE_RESPONSE_MSK:
895 priv->reply_agg_tx_stats.response++;
896 break;
897 case AGG_TX_STATE_DUMP_TX_MSK:
898 priv->reply_agg_tx_stats.dump_tx++;
899 break;
900 case AGG_TX_STATE_DELAY_TX_MSK:
901 priv->reply_agg_tx_stats.delay_tx++;
902 break;
903 default:
904 priv->reply_agg_tx_stats.unknown++;
905 break;
906 }
907}
908
909static void iwl_rx_reply_tx_agg(struct iwl_priv *priv,
910 struct iwlagn_tx_resp *tx_resp)
911{
912 struct agg_tx_status *frame_status = &tx_resp->status;
913 int tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
914 IWLAGN_TX_RES_TID_POS;
915 int sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
916 IWLAGN_TX_RES_RA_POS;
917 struct iwl_ht_agg *agg = &priv->tid_data[sta_id][tid].agg;
918 u32 status = le16_to_cpu(tx_resp->status.status);
919 int i;
920
921 WARN_ON(tid == IWL_TID_NON_QOS);
922
923 if (agg->wait_for_ba)
924 IWL_DEBUG_TX_REPLY(priv,
925 "got tx response w/o block-ack\n");
926
927 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
928 agg->wait_for_ba = (tx_resp->frame_count > 1);
929
930 /*
931 * If the BT kill count is non-zero, we'll get this
932 * notification again.
933 */
934 if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
935 priv->cfg->bt_params &&
936 priv->cfg->bt_params->advanced_bt_coexist) {
937 IWL_DEBUG_COEX(priv, "receive reply tx w/ bt_kill\n");
938 }
939
940 if (tx_resp->frame_count == 1)
941 return;
942
943 /* Construct bit-map of pending frames within Tx window */
944 for (i = 0; i < tx_resp->frame_count; i++) {
945 u16 fstatus = le16_to_cpu(frame_status[i].status);
946
947 if (status & AGG_TX_STATUS_MSK)
948 iwlagn_count_agg_tx_err_status(priv, fstatus);
949
950 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
951 AGG_TX_STATE_ABORT_MSK))
952 continue;
953
954 IWL_DEBUG_TX_REPLY(priv, "status %s (0x%08x), "
955 "try-count (0x%08x)\n",
956 iwl_get_agg_tx_fail_reason(fstatus),
957 fstatus & AGG_TX_STATUS_MSK,
958 fstatus & AGG_TX_TRY_MSK);
959 }
960}
961
962#ifdef CONFIG_IWLWIFI_DEBUG
963#define AGG_TX_STATE_FAIL(x) case AGG_TX_STATE_ ## x: return #x
964
965const char *iwl_get_agg_tx_fail_reason(u16 status)
966{
967 status &= AGG_TX_STATUS_MSK;
968 switch (status) {
969 case AGG_TX_STATE_TRANSMITTED:
970 return "SUCCESS";
971 AGG_TX_STATE_FAIL(UNDERRUN_MSK);
972 AGG_TX_STATE_FAIL(BT_PRIO_MSK);
973 AGG_TX_STATE_FAIL(FEW_BYTES_MSK);
974 AGG_TX_STATE_FAIL(ABORT_MSK);
975 AGG_TX_STATE_FAIL(LAST_SENT_TTL_MSK);
976 AGG_TX_STATE_FAIL(LAST_SENT_TRY_CNT_MSK);
977 AGG_TX_STATE_FAIL(LAST_SENT_BT_KILL_MSK);
978 AGG_TX_STATE_FAIL(SCD_QUERY_MSK);
979 AGG_TX_STATE_FAIL(TEST_BAD_CRC32_MSK);
980 AGG_TX_STATE_FAIL(RESPONSE_MSK);
981 AGG_TX_STATE_FAIL(DUMP_TX_MSK);
982 AGG_TX_STATE_FAIL(DELAY_TX_MSK);
983 }
984
985 return "UNKNOWN";
986}
987#endif /* CONFIG_IWLWIFI_DEBUG */
988
989static inline u32 iwlagn_get_scd_ssn(struct iwlagn_tx_resp *tx_resp)
990{
991 return le32_to_cpup((__le32 *)&tx_resp->status +
992 tx_resp->frame_count) & MAX_SN;
993}
994
995static void iwlagn_count_tx_err_status(struct iwl_priv *priv, u16 status)
996{
997 status &= TX_STATUS_MSK;
998
999 switch (status) {
1000 case TX_STATUS_POSTPONE_DELAY:
1001 priv->reply_tx_stats.pp_delay++;
1002 break;
1003 case TX_STATUS_POSTPONE_FEW_BYTES:
1004 priv->reply_tx_stats.pp_few_bytes++;
1005 break;
1006 case TX_STATUS_POSTPONE_BT_PRIO:
1007 priv->reply_tx_stats.pp_bt_prio++;
1008 break;
1009 case TX_STATUS_POSTPONE_QUIET_PERIOD:
1010 priv->reply_tx_stats.pp_quiet_period++;
1011 break;
1012 case TX_STATUS_POSTPONE_CALC_TTAK:
1013 priv->reply_tx_stats.pp_calc_ttak++;
1014 break;
1015 case TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY:
1016 priv->reply_tx_stats.int_crossed_retry++;
1017 break;
1018 case TX_STATUS_FAIL_SHORT_LIMIT:
1019 priv->reply_tx_stats.short_limit++;
1020 break;
1021 case TX_STATUS_FAIL_LONG_LIMIT:
1022 priv->reply_tx_stats.long_limit++;
1023 break;
1024 case TX_STATUS_FAIL_FIFO_UNDERRUN:
1025 priv->reply_tx_stats.fifo_underrun++;
1026 break;
1027 case TX_STATUS_FAIL_DRAIN_FLOW:
1028 priv->reply_tx_stats.drain_flow++;
1029 break;
1030 case TX_STATUS_FAIL_RFKILL_FLUSH:
1031 priv->reply_tx_stats.rfkill_flush++;
1032 break;
1033 case TX_STATUS_FAIL_LIFE_EXPIRE:
1034 priv->reply_tx_stats.life_expire++;
1035 break;
1036 case TX_STATUS_FAIL_DEST_PS:
1037 priv->reply_tx_stats.dest_ps++;
1038 break;
1039 case TX_STATUS_FAIL_HOST_ABORTED:
1040 priv->reply_tx_stats.host_abort++;
1041 break;
1042 case TX_STATUS_FAIL_BT_RETRY:
1043 priv->reply_tx_stats.bt_retry++;
1044 break;
1045 case TX_STATUS_FAIL_STA_INVALID:
1046 priv->reply_tx_stats.sta_invalid++;
1047 break;
1048 case TX_STATUS_FAIL_FRAG_DROPPED:
1049 priv->reply_tx_stats.frag_drop++;
1050 break;
1051 case TX_STATUS_FAIL_TID_DISABLE:
1052 priv->reply_tx_stats.tid_disable++;
1053 break;
1054 case TX_STATUS_FAIL_FIFO_FLUSHED:
1055 priv->reply_tx_stats.fifo_flush++;
1056 break;
1057 case TX_STATUS_FAIL_INSUFFICIENT_CF_POLL:
1058 priv->reply_tx_stats.insuff_cf_poll++;
1059 break;
1060 case TX_STATUS_FAIL_PASSIVE_NO_RX:
1061 priv->reply_tx_stats.fail_hw_drop++;
1062 break;
1063 case TX_STATUS_FAIL_NO_BEACON_ON_RADAR:
1064 priv->reply_tx_stats.sta_color_mismatch++;
1065 break;
1066 default:
1067 priv->reply_tx_stats.unknown++;
1068 break;
1069 }
1070}
1071
1072static void iwlagn_set_tx_status(struct iwl_priv *priv,
1073 struct ieee80211_tx_info *info,
1074 struct iwlagn_tx_resp *tx_resp,
1075 bool is_agg)
1076{
1077 u16 status = le16_to_cpu(tx_resp->status.status);
1078
1079 info->status.rates[0].count = tx_resp->failure_frame + 1;
1080 if (is_agg)
1081 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
1082 info->flags |= iwl_tx_status_to_mac80211(status);
1083 iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
1084 info);
1085 if (!iwl_is_tx_success(status))
1086 iwlagn_count_tx_err_status(priv, status);
1087}
1088
1089static void iwl_check_abort_status(struct iwl_priv *priv,
1090 u8 frame_count, u32 status)
1091{
1092 if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
1093 IWL_ERR(priv, "Tx flush command to flush out all frames\n");
1094 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
1095 queue_work(priv->workqueue, &priv->tx_flush);
1096 }
1097}
1098
1099static int iwl_reclaim(struct iwl_priv *priv, int sta_id, int tid,
1100 int txq_id, int ssn, struct sk_buff_head *skbs)
1101{
1102 if (unlikely(txq_id >= IWLAGN_FIRST_AMPDU_QUEUE &&
1103 tid != IWL_TID_NON_QOS &&
1104 txq_id != priv->tid_data[sta_id][tid].agg.txq_id)) {
1105 /*
1106 * FIXME: this is a uCode bug which need to be addressed,
1107 * log the information and return for now.
1108 * Since it is can possibly happen very often and in order
1109 * not to fill the syslog, don't use IWL_ERR or IWL_WARN
1110 */
1111 IWL_DEBUG_TX_QUEUES(priv,
1112 "Bad queue mapping txq_id=%d, agg_txq[sta:%d,tid:%d]=%d\n",
1113 txq_id, sta_id, tid,
1114 priv->tid_data[sta_id][tid].agg.txq_id);
1115 return 1;
1116 }
1117
1118 iwl_trans_reclaim(priv->trans, txq_id, ssn, skbs);
1119 return 0;
1120}
1121
1122int iwlagn_rx_reply_tx(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
1123 struct iwl_device_cmd *cmd)
1124{
1125 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1126 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1127 int txq_id = SEQ_TO_QUEUE(sequence);
1128 int cmd_index __maybe_unused = SEQ_TO_INDEX(sequence);
1129 struct iwlagn_tx_resp *tx_resp = (void *)pkt->data;
1130 struct ieee80211_hdr *hdr;
1131 u32 status = le16_to_cpu(tx_resp->status.status);
1132 u16 ssn = iwlagn_get_scd_ssn(tx_resp);
1133 int tid;
1134 int sta_id;
1135 int freed;
1136 struct ieee80211_tx_info *info;
1137 struct sk_buff_head skbs;
1138 struct sk_buff *skb;
1139 struct iwl_rxon_context *ctx;
1140 bool is_agg = (txq_id >= IWLAGN_FIRST_AMPDU_QUEUE);
1141 bool is_offchannel_skb;
1142
1143 tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
1144 IWLAGN_TX_RES_TID_POS;
1145 sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
1146 IWLAGN_TX_RES_RA_POS;
1147
1148 spin_lock(&priv->sta_lock);
1149
1150 if (is_agg)
1151 iwl_rx_reply_tx_agg(priv, tx_resp);
1152
1153 __skb_queue_head_init(&skbs);
1154
1155 is_offchannel_skb = false;
1156
1157 if (tx_resp->frame_count == 1) {
1158 u16 next_reclaimed = le16_to_cpu(tx_resp->seq_ctl);
1159 next_reclaimed = SEQ_TO_SN(next_reclaimed + 0x10);
1160
1161 if (is_agg) {
1162 /* If this is an aggregation queue, we can rely on the
1163 * ssn since the wifi sequence number corresponds to
1164 * the index in the TFD ring (%256).
1165 * The seq_ctl is the sequence control of the packet
1166 * to which this Tx response relates. But if there is a
1167 * hole in the bitmap of the BA we received, this Tx
1168 * response may allow to reclaim the hole and all the
1169 * subsequent packets that were already acked.
1170 * In that case, seq_ctl != ssn, and the next packet
1171 * to be reclaimed will be ssn and not seq_ctl.
1172 */
1173 next_reclaimed = ssn;
1174 }
1175
1176 if (tid != IWL_TID_NON_QOS) {
1177 priv->tid_data[sta_id][tid].next_reclaimed =
1178 next_reclaimed;
1179 IWL_DEBUG_TX_REPLY(priv, "Next reclaimed packet:%d\n",
1180 next_reclaimed);
1181 }
1182
1183 /*we can free until ssn % q.n_bd not inclusive */
1184 WARN_ON(iwl_reclaim(priv, sta_id, tid, txq_id, ssn, &skbs));
1185 iwlagn_check_ratid_empty(priv, sta_id, tid);
1186 freed = 0;
1187
1188 /* process frames */
1189 skb_queue_walk(&skbs, skb) {
1190 hdr = (struct ieee80211_hdr *)skb->data;
1191
1192 if (!ieee80211_is_data_qos(hdr->frame_control))
1193 priv->last_seq_ctl = tx_resp->seq_ctl;
1194
1195 info = IEEE80211_SKB_CB(skb);
1196 ctx = info->driver_data[0];
1197 kmem_cache_free(iwl_tx_cmd_pool,
1198 (info->driver_data[1]));
1199
1200 memset(&info->status, 0, sizeof(info->status));
1201
1202 if (status == TX_STATUS_FAIL_PASSIVE_NO_RX &&
1203 iwl_is_associated_ctx(ctx) && ctx->vif &&
1204 ctx->vif->type == NL80211_IFTYPE_STATION) {
1205 /* block and stop all queues */
1206 priv->passive_no_rx = true;
1207 IWL_DEBUG_TX_QUEUES(priv, "stop all queues: "
1208 "passive channel");
1209 ieee80211_stop_queues(priv->hw);
1210
1211 IWL_DEBUG_TX_REPLY(priv,
1212 "TXQ %d status %s (0x%08x) "
1213 "rate_n_flags 0x%x retries %d\n",
1214 txq_id,
1215 iwl_get_tx_fail_reason(status),
1216 status,
1217 le32_to_cpu(tx_resp->rate_n_flags),
1218 tx_resp->failure_frame);
1219
1220 IWL_DEBUG_TX_REPLY(priv,
1221 "FrameCnt = %d, idx=%d\n",
1222 tx_resp->frame_count, cmd_index);
1223 }
1224
1225 /* check if BAR is needed */
1226 if (is_agg && !iwl_is_tx_success(status))
1227 info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
1228 iwlagn_set_tx_status(priv, IEEE80211_SKB_CB(skb),
1229 tx_resp, is_agg);
1230 if (!is_agg)
1231 iwlagn_non_agg_tx_status(priv, ctx, hdr->addr1);
1232
1233 is_offchannel_skb =
1234 (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN);
1235 freed++;
1236 }
1237
1238 WARN_ON(!is_agg && freed != 1);
1239
1240 /*
1241 * An offchannel frame can be send only on the AUX queue, where
1242 * there is no aggregation (and reordering) so it only is single
1243 * skb is expected to be processed.
1244 */
1245 WARN_ON(is_offchannel_skb && freed != 1);
1246 }
1247
1248 iwl_check_abort_status(priv, tx_resp->frame_count, status);
1249 spin_unlock(&priv->sta_lock);
1250
1251 while (!skb_queue_empty(&skbs)) {
1252 skb = __skb_dequeue(&skbs);
1253 ieee80211_tx_status(priv->hw, skb);
1254 }
1255
1256 if (is_offchannel_skb)
1257 iwl_scan_offchannel_skb_status(priv);
1258
1259 return 0;
1260}
1261
1262/**
1263 * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
1264 *
1265 * Handles block-acknowledge notification from device, which reports success
1266 * of frames sent via aggregation.
1267 */
1268int iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
1269 struct iwl_rx_cmd_buffer *rxb,
1270 struct iwl_device_cmd *cmd)
1271{
1272 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1273 struct iwl_compressed_ba_resp *ba_resp = (void *)pkt->data;
1274 struct iwl_ht_agg *agg;
1275 struct sk_buff_head reclaimed_skbs;
1276 struct ieee80211_tx_info *info;
1277 struct ieee80211_hdr *hdr;
1278 struct sk_buff *skb;
1279 int sta_id;
1280 int tid;
1281 int freed;
1282
1283 /* "flow" corresponds to Tx queue */
1284 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
1285
1286 /* "ssn" is start of block-ack Tx window, corresponds to index
1287 * (in Tx queue's circular buffer) of first TFD/frame in window */
1288 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
1289
1290 if (scd_flow >= priv->cfg->base_params->num_of_queues) {
1291 IWL_ERR(priv,
1292 "BUG_ON scd_flow is bigger than number of queues\n");
1293 return 0;
1294 }
1295
1296 sta_id = ba_resp->sta_id;
1297 tid = ba_resp->tid;
1298 agg = &priv->tid_data[sta_id][tid].agg;
1299
1300 spin_lock(&priv->sta_lock);
1301
1302 if (unlikely(!agg->wait_for_ba)) {
1303 if (unlikely(ba_resp->bitmap))
1304 IWL_ERR(priv, "Received BA when not expected\n");
1305 spin_unlock(&priv->sta_lock);
1306 return 0;
1307 }
1308
1309 __skb_queue_head_init(&reclaimed_skbs);
1310
1311 /* Release all TFDs before the SSN, i.e. all TFDs in front of
1312 * block-ack window (we assume that they've been successfully
1313 * transmitted ... if not, it's too late anyway). */
1314 if (iwl_reclaim(priv, sta_id, tid, scd_flow,
1315 ba_resp_scd_ssn, &reclaimed_skbs)) {
1316 spin_unlock(&priv->sta_lock);
1317 return 0;
1318 }
1319
1320 IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
1321 "sta_id = %d\n",
1322 agg->wait_for_ba,
1323 (u8 *) &ba_resp->sta_addr_lo32,
1324 ba_resp->sta_id);
1325 IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, "
1326 "scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
1327 ba_resp->tid, le16_to_cpu(ba_resp->seq_ctl),
1328 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
1329 scd_flow, ba_resp_scd_ssn, ba_resp->txed,
1330 ba_resp->txed_2_done);
1331
1332 /* Mark that the expected block-ack response arrived */
1333 agg->wait_for_ba = false;
1334
1335 /* Sanity check values reported by uCode */
1336 if (ba_resp->txed_2_done > ba_resp->txed) {
1337 IWL_DEBUG_TX_REPLY(priv,
1338 "bogus sent(%d) and ack(%d) count\n",
1339 ba_resp->txed, ba_resp->txed_2_done);
1340 /*
1341 * set txed_2_done = txed,
1342 * so it won't impact rate scale
1343 */
1344 ba_resp->txed = ba_resp->txed_2_done;
1345 }
1346
1347 priv->tid_data[sta_id][tid].next_reclaimed = ba_resp_scd_ssn;
1348
1349 iwlagn_check_ratid_empty(priv, sta_id, tid);
1350 freed = 0;
1351
1352 skb_queue_walk(&reclaimed_skbs, skb) {
1353 hdr = (struct ieee80211_hdr *)skb->data;
1354
1355 if (ieee80211_is_data_qos(hdr->frame_control))
1356 freed++;
1357 else
1358 WARN_ON_ONCE(1);
1359
1360 info = IEEE80211_SKB_CB(skb);
1361 kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
1362
1363 if (freed == 1) {
1364 /* this is the first skb we deliver in this batch */
1365 /* put the rate scaling data there */
1366 info = IEEE80211_SKB_CB(skb);
1367 memset(&info->status, 0, sizeof(info->status));
1368 info->flags |= IEEE80211_TX_STAT_ACK;
1369 info->flags |= IEEE80211_TX_STAT_AMPDU;
1370 info->status.ampdu_ack_len = ba_resp->txed_2_done;
1371 info->status.ampdu_len = ba_resp->txed;
1372 iwlagn_hwrate_to_tx_control(priv, agg->rate_n_flags,
1373 info);
1374 }
1375 }
1376
1377 spin_unlock(&priv->sta_lock);
1378
1379 while (!skb_queue_empty(&reclaimed_skbs)) {
1380 skb = __skb_dequeue(&reclaimed_skbs);
1381 ieee80211_tx_status(priv->hw, skb);
1382 }
1383
1384 return 0;
1385}
diff --git a/drivers/net/wireless/iwlwifi/dvm/ucode.c b/drivers/net/wireless/iwlwifi/dvm/ucode.c
new file mode 100644
index 000000000000..5a2e186c7ded
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/dvm/ucode.c
@@ -0,0 +1,533 @@
1/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <linux/kernel.h>
31#include <linux/init.h>
32
33#include "iwl-io.h"
34#include "iwl-agn-hw.h"
35#include "iwl-trans.h"
36#include "iwl-fh.h"
37#include "iwl-op-mode.h"
38
39#include "dev.h"
40#include "agn.h"
41#include "calib.h"
42
43/******************************************************************************
44 *
45 * uCode download functions
46 *
47 ******************************************************************************/
48
49static inline const struct fw_img *
50iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
51{
52 if (ucode_type >= IWL_UCODE_TYPE_MAX)
53 return NULL;
54
55 return &priv->fw->img[ucode_type];
56}
57
58/*
59 * Calibration
60 */
61static int iwl_set_Xtal_calib(struct iwl_priv *priv)
62{
63 struct iwl_calib_xtal_freq_cmd cmd;
64 __le16 *xtal_calib =
65 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_XTAL);
66
67 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
68 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
69 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
70 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
71}
72
73static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
74{
75 struct iwl_calib_temperature_offset_cmd cmd;
76 __le16 *offset_calib =
77 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_RAW_TEMPERATURE);
78
79 memset(&cmd, 0, sizeof(cmd));
80 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
81 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
82 if (!(cmd.radio_sensor_offset))
83 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
84
85 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
86 le16_to_cpu(cmd.radio_sensor_offset));
87 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
88}
89
90static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
91{
92 struct iwl_calib_temperature_offset_v2_cmd cmd;
93 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv,
94 EEPROM_KELVIN_TEMPERATURE);
95 __le16 *offset_calib_low =
96 (__le16 *)iwl_eeprom_query_addr(priv, EEPROM_RAW_TEMPERATURE);
97 struct iwl_eeprom_calib_hdr *hdr;
98
99 memset(&cmd, 0, sizeof(cmd));
100 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
101 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
102 EEPROM_CALIB_ALL);
103 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
104 sizeof(*offset_calib_high));
105 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
106 sizeof(*offset_calib_low));
107 if (!(cmd.radio_sensor_offset_low)) {
108 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
109 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
110 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
111 }
112 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
113 sizeof(hdr->voltage));
114
115 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
116 le16_to_cpu(cmd.radio_sensor_offset_high));
117 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
118 le16_to_cpu(cmd.radio_sensor_offset_low));
119 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
120 le16_to_cpu(cmd.burntVoltageRef));
121
122 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
123}
124
125static int iwl_send_calib_cfg(struct iwl_priv *priv)
126{
127 struct iwl_calib_cfg_cmd calib_cfg_cmd;
128 struct iwl_host_cmd cmd = {
129 .id = CALIBRATION_CFG_CMD,
130 .len = { sizeof(struct iwl_calib_cfg_cmd), },
131 .data = { &calib_cfg_cmd, },
132 };
133
134 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
135 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
136 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
137 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
138 calib_cfg_cmd.ucd_calib_cfg.flags =
139 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
140
141 return iwl_dvm_send_cmd(priv, &cmd);
142}
143
144int iwl_init_alive_start(struct iwl_priv *priv)
145{
146 int ret;
147
148 if (priv->cfg->bt_params &&
149 priv->cfg->bt_params->advanced_bt_coexist) {
150 /*
151 * Tell uCode we are ready to perform calibration
152 * need to perform this before any calibration
153 * no need to close the envlope since we are going
154 * to load the runtime uCode later.
155 */
156 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
157 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
158 if (ret)
159 return ret;
160
161 }
162
163 ret = iwl_send_calib_cfg(priv);
164 if (ret)
165 return ret;
166
167 /**
168 * temperature offset calibration is only needed for runtime ucode,
169 * so prepare the value now.
170 */
171 if (priv->cfg->need_temp_offset_calib) {
172 if (priv->cfg->temp_offset_v2)
173 return iwl_set_temperature_offset_calib_v2(priv);
174 else
175 return iwl_set_temperature_offset_calib(priv);
176 }
177
178 return 0;
179}
180
181static int iwl_send_wimax_coex(struct iwl_priv *priv)
182{
183 struct iwl_wimax_coex_cmd coex_cmd;
184
185 /* coexistence is disabled */
186 memset(&coex_cmd, 0, sizeof(coex_cmd));
187
188 return iwl_dvm_send_cmd_pdu(priv,
189 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
190 sizeof(coex_cmd), &coex_cmd);
191}
192
193static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
194 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
195 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
196 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
197 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
198 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
199 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
200 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
201 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
202 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
203 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
204 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
205 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
206 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
207 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
208 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
209 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
210 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
211 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
212 0, 0, 0, 0, 0, 0, 0
213};
214
215void iwl_send_prio_tbl(struct iwl_priv *priv)
216{
217 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
218
219 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
220 sizeof(iwl_bt_prio_tbl));
221 if (iwl_dvm_send_cmd_pdu(priv,
222 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
223 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
224 IWL_ERR(priv, "failed to send BT prio tbl command\n");
225}
226
227int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
228{
229 struct iwl_bt_coex_prot_env_cmd env_cmd;
230 int ret;
231
232 env_cmd.action = action;
233 env_cmd.type = type;
234 ret = iwl_dvm_send_cmd_pdu(priv,
235 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
236 sizeof(env_cmd), &env_cmd);
237 if (ret)
238 IWL_ERR(priv, "failed to send BT env command\n");
239 return ret;
240}
241
242
243static int iwl_alive_notify(struct iwl_priv *priv)
244{
245 int ret;
246
247 iwl_trans_fw_alive(priv->trans);
248
249 priv->passive_no_rx = false;
250 priv->transport_queue_stop = 0;
251
252 ret = iwl_send_wimax_coex(priv);
253 if (ret)
254 return ret;
255
256 if (!priv->cfg->no_xtal_calib) {
257 ret = iwl_set_Xtal_calib(priv);
258 if (ret)
259 return ret;
260 }
261
262 return iwl_send_calib_results(priv);
263}
264
265
266/**
267 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
268 * using sample data 100 bytes apart. If these sample points are good,
269 * it's a pretty good bet that everything between them is good, too.
270 */
271static int iwl_verify_sec_sparse(struct iwl_priv *priv,
272 const struct fw_desc *fw_desc)
273{
274 __le32 *image = (__le32 *)fw_desc->v_addr;
275 u32 len = fw_desc->len;
276 u32 val;
277 u32 i;
278
279 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
280
281 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
282 /* read data comes through single port, auto-incr addr */
283 /* NOTE: Use the debugless read so we don't flood kernel log
284 * if IWL_DL_IO is set */
285 iwl_write_direct32(priv->trans, HBUS_TARG_MEM_RADDR,
286 i + fw_desc->offset);
287 val = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
288 if (val != le32_to_cpu(*image))
289 return -EIO;
290 }
291
292 return 0;
293}
294
295static void iwl_print_mismatch_sec(struct iwl_priv *priv,
296 const struct fw_desc *fw_desc)
297{
298 __le32 *image = (__le32 *)fw_desc->v_addr;
299 u32 len = fw_desc->len;
300 u32 val;
301 u32 offs;
302 int errors = 0;
303
304 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
305
306 iwl_write_direct32(priv->trans, HBUS_TARG_MEM_RADDR,
307 fw_desc->offset);
308
309 for (offs = 0;
310 offs < len && errors < 20;
311 offs += sizeof(u32), image++) {
312 /* read data comes through single port, auto-incr addr */
313 val = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
314 if (val != le32_to_cpu(*image)) {
315 IWL_ERR(priv, "uCode INST section at "
316 "offset 0x%x, is 0x%x, s/b 0x%x\n",
317 offs, val, le32_to_cpu(*image));
318 errors++;
319 }
320 }
321}
322
323/**
324 * iwl_verify_ucode - determine which instruction image is in SRAM,
325 * and verify its contents
326 */
327static int iwl_verify_ucode(struct iwl_priv *priv,
328 enum iwl_ucode_type ucode_type)
329{
330 const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
331
332 if (!img) {
333 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
334 return -EINVAL;
335 }
336
337 if (!iwl_verify_sec_sparse(priv, &img->sec[IWL_UCODE_SECTION_INST])) {
338 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
339 return 0;
340 }
341
342 IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
343
344 iwl_print_mismatch_sec(priv, &img->sec[IWL_UCODE_SECTION_INST]);
345 return -EIO;
346}
347
348struct iwl_alive_data {
349 bool valid;
350 u8 subtype;
351};
352
353static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
354 struct iwl_rx_packet *pkt, void *data)
355{
356 struct iwl_priv *priv =
357 container_of(notif_wait, struct iwl_priv, notif_wait);
358 struct iwl_alive_data *alive_data = data;
359 struct iwl_alive_resp *palive;
360
361 palive = (void *)pkt->data;
362
363 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
364 "0x%01X 0x%01X\n",
365 palive->is_valid, palive->ver_type,
366 palive->ver_subtype);
367
368 priv->device_pointers.error_event_table =
369 le32_to_cpu(palive->error_event_table_ptr);
370 priv->device_pointers.log_event_table =
371 le32_to_cpu(palive->log_event_table_ptr);
372
373 alive_data->subtype = palive->ver_subtype;
374 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
375
376 return true;
377}
378
379#define UCODE_ALIVE_TIMEOUT HZ
380#define UCODE_CALIB_TIMEOUT (2*HZ)
381
382int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
383 enum iwl_ucode_type ucode_type)
384{
385 struct iwl_notification_wait alive_wait;
386 struct iwl_alive_data alive_data;
387 const struct fw_img *fw;
388 int ret;
389 enum iwl_ucode_type old_type;
390 static const u8 alive_cmd[] = { REPLY_ALIVE };
391
392 old_type = priv->cur_ucode;
393 priv->cur_ucode = ucode_type;
394 fw = iwl_get_ucode_image(priv, ucode_type);
395
396 priv->ucode_loaded = false;
397
398 if (!fw)
399 return -EINVAL;
400
401 iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
402 alive_cmd, ARRAY_SIZE(alive_cmd),
403 iwl_alive_fn, &alive_data);
404
405 ret = iwl_trans_start_fw(priv->trans, fw);
406 if (ret) {
407 priv->cur_ucode = old_type;
408 iwl_remove_notification(&priv->notif_wait, &alive_wait);
409 return ret;
410 }
411
412 /*
413 * Some things may run in the background now, but we
414 * just wait for the ALIVE notification here.
415 */
416 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
417 UCODE_ALIVE_TIMEOUT);
418 if (ret) {
419 priv->cur_ucode = old_type;
420 return ret;
421 }
422
423 if (!alive_data.valid) {
424 IWL_ERR(priv, "Loaded ucode is not valid!\n");
425 priv->cur_ucode = old_type;
426 return -EIO;
427 }
428
429 /*
430 * This step takes a long time (60-80ms!!) and
431 * WoWLAN image should be loaded quickly, so
432 * skip it for WoWLAN.
433 */
434 if (ucode_type != IWL_UCODE_WOWLAN) {
435 ret = iwl_verify_ucode(priv, ucode_type);
436 if (ret) {
437 priv->cur_ucode = old_type;
438 return ret;
439 }
440
441 /* delay a bit to give rfkill time to run */
442 msleep(5);
443 }
444
445 ret = iwl_alive_notify(priv);
446 if (ret) {
447 IWL_WARN(priv,
448 "Could not complete ALIVE transition: %d\n", ret);
449 priv->cur_ucode = old_type;
450 return ret;
451 }
452
453 priv->ucode_loaded = true;
454
455 return 0;
456}
457
458static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
459 struct iwl_rx_packet *pkt, void *data)
460{
461 struct iwl_priv *priv = data;
462 struct iwl_calib_hdr *hdr;
463 int len;
464
465 if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
466 WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
467 return true;
468 }
469
470 hdr = (struct iwl_calib_hdr *)pkt->data;
471 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
472
473 /* reduce the size by the length field itself */
474 len -= sizeof(__le32);
475
476 if (iwl_calib_set(priv, hdr, len))
477 IWL_ERR(priv, "Failed to record calibration data %d\n",
478 hdr->op_code);
479
480 return false;
481}
482
483int iwl_run_init_ucode(struct iwl_priv *priv)
484{
485 struct iwl_notification_wait calib_wait;
486 static const u8 calib_complete[] = {
487 CALIBRATION_RES_NOTIFICATION,
488 CALIBRATION_COMPLETE_NOTIFICATION
489 };
490 int ret;
491
492 lockdep_assert_held(&priv->mutex);
493
494 /* No init ucode required? Curious, but maybe ok */
495 if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
496 return 0;
497
498 if (priv->init_ucode_run)
499 return 0;
500
501 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
502 calib_complete, ARRAY_SIZE(calib_complete),
503 iwlagn_wait_calib, priv);
504
505 /* Will also start the device */
506 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
507 if (ret)
508 goto error;
509
510 ret = iwl_init_alive_start(priv);
511 if (ret)
512 goto error;
513
514 /*
515 * Some things may run in the background now, but we
516 * just wait for the calibration complete notification.
517 */
518 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
519 UCODE_CALIB_TIMEOUT);
520 if (!ret)
521 priv->init_ucode_run = true;
522
523 goto out;
524
525 error:
526 iwl_remove_notification(&priv->notif_wait, &calib_wait);
527 out:
528 /* Whatever happened, stop the device */
529 iwl_trans_stop_device(priv->trans);
530 priv->ucode_loaded = false;
531
532 return ret;
533}