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 if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
59 mwifiex_handle_uap_rx_forward(priv, rx_tmp_ptr);
61 mwifiex_process_rx_packet(priv, rx_tmp_ptr);
65 spin_lock_irqsave(&priv->rx_pkt_lock, flags);
67 * We don't have a circular buffer, hence use rotation to simulate
70 for (i = 0; i < tbl->win_size - pkt_to_send; ++i) {
71 tbl->rx_reorder_ptr[i] = tbl->rx_reorder_ptr[pkt_to_send + i];
72 tbl->rx_reorder_ptr[pkt_to_send + i] = NULL;
75 tbl->start_win = start_win;
76 spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
80 * This function dispatches all packets in the Rx reorder table until
83 * The start window is adjusted automatically when a hole is located.
84 * Since the buffer is linear, the function uses rotation to simulate
88 mwifiex_11n_scan_and_dispatch(struct mwifiex_private *priv,
89 struct mwifiex_rx_reorder_tbl *tbl)
95 for (i = 0; i < tbl->win_size; ++i) {
96 spin_lock_irqsave(&priv->rx_pkt_lock, flags);
97 if (!tbl->rx_reorder_ptr[i]) {
98 spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
101 rx_tmp_ptr = tbl->rx_reorder_ptr[i];
102 tbl->rx_reorder_ptr[i] = NULL;
103 spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
105 if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
106 mwifiex_handle_uap_rx_forward(priv, rx_tmp_ptr);
108 mwifiex_process_rx_packet(priv, rx_tmp_ptr);
111 spin_lock_irqsave(&priv->rx_pkt_lock, flags);
113 * We don't have a circular buffer, hence use rotation to simulate
117 xchg = tbl->win_size - i;
118 for (j = 0; j < xchg; ++j) {
119 tbl->rx_reorder_ptr[j] = tbl->rx_reorder_ptr[i + j];
120 tbl->rx_reorder_ptr[i + j] = NULL;
123 tbl->start_win = (tbl->start_win + i) & (MAX_TID_VALUE - 1);
124 spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
128 * This function deletes the Rx reorder table and frees the memory.
130 * The function stops the associated timer and dispatches all the
131 * pending packets in the Rx reorder table before deletion.
134 mwifiex_del_rx_reorder_entry(struct mwifiex_private *priv,
135 struct mwifiex_rx_reorder_tbl *tbl)
142 mwifiex_11n_dispatch_pkt(priv, tbl, (tbl->start_win + tbl->win_size) &
143 (MAX_TID_VALUE - 1));
145 del_timer(&tbl->timer_context.timer);
147 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
148 list_del(&tbl->list);
149 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
151 kfree(tbl->rx_reorder_ptr);
156 * This function returns the pointer to an entry in Rx reordering
157 * table which matches the given TA/TID pair.
159 struct mwifiex_rx_reorder_tbl *
160 mwifiex_11n_get_rx_reorder_tbl(struct mwifiex_private *priv, int tid, u8 *ta)
162 struct mwifiex_rx_reorder_tbl *tbl;
165 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
166 list_for_each_entry(tbl, &priv->rx_reorder_tbl_ptr, list) {
167 if (!memcmp(tbl->ta, ta, ETH_ALEN) && tbl->tid == tid) {
168 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock,
173 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
178 /* This function retrieves the pointer to an entry in Rx reordering
179 * table which matches the given TA and deletes it.
181 void mwifiex_11n_del_rx_reorder_tbl_by_ta(struct mwifiex_private *priv, u8 *ta)
183 struct mwifiex_rx_reorder_tbl *tbl, *tmp;
189 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
190 list_for_each_entry_safe(tbl, tmp, &priv->rx_reorder_tbl_ptr, list) {
191 if (!memcmp(tbl->ta, ta, ETH_ALEN)) {
192 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock,
194 mwifiex_del_rx_reorder_entry(priv, tbl);
195 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
198 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
204 * This function finds the last sequence number used in the packets
205 * buffered in Rx reordering table.
208 mwifiex_11n_find_last_seq_num(struct mwifiex_rx_reorder_tbl *rx_reorder_tbl_ptr)
212 for (i = (rx_reorder_tbl_ptr->win_size - 1); i >= 0; --i)
213 if (rx_reorder_tbl_ptr->rx_reorder_ptr[i])
220 * This function flushes all the packets in Rx reordering table.
222 * The function checks if any packets are currently buffered in the
223 * table or not. In case there are packets available, it dispatches
224 * them and then dumps the Rx reordering table.
227 mwifiex_flush_data(unsigned long context)
229 struct reorder_tmr_cnxt *ctx =
230 (struct reorder_tmr_cnxt *) context;
233 start_win = mwifiex_11n_find_last_seq_num(ctx->ptr);
238 dev_dbg(ctx->priv->adapter->dev, "info: flush data %d\n", start_win);
239 mwifiex_11n_dispatch_pkt(ctx->priv, ctx->ptr,
240 (ctx->ptr->start_win + start_win + 1) &
241 (MAX_TID_VALUE - 1));
245 * This function creates an entry in Rx reordering table for the
248 * The function also initializes the entry with sequence number, window
249 * size as well as initializes the timer.
251 * If the received TA/TID pair is already present, all the packets are
252 * dispatched and the window size is moved until the SSN.
255 mwifiex_11n_create_rx_reorder_tbl(struct mwifiex_private *priv, u8 *ta,
256 int tid, int win_size, int seq_num)
259 struct mwifiex_rx_reorder_tbl *tbl, *new_node;
262 struct mwifiex_sta_node *node;
265 * If we get a TID, ta pair which is already present dispatch all the
266 * the packets and move the window size until the ssn
268 tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid, ta);
270 mwifiex_11n_dispatch_pkt(priv, tbl, seq_num);
273 /* if !tbl then create one */
274 new_node = kzalloc(sizeof(struct mwifiex_rx_reorder_tbl), GFP_KERNEL);
276 dev_err(priv->adapter->dev, "%s: failed to alloc new_node\n",
281 INIT_LIST_HEAD(&new_node->list);
283 memcpy(new_node->ta, ta, ETH_ALEN);
284 new_node->start_win = seq_num;
286 if (mwifiex_queuing_ra_based(priv)) {
287 dev_dbg(priv->adapter->dev,
288 "info: AP/ADHOC:last_seq=%d start_win=%d\n",
289 last_seq, new_node->start_win);
290 if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP) {
291 node = mwifiex_get_sta_entry(priv, ta);
293 last_seq = node->rx_seq[tid];
296 last_seq = priv->rx_seq[tid];
299 if (last_seq != MWIFIEX_DEF_11N_RX_SEQ_NUM &&
300 last_seq >= new_node->start_win)
301 new_node->start_win = last_seq + 1;
303 new_node->win_size = win_size;
306 new_node->rx_reorder_ptr = kzalloc(sizeof(void *) * win_size,
308 if (!new_node->rx_reorder_ptr) {
309 kfree((u8 *) new_node);
310 dev_err(priv->adapter->dev,
311 "%s: failed to alloc reorder_ptr\n", __func__);
315 new_node->timer_context.ptr = new_node;
316 new_node->timer_context.priv = priv;
318 init_timer(&new_node->timer_context.timer);
319 new_node->timer_context.timer.function = mwifiex_flush_data;
320 new_node->timer_context.timer.data =
321 (unsigned long) &new_node->timer_context;
323 for (i = 0; i < win_size; ++i)
324 new_node->rx_reorder_ptr[i] = NULL;
326 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
327 list_add_tail(&new_node->list, &priv->rx_reorder_tbl_ptr);
328 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
332 * This function prepares command for adding a BA request.
334 * Preparation includes -
335 * - Setting command ID and proper size
336 * - Setting add BA request buffer
337 * - Ensuring correct endian-ness
339 int mwifiex_cmd_11n_addba_req(struct host_cmd_ds_command *cmd, void *data_buf)
341 struct host_cmd_ds_11n_addba_req *add_ba_req = &cmd->params.add_ba_req;
343 cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_REQ);
344 cmd->size = cpu_to_le16(sizeof(*add_ba_req) + S_DS_GEN);
345 memcpy(add_ba_req, data_buf, sizeof(*add_ba_req));
351 * This function prepares command for adding a BA response.
353 * Preparation includes -
354 * - Setting command ID and proper size
355 * - Setting add BA response buffer
356 * - Ensuring correct endian-ness
358 int mwifiex_cmd_11n_addba_rsp_gen(struct mwifiex_private *priv,
359 struct host_cmd_ds_command *cmd,
360 struct host_cmd_ds_11n_addba_req
363 struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &cmd->params.add_ba_rsp;
366 uint16_t block_ack_param_set;
368 cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_RSP);
369 cmd->size = cpu_to_le16(sizeof(*add_ba_rsp) + S_DS_GEN);
371 memcpy(add_ba_rsp->peer_mac_addr, cmd_addba_req->peer_mac_addr,
373 add_ba_rsp->dialog_token = cmd_addba_req->dialog_token;
374 add_ba_rsp->block_ack_tmo = cmd_addba_req->block_ack_tmo;
375 add_ba_rsp->ssn = cmd_addba_req->ssn;
377 block_ack_param_set = le16_to_cpu(cmd_addba_req->block_ack_param_set);
378 tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
379 >> BLOCKACKPARAM_TID_POS;
380 add_ba_rsp->status_code = cpu_to_le16(ADDBA_RSP_STATUS_ACCEPT);
381 block_ack_param_set &= ~IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK;
382 /* We donot support AMSDU inside AMPDU, hence reset the bit */
383 block_ack_param_set &= ~BLOCKACKPARAM_AMSDU_SUPP_MASK;
384 block_ack_param_set |= (priv->add_ba_param.rx_win_size <<
385 BLOCKACKPARAM_WINSIZE_POS);
386 add_ba_rsp->block_ack_param_set = cpu_to_le16(block_ack_param_set);
387 win_size = (le16_to_cpu(add_ba_rsp->block_ack_param_set)
388 & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
389 >> BLOCKACKPARAM_WINSIZE_POS;
390 cmd_addba_req->block_ack_param_set = cpu_to_le16(block_ack_param_set);
392 mwifiex_11n_create_rx_reorder_tbl(priv, cmd_addba_req->peer_mac_addr,
394 le16_to_cpu(cmd_addba_req->ssn));
399 * This function prepares command for deleting a BA request.
401 * Preparation includes -
402 * - Setting command ID and proper size
403 * - Setting del BA request buffer
404 * - Ensuring correct endian-ness
406 int mwifiex_cmd_11n_delba(struct host_cmd_ds_command *cmd, void *data_buf)
408 struct host_cmd_ds_11n_delba *del_ba = &cmd->params.del_ba;
410 cmd->command = cpu_to_le16(HostCmd_CMD_11N_DELBA);
411 cmd->size = cpu_to_le16(sizeof(*del_ba) + S_DS_GEN);
412 memcpy(del_ba, data_buf, sizeof(*del_ba));
418 * This function identifies if Rx reordering is needed for a received packet.
420 * In case reordering is required, the function will do the reordering
421 * before sending it to kernel.
423 * The Rx reorder table is checked first with the received TID/TA pair. If
424 * not found, the received packet is dispatched immediately. But if found,
425 * the packet is reordered and all the packets in the updated Rx reordering
426 * table is dispatched until a hole is found.
428 * For sequence number less than the starting window, the packet is dropped.
430 int mwifiex_11n_rx_reorder_pkt(struct mwifiex_private *priv,
431 u16 seq_num, u16 tid,
432 u8 *ta, u8 pkt_type, void *payload)
434 struct mwifiex_rx_reorder_tbl *tbl;
435 int start_win, end_win, win_size;
438 tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid, ta);
440 if (pkt_type != PKT_TYPE_BAR) {
441 if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
442 mwifiex_handle_uap_rx_forward(priv, payload);
444 mwifiex_process_rx_packet(priv, payload);
448 start_win = tbl->start_win;
449 win_size = tbl->win_size;
450 end_win = ((start_win + win_size) - 1) & (MAX_TID_VALUE - 1);
451 del_timer(&tbl->timer_context.timer);
452 mod_timer(&tbl->timer_context.timer,
453 jiffies + (MIN_FLUSH_TIMER_MS * win_size * HZ) / 1000);
456 * If seq_num is less then starting win then ignore and drop the
459 if (tbl->flags & RXREOR_FORCE_NO_DROP) {
460 dev_dbg(priv->adapter->dev,
461 "RXREOR_FORCE_NO_DROP when HS is activated\n");
462 tbl->flags &= ~RXREOR_FORCE_NO_DROP;
464 if ((start_win + TWOPOW11) > (MAX_TID_VALUE - 1)) {
465 if (seq_num >= ((start_win + TWOPOW11) &
466 (MAX_TID_VALUE - 1)) &&
469 } else if ((seq_num < start_win) ||
470 (seq_num > (start_win + TWOPOW11))) {
476 * If this packet is a BAR we adjust seq_num as
479 if (pkt_type == PKT_TYPE_BAR)
480 seq_num = ((seq_num + win_size) - 1) & (MAX_TID_VALUE - 1);
482 if (((end_win < start_win) &&
483 (seq_num < start_win) && (seq_num > end_win)) ||
484 ((end_win > start_win) && ((seq_num > end_win) ||
485 (seq_num < start_win)))) {
487 if (((seq_num - win_size) + 1) >= 0)
488 start_win = (end_win - win_size) + 1;
490 start_win = (MAX_TID_VALUE - (win_size - seq_num)) + 1;
491 mwifiex_11n_dispatch_pkt(priv, tbl, start_win);
494 if (pkt_type != PKT_TYPE_BAR) {
495 if (seq_num >= start_win)
496 pkt_index = seq_num - start_win;
498 pkt_index = (seq_num+MAX_TID_VALUE) - start_win;
500 if (tbl->rx_reorder_ptr[pkt_index])
503 tbl->rx_reorder_ptr[pkt_index] = payload;
507 * Dispatch all packets sequentially from start_win until a
508 * hole is found and adjust the start_win appropriately
510 mwifiex_11n_scan_and_dispatch(priv, tbl);
516 * This function deletes an entry for a given TID/TA pair.
518 * The TID/TA are taken from del BA event body.
521 mwifiex_del_ba_tbl(struct mwifiex_private *priv, int tid, u8 *peer_mac,
522 u8 type, int initiator)
524 struct mwifiex_rx_reorder_tbl *tbl;
525 struct mwifiex_tx_ba_stream_tbl *ptx_tbl;
526 u8 cleanup_rx_reorder_tbl;
529 if (type == TYPE_DELBA_RECEIVE)
530 cleanup_rx_reorder_tbl = (initiator) ? true : false;
532 cleanup_rx_reorder_tbl = (initiator) ? false : true;
534 dev_dbg(priv->adapter->dev, "event: DELBA: %pM tid=%d initiator=%d\n",
535 peer_mac, tid, initiator);
537 if (cleanup_rx_reorder_tbl) {
538 tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
541 dev_dbg(priv->adapter->dev,
542 "event: TID, TA not found in table\n");
545 mwifiex_del_rx_reorder_entry(priv, tbl);
547 ptx_tbl = mwifiex_get_ba_tbl(priv, tid, peer_mac);
549 dev_dbg(priv->adapter->dev,
550 "event: TID, RA not found in table\n");
554 spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
555 mwifiex_11n_delete_tx_ba_stream_tbl_entry(priv, ptx_tbl);
556 spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
561 * This function handles the command response of an add BA response.
563 * Handling includes changing the header fields into CPU format and
564 * creating the stream, provided the add BA is accepted.
566 int mwifiex_ret_11n_addba_resp(struct mwifiex_private *priv,
567 struct host_cmd_ds_command *resp)
569 struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &resp->params.add_ba_rsp;
571 struct mwifiex_rx_reorder_tbl *tbl;
572 uint16_t block_ack_param_set;
574 block_ack_param_set = le16_to_cpu(add_ba_rsp->block_ack_param_set);
576 tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
577 >> BLOCKACKPARAM_TID_POS;
579 * Check if we had rejected the ADDBA, if yes then do not create
582 if (le16_to_cpu(add_ba_rsp->status_code) == BA_RESULT_SUCCESS) {
583 win_size = (block_ack_param_set &
584 IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
585 >> BLOCKACKPARAM_WINSIZE_POS;
587 dev_dbg(priv->adapter->dev,
588 "cmd: ADDBA RSP: %pM tid=%d ssn=%d win_size=%d\n",
589 add_ba_rsp->peer_mac_addr, tid,
590 add_ba_rsp->ssn, win_size);
592 dev_err(priv->adapter->dev, "ADDBA RSP: failed %pM tid=%d)\n",
593 add_ba_rsp->peer_mac_addr, tid);
595 tbl = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
596 add_ba_rsp->peer_mac_addr);
598 mwifiex_del_rx_reorder_entry(priv, tbl);
605 * This function handles BA stream timeout event by preparing and sending
606 * a command to the firmware.
608 void mwifiex_11n_ba_stream_timeout(struct mwifiex_private *priv,
609 struct host_cmd_ds_11n_batimeout *event)
611 struct host_cmd_ds_11n_delba delba;
613 memset(&delba, 0, sizeof(struct host_cmd_ds_11n_delba));
614 memcpy(delba.peer_mac_addr, event->peer_mac_addr, ETH_ALEN);
616 delba.del_ba_param_set |=
617 cpu_to_le16((u16) event->tid << DELBA_TID_POS);
618 delba.del_ba_param_set |= cpu_to_le16(
619 (u16) event->origninator << DELBA_INITIATOR_POS);
620 delba.reason_code = cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
621 mwifiex_send_cmd_async(priv, HostCmd_CMD_11N_DELBA, 0, 0, &delba);
625 * This function cleans up the Rx reorder table by deleting all the entries
626 * and re-initializing.
628 void mwifiex_11n_cleanup_reorder_tbl(struct mwifiex_private *priv)
630 struct mwifiex_rx_reorder_tbl *del_tbl_ptr, *tmp_node;
633 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
634 list_for_each_entry_safe(del_tbl_ptr, tmp_node,
635 &priv->rx_reorder_tbl_ptr, list) {
636 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
637 mwifiex_del_rx_reorder_entry(priv, del_tbl_ptr);
638 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
640 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
642 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
643 mwifiex_reset_11n_rx_seq_num(priv);
647 * This function updates all rx_reorder_tbl's flags.
649 void mwifiex_update_rxreor_flags(struct mwifiex_adapter *adapter, u8 flags)
651 struct mwifiex_private *priv;
652 struct mwifiex_rx_reorder_tbl *tbl;
653 unsigned long lock_flags;
656 for (i = 0; i < adapter->priv_num; i++) {
657 priv = adapter->priv[i];
660 if (list_empty(&priv->rx_reorder_tbl_ptr))
663 spin_lock_irqsave(&priv->rx_reorder_tbl_lock, lock_flags);
664 list_for_each_entry(tbl, &priv->rx_reorder_tbl_ptr, list)
666 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, lock_flags);