xmit.c 26 KB

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