txrx.c 44.6 KB
Newer Older
Kalle Valo's avatar
Kalle Valo committed
1 2
/*
 * Copyright (c) 2004-2011 Atheros Communications Inc.
3
 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
Kalle Valo's avatar
Kalle Valo committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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.
 */

18 19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Kalle Valo's avatar
Kalle Valo committed
20 21
#include "core.h"
#include "debug.h"
Kalle Valo's avatar
Kalle Valo committed
22
#include "htc-ops.h"
23
#include "trace.h"
Kalle Valo's avatar
Kalle Valo committed
24

25 26 27 28 29
/*
 * tid - tid_mux0..tid_mux3
 * aid - tid_mux4..tid_mux7
 */
#define ATH6KL_TID_MASK 0xf
30
#define ATH6KL_AID_SHIFT 4
31 32 33 34 35 36

static inline u8 ath6kl_get_tid(u8 tid_mux)
{
	return tid_mux & ATH6KL_TID_MASK;
}

37 38 39 40 41
static inline u8 ath6kl_get_aid(u8 tid_mux)
{
	return tid_mux >> ATH6KL_AID_SHIFT;
}

Kalle Valo's avatar
Kalle Valo committed
42 43 44 45 46 47 48 49 50 51 52 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
static u8 ath6kl_ibss_map_epid(struct sk_buff *skb, struct net_device *dev,
			       u32 *map_no)
{
	struct ath6kl *ar = ath6kl_priv(dev);
	struct ethhdr *eth_hdr;
	u32 i, ep_map = -1;
	u8 *datap;

	*map_no = 0;
	datap = skb->data;
	eth_hdr = (struct ethhdr *) (datap + sizeof(struct wmi_data_hdr));

	if (is_multicast_ether_addr(eth_hdr->h_dest))
		return ENDPOINT_2;

	for (i = 0; i < ar->node_num; i++) {
		if (memcmp(eth_hdr->h_dest, ar->node_map[i].mac_addr,
			   ETH_ALEN) == 0) {
			*map_no = i + 1;
			ar->node_map[i].tx_pend++;
			return ar->node_map[i].ep_id;
		}

		if ((ep_map == -1) && !ar->node_map[i].tx_pend)
			ep_map = i;
	}

	if (ep_map == -1) {
		ep_map = ar->node_num;
		ar->node_num++;
		if (ar->node_num > MAX_NODE_NUM)
			return ENDPOINT_UNUSED;
	}

	memcpy(ar->node_map[ep_map].mac_addr, eth_hdr->h_dest, ETH_ALEN);

	for (i = ENDPOINT_2; i <= ENDPOINT_5; i++) {
		if (!ar->tx_pending[i]) {
			ar->node_map[ep_map].ep_id = i;
			break;
		}

		/*
		 * No free endpoint is available, start redistribution on
		 * the inuse endpoints.
		 */
		if (i == ENDPOINT_5) {
			ar->node_map[ep_map].ep_id = ar->next_ep_id;
			ar->next_ep_id++;
			if (ar->next_ep_id > ENDPOINT_5)
				ar->next_ep_id = ENDPOINT_2;
		}
	}

	*map_no = ep_map + 1;
	ar->node_map[ep_map].tx_pend++;

	return ar->node_map[ep_map].ep_id;
}

102 103 104 105 106 107 108 109
static bool ath6kl_process_uapsdq(struct ath6kl_sta *conn,
				struct ath6kl_vif *vif,
				struct sk_buff *skb,
				u32 *flags)
{
	struct ath6kl *ar = vif->ar;
	bool is_apsdq_empty = false;
	struct ethhdr *datap = (struct ethhdr *) skb->data;
110
	u8 up = 0, traffic_class, *ip_hdr;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	u16 ether_type;
	struct ath6kl_llc_snap_hdr *llc_hdr;

	if (conn->sta_flags & STA_PS_APSD_TRIGGER) {
		/*
		 * This tx is because of a uAPSD trigger, determine
		 * more and EOSP bit. Set EOSP if queue is empty
		 * or sufficient frames are delivered for this trigger.
		 */
		spin_lock_bh(&conn->psq_lock);
		if (!skb_queue_empty(&conn->apsdq))
			*flags |= WMI_DATA_HDR_FLAGS_MORE;
		else if (conn->sta_flags & STA_PS_APSD_EOSP)
			*flags |= WMI_DATA_HDR_FLAGS_EOSP;
		*flags |= WMI_DATA_HDR_FLAGS_UAPSD;
		spin_unlock_bh(&conn->psq_lock);
		return false;
128
	} else if (!conn->apsd_info) {
129
		return false;
130
	}
131 132 133 134 135 136 137 138 139 140 141 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

	if (test_bit(WMM_ENABLED, &vif->flags)) {
		ether_type = be16_to_cpu(datap->h_proto);
		if (is_ethertype(ether_type)) {
			/* packet is in DIX format  */
			ip_hdr = (u8 *)(datap + 1);
		} else {
			/* packet is in 802.3 format */
			llc_hdr = (struct ath6kl_llc_snap_hdr *)
							(datap + 1);
			ether_type = be16_to_cpu(llc_hdr->eth_type);
			ip_hdr = (u8 *)(llc_hdr + 1);
		}

		if (ether_type == IP_ETHERTYPE)
			up = ath6kl_wmi_determine_user_priority(
							ip_hdr, 0);
	}

	traffic_class = ath6kl_wmi_get_traffic_class(up);

	if ((conn->apsd_info & (1 << traffic_class)) == 0)
		return false;

	/* Queue the frames if the STA is sleeping */
	spin_lock_bh(&conn->psq_lock);
	is_apsdq_empty = skb_queue_empty(&conn->apsdq);
	skb_queue_tail(&conn->apsdq, skb);
	spin_unlock_bh(&conn->psq_lock);

	/*
	 * If this is the first pkt getting queued
	 * for this STA, update the PVB for this STA
	 */
	if (is_apsdq_empty) {
		ath6kl_wmi_set_apsd_bfrd_traf(ar->wmi,
167 168
					      vif->fw_vif_idx,
					      conn->aid, 1, 0);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	}
	*flags |= WMI_DATA_HDR_FLAGS_UAPSD;

	return true;
}

static bool ath6kl_process_psq(struct ath6kl_sta *conn,
				struct ath6kl_vif *vif,
				struct sk_buff *skb,
				u32 *flags)
{
	bool is_psq_empty = false;
	struct ath6kl *ar = vif->ar;

	if (conn->sta_flags & STA_PS_POLLED) {
		spin_lock_bh(&conn->psq_lock);
		if (!skb_queue_empty(&conn->psq))
			*flags |= WMI_DATA_HDR_FLAGS_MORE;
		spin_unlock_bh(&conn->psq_lock);
		return false;
	}

	/* Queue the frames if the STA is sleeping */
	spin_lock_bh(&conn->psq_lock);
	is_psq_empty = skb_queue_empty(&conn->psq);
	skb_queue_tail(&conn->psq, skb);
	spin_unlock_bh(&conn->psq_lock);

	/*
	 * If this is the first pkt getting queued
	 * for this STA, update the PVB for this
	 * STA.
	 */
	if (is_psq_empty)
		ath6kl_wmi_set_pvb_cmd(ar->wmi,
				       vif->fw_vif_idx,
				       conn->aid, 1);
	return true;
}

209
static bool ath6kl_powersave_ap(struct ath6kl_vif *vif, struct sk_buff *skb,
210
				u32 *flags)
Kalle Valo's avatar
Kalle Valo committed
211 212 213
{
	struct ethhdr *datap = (struct ethhdr *) skb->data;
	struct ath6kl_sta *conn = NULL;
214
	bool ps_queued = false;
215
	struct ath6kl *ar = vif->ar;
Kalle Valo's avatar
Kalle Valo committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

	if (is_multicast_ether_addr(datap->h_dest)) {
		u8 ctr = 0;
		bool q_mcast = false;

		for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
			if (ar->sta_list[ctr].sta_flags & STA_PS_SLEEP) {
				q_mcast = true;
				break;
			}
		}

		if (q_mcast) {
			/*
			 * If this transmit is not because of a Dtim Expiry
			 * q it.
			 */
233
			if (!test_bit(DTIM_EXPIRED, &vif->flags)) {
Kalle Valo's avatar
Kalle Valo committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
				bool is_mcastq_empty = false;

				spin_lock_bh(&ar->mcastpsq_lock);
				is_mcastq_empty =
					skb_queue_empty(&ar->mcastpsq);
				skb_queue_tail(&ar->mcastpsq, skb);
				spin_unlock_bh(&ar->mcastpsq_lock);

				/*
				 * If this is the first Mcast pkt getting
				 * queued indicate to the target to set the
				 * BitmapControl LSB of the TIM IE.
				 */
				if (is_mcastq_empty)
					ath6kl_wmi_set_pvb_cmd(ar->wmi,
249
							       vif->fw_vif_idx,
Kalle Valo's avatar
Kalle Valo committed
250 251 252 253 254 255 256 257 258 259
							       MCAST_AID, 1);

				ps_queued = true;
			} else {
				/*
				 * This transmit is because of Dtim expiry.
				 * Determine if MoreData bit has to be set.
				 */
				spin_lock_bh(&ar->mcastpsq_lock);
				if (!skb_queue_empty(&ar->mcastpsq))
260
					*flags |= WMI_DATA_HDR_FLAGS_MORE;
Kalle Valo's avatar
Kalle Valo committed
261 262 263 264
				spin_unlock_bh(&ar->mcastpsq_lock);
			}
		}
	} else {
265
		conn = ath6kl_find_sta(vif, datap->h_dest);
Kalle Valo's avatar
Kalle Valo committed
266 267 268 269 270 271 272 273
		if (!conn) {
			dev_kfree_skb(skb);

			/* Inform the caller that the skb is consumed */
			return true;
		}

		if (conn->sta_flags & STA_PS_SLEEP) {
274 275 276 277 278
			ps_queued = ath6kl_process_uapsdq(conn,
						vif, skb, flags);
			if (!(*flags & WMI_DATA_HDR_FLAGS_UAPSD))
				ps_queued = ath6kl_process_psq(conn,
						vif, skb, flags);
Kalle Valo's avatar
Kalle Valo committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292
		}
	}
	return ps_queued;
}

