mwifiex: complete usb tx data with multi endpoints
[cascardo/linux.git] / drivers / net / wireless / mwifiex / wmm.c
1 /*
2  * Marvell Wireless LAN device driver: WMM
3  *
4  * Copyright (C) 2011-2014, 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
28
29 /* Maximum value FW can accept for driver delay in packet transmission */
30 #define DRV_PKT_DELAY_TO_FW_MAX   512
31
32
33 #define WMM_QUEUED_PACKET_LOWER_LIMIT   180
34
35 #define WMM_QUEUED_PACKET_UPPER_LIMIT   200
36
37 /* Offset for TOS field in the IP header */
38 #define IPTOS_OFFSET 5
39
40 static bool disable_tx_amsdu;
41 module_param(disable_tx_amsdu, bool, 0644);
42
43 /* WMM information IE */
44 static const u8 wmm_info_ie[] = { WLAN_EID_VENDOR_SPECIFIC, 0x07,
45         0x00, 0x50, 0xf2, 0x02,
46         0x00, 0x01, 0x00
47 };
48
49 static const u8 wmm_aci_to_qidx_map[] = { WMM_AC_BE,
50         WMM_AC_BK,
51         WMM_AC_VI,
52         WMM_AC_VO
53 };
54
55 static u8 tos_to_tid[] = {
56         /* TID DSCP_P2 DSCP_P1 DSCP_P0 WMM_AC */
57         0x01,                   /* 0 1 0 AC_BK */
58         0x02,                   /* 0 0 0 AC_BK */
59         0x00,                   /* 0 0 1 AC_BE */
60         0x03,                   /* 0 1 1 AC_BE */
61         0x04,                   /* 1 0 0 AC_VI */
62         0x05,                   /* 1 0 1 AC_VI */
63         0x06,                   /* 1 1 0 AC_VO */
64         0x07                    /* 1 1 1 AC_VO */
65 };
66
67 static u8 ac_to_tid[4][2] = { {1, 2}, {0, 3}, {4, 5}, {6, 7} };
68
69 /*
70  * This function debug prints the priority parameters for a WMM AC.
71  */
72 static void
73 mwifiex_wmm_ac_debug_print(const struct ieee_types_wmm_ac_parameters *ac_param)
74 {
75         const char *ac_str[] = { "BK", "BE", "VI", "VO" };
76
77         pr_debug("info: WMM AC_%s: ACI=%d, ACM=%d, Aifsn=%d, "
78                  "EcwMin=%d, EcwMax=%d, TxopLimit=%d\n",
79                  ac_str[wmm_aci_to_qidx_map[(ac_param->aci_aifsn_bitmap
80                                              & MWIFIEX_ACI) >> 5]],
81                  (ac_param->aci_aifsn_bitmap & MWIFIEX_ACI) >> 5,
82                  (ac_param->aci_aifsn_bitmap & MWIFIEX_ACM) >> 4,
83                  ac_param->aci_aifsn_bitmap & MWIFIEX_AIFSN,
84                  ac_param->ecw_bitmap & MWIFIEX_ECW_MIN,
85                  (ac_param->ecw_bitmap & MWIFIEX_ECW_MAX) >> 4,
86                  le16_to_cpu(ac_param->tx_op_limit));
87 }
88
89 /*
90  * This function allocates a route address list.
91  *
92  * The function also initializes the list with the provided RA.
93  */
94 static struct mwifiex_ra_list_tbl *
95 mwifiex_wmm_allocate_ralist_node(struct mwifiex_adapter *adapter, const u8 *ra)
96 {
97         struct mwifiex_ra_list_tbl *ra_list;
98
99         ra_list = kzalloc(sizeof(struct mwifiex_ra_list_tbl), GFP_ATOMIC);
100         if (!ra_list)
101                 return NULL;
102
103         INIT_LIST_HEAD(&ra_list->list);
104         skb_queue_head_init(&ra_list->skb_head);
105
106         memcpy(ra_list->ra, ra, ETH_ALEN);
107
108         ra_list->total_pkt_count = 0;
109
110         mwifiex_dbg(adapter, INFO, "info: allocated ra_list %p\n", ra_list);
111
112         return ra_list;
113 }
114
115 /* This function returns random no between 16 and 32 to be used as threshold
116  * for no of packets after which BA setup is initiated.
117  */
118 static u8 mwifiex_get_random_ba_threshold(void)
119 {
120         u32 sec, usec;
121         struct timeval ba_tstamp;
122         u8 ba_threshold;
123
124         /* setup ba_packet_threshold here random number between
125          * [BA_SETUP_PACKET_OFFSET,
126          * BA_SETUP_PACKET_OFFSET+BA_SETUP_MAX_PACKET_THRESHOLD-1]
127          */
128
129         do_gettimeofday(&ba_tstamp);
130         sec = (ba_tstamp.tv_sec & 0xFFFF) + (ba_tstamp.tv_sec >> 16);
131         usec = (ba_tstamp.tv_usec & 0xFFFF) + (ba_tstamp.tv_usec >> 16);
132         ba_threshold = (((sec << 16) + usec) % BA_SETUP_MAX_PACKET_THRESHOLD)
133                                                       + BA_SETUP_PACKET_OFFSET;
134
135         return ba_threshold;
136 }
137
138 /*
139  * This function allocates and adds a RA list for all TIDs
140  * with the given RA.
141  */
142 void mwifiex_ralist_add(struct mwifiex_private *priv, const u8 *ra)
143 {
144         int i;
145         struct mwifiex_ra_list_tbl *ra_list;
146         struct mwifiex_adapter *adapter = priv->adapter;
147         struct mwifiex_sta_node *node;
148         unsigned long flags;
149
150
151         for (i = 0; i < MAX_NUM_TID; ++i) {
152                 ra_list = mwifiex_wmm_allocate_ralist_node(adapter, ra);
153                 mwifiex_dbg(adapter, INFO,
154                             "info: created ra_list %p\n", ra_list);
155
156                 if (!ra_list)
157                         break;
158
159                 ra_list->is_11n_enabled = 0;
160                 ra_list->tdls_link = false;
161                 ra_list->ba_status = BA_SETUP_NONE;
162                 ra_list->amsdu_in_ampdu = false;
163                 if (!mwifiex_queuing_ra_based(priv)) {
164                         if (mwifiex_is_tdls_link_setup
165                                 (mwifiex_get_tdls_link_status(priv, ra))) {
166                                 ra_list->tdls_link = true;
167                                 ra_list->is_11n_enabled =
168                                         mwifiex_tdls_peer_11n_enabled(priv, ra);
169                         } else {
170                                 ra_list->is_11n_enabled = IS_11N_ENABLED(priv);
171                         }
172                 } else {
173                         spin_lock_irqsave(&priv->sta_list_spinlock, flags);
174                         node = mwifiex_get_sta_entry(priv, ra);
175                         if (node)
176                                 ra_list->tx_paused = node->tx_pause;
177                         ra_list->is_11n_enabled =
178                                       mwifiex_is_sta_11n_enabled(priv, node);
179                         if (ra_list->is_11n_enabled)
180                                 ra_list->max_amsdu = node->max_amsdu;
181                         spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
182                 }
183
184                 mwifiex_dbg(adapter, DATA, "data: ralist %p: is_11n_enabled=%d\n",
185                             ra_list, ra_list->is_11n_enabled);
186
187                 if (ra_list->is_11n_enabled) {
188                         ra_list->ba_pkt_count = 0;
189                         ra_list->ba_packet_thr =
190                                               mwifiex_get_random_ba_threshold();
191                 }
192                 list_add_tail(&ra_list->list,
193                               &priv->wmm.tid_tbl_ptr[i].ra_list);
194         }
195 }
196
197 /*
198  * This function sets the WMM queue priorities to their default values.
199  */
200 static void mwifiex_wmm_default_queue_priorities(struct mwifiex_private *priv)
201 {
202         /* Default queue priorities: VO->VI->BE->BK */
203         priv->wmm.queue_priority[0] = WMM_AC_VO;
204         priv->wmm.queue_priority[1] = WMM_AC_VI;
205         priv->wmm.queue_priority[2] = WMM_AC_BE;
206         priv->wmm.queue_priority[3] = WMM_AC_BK;
207 }
208
209 /*
210  * This function map ACs to TIDs.
211  */
212 static void
213 mwifiex_wmm_queue_priorities_tid(struct mwifiex_private *priv)
214 {
215         struct mwifiex_wmm_desc *wmm = &priv->wmm;
216         u8 *queue_priority = wmm->queue_priority;
217         int i;
218
219         for (i = 0; i < 4; ++i) {
220                 tos_to_tid[7 - (i * 2)] = ac_to_tid[queue_priority[i]][1];
221                 tos_to_tid[6 - (i * 2)] = ac_to_tid[queue_priority[i]][0];
222         }
223
224         for (i = 0; i < MAX_NUM_TID; ++i)
225                 priv->tos_to_tid_inv[tos_to_tid[i]] = (u8)i;
226
227         atomic_set(&wmm->highest_queued_prio, HIGH_PRIO_TID);
228 }
229
230 /*
231  * This function initializes WMM priority queues.
232  */
233 void
234 mwifiex_wmm_setup_queue_priorities(struct mwifiex_private *priv,
235                                    struct ieee_types_wmm_parameter *wmm_ie)
236 {
237         u16 cw_min, avg_back_off, tmp[4];
238         u32 i, j, num_ac;
239         u8 ac_idx;
240
241         if (!wmm_ie || !priv->wmm_enabled) {
242                 /* WMM is not enabled, just set the defaults and return */
243                 mwifiex_wmm_default_queue_priorities(priv);
244                 return;
245         }
246
247         mwifiex_dbg(priv->adapter, INFO,
248                     "info: WMM Parameter IE: version=%d,\t"
249                     "qos_info Parameter Set Count=%d, Reserved=%#x\n",
250                     wmm_ie->vend_hdr.version, wmm_ie->qos_info_bitmap &
251                     IEEE80211_WMM_IE_AP_QOSINFO_PARAM_SET_CNT_MASK,
252                     wmm_ie->reserved);
253
254         for (num_ac = 0; num_ac < ARRAY_SIZE(wmm_ie->ac_params); num_ac++) {
255                 u8 ecw = wmm_ie->ac_params[num_ac].ecw_bitmap;
256                 u8 aci_aifsn = wmm_ie->ac_params[num_ac].aci_aifsn_bitmap;
257                 cw_min = (1 << (ecw & MWIFIEX_ECW_MIN)) - 1;
258                 avg_back_off = (cw_min >> 1) + (aci_aifsn & MWIFIEX_AIFSN);
259
260                 ac_idx = wmm_aci_to_qidx_map[(aci_aifsn & MWIFIEX_ACI) >> 5];
261                 priv->wmm.queue_priority[ac_idx] = ac_idx;
262                 tmp[ac_idx] = avg_back_off;
263
264                 mwifiex_dbg(priv->adapter, INFO,
265                             "info: WMM: CWmax=%d CWmin=%d Avg Back-off=%d\n",
266                             (1 << ((ecw & MWIFIEX_ECW_MAX) >> 4)) - 1,
267                             cw_min, avg_back_off);
268                 mwifiex_wmm_ac_debug_print(&wmm_ie->ac_params[num_ac]);
269         }
270
271         /* Bubble sort */
272         for (i = 0; i < num_ac; i++) {
273                 for (j = 1; j < num_ac - i; j++) {
274                         if (tmp[j - 1] > tmp[j]) {
275                                 swap(tmp[j - 1], tmp[j]);
276                                 swap(priv->wmm.queue_priority[j - 1],
277                                      priv->wmm.queue_priority[j]);
278                         } else if (tmp[j - 1] == tmp[j]) {
279                                 if (priv->wmm.queue_priority[j - 1]
280                                     < priv->wmm.queue_priority[j])
281                                         swap(priv->wmm.queue_priority[j - 1],
282                                              priv->wmm.queue_priority[j]);
283                         }
284                 }
285         }
286
287         mwifiex_wmm_queue_priorities_tid(priv);
288 }
289
290 /*
291  * This function evaluates whether or not an AC is to be downgraded.
292  *
293  * In case the AC is not enabled, the highest AC is returned that is
294  * enabled and does not require admission control.
295  */
296 static enum mwifiex_wmm_ac_e
297 mwifiex_wmm_eval_downgrade_ac(struct mwifiex_private *priv,
298                               enum mwifiex_wmm_ac_e eval_ac)
299 {
300         int down_ac;
301         enum mwifiex_wmm_ac_e ret_ac;
302         struct mwifiex_wmm_ac_status *ac_status;
303
304         ac_status = &priv->wmm.ac_status[eval_ac];
305
306         if (!ac_status->disabled)
307                 /* Okay to use this AC, its enabled */
308                 return eval_ac;
309
310         /* Setup a default return value of the lowest priority */
311         ret_ac = WMM_AC_BK;
312
313         /*
314          *  Find the highest AC that is enabled and does not require
315          *  admission control. The spec disallows downgrading to an AC,
316          *  which is enabled due to a completed admission control.
317          *  Unadmitted traffic is not to be sent on an AC with admitted
318          *  traffic.
319          */
320         for (down_ac = WMM_AC_BK; down_ac < eval_ac; down_ac++) {
321                 ac_status = &priv->wmm.ac_status[down_ac];
322
323                 if (!ac_status->disabled && !ac_status->flow_required)
324                         /* AC is enabled and does not require admission
325                            control */
326                         ret_ac = (enum mwifiex_wmm_ac_e) down_ac;
327         }
328
329         return ret_ac;
330 }
331
332 /*
333  * This function downgrades WMM priority queue.
334  */
335 void
336 mwifiex_wmm_setup_ac_downgrade(struct mwifiex_private *priv)
337 {
338         int ac_val;
339
340         mwifiex_dbg(priv->adapter, INFO, "info: WMM: AC Priorities:\t"
341                     "BK(0), BE(1), VI(2), VO(3)\n");
342
343         if (!priv->wmm_enabled) {
344                 /* WMM is not enabled, default priorities */
345                 for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++)
346                         priv->wmm.ac_down_graded_vals[ac_val] =
347                                                 (enum mwifiex_wmm_ac_e) ac_val;
348         } else {
349                 for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++) {
350                         priv->wmm.ac_down_graded_vals[ac_val]
351                                 = mwifiex_wmm_eval_downgrade_ac(priv,
352                                                 (enum mwifiex_wmm_ac_e) ac_val);
353                         mwifiex_dbg(priv->adapter, INFO,
354                                     "info: WMM: AC PRIO %d maps to %d\n",
355                                     ac_val,
356                                     priv->wmm.ac_down_graded_vals[ac_val]);
357                 }
358         }
359 }
360
361 /*
362  * This function converts the IP TOS field to an WMM AC
363  * Queue assignment.
364  */
365 static enum mwifiex_wmm_ac_e
366 mwifiex_wmm_convert_tos_to_ac(struct mwifiex_adapter *adapter, u32 tos)
367 {
368         /* Map of TOS UP values to WMM AC */
369         const enum mwifiex_wmm_ac_e tos_to_ac[] = { WMM_AC_BE,
370                 WMM_AC_BK,
371                 WMM_AC_BK,
372                 WMM_AC_BE,
373                 WMM_AC_VI,
374                 WMM_AC_VI,
375                 WMM_AC_VO,
376                 WMM_AC_VO
377         };
378
379         if (tos >= ARRAY_SIZE(tos_to_ac))
380                 return WMM_AC_BE;
381
382         return tos_to_ac[tos];
383 }
384
385 /*
386  * This function evaluates a given TID and downgrades it to a lower
387  * TID if the WMM Parameter IE received from the AP indicates that the
388  * AP is disabled (due to call admission control (ACM bit). Mapping
389  * of TID to AC is taken care of internally.
390  */
391 u8 mwifiex_wmm_downgrade_tid(struct mwifiex_private *priv, u32 tid)
392 {
393         enum mwifiex_wmm_ac_e ac, ac_down;
394         u8 new_tid;
395
396         ac = mwifiex_wmm_convert_tos_to_ac(priv->adapter, tid);
397         ac_down = priv->wmm.ac_down_graded_vals[ac];
398
399         /* Send the index to tid array, picking from the array will be
400          * taken care by dequeuing function
401          */
402         new_tid = ac_to_tid[ac_down][tid % 2];
403
404         return new_tid;
405 }
406
407 /*
408  * This function initializes the WMM state information and the
409  * WMM data path queues.
410  */
411 void
412 mwifiex_wmm_init(struct mwifiex_adapter *adapter)
413 {
414         int i, j;
415         struct mwifiex_private *priv;
416
417         for (j = 0; j < adapter->priv_num; ++j) {
418                 priv = adapter->priv[j];
419                 if (!priv)
420                         continue;
421
422                 for (i = 0; i < MAX_NUM_TID; ++i) {
423                         if (!disable_tx_amsdu &&
424                             adapter->tx_buf_size > MWIFIEX_TX_DATA_BUF_SIZE_2K)
425                                 priv->aggr_prio_tbl[i].amsdu =
426                                                         priv->tos_to_tid_inv[i];
427                         else
428                                 priv->aggr_prio_tbl[i].amsdu =
429                                                         BA_STREAM_NOT_ALLOWED;
430                         priv->aggr_prio_tbl[i].ampdu_ap =
431                                                         priv->tos_to_tid_inv[i];
432                         priv->aggr_prio_tbl[i].ampdu_user =
433                                                         priv->tos_to_tid_inv[i];
434                 }
435
436                 priv->aggr_prio_tbl[6].amsdu
437                                         = priv->aggr_prio_tbl[6].ampdu_ap
438                                         = priv->aggr_prio_tbl[6].ampdu_user
439                                         = BA_STREAM_NOT_ALLOWED;
440
441                 priv->aggr_prio_tbl[7].amsdu = priv->aggr_prio_tbl[7].ampdu_ap
442                                         = priv->aggr_prio_tbl[7].ampdu_user
443                                         = BA_STREAM_NOT_ALLOWED;
444
445                 mwifiex_set_ba_params(priv);
446                 mwifiex_reset_11n_rx_seq_num(priv);
447
448                 atomic_set(&priv->wmm.tx_pkts_queued, 0);
449                 atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
450         }
451 }
452
453 int mwifiex_bypass_txlist_empty(struct mwifiex_adapter *adapter)
454 {
455         struct mwifiex_private *priv;
456         int i;
457
458         for (i = 0; i < adapter->priv_num; i++) {
459                 priv = adapter->priv[i];
460                 if (!priv)
461                         continue;
462                 if (adapter->if_ops.is_port_ready &&
463                     !adapter->if_ops.is_port_ready(priv))
464                         continue;
465                 if (!skb_queue_empty(&priv->bypass_txq))
466                         return false;
467         }
468
469         return true;
470 }
471
472 /*
473  * This function checks if WMM Tx queue is empty.
474  */
475 int
476 mwifiex_wmm_lists_empty(struct mwifiex_adapter *adapter)
477 {
478         int i;
479         struct mwifiex_private *priv;
480
481         for (i = 0; i < adapter->priv_num; ++i) {
482                 priv = adapter->priv[i];
483                 if (!priv)
484                         continue;
485                 if (!priv->port_open)
486                         continue;
487                 if (adapter->if_ops.is_port_ready &&
488                     !adapter->if_ops.is_port_ready(priv))
489                         continue;
490                 if (atomic_read(&priv->wmm.tx_pkts_queued))
491                         return false;
492         }
493
494         return true;
495 }
496
497 /*
498  * This function deletes all packets in an RA list node.
499  *
500  * The packet sent completion callback handler are called with
501  * status failure, after they are dequeued to ensure proper
502  * cleanup. The RA list node itself is freed at the end.
503  */
504 static void
505 mwifiex_wmm_del_pkts_in_ralist_node(struct mwifiex_private *priv,
506                                     struct mwifiex_ra_list_tbl *ra_list)
507 {
508         struct mwifiex_adapter *adapter = priv->adapter;
509         struct sk_buff *skb, *tmp;
510
511         skb_queue_walk_safe(&ra_list->skb_head, skb, tmp)
512                 mwifiex_write_data_complete(adapter, skb, 0, -1);
513 }
514
515 /*
516  * This function deletes all packets in an RA list.
517  *
518  * Each nodes in the RA list are freed individually first, and then
519  * the RA list itself is freed.
520  */
521 static void
522 mwifiex_wmm_del_pkts_in_ralist(struct mwifiex_private *priv,
523                                struct list_head *ra_list_head)
524 {
525         struct mwifiex_ra_list_tbl *ra_list;
526
527         list_for_each_entry(ra_list, ra_list_head, list)
528                 mwifiex_wmm_del_pkts_in_ralist_node(priv, ra_list);
529 }
530
531 /*
532  * This function deletes all packets in all RA lists.
533  */
534 static void mwifiex_wmm_cleanup_queues(struct mwifiex_private *priv)
535 {
536         int i;
537
538         for (i = 0; i < MAX_NUM_TID; i++)
539                 mwifiex_wmm_del_pkts_in_ralist(priv, &priv->wmm.tid_tbl_ptr[i].
540                                                                        ra_list);
541
542         atomic_set(&priv->wmm.tx_pkts_queued, 0);
543         atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
544 }
545
546 /*
547  * This function deletes all route addresses from all RA lists.
548  */
549 static void mwifiex_wmm_delete_all_ralist(struct mwifiex_private *priv)
550 {
551         struct mwifiex_ra_list_tbl *ra_list, *tmp_node;
552         int i;
553
554         for (i = 0; i < MAX_NUM_TID; ++i) {
555                 mwifiex_dbg(priv->adapter, INFO,
556                             "info: ra_list: freeing buf for tid %d\n", i);
557                 list_for_each_entry_safe(ra_list, tmp_node,
558                                          &priv->wmm.tid_tbl_ptr[i].ra_list,
559                                          list) {
560                         list_del(&ra_list->list);
561                         kfree(ra_list);
562                 }
563
564                 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[i].ra_list);
565         }
566 }
567
568 static int mwifiex_free_ack_frame(int id, void *p, void *data)
569 {
570         pr_warn("Have pending ack frames!\n");
571         kfree_skb(p);
572         return 0;
573 }
574
575 /*
576  * This function cleans up the Tx and Rx queues.
577  *
578  * Cleanup includes -
579  *      - All packets in RA lists
580  *      - All entries in Rx reorder table
581  *      - All entries in Tx BA stream table
582  *      - MPA buffer (if required)
583  *      - All RA lists
584  */
585 void
586 mwifiex_clean_txrx(struct mwifiex_private *priv)
587 {
588         unsigned long flags;
589         struct sk_buff *skb, *tmp;
590
591         mwifiex_11n_cleanup_reorder_tbl(priv);
592         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
593
594         mwifiex_wmm_cleanup_queues(priv);
595         mwifiex_11n_delete_all_tx_ba_stream_tbl(priv);
596
597         if (priv->adapter->if_ops.cleanup_mpa_buf)
598                 priv->adapter->if_ops.cleanup_mpa_buf(priv->adapter);
599
600         mwifiex_wmm_delete_all_ralist(priv);
601         memcpy(tos_to_tid, ac_to_tid, sizeof(tos_to_tid));
602
603         if (priv->adapter->if_ops.clean_pcie_ring &&
604             !priv->adapter->surprise_removed)
605                 priv->adapter->if_ops.clean_pcie_ring(priv->adapter);
606         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
607
608         skb_queue_walk_safe(&priv->tdls_txq, skb, tmp)
609                 mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
610
611         skb_queue_walk_safe(&priv->bypass_txq, skb, tmp)
612                 mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
613         atomic_set(&priv->adapter->bypass_tx_pending, 0);
614
615         idr_for_each(&priv->ack_status_frames, mwifiex_free_ack_frame, NULL);
616         idr_destroy(&priv->ack_status_frames);
617 }
618
619 /*
620  * This function retrieves a particular RA list node, matching with the
621  * given TID and RA address.
622  */
623 struct mwifiex_ra_list_tbl *
624 mwifiex_wmm_get_ralist_node(struct mwifiex_private *priv, u8 tid,
625                             const u8 *ra_addr)
626 {
627         struct mwifiex_ra_list_tbl *ra_list;
628
629         list_for_each_entry(ra_list, &priv->wmm.tid_tbl_ptr[tid].ra_list,
630                             list) {
631                 if (!memcmp(ra_list->ra, ra_addr, ETH_ALEN))
632                         return ra_list;
633         }
634
635         return NULL;
636 }
637
638 void mwifiex_update_ralist_tx_pause(struct mwifiex_private *priv, u8 *mac,
639                                     u8 tx_pause)
640 {
641         struct mwifiex_ra_list_tbl *ra_list;
642         u32 pkt_cnt = 0, tx_pkts_queued;
643         unsigned long flags;
644         int i;
645
646         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
647
648         for (i = 0; i < MAX_NUM_TID; ++i) {
649                 ra_list = mwifiex_wmm_get_ralist_node(priv, i, mac);
650                 if (ra_list && ra_list->tx_paused != tx_pause) {
651                         pkt_cnt += ra_list->total_pkt_count;
652                         ra_list->tx_paused = tx_pause;
653                         if (tx_pause)
654                                 priv->wmm.pkts_paused[i] +=
655                                         ra_list->total_pkt_count;
656                         else
657                                 priv->wmm.pkts_paused[i] -=
658                                         ra_list->total_pkt_count;
659                 }
660         }
661
662         if (pkt_cnt) {
663                 tx_pkts_queued = atomic_read(&priv->wmm.tx_pkts_queued);
664                 if (tx_pause)
665                         tx_pkts_queued -= pkt_cnt;
666                 else
667                         tx_pkts_queued += pkt_cnt;
668
669                 atomic_set(&priv->wmm.tx_pkts_queued, tx_pkts_queued);
670                 atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
671         }
672         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
673 }
674
675 /* This function update non-tdls peer ralist tx_pause while
676  * tdls channel swithing
677  */
678 void mwifiex_update_ralist_tx_pause_in_tdls_cs(struct mwifiex_private *priv,
679                                                u8 *mac, u8 tx_pause)
680 {
681         struct mwifiex_ra_list_tbl *ra_list;
682         u32 pkt_cnt = 0, tx_pkts_queued;
683         unsigned long flags;
684         int i;
685
686         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
687
688         for (i = 0; i < MAX_NUM_TID; ++i) {
689                 list_for_each_entry(ra_list, &priv->wmm.tid_tbl_ptr[i].ra_list,
690                                     list) {
691                         if (!memcmp(ra_list->ra, mac, ETH_ALEN))
692                                 continue;
693
694                         if (ra_list && ra_list->tx_paused != tx_pause) {
695                                 pkt_cnt += ra_list->total_pkt_count;
696                                 ra_list->tx_paused = tx_pause;
697                                 if (tx_pause)
698                                         priv->wmm.pkts_paused[i] +=
699                                                 ra_list->total_pkt_count;
700                                 else
701                                         priv->wmm.pkts_paused[i] -=
702                                                 ra_list->total_pkt_count;
703                         }
704                 }
705         }
706
707         if (pkt_cnt) {
708                 tx_pkts_queued = atomic_read(&priv->wmm.tx_pkts_queued);
709                 if (tx_pause)
710                         tx_pkts_queued -= pkt_cnt;
711                 else
712                         tx_pkts_queued += pkt_cnt;
713
714                 atomic_set(&priv->wmm.tx_pkts_queued, tx_pkts_queued);
715                 atomic_set(&priv->wmm.highest_queued_prio, HIGH_PRIO_TID);
716         }
717         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
718 }
719
720 /*
721  * This function retrieves an RA list node for a given TID and
722  * RA address pair.
723  *
724  * If no such node is found, a new node is added first and then
725  * retrieved.
726  */
727 struct mwifiex_ra_list_tbl *
728 mwifiex_wmm_get_queue_raptr(struct mwifiex_private *priv, u8 tid,
729                             const u8 *ra_addr)
730 {
731         struct mwifiex_ra_list_tbl *ra_list;
732
733         ra_list = mwifiex_wmm_get_ralist_node(priv, tid, ra_addr);
734         if (ra_list)
735                 return ra_list;
736         mwifiex_ralist_add(priv, ra_addr);
737
738         return mwifiex_wmm_get_ralist_node(priv, tid, ra_addr);
739 }
740
741 /*
742  * This function deletes RA list nodes for given mac for all TIDs.
743  * Function also decrements TX pending count accordingly.
744  */
745 void
746 mwifiex_wmm_del_peer_ra_list(struct mwifiex_private *priv, const u8 *ra_addr)
747 {
748         struct mwifiex_ra_list_tbl *ra_list;
749         unsigned long flags;
750         int i;
751
752         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
753
754         for (i = 0; i < MAX_NUM_TID; ++i) {
755                 ra_list = mwifiex_wmm_get_ralist_node(priv, i, ra_addr);
756
757                 if (!ra_list)
758                         continue;
759                 mwifiex_wmm_del_pkts_in_ralist_node(priv, ra_list);
760                 if (ra_list->tx_paused)
761                         priv->wmm.pkts_paused[i] -= ra_list->total_pkt_count;
762                 else
763                         atomic_sub(ra_list->total_pkt_count,
764                                    &priv->wmm.tx_pkts_queued);
765                 list_del(&ra_list->list);
766                 kfree(ra_list);
767         }
768         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
769 }
770
771 /*
772  * This function checks if a particular RA list node exists in a given TID
773  * table index.
774  */
775 int
776 mwifiex_is_ralist_valid(struct mwifiex_private *priv,
777                         struct mwifiex_ra_list_tbl *ra_list, int ptr_index)
778 {
779         struct mwifiex_ra_list_tbl *rlist;
780
781         list_for_each_entry(rlist, &priv->wmm.tid_tbl_ptr[ptr_index].ra_list,
782                             list) {
783                 if (rlist == ra_list)
784                         return true;
785         }
786
787         return false;
788 }
789
790 /*
791  * This function adds a packet to bypass TX queue.
792  * This is special TX queue for packets which can be sent even when port_open
793  * is false.
794  */
795 void
796 mwifiex_wmm_add_buf_bypass_txqueue(struct mwifiex_private *priv,
797                                    struct sk_buff *skb)
798 {
799         skb_queue_tail(&priv->bypass_txq, skb);
800 }
801
802 /*
803  * This function adds a packet to WMM queue.
804  *
805  * In disconnected state the packet is immediately dropped and the
806  * packet send completion callback is called with status failure.
807  *
808  * Otherwise, the correct RA list node is located and the packet
809  * is queued at the list tail.
810  */
811 void
812 mwifiex_wmm_add_buf_txqueue(struct mwifiex_private *priv,
813                             struct sk_buff *skb)
814 {
815         struct mwifiex_adapter *adapter = priv->adapter;
816         u32 tid;
817         struct mwifiex_ra_list_tbl *ra_list;
818         u8 ra[ETH_ALEN], tid_down;
819         unsigned long flags;
820         struct list_head list_head;
821         int tdls_status = TDLS_NOT_SETUP;
822         struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
823         struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb);
824
825         memcpy(ra, eth_hdr->h_dest, ETH_ALEN);
826
827         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
828             ISSUPP_TDLS_ENABLED(adapter->fw_cap_info)) {
829                 if (ntohs(eth_hdr->h_proto) == ETH_P_TDLS)
830                         mwifiex_dbg(adapter, DATA,
831                                     "TDLS setup packet for %pM.\t"
832                                     "Don't block\n", ra);
833                 else if (memcmp(priv->cfg_bssid, ra, ETH_ALEN))
834                         tdls_status = mwifiex_get_tdls_link_status(priv, ra);
835         }
836
837         if (!priv->media_connected && !mwifiex_is_skb_mgmt_frame(skb)) {
838                 mwifiex_dbg(adapter, DATA, "data: drop packet in disconnect\n");
839                 mwifiex_write_data_complete(adapter, skb, 0, -1);
840                 return;
841         }
842
843         tid = skb->priority;
844
845         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
846
847         tid_down = mwifiex_wmm_downgrade_tid(priv, tid);
848
849         /* In case of infra as we have already created the list during
850            association we just don't have to call get_queue_raptr, we will
851            have only 1 raptr for a tid in case of infra */
852         if (!mwifiex_queuing_ra_based(priv) &&
853             !mwifiex_is_skb_mgmt_frame(skb)) {
854                 switch (tdls_status) {
855                 case TDLS_SETUP_COMPLETE:
856                 case TDLS_CHAN_SWITCHING:
857                 case TDLS_IN_BASE_CHAN:
858                 case TDLS_IN_OFF_CHAN:
859                         ra_list = mwifiex_wmm_get_queue_raptr(priv, tid_down,
860                                                               ra);
861                         tx_info->flags |= MWIFIEX_BUF_FLAG_TDLS_PKT;
862                         break;
863                 case TDLS_SETUP_INPROGRESS:
864                         skb_queue_tail(&priv->tdls_txq, skb);
865                         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
866                                                flags);
867                         return;
868                 default:
869                         list_head = priv->wmm.tid_tbl_ptr[tid_down].ra_list;
870                         if (!list_empty(&list_head))
871                                 ra_list = list_first_entry(
872                                         &list_head, struct mwifiex_ra_list_tbl,
873                                         list);
874                         else
875                                 ra_list = NULL;
876                         break;
877                 }
878         } else {
879                 memcpy(ra, skb->data, ETH_ALEN);
880                 if (ra[0] & 0x01 || mwifiex_is_skb_mgmt_frame(skb))
881                         eth_broadcast_addr(ra);
882                 ra_list = mwifiex_wmm_get_queue_raptr(priv, tid_down, ra);
883         }
884
885         if (!ra_list) {
886                 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
887                 mwifiex_write_data_complete(adapter, skb, 0, -1);
888                 return;
889         }
890
891         skb_queue_tail(&ra_list->skb_head, skb);
892
893         ra_list->ba_pkt_count++;
894         ra_list->total_pkt_count++;
895
896         if (atomic_read(&priv->wmm.highest_queued_prio) <
897                                                 priv->tos_to_tid_inv[tid_down])
898                 atomic_set(&priv->wmm.highest_queued_prio,
899                            priv->tos_to_tid_inv[tid_down]);
900
901         if (ra_list->tx_paused)
902                 priv->wmm.pkts_paused[tid_down]++;
903         else
904                 atomic_inc(&priv->wmm.tx_pkts_queued);
905
906         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
907 }
908
909 /*
910  * This function processes the get WMM status command response from firmware.
911  *
912  * The response may contain multiple TLVs -
913  *      - AC Queue status TLVs
914  *      - Current WMM Parameter IE TLV
915  *      - Admission Control action frame TLVs
916  *
917  * This function parses the TLVs and then calls further specific functions
918  * to process any changes in the queue prioritize or state.
919  */
920 int mwifiex_ret_wmm_get_status(struct mwifiex_private *priv,
921                                const struct host_cmd_ds_command *resp)
922 {
923         u8 *curr = (u8 *) &resp->params.get_wmm_status;
924         uint16_t resp_len = le16_to_cpu(resp->size), tlv_len;
925         int mask = IEEE80211_WMM_IE_AP_QOSINFO_PARAM_SET_CNT_MASK;
926         bool valid = true;
927
928         struct mwifiex_ie_types_data *tlv_hdr;
929         struct mwifiex_ie_types_wmm_queue_status *tlv_wmm_qstatus;
930         struct ieee_types_wmm_parameter *wmm_param_ie = NULL;
931         struct mwifiex_wmm_ac_status *ac_status;
932
933         mwifiex_dbg(priv->adapter, INFO,
934                     "info: WMM: WMM_GET_STATUS cmdresp received: %d\n",
935                     resp_len);
936
937         while ((resp_len >= sizeof(tlv_hdr->header)) && valid) {
938                 tlv_hdr = (struct mwifiex_ie_types_data *) curr;
939                 tlv_len = le16_to_cpu(tlv_hdr->header.len);
940
941                 if (resp_len < tlv_len + sizeof(tlv_hdr->header))
942                         break;
943
944                 switch (le16_to_cpu(tlv_hdr->header.type)) {
945                 case TLV_TYPE_WMMQSTATUS:
946                         tlv_wmm_qstatus =
947                                 (struct mwifiex_ie_types_wmm_queue_status *)
948                                 tlv_hdr;
949                         mwifiex_dbg(priv->adapter, CMD,
950                                     "info: CMD_RESP: WMM_GET_STATUS:\t"
951                                     "QSTATUS TLV: %d, %d, %d\n",
952                                     tlv_wmm_qstatus->queue_index,
953                                     tlv_wmm_qstatus->flow_required,
954                                     tlv_wmm_qstatus->disabled);
955
956                         ac_status = &priv->wmm.ac_status[tlv_wmm_qstatus->
957                                                          queue_index];
958                         ac_status->disabled = tlv_wmm_qstatus->disabled;
959                         ac_status->flow_required =
960                                                 tlv_wmm_qstatus->flow_required;
961                         ac_status->flow_created = tlv_wmm_qstatus->flow_created;
962                         break;
963
964                 case WLAN_EID_VENDOR_SPECIFIC:
965                         /*
966                          * Point the regular IEEE IE 2 bytes into the Marvell IE
967                          *   and setup the IEEE IE type and length byte fields
968                          */
969
970                         wmm_param_ie =
971                                 (struct ieee_types_wmm_parameter *) (curr +
972                                                                     2);
973                         wmm_param_ie->vend_hdr.len = (u8) tlv_len;
974                         wmm_param_ie->vend_hdr.element_id =
975                                                 WLAN_EID_VENDOR_SPECIFIC;
976
977                         mwifiex_dbg(priv->adapter, CMD,
978                                     "info: CMD_RESP: WMM_GET_STATUS:\t"
979                                     "WMM Parameter Set Count: %d\n",
980                                     wmm_param_ie->qos_info_bitmap & mask);
981
982                         memcpy((u8 *) &priv->curr_bss_params.bss_descriptor.
983                                wmm_ie, wmm_param_ie,
984                                wmm_param_ie->vend_hdr.len + 2);
985
986                         break;
987
988                 default:
989                         valid = false;
990                         break;
991                 }
992
993                 curr += (tlv_len + sizeof(tlv_hdr->header));
994                 resp_len -= (tlv_len + sizeof(tlv_hdr->header));
995         }
996
997         mwifiex_wmm_setup_queue_priorities(priv, wmm_param_ie);
998         mwifiex_wmm_setup_ac_downgrade(priv);
999
1000         return 0;
1001 }
1002
1003 /*
1004  * Callback handler from the command module to allow insertion of a WMM TLV.
1005  *
1006  * If the BSS we are associating to supports WMM, this function adds the
1007  * required WMM Information IE to the association request command buffer in
1008  * the form of a Marvell extended IEEE IE.
1009  */
1010 u32
1011 mwifiex_wmm_process_association_req(struct mwifiex_private *priv,
1012                                     u8 **assoc_buf,
1013                                     struct ieee_types_wmm_parameter *wmm_ie,
1014                                     struct ieee80211_ht_cap *ht_cap)
1015 {
1016         struct mwifiex_ie_types_wmm_param_set *wmm_tlv;
1017         u32 ret_len = 0;
1018
1019         /* Null checks */
1020         if (!assoc_buf)
1021                 return 0;
1022         if (!(*assoc_buf))
1023                 return 0;
1024
1025         if (!wmm_ie)
1026                 return 0;
1027
1028         mwifiex_dbg(priv->adapter, INFO,
1029                     "info: WMM: process assoc req: bss->wmm_ie=%#x\n",
1030                     wmm_ie->vend_hdr.element_id);
1031
1032         if ((priv->wmm_required ||
1033              (ht_cap && (priv->adapter->config_bands & BAND_GN ||
1034              priv->adapter->config_bands & BAND_AN))) &&
1035             wmm_ie->vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC) {
1036                 wmm_tlv = (struct mwifiex_ie_types_wmm_param_set *) *assoc_buf;
1037                 wmm_tlv->header.type = cpu_to_le16((u16) wmm_info_ie[0]);
1038                 wmm_tlv->header.len = cpu_to_le16((u16) wmm_info_ie[1]);
1039                 memcpy(wmm_tlv->wmm_ie, &wmm_info_ie[2],
1040                        le16_to_cpu(wmm_tlv->header.len));
1041                 if (wmm_ie->qos_info_bitmap & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD)
1042                         memcpy((u8 *) (wmm_tlv->wmm_ie
1043                                        + le16_to_cpu(wmm_tlv->header.len)
1044                                        - sizeof(priv->wmm_qosinfo)),
1045                                &priv->wmm_qosinfo, sizeof(priv->wmm_qosinfo));
1046
1047                 ret_len = sizeof(wmm_tlv->header)
1048                           + le16_to_cpu(wmm_tlv->header.len);
1049
1050                 *assoc_buf += ret_len;
1051         }
1052
1053         return ret_len;
1054 }
1055
1056 /*
1057  * This function computes the time delay in the driver queues for a
1058  * given packet.
1059  *
1060  * When the packet is received at the OS/Driver interface, the current
1061  * time is set in the packet structure. The difference between the present
1062  * time and that received time is computed in this function and limited
1063  * based on pre-compiled limits in the driver.
1064  */
1065 u8
1066 mwifiex_wmm_compute_drv_pkt_delay(struct mwifiex_private *priv,
1067                                   const struct sk_buff *skb)
1068 {
1069         u32 queue_delay = ktime_to_ms(net_timedelta(skb->tstamp));
1070         u8 ret_val;
1071
1072         /*
1073          * Queue delay is passed as a uint8 in units of 2ms (ms shifted
1074          *  by 1). Min value (other than 0) is therefore 2ms, max is 510ms.
1075          *
1076          * Pass max value if queue_delay is beyond the uint8 range
1077          */
1078         ret_val = (u8) (min(queue_delay, priv->wmm.drv_pkt_delay_max) >> 1);
1079
1080         mwifiex_dbg(priv->adapter, DATA, "data: WMM: Pkt Delay: %d ms,\t"
1081                     "%d ms sent to FW\n", queue_delay, ret_val);
1082
1083         return ret_val;
1084 }
1085
1086 /*
1087  * This function retrieves the highest priority RA list table pointer.
1088  */
1089 static struct mwifiex_ra_list_tbl *
1090 mwifiex_wmm_get_highest_priolist_ptr(struct mwifiex_adapter *adapter,
1091                                      struct mwifiex_private **priv, int *tid)
1092 {
1093         struct mwifiex_private *priv_tmp;
1094         struct mwifiex_ra_list_tbl *ptr;
1095         struct mwifiex_tid_tbl *tid_ptr;
1096         atomic_t *hqp;
1097         unsigned long flags_ra;
1098         int i, j;
1099
1100         /* check the BSS with highest priority first */
1101         for (j = adapter->priv_num - 1; j >= 0; --j) {
1102                 /* iterate over BSS with the equal priority */
1103                 list_for_each_entry(adapter->bss_prio_tbl[j].bss_prio_cur,
1104                                     &adapter->bss_prio_tbl[j].bss_prio_head,
1105                                     list) {
1106
1107                         priv_tmp = adapter->bss_prio_tbl[j].bss_prio_cur->priv;
1108
1109                         if (!priv_tmp->port_open ||
1110                             (atomic_read(&priv_tmp->wmm.tx_pkts_queued) == 0))
1111                                 continue;
1112
1113                         if (adapter->if_ops.is_port_ready &&
1114                             !adapter->if_ops.is_port_ready(priv_tmp))
1115                                 continue;
1116
1117                         /* iterate over the WMM queues of the BSS */
1118                         hqp = &priv_tmp->wmm.highest_queued_prio;
1119                         for (i = atomic_read(hqp); i >= LOW_PRIO_TID; --i) {
1120
1121                                 spin_lock_irqsave(&priv_tmp->wmm.
1122                                                   ra_list_spinlock, flags_ra);
1123
1124                                 tid_ptr = &(priv_tmp)->wmm.
1125                                         tid_tbl_ptr[tos_to_tid[i]];
1126
1127                                 /* iterate over receiver addresses */
1128                                 list_for_each_entry(ptr, &tid_ptr->ra_list,
1129                                                     list) {
1130
1131                                         if (!ptr->tx_paused &&
1132                                             !skb_queue_empty(&ptr->skb_head))
1133                                                 /* holds both locks */
1134                                                 goto found;
1135                                 }
1136
1137                                 spin_unlock_irqrestore(&priv_tmp->wmm.
1138                                                        ra_list_spinlock,
1139                                                        flags_ra);
1140                         }
1141                 }
1142
1143         }
1144
1145         return NULL;
1146
1147 found:
1148         /* holds ra_list_spinlock */
1149         if (atomic_read(hqp) > i)
1150                 atomic_set(hqp, i);
1151         spin_unlock_irqrestore(&priv_tmp->wmm.ra_list_spinlock, flags_ra);
1152
1153         *priv = priv_tmp;
1154         *tid = tos_to_tid[i];
1155
1156         return ptr;
1157 }
1158
1159 /* This functions rotates ra and bss lists so packets are picked round robin.
1160  *
1161  * After a packet is successfully transmitted, rotate the ra list, so the ra
1162  * next to the one transmitted, will come first in the list. This way we pick
1163  * the ra' in a round robin fashion. Same applies to bss nodes of equal
1164  * priority.
1165  *
1166  * Function also increments wmm.packets_out counter.
1167  */
1168 void mwifiex_rotate_priolists(struct mwifiex_private *priv,
1169                                  struct mwifiex_ra_list_tbl *ra,
1170                                  int tid)
1171 {
1172         struct mwifiex_adapter *adapter = priv->adapter;
1173         struct mwifiex_bss_prio_tbl *tbl = adapter->bss_prio_tbl;
1174         struct mwifiex_tid_tbl *tid_ptr = &priv->wmm.tid_tbl_ptr[tid];
1175         unsigned long flags;
1176
1177         spin_lock_irqsave(&tbl[priv->bss_priority].bss_prio_lock, flags);
1178         /*
1179          * dirty trick: we remove 'head' temporarily and reinsert it after
1180          * curr bss node. imagine list to stay fixed while head is moved
1181          */
1182         list_move(&tbl[priv->bss_priority].bss_prio_head,
1183                   &tbl[priv->bss_priority].bss_prio_cur->list);
1184         spin_unlock_irqrestore(&tbl[priv->bss_priority].bss_prio_lock, flags);
1185
1186         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
1187         if (mwifiex_is_ralist_valid(priv, ra, tid)) {
1188                 priv->wmm.packets_out[tid]++;
1189                 /* same as above */
1190                 list_move(&tid_ptr->ra_list, &ra->list);
1191         }
1192         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
1193 }
1194
1195 /*
1196  * This function checks if 11n aggregation is possible.
1197  */
1198 static int
1199 mwifiex_is_11n_aggragation_possible(struct mwifiex_private *priv,
1200                                     struct mwifiex_ra_list_tbl *ptr,
1201                                     int max_buf_size)
1202 {
1203         int count = 0, total_size = 0;
1204         struct sk_buff *skb, *tmp;
1205         int max_amsdu_size;
1206
1207         if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP && priv->ap_11n_enabled &&
1208             ptr->is_11n_enabled)
1209                 max_amsdu_size = min_t(int, ptr->max_amsdu, max_buf_size);
1210         else
1211                 max_amsdu_size = max_buf_size;
1212
1213         skb_queue_walk_safe(&ptr->skb_head, skb, tmp) {
1214                 total_size += skb->len;
1215                 if (total_size >= max_amsdu_size)
1216                         break;
1217                 if (++count >= MIN_NUM_AMSDU)
1218                         return true;
1219         }
1220
1221         return false;
1222 }
1223
1224 /*
1225  * This function sends a single packet to firmware for transmission.
1226  */
1227 static void
1228 mwifiex_send_single_packet(struct mwifiex_private *priv,
1229                            struct mwifiex_ra_list_tbl *ptr, int ptr_index,
1230                            unsigned long ra_list_flags)
1231                            __releases(&priv->wmm.ra_list_spinlock)
1232 {
1233         struct sk_buff *skb, *skb_next;
1234         struct mwifiex_tx_param tx_param;
1235         struct mwifiex_adapter *adapter = priv->adapter;
1236         struct mwifiex_txinfo *tx_info;
1237
1238         if (skb_queue_empty(&ptr->skb_head)) {
1239                 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1240                                        ra_list_flags);
1241                 mwifiex_dbg(adapter, DATA, "data: nothing to send\n");
1242                 return;
1243         }
1244
1245         skb = skb_dequeue(&ptr->skb_head);
1246
1247         tx_info = MWIFIEX_SKB_TXCB(skb);
1248         mwifiex_dbg(adapter, DATA,
1249                     "data: dequeuing the packet %p %p\n", ptr, skb);
1250
1251         ptr->total_pkt_count--;
1252
1253         if (!skb_queue_empty(&ptr->skb_head))
1254                 skb_next = skb_peek(&ptr->skb_head);
1255         else
1256                 skb_next = NULL;
1257
1258         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, ra_list_flags);
1259
1260         tx_param.next_pkt_len = ((skb_next) ? skb_next->len +
1261                                 sizeof(struct txpd) : 0);
1262
1263         if (mwifiex_process_tx(priv, skb, &tx_param) == -EBUSY) {
1264                 /* Queue the packet back at the head */
1265                 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1266
1267                 if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1268                         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1269                                                ra_list_flags);
1270                         mwifiex_write_data_complete(adapter, skb, 0, -1);
1271                         return;
1272                 }
1273
1274                 skb_queue_tail(&ptr->skb_head, skb);
1275
1276                 ptr->total_pkt_count++;
1277                 ptr->ba_pkt_count++;
1278                 tx_info->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT;
1279                 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1280                                        ra_list_flags);
1281         } else {
1282                 mwifiex_rotate_priolists(priv, ptr, ptr_index);
1283                 atomic_dec(&priv->wmm.tx_pkts_queued);
1284         }
1285 }
1286
1287 /*
1288  * This function checks if the first packet in the given RA list
1289  * is already processed or not.
1290  */
1291 static int
1292 mwifiex_is_ptr_processed(struct mwifiex_private *priv,
1293                          struct mwifiex_ra_list_tbl *ptr)
1294 {
1295         struct sk_buff *skb;
1296         struct mwifiex_txinfo *tx_info;
1297
1298         if (skb_queue_empty(&ptr->skb_head))
1299                 return false;
1300
1301         skb = skb_peek(&ptr->skb_head);
1302
1303         tx_info = MWIFIEX_SKB_TXCB(skb);
1304         if (tx_info->flags & MWIFIEX_BUF_FLAG_REQUEUED_PKT)
1305                 return true;
1306
1307         return false;
1308 }
1309
1310 /*
1311  * This function sends a single processed packet to firmware for
1312  * transmission.
1313  */
1314 static void
1315 mwifiex_send_processed_packet(struct mwifiex_private *priv,
1316                               struct mwifiex_ra_list_tbl *ptr, int ptr_index,
1317                               unsigned long ra_list_flags)
1318                                 __releases(&priv->wmm.ra_list_spinlock)
1319 {
1320         struct mwifiex_tx_param tx_param;
1321         struct mwifiex_adapter *adapter = priv->adapter;
1322         int ret = -1;
1323         struct sk_buff *skb, *skb_next;
1324         struct mwifiex_txinfo *tx_info;
1325
1326         if (skb_queue_empty(&ptr->skb_head)) {
1327                 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1328                                        ra_list_flags);
1329                 return;
1330         }
1331
1332         skb = skb_dequeue(&ptr->skb_head);
1333
1334         if (adapter->data_sent || adapter->tx_lock_flag) {
1335                 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1336                                        ra_list_flags);
1337                 skb_queue_tail(&adapter->tx_data_q, skb);
1338                 atomic_inc(&adapter->tx_queued);
1339                 return;
1340         }
1341
1342         if (!skb_queue_empty(&ptr->skb_head))
1343                 skb_next = skb_peek(&ptr->skb_head);
1344         else
1345                 skb_next = NULL;
1346
1347         tx_info = MWIFIEX_SKB_TXCB(skb);
1348
1349         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, ra_list_flags);
1350
1351         if (adapter->iface_type == MWIFIEX_USB) {
1352                 ret = adapter->if_ops.host_to_card(adapter, priv->usb_port,
1353                                                    skb, NULL);
1354         } else {
1355                 tx_param.next_pkt_len =
1356                         ((skb_next) ? skb_next->len +
1357                          sizeof(struct txpd) : 0);
1358                 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_DATA,
1359                                                    skb, &tx_param);
1360         }
1361
1362         switch (ret) {
1363         case -EBUSY:
1364                 mwifiex_dbg(adapter, ERROR, "data: -EBUSY is returned\n");
1365                 spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags);
1366
1367                 if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1368                         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1369                                                ra_list_flags);
1370                         mwifiex_write_data_complete(adapter, skb, 0, -1);
1371                         return;
1372                 }
1373
1374                 skb_queue_tail(&ptr->skb_head, skb);
1375
1376                 tx_info->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT;
1377                 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock,
1378                                        ra_list_flags);
1379                 break;
1380         case -1:
1381                 mwifiex_dbg(adapter, ERROR, "host_to_card failed: %#x\n", ret);
1382                 adapter->dbg.num_tx_host_to_card_failure++;
1383                 mwifiex_write_data_complete(adapter, skb, 0, ret);
1384                 break;
1385         case -EINPROGRESS:
1386                 break;
1387         case 0:
1388                 mwifiex_write_data_complete(adapter, skb, 0, ret);
1389         default:
1390                 break;
1391         }
1392         if (ret != -EBUSY) {
1393                 mwifiex_rotate_priolists(priv, ptr, ptr_index);
1394                 atomic_dec(&priv->wmm.tx_pkts_queued);
1395         }
1396 }
1397
1398 /*
1399  * This function dequeues a packet from the highest priority list
1400  * and transmits it.
1401  */
1402 static int
1403 mwifiex_dequeue_tx_packet(struct mwifiex_adapter *adapter)
1404 {
1405         struct mwifiex_ra_list_tbl *ptr;
1406         struct mwifiex_private *priv = NULL;
1407         int ptr_index = 0;
1408         u8 ra[ETH_ALEN];
1409         int tid_del = 0, tid = 0;
1410         unsigned long flags;
1411
1412         ptr = mwifiex_wmm_get_highest_priolist_ptr(adapter, &priv, &ptr_index);
1413         if (!ptr)
1414                 return -1;
1415
1416         tid = mwifiex_get_tid(ptr);
1417
1418         mwifiex_dbg(adapter, DATA, "data: tid=%d\n", tid);
1419
1420         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
1421         if (!mwifiex_is_ralist_valid(priv, ptr, ptr_index)) {
1422                 spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
1423                 return -1;
1424         }
1425
1426         if (mwifiex_is_ptr_processed(priv, ptr)) {
1427                 mwifiex_send_processed_packet(priv, ptr, ptr_index, flags);
1428                 /* ra_list_spinlock has been freed in
1429                    mwifiex_send_processed_packet() */
1430                 return 0;
1431         }
1432
1433         if (!ptr->is_11n_enabled ||
1434                 ptr->ba_status ||
1435                 priv->wps.session_enable) {
1436                 if (ptr->is_11n_enabled &&
1437                         ptr->ba_status &&
1438                         ptr->amsdu_in_ampdu &&
1439                         mwifiex_is_amsdu_allowed(priv, tid) &&
1440                         mwifiex_is_11n_aggragation_possible(priv, ptr,
1441                                                         adapter->tx_buf_size))
1442                         mwifiex_11n_aggregate_pkt(priv, ptr, ptr_index, flags);
1443                         /* ra_list_spinlock has been freed in
1444                          * mwifiex_11n_aggregate_pkt()
1445                          */
1446                 else
1447                         mwifiex_send_single_packet(priv, ptr, ptr_index, flags);
1448                         /* ra_list_spinlock has been freed in
1449                          * mwifiex_send_single_packet()
1450                          */
1451         } else {
1452                 if (mwifiex_is_ampdu_allowed(priv, ptr, tid) &&
1453                     ptr->ba_pkt_count > ptr->ba_packet_thr) {
1454                         if (mwifiex_space_avail_for_new_ba_stream(adapter)) {
1455                                 mwifiex_create_ba_tbl(priv, ptr->ra, tid,
1456                                                       BA_SETUP_INPROGRESS);
1457                                 mwifiex_send_addba(priv, tid, ptr->ra);
1458                         } else if (mwifiex_find_stream_to_delete
1459                                    (priv, tid, &tid_del, ra)) {
1460                                 mwifiex_create_ba_tbl(priv, ptr->ra, tid,
1461                                                       BA_SETUP_INPROGRESS);
1462                                 mwifiex_send_delba(priv, tid_del, ra, 1);
1463                         }
1464                 }
1465                 if (mwifiex_is_amsdu_allowed(priv, tid) &&
1466                     mwifiex_is_11n_aggragation_possible(priv, ptr,
1467                                                         adapter->tx_buf_size))
1468                         mwifiex_11n_aggregate_pkt(priv, ptr, ptr_index, flags);
1469                         /* ra_list_spinlock has been freed in
1470                            mwifiex_11n_aggregate_pkt() */
1471                 else
1472                         mwifiex_send_single_packet(priv, ptr, ptr_index, flags);
1473                         /* ra_list_spinlock has been freed in
1474                            mwifiex_send_single_packet() */
1475         }
1476         return 0;
1477 }
1478
1479 void mwifiex_process_bypass_tx(struct mwifiex_adapter *adapter)
1480 {
1481         struct mwifiex_tx_param tx_param;
1482         struct sk_buff *skb;
1483         struct mwifiex_txinfo *tx_info;
1484         struct mwifiex_private *priv;
1485         int i;
1486
1487         if (adapter->data_sent || adapter->tx_lock_flag)
1488                 return;
1489
1490         for (i = 0; i < adapter->priv_num; ++i) {
1491                 priv = adapter->priv[i];
1492
1493                 if (!priv)
1494                         continue;
1495
1496                 if (adapter->if_ops.is_port_ready &&
1497                     !adapter->if_ops.is_port_ready(priv))
1498                         continue;
1499
1500                 if (skb_queue_empty(&priv->bypass_txq))
1501                         continue;
1502
1503                 skb = skb_dequeue(&priv->bypass_txq);
1504                 tx_info = MWIFIEX_SKB_TXCB(skb);
1505
1506                 /* no aggregation for bypass packets */
1507                 tx_param.next_pkt_len = 0;
1508
1509                 if (mwifiex_process_tx(priv, skb, &tx_param) == -EBUSY) {
1510                         skb_queue_head(&priv->bypass_txq, skb);
1511                         tx_info->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT;
1512                 } else {
1513                         atomic_dec(&adapter->bypass_tx_pending);
1514                 }
1515         }
1516 }
1517
1518 /*
1519  * This function transmits the highest priority packet awaiting in the
1520  * WMM Queues.
1521  */
1522 void
1523 mwifiex_wmm_process_tx(struct mwifiex_adapter *adapter)
1524 {
1525         do {
1526                 if (mwifiex_dequeue_tx_packet(adapter))
1527                         break;
1528                 if (adapter->iface_type != MWIFIEX_SDIO) {
1529                         if (adapter->data_sent ||
1530                             adapter->tx_lock_flag)
1531                                 break;
1532                 } else {
1533                         if (atomic_read(&adapter->tx_queued) >=
1534                             MWIFIEX_MAX_PKTS_TXQ)
1535                                 break;
1536                 }
1537         } while (!mwifiex_wmm_lists_empty(adapter));
1538 }