htc_drv_txrx.c 19.4 KB
Newer Older
S
Sujith 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (c) 2010 Atheros Communications Inc.
 *
 * 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"

/******/
/* TX */
/******/

23 24 25 26 27 28 29
static const int subtype_txq_to_hwq[] = {
	[WME_AC_BE] = ATH_TXQ_AC_BE,
	[WME_AC_BK] = ATH_TXQ_AC_BK,
	[WME_AC_VI] = ATH_TXQ_AC_VI,
	[WME_AC_VO] = ATH_TXQ_AC_VO,
};

S
Sujith 已提交
30
#define ATH9K_HTC_INIT_TXQ(subtype) do {			\
31
		qi.tqi_subtype = subtype_txq_to_hwq[subtype];	\
S
Sujith 已提交
32 33 34 35 36 37 38 39
		qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;		\
		qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;		\
		qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;		\
		qi.tqi_physCompBuf = 0;				\
		qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |	\
			TXQ_FLAG_TXDESCINT_ENABLE;		\
	} while (0)

S
Sujith 已提交
40 41 42 43
int get_hw_qnum(u16 queue, int *hwq_map)
{
	switch (queue) {
	case 0:
44
		return hwq_map[WME_AC_VO];
S
Sujith 已提交
45
	case 1:
46
		return hwq_map[WME_AC_VI];
S
Sujith 已提交
47
	case 2:
48
		return hwq_map[WME_AC_BE];
S
Sujith 已提交
49
	case 3:
50
		return hwq_map[WME_AC_BK];
S
Sujith 已提交
51
	default:
52
		return hwq_map[WME_AC_BE];
S
Sujith 已提交
53 54 55
	}
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
void ath9k_htc_check_stop_queues(struct ath9k_htc_priv *priv)
{
	spin_lock_bh(&priv->tx.tx_lock);
	priv->tx.queued_cnt++;
	if ((priv->tx.queued_cnt >= ATH9K_HTC_TX_THRESHOLD) &&
	    !(priv->tx.flags & ATH9K_HTC_OP_TX_QUEUES_STOP)) {
		priv->tx.flags |= ATH9K_HTC_OP_TX_QUEUES_STOP;
		ieee80211_stop_queues(priv->hw);
	}
	spin_unlock_bh(&priv->tx.tx_lock);
}

void ath9k_htc_check_wake_queues(struct ath9k_htc_priv *priv)
{
	spin_lock_bh(&priv->tx.tx_lock);
	if ((priv->tx.queued_cnt < ATH9K_HTC_TX_THRESHOLD) &&
	    (priv->tx.flags & ATH9K_HTC_OP_TX_QUEUES_STOP)) {
		priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
		ieee80211_wake_queues(priv->hw);
	}
	spin_unlock_bh(&priv->tx.tx_lock);
}

79 80
int ath_htc_txq_update(struct ath9k_htc_priv *priv, int qnum,
		       struct ath9k_tx_queue_info *qinfo)
S
Sujith 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94
{
	struct ath_hw *ah = priv->ah;
	int error = 0;
	struct ath9k_tx_queue_info qi;

	ath9k_hw_get_txq_props(ah, qnum, &qi);

	qi.tqi_aifs = qinfo->tqi_aifs;
	qi.tqi_cwmin = qinfo->tqi_cwmin / 2; /* XXX */
	qi.tqi_cwmax = qinfo->tqi_cwmax;
	qi.tqi_burstTime = qinfo->tqi_burstTime;
	qi.tqi_readyTime = qinfo->tqi_readyTime;

	if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
95 96
		ath_err(ath9k_hw_common(ah),
			"Unable to update hardware queue %u!\n", qnum);
S
Sujith 已提交
97 98 99 100 101 102 103 104
		error = -EIO;
	} else {
		ath9k_hw_resettxqueue(ah, qnum);
	}

	return error;
}

