xmit.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 <m@bues.ch>
  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_p1k(info->control.hw_key, skb_frag, phase1key);
  291. /* phase1key is in host endian. Copy to little-endian txhdr->iv. */
  292. for (i = 0; i < 5; i++) {
  293. txhdr->iv[i * 2 + 0] = phase1key[i];
  294. txhdr->iv[i * 2 + 1] = phase1key[i] >> 8;
  295. }
  296. /* iv16 */
  297. memcpy(txhdr->iv + 10, ((u8 *) wlhdr) + wlhdr_len, 3);
  298. } else {
  299. iv_len = min((size_t) info->control.hw_key->iv_len,
  300. ARRAY_SIZE(txhdr->iv));
  301. memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
  302. }
  303. }
  304. if (b43_is_old_txhdr_format(dev)) {
  305. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->old_format.plcp),
  306. plcp_fragment_len, rate);
  307. } else {
  308. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->new_format.plcp),
  309. plcp_fragment_len, rate);
  310. }
  311. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
  312. plcp_fragment_len, rate_fb);
  313. /* Extra Frame Types */
  314. if (rate_fb_ofdm)
  315. extra_ft |= B43_TXH_EFT_FB_OFDM;
  316. else
  317. extra_ft |= B43_TXH_EFT_FB_CCK;
  318. /* Set channel radio code. Note that the micrcode ORs 0x100 to
  319. * this value before comparing it to the value in SHM, if this
  320. * is a 5Ghz packet.
  321. */
  322. txhdr->chan_radio_code = phy->channel;
  323. /* PHY TX Control word */
  324. if (rate_ofdm)
  325. phy_ctl |= B43_TXH_PHY_ENC_OFDM;
  326. else
  327. phy_ctl |= B43_TXH_PHY_ENC_CCK;
  328. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  329. phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
  330. switch (b43_ieee80211_antenna_sanitize(dev, info->antenna_sel_tx)) {
  331. case 0: /* Default */
  332. phy_ctl |= B43_TXH_PHY_ANT01AUTO;
  333. break;
  334. case 1: /* Antenna 0 */
  335. phy_ctl |= B43_TXH_PHY_ANT0;
  336. break;
  337. case 2: /* Antenna 1 */
  338. phy_ctl |= B43_TXH_PHY_ANT1;
  339. break;
  340. case 3: /* Antenna 2 */
  341. phy_ctl |= B43_TXH_PHY_ANT2;
  342. break;
  343. case 4: /* Antenna 3 */
  344. phy_ctl |= B43_TXH_PHY_ANT3;
  345. break;
  346. default:
  347. B43_WARN_ON(1);
  348. }
  349. rates = info->control.rates;
  350. /* MAC control */
  351. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  352. mac_ctl |= B43_TXH_MAC_ACK;
  353. /* use hardware sequence counter as the non-TID counter */
  354. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  355. mac_ctl |= B43_TXH_MAC_HWSEQ;
  356. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  357. mac_ctl |= B43_TXH_MAC_STMSDU;
  358. if (phy->type == B43_PHYTYPE_A)
  359. mac_ctl |= B43_TXH_MAC_5GHZ;
  360. /* Overwrite rates[0].count to make the retry calculation
  361. * in the tx status easier. need the actual retry limit to
  362. * detect whether the fallback rate was used.
  363. */
  364. if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  365. (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
  366. rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
  367. mac_ctl |= B43_TXH_MAC_LONGFRAME;
  368. } else {
  369. rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
  370. }
  371. /* Generate the RTS or CTS-to-self frame */
  372. if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  373. (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
  374. unsigned int len;
  375. struct ieee80211_hdr *hdr;
  376. int rts_rate, rts_rate_fb;
  377. int rts_rate_ofdm, rts_rate_fb_ofdm;
  378. struct b43_plcp_hdr6 *plcp;
  379. struct ieee80211_rate *rts_cts_rate;
  380. rts_cts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info);
  381. rts_rate = rts_cts_rate ? rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
  382. rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
  383. rts_rate_fb = b43_calc_fallback_rate(rts_rate);
  384. rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
  385. if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  386. struct ieee80211_cts *cts;
  387. if (b43_is_old_txhdr_format(dev)) {
  388. cts = (struct ieee80211_cts *)
  389. (txhdr->old_format.rts_frame);
  390. } else {
  391. cts = (struct ieee80211_cts *)
  392. (txhdr->new_format.rts_frame);
  393. }
  394. ieee80211_ctstoself_get(dev->wl->hw, info->control.vif,
  395. fragment_data, fragment_len,
  396. info, cts);
  397. mac_ctl |= B43_TXH_MAC_SENDCTS;
  398. len = sizeof(struct ieee80211_cts);
  399. } else {
  400. struct ieee80211_rts *rts;
  401. if (b43_is_old_txhdr_format(dev)) {
  402. rts = (struct ieee80211_rts *)
  403. (txhdr->old_format.rts_frame);
  404. } else {
  405. rts = (struct ieee80211_rts *)
  406. (txhdr->new_format.rts_frame);
  407. }
  408. ieee80211_rts_get(dev->wl->hw, info->control.vif,
  409. fragment_data, fragment_len,
  410. info, rts);
  411. mac_ctl |= B43_TXH_MAC_SENDRTS;
  412. len = sizeof(struct ieee80211_rts);
  413. }
  414. len += FCS_LEN;
  415. /* Generate the PLCP headers for the RTS/CTS frame */
  416. if (b43_is_old_txhdr_format(dev))
  417. plcp = &txhdr->old_format.rts_plcp;
  418. else
  419. plcp = &txhdr->new_format.rts_plcp;
  420. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
  421. len, rts_rate);
  422. plcp = &txhdr->rts_plcp_fb;
  423. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
  424. len, rts_rate_fb);
  425. if (b43_is_old_txhdr_format(dev)) {
  426. hdr = (struct ieee80211_hdr *)
  427. (&txhdr->old_format.rts_frame);
  428. } else {
  429. hdr = (struct ieee80211_hdr *)
  430. (&txhdr->new_format.rts_frame);
  431. }
  432. txhdr->rts_dur_fb = hdr->duration_id;
  433. if (rts_rate_ofdm) {
  434. extra_ft |= B43_TXH_EFT_RTS_OFDM;
  435. txhdr->phy_rate_rts =
  436. b43_plcp_get_ratecode_ofdm(rts_rate);
  437. } else {
  438. extra_ft |= B43_TXH_EFT_RTS_CCK;
  439. txhdr->phy_rate_rts =
  440. b43_plcp_get_ratecode_cck(rts_rate);
  441. }
  442. if (rts_rate_fb_ofdm)
  443. extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
  444. else
  445. extra_ft |= B43_TXH_EFT_RTSFB_CCK;
  446. if (rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS &&
  447. phy->type == B43_PHYTYPE_N) {
  448. txhdr->phy_ctl1_rts = cpu_to_le16(
  449. b43_generate_tx_phy_ctl1(dev, rts_rate));
  450. txhdr->phy_ctl1_rts_fb = cpu_to_le16(
  451. b43_generate_tx_phy_ctl1(dev, rts_rate_fb));
  452. }
  453. }
  454. /* Magic cookie */
  455. if (b43_is_old_txhdr_format(dev))
  456. txhdr->old_format.cookie = cpu_to_le16(cookie);
  457. else
  458. txhdr->new_format.cookie = cpu_to_le16(cookie);
  459. if (phy->type == B43_PHYTYPE_N) {
  460. txhdr->phy_ctl1 =
  461. cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate));
  462. txhdr->phy_ctl1_fb =
  463. cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate_fb));
  464. }
  465. /* Apply the bitfields */
  466. txhdr->mac_ctl = cpu_to_le32(mac_ctl);
  467. txhdr->phy_ctl = cpu_to_le16(phy_ctl);
  468. txhdr->extra_ft = extra_ft;
  469. return 0;
  470. }
  471. static s8 b43_rssi_postprocess(struct b43_wldev *dev,
  472. u8 in_rssi, int ofdm,
  473. int adjust_2053, int adjust_2050)
  474. {
  475. struct b43_phy *phy = &dev->phy;
  476. struct b43_phy_g *gphy = phy->g;
  477. s32 tmp;
  478. switch (phy->radio_ver) {
  479. case 0x2050:
  480. if (ofdm) {
  481. tmp = in_rssi;
  482. if (tmp > 127)
  483. tmp -= 256;
  484. tmp *= 73;
  485. tmp /= 64;
  486. if (adjust_2050)
  487. tmp += 25;
  488. else
  489. tmp -= 3;
  490. } else {
  491. if (dev->dev->bus_sprom->
  492. boardflags_lo & B43_BFL_RSSI) {
  493. if (in_rssi > 63)
  494. in_rssi = 63;
  495. B43_WARN_ON(phy->type != B43_PHYTYPE_G);
  496. tmp = gphy->nrssi_lt[in_rssi];
  497. tmp = 31 - tmp;
  498. tmp *= -131;
  499. tmp /= 128;
  500. tmp -= 57;
  501. } else {
  502. tmp = in_rssi;
  503. tmp = 31 - tmp;
  504. tmp *= -149;
  505. tmp /= 128;
  506. tmp -= 68;
  507. }
  508. if (phy->type == B43_PHYTYPE_G && adjust_2050)
  509. tmp += 25;
  510. }
  511. break;
  512. case 0x2060:
  513. if (in_rssi > 127)
  514. tmp = in_rssi - 256;
  515. else
  516. tmp = in_rssi;
  517. break;
  518. default:
  519. tmp = in_rssi;
  520. tmp -= 11;
  521. tmp *= 103;
  522. tmp /= 64;
  523. if (adjust_2053)
  524. tmp -= 109;
  525. else
  526. tmp -= 83;
  527. }
  528. return (s8) tmp;
  529. }
  530. //TODO
  531. #if 0
  532. static s8 b43_rssinoise_postprocess(struct b43_wldev *dev, u8 in_rssi)
  533. {
  534. struct b43_phy *phy = &dev->phy;
  535. s8 ret;
  536. if (phy->type == B43_PHYTYPE_A) {
  537. //TODO: Incomplete specs.
  538. ret = 0;
  539. } else
  540. ret = b43_rssi_postprocess(dev, in_rssi, 0, 1, 1);
  541. return ret;
  542. }
  543. #endif
  544. void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
  545. {
  546. struct ieee80211_rx_status status;
  547. struct b43_plcp_hdr6 *plcp;
  548. struct ieee80211_hdr *wlhdr;
  549. const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
  550. __le16 fctl;
  551. u16 phystat0, phystat3, chanstat, mactime;
  552. u32 macstat;
  553. u16 chanid;
  554. u16 phytype;
  555. int padding;
  556. memset(&status, 0, sizeof(status));
  557. /* Get metadata about the frame from the header. */
  558. phystat0 = le16_to_cpu(rxhdr->phy_status0);
  559. phystat3 = le16_to_cpu(rxhdr->phy_status3);
  560. macstat = le32_to_cpu(rxhdr->mac_status);
  561. mactime = le16_to_cpu(rxhdr->mac_time);
  562. chanstat = le16_to_cpu(rxhdr->channel);
  563. phytype = chanstat & B43_RX_CHAN_PHYTYPE;
  564. if (unlikely(macstat & B43_RX_MAC_FCSERR)) {
  565. dev->wl->ieee_stats.dot11FCSErrorCount++;
  566. status.flag |= RX_FLAG_FAILED_FCS_CRC;
  567. }
  568. if (unlikely(phystat0 & (B43_RX_PHYST0_PLCPHCF | B43_RX_PHYST0_PLCPFV)))
  569. status.flag |= RX_FLAG_FAILED_PLCP_CRC;
  570. if (phystat0 & B43_RX_PHYST0_SHORTPRMBL)
  571. status.flag |= RX_FLAG_SHORTPRE;
  572. if (macstat & B43_RX_MAC_DECERR) {
  573. /* Decryption with the given key failed.
  574. * Drop the packet. We also won't be able to decrypt it with
  575. * the key in software. */
  576. goto drop;
  577. }
  578. /* Skip PLCP and padding */
  579. padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
  580. if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
  581. b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
  582. goto drop;
  583. }
  584. plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
  585. skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
  586. /* The skb contains the Wireless Header + payload data now */
  587. if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
  588. b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
  589. goto drop;
  590. }
  591. wlhdr = (struct ieee80211_hdr *)(skb->data);
  592. fctl = wlhdr->frame_control;
  593. if (macstat & B43_RX_MAC_DEC) {
  594. unsigned int keyidx;
  595. int wlhdr_len;
  596. keyidx = ((macstat & B43_RX_MAC_KEYIDX)
  597. >> B43_RX_MAC_KEYIDX_SHIFT);
  598. /* We must adjust the key index here. We want the "physical"
  599. * key index, but the ucode passed it slightly different.
  600. */
  601. keyidx = b43_kidx_to_raw(dev, keyidx);
  602. B43_WARN_ON(keyidx >= ARRAY_SIZE(dev->key));
  603. if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
  604. wlhdr_len = ieee80211_hdrlen(fctl);
  605. if (unlikely(skb->len < (wlhdr_len + 3))) {
  606. b43dbg(dev->wl,
  607. "RX: Packet size underrun (3)\n");
  608. goto drop;
  609. }
  610. status.flag |= RX_FLAG_DECRYPTED;
  611. }
  612. }
  613. /* Link quality statistics */
  614. if ((chanstat & B43_RX_CHAN_PHYTYPE) == B43_PHYTYPE_N) {
  615. // s8 rssi = max(rxhdr->power0, rxhdr->power1);
  616. //TODO: Find out what the rssi value is (dBm or percentage?)
  617. // and also find out what the maximum possible value is.
  618. // Fill status.ssi and status.signal fields.
  619. } else {
  620. status.signal = b43_rssi_postprocess(dev, rxhdr->jssi,
  621. (phystat0 & B43_RX_PHYST0_OFDM),
  622. (phystat0 & B43_RX_PHYST0_GAINCTL),
  623. (phystat3 & B43_RX_PHYST3_TRSTATE));
  624. }
  625. if (phystat0 & B43_RX_PHYST0_OFDM)
  626. status.rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
  627. phytype == B43_PHYTYPE_A);
  628. else
  629. status.rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
  630. if (unlikely(status.rate_idx == -1)) {
  631. /* PLCP seems to be corrupted.
  632. * Drop the frame, if we are not interested in corrupted frames. */
  633. if (!(dev->wl->filter_flags & FIF_PLCPFAIL))
  634. goto drop;
  635. }
  636. status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
  637. /*
  638. * All frames on monitor interfaces and beacons always need a full
  639. * 64-bit timestamp. Monitor interfaces need it for diagnostic
  640. * purposes and beacons for IBSS merging.
  641. * This code assumes we get to process the packet within 16 bits
  642. * of timestamp, i.e. about 65 milliseconds after the PHY received
  643. * the first symbol.
  644. */
  645. if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
  646. u16 low_mactime_now;
  647. b43_tsf_read(dev, &status.mactime);
  648. low_mactime_now = status.mactime;
  649. status.mactime = status.mactime & ~0xFFFFULL;
  650. status.mactime += mactime;
  651. if (low_mactime_now <= mactime)
  652. status.mactime -= 0x10000;
  653. status.flag |= RX_FLAG_MACTIME_MPDU;
  654. }
  655. chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
  656. switch (chanstat & B43_RX_CHAN_PHYTYPE) {
  657. case B43_PHYTYPE_A:
  658. status.band = IEEE80211_BAND_5GHZ;
  659. B43_WARN_ON(1);
  660. /* FIXME: We don't really know which value the "chanid" contains.
  661. * So the following assignment might be wrong. */
  662. status.freq = b43_channel_to_freq_5ghz(chanid);
  663. break;
  664. case B43_PHYTYPE_G:
  665. status.band = IEEE80211_BAND_2GHZ;
  666. /* chanid is the radio channel cookie value as used
  667. * to tune the radio. */
  668. status.freq = chanid + 2400;
  669. break;
  670. case B43_PHYTYPE_N:
  671. case B43_PHYTYPE_LP:
  672. /* chanid is the SHM channel cookie. Which is the plain
  673. * channel number in b43. */
  674. if (chanstat & B43_RX_CHAN_5GHZ) {
  675. status.band = IEEE80211_BAND_5GHZ;
  676. status.freq = b43_freq_to_channel_5ghz(chanid);
  677. } else {
  678. status.band = IEEE80211_BAND_2GHZ;
  679. status.freq = b43_freq_to_channel_2ghz(chanid);
  680. }
  681. break;
  682. default:
  683. B43_WARN_ON(1);
  684. goto drop;
  685. }
  686. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  687. ieee80211_rx_ni(dev->wl->hw, skb);
  688. #if B43_DEBUG
  689. dev->rx_count++;
  690. #endif
  691. return;
  692. drop:
  693. b43dbg(dev->wl, "RX: Packet dropped\n");
  694. dev_kfree_skb_any(skb);
  695. }
  696. void b43_handle_txstatus(struct b43_wldev *dev,
  697. const struct b43_txstatus *status)
  698. {
  699. b43_debugfs_log_txstat(dev, status);
  700. if (status->intermediate)
  701. return;
  702. if (status->for_ampdu)
  703. return;
  704. if (!status->acked)
  705. dev->wl->ieee_stats.dot11ACKFailureCount++;
  706. if (status->rts_count) {
  707. if (status->rts_count == 0xF) //FIXME
  708. dev->wl->ieee_stats.dot11RTSFailureCount++;
  709. else
  710. dev->wl->ieee_stats.dot11RTSSuccessCount++;
  711. }
  712. if (b43_using_pio_transfers(dev))
  713. b43_pio_handle_txstatus(dev, status);
  714. else
  715. b43_dma_handle_txstatus(dev, status);
  716. b43_phy_txpower_check(dev, 0);
  717. }
  718. /* Fill out the mac80211 TXstatus report based on the b43-specific
  719. * txstatus report data. This returns a boolean whether the frame was
  720. * successfully transmitted. */
  721. bool b43_fill_txstatus_report(struct b43_wldev *dev,
  722. struct ieee80211_tx_info *report,
  723. const struct b43_txstatus *status)
  724. {
  725. bool frame_success = 1;
  726. int retry_limit;
  727. /* preserve the confiured retry limit before clearing the status
  728. * The xmit function has overwritten the rc's value with the actual
  729. * retry limit done by the hardware */
  730. retry_limit = report->status.rates[0].count;
  731. ieee80211_tx_info_clear_status(report);
  732. if (status->acked) {
  733. /* The frame was ACKed. */
  734. report->flags |= IEEE80211_TX_STAT_ACK;
  735. } else {
  736. /* The frame was not ACKed... */
  737. if (!(report->flags & IEEE80211_TX_CTL_NO_ACK)) {
  738. /* ...but we expected an ACK. */
  739. frame_success = 0;
  740. }
  741. }
  742. if (status->frame_count == 0) {
  743. /* The frame was not transmitted at all. */
  744. report->status.rates[0].count = 0;
  745. } else if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
  746. /*
  747. * If the short retries (RTS, not data frame) have exceeded
  748. * the limit, the hw will not have tried the selected rate,
  749. * but will have used the fallback rate instead.
  750. * Don't let the rate control count attempts for the selected
  751. * rate in this case, otherwise the statistics will be off.
  752. */
  753. report->status.rates[0].count = 0;
  754. report->status.rates[1].count = status->frame_count;
  755. } else {
  756. if (status->frame_count > retry_limit) {
  757. report->status.rates[0].count = retry_limit;
  758. report->status.rates[1].count = status->frame_count -
  759. retry_limit;
  760. } else {
  761. report->status.rates[0].count = status->frame_count;
  762. report->status.rates[1].idx = -1;
  763. }
  764. }
  765. return frame_success;
  766. }
  767. /* Stop any TX operation on the device (suspend the hardware queues) */
  768. void b43_tx_suspend(struct b43_wldev *dev)
  769. {
  770. if (b43_using_pio_transfers(dev))
  771. b43_pio_tx_suspend(dev);
  772. else
  773. b43_dma_tx_suspend(dev);
  774. }
  775. /* Resume any TX operation on the device (resume the hardware queues) */
  776. void b43_tx_resume(struct b43_wldev *dev)
  777. {
  778. if (b43_using_pio_transfers(dev))
  779. b43_pio_tx_resume(dev);
  780. else
  781. b43_dma_tx_resume(dev);
  782. }