xmit.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. /* TODO: verify if needed for SSLPN or LCN */
  157. static u16 b43_generate_tx_phy_ctl1(struct b43_wldev *dev, u8 bitrate)
  158. {
  159. const struct b43_phy *phy = &dev->phy;
  160. const struct b43_tx_legacy_rate_phy_ctl_entry *e;
  161. u16 control = 0;
  162. u16 bw;
  163. if (phy->type == B43_PHYTYPE_LP)
  164. bw = B43_TXH_PHY1_BW_20;
  165. else /* FIXME */
  166. bw = B43_TXH_PHY1_BW_20;
  167. if (0) { /* FIXME: MIMO */
  168. } else if (b43_is_cck_rate(bitrate) && phy->type != B43_PHYTYPE_LP) {
  169. control = bw;
  170. } else {
  171. control = bw;
  172. e = b43_tx_legacy_rate_phy_ctl_ent(bitrate);
  173. if (e) {
  174. control |= e->coding_rate;
  175. control |= e->modulation;
  176. }
  177. control |= B43_TXH_PHY1_MODE_SISO;
  178. }
  179. return control;
  180. }
  181. static u8 b43_calc_fallback_rate(u8 bitrate)
  182. {
  183. switch (bitrate) {
  184. case B43_CCK_RATE_1MB:
  185. return B43_CCK_RATE_1MB;
  186. case B43_CCK_RATE_2MB:
  187. return B43_CCK_RATE_1MB;
  188. case B43_CCK_RATE_5MB:
  189. return B43_CCK_RATE_2MB;
  190. case B43_CCK_RATE_11MB:
  191. return B43_CCK_RATE_5MB;
  192. case B43_OFDM_RATE_6MB:
  193. return B43_CCK_RATE_5MB;
  194. case B43_OFDM_RATE_9MB:
  195. return B43_OFDM_RATE_6MB;
  196. case B43_OFDM_RATE_12MB:
  197. return B43_OFDM_RATE_9MB;
  198. case B43_OFDM_RATE_18MB:
  199. return B43_OFDM_RATE_12MB;
  200. case B43_OFDM_RATE_24MB:
  201. return B43_OFDM_RATE_18MB;
  202. case B43_OFDM_RATE_36MB:
  203. return B43_OFDM_RATE_24MB;
  204. case B43_OFDM_RATE_48MB:
  205. return B43_OFDM_RATE_36MB;
  206. case B43_OFDM_RATE_54MB:
  207. return B43_OFDM_RATE_48MB;
  208. }
  209. B43_WARN_ON(1);
  210. return 0;
  211. }
  212. /* Generate a TX data header. */
  213. int b43_generate_txhdr(struct b43_wldev *dev,
  214. u8 *_txhdr,
  215. struct sk_buff *skb_frag,
  216. struct ieee80211_tx_info *info,
  217. u16 cookie)
  218. {
  219. const unsigned char *fragment_data = skb_frag->data;
  220. unsigned int fragment_len = skb_frag->len;
  221. struct b43_txhdr *txhdr = (struct b43_txhdr *)_txhdr;
  222. const struct b43_phy *phy = &dev->phy;
  223. const struct ieee80211_hdr *wlhdr =
  224. (const struct ieee80211_hdr *)fragment_data;
  225. int use_encryption = !!info->control.hw_key;
  226. __le16 fctl = wlhdr->frame_control;
  227. struct ieee80211_rate *fbrate;
  228. u8 rate, rate_fb;
  229. int rate_ofdm, rate_fb_ofdm;
  230. unsigned int plcp_fragment_len;
  231. u32 mac_ctl = 0;
  232. u16 phy_ctl = 0;
  233. bool fill_phy_ctl1 = (phy->type == B43_PHYTYPE_LP ||
  234. phy->type == B43_PHYTYPE_N ||
  235. phy->type == B43_PHYTYPE_HT);
  236. u8 extra_ft = 0;
  237. struct ieee80211_rate *txrate;
  238. struct ieee80211_tx_rate *rates;
  239. memset(txhdr, 0, sizeof(*txhdr));
  240. txrate = ieee80211_get_tx_rate(dev->wl->hw, info);
  241. rate = txrate ? txrate->hw_value : B43_CCK_RATE_1MB;
  242. rate_ofdm = b43_is_ofdm_rate(rate);
  243. fbrate = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : txrate;
  244. rate_fb = fbrate->hw_value;
  245. rate_fb_ofdm = b43_is_ofdm_rate(rate_fb);
  246. if (rate_ofdm)
  247. txhdr->phy_rate = b43_plcp_get_ratecode_ofdm(rate);
  248. else
  249. txhdr->phy_rate = b43_plcp_get_ratecode_cck(rate);
  250. txhdr->mac_frame_ctl = wlhdr->frame_control;
  251. memcpy(txhdr->tx_receiver, wlhdr->addr1, 6);
  252. /* Calculate duration for fallback rate */
  253. if ((rate_fb == rate) ||
  254. (wlhdr->duration_id & cpu_to_le16(0x8000)) ||
  255. (wlhdr->duration_id == cpu_to_le16(0))) {
  256. /* If the fallback rate equals the normal rate or the
  257. * dur_id field contains an AID, CFP magic or 0,
  258. * use the original dur_id field. */
  259. txhdr->dur_fb = wlhdr->duration_id;
  260. } else {
  261. txhdr->dur_fb = ieee80211_generic_frame_duration(
  262. dev->wl->hw, info->control.vif, info->band,
  263. fragment_len, fbrate);
  264. }
  265. plcp_fragment_len = fragment_len + FCS_LEN;
  266. if (use_encryption) {
  267. u8 key_idx = info->control.hw_key->hw_key_idx;
  268. struct b43_key *key;
  269. int wlhdr_len;
  270. size_t iv_len;
  271. B43_WARN_ON(key_idx >= ARRAY_SIZE(dev->key));
  272. key = &(dev->key[key_idx]);
  273. if (unlikely(!key->keyconf)) {
  274. /* This key is invalid. This might only happen
  275. * in a short timeframe after machine resume before
  276. * we were able to reconfigure keys.
  277. * Drop this packet completely. Do not transmit it
  278. * unencrypted to avoid leaking information. */
  279. return -ENOKEY;
  280. }
  281. /* Hardware appends ICV. */
  282. plcp_fragment_len += info->control.hw_key->icv_len;
  283. key_idx = b43_kidx_to_fw(dev, key_idx);
  284. mac_ctl |= (key_idx << B43_TXH_MAC_KEYIDX_SHIFT) &
  285. B43_TXH_MAC_KEYIDX;
  286. mac_ctl |= (key->algorithm << B43_TXH_MAC_KEYALG_SHIFT) &
  287. B43_TXH_MAC_KEYALG;
  288. wlhdr_len = ieee80211_hdrlen(fctl);
  289. if (key->algorithm == B43_SEC_ALGO_TKIP) {
  290. u16 phase1key[5];
  291. int i;
  292. /* we give the phase1key and iv16 here, the key is stored in
  293. * shm. With that the hardware can do phase 2 and encryption.
  294. */
  295. ieee80211_get_tkip_p1k(info->control.hw_key, skb_frag, phase1key);
  296. /* phase1key is in host endian. Copy to little-endian txhdr->iv. */
  297. for (i = 0; i < 5; i++) {
  298. txhdr->iv[i * 2 + 0] = phase1key[i];
  299. txhdr->iv[i * 2 + 1] = phase1key[i] >> 8;
  300. }
  301. /* iv16 */
  302. memcpy(txhdr->iv + 10, ((u8 *) wlhdr) + wlhdr_len, 3);
  303. } else {
  304. iv_len = min((size_t) info->control.hw_key->iv_len,
  305. ARRAY_SIZE(txhdr->iv));
  306. memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
  307. }
  308. }
  309. switch (dev->fw.hdr_format) {
  310. case B43_FW_HDR_598:
  311. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_598.plcp),
  312. plcp_fragment_len, rate);
  313. break;
  314. case B43_FW_HDR_351:
  315. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_351.plcp),
  316. plcp_fragment_len, rate);
  317. break;
  318. case B43_FW_HDR_410:
  319. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_410.plcp),
  320. plcp_fragment_len, rate);
  321. break;
  322. }
  323. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
  324. plcp_fragment_len, rate_fb);
  325. /* Extra Frame Types */
  326. if (rate_fb_ofdm)
  327. extra_ft |= B43_TXH_EFT_FB_OFDM;
  328. else
  329. extra_ft |= B43_TXH_EFT_FB_CCK;
  330. /* Set channel radio code. Note that the micrcode ORs 0x100 to
  331. * this value before comparing it to the value in SHM, if this
  332. * is a 5Ghz packet.
  333. */
  334. txhdr->chan_radio_code = phy->channel;
  335. /* PHY TX Control word */
  336. if (rate_ofdm)
  337. phy_ctl |= B43_TXH_PHY_ENC_OFDM;
  338. else
  339. phy_ctl |= B43_TXH_PHY_ENC_CCK;
  340. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  341. phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
  342. switch (b43_ieee80211_antenna_sanitize(dev, 0)) {
  343. case 0: /* Default */
  344. phy_ctl |= B43_TXH_PHY_ANT01AUTO;
  345. break;
  346. case 1: /* Antenna 0 */
  347. phy_ctl |= B43_TXH_PHY_ANT0;
  348. break;
  349. case 2: /* Antenna 1 */
  350. phy_ctl |= B43_TXH_PHY_ANT1;
  351. break;
  352. case 3: /* Antenna 2 */
  353. phy_ctl |= B43_TXH_PHY_ANT2;
  354. break;
  355. case 4: /* Antenna 3 */
  356. phy_ctl |= B43_TXH_PHY_ANT3;
  357. break;
  358. default:
  359. B43_WARN_ON(1);
  360. }
  361. rates = info->control.rates;
  362. /* MAC control */
  363. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  364. mac_ctl |= B43_TXH_MAC_ACK;
  365. /* use hardware sequence counter as the non-TID counter */
  366. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  367. mac_ctl |= B43_TXH_MAC_HWSEQ;
  368. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  369. mac_ctl |= B43_TXH_MAC_STMSDU;
  370. if (phy->type == B43_PHYTYPE_A)
  371. mac_ctl |= B43_TXH_MAC_5GHZ;
  372. /* Overwrite rates[0].count to make the retry calculation
  373. * in the tx status easier. need the actual retry limit to
  374. * detect whether the fallback rate was used.
  375. */
  376. if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  377. (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
  378. rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
  379. mac_ctl |= B43_TXH_MAC_LONGFRAME;
  380. } else {
  381. rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
  382. }
  383. /* Generate the RTS or CTS-to-self frame */
  384. if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  385. (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
  386. unsigned int len;
  387. struct ieee80211_hdr *uninitialized_var(hdr);
  388. int rts_rate, rts_rate_fb;
  389. int rts_rate_ofdm, rts_rate_fb_ofdm;
  390. struct b43_plcp_hdr6 *uninitialized_var(plcp);
  391. struct ieee80211_rate *rts_cts_rate;
  392. rts_cts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info);
  393. rts_rate = rts_cts_rate ? rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
  394. rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
  395. rts_rate_fb = b43_calc_fallback_rate(rts_rate);
  396. rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
  397. if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  398. struct ieee80211_cts *uninitialized_var(cts);
  399. switch (dev->fw.hdr_format) {
  400. case B43_FW_HDR_598:
  401. cts = (struct ieee80211_cts *)
  402. (txhdr->format_598.rts_frame);
  403. break;
  404. case B43_FW_HDR_351:
  405. cts = (struct ieee80211_cts *)
  406. (txhdr->format_351.rts_frame);
  407. break;
  408. case B43_FW_HDR_410:
  409. cts = (struct ieee80211_cts *)
  410. (txhdr->format_410.rts_frame);
  411. break;
  412. }
  413. ieee80211_ctstoself_get(dev->wl->hw, info->control.vif,
  414. fragment_data, fragment_len,
  415. info, cts);
  416. mac_ctl |= B43_TXH_MAC_SENDCTS;
  417. len = sizeof(struct ieee80211_cts);
  418. } else {
  419. struct ieee80211_rts *uninitialized_var(rts);
  420. switch (dev->fw.hdr_format) {
  421. case B43_FW_HDR_598:
  422. rts = (struct ieee80211_rts *)
  423. (txhdr->format_598.rts_frame);
  424. break;
  425. case B43_FW_HDR_351:
  426. rts = (struct ieee80211_rts *)
  427. (txhdr->format_351.rts_frame);
  428. break;
  429. case B43_FW_HDR_410:
  430. rts = (struct ieee80211_rts *)
  431. (txhdr->format_410.rts_frame);
  432. break;
  433. }
  434. ieee80211_rts_get(dev->wl->hw, info->control.vif,
  435. fragment_data, fragment_len,
  436. info, rts);
  437. mac_ctl |= B43_TXH_MAC_SENDRTS;
  438. len = sizeof(struct ieee80211_rts);
  439. }
  440. len += FCS_LEN;
  441. /* Generate the PLCP headers for the RTS/CTS frame */
  442. switch (dev->fw.hdr_format) {
  443. case B43_FW_HDR_598:
  444. plcp = &txhdr->format_598.rts_plcp;
  445. break;
  446. case B43_FW_HDR_351:
  447. plcp = &txhdr->format_351.rts_plcp;
  448. break;
  449. case B43_FW_HDR_410:
  450. plcp = &txhdr->format_410.rts_plcp;
  451. break;
  452. }
  453. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
  454. len, rts_rate);
  455. plcp = &txhdr->rts_plcp_fb;
  456. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
  457. len, rts_rate_fb);
  458. switch (dev->fw.hdr_format) {
  459. case B43_FW_HDR_598:
  460. hdr = (struct ieee80211_hdr *)
  461. (&txhdr->format_598.rts_frame);
  462. break;
  463. case B43_FW_HDR_351:
  464. hdr = (struct ieee80211_hdr *)
  465. (&txhdr->format_351.rts_frame);
  466. break;
  467. case B43_FW_HDR_410:
  468. hdr = (struct ieee80211_hdr *)
  469. (&txhdr->format_410.rts_frame);
  470. break;
  471. }
  472. txhdr->rts_dur_fb = hdr->duration_id;
  473. if (rts_rate_ofdm) {
  474. extra_ft |= B43_TXH_EFT_RTS_OFDM;
  475. txhdr->phy_rate_rts =
  476. b43_plcp_get_ratecode_ofdm(rts_rate);
  477. } else {
  478. extra_ft |= B43_TXH_EFT_RTS_CCK;
  479. txhdr->phy_rate_rts =
  480. b43_plcp_get_ratecode_cck(rts_rate);
  481. }
  482. if (rts_rate_fb_ofdm)
  483. extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
  484. else
  485. extra_ft |= B43_TXH_EFT_RTSFB_CCK;
  486. if (rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS &&
  487. fill_phy_ctl1) {
  488. txhdr->phy_ctl1_rts = cpu_to_le16(
  489. b43_generate_tx_phy_ctl1(dev, rts_rate));
  490. txhdr->phy_ctl1_rts_fb = cpu_to_le16(
  491. b43_generate_tx_phy_ctl1(dev, rts_rate_fb));
  492. }
  493. }
  494. /* Magic cookie */
  495. switch (dev->fw.hdr_format) {
  496. case B43_FW_HDR_598:
  497. txhdr->format_598.cookie = cpu_to_le16(cookie);
  498. break;
  499. case B43_FW_HDR_351:
  500. txhdr->format_351.cookie = cpu_to_le16(cookie);
  501. break;
  502. case B43_FW_HDR_410:
  503. txhdr->format_410.cookie = cpu_to_le16(cookie);
  504. break;
  505. }
  506. if (fill_phy_ctl1) {
  507. txhdr->phy_ctl1 =
  508. cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate));
  509. txhdr->phy_ctl1_fb =
  510. cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate_fb));
  511. }
  512. /* Apply the bitfields */
  513. txhdr->mac_ctl = cpu_to_le32(mac_ctl);
  514. txhdr->phy_ctl = cpu_to_le16(phy_ctl);
  515. txhdr->extra_ft = extra_ft;
  516. return 0;
  517. }
  518. static s8 b43_rssi_postprocess(struct b43_wldev *dev,
  519. u8 in_rssi, int ofdm,
  520. int adjust_2053, int adjust_2050)
  521. {
  522. struct b43_phy *phy = &dev->phy;
  523. struct b43_phy_g *gphy = phy->g;
  524. s32 tmp;
  525. switch (phy->radio_ver) {
  526. case 0x2050:
  527. if (ofdm) {
  528. tmp = in_rssi;
  529. if (tmp > 127)
  530. tmp -= 256;
  531. tmp *= 73;
  532. tmp /= 64;
  533. if (adjust_2050)
  534. tmp += 25;
  535. else
  536. tmp -= 3;
  537. } else {
  538. if (dev->dev->bus_sprom->
  539. boardflags_lo & B43_BFL_RSSI) {
  540. if (in_rssi > 63)
  541. in_rssi = 63;
  542. B43_WARN_ON(phy->type != B43_PHYTYPE_G);
  543. tmp = gphy->nrssi_lt[in_rssi];
  544. tmp = 31 - tmp;
  545. tmp *= -131;
  546. tmp /= 128;
  547. tmp -= 57;
  548. } else {
  549. tmp = in_rssi;
  550. tmp = 31 - tmp;
  551. tmp *= -149;
  552. tmp /= 128;
  553. tmp -= 68;
  554. }
  555. if (phy->type == B43_PHYTYPE_G && adjust_2050)
  556. tmp += 25;
  557. }
  558. break;
  559. case 0x2060:
  560. if (in_rssi > 127)
  561. tmp = in_rssi - 256;
  562. else
  563. tmp = in_rssi;
  564. break;
  565. default:
  566. tmp = in_rssi;
  567. tmp -= 11;
  568. tmp *= 103;
  569. tmp /= 64;
  570. if (adjust_2053)
  571. tmp -= 109;
  572. else
  573. tmp -= 83;
  574. }
  575. return (s8) tmp;
  576. }
  577. //TODO
  578. #if 0
  579. static s8 b43_rssinoise_postprocess(struct b43_wldev *dev, u8 in_rssi)
  580. {
  581. struct b43_phy *phy = &dev->phy;
  582. s8 ret;
  583. if (phy->type == B43_PHYTYPE_A) {
  584. //TODO: Incomplete specs.
  585. ret = 0;
  586. } else
  587. ret = b43_rssi_postprocess(dev, in_rssi, 0, 1, 1);
  588. return ret;
  589. }
  590. #endif
  591. void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
  592. {
  593. struct ieee80211_rx_status status;
  594. struct b43_plcp_hdr6 *plcp;
  595. struct ieee80211_hdr *wlhdr;
  596. const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
  597. __le16 fctl;
  598. u16 phystat0, phystat3;
  599. u16 uninitialized_var(chanstat), uninitialized_var(mactime);
  600. u32 uninitialized_var(macstat);
  601. u16 chanid;
  602. u16 phytype;
  603. int padding, rate_idx;
  604. memset(&status, 0, sizeof(status));
  605. /* Get metadata about the frame from the header. */
  606. phystat0 = le16_to_cpu(rxhdr->phy_status0);
  607. phystat3 = le16_to_cpu(rxhdr->phy_status3);
  608. switch (dev->fw.hdr_format) {
  609. case B43_FW_HDR_598:
  610. macstat = le32_to_cpu(rxhdr->format_598.mac_status);
  611. mactime = le16_to_cpu(rxhdr->format_598.mac_time);
  612. chanstat = le16_to_cpu(rxhdr->format_598.channel);
  613. break;
  614. case B43_FW_HDR_410:
  615. case B43_FW_HDR_351:
  616. macstat = le32_to_cpu(rxhdr->format_351.mac_status);
  617. mactime = le16_to_cpu(rxhdr->format_351.mac_time);
  618. chanstat = le16_to_cpu(rxhdr->format_351.channel);
  619. break;
  620. }
  621. phytype = chanstat & B43_RX_CHAN_PHYTYPE;
  622. if (unlikely(macstat & B43_RX_MAC_FCSERR)) {
  623. dev->wl->ieee_stats.dot11FCSErrorCount++;
  624. status.flag |= RX_FLAG_FAILED_FCS_CRC;
  625. }
  626. if (unlikely(phystat0 & (B43_RX_PHYST0_PLCPHCF | B43_RX_PHYST0_PLCPFV)))
  627. status.flag |= RX_FLAG_FAILED_PLCP_CRC;
  628. if (phystat0 & B43_RX_PHYST0_SHORTPRMBL)
  629. status.flag |= RX_FLAG_SHORTPRE;
  630. if (macstat & B43_RX_MAC_DECERR) {
  631. /* Decryption with the given key failed.
  632. * Drop the packet. We also won't be able to decrypt it with
  633. * the key in software. */
  634. goto drop;
  635. }
  636. /* Skip PLCP and padding */
  637. padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
  638. if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
  639. b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
  640. goto drop;
  641. }
  642. plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
  643. skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
  644. /* The skb contains the Wireless Header + payload data now */
  645. if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
  646. b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
  647. goto drop;
  648. }
  649. wlhdr = (struct ieee80211_hdr *)(skb->data);
  650. fctl = wlhdr->frame_control;
  651. if (macstat & B43_RX_MAC_DEC) {
  652. unsigned int keyidx;
  653. int wlhdr_len;
  654. keyidx = ((macstat & B43_RX_MAC_KEYIDX)
  655. >> B43_RX_MAC_KEYIDX_SHIFT);
  656. /* We must adjust the key index here. We want the "physical"
  657. * key index, but the ucode passed it slightly different.
  658. */
  659. keyidx = b43_kidx_to_raw(dev, keyidx);
  660. B43_WARN_ON(keyidx >= ARRAY_SIZE(dev->key));
  661. if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
  662. wlhdr_len = ieee80211_hdrlen(fctl);
  663. if (unlikely(skb->len < (wlhdr_len + 3))) {
  664. b43dbg(dev->wl,
  665. "RX: Packet size underrun (3)\n");
  666. goto drop;
  667. }
  668. status.flag |= RX_FLAG_DECRYPTED;
  669. }
  670. }
  671. /* Link quality statistics */
  672. switch (chanstat & B43_RX_CHAN_PHYTYPE) {
  673. case B43_PHYTYPE_HT:
  674. /* TODO: is max the right choice? */
  675. status.signal = max_t(__s8,
  676. max(rxhdr->phy_ht_power0, rxhdr->phy_ht_power1),
  677. rxhdr->phy_ht_power2);
  678. break;
  679. case B43_PHYTYPE_N:
  680. /* Broadcom has code for min and avg, but always uses max */
  681. if (rxhdr->power0 == 16 || rxhdr->power0 == 32)
  682. status.signal = max(rxhdr->power1, rxhdr->power2);
  683. else
  684. status.signal = max(rxhdr->power0, rxhdr->power1);
  685. break;
  686. case B43_PHYTYPE_A:
  687. case B43_PHYTYPE_B:
  688. case B43_PHYTYPE_G:
  689. case B43_PHYTYPE_LP:
  690. status.signal = b43_rssi_postprocess(dev, rxhdr->jssi,
  691. (phystat0 & B43_RX_PHYST0_OFDM),
  692. (phystat0 & B43_RX_PHYST0_GAINCTL),
  693. (phystat3 & B43_RX_PHYST3_TRSTATE));
  694. break;
  695. }
  696. if (phystat0 & B43_RX_PHYST0_OFDM)
  697. rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
  698. phytype == B43_PHYTYPE_A);
  699. else
  700. rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
  701. if (unlikely(rate_idx == -1)) {
  702. /* PLCP seems to be corrupted.
  703. * Drop the frame, if we are not interested in corrupted frames. */
  704. if (!(dev->wl->filter_flags & FIF_PLCPFAIL))
  705. goto drop;
  706. }
  707. status.rate_idx = rate_idx;
  708. status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
  709. /*
  710. * All frames on monitor interfaces and beacons always need a full
  711. * 64-bit timestamp. Monitor interfaces need it for diagnostic
  712. * purposes and beacons for IBSS merging.
  713. * This code assumes we get to process the packet within 16 bits
  714. * of timestamp, i.e. about 65 milliseconds after the PHY received
  715. * the first symbol.
  716. */
  717. if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
  718. u16 low_mactime_now;
  719. b43_tsf_read(dev, &status.mactime);
  720. low_mactime_now = status.mactime;
  721. status.mactime = status.mactime & ~0xFFFFULL;
  722. status.mactime += mactime;
  723. if (low_mactime_now <= mactime)
  724. status.mactime -= 0x10000;
  725. status.flag |= RX_FLAG_MACTIME_START;
  726. }
  727. chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
  728. switch (chanstat & B43_RX_CHAN_PHYTYPE) {
  729. case B43_PHYTYPE_A:
  730. status.band = IEEE80211_BAND_5GHZ;
  731. B43_WARN_ON(1);
  732. /* FIXME: We don't really know which value the "chanid" contains.
  733. * So the following assignment might be wrong. */
  734. status.freq = b43_channel_to_freq_5ghz(chanid);
  735. break;
  736. case B43_PHYTYPE_G:
  737. status.band = IEEE80211_BAND_2GHZ;
  738. /* chanid is the radio channel cookie value as used
  739. * to tune the radio. */
  740. status.freq = chanid + 2400;
  741. break;
  742. case B43_PHYTYPE_N:
  743. case B43_PHYTYPE_LP:
  744. case B43_PHYTYPE_HT:
  745. /* chanid is the SHM channel cookie. Which is the plain
  746. * channel number in b43. */
  747. if (chanstat & B43_RX_CHAN_5GHZ) {
  748. status.band = IEEE80211_BAND_5GHZ;
  749. status.freq = b43_freq_to_channel_5ghz(chanid);
  750. } else {
  751. status.band = IEEE80211_BAND_2GHZ;
  752. status.freq = b43_freq_to_channel_2ghz(chanid);
  753. }
  754. break;
  755. default:
  756. B43_WARN_ON(1);
  757. goto drop;
  758. }
  759. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  760. ieee80211_rx_ni(dev->wl->hw, skb);
  761. #if B43_DEBUG
  762. dev->rx_count++;
  763. #endif
  764. return;
  765. drop:
  766. dev_kfree_skb_any(skb);
  767. }
  768. void b43_handle_txstatus(struct b43_wldev *dev,
  769. const struct b43_txstatus *status)
  770. {
  771. b43_debugfs_log_txstat(dev, status);
  772. if (status->intermediate)
  773. return;
  774. if (status->for_ampdu)
  775. return;
  776. if (!status->acked)
  777. dev->wl->ieee_stats.dot11ACKFailureCount++;
  778. if (status->rts_count) {
  779. if (status->rts_count == 0xF) //FIXME
  780. dev->wl->ieee_stats.dot11RTSFailureCount++;
  781. else
  782. dev->wl->ieee_stats.dot11RTSSuccessCount++;
  783. }
  784. if (b43_using_pio_transfers(dev))
  785. b43_pio_handle_txstatus(dev, status);
  786. else
  787. b43_dma_handle_txstatus(dev, status);
  788. b43_phy_txpower_check(dev, 0);
  789. }
  790. /* Fill out the mac80211 TXstatus report based on the b43-specific
  791. * txstatus report data. This returns a boolean whether the frame was
  792. * successfully transmitted. */
  793. bool b43_fill_txstatus_report(struct b43_wldev *dev,
  794. struct ieee80211_tx_info *report,
  795. const struct b43_txstatus *status)
  796. {
  797. bool frame_success = true;
  798. int retry_limit;
  799. /* preserve the confiured retry limit before clearing the status
  800. * The xmit function has overwritten the rc's value with the actual
  801. * retry limit done by the hardware */
  802. retry_limit = report->status.rates[0].count;
  803. ieee80211_tx_info_clear_status(report);
  804. if (status->acked) {
  805. /* The frame was ACKed. */
  806. report->flags |= IEEE80211_TX_STAT_ACK;
  807. } else {
  808. /* The frame was not ACKed... */
  809. if (!(report->flags & IEEE80211_TX_CTL_NO_ACK)) {
  810. /* ...but we expected an ACK. */
  811. frame_success = false;
  812. }
  813. }
  814. if (status->frame_count == 0) {
  815. /* The frame was not transmitted at all. */
  816. report->status.rates[0].count = 0;
  817. } else if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
  818. /*
  819. * If the short retries (RTS, not data frame) have exceeded
  820. * the limit, the hw will not have tried the selected rate,
  821. * but will have used the fallback rate instead.
  822. * Don't let the rate control count attempts for the selected
  823. * rate in this case, otherwise the statistics will be off.
  824. */
  825. report->status.rates[0].count = 0;
  826. report->status.rates[1].count = status->frame_count;
  827. } else {
  828. if (status->frame_count > retry_limit) {
  829. report->status.rates[0].count = retry_limit;
  830. report->status.rates[1].count = status->frame_count -
  831. retry_limit;
  832. } else {
  833. report->status.rates[0].count = status->frame_count;
  834. report->status.rates[1].idx = -1;
  835. }
  836. }
  837. return frame_success;
  838. }
  839. /* Stop any TX operation on the device (suspend the hardware queues) */
  840. void b43_tx_suspend(struct b43_wldev *dev)
  841. {
  842. if (b43_using_pio_transfers(dev))
  843. b43_pio_tx_suspend(dev);
  844. else
  845. b43_dma_tx_suspend(dev);
  846. }
  847. /* Resume any TX operation on the device (resume the hardware queues) */
  848. void b43_tx_resume(struct b43_wldev *dev)
  849. {
  850. if (b43_using_pio_transfers(dev))
  851. b43_pio_tx_resume(dev);
  852. else
  853. b43_dma_tx_resume(dev);
  854. }