xmit.c 19 KB

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