1 /*
2  * Copyright (c) 2010-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include "htc.h"
18 
19 /*************/
20 /* Utilities */
21 /*************/
22 
23 /* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
24 static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
25 					      struct ath9k_channel *ichan)
26 {
27 	if (IS_CHAN_5GHZ(ichan))
28 		return HTC_MODE_11NA;
29 
30 	return HTC_MODE_11NG;
31 }
32 
33 bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
34 			enum ath9k_power_mode mode)
35 {
36 	bool ret;
37 
38 	mutex_lock(&priv->htc_pm_lock);
39 	ret = ath9k_hw_setpower(priv->ah, mode);
40 	mutex_unlock(&priv->htc_pm_lock);
41 
42 	return ret;
43 }
44 
45 void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
46 {
47 	mutex_lock(&priv->htc_pm_lock);
48 	if (++priv->ps_usecount != 1)
49 		goto unlock;
50 	ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);
51 
52 unlock:
53 	mutex_unlock(&priv->htc_pm_lock);
54 }
55 
56 void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
57 {
58 	bool reset;
59 
60 	mutex_lock(&priv->htc_pm_lock);
61 	if (--priv->ps_usecount != 0)
62 		goto unlock;
63 
64 	if (priv->ps_idle) {
65 		ath9k_hw_setrxabort(priv->ah, true);
66 		ath9k_hw_stopdmarecv(priv->ah, &reset);
67 		ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
68 	} else if (priv->ps_enabled) {
69 		ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
70 	}
71 
72 unlock:
73 	mutex_unlock(&priv->htc_pm_lock);
74 }
75 
76 void ath9k_ps_work(struct work_struct *work)
77 {
78 	struct ath9k_htc_priv *priv =
79 		container_of(work, struct ath9k_htc_priv,
80 			     ps_work);
81 	ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
82 
83 	/* The chip wakes up after receiving the first beacon
84 	   while network sleep is enabled. For the driver to
85 	   be in sync with the hw, set the chip to awake and
86 	   only then set it to sleep.
87 	 */
88 	ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
89 }
90 
91 static void ath9k_htc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
92 {
93 	struct ath9k_htc_priv *priv = data;
94 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
95 
96 	if ((vif->type == NL80211_IFTYPE_AP ||
97 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
98 	    bss_conf->enable_beacon)
99 		priv->reconfig_beacon = true;
100 
101 	if (bss_conf->assoc) {
102 		priv->rearm_ani = true;
103 		priv->reconfig_beacon = true;
104 	}
105 }
106 
107 static void ath9k_htc_vif_reconfig(struct ath9k_htc_priv *priv)
108 {
109 	priv->rearm_ani = false;
110 	priv->reconfig_beacon = false;
111 
112 	ieee80211_iterate_active_interfaces_atomic(
113 		priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
114 		ath9k_htc_vif_iter, priv);
115 	if (priv->rearm_ani)
116 		ath9k_htc_start_ani(priv);
117 
118 	if (priv->reconfig_beacon) {
119 		ath9k_htc_ps_wakeup(priv);
120 		ath9k_htc_beacon_reconfig(priv);
121 		ath9k_htc_ps_restore(priv);
122 	}
123 }
124 
125 static void ath9k_htc_bssid_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
126 {
127 	struct ath9k_vif_iter_data *iter_data = data;
128 	int i;
129 
130 	if (iter_data->hw_macaddr != NULL) {
131 		for (i = 0; i < ETH_ALEN; i++)
132 			iter_data->mask[i] &= ~(iter_data->hw_macaddr[i] ^ mac[i]);
133 	} else {
134 		iter_data->hw_macaddr = mac;
135 	}
136 }
137 
138 static void ath9k_htc_set_mac_bssid_mask(struct ath9k_htc_priv *priv,
139 				     struct ieee80211_vif *vif)
140 {
141 	struct ath_common *common = ath9k_hw_common(priv->ah);
142 	struct ath9k_vif_iter_data iter_data;
143 
144 	/*
145 	 * Pick the MAC address of the first interface as the new hardware
146 	 * MAC address. The hardware will use it together with the BSSID mask
147 	 * when matching addresses.
148 	 */
149 	iter_data.hw_macaddr = NULL;
150 	memset(&iter_data.mask, 0xff, ETH_ALEN);
151 
152 	if (vif)
153 		ath9k_htc_bssid_iter(&iter_data, vif->addr, vif);
154 
155 	/* Get list of all active MAC addresses */
156 	ieee80211_iterate_active_interfaces_atomic(
157 		priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
158 		ath9k_htc_bssid_iter, &iter_data);
159 
160 	memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
161 
162 	if (iter_data.hw_macaddr)
163 		memcpy(common->macaddr, iter_data.hw_macaddr, ETH_ALEN);
164 
165 	ath_hw_setbssidmask(common);
166 }
167 
168 static void ath9k_htc_set_opmode(struct ath9k_htc_priv *priv)
169 {
170 	if (priv->num_ibss_vif)
171 		priv->ah->opmode = NL80211_IFTYPE_ADHOC;
172 	else if (priv->num_ap_vif)
173 		priv->ah->opmode = NL80211_IFTYPE_AP;
174 	else if (priv->num_mbss_vif)
175 		priv->ah->opmode = NL80211_IFTYPE_MESH_POINT;
176 	else
177 		priv->ah->opmode = NL80211_IFTYPE_STATION;
178 
179 	ath9k_hw_setopmode(priv->ah);
180 }
181 
182 void ath9k_htc_reset(struct ath9k_htc_priv *priv)
183 {
184 	struct ath_hw *ah = priv->ah;
185 	struct ath_common *common = ath9k_hw_common(ah);
186 	struct ieee80211_channel *channel = priv->hw->conf.chandef.chan;
187 	struct ath9k_hw_cal_data *caldata = NULL;
188 	enum htc_phymode mode;
189 	__be16 htc_mode;
190 	u8 cmd_rsp;
191 	int ret;
192 
193 	mutex_lock(&priv->mutex);
194 	ath9k_htc_ps_wakeup(priv);
195 
196 	ath9k_htc_stop_ani(priv);
197 	ieee80211_stop_queues(priv->hw);
198 
199 	del_timer_sync(&priv->tx.cleanup_timer);
200 	ath9k_htc_tx_drain(priv);
201 
202 	WMI_CMD(WMI_DISABLE_INTR_CMDID);
203 	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
204 	WMI_CMD(WMI_STOP_RECV_CMDID);
205 
206 	ath9k_wmi_event_drain(priv);
207 
208 	caldata = &priv->caldata;
209 	ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
210 	if (ret) {
211 		ath_err(common,
212 			"Unable to reset device (%u Mhz) reset status %d\n",
213 			channel->center_freq, ret);
214 	}
215 
216 	ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
217 			       &priv->curtxpow);
218 
219 	WMI_CMD(WMI_START_RECV_CMDID);
220 	ath9k_host_rx_init(priv);
221 
222 	mode = ath9k_htc_get_curmode(priv, ah->curchan);
223 	htc_mode = cpu_to_be16(mode);
224 	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
225 
226 	WMI_CMD(WMI_ENABLE_INTR_CMDID);
227 	htc_start(priv->htc);
228 	ath9k_htc_vif_reconfig(priv);
229 	ieee80211_wake_queues(priv->hw);
230 
231 	mod_timer(&priv->tx.cleanup_timer,
232 		  jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
233 
234 	ath9k_htc_ps_restore(priv);
235 	mutex_unlock(&priv->mutex);
236 }
237 
238 static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
239 				 struct ieee80211_hw *hw,
240 				 struct ath9k_channel *hchan)
241 {
242 	struct ath_hw *ah = priv->ah;
243 	struct ath_common *common = ath9k_hw_common(ah);
244 	struct ieee80211_conf *conf = &common->hw->conf;
245 	bool fastcc;
246 	struct ieee80211_channel *channel = hw->conf.chandef.chan;
247 	struct ath9k_hw_cal_data *caldata = NULL;
248 	enum htc_phymode mode;
249 	__be16 htc_mode;
250 	u8 cmd_rsp;
251 	int ret;
252 
253 	if (test_bit(ATH_OP_INVALID, &common->op_flags))
254 		return -EIO;
255 
256 	fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
257 
258 	ath9k_htc_ps_wakeup(priv);
259 
260 	del_timer_sync(&priv->tx.cleanup_timer);
261 	ath9k_htc_tx_drain(priv);
262 
263 	WMI_CMD(WMI_DISABLE_INTR_CMDID);
264 	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
265 	WMI_CMD(WMI_STOP_RECV_CMDID);
266 
267 	ath9k_wmi_event_drain(priv);
268 
269 	ath_dbg(common, CONFIG,
270 		"(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
271 		priv->ah->curchan->channel,
272 		channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
273 		fastcc);
274 
275 	if (!fastcc)
276 		caldata = &priv->caldata;
277 
278 	ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
279 	if (ret) {
280 		ath_err(common,
281 			"Unable to reset channel (%u Mhz) reset status %d\n",
282 			channel->center_freq, ret);
283 		goto err;
284 	}
285 
286 	ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
287 			       &priv->curtxpow);
288 
289 	WMI_CMD(WMI_START_RECV_CMDID);
290 	if (ret)
291 		goto err;
292 
293 	ath9k_host_rx_init(priv);
294 
295 	mode = ath9k_htc_get_curmode(priv, hchan);
296 	htc_mode = cpu_to_be16(mode);
297 	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
298 	if (ret)
299 		goto err;
300 
301 	WMI_CMD(WMI_ENABLE_INTR_CMDID);
302 	if (ret)
303 		goto err;
304 
305 	htc_start(priv->htc);
306 
307 	if (!test_bit(ATH_OP_SCANNING, &common->op_flags) &&
308 	    !(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
309 		ath9k_htc_vif_reconfig(priv);
310 
311 	mod_timer(&priv->tx.cleanup_timer,
312 		  jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
313 
314 err:
315 	ath9k_htc_ps_restore(priv);
316 	return ret;
317 }
318 
319 /*
320  * Monitor mode handling is a tad complicated because the firmware requires
321  * an interface to be created exclusively, while mac80211 doesn't associate
322  * an interface with the mode.
323  *
324  * So, for now, only one monitor interface can be configured.
325  */
326 static void __ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
327 {
328 	struct ath_common *common = ath9k_hw_common(priv->ah);
329 	struct ath9k_htc_target_vif hvif;
330 	int ret = 0;
331 	u8 cmd_rsp;
332 
333 	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
334 	memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
335 	hvif.index = priv->mon_vif_idx;
336 	WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
337 	if (ret) {
338 		ath_err(common, "Unable to remove monitor interface at idx: %d\n",
339 			priv->mon_vif_idx);
340 	}
341 
342 	priv->nvifs--;
343 	priv->vif_slot &= ~(1 << priv->mon_vif_idx);
344 }
345 
346 static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
347 {
348 	struct ath_common *common = ath9k_hw_common(priv->ah);
349 	struct ath9k_htc_target_vif hvif;
350 	struct ath9k_htc_target_sta tsta;
351 	int ret = 0, sta_idx;
352 	u8 cmd_rsp;
353 
354 	if ((priv->nvifs >= ATH9K_HTC_MAX_VIF) ||
355 	    (priv->nstations >= ATH9K_HTC_MAX_STA)) {
356 		ret = -ENOBUFS;
357 		goto err_vif;
358 	}
359 
360 	sta_idx = ffz(priv->sta_slot);
361 	if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA)) {
362 		ret = -ENOBUFS;
363 		goto err_vif;
364 	}
365 
366 	/*
367 	 * Add an interface.
368 	 */
369 	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
370 	memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
371 
372 	hvif.opmode = HTC_M_MONITOR;
373 	hvif.index = ffz(priv->vif_slot);
374 
375 	WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
376 	if (ret)
377 		goto err_vif;
378 
379 	/*
380 	 * Assign the monitor interface index as a special case here.
381 	 * This is needed when the interface is brought down.
382 	 */
383 	priv->mon_vif_idx = hvif.index;
384 	priv->vif_slot |= (1 << hvif.index);
385 
386 	/*
387 	 * Set the hardware mode to monitor only if there are no
388 	 * other interfaces.
389 	 */
390 	if (!priv->nvifs)
391 		priv->ah->opmode = NL80211_IFTYPE_MONITOR;
392 
393 	priv->nvifs++;
394 
395 	/*
396 	 * Associate a station with the interface for packet injection.
397 	 */
398 	memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
399 
400 	memcpy(&tsta.macaddr, common->macaddr, ETH_ALEN);
401 
402 	tsta.is_vif_sta = 1;
403 	tsta.sta_index = sta_idx;
404 	tsta.vif_index = hvif.index;
405 	tsta.maxampdu = cpu_to_be16(0xffff);
406 
407 	WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
408 	if (ret) {
409 		ath_err(common, "Unable to add station entry for monitor mode\n");
410 		goto err_sta;
411 	}
412 
413 	priv->sta_slot |= (1 << sta_idx);
414 	priv->nstations++;
415 	priv->vif_sta_pos[priv->mon_vif_idx] = sta_idx;
416 	priv->ah->is_monitoring = true;
417 
418 	ath_dbg(common, CONFIG,
419 		"Attached a monitor interface at idx: %d, sta idx: %d\n",
420 		priv->mon_vif_idx, sta_idx);
421 
422 	return 0;
423 
424 err_sta:
425 	/*
426 	 * Remove the interface from the target.
427 	 */
428 	__ath9k_htc_remove_monitor_interface(priv);
429 err_vif:
430 	ath_dbg(common, FATAL, "Unable to attach a monitor interface\n");
431 
432 	return ret;
433 }
434 
435 static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
436 {
437 	struct ath_common *common = ath9k_hw_common(priv->ah);
438 	int ret = 0;
439 	u8 cmd_rsp, sta_idx;
440 
441 	__ath9k_htc_remove_monitor_interface(priv);
442 
443 	sta_idx = priv->vif_sta_pos[priv->mon_vif_idx];
444 
445 	WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
446 	if (ret) {
447 		ath_err(common, "Unable to remove station entry for monitor mode\n");
448 		return ret;
449 	}
450 
451 	priv->sta_slot &= ~(1 << sta_idx);
452 	priv->nstations--;
453 	priv->ah->is_monitoring = false;
454 
455 	ath_dbg(common, CONFIG,
456 		"Removed a monitor interface at idx: %d, sta idx: %d\n",
457 		priv->mon_vif_idx, sta_idx);
458 
459 	return 0;
460 }
461 
462 static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
463 				 struct ieee80211_vif *vif,
464 				 struct ieee80211_sta *sta)
465 {
466 	struct ath_common *common = ath9k_hw_common(priv->ah);
467 	struct ath9k_htc_target_sta tsta;
468 	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
469 	struct ath9k_htc_sta *ista;
470 	int ret, sta_idx;
471 	u8 cmd_rsp;
472 	u16 maxampdu;
473 
474 	if (priv->nstations >= ATH9K_HTC_MAX_STA)
475 		return -ENOBUFS;
476 
477 	sta_idx = ffz(priv->sta_slot);
478 	if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA))
479 		return -ENOBUFS;
480 
481 	memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
482 
483 	if (sta) {
484 		ista = (struct ath9k_htc_sta *) sta->drv_priv;
485 		memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
486 		memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
487 		ista->index = sta_idx;
488 		tsta.is_vif_sta = 0;
489 		maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
490 				 sta->ht_cap.ampdu_factor);
491 		tsta.maxampdu = cpu_to_be16(maxampdu);
492 	} else {
493 		memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
494 		tsta.is_vif_sta = 1;
495 		tsta.maxampdu = cpu_to_be16(0xffff);
496 	}
497 
498 	tsta.sta_index = sta_idx;
499 	tsta.vif_index = avp->index;
500 
501 	WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
502 	if (ret) {
503 		if (sta)
504 			ath_err(common,
505 				"Unable to add station entry for: %pM\n",
506 				sta->addr);
507 		return ret;
508 	}
509 
510 	if (sta) {
511 		ath_dbg(common, CONFIG,
512 			"Added a station entry for: %pM (idx: %d)\n",
513 			sta->addr, tsta.sta_index);
514 	} else {
515 		ath_dbg(common, CONFIG,
516 			"Added a station entry for VIF %d (idx: %d)\n",
517 			avp->index, tsta.sta_index);
518 	}
519 
520 	priv->sta_slot |= (1 << sta_idx);
521 	priv->nstations++;
522 	if (!sta)
523 		priv->vif_sta_pos[avp->index] = sta_idx;
524 
525 	return 0;
526 }
527 
528 static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
529 				    struct ieee80211_vif *vif,
530 				    struct ieee80211_sta *sta)
531 {
532 	struct ath_common *common = ath9k_hw_common(priv->ah);
533 	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
534 	struct ath9k_htc_sta *ista;
535 	int ret;
536 	u8 cmd_rsp, sta_idx;
537 
538 	if (sta) {
539 		ista = (struct ath9k_htc_sta *) sta->drv_priv;
540 		sta_idx = ista->index;
541 	} else {
542 		sta_idx = priv->vif_sta_pos[avp->index];
543 	}
544 
545 	WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
546 	if (ret) {
547 		if (sta)
548 			ath_err(common,
549 				"Unable to remove station entry for: %pM\n",
550 				sta->addr);
551 		return ret;
552 	}
553 
554 	if (sta) {
555 		ath_dbg(common, CONFIG,
556 			"Removed a station entry for: %pM (idx: %d)\n",
557 			sta->addr, sta_idx);
558 	} else {
559 		ath_dbg(common, CONFIG,
560 			"Removed a station entry for VIF %d (idx: %d)\n",
561 			avp->index, sta_idx);
562 	}
563 
564 	priv->sta_slot &= ~(1 << sta_idx);
565 	priv->nstations--;
566 
567 	return 0;
568 }
569 
570 int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
571 				u8 enable_coex)
572 {
573 	struct ath9k_htc_cap_target tcap;
574 	int ret;
575 	u8 cmd_rsp;
576 
577 	memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
578 
579 	tcap.ampdu_limit = cpu_to_be32(0xffff);
580 	tcap.ampdu_subframes = 0xff;
581 	tcap.enable_coex = enable_coex;
582 	tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
583 
584 	WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
585 
586 	return ret;
587 }
588 
589 static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
590 				 struct ieee80211_sta *sta,
591 				 struct ath9k_htc_target_rate *trate)
592 {
593 	struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
594 	struct ieee80211_supported_band *sband;
595 	u32 caps = 0;
596 	int i, j;
597 
598 	sband = priv->hw->wiphy->bands[priv->hw->conf.chandef.chan->band];
599 
600 	for (i = 0, j = 0; i < sband->n_bitrates; i++) {
601 		if (sta->supp_rates[sband->band] & BIT(i)) {
602 			trate->rates.legacy_rates.rs_rates[j]
603 				= (sband->bitrates[i].bitrate * 2) / 10;
604 			j++;
605 		}
606 	}
607 	trate->rates.legacy_rates.rs_nrates = j;
608 
609 	if (sta->ht_cap.ht_supported) {
610 		for (i = 0, j = 0; i < 77; i++) {
611 			if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
612 				trate->rates.ht_rates.rs_rates[j++] = i;
613 			if (j == ATH_HTC_RATE_MAX)
614 				break;
615 		}
616 		trate->rates.ht_rates.rs_nrates = j;
617 
618 		caps = WLAN_RC_HT_FLAG;
619 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
620 			caps |= ATH_RC_TX_STBC_FLAG;
621 		if (sta->ht_cap.mcs.rx_mask[1])
622 			caps |= WLAN_RC_DS_FLAG;
623 		if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
624 		     (conf_is_ht40(&priv->hw->conf)))
625 			caps |= WLAN_RC_40_FLAG;
626 		if (conf_is_ht40(&priv->hw->conf) &&
627 		    (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
628 			caps |= WLAN_RC_SGI_FLAG;
629 		else if (conf_is_ht20(&priv->hw->conf) &&
630 			 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
631 			caps |= WLAN_RC_SGI_FLAG;
632 	}
633 
634 	trate->sta_index = ista->index;
635 	trate->isnew = 1;
636 	trate->capflags = cpu_to_be32(caps);
637 }
638 
639 static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
640 				    struct ath9k_htc_target_rate *trate)
641 {
642 	struct ath_common *common = ath9k_hw_common(priv->ah);
643 	int ret;
644 	u8 cmd_rsp;
645 
646 	WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
647 	if (ret) {
648 		ath_err(common,
649 			"Unable to initialize Rate information on target\n");
650 	}
651 
652 	return ret;
653 }
654 
655 static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
656 				struct ieee80211_sta *sta)
657 {
658 	struct ath_common *common = ath9k_hw_common(priv->ah);
659 	struct ath9k_htc_target_rate trate;
660 	int ret;
661 
662 	memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
663 	ath9k_htc_setup_rate(priv, sta, &trate);
664 	ret = ath9k_htc_send_rate_cmd(priv, &trate);
665 	if (!ret)
666 		ath_dbg(common, CONFIG,
667 			"Updated target sta: %pM, rate caps: 0x%X\n",
668 			sta->addr, be32_to_cpu(trate.capflags));
669 }
670 
671 static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
672 				  struct ieee80211_vif *vif,
673 				  struct ieee80211_bss_conf *bss_conf)
674 {
675 	struct ath_common *common = ath9k_hw_common(priv->ah);
676 	struct ath9k_htc_target_rate trate;
677 	struct ieee80211_sta *sta;
678 	int ret;
679 
680 	memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
681 
682 	rcu_read_lock();
683 	sta = ieee80211_find_sta(vif, bss_conf->bssid);
684 	if (!sta) {
685 		rcu_read_unlock();
686 		return;
687 	}
688 	ath9k_htc_setup_rate(priv, sta, &trate);
689 	rcu_read_unlock();
690 
691 	ret = ath9k_htc_send_rate_cmd(priv, &trate);
692 	if (!ret)
693 		ath_dbg(common, CONFIG,
694 			"Updated target sta: %pM, rate caps: 0x%X\n",
695 			bss_conf->bssid, be32_to_cpu(trate.capflags));
696 }
697 
698 static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
699 				  struct ieee80211_vif *vif,
700 				  struct ieee80211_sta *sta,
701 				  enum ieee80211_ampdu_mlme_action action,
702 				  u16 tid)
703 {
704 	struct ath_common *common = ath9k_hw_common(priv->ah);
705 	struct ath9k_htc_target_aggr aggr;
706 	struct ath9k_htc_sta *ista;
707 	int ret = 0;
708 	u8 cmd_rsp;
709 
710 	if (tid >= ATH9K_HTC_MAX_TID)
711 		return -EINVAL;
712 
713 	memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
714 	ista = (struct ath9k_htc_sta *) sta->drv_priv;
715 
716 	aggr.sta_index = ista->index;
717 	aggr.tidno = tid & 0xf;
718 	aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
719 
720 	WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
721 	if (ret)
722 		ath_dbg(common, CONFIG,
723 			"Unable to %s TX aggregation for (%pM, %d)\n",
724 			(aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
725 	else
726 		ath_dbg(common, CONFIG,
727 			"%s TX aggregation for (%pM, %d)\n",
728 			(aggr.aggr_enable) ? "Starting" : "Stopping",
729 			sta->addr, tid);
730 
731 	spin_lock_bh(&priv->tx.tx_lock);
732 	ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
733 	spin_unlock_bh(&priv->tx.tx_lock);
734 
735 	return ret;
736 }
737 
738 /*******/
739 /* ANI */
740 /*******/
741 
742 void ath9k_htc_start_ani(struct ath9k_htc_priv *priv)
743 {
744 	struct ath_common *common = ath9k_hw_common(priv->ah);
745 	unsigned long timestamp = jiffies_to_msecs(jiffies);
746 
747 	common->ani.longcal_timer = timestamp;
748 	common->ani.shortcal_timer = timestamp;
749 	common->ani.checkani_timer = timestamp;
750 
751 	set_bit(ATH_OP_ANI_RUN, &common->op_flags);
752 
753 	ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
754 				     msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
755 }
756 
757 void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv)
758 {
759 	struct ath_common *common = ath9k_hw_common(priv->ah);
760 	cancel_delayed_work_sync(&priv->ani_work);
761 	clear_bit(ATH_OP_ANI_RUN, &common->op_flags);
762 }
763 
764 void ath9k_htc_ani_work(struct work_struct *work)
765 {
766 	struct ath9k_htc_priv *priv =
767 		container_of(work, struct ath9k_htc_priv, ani_work.work);
768 	struct ath_hw *ah = priv->ah;
769 	struct ath_common *common = ath9k_hw_common(ah);
770 	bool longcal = false;
771 	bool shortcal = false;
772 	bool aniflag = false;
773 	unsigned int timestamp = jiffies_to_msecs(jiffies);
774 	u32 cal_interval, short_cal_interval;
775 
776 	short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
777 		ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
778 
779 	/* Only calibrate if awake */
780 	if (ah->power_mode != ATH9K_PM_AWAKE)
781 		goto set_timer;
782 
783 	/* Long calibration runs independently of short calibration. */
784 	if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
785 		longcal = true;
786 		ath_dbg(common, ANI, "longcal @%lu\n", jiffies);
787 		common->ani.longcal_timer = timestamp;
788 	}
789 
790 	/* Short calibration applies only while caldone is false */
791 	if (!common->ani.caldone) {
792 		if ((timestamp - common->ani.shortcal_timer) >=
793 		    short_cal_interval) {
794 			shortcal = true;
795 			ath_dbg(common, ANI, "shortcal @%lu\n", jiffies);
796 			common->ani.shortcal_timer = timestamp;
797 			common->ani.resetcal_timer = timestamp;
798 		}
799 	} else {
800 		if ((timestamp - common->ani.resetcal_timer) >=
801 		    ATH_RESTART_CALINTERVAL) {
802 			common->ani.caldone = ath9k_hw_reset_calvalid(ah);
803 			if (common->ani.caldone)
804 				common->ani.resetcal_timer = timestamp;
805 		}
806 	}
807 
808 	/* Verify whether we must check ANI */
809 	if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
810 		aniflag = true;
811 		common->ani.checkani_timer = timestamp;
812 	}
813 
814 	/* Skip all processing if there's nothing to do. */
815 	if (longcal || shortcal || aniflag) {
816 
817 		ath9k_htc_ps_wakeup(priv);
818 
819 		/* Call ANI routine if necessary */
820 		if (aniflag)
821 			ath9k_hw_ani_monitor(ah, ah->curchan);
822 
823 		/* Perform calibration if necessary */
824 		if (longcal || shortcal)
825 			common->ani.caldone =
826 				ath9k_hw_calibrate(ah, ah->curchan,
827 						   ah->rxchainmask, longcal);
828 
829 		ath9k_htc_ps_restore(priv);
830 	}
831 
832 set_timer:
833 	/*
834 	* Set timer interval based on previous results.
835 	* The interval must be the shortest necessary to satisfy ANI,
836 	* short calibration and long calibration.
837 	*/
838 	cal_interval = ATH_LONG_CALINTERVAL;
839 	cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
840 	if (!common->ani.caldone)
841 		cal_interval = min(cal_interval, (u32)short_cal_interval);
842 
843 	ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
844 				     msecs_to_jiffies(cal_interval));
845 }
846 
847 /**********************/
848 /* mac80211 Callbacks */
849 /**********************/
850 
851 static void ath9k_htc_tx(struct ieee80211_hw *hw,
852 			 struct ieee80211_tx_control *control,
853 			 struct sk_buff *skb)
854 {
855 	struct ieee80211_hdr *hdr;
856 	struct ath9k_htc_priv *priv = hw->priv;
857 	struct ath_common *common = ath9k_hw_common(priv->ah);
858 	int padpos, padsize, ret, slot;
859 
860 	hdr = (struct ieee80211_hdr *) skb->data;
861 
862 	/* Add the padding after the header if this is not already done */
863 	padpos = ieee80211_hdrlen(hdr->frame_control);
864 	padsize = padpos & 3;
865 	if (padsize && skb->len > padpos) {
866 		if (skb_headroom(skb) < padsize) {
867 			ath_dbg(common, XMIT, "No room for padding\n");
868 			goto fail_tx;
869 		}
870 		skb_push(skb, padsize);
871 		memmove(skb->data, skb->data + padsize, padpos);
872 	}
873 
874 	slot = ath9k_htc_tx_get_slot(priv);
875 	if (slot < 0) {
876 		ath_dbg(common, XMIT, "No free TX slot\n");
877 		goto fail_tx;
878 	}
879 
880 	ret = ath9k_htc_tx_start(priv, control->sta, skb, slot, false);
881 	if (ret != 0) {
882 		ath_dbg(common, XMIT, "Tx failed\n");
883 		goto clear_slot;
884 	}
885 
886 	ath9k_htc_check_stop_queues(priv);
887 
888 	return;
889 
890 clear_slot:
891 	ath9k_htc_tx_clear_slot(priv, slot);
892 fail_tx:
893 	dev_kfree_skb_any(skb);
894 }
895 
896 static int ath9k_htc_start(struct ieee80211_hw *hw)
897 {
898 	struct ath9k_htc_priv *priv = hw->priv;
899 	struct ath_hw *ah = priv->ah;
900 	struct ath_common *common = ath9k_hw_common(ah);
901 	struct ieee80211_channel *curchan = hw->conf.chandef.chan;
902 	struct ath9k_channel *init_channel;
903 	int ret = 0;
904 	enum htc_phymode mode;
905 	__be16 htc_mode;
906 	u8 cmd_rsp;
907 
908 	mutex_lock(&priv->mutex);
909 
910 	ath_dbg(common, CONFIG,
911 		"Starting driver with initial channel: %d MHz\n",
912 		curchan->center_freq);
913 
914 	/* Ensure that HW is awake before flushing RX */
915 	ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
916 	WMI_CMD(WMI_FLUSH_RECV_CMDID);
917 
918 	/* setup initial channel */
919 	init_channel = ath9k_cmn_get_channel(hw, ah, &hw->conf.chandef);
920 
921 	ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
922 	if (ret) {
923 		ath_err(common,
924 			"Unable to reset hardware; reset status %d (freq %u MHz)\n",
925 			ret, curchan->center_freq);
926 		mutex_unlock(&priv->mutex);
927 		return ret;
928 	}
929 
930 	ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
931 			       &priv->curtxpow);
932 
933 	mode = ath9k_htc_get_curmode(priv, init_channel);
934 	htc_mode = cpu_to_be16(mode);
935 	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
936 	WMI_CMD(WMI_ATH_INIT_CMDID);
937 	WMI_CMD(WMI_START_RECV_CMDID);
938 
939 	ath9k_host_rx_init(priv);
940 
941 	ret = ath9k_htc_update_cap_target(priv, 0);
942 	if (ret)
943 		ath_dbg(common, CONFIG,
944 			"Failed to update capability in target\n");
945 
946 	clear_bit(ATH_OP_INVALID, &common->op_flags);
947 	htc_start(priv->htc);
948 
949 	spin_lock_bh(&priv->tx.tx_lock);
950 	priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
951 	spin_unlock_bh(&priv->tx.tx_lock);
952 
953 	ieee80211_wake_queues(hw);
954 
955 	mod_timer(&priv->tx.cleanup_timer,
956 		  jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
957 
958 	ath9k_htc_start_btcoex(priv);
959 
960 	mutex_unlock(&priv->mutex);
961 
962 	return ret;
963 }
964 
965 static void ath9k_htc_stop(struct ieee80211_hw *hw)
966 {
967 	struct ath9k_htc_priv *priv = hw->priv;
968 	struct ath_hw *ah = priv->ah;
969 	struct ath_common *common = ath9k_hw_common(ah);
970 	int ret __attribute__ ((unused));
971 	u8 cmd_rsp;
972 
973 	mutex_lock(&priv->mutex);
974 
975 	if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
976 		ath_dbg(common, ANY, "Device not present\n");
977 		mutex_unlock(&priv->mutex);
978 		return;
979 	}
980 
981 	ath9k_htc_ps_wakeup(priv);
982 
983 	WMI_CMD(WMI_DISABLE_INTR_CMDID);
984 	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
985 	WMI_CMD(WMI_STOP_RECV_CMDID);
986 
987 	tasklet_kill(&priv->rx_tasklet);
988 
989 	del_timer_sync(&priv->tx.cleanup_timer);
990 	ath9k_htc_tx_drain(priv);
991 	ath9k_wmi_event_drain(priv);
992 
993 	mutex_unlock(&priv->mutex);
994 
995 	/* Cancel all the running timers/work .. */
996 	cancel_work_sync(&priv->fatal_work);
997 	cancel_work_sync(&priv->ps_work);
998 
999 #ifdef CONFIG_MAC80211_LEDS
1000 	cancel_work_sync(&priv->led_work);
1001 #endif
1002 	ath9k_htc_stop_ani(priv);
1003 
1004 	mutex_lock(&priv->mutex);
1005 
1006 	ath9k_htc_stop_btcoex(priv);
1007 
1008 	/* Remove a monitor interface if it's present. */
1009 	if (priv->ah->is_monitoring)
1010 		ath9k_htc_remove_monitor_interface(priv);
1011 
1012 	ath9k_hw_phy_disable(ah);
1013 	ath9k_hw_disable(ah);
1014 	ath9k_htc_ps_restore(priv);
1015 	ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1016 
1017 	set_bit(ATH_OP_INVALID, &common->op_flags);
1018 
1019 	ath_dbg(common, CONFIG, "Driver halt\n");
1020 	mutex_unlock(&priv->mutex);
1021 }
1022 
1023 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1024 				   struct ieee80211_vif *vif)
1025 {
1026 	struct ath9k_htc_priv *priv = hw->priv;
1027 	struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1028 	struct ath_common *common = ath9k_hw_common(priv->ah);
1029 	struct ath9k_htc_target_vif hvif;
1030 	int ret = 0;
1031 	u8 cmd_rsp;
1032 
1033 	mutex_lock(&priv->mutex);
1034 
1035 	ath9k_htc_ps_wakeup(priv);
1036 	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1037 	memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1038 
1039 	switch (vif->type) {
1040 	case NL80211_IFTYPE_STATION:
1041 		hvif.opmode = HTC_M_STA;
1042 		break;
1043 	case NL80211_IFTYPE_ADHOC:
1044 		hvif.opmode = HTC_M_IBSS;
1045 		break;
1046 	case NL80211_IFTYPE_AP:
1047 		hvif.opmode = HTC_M_HOSTAP;
1048 		break;
1049 	case NL80211_IFTYPE_MESH_POINT:
1050 		hvif.opmode = HTC_M_WDS;	/* close enough */
1051 		break;
1052 	default:
1053 		ath_err(common,
1054 			"Interface type %d not yet supported\n", vif->type);
1055 		ret = -EOPNOTSUPP;
1056 		goto out;
1057 	}
1058 
1059 	/* Index starts from zero on the target */
1060 	avp->index = hvif.index = ffz(priv->vif_slot);
1061 	hvif.rtsthreshold = cpu_to_be16(2304);
1062 	WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1063 	if (ret)
1064 		goto out;
1065 
1066 	/*
1067 	 * We need a node in target to tx mgmt frames
1068 	 * before association.
1069 	 */
1070 	ret = ath9k_htc_add_station(priv, vif, NULL);
1071 	if (ret) {
1072 		WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1073 		goto out;
1074 	}
1075 
1076 	ath9k_htc_set_mac_bssid_mask(priv, vif);
1077 
1078 	priv->vif_slot |= (1 << avp->index);
1079 	priv->nvifs++;
1080 
1081 	INC_VIF(priv, vif->type);
1082 
1083 	if ((vif->type == NL80211_IFTYPE_AP) ||
1084 	    (vif->type == NL80211_IFTYPE_MESH_POINT) ||
1085 	    (vif->type == NL80211_IFTYPE_ADHOC))
1086 		ath9k_htc_assign_bslot(priv, vif);
1087 
1088 	ath9k_htc_set_opmode(priv);
1089 
1090 	if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1091 	    !test_bit(ATH_OP_ANI_RUN, &common->op_flags)) {
1092 		ath9k_hw_set_tsfadjust(priv->ah, true);
1093 		ath9k_htc_start_ani(priv);
1094 	}
1095 
1096 	ath_dbg(common, CONFIG, "Attach a VIF of type: %d at idx: %d\n",
1097 		vif->type, avp->index);
1098 
1099 out:
1100 	ath9k_htc_ps_restore(priv);
1101 	mutex_unlock(&priv->mutex);
1102 
1103 	return ret;
1104 }
1105 
1106 static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1107 				       struct ieee80211_vif *vif)
1108 {
1109 	struct ath9k_htc_priv *priv = hw->priv;
1110 	struct ath_common *common = ath9k_hw_common(priv->ah);
1111 	struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1112 	struct ath9k_htc_target_vif hvif;
1113 	int ret = 0;
1114 	u8 cmd_rsp;
1115 
1116 	mutex_lock(&priv->mutex);
1117 	ath9k_htc_ps_wakeup(priv);
1118 
1119 	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1120 	memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1121 	hvif.index = avp->index;
1122 	WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1123 	if (ret) {
1124 		ath_err(common, "Unable to remove interface at idx: %d\n",
1125 			avp->index);
1126 	}
1127 	priv->nvifs--;
1128 	priv->vif_slot &= ~(1 << avp->index);
1129 
1130 	ath9k_htc_remove_station(priv, vif, NULL);
1131 
1132 	DEC_VIF(priv, vif->type);
1133 
1134 	if ((vif->type == NL80211_IFTYPE_AP) ||
1135 	     vif->type == NL80211_IFTYPE_MESH_POINT ||
1136 	    (vif->type == NL80211_IFTYPE_ADHOC))
1137 		ath9k_htc_remove_bslot(priv, vif);
1138 
1139 	ath9k_htc_set_opmode(priv);
1140 
1141 	ath9k_htc_set_mac_bssid_mask(priv, vif);
1142 
1143 	/*
1144 	 * Stop ANI only if there are no associated station interfaces.
1145 	 */
1146 	if ((vif->type == NL80211_IFTYPE_AP) && (priv->num_ap_vif == 0)) {
1147 		priv->rearm_ani = false;
1148 		ieee80211_iterate_active_interfaces_atomic(
1149 			priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1150 			ath9k_htc_vif_iter, priv);
1151 		if (!priv->rearm_ani)
1152 			ath9k_htc_stop_ani(priv);
1153 	}
1154 
1155 	ath_dbg(common, CONFIG, "Detach Interface at idx: %d\n", avp->index);
1156 
1157 	ath9k_htc_ps_restore(priv);
1158 	mutex_unlock(&priv->mutex);
1159 }
1160 
1161 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1162 {
1163 	struct ath9k_htc_priv *priv = hw->priv;
1164 	struct ath_common *common = ath9k_hw_common(priv->ah);
1165 	struct ieee80211_conf *conf = &hw->conf;
1166 	bool chip_reset = false;
1167 	int ret = 0;
1168 
1169 	mutex_lock(&priv->mutex);
1170 	ath9k_htc_ps_wakeup(priv);
1171 
1172 	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1173 		mutex_lock(&priv->htc_pm_lock);
1174 
1175 		priv->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1176 		if (!priv->ps_idle)
1177 			chip_reset = true;
1178 
1179 		mutex_unlock(&priv->htc_pm_lock);
1180 	}
1181 
1182 	/*
1183 	 * Monitor interface should be added before
1184 	 * IEEE80211_CONF_CHANGE_CHANNEL is handled.
1185 	 */
1186 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1187 		if ((conf->flags & IEEE80211_CONF_MONITOR) &&
1188 		    !priv->ah->is_monitoring)
1189 			ath9k_htc_add_monitor_interface(priv);
1190 		else if (priv->ah->is_monitoring)
1191 			ath9k_htc_remove_monitor_interface(priv);
1192 	}
1193 
1194 	if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || chip_reset) {
1195 		struct ieee80211_channel *curchan = hw->conf.chandef.chan;
1196 		int pos = curchan->hw_value;
1197 
1198 		ath_dbg(common, CONFIG, "Set channel: %d MHz\n",
1199 			curchan->center_freq);
1200 
1201 		ath9k_cmn_get_channel(hw, priv->ah, &hw->conf.chandef);
1202 		if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1203 			ath_err(common, "Unable to set channel\n");
1204 			ret = -EINVAL;
1205 			goto out;
1206 		}
1207 
1208 	}
1209 
1210 	if (changed & IEEE80211_CONF_CHANGE_PS) {
1211 		if (conf->flags & IEEE80211_CONF_PS) {
1212 			ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1213 			priv->ps_enabled = true;
1214 		} else {
1215 			priv->ps_enabled = false;
1216 			cancel_work_sync(&priv->ps_work);
1217 			ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1218 		}
1219 	}
1220 
1221 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
1222 		priv->txpowlimit = 2 * conf->power_level;
1223 		ath9k_cmn_update_txpow(priv->ah, priv->curtxpow,
1224 				       priv->txpowlimit, &priv->curtxpow);
1225 	}
1226 
1227 out:
1228 	ath9k_htc_ps_restore(priv);
1229 	mutex_unlock(&priv->mutex);
1230 	return ret;
1231 }
1232 
1233 #define SUPPORTED_FILTERS			\
1234 	(FIF_PROMISC_IN_BSS |			\
1235 	FIF_ALLMULTI |				\
1236 	FIF_CONTROL |				\
1237 	FIF_PSPOLL |				\
1238 	FIF_OTHER_BSS |				\
1239 	FIF_BCN_PRBRESP_PROMISC |		\
1240 	FIF_PROBE_REQ |				\
1241 	FIF_FCSFAIL)
1242 
1243 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1244 				       unsigned int changed_flags,
1245 				       unsigned int *total_flags,
1246 				       u64 multicast)
1247 {
1248 	struct ath9k_htc_priv *priv = hw->priv;
1249 	struct ath_common *common = ath9k_hw_common(priv->ah);
1250 	u32 rfilt;
1251 
1252 	mutex_lock(&priv->mutex);
1253 	changed_flags &= SUPPORTED_FILTERS;
1254 	*total_flags &= SUPPORTED_FILTERS;
1255 
1256 	if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
1257 		ath_dbg(ath9k_hw_common(priv->ah), ANY,
1258 			"Unable to configure filter on invalid state\n");
1259 		mutex_unlock(&priv->mutex);
1260 		return;
1261 	}
1262 	ath9k_htc_ps_wakeup(priv);
1263 
1264 	priv->rxfilter = *total_flags;
1265 	rfilt = ath9k_htc_calcrxfilter(priv);
1266 	ath9k_hw_setrxfilter(priv->ah, rfilt);
1267 
1268 	ath_dbg(ath9k_hw_common(priv->ah), CONFIG, "Set HW RX filter: 0x%x\n",
1269 		rfilt);
1270 
1271 	ath9k_htc_ps_restore(priv);
1272 	mutex_unlock(&priv->mutex);
1273 }
1274 
1275 static void ath9k_htc_sta_rc_update_work(struct work_struct *work)
1276 {
1277 	struct ath9k_htc_sta *ista =
1278 	    container_of(work, struct ath9k_htc_sta, rc_update_work);
1279 	struct ieee80211_sta *sta =
1280 	    container_of((void *)ista, struct ieee80211_sta, drv_priv);
1281 	struct ath9k_htc_priv *priv = ista->htc_priv;
1282 	struct ath_common *common = ath9k_hw_common(priv->ah);
1283 	struct ath9k_htc_target_rate trate;
1284 
1285 	mutex_lock(&priv->mutex);
1286 	ath9k_htc_ps_wakeup(priv);
1287 
1288 	memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
1289 	ath9k_htc_setup_rate(priv, sta, &trate);
1290 	if (!ath9k_htc_send_rate_cmd(priv, &trate))
1291 		ath_dbg(common, CONFIG,
1292 			"Supported rates for sta: %pM updated, rate caps: 0x%X\n",
1293 			sta->addr, be32_to_cpu(trate.capflags));
1294 	else
1295 		ath_dbg(common, CONFIG,
1296 			"Unable to update supported rates for sta: %pM\n",
1297 			sta->addr);
1298 
1299 	ath9k_htc_ps_restore(priv);
1300 	mutex_unlock(&priv->mutex);
1301 }
1302 
1303 static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1304 			     struct ieee80211_vif *vif,
1305 			     struct ieee80211_sta *sta)
1306 {
1307 	struct ath9k_htc_priv *priv = hw->priv;
1308 	struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1309 	int ret;
1310 
1311 	mutex_lock(&priv->mutex);
1312 	ath9k_htc_ps_wakeup(priv);
1313 	ret = ath9k_htc_add_station(priv, vif, sta);
1314 	if (!ret) {
1315 		INIT_WORK(&ista->rc_update_work, ath9k_htc_sta_rc_update_work);
1316 		ista->htc_priv = priv;
1317 		ath9k_htc_init_rate(priv, sta);
1318 	}
1319 	ath9k_htc_ps_restore(priv);
1320 	mutex_unlock(&priv->mutex);
1321 
1322 	return ret;
1323 }
1324 
1325 static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1326 				struct ieee80211_vif *vif,
1327 				struct ieee80211_sta *sta)
1328 {
1329 	struct ath9k_htc_priv *priv = hw->priv;
1330 	struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1331 	int ret;
1332 
1333 	cancel_work_sync(&ista->rc_update_work);
1334 
1335 	mutex_lock(&priv->mutex);
1336 	ath9k_htc_ps_wakeup(priv);
1337 	htc_sta_drain(priv->htc, ista->index);
1338 	ret = ath9k_htc_remove_station(priv, vif, sta);
1339 	ath9k_htc_ps_restore(priv);
1340 	mutex_unlock(&priv->mutex);
1341 
1342 	return ret;
1343 }
1344 
1345 static void ath9k_htc_sta_rc_update(struct ieee80211_hw *hw,
1346 				    struct ieee80211_vif *vif,
1347 				    struct ieee80211_sta *sta, u32 changed)
1348 {
1349 	struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1350 
1351 	if (!(changed & IEEE80211_RC_SUPP_RATES_CHANGED))
1352 		return;
1353 
1354 	schedule_work(&ista->rc_update_work);
1355 }
1356 
1357 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw,
1358 			     struct ieee80211_vif *vif, u16 queue,
1359 			     const struct ieee80211_tx_queue_params *params)
1360 {
1361 	struct ath9k_htc_priv *priv = hw->priv;
1362 	struct ath_common *common = ath9k_hw_common(priv->ah);
1363 	struct ath9k_tx_queue_info qi;
1364 	int ret = 0, qnum;
1365 
1366 	if (queue >= IEEE80211_NUM_ACS)
1367 		return 0;
1368 
1369 	mutex_lock(&priv->mutex);
1370 	ath9k_htc_ps_wakeup(priv);
1371 
1372 	memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1373 
1374 	qi.tqi_aifs = params->aifs;
1375 	qi.tqi_cwmin = params->cw_min;
1376 	qi.tqi_cwmax = params->cw_max;
1377 	qi.tqi_burstTime = params->txop * 32;
1378 
1379 	qnum = get_hw_qnum(queue, priv->hwq_map);
1380 
1381 	ath_dbg(common, CONFIG,
1382 		"Configure tx [queue/hwq] [%d/%d],  aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1383 		queue, qnum, params->aifs, params->cw_min,
1384 		params->cw_max, params->txop);
1385 
1386 	ret = ath_htc_txq_update(priv, qnum, &qi);
1387 	if (ret) {
1388 		ath_err(common, "TXQ Update failed\n");
1389 		goto out;
1390 	}
1391 
1392 	if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1393 	    (qnum == priv->hwq_map[IEEE80211_AC_BE]))
1394 		    ath9k_htc_beaconq_config(priv);
1395 out:
1396 	ath9k_htc_ps_restore(priv);
1397 	mutex_unlock(&priv->mutex);
1398 
1399 	return ret;
1400 }
1401 
1402 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1403 			     enum set_key_cmd cmd,
1404 			     struct ieee80211_vif *vif,
1405 			     struct ieee80211_sta *sta,
1406 			     struct ieee80211_key_conf *key)
1407 {
1408 	struct ath9k_htc_priv *priv = hw->priv;
1409 	struct ath_common *common = ath9k_hw_common(priv->ah);
1410 	int ret = 0;
1411 
1412 	if (htc_modparam_nohwcrypt)
1413 		return -ENOSPC;
1414 
1415 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
1416 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
1417 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1418 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1419 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1420 		/*
1421 		 * For now, disable hw crypto for the RSN IBSS group keys. This
1422 		 * could be optimized in the future to use a modified key cache
1423 		 * design to support per-STA RX GTK, but until that gets
1424 		 * implemented, use of software crypto for group addressed
1425 		 * frames is a acceptable to allow RSN IBSS to be used.
1426 		 */
1427 		return -EOPNOTSUPP;
1428 	}
1429 
1430 	mutex_lock(&priv->mutex);
1431 	ath_dbg(common, CONFIG, "Set HW Key\n");
1432 	ath9k_htc_ps_wakeup(priv);
1433 
1434 	switch (cmd) {
1435 	case SET_KEY:
1436 		ret = ath_key_config(common, vif, sta, key);
1437 		if (ret >= 0) {
1438 			key->hw_key_idx = ret;
1439 			/* push IV and Michael MIC generation to stack */
1440 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1441 			if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1442 				key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1443 			if (priv->ah->sw_mgmt_crypto &&
1444 			    key->cipher == WLAN_CIPHER_SUITE_CCMP)
1445 				key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1446 			ret = 0;
1447 		}
1448 		break;
1449 	case DISABLE_KEY:
1450 		ath_key_delete(common, key);
1451 		break;
1452 	default:
1453 		ret = -EINVAL;
1454 	}
1455 
1456 	ath9k_htc_ps_restore(priv);
1457 	mutex_unlock(&priv->mutex);
1458 
1459 	return ret;
1460 }
1461 
1462 static void ath9k_htc_set_bssid(struct ath9k_htc_priv *priv)
1463 {
1464 	struct ath_common *common = ath9k_hw_common(priv->ah);
1465 
1466 	ath9k_hw_write_associd(priv->ah);
1467 	ath_dbg(common, CONFIG, "BSSID: %pM aid: 0x%x\n",
1468 		common->curbssid, common->curaid);
1469 }
1470 
1471 static void ath9k_htc_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1472 {
1473 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
1474 	struct ath_common *common = ath9k_hw_common(priv->ah);
1475 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1476 
1477 	if ((vif->type == NL80211_IFTYPE_STATION) && bss_conf->assoc) {
1478 		common->curaid = bss_conf->aid;
1479 		common->last_rssi = ATH_RSSI_DUMMY_MARKER;
1480 		memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1481 		set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1482 	}
1483 }
1484 
1485 static void ath9k_htc_choose_set_bssid(struct ath9k_htc_priv *priv)
1486 {
1487 	if (priv->num_sta_assoc_vif == 1) {
1488 		ieee80211_iterate_active_interfaces_atomic(
1489 			priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1490 			ath9k_htc_bss_iter, priv);
1491 		ath9k_htc_set_bssid(priv);
1492 	}
1493 }
1494 
1495 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1496 				       struct ieee80211_vif *vif,
1497 				       struct ieee80211_bss_conf *bss_conf,
1498 				       u32 changed)
1499 {
1500 	struct ath9k_htc_priv *priv = hw->priv;
1501 	struct ath_hw *ah = priv->ah;
1502 	struct ath_common *common = ath9k_hw_common(ah);
1503 	int slottime;
1504 
1505 	mutex_lock(&priv->mutex);
1506 	ath9k_htc_ps_wakeup(priv);
1507 
1508 	if (changed & BSS_CHANGED_ASSOC) {
1509 		ath_dbg(common, CONFIG, "BSS Changed ASSOC %d\n",
1510 			bss_conf->assoc);
1511 
1512 		bss_conf->assoc ?
1513 			priv->num_sta_assoc_vif++ : priv->num_sta_assoc_vif--;
1514 
1515 		if (!bss_conf->assoc)
1516 			clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1517 
1518 		if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1519 			ath9k_htc_choose_set_bssid(priv);
1520 			if (bss_conf->assoc && (priv->num_sta_assoc_vif == 1))
1521 				ath9k_htc_start_ani(priv);
1522 			else if (priv->num_sta_assoc_vif == 0)
1523 				ath9k_htc_stop_ani(priv);
1524 		}
1525 	}
1526 
1527 	if (changed & BSS_CHANGED_IBSS) {
1528 		if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
1529 			common->curaid = bss_conf->aid;
1530 			memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1531 			ath9k_htc_set_bssid(priv);
1532 		}
1533 	}
1534 
1535 	if ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon) {
1536 		ath_dbg(common, CONFIG, "Beacon enabled for BSS: %pM\n",
1537 			bss_conf->bssid);
1538 		ath9k_htc_set_tsfadjust(priv, vif);
1539 		priv->cur_beacon_conf.enable_beacon = 1;
1540 		ath9k_htc_beacon_config(priv, vif);
1541 	}
1542 
1543 	if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon) {
1544 		/*
1545 		 * Disable SWBA interrupt only if there are no
1546 		 * concurrent AP/mesh or IBSS interfaces.
1547 		 */
1548 		if ((priv->num_ap_vif + priv->num_mbss_vif <= 1) ||
1549 		     priv->num_ibss_vif) {
1550 			ath_dbg(common, CONFIG,
1551 				"Beacon disabled for BSS: %pM\n",
1552 				bss_conf->bssid);
1553 			priv->cur_beacon_conf.enable_beacon = 0;
1554 			ath9k_htc_beacon_config(priv, vif);
1555 		}
1556 	}
1557 
1558 	if (changed & BSS_CHANGED_BEACON_INT) {
1559 		/*
1560 		 * Reset the HW TSF for the first AP or mesh interface.
1561 		 */
1562 		if (priv->nvifs == 1 &&
1563 		    ((priv->ah->opmode == NL80211_IFTYPE_AP &&
1564 		      vif->type == NL80211_IFTYPE_AP &&
1565 		      priv->num_ap_vif == 1) ||
1566 		    (priv->ah->opmode == NL80211_IFTYPE_MESH_POINT &&
1567 		      vif->type == NL80211_IFTYPE_MESH_POINT &&
1568 		      priv->num_mbss_vif == 1))) {
1569 			set_bit(OP_TSF_RESET, &priv->op_flags);
1570 		}
1571 		ath_dbg(common, CONFIG,
1572 			"Beacon interval changed for BSS: %pM\n",
1573 			bss_conf->bssid);
1574 		ath9k_htc_beacon_config(priv, vif);
1575 	}
1576 
1577 	if (changed & BSS_CHANGED_ERP_SLOT) {
1578 		if (bss_conf->use_short_slot)
1579 			slottime = 9;
1580 		else
1581 			slottime = 20;
1582 		if (vif->type == NL80211_IFTYPE_AP) {
1583 			/*
1584 			 * Defer update, so that connected stations can adjust
1585 			 * their settings at the same time.
1586 			 * See beacon.c for more details
1587 			 */
1588 			priv->beacon.slottime = slottime;
1589 			priv->beacon.updateslot = UPDATE;
1590 		} else {
1591 			ah->slottime = slottime;
1592 			ath9k_hw_init_global_settings(ah);
1593 		}
1594 	}
1595 
1596 	if (changed & BSS_CHANGED_HT)
1597 		ath9k_htc_update_rate(priv, vif, bss_conf);
1598 
1599 	ath9k_htc_ps_restore(priv);
1600 	mutex_unlock(&priv->mutex);
1601 }
1602 
1603 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw,
1604 			     struct ieee80211_vif *vif)
1605 {
1606 	struct ath9k_htc_priv *priv = hw->priv;
1607 	u64 tsf;
1608 
1609 	mutex_lock(&priv->mutex);
1610 	ath9k_htc_ps_wakeup(priv);
1611 	tsf = ath9k_hw_gettsf64(priv->ah);
1612 	ath9k_htc_ps_restore(priv);
1613 	mutex_unlock(&priv->mutex);
1614 
1615 	return tsf;
1616 }
1617 
1618 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw,
1619 			      struct ieee80211_vif *vif, u64 tsf)
1620 {
1621 	struct ath9k_htc_priv *priv = hw->priv;
1622 
1623 	mutex_lock(&priv->mutex);
1624 	ath9k_htc_ps_wakeup(priv);
1625 	ath9k_hw_settsf64(priv->ah, tsf);
1626 	ath9k_htc_ps_restore(priv);
1627 	mutex_unlock(&priv->mutex);
1628 }
1629 
1630 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw,
1631 				struct ieee80211_vif *vif)
1632 {
1633 	struct ath9k_htc_priv *priv = hw->priv;
1634 
1635 	mutex_lock(&priv->mutex);
1636 	ath9k_htc_ps_wakeup(priv);
1637 	ath9k_hw_reset_tsf(priv->ah);
1638 	ath9k_htc_ps_restore(priv);
1639 	mutex_unlock(&priv->mutex);
1640 }
1641 
1642 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1643 				  struct ieee80211_vif *vif,
1644 				  enum ieee80211_ampdu_mlme_action action,
1645 				  struct ieee80211_sta *sta,
1646 				  u16 tid, u16 *ssn, u8 buf_size)
1647 {
1648 	struct ath9k_htc_priv *priv = hw->priv;
1649 	struct ath9k_htc_sta *ista;
1650 	int ret = 0;
1651 
1652 	mutex_lock(&priv->mutex);
1653 	ath9k_htc_ps_wakeup(priv);
1654 
1655 	switch (action) {
1656 	case IEEE80211_AMPDU_RX_START:
1657 		break;
1658 	case IEEE80211_AMPDU_RX_STOP:
1659 		break;
1660 	case IEEE80211_AMPDU_TX_START:
1661 		ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1662 		if (!ret)
1663 			ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1664 		break;
1665 	case IEEE80211_AMPDU_TX_STOP_CONT:
1666 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1667 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1668 		ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1669 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1670 		break;
1671 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1672 		ista = (struct ath9k_htc_sta *) sta->drv_priv;
1673 		spin_lock_bh(&priv->tx.tx_lock);
1674 		ista->tid_state[tid] = AGGR_OPERATIONAL;
1675 		spin_unlock_bh(&priv->tx.tx_lock);
1676 		break;
1677 	default:
1678 		ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1679 	}
1680 
1681 	ath9k_htc_ps_restore(priv);
1682 	mutex_unlock(&priv->mutex);
1683 
1684 	return ret;
1685 }
1686 
1687 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1688 {
1689 	struct ath9k_htc_priv *priv = hw->priv;
1690 	struct ath_common *common = ath9k_hw_common(priv->ah);
1691 
1692 	mutex_lock(&priv->mutex);
1693 	spin_lock_bh(&priv->beacon_lock);
1694 	set_bit(ATH_OP_SCANNING, &common->op_flags);
1695 	spin_unlock_bh(&priv->beacon_lock);
1696 	cancel_work_sync(&priv->ps_work);
1697 	ath9k_htc_stop_ani(priv);
1698 	mutex_unlock(&priv->mutex);
1699 }
1700 
1701 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1702 {
1703 	struct ath9k_htc_priv *priv = hw->priv;
1704 	struct ath_common *common = ath9k_hw_common(priv->ah);
1705 
1706 	mutex_lock(&priv->mutex);
1707 	spin_lock_bh(&priv->beacon_lock);
1708 	clear_bit(ATH_OP_SCANNING, &common->op_flags);
1709 	spin_unlock_bh(&priv->beacon_lock);
1710 	ath9k_htc_ps_wakeup(priv);
1711 	ath9k_htc_vif_reconfig(priv);
1712 	ath9k_htc_ps_restore(priv);
1713 	mutex_unlock(&priv->mutex);
1714 }
1715 
1716 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1717 {
1718 	return 0;
1719 }
1720 
1721 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1722 					 u8 coverage_class)
1723 {
1724 	struct ath9k_htc_priv *priv = hw->priv;
1725 
1726 	mutex_lock(&priv->mutex);
1727 	ath9k_htc_ps_wakeup(priv);
1728 	priv->ah->coverage_class = coverage_class;
1729 	ath9k_hw_init_global_settings(priv->ah);
1730 	ath9k_htc_ps_restore(priv);
1731 	mutex_unlock(&priv->mutex);
1732 }
1733 
1734 /*
1735  * Currently, this is used only for selecting the minimum rate
1736  * for management frames, rate selection for data frames remain
1737  * unaffected.
1738  */
1739 static int ath9k_htc_set_bitrate_mask(struct ieee80211_hw *hw,
1740 				      struct ieee80211_vif *vif,
1741 				      const struct cfg80211_bitrate_mask *mask)
1742 {
1743 	struct ath9k_htc_priv *priv = hw->priv;
1744 	struct ath_common *common = ath9k_hw_common(priv->ah);
1745 	struct ath9k_htc_target_rate_mask tmask;
1746 	struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1747 	int ret = 0;
1748 	u8 cmd_rsp;
1749 
1750 	memset(&tmask, 0, sizeof(struct ath9k_htc_target_rate_mask));
1751 
1752 	tmask.vif_index = avp->index;
1753 	tmask.band = IEEE80211_BAND_2GHZ;
1754 	tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_2GHZ].legacy);
1755 
1756 	WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1757 	if (ret) {
1758 		ath_err(common,
1759 			"Unable to set 2G rate mask for "
1760 			"interface at idx: %d\n", avp->index);
1761 		goto out;
1762 	}
1763 
1764 	tmask.band = IEEE80211_BAND_5GHZ;
1765 	tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_5GHZ].legacy);
1766 
1767 	WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1768 	if (ret) {
1769 		ath_err(common,
1770 			"Unable to set 5G rate mask for "
1771 			"interface at idx: %d\n", avp->index);
1772 		goto out;
1773 	}
1774 
1775 	ath_dbg(common, CONFIG, "Set bitrate masks: 0x%x, 0x%x\n",
1776 		mask->control[IEEE80211_BAND_2GHZ].legacy,
1777 		mask->control[IEEE80211_BAND_5GHZ].legacy);
1778 out:
1779 	return ret;
1780 }
1781 
1782 
1783 static int ath9k_htc_get_stats(struct ieee80211_hw *hw,
1784 			       struct ieee80211_low_level_stats *stats)
1785 {
1786 	struct ath9k_htc_priv *priv = hw->priv;
1787 	struct ath_hw *ah = priv->ah;
1788 	struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
1789 
1790 	stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
1791 	stats->dot11RTSFailureCount = mib_stats->rts_bad;
1792 	stats->dot11FCSErrorCount = mib_stats->fcs_bad;
1793 	stats->dot11RTSSuccessCount = mib_stats->rts_good;
1794 
1795 	return 0;
1796 }
1797 
1798 struct base_eep_header *ath9k_htc_get_eeprom_base(struct ath9k_htc_priv *priv)
1799 {
1800 	struct base_eep_header *pBase = NULL;
1801 	/*
1802 	 * This can be done since all the 3 EEPROM families have the
1803 	 * same base header upto a certain point, and we are interested in
1804 	 * the data only upto that point.
1805 	 */
1806 
1807 	if (AR_SREV_9271(priv->ah))
1808 		pBase = (struct base_eep_header *)
1809 			&priv->ah->eeprom.map4k.baseEepHeader;
1810 	else if (priv->ah->hw_version.usbdev == AR9280_USB)
1811 		pBase = (struct base_eep_header *)
1812 			&priv->ah->eeprom.def.baseEepHeader;
1813 	else if (priv->ah->hw_version.usbdev == AR9287_USB)
1814 		pBase = (struct base_eep_header *)
1815 			&priv->ah->eeprom.map9287.baseEepHeader;
1816 	return pBase;
1817 }
1818 
1819 
1820 static int ath9k_htc_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
1821 				 u32 *rx_ant)
1822 {
1823 	struct ath9k_htc_priv *priv = hw->priv;
1824 	struct base_eep_header *pBase = ath9k_htc_get_eeprom_base(priv);
1825 	if (pBase) {
1826 		*tx_ant = pBase->txMask;
1827 		*rx_ant = pBase->rxMask;
1828 	} else {
1829 		*tx_ant = 0;
1830 		*rx_ant = 0;
1831 	}
1832 	return 0;
1833 }
1834 
1835 struct ieee80211_ops ath9k_htc_ops = {
1836 	.tx                 = ath9k_htc_tx,
1837 	.start              = ath9k_htc_start,
1838 	.stop               = ath9k_htc_stop,
1839 	.add_interface      = ath9k_htc_add_interface,
1840 	.remove_interface   = ath9k_htc_remove_interface,
1841 	.config             = ath9k_htc_config,
1842 	.configure_filter   = ath9k_htc_configure_filter,
1843 	.sta_add            = ath9k_htc_sta_add,
1844 	.sta_remove         = ath9k_htc_sta_remove,
1845 	.conf_tx            = ath9k_htc_conf_tx,
1846 	.sta_rc_update      = ath9k_htc_sta_rc_update,
1847 	.bss_info_changed   = ath9k_htc_bss_info_changed,
1848 	.set_key            = ath9k_htc_set_key,
1849 	.get_tsf            = ath9k_htc_get_tsf,
1850 	.set_tsf            = ath9k_htc_set_tsf,
1851 	.reset_tsf          = ath9k_htc_reset_tsf,
1852 	.ampdu_action       = ath9k_htc_ampdu_action,
1853 	.sw_scan_start      = ath9k_htc_sw_scan_start,
1854 	.sw_scan_complete   = ath9k_htc_sw_scan_complete,
1855 	.set_rts_threshold  = ath9k_htc_set_rts_threshold,
1856 	.rfkill_poll        = ath9k_htc_rfkill_poll_state,
1857 	.set_coverage_class = ath9k_htc_set_coverage_class,
1858 	.set_bitrate_mask   = ath9k_htc_set_bitrate_mask,
1859 	.get_stats	    = ath9k_htc_get_stats,
1860 	.get_antenna	    = ath9k_htc_get_antenna,
1861 
1862 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
1863 	.get_et_sset_count  = ath9k_htc_get_et_sset_count,
1864 	.get_et_stats       = ath9k_htc_get_et_stats,
1865 	.get_et_strings     = ath9k_htc_get_et_strings,
1866 #endif
1867 };
1868