/* Tx functions */

int ath6kl_control_tx(void *devt, struct sk_buff *skb,
		      enum htc_endpoint_id eid)
{
	struct ath6kl *ar = devt;
	int status = 0;
	struct ath6kl_cookie *cookie = NULL;

293 294
	trace_ath6kl_wmi_cmd(skb->data, skb->len);

295
	if (WARN_ON_ONCE(ar->state == ATH6KL_STATE_WOW)) {
296
		dev_kfree_skb(skb);
297
		return -EACCES;
298
	}
299

300 301 302 303 304 305
	if (WARN_ON_ONCE(eid == ENDPOINT_UNUSED ||
			 eid >= ENDPOINT_MAX)) {
		status = -EINVAL;
		goto fail_ctrl_tx;
	}

Kalle Valo's avatar
Kalle Valo committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319
	spin_lock_bh(&ar->lock);

	ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
		   "%s: skb=0x%p, len=0x%x eid =%d\n", __func__,
		   skb, skb->len, eid);

	if (test_bit(WMI_CTRL_EP_FULL, &ar->flag) && (eid == ar->ctrl_ep)) {
		/*
		 * Control endpoint is full, don't allocate resources, we
		 * are just going to drop this packet.
		 */
		cookie = NULL;
		ath6kl_err("wmi ctrl ep full, dropping pkt : 0x%p, len:%d\n",
			   skb, skb->len);
320
	} else {
Kalle Valo's avatar
Kalle Valo committed
321
		cookie = ath6kl_alloc_cookie(ar);
322
	}
Kalle Valo's avatar
Kalle Valo committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

	if (cookie == NULL) {
		spin_unlock_bh(&ar->lock);
		status = -ENOMEM;
		goto fail_ctrl_tx;
	}

	ar->tx_pending[eid]++;

	if (eid != ar->ctrl_ep)
		ar->total_tx_data_pend++;

	spin_unlock_bh(&ar->lock);

	cookie->skb = skb;
	cookie->map_no = 0;
	set_htc_pkt_info(&cookie->htc_pkt, cookie, skb->data, skb->len,
			 eid, ATH6KL_CONTROL_PKT_TAG);
341
	cookie->htc_pkt.skb = skb;
Kalle Valo's avatar
Kalle Valo committed
342 343 344 345 346

	/*
	 * This interface is asynchronous, if there is an error, cleanup
	 * will happen in the TX completion callback.
	 */
347
	ath6kl_htc_tx(ar->htc_target, &cookie->htc_pkt);
Kalle Valo's avatar
Kalle Valo committed
348 349 350 351 352 353 354 355 356 357 358 359 360

	return 0;

fail_ctrl_tx:
	dev_kfree_skb(skb);
	return status;
}

int ath6kl_data_tx(struct sk_buff *skb, struct net_device *dev)
{
	struct ath6kl *ar = ath6kl_priv(dev);
	struct ath6kl_cookie *cookie = NULL;
	enum htc_endpoint_id eid = ENDPOINT_UNUSED;
361
	struct ath6kl_vif *vif = netdev_priv(dev);
Kalle Valo's avatar
Kalle Valo committed
362 363
	u32 map_no = 0;
	u16 htc_tag = ATH6KL_DATA_PKT_TAG;
364
	u8 ac = 99; /* initialize to unmapped ac */
365
	bool chk_adhoc_ps_mapping = false;
Kalle Valo's avatar
Kalle Valo committed
366
	int ret;
367 368 369 370
	struct wmi_tx_meta_v2 meta_v2;
	void *meta;
	u8 csum_start = 0, csum_dest = 0, csum = skb->ip_summed;
	u8 meta_ver = 0;
371
	u32 flags = 0;
Kalle Valo's avatar
Kalle Valo committed
372 373 374 375 376 377

	ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
		   "%s: skb=0x%p, data=0x%p, len=0x%x\n", __func__,
		   skb, skb->data, skb->len);

	/* If target is not associated */
378 379
	if (!test_bit(CONNECTED, &vif->flags))
		goto fail_tx;
Kalle Valo's avatar
Kalle Valo committed
380

381 382
	if (WARN_ON_ONCE(ar->state != ATH6KL_STATE_ON))
		goto fail_tx;
383

Kalle Valo's avatar
Kalle Valo committed
384 385 386 387
	if (!test_bit(WMI_READY, &ar->flag))
		goto fail_tx;

	/* AP mode Power saving processing */
388
	if (vif->nw_type == AP_NETWORK) {
389
		if (ath6kl_powersave_ap(vif, skb, &flags))
Kalle Valo's avatar
Kalle Valo committed
390 391 392 393
			return 0;
	}

	if (test_bit(WMI_ENABLED, &ar->flag)) {
394
		if ((dev->features & NETIF_F_IP_CSUM) &&
395
		    (csum == CHECKSUM_PARTIAL)) {
396 397 398 399 400 401
			csum_start = skb->csum_start -
					(skb_network_header(skb) - skb->head) +
					sizeof(struct ath6kl_llc_snap_hdr);
			csum_dest = skb->csum_offset + csum_start;
		}

402 403 404 405
		if (skb_cow_head(skb, dev->needed_headroom)) {
			dev->stats.tx_dropped++;
			kfree_skb(skb);
			return 0;
Kalle Valo's avatar
Kalle Valo committed
406 407 408 409 410 411 412
		}

		if (ath6kl_wmi_dix_2_dot3(ar->wmi, skb)) {
			ath6kl_err("ath6kl_wmi_dix_2_dot3 failed\n");
			goto fail_tx;
		}

413
		if ((dev->features & NETIF_F_IP_CSUM) &&
414
		    (csum == CHECKSUM_PARTIAL)) {
415 416 417 418 419 420 421 422 423 424 425 426 427
			meta_v2.csum_start = csum_start;
			meta_v2.csum_dest = csum_dest;

			/* instruct target to calculate checksum */
			meta_v2.csum_flags = WMI_META_V2_FLAG_CSUM_OFFLOAD;
			meta_ver = WMI_META_VERSION_2;
			meta = &meta_v2;
		} else {
			meta_ver = 0;
			meta = NULL;
		}

		ret = ath6kl_wmi_data_hdr_add(ar->wmi, skb,
428
				DATA_MSGTYPE, flags, 0,
429 430 431 432 433 434
				meta_ver,
				meta, vif->fw_vif_idx);

		if (ret) {
			ath6kl_warn("failed to add wmi data header:%d\n"
				, ret);
Kalle Valo's avatar
Kalle Valo committed
435 436 437
			goto fail_tx;
		}

438
		if ((vif->nw_type == ADHOC_NETWORK) &&
439
		    ar->ibss_ps_enable && test_bit(CONNECTED, &vif->flags))
Kalle Valo's avatar
Kalle Valo committed
440 441 442
			chk_adhoc_ps_mapping = true;
		else {
			/* get the stream mapping */
443 444
			ret = ath6kl_wmi_implicit_create_pstream(ar->wmi,
				    vif->fw_vif_idx, skb,
445
				    0, test_bit(WMM_ENABLED, &vif->flags), &ac);
Kalle Valo's avatar
Kalle Valo committed
446 447 448
			if (ret)
				goto fail_tx;
		}
449
	} else {
Kalle Valo's avatar
Kalle Valo committed
450
		goto fail_tx;
451
	}
Kalle Valo's avatar
Kalle Valo committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

	spin_lock_bh(&ar->lock);

	if (chk_adhoc_ps_mapping)
		eid = ath6kl_ibss_map_epid(skb, dev, &map_no);
	else
		eid = ar->ac2ep_map[ac];

	if (eid == 0 || eid == ENDPOINT_UNUSED) {
		ath6kl_err("eid %d is not mapped!\n", eid);
		spin_unlock_bh(&ar->lock);
		goto fail_tx;
	}

	/* allocate resource for this packet */
	cookie = ath6kl_alloc_cookie(ar);

	if (!cookie) {
		spin_unlock_bh(&ar->lock);
		goto fail_tx;
	}

	/* update counts while the lock is held */
	ar->tx_pending[eid]++;
	ar->total_tx_data_pend++;

	spin_unlock_bh(&ar->lock);

Jouni Malinen's avatar
Jouni Malinen committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	if (!IS_ALIGNED((unsigned long) skb->data - HTC_HDR_LENGTH, 4) &&
	    skb_cloned(skb)) {
		/*
		 * We will touch (move the buffer data to align it. Since the
		 * skb buffer is cloned and not only the header is changed, we
		 * have to copy it to allow the changes. Since we are copying
		 * the data here, we may as well align it by reserving suitable
		 * headroom to avoid the memmove in ath6kl_htc_tx_buf_align().
		 */
		struct sk_buff *nskb;

		nskb = skb_copy_expand(skb, HTC_HDR_LENGTH, 0, GFP_ATOMIC);
		if (nskb == NULL)
			goto fail_tx;
		kfree_skb(skb);
		skb = nskb;
	}

Kalle Valo's avatar
Kalle Valo committed
498 499 500 501
	cookie->skb = skb;
	cookie->map_no = map_no;
	set_htc_pkt_info(&cookie->htc_pkt, cookie, skb->data, skb->len,
			 eid, htc_tag);
