2 * Marvell Wireless LAN device driver: 802.11n RX Re-ordering
4 * Copyright (C) 2011, Marvell International Ltd.
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.
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.
27 #include "11n_rxreorder.h"
30 * This function dispatches all packets in the Rx reorder table until the
33 * There could be holes in the buffer, which are skipped by the function.
34 * Since the buffer is linear, the function uses rotation to simulate
38 mwifiex_11n_dispatch_pkt(struct mwifiex_private
*priv
,
39 struct mwifiex_rx_reorder_tbl
*tbl
, int start_win
)
45 pkt_to_send
= (start_win
> tbl
->start_win
) ?
46 min((start_win
- tbl
->start_win
), tbl
->win_size
) :
49 for (i
= 0; i
< pkt_to_send
; ++i
) {
50 spin_lock_irqsave(&priv
->rx_pkt_lock
, flags
);
52 if (tbl
->rx_reorder_ptr
[i
]) {
53 rx_tmp_ptr
= tbl
->rx_reorder_ptr
[i
];
54 tbl
->rx_reorder_ptr
[i
] = NULL
;
56 spin_unlock_irqrestore(&priv
->rx_pkt_lock
, flags
);
58 mwifiex_process_rx_packet(priv
->adapter
, rx_tmp_ptr
);
61 spin_lock_irqsave(&priv
->rx_pkt_lock
, flags
);
63 * We don't have a circular buffer, hence use rotation to simulate
66 for (i
= 0; i
< tbl
->win_size
- pkt_to_send
; ++i
) {
67 tbl
->rx_reorder_ptr
[i
] = tbl
->rx_reorder_ptr
[pkt_to_send
+ i
];
68 tbl
->rx_reorder_ptr
[pkt_to_send
+ i
] = NULL
;
71 tbl
->start_win
= start_win
;
72 spin_unlock_irqrestore(&priv
->rx_pkt_lock
, flags
);
76 * This function dispatches all packets in the Rx reorder table until
79 * The start window is adjusted automatically when a hole is located.
80 * Since the buffer is linear, the function uses rotation to simulate
84 mwifiex_11n_scan_and_dispatch(struct mwifiex_private
*priv
,
85 struct mwifiex_rx_reorder_tbl
*tbl
)
91 for (i
= 0; i
< tbl
->win_size
; ++i
) {
92 spin_lock_irqsave(&priv
->rx_pkt_lock
, flags
);
93 if (!tbl
->rx_reorder_ptr
[i
]) {
94 spin_unlock_irqrestore(&priv
->rx_pkt_lock
, flags
);
97 rx_tmp_ptr
= tbl
->rx_reorder_ptr
[i
];
98 tbl
->rx_reorder_ptr
[i
] = NULL
;
99 spin_unlock_irqrestore(&priv
->rx_pkt_lock
, flags
);
100 mwifiex_process_rx_packet(priv
->adapter
, rx_tmp_ptr
);
103 spin_lock_irqsave(&priv
->rx_pkt_lock
, flags
);
105 * We don't have a circular buffer, hence use rotation to simulate
109 xchg
= tbl
->win_size
- i
;
110 for (j
= 0; j
< xchg
; ++j
) {
111 tbl
->rx_reorder_ptr
[j
] = tbl
->rx_reorder_ptr
[i
+ j
];
112 tbl
->rx_reorder_ptr
[i
+ j
] = NULL
;
115 tbl
->start_win
= (tbl
->start_win
+ i
) & (MAX_TID_VALUE
- 1);
116 spin_unlock_irqrestore(&priv
->rx_pkt_lock
, flags
);
120 * This function deletes the Rx reorder table and frees the memory.
122 * The function stops the associated timer and dispatches all the
123 * pending packets in the Rx reorder table before deletion.
126 mwifiex_del_rx_reorder_entry(struct mwifiex_private
*priv
,
127 struct mwifiex_rx_reorder_tbl
*tbl
)
134 mwifiex_11n_dispatch_pkt(priv
, tbl
, (tbl
->start_win
+ tbl
->win_size
) &
135 (MAX_TID_VALUE
- 1));
137 del_timer(&tbl
->timer_context
.timer
);
139 spin_lock_irqsave(&priv
->rx_reorder_tbl_lock
, flags
);
140 list_del(&tbl
->list
);
141 spin_unlock_irqrestore(&priv
->rx_reorder_tbl_lock
, flags
);
143 kfree(tbl
->rx_reorder_ptr
);
148 * This function returns the pointer to an entry in Rx reordering
149 * table which matches the given TA/TID pair.
151 static struct mwifiex_rx_reorder_tbl
*
152 mwifiex_11n_get_rx_reorder_tbl(struct mwifiex_private
*priv
, int tid
, u8
*ta
)
154 struct mwifiex_rx_reorder_tbl
*tbl
;
157 spin_lock_irqsave(&priv
->rx_reorder_tbl_lock
, flags
);
158 list_for_each_entry(tbl
, &priv
->rx_reorder_tbl_ptr
, list
) {
159 if (!memcmp(tbl
->ta
, ta
, ETH_ALEN
) && tbl
->tid
== tid
) {
160 spin_unlock_irqrestore(&priv
->rx_reorder_tbl_lock
,
165 spin_unlock_irqrestore(&priv
->rx_reorder_tbl_lock
, flags
);
171 * This function finds the last sequence number used in the packets
172 * buffered in Rx reordering table.
175 mwifiex_11n_find_last_seq_num(struct mwifiex_rx_reorder_tbl
*rx_reorder_tbl_ptr
)
179 for (i
= (rx_reorder_tbl_ptr
->win_size
- 1); i
>= 0; --i
)
180 if (rx_reorder_tbl_ptr
->rx_reorder_ptr
[i
])
187 * This function flushes all the packets in Rx reordering table.
189 * The function checks if any packets are currently buffered in the
190 * table or not. In case there are packets available, it dispatches
191 * them and then dumps the Rx reordering table.
194 mwifiex_flush_data(unsigned long context
)
196 struct reorder_tmr_cnxt
*ctx
=
197 (struct reorder_tmr_cnxt
*) context
;
200 start_win
= mwifiex_11n_find_last_seq_num(ctx
->ptr
);
205 dev_dbg(ctx
->priv
->adapter
->dev
, "info: flush data %d\n", start_win
);
206 mwifiex_11n_dispatch_pkt(ctx
->priv
, ctx
->ptr
,
207 (ctx
->ptr
->start_win
+ start_win
+ 1) &
208 (MAX_TID_VALUE
- 1));
212 * This function creates an entry in Rx reordering table for the
215 * The function also initializes the entry with sequence number, window
216 * size as well as initializes the timer.
218 * If the received TA/TID pair is already present, all the packets are
219 * dispatched and the window size is moved until the SSN.
222 mwifiex_11n_create_rx_reorder_tbl(struct mwifiex_private
*priv
, u8
*ta
,
223 int tid
, int win_size
, int seq_num
)
226 struct mwifiex_rx_reorder_tbl
*tbl
, *new_node
;
231 * If we get a TID, ta pair which is already present dispatch all the
232 * the packets and move the window size until the ssn
234 tbl
= mwifiex_11n_get_rx_reorder_tbl(priv
, tid
, ta
);
236 mwifiex_11n_dispatch_pkt(priv
, tbl
, seq_num
);
239 /* if !tbl then create one */
240 new_node
= kzalloc(sizeof(struct mwifiex_rx_reorder_tbl
), GFP_KERNEL
);
242 dev_err(priv
->adapter
->dev
, "%s: failed to alloc new_node\n",
247 INIT_LIST_HEAD(&new_node
->list
);
249 memcpy(new_node
->ta
, ta
, ETH_ALEN
);
250 new_node
->start_win
= seq_num
;
251 if (mwifiex_queuing_ra_based(priv
))
253 dev_dbg(priv
->adapter
->dev
,
254 "info: ADHOC:last_seq=%d start_win=%d\n",
255 last_seq
, new_node
->start_win
);
257 last_seq
= priv
->rx_seq
[tid
];
259 if (last_seq
!= MWIFIEX_DEF_11N_RX_SEQ_NUM
&&
260 last_seq
>= new_node
->start_win
)
261 new_node
->start_win
= last_seq
+ 1;
263 new_node
->win_size
= win_size
;
265 new_node
->rx_reorder_ptr
= kzalloc(sizeof(void *) * win_size
,
267 if (!new_node
->rx_reorder_ptr
) {
268 kfree((u8
*) new_node
);
269 dev_err(priv
->adapter
->dev
,
270 "%s: failed to alloc reorder_ptr\n", __func__
);
274 new_node
->timer_context
.ptr
= new_node
;
275 new_node
->timer_context
.priv
= priv
;
277 init_timer(&new_node
->timer_context
.timer
);
278 new_node
->timer_context
.timer
.function
= mwifiex_flush_data
;
279 new_node
->timer_context
.timer
.data
=
280 (unsigned long) &new_node
->timer_context
;
282 for (i
= 0; i
< win_size
; ++i
)
283 new_node
->rx_reorder_ptr
[i
] = NULL
;
285 spin_lock_irqsave(&priv
->rx_reorder_tbl_lock
, flags
);
286 list_add_tail(&new_node
->list
, &priv
->rx_reorder_tbl_ptr
);
287 spin_unlock_irqrestore(&priv
->rx_reorder_tbl_lock
, flags
);
291 * This function prepares command for adding a BA request.
293 * Preparation includes -
294 * - Setting command ID and proper size
295 * - Setting add BA request buffer
296 * - Ensuring correct endian-ness
298 int mwifiex_cmd_11n_addba_req(struct host_cmd_ds_command
*cmd
, void *data_buf
)
300 struct host_cmd_ds_11n_addba_req
*add_ba_req
=
301 (struct host_cmd_ds_11n_addba_req
*)
302 &cmd
->params
.add_ba_req
;
304 cmd
->command
= cpu_to_le16(HostCmd_CMD_11N_ADDBA_REQ
);
305 cmd
->size
= cpu_to_le16(sizeof(*add_ba_req
) + S_DS_GEN
);
306 memcpy(add_ba_req
, data_buf
, sizeof(*add_ba_req
));
312 * This function prepares command for adding a BA response.
314 * Preparation includes -
315 * - Setting command ID and proper size
316 * - Setting add BA response buffer
317 * - Ensuring correct endian-ness
319 int mwifiex_cmd_11n_addba_rsp_gen(struct mwifiex_private
*priv
,
320 struct host_cmd_ds_command
*cmd
,
321 struct host_cmd_ds_11n_addba_req
324 struct host_cmd_ds_11n_addba_rsp
*add_ba_rsp
=
325 (struct host_cmd_ds_11n_addba_rsp
*)
326 &cmd
->params
.add_ba_rsp
;
329 uint16_t block_ack_param_set
;
331 cmd
->command
= cpu_to_le16(HostCmd_CMD_11N_ADDBA_RSP
);
332 cmd
->size
= cpu_to_le16(sizeof(*add_ba_rsp
) + S_DS_GEN
);
334 memcpy(add_ba_rsp
->peer_mac_addr
, cmd_addba_req
->peer_mac_addr
,
336 add_ba_rsp
->dialog_token
= cmd_addba_req
->dialog_token
;
337 add_ba_rsp
->block_ack_tmo
= cmd_addba_req
->block_ack_tmo
;
338 add_ba_rsp
->ssn
= cmd_addba_req
->ssn
;
340 block_ack_param_set
= le16_to_cpu(cmd_addba_req
->block_ack_param_set
);
341 tid
= (block_ack_param_set
& IEEE80211_ADDBA_PARAM_TID_MASK
)
342 >> BLOCKACKPARAM_TID_POS
;
343 add_ba_rsp
->status_code
= cpu_to_le16(ADDBA_RSP_STATUS_ACCEPT
);
344 block_ack_param_set
&= ~IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK
;
345 /* We donot support AMSDU inside AMPDU, hence reset the bit */
346 block_ack_param_set
&= ~BLOCKACKPARAM_AMSDU_SUPP_MASK
;
347 block_ack_param_set
|= (priv
->add_ba_param
.rx_win_size
<<
348 BLOCKACKPARAM_WINSIZE_POS
);
349 add_ba_rsp
->block_ack_param_set
= cpu_to_le16(block_ack_param_set
);
350 win_size
= (le16_to_cpu(add_ba_rsp
->block_ack_param_set
)
351 & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK
)
352 >> BLOCKACKPARAM_WINSIZE_POS
;
353 cmd_addba_req
->block_ack_param_set
= cpu_to_le16(block_ack_param_set
);
355 mwifiex_11n_create_rx_reorder_tbl(priv
, cmd_addba_req
->peer_mac_addr
,
357 le16_to_cpu(cmd_addba_req
->ssn
));
362 * This function prepares command for deleting a BA request.
364 * Preparation includes -
365 * - Setting command ID and proper size
366 * - Setting del BA request buffer
367 * - Ensuring correct endian-ness
369 int mwifiex_cmd_11n_delba(struct host_cmd_ds_command
*cmd
, void *data_buf
)
371 struct host_cmd_ds_11n_delba
*del_ba
= (struct host_cmd_ds_11n_delba
*)
374 cmd
->command
= cpu_to_le16(HostCmd_CMD_11N_DELBA
);
375 cmd
->size
= cpu_to_le16(sizeof(*del_ba
) + S_DS_GEN
);
376 memcpy(del_ba
, data_buf
, sizeof(*del_ba
));
382 * This function identifies if Rx reordering is needed for a received packet.
384 * In case reordering is required, the function will do the reordering
385 * before sending it to kernel.
387 * The Rx reorder table is checked first with the received TID/TA pair. If
388 * not found, the received packet is dispatched immediately. But if found,
389 * the packet is reordered and all the packets in the updated Rx reordering
390 * table is dispatched until a hole is found.
392 * For sequence number less than the starting window, the packet is dropped.
394 int mwifiex_11n_rx_reorder_pkt(struct mwifiex_private
*priv
,
395 u16 seq_num
, u16 tid
,
396 u8
*ta
, u8 pkt_type
, void *payload
)
398 struct mwifiex_rx_reorder_tbl
*tbl
;
399 int start_win
, end_win
, win_size
;
402 tbl
= mwifiex_11n_get_rx_reorder_tbl((struct mwifiex_private
*) priv
,
405 if (pkt_type
!= PKT_TYPE_BAR
)
406 mwifiex_process_rx_packet(priv
->adapter
, payload
);
409 start_win
= tbl
->start_win
;
410 win_size
= tbl
->win_size
;
411 end_win
= ((start_win
+ win_size
) - 1) & (MAX_TID_VALUE
- 1);
412 del_timer(&tbl
->timer_context
.timer
);
413 mod_timer(&tbl
->timer_context
.timer
,
414 jiffies
+ (MIN_FLUSH_TIMER_MS
* win_size
* HZ
) / 1000);
417 * If seq_num is less then starting win then ignore and drop the
420 if ((start_win
+ TWOPOW11
) > (MAX_TID_VALUE
- 1)) {/* Wrap */
421 if (seq_num
>= ((start_win
+ TWOPOW11
) &
422 (MAX_TID_VALUE
- 1)) && (seq_num
< start_win
))
424 } else if ((seq_num
< start_win
) ||
425 (seq_num
> (start_win
+ TWOPOW11
))) {
430 * If this packet is a BAR we adjust seq_num as
433 if (pkt_type
== PKT_TYPE_BAR
)
434 seq_num
= ((seq_num
+ win_size
) - 1) & (MAX_TID_VALUE
- 1);
436 if (((end_win
< start_win
) &&
437 (seq_num
< (TWOPOW11
- (MAX_TID_VALUE
- start_win
))) &&
438 (seq_num
> end_win
)) ||
439 ((end_win
> start_win
) && ((seq_num
> end_win
) ||
440 (seq_num
< start_win
)))) {
442 if (((seq_num
- win_size
) + 1) >= 0)
443 start_win
= (end_win
- win_size
) + 1;
445 start_win
= (MAX_TID_VALUE
- (win_size
- seq_num
)) + 1;
446 mwifiex_11n_dispatch_pkt(priv
, tbl
, start_win
);
449 if (pkt_type
!= PKT_TYPE_BAR
) {
450 if (seq_num
>= start_win
)
451 pkt_index
= seq_num
- start_win
;
453 pkt_index
= (seq_num
+MAX_TID_VALUE
) - start_win
;
455 if (tbl
->rx_reorder_ptr
[pkt_index
])
458 tbl
->rx_reorder_ptr
[pkt_index
] = payload
;
462 * Dispatch all packets sequentially from start_win until a
463 * hole is found and adjust the start_win appropriately
465 mwifiex_11n_scan_and_dispatch(priv
, tbl
);
471 * This function deletes an entry for a given TID/TA pair.
473 * The TID/TA are taken from del BA event body.
476 mwifiex_del_ba_tbl(struct mwifiex_private
*priv
, int tid
, u8
*peer_mac
,
477 u8 type
, int initiator
)
479 struct mwifiex_rx_reorder_tbl
*tbl
;
480 struct mwifiex_tx_ba_stream_tbl
*ptx_tbl
;
481 u8 cleanup_rx_reorder_tbl
;
484 if (type
== TYPE_DELBA_RECEIVE
)
485 cleanup_rx_reorder_tbl
= (initiator
) ? true : false;
487 cleanup_rx_reorder_tbl
= (initiator
) ? false : true;
489 dev_dbg(priv
->adapter
->dev
, "event: DELBA: %pM tid=%d initiator=%d\n",
490 peer_mac
, tid
, initiator
);
492 if (cleanup_rx_reorder_tbl
) {
493 tbl
= mwifiex_11n_get_rx_reorder_tbl(priv
, tid
,
496 dev_dbg(priv
->adapter
->dev
,
497 "event: TID, TA not found in table\n");
500 mwifiex_del_rx_reorder_entry(priv
, tbl
);
502 ptx_tbl
= mwifiex_get_ba_tbl(priv
, tid
, peer_mac
);
504 dev_dbg(priv
->adapter
->dev
,
505 "event: TID, RA not found in table\n");
509 spin_lock_irqsave(&priv
->tx_ba_stream_tbl_lock
, flags
);
510 mwifiex_11n_delete_tx_ba_stream_tbl_entry(priv
, ptx_tbl
);
511 spin_unlock_irqrestore(&priv
->tx_ba_stream_tbl_lock
, flags
);
516 * This function handles the command response of an add BA response.
518 * Handling includes changing the header fields into CPU format and
519 * creating the stream, provided the add BA is accepted.
521 int mwifiex_ret_11n_addba_resp(struct mwifiex_private
*priv
,
522 struct host_cmd_ds_command
*resp
)
524 struct host_cmd_ds_11n_addba_rsp
*add_ba_rsp
=
525 (struct host_cmd_ds_11n_addba_rsp
*)
526 &resp
->params
.add_ba_rsp
;
528 struct mwifiex_rx_reorder_tbl
*tbl
;
529 uint16_t block_ack_param_set
;
531 block_ack_param_set
= le16_to_cpu(add_ba_rsp
->block_ack_param_set
);
533 tid
= (block_ack_param_set
& IEEE80211_ADDBA_PARAM_TID_MASK
)
534 >> BLOCKACKPARAM_TID_POS
;
536 * Check if we had rejected the ADDBA, if yes then do not create
539 if (le16_to_cpu(add_ba_rsp
->status_code
) == BA_RESULT_SUCCESS
) {
540 win_size
= (block_ack_param_set
&
541 IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK
)
542 >> BLOCKACKPARAM_WINSIZE_POS
;
544 dev_dbg(priv
->adapter
->dev
,
545 "cmd: ADDBA RSP: %pM tid=%d ssn=%d win_size=%d\n",
546 add_ba_rsp
->peer_mac_addr
, tid
,
547 add_ba_rsp
->ssn
, win_size
);
549 dev_err(priv
->adapter
->dev
, "ADDBA RSP: failed %pM tid=%d)\n",
550 add_ba_rsp
->peer_mac_addr
, tid
);
552 tbl
= mwifiex_11n_get_rx_reorder_tbl(priv
, tid
,
553 add_ba_rsp
->peer_mac_addr
);
555 mwifiex_del_rx_reorder_entry(priv
, tbl
);
562 * This function handles BA stream timeout event by preparing and sending
563 * a command to the firmware.
565 void mwifiex_11n_ba_stream_timeout(struct mwifiex_private
*priv
,
566 struct host_cmd_ds_11n_batimeout
*event
)
568 struct host_cmd_ds_11n_delba delba
;
570 memset(&delba
, 0, sizeof(struct host_cmd_ds_11n_delba
));
571 memcpy(delba
.peer_mac_addr
, event
->peer_mac_addr
, ETH_ALEN
);
573 delba
.del_ba_param_set
|=
574 cpu_to_le16((u16
) event
->tid
<< DELBA_TID_POS
);
575 delba
.del_ba_param_set
|= cpu_to_le16(
576 (u16
) event
->origninator
<< DELBA_INITIATOR_POS
);
577 delba
.reason_code
= cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT
);
578 mwifiex_send_cmd_async(priv
, HostCmd_CMD_11N_DELBA
, 0, 0, &delba
);
582 * This function cleans up the Rx reorder table by deleting all the entries
583 * and re-initializing.
585 void mwifiex_11n_cleanup_reorder_tbl(struct mwifiex_private
*priv
)
587 struct mwifiex_rx_reorder_tbl
*del_tbl_ptr
, *tmp_node
;
590 spin_lock_irqsave(&priv
->rx_reorder_tbl_lock
, flags
);
591 list_for_each_entry_safe(del_tbl_ptr
, tmp_node
,
592 &priv
->rx_reorder_tbl_ptr
, list
) {
593 spin_unlock_irqrestore(&priv
->rx_reorder_tbl_lock
, flags
);
594 mwifiex_del_rx_reorder_entry(priv
, del_tbl_ptr
);
595 spin_lock_irqsave(&priv
->rx_reorder_tbl_lock
, flags
);
597 spin_unlock_irqrestore(&priv
->rx_reorder_tbl_lock
, flags
);
599 INIT_LIST_HEAD(&priv
->rx_reorder_tbl_ptr
);
600 mwifiex_reset_11n_rx_seq_num(priv
);