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