htc_drv_main.c 45.5 KB
Newer Older
S
Sujith 已提交
1
/*
2
 * Copyright (c) 2010-2011 Atheros Communications Inc.
S
Sujith 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "htc.h"

/*************/
/* Utilities */
/*************/

/* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
					      struct ath9k_channel *ichan)
{
	enum htc_phymode mode;

S
Sujith Manoharan 已提交
29
	mode = -EINVAL;
S
Sujith 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

	switch (ichan->chanmode) {
	case CHANNEL_G:
	case CHANNEL_G_HT20:
	case CHANNEL_G_HT40PLUS:
	case CHANNEL_G_HT40MINUS:
		mode = HTC_MODE_11NG;
		break;
	case CHANNEL_A:
	case CHANNEL_A_HT20:
	case CHANNEL_A_HT40PLUS:
	case CHANNEL_A_HT40MINUS:
		mode = HTC_MODE_11NA;
		break;
	default:
		break;
	}

S
Sujith Manoharan 已提交
48 49
	WARN_ON(mode < 0);

S
Sujith 已提交
50 51 52
	return mode;
}

S
Sujith Manoharan 已提交
53 54
bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
			enum ath9k_power_mode mode)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
{
	bool ret;

	mutex_lock(&priv->htc_pm_lock);
	ret = ath9k_hw_setpower(priv->ah, mode);
	mutex_unlock(&priv->htc_pm_lock);

	return ret;
}

void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
{
	mutex_lock(&priv->htc_pm_lock);
	if (++priv->ps_usecount != 1)
		goto unlock;
	ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);

unlock:
	mutex_unlock(&priv->htc_pm_lock);
}

void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
{
78 79
	bool reset;

80 81 82 83
	mutex_lock(&priv->htc_pm_lock);
	if (--priv->ps_usecount != 0)
		goto unlock;

84 85 86
	if (priv->ps_idle) {
		ath9k_hw_setrxabort(priv->ah, true);
		ath9k_hw_stopdmarecv(priv->ah, &reset);
87
		ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
88
	} else if (priv->ps_enabled) {
89
		ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
90
	}
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
unlock:
	mutex_unlock(&priv->htc_pm_lock);
}

void ath9k_ps_work(struct work_struct *work)
{
	struct ath9k_htc_priv *priv =
		container_of(work, struct ath9k_htc_priv,
			     ps_work);
	ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);

	/* The chip wakes up after receiving the first beacon
	   while network sleep is enabled. For the driver to
	   be in sync with the hw, set the chip to awake and
	   only then set it to sleep.
	 */
	ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
}

111 112 113 114 115
static void ath9k_htc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
{
	struct ath9k_htc_priv *priv = data;
	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;

116 117 118
	if ((vif->type == NL80211_IFTYPE_AP) && bss_conf->enable_beacon)
		priv->reconfig_beacon = true;

119 120 121 122 123 124 125 126 127 128 129 130 131 132
	if (bss_conf->assoc) {
		priv->rearm_ani = true;
		priv->reconfig_beacon = true;
	}
}

static void ath9k_htc_vif_reconfig(struct ath9k_htc_priv *priv)
{
	priv->rearm_ani = false;
	priv->reconfig_beacon = false;

	ieee80211_iterate_active_interfaces_atomic(priv->hw,
						   ath9k_htc_vif_iter, priv);
	if (priv->rearm_ani)
133
		ath9k_htc_start_ani(priv);
134 135 136 137 138 139 140 141

	if (priv->reconfig_beacon) {
		ath9k_htc_ps_wakeup(priv);
		ath9k_htc_beacon_reconfig(priv);
		ath9k_htc_ps_restore(priv);
	}
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
static void ath9k_htc_bssid_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
{
	struct ath9k_vif_iter_data *iter_data = data;
	int i;

	for (i = 0; i < ETH_ALEN; i++)
		iter_data->mask[i] &= ~(iter_data->hw_macaddr[i] ^ mac[i]);
}

static void ath9k_htc_set_bssid_mask(struct ath9k_htc_priv *priv,
				     struct ieee80211_vif *vif)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_vif_iter_data iter_data;

	/*
	 * Use the hardware MAC address as reference, the hardware uses it
	 * together with the BSSID mask when matching addresses.
	 */
	iter_data.hw_macaddr = common->macaddr;
	memset(&iter_data.mask, 0xff, ETH_ALEN);

	if (vif)
		ath9k_htc_bssid_iter(&iter_data, vif->addr, vif);

	/* Get list of all active MAC addresses */
	ieee80211_iterate_active_interfaces_atomic(priv->hw, ath9k_htc_bssid_iter,
						   &iter_data);

	memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
	ath_hw_setbssidmask(common);
}

175 176 177 178 179 180 181 182 183 184 185 186
static void ath9k_htc_set_opmode(struct ath9k_htc_priv *priv)
{
	if (priv->num_ibss_vif)
		priv->ah->opmode = NL80211_IFTYPE_ADHOC;
	else if (priv->num_ap_vif)
		priv->ah->opmode = NL80211_IFTYPE_AP;
	else
		priv->ah->opmode = NL80211_IFTYPE_STATION;

	ath9k_hw_setopmode(priv->ah);
}

187 188 189 190 191
void ath9k_htc_reset(struct ath9k_htc_priv *priv)
{
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ieee80211_channel *channel = priv->hw->conf.channel;
192
	struct ath9k_hw_cal_data *caldata = NULL;
193 194 195 196 197 198 199 200
	enum htc_phymode mode;
	__be16 htc_mode;
	u8 cmd_rsp;
	int ret;

	mutex_lock(&priv->mutex);
	ath9k_htc_ps_wakeup(priv);

201
	ath9k_htc_stop_ani(priv);
202
	ieee80211_stop_queues(priv->hw);
203

204
	del_timer_sync(&priv->tx.cleanup_timer);
205 206
	ath9k_htc_tx_drain(priv);

207 208 209 210
	WMI_CMD(WMI_DISABLE_INTR_CMDID);
	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
	WMI_CMD(WMI_STOP_RECV_CMDID);

S
Sujith Manoharan 已提交
211 212
	ath9k_wmi_event_drain(priv);

213
	caldata = &priv->caldata;
214 215 216 217 218 219 220
	ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
	if (ret) {
		ath_err(common,
			"Unable to reset device (%u Mhz) reset status %d\n",
			channel->center_freq, ret);
	}

221 222
	ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
			       &priv->curtxpow);
223 224 225 226 227 228 229 230 231 232

	WMI_CMD(WMI_START_RECV_CMDID);
	ath9k_host_rx_init(priv);

	mode = ath9k_htc_get_curmode(priv, ah->curchan);
	htc_mode = cpu_to_be16(mode);
	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);

	WMI_CMD(WMI_ENABLE_INTR_CMDID);
	htc_start(priv->htc);
233
	ath9k_htc_vif_reconfig(priv);
234 235
	ieee80211_wake_queues(priv->hw);

236 237 238
	mod_timer(&priv->tx.cleanup_timer,
		  jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));

239 240 241 242
	ath9k_htc_ps_restore(priv);
	mutex_unlock(&priv->mutex);
}

