Merge remote-tracking branches 'asoc/topic/adau1977', 'asoc/topic/ak4642', 'asoc...
[sfrench/cifs-2.6.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
102         uap_rx_pd = (struct uap_rxpd *)(skb->data);
103         rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
104
105         if ((atomic_read(&adapter->pending_bridged_pkts) >=
106                                              MWIFIEX_BRIDGED_PKTS_THR_HIGH)) {
107                 dev_err(priv->adapter->dev,
108                         "Tx: Bridge packet limit reached. Drop packet!\n");
109                 kfree_skb(skb);
110                 mwifiex_uap_cleanup_tx_queues(priv);
111                 return;
112         }
113
114         if ((!memcmp(&rx_pkt_hdr->rfc1042_hdr, bridge_tunnel_header,
115                      sizeof(bridge_tunnel_header))) ||
116             (!memcmp(&rx_pkt_hdr->rfc1042_hdr, rfc1042_header,
117                      sizeof(rfc1042_header)) &&
118              ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_AARP &&
119              ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_IPX)) {
120                 /* Replace the 803 header and rfc1042 header (llc/snap) with
121                  * an Ethernet II header, keep the src/dst and snap_type
122                  * (ethertype).
123                  *
124                  * The firmware only passes up SNAP frames converting all RX
125                  * data from 802.11 to 802.2/LLC/SNAP frames.
126                  *
127                  * To create the Ethernet II, just move the src, dst address
128                  * right before the snap_type.
129                  */
130                 p_ethhdr = (struct ethhdr *)
131                         ((u8 *)(&rx_pkt_hdr->eth803_hdr)
132                          + sizeof(rx_pkt_hdr->eth803_hdr)
133                          + sizeof(rx_pkt_hdr->rfc1042_hdr)
134                          - sizeof(rx_pkt_hdr->eth803_hdr.h_dest)
135                          - sizeof(rx_pkt_hdr->eth803_hdr.h_source)
136                          - sizeof(rx_pkt_hdr->rfc1042_hdr.snap_type));
137                 memcpy(p_ethhdr->h_source, rx_pkt_hdr->eth803_hdr.h_source,
138                        sizeof(p_ethhdr->h_source));
139                 memcpy(p_ethhdr->h_dest, rx_pkt_hdr->eth803_hdr.h_dest,
140                        sizeof(p_ethhdr->h_dest));
141                 /* Chop off the rxpd + the excess memory from
142                  * 802.2/llc/snap header that was removed.
143                  */
144                 hdr_chop = (u8 *)p_ethhdr - (u8 *)uap_rx_pd;
145         } else {
146                 /* Chop off the rxpd */
147                 hdr_chop = (u8 *)&rx_pkt_hdr->eth803_hdr - (u8 *)uap_rx_pd;
148         }
149
150         /* Chop off the leading header bytes so that it points
151          * to the start of either the reconstructed EthII frame
152          * or the 802.2/llc/snap frame.
153          */
154         skb_pull(skb, hdr_chop);
155
156         if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
157                 dev_dbg(priv->adapter->dev,
158                         "data: Tx: insufficient skb headroom %d\n",
159                         skb_headroom(skb));
160                 /* Insufficient skb headroom - allocate a new skb */
161                 new_skb =
162                         skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
163                 if (unlikely(!new_skb)) {
164                         dev_err(priv->adapter->dev,
165                                 "Tx: cannot allocate new_skb\n");
166                         kfree_skb(skb);
167                         priv->stats.tx_dropped++;
168                         return;
169                 }
170
171                 kfree_skb(skb);
172                 skb = new_skb;
173                 dev_dbg(priv->adapter->dev, "info: new skb headroom %d\n",
174                         skb_headroom(skb));
175         }
176
177         tx_info = MWIFIEX_SKB_TXCB(skb);
178         memset(tx_info, 0, sizeof(*tx_info));
179         tx_info->bss_num = priv->bss_num;
180         tx_info->bss_type = priv->bss_type;
181         tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT;
182
183         if (is_unicast_ether_addr(rx_pkt_hdr->eth803_hdr.h_dest)) {
184                 /* Update bridge packet statistics as the
185                  * packet is not going to kernel/upper layer.
186                  */
187                 priv->stats.rx_bytes += skb->len;
188                 priv->stats.rx_packets++;
189
190                 /* Sending bridge packet to TX queue, so save the packet
191                  * length in TXCB to update statistics in TX complete.
192                  */
193                 tx_info->pkt_len = skb->len;
194         }
195
196         do_gettimeofday(&tv);
197         skb->tstamp = timeval_to_ktime(tv);
198         mwifiex_wmm_add_buf_txqueue(priv, skb);
199         atomic_inc(&adapter->tx_pending);
200         atomic_inc(&adapter->pending_bridged_pkts);
201
202         return;
203 }
204
205 /*
206  * This function contains logic for AP packet forwarding.
207  *
208  * If a packet is multicast/broadcast, it is sent to kernel/upper layer
209  * as well as queued back to AP TX queue so that it can be sent to other
210  * associated stations.
211  * If a packet is unicast and RA is present in associated station list,
212  * it is again requeued into AP TX queue.
213  * If a packet is unicast and RA is not in associated station list,
214  * packet is forwarded to kernel to handle routing logic.
215  */
216 int mwifiex_handle_uap_rx_forward(struct mwifiex_private *priv,
217                                   struct sk_buff *skb)
218 {
219         struct mwifiex_adapter *adapter = priv->adapter;
220         struct uap_rxpd *uap_rx_pd;
221         struct rx_packet_hdr *rx_pkt_hdr;
222         u8 ra[ETH_ALEN];
223         struct sk_buff *skb_uap;
224
225         uap_rx_pd = (struct uap_rxpd *)(skb->data);
226         rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
227
228         /* don't do packet forwarding in disconnected state */
229         if (!priv->media_connected) {
230                 dev_err(adapter->dev, "drop packet in disconnected state.\n");
231                 dev_kfree_skb_any(skb);
232                 return 0;
233         }
234
235         memcpy(ra, rx_pkt_hdr->eth803_hdr.h_dest, ETH_ALEN);
236
237         if (is_multicast_ether_addr(ra)) {
238                 skb_uap = skb_copy(skb, GFP_ATOMIC);
239                 mwifiex_uap_queue_bridged_pkt(priv, skb_uap);
240         } else {
241                 if (mwifiex_get_sta_entry(priv, ra)) {
242                         /* Requeue Intra-BSS packet */
243                         mwifiex_uap_queue_bridged_pkt(priv, skb);
244                         return 0;
245                 }
246         }
247
248         /* Forward unicat/Inter-BSS packets to kernel. */
249         return mwifiex_process_rx_packet(priv, skb);
250 }
251
252 /*
253  * This function processes the packet received on AP interface.
254  *
255  * The function looks into the RxPD and performs sanity tests on the
256  * received buffer to ensure its a valid packet before processing it
257  * further. If the packet is determined to be aggregated, it is
258  * de-aggregated accordingly. Then skb is passed to AP packet forwarding logic.
259  *
260  * The completion callback is called after processing is complete.
261  */
262 int mwifiex_process_uap_rx_packet(struct mwifiex_private *priv,
263                                   struct sk_buff *skb)
264 {
265         struct mwifiex_adapter *adapter = priv->adapter;
266         int ret;
267         struct uap_rxpd *uap_rx_pd;
268         struct rx_packet_hdr *rx_pkt_hdr;
269         u16 rx_pkt_type;
270         u8 ta[ETH_ALEN], pkt_type;
271         struct mwifiex_sta_node *node;
272
273         uap_rx_pd = (struct uap_rxpd *)(skb->data);
274         rx_pkt_type = le16_to_cpu(uap_rx_pd->rx_pkt_type);
275         rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
276
277         if ((le16_to_cpu(uap_rx_pd->rx_pkt_offset) +
278              le16_to_cpu(uap_rx_pd->rx_pkt_length)) > (u16) skb->len) {
279                 dev_err(adapter->dev,
280                         "wrong rx packet: len=%d, offset=%d, length=%d\n",
281                         skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset),
282                         le16_to_cpu(uap_rx_pd->rx_pkt_length));
283                 priv->stats.rx_dropped++;
284                 dev_kfree_skb_any(skb);
285                 return 0;
286         }
287
288         if (rx_pkt_type == PKT_TYPE_MGMT) {
289                 ret = mwifiex_process_mgmt_packet(priv, skb);
290                 if (ret)
291                         dev_err(adapter->dev, "Rx of mgmt packet failed");
292                 dev_kfree_skb_any(skb);
293                 return ret;
294         }
295
296         memcpy(ta, rx_pkt_hdr->eth803_hdr.h_source, ETH_ALEN);
297
298         if (rx_pkt_type != PKT_TYPE_BAR && uap_rx_pd->priority < MAX_NUM_TID) {
299                 node = mwifiex_get_sta_entry(priv, ta);
300                 if (node)
301                         node->rx_seq[uap_rx_pd->priority] =
302                                                 le16_to_cpu(uap_rx_pd->seq_num);
303         }
304
305         if (!priv->ap_11n_enabled ||
306             (!mwifiex_11n_get_rx_reorder_tbl(priv, uap_rx_pd->priority, ta) &&
307             (le16_to_cpu(uap_rx_pd->rx_pkt_type) != PKT_TYPE_AMSDU))) {
308                 ret = mwifiex_handle_uap_rx_forward(priv, skb);
309                 return ret;
310         }
311
312         /* Reorder and send to kernel */
313         pkt_type = (u8)le16_to_cpu(uap_rx_pd->rx_pkt_type);
314         ret = mwifiex_11n_rx_reorder_pkt(priv, le16_to_cpu(uap_rx_pd->seq_num),
315                                          uap_rx_pd->priority, ta, pkt_type,
316                                          skb);
317
318         if (ret || (rx_pkt_type == PKT_TYPE_BAR))
319                 dev_kfree_skb_any(skb);
320
321         if (ret)
322                 priv->stats.rx_dropped++;
323
324         return ret;
325 }
326
327 /*
328  * This function fills the TxPD for AP tx packets.
329  *
330  * The Tx buffer received by this function should already have the
331  * header space allocated for TxPD.
332  *
333  * This function inserts the TxPD in between interface header and actual
334  * data and adjusts the buffer pointers accordingly.
335  *
336  * The following TxPD fields are set by this function, as required -
337  *      - BSS number
338  *      - Tx packet length and offset
339  *      - Priority
340  *      - Packet delay
341  *      - Priority specific Tx control
342  *      - Flags
343  */
344 void *mwifiex_process_uap_txpd(struct mwifiex_private *priv,
345                                struct sk_buff *skb)
346 {
347         struct mwifiex_adapter *adapter = priv->adapter;
348         struct uap_txpd *txpd;
349         struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb);
350         int pad, len;
351         u16 pkt_type;
352
353         if (!skb->len) {
354                 dev_err(adapter->dev, "Tx: bad packet length: %d\n", skb->len);
355                 tx_info->status_code = -1;
356                 return skb->data;
357         }
358
359         pkt_type = mwifiex_is_skb_mgmt_frame(skb) ? PKT_TYPE_MGMT : 0;
360
361         /* If skb->data is not aligned, add padding */
362         pad = (4 - (((void *)skb->data - NULL) & 0x3)) % 4;
363
364         len = sizeof(*txpd) + pad;
365
366         BUG_ON(skb_headroom(skb) < len + INTF_HEADER_LEN);
367
368         skb_push(skb, len);
369
370         txpd = (struct uap_txpd *)skb->data;
371         memset(txpd, 0, sizeof(*txpd));
372         txpd->bss_num = priv->bss_num;
373         txpd->bss_type = priv->bss_type;
374         txpd->tx_pkt_length = cpu_to_le16((u16)(skb->len - len));
375
376         txpd->priority = (u8)skb->priority;
377         txpd->pkt_delay_2ms = mwifiex_wmm_compute_drv_pkt_delay(priv, skb);
378
379         if (txpd->priority < ARRAY_SIZE(priv->wmm.user_pri_pkt_tx_ctrl))
380                 /*
381                  * Set the priority specific tx_control field, setting of 0 will
382                  * cause the default value to be used later in this function.
383                  */
384                 txpd->tx_control =
385                     cpu_to_le32(priv->wmm.user_pri_pkt_tx_ctrl[txpd->priority]);
386
387         /* Offset of actual data */
388         if (pkt_type == PKT_TYPE_MGMT) {
389                 /* Set the packet type and add header for management frame */
390                 txpd->tx_pkt_type = cpu_to_le16(pkt_type);
391                 len += MWIFIEX_MGMT_FRAME_HEADER_SIZE;
392         }
393
394         txpd->tx_pkt_offset = cpu_to_le16(len);
395
396         /* make space for INTF_HEADER_LEN */
397         skb_push(skb, INTF_HEADER_LEN);
398
399         if (!txpd->tx_control)
400                 /* TxCtrl set by user or default */
401                 txpd->tx_control = cpu_to_le32(priv->pkt_tx_ctrl);
402
403         return skb->data;
404 }