Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetoot...
[cascardo/linux.git] / drivers / net / wireless / mwifiex / uap_txrx.c
1 /*
2  * Marvell Wireless LAN device driver: AP TX and RX data handling
3  *
4  * Copyright (C) 2012, 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 "main.h"
23 #include "wmm.h"
24 #include "11n_aggr.h"
25 #include "11n_rxreorder.h"
26
27 /* This function checks if particular RA list has packets more than low bridge
28  * packet threshold and then deletes packet from this RA list.
29  * Function deletes packets from such RA list and returns true. If no such list
30  * is found, false is returned.
31  */
32 static bool
33 mwifiex_uap_del_tx_pkts_in_ralist(struct mwifiex_private *priv,
34                                   struct list_head *ra_list_head)
35 {
36         struct mwifiex_ra_list_tbl *ra_list;
37         struct sk_buff *skb, *tmp;
38         bool pkt_deleted = false;
39         struct mwifiex_txinfo *tx_info;
40         struct mwifiex_adapter *adapter = priv->adapter;
41
42         list_for_each_entry(ra_list, ra_list_head, list) {
43                 if (skb_queue_empty(&ra_list->skb_head))
44                         continue;
45
46                 skb_queue_walk_safe(&ra_list->skb_head, skb, tmp) {
47                         tx_info = MWIFIEX_SKB_TXCB(skb);
48                         if (tx_info->flags & MWIFIEX_BUF_FLAG_BRIDGED_PKT) {
49                                 __skb_unlink(skb, &ra_list->skb_head);
50                                 mwifiex_write_data_complete(adapter, skb, 0,
51                                                             -1);
52                                 atomic_dec(&priv->wmm.tx_pkts_queued);
53                                 pkt_deleted = true;
54                         }
55                         if ((atomic_read(&adapter->pending_bridged_pkts) <=
56                                              MWIFIEX_BRIDGED_PKTS_THR_LOW))
57                                 break;
58                 }
59         }
60
61         return pkt_deleted;
62 }
63
64 /* This function deletes packets from particular RA List. RA list index
65  * from which packets are deleted is preserved so that packets from next RA
66  * list are deleted upon subsequent call thus maintaining fairness.
67  */
68 static void mwifiex_uap_cleanup_tx_queues(struct mwifiex_private *priv)
69 {
70         unsigned long flags;
71         struct list_head *ra_list;
72         int i;
73
74         spin_lock_irqsave(&priv->wmm.ra_list_spinlock, flags);
75
76         for (i = 0; i < MAX_NUM_TID; i++, priv->del_list_idx++) {
77                 if (priv->del_list_idx == MAX_NUM_TID)
78                         priv->del_list_idx = 0;
79                 ra_list = &priv->wmm.tid_tbl_ptr[priv->del_list_idx].ra_list;
80                 if (mwifiex_uap_del_tx_pkts_in_ralist(priv, ra_list)) {
81                         priv->del_list_idx++;
82                         break;
83                 }
84         }
85
86         spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, flags);
87 }
88
89
90 static void mwifiex_uap_queue_bridged_pkt(struct mwifiex_private *priv,
91                                          struct sk_buff *skb)
92 {
93         struct mwifiex_adapter *adapter = priv->adapter;
94         struct uap_rxpd *uap_rx_pd;
95         struct rx_packet_hdr *rx_pkt_hdr;
96         struct sk_buff *new_skb;
97         struct mwifiex_txinfo *tx_info;
98         int hdr_chop;
99         struct timeval tv;
100         struct ethhdr *p_ethhdr;
101         u8 rfc1042_eth_hdr[ETH_ALEN] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
102
103         uap_rx_pd = (struct uap_rxpd *)(skb->data);
104         rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
105
106         if ((atomic_read(&adapter->pending_bridged_pkts) >=
107                                              MWIFIEX_BRIDGED_PKTS_THR_HIGH)) {
108                 dev_err(priv->adapter->dev,
109                         "Tx: Bridge packet limit reached. Drop packet!\n");
110                 kfree_skb(skb);
111                 mwifiex_uap_cleanup_tx_queues(priv);
112                 return;
113         }
114
115         if (!memcmp(&rx_pkt_hdr->rfc1042_hdr,
116                     rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr))) {
117                 /* Replace the 803 header and rfc1042 header (llc/snap) with
118                  * an Ethernet II header, keep the src/dst and snap_type
119                  * (ethertype).
120                  *
121                  * The firmware only passes up SNAP frames converting all RX
122                  * data from 802.11 to 802.2/LLC/SNAP frames.
123                  *
124                  * To create the Ethernet II, just move the src, dst address
125                  * right before the snap_type.
126                  */
127                 p_ethhdr = (struct ethhdr *)
128                         ((u8 *)(&rx_pkt_hdr->eth803_hdr)
129                          + sizeof(rx_pkt_hdr->eth803_hdr)
130                          + sizeof(rx_pkt_hdr->rfc1042_hdr)
131                          - sizeof(rx_pkt_hdr->eth803_hdr.h_dest)
132                          - sizeof(rx_pkt_hdr->eth803_hdr.h_source)
133                          - sizeof(rx_pkt_hdr->rfc1042_hdr.snap_type));
134                 memcpy(p_ethhdr->h_source, rx_pkt_hdr->eth803_hdr.h_source,
135                        sizeof(p_ethhdr->h_source));
136                 memcpy(p_ethhdr->h_dest, rx_pkt_hdr->eth803_hdr.h_dest,
137                        sizeof(p_ethhdr->h_dest));
138                 /* Chop off the rxpd + the excess memory from
139                  * 802.2/llc/snap header that was removed.
140                  */
141                 hdr_chop = (u8 *)p_ethhdr - (u8 *)uap_rx_pd;
142         } else {
143                 /* Chop off the rxpd */
144                 hdr_chop = (u8 *)&rx_pkt_hdr->eth803_hdr - (u8 *)uap_rx_pd;
145         }
146
147         /* Chop off the leading header bytes so the it points
148          * to the start of either the reconstructed EthII frame
149          * or the 802.2/llc/snap frame.
150          */
151         skb_pull(skb, hdr_chop);
152
153         if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
154                 dev_dbg(priv->adapter->dev,
155                         "data: Tx: insufficient skb headroom %d\n",
156                         skb_headroom(skb));
157                 /* Insufficient skb headroom - allocate a new skb */
158                 new_skb =
159                         skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
160                 if (unlikely(!new_skb)) {
161                         dev_err(priv->adapter->dev,
162                                 "Tx: cannot allocate new_skb\n");
163                         kfree_skb(skb);
164                         priv->stats.tx_dropped++;
165                         return;
166                 }
167
168                 kfree_skb(skb);
169                 skb = new_skb;
170                 dev_dbg(priv->adapter->dev, "info: new skb headroom %d\n",
171                         skb_headroom(skb));
172         }
173
174         tx_info = MWIFIEX_SKB_TXCB(skb);
175         tx_info->bss_num = priv->bss_num;
176         tx_info->bss_type = priv->bss_type;
177         tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT;
178
179         do_gettimeofday(&tv);
180         skb->tstamp = timeval_to_ktime(tv);
181         mwifiex_wmm_add_buf_txqueue(priv, skb);
182         atomic_inc(&adapter->tx_pending);
183         atomic_inc(&adapter->pending_bridged_pkts);
184
185         return;
186 }
187
188 /*
189  * This function contains logic for AP packet forwarding.
190  *
191  * If a packet is multicast/broadcast, it is sent to kernel/upper layer
192  * as well as queued back to AP TX queue so that it can be sent to other
193  * associated stations.
194  * If a packet is unicast and RA is present in associated station list,
195  * it is again requeued into AP TX queue.
196  * If a packet is unicast and RA is not in associated station list,
197  * packet is forwarded to kernel to handle routing logic.
198  */
199 int mwifiex_handle_uap_rx_forward(struct mwifiex_private *priv,
200                                   struct sk_buff *skb)
201 {
202         struct mwifiex_adapter *adapter = priv->adapter;
203         struct uap_rxpd *uap_rx_pd;
204         struct rx_packet_hdr *rx_pkt_hdr;
205         u8 ra[ETH_ALEN];
206         struct sk_buff *skb_uap;
207
208         uap_rx_pd = (struct uap_rxpd *)(skb->data);
209         rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
210
211         /* don't do packet forwarding in disconnected state */
212         if (!priv->media_connected) {
213                 dev_err(adapter->dev, "drop packet in disconnected state.\n");
214                 dev_kfree_skb_any(skb);
215                 return 0;
216         }
217
218         memcpy(ra, rx_pkt_hdr->eth803_hdr.h_dest, ETH_ALEN);
219
220         if (is_multicast_ether_addr(ra)) {
221                 skb_uap = skb_copy(skb, GFP_ATOMIC);
222                 mwifiex_uap_queue_bridged_pkt(priv, skb_uap);
223         } else {
224                 if (mwifiex_get_sta_entry(priv, ra)) {
225                         /* Requeue Intra-BSS packet */
226                         mwifiex_uap_queue_bridged_pkt(priv, skb);
227                         return 0;
228                 }
229         }
230
231         /* Forward unicat/Inter-BSS packets to kernel. */
232         return mwifiex_process_rx_packet(priv, skb);
233 }
234
235 /*
236  * This function processes the packet received on AP interface.
237  *
238  * The function looks into the RxPD and performs sanity tests on the
239  * received buffer to ensure its a valid packet before processing it
240  * further. If the packet is determined to be aggregated, it is
241  * de-aggregated accordingly. Then skb is passed to AP packet forwarding logic.
242  *
243  * The completion callback is called after processing is complete.
244  */
245 int mwifiex_process_uap_rx_packet(struct mwifiex_private *priv,
246                                   struct sk_buff *skb)
247 {
248         struct mwifiex_adapter *adapter = priv->adapter;
249         int ret;
250         struct uap_rxpd *uap_rx_pd;
251         struct rx_packet_hdr *rx_pkt_hdr;
252         u16 rx_pkt_type;
253         u8 ta[ETH_ALEN], pkt_type;
254         struct mwifiex_sta_node *node;
255
256         uap_rx_pd = (struct uap_rxpd *)(skb->data);
257         rx_pkt_type = le16_to_cpu(uap_rx_pd->rx_pkt_type);
258         rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
259
260         if ((le16_to_cpu(uap_rx_pd->rx_pkt_offset) +
261              le16_to_cpu(uap_rx_pd->rx_pkt_length)) > (u16) skb->len) {
262                 dev_err(adapter->dev,
263                         "wrong rx packet: len=%d, offset=%d, length=%d\n",
264                         skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset),
265                         le16_to_cpu(uap_rx_pd->rx_pkt_length));
266                 priv->stats.rx_dropped++;
267
268                 if (adapter->if_ops.data_complete)
269                         adapter->if_ops.data_complete(adapter, skb);
270                 else
271                         dev_kfree_skb_any(skb);
272
273                 return 0;
274         }
275
276         if (le16_to_cpu(uap_rx_pd->rx_pkt_type) == PKT_TYPE_AMSDU) {
277                 struct sk_buff_head list;
278                 struct sk_buff *rx_skb;
279
280                 __skb_queue_head_init(&list);
281                 skb_pull(skb, le16_to_cpu(uap_rx_pd->rx_pkt_offset));
282                 skb_trim(skb, le16_to_cpu(uap_rx_pd->rx_pkt_length));
283
284                 ieee80211_amsdu_to_8023s(skb, &list, priv->curr_addr,
285                                          priv->wdev->iftype, 0, false);
286
287                 while (!skb_queue_empty(&list)) {
288                         rx_skb = __skb_dequeue(&list);
289                         ret = mwifiex_recv_packet(priv, rx_skb);
290                         if (ret)
291                                 dev_err(adapter->dev,
292                                         "AP:Rx A-MSDU failed");
293                 }
294
295                 return 0;
296         } else if (rx_pkt_type == PKT_TYPE_MGMT) {
297                 ret = mwifiex_process_mgmt_packet(priv, skb);
298                 if (ret)
299                         dev_err(adapter->dev, "Rx of mgmt packet failed");
300                 dev_kfree_skb_any(skb);
301                 return ret;
302         }
303
304         memcpy(ta, rx_pkt_hdr->eth803_hdr.h_source, ETH_ALEN);
305
306         if (rx_pkt_type != PKT_TYPE_BAR && uap_rx_pd->priority < MAX_NUM_TID) {
307                 node = mwifiex_get_sta_entry(priv, ta);
308                 if (node)
309                         node->rx_seq[uap_rx_pd->priority] =
310                                                 le16_to_cpu(uap_rx_pd->seq_num);
311         }
312
313         if (!priv->ap_11n_enabled ||
314             (!mwifiex_11n_get_rx_reorder_tbl(priv, uap_rx_pd->priority, ta) &&
315             (le16_to_cpu(uap_rx_pd->rx_pkt_type) != PKT_TYPE_AMSDU))) {
316                 ret = mwifiex_handle_uap_rx_forward(priv, skb);
317                 return ret;
318         }
319
320         /* Reorder and send to kernel */
321         pkt_type = (u8)le16_to_cpu(uap_rx_pd->rx_pkt_type);
322         ret = mwifiex_11n_rx_reorder_pkt(priv, le16_to_cpu(uap_rx_pd->seq_num),
323                                          uap_rx_pd->priority, ta, pkt_type,
324                                          skb);
325
326         if (ret || (rx_pkt_type == PKT_TYPE_BAR)) {
327                 if (adapter->if_ops.data_complete)
328                         adapter->if_ops.data_complete(adapter, skb);
329                 else
330                         dev_kfree_skb_any(skb);
331         }
332
333         if (ret)
334                 priv->stats.rx_dropped++;
335
336         return ret;
337 }
338
339 /*
340  * This function fills the TxPD for AP tx packets.
341  *
342  * The Tx buffer received by this function should already have the
343  * header space allocated for TxPD.
344  *
345  * This function inserts the TxPD in between interface header and actual
346  * data and adjusts the buffer pointers accordingly.
347  *
348  * The following TxPD fields are set by this function, as required -
349  *      - BSS number
350  *      - Tx packet length and offset
351  *      - Priority
352  *      - Packet delay
353  *      - Priority specific Tx control
354  *      - Flags
355  */
356 void *mwifiex_process_uap_txpd(struct mwifiex_private *priv,
357                                struct sk_buff *skb)
358 {
359         struct mwifiex_adapter *adapter = priv->adapter;
360         struct uap_txpd *txpd;
361         struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb);
362         int pad, len;
363         u16 pkt_type;
364
365         if (!skb->len) {
366                 dev_err(adapter->dev, "Tx: bad packet length: %d\n", skb->len);
367                 tx_info->status_code = -1;
368                 return skb->data;
369         }
370
371         pkt_type = mwifiex_is_skb_mgmt_frame(skb) ? PKT_TYPE_MGMT : 0;
372
373         /* If skb->data is not aligned, add padding */
374         pad = (4 - (((void *)skb->data - NULL) & 0x3)) % 4;
375
376         len = sizeof(*txpd) + pad;
377
378         BUG_ON(skb_headroom(skb) < len + INTF_HEADER_LEN);
379
380         skb_push(skb, len);
381
382         txpd = (struct uap_txpd *)skb->data;
383         memset(txpd, 0, sizeof(*txpd));
384         txpd->bss_num = priv->bss_num;
385         txpd->bss_type = priv->bss_type;
386         txpd->tx_pkt_length = cpu_to_le16((u16)(skb->len - len));
387
388         txpd->priority = (u8)skb->priority;
389         txpd->pkt_delay_2ms = mwifiex_wmm_compute_drv_pkt_delay(priv, skb);
390
391         if (txpd->priority < ARRAY_SIZE(priv->wmm.user_pri_pkt_tx_ctrl))
392                 /*
393                  * Set the priority specific tx_control field, setting of 0 will
394                  * cause the default value to be used later in this function.
395                  */
396                 txpd->tx_control =
397                     cpu_to_le32(priv->wmm.user_pri_pkt_tx_ctrl[txpd->priority]);
398
399         /* Offset of actual data */
400         if (pkt_type == PKT_TYPE_MGMT) {
401                 /* Set the packet type and add header for management frame */
402                 txpd->tx_pkt_type = cpu_to_le16(pkt_type);
403                 len += MWIFIEX_MGMT_FRAME_HEADER_SIZE;
404         }
405
406         txpd->tx_pkt_offset = cpu_to_le16(len);
407
408         /* make space for INTF_HEADER_LEN */
409         skb_push(skb, INTF_HEADER_LEN);
410
411         if (!txpd->tx_control)
412                 /* TxCtrl set by user or default */
413                 txpd->tx_control = cpu_to_le32(priv->pkt_tx_ctrl);
414
415         return skb->data;
416 }