S
Sujith 已提交
243 244 245 246 247 248 249
static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
				 struct ieee80211_hw *hw,
				 struct ath9k_channel *hchan)
{
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ieee80211_conf *conf = &common->hw->conf;
250
	bool fastcc;
S
Sujith 已提交
251
	struct ieee80211_channel *channel = hw->conf.channel;
252
	struct ath9k_hw_cal_data *caldata = NULL;
S
Sujith 已提交
253
	enum htc_phymode mode;
S
Sujith 已提交
254
	__be16 htc_mode;
S
Sujith 已提交
255 256 257
	u8 cmd_rsp;
	int ret;

258
	if (test_bit(OP_INVALID, &priv->op_flags))
S
Sujith 已提交
259 260
		return -EIO;

261
	fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
S
Sujith 已提交
262

263
	ath9k_htc_ps_wakeup(priv);
264

265
	del_timer_sync(&priv->tx.cleanup_timer);
266 267
	ath9k_htc_tx_drain(priv);

S
Sujith 已提交
268 269 270 271
	WMI_CMD(WMI_DISABLE_INTR_CMDID);
	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
	WMI_CMD(WMI_STOP_RECV_CMDID);

S
Sujith Manoharan 已提交
272 273
	ath9k_wmi_event_drain(priv);

274
	ath_dbg(common, CONFIG,
J
Joe Perches 已提交
275 276 277 278
		"(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
		priv->ah->curchan->channel,
		channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
		fastcc);
S
Sujith 已提交
279

280 281
	if (!fastcc)
		caldata = &priv->caldata;
282

283
	ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
S
Sujith 已提交
284
	if (ret) {
285 286 287
		ath_err(common,
			"Unable to reset channel (%u Mhz) reset status %d\n",
			channel->center_freq, ret);
S
Sujith 已提交
288 289 290
		goto err;
	}

291 292
	ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
			       &priv->curtxpow);
S
Sujith 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

	WMI_CMD(WMI_START_RECV_CMDID);
	if (ret)
		goto err;

	ath9k_host_rx_init(priv);

	mode = ath9k_htc_get_curmode(priv, hchan);
	htc_mode = cpu_to_be16(mode);
	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
	if (ret)
		goto err;

	WMI_CMD(WMI_ENABLE_INTR_CMDID);
	if (ret)
		goto err;

	htc_start(priv->htc);
311

312
	if (!test_bit(OP_SCANNING, &priv->op_flags) &&
313 314 315
	    !(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
		ath9k_htc_vif_reconfig(priv);

316 317 318
	mod_timer(&priv->tx.cleanup_timer,
		  jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));

S
Sujith 已提交
319
err:
320
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
321 322 323
	return ret;
}

324 325 326 327 328 329 330
/*
 * Monitor mode handling is a tad complicated because the firmware requires
 * an interface to be created exclusively, while mac80211 doesn't associate
 * an interface with the mode.
 *
 * So, for now, only one monitor interface can be configured.
 */
331 332 333 334 335 336 337 338 339
static void __ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_target_vif hvif;
	int ret = 0;
	u8 cmd_rsp;

	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
	memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
340
	hvif.index = priv->mon_vif_idx;
341
	WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
342 343 344 345 346
	if (ret) {
		ath_err(common, "Unable to remove monitor interface at idx: %d\n",
			priv->mon_vif_idx);
	}

347
	priv->nvifs--;
348
	priv->vif_slot &= ~(1 << priv->mon_vif_idx);
349 350
}

351 352 353 354
static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_target_vif hvif;
355
	struct ath9k_htc_target_sta tsta;
356
	int ret = 0, sta_idx;
357 358
	u8 cmd_rsp;

359 360 361 362 363
	if ((priv->nvifs >= ATH9K_HTC_MAX_VIF) ||
	    (priv->nstations >= ATH9K_HTC_MAX_STA)) {
		ret = -ENOBUFS;
		goto err_vif;
	}
364

365 366 367 368 369
	sta_idx = ffz(priv->sta_slot);
	if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA)) {
		ret = -ENOBUFS;
		goto err_vif;
	}
370 371 372 373

	/*
	 * Add an interface.
	 */
374 375 376
	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
	memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);

377
	hvif.opmode = HTC_M_MONITOR;
378
	hvif.index = ffz(priv->vif_slot);
379 380 381

	WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
	if (ret)
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
		goto err_vif;

	/*
	 * Assign the monitor interface index as a special case here.
	 * This is needed when the interface is brought down.
	 */
	priv->mon_vif_idx = hvif.index;
	priv->vif_slot |= (1 << hvif.index);

	/*
	 * Set the hardware mode to monitor only if there are no
	 * other interfaces.
	 */
	if (!priv->nvifs)
		priv->ah->opmode = NL80211_IFTYPE_MONITOR;
397 398

	priv->nvifs++;
399 400 401 402 403 404 405 406 407

	/*
	 * Associate a station with the interface for packet injection.
	 */
	memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));

	memcpy(&tsta.macaddr, common->macaddr, ETH_ALEN);

	tsta.is_vif_sta = 1;
408
	tsta.sta_index = sta_idx;
409
	tsta.vif_index = hvif.index;
410
	tsta.maxampdu = cpu_to_be16(0xffff);
411 412 413 414

	WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
	if (ret) {
		ath_err(common, "Unable to add station entry for monitor mode\n");
415
		goto err_sta;
416 417
	}

418
	priv->sta_slot |= (1 << sta_idx);
419
	priv->nstations++;
420
	priv->vif_sta_pos[priv->mon_vif_idx] = sta_idx;
421 422
	priv->ah->is_monitoring = true;

423
	ath_dbg(common, CONFIG,
424 425 426
		"Attached a monitor interface at idx: %d, sta idx: %d\n",
		priv->mon_vif_idx, sta_idx);

427
	return 0;
428

429
err_sta:
430 431 432 433
	/*
	 * Remove the interface from the target.
	 */
	__ath9k_htc_remove_monitor_interface(priv);
434
err_vif:
435
	ath_dbg(common, FATAL, "Unable to attach a monitor interface\n");
436

437
	return ret;
438 439 440 441 442 443
}

static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	int ret = 0;
444
	u8 cmd_rsp, sta_idx;
445

446
	__ath9k_htc_remove_monitor_interface(priv);
447

448
	sta_idx = priv->vif_sta_pos[priv->mon_vif_idx];
449 450 451 452 453 454 455

	WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
	if (ret) {
		ath_err(common, "Unable to remove station entry for monitor mode\n");
		return ret;
	}

456
	priv->sta_slot &= ~(1 << sta_idx);
457
	priv->nstations--;
458
	priv->ah->is_monitoring = false;
459

460
	ath_dbg(common, CONFIG,
461 462 463
		"Removed a monitor interface at idx: %d, sta idx: %d\n",
		priv->mon_vif_idx, sta_idx);

464
	return 0;
465 466
}

S
Sujith 已提交
467 468 469 470 471 472 473 474
static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
				 struct ieee80211_vif *vif,
				 struct ieee80211_sta *sta)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_target_sta tsta;
	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
	struct ath9k_htc_sta *ista;
475
	int ret, sta_idx;
S
Sujith 已提交
476
	u8 cmd_rsp;
477
	u16 maxampdu;
S
Sujith 已提交
478 479 480 481

	if (priv->nstations >= ATH9K_HTC_MAX_STA)
		return -ENOBUFS;

482 483 484 485
	sta_idx = ffz(priv->sta_slot);
	if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA))
		return -ENOBUFS;

S
Sujith 已提交
486 487 488 489 490 491
	memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));

	if (sta) {
		ista = (struct ath9k_htc_sta *) sta->drv_priv;
		memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
		memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
492
		ista->index = sta_idx;
493 494 495 496
		tsta.is_vif_sta = 0;
		maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
				 sta->ht_cap.ampdu_factor);
		tsta.maxampdu = cpu_to_be16(maxampdu);
S
Sujith 已提交
497 498 499
	} else {
		memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
		tsta.is_vif_sta = 1;
500
		tsta.maxampdu = cpu_to_be16(0xffff);
S
Sujith 已提交
501 502
	}

503
	tsta.sta_index = sta_idx;
S
Sujith 已提交
504
	tsta.vif_index = avp->index;
505

S
Sujith 已提交
506 507 508
	WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
	if (ret) {
		if (sta)
509 510 511
			ath_err(common,
				"Unable to add station entry for: %pM\n",
				sta->addr);
S
Sujith 已提交
512 513 514
		return ret;
	}

515
	if (sta) {
516
		ath_dbg(common, CONFIG,
J
Joe Perches 已提交
517 518
			"Added a station entry for: %pM (idx: %d)\n",
			sta->addr, tsta.sta_index);
519
	} else {
520
		ath_dbg(common, CONFIG,
521 522 523
			"Added a station entry for VIF %d (idx: %d)\n",
			avp->index, tsta.sta_index);
	}
