xmit.c 21 KB

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