Merge tag 'iwlwifi-next-for-kalle-2014-12-30' of https://git.kernel.org/pub/scm/linux...
[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         return ret;
68 }
69 EXPORT_SYMBOL_GPL(mwifiex_handle_rx_packet);
70
71 /*
72  * This function sends a packet to device.
73  *
74  * It processes the packet to add the TxPD, checks condition and
75  * sends the processed packet to firmware for transmission.
76  *
77  * On successful completion, the function calls the completion callback
78  * and logs the time.
79  */
80 int mwifiex_process_tx(struct mwifiex_private *priv, struct sk_buff *skb,
81                        struct mwifiex_tx_param *tx_param)
82 {
83         int ret = -1;
84         struct mwifiex_adapter *adapter = priv->adapter;
85         u8 *head_ptr;
86         struct txpd *local_tx_pd = NULL;
87
88         if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
89                 head_ptr = mwifiex_process_uap_txpd(priv, skb);
90         else
91                 head_ptr = mwifiex_process_sta_txpd(priv, skb);
92
93         if (head_ptr) {
94                 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA)
95                         local_tx_pd =
96                                 (struct txpd *) (head_ptr + INTF_HEADER_LEN);
97                 if (adapter->iface_type == MWIFIEX_USB) {
98                         adapter->data_sent = true;
99                         skb_pull(skb, INTF_HEADER_LEN);
100                         ret = adapter->if_ops.host_to_card(adapter,
101                                                            MWIFIEX_USB_EP_DATA,
102                                                            skb, NULL);
103                 } else {
104                         ret = adapter->if_ops.host_to_card(adapter,
105                                                            MWIFIEX_TYPE_DATA,
106                                                            skb, tx_param);
107                 }
108         }
109
110         switch (ret) {
111         case -ENOSR:
112                 dev_dbg(adapter->dev, "data: -ENOSR is returned\n");
113                 break;
114         case -EBUSY:
115                 if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
116                     (adapter->pps_uapsd_mode) && (adapter->tx_lock_flag)) {
117                                 priv->adapter->tx_lock_flag = false;
118                                 if (local_tx_pd)
119                                         local_tx_pd->flags = 0;
120                 }
121                 dev_dbg(adapter->dev, "data: -EBUSY is returned\n");
122                 break;
123         case -1:
124                 if (adapter->iface_type != MWIFIEX_PCIE)
125                         adapter->data_sent = false;
126                 dev_err(adapter->dev, "mwifiex_write_data_async failed: 0x%X\n",
127                         ret);
128                 adapter->dbg.num_tx_host_to_card_failure++;
129                 mwifiex_write_data_complete(adapter, skb, 0, ret);
130                 break;
131         case -EINPROGRESS:
132                 if (adapter->iface_type != MWIFIEX_PCIE)
133                         adapter->data_sent = false;
134                 break;
135         case 0:
136                 mwifiex_write_data_complete(adapter, skb, 0, ret);
137                 break;
138         default:
139                 break;
140         }
141
142         return ret;
143 }
144
145 /*
146  * Packet send completion callback handler.
147  *
148  * It either frees the buffer directly or forwards it to another
149  * completion callback which checks conditions, updates statistics,
150  * wakes up stalled traffic queue if required, and then frees the buffer.
151  */
152 int mwifiex_write_data_complete(struct mwifiex_adapter *adapter,
153                                 struct sk_buff *skb, int aggr, int status)
154 {
155         struct mwifiex_private *priv;
156         struct mwifiex_txinfo *tx_info;
157         struct netdev_queue *txq;
158         int index;
159
160         if (!skb)
161                 return 0;
162
163         tx_info = MWIFIEX_SKB_TXCB(skb);
164         priv = mwifiex_get_priv_by_id(adapter, tx_info->bss_num,
165                                       tx_info->bss_type);
166         if (!priv)
167                 goto done;
168
169         if (adapter->iface_type == MWIFIEX_USB)
170                 adapter->data_sent = false;
171
172         mwifiex_set_trans_start(priv->netdev);
173         if (!status) {
174                 priv->stats.tx_packets++;
175                 priv->stats.tx_bytes += tx_info->pkt_len;
176                 if (priv->tx_timeout_cnt)
177                         priv->tx_timeout_cnt = 0;
178         } else {
179                 priv->stats.tx_errors++;
180         }
181
182         if (tx_info->flags & MWIFIEX_BUF_FLAG_BRIDGED_PKT)
183                 atomic_dec_return(&adapter->pending_bridged_pkts);
184
185         if (aggr)
186                 /* For skb_aggr, do not wake up tx queue */
187                 goto done;
188
189         atomic_dec(&adapter->tx_pending);
190
191         index = mwifiex_1d_to_wmm_queue[skb->priority];
192         if (atomic_dec_return(&priv->wmm_tx_pending[index]) < LOW_TX_PENDING) {
193                 txq = netdev_get_tx_queue(priv->netdev, index);
194                 if (netif_tx_queue_stopped(txq)) {
195                         netif_tx_wake_queue(txq);
196                         dev_dbg(adapter->dev, "wake queue: %d\n", index);
197                 }
198         }
199 done:
200         dev_kfree_skb_any(skb);
201
202         return 0;
203 }
204 EXPORT_SYMBOL_GPL(mwifiex_write_data_complete);
205
206 void mwifiex_parse_tx_status_event(struct mwifiex_private *priv,
207                                    void *event_body)
208 {
209         struct tx_status_event *tx_status = (void *)priv->adapter->event_body;
210         struct sk_buff *ack_skb;
211         unsigned long flags;
212         struct mwifiex_txinfo *tx_info;
213
214         if (!tx_status->tx_token_id)
215                 return;
216
217         spin_lock_irqsave(&priv->ack_status_lock, flags);
218         ack_skb = idr_find(&priv->ack_status_frames, tx_status->tx_token_id);
219         if (ack_skb)
220                 idr_remove(&priv->ack_status_frames, tx_status->tx_token_id);
221         spin_unlock_irqrestore(&priv->ack_status_lock, flags);
222
223         if (ack_skb) {
224                 tx_info = MWIFIEX_SKB_TXCB(ack_skb);
225
226                 if (tx_info->flags & MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS) {
227                         /* consumes ack_skb */
228                         skb_complete_wifi_ack(ack_skb, !tx_status->status);
229                 } else {
230                         cfg80211_mgmt_tx_status(priv->wdev, tx_info->cookie,
231                                                 ack_skb->data, ack_skb->len,
232                                                 !tx_status->status, GFP_ATOMIC);
233                         dev_kfree_skb_any(ack_skb);
234                 }
235         }
236 }