kconfig: simplify symbol-search code
[carl9170fw.git] / carlfw / src / wlantx.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * WLAN transmit and tx status
5  *
6  * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7  * Copyright (c) 2007-2009 Atheros Communications, Inc.
8  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
9  * Copyright 2009-2012  Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "carl9170.h"
27 #include "shared/phy.h"
28 #include "hostif.h"
29 #include "timer.h"
30 #include "wl.h"
31 #include "printf.h"
32 #include "rf.h"
33 #include "linux/ieee80211.h"
34 #include "wol.h"
35
36 static void wlan_txunstuck(unsigned int qidx)
37 {
38         struct dma_queue *queue = &fw.wlan.tx_queue[qidx];
39         struct dma_desc *iter;
40
41         /*
42          * walk up to the last descriptor which hasn't been
43          * processed by the hardware before it bailed out
44          * due to a TX error.
45          * Note: if there was no more "pending" frame
46          * in the queue, it iter will be on the
47          *    queue->terminator (which is fine)
48          */
49         __for_each_desc_bits(iter, queue, AR9170_OWN_BITS_SW);
50
51         set_wlan_txq_dma_addr(qidx, ((uint32_t) iter) | 1);
52         wlan_trigger(BIT(qidx));
53 }
54
55 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
56 static void wlan_txupdate(unsigned int qidx)
57 {
58         struct dma_queue *queue = &fw.wlan.tx_queue[qidx];
59         struct dma_desc *iter;
60         /* comment in wlan_txunstuck applies here too. */
61         __for_each_desc_bits(iter, queue, AR9170_OWN_BITS_SW);
62
63         set_wlan_txq_dma_addr(qidx, ((uint32_t) iter));
64         wlan_trigger(BIT(qidx));
65 }
66
67 void wlan_dma_bump(unsigned int qidx)
68 {
69         unsigned int offset = qidx;
70         uint32_t status, trigger;
71
72         status = get(AR9170_MAC_REG_DMA_STATUS) >> 12;
73         trigger = get(AR9170_MAC_REG_DMA_TRIGGER) >> 12;
74
75         while (offset != 0) {
76                 status >>= 4;
77                 trigger >>= 4;
78                 offset--;
79         }
80
81         status &= 0xf;
82         trigger &= 0xf;
83
84         if ((trigger == 0xa) && (status == 0x8)) {
85                 DBG("UNSTUCK");
86                 wlan_txunstuck(qidx);
87         } else {
88                 DBG("UPDATE");
89                 wlan_txupdate(qidx);
90         }
91 }
92 #else
93 void wlan_dma_bump(unsigned int __unused qidx)
94 {
95 }
96 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
97
98 void wlan_send_buffered_tx_status(void)
99 {
100         unsigned int len;
101
102         while (fw.wlan.tx_status_pending) {
103                 len = min((unsigned int)fw.wlan.tx_status_pending,
104                           CARL9170_RSP_TX_STATUS_NUM);
105                 len = min(len, CARL9170_TX_STATUS_NUM - fw.wlan.tx_status_head_idx);
106
107                 /*
108                  * rather than memcpy each individual request into a large buffer,
109                  * we _splice_ them all together.
110                  *
111                  * The only downside is however that we have to be careful around
112                  * the edges of the tx_status_cache.
113                  *
114                  * Note:
115                  * Each tx_status is about 2 bytes. However every command package
116                  * must have a size which is a multiple of 4.
117                  */
118
119                 send_cmd_to_host((len * sizeof(struct carl9170_tx_status) + 3) & ~3,
120                                  CARL9170_RSP_TXCOMP, len, (void *)
121                                  &fw.wlan.tx_status_cache[fw.wlan.tx_status_head_idx]);
122
123                 fw.wlan.tx_status_pending -= len;
124                 fw.wlan.tx_status_head_idx += len;
125                 fw.wlan.tx_status_head_idx %= CARL9170_TX_STATUS_NUM;
126         }
127 }
128
129 static struct carl9170_tx_status *wlan_get_tx_status_buffer(void)
130 {
131         struct carl9170_tx_status *tmp;
132
133         tmp = &fw.wlan.tx_status_cache[fw.wlan.tx_status_tail_idx++];
134         fw.wlan.tx_status_tail_idx %= CARL9170_TX_STATUS_NUM;
135
136         if (fw.wlan.tx_status_pending == CARL9170_TX_STATUS_NUM)
137                 wlan_send_buffered_tx_status();
138
139         fw.wlan.tx_status_pending++;
140
141         return tmp;
142 }
143
144 /* generate _aggregated_ tx_status for the host */
145 void wlan_tx_complete(struct carl9170_tx_superframe *super,
146                       bool txs)
147 {
148         struct carl9170_tx_status *status;
149
150         status = wlan_get_tx_status_buffer();
151
152         /*
153          * The *unique* cookie and AC_ID is used by the driver for
154          * frame lookup.
155          */
156         status->cookie = super->s.cookie;
157         status->queue = super->s.queue;
158         super->s.cookie = 0;
159
160         /*
161          * This field holds the number of tries of the rate in
162          * the rate index field (rix).
163          */
164         status->rix = super->s.rix;
165         status->tries = super->s.cnt;
166         status->success = (txs) ? 1 : 0;
167 }
168
169 static bool wlan_tx_consume_retry(struct carl9170_tx_superframe *super)
170 {
171         /* check if this was the last possible retry with this rate */
172         if (unlikely(super->s.cnt >= super->s.ri[super->s.rix].tries)) {
173                 /* end of the road - indicate tx failure */
174                 if (unlikely(super->s.rix == CARL9170_TX_MAX_RETRY_RATES))
175                         return false;
176
177                 /* check if there are alternative rates available */
178                 if (!super->s.rr[super->s.rix].set)
179                         return false;
180
181                 /* try next retry rate */
182                 super->f.hdr.phy.set = super->s.rr[super->s.rix].set;
183
184                 /* finally - mark the old rate as USED */
185                 super->s.rix++;
186
187                 /* update MAC flags */
188                 super->f.hdr.mac.erp_prot = super->s.ri[super->s.rix].erp_prot;
189                 super->f.hdr.mac.ampdu = super->s.ri[super->s.rix].ampdu;
190
191                 /* reinitialize try counter */
192                 super->s.cnt = 1;
193         } else {
194                 /* just increase retry counter */
195                 super->s.cnt++;
196         }
197
198         return true;
199 }
200
201 static inline u16 get_tid(struct ieee80211_hdr *hdr)
202 {
203         return (ieee80211_get_qos_ctl(hdr))[0] & IEEE80211_QOS_CTL_TID_MASK;
204 }
205
206 /* This function will only work on uint32_t-aligned pointers! */
207 static bool same_hdr(const void *_d0, const void *_d1)
208 {
209         const uint32_t *d0 = _d0;
210         const uint32_t *d1 = _d1;
211
212         /* BUG_ON((unsigned long)d0 & 3 || (unsigned long)d1 & 3)) */
213         return !((d0[0] ^ d1[0]) |                      /* FC + DU */
214                  (d0[1] ^ d1[1]) |                      /* addr1 */
215                  (d0[2] ^ d1[2]) | (d0[3] ^ d1[3]) |    /* addr2 + addr3 */
216                  (d0[4] ^ d1[4]));                      /* addr3 */
217 }
218
219 static inline bool same_aggr(struct ieee80211_hdr *a, struct ieee80211_hdr *b)
220 {
221         return (get_tid(a) == get_tid(b)) || same_hdr(a, b);
222 }
223
224 static void wlan_tx_ampdu_reset(unsigned int qidx)
225 {
226         fw.wlan.ampdu_prev[qidx] = NULL;
227 }
228
229 static void wlan_tx_ampdu_end(unsigned int qidx)
230 {
231         struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
232
233         if (ht_prev)
234                 ht_prev->f.hdr.mac.ba_end = 1;
235
236         wlan_tx_ampdu_reset(qidx);
237 }
238
239 static void wlan_tx_ampdu(struct carl9170_tx_superframe *super)
240 {
241         unsigned int qidx = super->s.queue;
242         struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
243
244         if (super->f.hdr.mac.ampdu) {
245                 if (ht_prev &&
246                     !same_aggr(&super->f.data.i3e, &ht_prev->f.data.i3e))
247                         ht_prev->f.hdr.mac.ba_end = 1;
248                 else
249                         super->f.hdr.mac.ba_end = 0;
250
251                 fw.wlan.ampdu_prev[qidx] = super;
252         } else {
253                 wlan_tx_ampdu_end(qidx);
254         }
255 }
256
257 /* for all tries */
258 static void __wlan_tx(struct dma_desc *desc)
259 {
260         struct carl9170_tx_superframe *super = get_super(desc);
261
262         if (unlikely(super->s.fill_in_tsf)) {
263                 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
264                 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
265
266                 /*
267                  * Truth be told: this is a hack.
268                  *
269                  * The *real* TSF is definitely going to be higher/older.
270                  * But this hardware emulation code is head and shoulders
271                  * above anything a driver can possibly do.
272                  *
273                  * (even, if it's got an accurate atomic clock source).
274                  */
275
276                 read_tsf(tsf);
277         }
278
279         wlan_tx_ampdu(super);
280
281 #if (defined CONFIG_CARL9170FW_DEBUG) && (defined CONFIG_CARL9170FW_RADIO_FUNCTIONS)
282         BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
283 #endif /* CONFIG_CARL9170FW_DEBUG && CONFIG_CARL9170FW_RADIO_FUNCTIONS */
284
285         /* insert desc into the right queue */
286         dma_put(&fw.wlan.tx_queue[super->s.queue], desc);
287 }
288
289 static void wlan_assign_seq(struct ieee80211_hdr *hdr, unsigned int vif)
290 {
291         hdr->seq_ctrl &= cpu_to_le16(~IEEE80211_SCTL_SEQ);
292         hdr->seq_ctrl |= cpu_to_le16(fw.wlan.sequence[vif]);
293
294         if (ieee80211_is_first_frag(hdr->seq_ctrl))
295                 fw.wlan.sequence[vif] += 0x10;
296 }
297
298 /* prepares frame for the first transmission */
299 static void _wlan_tx(struct dma_desc *desc)
300 {
301         struct carl9170_tx_superframe *super = get_super(desc);
302
303         if (unlikely(super->s.assign_seq))
304                 wlan_assign_seq(&super->f.data.i3e, super->s.vif_id);
305
306         if (unlikely(super->s.ampdu_commit_density)) {
307                 set(AR9170_MAC_REG_AMPDU_DENSITY,
308                     MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
309                             get(AR9170_MAC_REG_AMPDU_DENSITY),
310                             super->s.ampdu_density));
311         }
312
313         if (unlikely(super->s.ampdu_commit_factor)) {
314                 set(AR9170_MAC_REG_AMPDU_FACTOR,
315                     MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
316                             get(AR9170_MAC_REG_AMPDU_FACTOR),
317                             8 << super->s.ampdu_factor));
318         }
319 }
320
321 /* propagate transmission status back to the driver */
322 static bool wlan_tx_status(struct dma_queue *queue,
323                            struct dma_desc *desc)
324 {
325         struct carl9170_tx_superframe *super = get_super(desc);
326         unsigned int qidx = super->s.queue;
327         bool txfail = false, success;
328
329         success = true;
330
331         /* update hangcheck */
332         fw.wlan.last_super_num[qidx] = 0;
333
334         /*
335          * Note:
336          * There could be a corner case when the TXFAIL is set
337          * even though the frame was properly ACKed by the peer:
338          *   a BlockAckReq with the immediate policy will cause
339          *   the receiving peer to produce a BlockACK unfortunately
340          *   the MAC in this chip seems to be expecting a legacy
341          *   ACK and marks the BAR as failed!
342          */
343
344         if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
345                 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
346
347                 /* reset retry indicator flags */
348                 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
349
350                 /*
351                  * Note: wlan_tx_consume_retry will override the old
352                  * phy [CCK,OFDM, HT, BW20/40, MCS...] and mac vectors
353                  * [AMPDU,RTS/CTS,...] therefore be careful when they
354                  * are used.
355                  */
356                 if (wlan_tx_consume_retry(super)) {
357                         /*
358                          * retry for simple and aggregated 802.11 frames.
359                          *
360                          * Note: We must not mess up the original frame
361                          * order.
362                          */
363
364                         if (!super->f.hdr.mac.ampdu) {
365                                 /*
366                                  * 802.11 - 7.1.3.1.5.
367                                  * set "Retry Field" for consecutive attempts
368                                  *
369                                  * Note: For AMPDU see:
370                                  * 802.11n 9.9.1.6 "Retransmit Procedures"
371                                  */
372                                 super->f.data.i3e.frame_control |=
373                                         cpu_to_le16(IEEE80211_FCTL_RETRY);
374                         }
375
376                         if (txfail) {
377                                 /* Normal TX Failure */
378
379                                 /* demise descriptor ownership back to the hardware */
380                                 dma_rearm(desc);
381
382                                 /*
383                                  * And this will get the queue going again.
384                                  * To understand why: you have to get the HW
385                                  * specs... But sadly I never saw them.
386                                  */
387                                 wlan_txunstuck(qidx);
388
389                                 /* abort cycle - this is necessary due to HW design */
390                                 goto out;
391                         } else {
392                                 /* (HT-) BlockACK failure */
393
394                                 /*
395                                  * Unlink the failed attempt and put it into
396                                  * the retry queue. The caller routine must
397                                  * be aware of this so the frames don't get lost.
398                                  */
399
400 #ifndef CONFIG_CARL9170FW_DEBUG
401                                 dma_unlink_head(queue);
402 #else /* CONFIG_CARL9170FW_DEBUG */
403                                 BUG_ON(dma_unlink_head(queue) != desc);
404 #endif /* CONFIG_CARL9170FW_DEBUG */
405                                 dma_put(&fw.wlan.tx_retry, desc);
406                                 goto out;
407                         }
408                 } else {
409                         /* out of frame attempts - discard frame */
410                         success = false;
411                 }
412         }
413
414 #ifndef CONFIG_CARL9170FW_DEBUG
415         dma_unlink_head(queue);
416 #else /* CONFIG_CARL9170FW_DEBUG */
417         BUG_ON(dma_unlink_head(queue) != desc);
418 #endif /* CONFIG_CARL9170FW_DEBUG */
419         if (txfail) {
420                 /*
421                  * Issue the queue bump,
422                  * We need to do this in case this was the frame's last
423                  * possible retry attempt and it unfortunately: it failed.
424                  */
425
426                 wlan_txunstuck(qidx);
427         }
428
429         unhide_super(desc);
430
431         if (unlikely(super == fw.wlan.fw_desc_data)) {
432                 fw.wlan.fw_desc = desc;
433                 fw.wlan.fw_desc_available = 1;
434
435                 if (fw.wlan.fw_desc_callback)
436                         fw.wlan.fw_desc_callback(super, success);
437
438                 goto out;
439         }
440
441         if (unlikely(super->s.cab))
442                 fw.wlan.cab_queue_len[super->s.vif_id]--;
443
444         wlan_tx_complete(super, success);
445
446         if (ieee80211_is_back_req(super->f.data.i3e.frame_control)) {
447                 fw.wlan.queued_bar--;
448         }
449
450         /* recycle freed descriptors */
451         dma_reclaim(&fw.pta.down_queue, desc);
452         down_trigger();
453 out:
454         /*
455          * if we encounter a frame which run out of (normal)
456          * tx retries we have to stop too.
457          */
458         return !txfail;
459 }
460
461 void handle_wlan_tx_completion(void)
462 {
463         struct dma_desc *desc;
464         int i;
465
466         for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
467                 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
468                         if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
469                                 /* termination requested. */
470                                 break;
471                         }
472                 }
473
474                 wlan_tx_ampdu_reset(i);
475
476                 for_each_desc(desc, &fw.wlan.tx_retry)
477                         __wlan_tx(desc);
478
479                 wlan_tx_ampdu_end(i);
480                 if (!queue_empty(&fw.wlan.tx_queue[i]))
481                         wlan_trigger(BIT(i));
482         }
483 }
484
485 void __hot wlan_tx(struct dma_desc *desc)
486 {
487         struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
488
489         if (ieee80211_is_back_req(super->f.data.i3e.frame_control)) {
490                 fw.wlan.queued_bar++;
491         }
492
493         /* initialize rate control struct */
494         super->s.rix = 0;
495         super->s.cnt = 1;
496         hide_super(desc);
497
498         if (unlikely(super->s.cab)) {
499                 fw.wlan.cab_queue_len[super->s.vif_id]++;
500                 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
501                 return;
502         }
503
504         _wlan_tx(desc);
505         __wlan_tx(desc);
506         wlan_trigger(BIT(super->s.queue));
507 }
508
509 void wlan_tx_fw(struct carl9170_tx_superdesc *super, fw_desc_callback_t cb)
510 {
511         if (!fw.wlan.fw_desc_available)
512                 return;
513
514         fw.wlan.fw_desc_available = 0;
515
516         /* Format BlockAck */
517         fw.wlan.fw_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
518         fw.wlan.fw_desc->status = AR9170_OWN_BITS_SW;
519
520         fw.wlan.fw_desc->totalLen = fw.wlan.fw_desc->dataSize = super->len;
521         fw.wlan.fw_desc_data = fw.wlan.fw_desc->dataAddr = super;
522         fw.wlan.fw_desc->nextAddr = fw.wlan.fw_desc->lastAddr =
523                 fw.wlan.fw_desc;
524         fw.wlan.fw_desc_callback = cb;
525         wlan_tx(fw.wlan.fw_desc);
526 }
527
528 void wlan_send_buffered_ba(void)
529 {
530         struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
531         struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
532         struct carl9170_bar_ctx *ctx;
533
534         if (likely(!fw.wlan.queued_ba))
535                 return;
536
537         /* there's no point to continue when the ba_desc is not available. */
538         if (!fw.wlan.fw_desc_available)
539                 return;
540
541         ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx];
542         fw.wlan.ba_head_idx++;
543         fw.wlan.ba_head_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
544         fw.wlan.queued_ba--;
545
546         baf->s.len = sizeof(struct carl9170_tx_superdesc) +
547                      sizeof(struct ar9170_tx_hwdesc) +
548                      sizeof(struct ieee80211_ba);
549         baf->s.ri[0].tries = 1;
550         baf->s.cookie = 0;
551         baf->s.queue = AR9170_TXQ_VO;
552         baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
553
554         baf->f.hdr.mac.no_ack = 1;
555
556         baf->f.hdr.phy.modulation = 1; /* OFDM */
557         baf->f.hdr.phy.tx_power = 34; /* 17 dBm */
558         baf->f.hdr.phy.chains = 1;
559         baf->f.hdr.phy.mcs = AR9170_TXRX_PHY_RATE_OFDM_6M;
560
561         /* format outgoing BA */
562         ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
563         ba->duration = cpu_to_le16(0);
564
565         /* the BAR contains all necessary MACs. All we need is to swap them */
566         memcpy(ba->ra, ctx->ta, 6);
567         memcpy(ba->ta, ctx->ra, 6);
568
569         /*
570          * Unfortunately, we cannot look into the hardware's scoreboard.
571          * Therefore we have to proceed as described in 802.11n 9.10.7.5
572          * and send a null BlockAck.
573          */
574         memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
575
576         /*
577          * Both, the original firmare and ath9k set the NO ACK flag in
578          * the BA Ack Policy subfield.
579          */
580         ba->control = ctx->control | cpu_to_le16(1);
581         ba->start_seq_num = ctx->start_seq_num;
582         wlan_tx_fw(&baf->s, NULL);
583 }
584
585 void wlan_cab_flush_queue(const unsigned int vif)
586 {
587         struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
588         struct dma_desc *desc;
589
590         /* move queued frames into the main tx queues */
591         for_each_desc(desc, cab_queue) {
592                 struct carl9170_tx_superframe *super = get_super(desc);
593                 if (!queue_empty(cab_queue)) {
594                         /*
595                          * Set MOREDATA flag for all,
596                          * but the last queued frame.
597                          * see: 802.11-2007 11.2.1.5 f)
598                          *
599                          * This is actually the reason to why
600                          * we need to prevent the reentry.
601                          */
602
603                         super->f.data.i3e.frame_control |=
604                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
605                 } else {
606                         super->f.data.i3e.frame_control &=
607                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
608                 }
609
610                 /* ready to roll! */
611                 _wlan_tx(desc);
612                 __wlan_tx(desc);
613                 wlan_trigger(BIT(super->s.queue));
614         }
615 }
616
617 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
618                                const unsigned int len)
619 {
620         struct ieee80211_mgmt *mgmt = addr;
621         uint8_t *pos, *end;
622
623         pos = mgmt->u.beacon.variable;
624         end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
625         while (pos < end) {
626                 if (pos + 2 + pos[1] > end)
627                         return NULL;
628
629                 if (pos[0] == ie)
630                         return pos;
631
632                 pos += pos[1] + 2;
633         }
634
635         return NULL;
636 }
637
638 void wlan_modify_beacon(const unsigned int vif,
639         const unsigned int addr, const unsigned int len)
640 {
641         uint8_t *_ie;
642         struct ieee80211_tim_ie *ie;
643
644         _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
645         if (likely(_ie)) {
646                 ie = (struct ieee80211_tim_ie *) &_ie[2];
647
648                 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
649                         /* schedule DTIM transfer */
650                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
651                 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
652                         /* undo all chances to the beacon structure */
653                         ie->bitmap_ctrl &= ~0x1;
654                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
655                 }
656
657                 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
658                 if (fw.wlan.cab_flush_trigger[vif]) {
659                         /* Set the almighty Multicast Traffic Indication Bit. */
660                         ie->bitmap_ctrl |= 0x1;
661                 }
662         }
663
664         /*
665          * Ideally, the sequence number should be assigned by the TX arbiter
666          * hardware. But AFAIK that's not possible, so we have to go for the
667          * next best thing and write it into the beacon fifo during the open
668          * beacon update window.
669          */
670
671         wlan_assign_seq((struct ieee80211_hdr *)addr, vif);
672 }
673
674 void wlan_send_buffered_cab(void)
675 {
676         unsigned int i;
677
678         for (i = 0; i < CARL9170_INTF_NUM; i++) {
679                 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
680                         /*
681                          * This is hardcoded into carl9170usb driver.
682                          *
683                          * The driver must set the PRETBTT event to beacon_interval -
684                          * CARL9170_PRETBTT_KUS (usually 6) Kus.
685                          *
686                          * But still, we can only do so much about 802.11-2007 9.3.2.1 &
687                          * 11.2.1.6. Let's hope the current solution is adequate enough.
688                          */
689
690                         if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
691                                 wlan_cab_flush_queue(i);
692
693                                 /*
694                                  * This prevents the code from sending new BC/MC frames
695                                  * which were queued after the previous buffered traffic
696                                  * has been sent out... They will have to wait until the
697                                  * next DTIM beacon comes along.
698                                  */
699                                 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
700                         }
701                 }
702
703         }
704 }