105 106
int ath9k_htc_tx_start(struct ath9k_htc_priv *priv,
		       struct sk_buff *skb, bool is_cab)
S
Sujith 已提交
107 108
{
	struct ieee80211_hdr *hdr;
109
	struct ieee80211_mgmt *mgmt;
S
Sujith 已提交
110 111
	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
	struct ieee80211_sta *sta = tx_info->control.sta;
112
	struct ieee80211_vif *vif = tx_info->control.vif;
S
Sujith 已提交
113
	struct ath9k_htc_sta *ista;
114
	struct ath9k_htc_vif *avp = NULL;
115
	struct ath9k_htc_tx_ctl *tx_ctl;
S
Sujith 已提交
116
	enum htc_endpoint_id epid;
S
Sujith 已提交
117
	u16 qnum;
S
Sujith 已提交
118 119
	__le16 fc;
	u8 *tx_fhdr;
120
	u8 sta_idx, vif_idx;
S
Sujith 已提交
121

122 123 124
	tx_ctl = HTC_SKB_CB(skb);
	memset(tx_ctl, 0, sizeof(*tx_ctl));

S
Sujith 已提交
125 126 127
	hdr = (struct ieee80211_hdr *) skb->data;
	fc = hdr->frame_control;

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	/*
	 * Find out on which interface this packet has to be
	 * sent out.
	 */
	if (vif) {
		avp = (struct ath9k_htc_vif *) vif->drv_priv;
		vif_idx = avp->index;
	} else {
		if (!priv->ah->is_monitoring) {
			ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
				"VIF is null, but no monitor interface !\n");
			return -EINVAL;
		}

		vif_idx = priv->mon_vif_idx;
	}
144

145 146 147
	/*
	 * Find out which station this packet is destined for.
	 */
S
Sujith 已提交
148 149 150 151
	if (sta) {
		ista = (struct ath9k_htc_sta *) sta->drv_priv;
		sta_idx = ista->index;
	} else {
152
		sta_idx = priv->vif_sta_pos[vif_idx];
S
Sujith 已提交
153 154 155 156
	}

	if (ieee80211_is_data(fc)) {
		struct tx_frame_hdr tx_hdr;
157
		u32 flags = 0;
S
Sujith 已提交
158 159 160 161 162
		u8 *qc;

		memset(&tx_hdr, 0, sizeof(struct tx_frame_hdr));

		tx_hdr.node_idx = sta_idx;
163
		tx_hdr.vif_idx = vif_idx;
S
Sujith 已提交
164 165

		if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
166
			tx_ctl->type = ATH9K_HTC_AMPDU;
S
Sujith 已提交
167 168
			tx_hdr.data_type = ATH9K_HTC_AMPDU;
		} else {
169
			tx_ctl->type = ATH9K_HTC_NORMAL;
S
Sujith 已提交
170 171 172
			tx_hdr.data_type = ATH9K_HTC_NORMAL;
		}

173
		if (ieee80211_is_data_qos(fc)) {
S
Sujith 已提交
174 175 176 177 178 179 180
			qc = ieee80211_get_qos_ctl(hdr);
			tx_hdr.tidno = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
		}

		/* Check for RTS protection */
		if (priv->hw->wiphy->rts_threshold != (u32) -1)
			if (skb->len > priv->hw->wiphy->rts_threshold)
181
				flags |= ATH9K_HTC_TX_RTSCTS;
S
Sujith 已提交
182 183

		/* CTS-to-self */
184
		if (!(flags & ATH9K_HTC_TX_RTSCTS) &&
185
		    (vif && vif->bss_conf.use_cts_prot))
186
			flags |= ATH9K_HTC_TX_CTSONLY;
S
Sujith 已提交
187

188
		tx_hdr.flags = cpu_to_be32(flags);
