8e94e620e6f47f2c42ae6671cb7c5db803477f21
[pandora-kernel.git] / drivers / net / wireless / mwifiex / 11n_rxreorder.c
1 /*
2  * Marvell Wireless LAN device driver: 802.11n RX Re-ordering
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27 #include "11n_rxreorder.h"
28
29 /*
30  * This function processes a received packet and forwards
31  * it to the kernel/upper layer.
32  */
33 static int mwifiex_11n_dispatch_pkt(struct mwifiex_private *priv, void *payload)
34 {
35         int ret = 0;
36         struct mwifiex_adapter *adapter = priv->adapter;
37
38         ret = mwifiex_process_rx_packet(adapter, (struct sk_buff *) payload);
39         return ret;
40 }
41
42 /*
43  * This function dispatches all packets in the Rx reorder table.
44  *
45  * There could be holes in the buffer, which are skipped by the function.
46  * Since the buffer is linear, the function uses rotation to simulate
47  * circular buffer.
48  */
49 static int
50 mwifiex_11n_dispatch_pkt_until_start_win(struct mwifiex_private *priv,
51                                          struct mwifiex_rx_reorder_tbl
52                                          *rx_reor_tbl_ptr, int start_win)
53 {
54         int no_pkt_to_send, i, xchg;
55         void *rx_tmp_ptr = NULL;
56         unsigned long flags;
57
58         no_pkt_to_send = (start_win > rx_reor_tbl_ptr->start_win) ?
59                 min((start_win - rx_reor_tbl_ptr->start_win),
60                     rx_reor_tbl_ptr->win_size) : rx_reor_tbl_ptr->win_size;
61
62         for (i = 0; i < no_pkt_to_send; ++i) {
63                 spin_lock_irqsave(&priv->rx_pkt_lock, flags);
64                 rx_tmp_ptr = NULL;
65                 if (rx_reor_tbl_ptr->rx_reorder_ptr[i]) {
66                         rx_tmp_ptr = rx_reor_tbl_ptr->rx_reorder_ptr[i];
67                         rx_reor_tbl_ptr->rx_reorder_ptr[i] = NULL;
68                 }
69                 spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
70                 if (rx_tmp_ptr)
71                         mwifiex_11n_dispatch_pkt(priv, rx_tmp_ptr);
72         }
73
74         spin_lock_irqsave(&priv->rx_pkt_lock, flags);
75         /*
76          * We don't have a circular buffer, hence use rotation to simulate
77          * circular buffer
78          */
79         xchg = rx_reor_tbl_ptr->win_size - no_pkt_to_send;
80         for (i = 0; i < xchg; ++i) {
81                 rx_reor_tbl_ptr->rx_reorder_ptr[i] =
82                         rx_reor_tbl_ptr->rx_reorder_ptr[no_pkt_to_send + i];
83                 rx_reor_tbl_ptr->rx_reorder_ptr[no_pkt_to_send + i] = NULL;
84         }
85
86         rx_reor_tbl_ptr->start_win = start_win;
87         spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
88
89         return 0;
90 }
91
92 /*
93  * This function dispatches all packets in the Rx reorder table until
94  * a hole is found.
95  *
96  * The start window is adjusted automatically when a hole is located.
97  * Since the buffer is linear, the function uses rotation to simulate
98  * circular buffer.
99  */
100 static int
101 mwifiex_11n_scan_and_dispatch(struct mwifiex_private *priv,
102                               struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr)
103 {
104         int i, j, xchg;
105         void *rx_tmp_ptr = NULL;
106         unsigned long flags;
107
108         for (i = 0; i < rx_reor_tbl_ptr->win_size; ++i) {
109                 spin_lock_irqsave(&priv->rx_pkt_lock, flags);
110                 if (!rx_reor_tbl_ptr->rx_reorder_ptr[i]) {
111                         spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
112                         break;
113                 }
114                 rx_tmp_ptr = rx_reor_tbl_ptr->rx_reorder_ptr[i];
115                 rx_reor_tbl_ptr->rx_reorder_ptr[i] = NULL;
116                 spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
117                 mwifiex_11n_dispatch_pkt(priv, rx_tmp_ptr);
118         }
119
120         spin_lock_irqsave(&priv->rx_pkt_lock, flags);
121         /*
122          * We don't have a circular buffer, hence use rotation to simulate
123          * circular buffer
124          */
125         if (i > 0) {
126                 xchg = rx_reor_tbl_ptr->win_size - i;
127                 for (j = 0; j < xchg; ++j) {
128                         rx_reor_tbl_ptr->rx_reorder_ptr[j] =
129                                 rx_reor_tbl_ptr->rx_reorder_ptr[i + j];
130                         rx_reor_tbl_ptr->rx_reorder_ptr[i + j] = NULL;
131                 }
132         }
133         rx_reor_tbl_ptr->start_win = (rx_reor_tbl_ptr->start_win + i)
134                 &(MAX_TID_VALUE - 1);
135         spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
136         return 0;
137 }
138
139 /*
140  * This function deletes the Rx reorder table and frees the memory.
141  *
142  * The function stops the associated timer and dispatches all the
143  * pending packets in the Rx reorder table before deletion.
144  */
145 static void
146 mwifiex_11n_delete_rx_reorder_tbl_entry(struct mwifiex_private *priv,
147                                        struct mwifiex_rx_reorder_tbl
148                                        *rx_reor_tbl_ptr)
149 {
150         unsigned long flags;
151
152         if (!rx_reor_tbl_ptr)
153                 return;
154
155         mwifiex_11n_dispatch_pkt_until_start_win(priv, rx_reor_tbl_ptr,
156                                                  (rx_reor_tbl_ptr->start_win +
157                                                   rx_reor_tbl_ptr->win_size)
158                                                  &(MAX_TID_VALUE - 1));
159
160         del_timer(&rx_reor_tbl_ptr->timer_context.timer);
161
162         spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
163         list_del(&rx_reor_tbl_ptr->list);
164         spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
165
166         kfree(rx_reor_tbl_ptr->rx_reorder_ptr);
167         kfree(rx_reor_tbl_ptr);
168 }
169
170 /*
171  * This function returns the pointer to an entry in Rx reordering
172  * table which matches the given TA/TID pair.
173  */
174 static struct mwifiex_rx_reorder_tbl *
175 mwifiex_11n_get_rx_reorder_tbl(struct mwifiex_private *priv, int tid, u8 *ta)
176 {
177         struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr;
178         unsigned long flags;
179
180         spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
181         list_for_each_entry(rx_reor_tbl_ptr, &priv->rx_reorder_tbl_ptr, list) {
182                 if ((!memcmp(rx_reor_tbl_ptr->ta, ta, ETH_ALEN))
183                     && (rx_reor_tbl_ptr->tid == tid)) {
184                         spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock,
185                                                flags);
186                         return rx_reor_tbl_ptr;
187                 }
188         }
189         spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
190
191         return NULL;
192 }
193
194 /*
195  * This function finds the last sequence number used in the packets
196  * buffered in Rx reordering table.
197  */
198 static int
199 mwifiex_11n_find_last_seq_num(struct mwifiex_rx_reorder_tbl *rx_reorder_tbl_ptr)
200 {
201         int i;
202
203         for (i = (rx_reorder_tbl_ptr->win_size - 1); i >= 0; --i)
204                 if (rx_reorder_tbl_ptr->rx_reorder_ptr[i])
205                         return i;
206
207         return -1;
208 }
209
210 /*
211  * This function flushes all the packets in Rx reordering table.
212  *
213  * The function checks if any packets are currently buffered in the
214  * table or not. In case there are packets available, it dispatches
215  * them and then dumps the Rx reordering table.
216  */
217 static void
218 mwifiex_flush_data(unsigned long context)
219 {
220         struct reorder_tmr_cnxt *reorder_cnxt =
221                 (struct reorder_tmr_cnxt *) context;
222         int start_win;
223
224         start_win = mwifiex_11n_find_last_seq_num(reorder_cnxt->ptr);
225         if (start_win >= 0) {
226                 dev_dbg(reorder_cnxt->priv->adapter->dev,
227                                 "info: flush data %d\n", start_win);
228                 mwifiex_11n_dispatch_pkt_until_start_win(reorder_cnxt->priv,
229                                 reorder_cnxt->ptr,
230                                 ((reorder_cnxt->ptr->start_win +
231                                   start_win + 1) & (MAX_TID_VALUE - 1)));
232         }
233 }
234
235 /*
236  * This function creates an entry in Rx reordering table for the
237  * given TA/TID.
238  *
239  * The function also initializes the entry with sequence number, window
240  * size as well as initializes the timer.
241  *
242  * If the received TA/TID pair is already present, all the packets are
243  * dispatched and the window size is moved until the SSN.
244  */
245 static void
246 mwifiex_11n_create_rx_reorder_tbl(struct mwifiex_private *priv, u8 *ta,
247                                  int tid, int win_size, int seq_num)
248 {
249         int i;
250         struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr, *new_node;
251         u16 last_seq = 0;
252         unsigned long flags;
253
254         /*
255          * If we get a TID, ta pair which is already present dispatch all the
256          * the packets and move the window size until the ssn
257          */
258         rx_reor_tbl_ptr = mwifiex_11n_get_rx_reorder_tbl(priv, tid, ta);
259         if (rx_reor_tbl_ptr) {
260                 mwifiex_11n_dispatch_pkt_until_start_win(priv, rx_reor_tbl_ptr,
261                                                          seq_num);
262                 return;
263         }
264         /* if !rx_reor_tbl_ptr then create one */
265         new_node = kzalloc(sizeof(struct mwifiex_rx_reorder_tbl), GFP_KERNEL);
266         if (!new_node) {
267                 dev_err(priv->adapter->dev, "%s: failed to alloc new_node\n",
268                        __func__);
269                 return;
270         }
271
272         INIT_LIST_HEAD(&new_node->list);
273         new_node->tid = tid;
274         memcpy(new_node->ta, ta, ETH_ALEN);
275         new_node->start_win = seq_num;
276         if (mwifiex_queuing_ra_based(priv))
277                 /* TODO for adhoc */
278                 dev_dbg(priv->adapter->dev,
279                         "info: ADHOC:last_seq=%d start_win=%d\n",
280                         last_seq, new_node->start_win);
281         else
282                 last_seq = priv->rx_seq[tid];
283
284         if (last_seq >= new_node->start_win)
285                 new_node->start_win = last_seq + 1;
286
287         new_node->win_size = win_size;
288
289         new_node->rx_reorder_ptr = kzalloc(sizeof(void *) * win_size,
290                                         GFP_KERNEL);
291         if (!new_node->rx_reorder_ptr) {
292                 kfree((u8 *) new_node);
293                 dev_err(priv->adapter->dev,
294                         "%s: failed to alloc reorder_ptr\n", __func__);
295                 return;
296         }
297
298         new_node->timer_context.ptr = new_node;
299         new_node->timer_context.priv = priv;
300
301         init_timer(&new_node->timer_context.timer);
302         new_node->timer_context.timer.function = mwifiex_flush_data;
303         new_node->timer_context.timer.data =
304                         (unsigned long) &new_node->timer_context;
305
306         for (i = 0; i < win_size; ++i)
307                 new_node->rx_reorder_ptr[i] = NULL;
308
309         spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
310         list_add_tail(&new_node->list, &priv->rx_reorder_tbl_ptr);
311         spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
312
313         return;
314 }
315
316 /*
317  * This function prepares command for adding a BA request.
318  *
319  * Preparation includes -
320  *      - Setting command ID and proper size
321  *      - Setting add BA request buffer
322  *      - Ensuring correct endian-ness
323  */
324 int mwifiex_cmd_11n_addba_req(struct mwifiex_private *priv,
325                               struct host_cmd_ds_command *cmd, void *data_buf)
326 {
327         struct host_cmd_ds_11n_addba_req *add_ba_req =
328                 (struct host_cmd_ds_11n_addba_req *)
329                 &cmd->params.add_ba_req;
330
331         cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_REQ);
332         cmd->size = cpu_to_le16(sizeof(*add_ba_req) + S_DS_GEN);
333         memcpy(add_ba_req, data_buf, sizeof(*add_ba_req));
334
335         return 0;
336 }
337
338 /*
339  * This function prepares command for adding a BA response.
340  *
341  * Preparation includes -
342  *      - Setting command ID and proper size
343  *      - Setting add BA response buffer
344  *      - Ensuring correct endian-ness
345  */
346 int mwifiex_cmd_11n_addba_rsp_gen(struct mwifiex_private *priv,
347                                   struct host_cmd_ds_command *cmd,
348                                   void *data_buf)
349 {
350         struct host_cmd_ds_11n_addba_rsp *add_ba_rsp =
351                 (struct host_cmd_ds_11n_addba_rsp *)
352                 &cmd->params.add_ba_rsp;
353         struct host_cmd_ds_11n_addba_req *cmd_addba_req =
354                 (struct host_cmd_ds_11n_addba_req *) data_buf;
355         u8 tid = 0;
356         int win_size = 0;
357         uint16_t block_ack_param_set;
358
359         cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_RSP);
360         cmd->size = cpu_to_le16(sizeof(*add_ba_rsp) + S_DS_GEN);
361
362         memcpy(add_ba_rsp->peer_mac_addr, cmd_addba_req->peer_mac_addr,
363                ETH_ALEN);
364         add_ba_rsp->dialog_token = cmd_addba_req->dialog_token;
365         add_ba_rsp->block_ack_tmo = cmd_addba_req->block_ack_tmo;
366         add_ba_rsp->ssn = cmd_addba_req->ssn;
367
368         block_ack_param_set = le16_to_cpu(cmd_addba_req->block_ack_param_set);
369         tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
370                 >> BLOCKACKPARAM_TID_POS;
371         add_ba_rsp->status_code = cpu_to_le16(ADDBA_RSP_STATUS_ACCEPT);
372         block_ack_param_set &= ~IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK;
373         /* We donot support AMSDU inside AMPDU, hence reset the bit */
374         block_ack_param_set &= ~BLOCKACKPARAM_AMSDU_SUPP_MASK;
375         block_ack_param_set |= (priv->add_ba_param.rx_win_size <<
376                                              BLOCKACKPARAM_WINSIZE_POS);
377         add_ba_rsp->block_ack_param_set = cpu_to_le16(block_ack_param_set);
378         win_size = (le16_to_cpu(add_ba_rsp->block_ack_param_set)
379                                         & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
380                                         >> BLOCKACKPARAM_WINSIZE_POS;
381         cmd_addba_req->block_ack_param_set = cpu_to_le16(block_ack_param_set);
382
383         mwifiex_11n_create_rx_reorder_tbl(priv, cmd_addba_req->peer_mac_addr,
384                             tid, win_size, le16_to_cpu(cmd_addba_req->ssn));
385         return 0;
386 }
387
388 /*
389  * This function prepares command for deleting a BA request.
390  *
391  * Preparation includes -
392  *      - Setting command ID and proper size
393  *      - Setting del BA request buffer
394  *      - Ensuring correct endian-ness
395  */
396 int mwifiex_cmd_11n_delba(struct mwifiex_private *priv,
397                           struct host_cmd_ds_command *cmd, void *data_buf)
398 {
399         struct host_cmd_ds_11n_delba *del_ba = (struct host_cmd_ds_11n_delba *)
400                 &cmd->params.del_ba;
401
402         cmd->command = cpu_to_le16(HostCmd_CMD_11N_DELBA);
403         cmd->size = cpu_to_le16(sizeof(*del_ba) + S_DS_GEN);
404         memcpy(del_ba, data_buf, sizeof(*del_ba));
405
406         return 0;
407 }
408
409 /*
410  * This function identifies if Rx reordering is needed for a received packet.
411  *
412  * In case reordering is required, the function will do the reordering
413  * before sending it to kernel.
414  *
415  * The Rx reorder table is checked first with the received TID/TA pair. If
416  * not found, the received packet is dispatched immediately. But if found,
417  * the packet is reordered and all the packets in the updated Rx reordering
418  * table is dispatched until a hole is found.
419  *
420  * For sequence number less than the starting window, the packet is dropped.
421  */
422 int mwifiex_11n_rx_reorder_pkt(struct mwifiex_private *priv,
423                                 u16 seq_num, u16 tid,
424                                 u8 *ta, u8 pkt_type, void *payload)
425 {
426         struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr;
427         int start_win, end_win, win_size;
428         int ret = 0;
429         u16 pkt_index = 0;
430
431         rx_reor_tbl_ptr =
432                 mwifiex_11n_get_rx_reorder_tbl((struct mwifiex_private *) priv,
433                                                 tid, ta);
434         if (!rx_reor_tbl_ptr) {
435                 if (pkt_type != PKT_TYPE_BAR)
436                         mwifiex_11n_dispatch_pkt(priv, payload);
437                 return 0;
438         }
439         start_win = rx_reor_tbl_ptr->start_win;
440         win_size = rx_reor_tbl_ptr->win_size;
441         end_win = ((start_win + win_size) - 1) & (MAX_TID_VALUE - 1);
442         del_timer(&rx_reor_tbl_ptr->timer_context.timer);
443         mod_timer(&rx_reor_tbl_ptr->timer_context.timer, jiffies
444                         + (MIN_FLUSH_TIMER_MS * win_size * HZ) / 1000);
445
446         /*
447          * If seq_num is less then starting win then ignore and drop the
448          * packet
449          */
450         if ((start_win + TWOPOW11) > (MAX_TID_VALUE - 1)) {/* Wrap */
451                 if (seq_num >= ((start_win + (TWOPOW11)) & (MAX_TID_VALUE - 1))
452                                 && (seq_num < start_win))
453                         return -1;
454         } else if ((seq_num < start_win)
455                         || (seq_num > (start_win + (TWOPOW11)))) {
456                 return -1;
457         }
458
459         /*
460          * If this packet is a BAR we adjust seq_num as
461          * WinStart = seq_num
462          */
463         if (pkt_type == PKT_TYPE_BAR)
464                 seq_num = ((seq_num + win_size) - 1) & (MAX_TID_VALUE - 1);
465
466         if (((end_win < start_win)
467              && (seq_num < (TWOPOW11 - (MAX_TID_VALUE - start_win)))
468              && (seq_num > end_win)) || ((end_win > start_win)
469              && ((seq_num > end_win) || (seq_num < start_win)))) {
470                 end_win = seq_num;
471                 if (((seq_num - win_size) + 1) >= 0)
472                         start_win = (end_win - win_size) + 1;
473                 else
474                         start_win = (MAX_TID_VALUE - (win_size - seq_num)) + 1;
475                 ret = mwifiex_11n_dispatch_pkt_until_start_win(priv,
476                                                 rx_reor_tbl_ptr, start_win);
477
478                 if (ret)
479                         return ret;
480         }
481
482         if (pkt_type != PKT_TYPE_BAR) {
483                 if (seq_num >= start_win)
484                         pkt_index = seq_num - start_win;
485                 else
486                         pkt_index = (seq_num+MAX_TID_VALUE) - start_win;
487
488                 if (rx_reor_tbl_ptr->rx_reorder_ptr[pkt_index])
489                         return -1;
490
491                 rx_reor_tbl_ptr->rx_reorder_ptr[pkt_index] = payload;
492         }
493
494         /*
495          * Dispatch all packets sequentially from start_win until a
496          * hole is found and adjust the start_win appropriately
497          */
498         ret = mwifiex_11n_scan_and_dispatch(priv, rx_reor_tbl_ptr);
499
500         return ret;
501 }
502
503 /*
504  * This function deletes an entry for a given TID/TA pair.
505  *
506  * The TID/TA are taken from del BA event body.
507  */
508 void
509 mwifiex_11n_delete_ba_stream_tbl(struct mwifiex_private *priv, int tid,
510                                 u8 *peer_mac, u8 type, int initiator)
511 {
512         struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr;
513         struct mwifiex_tx_ba_stream_tbl *ptx_tbl;
514         u8 cleanup_rx_reorder_tbl;
515         unsigned long flags;
516
517         if (type == TYPE_DELBA_RECEIVE)
518                 cleanup_rx_reorder_tbl = (initiator) ? true : false;
519         else
520                 cleanup_rx_reorder_tbl = (initiator) ? false : true;
521
522         dev_dbg(priv->adapter->dev, "event: DELBA: %pM tid=%d, "
523                "initiator=%d\n", peer_mac, tid, initiator);
524
525         if (cleanup_rx_reorder_tbl) {
526                 rx_reor_tbl_ptr = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
527                                                                  peer_mac);
528                 if (!rx_reor_tbl_ptr) {
529                         dev_dbg(priv->adapter->dev,
530                                         "event: TID, TA not found in table\n");
531                         return;
532                 }
533                 mwifiex_11n_delete_rx_reorder_tbl_entry(priv, rx_reor_tbl_ptr);
534         } else {
535                 ptx_tbl = mwifiex_11n_get_tx_ba_stream_tbl(priv, tid, peer_mac);
536                 if (!ptx_tbl) {
537                         dev_dbg(priv->adapter->dev,
538                                         "event: TID, RA not found in table\n");
539                         return;
540                 }
541
542                 spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
543                 mwifiex_11n_delete_tx_ba_stream_tbl_entry(priv, ptx_tbl);
544                 spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
545         }
546 }
547
548 /*
549  * This function handles the command response of an add BA response.
550  *
551  * Handling includes changing the header fields into CPU format and
552  * creating the stream, provided the add BA is accepted.
553  */
554 int mwifiex_ret_11n_addba_resp(struct mwifiex_private *priv,
555                                struct host_cmd_ds_command *resp)
556 {
557         struct host_cmd_ds_11n_addba_rsp *add_ba_rsp =
558                 (struct host_cmd_ds_11n_addba_rsp *)
559                 &resp->params.add_ba_rsp;
560         int tid, win_size;
561         struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr = NULL;
562         uint16_t block_ack_param_set;
563
564         block_ack_param_set = le16_to_cpu(add_ba_rsp->block_ack_param_set);
565
566         tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
567                 >> BLOCKACKPARAM_TID_POS;
568         /*
569          * Check if we had rejected the ADDBA, if yes then do not create
570          * the stream
571          */
572         if (le16_to_cpu(add_ba_rsp->status_code) == BA_RESULT_SUCCESS) {
573                 win_size = (block_ack_param_set &
574                         IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
575                         >> BLOCKACKPARAM_WINSIZE_POS;
576
577                 dev_dbg(priv->adapter->dev, "cmd: ADDBA RSP: %pM"
578                        " tid=%d ssn=%d win_size=%d\n",
579                        add_ba_rsp->peer_mac_addr,
580                        tid, add_ba_rsp->ssn, win_size);
581         } else {
582                 dev_err(priv->adapter->dev, "ADDBA RSP: failed %pM tid=%d)\n",
583                                         add_ba_rsp->peer_mac_addr, tid);
584
585                 rx_reor_tbl_ptr = mwifiex_11n_get_rx_reorder_tbl(priv,
586                                         tid, add_ba_rsp->peer_mac_addr);
587                 if (rx_reor_tbl_ptr)
588                         mwifiex_11n_delete_rx_reorder_tbl_entry(priv,
589                                 rx_reor_tbl_ptr);
590         }
591
592         return 0;
593 }
594
595 /*
596  * This function handles BA stream timeout event by preparing and sending
597  * a command to the firmware.
598  */
599 void mwifiex_11n_ba_stream_timeout(struct mwifiex_private *priv,
600                                    struct host_cmd_ds_11n_batimeout *event)
601 {
602         struct host_cmd_ds_11n_delba delba;
603
604         memset(&delba, 0, sizeof(struct host_cmd_ds_11n_delba));
605         memcpy(delba.peer_mac_addr, event->peer_mac_addr, ETH_ALEN);
606
607         delba.del_ba_param_set |=
608                 cpu_to_le16((u16) event->tid << DELBA_TID_POS);
609         delba.del_ba_param_set |= cpu_to_le16(
610                 (u16) event->origninator << DELBA_INITIATOR_POS);
611         delba.reason_code = cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
612         mwifiex_prepare_cmd(priv, HostCmd_CMD_11N_DELBA, 0, 0, NULL, &delba);
613
614         return;
615 }
616
617 /*
618  * This function cleans up the Rx reorder table by deleting all the entries
619  * and re-initializing.
620  */
621 void mwifiex_11n_cleanup_reorder_tbl(struct mwifiex_private *priv)
622 {
623         struct mwifiex_rx_reorder_tbl *del_tbl_ptr, *tmp_node;
624         unsigned long flags;
625
626         spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
627         list_for_each_entry_safe(del_tbl_ptr, tmp_node,
628                                  &priv->rx_reorder_tbl_ptr, list) {
629                 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
630                 mwifiex_11n_delete_rx_reorder_tbl_entry(priv, del_tbl_ptr);
631                 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
632         }
633         spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
634
635         INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
636         memset(priv->rx_seq, 0, sizeof(priv->rx_seq));
637 }