xmit.c 25 KB

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