4a36ec53f7320ccde9df38d698a499ca85e3ca09
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath9k / mac.c
1 /*
2  * Copyright (c) 2008-2009 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "hw.h"
18
19 static void ar9002_hw_rx_enable(struct ath_hw *ah)
20 {
21         REG_WRITE(ah, AR_CR, AR_CR_RXE);
22 }
23
24 static void ar9002_hw_set_desc_link(void *ds, u32 ds_link)
25 {
26         ((struct ath_desc *) ds)->ds_link = ds_link;
27 }
28
29 static void ar9002_hw_get_desc_link(void *ds, u32 **ds_link)
30 {
31         *ds_link = &((struct ath_desc *)ds)->ds_link;
32 }
33
34 static bool ar9002_hw_get_isr(struct ath_hw *ah, enum ath9k_int *masked)
35 {
36         u32 isr = 0;
37         u32 mask2 = 0;
38         struct ath9k_hw_capabilities *pCap = &ah->caps;
39         u32 sync_cause = 0;
40         bool fatal_int = false;
41         struct ath_common *common = ath9k_hw_common(ah);
42
43         if (!AR_SREV_9100(ah)) {
44                 if (REG_READ(ah, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) {
45                         if ((REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M)
46                             == AR_RTC_STATUS_ON) {
47                                 isr = REG_READ(ah, AR_ISR);
48                         }
49                 }
50
51                 sync_cause = REG_READ(ah, AR_INTR_SYNC_CAUSE) &
52                         AR_INTR_SYNC_DEFAULT;
53
54                 *masked = 0;
55
56                 if (!isr && !sync_cause)
57                         return false;
58         } else {
59                 *masked = 0;
60                 isr = REG_READ(ah, AR_ISR);
61         }
62
63         if (isr) {
64                 if (isr & AR_ISR_BCNMISC) {
65                         u32 isr2;
66                         isr2 = REG_READ(ah, AR_ISR_S2);
67                         if (isr2 & AR_ISR_S2_TIM)
68                                 mask2 |= ATH9K_INT_TIM;
69                         if (isr2 & AR_ISR_S2_DTIM)
70                                 mask2 |= ATH9K_INT_DTIM;
71                         if (isr2 & AR_ISR_S2_DTIMSYNC)
72                                 mask2 |= ATH9K_INT_DTIMSYNC;
73                         if (isr2 & (AR_ISR_S2_CABEND))
74                                 mask2 |= ATH9K_INT_CABEND;
75                         if (isr2 & AR_ISR_S2_GTT)
76                                 mask2 |= ATH9K_INT_GTT;
77                         if (isr2 & AR_ISR_S2_CST)
78                                 mask2 |= ATH9K_INT_CST;
79                         if (isr2 & AR_ISR_S2_TSFOOR)
80                                 mask2 |= ATH9K_INT_TSFOOR;
81                 }
82
83                 isr = REG_READ(ah, AR_ISR_RAC);
84                 if (isr == 0xffffffff) {
85                         *masked = 0;
86                         return false;
87                 }
88
89                 *masked = isr & ATH9K_INT_COMMON;
90
91                 if (ah->config.rx_intr_mitigation) {
92                         if (isr & (AR_ISR_RXMINTR | AR_ISR_RXINTM))
93                                 *masked |= ATH9K_INT_RX;
94                 }
95
96                 if (isr & (AR_ISR_RXOK | AR_ISR_RXERR))
97                         *masked |= ATH9K_INT_RX;
98                 if (isr &
99                     (AR_ISR_TXOK | AR_ISR_TXDESC | AR_ISR_TXERR |
100                      AR_ISR_TXEOL)) {
101                         u32 s0_s, s1_s;
102
103                         *masked |= ATH9K_INT_TX;
104
105                         s0_s = REG_READ(ah, AR_ISR_S0_S);
106                         ah->intr_txqs |= MS(s0_s, AR_ISR_S0_QCU_TXOK);
107                         ah->intr_txqs |= MS(s0_s, AR_ISR_S0_QCU_TXDESC);
108
109                         s1_s = REG_READ(ah, AR_ISR_S1_S);
110                         ah->intr_txqs |= MS(s1_s, AR_ISR_S1_QCU_TXERR);
111                         ah->intr_txqs |= MS(s1_s, AR_ISR_S1_QCU_TXEOL);
112                 }
113
114                 if (isr & AR_ISR_RXORN) {
115                         ath_print(common, ATH_DBG_INTERRUPT,
116                                   "receive FIFO overrun interrupt\n");
117                 }
118
119                 if (!AR_SREV_9100(ah)) {
120                         if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
121                                 u32 isr5 = REG_READ(ah, AR_ISR_S5_S);
122                                 if (isr5 & AR_ISR_S5_TIM_TIMER)
123                                         *masked |= ATH9K_INT_TIM_TIMER;
124                         }
125                 }
126
127                 *masked |= mask2;
128         }
129
130         if (AR_SREV_9100(ah))
131                 return true;
132
133         if (isr & AR_ISR_GENTMR) {
134                 u32 s5_s;
135
136                 s5_s = REG_READ(ah, AR_ISR_S5_S);
137                 if (isr & AR_ISR_GENTMR) {
138                         ah->intr_gen_timer_trigger =
139                                 MS(s5_s, AR_ISR_S5_GENTIMER_TRIG);
140
141                         ah->intr_gen_timer_thresh =
142                                 MS(s5_s, AR_ISR_S5_GENTIMER_THRESH);
143
144                         if (ah->intr_gen_timer_trigger)
145                                 *masked |= ATH9K_INT_GENTIMER;
146
147                 }
148         }
149
150         if (sync_cause) {
151                 fatal_int =
152                         (sync_cause &
153                          (AR_INTR_SYNC_HOST1_FATAL | AR_INTR_SYNC_HOST1_PERR))
154                         ? true : false;
155
156                 if (fatal_int) {
157                         if (sync_cause & AR_INTR_SYNC_HOST1_FATAL) {
158                                 ath_print(common, ATH_DBG_ANY,
159                                           "received PCI FATAL interrupt\n");
160                         }
161                         if (sync_cause & AR_INTR_SYNC_HOST1_PERR) {
162                                 ath_print(common, ATH_DBG_ANY,
163                                           "received PCI PERR interrupt\n");
164                         }
165                         *masked |= ATH9K_INT_FATAL;
166                 }
167                 if (sync_cause & AR_INTR_SYNC_RADM_CPL_TIMEOUT) {
168                         ath_print(common, ATH_DBG_INTERRUPT,
169                                   "AR_INTR_SYNC_RADM_CPL_TIMEOUT\n");
170                         REG_WRITE(ah, AR_RC, AR_RC_HOSTIF);
171                         REG_WRITE(ah, AR_RC, 0);
172                         *masked |= ATH9K_INT_FATAL;
173                 }
174                 if (sync_cause & AR_INTR_SYNC_LOCAL_TIMEOUT) {
175                         ath_print(common, ATH_DBG_INTERRUPT,
176                                   "AR_INTR_SYNC_LOCAL_TIMEOUT\n");
177                 }
178
179                 REG_WRITE(ah, AR_INTR_SYNC_CAUSE_CLR, sync_cause);
180                 (void) REG_READ(ah, AR_INTR_SYNC_CAUSE_CLR);
181         }
182
183         return true;
184 }
185
186 void ar9002_hw_attach_mac_ops(struct ath_hw *ah)
187 {
188         struct ath_hw_ops *ops = ath9k_hw_ops(ah);
189
190         ops->rx_enable = ar9002_hw_rx_enable;
191         ops->set_desc_link = ar9002_hw_set_desc_link;
192         ops->get_desc_link = ar9002_hw_get_desc_link;
193         ops->get_isr = ar9002_hw_get_isr;
194 }
195
196 static void ath9k_hw_set_txq_interrupts(struct ath_hw *ah,
197                                         struct ath9k_tx_queue_info *qi)
198 {
199         ath_print(ath9k_hw_common(ah), ATH_DBG_INTERRUPT,
200                   "tx ok 0x%x err 0x%x desc 0x%x eol 0x%x urn 0x%x\n",
201                   ah->txok_interrupt_mask, ah->txerr_interrupt_mask,
202                   ah->txdesc_interrupt_mask, ah->txeol_interrupt_mask,
203                   ah->txurn_interrupt_mask);
204
205         REG_WRITE(ah, AR_IMR_S0,
206                   SM(ah->txok_interrupt_mask, AR_IMR_S0_QCU_TXOK)
207                   | SM(ah->txdesc_interrupt_mask, AR_IMR_S0_QCU_TXDESC));
208         REG_WRITE(ah, AR_IMR_S1,
209                   SM(ah->txerr_interrupt_mask, AR_IMR_S1_QCU_TXERR)
210                   | SM(ah->txeol_interrupt_mask, AR_IMR_S1_QCU_TXEOL));
211
212         ah->imrs2_reg &= ~AR_IMR_S2_QCU_TXURN;
213         ah->imrs2_reg |= (ah->txurn_interrupt_mask & AR_IMR_S2_QCU_TXURN);
214         REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
215 }
216
217 u32 ath9k_hw_gettxbuf(struct ath_hw *ah, u32 q)
218 {
219         return REG_READ(ah, AR_QTXDP(q));
220 }
221 EXPORT_SYMBOL(ath9k_hw_gettxbuf);
222
223 void ath9k_hw_puttxbuf(struct ath_hw *ah, u32 q, u32 txdp)
224 {
225         REG_WRITE(ah, AR_QTXDP(q), txdp);
226 }
227 EXPORT_SYMBOL(ath9k_hw_puttxbuf);
228
229 void ath9k_hw_txstart(struct ath_hw *ah, u32 q)
230 {
231         ath_print(ath9k_hw_common(ah), ATH_DBG_QUEUE,
232                   "Enable TXE on queue: %u\n", q);
233         REG_WRITE(ah, AR_Q_TXE, 1 << q);
234 }
235 EXPORT_SYMBOL(ath9k_hw_txstart);
236
237 u32 ath9k_hw_numtxpending(struct ath_hw *ah, u32 q)
238 {
239         u32 npend;
240
241         npend = REG_READ(ah, AR_QSTS(q)) & AR_Q_STS_PEND_FR_CNT;
242         if (npend == 0) {
243
244                 if (REG_READ(ah, AR_Q_TXE) & (1 << q))
245                         npend = 1;
246         }
247
248         return npend;
249 }
250 EXPORT_SYMBOL(ath9k_hw_numtxpending);
251
252 /**
253  * ath9k_hw_updatetxtriglevel - adjusts the frame trigger level
254  *
255  * @ah: atheros hardware struct
256  * @bIncTrigLevel: whether or not the frame trigger level should be updated
257  *
258  * The frame trigger level specifies the minimum number of bytes,
259  * in units of 64 bytes, that must be DMA'ed into the PCU TX FIFO
260  * before the PCU will initiate sending the frame on the air. This can
261  * mean we initiate transmit before a full frame is on the PCU TX FIFO.
262  * Resets to 0x1 (meaning 64 bytes or a full frame, whichever occurs
263  * first)
264  *
265  * Caution must be taken to ensure to set the frame trigger level based
266  * on the DMA request size. For example if the DMA request size is set to
267  * 128 bytes the trigger level cannot exceed 6 * 64 = 384. This is because
268  * there need to be enough space in the tx FIFO for the requested transfer
269  * size. Hence the tx FIFO will stop with 512 - 128 = 384 bytes. If we set
270  * the threshold to a value beyond 6, then the transmit will hang.
271  *
272  * Current dual   stream devices have a PCU TX FIFO size of 8 KB.
273  * Current single stream devices have a PCU TX FIFO size of 4 KB, however,
274  * there is a hardware issue which forces us to use 2 KB instead so the
275  * frame trigger level must not exceed 2 KB for these chipsets.
276  */
277 bool ath9k_hw_updatetxtriglevel(struct ath_hw *ah, bool bIncTrigLevel)
278 {
279         u32 txcfg, curLevel, newLevel;
280         enum ath9k_int omask;
281
282         if (ah->tx_trig_level >= ah->config.max_txtrig_level)
283                 return false;
284
285         omask = ath9k_hw_set_interrupts(ah, ah->imask & ~ATH9K_INT_GLOBAL);
286
287         txcfg = REG_READ(ah, AR_TXCFG);
288         curLevel = MS(txcfg, AR_FTRIG);
289         newLevel = curLevel;
290         if (bIncTrigLevel) {
291                 if (curLevel < ah->config.max_txtrig_level)
292                         newLevel++;
293         } else if (curLevel > MIN_TX_FIFO_THRESHOLD)
294                 newLevel--;
295         if (newLevel != curLevel)
296                 REG_WRITE(ah, AR_TXCFG,
297                           (txcfg & ~AR_FTRIG) | SM(newLevel, AR_FTRIG));
298
299         ath9k_hw_set_interrupts(ah, omask);
300
301         ah->tx_trig_level = newLevel;
302
303         return newLevel != curLevel;
304 }
305 EXPORT_SYMBOL(ath9k_hw_updatetxtriglevel);
306
307 bool ath9k_hw_stoptxdma(struct ath_hw *ah, u32 q)
308 {
309 #define ATH9K_TX_STOP_DMA_TIMEOUT       4000    /* usec */
310 #define ATH9K_TIME_QUANTUM              100     /* usec */
311         struct ath_common *common = ath9k_hw_common(ah);
312         struct ath9k_hw_capabilities *pCap = &ah->caps;
313         struct ath9k_tx_queue_info *qi;
314         u32 tsfLow, j, wait;
315         u32 wait_time = ATH9K_TX_STOP_DMA_TIMEOUT / ATH9K_TIME_QUANTUM;
316
317         if (q >= pCap->total_queues) {
318                 ath_print(common, ATH_DBG_QUEUE, "Stopping TX DMA, "
319                           "invalid queue: %u\n", q);
320                 return false;
321         }
322
323         qi = &ah->txq[q];
324         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
325                 ath_print(common, ATH_DBG_QUEUE, "Stopping TX DMA, "
326                           "inactive queue: %u\n", q);
327                 return false;
328         }
329
330         REG_WRITE(ah, AR_Q_TXD, 1 << q);
331
332         for (wait = wait_time; wait != 0; wait--) {
333                 if (ath9k_hw_numtxpending(ah, q) == 0)
334                         break;
335                 udelay(ATH9K_TIME_QUANTUM);
336         }
337
338         if (ath9k_hw_numtxpending(ah, q)) {
339                 ath_print(common, ATH_DBG_QUEUE,
340                           "%s: Num of pending TX Frames %d on Q %d\n",
341                           __func__, ath9k_hw_numtxpending(ah, q), q);
342
343                 for (j = 0; j < 2; j++) {
344                         tsfLow = REG_READ(ah, AR_TSF_L32);
345                         REG_WRITE(ah, AR_QUIET2,
346                                   SM(10, AR_QUIET2_QUIET_DUR));
347                         REG_WRITE(ah, AR_QUIET_PERIOD, 100);
348                         REG_WRITE(ah, AR_NEXT_QUIET_TIMER, tsfLow >> 10);
349                         REG_SET_BIT(ah, AR_TIMER_MODE,
350                                        AR_QUIET_TIMER_EN);
351
352                         if ((REG_READ(ah, AR_TSF_L32) >> 10) == (tsfLow >> 10))
353                                 break;
354
355                         ath_print(common, ATH_DBG_QUEUE,
356                                   "TSF has moved while trying to set "
357                                   "quiet time TSF: 0x%08x\n", tsfLow);
358                 }
359
360                 REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
361
362                 udelay(200);
363                 REG_CLR_BIT(ah, AR_TIMER_MODE, AR_QUIET_TIMER_EN);
364
365                 wait = wait_time;
366                 while (ath9k_hw_numtxpending(ah, q)) {
367                         if ((--wait) == 0) {
368                                 ath_print(common, ATH_DBG_FATAL,
369                                           "Failed to stop TX DMA in 100 "
370                                           "msec after killing last frame\n");
371                                 break;
372                         }
373                         udelay(ATH9K_TIME_QUANTUM);
374                 }
375
376                 REG_CLR_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
377         }
378
379         REG_WRITE(ah, AR_Q_TXD, 0);
380         return wait != 0;
381
382 #undef ATH9K_TX_STOP_DMA_TIMEOUT
383 #undef ATH9K_TIME_QUANTUM
384 }
385 EXPORT_SYMBOL(ath9k_hw_stoptxdma);
386
387 void ath9k_hw_filltxdesc(struct ath_hw *ah, struct ath_desc *ds,
388                          u32 segLen, bool firstSeg,
389                          bool lastSeg, const struct ath_desc *ds0,
390                          dma_addr_t buf_addr)
391 {
392         struct ar5416_desc *ads = AR5416DESC(ds);
393
394         ads->ds_data = buf_addr;
395
396         if (firstSeg) {
397                 ads->ds_ctl1 |= segLen | (lastSeg ? 0 : AR_TxMore);
398         } else if (lastSeg) {
399                 ads->ds_ctl0 = 0;
400                 ads->ds_ctl1 = segLen;
401                 ads->ds_ctl2 = AR5416DESC_CONST(ds0)->ds_ctl2;
402                 ads->ds_ctl3 = AR5416DESC_CONST(ds0)->ds_ctl3;
403         } else {
404                 ads->ds_ctl0 = 0;
405                 ads->ds_ctl1 = segLen | AR_TxMore;
406                 ads->ds_ctl2 = 0;
407                 ads->ds_ctl3 = 0;
408         }
409         ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
410         ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
411         ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
412         ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
413         ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
414 }
415 EXPORT_SYMBOL(ath9k_hw_filltxdesc);
416
417 void ath9k_hw_cleartxdesc(struct ath_hw *ah, struct ath_desc *ds)
418 {
419         struct ar5416_desc *ads = AR5416DESC(ds);
420
421         ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
422         ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
423         ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
424         ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
425         ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
426 }
427 EXPORT_SYMBOL(ath9k_hw_cleartxdesc);
428
429 int ath9k_hw_txprocdesc(struct ath_hw *ah, struct ath_desc *ds,
430                         struct ath_tx_status *ts)
431 {
432         struct ar5416_desc *ads = AR5416DESC(ds);
433
434         if ((ads->ds_txstatus9 & AR_TxDone) == 0)
435                 return -EINPROGRESS;
436
437         ts->ts_seqnum = MS(ads->ds_txstatus9, AR_SeqNum);
438         ts->ts_tstamp = ads->AR_SendTimestamp;
439         ts->ts_status = 0;
440         ts->ts_flags = 0;
441
442         if (ads->ds_txstatus1 & AR_FrmXmitOK)
443                 ts->ts_status |= ATH9K_TX_ACKED;
444         if (ads->ds_txstatus1 & AR_ExcessiveRetries)
445                 ts->ts_status |= ATH9K_TXERR_XRETRY;
446         if (ads->ds_txstatus1 & AR_Filtered)
447                 ts->ts_status |= ATH9K_TXERR_FILT;
448         if (ads->ds_txstatus1 & AR_FIFOUnderrun) {
449                 ts->ts_status |= ATH9K_TXERR_FIFO;
450                 ath9k_hw_updatetxtriglevel(ah, true);
451         }
452         if (ads->ds_txstatus9 & AR_TxOpExceeded)
453                 ts->ts_status |= ATH9K_TXERR_XTXOP;
454         if (ads->ds_txstatus1 & AR_TxTimerExpired)
455                 ts->ts_status |= ATH9K_TXERR_TIMER_EXPIRED;
456
457         if (ads->ds_txstatus1 & AR_DescCfgErr)
458                 ts->ts_flags |= ATH9K_TX_DESC_CFG_ERR;
459         if (ads->ds_txstatus1 & AR_TxDataUnderrun) {
460                 ts->ts_flags |= ATH9K_TX_DATA_UNDERRUN;
461                 ath9k_hw_updatetxtriglevel(ah, true);
462         }
463         if (ads->ds_txstatus1 & AR_TxDelimUnderrun) {
464                 ts->ts_flags |= ATH9K_TX_DELIM_UNDERRUN;
465                 ath9k_hw_updatetxtriglevel(ah, true);
466         }
467         if (ads->ds_txstatus0 & AR_TxBaStatus) {
468                 ts->ts_flags |= ATH9K_TX_BA;
469                 ts->ba_low = ads->AR_BaBitmapLow;
470                 ts->ba_high = ads->AR_BaBitmapHigh;
471         }
472
473         ts->ts_rateindex = MS(ads->ds_txstatus9, AR_FinalTxIdx);
474         switch (ts->ts_rateindex) {
475         case 0:
476                 ts->ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate0);
477                 break;
478         case 1:
479                 ts->ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate1);
480                 break;
481         case 2:
482                 ts->ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate2);
483                 break;
484         case 3:
485                 ts->ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate3);
486                 break;
487         }
488
489         ts->ts_rssi = MS(ads->ds_txstatus5, AR_TxRSSICombined);
490         ts->ts_rssi_ctl0 = MS(ads->ds_txstatus0, AR_TxRSSIAnt00);
491         ts->ts_rssi_ctl1 = MS(ads->ds_txstatus0, AR_TxRSSIAnt01);
492         ts->ts_rssi_ctl2 = MS(ads->ds_txstatus0, AR_TxRSSIAnt02);
493         ts->ts_rssi_ext0 = MS(ads->ds_txstatus5, AR_TxRSSIAnt10);
494         ts->ts_rssi_ext1 = MS(ads->ds_txstatus5, AR_TxRSSIAnt11);
495         ts->ts_rssi_ext2 = MS(ads->ds_txstatus5, AR_TxRSSIAnt12);
496         ts->evm0 = ads->AR_TxEVM0;
497         ts->evm1 = ads->AR_TxEVM1;
498         ts->evm2 = ads->AR_TxEVM2;
499         ts->ts_shortretry = MS(ads->ds_txstatus1, AR_RTSFailCnt);
500         ts->ts_longretry = MS(ads->ds_txstatus1, AR_DataFailCnt);
501         ts->ts_virtcol = MS(ads->ds_txstatus1, AR_VirtRetryCnt);
502         ts->ts_antenna = 0;
503
504         return 0;
505 }
506 EXPORT_SYMBOL(ath9k_hw_txprocdesc);
507
508 void ath9k_hw_set11n_txdesc(struct ath_hw *ah, struct ath_desc *ds,
509                             u32 pktLen, enum ath9k_pkt_type type, u32 txPower,
510                             u32 keyIx, enum ath9k_key_type keyType, u32 flags)
511 {
512         struct ar5416_desc *ads = AR5416DESC(ds);
513
514         txPower += ah->txpower_indexoffset;
515         if (txPower > 63)
516                 txPower = 63;
517
518         ads->ds_ctl0 = (pktLen & AR_FrameLen)
519                 | (flags & ATH9K_TXDESC_VMF ? AR_VirtMoreFrag : 0)
520                 | SM(txPower, AR_XmitPower)
521                 | (flags & ATH9K_TXDESC_VEOL ? AR_VEOL : 0)
522                 | (flags & ATH9K_TXDESC_CLRDMASK ? AR_ClrDestMask : 0)
523                 | (flags & ATH9K_TXDESC_INTREQ ? AR_TxIntrReq : 0)
524                 | (keyIx != ATH9K_TXKEYIX_INVALID ? AR_DestIdxValid : 0);
525
526         ads->ds_ctl1 =
527                 (keyIx != ATH9K_TXKEYIX_INVALID ? SM(keyIx, AR_DestIdx) : 0)
528                 | SM(type, AR_FrameType)
529                 | (flags & ATH9K_TXDESC_NOACK ? AR_NoAck : 0)
530                 | (flags & ATH9K_TXDESC_EXT_ONLY ? AR_ExtOnly : 0)
531                 | (flags & ATH9K_TXDESC_EXT_AND_CTL ? AR_ExtAndCtl : 0);
532
533         ads->ds_ctl6 = SM(keyType, AR_EncrType);
534
535         if (AR_SREV_9285(ah) || AR_SREV_9271(ah)) {
536                 ads->ds_ctl8 = 0;
537                 ads->ds_ctl9 = 0;
538                 ads->ds_ctl10 = 0;
539                 ads->ds_ctl11 = 0;
540         }
541 }
542 EXPORT_SYMBOL(ath9k_hw_set11n_txdesc);
543
544 void ath9k_hw_set11n_ratescenario(struct ath_hw *ah, struct ath_desc *ds,
545                                   struct ath_desc *lastds,
546                                   u32 durUpdateEn, u32 rtsctsRate,
547                                   u32 rtsctsDuration,
548                                   struct ath9k_11n_rate_series series[],
549                                   u32 nseries, u32 flags)
550 {
551         struct ar5416_desc *ads = AR5416DESC(ds);
552         struct ar5416_desc *last_ads = AR5416DESC(lastds);
553         u32 ds_ctl0;
554
555         if (flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA)) {
556                 ds_ctl0 = ads->ds_ctl0;
557
558                 if (flags & ATH9K_TXDESC_RTSENA) {
559                         ds_ctl0 &= ~AR_CTSEnable;
560                         ds_ctl0 |= AR_RTSEnable;
561                 } else {
562                         ds_ctl0 &= ~AR_RTSEnable;
563                         ds_ctl0 |= AR_CTSEnable;
564                 }
565
566                 ads->ds_ctl0 = ds_ctl0;
567         } else {
568                 ads->ds_ctl0 =
569                         (ads->ds_ctl0 & ~(AR_RTSEnable | AR_CTSEnable));
570         }
571
572         ads->ds_ctl2 = set11nTries(series, 0)
573                 | set11nTries(series, 1)
574                 | set11nTries(series, 2)
575                 | set11nTries(series, 3)
576                 | (durUpdateEn ? AR_DurUpdateEna : 0)
577                 | SM(0, AR_BurstDur);
578
579         ads->ds_ctl3 = set11nRate(series, 0)
580                 | set11nRate(series, 1)
581                 | set11nRate(series, 2)
582                 | set11nRate(series, 3);
583
584         ads->ds_ctl4 = set11nPktDurRTSCTS(series, 0)
585                 | set11nPktDurRTSCTS(series, 1);
586
587         ads->ds_ctl5 = set11nPktDurRTSCTS(series, 2)
588                 | set11nPktDurRTSCTS(series, 3);
589
590         ads->ds_ctl7 = set11nRateFlags(series, 0)
591                 | set11nRateFlags(series, 1)
592                 | set11nRateFlags(series, 2)
593                 | set11nRateFlags(series, 3)
594                 | SM(rtsctsRate, AR_RTSCTSRate);
595         last_ads->ds_ctl2 = ads->ds_ctl2;
596         last_ads->ds_ctl3 = ads->ds_ctl3;
597 }
598 EXPORT_SYMBOL(ath9k_hw_set11n_ratescenario);
599
600 void ath9k_hw_set11n_aggr_first(struct ath_hw *ah, struct ath_desc *ds,
601                                 u32 aggrLen)
602 {
603         struct ar5416_desc *ads = AR5416DESC(ds);
604
605         ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
606         ads->ds_ctl6 &= ~AR_AggrLen;
607         ads->ds_ctl6 |= SM(aggrLen, AR_AggrLen);
608 }
609 EXPORT_SYMBOL(ath9k_hw_set11n_aggr_first);
610
611 void ath9k_hw_set11n_aggr_middle(struct ath_hw *ah, struct ath_desc *ds,
612                                  u32 numDelims)
613 {
614         struct ar5416_desc *ads = AR5416DESC(ds);
615         unsigned int ctl6;
616
617         ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
618
619         ctl6 = ads->ds_ctl6;
620         ctl6 &= ~AR_PadDelim;
621         ctl6 |= SM(numDelims, AR_PadDelim);
622         ads->ds_ctl6 = ctl6;
623 }
624 EXPORT_SYMBOL(ath9k_hw_set11n_aggr_middle);
625
626 void ath9k_hw_set11n_aggr_last(struct ath_hw *ah, struct ath_desc *ds)
627 {
628         struct ar5416_desc *ads = AR5416DESC(ds);
629
630         ads->ds_ctl1 |= AR_IsAggr;
631         ads->ds_ctl1 &= ~AR_MoreAggr;
632         ads->ds_ctl6 &= ~AR_PadDelim;
633 }
634 EXPORT_SYMBOL(ath9k_hw_set11n_aggr_last);
635
636 void ath9k_hw_clr11n_aggr(struct ath_hw *ah, struct ath_desc *ds)
637 {
638         struct ar5416_desc *ads = AR5416DESC(ds);
639
640         ads->ds_ctl1 &= (~AR_IsAggr & ~AR_MoreAggr);
641 }
642 EXPORT_SYMBOL(ath9k_hw_clr11n_aggr);
643
644 void ath9k_hw_set11n_burstduration(struct ath_hw *ah, struct ath_desc *ds,
645                                    u32 burstDuration)
646 {
647         struct ar5416_desc *ads = AR5416DESC(ds);
648
649         ads->ds_ctl2 &= ~AR_BurstDur;
650         ads->ds_ctl2 |= SM(burstDuration, AR_BurstDur);
651 }
652 EXPORT_SYMBOL(ath9k_hw_set11n_burstduration);
653
654 void ath9k_hw_set11n_virtualmorefrag(struct ath_hw *ah, struct ath_desc *ds,
655                                      u32 vmf)
656 {
657         struct ar5416_desc *ads = AR5416DESC(ds);
658
659         if (vmf)
660                 ads->ds_ctl0 |= AR_VirtMoreFrag;
661         else
662                 ads->ds_ctl0 &= ~AR_VirtMoreFrag;
663 }
664
665 void ath9k_hw_gettxintrtxqs(struct ath_hw *ah, u32 *txqs)
666 {
667         *txqs &= ah->intr_txqs;
668         ah->intr_txqs &= ~(*txqs);
669 }
670 EXPORT_SYMBOL(ath9k_hw_gettxintrtxqs);
671
672 bool ath9k_hw_set_txq_props(struct ath_hw *ah, int q,
673                             const struct ath9k_tx_queue_info *qinfo)
674 {
675         u32 cw;
676         struct ath_common *common = ath9k_hw_common(ah);
677         struct ath9k_hw_capabilities *pCap = &ah->caps;
678         struct ath9k_tx_queue_info *qi;
679
680         if (q >= pCap->total_queues) {
681                 ath_print(common, ATH_DBG_QUEUE, "Set TXQ properties, "
682                           "invalid queue: %u\n", q);
683                 return false;
684         }
685
686         qi = &ah->txq[q];
687         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
688                 ath_print(common, ATH_DBG_QUEUE, "Set TXQ properties, "
689                           "inactive queue: %u\n", q);
690                 return false;
691         }
692
693         ath_print(common, ATH_DBG_QUEUE, "Set queue properties for: %u\n", q);
694
695         qi->tqi_ver = qinfo->tqi_ver;
696         qi->tqi_subtype = qinfo->tqi_subtype;
697         qi->tqi_qflags = qinfo->tqi_qflags;
698         qi->tqi_priority = qinfo->tqi_priority;
699         if (qinfo->tqi_aifs != ATH9K_TXQ_USEDEFAULT)
700                 qi->tqi_aifs = min(qinfo->tqi_aifs, 255U);
701         else
702                 qi->tqi_aifs = INIT_AIFS;
703         if (qinfo->tqi_cwmin != ATH9K_TXQ_USEDEFAULT) {
704                 cw = min(qinfo->tqi_cwmin, 1024U);
705                 qi->tqi_cwmin = 1;
706                 while (qi->tqi_cwmin < cw)
707                         qi->tqi_cwmin = (qi->tqi_cwmin << 1) | 1;
708         } else
709                 qi->tqi_cwmin = qinfo->tqi_cwmin;
710         if (qinfo->tqi_cwmax != ATH9K_TXQ_USEDEFAULT) {
711                 cw = min(qinfo->tqi_cwmax, 1024U);
712                 qi->tqi_cwmax = 1;
713                 while (qi->tqi_cwmax < cw)
714                         qi->tqi_cwmax = (qi->tqi_cwmax << 1) | 1;
715         } else
716                 qi->tqi_cwmax = INIT_CWMAX;
717
718         if (qinfo->tqi_shretry != 0)
719                 qi->tqi_shretry = min((u32) qinfo->tqi_shretry, 15U);
720         else
721                 qi->tqi_shretry = INIT_SH_RETRY;
722         if (qinfo->tqi_lgretry != 0)
723                 qi->tqi_lgretry = min((u32) qinfo->tqi_lgretry, 15U);
724         else
725                 qi->tqi_lgretry = INIT_LG_RETRY;
726         qi->tqi_cbrPeriod = qinfo->tqi_cbrPeriod;
727         qi->tqi_cbrOverflowLimit = qinfo->tqi_cbrOverflowLimit;
728         qi->tqi_burstTime = qinfo->tqi_burstTime;
729         qi->tqi_readyTime = qinfo->tqi_readyTime;
730
731         switch (qinfo->tqi_subtype) {
732         case ATH9K_WME_UPSD:
733                 if (qi->tqi_type == ATH9K_TX_QUEUE_DATA)
734                         qi->tqi_intFlags = ATH9K_TXQ_USE_LOCKOUT_BKOFF_DIS;
735                 break;
736         default:
737                 break;
738         }
739
740         return true;
741 }
742 EXPORT_SYMBOL(ath9k_hw_set_txq_props);
743
744 bool ath9k_hw_get_txq_props(struct ath_hw *ah, int q,
745                             struct ath9k_tx_queue_info *qinfo)
746 {
747         struct ath_common *common = ath9k_hw_common(ah);
748         struct ath9k_hw_capabilities *pCap = &ah->caps;
749         struct ath9k_tx_queue_info *qi;
750
751         if (q >= pCap->total_queues) {
752                 ath_print(common, ATH_DBG_QUEUE, "Get TXQ properties, "
753                           "invalid queue: %u\n", q);
754                 return false;
755         }
756
757         qi = &ah->txq[q];
758         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
759                 ath_print(common, ATH_DBG_QUEUE, "Get TXQ properties, "
760                           "inactive queue: %u\n", q);
761                 return false;
762         }
763
764         qinfo->tqi_qflags = qi->tqi_qflags;
765         qinfo->tqi_ver = qi->tqi_ver;
766         qinfo->tqi_subtype = qi->tqi_subtype;
767         qinfo->tqi_qflags = qi->tqi_qflags;
768         qinfo->tqi_priority = qi->tqi_priority;
769         qinfo->tqi_aifs = qi->tqi_aifs;
770         qinfo->tqi_cwmin = qi->tqi_cwmin;
771         qinfo->tqi_cwmax = qi->tqi_cwmax;
772         qinfo->tqi_shretry = qi->tqi_shretry;
773         qinfo->tqi_lgretry = qi->tqi_lgretry;
774         qinfo->tqi_cbrPeriod = qi->tqi_cbrPeriod;
775         qinfo->tqi_cbrOverflowLimit = qi->tqi_cbrOverflowLimit;
776         qinfo->tqi_burstTime = qi->tqi_burstTime;
777         qinfo->tqi_readyTime = qi->tqi_readyTime;
778
779         return true;
780 }
781 EXPORT_SYMBOL(ath9k_hw_get_txq_props);
782
783 int ath9k_hw_setuptxqueue(struct ath_hw *ah, enum ath9k_tx_queue type,
784                           const struct ath9k_tx_queue_info *qinfo)
785 {
786         struct ath_common *common = ath9k_hw_common(ah);
787         struct ath9k_tx_queue_info *qi;
788         struct ath9k_hw_capabilities *pCap = &ah->caps;
789         int q;
790
791         switch (type) {
792         case ATH9K_TX_QUEUE_BEACON:
793                 q = pCap->total_queues - 1;
794                 break;
795         case ATH9K_TX_QUEUE_CAB:
796                 q = pCap->total_queues - 2;
797                 break;
798         case ATH9K_TX_QUEUE_PSPOLL:
799                 q = 1;
800                 break;
801         case ATH9K_TX_QUEUE_UAPSD:
802                 q = pCap->total_queues - 3;
803                 break;
804         case ATH9K_TX_QUEUE_DATA:
805                 for (q = 0; q < pCap->total_queues; q++)
806                         if (ah->txq[q].tqi_type ==
807                             ATH9K_TX_QUEUE_INACTIVE)
808                                 break;
809                 if (q == pCap->total_queues) {
810                         ath_print(common, ATH_DBG_FATAL,
811                                   "No available TX queue\n");
812                         return -1;
813                 }
814                 break;
815         default:
816                 ath_print(common, ATH_DBG_FATAL,
817                           "Invalid TX queue type: %u\n", type);
818                 return -1;
819         }
820
821         ath_print(common, ATH_DBG_QUEUE, "Setup TX queue: %u\n", q);
822
823         qi = &ah->txq[q];
824         if (qi->tqi_type != ATH9K_TX_QUEUE_INACTIVE) {
825                 ath_print(common, ATH_DBG_FATAL,
826                           "TX queue: %u already active\n", q);
827                 return -1;
828         }
829         memset(qi, 0, sizeof(struct ath9k_tx_queue_info));
830         qi->tqi_type = type;
831         if (qinfo == NULL) {
832                 qi->tqi_qflags =
833                         TXQ_FLAG_TXOKINT_ENABLE
834                         | TXQ_FLAG_TXERRINT_ENABLE
835                         | TXQ_FLAG_TXDESCINT_ENABLE | TXQ_FLAG_TXURNINT_ENABLE;
836                 qi->tqi_aifs = INIT_AIFS;
837                 qi->tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
838                 qi->tqi_cwmax = INIT_CWMAX;
839                 qi->tqi_shretry = INIT_SH_RETRY;
840                 qi->tqi_lgretry = INIT_LG_RETRY;
841                 qi->tqi_physCompBuf = 0;
842         } else {
843                 qi->tqi_physCompBuf = qinfo->tqi_physCompBuf;
844                 (void) ath9k_hw_set_txq_props(ah, q, qinfo);
845         }
846
847         return q;
848 }
849 EXPORT_SYMBOL(ath9k_hw_setuptxqueue);
850
851 bool ath9k_hw_releasetxqueue(struct ath_hw *ah, u32 q)
852 {
853         struct ath9k_hw_capabilities *pCap = &ah->caps;
854         struct ath_common *common = ath9k_hw_common(ah);
855         struct ath9k_tx_queue_info *qi;
856
857         if (q >= pCap->total_queues) {
858                 ath_print(common, ATH_DBG_QUEUE, "Release TXQ, "
859                           "invalid queue: %u\n", q);
860                 return false;
861         }
862         qi = &ah->txq[q];
863         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
864                 ath_print(common, ATH_DBG_QUEUE, "Release TXQ, "
865                           "inactive queue: %u\n", q);
866                 return false;
867         }
868
869         ath_print(common, ATH_DBG_QUEUE, "Release TX queue: %u\n", q);
870
871         qi->tqi_type = ATH9K_TX_QUEUE_INACTIVE;
872         ah->txok_interrupt_mask &= ~(1 << q);
873         ah->txerr_interrupt_mask &= ~(1 << q);
874         ah->txdesc_interrupt_mask &= ~(1 << q);
875         ah->txeol_interrupt_mask &= ~(1 << q);
876         ah->txurn_interrupt_mask &= ~(1 << q);
877         ath9k_hw_set_txq_interrupts(ah, qi);
878
879         return true;
880 }
881 EXPORT_SYMBOL(ath9k_hw_releasetxqueue);
882
883 bool ath9k_hw_resettxqueue(struct ath_hw *ah, u32 q)
884 {
885         struct ath9k_hw_capabilities *pCap = &ah->caps;
886         struct ath_common *common = ath9k_hw_common(ah);
887         struct ath9k_channel *chan = ah->curchan;
888         struct ath9k_tx_queue_info *qi;
889         u32 cwMin, chanCwMin, value;
890
891         if (q >= pCap->total_queues) {
892                 ath_print(common, ATH_DBG_QUEUE, "Reset TXQ, "
893                           "invalid queue: %u\n", q);
894                 return false;
895         }
896
897         qi = &ah->txq[q];
898         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
899                 ath_print(common, ATH_DBG_QUEUE, "Reset TXQ, "
900                           "inactive queue: %u\n", q);
901                 return true;
902         }
903
904         ath_print(common, ATH_DBG_QUEUE, "Reset TX queue: %u\n", q);
905
906         if (qi->tqi_cwmin == ATH9K_TXQ_USEDEFAULT) {
907                 if (chan && IS_CHAN_B(chan))
908                         chanCwMin = INIT_CWMIN_11B;
909                 else
910                         chanCwMin = INIT_CWMIN;
911
912                 for (cwMin = 1; cwMin < chanCwMin; cwMin = (cwMin << 1) | 1);
913         } else
914                 cwMin = qi->tqi_cwmin;
915
916         REG_WRITE(ah, AR_DLCL_IFS(q),
917                   SM(cwMin, AR_D_LCL_IFS_CWMIN) |
918                   SM(qi->tqi_cwmax, AR_D_LCL_IFS_CWMAX) |
919                   SM(qi->tqi_aifs, AR_D_LCL_IFS_AIFS));
920
921         REG_WRITE(ah, AR_DRETRY_LIMIT(q),
922                   SM(INIT_SSH_RETRY, AR_D_RETRY_LIMIT_STA_SH) |
923                   SM(INIT_SLG_RETRY, AR_D_RETRY_LIMIT_STA_LG) |
924                   SM(qi->tqi_shretry, AR_D_RETRY_LIMIT_FR_SH));
925
926         REG_WRITE(ah, AR_QMISC(q), AR_Q_MISC_DCU_EARLY_TERM_REQ);
927         REG_WRITE(ah, AR_DMISC(q),
928                   AR_D_MISC_CW_BKOFF_EN | AR_D_MISC_FRAG_WAIT_EN | 0x2);
929
930         if (qi->tqi_cbrPeriod) {
931                 REG_WRITE(ah, AR_QCBRCFG(q),
932                           SM(qi->tqi_cbrPeriod, AR_Q_CBRCFG_INTERVAL) |
933                           SM(qi->tqi_cbrOverflowLimit, AR_Q_CBRCFG_OVF_THRESH));
934                 REG_WRITE(ah, AR_QMISC(q),
935                           REG_READ(ah, AR_QMISC(q)) | AR_Q_MISC_FSP_CBR |
936                           (qi->tqi_cbrOverflowLimit ?
937                            AR_Q_MISC_CBR_EXP_CNTR_LIMIT_EN : 0));
938         }
939         if (qi->tqi_readyTime && (qi->tqi_type != ATH9K_TX_QUEUE_CAB)) {
940                 REG_WRITE(ah, AR_QRDYTIMECFG(q),
941                           SM(qi->tqi_readyTime, AR_Q_RDYTIMECFG_DURATION) |
942                           AR_Q_RDYTIMECFG_EN);
943         }
944
945         REG_WRITE(ah, AR_DCHNTIME(q),
946                   SM(qi->tqi_burstTime, AR_D_CHNTIME_DUR) |
947                   (qi->tqi_burstTime ? AR_D_CHNTIME_EN : 0));
948
949         if (qi->tqi_burstTime
950             && (qi->tqi_qflags & TXQ_FLAG_RDYTIME_EXP_POLICY_ENABLE)) {
951                 REG_WRITE(ah, AR_QMISC(q),
952                           REG_READ(ah, AR_QMISC(q)) |
953                           AR_Q_MISC_RDYTIME_EXP_POLICY);
954
955         }
956
957         if (qi->tqi_qflags & TXQ_FLAG_BACKOFF_DISABLE) {
958                 REG_WRITE(ah, AR_DMISC(q),
959                           REG_READ(ah, AR_DMISC(q)) |
960                           AR_D_MISC_POST_FR_BKOFF_DIS);
961         }
962         if (qi->tqi_qflags & TXQ_FLAG_FRAG_BURST_BACKOFF_ENABLE) {
963                 REG_WRITE(ah, AR_DMISC(q),
964                           REG_READ(ah, AR_DMISC(q)) |
965                           AR_D_MISC_FRAG_BKOFF_EN);
966         }
967         switch (qi->tqi_type) {
968         case ATH9K_TX_QUEUE_BEACON:
969                 REG_WRITE(ah, AR_QMISC(q), REG_READ(ah, AR_QMISC(q))
970                           | AR_Q_MISC_FSP_DBA_GATED
971                           | AR_Q_MISC_BEACON_USE
972                           | AR_Q_MISC_CBR_INCR_DIS1);
973
974                 REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
975                           | (AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL <<
976                              AR_D_MISC_ARB_LOCKOUT_CNTRL_S)
977                           | AR_D_MISC_BEACON_USE
978                           | AR_D_MISC_POST_FR_BKOFF_DIS);
979                 break;
980         case ATH9K_TX_QUEUE_CAB:
981                 REG_WRITE(ah, AR_QMISC(q), REG_READ(ah, AR_QMISC(q))
982                           | AR_Q_MISC_FSP_DBA_GATED
983                           | AR_Q_MISC_CBR_INCR_DIS1
984                           | AR_Q_MISC_CBR_INCR_DIS0);
985                 value = (qi->tqi_readyTime -
986                          (ah->config.sw_beacon_response_time -
987                           ah->config.dma_beacon_response_time) -
988                          ah->config.additional_swba_backoff) * 1024;
989                 REG_WRITE(ah, AR_QRDYTIMECFG(q),
990                           value | AR_Q_RDYTIMECFG_EN);
991                 REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
992                           | (AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL <<
993                              AR_D_MISC_ARB_LOCKOUT_CNTRL_S));
994                 break;
995         case ATH9K_TX_QUEUE_PSPOLL:
996                 REG_WRITE(ah, AR_QMISC(q),
997                           REG_READ(ah, AR_QMISC(q)) | AR_Q_MISC_CBR_INCR_DIS1);
998                 break;
999         case ATH9K_TX_QUEUE_UAPSD:
1000                 REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q)) |
1001                           AR_D_MISC_POST_FR_BKOFF_DIS);
1002                 break;
1003         default:
1004                 break;
1005         }
1006
1007         if (qi->tqi_intFlags & ATH9K_TXQ_USE_LOCKOUT_BKOFF_DIS) {
1008                 REG_WRITE(ah, AR_DMISC(q),
1009                           REG_READ(ah, AR_DMISC(q)) |
1010                           SM(AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL,
1011                              AR_D_MISC_ARB_LOCKOUT_CNTRL) |
1012                           AR_D_MISC_POST_FR_BKOFF_DIS);
1013         }
1014
1015         if (qi->tqi_qflags & TXQ_FLAG_TXOKINT_ENABLE)
1016                 ah->txok_interrupt_mask |= 1 << q;
1017         else
1018                 ah->txok_interrupt_mask &= ~(1 << q);
1019         if (qi->tqi_qflags & TXQ_FLAG_TXERRINT_ENABLE)
1020                 ah->txerr_interrupt_mask |= 1 << q;
1021         else
1022                 ah->txerr_interrupt_mask &= ~(1 << q);
1023         if (qi->tqi_qflags & TXQ_FLAG_TXDESCINT_ENABLE)
1024                 ah->txdesc_interrupt_mask |= 1 << q;
1025         else
1026                 ah->txdesc_interrupt_mask &= ~(1 << q);
1027         if (qi->tqi_qflags & TXQ_FLAG_TXEOLINT_ENABLE)
1028                 ah->txeol_interrupt_mask |= 1 << q;
1029         else
1030                 ah->txeol_interrupt_mask &= ~(1 << q);
1031         if (qi->tqi_qflags & TXQ_FLAG_TXURNINT_ENABLE)
1032                 ah->txurn_interrupt_mask |= 1 << q;
1033         else
1034                 ah->txurn_interrupt_mask &= ~(1 << q);
1035         ath9k_hw_set_txq_interrupts(ah, qi);
1036
1037         return true;
1038 }
1039 EXPORT_SYMBOL(ath9k_hw_resettxqueue);
1040
1041 int ath9k_hw_rxprocdesc(struct ath_hw *ah, struct ath_desc *ds,
1042                         struct ath_rx_status *rs, u64 tsf)
1043 {
1044         struct ar5416_desc ads;
1045         struct ar5416_desc *adsp = AR5416DESC(ds);
1046         u32 phyerr;
1047
1048         if ((adsp->ds_rxstatus8 & AR_RxDone) == 0)
1049                 return -EINPROGRESS;
1050
1051         ads.u.rx = adsp->u.rx;
1052
1053         rs->rs_status = 0;
1054         rs->rs_flags = 0;
1055
1056         rs->rs_datalen = ads.ds_rxstatus1 & AR_DataLen;
1057         rs->rs_tstamp = ads.AR_RcvTimestamp;
1058
1059         if (ads.ds_rxstatus8 & AR_PostDelimCRCErr) {
1060                 rs->rs_rssi = ATH9K_RSSI_BAD;
1061                 rs->rs_rssi_ctl0 = ATH9K_RSSI_BAD;
1062                 rs->rs_rssi_ctl1 = ATH9K_RSSI_BAD;
1063                 rs->rs_rssi_ctl2 = ATH9K_RSSI_BAD;
1064                 rs->rs_rssi_ext0 = ATH9K_RSSI_BAD;
1065                 rs->rs_rssi_ext1 = ATH9K_RSSI_BAD;
1066                 rs->rs_rssi_ext2 = ATH9K_RSSI_BAD;
1067         } else {
1068                 rs->rs_rssi = MS(ads.ds_rxstatus4, AR_RxRSSICombined);
1069                 rs->rs_rssi_ctl0 = MS(ads.ds_rxstatus0,
1070                                                 AR_RxRSSIAnt00);
1071                 rs->rs_rssi_ctl1 = MS(ads.ds_rxstatus0,
1072                                                 AR_RxRSSIAnt01);
1073                 rs->rs_rssi_ctl2 = MS(ads.ds_rxstatus0,
1074                                                 AR_RxRSSIAnt02);
1075                 rs->rs_rssi_ext0 = MS(ads.ds_rxstatus4,
1076                                                 AR_RxRSSIAnt10);
1077                 rs->rs_rssi_ext1 = MS(ads.ds_rxstatus4,
1078                                                 AR_RxRSSIAnt11);
1079                 rs->rs_rssi_ext2 = MS(ads.ds_rxstatus4,
1080                                                 AR_RxRSSIAnt12);
1081         }
1082         if (ads.ds_rxstatus8 & AR_RxKeyIdxValid)
1083                 rs->rs_keyix = MS(ads.ds_rxstatus8, AR_KeyIdx);
1084         else
1085                 rs->rs_keyix = ATH9K_RXKEYIX_INVALID;
1086
1087         rs->rs_rate = RXSTATUS_RATE(ah, (&ads));
1088         rs->rs_more = (ads.ds_rxstatus1 & AR_RxMore) ? 1 : 0;
1089
1090         rs->rs_isaggr = (ads.ds_rxstatus8 & AR_RxAggr) ? 1 : 0;
1091         rs->rs_moreaggr =
1092                 (ads.ds_rxstatus8 & AR_RxMoreAggr) ? 1 : 0;
1093         rs->rs_antenna = MS(ads.ds_rxstatus3, AR_RxAntenna);
1094         rs->rs_flags =
1095                 (ads.ds_rxstatus3 & AR_GI) ? ATH9K_RX_GI : 0;
1096         rs->rs_flags |=
1097                 (ads.ds_rxstatus3 & AR_2040) ? ATH9K_RX_2040 : 0;
1098
1099         if (ads.ds_rxstatus8 & AR_PreDelimCRCErr)
1100                 rs->rs_flags |= ATH9K_RX_DELIM_CRC_PRE;
1101         if (ads.ds_rxstatus8 & AR_PostDelimCRCErr)
1102                 rs->rs_flags |= ATH9K_RX_DELIM_CRC_POST;
1103         if (ads.ds_rxstatus8 & AR_DecryptBusyErr)
1104                 rs->rs_flags |= ATH9K_RX_DECRYPT_BUSY;
1105
1106         if ((ads.ds_rxstatus8 & AR_RxFrameOK) == 0) {
1107                 if (ads.ds_rxstatus8 & AR_CRCErr)
1108                         rs->rs_status |= ATH9K_RXERR_CRC;
1109                 else if (ads.ds_rxstatus8 & AR_PHYErr) {
1110                         rs->rs_status |= ATH9K_RXERR_PHY;
1111                         phyerr = MS(ads.ds_rxstatus8, AR_PHYErrCode);
1112                         rs->rs_phyerr = phyerr;
1113                 } else if (ads.ds_rxstatus8 & AR_DecryptCRCErr)
1114                         rs->rs_status |= ATH9K_RXERR_DECRYPT;
1115                 else if (ads.ds_rxstatus8 & AR_MichaelErr)
1116                         rs->rs_status |= ATH9K_RXERR_MIC;
1117         }
1118
1119         return 0;
1120 }
1121 EXPORT_SYMBOL(ath9k_hw_rxprocdesc);
1122
1123 void ath9k_hw_setuprxdesc(struct ath_hw *ah, struct ath_desc *ds,
1124                           u32 size, u32 flags)
1125 {
1126         struct ar5416_desc *ads = AR5416DESC(ds);
1127         struct ath9k_hw_capabilities *pCap = &ah->caps;
1128
1129         ads->ds_ctl1 = size & AR_BufLen;
1130         if (flags & ATH9K_RXDESC_INTREQ)
1131                 ads->ds_ctl1 |= AR_RxIntrReq;
1132
1133         ads->ds_rxstatus8 &= ~AR_RxDone;
1134         if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
1135                 memset(&(ads->u), 0, sizeof(ads->u));
1136 }
1137 EXPORT_SYMBOL(ath9k_hw_setuprxdesc);
1138
1139 /*
1140  * This can stop or re-enables RX.
1141  *
1142  * If bool is set this will kill any frame which is currently being
1143  * transferred between the MAC and baseband and also prevent any new
1144  * frames from getting started.
1145  */
1146 bool ath9k_hw_setrxabort(struct ath_hw *ah, bool set)
1147 {
1148         u32 reg;
1149
1150         if (set) {
1151                 REG_SET_BIT(ah, AR_DIAG_SW,
1152                             (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
1153
1154                 if (!ath9k_hw_wait(ah, AR_OBS_BUS_1, AR_OBS_BUS_1_RX_STATE,
1155                                    0, AH_WAIT_TIMEOUT)) {
1156                         REG_CLR_BIT(ah, AR_DIAG_SW,
1157                                     (AR_DIAG_RX_DIS |
1158                                      AR_DIAG_RX_ABORT));
1159
1160                         reg = REG_READ(ah, AR_OBS_BUS_1);
1161                         ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
1162                                   "RX failed to go idle in 10 ms RXSM=0x%x\n",
1163                                   reg);
1164
1165                         return false;
1166                 }
1167         } else {
1168                 REG_CLR_BIT(ah, AR_DIAG_SW,
1169                             (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
1170         }
1171
1172         return true;
1173 }
1174 EXPORT_SYMBOL(ath9k_hw_setrxabort);
1175
1176 void ath9k_hw_putrxbuf(struct ath_hw *ah, u32 rxdp)
1177 {
1178         REG_WRITE(ah, AR_RXDP, rxdp);
1179 }
1180 EXPORT_SYMBOL(ath9k_hw_putrxbuf);
1181
1182 void ath9k_hw_startpcureceive(struct ath_hw *ah)
1183 {
1184         ath9k_enable_mib_counters(ah);
1185
1186         ath9k_ani_reset(ah);
1187
1188         REG_CLR_BIT(ah, AR_DIAG_SW, (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
1189 }
1190 EXPORT_SYMBOL(ath9k_hw_startpcureceive);
1191
1192 void ath9k_hw_stoppcurecv(struct ath_hw *ah)
1193 {
1194         REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_RX_DIS);
1195
1196         ath9k_hw_disable_mib_counters(ah);
1197 }
1198 EXPORT_SYMBOL(ath9k_hw_stoppcurecv);
1199
1200 bool ath9k_hw_stopdmarecv(struct ath_hw *ah)
1201 {
1202 #define AH_RX_STOP_DMA_TIMEOUT 10000   /* usec */
1203 #define AH_RX_TIME_QUANTUM     100     /* usec */
1204         struct ath_common *common = ath9k_hw_common(ah);
1205         int i;
1206
1207         REG_WRITE(ah, AR_CR, AR_CR_RXD);
1208
1209         /* Wait for rx enable bit to go low */
1210         for (i = AH_RX_STOP_DMA_TIMEOUT / AH_TIME_QUANTUM; i != 0; i--) {
1211                 if ((REG_READ(ah, AR_CR) & AR_CR_RXE) == 0)
1212                         break;
1213                 udelay(AH_TIME_QUANTUM);
1214         }
1215
1216         if (i == 0) {
1217                 ath_print(common, ATH_DBG_FATAL,
1218                           "DMA failed to stop in %d ms "
1219                           "AR_CR=0x%08x AR_DIAG_SW=0x%08x\n",
1220                           AH_RX_STOP_DMA_TIMEOUT / 1000,
1221                           REG_READ(ah, AR_CR),
1222                           REG_READ(ah, AR_DIAG_SW));
1223                 return false;
1224         } else {
1225                 return true;
1226         }
1227
1228 #undef AH_RX_TIME_QUANTUM
1229 #undef AH_RX_STOP_DMA_TIMEOUT
1230 }
1231 EXPORT_SYMBOL(ath9k_hw_stopdmarecv);
1232
1233 int ath9k_hw_beaconq_setup(struct ath_hw *ah)
1234 {
1235         struct ath9k_tx_queue_info qi;
1236
1237         memset(&qi, 0, sizeof(qi));
1238         qi.tqi_aifs = 1;
1239         qi.tqi_cwmin = 0;
1240         qi.tqi_cwmax = 0;
1241         /* NB: don't enable any interrupts */
1242         return ath9k_hw_setuptxqueue(ah, ATH9K_TX_QUEUE_BEACON, &qi);
1243 }
1244 EXPORT_SYMBOL(ath9k_hw_beaconq_setup);
1245
1246 bool ath9k_hw_intrpend(struct ath_hw *ah)
1247 {
1248         u32 host_isr;
1249
1250         if (AR_SREV_9100(ah))
1251                 return true;
1252
1253         host_isr = REG_READ(ah, AR_INTR_ASYNC_CAUSE);
1254         if ((host_isr & AR_INTR_MAC_IRQ) && (host_isr != AR_INTR_SPURIOUS))
1255                 return true;
1256
1257         host_isr = REG_READ(ah, AR_INTR_SYNC_CAUSE);
1258         if ((host_isr & AR_INTR_SYNC_DEFAULT)
1259             && (host_isr != AR_INTR_SPURIOUS))
1260                 return true;
1261
1262         return false;
1263 }
1264 EXPORT_SYMBOL(ath9k_hw_intrpend);
1265
1266 enum ath9k_int ath9k_hw_set_interrupts(struct ath_hw *ah,
1267                                               enum ath9k_int ints)
1268 {
1269         enum ath9k_int omask = ah->imask;
1270         u32 mask, mask2;
1271         struct ath9k_hw_capabilities *pCap = &ah->caps;
1272         struct ath_common *common = ath9k_hw_common(ah);
1273
1274         ath_print(common, ATH_DBG_INTERRUPT, "0x%x => 0x%x\n", omask, ints);
1275
1276         if (omask & ATH9K_INT_GLOBAL) {
1277                 ath_print(common, ATH_DBG_INTERRUPT, "disable IER\n");
1278                 REG_WRITE(ah, AR_IER, AR_IER_DISABLE);
1279                 (void) REG_READ(ah, AR_IER);
1280                 if (!AR_SREV_9100(ah)) {
1281                         REG_WRITE(ah, AR_INTR_ASYNC_ENABLE, 0);
1282                         (void) REG_READ(ah, AR_INTR_ASYNC_ENABLE);
1283
1284                         REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
1285                         (void) REG_READ(ah, AR_INTR_SYNC_ENABLE);
1286                 }
1287         }
1288
1289         /* TODO: global int Ref count */
1290         mask = ints & ATH9K_INT_COMMON;
1291         mask2 = 0;
1292
1293         if (ints & ATH9K_INT_TX) {
1294                 if (ah->config.tx_intr_mitigation)
1295                         mask |= AR_IMR_TXMINTR | AR_IMR_TXINTM;
1296                 if (ah->txok_interrupt_mask)
1297                         mask |= AR_IMR_TXOK;
1298                 if (ah->txdesc_interrupt_mask)
1299                         mask |= AR_IMR_TXDESC;
1300                 if (ah->txerr_interrupt_mask)
1301                         mask |= AR_IMR_TXERR;
1302                 if (ah->txeol_interrupt_mask)
1303                         mask |= AR_IMR_TXEOL;
1304         }
1305         if (ints & ATH9K_INT_RX) {
1306                 if (AR_SREV_9300_20_OR_LATER(ah)) {
1307                         mask |= AR_IMR_RXERR | AR_IMR_RXOK_HP;
1308                         if (ah->config.rx_intr_mitigation) {
1309                                 mask &= ~AR_IMR_RXOK_LP;
1310                                 mask |=  AR_IMR_RXMINTR | AR_IMR_RXINTM;
1311                         } else {
1312                                 mask |= AR_IMR_RXOK_LP;
1313                         }
1314                 } else {
1315                         if (ah->config.rx_intr_mitigation)
1316                                 mask |= AR_IMR_RXMINTR | AR_IMR_RXINTM;
1317                         else
1318                                 mask |= AR_IMR_RXOK | AR_IMR_RXDESC;
1319                 }
1320                 if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
1321                         mask |= AR_IMR_GENTMR;
1322         }
1323
1324         if (ints & (ATH9K_INT_BMISC)) {
1325                 mask |= AR_IMR_BCNMISC;
1326                 if (ints & ATH9K_INT_TIM)
1327                         mask2 |= AR_IMR_S2_TIM;
1328                 if (ints & ATH9K_INT_DTIM)
1329                         mask2 |= AR_IMR_S2_DTIM;
1330                 if (ints & ATH9K_INT_DTIMSYNC)
1331                         mask2 |= AR_IMR_S2_DTIMSYNC;
1332                 if (ints & ATH9K_INT_CABEND)
1333                         mask2 |= AR_IMR_S2_CABEND;
1334                 if (ints & ATH9K_INT_TSFOOR)
1335                         mask2 |= AR_IMR_S2_TSFOOR;
1336         }
1337
1338         if (ints & (ATH9K_INT_GTT | ATH9K_INT_CST)) {
1339                 mask |= AR_IMR_BCNMISC;
1340                 if (ints & ATH9K_INT_GTT)
1341                         mask2 |= AR_IMR_S2_GTT;
1342                 if (ints & ATH9K_INT_CST)
1343                         mask2 |= AR_IMR_S2_CST;
1344         }
1345
1346         ath_print(common, ATH_DBG_INTERRUPT, "new IMR 0x%x\n", mask);
1347         REG_WRITE(ah, AR_IMR, mask);
1348         ah->imrs2_reg &= ~(AR_IMR_S2_TIM | AR_IMR_S2_DTIM | AR_IMR_S2_DTIMSYNC |
1349                            AR_IMR_S2_CABEND | AR_IMR_S2_CABTO |
1350                            AR_IMR_S2_TSFOOR | AR_IMR_S2_GTT | AR_IMR_S2_CST);
1351         ah->imrs2_reg |= mask2;
1352         REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
1353
1354         if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1355                 if (ints & ATH9K_INT_TIM_TIMER)
1356                         REG_SET_BIT(ah, AR_IMR_S5, AR_IMR_S5_TIM_TIMER);
1357                 else
1358                         REG_CLR_BIT(ah, AR_IMR_S5, AR_IMR_S5_TIM_TIMER);
1359         }
1360
1361         if (ints & ATH9K_INT_GLOBAL) {
1362                 ath_print(common, ATH_DBG_INTERRUPT, "enable IER\n");
1363                 REG_WRITE(ah, AR_IER, AR_IER_ENABLE);
1364                 if (!AR_SREV_9100(ah)) {
1365                         REG_WRITE(ah, AR_INTR_ASYNC_ENABLE,
1366                                   AR_INTR_MAC_IRQ);
1367                         REG_WRITE(ah, AR_INTR_ASYNC_MASK, AR_INTR_MAC_IRQ);
1368
1369
1370                         REG_WRITE(ah, AR_INTR_SYNC_ENABLE,
1371                                   AR_INTR_SYNC_DEFAULT);
1372                         REG_WRITE(ah, AR_INTR_SYNC_MASK,
1373                                   AR_INTR_SYNC_DEFAULT);
1374                 }
1375                 ath_print(common, ATH_DBG_INTERRUPT, "AR_IMR 0x%x IER 0x%x\n",
1376                           REG_READ(ah, AR_IMR), REG_READ(ah, AR_IER));
1377         }
1378
1379         return omask;
1380 }
1381 EXPORT_SYMBOL(ath9k_hw_set_interrupts);