ath9k: simplify hw reset locking
[linux-2.6-block.git] / drivers / net / wireless / ath / ath9k / ar9002_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 #define AR_BufLen           0x00000fff
20
21 static void ar9002_hw_rx_enable(struct ath_hw *ah)
22 {
23         REG_WRITE(ah, AR_CR, AR_CR_RXE);
24 }
25
26 static void ar9002_hw_set_desc_link(void *ds, u32 ds_link)
27 {
28         ((struct ath_desc*) ds)->ds_link = ds_link;
29 }
30
31 static void ar9002_hw_get_desc_link(void *ds, u32 **ds_link)
32 {
33         *ds_link = &((struct ath_desc *)ds)->ds_link;
34 }
35
36 static bool ar9002_hw_get_isr(struct ath_hw *ah, enum ath9k_int *masked)
37 {
38         u32 isr = 0;
39         u32 mask2 = 0;
40         struct ath9k_hw_capabilities *pCap = &ah->caps;
41         u32 sync_cause = 0;
42         bool fatal_int = false;
43         struct ath_common *common = ath9k_hw_common(ah);
44
45         if (!AR_SREV_9100(ah)) {
46                 if (REG_READ(ah, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) {
47                         if ((REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M)
48                             == AR_RTC_STATUS_ON) {
49                                 isr = REG_READ(ah, AR_ISR);
50                         }
51                 }
52
53                 sync_cause = REG_READ(ah, AR_INTR_SYNC_CAUSE) &
54                         AR_INTR_SYNC_DEFAULT;
55
56                 *masked = 0;
57
58                 if (!isr && !sync_cause)
59                         return false;
60         } else {
61                 *masked = 0;
62                 isr = REG_READ(ah, AR_ISR);
63         }
64
65         if (isr) {
66                 if (isr & AR_ISR_BCNMISC) {
67                         u32 isr2;
68                         isr2 = REG_READ(ah, AR_ISR_S2);
69                         if (isr2 & AR_ISR_S2_TIM)
70                                 mask2 |= ATH9K_INT_TIM;
71                         if (isr2 & AR_ISR_S2_DTIM)
72                                 mask2 |= ATH9K_INT_DTIM;
73                         if (isr2 & AR_ISR_S2_DTIMSYNC)
74                                 mask2 |= ATH9K_INT_DTIMSYNC;
75                         if (isr2 & (AR_ISR_S2_CABEND))
76                                 mask2 |= ATH9K_INT_CABEND;
77                         if (isr2 & AR_ISR_S2_GTT)
78                                 mask2 |= ATH9K_INT_GTT;
79                         if (isr2 & AR_ISR_S2_CST)
80                                 mask2 |= ATH9K_INT_CST;
81                         if (isr2 & AR_ISR_S2_TSFOOR)
82                                 mask2 |= ATH9K_INT_TSFOOR;
83                 }
84
85                 isr = REG_READ(ah, AR_ISR_RAC);
86                 if (isr == 0xffffffff) {
87                         *masked = 0;
88                         return false;
89                 }
90
91                 *masked = isr & ATH9K_INT_COMMON;
92
93                 if (isr & (AR_ISR_RXMINTR | AR_ISR_RXINTM |
94                            AR_ISR_RXOK | AR_ISR_RXERR))
95                         *masked |= ATH9K_INT_RX;
96
97                 if (isr &
98                     (AR_ISR_TXOK | AR_ISR_TXDESC | AR_ISR_TXERR |
99                      AR_ISR_TXEOL)) {
100                         u32 s0_s, s1_s;
101
102                         *masked |= ATH9K_INT_TX;
103
104                         s0_s = REG_READ(ah, AR_ISR_S0_S);
105                         ah->intr_txqs |= MS(s0_s, AR_ISR_S0_QCU_TXOK);
106                         ah->intr_txqs |= MS(s0_s, AR_ISR_S0_QCU_TXDESC);
107
108                         s1_s = REG_READ(ah, AR_ISR_S1_S);
109                         ah->intr_txqs |= MS(s1_s, AR_ISR_S1_QCU_TXERR);
110                         ah->intr_txqs |= MS(s1_s, AR_ISR_S1_QCU_TXEOL);
111                 }
112
113                 if (isr & AR_ISR_RXORN) {
114                         ath_print(common, ATH_DBG_INTERRUPT,
115                                   "receive FIFO overrun interrupt\n");
116                 }
117
118                 *masked |= mask2;
119         }
120
121         if (AR_SREV_9100(ah))
122                 return true;
123
124         if (isr & AR_ISR_GENTMR) {
125                 u32 s5_s;
126
127                 s5_s = REG_READ(ah, AR_ISR_S5_S);
128                 ah->intr_gen_timer_trigger =
129                                 MS(s5_s, AR_ISR_S5_GENTIMER_TRIG);
130
131                 ah->intr_gen_timer_thresh =
132                         MS(s5_s, AR_ISR_S5_GENTIMER_THRESH);
133
134                 if (ah->intr_gen_timer_trigger)
135                         *masked |= ATH9K_INT_GENTIMER;
136
137                 if ((s5_s & AR_ISR_S5_TIM_TIMER) &&
138                     !(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
139                         *masked |= ATH9K_INT_TIM_TIMER;
140         }
141
142         if (sync_cause) {
143                 fatal_int =
144                         (sync_cause &
145                          (AR_INTR_SYNC_HOST1_FATAL | AR_INTR_SYNC_HOST1_PERR))
146                         ? true : false;
147
148                 if (fatal_int) {
149                         if (sync_cause & AR_INTR_SYNC_HOST1_FATAL) {
150                                 ath_print(common, ATH_DBG_ANY,
151                                           "received PCI FATAL interrupt\n");
152                         }
153                         if (sync_cause & AR_INTR_SYNC_HOST1_PERR) {
154                                 ath_print(common, ATH_DBG_ANY,
155                                           "received PCI PERR interrupt\n");
156                         }
157                         *masked |= ATH9K_INT_FATAL;
158                 }
159                 if (sync_cause & AR_INTR_SYNC_RADM_CPL_TIMEOUT) {
160                         ath_print(common, ATH_DBG_INTERRUPT,
161                                   "AR_INTR_SYNC_RADM_CPL_TIMEOUT\n");
162                         REG_WRITE(ah, AR_RC, AR_RC_HOSTIF);
163                         REG_WRITE(ah, AR_RC, 0);
164                         *masked |= ATH9K_INT_FATAL;
165                 }
166                 if (sync_cause & AR_INTR_SYNC_LOCAL_TIMEOUT) {
167                         ath_print(common, ATH_DBG_INTERRUPT,
168                                   "AR_INTR_SYNC_LOCAL_TIMEOUT\n");
169                 }
170
171                 REG_WRITE(ah, AR_INTR_SYNC_CAUSE_CLR, sync_cause);
172                 (void) REG_READ(ah, AR_INTR_SYNC_CAUSE_CLR);
173         }
174
175         return true;
176 }
177
178 static void ar9002_hw_fill_txdesc(struct ath_hw *ah, void *ds, u32 seglen,
179                                   bool is_firstseg, bool is_lastseg,
180                                   const void *ds0, dma_addr_t buf_addr,
181                                   unsigned int qcu)
182 {
183         struct ar5416_desc *ads = AR5416DESC(ds);
184
185         ads->ds_data = buf_addr;
186
187         if (is_firstseg) {
188                 ads->ds_ctl1 |= seglen | (is_lastseg ? 0 : AR_TxMore);
189         } else if (is_lastseg) {
190                 ads->ds_ctl0 = 0;
191                 ads->ds_ctl1 = seglen;
192                 ads->ds_ctl2 = AR5416DESC_CONST(ds0)->ds_ctl2;
193                 ads->ds_ctl3 = AR5416DESC_CONST(ds0)->ds_ctl3;
194         } else {
195                 ads->ds_ctl0 = 0;
196                 ads->ds_ctl1 = seglen | AR_TxMore;
197                 ads->ds_ctl2 = 0;
198                 ads->ds_ctl3 = 0;
199         }
200         ads->ds_txstatus0 = ads->ds_txstatus1 = 0;
201         ads->ds_txstatus2 = ads->ds_txstatus3 = 0;
202         ads->ds_txstatus4 = ads->ds_txstatus5 = 0;
203         ads->ds_txstatus6 = ads->ds_txstatus7 = 0;
204         ads->ds_txstatus8 = ads->ds_txstatus9 = 0;
205 }
206
207 static int ar9002_hw_proc_txdesc(struct ath_hw *ah, void *ds,
208                                  struct ath_tx_status *ts)
209 {
210         struct ar5416_desc *ads = AR5416DESC(ds);
211         u32 status;
212
213         status = ACCESS_ONCE(ads->ds_txstatus9);
214         if ((status & AR_TxDone) == 0)
215                 return -EINPROGRESS;
216
217         ts->ts_tstamp = ads->AR_SendTimestamp;
218         ts->ts_status = 0;
219         ts->ts_flags = 0;
220
221         if (status & AR_TxOpExceeded)
222                 ts->ts_status |= ATH9K_TXERR_XTXOP;
223         ts->tid = MS(status, AR_TxTid);
224         ts->ts_rateindex = MS(status, AR_FinalTxIdx);
225         ts->ts_seqnum = MS(status, AR_SeqNum);
226
227         status = ACCESS_ONCE(ads->ds_txstatus0);
228         ts->ts_rssi_ctl0 = MS(status, AR_TxRSSIAnt00);
229         ts->ts_rssi_ctl1 = MS(status, AR_TxRSSIAnt01);
230         ts->ts_rssi_ctl2 = MS(status, AR_TxRSSIAnt02);
231         if (status & AR_TxBaStatus) {
232                 ts->ts_flags |= ATH9K_TX_BA;
233                 ts->ba_low = ads->AR_BaBitmapLow;
234                 ts->ba_high = ads->AR_BaBitmapHigh;
235         }
236
237         status = ACCESS_ONCE(ads->ds_txstatus1);
238         if (status & AR_FrmXmitOK)
239                 ts->ts_status |= ATH9K_TX_ACKED;
240         else {
241                 if (status & AR_ExcessiveRetries)
242                         ts->ts_status |= ATH9K_TXERR_XRETRY;
243                 if (status & AR_Filtered)
244                         ts->ts_status |= ATH9K_TXERR_FILT;
245                 if (status & AR_FIFOUnderrun) {
246                         ts->ts_status |= ATH9K_TXERR_FIFO;
247                         ath9k_hw_updatetxtriglevel(ah, true);
248                 }
249         }
250         if (status & AR_TxTimerExpired)
251                 ts->ts_status |= ATH9K_TXERR_TIMER_EXPIRED;
252         if (status & AR_DescCfgErr)
253                 ts->ts_flags |= ATH9K_TX_DESC_CFG_ERR;
254         if (status & AR_TxDataUnderrun) {
255                 ts->ts_flags |= ATH9K_TX_DATA_UNDERRUN;
256                 ath9k_hw_updatetxtriglevel(ah, true);
257         }
258         if (status & AR_TxDelimUnderrun) {
259                 ts->ts_flags |= ATH9K_TX_DELIM_UNDERRUN;
260                 ath9k_hw_updatetxtriglevel(ah, true);
261         }
262         ts->ts_shortretry = MS(status, AR_RTSFailCnt);
263         ts->ts_longretry = MS(status, AR_DataFailCnt);
264         ts->ts_virtcol = MS(status, AR_VirtRetryCnt);
265
266         status = ACCESS_ONCE(ads->ds_txstatus5);
267         ts->ts_rssi = MS(status, AR_TxRSSICombined);
268         ts->ts_rssi_ext0 = MS(status, AR_TxRSSIAnt10);
269         ts->ts_rssi_ext1 = MS(status, AR_TxRSSIAnt11);
270         ts->ts_rssi_ext2 = MS(status, AR_TxRSSIAnt12);
271
272         ts->evm0 = ads->AR_TxEVM0;
273         ts->evm1 = ads->AR_TxEVM1;
274         ts->evm2 = ads->AR_TxEVM2;
275
276         return 0;
277 }
278
279 static void ar9002_hw_set11n_txdesc(struct ath_hw *ah, void *ds,
280                                     u32 pktLen, enum ath9k_pkt_type type,
281                                     u32 txPower, u32 keyIx,
282                                     enum ath9k_key_type keyType, u32 flags)
283 {
284         struct ar5416_desc *ads = AR5416DESC(ds);
285
286         txPower += ah->txpower_indexoffset;
287         if (txPower > 63)
288                 txPower = 63;
289
290         ads->ds_ctl0 = (pktLen & AR_FrameLen)
291                 | (flags & ATH9K_TXDESC_VMF ? AR_VirtMoreFrag : 0)
292                 | SM(txPower, AR_XmitPower)
293                 | (flags & ATH9K_TXDESC_VEOL ? AR_VEOL : 0)
294                 | (flags & ATH9K_TXDESC_CLRDMASK ? AR_ClrDestMask : 0)
295                 | (flags & ATH9K_TXDESC_INTREQ ? AR_TxIntrReq : 0)
296                 | (keyIx != ATH9K_TXKEYIX_INVALID ? AR_DestIdxValid : 0);
297
298         ads->ds_ctl1 =
299                 (keyIx != ATH9K_TXKEYIX_INVALID ? SM(keyIx, AR_DestIdx) : 0)
300                 | SM(type, AR_FrameType)
301                 | (flags & ATH9K_TXDESC_NOACK ? AR_NoAck : 0)
302                 | (flags & ATH9K_TXDESC_EXT_ONLY ? AR_ExtOnly : 0)
303                 | (flags & ATH9K_TXDESC_EXT_AND_CTL ? AR_ExtAndCtl : 0);
304
305         ads->ds_ctl6 = SM(keyType, AR_EncrType);
306
307         if (AR_SREV_9285(ah) || AR_SREV_9271(ah)) {
308                 ads->ds_ctl8 = 0;
309                 ads->ds_ctl9 = 0;
310                 ads->ds_ctl10 = 0;
311                 ads->ds_ctl11 = 0;
312         }
313 }
314
315 static void ar9002_hw_set11n_ratescenario(struct ath_hw *ah, void *ds,
316                                           void *lastds,
317                                           u32 durUpdateEn, u32 rtsctsRate,
318                                           u32 rtsctsDuration,
319                                           struct ath9k_11n_rate_series series[],
320                                           u32 nseries, u32 flags)
321 {
322         struct ar5416_desc *ads = AR5416DESC(ds);
323         struct ar5416_desc *last_ads = AR5416DESC(lastds);
324         u32 ds_ctl0;
325
326         if (flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA)) {
327                 ds_ctl0 = ads->ds_ctl0;
328
329                 if (flags & ATH9K_TXDESC_RTSENA) {
330                         ds_ctl0 &= ~AR_CTSEnable;
331                         ds_ctl0 |= AR_RTSEnable;
332                 } else {
333                         ds_ctl0 &= ~AR_RTSEnable;
334                         ds_ctl0 |= AR_CTSEnable;
335                 }
336
337                 ads->ds_ctl0 = ds_ctl0;
338         } else {
339                 ads->ds_ctl0 =
340                         (ads->ds_ctl0 & ~(AR_RTSEnable | AR_CTSEnable));
341         }
342
343         ads->ds_ctl2 = set11nTries(series, 0)
344                 | set11nTries(series, 1)
345                 | set11nTries(series, 2)
346                 | set11nTries(series, 3)
347                 | (durUpdateEn ? AR_DurUpdateEna : 0)
348                 | SM(0, AR_BurstDur);
349
350         ads->ds_ctl3 = set11nRate(series, 0)
351                 | set11nRate(series, 1)
352                 | set11nRate(series, 2)
353                 | set11nRate(series, 3);
354
355         ads->ds_ctl4 = set11nPktDurRTSCTS(series, 0)
356                 | set11nPktDurRTSCTS(series, 1);
357
358         ads->ds_ctl5 = set11nPktDurRTSCTS(series, 2)
359                 | set11nPktDurRTSCTS(series, 3);
360
361         ads->ds_ctl7 = set11nRateFlags(series, 0)
362                 | set11nRateFlags(series, 1)
363                 | set11nRateFlags(series, 2)
364                 | set11nRateFlags(series, 3)
365                 | SM(rtsctsRate, AR_RTSCTSRate);
366         last_ads->ds_ctl2 = ads->ds_ctl2;
367         last_ads->ds_ctl3 = ads->ds_ctl3;
368 }
369
370 static void ar9002_hw_set11n_aggr_first(struct ath_hw *ah, void *ds,
371                                         u32 aggrLen)
372 {
373         struct ar5416_desc *ads = AR5416DESC(ds);
374
375         ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
376         ads->ds_ctl6 &= ~AR_AggrLen;
377         ads->ds_ctl6 |= SM(aggrLen, AR_AggrLen);
378 }
379
380 static void ar9002_hw_set11n_aggr_middle(struct ath_hw *ah, void *ds,
381                                          u32 numDelims)
382 {
383         struct ar5416_desc *ads = AR5416DESC(ds);
384         unsigned int ctl6;
385
386         ads->ds_ctl1 |= (AR_IsAggr | AR_MoreAggr);
387
388         ctl6 = ads->ds_ctl6;
389         ctl6 &= ~AR_PadDelim;
390         ctl6 |= SM(numDelims, AR_PadDelim);
391         ads->ds_ctl6 = ctl6;
392 }
393
394 static void ar9002_hw_set11n_aggr_last(struct ath_hw *ah, void *ds)
395 {
396         struct ar5416_desc *ads = AR5416DESC(ds);
397
398         ads->ds_ctl1 |= AR_IsAggr;
399         ads->ds_ctl1 &= ~AR_MoreAggr;
400         ads->ds_ctl6 &= ~AR_PadDelim;
401 }
402
403 static void ar9002_hw_clr11n_aggr(struct ath_hw *ah, void *ds)
404 {
405         struct ar5416_desc *ads = AR5416DESC(ds);
406
407         ads->ds_ctl1 &= (~AR_IsAggr & ~AR_MoreAggr);
408 }
409
410 static void ar9002_hw_set11n_burstduration(struct ath_hw *ah, void *ds,
411                                            u32 burstDuration)
412 {
413         struct ar5416_desc *ads = AR5416DESC(ds);
414
415         ads->ds_ctl2 &= ~AR_BurstDur;
416         ads->ds_ctl2 |= SM(burstDuration, AR_BurstDur);
417 }
418
419 static void ar9002_hw_set11n_virtualmorefrag(struct ath_hw *ah, void *ds,
420                                             u32 vmf)
421 {
422         struct ar5416_desc *ads = AR5416DESC(ds);
423
424         if (vmf)
425                 ads->ds_ctl0 |= AR_VirtMoreFrag;
426         else
427                 ads->ds_ctl0 &= ~AR_VirtMoreFrag;
428 }
429
430 void ath9k_hw_setuprxdesc(struct ath_hw *ah, struct ath_desc *ds,
431                           u32 size, u32 flags)
432 {
433         struct ar5416_desc *ads = AR5416DESC(ds);
434         struct ath9k_hw_capabilities *pCap = &ah->caps;
435
436         ads->ds_ctl1 = size & AR_BufLen;
437         if (flags & ATH9K_RXDESC_INTREQ)
438                 ads->ds_ctl1 |= AR_RxIntrReq;
439
440         ads->ds_rxstatus8 &= ~AR_RxDone;
441         if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
442                 memset(&(ads->u), 0, sizeof(ads->u));
443 }
444 EXPORT_SYMBOL(ath9k_hw_setuprxdesc);
445
446 void ar9002_hw_attach_mac_ops(struct ath_hw *ah)
447 {
448         struct ath_hw_ops *ops = ath9k_hw_ops(ah);
449
450         ops->rx_enable = ar9002_hw_rx_enable;
451         ops->set_desc_link = ar9002_hw_set_desc_link;
452         ops->get_desc_link = ar9002_hw_get_desc_link;
453         ops->get_isr = ar9002_hw_get_isr;
454         ops->fill_txdesc = ar9002_hw_fill_txdesc;
455         ops->proc_txdesc = ar9002_hw_proc_txdesc;
456         ops->set11n_txdesc = ar9002_hw_set11n_txdesc;
457         ops->set11n_ratescenario = ar9002_hw_set11n_ratescenario;
458         ops->set11n_aggr_first = ar9002_hw_set11n_aggr_first;
459         ops->set11n_aggr_middle = ar9002_hw_set11n_aggr_middle;
460         ops->set11n_aggr_last = ar9002_hw_set11n_aggr_last;
461         ops->clr11n_aggr = ar9002_hw_clr11n_aggr;
462         ops->set11n_burstduration = ar9002_hw_set11n_burstduration;
463         ops->set11n_virtualmorefrag = ar9002_hw_set11n_virtualmorefrag;
464 }