S
Sujith 已提交
189 190 191 192 193 194 195 196 197
		tx_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
		if (tx_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
			tx_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
		else
			tx_hdr.keyix = tx_info->control.hw_key->hw_key_idx;

		tx_fhdr = skb_push(skb, sizeof(tx_hdr));
		memcpy(tx_fhdr, (u8 *) &tx_hdr, sizeof(tx_hdr));

198 199 200 201 202 203
		if (is_cab) {
			CAB_STAT_INC;
			epid = priv->cab_ep;
			goto send;
		}

S
Sujith 已提交
204 205
		qnum = skb_get_queue_mapping(skb);

S
Sujith 已提交
206
		switch (qnum) {
S
Sujith 已提交
207
		case 0:
S
Sujith 已提交
208 209
			TX_QSTAT_INC(WME_AC_VO);
			epid = priv->data_vo_ep;
S
Sujith 已提交
210
			break;
S
Sujith 已提交
211
		case 1:
212
			TX_QSTAT_INC(WME_AC_VI);
S
Sujith 已提交
213 214
			epid = priv->data_vi_ep;
			break;
S
Sujith 已提交
215 216 217
		case 2:
			TX_QSTAT_INC(WME_AC_BE);
			epid = priv->data_be_ep;
S
Sujith 已提交
218
			break;
S
Sujith 已提交
219
		case 3:
S
Sujith 已提交
220
		default:
221
			TX_QSTAT_INC(WME_AC_BK);
S
Sujith 已提交
222 223 224 225 226 227 228 229
			epid = priv->data_bk_ep;
			break;
		}
	} else {
		struct tx_mgmt_hdr mgmt_hdr;

		memset(&mgmt_hdr, 0, sizeof(struct tx_mgmt_hdr));

230 231 232 233 234 235 236 237 238
		/*
		 * Set the TSF adjust value for probe response
		 * frame also.
		 */
		if (avp && unlikely(ieee80211_is_probe_resp(fc))) {
			mgmt = (struct ieee80211_mgmt *)skb->data;
			mgmt->u.probe_resp.timestamp = avp->tsfadjust;
		}

239
		tx_ctl->type = ATH9K_HTC_MGMT;
S
Sujith 已提交
240 241

		mgmt_hdr.node_idx = sta_idx;
242
		mgmt_hdr.vif_idx = vif_idx;
S
Sujith 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255
		mgmt_hdr.tidno = 0;
		mgmt_hdr.flags = 0;

		mgmt_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
		if (mgmt_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
			mgmt_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
		else
			mgmt_hdr.keyix = tx_info->control.hw_key->hw_key_idx;

		tx_fhdr = skb_push(skb, sizeof(mgmt_hdr));
		memcpy(tx_fhdr, (u8 *) &mgmt_hdr, sizeof(mgmt_hdr));
		epid = priv->mgmt_ep;
	}
256
send:
257
	return htc_send(priv->htc, skb, epid);
S
Sujith 已提交
258 259
}

S
Sujith 已提交
260 261 262 263 264
static bool ath9k_htc_check_tx_aggr(struct ath9k_htc_priv *priv,
				    struct ath9k_htc_sta *ista, u8 tid)
{
	bool ret = false;

265
	spin_lock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
266 267
	if ((tid < ATH9K_HTC_MAX_TID) && (ista->tid_state[tid] == AGGR_STOP))
		ret = true;
268
	spin_unlock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
269 270 271 272

	return ret;
}