502
	cookie->htc_pkt.skb = skb;
Kalle Valo's avatar
Kalle Valo committed
503

504 505
	ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, __func__, "tx ",
			skb->data, skb->len);
Kalle Valo's avatar
Kalle Valo committed
506 507 508 509 510

	/*
	 * HTC interface is asynchronous, if this fails, cleanup will
	 * happen in the ath6kl_tx_complete callback.
	 */
511
	ath6kl_htc_tx(ar->htc_target, &cookie->htc_pkt);
Kalle Valo's avatar
Kalle Valo committed
512 513 514 515 516 517

	return 0;

fail_tx:
	dev_kfree_skb(skb);

518 519
	dev->stats.tx_dropped++;
	dev->stats.tx_aborted_errors++;
Kalle Valo's avatar
Kalle Valo committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582

	return 0;
}

/* indicate tx activity or inactivity on a WMI stream */
void ath6kl_indicate_tx_activity(void *devt, u8 traffic_class, bool active)
{
	struct ath6kl *ar = devt;
	enum htc_endpoint_id eid;
	int i;

	eid = ar->ac2ep_map[traffic_class];

	if (!test_bit(WMI_ENABLED, &ar->flag))
		goto notify_htc;

	spin_lock_bh(&ar->lock);

	ar->ac_stream_active[traffic_class] = active;

	if (active) {
		/*
		 * Keep track of the active stream with the highest
		 * priority.
		 */
		if (ar->ac_stream_pri_map[traffic_class] >
		    ar->hiac_stream_active_pri)
			/* set the new highest active priority */
			ar->hiac_stream_active_pri =
					ar->ac_stream_pri_map[traffic_class];

	} else {
		/*
		 * We may have to search for the next active stream
		 * that is the highest priority.
		 */
		if (ar->hiac_stream_active_pri ==
			ar->ac_stream_pri_map[traffic_class]) {
			/*
			 * The highest priority stream just went inactive
			 * reset and search for the "next" highest "active"
			 * priority stream.
			 */
			ar->hiac_stream_active_pri = 0;

			for (i = 0; i < WMM_NUM_AC; i++) {
				if (ar->ac_stream_active[i] &&
				    (ar->ac_stream_pri_map[i] >
				     ar->hiac_stream_active_pri))
					/*
					 * Set the new highest active
					 * priority.
					 */
					ar->hiac_stream_active_pri =
						ar->ac_stream_pri_map[i];
			}
		}
	}

	spin_unlock_bh(&ar->lock);

notify_htc:
	/* notify HTC, this may cause credit distribution changes */
Kalle Valo's avatar
Kalle Valo committed
583
	ath6kl_htc_activity_changed(ar->htc_target, eid, active);
Kalle Valo's avatar
Kalle Valo committed
584 585 586 587 588 589
}

enum htc_send_full_action ath6kl_tx_queue_full(struct htc_target *target,
					       struct htc_packet *packet)
{
	struct ath6kl *ar = target->dev->ar;
590
	struct ath6kl_vif *vif;
Kalle Valo's avatar
Kalle Valo committed
591
	enum htc_endpoint_id endpoint = packet->endpoint;
592
	enum htc_send_full_action action = HTC_SEND_FULL_KEEP;
Kalle Valo's avatar
Kalle Valo committed
593 594 595 596 597 598 599 600 601 602

	if (endpoint == ar->ctrl_ep) {
		/*
		 * Under normal WMI if this is getting full, then something
		 * is running rampant the host should not be exhausting the
		 * WMI queue with too many commands the only exception to
		 * this is during testing using endpointping.
		 */
		set_bit(WMI_CTRL_EP_FULL, &ar->flag);
		ath6kl_err("wmi ctrl ep is full\n");
603
		ath6kl_recovery_err_notify(ar, ATH6KL_FW_EP_FULL);
604
		return action;
Kalle Valo's avatar
Kalle Valo committed
605 606 607
	}

	if (packet->info.tx.tag == ATH6KL_CONTROL_PKT_TAG)
608
		return action;
Kalle Valo's avatar
Kalle Valo committed
609 610 611 612 613 614 615

	/*
	 * The last MAX_HI_COOKIE_NUM "batch" of cookies are reserved for
	 * the highest active stream.
	 */
	if (ar->ac_stream_pri_map[ar->ep2ac_map[endpoint]] <
	    ar->hiac_stream_active_pri &&
616 617
	    ar->cookie_count <=
			target->endpoint[endpoint].tx_drop_packet_threshold)
Kalle Valo's avatar
Kalle Valo committed
618 619 620 621
		/*
		 * Give preference to the highest priority stream by
		 * dropping the packets which overflowed.
		 */
622 623 624
		action = HTC_SEND_FULL_DROP;

	/* FIXME: Locking */
625
	spin_lock_bh(&ar->list_lock);
626
	list_for_each_entry(vif, &ar->vif_list, list) {
627 628
		if (vif->nw_type == ADHOC_NETWORK ||
		    action != HTC_SEND_FULL_DROP) {
629
			spin_unlock_bh(&ar->list_lock);
Kalle Valo's avatar
Kalle Valo committed
630

631 632
			set_bit(NETQ_STOPPED, &vif->flags);
			netif_stop_queue(vif->ndev);
Kalle Valo's avatar
Kalle Valo committed
633

634 635 636
			return action;
		}
	}
637
	spin_unlock_bh(&ar->list_lock);
638 639

	return action;
Kalle Valo's avatar
Kalle Valo committed
640 641 642
}

/* TODO this needs to be looked at */
643
static void ath6kl_tx_clear_node_map(struct ath6kl_vif *vif,
Kalle Valo's avatar
Kalle Valo committed
644 645
				     enum htc_endpoint_id eid, u32 map_no)
{
646
	struct ath6kl *ar = vif->ar;
Kalle Valo's avatar
Kalle Valo committed
647 648
	u32 i;

649
	if (vif->nw_type != ADHOC_NETWORK)
Kalle Valo's avatar
Kalle Valo committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
		return;

	if (!ar->ibss_ps_enable)
		return;

	if (eid == ar->ctrl_ep)
		return;

	if (map_no == 0)
		return;

	map_no--;
	ar->node_map[map_no].tx_pend--;

	if (ar->node_map[map_no].tx_pend)
		return;

	if (map_no != (ar->node_num - 1))
		return;

	for (i = ar->node_num; i > 0; i--) {
		if (ar->node_map[i - 1].tx_pend)
			break;

		memset(&ar->node_map[i - 1], 0,
		       sizeof(struct ath6kl_node_mapping));
		ar->node_num--;
	}
}

680 681
void ath6kl_tx_complete(struct htc_target *target,
			struct list_head *packet_queue)
Kalle Valo's avatar
Kalle Valo committed
682
{
683
	struct ath6kl *ar = target->dev->ar;
Kalle Valo's avatar
Kalle Valo committed
684 685 686 687 688 689 690 691
	struct sk_buff_head skb_queue;
	struct htc_packet *packet;
	struct sk_buff *skb;
	struct ath6kl_cookie *ath6kl_cookie;
	u32 map_no = 0;
	int status;
	enum htc_endpoint_id eid;
	bool wake_event = false;
692
	bool flushing[ATH6KL_VIF_MAX] = {false};
693
	u8 if_idx;
694
	struct ath6kl_vif *vif;
Kalle Valo's avatar
Kalle Valo committed
695 696 697 698 699 700 701 702 703 704 705 706

	skb_queue_head_init(&skb_queue);

	/* lock the driver as we update internal state */
	spin_lock_bh(&ar->lock);

	/* reap completed packets */
	while (!list_empty(packet_queue)) {
		packet = list_first_entry(packet_queue, struct htc_packet,
					  list);
		list_del(&packet->list);

707 708 709 710
		if (WARN_ON_ONCE(packet->endpoint == ENDPOINT_UNUSED ||
				 packet->endpoint >= ENDPOINT_MAX))
			continue;

Kalle Valo's avatar
Kalle Valo committed
711
		ath6kl_cookie = (struct ath6kl_cookie *)packet->pkt_cntxt;
712 713
		if (WARN_ON_ONCE(!ath6kl_cookie))
			continue;
Kalle Valo's avatar
Kalle Valo committed
714 715 716 717 718 719

		status = packet->status;
		skb = ath6kl_cookie->skb;
		eid = packet->endpoint;
		map_no = ath6kl_cookie->map_no;

720 721 722 723 724
		if (WARN_ON_ONCE(!skb || !skb->data)) {
			dev_kfree_skb(skb);
			ath6kl_free_cookie(ar, ath6kl_cookie);
			continue;
		}
Kalle Valo's avatar
Kalle Valo committed
725 726 727

		__skb_queue_tail(&skb_queue, skb);

728 729 730 731
		if (WARN_ON_ONCE(!status && (packet->act_len != skb->len))) {
			ath6kl_free_cookie(ar, ath6kl_cookie);
			continue;
		}
Kalle Valo's avatar
Kalle Valo committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745

		ar->tx_pending[eid]--;

		if (eid != ar->ctrl_ep)
			ar->total_tx_data_pend--;

		if (eid == ar->ctrl_ep) {
			if (test_bit(WMI_CTRL_EP_FULL, &ar->flag))
				clear_bit(WMI_CTRL_EP_FULL, &ar->flag);

			if (ar->tx_pending[eid] == 0)
				wake_event = true;
		}

746 747
		if (eid == ar->ctrl_ep) {
			if_idx = wmi_cmd_hdr_get_if_idx(
748
				(struct wmi_cmd_hdr *) packet->buf);
749 750
		} else {
			if_idx = wmi_data_hdr_get_if_idx(
751
				(struct wmi_data_hdr *) packet->buf);
752 753 754 755 756 757 758 759
		}

		vif = ath6kl_get_vif_by_index(ar, if_idx);
		if (!vif) {
			ath6kl_free_cookie(ar, ath6kl_cookie);
			continue;
		}

Kalle Valo's avatar
Kalle Valo committed
760 761 762
		if (status) {
			if (status == -ECANCELED)
				/* a packet was flushed  */
763
				flushing[if_idx] = true;
Kalle Valo's avatar
Kalle Valo committed
764

765
			vif->ndev->stats.tx_errors++;
Kalle Valo's avatar
Kalle Valo committed
766

767 768 769
			if (status != -ENOSPC && status != -ECANCELED)
				ath6kl_warn("tx complete error: %d\n", status);

Kalle Valo's avatar
Kalle Valo committed
770 771 772 773 774 775 776 777 778 779
			ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
				   "%s: skb=0x%p data=0x%p len=0x%x eid=%d %s\n",
				   __func__, skb, packet->buf, packet->act_len,
				   eid, "error!");
		} else {
			ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
				   "%s: skb=0x%p data=0x%p len=0x%x eid=%d %s\n",
				   __func__, skb, packet->buf, packet->act_len,
				   eid, "OK");

780
			flushing[if_idx] = false;
781 782
			vif->ndev->stats.tx_packets++;
			vif->ndev->stats.tx_bytes += skb->len;
Kalle Valo's avatar
Kalle Valo committed
783 784
		}

