5b28e918d8ced189fc7d02f9609ca59b966883fd
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5210 / ar5210_recv.c
1 /*
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2004 Atheros Communications, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * $Id: ar5210_recv.c,v 1.4 2008/11/10 04:08:02 sam Exp $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24 #include "ah_desc.h"
25
26 #include "ar5210/ar5210.h"
27 #include "ar5210/ar5210reg.h"
28 #include "ar5210/ar5210desc.h"
29
30 /*
31  * Get the RXDP.
32  */
33 uint32_t
34 ar5210GetRxDP(struct ath_hal *ah)
35 {
36         return OS_REG_READ(ah, AR_RXDP);
37 }
38
39 /*
40  * Set the RxDP.
41  */
42 void
43 ar5210SetRxDP(struct ath_hal *ah, uint32_t rxdp)
44 {
45         OS_REG_WRITE(ah, AR_RXDP, rxdp);
46 }
47
48
49 /*
50  * Set Receive Enable bits.
51  */
52 void
53 ar5210EnableReceive(struct ath_hal *ah)
54 {
55         OS_REG_WRITE(ah, AR_CR, AR_CR_RXE);
56 }
57
58 /*
59  * Stop Receive at the DMA engine
60  */
61 HAL_BOOL
62 ar5210StopDmaReceive(struct ath_hal *ah)
63 {
64         int i;
65
66         OS_REG_WRITE(ah, AR_CR, AR_CR_RXD);     /* Set receive disable bit */
67         for (i = 0; i < 1000; i++) {
68                 if ((OS_REG_READ(ah, AR_CR) & AR_CR_RXE) == 0)
69                         return AH_TRUE;
70                 OS_DELAY(10);
71         }
72 #ifdef AH_DEBUG
73         ath_hal_printf(ah, "ar5210: dma receive failed to stop in 10ms\n");
74         ath_hal_printf(ah, "AR_CR=0x%x\n", OS_REG_READ(ah, AR_CR));
75         ath_hal_printf(ah, "AR_DIAG_SW=0x%x\n", OS_REG_READ(ah, AR_DIAG_SW));
76 #endif
77         return AH_FALSE;
78 }
79
80 /*
81  * Start Transmit at the PCU engine (unpause receive)
82  */
83 void
84 ar5210StartPcuReceive(struct ath_hal *ah)
85 {
86         OS_REG_WRITE(ah, AR_DIAG_SW,
87                 OS_REG_READ(ah, AR_DIAG_SW) & ~(AR_DIAG_SW_DIS_RX));
88 }
89
90 /*
91  * Stop Transmit at the PCU engine (pause receive)
92  */
93 void
94 ar5210StopPcuReceive(struct ath_hal *ah)
95 {
96         OS_REG_WRITE(ah, AR_DIAG_SW,
97                 OS_REG_READ(ah, AR_DIAG_SW) | AR_DIAG_SW_DIS_RX);
98 }
99
100 /*
101  * Set multicast filter 0 (lower 32-bits)
102  *                         filter 1 (upper 32-bits)
103  */
104 void
105 ar5210SetMulticastFilter(struct ath_hal *ah, uint32_t filter0, uint32_t filter1)
106 {
107         OS_REG_WRITE(ah, AR_MCAST_FIL0, filter0);
108         OS_REG_WRITE(ah, AR_MCAST_FIL1, filter1);
109 }
110
111 /*
112  * Clear multicast filter by index
113  */
114 HAL_BOOL
115 ar5210ClrMulticastFilterIndex(struct ath_hal *ah, uint32_t ix)
116 {
117         uint32_t val;
118
119         if (ix >= 64)
120                 return AH_FALSE;
121         if (ix >= 32) {
122                 val = OS_REG_READ(ah, AR_MCAST_FIL1);
123                 OS_REG_WRITE(ah, AR_MCAST_FIL1, (val &~ (1<<(ix-32))));
124         } else {
125                 val = OS_REG_READ(ah, AR_MCAST_FIL0);
126                 OS_REG_WRITE(ah, AR_MCAST_FIL0, (val &~ (1<<ix)));
127         }
128         return AH_TRUE;
129 }
130
131 /*
132  * Set multicast filter by index
133  */
134 HAL_BOOL
135 ar5210SetMulticastFilterIndex(struct ath_hal *ah, uint32_t ix)
136 {
137         uint32_t val;
138
139         if (ix >= 64)
140                 return AH_FALSE;
141         if (ix >= 32) {
142                 val = OS_REG_READ(ah, AR_MCAST_FIL1);
143                 OS_REG_WRITE(ah, AR_MCAST_FIL1, (val | (1<<(ix-32))));
144         } else {
145                 val = OS_REG_READ(ah, AR_MCAST_FIL0);
146                 OS_REG_WRITE(ah, AR_MCAST_FIL0, (val | (1<<ix)));
147         }
148         return AH_TRUE;
149 }
150
151 /*
152  * Return the receive packet filter.
153  */
154 uint32_t
155 ar5210GetRxFilter(struct ath_hal *ah)
156 {
157         /* XXX can't be sure if promiscuous mode is set because of PHYRADAR */
158         return OS_REG_READ(ah, AR_RX_FILTER);
159 }
160
161 /*
162  * Turn off/on bits in the receive packet filter.
163  */
164 void
165 ar5210SetRxFilter(struct ath_hal *ah, uint32_t bits)
166 {
167         if (bits & HAL_RX_FILTER_PHYRADAR) {
168                 /* must enable promiscuous mode to get radar */
169                 bits = (bits &~ HAL_RX_FILTER_PHYRADAR) | AR_RX_FILTER_PROMISCUOUS;
170         }
171         OS_REG_WRITE(ah, AR_RX_FILTER, bits);
172 }
173
174 /*
175  * Initialize RX descriptor, by clearing the status and clearing
176  * the size.  This is not strictly HW dependent, but we want the
177  * control and status words to be opaque above the hal.
178  */
179 HAL_BOOL
180 ar5210SetupRxDesc(struct ath_hal *ah, struct ath_desc *ds,
181         uint32_t size, u_int flags)
182 {
183         struct ar5210_desc *ads = AR5210DESC(ds);
184
185         (void) flags;
186
187         ads->ds_ctl0 = 0;
188         ads->ds_ctl1 = size & AR_BufLen;
189         if (ads->ds_ctl1 != size) {
190                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: buffer size %u too large\n",
191                     __func__, size);
192                 return AH_FALSE;
193         }
194         if (flags & HAL_RXDESC_INTREQ)
195                 ads->ds_ctl1 |= AR_RxInterReq;
196         ads->ds_status0 = ads->ds_status1 = 0;
197
198         return AH_TRUE;
199 }
200
201 /*
202  * Process an RX descriptor, and return the status to the caller.
203  * Copy some hardware specific items into the software portion
204  * of the descriptor.
205  *
206  * NB: the caller is responsible for validating the memory contents
207  *     of the descriptor (e.g. flushing any cached copy).
208  */
209 HAL_STATUS
210 ar5210ProcRxDesc(struct ath_hal *ah, struct ath_desc *ds,
211         uint32_t pa, struct ath_desc *nds, uint64_t tsf,
212         struct ath_rx_status *rs)
213 {
214         struct ar5210_desc *ads = AR5210DESC(ds);
215         struct ar5210_desc *ands = AR5210DESC(nds);
216         uint32_t now, rstamp;
217
218         if ((ads->ds_status1 & AR_Done) == 0)
219                 return HAL_EINPROGRESS;
220         /*
221          * Given the use of a self-linked tail be very sure that the hw is
222          * done with this descriptor; the hw may have done this descriptor
223          * once and picked it up again...make sure the hw has moved on.
224          */
225         if ((ands->ds_status1 & AR_Done) == 0 && OS_REG_READ(ah, AR_RXDP) == pa)
226                 return HAL_EINPROGRESS;
227
228         rs->rs_datalen = ads->ds_status0 & AR_DataLen;
229         rstamp = MS(ads->ds_status1, AR_RcvTimestamp);
230         /*
231          * Convert timestamp.  The value in the
232          * descriptor is bits [10..22] of the TSF.
233          */
234         now = (OS_REG_READ(ah, AR_TSF_L32) >> 10) & 0xffff;
235         if ((now & 0x1fff) < rstamp)
236                 rstamp |= (now - 0x2000) & 0xffff;
237         else
238                 rstamp |= now;
239         /* NB: keep only 15 bits for consistency w/ other chips */
240         rs->rs_tstamp = rstamp & 0x7fff;
241         rs->rs_status = 0;
242         if ((ads->ds_status1 & AR_FrmRcvOK) == 0) {
243                 if (ads->ds_status1 & AR_CRCErr)
244                         rs->rs_status |= HAL_RXERR_CRC;
245                 else if (ads->ds_status1 & AR_DecryptCRCErr)
246                         rs->rs_status |= HAL_RXERR_DECRYPT;
247                 else if (ads->ds_status1 & AR_FIFOOverrun)
248                         rs->rs_status |= HAL_RXERR_FIFO;
249                 else {
250                         rs->rs_status |= HAL_RXERR_PHY;
251                         rs->rs_phyerr =
252                                 (ads->ds_status1 & AR_PHYErr) >> AR_PHYErr_S;
253                 }
254         }
255         /* XXX what about KeyCacheMiss? */
256         rs->rs_rssi = MS(ads->ds_status0, AR_RcvSigStrength);
257         if (ads->ds_status1 & AR_KeyIdxValid)
258                 rs->rs_keyix = MS(ads->ds_status1, AR_KeyIdx);
259         else
260                 rs->rs_keyix = HAL_RXKEYIX_INVALID;
261         /* NB: caller expected to do rate table mapping */
262         rs->rs_rate = MS(ads->ds_status0, AR_RcvRate);
263         rs->rs_antenna  = (ads->ds_status0 & AR_RcvAntenna) ? 1 : 0;
264         rs->rs_more = (ads->ds_status0 & AR_More) ? 1 : 0;
265
266         return HAL_OK;
267 }