S
Sujith 已提交
524

525
	priv->sta_slot |= (1 << sta_idx);
S
Sujith 已提交
526
	priv->nstations++;
527 528 529
	if (!sta)
		priv->vif_sta_pos[avp->index] = sta_idx;

S
Sujith 已提交
530 531 532 533 534 535 536 537
	return 0;
}

static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
				    struct ieee80211_vif *vif,
				    struct ieee80211_sta *sta)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
538
	struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
S
Sujith 已提交
539 540 541 542 543 544 545 546
	struct ath9k_htc_sta *ista;
	int ret;
	u8 cmd_rsp, sta_idx;

	if (sta) {
		ista = (struct ath9k_htc_sta *) sta->drv_priv;
		sta_idx = ista->index;
	} else {
547
		sta_idx = priv->vif_sta_pos[avp->index];
S
Sujith 已提交
548 549 550 551 552
	}

	WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
	if (ret) {
		if (sta)
553 554 555
			ath_err(common,
				"Unable to remove station entry for: %pM\n",
				sta->addr);
S
Sujith 已提交
556 557 558
		return ret;
	}

559
	if (sta) {
560
		ath_dbg(common, CONFIG,
J
Joe Perches 已提交
561 562
			"Removed a station entry for: %pM (idx: %d)\n",
			sta->addr, sta_idx);
563
	} else {
564
		ath_dbg(common, CONFIG,
565 566 567
			"Removed a station entry for VIF %d (idx: %d)\n",
			avp->index, sta_idx);
	}
S
Sujith 已提交
568

569
	priv->sta_slot &= ~(1 << sta_idx);
S
Sujith 已提交
570
	priv->nstations--;
571

S
Sujith 已提交
572 573 574
	return 0;
}

575 576
int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
				u8 enable_coex)
S
Sujith 已提交
577 578 579 580 581 582 583
{
	struct ath9k_htc_cap_target tcap;
	int ret;
	u8 cmd_rsp;

	memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));

584
	tcap.ampdu_limit = cpu_to_be32(0xffff);
585
	tcap.ampdu_subframes = 0xff;
586
	tcap.enable_coex = enable_coex;
S
Sujith 已提交
587
	tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
S
Sujith 已提交
588 589 590 591 592 593

	WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);

	return ret;
}

S
Sujith 已提交
594 595 596
static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
				 struct ieee80211_sta *sta,
				 struct ath9k_htc_target_rate *trate)
S
Sujith 已提交
597 598 599 600
{
	struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
	struct ieee80211_supported_band *sband;
	u32 caps = 0;
S
Sujith 已提交
601
	int i, j;
S
Sujith 已提交
602

S
Sujith 已提交
603
	sband = priv->hw->wiphy->bands[priv->hw->conf.channel->band];
S
Sujith 已提交
604 605 606

	for (i = 0, j = 0; i < sband->n_bitrates; i++) {
		if (sta->supp_rates[sband->band] & BIT(i)) {
S
Sujith 已提交
607
			trate->rates.legacy_rates.rs_rates[j]
S
Sujith 已提交
608 609 610 611
				= (sband->bitrates[i].bitrate * 2) / 10;
			j++;
		}
	}
S
Sujith 已提交
612
	trate->rates.legacy_rates.rs_nrates = j;
S
Sujith 已提交
613 614 615 616

	if (sta->ht_cap.ht_supported) {
		for (i = 0, j = 0; i < 77; i++) {
			if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
S
Sujith 已提交
617
				trate->rates.ht_rates.rs_rates[j++] = i;
S
Sujith 已提交
618 619 620
			if (j == ATH_HTC_RATE_MAX)
				break;
		}
S
Sujith 已提交
621
		trate->rates.ht_rates.rs_nrates = j;
S
Sujith 已提交
622 623

		caps = WLAN_RC_HT_FLAG;
624 625
		if (sta->ht_cap.mcs.rx_mask[1])
			caps |= WLAN_RC_DS_FLAG;
626 627
		if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
		     (conf_is_ht40(&priv->hw->conf)))
S
Sujith 已提交
628
			caps |= WLAN_RC_40_FLAG;
629 630 631 632 633
		if (conf_is_ht40(&priv->hw->conf) &&
		    (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
			caps |= WLAN_RC_SGI_FLAG;
		else if (conf_is_ht20(&priv->hw->conf) &&
			 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
S
Sujith 已提交
634 635 636
			caps |= WLAN_RC_SGI_FLAG;
	}

S
Sujith 已提交
637 638 639 640 641 642 643 644 645 646 647
	trate->sta_index = ista->index;
	trate->isnew = 1;
	trate->capflags = cpu_to_be32(caps);
}

static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
				    struct ath9k_htc_target_rate *trate)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	int ret;
	u8 cmd_rsp;
S
Sujith 已提交
648

S
Sujith 已提交
649
	WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
S
Sujith 已提交
650
	if (ret) {
651 652
		ath_err(common,
			"Unable to initialize Rate information on target\n");
S
Sujith 已提交
653 654
	}

S
Sujith 已提交
655
	return ret;
S
Sujith 已提交
656 657
}

S
Sujith 已提交
658 659
static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
				struct ieee80211_sta *sta)
S
Sujith 已提交
660 661
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
S
Sujith 已提交
662
	struct ath9k_htc_target_rate trate;
S
Sujith 已提交
663 664
	int ret;

S
Sujith 已提交
665 666 667 668
	memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
	ath9k_htc_setup_rate(priv, sta, &trate);
	ret = ath9k_htc_send_rate_cmd(priv, &trate);
	if (!ret)
669
		ath_dbg(common, CONFIG,
J
Joe Perches 已提交
670 671
			"Updated target sta: %pM, rate caps: 0x%X\n",
			sta->addr, be32_to_cpu(trate.capflags));
S
Sujith 已提交
672 673
}

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
				  struct ieee80211_vif *vif,
				  struct ieee80211_bss_conf *bss_conf)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_target_rate trate;
	struct ieee80211_sta *sta;
	int ret;

	memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));

	rcu_read_lock();
	sta = ieee80211_find_sta(vif, bss_conf->bssid);
	if (!sta) {
		rcu_read_unlock();
		return;
	}
	ath9k_htc_setup_rate(priv, sta, &trate);
	rcu_read_unlock();

	ret = ath9k_htc_send_rate_cmd(priv, &trate);
	if (!ret)
696
		ath_dbg(common, CONFIG,
J
Joe Perches 已提交
697 698
			"Updated target sta: %pM, rate caps: 0x%X\n",
			bss_conf->bssid, be32_to_cpu(trate.capflags));
699 700
}

701 702 703 704 705
static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
				  struct ieee80211_vif *vif,
				  struct ieee80211_sta *sta,
				  enum ieee80211_ampdu_mlme_action action,
				  u16 tid)
S
Sujith 已提交
706 707 708
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_target_aggr aggr;
709
	struct ath9k_htc_sta *ista;
S
Sujith 已提交
710 711 712
	int ret = 0;
	u8 cmd_rsp;

713
	if (tid >= ATH9K_HTC_MAX_TID)
S
Sujith 已提交
714 715
		return -EINVAL;

716 717
	memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
	ista = (struct ath9k_htc_sta *) sta->drv_priv;
S
Sujith 已提交
718 719

	aggr.sta_index = ista->index;
S
Sujith 已提交
720 721
	aggr.tidno = tid & 0xf;
	aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
S
Sujith 已提交
722 723 724

	WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
	if (ret)
