tx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #include "mt76.h"
  6. static int
  7. mt76_txq_get_qid(struct ieee80211_txq *txq)
  8. {
  9. if (!txq->sta)
  10. return MT_TXQ_BE;
  11. return txq->ac;
  12. }
  13. void
  14. mt76_tx_check_agg_ssn(struct ieee80211_sta *sta, struct sk_buff *skb)
  15. {
  16. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  17. struct ieee80211_txq *txq;
  18. struct mt76_txq *mtxq;
  19. u8 tid;
  20. if (!sta || !ieee80211_is_data_qos(hdr->frame_control) ||
  21. !ieee80211_is_data_present(hdr->frame_control))
  22. return;
  23. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  24. txq = sta->txq[tid];
  25. mtxq = (struct mt76_txq *)txq->drv_priv;
  26. if (!mtxq->aggr)
  27. return;
  28. mtxq->agg_ssn = le16_to_cpu(hdr->seq_ctrl) + 0x10;
  29. }
  30. EXPORT_SYMBOL_GPL(mt76_tx_check_agg_ssn);
  31. void
  32. mt76_tx_status_lock(struct mt76_dev *dev, struct sk_buff_head *list)
  33. __acquires(&dev->status_list.lock)
  34. {
  35. __skb_queue_head_init(list);
  36. spin_lock_bh(&dev->status_list.lock);
  37. }
  38. EXPORT_SYMBOL_GPL(mt76_tx_status_lock);
  39. void
  40. mt76_tx_status_unlock(struct mt76_dev *dev, struct sk_buff_head *list)
  41. __releases(&dev->status_list.lock)
  42. {
  43. struct ieee80211_hw *hw;
  44. struct sk_buff *skb;
  45. spin_unlock_bh(&dev->status_list.lock);
  46. rcu_read_lock();
  47. while ((skb = __skb_dequeue(list)) != NULL) {
  48. struct ieee80211_tx_status status = {
  49. .skb = skb,
  50. .info = IEEE80211_SKB_CB(skb),
  51. };
  52. struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
  53. struct mt76_wcid *wcid;
  54. wcid = rcu_dereference(dev->wcid[cb->wcid]);
  55. if (wcid)
  56. status.sta = wcid_to_sta(wcid);
  57. hw = mt76_tx_status_get_hw(dev, skb);
  58. ieee80211_tx_status_ext(hw, &status);
  59. }
  60. rcu_read_unlock();
  61. }
  62. EXPORT_SYMBOL_GPL(mt76_tx_status_unlock);
  63. static void
  64. __mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb, u8 flags,
  65. struct sk_buff_head *list)
  66. {
  67. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  68. struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
  69. u8 done = MT_TX_CB_DMA_DONE | MT_TX_CB_TXS_DONE;
  70. flags |= cb->flags;
  71. cb->flags = flags;
  72. if ((flags & done) != done)
  73. return;
  74. __skb_unlink(skb, &dev->status_list);
  75. /* Tx status can be unreliable. if it fails, mark the frame as ACKed */
  76. if (flags & MT_TX_CB_TXS_FAILED) {
  77. info->status.rates[0].count = 0;
  78. info->status.rates[0].idx = -1;
  79. info->flags |= IEEE80211_TX_STAT_ACK;
  80. }
  81. __skb_queue_tail(list, skb);
  82. }
  83. void
  84. mt76_tx_status_skb_done(struct mt76_dev *dev, struct sk_buff *skb,
  85. struct sk_buff_head *list)
  86. {
  87. __mt76_tx_status_skb_done(dev, skb, MT_TX_CB_TXS_DONE, list);
  88. }
  89. EXPORT_SYMBOL_GPL(mt76_tx_status_skb_done);
  90. int
  91. mt76_tx_status_skb_add(struct mt76_dev *dev, struct mt76_wcid *wcid,
  92. struct sk_buff *skb)
  93. {
  94. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  95. struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
  96. int pid;
  97. if (!wcid)
  98. return MT_PACKET_ID_NO_ACK;
  99. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  100. return MT_PACKET_ID_NO_ACK;
  101. if (!(info->flags & (IEEE80211_TX_CTL_REQ_TX_STATUS |
  102. IEEE80211_TX_CTL_RATE_CTRL_PROBE)))
  103. return MT_PACKET_ID_NO_SKB;
  104. spin_lock_bh(&dev->status_list.lock);
  105. memset(cb, 0, sizeof(*cb));
  106. pid = mt76_get_next_pkt_id(wcid);
  107. cb->wcid = wcid->idx;
  108. cb->pktid = pid;
  109. cb->jiffies = jiffies;
  110. __skb_queue_tail(&dev->status_list, skb);
  111. spin_unlock_bh(&dev->status_list.lock);
  112. return pid;
  113. }
  114. EXPORT_SYMBOL_GPL(mt76_tx_status_skb_add);
  115. struct sk_buff *
  116. mt76_tx_status_skb_get(struct mt76_dev *dev, struct mt76_wcid *wcid, int pktid,
  117. struct sk_buff_head *list)
  118. {
  119. struct sk_buff *skb, *tmp;
  120. skb_queue_walk_safe(&dev->status_list, skb, tmp) {
  121. struct mt76_tx_cb *cb = mt76_tx_skb_cb(skb);
  122. if (wcid && cb->wcid != wcid->idx)
  123. continue;
  124. if (cb->pktid == pktid)
  125. return skb;
  126. if (pktid >= 0 && !time_after(jiffies, cb->jiffies +
  127. MT_TX_STATUS_SKB_TIMEOUT))
  128. continue;
  129. __mt76_tx_status_skb_done(dev, skb, MT_TX_CB_TXS_FAILED |
  130. MT_TX_CB_TXS_DONE, list);
  131. }
  132. return NULL;
  133. }
  134. EXPORT_SYMBOL_GPL(mt76_tx_status_skb_get);
  135. void
  136. mt76_tx_status_check(struct mt76_dev *dev, struct mt76_wcid *wcid, bool flush)
  137. {
  138. struct sk_buff_head list;
  139. mt76_tx_status_lock(dev, &list);
  140. mt76_tx_status_skb_get(dev, wcid, flush ? -1 : 0, &list);
  141. mt76_tx_status_unlock(dev, &list);
  142. }
  143. EXPORT_SYMBOL_GPL(mt76_tx_status_check);
  144. static void
  145. mt76_tx_check_non_aql(struct mt76_dev *dev, struct mt76_wcid *wcid,
  146. struct sk_buff *skb)
  147. {
  148. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  149. int pending;
  150. if (!wcid || info->tx_time_est)
  151. return;
  152. pending = atomic_dec_return(&wcid->non_aql_packets);
  153. if (pending < 0)
  154. atomic_cmpxchg(&wcid->non_aql_packets, pending, 0);
  155. }
  156. void __mt76_tx_complete_skb(struct mt76_dev *dev, u16 wcid_idx, struct sk_buff *skb,
  157. struct list_head *free_list)
  158. {
  159. struct ieee80211_tx_status status = {
  160. .skb = skb,
  161. .free_list = free_list,
  162. };
  163. struct mt76_wcid *wcid = NULL;
  164. struct ieee80211_hw *hw;
  165. struct sk_buff_head list;
  166. rcu_read_lock();
  167. if (wcid_idx < ARRAY_SIZE(dev->wcid))
  168. wcid = rcu_dereference(dev->wcid[wcid_idx]);
  169. mt76_tx_check_non_aql(dev, wcid, skb);
  170. #ifdef CONFIG_NL80211_TESTMODE
  171. if (mt76_is_testmode_skb(dev, skb, &hw)) {
  172. struct mt76_phy *phy = hw->priv;
  173. if (skb == phy->test.tx_skb)
  174. phy->test.tx_done++;
  175. if (phy->test.tx_queued == phy->test.tx_done)
  176. wake_up(&dev->tx_wait);
  177. dev_kfree_skb_any(skb);
  178. goto out;
  179. }
  180. #endif
  181. if (!skb->prev) {
  182. hw = mt76_tx_status_get_hw(dev, skb);
  183. status.sta = wcid_to_sta(wcid);
  184. ieee80211_tx_status_ext(hw, &status);
  185. goto out;
  186. }
  187. mt76_tx_status_lock(dev, &list);
  188. __mt76_tx_status_skb_done(dev, skb, MT_TX_CB_DMA_DONE, &list);
  189. mt76_tx_status_unlock(dev, &list);
  190. out:
  191. rcu_read_unlock();
  192. }
  193. EXPORT_SYMBOL_GPL(__mt76_tx_complete_skb);
  194. static int
  195. __mt76_tx_queue_skb(struct mt76_phy *phy, int qid, struct sk_buff *skb,
  196. struct mt76_wcid *wcid, struct ieee80211_sta *sta,
  197. bool *stop)
  198. {
  199. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  200. struct mt76_queue *q = phy->q_tx[qid];
  201. struct mt76_dev *dev = phy->dev;
  202. bool non_aql;
  203. int pending;
  204. int idx;
  205. non_aql = !info->tx_time_est;
  206. idx = dev->queue_ops->tx_queue_skb(dev, q, skb, wcid, sta);
  207. if (idx < 0 || !sta)
  208. return idx;
  209. wcid = (struct mt76_wcid *)sta->drv_priv;
  210. q->entry[idx].wcid = wcid->idx;
  211. if (!non_aql)
  212. return idx;
  213. pending = atomic_inc_return(&wcid->non_aql_packets);
  214. if (stop && pending >= MT_MAX_NON_AQL_PKT)
  215. *stop = true;
  216. return idx;
  217. }
  218. void
  219. mt76_tx(struct mt76_phy *phy, struct ieee80211_sta *sta,
  220. struct mt76_wcid *wcid, struct sk_buff *skb)
  221. {
  222. struct mt76_dev *dev = phy->dev;
  223. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  224. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  225. struct mt76_queue *q;
  226. int qid = skb_get_queue_mapping(skb);
  227. bool ext_phy = phy != &dev->phy;
  228. if (mt76_testmode_enabled(phy)) {
  229. ieee80211_free_txskb(phy->hw, skb);
  230. return;
  231. }
  232. if (WARN_ON(qid >= MT_TXQ_PSD)) {
  233. qid = MT_TXQ_BE;
  234. skb_set_queue_mapping(skb, qid);
  235. }
  236. if ((dev->drv->drv_flags & MT_DRV_HW_MGMT_TXQ) &&
  237. !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
  238. !ieee80211_is_data(hdr->frame_control) &&
  239. !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
  240. qid = MT_TXQ_PSD;
  241. skb_set_queue_mapping(skb, qid);
  242. }
  243. if (wcid && !(wcid->tx_info & MT_WCID_TX_INFO_SET))
  244. ieee80211_get_tx_rates(info->control.vif, sta, skb,
  245. info->control.rates, 1);
  246. if (ext_phy)
  247. info->hw_queue |= MT_TX_HW_QUEUE_EXT_PHY;
  248. q = phy->q_tx[qid];
  249. spin_lock_bh(&q->lock);
  250. __mt76_tx_queue_skb(phy, qid, skb, wcid, sta, NULL);
  251. dev->queue_ops->kick(dev, q);
  252. spin_unlock_bh(&q->lock);
  253. }
  254. EXPORT_SYMBOL_GPL(mt76_tx);
  255. static struct sk_buff *
  256. mt76_txq_dequeue(struct mt76_phy *phy, struct mt76_txq *mtxq)
  257. {
  258. struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
  259. struct ieee80211_tx_info *info;
  260. bool ext_phy = phy != &phy->dev->phy;
  261. struct sk_buff *skb;
  262. skb = ieee80211_tx_dequeue(phy->hw, txq);
  263. if (!skb)
  264. return NULL;
  265. info = IEEE80211_SKB_CB(skb);
  266. if (ext_phy)
  267. info->hw_queue |= MT_TX_HW_QUEUE_EXT_PHY;
  268. return skb;
  269. }
  270. static void
  271. mt76_queue_ps_skb(struct mt76_phy *phy, struct ieee80211_sta *sta,
  272. struct sk_buff *skb, bool last)
  273. {
  274. struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
  275. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  276. info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
  277. if (last)
  278. info->flags |= IEEE80211_TX_STATUS_EOSP |
  279. IEEE80211_TX_CTL_REQ_TX_STATUS;
  280. mt76_skb_set_moredata(skb, !last);
  281. __mt76_tx_queue_skb(phy, MT_TXQ_PSD, skb, wcid, sta, NULL);
  282. }
  283. void
  284. mt76_release_buffered_frames(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
  285. u16 tids, int nframes,
  286. enum ieee80211_frame_release_type reason,
  287. bool more_data)
  288. {
  289. struct mt76_phy *phy = hw->priv;
  290. struct mt76_dev *dev = phy->dev;
  291. struct sk_buff *last_skb = NULL;
  292. struct mt76_queue *hwq = phy->q_tx[MT_TXQ_PSD];
  293. int i;
  294. spin_lock_bh(&hwq->lock);
  295. for (i = 0; tids && nframes; i++, tids >>= 1) {
  296. struct ieee80211_txq *txq = sta->txq[i];
  297. struct mt76_txq *mtxq = (struct mt76_txq *)txq->drv_priv;
  298. struct sk_buff *skb;
  299. if (!(tids & 1))
  300. continue;
  301. do {
  302. skb = mt76_txq_dequeue(phy, mtxq);
  303. if (!skb)
  304. break;
  305. nframes--;
  306. if (last_skb)
  307. mt76_queue_ps_skb(phy, sta, last_skb, false);
  308. last_skb = skb;
  309. } while (nframes);
  310. }
  311. if (last_skb) {
  312. mt76_queue_ps_skb(phy, sta, last_skb, true);
  313. dev->queue_ops->kick(dev, hwq);
  314. } else {
  315. ieee80211_sta_eosp(sta);
  316. }
  317. spin_unlock_bh(&hwq->lock);
  318. }
  319. EXPORT_SYMBOL_GPL(mt76_release_buffered_frames);
  320. static bool
  321. mt76_txq_stopped(struct mt76_queue *q)
  322. {
  323. return q->stopped || q->blocked ||
  324. q->queued + MT_TXQ_FREE_THR >= q->ndesc;
  325. }
  326. static int
  327. mt76_txq_send_burst(struct mt76_phy *phy, struct mt76_queue *q,
  328. struct mt76_txq *mtxq)
  329. {
  330. struct mt76_dev *dev = phy->dev;
  331. struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
  332. enum mt76_txq_id qid = mt76_txq_get_qid(txq);
  333. struct mt76_wcid *wcid = mtxq->wcid;
  334. struct ieee80211_tx_info *info;
  335. struct sk_buff *skb;
  336. int n_frames = 1;
  337. bool stop = false;
  338. int idx;
  339. if (test_bit(MT_WCID_FLAG_PS, &wcid->flags))
  340. return 0;
  341. if (atomic_read(&wcid->non_aql_packets) >= MT_MAX_NON_AQL_PKT)
  342. return 0;
  343. skb = mt76_txq_dequeue(phy, mtxq);
  344. if (!skb)
  345. return 0;
  346. info = IEEE80211_SKB_CB(skb);
  347. if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
  348. ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
  349. info->control.rates, 1);
  350. idx = __mt76_tx_queue_skb(phy, qid, skb, wcid, txq->sta, &stop);
  351. if (idx < 0)
  352. return idx;
  353. do {
  354. if (test_bit(MT76_RESET, &phy->state))
  355. return -EBUSY;
  356. if (stop || mt76_txq_stopped(q))
  357. break;
  358. skb = mt76_txq_dequeue(phy, mtxq);
  359. if (!skb)
  360. break;
  361. info = IEEE80211_SKB_CB(skb);
  362. if (!(wcid->tx_info & MT_WCID_TX_INFO_SET))
  363. ieee80211_get_tx_rates(txq->vif, txq->sta, skb,
  364. info->control.rates, 1);
  365. idx = __mt76_tx_queue_skb(phy, qid, skb, wcid, txq->sta, &stop);
  366. if (idx < 0)
  367. break;
  368. n_frames++;
  369. } while (1);
  370. dev->queue_ops->kick(dev, q);
  371. return n_frames;
  372. }
  373. static int
  374. mt76_txq_schedule_list(struct mt76_phy *phy, enum mt76_txq_id qid)
  375. {
  376. struct mt76_queue *q = phy->q_tx[qid];
  377. struct mt76_dev *dev = phy->dev;
  378. struct ieee80211_txq *txq;
  379. struct mt76_txq *mtxq;
  380. struct mt76_wcid *wcid;
  381. int ret = 0;
  382. while (1) {
  383. int n_frames = 0;
  384. if (test_bit(MT76_RESET, &phy->state))
  385. return -EBUSY;
  386. if (dev->queue_ops->tx_cleanup &&
  387. q->queued + 2 * MT_TXQ_FREE_THR >= q->ndesc) {
  388. dev->queue_ops->tx_cleanup(dev, q, false);
  389. }
  390. txq = ieee80211_next_txq(phy->hw, qid);
  391. if (!txq)
  392. break;
  393. mtxq = (struct mt76_txq *)txq->drv_priv;
  394. wcid = mtxq->wcid;
  395. if (wcid && test_bit(MT_WCID_FLAG_PS, &wcid->flags))
  396. continue;
  397. spin_lock_bh(&q->lock);
  398. if (mtxq->send_bar && mtxq->aggr) {
  399. struct ieee80211_txq *txq = mtxq_to_txq(mtxq);
  400. struct ieee80211_sta *sta = txq->sta;
  401. struct ieee80211_vif *vif = txq->vif;
  402. u16 agg_ssn = mtxq->agg_ssn;
  403. u8 tid = txq->tid;
  404. mtxq->send_bar = false;
  405. spin_unlock_bh(&q->lock);
  406. ieee80211_send_bar(vif, sta->addr, tid, agg_ssn);
  407. spin_lock_bh(&q->lock);
  408. }
  409. if (!mt76_txq_stopped(q))
  410. n_frames = mt76_txq_send_burst(phy, q, mtxq);
  411. spin_unlock_bh(&q->lock);
  412. ieee80211_return_txq(phy->hw, txq, false);
  413. if (unlikely(n_frames < 0))
  414. return n_frames;
  415. ret += n_frames;
  416. }
  417. return ret;
  418. }
  419. void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid)
  420. {
  421. int len;
  422. if (qid >= 4)
  423. return;
  424. rcu_read_lock();
  425. do {
  426. ieee80211_txq_schedule_start(phy->hw, qid);
  427. len = mt76_txq_schedule_list(phy, qid);
  428. ieee80211_txq_schedule_end(phy->hw, qid);
  429. } while (len > 0);
  430. rcu_read_unlock();
  431. }
  432. EXPORT_SYMBOL_GPL(mt76_txq_schedule);
  433. void mt76_txq_schedule_all(struct mt76_phy *phy)
  434. {
  435. int i;
  436. for (i = 0; i <= MT_TXQ_BK; i++)
  437. mt76_txq_schedule(phy, i);
  438. }
  439. EXPORT_SYMBOL_GPL(mt76_txq_schedule_all);
  440. void mt76_tx_worker_run(struct mt76_dev *dev)
  441. {
  442. mt76_txq_schedule_all(&dev->phy);
  443. if (dev->phy2)
  444. mt76_txq_schedule_all(dev->phy2);
  445. #ifdef CONFIG_NL80211_TESTMODE
  446. if (dev->phy.test.tx_pending)
  447. mt76_testmode_tx_pending(&dev->phy);
  448. if (dev->phy2 && dev->phy2->test.tx_pending)
  449. mt76_testmode_tx_pending(dev->phy2);
  450. #endif
  451. }
  452. EXPORT_SYMBOL_GPL(mt76_tx_worker_run);
  453. void mt76_tx_worker(struct mt76_worker *w)
  454. {
  455. struct mt76_dev *dev = container_of(w, struct mt76_dev, tx_worker);
  456. mt76_tx_worker_run(dev);
  457. }
  458. void mt76_stop_tx_queues(struct mt76_phy *phy, struct ieee80211_sta *sta,
  459. bool send_bar)
  460. {
  461. int i;
  462. for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
  463. struct ieee80211_txq *txq = sta->txq[i];
  464. struct mt76_queue *hwq;
  465. struct mt76_txq *mtxq;
  466. if (!txq)
  467. continue;
  468. hwq = phy->q_tx[mt76_txq_get_qid(txq)];
  469. mtxq = (struct mt76_txq *)txq->drv_priv;
  470. spin_lock_bh(&hwq->lock);
  471. mtxq->send_bar = mtxq->aggr && send_bar;
  472. spin_unlock_bh(&hwq->lock);
  473. }
  474. }
  475. EXPORT_SYMBOL_GPL(mt76_stop_tx_queues);
  476. void mt76_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
  477. {
  478. struct mt76_phy *phy = hw->priv;
  479. struct mt76_dev *dev = phy->dev;
  480. if (!test_bit(MT76_STATE_RUNNING, &phy->state))
  481. return;
  482. mt76_worker_schedule(&dev->tx_worker);
  483. }
  484. EXPORT_SYMBOL_GPL(mt76_wake_tx_queue);
  485. u8 mt76_ac_to_hwq(u8 ac)
  486. {
  487. static const u8 wmm_queue_map[] = {
  488. [IEEE80211_AC_BE] = 0,
  489. [IEEE80211_AC_BK] = 1,
  490. [IEEE80211_AC_VI] = 2,
  491. [IEEE80211_AC_VO] = 3,
  492. };
  493. if (WARN_ON(ac >= IEEE80211_NUM_ACS))
  494. return 0;
  495. return wmm_queue_map[ac];
  496. }
  497. EXPORT_SYMBOL_GPL(mt76_ac_to_hwq);
  498. int mt76_skb_adjust_pad(struct sk_buff *skb, int pad)
  499. {
  500. struct sk_buff *iter, *last = skb;
  501. /* First packet of a A-MSDU burst keeps track of the whole burst
  502. * length, need to update length of it and the last packet.
  503. */
  504. skb_walk_frags(skb, iter) {
  505. last = iter;
  506. if (!iter->next) {
  507. skb->data_len += pad;
  508. skb->len += pad;
  509. break;
  510. }
  511. }
  512. if (skb_pad(last, pad))
  513. return -ENOMEM;
  514. __skb_put(last, pad);
  515. return 0;
  516. }
  517. EXPORT_SYMBOL_GPL(mt76_skb_adjust_pad);
  518. void mt76_queue_tx_complete(struct mt76_dev *dev, struct mt76_queue *q,
  519. struct mt76_queue_entry *e)
  520. {
  521. if (e->skb)
  522. dev->drv->tx_complete_skb(dev, e);
  523. spin_lock_bh(&q->lock);
  524. q->tail = (q->tail + 1) % q->ndesc;
  525. q->queued--;
  526. spin_unlock_bh(&q->lock);
  527. }
  528. EXPORT_SYMBOL_GPL(mt76_queue_tx_complete);
  529. void __mt76_set_tx_blocked(struct mt76_dev *dev, bool blocked)
  530. {
  531. struct mt76_phy *phy = &dev->phy, *phy2 = dev->phy2;
  532. struct mt76_queue *q, *q2 = NULL;
  533. q = phy->q_tx[0];
  534. if (blocked == q->blocked)
  535. return;
  536. q->blocked = blocked;
  537. if (phy2) {
  538. q2 = phy2->q_tx[0];
  539. q2->blocked = blocked;
  540. }
  541. if (!blocked)
  542. mt76_worker_schedule(&dev->tx_worker);
  543. }
  544. EXPORT_SYMBOL_GPL(__mt76_set_tx_blocked);
  545. int mt76_token_consume(struct mt76_dev *dev, struct mt76_txwi_cache **ptxwi)
  546. {
  547. int token;
  548. spin_lock_bh(&dev->token_lock);
  549. token = idr_alloc(&dev->token, *ptxwi, 0, dev->drv->token_size,
  550. GFP_ATOMIC);
  551. if (token >= 0)
  552. dev->token_count++;
  553. if (dev->token_count >= dev->drv->token_size - MT76_TOKEN_FREE_THR)
  554. __mt76_set_tx_blocked(dev, true);
  555. spin_unlock_bh(&dev->token_lock);
  556. return token;
  557. }
  558. EXPORT_SYMBOL_GPL(mt76_token_consume);
  559. struct mt76_txwi_cache *
  560. mt76_token_release(struct mt76_dev *dev, int token, bool *wake)
  561. {
  562. struct mt76_txwi_cache *txwi;
  563. spin_lock_bh(&dev->token_lock);
  564. txwi = idr_remove(&dev->token, token);
  565. if (txwi)
  566. dev->token_count--;
  567. if (dev->token_count < dev->drv->token_size - MT76_TOKEN_FREE_THR &&
  568. dev->phy.q_tx[0]->blocked)
  569. *wake = true;
  570. spin_unlock_bh(&dev->token_lock);
  571. return txwi;
  572. }
  573. EXPORT_SYMBOL_GPL(mt76_token_release);