tx.c revision d2f4d47d84f8c665ab9babb2cc84d2e7872a96e1
1/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2009 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/etherdevice.h>
27
28#include "wl12xx.h"
29#include "io.h"
30#include "reg.h"
31#include "ps.h"
32#include "tx.h"
33
34static int wl1271_set_default_wep_key(struct wl1271 *wl, u8 id)
35{
36	int ret;
37	bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
38
39	if (is_ap)
40		ret = wl1271_cmd_set_ap_default_wep_key(wl, id);
41	else
42		ret = wl1271_cmd_set_sta_default_wep_key(wl, id);
43
44	if (ret < 0)
45		return ret;
46
47	wl1271_debug(DEBUG_CRYPT, "default wep key idx: %d", (int)id);
48	return 0;
49}
50
51static int wl1271_alloc_tx_id(struct wl1271 *wl, struct sk_buff *skb)
52{
53	int id;
54
55	id = find_first_zero_bit(wl->tx_frames_map, ACX_TX_DESCRIPTORS);
56	if (id >= ACX_TX_DESCRIPTORS)
57		return -EBUSY;
58
59	__set_bit(id, wl->tx_frames_map);
60	wl->tx_frames[id] = skb;
61	wl->tx_frames_cnt++;
62	return id;
63}
64
65static void wl1271_free_tx_id(struct wl1271 *wl, int id)
66{
67	if (__test_and_clear_bit(id, wl->tx_frames_map)) {
68		wl->tx_frames[id] = NULL;
69		wl->tx_frames_cnt--;
70	}
71}
72
73static int wl1271_tx_update_filters(struct wl1271 *wl,
74						 struct sk_buff *skb)
75{
76	struct ieee80211_hdr *hdr;
77
78	hdr = (struct ieee80211_hdr *)(skb->data +
79				       sizeof(struct wl1271_tx_hw_descr));
80
81	/*
82	 * stop bssid-based filtering before transmitting authentication
83	 * requests. this way the hw will never drop authentication
84	 * responses coming from BSSIDs it isn't familiar with (e.g. on
85	 * roaming)
86	 */
87	if (!ieee80211_is_auth(hdr->frame_control))
88		return 0;
89
90	wl1271_configure_filters(wl, FIF_OTHER_BSS);
91
92	return wl1271_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
93}
94
95static void wl1271_tx_ap_update_inconnection_sta(struct wl1271 *wl,
96						 struct sk_buff *skb)
97{
98	struct ieee80211_hdr *hdr;
99
100	/*
101	 * add the station to the known list before transmitting the
102	 * authentication response. this way it won't get de-authed by FW
103	 * when transmitting too soon.
104	 */
105	hdr = (struct ieee80211_hdr *)(skb->data +
106				       sizeof(struct wl1271_tx_hw_descr));
107	if (ieee80211_is_auth(hdr->frame_control))
108		wl1271_acx_set_inconnection_sta(wl, hdr->addr1);
109}
110
111static void wl1271_tx_regulate_link(struct wl1271 *wl, u8 hlid)
112{
113	bool fw_ps;
114	u8 tx_blks;
115
116	/* only regulate station links */
117	if (hlid < WL1271_AP_STA_HLID_START)
118		return;
119
120	fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
121	tx_blks = wl->links[hlid].allocated_blks;
122
123	/*
124	 * if in FW PS and there is enough data in FW we can put the link
125	 * into high-level PS and clean out its TX queues.
126	 */
127	if (fw_ps && tx_blks >= WL1271_PS_STA_MAX_BLOCKS)
128		wl1271_ps_link_start(wl, hlid, true);
129}
130
131u8 wl1271_tx_get_hlid(struct sk_buff *skb)
132{
133	struct ieee80211_tx_info *control = IEEE80211_SKB_CB(skb);
134
135	if (control->control.sta) {
136		struct wl1271_station *wl_sta;
137
138		wl_sta = (struct wl1271_station *)
139				control->control.sta->drv_priv;
140		return wl_sta->hlid;
141	} else {
142		struct ieee80211_hdr *hdr;
143
144		hdr = (struct ieee80211_hdr *)skb->data;
145		if (ieee80211_is_mgmt(hdr->frame_control))
146			return WL1271_AP_GLOBAL_HLID;
147		else
148			return WL1271_AP_BROADCAST_HLID;
149	}
150}
151
152static unsigned int wl12xx_calc_packet_alignment(struct wl1271 *wl,
153						unsigned int packet_length)
154{
155	if (wl->quirks & WL12XX_QUIRK_BLOCKSIZE_ALIGNMENT)
156		return ALIGN(packet_length, WL12XX_BUS_BLOCK_SIZE);
157	else
158		return ALIGN(packet_length, WL1271_TX_ALIGN_TO);
159}
160
161static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra,
162				u32 buf_offset, u8 hlid)
163{
164	struct wl1271_tx_hw_descr *desc;
165	u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
166	u32 len;
167	u32 total_blocks;
168	int id, ret = -EBUSY;
169	u32 spare_blocks;
170
171	if (unlikely(wl->quirks & WL12XX_QUIRK_USE_2_SPARE_BLOCKS))
172		spare_blocks = 2;
173	else
174		spare_blocks = 1;
175
176	if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
177		return -EAGAIN;
178
179	/* allocate free identifier for the packet */
180	id = wl1271_alloc_tx_id(wl, skb);
181	if (id < 0)
182		return id;
183
184	/* approximate the number of blocks required for this packet
185	   in the firmware */
186	len = wl12xx_calc_packet_alignment(wl, total_len);
187
188	total_blocks = (len + TX_HW_BLOCK_SIZE - 1) / TX_HW_BLOCK_SIZE +
189		spare_blocks;
190
191	if (total_blocks <= wl->tx_blocks_available) {
192		desc = (struct wl1271_tx_hw_descr *)skb_push(
193			skb, total_len - skb->len);
194
195		/* HW descriptor fields change between wl127x and wl128x */
196		if (wl->chip.id == CHIP_ID_1283_PG20) {
197			desc->wl128x_mem.total_mem_blocks = total_blocks;
198		} else {
199			desc->wl127x_mem.extra_blocks = spare_blocks;
200			desc->wl127x_mem.total_mem_blocks = total_blocks;
201		}
202
203		desc->id = id;
204
205		wl->tx_blocks_available -= total_blocks;
206		wl->tx_allocated_blocks += total_blocks;
207
208		if (wl->bss_type == BSS_TYPE_AP_BSS)
209			wl->links[hlid].allocated_blks += total_blocks;
210
211		ret = 0;
212
213		wl1271_debug(DEBUG_TX,
214			     "tx_allocate: size: %d, blocks: %d, id: %d",
215			     total_len, total_blocks, id);
216	} else {
217		wl1271_free_tx_id(wl, id);
218	}
219
220	return ret;
221}
222
223static bool wl12xx_is_dummy_packet(struct wl1271 *wl, struct sk_buff *skb)
224{
225	return wl->dummy_packet == skb;
226}
227
228static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
229			      u32 extra, struct ieee80211_tx_info *control,
230			      u8 hlid)
231{
232	struct timespec ts;
233	struct wl1271_tx_hw_descr *desc;
234	int aligned_len, ac, rate_idx;
235	s64 hosttime;
236	u16 tx_attr;
237
238	desc = (struct wl1271_tx_hw_descr *) skb->data;
239
240	/* relocate space for security header */
241	if (extra) {
242		void *framestart = skb->data + sizeof(*desc);
243		u16 fc = *(u16 *)(framestart + extra);
244		int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
245		memmove(framestart, framestart + extra, hdrlen);
246	}
247
248	/* configure packet life time */
249	getnstimeofday(&ts);
250	hosttime = (timespec_to_ns(&ts) >> 10);
251	desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
252
253	if (wl->bss_type != BSS_TYPE_AP_BSS)
254		desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
255	else
256		desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
257
258	/* queue */
259	ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
260	desc->tid = skb->priority;
261
262	if (wl12xx_is_dummy_packet(wl, skb)) {
263		/*
264		 * FW expects the dummy packet to have an invalid session id -
265		 * any session id that is different than the one set in the join
266		 */
267		tx_attr = ((~wl->session_counter) <<
268			   TX_HW_ATTR_OFST_SESSION_COUNTER) &
269			   TX_HW_ATTR_SESSION_COUNTER;
270
271		tx_attr |= TX_HW_ATTR_TX_DUMMY_REQ;
272	} else {
273		/* configure the tx attributes */
274		tx_attr =
275			wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
276	}
277
278	if (wl->bss_type != BSS_TYPE_AP_BSS) {
279		desc->aid = hlid;
280
281		/* if the packets are destined for AP (have a STA entry)
282		   send them with AP rate policies, otherwise use default
283		   basic rates */
284		if (control->control.sta)
285			rate_idx = ACX_TX_AP_FULL_RATE;
286		else
287			rate_idx = ACX_TX_BASIC_RATE;
288	} else {
289		desc->hlid = hlid;
290		switch (hlid) {
291		case WL1271_AP_GLOBAL_HLID:
292			rate_idx = ACX_TX_AP_MODE_MGMT_RATE;
293			break;
294		case WL1271_AP_BROADCAST_HLID:
295			rate_idx = ACX_TX_AP_MODE_BCST_RATE;
296			break;
297		default:
298			rate_idx = ac;
299			break;
300		}
301	}
302
303	tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
304	desc->reserved = 0;
305
306	aligned_len = wl12xx_calc_packet_alignment(wl, skb->len);
307
308	if (wl->chip.id == CHIP_ID_1283_PG20) {
309		desc->wl128x_mem.extra_bytes = aligned_len - skb->len;
310		desc->length = cpu_to_le16(aligned_len >> 2);
311
312		wl1271_debug(DEBUG_TX, "tx_fill_hdr: hlid: %d "
313			     "tx_attr: 0x%x len: %d life: %d mem: %d",
314			     desc->hlid, tx_attr,
315			     le16_to_cpu(desc->length),
316			     le16_to_cpu(desc->life_time),
317			     desc->wl128x_mem.total_mem_blocks);
318	} else {
319		int pad;
320
321		/* Store the aligned length in terms of words */
322		desc->length = cpu_to_le16(aligned_len >> 2);
323
324		/* calculate number of padding bytes */
325		pad = aligned_len - skb->len;
326		tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
327
328		wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d hlid: %d "
329			     "tx_attr: 0x%x len: %d life: %d mem: %d", pad,
330			     desc->hlid, tx_attr,
331			     le16_to_cpu(desc->length),
332			     le16_to_cpu(desc->life_time),
333			     desc->wl127x_mem.total_mem_blocks);
334	}
335
336	desc->tx_attr = cpu_to_le16(tx_attr);
337}
338
339/* caller must hold wl->mutex */
340static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct sk_buff *skb,
341							u32 buf_offset)
342{
343	struct ieee80211_tx_info *info;
344	u32 extra = 0;
345	int ret = 0;
346	u32 total_len;
347	u8 hlid;
348
349	if (!skb)
350		return -EINVAL;
351
352	info = IEEE80211_SKB_CB(skb);
353
354	if (info->control.hw_key &&
355	    info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
356		extra = WL1271_TKIP_IV_SPACE;
357
358	if (info->control.hw_key) {
359		bool is_wep;
360		u8 idx = info->control.hw_key->hw_key_idx;
361		u32 cipher = info->control.hw_key->cipher;
362
363		is_wep = (cipher == WLAN_CIPHER_SUITE_WEP40) ||
364			 (cipher == WLAN_CIPHER_SUITE_WEP104);
365
366		if (unlikely(is_wep && wl->default_key != idx)) {
367			ret = wl1271_set_default_wep_key(wl, idx);
368			if (ret < 0)
369				return ret;
370			wl->default_key = idx;
371		}
372	}
373
374	if (wl->bss_type == BSS_TYPE_AP_BSS)
375		hlid = wl1271_tx_get_hlid(skb);
376	else
377		hlid = TX_HW_DEFAULT_AID;
378
379	ret = wl1271_tx_allocate(wl, skb, extra, buf_offset, hlid);
380	if (ret < 0)
381		return ret;
382
383	if (wl->bss_type == BSS_TYPE_AP_BSS) {
384		wl1271_tx_ap_update_inconnection_sta(wl, skb);
385		wl1271_tx_regulate_link(wl, hlid);
386	} else {
387		wl1271_tx_update_filters(wl, skb);
388	}
389
390	wl1271_tx_fill_hdr(wl, skb, extra, info, hlid);
391
392	/*
393	 * The length of each packet is stored in terms of
394	 * words. Thus, we must pad the skb data to make sure its
395	 * length is aligned.  The number of padding bytes is computed
396	 * and set in wl1271_tx_fill_hdr.
397	 * In special cases, we want to align to a specific block size
398	 * (eg. for wl128x with SDIO we align to 256).
399	 */
400	total_len = wl12xx_calc_packet_alignment(wl, skb->len);
401
402	memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
403	memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
404
405	/* Revert side effects in the dummy packet skb, so it can be reused */
406	if (wl12xx_is_dummy_packet(wl, skb))
407		skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
408
409	return total_len;
410}
411
412u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
413{
414	struct ieee80211_supported_band *band;
415	u32 enabled_rates = 0;
416	int bit;
417
418	band = wl->hw->wiphy->bands[wl->band];
419	for (bit = 0; bit < band->n_bitrates; bit++) {
420		if (rate_set & 0x1)
421			enabled_rates |= band->bitrates[bit].hw_value;
422		rate_set >>= 1;
423	}
424
425#ifdef CONFIG_WL12XX_HT
426	/* MCS rates indication are on bits 16 - 23 */
427	rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
428
429	for (bit = 0; bit < 8; bit++) {
430		if (rate_set & 0x1)
431			enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
432		rate_set >>= 1;
433	}
434#endif
435
436	return enabled_rates;
437}
438
439void wl1271_handle_tx_low_watermark(struct wl1271 *wl)
440{
441	unsigned long flags;
442
443	if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
444	    wl->tx_queue_count <= WL1271_TX_QUEUE_LOW_WATERMARK) {
445		/* firmware buffer has space, restart queues */
446		spin_lock_irqsave(&wl->wl_lock, flags);
447		ieee80211_wake_queues(wl->hw);
448		clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
449		spin_unlock_irqrestore(&wl->wl_lock, flags);
450	}
451}
452
453static struct sk_buff *wl1271_sta_skb_dequeue(struct wl1271 *wl)
454{
455	struct sk_buff *skb = NULL;
456	unsigned long flags;
457
458	skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VO]);
459	if (skb)
460		goto out;
461	skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VI]);
462	if (skb)
463		goto out;
464	skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BE]);
465	if (skb)
466		goto out;
467	skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BK]);
468
469out:
470	if (skb) {
471		spin_lock_irqsave(&wl->wl_lock, flags);
472		wl->tx_queue_count--;
473		spin_unlock_irqrestore(&wl->wl_lock, flags);
474	}
475
476	return skb;
477}
478
479static struct sk_buff *wl1271_ap_skb_dequeue(struct wl1271 *wl)
480{
481	struct sk_buff *skb = NULL;
482	unsigned long flags;
483	int i, h, start_hlid;
484
485	/* start from the link after the last one */
486	start_hlid = (wl->last_tx_hlid + 1) % AP_MAX_LINKS;
487
488	/* dequeue according to AC, round robin on each link */
489	for (i = 0; i < AP_MAX_LINKS; i++) {
490		h = (start_hlid + i) % AP_MAX_LINKS;
491
492		skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_VO]);
493		if (skb)
494			goto out;
495		skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_VI]);
496		if (skb)
497			goto out;
498		skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_BE]);
499		if (skb)
500			goto out;
501		skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_BK]);
502		if (skb)
503			goto out;
504	}
505
506out:
507	if (skb) {
508		wl->last_tx_hlid = h;
509		spin_lock_irqsave(&wl->wl_lock, flags);
510		wl->tx_queue_count--;
511		spin_unlock_irqrestore(&wl->wl_lock, flags);
512	} else {
513		wl->last_tx_hlid = 0;
514	}
515
516	return skb;
517}
518
519static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
520{
521	unsigned long flags;
522	struct sk_buff *skb = NULL;
523
524	if (wl->bss_type == BSS_TYPE_AP_BSS)
525		skb = wl1271_ap_skb_dequeue(wl);
526	else
527		skb = wl1271_sta_skb_dequeue(wl);
528
529	if (!skb &&
530	    test_and_clear_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags)) {
531		skb = wl->dummy_packet;
532		spin_lock_irqsave(&wl->wl_lock, flags);
533		wl->tx_queue_count--;
534		spin_unlock_irqrestore(&wl->wl_lock, flags);
535	}
536
537	return skb;
538}
539
540static void wl1271_skb_queue_head(struct wl1271 *wl, struct sk_buff *skb)
541{
542	unsigned long flags;
543	int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
544
545	if (wl12xx_is_dummy_packet(wl, skb)) {
546		set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
547	} else if (wl->bss_type == BSS_TYPE_AP_BSS) {
548		u8 hlid = wl1271_tx_get_hlid(skb);
549		skb_queue_head(&wl->links[hlid].tx_queue[q], skb);
550
551		/* make sure we dequeue the same packet next time */
552		wl->last_tx_hlid = (hlid + AP_MAX_LINKS - 1) % AP_MAX_LINKS;
553	} else {
554		skb_queue_head(&wl->tx_queue[q], skb);
555	}
556
557	spin_lock_irqsave(&wl->wl_lock, flags);
558	wl->tx_queue_count++;
559	spin_unlock_irqrestore(&wl->wl_lock, flags);
560}
561
562void wl1271_tx_work_locked(struct wl1271 *wl)
563{
564	struct sk_buff *skb;
565	u32 buf_offset = 0;
566	bool sent_packets = false;
567	int ret;
568
569	if (unlikely(wl->state == WL1271_STATE_OFF))
570		return;
571
572	while ((skb = wl1271_skb_dequeue(wl))) {
573		ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
574		if (ret == -EAGAIN) {
575			/*
576			 * Aggregation buffer is full.
577			 * Flush buffer and try again.
578			 */
579			wl1271_skb_queue_head(wl, skb);
580			wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
581				     buf_offset, true);
582			sent_packets = true;
583			buf_offset = 0;
584			continue;
585		} else if (ret == -EBUSY) {
586			/*
587			 * Firmware buffer is full.
588			 * Queue back last skb, and stop aggregating.
589			 */
590			wl1271_skb_queue_head(wl, skb);
591			/* No work left, avoid scheduling redundant tx work */
592			set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
593			goto out_ack;
594		} else if (ret < 0) {
595			dev_kfree_skb(skb);
596			goto out_ack;
597		}
598		buf_offset += ret;
599		wl->tx_packets_count++;
600	}
601
602out_ack:
603	if (buf_offset) {
604		wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
605				buf_offset, true);
606		sent_packets = true;
607	}
608	if (sent_packets) {
609		/*
610		 * Interrupt the firmware with the new packets. This is only
611		 * required for older hardware revisions
612		 */
613		if (wl->quirks & WL12XX_QUIRK_END_OF_TRANSACTION)
614			wl1271_write32(wl, WL1271_HOST_WR_ACCESS,
615				       wl->tx_packets_count);
616
617		wl1271_handle_tx_low_watermark(wl);
618	}
619}
620
621void wl1271_tx_work(struct work_struct *work)
622{
623	struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
624	int ret;
625
626	mutex_lock(&wl->mutex);
627	ret = wl1271_ps_elp_wakeup(wl);
628	if (ret < 0)
629		goto out;
630
631	wl1271_tx_work_locked(wl);
632
633	wl1271_ps_elp_wakeup(wl);
634out:
635	mutex_unlock(&wl->mutex);
636}
637
638static void wl1271_tx_complete_packet(struct wl1271 *wl,
639				      struct wl1271_tx_hw_res_descr *result)
640{
641	struct ieee80211_tx_info *info;
642	struct sk_buff *skb;
643	int id = result->id;
644	int rate = -1;
645	u8 retries = 0;
646
647	/* check for id legality */
648	if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
649		wl1271_warning("TX result illegal id: %d", id);
650		return;
651	}
652
653	skb = wl->tx_frames[id];
654	info = IEEE80211_SKB_CB(skb);
655
656	if (wl12xx_is_dummy_packet(wl, skb)) {
657		wl1271_free_tx_id(wl, id);
658		return;
659	}
660
661	/* update the TX status info */
662	if (result->status == TX_SUCCESS) {
663		if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
664			info->flags |= IEEE80211_TX_STAT_ACK;
665		rate = wl1271_rate_to_idx(result->rate_class_index, wl->band);
666		retries = result->ack_failures;
667	} else if (result->status == TX_RETRY_EXCEEDED) {
668		wl->stats.excessive_retries++;
669		retries = result->ack_failures;
670	}
671
672	info->status.rates[0].idx = rate;
673	info->status.rates[0].count = retries;
674	info->status.rates[0].flags = 0;
675	info->status.ack_signal = -1;
676
677	wl->stats.retry_count += result->ack_failures;
678
679	/* update security sequence number */
680	wl->tx_security_seq += (result->lsb_security_sequence_number -
681				wl->tx_security_last_seq);
682	wl->tx_security_last_seq = result->lsb_security_sequence_number;
683
684	/* remove private header from packet */
685	skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
686
687	/* remove TKIP header space if present */
688	if (info->control.hw_key &&
689	    info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
690		int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
691		memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
692		skb_pull(skb, WL1271_TKIP_IV_SPACE);
693	}
694
695	wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
696		     " status 0x%x",
697		     result->id, skb, result->ack_failures,
698		     result->rate_class_index, result->status);
699
700	/* return the packet to the stack */
701	skb_queue_tail(&wl->deferred_tx_queue, skb);
702	ieee80211_queue_work(wl->hw, &wl->netstack_work);
703	wl1271_free_tx_id(wl, result->id);
704}
705
706/* Called upon reception of a TX complete interrupt */
707void wl1271_tx_complete(struct wl1271 *wl)
708{
709	struct wl1271_acx_mem_map *memmap =
710		(struct wl1271_acx_mem_map *)wl->target_mem_map;
711	u32 count, fw_counter;
712	u32 i;
713
714	/* read the tx results from the chipset */
715	wl1271_read(wl, le32_to_cpu(memmap->tx_result),
716		    wl->tx_res_if, sizeof(*wl->tx_res_if), false);
717	fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
718
719	/* write host counter to chipset (to ack) */
720	wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
721		       offsetof(struct wl1271_tx_hw_res_if,
722				tx_result_host_counter), fw_counter);
723
724	count = fw_counter - wl->tx_results_count;
725	wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
726
727	/* verify that the result buffer is not getting overrun */
728	if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
729		wl1271_warning("TX result overflow from chipset: %d", count);
730
731	/* process the results */
732	for (i = 0; i < count; i++) {
733		struct wl1271_tx_hw_res_descr *result;
734		u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
735
736		/* process the packet */
737		result =  &(wl->tx_res_if->tx_results_queue[offset]);
738		wl1271_tx_complete_packet(wl, result);
739
740		wl->tx_results_count++;
741	}
742}
743
744void wl1271_tx_reset_link_queues(struct wl1271 *wl, u8 hlid)
745{
746	struct sk_buff *skb;
747	int i, total = 0;
748	unsigned long flags;
749	struct ieee80211_tx_info *info;
750
751	for (i = 0; i < NUM_TX_QUEUES; i++) {
752		while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
753			wl1271_debug(DEBUG_TX, "link freeing skb 0x%p", skb);
754			info = IEEE80211_SKB_CB(skb);
755			info->status.rates[0].idx = -1;
756			info->status.rates[0].count = 0;
757			ieee80211_tx_status(wl->hw, skb);
758			total++;
759		}
760	}
761
762	spin_lock_irqsave(&wl->wl_lock, flags);
763	wl->tx_queue_count -= total;
764	spin_unlock_irqrestore(&wl->wl_lock, flags);
765
766	wl1271_handle_tx_low_watermark(wl);
767}
768
769/* caller must hold wl->mutex */
770void wl1271_tx_reset(struct wl1271 *wl)
771{
772	int i;
773	struct sk_buff *skb;
774	struct ieee80211_tx_info *info;
775
776	/* TX failure */
777	if (wl->bss_type == BSS_TYPE_AP_BSS) {
778		for (i = 0; i < AP_MAX_LINKS; i++) {
779			wl1271_tx_reset_link_queues(wl, i);
780			wl->links[i].allocated_blks = 0;
781			wl->links[i].prev_freed_blks = 0;
782		}
783
784		wl->last_tx_hlid = 0;
785	} else {
786		for (i = 0; i < NUM_TX_QUEUES; i++) {
787			while ((skb = skb_dequeue(&wl->tx_queue[i]))) {
788				wl1271_debug(DEBUG_TX, "freeing skb 0x%p",
789					     skb);
790
791				if (!wl12xx_is_dummy_packet(wl, skb)) {
792					info = IEEE80211_SKB_CB(skb);
793					info->status.rates[0].idx = -1;
794					info->status.rates[0].count = 0;
795					ieee80211_tx_status(wl->hw, skb);
796				}
797			}
798		}
799	}
800
801	wl->tx_queue_count = 0;
802
803	/*
804	 * Make sure the driver is at a consistent state, in case this
805	 * function is called from a context other than interface removal.
806	 */
807	wl1271_handle_tx_low_watermark(wl);
808
809	for (i = 0; i < ACX_TX_DESCRIPTORS; i++) {
810		if (wl->tx_frames[i] == NULL)
811			continue;
812
813		skb = wl->tx_frames[i];
814		wl1271_free_tx_id(wl, i);
815		wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
816
817		if (!wl12xx_is_dummy_packet(wl, skb)) {
818			/*
819			 * Remove private headers before passing the skb to
820			 * mac80211
821			 */
822			info = IEEE80211_SKB_CB(skb);
823			skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
824			if (info->control.hw_key &&
825			    info->control.hw_key->cipher ==
826			    WLAN_CIPHER_SUITE_TKIP) {
827				int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
828				memmove(skb->data + WL1271_TKIP_IV_SPACE,
829					skb->data, hdrlen);
830				skb_pull(skb, WL1271_TKIP_IV_SPACE);
831			}
832
833			info->status.rates[0].idx = -1;
834			info->status.rates[0].count = 0;
835
836			ieee80211_tx_status(wl->hw, skb);
837		}
838	}
839}
840
841#define WL1271_TX_FLUSH_TIMEOUT 500000
842
843/* caller must *NOT* hold wl->mutex */
844void wl1271_tx_flush(struct wl1271 *wl)
845{
846	unsigned long timeout;
847	timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
848
849	while (!time_after(jiffies, timeout)) {
850		mutex_lock(&wl->mutex);
851		wl1271_debug(DEBUG_TX, "flushing tx buffer: %d %d",
852			     wl->tx_frames_cnt, wl->tx_queue_count);
853		if ((wl->tx_frames_cnt == 0) && (wl->tx_queue_count == 0)) {
854			mutex_unlock(&wl->mutex);
855			return;
856		}
857		mutex_unlock(&wl->mutex);
858		msleep(1);
859	}
860
861	wl1271_warning("Unable to flush all TX buffers, timed out.");
862}
863
864u32 wl1271_tx_min_rate_get(struct wl1271 *wl)
865{
866	int i;
867	u32 rate = 0;
868
869	if (!wl->basic_rate_set) {
870		WARN_ON(1);
871		wl->basic_rate_set = wl->conf.tx.basic_rate;
872	}
873
874	for (i = 0; !rate; i++) {
875		if ((wl->basic_rate_set >> i) & 0x1)
876			rate = 1 << i;
877	}
878
879	return rate;
880}
881