785
		ath6kl_tx_clear_node_map(vif, eid, map_no);
Kalle Valo's avatar
Kalle Valo committed
786 787 788

		ath6kl_free_cookie(ar, ath6kl_cookie);

789 790
		if (test_bit(NETQ_STOPPED, &vif->flags))
			clear_bit(NETQ_STOPPED, &vif->flags);
Kalle Valo's avatar
Kalle Valo committed
791 792 793 794 795 796
	}

	spin_unlock_bh(&ar->lock);

	__skb_queue_purge(&skb_queue);

797
	/* FIXME: Locking */
798
	spin_lock_bh(&ar->list_lock);
799 800 801
	list_for_each_entry(vif, &ar->vif_list, list) {
		if (test_bit(CONNECTED, &vif->flags) &&
		    !flushing[vif->fw_vif_idx]) {
802
			spin_unlock_bh(&ar->list_lock);
803
			netif_wake_queue(vif->ndev);
804
			spin_lock_bh(&ar->list_lock);
805
		}
Kalle Valo's avatar
Kalle Valo committed
806
	}
807
	spin_unlock_bh(&ar->list_lock);
Kalle Valo's avatar
Kalle Valo committed
808 809 810 811 812 813 814 815 816 817 818 819 820

	if (wake_event)
		wake_up(&ar->event_wq);

	return;
}

void ath6kl_tx_data_cleanup(struct ath6kl *ar)
{
	int i;

	/* flush all the data (non-control) streams */
	for (i = 0; i < WMM_NUM_AC; i++)
821 822
		ath6kl_htc_flush_txep(ar->htc_target, ar->ac2ep_map[i],
				      ATH6KL_DATA_PKT_TAG);
Kalle Valo's avatar
Kalle Valo committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
}

/* Rx functions */

static void ath6kl_deliver_frames_to_nw_stack(struct net_device *dev,
					      struct sk_buff *skb)
{
	if (!skb)
		return;

	skb->dev = dev;

	if (!(skb->dev->flags & IFF_UP)) {
		dev_kfree_skb(skb);
		return;
	}

	skb->protocol = eth_type_trans(skb, skb->dev);

	netif_rx_ni(skb);
}

static void ath6kl_alloc_netbufs(struct sk_buff_head *q, u16 num)
{
	struct sk_buff *skb;

	while (num) {
		skb = ath6kl_buf_alloc(ATH6KL_BUFFER_SIZE);
		if (!skb) {
			ath6kl_err("netbuf allocation failed\n");
			return;
		}
		skb_queue_tail(q, skb);
		num--;
	}
}

static struct sk_buff *aggr_get_free_skb(struct aggr_info *p_aggr)
{
	struct sk_buff *skb = NULL;

864 865 866 867
	if (skb_queue_len(&p_aggr->rx_amsdu_freeq) <
	    (AGGR_NUM_OF_FREE_NETBUFS >> 2))
		ath6kl_alloc_netbufs(&p_aggr->rx_amsdu_freeq,
				     AGGR_NUM_OF_FREE_NETBUFS);
Kalle Valo's avatar
Kalle Valo committed
868

869
	skb = skb_dequeue(&p_aggr->rx_amsdu_freeq);
Kalle Valo's avatar
Kalle Valo committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883

	return skb;
}

void ath6kl_rx_refill(struct htc_target *target, enum htc_endpoint_id endpoint)
{
	struct ath6kl *ar = target->dev->ar;
	struct sk_buff *skb;
	int rx_buf;
	int n_buf_refill;
	struct htc_packet *packet;
	struct list_head queue;

	n_buf_refill = ATH6KL_MAX_RX_BUFFERS -
884
			  ath6kl_htc_get_rxbuf_num(ar->htc_target, endpoint);
Kalle Valo's avatar
Kalle Valo committed
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

	if (n_buf_refill <= 0)
		return;

	INIT_LIST_HEAD(&queue);

	ath6kl_dbg(ATH6KL_DBG_WLAN_RX,
		   "%s: providing htc with %d buffers at eid=%d\n",
		   __func__, n_buf_refill, endpoint);

	for (rx_buf = 0; rx_buf < n_buf_refill; rx_buf++) {
		skb = ath6kl_buf_alloc(ATH6KL_BUFFER_SIZE);
		if (!skb)
			break;

		packet = (struct htc_packet *) skb->head;
901 902
		if (!IS_ALIGNED((unsigned long) skb->data, 4)) {
			size_t len = skb_headlen(skb);
903
			skb->data = PTR_ALIGN(skb->data - 4, 4);
904 905
			skb_set_tail_pointer(skb, len);
		}
Kalle Valo's avatar
Kalle Valo committed
906
		set_htc_rxpkt_info(packet, skb, skb->data,
907
				   ATH6KL_BUFFER_SIZE, endpoint);
908
		packet->skb = skb;
Kalle Valo's avatar
Kalle Valo committed
909 910 911 912
		list_add_tail(&packet->list, &queue);
	}

	if (!list_empty(&queue))
913
		ath6kl_htc_add_rxbuf_multiple(ar->htc_target, &queue);
Kalle Valo's avatar
Kalle Valo committed
914 915 916 917 918 919 920 921 922 923 924 925 926
}

void ath6kl_refill_amsdu_rxbufs(struct ath6kl *ar, int count)
{
	struct htc_packet *packet;
	struct sk_buff *skb;

	while (count) {
		skb = ath6kl_buf_alloc(ATH6KL_AMSDU_BUFFER_SIZE);
		if (!skb)
			return;

		packet = (struct htc_packet *) skb->head;
927 928
		if (!IS_ALIGNED((unsigned long) skb->data, 4)) {
			size_t len = skb_headlen(skb);
929
			skb->data = PTR_ALIGN(skb->data - 4, 4);
930 931
			skb_set_tail_pointer(skb, len);
		}
Kalle Valo's avatar
Kalle Valo committed
932 933
		set_htc_rxpkt_info(packet, skb, skb->data,
				   ATH6KL_AMSDU_BUFFER_SIZE, 0);
934 935
		packet->skb = skb;

Kalle Valo's avatar
Kalle Valo committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
		spin_lock_bh(&ar->lock);
		list_add_tail(&packet->list, &ar->amsdu_rx_buffer_queue);
		spin_unlock_bh(&ar->lock);
		count--;
	}
}

/*
 * Callback to allocate a receive buffer for a pending packet. We use a
 * pre-allocated list of buffers of maximum AMSDU size (4K).
 */
struct htc_packet *ath6kl_alloc_amsdu_rxbuf(struct htc_target *target,
					    enum htc_endpoint_id endpoint,
					    int len)
{
	struct ath6kl *ar = target->dev->ar;
	struct htc_packet *packet = NULL;
	struct list_head *pkt_pos;
	int refill_cnt = 0, depth = 0;

	ath6kl_dbg(ATH6KL_DBG_WLAN_RX, "%s: eid=%d, len:%d\n",
		   __func__, endpoint, len);

	if ((len <= ATH6KL_BUFFER_SIZE) ||
	    (len > ATH6KL_AMSDU_BUFFER_SIZE))
		return NULL;

	spin_lock_bh(&ar->lock);

	if (list_empty(&ar->amsdu_rx_buffer_queue)) {
		spin_unlock_bh(&ar->lock);
		refill_cnt = ATH6KL_MAX_AMSDU_RX_BUFFERS;
		goto refill_buf;
	}

	packet = list_first_entry(&ar->amsdu_rx_buffer_queue,
				  struct htc_packet, list);
	list_del(&packet->list);
	list_for_each(pkt_pos, &ar->amsdu_rx_buffer_queue)
		depth++;

	refill_cnt = ATH6KL_MAX_AMSDU_RX_BUFFERS - depth;
	spin_unlock_bh(&ar->lock);

	/* set actual endpoint ID */
	packet->endpoint = endpoint;

refill_buf:
	if (refill_cnt >= ATH6KL_AMSDU_REFILL_THRESHOLD)
		ath6kl_refill_amsdu_rxbufs(ar, refill_cnt);

	return packet;
}

static void aggr_slice_amsdu(struct aggr_info *p_aggr,
			     struct rxtid *rxtid, struct sk_buff *skb)
{
	struct sk_buff *new_skb;
	struct ethhdr *hdr;
	u16 frame_8023_len, payload_8023_len, mac_hdr_len, amsdu_len;
	u8 *framep;

