xmit.c 21 KB

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