cris: add arch/cris/include/asm/serial.h
[linux-2.6/next.git] / drivers / net / wireless / mwifiex / wmm.c
blob69e260b417116c0a815d0b8502a6e0e63666d56b
1 /*
2 * Marvell Wireless LAN device driver: WMM
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.
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"
29 /* Maximum value FW can accept for driver delay in packet transmission */
30 #define DRV_PKT_DELAY_TO_FW_MAX 512
33 #define WMM_QUEUED_PACKET_LOWER_LIMIT 180
35 #define WMM_QUEUED_PACKET_UPPER_LIMIT 200
37 /* Offset for TOS field in the IP header */
38 #define IPTOS_OFFSET 5
40 /* WMM information IE */
41 static const u8 wmm_info_ie[] = { WLAN_EID_VENDOR_SPECIFIC, 0x07,
42 0x00, 0x50, 0xf2, 0x02,
43 0x00, 0x01, 0x00
46 static const u8 wmm_aci_to_qidx_map[] = { WMM_AC_BE,
47 WMM_AC_BK,
48 WMM_AC_VI,
49 WMM_AC_VO
52 static u8 tos_to_tid[] = {
53 /* TID DSCP_P2 DSCP_P1 DSCP_P0 WMM_AC */
54 0x01, /* 0 1 0 AC_BK */
55 0x02, /* 0 0 0 AC_BK */
56 0x00, /* 0 0 1 AC_BE */
57 0x03, /* 0 1 1 AC_BE */
58 0x04, /* 1 0 0 AC_VI */
59 0x05, /* 1 0 1 AC_VI */
60 0x06, /* 1 1 0 AC_VO */
61 0x07 /* 1 1 1 AC_VO */
65 * This table inverses the tos_to_tid operation to get a priority
66 * which is in sequential order, and can be compared.
67 * Use this to compare the priority of two different TIDs.
69 static u8 tos_to_tid_inv[] = {
70 0x02, /* from tos_to_tid[2] = 0 */
71 0x00, /* from tos_to_tid[0] = 1 */
72 0x01, /* from tos_to_tid[1] = 2 */
73 0x03,
74 0x04,
75 0x05,
76 0x06,
77 0x07};
79 static u8 ac_to_tid[4][2] = { {1, 2}, {0, 3}, {4, 5}, {6, 7} };
82 * This function debug prints the priority parameters for a WMM AC.
84 static void
85 mwifiex_wmm_ac_debug_print(const struct ieee_types_wmm_ac_parameters *ac_param)
87 const char *ac_str[] = { "BK", "BE", "VI", "VO" };
89 pr_debug("info: WMM AC_%s: ACI=%d, ACM=%d, Aifsn=%d, "
90 "EcwMin=%d, EcwMax=%d, TxopLimit=%d\n",
91 ac_str[wmm_aci_to_qidx_map[(ac_param->aci_aifsn_bitmap
92 & MWIFIEX_ACI) >> 5]],
93 (ac_param->aci_aifsn_bitmap & MWIFIEX_ACI) >> 5,
94 (ac_param->aci_aifsn_bitmap & MWIFIEX_ACM) >> 4,
95 ac_param->aci_aifsn_bitmap & MWIFIEX_AIFSN,
96 ac_param->ecw_bitmap & MWIFIEX_ECW_MIN,
97 (ac_param->ecw_bitmap & MWIFIEX_ECW_MAX) >> 4,
98 le16_to_cpu(ac_param->tx_op_limit));
102 * This function allocates a route address list.
104 * The function also initializes the list with the provided RA.
106 static struct mwifiex_ra_list_tbl *
107 mwifiex_wmm_allocate_ralist_node(struct mwifiex_adapter *adapter, u8 *ra)
109 struct mwifiex_ra_list_tbl *ra_list;
111 ra_list = kzalloc(sizeof(struct mwifiex_ra_list_tbl), GFP_ATOMIC);
113 if (!ra_list) {
114 dev_err(adapter->dev, "%s: failed to alloc ra_list\n",
115 __func__);
116 return NULL;
118 INIT_LIST_HEAD(&ra_list->list);
119 skb_queue_head_init(&ra_list->skb_head);
121 memcpy(ra_list->ra, ra, ETH_ALEN);
123 ra_list->total_pkts_size = 0;
124 ra_list->total_pkts = 0;
126 dev_dbg(adapter->dev, "info: allocated ra_list %p\n", ra_list);
128 return ra_list;
132 * This function allocates and adds a RA list for all TIDs
133 * with the given RA.
135 void
136 mwifiex_ralist_add(struct mwifiex_private *priv, u8 *ra)
138 int i;
139 struct mwifiex_ra_list_tbl *ra_list;
140 struct mwifiex_adapter *adapter = priv->adapter;
142 for (i = 0; i < MAX_NUM_TID; ++i) {
143 ra_list = mwifiex_wmm_allocate_ralist_node(adapter, ra);
144 dev_dbg(adapter->dev, "info: created ra_list %p\n", ra_list);
146 if (!ra_list)
147 break;
149 if (!mwifiex_queuing_ra_based(priv))
150 ra_list->is_11n_enabled = IS_11N_ENABLED(priv);
151 else
152 ra_list->is_11n_enabled = false;
154 dev_dbg(adapter->dev, "data: ralist %p: is_11n_enabled=%d\n",
155 ra_list, ra_list->is_11n_enabled);
157 list_add_tail(&ra_list->list,
158 &priv->wmm.tid_tbl_ptr[i].ra_list);
160 if (!priv->wmm.tid_tbl_ptr[i].ra_list_curr)
161 priv->wmm.tid_tbl_ptr[i].ra_list_curr = ra_list;
166 * This function sets the WMM queue priorities to their default values.
168 static void mwifiex_wmm_default_queue_priorities(struct mwifiex_private *priv)
170 /* Default queue priorities: VO->VI->BE->BK */
171 priv->wmm.queue_priority[0] = WMM_AC_VO;
172 priv->wmm.queue_priority[1] = WMM_AC_VI;
173 priv->wmm.queue_priority[2] = WMM_AC_BE;
174 priv->wmm.queue_priority[3] = WMM_AC_BK;
178 * This function map ACs to TIDs.
180 static void
181 mwifiex_wmm_queue_priorities_tid(struct mwifiex_wmm_desc *wmm)
183 u8 *queue_priority = wmm->queue_priority;
184 int i;
186 for (i = 0; i < 4; ++i) {
187 tos_to_tid[7 - (i * 2)] = ac_to_tid[queue_priority[i]][1];
188 tos_to_tid[6 - (i * 2)] = ac_to_tid[queue_priority[i]][0];
191 for (i = 0; i < MAX_NUM_TID; ++i)
192 tos_to_tid_inv[tos_to_tid[i]] = (u8)i;
194 atomic_set(&wmm->highest_queued_prio, HIGH_PRIO_TID);
198 * This function initializes WMM priority queues.
200 void
201 mwifiex_wmm_setup_queue_priorities(struct mwifiex_private *priv,
202 struct ieee_types_wmm_parameter *wmm_ie)
204 u16 cw_min, avg_back_off, tmp[4];
205 u32 i, j, num_ac;
206 u8 ac_idx;
208 if (!wmm_ie || !priv->wmm_enabled) {
209 /* WMM is not enabled, just set the defaults and return */
210 mwifiex_wmm_default_queue_priorities(priv);
211 return;
214 dev_dbg(priv->adapter->dev, "info: WMM Parameter IE: version=%d, "
215 "qos_info Parameter Set Count=%d, Reserved=%#x\n",
216 wmm_ie->vend_hdr.version, wmm_ie->qos_info_bitmap &
217 IEEE80211_WMM_IE_AP_QOSINFO_PARAM_SET_CNT_MASK,
218 wmm_ie->reserved);
220 for (num_ac = 0; num_ac < ARRAY_SIZE(wmm_ie->ac_params); num_ac++) {
221 cw_min = (1 << (wmm_ie->ac_params[num_ac].ecw_bitmap &
222 MWIFIEX_ECW_MIN)) - 1;
223 avg_back_off = (cw_min >> 1) +
224 (wmm_ie->ac_params[num_ac].aci_aifsn_bitmap &
225 MWIFIEX_AIFSN);
227 ac_idx = wmm_aci_to_qidx_map[(wmm_ie->ac_params[num_ac].
228 aci_aifsn_bitmap &
229 MWIFIEX_ACI) >> 5];
230 priv->wmm.queue_priority[ac_idx] = ac_idx;
231 tmp[ac_idx] = avg_back_off;
233 dev_dbg(priv->adapter->dev, "info: WMM: CWmax=%d CWmin=%d Avg Back-off=%d\n",
234 (1 << ((wmm_ie->ac_params[num_ac].ecw_bitmap &
235 MWIFIEX_ECW_MAX) >> 4)) - 1,
236 cw_min, avg_back_off);
237 mwifiex_wmm_ac_debug_print(&wmm_ie->ac_params[num_ac]);
240 /* Bubble sort */
241 for (i = 0; i < num_ac; i++) {
242 for (j = 1; j < num_ac - i; j++) {
243 if (tmp[j - 1] > tmp[j]) {
244 swap(tmp[j - 1], tmp[j]);
245 swap(priv->wmm.queue_priority[j - 1],
246 priv->wmm.queue_priority[j]);
247 } else if (tmp[j - 1] == tmp[j]) {
248 if (priv->wmm.queue_priority[j - 1]
249 < priv->wmm.queue_priority[j])
250 swap(priv->wmm.queue_priority[j - 1],
251 priv->wmm.queue_priority[j]);
256 mwifiex_wmm_queue_priorities_tid(&priv->wmm);
260 * This function evaluates whether or not an AC is to be downgraded.
262 * In case the AC is not enabled, the highest AC is returned that is
263 * enabled and does not require admission control.
265 static enum mwifiex_wmm_ac_e
266 mwifiex_wmm_eval_downgrade_ac(struct mwifiex_private *priv,
267 enum mwifiex_wmm_ac_e eval_ac)
269 int down_ac;
270 enum mwifiex_wmm_ac_e ret_ac;
271 struct mwifiex_wmm_ac_status *ac_status;
273 ac_status = &priv->wmm.ac_status[eval_ac];
275 if (!ac_status->disabled)
276 /* Okay to use this AC, its enabled */
277 return eval_ac;
279 /* Setup a default return value of the lowest priority */
280 ret_ac = WMM_AC_BK;
283 * Find the highest AC that is enabled and does not require
284 * admission control. The spec disallows downgrading to an AC,
285 * which is enabled due to a completed admission control.
286 * Unadmitted traffic is not to be sent on an AC with admitted
287 * traffic.
289 for (down_ac = WMM_AC_BK; down_ac < eval_ac; down_ac++) {
290 ac_status = &priv->wmm.ac_status[down_ac];
292 if (!ac_status->disabled && !ac_status->flow_required)
293 /* AC is enabled and does not require admission
294 control */
295 ret_ac = (enum mwifiex_wmm_ac_e) down_ac;
298 return ret_ac;
302 * This function downgrades WMM priority queue.
304 void
305 mwifiex_wmm_setup_ac_downgrade(struct mwifiex_private *priv)
307 int ac_val;
309 dev_dbg(priv->adapter->dev, "info: WMM: AC Priorities:"
310 "BK(0), BE(1), VI(2), VO(3)\n");
312 if (!priv->wmm_enabled) {
313 /* WMM is not enabled, default priorities */
314 for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++)
315 priv->wmm.ac_down_graded_vals[ac_val] =
316 (enum mwifiex_wmm_ac_e) ac_val;
317 } else {
318 for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++) {
319 priv->wmm.ac_down_graded_vals[ac_val]
320 = mwifiex_wmm_eval_downgrade_ac(priv,
321 (enum mwifiex_wmm_ac_e) ac_val);
322 dev_dbg(priv->adapter->dev, "info: WMM: AC PRIO %d maps to %d\n",
323 ac_val, priv->wmm.ac_down_graded_vals[ac_val]);
329 * This function converts the IP TOS field to an WMM AC
330 * Queue assignment.
332 static enum mwifiex_wmm_ac_e
333 mwifiex_wmm_convert_tos_to_ac(struct mwifiex_adapter *adapter, u32 tos)
335 /* Map of TOS UP values to WMM AC */
336 const enum mwifiex_wmm_ac_e tos_to_ac[] = { WMM_AC_BE,
337 WMM_AC_BK,
338 WMM_AC_BK,
339 WMM_AC_BE,
340 WMM_AC_VI,
341 WMM_AC_VI,
342 WMM_AC_VO,
343 WMM_AC_VO
346 if (tos >= ARRAY_SIZE(tos_to_ac))
347 return WMM_AC_BE;
349 return tos_to_ac[tos];
353 * This function evaluates a given TID and downgrades it to a lower
354 * TID if the WMM Parameter IE received from the AP indicates that the
355 * AP is disabled (due to call admission control (ACM bit). Mapping
356 * of TID to AC is taken care of internally.
358 static u8
359 mwifiex_wmm_downgrade_tid(struct mwifiex_private *priv, u32 tid)
361 enum mwifiex_wmm_ac_e ac, ac_down;
362 u8 new_tid;
364 ac = mwifiex_wmm_convert_tos_to_ac(priv->adapter, tid);
365 ac_down = priv->wmm.ac_down_graded_vals[ac];
367 /* Send the index to tid array, picking from the array will be
368 * taken care by dequeuing function
370 new_tid = ac_to_tid[ac_down][tid % 2];
372 return new_tid;
376 * This function initializes the WMM state information and the
377 * WMM data path queues.
379 void
380 mwifiex_wmm_init(struct mwifiex_adapter *adapter)
382 int i, j;
383 struct mwifiex_private *priv;
385 for (j = 0; j < adapter->priv_num; ++j) {
386 priv = adapter->priv[j];
387 if (!priv)
388 continue;
390 for (i = 0; i < MAX_NUM_TID; ++i) {
391 priv->aggr_prio_tbl[i].amsdu = tos_to_tid_inv[i];
392 priv->aggr_prio_tbl[i].ampdu_ap = tos_to_tid_inv[i];
393 priv->aggr_prio_tbl[i].ampdu_user = tos_to_tid_inv[i];
394 priv->wmm.tid_tbl_ptr[i].ra_list_curr = NULL;
397 priv->aggr_prio_tbl[6].amsdu
398 = priv->aggr_prio_tbl[6].ampdu_ap
399 = priv->aggr_prio_tbl[6].ampdu_user
400 = BA_STREAM_NOT_ALLOWED;
402 priv->aggr_prio_tbl[7].amsdu = priv->aggr_prio_tbl[7].ampdu_ap
403 = priv->aggr_prio_tbl[7].ampdu_user
404 = BA_STREAM_NOT_ALLOWED;
406 priv->add_ba_param.timeout = MWIFIEX_DEFAULT_BLOCK_ACK_TIMEOUT;
407 priv->add_ba_param.tx_win_size = MWIFIEX_AMPDU_DEF_TXWINSIZE;
408 priv->add_ba_param.rx_win_size = MWIFIEX_AMPDU_DEF_RXWINSIZE;
410 atomic_set(&priv->wmm.tx_pkts_queued, 0);
411 atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
416 * This function checks if WMM Tx queue is empty.
419 mwifiex_wmm_lists_empty(struct mwifiex_adapter *adapter)
421 int i;
422 struct mwifiex_private *priv;
424 for (i = 0; i < adapter->priv_num; ++i) {
425 priv = adapter->priv[i];
426 if (priv && atomic_read(&priv->wmm.tx_pkts_queued))
427 return false;
430 return true;
434 * This function deletes all packets in an RA list node.
436 * The packet sent completion callback handler are called with
437 * status failure, after they are dequeued to ensure proper
438 * cleanup. The RA list node itself is freed at the end.
440 static void
441 mwifiex_wmm_del_pkts_in_ralist_node(struct mwifiex_private *priv,
442 struct mwifiex_ra_list_tbl *ra_list)
444 struct mwifiex_adapter *adapter = priv->adapter;
445 struct sk_buff *skb, *tmp;
447 skb_queue_walk_safe(&ra_list->skb_head, skb, tmp)
448 mwifiex_write_data_complete(adapter, skb, -1);
452 * This function deletes all packets in an RA list.
454 * Each nodes in the RA list are freed individually first, and then
455 * the RA list itself is freed.
457 static void
458 mwifiex_wmm_del_pkts_in_ralist(struct mwifiex_private *priv,
459 struct list_head *ra_list_head)
461 struct mwifiex_ra_list_tbl *ra_list;
463 list_for_each_entry(ra_list, ra_list_head, list)
464 mwifiex_wmm_del_pkts_in_ralist_node(priv, ra_list);
468 * This function deletes all packets in all RA lists.
470 static void mwifiex_wmm_cleanup_queues(struct mwifiex_private *priv)
472 int i;
474 for (i = 0; i < MAX_NUM_TID; i++)
475 mwifiex_wmm_del_pkts_in_ralist(priv, &priv->wmm.tid_tbl_ptr[i].
476 ra_list);
478 atomic_set(&priv->wmm.tx_pkts_queued, 0);
479 atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
483 * This function deletes all route addresses from all RA lists.
485 static void mwifiex_wmm_delete_all_ralist(struct mwifiex_private *priv)
487 struct mwifiex_ra_list_tbl *ra_list, *tmp_node;
488 int i;
490 for (i = 0; i < MAX_NUM_TID; ++i) {
491 dev_dbg(priv->adapter->dev,
492 "info: ra_list: freeing buf for tid %d\n", i);
493 list_for_each_entry_safe(ra_list, tmp_node,
494 &priv->wmm.tid_tbl_ptr[i].ra_list, list) {
495 list_del(&ra_list->list);
496 kfree(ra_list);
499 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[i].ra_list);
501 priv->wmm.tid_tbl_ptr[i].ra_list_curr = NULL;
506 * This function cleans up the Tx and Rx queues.
508 * Cleanup includes -
509 * - All packets in RA lists
510 * - All entries in Rx reorder table
511 * - All entries in Tx BA stream table
512 * - MPA buffer (if required)
513 * - All RA lists
515 void
516 mwifiex_clean_txrx(struct mwifiex_private *priv)
518 unsigned long flags;
520 mwifiex_11n_cleanup_reorder_tbl(priv);
521 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
523 mwifiex_wmm_cleanup_queues(priv);
524 mwifiex_11n_delete_all_tx_ba_stream_tbl(priv);
526 if (priv->adapter->if_ops.cleanup_mpa_buf)
527 priv->adapter->if_ops.cleanup_mpa_buf(priv->adapter);
529 mwifiex_wmm_delete_all_ralist(priv);
530 memcpy(tos_to_tid, ac_to_tid, sizeof(tos_to_tid));
532 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
536 * This function retrieves a particular RA list node, matching with the
537 * given TID and RA address.
539 static struct mwifiex_ra_list_tbl *
540 mwifiex_wmm_get_ralist_node(struct mwifiex_private *priv, u8 tid,
541 u8 *ra_addr)
543 struct mwifiex_ra_list_tbl *ra_list;
545 list_for_each_entry(ra_list, &priv->wmm.tid_tbl_ptr[tid].ra_list,
546 list) {
547 if (!memcmp(ra_list->ra, ra_addr, ETH_ALEN))
548 return ra_list;
551 return NULL;
555 * This function retrieves an RA list node for a given TID and
556 * RA address pair.
558 * If no such node is found, a new node is added first and then
559 * retrieved.
561 static struct mwifiex_ra_list_tbl *
562 mwifiex_wmm_get_queue_raptr(struct mwifiex_private *priv, u8 tid, u8 *ra_addr)
564 struct mwifiex_ra_list_tbl *ra_list;
566 ra_list = mwifiex_wmm_get_ralist_node(priv, tid, ra_addr);
567 if (ra_list)
568 return ra_list;
569 mwifiex_ralist_add(priv, ra_addr);
571 return mwifiex_wmm_get_ralist_node(priv, tid, ra_addr);
575 * This function checks if a particular RA list node exists in a given TID
576 * table index.
579 mwifiex_is_ralist_valid(struct mwifiex_private *priv,
580 struct mwifiex_ra_list_tbl *ra_list, int ptr_index)
582 struct mwifiex_ra_list_tbl *rlist;
584 list_for_each_entry(rlist, &priv->wmm.tid_tbl_ptr[ptr_index].ra_list,
585 list) {
586 if (rlist == ra_list)
587 return true;
590 return false;
594 * This function adds a packet to WMM queue.
596 * In disconnected state the packet is immediately dropped and the
597 * packet send completion callback is called with status failure.
599 * Otherwise, the correct RA list node is located and the packet
600 * is queued at the list tail.
602 void
603 mwifiex_wmm_add_buf_txqueue(struct mwifiex_adapter *adapter,
604 struct sk_buff *skb)
606 struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb);
607 struct mwifiex_private *priv = adapter->priv[tx_info->bss_index];
608 u32 tid;
609 struct mwifiex_ra_list_tbl *ra_list;
610 u8 ra[ETH_ALEN], tid_down;
611 unsigned long flags;
613 if (!priv->media_connected) {
614 dev_dbg(adapter->dev, "data: drop packet in disconnect\n");
615 mwifiex_write_data_complete(adapter, skb, -1);
616 return;
619 tid = skb->priority;
621 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
623 tid_down = mwifiex_wmm_downgrade_tid(priv, tid);
625 /* In case of infra as we have already created the list during
626 association we just don't have to call get_queue_raptr, we will
627 have only 1 raptr for a tid in case of infra */
628 if (!mwifiex_queuing_ra_based(priv)) {
629 if (!list_empty(&priv->wmm.tid_tbl_ptr[tid_down].ra_list))
630 ra_list = list_first_entry(
631 &priv->wmm.tid_tbl_ptr[tid_down].ra_list,
632 struct mwifiex_ra_list_tbl, list);
633 else
634 ra_list = NULL;
635 } else {
636 memcpy(ra, skb->data, ETH_ALEN);
637 if (ra[0] & 0x01)
638 memset(ra, 0xff, ETH_ALEN);
639 ra_list = mwifiex_wmm_get_queue_raptr(priv, tid_down, ra);
642 if (!ra_list) {
643 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
644 mwifiex_write_data_complete(adapter, skb, -1);
645 return;
648 skb_queue_tail(&ra_list->skb_head, skb);
650 ra_list->total_pkts_size += skb->len;
651 ra_list->total_pkts++;
653 atomic_inc(&priv->wmm.tx_pkts_queued);
655 if (atomic_read(&priv->wmm.highest_queued_prio) <
656 tos_to_tid_inv[tid_down])
657 atomic_set(&priv->wmm.highest_queued_prio,
658 tos_to_tid_inv[tid_down]);
660 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
664 * This function processes the get WMM status command response from firmware.
666 * The response may contain multiple TLVs -
667 * - AC Queue status TLVs
668 * - Current WMM Parameter IE TLV
669 * - Admission Control action frame TLVs
671 * This function parses the TLVs and then calls further specific functions
672 * to process any changes in the queue prioritize or state.
674 int mwifiex_ret_wmm_get_status(struct mwifiex_private *priv,
675 const struct host_cmd_ds_command *resp)
677 u8 *curr = (u8 *) &resp->params.get_wmm_status;
678 uint16_t resp_len = le16_to_cpu(resp->size), tlv_len;
679 int valid = true;
681 struct mwifiex_ie_types_data *tlv_hdr;
682 struct mwifiex_ie_types_wmm_queue_status *tlv_wmm_qstatus;
683 struct ieee_types_wmm_parameter *wmm_param_ie = NULL;
684 struct mwifiex_wmm_ac_status *ac_status;
686 dev_dbg(priv->adapter->dev, "info: WMM: WMM_GET_STATUS cmdresp received: %d\n",
687 resp_len);
689 while ((resp_len >= sizeof(tlv_hdr->header)) && valid) {
690 tlv_hdr = (struct mwifiex_ie_types_data *) curr;
691 tlv_len = le16_to_cpu(tlv_hdr->header.len);
693 switch (le16_to_cpu(tlv_hdr->header.type)) {
694 case TLV_TYPE_WMMQSTATUS:
695 tlv_wmm_qstatus =
696 (struct mwifiex_ie_types_wmm_queue_status *)
697 tlv_hdr;
698 dev_dbg(priv->adapter->dev,
699 "info: CMD_RESP: WMM_GET_STATUS:"
700 " QSTATUS TLV: %d, %d, %d\n",
701 tlv_wmm_qstatus->queue_index,
702 tlv_wmm_qstatus->flow_required,
703 tlv_wmm_qstatus->disabled);
705 ac_status = &priv->wmm.ac_status[tlv_wmm_qstatus->
706 queue_index];
707 ac_status->disabled = tlv_wmm_qstatus->disabled;
708 ac_status->flow_required =
709 tlv_wmm_qstatus->flow_required;
710 ac_status->flow_created = tlv_wmm_qstatus->flow_created;
711 break;
713 case WLAN_EID_VENDOR_SPECIFIC:
715 * Point the regular IEEE IE 2 bytes into the Marvell IE
716 * and setup the IEEE IE type and length byte fields
719 wmm_param_ie =
720 (struct ieee_types_wmm_parameter *) (curr +
722 wmm_param_ie->vend_hdr.len = (u8) tlv_len;
723 wmm_param_ie->vend_hdr.element_id =
724 WLAN_EID_VENDOR_SPECIFIC;
726 dev_dbg(priv->adapter->dev,
727 "info: CMD_RESP: WMM_GET_STATUS:"
728 " WMM Parameter Set Count: %d\n",
729 wmm_param_ie->qos_info_bitmap &
730 IEEE80211_WMM_IE_AP_QOSINFO_PARAM_SET_CNT_MASK);
732 memcpy((u8 *) &priv->curr_bss_params.bss_descriptor.
733 wmm_ie, wmm_param_ie,
734 wmm_param_ie->vend_hdr.len + 2);
736 break;
738 default:
739 valid = false;
740 break;
743 curr += (tlv_len + sizeof(tlv_hdr->header));
744 resp_len -= (tlv_len + sizeof(tlv_hdr->header));
747 mwifiex_wmm_setup_queue_priorities(priv, wmm_param_ie);
748 mwifiex_wmm_setup_ac_downgrade(priv);
750 return 0;
754 * Callback handler from the command module to allow insertion of a WMM TLV.
756 * If the BSS we are associating to supports WMM, this function adds the
757 * required WMM Information IE to the association request command buffer in
758 * the form of a Marvell extended IEEE IE.
761 mwifiex_wmm_process_association_req(struct mwifiex_private *priv,
762 u8 **assoc_buf,
763 struct ieee_types_wmm_parameter *wmm_ie,
764 struct ieee80211_ht_cap *ht_cap)
766 struct mwifiex_ie_types_wmm_param_set *wmm_tlv;
767 u32 ret_len = 0;
769 /* Null checks */
770 if (!assoc_buf)
771 return 0;
772 if (!(*assoc_buf))
773 return 0;
775 if (!wmm_ie)
776 return 0;
778 dev_dbg(priv->adapter->dev, "info: WMM: process assoc req:"
779 "bss->wmmIe=0x%x\n",
780 wmm_ie->vend_hdr.element_id);
782 if ((priv->wmm_required
783 || (ht_cap && (priv->adapter->config_bands & BAND_GN
784 || priv->adapter->config_bands & BAND_AN))
786 && wmm_ie->vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC) {
787 wmm_tlv = (struct mwifiex_ie_types_wmm_param_set *) *assoc_buf;
788 wmm_tlv->header.type = cpu_to_le16((u16) wmm_info_ie[0]);
789 wmm_tlv->header.len = cpu_to_le16((u16) wmm_info_ie[1]);
790 memcpy(wmm_tlv->wmm_ie, &wmm_info_ie[2],
791 le16_to_cpu(wmm_tlv->header.len));
792 if (wmm_ie->qos_info_bitmap & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD)
793 memcpy((u8 *) (wmm_tlv->wmm_ie
794 + le16_to_cpu(wmm_tlv->header.len)
795 - sizeof(priv->wmm_qosinfo)),
796 &priv->wmm_qosinfo,
797 sizeof(priv->wmm_qosinfo));
799 ret_len = sizeof(wmm_tlv->header)
800 + le16_to_cpu(wmm_tlv->header.len);
802 *assoc_buf += ret_len;
805 return ret_len;
809 * This function computes the time delay in the driver queues for a
810 * given packet.
812 * When the packet is received at the OS/Driver interface, the current
813 * time is set in the packet structure. The difference between the present
814 * time and that received time is computed in this function and limited
815 * based on pre-compiled limits in the driver.
818 mwifiex_wmm_compute_drv_pkt_delay(struct mwifiex_private *priv,
819 const struct sk_buff *skb)
821 u8 ret_val;
822 struct timeval out_tstamp, in_tstamp;
823 u32 queue_delay;
825 do_gettimeofday(&out_tstamp);
826 in_tstamp = ktime_to_timeval(skb->tstamp);
828 queue_delay = (out_tstamp.tv_sec - in_tstamp.tv_sec) * 1000;
829 queue_delay += (out_tstamp.tv_usec - in_tstamp.tv_usec) / 1000;
832 * Queue delay is passed as a uint8 in units of 2ms (ms shifted
833 * by 1). Min value (other than 0) is therefore 2ms, max is 510ms.
835 * Pass max value if queue_delay is beyond the uint8 range
837 ret_val = (u8) (min(queue_delay, priv->wmm.drv_pkt_delay_max) >> 1);
839 dev_dbg(priv->adapter->dev, "data: WMM: Pkt Delay: %d ms,"
840 " %d ms sent to FW\n", queue_delay, ret_val);
842 return ret_val;
846 * This function retrieves the highest priority RA list table pointer.
848 static struct mwifiex_ra_list_tbl *
849 mwifiex_wmm_get_highest_priolist_ptr(struct mwifiex_adapter *adapter,
850 struct mwifiex_private **priv, int *tid)
852 struct mwifiex_private *priv_tmp;
853 struct mwifiex_ra_list_tbl *ptr, *head;
854 struct mwifiex_bss_prio_node *bssprio_node, *bssprio_head;
855 struct mwifiex_tid_tbl *tid_ptr;
856 int is_list_empty;
857 unsigned long flags;
858 int i, j;
860 for (j = adapter->priv_num - 1; j >= 0; --j) {
861 spin_lock_irqsave(&adapter->bss_prio_tbl[j].bss_prio_lock,
862 flags);
863 is_list_empty = list_empty(&adapter->bss_prio_tbl[j]
864 .bss_prio_head);
865 spin_unlock_irqrestore(&adapter->bss_prio_tbl[j].bss_prio_lock,
866 flags);
867 if (is_list_empty)
868 continue;
870 if (adapter->bss_prio_tbl[j].bss_prio_cur ==
871 (struct mwifiex_bss_prio_node *)
872 &adapter->bss_prio_tbl[j].bss_prio_head) {
873 bssprio_node =
874 list_first_entry(&adapter->bss_prio_tbl[j]
875 .bss_prio_head,
876 struct mwifiex_bss_prio_node,
877 list);
878 bssprio_head = bssprio_node;
879 } else {
880 bssprio_node = adapter->bss_prio_tbl[j].bss_prio_cur;
881 bssprio_head = bssprio_node;
884 do {
885 atomic_t *hqp;
886 spinlock_t *lock;
888 priv_tmp = bssprio_node->priv;
889 hqp = &priv_tmp->wmm.highest_queued_prio;
890 lock = &priv_tmp->wmm.ra_list_spinlock;
892 for (i = atomic_read(hqp); i >= LOW_PRIO_TID; --i) {
894 tid_ptr = &(priv_tmp)->wmm.
895 tid_tbl_ptr[tos_to_tid[i]];
897 spin_lock_irqsave(&tid_ptr->tid_tbl_lock,
898 flags);
899 is_list_empty =
900 list_empty(&adapter->bss_prio_tbl[j]
901 .bss_prio_head);
902 spin_unlock_irqrestore(&tid_ptr->tid_tbl_lock,
903 flags);
904 if (is_list_empty)
905 continue;
908 * Always choose the next ra we transmitted
909 * last time, this way we pick the ra's in
910 * round robin fashion.
912 ptr = list_first_entry(
913 &tid_ptr->ra_list_curr->list,
914 struct mwifiex_ra_list_tbl,
915 list);
917 head = ptr;
918 if (ptr == (struct mwifiex_ra_list_tbl *)
919 &tid_ptr->ra_list) {
920 /* Get next ra */
921 ptr = list_first_entry(&ptr->list,
922 struct mwifiex_ra_list_tbl, list);
923 head = ptr;
926 do {
927 is_list_empty =
928 skb_queue_empty(&ptr->skb_head);
929 if (!is_list_empty) {
930 spin_lock_irqsave(lock, flags);
931 if (atomic_read(hqp) > i)
932 atomic_set(hqp, i);
933 spin_unlock_irqrestore(lock,
934 flags);
935 *priv = priv_tmp;
936 *tid = tos_to_tid[i];
937 return ptr;
939 /* Get next ra */
940 ptr = list_first_entry(&ptr->list,
941 struct mwifiex_ra_list_tbl,
942 list);
943 if (ptr ==
944 (struct mwifiex_ra_list_tbl *)
945 &tid_ptr->ra_list)
946 ptr = list_first_entry(
947 &ptr->list,
948 struct mwifiex_ra_list_tbl,
949 list);
950 } while (ptr != head);
953 /* No packet at any TID for this priv. Mark as such
954 * to skip checking TIDs for this priv (until pkt is
955 * added).
957 atomic_set(hqp, NO_PKT_PRIO_TID);
959 /* Get next bss priority node */
960 bssprio_node = list_first_entry(&bssprio_node->list,
961 struct mwifiex_bss_prio_node,
962 list);
964 if (bssprio_node ==
965 (struct mwifiex_bss_prio_node *)
966 &adapter->bss_prio_tbl[j].bss_prio_head)
967 /* Get next bss priority node */
968 bssprio_node = list_first_entry(
969 &bssprio_node->list,
970 struct mwifiex_bss_prio_node,
971 list);
972 } while (bssprio_node != bssprio_head);
974 return NULL;
978 * This function sends a single packet to firmware for transmission.
980 static void
981 mwifiex_send_single_packet(struct mwifiex_private *priv,
982 struct mwifiex_ra_list_tbl *ptr, int ptr_index,
983 unsigned long ra_list_flags)
984 __releases(&priv->wmm.ra_list_spinlock)
986 struct sk_buff *skb, *skb_next;
987 struct mwifiex_tx_param tx_param;
988 struct mwifiex_adapter *adapter = priv->adapter;
989 struct mwifiex_txinfo *tx_info;
991 if (skb_queue_empty(&ptr->skb_head)) {
992 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
993 ra_list_flags);
994 dev_dbg(adapter->dev, "data: nothing to send\n");
995 return;
998 skb = skb_dequeue(&ptr->skb_head);
1000 tx_info = MWIFIEX_SKB_TXCB(skb);
1001 dev_dbg(adapter->dev, "data: dequeuing the packet %p %p\n", ptr, skb);
1003 ptr->total_pkts_size -= skb->len;
1004 ptr->total_pkts--;
1006 if (!skb_queue_empty(&ptr->skb_head))
1007 skb_next = skb_peek(&ptr->skb_head);
1008 else
1009 skb_next = NULL;
1011 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, ra_list_flags);
1013 tx_param.next_pkt_len = ((skb_next) ? skb_next->len +
1014 sizeof(struct txpd) : 0);
1016 if (mwifiex_process_tx(priv, skb, &tx_param) == -EBUSY) {
1017 /* Queue the packet back at the head */
1018 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1020 if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1021 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1022 ra_list_flags);
1023 mwifiex_write_data_complete(adapter, skb, -1);
1024 return;
1027 skb_queue_tail(&ptr->skb_head, skb);
1029 ptr->total_pkts_size += skb->len;
1030 ptr->total_pkts++;
1031 tx_info->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT;
1032 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1033 ra_list_flags);
1034 } else {
1035 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1036 if (mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1037 priv->wmm.packets_out[ptr_index]++;
1038 priv->wmm.tid_tbl_ptr[ptr_index].ra_list_curr = ptr;
1040 adapter->bss_prio_tbl[priv->bss_priority].bss_prio_cur =
1041 list_first_entry(
1042 &adapter->bss_prio_tbl[priv->bss_priority]
1043 .bss_prio_cur->list,
1044 struct mwifiex_bss_prio_node,
1045 list);
1046 atomic_dec(&priv->wmm.tx_pkts_queued);
1047 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1048 ra_list_flags);
1053 * This function checks if the first packet in the given RA list
1054 * is already processed or not.
1056 static int
1057 mwifiex_is_ptr_processed(struct mwifiex_private *priv,
1058 struct mwifiex_ra_list_tbl *ptr)
1060 struct sk_buff *skb;
1061 struct mwifiex_txinfo *tx_info;
1063 if (skb_queue_empty(&ptr->skb_head))
1064 return false;
1066 skb = skb_peek(&ptr->skb_head);
1068 tx_info = MWIFIEX_SKB_TXCB(skb);
1069 if (tx_info->flags & MWIFIEX_BUF_FLAG_REQUEUED_PKT)
1070 return true;
1072 return false;
1076 * This function sends a single processed packet to firmware for
1077 * transmission.
1079 static void
1080 mwifiex_send_processed_packet(struct mwifiex_private *priv,
1081 struct mwifiex_ra_list_tbl *ptr, int ptr_index,
1082 unsigned long ra_list_flags)
1083 __releases(&priv->wmm.ra_list_spinlock)
1085 struct mwifiex_tx_param tx_param;
1086 struct mwifiex_adapter *adapter = priv->adapter;
1087 int ret = -1;
1088 struct sk_buff *skb, *skb_next;
1089 struct mwifiex_txinfo *tx_info;
1091 if (skb_queue_empty(&ptr->skb_head)) {
1092 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1093 ra_list_flags);
1094 return;
1097 skb = skb_dequeue(&ptr->skb_head);
1099 if (!skb_queue_empty(&ptr->skb_head))
1100 skb_next = skb_peek(&ptr->skb_head);
1101 else
1102 skb_next = NULL;
1104 tx_info = MWIFIEX_SKB_TXCB(skb);
1106 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, ra_list_flags);
1107 tx_param.next_pkt_len =
1108 ((skb_next) ? skb_next->len +
1109 sizeof(struct txpd) : 0);
1110 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_DATA,
1111 skb->data, skb->len, &tx_param);
1112 switch (ret) {
1113 case -EBUSY:
1114 dev_dbg(adapter->dev, "data: -EBUSY is returned\n");
1115 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1117 if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1118 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1119 ra_list_flags);
1120 mwifiex_write_data_complete(adapter, skb, -1);
1121 return;
1124 skb_queue_tail(&ptr->skb_head, skb);
1126 tx_info->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT;
1127 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1128 ra_list_flags);
1129 break;
1130 case -1:
1131 adapter->data_sent = false;
1132 dev_err(adapter->dev, "host_to_card failed: %#x\n", ret);
1133 adapter->dbg.num_tx_host_to_card_failure++;
1134 mwifiex_write_data_complete(adapter, skb, ret);
1135 break;
1136 case -EINPROGRESS:
1137 adapter->data_sent = false;
1138 default:
1139 break;
1141 if (ret != -EBUSY) {
1142 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1143 if (mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1144 priv->wmm.packets_out[ptr_index]++;
1145 priv->wmm.tid_tbl_ptr[ptr_index].ra_list_curr = ptr;
1147 adapter->bss_prio_tbl[priv->bss_priority].bss_prio_cur =
1148 list_first_entry(
1149 &adapter->bss_prio_tbl[priv->bss_priority]
1150 .bss_prio_cur->list,
1151 struct mwifiex_bss_prio_node,
1152 list);
1153 atomic_dec(&priv->wmm.tx_pkts_queued);
1154 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1155 ra_list_flags);
1160 * This function dequeues a packet from the highest priority list
1161 * and transmits it.
1163 static int
1164 mwifiex_dequeue_tx_packet(struct mwifiex_adapter *adapter)
1166 struct mwifiex_ra_list_tbl *ptr;
1167 struct mwifiex_private *priv = NULL;
1168 int ptr_index = 0;
1169 u8 ra[ETH_ALEN];
1170 int tid_del = 0, tid = 0;
1171 unsigned long flags;
1173 ptr = mwifiex_wmm_get_highest_priolist_ptr(adapter, &priv, &ptr_index);
1174 if (!ptr)
1175 return -1;
1177 tid = mwifiex_get_tid(ptr);
1179 dev_dbg(adapter->dev, "data: tid=%d\n", tid);
1181 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
1182 if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1183 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
1184 return -1;
1187 if (mwifiex_is_ptr_processed(priv, ptr)) {
1188 mwifiex_send_processed_packet(priv, ptr, ptr_index, flags);
1189 /* ra_list_spinlock has been freed in
1190 mwifiex_send_processed_packet() */
1191 return 0;
1194 if (!ptr->is_11n_enabled || mwifiex_is_ba_stream_setup(priv, ptr, tid)
1195 || ((priv->sec_info.wpa_enabled
1196 || priv->sec_info.wpa2_enabled) && !priv->wpa_is_gtk_set)
1198 mwifiex_send_single_packet(priv, ptr, ptr_index, flags);
1199 /* ra_list_spinlock has been freed in
1200 mwifiex_send_single_packet() */
1201 } else {
1202 if (mwifiex_is_ampdu_allowed(priv, tid)) {
1203 if (mwifiex_space_avail_for_new_ba_stream(adapter)) {
1204 mwifiex_11n_create_tx_ba_stream_tbl(priv,
1205 ptr->ra, tid,
1206 BA_STREAM_SETUP_INPROGRESS);
1207 mwifiex_send_addba(priv, tid, ptr->ra);
1208 } else if (mwifiex_find_stream_to_delete
1209 (priv, tid, &tid_del, ra)) {
1210 mwifiex_11n_create_tx_ba_stream_tbl(priv,
1211 ptr->ra, tid,
1212 BA_STREAM_SETUP_INPROGRESS);
1213 mwifiex_send_delba(priv, tid_del, ra, 1);
1216 /* Minimum number of AMSDU */
1217 #define MIN_NUM_AMSDU 2
1219 if (mwifiex_is_amsdu_allowed(priv, tid) &&
1220 (ptr->total_pkts >= MIN_NUM_AMSDU))
1221 mwifiex_11n_aggregate_pkt(priv, ptr, INTF_HEADER_LEN,
1222 ptr_index, flags);
1223 /* ra_list_spinlock has been freed in
1224 mwifiex_11n_aggregate_pkt() */
1225 else
1226 mwifiex_send_single_packet(priv, ptr, ptr_index, flags);
1227 /* ra_list_spinlock has been freed in
1228 mwifiex_send_single_packet() */
1230 return 0;
1234 * This function transmits the highest priority packet awaiting in the
1235 * WMM Queues.
1237 void
1238 mwifiex_wmm_process_tx(struct mwifiex_adapter *adapter)
1240 do {
1241 /* Check if busy */
1242 if (adapter->data_sent || adapter->tx_lock_flag)
1243 break;
1245 if (mwifiex_dequeue_tx_packet(adapter))
1246 break;
1247 } while (!mwifiex_wmm_lists_empty(adapter));