725
		ath_dbg(common, CONFIG,
J
Joe Perches 已提交
726 727
			"Unable to %s TX aggregation for (%pM, %d)\n",
			(aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
S
Sujith 已提交
728
	else
729
		ath_dbg(common, CONFIG,
J
Joe Perches 已提交
730 731 732
			"%s TX aggregation for (%pM, %d)\n",
			(aggr.aggr_enable) ? "Starting" : "Stopping",
			sta->addr, tid);
S
Sujith 已提交
733

734
	spin_lock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
735
	ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
736
	spin_unlock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
737

S
Sujith 已提交
738
	return ret;
S
Sujith 已提交
739 740 741 742 743 744
}

/*******/
/* ANI */
/*******/

745
void ath9k_htc_start_ani(struct ath9k_htc_priv *priv)
S
Sujith 已提交
746 747 748 749 750 751 752 753
{
	struct ath_common *common = ath9k_hw_common(priv->ah);
	unsigned long timestamp = jiffies_to_msecs(jiffies);

	common->ani.longcal_timer = timestamp;
	common->ani.shortcal_timer = timestamp;
	common->ani.checkani_timer = timestamp;

754
	set_bit(OP_ANI_RUNNING, &priv->op_flags);
755 756

	ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
S
Sujith 已提交
757 758 759
				     msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
}

760 761 762
void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv)
{
	cancel_delayed_work_sync(&priv->ani_work);
763
	clear_bit(OP_ANI_RUNNING, &priv->op_flags);
764 765 766
}

void ath9k_htc_ani_work(struct work_struct *work)
S
Sujith 已提交
767 768
{
	struct ath9k_htc_priv *priv =
769
		container_of(work, struct ath9k_htc_priv, ani_work.work);
S
Sujith 已提交
770 771 772 773 774 775 776 777
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);
	bool longcal = false;
	bool shortcal = false;
	bool aniflag = false;
	unsigned int timestamp = jiffies_to_msecs(jiffies);
	u32 cal_interval, short_cal_interval;

778 779
	short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
		ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
S
Sujith 已提交
780

781 782 783 784
	/* Only calibrate if awake */
	if (ah->power_mode != ATH9K_PM_AWAKE)
		goto set_timer;

S
Sujith 已提交
785 786 787
	/* Long calibration runs independently of short calibration. */
	if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
		longcal = true;
788
		ath_dbg(common, ANI, "longcal @%lu\n", jiffies);
S
Sujith 已提交
789 790 791 792 793 794 795 796
		common->ani.longcal_timer = timestamp;
	}

	/* Short calibration applies only while caldone is false */
	if (!common->ani.caldone) {
		if ((timestamp - common->ani.shortcal_timer) >=
		    short_cal_interval) {
			shortcal = true;
797
			ath_dbg(common, ANI, "shortcal @%lu\n", jiffies);
S
Sujith 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810
			common->ani.shortcal_timer = timestamp;
			common->ani.resetcal_timer = timestamp;
		}
	} else {
		if ((timestamp - common->ani.resetcal_timer) >=
		    ATH_RESTART_CALINTERVAL) {
			common->ani.caldone = ath9k_hw_reset_calvalid(ah);
			if (common->ani.caldone)
				common->ani.resetcal_timer = timestamp;
		}
	}

	/* Verify whether we must check ANI */
811 812
	if (ah->config.enable_ani &&
	    (timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
S
Sujith 已提交
813 814 815 816 817 818
		aniflag = true;
		common->ani.checkani_timer = timestamp;
	}

	/* Skip all processing if there's nothing to do. */
	if (longcal || shortcal || aniflag) {
819 820 821

		ath9k_htc_ps_wakeup(priv);

S
Sujith 已提交
822 823 824 825 826
		/* Call ANI routine if necessary */
		if (aniflag)
			ath9k_hw_ani_monitor(ah, ah->curchan);

		/* Perform calibration if necessary */
827
		if (longcal || shortcal)
S
Sujith 已提交
828 829
			common->ani.caldone =
				ath9k_hw_calibrate(ah, ah->curchan,
830
						   ah->rxchainmask, longcal);
S
Sujith 已提交
831

832
		ath9k_htc_ps_restore(priv);
S
Sujith 已提交
833 834
	}

835
set_timer:
S
Sujith 已提交
836 837 838 839 840 841
	/*
	* Set timer interval based on previous results.
	* The interval must be the shortest necessary to satisfy ANI,
	* short calibration and long calibration.
	*/
	cal_interval = ATH_LONG_CALINTERVAL;
842
	if (ah->config.enable_ani)
S
Sujith 已提交
843 844 845 846
		cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
	if (!common->ani.caldone)
		cal_interval = min(cal_interval, (u32)short_cal_interval);

847
	ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
S
Sujith 已提交
848 849 850 851 852 853 854
				     msecs_to_jiffies(cal_interval));
}

/**********************/
/* mac80211 Callbacks */
/**********************/

855 856 857
static void ath9k_htc_tx(struct ieee80211_hw *hw,
			 struct ieee80211_tx_control *control,
			 struct sk_buff *skb)
S
Sujith 已提交
858 859 860
{
	struct ieee80211_hdr *hdr;
	struct ath9k_htc_priv *priv = hw->priv;
861
	struct ath_common *common = ath9k_hw_common(priv->ah);
S
Sujith Manoharan 已提交
862
	int padpos, padsize, ret, slot;
S
Sujith 已提交
863 864 865 866 867 868 869

	hdr = (struct ieee80211_hdr *) skb->data;

	/* Add the padding after the header if this is not already done */
	padpos = ath9k_cmn_padpos(hdr->frame_control);
	padsize = padpos & 3;
	if (padsize && skb->len > padpos) {
870
		if (skb_headroom(skb) < padsize) {
871
			ath_dbg(common, XMIT, "No room for padding\n");
872
			goto fail_tx;
873
		}
S
Sujith 已提交
874 875 876 877
		skb_push(skb, padsize);
		memmove(skb->data, skb->data + padsize, padpos);
	}

S
Sujith Manoharan 已提交
878 879
	slot = ath9k_htc_tx_get_slot(priv);
	if (slot < 0) {
880
		ath_dbg(common, XMIT, "No free TX slot\n");
S
Sujith Manoharan 已提交
881 882 883
		goto fail_tx;
	}

884
	ret = ath9k_htc_tx_start(priv, control->sta, skb, slot, false);
S
Sujith 已提交
885
	if (ret != 0) {
886
		ath_dbg(common, XMIT, "Tx failed\n");
S
Sujith Manoharan 已提交
887
		goto clear_slot;
S
Sujith 已提交
888 889
	}

890 891
	ath9k_htc_check_stop_queues(priv);

892
	return;
S
Sujith 已提交
893

S
Sujith Manoharan 已提交
894 895
clear_slot:
	ath9k_htc_tx_clear_slot(priv, slot);
S
Sujith 已提交
896 897 898 899
fail_tx:
	dev_kfree_skb_any(skb);
}

S
Sujith 已提交
900
static int ath9k_htc_start(struct ieee80211_hw *hw)
S
Sujith 已提交
901 902 903 904 905 906 907 908
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ieee80211_channel *curchan = hw->conf.channel;
	struct ath9k_channel *init_channel;
	int ret = 0;
	enum htc_phymode mode;
S
Sujith 已提交
909
	__be16 htc_mode;
S
Sujith 已提交
910 911
	u8 cmd_rsp;

S
Sujith 已提交
912 913
	mutex_lock(&priv->mutex);

914
	ath_dbg(common, CONFIG,
J
Joe Perches 已提交
915 916
		"Starting driver with initial channel: %d MHz\n",
		curchan->center_freq);
S
Sujith 已提交
917

S
Sujith 已提交
918 919 920 921
	/* Ensure that HW is awake before flushing RX */
	ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
	WMI_CMD(WMI_FLUSH_RECV_CMDID);

S
Sujith 已提交
922 923 924
	/* setup initial channel */
	init_channel = ath9k_cmn_get_curchannel(hw, ah);

925
	ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
S
Sujith 已提交
926
	if (ret) {
927 928 929
		ath_err(common,
			"Unable to reset hardware; reset status %d (freq %u MHz)\n",
			ret, curchan->center_freq);
S
Sujith 已提交
930
		mutex_unlock(&priv->mutex);
931
		return ret;
S
Sujith 已提交
932 933
	}

934 935
	ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
			       &priv->curtxpow);