	mac_hdr_len = sizeof(struct ethhdr);
	framep = skb->data + mac_hdr_len;
	amsdu_len = skb->len - mac_hdr_len;

	while (amsdu_len > mac_hdr_len) {
		hdr = (struct ethhdr *) framep;
		payload_8023_len = ntohs(hdr->h_proto);

		if (payload_8023_len < MIN_MSDU_SUBFRAME_PAYLOAD_LEN ||
		    payload_8023_len > MAX_MSDU_SUBFRAME_PAYLOAD_LEN) {
			ath6kl_err("802.3 AMSDU frame bound check failed. len %d\n",
				   payload_8023_len);
			break;
		}

		frame_8023_len = payload_8023_len + mac_hdr_len;
		new_skb = aggr_get_free_skb(p_aggr);
		if (!new_skb) {
			ath6kl_err("no buffer available\n");
			break;
		}

		memcpy(new_skb->data, framep, frame_8023_len);
		skb_put(new_skb, frame_8023_len);
		if (ath6kl_wmi_dot3_2_dix(new_skb)) {
			ath6kl_err("dot3_2_dix error\n");
			dev_kfree_skb(new_skb);
			break;
		}

		skb_queue_tail(&rxtid->q, new_skb);

		/* Is this the last subframe within this aggregate ? */
		if ((amsdu_len - frame_8023_len) == 0)
			break;

		/* Add the length of A-MSDU subframe padding bytes -
		 * Round to nearest word.
		 */
1037
		frame_8023_len = ALIGN(frame_8023_len, 4);
Kalle Valo's avatar
Kalle Valo committed
1038 1039 1040 1041 1042 1043 1044 1045

		framep += frame_8023_len;
		amsdu_len -= frame_8023_len;
	}

	dev_kfree_skb(skb);
}

1046
static void aggr_deque_frms(struct aggr_info_conn *agg_conn, u8 tid,
Kalle Valo's avatar
Kalle Valo committed
1047 1048 1049 1050 1051 1052 1053 1054
			    u16 seq_no, u8 order)
{
	struct sk_buff *skb;
	struct rxtid *rxtid;
	struct skb_hold_q *node;
	u16 idx, idx_end, seq_end;
	struct rxtid_stats *stats;

1055 1056
	rxtid = &agg_conn->rx_tid[tid];
	stats = &agg_conn->stat[tid];
Kalle Valo's avatar
Kalle Valo committed
1057

1058
	spin_lock_bh(&rxtid->lock);
Kalle Valo's avatar
Kalle Valo committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	idx = AGGR_WIN_IDX(rxtid->seq_next, rxtid->hold_q_sz);

	/*
	 * idx_end is typically the last possible frame in the window,
	 * but changes to 'the' seq_no, when BAR comes. If seq_no
	 * is non-zero, we will go up to that and stop.
	 * Note: last seq no in current window will occupy the same
	 * index position as index that is just previous to start.
	 * An imp point : if win_sz is 7, for seq_no space of 4095,
	 * then, there would be holes when sequence wrap around occurs.
	 * Target should judiciously choose the win_sz, based on
	 * this condition. For 4095, (TID_WINDOW_SZ = 2 x win_sz
	 * 2, 4, 8, 16 win_sz works fine).
	 * We must deque from "idx" to "idx_end", including both.
	 */
	seq_end = seq_no ? seq_no : rxtid->seq_next;
	idx_end = AGGR_WIN_IDX(seq_end, rxtid->hold_q_sz);

	do {
		node = &rxtid->hold_q[idx];
		if ((order == 1) && (!node->skb))
			break;

		if (node->skb) {
			if (node->is_amsdu)
1084 1085
				aggr_slice_amsdu(agg_conn->aggr_info, rxtid,
						 node->skb);
Kalle Valo's avatar
Kalle Valo committed
1086 1087 1088
			else
				skb_queue_tail(&rxtid->q, node->skb);
			node->skb = NULL;
1089
		} else {
Kalle Valo's avatar
Kalle Valo committed
1090
			stats->num_hole++;
1091
		}
Kalle Valo's avatar
Kalle Valo committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

		rxtid->seq_next = ATH6KL_NEXT_SEQ_NO(rxtid->seq_next);
		idx = AGGR_WIN_IDX(rxtid->seq_next, rxtid->hold_q_sz);
	} while (idx != idx_end);

	spin_unlock_bh(&rxtid->lock);

	stats->num_delivered += skb_queue_len(&rxtid->q);

	while ((skb = skb_dequeue(&rxtid->q)))
1102
		ath6kl_deliver_frames_to_nw_stack(agg_conn->dev, skb);
Kalle Valo's avatar
Kalle Valo committed
1103 1104
}

1105
static bool aggr_process_recv_frm(struct aggr_info_conn *agg_conn, u8 tid,
Kalle Valo's avatar
Kalle Valo committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
				  u16 seq_no,
				  bool is_amsdu, struct sk_buff *frame)
{
	struct rxtid *rxtid;
	struct rxtid_stats *stats;
	struct sk_buff *skb;
	struct skb_hold_q *node;
	u16 idx, st, cur, end;
	bool is_queued = false;
	u16 extended_end;

1117 1118
	rxtid = &agg_conn->rx_tid[tid];
	stats = &agg_conn->stat[tid];
Kalle Valo's avatar
Kalle Valo committed
1119 1120 1121 1122 1123

	stats->num_into_aggr++;

	if (!rxtid->aggr) {
		if (is_amsdu) {
1124
			aggr_slice_amsdu(agg_conn->aggr_info, rxtid, frame);
Kalle Valo's avatar
Kalle Valo committed
1125 1126 1127
			is_queued = true;
			stats->num_amsdu++;
			while ((skb = skb_dequeue(&rxtid->q)))
1128
				ath6kl_deliver_frames_to_nw_stack(agg_conn->dev,
Kalle Valo's avatar
Kalle Valo committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
								  skb);
		}
		return is_queued;
	}

	/* Check the incoming sequence no, if it's in the window */
	st = rxtid->seq_next;
	cur = seq_no;
	end = (st + rxtid->hold_q_sz-1) & ATH6KL_MAX_SEQ_NO;

	if (((st < end) && (cur < st || cur > end)) ||
	    ((st > end) && (cur > end) && (cur < st))) {
		extended_end = (end + rxtid->hold_q_sz - 1) &
			ATH6KL_MAX_SEQ_NO;

		if (((end < extended_end) &&
		     (cur < end || cur > extended_end)) ||
		    ((end > extended_end) && (cur > extended_end) &&
		     (cur < end))) {
1148
			aggr_deque_frms(agg_conn, tid, 0, 0);
1149
			spin_lock_bh(&rxtid->lock);
Kalle Valo's avatar
Kalle Valo committed
1150 1151 1152 1153 1154
			if (cur >= rxtid->hold_q_sz - 1)
				rxtid->seq_next = cur - (rxtid->hold_q_sz - 1);
			else
				rxtid->seq_next = ATH6KL_MAX_SEQ_NO -
						  (rxtid->hold_q_sz - 2 - cur);
1155
			spin_unlock_bh(&rxtid->lock);
Kalle Valo's avatar
Kalle Valo committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
		} else {
			/*
			 * Dequeue only those frames that are outside the
			 * new shifted window.
			 */
			if (cur >= rxtid->hold_q_sz - 1)
				st = cur - (rxtid->hold_q_sz - 1);
			else
				st = ATH6KL_MAX_SEQ_NO -
					(rxtid->hold_q_sz - 2 - cur);

1167
			aggr_deque_frms(agg_conn, tid, st, 0);
Kalle Valo's avatar
Kalle Valo committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
		}

		stats->num_oow++;
	}

	idx = AGGR_WIN_IDX(seq_no, rxtid->hold_q_sz);

	node = &rxtid->hold_q[idx];

	spin_lock_bh(&rxtid->lock);

	/*
	 * Is the cur frame duplicate or something beyond our window(hold_q
	 * -> which is 2x, already)?
	 *
	 * 1. Duplicate is easy - drop incoming frame.
	 * 2. Not falling in current sliding window.
	 *  2a. is the frame_seq_no preceding current tid_seq_no?
	 *      -> drop the frame. perhaps sender did not get our ACK.
	 *         this is taken care of above.
	 *  2b. is the frame_seq_no beyond window(st, TID_WINDOW_SZ);
	 *      -> Taken care of it above, by moving window forward.
	 */
	dev_kfree_skb(node->skb);
	stats->num_dups++;

	node->skb = frame;
	is_queued = true;
	node->is_amsdu = is_amsdu;
	node->seq_no = seq_no;

	if (node->is_amsdu)
		stats->num_amsdu++;
	else
		stats->num_mpdu++;

	spin_unlock_bh(&rxtid->lock);

1206
	aggr_deque_frms(agg_conn, tid, 0, 1);
Kalle Valo's avatar
Kalle Valo committed
1207

1208
	if (agg_conn->timer_scheduled)
1209 1210 1211
		return is_queued;

	spin_lock_bh(&rxtid->lock);
1212
	for (idx = 0; idx < rxtid->hold_q_sz; idx++) {
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
		if (rxtid->hold_q[idx].skb) {
			/*
			 * There is a frame in the queue and no
			 * timer so start a timer to ensure that
			 * the frame doesn't remain stuck
			 * forever.
			 */
			agg_conn->timer_scheduled = true;
			mod_timer(&agg_conn->timer,
				  (jiffies + (HZ * AGGR_RX_TIMEOUT) / 1000));
			rxtid->timer_mon = true;
			break;
Kalle Valo's avatar
Kalle Valo committed
1225
		}
1226
	}
1227
	spin_unlock_bh(&rxtid->lock);
Kalle Valo's avatar
Kalle Valo committed
1228 1229 1230 1231

	return is_queued;
}

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
static void ath6kl_uapsd_trigger_frame_rx(struct ath6kl_vif *vif,
						 struct ath6kl_sta *conn)
{
	struct ath6kl *ar = vif->ar;
	bool is_apsdq_empty, is_apsdq_empty_at_start;
	u32 num_frames_to_deliver, flags;
	struct sk_buff *skb = NULL;

