1// SPDX-License-Identifier: GPL-2.0-only
2/******************************************************************************
3 *
4 * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
5 * Copyright (C) 2018 - 2019 Intel Corporation
6 *
7 * Portions of this file are derived from the ipw3945 project, as well
8 * as portions of the ieee80211 subsystem header files.
9 *
10 * Contact Information:
11 *  Intel Linux Wireless <linuxwifi@intel.com>
12 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
13 *
14 *****************************************************************************/
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/dma-mapping.h>
19#include <linux/delay.h>
20#include <linux/sched.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/if_arp.h>
25
26#include <net/ieee80211_radiotap.h>
27#include <net/mac80211.h>
28
29#include <asm/div64.h>
30
31#include "iwl-io.h"
32#include "iwl-trans.h"
33#include "iwl-op-mode.h"
34#include "iwl-modparams.h"
35
36#include "dev.h"
37#include "calib.h"
38#include "agn.h"
39
40/*****************************************************************************
41 *
42 * mac80211 entry point functions
43 *
44 *****************************************************************************/
45
46static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
47	{
48		.max = 1,
49		.types = BIT(NL80211_IFTYPE_STATION),
50	},
51	{
52		.max = 1,
53		.types = BIT(NL80211_IFTYPE_AP),
54	},
55};
56
57static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
58	{
59		.max = 2,
60		.types = BIT(NL80211_IFTYPE_STATION),
61	},
62};
63
64static const struct ieee80211_iface_combination
65iwlagn_iface_combinations_dualmode[] = {
66	{ .num_different_channels = 1,
67	  .max_interfaces = 2,
68	  .beacon_int_infra_match = true,
69	  .limits = iwlagn_sta_ap_limits,
70	  .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
71	},
72	{ .num_different_channels = 1,
73	  .max_interfaces = 2,
74	  .limits = iwlagn_2sta_limits,
75	  .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
76	},
77};
78
79/*
80 * Not a mac80211 entry point function, but it fits in with all the
81 * other mac80211 functions grouped here.
82 */
83int iwlagn_mac_setup_register(struct iwl_priv *priv,
84			      const struct iwl_ucode_capabilities *capa)
85{
86	int ret;
87	struct ieee80211_hw *hw = priv->hw;
88	struct iwl_rxon_context *ctx;
89
90	hw->rate_control_algorithm = "iwl-agn-rs";
91
92	/* Tell mac80211 our characteristics */
93	ieee80211_hw_set(hw, SIGNAL_DBM);
94	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
95	ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
96	ieee80211_hw_set(hw, SPECTRUM_MGMT);
97	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
98	ieee80211_hw_set(hw, QUEUE_CONTROL);
99	ieee80211_hw_set(hw, SUPPORTS_PS);
100	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
101	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
102	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
103
104	if (priv->trans->max_skb_frags)
105		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
106
107	hw->offchannel_tx_hw_queue = IWL_AUX_QUEUE;
108	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
109
110	/*
111	 * Including the following line will crash some AP's.  This
112	 * workaround removes the stimulus which causes the crash until
113	 * the AP software can be fixed.
114	hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
115	 */
116
117	if (priv->nvm_data->sku_cap_11n_enable)
118		hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS |
119				       NL80211_FEATURE_STATIC_SMPS;
120
121	/*
122	 * Enable 11w if advertised by firmware and software crypto
123	 * is not enabled (as the firmware will interpret some mgmt
124	 * packets, so enabling it with software crypto isn't safe)
125	 */
126	if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
127	    !iwlwifi_mod_params.swcrypto)
128		ieee80211_hw_set(hw, MFP_CAPABLE);
129
130	hw->sta_data_size = sizeof(struct iwl_station_priv);
131	hw->vif_data_size = sizeof(struct iwl_vif_priv);
132
133	for_each_context(priv, ctx) {
134		hw->wiphy->interface_modes |= ctx->interface_modes;
135		hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
136	}
137
138	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
139
140	if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
141		hw->wiphy->iface_combinations =
142			iwlagn_iface_combinations_dualmode;
143		hw->wiphy->n_iface_combinations =
144			ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
145	}
146
147	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
148	hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
149				       REGULATORY_DISABLE_BEACON_HINTS;
150
151#ifdef CONFIG_PM_SLEEP
152	if (priv->fw->img[IWL_UCODE_WOWLAN].num_sec &&
153	    priv->trans->ops->d3_suspend &&
154	    priv->trans->ops->d3_resume &&
155	    device_can_wakeup(priv->trans->dev)) {
156		priv->wowlan_support.flags = WIPHY_WOWLAN_MAGIC_PKT |
157					     WIPHY_WOWLAN_DISCONNECT |
158					     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
159					     WIPHY_WOWLAN_RFKILL_RELEASE;
160		if (!iwlwifi_mod_params.swcrypto)
161			priv->wowlan_support.flags |=
162				WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
163				WIPHY_WOWLAN_GTK_REKEY_FAILURE;
164
165		priv->wowlan_support.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
166		priv->wowlan_support.pattern_min_len =
167					IWLAGN_WOWLAN_MIN_PATTERN_LEN;
168		priv->wowlan_support.pattern_max_len =
169					IWLAGN_WOWLAN_MAX_PATTERN_LEN;
170		hw->wiphy->wowlan = &priv->wowlan_support;
171	}
172#endif
173
174	if (iwlwifi_mod_params.power_save)
175		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
176	else
177		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
178
179	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
180	/* we create the 802.11 header and a max-length SSID element */
181	hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 34;
182
183	/*
184	 * We don't use all queues: 4 and 9 are unused and any
185	 * aggregation queue gets mapped down to the AC queue.
186	 */
187	hw->queues = IWLAGN_FIRST_AMPDU_QUEUE;
188
189	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
190
191	if (priv->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
192		priv->hw->wiphy->bands[NL80211_BAND_2GHZ] =
193			&priv->nvm_data->bands[NL80211_BAND_2GHZ];
194	if (priv->nvm_data->bands[NL80211_BAND_5GHZ].n_channels)
195		priv->hw->wiphy->bands[NL80211_BAND_5GHZ] =
196			&priv->nvm_data->bands[NL80211_BAND_5GHZ];
197
198	hw->wiphy->hw_version = priv->trans->hw_id;
199
200	iwl_leds_init(priv);
201
202	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
203	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_EXT_KEY_ID);
204
205	ret = ieee80211_register_hw(priv->hw);
206	if (ret) {
207		IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
208		iwl_leds_exit(priv);
209		return ret;
210	}
211	priv->mac80211_registered = 1;
212
213	return 0;
214}
215
216void iwlagn_mac_unregister(struct iwl_priv *priv)
217{
218	if (!priv->mac80211_registered)
219		return;
220	iwl_leds_exit(priv);
221	ieee80211_unregister_hw(priv->hw);
222	priv->mac80211_registered = 0;
223}
224
225static int __iwl_up(struct iwl_priv *priv)
226{
227	struct iwl_rxon_context *ctx;
228	int ret;
229
230	lockdep_assert_held(&priv->mutex);
231
232	if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
233		IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
234		return -EIO;
235	}
236
237	for_each_context(priv, ctx) {
238		ret = iwlagn_alloc_bcast_station(priv, ctx);
239		if (ret) {
240			iwl_dealloc_bcast_stations(priv);
241			return ret;
242		}
243	}
244
245	ret = iwl_trans_start_hw(priv->trans);
246	if (ret) {
247		IWL_ERR(priv, "Failed to start HW: %d\n", ret);
248		goto error;
249	}
250
251	ret = iwl_run_init_ucode(priv);
252	if (ret) {
253		IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
254		goto error;
255	}
256
257	ret = iwl_trans_start_hw(priv->trans);
258	if (ret) {
259		IWL_ERR(priv, "Failed to start HW: %d\n", ret);
260		goto error;
261	}
262
263	ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
264	if (ret) {
265		IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
266		goto error;
267	}
268
269	ret = iwl_alive_start(priv);
270	if (ret)
271		goto error;
272	return 0;
273
274 error:
275	set_bit(STATUS_EXIT_PENDING, &priv->status);
276	iwl_down(priv);
277	clear_bit(STATUS_EXIT_PENDING, &priv->status);
278
279	IWL_ERR(priv, "Unable to initialize device.\n");
280	return ret;
281}
282
283static int iwlagn_mac_start(struct ieee80211_hw *hw)
284{
285	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
286	int ret;
287
288	IWL_DEBUG_MAC80211(priv, "enter\n");
289
290	/* we should be verifying the device is ready to be opened */
291	mutex_lock(&priv->mutex);
292	ret = __iwl_up(priv);
293	mutex_unlock(&priv->mutex);
294	if (ret)
295		return ret;
296
297	IWL_DEBUG_INFO(priv, "Start UP work done.\n");
298
299	/* Now we should be done, and the READY bit should be set. */
300	if (WARN_ON(!test_bit(STATUS_READY, &priv->status)))
301		ret = -EIO;
302
303	iwlagn_led_enable(priv);
304
305	priv->is_open = 1;
306	IWL_DEBUG_MAC80211(priv, "leave\n");
307	return ret;
308}
309
310static void iwlagn_mac_stop(struct ieee80211_hw *hw)
311{
312	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
313
314	IWL_DEBUG_MAC80211(priv, "enter\n");
315
316	if (!priv->is_open)
317		return;
318
319	priv->is_open = 0;
320
321	mutex_lock(&priv->mutex);
322	iwl_down(priv);
323	mutex_unlock(&priv->mutex);
324
325	iwl_cancel_deferred_work(priv);
326
327	flush_workqueue(priv->workqueue);
328
329	IWL_DEBUG_MAC80211(priv, "leave\n");
330}
331
332static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
333				      struct ieee80211_vif *vif,
334				      struct cfg80211_gtk_rekey_data *data)
335{
336	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
337
338	if (iwlwifi_mod_params.swcrypto)
339		return;
340
341	IWL_DEBUG_MAC80211(priv, "enter\n");
342	mutex_lock(&priv->mutex);
343
344	if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
345		goto out;
346
347	memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
348	memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
349	priv->replay_ctr =
350		cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
351	priv->have_rekey_data = true;
352
353 out:
354	mutex_unlock(&priv->mutex);
355	IWL_DEBUG_MAC80211(priv, "leave\n");
356}
357
358#ifdef CONFIG_PM_SLEEP
359
360static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
361			      struct cfg80211_wowlan *wowlan)
362{
363	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
364	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
365	int ret;
366
367	if (WARN_ON(!wowlan))
368		return -EINVAL;
369
370	IWL_DEBUG_MAC80211(priv, "enter\n");
371	mutex_lock(&priv->mutex);
372
373	/* Don't attempt WoWLAN when not associated, tear down instead. */
374	if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
375	    !iwl_is_associated_ctx(ctx)) {
376		ret = 1;
377		goto out;
378	}
379
380	ret = iwlagn_suspend(priv, wowlan);
381	if (ret)
382		goto error;
383
384	/* let the ucode operate on its own */
385	iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
386		    CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
387
388	iwl_trans_d3_suspend(priv->trans, false, true);
389
390	goto out;
391
392 error:
393	priv->wowlan = false;
394	iwlagn_prepare_restart(priv);
395	ieee80211_restart_hw(priv->hw);
396 out:
397	mutex_unlock(&priv->mutex);
398	IWL_DEBUG_MAC80211(priv, "leave\n");
399
400	return ret;
401}
402
403struct iwl_resume_data {
404	struct iwl_priv *priv;
405	struct iwlagn_wowlan_status *cmd;
406	bool valid;
407};
408
409static bool iwl_resume_status_fn(struct iwl_notif_wait_data *notif_wait,
410				 struct iwl_rx_packet *pkt, void *data)
411{
412	struct iwl_resume_data *resume_data = data;
413	struct iwl_priv *priv = resume_data->priv;
414
415	if (iwl_rx_packet_payload_len(pkt) != sizeof(*resume_data->cmd)) {
416		IWL_ERR(priv, "rx wrong size data\n");
417		return true;
418	}
419	memcpy(resume_data->cmd, pkt->data, sizeof(*resume_data->cmd));
420	resume_data->valid = true;
421
422	return true;
423}
424
425static int iwlagn_mac_resume(struct ieee80211_hw *hw)
426{
427	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
428	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
429	struct ieee80211_vif *vif;
430	u32 base;
431	int ret;
432	enum iwl_d3_status d3_status;
433	struct error_table_start {
434		/* cf. struct iwl_error_event_table */
435		u32 valid;
436		u32 error_id;
437	} err_info;
438	struct iwl_notification_wait status_wait;
439	static const u16 status_cmd[] = {
440		REPLY_WOWLAN_GET_STATUS,
441	};
442	struct iwlagn_wowlan_status status_data = {};
443	struct iwl_resume_data resume_data = {
444		.priv = priv,
445		.cmd = &status_data,
446		.valid = false,
447	};
448	struct cfg80211_wowlan_wakeup wakeup = {
449		.pattern_idx = -1,
450	};
451#ifdef CONFIG_IWLWIFI_DEBUGFS
452	const struct fw_img *img;
453#endif
454
455	IWL_DEBUG_MAC80211(priv, "enter\n");
456	mutex_lock(&priv->mutex);
457
458	/* we'll clear ctx->vif during iwlagn_prepare_restart() */
459	vif = ctx->vif;
460
461	ret = iwl_trans_d3_resume(priv->trans, &d3_status, false, true);
462	if (ret)
463		goto out_unlock;
464
465	if (d3_status != IWL_D3_STATUS_ALIVE) {
466		IWL_INFO(priv, "Device was reset during suspend\n");
467		goto out_unlock;
468	}
469
470	/* uCode is no longer operating by itself */
471	iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
472		    CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
473
474	base = priv->device_pointers.error_event_table;
475	if (!iwlagn_hw_valid_rtc_data_addr(base)) {
476		IWL_WARN(priv, "Invalid error table during resume!\n");
477		goto out_unlock;
478	}
479
480	iwl_trans_read_mem_bytes(priv->trans, base,
481				 &err_info, sizeof(err_info));
482
483	if (err_info.valid) {
484		IWL_INFO(priv, "error table is valid (%d, 0x%x)\n",
485			 err_info.valid, err_info.error_id);
486		if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
487			wakeup.rfkill_release = true;
488			ieee80211_report_wowlan_wakeup(vif, &wakeup,
489						       GFP_KERNEL);
490		}
491		goto out_unlock;
492	}
493
494#ifdef CONFIG_IWLWIFI_DEBUGFS
495	img = &priv->fw->img[IWL_UCODE_WOWLAN];
496	if (!priv->wowlan_sram)
497		priv->wowlan_sram =
498			kzalloc(img->sec[IWL_UCODE_SECTION_DATA].len,
499				GFP_KERNEL);
500
501	if (priv->wowlan_sram)
502		iwl_trans_read_mem(priv->trans, 0x800000,
503				   priv->wowlan_sram,
504				   img->sec[IWL_UCODE_SECTION_DATA].len / 4);
505#endif
506
507	/*
508	 * This is very strange. The GET_STATUS command is sent but the device
509	 * doesn't reply properly, it seems it doesn't close the RBD so one is
510	 * always left open ... As a result, we need to send another command
511	 * and have to reset the driver afterwards. As we need to switch to
512	 * runtime firmware again that'll happen.
513	 */
514
515	iwl_init_notification_wait(&priv->notif_wait, &status_wait, status_cmd,
516				   ARRAY_SIZE(status_cmd), iwl_resume_status_fn,
517				   &resume_data);
518
519	iwl_dvm_send_cmd_pdu(priv, REPLY_WOWLAN_GET_STATUS, CMD_ASYNC, 0, NULL);
520	iwl_dvm_send_cmd_pdu(priv, REPLY_ECHO, CMD_ASYNC, 0, NULL);
521	/* an RBD is left open in the firmware now! */
522
523	ret = iwl_wait_notification(&priv->notif_wait, &status_wait, HZ/5);
524	if (ret)
525		goto out_unlock;
526
527	if (resume_data.valid && priv->contexts[IWL_RXON_CTX_BSS].vif) {
528		u32 reasons = le32_to_cpu(status_data.wakeup_reason);
529		struct cfg80211_wowlan_wakeup *wakeup_report;
530
531		IWL_INFO(priv, "WoWLAN wakeup reason(s): 0x%.8x\n", reasons);
532
533		if (reasons) {
534			if (reasons & IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET)
535				wakeup.magic_pkt = true;
536			if (reasons & IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH)
537				wakeup.pattern_idx = status_data.pattern_number;
538			if (reasons & (IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
539				       IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE))
540				wakeup.disconnect = true;
541			if (reasons & IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL)
542				wakeup.gtk_rekey_failure = true;
543			if (reasons & IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ)
544				wakeup.eap_identity_req = true;
545			if (reasons & IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE)
546				wakeup.four_way_handshake = true;
547			wakeup_report = &wakeup;
548		} else {
549			wakeup_report = NULL;
550		}
551
552		ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
553	}
554
555	priv->wowlan = false;
556
557	iwlagn_prepare_restart(priv);
558
559	memset((void *)&ctx->active, 0, sizeof(ctx->active));
560	iwl_connection_init_rx_config(priv, ctx);
561	iwlagn_set_rxon_chain(priv, ctx);
562
563 out_unlock:
564	mutex_unlock(&priv->mutex);
565	IWL_DEBUG_MAC80211(priv, "leave\n");
566
567	ieee80211_resume_disconnect(vif);
568
569	return 1;
570}
571
572static void iwlagn_mac_set_wakeup(struct ieee80211_hw *hw, bool enabled)
573{
574	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
575
576	device_set_wakeup_enable(priv->trans->dev, enabled);
577}
578#endif
579
580static void iwlagn_mac_tx(struct ieee80211_hw *hw,
581			  struct ieee80211_tx_control *control,
582			  struct sk_buff *skb)
583{
584	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
585
586	if (iwlagn_tx_skb(priv, control->sta, skb))
587		ieee80211_free_txskb(hw, skb);
588}
589
590static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
591				       struct ieee80211_vif *vif,
592				       struct ieee80211_key_conf *keyconf,
593				       struct ieee80211_sta *sta,
594				       u32 iv32, u16 *phase1key)
595{
596	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
597
598	iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
599}
600
601static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
602			      struct ieee80211_vif *vif,
603			      struct ieee80211_sta *sta,
604			      struct ieee80211_key_conf *key)
605{
606	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
607	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
608	struct iwl_rxon_context *ctx = vif_priv->ctx;
609	int ret;
610	bool is_default_wep_key = false;
611
612	IWL_DEBUG_MAC80211(priv, "enter\n");
613
614	if (iwlwifi_mod_params.swcrypto) {
615		IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
616		return -EOPNOTSUPP;
617	}
618
619	switch (key->cipher) {
620	case WLAN_CIPHER_SUITE_TKIP:
621		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
622		/* fall through */
623	case WLAN_CIPHER_SUITE_CCMP:
624		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
625		break;
626	default:
627		break;
628	}
629
630	/*
631	 * We could program these keys into the hardware as well, but we
632	 * don't expect much multicast traffic in IBSS and having keys
633	 * for more stations is probably more useful.
634	 *
635	 * Mark key TX-only and return 0.
636	 */
637	if (vif->type == NL80211_IFTYPE_ADHOC &&
638	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
639		key->hw_key_idx = WEP_INVALID_OFFSET;
640		return 0;
641	}
642
643	/* If they key was TX-only, accept deletion */
644	if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
645		return 0;
646
647	mutex_lock(&priv->mutex);
648	iwl_scan_cancel_timeout(priv, 100);
649
650	BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
651
652	/*
653	 * If we are getting WEP group key and we didn't receive any key mapping
654	 * so far, we are in legacy wep mode (group key only), otherwise we are
655	 * in 1X mode.
656	 * In legacy wep mode, we use another host command to the uCode.
657	 */
658	if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
659	     key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
660		if (cmd == SET_KEY)
661			is_default_wep_key = !ctx->key_mapping_keys;
662		else
663			is_default_wep_key =
664				key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
665	}
666
667
668	switch (cmd) {
669	case SET_KEY:
670		if (is_default_wep_key) {
671			ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
672			break;
673		}
674		ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
675		if (ret) {
676			/*
677			 * can't add key for RX, but we don't need it
678			 * in the device for TX so still return 0
679			 */
680			ret = 0;
681			key->hw_key_idx = WEP_INVALID_OFFSET;
682		}
683
684		IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
685		break;
686	case DISABLE_KEY:
687		if (is_default_wep_key)
688			ret = iwl_remove_default_wep_key(priv, ctx, key);
689		else
690			ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
691
692		IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
693		break;
694	default:
695		ret = -EINVAL;
696	}
697
698	mutex_unlock(&priv->mutex);
699	IWL_DEBUG_MAC80211(priv, "leave\n");
700
701	return ret;
702}
703
704static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
705				   struct ieee80211_vif *vif,
706				   struct ieee80211_ampdu_params *params)
707{
708	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
709	int ret = -EINVAL;
710	struct ieee80211_sta *sta = params->sta;
711	enum ieee80211_ampdu_mlme_action action = params->action;
712	u16 tid = params->tid;
713	u16 *ssn = &params->ssn;
714	u8 buf_size = params->buf_size;
715	struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
716
717	IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
718		     sta->addr, tid);
719
720	if (!(priv->nvm_data->sku_cap_11n_enable))
721		return -EACCES;
722
723	IWL_DEBUG_MAC80211(priv, "enter\n");
724	mutex_lock(&priv->mutex);
725
726	switch (action) {
727	case IEEE80211_AMPDU_RX_START:
728		if (!iwl_enable_rx_ampdu())
729			break;
730		IWL_DEBUG_HT(priv, "start Rx\n");
731		ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
732		break;
733	case IEEE80211_AMPDU_RX_STOP:
734		IWL_DEBUG_HT(priv, "stop Rx\n");
735		ret = iwl_sta_rx_agg_stop(priv, sta, tid);
736		break;
737	case IEEE80211_AMPDU_TX_START:
738		if (!priv->trans->ops->txq_enable)
739			break;
740		if (!iwl_enable_tx_ampdu())
741			break;
742		IWL_DEBUG_HT(priv, "start Tx\n");
743		ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
744		break;
745	case IEEE80211_AMPDU_TX_STOP_FLUSH:
746	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
747		IWL_DEBUG_HT(priv, "Flush Tx\n");
748		ret = iwlagn_tx_agg_flush(priv, vif, sta, tid);
749		break;
750	case IEEE80211_AMPDU_TX_STOP_CONT:
751		IWL_DEBUG_HT(priv, "stop Tx\n");
752		ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
753		if ((ret == 0) && (priv->agg_tids_count > 0)) {
754			priv->agg_tids_count--;
755			IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
756				     priv->agg_tids_count);
757		}
758		if (!priv->agg_tids_count &&
759		    priv->hw_params.use_rts_for_aggregation) {
760			/*
761			 * switch off RTS/CTS if it was previously enabled
762			 */
763			sta_priv->lq_sta.lq.general_params.flags &=
764				~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
765			iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
766					&sta_priv->lq_sta.lq, CMD_ASYNC, false);
767		}
768		break;
769	case IEEE80211_AMPDU_TX_OPERATIONAL:
770		ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
771		break;
772	}
773	mutex_unlock(&priv->mutex);
774	IWL_DEBUG_MAC80211(priv, "leave\n");
775	return ret;
776}
777
778static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
779			      struct ieee80211_vif *vif,
780			      struct ieee80211_sta *sta)
781{
782	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
783	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
784	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
785	bool is_ap = vif->type == NL80211_IFTYPE_STATION;
786	int ret;
787	u8 sta_id;
788
789	IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
790			sta->addr);
791	sta_priv->sta_id = IWL_INVALID_STATION;
792
793	atomic_set(&sta_priv->pending_frames, 0);
794	if (vif->type == NL80211_IFTYPE_AP)
795		sta_priv->client = true;
796
797	ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
798				     is_ap, sta, &sta_id);
799	if (ret) {
800		IWL_ERR(priv, "Unable to add station %pM (%d)\n",
801			sta->addr, ret);
802		/* Should we return success if return code is EEXIST ? */
803		return ret;
804	}
805
806	sta_priv->sta_id = sta_id;
807
808	return 0;
809}
810
811static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
812				 struct ieee80211_vif *vif,
813				 struct ieee80211_sta *sta)
814{
815	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
816	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
817	int ret;
818
819	IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n", sta->addr);
820
821	if (vif->type == NL80211_IFTYPE_STATION) {
822		/*
823		 * Station will be removed from device when the RXON
824		 * is set to unassociated -- just deactivate it here
825		 * to avoid re-programming it.
826		 */
827		ret = 0;
828		iwl_deactivate_station(priv, sta_priv->sta_id, sta->addr);
829	} else {
830		ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
831		if (ret)
832			IWL_DEBUG_QUIET_RFKILL(priv,
833				"Error removing station %pM\n", sta->addr);
834	}
835	return ret;
836}
837
838static int iwlagn_mac_sta_state(struct ieee80211_hw *hw,
839				struct ieee80211_vif *vif,
840				struct ieee80211_sta *sta,
841				enum ieee80211_sta_state old_state,
842				enum ieee80211_sta_state new_state)
843{
844	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
845	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
846	enum {
847		NONE, ADD, REMOVE, HT_RATE_INIT, ADD_RATE_INIT,
848	} op = NONE;
849	int ret;
850
851	IWL_DEBUG_MAC80211(priv, "station %pM state change %d->%d\n",
852			   sta->addr, old_state, new_state);
853
854	mutex_lock(&priv->mutex);
855	if (vif->type == NL80211_IFTYPE_STATION) {
856		if (old_state == IEEE80211_STA_NOTEXIST &&
857		    new_state == IEEE80211_STA_NONE)
858			op = ADD;
859		else if (old_state == IEEE80211_STA_NONE &&
860			 new_state == IEEE80211_STA_NOTEXIST)
861			op = REMOVE;
862		else if (old_state == IEEE80211_STA_AUTH &&
863			 new_state == IEEE80211_STA_ASSOC)
864			op = HT_RATE_INIT;
865	} else {
866		if (old_state == IEEE80211_STA_AUTH &&
867		    new_state == IEEE80211_STA_ASSOC)
868			op = ADD_RATE_INIT;
869		else if (old_state == IEEE80211_STA_ASSOC &&
870			 new_state == IEEE80211_STA_AUTH)
871			op = REMOVE;
872	}
873
874	switch (op) {
875	case ADD:
876		ret = iwlagn_mac_sta_add(hw, vif, sta);
877		if (ret)
878			break;
879		/*
880		 * Clear the in-progress flag, the AP station entry was added
881		 * but we'll initialize LQ only when we've associated (which
882		 * would also clear the in-progress flag). This is necessary
883		 * in case we never initialize LQ because association fails.
884		 */
885		spin_lock_bh(&priv->sta_lock);
886		priv->stations[iwl_sta_id(sta)].used &=
887			~IWL_STA_UCODE_INPROGRESS;
888		spin_unlock_bh(&priv->sta_lock);
889		break;
890	case REMOVE:
891		ret = iwlagn_mac_sta_remove(hw, vif, sta);
892		break;
893	case ADD_RATE_INIT:
894		ret = iwlagn_mac_sta_add(hw, vif, sta);
895		if (ret)
896			break;
897		/* Initialize rate scaling */
898		IWL_DEBUG_INFO(priv,
899			       "Initializing rate scaling for station %pM\n",
900			       sta->addr);
901		iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
902		ret = 0;
903		break;
904	case HT_RATE_INIT:
905		/* Initialize rate scaling */
906		ret = iwl_sta_update_ht(priv, vif_priv->ctx, sta);
907		if (ret)
908			break;
909		IWL_DEBUG_INFO(priv,
910			       "Initializing rate scaling for station %pM\n",
911			       sta->addr);
912		iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
913		ret = 0;
914		break;
915	default:
916		ret = 0;
917		break;
918	}
919
920	/*
921	 * mac80211 might WARN if we fail, but due the way we
922	 * (badly) handle hard rfkill, we might fail here
923	 */
924	if (iwl_is_rfkill(priv))
925		ret = 0;
926
927	mutex_unlock(&priv->mutex);
928	IWL_DEBUG_MAC80211(priv, "leave\n");
929
930	return ret;
931}
932
933static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
934				      struct ieee80211_vif *vif,
935				      struct ieee80211_channel_switch *ch_switch)
936{
937	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
938	struct ieee80211_conf *conf = &hw->conf;
939	struct ieee80211_channel *channel = ch_switch->chandef.chan;
940	struct iwl_ht_config *ht_conf = &priv->current_ht_config;
941	/*
942	 * MULTI-FIXME
943	 * When we add support for multiple interfaces, we need to
944	 * revisit this. The channel switch command in the device
945	 * only affects the BSS context, but what does that really
946	 * mean? And what if we get a CSA on the second interface?
947	 * This needs a lot of work.
948	 */
949	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
950	u16 ch;
951
952	IWL_DEBUG_MAC80211(priv, "enter\n");
953
954	mutex_lock(&priv->mutex);
955
956	if (iwl_is_rfkill(priv))
957		goto out;
958
959	if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
960	    test_bit(STATUS_SCANNING, &priv->status) ||
961	    test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
962		goto out;
963
964	if (!iwl_is_associated_ctx(ctx))
965		goto out;
966
967	if (!priv->lib->set_channel_switch)
968		goto out;
969
970	ch = channel->hw_value;
971	if (le16_to_cpu(ctx->active.channel) == ch)
972		goto out;
973
974	priv->current_ht_config.smps = conf->smps_mode;
975
976	/* Configure HT40 channels */
977	switch (cfg80211_get_chandef_type(&ch_switch->chandef)) {
978	case NL80211_CHAN_NO_HT:
979	case NL80211_CHAN_HT20:
980		ctx->ht.is_40mhz = false;
981		ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
982		break;
983	case NL80211_CHAN_HT40MINUS:
984		ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
985		ctx->ht.is_40mhz = true;
986		break;
987	case NL80211_CHAN_HT40PLUS:
988		ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
989		ctx->ht.is_40mhz = true;
990		break;
991	}
992
993	if ((le16_to_cpu(ctx->staging.channel) != ch))
994		ctx->staging.flags = 0;
995
996	iwl_set_rxon_channel(priv, channel, ctx);
997	iwl_set_rxon_ht(priv, ht_conf);
998	iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
999
1000	/*
1001	 * at this point, staging_rxon has the
1002	 * configuration for channel switch
1003	 */
1004	set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
1005	priv->switch_channel = cpu_to_le16(ch);
1006	if (priv->lib->set_channel_switch(priv, ch_switch)) {
1007		clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
1008		priv->switch_channel = 0;
1009		ieee80211_chswitch_done(ctx->vif, false);
1010	}
1011
1012out:
1013	mutex_unlock(&priv->mutex);
1014	IWL_DEBUG_MAC80211(priv, "leave\n");
1015}
1016
1017void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
1018{
1019	/*
1020	 * MULTI-FIXME
1021	 * See iwlagn_mac_channel_switch.
1022	 */
1023	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1024
1025	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1026		return;
1027
1028	if (!test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
1029		return;
1030
1031	if (ctx->vif)
1032		ieee80211_chswitch_done(ctx->vif, is_success);
1033}
1034
1035static void iwlagn_configure_filter(struct ieee80211_hw *hw,
1036				    unsigned int changed_flags,
1037				    unsigned int *total_flags,
1038				    u64 multicast)
1039{
1040	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1041	__le32 filter_or = 0, filter_nand = 0;
1042	struct iwl_rxon_context *ctx;
1043
1044#define CHK(test, flag)	do { \
1045	if (*total_flags & (test))		\
1046		filter_or |= (flag);		\
1047	else					\
1048		filter_nand |= (flag);		\
1049	} while (0)
1050
1051	IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
1052			changed_flags, *total_flags);
1053
1054	CHK(FIF_OTHER_BSS, RXON_FILTER_PROMISC_MSK);
1055	/* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
1056	CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
1057	CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
1058
1059#undef CHK
1060
1061	mutex_lock(&priv->mutex);
1062
1063	for_each_context(priv, ctx) {
1064		ctx->staging.filter_flags &= ~filter_nand;
1065		ctx->staging.filter_flags |= filter_or;
1066
1067		/*
1068		 * Not committing directly because hardware can perform a scan,
1069		 * but we'll eventually commit the filter flags change anyway.
1070		 */
1071	}
1072
1073	mutex_unlock(&priv->mutex);
1074
1075	/*
1076	 * Receiving all multicast frames is always enabled by the
1077	 * default flags setup in iwl_connection_init_rx_config()
1078	 * since we currently do not support programming multicast
1079	 * filters into the device.
1080	 */
1081	*total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI |
1082			FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
1083}
1084
1085static void iwlagn_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1086			     u32 queues, bool drop)
1087{
1088	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1089	u32 scd_queues;
1090
1091	mutex_lock(&priv->mutex);
1092	IWL_DEBUG_MAC80211(priv, "enter\n");
1093
1094	if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1095		IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
1096		goto done;
1097	}
1098	if (iwl_is_rfkill(priv)) {
1099		IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
1100		goto done;
1101	}
1102
1103	scd_queues = BIT(priv->trans->trans_cfg->base_params->num_of_queues) - 1;
1104	scd_queues &= ~(BIT(IWL_IPAN_CMD_QUEUE_NUM) |
1105			BIT(IWL_DEFAULT_CMD_QUEUE_NUM));
1106
1107	if (drop) {
1108		IWL_DEBUG_TX_QUEUES(priv, "Flushing SCD queues: 0x%x\n",
1109				    scd_queues);
1110		if (iwlagn_txfifo_flush(priv, scd_queues)) {
1111			IWL_ERR(priv, "flush request fail\n");
1112			goto done;
1113		}
1114	}
1115
1116	IWL_DEBUG_TX_QUEUES(priv, "wait transmit/flush all frames\n");
1117	iwl_trans_wait_tx_queues_empty(priv->trans, scd_queues);
1118done:
1119	mutex_unlock(&priv->mutex);
1120	IWL_DEBUG_MAC80211(priv, "leave\n");
1121}
1122
1123static void iwlagn_mac_event_callback(struct ieee80211_hw *hw,
1124				      struct ieee80211_vif *vif,
1125				      const struct ieee80211_event *event)
1126{
1127	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1128
1129	if (event->type != RSSI_EVENT)
1130		return;
1131
1132	IWL_DEBUG_MAC80211(priv, "enter\n");
1133
1134	if (priv->lib->bt_params &&
1135	    priv->lib->bt_params->advanced_bt_coexist) {
1136		if (event->u.rssi.data == RSSI_EVENT_LOW)
1137			priv->bt_enable_pspoll = true;
1138		else if (event->u.rssi.data == RSSI_EVENT_HIGH)
1139			priv->bt_enable_pspoll = false;
1140
1141		queue_work(priv->workqueue, &priv->bt_runtime_config);
1142	} else {
1143		IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
1144				"ignoring RSSI callback\n");
1145	}
1146
1147	IWL_DEBUG_MAC80211(priv, "leave\n");
1148}
1149
1150static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
1151			      struct ieee80211_sta *sta, bool set)
1152{
1153	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1154
1155	queue_work(priv->workqueue, &priv->beacon_update);
1156
1157	return 0;
1158}
1159
1160static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
1161			      struct ieee80211_vif *vif, u16 queue,
1162			      const struct ieee80211_tx_queue_params *params)
1163{
1164	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1165	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1166	struct iwl_rxon_context *ctx = vif_priv->ctx;
1167	int q;
1168
1169	if (WARN_ON(!ctx))
1170		return -EINVAL;
1171
1172	IWL_DEBUG_MAC80211(priv, "enter\n");
1173
1174	if (!iwl_is_ready_rf(priv)) {
1175		IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1176		return -EIO;
1177	}
1178
1179	if (queue >= AC_NUM) {
1180		IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1181		return 0;
1182	}
1183
1184	q = AC_NUM - 1 - queue;
1185
1186	mutex_lock(&priv->mutex);
1187
1188	ctx->qos_data.def_qos_parm.ac[q].cw_min =
1189		cpu_to_le16(params->cw_min);
1190	ctx->qos_data.def_qos_parm.ac[q].cw_max =
1191		cpu_to_le16(params->cw_max);
1192	ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1193	ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1194			cpu_to_le16((params->txop * 32));
1195
1196	ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1197
1198	mutex_unlock(&priv->mutex);
1199
1200	IWL_DEBUG_MAC80211(priv, "leave\n");
1201	return 0;
1202}
1203
1204static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
1205{
1206	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1207
1208	return priv->ibss_manager == IWL_IBSS_MANAGER;
1209}
1210
1211static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1212{
1213	iwl_connection_init_rx_config(priv, ctx);
1214
1215	iwlagn_set_rxon_chain(priv, ctx);
1216
1217	return iwlagn_commit_rxon(priv, ctx);
1218}
1219
1220static int iwl_setup_interface(struct iwl_priv *priv,
1221			       struct iwl_rxon_context *ctx)
1222{
1223	struct ieee80211_vif *vif = ctx->vif;
1224	int err, ac;
1225
1226	lockdep_assert_held(&priv->mutex);
1227
1228	/*
1229	 * This variable will be correct only when there's just
1230	 * a single context, but all code using it is for hardware
1231	 * that supports only one context.
1232	 */
1233	priv->iw_mode = vif->type;
1234
1235	ctx->is_active = true;
1236
1237	err = iwl_set_mode(priv, ctx);
1238	if (err) {
1239		if (!ctx->always_active)
1240			ctx->is_active = false;
1241		return err;
1242	}
1243
1244	if (priv->lib->bt_params && priv->lib->bt_params->advanced_bt_coexist &&
1245	    vif->type == NL80211_IFTYPE_ADHOC) {
1246		/*
1247		 * pretend to have high BT traffic as long as we
1248		 * are operating in IBSS mode, as this will cause
1249		 * the rate scaling etc. to behave as intended.
1250		 */
1251		priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1252	}
1253
1254	/* set up queue mappings */
1255	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1256		vif->hw_queue[ac] = ctx->ac_to_queue[ac];
1257
1258	if (vif->type == NL80211_IFTYPE_AP)
1259		vif->cab_queue = ctx->mcast_queue;
1260	else
1261		vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
1262
1263	return 0;
1264}
1265
1266static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
1267				    struct ieee80211_vif *vif)
1268{
1269	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1270	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1271	struct iwl_rxon_context *tmp, *ctx = NULL;
1272	int err;
1273	enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1274	bool reset = false;
1275
1276	IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1277			   viftype, vif->addr);
1278
1279	mutex_lock(&priv->mutex);
1280
1281	if (!iwl_is_ready_rf(priv)) {
1282		IWL_WARN(priv, "Try to add interface when device not ready\n");
1283		err = -EINVAL;
1284		goto out;
1285	}
1286
1287	for_each_context(priv, tmp) {
1288		u32 possible_modes =
1289			tmp->interface_modes | tmp->exclusive_interface_modes;
1290
1291		if (tmp->vif) {
1292			/* On reset we need to add the same interface again */
1293			if (tmp->vif == vif) {
1294				reset = true;
1295				ctx = tmp;
1296				break;
1297			}
1298
1299			/* check if this busy context is exclusive */
1300			if (tmp->exclusive_interface_modes &
1301						BIT(tmp->vif->type)) {
1302				err = -EINVAL;
1303				goto out;
1304			}
1305			continue;
1306		}
1307
1308		if (!(possible_modes & BIT(viftype)))
1309			continue;
1310
1311		/* have maybe usable context w/o interface */
1312		ctx = tmp;
1313		break;
1314	}
1315
1316	if (!ctx) {
1317		err = -EOPNOTSUPP;
1318		goto out;
1319	}
1320
1321	vif_priv->ctx = ctx;
1322	ctx->vif = vif;
1323
1324	/*
1325	 * In SNIFFER device type, the firmware reports the FCS to
1326	 * the host, rather than snipping it off. Unfortunately,
1327	 * mac80211 doesn't (yet) provide a per-packet flag for
1328	 * this, so that we have to set the hardware flag based
1329	 * on the interfaces added. As the monitor interface can
1330	 * only be present by itself, and will be removed before
1331	 * other interfaces are added, this is safe.
1332	 */
1333	if (vif->type == NL80211_IFTYPE_MONITOR)
1334		ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
1335	else
1336		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, priv->hw->flags);
1337
1338	err = iwl_setup_interface(priv, ctx);
1339	if (!err || reset)
1340		goto out;
1341
1342	ctx->vif = NULL;
1343	priv->iw_mode = NL80211_IFTYPE_STATION;
1344 out:
1345	mutex_unlock(&priv->mutex);
1346
1347	IWL_DEBUG_MAC80211(priv, "leave\n");
1348	return err;
1349}
1350
1351static void iwl_teardown_interface(struct iwl_priv *priv,
1352				   struct ieee80211_vif *vif,
1353				   bool mode_change)
1354{
1355	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1356
1357	lockdep_assert_held(&priv->mutex);
1358
1359	if (priv->scan_vif == vif) {
1360		iwl_scan_cancel_timeout(priv, 200);
1361		iwl_force_scan_end(priv);
1362	}
1363
1364	if (!mode_change) {
1365		iwl_set_mode(priv, ctx);
1366		if (!ctx->always_active)
1367			ctx->is_active = false;
1368	}
1369
1370	/*
1371	 * When removing the IBSS interface, overwrite the
1372	 * BT traffic load with the stored one from the last
1373	 * notification, if any. If this is a device that
1374	 * doesn't implement this, this has no effect since
1375	 * both values are the same and zero.
1376	 */
1377	if (vif->type == NL80211_IFTYPE_ADHOC)
1378		priv->bt_traffic_load = priv->last_bt_traffic_load;
1379}
1380
1381static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
1382			      struct ieee80211_vif *vif)
1383{
1384	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1385	struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1386
1387	IWL_DEBUG_MAC80211(priv, "enter\n");
1388
1389	mutex_lock(&priv->mutex);
1390
1391	WARN_ON(ctx->vif != vif);
1392	ctx->vif = NULL;
1393
1394	iwl_teardown_interface(priv, vif, false);
1395
1396	mutex_unlock(&priv->mutex);
1397
1398	IWL_DEBUG_MAC80211(priv, "leave\n");
1399
1400}
1401
1402static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
1403				       struct ieee80211_vif *vif,
1404				       enum nl80211_iftype newtype, bool newp2p)
1405{
1406	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1407	struct iwl_rxon_context *ctx, *tmp;
1408	enum nl80211_iftype newviftype = newtype;
1409	u32 interface_modes;
1410	int err;
1411
1412	IWL_DEBUG_MAC80211(priv, "enter\n");
1413
1414	newtype = ieee80211_iftype_p2p(newtype, newp2p);
1415
1416	mutex_lock(&priv->mutex);
1417
1418	ctx = iwl_rxon_ctx_from_vif(vif);
1419
1420	/*
1421	 * To simplify this code, only support changes on the
1422	 * BSS context. The PAN context is usually reassigned
1423	 * by creating/removing P2P interfaces anyway.
1424	 */
1425	if (ctx->ctxid != IWL_RXON_CTX_BSS) {
1426		err = -EBUSY;
1427		goto out;
1428	}
1429
1430	if (!ctx->vif || !iwl_is_ready_rf(priv)) {
1431		/*
1432		 * Huh? But wait ... this can maybe happen when
1433		 * we're in the middle of a firmware restart!
1434		 */
1435		err = -EBUSY;
1436		goto out;
1437	}
1438
1439	/* Check if the switch is supported in the same context */
1440	interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1441	if (!(interface_modes & BIT(newtype))) {
1442		err = -EBUSY;
1443		goto out;
1444	}
1445
1446	if (ctx->exclusive_interface_modes & BIT(newtype)) {
1447		for_each_context(priv, tmp) {
1448			if (ctx == tmp)
1449				continue;
1450
1451			if (!tmp->is_active)
1452				continue;
1453
1454			/*
1455			 * The current mode switch would be exclusive, but
1456			 * another context is active ... refuse the switch.
1457			 */
1458			err = -EBUSY;
1459			goto out;
1460		}
1461	}
1462
1463	/* success */
1464	iwl_teardown_interface(priv, vif, true);
1465	vif->type = newviftype;
1466	vif->p2p = newp2p;
1467	err = iwl_setup_interface(priv, ctx);
1468	WARN_ON(err);
1469	/*
1470	 * We've switched internally, but submitting to the
1471	 * device may have failed for some reason. Mask this
1472	 * error, because otherwise mac80211 will not switch
1473	 * (and set the interface type back) and we'll be
1474	 * out of sync with it.
1475	 */
1476	err = 0;
1477
1478 out:
1479	mutex_unlock(&priv->mutex);
1480	IWL_DEBUG_MAC80211(priv, "leave\n");
1481
1482	return err;
1483}
1484
1485static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
1486			      struct ieee80211_vif *vif,
1487			      struct ieee80211_scan_request *hw_req)
1488{
1489	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1490	struct cfg80211_scan_request *req = &hw_req->req;
1491	int ret;
1492
1493	IWL_DEBUG_MAC80211(priv, "enter\n");
1494
1495	if (req->n_channels == 0)
1496		return -EINVAL;
1497
1498	mutex_lock(&priv->mutex);
1499
1500	/*
1501	 * If an internal scan is in progress, just set
1502	 * up the scan_request as per above.
1503	 */
1504	if (priv->scan_type != IWL_SCAN_NORMAL) {
1505		IWL_DEBUG_SCAN(priv,
1506			       "SCAN request during internal scan - defer\n");
1507		priv->scan_request = req;
1508		priv->scan_vif = vif;
1509		ret = 0;
1510	} else {
1511		priv->scan_request = req;
1512		priv->scan_vif = vif;
1513		/*
1514		 * mac80211 will only ask for one band at a time
1515		 * so using channels[0] here is ok
1516		 */
1517		ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
1518					req->channels[0]->band);
1519		if (ret) {
1520			priv->scan_request = NULL;
1521			priv->scan_vif = NULL;
1522		}
1523	}
1524
1525	IWL_DEBUG_MAC80211(priv, "leave\n");
1526
1527	mutex_unlock(&priv->mutex);
1528
1529	return ret;
1530}
1531
1532static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1533{
1534	struct iwl_addsta_cmd cmd = {
1535		.mode = STA_CONTROL_MODIFY_MSK,
1536		.station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1537		.sta.sta_id = sta_id,
1538	};
1539
1540	iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1541}
1542
1543static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
1544				  struct ieee80211_vif *vif,
1545				  enum sta_notify_cmd cmd,
1546				  struct ieee80211_sta *sta)
1547{
1548	struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1549	struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1550	int sta_id;
1551
1552	IWL_DEBUG_MAC80211(priv, "enter\n");
1553
1554	switch (cmd) {
1555	case STA_NOTIFY_SLEEP:
1556		WARN_ON(!sta_priv->client);
1557		sta_priv->asleep = true;
1558		if (atomic_read(&sta_priv->pending_frames) > 0)
1559			ieee80211_sta_block_awake(hw, sta, true);
1560		break;
1561	case STA_NOTIFY_AWAKE:
1562		WARN_ON(!sta_priv->client);
1563		if (!sta_priv->asleep)
1564			break;
1565		sta_priv->asleep = false;
1566		sta_id = iwl_sta_id(sta);
1567		if (sta_id != IWL_INVALID_STATION)
1568			iwl_sta_modify_ps_wake(priv, sta_id);
1569		break;
1570	default:
1571		break;
1572	}
1573	IWL_DEBUG_MAC80211(priv, "leave\n");
1574}
1575
1576const struct ieee80211_ops iwlagn_hw_ops = {
1577	.tx = iwlagn_mac_tx,
1578	.start = iwlagn_mac_start,
1579	.stop = iwlagn_mac_stop,
1580#ifdef CONFIG_PM_SLEEP
1581	.suspend = iwlagn_mac_suspend,
1582	.resume = iwlagn_mac_resume,
1583	.set_wakeup = iwlagn_mac_set_wakeup,
1584#endif
1585	.add_interface = iwlagn_mac_add_interface,
1586	.remove_interface = iwlagn_mac_remove_interface,
1587	.change_interface = iwlagn_mac_change_interface,
1588	.config = iwlagn_mac_config,
1589	.configure_filter = iwlagn_configure_filter,
1590	.set_key = iwlagn_mac_set_key,
1591	.update_tkip_key = iwlagn_mac_update_tkip_key,
1592	.set_rekey_data = iwlagn_mac_set_rekey_data,
1593	.conf_tx = iwlagn_mac_conf_tx,
1594	.bss_info_changed = iwlagn_bss_info_changed,
1595	.ampdu_action = iwlagn_mac_ampdu_action,
1596	.hw_scan = iwlagn_mac_hw_scan,
1597	.sta_notify = iwlagn_mac_sta_notify,
1598	.sta_state = iwlagn_mac_sta_state,
1599	.channel_switch = iwlagn_mac_channel_switch,
1600	.flush = iwlagn_mac_flush,
1601	.tx_last_beacon = iwlagn_mac_tx_last_beacon,
1602	.event_callback = iwlagn_mac_event_callback,
1603	.set_tim = iwlagn_mac_set_tim,
1604};
1605
1606/* This function both allocates and initializes hw and priv. */
1607struct ieee80211_hw *iwl_alloc_all(void)
1608{
1609	struct iwl_priv *priv;
1610	struct iwl_op_mode *op_mode;
1611	/* mac80211 allocates memory for this device instance, including
1612	 *   space for this driver's private structure */
1613	struct ieee80211_hw *hw;
1614
1615	hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
1616				sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
1617	if (!hw)
1618		goto out;
1619
1620	op_mode = hw->priv;
1621	priv = IWL_OP_MODE_GET_DVM(op_mode);
1622	priv->hw = hw;
1623
1624out:
1625	return hw;
1626}
1627