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