Linux Audio

Check our new training course

Embedded Linux training

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