S
Sujith 已提交
936 937 938 939 940 941 942 943 944

	mode = ath9k_htc_get_curmode(priv, init_channel);
	htc_mode = cpu_to_be16(mode);
	WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
	WMI_CMD(WMI_ATH_INIT_CMDID);
	WMI_CMD(WMI_START_RECV_CMDID);

	ath9k_host_rx_init(priv);

945
	ret = ath9k_htc_update_cap_target(priv, 0);
946
	if (ret)
947
		ath_dbg(common, CONFIG,
948 949
			"Failed to update capability in target\n");

950
	clear_bit(OP_INVALID, &priv->op_flags);
S
Sujith 已提交
951 952
	htc_start(priv->htc);

953
	spin_lock_bh(&priv->tx.tx_lock);
954
	priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
955
	spin_unlock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
956 957 958

	ieee80211_wake_queues(hw);

959 960 961
	mod_timer(&priv->tx.cleanup_timer,
		  jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));

962 963
	ath9k_htc_start_btcoex(priv);

S
Sujith 已提交
964
	mutex_unlock(&priv->mutex);
965

S
Sujith 已提交
966 967 968
	return ret;
}

S
Sujith 已提交
969
static void ath9k_htc_stop(struct ieee80211_hw *hw)
S
Sujith 已提交
970 971 972 973
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);
974
	int ret __attribute__ ((unused));
S
Sujith 已提交
975 976
	u8 cmd_rsp;

S
Sujith 已提交
977 978
	mutex_lock(&priv->mutex);

979
	if (test_bit(OP_INVALID, &priv->op_flags)) {
980
		ath_dbg(common, ANY, "Device not present\n");
S
Sujith 已提交
981
		mutex_unlock(&priv->mutex);
S
Sujith 已提交
982 983 984
		return;
	}

985
	ath9k_htc_ps_wakeup(priv);
986

S
Sujith 已提交
987 988 989
	WMI_CMD(WMI_DISABLE_INTR_CMDID);
	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
	WMI_CMD(WMI_STOP_RECV_CMDID);
990 991

	tasklet_kill(&priv->rx_tasklet);
S
Sujith 已提交
992

993
	del_timer_sync(&priv->tx.cleanup_timer);
994
	ath9k_htc_tx_drain(priv);
S
Sujith Manoharan 已提交
995 996
	ath9k_wmi_event_drain(priv);

997 998 999 1000 1001
	mutex_unlock(&priv->mutex);

	/* Cancel all the running timers/work .. */
	cancel_work_sync(&priv->fatal_work);
	cancel_work_sync(&priv->ps_work);
1002 1003 1004 1005

#ifdef CONFIG_MAC80211_LEDS
	cancel_work_sync(&priv->led_work);
#endif
1006
	ath9k_htc_stop_ani(priv);
1007 1008 1009

	mutex_lock(&priv->mutex);

1010
	ath9k_htc_stop_btcoex(priv);
1011

1012 1013 1014 1015
	/* Remove a monitor interface if it's present. */
	if (priv->ah->is_monitoring)
		ath9k_htc_remove_monitor_interface(priv);

1016 1017 1018 1019 1020
	ath9k_hw_phy_disable(ah);
	ath9k_hw_disable(ah);
	ath9k_htc_ps_restore(priv);
	ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);

1021
	set_bit(OP_INVALID, &priv->op_flags);
S
Sujith 已提交
1022

1023
	ath_dbg(common, CONFIG, "Driver halt\n");
1024 1025 1026
	mutex_unlock(&priv->mutex);
}

S
Sujith 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
				   struct ieee80211_vif *vif)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_target_vif hvif;
	int ret = 0;
	u8 cmd_rsp;

	mutex_lock(&priv->mutex);

1039
	if (priv->nvifs >= ATH9K_HTC_MAX_VIF) {
1040
		mutex_unlock(&priv->mutex);
1041 1042 1043 1044 1045 1046 1047 1048
		return -ENOBUFS;
	}

	if (priv->num_ibss_vif ||
	    (priv->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
		ath_err(common, "IBSS coexistence with other modes is not allowed\n");
		mutex_unlock(&priv->mutex);
		return -ENOBUFS;
S
Sujith 已提交
1049 1050
	}

1051 1052 1053 1054 1055 1056 1057 1058
	if (((vif->type == NL80211_IFTYPE_AP) ||
	     (vif->type == NL80211_IFTYPE_ADHOC)) &&
	    ((priv->num_ap_vif + priv->num_ibss_vif) >= ATH9K_HTC_MAX_BCN_VIF)) {
		ath_err(common, "Max. number of beaconing interfaces reached\n");
		mutex_unlock(&priv->mutex);
		return -ENOBUFS;
	}

1059
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1060 1061 1062 1063 1064
	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
	memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);

	switch (vif->type) {
	case NL80211_IFTYPE_STATION:
1065
		hvif.opmode = HTC_M_STA;
S
Sujith 已提交
1066 1067
		break;
	case NL80211_IFTYPE_ADHOC:
1068
		hvif.opmode = HTC_M_IBSS;
S
Sujith 已提交
1069
		break;
1070
	case NL80211_IFTYPE_AP:
1071
		hvif.opmode = HTC_M_HOSTAP;
1072
		break;
S
Sujith 已提交
1073
	default:
1074
		ath_err(common,
S
Sujith 已提交
1075 1076 1077 1078 1079 1080
			"Interface type %d not yet supported\n", vif->type);
		ret = -EOPNOTSUPP;
		goto out;
	}

	/* Index starts from zero on the target */
1081
	avp->index = hvif.index = ffz(priv->vif_slot);
S
Sujith 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	hvif.rtsthreshold = cpu_to_be16(2304);
	WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
	if (ret)
		goto out;

	/*
	 * We need a node in target to tx mgmt frames
	 * before association.
	 */
	ret = ath9k_htc_add_station(priv, vif, NULL);
1092 1093
	if (ret) {
		WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
S
Sujith 已提交
1094
		goto out;
1095
	}
S
Sujith 已提交
1096

1097 1098
	ath9k_htc_set_bssid_mask(priv, vif);

1099
	priv->vif_slot |= (1 << avp->index);
1100
	priv->nvifs++;
1101

1102
	INC_VIF(priv, vif->type);
S
Sujith Manoharan 已提交
1103 1104 1105 1106 1107

	if ((vif->type == NL80211_IFTYPE_AP) ||
	    (vif->type == NL80211_IFTYPE_ADHOC))
		ath9k_htc_assign_bslot(priv, vif);

1108
	ath9k_htc_set_opmode(priv);
1109

1110
	if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1111
	    !test_bit(OP_ANI_RUNNING, &priv->op_flags)) {
1112
		ath9k_hw_set_tsfadjust(priv->ah, true);
1113
		ath9k_htc_start_ani(priv);
1114
	}
1115

1116 1117
	ath_dbg(common, CONFIG, "Attach a VIF of type: %d at idx: %d\n",
		vif->type, avp->index);
1118

S
Sujith 已提交
1119
out:
1120
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1121
	mutex_unlock(&priv->mutex);
S
Sujith 已提交
1122

S
Sujith 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
	return ret;
}

static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
				       struct ieee80211_vif *vif)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
	struct ath9k_htc_target_vif hvif;
	int ret = 0;
	u8 cmd_rsp;

	mutex_lock(&priv->mutex);
S
Sujith 已提交
1137
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1138 1139 1140 1141 1142

	memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
	memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
	hvif.index = avp->index;
	WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1143 1144 1145 1146
	if (ret) {
		ath_err(common, "Unable to remove interface at idx: %d\n",
			avp->index);
	}
S
Sujith 已提交
1147
	priv->nvifs--;
1148
	priv->vif_slot &= ~(1 << avp->index);
S
Sujith 已提交
1149 1150 1151

	ath9k_htc_remove_station(priv, vif, NULL);

1152
	DEC_VIF(priv, vif->type);
S
Sujith Manoharan 已提交
1153 1154 1155 1156 1157

	if ((vif->type == NL80211_IFTYPE_AP) ||
	    (vif->type == NL80211_IFTYPE_ADHOC))
		ath9k_htc_remove_bslot(priv, vif);

