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