rate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <brcmu_wifi.h>
  17. #include <brcmu_utils.h>
  18. #include "d11.h"
  19. #include "pub.h"
  20. #include "rate.h"
  21. /*
  22. * Rate info per rate: It tells whether a rate is ofdm or not and its phy_rate
  23. * value
  24. */
  25. const u8 rate_info[BRCM_MAXRATE + 1] = {
  26. /* 0 1 2 3 4 5 6 7 8 9 */
  27. /* 0 */ 0x00, 0x00, 0x0a, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. /* 10 */ 0x00, 0x37, 0x8b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x00,
  29. /* 20 */ 0x00, 0x00, 0x6e, 0x00, 0x8a, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. /* 30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00,
  31. /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x89, 0x00,
  32. /* 50 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. /* 60 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. /* 70 */ 0x00, 0x00, 0x8d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35. /* 80 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. /* 90 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
  37. /* 100 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c
  38. };
  39. /* rates are in units of Kbps */
  40. const struct brcms_mcs_info mcs_table[MCS_TABLE_SIZE] = {
  41. /* MCS 0: SS 1, MOD: BPSK, CR 1/2 */
  42. {6500, 13500, CEIL(6500 * 10, 9), CEIL(13500 * 10, 9), 0x00,
  43. BRCM_RATE_6M},
  44. /* MCS 1: SS 1, MOD: QPSK, CR 1/2 */
  45. {13000, 27000, CEIL(13000 * 10, 9), CEIL(27000 * 10, 9), 0x08,
  46. BRCM_RATE_12M},
  47. /* MCS 2: SS 1, MOD: QPSK, CR 3/4 */
  48. {19500, 40500, CEIL(19500 * 10, 9), CEIL(40500 * 10, 9), 0x0A,
  49. BRCM_RATE_18M},
  50. /* MCS 3: SS 1, MOD: 16QAM, CR 1/2 */
  51. {26000, 54000, CEIL(26000 * 10, 9), CEIL(54000 * 10, 9), 0x10,
  52. BRCM_RATE_24M},
  53. /* MCS 4: SS 1, MOD: 16QAM, CR 3/4 */
  54. {39000, 81000, CEIL(39000 * 10, 9), CEIL(81000 * 10, 9), 0x12,
  55. BRCM_RATE_36M},
  56. /* MCS 5: SS 1, MOD: 64QAM, CR 2/3 */
  57. {52000, 108000, CEIL(52000 * 10, 9), CEIL(108000 * 10, 9), 0x19,
  58. BRCM_RATE_48M},
  59. /* MCS 6: SS 1, MOD: 64QAM, CR 3/4 */
  60. {58500, 121500, CEIL(58500 * 10, 9), CEIL(121500 * 10, 9), 0x1A,
  61. BRCM_RATE_54M},
  62. /* MCS 7: SS 1, MOD: 64QAM, CR 5/6 */
  63. {65000, 135000, CEIL(65000 * 10, 9), CEIL(135000 * 10, 9), 0x1C,
  64. BRCM_RATE_54M},
  65. /* MCS 8: SS 2, MOD: BPSK, CR 1/2 */
  66. {13000, 27000, CEIL(13000 * 10, 9), CEIL(27000 * 10, 9), 0x40,
  67. BRCM_RATE_6M},
  68. /* MCS 9: SS 2, MOD: QPSK, CR 1/2 */
  69. {26000, 54000, CEIL(26000 * 10, 9), CEIL(54000 * 10, 9), 0x48,
  70. BRCM_RATE_12M},
  71. /* MCS 10: SS 2, MOD: QPSK, CR 3/4 */
  72. {39000, 81000, CEIL(39000 * 10, 9), CEIL(81000 * 10, 9), 0x4A,
  73. BRCM_RATE_18M},
  74. /* MCS 11: SS 2, MOD: 16QAM, CR 1/2 */
  75. {52000, 108000, CEIL(52000 * 10, 9), CEIL(108000 * 10, 9), 0x50,
  76. BRCM_RATE_24M},
  77. /* MCS 12: SS 2, MOD: 16QAM, CR 3/4 */
  78. {78000, 162000, CEIL(78000 * 10, 9), CEIL(162000 * 10, 9), 0x52,
  79. BRCM_RATE_36M},
  80. /* MCS 13: SS 2, MOD: 64QAM, CR 2/3 */
  81. {104000, 216000, CEIL(104000 * 10, 9), CEIL(216000 * 10, 9), 0x59,
  82. BRCM_RATE_48M},
  83. /* MCS 14: SS 2, MOD: 64QAM, CR 3/4 */
  84. {117000, 243000, CEIL(117000 * 10, 9), CEIL(243000 * 10, 9), 0x5A,
  85. BRCM_RATE_54M},
  86. /* MCS 15: SS 2, MOD: 64QAM, CR 5/6 */
  87. {130000, 270000, CEIL(130000 * 10, 9), CEIL(270000 * 10, 9), 0x5C,
  88. BRCM_RATE_54M},
  89. /* MCS 16: SS 3, MOD: BPSK, CR 1/2 */
  90. {19500, 40500, CEIL(19500 * 10, 9), CEIL(40500 * 10, 9), 0x80,
  91. BRCM_RATE_6M},
  92. /* MCS 17: SS 3, MOD: QPSK, CR 1/2 */
  93. {39000, 81000, CEIL(39000 * 10, 9), CEIL(81000 * 10, 9), 0x88,
  94. BRCM_RATE_12M},
  95. /* MCS 18: SS 3, MOD: QPSK, CR 3/4 */
  96. {58500, 121500, CEIL(58500 * 10, 9), CEIL(121500 * 10, 9), 0x8A,
  97. BRCM_RATE_18M},
  98. /* MCS 19: SS 3, MOD: 16QAM, CR 1/2 */
  99. {78000, 162000, CEIL(78000 * 10, 9), CEIL(162000 * 10, 9), 0x90,
  100. BRCM_RATE_24M},
  101. /* MCS 20: SS 3, MOD: 16QAM, CR 3/4 */
  102. {117000, 243000, CEIL(117000 * 10, 9), CEIL(243000 * 10, 9), 0x92,
  103. BRCM_RATE_36M},
  104. /* MCS 21: SS 3, MOD: 64QAM, CR 2/3 */
  105. {156000, 324000, CEIL(156000 * 10, 9), CEIL(324000 * 10, 9), 0x99,
  106. BRCM_RATE_48M},
  107. /* MCS 22: SS 3, MOD: 64QAM, CR 3/4 */
  108. {175500, 364500, CEIL(175500 * 10, 9), CEIL(364500 * 10, 9), 0x9A,
  109. BRCM_RATE_54M},
  110. /* MCS 23: SS 3, MOD: 64QAM, CR 5/6 */
  111. {195000, 405000, CEIL(195000 * 10, 9), CEIL(405000 * 10, 9), 0x9B,
  112. BRCM_RATE_54M},
  113. /* MCS 24: SS 4, MOD: BPSK, CR 1/2 */
  114. {26000, 54000, CEIL(26000 * 10, 9), CEIL(54000 * 10, 9), 0xC0,
  115. BRCM_RATE_6M},
  116. /* MCS 25: SS 4, MOD: QPSK, CR 1/2 */
  117. {52000, 108000, CEIL(52000 * 10, 9), CEIL(108000 * 10, 9), 0xC8,
  118. BRCM_RATE_12M},
  119. /* MCS 26: SS 4, MOD: QPSK, CR 3/4 */
  120. {78000, 162000, CEIL(78000 * 10, 9), CEIL(162000 * 10, 9), 0xCA,
  121. BRCM_RATE_18M},
  122. /* MCS 27: SS 4, MOD: 16QAM, CR 1/2 */
  123. {104000, 216000, CEIL(104000 * 10, 9), CEIL(216000 * 10, 9), 0xD0,
  124. BRCM_RATE_24M},
  125. /* MCS 28: SS 4, MOD: 16QAM, CR 3/4 */
  126. {156000, 324000, CEIL(156000 * 10, 9), CEIL(324000 * 10, 9), 0xD2,
  127. BRCM_RATE_36M},
  128. /* MCS 29: SS 4, MOD: 64QAM, CR 2/3 */
  129. {208000, 432000, CEIL(208000 * 10, 9), CEIL(432000 * 10, 9), 0xD9,
  130. BRCM_RATE_48M},
  131. /* MCS 30: SS 4, MOD: 64QAM, CR 3/4 */
  132. {234000, 486000, CEIL(234000 * 10, 9), CEIL(486000 * 10, 9), 0xDA,
  133. BRCM_RATE_54M},
  134. /* MCS 31: SS 4, MOD: 64QAM, CR 5/6 */
  135. {260000, 540000, CEIL(260000 * 10, 9), CEIL(540000 * 10, 9), 0xDB,
  136. BRCM_RATE_54M},
  137. /* MCS 32: SS 1, MOD: BPSK, CR 1/2 */
  138. {0, 6000, 0, CEIL(6000 * 10, 9), 0x00, BRCM_RATE_6M},
  139. };
  140. /*
  141. * phycfg for legacy OFDM frames: code rate, modulation scheme, spatial streams
  142. * Number of spatial streams: always 1 other fields: refer to table 78 of
  143. * section 17.3.2.2 of the original .11a standard
  144. */
  145. struct legacy_phycfg {
  146. u32 rate_ofdm; /* ofdm mac rate */
  147. /* phy ctl byte 3, code rate, modulation type, # of streams */
  148. u8 tx_phy_ctl3;
  149. };
  150. /* Number of legacy_rate_cfg entries in the table */
  151. #define LEGACY_PHYCFG_TABLE_SIZE 12
  152. /*
  153. * In CCK mode LPPHY overloads OFDM Modulation bits with CCK Data Rate
  154. * Eventually MIMOPHY would also be converted to this format
  155. * 0 = 1Mbps; 1 = 2Mbps; 2 = 5.5Mbps; 3 = 11Mbps
  156. */
  157. static const struct
  158. legacy_phycfg legacy_phycfg_table[LEGACY_PHYCFG_TABLE_SIZE] = {
  159. {BRCM_RATE_1M, 0x00}, /* CCK 1Mbps, data rate 0 */
  160. {BRCM_RATE_2M, 0x08}, /* CCK 2Mbps, data rate 1 */
  161. {BRCM_RATE_5M5, 0x10}, /* CCK 5.5Mbps, data rate 2 */
  162. {BRCM_RATE_11M, 0x18}, /* CCK 11Mbps, data rate 3 */
  163. /* OFDM 6Mbps, code rate 1/2, BPSK, 1 spatial stream */
  164. {BRCM_RATE_6M, 0x00},
  165. /* OFDM 9Mbps, code rate 3/4, BPSK, 1 spatial stream */
  166. {BRCM_RATE_9M, 0x02},
  167. /* OFDM 12Mbps, code rate 1/2, QPSK, 1 spatial stream */
  168. {BRCM_RATE_12M, 0x08},
  169. /* OFDM 18Mbps, code rate 3/4, QPSK, 1 spatial stream */
  170. {BRCM_RATE_18M, 0x0A},
  171. /* OFDM 24Mbps, code rate 1/2, 16-QAM, 1 spatial stream */
  172. {BRCM_RATE_24M, 0x10},
  173. /* OFDM 36Mbps, code rate 3/4, 16-QAM, 1 spatial stream */
  174. {BRCM_RATE_36M, 0x12},
  175. /* OFDM 48Mbps, code rate 2/3, 64-QAM, 1 spatial stream */
  176. {BRCM_RATE_48M, 0x19},
  177. /* OFDM 54Mbps, code rate 3/4, 64-QAM, 1 spatial stream */
  178. {BRCM_RATE_54M, 0x1A},
  179. };
  180. /* Hardware rates (also encodes default basic rates) */
  181. const struct brcms_c_rateset cck_ofdm_mimo_rates = {
  182. 12,
  183. /* 1b, 2b, 5.5b, 6, 9, 11b, 12, 18, 24, 36, 48, */
  184. { 0x82, 0x84, 0x8b, 0x0c, 0x12, 0x96, 0x18, 0x24, 0x30, 0x48, 0x60,
  185. /* 54 Mbps */
  186. 0x6c},
  187. 0x00,
  188. { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. 0x00, 0x00, 0x00, 0x00, 0x00}
  190. };
  191. const struct brcms_c_rateset ofdm_mimo_rates = {
  192. 8,
  193. /* 6b, 9, 12b, 18, 24b, 36, 48, 54 Mbps */
  194. { 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c},
  195. 0x00,
  196. { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  197. 0x00, 0x00, 0x00, 0x00, 0x00}
  198. };
  199. /* Default ratesets that include MCS32 for 40BW channels */
  200. static const struct brcms_c_rateset cck_ofdm_40bw_mimo_rates = {
  201. 12,
  202. /* 1b, 2b, 5.5b, 6, 9, 11b, 12, 18, 24, 36, 48 */
  203. { 0x82, 0x84, 0x8b, 0x0c, 0x12, 0x96, 0x18, 0x24, 0x30, 0x48, 0x60,
  204. /* 54 Mbps */
  205. 0x6c},
  206. 0x00,
  207. { 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x00, 0x00, 0x00, 0x00, 0x00}
  209. };
  210. static const struct brcms_c_rateset ofdm_40bw_mimo_rates = {
  211. 8,
  212. /* 6b, 9, 12b, 18, 24b, 36, 48, 54 Mbps */
  213. { 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c},
  214. 0x00,
  215. { 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  216. 0x00, 0x00, 0x00, 0x00, 0x00}
  217. };
  218. const struct brcms_c_rateset cck_ofdm_rates = {
  219. 12,
  220. /* 1b, 2b, 5.5b, 6, 9, 11b, 12, 18, 24, 36, 48,*/
  221. { 0x82, 0x84, 0x8b, 0x0c, 0x12, 0x96, 0x18, 0x24, 0x30, 0x48, 0x60,
  222. /*54 Mbps */
  223. 0x6c},
  224. 0x00,
  225. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  226. 0x00, 0x00, 0x00, 0x00, 0x00}
  227. };
  228. const struct brcms_c_rateset gphy_legacy_rates = {
  229. 4,
  230. /* 1b, 2b, 5.5b, 11b Mbps */
  231. { 0x82, 0x84, 0x8b, 0x96},
  232. 0x00,
  233. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  234. 0x00, 0x00, 0x00, 0x00, 0x00}
  235. };
  236. const struct brcms_c_rateset ofdm_rates = {
  237. 8,
  238. /* 6b, 9, 12b, 18, 24b, 36, 48, 54 Mbps */
  239. { 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c},
  240. 0x00,
  241. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  242. 0x00, 0x00, 0x00, 0x00, 0x00}
  243. };
  244. const struct brcms_c_rateset cck_rates = {
  245. 4,
  246. /* 1b, 2b, 5.5, 11 Mbps */
  247. { 0x82, 0x84, 0x0b, 0x16},
  248. 0x00,
  249. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  250. 0x00, 0x00, 0x00, 0x00, 0x00}
  251. };
  252. /* check if rateset is valid.
  253. * if check_brate is true, rateset without a basic rate is considered NOT valid.
  254. */
  255. static bool brcms_c_rateset_valid(struct brcms_c_rateset *rs, bool check_brate)
  256. {
  257. uint idx;
  258. if (!rs->count)
  259. return false;
  260. if (!check_brate)
  261. return true;
  262. /* error if no basic rates */
  263. for (idx = 0; idx < rs->count; idx++) {
  264. if (rs->rates[idx] & BRCMS_RATE_FLAG)
  265. return true;
  266. }
  267. return false;
  268. }
  269. void brcms_c_rateset_mcs_upd(struct brcms_c_rateset *rs, u8 txstreams)
  270. {
  271. int i;
  272. for (i = txstreams; i < MAX_STREAMS_SUPPORTED; i++)
  273. rs->mcs[i] = 0;
  274. }
  275. /*
  276. * filter based on hardware rateset, and sort filtered rateset with basic
  277. * bit(s) preserved, and check if resulting rateset is valid.
  278. */
  279. bool
  280. brcms_c_rate_hwrs_filter_sort_validate(struct brcms_c_rateset *rs,
  281. const struct brcms_c_rateset *hw_rs,
  282. bool check_brate, u8 txstreams)
  283. {
  284. u8 rateset[BRCM_MAXRATE + 1];
  285. u8 r;
  286. uint count;
  287. uint i;
  288. memset(rateset, 0, sizeof(rateset));
  289. count = rs->count;
  290. for (i = 0; i < count; i++) {
  291. /* mask off "basic rate" bit, BRCMS_RATE_FLAG */
  292. r = (int)rs->rates[i] & BRCMS_RATE_MASK;
  293. if ((r > BRCM_MAXRATE) || (rate_info[r] == 0))
  294. continue;
  295. rateset[r] = rs->rates[i]; /* preserve basic bit! */
  296. }
  297. /* fill out the rates in order, looking at only supported rates */
  298. count = 0;
  299. for (i = 0; i < hw_rs->count; i++) {
  300. r = hw_rs->rates[i] & BRCMS_RATE_MASK;
  301. if (rateset[r])
  302. rs->rates[count++] = rateset[r];
  303. }
  304. rs->count = count;
  305. /* only set the mcs rate bit if the equivalent hw mcs bit is set */
  306. for (i = 0; i < MCSSET_LEN; i++)
  307. rs->mcs[i] = (rs->mcs[i] & hw_rs->mcs[i]);
  308. if (brcms_c_rateset_valid(rs, check_brate))
  309. return true;
  310. else
  311. return false;
  312. }
  313. /* calculate the rate of a rx'd frame and return it as a ratespec */
  314. u32 brcms_c_compute_rspec(struct d11rxhdr *rxh, u8 *plcp)
  315. {
  316. int phy_type;
  317. u32 rspec = PHY_TXC1_BW_20MHZ << RSPEC_BW_SHIFT;
  318. phy_type =
  319. ((rxh->RxChan & RXS_CHAN_PHYTYPE_MASK) >> RXS_CHAN_PHYTYPE_SHIFT);
  320. if ((phy_type == PHY_TYPE_N) || (phy_type == PHY_TYPE_SSN) ||
  321. (phy_type == PHY_TYPE_LCN) || (phy_type == PHY_TYPE_HT)) {
  322. switch (rxh->PhyRxStatus_0 & PRXS0_FT_MASK) {
  323. case PRXS0_CCK:
  324. rspec =
  325. cck_phy2mac_rate(
  326. ((struct cck_phy_hdr *) plcp)->signal);
  327. break;
  328. case PRXS0_OFDM:
  329. rspec =
  330. ofdm_phy2mac_rate(
  331. ((struct ofdm_phy_hdr *) plcp)->rlpt[0]);
  332. break;
  333. case PRXS0_PREN:
  334. rspec = (plcp[0] & MIMO_PLCP_MCS_MASK) | RSPEC_MIMORATE;
  335. if (plcp[0] & MIMO_PLCP_40MHZ) {
  336. /* indicate rspec is for 40 MHz mode */
  337. rspec &= ~RSPEC_BW_MASK;
  338. rspec |= (PHY_TXC1_BW_40MHZ << RSPEC_BW_SHIFT);
  339. }
  340. break;
  341. case PRXS0_STDN:
  342. /* fallthru */
  343. default:
  344. /* not supported, error condition */
  345. break;
  346. }
  347. if (plcp3_issgi(plcp[3]))
  348. rspec |= RSPEC_SHORT_GI;
  349. } else
  350. if ((phy_type == PHY_TYPE_A) || (rxh->PhyRxStatus_0 & PRXS0_OFDM))
  351. rspec = ofdm_phy2mac_rate(
  352. ((struct ofdm_phy_hdr *) plcp)->rlpt[0]);
  353. else
  354. rspec = cck_phy2mac_rate(
  355. ((struct cck_phy_hdr *) plcp)->signal);
  356. return rspec;
  357. }
  358. /* copy rateset src to dst as-is (no masking or sorting) */
  359. void brcms_c_rateset_copy(const struct brcms_c_rateset *src,
  360. struct brcms_c_rateset *dst)
  361. {
  362. memcpy(dst, src, sizeof(struct brcms_c_rateset));
  363. }
  364. /*
  365. * Copy and selectively filter one rateset to another.
  366. * 'basic_only' means only copy basic rates.
  367. * 'rates' indicates cck (11b) and ofdm rates combinations.
  368. * - 0: cck and ofdm
  369. * - 1: cck only
  370. * - 2: ofdm only
  371. * 'xmask' is the copy mask (typically 0x7f or 0xff).
  372. */
  373. void
  374. brcms_c_rateset_filter(struct brcms_c_rateset *src, struct brcms_c_rateset *dst,
  375. bool basic_only, u8 rates, uint xmask, bool mcsallow)
  376. {
  377. uint i;
  378. uint r;
  379. uint count;
  380. count = 0;
  381. for (i = 0; i < src->count; i++) {
  382. r = src->rates[i];
  383. if (basic_only && !(r & BRCMS_RATE_FLAG))
  384. continue;
  385. if (rates == BRCMS_RATES_CCK &&
  386. is_ofdm_rate((r & BRCMS_RATE_MASK)))
  387. continue;
  388. if (rates == BRCMS_RATES_OFDM &&
  389. is_cck_rate((r & BRCMS_RATE_MASK)))
  390. continue;
  391. dst->rates[count++] = r & xmask;
  392. }
  393. dst->count = count;
  394. dst->htphy_membership = src->htphy_membership;
  395. if (mcsallow && rates != BRCMS_RATES_CCK)
  396. memcpy(&dst->mcs[0], &src->mcs[0], MCSSET_LEN);
  397. else
  398. brcms_c_rateset_mcs_clear(dst);
  399. }
  400. /* select rateset for a given phy_type and bandtype and filter it, sort it
  401. * and fill rs_tgt with result
  402. */
  403. void
  404. brcms_c_rateset_default(struct brcms_c_rateset *rs_tgt,
  405. const struct brcms_c_rateset *rs_hw,
  406. uint phy_type, int bandtype, bool cck_only,
  407. uint rate_mask, bool mcsallow, u8 bw, u8 txstreams)
  408. {
  409. const struct brcms_c_rateset *rs_dflt;
  410. struct brcms_c_rateset rs_sel;
  411. if ((PHYTYPE_IS(phy_type, PHY_TYPE_HT)) ||
  412. (PHYTYPE_IS(phy_type, PHY_TYPE_N)) ||
  413. (PHYTYPE_IS(phy_type, PHY_TYPE_LCN)) ||
  414. (PHYTYPE_IS(phy_type, PHY_TYPE_SSN))) {
  415. if (bandtype == BRCM_BAND_5G)
  416. rs_dflt = (bw == BRCMS_20_MHZ ?
  417. &ofdm_mimo_rates : &ofdm_40bw_mimo_rates);
  418. else
  419. rs_dflt = (bw == BRCMS_20_MHZ ?
  420. &cck_ofdm_mimo_rates :
  421. &cck_ofdm_40bw_mimo_rates);
  422. } else if (PHYTYPE_IS(phy_type, PHY_TYPE_LP)) {
  423. rs_dflt = (bandtype == BRCM_BAND_5G) ?
  424. &ofdm_rates : &cck_ofdm_rates;
  425. } else if (PHYTYPE_IS(phy_type, PHY_TYPE_A)) {
  426. rs_dflt = &ofdm_rates;
  427. } else if (PHYTYPE_IS(phy_type, PHY_TYPE_G)) {
  428. rs_dflt = &cck_ofdm_rates;
  429. } else {
  430. /* should not happen, error condition */
  431. rs_dflt = &cck_rates; /* force cck */
  432. }
  433. /* if hw rateset is not supplied, assign selected rateset to it */
  434. if (!rs_hw)
  435. rs_hw = rs_dflt;
  436. brcms_c_rateset_copy(rs_dflt, &rs_sel);
  437. brcms_c_rateset_mcs_upd(&rs_sel, txstreams);
  438. brcms_c_rateset_filter(&rs_sel, rs_tgt, false,
  439. cck_only ? BRCMS_RATES_CCK : BRCMS_RATES_CCK_OFDM,
  440. rate_mask, mcsallow);
  441. brcms_c_rate_hwrs_filter_sort_validate(rs_tgt, rs_hw, false,
  442. mcsallow ? txstreams : 1);
  443. }
  444. s16 brcms_c_rate_legacy_phyctl(uint rate)
  445. {
  446. uint i;
  447. for (i = 0; i < LEGACY_PHYCFG_TABLE_SIZE; i++)
  448. if (rate == legacy_phycfg_table[i].rate_ofdm)
  449. return legacy_phycfg_table[i].tx_phy_ctl3;
  450. return -1;
  451. }
  452. void brcms_c_rateset_mcs_clear(struct brcms_c_rateset *rateset)
  453. {
  454. uint i;
  455. for (i = 0; i < MCSSET_LEN; i++)
  456. rateset->mcs[i] = 0;
  457. }
  458. void brcms_c_rateset_mcs_build(struct brcms_c_rateset *rateset, u8 txstreams)
  459. {
  460. memcpy(&rateset->mcs[0], &cck_ofdm_mimo_rates.mcs[0], MCSSET_LEN);
  461. brcms_c_rateset_mcs_upd(rateset, txstreams);
  462. }
  463. /* Based on bandwidth passed, allow/disallow MCS 32 in the rateset */
  464. void brcms_c_rateset_bw_mcs_filter(struct brcms_c_rateset *rateset, u8 bw)
  465. {
  466. if (bw == BRCMS_40_MHZ)
  467. setbit(rateset->mcs, 32);
  468. else
  469. clrbit(rateset->mcs, 32);
  470. }