1158
	ath9k_htc_set_opmode(priv);
1159

1160 1161
	ath9k_htc_set_bssid_mask(priv, vif);

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	/*
	 * Stop ANI only if there are no associated station interfaces.
	 */
	if ((vif->type == NL80211_IFTYPE_AP) && (priv->num_ap_vif == 0)) {
		priv->rearm_ani = false;
		ieee80211_iterate_active_interfaces_atomic(priv->hw,
						   ath9k_htc_vif_iter, priv);
		if (!priv->rearm_ani)
			ath9k_htc_stop_ani(priv);
	}

1173
	ath_dbg(common, CONFIG, "Detach Interface at idx: %d\n", avp->index);
1174

S
Sujith 已提交
1175
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1176 1177 1178 1179 1180 1181 1182 1183
	mutex_unlock(&priv->mutex);
}

static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ieee80211_conf *conf = &hw->conf;
1184 1185
	bool chip_reset = false;
	int ret = 0;
S
Sujith 已提交
1186 1187

	mutex_lock(&priv->mutex);
1188
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1189

1190
	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
S
Sujith 已提交
1191
		mutex_lock(&priv->htc_pm_lock);
1192

1193 1194 1195 1196 1197
		priv->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
		if (priv->ps_idle)
			chip_reset = true;

		mutex_unlock(&priv->htc_pm_lock);
1198 1199
	}

1200 1201 1202 1203 1204
	/*
	 * Monitor interface should be added before
	 * IEEE80211_CONF_CHANGE_CHANNEL is handled.
	 */
	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1205 1206 1207 1208 1209
		if ((conf->flags & IEEE80211_CONF_MONITOR) &&
		    !priv->ah->is_monitoring)
			ath9k_htc_add_monitor_interface(priv);
		else if (priv->ah->is_monitoring)
			ath9k_htc_remove_monitor_interface(priv);
1210 1211
	}

1212
	if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || chip_reset) {
S
Sujith 已提交
1213 1214 1215
		struct ieee80211_channel *curchan = hw->conf.channel;
		int pos = curchan->hw_value;

1216
		ath_dbg(common, CONFIG, "Set channel: %d MHz\n",
J
Joe Perches 已提交
1217
			curchan->center_freq);
S
Sujith 已提交
1218

1219 1220 1221
		ath9k_cmn_update_ichannel(&priv->ah->channels[pos],
					  hw->conf.channel,
					  hw->conf.channel_type);
S
Sujith 已提交
1222 1223

		if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1224
			ath_err(common, "Unable to set channel\n");
1225 1226
			ret = -EINVAL;
			goto out;
S
Sujith 已提交
1227 1228 1229
		}

	}
1230

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
	if (changed & IEEE80211_CONF_CHANGE_PS) {
		if (conf->flags & IEEE80211_CONF_PS) {
			ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
			priv->ps_enabled = true;
		} else {
			priv->ps_enabled = false;
			cancel_work_sync(&priv->ps_work);
			ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
		}
	}
S
Sujith 已提交
1241

1242 1243
	if (changed & IEEE80211_CONF_CHANGE_POWER) {
		priv->txpowlimit = 2 * conf->power_level;
1244 1245
		ath9k_cmn_update_txpow(priv->ah, priv->curtxpow,
				       priv->txpowlimit, &priv->curtxpow);
1246 1247
	}

S
Sujith 已提交
1248
out:
1249
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1250
	mutex_unlock(&priv->mutex);
1251
	return ret;
S
Sujith 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260
}

#define SUPPORTED_FILTERS			\
	(FIF_PROMISC_IN_BSS |			\
	FIF_ALLMULTI |				\
	FIF_CONTROL |				\
	FIF_PSPOLL |				\
	FIF_OTHER_BSS |				\
	FIF_BCN_PRBRESP_PROMISC |		\
1261
	FIF_PROBE_REQ |				\
S
Sujith 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	FIF_FCSFAIL)

static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
				       unsigned int changed_flags,
				       unsigned int *total_flags,
				       u64 multicast)
{
	struct ath9k_htc_priv *priv = hw->priv;
	u32 rfilt;

	mutex_lock(&priv->mutex);
	changed_flags &= SUPPORTED_FILTERS;
	*total_flags &= SUPPORTED_FILTERS;

1276
	if (test_bit(OP_INVALID, &priv->op_flags)) {
1277
		ath_dbg(ath9k_hw_common(priv->ah), ANY,
1278
			"Unable to configure filter on invalid state\n");
L
Larry Finger 已提交
1279
		mutex_unlock(&priv->mutex);
1280 1281 1282 1283
		return;
	}
	ath9k_htc_ps_wakeup(priv);

S
Sujith 已提交
1284
	priv->rxfilter = *total_flags;
1285
	rfilt = ath9k_htc_calcrxfilter(priv);
S
Sujith 已提交
1286 1287
	ath9k_hw_setrxfilter(priv->ah, rfilt);

1288 1289
	ath_dbg(ath9k_hw_common(priv->ah), CONFIG, "Set HW RX filter: 0x%x\n",
		rfilt);
S
Sujith 已提交
1290

1291
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1292 1293 1294
	mutex_unlock(&priv->mutex);
}

1295 1296 1297
static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
			     struct ieee80211_vif *vif,
			     struct ieee80211_sta *sta)
S
Sujith 已提交
1298 1299 1300 1301
{
	struct ath9k_htc_priv *priv = hw->priv;
	int ret;

1302
	mutex_lock(&priv->mutex);
S
Sujith 已提交
1303
	ath9k_htc_ps_wakeup(priv);
1304 1305 1306 1307 1308
	ret = ath9k_htc_add_station(priv, vif, sta);
	if (!ret)
		ath9k_htc_init_rate(priv, sta);
	ath9k_htc_ps_restore(priv);
	mutex_unlock(&priv->mutex);
1309

1310 1311 1312 1313 1314 1315 1316 1317
	return ret;
}

static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
				struct ieee80211_vif *vif,
				struct ieee80211_sta *sta)
{
	struct ath9k_htc_priv *priv = hw->priv;
1318
	struct ath9k_htc_sta *ista;
1319
	int ret;
1320

1321 1322
	mutex_lock(&priv->mutex);
	ath9k_htc_ps_wakeup(priv);
1323 1324
	ista = (struct ath9k_htc_sta *) sta->drv_priv;
	htc_sta_drain(priv->htc, ista->index);
1325
	ret = ath9k_htc_remove_station(priv, vif, sta);
S
Sujith 已提交
1326
	ath9k_htc_ps_restore(priv);
1327
	mutex_unlock(&priv->mutex);
1328 1329

	return ret;
S
Sujith 已提交
1330 1331
}

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
static void ath9k_htc_sta_rc_update(struct ieee80211_hw *hw,
				    struct ieee80211_vif *vif,
				    struct ieee80211_sta *sta, u32 changed)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_target_rate trate;

	mutex_lock(&priv->mutex);
	ath9k_htc_ps_wakeup(priv);

	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
		memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
		ath9k_htc_setup_rate(priv, sta, &trate);
		if (!ath9k_htc_send_rate_cmd(priv, &trate))
			ath_dbg(common, CONFIG,
				"Supported rates for sta: %pM updated, rate caps: 0x%X\n",
				sta->addr, be32_to_cpu(trate.capflags));
		else
			ath_dbg(common, CONFIG,
				"Unable to update supported rates for sta: %pM\n",
				sta->addr);
	}

	ath9k_htc_ps_restore(priv);
	mutex_unlock(&priv->mutex);
}

1360 1361
static int ath9k_htc_conf_tx(struct ieee80211_hw *hw,
			     struct ieee80211_vif *vif, u16 queue,
S
Sujith 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
			     const struct ieee80211_tx_queue_params *params)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_tx_queue_info qi;
	int ret = 0, qnum;

	if (queue >= WME_NUM_AC)
		return 0;

	mutex_lock(&priv->mutex);
