xmit.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. Broadcom B43 wireless driver
  3. Transmission (TX/RX) related functions.
  4. Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
  5. Copyright (C) 2005 Stefano Brivio <stefano.brivio@polimi.it>
  6. Copyright (C) 2005, 2006 Michael Buesch <mb@bu3sch.de>
  7. Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
  8. Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  20. Boston, MA 02110-1301, USA.
  21. */
  22. #include "xmit.h"
  23. #include "phy_common.h"
  24. #include "dma.h"
  25. #include "pio.h"
  26. static const struct b43_tx_legacy_rate_phy_ctl_entry b43_tx_legacy_rate_phy_ctl[] = {
  27. { B43_CCK_RATE_1MB, 0x0, 0x0 },
  28. { B43_CCK_RATE_2MB, 0x0, 0x1 },
  29. { B43_CCK_RATE_5MB, 0x0, 0x2 },
  30. { B43_CCK_RATE_11MB, 0x0, 0x3 },
  31. { B43_OFDM_RATE_6MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_BPSK },
  32. { B43_OFDM_RATE_9MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_BPSK },
  33. { B43_OFDM_RATE_12MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_QPSK },
  34. { B43_OFDM_RATE_18MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QPSK },
  35. { B43_OFDM_RATE_24MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_QAM16 },
  36. { B43_OFDM_RATE_36MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QAM16 },
  37. { B43_OFDM_RATE_48MB, B43_TXH_PHY1_CRATE_2_3, B43_TXH_PHY1_MODUL_QAM64 },
  38. { B43_OFDM_RATE_54MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QAM64 },
  39. };
  40. static const struct b43_tx_legacy_rate_phy_ctl_entry *
  41. b43_tx_legacy_rate_phy_ctl_ent(u8 bitrate)
  42. {
  43. const struct b43_tx_legacy_rate_phy_ctl_entry *e;
  44. unsigned int i;
  45. for (i = 0; i < ARRAY_SIZE(b43_tx_legacy_rate_phy_ctl); i++) {
  46. e = &(b43_tx_legacy_rate_phy_ctl[i]);
  47. if (e->bitrate == bitrate)
  48. return e;
  49. }
  50. B43_WARN_ON(1);
  51. return NULL;
  52. }
  53. /* Extract the bitrate index out of a CCK PLCP header. */
  54. static int b43_plcp_get_bitrate_idx_cck(struct b43_plcp_hdr6 *plcp)
  55. {
  56. switch (plcp->raw[0]) {
  57. case 0x0A:
  58. return 0;
  59. case 0x14:
  60. return 1;
  61. case 0x37:
  62. return 2;
  63. case 0x6E:
  64. return 3;
  65. }
  66. return -1;
  67. }
  68. /* Extract the bitrate index out of an OFDM PLCP header. */
  69. static int b43_plcp_get_bitrate_idx_ofdm(struct b43_plcp_hdr6 *plcp, bool aphy)
  70. {
  71. int base = aphy ? 0 : 4;
  72. switch (plcp->raw[0] & 0xF) {
  73. case 0xB:
  74. return base + 0;
  75. case 0xF:
  76. return base + 1;
  77. case 0xA:
  78. return base + 2;
  79. case 0xE:
  80. return base + 3;
  81. case 0x9:
  82. return base + 4;
  83. case 0xD:
  84. return base + 5;
  85. case 0x8:
  86. return base + 6;
  87. case 0xC:
  88. return base + 7;
  89. }
  90. return -1;
  91. }
  92. u8 b43_plcp_get_ratecode_cck(const u8 bitrate)
  93. {
  94. switch (bitrate) {
  95. case B43_CCK_RATE_1MB:
  96. return 0x0A;
  97. case B43_CCK_RATE_2MB:
  98. return 0x14;
  99. case B43_CCK_RATE_5MB:
  100. return 0x37;
  101. case B43_CCK_RATE_11MB:
  102. return 0x6E;
  103. }
  104. B43_WARN_ON(1);
  105. return 0;
  106. }
  107. u8 b43_plcp_get_ratecode_ofdm(const u8 bitrate)
  108. {
  109. switch (bitrate) {
  110. case B43_OFDM_RATE_6MB:
  111. return 0xB;
  112. case B43_OFDM_RATE_9MB:
  113. return 0xF;
  114. case B43_OFDM_RATE_12MB:
  115. return 0xA;
  116. case B43_OFDM_RATE_18MB:
  117. return 0xE;
  118. case B43_OFDM_RATE_24MB:
  119. return 0x9;
  120. case B43_OFDM_RATE_36MB:
  121. return 0xD;
  122. case B43_OFDM_RATE_48MB:
  123. return 0x8;
  124. case B43_OFDM_RATE_54MB:
  125. return 0xC;
  126. }
  127. B43_WARN_ON(1);
  128. return 0;
  129. }
  130. void b43_generate_plcp_hdr(struct b43_plcp_hdr4 *plcp,
  131. const u16 octets, const u8 bitrate)
  132. {
  133. __u8 *raw = plcp->raw;
  134. if (b43_is_ofdm_rate(bitrate)) {
  135. u32 d;
  136. d = b43_plcp_get_ratecode_ofdm(bitrate);
  137. B43_WARN_ON(octets & 0xF000);
  138. d |= (octets << 5);
  139. plcp->data = cpu_to_le32(d);
  140. } else {
  141. u32 plen;
  142. plen = octets * 16 / bitrate;
  143. if ((octets * 16 % bitrate) > 0) {
  144. plen++;
  145. if ((bitrate == B43_CCK_RATE_11MB)
  146. && ((octets * 8 % 11) < 4)) {
  147. raw[1] = 0x84;
  148. } else
  149. raw[1] = 0x04;
  150. } else
  151. raw[1] = 0x04;
  152. plcp->data |= cpu_to_le32(plen << 16);
  153. raw[0] = b43_plcp_get_ratecode_cck(bitrate);
  154. }
  155. }
  156. static u16 b43_generate_tx_phy_ctl1(struct b43_wldev *dev, u8 bitrate)
  157. {
  158. const struct b43_phy *phy = &dev->phy;
  159. const struct b43_tx_legacy_rate_phy_ctl_entry *e;
  160. u16 control = 0;
  161. u16 bw;
  162. if (phy->type == B43_PHYTYPE_LP)
  163. bw = B43_TXH_PHY1_BW_20;
  164. else /* FIXME */
  165. bw = B43_TXH_PHY1_BW_20;
  166. if (0) { /* FIXME: MIMO */
  167. } else if (b43_is_cck_rate(bitrate) && phy->type != B43_PHYTYPE_LP) {
  168. control = bw;
  169. } else {
  170. control = bw;
  171. e = b43_tx_legacy_rate_phy_ctl_ent(bitrate);
  172. if (e) {
  173. control |= e->coding_rate;
  174. control |= e->modulation;
  175. }
  176. control |= B43_TXH_PHY1_MODE_SISO;
  177. }
  178. return control;
  179. }
  180. static u8 b43_calc_fallback_rate(u8 bitrate)
  181. {
  182. switch (bitrate) {
  183. case B43_CCK_RATE_1MB:
  184. return B43_CCK_RATE_1MB;
  185. case B43_CCK_RATE_2MB:
  186. return B43_CCK_RATE_1MB;
  187. case B43_CCK_RATE_5MB:
  188. return B43_CCK_RATE_2MB;
  189. case B43_CCK_RATE_11MB:
  190. return B43_CCK_RATE_5MB;
  191. case B43_OFDM_RATE_6MB:
  192. return B43_CCK_RATE_5MB;
  193. case B43_OFDM_RATE_9MB:
  194. return B43_OFDM_RATE_6MB;
  195. case B43_OFDM_RATE_12MB:
  196. return B43_OFDM_RATE_9MB;
  197. case B43_OFDM_RATE_18MB:
  198. return B43_OFDM_RATE_12MB;
  199. case B43_OFDM_RATE_24MB:
  200. return B43_OFDM_RATE_18MB;
  201. case B43_OFDM_RATE_36MB:
  202. return B43_OFDM_RATE_24MB;
  203. case B43_OFDM_RATE_48MB:
  204. return B43_OFDM_RATE_36MB;
  205. case B43_OFDM_RATE_54MB:
  206. return B43_OFDM_RATE_48MB;
  207. }
  208. B43_WARN_ON(1);
  209. return 0;
  210. }
  211. /* Generate a TX data header. */
  212. int b43_generate_txhdr(struct b43_wldev *dev,
  213. u8 *_txhdr,
  214. struct sk_buff *skb_frag,
  215. struct ieee80211_tx_info *info,
  216. u16 cookie)
  217. {
  218. const unsigned char *fragment_data = skb_frag->data;
  219. unsigned int fragment_len = skb_frag->len;
  220. struct b43_txhdr *txhdr = (struct b43_txhdr *)_txhdr;
  221. const struct b43_phy *phy = &dev->phy;
  222. const struct ieee80211_hdr *wlhdr =
  223. (const struct ieee80211_hdr *)fragment_data;
  224. int use_encryption = !!info->control.hw_key;
  225. __le16 fctl = wlhdr->frame_control;
  226. struct ieee80211_rate *fbrate;
  227. u8 rate, rate_fb;
  228. int rate_ofdm, rate_fb_ofdm;
  229. unsigned int plcp_fragment_len;
  230. u32 mac_ctl = 0;
  231. u16 phy_ctl = 0;
  232. u8 extra_ft = 0;
  233. struct ieee80211_rate *txrate;
  234. struct ieee80211_tx_rate *rates;
  235. memset(txhdr, 0, sizeof(*txhdr));
  236. txrate = ieee80211_get_tx_rate(dev->wl->hw, info);
  237. rate = txrate ? txrate->hw_value : B43_CCK_RATE_1MB;
  238. rate_ofdm = b43_is_ofdm_rate(rate);
  239. fbrate = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : txrate;
  240. rate_fb = fbrate->hw_value;
  241. rate_fb_ofdm = b43_is_ofdm_rate(rate_fb);
  242. if (rate_ofdm)
  243. txhdr->phy_rate = b43_plcp_get_ratecode_ofdm(rate);
  244. else
  245. txhdr->phy_rate = b43_plcp_get_ratecode_cck(rate);
  246. txhdr->mac_frame_ctl = wlhdr->frame_control;
  247. memcpy(txhdr->tx_receiver, wlhdr->addr1, 6);
  248. /* Calculate duration for fallback rate */
  249. if ((rate_fb == rate) ||
  250. (wlhdr->duration_id & cpu_to_le16(0x8000)) ||
  251. (wlhdr->duration_id == cpu_to_le16(0))) {
  252. /* If the fallback rate equals the normal rate or the
  253. * dur_id field contains an AID, CFP magic or 0,
  254. * use the original dur_id field. */
  255. txhdr->dur_fb = wlhdr->duration_id;
  256. } else {
  257. txhdr->dur_fb = ieee80211_generic_frame_duration(
  258. dev->wl->hw, info->control.vif, fragment_len, fbrate);
  259. }
  260. plcp_fragment_len = fragment_len + FCS_LEN;
  261. if (use_encryption) {
  262. u8 key_idx = info->control.hw_key->hw_key_idx;
  263. struct b43_key *key;
  264. int wlhdr_len;
  265. size_t iv_len;
  266. B43_WARN_ON(key_idx >= ARRAY_SIZE(dev->key));
  267. key = &(dev->key[key_idx]);
  268. if (unlikely(!key->keyconf)) {
  269. /* This key is invalid. This might only happen
  270. * in a short timeframe after machine resume before
  271. * we were able to reconfigure keys.
  272. * Drop this packet completely. Do not transmit it
  273. * unencrypted to avoid leaking information. */
  274. return -ENOKEY;
  275. }
  276. /* Hardware appends ICV. */
  277. plcp_fragment_len += info->control.hw_key->icv_len;
  278. key_idx = b43_kidx_to_fw(dev, key_idx);
  279. mac_ctl |= (key_idx << B43_TXH_MAC_KEYIDX_SHIFT) &
  280. B43_TXH_MAC_KEYIDX;
  281. mac_ctl |= (key->algorithm << B43_TXH_MAC_KEYALG_SHIFT) &
  282. B43_TXH_MAC_KEYALG;
  283. wlhdr_len = ieee80211_hdrlen(fctl);
  284. if (key->algorithm == B43_SEC_ALGO_TKIP) {
  285. u16 phase1key[5];
  286. int i;
  287. /* we give the phase1key and iv16 here, the key is stored in
  288. * shm. With that the hardware can do phase 2 and encryption.
  289. */
  290. ieee80211_get_tkip_key(info->control.hw_key, skb_frag,
  291. IEEE80211_TKIP_P1_KEY, (u8*)phase1key);
  292. /* phase1key is in host endian. Copy to little-endian txhdr->iv. */
  293. for (i = 0; i < 5; i++) {
  294. txhdr->iv[i * 2 + 0] = phase1key[i];
  295. txhdr->iv[i * 2 + 1] = phase1key[i] >> 8;
  296. }
  297. /* iv16 */
  298. memcpy(txhdr->iv + 10, ((u8 *) wlhdr) + wlhdr_len, 3);
  299. } else {
  300. iv_len = min((size_t) info->control.hw_key->iv_len,
  301. ARRAY_SIZE(txhdr->iv));
  302. memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
  303. }
  304. }
  305. if (b43_is_old_txhdr_format(dev)) {
  306. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->old_format.plcp),
  307. plcp_fragment_len, rate);
  308. } else {
  309. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->new_format.plcp),
  310. plcp_fragment_len, rate);
  311. }
  312. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
  313. plcp_fragment_len, rate_fb);
  314. /* Extra Frame Types */
  315. if (rate_fb_ofdm)
  316. extra_ft |= B43_TXH_EFT_FB_OFDM;
  317. else
  318. extra_ft |= B43_TXH_EFT_FB_CCK;
  319. /* Set channel radio code. Note that the micrcode ORs 0x100 to
  320. * this value before comparing it to the value in SHM, if this
  321. * is a 5Ghz packet.
  322. */
  323. txhdr->chan_radio_code = phy->channel;
  324. /* PHY TX Control word */
  325. if (rate_ofdm)
  326. phy_ctl |= B43_TXH_PHY_ENC_OFDM;
  327. else
  328. phy_ctl |= B43_TXH_PHY_ENC_CCK;
  329. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  330. phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
  331. switch (b43_ieee80211_antenna_sanitize(dev, info->antenna_sel_tx)) {
  332. case 0: /* Default */
  333. phy_ctl |= B43_TXH_PHY_ANT01AUTO;
  334. break;
  335. case 1: /* Antenna 0 */
  336. phy_ctl |= B43_TXH_PHY_ANT0;
  337. break;
  338. case 2: /* Antenna 1 */
  339. phy_ctl |= B43_TXH_PHY_ANT1;
  340. break;
  341. case 3: /* Antenna 2 */
  342. phy_ctl |= B43_TXH_PHY_ANT2;
  343. break;
  344. case 4: /* Antenna 3 */
  345. phy_ctl |= B43_TXH_PHY_ANT3;
  346. break;
  347. default:
  348. B43_WARN_ON(1);
  349. }
  350. rates = info->control.rates;
  351. /* MAC control */
  352. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  353. mac_ctl |= B43_TXH_MAC_ACK;
  354. /* use hardware sequence counter as the non-TID counter */
  355. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  356. mac_ctl |= B43_TXH_MAC_HWSEQ;
  357. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  358. mac_ctl |= B43_TXH_MAC_STMSDU;
  359. if (phy->type == B43_PHYTYPE_A)
  360. mac_ctl |= B43_TXH_MAC_5GHZ;
  361. /* Overwrite rates[0].count to make the retry calculation
  362. * in the tx status easier. need the actual retry limit to
  363. * detect whether the fallback rate was used.
  364. */
  365. if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  366. (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
  367. rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
  368. mac_ctl |= B43_TXH_MAC_LONGFRAME;
  369. } else {
  370. rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
  371. }
  372. /* Generate the RTS or CTS-to-self frame */
  373. if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  374. (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
  375. unsigned int len;
  376. struct ieee80211_hdr *hdr;
  377. int rts_rate, rts_rate_fb;
  378. int rts_rate_ofdm, rts_rate_fb_ofdm;
  379. struct b43_plcp_hdr6 *plcp;
  380. struct ieee80211_rate *rts_cts_rate;
  381. rts_cts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info);
  382. rts_rate = rts_cts_rate ? rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
  383. rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
  384. rts_rate_fb = b43_calc_fallback_rate(rts_rate);
  385. rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
  386. if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  387. struct ieee80211_cts *cts;
  388. if (b43_is_old_txhdr_format(dev)) {
  389. cts = (struct ieee80211_cts *)
  390. (txhdr->old_format.rts_frame);
  391. } else {
  392. cts = (struct ieee80211_cts *)
  393. (txhdr->new_format.rts_frame);
  394. }
  395. ieee80211_ctstoself_get(dev->wl->hw, info->control.vif,
  396. fragment_data, fragment_len,
  397. info, cts);
  398. mac_ctl |= B43_TXH_MAC_SENDCTS;
  399. len = sizeof(struct ieee80211_cts);
  400. } else {
  401. struct ieee80211_rts *rts;
  402. if (b43_is_old_txhdr_format(dev)) {
  403. rts = (struct ieee80211_rts *)
  404. (txhdr->old_format.rts_frame);
  405. } else {
  406. rts = (struct ieee80211_rts *)
  407. (txhdr->new_format.rts_frame);
  408. }
  409. ieee80211_rts_get(dev->wl->hw, info->control.vif,
  410. fragment_data, fragment_len,
  411. info, rts);
  412. mac_ctl |= B43_TXH_MAC_SENDRTS;
  413. len = sizeof(struct ieee80211_rts);
  414. }
  415. len += FCS_LEN;
  416. /* Generate the PLCP headers for the RTS/CTS frame */
  417. if (b43_is_old_txhdr_format(dev))
  418. plcp = &txhdr->old_format.rts_plcp;
  419. else
  420. plcp = &txhdr->new_format.rts_plcp;
  421. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
  422. len, rts_rate);
  423. plcp = &txhdr->rts_plcp_fb;
  424. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
  425. len, rts_rate_fb);
  426. if (b43_is_old_txhdr_format(dev)) {
  427. hdr = (struct ieee80211_hdr *)
  428. (&txhdr->old_format.rts_frame);
  429. } else {
  430. hdr = (struct ieee80211_hdr *)
  431. (&txhdr->new_format.rts_frame);
  432. }
  433. txhdr->rts_dur_fb = hdr->duration_id;
  434. if (rts_rate_ofdm) {
  435. extra_ft |= B43_TXH_EFT_RTS_OFDM;
  436. txhdr->phy_rate_rts =
  437. b43_plcp_get_ratecode_ofdm(rts_rate);
  438. } else {
  439. extra_ft |= B43_TXH_EFT_RTS_CCK;
  440. txhdr->phy_rate_rts =
  441. b43_plcp_get_ratecode_cck(rts_rate);
  442. }
  443. if (rts_rate_fb_ofdm)
  444. extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
  445. else
  446. extra_ft |= B43_TXH_EFT_RTSFB_CCK;
  447. if (rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS &&
  448. phy->type == B43_PHYTYPE_N) {
  449. txhdr->phy_ctl1_rts = cpu_to_le16(
  450. b43_generate_tx_phy_ctl1(dev, rts_rate));
  451. txhdr->phy_ctl1_rts_fb = cpu_to_le16(
  452. b43_generate_tx_phy_ctl1(dev, rts_rate_fb));
  453. }
  454. }
  455. /* Magic cookie */
  456. if (b43_is_old_txhdr_format(dev))
  457. txhdr->old_format.cookie = cpu_to_le16(cookie);
  458. else
  459. txhdr->new_format.cookie = cpu_to_le16(cookie);
  460. if (phy->type == B43_PHYTYPE_N) {
  461. txhdr->phy_ctl1 =
  462. cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate));
  463. txhdr->phy_ctl1_fb =
  464. cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate_fb));
  465. }
  466. /* Apply the bitfields */
  467. txhdr->mac_ctl = cpu_to_le32(mac_ctl);
  468. txhdr->phy_ctl = cpu_to_le16(phy_ctl);
  469. txhdr->extra_ft = extra_ft;
  470. return 0;
  471. }
  472. static s8 b43_rssi_postprocess(struct b43_wldev *dev,
  473. u8 in_rssi, int ofdm,
  474. int adjust_2053, int adjust_2050)
  475. {
  476. struct b43_phy *phy = &dev->phy;
  477. struct b43_phy_g *gphy = phy->g;
  478. s32 tmp;
  479. switch (phy->radio_ver) {
  480. case 0x2050:
  481. if (ofdm) {
  482. tmp = in_rssi;
  483. if (tmp > 127)
  484. tmp -= 256;
  485. tmp *= 73;
  486. tmp /= 64;
  487. if (adjust_2050)
  488. tmp += 25;
  489. else
  490. tmp -= 3;
  491. } else {
  492. if (dev->dev->bus->sprom.
  493. boardflags_lo & B43_BFL_RSSI) {
  494. if (in_rssi > 63)
  495. in_rssi = 63;
  496. B43_WARN_ON(phy->type != B43_PHYTYPE_G);
  497. tmp = gphy->nrssi_lt[in_rssi];
  498. tmp = 31 - tmp;
  499. tmp *= -131;
  500. tmp /= 128;
  501. tmp -= 57;
  502. } else {
  503. tmp = in_rssi;
  504. tmp = 31 - tmp;
  505. tmp *= -149;
  506. tmp /= 128;
  507. tmp -= 68;
  508. }
  509. if (phy->type == B43_PHYTYPE_G && adjust_2050)
  510. tmp += 25;
  511. }
  512. break;
  513. case 0x2060:
  514. if (in_rssi > 127)
  515. tmp = in_rssi - 256;
  516. else
  517. tmp = in_rssi;
  518. break;
  519. default:
  520. tmp = in_rssi;
  521. tmp -= 11;
  522. tmp *= 103;
  523. tmp /= 64;
  524. if (adjust_2053)
  525. tmp -= 109;
  526. else
  527. tmp -= 83;
  528. }
  529. return (s8) tmp;
  530. }
  531. //TODO
  532. #if 0
  533. static s8 b43_rssinoise_postprocess(struct b43_wldev *dev, u8 in_rssi)
  534. {
  535. struct b43_phy *phy = &dev->phy;
  536. s8 ret;
  537. if (phy->type == B43_PHYTYPE_A) {
  538. //TODO: Incomplete specs.
  539. ret = 0;
  540. } else
  541. ret = b43_rssi_postprocess(dev, in_rssi, 0, 1, 1);
  542. return ret;
  543. }
  544. #endif
  545. void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
  546. {
  547. struct ieee80211_rx_status status;
  548. struct b43_plcp_hdr6 *plcp;
  549. struct ieee80211_hdr *wlhdr;
  550. const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
  551. __le16 fctl;
  552. u16 phystat0, phystat3, chanstat, mactime;
  553. u32 macstat;
  554. u16 chanid;
  555. u16 phytype;
  556. int padding;
  557. memset(&status, 0, sizeof(status));
  558. /* Get metadata about the frame from the header. */
  559. phystat0 = le16_to_cpu(rxhdr->phy_status0);
  560. phystat3 = le16_to_cpu(rxhdr->phy_status3);
  561. macstat = le32_to_cpu(rxhdr->mac_status);
  562. mactime = le16_to_cpu(rxhdr->mac_time);
  563. chanstat = le16_to_cpu(rxhdr->channel);
  564. phytype = chanstat & B43_RX_CHAN_PHYTYPE;
  565. if (unlikely(macstat & B43_RX_MAC_FCSERR)) {
  566. dev->wl->ieee_stats.dot11FCSErrorCount++;
  567. status.flag |= RX_FLAG_FAILED_FCS_CRC;
  568. }
  569. if (unlikely(phystat0 & (B43_RX_PHYST0_PLCPHCF | B43_RX_PHYST0_PLCPFV)))
  570. status.flag |= RX_FLAG_FAILED_PLCP_CRC;
  571. if (phystat0 & B43_RX_PHYST0_SHORTPRMBL)
  572. status.flag |= RX_FLAG_SHORTPRE;
  573. if (macstat & B43_RX_MAC_DECERR) {
  574. /* Decryption with the given key failed.
  575. * Drop the packet. We also won't be able to decrypt it with
  576. * the key in software. */
  577. goto drop;
  578. }
  579. /* Skip PLCP and padding */
  580. padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
  581. if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
  582. b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
  583. goto drop;
  584. }
  585. plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
  586. skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
  587. /* The skb contains the Wireless Header + payload data now */
  588. if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
  589. b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
  590. goto drop;
  591. }
  592. wlhdr = (struct ieee80211_hdr *)(skb->data);
  593. fctl = wlhdr->frame_control;
  594. if (macstat & B43_RX_MAC_DEC) {
  595. unsigned int keyidx;
  596. int wlhdr_len;
  597. keyidx = ((macstat & B43_RX_MAC_KEYIDX)
  598. >> B43_RX_MAC_KEYIDX_SHIFT);
  599. /* We must adjust the key index here. We want the "physical"
  600. * key index, but the ucode passed it slightly different.
  601. */
  602. keyidx = b43_kidx_to_raw(dev, keyidx);
  603. B43_WARN_ON(keyidx >= ARRAY_SIZE(dev->key));
  604. if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
  605. wlhdr_len = ieee80211_hdrlen(fctl);
  606. if (unlikely(skb->len < (wlhdr_len + 3))) {
  607. b43dbg(dev->wl,
  608. "RX: Packet size underrun (3)\n");
  609. goto drop;
  610. }
  611. status.flag |= RX_FLAG_DECRYPTED;
  612. }
  613. }
  614. /* Link quality statistics */
  615. if ((chanstat & B43_RX_CHAN_PHYTYPE) == B43_PHYTYPE_N) {
  616. // s8 rssi = max(rxhdr->power0, rxhdr->power1);
  617. //TODO: Find out what the rssi value is (dBm or percentage?)
  618. // and also find out what the maximum possible value is.
  619. // Fill status.ssi and status.signal fields.
  620. } else {
  621. status.signal = b43_rssi_postprocess(dev, rxhdr->jssi,
  622. (phystat0 & B43_RX_PHYST0_OFDM),
  623. (phystat0 & B43_RX_PHYST0_GAINCTL),
  624. (phystat3 & B43_RX_PHYST3_TRSTATE));
  625. }
  626. if (phystat0 & B43_RX_PHYST0_OFDM)
  627. status.rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
  628. phytype == B43_PHYTYPE_A);
  629. else
  630. status.rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
  631. if (unlikely(status.rate_idx == -1)) {
  632. /* PLCP seems to be corrupted.
  633. * Drop the frame, if we are not interested in corrupted frames. */
  634. if (!(dev->wl->filter_flags & FIF_PLCPFAIL))
  635. goto drop;
  636. }
  637. status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
  638. /*
  639. * All frames on monitor interfaces and beacons always need a full
  640. * 64-bit timestamp. Monitor interfaces need it for diagnostic
  641. * purposes and beacons for IBSS merging.
  642. * This code assumes we get to process the packet within 16 bits
  643. * of timestamp, i.e. about 65 milliseconds after the PHY received
  644. * the first symbol.
  645. */
  646. if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
  647. u16 low_mactime_now;
  648. b43_tsf_read(dev, &status.mactime);
  649. low_mactime_now = status.mactime;
  650. status.mactime = status.mactime & ~0xFFFFULL;
  651. status.mactime += mactime;
  652. if (low_mactime_now <= mactime)
  653. status.mactime -= 0x10000;
  654. status.flag |= RX_FLAG_MACTIME_MPDU;
  655. }
  656. chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
  657. switch (chanstat & B43_RX_CHAN_PHYTYPE) {
  658. case B43_PHYTYPE_A:
  659. status.band = IEEE80211_BAND_5GHZ;
  660. B43_WARN_ON(1);
  661. /* FIXME: We don't really know which value the "chanid" contains.
  662. * So the following assignment might be wrong. */
  663. status.freq = b43_channel_to_freq_5ghz(chanid);
  664. break;
  665. case B43_PHYTYPE_G:
  666. status.band = IEEE80211_BAND_2GHZ;
  667. /* chanid is the radio channel cookie value as used
  668. * to tune the radio. */
  669. status.freq = chanid + 2400;
  670. break;
  671. case B43_PHYTYPE_N:
  672. case B43_PHYTYPE_LP:
  673. /* chanid is the SHM channel cookie. Which is the plain
  674. * channel number in b43. */
  675. if (chanstat & B43_RX_CHAN_5GHZ) {
  676. status.band = IEEE80211_BAND_5GHZ;
  677. status.freq = b43_freq_to_channel_5ghz(chanid);
  678. } else {
  679. status.band = IEEE80211_BAND_2GHZ;
  680. status.freq = b43_freq_to_channel_2ghz(chanid);
  681. }
  682. break;
  683. default:
  684. B43_WARN_ON(1);
  685. goto drop;
  686. }
  687. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  688. ieee80211_rx_ni(dev->wl->hw, skb);
  689. #if B43_DEBUG
  690. dev->rx_count++;
  691. #endif
  692. return;
  693. drop:
  694. b43dbg(dev->wl, "RX: Packet dropped\n");
  695. dev_kfree_skb_any(skb);
  696. }
  697. void b43_handle_txstatus(struct b43_wldev *dev,
  698. const struct b43_txstatus *status)
  699. {
  700. b43_debugfs_log_txstat(dev, status);
  701. if (status->intermediate)
  702. return;
  703. if (status->for_ampdu)
  704. return;
  705. if (!status->acked)
  706. dev->wl->ieee_stats.dot11ACKFailureCount++;
  707. if (status->rts_count) {
  708. if (status->rts_count == 0xF) //FIXME
  709. dev->wl->ieee_stats.dot11RTSFailureCount++;
  710. else
  711. dev->wl->ieee_stats.dot11RTSSuccessCount++;
  712. }
  713. if (b43_using_pio_transfers(dev))
  714. b43_pio_handle_txstatus(dev, status);
  715. else
  716. b43_dma_handle_txstatus(dev, status);
  717. b43_phy_txpower_check(dev, 0);
  718. }
  719. /* Fill out the mac80211 TXstatus report based on the b43-specific
  720. * txstatus report data. This returns a boolean whether the frame was
  721. * successfully transmitted. */
  722. bool b43_fill_txstatus_report(struct b43_wldev *dev,
  723. struct ieee80211_tx_info *report,
  724. const struct b43_txstatus *status)
  725. {
  726. bool frame_success = 1;
  727. int retry_limit;
  728. /* preserve the confiured retry limit before clearing the status
  729. * The xmit function has overwritten the rc's value with the actual
  730. * retry limit done by the hardware */
  731. retry_limit = report->status.rates[0].count;
  732. ieee80211_tx_info_clear_status(report);
  733. if (status->acked) {
  734. /* The frame was ACKed. */
  735. report->flags |= IEEE80211_TX_STAT_ACK;
  736. } else {
  737. /* The frame was not ACKed... */
  738. if (!(report->flags & IEEE80211_TX_CTL_NO_ACK)) {
  739. /* ...but we expected an ACK. */
  740. frame_success = 0;
  741. }
  742. }
  743. if (status->frame_count == 0) {
  744. /* The frame was not transmitted at all. */
  745. report->status.rates[0].count = 0;
  746. } else if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
  747. /*
  748. * If the short retries (RTS, not data frame) have exceeded
  749. * the limit, the hw will not have tried the selected rate,
  750. * but will have used the fallback rate instead.
  751. * Don't let the rate control count attempts for the selected
  752. * rate in this case, otherwise the statistics will be off.
  753. */
  754. report->status.rates[0].count = 0;
  755. report->status.rates[1].count = status->frame_count;
  756. } else {
  757. if (status->frame_count > retry_limit) {
  758. report->status.rates[0].count = retry_limit;
  759. report->status.rates[1].count = status->frame_count -
  760. retry_limit;
  761. } else {
  762. report->status.rates[0].count = status->frame_count;
  763. report->status.rates[1].idx = -1;
  764. }
  765. }
  766. return frame_success;
  767. }
  768. /* Stop any TX operation on the device (suspend the hardware queues) */
  769. void b43_tx_suspend(struct b43_wldev *dev)
  770. {
  771. if (b43_using_pio_transfers(dev))
  772. b43_pio_tx_suspend(dev);
  773. else
  774. b43_dma_tx_suspend(dev);
  775. }
  776. /* Resume any TX operation on the device (resume the hardware queues) */
  777. void b43_tx_resume(struct b43_wldev *dev)
  778. {
  779. if (b43_using_pio_transfers(dev))
  780. b43_pio_tx_resume(dev);
  781. else
  782. b43_dma_tx_resume(dev);
  783. }