xref: /titanic_51/usr/src/uts/common/io/ral/ral_rate.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 #include <sys/types.h>
39 #include <sys/byteorder.h>
40 #include <sys/conf.h>
41 #include <sys/cmn_err.h>
42 #include <sys/ddi.h>
43 #include <sys/sunddi.h>
44 #include <sys/stream.h>
45 #include <sys/strsun.h>
46 #include <sys/modctl.h>
47 #include <sys/devops.h>
48 #include <sys/dlpi.h>
49 #include <sys/gld.h>
50 #include <sys/varargs.h>
51 #include <sys/pci.h>
52 #include <sys/net80211.h>
53 
54 #include "ral_rate.h"
55 
56 #define	MAX(a, b) ((a) > (b) ? (a) : (b))
57 
58 #ifdef interpolate
59 #undef interpolate
60 #endif
61 #define	interpolate(parm, old, new)	\
62 	((parm##_old * (old) + \
63 	(parm##_denom - parm##_old) * (new)) /	\
64 	parm##_denom)
65 
66 static struct ral_rssadapt_expavgctl master_expavgctl;
67 
68 void
69 ral_rate_init(void)
70 {
71 	master_expavgctl.rc_decay_denom = 16;
72 	master_expavgctl.rc_decay_old = 15;
73 	master_expavgctl.rc_thresh_denom = 8;
74 	master_expavgctl.rc_thresh_old = 4;
75 	master_expavgctl.rc_avgrssi_denom = 8;
76 	master_expavgctl.rc_avgrssi_old = 4;
77 }
78 
79 /* ARGSUSED */
80 int
81 ral_rssadapt_choose(struct ral_rssadapt *ra, struct ieee80211_rateset *rs,
82     struct ieee80211_frame *wh, uint32_t len,
83     const char *dvname, int do_not_adapt)
84 {
85 	uint16_t (*thrs)[IEEE80211_RATE_SIZE];
86 	int flags = 0, i, rateidx = 0, thridx, top;
87 
88 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL)
89 		flags |= IEEE80211_RATE_BASIC;
90 
91 	for (i = 0, top = RAL_RSSADAPT_BKT0;
92 	    i < RAL_RSSADAPT_BKTS;
93 	    i++, top <<= RAL_RSSADAPT_BKTPOWER) {
94 		thridx = i;
95 		if (len <= top)
96 			break;
97 	}
98 
99 	thrs = &ra->ra_rate_thresh[thridx];
100 
101 	i = rs->ir_nrates;
102 	while (--i >= 0) {
103 		rateidx = i;
104 		if ((rs->ir_rates[i] & flags) != flags)
105 			continue;
106 		if (do_not_adapt)
107 			break;
108 		if ((*thrs)[i] < ra->ra_avg_rssi)
109 			break;
110 	}
111 
112 	return (rateidx);
113 }
114 
115 void
116 ral_rssadapt_updatestats(struct ral_rssadapt *ra)
117 {
118 	long interval;
119 
120 	ra->ra_pktrate =
121 	    (ra->ra_pktrate + 10 * (ra->ra_nfail + ra->ra_nok)) / 2;
122 	ra->ra_nfail = ra->ra_nok = 0;
123 
124 	/*
125 	 * a node is eligible for its rate to be raised every 1/10 to 10
126 	 * seconds, more eligible in proportion to recent packet rates.
127 	 */
128 	interval = MAX(100000, 10000000 / MAX(1, 10 * ra->ra_pktrate));
129 	ra->ra_raise_interval.tv_sec = interval / (1000 * 1000);
130 	ra->ra_raise_interval.tv_usec = interval % (1000 * 1000);
131 }
132 
133 /* ARGSUSED */
134 void
135 ral_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
136     struct ral_rssadapt *ra, int rssi)
137 {
138 	ra->ra_avg_rssi = interpolate(master_expavgctl.rc_avgrssi,
139 	    ra->ra_avg_rssi, (rssi << 8));
140 }
141 
142 /*
143  * Adapt the data rate to suit the conditions.  When a transmitted
144  * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
145  * raise the RSS threshold for transmitting packets of similar length at
146  * the same data rate.
147  */
148 /* ARGSUSED */
149 void
150 ral_rssadapt_lower_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
151     struct ral_rssadapt *ra, struct ral_rssdesc *id)
152 {
153 	struct ieee80211_rateset *rs = &ni->in_rates;
154 	uint16_t last_thr;
155 	uint32_t i, thridx, top;
156 
157 	ra->ra_nfail++;
158 
159 	if (id->id_rateidx >= rs->ir_nrates)
160 		return;
161 
162 	for (i = 0, top = RAL_RSSADAPT_BKT0;
163 	    i < RAL_RSSADAPT_BKTS;
164 	    i++, top <<= RAL_RSSADAPT_BKTPOWER) {
165 		thridx = i;
166 		if (id->id_len <= top)
167 			break;
168 	}
169 
170 	last_thr = ra->ra_rate_thresh[thridx][id->id_rateidx];
171 	ra->ra_rate_thresh[thridx][id->id_rateidx] =
172 	    interpolate(master_expavgctl.rc_thresh, last_thr,
173 	    (id->id_rssi << 8));
174 }
175 
176 /* ARGSUSED */
177 void
178 ral_rssadapt_raise_rate(struct ieee80211com *ic, struct ral_rssadapt *ra,
179     struct ral_rssdesc *id)
180 {
181 	uint16_t (*thrs)[IEEE80211_RATE_SIZE], newthr, oldthr;
182 	struct ieee80211_node *ni = id->id_node;
183 	struct ieee80211_rateset *rs = &ni->in_rates;
184 	int i, top;
185 
186 	ra->ra_nok++;
187 
188 	for (i = 0, top = RAL_RSSADAPT_BKT0;
189 	    i < RAL_RSSADAPT_BKTS;
190 	    i++, top <<= RAL_RSSADAPT_BKTPOWER) {
191 		thrs = &ra->ra_rate_thresh[i];
192 		if (id->id_len <= top)
193 			break;
194 	}
195 
196 	if (id->id_rateidx + 1 < rs->ir_nrates &&
197 	    (*thrs)[id->id_rateidx + 1] > (*thrs)[id->id_rateidx]) {
198 		oldthr = (*thrs)[id->id_rateidx + 1];
199 		if ((*thrs)[id->id_rateidx] == 0)
200 			newthr = ra->ra_avg_rssi;
201 		else
202 			newthr = (*thrs)[id->id_rateidx];
203 		(*thrs)[id->id_rateidx + 1] =
204 		    interpolate(master_expavgctl.rc_decay, oldthr, newthr);
205 	}
206 }
207