Merge pull request #9 from KrasnikovEugene/master
[open-ath9k-htc-firmware.git] / target_firmware / wlan / ar5416_hw.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include "opt_ah.h"
37 #include "ah.h"
38 #include "ah_internal.h"
39 #include "ar5416.h"
40 #include "ar5416reg.h"
41 #include "ar5416phy.h"
42 #include "ar5416desc.h"
43
44 #define N(a) (sizeof(a)/sizeof(a[0]))
45 #define AR_INTR_SPURIOUS 0xffffffff
46 #define ar5416_desc ar5416_desc_20
47 #define AR5416_ABORT_LOOPS 1000
48 #define AR5416_ABORT_WAIT  5
49 #define AR5416DESC         AR5416DESC_20
50 #define AR5416DESC_CONST   AR5416DESC_CONST_20
51
52 /*****************/
53 /* Attach/Detach */
54 /*****************/
55
56 static const struct ath_hal_private ar5416hal_10 = {{
57                 .ah_getRateTable        = ar5416GetRateTable,
58                 .ah_detach              = ar5416Detach,
59
60                 /* Transmit functions */
61                 .ah_updateTxTrigLevel   = ar5416UpdateTxTrigLevel,
62                 .ah_getTxDP             = ar5416GetTxDP,
63                 .ah_setTxDP             = ar5416SetTxDP,
64                 .ah_numTxPending        = ar5416NumTxPending,    
65                 .ah_startTxDma          = ar5416StartTxDma,
66                 .ah_stopTxDma           = ar5416StopTxDma,
67         
68                 .ah_getTxIntrQueue      = ar5416GetTxIntrQueue,
69                 .ah_abortTxDma          = ar5416AbortTxDma,
70
71                 /* Misc Functions */
72                 .ah_getCapability       = ar5416GetCapability,
73                 .ah_getTsf32            = ar5416GetTsf32,
74                 .ah_getTsf64            = ar5416GetTsf64,
75                 .ah_resetTsf            = ar5416ResetTsf,
76                 .ah_setRxFilter         = ar5416SetRxFilter,
77
78                 /* RX Functions */
79                 .ah_getRxDP             = ar5416GetRxDP,
80                 .ah_setRxDP             = ar5416SetRxDP,
81                 .ah_stopDmaReceive      = ar5416StopDmaReceive,
82                 .ah_enableReceive       = ar5416EnableReceive,
83                 .ah_startPcuReceive     = ar5416StartPcuReceive,
84                 .ah_stopPcuReceive      = ar5416StopPcuReceive,
85
86                 /* Interrupt Functions */
87                 .ah_isInterruptPending   = ar5416IsInterruptPending,
88                 .ah_getPendingInterrupts = ar5416GetPendingInterrupts,
89                 .ah_getInterrupts        = ar5416GetInterrupts,
90                 .ah_setInterrupts        = ar5416SetInterrupts,
91         },
92 };
93
94 void ar5416Detach(struct ath_hal *ah)
95 {
96         HALASSERT(ah != AH_NULL);
97         ath_hal_free(ah);
98 }
99
100 struct ath_hal *
101 ar5416Attach(a_uint32_t devid,HAL_SOFTC sc, adf_os_device_t dev,
102              a_uint32_t flags, HAL_STATUS *status)
103 {
104         struct ath_hal_5416 *ahp;
105         struct ath_hal *ah;
106
107         ahp = ath_hal_malloc(sizeof (struct ath_hal_5416));
108         if (ahp == AH_NULL) {
109                 *status = HAL_ENOMEM;
110                 return AH_NULL;
111         }
112         ah = &ahp->ah_priv.h;
113
114         OS_MEMCPY(&ahp->ah_priv, &ar5416hal_10, sizeof(struct ath_hal_private));
115
116         ah->ah_dev = dev;
117         ah->ah_sc = sc;
118         
119         /* If its a Owl 2.0 chip then change the hal structure to
120            point to the Owl 2.0 ar5416_hal_20 structure */
121         if(1) {
122                 ah->ah_set11nTxDesc        = ar5416Set11nTxDesc_20;
123                 ah->ah_set11nRateScenario  = ar5416Set11nRateScenario_20;
124                 ah->ah_set11nAggrFirst     = ar5416Set11nAggrFirst_20;
125                 ah->ah_set11nAggrMiddle    = ar5416Set11nAggrMiddle_20;
126                 ah->ah_set11nAggrLast      = ar5416Set11nAggrLast_20;
127                 ah->ah_clr11nAggr          = ar5416Clr11nAggr_20;
128                 ah->ah_set11nBurstDuration = ar5416Set11nBurstDuration_20;
129                 ah->ah_setupRxDesc         = ar5416SetupRxDesc_20;
130                 ah->ah_procRxDescFast      = ar5416ProcRxDescFast_20;
131                 ah->ah_updateCTSForBursting = NULL;
132                 ah->ah_setupTxDesc         = ar5416SetupTxDesc_20;
133                 ah->ah_reqTxIntrDesc       = ar5416IntrReqTxDesc_20;
134                 ah->ah_fillTxDesc          = ar5416FillTxDesc_20;
135                 ah->ah_fillKeyTxDesc       = ar5416FillKeyTxDesc_20;
136                 ah->ah_procTxDesc          = ar5416ProcTxDesc_20;
137                 ah->ah_set11nVirtualMoreFrag = ar5416Set11nVirtualMoreFrag_20;
138         }
139
140         return ah;
141 }
142
143 /**********************/
144 /* Interrupt Handling */
145 /**********************/
146
147 HAL_BOOL ar5416IsInterruptPending(struct ath_hal *ah)
148 {
149         a_uint32_t host_isr = OS_REG_READ(ah, AR_INTR_ASYNC_CAUSE);
150         /*
151          * Some platforms trigger our ISR before applying power to
152          * the card, so make sure.
153          */
154         return ((host_isr != AR_INTR_SPURIOUS) && (host_isr & AR_INTR_MAC_IRQ));
155 }
156
157 HAL_BOOL ar5416GetPendingInterrupts(struct ath_hal *ah, HAL_INT *masked)
158 {
159         a_uint32_t isr;
160 #ifndef AR9100
161         HAL_BOOL fatal_int = AH_FALSE;
162         a_uint32_t sync_cause;
163
164         if (OS_REG_READ(ah, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) {
165                 if ((OS_REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M) != AR_RTC_STATUS_ON) {
166                         *masked = 0;
167                         return AH_FALSE;
168                 }
169         } else {
170                 *masked = 0;
171                 return AH_FALSE;
172         }
173 #endif
174         isr = OS_REG_READ(ah, AR_ISR_RAC);
175         if (isr == 0xffffffff) {
176                 *masked = 0;
177                 return AH_FALSE;
178         }
179
180         *masked = isr & HAL_INT_COMMON;
181
182 #ifdef AR5416_INT_MITIGATION
183         if (isr & (AR_ISR_RXMINTR | AR_ISR_RXINTM)) {
184                 *masked |= HAL_INT_RX;
185         }
186         if (isr & (AR_ISR_TXMINTR | AR_ISR_TXINTM)) {
187                 *masked |= HAL_INT_TX;
188         }
189 #endif
190
191         if (isr & AR_ISR_BCNMISC) {
192                 a_uint32_t s2_s;
193
194                 s2_s = OS_REG_READ(ah, AR_ISR_S2_S);
195
196                 if (s2_s & AR_ISR_S2_GTT) {
197                         *masked |= HAL_INT_GTT;
198                 }
199
200                 if (s2_s & AR_ISR_S2_CST) {
201                         *masked |= HAL_INT_CST;
202                 }
203         }
204
205         if (isr & (AR_ISR_RXOK | AR_ISR_RXERR))
206                 *masked |= HAL_INT_RX;
207         if (isr & (AR_ISR_TXOK | AR_ISR_TXDESC | AR_ISR_TXERR | AR_ISR_TXEOL)) {
208                 struct ath_hal_5416 *ahp = AH5416(ah);
209                 a_uint32_t           s0_s, s1_s;
210
211                 *masked |= HAL_INT_TX;
212                 s0_s = OS_REG_READ(ah, AR_ISR_S0_S);
213                 s1_s = OS_REG_READ(ah, AR_ISR_S1_S);
214                 ahp->ah_intrTxqs |= MS(s0_s, AR_ISR_S0_QCU_TXOK);
215                 ahp->ah_intrTxqs |= MS(s0_s, AR_ISR_S0_QCU_TXDESC);
216                 ahp->ah_intrTxqs |= MS(s1_s, AR_ISR_S1_QCU_TXERR);
217                 ahp->ah_intrTxqs |= MS(s1_s, AR_ISR_S1_QCU_TXEOL);
218         }
219
220 #ifndef AR9100
221         sync_cause = OS_REG_READ(ah, AR_INTR_SYNC_CAUSE);
222         fatal_int = ((sync_cause != AR_INTR_SPURIOUS) &&
223                      (sync_cause & (AR_INTR_SYNC_HOST1_FATAL | AR_INTR_SYNC_HOST1_PERR))) ?
224                 AH_TRUE : AH_FALSE;
225
226         if (AH_TRUE == fatal_int) {
227                 OS_REG_WRITE(ah, AR_INTR_SYNC_CAUSE_CLR, sync_cause);
228                 (void) OS_REG_READ(ah, AR_INTR_SYNC_CAUSE_CLR);
229         }
230 #endif
231         return AH_TRUE;
232 }
233
234 HAL_INT ar5416GetInterrupts(struct ath_hal *ah)
235 {
236         return AH5416(ah)->ah_maskReg;
237 }
238
239 HAL_INT
240 ar5416SetInterrupts(struct ath_hal *ah, HAL_INT ints)
241 {
242         struct ath_hal_5416 *ahp = AH5416(ah);
243         a_uint32_t omask = ahp->ah_maskReg;
244         a_uint32_t mask;
245
246         if (omask & HAL_INT_GLOBAL) {
247                 OS_REG_WRITE(ah, AR_IER, AR_IER_DISABLE);
248                 (void) OS_REG_READ(ah, AR_IER);
249         }
250
251         mask = ints & HAL_INT_COMMON;
252         if (ints & HAL_INT_TX) {
253 #ifdef AR5416_INT_MITIGATION
254                 mask |= AR_IMR_TXMINTR | AR_IMR_TXINTM;
255 #else
256                 mask |= AR_IMR_TXOK;
257                 mask |= AR_IMR_TXDESC;
258 #endif
259                 mask |= AR_IMR_TXERR;
260                 mask |= AR_IMR_TXEOL;
261         }
262         if (ints & HAL_INT_RX) {
263                 mask |= AR_IMR_RXERR;
264 #ifdef AR5416_INT_MITIGATION
265                 mask |=  AR_IMR_RXMINTR | AR_IMR_RXINTM;
266 #else
267                 mask |= AR_IMR_RXOK | AR_IMR_RXDESC;
268 #endif
269         }
270
271         if (ints & (HAL_INT_GTT | HAL_INT_CST)) {
272                 mask |= AR_IMR_BCNMISC;
273         }
274
275         OS_REG_WRITE(ah, AR_IMR, mask);
276         (void) OS_REG_READ(ah, AR_IMR);
277         ahp->ah_maskReg = ints;
278
279         /* Re-enable interrupts if they were enabled before. */
280         if (ints & HAL_INT_GLOBAL) {
281                 OS_REG_WRITE(ah, AR_IER, AR_IER_ENABLE);
282                 /* See explanation above... */
283                 (void) OS_REG_READ(ah, AR_IER);
284         }
285
286         OS_REG_WRITE(ah, AR_INTR_ASYNC_ENABLE, AR_INTR_MAC_IRQ);
287         OS_REG_WRITE(ah, AR_INTR_ASYNC_MASK, AR_INTR_MAC_IRQ);
288         OS_REG_WRITE(ah, AR_INTR_SYNC_ENABLE, AR_INTR_SYNC_ALL);
289
290         return omask;
291 }
292
293 /****************/
294 /* Capabilities */
295 /****************/
296
297 HAL_STATUS ar5416GetCapability(struct ath_hal *ah, HAL_CAPABILITY_TYPE type,
298                                a_uint32_t capability, a_uint32_t *result)
299
300 {
301         HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
302 #ifndef MAGPIE_MERLIN // K2
303         pCap->halRxStbcSupport = 1;  /* K2 supports STBC Rx only */
304         pCap->halTxStbcSupport = 0;
305 #else
306         pCap->halRxStbcSupport = 1;  /* number of streams for STBC recieve. */
307         pCap->halTxStbcSupport = 1;
308 #endif
309
310         switch (type) {
311 #ifdef MAGPIE_MERLIN
312         case HAL_CAP_RX_STBC:
313         {
314                 *result = pCap->halRxStbcSupport;
315                 return HAL_OK;
316         }
317         case HAL_CAP_TX_STBC:
318         {
319                 *result = pCap->halTxStbcSupport;
320                 return HAL_OK;
321         }
322 #endif
323         default:
324                 return ath_hal_getcapability(ah, type, capability, result);
325         }
326 }
327
328 /****************/
329 /* TSF Handling */
330 /****************/
331
332 u_int64_t ar5416GetTsf64(struct ath_hal *ah)
333 {
334         u_int64_t tsf;
335
336         tsf = OS_REG_READ(ah, AR_TSF_U32);
337         tsf = (tsf << 32) | OS_REG_READ(ah, AR_TSF_L32);
338
339         return tsf;
340 }
341
342 a_uint32_t ar5416GetTsf32(struct ath_hal *ah)
343 {
344         return OS_REG_READ(ah, AR_TSF_L32);
345 }
346
347 void ar5416ResetTsf(struct ath_hal *ah)
348 {
349         a_int32_t count;
350
351         count = 0;
352
353         while (OS_REG_READ(ah, AR_SLP32_MODE) & AR_SLP32_TSF_WRITE_STATUS) {
354                 count++;
355                 if (count > 10) {
356                         break;
357                 }
358                 OS_DELAY(10);
359         }
360         OS_REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
361 }
362
363 /******/
364 /* RX */
365 /******/
366
367 a_uint32_t ar5416GetRxDP(struct ath_hal *ath)
368 {
369         return OS_REG_READ(ath, AR_RXDP);
370 }
371
372
373 void ar5416SetRxDP(struct ath_hal *ah, a_uint32_t rxdp)
374 {
375         OS_REG_WRITE(ah, AR_RXDP, rxdp);
376         HALASSERT(OS_REG_READ(ah, AR_RXDP) == rxdp);
377 }
378
379 void ar5416SetMulticastFilter(struct ath_hal *ah, a_uint32_t filter0, a_uint32_t filter1)
380 {
381         OS_REG_WRITE(ah, AR_MCAST_FIL0, filter0);
382         OS_REG_WRITE(ah, AR_MCAST_FIL1, filter1);
383 }
384
385 HAL_BOOL ar5416ClrMulticastFilterIndex(struct ath_hal *ah, a_uint32_t ix)
386 {
387         a_uint32_t val;
388
389         if (ix >= 64)
390                 return AH_FALSE;
391         if (ix >= 32) {
392                 val = OS_REG_READ(ah, AR_MCAST_FIL1);
393                 OS_REG_WRITE(ah, AR_MCAST_FIL1, (val &~ (1<<(ix-32))));
394         } else {
395                 val = OS_REG_READ(ah, AR_MCAST_FIL0);
396                 OS_REG_WRITE(ah, AR_MCAST_FIL0, (val &~ (1<<ix)));
397         }
398         return AH_TRUE;
399 }
400
401 HAL_BOOL ar5416StopDmaReceive(struct ath_hal *ah)
402 {
403         OS_REG_WRITE(ah, AR_CR, AR_CR_RXD); /* Set receive disable bit */
404         if (!ath_hal_wait(ah, AR_CR, AR_CR_RXE, 0)) {
405                 return AH_FALSE;
406         } else {
407                 return AH_TRUE;
408         }
409 }
410
411 HAL_BOOL ar5416SetMulticastFilterIndex(struct ath_hal *ah, a_uint32_t ix)
412 {
413         a_uint32_t val;
414
415         if (ix >= 64)
416                 return AH_FALSE;
417         if (ix >= 32) {
418                 val = OS_REG_READ(ah, AR_MCAST_FIL1);
419                 OS_REG_WRITE(ah, AR_MCAST_FIL1, (val | (1<<(ix-32))));
420         } else {
421                 val = OS_REG_READ(ah, AR_MCAST_FIL0);
422                 OS_REG_WRITE(ah, AR_MCAST_FIL0, (val | (1<<ix)));
423         }
424         return AH_TRUE;
425 }
426
427 void ar5416StartPcuReceive(struct ath_hal *ah)
428 {
429         OS_REG_CLR_BIT(ah, AR_DIAG_SW,
430                        (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
431 }
432
433 void ar5416SetRxFilter(struct ath_hal *ah, a_uint32_t bits)
434 {
435         a_uint32_t phybits;
436     
437         OS_REG_WRITE(ah, AR_RX_FILTER, (bits & 0xff) | AR_RX_COMPR_BAR);
438         phybits = 0;
439         if (bits & HAL_RX_FILTER_PHYRADAR)
440                 phybits |= AR_PHY_ERR_RADAR;
441         if (bits & HAL_RX_FILTER_PHYERR)
442                 phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
443         OS_REG_WRITE(ah, AR_PHY_ERR, phybits);
444         if (phybits) {
445                 OS_REG_WRITE(ah, AR_RXCFG,OS_REG_READ(ah, AR_RXCFG) | AR_RXCFG_ZLFDMA);
446         } else {
447                 OS_REG_WRITE(ah, AR_RXCFG,OS_REG_READ(ah, AR_RXCFG) &~ AR_RXCFG_ZLFDMA);
448         }
449 }
450
451 void ar5416EnableReceive(struct ath_hal *ah)
452 {
453         OS_REG_WRITE(ah, AR_CR, AR_CR_RXE);
454 }
455
456 void ar5416StopPcuReceive(struct ath_hal *ah)
457 {
458         OS_REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_RX_DIS);
459 }
460
461 HAL_BOOL ar5416SetupRxDesc_20(struct ath_hal *ah, struct ath_rx_desc *ds,
462                               a_uint32_t size, a_uint32_t flags)
463 {
464         struct ar5416_desc *ads = AR5416DESC(ds);
465
466         HALASSERT((size &~ AR_BufLen) == 0);
467
468         ads->ds_ctl1 = size & AR_BufLen;
469         if (flags & HAL_RXDESC_INTREQ)
470                 ads->ds_ctl1 |= AR_RxIntrReq;
471
472         /* this should be enough */
473         ads->ds_rxstatus8 &= ~AR_RxDone;
474
475         return AH_TRUE;
476 }
477
478 HAL_STATUS ar5416ProcRxDescFast_20(struct ath_hal *ah, struct ath_rx_desc *ds,
479                                    a_uint32_t pa, struct ath_desc *nds,
480                                    struct ath_rx_status *rx_stats)
481 {
482         struct ar5416_desc ads;
483         struct ar5416_desc *adsp = AR5416DESC(ds);
484         struct ar5416_desc *ands = AR5416DESC(nds);
485
486         if ((adsp->ds_rxstatus8 & AR_RxDone) == 0)
487                 return HAL_EINPROGRESS;
488         /*
489          * Given the use of a self-linked tail be very sure that the hw is
490          * done with this descriptor; the hw may have done this descriptor
491          * once and picked it up again...make sure the hw has moved on.
492          */
493         if ((ands->ds_rxstatus8 & AR_RxDone) == 0
494             && OS_REG_READ(ah, AR_RXDP) == pa)
495                 return HAL_EINPROGRESS;
496
497         /*
498          * Now we need to get the stats from the descriptor. Since desc are 
499          * uncached, lets make a copy of the stats first. Note that, since we
500          * touch most of the rx stats, a memcpy would always be more efficient
501          *
502          * Next we fill in all values in a caller passed stack variable.
503          * This reduces the number of uncached accesses.
504          * Do this copy here, after the check so that when the checks fail, we
505          * dont end up copying the entire stats uselessly.
506          */
507         ads.u.rx = adsp->u.rx;
508
509         rx_stats->rs_status = 0;
510         rx_stats->rs_flags = 0;
511
512         rx_stats->rs_datalen = ads.ds_rxstatus1 & AR_DataLen;
513         rx_stats->rs_tstamp =  ads.AR_RcvTimestamp;
514
515         /* XXX what about KeyCacheMiss? */
516         rx_stats->rs_rssi_combined = 
517                 MS(ads.ds_rxstatus4, AR_RxRSSICombined);
518         rx_stats->rs_rssi_ctl0 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt00);
519         rx_stats->rs_rssi_ctl1 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt01);
520         rx_stats->rs_rssi_ctl2 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt02);
521         rx_stats->rs_rssi_ext0 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt10);
522         rx_stats->rs_rssi_ext1 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt11);
523         rx_stats->rs_rssi_ext2 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt12);
524         if (ads.ds_rxstatus8 & AR_RxKeyIdxValid)
525                 rx_stats->rs_keyix = MS(ads.ds_rxstatus8, AR_KeyIdx);
526         else
527                 rx_stats->rs_keyix = HAL_RXKEYIX_INVALID;
528         /* NB: caller expected to do rate table mapping */
529         rx_stats->rs_rate = RXSTATUS_RATE(ah, (&ads));
530         rx_stats->rs_more = (ads.ds_rxstatus1 & AR_RxMore) ? 1 : 0;
531
532         rx_stats->rs_isaggr = (ads.ds_rxstatus8 & AR_RxAggr) ? 1 : 0;
533         rx_stats->rs_moreaggr = (ads.ds_rxstatus8 & AR_RxMoreAggr) ? 1 : 0;
534         rx_stats->rs_flags  |= (ads.ds_rxstatus3 & AR_GI) ? HAL_RX_GI : 0;
535         rx_stats->rs_flags  |= (ads.ds_rxstatus3 & AR_2040) ? HAL_RX_2040 : 0;
536
537         if (ads.ds_rxstatus8 & AR_PreDelimCRCErr)
538                 rx_stats->rs_flags |= HAL_RX_DELIM_CRC_PRE;
539         if (ads.ds_rxstatus8 & AR_PostDelimCRCErr)
540                 rx_stats->rs_flags |= HAL_RX_DELIM_CRC_POST;
541         if (ads.ds_rxstatus8 & AR_DecryptBusyErr)
542                 rx_stats->rs_flags |= HAL_RX_DECRYPT_BUSY;
543
544         if ((ads.ds_rxstatus8 & AR_RxFrameOK) == 0) {
545                 /*
546                  * These four bits should not be set together.  The
547                  * 5416 spec states a Michael error can only occur if
548                  * DecryptCRCErr not set (and TKIP is used).  Experience
549                  * indicates however that you can also get Michael errors
550                  * when a CRC error is detected, but these are specious.
551                  * Consequently we filter them out here so we don't
552                  * confuse and/or complicate drivers.
553                  */
554                 if (ads.ds_rxstatus8 & AR_CRCErr)
555                         rx_stats->rs_status |= HAL_RXERR_CRC;
556                 else if (ads.ds_rxstatus8 & AR_PHYErr) {
557                         a_uint32_t phyerr;
558
559                         rx_stats->rs_status |= HAL_RXERR_PHY;
560                         phyerr = MS(ads.ds_rxstatus8, AR_PHYErrCode);
561                         rx_stats->rs_phyerr = phyerr;
562                 } else if (ads.ds_rxstatus8 & AR_DecryptCRCErr)
563                         rx_stats->rs_status |= HAL_RXERR_DECRYPT;
564                 else if (ads.ds_rxstatus8 & AR_MichaelErr)
565                         rx_stats->rs_status |= HAL_RXERR_MIC;
566         }
567         rx_stats->evm0=ads.AR_RxEVM0;
568         rx_stats->evm1=ads.AR_RxEVM1;
569         rx_stats->evm2=ads.AR_RxEVM2;
570
571         return HAL_OK;
572 }
573
574 /******/
575 /* TX */
576 /******/
577
578 HAL_BOOL ar5416UpdateTxTrigLevel(struct ath_hal *ah, HAL_BOOL bIncTrigLevel)
579 {
580         struct ath_hal_5416 *ahp = AH5416(ah);
581         a_uint32_t txcfg, curLevel, newLevel;
582         HAL_INT omask;
583
584         /*
585          * Disable interrupts while futzing with the fifo level.
586          */
587         omask = ar5416SetInterrupts(ah, ahp->ah_maskReg &~ HAL_INT_GLOBAL);
588
589         txcfg = OS_REG_READ(ah, AR_TXCFG);
590         curLevel = MS(txcfg, AR_FTRIG);
591         newLevel = curLevel;
592
593         if (bIncTrigLevel)  {
594                 if (curLevel < MAX_TX_FIFO_THRESHOLD)
595                         newLevel ++;
596         } else if (curLevel > MIN_TX_FIFO_THRESHOLD)
597                 newLevel--;
598         if (newLevel != curLevel)
599                 OS_REG_WRITE(ah, AR_TXCFG,
600                              (txcfg &~ AR_FTRIG) | SM(newLevel, AR_FTRIG));
601
602         /* re-enable chip interrupts */
603         ar5416SetInterrupts(ah, omask);
604
605         return (newLevel != curLevel);
606 }
607
608 a_uint32_t ar5416GetTxDP(struct ath_hal *ah, a_uint32_t q)
609 {
610         HALASSERT(q < AH_PRIVATE(ah)->ah_caps.halTotalQueues);
611         return OS_REG_READ(ah, AR_QTXDP(q));
612 }
613
614 HAL_BOOL ar5416SetTxDP(struct ath_hal *ah, a_uint32_t q, a_uint32_t txdp)
615 {
616         HALASSERT(q < AH_PRIVATE(ah)->ah_caps.halTotalQueues);
617         HALASSERT(AH5416(ah)->ah_txq[q].tqi_type != HAL_TX_QUEUE_INACTIVE);
618
619         /*
620          * Make sure that TXE is deasserted before setting the TXDP.  If TXE
621          * is still asserted, setting TXDP will have no effect.
622          */
623         HALASSERT((OS_REG_READ(ah, AR_Q_TXE) & (1 << q)) == 0);
624
625         OS_REG_WRITE(ah, AR_QTXDP(q), txdp);
626
627         return AH_TRUE;
628 }
629
630 HAL_BOOL ar5416StartTxDma(struct ath_hal *ah, a_uint32_t q)
631 {
632         HALASSERT(q < AH_PRIVATE(ah)->ah_caps.halTotalQueues);
633         HALASSERT(AH5416(ah)->ah_txq[q].tqi_type != HAL_TX_QUEUE_INACTIVE);
634
635         /* Check to be sure we're not enabling a q that has its TXD bit set. */
636         HALASSERT((OS_REG_READ(ah, AR_Q_TXD) & (1 << q)) == 0);
637
638         OS_REG_WRITE(ah, AR_Q_TXE, 1 << q);
639
640         return AH_TRUE;
641 }
642
643 a_uint32_t ar5416NumTxPending(struct ath_hal *ah, a_uint32_t q)
644 {
645         a_uint32_t npend;
646
647         HALASSERT(q < AH_PRIVATE(ah)->ah_caps.halTotalQueues);
648         HALASSERT(AH5416(ah)->ah_txq[q].tqi_type != HAL_TX_QUEUE_INACTIVE);
649
650         npend = OS_REG_READ(ah, AR_QSTS(q)) & AR_Q_STS_PEND_FR_CNT;
651         if (npend == 0) {
652                 /*
653                  * Pending frame count (PFC) can momentarily go to zero
654                  * while TXE remains asserted.  In other words a PFC of
655                  * zero is not sufficient to say that the queue has stopped.
656                  */
657                 if (OS_REG_READ(ah, AR_Q_TXE) & (1 << q))
658                         npend = 1;
659         }
660 #ifdef DEBUG
661         if (npend && (AH5416(ah)->ah_txq[q].tqi_type == HAL_TX_QUEUE_CAB)) {
662                 if (OS_REG_READ(ah, AR_Q_RDYTIMESHDN) & (1 << q)) {
663                         isrPrintf("RTSD on CAB queue\n");
664                         /* Clear the ReadyTime shutdown status bits */
665                         OS_REG_WRITE(ah, AR_Q_RDYTIMESHDN, 1 << q);
666                 }
667         }
668 #endif
669         return npend;
670 }
671
672 HAL_BOOL ar5416AbortTxDma(struct ath_hal *ah)
673 {
674         a_int32_t i, q;
675
676         /*
677          * set txd on all queues
678          */
679         OS_REG_WRITE(ah, AR_Q_TXD, AR_Q_TXD_M);
680
681         /*
682          * set tx abort bits
683          */
684         OS_REG_SET_BIT(ah, AR_PCU_MISC, (AR_PCU_FORCE_QUIET_COLL | AR_PCU_CLEAR_VMF));
685         OS_REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
686         OS_REG_SET_BIT(ah, AR_D_GBL_IFS_MISC, AR_D_GBL_IFS_MISC_IGNORE_BACKOFF);
687
688         /*
689          * wait on all tx queues
690          */
691         for (q = 0; q < AR_NUM_QCU; q++) {
692                 for (i = 0; i < AR5416_ABORT_LOOPS; i++) {
693                         if (!ar5416NumTxPending(ah, q))
694                                 break;
695
696                         OS_DELAY(AR5416_ABORT_WAIT);
697                 }
698                 if (i == AR5416_ABORT_LOOPS) {
699                         return AH_FALSE;
700                 }
701         }
702
703         /*
704          * clear tx abort bits
705          */
706         OS_REG_CLR_BIT(ah, AR_PCU_MISC, (AR_PCU_FORCE_QUIET_COLL | AR_PCU_CLEAR_VMF));
707         OS_REG_CLR_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
708         OS_REG_CLR_BIT(ah, AR_D_GBL_IFS_MISC, AR_D_GBL_IFS_MISC_IGNORE_BACKOFF);
709
710         /*
711          * clear txd
712          */
713         OS_REG_WRITE(ah, AR_Q_TXD, 0);
714
715         return AH_TRUE;
716 }
717
718 HAL_BOOL ar5416StopTxDma(struct ath_hal*ah, a_uint32_t q)
719 {
720         a_uint32_t i;
721         
722         HALASSERT(q < AH_PRIVATE(ah)->ah_caps.halTotalQueues);
723
724         HALASSERT(AH5416(ah)->ah_txq[q].tqi_type != HAL_TX_QUEUE_INACTIVE);
725
726         OS_REG_WRITE(ah, AR_Q_TXD, 1 << q);
727         for (i = 1000; i != 0; i--) {
728                 if (ar5416NumTxPending(ah, q) == 0)
729                         break;
730                 OS_DELAY(100);        /* XXX get actual value */
731         }
732
733         OS_REG_WRITE(ah, AR_Q_TXD, 0);
734         return (i != 0);
735 }
736
737 void ar5416GetTxIntrQueue(struct ath_hal *ah, a_uint32_t *txqs)
738 {
739         struct ath_hal_5416 *ahp = AH5416(ah);
740         *txqs &= ahp->ah_intrTxqs;
741         ahp->ah_intrTxqs &= ~(*txqs);
742 }
743
744 void ar5416IntrReqTxDesc_20(struct ath_hal *ah, struct ath_desc *ds)
745 {
746         struct ar5416_desc *ads = AR5416DESC(ds);
747         ads->ds_ctl0 |= AR_TxIntrReq;
748 }
749
750 HAL_BOOL ar5416SetupTxDesc_20(struct ath_hal *ah, struct ath_tx_desc *ds,
751                               a_uint32_t pktLen,
752                               a_uint32_t hdrLen,
753                               HAL_PKT_TYPE type,
754                               a_uint32_t txPower,
755                               a_uint32_t txRate0, a_uint32_t txTries0,
756                               a_uint32_t keyIx,
757                               a_uint32_t antMode,
758                               a_uint32_t flags,
759                               a_uint32_t rtsctsRate,
760                               a_uint32_t rtsctsDuration,
761                               a_uint32_t compicvLen,
762                               a_uint32_t compivLen,
763                               a_uint32_t comp)
764 {
765 #define RTSCTS  (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)
766
767         struct ar5416_desc *ads = AR5416DESC(ds);
768
769         (void) hdrLen;
770
771         ads->ds_txstatus9 &= ~AR_TxDone;
772
773         HALASSERT(txTries0 != 0);
774         HALASSERT(isValidPktType(type));
775         HALASSERT(isValidTxRate(txRate0));
776         HALASSERT((flags & RTSCTS) != RTSCTS);
777
778         if (txPower > 63)
779                 txPower=63;
780
781         ads->ds_ctl0 = (pktLen & AR_FrameLen)
782                 | (txPower << AR_XmitPower_S)
783                 | (flags & HAL_TXDESC_VEOL ? AR_VEOL : 0)
784                 | (flags & HAL_TXDESC_CLRDMASK ? AR_ClrDestMask : 0)
785                 | (flags & HAL_TXDESC_INTREQ ? AR_TxIntrReq : 0);
786
787         ads->ds_ctl1 = (type << AR_FrameType_S)
788                 | (flags & HAL_TXDESC_NOACK ? AR_NoAck : 0);
789         ads->ds_ctl2 = SM(txTries0, AR_XmitDataTries0);
790         ads->ds_ctl3 = (txRate0 << AR_XmitRate0_S);
791
792         ads->ds_ctl7 = SM(AR5416_LEGACY_CHAINMASK, AR_ChainSel0) 
793                 | SM(AR5416_LEGACY_CHAINMASK, AR_ChainSel1)
794                 | SM(AR5416_LEGACY_CHAINMASK, AR_ChainSel2) 
795                 | SM(AR5416_LEGACY_CHAINMASK, AR_ChainSel3);
796
797         if (keyIx != HAL_TXKEYIX_INVALID) {
798                 /* XXX validate key index */
799                 ads->ds_ctl1 |= SM(keyIx, AR_DestIdx);
800                 ads->ds_ctl0 |= AR_DestIdxValid;
801         }
802
803         if (flags & RTSCTS) {
804                 if (!isValidTxRate(rtsctsRate)) {
805                         return AH_FALSE;
806                 }
807                 /* XXX validate rtsctsDuration */
808                 ads->ds_ctl0 |= (flags & HAL_TXDESC_CTSENA ? AR_CTSEnable : 0)
809                         | (flags & HAL_TXDESC_RTSENA ? AR_RTSEnable : 0);
810                 ads->ds_ctl2 |= SM(rtsctsDuration, AR_BurstDur);
811                 ads->ds_ctl3 |= (rtsctsRate << AR_RTSCTSRate_S);
812         }
813         return AH_TRUE;
814
815 #undef RTSCTS
816 }
817
818 HAL_BOOL ar5416FillTxDesc_20(struct ath_hal *ah, struct ath_tx_desc *ds,
819                              a_uint32_t segLen, HAL_BOOL firstSeg, HAL_BOOL lastSeg,
820                              const struct ath_tx_desc *ds0)
821 {
822         struct ar5416_desc *ads = AR5416DESC(ds);
823
824         HALASSERT((segLen &~ AR_BufLen) == 0);
825
826         if (firstSeg) {
827                 /*
828                  * First descriptor, don't clobber xmit control data
829                  * setup by ar5416SetupTxDesc.
830                  */
831                 ads->ds_ctl1 |= segLen | (lastSeg ? 0 : AR_TxMore);
832         } else if (lastSeg) {
833                 /*
834                  * Last descriptor in a multi-descriptor frame,
835                  * copy the multi-rate transmit parameters from
836                  * the first frame for processing on completion.
837                  */
838                 ads->ds_ctl0 = 0;
839                 ads->ds_ctl1 = segLen;
840                 ads->ds_ctl2 = AR5416DESC_CONST(ds0)->ds_ctl2;
841                 ads->ds_ctl3 = AR5416DESC_CONST(ds0)->ds_ctl3;
842         } else {
843                 /*
844                  * Intermediate descriptor in a multi-descriptor frame.
845                  */
846                 ads->ds_ctl0 = 0;
847                 ads->ds_ctl1 = segLen | AR_TxMore;
848                 ads->ds_ctl2 = 0;
849                 ads->ds_ctl3 = 0;
850         }
851         ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
852
853         return AH_TRUE;
854 }
855
856 HAL_BOOL ar5416FillKeyTxDesc_20(struct ath_hal *ah, struct ath_tx_desc *ds,
857                                 HAL_KEY_TYPE keyType)
858 {
859         struct ar5416_desc *ads = AR5416DESC(ds);
860
861         ads->ds_ctl6 = SM(keyType, AR_EncrType);
862         return AH_TRUE;
863 }
864
865 HAL_STATUS ar5416ProcTxDesc_20(struct ath_hal *ah, struct ath_tx_desc *gds)
866 {
867         struct ar5416_desc *ads = AR5416DESC(gds);
868         struct ath_tx_desc *ds = (struct ath_tx_desc *)gds;
869         
870         if ((ads->ds_txstatus9 & AR_TxDone) == 0)
871                 return HAL_EINPROGRESS;
872
873         ads->ds_txstatus9 &= ~AR_TxDone;
874
875         /* Update software copies of the HW status */
876         ds->ds_txstat.ts_seqnum = MS(ads->ds_txstatus9, AR_SeqNum);
877         ds->ds_txstat.ts_tstamp = ads->AR_SendTimestamp;
878         ds->ds_txstat.ts_status = 0;
879         ds->ds_txstat.ts_flags  = 0;
880
881         if (ads->ds_txstatus1 & AR_ExcessiveRetries)
882                 ds->ds_txstat.ts_status |= HAL_TXERR_XRETRY;
883         if (ads->ds_txstatus1 & AR_Filtered)
884                 ds->ds_txstat.ts_status |= HAL_TXERR_FILT;
885         if (ads->ds_txstatus1 & AR_FIFOUnderrun)
886                 ds->ds_txstat.ts_status |= HAL_TXERR_FIFO;
887         if (ads->ds_txstatus9 & AR_TxOpExceeded)
888                 ds->ds_txstat.ts_status |= HAL_TXERR_XTXOP;
889         if (ads->ds_txstatus1 & AR_TxTimerExpired)
890                 ds->ds_txstat.ts_status |= HAL_TXERR_TIMER_EXPIRED;
891
892         if (ads->ds_txstatus1 & AR_DescCfgErr)
893                 ds->ds_txstat.ts_flags |= HAL_TX_DESC_CFG_ERR;
894         if (ads->ds_txstatus1 & AR_TxDataUnderrun) {
895                 ds->ds_txstat.ts_flags |= HAL_TX_DATA_UNDERRUN;
896                 ar5416UpdateTxTrigLevel(ah, AH_TRUE);
897         }
898         if (ads->ds_txstatus1 & AR_TxDelimUnderrun) {
899                 ds->ds_txstat.ts_flags |= HAL_TX_DELIM_UNDERRUN;
900                 ar5416UpdateTxTrigLevel(ah, AH_TRUE);
901         }
902         if (ads->ds_txstatus0 & AR_TxBaStatus) {
903                 ds->ds_txstat.ts_flags |= HAL_TX_BA;
904                 ds->ds_txstat.ba_low = ads->AR_BaBitmapLow;
905                 ds->ds_txstat.ba_high = ads->AR_BaBitmapHigh;
906         }
907
908         /*
909          * Extract the transmit rate used and mark the rate as
910          * ``alternate'' if it wasn't the series 0 rate.
911          */
912         ds->ds_txstat.ts_rate = MS(ads->ds_txstatus9, AR_FinalTxIdx);
913         ds->ds_txstat.ts_rssi_combined = 
914                 MS(ads->ds_txstatus5, AR_TxRSSICombined);
915         ds->ds_txstat.ts_rssi_ctl0 = MS(ads->ds_txstatus0, AR_TxRSSIAnt00);
916         ds->ds_txstat.ts_rssi_ctl1 = MS(ads->ds_txstatus0, AR_TxRSSIAnt01);
917         ds->ds_txstat.ts_rssi_ctl2 = MS(ads->ds_txstatus0, AR_TxRSSIAnt02);
918         ds->ds_txstat.ts_rssi_ext0 = MS(ads->ds_txstatus5, AR_TxRSSIAnt10);
919         ds->ds_txstat.ts_rssi_ext1 = MS(ads->ds_txstatus5, AR_TxRSSIAnt11);
920         ds->ds_txstat.ts_rssi_ext2 = MS(ads->ds_txstatus5, AR_TxRSSIAnt12);
921         ds->ds_txstat.evm0 = ads->AR_TxEVM0;
922         ds->ds_txstat.evm1 = ads->AR_TxEVM1;
923         ds->ds_txstat.evm2 = ads->AR_TxEVM2;
924         ds->ds_txstat.ts_shortretry = MS(ads->ds_txstatus1, AR_RTSFailCnt);
925         ds->ds_txstat.ts_longretry = MS(ads->ds_txstatus1, AR_DataFailCnt);
926         ds->ds_txstat.ts_virtcol = MS(ads->ds_txstatus1, AR_VirtRetryCnt);
927         ds->ds_txstat.ts_antenna = 0;           /* ignored for owl */
928
929         return HAL_OK;
930 }
931
932 void ar5416Set11nTxDesc_20(struct ath_hal *ah, struct ath_tx_desc *ds,
933                            a_uint32_t pktLen, HAL_PKT_TYPE type, a_uint32_t txPower,
934                            a_uint32_t keyIx, HAL_KEY_TYPE keyType,
935                            a_uint32_t flags)
936 {
937         struct ar5416_desc *ads = AR5416DESC(ds);
938
939         HALASSERT(isValidPktType(type));
940         HALASSERT(isValidKeyType(keyType));
941
942         if (txPower > 63)
943                 txPower = 63;
944
945         ads->ds_ctl0 = (pktLen & AR_FrameLen)
946                 | (flags & HAL_TXDESC_VMF ? AR_VirtMoreFrag : 0)
947                 | SM(txPower, AR_XmitPower)
948                 | (flags & HAL_TXDESC_RTSENA ? AR_RTSEnable : 0)
949                 | (flags & HAL_TXDESC_VEOL ? AR_VEOL : 0)
950                 | (flags & HAL_TXDESC_CLRDMASK ? AR_ClrDestMask : 0)
951                 | (flags & HAL_TXDESC_INTREQ ? AR_TxIntrReq : 0)
952                 | (keyIx != HAL_TXKEYIX_INVALID ? AR_DestIdxValid : 0)
953                 | (flags & HAL_TXDESC_CTSENA ? AR_CTSEnable : 0);
954
955         ads->ds_ctl1 = (keyIx != HAL_TXKEYIX_INVALID ? SM(keyIx, AR_DestIdx) : 0)
956                 | SM(type, AR_FrameType)
957                 | (flags & HAL_TXDESC_NOACK ? AR_NoAck : 0)
958                 | (flags & HAL_TXDESC_EXT_ONLY ? AR_ExtOnly : 0)
959                 | (flags & HAL_TXDESC_EXT_AND_CTL ? AR_ExtAndCtl : 0);
960
961         ads->ds_ctl6 = SM(keyType, AR_EncrType);
962 }
963
964 #ifdef MAGPIE_MERLIN
965
966 void ar5416Set11nRateScenario_20(struct ath_hal *ah, struct ath_tx_desc *ds,
967                                  a_uint32_t durUpdateEn, a_uint32_t rtsctsRate,
968                                  a_uint32_t rtsctsDuration,
969                                  HAL_11N_RATE_SERIES series[], a_uint32_t nseries,
970                                  a_uint32_t flags)
971 {
972         struct ar5416_desc *ads = AR5416DESC(ds);
973         a_uint32_t ds_ctl0;
974
975         HALASSERT(nseries == 4);
976         (void)nseries;
977
978         /*
979          * Rate control settings override
980          */
981         ds_ctl0 = ads->ds_ctl0;
982
983         if (flags & (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) {
984                 if (flags & HAL_TXDESC_RTSENA) {
985                         ds_ctl0 &= ~AR_CTSEnable;
986                         ds_ctl0 |= AR_RTSEnable;
987                 } else {
988                         ds_ctl0 &= ~AR_RTSEnable;
989                         ds_ctl0 |= AR_CTSEnable;
990                 }
991         } else {
992                 ds_ctl0 = (ds_ctl0 & ~(AR_RTSEnable | AR_CTSEnable));
993         }
994
995         ads->ds_ctl0 = ds_ctl0;
996
997         ads->ds_ctl2 = set11nTries(series, 0)
998                 |  set11nTries(series, 1)
999                 |  set11nTries(series, 2)
1000                 |  set11nTries(series, 3)
1001                 |  (durUpdateEn ? AR_DurUpdateEn : 0);
1002
1003         ads->ds_ctl3 = set11nRate(series, 0)
1004                 |  set11nRate(series, 1)
1005                 |  set11nRate(series, 2)
1006                 |  set11nRate(series, 3);
1007
1008         ads->ds_ctl4 = set11nPktDurRTSCTS(series, 0)
1009                 |  set11nPktDurRTSCTS(series, 1);
1010
1011         ads->ds_ctl5 = set11nPktDurRTSCTS(series, 2)
1012                 |  set11nPktDurRTSCTS(series, 3);
1013
1014         ads->ds_ctl7 = set11nRateFlags(series, 0)
1015                 |  set11nRateFlags(series, 1)
1016                 |  set11nRateFlags(series, 2)
1017                 |  set11nRateFlags(series, 3)
1018                 | SM(rtsctsRate, AR_RTSCTSRate);
1019 }
1020
1021 #else
1022
1023 void ar5416Set11nRateScenario_20(struct ath_hal *ah, struct ath_tx_desc *ds,
1024                                  a_uint32_t durUpdateEn, a_uint32_t rtsctsRate,
1025                                  a_uint32_t rtsctsDuration,
1026                                  HAL_11N_RATE_SERIES series[], a_uint32_t nseries,
1027                                  a_uint32_t flags)
1028 {
1029         struct ar5416_desc *ads = AR5416DESC(ds);
1030         a_uint32_t ds_ctl0;
1031
1032         HALASSERT(nseries == 4);
1033         (void)nseries;
1034
1035         /*
1036          * Rate control settings override
1037          */
1038         if (flags & (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) {
1039                 ds_ctl0 = ads->ds_ctl0;
1040
1041                 if (flags & HAL_TXDESC_RTSENA) {
1042                         ds_ctl0 &= ~AR_CTSEnable;
1043                         ds_ctl0 |= AR_RTSEnable;
1044                 } else {
1045                         ds_ctl0 &= ~AR_RTSEnable;
1046                         ds_ctl0 |= AR_CTSEnable;
1047                 }
1048
1049                 ads->ds_ctl0 = ds_ctl0;
1050         }
1051
1052         ads->ds_ctl2 = set11nTries(series, 0)
1053                 |  set11nTries(series, 1)
1054                 |  set11nTries(series, 2)
1055                 |  set11nTries(series, 3)
1056                 |  (durUpdateEn ? AR_DurUpdateEn : 0);
1057
1058         ads->ds_ctl3 = set11nRate(series, 0)
1059                 |  set11nRate(series, 1)
1060                 |  set11nRate(series, 2)
1061                 |  set11nRate(series, 3);
1062
1063         ads->ds_ctl4 = set11nPktDurRTSCTS(series, 0)
1064                 |  set11nPktDurRTSCTS(series, 1);
1065
1066         ads->ds_ctl5 = set11nPktDurRTSCTS(series, 2)
1067                 |  set11nPktDurRTSCTS(series, 3);
1068
1069         ads->ds_ctl7 = set11nRateFlags(series, 0)
1070                 |  set11nRateFlags(series, 1)
1071                 |  set11nRateFlags(series, 2)
1072                 |  set11nRateFlags(series, 3)
1073                 | SM(rtsctsRate, AR_RTSCTSRate);
1074 }
1075
1076 #endif
1077
1078 void ar5416Set11nAggrFirst_20(struct ath_hal *ah, struct ath_tx_desc *ds, a_uint32_t aggrLen,
1079                               a_uint32_t numDelims)
1080 {
1081         struct ar5416_desc *ads = AR5416DESC(ds);
1082
1083         ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
1084
1085         ads->ds_ctl6 &= ~(AR_AggrLen | AR_PadDelim);
1086         ads->ds_ctl6 |= SM(aggrLen, AR_AggrLen) |
1087                 SM(numDelims, AR_PadDelim);
1088 }
1089
1090 void ar5416Set11nAggrMiddle_20(struct ath_hal *ah, struct ath_tx_desc *ds, a_uint32_t numDelims)
1091 {
1092         struct ar5416_desc *ads = AR5416DESC(ds);
1093         a_uint32_t ctl6;
1094
1095         ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
1096
1097         /*
1098          * We use a stack variable to manipulate ctl6 to reduce uncached 
1099          * read modify, modfiy, write.
1100          */
1101         ctl6 = ads->ds_ctl6;
1102         ctl6 &= ~AR_PadDelim;
1103         ctl6 |= SM(numDelims, AR_PadDelim);
1104         ads->ds_ctl6 = ctl6;
1105 }
1106
1107 void ar5416Set11nAggrLast_20(struct ath_hal *ah, struct ath_tx_desc *ds)
1108 {
1109         struct ar5416_desc *ads = AR5416DESC(ds);
1110
1111         ads->ds_ctl1 |= AR_IsAggr;
1112         ads->ds_ctl1 &= ~AR_MoreAggr;
1113         ads->ds_ctl6 &= ~AR_PadDelim;
1114 }
1115
1116 void ar5416Clr11nAggr_20(struct ath_hal *ah, struct ath_tx_desc *ds)
1117 {
1118         struct ar5416_desc *ads = AR5416DESC(ds);
1119
1120         ads->ds_ctl1 &= (~AR_IsAggr & ~AR_MoreAggr);
1121 }
1122
1123 void ar5416Set11nBurstDuration_20(struct ath_hal *ah, struct ath_tx_desc *ds,
1124                                   a_uint32_t burstDuration)
1125 {
1126         struct ar5416_desc *ads = AR5416DESC(ds);
1127
1128         ads->ds_ctl2 &= ~AR_BurstDur;
1129         ads->ds_ctl2 |= SM(burstDuration, AR_BurstDur);
1130 }
1131
1132 void ar5416Set11nVirtualMoreFrag_20(struct ath_hal *ah, struct ath_tx_desc *ds,
1133                                     a_uint32_t vmf)
1134 {
1135         struct ar5416_desc *ads = AR5416DESC(ds);
1136
1137         if (vmf) {
1138                 ads->ds_ctl0 |= AR_VirtMoreFrag;
1139         } else {
1140                 ads->ds_ctl0 &= ~AR_VirtMoreFrag;
1141         }
1142 }