status.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2008-2010 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/export.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/time.h>
  14. #include <net/mac80211.h>
  15. #include <asm/unaligned.h>
  16. #include "ieee80211_i.h"
  17. #include "rate.h"
  18. #include "mesh.h"
  19. #include "led.h"
  20. #include "wme.h"
  21. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  22. struct sk_buff *skb)
  23. {
  24. struct ieee80211_local *local = hw_to_local(hw);
  25. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  26. int tmp;
  27. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  28. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  29. &local->skb_queue : &local->skb_queue_unreliable, skb);
  30. tmp = skb_queue_len(&local->skb_queue) +
  31. skb_queue_len(&local->skb_queue_unreliable);
  32. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  33. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  34. ieee80211_free_txskb(hw, skb);
  35. tmp--;
  36. I802_DEBUG_INC(local->tx_status_drop);
  37. }
  38. tasklet_schedule(&local->tasklet);
  39. }
  40. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  41. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  42. struct sta_info *sta,
  43. struct sk_buff *skb)
  44. {
  45. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  46. struct ieee80211_hdr *hdr = (void *)skb->data;
  47. int ac;
  48. /*
  49. * This skb 'survived' a round-trip through the driver, and
  50. * hopefully the driver didn't mangle it too badly. However,
  51. * we can definitely not rely on the control information
  52. * being correct. Clear it so we don't get junk there, and
  53. * indicate that it needs new processing, but must not be
  54. * modified/encrypted again.
  55. */
  56. memset(&info->control, 0, sizeof(info->control));
  57. info->control.jiffies = jiffies;
  58. info->control.vif = &sta->sdata->vif;
  59. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING |
  60. IEEE80211_TX_INTFL_RETRANSMISSION;
  61. info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
  62. sta->tx_filtered_count++;
  63. /*
  64. * Clear more-data bit on filtered frames, it might be set
  65. * but later frames might time out so it might have to be
  66. * clear again ... It's all rather unlikely (this frame
  67. * should time out first, right?) but let's not confuse
  68. * peers unnecessarily.
  69. */
  70. if (hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_MOREDATA))
  71. hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  72. if (ieee80211_is_data_qos(hdr->frame_control)) {
  73. u8 *p = ieee80211_get_qos_ctl(hdr);
  74. int tid = *p & IEEE80211_QOS_CTL_TID_MASK;
  75. /*
  76. * Clear EOSP if set, this could happen e.g.
  77. * if an absence period (us being a P2P GO)
  78. * shortens the SP.
  79. */
  80. if (*p & IEEE80211_QOS_CTL_EOSP)
  81. *p &= ~IEEE80211_QOS_CTL_EOSP;
  82. ac = ieee802_1d_to_ac[tid & 7];
  83. } else {
  84. ac = IEEE80211_AC_BE;
  85. }
  86. /*
  87. * Clear the TX filter mask for this STA when sending the next
  88. * packet. If the STA went to power save mode, this will happen
  89. * when it wakes up for the next time.
  90. */
  91. set_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT);
  92. /*
  93. * This code races in the following way:
  94. *
  95. * (1) STA sends frame indicating it will go to sleep and does so
  96. * (2) hardware/firmware adds STA to filter list, passes frame up
  97. * (3) hardware/firmware processes TX fifo and suppresses a frame
  98. * (4) we get TX status before having processed the frame and
  99. * knowing that the STA has gone to sleep.
  100. *
  101. * This is actually quite unlikely even when both those events are
  102. * processed from interrupts coming in quickly after one another or
  103. * even at the same time because we queue both TX status events and
  104. * RX frames to be processed by a tasklet and process them in the
  105. * same order that they were received or TX status last. Hence, there
  106. * is no race as long as the frame RX is processed before the next TX
  107. * status, which drivers can ensure, see below.
  108. *
  109. * Note that this can only happen if the hardware or firmware can
  110. * actually add STAs to the filter list, if this is done by the
  111. * driver in response to set_tim() (which will only reduce the race
  112. * this whole filtering tries to solve, not completely solve it)
  113. * this situation cannot happen.
  114. *
  115. * To completely solve this race drivers need to make sure that they
  116. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  117. * functions and
  118. * (b) always process RX events before TX status events if ordering
  119. * can be unknown, for example with different interrupt status
  120. * bits.
  121. * (c) if PS mode transitions are manual (i.e. the flag
  122. * %IEEE80211_HW_AP_LINK_PS is set), always process PS state
  123. * changes before calling TX status events if ordering can be
  124. * unknown.
  125. */
  126. if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
  127. skb_queue_len(&sta->tx_filtered[ac]) < STA_MAX_TX_BUFFER) {
  128. skb_queue_tail(&sta->tx_filtered[ac], skb);
  129. sta_info_recalc_tim(sta);
  130. if (!timer_pending(&local->sta_cleanup))
  131. mod_timer(&local->sta_cleanup,
  132. round_jiffies(jiffies +
  133. STA_INFO_CLEANUP_INTERVAL));
  134. return;
  135. }
  136. if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
  137. !(info->flags & IEEE80211_TX_INTFL_RETRIED)) {
  138. /* Software retry the packet once */
  139. info->flags |= IEEE80211_TX_INTFL_RETRIED;
  140. ieee80211_add_pending_skb(local, skb);
  141. return;
  142. }
  143. ps_dbg_ratelimited(sta->sdata,
  144. "dropped TX filtered frame, queue_len=%d PS=%d @%lu\n",
  145. skb_queue_len(&sta->tx_filtered[ac]),
  146. !!test_sta_flag(sta, WLAN_STA_PS_STA), jiffies);
  147. ieee80211_free_txskb(&local->hw, skb);
  148. }
  149. static void ieee80211_check_pending_bar(struct sta_info *sta, u8 *addr, u8 tid)
  150. {
  151. struct tid_ampdu_tx *tid_tx;
  152. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  153. if (!tid_tx || !tid_tx->bar_pending)
  154. return;
  155. tid_tx->bar_pending = false;
  156. ieee80211_send_bar(&sta->sdata->vif, addr, tid, tid_tx->failed_bar_ssn);
  157. }
  158. static void ieee80211_frame_acked(struct sta_info *sta, struct sk_buff *skb)
  159. {
  160. struct ieee80211_mgmt *mgmt = (void *) skb->data;
  161. struct ieee80211_local *local = sta->local;
  162. struct ieee80211_sub_if_data *sdata = sta->sdata;
  163. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  164. sta->last_rx = jiffies;
  165. if (ieee80211_is_data_qos(mgmt->frame_control)) {
  166. struct ieee80211_hdr *hdr = (void *) skb->data;
  167. u8 *qc = ieee80211_get_qos_ctl(hdr);
  168. u16 tid = qc[0] & 0xf;
  169. ieee80211_check_pending_bar(sta, hdr->addr1, tid);
  170. }
  171. if (ieee80211_is_action(mgmt->frame_control) &&
  172. mgmt->u.action.category == WLAN_CATEGORY_HT &&
  173. mgmt->u.action.u.ht_smps.action == WLAN_HT_ACTION_SMPS &&
  174. ieee80211_sdata_running(sdata)) {
  175. enum ieee80211_smps_mode smps_mode;
  176. switch (mgmt->u.action.u.ht_smps.smps_control) {
  177. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  178. smps_mode = IEEE80211_SMPS_DYNAMIC;
  179. break;
  180. case WLAN_HT_SMPS_CONTROL_STATIC:
  181. smps_mode = IEEE80211_SMPS_STATIC;
  182. break;
  183. case WLAN_HT_SMPS_CONTROL_DISABLED:
  184. default: /* shouldn't happen since we don't send that */
  185. smps_mode = IEEE80211_SMPS_OFF;
  186. break;
  187. }
  188. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  189. /*
  190. * This update looks racy, but isn't -- if we come
  191. * here we've definitely got a station that we're
  192. * talking to, and on a managed interface that can
  193. * only be the AP. And the only other place updating
  194. * this variable in managed mode is before association.
  195. */
  196. sdata->smps_mode = smps_mode;
  197. ieee80211_queue_work(&local->hw, &sdata->recalc_smps);
  198. } else if (sdata->vif.type == NL80211_IFTYPE_AP ||
  199. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  200. sta->known_smps_mode = smps_mode;
  201. }
  202. }
  203. }
  204. static void ieee80211_set_bar_pending(struct sta_info *sta, u8 tid, u16 ssn)
  205. {
  206. struct tid_ampdu_tx *tid_tx;
  207. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  208. if (!tid_tx)
  209. return;
  210. tid_tx->failed_bar_ssn = ssn;
  211. tid_tx->bar_pending = true;
  212. }
  213. static int ieee80211_tx_radiotap_len(struct ieee80211_tx_info *info)
  214. {
  215. int len = sizeof(struct ieee80211_radiotap_header);
  216. /* IEEE80211_RADIOTAP_RATE rate */
  217. if (info->status.rates[0].idx >= 0 &&
  218. !(info->status.rates[0].flags & (IEEE80211_TX_RC_MCS |
  219. IEEE80211_TX_RC_VHT_MCS)))
  220. len += 2;
  221. /* IEEE80211_RADIOTAP_TX_FLAGS */
  222. len += 2;
  223. /* IEEE80211_RADIOTAP_DATA_RETRIES */
  224. len += 1;
  225. /* IEEE80211_RADIOTAP_MCS
  226. * IEEE80211_RADIOTAP_VHT */
  227. if (info->status.rates[0].idx >= 0) {
  228. if (info->status.rates[0].flags & IEEE80211_TX_RC_MCS)
  229. len += 3;
  230. else if (info->status.rates[0].flags & IEEE80211_TX_RC_VHT_MCS)
  231. len = ALIGN(len, 2) + 12;
  232. }
  233. return len;
  234. }
  235. static void
  236. ieee80211_add_tx_radiotap_header(struct ieee80211_local *local,
  237. struct ieee80211_supported_band *sband,
  238. struct sk_buff *skb, int retry_count,
  239. int rtap_len, int shift)
  240. {
  241. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  242. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  243. struct ieee80211_radiotap_header *rthdr;
  244. unsigned char *pos;
  245. u16 txflags;
  246. rthdr = (struct ieee80211_radiotap_header *) skb_push(skb, rtap_len);
  247. memset(rthdr, 0, rtap_len);
  248. rthdr->it_len = cpu_to_le16(rtap_len);
  249. rthdr->it_present =
  250. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  251. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  252. pos = (unsigned char *)(rthdr + 1);
  253. /*
  254. * XXX: Once radiotap gets the bitmap reset thing the vendor
  255. * extensions proposal contains, we can actually report
  256. * the whole set of tries we did.
  257. */
  258. /* IEEE80211_RADIOTAP_RATE */
  259. if (info->status.rates[0].idx >= 0 &&
  260. !(info->status.rates[0].flags & (IEEE80211_TX_RC_MCS |
  261. IEEE80211_TX_RC_VHT_MCS))) {
  262. u16 rate;
  263. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  264. rate = sband->bitrates[info->status.rates[0].idx].bitrate;
  265. *pos = DIV_ROUND_UP(rate, 5 * (1 << shift));
  266. /* padding for tx flags */
  267. pos += 2;
  268. }
  269. /* IEEE80211_RADIOTAP_TX_FLAGS */
  270. txflags = 0;
  271. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  272. !is_multicast_ether_addr(hdr->addr1))
  273. txflags |= IEEE80211_RADIOTAP_F_TX_FAIL;
  274. if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  275. txflags |= IEEE80211_RADIOTAP_F_TX_CTS;
  276. if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  277. txflags |= IEEE80211_RADIOTAP_F_TX_RTS;
  278. put_unaligned_le16(txflags, pos);
  279. pos += 2;
  280. /* IEEE80211_RADIOTAP_DATA_RETRIES */
  281. /* for now report the total retry_count */
  282. *pos = retry_count;
  283. pos++;
  284. if (info->status.rates[0].idx < 0)
  285. return;
  286. /* IEEE80211_RADIOTAP_MCS
  287. * IEEE80211_RADIOTAP_VHT */
  288. if (info->status.rates[0].flags & IEEE80211_TX_RC_MCS) {
  289. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
  290. pos[0] = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
  291. IEEE80211_RADIOTAP_MCS_HAVE_GI |
  292. IEEE80211_RADIOTAP_MCS_HAVE_BW;
  293. if (info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  294. pos[1] |= IEEE80211_RADIOTAP_MCS_SGI;
  295. if (info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  296. pos[1] |= IEEE80211_RADIOTAP_MCS_BW_40;
  297. if (info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)
  298. pos[1] |= IEEE80211_RADIOTAP_MCS_FMT_GF;
  299. pos[2] = info->status.rates[0].idx;
  300. pos += 3;
  301. } else if (info->status.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
  302. u16 known = local->hw.radiotap_vht_details &
  303. (IEEE80211_RADIOTAP_VHT_KNOWN_GI |
  304. IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH);
  305. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
  306. /* required alignment from rthdr */
  307. pos = (u8 *)rthdr + ALIGN(pos - (u8 *)rthdr, 2);
  308. /* u16 known - IEEE80211_RADIOTAP_VHT_KNOWN_* */
  309. put_unaligned_le16(known, pos);
  310. pos += 2;
  311. /* u8 flags - IEEE80211_RADIOTAP_VHT_FLAG_* */
  312. if (info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  313. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
  314. pos++;
  315. /* u8 bandwidth */
  316. if (info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  317. *pos = 1;
  318. else if (info->status.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  319. *pos = 4;
  320. else if (info->status.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  321. *pos = 11;
  322. else /* IEEE80211_TX_RC_{20_MHZ_WIDTH,FIXME:DUP_DATA} */
  323. *pos = 0;
  324. pos++;
  325. /* u8 mcs_nss[4] */
  326. *pos = (ieee80211_rate_get_vht_mcs(&info->status.rates[0]) << 4) |
  327. ieee80211_rate_get_vht_nss(&info->status.rates[0]);
  328. pos += 4;
  329. /* u8 coding */
  330. pos++;
  331. /* u8 group_id */
  332. pos++;
  333. /* u16 partial_aid */
  334. pos += 2;
  335. }
  336. }
  337. static void ieee80211_report_used_skb(struct ieee80211_local *local,
  338. struct sk_buff *skb, bool dropped)
  339. {
  340. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  341. struct ieee80211_hdr *hdr = (void *)skb->data;
  342. bool acked = info->flags & IEEE80211_TX_STAT_ACK;
  343. if (dropped)
  344. acked = false;
  345. if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
  346. IEEE80211_TX_INTFL_MLME_CONN_TX)) {
  347. struct ieee80211_sub_if_data *sdata = NULL;
  348. struct ieee80211_sub_if_data *iter_sdata;
  349. u64 cookie = (unsigned long)skb;
  350. rcu_read_lock();
  351. if (skb->dev) {
  352. list_for_each_entry_rcu(iter_sdata, &local->interfaces,
  353. list) {
  354. if (!iter_sdata->dev)
  355. continue;
  356. if (skb->dev == iter_sdata->dev) {
  357. sdata = iter_sdata;
  358. break;
  359. }
  360. }
  361. } else {
  362. sdata = rcu_dereference(local->p2p_sdata);
  363. }
  364. if (!sdata) {
  365. skb->dev = NULL;
  366. } else if (info->flags & IEEE80211_TX_INTFL_MLME_CONN_TX) {
  367. ieee80211_mgd_conn_tx_status(sdata, hdr->frame_control,
  368. acked);
  369. } else if (ieee80211_is_nullfunc(hdr->frame_control) ||
  370. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  371. cfg80211_probe_status(sdata->dev, hdr->addr1,
  372. cookie, acked, GFP_ATOMIC);
  373. } else {
  374. cfg80211_mgmt_tx_status(&sdata->wdev, cookie, skb->data,
  375. skb->len, acked, GFP_ATOMIC);
  376. }
  377. rcu_read_unlock();
  378. }
  379. if (unlikely(info->ack_frame_id)) {
  380. struct sk_buff *ack_skb;
  381. unsigned long flags;
  382. spin_lock_irqsave(&local->ack_status_lock, flags);
  383. ack_skb = idr_find(&local->ack_status_frames,
  384. info->ack_frame_id);
  385. if (ack_skb)
  386. idr_remove(&local->ack_status_frames,
  387. info->ack_frame_id);
  388. spin_unlock_irqrestore(&local->ack_status_lock, flags);
  389. if (ack_skb) {
  390. if (!dropped) {
  391. /* consumes ack_skb */
  392. skb_complete_wifi_ack(ack_skb, acked);
  393. } else {
  394. dev_kfree_skb_any(ack_skb);
  395. }
  396. }
  397. }
  398. }
  399. /*
  400. * Measure Tx frame completion and removal time for Tx latency statistics
  401. * calculation. A single Tx frame latency should be measured from when it
  402. * is entering the Kernel until we receive Tx complete confirmation indication
  403. * and remove the skb.
  404. */
  405. static void ieee80211_tx_latency_end_msrmnt(struct ieee80211_local *local,
  406. struct sk_buff *skb,
  407. struct sta_info *sta,
  408. struct ieee80211_hdr *hdr)
  409. {
  410. ktime_t skb_dprt;
  411. struct timespec dprt_time;
  412. u32 msrmnt;
  413. u16 tid;
  414. u8 *qc;
  415. int i, bin_range_count;
  416. u32 *bin_ranges;
  417. __le16 fc;
  418. struct ieee80211_tx_latency_stat *tx_lat;
  419. struct ieee80211_tx_latency_bin_ranges *tx_latency;
  420. ktime_t skb_arv = skb->tstamp;
  421. tx_latency = rcu_dereference(local->tx_latency);
  422. /* assert Tx latency stats are enabled & frame arrived when enabled */
  423. if (!tx_latency || !ktime_to_ns(skb_arv))
  424. return;
  425. fc = hdr->frame_control;
  426. if (!ieee80211_is_data(fc)) /* make sure it is a data frame */
  427. return;
  428. /* get frame tid */
  429. if (ieee80211_is_data_qos(hdr->frame_control)) {
  430. qc = ieee80211_get_qos_ctl(hdr);
  431. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  432. } else {
  433. tid = 0;
  434. }
  435. tx_lat = &sta->tx_lat[tid];
  436. ktime_get_ts(&dprt_time); /* time stamp completion time */
  437. skb_dprt = ktime_set(dprt_time.tv_sec, dprt_time.tv_nsec);
  438. msrmnt = ktime_to_ms(ktime_sub(skb_dprt, skb_arv));
  439. if (tx_lat->max < msrmnt) /* update stats */
  440. tx_lat->max = msrmnt;
  441. tx_lat->counter++;
  442. tx_lat->sum += msrmnt;
  443. if (!tx_lat->bins) /* bins not activated */
  444. return;
  445. /* count how many Tx frames transmitted with the appropriate latency */
  446. bin_range_count = tx_latency->n_ranges;
  447. bin_ranges = tx_latency->ranges;
  448. for (i = 0; i < bin_range_count; i++) {
  449. if (msrmnt <= bin_ranges[i]) {
  450. tx_lat->bins[i]++;
  451. break;
  452. }
  453. }
  454. if (i == bin_range_count) /* msrmnt is bigger than the biggest range */
  455. tx_lat->bins[i]++;
  456. }
  457. /*
  458. * Use a static threshold for now, best value to be determined
  459. * by testing ...
  460. * Should it depend on:
  461. * - on # of retransmissions
  462. * - current throughput (higher value for higher tpt)?
  463. */
  464. #define STA_LOST_PKT_THRESHOLD 50
  465. static void ieee80211_lost_packet(struct sta_info *sta, struct sk_buff *skb)
  466. {
  467. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  468. /* This packet was aggregated but doesn't carry status info */
  469. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  470. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  471. return;
  472. if (++sta->lost_packets < STA_LOST_PKT_THRESHOLD)
  473. return;
  474. cfg80211_cqm_pktloss_notify(sta->sdata->dev, sta->sta.addr,
  475. sta->lost_packets, GFP_ATOMIC);
  476. sta->lost_packets = 0;
  477. }
  478. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  479. {
  480. struct sk_buff *skb2;
  481. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  482. struct ieee80211_local *local = hw_to_local(hw);
  483. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  484. __le16 fc;
  485. struct ieee80211_supported_band *sband;
  486. struct ieee80211_sub_if_data *sdata;
  487. struct net_device *prev_dev = NULL;
  488. struct sta_info *sta, *tmp;
  489. int retry_count = -1, i;
  490. int rates_idx = -1;
  491. bool send_to_cooked;
  492. bool acked;
  493. struct ieee80211_bar *bar;
  494. int rtap_len;
  495. int shift = 0;
  496. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  497. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  498. !(info->flags & IEEE80211_TX_STAT_AMPDU)) {
  499. /* just the first aggr frame carry status info */
  500. info->status.rates[i].idx = -1;
  501. info->status.rates[i].count = 0;
  502. break;
  503. } else if (info->status.rates[i].idx < 0) {
  504. break;
  505. } else if (i >= hw->max_report_rates) {
  506. /* the HW cannot have attempted that rate */
  507. info->status.rates[i].idx = -1;
  508. info->status.rates[i].count = 0;
  509. break;
  510. }
  511. retry_count += info->status.rates[i].count;
  512. }
  513. rates_idx = i - 1;
  514. if (retry_count < 0)
  515. retry_count = 0;
  516. rcu_read_lock();
  517. sband = local->hw.wiphy->bands[info->band];
  518. fc = hdr->frame_control;
  519. for_each_sta_info(local, hdr->addr1, sta, tmp) {
  520. /* skip wrong virtual interface */
  521. if (!ether_addr_equal(hdr->addr2, sta->sdata->vif.addr))
  522. continue;
  523. shift = ieee80211_vif_get_shift(&sta->sdata->vif);
  524. if (info->flags & IEEE80211_TX_STATUS_EOSP)
  525. clear_sta_flag(sta, WLAN_STA_SP);
  526. acked = !!(info->flags & IEEE80211_TX_STAT_ACK);
  527. if (!acked && test_sta_flag(sta, WLAN_STA_PS_STA)) {
  528. /*
  529. * The STA is in power save mode, so assume
  530. * that this TX packet failed because of that.
  531. */
  532. ieee80211_handle_filtered_frame(local, sta, skb);
  533. rcu_read_unlock();
  534. return;
  535. }
  536. /* mesh Peer Service Period support */
  537. if (ieee80211_vif_is_mesh(&sta->sdata->vif) &&
  538. ieee80211_is_data_qos(fc))
  539. ieee80211_mpsp_trigger_process(
  540. ieee80211_get_qos_ctl(hdr),
  541. sta, true, acked);
  542. if ((local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) &&
  543. (ieee80211_is_data(hdr->frame_control)) &&
  544. (rates_idx != -1))
  545. sta->last_tx_rate = info->status.rates[rates_idx];
  546. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  547. (ieee80211_is_data_qos(fc))) {
  548. u16 tid, ssn;
  549. u8 *qc;
  550. qc = ieee80211_get_qos_ctl(hdr);
  551. tid = qc[0] & 0xf;
  552. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  553. & IEEE80211_SCTL_SEQ);
  554. ieee80211_send_bar(&sta->sdata->vif, hdr->addr1,
  555. tid, ssn);
  556. }
  557. if (!acked && ieee80211_is_back_req(fc)) {
  558. u16 tid, control;
  559. /*
  560. * BAR failed, store the last SSN and retry sending
  561. * the BAR when the next unicast transmission on the
  562. * same TID succeeds.
  563. */
  564. bar = (struct ieee80211_bar *) skb->data;
  565. control = le16_to_cpu(bar->control);
  566. if (!(control & IEEE80211_BAR_CTRL_MULTI_TID)) {
  567. u16 ssn = le16_to_cpu(bar->start_seq_num);
  568. tid = (control &
  569. IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
  570. IEEE80211_BAR_CTRL_TID_INFO_SHIFT;
  571. ieee80211_set_bar_pending(sta, tid, ssn);
  572. }
  573. }
  574. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  575. ieee80211_handle_filtered_frame(local, sta, skb);
  576. rcu_read_unlock();
  577. return;
  578. } else {
  579. if (!acked)
  580. sta->tx_retry_failed++;
  581. sta->tx_retry_count += retry_count;
  582. }
  583. rate_control_tx_status(local, sband, sta, skb);
  584. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  585. ieee80211s_update_metric(local, sta, skb);
  586. if (!(info->flags & IEEE80211_TX_CTL_INJECTED) && acked)
  587. ieee80211_frame_acked(sta, skb);
  588. if ((sta->sdata->vif.type == NL80211_IFTYPE_STATION) &&
  589. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  590. ieee80211_sta_tx_notify(sta->sdata, (void *) skb->data, acked);
  591. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  592. if (info->flags & IEEE80211_TX_STAT_ACK) {
  593. if (sta->lost_packets)
  594. sta->lost_packets = 0;
  595. } else {
  596. ieee80211_lost_packet(sta, skb);
  597. }
  598. }
  599. if (acked)
  600. sta->last_ack_signal = info->status.ack_signal;
  601. /*
  602. * Measure frame removal for tx latency
  603. * statistics calculation
  604. */
  605. ieee80211_tx_latency_end_msrmnt(local, skb, sta, hdr);
  606. }
  607. rcu_read_unlock();
  608. ieee80211_led_tx(local);
  609. /* SNMP counters
  610. * Fragments are passed to low-level drivers as separate skbs, so these
  611. * are actually fragments, not frames. Update frame counters only for
  612. * the first fragment of the frame. */
  613. if (info->flags & IEEE80211_TX_STAT_ACK) {
  614. if (ieee80211_is_first_frag(hdr->seq_ctrl)) {
  615. local->dot11TransmittedFrameCount++;
  616. if (is_multicast_ether_addr(hdr->addr1))
  617. local->dot11MulticastTransmittedFrameCount++;
  618. if (retry_count > 0)
  619. local->dot11RetryCount++;
  620. if (retry_count > 1)
  621. local->dot11MultipleRetryCount++;
  622. }
  623. /* This counter shall be incremented for an acknowledged MPDU
  624. * with an individual address in the address 1 field or an MPDU
  625. * with a multicast address in the address 1 field of type Data
  626. * or Management. */
  627. if (!is_multicast_ether_addr(hdr->addr1) ||
  628. ieee80211_is_data(fc) ||
  629. ieee80211_is_mgmt(fc))
  630. local->dot11TransmittedFragmentCount++;
  631. } else {
  632. if (ieee80211_is_first_frag(hdr->seq_ctrl))
  633. local->dot11FailedCount++;
  634. }
  635. if (ieee80211_is_nullfunc(fc) && ieee80211_has_pm(fc) &&
  636. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  637. !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
  638. local->ps_sdata && !(local->scanning)) {
  639. if (info->flags & IEEE80211_TX_STAT_ACK) {
  640. local->ps_sdata->u.mgd.flags |=
  641. IEEE80211_STA_NULLFUNC_ACKED;
  642. } else
  643. mod_timer(&local->dynamic_ps_timer, jiffies +
  644. msecs_to_jiffies(10));
  645. }
  646. ieee80211_report_used_skb(local, skb, false);
  647. /* this was a transmitted frame, but now we want to reuse it */
  648. skb_orphan(skb);
  649. /* Need to make a copy before skb->cb gets cleared */
  650. send_to_cooked = !!(info->flags & IEEE80211_TX_CTL_INJECTED) ||
  651. !(ieee80211_is_data(fc));
  652. /*
  653. * This is a bit racy but we can avoid a lot of work
  654. * with this test...
  655. */
  656. if (!local->monitors && (!send_to_cooked || !local->cooked_mntrs)) {
  657. dev_kfree_skb(skb);
  658. return;
  659. }
  660. /* send frame to monitor interfaces now */
  661. rtap_len = ieee80211_tx_radiotap_len(info);
  662. if (WARN_ON_ONCE(skb_headroom(skb) < rtap_len)) {
  663. pr_err("ieee80211_tx_status: headroom too small\n");
  664. dev_kfree_skb(skb);
  665. return;
  666. }
  667. ieee80211_add_tx_radiotap_header(local, sband, skb, retry_count,
  668. rtap_len, shift);
  669. /* XXX: is this sufficient for BPF? */
  670. skb_set_mac_header(skb, 0);
  671. skb->ip_summed = CHECKSUM_UNNECESSARY;
  672. skb->pkt_type = PACKET_OTHERHOST;
  673. skb->protocol = htons(ETH_P_802_2);
  674. memset(skb->cb, 0, sizeof(skb->cb));
  675. rcu_read_lock();
  676. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  677. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  678. if (!ieee80211_sdata_running(sdata))
  679. continue;
  680. if ((sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) &&
  681. !send_to_cooked)
  682. continue;
  683. if (prev_dev) {
  684. skb2 = skb_clone(skb, GFP_ATOMIC);
  685. if (skb2) {
  686. skb2->dev = prev_dev;
  687. netif_rx(skb2);
  688. }
  689. }
  690. prev_dev = sdata->dev;
  691. }
  692. }
  693. if (prev_dev) {
  694. skb->dev = prev_dev;
  695. netif_rx(skb);
  696. skb = NULL;
  697. }
  698. rcu_read_unlock();
  699. dev_kfree_skb(skb);
  700. }
  701. EXPORT_SYMBOL(ieee80211_tx_status);
  702. void ieee80211_report_low_ack(struct ieee80211_sta *pubsta, u32 num_packets)
  703. {
  704. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  705. cfg80211_cqm_pktloss_notify(sta->sdata->dev, sta->sta.addr,
  706. num_packets, GFP_ATOMIC);
  707. }
  708. EXPORT_SYMBOL(ieee80211_report_low_ack);
  709. void ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb)
  710. {
  711. struct ieee80211_local *local = hw_to_local(hw);
  712. ieee80211_report_used_skb(local, skb, true);
  713. dev_kfree_skb_any(skb);
  714. }
  715. EXPORT_SYMBOL(ieee80211_free_txskb);
  716. void ieee80211_purge_tx_queue(struct ieee80211_hw *hw,
  717. struct sk_buff_head *skbs)
  718. {
  719. struct sk_buff *skb;
  720. while ((skb = __skb_dequeue(skbs)))
  721. ieee80211_free_txskb(hw, skb);
  722. }