S
Sujith 已提交
1373
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1374 1375 1376 1377 1378 1379

	memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));

	qi.tqi_aifs = params->aifs;
	qi.tqi_cwmin = params->cw_min;
	qi.tqi_cwmax = params->cw_max;
1380
	qi.tqi_burstTime = params->txop * 32;
S
Sujith 已提交
1381 1382 1383

	qnum = get_hw_qnum(queue, priv->hwq_map);

1384
	ath_dbg(common, CONFIG,
J
Joe Perches 已提交
1385 1386 1387
		"Configure tx [queue/hwq] [%d/%d],  aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
		queue, qnum, params->aifs, params->cw_min,
		params->cw_max, params->txop);
S
Sujith 已提交
1388

1389
	ret = ath_htc_txq_update(priv, qnum, &qi);
S
Sujith 已提交
1390
	if (ret) {
1391
		ath_err(common, "TXQ Update failed\n");
S
Sujith 已提交
1392 1393
		goto out;
	}
S
Sujith 已提交
1394

S
Sujith 已提交
1395
	if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1396
	    (qnum == priv->hwq_map[WME_AC_BE]))
S
Sujith 已提交
1397 1398
		    ath9k_htc_beaconq_config(priv);
out:
S
Sujith 已提交
1399
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
	mutex_unlock(&priv->mutex);

	return ret;
}

static int ath9k_htc_set_key(struct ieee80211_hw *hw,
			     enum set_key_cmd cmd,
			     struct ieee80211_vif *vif,
			     struct ieee80211_sta *sta,
			     struct ieee80211_key_conf *key)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_common *common = ath9k_hw_common(priv->ah);
	int ret = 0;

1415
	if (htc_modparam_nohwcrypt)
S
Sujith 已提交
1416 1417
		return -ENOSPC;

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	if ((vif->type == NL80211_IFTYPE_ADHOC ||
	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
		/*
		 * For now, disable hw crypto for the RSN IBSS group keys. This
		 * could be optimized in the future to use a modified key cache
		 * design to support per-STA RX GTK, but until that gets
		 * implemented, use of software crypto for group addressed
		 * frames is a acceptable to allow RSN IBSS to be used.
		 */
		return -EOPNOTSUPP;
	}

S
Sujith 已提交
1433
	mutex_lock(&priv->mutex);
1434
	ath_dbg(common, CONFIG, "Set HW Key\n");
1435
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1436 1437 1438

	switch (cmd) {
	case SET_KEY:
1439
		ret = ath_key_config(common, vif, sta, key);
S
Sujith 已提交
1440 1441 1442 1443
		if (ret >= 0) {
			key->hw_key_idx = ret;
			/* push IV and Michael MIC generation to stack */
			key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1444
			if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
S
Sujith 已提交
1445
				key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1446 1447
			if (priv->ah->sw_mgmt_crypto &&
			    key->cipher == WLAN_CIPHER_SUITE_CCMP)
S
Sujith 已提交
1448 1449 1450 1451 1452
				key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
			ret = 0;
		}
		break;
	case DISABLE_KEY:
1453
		ath_key_delete(common, key);
S
Sujith 已提交
1454 1455 1456 1457 1458
		break;
	default:
		ret = -EINVAL;
	}

1459
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1460 1461 1462 1463 1464
	mutex_unlock(&priv->mutex);

	return ret;
}

1465 1466 1467 1468 1469
static void ath9k_htc_set_bssid(struct ath9k_htc_priv *priv)
{
	struct ath_common *common = ath9k_hw_common(priv->ah);

	ath9k_hw_write_associd(priv->ah);
1470
	ath_dbg(common, CONFIG, "BSSID: %pM aid: 0x%x\n",
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
		common->curbssid, common->curaid);
}

static void ath9k_htc_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
{
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;

	if ((vif->type == NL80211_IFTYPE_STATION) && bss_conf->assoc) {
		common->curaid = bss_conf->aid;
		memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
	}
}

static void ath9k_htc_choose_set_bssid(struct ath9k_htc_priv *priv)
{
	if (priv->num_sta_assoc_vif == 1) {
		ieee80211_iterate_active_interfaces_atomic(priv->hw,
							   ath9k_htc_bss_iter, priv);
		ath9k_htc_set_bssid(priv);
	}
}

S
Sujith 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
				       struct ieee80211_vif *vif,
				       struct ieee80211_bss_conf *bss_conf,
				       u32 changed)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);

	mutex_lock(&priv->mutex);
1505
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1506 1507

	if (changed & BSS_CHANGED_ASSOC) {
1508
		ath_dbg(common, CONFIG, "BSS Changed ASSOC %d\n",
1509
			bss_conf->assoc);
1510

1511 1512
		bss_conf->assoc ?
			priv->num_sta_assoc_vif++ : priv->num_sta_assoc_vif--;
1513

1514
		if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1515
			ath9k_htc_choose_set_bssid(priv);
1516
			if (bss_conf->assoc && (priv->num_sta_assoc_vif == 1))
1517
				ath9k_htc_start_ani(priv);
1518
			else if (priv->num_sta_assoc_vif == 0)
1519 1520
				ath9k_htc_stop_ani(priv);
		}
S
Sujith 已提交
1521 1522
	}

1523
	if (changed & BSS_CHANGED_IBSS) {
1524 1525
		if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
			common->curaid = bss_conf->aid;
1526
			memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1527
			ath9k_htc_set_bssid(priv);
1528
		}
S
Sujith 已提交
1529 1530
	}

1531
	if ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon) {
1532 1533
		ath_dbg(common, CONFIG, "Beacon enabled for BSS: %pM\n",
			bss_conf->bssid);
1534
		ath9k_htc_set_tsfadjust(priv, vif);
1535
		set_bit(OP_ENABLE_BEACON, &priv->op_flags);
1536
		ath9k_htc_beacon_config(priv, vif);
S
Sujith 已提交
1537 1538
	}

1539 1540 1541 1542 1543 1544
	if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon) {
		/*
		 * Disable SWBA interrupt only if there are no
		 * AP/IBSS interfaces.
		 */
		if ((priv->num_ap_vif <= 1) || priv->num_ibss_vif) {
1545
			ath_dbg(common, CONFIG,
1546 1547
				"Beacon disabled for BSS: %pM\n",
				bss_conf->bssid);
1548
			clear_bit(OP_ENABLE_BEACON, &priv->op_flags);
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
			ath9k_htc_beacon_config(priv, vif);
		}
	}

	if (changed & BSS_CHANGED_BEACON_INT) {
		/*
		 * Reset the HW TSF for the first AP interface.
		 */
		if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
		    (priv->nvifs == 1) &&
		    (priv->num_ap_vif == 1) &&
		    (vif->type == NL80211_IFTYPE_AP)) {
1561
			set_bit(OP_TSF_RESET, &priv->op_flags);
1562
		}
1563
		ath_dbg(common, CONFIG,
1564 1565
			"Beacon interval changed for BSS: %pM\n",
			bss_conf->bssid);
1566
		ath9k_htc_beacon_config(priv, vif);
S
Sujith 已提交
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	}

	if (changed & BSS_CHANGED_ERP_SLOT) {
		if (bss_conf->use_short_slot)
			ah->slottime = 9;
		else
			ah->slottime = 20;

		ath9k_hw_init_global_settings(ah);
	}

1578 1579 1580
	if (changed & BSS_CHANGED_HT)
		ath9k_htc_update_rate(priv, vif, bss_conf);

1581
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1582 1583 1584
	mutex_unlock(&priv->mutex);
}

1585 1586
static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw,
			     struct ieee80211_vif *vif)
S
Sujith 已提交
1587 1588 1589 1590 1591
{
	struct ath9k_htc_priv *priv = hw->priv;
	u64 tsf;

	mutex_lock(&priv->mutex);
S
Sujith 已提交
1592
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1593
	tsf = ath9k_hw_gettsf64(priv->ah);
S
Sujith 已提交
1594
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1595 1596 1597 1598 1599
	mutex_unlock(&priv->mutex);

	return tsf;
}

1600 1601
static void ath9k_htc_set_tsf(struct ieee80211_hw *hw,
			      struct ieee80211_vif *vif, u64 tsf)
