xref: /openbmc/linux/drivers/net/wireless/ath/ath9k/htc_drv_main.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
1 /*
2  * Copyright (c) 2010 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 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
20 static struct dentry *ath9k_debugfs_root;
21 #endif
22 
23 /*************/
24 /* Utilities */
25 /*************/
26 
27 void ath_update_txpow(struct ath9k_htc_priv *priv)
28 {
29 	struct ath_hw *ah = priv->ah;
30 
31 	if (priv->curtxpow != priv->txpowlimit) {
32 		ath9k_hw_set_txpowerlimit(ah, priv->txpowlimit, false);
33 		/* read back in case value is clamped */
34 		priv->curtxpow = ath9k_hw_regulatory(ah)->power_limit;
35 	}
36 }
37 
38 /* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
39 static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
40 					      struct ath9k_channel *ichan)
41 {
42 	enum htc_phymode mode;
43 
44 	mode = HTC_MODE_AUTO;
45 
46 	switch (ichan->chanmode) {
47 	case CHANNEL_G:
48 	case CHANNEL_G_HT20:
49 	case CHANNEL_G_HT40PLUS:
50 	case CHANNEL_G_HT40MINUS:
51 		mode = HTC_MODE_11NG;
52 		break;
53 	case CHANNEL_A:
54 	case CHANNEL_A_HT20:
55 	case CHANNEL_A_HT40PLUS:
56 	case CHANNEL_A_HT40MINUS:
57 		mode = HTC_MODE_11NA;
58 		break;
59 	default:
60 		break;
61 	}
62 
63 	return mode;
64 }
65 
66 bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
67 			enum ath9k_power_mode mode)
68 {
69 	bool ret;
70 
71 	mutex_lock(&priv->htc_pm_lock);
72 	ret = ath9k_hw_setpower(priv->ah, mode);
73 	mutex_unlock(&priv->htc_pm_lock);
74 
75 	return ret;
76 }
77 
78 void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
79 {
80 	mutex_lock(&priv->htc_pm_lock);
81 	if (++priv->ps_usecount != 1)
82 		goto unlock;
83 	ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);
84 
85 unlock:
86 	mutex_unlock(&priv->htc_pm_lock);
87 }
88 
89 void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
90 {
91 	mutex_lock(&priv->htc_pm_lock);
92 	if (--priv->ps_usecount != 0)
93 		goto unlock;
94 
95 	if (priv->ps_idle)
96 		ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
97 	else if (priv->ps_enabled)
98 		ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
99 
100 unlock:
101 	mutex_unlock(&priv->htc_pm_lock);
102 }
103 
104 void ath9k_ps_work(struct work_struct *work)
105 {
106 	struct ath9k_htc_priv *priv =
107 		container_of(work, struct ath9k_htc_priv,
108 			     ps_work);
109 	ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
110 
111 	/* The chip wakes up after receiving the first beacon
112 	   while network sleep is enabled. For the driver to
113 	   be in sync with the hw, set the chip to awake and
114 	   only then set it to sleep.
115 	 */
116 	ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
117 }
118 
119 void ath9k_htc_reset(struct ath9k_htc_priv *priv)
120 {
121 	struct ath_hw *ah = priv->ah;
122 	struct ath_common *common = ath9k_hw_common(ah);
123 	struct ieee80211_channel *channel = priv->hw->conf.channel;
124 	struct ath9k_hw_cal_data *caldata;
125 	enum htc_phymode mode;
126 	__be16 htc_mode;
127 	u8 cmd_rsp;
128 	int ret;
129 
130 	mutex_lock(&priv->mutex);
131 	ath9k_htc_ps_wakeup(priv);
132 
133 	if (priv->op_flags & OP_ASSOCIATED)
134 		cancel_delayed_work_sync(&priv->ath9k_ani_work);
135 
136 	ieee80211_stop_queues(priv->hw);
137 	htc_stop(priv->htc);
138 	WMI_CMD(WMI_DISABLE_INTR_CMDID);
139 	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
140 	WMI_CMD(WMI_STOP_RECV_CMDID);
141 
142 	caldata = &priv->caldata[channel->hw_value];
143 	ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
144 	if (ret) {
145 		ath_err(common,
146 			"Unable to reset device (%u Mhz) reset status %d\n",
147 			channel->center_freq, ret);
148 	}
149 
150 	ath_update_txpow(priv);
151 
152 	WMI_CMD(WMI_START_RECV_CMDID);
153 	ath9k_host_rx_init(priv);
154 
155 	mode = ath9k_htc_get_curmode(priv, ah->curchan);
156 	htc_mode = cpu_to_be16(mode);
157 	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
158 
159 	WMI_CMD(WMI_ENABLE_INTR_CMDID);
160 	htc_start(priv->htc);
161 
162 	if (priv->op_flags & OP_ASSOCIATED) {
163 		ath9k_htc_beacon_config(priv, priv->vif);
164 		ath_start_ani(priv);
165 	}
166 
167 	ieee80211_wake_queues(priv->hw);
168 
169 	ath9k_htc_ps_restore(priv);
170 	mutex_unlock(&priv->mutex);
171 }
172 
173 static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
174 				 struct ieee80211_hw *hw,
175 				 struct ath9k_channel *hchan)
176 {
177 	struct ath_hw *ah = priv->ah;
178 	struct ath_common *common = ath9k_hw_common(ah);
179 	struct ieee80211_conf *conf = &common->hw->conf;
180 	bool fastcc;
181 	struct ieee80211_channel *channel = hw->conf.channel;
182 	struct ath9k_hw_cal_data *caldata;
183 	enum htc_phymode mode;
184 	__be16 htc_mode;
185 	u8 cmd_rsp;
186 	int ret;
187 
188 	if (priv->op_flags & OP_INVALID)
189 		return -EIO;
190 
191 	fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
192 
193 	ath9k_htc_ps_wakeup(priv);
194 	htc_stop(priv->htc);
195 	WMI_CMD(WMI_DISABLE_INTR_CMDID);
196 	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
197 	WMI_CMD(WMI_STOP_RECV_CMDID);
198 
199 	ath_dbg(common, ATH_DBG_CONFIG,
200 		"(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
201 		priv->ah->curchan->channel,
202 		channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
203 		fastcc);
204 
205 	caldata = &priv->caldata[channel->hw_value];
206 	ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
207 	if (ret) {
208 		ath_err(common,
209 			"Unable to reset channel (%u Mhz) reset status %d\n",
210 			channel->center_freq, ret);
211 		goto err;
212 	}
213 
214 	ath_update_txpow(priv);
215 
216 	WMI_CMD(WMI_START_RECV_CMDID);
217 	if (ret)
218 		goto err;
219 
220 	ath9k_host_rx_init(priv);
221 
222 	mode = ath9k_htc_get_curmode(priv, hchan);
223 	htc_mode = cpu_to_be16(mode);
224 	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
225 	if (ret)
226 		goto err;
227 
228 	WMI_CMD(WMI_ENABLE_INTR_CMDID);
229 	if (ret)
230 		goto err;
231 
232 	htc_start(priv->htc);
233 err:
234 	ath9k_htc_ps_restore(priv);
235 	return ret;
236 }
237 
238 static void __ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
239 {
240 	struct ath_common *common = ath9k_hw_common(priv->ah);
241 	struct ath9k_htc_target_vif hvif;
242 	int ret = 0;
243 	u8 cmd_rsp;
244 
245 	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
246 	memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
247 	hvif.index = 0; /* Should do for now */
248 	WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
249 	priv->nvifs--;
250 }
251 
252 static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
253 {
254 	struct ath_common *common = ath9k_hw_common(priv->ah);
255 	struct ath9k_htc_target_vif hvif;
256 	struct ath9k_htc_target_sta tsta;
257 	int ret = 0;
258 	u8 cmd_rsp;
259 
260 	if (priv->nvifs > 0)
261 		return -ENOBUFS;
262 
263 	if (priv->nstations >= ATH9K_HTC_MAX_STA)
264 		return -ENOBUFS;
265 
266 	/*
267 	 * Add an interface.
268 	 */
269 
270 	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
271 	memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
272 
273 	hvif.opmode = cpu_to_be32(HTC_M_MONITOR);
274 	priv->ah->opmode = NL80211_IFTYPE_MONITOR;
275 	hvif.index = priv->nvifs;
276 
277 	WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
278 	if (ret)
279 		return ret;
280 
281 	priv->nvifs++;
282 
283 	/*
284 	 * Associate a station with the interface for packet injection.
285 	 */
286 
287 	memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
288 
289 	memcpy(&tsta.macaddr, common->macaddr, ETH_ALEN);
290 
291 	tsta.is_vif_sta = 1;
292 	tsta.sta_index = priv->nstations;
293 	tsta.vif_index = hvif.index;
294 	tsta.maxampdu = 0xffff;
295 
296 	WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
297 	if (ret) {
298 		ath_err(common, "Unable to add station entry for monitor mode\n");
299 		goto err_vif;
300 	}
301 
302 	priv->nstations++;
303 
304 	return 0;
305 
306 err_vif:
307 	/*
308 	 * Remove the interface from the target.
309 	 */
310 	__ath9k_htc_remove_monitor_interface(priv);
311 	return ret;
312 }
313 
314 static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
315 {
316 	struct ath_common *common = ath9k_hw_common(priv->ah);
317 	int ret = 0;
318 	u8 cmd_rsp, sta_idx;
319 
320 	__ath9k_htc_remove_monitor_interface(priv);
321 
322 	sta_idx = 0; /* Only single interface, for now */
323 
324 	WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
325 	if (ret) {
326 		ath_err(common, "Unable to remove station entry for monitor mode\n");
327 		return ret;
328 	}
329 
330 	priv->nstations--;
331 
332 	return 0;
333 }
334 
335 static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
336 				 struct ieee80211_vif *vif,
337 				 struct ieee80211_sta *sta)
338 {
339 	struct ath_common *common = ath9k_hw_common(priv->ah);
340 	struct ath9k_htc_target_sta tsta;
341 	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
342 	struct ath9k_htc_sta *ista;
343 	int ret;
344 	u8 cmd_rsp;
345 
346 	if (priv->nstations >= ATH9K_HTC_MAX_STA)
347 		return -ENOBUFS;
348 
349 	memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
350 
351 	if (sta) {
352 		ista = (struct ath9k_htc_sta *) sta->drv_priv;
353 		memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
354 		memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
355 		tsta.associd = common->curaid;
356 		tsta.is_vif_sta = 0;
357 		tsta.valid = true;
358 		ista->index = priv->nstations;
359 	} else {
360 		memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
361 		tsta.is_vif_sta = 1;
362 	}
363 
364 	tsta.sta_index = priv->nstations;
365 	tsta.vif_index = avp->index;
366 	tsta.maxampdu = 0xffff;
367 	if (sta && sta->ht_cap.ht_supported)
368 		tsta.flags = cpu_to_be16(ATH_HTC_STA_HT);
369 
370 	WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
371 	if (ret) {
372 		if (sta)
373 			ath_err(common,
374 				"Unable to add station entry for: %pM\n",
375 				sta->addr);
376 		return ret;
377 	}
378 
379 	if (sta)
380 		ath_dbg(common, ATH_DBG_CONFIG,
381 			"Added a station entry for: %pM (idx: %d)\n",
382 			sta->addr, tsta.sta_index);
383 
384 	priv->nstations++;
385 	return 0;
386 }
387 
388 static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
389 				    struct ieee80211_vif *vif,
390 				    struct ieee80211_sta *sta)
391 {
392 	struct ath_common *common = ath9k_hw_common(priv->ah);
393 	struct ath9k_htc_sta *ista;
394 	int ret;
395 	u8 cmd_rsp, sta_idx;
396 
397 	if (sta) {
398 		ista = (struct ath9k_htc_sta *) sta->drv_priv;
399 		sta_idx = ista->index;
400 	} else {
401 		sta_idx = 0;
402 	}
403 
404 	WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
405 	if (ret) {
406 		if (sta)
407 			ath_err(common,
408 				"Unable to remove station entry for: %pM\n",
409 				sta->addr);
410 		return ret;
411 	}
412 
413 	if (sta)
414 		ath_dbg(common, ATH_DBG_CONFIG,
415 			"Removed a station entry for: %pM (idx: %d)\n",
416 			sta->addr, sta_idx);
417 
418 	priv->nstations--;
419 	return 0;
420 }
421 
422 static int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv)
423 {
424 	struct ath9k_htc_cap_target tcap;
425 	int ret;
426 	u8 cmd_rsp;
427 
428 	memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
429 
430 	/* FIXME: Values are hardcoded */
431 	tcap.flags = 0x240c40;
432 	tcap.flags_ext = 0x80601000;
433 	tcap.ampdu_limit = 0xffff0000;
434 	tcap.ampdu_subframes = 20;
435 	tcap.tx_chainmask_legacy = priv->ah->caps.tx_chainmask;
436 	tcap.protmode = 1;
437 	tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
438 
439 	WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
440 
441 	return ret;
442 }
443 
444 static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
445 				 struct ieee80211_sta *sta,
446 				 struct ath9k_htc_target_rate *trate)
447 {
448 	struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
449 	struct ieee80211_supported_band *sband;
450 	u32 caps = 0;
451 	int i, j;
452 
453 	sband = priv->hw->wiphy->bands[priv->hw->conf.channel->band];
454 
455 	for (i = 0, j = 0; i < sband->n_bitrates; i++) {
456 		if (sta->supp_rates[sband->band] & BIT(i)) {
457 			trate->rates.legacy_rates.rs_rates[j]
458 				= (sband->bitrates[i].bitrate * 2) / 10;
459 			j++;
460 		}
461 	}
462 	trate->rates.legacy_rates.rs_nrates = j;
463 
464 	if (sta->ht_cap.ht_supported) {
465 		for (i = 0, j = 0; i < 77; i++) {
466 			if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
467 				trate->rates.ht_rates.rs_rates[j++] = i;
468 			if (j == ATH_HTC_RATE_MAX)
469 				break;
470 		}
471 		trate->rates.ht_rates.rs_nrates = j;
472 
473 		caps = WLAN_RC_HT_FLAG;
474 		if (sta->ht_cap.mcs.rx_mask[1])
475 			caps |= WLAN_RC_DS_FLAG;
476 		if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
477 		     (conf_is_ht40(&priv->hw->conf)))
478 			caps |= WLAN_RC_40_FLAG;
479 		if (conf_is_ht40(&priv->hw->conf) &&
480 		    (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
481 			caps |= WLAN_RC_SGI_FLAG;
482 		else if (conf_is_ht20(&priv->hw->conf) &&
483 			 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
484 			caps |= WLAN_RC_SGI_FLAG;
485 	}
486 
487 	trate->sta_index = ista->index;
488 	trate->isnew = 1;
489 	trate->capflags = cpu_to_be32(caps);
490 }
491 
492 static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
493 				    struct ath9k_htc_target_rate *trate)
494 {
495 	struct ath_common *common = ath9k_hw_common(priv->ah);
496 	int ret;
497 	u8 cmd_rsp;
498 
499 	WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
500 	if (ret) {
501 		ath_err(common,
502 			"Unable to initialize Rate information on target\n");
503 	}
504 
505 	return ret;
506 }
507 
508 static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
509 				struct ieee80211_sta *sta)
510 {
511 	struct ath_common *common = ath9k_hw_common(priv->ah);
512 	struct ath9k_htc_target_rate trate;
513 	int ret;
514 
515 	memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
516 	ath9k_htc_setup_rate(priv, sta, &trate);
517 	ret = ath9k_htc_send_rate_cmd(priv, &trate);
518 	if (!ret)
519 		ath_dbg(common, ATH_DBG_CONFIG,
520 			"Updated target sta: %pM, rate caps: 0x%X\n",
521 			sta->addr, be32_to_cpu(trate.capflags));
522 }
523 
524 static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
525 				  struct ieee80211_vif *vif,
526 				  struct ieee80211_bss_conf *bss_conf)
527 {
528 	struct ath_common *common = ath9k_hw_common(priv->ah);
529 	struct ath9k_htc_target_rate trate;
530 	struct ieee80211_sta *sta;
531 	int ret;
532 
533 	memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
534 
535 	rcu_read_lock();
536 	sta = ieee80211_find_sta(vif, bss_conf->bssid);
537 	if (!sta) {
538 		rcu_read_unlock();
539 		return;
540 	}
541 	ath9k_htc_setup_rate(priv, sta, &trate);
542 	rcu_read_unlock();
543 
544 	ret = ath9k_htc_send_rate_cmd(priv, &trate);
545 	if (!ret)
546 		ath_dbg(common, ATH_DBG_CONFIG,
547 			"Updated target sta: %pM, rate caps: 0x%X\n",
548 			bss_conf->bssid, be32_to_cpu(trate.capflags));
549 }
550 
551 static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
552 				  struct ieee80211_vif *vif,
553 				  struct ieee80211_sta *sta,
554 				  enum ieee80211_ampdu_mlme_action action,
555 				  u16 tid)
556 {
557 	struct ath_common *common = ath9k_hw_common(priv->ah);
558 	struct ath9k_htc_target_aggr aggr;
559 	struct ath9k_htc_sta *ista;
560 	int ret = 0;
561 	u8 cmd_rsp;
562 
563 	if (tid >= ATH9K_HTC_MAX_TID)
564 		return -EINVAL;
565 
566 	memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
567 	ista = (struct ath9k_htc_sta *) sta->drv_priv;
568 
569 	aggr.sta_index = ista->index;
570 	aggr.tidno = tid & 0xf;
571 	aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
572 
573 	WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
574 	if (ret)
575 		ath_dbg(common, ATH_DBG_CONFIG,
576 			"Unable to %s TX aggregation for (%pM, %d)\n",
577 			(aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
578 	else
579 		ath_dbg(common, ATH_DBG_CONFIG,
580 			"%s TX aggregation for (%pM, %d)\n",
581 			(aggr.aggr_enable) ? "Starting" : "Stopping",
582 			sta->addr, tid);
583 
584 	spin_lock_bh(&priv->tx_lock);
585 	ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
586 	spin_unlock_bh(&priv->tx_lock);
587 
588 	return ret;
589 }
590 
591 /*********/
592 /* DEBUG */
593 /*********/
594 
595 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
596 
597 static int ath9k_debugfs_open(struct inode *inode, struct file *file)
598 {
599 	file->private_data = inode->i_private;
600 	return 0;
601 }
602 
603 static ssize_t read_file_tgt_stats(struct file *file, char __user *user_buf,
604 				   size_t count, loff_t *ppos)
605 {
606 	struct ath9k_htc_priv *priv = file->private_data;
607 	struct ath9k_htc_target_stats cmd_rsp;
608 	char buf[512];
609 	unsigned int len = 0;
610 	int ret = 0;
611 
612 	memset(&cmd_rsp, 0, sizeof(cmd_rsp));
613 
614 	WMI_CMD(WMI_TGT_STATS_CMDID);
615 	if (ret)
616 		return -EINVAL;
617 
618 
619 	len += snprintf(buf + len, sizeof(buf) - len,
620 			"%19s : %10u\n", "TX Short Retries",
621 			be32_to_cpu(cmd_rsp.tx_shortretry));
622 	len += snprintf(buf + len, sizeof(buf) - len,
623 			"%19s : %10u\n", "TX Long Retries",
624 			be32_to_cpu(cmd_rsp.tx_longretry));
625 	len += snprintf(buf + len, sizeof(buf) - len,
626 			"%19s : %10u\n", "TX Xretries",
627 			be32_to_cpu(cmd_rsp.tx_xretries));
628 	len += snprintf(buf + len, sizeof(buf) - len,
629 			"%19s : %10u\n", "TX Unaggr. Xretries",
630 			be32_to_cpu(cmd_rsp.ht_txunaggr_xretry));
631 	len += snprintf(buf + len, sizeof(buf) - len,
632 			"%19s : %10u\n", "TX Xretries (HT)",
633 			be32_to_cpu(cmd_rsp.ht_tx_xretries));
634 	len += snprintf(buf + len, sizeof(buf) - len,
635 			"%19s : %10u\n", "TX Rate", priv->debug.txrate);
636 
637 	if (len > sizeof(buf))
638 		len = sizeof(buf);
639 
640 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
641 }
642 
643 static const struct file_operations fops_tgt_stats = {
644 	.read = read_file_tgt_stats,
645 	.open = ath9k_debugfs_open,
646 	.owner = THIS_MODULE,
647 	.llseek = default_llseek,
648 };
649 
650 static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
651 			      size_t count, loff_t *ppos)
652 {
653 	struct ath9k_htc_priv *priv = file->private_data;
654 	char buf[512];
655 	unsigned int len = 0;
656 
657 	len += snprintf(buf + len, sizeof(buf) - len,
658 			"%20s : %10u\n", "Buffers queued",
659 			priv->debug.tx_stats.buf_queued);
660 	len += snprintf(buf + len, sizeof(buf) - len,
661 			"%20s : %10u\n", "Buffers completed",
662 			priv->debug.tx_stats.buf_completed);
663 	len += snprintf(buf + len, sizeof(buf) - len,
664 			"%20s : %10u\n", "SKBs queued",
665 			priv->debug.tx_stats.skb_queued);
666 	len += snprintf(buf + len, sizeof(buf) - len,
667 			"%20s : %10u\n", "SKBs completed",
668 			priv->debug.tx_stats.skb_completed);
669 	len += snprintf(buf + len, sizeof(buf) - len,
670 			"%20s : %10u\n", "SKBs dropped",
671 			priv->debug.tx_stats.skb_dropped);
672 
673 	len += snprintf(buf + len, sizeof(buf) - len,
674 			"%20s : %10u\n", "BE queued",
675 			priv->debug.tx_stats.queue_stats[WME_AC_BE]);
676 	len += snprintf(buf + len, sizeof(buf) - len,
677 			"%20s : %10u\n", "BK queued",
678 			priv->debug.tx_stats.queue_stats[WME_AC_BK]);
679 	len += snprintf(buf + len, sizeof(buf) - len,
680 			"%20s : %10u\n", "VI queued",
681 			priv->debug.tx_stats.queue_stats[WME_AC_VI]);
682 	len += snprintf(buf + len, sizeof(buf) - len,
683 			"%20s : %10u\n", "VO queued",
684 			priv->debug.tx_stats.queue_stats[WME_AC_VO]);
685 
686 	if (len > sizeof(buf))
687 		len = sizeof(buf);
688 
689 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
690 }
691 
692 static const struct file_operations fops_xmit = {
693 	.read = read_file_xmit,
694 	.open = ath9k_debugfs_open,
695 	.owner = THIS_MODULE,
696 	.llseek = default_llseek,
697 };
698 
699 static ssize_t read_file_recv(struct file *file, char __user *user_buf,
700 			      size_t count, loff_t *ppos)
701 {
702 	struct ath9k_htc_priv *priv = file->private_data;
703 	char buf[512];
704 	unsigned int len = 0;
705 
706 	len += snprintf(buf + len, sizeof(buf) - len,
707 			"%20s : %10u\n", "SKBs allocated",
708 			priv->debug.rx_stats.skb_allocated);
709 	len += snprintf(buf + len, sizeof(buf) - len,
710 			"%20s : %10u\n", "SKBs completed",
711 			priv->debug.rx_stats.skb_completed);
712 	len += snprintf(buf + len, sizeof(buf) - len,
713 			"%20s : %10u\n", "SKBs Dropped",
714 			priv->debug.rx_stats.skb_dropped);
715 
716 	if (len > sizeof(buf))
717 		len = sizeof(buf);
718 
719 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
720 }
721 
722 static const struct file_operations fops_recv = {
723 	.read = read_file_recv,
724 	.open = ath9k_debugfs_open,
725 	.owner = THIS_MODULE,
726 	.llseek = default_llseek,
727 };
728 
729 int ath9k_htc_init_debug(struct ath_hw *ah)
730 {
731 	struct ath_common *common = ath9k_hw_common(ah);
732 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
733 
734 	if (!ath9k_debugfs_root)
735 		return -ENOENT;
736 
737 	priv->debug.debugfs_phy = debugfs_create_dir(wiphy_name(priv->hw->wiphy),
738 						     ath9k_debugfs_root);
739 	if (!priv->debug.debugfs_phy)
740 		goto err;
741 
742 	priv->debug.debugfs_tgt_stats = debugfs_create_file("tgt_stats", S_IRUSR,
743 						    priv->debug.debugfs_phy,
744 						    priv, &fops_tgt_stats);
745 	if (!priv->debug.debugfs_tgt_stats)
746 		goto err;
747 
748 
749 	priv->debug.debugfs_xmit = debugfs_create_file("xmit", S_IRUSR,
750 						       priv->debug.debugfs_phy,
751 						       priv, &fops_xmit);
752 	if (!priv->debug.debugfs_xmit)
753 		goto err;
754 
755 	priv->debug.debugfs_recv = debugfs_create_file("recv", S_IRUSR,
756 						       priv->debug.debugfs_phy,
757 						       priv, &fops_recv);
758 	if (!priv->debug.debugfs_recv)
759 		goto err;
760 
761 	return 0;
762 
763 err:
764 	ath9k_htc_exit_debug(ah);
765 	return -ENOMEM;
766 }
767 
768 void ath9k_htc_exit_debug(struct ath_hw *ah)
769 {
770 	struct ath_common *common = ath9k_hw_common(ah);
771 	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
772 
773 	debugfs_remove(priv->debug.debugfs_recv);
774 	debugfs_remove(priv->debug.debugfs_xmit);
775 	debugfs_remove(priv->debug.debugfs_tgt_stats);
776 	debugfs_remove(priv->debug.debugfs_phy);
777 }
778 
779 int ath9k_htc_debug_create_root(void)
780 {
781 	ath9k_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
782 	if (!ath9k_debugfs_root)
783 		return -ENOENT;
784 
785 	return 0;
786 }
787 
788 void ath9k_htc_debug_remove_root(void)
789 {
790 	debugfs_remove(ath9k_debugfs_root);
791 	ath9k_debugfs_root = NULL;
792 }
793 
794 #endif /* CONFIG_ATH9K_HTC_DEBUGFS */
795 
796 /*******/
797 /* ANI */
798 /*******/
799 
800 void ath_start_ani(struct ath9k_htc_priv *priv)
801 {
802 	struct ath_common *common = ath9k_hw_common(priv->ah);
803 	unsigned long timestamp = jiffies_to_msecs(jiffies);
804 
805 	common->ani.longcal_timer = timestamp;
806 	common->ani.shortcal_timer = timestamp;
807 	common->ani.checkani_timer = timestamp;
808 
809 	ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
810 				     msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
811 }
812 
813 void ath9k_ani_work(struct work_struct *work)
814 {
815 	struct ath9k_htc_priv *priv =
816 		container_of(work, struct ath9k_htc_priv,
817 			     ath9k_ani_work.work);
818 	struct ath_hw *ah = priv->ah;
819 	struct ath_common *common = ath9k_hw_common(ah);
820 	bool longcal = false;
821 	bool shortcal = false;
822 	bool aniflag = false;
823 	unsigned int timestamp = jiffies_to_msecs(jiffies);
824 	u32 cal_interval, short_cal_interval;
825 
826 	short_cal_interval = ATH_STA_SHORT_CALINTERVAL;
827 
828 	/* Only calibrate if awake */
829 	if (ah->power_mode != ATH9K_PM_AWAKE)
830 		goto set_timer;
831 
832 	/* Long calibration runs independently of short calibration. */
833 	if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
834 		longcal = true;
835 		ath_dbg(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
836 		common->ani.longcal_timer = timestamp;
837 	}
838 
839 	/* Short calibration applies only while caldone is false */
840 	if (!common->ani.caldone) {
841 		if ((timestamp - common->ani.shortcal_timer) >=
842 		    short_cal_interval) {
843 			shortcal = true;
844 			ath_dbg(common, ATH_DBG_ANI,
845 				"shortcal @%lu\n", jiffies);
846 			common->ani.shortcal_timer = timestamp;
847 			common->ani.resetcal_timer = timestamp;
848 		}
849 	} else {
850 		if ((timestamp - common->ani.resetcal_timer) >=
851 		    ATH_RESTART_CALINTERVAL) {
852 			common->ani.caldone = ath9k_hw_reset_calvalid(ah);
853 			if (common->ani.caldone)
854 				common->ani.resetcal_timer = timestamp;
855 		}
856 	}
857 
858 	/* Verify whether we must check ANI */
859 	if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
860 		aniflag = true;
861 		common->ani.checkani_timer = timestamp;
862 	}
863 
864 	/* Skip all processing if there's nothing to do. */
865 	if (longcal || shortcal || aniflag) {
866 
867 		ath9k_htc_ps_wakeup(priv);
868 
869 		/* Call ANI routine if necessary */
870 		if (aniflag)
871 			ath9k_hw_ani_monitor(ah, ah->curchan);
872 
873 		/* Perform calibration if necessary */
874 		if (longcal || shortcal)
875 			common->ani.caldone =
876 				ath9k_hw_calibrate(ah, ah->curchan,
877 						   common->rx_chainmask,
878 						   longcal);
879 
880 		ath9k_htc_ps_restore(priv);
881 	}
882 
883 set_timer:
884 	/*
885 	* Set timer interval based on previous results.
886 	* The interval must be the shortest necessary to satisfy ANI,
887 	* short calibration and long calibration.
888 	*/
889 	cal_interval = ATH_LONG_CALINTERVAL;
890 	if (priv->ah->config.enable_ani)
891 		cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
892 	if (!common->ani.caldone)
893 		cal_interval = min(cal_interval, (u32)short_cal_interval);
894 
895 	ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
896 				     msecs_to_jiffies(cal_interval));
897 }
898 
899 /**********************/
900 /* mac80211 Callbacks */
901 /**********************/
902 
903 static int ath9k_htc_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
904 {
905 	struct ieee80211_hdr *hdr;
906 	struct ath9k_htc_priv *priv = hw->priv;
907 	int padpos, padsize, ret;
908 
909 	hdr = (struct ieee80211_hdr *) skb->data;
910 
911 	/* Add the padding after the header if this is not already done */
912 	padpos = ath9k_cmn_padpos(hdr->frame_control);
913 	padsize = padpos & 3;
914 	if (padsize && skb->len > padpos) {
915 		if (skb_headroom(skb) < padsize)
916 			return -1;
917 		skb_push(skb, padsize);
918 		memmove(skb->data, skb->data + padsize, padpos);
919 	}
920 
921 	ret = ath9k_htc_tx_start(priv, skb);
922 	if (ret != 0) {
923 		if (ret == -ENOMEM) {
924 			ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
925 				"Stopping TX queues\n");
926 			ieee80211_stop_queues(hw);
927 			spin_lock_bh(&priv->tx_lock);
928 			priv->tx_queues_stop = true;
929 			spin_unlock_bh(&priv->tx_lock);
930 		} else {
931 			ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
932 				"Tx failed\n");
933 		}
934 		goto fail_tx;
935 	}
936 
937 	return 0;
938 
939 fail_tx:
940 	dev_kfree_skb_any(skb);
941 	return 0;
942 }
943 
944 static int ath9k_htc_start(struct ieee80211_hw *hw)
945 {
946 	struct ath9k_htc_priv *priv = hw->priv;
947 	struct ath_hw *ah = priv->ah;
948 	struct ath_common *common = ath9k_hw_common(ah);
949 	struct ieee80211_channel *curchan = hw->conf.channel;
950 	struct ath9k_channel *init_channel;
951 	int ret = 0;
952 	enum htc_phymode mode;
953 	__be16 htc_mode;
954 	u8 cmd_rsp;
955 
956 	mutex_lock(&priv->mutex);
957 
958 	ath_dbg(common, ATH_DBG_CONFIG,
959 		"Starting driver with initial channel: %d MHz\n",
960 		curchan->center_freq);
961 
962 	/* Ensure that HW is awake before flushing RX */
963 	ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
964 	WMI_CMD(WMI_FLUSH_RECV_CMDID);
965 
966 	/* setup initial channel */
967 	init_channel = ath9k_cmn_get_curchannel(hw, ah);
968 
969 	ath9k_hw_htc_resetinit(ah);
970 	ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
971 	if (ret) {
972 		ath_err(common,
973 			"Unable to reset hardware; reset status %d (freq %u MHz)\n",
974 			ret, curchan->center_freq);
975 		mutex_unlock(&priv->mutex);
976 		return ret;
977 	}
978 
979 	ath_update_txpow(priv);
980 
981 	mode = ath9k_htc_get_curmode(priv, init_channel);
982 	htc_mode = cpu_to_be16(mode);
983 	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
984 	WMI_CMD(WMI_ATH_INIT_CMDID);
985 	WMI_CMD(WMI_START_RECV_CMDID);
986 
987 	ath9k_host_rx_init(priv);
988 
989 	priv->op_flags &= ~OP_INVALID;
990 	htc_start(priv->htc);
991 
992 	spin_lock_bh(&priv->tx_lock);
993 	priv->tx_queues_stop = false;
994 	spin_unlock_bh(&priv->tx_lock);
995 
996 	ieee80211_wake_queues(hw);
997 
998 	if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE) {
999 		ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
1000 					   AR_STOMP_LOW_WLAN_WGHT);
1001 		ath9k_hw_btcoex_enable(ah);
1002 		ath_htc_resume_btcoex_work(priv);
1003 	}
1004 	mutex_unlock(&priv->mutex);
1005 
1006 	return ret;
1007 }
1008 
1009 static void ath9k_htc_stop(struct ieee80211_hw *hw)
1010 {
1011 	struct ath9k_htc_priv *priv = hw->priv;
1012 	struct ath_hw *ah = priv->ah;
1013 	struct ath_common *common = ath9k_hw_common(ah);
1014 	int ret = 0;
1015 	u8 cmd_rsp;
1016 
1017 	/* Cancel all the running timers/work .. */
1018 	cancel_work_sync(&priv->fatal_work);
1019 	cancel_work_sync(&priv->ps_work);
1020 	cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
1021 	ath9k_led_stop_brightness(priv);
1022 
1023 	mutex_lock(&priv->mutex);
1024 
1025 	if (priv->op_flags & OP_INVALID) {
1026 		ath_dbg(common, ATH_DBG_ANY, "Device not present\n");
1027 		mutex_unlock(&priv->mutex);
1028 		return;
1029 	}
1030 
1031 	ath9k_htc_ps_wakeup(priv);
1032 	htc_stop(priv->htc);
1033 	WMI_CMD(WMI_DISABLE_INTR_CMDID);
1034 	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
1035 	WMI_CMD(WMI_STOP_RECV_CMDID);
1036 	skb_queue_purge(&priv->tx_queue);
1037 
1038 	/* Remove monitor interface here */
1039 	if (ah->opmode == NL80211_IFTYPE_MONITOR) {
1040 		if (ath9k_htc_remove_monitor_interface(priv))
1041 			ath_err(common, "Unable to remove monitor interface\n");
1042 		else
1043 			ath_dbg(common, ATH_DBG_CONFIG,
1044 				"Monitor interface removed\n");
1045 	}
1046 
1047 	if (ah->btcoex_hw.enabled) {
1048 		ath9k_hw_btcoex_disable(ah);
1049 		if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
1050 			ath_htc_cancel_btcoex_work(priv);
1051 	}
1052 
1053 	ath9k_hw_phy_disable(ah);
1054 	ath9k_hw_disable(ah);
1055 	ath9k_htc_ps_restore(priv);
1056 	ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1057 
1058 	priv->op_flags |= OP_INVALID;
1059 
1060 	ath_dbg(common, ATH_DBG_CONFIG, "Driver halt\n");
1061 	mutex_unlock(&priv->mutex);
1062 }
1063 
1064 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1065 				   struct ieee80211_vif *vif)
1066 {
1067 	struct ath9k_htc_priv *priv = hw->priv;
1068 	struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1069 	struct ath_common *common = ath9k_hw_common(priv->ah);
1070 	struct ath9k_htc_target_vif hvif;
1071 	int ret = 0;
1072 	u8 cmd_rsp;
1073 
1074 	mutex_lock(&priv->mutex);
1075 
1076 	/* Only one interface for now */
1077 	if (priv->nvifs > 0) {
1078 		ret = -ENOBUFS;
1079 		goto out;
1080 	}
1081 
1082 	ath9k_htc_ps_wakeup(priv);
1083 	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1084 	memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1085 
1086 	switch (vif->type) {
1087 	case NL80211_IFTYPE_STATION:
1088 		hvif.opmode = cpu_to_be32(HTC_M_STA);
1089 		break;
1090 	case NL80211_IFTYPE_ADHOC:
1091 		hvif.opmode = cpu_to_be32(HTC_M_IBSS);
1092 		break;
1093 	default:
1094 		ath_err(common,
1095 			"Interface type %d not yet supported\n", vif->type);
1096 		ret = -EOPNOTSUPP;
1097 		goto out;
1098 	}
1099 
1100 	ath_dbg(common, ATH_DBG_CONFIG,
1101 		"Attach a VIF of type: %d\n", vif->type);
1102 
1103 	priv->ah->opmode = vif->type;
1104 
1105 	/* Index starts from zero on the target */
1106 	avp->index = hvif.index = priv->nvifs;
1107 	hvif.rtsthreshold = cpu_to_be16(2304);
1108 	WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1109 	if (ret)
1110 		goto out;
1111 
1112 	priv->nvifs++;
1113 
1114 	/*
1115 	 * We need a node in target to tx mgmt frames
1116 	 * before association.
1117 	 */
1118 	ret = ath9k_htc_add_station(priv, vif, NULL);
1119 	if (ret)
1120 		goto out;
1121 
1122 	ret = ath9k_htc_update_cap_target(priv);
1123 	if (ret)
1124 		ath_dbg(common, ATH_DBG_CONFIG,
1125 			"Failed to update capability in target\n");
1126 
1127 	priv->vif = vif;
1128 out:
1129 	ath9k_htc_ps_restore(priv);
1130 	mutex_unlock(&priv->mutex);
1131 
1132 	return ret;
1133 }
1134 
1135 static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1136 				       struct ieee80211_vif *vif)
1137 {
1138 	struct ath9k_htc_priv *priv = hw->priv;
1139 	struct ath_common *common = ath9k_hw_common(priv->ah);
1140 	struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1141 	struct ath9k_htc_target_vif hvif;
1142 	int ret = 0;
1143 	u8 cmd_rsp;
1144 
1145 	ath_dbg(common, ATH_DBG_CONFIG, "Detach Interface\n");
1146 
1147 	mutex_lock(&priv->mutex);
1148 	ath9k_htc_ps_wakeup(priv);
1149 
1150 	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1151 	memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1152 	hvif.index = avp->index;
1153 	WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1154 	priv->nvifs--;
1155 
1156 	ath9k_htc_remove_station(priv, vif, NULL);
1157 	priv->vif = NULL;
1158 
1159 	ath9k_htc_ps_restore(priv);
1160 	mutex_unlock(&priv->mutex);
1161 }
1162 
1163 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1164 {
1165 	struct ath9k_htc_priv *priv = hw->priv;
1166 	struct ath_common *common = ath9k_hw_common(priv->ah);
1167 	struct ieee80211_conf *conf = &hw->conf;
1168 
1169 	mutex_lock(&priv->mutex);
1170 
1171 	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1172 		bool enable_radio = false;
1173 		bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1174 
1175 		mutex_lock(&priv->htc_pm_lock);
1176 		if (!idle && priv->ps_idle)
1177 			enable_radio = true;
1178 		priv->ps_idle = idle;
1179 		mutex_unlock(&priv->htc_pm_lock);
1180 
1181 		if (enable_radio) {
1182 			ath_dbg(common, ATH_DBG_CONFIG,
1183 				"not-idle: enabling radio\n");
1184 			ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1185 			ath9k_htc_radio_enable(hw);
1186 		}
1187 	}
1188 
1189 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1190 		struct ieee80211_channel *curchan = hw->conf.channel;
1191 		int pos = curchan->hw_value;
1192 
1193 		ath_dbg(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
1194 			curchan->center_freq);
1195 
1196 		ath9k_cmn_update_ichannel(&priv->ah->channels[pos],
1197 					  hw->conf.channel,
1198 					  hw->conf.channel_type);
1199 
1200 		if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1201 			ath_err(common, "Unable to set channel\n");
1202 			mutex_unlock(&priv->mutex);
1203 			return -EINVAL;
1204 		}
1205 
1206 	}
1207 
1208 	if (changed & IEEE80211_CONF_CHANGE_PS) {
1209 		if (conf->flags & IEEE80211_CONF_PS) {
1210 			ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1211 			priv->ps_enabled = true;
1212 		} else {
1213 			priv->ps_enabled = false;
1214 			cancel_work_sync(&priv->ps_work);
1215 			ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1216 		}
1217 	}
1218 
1219 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
1220 		priv->txpowlimit = 2 * conf->power_level;
1221 		ath_update_txpow(priv);
1222 	}
1223 
1224 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1225 		if (conf->flags & IEEE80211_CONF_MONITOR) {
1226 			if (ath9k_htc_add_monitor_interface(priv))
1227 				ath_err(common, "Failed to set monitor mode\n");
1228 			else
1229 				ath_dbg(common, ATH_DBG_CONFIG,
1230 					"HW opmode set to Monitor mode\n");
1231 		}
1232 	}
1233 
1234 	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1235 		mutex_lock(&priv->htc_pm_lock);
1236 		if (!priv->ps_idle) {
1237 			mutex_unlock(&priv->htc_pm_lock);
1238 			goto out;
1239 		}
1240 		mutex_unlock(&priv->htc_pm_lock);
1241 
1242 		ath_dbg(common, ATH_DBG_CONFIG,
1243 			"idle: disabling radio\n");
1244 		ath9k_htc_radio_disable(hw);
1245 	}
1246 
1247 out:
1248 	mutex_unlock(&priv->mutex);
1249 	return 0;
1250 }
1251 
1252 #define SUPPORTED_FILTERS			\
1253 	(FIF_PROMISC_IN_BSS |			\
1254 	FIF_ALLMULTI |				\
1255 	FIF_CONTROL |				\
1256 	FIF_PSPOLL |				\
1257 	FIF_OTHER_BSS |				\
1258 	FIF_BCN_PRBRESP_PROMISC |		\
1259 	FIF_PROBE_REQ |				\
1260 	FIF_FCSFAIL)
1261 
1262 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1263 				       unsigned int changed_flags,
1264 				       unsigned int *total_flags,
1265 				       u64 multicast)
1266 {
1267 	struct ath9k_htc_priv *priv = hw->priv;
1268 	u32 rfilt;
1269 
1270 	mutex_lock(&priv->mutex);
1271 	ath9k_htc_ps_wakeup(priv);
1272 
1273 	changed_flags &= SUPPORTED_FILTERS;
1274 	*total_flags &= SUPPORTED_FILTERS;
1275 
1276 	priv->rxfilter = *total_flags;
1277 	rfilt = ath9k_htc_calcrxfilter(priv);
1278 	ath9k_hw_setrxfilter(priv->ah, rfilt);
1279 
1280 	ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_CONFIG,
1281 		"Set HW RX filter: 0x%x\n", rfilt);
1282 
1283 	ath9k_htc_ps_restore(priv);
1284 	mutex_unlock(&priv->mutex);
1285 }
1286 
1287 static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1288 			     struct ieee80211_vif *vif,
1289 			     struct ieee80211_sta *sta)
1290 {
1291 	struct ath9k_htc_priv *priv = hw->priv;
1292 	int ret;
1293 
1294 	mutex_lock(&priv->mutex);
1295 	ath9k_htc_ps_wakeup(priv);
1296 	ret = ath9k_htc_add_station(priv, vif, sta);
1297 	if (!ret)
1298 		ath9k_htc_init_rate(priv, sta);
1299 	ath9k_htc_ps_restore(priv);
1300 	mutex_unlock(&priv->mutex);
1301 
1302 	return ret;
1303 }
1304 
1305 static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1306 				struct ieee80211_vif *vif,
1307 				struct ieee80211_sta *sta)
1308 {
1309 	struct ath9k_htc_priv *priv = hw->priv;
1310 	int ret;
1311 
1312 	mutex_lock(&priv->mutex);
1313 	ath9k_htc_ps_wakeup(priv);
1314 	ret = ath9k_htc_remove_station(priv, vif, sta);
1315 	ath9k_htc_ps_restore(priv);
1316 	mutex_unlock(&priv->mutex);
1317 
1318 	return ret;
1319 }
1320 
1321 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw, u16 queue,
1322 			     const struct ieee80211_tx_queue_params *params)
1323 {
1324 	struct ath9k_htc_priv *priv = hw->priv;
1325 	struct ath_common *common = ath9k_hw_common(priv->ah);
1326 	struct ath9k_tx_queue_info qi;
1327 	int ret = 0, qnum;
1328 
1329 	if (queue >= WME_NUM_AC)
1330 		return 0;
1331 
1332 	mutex_lock(&priv->mutex);
1333 	ath9k_htc_ps_wakeup(priv);
1334 
1335 	memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1336 
1337 	qi.tqi_aifs = params->aifs;
1338 	qi.tqi_cwmin = params->cw_min;
1339 	qi.tqi_cwmax = params->cw_max;
1340 	qi.tqi_burstTime = params->txop;
1341 
1342 	qnum = get_hw_qnum(queue, priv->hwq_map);
1343 
1344 	ath_dbg(common, ATH_DBG_CONFIG,
1345 		"Configure tx [queue/hwq] [%d/%d],  aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1346 		queue, qnum, params->aifs, params->cw_min,
1347 		params->cw_max, params->txop);
1348 
1349 	ret = ath_htc_txq_update(priv, qnum, &qi);
1350 	if (ret) {
1351 		ath_err(common, "TXQ Update failed\n");
1352 		goto out;
1353 	}
1354 
1355 	if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1356 	    (qnum == priv->hwq_map[WME_AC_BE]))
1357 		    ath9k_htc_beaconq_config(priv);
1358 out:
1359 	ath9k_htc_ps_restore(priv);
1360 	mutex_unlock(&priv->mutex);
1361 
1362 	return ret;
1363 }
1364 
1365 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1366 			     enum set_key_cmd cmd,
1367 			     struct ieee80211_vif *vif,
1368 			     struct ieee80211_sta *sta,
1369 			     struct ieee80211_key_conf *key)
1370 {
1371 	struct ath9k_htc_priv *priv = hw->priv;
1372 	struct ath_common *common = ath9k_hw_common(priv->ah);
1373 	int ret = 0;
1374 
1375 	if (htc_modparam_nohwcrypt)
1376 		return -ENOSPC;
1377 
1378 	mutex_lock(&priv->mutex);
1379 	ath_dbg(common, ATH_DBG_CONFIG, "Set HW Key\n");
1380 	ath9k_htc_ps_wakeup(priv);
1381 
1382 	switch (cmd) {
1383 	case SET_KEY:
1384 		ret = ath_key_config(common, vif, sta, key);
1385 		if (ret >= 0) {
1386 			key->hw_key_idx = ret;
1387 			/* push IV and Michael MIC generation to stack */
1388 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1389 			if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1390 				key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1391 			if (priv->ah->sw_mgmt_crypto &&
1392 			    key->cipher == WLAN_CIPHER_SUITE_CCMP)
1393 				key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1394 			ret = 0;
1395 		}
1396 		break;
1397 	case DISABLE_KEY:
1398 		ath_key_delete(common, key);
1399 		break;
1400 	default:
1401 		ret = -EINVAL;
1402 	}
1403 
1404 	ath9k_htc_ps_restore(priv);
1405 	mutex_unlock(&priv->mutex);
1406 
1407 	return ret;
1408 }
1409 
1410 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1411 				       struct ieee80211_vif *vif,
1412 				       struct ieee80211_bss_conf *bss_conf,
1413 				       u32 changed)
1414 {
1415 	struct ath9k_htc_priv *priv = hw->priv;
1416 	struct ath_hw *ah = priv->ah;
1417 	struct ath_common *common = ath9k_hw_common(ah);
1418 
1419 	mutex_lock(&priv->mutex);
1420 	ath9k_htc_ps_wakeup(priv);
1421 
1422 	if (changed & BSS_CHANGED_ASSOC) {
1423 		common->curaid = bss_conf->assoc ?
1424 				 bss_conf->aid : 0;
1425 		ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
1426 			bss_conf->assoc);
1427 
1428 		if (bss_conf->assoc) {
1429 			priv->op_flags |= OP_ASSOCIATED;
1430 			ath_start_ani(priv);
1431 		} else {
1432 			priv->op_flags &= ~OP_ASSOCIATED;
1433 			cancel_delayed_work_sync(&priv->ath9k_ani_work);
1434 		}
1435 	}
1436 
1437 	if (changed & BSS_CHANGED_BSSID) {
1438 		/* Set BSSID */
1439 		memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1440 		ath9k_hw_write_associd(ah);
1441 
1442 		ath_dbg(common, ATH_DBG_CONFIG,
1443 			"BSSID: %pM aid: 0x%x\n",
1444 			common->curbssid, common->curaid);
1445 	}
1446 
1447 	if ((changed & BSS_CHANGED_BEACON_INT) ||
1448 	    (changed & BSS_CHANGED_BEACON) ||
1449 	    ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1450 	    bss_conf->enable_beacon)) {
1451 		priv->op_flags |= OP_ENABLE_BEACON;
1452 		ath9k_htc_beacon_config(priv, vif);
1453 	}
1454 
1455 	if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
1456 	    !bss_conf->enable_beacon) {
1457 		priv->op_flags &= ~OP_ENABLE_BEACON;
1458 		ath9k_htc_beacon_config(priv, vif);
1459 	}
1460 
1461 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1462 		ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
1463 			bss_conf->use_short_preamble);
1464 		if (bss_conf->use_short_preamble)
1465 			priv->op_flags |= OP_PREAMBLE_SHORT;
1466 		else
1467 			priv->op_flags &= ~OP_PREAMBLE_SHORT;
1468 	}
1469 
1470 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1471 		ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
1472 			bss_conf->use_cts_prot);
1473 		if (bss_conf->use_cts_prot &&
1474 		    hw->conf.channel->band != IEEE80211_BAND_5GHZ)
1475 			priv->op_flags |= OP_PROTECT_ENABLE;
1476 		else
1477 			priv->op_flags &= ~OP_PROTECT_ENABLE;
1478 	}
1479 
1480 	if (changed & BSS_CHANGED_ERP_SLOT) {
1481 		if (bss_conf->use_short_slot)
1482 			ah->slottime = 9;
1483 		else
1484 			ah->slottime = 20;
1485 
1486 		ath9k_hw_init_global_settings(ah);
1487 	}
1488 
1489 	if (changed & BSS_CHANGED_HT)
1490 		ath9k_htc_update_rate(priv, vif, bss_conf);
1491 
1492 	ath9k_htc_ps_restore(priv);
1493 	mutex_unlock(&priv->mutex);
1494 }
1495 
1496 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw)
1497 {
1498 	struct ath9k_htc_priv *priv = hw->priv;
1499 	u64 tsf;
1500 
1501 	mutex_lock(&priv->mutex);
1502 	ath9k_htc_ps_wakeup(priv);
1503 	tsf = ath9k_hw_gettsf64(priv->ah);
1504 	ath9k_htc_ps_restore(priv);
1505 	mutex_unlock(&priv->mutex);
1506 
1507 	return tsf;
1508 }
1509 
1510 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw, u64 tsf)
1511 {
1512 	struct ath9k_htc_priv *priv = hw->priv;
1513 
1514 	mutex_lock(&priv->mutex);
1515 	ath9k_htc_ps_wakeup(priv);
1516 	ath9k_hw_settsf64(priv->ah, tsf);
1517 	ath9k_htc_ps_restore(priv);
1518 	mutex_unlock(&priv->mutex);
1519 }
1520 
1521 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw)
1522 {
1523 	struct ath9k_htc_priv *priv = hw->priv;
1524 
1525 	mutex_lock(&priv->mutex);
1526 	ath9k_htc_ps_wakeup(priv);
1527 	ath9k_hw_reset_tsf(priv->ah);
1528 	ath9k_htc_ps_restore(priv);
1529 	mutex_unlock(&priv->mutex);
1530 }
1531 
1532 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1533 				  struct ieee80211_vif *vif,
1534 				  enum ieee80211_ampdu_mlme_action action,
1535 				  struct ieee80211_sta *sta,
1536 				  u16 tid, u16 *ssn)
1537 {
1538 	struct ath9k_htc_priv *priv = hw->priv;
1539 	struct ath9k_htc_sta *ista;
1540 	int ret = 0;
1541 
1542 	switch (action) {
1543 	case IEEE80211_AMPDU_RX_START:
1544 		break;
1545 	case IEEE80211_AMPDU_RX_STOP:
1546 		break;
1547 	case IEEE80211_AMPDU_TX_START:
1548 		ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1549 		if (!ret)
1550 			ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1551 		break;
1552 	case IEEE80211_AMPDU_TX_STOP:
1553 		ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1554 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1555 		break;
1556 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1557 		ista = (struct ath9k_htc_sta *) sta->drv_priv;
1558 		spin_lock_bh(&priv->tx_lock);
1559 		ista->tid_state[tid] = AGGR_OPERATIONAL;
1560 		spin_unlock_bh(&priv->tx_lock);
1561 		break;
1562 	default:
1563 		ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1564 	}
1565 
1566 	return ret;
1567 }
1568 
1569 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1570 {
1571 	struct ath9k_htc_priv *priv = hw->priv;
1572 
1573 	mutex_lock(&priv->mutex);
1574 	spin_lock_bh(&priv->beacon_lock);
1575 	priv->op_flags |= OP_SCANNING;
1576 	spin_unlock_bh(&priv->beacon_lock);
1577 	cancel_work_sync(&priv->ps_work);
1578 	if (priv->op_flags & OP_ASSOCIATED)
1579 		cancel_delayed_work_sync(&priv->ath9k_ani_work);
1580 	mutex_unlock(&priv->mutex);
1581 }
1582 
1583 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1584 {
1585 	struct ath9k_htc_priv *priv = hw->priv;
1586 
1587 	mutex_lock(&priv->mutex);
1588 	ath9k_htc_ps_wakeup(priv);
1589 	spin_lock_bh(&priv->beacon_lock);
1590 	priv->op_flags &= ~OP_SCANNING;
1591 	spin_unlock_bh(&priv->beacon_lock);
1592 	if (priv->op_flags & OP_ASSOCIATED) {
1593 		ath9k_htc_beacon_config(priv, priv->vif);
1594 		ath_start_ani(priv);
1595 	}
1596 	ath9k_htc_ps_restore(priv);
1597 	mutex_unlock(&priv->mutex);
1598 }
1599 
1600 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1601 {
1602 	return 0;
1603 }
1604 
1605 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1606 					 u8 coverage_class)
1607 {
1608 	struct ath9k_htc_priv *priv = hw->priv;
1609 
1610 	mutex_lock(&priv->mutex);
1611 	ath9k_htc_ps_wakeup(priv);
1612 	priv->ah->coverage_class = coverage_class;
1613 	ath9k_hw_init_global_settings(priv->ah);
1614 	ath9k_htc_ps_restore(priv);
1615 	mutex_unlock(&priv->mutex);
1616 }
1617 
1618 struct ieee80211_ops ath9k_htc_ops = {
1619 	.tx                 = ath9k_htc_tx,
1620 	.start              = ath9k_htc_start,
1621 	.stop               = ath9k_htc_stop,
1622 	.add_interface      = ath9k_htc_add_interface,
1623 	.remove_interface   = ath9k_htc_remove_interface,
1624 	.config             = ath9k_htc_config,
1625 	.configure_filter   = ath9k_htc_configure_filter,
1626 	.sta_add            = ath9k_htc_sta_add,
1627 	.sta_remove         = ath9k_htc_sta_remove,
1628 	.conf_tx            = ath9k_htc_conf_tx,
1629 	.bss_info_changed   = ath9k_htc_bss_info_changed,
1630 	.set_key            = ath9k_htc_set_key,
1631 	.get_tsf            = ath9k_htc_get_tsf,
1632 	.set_tsf            = ath9k_htc_set_tsf,
1633 	.reset_tsf          = ath9k_htc_reset_tsf,
1634 	.ampdu_action       = ath9k_htc_ampdu_action,
1635 	.sw_scan_start      = ath9k_htc_sw_scan_start,
1636 	.sw_scan_complete   = ath9k_htc_sw_scan_complete,
1637 	.set_rts_threshold  = ath9k_htc_set_rts_threshold,
1638 	.rfkill_poll        = ath9k_htc_rfkill_poll_state,
1639 	.set_coverage_class = ath9k_htc_set_coverage_class,
1640 };
1641