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