S
Sujith 已提交
273 274 275
void ath9k_tx_tasklet(unsigned long data)
{
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
276
	struct ieee80211_vif *vif;
S
Sujith 已提交
277 278 279 280 281 282
	struct ieee80211_sta *sta;
	struct ieee80211_hdr *hdr;
	struct ieee80211_tx_info *tx_info;
	struct sk_buff *skb = NULL;
	__le16 fc;

283
	while ((skb = skb_dequeue(&priv->tx.tx_queue)) != NULL) {
S
Sujith 已提交
284 285 286 287

		hdr = (struct ieee80211_hdr *) skb->data;
		fc = hdr->frame_control;
		tx_info = IEEE80211_SKB_CB(skb);
288
		vif = tx_info->control.vif;
289 290

		memset(&tx_info->status, 0, sizeof(tx_info->status));
S
Sujith 已提交
291

292 293 294
		if (!vif)
			goto send_mac80211;

S
Sujith 已提交
295 296
		rcu_read_lock();

297
		sta = ieee80211_find_sta(vif, hdr->addr1);
298 299 300 301 302 303 304 305
		if (!sta) {
			rcu_read_unlock();
			ieee80211_tx_status(priv->hw, skb);
			continue;
		}

		/* Check if we need to start aggregation */

S
Sujith 已提交
306
		if (sta && conf_is_ht(&priv->hw->conf) &&
S
Sujith 已提交
307
		    !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
S
Sujith 已提交
308 309 310 311 312 313 314 315
			if (ieee80211_is_data_qos(fc)) {
				u8 *qc, tid;
				struct ath9k_htc_sta *ista;

				qc = ieee80211_get_qos_ctl(hdr);
				tid = qc[0] & 0xf;
				ista = (struct ath9k_htc_sta *)sta->drv_priv;

S
Sujith 已提交
316
				if (ath9k_htc_check_tx_aggr(priv, ista, tid)) {
317
					ieee80211_start_tx_ba_session(sta, tid, 0);
318
					spin_lock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
319
					ista->tid_state[tid] = AGGR_PROGRESS;
320
					spin_unlock_bh(&priv->tx.tx_lock);
S
Sujith 已提交
321 322 323 324 325 326
				}
			}
		}

		rcu_read_unlock();

327
	send_mac80211:
328 329 330 331 332
		spin_lock_bh(&priv->tx.tx_lock);
		if (WARN_ON(--priv->tx.queued_cnt < 0))
			priv->tx.queued_cnt = 0;
		spin_unlock_bh(&priv->tx.tx_lock);

333
		/* Send status to mac80211 */
S
Sujith 已提交
334 335
		ieee80211_tx_status(priv->hw, skb);
	}
S
Sujith 已提交
336 337

	/* Wake TX queues if needed */
338
	ath9k_htc_check_wake_queues(priv);
S
Sujith 已提交
339 340 341 342 343 344
}

void ath9k_htc_txep(void *drv_priv, struct sk_buff *skb,
		    enum htc_endpoint_id ep_id, bool txok)
{
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) drv_priv;
S
Sujith 已提交
345
	struct ath_common *common = ath9k_hw_common(priv->ah);
S
Sujith 已提交
346 347 348 349 350
	struct ieee80211_tx_info *tx_info;

	if (!skb)
		return;

S
Sujith 已提交
351
	if (ep_id == priv->mgmt_ep) {
S
Sujith 已提交
352
		skb_pull(skb, sizeof(struct tx_mgmt_hdr));
S
Sujith 已提交
353 354 355
	} else if ((ep_id == priv->data_bk_ep) ||
		   (ep_id == priv->data_be_ep) ||
		   (ep_id == priv->data_vi_ep) ||
356 357
		   (ep_id == priv->data_vo_ep) ||
		   (ep_id == priv->cab_ep)) {
S
Sujith 已提交
358
		skb_pull(skb, sizeof(struct tx_frame_hdr));
S
Sujith 已提交
359
	} else {
360
		ath_err(common, "Unsupported TX EPID: %d\n", ep_id);
S
Sujith 已提交
361 362 363
		dev_kfree_skb_any(skb);
		return;
	}
S
Sujith 已提交
364 365 366 367 368 369

	tx_info = IEEE80211_SKB_CB(skb);

	if (txok)
		tx_info->flags |= IEEE80211_TX_STAT_ACK;

370
	skb_queue_tail(&priv->tx.tx_queue, skb);
S
Sujith 已提交
371 372 373 374 375
	tasklet_schedule(&priv->tx_tasklet);
}

