1*4609Szf162725 /*
2*4609Szf162725 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
3*4609Szf162725 * Use is subject to license terms.
4*4609Szf162725 */
5*4609Szf162725
6*4609Szf162725 /*
7*4609Szf162725 * Copyright (c) 2003, 2004 David Young. All rights reserved.
8*4609Szf162725 *
9*4609Szf162725 * Redistribution and use in source and binary forms, with or
10*4609Szf162725 * without modification, are permitted provided that the following
11*4609Szf162725 * conditions are met:
12*4609Szf162725 * 1. Redistributions of source code must retain the above copyright
13*4609Szf162725 * notice, this list of conditions and the following disclaimer.
14*4609Szf162725 * 2. Redistributions in binary form must reproduce the above
15*4609Szf162725 * copyright notice, this list of conditions and the following
16*4609Szf162725 * disclaimer in the documentation and/or other materials provided
17*4609Szf162725 * with the distribution.
18*4609Szf162725 * 3. The name of David Young may not be used to endorse or promote
19*4609Szf162725 * products derived from this software without specific prior
20*4609Szf162725 * written permission.
21*4609Szf162725 *
22*4609Szf162725 * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
23*4609Szf162725 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24*4609Szf162725 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25*4609Szf162725 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
26*4609Szf162725 * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27*4609Szf162725 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*4609Szf162725 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29*4609Szf162725 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30*4609Szf162725 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31*4609Szf162725 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*4609Szf162725 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33*4609Szf162725 * OF SUCH DAMAGE.
34*4609Szf162725 */
35*4609Szf162725
36*4609Szf162725 #pragma ident "%Z%%M% %I% %E% SMI"
37*4609Szf162725
38*4609Szf162725 #include <sys/types.h>
39*4609Szf162725 #include <sys/byteorder.h>
40*4609Szf162725 #include <sys/conf.h>
41*4609Szf162725 #include <sys/cmn_err.h>
42*4609Szf162725 #include <sys/ddi.h>
43*4609Szf162725 #include <sys/sunddi.h>
44*4609Szf162725 #include <sys/stream.h>
45*4609Szf162725 #include <sys/strsun.h>
46*4609Szf162725 #include <sys/modctl.h>
47*4609Szf162725 #include <sys/devops.h>
48*4609Szf162725 #include <sys/dlpi.h>
49*4609Szf162725 #include <sys/gld.h>
50*4609Szf162725 #include <sys/varargs.h>
51*4609Szf162725 #include <sys/pci.h>
52*4609Szf162725 #include <sys/net80211.h>
53*4609Szf162725
54*4609Szf162725 #include "ral_rate.h"
55*4609Szf162725
56*4609Szf162725 #define MAX(a, b) ((a) > (b) ? (a) : (b))
57*4609Szf162725
58*4609Szf162725 #ifdef interpolate
59*4609Szf162725 #undef interpolate
60*4609Szf162725 #endif
61*4609Szf162725 #define interpolate(parm, old, new) \
62*4609Szf162725 ((parm##_old * (old) + \
63*4609Szf162725 (parm##_denom - parm##_old) * (new)) / \
64*4609Szf162725 parm##_denom)
65*4609Szf162725
66*4609Szf162725 static struct ral_rssadapt_expavgctl master_expavgctl;
67*4609Szf162725
68*4609Szf162725 void
ral_rate_init(void)69*4609Szf162725 ral_rate_init(void)
70*4609Szf162725 {
71*4609Szf162725 master_expavgctl.rc_decay_denom = 16;
72*4609Szf162725 master_expavgctl.rc_decay_old = 15;
73*4609Szf162725 master_expavgctl.rc_thresh_denom = 8;
74*4609Szf162725 master_expavgctl.rc_thresh_old = 4;
75*4609Szf162725 master_expavgctl.rc_avgrssi_denom = 8;
76*4609Szf162725 master_expavgctl.rc_avgrssi_old = 4;
77*4609Szf162725 }
78*4609Szf162725
79*4609Szf162725 /* ARGSUSED */
80*4609Szf162725 int
ral_rssadapt_choose(struct ral_rssadapt * ra,struct ieee80211_rateset * rs,struct ieee80211_frame * wh,uint32_t len,const char * dvname,int do_not_adapt)81*4609Szf162725 ral_rssadapt_choose(struct ral_rssadapt *ra, struct ieee80211_rateset *rs,
82*4609Szf162725 struct ieee80211_frame *wh, uint32_t len,
83*4609Szf162725 const char *dvname, int do_not_adapt)
84*4609Szf162725 {
85*4609Szf162725 uint16_t (*thrs)[IEEE80211_RATE_SIZE];
86*4609Szf162725 int flags = 0, i, rateidx = 0, thridx, top;
87*4609Szf162725
88*4609Szf162725 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
89*4609Szf162725 flags |= IEEE80211_RATE_BASIC;
90*4609Szf162725
91*4609Szf162725 for (i = 0, top = RAL_RSSADAPT_BKT0;
92*4609Szf162725 i < RAL_RSSADAPT_BKTS;
93*4609Szf162725 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
94*4609Szf162725 thridx = i;
95*4609Szf162725 if (len <= top)
96*4609Szf162725 break;
97*4609Szf162725 }
98*4609Szf162725
99*4609Szf162725 thrs = &ra->ra_rate_thresh[thridx];
100*4609Szf162725
101*4609Szf162725 i = rs->ir_nrates;
102*4609Szf162725 while (--i >= 0) {
103*4609Szf162725 rateidx = i;
104*4609Szf162725 if ((rs->ir_rates[i] & flags) != flags)
105*4609Szf162725 continue;
106*4609Szf162725 if (do_not_adapt)
107*4609Szf162725 break;
108*4609Szf162725 if ((*thrs)[i] < ra->ra_avg_rssi)
109*4609Szf162725 break;
110*4609Szf162725 }
111*4609Szf162725
112*4609Szf162725 return (rateidx);
113*4609Szf162725 }
114*4609Szf162725
115*4609Szf162725 void
ral_rssadapt_updatestats(struct ral_rssadapt * ra)116*4609Szf162725 ral_rssadapt_updatestats(struct ral_rssadapt *ra)
117*4609Szf162725 {
118*4609Szf162725 long interval;
119*4609Szf162725
120*4609Szf162725 ra->ra_pktrate =
121*4609Szf162725 (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
122*4609Szf162725 ra->ra_nfail = ra->ra_nok = 0;
123*4609Szf162725
124*4609Szf162725 /*
125*4609Szf162725 * a node is eligible for its rate to be raised every 1/10 to 10
126*4609Szf162725 * seconds, more eligible in proportion to recent packet rates.
127*4609Szf162725 */
128*4609Szf162725 interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate));
129*4609Szf162725 ra->ra_raise_interval.tv_sec = interval / (1000 * 1000);
130*4609Szf162725 ra->ra_raise_interval.tv_usec = interval % (1000 * 1000);
131*4609Szf162725 }
132*4609Szf162725
133*4609Szf162725 /* ARGSUSED */
134*4609Szf162725 void
ral_rssadapt_input(struct ieee80211com * ic,struct ieee80211_node * ni,struct ral_rssadapt * ra,int rssi)135*4609Szf162725 ral_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
136*4609Szf162725 struct ral_rssadapt *ra, int rssi)
137*4609Szf162725 {
138*4609Szf162725 ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
139*4609Szf162725 ra->ra_avg_rssi, (rssi << 8));
140*4609Szf162725 }
141*4609Szf162725
142*4609Szf162725 /*
143*4609Szf162725 * Adapt the data rate to suit the conditions. When a transmitted
144*4609Szf162725 * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
145*4609Szf162725 * raise the RSS threshold for transmitting packets of similar length at
146*4609Szf162725 * the same data rate.
147*4609Szf162725 */
148*4609Szf162725 /* ARGSUSED */
149*4609Szf162725 void
ral_rssadapt_lower_rate(struct ieee80211com * ic,struct ieee80211_node * ni,struct ral_rssadapt * ra,struct ral_rssdesc * id)150*4609Szf162725 ral_rssadapt_lower_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
151*4609Szf162725 struct ral_rssadapt *ra, struct ral_rssdesc *id)
152*4609Szf162725 {
153*4609Szf162725 struct ieee80211_rateset *rs = &ni->in_rates;
154*4609Szf162725 uint16_t last_thr;
155*4609Szf162725 uint32_t i, thridx, top;
156*4609Szf162725
157*4609Szf162725 ra->ra_nfail++;
158*4609Szf162725
159*4609Szf162725 if (id->id_rateidx >= rs->ir_nrates)
160*4609Szf162725 return;
161*4609Szf162725
162*4609Szf162725 for (i = 0, top = RAL_RSSADAPT_BKT0;
163*4609Szf162725 i < RAL_RSSADAPT_BKTS;
164*4609Szf162725 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
165*4609Szf162725 thridx = i;
166*4609Szf162725 if (id->id_len <= top)
167*4609Szf162725 break;
168*4609Szf162725 }
169*4609Szf162725
170*4609Szf162725 last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx];
171*4609Szf162725 ra->ra_rate_thresh[thridx][id->id_rateidx] =
172*4609Szf162725 interpolate(master_expavgctl.rc_thresh, last_thr,
173*4609Szf162725 (id->id_rssi << 8));
174*4609Szf162725 }
175*4609Szf162725
176*4609Szf162725 /* ARGSUSED */
177*4609Szf162725 void
ral_rssadapt_raise_rate(struct ieee80211com * ic,struct ral_rssadapt * ra,struct ral_rssdesc * id)178*4609Szf162725 ral_rssadapt_raise_rate(struct ieee80211com *ic, struct ral_rssadapt *ra,
179*4609Szf162725 struct ral_rssdesc *id)
180*4609Szf162725 {
181*4609Szf162725 uint16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr;
182*4609Szf162725 struct ieee80211_node *ni = id->id_node;
183*4609Szf162725 struct ieee80211_rateset *rs = &ni->in_rates;
184*4609Szf162725 int i, top;
185*4609Szf162725
186*4609Szf162725 ra->ra_nok++;
187*4609Szf162725
188*4609Szf162725 for (i = 0, top = RAL_RSSADAPT_BKT0;
189*4609Szf162725 i < RAL_RSSADAPT_BKTS;
190*4609Szf162725 i++, top <<= RAL_RSSADAPT_BKTPOWER) {
191*4609Szf162725 thrs = &ra->ra_rate_thresh[i];
192*4609Szf162725 if (id->id_len <= top)
193*4609Szf162725 break;
194*4609Szf162725 }
195*4609Szf162725
196*4609Szf162725 if (id->id_rateidx + 1 < rs->ir_nrates &&
197*4609Szf162725 (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) {
198*4609Szf162725 oldthr = (*thrs)[id->id_rateidx + 1];
199*4609Szf162725 if ((*thrs)[id->id_rateidx] == 0)
200*4609Szf162725 newthr = ra->ra_avg_rssi;
201*4609Szf162725 else
202*4609Szf162725 newthr = (*thrs)[id->id_rateidx];
203*4609Szf162725 (*thrs)[id->id_rateidx + 1] =
204*4609Szf162725 interpolate(master_expavgctl.rc_decay, oldthr, newthr);
205*4609Szf162725 }
206*4609Szf162725 }
207