xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/ath/ath9k/common-beacon.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2008-2011 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 "common.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define FUDGE 2
20*4882a593Smuzhiyun 
ath9k_get_next_tbtt(struct ath_hw * ah,u64 tsf,unsigned int interval)21*4882a593Smuzhiyun static u32 ath9k_get_next_tbtt(struct ath_hw *ah, u64 tsf,
22*4882a593Smuzhiyun 			       unsigned int interval)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	unsigned int offset, divisor;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	tsf += TU_TO_USEC(FUDGE + ah->config.sw_beacon_response_time);
27*4882a593Smuzhiyun 	divisor = TU_TO_USEC(interval);
28*4882a593Smuzhiyun 	div_u64_rem(tsf, divisor, &offset);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	return (u32) tsf + divisor - offset;
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun  * This sets up the beacon timers according to the timestamp of the last
35*4882a593Smuzhiyun  * received beacon and the current TSF, configures PCF and DTIM
36*4882a593Smuzhiyun  * handling, programs the sleep registers so the hardware will wakeup in
37*4882a593Smuzhiyun  * time to receive beacons, and configures the beacon miss handling so
38*4882a593Smuzhiyun  * we'll receive a BMISS interrupt when we stop seeing beacons from the AP
39*4882a593Smuzhiyun  * we've associated with.
40*4882a593Smuzhiyun  */
ath9k_cmn_beacon_config_sta(struct ath_hw * ah,struct ath_beacon_config * conf,struct ath9k_beacon_state * bs)41*4882a593Smuzhiyun int ath9k_cmn_beacon_config_sta(struct ath_hw *ah,
42*4882a593Smuzhiyun 				 struct ath_beacon_config *conf,
43*4882a593Smuzhiyun 				 struct ath9k_beacon_state *bs)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	struct ath_common *common = ath9k_hw_common(ah);
46*4882a593Smuzhiyun 	int dtim_intval;
47*4882a593Smuzhiyun 	u64 tsf;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	/* No need to configure beacon if we are not associated */
50*4882a593Smuzhiyun 	if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
51*4882a593Smuzhiyun 		ath_dbg(common, BEACON,
52*4882a593Smuzhiyun 			"STA is not yet associated..skipping beacon config\n");
53*4882a593Smuzhiyun 		return -EPERM;
54*4882a593Smuzhiyun 	}
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	memset(bs, 0, sizeof(*bs));
57*4882a593Smuzhiyun 	conf->intval = conf->beacon_interval;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	/*
60*4882a593Smuzhiyun 	 * Setup dtim parameters according to
61*4882a593Smuzhiyun 	 * last beacon we received (which may be none).
62*4882a593Smuzhiyun 	 */
63*4882a593Smuzhiyun 	dtim_intval = conf->intval * conf->dtim_period;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	/*
66*4882a593Smuzhiyun 	 * Pull nexttbtt forward to reflect the current
67*4882a593Smuzhiyun 	 * TSF and calculate dtim state for the result.
68*4882a593Smuzhiyun 	 */
69*4882a593Smuzhiyun 	tsf = ath9k_hw_gettsf64(ah);
70*4882a593Smuzhiyun 	conf->nexttbtt = ath9k_get_next_tbtt(ah, tsf, conf->intval);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	bs->bs_intval = TU_TO_USEC(conf->intval);
73*4882a593Smuzhiyun 	bs->bs_dtimperiod = conf->dtim_period * bs->bs_intval;
74*4882a593Smuzhiyun 	bs->bs_nexttbtt = conf->nexttbtt;
75*4882a593Smuzhiyun 	bs->bs_nextdtim = conf->nexttbtt;
76*4882a593Smuzhiyun 	if (conf->dtim_period > 1)
77*4882a593Smuzhiyun 		bs->bs_nextdtim = ath9k_get_next_tbtt(ah, tsf, dtim_intval);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/*
80*4882a593Smuzhiyun 	 * Calculate the number of consecutive beacons to miss* before taking
81*4882a593Smuzhiyun 	 * a BMISS interrupt. The configuration is specified in TU so we only
82*4882a593Smuzhiyun 	 * need calculate based	on the beacon interval.  Note that we clamp the
83*4882a593Smuzhiyun 	 * result to at most 15 beacons.
84*4882a593Smuzhiyun 	 */
85*4882a593Smuzhiyun 	bs->bs_bmissthreshold = DIV_ROUND_UP(conf->bmiss_timeout, conf->intval);
86*4882a593Smuzhiyun 	if (bs->bs_bmissthreshold > 15)
87*4882a593Smuzhiyun 		bs->bs_bmissthreshold = 15;
88*4882a593Smuzhiyun 	else if (bs->bs_bmissthreshold <= 0)
89*4882a593Smuzhiyun 		bs->bs_bmissthreshold = 1;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	/*
92*4882a593Smuzhiyun 	 * Calculate sleep duration. The configuration is given in ms.
93*4882a593Smuzhiyun 	 * We ensure a multiple of the beacon period is used. Also, if the sleep
94*4882a593Smuzhiyun 	 * duration is greater than the DTIM period then it makes senses
95*4882a593Smuzhiyun 	 * to make it a multiple of that.
96*4882a593Smuzhiyun 	 *
97*4882a593Smuzhiyun 	 * XXX fixed at 100ms
98*4882a593Smuzhiyun 	 */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	bs->bs_sleepduration = TU_TO_USEC(roundup(IEEE80211_MS_TO_TU(100),
101*4882a593Smuzhiyun 						  conf->intval));
102*4882a593Smuzhiyun 	if (bs->bs_sleepduration > bs->bs_dtimperiod)
103*4882a593Smuzhiyun 		bs->bs_sleepduration = bs->bs_dtimperiod;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* TSF out of range threshold fixed at 1 second */
106*4882a593Smuzhiyun 	bs->bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	ath_dbg(common, BEACON, "bmiss: %u sleep: %u\n",
109*4882a593Smuzhiyun 		bs->bs_bmissthreshold, bs->bs_sleepduration);
110*4882a593Smuzhiyun 	return 0;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun EXPORT_SYMBOL(ath9k_cmn_beacon_config_sta);
113*4882a593Smuzhiyun 
ath9k_cmn_beacon_config_adhoc(struct ath_hw * ah,struct ath_beacon_config * conf)114*4882a593Smuzhiyun void ath9k_cmn_beacon_config_adhoc(struct ath_hw *ah,
115*4882a593Smuzhiyun 				   struct ath_beacon_config *conf)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	struct ath_common *common = ath9k_hw_common(ah);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	conf->intval = TU_TO_USEC(conf->beacon_interval);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (conf->ibss_creator)
122*4882a593Smuzhiyun 		conf->nexttbtt = conf->intval;
123*4882a593Smuzhiyun 	else
124*4882a593Smuzhiyun 		conf->nexttbtt = ath9k_get_next_tbtt(ah, ath9k_hw_gettsf64(ah),
125*4882a593Smuzhiyun 					       conf->beacon_interval);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	if (conf->enable_beacon)
128*4882a593Smuzhiyun 		ah->imask |= ATH9K_INT_SWBA;
129*4882a593Smuzhiyun 	else
130*4882a593Smuzhiyun 		ah->imask &= ~ATH9K_INT_SWBA;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	ath_dbg(common, BEACON,
133*4882a593Smuzhiyun 		"IBSS (%s) nexttbtt: %u intval: %u conf_intval: %u\n",
134*4882a593Smuzhiyun 		(conf->enable_beacon) ? "Enable" : "Disable",
135*4882a593Smuzhiyun 		conf->nexttbtt, conf->intval, conf->beacon_interval);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun EXPORT_SYMBOL(ath9k_cmn_beacon_config_adhoc);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun  * For multi-bss ap support beacons are either staggered evenly over N slots or
141*4882a593Smuzhiyun  * burst together.  For the former arrange for the SWBA to be delivered for each
142*4882a593Smuzhiyun  * slot. Slots that are not occupied will generate nothing.
143*4882a593Smuzhiyun  */
ath9k_cmn_beacon_config_ap(struct ath_hw * ah,struct ath_beacon_config * conf,unsigned int bc_buf)144*4882a593Smuzhiyun void ath9k_cmn_beacon_config_ap(struct ath_hw *ah,
145*4882a593Smuzhiyun 				struct ath_beacon_config *conf,
146*4882a593Smuzhiyun 				unsigned int bc_buf)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	struct ath_common *common = ath9k_hw_common(ah);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/* NB: the beacon interval is kept internally in TU's */
151*4882a593Smuzhiyun 	conf->intval = TU_TO_USEC(conf->beacon_interval);
152*4882a593Smuzhiyun 	conf->intval /= bc_buf;
153*4882a593Smuzhiyun 	conf->nexttbtt = ath9k_get_next_tbtt(ah, ath9k_hw_gettsf64(ah),
154*4882a593Smuzhiyun 				       conf->beacon_interval);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	if (conf->enable_beacon)
157*4882a593Smuzhiyun 		ah->imask |= ATH9K_INT_SWBA;
158*4882a593Smuzhiyun 	else
159*4882a593Smuzhiyun 		ah->imask &= ~ATH9K_INT_SWBA;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	ath_dbg(common, BEACON,
162*4882a593Smuzhiyun 		"AP (%s) nexttbtt: %u intval: %u conf_intval: %u\n",
163*4882a593Smuzhiyun 		(conf->enable_beacon) ? "Enable" : "Disable",
164*4882a593Smuzhiyun 		conf->nexttbtt, conf->intval, conf->beacon_interval);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun EXPORT_SYMBOL(ath9k_cmn_beacon_config_ap);
167