int ath9k_tx_init(struct ath9k_htc_priv *priv)
{
376
	skb_queue_head_init(&priv->tx.tx_queue);
S
Sujith 已提交
377 378 379 380 381 382 383 384
	return 0;
}

void ath9k_tx_cleanup(struct ath9k_htc_priv *priv)
{

}

385
bool ath9k_htc_txq_setup(struct ath9k_htc_priv *priv, int subtype)
S
Sujith 已提交
386 387 388 389 390 391 392
{
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_tx_queue_info qi;
	int qnum;

	memset(&qi, 0, sizeof(qi));
S
Sujith 已提交
393
	ATH9K_HTC_INIT_TXQ(subtype);
S
Sujith 已提交
394 395 396 397 398 399

	qnum = ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_DATA, &qi);
	if (qnum == -1)
		return false;

	if (qnum >= ARRAY_SIZE(priv->hwq_map)) {
400 401
		ath_err(common, "qnum %u out of range, max %zu!\n",
			qnum, ARRAY_SIZE(priv->hwq_map));
S
Sujith 已提交
402 403 404 405 406 407 408 409
		ath9k_hw_releasetxqueue(ah, qnum);
		return false;
	}

	priv->hwq_map[subtype] = qnum;
	return true;
}

S
Sujith 已提交
410 411 412 413 414 415 416 417 418 419
int ath9k_htc_cabq_setup(struct ath9k_htc_priv *priv)
{
	struct ath9k_tx_queue_info qi;

	memset(&qi, 0, sizeof(qi));
	ATH9K_HTC_INIT_TXQ(0);

	return ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_CAB, &qi);
}

S
Sujith 已提交
420 421 422 423
/******/
/* RX */
/******/

424 425 426 427 428 429 430 431 432 433 434 435 436 437
/*
 * Calculate the RX filter to be set in the HW.
 */
u32 ath9k_htc_calcrxfilter(struct ath9k_htc_priv *priv)
{
#define	RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)

	struct ath_hw *ah = priv->ah;
	u32 rfilt;

	rfilt = (ath9k_hw_getrxfilter(ah) & RX_FILTER_PRESERVE)
		| ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
		| ATH9K_RX_FILTER_MCAST;

438
	if (priv->rxfilter & FIF_PROBE_REQ)
439 440 441 442 443 444 445 446 447
		rfilt |= ATH9K_RX_FILTER_PROBEREQ;

	/*
	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
	 * mode interface or when in monitor mode. AP mode does not need this
	 * since it receives all in-BSS frames anyway.
	 */
	if (((ah->opmode != NL80211_IFTYPE_AP) &&
	     (priv->rxfilter & FIF_PROMISC_IN_BSS)) ||
S
Sujith Manoharan 已提交
448
	    ah->is_monitoring)
449 450 451 452 453 454 455 456 457 458 459
		rfilt |= ATH9K_RX_FILTER_PROM;

	if (priv->rxfilter & FIF_CONTROL)
		rfilt |= ATH9K_RX_FILTER_CONTROL;

	if ((ah->opmode == NL80211_IFTYPE_STATION) &&
	    !(priv->rxfilter & FIF_BCN_PRBRESP_PROMISC))
		rfilt |= ATH9K_RX_FILTER_MYBEACON;
	else
		rfilt |= ATH9K_RX_FILTER_BEACON;

S
Sujith Manoharan 已提交
460
	if (conf_is_ht(&priv->hw->conf)) {
461
		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
S
Sujith Manoharan 已提交
462 463 464 465 466
		rfilt |= ATH9K_RX_FILTER_UNCOMP_BA_BAR;
	}

	if (priv->rxfilter & FIF_PSPOLL)
		rfilt |= ATH9K_RX_FILTER_PSPOLL;
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

	return rfilt;

#undef RX_FILTER_PRESERVE
}

/*
 * Recv initialization for opmode change.
 */
