Merge branch 'for_linus' into for_linus_merged
[cascardo/linux.git] / drivers / net / wireless / wl12xx / tx.c
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 "debug.h"
30 #include "io.h"
31 #include "reg.h"
32 #include "ps.h"
33 #include "tx.h"
34 #include "event.h"
35
36 static int wl1271_set_default_wep_key(struct wl1271 *wl,
37                                       struct wl12xx_vif *wlvif, u8 id)
38 {
39         int ret;
40         bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
41
42         if (is_ap)
43                 ret = wl12xx_cmd_set_default_wep_key(wl, id,
44                                                      wlvif->ap.bcast_hlid);
45         else
46                 ret = wl12xx_cmd_set_default_wep_key(wl, id, wlvif->sta.hlid);
47
48         if (ret < 0)
49                 return ret;
50
51         wl1271_debug(DEBUG_CRYPT, "default wep key idx: %d", (int)id);
52         return 0;
53 }
54
55 static int wl1271_alloc_tx_id(struct wl1271 *wl, struct sk_buff *skb)
56 {
57         int id;
58
59         id = find_first_zero_bit(wl->tx_frames_map, ACX_TX_DESCRIPTORS);
60         if (id >= ACX_TX_DESCRIPTORS)
61                 return -EBUSY;
62
63         __set_bit(id, wl->tx_frames_map);
64         wl->tx_frames[id] = skb;
65         wl->tx_frames_cnt++;
66         return id;
67 }
68
69 static void wl1271_free_tx_id(struct wl1271 *wl, int id)
70 {
71         if (__test_and_clear_bit(id, wl->tx_frames_map)) {
72                 if (unlikely(wl->tx_frames_cnt == ACX_TX_DESCRIPTORS))
73                         clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
74
75                 wl->tx_frames[id] = NULL;
76                 wl->tx_frames_cnt--;
77         }
78 }
79
80 static int wl1271_tx_update_filters(struct wl1271 *wl,
81                                     struct wl12xx_vif *wlvif,
82                                     struct sk_buff *skb)
83 {
84         struct ieee80211_hdr *hdr;
85         int ret;
86
87         hdr = (struct ieee80211_hdr *)skb->data;
88
89         /*
90          * stop bssid-based filtering before transmitting authentication
91          * requests. this way the hw will never drop authentication
92          * responses coming from BSSIDs it isn't familiar with (e.g. on
93          * roaming)
94          */
95         if (!ieee80211_is_auth(hdr->frame_control))
96                 return 0;
97
98         if (wlvif->dev_hlid != WL12XX_INVALID_LINK_ID)
99                 goto out;
100
101         wl1271_debug(DEBUG_CMD, "starting device role for roaming");
102         ret = wl12xx_start_dev(wl, wlvif);
103         if (ret < 0)
104                 goto out;
105 out:
106         return 0;
107 }
108
109 static void wl1271_tx_ap_update_inconnection_sta(struct wl1271 *wl,
110                                                  struct sk_buff *skb)
111 {
112         struct ieee80211_hdr *hdr;
113
114         /*
115          * add the station to the known list before transmitting the
116          * authentication response. this way it won't get de-authed by FW
117          * when transmitting too soon.
118          */
119         hdr = (struct ieee80211_hdr *)(skb->data +
120                                        sizeof(struct wl1271_tx_hw_descr));
121         if (ieee80211_is_auth(hdr->frame_control))
122                 wl1271_acx_set_inconnection_sta(wl, hdr->addr1);
123 }
124
125 static void wl1271_tx_regulate_link(struct wl1271 *wl,
126                                     struct wl12xx_vif *wlvif,
127                                     u8 hlid)
128 {
129         bool fw_ps, single_sta;
130         u8 tx_pkts;
131
132         if (WARN_ON(!test_bit(hlid, wlvif->links_map)))
133                 return;
134
135         fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
136         tx_pkts = wl->links[hlid].allocated_pkts;
137         single_sta = (wl->active_sta_count == 1);
138
139         /*
140          * if in FW PS and there is enough data in FW we can put the link
141          * into high-level PS and clean out its TX queues.
142          * Make an exception if this is the only connected station. In this
143          * case FW-memory congestion is not a problem.
144          */
145         if (!single_sta && fw_ps && tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
146                 wl12xx_ps_link_start(wl, wlvif, hlid, true);
147 }
148
149 bool wl12xx_is_dummy_packet(struct wl1271 *wl, struct sk_buff *skb)
150 {
151         return wl->dummy_packet == skb;
152 }
153
154 u8 wl12xx_tx_get_hlid_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif,
155                          struct sk_buff *skb)
156 {
157         struct ieee80211_tx_info *control = IEEE80211_SKB_CB(skb);
158
159         if (control->control.sta) {
160                 struct wl1271_station *wl_sta;
161
162                 wl_sta = (struct wl1271_station *)
163                                 control->control.sta->drv_priv;
164                 return wl_sta->hlid;
165         } else {
166                 struct ieee80211_hdr *hdr;
167
168                 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
169                         return wl->system_hlid;
170
171                 hdr = (struct ieee80211_hdr *)skb->data;
172                 if (ieee80211_is_mgmt(hdr->frame_control))
173                         return wlvif->ap.global_hlid;
174                 else
175                         return wlvif->ap.bcast_hlid;
176         }
177 }
178
179 u8 wl12xx_tx_get_hlid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
180                       struct sk_buff *skb)
181 {
182         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
183
184         if (!wlvif || wl12xx_is_dummy_packet(wl, skb))
185                 return wl->system_hlid;
186
187         if (wlvif->bss_type == BSS_TYPE_AP_BSS)
188                 return wl12xx_tx_get_hlid_ap(wl, wlvif, skb);
189
190         wl1271_tx_update_filters(wl, wlvif, skb);
191
192         if ((test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
193              test_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags)) &&
194             !ieee80211_is_auth(hdr->frame_control) &&
195             !ieee80211_is_assoc_req(hdr->frame_control))
196                 return wlvif->sta.hlid;
197         else
198                 return wlvif->dev_hlid;
199 }
200
201 static unsigned int wl12xx_calc_packet_alignment(struct wl1271 *wl,
202                                                 unsigned int packet_length)
203 {
204         if (wl->quirks & WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT)
205                 return ALIGN(packet_length, WL1271_TX_ALIGN_TO);
206         else
207                 return ALIGN(packet_length, WL12XX_BUS_BLOCK_SIZE);
208 }
209
210 static int wl1271_tx_allocate(struct wl1271 *wl, struct wl12xx_vif *wlvif,
211                               struct sk_buff *skb, u32 extra, u32 buf_offset,
212                               u8 hlid)
213 {
214         struct wl1271_tx_hw_descr *desc;
215         u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
216         u32 len;
217         u32 total_blocks;
218         int id, ret = -EBUSY, ac;
219         u32 spare_blocks = wl->tx_spare_blocks;
220         bool is_dummy = false;
221
222         if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
223                 return -EAGAIN;
224
225         /* allocate free identifier for the packet */
226         id = wl1271_alloc_tx_id(wl, skb);
227         if (id < 0)
228                 return id;
229
230         /* approximate the number of blocks required for this packet
231            in the firmware */
232         len = wl12xx_calc_packet_alignment(wl, total_len);
233
234         /* in case of a dummy packet, use default amount of spare mem blocks */
235         if (unlikely(wl12xx_is_dummy_packet(wl, skb))) {
236                 is_dummy = true;
237                 spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
238         }
239
240         total_blocks = (len + TX_HW_BLOCK_SIZE - 1) / TX_HW_BLOCK_SIZE +
241                 spare_blocks;
242
243         if (total_blocks <= wl->tx_blocks_available) {
244                 desc = (struct wl1271_tx_hw_descr *)skb_push(
245                         skb, total_len - skb->len);
246
247                 /* HW descriptor fields change between wl127x and wl128x */
248                 if (wl->chip.id == CHIP_ID_1283_PG20) {
249                         desc->wl128x_mem.total_mem_blocks = total_blocks;
250                 } else {
251                         desc->wl127x_mem.extra_blocks = spare_blocks;
252                         desc->wl127x_mem.total_mem_blocks = total_blocks;
253                 }
254
255                 desc->id = id;
256
257                 wl->tx_blocks_available -= total_blocks;
258                 wl->tx_allocated_blocks += total_blocks;
259
260                 ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
261                 wl->tx_allocated_pkts[ac]++;
262
263                 if (!is_dummy && wlvif &&
264                     wlvif->bss_type == BSS_TYPE_AP_BSS &&
265                     test_bit(hlid, wlvif->ap.sta_hlid_map))
266                         wl->links[hlid].allocated_pkts++;
267
268                 ret = 0;
269
270                 wl1271_debug(DEBUG_TX,
271                              "tx_allocate: size: %d, blocks: %d, id: %d",
272                              total_len, total_blocks, id);
273         } else {
274                 wl1271_free_tx_id(wl, id);
275         }
276
277         return ret;
278 }
279
280 static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct wl12xx_vif *wlvif,
281                                struct sk_buff *skb, u32 extra,
282                                struct ieee80211_tx_info *control, u8 hlid)
283 {
284         struct timespec ts;
285         struct wl1271_tx_hw_descr *desc;
286         int aligned_len, ac, rate_idx;
287         s64 hosttime;
288         u16 tx_attr = 0;
289         bool is_dummy;
290
291         desc = (struct wl1271_tx_hw_descr *) skb->data;
292
293         /* relocate space for security header */
294         if (extra) {
295                 void *framestart = skb->data + sizeof(*desc);
296                 u16 fc = *(u16 *)(framestart + extra);
297                 int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
298                 memmove(framestart, framestart + extra, hdrlen);
299         }
300
301         /* configure packet life time */
302         getnstimeofday(&ts);
303         hosttime = (timespec_to_ns(&ts) >> 10);
304         desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
305
306         is_dummy = wl12xx_is_dummy_packet(wl, skb);
307         if (is_dummy || !wlvif || wlvif->bss_type != BSS_TYPE_AP_BSS)
308                 desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
309         else
310                 desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
311
312         /* queue */
313         ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
314         desc->tid = skb->priority;
315
316         if (is_dummy) {
317                 /*
318                  * FW expects the dummy packet to have an invalid session id -
319                  * any session id that is different than the one set in the join
320                  */
321                 tx_attr = (SESSION_COUNTER_INVALID <<
322                            TX_HW_ATTR_OFST_SESSION_COUNTER) &
323                            TX_HW_ATTR_SESSION_COUNTER;
324
325                 tx_attr |= TX_HW_ATTR_TX_DUMMY_REQ;
326         } else if (wlvif) {
327                 /* configure the tx attributes */
328                 tx_attr = wlvif->session_counter <<
329                           TX_HW_ATTR_OFST_SESSION_COUNTER;
330         }
331
332         desc->hlid = hlid;
333         if (is_dummy || !wlvif)
334                 rate_idx = 0;
335         else if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
336                 /* if the packets are destined for AP (have a STA entry)
337                    send them with AP rate policies, otherwise use default
338                    basic rates */
339                 if (control->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
340                         rate_idx = wlvif->sta.p2p_rate_idx;
341                 else if (control->control.sta)
342                         rate_idx = wlvif->sta.ap_rate_idx;
343                 else
344                         rate_idx = wlvif->sta.basic_rate_idx;
345         } else {
346                 if (hlid == wlvif->ap.global_hlid)
347                         rate_idx = wlvif->ap.mgmt_rate_idx;
348                 else if (hlid == wlvif->ap.bcast_hlid)
349                         rate_idx = wlvif->ap.bcast_rate_idx;
350                 else
351                         rate_idx = wlvif->ap.ucast_rate_idx[ac];
352         }
353
354         tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
355         desc->reserved = 0;
356
357         aligned_len = wl12xx_calc_packet_alignment(wl, skb->len);
358
359         if (wl->chip.id == CHIP_ID_1283_PG20) {
360                 desc->wl128x_mem.extra_bytes = aligned_len - skb->len;
361                 desc->length = cpu_to_le16(aligned_len >> 2);
362
363                 wl1271_debug(DEBUG_TX, "tx_fill_hdr: hlid: %d "
364                              "tx_attr: 0x%x len: %d life: %d mem: %d",
365                              desc->hlid, tx_attr,
366                              le16_to_cpu(desc->length),
367                              le16_to_cpu(desc->life_time),
368                              desc->wl128x_mem.total_mem_blocks);
369         } else {
370                 int pad;
371
372                 /* Store the aligned length in terms of words */
373                 desc->length = cpu_to_le16(aligned_len >> 2);
374
375                 /* calculate number of padding bytes */
376                 pad = aligned_len - skb->len;
377                 tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
378
379                 wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d hlid: %d "
380                              "tx_attr: 0x%x len: %d life: %d mem: %d", pad,
381                              desc->hlid, tx_attr,
382                              le16_to_cpu(desc->length),
383                              le16_to_cpu(desc->life_time),
384                              desc->wl127x_mem.total_mem_blocks);
385         }
386
387         desc->tx_attr = cpu_to_le16(tx_attr);
388 }
389
390 /* caller must hold wl->mutex */
391 static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct wl12xx_vif *wlvif,
392                                    struct sk_buff *skb, u32 buf_offset)
393 {
394         struct ieee80211_tx_info *info;
395         u32 extra = 0;
396         int ret = 0;
397         u32 total_len;
398         u8 hlid;
399         bool is_dummy;
400
401         if (!skb)
402                 return -EINVAL;
403
404         info = IEEE80211_SKB_CB(skb);
405
406         /* TODO: handle dummy packets on multi-vifs */
407         is_dummy = wl12xx_is_dummy_packet(wl, skb);
408
409         if (info->control.hw_key &&
410             info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
411                 extra = WL1271_TKIP_IV_SPACE;
412
413         if (info->control.hw_key) {
414                 bool is_wep;
415                 u8 idx = info->control.hw_key->hw_key_idx;
416                 u32 cipher = info->control.hw_key->cipher;
417
418                 is_wep = (cipher == WLAN_CIPHER_SUITE_WEP40) ||
419                          (cipher == WLAN_CIPHER_SUITE_WEP104);
420
421                 if (unlikely(is_wep && wlvif->default_key != idx)) {
422                         ret = wl1271_set_default_wep_key(wl, wlvif, idx);
423                         if (ret < 0)
424                                 return ret;
425                         wlvif->default_key = idx;
426                 }
427         }
428         hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
429         if (hlid == WL12XX_INVALID_LINK_ID) {
430                 wl1271_error("invalid hlid. dropping skb 0x%p", skb);
431                 return -EINVAL;
432         }
433
434         ret = wl1271_tx_allocate(wl, wlvif, skb, extra, buf_offset, hlid);
435         if (ret < 0)
436                 return ret;
437
438         wl1271_tx_fill_hdr(wl, wlvif, skb, extra, info, hlid);
439
440         if (!is_dummy && wlvif && wlvif->bss_type == BSS_TYPE_AP_BSS) {
441                 wl1271_tx_ap_update_inconnection_sta(wl, skb);
442                 wl1271_tx_regulate_link(wl, wlvif, hlid);
443         }
444
445         /*
446          * The length of each packet is stored in terms of
447          * words. Thus, we must pad the skb data to make sure its
448          * length is aligned.  The number of padding bytes is computed
449          * and set in wl1271_tx_fill_hdr.
450          * In special cases, we want to align to a specific block size
451          * (eg. for wl128x with SDIO we align to 256).
452          */
453         total_len = wl12xx_calc_packet_alignment(wl, skb->len);
454
455         memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
456         memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
457
458         /* Revert side effects in the dummy packet skb, so it can be reused */
459         if (is_dummy)
460                 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
461
462         return total_len;
463 }
464
465 u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set,
466                                 enum ieee80211_band rate_band)
467 {
468         struct ieee80211_supported_band *band;
469         u32 enabled_rates = 0;
470         int bit;
471
472         band = wl->hw->wiphy->bands[rate_band];
473         for (bit = 0; bit < band->n_bitrates; bit++) {
474                 if (rate_set & 0x1)
475                         enabled_rates |= band->bitrates[bit].hw_value;
476                 rate_set >>= 1;
477         }
478
479         /* MCS rates indication are on bits 16 - 23 */
480         rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
481
482         for (bit = 0; bit < 8; bit++) {
483                 if (rate_set & 0x1)
484                         enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
485                 rate_set >>= 1;
486         }
487
488         return enabled_rates;
489 }
490
491 void wl1271_handle_tx_low_watermark(struct wl1271 *wl)
492 {
493         unsigned long flags;
494         int i;
495
496         for (i = 0; i < NUM_TX_QUEUES; i++) {
497                 if (test_bit(i, &wl->stopped_queues_map) &&
498                     wl->tx_queue_count[i] <= WL1271_TX_QUEUE_LOW_WATERMARK) {
499                         /* firmware buffer has space, restart queues */
500                         spin_lock_irqsave(&wl->wl_lock, flags);
501                         ieee80211_wake_queue(wl->hw,
502                                              wl1271_tx_get_mac80211_queue(i));
503                         clear_bit(i, &wl->stopped_queues_map);
504                         spin_unlock_irqrestore(&wl->wl_lock, flags);
505                 }
506         }
507 }
508
509 static struct sk_buff_head *wl1271_select_queue(struct wl1271 *wl,
510                                                 struct sk_buff_head *queues)
511 {
512         int i, q = -1, ac;
513         u32 min_pkts = 0xffffffff;
514
515         /*
516          * Find a non-empty ac where:
517          * 1. There are packets to transmit
518          * 2. The FW has the least allocated blocks
519          *
520          * We prioritize the ACs according to VO>VI>BE>BK
521          */
522         for (i = 0; i < NUM_TX_QUEUES; i++) {
523                 ac = wl1271_tx_get_queue(i);
524                 if (!skb_queue_empty(&queues[ac]) &&
525                     (wl->tx_allocated_pkts[ac] < min_pkts)) {
526                         q = ac;
527                         min_pkts = wl->tx_allocated_pkts[q];
528                 }
529         }
530
531         if (q == -1)
532                 return NULL;
533
534         return &queues[q];
535 }
536
537 static struct sk_buff *wl12xx_lnk_skb_dequeue(struct wl1271 *wl,
538                                               struct wl1271_link *lnk)
539 {
540         struct sk_buff *skb;
541         unsigned long flags;
542         struct sk_buff_head *queue;
543
544         queue = wl1271_select_queue(wl, lnk->tx_queue);
545         if (!queue)
546                 return NULL;
547
548         skb = skb_dequeue(queue);
549         if (skb) {
550                 int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
551                 spin_lock_irqsave(&wl->wl_lock, flags);
552                 wl->tx_queue_count[q]--;
553                 spin_unlock_irqrestore(&wl->wl_lock, flags);
554         }
555
556         return skb;
557 }
558
559 static struct sk_buff *wl12xx_vif_skb_dequeue(struct wl1271 *wl,
560                                               struct wl12xx_vif *wlvif)
561 {
562         struct sk_buff *skb = NULL;
563         int i, h, start_hlid;
564
565         /* start from the link after the last one */
566         start_hlid = (wlvif->last_tx_hlid + 1) % WL12XX_MAX_LINKS;
567
568         /* dequeue according to AC, round robin on each link */
569         for (i = 0; i < WL12XX_MAX_LINKS; i++) {
570                 h = (start_hlid + i) % WL12XX_MAX_LINKS;
571
572                 /* only consider connected stations */
573                 if (!test_bit(h, wlvif->links_map))
574                         continue;
575
576                 skb = wl12xx_lnk_skb_dequeue(wl, &wl->links[h]);
577                 if (!skb)
578                         continue;
579
580                 wlvif->last_tx_hlid = h;
581                 break;
582         }
583
584         if (!skb)
585                 wlvif->last_tx_hlid = 0;
586
587         return skb;
588 }
589
590 static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
591 {
592         unsigned long flags;
593         struct wl12xx_vif *wlvif = wl->last_wlvif;
594         struct sk_buff *skb = NULL;
595
596         if (wlvif) {
597                 wl12xx_for_each_wlvif_continue(wl, wlvif) {
598                         skb = wl12xx_vif_skb_dequeue(wl, wlvif);
599                         if (skb) {
600                                 wl->last_wlvif = wlvif;
601                                 break;
602                         }
603                 }
604         }
605
606         /* do another pass */
607         if (!skb) {
608                 wl12xx_for_each_wlvif(wl, wlvif) {
609                         skb = wl12xx_vif_skb_dequeue(wl, wlvif);
610                         if (skb) {
611                                 wl->last_wlvif = wlvif;
612                                 break;
613                         }
614                 }
615         }
616
617         if (!skb)
618                 skb = wl12xx_lnk_skb_dequeue(wl, &wl->links[wl->system_hlid]);
619
620         if (!skb &&
621             test_and_clear_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags)) {
622                 int q;
623
624                 skb = wl->dummy_packet;
625                 q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
626                 spin_lock_irqsave(&wl->wl_lock, flags);
627                 wl->tx_queue_count[q]--;
628                 spin_unlock_irqrestore(&wl->wl_lock, flags);
629         }
630
631         return skb;
632 }
633
634 static void wl1271_skb_queue_head(struct wl1271 *wl, struct wl12xx_vif *wlvif,
635                                   struct sk_buff *skb)
636 {
637         unsigned long flags;
638         int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
639
640         if (wl12xx_is_dummy_packet(wl, skb)) {
641                 set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
642         } else {
643                 u8 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
644                 skb_queue_head(&wl->links[hlid].tx_queue[q], skb);
645
646                 /* make sure we dequeue the same packet next time */
647                 wlvif->last_tx_hlid = (hlid + WL12XX_MAX_LINKS - 1) %
648                                       WL12XX_MAX_LINKS;
649         }
650
651         spin_lock_irqsave(&wl->wl_lock, flags);
652         wl->tx_queue_count[q]++;
653         spin_unlock_irqrestore(&wl->wl_lock, flags);
654 }
655
656 static bool wl1271_tx_is_data_present(struct sk_buff *skb)
657 {
658         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
659
660         return ieee80211_is_data_present(hdr->frame_control);
661 }
662
663 void wl12xx_rearm_rx_streaming(struct wl1271 *wl, unsigned long *active_hlids)
664 {
665         struct wl12xx_vif *wlvif;
666         u32 timeout;
667         u8 hlid;
668
669         if (!wl->conf.rx_streaming.interval)
670                 return;
671
672         if (!wl->conf.rx_streaming.always &&
673             !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags))
674                 return;
675
676         timeout = wl->conf.rx_streaming.duration;
677         wl12xx_for_each_wlvif_sta(wl, wlvif) {
678                 bool found = false;
679                 for_each_set_bit(hlid, active_hlids, WL12XX_MAX_LINKS) {
680                         if (test_bit(hlid, wlvif->links_map)) {
681                                 found  = true;
682                                 break;
683                         }
684                 }
685
686                 if (!found)
687                         continue;
688
689                 /* enable rx streaming */
690                 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
691                         ieee80211_queue_work(wl->hw,
692                                              &wlvif->rx_streaming_enable_work);
693
694                 mod_timer(&wlvif->rx_streaming_timer,
695                           jiffies + msecs_to_jiffies(timeout));
696         }
697 }
698
699 void wl1271_tx_work_locked(struct wl1271 *wl)
700 {
701         struct wl12xx_vif *wlvif;
702         struct sk_buff *skb;
703         struct wl1271_tx_hw_descr *desc;
704         u32 buf_offset = 0;
705         bool sent_packets = false;
706         unsigned long active_hlids[BITS_TO_LONGS(WL12XX_MAX_LINKS)] = {0};
707         int ret;
708
709         if (unlikely(wl->state == WL1271_STATE_OFF))
710                 return;
711
712         while ((skb = wl1271_skb_dequeue(wl))) {
713                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
714                 bool has_data = false;
715
716                 wlvif = NULL;
717                 if (!wl12xx_is_dummy_packet(wl, skb) && info->control.vif)
718                         wlvif = wl12xx_vif_to_data(info->control.vif);
719
720                 has_data = wlvif && wl1271_tx_is_data_present(skb);
721                 ret = wl1271_prepare_tx_frame(wl, wlvif, skb, buf_offset);
722                 if (ret == -EAGAIN) {
723                         /*
724                          * Aggregation buffer is full.
725                          * Flush buffer and try again.
726                          */
727                         wl1271_skb_queue_head(wl, wlvif, skb);
728                         wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
729                                      buf_offset, true);
730                         sent_packets = true;
731                         buf_offset = 0;
732                         continue;
733                 } else if (ret == -EBUSY) {
734                         /*
735                          * Firmware buffer is full.
736                          * Queue back last skb, and stop aggregating.
737                          */
738                         wl1271_skb_queue_head(wl, wlvif, skb);
739                         /* No work left, avoid scheduling redundant tx work */
740                         set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
741                         goto out_ack;
742                 } else if (ret < 0) {
743                         if (wl12xx_is_dummy_packet(wl, skb))
744                                 /*
745                                  * fw still expects dummy packet,
746                                  * so re-enqueue it
747                                  */
748                                 wl1271_skb_queue_head(wl, wlvif, skb);
749                         else
750                                 ieee80211_free_txskb(wl->hw, skb);
751                         goto out_ack;
752                 }
753                 buf_offset += ret;
754                 wl->tx_packets_count++;
755                 if (has_data) {
756                         desc = (struct wl1271_tx_hw_descr *) skb->data;
757                         __set_bit(desc->hlid, active_hlids);
758                 }
759         }
760
761 out_ack:
762         if (buf_offset) {
763                 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
764                                 buf_offset, true);
765                 sent_packets = true;
766         }
767         if (sent_packets) {
768                 /*
769                  * Interrupt the firmware with the new packets. This is only
770                  * required for older hardware revisions
771                  */
772                 if (wl->quirks & WL12XX_QUIRK_END_OF_TRANSACTION)
773                         wl1271_write32(wl, WL1271_HOST_WR_ACCESS,
774                                        wl->tx_packets_count);
775
776                 wl1271_handle_tx_low_watermark(wl);
777         }
778         wl12xx_rearm_rx_streaming(wl, active_hlids);
779 }
780
781 void wl1271_tx_work(struct work_struct *work)
782 {
783         struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
784         int ret;
785
786         mutex_lock(&wl->mutex);
787         ret = wl1271_ps_elp_wakeup(wl);
788         if (ret < 0)
789                 goto out;
790
791         wl1271_tx_work_locked(wl);
792
793         wl1271_ps_elp_sleep(wl);
794 out:
795         mutex_unlock(&wl->mutex);
796 }
797
798 static void wl1271_tx_complete_packet(struct wl1271 *wl,
799                                       struct wl1271_tx_hw_res_descr *result)
800 {
801         struct ieee80211_tx_info *info;
802         struct ieee80211_vif *vif;
803         struct wl12xx_vif *wlvif;
804         struct sk_buff *skb;
805         int id = result->id;
806         int rate = -1;
807         u8 retries = 0;
808
809         /* check for id legality */
810         if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
811                 wl1271_warning("TX result illegal id: %d", id);
812                 return;
813         }
814
815         skb = wl->tx_frames[id];
816         info = IEEE80211_SKB_CB(skb);
817
818         if (wl12xx_is_dummy_packet(wl, skb)) {
819                 wl1271_free_tx_id(wl, id);
820                 return;
821         }
822
823         /* info->control is valid as long as we don't update info->status */
824         vif = info->control.vif;
825         wlvif = wl12xx_vif_to_data(vif);
826
827         /* update the TX status info */
828         if (result->status == TX_SUCCESS) {
829                 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
830                         info->flags |= IEEE80211_TX_STAT_ACK;
831                 rate = wl1271_rate_to_idx(result->rate_class_index,
832                                           wlvif->band);
833                 retries = result->ack_failures;
834         } else if (result->status == TX_RETRY_EXCEEDED) {
835                 wl->stats.excessive_retries++;
836                 retries = result->ack_failures;
837         }
838
839         info->status.rates[0].idx = rate;
840         info->status.rates[0].count = retries;
841         info->status.rates[0].flags = 0;
842         info->status.ack_signal = -1;
843
844         wl->stats.retry_count += result->ack_failures;
845
846         /*
847          * update sequence number only when relevant, i.e. only in
848          * sessions of TKIP, AES and GEM (not in open or WEP sessions)
849          */
850         if (info->control.hw_key &&
851             (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP ||
852              info->control.hw_key->cipher == WLAN_CIPHER_SUITE_CCMP ||
853              info->control.hw_key->cipher == WL1271_CIPHER_SUITE_GEM)) {
854                 u8 fw_lsb = result->tx_security_sequence_number_lsb;
855                 u8 cur_lsb = wlvif->tx_security_last_seq_lsb;
856
857                 /*
858                  * update security sequence number, taking care of potential
859                  * wrap-around
860                  */
861                 wlvif->tx_security_seq += (fw_lsb - cur_lsb) & 0xff;
862                 wlvif->tx_security_last_seq_lsb = fw_lsb;
863         }
864
865         /* remove private header from packet */
866         skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
867
868         /* remove TKIP header space if present */
869         if (info->control.hw_key &&
870             info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
871                 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
872                 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
873                 skb_pull(skb, WL1271_TKIP_IV_SPACE);
874         }
875
876         wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
877                      " status 0x%x",
878                      result->id, skb, result->ack_failures,
879                      result->rate_class_index, result->status);
880
881         /* return the packet to the stack */
882         skb_queue_tail(&wl->deferred_tx_queue, skb);
883         queue_work(wl->freezable_wq, &wl->netstack_work);
884         wl1271_free_tx_id(wl, result->id);
885 }
886
887 /* Called upon reception of a TX complete interrupt */
888 void wl1271_tx_complete(struct wl1271 *wl)
889 {
890         struct wl1271_acx_mem_map *memmap =
891                 (struct wl1271_acx_mem_map *)wl->target_mem_map;
892         u32 count, fw_counter;
893         u32 i;
894
895         /* read the tx results from the chipset */
896         wl1271_read(wl, le32_to_cpu(memmap->tx_result),
897                     wl->tx_res_if, sizeof(*wl->tx_res_if), false);
898         fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
899
900         /* write host counter to chipset (to ack) */
901         wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
902                        offsetof(struct wl1271_tx_hw_res_if,
903                                 tx_result_host_counter), fw_counter);
904
905         count = fw_counter - wl->tx_results_count;
906         wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
907
908         /* verify that the result buffer is not getting overrun */
909         if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
910                 wl1271_warning("TX result overflow from chipset: %d", count);
911
912         /* process the results */
913         for (i = 0; i < count; i++) {
914                 struct wl1271_tx_hw_res_descr *result;
915                 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
916
917                 /* process the packet */
918                 result =  &(wl->tx_res_if->tx_results_queue[offset]);
919                 wl1271_tx_complete_packet(wl, result);
920
921                 wl->tx_results_count++;
922         }
923 }
924
925 void wl1271_tx_reset_link_queues(struct wl1271 *wl, u8 hlid)
926 {
927         struct sk_buff *skb;
928         int i;
929         unsigned long flags;
930         struct ieee80211_tx_info *info;
931         int total[NUM_TX_QUEUES];
932
933         for (i = 0; i < NUM_TX_QUEUES; i++) {
934                 total[i] = 0;
935                 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
936                         wl1271_debug(DEBUG_TX, "link freeing skb 0x%p", skb);
937
938                         if (!wl12xx_is_dummy_packet(wl, skb)) {
939                                 info = IEEE80211_SKB_CB(skb);
940                                 info->status.rates[0].idx = -1;
941                                 info->status.rates[0].count = 0;
942                                 ieee80211_tx_status_ni(wl->hw, skb);
943                         }
944
945                         total[i]++;
946                 }
947         }
948
949         spin_lock_irqsave(&wl->wl_lock, flags);
950         for (i = 0; i < NUM_TX_QUEUES; i++)
951                 wl->tx_queue_count[i] -= total[i];
952         spin_unlock_irqrestore(&wl->wl_lock, flags);
953
954         wl1271_handle_tx_low_watermark(wl);
955 }
956
957 /* caller must hold wl->mutex and TX must be stopped */
958 void wl12xx_tx_reset_wlvif(struct wl1271 *wl, struct wl12xx_vif *wlvif)
959 {
960         int i;
961
962         /* TX failure */
963         for_each_set_bit(i, wlvif->links_map, WL12XX_MAX_LINKS) {
964                 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
965                         wl1271_free_sta(wl, wlvif, i);
966                 else
967                         wlvif->sta.ba_rx_bitmap = 0;
968
969                 wl1271_tx_reset_link_queues(wl, i);
970                 wl->links[i].allocated_pkts = 0;
971                 wl->links[i].prev_freed_pkts = 0;
972         }
973         wlvif->last_tx_hlid = 0;
974
975 }
976 /* caller must hold wl->mutex and TX must be stopped */
977 void wl12xx_tx_reset(struct wl1271 *wl, bool reset_tx_queues)
978 {
979         int i;
980         struct sk_buff *skb;
981         struct ieee80211_tx_info *info;
982
983         for (i = 0; i < NUM_TX_QUEUES; i++)
984                 wl->tx_queue_count[i] = 0;
985
986         wl->stopped_queues_map = 0;
987
988         /*
989          * Make sure the driver is at a consistent state, in case this
990          * function is called from a context other than interface removal.
991          * This call will always wake the TX queues.
992          */
993         if (reset_tx_queues)
994                 wl1271_handle_tx_low_watermark(wl);
995
996         for (i = 0; i < ACX_TX_DESCRIPTORS; i++) {
997                 if (wl->tx_frames[i] == NULL)
998                         continue;
999
1000                 skb = wl->tx_frames[i];
1001                 wl1271_free_tx_id(wl, i);
1002                 wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
1003
1004                 if (!wl12xx_is_dummy_packet(wl, skb)) {
1005                         /*
1006                          * Remove private headers before passing the skb to
1007                          * mac80211
1008                          */
1009                         info = IEEE80211_SKB_CB(skb);
1010                         skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
1011                         if (info->control.hw_key &&
1012                             info->control.hw_key->cipher ==
1013                             WLAN_CIPHER_SUITE_TKIP) {
1014                                 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1015                                 memmove(skb->data + WL1271_TKIP_IV_SPACE,
1016                                         skb->data, hdrlen);
1017                                 skb_pull(skb, WL1271_TKIP_IV_SPACE);
1018                         }
1019
1020                         info->status.rates[0].idx = -1;
1021                         info->status.rates[0].count = 0;
1022
1023                         ieee80211_tx_status_ni(wl->hw, skb);
1024                 }
1025         }
1026 }
1027
1028 #define WL1271_TX_FLUSH_TIMEOUT 500000
1029
1030 /* caller must *NOT* hold wl->mutex */
1031 void wl1271_tx_flush(struct wl1271 *wl)
1032 {
1033         unsigned long timeout;
1034         timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
1035
1036         while (!time_after(jiffies, timeout)) {
1037                 mutex_lock(&wl->mutex);
1038                 wl1271_debug(DEBUG_TX, "flushing tx buffer: %d %d",
1039                              wl->tx_frames_cnt,
1040                              wl1271_tx_total_queue_count(wl));
1041                 if ((wl->tx_frames_cnt == 0) &&
1042                     (wl1271_tx_total_queue_count(wl) == 0)) {
1043                         mutex_unlock(&wl->mutex);
1044                         return;
1045                 }
1046                 mutex_unlock(&wl->mutex);
1047                 msleep(1);
1048         }
1049
1050         wl1271_warning("Unable to flush all TX buffers, timed out.");
1051 }
1052
1053 u32 wl1271_tx_min_rate_get(struct wl1271 *wl, u32 rate_set)
1054 {
1055         if (WARN_ON(!rate_set))
1056                 return 0;
1057
1058         return BIT(__ffs(rate_set));
1059 }