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