S
Sujith 已提交
1602 1603 1604 1605
{
	struct ath9k_htc_priv *priv = hw->priv;

	mutex_lock(&priv->mutex);
S
Sujith 已提交
1606
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1607
	ath9k_hw_settsf64(priv->ah, tsf);
S
Sujith 已提交
1608
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1609 1610 1611
	mutex_unlock(&priv->mutex);
}

1612 1613
static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw,
				struct ieee80211_vif *vif)
S
Sujith 已提交
1614 1615 1616 1617
{
	struct ath9k_htc_priv *priv = hw->priv;

	mutex_lock(&priv->mutex);
S
Sujith 已提交
1618
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1619
	ath9k_hw_reset_tsf(priv->ah);
1620
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1621
	mutex_unlock(&priv->mutex);
S
Sujith 已提交
1622 1623 1624 1625 1626 1627
}

static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
				  struct ieee80211_vif *vif,
				  enum ieee80211_ampdu_mlme_action action,
				  struct ieee80211_sta *sta,
1628
				  u16 tid, u16 *ssn, u8 buf_size)
S
Sujith 已提交
1629 1630 1631
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath9k_htc_sta *ista;
S
Sujith 已提交
1632
	int ret = 0;
S
Sujith 已提交
1633

1634
	mutex_lock(&priv->mutex);
1635
	ath9k_htc_ps_wakeup(priv);
1636

S
Sujith 已提交
1637 1638 1639 1640 1641 1642
	switch (action) {
	case IEEE80211_AMPDU_RX_START:
		break;
	case IEEE80211_AMPDU_RX_STOP:
		break;
	case IEEE80211_AMPDU_TX_START:
S
Sujith 已提交
1643 1644 1645 1646
		ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
		if (!ret)
			ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
		break;
S
Sujith 已提交
1647
	case IEEE80211_AMPDU_TX_STOP:
S
Sujith 已提交
1648 1649
		ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
S
Sujith 已提交
1650 1651 1652
		break;
	case IEEE80211_AMPDU_TX_OPERATIONAL:
		ista = (struct ath9k_htc_sta *) sta->drv_priv;
1653
		spin_lock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
1654
		ista->tid_state[tid] = AGGR_OPERATIONAL;
1655
		spin_unlock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
1656 1657
		break;
	default:
1658
		ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
S
Sujith 已提交
1659 1660
	}

1661
	ath9k_htc_ps_restore(priv);
1662 1663
	mutex_unlock(&priv->mutex);

S
Sujith 已提交
1664
	return ret;
S
Sujith 已提交
1665 1666 1667 1668 1669 1670 1671 1672
}

static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
{
	struct ath9k_htc_priv *priv = hw->priv;

	mutex_lock(&priv->mutex);
	spin_lock_bh(&priv->beacon_lock);
1673
	set_bit(OP_SCANNING, &priv->op_flags);
S
Sujith 已提交
1674
	spin_unlock_bh(&priv->beacon_lock);
1675
	cancel_work_sync(&priv->ps_work);
1676
	ath9k_htc_stop_ani(priv);
S
Sujith 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685
	mutex_unlock(&priv->mutex);
}

static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
{
	struct ath9k_htc_priv *priv = hw->priv;

	mutex_lock(&priv->mutex);
	spin_lock_bh(&priv->beacon_lock);
1686
	clear_bit(OP_SCANNING, &priv->op_flags);
S
Sujith 已提交
1687
	spin_unlock_bh(&priv->beacon_lock);
1688 1689
	ath9k_htc_ps_wakeup(priv);
	ath9k_htc_vif_reconfig(priv);
1690
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1691
	mutex_unlock(&priv->mutex);
S
Sujith 已提交
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
}

static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
{
	return 0;
}

static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
					 u8 coverage_class)
{
	struct ath9k_htc_priv *priv = hw->priv;

	mutex_lock(&priv->mutex);
S
Sujith 已提交
1705
	ath9k_htc_ps_wakeup(priv);
S
Sujith 已提交
1706 1707
	priv->ah->coverage_class = coverage_class;
	ath9k_hw_init_global_settings(priv->ah);
S
Sujith 已提交
1708
	ath9k_htc_ps_restore(priv);
S
Sujith 已提交
1709 1710 1711
	mutex_unlock(&priv->mutex);
}

1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
/*
 * Currently, this is used only for selecting the minimum rate
 * for management frames, rate selection for data frames remain
 * unaffected.
 */
static int ath9k_htc_set_bitrate_mask(struct ieee80211_hw *hw,
				      struct ieee80211_vif *vif,
				      const struct cfg80211_bitrate_mask *mask)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_common *common = ath9k_hw_common(priv->ah);
	struct ath9k_htc_target_rate_mask tmask;
	struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
	int ret = 0;
	u8 cmd_rsp;

	memset(&tmask, 0, sizeof(struct ath9k_htc_target_rate_mask));

	tmask.vif_index = avp->index;
	tmask.band = IEEE80211_BAND_2GHZ;
	tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_2GHZ].legacy);

	WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
	if (ret) {
		ath_err(common,
			"Unable to set 2G rate mask for "
			"interface at idx: %d\n", avp->index);
		goto out;
	}

	tmask.band = IEEE80211_BAND_5GHZ;
	tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_5GHZ].legacy);

	WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
	if (ret) {
		ath_err(common,
			"Unable to set 5G rate mask for "
			"interface at idx: %d\n", avp->index);
		goto out;
	}

1753
	ath_dbg(common, CONFIG, "Set bitrate masks: 0x%x, 0x%x\n",
1754 1755 1756 1757 1758 1759
		mask->control[IEEE80211_BAND_2GHZ].legacy,
		mask->control[IEEE80211_BAND_5GHZ].legacy);
out:
	return ret;
}

1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775

static int ath9k_htc_get_stats(struct ieee80211_hw *hw,
			       struct ieee80211_low_level_stats *stats)
{
	struct ath9k_htc_priv *priv = hw->priv;
	struct ath_hw *ah = priv->ah;
	struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;

	stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
	stats->dot11RTSFailureCount = mib_stats->rts_bad;
	stats->dot11FCSErrorCount = mib_stats->fcs_bad;
	stats->dot11RTSSuccessCount = mib_stats->rts_good;

	return 0;
}

S
Sujith 已提交
1776 1777 1778 1779 1780 1781 1782 1783
struct ieee80211_ops ath9k_htc_ops = {
	.tx                 = ath9k_htc_tx,
	.start              = ath9k_htc_start,
	.stop               = ath9k_htc_stop,
	.add_interface      = ath9k_htc_add_interface,
	.remove_interface   = ath9k_htc_remove_interface,
	.config             = ath9k_htc_config,
	.configure_filter   = ath9k_htc_configure_filter,
1784 1785
	.sta_add            = ath9k_htc_sta_add,
	.sta_remove         = ath9k_htc_sta_remove,
S
Sujith 已提交
1786
	.conf_tx            = ath9k_htc_conf_tx,
1787
	.sta_rc_update      = ath9k_htc_sta_rc_update,
S
Sujith 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
	.bss_info_changed   = ath9k_htc_bss_info_changed,
	.set_key            = ath9k_htc_set_key,
	.get_tsf            = ath9k_htc_get_tsf,
	.set_tsf            = ath9k_htc_set_tsf,
	.reset_tsf          = ath9k_htc_reset_tsf,
	.ampdu_action       = ath9k_htc_ampdu_action,
	.sw_scan_start      = ath9k_htc_sw_scan_start,
	.sw_scan_complete   = ath9k_htc_sw_scan_complete,
	.set_rts_threshold  = ath9k_htc_set_rts_threshold,
	.rfkill_poll        = ath9k_htc_rfkill_poll_state,
	.set_coverage_class = ath9k_htc_set_coverage_class,
1799
	.set_bitrate_mask   = ath9k_htc_set_bitrate_mask,
1800
	.get_stats	    = ath9k_htc_get_stats,
S
Sujith 已提交
1801
};