static void ath9k_htc_opmode_init(struct ath9k_htc_priv *priv)
{
	struct ath_hw *ah = priv->ah;
	u32 rfilt, mfilt[2];

	/* configure rx filter */
	rfilt = ath9k_htc_calcrxfilter(priv);
	ath9k_hw_setrxfilter(ah, rfilt);

	/* calculate and install multicast filter */
	mfilt[0] = mfilt[1] = ~0;
	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
}

S
Sujith 已提交
490 491 492
void ath9k_host_rx_init(struct ath9k_htc_priv *priv)
{
	ath9k_hw_rxena(priv->ah);
493
	ath9k_htc_opmode_init(priv);
494
	ath9k_hw_startpcureceive(priv->ah, (priv->op_flags & OP_SCANNING));
S
Sujith 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	priv->rx.last_rssi = ATH_RSSI_DUMMY_MARKER;
}

static void ath9k_process_rate(struct ieee80211_hw *hw,
			       struct ieee80211_rx_status *rxs,
			       u8 rx_rate, u8 rs_flags)
{
	struct ieee80211_supported_band *sband;
	enum ieee80211_band band;
	unsigned int i = 0;

	if (rx_rate & 0x80) {
		/* HT rate */
		rxs->flag |= RX_FLAG_HT;
		if (rs_flags & ATH9K_RX_2040)
			rxs->flag |= RX_FLAG_40MHZ;
		if (rs_flags & ATH9K_RX_GI)
			rxs->flag |= RX_FLAG_SHORT_GI;
		rxs->rate_idx = rx_rate & 0x7f;
		return;
	}

	band = hw->conf.channel->band;
	sband = hw->wiphy->bands[band];

	for (i = 0; i < sband->n_bitrates; i++) {
		if (sband->bitrates[i].hw_value == rx_rate) {
			rxs->rate_idx = i;
			return;
		}
		if (sband->bitrates[i].hw_value_short == rx_rate) {
			rxs->rate_idx = i;
			rxs->flag |= RX_FLAG_SHORTPRE;
			return;
		}
	}

}

static bool ath9k_rx_prepare(struct ath9k_htc_priv *priv,
			     struct ath9k_htc_rxbuf *rxbuf,
			     struct ieee80211_rx_status *rx_status)

{
	struct ieee80211_hdr *hdr;
	struct ieee80211_hw *hw = priv->hw;
	struct sk_buff *skb = rxbuf->skb;
	struct ath_common *common = ath9k_hw_common(priv->ah);
S
Sujith 已提交
543
	struct ath_htc_rx_status *rxstatus;
S
Sujith 已提交
544 545 546 547
	int hdrlen, padpos, padsize;
	int last_rssi = ATH_RSSI_DUMMY_MARKER;
	__le16 fc;

548 549 550
	if (skb->len < HTC_RX_FRAME_HEADER_SIZE) {
		ath_err(common, "Corrupted RX frame, dropping (len: %d)\n",
			skb->len);
S
Sujith 已提交
551 552 553 554 555 556 557
		goto rx_next;
	}

	rxstatus = (struct ath_htc_rx_status *)skb->data;

	if (be16_to_cpu(rxstatus->rs_datalen) -
	    (skb->len - HTC_RX_FRAME_HEADER_SIZE) != 0) {
558 559 560
		ath_err(common,
			"Corrupted RX data len, dropping (dlen: %d, skblen: %d)\n",
			rxstatus->rs_datalen, skb->len);
S
Sujith 已提交
561 562 563
		goto rx_next;
	}

564 565
	ath9k_htc_err_stat_rx(priv, rxstatus);

S
Sujith 已提交
566 567 568 569
	/* Get the RX status information */
	memcpy(&rxbuf->rxstatus, rxstatus, HTC_RX_FRAME_HEADER_SIZE);
	skb_pull(skb, HTC_RX_FRAME_HEADER_SIZE);

S
Sujith 已提交
570 571 572 573 574 575 576
	hdr = (struct ieee80211_hdr *)skb->data;
	fc = hdr->frame_control;
	hdrlen = ieee80211_get_hdrlen_from_skb(skb);

