queue.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Queue between the tx operation and the bh workqueue.
  4. *
  5. * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
  6. * Copyright (c) 2010, ST-Ericsson
  7. */
  8. #include <linux/sched.h>
  9. #include <net/mac80211.h>
  10. #include "queue.h"
  11. #include "wfx.h"
  12. #include "sta.h"
  13. #include "data_tx.h"
  14. #include "traces.h"
  15. void wfx_tx_lock(struct wfx_dev *wdev)
  16. {
  17. atomic_inc(&wdev->tx_lock);
  18. }
  19. void wfx_tx_unlock(struct wfx_dev *wdev)
  20. {
  21. int tx_lock = atomic_dec_return(&wdev->tx_lock);
  22. WARN(tx_lock < 0, "inconsistent tx_lock value");
  23. if (!tx_lock)
  24. wfx_bh_request_tx(wdev);
  25. }
  26. void wfx_tx_flush(struct wfx_dev *wdev)
  27. {
  28. int ret;
  29. /* Do not wait for any reply if chip is frozen */
  30. if (wdev->chip_frozen)
  31. return;
  32. wfx_tx_lock(wdev);
  33. mutex_lock(&wdev->hif_cmd.lock);
  34. ret = wait_event_timeout(wdev->hif.tx_buffers_empty, !wdev->hif.tx_buffers_used,
  35. msecs_to_jiffies(3000));
  36. if (!ret) {
  37. dev_warn(wdev->dev, "cannot flush tx buffers (%d still busy)\n",
  38. wdev->hif.tx_buffers_used);
  39. wfx_pending_dump_old_frames(wdev, 3000);
  40. /* FIXME: drop pending frames here */
  41. wdev->chip_frozen = true;
  42. }
  43. mutex_unlock(&wdev->hif_cmd.lock);
  44. wfx_tx_unlock(wdev);
  45. }
  46. void wfx_tx_lock_flush(struct wfx_dev *wdev)
  47. {
  48. wfx_tx_lock(wdev);
  49. wfx_tx_flush(wdev);
  50. }
  51. void wfx_tx_queues_init(struct wfx_vif *wvif)
  52. {
  53. /* The device is in charge to respect the details of the QoS parameters. The driver just
  54. * ensure that it roughtly respect the priorities to avoid any shortage.
  55. */
  56. const int priorities[IEEE80211_NUM_ACS] = { 1, 2, 64, 128 };
  57. int i;
  58. for (i = 0; i < IEEE80211_NUM_ACS; ++i) {
  59. skb_queue_head_init(&wvif->tx_queue[i].normal);
  60. skb_queue_head_init(&wvif->tx_queue[i].cab);
  61. skb_queue_head_init(&wvif->tx_queue[i].offchan);
  62. wvif->tx_queue[i].priority = priorities[i];
  63. }
  64. }
  65. bool wfx_tx_queue_empty(struct wfx_vif *wvif, struct wfx_queue *queue)
  66. {
  67. return skb_queue_empty_lockless(&queue->normal) &&
  68. skb_queue_empty_lockless(&queue->cab) &&
  69. skb_queue_empty_lockless(&queue->offchan);
  70. }
  71. void wfx_tx_queues_check_empty(struct wfx_vif *wvif)
  72. {
  73. int i;
  74. for (i = 0; i < IEEE80211_NUM_ACS; ++i) {
  75. WARN_ON(atomic_read(&wvif->tx_queue[i].pending_frames));
  76. WARN_ON(!wfx_tx_queue_empty(wvif, &wvif->tx_queue[i]));
  77. }
  78. }
  79. static void __wfx_tx_queue_drop(struct wfx_vif *wvif,
  80. struct sk_buff_head *skb_queue, struct sk_buff_head *dropped)
  81. {
  82. struct sk_buff *skb, *tmp;
  83. spin_lock_bh(&skb_queue->lock);
  84. skb_queue_walk_safe(skb_queue, skb, tmp) {
  85. __skb_unlink(skb, skb_queue);
  86. skb_queue_head(dropped, skb);
  87. }
  88. spin_unlock_bh(&skb_queue->lock);
  89. }
  90. void wfx_tx_queue_drop(struct wfx_vif *wvif, struct wfx_queue *queue,
  91. struct sk_buff_head *dropped)
  92. {
  93. __wfx_tx_queue_drop(wvif, &queue->normal, dropped);
  94. __wfx_tx_queue_drop(wvif, &queue->cab, dropped);
  95. __wfx_tx_queue_drop(wvif, &queue->offchan, dropped);
  96. wake_up(&wvif->wdev->tx_dequeue);
  97. }
  98. void wfx_tx_queues_put(struct wfx_vif *wvif, struct sk_buff *skb)
  99. {
  100. struct wfx_queue *queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
  101. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  102. if (tx_info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  103. skb_queue_tail(&queue->offchan, skb);
  104. else if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
  105. skb_queue_tail(&queue->cab, skb);
  106. else
  107. skb_queue_tail(&queue->normal, skb);
  108. }
  109. void wfx_pending_drop(struct wfx_dev *wdev, struct sk_buff_head *dropped)
  110. {
  111. struct wfx_queue *queue;
  112. struct wfx_vif *wvif;
  113. struct sk_buff *skb;
  114. WARN(!wdev->chip_frozen, "%s should only be used to recover a frozen device", __func__);
  115. while ((skb = skb_dequeue(&wdev->tx_pending)) != NULL) {
  116. wvif = wfx_skb_wvif(wdev, skb);
  117. if (wvif) {
  118. queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
  119. WARN_ON(skb_get_queue_mapping(skb) > 3);
  120. WARN_ON(!atomic_read(&queue->pending_frames));
  121. atomic_dec(&queue->pending_frames);
  122. }
  123. skb_queue_head(dropped, skb);
  124. }
  125. }
  126. struct sk_buff *wfx_pending_get(struct wfx_dev *wdev, u32 packet_id)
  127. {
  128. struct wfx_queue *queue;
  129. struct wfx_hif_req_tx *req;
  130. struct wfx_vif *wvif;
  131. struct wfx_hif_msg *hif;
  132. struct sk_buff *skb;
  133. spin_lock_bh(&wdev->tx_pending.lock);
  134. skb_queue_walk(&wdev->tx_pending, skb) {
  135. hif = (struct wfx_hif_msg *)skb->data;
  136. req = (struct wfx_hif_req_tx *)hif->body;
  137. if (req->packet_id != packet_id)
  138. continue;
  139. spin_unlock_bh(&wdev->tx_pending.lock);
  140. wvif = wfx_skb_wvif(wdev, skb);
  141. if (wvif) {
  142. queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
  143. WARN_ON(skb_get_queue_mapping(skb) > 3);
  144. WARN_ON(!atomic_read(&queue->pending_frames));
  145. atomic_dec(&queue->pending_frames);
  146. }
  147. skb_unlink(skb, &wdev->tx_pending);
  148. return skb;
  149. }
  150. spin_unlock_bh(&wdev->tx_pending.lock);
  151. WARN(1, "cannot find packet in pending queue");
  152. return NULL;
  153. }
  154. void wfx_pending_dump_old_frames(struct wfx_dev *wdev, unsigned int limit_ms)
  155. {
  156. ktime_t now = ktime_get();
  157. struct wfx_tx_priv *tx_priv;
  158. struct wfx_hif_req_tx *req;
  159. struct sk_buff *skb;
  160. bool first = true;
  161. spin_lock_bh(&wdev->tx_pending.lock);
  162. skb_queue_walk(&wdev->tx_pending, skb) {
  163. tx_priv = wfx_skb_tx_priv(skb);
  164. req = wfx_skb_txreq(skb);
  165. if (ktime_after(now, ktime_add_ms(tx_priv->xmit_timestamp, limit_ms))) {
  166. if (first) {
  167. dev_info(wdev->dev, "frames stuck in firmware since %dms or more:\n",
  168. limit_ms);
  169. first = false;
  170. }
  171. dev_info(wdev->dev, " id %08x sent %lldms ago\n",
  172. req->packet_id, ktime_ms_delta(now, tx_priv->xmit_timestamp));
  173. }
  174. }
  175. spin_unlock_bh(&wdev->tx_pending.lock);
  176. }
  177. unsigned int wfx_pending_get_pkt_us_delay(struct wfx_dev *wdev, struct sk_buff *skb)
  178. {
  179. ktime_t now = ktime_get();
  180. struct wfx_tx_priv *tx_priv = wfx_skb_tx_priv(skb);
  181. return ktime_us_delta(now, tx_priv->xmit_timestamp);
  182. }
  183. bool wfx_tx_queues_has_cab(struct wfx_vif *wvif)
  184. {
  185. struct ieee80211_vif *vif = wvif_to_vif(wvif);
  186. int i;
  187. if (vif->type != NL80211_IFTYPE_AP)
  188. return false;
  189. for (i = 0; i < IEEE80211_NUM_ACS; ++i)
  190. /* Note: since only AP can have mcast frames in queue and only one vif can be AP,
  191. * all queued frames has same interface id
  192. */
  193. if (!skb_queue_empty_lockless(&wvif->tx_queue[i].cab))
  194. return true;
  195. return false;
  196. }
  197. static int wfx_tx_queue_get_weight(struct wfx_queue *queue)
  198. {
  199. return atomic_read(&queue->pending_frames) * queue->priority;
  200. }
  201. static struct sk_buff *wfx_tx_queues_get_skb(struct wfx_dev *wdev)
  202. {
  203. struct wfx_queue *queues[IEEE80211_NUM_ACS * ARRAY_SIZE(wdev->vif)];
  204. int i, j, num_queues = 0;
  205. struct wfx_vif *wvif;
  206. struct wfx_hif_msg *hif;
  207. struct sk_buff *skb;
  208. /* sort the queues */
  209. wvif = NULL;
  210. while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
  211. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  212. WARN_ON(num_queues >= ARRAY_SIZE(queues));
  213. queues[num_queues] = &wvif->tx_queue[i];
  214. for (j = num_queues; j > 0; j--)
  215. if (wfx_tx_queue_get_weight(queues[j]) <
  216. wfx_tx_queue_get_weight(queues[j - 1]))
  217. swap(queues[j - 1], queues[j]);
  218. num_queues++;
  219. }
  220. }
  221. wvif = NULL;
  222. while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
  223. for (i = 0; i < num_queues; i++) {
  224. skb = skb_dequeue(&queues[i]->offchan);
  225. if (!skb)
  226. continue;
  227. hif = (struct wfx_hif_msg *)skb->data;
  228. /* Offchan frames are assigned to a special interface.
  229. * The only interface allowed to send data during scan.
  230. */
  231. WARN_ON(hif->interface != 2);
  232. atomic_inc(&queues[i]->pending_frames);
  233. trace_queues_stats(wdev, queues[i]);
  234. return skb;
  235. }
  236. }
  237. if (mutex_is_locked(&wdev->scan_lock))
  238. return NULL;
  239. wvif = NULL;
  240. while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
  241. if (!wvif->after_dtim_tx_allowed)
  242. continue;
  243. for (i = 0; i < num_queues; i++) {
  244. skb = skb_dequeue(&queues[i]->cab);
  245. if (!skb)
  246. continue;
  247. /* Note: since only AP can have mcast frames in queue and only one vif can
  248. * be AP, all queued frames has same interface id
  249. */
  250. hif = (struct wfx_hif_msg *)skb->data;
  251. WARN_ON(hif->interface != wvif->id);
  252. WARN_ON(queues[i] != &wvif->tx_queue[skb_get_queue_mapping(skb)]);
  253. atomic_inc(&queues[i]->pending_frames);
  254. trace_queues_stats(wdev, queues[i]);
  255. return skb;
  256. }
  257. /* No more multicast to sent */
  258. wvif->after_dtim_tx_allowed = false;
  259. schedule_work(&wvif->update_tim_work);
  260. }
  261. for (i = 0; i < num_queues; i++) {
  262. skb = skb_dequeue(&queues[i]->normal);
  263. if (skb) {
  264. atomic_inc(&queues[i]->pending_frames);
  265. trace_queues_stats(wdev, queues[i]);
  266. return skb;
  267. }
  268. }
  269. return NULL;
  270. }
  271. struct wfx_hif_msg *wfx_tx_queues_get(struct wfx_dev *wdev)
  272. {
  273. struct wfx_tx_priv *tx_priv;
  274. struct sk_buff *skb;
  275. if (atomic_read(&wdev->tx_lock))
  276. return NULL;
  277. skb = wfx_tx_queues_get_skb(wdev);
  278. if (!skb)
  279. return NULL;
  280. skb_queue_tail(&wdev->tx_pending, skb);
  281. wake_up(&wdev->tx_dequeue);
  282. tx_priv = wfx_skb_tx_priv(skb);
  283. tx_priv->xmit_timestamp = ktime_get();
  284. return (struct wfx_hif_msg *)skb->data;
  285. }