xmit.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Broadcom B43 wireless driver
  4. Transmission (TX/RX) related functions.
  5. Copyright (C) 2005 Martin Langer <[email protected]>
  6. Copyright (C) 2005 Stefano Brivio <[email protected]>
  7. Copyright (C) 2005, 2006 Michael Buesch <[email protected]>
  8. Copyright (C) 2005 Danny van Dyk <[email protected]>
  9. Copyright (C) 2005 Andreas Jaggi <[email protected]>
  10. */
  11. #include "xmit.h"
  12. #include "phy_common.h"
  13. #include "dma.h"
  14. #include "pio.h"
  15. static const struct b43_tx_legacy_rate_phy_ctl_entry b43_tx_legacy_rate_phy_ctl[] = {
  16. { B43_CCK_RATE_1MB, 0x0, 0x0 },
  17. { B43_CCK_RATE_2MB, 0x0, 0x1 },
  18. { B43_CCK_RATE_5MB, 0x0, 0x2 },
  19. { B43_CCK_RATE_11MB, 0x0, 0x3 },
  20. { B43_OFDM_RATE_6MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_BPSK },
  21. { B43_OFDM_RATE_9MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_BPSK },
  22. { B43_OFDM_RATE_12MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_QPSK },
  23. { B43_OFDM_RATE_18MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QPSK },
  24. { B43_OFDM_RATE_24MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_QAM16 },
  25. { B43_OFDM_RATE_36MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QAM16 },
  26. { B43_OFDM_RATE_48MB, B43_TXH_PHY1_CRATE_2_3, B43_TXH_PHY1_MODUL_QAM64 },
  27. { B43_OFDM_RATE_54MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QAM64 },
  28. };
  29. static const struct b43_tx_legacy_rate_phy_ctl_entry *
  30. b43_tx_legacy_rate_phy_ctl_ent(u8 bitrate)
  31. {
  32. const struct b43_tx_legacy_rate_phy_ctl_entry *e;
  33. unsigned int i;
  34. for (i = 0; i < ARRAY_SIZE(b43_tx_legacy_rate_phy_ctl); i++) {
  35. e = &(b43_tx_legacy_rate_phy_ctl[i]);
  36. if (e->bitrate == bitrate)
  37. return e;
  38. }
  39. B43_WARN_ON(1);
  40. return NULL;
  41. }
  42. /* Extract the bitrate index out of a CCK PLCP header. */
  43. static int b43_plcp_get_bitrate_idx_cck(struct b43_plcp_hdr6 *plcp)
  44. {
  45. switch (plcp->raw[0]) {
  46. case 0x0A:
  47. return 0;
  48. case 0x14:
  49. return 1;
  50. case 0x37:
  51. return 2;
  52. case 0x6E:
  53. return 3;
  54. }
  55. return -1;
  56. }
  57. /* Extract the bitrate index out of an OFDM PLCP header. */
  58. static int b43_plcp_get_bitrate_idx_ofdm(struct b43_plcp_hdr6 *plcp, bool ghz5)
  59. {
  60. /* For 2 GHz band first OFDM rate is at index 4, see main.c */
  61. int base = ghz5 ? 0 : 4;
  62. switch (plcp->raw[0] & 0xF) {
  63. case 0xB:
  64. return base + 0;
  65. case 0xF:
  66. return base + 1;
  67. case 0xA:
  68. return base + 2;
  69. case 0xE:
  70. return base + 3;
  71. case 0x9:
  72. return base + 4;
  73. case 0xD:
  74. return base + 5;
  75. case 0x8:
  76. return base + 6;
  77. case 0xC:
  78. return base + 7;
  79. }
  80. return -1;
  81. }
  82. u8 b43_plcp_get_ratecode_cck(const u8 bitrate)
  83. {
  84. switch (bitrate) {
  85. case B43_CCK_RATE_1MB:
  86. return 0x0A;
  87. case B43_CCK_RATE_2MB:
  88. return 0x14;
  89. case B43_CCK_RATE_5MB:
  90. return 0x37;
  91. case B43_CCK_RATE_11MB:
  92. return 0x6E;
  93. }
  94. B43_WARN_ON(1);
  95. return 0;
  96. }
  97. u8 b43_plcp_get_ratecode_ofdm(const u8 bitrate)
  98. {
  99. switch (bitrate) {
  100. case B43_OFDM_RATE_6MB:
  101. return 0xB;
  102. case B43_OFDM_RATE_9MB:
  103. return 0xF;
  104. case B43_OFDM_RATE_12MB:
  105. return 0xA;
  106. case B43_OFDM_RATE_18MB:
  107. return 0xE;
  108. case B43_OFDM_RATE_24MB:
  109. return 0x9;
  110. case B43_OFDM_RATE_36MB:
  111. return 0xD;
  112. case B43_OFDM_RATE_48MB:
  113. return 0x8;
  114. case B43_OFDM_RATE_54MB:
  115. return 0xC;
  116. }
  117. B43_WARN_ON(1);
  118. return 0;
  119. }
  120. void b43_generate_plcp_hdr(struct b43_plcp_hdr4 *plcp,
  121. const u16 octets, const u8 bitrate)
  122. {
  123. __u8 *raw = plcp->raw;
  124. if (b43_is_ofdm_rate(bitrate)) {
  125. u32 d;
  126. d = b43_plcp_get_ratecode_ofdm(bitrate);
  127. B43_WARN_ON(octets & 0xF000);
  128. d |= (octets << 5);
  129. plcp->data = cpu_to_le32(d);
  130. } else {
  131. u32 plen;
  132. plen = octets * 16 / bitrate;
  133. if ((octets * 16 % bitrate) > 0) {
  134. plen++;
  135. if ((bitrate == B43_CCK_RATE_11MB)
  136. && ((octets * 8 % 11) < 4)) {
  137. raw[1] = 0x84;
  138. } else
  139. raw[1] = 0x04;
  140. } else
  141. raw[1] = 0x04;
  142. plcp->data |= cpu_to_le32(plen << 16);
  143. raw[0] = b43_plcp_get_ratecode_cck(bitrate);
  144. }
  145. }
  146. /* TODO: verify if needed for SSLPN or LCN */
  147. static u16 b43_generate_tx_phy_ctl1(struct b43_wldev *dev, u8 bitrate)
  148. {
  149. const struct b43_phy *phy = &dev->phy;
  150. const struct b43_tx_legacy_rate_phy_ctl_entry *e;
  151. u16 control = 0;
  152. u16 bw;
  153. if (phy->type == B43_PHYTYPE_LP)
  154. bw = B43_TXH_PHY1_BW_20;
  155. else /* FIXME */
  156. bw = B43_TXH_PHY1_BW_20;
  157. if (0) { /* FIXME: MIMO */
  158. } else if (b43_is_cck_rate(bitrate) && phy->type != B43_PHYTYPE_LP) {
  159. control = bw;
  160. } else {
  161. control = bw;
  162. e = b43_tx_legacy_rate_phy_ctl_ent(bitrate);
  163. if (e) {
  164. control |= e->coding_rate;
  165. control |= e->modulation;
  166. }
  167. control |= B43_TXH_PHY1_MODE_SISO;
  168. }
  169. return control;
  170. }
  171. static u8 b43_calc_fallback_rate(u8 bitrate, int gmode)
  172. {
  173. switch (bitrate) {
  174. case B43_CCK_RATE_1MB:
  175. return B43_CCK_RATE_1MB;
  176. case B43_CCK_RATE_2MB:
  177. return B43_CCK_RATE_1MB;
  178. case B43_CCK_RATE_5MB:
  179. return B43_CCK_RATE_2MB;
  180. case B43_CCK_RATE_11MB:
  181. return B43_CCK_RATE_5MB;
  182. /*
  183. * Don't just fallback to CCK; it may be in 5GHz operation
  184. * and falling back to CCK won't work out very well.
  185. */
  186. case B43_OFDM_RATE_6MB:
  187. if (gmode)
  188. return B43_CCK_RATE_5MB;
  189. else
  190. return B43_OFDM_RATE_6MB;
  191. case B43_OFDM_RATE_9MB:
  192. return B43_OFDM_RATE_6MB;
  193. case B43_OFDM_RATE_12MB:
  194. return B43_OFDM_RATE_9MB;
  195. case B43_OFDM_RATE_18MB:
  196. return B43_OFDM_RATE_12MB;
  197. case B43_OFDM_RATE_24MB:
  198. return B43_OFDM_RATE_18MB;
  199. case B43_OFDM_RATE_36MB:
  200. return B43_OFDM_RATE_24MB;
  201. case B43_OFDM_RATE_48MB:
  202. return B43_OFDM_RATE_36MB;
  203. case B43_OFDM_RATE_54MB:
  204. return B43_OFDM_RATE_48MB;
  205. }
  206. B43_WARN_ON(1);
  207. return 0;
  208. }
  209. /* Generate a TX data header. */
  210. int b43_generate_txhdr(struct b43_wldev *dev,
  211. u8 *_txhdr,
  212. struct sk_buff *skb_frag,
  213. struct ieee80211_tx_info *info,
  214. u16 cookie)
  215. {
  216. const unsigned char *fragment_data = skb_frag->data;
  217. unsigned int fragment_len = skb_frag->len;
  218. struct b43_txhdr *txhdr = (struct b43_txhdr *)_txhdr;
  219. const struct b43_phy *phy = &dev->phy;
  220. const struct ieee80211_hdr *wlhdr =
  221. (const struct ieee80211_hdr *)fragment_data;
  222. int use_encryption = !!info->control.hw_key;
  223. __le16 fctl = wlhdr->frame_control;
  224. struct ieee80211_rate *fbrate;
  225. u8 rate, rate_fb;
  226. int rate_ofdm, rate_fb_ofdm;
  227. unsigned int plcp_fragment_len;
  228. u32 mac_ctl = 0;
  229. u16 phy_ctl = 0;
  230. bool fill_phy_ctl1 = (phy->type == B43_PHYTYPE_LP ||
  231. phy->type == B43_PHYTYPE_N ||
  232. phy->type == B43_PHYTYPE_HT);
  233. u8 extra_ft = 0;
  234. struct ieee80211_rate *txrate;
  235. struct ieee80211_tx_rate *rates;
  236. memset(txhdr, 0, sizeof(*txhdr));
  237. txrate = ieee80211_get_tx_rate(dev->wl->hw, info);
  238. rate = txrate ? txrate->hw_value : B43_CCK_RATE_1MB;
  239. rate_ofdm = b43_is_ofdm_rate(rate);
  240. fbrate = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : txrate;
  241. rate_fb = fbrate->hw_value;
  242. rate_fb_ofdm = b43_is_ofdm_rate(rate_fb);
  243. if (rate_ofdm)
  244. txhdr->phy_rate = b43_plcp_get_ratecode_ofdm(rate);
  245. else
  246. txhdr->phy_rate = b43_plcp_get_ratecode_cck(rate);
  247. txhdr->mac_frame_ctl = wlhdr->frame_control;
  248. memcpy(txhdr->tx_receiver, wlhdr->addr1, ETH_ALEN);
  249. /* Calculate duration for fallback rate */
  250. if ((rate_fb == rate) ||
  251. (wlhdr->duration_id & cpu_to_le16(0x8000)) ||
  252. (wlhdr->duration_id == cpu_to_le16(0))) {
  253. /* If the fallback rate equals the normal rate or the
  254. * dur_id field contains an AID, CFP magic or 0,
  255. * use the original dur_id field. */
  256. txhdr->dur_fb = wlhdr->duration_id;
  257. } else {
  258. txhdr->dur_fb = ieee80211_generic_frame_duration(
  259. dev->wl->hw, info->control.vif, info->band,
  260. fragment_len, fbrate);
  261. }
  262. plcp_fragment_len = fragment_len + FCS_LEN;
  263. if (use_encryption) {
  264. u8 key_idx = info->control.hw_key->hw_key_idx;
  265. struct b43_key *key;
  266. int wlhdr_len;
  267. size_t iv_len;
  268. B43_WARN_ON(key_idx >= ARRAY_SIZE(dev->key));
  269. key = &(dev->key[key_idx]);
  270. if (unlikely(!key->keyconf)) {
  271. /* This key is invalid. This might only happen
  272. * in a short timeframe after machine resume before
  273. * we were able to reconfigure keys.
  274. * Drop this packet completely. Do not transmit it
  275. * unencrypted to avoid leaking information. */
  276. return -ENOKEY;
  277. }
  278. /* Hardware appends ICV. */
  279. plcp_fragment_len += info->control.hw_key->icv_len;
  280. key_idx = b43_kidx_to_fw(dev, key_idx);
  281. mac_ctl |= (key_idx << B43_TXH_MAC_KEYIDX_SHIFT) &
  282. B43_TXH_MAC_KEYIDX;
  283. mac_ctl |= (key->algorithm << B43_TXH_MAC_KEYALG_SHIFT) &
  284. B43_TXH_MAC_KEYALG;
  285. wlhdr_len = ieee80211_hdrlen(fctl);
  286. if (key->algorithm == B43_SEC_ALGO_TKIP) {
  287. u16 phase1key[5];
  288. int i;
  289. /* we give the phase1key and iv16 here, the key is stored in
  290. * shm. With that the hardware can do phase 2 and encryption.
  291. */
  292. ieee80211_get_tkip_p1k(info->control.hw_key, skb_frag, phase1key);
  293. /* phase1key is in host endian. Copy to little-endian txhdr->iv. */
  294. for (i = 0; i < 5; i++) {
  295. txhdr->iv[i * 2 + 0] = phase1key[i];
  296. txhdr->iv[i * 2 + 1] = phase1key[i] >> 8;
  297. }
  298. /* iv16 */
  299. memcpy(txhdr->iv + 10, ((u8 *) wlhdr) + wlhdr_len, 3);
  300. } else {
  301. iv_len = min_t(size_t, info->control.hw_key->iv_len,
  302. ARRAY_SIZE(txhdr->iv));
  303. memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
  304. }
  305. }
  306. switch (dev->fw.hdr_format) {
  307. case B43_FW_HDR_598:
  308. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_598.plcp),
  309. plcp_fragment_len, rate);
  310. break;
  311. case B43_FW_HDR_351:
  312. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_351.plcp),
  313. plcp_fragment_len, rate);
  314. break;
  315. case B43_FW_HDR_410:
  316. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_410.plcp),
  317. plcp_fragment_len, rate);
  318. break;
  319. }
  320. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
  321. plcp_fragment_len, rate_fb);
  322. /* Extra Frame Types */
  323. if (rate_fb_ofdm)
  324. extra_ft |= B43_TXH_EFT_FB_OFDM;
  325. else
  326. extra_ft |= B43_TXH_EFT_FB_CCK;
  327. /* Set channel radio code. Note that the micrcode ORs 0x100 to
  328. * this value before comparing it to the value in SHM, if this
  329. * is a 5Ghz packet.
  330. */
  331. txhdr->chan_radio_code = phy->channel;
  332. /* PHY TX Control word */
  333. if (rate_ofdm)
  334. phy_ctl |= B43_TXH_PHY_ENC_OFDM;
  335. else
  336. phy_ctl |= B43_TXH_PHY_ENC_CCK;
  337. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  338. phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
  339. switch (b43_ieee80211_antenna_sanitize(dev, 0)) {
  340. case 0: /* Default */
  341. phy_ctl |= B43_TXH_PHY_ANT01AUTO;
  342. break;
  343. case 1: /* Antenna 0 */
  344. phy_ctl |= B43_TXH_PHY_ANT0;
  345. break;
  346. case 2: /* Antenna 1 */
  347. phy_ctl |= B43_TXH_PHY_ANT1;
  348. break;
  349. case 3: /* Antenna 2 */
  350. phy_ctl |= B43_TXH_PHY_ANT2;
  351. break;
  352. case 4: /* Antenna 3 */
  353. phy_ctl |= B43_TXH_PHY_ANT3;
  354. break;
  355. default:
  356. B43_WARN_ON(1);
  357. }
  358. rates = info->control.rates;
  359. /* MAC control */
  360. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  361. mac_ctl |= B43_TXH_MAC_ACK;
  362. /* use hardware sequence counter as the non-TID counter */
  363. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  364. mac_ctl |= B43_TXH_MAC_HWSEQ;
  365. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  366. mac_ctl |= B43_TXH_MAC_STMSDU;
  367. if (!phy->gmode)
  368. mac_ctl |= B43_TXH_MAC_5GHZ;
  369. /* Overwrite rates[0].count to make the retry calculation
  370. * in the tx status easier. need the actual retry limit to
  371. * detect whether the fallback rate was used.
  372. */
  373. if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  374. (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
  375. rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
  376. mac_ctl |= B43_TXH_MAC_LONGFRAME;
  377. } else {
  378. rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
  379. }
  380. /* Generate the RTS or CTS-to-self frame */
  381. if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
  382. (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
  383. unsigned int len;
  384. struct ieee80211_hdr *hdr;
  385. int rts_rate, rts_rate_fb;
  386. int rts_rate_ofdm, rts_rate_fb_ofdm;
  387. struct b43_plcp_hdr6 *plcp;
  388. struct ieee80211_rate *rts_cts_rate;
  389. rts_cts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info);
  390. rts_rate = rts_cts_rate ? rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
  391. rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
  392. rts_rate_fb = b43_calc_fallback_rate(rts_rate, phy->gmode);
  393. rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
  394. if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  395. struct ieee80211_cts *cts;
  396. switch (dev->fw.hdr_format) {
  397. case B43_FW_HDR_598:
  398. cts = (struct ieee80211_cts *)
  399. (txhdr->format_598.rts_frame);
  400. break;
  401. case B43_FW_HDR_351:
  402. cts = (struct ieee80211_cts *)
  403. (txhdr->format_351.rts_frame);
  404. break;
  405. case B43_FW_HDR_410:
  406. cts = (struct ieee80211_cts *)
  407. (txhdr->format_410.rts_frame);
  408. break;
  409. }
  410. ieee80211_ctstoself_get(dev->wl->hw, info->control.vif,
  411. fragment_data, fragment_len,
  412. info, cts);
  413. mac_ctl |= B43_TXH_MAC_SENDCTS;
  414. len = sizeof(struct ieee80211_cts);
  415. } else {
  416. struct ieee80211_rts *rts;
  417. switch (dev->fw.hdr_format) {
  418. case B43_FW_HDR_598:
  419. rts = (struct ieee80211_rts *)
  420. (txhdr->format_598.rts_frame);
  421. break;
  422. case B43_FW_HDR_351:
  423. rts = (struct ieee80211_rts *)
  424. (txhdr->format_351.rts_frame);
  425. break;
  426. case B43_FW_HDR_410:
  427. rts = (struct ieee80211_rts *)
  428. (txhdr->format_410.rts_frame);
  429. break;
  430. }
  431. ieee80211_rts_get(dev->wl->hw, info->control.vif,
  432. fragment_data, fragment_len,
  433. info, rts);
  434. mac_ctl |= B43_TXH_MAC_SENDRTS;
  435. len = sizeof(struct ieee80211_rts);
  436. }
  437. len += FCS_LEN;
  438. /* Generate the PLCP headers for the RTS/CTS frame */
  439. switch (dev->fw.hdr_format) {
  440. case B43_FW_HDR_598:
  441. plcp = &txhdr->format_598.rts_plcp;
  442. break;
  443. case B43_FW_HDR_351:
  444. plcp = &txhdr->format_351.rts_plcp;
  445. break;
  446. case B43_FW_HDR_410:
  447. plcp = &txhdr->format_410.rts_plcp;
  448. break;
  449. }
  450. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
  451. len, rts_rate);
  452. plcp = &txhdr->rts_plcp_fb;
  453. b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
  454. len, rts_rate_fb);
  455. switch (dev->fw.hdr_format) {
  456. case B43_FW_HDR_598:
  457. hdr = (struct ieee80211_hdr *)
  458. (&txhdr->format_598.rts_frame);
  459. break;
  460. case B43_FW_HDR_351:
  461. hdr = (struct ieee80211_hdr *)
  462. (&txhdr->format_351.rts_frame);
  463. break;
  464. case B43_FW_HDR_410:
  465. hdr = (struct ieee80211_hdr *)
  466. (&txhdr->format_410.rts_frame);
  467. break;
  468. }
  469. txhdr->rts_dur_fb = hdr->duration_id;
  470. if (rts_rate_ofdm) {
  471. extra_ft |= B43_TXH_EFT_RTS_OFDM;
  472. txhdr->phy_rate_rts =
  473. b43_plcp_get_ratecode_ofdm(rts_rate);
  474. } else {
  475. extra_ft |= B43_TXH_EFT_RTS_CCK;
  476. txhdr->phy_rate_rts =
  477. b43_plcp_get_ratecode_cck(rts_rate);
  478. }
  479. if (rts_rate_fb_ofdm)
  480. extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
  481. else
  482. extra_ft |= B43_TXH_EFT_RTSFB_CCK;
  483. if (rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS &&
  484. fill_phy_ctl1) {
  485. txhdr->phy_ctl1_rts = cpu_to_le16(
  486. b43_generate_tx_phy_ctl1(dev, rts_rate));
  487. txhdr->phy_ctl1_rts_fb = cpu_to_le16(
  488. b43_generate_tx_phy_ctl1(dev, rts_rate_fb));
  489. }
  490. }
  491. /* Magic cookie */
  492. switch (dev->fw.hdr_format) {
  493. case B43_FW_HDR_598:
  494. txhdr->format_598.cookie = cpu_to_le16(cookie);
  495. break;
  496. case B43_FW_HDR_351:
  497. txhdr->format_351.cookie = cpu_to_le16(cookie);
  498. break;
  499. case B43_FW_HDR_410:
  500. txhdr->format_410.cookie = cpu_to_le16(cookie);
  501. break;
  502. }
  503. if (fill_phy_ctl1) {
  504. txhdr->phy_ctl1 =
  505. cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate));
  506. txhdr->phy_ctl1_fb =
  507. cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate_fb));
  508. }
  509. /* Apply the bitfields */
  510. txhdr->mac_ctl = cpu_to_le32(mac_ctl);
  511. txhdr->phy_ctl = cpu_to_le16(phy_ctl);
  512. txhdr->extra_ft = extra_ft;
  513. return 0;
  514. }
  515. static s8 b43_rssi_postprocess(struct b43_wldev *dev,
  516. u8 in_rssi, int ofdm,
  517. int adjust_2053, int adjust_2050)
  518. {
  519. struct b43_phy *phy = &dev->phy;
  520. struct b43_phy_g *gphy = phy->g;
  521. s32 tmp;
  522. switch (phy->radio_ver) {
  523. case 0x2050:
  524. if (ofdm) {
  525. tmp = in_rssi;
  526. if (tmp > 127)
  527. tmp -= 256;
  528. tmp *= 73;
  529. tmp /= 64;
  530. if (adjust_2050)
  531. tmp += 25;
  532. else
  533. tmp -= 3;
  534. } else {
  535. if (dev->dev->bus_sprom->
  536. boardflags_lo & B43_BFL_RSSI) {
  537. if (in_rssi > 63)
  538. in_rssi = 63;
  539. B43_WARN_ON(phy->type != B43_PHYTYPE_G);
  540. tmp = gphy->nrssi_lt[in_rssi];
  541. tmp = 31 - tmp;
  542. tmp *= -131;
  543. tmp /= 128;
  544. tmp -= 57;
  545. } else {
  546. tmp = in_rssi;
  547. tmp = 31 - tmp;
  548. tmp *= -149;
  549. tmp /= 128;
  550. tmp -= 68;
  551. }
  552. if (phy->type == B43_PHYTYPE_G && adjust_2050)
  553. tmp += 25;
  554. }
  555. break;
  556. case 0x2060:
  557. if (in_rssi > 127)
  558. tmp = in_rssi - 256;
  559. else
  560. tmp = in_rssi;
  561. break;
  562. default:
  563. tmp = in_rssi;
  564. tmp -= 11;
  565. tmp *= 103;
  566. tmp /= 64;
  567. if (adjust_2053)
  568. tmp -= 109;
  569. else
  570. tmp -= 83;
  571. }
  572. return (s8) tmp;
  573. }
  574. void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
  575. {
  576. struct ieee80211_rx_status status;
  577. struct b43_plcp_hdr6 *plcp;
  578. struct ieee80211_hdr *wlhdr;
  579. const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
  580. __le16 fctl;
  581. u16 phystat0, phystat3;
  582. u16 chanstat, mactime;
  583. u32 macstat;
  584. u16 chanid;
  585. int padding, rate_idx;
  586. memset(&status, 0, sizeof(status));
  587. /* Get metadata about the frame from the header. */
  588. phystat0 = le16_to_cpu(rxhdr->phy_status0);
  589. phystat3 = le16_to_cpu(rxhdr->phy_status3);
  590. switch (dev->fw.hdr_format) {
  591. case B43_FW_HDR_598:
  592. macstat = le32_to_cpu(rxhdr->format_598.mac_status);
  593. mactime = le16_to_cpu(rxhdr->format_598.mac_time);
  594. chanstat = le16_to_cpu(rxhdr->format_598.channel);
  595. break;
  596. case B43_FW_HDR_410:
  597. case B43_FW_HDR_351:
  598. macstat = le32_to_cpu(rxhdr->format_351.mac_status);
  599. mactime = le16_to_cpu(rxhdr->format_351.mac_time);
  600. chanstat = le16_to_cpu(rxhdr->format_351.channel);
  601. break;
  602. }
  603. if (unlikely(macstat & B43_RX_MAC_FCSERR)) {
  604. dev->wl->ieee_stats.dot11FCSErrorCount++;
  605. status.flag |= RX_FLAG_FAILED_FCS_CRC;
  606. }
  607. if (unlikely(phystat0 & (B43_RX_PHYST0_PLCPHCF | B43_RX_PHYST0_PLCPFV)))
  608. status.flag |= RX_FLAG_FAILED_PLCP_CRC;
  609. if (phystat0 & B43_RX_PHYST0_SHORTPRMBL)
  610. status.enc_flags |= RX_ENC_FLAG_SHORTPRE;
  611. if (macstat & B43_RX_MAC_DECERR) {
  612. /* Decryption with the given key failed.
  613. * Drop the packet. We also won't be able to decrypt it with
  614. * the key in software. */
  615. goto drop;
  616. }
  617. /* Skip PLCP and padding */
  618. padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
  619. if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
  620. b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
  621. goto drop;
  622. }
  623. plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
  624. skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
  625. /* The skb contains the Wireless Header + payload data now */
  626. if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
  627. b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
  628. goto drop;
  629. }
  630. wlhdr = (struct ieee80211_hdr *)(skb->data);
  631. fctl = wlhdr->frame_control;
  632. if (macstat & B43_RX_MAC_DEC) {
  633. unsigned int keyidx;
  634. int wlhdr_len;
  635. keyidx = ((macstat & B43_RX_MAC_KEYIDX)
  636. >> B43_RX_MAC_KEYIDX_SHIFT);
  637. /* We must adjust the key index here. We want the "physical"
  638. * key index, but the ucode passed it slightly different.
  639. */
  640. keyidx = b43_kidx_to_raw(dev, keyidx);
  641. B43_WARN_ON(keyidx >= ARRAY_SIZE(dev->key));
  642. if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
  643. wlhdr_len = ieee80211_hdrlen(fctl);
  644. if (unlikely(skb->len < (wlhdr_len + 3))) {
  645. b43dbg(dev->wl,
  646. "RX: Packet size underrun (3)\n");
  647. goto drop;
  648. }
  649. status.flag |= RX_FLAG_DECRYPTED;
  650. }
  651. }
  652. /* Link quality statistics */
  653. switch (chanstat & B43_RX_CHAN_PHYTYPE) {
  654. case B43_PHYTYPE_HT:
  655. /* TODO: is max the right choice? */
  656. status.signal = max_t(__s8,
  657. max(rxhdr->phy_ht_power0, rxhdr->phy_ht_power1),
  658. rxhdr->phy_ht_power2);
  659. break;
  660. case B43_PHYTYPE_N:
  661. /* Broadcom has code for min and avg, but always uses max */
  662. if (rxhdr->power0 == 16 || rxhdr->power0 == 32)
  663. status.signal = max(rxhdr->power1, rxhdr->power2);
  664. else
  665. status.signal = max(rxhdr->power0, rxhdr->power1);
  666. break;
  667. case B43_PHYTYPE_B:
  668. case B43_PHYTYPE_G:
  669. case B43_PHYTYPE_LP:
  670. status.signal = b43_rssi_postprocess(dev, rxhdr->jssi,
  671. (phystat0 & B43_RX_PHYST0_OFDM),
  672. (phystat0 & B43_RX_PHYST0_GAINCTL),
  673. (phystat3 & B43_RX_PHYST3_TRSTATE));
  674. break;
  675. }
  676. if (phystat0 & B43_RX_PHYST0_OFDM)
  677. rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
  678. !!(chanstat & B43_RX_CHAN_5GHZ));
  679. else
  680. rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
  681. if (unlikely(rate_idx == -1)) {
  682. /* PLCP seems to be corrupted.
  683. * Drop the frame, if we are not interested in corrupted frames. */
  684. if (!(dev->wl->filter_flags & FIF_PLCPFAIL))
  685. goto drop;
  686. }
  687. status.rate_idx = rate_idx;
  688. status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
  689. /*
  690. * All frames on monitor interfaces and beacons always need a full
  691. * 64-bit timestamp. Monitor interfaces need it for diagnostic
  692. * purposes and beacons for IBSS merging.
  693. * This code assumes we get to process the packet within 16 bits
  694. * of timestamp, i.e. about 65 milliseconds after the PHY received
  695. * the first symbol.
  696. */
  697. if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
  698. u16 low_mactime_now;
  699. b43_tsf_read(dev, &status.mactime);
  700. low_mactime_now = status.mactime;
  701. status.mactime = status.mactime & ~0xFFFFULL;
  702. status.mactime += mactime;
  703. if (low_mactime_now <= mactime)
  704. status.mactime -= 0x10000;
  705. status.flag |= RX_FLAG_MACTIME_START;
  706. }
  707. chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
  708. switch (chanstat & B43_RX_CHAN_PHYTYPE) {
  709. case B43_PHYTYPE_G:
  710. status.band = NL80211_BAND_2GHZ;
  711. /* Somewhere between 478.104 and 508.1084 firmware for G-PHY
  712. * has been modified to be compatible with N-PHY and others.
  713. */
  714. if (dev->fw.rev >= 508)
  715. status.freq = ieee80211_channel_to_frequency(chanid, status.band);
  716. else
  717. status.freq = chanid + 2400;
  718. break;
  719. case B43_PHYTYPE_N:
  720. case B43_PHYTYPE_LP:
  721. case B43_PHYTYPE_HT:
  722. /* chanid is the SHM channel cookie. Which is the plain
  723. * channel number in b43. */
  724. if (chanstat & B43_RX_CHAN_5GHZ)
  725. status.band = NL80211_BAND_5GHZ;
  726. else
  727. status.band = NL80211_BAND_2GHZ;
  728. status.freq =
  729. ieee80211_channel_to_frequency(chanid, status.band);
  730. break;
  731. default:
  732. B43_WARN_ON(1);
  733. goto drop;
  734. }
  735. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  736. ieee80211_rx_ni(dev->wl->hw, skb);
  737. #if B43_DEBUG
  738. dev->rx_count++;
  739. #endif
  740. return;
  741. drop:
  742. dev_kfree_skb_any(skb);
  743. }
  744. void b43_handle_txstatus(struct b43_wldev *dev,
  745. const struct b43_txstatus *status)
  746. {
  747. b43_debugfs_log_txstat(dev, status);
  748. if (status->intermediate)
  749. return;
  750. if (status->for_ampdu)
  751. return;
  752. if (!status->acked)
  753. dev->wl->ieee_stats.dot11ACKFailureCount++;
  754. if (status->rts_count) {
  755. if (status->rts_count == 0xF) //FIXME
  756. dev->wl->ieee_stats.dot11RTSFailureCount++;
  757. else
  758. dev->wl->ieee_stats.dot11RTSSuccessCount++;
  759. }
  760. if (b43_using_pio_transfers(dev))
  761. b43_pio_handle_txstatus(dev, status);
  762. else
  763. b43_dma_handle_txstatus(dev, status);
  764. b43_phy_txpower_check(dev, 0);
  765. }
  766. /* Fill out the mac80211 TXstatus report based on the b43-specific
  767. * txstatus report data. This returns a boolean whether the frame was
  768. * successfully transmitted. */
  769. bool b43_fill_txstatus_report(struct b43_wldev *dev,
  770. struct ieee80211_tx_info *report,
  771. const struct b43_txstatus *status)
  772. {
  773. bool frame_success = true;
  774. int retry_limit;
  775. /* preserve the confiured retry limit before clearing the status
  776. * The xmit function has overwritten the rc's value with the actual
  777. * retry limit done by the hardware */
  778. retry_limit = report->status.rates[0].count;
  779. ieee80211_tx_info_clear_status(report);
  780. if (status->acked) {
  781. /* The frame was ACKed. */
  782. report->flags |= IEEE80211_TX_STAT_ACK;
  783. } else {
  784. /* The frame was not ACKed... */
  785. if (!(report->flags & IEEE80211_TX_CTL_NO_ACK)) {
  786. /* ...but we expected an ACK. */
  787. frame_success = false;
  788. }
  789. }
  790. if (status->frame_count == 0) {
  791. /* The frame was not transmitted at all. */
  792. report->status.rates[0].count = 0;
  793. } else if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
  794. /*
  795. * If the short retries (RTS, not data frame) have exceeded
  796. * the limit, the hw will not have tried the selected rate,
  797. * but will have used the fallback rate instead.
  798. * Don't let the rate control count attempts for the selected
  799. * rate in this case, otherwise the statistics will be off.
  800. */
  801. report->status.rates[0].count = 0;
  802. report->status.rates[1].count = status->frame_count;
  803. } else {
  804. if (status->frame_count > retry_limit) {
  805. report->status.rates[0].count = retry_limit;
  806. report->status.rates[1].count = status->frame_count -
  807. retry_limit;
  808. } else {
  809. report->status.rates[0].count = status->frame_count;
  810. report->status.rates[1].idx = -1;
  811. }
  812. }
  813. return frame_success;
  814. }
  815. /* Stop any TX operation on the device (suspend the hardware queues) */
  816. void b43_tx_suspend(struct b43_wldev *dev)
  817. {
  818. if (b43_using_pio_transfers(dev))
  819. b43_pio_tx_suspend(dev);
  820. else
  821. b43_dma_tx_suspend(dev);
  822. }
  823. /* Resume any TX operation on the device (resume the hardware queues) */
  824. void b43_tx_resume(struct b43_wldev *dev)
  825. {
  826. if (b43_using_pio_transfers(dev))
  827. b43_pio_tx_resume(dev);
  828. else
  829. b43_dma_tx_resume(dev);
  830. }