xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/ssv6xxx/smac/ssv_rc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2015 South Silicon Valley Microelectronics Inc.
3  * Copyright (c) 2015 iComm Corporation
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <linux/version.h>
18 #include <ssv6200.h>
19 #include "dev.h"
20 #include "ssv_ht_rc.h"
21 #include "ssv_rc.h"
22 #include "ssv_rc_common.h"
23 static struct ssv_rc_rate ssv_11bgn_rate_table[] =
24 {
25         [0] = { .rc_flags = RC_FLAG_LEGACY,
26                 .phy_type = WLAN_RC_PHY_CCK,
27                 .rate_kbps = 1000,
28                 .dot11_rate_idx = 0,
29                 .ctrl_rate_idx = 0,
30                 .hw_rate_idx = 0,
31                 .arith_shift = 8,
32                 .target_pf = 26,
33            },
34         [1] = { .rc_flags = RC_FLAG_LEGACY,
35                 .phy_type = WLAN_RC_PHY_CCK,
36                 .rate_kbps = 2000,
37                 .dot11_rate_idx = 1,
38                 .ctrl_rate_idx = 1,
39                 .hw_rate_idx = 1,
40                 .arith_shift = 8,
41                 .target_pf = 26,
42            },
43         [2] = { .rc_flags = RC_FLAG_LEGACY,
44                 .phy_type = WLAN_RC_PHY_CCK,
45                 .rate_kbps = 5500,
46                 .dot11_rate_idx = 2,
47                 .ctrl_rate_idx = 1,
48                 .hw_rate_idx = 2,
49                 .arith_shift = 8,
50                 .target_pf = 26,
51            },
52         [3] = { .rc_flags = RC_FLAG_LEGACY,
53                 .phy_type = WLAN_RC_PHY_CCK,
54                 .rate_kbps = 11000,
55                 .dot11_rate_idx = 3,
56                 .ctrl_rate_idx = 1,
57                 .hw_rate_idx = 3,
58                 .arith_shift = 8,
59                 .target_pf = 26,
60            },
61         [4] = { .rc_flags = RC_FLAG_LEGACY | RC_FLAG_SHORT_PREAMBLE,
62                 .phy_type = WLAN_RC_PHY_CCK,
63                 .rate_kbps = 2000,
64                 .dot11_rate_idx = 1,
65                 .ctrl_rate_idx = 4,
66                 .hw_rate_idx = 4,
67                 .arith_shift = 8,
68                 .target_pf = 26,
69            },
70         [5] = { .rc_flags = RC_FLAG_LEGACY | RC_FLAG_SHORT_PREAMBLE,
71                 .phy_type = WLAN_RC_PHY_CCK,
72                 .rate_kbps = 5500,
73                 .dot11_rate_idx = 2,
74                 .ctrl_rate_idx = 4,
75                 .hw_rate_idx = 5,
76                 .arith_shift = 8,
77                 .target_pf = 26,
78            },
79         [6] = { .rc_flags = RC_FLAG_LEGACY | RC_FLAG_SHORT_PREAMBLE,
80                 .phy_type = WLAN_RC_PHY_CCK,
81                 .rate_kbps = 11000,
82                 .dot11_rate_idx = 3,
83                 .ctrl_rate_idx = 4,
84                 .hw_rate_idx = 6,
85                 .arith_shift = 8,
86                 .target_pf = 26,
87            },
88         [7] = { .rc_flags = RC_FLAG_LEGACY,
89                 .phy_type = WLAN_RC_PHY_OFDM,
90                 .rate_kbps = 6000,
91                 .dot11_rate_idx = 4,
92                 .ctrl_rate_idx = 7,
93                 .hw_rate_idx = 7,
94                 .arith_shift = 8,
95                 .target_pf = 26,
96            },
97         [8] = { .rc_flags = RC_FLAG_LEGACY,
98                 .phy_type = WLAN_RC_PHY_OFDM,
99                 .rate_kbps = 9000,
100                 .dot11_rate_idx = 5,
101                 .ctrl_rate_idx = 7,
102                 .hw_rate_idx = 8,
103                 .arith_shift = 8,
104                 .target_pf = 26,
105            },
106         [9] = { .rc_flags = RC_FLAG_LEGACY,
107                 .phy_type = WLAN_RC_PHY_OFDM,
108                 .rate_kbps = 12000,
109                 .dot11_rate_idx = 6,
110                 .ctrl_rate_idx = 9,
111                 .hw_rate_idx = 9,
112                 .arith_shift = 8,
113                 .target_pf = 26,
114            },
115         [10] = { .rc_flags = RC_FLAG_LEGACY,
116                  .phy_type = WLAN_RC_PHY_OFDM,
117                  .rate_kbps = 18000,
118                  .dot11_rate_idx = 7,
119                  .ctrl_rate_idx = 9,
120                  .hw_rate_idx = 10,
121                  .arith_shift = 8,
122                  .target_pf = 26,
123                },
124         [11] = { .rc_flags = RC_FLAG_LEGACY,
125                  .phy_type = WLAN_RC_PHY_OFDM,
126                  .rate_kbps = 24000,
127                  .dot11_rate_idx = 8,
128                  .ctrl_rate_idx = 11,
129                  .hw_rate_idx = 11,
130                  .arith_shift = 8,
131                  .target_pf = 26,
132                },
133         [12] = { .rc_flags = RC_FLAG_LEGACY,
134                  .phy_type = WLAN_RC_PHY_OFDM,
135                  .rate_kbps = 36000,
136                  .dot11_rate_idx = 9,
137                  .ctrl_rate_idx = 11,
138                  .hw_rate_idx = 12,
139                  .arith_shift = 8,
140                  .target_pf = 26,
141                },
142         [13] = { .rc_flags = RC_FLAG_LEGACY,
143                  .phy_type = WLAN_RC_PHY_OFDM,
144                  .rate_kbps = 48000,
145                  .dot11_rate_idx = 10,
146                  .ctrl_rate_idx = 11,
147                  .hw_rate_idx = 13,
148                  .arith_shift = 8,
149                  .target_pf = 26,
150                },
151         [14] = { .rc_flags = RC_FLAG_LEGACY,
152                  .phy_type = WLAN_RC_PHY_OFDM,
153                  .rate_kbps = 54000,
154                  .dot11_rate_idx = 11,
155                  .ctrl_rate_idx = 11,
156                  .hw_rate_idx = 14,
157                  .arith_shift = 8,
158                  .target_pf = 8
159                },
160         [15] = { .rc_flags = RC_FLAG_HT,
161                  .phy_type = WLAN_RC_PHY_HT_20_SS_LGI,
162                  .rate_kbps = 6500,
163                  .dot11_rate_idx = 0,
164                  .ctrl_rate_idx = 7,
165                  .hw_rate_idx = 15,
166                  .arith_shift = 8,
167                  .target_pf = 26,
168                },
169         [16] = { .rc_flags = RC_FLAG_HT,
170                  .phy_type = WLAN_RC_PHY_HT_20_SS_LGI,
171                  .rate_kbps = 13000,
172                  .dot11_rate_idx = 1,
173                  .ctrl_rate_idx = 9,
174                  .hw_rate_idx = 16,
175                  .arith_shift = 8,
176                  .target_pf = 26,
177                },
178         [17] = { .rc_flags = RC_FLAG_HT,
179                  .phy_type = WLAN_RC_PHY_HT_20_SS_LGI,
180                  .rate_kbps = 19500,
181                  .dot11_rate_idx = 2,
182                  .ctrl_rate_idx = 9,
183                  .hw_rate_idx = 17,
184                  .arith_shift = 8,
185                  .target_pf = 26,
186                  },
187         [18] = { .rc_flags = RC_FLAG_HT,
188                  .phy_type = WLAN_RC_PHY_HT_20_SS_LGI,
189                  .rate_kbps = 26000,
190                  .dot11_rate_idx = 3,
191                  .ctrl_rate_idx = 11,
192                  .hw_rate_idx = 18,
193                  .arith_shift = 8,
194                  .target_pf = 26,
195                },
196         [19] = { .rc_flags = RC_FLAG_HT,
197                  .phy_type = WLAN_RC_PHY_HT_20_SS_LGI,
198                  .rate_kbps = 39000,
199                  .dot11_rate_idx = 4,
200                  .ctrl_rate_idx = 11,
201                  .hw_rate_idx = 19,
202                  .arith_shift = 8,
203                  .target_pf = 26,
204                },
205         [20] = { .rc_flags = RC_FLAG_HT,
206                  .phy_type = WLAN_RC_PHY_HT_20_SS_LGI,
207                  .rate_kbps = 52000,
208                  .dot11_rate_idx = 5,
209                  .ctrl_rate_idx = 11,
210                  .hw_rate_idx = 20,
211                  .arith_shift = 8,
212                  .target_pf = 26,
213                },
214         [21] = { .rc_flags = RC_FLAG_HT,
215                  .phy_type = WLAN_RC_PHY_HT_20_SS_LGI,
216                  .rate_kbps = 58500,
217                  .dot11_rate_idx = 6,
218                  .ctrl_rate_idx = 11,
219                  .hw_rate_idx = 21,
220                  .arith_shift = 8,
221                  .target_pf = 26,
222                },
223         [22] = { .rc_flags = RC_FLAG_HT,
224                  .phy_type = WLAN_RC_PHY_HT_20_SS_LGI,
225                  .rate_kbps = 65000,
226                  .dot11_rate_idx = 7,
227                  .ctrl_rate_idx = 11,
228                  .hw_rate_idx = 22,
229                  .arith_shift = 8,
230                  .target_pf = 8
231                },
232         [23] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_SGI,
233                  .phy_type = WLAN_RC_PHY_HT_20_SS_SGI,
234                  .rate_kbps = 7200,
235                  .dot11_rate_idx = 0,
236                  .ctrl_rate_idx = 7,
237                  .hw_rate_idx = 23,
238                  .arith_shift = 8,
239                  .target_pf = 26,
240                },
241         [24] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_SGI,
242                  .phy_type = WLAN_RC_PHY_HT_20_SS_SGI,
243                  .rate_kbps = 14400,
244                  .dot11_rate_idx = 1,
245                  .ctrl_rate_idx = 9,
246                  .hw_rate_idx = 24,
247                  .arith_shift = 8,
248                  .target_pf = 26,
249                },
250         [25] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_SGI,
251                  .phy_type = WLAN_RC_PHY_HT_20_SS_SGI,
252                  .rate_kbps = 21700,
253                  .dot11_rate_idx = 2,
254                  .ctrl_rate_idx = 9,
255                  .hw_rate_idx = 25,
256                  .arith_shift = 8,
257                  .target_pf = 26,
258                },
259         [26] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_SGI,
260                  .phy_type = WLAN_RC_PHY_HT_20_SS_SGI,
261                  .rate_kbps = 28900,
262                  .dot11_rate_idx = 3,
263                  .ctrl_rate_idx = 11,
264                  .hw_rate_idx = 26,
265                  .arith_shift = 8,
266                  .target_pf = 26,
267                },
268         [27] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_SGI,
269                  .phy_type = WLAN_RC_PHY_HT_20_SS_SGI,
270                  .rate_kbps = 43300,
271                  .dot11_rate_idx = 4,
272                  .ctrl_rate_idx = 11,
273                  .hw_rate_idx = 27,
274                  .arith_shift = 8,
275                  .target_pf = 26,
276                },
277         [28] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_SGI,
278                  .phy_type = WLAN_RC_PHY_HT_20_SS_SGI,
279                  .rate_kbps = 57800,
280                  .dot11_rate_idx = 5,
281                  .ctrl_rate_idx = 11,
282                  .hw_rate_idx = 28,
283                  .arith_shift = 8,
284                  .target_pf = 26,
285                },
286         [29] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_SGI,
287                  .phy_type = WLAN_RC_PHY_HT_20_SS_SGI,
288                  .rate_kbps = 65000,
289                  .dot11_rate_idx = 6,
290                  .ctrl_rate_idx = 11,
291                  .hw_rate_idx = 29,
292                  .arith_shift = 8,
293                  .target_pf = 26,
294                },
295         [30] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_SGI,
296                  .phy_type = WLAN_RC_PHY_HT_20_SS_SGI,
297                  .rate_kbps = 72200,
298                  .dot11_rate_idx = 7,
299                  .ctrl_rate_idx = 11,
300                  .hw_rate_idx = 30,
301                  .arith_shift = 8,
302                  .target_pf = 8
303                },
304         [31] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_GF,
305                  .phy_type = WLAN_RC_PHY_HT_20_SS_GF,
306                  .rate_kbps = 6500,
307                  .dot11_rate_idx = 0,
308                  .ctrl_rate_idx = 7,
309                  .hw_rate_idx = 31,
310                  .arith_shift = 8,
311                  .target_pf = 26,
312                },
313         [32] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_GF,
314                  .phy_type = WLAN_RC_PHY_HT_20_SS_GF,
315                  .rate_kbps = 13000,
316                  .dot11_rate_idx = 1,
317                  .ctrl_rate_idx = 9,
318                  .hw_rate_idx = 32,
319                  .arith_shift = 8,
320                  .target_pf = 26,
321                },
322         [33] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_GF,
323                  .phy_type = WLAN_RC_PHY_HT_20_SS_GF,
324                  .rate_kbps = 19500,
325                  .dot11_rate_idx = 2,
326                  .ctrl_rate_idx = 9,
327                  .hw_rate_idx = 33,
328                  .arith_shift = 8,
329                  .target_pf = 26,
330                },
331         [34] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_GF,
332                  .phy_type = WLAN_RC_PHY_HT_20_SS_GF,
333                  .rate_kbps = 26000,
334                  .dot11_rate_idx = 3,
335                  .ctrl_rate_idx = 11,
336                  .hw_rate_idx = 34,
337                  .arith_shift = 8,
338                  .target_pf = 26,
339                },
340         [35] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_GF,
341                  .phy_type = WLAN_RC_PHY_HT_20_SS_GF,
342                  .rate_kbps = 39000,
343                  .dot11_rate_idx = 4,
344                  .ctrl_rate_idx = 11,
345                  .hw_rate_idx = 35,
346                  .arith_shift = 8,
347                  .target_pf = 26,
348                },
349         [36] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_GF,
350                  .phy_type = WLAN_RC_PHY_HT_20_SS_GF,
351                  .rate_kbps = 52000,
352                  .dot11_rate_idx = 5,
353                  .ctrl_rate_idx = 11,
354                  .hw_rate_idx = 36,
355                  .arith_shift = 8,
356                  .target_pf = 26,
357                },
358         [37] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_GF,
359                  .phy_type = WLAN_RC_PHY_HT_20_SS_GF,
360                  .rate_kbps = 58500,
361                  .dot11_rate_idx = 6,
362                  .ctrl_rate_idx = 11,
363                  .hw_rate_idx = 37,
364                  .arith_shift = 8,
365                  .target_pf = 26,
366                },
367         [38] = { .rc_flags = RC_FLAG_HT | RC_FLAG_HT_GF,
368                  .phy_type = WLAN_RC_PHY_HT_20_SS_GF,
369                  .rate_kbps = 65000,
370                  .dot11_rate_idx = 7,
371                  .ctrl_rate_idx = 11,
372                  .hw_rate_idx = 38,
373                  .arith_shift = 8,
374                  .target_pf = 8
375                 },
376 };
377 const u16 ssv6xxx_rc_rate_set[RC_TYPE_MAX][13] =
378 {
379     [RC_TYPE_B_ONLY] = { 4, 0, 1, 2, 3},
380     [RC_TYPE_LEGACY_GB] = { 12, 0, 1, 2, 7, 8, 3, 9, 10, 11, 12, 13, 14 },
381 #if 0
382     [RC_TYPE_SGI_20] = { 12, 0, 1, 2, 3, 23, 24, 25, 26, 27, 28, 29, 30 },
383     [RC_TYPE_LGI_20] = { 12, 0, 1, 2, 3, 15, 16, 17, 18, 19, 20, 21, 22 },
384 #else
385     [RC_TYPE_SGI_20] = { 8, 23, 24, 25, 26, 27, 28, 29, 30 },
386     [RC_TYPE_LGI_20] = { 8, 15, 16, 17, 18, 19, 20, 21, 22 },
387 #endif
388     [RC_TYPE_HT_SGI_20] = { 8, 23, 24, 25, 26, 27, 28, 29, 30 },
389     [RC_TYPE_HT_LGI_20] = { 8, 15, 16, 17, 18, 19, 20, 21, 22 },
390     [RC_TYPE_HT_GF] = { 8, 31, 32, 33, 34, 35, 36, 37, 38 },
391 };
ssv6xxx_rate_supported(struct ssv_sta_rc_info * rc_sta,u32 index)392 static u32 ssv6xxx_rate_supported(struct ssv_sta_rc_info *rc_sta, u32 index)
393 {
394     return (rc_sta->rc_supp_rates & BIT(index));
395 }
396 #if 1
ssv6xxx_rate_lowest_index(struct ssv_sta_rc_info * rc_sta)397 static u8 ssv6xxx_rate_lowest_index(struct ssv_sta_rc_info *rc_sta)
398 {
399     int i;
400     for (i = 0; i < rc_sta->rc_num_rate; i++)
401         if (ssv6xxx_rate_supported(rc_sta, i))
402             return i;
403     return 0;
404 }
405 #endif
406 #ifdef DISABLE_RATE_CONTROL_SAMPLE
ssv6xxx_rate_highest_index(struct ssv_sta_rc_info * rc_sta)407 static u8 ssv6xxx_rate_highest_index(struct ssv_sta_rc_info *rc_sta)
408 {
409     int i;
410     for (i=rc_sta->rc_num_rate-1; i >= 0; i--)
411         if (ssv6xxx_rate_supported(rc_sta, i))
412             return i;
413     return 0;
414 }
415 #endif
rate_control_pid_adjust_rate(struct ssv_sta_rc_info * rc_sta,struct rc_pid_sta_info * spinfo,int adj,struct rc_pid_rateinfo * rinfo)416 static void rate_control_pid_adjust_rate(struct ssv_sta_rc_info *rc_sta,
417             struct rc_pid_sta_info *spinfo, int adj,struct rc_pid_rateinfo *rinfo)
418 {
419     int cur_sorted, new_sorted, probe, tmp, n_bitrates;
420     int cur = spinfo->txrate_idx;
421     n_bitrates = rc_sta->rc_num_rate;
422     cur_sorted = rinfo[cur].index;
423     new_sorted = cur_sorted + adj;
424     if (new_sorted < 0)
425         new_sorted = rinfo[0].index;
426     else if (new_sorted >= n_bitrates)
427         new_sorted = rinfo[n_bitrates - 1].index;
428     tmp = new_sorted;
429  if (adj < 0) {
430   for (probe = cur_sorted; probe >= new_sorted; probe--)
431    if (rinfo[probe].diff <= rinfo[cur_sorted].diff &&
432        ssv6xxx_rate_supported(rc_sta, rinfo[probe].index))
433     tmp = probe;
434  } else {
435   for (probe = new_sorted + 1; probe < n_bitrates; probe++)
436    if (rinfo[probe].diff <= rinfo[new_sorted].diff &&
437        ssv6xxx_rate_supported(rc_sta, rinfo[probe].index))
438     tmp = probe;
439  }
440     BUG_ON(tmp<0 || tmp>=n_bitrates);
441  do {
442   if (ssv6xxx_rate_supported(rc_sta, rinfo[tmp].index)) {
443    spinfo->tmp_rate_idx = rinfo[tmp].index;
444    break;
445   }
446   if (adj < 0)
447    tmp--;
448   else
449    tmp++;
450  } while (tmp < n_bitrates && tmp >= 0);
451     spinfo->oldrate = spinfo->txrate_idx;
452  if (spinfo->tmp_rate_idx != spinfo->txrate_idx) {
453   spinfo->monitoring = 1;
454 #ifdef RATE_CONTROL_PARAMETER_DEBUG
455         printk("Trigger monitor tmp_rate_idx=[%d]\n",spinfo->tmp_rate_idx);
456 #endif
457   spinfo->probe_cnt = MAXPROBES;
458  }
459 }
rate_control_pid_normalize(struct rc_pid_info * pinfo,int l)460 static void rate_control_pid_normalize(struct rc_pid_info *pinfo, int l)
461 {
462         int i, norm_offset = RC_PID_NORM_OFFSET;
463         struct rc_pid_rateinfo *r = pinfo->rinfo;
464         if (r[0].diff > norm_offset)
465                 r[0].diff -= norm_offset;
466         else if (r[0].diff < -norm_offset)
467                 r[0].diff += norm_offset;
468         for (i = 0; i < l - 1; i++)
469                 if (r[i + 1].diff > r[i].diff + norm_offset)
470                         r[i + 1].diff -= norm_offset;
471                 else if (r[i + 1].diff <= r[i].diff)
472                         r[i + 1].diff += norm_offset;
473 }
474 #ifdef RATE_CONTROL_DEBUG
475     unsigned int txrate_dlr=0;
476 #endif
rate_control_pid_sample(struct ssv_rate_ctrl * ssv_rc,struct rc_pid_info * pinfo,struct ssv_sta_rc_info * rc_sta,struct rc_pid_sta_info * spinfo)477 static void rate_control_pid_sample(struct ssv_rate_ctrl* ssv_rc,struct rc_pid_info *pinfo,
478         struct ssv_sta_rc_info *rc_sta,
479         struct rc_pid_sta_info *spinfo)
480 {
481  struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
482  u8 pf;
483  s32 err_avg;
484  s32 err_prop;
485  s32 err_int;
486  s32 err_der;
487  int adj, i, j, tmp;
488     struct ssv_rc_rate *rc_table;
489  unsigned int dlr;
490  unsigned int perfect_time = 0;
491  unsigned int this_thp, ewma_thp;
492  struct rc_pid_rateinfo *rate;
493  if (!spinfo->monitoring)
494  {
495 #if 0
496   period = msecs_to_jiffies(pinfo->sampling_period);
497   if (jiffies - spinfo->last_sample > 2 * period)
498    spinfo->sharp_cnt = RC_PID_SHARPENING_DURATION;
499 #endif
500         if (spinfo->tx_num_xmit == 0)
501             return;
502   spinfo->last_sample = jiffies;
503   pf = spinfo->tx_num_failed * 100 / spinfo->tx_num_xmit;
504   if (pinfo->rinfo[spinfo->txrate_idx].this_attempt > 0)
505   {
506    rate = &pinfo->rinfo[spinfo->txrate_idx];
507             rc_table = &ssv_rc->rc_table[spinfo->txrate_idx];
508    dlr = 100 - rate->this_fail * 100 / rate->this_attempt;
509    perfect_time = rate->perfect_tx_time;
510    if (!perfect_time)
511     perfect_time = 1000000;
512    this_thp = dlr * (1000000 / perfect_time);
513    ewma_thp = rate->throughput;
514    if (ewma_thp == 0)
515     rate->throughput = this_thp;
516    else
517     rate->throughput = (ewma_thp + this_thp) >> 1;
518    rate->attempt += rate->this_attempt;
519    rate->success += rate->this_success;
520    rate->fail += rate->this_fail;
521    spinfo->tx_num_xmit = 0;
522    spinfo->tx_num_failed = 0;
523    rate->this_fail = 0;
524    rate->this_success = 0;
525    rate->this_attempt = 0;
526             if (pinfo->oldrate<0 || pinfo->oldrate>=rc_sta->rc_num_rate)
527             {
528                 WARN_ON(1);
529             }
530             if (spinfo->txrate_idx<0 || spinfo->txrate_idx>=rc_sta->rc_num_rate)
531             {
532                 WARN_ON(1);
533             }
534    if (pinfo->oldrate != spinfo->txrate_idx) {
535     i = rinfo[pinfo->oldrate].index;
536     j = rinfo[spinfo->txrate_idx].index;
537                 tmp = (pf - spinfo->last_pf);
538     tmp = RC_PID_DO_ARITH_RIGHT_SHIFT(tmp, rc_table->arith_shift);
539     rinfo[j].diff = rinfo[i].diff + tmp;
540     pinfo->oldrate = spinfo->txrate_idx;
541    }
542    rate_control_pid_normalize(pinfo, rc_sta->rc_num_rate);
543    err_prop = (rc_table->target_pf - pf) << rc_table->arith_shift;
544    err_avg = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
545    spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
546    err_int = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
547    err_der = pf - spinfo->last_pf;
548    spinfo->last_pf = pf;
549    spinfo->last_dlr = dlr;
550    spinfo->oldrate = spinfo->txrate_idx;
551 #if 0
552    if (spinfo->sharp_cnt)
553     spinfo->sharp_cnt--;
554 #endif
555    adj = (err_prop * RC_PID_COEFF_P + err_int * RC_PID_COEFF_I + err_der * RC_PID_COEFF_D);
556    adj = RC_PID_DO_ARITH_RIGHT_SHIFT(adj, rc_table->arith_shift<<1);
557    if (adj) {
558 #ifdef RATE_CONTROL_PARAMETER_DEBUG
559                 if((spinfo->txrate_idx!=11) || ((spinfo->txrate_idx==11)&&(adj < 0)))
560                 printk("[RC]Probe adjust[%d] dlr[%d%%] this_thp[%d] ewma_thp[%d] index[%d]\n",adj ,dlr,this_thp,ewma_thp,spinfo->txrate_idx);
561 #endif
562     rate_control_pid_adjust_rate(rc_sta, spinfo, adj, rinfo);
563    }
564   }
565  }
566  else
567     {
568         if((spinfo->feedback_probes >= MAXPROBES) || (spinfo->feedback_probes && spinfo->probe_cnt))
569         {
570       rate = &pinfo->rinfo[spinfo->txrate_idx];
571 #if 0
572       period = msecs_to_jiffies(pinfo->sampling_period);
573       if (jiffies - spinfo->last_sample > 2 * period)
574        spinfo->sharp_cnt = RC_PID_SHARPENING_DURATION;
575 #endif
576       spinfo->last_sample = jiffies;
577       if (rate->this_attempt > 0) {
578        dlr = 100 - rate->this_fail * 100 / rate->this_attempt;
579 #ifdef RATE_CONTROL_DEBUG
580 #ifdef PROBE
581                 txrate_dlr=dlr;
582 #endif
583 #endif
584        spinfo->last_dlr = dlr;
585        perfect_time = rate->perfect_tx_time;
586        if (!perfect_time)
587         perfect_time = 1000000;
588        this_thp = dlr * (1000000 / perfect_time);
589        ewma_thp = rate->throughput;
590        if (ewma_thp == 0)
591         rate->throughput = this_thp;
592        else
593         rate->throughput = (ewma_thp + this_thp) >> 1;
594        rate->attempt += rate->this_attempt;
595        rate->success += rate->this_success;
596        rinfo[spinfo->txrate_idx].fail += rate->this_fail;
597        rate->this_fail = 0;
598        rate->this_success = 0;
599        rate->this_attempt = 0;
600       }
601             else
602             {
603 #ifdef RATE_CONTROL_DEBUG
604 #ifdef PROBE
605                 txrate_dlr=0;
606 #endif
607 #endif
608             }
609             rate = &pinfo->rinfo[spinfo->tmp_rate_idx];
610             if (rate->this_attempt > 0) {
611                 dlr = 100 - ((rate->this_fail * 100) / rate->this_attempt);
612                 {
613                     perfect_time = rate->perfect_tx_time;
614                     if (!perfect_time)
615                         perfect_time = 1000000;
616                     if(dlr)
617                         this_thp = dlr * (1000000 / perfect_time);
618                     else
619                         this_thp = 0;
620                     ewma_thp = rate->throughput;
621                     if (ewma_thp == 0)
622                         rate->throughput = this_thp;
623                     else
624                         rate->throughput = (ewma_thp + this_thp) >> 1;
625                     if (rate->throughput > pinfo->rinfo[spinfo->txrate_idx].throughput)
626                     {
627 #ifdef RATE_CONTROL_PARAMETER_DEBUG
628                         printk("[RC]UPDATE probe rate idx[%d] [%d][%d%%] Old idx[%d] [%d][%d%%] feedback[%d] \n",spinfo->tmp_rate_idx,rate->throughput,dlr,spinfo->txrate_idx,pinfo->rinfo[spinfo->txrate_idx].throughput,txrate_dlr,spinfo->feedback_probes);
629 #endif
630                         spinfo->txrate_idx = spinfo->tmp_rate_idx;
631                     }
632                     else
633                     {
634 #ifdef RATE_CONTROL_PARAMETER_DEBUG
635                         printk("[RC]Fail probe rate idx[%d] [%d][%d%%] Old idx[%d] [%d][%d%%] feedback[%d] \n",spinfo->tmp_rate_idx,rate->throughput,dlr,spinfo->txrate_idx,pinfo->rinfo[spinfo->txrate_idx].throughput,txrate_dlr,spinfo->feedback_probes);
636 #endif
637                         ;
638                     }
639                     rate->attempt += rate->this_attempt;
640                     rate->success += rate->this_success;
641                     rate->fail += rate->this_fail;
642                     rate->this_fail = 0;
643                     rate->this_success = 0;
644                     rate->this_attempt = 0;
645                     spinfo->oldrate = spinfo->txrate_idx;
646                 }
647             }
648 #ifdef RATE_CONTROL_DEBUG
649             else
650                 printk("SHIT-2!!!!\n");
651 #endif
652             spinfo->feedback_probes = 0;
653             spinfo->tx_num_xmit = 0;
654             spinfo->tx_num_failed = 0;
655             spinfo->monitoring = 0;
656 #ifdef RATE_CONTROL_PARAMETER_DEBUG
657             printk("Disable monitor\n");
658 #endif
659             spinfo->probe_report_flag = 0;
660             spinfo->probe_wating_times = 0;
661         }
662         else
663         {
664             spinfo->probe_wating_times ++;
665 #ifdef RATE_CONTROL_DEBUG
666             if(spinfo->probe_wating_times > 3)
667             {
668                 printk("[RC]@@@@@ PROBE LOSE @@@@@ feedback=[%d] need=[%d] probe_cnt=[%d] wating times[%d]\n",
669                     spinfo->feedback_probes,MAXPROBES,spinfo->probe_cnt,spinfo->probe_wating_times);
670                 spinfo->feedback_probes = 0;
671                 spinfo->tx_num_xmit = 0;
672                 spinfo->tx_num_failed = 0;
673                 spinfo->monitoring = 0;
674                 spinfo->probe_report_flag = 0;
675                 spinfo->probe_wating_times = 0;
676             }
677 #else
678             if(spinfo->probe_wating_times > 3)
679             {
680                 spinfo->feedback_probes = 0;
681                 spinfo->tx_num_xmit = 0;
682                 spinfo->tx_num_failed = 0;
683                 spinfo->monitoring = 0;
684                 spinfo->probe_report_flag = 0;
685                 spinfo->probe_wating_times = 0;
686             }
687 #endif
688         }
689     }
690 }
691 #ifdef RATE_CONTROL_PERCENTAGE_TRACE
692 int percentage = 0;
693 int percentageCounter = 0;
694 #endif
ssv6xxx_legacy_report_handler(struct ssv_softc * sc,struct sk_buff * skb,struct ssv_sta_rc_info * rc_sta)695 void ssv6xxx_legacy_report_handler(struct ssv_softc *sc,struct sk_buff *skb,struct ssv_sta_rc_info *rc_sta)
696 {
697     struct ssv_rate_ctrl *ssv_rc=sc->rc;
698     struct cfg_host_event *host_event;
699     struct firmware_rate_control_report_data *report_data;
700     struct rc_pid_info *pinfo;
701     struct rc_pid_sta_info *spinfo;
702     struct rc_pid_rateinfo * pidrate;
703     struct rc_pid_rateinfo *rate;
704     s32 report_data_index = 0;
705     unsigned long period;
706     host_event = (struct cfg_host_event *)skb->data;
707     report_data = (struct firmware_rate_control_report_data *)&host_event->dat[0];
708     if ( (report_data->wsid != (-1))
709         && sc->sta_info[report_data->wsid].sta == NULL)
710     {
711         dev_warn(sc->dev, "RC report has no valid STA.(%d)\n", report_data->wsid);
712         return;
713     }
714     pinfo = &rc_sta->pinfo;
715     spinfo = &rc_sta->spinfo;
716     pidrate = rc_sta->pinfo.rinfo;
717     if(host_event->h_event == SOC_EVT_RC_AMPDU_REPORT)
718     {
719 #if 1
720         period = msecs_to_jiffies(HT_RC_UPDATE_INTERVAL);
721         if (time_after(jiffies, spinfo->last_sample + period))
722         {
723             if(rc_sta->rc_num_rate == 12)
724                 spinfo->txrate_idx = rc_sta->ht.max_tp_rate + 4;
725             else
726                 spinfo->txrate_idx = rc_sta->ht.max_tp_rate;
727 #ifdef RATE_CONTROL_DEBUG
728             printk("MPDU rate update time txrate_idx[%d]!!\n",spinfo->txrate_idx);
729 #endif
730             spinfo->last_sample = jiffies;
731         }
732 #endif
733         return;
734     }
735     else if(host_event->h_event == SOC_EVT_RC_MPDU_REPORT)
736     {
737 #if 0
738         printk("SC report !MPDU! wsid[%d]rate[%d]M[%d]S[%d]R[%d]\n",report_data->wsid,report_data->rates[0].data_rate,
739                report_data->ampdu_len,report_data->ampdu_ack_len,report_data->rates[0].count);
740 #endif
741         ;
742     }
743     else
744     {
745         printk("RC work get garbage!!\n");
746         return;
747     }
748     if(report_data->rates[0].data_rate < 7)
749     {
750         if(report_data->rates[0].data_rate>3)
751         {
752             report_data->rates[0].data_rate -= 3;
753         }
754     }
755     if(ssv_rc->rc_table[rc_sta->pinfo.rinfo[spinfo->txrate_idx].rc_index].hw_rate_idx == report_data->rates[0].data_rate)
756     {
757         report_data_index = rc_sta->pinfo.rinfo[spinfo->txrate_idx].index;
758     }
759     else if(ssv_rc->rc_table[rc_sta->pinfo.rinfo[spinfo->tmp_rate_idx].rc_index].hw_rate_idx == report_data->rates[0].data_rate)
760     {
761         report_data_index = rc_sta->pinfo.rinfo[spinfo->tmp_rate_idx].index;
762     }
763     if((report_data_index != spinfo->tmp_rate_idx) && (report_data_index != spinfo->txrate_idx))
764     {
765 #ifdef RATE_CONTROL_DEBUG
766         printk("Rate control report mismatch report_rate_idx[%d] tmp_rate_idx[%d]rate[%d] txrate_idx[%d]rate[%d]!!\n",
767             report_data->rates[0].data_rate,spinfo->tmp_rate_idx,
768             ssv_rc->rc_table[rc_sta->pinfo.rinfo[spinfo->tmp_rate_idx].rc_index].hw_rate_idx,
769             spinfo->txrate_idx,
770             ssv_rc->rc_table[rc_sta->pinfo.rinfo[spinfo->txrate_idx].rc_index].hw_rate_idx);
771 #endif
772         return;
773     }
774     if(report_data_index == spinfo->txrate_idx)
775     {
776         spinfo->tx_num_xmit += report_data->rates[0].count;
777         spinfo->tx_num_failed += (report_data->rates[0].count - report_data->ampdu_ack_len );
778         rate = &pidrate[spinfo->txrate_idx];
779         rate->this_fail += ( report_data->rates[0].count - report_data->ampdu_ack_len );
780         rate->this_attempt += report_data->rates[0].count;
781         rate->this_success += report_data->ampdu_ack_len;
782     }
783     if (report_data_index != spinfo->txrate_idx && report_data_index == spinfo->tmp_rate_idx)
784     {
785         spinfo->feedback_probes += report_data->ampdu_len;
786         rate = &pidrate[spinfo->tmp_rate_idx];
787         rate->this_fail += ( report_data->rates[0].count - report_data->ampdu_ack_len );
788         rate->this_attempt += report_data->rates[0].count;
789         rate->this_success += report_data->ampdu_ack_len;
790     }
791     period = msecs_to_jiffies(RC_PID_INTERVAL);
792     if (time_after(jiffies, spinfo->last_sample + period))
793     {
794 #ifdef RATE_CONTROL_PERCENTAGE_TRACE
795         rate = &pidrate[spinfo->txrate_idx];
796         if(rate->this_success > rate->this_attempt)
797         {
798             printk("#############################\n");
799             printk("this_success[%ld] this_attempt[%ld]\n",rate->this_success,rate->this_attempt);
800             printk("#############################\n");
801         }
802         else
803         {
804             if(percentage == 0)
805                 percentage = (int)((rate->this_success*100)/rate->this_attempt);
806             else
807                 percentage = (percentage + (int)((rate->this_success*100)/rate->this_attempt))/2;
808             printk("Percentage[%d]\n",percentage);
809             if((percentageCounter % 16)==1)
810                 percentage = 0;
811         }
812 #endif
813 #ifdef RATE_CONTROL_STUPID_DEBUG
814         if (spinfo->txrate_idx != spinfo->tmp_rate_idx)
815         {
816             rate = &pidrate[spinfo->tmp_rate_idx];
817             if (spinfo->monitoring && ((rate->this_attempt == 0)||(rate->this_attempt!=MAXPROBES)))
818             {
819                 printk("Probe result a[%ld]s[%ld]f[%ld]",rate->this_attempt,rate->this_success,rate->this_fail);
820             }
821             rate = &pidrate[spinfo->txrate_idx];
822             printk("New a[%ld]s[%ld]f[%ld] \n",rate->this_attempt,rate->this_success,rate->this_fail);
823         }
824         else
825         {
826             rate = &pidrate[spinfo->txrate_idx];
827             printk("New a[%ld]s[%ld]f[%ld] \n",rate->this_attempt,rate->this_success,rate->this_fail);
828         }
829         printk("w[%d]x%03d-f%03d\n",rc_sta->rc_wsid,spinfo->tx_num_xmit,spinfo->tx_num_failed);
830 #endif
831         rate_control_pid_sample(sc->rc, pinfo, rc_sta, spinfo);
832     }
833 }
ssv6xxx_sample_work(struct work_struct * work)834 void ssv6xxx_sample_work(struct work_struct *work)
835 {
836     struct ssv_softc *sc = container_of(work, struct ssv_softc, rc_sample_work);
837     struct ssv_rate_ctrl *ssv_rc=sc->rc;
838     struct sk_buff *skb;
839     struct cfg_host_event *host_event;
840     struct ssv_sta_rc_info *rc_sta=NULL;
841     struct firmware_rate_control_report_data *report_data;
842     struct ssv_sta_info *ssv_sta;
843     u8 hw_wsid = 0;
844     sc->rc_sample_sechedule = 1;
845     while(1)
846     {
847         skb = skb_dequeue(&sc->rc_report_queue);
848         if(skb == NULL)
849             break;
850 #ifdef DISABLE_RATE_CONTROL_SAMPLE
851         {
852             dev_kfree_skb_any(skb);
853             continue;
854         }
855 #endif
856         host_event = (struct cfg_host_event *)skb->data;
857         if((host_event->h_event == SOC_EVT_RC_AMPDU_REPORT) || (host_event->h_event == SOC_EVT_RC_MPDU_REPORT))
858         {
859             report_data = (struct firmware_rate_control_report_data *)&host_event->dat[0];
860             hw_wsid = report_data->wsid;
861         }
862         else
863         {
864             printk("RC work get garbage!!\n");
865             dev_kfree_skb_any(skb);
866             continue;
867         }
868         if(hw_wsid >= SSV_RC_MAX_HARDWARE_SUPPORT)
869         {
870 #ifdef RATE_CONTROL_DEBUG
871             printk("[RC]rc_sta is NULL pointer Check-0!!\n");
872 #endif
873             dev_kfree_skb_any(skb);
874             continue;
875         }
876         ssv_sta = &sc->sta_info[hw_wsid];
877         if (ssv_sta->sta == NULL)
878         {
879             dev_err(sc->dev, "Null STA %d for RC report.\n", hw_wsid);
880             rc_sta = NULL;
881         }
882         else
883         {
884             struct ssv_sta_priv_data *ssv_sta_priv = (struct ssv_sta_priv_data *)ssv_sta->sta->drv_priv;
885             rc_sta = &ssv_rc->sta_rc_info[ssv_sta_priv->rc_idx];
886             if (rc_sta->rc_wsid != hw_wsid)
887             {
888                 rc_sta = NULL;
889             }
890         }
891         if(rc_sta == NULL)
892         {
893             dev_err(sc->dev, "[RC]rc_sta is NULL pointer Check-1!!\n");
894             dev_kfree_skb_any(skb);
895             continue;
896         }
897 #if 0
898         if(rc_sta->rc_wsid != hw_wsid)
899         {
900             printk("[RC] wsid mapping [ERROR] feedback wsid[%d] rc_wsid[%d]...><\n",hw_wsid,rc_sta->rc_wsid);
901 #if 1
902             for(i=0;i<SSV_RC_MAX_STA;i++)
903             {
904                 rc_sta = &ssv_rc->sta_rc_info[i];
905                 if(rc_sta == NULL)
906                     break;
907                 if(hw_wsid == rc_sta->rc_wsid)
908                 {
909                     printk("[RC] Get new mapping-%d-%d...@o@\n",hw_wsid, i);
910                     break;
911                 }
912             }
913    if(i == SSV_RC_MAX_STA)
914 #endif
915                 rc_sta = NULL;
916         }
917 #endif
918         if(rc_sta == NULL)
919         {
920 #ifdef RATE_CONTROL_DEBUG
921             printk("[RC]rc_sta is NULL pointer Check-2!!\n");
922 #endif
923             dev_kfree_skb_any(skb);
924             continue;
925         }
926         if(rc_sta->is_ht)
927         {
928             ssv6xxx_legacy_report_handler(sc,skb,rc_sta);
929             ssv6xxx_ht_report_handler(sc,skb,rc_sta);
930         }
931         else
932             ssv6xxx_legacy_report_handler(sc,skb,rc_sta);
933         dev_kfree_skb_any(skb);
934     }
935     sc->rc_sample_sechedule = 0;
936 }
ssv6xxx_tx_status(void * priv,struct ieee80211_supported_band * sband,struct ieee80211_sta * sta,void * priv_sta,struct sk_buff * skb)937 static void ssv6xxx_tx_status(void *priv, struct ieee80211_supported_band *sband,
938                   struct ieee80211_sta *sta, void *priv_sta,
939                   struct sk_buff *skb)
940 {
941     struct ssv_softc *sc;
942     struct ieee80211_hdr *hdr;
943     __le16 fc;
944     hdr = (struct ieee80211_hdr *)skb->data;
945     fc = hdr->frame_control;
946     if (!priv_sta || !ieee80211_is_data_qos(fc))
947         return;
948     sc = (struct ssv_softc *)priv;
949     if ( conf_is_ht(&sc->hw->conf)
950         && (!(skb->protocol == cpu_to_be16(ETH_P_PAE))))
951     {
952         if (skb_get_queue_mapping(skb) != IEEE80211_AC_VO)
953             ssv6200_ampdu_tx_update_state(priv, sta, skb);
954     }
955     return;
956 }
957 #if 1
rateControlGetRate(u8 rateIndex,char * pointer)958 static void rateControlGetRate(u8 rateIndex, char * pointer)
959 {
960     switch(rateIndex)
961     {
962         case 0:
963             sprintf(pointer, "1Mbps");
964             return;
965         case 1:
966         case 4:
967             sprintf(pointer, "2Mbps");
968             return;
969         case 2:
970         case 5:
971             sprintf(pointer, "5.5Mbps");
972             return;
973         case 3:
974         case 6:
975             sprintf(pointer, "11Mbps");
976             return;
977         case 7:
978             sprintf(pointer, "6Mbps");
979             return;
980         case 8:
981             sprintf(pointer, "9Mbps");
982             return;
983         case 9:
984             sprintf(pointer, "12Mbps");
985             return;
986         case 10:
987             sprintf(pointer, "18Mbps");
988             return;
989         case 11:
990             sprintf(pointer, "24Mbps");
991             return;
992         case 12:
993             sprintf(pointer, "36Mbps");
994             return;
995         case 13:
996             sprintf(pointer, "48Mbps");
997             return;
998         case 14:
999             sprintf(pointer, "54Mbps");
1000             return;
1001         case 15:
1002         case 31:
1003             sprintf(pointer, "MCS0-l");
1004             return;
1005         case 16:
1006         case 32:
1007             sprintf(pointer, "MCS1-l");
1008             return;
1009         case 17:
1010         case 33:
1011             sprintf(pointer, "MCS2-l");
1012             return;
1013         case 18:
1014         case 34:
1015             sprintf(pointer, "MCS3-l");
1016             return;
1017         case 19:
1018         case 35:
1019             sprintf(pointer, "MCS4-l");
1020             return;
1021         case 20:
1022         case 36:
1023             sprintf(pointer, "MCS5-l");
1024             return;
1025         case 21:
1026         case 37:
1027             sprintf(pointer, "MCS6-l");
1028             return;
1029         case 22:
1030         case 38:
1031             sprintf(pointer, "MCS7-l");
1032             return;
1033         case 23:
1034             sprintf(pointer, "MCS0-s");
1035             return;
1036         case 24:
1037             sprintf(pointer, "MCS1-s");
1038             return;
1039         case 25:
1040             sprintf(pointer, "MCS2-s");
1041             return;
1042         case 26:
1043             sprintf(pointer, "MCS3-s");
1044             return;
1045         case 27:
1046             sprintf(pointer, "MCS4-s");
1047             return;
1048         case 28:
1049             sprintf(pointer, "MCS5-s");
1050             return;
1051         case 29:
1052             sprintf(pointer, "MCS6-s");
1053             return;
1054         case 30:
1055             sprintf(pointer, "MCS7-s");
1056             return;
1057         default:
1058             sprintf(pointer, "Unknow");
1059             return;
1060     };
1061 }
1062 #endif
ssv6xxx_get_rate(void * priv,struct ieee80211_sta * sta,void * priv_sta,struct ieee80211_tx_rate_control * txrc)1063 static void ssv6xxx_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1064                  struct ieee80211_tx_rate_control *txrc)
1065 {
1066     struct ssv_softc *sc=priv;
1067     struct ssv_rate_ctrl *ssv_rc=sc->rc;
1068     struct ssv_sta_rc_info *rc_sta=priv_sta;
1069     struct sk_buff *skb = txrc->skb;
1070     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1071     struct ieee80211_tx_rate *rates = tx_info->control.rates;
1072     struct rc_pid_sta_info *spinfo=&rc_sta->spinfo;
1073     struct ssv_rc_rate *rc_rate = NULL;
1074     struct ssv_sta_priv_data *ssv_sta_priv;
1075     int rateidx = 99;
1076     #if 0
1077     if ( (tx_info->control.vif != NULL)
1078         && (tx_info->control.vif->p2p))
1079     {
1080         tx_info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
1081     }
1082     #endif
1083     if (rate_control_send_low(sta, priv_sta, txrc))
1084     {
1085         int i = 0;
1086         int total_rates = (sizeof(ssv_11bgn_rate_table) / sizeof(ssv_11bgn_rate_table[0]));
1087 #if 1
1088         if ((txrc->rate_idx_mask & (1 << rates[0].idx)) == 0)
1089         {
1090             u32 rate_idx = rates[0].idx + 1;
1091             u32 rate_idx_mask = txrc->rate_idx_mask >> rate_idx;
1092             while (rate_idx_mask && (rate_idx_mask & 1) == 0)
1093             {
1094                 rate_idx_mask >>= 1;
1095                 rate_idx++;
1096             }
1097             if (rate_idx_mask)
1098                 rates[0].idx = rate_idx;
1099             else
1100             {
1101                 WARN_ON(rate_idx_mask == 0);
1102             }
1103         }
1104 #endif
1105         for (i = 0; i < total_rates; i++)
1106         {
1107             if (rates[0].idx == ssv_11bgn_rate_table[i].dot11_rate_idx)
1108             {
1109                 break;
1110             }
1111         }
1112         if (i < total_rates)
1113             rc_rate = &ssv_rc->rc_table[i];
1114         else
1115         {
1116             WARN_ON("Failed to find matching low rate.");
1117         }
1118     }
1119     if (rc_rate == NULL) {
1120         if (conf_is_ht(&sc->hw->conf) &&
1121                 (sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING))
1122             tx_info->flags |= IEEE80211_TX_CTL_LDPC;
1123         if (conf_is_ht(&sc->hw->conf) &&
1124                 (sta->ht_cap.cap & IEEE80211_HT_CAP_TX_STBC))
1125             tx_info->flags |= (1 << IEEE80211_TX_CTL_STBC_SHIFT);
1126         if (sc->sc_flags & SC_OP_FIXED_RATE) {
1127             rateidx = sc->max_rate_idx;
1128         }
1129         else {
1130             if (rc_sta->rc_valid == false) {
1131                 rateidx = 0;
1132             }
1133             else {
1134                 if ((rc_sta->rc_wsid >= SSV_RC_MAX_HARDWARE_SUPPORT) || (rc_sta->rc_wsid < 0))
1135                 {
1136                     ssv_sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
1137                     {
1138                         if ((rc_sta->ht_rc_type >= RC_TYPE_HT_SGI_20) &&
1139                             (ssv_sta_priv->rx_data_rate < SSV62XX_RATE_MCS_INDEX))
1140                         {
1141                             rateidx = rc_sta->pinfo.rinfo[spinfo->txrate_idx].rc_index;
1142                             #if 0
1143                             printk("RC %d rx %d tx %d\n", ssv_sta_priv->sta_idx,
1144                                    ssv_sta_priv->rx_data_rate, rateidx);
1145                             #endif
1146                         }
1147                         else
1148                         {
1149                             rateidx = ssv_sta_priv->rx_data_rate;
1150                         }
1151                     }
1152                 }
1153                 else
1154                 {
1155                     if (rc_sta->is_ht)
1156                     {
1157 #ifdef DISABLE_RATE_CONTROL_SAMPLE
1158                         rateidx = rc_sta->ht.groups.rates[MCS_GROUP_RATES-1].rc_index;
1159 #else
1160                         rateidx = rc_sta->pinfo.rinfo[spinfo->txrate_idx].rc_index;
1161 #endif
1162                     }
1163                     else
1164                     {
1165 #if 0
1166                         if (spinfo->monitoring && spinfo->probe_cnt > 0) {
1167                             rateidx = rc_sta->pinfo.rinfo[spinfo->tmp_rate_idx].rc_index;
1168                             spinfo->probe_cnt--;
1169                         }
1170                         else
1171 #endif
1172                         {
1173                             BUG_ON(spinfo->txrate_idx >= rc_sta->rc_num_rate);
1174                             rateidx = rc_sta->pinfo.rinfo[spinfo->txrate_idx].rc_index;
1175                         }
1176                         if(rateidx<4)
1177                         {
1178                             if(rateidx)
1179                             {
1180                                 if ((sc->sc_flags & SC_OP_SHORT_PREAMBLE)||(txrc->short_preamble))
1181                                 {
1182                                     rateidx += 3;
1183                                 }
1184                             }
1185                         }
1186                     }
1187                 }
1188             }
1189         }
1190         rc_rate = &ssv_rc->rc_table[rateidx];
1191 #if 1
1192         if (spinfo->real_hw_index != rc_rate->hw_rate_idx)
1193         {
1194             char string[24];
1195             rateControlGetRate(rc_rate->hw_rate_idx,string);
1196         }
1197 #endif
1198   spinfo->real_hw_index = rc_rate->hw_rate_idx;
1199         rates[0].count = 4;
1200         rates[0].idx = rc_rate->dot11_rate_idx;
1201         tx_info->control.rts_cts_rate_idx =
1202             ssv_rc->rc_table[rc_rate->ctrl_rate_idx].dot11_rate_idx;
1203         if (rc_rate->rc_flags & RC_FLAG_SHORT_PREAMBLE)
1204             rates[0].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
1205         if (rc_rate->rc_flags & RC_FLAG_HT) {
1206             rates[0].flags |= IEEE80211_TX_RC_MCS;
1207             if (rc_rate->rc_flags & RC_FLAG_HT_SGI)
1208                 rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
1209             if (rc_rate->rc_flags & RC_FLAG_HT_GF)
1210                 rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
1211         }
1212 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
1213         if (txrc->rts)
1214         {
1215             rates[0].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
1216         }
1217         if ((tx_info->control.vif &&
1218             tx_info->control.vif->bss_conf.use_cts_prot) &&
1219             (rc_rate->phy_type==WLAN_RC_PHY_OFDM ||
1220             rc_rate->phy_type>WLAN_RC_PHY_OFDM))
1221         {
1222             rates[0].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
1223             tx_info->control.rts_cts_rate_idx = 1;
1224         }
1225 #endif
1226     }
1227     rates[1].count = 0;
1228     rates[1].idx = -1;
1229     rates[SSV_DRATE_IDX].count = rc_rate->hw_rate_idx;
1230  rc_rate = &ssv_rc->rc_table[rc_rate->ctrl_rate_idx];
1231  rates[SSV_CRATE_IDX].count = rc_rate->hw_rate_idx;
1232 }
pide_frame_duration(size_t len,int rate,int short_preamble,int flags)1233 int pide_frame_duration(size_t len,
1234                  int rate, int short_preamble, int flags)
1235 {
1236     int dur=0;
1237     if (flags == WLAN_RC_PHY_CCK)
1238     {
1239         dur = 10;
1240         dur += short_preamble ? (72 + 24) : (144 + 48);
1241         dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
1242     }
1243     else {
1244         dur = 16;
1245         dur += 16;
1246         dur += 4;
1247         dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
1248                     4 * rate);
1249     }
1250     return dur;
1251 }
ssv62xx_rc_caps(struct ssv_sta_rc_info * rc_sta)1252 static void ssv62xx_rc_caps(struct ssv_sta_rc_info *rc_sta)
1253 {
1254     struct rc_pid_sta_info *spinfo;
1255     struct rc_pid_info *pinfo;
1256     struct rc_pid_rateinfo *rinfo;
1257     int i;
1258     spinfo = &rc_sta->spinfo;
1259     pinfo = &rc_sta->pinfo;
1260     memset(spinfo, 0, sizeof(struct rc_pid_sta_info));
1261     memset(pinfo, 0, sizeof(struct rc_pid_info));
1262     rinfo = rc_sta->pinfo.rinfo;
1263     for(i=0; i<rc_sta->rc_num_rate; i++) {
1264         rinfo[i].rc_index = ssv6xxx_rc_rate_set[rc_sta->rc_type][i+1];
1265         rinfo[i].diff = i * RC_PID_NORM_OFFSET;
1266         rinfo[i].index = (u16)i;
1267         rinfo[i].perfect_tx_time = TDIFS + (TSLOT * 15 >> 1) + pide_frame_duration(1530,
1268             ssv_11bgn_rate_table[rinfo[i].rc_index].rate_kbps/100, 1,ssv_11bgn_rate_table[rinfo[i].rc_index].phy_type) +
1269             pide_frame_duration(10, ssv_11bgn_rate_table[rinfo[i].rc_index].rate_kbps/100, 1,ssv_11bgn_rate_table[rinfo[i].rc_index].phy_type);
1270 #if 1
1271         printk("[RC]Init perfect_tx_time[%d][%d]\n",i,rinfo[i].perfect_tx_time);
1272 #endif
1273         rinfo[i].throughput = 0;
1274     }
1275     if(rc_sta->is_ht)
1276     {
1277         if(ssv6xxx_rc_rate_set[rc_sta->ht_rc_type][0] == 12)
1278             spinfo->txrate_idx = 4;
1279         else
1280             spinfo->txrate_idx = 0;
1281     }
1282     else
1283     {
1284         spinfo->txrate_idx = ssv6xxx_rate_lowest_index(rc_sta);
1285 #ifdef DISABLE_RATE_CONTROL_SAMPLE
1286         spinfo->txrate_idx = ssv6xxx_rate_highest_index(rc_sta);
1287 #endif
1288     }
1289     spinfo->real_hw_index = 0;
1290     spinfo->probe_cnt = MAXPROBES;
1291     spinfo->tmp_rate_idx = spinfo->txrate_idx;
1292     spinfo->oldrate = spinfo->txrate_idx;
1293     spinfo->last_sample = jiffies;
1294     spinfo->last_report = jiffies;
1295 }
ssv6xxx_rate_update_rc_type(void * priv,struct ieee80211_supported_band * sband,struct ieee80211_sta * sta,void * priv_sta)1296 static void ssv6xxx_rate_update_rc_type(void *priv, struct ieee80211_supported_band *sband,
1297                   struct ieee80211_sta *sta, void *priv_sta)
1298 {
1299     struct ssv_softc *sc=priv;
1300     struct ssv_hw *sh=sc->sh;
1301     struct ssv_sta_rc_info *rc_sta=priv_sta;
1302     int i;
1303     u32 ht_supp_rates = 0;
1304     BUG_ON(rc_sta->rc_valid == false);
1305     printk("[I] %s(): \n", __FUNCTION__);
1306     rc_sta->ht_supp_rates = 0;
1307     rc_sta->rc_supp_rates = 0;
1308     rc_sta->is_ht = 0;
1309 #ifndef CONFIG_CH14_SUPPORT_GN_MODE
1310     if(sc->cur_channel->hw_value == 14)
1311     {
1312         printk("[RC init ]Channel 14 support\n");
1313         if((sta->supp_rates[sband->band] & (~0xfL)) == 0x0)
1314         {
1315             printk("[RC init ]B only mode\n");
1316             rc_sta->rc_type = RC_TYPE_B_ONLY;
1317         }
1318         else
1319         {
1320             printk("[RC init ]GB mode\n");
1321             rc_sta->rc_type = RC_TYPE_LEGACY_GB;
1322         }
1323     }
1324     else
1325 #endif
1326     if (sta->ht_cap.ht_supported == true) {
1327         printk("[RC init ]HT support wsid\n");
1328         for (i = 0; i < SSV_HT_RATE_MAX; i++) {
1329             if (sta->ht_cap.mcs.rx_mask[i/MCS_GROUP_RATES] & (1<<(i%MCS_GROUP_RATES)))
1330                 ht_supp_rates |= BIT(i);
1331         }
1332         rc_sta->ht_supp_rates = ht_supp_rates;
1333         if (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD)
1334         {
1335             rc_sta->rc_type = RC_TYPE_HT_GF;
1336             rc_sta->ht_rc_type = RC_TYPE_HT_GF;
1337         }
1338         else if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
1339         {
1340             rc_sta->rc_type = RC_TYPE_SGI_20;
1341             rc_sta->ht_rc_type = RC_TYPE_HT_SGI_20;
1342         }
1343         else
1344         {
1345             rc_sta->rc_type = RC_TYPE_LGI_20;
1346             rc_sta->ht_rc_type = RC_TYPE_HT_LGI_20;
1347         }
1348     }
1349     else
1350     {
1351         if((sta->supp_rates[sband->band] & (~0xfL)) == 0x0){
1352             rc_sta->rc_type = RC_TYPE_B_ONLY;
1353             printk("[RC init ]B only mode\n");
1354         }
1355         else{
1356             rc_sta->rc_type = RC_TYPE_LEGACY_GB;
1357             printk("[RC init ]legacy G mode\n");
1358         }
1359     }
1360 #ifdef CONFIG_SSV_DPD
1361 #ifdef CONFIG_SSV_CABRIO_E
1362     if(rc_sta->rc_type == RC_TYPE_B_ONLY)
1363     {
1364         SMAC_REG_WRITE(sh, ADR_TX_FE_REGISTER, 0x3D3E84FE);
1365         SMAC_REG_WRITE(sh, ADR_RX_FE_REGISTER_1, 0x1457D79);
1366         SMAC_REG_WRITE(sh, ADR_DPD_CONTROL, 0x0);
1367     }
1368     else
1369     {
1370         SMAC_REG_WRITE(sh, ADR_TX_FE_REGISTER, 0x3CBE84FE);
1371         SMAC_REG_WRITE(sh, ADR_RX_FE_REGISTER_1, 0x4507F9);
1372         SMAC_REG_WRITE(sh, ADR_DPD_CONTROL, 0x3);
1373     }
1374 #endif
1375 #endif
1376     if((rc_sta->rc_type != RC_TYPE_B_ONLY) && (rc_sta->rc_type != RC_TYPE_LEGACY_GB))
1377     {
1378         if ((sta->ht_cap.ht_supported) && (sh->cfg.hw_caps & SSV6200_HW_CAP_AMPDU_TX))
1379         {
1380             rc_sta->is_ht = 1;
1381             ssv62xx_ht_rc_caps(ssv6xxx_rc_rate_set, rc_sta);
1382         }
1383     }
1384     {
1385         rc_sta->rc_num_rate = (u8)ssv6xxx_rc_rate_set[rc_sta->rc_type][0];
1386         if((rc_sta->rc_type == RC_TYPE_HT_GF) ||
1387             (rc_sta->rc_type == RC_TYPE_LGI_20) || (rc_sta->rc_type == RC_TYPE_SGI_20))
1388         {
1389             if(rc_sta->rc_num_rate == 12)
1390             {
1391                 rc_sta->rc_supp_rates = sta->supp_rates[sband->band] & 0xfL;
1392                 rc_sta->rc_supp_rates |= (ht_supp_rates << 4);
1393             }
1394             else
1395                 rc_sta->rc_supp_rates = ht_supp_rates;
1396         }
1397         else if(rc_sta->rc_type == RC_TYPE_LEGACY_GB)
1398             rc_sta->rc_supp_rates = sta->supp_rates[sband->band];
1399         else if(rc_sta->rc_type == RC_TYPE_B_ONLY)
1400             rc_sta->rc_supp_rates = sta->supp_rates[sband->band] & 0xfL;
1401         ssv62xx_rc_caps(rc_sta);
1402     }
1403 }
1404 #if LINUX_VERSION_CODE > 0x030500
ssv6xxx_rate_update(void * priv,struct ieee80211_supported_band * sband,struct cfg80211_chan_def * chandef,struct ieee80211_sta * sta,void * priv_sta,u32 changed)1405 static void ssv6xxx_rate_update(void *priv, struct ieee80211_supported_band *sband,
1406 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
1407                             struct cfg80211_chan_def *chandef,
1408 #endif
1409                             struct ieee80211_sta *sta, void *priv_sta,
1410                             u32 changed)
1411 #else
1412 static void ssv6xxx_rate_update(void *priv, struct ieee80211_supported_band *sband,
1413                             struct ieee80211_sta *sta, void *priv_sta,
1414                             u32 changed, enum nl80211_channel_type oper_chan_type)
1415 #endif
1416 {
1417     printk("%s: changed=%d\n",__FUNCTION__,changed);
1418     return;
1419 }
ssv6xxx_rate_init(void * priv,struct ieee80211_supported_band * sband,struct cfg80211_chan_def * chandef,struct ieee80211_sta * sta,void * priv_sta)1420 static void ssv6xxx_rate_init(void *priv, struct ieee80211_supported_band *sband,
1421 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
1422                   struct cfg80211_chan_def *chandef,
1423 #endif
1424                   struct ieee80211_sta *sta, void *priv_sta)
1425 {
1426     ssv6xxx_rate_update_rc_type(priv, sband, sta, priv_sta);
1427 }
ssv6xxx_rate_alloc_sta(void * priv,struct ieee80211_sta * sta,gfp_t gfp)1428 static void *ssv6xxx_rate_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1429 {
1430     struct ssv_sta_priv_data *sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
1431 #ifndef RC_STA_DIRECT_MAP
1432     struct ssv_softc *sc = priv;
1433     struct ssv_rate_ctrl *ssv_rc = sc->rc;
1434     int s;
1435     sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
1436     for(s=0; s<SSV_RC_MAX_STA; s++) {
1437         if (ssv_rc->sta_rc_info[s].rc_valid == false) {
1438             printk("%s(): use index %d\n", __FUNCTION__, s);
1439             memset(&ssv_rc->sta_rc_info[s], 0, sizeof(struct ssv_sta_rc_info));
1440             ssv_rc->sta_rc_info[s].rc_valid = true;
1441             ssv_rc->sta_rc_info[s].rc_wsid = -1;
1442             sta_priv->rc_idx = s;
1443             return &ssv_rc->sta_rc_info[s];
1444         }
1445     }
1446     return NULL;
1447 #else
1448     sta_priv->rc_idx = (-1);
1449     return sta_priv;
1450 #endif
1451 }
ssv6xxx_rate_free_sta(void * priv,struct ieee80211_sta * sta,void * priv_sta)1452 static void ssv6xxx_rate_free_sta(void *priv, struct ieee80211_sta *sta,
1453                               void *priv_sta)
1454 {
1455     struct ssv_sta_rc_info *rc_sta=priv_sta;
1456     rc_sta->rc_valid = false;
1457 }
ssv6xxx_rate_alloc(struct ieee80211_hw * hw,struct dentry * debugfsdir)1458 static void *ssv6xxx_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
1459 {
1460     struct ssv_softc *sc=hw->priv;
1461     struct ssv_rate_ctrl *ssv_rc;
1462     sc->rc = kzalloc(sizeof(struct ssv_rate_ctrl), GFP_KERNEL);
1463     if (!sc->rc) {
1464         printk("%s(): Unable to allocate RC structure !\n",
1465             __FUNCTION__);
1466         return NULL;
1467     }
1468     memset(sc->rc, 0, sizeof(struct ssv_rate_ctrl));
1469     ssv_rc = (struct ssv_rate_ctrl *)sc->rc;
1470     ssv_rc->rc_table = ssv_11bgn_rate_table;
1471     skb_queue_head_init(&sc->rc_report_queue);
1472     INIT_WORK(&sc->rc_sample_work,ssv6xxx_sample_work);
1473     sc->rc_sample_workqueue = create_workqueue("ssv6xxx_rc_sample");
1474     sc->rc_sample_sechedule = 0;
1475     return hw->priv;
1476 }
ssv6xxx_rate_free(void * priv)1477 static void ssv6xxx_rate_free(void *priv)
1478 {
1479     struct ssv_softc *sc=priv;
1480     if (sc->rc) {
1481         kfree(sc->rc);
1482         sc->rc = NULL;
1483     }
1484     sc->rc_sample_sechedule = 0;
1485     cancel_work_sync(&sc->rc_sample_work);
1486     flush_workqueue(sc->rc_sample_workqueue);
1487     destroy_workqueue(sc->rc_sample_workqueue);
1488 }
1489 static struct rate_control_ops ssv_rate_ops =
1490 {
1491 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0)
1492     .module = NULL,
1493 #endif
1494     .name = "ssv6xxx_rate_control",
1495     .tx_status = ssv6xxx_tx_status,
1496     .get_rate = ssv6xxx_get_rate,
1497     .rate_init = ssv6xxx_rate_init,
1498     .rate_update = ssv6xxx_rate_update,
1499     .alloc = ssv6xxx_rate_alloc,
1500     .free = ssv6xxx_rate_free,
1501     .alloc_sta = ssv6xxx_rate_alloc_sta,
1502     .free_sta = ssv6xxx_rate_free_sta,
1503 };
ssv6xxx_rc_mac8011_rate_idx(struct ssv_softc * sc,int hw_rate_idx,struct ieee80211_rx_status * rxs)1504 void ssv6xxx_rc_mac8011_rate_idx(struct ssv_softc *sc,
1505             int hw_rate_idx, struct ieee80211_rx_status *rxs)
1506 {
1507     struct ssv_rate_ctrl *ssv_rc=sc->rc;
1508     struct ssv_rc_rate *rc_rate;
1509     BUG_ON(hw_rate_idx>=RATE_TABLE_SIZE &&
1510         hw_rate_idx < 0);
1511     rc_rate = &ssv_rc->rc_table[hw_rate_idx];
1512     if (rc_rate->rc_flags & RC_FLAG_HT) {
1513         rxs->flag |= RX_FLAG_HT;
1514         if (rc_rate->rc_flags & RC_FLAG_HT_SGI)
1515             rxs->flag |= RX_FLAG_SHORT_GI;
1516     }
1517     else {
1518         if (rc_rate->rc_flags & RC_FLAG_SHORT_PREAMBLE)
1519             rxs->flag |= RX_FLAG_SHORTPRE;
1520     }
1521     rxs->rate_idx = rc_rate->dot11_rate_idx;
1522 }
ssv6xxx_rc_hw_rate_idx(struct ssv_softc * sc,struct ieee80211_tx_info * info,struct ssv_rate_info * sr)1523 void ssv6xxx_rc_hw_rate_idx(struct ssv_softc *sc,
1524             struct ieee80211_tx_info *info, struct ssv_rate_info *sr)
1525 {
1526     struct ieee80211_tx_rate *tx_rate;
1527     struct ssv_rate_ctrl *ssv_rc=sc->rc;
1528     tx_rate = &info->control.rates[0];
1529     sr->d_flags = (ssv_rc->rc_table[tx_rate[SSV_DRATE_IDX].count].phy_type == WLAN_RC_PHY_OFDM) ? IEEE80211_RATE_ERP_G:0;
1530     sr->d_flags |= (ssv_rc->rc_table[tx_rate[SSV_DRATE_IDX].count].rc_flags & RC_FLAG_SHORT_PREAMBLE)? IEEE80211_RATE_SHORT_PREAMBLE:0;
1531     sr->c_flags = (ssv_rc->rc_table[tx_rate[SSV_CRATE_IDX].count].phy_type == WLAN_RC_PHY_OFDM) ? IEEE80211_RATE_ERP_G:0;
1532     sr->c_flags |= (ssv_rc->rc_table[tx_rate[SSV_CRATE_IDX].count].rc_flags & RC_FLAG_SHORT_PREAMBLE)? IEEE80211_RATE_SHORT_PREAMBLE:0;
1533     sr->drate_kbps = ssv_rc->rc_table[tx_rate[SSV_DRATE_IDX].count].rate_kbps;
1534     sr->drate_hw_idx = tx_rate[SSV_DRATE_IDX].count;
1535     sr->crate_kbps = ssv_rc->rc_table[tx_rate[SSV_CRATE_IDX].count].rate_kbps;
1536     sr->crate_hw_idx = tx_rate[SSV_CRATE_IDX].count;
1537 }
1538 #ifdef RATE_CONTROL_REALTIME_UPDATA
ssv6xxx_rc_hw_rate_update_check(struct sk_buff * skb,struct ssv_softc * sc,u32 do_rts_cts)1539 u8 ssv6xxx_rc_hw_rate_update_check(struct sk_buff *skb, struct ssv_softc *sc, u32 do_rts_cts)
1540 {
1541     int ret = 0;
1542     struct ssv_rate_ctrl *ssv_rc = sc->rc;
1543     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1544     struct SKB_info_st *skb_info = (struct SKB_info_st *)skb->head;
1545     struct ieee80211_sta *sta = skb_info->sta;
1546     struct ieee80211_tx_rate *rates = &tx_info->control.rates[0];
1547     struct ssv_rc_rate *rc_rate = NULL;
1548     u8 rateidx=0;
1549     struct ssv_sta_rc_info *rc_sta = NULL;
1550     struct rc_pid_sta_info *spinfo;
1551     struct ssv_sta_priv_data *sta_priv = NULL;
1552     unsigned long period=0;
1553     if (sc->sc_flags & SC_OP_FIXED_RATE)
1554         return ret;
1555     if(sta == NULL)
1556         return ret;
1557     sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
1558     if(sta_priv == NULL)
1559     {
1560 #ifdef RATE_CONTROL_DEBUG
1561         printk("%s sta_priv == NULL \n\r", __FUNCTION__);
1562 #endif
1563         return ret;
1564     }
1565     if((sta_priv->rc_idx < 0)||(sta_priv->rc_idx >= SSV_RC_MAX_STA))
1566     {
1567 #ifdef RATE_CONTROL_DEBUG
1568         printk("%s rc_idx %x illegal \n\r", __FUNCTION__, sta_priv->rc_idx);
1569 #endif
1570         return ret;
1571     }
1572     rc_sta = &ssv_rc->sta_rc_info[sta_priv->rc_idx];
1573     if(rc_sta->rc_valid == false)
1574     {
1575 #ifdef RATE_CONTROL_DEBUG
1576         printk("%s rc_valid false \n\r", __FUNCTION__);
1577 #endif
1578         return ret;
1579     }
1580     spinfo= &rc_sta->spinfo;
1581     period = msecs_to_jiffies(RC_PID_REPORT_INTERVAL);
1582     if (time_after(jiffies, spinfo->last_report + period))
1583     {
1584         ret |= RC_FIRMWARE_REPORT_FLAG;
1585         spinfo->last_report = jiffies;
1586     }
1587     {
1588         if (spinfo->monitoring)
1589         {
1590             if(spinfo->probe_report_flag == 0)
1591             {
1592                 ret |= RC_FIRMWARE_REPORT_FLAG;
1593                 spinfo->last_report = jiffies;
1594                 spinfo->probe_report_flag = 1;
1595                 rateidx = spinfo->real_hw_index;
1596             }
1597             else if (spinfo->probe_cnt > 0 && spinfo->probe_report_flag) {
1598                 rateidx = rc_sta->pinfo.rinfo[spinfo->tmp_rate_idx].rc_index;
1599                 spinfo->probe_cnt--;
1600                 if(spinfo->probe_cnt == 0)
1601                 {
1602                     ret |= RC_FIRMWARE_REPORT_FLAG;
1603                     spinfo->last_report = jiffies;
1604                 }
1605             }
1606             else
1607                 rateidx = spinfo->real_hw_index;
1608         }
1609         else
1610             rateidx = spinfo->real_hw_index;
1611     }
1612     if(rateidx >= RATE_TABLE_SIZE)
1613     {
1614         printk("[ERROR]rateidx over range\n\r");
1615         return 0;
1616     }
1617     rc_rate = &ssv_rc->rc_table[rateidx];
1618 #ifdef RATE_CONTROL_STUPID_DEBUG
1619     if (spinfo->monitoring && (spinfo->probe_cnt))
1620     {
1621         char string[24];
1622         rateControlGetRate(rc_rate->hw_rate_idx,string);
1623         printk("[RC]Probe rate[%s]\n",string);
1624     }
1625 #endif
1626     if(rc_rate == NULL)
1627         return ret;
1628     if(rc_rate->hw_rate_idx != rates[SSV_DRATE_IDX].count)
1629     {
1630         rates[0].flags = 0;
1631         if (rc_rate->rc_flags & RC_FLAG_SHORT_PREAMBLE)
1632             rates[0].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
1633         if (rc_rate->rc_flags & RC_FLAG_HT) {
1634             rates[0].flags |= IEEE80211_TX_RC_MCS;
1635             if (rc_rate->rc_flags & RC_FLAG_HT_SGI)
1636                 rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
1637             if (rc_rate->rc_flags & RC_FLAG_HT_GF)
1638                 rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
1639         }
1640         rates[SSV_DRATE_IDX].count = rc_rate->hw_rate_idx;
1641         if (do_rts_cts & IEEE80211_TX_RC_USE_CTS_PROTECT)
1642         {
1643             rates[SSV_CRATE_IDX].count = 0;
1644         }
1645         else
1646         {
1647             rc_rate = &ssv_rc->rc_table[rc_rate->ctrl_rate_idx];
1648             rates[SSV_CRATE_IDX].count = rc_rate->hw_rate_idx;
1649         }
1650         ret |= 0x1;
1651     }
1652     return ret;
1653 }
1654 #endif
ssv6xxx_rc_hw_reset(struct ssv_softc * sc,int rc_idx,int hwidx)1655 void ssv6xxx_rc_hw_reset(struct ssv_softc *sc, int rc_idx, int hwidx)
1656 {
1657     struct ssv_rate_ctrl *ssv_rc=sc->rc;
1658     struct ssv_sta_rc_info *rc_sta;
1659     u32 rc_hw_reg[] = { ADR_MTX_MIB_WSID0, ADR_MTX_MIB_WSID1 };
1660     BUG_ON(rc_idx >= SSV_RC_MAX_STA);
1661     rc_sta = &ssv_rc->sta_rc_info[rc_idx];
1662     if (hwidx >=0 && hwidx<SSV_NUM_HW_STA) {
1663         rc_sta->rc_wsid = hwidx;
1664         printk("rc_wsid[%d] rc_idx[%d]\n",rc_sta[rc_idx].rc_wsid,rc_idx);
1665         SMAC_REG_WRITE(sc->sh, rc_hw_reg[hwidx], 0x40000000);
1666     }
1667     else
1668     {
1669         rc_sta->rc_wsid = -1;
1670     }
1671 }
1672 #define UPDATE_PHY_INFO_ACK_RATE(_phy_info,_ack_rate_idx) ( _phy_info = (_phy_info&0xfffffc0f)|(_ack_rate_idx<<4))
ssv6xxx_rc_update_bmode_ctrl_rate(struct ssv_softc * sc,int rate_tbl_idx,int ctrl_rate_idx)1673 int ssv6xxx_rc_update_bmode_ctrl_rate(struct ssv_softc *sc, int rate_tbl_idx, int ctrl_rate_idx)
1674 {
1675      u32 temp32;
1676      struct ssv_hw *sh = sc->sh;
1677      u32 addr;
1678     addr = sh->hw_pinfo+rate_tbl_idx*4;
1679     ssv_11bgn_rate_table[rate_tbl_idx].ctrl_rate_idx = ctrl_rate_idx;
1680     SMAC_REG_READ(sh, addr, &temp32);
1681     UPDATE_PHY_INFO_ACK_RATE(temp32, ctrl_rate_idx);
1682     SMAC_REG_WRITE(sh, addr, temp32);
1683     SMAC_REG_CONFIRM(sh, addr, temp32);
1684     return 0;
1685 }
ssv6xxx_rc_update_basic_rate(struct ssv_softc * sc,u32 basic_rates)1686 void ssv6xxx_rc_update_basic_rate(struct ssv_softc *sc, u32 basic_rates)
1687 {
1688     int i;
1689     int rate_idx, pre_rate_idx = 0;
1690     for(i=0;i<4;i++)
1691     {
1692         if(((basic_rates>>i)&0x01))
1693         {
1694             rate_idx = i;
1695             pre_rate_idx = i;
1696         }
1697         else
1698             rate_idx = pre_rate_idx;
1699         ssv6xxx_rc_update_bmode_ctrl_rate(sc, i, rate_idx);
1700         if(i)
1701             ssv6xxx_rc_update_bmode_ctrl_rate(sc, i+3, rate_idx);
1702     }
1703 }
ssv6xxx_rate_control_register(void)1704 int ssv6xxx_rate_control_register(void)
1705 {
1706     return ieee80211_rate_control_register(&ssv_rate_ops);
1707 }
ssv6xxx_rate_control_unregister(void)1708 void ssv6xxx_rate_control_unregister(void)
1709 {
1710     ieee80211_rate_control_unregister(&ssv_rate_ops);
1711 }
ssv6xxx_rc_rx_data_handler(struct ieee80211_hw * hw,struct sk_buff * skb,u32 rate_index)1712 void ssv6xxx_rc_rx_data_handler(struct ieee80211_hw *hw, struct sk_buff *skb, u32 rate_index)
1713 {
1714     struct ssv_softc *sc = hw->priv;
1715     struct ieee80211_sta *sta;
1716     struct ssv_sta_priv_data *ssv_sta_priv;
1717     sta = ssv6xxx_find_sta_by_rx_skb(sc, skb);
1718     if(sta == NULL)
1719     {
1720         return;
1721     }
1722     ssv_sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
1723     ssv_sta_priv->rx_data_rate = rate_index;
1724 }
1725