rx_reorder.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2014-2015 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. /* called in NAPI context */
  78. void wil_rx_reorder(struct wil6210_priv *wil, struct sk_buff *skb)
  79. __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
  80. {
  81. struct net_device *ndev = wil_to_ndev(wil);
  82. struct vring_rx_desc *d = wil_skb_rxdesc(skb);
  83. int tid = wil_rxdesc_tid(d);
  84. int cid = wil_rxdesc_cid(d);
  85. int mid = wil_rxdesc_mid(d);
  86. u16 seq = wil_rxdesc_seq(d);
  87. int mcast = wil_rxdesc_mcast(d);
  88. struct wil_sta_info *sta = &wil->sta[cid];
  89. struct wil_tid_ampdu_rx *r;
  90. u16 hseq;
  91. int index;
  92. wil_dbg_txrx(wil, "MID %d CID %d TID %d Seq 0x%03x mcast %01x\n",
  93. mid, cid, tid, seq, mcast);
  94. if (unlikely(mcast)) {
  95. wil_netif_rx_any(skb, ndev);
  96. return;
  97. }
  98. spin_lock(&sta->tid_rx_lock);
  99. r = sta->tid_rx[tid];
  100. if (!r) {
  101. wil_netif_rx_any(skb, ndev);
  102. goto out;
  103. }
  104. hseq = r->head_seq_num;
  105. /** Due to the race between WMI events, where BACK establishment
  106. * reported, and data Rx, few packets may be pass up before reorder
  107. * buffer get allocated. Catch up by pretending SSN is what we
  108. * see in the 1-st Rx packet
  109. *
  110. * Another scenario, Rx get delayed and we got packet from before
  111. * BACK. Pass it to the stack and wait.
  112. */
  113. if (r->first_time) {
  114. r->first_time = false;
  115. if (seq != r->head_seq_num) {
  116. if (seq_less(seq, r->head_seq_num)) {
  117. wil_err(wil,
  118. "Error: frame with early sequence 0x%03x, should be 0x%03x. Waiting...\n",
  119. seq, r->head_seq_num);
  120. r->first_time = true;
  121. wil_netif_rx_any(skb, ndev);
  122. goto out;
  123. }
  124. wil_err(wil,
  125. "Error: 1-st frame with wrong sequence 0x%03x, should be 0x%03x. Fixing...\n",
  126. seq, r->head_seq_num);
  127. r->head_seq_num = seq;
  128. r->ssn = seq;
  129. }
  130. }
  131. /* frame with out of date sequence number */
  132. if (seq_less(seq, r->head_seq_num)) {
  133. r->ssn_last_drop = seq;
  134. dev_kfree_skb(skb);
  135. goto out;
  136. }
  137. /*
  138. * If frame the sequence number exceeds our buffering window
  139. * size release some previous frames to make room for this one.
  140. */
  141. if (!seq_less(seq, r->head_seq_num + r->buf_size)) {
  142. hseq = seq_inc(seq_sub(seq, r->buf_size));
  143. /* release stored frames up to new head to stack */
  144. wil_release_reorder_frames(wil, r, hseq);
  145. }
  146. /* Now the new frame is always in the range of the reordering buffer */
  147. index = reorder_index(r, seq);
  148. /* check if we already stored this frame */
  149. if (r->reorder_buf[index]) {
  150. dev_kfree_skb(skb);
  151. goto out;
  152. }
  153. /*
  154. * If the current MPDU is in the right order and nothing else
  155. * is stored we can process it directly, no need to buffer it.
  156. * If it is first but there's something stored, we may be able
  157. * to release frames after this one.
  158. */
  159. if (seq == r->head_seq_num && r->stored_mpdu_num == 0) {
  160. r->head_seq_num = seq_inc(r->head_seq_num);
  161. wil_netif_rx_any(skb, ndev);
  162. goto out;
  163. }
  164. /* put the frame in the reordering buffer */
  165. r->reorder_buf[index] = skb;
  166. r->reorder_time[index] = jiffies;
  167. r->stored_mpdu_num++;
  168. wil_reorder_release(wil, r);
  169. out:
  170. spin_unlock(&sta->tid_rx_lock);
  171. }
  172. struct wil_tid_ampdu_rx *wil_tid_ampdu_rx_alloc(struct wil6210_priv *wil,
  173. int size, u16 ssn)
  174. {
  175. struct wil_tid_ampdu_rx *r = kzalloc(sizeof(*r), GFP_KERNEL);
  176. if (!r)
  177. return NULL;
  178. r->reorder_buf =
  179. kcalloc(size, sizeof(struct sk_buff *), GFP_KERNEL);
  180. r->reorder_time =
  181. kcalloc(size, sizeof(unsigned long), GFP_KERNEL);
  182. if (!r->reorder_buf || !r->reorder_time) {
  183. kfree(r->reorder_buf);
  184. kfree(r->reorder_time);
  185. kfree(r);
  186. return NULL;
  187. }
  188. r->ssn = ssn;
  189. r->head_seq_num = ssn;
  190. r->buf_size = size;
  191. r->stored_mpdu_num = 0;
  192. r->first_time = true;
  193. return r;
  194. }
  195. void wil_tid_ampdu_rx_free(struct wil6210_priv *wil,
  196. struct wil_tid_ampdu_rx *r)
  197. {
  198. if (!r)
  199. return;
  200. wil_release_reorder_frames(wil, r, r->head_seq_num + r->buf_size);
  201. kfree(r->reorder_buf);
  202. kfree(r->reorder_time);
  203. kfree(r);
  204. }
  205. /* ADDBA processing */
  206. static u16 wil_agg_size(struct wil6210_priv *wil, u16 req_agg_wsize)
  207. {
  208. u16 max_agg_size = min_t(u16, WIL_MAX_AGG_WSIZE, WIL_MAX_AMPDU_SIZE /
  209. (mtu_max + WIL_MAX_MPDU_OVERHEAD));
  210. if (!req_agg_wsize)
  211. return max_agg_size;
  212. return min(max_agg_size, req_agg_wsize);
  213. }
  214. /* Block Ack - Rx side (recipient */
  215. int wil_addba_rx_request(struct wil6210_priv *wil, u8 cidxtid,
  216. u8 dialog_token, __le16 ba_param_set,
  217. __le16 ba_timeout, __le16 ba_seq_ctrl)
  218. {
  219. struct wil_back_rx *req = kzalloc(sizeof(*req), GFP_KERNEL);
  220. if (!req)
  221. return -ENOMEM;
  222. req->cidxtid = cidxtid;
  223. req->dialog_token = dialog_token;
  224. req->ba_param_set = le16_to_cpu(ba_param_set);
  225. req->ba_timeout = le16_to_cpu(ba_timeout);
  226. req->ba_seq_ctrl = le16_to_cpu(ba_seq_ctrl);
  227. mutex_lock(&wil->back_rx_mutex);
  228. list_add_tail(&req->list, &wil->back_rx_pending);
  229. mutex_unlock(&wil->back_rx_mutex);
  230. queue_work(wil->wq_service, &wil->back_rx_worker);
  231. return 0;
  232. }
  233. static void wil_back_rx_handle(struct wil6210_priv *wil,
  234. struct wil_back_rx *req)
  235. __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
  236. {
  237. struct wil_sta_info *sta;
  238. u8 cid, tid;
  239. u16 agg_wsize = 0;
  240. /* bit 0: A-MSDU supported
  241. * bit 1: policy (should be 0 for us)
  242. * bits 2..5: TID
  243. * bits 6..15: buffer size
  244. */
  245. u16 req_agg_wsize = WIL_GET_BITS(req->ba_param_set, 6, 15);
  246. bool agg_amsdu = !!(req->ba_param_set & BIT(0));
  247. int ba_policy = req->ba_param_set & BIT(1);
  248. u16 agg_timeout = req->ba_timeout;
  249. u16 status = WLAN_STATUS_SUCCESS;
  250. u16 ssn = req->ba_seq_ctrl >> 4;
  251. struct wil_tid_ampdu_rx *r;
  252. int rc;
  253. might_sleep();
  254. parse_cidxtid(req->cidxtid, &cid, &tid);
  255. /* sanity checks */
  256. if (cid >= WIL6210_MAX_CID) {
  257. wil_err(wil, "BACK: invalid CID %d\n", cid);
  258. return;
  259. }
  260. sta = &wil->sta[cid];
  261. if (sta->status != wil_sta_connected) {
  262. wil_err(wil, "BACK: CID %d not connected\n", cid);
  263. return;
  264. }
  265. wil_dbg_wmi(wil,
  266. "ADDBA request for CID %d %pM TID %d size %d timeout %d AMSDU%s policy %d token %d SSN 0x%03x\n",
  267. cid, sta->addr, tid, req_agg_wsize, req->ba_timeout,
  268. agg_amsdu ? "+" : "-", !!ba_policy, req->dialog_token, ssn);
  269. /* apply policies */
  270. if (ba_policy) {
  271. wil_err(wil, "BACK requested unsupported ba_policy == 1\n");
  272. status = WLAN_STATUS_INVALID_QOS_PARAM;
  273. }
  274. if (status == WLAN_STATUS_SUCCESS)
  275. agg_wsize = wil_agg_size(wil, req_agg_wsize);
  276. rc = wmi_addba_rx_resp(wil, cid, tid, req->dialog_token, status,
  277. agg_amsdu, agg_wsize, agg_timeout);
  278. if (rc || (status != WLAN_STATUS_SUCCESS))
  279. return;
  280. /* apply */
  281. r = wil_tid_ampdu_rx_alloc(wil, agg_wsize, ssn);
  282. spin_lock_bh(&sta->tid_rx_lock);
  283. wil_tid_ampdu_rx_free(wil, sta->tid_rx[tid]);
  284. sta->tid_rx[tid] = r;
  285. spin_unlock_bh(&sta->tid_rx_lock);
  286. }
  287. void wil_back_rx_flush(struct wil6210_priv *wil)
  288. {
  289. struct wil_back_rx *evt, *t;
  290. wil_dbg_misc(wil, "%s()\n", __func__);
  291. mutex_lock(&wil->back_rx_mutex);
  292. list_for_each_entry_safe(evt, t, &wil->back_rx_pending, list) {
  293. list_del(&evt->list);
  294. kfree(evt);
  295. }
  296. mutex_unlock(&wil->back_rx_mutex);
  297. }
  298. /* Retrieve next ADDBA request from the pending list */
  299. static struct list_head *next_back_rx(struct wil6210_priv *wil)
  300. {
  301. struct list_head *ret = NULL;
  302. mutex_lock(&wil->back_rx_mutex);
  303. if (!list_empty(&wil->back_rx_pending)) {
  304. ret = wil->back_rx_pending.next;
  305. list_del(ret);
  306. }
  307. mutex_unlock(&wil->back_rx_mutex);
  308. return ret;
  309. }
  310. void wil_back_rx_worker(struct work_struct *work)
  311. {
  312. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  313. back_rx_worker);
  314. struct wil_back_rx *evt;
  315. struct list_head *lh;
  316. while ((lh = next_back_rx(wil)) != NULL) {
  317. evt = list_entry(lh, struct wil_back_rx, list);
  318. wil_back_rx_handle(wil, evt);
  319. kfree(evt);
  320. }
  321. }
  322. /* BACK - Tx (originator) side */
  323. static void wil_back_tx_handle(struct wil6210_priv *wil,
  324. struct wil_back_tx *req)
  325. {
  326. struct vring_tx_data *txdata = &wil->vring_tx_data[req->ringid];
  327. int rc;
  328. if (txdata->addba_in_progress) {
  329. wil_dbg_misc(wil, "ADDBA for vring[%d] already in progress\n",
  330. req->ringid);
  331. return;
  332. }
  333. if (txdata->agg_wsize) {
  334. wil_dbg_misc(wil,
  335. "ADDBA for vring[%d] already established wsize %d\n",
  336. req->ringid, txdata->agg_wsize);
  337. return;
  338. }
  339. txdata->addba_in_progress = true;
  340. rc = wmi_addba(wil, req->ringid, req->agg_wsize, req->agg_timeout);
  341. if (rc)
  342. txdata->addba_in_progress = false;
  343. }
  344. static struct list_head *next_back_tx(struct wil6210_priv *wil)
  345. {
  346. struct list_head *ret = NULL;
  347. mutex_lock(&wil->back_tx_mutex);
  348. if (!list_empty(&wil->back_tx_pending)) {
  349. ret = wil->back_tx_pending.next;
  350. list_del(ret);
  351. }
  352. mutex_unlock(&wil->back_tx_mutex);
  353. return ret;
  354. }
  355. void wil_back_tx_worker(struct work_struct *work)
  356. {
  357. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  358. back_tx_worker);
  359. struct wil_back_tx *evt;
  360. struct list_head *lh;
  361. while ((lh = next_back_tx(wil)) != NULL) {
  362. evt = list_entry(lh, struct wil_back_tx, list);
  363. wil_back_tx_handle(wil, evt);
  364. kfree(evt);
  365. }
  366. }
  367. void wil_back_tx_flush(struct wil6210_priv *wil)
  368. {
  369. struct wil_back_tx *evt, *t;
  370. wil_dbg_misc(wil, "%s()\n", __func__);
  371. mutex_lock(&wil->back_tx_mutex);
  372. list_for_each_entry_safe(evt, t, &wil->back_tx_pending, list) {
  373. list_del(&evt->list);
  374. kfree(evt);
  375. }
  376. mutex_unlock(&wil->back_tx_mutex);
  377. }
  378. int wil_addba_tx_request(struct wil6210_priv *wil, u8 ringid, u16 wsize)
  379. {
  380. struct wil_back_tx *req = kzalloc(sizeof(*req), GFP_KERNEL);
  381. if (!req)
  382. return -ENOMEM;
  383. req->ringid = ringid;
  384. req->agg_wsize = wil_agg_size(wil, wsize);
  385. req->agg_timeout = 0;
  386. mutex_lock(&wil->back_tx_mutex);
  387. list_add_tail(&req->list, &wil->back_tx_pending);
  388. mutex_unlock(&wil->back_tx_mutex);
  389. queue_work(wil->wq_service, &wil->back_tx_worker);
  390. return 0;
  391. }