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