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