	padpos = ath9k_cmn_padpos(fc);

	padsize = padpos & 3;
S
Sujith 已提交
577
	if (padsize && skb->len >= padpos+padsize+FCS_LEN) {
S
Sujith 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
		memmove(skb->data + padsize, skb->data, padpos);
		skb_pull(skb, padsize);
	}

	memset(rx_status, 0, sizeof(struct ieee80211_rx_status));

	if (rxbuf->rxstatus.rs_status != 0) {
		if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_CRC)
			rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
		if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_PHY)
			goto rx_next;

		if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT) {
			/* FIXME */
		} else if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_MIC) {
			if (ieee80211_is_ctl(fc))
				/*
				 * Sometimes, we get invalid
				 * MIC failures on valid control frames.
				 * Remove these mic errors.
				 */
				rxbuf->rxstatus.rs_status &= ~ATH9K_RXERR_MIC;
			else
				rx_status->flag |= RX_FLAG_MMIC_ERROR;
		}

		/*
		 * Reject error frames with the exception of
		 * decryption and MIC failures. For monitor mode,
		 * we also ignore the CRC error.
		 */
		if (priv->ah->opmode == NL80211_IFTYPE_MONITOR) {
			if (rxbuf->rxstatus.rs_status &
			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
			      ATH9K_RXERR_CRC))
				goto rx_next;
		} else {
			if (rxbuf->rxstatus.rs_status &
			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
				goto rx_next;
			}
		}
	}

	if (!(rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT)) {
		u8 keyix;
		keyix = rxbuf->rxstatus.rs_keyix;
		if (keyix != ATH9K_RXKEYIX_INVALID) {
			rx_status->flag |= RX_FLAG_DECRYPTED;
		} else if (ieee80211_has_protected(fc) &&
			   skb->len >= hdrlen + 4) {
			keyix = skb->data[hdrlen + 3] >> 6;
			if (test_bit(keyix, common->keymap))
				rx_status->flag |= RX_FLAG_DECRYPTED;
		}
	}

	ath9k_process_rate(hw, rx_status, rxbuf->rxstatus.rs_rate,
			   rxbuf->rxstatus.rs_flags);

638 639 640 641
	if (rxbuf->rxstatus.rs_rssi != ATH9K_RSSI_BAD &&
	    !rxbuf->rxstatus.rs_moreaggr)
		ATH_RSSI_LPF(priv->rx.last_rssi,
			     rxbuf->rxstatus.rs_rssi);
S
Sujith 已提交
642

643
	last_rssi = priv->rx.last_rssi;
S
Sujith 已提交
644

645 646 647
	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
		rxbuf->rxstatus.rs_rssi = ATH_EP_RND(last_rssi,
						     ATH_RSSI_EP_MULTIPLIER);
S
Sujith 已提交
648

649 650
	if (rxbuf->rxstatus.rs_rssi < 0)
		rxbuf->rxstatus.rs_rssi = 0;
S
Sujith 已提交
651

652 653
	if (ieee80211_is_beacon(fc))
		priv->ah->stats.avgbrssi = rxbuf->rxstatus.rs_rssi;
S
Sujith 已提交
654

S
Sujith 已提交
655
	rx_status->mactime = be64_to_cpu(rxbuf->rxstatus.rs_tstamp);
S
Sujith 已提交
656 657 658 659
	rx_status->band = hw->conf.channel->band;
	rx_status->freq = hw->conf.channel->center_freq;
	rx_status->signal =  rxbuf->rxstatus.rs_rssi + ATH_DEFAULT_NOISE_FLOOR;
	rx_status->antenna = rxbuf->rxstatus.rs_antenna;
J
Johannes Berg 已提交
660
	rx_status->flag |= RX_FLAG_MACTIME_MPDU;
S
Sujith 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

	return true;

rx_next:
	return false;
}

