Merge tag 'at91-drivers-for-3.17' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / net / wireless / mwifiex / txrx.c
1 /*
2  * Marvell Wireless LAN device driver: generic TX/RX data handling
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
27 /*
28  * This function processes the received buffer.
29  *
30  * Main responsibility of this function is to parse the RxPD to
31  * identify the correct interface this packet is headed for and
32  * forwarding it to the associated handling function, where the
33  * packet will be further processed and sent to kernel/upper layer
34  * if required.
35  */
36 int mwifiex_handle_rx_packet(struct mwifiex_adapter *adapter,
37                              struct sk_buff *skb)
38 {
39         struct mwifiex_private *priv =
40                 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
41         struct rxpd *local_rx_pd;
42         struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
43         int ret;
44
45         local_rx_pd = (struct rxpd *) (skb->data);
46         /* Get the BSS number from rxpd, get corresponding priv */
47         priv = mwifiex_get_priv_by_id(adapter, local_rx_pd->bss_num &
48                                       BSS_NUM_MASK, local_rx_pd->bss_type);
49         if (!priv)
50                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
51
52         if (!priv) {
53                 dev_err(adapter->dev, "data: priv not found. Drop RX packet\n");
54                 dev_kfree_skb_any(skb);
55                 return -1;
56         }
57
58         memset(rx_info, 0, sizeof(*rx_info));
59         rx_info->bss_num = priv->bss_num;
60         rx_info->bss_type = priv->bss_type;
61
62         if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
63                 ret = mwifiex_process_uap_rx_packet(priv, skb);
64         else
65                 ret = mwifiex_process_sta_rx_packet(priv, skb);
66
67         /* Decrement RX pending counter for each packet */
68         if (adapter->if_ops.data_complete)
69                 adapter->if_ops.data_complete(adapter);
70
71         return ret;
72 }
73 EXPORT_SYMBOL_GPL(mwifiex_handle_rx_packet);
74
75 /*
76  * This function sends a packet to device.
77  *
78  * It processes the packet to add the TxPD, checks condition and
79  * sends the processed packet to firmware for transmission.
80  *
81  * On successful completion, the function calls the completion callback
82  * and logs the time.
83  */
84 int mwifiex_process_tx(struct mwifiex_private *priv, struct sk_buff *skb,
85                        struct mwifiex_tx_param *tx_param)
86 {
87         int ret = -1;
88         struct mwifiex_adapter *adapter = priv->adapter;
89         u8 *head_ptr;
90         struct txpd *local_tx_pd = NULL;
91
92         if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
93                 head_ptr = mwifiex_process_uap_txpd(priv, skb);
94         else
95                 head_ptr = mwifiex_process_sta_txpd(priv, skb);
96
97         if (head_ptr) {
98                 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA)
99                         local_tx_pd =
100                                 (struct txpd *) (head_ptr + INTF_HEADER_LEN);
101                 if (adapter->iface_type == MWIFIEX_USB) {
102                         adapter->data_sent = true;
103                         skb_pull(skb, INTF_HEADER_LEN);
104                         ret = adapter->if_ops.host_to_card(adapter,
105                                                            MWIFIEX_USB_EP_DATA,
106                                                            skb, NULL);
107                 } else {
108                         ret = adapter->if_ops.host_to_card(adapter,
109                                                            MWIFIEX_TYPE_DATA,
110                                                            skb, tx_param);
111                 }
112         }
113
114         switch (ret) {
115         case -ENOSR:
116                 dev_dbg(adapter->dev, "data: -ENOSR is returned\n");
117                 break;
118         case -EBUSY:
119                 if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
120                     (adapter->pps_uapsd_mode) && (adapter->tx_lock_flag)) {
121                                 priv->adapter->tx_lock_flag = false;
122                                 if (local_tx_pd)
123                                         local_tx_pd->flags = 0;
124                 }
125                 dev_dbg(adapter->dev, "data: -EBUSY is returned\n");
126                 break;
127         case -1:
128                 if (adapter->iface_type != MWIFIEX_PCIE)
129                         adapter->data_sent = false;
130                 dev_err(adapter->dev, "mwifiex_write_data_async failed: 0x%X\n",
131                         ret);
132                 adapter->dbg.num_tx_host_to_card_failure++;
133                 mwifiex_write_data_complete(adapter, skb, 0, ret);
134                 break;
135         case -EINPROGRESS:
136                 if (adapter->iface_type != MWIFIEX_PCIE)
137                         adapter->data_sent = false;
138                 break;
139         case 0:
140                 mwifiex_write_data_complete(adapter, skb, 0, ret);
141                 break;
142         default:
143                 break;
144         }
145
146         return ret;
147 }
148
149 /*
150  * Packet send completion callback handler.
151  *
152  * It either frees the buffer directly or forwards it to another
153  * completion callback which checks conditions, updates statistics,
154  * wakes up stalled traffic queue if required, and then frees the buffer.
155  */
156 int mwifiex_write_data_complete(struct mwifiex_adapter *adapter,
157                                 struct sk_buff *skb, int aggr, int status)
158 {
159         struct mwifiex_private *priv;
160         struct mwifiex_txinfo *tx_info;
161         struct netdev_queue *txq;
162         int index;
163
164         if (!skb)
165                 return 0;
166
167         tx_info = MWIFIEX_SKB_TXCB(skb);
168         priv = mwifiex_get_priv_by_id(adapter, tx_info->bss_num,
169                                       tx_info->bss_type);
170         if (!priv)
171                 goto done;
172
173         if (adapter->iface_type == MWIFIEX_USB)
174                 adapter->data_sent = false;
175
176         mwifiex_set_trans_start(priv->netdev);
177         if (!status) {
178                 priv->stats.tx_packets++;
179                 priv->stats.tx_bytes += tx_info->pkt_len;
180                 if (priv->tx_timeout_cnt)
181                         priv->tx_timeout_cnt = 0;
182         } else {
183                 priv->stats.tx_errors++;
184         }
185
186         if (tx_info->flags & MWIFIEX_BUF_FLAG_BRIDGED_PKT)
187                 atomic_dec_return(&adapter->pending_bridged_pkts);
188
189         if (aggr)
190                 /* For skb_aggr, do not wake up tx queue */
191                 goto done;
192
193         atomic_dec(&adapter->tx_pending);
194
195         index = mwifiex_1d_to_wmm_queue[skb->priority];
196         if (atomic_dec_return(&priv->wmm_tx_pending[index]) < LOW_TX_PENDING) {
197                 txq = netdev_get_tx_queue(priv->netdev, index);
198                 if (netif_tx_queue_stopped(txq)) {
199                         netif_tx_wake_queue(txq);
200                         dev_dbg(adapter->dev, "wake queue: %d\n", index);
201                 }
202         }
203 done:
204         dev_kfree_skb_any(skb);
205
206         return 0;
207 }
208 EXPORT_SYMBOL_GPL(mwifiex_write_data_complete);
209