tx.c 16 KB

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