/*
 * FIXME: Handle FLUSH later on.
 */
void ath9k_rx_tasklet(unsigned long data)
{
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
	struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
	struct ieee80211_rx_status rx_status;
	struct sk_buff *skb;
	unsigned long flags;
678
	struct ieee80211_hdr *hdr;
S
Sujith 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

	do {
		spin_lock_irqsave(&priv->rx.rxbuflock, flags);
		list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
			if (tmp_buf->in_process) {
				rxbuf = tmp_buf;
				break;
			}
		}

		if (rxbuf == NULL) {
			spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
			break;
		}

		if (!rxbuf->skb)
			goto requeue;

		if (!ath9k_rx_prepare(priv, rxbuf, &rx_status)) {
			dev_kfree_skb_any(rxbuf->skb);
			goto requeue;
		}

		memcpy(IEEE80211_SKB_RXCB(rxbuf->skb), &rx_status,
		       sizeof(struct ieee80211_rx_status));
		skb = rxbuf->skb;
705 706 707 708 709
		hdr = (struct ieee80211_hdr *) skb->data;

		if (ieee80211_is_beacon(hdr->frame_control) && priv->ps_enabled)
				ieee80211_queue_work(priv->hw, &priv->ps_work);

S
Sujith 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);

		ieee80211_rx(priv->hw, skb);

		spin_lock_irqsave(&priv->rx.rxbuflock, flags);
requeue:
		rxbuf->in_process = false;
		rxbuf->skb = NULL;
		list_move_tail(&rxbuf->list, &priv->rx.rxbuf);
		rxbuf = NULL;
		spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
	} while (1);

}

void ath9k_htc_rxep(void *drv_priv, struct sk_buff *skb,
		    enum htc_endpoint_id ep_id)
{
	struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)drv_priv;
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;

	spin_lock(&priv->rx.rxbuflock);
	list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
		if (!tmp_buf->in_process) {
			rxbuf = tmp_buf;
			break;
		}
	}
	spin_unlock(&priv->rx.rxbuflock);

	if (rxbuf == NULL) {
J
Joe Perches 已提交
743 744
		ath_dbg(common, ATH_DBG_ANY,
			"No free RX buffer\n");
S
Sujith 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
		goto err;
	}

	spin_lock(&priv->rx.rxbuflock);
	rxbuf->skb = skb;
	rxbuf->in_process = true;
	spin_unlock(&priv->rx.rxbuflock);

	tasklet_schedule(&priv->rx_tasklet);
	return;
err:
	dev_kfree_skb_any(skb);
}

/* FIXME: Locking for cleanup/init */

void ath9k_rx_cleanup(struct ath9k_htc_priv *priv)
{
	struct ath9k_htc_rxbuf *rxbuf, *tbuf;

	list_for_each_entry_safe(rxbuf, tbuf, &priv->rx.rxbuf, list) {
		list_del(&rxbuf->list);
		if (rxbuf->skb)
			dev_kfree_skb_any(rxbuf->skb);
		kfree(rxbuf);
	}
}

int ath9k_rx_init(struct ath9k_htc_priv *priv)
{
	struct ath_hw *ah = priv->ah;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_htc_rxbuf *rxbuf;
	int i = 0;

	INIT_LIST_HEAD(&priv->rx.rxbuf);
	spin_lock_init(&priv->rx.rxbuflock);

	for (i = 0; i < ATH9K_HTC_RXBUF; i++) {
		rxbuf = kzalloc(sizeof(struct ath9k_htc_rxbuf), GFP_KERNEL);
		if (rxbuf == NULL) {
786
			ath_err(common, "Unable to allocate RX buffers\n");
S
Sujith 已提交
787 788 789 790 791 792 793 794 795 796 797
			goto err;
		}
		list_add_tail(&rxbuf->list, &priv->rx.rxbuf);
	}

	return 0;

err:
	ath9k_rx_cleanup(priv);
	return -ENOMEM;
}