staging: csr: remove unused define ALLOW_Q_PAUSE
[firefly-linux-kernel-4.4.55.git] / drivers / staging / csr / netdev.c
1 /*
2  * ---------------------------------------------------------------------------
3  * FILE:     netdev.c
4  *
5  * PURPOSE:
6  *      This file provides the upper edge interface to the linux netdevice
7  *      and wireless extensions.
8  *      It is part of the porting exercise.
9  *
10  * Copyright (C) 2005-2010 by Cambridge Silicon Radio Ltd.
11  *
12  * Refer to LICENSE.txt included with this source code for details on
13  * the license terms.
14  *
15  * ---------------------------------------------------------------------------
16  */
17
18 /*
19  * Porting Notes:
20  * This file implements the data plane of the UniFi linux driver.
21  *
22  * All the Tx packets are passed to the HIP core lib, using the
23  * unifi_send_signal() API. For EAPOL packets use the MLME-EAPOL.req
24  * signal, for all other use the MLME-UNITDATA.req. The unifi_send_signal()
25  * expects the wire-formatted (packed) signal. For convenience, in the OS
26  * layer we only use the native (unpacked) signal structures. The HIP core lib
27  * provides the write_pack() helper function to convert to the packed signal.
28  * The packet is stored in the bulk data of the signal. We do not need to
29  * allocate new memory to store the packet, because unifi_net_data_malloc()
30  * is implemented to return a skb, which is the format of packet in Linux.
31  * The HIP core lib frees the bulk data buffers, so we do not need to do
32  * this in the OS layer.
33  *
34  * All the Rx packets are MLME-UNITDATA.ind signals, passed by the HIP core lib
35  * in unifi_receive_event(). We do not need to allocate an skb and copy the
36  * received packet because the HIP core lib has stored in memory allocated by
37  * unifi_net_data_malloc(). Also, we can perform the 802.11 to Ethernet
38  * translation in-place because we allocate the extra memory allocated in
39  * unifi_net_data_malloc().
40  *
41  * If possible, the porting exercise should appropriately implement
42  * unifi_net_data_malloc() and unifi_net_data_free() to save copies between
43  * network and driver buffers.
44  */
45
46 #include <linux/types.h>
47 #include <linux/etherdevice.h>
48 #include <linux/mutex.h>
49 #include <linux/semaphore.h>
50 #include <linux/version.h>
51 #include <linux/vmalloc.h>
52 #include "csr_wifi_hip_unifi.h"
53 #include "csr_wifi_hip_conversions.h"
54 #include "unifi_priv.h"
55 #include <net/pkt_sched.h>
56
57
58 #ifdef UNIFI_NET_NAME
59 #define UF_ALLOC_NETDEV(_dev, _size, _name, _setup, _num_of_queues)     \
60     do {                                                                \
61         static char name[8];                                           \
62         sprintf(name, "%s%s", UNIFI_NET_NAME, _name);                   \
63         _dev = alloc_netdev_mq(_size, name, _setup, _num_of_queues);    \
64     } while (0);
65 #else
66 #define UF_ALLOC_NETDEV(_dev, _size, _name, _setup, _num_of_queues)     \
67     do {                                                                \
68         _dev = alloc_etherdev_mq(_size, _num_of_queues);                \
69     } while (0);
70 #endif /* UNIFI_NET_NAME */
71
72
73 /* Wext handler is suported only if CSR_SUPPORT_WEXT is defined */
74 #ifdef CSR_SUPPORT_WEXT
75 extern struct iw_handler_def unifi_iw_handler_def;
76 #endif /* CSR_SUPPORT_WEXT */
77 static void check_ba_frame_age_timeout( unifi_priv_t *priv,
78                                             netInterface_priv_t *interfacePriv,
79                                             ba_session_rx_struct *ba_session);
80 static void process_ba_frame(unifi_priv_t *priv,
81                              netInterface_priv_t *interfacePriv,
82                              ba_session_rx_struct *ba_session,
83                              frame_desc_struct *frame_desc);
84 static void process_ba_complete(unifi_priv_t *priv, netInterface_priv_t *interfacePriv);
85 static void process_ma_packet_error_ind(unifi_priv_t *priv, CSR_SIGNAL *signal, bulk_data_param_t *bulkdata);
86 static void process_amsdu(unifi_priv_t *priv, CSR_SIGNAL *signal, bulk_data_param_t *bulkdata);
87 static int uf_net_open(struct net_device *dev);
88 static int uf_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
89 static int uf_net_stop(struct net_device *dev);
90 static struct net_device_stats *uf_net_get_stats(struct net_device *dev);
91 static u16 uf_net_select_queue(struct net_device *dev, struct sk_buff *skb);
92 static netdev_tx_t uf_net_xmit(struct sk_buff *skb, struct net_device *dev);
93 static void uf_set_multicast_list(struct net_device *dev);
94
95
96 typedef int (*tx_signal_handler)(unifi_priv_t *priv, struct sk_buff *skb, const struct ethhdr *ehdr, CSR_PRIORITY priority);
97
98 #ifdef CONFIG_NET_SCHED
99 /*
100  * Queueing Discipline Interface
101  * Only used if kernel is configured with CONFIG_NET_SCHED
102  */
103
104 /*
105  * The driver uses the qdisc interface to buffer and control all
106  * outgoing traffic. We create a root qdisc, register our qdisc operations
107  * and later we create two subsiduary pfifo queues for the uncontrolled
108  * and controlled ports.
109  *
110  * The network stack delivers all outgoing packets in our enqueue handler.
111  * There, we classify the packet and decide whether to store it or drop it
112  * (if the controlled port state is set to "discard").
113  * If the packet is enqueued, the network stack call our dequeue handler.
114  * There, we decide whether we can send the packet, delay it or drop it
115  * (the controlled port configuration might have changed meanwhile).
116  * If a packet is dequeued, then the network stack calls our hard_start_xmit
117  * handler where finally we send the packet.
118  *
119  * If the hard_start_xmit handler fails to send the packet, we return
120  * NETDEV_TX_BUSY and the network stack call our requeue handler where
121  * we put the packet back in the same queue in came from.
122  *
123  */
124
125 struct uf_sched_data
126 {
127     /* Traffic Classifier TBD */
128     struct tcf_proto *filter_list;
129     /* Our two queues */
130     struct Qdisc *queues[UNIFI_TRAFFIC_Q_MAX];
131 };
132
133 struct uf_tx_packet_data {
134     /* Queue the packet is stored in */
135     unifi_TrafficQueue queue;
136     /* QoS Priority determined when enqueing packet */
137     CSR_PRIORITY priority;
138     /* Debug */
139     unsigned long host_tag;
140 };
141
142 #endif /* CONFIG_NET_SCHED */
143
144 static const struct net_device_ops uf_netdev_ops =
145 {
146     .ndo_open = uf_net_open,
147     .ndo_stop = uf_net_stop,
148     .ndo_start_xmit = uf_net_xmit,
149     .ndo_do_ioctl = uf_net_ioctl,
150     .ndo_get_stats = uf_net_get_stats, /* called by /proc/net/dev */
151     .ndo_set_rx_mode = uf_set_multicast_list,
152     .ndo_select_queue = uf_net_select_queue,
153 };
154
155 static u8 oui_rfc1042[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
156 static u8 oui_8021h[P80211_OUI_LEN]   = { 0x00, 0x00, 0xf8 };
157
158
159 /* Callback for event logging to blocking clients */
160 static void netdev_mlme_event_handler(ul_client_t  *client,
161                                       const u8 *sig_packed, int sig_len,
162                                       const bulk_data_param_t *bulkdata,
163                                       int dir);
164
165 #ifdef CSR_SUPPORT_WEXT
166 /* Declare netdev_notifier block which will contain the state change
167  * handler callback function
168  */
169 static struct notifier_block uf_netdev_notifier;
170 #endif
171
172 /*
173  * ---------------------------------------------------------------------------
174  *  uf_alloc_netdevice
175  *
176  *      Allocate memory for the net_device and device private structs
177  *      for this interface.
178  *      Fill in the fields, but don't register the interface yet.
179  *      We need to configure the UniFi first.
180  *
181  *  Arguments:
182  *      sdio_dev        Pointer to SDIO context handle to use for all
183  *                      SDIO ops.
184  *      bus_id          A small number indicating the SDIO card position on the
185  *                      bus. Typically this is the slot number, e.g. 0, 1 etc.
186  *                      Valid values are 0 to MAX_UNIFI_DEVS-1.
187  *
188  *  Returns:
189  *      Pointer to device private struct.
190  *
191  *  Notes:
192  *      The net_device and device private structs are allocated together
193  *      and should be freed by freeing the net_device pointer.
194  * ---------------------------------------------------------------------------
195  */
196 unifi_priv_t *
197 uf_alloc_netdevice(CsrSdioFunction *sdio_dev, int bus_id)
198 {
199     struct net_device *dev;
200     unifi_priv_t *priv;
201     netInterface_priv_t *interfacePriv;
202 #ifdef CSR_SUPPORT_WEXT
203     int rc;
204 #endif
205     unsigned char i; /* loop index */
206
207     /*
208      * Allocate netdevice struct, assign name template and
209      * setup as an ethernet device.
210      * The net_device and private structs are zeroed. Ether_setup() then
211      * sets up ethernet handlers and values.
212      * The RedHat 9 redhat-config-network tool doesn't recognise wlan* devices,
213      * so use "eth*" (like other wireless extns drivers).
214      */
215     UF_ALLOC_NETDEV(dev, sizeof(unifi_priv_t)+sizeof(netInterface_priv_t), "%d", ether_setup, UNIFI_TRAFFIC_Q_MAX);
216
217     if (dev == NULL) {
218         return NULL;
219     }
220
221     /* Set up back pointer from priv to netdev */
222     interfacePriv = (netInterface_priv_t *)netdev_priv(dev);
223     priv = (unifi_priv_t *)(interfacePriv + 1);
224     interfacePriv->privPtr = priv;
225     interfacePriv->InterfaceTag = 0;
226
227
228     /* Initialize all supported netdev interface to be NULL */
229     for(i=0; i<CSR_WIFI_NUM_INTERFACES; i++) {
230         priv->netdev[i] = NULL;
231         priv->interfacePriv[i] = NULL;
232     }
233     priv->netdev[0] = dev;
234     priv->interfacePriv[0] = interfacePriv;
235
236     /* Setup / override net_device fields */
237     dev->netdev_ops = &uf_netdev_ops;
238
239 #ifdef CSR_SUPPORT_WEXT
240     dev->wireless_handlers = &unifi_iw_handler_def;
241 #if IW_HANDLER_VERSION < 6
242     dev->get_wireless_stats = unifi_get_wireless_stats;
243 #endif /* IW_HANDLER_VERSION */
244 #endif /* CSR_SUPPORT_WEXT */
245
246     /* This gives us enough headroom to add the 802.11 header */
247     dev->needed_headroom = 32;
248
249     /* Use bus_id as instance number */
250     priv->instance = bus_id;
251     /* Store SDIO pointer to pass in the core */
252     priv->sdio = sdio_dev;
253
254     sdio_dev->driverData = (void*)priv;
255     /* Consider UniFi to be uninitialised */
256     priv->init_progress = UNIFI_INIT_NONE;
257
258     priv->prev_queue = 0;
259
260     /*
261      * Initialise the clients structure array.
262      * We do not need protection around ul_init_clients() because
263      * the character device can not be used until uf_alloc_netdevice()
264      * returns and Unifi_instances[bus_id]=priv is set, since unifi_open()
265      * will return -ENODEV.
266      */
267     ul_init_clients(priv);
268
269     /*
270      * Register a new ul client to send the multicast list signals.
271      * Note: priv->instance must be set before calling this.
272      */
273     priv->netdev_client = ul_register_client(priv,
274             0,
275             netdev_mlme_event_handler);
276     if (priv->netdev_client == NULL) {
277         unifi_error(priv,
278                 "Failed to register a unifi client for background netdev processing\n");
279         free_netdev(priv->netdev[0]);
280         return NULL;
281     }
282     unifi_trace(priv, UDBG2, "Netdev %p client (id:%d s:0x%X) is registered\n",
283             dev, priv->netdev_client->client_id, priv->netdev_client->sender_id);
284
285     priv->sta_wmm_capabilities = 0;
286
287 #if (defined(CSR_WIFI_SECURITY_WAPI_ENABLE) && defined(CSR_SUPPORT_SME))
288     priv->wapi_multicast_filter = 0;
289     priv->wapi_unicast_filter = 0;
290     priv->wapi_unicast_queued_pkt_filter = 0;
291 #ifdef CSR_WIFI_SECURITY_WAPI_QOSCTRL_MIC_WORKAROUND
292     priv->isWapiConnection = FALSE;
293 #endif
294 #endif
295
296     /* Enable all queues by default */
297     interfacePriv->queueEnabled[0] = 1;
298     interfacePriv->queueEnabled[1] = 1;
299     interfacePriv->queueEnabled[2] = 1;
300     interfacePriv->queueEnabled[3] = 1;
301
302 #ifdef CSR_SUPPORT_SME
303     priv->allPeerDozing = 0;
304 #endif
305     /*
306      * Initialise the OS private struct.
307      */
308     /*
309      * Instead of deciding in advance to use 11bg or 11a, we could do a more
310      * clever scan on both radios.
311      */
312     if (use_5g) {
313         priv->if_index = CSR_INDEX_5G;
314         unifi_info(priv, "Using the 802.11a radio\n");
315     } else {
316         priv->if_index = CSR_INDEX_2G4;
317     }
318
319     /* Initialise bh thread structure */
320     priv->bh_thread.thread_task = NULL;
321     priv->bh_thread.block_thread = 1;
322     init_waitqueue_head(&priv->bh_thread.wakeup_q);
323     priv->bh_thread.wakeup_flag = 0;
324     sprintf(priv->bh_thread.name, "uf_bh_thread");
325
326     /* reset the connected state for the interface */
327     interfacePriv->connected = UnifiConnectedUnknown;  /* -1 unknown, 0 no, 1 yes */
328
329 #ifdef USE_DRIVER_LOCK
330     sema_init(&priv->lock, 1);
331 #endif /* USE_DRIVER_LOCK */
332
333     spin_lock_init(&priv->send_signal_lock);
334
335     spin_lock_init(&priv->m4_lock);
336     sema_init(&priv->ba_mutex, 1);
337
338 #if (defined(CSR_WIFI_SECURITY_WAPI_ENABLE) && defined(CSR_WIFI_SECURITY_WAPI_SW_ENCRYPTION))
339     spin_lock_init(&priv->wapi_lock);
340 #endif
341
342 #ifdef CSR_SUPPORT_SME
343     spin_lock_init(&priv->staRecord_lock);
344     spin_lock_init(&priv->tx_q_lock);
345 #endif
346
347     /* Create the Traffic Analysis workqueue */
348     priv->unifi_workqueue = create_singlethread_workqueue("unifi_workq");
349     if (priv->unifi_workqueue == NULL) {
350         /* Deregister priv->netdev_client */
351         ul_deregister_client(priv->netdev_client);
352         free_netdev(priv->netdev[0]);
353         return NULL;
354     }
355
356 #ifdef CSR_SUPPORT_SME
357     /* Create the Multicast Addresses list work structure */
358     INIT_WORK(&priv->multicast_list_task, uf_multicast_list_wq);
359
360     /* Create m4 buffering work structure */
361     INIT_WORK(&interfacePriv->send_m4_ready_task, uf_send_m4_ready_wq);
362
363 #if (defined(CSR_WIFI_SECURITY_WAPI_ENABLE) && defined(CSR_WIFI_SECURITY_WAPI_SW_ENCRYPTION))
364     /* Create work structure to buffer the WAPI data packets to be sent to SME for encryption */
365     INIT_WORK(&interfacePriv->send_pkt_to_encrypt, uf_send_pkt_to_encrypt);
366 #endif
367 #endif
368
369     priv->ref_count = 1;
370
371     priv->amp_client = NULL;
372     priv->coredump_mode = 0;
373     priv->ptest_mode = 0;
374     priv->wol_suspend = FALSE;
375     INIT_LIST_HEAD(&interfacePriv->rx_uncontrolled_list);
376     INIT_LIST_HEAD(&interfacePriv->rx_controlled_list);
377     sema_init(&priv->rx_q_sem, 1);
378
379 #ifdef CSR_SUPPORT_WEXT
380     interfacePriv->netdev_callback_registered = FALSE;
381     interfacePriv->wait_netdev_change = FALSE;
382     /* Register callback for netdevice state changes */
383     if ((rc = register_netdevice_notifier(&uf_netdev_notifier)) == 0) {
384         interfacePriv->netdev_callback_registered = TRUE;
385     }
386     else {
387         unifi_warning(priv, "Failed to register netdevice notifier : %d %p\n", rc, dev);
388     }
389 #endif /* CSR_SUPPORT_WEXT */
390
391 #ifdef CSR_WIFI_SPLIT_PATCH
392     /* set it to some invalid value */
393     priv->pending_mode_set.common.destination = 0xaaaa;
394 #endif
395
396     return priv;
397 } /* uf_alloc_netdevice() */
398
399 /*
400  *---------------------------------------------------------------------------
401  *  uf_alloc_netdevice_for_other_interfaces
402  *
403  *      Allocate memory for the net_device and device private structs
404  *      for this interface.
405  *      Fill in the fields, but don't register the interface yet.
406  *      We need to configure the UniFi first.
407  *
408  *  Arguments:
409  *      interfaceTag   Interface number.
410  *      sdio_dev        Pointer to SDIO context handle to use for all
411  *                      SDIO ops.
412  *      bus_id          A small number indicating the SDIO card position on the
413  *                      bus. Typically this is the slot number, e.g. 0, 1 etc.
414  *                      Valid values are 0 to MAX_UNIFI_DEVS-1.
415  *
416  *  Returns:
417  *      Pointer to device private struct.
418  *
419  *  Notes:
420  *      The device private structure contains the interfaceTag and pointer to the unifi_priv
421  *      structure created allocated by net_device od interface0.
422  *      The net_device and device private structs are allocated together
423  *      and should be freed by freeing the net_device pointer.
424  * ---------------------------------------------------------------------------
425  */
426 u8
427 uf_alloc_netdevice_for_other_interfaces(unifi_priv_t *priv, u16 interfaceTag)
428 {
429     struct net_device *dev;
430     netInterface_priv_t *interfacePriv;
431
432     /*
433      * Allocate netdevice struct, assign name template and
434      * setup as an ethernet device.
435      * The net_device and private structs are zeroed. Ether_setup() then
436      * sets up ethernet handlers and values.
437      * The RedHat 9 redhat-config-network tool doesn't recognise wlan* devices,
438      * so use "eth*" (like other wireless extns drivers).
439      */
440     UF_ALLOC_NETDEV(dev, sizeof(netInterface_priv_t), "%d", ether_setup, 1);
441     if (dev == NULL) {
442         return FALSE;
443     }
444
445     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
446         unifi_error(priv, "uf_alloc_netdevice_for_other_interfaces bad interfaceTag\n");
447         return FALSE;
448     }
449
450     /* Set up back pointer from priv to netdev */
451     interfacePriv = (netInterface_priv_t *)netdev_priv(dev);
452     interfacePriv->privPtr = priv;
453     interfacePriv->InterfaceTag = interfaceTag;
454     priv->netdev[interfaceTag] = dev;
455     priv->interfacePriv[interfacePriv->InterfaceTag] = interfacePriv;
456
457     /* reset the connected state for the interface */
458     interfacePriv->connected = UnifiConnectedUnknown;  /* -1 unknown, 0 no, 1 yes */
459     INIT_LIST_HEAD(&interfacePriv->rx_uncontrolled_list);
460     INIT_LIST_HEAD(&interfacePriv->rx_controlled_list);
461
462     /* Setup / override net_device fields */
463     dev->netdev_ops = &uf_netdev_ops;
464
465 #ifdef CSR_SUPPORT_WEXT
466     dev->wireless_handlers = &unifi_iw_handler_def;
467 #if IW_HANDLER_VERSION < 6
468     dev->get_wireless_stats = unifi_get_wireless_stats;
469 #endif /* IW_HANDLER_VERSION */
470 #endif /* CSR_SUPPORT_WEXT */
471     return TRUE;
472 } /* uf_alloc_netdevice() */
473
474
475
476 /*
477  * ---------------------------------------------------------------------------
478  *  uf_free_netdevice
479  *
480  *      Unregister the network device and free the memory allocated for it.
481  *      NB This includes the memory for the priv struct.
482  *
483  *  Arguments:
484  *      priv            Device private pointer.
485  *
486  *  Returns:
487  *      None.
488  * ---------------------------------------------------------------------------
489  */
490 int
491 uf_free_netdevice(unifi_priv_t *priv)
492 {
493     int i;
494     unsigned long flags;
495
496     func_enter();
497
498     unifi_trace(priv, UDBG1, "uf_free_netdevice\n");
499
500     if (!priv) {
501         return -EINVAL;
502     }
503
504     /*
505      * Free any buffers used for holding firmware
506      */
507     uf_release_firmware_files(priv);
508
509 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
510     if (priv->connection_config.mlmeAssociateReqInformationElements) {
511         kfree(priv->connection_config.mlmeAssociateReqInformationElements);
512     }
513     priv->connection_config.mlmeAssociateReqInformationElements = NULL;
514     priv->connection_config.mlmeAssociateReqInformationElementsLength = 0;
515
516     if (priv->mib_data.length) {
517         vfree(priv->mib_data.data);
518     }
519     priv->mib_data.data = NULL;
520     priv->mib_data.length = 0;
521
522 #endif /* CSR_SUPPORT_SME && CSR_SUPPORT_WEXT*/
523
524     /* Free any bulkdata buffers allocated for M4 caching */
525     spin_lock_irqsave(&priv->m4_lock, flags);
526     for (i = 0; i < CSR_WIFI_NUM_INTERFACES; i++) {
527         netInterface_priv_t *interfacePriv = priv->interfacePriv[i];
528         if (interfacePriv->m4_bulk_data.data_length > 0) {
529             unifi_trace(priv, UDBG5, "uf_free_netdevice: free M4 bulkdata %d\n", i);
530             unifi_net_data_free(priv, &interfacePriv->m4_bulk_data);
531         }
532     }
533     spin_unlock_irqrestore(&priv->m4_lock, flags);
534
535 #if (defined(CSR_WIFI_SECURITY_WAPI_ENABLE) && defined(CSR_WIFI_SECURITY_WAPI_SW_ENCRYPTION))
536     /* Free any bulkdata buffers allocated for M4 caching */
537     spin_lock_irqsave(&priv->wapi_lock, flags);
538     for (i = 0; i < CSR_WIFI_NUM_INTERFACES; i++) {
539         netInterface_priv_t *interfacePriv = priv->interfacePriv[i];
540         if (interfacePriv->wapi_unicast_bulk_data.data_length > 0) {
541             unifi_trace(priv, UDBG5, "uf_free_netdevice: free WAPI PKT bulk data %d\n", i);
542             unifi_net_data_free(priv, &interfacePriv->wapi_unicast_bulk_data);
543         }
544     }
545     spin_unlock_irqrestore(&priv->wapi_lock, flags);
546 #endif
547
548 #ifdef CSR_SUPPORT_WEXT
549     /* Unregister callback for netdevice state changes */
550     unregister_netdevice_notifier(&uf_netdev_notifier);
551 #endif /* CSR_SUPPORT_WEXT */
552
553 #ifdef CSR_SUPPORT_SME
554     /* Cancel work items and destroy the workqueue */
555     cancel_work_sync(&priv->multicast_list_task);
556 #endif
557 /* Destroy the workqueues. */
558     flush_workqueue(priv->unifi_workqueue);
559     destroy_workqueue(priv->unifi_workqueue);
560
561     /* Free up netdev in reverse order: priv is allocated with netdev[0].
562      * So, netdev[0] should be freed after all other netdevs are freed up
563      */
564     for (i=CSR_WIFI_NUM_INTERFACES-1; i>=0; i--) {
565         /*Free the netdev struct and priv, which are all one lump*/
566         if (priv->netdev[i]) {
567             unifi_error(priv, "uf_free_netdevice: netdev %d %p\n", i, priv->netdev[i]);
568             free_netdev(priv->netdev[i]);
569         }
570     }
571
572     func_exit();
573     return 0;
574 } /* uf_free_netdevice() */
575
576
577 /*
578  * ---------------------------------------------------------------------------
579  *  uf_net_open
580  *
581  *      Called when userland does "ifconfig wlan0 up".
582  *
583  *  Arguments:
584  *      dev             Device pointer.
585  *
586  *  Returns:
587  *      None.
588  * ---------------------------------------------------------------------------
589  */
590 static int
591 uf_net_open(struct net_device *dev)
592 {
593     netInterface_priv_t *interfacePriv = (netInterface_priv_t *)netdev_priv(dev);
594     unifi_priv_t *priv = interfacePriv->privPtr;
595
596     func_enter();
597
598     /* If we haven't finished UniFi initialisation, we can't start */
599     if (priv->init_progress != UNIFI_INIT_COMPLETED) {
600         unifi_warning(priv, "%s: unifi not ready, failing net_open\n", __FUNCTION__);
601         return -EINVAL;
602     }
603
604 #if (defined CSR_NATIVE_LINUX) && (defined UNIFI_SNIFF_ARPHRD) && defined(CSR_SUPPORT_WEXT)
605     /*
606      * To sniff, the user must do "iwconfig mode monitor", which sets
607      * priv->wext_conf.mode to IW_MODE_MONITOR.
608      * Then he/she must do "ifconfig ethn up", which calls this fn.
609      * There is no point in starting the sniff with SNIFFJOIN until
610      * this point.
611      */
612     if (priv->wext_conf.mode == IW_MODE_MONITOR) {
613         int err;
614         err = uf_start_sniff(priv);
615         if (err) {
616             return err;
617         }
618         netif_carrier_on(dev);
619     }
620 #endif
621
622 #ifdef CSR_SUPPORT_WEXT
623     if (interfacePriv->wait_netdev_change) {
624         unifi_trace(priv, UDBG1, "%s: Waiting for NETDEV_CHANGE, assume connected\n",
625                     __FUNCTION__);
626         interfacePriv->connected = UnifiConnected;
627         interfacePriv->wait_netdev_change = FALSE;
628     }
629 #endif
630
631     UF_NETIF_TX_START_ALL_QUEUES(dev);
632
633     func_exit();
634     return 0;
635 } /* uf_net_open() */
636
637
638 static int
639 uf_net_stop(struct net_device *dev)
640 {
641 #if defined(CSR_NATIVE_LINUX) && defined(UNIFI_SNIFF_ARPHRD) && defined(CSR_SUPPORT_WEXT)
642     netInterface_priv_t *interfacePriv = (netInterface_priv_t*)netdev_priv(dev);
643     unifi_priv_t *priv = interfacePriv->privPtr;
644
645     func_enter();
646
647     /* Stop sniffing if in Monitor mode */
648     if (priv->wext_conf.mode == IW_MODE_MONITOR) {
649         if (priv->card) {
650             int err;
651             err = unifi_reset_state(priv, dev->dev_addr, 1);
652             if (err) {
653                 return err;
654             }
655         }
656     }
657 #else
658     func_enter();
659 #endif
660
661     UF_NETIF_TX_STOP_ALL_QUEUES(dev);
662
663     func_exit();
664     return 0;
665 } /* uf_net_stop() */
666
667
668 /* This is called after the WE handlers */
669 static int
670 uf_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
671 {
672     int rc;
673
674     rc = -EOPNOTSUPP;
675
676     return rc;
677 } /* uf_net_ioctl() */
678
679
680
681 static struct net_device_stats *
682 uf_net_get_stats(struct net_device *dev)
683 {
684     netInterface_priv_t *interfacePriv = (netInterface_priv_t *)netdev_priv(dev);
685
686     return &interfacePriv->stats;
687 } /* uf_net_get_stats() */
688
689 static CSR_PRIORITY uf_get_packet_priority(unifi_priv_t *priv, netInterface_priv_t *interfacePriv, struct sk_buff *skb, const int proto)
690 {
691     CSR_PRIORITY priority = CSR_CONTENTION;
692
693     func_enter();
694     priority = (CSR_PRIORITY) (skb->priority >> 5);
695
696     if (priority == CSR_QOS_UP0) { /* 0 */
697
698         unifi_trace(priv, UDBG5, "uf_get_packet_priority: proto = 0x%.4X\n", proto);
699
700         switch (proto) {
701             case 0x0800:        /* IPv4 */
702             case 0x814C:        /* SNMP */
703             case 0x880C:        /* GSMP */
704                 priority = (CSR_PRIORITY) (skb->data[1 + ETH_HLEN] >> 5);
705                 break;
706
707             case 0x8100:        /* VLAN */
708                 priority = (CSR_PRIORITY) (skb->data[0 + ETH_HLEN] >> 5);
709                 break;
710
711             case 0x86DD:        /* IPv6 */
712                 priority = (CSR_PRIORITY) ((skb->data[0 + ETH_HLEN] & 0x0E) >> 1);
713                 break;
714
715             default:
716                 priority = CSR_QOS_UP0;
717                 break;
718         }
719     }
720
721     /* Check if we are allowed to transmit on this AC. Because of ACM we may have to downgrade to a lower
722      * priority */
723     if (interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_STA ||
724         interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PCLI) {
725         unifi_TrafficQueue queue;
726
727         /* Keep trying lower priorities until we find a queue
728          * Priority to queue mapping is 1,2 - BK, 0,3 - BE, 4,5 - VI, 6,7 - VO */
729         queue = unifi_frame_priority_to_queue(priority);
730
731         while (queue > UNIFI_TRAFFIC_Q_BK && !interfacePriv->queueEnabled[queue]) {
732             queue--;
733             priority = unifi_get_default_downgrade_priority(queue);
734         }
735     }
736
737     unifi_trace(priv, UDBG5, "Packet priority = %d\n", priority);
738
739     func_exit();
740     return priority;
741 }
742
743 /*
744  */
745 /*
746  * ---------------------------------------------------------------------------
747  *  get_packet_priority
748  *
749  *  Arguments:
750  *      priv             private data area of functional driver
751  *      skb              socket buffer
752  *      ehdr             ethernet header to fetch protocol
753  *      interfacePriv    For accessing station record database
754  *
755  *
756  *  Returns:
757  *      CSR_PRIORITY.
758  * ---------------------------------------------------------------------------
759  */
760 CSR_PRIORITY
761 get_packet_priority(unifi_priv_t *priv, struct sk_buff *skb, const struct ethhdr *ehdr, netInterface_priv_t *interfacePriv)
762 {
763     CSR_PRIORITY priority = CSR_CONTENTION;
764     const int proto = ntohs(ehdr->h_proto);
765
766     u8 interfaceMode = interfacePriv->interfaceMode;
767
768     func_enter();
769
770     /* Priority Mapping for all the Modes */
771     switch(interfaceMode)
772     {
773         case CSR_WIFI_ROUTER_CTRL_MODE_STA:
774         case CSR_WIFI_ROUTER_CTRL_MODE_P2PCLI:
775             unifi_trace(priv, UDBG4, "mode is STA \n");
776             if ((priv->sta_wmm_capabilities & QOS_CAPABILITY_WMM_ENABLED) == 1) {
777                 priority = uf_get_packet_priority(priv, interfacePriv, skb, proto);
778             } else {
779                 priority = CSR_CONTENTION;
780             }
781             break;
782 #ifdef CSR_SUPPORT_SME
783         case CSR_WIFI_ROUTER_CTRL_MODE_AP:
784         case CSR_WIFI_ROUTER_CTRL_MODE_P2PGO:
785         case CSR_WIFI_ROUTER_CTRL_MODE_IBSS:
786             {
787                 CsrWifiRouterCtrlStaInfo_t * dstStaInfo =
788                     CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv,ehdr->h_dest, interfacePriv->InterfaceTag);
789                 unifi_trace(priv, UDBG4, "mode is AP \n");
790                 if (!(ehdr->h_dest[0] & 0x01) && dstStaInfo && dstStaInfo->wmmOrQosEnabled) {
791                     /* If packet is not Broadcast/multicast */
792                     priority = uf_get_packet_priority(priv, interfacePriv, skb, proto);
793                 } else {
794                     /* Since packet destination is not QSTA, set priority to CSR_CONTENTION */
795                     unifi_trace(priv, UDBG4, "Destination is not QSTA or BroadCast/Multicast\n");
796                     priority = CSR_CONTENTION;
797                 }
798             }
799             break;
800 #endif
801         default:
802             unifi_trace(priv, UDBG3, " mode unknown in %s func, mode=%x\n", __FUNCTION__, interfaceMode);
803     }
804     unifi_trace(priv, UDBG5, "priority = %x\n", priority);
805
806     func_exit();
807     return priority;
808 }
809
810 /*
811  * ---------------------------------------------------------------------------
812  *  uf_net_select_queue
813  *
814  *      Called by the kernel to select which queue to put the packet in
815  *
816  *  Arguments:
817  *      dev             Device pointer
818  *      skb             Packet
819  *
820  *  Returns:
821  *      Queue index
822  * ---------------------------------------------------------------------------
823  */
824 static u16
825 uf_net_select_queue(struct net_device *dev, struct sk_buff *skb)
826 {
827     netInterface_priv_t *interfacePriv = (netInterface_priv_t *)netdev_priv(dev);
828     unifi_priv_t *priv = (unifi_priv_t *)interfacePriv->privPtr;
829     struct ethhdr ehdr;
830     unifi_TrafficQueue queue;
831     int proto;
832     CSR_PRIORITY priority;
833
834     func_enter();
835
836     memcpy(&ehdr, skb->data, ETH_HLEN);
837     proto = ntohs(ehdr.h_proto);
838
839     /* 802.1x - apply controlled/uncontrolled port rules */
840     if ((proto != ETH_P_PAE)
841 #ifdef CSR_WIFI_SECURITY_WAPI_ENABLE
842             && (proto != ETH_P_WAI)
843 #endif
844        ) {
845         /* queues 0 - 3 */
846         priority = get_packet_priority(priv, skb, &ehdr, interfacePriv);
847         queue = unifi_frame_priority_to_queue(priority);
848     } else {
849         /* queue 4 */
850         queue = UNIFI_TRAFFIC_Q_EAPOL;
851     }
852
853
854     func_exit();
855     return (u16)queue;
856 } /* uf_net_select_queue() */
857
858 int
859 skb_add_llc_snap(struct net_device *dev, struct sk_buff *skb, int proto)
860 {
861     llc_snap_hdr_t *snap;
862     netInterface_priv_t *interfacePriv = (netInterface_priv_t *)netdev_priv(dev);
863     unifi_priv_t *priv = interfacePriv->privPtr;
864     int headroom;
865
866     /* get the headroom available in skb */
867     headroom = skb_headroom(skb);
868     /* step 1: classify ether frame, DIX or 802.3? */
869
870     if (proto < 0x600) {
871         /* codes <= 1500 reserved for 802.3 lengths */
872         /* it's 802.3, pass ether payload unchanged,  */
873         unifi_trace(priv, UDBG3, "802.3 len: %d\n", skb->len);
874
875         /*   leave off any PAD octets.  */
876         skb_trim(skb, proto);
877     } else if (proto == ETH_P_8021Q) {
878
879         /* Store the VLAN SNAP (should be 87-65). */
880         u16 vlan_snap = *(u16*)skb->data;
881         /* check for headroom availability before skb_push 14 = (4 + 10) */
882         if (headroom < 14) {
883             unifi_trace(priv, UDBG3, "cant append vlan snap: debug\n");
884             return -1;
885         }
886         /* Add AA-AA-03-00-00-00 */
887         snap = (llc_snap_hdr_t *)skb_push(skb, 4);
888         snap->dsap = snap->ssap = 0xAA;
889         snap->ctrl = 0x03;
890         memcpy(snap->oui, oui_rfc1042, P80211_OUI_LEN);
891
892         /* Add AA-AA-03-00-00-00 */
893         snap = (llc_snap_hdr_t *)skb_push(skb, 10);
894         snap->dsap = snap->ssap = 0xAA;
895         snap->ctrl = 0x03;
896         memcpy(snap->oui, oui_rfc1042, P80211_OUI_LEN);
897
898         /* Add the VLAN specific information */
899         snap->protocol = htons(proto);
900         *(u16*)(snap + 1) = vlan_snap;
901
902     } else
903     {
904         /* it's DIXII, time for some conversion */
905         unifi_trace(priv, UDBG3, "DIXII len: %d\n", skb->len);
906
907         /* check for headroom availability before skb_push */
908         if (headroom < sizeof(llc_snap_hdr_t)) {
909             unifi_trace(priv, UDBG3, "cant append snap: debug\n");
910             return -1;
911         }
912         /* tack on SNAP */
913         snap = (llc_snap_hdr_t *)skb_push(skb, sizeof(llc_snap_hdr_t));
914         snap->dsap = snap->ssap = 0xAA;
915         snap->ctrl = 0x03;
916         /* Use the appropriate OUI. */
917         if ((proto == ETH_P_AARP) || (proto == ETH_P_IPX)) {
918             memcpy(snap->oui, oui_8021h, P80211_OUI_LEN);
919         } else {
920             memcpy(snap->oui, oui_rfc1042, P80211_OUI_LEN);
921         }
922         snap->protocol = htons(proto);
923     }
924
925     return 0;
926 } /* skb_add_llc_snap() */
927
928 #ifdef CSR_SUPPORT_SME
929 static int
930 _identify_sme_ma_pkt_ind(unifi_priv_t *priv,
931                          const s8 *oui, u16 protocol,
932                          const CSR_SIGNAL *signal,
933                          bulk_data_param_t *bulkdata,
934                          const unsigned char *daddr,
935                          const unsigned char *saddr)
936 {
937     CSR_MA_PACKET_INDICATION *pkt_ind = (CSR_MA_PACKET_INDICATION*)&signal->u.MaPacketIndication;
938     int r;
939     u8 i;
940
941     unifi_trace(priv, UDBG5,
942             "_identify_sme_ma_pkt_ind -->\n");
943     for (i = 0; i < MAX_MA_UNIDATA_IND_FILTERS; i++) {
944         if (priv->sme_unidata_ind_filters[i].in_use) {
945             if (!memcmp(oui, priv->sme_unidata_ind_filters[i].oui, 3) &&
946                     (protocol == priv->sme_unidata_ind_filters[i].protocol)) {
947
948                 /* Send to client */
949                 if (priv->sme_cli) {
950                     /*
951                      * Pass the packet to the SME, using unifi_sys_ma_unitdata_ind().
952                      * The frame needs to be converted according to the encapsulation.
953                      */
954                     unifi_trace(priv, UDBG1,
955                             "_identify_sme_ma_pkt_ind: handle=%d, encap=%d, proto=%x\n",
956                             i, priv->sme_unidata_ind_filters[i].encapsulation,
957                             priv->sme_unidata_ind_filters[i].protocol);
958                     if (priv->sme_unidata_ind_filters[i].encapsulation == CSR_WIFI_ROUTER_ENCAPSULATION_ETHERNET) {
959                         struct sk_buff *skb;
960                         /* The translation is performed on skb... */
961                         skb = (struct sk_buff*)bulkdata->d[0].os_net_buf_ptr;
962                         skb->len = bulkdata->d[0].data_length;
963
964                         unifi_trace(priv, UDBG1,
965                                 "_identify_sme_ma_pkt_ind: skb_80211_to_ether -->\n");
966                         r = skb_80211_to_ether(priv, skb, daddr, saddr,
967                                 signal, bulkdata);
968                         unifi_trace(priv, UDBG1,
969                                 "_identify_sme_ma_pkt_ind: skb_80211_to_ether <--\n");
970                         if (r) {
971                             return -EINVAL;
972                         }
973
974                         /* ... but we indicate buffer and length */
975                         bulkdata->d[0].os_data_ptr = skb->data;
976                         bulkdata->d[0].data_length = skb->len;
977                     } else {
978                         /* Add the MAC addresses before the SNAP */
979                         bulkdata->d[0].os_data_ptr -= 2*ETH_ALEN;
980                         bulkdata->d[0].data_length += 2*ETH_ALEN;
981                         memcpy((void*)bulkdata->d[0].os_data_ptr, daddr, ETH_ALEN);
982                         memcpy((void*)bulkdata->d[0].os_data_ptr + ETH_ALEN, saddr, ETH_ALEN);
983                     }
984
985                     unifi_trace(priv, UDBG1,
986                             "_identify_sme_ma_pkt_ind: unifi_sys_ma_pkt_ind -->\n");
987                     CsrWifiRouterMaPacketIndSend(priv->sme_unidata_ind_filters[i].appHandle,
988                             (pkt_ind->VirtualInterfaceIdentifier & 0xff),
989                             i,
990                             pkt_ind->ReceptionStatus,
991                             bulkdata->d[0].data_length,
992                             (u8*)bulkdata->d[0].os_data_ptr,
993                             NULL,
994                             pkt_ind->Rssi,
995                             pkt_ind->Snr,
996                             pkt_ind->ReceivedRate);
997
998
999                     unifi_trace(priv, UDBG1,
1000                             "_identify_sme_ma_pkt_ind: unifi_sys_ma_pkt_ind <--\n");
1001                 }
1002
1003                 return 1;
1004             }
1005         }
1006     }
1007
1008     return -1;
1009 }
1010 #endif /* CSR_SUPPORT_SME */
1011
1012 /*
1013  * ---------------------------------------------------------------------------
1014  *  skb_80211_to_ether
1015  *
1016  *      Make sure the received frame is in Ethernet (802.3) form.
1017  *      De-encapsulates SNAP if necessary, adds a ethernet header.
1018  *      The source buffer should not contain an 802.11 MAC header
1019  *
1020  *  Arguments:
1021  *      payload         Pointer to packet data received from UniFi.
1022  *      payload_length  Number of bytes of data received from UniFi.
1023  *      daddr           Destination MAC address.
1024  *      saddr           Source MAC address.
1025  *
1026  *  Returns:
1027  *      0 on success, -1 if the packet is bad and should be dropped,
1028  *      1 if the packet was forwarded to the SME or AMP client.
1029  * ---------------------------------------------------------------------------
1030  */
1031 int
1032 skb_80211_to_ether(unifi_priv_t *priv, struct sk_buff *skb,
1033                    const unsigned char *daddr, const unsigned char *saddr,
1034                    const CSR_SIGNAL *signal,
1035                    bulk_data_param_t *bulkdata)
1036 {
1037     unsigned char *payload;
1038     int payload_length;
1039     struct ethhdr *eth;
1040     llc_snap_hdr_t *snap;
1041     int headroom;
1042 #define UF_VLAN_LLC_HEADER_SIZE     18
1043     static const u8 vlan_inner_snap[] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00 };
1044 #if defined(CSR_NATIVE_SOFTMAC) && defined(CSR_SUPPORT_SME)
1045     const CSR_MA_PACKET_INDICATION *pkt_ind = &signal->u.MaPacketIndication;
1046 #endif
1047
1048     if(skb== NULL || daddr == NULL || saddr == NULL){
1049         unifi_error(priv,"skb_80211_to_ether: PBC fail\n");
1050         return 1;
1051     }
1052
1053     payload = skb->data;
1054     payload_length = skb->len;
1055
1056     snap = (llc_snap_hdr_t *)payload;
1057     eth  = (struct ethhdr *)payload;
1058
1059     /* get the skb headroom size */
1060     headroom = skb_headroom(skb);
1061
1062     /*
1063      * Test for the various encodings
1064      */
1065     if ((payload_length >= sizeof(llc_snap_hdr_t)) &&
1066             (snap->dsap == 0xAA) &&
1067             (snap->ssap == 0xAA) &&
1068             (snap->ctrl == 0x03) &&
1069             (snap->oui[0] == 0) &&
1070             (snap->oui[1] == 0) &&
1071             ((snap->oui[2] == 0) || (snap->oui[2] == 0xF8)))
1072     {
1073         /* AppleTalk AARP (2) or IPX SNAP */
1074         if ((snap->oui[2] == 0) &&
1075                 ((ntohs(snap->protocol) == ETH_P_AARP) || (ntohs(snap->protocol) == ETH_P_IPX)))
1076         {
1077             u16 len;
1078
1079             unifi_trace(priv, UDBG3, "%s len: %d\n",
1080                     (ntohs(snap->protocol) == ETH_P_AARP) ? "ETH_P_AARP" : "ETH_P_IPX",
1081                     payload_length);
1082
1083             /* check for headroom availability before skb_push */
1084             if (headroom < (2 * ETH_ALEN + 2)) {
1085                 unifi_warning(priv, "headroom not available to skb_push ether header\n");
1086                 return -1;
1087             }
1088
1089             /* Add 802.3 header and leave full payload */
1090             len = htons(skb->len);
1091             memcpy(skb_push(skb, 2), &len, 2);
1092             memcpy(skb_push(skb, ETH_ALEN), saddr, ETH_ALEN);
1093             memcpy(skb_push(skb, ETH_ALEN), daddr, ETH_ALEN);
1094
1095             return 0;
1096         }
1097         /* VLAN-tagged IP */
1098         if ((snap->oui[2] == 0) && (ntohs(snap->protocol) == ETH_P_8021Q))
1099         {
1100             /*
1101              * The translation doesn't change the packet length, so is done in-place.
1102              *
1103              * Example header (from Std 802.11-2007 Annex M):
1104              * AA-AA-03-00-00-00-81-00-87-65-AA-AA-03-00-00-00-08-06
1105              * -------SNAP-------p1-p1-ll-ll-------SNAP--------p2-p2
1106              * dd-dd-dd-dd-dd-dd-aa-aa-aa-aa-aa-aa-p1-p1-ll-ll-p2-p2
1107              * dd-dd-dd-dd-dd-dd-aa-aa-aa-aa-aa-aa-81-00-87-65-08-06
1108              */
1109             u16 vlan_snap;
1110
1111             if (payload_length < UF_VLAN_LLC_HEADER_SIZE) {
1112                 unifi_warning(priv, "VLAN SNAP header too short: %d bytes\n", payload_length);
1113                 return -1;
1114             }
1115
1116             if (memcmp(payload + 10, vlan_inner_snap, 6)) {
1117                 unifi_warning(priv, "VLAN malformatted SNAP header.\n");
1118                 return -1;
1119             }
1120
1121             unifi_trace(priv, UDBG3, "VLAN SNAP: %02x-%02x\n", payload[8], payload[9]);
1122             unifi_trace(priv, UDBG3, "VLAN len: %d\n", payload_length);
1123
1124             /* Create the 802.3 header */
1125
1126             vlan_snap = *((u16*)(payload + 8));
1127
1128             /* Create LLC header without byte-swapping */
1129             eth->h_proto = snap->protocol;
1130
1131             memcpy(eth->h_dest, daddr, ETH_ALEN);
1132             memcpy(eth->h_source, saddr, ETH_ALEN);
1133             *(u16*)(eth + 1) = vlan_snap;
1134             return 0;
1135         }
1136
1137         /* it's a SNAP + RFC1042 frame */
1138         unifi_trace(priv, UDBG3, "SNAP+RFC1042 len: %d\n", payload_length);
1139
1140         /* chop SNAP+llc header from skb. */
1141         skb_pull(skb, sizeof(llc_snap_hdr_t));
1142
1143         /* Since skb_pull called above to chop snap+llc, no need to check for headroom
1144          * availability before skb_push
1145          */
1146         /* create 802.3 header at beginning of skb. */
1147         eth = (struct ethhdr *)skb_push(skb, ETH_HLEN);
1148         memcpy(eth->h_dest, daddr, ETH_ALEN);
1149         memcpy(eth->h_source, saddr, ETH_ALEN);
1150         /* Copy protocol field without byte-swapping */
1151         eth->h_proto = snap->protocol;
1152     } else {
1153         u16 len;
1154
1155         /* check for headroom availability before skb_push */
1156         if (headroom < (2 * ETH_ALEN + 2)) {
1157             unifi_warning(priv, "headroom not available to skb_push ether header\n");
1158             return -1;
1159         }
1160         /* Add 802.3 header and leave full payload */
1161         len = htons(skb->len);
1162         memcpy(skb_push(skb, 2), &len, 2);
1163         memcpy(skb_push(skb, ETH_ALEN), saddr, ETH_ALEN);
1164         memcpy(skb_push(skb, ETH_ALEN), daddr, ETH_ALEN);
1165
1166         return 1;
1167     }
1168
1169     return 0;
1170 } /* skb_80211_to_ether() */
1171
1172
1173 static CsrWifiRouterCtrlPortAction verify_port(unifi_priv_t *priv, unsigned char *address, int queue, u16 interfaceTag)
1174 {
1175 #ifdef CSR_NATIVE_LINUX
1176 #ifdef CSR_SUPPORT_WEXT
1177     if (queue == UF_CONTROLLED_PORT_Q) {
1178         return priv->wext_conf.block_controlled_port;
1179     } else {
1180         return CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_OPEN;
1181     }
1182 #else
1183     return CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_OPEN; /* default to open for softmac dev */
1184 #endif
1185 #else
1186     return uf_sme_port_state(priv, address, queue, interfaceTag);
1187 #endif
1188 }
1189
1190 /*
1191  * ---------------------------------------------------------------------------
1192  *  prepare_and_add_macheader
1193  *
1194  *
1195  *      These functions adds mac header for packet from netdev
1196  *      to UniFi for transmission.
1197  *      EAP protocol packets are also appended with Mac header &
1198  *      sent using send_ma_pkt_request().
1199  *
1200  *  Arguments:
1201  *      priv            Pointer to device private context struct
1202  *      skb             Socket buffer containing data packet to transmit
1203  *      newSkb          Socket buffer containing data packet + Mac header if no sufficient headroom in skb
1204  *      serviceClass    to append QOS control header in Mac header
1205  *      bulkdata        if newSkb allocated then bulkdata updated to send to unifi
1206  *      interfaceTag    the interfaceID on which activity going on
1207  *      daddr           destination address
1208  *      saddr           source address
1209  *      protection      protection bit set in framce control of mac header
1210  *
1211  *  Returns:
1212  *      Zero on success or error code.
1213  * ---------------------------------------------------------------------------
1214  */
1215
1216 int prepare_and_add_macheader(unifi_priv_t *priv, struct sk_buff *skb, struct sk_buff *newSkb,
1217                               CSR_PRIORITY priority,
1218                               bulk_data_param_t *bulkdata,
1219                               u16 interfaceTag,
1220                               const u8 *daddr,
1221                               const u8 *saddr,
1222                               u8 protection)
1223 {
1224     u16 fc = 0;
1225     u8 qc = 0;
1226     u8 macHeaderLengthInBytes = MAC_HEADER_SIZE, *bufPtr = NULL;
1227     bulk_data_param_t data_ptrs;
1228     CsrResult csrResult;
1229     int headroom =0;
1230     u8 direction = 0;
1231     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
1232     u8 *addressOne;
1233     u8 bQosNull = false;
1234
1235     if (skb == NULL) {
1236         unifi_error(priv,"prepare_and_add_macheader: Invalid SKB reference\n");
1237         return -1;
1238     }
1239
1240     /* add a MAC header refer: 7.1.3.1 Frame Control field in P802.11REVmb.book */
1241     if (priority != CSR_CONTENTION) {
1242         /* EAPOL packets don't go as QOS_DATA */
1243         if (priority == CSR_MANAGEMENT) {
1244             fc |= cpu_to_le16(IEEE802_11_FC_TYPE_DATA);
1245         } else {
1246             /* Qos Control Field */
1247             macHeaderLengthInBytes += QOS_CONTROL_HEADER_SIZE;
1248
1249             if (skb->len) {
1250
1251                 fc |= cpu_to_le16(IEEE802_11_FC_TYPE_QOS_DATA);
1252             } else {
1253                 fc |= cpu_to_le16(IEEE802_11_FC_TYPE_QOS_NULL);
1254                 bQosNull = true;
1255             }
1256         }
1257     } else {
1258         if(skb->len == 0) {
1259             fc |= cpu_to_le16(IEEE802_11_FC_TYPE_NULL);
1260         } else {
1261             fc |= cpu_to_le16(IEEE802_11_FC_TYPE_DATA);
1262         }
1263     }
1264
1265     switch (interfacePriv->interfaceMode)
1266     {
1267         case  CSR_WIFI_ROUTER_CTRL_MODE_STA:
1268         case CSR_WIFI_ROUTER_CTRL_MODE_P2PCLI:
1269             direction = 2;
1270             fc |= cpu_to_le16(IEEE802_11_FC_TO_DS_MASK);
1271             break;
1272         case  CSR_WIFI_ROUTER_CTRL_MODE_IBSS:
1273             direction = 0;
1274             break;
1275         case  CSR_WIFI_ROUTER_CTRL_MODE_AP:
1276         case CSR_WIFI_ROUTER_CTRL_MODE_P2PGO:
1277             direction = 1;
1278             fc |= cpu_to_le16(IEEE802_11_FC_FROM_DS_MASK);
1279             break;
1280         case CSR_WIFI_ROUTER_CTRL_MODE_AMP:
1281             if (priority == CSR_MANAGEMENT ) {
1282
1283                 direction = 2;
1284                 fc |= cpu_to_le16(IEEE802_11_FC_TO_DS_MASK);
1285             } else {
1286                 /* Data frames have to use WDS 4 address frames */
1287                 direction = 3;
1288                 fc |= cpu_to_le16(IEEE802_11_FC_TO_DS_MASK | IEEE802_11_FC_FROM_DS_MASK);
1289                 macHeaderLengthInBytes += 6;
1290             }
1291             break;
1292         default:
1293             unifi_warning(priv, "prepare_and_add_macheader: Unknown mode %d\n",
1294                           interfacePriv->interfaceMode);
1295     }
1296
1297
1298     /* If Sta is QOS & HTC is supported then need to set 'order' bit */
1299     /* We don't support HT Control for now */
1300
1301     if(protection) {
1302         fc |= cpu_to_le16(IEEE802_11_FC_PROTECTED_MASK);
1303     }
1304
1305     /* check the skb headroom before pushing mac header */
1306     headroom = skb_headroom(skb);
1307
1308     if (headroom < macHeaderLengthInBytes) {
1309         unifi_trace(priv, UDBG5,
1310                     "prepare_and_add_macheader: Allocate headroom extra %d bytes\n",
1311                     macHeaderLengthInBytes);
1312
1313         csrResult = unifi_net_data_malloc(priv, &data_ptrs.d[0], skb->len + macHeaderLengthInBytes);
1314
1315         if (csrResult != CSR_RESULT_SUCCESS) {
1316             unifi_error(priv, " failed to allocate request_data. in %s func\n", __FUNCTION__);
1317             return -1;
1318         }
1319         newSkb = (struct sk_buff *)(data_ptrs.d[0].os_net_buf_ptr);
1320         newSkb->len = skb->len + macHeaderLengthInBytes;
1321
1322         memcpy((void*)data_ptrs.d[0].os_data_ptr + macHeaderLengthInBytes,
1323                 skb->data, skb->len);
1324
1325         bulkdata->d[0].os_data_ptr = newSkb->data;
1326         bulkdata->d[0].os_net_buf_ptr = (unsigned char*)newSkb;
1327         bulkdata->d[0].data_length = newSkb->len;
1328
1329         bufPtr = (u8*)data_ptrs.d[0].os_data_ptr;
1330
1331         /* The old skb will not be used again */
1332             kfree_skb(skb);
1333     } else {
1334
1335         /* headroom has sufficient size, so will get proper pointer */
1336         bufPtr = (u8*)skb_push(skb, macHeaderLengthInBytes);
1337         bulkdata->d[0].os_data_ptr = skb->data;
1338         bulkdata->d[0].os_net_buf_ptr = (unsigned char*)skb;
1339         bulkdata->d[0].data_length = skb->len;
1340     }
1341
1342     /* Frame the actual MAC header */
1343
1344     memset(bufPtr, 0, macHeaderLengthInBytes);
1345
1346     /* copy frameControl field */
1347     memcpy(bufPtr, &fc, sizeof(fc));
1348     bufPtr += sizeof(fc);
1349     macHeaderLengthInBytes -= sizeof(fc);
1350
1351     /* Duration/ID field which is 2 bytes */
1352     bufPtr += 2;
1353     macHeaderLengthInBytes -= 2;
1354
1355     switch(direction)
1356     {
1357         case 0:
1358             /* Its an Ad-Hoc no need to route it through AP */
1359             /* Address1: MAC address of the destination from eth header */
1360             memcpy(bufPtr, daddr, ETH_ALEN);
1361             bufPtr += ETH_ALEN;
1362             macHeaderLengthInBytes -= ETH_ALEN;
1363
1364             /* Address2: MAC address of the source */
1365             memcpy(bufPtr, saddr, ETH_ALEN);
1366             bufPtr += ETH_ALEN;
1367             macHeaderLengthInBytes -= ETH_ALEN;
1368
1369             /* Address3: the BSSID (locally generated in AdHoc (creators Bssid)) */
1370             memcpy(bufPtr, &interfacePriv->bssid, ETH_ALEN);
1371             bufPtr += ETH_ALEN;
1372             macHeaderLengthInBytes -= ETH_ALEN;
1373             break;
1374         case 1:
1375            /* Address1: MAC address of the actual destination */
1376             memcpy(bufPtr, daddr, ETH_ALEN);
1377             bufPtr += ETH_ALEN;
1378             macHeaderLengthInBytes -= ETH_ALEN;
1379             /* Address2: The MAC address of the AP */
1380             memcpy(bufPtr, &interfacePriv->bssid, ETH_ALEN);
1381             bufPtr += ETH_ALEN;
1382             macHeaderLengthInBytes -= ETH_ALEN;
1383
1384             /* Address3: MAC address of the source from eth header */
1385             memcpy(bufPtr, saddr, ETH_ALEN);
1386             bufPtr += ETH_ALEN;
1387             macHeaderLengthInBytes -= ETH_ALEN;
1388             break;
1389         case  2:
1390             /* Address1: To AP is the MAC address of the AP to which its associated */
1391             memcpy(bufPtr, &interfacePriv->bssid, ETH_ALEN);
1392             bufPtr += ETH_ALEN;
1393             macHeaderLengthInBytes -= ETH_ALEN;
1394
1395             /* Address2: MAC address of the source from eth header */
1396             memcpy(bufPtr, saddr, ETH_ALEN);
1397             bufPtr += ETH_ALEN;
1398             macHeaderLengthInBytes -= ETH_ALEN;
1399
1400             /* Address3: MAC address of the actual destination on the distribution system */
1401             memcpy(bufPtr, daddr, ETH_ALEN);
1402             bufPtr += ETH_ALEN;
1403             macHeaderLengthInBytes -= ETH_ALEN;
1404             break;
1405         case 3:
1406             memcpy(bufPtr, &interfacePriv->bssid, ETH_ALEN);
1407             bufPtr += ETH_ALEN;
1408             macHeaderLengthInBytes -= ETH_ALEN;
1409
1410             /* Address2: MAC address of the source from eth header */
1411             memcpy(bufPtr, saddr, ETH_ALEN);
1412             bufPtr += ETH_ALEN;
1413             macHeaderLengthInBytes -= ETH_ALEN;
1414
1415             /* Address3: MAC address of the actual destination on the distribution system */
1416             memcpy(bufPtr, daddr, ETH_ALEN);
1417             bufPtr += ETH_ALEN;
1418             macHeaderLengthInBytes -= ETH_ALEN;
1419             break;
1420         default:
1421             unifi_error(priv,"Unknown direction =%d : Not handled now\n",direction);
1422             return -1;
1423     }
1424     /* 2 bytes of frame control field, appended by firmware */
1425     bufPtr += 2;
1426     macHeaderLengthInBytes -= 2;
1427
1428     if (3 == direction) {
1429         /* Address4: MAC address of the source */
1430         memcpy(bufPtr, saddr, ETH_ALEN);
1431         bufPtr += ETH_ALEN;
1432         macHeaderLengthInBytes -= ETH_ALEN;
1433     }
1434
1435     /* IF Qos Data or Qos Null Data then set QosControl field */
1436     if ((priority != CSR_CONTENTION) && (macHeaderLengthInBytes >= QOS_CONTROL_HEADER_SIZE)) {
1437
1438         if (priority > 7) {
1439             unifi_trace(priv, UDBG1, "data packets priority is more than 7, priority = %x\n", priority);
1440             qc |= 7;
1441         } else {
1442             qc |= priority;
1443         }
1444         /*assigning address1
1445         * Address1 offset taken fromm bufPtr(currently bufPtr pointing to Qos contorl) variable in reverse direction
1446         * Address4 don't exit
1447         */
1448
1449         addressOne = bufPtr- ADDRESS_ONE_OFFSET;
1450
1451         if (addressOne[0] & 0x1) {
1452             /* multicast/broadcast frames, no acknowledgement needed */
1453             qc |= 1 << 5;
1454         }
1455         /* non-AP mode only for now */
1456         if(interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_STA ||
1457            interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_IBSS ||
1458            interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PCLI) {
1459            /* In case of STA and IBSS case eosp and txop limit is 0. */
1460         } else {
1461             if(bQosNull) {
1462                 qc |= 1 << 4;
1463             }
1464         }
1465
1466         /* append Qos control field to mac header */
1467         bufPtr[0] = qc;
1468         /* txop limit is 0 */
1469         bufPtr[1] = 0;
1470         macHeaderLengthInBytes -= QOS_CONTROL_HEADER_SIZE;
1471     }
1472     if (macHeaderLengthInBytes) {
1473         unifi_warning(priv, " Mac header not appended properly\n");
1474         return -1;
1475     }
1476     return 0;
1477 }
1478
1479 /*
1480  * ---------------------------------------------------------------------------
1481  *  send_ma_pkt_request
1482  *
1483  *      These functions send a data packet to UniFi for transmission.
1484  *      EAP protocol packets are also sent as send_ma_pkt_request().
1485  *
1486  *  Arguments:
1487  *      priv            Pointer to device private context struct
1488  *      skb             Socket buffer containing data packet to transmit
1489  *      ehdr            Pointer to Ethernet header within skb.
1490  *
1491  *  Returns:
1492  *      Zero on success or error code.
1493  * ---------------------------------------------------------------------------
1494  */
1495
1496 static int
1497 send_ma_pkt_request(unifi_priv_t *priv, struct sk_buff *skb, const struct ethhdr *ehdr, CSR_PRIORITY priority)
1498 {
1499     int r;
1500     u16 i;
1501     u8 eapolStore = FALSE;
1502     struct sk_buff *newSkb = NULL;
1503     bulk_data_param_t bulkdata;
1504     const int proto = ntohs(ehdr->h_proto);
1505     u16 interfaceTag;
1506     CsrWifiMacAddress peerAddress;
1507     CSR_TRANSMISSION_CONTROL transmissionControl = CSR_NO_CONFIRM_REQUIRED;
1508     s8 protection;
1509     netInterface_priv_t *interfacePriv = NULL;
1510     CSR_RATE TransmitRate = (CSR_RATE)0;
1511
1512     unifi_trace(priv, UDBG5, "entering send_ma_pkt_request\n");
1513
1514     /* Get the interface Tag by means of source Mac address */
1515     for (i = 0; i < CSR_WIFI_NUM_INTERFACES; i++) {
1516         if (!memcmp(priv->netdev[i]->dev_addr, ehdr->h_source, ETH_ALEN)) {
1517             interfaceTag = i;
1518             interfacePriv = priv->interfacePriv[interfaceTag];
1519             break;
1520         }
1521     }
1522
1523     if (interfacePriv == NULL) {
1524         /* No match found - error */
1525         interfaceTag = 0;
1526         interfacePriv = priv->interfacePriv[interfaceTag];
1527         unifi_warning(priv, "Mac address not matching ... debugging needed\n");
1528         interfacePriv->stats.tx_dropped++;
1529         kfree_skb(skb);
1530         return -1;
1531     }
1532
1533     /* Add a SNAP header if necessary */
1534     if (skb_add_llc_snap(priv->netdev[interfaceTag], skb, proto) != 0) {
1535         /* convert failed */
1536         unifi_error(priv, "skb_add_llc_snap failed.\n");
1537         kfree_skb(skb);
1538         return -1;
1539     }
1540
1541     bulkdata.d[0].os_data_ptr = skb->data;
1542     bulkdata.d[0].os_net_buf_ptr = (unsigned char*)skb;
1543     bulkdata.d[0].net_buf_length = bulkdata.d[0].data_length = skb->len;
1544     bulkdata.d[1].os_data_ptr = NULL;
1545     bulkdata.d[1].os_net_buf_ptr = NULL;
1546     bulkdata.d[1].net_buf_length = bulkdata.d[1].data_length = 0;
1547
1548 #ifdef CSR_SUPPORT_SME
1549     /* Notify the TA module for the Tx frame  for non AP/P2PGO mode*/
1550     if ((interfacePriv->interfaceMode != CSR_WIFI_ROUTER_CTRL_MODE_AP) &&
1551         (interfacePriv->interfaceMode != CSR_WIFI_ROUTER_CTRL_MODE_P2PGO)) {
1552         unifi_ta_sample(priv->card, CSR_WIFI_ROUTER_CTRL_PROTOCOL_DIRECTION_TX,
1553                         &bulkdata.d[0], ehdr->h_source,
1554                         priv->netdev[interfaceTag]->dev_addr,
1555                         jiffies_to_msecs(jiffies),
1556                         0);     /* rate is unknown on tx */
1557     }
1558 #endif /* CSR_SUPPORT_SME */
1559
1560     if ((proto == ETH_P_PAE)
1561 #ifdef CSR_WIFI_SECURITY_WAPI_ENABLE
1562             || (proto == ETH_P_WAI)
1563 #endif
1564        )
1565     {
1566         /* check for m4 detection */
1567         if (0 == uf_verify_m4(priv, bulkdata.d[0].os_data_ptr, bulkdata.d[0].data_length)) {
1568             eapolStore = TRUE;
1569         }
1570     }
1571
1572 #ifdef CSR_WIFI_SECURITY_WAPI_ENABLE
1573     if (proto == ETH_P_WAI)
1574      {
1575         protection = 0; /*WAI packets always sent unencrypted*/
1576      }
1577    else
1578      {
1579 #endif
1580 #ifdef CSR_SUPPORT_SME
1581     if ((protection = uf_get_protection_bit_from_interfacemode(priv, interfaceTag, ehdr->h_dest)) < 0) {
1582         unifi_warning(priv, "unicast address, but destination not in station record database\n");
1583         unifi_net_data_free(priv, &bulkdata.d[0]);
1584         return -1;
1585     }
1586 #else
1587     protection = 0;
1588 #endif
1589 #ifdef CSR_WIFI_SECURITY_WAPI_ENABLE
1590    }
1591 #endif
1592
1593     /* append Mac header for Eapol as well as data packet */
1594     if (prepare_and_add_macheader(priv, skb, newSkb, priority, &bulkdata, interfaceTag, ehdr->h_dest, ehdr->h_source, protection)) {
1595         unifi_error(priv, "failed to create MAC header\n");
1596         unifi_net_data_free(priv, &bulkdata.d[0]);
1597         return -1;
1598     }
1599
1600     /* RA adrress must contain the immediate destination MAC address that is similiar to
1601      * the Address 1 field of 802.11 Mac header here 4 is: (sizeof(framecontrol) + sizeof (durationID))
1602      * which is address 1 field
1603      */
1604     memcpy(peerAddress.a, ((u8 *) bulkdata.d[0].os_data_ptr) + 4, ETH_ALEN);
1605
1606     unifi_trace(priv, UDBG5, "RA[0]=%x, RA[1]=%x, RA[2]=%x, RA[3]=%x, RA[4]=%x, RA[5]=%x\n",
1607                 peerAddress.a[0],peerAddress.a[1], peerAddress.a[2], peerAddress.a[3],
1608                 peerAddress.a[4],peerAddress.a[5]);
1609
1610
1611     if ((proto == ETH_P_PAE)
1612 #ifdef CSR_WIFI_SECURITY_WAPI_ENABLE
1613             || (proto == ETH_P_WAI)
1614 #endif
1615        )
1616     {
1617         CSR_SIGNAL signal;
1618         CSR_MA_PACKET_REQUEST *req = &signal.u.MaPacketRequest;
1619
1620         /* initialize signal to zero */
1621         memset(&signal, 0, sizeof(CSR_SIGNAL));
1622
1623         /* Frame MA_PACKET request */
1624         signal.SignalPrimitiveHeader.SignalId = CSR_MA_PACKET_REQUEST_ID;
1625         signal.SignalPrimitiveHeader.ReceiverProcessId = 0;
1626         signal.SignalPrimitiveHeader.SenderProcessId = priv->netdev_client->sender_id;
1627
1628         transmissionControl = req->TransmissionControl = 0;
1629 #ifdef CSR_SUPPORT_SME
1630         if (eapolStore)
1631         {
1632             netInterface_priv_t *netpriv = (netInterface_priv_t *)netdev_priv(priv->netdev[interfaceTag]);
1633
1634             /* Fill the MA-PACKET.req */
1635
1636             req->Priority = priority;
1637             unifi_trace(priv, UDBG3, "Tx Frame with Priority: %x\n", req->Priority);
1638
1639             /* rate selected by firmware */
1640             req->TransmitRate = 0;
1641             req->HostTag = CSR_WIFI_EAPOL_M4_HOST_TAG;
1642             /* RA address matching with address 1 of Mac header */
1643             memcpy(req->Ra.x, ((u8 *) bulkdata.d[0].os_data_ptr) + 4, ETH_ALEN);
1644
1645             spin_lock(&priv->m4_lock);
1646             /* Store the M4-PACKET.req for later */
1647             interfacePriv->m4_signal = signal;
1648             interfacePriv->m4_bulk_data.net_buf_length = bulkdata.d[0].net_buf_length;
1649             interfacePriv->m4_bulk_data.data_length = bulkdata.d[0].data_length;
1650             interfacePriv->m4_bulk_data.os_data_ptr = bulkdata.d[0].os_data_ptr;
1651             interfacePriv->m4_bulk_data.os_net_buf_ptr = bulkdata.d[0].os_net_buf_ptr;
1652             spin_unlock(&priv->m4_lock);
1653
1654             /* Signal the workqueue to call CsrWifiRouterCtrlM4ReadyToSendIndSend().
1655              * It cannot be called directly from the tx path because it
1656              * does a non-atomic kmalloc via the framework's CsrPmemAlloc().
1657              */
1658             queue_work(priv->unifi_workqueue, &netpriv->send_m4_ready_task);
1659
1660             return 0;
1661         }
1662 #endif
1663     }/*EAPOL or WAI packet*/
1664
1665 #if (defined(CSR_WIFI_SECURITY_WAPI_ENABLE) && defined(CSR_WIFI_SECURITY_WAPI_SW_ENCRYPTION))
1666     if ((CSR_WIFI_ROUTER_CTRL_MODE_STA == interfacePriv->interfaceMode) && \
1667         (priv->wapi_unicast_filter) && \
1668         (proto != ETH_P_PAE) && \
1669         (proto != ETH_P_WAI) && \
1670         (skb->len > 0))
1671     {
1672         CSR_SIGNAL signal;
1673         CSR_MA_PACKET_REQUEST *req = &signal.u.MaPacketRequest;
1674         netInterface_priv_t *netpriv = (netInterface_priv_t *)netdev_priv(priv->netdev[interfaceTag]);
1675
1676         unifi_trace(priv, UDBG4, "send_ma_pkt_request() - WAPI unicast data packet when USKID = 1 \n");
1677
1678         /* initialize signal to zero */
1679         memset(&signal, 0, sizeof(CSR_SIGNAL));
1680         /* Frame MA_PACKET request */
1681         signal.SignalPrimitiveHeader.SignalId = CSR_MA_PACKET_REQUEST_ID;
1682         signal.SignalPrimitiveHeader.ReceiverProcessId = 0;
1683         signal.SignalPrimitiveHeader.SenderProcessId = priv->netdev_client->sender_id;
1684
1685         /* Fill the MA-PACKET.req */
1686         req->TransmissionControl = 0;
1687         req->Priority = priority;
1688         unifi_trace(priv, UDBG3, "Tx Frame with Priority: %x\n", req->Priority);
1689         req->TransmitRate = (CSR_RATE) 0; /* rate selected by firmware */
1690         req->HostTag = 0xffffffff;        /* Ask for a new HostTag */
1691         /* RA address matching with address 1 of Mac header */
1692         memcpy(req->Ra.x, ((u8 *) bulkdata.d[0].os_data_ptr) + 4, ETH_ALEN);
1693
1694         /* Store the M4-PACKET.req for later */
1695         spin_lock(&priv->wapi_lock);
1696         interfacePriv->wapi_unicast_ma_pkt_sig = signal;
1697         interfacePriv->wapi_unicast_bulk_data.net_buf_length = bulkdata.d[0].net_buf_length;
1698         interfacePriv->wapi_unicast_bulk_data.data_length = bulkdata.d[0].data_length;
1699         interfacePriv->wapi_unicast_bulk_data.os_data_ptr = bulkdata.d[0].os_data_ptr;
1700         interfacePriv->wapi_unicast_bulk_data.os_net_buf_ptr = bulkdata.d[0].os_net_buf_ptr;
1701         spin_unlock(&priv->wapi_lock);
1702
1703         /* Signal the workqueue to call CsrWifiRouterCtrlWapiUnicastTxEncryptIndSend().
1704          * It cannot be called directly from the tx path because it
1705          * does a non-atomic kmalloc via the framework's CsrPmemAlloc().
1706          */
1707         queue_work(priv->unifi_workqueue, &netpriv->send_pkt_to_encrypt);
1708
1709         return 0;
1710     }
1711 #endif
1712
1713     if(priv->cmanrTestMode)
1714     {
1715         TransmitRate = priv->cmanrTestModeTransmitRate;
1716         unifi_trace(priv, UDBG2, "send_ma_pkt_request: cmanrTestModeTransmitRate = %d TransmitRate=%d\n",
1717                     priv->cmanrTestModeTransmitRate,
1718                     TransmitRate
1719                    );
1720     }
1721
1722     /* Send UniFi msg */
1723     /* Here hostTag is been sent as 0xffffffff, its been appended properly while framing MA-Packet request in pdu_processing.c file */
1724     r = uf_process_ma_packet_req(priv,
1725                                  peerAddress.a,
1726                                  0xffffffff,  /* Ask for a new HostTag */
1727                                  interfaceTag,
1728                                  transmissionControl,
1729                                  TransmitRate,
1730                                  priority,
1731                                  priv->netdev_client->sender_id,
1732                                  &bulkdata);
1733
1734     if (r) {
1735         unifi_trace(priv, UDBG1, "(HIP validation failure) r = %x\n", r);
1736         unifi_net_data_free(priv, &bulkdata.d[0]);
1737         return -1;
1738     }
1739
1740     unifi_trace(priv, UDBG3, "leaving send_ma_pkt_request, UNITDATA result code = %d\n", r);
1741
1742     return r;
1743 } /* send_ma_pkt_request() */
1744
1745 /*
1746  * ---------------------------------------------------------------------------
1747  *  uf_net_xmit
1748  *
1749  *      This function is called by the higher level stack to transmit an
1750  *      ethernet packet.
1751  *
1752  *  Arguments:
1753  *      skb     Ethernet packet to send.
1754  *      dev     Pointer to the linux net device.
1755  *
1756  *  Returns:
1757  *      0   on success (packet was consumed, not necessarily transmitted)
1758  *      1   if packet was requeued
1759  *     -1   on error
1760  *
1761  *
1762  *  Notes:
1763  *      The controlled port is handled in the qdisc dequeue handler.
1764  * ---------------------------------------------------------------------------
1765  */
1766 static netdev_tx_t
1767 uf_net_xmit(struct sk_buff *skb, struct net_device *dev)
1768 {
1769     netInterface_priv_t *interfacePriv = (netInterface_priv_t *)netdev_priv(dev);
1770     unifi_priv_t *priv = interfacePriv->privPtr;
1771     struct ethhdr ehdr;
1772     int proto, port;
1773     int result;
1774     static tx_signal_handler tx_handler;
1775     CSR_PRIORITY priority;
1776     CsrWifiRouterCtrlPortAction port_action;
1777
1778     func_enter();
1779
1780     unifi_trace(priv, UDBG5, "unifi_net_xmit: skb = %x\n", skb);
1781
1782     memcpy(&ehdr, skb->data, ETH_HLEN);
1783     proto = ntohs(ehdr.h_proto);
1784     priority = get_packet_priority(priv, skb, &ehdr, interfacePriv);
1785
1786     /* All frames are sent as MA-PACKET.req (EAPOL also) */
1787     tx_handler = send_ma_pkt_request;
1788
1789     /* 802.1x - apply controlled/uncontrolled port rules */
1790     if ((proto != ETH_P_PAE)
1791 #ifdef CSR_WIFI_SECURITY_WAPI_ENABLE
1792             && (proto != ETH_P_WAI)
1793 #endif
1794        ) {
1795         port = UF_CONTROLLED_PORT_Q;
1796     } else {
1797         /* queue 4 */
1798         port = UF_UNCONTROLLED_PORT_Q;
1799     }
1800
1801     /* Uncontrolled port rules apply */
1802     port_action = verify_port(priv
1803         , (((CSR_WIFI_ROUTER_CTRL_MODE_STA == interfacePriv->interfaceMode)||(CSR_WIFI_ROUTER_CTRL_MODE_P2PCLI== interfacePriv->interfaceMode))? interfacePriv->bssid.a: ehdr.h_dest)
1804         , port
1805         , interfacePriv->InterfaceTag);
1806
1807     if (port_action == CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_OPEN) {
1808         unifi_trace(priv, UDBG5,
1809                     "uf_net_xmit: %s controlled port open\n",
1810                     port ? "" : "un");
1811         /* Remove the ethernet header */
1812         skb_pull(skb, ETH_HLEN);
1813         result = tx_handler(priv, skb, &ehdr, priority);
1814     } else {
1815
1816         /* Discard the packet if necessary */
1817         unifi_trace(priv, UDBG2,
1818                 "uf_net_xmit: %s controlled port %s\n",
1819                 port ? "" : "un", port_action==CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_CLOSED_BLOCK ? "blocked" : "closed");
1820         interfacePriv->stats.tx_dropped++;
1821         kfree_skb(skb);
1822
1823         func_exit();
1824         return NETDEV_TX_OK;
1825     }
1826
1827     if (result == NETDEV_TX_OK) {
1828 #if (defined(CSR_WIFI_SECURITY_WAPI_ENABLE) && defined(CSR_WIFI_SECURITY_WAPI_SW_ENCRYPTION))
1829         /* Don't update the tx stats when the pkt is to be sent for sw encryption*/
1830         if (!((CSR_WIFI_ROUTER_CTRL_MODE_STA == interfacePriv->interfaceMode) &&
1831               (priv->wapi_unicast_filter == 1)))
1832         {
1833             dev->trans_start = jiffies;
1834             /* Should really count tx stats in the UNITDATA.status signal but
1835              * that doesn't have the length.
1836              */
1837             interfacePriv->stats.tx_packets++;
1838             /* count only the packet payload */
1839             interfacePriv->stats.tx_bytes += skb->len;
1840
1841         }
1842 #else
1843         dev->trans_start = jiffies;
1844
1845         /*
1846          * Should really count tx stats in the UNITDATA.status signal but
1847          * that doesn't have the length.
1848          */
1849         interfacePriv->stats.tx_packets++;
1850         /* count only the packet payload */
1851         interfacePriv->stats.tx_bytes += skb->len;
1852 #endif
1853     } else if (result < 0) {
1854
1855         /* Failed to send: fh queue was full, and the skb was discarded.
1856          * Return OK to indicate that the buffer was consumed, to stop the
1857          * kernel re-transmitting the freed buffer.
1858          */
1859         interfacePriv->stats.tx_dropped++;
1860         unifi_trace(priv, UDBG1, "unifi_net_xmit: (Packet Drop), dropped count = %x\n", interfacePriv->stats.tx_dropped);
1861         result = NETDEV_TX_OK;
1862     }
1863
1864     /* The skb will have been freed by send_XXX_request() */
1865
1866     func_exit();
1867     return result;
1868 } /* uf_net_xmit() */
1869
1870 /*
1871  * ---------------------------------------------------------------------------
1872  *  unifi_pause_xmit
1873  *  unifi_restart_xmit
1874  *
1875  *      These functions are called from the UniFi core to control the flow
1876  *      of packets from the upper layers.
1877  *      unifi_pause_xmit() is called when the internal queue is full and
1878  *      should take action to stop unifi_ma_unitdata() being called.
1879  *      When the queue has drained, unifi_restart_xmit() will be called to
1880  *      re-enable the flow of packets for transmission.
1881  *
1882  *  Arguments:
1883  *      ospriv          OS private context pointer.
1884  *
1885  *  Returns:
1886  *      unifi_pause_xmit() is called from interrupt context.
1887  * ---------------------------------------------------------------------------
1888  */
1889 void
1890 unifi_pause_xmit(void *ospriv, unifi_TrafficQueue queue)
1891 {
1892     unifi_priv_t *priv = ospriv;
1893     int i; /* used as a loop counter */
1894
1895     func_enter();
1896     unifi_trace(priv, UDBG2, "Stopping queue %d\n", queue);
1897
1898     for(i=0;i<CSR_WIFI_NUM_INTERFACES;i++)
1899     {
1900         if (netif_running(priv->netdev[i]))
1901         {
1902             netif_stop_subqueue(priv->netdev[i], (u16)queue);
1903         }
1904     }
1905
1906 #ifdef CSR_SUPPORT_SME
1907     if(queue<=3) {
1908         routerStartBuffering(priv,queue);
1909         unifi_trace(priv,UDBG2,"Start buffering %d\n", queue);
1910      } else {
1911         routerStartBuffering(priv,0);
1912         unifi_error(priv, "Start buffering %d defaulting to 0\n", queue);
1913      }
1914 #endif
1915     func_exit();
1916
1917 } /* unifi_pause_xmit() */
1918
1919 void
1920 unifi_restart_xmit(void *ospriv, unifi_TrafficQueue queue)
1921 {
1922     unifi_priv_t *priv = ospriv;
1923     int i=0; /* used as a loop counter */
1924
1925     func_enter();
1926     unifi_trace(priv, UDBG2, "Waking queue %d\n", queue);
1927
1928     for(i=0;i<CSR_WIFI_NUM_INTERFACES;i++)
1929     {
1930         if (netif_running(priv->netdev[i]))
1931         {
1932             netif_wake_subqueue(priv->netdev[i], (u16)queue);
1933         }
1934     }
1935
1936 #ifdef CSR_SUPPORT_SME
1937     if(queue <=3) {
1938         routerStopBuffering(priv,queue);
1939         uf_send_buffered_frames(priv,queue);
1940     } else {
1941         routerStopBuffering(priv,0);
1942         uf_send_buffered_frames(priv,0);
1943     }
1944 #endif
1945     func_exit();
1946 } /* unifi_restart_xmit() */
1947
1948
1949 static void
1950 indicate_rx_skb(unifi_priv_t *priv, u16 ifTag, u8* dst_a, u8* src_a, struct sk_buff *skb, CSR_SIGNAL *signal,
1951                 bulk_data_param_t *bulkdata)
1952 {
1953     int r, sr = 0;
1954     struct net_device *dev;
1955
1956 #ifdef CSR_SUPPORT_SME
1957     llc_snap_hdr_t *snap;
1958
1959     snap = (llc_snap_hdr_t *)skb->data;
1960
1961     sr = _identify_sme_ma_pkt_ind(priv,
1962                                   snap->oui, ntohs(snap->protocol),
1963                                   signal,
1964                                   bulkdata,
1965                                   dst_a, src_a );
1966 #endif
1967
1968     /*
1969      * Decapsulate any SNAP header and
1970      * prepend an ethernet header so that the skb manipulation and ARP
1971      * stuff works.
1972      */
1973     r = skb_80211_to_ether(priv, skb, dst_a, src_a,
1974                            signal, bulkdata);
1975     if (r == -1) {
1976         /* Drop the packet and return */
1977         priv->interfacePriv[ifTag]->stats.rx_errors++;
1978         priv->interfacePriv[ifTag]->stats.rx_frame_errors++;
1979         unifi_net_data_free(priv, &bulkdata->d[0]);
1980         unifi_notice(priv, "indicate_rx_skb: Discard unknown frame.\n");
1981         func_exit();
1982         return;
1983     }
1984
1985     /* Handle the case where packet is sent up through the subscription
1986      * API but should not be given to the network stack (AMP PAL case)
1987      * LLC header is different from WiFi and the packet has been subscribed for
1988      */
1989     if (r == 1 && sr == 1) {
1990         unifi_net_data_free(priv, &bulkdata->d[0]);
1991         unifi_trace(priv, UDBG5, "indicate_rx_skb: Data given to subscription"
1992                 "API, not being given to kernel\n");
1993         func_exit();
1994         return;
1995     }
1996
1997     dev = priv->netdev[ifTag];
1998     /* Now we look like a regular ethernet frame */
1999     /* Fill in SKB meta data */
2000     skb->dev = dev;
2001     skb->protocol = eth_type_trans(skb, dev);
2002     skb->ip_summed = CHECKSUM_UNNECESSARY;
2003
2004     /* Test for an overlength frame */
2005     if (skb->len > (dev->mtu + ETH_HLEN)) {
2006         /* A bogus length ethfrm has been encap'd. */
2007         /* Is someone trying an oflow attack? */
2008         unifi_error(priv, "%s: oversize frame (%d > %d)\n",
2009                     dev->name,
2010                     skb->len, dev->mtu + ETH_HLEN);
2011
2012         /* Drop the packet and return */
2013         priv->interfacePriv[ifTag]->stats.rx_errors++;
2014         priv->interfacePriv[ifTag]->stats.rx_length_errors++;
2015         unifi_net_data_free(priv, &bulkdata->d[0]);
2016         func_exit();
2017         return;
2018     }
2019
2020
2021     if(priv->cmanrTestMode)
2022     {
2023         const CSR_MA_PACKET_INDICATION *pkt_ind = &signal->u.MaPacketIndication;
2024         priv->cmanrTestModeTransmitRate = pkt_ind->ReceivedRate;
2025         unifi_trace(priv, UDBG2, "indicate_rx_skb: cmanrTestModeTransmitRate=%d\n", priv->cmanrTestModeTransmitRate);
2026     }
2027
2028     /* Pass SKB up the stack */
2029 #ifdef CSR_WIFI_USE_NETIF_RX
2030         netif_rx(skb);
2031 #else
2032         netif_rx_ni(skb);
2033 #endif
2034
2035     if (dev != NULL) {
2036         dev->last_rx = jiffies;
2037     }
2038
2039     /* Bump rx stats */
2040     priv->interfacePriv[ifTag]->stats.rx_packets++;
2041     priv->interfacePriv[ifTag]->stats.rx_bytes += bulkdata->d[0].data_length;
2042
2043     func_exit();
2044     return;
2045 }
2046
2047 void
2048 uf_process_rx_pending_queue(unifi_priv_t *priv, int queue,
2049                             CsrWifiMacAddress source_address,
2050                             int indicate, u16 interfaceTag)
2051 {
2052     rx_buffered_packets_t *rx_q_item;
2053     struct list_head *rx_list;
2054     struct list_head *n;
2055     struct list_head *l_h;
2056     static const CsrWifiMacAddress broadcast_address = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
2057     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2058
2059     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
2060         unifi_error(priv, "uf_process_rx_pending_queue bad interfaceTag\n");
2061         return;
2062     }
2063
2064     if (queue == UF_CONTROLLED_PORT_Q) {
2065         rx_list = &interfacePriv->rx_controlled_list;
2066     } else {
2067         rx_list = &interfacePriv->rx_uncontrolled_list;
2068     }
2069
2070     down(&priv->rx_q_sem);
2071     list_for_each_safe(l_h, n, rx_list) {
2072         rx_q_item = list_entry(l_h, rx_buffered_packets_t, q);
2073
2074         /* Validate against the source address */
2075         if (memcmp(broadcast_address.a, source_address.a, ETH_ALEN) &&
2076                 memcmp(rx_q_item->sa.a, source_address.a, ETH_ALEN)) {
2077
2078             unifi_trace(priv, UDBG2,
2079                         "uf_process_rx_pending_queue: Skipping sa=%02X%02X%02X%02X%02X%02X skb=%p, bulkdata=%p\n",
2080                         rx_q_item->sa.a[0], rx_q_item->sa.a[1],
2081                         rx_q_item->sa.a[2], rx_q_item->sa.a[3],
2082                         rx_q_item->sa.a[4], rx_q_item->sa.a[5],
2083                         rx_q_item->skb, &rx_q_item->bulkdata.d[0]);
2084             continue;
2085         }
2086
2087         list_del(l_h);
2088
2089
2090         unifi_trace(priv, UDBG2,
2091                     "uf_process_rx_pending_queue: Was Blocked skb=%p, bulkdata=%p\n",
2092                     rx_q_item->skb, &rx_q_item->bulkdata);
2093
2094         if (indicate) {
2095             indicate_rx_skb(priv, interfaceTag, rx_q_item->da.a, rx_q_item->sa.a, rx_q_item->skb, &rx_q_item->signal, &rx_q_item->bulkdata);
2096         } else {
2097             interfacePriv->stats.rx_dropped++;
2098             unifi_net_data_free(priv, &rx_q_item->bulkdata.d[0]);
2099         }
2100
2101         /* It is our resposibility to free the Rx structure object. */
2102         kfree(rx_q_item);
2103     }
2104     up(&priv->rx_q_sem);
2105 }
2106
2107 /*
2108  * ---------------------------------------------------------------------------
2109  *  uf_resume_data_plane
2110  *
2111  *      Is called when the (un)controlled port is set to open,
2112  *      to notify the network stack to schedule for transmission
2113  *      any packets queued in the qdisk while port was closed and
2114  *      indicated to the stack any packets buffered in the Rx queues.
2115  *
2116  *  Arguments:
2117  *      priv        Pointer to device private struct
2118  *
2119  *  Returns:
2120  * ---------------------------------------------------------------------------
2121  */
2122 void
2123 uf_resume_data_plane(unifi_priv_t *priv, int queue,
2124                      CsrWifiMacAddress peer_address,
2125                      u16 interfaceTag)
2126 {
2127 #ifdef CSR_SUPPORT_WEXT
2128     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2129 #endif
2130
2131     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
2132         unifi_error(priv, "uf_resume_data_plane bad interfaceTag\n");
2133         return;
2134     }
2135
2136     unifi_trace(priv, UDBG2, "Resuming netif\n");
2137
2138     /*
2139      * If we are waiting for the net device to enter the up state, don't
2140      * process the rx queue yet as it will be done by the callback when
2141      * the device is ready.
2142      */
2143 #ifdef CSR_SUPPORT_WEXT
2144     if (!interfacePriv->wait_netdev_change)
2145 #endif
2146     {
2147 #ifdef CONFIG_NET_SCHED
2148         if (netif_running(priv->netdev[interfaceTag])) {
2149             netif_tx_schedule_all(priv->netdev[interfaceTag]);
2150         }
2151 #endif
2152         uf_process_rx_pending_queue(priv, queue, peer_address, 1,interfaceTag);
2153     }
2154 } /* uf_resume_data_plane() */
2155
2156
2157 void uf_free_pending_rx_packets(unifi_priv_t *priv, int queue, CsrWifiMacAddress peer_address,u16 interfaceTag)
2158 {
2159     uf_process_rx_pending_queue(priv, queue, peer_address, 0,interfaceTag);
2160
2161 } /* uf_free_pending_rx_packets() */
2162
2163
2164 /*
2165  * ---------------------------------------------------------------------------
2166  *  unifi_rx
2167  *
2168  *      Reformat a UniFi data received packet into a p80211 packet and
2169  *      pass it up the protocol stack.
2170  *
2171  *  Arguments:
2172  *      None.
2173  *
2174  *  Returns:
2175  *      None.
2176  * ---------------------------------------------------------------------------
2177  */
2178 static void
2179 unifi_rx(unifi_priv_t *priv, CSR_SIGNAL *signal, bulk_data_param_t *bulkdata)
2180 {
2181     u16 interfaceTag;
2182     bulk_data_desc_t *pData;
2183     const CSR_MA_PACKET_INDICATION *pkt_ind = &signal->u.MaPacketIndication;
2184     struct sk_buff *skb;
2185     CsrWifiRouterCtrlPortAction port_action;
2186     u8 dataFrameType;
2187     int proto;
2188     int queue;
2189
2190     u8 da[ETH_ALEN], sa[ETH_ALEN];
2191     u8 toDs, fromDs, frameType, macHeaderLengthInBytes = MAC_HEADER_SIZE;
2192     u16 frameControl;
2193     netInterface_priv_t *interfacePriv;
2194     struct ethhdr ehdr;
2195
2196     func_enter();
2197
2198     interfaceTag = (pkt_ind->VirtualInterfaceIdentifier & 0xff);
2199     interfacePriv = priv->interfacePriv[interfaceTag];
2200
2201     /* Sanity check that the VIF refers to a sensible interface */
2202     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES)
2203     {
2204         unifi_error(priv, "%s: MA-PACKET indication with bad interfaceTag %d\n", __FUNCTION__, interfaceTag);
2205         unifi_net_data_free(priv,&bulkdata->d[0]);
2206         func_exit();
2207         return;
2208     }
2209
2210     /* Sanity check that the VIF refers to an allocated netdev */
2211     if (!interfacePriv->netdev_registered)
2212     {
2213         unifi_error(priv, "%s: MA-PACKET indication with unallocated interfaceTag %d\n", __FUNCTION__, interfaceTag);
2214         unifi_net_data_free(priv, &bulkdata->d[0]);
2215         func_exit();
2216         return;
2217     }
2218
2219     if (bulkdata->d[0].data_length == 0) {
2220         unifi_warning(priv, "%s: MA-PACKET indication with zero bulk data\n", __FUNCTION__);
2221         unifi_net_data_free(priv,&bulkdata->d[0]);
2222         func_exit();
2223         return;
2224     }
2225
2226
2227     skb = (struct sk_buff*)bulkdata->d[0].os_net_buf_ptr;
2228     skb->len = bulkdata->d[0].data_length;
2229
2230     /* Point to the addresses */
2231     toDs = (skb->data[1] & 0x01) ? 1 : 0;
2232     fromDs = (skb->data[1] & 0x02) ? 1 : 0;
2233
2234     memcpy(da,(skb->data+4+toDs*12),ETH_ALEN);/* Address1 or 3 */
2235     memcpy(sa,(skb->data+10+fromDs*(6+toDs*8)),ETH_ALEN); /* Address2, 3 or 4 */
2236
2237
2238     pData = &bulkdata->d[0];
2239     frameControl = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(pData->os_data_ptr);
2240     frameType = ((frameControl & 0x000C) >> 2);
2241
2242     dataFrameType =((frameControl & 0x00f0) >> 4);
2243     unifi_trace(priv, UDBG6,
2244                 "%s: Receive Data Frame Type %d \n", __FUNCTION__,dataFrameType);
2245
2246     switch(dataFrameType)
2247     {
2248         case QOS_DATA:
2249         case QOS_DATA_NULL:
2250             /* If both are set then the Address4 exists (only for AP) */
2251             if (fromDs && toDs)
2252             {
2253                 /* 6 is the size of Address4 field */
2254                 macHeaderLengthInBytes += (QOS_CONTROL_HEADER_SIZE + 6);
2255             }
2256             else
2257             {
2258                 macHeaderLengthInBytes += QOS_CONTROL_HEADER_SIZE;
2259             }
2260
2261             /* If order bit set then HT control field is the part of MAC header */
2262             if (frameControl & FRAME_CONTROL_ORDER_BIT)
2263                 macHeaderLengthInBytes += HT_CONTROL_HEADER_SIZE;
2264             break;
2265         default:
2266             if (fromDs && toDs)
2267                 macHeaderLengthInBytes += 6;
2268     }
2269
2270     /* Prepare the ethernet header from snap header of skb data */
2271     switch(dataFrameType)
2272     {
2273         case DATA_NULL:
2274         case QOS_DATA_NULL:
2275             /* This is for only queue info fetching, EAPOL wont come as
2276              * null data so the proto is initialized as zero
2277              */
2278             proto = 0x0;
2279             break;
2280         default:
2281             {
2282                 llc_snap_hdr_t *snap;
2283                 /* Fetch a snap header to find protocol (for IPV4/IPV6 packets
2284                  * the snap header fetching offset is same)
2285                  */
2286                 snap = (llc_snap_hdr_t *) (skb->data + macHeaderLengthInBytes);
2287
2288                 /* prepare the ethernet header from the snap header & addresses */
2289                 ehdr.h_proto = snap->protocol;
2290                 memcpy(ehdr.h_dest, da, ETH_ALEN);
2291                 memcpy(ehdr.h_source, sa, ETH_ALEN);
2292             }
2293             proto = ntohs(ehdr.h_proto);
2294     }
2295     unifi_trace(priv, UDBG3, "in unifi_rx protocol from snap header = 0x%x\n", proto);
2296
2297     if ((proto != ETH_P_PAE)
2298 #ifdef CSR_WIFI_SECURITY_WAPI_ENABLE
2299             && (proto != ETH_P_WAI)
2300 #endif
2301        ) {
2302         queue = UF_CONTROLLED_PORT_Q;
2303     } else {
2304         queue = UF_UNCONTROLLED_PORT_Q;
2305     }
2306
2307     port_action = verify_port(priv, (unsigned char*)sa, queue, interfaceTag);
2308     unifi_trace(priv, UDBG3, "in unifi_rx port action is = 0x%x & queue = %x\n", port_action, queue);
2309
2310 #ifdef CSR_SUPPORT_SME
2311     /* Notify the TA module for the Rx frame for non P2PGO and AP cases*/
2312     if((interfacePriv->interfaceMode != CSR_WIFI_ROUTER_CTRL_MODE_AP) &&
2313             (interfacePriv->interfaceMode != CSR_WIFI_ROUTER_CTRL_MODE_P2PGO))
2314     {
2315         /* Remove MAC header of length(macHeaderLengthInBytes) before sampling */
2316         skb_pull(skb, macHeaderLengthInBytes);
2317         pData->os_data_ptr = skb->data;
2318         pData->data_length -= macHeaderLengthInBytes;
2319
2320         if (pData->data_length) {
2321             unifi_ta_sample(priv->card, CSR_WIFI_ROUTER_CTRL_PROTOCOL_DIRECTION_RX,
2322                             &bulkdata->d[0],
2323                             sa, priv->netdev[interfaceTag]->dev_addr,
2324                             jiffies_to_msecs(jiffies),
2325                             pkt_ind->ReceivedRate);
2326         }
2327     } else {
2328
2329         /* AP/P2PGO specific handling here */
2330         CsrWifiRouterCtrlStaInfo_t * srcStaInfo =
2331             CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv,sa,interfaceTag);
2332
2333         /* Defensive check only; Source address is already checked in
2334         process_ma_packet_ind and we should have a valid source address here */
2335
2336          if(srcStaInfo == NULL) {
2337             CsrWifiMacAddress peerMacAddress;
2338             /* Unknown data PDU */
2339             memcpy(peerMacAddress.a,sa,ETH_ALEN);
2340             unifi_trace(priv, UDBG1, "%s: Unexpected frame from peer = %x:%x:%x:%x:%x:%x\n", __FUNCTION__,
2341             sa[0], sa[1],sa[2], sa[3], sa[4],sa[5]);
2342             CsrWifiRouterCtrlUnexpectedFrameIndSend(priv->CSR_WIFI_SME_IFACEQUEUE,0,interfaceTag,peerMacAddress);
2343             unifi_net_data_free(priv, &bulkdata->d[0]);
2344             func_exit();
2345             return;
2346         }
2347
2348        /* For AP GO mode, don't store the PDUs */
2349         if (port_action != CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_OPEN) {
2350             /* Drop the packet and return */
2351             CsrWifiMacAddress peerMacAddress;
2352             memcpy(peerMacAddress.a,sa,ETH_ALEN);
2353             unifi_trace(priv, UDBG3, "%s: Port is not open: unexpected frame from peer = %x:%x:%x:%x:%x:%x\n",
2354                         __FUNCTION__, sa[0], sa[1],sa[2], sa[3], sa[4],sa[5]);
2355
2356             CsrWifiRouterCtrlUnexpectedFrameIndSend(priv->CSR_WIFI_SME_IFACEQUEUE,0,interfaceTag,peerMacAddress);
2357             interfacePriv->stats.rx_dropped++;
2358             unifi_net_data_free(priv, &bulkdata->d[0]);
2359             unifi_notice(priv, "%s: Dropping packet, proto=0x%04x, %s port\n", __FUNCTION__,
2360                          proto, queue ? "Controlled" : "Un-controlled");
2361             func_exit();
2362             return;
2363         }
2364
2365          /* Qos NULL/Data NULL  are freed here and not processed further */
2366         if((dataFrameType == QOS_DATA_NULL) || (dataFrameType == DATA_NULL)){
2367             unifi_trace(priv, UDBG5, "%s: Null Frame Received and Freed\n", __FUNCTION__);
2368             unifi_net_data_free(priv, &bulkdata->d[0]);
2369             func_exit();
2370             return;
2371         }
2372
2373         /* Now we have done with MAC header so proceed with the real data part*/
2374         /* This function takes care of appropriate routing for AP/P2PGO case*/
2375         /* the function hadnles following things
2376            2. Routing the PDU to appropriate location
2377            3. Error case handling
2378            */
2379         if(!(uf_ap_process_data_pdu(priv, skb, &ehdr, srcStaInfo,
2380              signal,
2381              bulkdata,
2382              macHeaderLengthInBytes)))
2383         {
2384             func_exit();
2385             return;
2386         }
2387         unifi_trace(priv, UDBG5, "unifi_rx: no specific AP handling process as normal frame, MAC Header len %d\n",macHeaderLengthInBytes);
2388         /* Remove the MAC header for subsequent conversion */
2389         skb_pull(skb, macHeaderLengthInBytes);
2390         pData->os_data_ptr = skb->data;
2391         pData->data_length -= macHeaderLengthInBytes;
2392         pData->os_net_buf_ptr = (unsigned char*)skb;
2393         pData->net_buf_length = skb->len;
2394     }
2395 #endif /* CSR_SUPPORT_SME */
2396
2397
2398     /* Now that the MAC header is removed, null-data frames have zero length
2399      * and can be dropped
2400      */
2401     if (pData->data_length == 0) {
2402         if (((frameControl & 0x00f0) >> 4) != QOS_DATA_NULL &&
2403             ((frameControl & 0x00f0) >> 4) != DATA_NULL) {
2404             unifi_trace(priv, UDBG1, "Zero length frame, but not null-data %04x\n", frameControl);
2405         }
2406         unifi_net_data_free(priv, &bulkdata->d[0]);
2407         func_exit();
2408         return;
2409     }
2410
2411     if (port_action == CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_CLOSED_DISCARD) {
2412         /* Drop the packet and return */
2413         interfacePriv->stats.rx_dropped++;
2414         unifi_net_data_free(priv, &bulkdata->d[0]);
2415         unifi_notice(priv, "%s: Dropping packet, proto=0x%04x, %s port\n",
2416                      __FUNCTION__, proto, queue ? "controlled" : "uncontrolled");
2417         func_exit();
2418         return;
2419     } else if ( (port_action == CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_CLOSED_BLOCK) ||
2420                    (interfacePriv->connected != UnifiConnected) ) {
2421
2422         /* Buffer the packet into the Rx queues */
2423         rx_buffered_packets_t *rx_q_item;
2424         struct list_head *rx_list;
2425
2426         rx_q_item = (rx_buffered_packets_t *)kmalloc(sizeof(rx_buffered_packets_t),
2427                 GFP_KERNEL);
2428         if (rx_q_item == NULL) {
2429             unifi_error(priv, "%s: Failed to allocate %d bytes for rx packet record\n",
2430                         __FUNCTION__, sizeof(rx_buffered_packets_t));
2431             interfacePriv->stats.rx_dropped++;
2432             unifi_net_data_free(priv, &bulkdata->d[0]);
2433             func_exit();
2434             return;
2435         }
2436
2437         INIT_LIST_HEAD(&rx_q_item->q);
2438         rx_q_item->bulkdata = *bulkdata;
2439         rx_q_item->skb = skb;
2440         rx_q_item->signal = *signal;
2441         memcpy(rx_q_item->sa.a, sa, ETH_ALEN);
2442         memcpy(rx_q_item->da.a, da, ETH_ALEN);
2443         unifi_trace(priv, UDBG2, "%s: Blocked skb=%p, bulkdata=%p\n",
2444                     __FUNCTION__, rx_q_item->skb, &rx_q_item->bulkdata);
2445
2446         if (queue == UF_CONTROLLED_PORT_Q) {
2447             rx_list = &interfacePriv->rx_controlled_list;
2448         } else {
2449             rx_list = &interfacePriv->rx_uncontrolled_list;
2450         }
2451
2452         /* Add to tail of packets queue */
2453         down(&priv->rx_q_sem);
2454         list_add_tail(&rx_q_item->q, rx_list);
2455         up(&priv->rx_q_sem);
2456
2457         func_exit();
2458         return;
2459
2460     }
2461
2462     indicate_rx_skb(priv, interfaceTag, da, sa, skb, signal, bulkdata);
2463
2464     func_exit();
2465
2466 } /* unifi_rx() */
2467
2468 static void process_ma_packet_cfm(unifi_priv_t *priv, CSR_SIGNAL *signal, bulk_data_param_t *bulkdata)
2469 {
2470     u16 interfaceTag;
2471     const CSR_MA_PACKET_CONFIRM *pkt_cfm = &signal->u.MaPacketConfirm;
2472     netInterface_priv_t *interfacePriv;
2473
2474     func_enter();
2475     interfaceTag = (pkt_cfm->VirtualInterfaceIdentifier & 0xff);
2476     interfacePriv = priv->interfacePriv[interfaceTag];
2477
2478     /* Sanity check that the VIF refers to a sensible interface */
2479     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES)
2480     {
2481         unifi_error(priv, "%s: MA-PACKET confirm with bad interfaceTag %d\n", __FUNCTION__, interfaceTag);
2482         func_exit();
2483         return;
2484     }
2485 #ifdef CSR_SUPPORT_SME
2486     if(interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_AP ||
2487        interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PGO) {
2488
2489         uf_process_ma_pkt_cfm_for_ap(priv,interfaceTag,pkt_cfm);
2490     } else if (interfacePriv->m4_sent && (pkt_cfm->HostTag == interfacePriv->m4_hostTag)) {
2491         /* Check if this is a confirm for EAPOL M4 frame and we need to send transmistted ind*/
2492         CsrResult result = pkt_cfm->TransmissionStatus == CSR_TX_SUCCESSFUL?CSR_RESULT_SUCCESS:CSR_RESULT_FAILURE;
2493         CsrWifiMacAddress peerMacAddress;
2494         memcpy(peerMacAddress.a, interfacePriv->m4_signal.u.MaPacketRequest.Ra.x, ETH_ALEN);
2495
2496         unifi_trace(priv, UDBG1, "%s: Sending M4 Transmit CFM\n", __FUNCTION__);
2497         CsrWifiRouterCtrlM4TransmittedIndSend(priv->CSR_WIFI_SME_IFACEQUEUE, 0,
2498                                               interfaceTag,
2499                                               peerMacAddress,
2500                                               result);
2501         interfacePriv->m4_sent = FALSE;
2502         interfacePriv->m4_hostTag = 0xffffffff;
2503     }
2504 #endif
2505     func_exit();
2506     return;
2507 }
2508
2509
2510 /*
2511  * ---------------------------------------------------------------------------
2512  *  unifi_rx
2513  *
2514  *      Reformat a UniFi data received packet into a p80211 packet and
2515  *      pass it up the protocol stack.
2516  *
2517  *  Arguments:
2518  *      None.
2519  *
2520  *  Returns:
2521  *      None.
2522  * ---------------------------------------------------------------------------
2523  */
2524 static void process_ma_packet_ind(unifi_priv_t *priv, CSR_SIGNAL *signal, bulk_data_param_t *bulkdata)
2525 {
2526     u16 interfaceTag;
2527     bulk_data_desc_t *pData;
2528     CSR_MA_PACKET_INDICATION *pkt_ind = (CSR_MA_PACKET_INDICATION*)&signal->u.MaPacketIndication;
2529     struct sk_buff *skb;
2530     u16 frameControl;
2531     netInterface_priv_t *interfacePriv;
2532     u8 da[ETH_ALEN], sa[ETH_ALEN];
2533     u8 *bssid = NULL, *ba_addr = NULL;
2534     u8 toDs, fromDs, frameType;
2535     u8 i =0;
2536
2537 #ifdef CSR_SUPPORT_SME
2538     u8 dataFrameType = 0;
2539     u8 powerSaveChanged = FALSE;
2540     u8 pmBit = 0;
2541     CsrWifiRouterCtrlStaInfo_t *srcStaInfo = NULL;
2542     u16 qosControl;
2543
2544 #endif
2545
2546     func_enter();
2547
2548     interfaceTag = (pkt_ind->VirtualInterfaceIdentifier & 0xff);
2549     interfacePriv = priv->interfacePriv[interfaceTag];
2550
2551
2552     /* Sanity check that the VIF refers to a sensible interface */
2553     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES)
2554     {
2555         unifi_error(priv, "%s: MA-PACKET indication with bad interfaceTag %d\n", __FUNCTION__, interfaceTag);
2556         unifi_net_data_free(priv,&bulkdata->d[0]);
2557         func_exit();
2558         return;
2559     }
2560
2561     /* Sanity check that the VIF refers to an allocated netdev */
2562     if (!interfacePriv->netdev_registered)
2563     {
2564         unifi_error(priv, "%s: MA-PACKET indication with unallocated interfaceTag %d\n", __FUNCTION__, interfaceTag);
2565         unifi_net_data_free(priv, &bulkdata->d[0]);
2566         func_exit();
2567         return;
2568     }
2569
2570     if (bulkdata->d[0].data_length == 0) {
2571         unifi_warning(priv, "%s: MA-PACKET indication with zero bulk data\n", __FUNCTION__);
2572         unifi_net_data_free(priv,&bulkdata->d[0]);
2573         func_exit();
2574         return;
2575     }
2576     /* For monitor mode we need to pass this indication to the registered application
2577     handle this seperately*/
2578     /* MIC failure is already taken care of so no need to send the PDUs which are not successfully received in non-monitor mode*/
2579     if(pkt_ind->ReceptionStatus != CSR_RX_SUCCESS)
2580     {
2581         unifi_warning(priv, "%s: MA-PACKET indication with status = %d\n",__FUNCTION__, pkt_ind->ReceptionStatus);
2582         unifi_net_data_free(priv,&bulkdata->d[0]);
2583         func_exit();
2584         return;
2585     }
2586
2587
2588     skb = (struct sk_buff*)bulkdata->d[0].os_net_buf_ptr;
2589     skb->len = bulkdata->d[0].data_length;
2590
2591     /* Point to the addresses */
2592     toDs = (skb->data[1] & 0x01) ? 1 : 0;
2593     fromDs = (skb->data[1] & 0x02) ? 1 : 0;
2594
2595     memcpy(da,(skb->data+4+toDs*12),ETH_ALEN);/* Address1 or 3 */
2596     memcpy(sa,(skb->data+10+fromDs*(6+toDs*8)),ETH_ALEN); /* Address2, 3 or 4 */
2597
2598     /* Find the BSSID, which will be used to match the BA session */
2599     if (toDs && fromDs)
2600     {
2601         unifi_trace(priv, UDBG6, "4 address frame - don't try to find BSSID\n");
2602         bssid = NULL;
2603     }
2604     else
2605     {
2606         bssid = (u8 *) (skb->data + 4 + 12 - (fromDs * 6) - (toDs * 12));
2607     }
2608
2609     pData = &bulkdata->d[0];
2610     frameControl = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(pData->os_data_ptr);
2611     frameType = ((frameControl & 0x000C) >> 2);
2612
2613     unifi_trace(priv, UDBG3, "Rx Frame Type: %d sn: %d\n",frameType,
2614          (le16_to_cpu(*((u16*)(bulkdata->d[0].os_data_ptr + IEEE802_11_SEQUENCE_CONTROL_OFFSET))) >> 4) & 0xfff);
2615     if(frameType == IEEE802_11_FRAMETYPE_CONTROL){
2616 #ifdef CSR_SUPPORT_SME
2617         unifi_trace(priv, UDBG6, "%s: Received Control Frame\n", __FUNCTION__);
2618
2619         if((frameControl & 0x00f0) == 0x00A0){
2620             /* This is a PS-POLL request */
2621             u8 pmBit = (frameControl & 0x1000)?0x01:0x00;
2622             unifi_trace(priv, UDBG6, "%s: Received PS-POLL Frame\n", __FUNCTION__);
2623
2624             uf_process_ps_poll(priv,sa,da,pmBit,interfaceTag);
2625         }
2626         else {
2627             unifi_warning(priv, "%s: Non PS-POLL control frame is received\n", __FUNCTION__);
2628         }
2629 #endif
2630         unifi_net_data_free(priv,&bulkdata->d[0]);
2631         func_exit();
2632         return;
2633     }
2634     if(frameType != IEEE802_11_FRAMETYPE_DATA) {
2635         unifi_warning(priv, "%s: Non control Non Data frame is received\n",__FUNCTION__);
2636         unifi_net_data_free(priv,&bulkdata->d[0]);
2637         func_exit();
2638         return;
2639     }
2640
2641 #ifdef CSR_SUPPORT_SME
2642     if((interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_AP) ||
2643        (interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PGO)){
2644
2645         srcStaInfo = CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv,sa,interfaceTag);
2646
2647         if(srcStaInfo == NULL) {
2648             CsrWifiMacAddress peerMacAddress;
2649             /* Unknown data PDU */
2650             memcpy(peerMacAddress.a,sa,ETH_ALEN);
2651             unifi_trace(priv, UDBG1, "%s: Unexpected frame from peer = %x:%x:%x:%x:%x:%x\n", __FUNCTION__,
2652             sa[0], sa[1],sa[2], sa[3], sa[4],sa[5]);
2653             CsrWifiRouterCtrlUnexpectedFrameIndSend(priv->CSR_WIFI_SME_IFACEQUEUE,0,interfaceTag,peerMacAddress);
2654             unifi_net_data_free(priv, &bulkdata->d[0]);
2655             func_exit();
2656             return;
2657         }
2658
2659         /*
2660         verify power management bit here so as to ensure host and unifi are always
2661         in sync with power management status of peer.
2662
2663         If we do it later, it may so happen we have stored the frame in BA re-ordering
2664         buffer and hence host and unifi are out of sync for power management status
2665         */
2666
2667         pmBit = (frameControl & 0x1000)?0x01:0x00;
2668         powerSaveChanged = uf_process_pm_bit_for_peer(priv,srcStaInfo,pmBit,interfaceTag);
2669
2670         /* Update station last activity time */
2671         srcStaInfo->activity_flag = TRUE;
2672
2673         /* For Qos Frame if PM bit is toggled to indicate the change in power save state then it shall not be
2674         considered as Trigger Frame. Enter only if WMM STA and peer is in Power save */
2675
2676         dataFrameType = ((frameControl & 0x00f0) >> 4);
2677
2678         if((powerSaveChanged == FALSE)&&(srcStaInfo->wmmOrQosEnabled == TRUE)&&
2679         (srcStaInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_POWER_SAVE)){
2680
2681             if((dataFrameType == QOS_DATA) || (dataFrameType == QOS_DATA_NULL)){
2682
2683                 /*
2684                  * QoS control field is offset from frame control by 2 (frame control)
2685                  * + 2 (duration/ID) + 2 (sequence control) + 3*ETH_ALEN or 4*ETH_ALEN
2686                  */
2687                 if((frameControl & IEEE802_11_FC_TO_DS_MASK) && (frameControl & IEEE802_11_FC_FROM_DS_MASK)){
2688                     qosControl= CSR_GET_UINT16_FROM_LITTLE_ENDIAN(pData->os_data_ptr + 30);
2689                 }
2690                 else{
2691                     qosControl = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(pData->os_data_ptr + 24);
2692                 }
2693                 unifi_trace(priv, UDBG5, "%s: Check if U-APSD operations are triggered for qosControl: 0x%x\n",__FUNCTION__,qosControl);
2694                 uf_process_wmm_deliver_ac_uapsd(priv,srcStaInfo,qosControl,interfaceTag);
2695             }
2696         }
2697     }
2698
2699 #endif
2700
2701     if( ((frameControl & 0x00f0) >> 4) == QOS_DATA) {
2702         u8 *qos_control_ptr = (u8*)bulkdata->d[0].os_data_ptr + (((frameControl & IEEE802_11_FC_TO_DS_MASK) && (frameControl & IEEE802_11_FC_FROM_DS_MASK))?30: 24);
2703         int tID = *qos_control_ptr & IEEE802_11_QC_TID_MASK; /* using ls octet of qos control */
2704         ba_session_rx_struct *ba_session;
2705         u8 ba_session_idx = 0;
2706         /* Get the BA originator address */
2707         if(interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_AP ||
2708            interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PGO){
2709             ba_addr = sa;
2710         }else{
2711             ba_addr = bssid;
2712         }
2713
2714         down(&priv->ba_mutex);
2715         for (ba_session_idx=0; ba_session_idx < MAX_SUPPORTED_BA_SESSIONS_RX; ba_session_idx++){
2716             ba_session = interfacePriv->ba_session_rx[ba_session_idx];
2717             if (ba_session){
2718                 unifi_trace(priv, UDBG6, "found ba_session=0x%x ba_session_idx=%d", ba_session, ba_session_idx);
2719                 if ((!memcmp(ba_session->macAddress.a, ba_addr, ETH_ALEN)) && (ba_session->tID == tID)){
2720                         frame_desc_struct frame_desc;
2721                         frame_desc.bulkdata = *bulkdata;
2722                         frame_desc.signal = *signal;
2723                         frame_desc.sn = (le16_to_cpu(*((u16*)(bulkdata->d[0].os_data_ptr + IEEE802_11_SEQUENCE_CONTROL_OFFSET))) >> 4) & 0xfff;
2724                         frame_desc.active = TRUE;
2725                         unifi_trace(priv, UDBG6, "%s: calling process_ba_frame (session=%d)\n", __FUNCTION__, ba_session_idx);
2726                         process_ba_frame(priv, interfacePriv, ba_session, &frame_desc);
2727                         up(&priv->ba_mutex);
2728                         process_ba_complete(priv, interfacePriv);
2729                         break;
2730                 }
2731             }
2732         }
2733         if (ba_session_idx == MAX_SUPPORTED_BA_SESSIONS_RX){
2734             up(&priv->ba_mutex);
2735             unifi_trace(priv, UDBG6, "%s: calling process_amsdu()", __FUNCTION__);
2736             process_amsdu(priv, signal, bulkdata);
2737         }
2738     } else {
2739         unifi_trace(priv, UDBG6, "calling unifi_rx()");
2740         unifi_rx(priv, signal, bulkdata);
2741     }
2742
2743     /* check if the frames in reorder buffer has aged, the check
2744      * is done after receive processing so that if the missing frame
2745      * has arrived in this receive process, then it is handled cleanly.
2746      *
2747      * And also this code here takes care that timeout check is made for all
2748      * the receive indications
2749      */
2750     down(&priv->ba_mutex);
2751     for (i=0; i < MAX_SUPPORTED_BA_SESSIONS_RX; i++){
2752         ba_session_rx_struct *ba_session;
2753         ba_session = interfacePriv->ba_session_rx[i];
2754             if (ba_session){
2755                 check_ba_frame_age_timeout(priv, interfacePriv, ba_session);
2756             }
2757     }
2758     up(&priv->ba_mutex);
2759     process_ba_complete(priv, interfacePriv);
2760
2761     func_exit();
2762 }
2763 /*
2764  * ---------------------------------------------------------------------------
2765  *  uf_set_multicast_list
2766  *
2767  *      This function is called by the higher level stack to set
2768  *      a list of multicast rx addresses.
2769  *
2770  *  Arguments:
2771  *      dev             Network Device pointer.
2772  *
2773  *  Returns:
2774  *      None.
2775  *
2776  *  Notes:
2777  * ---------------------------------------------------------------------------
2778  */
2779
2780 static void
2781 uf_set_multicast_list(struct net_device *dev)
2782 {
2783     netInterface_priv_t *interfacePriv = (netInterface_priv_t *)netdev_priv(dev);
2784     unifi_priv_t *priv = interfacePriv->privPtr;
2785
2786 #ifdef CSR_NATIVE_LINUX
2787     unifi_trace(priv, UDBG3, "uf_set_multicast_list unsupported\n");
2788     return;
2789 #else
2790
2791     u8 *mc_list = interfacePriv->mc_list;
2792     struct netdev_hw_addr *mc_addr;
2793     int mc_addr_count;
2794
2795     if (priv->init_progress != UNIFI_INIT_COMPLETED) {
2796         return;
2797     }
2798
2799     mc_addr_count = netdev_mc_count(dev);
2800
2801     unifi_trace(priv, UDBG3,
2802             "uf_set_multicast_list (count=%d)\n", mc_addr_count);
2803
2804
2805     /* Not enough space? */
2806     if (mc_addr_count > UNIFI_MAX_MULTICAST_ADDRESSES) {
2807         return;
2808     }
2809
2810     /* Store the list to be processed by the work item. */
2811     interfacePriv->mc_list_count = mc_addr_count;
2812     netdev_hw_addr_list_for_each(mc_addr, &dev->mc) {
2813         memcpy(mc_list, mc_addr->addr, ETH_ALEN);
2814         mc_list += ETH_ALEN;
2815     }
2816
2817     /* Send a message to the workqueue */
2818     queue_work(priv->unifi_workqueue, &priv->multicast_list_task);
2819 #endif
2820
2821 } /* uf_set_multicast_list() */
2822
2823 /*
2824  * ---------------------------------------------------------------------------
2825  *  netdev_mlme_event_handler
2826  *
2827  *      Callback function to be used as the udi_event_callback when registering
2828  *      as a netdev client.
2829  *      To use it, a client specifies this function as the udi_event_callback
2830  *      to ul_register_client(). The signal dispatcher in
2831  *      unifi_receive_event() will call this function to deliver a signal.
2832  *
2833  *  Arguments:
2834  *      pcli            Pointer to the client instance.
2835  *      signal          Pointer to the received signal.
2836  *      signal_len      Size of the signal structure in bytes.
2837  *      bulkdata        Pointer to structure containing any associated bulk data.
2838  *      dir             Direction of the signal. Zero means from host,
2839  *                      non-zero means to host.
2840  *
2841  *  Returns:
2842  *      None.
2843  * ---------------------------------------------------------------------------
2844  */
2845 static void
2846 netdev_mlme_event_handler(ul_client_t *pcli, const u8 *sig_packed, int sig_len,
2847                           const bulk_data_param_t *bulkdata_o, int dir)
2848 {
2849     CSR_SIGNAL signal;
2850     unifi_priv_t *priv = uf_find_instance(pcli->instance);
2851     int id, r;
2852     bulk_data_param_t bulkdata;
2853
2854     func_enter();
2855
2856     /* Just a sanity check */
2857     if (sig_packed == NULL) {
2858         return;
2859     }
2860
2861     /*
2862      * This copy is to silence a compiler warning about discarding the
2863      * const qualifier.
2864      */
2865     bulkdata = *bulkdata_o;
2866
2867     /* Get the unpacked signal */
2868     r = read_unpack_signal(sig_packed, &signal);
2869     if (r) {
2870         /*
2871          * The CSR_MLME_CONNECTED_INDICATION_ID has a receiverID=0 so will
2872          * fall through this case. It is safe to ignore this signal.
2873          */
2874         unifi_trace(priv, UDBG1,
2875                     "Netdev - Received unknown signal 0x%.4X.\n",
2876                     CSR_GET_UINT16_FROM_LITTLE_ENDIAN(sig_packed));
2877         return;
2878     }
2879
2880     id = signal.SignalPrimitiveHeader.SignalId;
2881     unifi_trace(priv, UDBG3, "Netdev - Process signal 0x%.4X\n", id);
2882
2883     /*
2884      * Take the appropriate action for the signal.
2885      */
2886     switch (id) {
2887         case CSR_MA_PACKET_ERROR_INDICATION_ID:
2888             process_ma_packet_error_ind(priv, &signal, &bulkdata);
2889             break;
2890         case CSR_MA_PACKET_INDICATION_ID:
2891             process_ma_packet_ind(priv, &signal, &bulkdata);
2892             break;
2893         case  CSR_MA_PACKET_CONFIRM_ID:
2894             process_ma_packet_cfm(priv, &signal, &bulkdata);
2895             break;
2896 #ifdef CSR_SUPPORT_SME
2897         case CSR_MLME_SET_TIM_CONFIRM_ID:
2898             /* Handle TIM confirms from FW & set the station record's TIM state appropriately,
2899              * In case of failures, tries with max_retransmit limit
2900              */
2901             uf_handle_tim_cfm(priv, &signal.u.MlmeSetTimConfirm, signal.SignalPrimitiveHeader.ReceiverProcessId);
2902             break;
2903 #endif
2904         case CSR_DEBUG_STRING_INDICATION_ID:
2905             debug_string_indication(priv, bulkdata.d[0].os_data_ptr, bulkdata.d[0].data_length);
2906             break;
2907
2908         case CSR_DEBUG_WORD16_INDICATION_ID:
2909             debug_word16_indication(priv, &signal);
2910             break;
2911
2912         case CSR_DEBUG_GENERIC_CONFIRM_ID:
2913         case CSR_DEBUG_GENERIC_INDICATION_ID:
2914             debug_generic_indication(priv, &signal);
2915             break;
2916         default:
2917             break;
2918     }
2919
2920     func_exit();
2921 } /* netdev_mlme_event_handler() */
2922
2923
2924 /*
2925  * ---------------------------------------------------------------------------
2926  *  uf_net_get_name
2927  *
2928  *      Retrieve the name (e.g. eth1) associated with this network device
2929  *
2930  *  Arguments:
2931  *      dev             Pointer to the network device.
2932  *      name            Buffer to write name
2933  *      len             Size of buffer in bytes
2934  *
2935  *  Returns:
2936  *      None
2937  *
2938  *  Notes:
2939  * ---------------------------------------------------------------------------
2940  */
2941 void uf_net_get_name(struct net_device *dev, char *name, int len)
2942 {
2943     *name = '\0';
2944     if (dev) {
2945         strlcpy(name, dev->name, (len > IFNAMSIZ) ? IFNAMSIZ : len);
2946     }
2947
2948 } /* uf_net_get_name */
2949
2950 #ifdef CSR_SUPPORT_WEXT
2951
2952 /*
2953  * ---------------------------------------------------------------------------
2954  *  uf_netdev_event
2955  *
2956  *     Callback function to handle netdev state changes
2957  *
2958  *  Arguments:
2959  *      notif           Pointer to a notifier_block.
2960  *      event           Event prompting notification
2961  *      ptr             net_device pointer
2962  *
2963  *  Returns:
2964  *      None
2965  *
2966  *  Notes:
2967  *   The event handler is global, and may occur on non-UniFi netdevs.
2968  * ---------------------------------------------------------------------------
2969  */
2970 static int
2971 uf_netdev_event(struct notifier_block *notif, unsigned long event, void* ptr) {
2972     struct net_device *netdev = ptr;
2973     netInterface_priv_t *interfacePriv = (netInterface_priv_t *)netdev_priv(netdev);
2974     unifi_priv_t *priv = NULL;
2975     static const CsrWifiMacAddress broadcast_address = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
2976
2977     /* Check that the event is for a UniFi netdev. If it's not, the netdev_priv
2978      * structure is not safe to use.
2979      */
2980     if (uf_find_netdev_priv(interfacePriv) == -1) {
2981         unifi_trace(NULL, UDBG1, "uf_netdev_event: ignore e=%d, ptr=%p, priv=%p %s\n",
2982                     event, ptr, interfacePriv, netdev->name);
2983         return 0;
2984     }
2985
2986     switch(event) {
2987     case NETDEV_CHANGE:
2988         priv = interfacePriv->privPtr;
2989         unifi_trace(priv, UDBG1, "NETDEV_CHANGE: %p %s %s waiting for it\n",
2990                     ptr,
2991                     netdev->name,
2992                     interfacePriv->wait_netdev_change ? "" : "not");
2993
2994         if (interfacePriv->wait_netdev_change) {
2995             UF_NETIF_TX_WAKE_ALL_QUEUES(priv->netdev[interfacePriv->InterfaceTag]);
2996             interfacePriv->connected = UnifiConnected;
2997             interfacePriv->wait_netdev_change = FALSE;
2998             /* Note: passing the broadcast address here will allow anyone to attempt to join our adhoc network */
2999             uf_process_rx_pending_queue(priv, UF_UNCONTROLLED_PORT_Q, broadcast_address, 1,interfacePriv->InterfaceTag);
3000             uf_process_rx_pending_queue(priv, UF_CONTROLLED_PORT_Q, broadcast_address, 1,interfacePriv->InterfaceTag);
3001         }
3002         break;
3003
3004     default:
3005         break;
3006     }
3007     return 0;
3008 }
3009
3010 static struct notifier_block uf_netdev_notifier = {
3011     .notifier_call = uf_netdev_event,
3012 };
3013 #endif /* CSR_SUPPORT_WEXT */
3014
3015
3016 static void
3017         process_amsdu(unifi_priv_t *priv, CSR_SIGNAL *signal, bulk_data_param_t *bulkdata)
3018 {
3019     u32 offset;
3020     u32 length = bulkdata->d[0].data_length;
3021     u32 subframe_length, subframe_body_length, dot11_hdr_size;
3022     u8 *ptr;
3023     bulk_data_param_t subframe_bulkdata;
3024     u8 *dot11_hdr_ptr = (u8*)bulkdata->d[0].os_data_ptr;
3025     CsrResult csrResult;
3026     u16 frameControl;
3027     u8 *qos_control_ptr;
3028
3029     frameControl = le16_to_cpu(*((u16*)dot11_hdr_ptr));
3030     qos_control_ptr = dot11_hdr_ptr + (((frameControl & IEEE802_11_FC_TO_DS_MASK) && (frameControl & IEEE802_11_FC_FROM_DS_MASK))?30: 24);
3031     if(!(*qos_control_ptr & IEEE802_11_QC_A_MSDU_PRESENT)) {
3032         unifi_trace(priv, UDBG6, "%s: calling unifi_rx()", __FUNCTION__);
3033         unifi_rx(priv, signal, bulkdata);
3034         return;
3035     }
3036     *qos_control_ptr &= ~(IEEE802_11_QC_A_MSDU_PRESENT);
3037
3038     ptr = qos_control_ptr + 2;
3039     offset = dot11_hdr_size = ptr - dot11_hdr_ptr;
3040
3041     while(length > (offset + sizeof(struct ethhdr) + sizeof(llc_snap_hdr_t))) {
3042         subframe_body_length = ntohs(((struct ethhdr*)ptr)->h_proto);
3043         if(subframe_body_length > IEEE802_11_MAX_DATA_LEN) {
3044             unifi_error(priv, "%s: bad subframe_body_length = %d\n", __FUNCTION__, subframe_body_length);
3045             break;
3046         }
3047         subframe_length = sizeof(struct ethhdr) + subframe_body_length;
3048         memset(&subframe_bulkdata, 0, sizeof(bulk_data_param_t));
3049
3050         csrResult = unifi_net_data_malloc(priv, &subframe_bulkdata.d[0], dot11_hdr_size + subframe_body_length);
3051
3052         if (csrResult != CSR_RESULT_SUCCESS) {
3053             unifi_error(priv, "%s: unifi_net_data_malloc failed\n", __FUNCTION__);
3054             break;
3055         }
3056
3057         memcpy((u8*)subframe_bulkdata.d[0].os_data_ptr, dot11_hdr_ptr, dot11_hdr_size);
3058
3059
3060         /* When to DS=0 and from DS=0, address 3 will already have BSSID so no need to re-program */
3061         if ((frameControl & IEEE802_11_FC_TO_DS_MASK) && !(frameControl & IEEE802_11_FC_FROM_DS_MASK)){
3062                 memcpy((u8*)subframe_bulkdata.d[0].os_data_ptr + IEEE802_11_ADDR3_OFFSET, ((struct ethhdr*)ptr)->h_dest, ETH_ALEN);
3063         }
3064         else if (!(frameControl & IEEE802_11_FC_TO_DS_MASK) && (frameControl & IEEE802_11_FC_FROM_DS_MASK)){
3065                 memcpy((u8*)subframe_bulkdata.d[0].os_data_ptr + IEEE802_11_ADDR3_OFFSET,
3066                          ((struct ethhdr*)ptr)->h_source,
3067                            ETH_ALEN);
3068         }
3069
3070         memcpy((u8*)subframe_bulkdata.d[0].os_data_ptr + dot11_hdr_size,
3071                 ptr + sizeof(struct ethhdr),
3072                              subframe_body_length);
3073         unifi_trace(priv, UDBG6, "%s: calling unifi_rx. length = %d subframe_length = %d\n", __FUNCTION__, length, subframe_length);
3074         unifi_rx(priv, signal, &subframe_bulkdata);
3075
3076         subframe_length = (subframe_length + 3)&(~0x3);
3077         ptr += subframe_length;
3078         offset += subframe_length;
3079     }
3080     unifi_net_data_free(priv, &bulkdata->d[0]);
3081 }
3082
3083
3084 #define SN_TO_INDEX(__ba_session, __sn) (((__sn - __ba_session->start_sn) & 0xFFF) % __ba_session->wind_size)
3085
3086
3087 #define ADVANCE_EXPECTED_SN(__ba_session) \
3088 { \
3089     __ba_session->expected_sn++; \
3090     __ba_session->expected_sn &= 0xFFF; \
3091 }
3092
3093 #define FREE_BUFFER_SLOT(__ba_session, __index) \
3094 { \
3095     __ba_session->occupied_slots--; \
3096     __ba_session->buffer[__index].active = FALSE; \
3097     ADVANCE_EXPECTED_SN(__ba_session); \
3098 }
3099
3100 static void add_frame_to_ba_complete(unifi_priv_t *priv,
3101                           netInterface_priv_t *interfacePriv,
3102                           frame_desc_struct *frame_desc)
3103 {
3104     interfacePriv->ba_complete[interfacePriv->ba_complete_index] = *frame_desc;
3105     interfacePriv->ba_complete_index++;
3106 }
3107
3108
3109 static void update_expected_sn(unifi_priv_t *priv,
3110                           netInterface_priv_t *interfacePriv,
3111                           ba_session_rx_struct *ba_session,
3112                           u16 sn)
3113 {
3114     int i, j;
3115     u16 gap;
3116
3117     gap = (sn - ba_session->expected_sn) & 0xFFF;
3118     unifi_trace(priv, UDBG6, "%s: proccess the frames up to new_expected_sn = %d gap = %d\n", __FUNCTION__, sn, gap);
3119     for(j = 0; j < gap && j < ba_session->wind_size; j++) {
3120         i = SN_TO_INDEX(ba_session, ba_session->expected_sn);
3121         unifi_trace(priv, UDBG6, "%s: proccess the slot index = %d\n", __FUNCTION__, i);
3122         if(ba_session->buffer[i].active) {
3123             add_frame_to_ba_complete(priv, interfacePriv, &ba_session->buffer[i]);
3124             unifi_trace(priv, UDBG6, "%s: proccess the frame at index = %d expected_sn = %d\n", __FUNCTION__, i, ba_session->expected_sn);
3125             FREE_BUFFER_SLOT(ba_session, i);
3126         } else {
3127             unifi_trace(priv, UDBG6, "%s: empty slot at index = %d\n", __FUNCTION__, i);
3128             ADVANCE_EXPECTED_SN(ba_session);
3129         }
3130     }
3131     ba_session->expected_sn = sn;
3132 }
3133
3134
3135 static void complete_ready_sequence(unifi_priv_t *priv,
3136                                netInterface_priv_t *interfacePriv,
3137                                ba_session_rx_struct *ba_session)
3138 {
3139     int i;
3140
3141     i = SN_TO_INDEX(ba_session, ba_session->expected_sn);
3142     while (ba_session->buffer[i].active) {
3143         add_frame_to_ba_complete(priv, interfacePriv, &ba_session->buffer[i]);
3144         unifi_trace(priv, UDBG6, "%s: completed stored frame(expected_sn=%d) at i = %d\n", __FUNCTION__, ba_session->expected_sn, i);
3145         FREE_BUFFER_SLOT(ba_session, i);
3146         i = SN_TO_INDEX(ba_session, ba_session->expected_sn);
3147     }
3148 }
3149
3150
3151 void scroll_ba_window(unifi_priv_t *priv,
3152                                 netInterface_priv_t *interfacePriv,
3153                                 ba_session_rx_struct *ba_session,
3154                                 u16 sn)
3155 {
3156     if(((sn - ba_session->expected_sn) & 0xFFF) <= 2048) {
3157         update_expected_sn(priv, interfacePriv, ba_session, sn);
3158         complete_ready_sequence(priv, interfacePriv, ba_session);
3159     }
3160 }
3161
3162
3163 static int consume_frame_or_get_buffer_index(unifi_priv_t *priv,
3164                                             netInterface_priv_t *interfacePriv,
3165                                             ba_session_rx_struct *ba_session,
3166                                             u16 sn,
3167                                             frame_desc_struct *frame_desc) {
3168     int i;
3169     u16 sn_temp;
3170
3171     if(((sn - ba_session->expected_sn) & 0xFFF) <= 2048) {
3172
3173         /* once we are in BA window, set the flag for BA trigger */
3174         if(!ba_session->trigger_ba_after_ssn){
3175             ba_session->trigger_ba_after_ssn = TRUE;
3176         }
3177
3178         sn_temp = ba_session->expected_sn + ba_session->wind_size;
3179         unifi_trace(priv, UDBG6, "%s: new frame: sn=%d\n", __FUNCTION__, sn);
3180         if(!(((sn - sn_temp) & 0xFFF) > 2048)) {
3181             u16 new_expected_sn;
3182             unifi_trace(priv, UDBG6, "%s: frame is out of window\n", __FUNCTION__);
3183             sn_temp = (sn - ba_session->wind_size) & 0xFFF;
3184             new_expected_sn = (sn_temp + 1) & 0xFFF;
3185             update_expected_sn(priv, interfacePriv, ba_session, new_expected_sn);
3186         }
3187         i = -1;
3188         if (sn == ba_session->expected_sn) {
3189             unifi_trace(priv, UDBG6, "%s: sn = ba_session->expected_sn = %d\n", __FUNCTION__, sn);
3190             ADVANCE_EXPECTED_SN(ba_session);
3191             add_frame_to_ba_complete(priv, interfacePriv, frame_desc);
3192         } else {
3193             i = SN_TO_INDEX(ba_session, sn);
3194             unifi_trace(priv, UDBG6, "%s: sn(%d) != ba_session->expected_sn(%d), i = %d\n", __FUNCTION__, sn, ba_session->expected_sn, i);
3195             if (ba_session->buffer[i].active) {
3196                 unifi_trace(priv, UDBG6, "%s: free frame at i = %d\n", __FUNCTION__, i);
3197                 i = -1;
3198                 unifi_net_data_free(priv, &frame_desc->bulkdata.d[0]);
3199             }
3200         }
3201     } else {
3202         i = -1;
3203         if(!ba_session->trigger_ba_after_ssn){
3204             unifi_trace(priv, UDBG6, "%s: frame before ssn, pass it up: sn=%d\n", __FUNCTION__, sn);
3205             add_frame_to_ba_complete(priv, interfacePriv, frame_desc);
3206         }else{
3207             unifi_trace(priv, UDBG6, "%s: old frame, drop: sn=%d, expected_sn=%d\n", __FUNCTION__, sn, ba_session->expected_sn);
3208             unifi_net_data_free(priv, &frame_desc->bulkdata.d[0]);
3209         }
3210     }
3211     return i;
3212 }
3213
3214
3215
3216 static void process_ba_frame(unifi_priv_t *priv,
3217                                              netInterface_priv_t *interfacePriv,
3218                                              ba_session_rx_struct *ba_session,
3219                                              frame_desc_struct *frame_desc)
3220 {
3221     int i;
3222     u16 sn = frame_desc->sn;
3223
3224     if (ba_session->timeout) {
3225         mod_timer(&ba_session->timer, (jiffies + usecs_to_jiffies((ba_session->timeout) * 1024)));
3226     }
3227     unifi_trace(priv, UDBG6, "%s: got frame(sn=%d)\n", __FUNCTION__, sn);
3228
3229     i = consume_frame_or_get_buffer_index(priv, interfacePriv, ba_session, sn, frame_desc);
3230     if(i >= 0) {
3231         unifi_trace(priv, UDBG6, "%s: store frame(sn=%d) at i = %d\n", __FUNCTION__, sn, i);
3232         ba_session->buffer[i] = *frame_desc;
3233         ba_session->buffer[i].recv_time = CsrTimeGet(NULL);
3234         ba_session->occupied_slots++;
3235     } else {
3236         unifi_trace(priv, UDBG6, "%s: frame consumed - sn = %d\n", __FUNCTION__, sn);
3237     }
3238     complete_ready_sequence(priv, interfacePriv, ba_session);
3239 }
3240
3241
3242 static void process_ba_complete(unifi_priv_t *priv, netInterface_priv_t *interfacePriv)
3243 {
3244     frame_desc_struct *frame_desc;
3245     u8 i;
3246
3247     for(i = 0; i < interfacePriv->ba_complete_index; i++) {
3248         frame_desc = &interfacePriv->ba_complete[i];
3249         unifi_trace(priv, UDBG6, "%s: calling process_amsdu()\n", __FUNCTION__);
3250         process_amsdu(priv, &frame_desc->signal, &frame_desc->bulkdata);
3251     }
3252     interfacePriv->ba_complete_index = 0;
3253
3254 }
3255
3256
3257 /* Check if the frames in BA reoder buffer has aged and
3258  * if so release the frames to upper processes and move
3259  * the window
3260  */
3261 static void check_ba_frame_age_timeout( unifi_priv_t *priv,
3262                                         netInterface_priv_t *interfacePriv,
3263                                         ba_session_rx_struct *ba_session)
3264 {
3265     CsrTime now;
3266     CsrTime age;
3267     u8 i, j;
3268     u16 sn_temp;
3269
3270     /* gap is started at 1 because we have buffered frames and
3271      * hence a minimum gap of 1 exists
3272      */
3273     u8 gap=1;
3274
3275     now = CsrTimeGet(NULL);
3276
3277     if (ba_session->occupied_slots)
3278     {
3279         /* expected sequence has not arrived so start searching from next
3280          * sequence number until a frame is available and determine the gap.
3281          * Check if the frame available has timedout, if so advance the
3282          * expected sequence number and release the frames
3283          */
3284         sn_temp = (ba_session->expected_sn + 1) & 0xFFF;
3285
3286         for(j = 0; j < ba_session->wind_size; j++)
3287         {
3288             i = SN_TO_INDEX(ba_session, sn_temp);
3289
3290             if(ba_session->buffer[i].active)
3291             {
3292                 unifi_trace(priv, UDBG6, "check age at slot index = %d sn = %d recv_time = %u now = %u\n",
3293                                         i,
3294                                         ba_session->buffer[i].sn,
3295                                         ba_session->buffer[i].recv_time,
3296                                         now);
3297
3298                 if (ba_session->buffer[i].recv_time > now)
3299                 {
3300                     /* timer wrap */
3301                     age = CsrTimeAdd((CsrTime)CsrTimeSub(CSR_SCHED_TIME_MAX, ba_session->buffer[i].recv_time), now);
3302                 }
3303                 else
3304                 {
3305                     age = (CsrTime)CsrTimeSub(now, ba_session->buffer[i].recv_time);
3306                 }
3307
3308                 if (age >= CSR_WIFI_BA_MPDU_FRAME_AGE_TIMEOUT)
3309                 {
3310                     unifi_trace(priv, UDBG2, "release the frame at index = %d gap = %d expected_sn = %d sn = %d\n",
3311                                             i,
3312                                             gap,
3313                                             ba_session->expected_sn,
3314                                             ba_session->buffer[i].sn);
3315
3316                     /* if it has timedout don't wait for missing frames, move the window */
3317                     while (gap--)
3318                     {
3319                         ADVANCE_EXPECTED_SN(ba_session);
3320                     }
3321                     add_frame_to_ba_complete(priv, interfacePriv, &ba_session->buffer[i]);
3322                     FREE_BUFFER_SLOT(ba_session, i);
3323                     complete_ready_sequence(priv, interfacePriv, ba_session);
3324                 }
3325                 break;
3326
3327             }
3328             else
3329             {
3330                 /* advance temp sequence number and frame gap */
3331                 sn_temp = (sn_temp + 1) & 0xFFF;
3332                 gap++;
3333             }
3334         }
3335     }
3336 }
3337
3338
3339 static void process_ma_packet_error_ind(unifi_priv_t *priv, CSR_SIGNAL *signal, bulk_data_param_t *bulkdata)
3340 {
3341     u16 interfaceTag;
3342     const CSR_MA_PACKET_ERROR_INDICATION *pkt_err_ind = &signal->u.MaPacketErrorIndication;
3343     netInterface_priv_t *interfacePriv;
3344     ba_session_rx_struct *ba_session;
3345     u8 ba_session_idx = 0;
3346     CSR_PRIORITY        UserPriority;
3347     CSR_SEQUENCE_NUMBER sn;
3348
3349     func_enter();
3350
3351     interfaceTag = (pkt_err_ind->VirtualInterfaceIdentifier & 0xff);
3352
3353
3354     /* Sanity check that the VIF refers to a sensible interface */
3355     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES)
3356     {
3357         unifi_error(priv, "%s: MaPacketErrorIndication indication with bad interfaceTag %d\n", __FUNCTION__, interfaceTag);
3358         func_exit();
3359         return;
3360     }
3361
3362     interfacePriv = priv->interfacePriv[interfaceTag];
3363     UserPriority = pkt_err_ind->UserPriority;
3364     if(UserPriority > 15) {
3365         unifi_error(priv, "%s: MaPacketErrorIndication indication with bad UserPriority=%d\n", __FUNCTION__, UserPriority);
3366         func_exit();
3367     }
3368     sn = pkt_err_ind->SequenceNumber;
3369
3370     down(&priv->ba_mutex);
3371     /* To find the right ba_session loop through the BA sessions, compare MAC address and tID */
3372     for (ba_session_idx=0; ba_session_idx < MAX_SUPPORTED_BA_SESSIONS_RX; ba_session_idx++){
3373         ba_session = interfacePriv->ba_session_rx[ba_session_idx];
3374         if (ba_session){
3375             if ((!memcmp(ba_session->macAddress.a, pkt_err_ind->PeerQstaAddress.x, ETH_ALEN)) && (ba_session->tID == UserPriority)){
3376                 if (ba_session->timeout) {
3377                     mod_timer(&ba_session->timer, (jiffies + usecs_to_jiffies((ba_session->timeout) * 1024)));
3378                 }
3379                 scroll_ba_window(priv, interfacePriv, ba_session, sn);
3380                 break;
3381             }
3382         }
3383     }
3384
3385     up(&priv->ba_mutex);
3386     process_ba_complete(priv, interfacePriv);
3387     func_exit();
3388 }
3389
3390