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