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 processes a received packet and forwards
31 * it to the kernel/upper layer.
33 static int mwifiex_11n_dispatch_pkt(struct mwifiex_private *priv, void *payload)
36 struct mwifiex_adapter *adapter = priv->adapter;
38 ret = mwifiex_process_rx_packet(adapter, (struct sk_buff *) payload);
43 * This function dispatches all packets in the Rx reorder table.
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
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)
54 int no_pkt_to_send, i, xchg;
55 void *rx_tmp_ptr = NULL;
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;
62 for (i = 0; i < no_pkt_to_send; ++i) {
63 spin_lock_irqsave(&priv->rx_pkt_lock, flags);
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;
69 spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
71 mwifiex_11n_dispatch_pkt(priv, rx_tmp_ptr);
74 spin_lock_irqsave(&priv->rx_pkt_lock, flags);
76 * We don't have a circular buffer, hence use rotation to simulate
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;
86 rx_reor_tbl_ptr->start_win = start_win;
87 spin_unlock_irqrestore(&priv->rx_pkt_lock, flags);
93 * This function dispatches all packets in the Rx reorder table until
96 * The start window is adjusted automatically when a hole is located.
97 * Since the buffer is linear, the function uses rotation to simulate
101 mwifiex_11n_scan_and_dispatch(struct mwifiex_private *priv,
102 struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr)
105 void *rx_tmp_ptr = NULL;
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);
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);
120 spin_lock_irqsave(&priv->rx_pkt_lock, flags);
122 * We don't have a circular buffer, hence use rotation to simulate
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;
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);
140 * This function deletes the Rx reorder table and frees the memory.
142 * The function stops the associated timer and dispatches all the
143 * pending packets in the Rx reorder table before deletion.
146 mwifiex_11n_delete_rx_reorder_tbl_entry(struct mwifiex_private *priv,
147 struct mwifiex_rx_reorder_tbl
152 if (!rx_reor_tbl_ptr)
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));
160 del_timer(&rx_reor_tbl_ptr->timer_context.timer);
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);
166 kfree(rx_reor_tbl_ptr->rx_reorder_ptr);
167 kfree(rx_reor_tbl_ptr);
171 * This function returns the pointer to an entry in Rx reordering
172 * table which matches the given TA/TID pair.
174 static struct mwifiex_rx_reorder_tbl *
175 mwifiex_11n_get_rx_reorder_tbl(struct mwifiex_private *priv, int tid, u8 *ta)
177 struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr;
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,
186 return rx_reor_tbl_ptr;
189 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
195 * This function finds the last sequence number used in the packets
196 * buffered in Rx reordering table.
199 mwifiex_11n_find_last_seq_num(struct mwifiex_rx_reorder_tbl *rx_reorder_tbl_ptr)
203 for (i = (rx_reorder_tbl_ptr->win_size - 1); i >= 0; --i)
204 if (rx_reorder_tbl_ptr->rx_reorder_ptr[i])
211 * This function flushes all the packets in Rx reordering table.
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.
218 mwifiex_flush_data(unsigned long context)
220 struct reorder_tmr_cnxt *reorder_cnxt =
221 (struct reorder_tmr_cnxt *) context;
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,
230 ((reorder_cnxt->ptr->start_win +
231 start_win + 1) & (MAX_TID_VALUE - 1)));
236 * This function creates an entry in Rx reordering table for the
239 * The function also initializes the entry with sequence number, window
240 * size as well as initializes the timer.
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.
246 mwifiex_11n_create_rx_reorder_tbl(struct mwifiex_private *priv, u8 *ta,
247 int tid, int win_size, int seq_num)
250 struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr, *new_node;
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
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,
264 /* if !rx_reor_tbl_ptr then create one */
265 new_node = kzalloc(sizeof(struct mwifiex_rx_reorder_tbl), GFP_KERNEL);
267 dev_err(priv->adapter->dev, "%s: failed to alloc new_node\n",
272 INIT_LIST_HEAD(&new_node->list);
274 memcpy(new_node->ta, ta, ETH_ALEN);
275 new_node->start_win = seq_num;
276 if (mwifiex_queuing_ra_based(priv))
278 dev_dbg(priv->adapter->dev,
279 "info: ADHOC:last_seq=%d start_win=%d\n",
280 last_seq, new_node->start_win);
282 last_seq = priv->rx_seq[tid];
284 if (last_seq >= new_node->start_win)
285 new_node->start_win = last_seq + 1;
287 new_node->win_size = win_size;
289 new_node->rx_reorder_ptr = kzalloc(sizeof(void *) * win_size,
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__);
298 new_node->timer_context.ptr = new_node;
299 new_node->timer_context.priv = priv;
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;
306 for (i = 0; i < win_size; ++i)
307 new_node->rx_reorder_ptr[i] = NULL;
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);
317 * This function prepares command for adding a BA request.
319 * Preparation includes -
320 * - Setting command ID and proper size
321 * - Setting add BA request buffer
322 * - Ensuring correct endian-ness
324 int mwifiex_cmd_11n_addba_req(struct mwifiex_private *priv,
325 struct host_cmd_ds_command *cmd, void *data_buf)
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;
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));
339 * This function prepares command for adding a BA response.
341 * Preparation includes -
342 * - Setting command ID and proper size
343 * - Setting add BA response buffer
344 * - Ensuring correct endian-ness
346 int mwifiex_cmd_11n_addba_rsp_gen(struct mwifiex_private *priv,
347 struct host_cmd_ds_command *cmd,
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;
357 uint16_t block_ack_param_set;
359 cmd->command = cpu_to_le16(HostCmd_CMD_11N_ADDBA_RSP);
360 cmd->size = cpu_to_le16(sizeof(*add_ba_rsp) + S_DS_GEN);
362 memcpy(add_ba_rsp->peer_mac_addr, cmd_addba_req->peer_mac_addr,
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;
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);
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));
389 * This function prepares command for deleting a BA request.
391 * Preparation includes -
392 * - Setting command ID and proper size
393 * - Setting del BA request buffer
394 * - Ensuring correct endian-ness
396 int mwifiex_cmd_11n_delba(struct mwifiex_private *priv,
397 struct host_cmd_ds_command *cmd, void *data_buf)
399 struct host_cmd_ds_11n_delba *del_ba = (struct host_cmd_ds_11n_delba *)
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));
410 * This function identifies if Rx reordering is needed for a received packet.
412 * In case reordering is required, the function will do the reordering
413 * before sending it to kernel.
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.
420 * For sequence number less than the starting window, the packet is dropped.
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)
426 struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr;
427 int start_win, end_win, win_size;
432 mwifiex_11n_get_rx_reorder_tbl((struct mwifiex_private *) priv,
434 if (!rx_reor_tbl_ptr) {
435 if (pkt_type != PKT_TYPE_BAR)
436 mwifiex_11n_dispatch_pkt(priv, payload);
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);
447 * If seq_num is less then starting win then ignore and drop the
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))
454 } else if ((seq_num < start_win)
455 || (seq_num > (start_win + (TWOPOW11)))) {
460 * If this packet is a BAR we adjust seq_num as
463 if (pkt_type == PKT_TYPE_BAR)
464 seq_num = ((seq_num + win_size) - 1) & (MAX_TID_VALUE - 1);
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)))) {
471 if (((seq_num - win_size) + 1) >= 0)
472 start_win = (end_win - win_size) + 1;
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);
482 if (pkt_type != PKT_TYPE_BAR) {
483 if (seq_num >= start_win)
484 pkt_index = seq_num - start_win;
486 pkt_index = (seq_num+MAX_TID_VALUE) - start_win;
488 if (rx_reor_tbl_ptr->rx_reorder_ptr[pkt_index])
491 rx_reor_tbl_ptr->rx_reorder_ptr[pkt_index] = payload;
495 * Dispatch all packets sequentially from start_win until a
496 * hole is found and adjust the start_win appropriately
498 ret = mwifiex_11n_scan_and_dispatch(priv, rx_reor_tbl_ptr);
504 * This function deletes an entry for a given TID/TA pair.
506 * The TID/TA are taken from del BA event body.
509 mwifiex_11n_delete_ba_stream_tbl(struct mwifiex_private *priv, int tid,
510 u8 *peer_mac, u8 type, int initiator)
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;
517 if (type == TYPE_DELBA_RECEIVE)
518 cleanup_rx_reorder_tbl = (initiator) ? true : false;
520 cleanup_rx_reorder_tbl = (initiator) ? false : true;
522 dev_dbg(priv->adapter->dev, "event: DELBA: %pM tid=%d, "
523 "initiator=%d\n", peer_mac, tid, initiator);
525 if (cleanup_rx_reorder_tbl) {
526 rx_reor_tbl_ptr = mwifiex_11n_get_rx_reorder_tbl(priv, tid,
528 if (!rx_reor_tbl_ptr) {
529 dev_dbg(priv->adapter->dev,
530 "event: TID, TA not found in table\n");
533 mwifiex_11n_delete_rx_reorder_tbl_entry(priv, rx_reor_tbl_ptr);
535 ptx_tbl = mwifiex_11n_get_tx_ba_stream_tbl(priv, tid, peer_mac);
537 dev_dbg(priv->adapter->dev,
538 "event: TID, RA not found in table\n");
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);
549 * This function handles the command response of an add BA response.
551 * Handling includes changing the header fields into CPU format and
552 * creating the stream, provided the add BA is accepted.
554 int mwifiex_ret_11n_addba_resp(struct mwifiex_private *priv,
555 struct host_cmd_ds_command *resp)
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;
561 struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr = NULL;
562 uint16_t block_ack_param_set;
564 block_ack_param_set = le16_to_cpu(add_ba_rsp->block_ack_param_set);
566 tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
567 >> BLOCKACKPARAM_TID_POS;
569 * Check if we had rejected the ADDBA, if yes then do not create
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;
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);
582 dev_err(priv->adapter->dev, "ADDBA RSP: failed %pM tid=%d)\n",
583 add_ba_rsp->peer_mac_addr, tid);
585 rx_reor_tbl_ptr = mwifiex_11n_get_rx_reorder_tbl(priv,
586 tid, add_ba_rsp->peer_mac_addr);
588 mwifiex_11n_delete_rx_reorder_tbl_entry(priv,
596 * This function handles BA stream timeout event by preparing and sending
597 * a command to the firmware.
599 void mwifiex_11n_ba_stream_timeout(struct mwifiex_private *priv,
600 struct host_cmd_ds_11n_batimeout *event)
602 struct host_cmd_ds_11n_delba delba;
604 memset(&delba, 0, sizeof(struct host_cmd_ds_11n_delba));
605 memcpy(delba.peer_mac_addr, event->peer_mac_addr, ETH_ALEN);
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);
618 * This function cleans up the Rx reorder table by deleting all the entries
619 * and re-initializing.
621 void mwifiex_11n_cleanup_reorder_tbl(struct mwifiex_private *priv)
623 struct mwifiex_rx_reorder_tbl *del_tbl_ptr, *tmp_node;
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);
633 spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
635 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
636 memset(priv->rx_seq, 0, sizeof(priv->rx_seq));