linux/drivers/net/wireless/microchip/wilc1000/netdev.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
   4 * All rights reserved.
   5 */
   6
   7#include <linux/irq.h>
   8#include <linux/kthread.h>
   9#include <linux/firmware.h>
  10#include <linux/netdevice.h>
  11#include <linux/inetdevice.h>
  12
  13#include "cfg80211.h"
  14#include "wlan_cfg.h"
  15
  16#define WILC_MULTICAST_TABLE_SIZE       8
  17
  18/* latest API version supported */
  19#define WILC1000_API_VER                1
  20
  21#define WILC1000_FW_PREFIX              "atmel/wilc1000_wifi_firmware-"
  22#define __WILC1000_FW(api)              WILC1000_FW_PREFIX #api ".bin"
  23#define WILC1000_FW(api)                __WILC1000_FW(api)
  24
  25static irqreturn_t isr_uh_routine(int irq, void *user_data)
  26{
  27        struct wilc *wilc = user_data;
  28
  29        if (wilc->close) {
  30                pr_err("Can't handle UH interrupt");
  31                return IRQ_HANDLED;
  32        }
  33        return IRQ_WAKE_THREAD;
  34}
  35
  36static irqreturn_t isr_bh_routine(int irq, void *userdata)
  37{
  38        struct wilc *wilc = userdata;
  39
  40        if (wilc->close) {
  41                pr_err("Can't handle BH interrupt\n");
  42                return IRQ_HANDLED;
  43        }
  44
  45        wilc_handle_isr(wilc);
  46
  47        return IRQ_HANDLED;
  48}
  49
  50static int init_irq(struct net_device *dev)
  51{
  52        struct wilc_vif *vif = netdev_priv(dev);
  53        struct wilc *wl = vif->wilc;
  54        int ret;
  55
  56        ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
  57                                   isr_bh_routine,
  58                                   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  59                                   "WILC_IRQ", wl);
  60        if (ret) {
  61                netdev_err(dev, "Failed to request IRQ [%d]\n", ret);
  62                return ret;
  63        }
  64        netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n", wl->dev_irq_num);
  65
  66        return 0;
  67}
  68
  69static void deinit_irq(struct net_device *dev)
  70{
  71        struct wilc_vif *vif = netdev_priv(dev);
  72        struct wilc *wilc = vif->wilc;
  73
  74        /* Deinitialize IRQ */
  75        if (wilc->dev_irq_num)
  76                free_irq(wilc->dev_irq_num, wilc);
  77}
  78
  79void wilc_mac_indicate(struct wilc *wilc)
  80{
  81        s8 status;
  82
  83        wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
  84        if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
  85                wilc->mac_status = status;
  86                complete(&wilc->sync_event);
  87        } else {
  88                wilc->mac_status = status;
  89        }
  90}
  91
  92static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
  93{
  94        struct net_device *ndev = NULL;
  95        struct wilc_vif *vif;
  96        struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
  97
  98        list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
  99                if (vif->mode == WILC_STATION_MODE)
 100                        if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
 101                                ndev = vif->ndev;
 102                                goto out;
 103                        }
 104                if (vif->mode == WILC_AP_MODE)
 105                        if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
 106                                ndev = vif->ndev;
 107                                goto out;
 108                        }
 109        }
 110out:
 111        return ndev;
 112}
 113
 114void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
 115{
 116        struct wilc_vif *vif = netdev_priv(wilc_netdev);
 117
 118        if (bssid)
 119                ether_addr_copy(vif->bssid, bssid);
 120        else
 121                eth_zero_addr(vif->bssid);
 122
 123        vif->mode = mode;
 124}
 125
 126int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
 127{
 128        int srcu_idx;
 129        u8 ret_val = 0;
 130        struct wilc_vif *vif;
 131
 132        srcu_idx = srcu_read_lock(&wilc->srcu);
 133        list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 134                if (!is_zero_ether_addr(vif->bssid))
 135                        ret_val++;
 136        }
 137        srcu_read_unlock(&wilc->srcu, srcu_idx);
 138        return ret_val;
 139}
 140
 141static int wilc_txq_task(void *vp)
 142{
 143        int ret;
 144        u32 txq_count;
 145        struct wilc *wl = vp;
 146
 147        complete(&wl->txq_thread_started);
 148        while (1) {
 149                wait_for_completion(&wl->txq_event);
 150
 151                if (wl->close) {
 152                        complete(&wl->txq_thread_started);
 153
 154                        while (!kthread_should_stop())
 155                                schedule();
 156                        break;
 157                }
 158                do {
 159                        ret = wilc_wlan_handle_txq(wl, &txq_count);
 160                        if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
 161                                int srcu_idx;
 162                                struct wilc_vif *ifc;
 163
 164                                srcu_idx = srcu_read_lock(&wl->srcu);
 165                                list_for_each_entry_rcu(ifc, &wl->vif_list,
 166                                                        list) {
 167                                        if (ifc->mac_opened && ifc->ndev)
 168                                                netif_wake_queue(ifc->ndev);
 169                                }
 170                                srcu_read_unlock(&wl->srcu, srcu_idx);
 171                        }
 172                } while (ret == WILC_VMM_ENTRY_FULL_RETRY && !wl->close);
 173        }
 174        return 0;
 175}
 176
 177static int wilc_wlan_get_firmware(struct net_device *dev)
 178{
 179        struct wilc_vif *vif = netdev_priv(dev);
 180        struct wilc *wilc = vif->wilc;
 181        int chip_id;
 182        const struct firmware *wilc_fw;
 183        int ret;
 184
 185        chip_id = wilc_get_chipid(wilc, false);
 186
 187        netdev_info(dev, "ChipID [%x] loading firmware [%s]\n", chip_id,
 188                    WILC1000_FW(WILC1000_API_VER));
 189
 190        ret = request_firmware(&wilc_fw, WILC1000_FW(WILC1000_API_VER),
 191                               wilc->dev);
 192        if (ret != 0) {
 193                netdev_err(dev, "%s - firmware not available\n",
 194                           WILC1000_FW(WILC1000_API_VER));
 195                return -EINVAL;
 196        }
 197        wilc->firmware = wilc_fw;
 198
 199        return 0;
 200}
 201
 202static int wilc_start_firmware(struct net_device *dev)
 203{
 204        struct wilc_vif *vif = netdev_priv(dev);
 205        struct wilc *wilc = vif->wilc;
 206        int ret = 0;
 207
 208        ret = wilc_wlan_start(wilc);
 209        if (ret)
 210                return ret;
 211
 212        if (!wait_for_completion_timeout(&wilc->sync_event,
 213                                         msecs_to_jiffies(5000)))
 214                return -ETIME;
 215
 216        return 0;
 217}
 218
 219static int wilc1000_firmware_download(struct net_device *dev)
 220{
 221        struct wilc_vif *vif = netdev_priv(dev);
 222        struct wilc *wilc = vif->wilc;
 223        int ret = 0;
 224
 225        if (!wilc->firmware) {
 226                netdev_err(dev, "Firmware buffer is NULL\n");
 227                return -ENOBUFS;
 228        }
 229
 230        ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
 231                                          wilc->firmware->size);
 232        if (ret)
 233                return ret;
 234
 235        release_firmware(wilc->firmware);
 236        wilc->firmware = NULL;
 237
 238        netdev_dbg(dev, "Download Succeeded\n");
 239
 240        return 0;
 241}
 242
 243static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
 244{
 245        struct wilc_priv *priv = &vif->priv;
 246        struct host_if_drv *hif_drv;
 247        u8 b;
 248        u16 hw;
 249        u32 w;
 250
 251        netdev_dbg(dev, "Start configuring Firmware\n");
 252        hif_drv = (struct host_if_drv *)priv->hif_drv;
 253        netdev_dbg(dev, "Host = %p\n", hif_drv);
 254
 255        w = vif->iftype;
 256        cpu_to_le32s(&w);
 257        if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
 258                               0, 0))
 259                goto fail;
 260
 261        b = WILC_FW_BSS_TYPE_INFRA;
 262        if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
 263                goto fail;
 264
 265        b = WILC_FW_TX_RATE_AUTO;
 266        if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
 267                goto fail;
 268
 269        b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
 270        if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
 271                goto fail;
 272
 273        b = WILC_FW_PREAMBLE_SHORT;
 274        if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
 275                goto fail;
 276
 277        b = WILC_FW_11N_PROT_AUTO;
 278        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
 279                goto fail;
 280
 281        b = WILC_FW_ACTIVE_SCAN;
 282        if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
 283                goto fail;
 284
 285        b = WILC_FW_SITE_SURVEY_OFF;
 286        if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
 287                goto fail;
 288
 289        hw = 0xffff;
 290        cpu_to_le16s(&hw);
 291        if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 292                goto fail;
 293
 294        hw = 2346;
 295        cpu_to_le16s(&hw);
 296        if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 297                goto fail;
 298
 299        b = 0;
 300        if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
 301                goto fail;
 302
 303        b = 1;
 304        if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
 305                goto fail;
 306
 307        b = WILC_FW_NO_POWERSAVE;
 308        if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
 309                goto fail;
 310
 311        b = WILC_FW_SEC_NO;
 312        if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
 313                goto fail;
 314
 315        b = WILC_FW_AUTH_OPEN_SYSTEM;
 316        if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
 317                goto fail;
 318
 319        b = 3;
 320        if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
 321                goto fail;
 322
 323        b = 3;
 324        if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
 325                goto fail;
 326
 327        b = WILC_FW_ACK_POLICY_NORMAL;
 328        if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
 329                goto fail;
 330
 331        b = 0;
 332        if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
 333                               0, 0))
 334                goto fail;
 335
 336        b = 48;
 337        if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
 338                goto fail;
 339
 340        b = 28;
 341        if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
 342                goto fail;
 343
 344        hw = 100;
 345        cpu_to_le16s(&hw);
 346        if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
 347                goto fail;
 348
 349        b = WILC_FW_REKEY_POLICY_DISABLE;
 350        if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
 351                goto fail;
 352
 353        w = 84600;
 354        cpu_to_le32s(&w);
 355        if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
 356                goto fail;
 357
 358        w = 500;
 359        cpu_to_le32s(&w);
 360        if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
 361                               0))
 362                goto fail;
 363
 364        b = 1;
 365        if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
 366                               0))
 367                goto fail;
 368
 369        b = WILC_FW_ERP_PROT_SELF_CTS;
 370        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
 371                goto fail;
 372
 373        b = 1;
 374        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
 375                goto fail;
 376
 377        b = WILC_FW_11N_OP_MODE_HT_MIXED;
 378        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
 379                goto fail;
 380
 381        b = 1;
 382        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
 383                goto fail;
 384
 385        b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
 386        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
 387                               0, 0))
 388                goto fail;
 389
 390        b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
 391        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
 392                goto fail;
 393
 394        b = 0;
 395        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
 396                               0))
 397                goto fail;
 398
 399        b = 7;
 400        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
 401                goto fail;
 402
 403        b = 1;
 404        if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
 405                               1, 1))
 406                goto fail;
 407
 408        return 0;
 409
 410fail:
 411        return -EINVAL;
 412}
 413
 414static void wlan_deinitialize_threads(struct net_device *dev)
 415{
 416        struct wilc_vif *vif = netdev_priv(dev);
 417        struct wilc *wl = vif->wilc;
 418
 419        wl->close = 1;
 420
 421        complete(&wl->txq_event);
 422
 423        if (wl->txq_thread) {
 424                kthread_stop(wl->txq_thread);
 425                wl->txq_thread = NULL;
 426        }
 427}
 428
 429static void wilc_wlan_deinitialize(struct net_device *dev)
 430{
 431        struct wilc_vif *vif = netdev_priv(dev);
 432        struct wilc *wl = vif->wilc;
 433
 434        if (!wl) {
 435                netdev_err(dev, "wl is NULL\n");
 436                return;
 437        }
 438
 439        if (wl->initialized) {
 440                netdev_info(dev, "Deinitializing wilc1000...\n");
 441
 442                if (!wl->dev_irq_num &&
 443                    wl->hif_func->disable_interrupt) {
 444                        mutex_lock(&wl->hif_cs);
 445                        wl->hif_func->disable_interrupt(wl);
 446                        mutex_unlock(&wl->hif_cs);
 447                }
 448                complete(&wl->txq_event);
 449
 450                wlan_deinitialize_threads(dev);
 451                deinit_irq(dev);
 452
 453                wilc_wlan_stop(wl, vif);
 454                wilc_wlan_cleanup(dev);
 455
 456                wl->initialized = false;
 457
 458                netdev_dbg(dev, "wilc1000 deinitialization Done\n");
 459        } else {
 460                netdev_dbg(dev, "wilc1000 is not initialized\n");
 461        }
 462}
 463
 464static int wlan_initialize_threads(struct net_device *dev)
 465{
 466        struct wilc_vif *vif = netdev_priv(dev);
 467        struct wilc *wilc = vif->wilc;
 468
 469        wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
 470                                       "K_TXQ_TASK");
 471        if (IS_ERR(wilc->txq_thread)) {
 472                netdev_err(dev, "couldn't create TXQ thread\n");
 473                wilc->close = 0;
 474                return PTR_ERR(wilc->txq_thread);
 475        }
 476        wait_for_completion(&wilc->txq_thread_started);
 477
 478        return 0;
 479}
 480
 481static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
 482{
 483        int ret = 0;
 484        struct wilc *wl = vif->wilc;
 485
 486        if (!wl->initialized) {
 487                wl->mac_status = WILC_MAC_STATUS_INIT;
 488                wl->close = 0;
 489
 490                ret = wilc_wlan_init(dev);
 491                if (ret)
 492                        return ret;
 493
 494                ret = wlan_initialize_threads(dev);
 495                if (ret)
 496                        goto fail_wilc_wlan;
 497
 498                if (wl->dev_irq_num && init_irq(dev)) {
 499                        ret = -EIO;
 500                        goto fail_threads;
 501                }
 502
 503                if (!wl->dev_irq_num &&
 504                    wl->hif_func->enable_interrupt &&
 505                    wl->hif_func->enable_interrupt(wl)) {
 506                        ret = -EIO;
 507                        goto fail_irq_init;
 508                }
 509
 510                ret = wilc_wlan_get_firmware(dev);
 511                if (ret)
 512                        goto fail_irq_enable;
 513
 514                ret = wilc1000_firmware_download(dev);
 515                if (ret)
 516                        goto fail_irq_enable;
 517
 518                ret = wilc_start_firmware(dev);
 519                if (ret)
 520                        goto fail_irq_enable;
 521
 522                if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
 523                        int size;
 524                        char firmware_ver[20];
 525
 526                        size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
 527                                                     firmware_ver,
 528                                                     sizeof(firmware_ver));
 529                        firmware_ver[size] = '\0';
 530                        netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
 531                }
 532
 533                ret = wilc_init_fw_config(dev, vif);
 534                if (ret) {
 535                        netdev_err(dev, "Failed to configure firmware\n");
 536                        goto fail_fw_start;
 537                }
 538                wl->initialized = true;
 539                return 0;
 540
 541fail_fw_start:
 542                wilc_wlan_stop(wl, vif);
 543
 544fail_irq_enable:
 545                if (!wl->dev_irq_num &&
 546                    wl->hif_func->disable_interrupt)
 547                        wl->hif_func->disable_interrupt(wl);
 548fail_irq_init:
 549                if (wl->dev_irq_num)
 550                        deinit_irq(dev);
 551fail_threads:
 552                wlan_deinitialize_threads(dev);
 553fail_wilc_wlan:
 554                wilc_wlan_cleanup(dev);
 555                netdev_err(dev, "WLAN initialization FAILED\n");
 556        } else {
 557                netdev_dbg(dev, "wilc1000 already initialized\n");
 558        }
 559        return ret;
 560}
 561
 562static int mac_init_fn(struct net_device *ndev)
 563{
 564        netif_start_queue(ndev);
 565        netif_stop_queue(ndev);
 566
 567        return 0;
 568}
 569
 570static int wilc_mac_open(struct net_device *ndev)
 571{
 572        struct wilc_vif *vif = netdev_priv(ndev);
 573        struct wilc *wl = vif->wilc;
 574        int ret = 0;
 575        struct mgmt_frame_regs mgmt_regs = {};
 576
 577        if (!wl || !wl->dev) {
 578                netdev_err(ndev, "device not ready\n");
 579                return -ENODEV;
 580        }
 581
 582        netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
 583
 584        ret = wilc_init_host_int(ndev);
 585        if (ret)
 586                return ret;
 587
 588        ret = wilc_wlan_initialize(ndev, vif);
 589        if (ret) {
 590                wilc_deinit_host_int(ndev);
 591                return ret;
 592        }
 593
 594        wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
 595                                vif->idx);
 596
 597        if (is_valid_ether_addr(ndev->dev_addr))
 598                wilc_set_mac_address(vif, ndev->dev_addr);
 599        else
 600                wilc_get_mac_address(vif, ndev->dev_addr);
 601        netdev_dbg(ndev, "Mac address: %pM\n", ndev->dev_addr);
 602
 603        if (!is_valid_ether_addr(ndev->dev_addr)) {
 604                netdev_err(ndev, "Wrong MAC address\n");
 605                wilc_deinit_host_int(ndev);
 606                wilc_wlan_deinitialize(ndev);
 607                return -EINVAL;
 608        }
 609
 610        mgmt_regs.interface_stypes = vif->mgmt_reg_stypes;
 611        /* so we detect a change */
 612        vif->mgmt_reg_stypes = 0;
 613        wilc_update_mgmt_frame_registrations(vif->ndev->ieee80211_ptr->wiphy,
 614                                             vif->ndev->ieee80211_ptr,
 615                                             &mgmt_regs);
 616        netif_wake_queue(ndev);
 617        wl->open_ifcs++;
 618        vif->mac_opened = 1;
 619        return 0;
 620}
 621
 622static struct net_device_stats *mac_stats(struct net_device *dev)
 623{
 624        struct wilc_vif *vif = netdev_priv(dev);
 625
 626        return &vif->netstats;
 627}
 628
 629static int wilc_set_mac_addr(struct net_device *dev, void *p)
 630{
 631        int result;
 632        struct wilc_vif *vif = netdev_priv(dev);
 633        struct wilc *wilc = vif->wilc;
 634        struct sockaddr *addr = (struct sockaddr *)p;
 635        unsigned char mac_addr[ETH_ALEN];
 636        struct wilc_vif *tmp_vif;
 637        int srcu_idx;
 638
 639        if (!is_valid_ether_addr(addr->sa_data))
 640                return -EADDRNOTAVAIL;
 641
 642        if (!vif->mac_opened) {
 643                eth_commit_mac_addr_change(dev, p);
 644                return 0;
 645        }
 646
 647        /* Verify MAC Address is not already in use: */
 648
 649        srcu_idx = srcu_read_lock(&wilc->srcu);
 650        list_for_each_entry_rcu(tmp_vif, &wilc->vif_list, list) {
 651                wilc_get_mac_address(tmp_vif, mac_addr);
 652                if (ether_addr_equal(addr->sa_data, mac_addr)) {
 653                        if (vif != tmp_vif) {
 654                                srcu_read_unlock(&wilc->srcu, srcu_idx);
 655                                return -EADDRNOTAVAIL;
 656                        }
 657                        srcu_read_unlock(&wilc->srcu, srcu_idx);
 658                        return 0;
 659                }
 660        }
 661        srcu_read_unlock(&wilc->srcu, srcu_idx);
 662
 663        result = wilc_set_mac_address(vif, (u8 *)addr->sa_data);
 664        if (result)
 665                return result;
 666
 667        eth_commit_mac_addr_change(dev, p);
 668        return result;
 669}
 670
 671static void wilc_set_multicast_list(struct net_device *dev)
 672{
 673        struct netdev_hw_addr *ha;
 674        struct wilc_vif *vif = netdev_priv(dev);
 675        int i;
 676        u8 *mc_list;
 677        u8 *cur_mc;
 678
 679        if (dev->flags & IFF_PROMISC)
 680                return;
 681
 682        if (dev->flags & IFF_ALLMULTI ||
 683            dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
 684                wilc_setup_multicast_filter(vif, 0, 0, NULL);
 685                return;
 686        }
 687
 688        if (dev->mc.count == 0) {
 689                wilc_setup_multicast_filter(vif, 1, 0, NULL);
 690                return;
 691        }
 692
 693        mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
 694        if (!mc_list)
 695                return;
 696
 697        cur_mc = mc_list;
 698        i = 0;
 699        netdev_for_each_mc_addr(ha, dev) {
 700                memcpy(cur_mc, ha->addr, ETH_ALEN);
 701                netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
 702                i++;
 703                cur_mc += ETH_ALEN;
 704        }
 705
 706        if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
 707                kfree(mc_list);
 708}
 709
 710static void wilc_tx_complete(void *priv, int status)
 711{
 712        struct tx_complete_data *pv_data = priv;
 713
 714        dev_kfree_skb(pv_data->skb);
 715        kfree(pv_data);
 716}
 717
 718netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
 719{
 720        struct wilc_vif *vif = netdev_priv(ndev);
 721        struct wilc *wilc = vif->wilc;
 722        struct tx_complete_data *tx_data = NULL;
 723        int queue_count;
 724
 725        if (skb->dev != ndev) {
 726                netdev_err(ndev, "Packet not destined to this device\n");
 727                return NETDEV_TX_OK;
 728        }
 729
 730        tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
 731        if (!tx_data) {
 732                dev_kfree_skb(skb);
 733                netif_wake_queue(ndev);
 734                return NETDEV_TX_OK;
 735        }
 736
 737        tx_data->buff = skb->data;
 738        tx_data->size = skb->len;
 739        tx_data->skb  = skb;
 740
 741        vif->netstats.tx_packets++;
 742        vif->netstats.tx_bytes += tx_data->size;
 743        queue_count = wilc_wlan_txq_add_net_pkt(ndev, tx_data,
 744                                                tx_data->buff, tx_data->size,
 745                                                wilc_tx_complete);
 746
 747        if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
 748                int srcu_idx;
 749                struct wilc_vif *vif;
 750
 751                srcu_idx = srcu_read_lock(&wilc->srcu);
 752                list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 753                        if (vif->mac_opened)
 754                                netif_stop_queue(vif->ndev);
 755                }
 756                srcu_read_unlock(&wilc->srcu, srcu_idx);
 757        }
 758
 759        return NETDEV_TX_OK;
 760}
 761
 762static int wilc_mac_close(struct net_device *ndev)
 763{
 764        struct wilc_vif *vif = netdev_priv(ndev);
 765        struct wilc *wl = vif->wilc;
 766
 767        netdev_dbg(ndev, "Mac close\n");
 768
 769        if (wl->open_ifcs > 0)
 770                wl->open_ifcs--;
 771        else
 772                return 0;
 773
 774        if (vif->ndev) {
 775                netif_stop_queue(vif->ndev);
 776
 777                wilc_deinit_host_int(vif->ndev);
 778        }
 779
 780        if (wl->open_ifcs == 0) {
 781                netdev_dbg(ndev, "Deinitializing wilc1000\n");
 782                wl->close = 1;
 783                wilc_wlan_deinitialize(ndev);
 784        }
 785
 786        vif->mac_opened = 0;
 787
 788        return 0;
 789}
 790
 791void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
 792                       u32 pkt_offset)
 793{
 794        unsigned int frame_len = 0;
 795        int stats;
 796        unsigned char *buff_to_send = NULL;
 797        struct sk_buff *skb;
 798        struct net_device *wilc_netdev;
 799        struct wilc_vif *vif;
 800
 801        if (!wilc)
 802                return;
 803
 804        wilc_netdev = get_if_handler(wilc, buff);
 805        if (!wilc_netdev)
 806                return;
 807
 808        buff += pkt_offset;
 809        vif = netdev_priv(wilc_netdev);
 810
 811        if (size > 0) {
 812                frame_len = size;
 813                buff_to_send = buff;
 814
 815                skb = dev_alloc_skb(frame_len);
 816                if (!skb)
 817                        return;
 818
 819                skb->dev = wilc_netdev;
 820
 821                skb_put_data(skb, buff_to_send, frame_len);
 822
 823                skb->protocol = eth_type_trans(skb, wilc_netdev);
 824                vif->netstats.rx_packets++;
 825                vif->netstats.rx_bytes += frame_len;
 826                skb->ip_summed = CHECKSUM_UNNECESSARY;
 827                stats = netif_rx(skb);
 828                netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
 829        }
 830}
 831
 832void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
 833{
 834        int srcu_idx;
 835        struct wilc_vif *vif;
 836
 837        srcu_idx = srcu_read_lock(&wilc->srcu);
 838        list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 839                u16 type = le16_to_cpup((__le16 *)buff);
 840                u32 type_bit = BIT(type >> 4);
 841
 842                if (vif->priv.p2p_listen_state &&
 843                    vif->mgmt_reg_stypes & type_bit)
 844                        wilc_wfi_p2p_rx(vif, buff, size);
 845
 846                if (vif->monitor_flag)
 847                        wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
 848        }
 849        srcu_read_unlock(&wilc->srcu, srcu_idx);
 850}
 851
 852static const struct net_device_ops wilc_netdev_ops = {
 853        .ndo_init = mac_init_fn,
 854        .ndo_open = wilc_mac_open,
 855        .ndo_stop = wilc_mac_close,
 856        .ndo_set_mac_address = wilc_set_mac_addr,
 857        .ndo_start_xmit = wilc_mac_xmit,
 858        .ndo_get_stats = mac_stats,
 859        .ndo_set_rx_mode  = wilc_set_multicast_list,
 860};
 861
 862void wilc_netdev_cleanup(struct wilc *wilc)
 863{
 864        struct wilc_vif *vif;
 865        int srcu_idx, ifc_cnt = 0;
 866
 867        if (!wilc)
 868                return;
 869
 870        if (wilc->firmware) {
 871                release_firmware(wilc->firmware);
 872                wilc->firmware = NULL;
 873        }
 874
 875        srcu_idx = srcu_read_lock(&wilc->srcu);
 876        list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 877                if (vif->ndev)
 878                        unregister_netdev(vif->ndev);
 879        }
 880        srcu_read_unlock(&wilc->srcu, srcu_idx);
 881
 882        wilc_wfi_deinit_mon_interface(wilc, false);
 883        flush_workqueue(wilc->hif_workqueue);
 884        destroy_workqueue(wilc->hif_workqueue);
 885
 886        while (ifc_cnt < WILC_NUM_CONCURRENT_IFC) {
 887                mutex_lock(&wilc->vif_mutex);
 888                if (wilc->vif_num <= 0) {
 889                        mutex_unlock(&wilc->vif_mutex);
 890                        break;
 891                }
 892                vif = wilc_get_wl_to_vif(wilc);
 893                if (!IS_ERR(vif))
 894                        list_del_rcu(&vif->list);
 895
 896                wilc->vif_num--;
 897                mutex_unlock(&wilc->vif_mutex);
 898                synchronize_srcu(&wilc->srcu);
 899                ifc_cnt++;
 900        }
 901
 902        wilc_wlan_cfg_deinit(wilc);
 903        wlan_deinit_locks(wilc);
 904        kfree(wilc->bus_data);
 905        wiphy_unregister(wilc->wiphy);
 906        wiphy_free(wilc->wiphy);
 907}
 908EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
 909
 910static u8 wilc_get_available_idx(struct wilc *wl)
 911{
 912        int idx = 0;
 913        struct wilc_vif *vif;
 914        int srcu_idx;
 915
 916        srcu_idx = srcu_read_lock(&wl->srcu);
 917        list_for_each_entry_rcu(vif, &wl->vif_list, list) {
 918                if (vif->idx == 0)
 919                        idx = 1;
 920                else
 921                        idx = 0;
 922        }
 923        srcu_read_unlock(&wl->srcu, srcu_idx);
 924        return idx;
 925}
 926
 927struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
 928                                      int vif_type, enum nl80211_iftype type,
 929                                      bool rtnl_locked)
 930{
 931        struct net_device *ndev;
 932        struct wilc_vif *vif;
 933        int ret;
 934
 935        ndev = alloc_etherdev(sizeof(*vif));
 936        if (!ndev)
 937                return ERR_PTR(-ENOMEM);
 938
 939        vif = netdev_priv(ndev);
 940        ndev->ieee80211_ptr = &vif->priv.wdev;
 941        strcpy(ndev->name, name);
 942        vif->wilc = wl;
 943        vif->ndev = ndev;
 944        ndev->ml_priv = vif;
 945
 946        ndev->netdev_ops = &wilc_netdev_ops;
 947
 948        SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
 949
 950        vif->priv.wdev.wiphy = wl->wiphy;
 951        vif->priv.wdev.netdev = ndev;
 952        vif->priv.wdev.iftype = type;
 953        vif->priv.dev = ndev;
 954
 955        if (rtnl_locked)
 956                ret = cfg80211_register_netdevice(ndev);
 957        else
 958                ret = register_netdev(ndev);
 959
 960        if (ret) {
 961                free_netdev(ndev);
 962                return ERR_PTR(-EFAULT);
 963        }
 964
 965        ndev->needs_free_netdev = true;
 966        vif->iftype = vif_type;
 967        vif->idx = wilc_get_available_idx(wl);
 968        vif->mac_opened = 0;
 969        mutex_lock(&wl->vif_mutex);
 970        list_add_tail_rcu(&vif->list, &wl->vif_list);
 971        wl->vif_num += 1;
 972        mutex_unlock(&wl->vif_mutex);
 973        synchronize_srcu(&wl->srcu);
 974
 975        return vif;
 976}
 977
 978MODULE_LICENSE("GPL");
 979MODULE_FIRMWARE(WILC1000_FW(WILC1000_API_VER));
 980