	/*
	 * If the APSD q for this STA is not empty, dequeue and
	 * send a pkt from the head of the q. Also update the
	 * More data bit in the WMI_DATA_HDR if there are
	 * more pkts for this STA in the APSD q.
	 * If there are no more pkts for this STA,
	 * update the APSD bitmap for this STA.
	 */

	num_frames_to_deliver = (conn->apsd_info >> ATH6KL_APSD_NUM_OF_AC) &
						    ATH6KL_APSD_FRAME_MASK;
	/*
	 * Number of frames to send in a service period is
	 * indicated by the station
	 * in the QOS_INFO of the association request
	 * If it is zero, send all frames
	 */
	if (!num_frames_to_deliver)
		num_frames_to_deliver = ATH6KL_APSD_ALL_FRAME;

	spin_lock_bh(&conn->psq_lock);
	is_apsdq_empty = skb_queue_empty(&conn->apsdq);
	spin_unlock_bh(&conn->psq_lock);
	is_apsdq_empty_at_start = is_apsdq_empty;

	while ((!is_apsdq_empty) && (num_frames_to_deliver)) {
		spin_lock_bh(&conn->psq_lock);
		skb = skb_dequeue(&conn->apsdq);
		is_apsdq_empty = skb_queue_empty(&conn->apsdq);
		spin_unlock_bh(&conn->psq_lock);

		/*
		 * Set the STA flag to Trigger delivery,
		 * so that the frame will go out
		 */
		conn->sta_flags |= STA_PS_APSD_TRIGGER;
		num_frames_to_deliver--;

		/* Last frame in the service period, set EOSP or queue empty */
		if ((is_apsdq_empty) || (!num_frames_to_deliver))
			conn->sta_flags |= STA_PS_APSD_EOSP;

		ath6kl_data_tx(skb, vif->ndev);
		conn->sta_flags &= ~(STA_PS_APSD_TRIGGER);
		conn->sta_flags &= ~(STA_PS_APSD_EOSP);
	}

	if (is_apsdq_empty) {
		if (is_apsdq_empty_at_start)
			flags = WMI_AP_APSD_NO_DELIVERY_FRAMES;
		else
			flags = 0;

		ath6kl_wmi_set_apsd_bfrd_traf(ar->wmi,
1294 1295
					      vif->fw_vif_idx,
					      conn->aid, 0, flags);
1296 1297 1298 1299 1300
	}

	return;
}

Kalle Valo's avatar
Kalle Valo committed
1301 1302 1303 1304 1305 1306 1307 1308
void ath6kl_rx(struct htc_target *target, struct htc_packet *packet)
{
	struct ath6kl *ar = target->dev->ar;
	struct sk_buff *skb = packet->pkt_cntxt;
	struct wmi_rx_meta_v2 *meta;
	struct wmi_data_hdr *dhdr;
	int min_hdr_len;
	u8 meta_type, dot11_hdr = 0;
1309
	u8 pad_before_data_start;
Kalle Valo's avatar
Kalle Valo committed
1310 1311 1312
	int status = packet->status;
	enum htc_endpoint_id ept = packet->endpoint;
	bool is_amsdu, prev_ps, ps_state = false;
1313
	bool trig_state = false;
Kalle Valo's avatar
Kalle Valo committed
1314 1315 1316
	struct ath6kl_sta *conn = NULL;
	struct sk_buff *skb1 = NULL;
	struct ethhdr *datap = NULL;
1317
	struct ath6kl_vif *vif;
1318
	struct aggr_info_conn *aggr_conn;
Kalle Valo's avatar
Kalle Valo committed
1319
	u16 seq_no, offset;
1320
	u8 tid, if_idx;
Kalle Valo's avatar
Kalle Valo committed
1321 1322 1323 1324 1325 1326

	ath6kl_dbg(ATH6KL_DBG_WLAN_RX,
		   "%s: ar=0x%p eid=%d, skb=0x%p, data=0x%p, len=0x%x status:%d",
		   __func__, ar, ept, skb, packet->buf,
		   packet->act_len, status);

1327
	if (status || packet->act_len < HTC_HDR_LENGTH) {
1328 1329 1330 1331 1332 1333 1334
		dev_kfree_skb(skb);
		return;
	}

	skb_put(skb, packet->act_len + HTC_HDR_LENGTH);
	skb_pull(skb, HTC_HDR_LENGTH);

1335 1336 1337
	ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, __func__, "rx ",
			skb->data, skb->len);

1338
	if (ept == ar->ctrl_ep) {
1339 1340 1341 1342 1343
		if (test_bit(WMI_ENABLED, &ar->flag)) {
			ath6kl_check_wow_status(ar);
			ath6kl_wmi_control_rx(ar->wmi, skb);
			return;
		}
1344 1345 1346 1347 1348 1349 1350 1351 1352
		if_idx =
		wmi_cmd_hdr_get_if_idx((struct wmi_cmd_hdr *) skb->data);
	} else {
		if_idx =
		wmi_data_hdr_get_if_idx((struct wmi_data_hdr *) skb->data);
	}

	vif = ath6kl_get_vif_by_index(ar, if_idx);
	if (!vif) {
Kalle Valo's avatar
Kalle Valo committed
1353 1354 1355 1356 1357 1358 1359 1360
		dev_kfree_skb(skb);
		return;
	}

	/*
	 * Take lock to protect buffer counts and adaptive power throughput
	 * state.
	 */
1361
	spin_lock_bh(&vif->if_lock);
Kalle Valo's avatar
Kalle Valo committed
1362

1363 1364
	vif->ndev->stats.rx_packets++;
	vif->ndev->stats.rx_bytes += packet->act_len;
Kalle Valo's avatar
Kalle Valo committed
1365

1366
	spin_unlock_bh(&vif->if_lock);
1367

1368
	skb->dev = vif->ndev;
Kalle Valo's avatar
Kalle Valo committed
1369 1370 1371 1372

	if (!test_bit(WMI_ENABLED, &ar->flag)) {
		if (EPPING_ALIGNMENT_PAD > 0)
			skb_pull(skb, EPPING_ALIGNMENT_PAD);
1373
		ath6kl_deliver_frames_to_nw_stack(vif->ndev, skb);
Kalle Valo's avatar
Kalle Valo committed
1374 1375 1376
		return;
	}

1377 1378
	ath6kl_check_wow_status(ar);

1379 1380
	min_hdr_len = sizeof(struct ethhdr) + sizeof(struct wmi_data_hdr) +
		      sizeof(struct ath6kl_llc_snap_hdr);
Kalle Valo's avatar
Kalle Valo committed
1381 1382 1383 1384 1385 1386 1387 1388

	dhdr = (struct wmi_data_hdr *) skb->data;

	/*
	 * In the case of AP mode we may receive NULL data frames
	 * that do not have LLC hdr. They are 16 bytes in size.
	 * Allow these frames in the AP mode.
	 */
1389
	if (vif->nw_type != AP_NETWORK &&
Kalle Valo's avatar
Kalle Valo committed
1390 1391 1392
	    ((packet->act_len < min_hdr_len) ||
	     (packet->act_len > WMI_MAX_AMSDU_RX_DATA_FRAME_LENGTH))) {
		ath6kl_info("frame len is too short or too long\n");
1393 1394
		vif->ndev->stats.rx_errors++;
		vif->ndev->stats.rx_length_errors++;
Kalle Valo's avatar
Kalle Valo committed
1395 1396 1397 1398
		dev_kfree_skb(skb);
		return;
	}

1399 1400 1401 1402
	pad_before_data_start =
		(le16_to_cpu(dhdr->info3) >> WMI_DATA_HDR_PAD_BEFORE_DATA_SHIFT)
			& WMI_DATA_HDR_PAD_BEFORE_DATA_MASK;

Kalle Valo's avatar
Kalle Valo committed
1403
	/* Get the Power save state of the STA */
