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