ath9k: Lock mac80211 callbacks with a mutex
[linux-2.6] / drivers / net / wireless / ath9k / mac.c
1 /*
2  * Copyright (c) 2008 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 "core.h"
18 #include "hw.h"
19 #include "reg.h"
20 #include "phy.h"
21
22 static void ath9k_hw_set_txq_interrupts(struct ath_hal *ah,
23                                         struct ath9k_tx_queue_info *qi)
24 {
25         struct ath_hal_5416 *ahp = AH5416(ah);
26
27         DPRINTF(ah->ah_sc, ATH_DBG_INTERRUPT,
28                 "tx ok 0x%x err 0x%x desc 0x%x eol 0x%x urn 0x%x\n",
29                 ahp->ah_txOkInterruptMask, ahp->ah_txErrInterruptMask,
30                 ahp->ah_txDescInterruptMask, ahp->ah_txEolInterruptMask,
31                 ahp->ah_txUrnInterruptMask);
32
33         REG_WRITE(ah, AR_IMR_S0,
34                   SM(ahp->ah_txOkInterruptMask, AR_IMR_S0_QCU_TXOK)
35                   | SM(ahp->ah_txDescInterruptMask, AR_IMR_S0_QCU_TXDESC));
36         REG_WRITE(ah, AR_IMR_S1,
37                   SM(ahp->ah_txErrInterruptMask, AR_IMR_S1_QCU_TXERR)
38                   | SM(ahp->ah_txEolInterruptMask, AR_IMR_S1_QCU_TXEOL));
39         REG_RMW_FIELD(ah, AR_IMR_S2,
40                       AR_IMR_S2_QCU_TXURN, ahp->ah_txUrnInterruptMask);
41 }
42
43 u32 ath9k_hw_gettxbuf(struct ath_hal *ah, u32 q)
44 {
45         return REG_READ(ah, AR_QTXDP(q));
46 }
47
48 bool ath9k_hw_puttxbuf(struct ath_hal *ah, u32 q, u32 txdp)
49 {
50         REG_WRITE(ah, AR_QTXDP(q), txdp);
51
52         return true;
53 }
54
55 bool ath9k_hw_txstart(struct ath_hal *ah, u32 q)
56 {
57         DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "queue %u\n", q);
58
59         REG_WRITE(ah, AR_Q_TXE, 1 << q);
60
61         return true;
62 }
63
64 u32 ath9k_hw_numtxpending(struct ath_hal *ah, u32 q)
65 {
66         u32 npend;
67
68         npend = REG_READ(ah, AR_QSTS(q)) & AR_Q_STS_PEND_FR_CNT;
69         if (npend == 0) {
70
71                 if (REG_READ(ah, AR_Q_TXE) & (1 << q))
72                         npend = 1;
73         }
74
75         return npend;
76 }
77
78 bool ath9k_hw_updatetxtriglevel(struct ath_hal *ah, bool bIncTrigLevel)
79 {
80         struct ath_hal_5416 *ahp = AH5416(ah);
81         u32 txcfg, curLevel, newLevel;
82         enum ath9k_int omask;
83
84         if (ah->ah_txTrigLevel >= MAX_TX_FIFO_THRESHOLD)
85                 return false;
86
87         omask = ath9k_hw_set_interrupts(ah, ahp->ah_maskReg & ~ATH9K_INT_GLOBAL);
88
89         txcfg = REG_READ(ah, AR_TXCFG);
90         curLevel = MS(txcfg, AR_FTRIG);
91         newLevel = curLevel;
92         if (bIncTrigLevel) {
93                 if (curLevel < MAX_TX_FIFO_THRESHOLD)
94                         newLevel++;
95         } else if (curLevel > MIN_TX_FIFO_THRESHOLD)
96                 newLevel--;
97         if (newLevel != curLevel)
98                 REG_WRITE(ah, AR_TXCFG,
99                           (txcfg & ~AR_FTRIG) | SM(newLevel, AR_FTRIG));
100
101         ath9k_hw_set_interrupts(ah, omask);
102
103         ah->ah_txTrigLevel = newLevel;
104
105         return newLevel != curLevel;
106 }
107
108 bool ath9k_hw_stoptxdma(struct ath_hal *ah, u32 q)
109 {
110 #define ATH9K_TX_STOP_DMA_TIMEOUT       4000    /* usec */
111 #define ATH9K_TIME_QUANTUM              100     /* usec */
112
113         struct ath_hal_5416 *ahp = AH5416(ah);
114         struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
115         struct ath9k_tx_queue_info *qi;
116         u32 tsfLow, j, wait;
117         u32 wait_time = ATH9K_TX_STOP_DMA_TIMEOUT / ATH9K_TIME_QUANTUM;
118
119         if (q >= pCap->total_queues) {
120                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "invalid queue num %u\n", q);
121                 return false;
122         }
123
124         qi = &ahp->ah_txq[q];
125         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
126                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "inactive queue\n");
127                 return false;
128         }
129
130         REG_WRITE(ah, AR_Q_TXD, 1 << q);
131
132         for (wait = wait_time; wait != 0; wait--) {
133                 if (ath9k_hw_numtxpending(ah, q) == 0)
134                         break;
135                 udelay(ATH9K_TIME_QUANTUM);
136         }
137
138         if (ath9k_hw_numtxpending(ah, q)) {
139                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
140                         "%s: Num of pending TX Frames %d on Q %d\n",
141                         __func__, ath9k_hw_numtxpending(ah, q), q);
142
143                 for (j = 0; j < 2; j++) {
144                         tsfLow = REG_READ(ah, AR_TSF_L32);
145                         REG_WRITE(ah, AR_QUIET2,
146                                   SM(10, AR_QUIET2_QUIET_DUR));
147                         REG_WRITE(ah, AR_QUIET_PERIOD, 100);
148                         REG_WRITE(ah, AR_NEXT_QUIET_TIMER, tsfLow >> 10);
149                         REG_SET_BIT(ah, AR_TIMER_MODE,
150                                        AR_QUIET_TIMER_EN);
151
152                         if ((REG_READ(ah, AR_TSF_L32) >> 10) == (tsfLow >> 10))
153                                 break;
154
155                         DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
156                                 "TSF have moved while trying to set "
157                                 "quiet time TSF: 0x%08x\n", tsfLow);
158                 }
159
160                 REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
161
162                 udelay(200);
163                 REG_CLR_BIT(ah, AR_TIMER_MODE, AR_QUIET_TIMER_EN);
164
165                 wait = wait_time;
166                 while (ath9k_hw_numtxpending(ah, q)) {
167                         if ((--wait) == 0) {
168                                 DPRINTF(ah->ah_sc, ATH_DBG_XMIT,
169                                         "Failed to stop Tx DMA in 100 "
170                                         "msec after killing last frame\n");
171                                 break;
172                         }
173                         udelay(ATH9K_TIME_QUANTUM);
174                 }
175
176                 REG_CLR_BIT(ah, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
177         }
178
179         REG_WRITE(ah, AR_Q_TXD, 0);
180         return wait != 0;
181
182 #undef ATH9K_TX_STOP_DMA_TIMEOUT
183 #undef ATH9K_TIME_QUANTUM
184 }
185
186 bool ath9k_hw_filltxdesc(struct ath_hal *ah, struct ath_desc *ds,
187                          u32 segLen, bool firstSeg,
188                          bool lastSeg, const struct ath_desc *ds0)
189 {
190         struct ar5416_desc *ads = AR5416DESC(ds);
191
192         if (firstSeg) {
193                 ads->ds_ctl1 |= segLen | (lastSeg ? 0 : AR_TxMore);
194         } else if (lastSeg) {
195                 ads->ds_ctl0 = 0;
196                 ads->ds_ctl1 = segLen;
197                 ads->ds_ctl2 = AR5416DESC_CONST(ds0)->ds_ctl2;
198                 ads->ds_ctl3 = AR5416DESC_CONST(ds0)->ds_ctl3;
199         } else {
200                 ads->ds_ctl0 = 0;
201                 ads->ds_ctl1 = segLen | AR_TxMore;
202                 ads->ds_ctl2 = 0;
203                 ads->ds_ctl3 = 0;
204         }
205         ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
206         ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
207         ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
208         ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
209         ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
210
211         return true;
212 }
213
214 void ath9k_hw_cleartxdesc(struct ath_hal *ah, struct ath_desc *ds)
215 {
216         struct ar5416_desc *ads = AR5416DESC(ds);
217
218         ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
219         ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
220         ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
221         ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
222         ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
223 }
224
225 int ath9k_hw_txprocdesc(struct ath_hal *ah, struct ath_desc *ds)
226 {
227         struct ar5416_desc *ads = AR5416DESC(ds);
228
229         if ((ads->ds_txstatus9 & AR_TxDone) == 0)
230                 return -EINPROGRESS;
231
232         ds->ds_txstat.ts_seqnum = MS(ads->ds_txstatus9, AR_SeqNum);
233         ds->ds_txstat.ts_tstamp = ads->AR_SendTimestamp;
234         ds->ds_txstat.ts_status = 0;
235         ds->ds_txstat.ts_flags = 0;
236
237         if (ads->ds_txstatus1 & AR_ExcessiveRetries)
238                 ds->ds_txstat.ts_status |= ATH9K_TXERR_XRETRY;
239         if (ads->ds_txstatus1 & AR_Filtered)
240                 ds->ds_txstat.ts_status |= ATH9K_TXERR_FILT;
241         if (ads->ds_txstatus1 & AR_FIFOUnderrun) {
242                 ds->ds_txstat.ts_status |= ATH9K_TXERR_FIFO;
243                 ath9k_hw_updatetxtriglevel(ah, true);
244         }
245         if (ads->ds_txstatus9 & AR_TxOpExceeded)
246                 ds->ds_txstat.ts_status |= ATH9K_TXERR_XTXOP;
247         if (ads->ds_txstatus1 & AR_TxTimerExpired)
248                 ds->ds_txstat.ts_status |= ATH9K_TXERR_TIMER_EXPIRED;
249
250         if (ads->ds_txstatus1 & AR_DescCfgErr)
251                 ds->ds_txstat.ts_flags |= ATH9K_TX_DESC_CFG_ERR;
252         if (ads->ds_txstatus1 & AR_TxDataUnderrun) {
253                 ds->ds_txstat.ts_flags |= ATH9K_TX_DATA_UNDERRUN;
254                 ath9k_hw_updatetxtriglevel(ah, true);
255         }
256         if (ads->ds_txstatus1 & AR_TxDelimUnderrun) {
257                 ds->ds_txstat.ts_flags |= ATH9K_TX_DELIM_UNDERRUN;
258                 ath9k_hw_updatetxtriglevel(ah, true);
259         }
260         if (ads->ds_txstatus0 & AR_TxBaStatus) {
261                 ds->ds_txstat.ts_flags |= ATH9K_TX_BA;
262                 ds->ds_txstat.ba_low = ads->AR_BaBitmapLow;
263                 ds->ds_txstat.ba_high = ads->AR_BaBitmapHigh;
264         }
265
266         ds->ds_txstat.ts_rateindex = MS(ads->ds_txstatus9, AR_FinalTxIdx);
267         switch (ds->ds_txstat.ts_rateindex) {
268         case 0:
269                 ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate0);
270                 break;
271         case 1:
272                 ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate1);
273                 break;
274         case 2:
275                 ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate2);
276                 break;
277         case 3:
278                 ds->ds_txstat.ts_ratecode = MS(ads->ds_ctl3, AR_XmitRate3);
279                 break;
280         }
281
282         ds->ds_txstat.ts_rssi = MS(ads->ds_txstatus5, AR_TxRSSICombined);
283         ds->ds_txstat.ts_rssi_ctl0 = MS(ads->ds_txstatus0, AR_TxRSSIAnt00);
284         ds->ds_txstat.ts_rssi_ctl1 = MS(ads->ds_txstatus0, AR_TxRSSIAnt01);
285         ds->ds_txstat.ts_rssi_ctl2 = MS(ads->ds_txstatus0, AR_TxRSSIAnt02);
286         ds->ds_txstat.ts_rssi_ext0 = MS(ads->ds_txstatus5, AR_TxRSSIAnt10);
287         ds->ds_txstat.ts_rssi_ext1 = MS(ads->ds_txstatus5, AR_TxRSSIAnt11);
288         ds->ds_txstat.ts_rssi_ext2 = MS(ads->ds_txstatus5, AR_TxRSSIAnt12);
289         ds->ds_txstat.evm0 = ads->AR_TxEVM0;
290         ds->ds_txstat.evm1 = ads->AR_TxEVM1;
291         ds->ds_txstat.evm2 = ads->AR_TxEVM2;
292         ds->ds_txstat.ts_shortretry = MS(ads->ds_txstatus1, AR_RTSFailCnt);
293         ds->ds_txstat.ts_longretry = MS(ads->ds_txstatus1, AR_DataFailCnt);
294         ds->ds_txstat.ts_virtcol = MS(ads->ds_txstatus1, AR_VirtRetryCnt);
295         ds->ds_txstat.ts_antenna = 1;
296
297         return 0;
298 }
299
300 void ath9k_hw_set11n_txdesc(struct ath_hal *ah, struct ath_desc *ds,
301                             u32 pktLen, enum ath9k_pkt_type type, u32 txPower,
302                             u32 keyIx, enum ath9k_key_type keyType, u32 flags)
303 {
304         struct ar5416_desc *ads = AR5416DESC(ds);
305         struct ath_hal_5416 *ahp = AH5416(ah);
306
307         txPower += ahp->ah_txPowerIndexOffset;
308         if (txPower > 63)
309                 txPower = 63;
310
311         ads->ds_ctl0 = (pktLen & AR_FrameLen)
312                 | (flags & ATH9K_TXDESC_VMF ? AR_VirtMoreFrag : 0)
313                 | SM(txPower, AR_XmitPower)
314                 | (flags & ATH9K_TXDESC_VEOL ? AR_VEOL : 0)
315                 | (flags & ATH9K_TXDESC_CLRDMASK ? AR_ClrDestMask : 0)
316                 | (flags & ATH9K_TXDESC_INTREQ ? AR_TxIntrReq : 0)
317                 | (keyIx != ATH9K_TXKEYIX_INVALID ? AR_DestIdxValid : 0);
318
319         ads->ds_ctl1 =
320                 (keyIx != ATH9K_TXKEYIX_INVALID ? SM(keyIx, AR_DestIdx) : 0)
321                 | SM(type, AR_FrameType)
322                 | (flags & ATH9K_TXDESC_NOACK ? AR_NoAck : 0)
323                 | (flags & ATH9K_TXDESC_EXT_ONLY ? AR_ExtOnly : 0)
324                 | (flags & ATH9K_TXDESC_EXT_AND_CTL ? AR_ExtAndCtl : 0);
325
326         ads->ds_ctl6 = SM(keyType, AR_EncrType);
327
328         if (AR_SREV_9285(ah)) {
329                 ads->ds_ctl8 = 0;
330                 ads->ds_ctl9 = 0;
331                 ads->ds_ctl10 = 0;
332                 ads->ds_ctl11 = 0;
333         }
334 }
335
336 void ath9k_hw_set11n_ratescenario(struct ath_hal *ah, struct ath_desc *ds,
337                                   struct ath_desc *lastds,
338                                   u32 durUpdateEn, u32 rtsctsRate,
339                                   u32 rtsctsDuration,
340                                   struct ath9k_11n_rate_series series[],
341                                   u32 nseries, u32 flags)
342 {
343         struct ar5416_desc *ads = AR5416DESC(ds);
344         struct ar5416_desc *last_ads = AR5416DESC(lastds);
345         u32 ds_ctl0;
346
347         if (flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA)) {
348                 ds_ctl0 = ads->ds_ctl0;
349
350                 if (flags & ATH9K_TXDESC_RTSENA) {
351                         ds_ctl0 &= ~AR_CTSEnable;
352                         ds_ctl0 |= AR_RTSEnable;
353                 } else {
354                         ds_ctl0 &= ~AR_RTSEnable;
355                         ds_ctl0 |= AR_CTSEnable;
356                 }
357
358                 ads->ds_ctl0 = ds_ctl0;
359         } else {
360                 ads->ds_ctl0 =
361                         (ads->ds_ctl0 & ~(AR_RTSEnable | AR_CTSEnable));
362         }
363
364         ads->ds_ctl2 = set11nTries(series, 0)
365                 | set11nTries(series, 1)
366                 | set11nTries(series, 2)
367                 | set11nTries(series, 3)
368                 | (durUpdateEn ? AR_DurUpdateEna : 0)
369                 | SM(0, AR_BurstDur);
370
371         ads->ds_ctl3 = set11nRate(series, 0)
372                 | set11nRate(series, 1)
373                 | set11nRate(series, 2)
374                 | set11nRate(series, 3);
375
376         ads->ds_ctl4 = set11nPktDurRTSCTS(series, 0)
377                 | set11nPktDurRTSCTS(series, 1);
378
379         ads->ds_ctl5 = set11nPktDurRTSCTS(series, 2)
380                 | set11nPktDurRTSCTS(series, 3);
381
382         ads->ds_ctl7 = set11nRateFlags(series, 0)
383                 | set11nRateFlags(series, 1)
384                 | set11nRateFlags(series, 2)
385                 | set11nRateFlags(series, 3)
386                 | SM(rtsctsRate, AR_RTSCTSRate);
387         last_ads->ds_ctl2 = ads->ds_ctl2;
388         last_ads->ds_ctl3 = ads->ds_ctl3;
389 }
390
391 void ath9k_hw_set11n_aggr_first(struct ath_hal *ah, struct ath_desc *ds,
392                                 u32 aggrLen)
393 {
394         struct ar5416_desc *ads = AR5416DESC(ds);
395
396         ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
397         ads->ds_ctl6 &= ~AR_AggrLen;
398         ads->ds_ctl6 |= SM(aggrLen, AR_AggrLen);
399 }
400
401 void ath9k_hw_set11n_aggr_middle(struct ath_hal *ah, struct ath_desc *ds,
402                                  u32 numDelims)
403 {
404         struct ar5416_desc *ads = AR5416DESC(ds);
405         unsigned int ctl6;
406
407         ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
408
409         ctl6 = ads->ds_ctl6;
410         ctl6 &= ~AR_PadDelim;
411         ctl6 |= SM(numDelims, AR_PadDelim);
412         ads->ds_ctl6 = ctl6;
413 }
414
415 void ath9k_hw_set11n_aggr_last(struct ath_hal *ah, struct ath_desc *ds)
416 {
417         struct ar5416_desc *ads = AR5416DESC(ds);
418
419         ads->ds_ctl1 |= AR_IsAggr;
420         ads->ds_ctl1 &= ~AR_MoreAggr;
421         ads->ds_ctl6 &= ~AR_PadDelim;
422 }
423
424 void ath9k_hw_clr11n_aggr(struct ath_hal *ah, struct ath_desc *ds)
425 {
426         struct ar5416_desc *ads = AR5416DESC(ds);
427
428         ads->ds_ctl1 &= (~AR_IsAggr & ~AR_MoreAggr);
429 }
430
431 void ath9k_hw_set11n_burstduration(struct ath_hal *ah, struct ath_desc *ds,
432                                    u32 burstDuration)
433 {
434         struct ar5416_desc *ads = AR5416DESC(ds);
435
436         ads->ds_ctl2 &= ~AR_BurstDur;
437         ads->ds_ctl2 |= SM(burstDuration, AR_BurstDur);
438 }
439
440 void ath9k_hw_set11n_virtualmorefrag(struct ath_hal *ah, struct ath_desc *ds,
441                                      u32 vmf)
442 {
443         struct ar5416_desc *ads = AR5416DESC(ds);
444
445         if (vmf)
446                 ads->ds_ctl0 |= AR_VirtMoreFrag;
447         else
448                 ads->ds_ctl0 &= ~AR_VirtMoreFrag;
449 }
450
451 void ath9k_hw_gettxintrtxqs(struct ath_hal *ah, u32 *txqs)
452 {
453         struct ath_hal_5416 *ahp = AH5416(ah);
454
455         *txqs &= ahp->ah_intrTxqs;
456         ahp->ah_intrTxqs &= ~(*txqs);
457 }
458
459 bool ath9k_hw_set_txq_props(struct ath_hal *ah, int q,
460                             const struct ath9k_tx_queue_info *qinfo)
461 {
462         u32 cw;
463         struct ath_hal_5416 *ahp = AH5416(ah);
464         struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
465         struct ath9k_tx_queue_info *qi;
466
467         if (q >= pCap->total_queues) {
468                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "invalid queue num %u\n", q);
469                 return false;
470         }
471
472         qi = &ahp->ah_txq[q];
473         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
474                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "inactive queue\n");
475                 return false;
476         }
477
478         DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "queue %p\n", qi);
479
480         qi->tqi_ver = qinfo->tqi_ver;
481         qi->tqi_subtype = qinfo->tqi_subtype;
482         qi->tqi_qflags = qinfo->tqi_qflags;
483         qi->tqi_priority = qinfo->tqi_priority;
484         if (qinfo->tqi_aifs != ATH9K_TXQ_USEDEFAULT)
485                 qi->tqi_aifs = min(qinfo->tqi_aifs, 255U);
486         else
487                 qi->tqi_aifs = INIT_AIFS;
488         if (qinfo->tqi_cwmin != ATH9K_TXQ_USEDEFAULT) {
489                 cw = min(qinfo->tqi_cwmin, 1024U);
490                 qi->tqi_cwmin = 1;
491                 while (qi->tqi_cwmin < cw)
492                         qi->tqi_cwmin = (qi->tqi_cwmin << 1) | 1;
493         } else
494                 qi->tqi_cwmin = qinfo->tqi_cwmin;
495         if (qinfo->tqi_cwmax != ATH9K_TXQ_USEDEFAULT) {
496                 cw = min(qinfo->tqi_cwmax, 1024U);
497                 qi->tqi_cwmax = 1;
498                 while (qi->tqi_cwmax < cw)
499                         qi->tqi_cwmax = (qi->tqi_cwmax << 1) | 1;
500         } else
501                 qi->tqi_cwmax = INIT_CWMAX;
502
503         if (qinfo->tqi_shretry != 0)
504                 qi->tqi_shretry = min((u32) qinfo->tqi_shretry, 15U);
505         else
506                 qi->tqi_shretry = INIT_SH_RETRY;
507         if (qinfo->tqi_lgretry != 0)
508                 qi->tqi_lgretry = min((u32) qinfo->tqi_lgretry, 15U);
509         else
510                 qi->tqi_lgretry = INIT_LG_RETRY;
511         qi->tqi_cbrPeriod = qinfo->tqi_cbrPeriod;
512         qi->tqi_cbrOverflowLimit = qinfo->tqi_cbrOverflowLimit;
513         qi->tqi_burstTime = qinfo->tqi_burstTime;
514         qi->tqi_readyTime = qinfo->tqi_readyTime;
515
516         switch (qinfo->tqi_subtype) {
517         case ATH9K_WME_UPSD:
518                 if (qi->tqi_type == ATH9K_TX_QUEUE_DATA)
519                         qi->tqi_intFlags = ATH9K_TXQ_USE_LOCKOUT_BKOFF_DIS;
520                 break;
521         default:
522                 break;
523         }
524
525         return true;
526 }
527
528 bool ath9k_hw_get_txq_props(struct ath_hal *ah, int q,
529                             struct ath9k_tx_queue_info *qinfo)
530 {
531         struct ath_hal_5416 *ahp = AH5416(ah);
532         struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
533         struct ath9k_tx_queue_info *qi;
534
535         if (q >= pCap->total_queues) {
536                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "invalid queue num %u\n", q);
537                 return false;
538         }
539
540         qi = &ahp->ah_txq[q];
541         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
542                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "inactive queue\n");
543                 return false;
544         }
545
546         qinfo->tqi_qflags = qi->tqi_qflags;
547         qinfo->tqi_ver = qi->tqi_ver;
548         qinfo->tqi_subtype = qi->tqi_subtype;
549         qinfo->tqi_qflags = qi->tqi_qflags;
550         qinfo->tqi_priority = qi->tqi_priority;
551         qinfo->tqi_aifs = qi->tqi_aifs;
552         qinfo->tqi_cwmin = qi->tqi_cwmin;
553         qinfo->tqi_cwmax = qi->tqi_cwmax;
554         qinfo->tqi_shretry = qi->tqi_shretry;
555         qinfo->tqi_lgretry = qi->tqi_lgretry;
556         qinfo->tqi_cbrPeriod = qi->tqi_cbrPeriod;
557         qinfo->tqi_cbrOverflowLimit = qi->tqi_cbrOverflowLimit;
558         qinfo->tqi_burstTime = qi->tqi_burstTime;
559         qinfo->tqi_readyTime = qi->tqi_readyTime;
560
561         return true;
562 }
563
564 int ath9k_hw_setuptxqueue(struct ath_hal *ah, enum ath9k_tx_queue type,
565                           const struct ath9k_tx_queue_info *qinfo)
566 {
567         struct ath_hal_5416 *ahp = AH5416(ah);
568         struct ath9k_tx_queue_info *qi;
569         struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
570         int q;
571
572         switch (type) {
573         case ATH9K_TX_QUEUE_BEACON:
574                 q = pCap->total_queues - 1;
575                 break;
576         case ATH9K_TX_QUEUE_CAB:
577                 q = pCap->total_queues - 2;
578                 break;
579         case ATH9K_TX_QUEUE_PSPOLL:
580                 q = 1;
581                 break;
582         case ATH9K_TX_QUEUE_UAPSD:
583                 q = pCap->total_queues - 3;
584                 break;
585         case ATH9K_TX_QUEUE_DATA:
586                 for (q = 0; q < pCap->total_queues; q++)
587                         if (ahp->ah_txq[q].tqi_type ==
588                             ATH9K_TX_QUEUE_INACTIVE)
589                                 break;
590                 if (q == pCap->total_queues) {
591                         DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
592                                 "no available tx queue\n");
593                         return -1;
594                 }
595                 break;
596         default:
597                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "bad tx queue type %u\n", type);
598                 return -1;
599         }
600
601         DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "queue %u\n", q);
602
603         qi = &ahp->ah_txq[q];
604         if (qi->tqi_type != ATH9K_TX_QUEUE_INACTIVE) {
605                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
606                         "tx queue %u already active\n", q);
607                 return -1;
608         }
609         memset(qi, 0, sizeof(struct ath9k_tx_queue_info));
610         qi->tqi_type = type;
611         if (qinfo == NULL) {
612                 qi->tqi_qflags =
613                         TXQ_FLAG_TXOKINT_ENABLE
614                         | TXQ_FLAG_TXERRINT_ENABLE
615                         | TXQ_FLAG_TXDESCINT_ENABLE | TXQ_FLAG_TXURNINT_ENABLE;
616                 qi->tqi_aifs = INIT_AIFS;
617                 qi->tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
618                 qi->tqi_cwmax = INIT_CWMAX;
619                 qi->tqi_shretry = INIT_SH_RETRY;
620                 qi->tqi_lgretry = INIT_LG_RETRY;
621                 qi->tqi_physCompBuf = 0;
622         } else {
623                 qi->tqi_physCompBuf = qinfo->tqi_physCompBuf;
624                 (void) ath9k_hw_set_txq_props(ah, q, qinfo);
625         }
626
627         return q;
628 }
629
630 bool ath9k_hw_releasetxqueue(struct ath_hal *ah, u32 q)
631 {
632         struct ath_hal_5416 *ahp = AH5416(ah);
633         struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
634         struct ath9k_tx_queue_info *qi;
635
636         if (q >= pCap->total_queues) {
637                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "invalid queue num %u\n", q);
638                 return false;
639         }
640         qi = &ahp->ah_txq[q];
641         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
642                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "inactive queue %u\n", q);
643                 return false;
644         }
645
646         DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "release queue %u\n", q);
647
648         qi->tqi_type = ATH9K_TX_QUEUE_INACTIVE;
649         ahp->ah_txOkInterruptMask &= ~(1 << q);
650         ahp->ah_txErrInterruptMask &= ~(1 << q);
651         ahp->ah_txDescInterruptMask &= ~(1 << q);
652         ahp->ah_txEolInterruptMask &= ~(1 << q);
653         ahp->ah_txUrnInterruptMask &= ~(1 << q);
654         ath9k_hw_set_txq_interrupts(ah, qi);
655
656         return true;
657 }
658
659 bool ath9k_hw_resettxqueue(struct ath_hal *ah, u32 q)
660 {
661         struct ath_hal_5416 *ahp = AH5416(ah);
662         struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
663         struct ath9k_channel *chan = ah->ah_curchan;
664         struct ath9k_tx_queue_info *qi;
665         u32 cwMin, chanCwMin, value;
666
667         if (q >= pCap->total_queues) {
668                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "invalid queue num %u\n", q);
669                 return false;
670         }
671
672         qi = &ahp->ah_txq[q];
673         if (qi->tqi_type == ATH9K_TX_QUEUE_INACTIVE) {
674                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "inactive queue %u\n", q);
675                 return true;
676         }
677
678         DPRINTF(ah->ah_sc, ATH_DBG_QUEUE, "reset queue %u\n", q);
679
680         if (qi->tqi_cwmin == ATH9K_TXQ_USEDEFAULT) {
681                 if (chan && IS_CHAN_B(chan))
682                         chanCwMin = INIT_CWMIN_11B;
683                 else
684                         chanCwMin = INIT_CWMIN;
685
686                 for (cwMin = 1; cwMin < chanCwMin; cwMin = (cwMin << 1) | 1);
687         } else
688                 cwMin = qi->tqi_cwmin;
689
690         REG_WRITE(ah, AR_DLCL_IFS(q),
691                   SM(cwMin, AR_D_LCL_IFS_CWMIN) |
692                   SM(qi->tqi_cwmax, AR_D_LCL_IFS_CWMAX) |
693                   SM(qi->tqi_aifs, AR_D_LCL_IFS_AIFS));
694
695         REG_WRITE(ah, AR_DRETRY_LIMIT(q),
696                   SM(INIT_SSH_RETRY, AR_D_RETRY_LIMIT_STA_SH) |
697                   SM(INIT_SLG_RETRY, AR_D_RETRY_LIMIT_STA_LG) |
698                   SM(qi->tqi_shretry, AR_D_RETRY_LIMIT_FR_SH));
699
700         REG_WRITE(ah, AR_QMISC(q), AR_Q_MISC_DCU_EARLY_TERM_REQ);
701         REG_WRITE(ah, AR_DMISC(q),
702                   AR_D_MISC_CW_BKOFF_EN | AR_D_MISC_FRAG_WAIT_EN | 0x2);
703
704         if (qi->tqi_cbrPeriod) {
705                 REG_WRITE(ah, AR_QCBRCFG(q),
706                           SM(qi->tqi_cbrPeriod, AR_Q_CBRCFG_INTERVAL) |
707                           SM(qi->tqi_cbrOverflowLimit, AR_Q_CBRCFG_OVF_THRESH));
708                 REG_WRITE(ah, AR_QMISC(q),
709                           REG_READ(ah, AR_QMISC(q)) | AR_Q_MISC_FSP_CBR |
710                           (qi->tqi_cbrOverflowLimit ?
711                            AR_Q_MISC_CBR_EXP_CNTR_LIMIT_EN : 0));
712         }
713         if (qi->tqi_readyTime && (qi->tqi_type != ATH9K_TX_QUEUE_CAB)) {
714                 REG_WRITE(ah, AR_QRDYTIMECFG(q),
715                           SM(qi->tqi_readyTime, AR_Q_RDYTIMECFG_DURATION) |
716                           AR_Q_RDYTIMECFG_EN);
717         }
718
719         REG_WRITE(ah, AR_DCHNTIME(q),
720                   SM(qi->tqi_burstTime, AR_D_CHNTIME_DUR) |
721                   (qi->tqi_burstTime ? AR_D_CHNTIME_EN : 0));
722
723         if (qi->tqi_burstTime
724             && (qi->tqi_qflags & TXQ_FLAG_RDYTIME_EXP_POLICY_ENABLE)) {
725                 REG_WRITE(ah, AR_QMISC(q),
726                           REG_READ(ah, AR_QMISC(q)) |
727                           AR_Q_MISC_RDYTIME_EXP_POLICY);
728
729         }
730
731         if (qi->tqi_qflags & TXQ_FLAG_BACKOFF_DISABLE) {
732                 REG_WRITE(ah, AR_DMISC(q),
733                           REG_READ(ah, AR_DMISC(q)) |
734                           AR_D_MISC_POST_FR_BKOFF_DIS);
735         }
736         if (qi->tqi_qflags & TXQ_FLAG_FRAG_BURST_BACKOFF_ENABLE) {
737                 REG_WRITE(ah, AR_DMISC(q),
738                           REG_READ(ah, AR_DMISC(q)) |
739                           AR_D_MISC_FRAG_BKOFF_EN);
740         }
741         switch (qi->tqi_type) {
742         case ATH9K_TX_QUEUE_BEACON:
743                 REG_WRITE(ah, AR_QMISC(q), REG_READ(ah, AR_QMISC(q))
744                           | AR_Q_MISC_FSP_DBA_GATED
745                           | AR_Q_MISC_BEACON_USE
746                           | AR_Q_MISC_CBR_INCR_DIS1);
747
748                 REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
749                           | (AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL <<
750                              AR_D_MISC_ARB_LOCKOUT_CNTRL_S)
751                           | AR_D_MISC_BEACON_USE
752                           | AR_D_MISC_POST_FR_BKOFF_DIS);
753                 break;
754         case ATH9K_TX_QUEUE_CAB:
755                 REG_WRITE(ah, AR_QMISC(q), REG_READ(ah, AR_QMISC(q))
756                           | AR_Q_MISC_FSP_DBA_GATED
757                           | AR_Q_MISC_CBR_INCR_DIS1
758                           | AR_Q_MISC_CBR_INCR_DIS0);
759                 value = (qi->tqi_readyTime -
760                          (ah->ah_config.sw_beacon_response_time -
761                           ah->ah_config.dma_beacon_response_time) -
762                          ah->ah_config.additional_swba_backoff) * 1024;
763                 REG_WRITE(ah, AR_QRDYTIMECFG(q),
764                           value | AR_Q_RDYTIMECFG_EN);
765                 REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q))
766                           | (AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL <<
767                              AR_D_MISC_ARB_LOCKOUT_CNTRL_S));
768                 break;
769         case ATH9K_TX_QUEUE_PSPOLL:
770                 REG_WRITE(ah, AR_QMISC(q),
771                           REG_READ(ah, AR_QMISC(q)) | AR_Q_MISC_CBR_INCR_DIS1);
772                 break;
773         case ATH9K_TX_QUEUE_UAPSD:
774                 REG_WRITE(ah, AR_DMISC(q), REG_READ(ah, AR_DMISC(q)) |
775                           AR_D_MISC_POST_FR_BKOFF_DIS);
776                 break;
777         default:
778                 break;
779         }
780
781         if (qi->tqi_intFlags & ATH9K_TXQ_USE_LOCKOUT_BKOFF_DIS) {
782                 REG_WRITE(ah, AR_DMISC(q),
783                           REG_READ(ah, AR_DMISC(q)) |
784                           SM(AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL,
785                              AR_D_MISC_ARB_LOCKOUT_CNTRL) |
786                           AR_D_MISC_POST_FR_BKOFF_DIS);
787         }
788
789         if (qi->tqi_qflags & TXQ_FLAG_TXOKINT_ENABLE)
790                 ahp->ah_txOkInterruptMask |= 1 << q;
791         else
792                 ahp->ah_txOkInterruptMask &= ~(1 << q);
793         if (qi->tqi_qflags & TXQ_FLAG_TXERRINT_ENABLE)
794                 ahp->ah_txErrInterruptMask |= 1 << q;
795         else
796                 ahp->ah_txErrInterruptMask &= ~(1 << q);
797         if (qi->tqi_qflags & TXQ_FLAG_TXDESCINT_ENABLE)
798                 ahp->ah_txDescInterruptMask |= 1 << q;
799         else
800                 ahp->ah_txDescInterruptMask &= ~(1 << q);
801         if (qi->tqi_qflags & TXQ_FLAG_TXEOLINT_ENABLE)
802                 ahp->ah_txEolInterruptMask |= 1 << q;
803         else
804                 ahp->ah_txEolInterruptMask &= ~(1 << q);
805         if (qi->tqi_qflags & TXQ_FLAG_TXURNINT_ENABLE)
806                 ahp->ah_txUrnInterruptMask |= 1 << q;
807         else
808                 ahp->ah_txUrnInterruptMask &= ~(1 << q);
809         ath9k_hw_set_txq_interrupts(ah, qi);
810
811         return true;
812 }
813
814 int ath9k_hw_rxprocdesc(struct ath_hal *ah, struct ath_desc *ds,
815                         u32 pa, struct ath_desc *nds, u64 tsf)
816 {
817         struct ar5416_desc ads;
818         struct ar5416_desc *adsp = AR5416DESC(ds);
819         u32 phyerr;
820
821         if ((adsp->ds_rxstatus8 & AR_RxDone) == 0)
822                 return -EINPROGRESS;
823
824         ads.u.rx = adsp->u.rx;
825
826         ds->ds_rxstat.rs_status = 0;
827         ds->ds_rxstat.rs_flags = 0;
828
829         ds->ds_rxstat.rs_datalen = ads.ds_rxstatus1 & AR_DataLen;
830         ds->ds_rxstat.rs_tstamp = ads.AR_RcvTimestamp;
831
832         ds->ds_rxstat.rs_rssi = MS(ads.ds_rxstatus4, AR_RxRSSICombined);
833         ds->ds_rxstat.rs_rssi_ctl0 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt00);
834         ds->ds_rxstat.rs_rssi_ctl1 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt01);
835         ds->ds_rxstat.rs_rssi_ctl2 = MS(ads.ds_rxstatus0, AR_RxRSSIAnt02);
836         ds->ds_rxstat.rs_rssi_ext0 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt10);
837         ds->ds_rxstat.rs_rssi_ext1 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt11);
838         ds->ds_rxstat.rs_rssi_ext2 = MS(ads.ds_rxstatus4, AR_RxRSSIAnt12);
839         if (ads.ds_rxstatus8 & AR_RxKeyIdxValid)
840                 ds->ds_rxstat.rs_keyix = MS(ads.ds_rxstatus8, AR_KeyIdx);
841         else
842                 ds->ds_rxstat.rs_keyix = ATH9K_RXKEYIX_INVALID;
843
844         ds->ds_rxstat.rs_rate = RXSTATUS_RATE(ah, (&ads));
845         ds->ds_rxstat.rs_more = (ads.ds_rxstatus1 & AR_RxMore) ? 1 : 0;
846
847         ds->ds_rxstat.rs_isaggr = (ads.ds_rxstatus8 & AR_RxAggr) ? 1 : 0;
848         ds->ds_rxstat.rs_moreaggr =
849                 (ads.ds_rxstatus8 & AR_RxMoreAggr) ? 1 : 0;
850         ds->ds_rxstat.rs_antenna = MS(ads.ds_rxstatus3, AR_RxAntenna);
851         ds->ds_rxstat.rs_flags =
852                 (ads.ds_rxstatus3 & AR_GI) ? ATH9K_RX_GI : 0;
853         ds->ds_rxstat.rs_flags |=
854                 (ads.ds_rxstatus3 & AR_2040) ? ATH9K_RX_2040 : 0;
855
856         if (ads.ds_rxstatus8 & AR_PreDelimCRCErr)
857                 ds->ds_rxstat.rs_flags |= ATH9K_RX_DELIM_CRC_PRE;
858         if (ads.ds_rxstatus8 & AR_PostDelimCRCErr)
859                 ds->ds_rxstat.rs_flags |= ATH9K_RX_DELIM_CRC_POST;
860         if (ads.ds_rxstatus8 & AR_DecryptBusyErr)
861                 ds->ds_rxstat.rs_flags |= ATH9K_RX_DECRYPT_BUSY;
862
863         if ((ads.ds_rxstatus8 & AR_RxFrameOK) == 0) {
864                 if (ads.ds_rxstatus8 & AR_CRCErr)
865                         ds->ds_rxstat.rs_status |= ATH9K_RXERR_CRC;
866                 else if (ads.ds_rxstatus8 & AR_PHYErr) {
867                         ds->ds_rxstat.rs_status |= ATH9K_RXERR_PHY;
868                         phyerr = MS(ads.ds_rxstatus8, AR_PHYErrCode);
869                         ds->ds_rxstat.rs_phyerr = phyerr;
870                 } else if (ads.ds_rxstatus8 & AR_DecryptCRCErr)
871                         ds->ds_rxstat.rs_status |= ATH9K_RXERR_DECRYPT;
872                 else if (ads.ds_rxstatus8 & AR_MichaelErr)
873                         ds->ds_rxstat.rs_status |= ATH9K_RXERR_MIC;
874         }
875
876         return 0;
877 }
878
879 bool ath9k_hw_setuprxdesc(struct ath_hal *ah, struct ath_desc *ds,
880                           u32 size, u32 flags)
881 {
882         struct ar5416_desc *ads = AR5416DESC(ds);
883         struct ath9k_hw_capabilities *pCap = &ah->ah_caps;
884
885         ads->ds_ctl1 = size & AR_BufLen;
886         if (flags & ATH9K_RXDESC_INTREQ)
887                 ads->ds_ctl1 |= AR_RxIntrReq;
888
889         ads->ds_rxstatus8 &= ~AR_RxDone;
890         if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
891                 memset(&(ads->u), 0, sizeof(ads->u));
892
893         return true;
894 }
895
896 bool ath9k_hw_setrxabort(struct ath_hal *ah, bool set)
897 {
898         u32 reg;
899
900         if (set) {
901                 REG_SET_BIT(ah, AR_DIAG_SW,
902                             (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
903
904                 if (!ath9k_hw_wait(ah, AR_OBS_BUS_1, AR_OBS_BUS_1_RX_STATE, 0)) {
905                         REG_CLR_BIT(ah, AR_DIAG_SW,
906                                     (AR_DIAG_RX_DIS |
907                                      AR_DIAG_RX_ABORT));
908
909                         reg = REG_READ(ah, AR_OBS_BUS_1);
910                         DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
911                                 "rx failed to go idle in 10 ms RXSM=0x%x\n", reg);
912
913                         return false;
914                 }
915         } else {
916                 REG_CLR_BIT(ah, AR_DIAG_SW,
917                             (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
918         }
919
920         return true;
921 }
922
923 void ath9k_hw_putrxbuf(struct ath_hal *ah, u32 rxdp)
924 {
925         REG_WRITE(ah, AR_RXDP, rxdp);
926 }
927
928 void ath9k_hw_rxena(struct ath_hal *ah)
929 {
930         REG_WRITE(ah, AR_CR, AR_CR_RXE);
931 }
932
933 void ath9k_hw_startpcureceive(struct ath_hal *ah)
934 {
935         ath9k_enable_mib_counters(ah);
936
937         ath9k_ani_reset(ah);
938
939         REG_CLR_BIT(ah, AR_DIAG_SW, (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
940 }
941
942 void ath9k_hw_stoppcurecv(struct ath_hal *ah)
943 {
944         REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_RX_DIS);
945
946         ath9k_hw_disable_mib_counters(ah);
947 }
948
949 bool ath9k_hw_stopdmarecv(struct ath_hal *ah)
950 {
951         REG_WRITE(ah, AR_CR, AR_CR_RXD);
952
953         if (!ath9k_hw_wait(ah, AR_CR, AR_CR_RXE, 0)) {
954                 DPRINTF(ah->ah_sc, ATH_DBG_QUEUE,
955                         "dma failed to stop in 10ms\n"
956                         "AR_CR=0x%08x\nAR_DIAG_SW=0x%08x\n",
957                         REG_READ(ah, AR_CR), REG_READ(ah, AR_DIAG_SW));
958                 return false;
959         } else {
960                 return true;
961         }
962 }