rx_reorder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2014 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "wil6210.h"
  17. #include "txrx.h"
  18. #define SEQ_MODULO 0x1000
  19. #define SEQ_MASK 0xfff
  20. static inline int seq_less(u16 sq1, u16 sq2)
  21. {
  22. return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
  23. }
  24. static inline u16 seq_inc(u16 sq)
  25. {
  26. return (sq + 1) & SEQ_MASK;
  27. }
  28. static inline u16 seq_sub(u16 sq1, u16 sq2)
  29. {
  30. return (sq1 - sq2) & SEQ_MASK;
  31. }
  32. static inline int reorder_index(struct wil_tid_ampdu_rx *r, u16 seq)
  33. {
  34. return seq_sub(seq, r->ssn) % r->buf_size;
  35. }
  36. static void wil_release_reorder_frame(struct wil6210_priv *wil,
  37. struct wil_tid_ampdu_rx *r,
  38. int index)
  39. {
  40. struct net_device *ndev = wil_to_ndev(wil);
  41. struct sk_buff *skb = r->reorder_buf[index];
  42. if (!skb)
  43. goto no_frame;
  44. /* release the frame from the reorder ring buffer */
  45. r->stored_mpdu_num--;
  46. r->reorder_buf[index] = NULL;
  47. wil_netif_rx_any(skb, ndev);
  48. no_frame:
  49. r->head_seq_num = seq_inc(r->head_seq_num);
  50. }
  51. static void wil_release_reorder_frames(struct wil6210_priv *wil,
  52. struct wil_tid_ampdu_rx *r,
  53. u16 hseq)
  54. {
  55. int index;
  56. /* note: this function is never called with
  57. * hseq preceding r->head_seq_num, i.e it is always true
  58. * !seq_less(hseq, r->head_seq_num)
  59. * and thus on loop exit it should be
  60. * r->head_seq_num == hseq
  61. */
  62. while (seq_less(r->head_seq_num, hseq) && r->stored_mpdu_num) {
  63. index = reorder_index(r, r->head_seq_num);
  64. wil_release_reorder_frame(wil, r, index);
  65. }
  66. r->head_seq_num = hseq;
  67. }
  68. static void wil_reorder_release(struct wil6210_priv *wil,
  69. struct wil_tid_ampdu_rx *r)
  70. {
  71. int index = reorder_index(r, r->head_seq_num);
  72. while (r->reorder_buf[index]) {
  73. wil_release_reorder_frame(wil, r, index);
  74. index = reorder_index(r, r->head_seq_num);
  75. }
  76. }
  77. void wil_rx_reorder(struct wil6210_priv *wil, struct sk_buff *skb)
  78. {
  79. struct net_device *ndev = wil_to_ndev(wil);
  80. struct vring_rx_desc *d = wil_skb_rxdesc(skb);
  81. int tid = wil_rxdesc_tid(d);
  82. int cid = wil_rxdesc_cid(d);
  83. int mid = wil_rxdesc_mid(d);
  84. u16 seq = wil_rxdesc_seq(d);
  85. struct wil_sta_info *sta = &wil->sta[cid];
  86. struct wil_tid_ampdu_rx *r;
  87. u16 hseq;
  88. int index;
  89. unsigned long flags;
  90. wil_dbg_txrx(wil, "MID %d CID %d TID %d Seq 0x%03x\n",
  91. mid, cid, tid, seq);
  92. spin_lock_irqsave(&sta->tid_rx_lock, flags);
  93. r = sta->tid_rx[tid];
  94. if (!r) {
  95. spin_unlock_irqrestore(&sta->tid_rx_lock, flags);
  96. wil_netif_rx_any(skb, ndev);
  97. return;
  98. }
  99. hseq = r->head_seq_num;
  100. /** Due to the race between WMI events, where BACK establishment
  101. * reported, and data Rx, few packets may be pass up before reorder
  102. * buffer get allocated. Catch up by pretending SSN is what we
  103. * see in the 1-st Rx packet
  104. */
  105. if (r->first_time) {
  106. r->first_time = false;
  107. if (seq != r->head_seq_num) {
  108. wil_err(wil, "Error: 1-st frame with wrong sequence"
  109. " %d, should be %d. Fixing...\n", seq,
  110. r->head_seq_num);
  111. r->head_seq_num = seq;
  112. r->ssn = seq;
  113. }
  114. }
  115. /* frame with out of date sequence number */
  116. if (seq_less(seq, r->head_seq_num)) {
  117. r->ssn_last_drop = seq;
  118. dev_kfree_skb(skb);
  119. goto out;
  120. }
  121. /*
  122. * If frame the sequence number exceeds our buffering window
  123. * size release some previous frames to make room for this one.
  124. */
  125. if (!seq_less(seq, r->head_seq_num + r->buf_size)) {
  126. hseq = seq_inc(seq_sub(seq, r->buf_size));
  127. /* release stored frames up to new head to stack */
  128. wil_release_reorder_frames(wil, r, hseq);
  129. }
  130. /* Now the new frame is always in the range of the reordering buffer */
  131. index = reorder_index(r, seq);
  132. /* check if we already stored this frame */
  133. if (r->reorder_buf[index]) {
  134. dev_kfree_skb(skb);
  135. goto out;
  136. }
  137. /*
  138. * If the current MPDU is in the right order and nothing else
  139. * is stored we can process it directly, no need to buffer it.
  140. * If it is first but there's something stored, we may be able
  141. * to release frames after this one.
  142. */
  143. if (seq == r->head_seq_num && r->stored_mpdu_num == 0) {
  144. r->head_seq_num = seq_inc(r->head_seq_num);
  145. wil_netif_rx_any(skb, ndev);
  146. goto out;
  147. }
  148. /* put the frame in the reordering buffer */
  149. r->reorder_buf[index] = skb;
  150. r->reorder_time[index] = jiffies;
  151. r->stored_mpdu_num++;
  152. wil_reorder_release(wil, r);
  153. out:
  154. spin_unlock_irqrestore(&sta->tid_rx_lock, flags);
  155. }
  156. struct wil_tid_ampdu_rx *wil_tid_ampdu_rx_alloc(struct wil6210_priv *wil,
  157. int size, u16 ssn)
  158. {
  159. struct wil_tid_ampdu_rx *r = kzalloc(sizeof(*r), GFP_KERNEL);
  160. if (!r)
  161. return NULL;
  162. r->reorder_buf =
  163. kcalloc(size, sizeof(struct sk_buff *), GFP_KERNEL);
  164. r->reorder_time =
  165. kcalloc(size, sizeof(unsigned long), GFP_KERNEL);
  166. if (!r->reorder_buf || !r->reorder_time) {
  167. kfree(r->reorder_buf);
  168. kfree(r->reorder_time);
  169. kfree(r);
  170. return NULL;
  171. }
  172. r->ssn = ssn;
  173. r->head_seq_num = ssn;
  174. r->buf_size = size;
  175. r->stored_mpdu_num = 0;
  176. r->first_time = true;
  177. return r;
  178. }
  179. void wil_tid_ampdu_rx_free(struct wil6210_priv *wil,
  180. struct wil_tid_ampdu_rx *r)
  181. {
  182. if (!r)
  183. return;
  184. wil_release_reorder_frames(wil, r, r->head_seq_num + r->buf_size);
  185. kfree(r->reorder_buf);
  186. kfree(r->reorder_time);
  187. kfree(r);
  188. }
  189. /* ADDBA processing */
  190. static u16 wil_agg_size(struct wil6210_priv *wil, u16 req_agg_wsize)
  191. {
  192. u16 max_agg_size = min_t(u16, WIL_MAX_AGG_WSIZE, WIL_MAX_AMPDU_SIZE /
  193. (mtu_max + WIL_MAX_MPDU_OVERHEAD));
  194. if (!req_agg_wsize)
  195. return max_agg_size;
  196. return min(max_agg_size, req_agg_wsize);
  197. }
  198. /* Block Ack - Rx side (recipient */
  199. int wil_addba_rx_request(struct wil6210_priv *wil, u8 cidxtid,
  200. u8 dialog_token, __le16 ba_param_set,
  201. __le16 ba_timeout, __le16 ba_seq_ctrl)
  202. {
  203. struct wil_back_rx *req = kzalloc(sizeof(*req), GFP_KERNEL);
  204. if (!req)
  205. return -ENOMEM;
  206. req->cidxtid = cidxtid;
  207. req->dialog_token = dialog_token;
  208. req->ba_param_set = le16_to_cpu(ba_param_set);
  209. req->ba_timeout = le16_to_cpu(ba_timeout);
  210. req->ba_seq_ctrl = le16_to_cpu(ba_seq_ctrl);
  211. mutex_lock(&wil->back_rx_mutex);
  212. list_add_tail(&req->list, &wil->back_rx_pending);
  213. mutex_unlock(&wil->back_rx_mutex);
  214. queue_work(wil->wq_service, &wil->back_rx_worker);
  215. return 0;
  216. }
  217. static void wil_back_rx_handle(struct wil6210_priv *wil,
  218. struct wil_back_rx *req)
  219. {
  220. struct wil_sta_info *sta;
  221. u8 cid, tid;
  222. u16 agg_wsize = 0;
  223. /* bit 0: A-MSDU supported
  224. * bit 1: policy (should be 0 for us)
  225. * bits 2..5: TID
  226. * bits 6..15: buffer size
  227. */
  228. u16 req_agg_wsize = WIL_GET_BITS(req->ba_param_set, 6, 15);
  229. bool agg_amsdu = !!(req->ba_param_set & BIT(0));
  230. int ba_policy = req->ba_param_set & BIT(1);
  231. u16 agg_timeout = req->ba_timeout;
  232. u16 status = WLAN_STATUS_SUCCESS;
  233. unsigned long flags;
  234. int rc;
  235. parse_cidxtid(req->cidxtid, &cid, &tid);
  236. /* sanity checks */
  237. if (cid >= WIL6210_MAX_CID) {
  238. wil_err(wil, "BACK: invalid CID %d\n", cid);
  239. return;
  240. }
  241. sta = &wil->sta[cid];
  242. if (sta->status != wil_sta_connected) {
  243. wil_err(wil, "BACK: CID %d not connected\n", cid);
  244. return;
  245. }
  246. wil_dbg_wmi(wil,
  247. "ADDBA request for CID %d %pM TID %d size %d timeout %d AMSDU%s policy %d token %d\n",
  248. cid, sta->addr, tid, req_agg_wsize, req->ba_timeout,
  249. agg_amsdu ? "+" : "-", !!ba_policy, req->dialog_token);
  250. /* apply policies */
  251. if (ba_policy) {
  252. wil_err(wil, "BACK requested unsupported ba_policy == 1\n");
  253. status = WLAN_STATUS_INVALID_QOS_PARAM;
  254. }
  255. if (status == WLAN_STATUS_SUCCESS)
  256. agg_wsize = wil_agg_size(wil, req_agg_wsize);
  257. rc = wmi_addba_rx_resp(wil, cid, tid, req->dialog_token, status,
  258. agg_amsdu, agg_wsize, agg_timeout);
  259. if (rc || (status != WLAN_STATUS_SUCCESS))
  260. return;
  261. /* apply */
  262. spin_lock_irqsave(&sta->tid_rx_lock, flags);
  263. wil_tid_ampdu_rx_free(wil, sta->tid_rx[tid]);
  264. sta->tid_rx[tid] = wil_tid_ampdu_rx_alloc(wil, agg_wsize,
  265. req->ba_seq_ctrl >> 4);
  266. spin_unlock_irqrestore(&sta->tid_rx_lock, flags);
  267. }
  268. void wil_back_rx_flush(struct wil6210_priv *wil)
  269. {
  270. struct wil_back_rx *evt, *t;
  271. wil_dbg_misc(wil, "%s()\n", __func__);
  272. mutex_lock(&wil->back_rx_mutex);
  273. list_for_each_entry_safe(evt, t, &wil->back_rx_pending, list) {
  274. list_del(&evt->list);
  275. kfree(evt);
  276. }
  277. mutex_unlock(&wil->back_rx_mutex);
  278. }
  279. /* Retrieve next ADDBA request from the pending list */
  280. static struct list_head *next_back_rx(struct wil6210_priv *wil)
  281. {
  282. struct list_head *ret = NULL;
  283. mutex_lock(&wil->back_rx_mutex);
  284. if (!list_empty(&wil->back_rx_pending)) {
  285. ret = wil->back_rx_pending.next;
  286. list_del(ret);
  287. }
  288. mutex_unlock(&wil->back_rx_mutex);
  289. return ret;
  290. }
  291. void wil_back_rx_worker(struct work_struct *work)
  292. {
  293. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  294. back_rx_worker);
  295. struct wil_back_rx *evt;
  296. struct list_head *lh;
  297. while ((lh = next_back_rx(wil)) != NULL) {
  298. evt = list_entry(lh, struct wil_back_rx, list);
  299. wil_back_rx_handle(wil, evt);
  300. kfree(evt);
  301. }
  302. }
  303. /* BACK - Tx (originator) side */
  304. static void wil_back_tx_handle(struct wil6210_priv *wil,
  305. struct wil_back_tx *req)
  306. {
  307. struct vring_tx_data *txdata = &wil->vring_tx_data[req->ringid];
  308. int rc;
  309. if (txdata->addba_in_progress) {
  310. wil_dbg_misc(wil, "ADDBA for vring[%d] already in progress\n",
  311. req->ringid);
  312. return;
  313. }
  314. if (txdata->agg_wsize) {
  315. wil_dbg_misc(wil,
  316. "ADDBA for vring[%d] already established wsize %d\n",
  317. req->ringid, txdata->agg_wsize);
  318. return;
  319. }
  320. txdata->addba_in_progress = true;
  321. rc = wmi_addba(wil, req->ringid, req->agg_wsize, req->agg_timeout);
  322. if (rc)
  323. txdata->addba_in_progress = false;
  324. }
  325. static struct list_head *next_back_tx(struct wil6210_priv *wil)
  326. {
  327. struct list_head *ret = NULL;
  328. mutex_lock(&wil->back_tx_mutex);
  329. if (!list_empty(&wil->back_tx_pending)) {
  330. ret = wil->back_tx_pending.next;
  331. list_del(ret);
  332. }
  333. mutex_unlock(&wil->back_tx_mutex);
  334. return ret;
  335. }
  336. void wil_back_tx_worker(struct work_struct *work)
  337. {
  338. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  339. back_tx_worker);
  340. struct wil_back_tx *evt;
  341. struct list_head *lh;
  342. while ((lh = next_back_tx(wil)) != NULL) {
  343. evt = list_entry(lh, struct wil_back_tx, list);
  344. wil_back_tx_handle(wil, evt);
  345. kfree(evt);
  346. }
  347. }
  348. void wil_back_tx_flush(struct wil6210_priv *wil)
  349. {
  350. struct wil_back_tx *evt, *t;
  351. wil_dbg_misc(wil, "%s()\n", __func__);
  352. mutex_lock(&wil->back_tx_mutex);
  353. list_for_each_entry_safe(evt, t, &wil->back_tx_pending, list) {
  354. list_del(&evt->list);
  355. kfree(evt);
  356. }
  357. mutex_unlock(&wil->back_tx_mutex);
  358. }
  359. int wil_addba_tx_request(struct wil6210_priv *wil, u8 ringid)
  360. {
  361. struct wil_back_tx *req = kzalloc(sizeof(*req), GFP_KERNEL);
  362. if (!req)
  363. return -ENOMEM;
  364. req->ringid = ringid;
  365. req->agg_wsize = wil_agg_size(wil, 0);
  366. req->agg_timeout = 0;
  367. mutex_lock(&wil->back_tx_mutex);
  368. list_add_tail(&req->list, &wil->back_tx_pending);
  369. mutex_unlock(&wil->back_tx_mutex);
  370. queue_work(wil->wq_service, &wil->back_tx_worker);
  371. return 0;
  372. }