xmit.c 22 KB

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