1404
	if (vif->nw_type == AP_NETWORK) {
Kalle Valo's avatar
Kalle Valo committed
1405 1406 1407 1408 1409
		meta_type = wmi_data_hdr_get_meta(dhdr);

		ps_state = !!((dhdr->info >> WMI_DATA_HDR_PS_SHIFT) &
			      WMI_DATA_HDR_PS_MASK);

1410
		offset = sizeof(struct wmi_data_hdr) + pad_before_data_start;
1411
		trig_state = !!(le16_to_cpu(dhdr->info3) & WMI_DATA_HDR_TRIG);
Kalle Valo's avatar
Kalle Valo committed
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426

		switch (meta_type) {
		case 0:
			break;
		case WMI_META_VERSION_1:
			offset += sizeof(struct wmi_rx_meta_v1);
			break;
		case WMI_META_VERSION_2:
			offset += sizeof(struct wmi_rx_meta_v2);
			break;
		default:
			break;
		}

		datap = (struct ethhdr *) (skb->data + offset);
1427
		conn = ath6kl_find_sta(vif, datap->h_source);
Kalle Valo's avatar
Kalle Valo committed
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

		if (!conn) {
			dev_kfree_skb(skb);
			return;
		}

		/*
		 * If there is a change in PS state of the STA,
		 * take appropriate steps:
		 *
		 * 1. If Sleep-->Awake, flush the psq for the STA
		 *    Clear the PVB for the STA.
		 * 2. If Awake-->Sleep, Starting queueing frames
		 *    the STA.
		 */
		prev_ps = !!(conn->sta_flags & STA_PS_SLEEP);

		if (ps_state)
			conn->sta_flags |= STA_PS_SLEEP;
		else
			conn->sta_flags &= ~STA_PS_SLEEP;

1450 1451 1452 1453
		/* Accept trigger only when the station is in sleep */
		if ((conn->sta_flags & STA_PS_SLEEP) && trig_state)
			ath6kl_uapsd_trigger_frame_rx(vif, conn);

Kalle Valo's avatar
Kalle Valo committed
1454 1455 1456
		if (prev_ps ^ !!(conn->sta_flags & STA_PS_SLEEP)) {
			if (!(conn->sta_flags & STA_PS_SLEEP)) {
				struct sk_buff *skbuff = NULL;
1457
				bool is_apsdq_empty;
1458 1459
				struct ath6kl_mgmt_buff *mgmt;
				u8 idx;
Kalle Valo's avatar
Kalle Valo committed
1460 1461

				spin_lock_bh(&conn->psq_lock);
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
				while (conn->mgmt_psq_len > 0) {
					mgmt = list_first_entry(
							&conn->mgmt_psq,
							struct ath6kl_mgmt_buff,
							list);
					list_del(&mgmt->list);
					conn->mgmt_psq_len--;
					spin_unlock_bh(&conn->psq_lock);
					idx = vif->fw_vif_idx;

					ath6kl_wmi_send_mgmt_cmd(ar->wmi,
								 idx,
								 mgmt->id,
								 mgmt->freq,
								 mgmt->wait,
								 mgmt->buf,
								 mgmt->len,
								 mgmt->no_cck);

					kfree(mgmt);
					spin_lock_bh(&conn->psq_lock);
				}
				conn->mgmt_psq_len = 0;
1485 1486 1487 1488 1489 1490 1491 1492
				while ((skbuff = skb_dequeue(&conn->psq))) {
					spin_unlock_bh(&conn->psq_lock);
					ath6kl_data_tx(skbuff, vif->ndev);
					spin_lock_bh(&conn->psq_lock);
				}

				is_apsdq_empty = skb_queue_empty(&conn->apsdq);
				while ((skbuff = skb_dequeue(&conn->apsdq))) {
Kalle Valo's avatar
Kalle Valo committed
1493
					spin_unlock_bh(&conn->psq_lock);
1494
					ath6kl_data_tx(skbuff, vif->ndev);
Kalle Valo's avatar
Kalle Valo committed
1495 1496 1497
					spin_lock_bh(&conn->psq_lock);
				}
				spin_unlock_bh(&conn->psq_lock);
1498 1499 1500 1501 1502 1503 1504

				if (!is_apsdq_empty)
					ath6kl_wmi_set_apsd_bfrd_traf(
							ar->wmi,
							vif->fw_vif_idx,
							conn->aid, 0, 0);

Kalle Valo's avatar
Kalle Valo committed
1505
				/* Clear the PVB for this STA */
1506 1507
				ath6kl_wmi_set_pvb_cmd(ar->wmi, vif->fw_vif_idx,
						       conn->aid, 0);
Kalle Valo's avatar
Kalle Valo committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
			}
		}

		/* drop NULL data frames here */
		if ((packet->act_len < min_hdr_len) ||
		    (packet->act_len >
		     WMI_MAX_AMSDU_RX_DATA_FRAME_LENGTH)) {
			dev_kfree_skb(skb);
			return;
		}
	}

	is_amsdu = wmi_data_hdr_is_amsdu(dhdr) ? true : false;
	tid = wmi_data_hdr_get_up(dhdr);
	seq_no = wmi_data_hdr_get_seqno(dhdr);
	meta_type = wmi_data_hdr_get_meta(dhdr);
	dot11_hdr = wmi_data_hdr_get_dot11(dhdr);
1525

1526
	skb_pull(skb, sizeof(struct wmi_data_hdr));
Kalle Valo's avatar
Kalle Valo committed
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543

	switch (meta_type) {
	case WMI_META_VERSION_1:
		skb_pull(skb, sizeof(struct wmi_rx_meta_v1));
		break;
	case WMI_META_VERSION_2:
		meta = (struct wmi_rx_meta_v2 *) skb->data;
		if (meta->csum_flags & 0x1) {
			skb->ip_summed = CHECKSUM_COMPLETE;
			skb->csum = (__force __wsum) meta->csum;
		}
		skb_pull(skb, sizeof(struct wmi_rx_meta_v2));
		break;
	default:
		break;
	}

1544 1545
	skb_pull(skb, pad_before_data_start);

Kalle Valo's avatar
Kalle Valo committed
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	if (dot11_hdr)
		status = ath6kl_wmi_dot11_hdr_remove(ar->wmi, skb);
	else if (!is_amsdu)
		status = ath6kl_wmi_dot3_2_dix(skb);

	if (status) {
		/*
		 * Drop frames that could not be processed (lack of
		 * memory, etc.)
		 */
		dev_kfree_skb(skb);
		return;
	}

1560
	if (!(vif->ndev->flags & IFF_UP)) {
Kalle Valo's avatar
Kalle Valo committed
1561 1562 1563 1564
		dev_kfree_skb(skb);
		return;
	}

1565
	if (vif->nw_type == AP_NETWORK) {
Kalle Valo's avatar
Kalle Valo committed
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
		datap = (struct ethhdr *) skb->data;
		if (is_multicast_ether_addr(datap->h_dest))
			/*
			 * Bcast/Mcast frames should be sent to the
			 * OS stack as well as on the air.
			 */
			skb1 = skb_copy(skb, GFP_ATOMIC);
		else {
			/*
			 * Search for a connected STA with dstMac
			 * as the Mac address. If found send the
			 * frame to it on the air else send the
			 * frame up the stack.
			 */
1580
			conn = ath6kl_find_sta(vif, datap->h_dest);
Kalle Valo's avatar
Kalle Valo committed
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590

			if (conn && ar->intra_bss) {
				skb1 = skb;
				skb = NULL;
			} else if (conn && !ar->intra_bss) {
				dev_kfree_skb(skb);
				skb = NULL;
			}
		}
		if (skb1)
1591
			ath6kl_data_tx(skb1, vif->ndev);
1592 1593 1594 1595 1596

		if (skb == NULL) {
			/* nothing to deliver up the stack */
			return;
		}
Kalle Valo's avatar
Kalle Valo committed
1597 1598
	}

1599 1600
	datap = (struct ethhdr *) skb->data;

1601 1602 1603 1604 1605 1606
	if (is_unicast_ether_addr(datap->h_dest)) {
		if (vif->nw_type == AP_NETWORK) {
			conn = ath6kl_find_sta(vif, datap->h_source);
			if (!conn)
				return;
			aggr_conn = conn->aggr_conn;
1607
		} else {
1608
			aggr_conn = vif->aggr_cntxt->aggr_conn;
1609
		}
1610 1611

		if (aggr_process_recv_frm(aggr_conn, tid, seq_no,
1612
					  is_amsdu, skb)) {
1613 1614 1615
			/* aggregation code will handle the skb */
			return;
		}
1616
	} else if (!is_broadcast_ether_addr(datap->h_dest)) {
1617
		vif->ndev->stats.multicast++;
1618
	}
1619

1620
	ath6kl_deliver_frames_to_nw_stack(vif->ndev, skb);
Kalle Valo's avatar
Kalle Valo committed
1621 1622 1623 1624 1625
}

static void aggr_timeout(unsigned long arg)
{
	u8 i, j;
1626
	struct aggr_info_conn *aggr_conn = (struct aggr_info_conn *) arg;
Kalle Valo's avatar
Kalle Valo committed
1627 1628 1629 1630
	struct rxtid *rxtid;
	struct rxtid_stats *stats;

	for (i = 0; i < NUM_OF_TIDS; i++) {
1631 1632
		rxtid = &aggr_conn->rx_tid[i];
		stats = &aggr_conn->stat[i];
Kalle Valo's avatar
Kalle Valo committed
1633

1634
		if (!rxtid->aggr || !rxtid->timer_mon)
Kalle Valo's avatar
Kalle Valo committed
1635 1636 1637
			continue;

		stats->num_timeouts++;
1638 1639
		ath6kl_dbg(ATH6KL_DBG_AGGR,
			   "aggr timeout (st %d end %d)\n",
Kalle Valo's avatar
Kalle Valo committed
1640 1641 1642
			   rxtid->seq_next,
			   ((rxtid->seq_next + rxtid->hold_q_sz-1) &
			    ATH6KL_MAX_SEQ_NO));
1643
		aggr_deque_frms(aggr_conn, i, 0, 0);
Kalle Valo's avatar
Kalle Valo committed
1644 1645
	}

1646
	aggr_conn->timer_scheduled = false;
Kalle Valo's avatar
Kalle Valo committed
1647 1648

	for (i = 0; i < NUM_OF_TIDS; i++) {
1649
		rxtid = &aggr_conn->rx_tid[i];
Kalle Valo's avatar
Kalle Valo committed
1650 1651

		if (rxtid->aggr && rxtid->hold_q) {
1652
			spin_lock_bh(&rxtid->lock);
Kalle Valo's avatar
Kalle Valo committed
1653 1654
			for (j = 0; j < rxtid->hold_q_sz; j++) {
				if (rxtid->hold_q[j].skb) {
1655
					aggr_conn->timer_scheduled = true;
Kalle Valo's avatar
Kalle Valo committed
1656 1657 1658 1659
					rxtid->timer_mon = true;
					break;
				}
			}
1660
			spin_unlock_bh(&rxtid->lock);
Kalle Valo's avatar
Kalle Valo committed
1661 1662 1663 1664 1665 1666

			if (j >= rxtid->hold_q_sz)
				rxtid->timer_mon = false;
		}
	}

1667 1668
	if (aggr_conn->timer_scheduled)
		mod_timer(&aggr_conn->timer,
Kalle Valo's avatar
Kalle Valo committed
1669 1670 1671
			  jiffies + msecs_to_jiffies(AGGR_RX_TIMEOUT));
}

