xmit.c 21 KB

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