1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2009 Atheros Communications Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission to use, copy, modify, and/or distribute this software for any
5*4882a593Smuzhiyun * purpose with or without fee is hereby granted, provided that the above
6*4882a593Smuzhiyun * copyright notice and this permission notice appear in all copies.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*4882a593Smuzhiyun * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*4882a593Smuzhiyun * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*4882a593Smuzhiyun * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*4882a593Smuzhiyun * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*4882a593Smuzhiyun * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*4882a593Smuzhiyun * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/export.h>
18*4882a593Smuzhiyun #include <asm/unaligned.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "ath.h"
21*4882a593Smuzhiyun #include "reg.h"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define REG_READ (common->ops->read)
24*4882a593Smuzhiyun #define REG_WRITE(_ah, _reg, _val) (common->ops->write)(_ah, _val, _reg)
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /**
27*4882a593Smuzhiyun * ath_hw_set_bssid_mask - filter out bssids we listen
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * @common: the ath_common struct for the device.
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * BSSID masking is a method used by AR5212 and newer hardware to inform PCU
32*4882a593Smuzhiyun * which bits of the interface's MAC address should be looked at when trying
33*4882a593Smuzhiyun * to decide which packets to ACK. In station mode and AP mode with a single
34*4882a593Smuzhiyun * BSS every bit matters since we lock to only one BSS. In AP mode with
35*4882a593Smuzhiyun * multiple BSSes (virtual interfaces) not every bit matters because hw must
36*4882a593Smuzhiyun * accept frames for all BSSes and so we tweak some bits of our mac address
37*4882a593Smuzhiyun * in order to have multiple BSSes.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * NOTE: This is a simple filter and does *not* filter out all
40*4882a593Smuzhiyun * relevant frames. Some frames that are not for us might get ACKed from us
41*4882a593Smuzhiyun * by PCU because they just match the mask.
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * When handling multiple BSSes you can get the BSSID mask by computing the
44*4882a593Smuzhiyun * set of ~ ( MAC XOR BSSID ) for all bssids we handle.
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * When you do this you are essentially computing the common bits of all your
47*4882a593Smuzhiyun * BSSes. Later it is assumed the hardware will "and" (&) the BSSID mask with
48*4882a593Smuzhiyun * the MAC address to obtain the relevant bits and compare the result with
49*4882a593Smuzhiyun * (frame's BSSID & mask) to see if they match.
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * Simple example: on your card you have have two BSSes you have created with
52*4882a593Smuzhiyun * BSSID-01 and BSSID-02. Lets assume BSSID-01 will not use the MAC address.
53*4882a593Smuzhiyun * There is another BSSID-03 but you are not part of it. For simplicity's sake,
54*4882a593Smuzhiyun * assuming only 4 bits for a mac address and for BSSIDs you can then have:
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * \
57*4882a593Smuzhiyun * MAC: 0001 |
58*4882a593Smuzhiyun * BSSID-01: 0100 | --> Belongs to us
59*4882a593Smuzhiyun * BSSID-02: 1001 |
60*4882a593Smuzhiyun * /
61*4882a593Smuzhiyun * -------------------
62*4882a593Smuzhiyun * BSSID-03: 0110 | --> External
63*4882a593Smuzhiyun * -------------------
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * Our bssid_mask would then be:
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * On loop iteration for BSSID-01:
68*4882a593Smuzhiyun * ~(0001 ^ 0100) -> ~(0101)
69*4882a593Smuzhiyun * -> 1010
70*4882a593Smuzhiyun * bssid_mask = 1010
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * On loop iteration for BSSID-02:
73*4882a593Smuzhiyun * bssid_mask &= ~(0001 ^ 1001)
74*4882a593Smuzhiyun * bssid_mask = (1010) & ~(0001 ^ 1001)
75*4882a593Smuzhiyun * bssid_mask = (1010) & ~(1000)
76*4882a593Smuzhiyun * bssid_mask = (1010) & (0111)
77*4882a593Smuzhiyun * bssid_mask = 0010
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * A bssid_mask of 0010 means "only pay attention to the second least
80*4882a593Smuzhiyun * significant bit". This is because its the only bit common
81*4882a593Smuzhiyun * amongst the MAC and all BSSIDs we support. To findout what the real
82*4882a593Smuzhiyun * common bit is we can simply "&" the bssid_mask now with any BSSID we have
83*4882a593Smuzhiyun * or our MAC address (we assume the hardware uses the MAC address).
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * Now, suppose there's an incoming frame for BSSID-03:
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * IFRAME-01: 0110
88*4882a593Smuzhiyun *
89*4882a593Smuzhiyun * An easy eye-inspeciton of this already should tell you that this frame
90*4882a593Smuzhiyun * will not pass our check. This is because the bssid_mask tells the
91*4882a593Smuzhiyun * hardware to only look at the second least significant bit and the
92*4882a593Smuzhiyun * common bit amongst the MAC and BSSIDs is 0, this frame has the 2nd LSB
93*4882a593Smuzhiyun * as 1, which does not match 0.
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * So with IFRAME-01 we *assume* the hardware will do:
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * allow = (IFRAME-01 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
98*4882a593Smuzhiyun * --> allow = (0110 & 0010) == (0010 & 0001) ? 1 : 0;
99*4882a593Smuzhiyun * --> allow = (0010) == 0000 ? 1 : 0;
100*4882a593Smuzhiyun * --> allow = 0
101*4882a593Smuzhiyun *
102*4882a593Smuzhiyun * Lets now test a frame that should work:
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * IFRAME-02: 0001 (we should allow)
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * allow = (IFRAME-02 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
107*4882a593Smuzhiyun * --> allow = (0001 & 0010) == (0010 & 0001) ? 1 :0;
108*4882a593Smuzhiyun * --> allow = (0000) == (0000)
109*4882a593Smuzhiyun * --> allow = 1
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * Other examples:
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * IFRAME-03: 0100 --> allowed
114*4882a593Smuzhiyun * IFRAME-04: 1001 --> allowed
115*4882a593Smuzhiyun * IFRAME-05: 1101 --> allowed but its not for us!!!
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun */
ath_hw_setbssidmask(struct ath_common * common)118*4882a593Smuzhiyun void ath_hw_setbssidmask(struct ath_common *common)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun void *ah = common->ah;
121*4882a593Smuzhiyun u32 id1;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
124*4882a593Smuzhiyun id1 = REG_READ(ah, AR_STA_ID1) & ~AR_STA_ID1_SADH_MASK;
125*4882a593Smuzhiyun id1 |= get_unaligned_le16(common->macaddr + 4);
126*4882a593Smuzhiyun REG_WRITE(ah, AR_STA_ID1, id1);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun REG_WRITE(ah, AR_BSSMSKL, get_unaligned_le32(common->bssidmask));
129*4882a593Smuzhiyun REG_WRITE(ah, AR_BSSMSKU, get_unaligned_le16(common->bssidmask + 4));
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun EXPORT_SYMBOL(ath_hw_setbssidmask);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /**
135*4882a593Smuzhiyun * ath_hw_cycle_counters_update - common function to update cycle counters
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * @common: the ath_common struct for the device.
138*4882a593Smuzhiyun *
139*4882a593Smuzhiyun * This function is used to update all cycle counters in one place.
140*4882a593Smuzhiyun * It has to be called while holding common->cc_lock!
141*4882a593Smuzhiyun */
ath_hw_cycle_counters_update(struct ath_common * common)142*4882a593Smuzhiyun void ath_hw_cycle_counters_update(struct ath_common *common)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun u32 cycles, busy, rx, tx;
145*4882a593Smuzhiyun void *ah = common->ah;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* freeze */
148*4882a593Smuzhiyun REG_WRITE(ah, AR_MIBC, AR_MIBC_FMC);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* read */
151*4882a593Smuzhiyun cycles = REG_READ(ah, AR_CCCNT);
152*4882a593Smuzhiyun busy = REG_READ(ah, AR_RCCNT);
153*4882a593Smuzhiyun rx = REG_READ(ah, AR_RFCNT);
154*4882a593Smuzhiyun tx = REG_READ(ah, AR_TFCNT);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* clear */
157*4882a593Smuzhiyun REG_WRITE(ah, AR_CCCNT, 0);
158*4882a593Smuzhiyun REG_WRITE(ah, AR_RFCNT, 0);
159*4882a593Smuzhiyun REG_WRITE(ah, AR_RCCNT, 0);
160*4882a593Smuzhiyun REG_WRITE(ah, AR_TFCNT, 0);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /* unfreeze */
163*4882a593Smuzhiyun REG_WRITE(ah, AR_MIBC, 0);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* update all cycle counters here */
166*4882a593Smuzhiyun common->cc_ani.cycles += cycles;
167*4882a593Smuzhiyun common->cc_ani.rx_busy += busy;
168*4882a593Smuzhiyun common->cc_ani.rx_frame += rx;
169*4882a593Smuzhiyun common->cc_ani.tx_frame += tx;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun common->cc_survey.cycles += cycles;
172*4882a593Smuzhiyun common->cc_survey.rx_busy += busy;
173*4882a593Smuzhiyun common->cc_survey.rx_frame += rx;
174*4882a593Smuzhiyun common->cc_survey.tx_frame += tx;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun EXPORT_SYMBOL(ath_hw_cycle_counters_update);
177*4882a593Smuzhiyun
ath_hw_get_listen_time(struct ath_common * common)178*4882a593Smuzhiyun int32_t ath_hw_get_listen_time(struct ath_common *common)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun struct ath_cycle_counters *cc = &common->cc_ani;
181*4882a593Smuzhiyun int32_t listen_time;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun listen_time = (cc->cycles - cc->rx_frame - cc->tx_frame) /
184*4882a593Smuzhiyun (common->clockrate * 1000);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun memset(cc, 0, sizeof(*cc));
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun return listen_time;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun EXPORT_SYMBOL(ath_hw_get_listen_time);
191