1672
static void aggr_delete_tid_state(struct aggr_info_conn *aggr_conn, u8 tid)
Kalle Valo's avatar
Kalle Valo committed
1673 1674 1675 1676
{
	struct rxtid *rxtid;
	struct rxtid_stats *stats;

1677
	if (!aggr_conn || tid >= NUM_OF_TIDS)
Kalle Valo's avatar
Kalle Valo committed
1678 1679
		return;

1680 1681
	rxtid = &aggr_conn->rx_tid[tid];
	stats = &aggr_conn->stat[tid];
Kalle Valo's avatar
Kalle Valo committed
1682 1683

	if (rxtid->aggr)
1684
		aggr_deque_frms(aggr_conn, tid, 0, 0);
Kalle Valo's avatar
Kalle Valo committed
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697

	rxtid->aggr = false;
	rxtid->timer_mon = false;
	rxtid->win_sz = 0;
	rxtid->seq_next = 0;
	rxtid->hold_q_sz = 0;

	kfree(rxtid->hold_q);
	rxtid->hold_q = NULL;

	memset(stats, 0, sizeof(struct rxtid_stats));
}

1698
void aggr_recv_addba_req_evt(struct ath6kl_vif *vif, u8 tid_mux, u16 seq_no,
1699
			     u8 win_sz)
Kalle Valo's avatar
Kalle Valo committed
1700
{
1701 1702
	struct ath6kl_sta *sta;
	struct aggr_info_conn *aggr_conn = NULL;
Kalle Valo's avatar
Kalle Valo committed
1703 1704 1705
	struct rxtid *rxtid;
	struct rxtid_stats *stats;
	u16 hold_q_size;
1706
	u8 tid, aid;
Kalle Valo's avatar
Kalle Valo committed
1707

1708 1709 1710 1711 1712
	if (vif->nw_type == AP_NETWORK) {
		aid = ath6kl_get_aid(tid_mux);
		sta = ath6kl_find_sta_by_aid(vif->ar, aid);
		if (sta)
			aggr_conn = sta->aggr_conn;
1713
	} else {
1714
		aggr_conn = vif->aggr_cntxt->aggr_conn;
1715
	}
Kalle Valo's avatar
Kalle Valo committed
1716

1717 1718
	if (!aggr_conn)
		return;
1719

1720 1721 1722 1723
	tid = ath6kl_get_tid(tid_mux);
	if (tid >= NUM_OF_TIDS)
		return;

1724 1725
	rxtid = &aggr_conn->rx_tid[tid];
	stats = &aggr_conn->stat[tid];
Kalle Valo's avatar
Kalle Valo committed
1726 1727 1728 1729 1730 1731

	if (win_sz < AGGR_WIN_SZ_MIN || win_sz > AGGR_WIN_SZ_MAX)
		ath6kl_dbg(ATH6KL_DBG_WLAN_RX, "%s: win_sz %d, tid %d\n",
			   __func__, win_sz, tid);

	if (rxtid->aggr)
1732
		aggr_delete_tid_state(aggr_conn, tid);
Kalle Valo's avatar
Kalle Valo committed
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747

	rxtid->seq_next = seq_no;
	hold_q_size = TID_WINDOW_SZ(win_sz) * sizeof(struct skb_hold_q);
	rxtid->hold_q = kzalloc(hold_q_size, GFP_KERNEL);
	if (!rxtid->hold_q)
		return;

	rxtid->win_sz = win_sz;
	rxtid->hold_q_sz = TID_WINDOW_SZ(win_sz);
	if (!skb_queue_empty(&rxtid->q))
		return;

	rxtid->aggr = true;
}

1748 1749
void aggr_conn_init(struct ath6kl_vif *vif, struct aggr_info *aggr_info,
		    struct aggr_info_conn *aggr_conn)
Kalle Valo's avatar
Kalle Valo committed
1750 1751 1752 1753
{
	struct rxtid *rxtid;
	u8 i;

1754 1755 1756 1757 1758
	aggr_conn->aggr_sz = AGGR_SZ_DEFAULT;
	aggr_conn->dev = vif->ndev;
	init_timer(&aggr_conn->timer);
	aggr_conn->timer.function = aggr_timeout;
	aggr_conn->timer.data = (unsigned long) aggr_conn;
1759
	aggr_conn->aggr_info = aggr_info;
Kalle Valo's avatar
Kalle Valo committed
1760

1761
	aggr_conn->timer_scheduled = false;
Kalle Valo's avatar
Kalle Valo committed
1762 1763

	for (i = 0; i < NUM_OF_TIDS; i++) {
1764
		rxtid = &aggr_conn->rx_tid[i];
Kalle Valo's avatar
Kalle Valo committed
1765 1766 1767 1768 1769
		rxtid->aggr = false;
		rxtid->timer_mon = false;
		skb_queue_head_init(&rxtid->q);
		spin_lock_init(&rxtid->lock);
	}
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
}

struct aggr_info *aggr_init(struct ath6kl_vif *vif)
{
	struct aggr_info *p_aggr = NULL;

	p_aggr = kzalloc(sizeof(struct aggr_info), GFP_KERNEL);
	if (!p_aggr) {
		ath6kl_err("failed to alloc memory for aggr_node\n");
		return NULL;
	}

	p_aggr->aggr_conn = kzalloc(sizeof(struct aggr_info_conn), GFP_KERNEL);
	if (!p_aggr->aggr_conn) {
		ath6kl_err("failed to alloc memory for connection specific aggr info\n");
		kfree(p_aggr);
		return NULL;
	}

1789
	aggr_conn_init(vif, p_aggr, p_aggr->aggr_conn);
1790 1791 1792 1793

	skb_queue_head_init(&p_aggr->rx_amsdu_freeq);
	ath6kl_alloc_netbufs(&p_aggr->rx_amsdu_freeq, AGGR_NUM_OF_FREE_NETBUFS);

Kalle Valo's avatar
Kalle Valo committed
1794 1795 1796
	return p_aggr;
}

1797
void aggr_recv_delba_req_evt(struct ath6kl_vif *vif, u8 tid_mux)
Kalle Valo's avatar
Kalle Valo committed
1798
{
1799
	struct ath6kl_sta *sta;
Kalle Valo's avatar
Kalle Valo committed
1800
	struct rxtid *rxtid;
1801 1802 1803 1804 1805 1806 1807 1808
	struct aggr_info_conn *aggr_conn = NULL;
	u8 tid, aid;

	if (vif->nw_type == AP_NETWORK) {
		aid = ath6kl_get_aid(tid_mux);
		sta = ath6kl_find_sta_by_aid(vif->ar, aid);
		if (sta)
			aggr_conn = sta->aggr_conn;
1809
	} else {
1810
		aggr_conn = vif->aggr_cntxt->aggr_conn;
1811
	}
Kalle Valo's avatar
Kalle Valo committed
1812

1813
	if (!aggr_conn)
Kalle Valo's avatar
Kalle Valo committed
1814 1815
		return;

1816 1817 1818 1819
	tid = ath6kl_get_tid(tid_mux);
	if (tid >= NUM_OF_TIDS)
		return;

1820
	rxtid = &aggr_conn->rx_tid[tid];
Kalle Valo's avatar
Kalle Valo committed
1821 1822

	if (rxtid->aggr)
1823
		aggr_delete_tid_state(aggr_conn, tid);
Kalle Valo's avatar
Kalle Valo committed
1824 1825
}

1826
void aggr_reset_state(struct aggr_info_conn *aggr_conn)
Kalle Valo's avatar
Kalle Valo committed
1827 1828 1829
{
	u8 tid;

1830
	if (!aggr_conn)
1831 1832
		return;

1833 1834 1835
	if (aggr_conn->timer_scheduled) {
		del_timer(&aggr_conn->timer);
		aggr_conn->timer_scheduled = false;
1836 1837
	}

Kalle Valo's avatar
Kalle Valo committed
1838
	for (tid = 0; tid < NUM_OF_TIDS; tid++)
1839
		aggr_delete_tid_state(aggr_conn, tid);
Kalle Valo's avatar
Kalle Valo committed
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
}

/* clean up our amsdu buffer list */
void ath6kl_cleanup_amsdu_rxbufs(struct ath6kl *ar)
{
	struct htc_packet *packet, *tmp_pkt;

	spin_lock_bh(&ar->lock);
	if (list_empty(&ar->amsdu_rx_buffer_queue)) {
		spin_unlock_bh(&ar->lock);
		return;
	}

	list_for_each_entry_safe(packet, tmp_pkt, &ar->amsdu_rx_buffer_queue,
				 list) {
		list_del(&packet->list);
		spin_unlock_bh(&ar->lock);
		dev_kfree_skb(packet->pkt_cntxt);
		spin_lock_bh(&ar->lock);
	}

	spin_unlock_bh(&ar->lock);
}

void aggr_module_destroy(struct aggr_info *aggr_info)
{
1866
	if (!aggr_info)
Kalle Valo's avatar
Kalle Valo committed
1867 1868
		return;

1869
	aggr_reset_state(aggr_info->aggr_conn);
1870 1871
	skb_queue_purge(&aggr_info->rx_amsdu_freeq);
	kfree(aggr_info->aggr_conn);
Kalle Valo's avatar
Kalle Valo committed
1872 1873
	kfree(aggr_info);
}