iwlwifi: mvm: fix L2P BA ressources leak
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / mvm / mac80211.c
CommitLineData
8ca151b5
JB
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) 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
410dc5aa 25 * in the file called COPYING.
8ca151b5
JB
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) 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>
f0c2646a 68#include <linux/ip.h>
8ca151b5 69#include <net/mac80211.h>
f0c2646a 70#include <net/tcp.h>
8ca151b5
JB
71
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"
80
81static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
82 {
83 .max = 1,
8eb38710 84 .types = BIT(NL80211_IFTYPE_STATION),
8ca151b5 85 },
3c15a0fb
JB
86 {
87 .max = 1,
8eb38710
IP
88 .types = BIT(NL80211_IFTYPE_AP) |
89 BIT(NL80211_IFTYPE_P2P_CLIENT) |
3c15a0fb
JB
90 BIT(NL80211_IFTYPE_P2P_GO),
91 },
92 {
93 .max = 1,
94 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
95 },
8ca151b5
JB
96};
97
98static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
99 {
100 .num_different_channels = 1,
101 .max_interfaces = 3,
102 .limits = iwl_mvm_limits,
103 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
104 },
105};
106
f0c2646a
JB
107#ifdef CONFIG_PM_SLEEP
108static const struct nl80211_wowlan_tcp_data_token_feature
109iwl_mvm_wowlan_tcp_token_feature = {
110 .min_len = 0,
111 .max_len = 255,
112 .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
113};
114
115static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
116 .tok = &iwl_mvm_wowlan_tcp_token_feature,
117 .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
118 sizeof(struct ethhdr) -
119 sizeof(struct iphdr) -
120 sizeof(struct tcphdr),
121 .data_interval_max = 65535, /* __le16 in API */
122 .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
123 sizeof(struct ethhdr) -
124 sizeof(struct iphdr) -
125 sizeof(struct tcphdr),
126 .seq = true,
127};
128#endif
129
fe0f2de3
IP
130static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
131{
132 int i;
133
134 memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
135 for (i = 0; i < NUM_PHY_CTX; i++) {
136 mvm->phy_ctxts[i].id = i;
137 mvm->phy_ctxts[i].ref = 0;
138 }
139}
140
8ca151b5
JB
141int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
142{
143 struct ieee80211_hw *hw = mvm->hw;
831e85f3 144 int num_mac, ret, i;
8ca151b5
JB
145
146 /* Tell mac80211 our characteristics */
147 hw->flags = IEEE80211_HW_SIGNAL_DBM |
148 IEEE80211_HW_SPECTRUM_MGMT |
149 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
150 IEEE80211_HW_QUEUE_CONTROL |
151 IEEE80211_HW_WANT_MONITOR_VIF |
8ca151b5
JB
152 IEEE80211_HW_SUPPORTS_PS |
153 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
d2931bbd 154 IEEE80211_HW_AMPDU_AGGREGATION |
d64048ed
HG
155 IEEE80211_HW_TIMING_BEACON_ONLY |
156 IEEE80211_HW_CONNECTION_MONITOR;
8ca151b5 157
398e8c6c
IP
158 hw->queues = IWL_MVM_FIRST_AGG_QUEUE;
159 hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
8ca151b5
JB
160 hw->rate_control_algorithm = "iwl-mvm-rs";
161
162 /*
163 * Enable 11w if advertised by firmware and software crypto
164 * is not enabled (as the firmware will interpret some mgmt
165 * packets, so enabling it with software crypto isn't safe)
166 */
167 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
168 !iwlwifi_mod_params.sw_crypto)
169 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
170
171 hw->sta_data_size = sizeof(struct iwl_mvm_sta);
172 hw->vif_data_size = sizeof(struct iwl_mvm_vif);
fe0f2de3 173 hw->chanctx_data_size = sizeof(u16);
8ca151b5
JB
174
175 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
3c15a0fb
JB
176 BIT(NL80211_IFTYPE_P2P_CLIENT) |
177 BIT(NL80211_IFTYPE_AP) |
178 BIT(NL80211_IFTYPE_P2P_GO) |
179 BIT(NL80211_IFTYPE_P2P_DEVICE);
8ca151b5
JB
180
181 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
182 WIPHY_FLAG_DISABLE_BEACON_HINTS |
183 WIPHY_FLAG_IBSS_RSN;
184
185 hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
186 hw->wiphy->n_iface_combinations =
187 ARRAY_SIZE(iwl_mvm_iface_combinations);
188
c451e6d4 189 hw->wiphy->max_remain_on_channel_duration = 10000;
8ca151b5
JB
190 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
191
192 /* Extract MAC address */
193 memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
194 hw->wiphy->addresses = mvm->addresses;
195 hw->wiphy->n_addresses = 1;
831e85f3
IP
196
197 /* Extract additional MAC addresses if available */
198 num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
199 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
200
201 for (i = 1; i < num_mac; i++) {
202 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
8ca151b5 203 ETH_ALEN);
831e85f3 204 mvm->addresses[i].addr[5]++;
8ca151b5
JB
205 hw->wiphy->n_addresses++;
206 }
207
fe0f2de3
IP
208 iwl_mvm_reset_phy_ctxts(mvm);
209
8ca151b5
JB
210 /* we create the 802.11 header and a max-length SSID element */
211 hw->wiphy->max_scan_ie_len =
212 mvm->fw->ucode_capa.max_probe_length - 24 - 34;
213 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
214
215 if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
216 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
217 &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
218 if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
219 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
220 &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
221
222 hw->wiphy->hw_version = mvm->trans->hw_id;
223
ade50652 224 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
8ca151b5
JB
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->features |= NL80211_FEATURE_P2P_GO_CTWIN |
230 NL80211_FEATURE_P2P_GO_OPPPS;
231
232 mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
233
234#ifdef CONFIG_PM_SLEEP
235 if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
236 mvm->trans->ops->d3_suspend &&
237 mvm->trans->ops->d3_resume &&
238 device_can_wakeup(mvm->trans->dev)) {
964dc9e2
JB
239 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
240 WIPHY_WOWLAN_DISCONNECT |
241 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
242 WIPHY_WOWLAN_RFKILL_RELEASE;
8ca151b5 243 if (!iwlwifi_mod_params.sw_crypto)
964dc9e2
JB
244 mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
245 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
246 WIPHY_WOWLAN_4WAY_HANDSHAKE;
247
248 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
249 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
250 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
251 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
252 hw->wiphy->wowlan = &mvm->wowlan;
8ca151b5
JB
253 }
254#endif
255
256 ret = iwl_mvm_leds_init(mvm);
257 if (ret)
258 return ret;
259
b7327d89
EG
260 ret = ieee80211_register_hw(mvm->hw);
261 if (ret)
262 iwl_mvm_leds_exit(mvm);
263
264 return ret;
8ca151b5
JB
265}
266
267static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
268 struct ieee80211_tx_control *control,
269 struct sk_buff *skb)
270{
271 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
272
9ee718aa
EL
273 if (iwl_mvm_is_radio_killed(mvm)) {
274 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
8ca151b5
JB
275 goto drop;
276 }
277
398e8c6c 278 if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
8ca151b5
JB
279 !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
280 goto drop;
281
282 if (control->sta) {
283 if (iwl_mvm_tx_skb(mvm, skb, control->sta))
284 goto drop;
285 return;
286 }
287
288 if (iwl_mvm_tx_skb_non_sta(mvm, skb))
289 goto drop;
290 return;
291 drop:
292 ieee80211_free_txskb(hw, skb);
293}
294
295static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
296 struct ieee80211_vif *vif,
297 enum ieee80211_ampdu_mlme_action action,
298 struct ieee80211_sta *sta, u16 tid,
299 u16 *ssn, u8 buf_size)
300{
301 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
302 int ret;
303
304 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
305 sta->addr, tid, action);
306
307 if (!(mvm->nvm_data->sku_cap_11n_enable))
308 return -EACCES;
309
310 mutex_lock(&mvm->mutex);
311
312 switch (action) {
313 case IEEE80211_AMPDU_RX_START:
314 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG) {
315 ret = -EINVAL;
316 break;
317 }
318 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
319 break;
320 case IEEE80211_AMPDU_RX_STOP:
321 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
322 break;
323 case IEEE80211_AMPDU_TX_START:
5d158efa
EG
324 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG) {
325 ret = -EINVAL;
326 break;
327 }
8ca151b5
JB
328 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
329 break;
330 case IEEE80211_AMPDU_TX_STOP_CONT:
e3d9e7ce
EG
331 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
332 break;
8ca151b5
JB
333 case IEEE80211_AMPDU_TX_STOP_FLUSH:
334 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
e3d9e7ce 335 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
8ca151b5
JB
336 break;
337 case IEEE80211_AMPDU_TX_OPERATIONAL:
338 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
339 break;
340 default:
341 WARN_ON_ONCE(1);
342 ret = -EINVAL;
343 break;
344 }
345 mutex_unlock(&mvm->mutex);
346
347 return ret;
348}
349
350static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
351 struct ieee80211_vif *vif)
352{
353 struct iwl_mvm *mvm = data;
354 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
355
356 mvmvif->uploaded = false;
357 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
358
359 /* does this make sense at all? */
360 mvmvif->color++;
361
362 spin_lock_bh(&mvm->time_event_lock);
363 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
364 spin_unlock_bh(&mvm->time_event_lock);
365
fe0f2de3 366 mvmvif->phy_ctxt = NULL;
8ca151b5
JB
367}
368
369static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
370{
371 iwl_trans_stop_device(mvm->trans);
372 iwl_trans_stop_hw(mvm->trans, false);
373
374 mvm->scan_status = IWL_MVM_SCAN_NONE;
375
376 /* just in case one was running */
377 ieee80211_remain_on_channel_expired(mvm->hw);
378
379 ieee80211_iterate_active_interfaces_atomic(
380 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
381 iwl_mvm_cleanup_iterator, mvm);
382
fe0f2de3
IP
383 mvm->p2p_device_vif = NULL;
384
385 iwl_mvm_reset_phy_ctxts(mvm);
8ca151b5
JB
386 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
387 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
388
389 ieee80211_wake_queues(mvm->hw);
390
391 mvm->vif_count = 0;
392}
393
394static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
395{
396 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
397 int ret;
398
399 mutex_lock(&mvm->mutex);
400
401 /* Clean up some internal and mac80211 state on restart */
402 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
403 iwl_mvm_restart_cleanup(mvm);
404
405 ret = iwl_mvm_up(mvm);
406 mutex_unlock(&mvm->mutex);
407
408 return ret;
409}
410
411static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
412{
413 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
414 int ret;
415
416 mutex_lock(&mvm->mutex);
417
418 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
419 ret = iwl_mvm_update_quotas(mvm, NULL);
420 if (ret)
421 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
422 ret);
423
424 mutex_unlock(&mvm->mutex);
425}
426
427static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
428{
429 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
430
431 flush_work(&mvm->async_handlers_wk);
432
433 mutex_lock(&mvm->mutex);
434 /* async_handlers_wk is now blocked */
435
436 /*
437 * The work item could be running or queued if the
438 * ROC time event stops just as we get here.
439 */
440 cancel_work_sync(&mvm->roc_done_wk);
441
442 iwl_trans_stop_device(mvm->trans);
443 iwl_trans_stop_hw(mvm->trans, false);
444
445 iwl_mvm_async_handlers_purge(mvm);
446 /* async_handlers_list is empty and will stay empty: HW is stopped */
447
448 /* the fw is stopped, the aux sta is dead: clean up driver state */
449 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
450
451 mutex_unlock(&mvm->mutex);
452
453 /*
454 * The worker might have been waiting for the mutex, let it run and
455 * discover that its list is now empty.
456 */
457 cancel_work_sync(&mvm->async_handlers_wk);
458}
459
460static void iwl_mvm_pm_disable_iterator(void *data, u8 *mac,
461 struct ieee80211_vif *vif)
462{
463 struct iwl_mvm *mvm = data;
464 int ret;
465
466 ret = iwl_mvm_power_disable(mvm, vif);
467 if (ret)
468 IWL_ERR(mvm, "failed to disable power management\n");
469}
470
471static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
472 struct ieee80211_vif *vif)
473{
474 struct iwl_mvm *mvm = data;
475
476 iwl_mvm_power_update_mode(mvm, vif);
477}
478
fe0f2de3
IP
479static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
480{
481 u16 i;
482
483 lockdep_assert_held(&mvm->mutex);
484
485 for (i = 0; i < NUM_PHY_CTX; i++)
486 if (!mvm->phy_ctxts[i].ref)
487 return &mvm->phy_ctxts[i];
488
489 IWL_ERR(mvm, "No available PHY context\n");
490 return NULL;
491}
492
8ca151b5
JB
493static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
494 struct ieee80211_vif *vif)
495{
496 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
497 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
498 int ret;
499
500 /*
501 * Not much to do here. The stack will not allow interface
502 * types or combinations that we didn't advertise, so we
503 * don't really have to check the types.
504 */
505
506 mutex_lock(&mvm->mutex);
507
508 /* Allocate resources for the MAC context, and add it the the fw */
509 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
510 if (ret)
511 goto out_unlock;
512
513 /*
514 * The AP binding flow can be done only after the beacon
515 * template is configured (which happens only in the mac80211
516 * start_ap() flow), and adding the broadcast station can happen
517 * only after the binding.
518 * In addition, since modifying the MAC before adding a bcast
519 * station is not allowed by the FW, delay the adding of MAC context to
520 * the point where we can also add the bcast station.
521 * In short: there's not much we can do at this point, other than
522 * allocating resources :)
523 */
524 if (vif->type == NL80211_IFTYPE_AP) {
525 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
526 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
527 qmask);
528 if (ret) {
529 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
530 goto out_release;
531 }
532
533 goto out_unlock;
534 }
535
536 /*
537 * TODO: remove this temporary code.
538 * Currently MVM FW supports power management only on single MAC.
5ee2b215
AB
539 * If new interface added, disable PM on existing interface.
540 * P2P device is a special case, since it is handled by FW similary to
541 * scan. If P2P deviced is added, PM remains enabled on existing
542 * interface.
8ca151b5
JB
543 * Note: the method below does not count the new interface being added
544 * at this moment.
545 */
5ee2b215
AB
546 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
547 mvm->vif_count++;
8ca151b5
JB
548 if (mvm->vif_count > 1) {
549 IWL_DEBUG_MAC80211(mvm,
550 "Disable power on existing interfaces\n");
5360cfb2 551 ieee80211_iterate_active_interfaces_atomic(
8ca151b5
JB
552 mvm->hw,
553 IEEE80211_IFACE_ITER_NORMAL,
554 iwl_mvm_pm_disable_iterator, mvm);
555 }
556
557 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
558 if (ret)
559 goto out_release;
560
561 /*
562 * Update power state on the new interface. Admittedly, based on
563 * mac80211 logics this power update will disable power management
564 */
565 iwl_mvm_power_update_mode(mvm, vif);
566
7df15b1e
HG
567 /* beacon filtering */
568 if (!mvm->bf_allowed_vif &&
569 vif->type == NL80211_IFTYPE_STATION && !vif->p2p){
570 mvm->bf_allowed_vif = mvmvif;
571 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
572 }
573
574 ret = iwl_mvm_disable_beacon_filter(mvm, vif);
575 if (ret)
576 goto out_release;
577
8ca151b5
JB
578 /*
579 * P2P_DEVICE interface does not have a channel context assigned to it,
580 * so a dedicated PHY context is allocated to it and the corresponding
581 * MAC context is bound to it at this stage.
582 */
583 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
8ca151b5 584
fe0f2de3
IP
585 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
586 if (!mvmvif->phy_ctxt) {
587 ret = -ENOSPC;
588 goto out_remove_mac;
589 }
8ca151b5 590
53a9d61e 591 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
8ca151b5
JB
592 ret = iwl_mvm_binding_add_vif(mvm, vif);
593 if (ret)
53a9d61e 594 goto out_unref_phy;
8ca151b5
JB
595
596 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
597 if (ret)
598 goto out_unbind;
599
600 /* Save a pointer to p2p device vif, so it can later be used to
601 * update the p2p device MAC when a GO is started/stopped */
602 mvm->p2p_device_vif = vif;
603 }
604
63494374 605 iwl_mvm_vif_dbgfs_register(mvm, vif);
8ca151b5
JB
606 goto out_unlock;
607
608 out_unbind:
609 iwl_mvm_binding_remove_vif(mvm, vif);
53a9d61e 610 out_unref_phy:
fe0f2de3 611 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
8ca151b5
JB
612 out_remove_mac:
613 mvmvif->phy_ctxt = NULL;
614 iwl_mvm_mac_ctxt_remove(mvm, vif);
615 out_release:
5ee2b215
AB
616 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
617 mvm->vif_count--;
4bf881f5
AB
618 ieee80211_iterate_active_interfaces(
619 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
620 iwl_mvm_power_update_iterator, mvm);
8ca151b5
JB
621 iwl_mvm_mac_ctxt_release(mvm, vif);
622 out_unlock:
623 mutex_unlock(&mvm->mutex);
624
625 return ret;
626}
627
38a12b5b
JB
628static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
629 struct ieee80211_vif *vif)
8ca151b5 630{
8ca151b5
JB
631 u32 tfd_msk = 0, ac;
632
633 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
634 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
635 tfd_msk |= BIT(vif->hw_queue[ac]);
636
637 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
638 tfd_msk |= BIT(vif->cab_queue);
639
640 if (tfd_msk) {
641 mutex_lock(&mvm->mutex);
642 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
643 mutex_unlock(&mvm->mutex);
644 }
645
646 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
647 /*
648 * Flush the ROC worker which will flush the OFFCHANNEL queue.
649 * We assume here that all the packets sent to the OFFCHANNEL
650 * queue are sent in ROC session.
651 */
652 flush_work(&mvm->roc_done_wk);
653 } else {
654 /*
655 * By now, all the AC queues are empty. The AGG queues are
656 * empty too. We already got all the Tx responses for all the
657 * packets in the queues. The drain work can have been
0742a75a 658 * triggered. Flush it.
8ca151b5
JB
659 */
660 flush_work(&mvm->sta_drained_wk);
661 }
38a12b5b
JB
662}
663
664static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
665 struct ieee80211_vif *vif)
666{
667 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
668 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
669
670 iwl_mvm_prepare_mac_removal(mvm, vif);
8ca151b5
JB
671
672 mutex_lock(&mvm->mutex);
673
7df15b1e
HG
674 if (mvm->bf_allowed_vif == mvmvif) {
675 mvm->bf_allowed_vif = NULL;
676 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
677 }
678
63494374
JB
679 iwl_mvm_vif_dbgfs_clean(mvm, vif);
680
8ca151b5
JB
681 /*
682 * For AP/GO interface, the tear down of the resources allocated to the
38a12b5b 683 * interface is be handled as part of the stop_ap flow.
8ca151b5
JB
684 */
685 if (vif->type == NL80211_IFTYPE_AP) {
686 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
687 goto out_release;
688 }
689
690 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
691 mvm->p2p_device_vif = NULL;
692 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
693 iwl_mvm_binding_remove_vif(mvm, vif);
fe0f2de3 694 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
8ca151b5
JB
695 mvmvif->phy_ctxt = NULL;
696 }
697
698 /*
699 * TODO: remove this temporary code.
700 * Currently MVM FW supports power management only on single MAC.
701 * Check if only one additional interface remains after removing
702 * current one. Update power mode on the remaining interface.
703 */
5ee2b215 704 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
8ca151b5
JB
705 mvm->vif_count--;
706 IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
707 mvm->vif_count);
708 if (mvm->vif_count == 1) {
709 ieee80211_iterate_active_interfaces(
710 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
711 iwl_mvm_power_update_iterator, mvm);
712 }
713
714 iwl_mvm_mac_ctxt_remove(mvm, vif);
715
716out_release:
717 iwl_mvm_mac_ctxt_release(mvm, vif);
718 mutex_unlock(&mvm->mutex);
719}
720
721static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
722{
723 return 0;
724}
725
726static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
727 unsigned int changed_flags,
728 unsigned int *total_flags,
729 u64 multicast)
730{
731 *total_flags = 0;
732}
733
51b6b9e0
EG
734static int iwl_mvm_configure_mcast_filter(struct iwl_mvm *mvm,
735 struct ieee80211_vif *vif)
736{
737 struct iwl_mcast_filter_cmd mcast_filter_cmd = {
738 .pass_all = 1,
739 };
740
741 memcpy(mcast_filter_cmd.bssid, vif->bss_conf.bssid, ETH_ALEN);
742
743 return iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC,
744 sizeof(mcast_filter_cmd),
745 &mcast_filter_cmd);
746}
747
8ca151b5
JB
748static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
749 struct ieee80211_vif *vif,
750 struct ieee80211_bss_conf *bss_conf,
751 u32 changes)
752{
753 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
754 int ret;
755
756 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
757 if (ret)
758 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
759
760 if (changes & BSS_CHANGED_ASSOC) {
761 if (bss_conf->assoc) {
762 /* add quota for this interface */
763 ret = iwl_mvm_update_quotas(mvm, vif);
764 if (ret) {
765 IWL_ERR(mvm, "failed to update quotas\n");
766 return;
767 }
9166b1ee 768 iwl_mvm_bt_coex_vif_assoc(mvm, vif);
51b6b9e0 769 iwl_mvm_configure_mcast_filter(mvm, vif);
8ca151b5
JB
770 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
771 /* remove AP station now that the MAC is unassoc */
772 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
773 if (ret)
774 IWL_ERR(mvm, "failed to remove AP station\n");
775 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
776 /* remove quota for this interface */
777 ret = iwl_mvm_update_quotas(mvm, NULL);
778 if (ret)
779 IWL_ERR(mvm, "failed to update quotas\n");
780 }
5ec42ecf
AB
781 ret = iwl_mvm_power_update_mode(mvm, vif);
782 if (ret)
783 IWL_ERR(mvm, "failed to update power mode\n");
989c6505 784 } else if (changes & BSS_CHANGED_BEACON_INFO) {
210a544e
JB
785 /*
786 * We received a beacon _after_ association so
787 * remove the session protection.
788 */
789 iwl_mvm_remove_time_event(mvm, mvmvif,
790 &mvmvif->time_event_data);
8ca151b5 791 } else if (changes & BSS_CHANGED_PS) {
4bf881f5
AB
792 ret = iwl_mvm_power_update_mode(mvm, vif);
793 if (ret)
794 IWL_ERR(mvm, "failed to update power mode\n");
8ca151b5
JB
795 }
796}
797
798static int iwl_mvm_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
799{
800 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
801 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
802 int ret;
803
804 mutex_lock(&mvm->mutex);
805
806 /* Send the beacon template */
807 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
808 if (ret)
809 goto out_unlock;
810
811 /* Add the mac context */
812 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
813 if (ret)
814 goto out_unlock;
815
816 /* Perform the binding */
817 ret = iwl_mvm_binding_add_vif(mvm, vif);
818 if (ret)
819 goto out_remove;
820
821 mvmvif->ap_active = true;
822
823 /* Send the bcast station. At this stage the TBTT and DTIM time events
824 * are added and applied to the scheduler */
825 ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
826 if (ret)
827 goto out_unbind;
828
829 ret = iwl_mvm_update_quotas(mvm, vif);
830 if (ret)
831 goto out_rm_bcast;
832
833 /* Need to update the P2P Device MAC */
834 if (vif->p2p && mvm->p2p_device_vif)
835 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
836
837 mutex_unlock(&mvm->mutex);
838 return 0;
839
840out_rm_bcast:
841 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
842out_unbind:
843 iwl_mvm_binding_remove_vif(mvm, vif);
844out_remove:
845 iwl_mvm_mac_ctxt_remove(mvm, vif);
846out_unlock:
847 mutex_unlock(&mvm->mutex);
848 return ret;
849}
850
851static void iwl_mvm_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
852{
853 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
854 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
855
38a12b5b
JB
856 iwl_mvm_prepare_mac_removal(mvm, vif);
857
8ca151b5
JB
858 mutex_lock(&mvm->mutex);
859
860 mvmvif->ap_active = false;
861
862 /* Need to update the P2P Device MAC */
863 if (vif->p2p && mvm->p2p_device_vif)
864 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
865
866 iwl_mvm_update_quotas(mvm, NULL);
867 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
868 iwl_mvm_binding_remove_vif(mvm, vif);
869 iwl_mvm_mac_ctxt_remove(mvm, vif);
870
871 mutex_unlock(&mvm->mutex);
872}
873
874static void iwl_mvm_bss_info_changed_ap(struct iwl_mvm *mvm,
875 struct ieee80211_vif *vif,
876 struct ieee80211_bss_conf *bss_conf,
877 u32 changes)
878{
879 /* Need to send a new beacon template to the FW */
880 if (changes & BSS_CHANGED_BEACON) {
881 if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
882 IWL_WARN(mvm, "Failed updating beacon data\n");
883 }
884}
885
886static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
887 struct ieee80211_vif *vif,
888 struct ieee80211_bss_conf *bss_conf,
889 u32 changes)
890{
891 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
892
893 mutex_lock(&mvm->mutex);
894
895 switch (vif->type) {
896 case NL80211_IFTYPE_STATION:
897 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
898 break;
899 case NL80211_IFTYPE_AP:
900 iwl_mvm_bss_info_changed_ap(mvm, vif, bss_conf, changes);
901 break;
902 default:
903 /* shouldn't happen */
904 WARN_ON_ONCE(1);
905 }
906
907 mutex_unlock(&mvm->mutex);
908}
909
910static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
911 struct ieee80211_vif *vif,
912 struct cfg80211_scan_request *req)
913{
914 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
915 int ret;
916
917 if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
918 return -EINVAL;
919
920 mutex_lock(&mvm->mutex);
921
922 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
923 ret = iwl_mvm_scan_request(mvm, vif, req);
924 else
925 ret = -EBUSY;
926
927 mutex_unlock(&mvm->mutex);
928
929 return ret;
930}
931
932static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
933 struct ieee80211_vif *vif)
934{
935 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
936
937 mutex_lock(&mvm->mutex);
938
939 iwl_mvm_cancel_scan(mvm);
940
941 mutex_unlock(&mvm->mutex);
942}
943
944static void
945iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
946 struct ieee80211_sta *sta, u16 tid,
947 int num_frames,
948 enum ieee80211_frame_release_type reason,
949 bool more_data)
950{
951 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
8ca151b5
JB
952
953 /* TODO: how do we tell the fw to send frames for a specific TID */
954
955 /*
956 * The fw will send EOSP notification when the last frame will be
957 * transmitted.
958 */
9cc40712 959 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
8ca151b5
JB
960}
961
962static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
963 struct ieee80211_vif *vif,
964 enum sta_notify_cmd cmd,
965 struct ieee80211_sta *sta)
966{
967 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
968 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
969
970 switch (cmd) {
971 case STA_NOTIFY_SLEEP:
e3d4bc8c 972 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
8ca151b5
JB
973 ieee80211_sta_block_awake(hw, sta, true);
974 /*
975 * The fw updates the STA to be asleep. Tx packets on the Tx
976 * queues to this station will not be transmitted. The fw will
977 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
978 */
979 break;
980 case STA_NOTIFY_AWAKE:
881acd89 981 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
8ca151b5 982 break;
9cc40712 983 iwl_mvm_sta_modify_ps_wake(mvm, sta);
8ca151b5
JB
984 break;
985 default:
986 break;
987 }
988}
989
990static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
991 struct ieee80211_vif *vif,
992 struct ieee80211_sta *sta,
993 enum ieee80211_sta_state old_state,
994 enum ieee80211_sta_state new_state)
995{
996 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
997 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
998 int ret;
999
1000 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1001 sta->addr, old_state, new_state);
1002
1003 /* this would be a mac80211 bug ... but don't crash */
1004 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1005 return -EINVAL;
1006
1007 /* if a STA is being removed, reuse its ID */
1008 flush_work(&mvm->sta_drained_wk);
1009
1010 mutex_lock(&mvm->mutex);
1011 if (old_state == IEEE80211_STA_NOTEXIST &&
1012 new_state == IEEE80211_STA_NONE) {
1013 ret = iwl_mvm_add_sta(mvm, vif, sta);
1014 } else if (old_state == IEEE80211_STA_NONE &&
1015 new_state == IEEE80211_STA_AUTH) {
1016 ret = 0;
1017 } else if (old_state == IEEE80211_STA_AUTH &&
1018 new_state == IEEE80211_STA_ASSOC) {
7a453973
JB
1019 ret = iwl_mvm_update_sta(mvm, vif, sta);
1020 if (ret == 0)
1021 iwl_mvm_rs_rate_init(mvm, sta,
1022 mvmvif->phy_ctxt->channel->band);
8ca151b5
JB
1023 } else if (old_state == IEEE80211_STA_ASSOC &&
1024 new_state == IEEE80211_STA_AUTHORIZED) {
7df15b1e
HG
1025 /* enable beacon filtering */
1026 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
8ca151b5
JB
1027 ret = 0;
1028 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1029 new_state == IEEE80211_STA_ASSOC) {
7df15b1e
HG
1030 /* disable beacon filtering */
1031 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif));
8ca151b5
JB
1032 ret = 0;
1033 } else if (old_state == IEEE80211_STA_ASSOC &&
1034 new_state == IEEE80211_STA_AUTH) {
1035 ret = 0;
1036 } else if (old_state == IEEE80211_STA_AUTH &&
1037 new_state == IEEE80211_STA_NONE) {
1038 ret = 0;
1039 } else if (old_state == IEEE80211_STA_NONE &&
1040 new_state == IEEE80211_STA_NOTEXIST) {
1041 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1042 } else {
1043 ret = -EIO;
1044 }
1045 mutex_unlock(&mvm->mutex);
1046
1047 return ret;
1048}
1049
1050static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1051{
1052 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1053
1054 mvm->rts_threshold = value;
1055
1056 return 0;
1057}
1058
1059static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1060 struct ieee80211_vif *vif, u16 ac,
1061 const struct ieee80211_tx_queue_params *params)
1062{
1063 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1064 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1065
1066 mvmvif->queue_params[ac] = *params;
1067
1068 /*
1069 * No need to update right away, we'll get BSS_CHANGED_QOS
1070 * The exception is P2P_DEVICE interface which needs immediate update.
1071 */
1072 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1073 int ret;
1074
1075 mutex_lock(&mvm->mutex);
1076 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1077 mutex_unlock(&mvm->mutex);
1078 return ret;
1079 }
1080 return 0;
1081}
1082
1083static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1084 struct ieee80211_vif *vif)
1085{
1086 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1087 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1088 200 + vif->bss_conf.beacon_int);
1089 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1090 100 + vif->bss_conf.beacon_int);
1091
1092 if (WARN_ON_ONCE(vif->bss_conf.assoc))
1093 return;
1094
1095 mutex_lock(&mvm->mutex);
1096 /* Try really hard to protect the session and hear a beacon */
1097 iwl_mvm_protect_session(mvm, vif, duration, min_duration);
1098 mutex_unlock(&mvm->mutex);
1099}
1100
1101static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1102 enum set_key_cmd cmd,
1103 struct ieee80211_vif *vif,
1104 struct ieee80211_sta *sta,
1105 struct ieee80211_key_conf *key)
1106{
1107 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1108 int ret;
1109
1110 if (iwlwifi_mod_params.sw_crypto) {
1111 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1112 return -EOPNOTSUPP;
1113 }
1114
1115 switch (key->cipher) {
1116 case WLAN_CIPHER_SUITE_TKIP:
1117 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1118 /* fall-through */
1119 case WLAN_CIPHER_SUITE_CCMP:
1120 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1121 break;
1122 case WLAN_CIPHER_SUITE_AES_CMAC:
1123 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1124 break;
1125 case WLAN_CIPHER_SUITE_WEP40:
1126 case WLAN_CIPHER_SUITE_WEP104:
1127 /*
1128 * Support for TX only, at least for now, so accept
1129 * the key and do nothing else. Then mac80211 will
1130 * pass it for TX but we don't have to use it for RX.
1131 */
1132 return 0;
1133 default:
1134 return -EOPNOTSUPP;
1135 }
1136
1137 mutex_lock(&mvm->mutex);
1138
1139 switch (cmd) {
1140 case SET_KEY:
6caffd4f
JB
1141 if (vif->type == NL80211_IFTYPE_AP && !sta) {
1142 /* GTK on AP interface is a TX-only key, return 0 */
1143 ret = 0;
1144 key->hw_key_idx = STA_KEY_IDX_INVALID;
1145 break;
1146 }
1147
8ca151b5
JB
1148 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1149 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1150 if (ret) {
1151 IWL_WARN(mvm, "set key failed\n");
1152 /*
1153 * can't add key for RX, but we don't need it
1154 * in the device for TX so still return 0
1155 */
6caffd4f 1156 key->hw_key_idx = STA_KEY_IDX_INVALID;
8ca151b5
JB
1157 ret = 0;
1158 }
1159
1160 break;
1161 case DISABLE_KEY:
6caffd4f
JB
1162 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1163 ret = 0;
1164 break;
1165 }
1166
8ca151b5
JB
1167 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1168 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1169 break;
1170 default:
1171 ret = -EINVAL;
1172 }
1173
1174 mutex_unlock(&mvm->mutex);
1175 return ret;
1176}
1177
1178static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1179 struct ieee80211_vif *vif,
1180 struct ieee80211_key_conf *keyconf,
1181 struct ieee80211_sta *sta,
1182 u32 iv32, u16 *phase1key)
1183{
1184 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1185
1186 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1187}
1188
1189
1190static int iwl_mvm_roc(struct ieee80211_hw *hw,
1191 struct ieee80211_vif *vif,
1192 struct ieee80211_channel *channel,
d339d5ca
IP
1193 int duration,
1194 enum ieee80211_roc_type type)
8ca151b5
JB
1195{
1196 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3 1197 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5 1198 struct cfg80211_chan_def chandef;
31d385ae
IP
1199 struct iwl_mvm_phy_ctxt *phy_ctxt;
1200 int ret, i;
1201
1202 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
1203 duration, type);
8ca151b5
JB
1204
1205 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1206 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1207 return -EINVAL;
1208 }
1209
8ca151b5
JB
1210 mutex_lock(&mvm->mutex);
1211
31d385ae
IP
1212 for (i = 0; i < NUM_PHY_CTX; i++) {
1213 phy_ctxt = &mvm->phy_ctxts[i];
1214 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
1215 continue;
1216
1217 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
1218 /*
1219 * Unbind the P2P_DEVICE from the current PHY context,
1220 * and if the PHY context is not used remove it.
1221 */
1222 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1223 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1224 goto out_unlock;
1225
1226 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1227
1228 /* Bind the P2P_DEVICE to the current PHY Context */
1229 mvmvif->phy_ctxt = phy_ctxt;
1230
1231 ret = iwl_mvm_binding_add_vif(mvm, vif);
1232 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1233 goto out_unlock;
1234
1235 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1236 goto schedule_time_event;
1237 }
1238 }
1239
1240 /* Need to update the PHY context only if the ROC channel changed */
1241 if (channel == mvmvif->phy_ctxt->channel)
1242 goto schedule_time_event;
1243
8ca151b5 1244 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
8ca151b5 1245
31d385ae
IP
1246 /*
1247 * Change the PHY context configuration as it is currently referenced
1248 * only by the P2P Device MAC
1249 */
1250 if (mvmvif->phy_ctxt->ref == 1) {
1251 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
1252 &chandef, 1, 1);
1253 if (ret)
1254 goto out_unlock;
1255 } else {
1256 /*
1257 * The PHY context is shared with other MACs. Need to remove the
1258 * P2P Device from the binding, allocate an new PHY context and
1259 * create a new binding
1260 */
1261 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1262 if (!phy_ctxt) {
1263 ret = -ENOSPC;
1264 goto out_unlock;
1265 }
1266
1267 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
1268 1, 1);
1269 if (ret) {
1270 IWL_ERR(mvm, "Failed to change PHY context\n");
1271 goto out_unlock;
1272 }
1273
1274 /* Unbind the P2P_DEVICE from the current PHY context */
1275 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1276 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1277 goto out_unlock;
1278
1279 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1280
1281 /* Bind the P2P_DEVICE to the new allocated PHY context */
1282 mvmvif->phy_ctxt = phy_ctxt;
1283
1284 ret = iwl_mvm_binding_add_vif(mvm, vif);
1285 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1286 goto out_unlock;
1287
1288 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1289 }
1290
1291schedule_time_event:
8ca151b5 1292 /* Schedule the time events */
e635c797 1293 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
8ca151b5 1294
31d385ae 1295out_unlock:
8ca151b5
JB
1296 mutex_unlock(&mvm->mutex);
1297 IWL_DEBUG_MAC80211(mvm, "leave\n");
8ca151b5
JB
1298 return ret;
1299}
1300
1301static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
1302{
1303 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1304
1305 IWL_DEBUG_MAC80211(mvm, "enter\n");
1306
1307 mutex_lock(&mvm->mutex);
1308 iwl_mvm_stop_p2p_roc(mvm);
1309 mutex_unlock(&mvm->mutex);
1310
1311 IWL_DEBUG_MAC80211(mvm, "leave\n");
1312 return 0;
1313}
1314
1315static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
1316 struct ieee80211_chanctx_conf *ctx)
1317{
1318 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3
IP
1319 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1320 struct iwl_mvm_phy_ctxt *phy_ctxt;
8ca151b5
JB
1321 int ret;
1322
53a9d61e 1323 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
fe0f2de3 1324
8ca151b5 1325 mutex_lock(&mvm->mutex);
fe0f2de3
IP
1326 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1327 if (!phy_ctxt) {
1328 ret = -ENOSPC;
1329 goto out;
1330 }
8ca151b5 1331
53a9d61e
IP
1332 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1333 ctx->rx_chains_static,
1334 ctx->rx_chains_dynamic);
fe0f2de3
IP
1335 if (ret) {
1336 IWL_ERR(mvm, "Failed to add PHY context\n");
1337 goto out;
1338 }
1339
53a9d61e 1340 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
fe0f2de3
IP
1341 *phy_ctxt_id = phy_ctxt->id;
1342out:
8ca151b5
JB
1343 mutex_unlock(&mvm->mutex);
1344 return ret;
1345}
1346
1347static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
1348 struct ieee80211_chanctx_conf *ctx)
1349{
1350 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3
IP
1351 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1352 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
8ca151b5
JB
1353
1354 mutex_lock(&mvm->mutex);
fe0f2de3 1355 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
8ca151b5
JB
1356 mutex_unlock(&mvm->mutex);
1357}
1358
1359static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
1360 struct ieee80211_chanctx_conf *ctx,
1361 u32 changed)
1362{
1363 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3
IP
1364 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1365 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
8ca151b5 1366
31d385ae
IP
1367 if (WARN_ONCE((phy_ctxt->ref > 1) &&
1368 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
1369 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
1370 IEEE80211_CHANCTX_CHANGE_RADAR)),
1371 "Cannot change PHY. Ref=%d, changed=0x%X\n",
1372 phy_ctxt->ref, changed))
1373 return;
1374
8ca151b5
JB
1375 mutex_lock(&mvm->mutex);
1376 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1377 ctx->rx_chains_static,
1378 ctx->rx_chains_dynamic);
1379 mutex_unlock(&mvm->mutex);
1380}
1381
1382static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
1383 struct ieee80211_vif *vif,
1384 struct ieee80211_chanctx_conf *ctx)
1385{
1386 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3
IP
1387 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1388 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
8ca151b5
JB
1389 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1390 int ret;
1391
1392 mutex_lock(&mvm->mutex);
1393
fe0f2de3 1394 mvmvif->phy_ctxt = phy_ctxt;
8ca151b5
JB
1395
1396 switch (vif->type) {
1397 case NL80211_IFTYPE_AP:
1398 /*
1399 * The AP binding flow is handled as part of the start_ap flow
1400 * (in bss_info_changed).
1401 */
1402 ret = 0;
1403 goto out_unlock;
1404 case NL80211_IFTYPE_STATION:
1405 case NL80211_IFTYPE_ADHOC:
1406 case NL80211_IFTYPE_MONITOR:
1407 break;
1408 default:
1409 ret = -EINVAL;
1410 goto out_unlock;
1411 }
1412
1413 ret = iwl_mvm_binding_add_vif(mvm, vif);
1414 if (ret)
1415 goto out_unlock;
1416
1417 /*
1418 * Setting the quota at this stage is only required for monitor
1419 * interfaces. For the other types, the bss_info changed flow
1420 * will handle quota settings.
1421 */
1422 if (vif->type == NL80211_IFTYPE_MONITOR) {
1e1391ca 1423 mvmvif->monitor_active = true;
8ca151b5
JB
1424 ret = iwl_mvm_update_quotas(mvm, vif);
1425 if (ret)
1426 goto out_remove_binding;
1427 }
1428
1429 goto out_unlock;
1430
1431 out_remove_binding:
1432 iwl_mvm_binding_remove_vif(mvm, vif);
1433 out_unlock:
1434 mutex_unlock(&mvm->mutex);
1435 if (ret)
1436 mvmvif->phy_ctxt = NULL;
1437 return ret;
1438}
1439
1440static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
1441 struct ieee80211_vif *vif,
1442 struct ieee80211_chanctx_conf *ctx)
1443{
1444 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1445 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1446
1447 mutex_lock(&mvm->mutex);
1448
1449 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
1450
1451 if (vif->type == NL80211_IFTYPE_AP)
1452 goto out_unlock;
1453
8ca151b5
JB
1454 switch (vif->type) {
1455 case NL80211_IFTYPE_MONITOR:
1e1391ca
IP
1456 mvmvif->monitor_active = false;
1457 iwl_mvm_update_quotas(mvm, NULL);
8ca151b5
JB
1458 break;
1459 default:
1460 break;
1461 }
1462
1e1391ca 1463 iwl_mvm_binding_remove_vif(mvm, vif);
8ca151b5
JB
1464out_unlock:
1465 mvmvif->phy_ctxt = NULL;
1466 mutex_unlock(&mvm->mutex);
1467}
1468
1469static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
1470 struct ieee80211_sta *sta,
1471 bool set)
1472{
1473 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1474 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1475
1476 if (!mvm_sta || !mvm_sta->vif) {
1477 IWL_ERR(mvm, "Station is not associated to a vif\n");
1478 return -EINVAL;
1479 }
1480
1481 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
1482}
1483
2b76ef13
EG
1484static void iwl_mvm_mac_rssi_callback(struct ieee80211_hw *hw,
1485 struct ieee80211_vif *vif,
1486 enum ieee80211_rssi_event rssi_event)
1487{
1488 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1489
1490 iwl_mvm_bt_rssi_event(mvm, vif, rssi_event);
1491}
1492
8ca151b5
JB
1493struct ieee80211_ops iwl_mvm_hw_ops = {
1494 .tx = iwl_mvm_mac_tx,
1495 .ampdu_action = iwl_mvm_mac_ampdu_action,
1496 .start = iwl_mvm_mac_start,
1497 .restart_complete = iwl_mvm_mac_restart_complete,
1498 .stop = iwl_mvm_mac_stop,
1499 .add_interface = iwl_mvm_mac_add_interface,
1500 .remove_interface = iwl_mvm_mac_remove_interface,
1501 .config = iwl_mvm_mac_config,
1502 .configure_filter = iwl_mvm_configure_filter,
1503 .bss_info_changed = iwl_mvm_bss_info_changed,
1504 .hw_scan = iwl_mvm_mac_hw_scan,
1505 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1506 .sta_state = iwl_mvm_mac_sta_state,
1507 .sta_notify = iwl_mvm_mac_sta_notify,
1508 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1509 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1510 .conf_tx = iwl_mvm_mac_conf_tx,
1511 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1512 .set_key = iwl_mvm_mac_set_key,
1513 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
1514 .remain_on_channel = iwl_mvm_roc,
1515 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
2b76ef13 1516 .rssi_callback = iwl_mvm_mac_rssi_callback,
8ca151b5
JB
1517
1518 .add_chanctx = iwl_mvm_add_chanctx,
1519 .remove_chanctx = iwl_mvm_remove_chanctx,
1520 .change_chanctx = iwl_mvm_change_chanctx,
1521 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
1522 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
1523
1524 .start_ap = iwl_mvm_start_ap,
1525 .stop_ap = iwl_mvm_stop_ap,
1526
1527 .set_tim = iwl_mvm_set_tim,
1528
1529#ifdef CONFIG_PM_SLEEP
1530 /* look at d3.c */
1531 .suspend = iwl_mvm_suspend,
1532 .resume = iwl_mvm_resume,
1533 .set_wakeup = iwl_mvm_set_wakeup,
1534 .set_rekey_data = iwl_mvm_set_rekey_data,
1535#if IS_ENABLED(CONFIG_IPV6)
1536 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1537#endif
1538 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1539#endif
1540};
This page took 0.123247 seconds and 5 git commands to generate.