blob: 6b3273d82157a64c491df82eb12a182c0edc0927 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
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) 2012 - 2013 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
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020025 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010026 *
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) 2012 - 2013 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/kernel.h>
64#include <linux/slab.h>
65#include <linux/skbuff.h>
66#include <linux/netdevice.h>
67#include <linux/etherdevice.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010068#include <linux/ip.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010069#include <net/mac80211.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010070#include <net/tcp.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010071
72#include "iwl-op-mode.h"
73#include "iwl-io.h"
74#include "mvm.h"
75#include "sta.h"
76#include "time-event.h"
77#include "iwl-eeprom-parse.h"
78#include "fw-api-scan.h"
79#include "iwl-phy-db.h"
David Spinadel507cadf2013-07-31 18:07:21 +030080#include "testmode.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010081
82static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
83 {
84 .max = 1,
Ilan Peer8eb38712013-06-01 20:17:18 +030085 .types = BIT(NL80211_IFTYPE_STATION),
Johannes Berg8ca151b2013-01-24 14:25:36 +010086 },
Johannes Berg3c15a0f2013-05-31 10:17:19 +020087 {
88 .max = 1,
Ilan Peer8eb38712013-06-01 20:17:18 +030089 .types = BIT(NL80211_IFTYPE_AP) |
90 BIT(NL80211_IFTYPE_P2P_CLIENT) |
Johannes Berg3c15a0f2013-05-31 10:17:19 +020091 BIT(NL80211_IFTYPE_P2P_GO),
92 },
93 {
94 .max = 1,
95 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
96 },
Johannes Berg8ca151b2013-01-24 14:25:36 +010097};
98
99static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
100 {
101 .num_different_channels = 1,
102 .max_interfaces = 3,
103 .limits = iwl_mvm_limits,
104 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
105 },
106};
107
Johannes Bergf0c26462013-01-22 20:41:58 +0100108#ifdef CONFIG_PM_SLEEP
109static const struct nl80211_wowlan_tcp_data_token_feature
110iwl_mvm_wowlan_tcp_token_feature = {
111 .min_len = 0,
112 .max_len = 255,
113 .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
114};
115
116static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
117 .tok = &iwl_mvm_wowlan_tcp_token_feature,
118 .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
119 sizeof(struct ethhdr) -
120 sizeof(struct iphdr) -
121 sizeof(struct tcphdr),
122 .data_interval_max = 65535, /* __le16 in API */
123 .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
124 sizeof(struct ethhdr) -
125 sizeof(struct iphdr) -
126 sizeof(struct tcphdr),
127 .seq = true,
128};
129#endif
130
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200131static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
132{
133 int i;
134
135 memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
136 for (i = 0; i < NUM_PHY_CTX; i++) {
137 mvm->phy_ctxts[i].id = i;
138 mvm->phy_ctxts[i].ref = 0;
139 }
140}
141
David Spinadel20f1a5d2013-08-21 09:14:27 +0300142static int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
143{
144 /* we create the 802.11 header and SSID element */
145 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID)
146 return mvm->fw->ucode_capa.max_probe_length - 24 - 2;
147 return mvm->fw->ucode_capa.max_probe_length - 24 - 34;
148}
149
Johannes Berg8ca151b2013-01-24 14:25:36 +0100150int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
151{
152 struct ieee80211_hw *hw = mvm->hw;
Ilan Peer831e85f2013-02-07 17:09:09 +0200153 int num_mac, ret, i;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100154
155 /* Tell mac80211 our characteristics */
156 hw->flags = IEEE80211_HW_SIGNAL_DBM |
157 IEEE80211_HW_SPECTRUM_MGMT |
158 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
159 IEEE80211_HW_QUEUE_CONTROL |
160 IEEE80211_HW_WANT_MONITOR_VIF |
Johannes Berg8ca151b2013-01-24 14:25:36 +0100161 IEEE80211_HW_SUPPORTS_PS |
162 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
Johannes Bergd2931bb2013-02-05 18:10:04 +0100163 IEEE80211_HW_AMPDU_AGGREGATION |
Hila Gonend64048e2013-03-13 18:00:03 +0200164 IEEE80211_HW_TIMING_BEACON_ONLY |
Emmanuel Grumbach147fc9b2013-07-28 11:00:52 +0300165 IEEE80211_HW_CONNECTION_MONITOR |
166 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
Alexander Bondare8e626a2013-10-16 00:21:34 +0200167 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100168
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200169 hw->queues = mvm->first_agg_queue;
Ilan Peer398e8c62013-03-13 15:20:35 +0200170 hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100171 hw->rate_control_algorithm = "iwl-mvm-rs";
172
173 /*
174 * Enable 11w if advertised by firmware and software crypto
175 * is not enabled (as the firmware will interpret some mgmt
176 * packets, so enabling it with software crypto isn't safe)
177 */
178 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
179 !iwlwifi_mod_params.sw_crypto)
180 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
181
Alexander Bondare8e626a2013-10-16 00:21:34 +0200182 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT) {
183 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
184 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
185 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
186 }
187
Johannes Berg8ca151b2013-01-24 14:25:36 +0100188 hw->sta_data_size = sizeof(struct iwl_mvm_sta);
189 hw->vif_data_size = sizeof(struct iwl_mvm_vif);
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200190 hw->chanctx_data_size = sizeof(u16);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100191
192 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
Johannes Berg3c15a0f2013-05-31 10:17:19 +0200193 BIT(NL80211_IFTYPE_P2P_CLIENT) |
194 BIT(NL80211_IFTYPE_AP) |
195 BIT(NL80211_IFTYPE_P2P_GO) |
196 BIT(NL80211_IFTYPE_P2P_DEVICE);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100197
Johannes Berg5023d962013-07-31 14:07:43 +0200198 /* IBSS has bugs in older versions */
199 if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 8)
200 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
201
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +0100202 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
203 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
204 REGULATORY_DISABLE_BEACON_HINTS;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100205
206 hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
207 hw->wiphy->n_iface_combinations =
208 ARRAY_SIZE(iwl_mvm_iface_combinations);
209
Ilan Peerc451e6d2013-02-20 08:41:54 +0200210 hw->wiphy->max_remain_on_channel_duration = 10000;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100211 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
212
213 /* Extract MAC address */
214 memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
215 hw->wiphy->addresses = mvm->addresses;
216 hw->wiphy->n_addresses = 1;
Ilan Peer831e85f2013-02-07 17:09:09 +0200217
218 /* Extract additional MAC addresses if available */
219 num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
220 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
221
222 for (i = 1; i < num_mac; i++) {
223 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100224 ETH_ALEN);
Ilan Peer831e85f2013-02-07 17:09:09 +0200225 mvm->addresses[i].addr[5]++;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100226 hw->wiphy->n_addresses++;
227 }
228
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200229 iwl_mvm_reset_phy_ctxts(mvm);
230
David Spinadel20f1a5d2013-08-21 09:14:27 +0300231 hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
232
Johannes Berg8ca151b2013-01-24 14:25:36 +0100233 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
234
235 if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
236 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
237 &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
238 if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
239 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
240 &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
241
242 hw->wiphy->hw_version = mvm->trans->hw_id;
243
Alexander Bondarade50652013-04-03 16:28:47 +0300244 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100245 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
246 else
247 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
248
David Spinadel35a000b2013-08-28 09:29:43 +0300249 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SCHED_SCAN) {
250 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
251 hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
252 hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
253 /* we create the 802.11 header and zero length SSID IE. */
254 hw->wiphy->max_sched_scan_ie_len =
255 SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
256 }
257
Johannes Berg8ca151b2013-01-24 14:25:36 +0100258 hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
259 NL80211_FEATURE_P2P_GO_OPPPS;
260
261 mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
262
263#ifdef CONFIG_PM_SLEEP
264 if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
265 mvm->trans->ops->d3_suspend &&
266 mvm->trans->ops->d3_resume &&
267 device_can_wakeup(mvm->trans->dev)) {
Johannes Berg964dc9e2013-06-03 17:25:34 +0200268 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
269 WIPHY_WOWLAN_DISCONNECT |
270 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
271 WIPHY_WOWLAN_RFKILL_RELEASE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100272 if (!iwlwifi_mod_params.sw_crypto)
Johannes Berg964dc9e2013-06-03 17:25:34 +0200273 mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
274 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
275 WIPHY_WOWLAN_4WAY_HANDSHAKE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100276
Johannes Berg964dc9e2013-06-03 17:25:34 +0200277 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
278 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
279 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
280 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
281 hw->wiphy->wowlan = &mvm->wowlan;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100282 }
283#endif
284
285 ret = iwl_mvm_leds_init(mvm);
286 if (ret)
287 return ret;
288
Emmanuel Grumbachb7327d82013-06-24 15:44:03 +0300289 ret = ieee80211_register_hw(mvm->hw);
290 if (ret)
291 iwl_mvm_leds_exit(mvm);
292
293 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100294}
295
296static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
297 struct ieee80211_tx_control *control,
298 struct sk_buff *skb)
299{
300 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
301
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300302 if (iwl_mvm_is_radio_killed(mvm)) {
303 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +0100304 goto drop;
305 }
306
Ilan Peer398e8c62013-03-13 15:20:35 +0200307 if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
Johannes Berg8ca151b2013-01-24 14:25:36 +0100308 !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
309 goto drop;
310
311 if (control->sta) {
312 if (iwl_mvm_tx_skb(mvm, skb, control->sta))
313 goto drop;
314 return;
315 }
316
317 if (iwl_mvm_tx_skb_non_sta(mvm, skb))
318 goto drop;
319 return;
320 drop:
321 ieee80211_free_txskb(hw, skb);
322}
323
324static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
325 struct ieee80211_vif *vif,
326 enum ieee80211_ampdu_mlme_action action,
327 struct ieee80211_sta *sta, u16 tid,
328 u16 *ssn, u8 buf_size)
329{
330 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
331 int ret;
332
333 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
334 sta->addr, tid, action);
335
336 if (!(mvm->nvm_data->sku_cap_11n_enable))
337 return -EACCES;
338
339 mutex_lock(&mvm->mutex);
340
341 switch (action) {
342 case IEEE80211_AMPDU_RX_START:
343 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG) {
344 ret = -EINVAL;
345 break;
346 }
347 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
348 break;
349 case IEEE80211_AMPDU_RX_STOP:
350 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
351 break;
352 case IEEE80211_AMPDU_TX_START:
Emmanuel Grumbach5d158ef2013-02-19 14:39:58 +0200353 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG) {
354 ret = -EINVAL;
355 break;
356 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100357 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
358 break;
359 case IEEE80211_AMPDU_TX_STOP_CONT:
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +0200360 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
361 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100362 case IEEE80211_AMPDU_TX_STOP_FLUSH:
363 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +0200364 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100365 break;
366 case IEEE80211_AMPDU_TX_OPERATIONAL:
367 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
368 break;
369 default:
370 WARN_ON_ONCE(1);
371 ret = -EINVAL;
372 break;
373 }
374 mutex_unlock(&mvm->mutex);
375
376 return ret;
377}
378
379static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
380 struct ieee80211_vif *vif)
381{
382 struct iwl_mvm *mvm = data;
383 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
384
385 mvmvif->uploaded = false;
386 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
387
388 /* does this make sense at all? */
389 mvmvif->color++;
390
391 spin_lock_bh(&mvm->time_event_lock);
392 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
393 spin_unlock_bh(&mvm->time_event_lock);
394
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200395 mvmvif->phy_ctxt = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100396}
397
398static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
399{
400 iwl_trans_stop_device(mvm->trans);
401 iwl_trans_stop_hw(mvm->trans, false);
402
403 mvm->scan_status = IWL_MVM_SCAN_NONE;
404
405 /* just in case one was running */
406 ieee80211_remain_on_channel_expired(mvm->hw);
407
408 ieee80211_iterate_active_interfaces_atomic(
409 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
410 iwl_mvm_cleanup_iterator, mvm);
411
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200412 mvm->p2p_device_vif = NULL;
413
414 iwl_mvm_reset_phy_ctxts(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100415 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
416 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
417
418 ieee80211_wake_queues(mvm->hw);
419
420 mvm->vif_count = 0;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300421 mvm->rx_ba_sessions = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100422}
423
424static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
425{
426 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
427 int ret;
428
429 mutex_lock(&mvm->mutex);
430
431 /* Clean up some internal and mac80211 state on restart */
432 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
433 iwl_mvm_restart_cleanup(mvm);
434
435 ret = iwl_mvm_up(mvm);
436 mutex_unlock(&mvm->mutex);
437
438 return ret;
439}
440
441static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
442{
443 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
444 int ret;
445
446 mutex_lock(&mvm->mutex);
447
448 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
449 ret = iwl_mvm_update_quotas(mvm, NULL);
450 if (ret)
451 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
452 ret);
453
454 mutex_unlock(&mvm->mutex);
455}
456
457static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
458{
459 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
460
461 flush_work(&mvm->async_handlers_wk);
462
463 mutex_lock(&mvm->mutex);
464 /* async_handlers_wk is now blocked */
465
466 /*
467 * The work item could be running or queued if the
468 * ROC time event stops just as we get here.
469 */
470 cancel_work_sync(&mvm->roc_done_wk);
471
472 iwl_trans_stop_device(mvm->trans);
473 iwl_trans_stop_hw(mvm->trans, false);
474
475 iwl_mvm_async_handlers_purge(mvm);
476 /* async_handlers_list is empty and will stay empty: HW is stopped */
477
478 /* the fw is stopped, the aux sta is dead: clean up driver state */
479 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
480
481 mutex_unlock(&mvm->mutex);
482
483 /*
484 * The worker might have been waiting for the mutex, let it run and
485 * discover that its list is now empty.
486 */
487 cancel_work_sync(&mvm->async_handlers_wk);
488}
489
490static void iwl_mvm_pm_disable_iterator(void *data, u8 *mac,
491 struct ieee80211_vif *vif)
492{
493 struct iwl_mvm *mvm = data;
494 int ret;
495
496 ret = iwl_mvm_power_disable(mvm, vif);
497 if (ret)
498 IWL_ERR(mvm, "failed to disable power management\n");
499}
500
501static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
502 struct ieee80211_vif *vif)
503{
504 struct iwl_mvm *mvm = data;
505
506 iwl_mvm_power_update_mode(mvm, vif);
507}
508
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200509static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
510{
511 u16 i;
512
513 lockdep_assert_held(&mvm->mutex);
514
515 for (i = 0; i < NUM_PHY_CTX; i++)
516 if (!mvm->phy_ctxts[i].ref)
517 return &mvm->phy_ctxts[i];
518
519 IWL_ERR(mvm, "No available PHY context\n");
520 return NULL;
521}
522
Johannes Berg8ca151b2013-01-24 14:25:36 +0100523static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
524 struct ieee80211_vif *vif)
525{
526 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
527 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
528 int ret;
529
530 /*
531 * Not much to do here. The stack will not allow interface
532 * types or combinations that we didn't advertise, so we
533 * don't really have to check the types.
534 */
535
536 mutex_lock(&mvm->mutex);
537
Eliad Pellere89044d2013-07-16 17:33:26 +0300538 /* Allocate resources for the MAC context, and add it to the fw */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100539 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
540 if (ret)
541 goto out_unlock;
542
543 /*
Ilan Peerea183d02013-07-23 14:41:53 +0300544 * TODO: remove this temporary code.
545 * Currently MVM FW supports power management only on single MAC.
546 * If new interface added, disable PM on existing interface.
547 * P2P device is a special case, since it is handled by FW similary to
548 * scan. If P2P deviced is added, PM remains enabled on existing
549 * interface.
550 * Note: the method below does not count the new interface being added
551 * at this moment.
552 */
553 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
554 mvm->vif_count++;
555 if (mvm->vif_count > 1) {
556 IWL_DEBUG_MAC80211(mvm,
557 "Disable power on existing interfaces\n");
558 ieee80211_iterate_active_interfaces_atomic(
559 mvm->hw,
560 IEEE80211_IFACE_ITER_NORMAL,
561 iwl_mvm_pm_disable_iterator, mvm);
562 }
563
564 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100565 * The AP binding flow can be done only after the beacon
566 * template is configured (which happens only in the mac80211
567 * start_ap() flow), and adding the broadcast station can happen
568 * only after the binding.
569 * In addition, since modifying the MAC before adding a bcast
570 * station is not allowed by the FW, delay the adding of MAC context to
571 * the point where we can also add the bcast station.
572 * In short: there's not much we can do at this point, other than
573 * allocating resources :)
574 */
Johannes Berg5023d962013-07-31 14:07:43 +0200575 if (vif->type == NL80211_IFTYPE_AP ||
576 vif->type == NL80211_IFTYPE_ADHOC) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100577 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
578 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
579 qmask);
580 if (ret) {
581 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
582 goto out_release;
583 }
584
Emmanuel Grumbach77740cb2013-06-26 23:51:41 +0300585 iwl_mvm_vif_dbgfs_register(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100586 goto out_unlock;
587 }
588
Johannes Berg8ca151b2013-01-24 14:25:36 +0100589 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
590 if (ret)
591 goto out_release;
592
593 /*
594 * Update power state on the new interface. Admittedly, based on
595 * mac80211 logics this power update will disable power management
596 */
597 iwl_mvm_power_update_mode(mvm, vif);
598
Hila Gonen7df15b12012-12-12 11:16:19 +0200599 /* beacon filtering */
Eliad Pellerbd3351b2013-07-16 17:50:17 +0300600 ret = iwl_mvm_disable_beacon_filter(mvm, vif);
601 if (ret)
602 goto out_remove_mac;
603
Hila Gonen7df15b12012-12-12 11:16:19 +0200604 if (!mvm->bf_allowed_vif &&
Hila Gonen5dca7c22013-07-16 11:15:35 +0300605 vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
606 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED){
Hila Gonen7df15b12012-12-12 11:16:19 +0200607 mvm->bf_allowed_vif = mvmvif;
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300608 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
609 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
Hila Gonen7df15b12012-12-12 11:16:19 +0200610 }
611
Johannes Berg8ca151b2013-01-24 14:25:36 +0100612 /*
613 * P2P_DEVICE interface does not have a channel context assigned to it,
614 * so a dedicated PHY context is allocated to it and the corresponding
615 * MAC context is bound to it at this stage.
616 */
617 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100618
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200619 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
620 if (!mvmvif->phy_ctxt) {
621 ret = -ENOSPC;
Eliad Pellerbd3351b2013-07-16 17:50:17 +0300622 goto out_free_bf;
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200623 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100624
Ilan Peer53a9d612013-04-28 11:55:08 +0300625 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100626 ret = iwl_mvm_binding_add_vif(mvm, vif);
627 if (ret)
Ilan Peer53a9d612013-04-28 11:55:08 +0300628 goto out_unref_phy;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100629
630 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
631 if (ret)
632 goto out_unbind;
633
634 /* Save a pointer to p2p device vif, so it can later be used to
635 * update the p2p device MAC when a GO is started/stopped */
636 mvm->p2p_device_vif = vif;
637 }
638
Johannes Berg63494372013-03-26 10:47:53 +0100639 iwl_mvm_vif_dbgfs_register(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100640 goto out_unlock;
641
642 out_unbind:
643 iwl_mvm_binding_remove_vif(mvm, vif);
Ilan Peer53a9d612013-04-28 11:55:08 +0300644 out_unref_phy:
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200645 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
Eliad Pellerbd3351b2013-07-16 17:50:17 +0300646 out_free_bf:
647 if (mvm->bf_allowed_vif == mvmvif) {
648 mvm->bf_allowed_vif = NULL;
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300649 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
650 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
Eliad Pellerbd3351b2013-07-16 17:50:17 +0300651 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100652 out_remove_mac:
653 mvmvif->phy_ctxt = NULL;
654 iwl_mvm_mac_ctxt_remove(mvm, vif);
655 out_release:
Alexander Bondar5ee2b212013-03-05 10:16:40 +0200656 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
657 mvm->vif_count--;
Alexander Bondar4bf881f2013-05-29 10:19:50 +0300658 ieee80211_iterate_active_interfaces(
659 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
660 iwl_mvm_power_update_iterator, mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100661 iwl_mvm_mac_ctxt_release(mvm, vif);
662 out_unlock:
663 mutex_unlock(&mvm->mutex);
664
665 return ret;
666}
667
Johannes Berg38a12b52013-02-22 14:07:56 +0100668static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
669 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100670{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100671 u32 tfd_msk = 0, ac;
672
673 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
674 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
675 tfd_msk |= BIT(vif->hw_queue[ac]);
676
677 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
678 tfd_msk |= BIT(vif->cab_queue);
679
680 if (tfd_msk) {
681 mutex_lock(&mvm->mutex);
682 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
683 mutex_unlock(&mvm->mutex);
684 }
685
686 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
687 /*
688 * Flush the ROC worker which will flush the OFFCHANNEL queue.
689 * We assume here that all the packets sent to the OFFCHANNEL
690 * queue are sent in ROC session.
691 */
692 flush_work(&mvm->roc_done_wk);
693 } else {
694 /*
695 * By now, all the AC queues are empty. The AGG queues are
696 * empty too. We already got all the Tx responses for all the
697 * packets in the queues. The drain work can have been
Emmanuel Grumbach0742a752013-06-10 14:10:33 +0300698 * triggered. Flush it.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100699 */
700 flush_work(&mvm->sta_drained_wk);
701 }
Johannes Berg38a12b52013-02-22 14:07:56 +0100702}
703
704static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
705 struct ieee80211_vif *vif)
706{
707 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
708 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
709
710 iwl_mvm_prepare_mac_removal(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100711
712 mutex_lock(&mvm->mutex);
713
Hila Gonen7df15b12012-12-12 11:16:19 +0200714 if (mvm->bf_allowed_vif == mvmvif) {
715 mvm->bf_allowed_vif = NULL;
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300716 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
717 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
Hila Gonen7df15b12012-12-12 11:16:19 +0200718 }
719
Johannes Berg63494372013-03-26 10:47:53 +0100720 iwl_mvm_vif_dbgfs_clean(mvm, vif);
721
Johannes Berg8ca151b2013-01-24 14:25:36 +0100722 /*
723 * For AP/GO interface, the tear down of the resources allocated to the
Johannes Berg38a12b52013-02-22 14:07:56 +0100724 * interface is be handled as part of the stop_ap flow.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100725 */
Johannes Berg5023d962013-07-31 14:07:43 +0200726 if (vif->type == NL80211_IFTYPE_AP ||
727 vif->type == NL80211_IFTYPE_ADHOC) {
David Spinadel507cadf2013-07-31 18:07:21 +0300728#ifdef CONFIG_NL80211_TESTMODE
729 if (vif == mvm->noa_vif) {
730 mvm->noa_vif = NULL;
731 mvm->noa_duration = 0;
732 }
733#endif
Johannes Berg8ca151b2013-01-24 14:25:36 +0100734 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
735 goto out_release;
736 }
737
738 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
739 mvm->p2p_device_vif = NULL;
740 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
741 iwl_mvm_binding_remove_vif(mvm, vif);
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200742 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100743 mvmvif->phy_ctxt = NULL;
744 }
745
746 /*
747 * TODO: remove this temporary code.
748 * Currently MVM FW supports power management only on single MAC.
749 * Check if only one additional interface remains after removing
750 * current one. Update power mode on the remaining interface.
751 */
Alexander Bondar5ee2b212013-03-05 10:16:40 +0200752 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100753 mvm->vif_count--;
754 IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
755 mvm->vif_count);
756 if (mvm->vif_count == 1) {
757 ieee80211_iterate_active_interfaces(
758 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
759 iwl_mvm_power_update_iterator, mvm);
760 }
761
762 iwl_mvm_mac_ctxt_remove(mvm, vif);
763
764out_release:
765 iwl_mvm_mac_ctxt_release(mvm, vif);
766 mutex_unlock(&mvm->mutex);
767}
768
Matti Gottlieb88f2fd72013-07-09 15:25:46 +0300769static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
770 s8 tx_power)
771{
772 /* FW is in charge of regulatory enforcement */
773 struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
774 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
775 .pwr_restriction = cpu_to_le16(tx_power),
776 };
777
778 return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
779 sizeof(reduce_txpwr_cmd),
780 &reduce_txpwr_cmd);
781}
782
Johannes Berg8ca151b2013-01-24 14:25:36 +0100783static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
784{
785 return 0;
786}
787
788static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
789 unsigned int changed_flags,
790 unsigned int *total_flags,
791 u64 multicast)
792{
793 *total_flags = 0;
794}
795
Emmanuel Grumbach51b6b9e2013-05-02 15:01:24 +0300796static int iwl_mvm_configure_mcast_filter(struct iwl_mvm *mvm,
797 struct ieee80211_vif *vif)
798{
799 struct iwl_mcast_filter_cmd mcast_filter_cmd = {
800 .pass_all = 1,
801 };
802
803 memcpy(mcast_filter_cmd.bssid, vif->bss_conf.bssid, ETH_ALEN);
804
805 return iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC,
806 sizeof(mcast_filter_cmd),
807 &mcast_filter_cmd);
808}
809
Johannes Berg8ca151b2013-01-24 14:25:36 +0100810static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
811 struct ieee80211_vif *vif,
812 struct ieee80211_bss_conf *bss_conf,
813 u32 changes)
814{
815 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
816 int ret;
817
818 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
819 if (ret)
820 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
821
822 if (changes & BSS_CHANGED_ASSOC) {
823 if (bss_conf->assoc) {
824 /* add quota for this interface */
825 ret = iwl_mvm_update_quotas(mvm, vif);
826 if (ret) {
827 IWL_ERR(mvm, "failed to update quotas\n");
828 return;
829 }
Emmanuel Grumbach51b6b9e2013-05-02 15:01:24 +0300830 iwl_mvm_configure_mcast_filter(mvm, vif);
Johannes Berg016d27e2013-05-03 11:16:15 +0200831
832 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
833 &mvm->status)) {
834 /*
835 * If we're restarting then the firmware will
836 * obviously have lost synchronisation with
837 * the AP. It will attempt to synchronise by
838 * itself, but we can make it more reliable by
839 * scheduling a session protection time event.
840 *
841 * The firmware needs to receive a beacon to
842 * catch up with synchronisation, use 110% of
843 * the beacon interval.
844 *
845 * Set a large maximum delay to allow for more
846 * than a single interface.
847 */
848 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
849 iwl_mvm_protect_session(mvm, vif, dur, dur,
850 5 * dur);
851 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100852 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
853 /* remove AP station now that the MAC is unassoc */
854 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
855 if (ret)
856 IWL_ERR(mvm, "failed to remove AP station\n");
857 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
858 /* remove quota for this interface */
859 ret = iwl_mvm_update_quotas(mvm, NULL);
860 if (ret)
861 IWL_ERR(mvm, "failed to update quotas\n");
862 }
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300863
864 /* reset rssi values */
865 mvmvif->bf_data.ave_beacon_signal = 0;
866
Alexander Bondare8e626a2013-10-16 00:21:34 +0200867 if (!(mvm->fw->ucode_capa.flags &
868 IWL_UCODE_TLV_FLAGS_PM_CMD_SUPPORT)) {
Alexander Bondare811ada2013-03-10 15:29:44 +0200869 /* Workaround for FW bug, otherwise FW disables device
870 * power save upon disassociation
871 */
872 ret = iwl_mvm_power_update_mode(mvm, vif);
873 if (ret)
874 IWL_ERR(mvm, "failed to update power mode\n");
875 }
Emmanuel Grumbach8e484f02013-10-02 15:02:25 +0300876 iwl_mvm_bt_coex_vif_change(mvm);
Alexander Bondar989c6502013-05-16 17:34:17 +0300877 } else if (changes & BSS_CHANGED_BEACON_INFO) {
Johannes Berg210a5442013-01-24 23:48:23 +0100878 /*
879 * We received a beacon _after_ association so
880 * remove the session protection.
881 */
882 iwl_mvm_remove_time_event(mvm, mvmvif,
883 &mvmvif->time_event_data);
Alexander Bondare3c588e2013-04-07 14:08:59 +0300884 } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_QOS)) {
Alexander Bondar4bf881f2013-05-29 10:19:50 +0300885 ret = iwl_mvm_power_update_mode(mvm, vif);
886 if (ret)
887 IWL_ERR(mvm, "failed to update power mode\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +0100888 }
Matti Gottlieb88f2fd72013-07-09 15:25:46 +0300889 if (changes & BSS_CHANGED_TXPOWER) {
890 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
891 bss_conf->txpower);
892 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
893 }
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300894
895 if (changes & BSS_CHANGED_CQM) {
896 IWL_DEBUG_MAC80211(mvm, "cqm info_changed");
897 /* reset cqm events tracking */
898 mvmvif->bf_data.last_cqm_event = 0;
899 ret = iwl_mvm_update_beacon_filter(mvm, vif);
900 if (ret)
901 IWL_ERR(mvm, "failed to update CQM thresholds\n");
902 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100903}
904
Johannes Berg5023d962013-07-31 14:07:43 +0200905static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
906 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100907{
908 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
909 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
910 int ret;
911
912 mutex_lock(&mvm->mutex);
913
914 /* Send the beacon template */
915 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
916 if (ret)
917 goto out_unlock;
918
919 /* Add the mac context */
920 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
921 if (ret)
922 goto out_unlock;
923
924 /* Perform the binding */
925 ret = iwl_mvm_binding_add_vif(mvm, vif);
926 if (ret)
927 goto out_remove;
928
Johannes Berg5023d962013-07-31 14:07:43 +0200929 mvmvif->ap_ibss_active = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100930
931 /* Send the bcast station. At this stage the TBTT and DTIM time events
932 * are added and applied to the scheduler */
933 ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
934 if (ret)
935 goto out_unbind;
936
937 ret = iwl_mvm_update_quotas(mvm, vif);
938 if (ret)
939 goto out_rm_bcast;
940
Johannes Berg5023d962013-07-31 14:07:43 +0200941 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100942 if (vif->p2p && mvm->p2p_device_vif)
943 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
944
Emmanuel Grumbach8e484f02013-10-02 15:02:25 +0300945 iwl_mvm_bt_coex_vif_change(mvm);
Emmanuel Grumbachdac94da2013-06-18 07:35:27 +0300946
Johannes Berg8ca151b2013-01-24 14:25:36 +0100947 mutex_unlock(&mvm->mutex);
948 return 0;
949
950out_rm_bcast:
951 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
952out_unbind:
953 iwl_mvm_binding_remove_vif(mvm, vif);
954out_remove:
955 iwl_mvm_mac_ctxt_remove(mvm, vif);
956out_unlock:
957 mutex_unlock(&mvm->mutex);
958 return ret;
959}
960
Johannes Berg5023d962013-07-31 14:07:43 +0200961static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
962 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963{
964 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
965 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
966
Johannes Berg38a12b52013-02-22 14:07:56 +0100967 iwl_mvm_prepare_mac_removal(mvm, vif);
968
Johannes Berg8ca151b2013-01-24 14:25:36 +0100969 mutex_lock(&mvm->mutex);
970
Johannes Berg5023d962013-07-31 14:07:43 +0200971 mvmvif->ap_ibss_active = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100972
Emmanuel Grumbach8e484f02013-10-02 15:02:25 +0300973 iwl_mvm_bt_coex_vif_change(mvm);
Emmanuel Grumbachdac94da2013-06-18 07:35:27 +0300974
Johannes Berg5023d962013-07-31 14:07:43 +0200975 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100976 if (vif->p2p && mvm->p2p_device_vif)
977 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
978
979 iwl_mvm_update_quotas(mvm, NULL);
980 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
981 iwl_mvm_binding_remove_vif(mvm, vif);
982 iwl_mvm_mac_ctxt_remove(mvm, vif);
983
984 mutex_unlock(&mvm->mutex);
985}
986
Johannes Berg5023d962013-07-31 14:07:43 +0200987static void
988iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
989 struct ieee80211_vif *vif,
990 struct ieee80211_bss_conf *bss_conf,
991 u32 changes)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100992{
Avri Altman8a5e3662013-11-12 19:16:03 +0200993 enum ieee80211_bss_change ht_change = BSS_CHANGED_ERP_CTS_PROT |
994 BSS_CHANGED_HT |
995 BSS_CHANGED_BANDWIDTH;
996 int ret;
997
998 if (changes & ht_change) {
999 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1000 if (ret)
1001 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1002 }
1003
Johannes Berg8ca151b2013-01-24 14:25:36 +01001004 /* Need to send a new beacon template to the FW */
1005 if (changes & BSS_CHANGED_BEACON) {
1006 if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1007 IWL_WARN(mvm, "Failed updating beacon data\n");
1008 }
1009}
1010
1011static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1012 struct ieee80211_vif *vif,
1013 struct ieee80211_bss_conf *bss_conf,
1014 u32 changes)
1015{
1016 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1017
1018 mutex_lock(&mvm->mutex);
1019
1020 switch (vif->type) {
1021 case NL80211_IFTYPE_STATION:
1022 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1023 break;
1024 case NL80211_IFTYPE_AP:
Johannes Berg5023d962013-07-31 14:07:43 +02001025 case NL80211_IFTYPE_ADHOC:
1026 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001027 break;
1028 default:
1029 /* shouldn't happen */
1030 WARN_ON_ONCE(1);
1031 }
1032
1033 mutex_unlock(&mvm->mutex);
1034}
1035
1036static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1037 struct ieee80211_vif *vif,
1038 struct cfg80211_scan_request *req)
1039{
1040 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1041 int ret;
1042
1043 if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
1044 return -EINVAL;
1045
1046 mutex_lock(&mvm->mutex);
1047
1048 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1049 ret = iwl_mvm_scan_request(mvm, vif, req);
1050 else
1051 ret = -EBUSY;
1052
1053 mutex_unlock(&mvm->mutex);
1054
1055 return ret;
1056}
1057
1058static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1059 struct ieee80211_vif *vif)
1060{
1061 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1062
1063 mutex_lock(&mvm->mutex);
1064
1065 iwl_mvm_cancel_scan(mvm);
1066
1067 mutex_unlock(&mvm->mutex);
1068}
1069
1070static void
1071iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1072 struct ieee80211_sta *sta, u16 tid,
1073 int num_frames,
1074 enum ieee80211_frame_release_type reason,
1075 bool more_data)
1076{
1077 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001078
1079 /* TODO: how do we tell the fw to send frames for a specific TID */
1080
1081 /*
1082 * The fw will send EOSP notification when the last frame will be
1083 * transmitted.
1084 */
Johannes Berg9cc40712013-02-15 22:47:48 +01001085 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001086}
1087
1088static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
1089 struct ieee80211_vif *vif,
1090 enum sta_notify_cmd cmd,
1091 struct ieee80211_sta *sta)
1092{
1093 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Johannes Berg5b577a92013-11-14 18:20:04 +01001094 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001095
1096 switch (cmd) {
1097 case STA_NOTIFY_SLEEP:
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001098 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001099 ieee80211_sta_block_awake(hw, sta, true);
1100 /*
1101 * The fw updates the STA to be asleep. Tx packets on the Tx
1102 * queues to this station will not be transmitted. The fw will
1103 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1104 */
1105 break;
1106 case STA_NOTIFY_AWAKE:
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001107 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001108 break;
Johannes Berg9cc40712013-02-15 22:47:48 +01001109 iwl_mvm_sta_modify_ps_wake(mvm, sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001110 break;
1111 default:
1112 break;
1113 }
1114}
1115
1116static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1117 struct ieee80211_vif *vif,
1118 struct ieee80211_sta *sta,
1119 enum ieee80211_sta_state old_state,
1120 enum ieee80211_sta_state new_state)
1121{
1122 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1123 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1124 int ret;
1125
1126 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1127 sta->addr, old_state, new_state);
1128
1129 /* this would be a mac80211 bug ... but don't crash */
1130 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1131 return -EINVAL;
1132
1133 /* if a STA is being removed, reuse its ID */
1134 flush_work(&mvm->sta_drained_wk);
1135
1136 mutex_lock(&mvm->mutex);
1137 if (old_state == IEEE80211_STA_NOTEXIST &&
1138 new_state == IEEE80211_STA_NONE) {
Johannes Berg48bc1302013-07-04 15:55:29 +02001139 /*
1140 * Firmware bug - it'll crash if the beacon interval is less
1141 * than 16. We can't avoid connecting at all, so refuse the
1142 * station state change, this will cause mac80211 to abandon
1143 * attempts to connect to this AP, and eventually wpa_s will
1144 * blacklist the AP...
1145 */
1146 if (vif->type == NL80211_IFTYPE_STATION &&
1147 vif->bss_conf.beacon_int < 16) {
1148 IWL_ERR(mvm,
1149 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1150 sta->addr, vif->bss_conf.beacon_int);
1151 ret = -EINVAL;
1152 goto out_unlock;
1153 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001154 ret = iwl_mvm_add_sta(mvm, vif, sta);
1155 } else if (old_state == IEEE80211_STA_NONE &&
1156 new_state == IEEE80211_STA_AUTH) {
1157 ret = 0;
1158 } else if (old_state == IEEE80211_STA_AUTH &&
1159 new_state == IEEE80211_STA_ASSOC) {
Johannes Berg7a453972013-02-12 13:10:44 +01001160 ret = iwl_mvm_update_sta(mvm, vif, sta);
1161 if (ret == 0)
1162 iwl_mvm_rs_rate_init(mvm, sta,
Eyal Shapirab87c2172013-11-09 23:37:55 +02001163 mvmvif->phy_ctxt->channel->band,
1164 true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001165 } else if (old_state == IEEE80211_STA_ASSOC &&
1166 new_state == IEEE80211_STA_AUTHORIZED) {
Hila Gonen7df15b12012-12-12 11:16:19 +02001167 /* enable beacon filtering */
1168 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001169 ret = 0;
1170 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1171 new_state == IEEE80211_STA_ASSOC) {
Hila Gonen7df15b12012-12-12 11:16:19 +02001172 /* disable beacon filtering */
1173 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001174 ret = 0;
1175 } else if (old_state == IEEE80211_STA_ASSOC &&
1176 new_state == IEEE80211_STA_AUTH) {
1177 ret = 0;
1178 } else if (old_state == IEEE80211_STA_AUTH &&
1179 new_state == IEEE80211_STA_NONE) {
1180 ret = 0;
1181 } else if (old_state == IEEE80211_STA_NONE &&
1182 new_state == IEEE80211_STA_NOTEXIST) {
1183 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1184 } else {
1185 ret = -EIO;
1186 }
Johannes Berg48bc1302013-07-04 15:55:29 +02001187 out_unlock:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001188 mutex_unlock(&mvm->mutex);
1189
1190 return ret;
1191}
1192
1193static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1194{
1195 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1196
1197 mvm->rts_threshold = value;
1198
1199 return 0;
1200}
1201
1202static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1203 struct ieee80211_vif *vif, u16 ac,
1204 const struct ieee80211_tx_queue_params *params)
1205{
1206 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1207 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1208
1209 mvmvif->queue_params[ac] = *params;
1210
1211 /*
1212 * No need to update right away, we'll get BSS_CHANGED_QOS
1213 * The exception is P2P_DEVICE interface which needs immediate update.
1214 */
1215 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1216 int ret;
1217
1218 mutex_lock(&mvm->mutex);
1219 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1220 mutex_unlock(&mvm->mutex);
1221 return ret;
1222 }
1223 return 0;
1224}
1225
1226static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1227 struct ieee80211_vif *vif)
1228{
1229 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1230 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1231 200 + vif->bss_conf.beacon_int);
1232 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1233 100 + vif->bss_conf.beacon_int);
1234
1235 if (WARN_ON_ONCE(vif->bss_conf.assoc))
1236 return;
1237
1238 mutex_lock(&mvm->mutex);
1239 /* Try really hard to protect the session and hear a beacon */
Johannes Berg016d27e2013-05-03 11:16:15 +02001240 iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001241 mutex_unlock(&mvm->mutex);
1242}
1243
David Spinadel35a000b2013-08-28 09:29:43 +03001244static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
1245 struct ieee80211_vif *vif,
1246 struct cfg80211_sched_scan_request *req,
1247 struct ieee80211_sched_scan_ies *ies)
1248{
1249 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1250 int ret;
1251
1252 mutex_lock(&mvm->mutex);
1253
1254 if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
1255 IWL_DEBUG_SCAN(mvm,
1256 "SCHED SCAN request during internal scan - abort\n");
1257 ret = -EBUSY;
1258 goto out;
1259 }
1260
1261 mvm->scan_status = IWL_MVM_SCAN_SCHED;
1262
1263 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1264 if (ret)
1265 goto err;
1266
1267 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1268 if (ret)
1269 goto err;
1270
1271 ret = iwl_mvm_sched_scan_start(mvm, req);
1272 if (!ret)
1273 goto out;
1274err:
1275 mvm->scan_status = IWL_MVM_SCAN_NONE;
1276out:
1277 mutex_unlock(&mvm->mutex);
1278 return ret;
1279}
1280
1281static void iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
1282 struct ieee80211_vif *vif)
1283{
1284 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1285
1286 mutex_lock(&mvm->mutex);
1287 iwl_mvm_sched_scan_stop(mvm);
1288 mutex_unlock(&mvm->mutex);
1289}
1290
Johannes Berg8ca151b2013-01-24 14:25:36 +01001291static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1292 enum set_key_cmd cmd,
1293 struct ieee80211_vif *vif,
1294 struct ieee80211_sta *sta,
1295 struct ieee80211_key_conf *key)
1296{
1297 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1298 int ret;
1299
1300 if (iwlwifi_mod_params.sw_crypto) {
1301 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1302 return -EOPNOTSUPP;
1303 }
1304
1305 switch (key->cipher) {
1306 case WLAN_CIPHER_SUITE_TKIP:
1307 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1308 /* fall-through */
1309 case WLAN_CIPHER_SUITE_CCMP:
1310 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1311 break;
1312 case WLAN_CIPHER_SUITE_AES_CMAC:
1313 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1314 break;
1315 case WLAN_CIPHER_SUITE_WEP40:
1316 case WLAN_CIPHER_SUITE_WEP104:
1317 /*
1318 * Support for TX only, at least for now, so accept
1319 * the key and do nothing else. Then mac80211 will
1320 * pass it for TX but we don't have to use it for RX.
1321 */
1322 return 0;
1323 default:
1324 return -EOPNOTSUPP;
1325 }
1326
1327 mutex_lock(&mvm->mutex);
1328
1329 switch (cmd) {
1330 case SET_KEY:
Johannes Berg5023d962013-07-31 14:07:43 +02001331 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1332 vif->type == NL80211_IFTYPE_AP) && !sta) {
1333 /*
1334 * GTK on AP interface is a TX-only key, return 0;
1335 * on IBSS they're per-station and because we're lazy
1336 * we don't support them for RX, so do the same.
1337 */
Johannes Berg6caffd42013-03-06 13:15:21 +01001338 ret = 0;
1339 key->hw_key_idx = STA_KEY_IDX_INVALID;
1340 break;
1341 }
1342
Johannes Berg8ca151b2013-01-24 14:25:36 +01001343 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1344 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1345 if (ret) {
1346 IWL_WARN(mvm, "set key failed\n");
1347 /*
1348 * can't add key for RX, but we don't need it
1349 * in the device for TX so still return 0
1350 */
Johannes Berg6caffd42013-03-06 13:15:21 +01001351 key->hw_key_idx = STA_KEY_IDX_INVALID;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001352 ret = 0;
1353 }
1354
1355 break;
1356 case DISABLE_KEY:
Johannes Berg6caffd42013-03-06 13:15:21 +01001357 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1358 ret = 0;
1359 break;
1360 }
1361
Johannes Berg8ca151b2013-01-24 14:25:36 +01001362 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1363 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1364 break;
1365 default:
1366 ret = -EINVAL;
1367 }
1368
1369 mutex_unlock(&mvm->mutex);
1370 return ret;
1371}
1372
1373static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1374 struct ieee80211_vif *vif,
1375 struct ieee80211_key_conf *keyconf,
1376 struct ieee80211_sta *sta,
1377 u32 iv32, u16 *phase1key)
1378{
1379 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1380
Johannes Berg5023d962013-07-31 14:07:43 +02001381 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1382 return;
1383
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1385}
1386
1387
1388static int iwl_mvm_roc(struct ieee80211_hw *hw,
1389 struct ieee80211_vif *vif,
1390 struct ieee80211_channel *channel,
Ilan Peerd339d5c2013-02-12 09:34:13 +02001391 int duration,
1392 enum ieee80211_roc_type type)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001393{
1394 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001395 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001396 struct cfg80211_chan_def chandef;
Ilan Peer31d385a2013-04-02 10:25:46 +03001397 struct iwl_mvm_phy_ctxt *phy_ctxt;
1398 int ret, i;
1399
1400 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
1401 duration, type);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001402
1403 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1404 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1405 return -EINVAL;
1406 }
1407
Johannes Berg8ca151b2013-01-24 14:25:36 +01001408 mutex_lock(&mvm->mutex);
1409
Ilan Peer31d385a2013-04-02 10:25:46 +03001410 for (i = 0; i < NUM_PHY_CTX; i++) {
1411 phy_ctxt = &mvm->phy_ctxts[i];
1412 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
1413 continue;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001414
Ilan Peer31d385a2013-04-02 10:25:46 +03001415 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
1416 /*
1417 * Unbind the P2P_DEVICE from the current PHY context,
1418 * and if the PHY context is not used remove it.
1419 */
1420 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1421 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1422 goto out_unlock;
1423
1424 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1425
1426 /* Bind the P2P_DEVICE to the current PHY Context */
1427 mvmvif->phy_ctxt = phy_ctxt;
1428
1429 ret = iwl_mvm_binding_add_vif(mvm, vif);
1430 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1431 goto out_unlock;
1432
1433 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1434 goto schedule_time_event;
1435 }
1436 }
1437
1438 /* Need to update the PHY context only if the ROC channel changed */
1439 if (channel == mvmvif->phy_ctxt->channel)
1440 goto schedule_time_event;
1441
1442 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1443
1444 /*
1445 * Change the PHY context configuration as it is currently referenced
1446 * only by the P2P Device MAC
1447 */
1448 if (mvmvif->phy_ctxt->ref == 1) {
1449 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
1450 &chandef, 1, 1);
1451 if (ret)
1452 goto out_unlock;
1453 } else {
1454 /*
1455 * The PHY context is shared with other MACs. Need to remove the
1456 * P2P Device from the binding, allocate an new PHY context and
1457 * create a new binding
1458 */
1459 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1460 if (!phy_ctxt) {
1461 ret = -ENOSPC;
1462 goto out_unlock;
1463 }
1464
1465 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
1466 1, 1);
1467 if (ret) {
1468 IWL_ERR(mvm, "Failed to change PHY context\n");
1469 goto out_unlock;
1470 }
1471
1472 /* Unbind the P2P_DEVICE from the current PHY context */
1473 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1474 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1475 goto out_unlock;
1476
1477 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1478
1479 /* Bind the P2P_DEVICE to the new allocated PHY context */
1480 mvmvif->phy_ctxt = phy_ctxt;
1481
1482 ret = iwl_mvm_binding_add_vif(mvm, vif);
1483 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1484 goto out_unlock;
1485
1486 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1487 }
1488
1489schedule_time_event:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001490 /* Schedule the time events */
Ilan Peere635c792013-02-13 11:05:18 +02001491 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001492
Ilan Peer31d385a2013-04-02 10:25:46 +03001493out_unlock:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001494 mutex_unlock(&mvm->mutex);
1495 IWL_DEBUG_MAC80211(mvm, "leave\n");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001496 return ret;
1497}
1498
1499static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
1500{
1501 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1502
1503 IWL_DEBUG_MAC80211(mvm, "enter\n");
1504
1505 mutex_lock(&mvm->mutex);
1506 iwl_mvm_stop_p2p_roc(mvm);
1507 mutex_unlock(&mvm->mutex);
1508
1509 IWL_DEBUG_MAC80211(mvm, "leave\n");
1510 return 0;
1511}
1512
1513static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
1514 struct ieee80211_chanctx_conf *ctx)
1515{
1516 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001517 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1518 struct iwl_mvm_phy_ctxt *phy_ctxt;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001519 int ret;
1520
Ilan Peer53a9d612013-04-28 11:55:08 +03001521 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001522
1523 mutex_lock(&mvm->mutex);
1524 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1525 if (!phy_ctxt) {
1526 ret = -ENOSPC;
1527 goto out;
1528 }
1529
Ilan Peer53a9d612013-04-28 11:55:08 +03001530 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1531 ctx->rx_chains_static,
1532 ctx->rx_chains_dynamic);
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001533 if (ret) {
1534 IWL_ERR(mvm, "Failed to add PHY context\n");
1535 goto out;
1536 }
1537
Ilan Peer53a9d612013-04-28 11:55:08 +03001538 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001539 *phy_ctxt_id = phy_ctxt->id;
1540out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001541 mutex_unlock(&mvm->mutex);
1542 return ret;
1543}
1544
1545static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
1546 struct ieee80211_chanctx_conf *ctx)
1547{
1548 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001549 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1550 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001551
1552 mutex_lock(&mvm->mutex);
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001553 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001554 mutex_unlock(&mvm->mutex);
1555}
1556
1557static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
1558 struct ieee80211_chanctx_conf *ctx,
1559 u32 changed)
1560{
1561 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001562 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1563 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001564
Ilan Peer31d385a2013-04-02 10:25:46 +03001565 if (WARN_ONCE((phy_ctxt->ref > 1) &&
1566 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
1567 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
1568 IEEE80211_CHANCTX_CHANGE_RADAR)),
1569 "Cannot change PHY. Ref=%d, changed=0x%X\n",
1570 phy_ctxt->ref, changed))
1571 return;
1572
Johannes Berg8ca151b2013-01-24 14:25:36 +01001573 mutex_lock(&mvm->mutex);
1574 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1575 ctx->rx_chains_static,
1576 ctx->rx_chains_dynamic);
Emmanuel Grumbach8e484f02013-10-02 15:02:25 +03001577 iwl_mvm_bt_coex_vif_change(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001578 mutex_unlock(&mvm->mutex);
1579}
1580
1581static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
1582 struct ieee80211_vif *vif,
1583 struct ieee80211_chanctx_conf *ctx)
1584{
1585 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001586 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1587 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001588 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1589 int ret;
1590
1591 mutex_lock(&mvm->mutex);
1592
Ilan Peerfe0f2de2013-03-21 10:23:52 +02001593 mvmvif->phy_ctxt = phy_ctxt;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001594
1595 switch (vif->type) {
1596 case NL80211_IFTYPE_AP:
Johannes Berg5023d962013-07-31 14:07:43 +02001597 case NL80211_IFTYPE_ADHOC:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001598 /*
1599 * The AP binding flow is handled as part of the start_ap flow
Johannes Berg5023d962013-07-31 14:07:43 +02001600 * (in bss_info_changed), similarly for IBSS.
Johannes Berg8ca151b2013-01-24 14:25:36 +01001601 */
1602 ret = 0;
1603 goto out_unlock;
1604 case NL80211_IFTYPE_STATION:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001605 case NL80211_IFTYPE_MONITOR:
1606 break;
1607 default:
1608 ret = -EINVAL;
1609 goto out_unlock;
1610 }
1611
1612 ret = iwl_mvm_binding_add_vif(mvm, vif);
1613 if (ret)
1614 goto out_unlock;
1615
1616 /*
1617 * Setting the quota at this stage is only required for monitor
1618 * interfaces. For the other types, the bss_info changed flow
1619 * will handle quota settings.
1620 */
1621 if (vif->type == NL80211_IFTYPE_MONITOR) {
Ilan Peer1e1391c2013-03-13 14:52:04 +02001622 mvmvif->monitor_active = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001623 ret = iwl_mvm_update_quotas(mvm, vif);
1624 if (ret)
1625 goto out_remove_binding;
1626 }
1627
1628 goto out_unlock;
1629
1630 out_remove_binding:
1631 iwl_mvm_binding_remove_vif(mvm, vif);
1632 out_unlock:
1633 mutex_unlock(&mvm->mutex);
1634 if (ret)
1635 mvmvif->phy_ctxt = NULL;
1636 return ret;
1637}
1638
1639static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
1640 struct ieee80211_vif *vif,
1641 struct ieee80211_chanctx_conf *ctx)
1642{
1643 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1644 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1645
1646 mutex_lock(&mvm->mutex);
1647
1648 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
1649
Johannes Berg8ca151b2013-01-24 14:25:36 +01001650 switch (vif->type) {
Johannes Berg5023d962013-07-31 14:07:43 +02001651 case NL80211_IFTYPE_AP:
1652 case NL80211_IFTYPE_ADHOC:
1653 goto out_unlock;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001654 case NL80211_IFTYPE_MONITOR:
Ilan Peer1e1391c2013-03-13 14:52:04 +02001655 mvmvif->monitor_active = false;
1656 iwl_mvm_update_quotas(mvm, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001657 break;
1658 default:
1659 break;
1660 }
1661
Ilan Peer1e1391c2013-03-13 14:52:04 +02001662 iwl_mvm_binding_remove_vif(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001663out_unlock:
1664 mvmvif->phy_ctxt = NULL;
1665 mutex_unlock(&mvm->mutex);
1666}
1667
1668static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
1669 struct ieee80211_sta *sta,
1670 bool set)
1671{
1672 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1673 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1674
1675 if (!mvm_sta || !mvm_sta->vif) {
1676 IWL_ERR(mvm, "Station is not associated to a vif\n");
1677 return -EINVAL;
1678 }
1679
1680 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
1681}
1682
David Spinadel507cadf2013-07-31 18:07:21 +03001683#ifdef CONFIG_NL80211_TESTMODE
1684static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
1685 [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
1686 [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
Johannes Bergf6c6ad422013-08-01 14:17:15 +02001687 [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
David Spinadel507cadf2013-07-31 18:07:21 +03001688};
1689
1690static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
1691 struct ieee80211_vif *vif,
1692 void *data, int len)
1693{
1694 struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
1695 int err;
1696 u32 noa_duration;
1697
1698 err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
1699 if (err)
1700 return err;
1701
1702 if (!tb[IWL_MVM_TM_ATTR_CMD])
1703 return -EINVAL;
1704
1705 switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
1706 case IWL_MVM_TM_CMD_SET_NOA:
1707 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
1708 !vif->bss_conf.enable_beacon ||
1709 !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
1710 return -EINVAL;
1711
1712 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
1713 if (noa_duration >= vif->bss_conf.beacon_int)
1714 return -EINVAL;
1715
1716 mvm->noa_duration = noa_duration;
1717 mvm->noa_vif = vif;
1718
1719 return iwl_mvm_update_quotas(mvm, NULL);
Johannes Bergf6c6ad422013-08-01 14:17:15 +02001720 case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
1721 /* must be associated client vif - ignore authorized */
1722 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
1723 !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
1724 !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
1725 return -EINVAL;
1726
1727 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
1728 return iwl_mvm_enable_beacon_filter(mvm, vif);
1729 return iwl_mvm_disable_beacon_filter(mvm, vif);
David Spinadel507cadf2013-07-31 18:07:21 +03001730 }
1731
1732 return -EOPNOTSUPP;
1733}
1734
1735static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
1736 struct ieee80211_vif *vif,
1737 void *data, int len)
1738{
1739 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1740 int err;
1741
1742 mutex_lock(&mvm->mutex);
1743 err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
1744 mutex_unlock(&mvm->mutex);
1745
1746 return err;
1747}
1748#endif
1749
Johannes Berg8ca151b2013-01-24 14:25:36 +01001750struct ieee80211_ops iwl_mvm_hw_ops = {
1751 .tx = iwl_mvm_mac_tx,
1752 .ampdu_action = iwl_mvm_mac_ampdu_action,
1753 .start = iwl_mvm_mac_start,
1754 .restart_complete = iwl_mvm_mac_restart_complete,
1755 .stop = iwl_mvm_mac_stop,
1756 .add_interface = iwl_mvm_mac_add_interface,
1757 .remove_interface = iwl_mvm_mac_remove_interface,
1758 .config = iwl_mvm_mac_config,
1759 .configure_filter = iwl_mvm_configure_filter,
1760 .bss_info_changed = iwl_mvm_bss_info_changed,
1761 .hw_scan = iwl_mvm_mac_hw_scan,
1762 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1763 .sta_state = iwl_mvm_mac_sta_state,
1764 .sta_notify = iwl_mvm_mac_sta_notify,
1765 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1766 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1767 .conf_tx = iwl_mvm_mac_conf_tx,
1768 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
David Spinadel35a000b2013-08-28 09:29:43 +03001769 .sched_scan_start = iwl_mvm_mac_sched_scan_start,
1770 .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001771 .set_key = iwl_mvm_mac_set_key,
1772 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
1773 .remain_on_channel = iwl_mvm_roc,
1774 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001775 .add_chanctx = iwl_mvm_add_chanctx,
1776 .remove_chanctx = iwl_mvm_remove_chanctx,
1777 .change_chanctx = iwl_mvm_change_chanctx,
1778 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
1779 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
1780
Johannes Berg5023d962013-07-31 14:07:43 +02001781 .start_ap = iwl_mvm_start_ap_ibss,
1782 .stop_ap = iwl_mvm_stop_ap_ibss,
1783 .join_ibss = iwl_mvm_start_ap_ibss,
1784 .leave_ibss = iwl_mvm_stop_ap_ibss,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001785
1786 .set_tim = iwl_mvm_set_tim,
1787
David Spinadel507cadf2013-07-31 18:07:21 +03001788 CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
1789
Johannes Berg8ca151b2013-01-24 14:25:36 +01001790#ifdef CONFIG_PM_SLEEP
1791 /* look at d3.c */
1792 .suspend = iwl_mvm_suspend,
1793 .resume = iwl_mvm_resume,
1794 .set_wakeup = iwl_mvm_set_wakeup,
1795 .set_rekey_data = iwl_mvm_set_rekey_data,
1796#if IS_ENABLED(CONFIG_IPV6)
1797 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1798#endif
1799 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1800#endif
1801};