linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (c) 2010 Broadcom Corporation
   4 */
   5
   6#include <linux/kernel.h>
   7#include <linux/etherdevice.h>
   8#include <linux/module.h>
   9#include <linux/inetdevice.h>
  10#include <net/cfg80211.h>
  11#include <net/rtnetlink.h>
  12#include <net/addrconf.h>
  13#include <net/ieee80211_radiotap.h>
  14#include <net/ipv6.h>
  15#include <brcmu_utils.h>
  16#include <brcmu_wifi.h>
  17
  18#include "core.h"
  19#include "bus.h"
  20#include "debug.h"
  21#include "fwil_types.h"
  22#include "p2p.h"
  23#include "pno.h"
  24#include "cfg80211.h"
  25#include "fwil.h"
  26#include "feature.h"
  27#include "proto.h"
  28#include "pcie.h"
  29#include "common.h"
  30
  31#define MAX_WAIT_FOR_8021X_TX                   msecs_to_jiffies(950)
  32
  33#define BRCMF_BSSIDX_INVALID                    -1
  34
  35#define RXS_PBPRES                              BIT(2)
  36
  37#define D11_PHY_HDR_LEN                         6
  38
  39struct d11rxhdr_le {
  40        __le16 RxFrameSize;
  41        u16 PAD;
  42        __le16 PhyRxStatus_0;
  43        __le16 PhyRxStatus_1;
  44        __le16 PhyRxStatus_2;
  45        __le16 PhyRxStatus_3;
  46        __le16 PhyRxStatus_4;
  47        __le16 PhyRxStatus_5;
  48        __le16 RxStatus1;
  49        __le16 RxStatus2;
  50        __le16 RxTSFTime;
  51        __le16 RxChan;
  52        u8 unknown[12];
  53} __packed;
  54
  55struct wlc_d11rxhdr {
  56        struct d11rxhdr_le rxhdr;
  57        __le32 tsf_l;
  58        s8 rssi;
  59        s8 rxpwr0;
  60        s8 rxpwr1;
  61        s8 do_rssi_ma;
  62        s8 rxpwr[4];
  63} __packed;
  64
  65char *brcmf_ifname(struct brcmf_if *ifp)
  66{
  67        if (!ifp)
  68                return "<if_null>";
  69
  70        if (ifp->ndev)
  71                return ifp->ndev->name;
  72
  73        return "<if_none>";
  74}
  75
  76struct brcmf_if *brcmf_get_ifp(struct brcmf_pub *drvr, int ifidx)
  77{
  78        struct brcmf_if *ifp;
  79        s32 bsscfgidx;
  80
  81        if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
  82                bphy_err(drvr, "ifidx %d out of range\n", ifidx);
  83                return NULL;
  84        }
  85
  86        ifp = NULL;
  87        bsscfgidx = drvr->if2bss[ifidx];
  88        if (bsscfgidx >= 0)
  89                ifp = drvr->iflist[bsscfgidx];
  90
  91        return ifp;
  92}
  93
  94void brcmf_configure_arp_nd_offload(struct brcmf_if *ifp, bool enable)
  95{
  96        s32 err;
  97        u32 mode;
  98
  99        if (enable)
 100                mode = BRCMF_ARP_OL_AGENT | BRCMF_ARP_OL_PEER_AUTO_REPLY;
 101        else
 102                mode = 0;
 103
 104        /* Try to set and enable ARP offload feature, this may fail, then it  */
 105        /* is simply not supported and err 0 will be returned                 */
 106        err = brcmf_fil_iovar_int_set(ifp, "arp_ol", mode);
 107        if (err) {
 108                brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, err = %d\n",
 109                          mode, err);
 110        } else {
 111                err = brcmf_fil_iovar_int_set(ifp, "arpoe", enable);
 112                if (err) {
 113                        brcmf_dbg(TRACE, "failed to configure (%d) ARP offload err = %d\n",
 114                                  enable, err);
 115                } else {
 116                        brcmf_dbg(TRACE, "successfully configured (%d) ARP offload to 0x%x\n",
 117                                  enable, mode);
 118                }
 119        }
 120
 121        err = brcmf_fil_iovar_int_set(ifp, "ndoe", enable);
 122        if (err) {
 123                brcmf_dbg(TRACE, "failed to configure (%d) ND offload err = %d\n",
 124                          enable, err);
 125        } else {
 126                brcmf_dbg(TRACE, "successfully configured (%d) ND offload to 0x%x\n",
 127                          enable, mode);
 128        }
 129}
 130
 131static void _brcmf_set_multicast_list(struct work_struct *work)
 132{
 133        struct brcmf_if *ifp = container_of(work, struct brcmf_if,
 134                                            multicast_work);
 135        struct brcmf_pub *drvr = ifp->drvr;
 136        struct net_device *ndev;
 137        struct netdev_hw_addr *ha;
 138        u32 cmd_value, cnt;
 139        __le32 cnt_le;
 140        char *buf, *bufp;
 141        u32 buflen;
 142        s32 err;
 143
 144        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
 145
 146        ndev = ifp->ndev;
 147
 148        /* Determine initial value of allmulti flag */
 149        cmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
 150
 151        /* Send down the multicast list first. */
 152        cnt = netdev_mc_count(ndev);
 153        buflen = sizeof(cnt) + (cnt * ETH_ALEN);
 154        buf = kmalloc(buflen, GFP_ATOMIC);
 155        if (!buf)
 156                return;
 157        bufp = buf;
 158
 159        cnt_le = cpu_to_le32(cnt);
 160        memcpy(bufp, &cnt_le, sizeof(cnt_le));
 161        bufp += sizeof(cnt_le);
 162
 163        netdev_for_each_mc_addr(ha, ndev) {
 164                if (!cnt)
 165                        break;
 166                memcpy(bufp, ha->addr, ETH_ALEN);
 167                bufp += ETH_ALEN;
 168                cnt--;
 169        }
 170
 171        err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
 172        if (err < 0) {
 173                bphy_err(drvr, "Setting mcast_list failed, %d\n", err);
 174                cmd_value = cnt ? true : cmd_value;
 175        }
 176
 177        kfree(buf);
 178
 179        /*
 180         * Now send the allmulti setting.  This is based on the setting in the
 181         * net_device flags, but might be modified above to be turned on if we
 182         * were trying to set some addresses and dongle rejected it...
 183         */
 184        err = brcmf_fil_iovar_int_set(ifp, "allmulti", cmd_value);
 185        if (err < 0)
 186                bphy_err(drvr, "Setting allmulti failed, %d\n", err);
 187
 188        /*Finally, pick up the PROMISC flag */
 189        cmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
 190        err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PROMISC, cmd_value);
 191        if (err < 0)
 192                bphy_err(drvr, "Setting BRCMF_C_SET_PROMISC failed, %d\n",
 193                         err);
 194        brcmf_configure_arp_nd_offload(ifp, !cmd_value);
 195}
 196
 197#if IS_ENABLED(CONFIG_IPV6)
 198static void _brcmf_update_ndtable(struct work_struct *work)
 199{
 200        struct brcmf_if *ifp = container_of(work, struct brcmf_if,
 201                                            ndoffload_work);
 202        struct brcmf_pub *drvr = ifp->drvr;
 203        int i, ret;
 204
 205        /* clear the table in firmware */
 206        ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip_clear", NULL, 0);
 207        if (ret) {
 208                brcmf_dbg(TRACE, "fail to clear nd ip table err:%d\n", ret);
 209                return;
 210        }
 211
 212        for (i = 0; i < ifp->ipv6addr_idx; i++) {
 213                ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip",
 214                                               &ifp->ipv6_addr_tbl[i],
 215                                               sizeof(struct in6_addr));
 216                if (ret)
 217                        bphy_err(drvr, "add nd ip err %d\n", ret);
 218        }
 219}
 220#else
 221static void _brcmf_update_ndtable(struct work_struct *work)
 222{
 223}
 224#endif
 225
 226static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
 227{
 228        struct brcmf_if *ifp = netdev_priv(ndev);
 229        struct sockaddr *sa = (struct sockaddr *)addr;
 230        struct brcmf_pub *drvr = ifp->drvr;
 231        int err;
 232
 233        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
 234
 235        err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", sa->sa_data,
 236                                       ETH_ALEN);
 237        if (err < 0) {
 238                bphy_err(drvr, "Setting cur_etheraddr failed, %d\n", err);
 239        } else {
 240                brcmf_dbg(TRACE, "updated to %pM\n", sa->sa_data);
 241                memcpy(ifp->mac_addr, sa->sa_data, ETH_ALEN);
 242                memcpy(ifp->ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
 243        }
 244        return err;
 245}
 246
 247static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
 248{
 249        struct brcmf_if *ifp = netdev_priv(ndev);
 250
 251        schedule_work(&ifp->multicast_work);
 252}
 253
 254/**
 255 * brcmf_skb_is_iapp - checks if skb is an IAPP packet
 256 *
 257 * @skb: skb to check
 258 */
 259static bool brcmf_skb_is_iapp(struct sk_buff *skb)
 260{
 261        static const u8 iapp_l2_update_packet[6] __aligned(2) = {
 262                0x00, 0x01, 0xaf, 0x81, 0x01, 0x00,
 263        };
 264        unsigned char *eth_data;
 265#if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
 266        const u16 *a, *b;
 267#endif
 268
 269        if (skb->len - skb->mac_len != 6 ||
 270            !is_multicast_ether_addr(eth_hdr(skb)->h_dest))
 271                return false;
 272
 273        eth_data = skb_mac_header(skb) + ETH_HLEN;
 274#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
 275        return !(((*(const u32 *)eth_data) ^ (*(const u32 *)iapp_l2_update_packet)) |
 276                 ((*(const u16 *)(eth_data + 4)) ^ (*(const u16 *)(iapp_l2_update_packet + 4))));
 277#else
 278        a = (const u16 *)eth_data;
 279        b = (const u16 *)iapp_l2_update_packet;
 280
 281        return !((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2]));
 282#endif
 283}
 284
 285static netdev_tx_t brcmf_netdev_start_xmit(struct sk_buff *skb,
 286                                           struct net_device *ndev)
 287{
 288        int ret;
 289        struct brcmf_if *ifp = netdev_priv(ndev);
 290        struct brcmf_pub *drvr = ifp->drvr;
 291        struct ethhdr *eh;
 292        int head_delta;
 293
 294        brcmf_dbg(DATA, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
 295
 296        /* Can the device send data? */
 297        if (drvr->bus_if->state != BRCMF_BUS_UP) {
 298                bphy_err(drvr, "xmit rejected state=%d\n", drvr->bus_if->state);
 299                netif_stop_queue(ndev);
 300                dev_kfree_skb(skb);
 301                ret = -ENODEV;
 302                goto done;
 303        }
 304
 305        /* Some recent Broadcom's firmwares disassociate STA when they receive
 306         * an 802.11f ADD frame. This behavior can lead to a local DoS security
 307         * issue. Attacker may trigger disassociation of any STA by sending a
 308         * proper Ethernet frame to the wireless interface.
 309         *
 310         * Moreover this feature may break AP interfaces in some specific
 311         * setups. This applies e.g. to the bridge with hairpin mode enabled and
 312         * IFLA_BRPORT_MCAST_TO_UCAST set. IAPP packet generated by a firmware
 313         * will get passed back to the wireless interface and cause immediate
 314         * disassociation of a just-connected STA.
 315         */
 316        if (!drvr->settings->iapp && brcmf_skb_is_iapp(skb)) {
 317                dev_kfree_skb(skb);
 318                ret = -EINVAL;
 319                goto done;
 320        }
 321
 322        /* Make sure there's enough writeable headroom */
 323        if (skb_headroom(skb) < drvr->hdrlen || skb_header_cloned(skb)) {
 324                head_delta = max_t(int, drvr->hdrlen - skb_headroom(skb), 0);
 325
 326                brcmf_dbg(INFO, "%s: insufficient headroom (%d)\n",
 327                          brcmf_ifname(ifp), head_delta);
 328                atomic_inc(&drvr->bus_if->stats.pktcowed);
 329                ret = pskb_expand_head(skb, ALIGN(head_delta, NET_SKB_PAD), 0,
 330                                       GFP_ATOMIC);
 331                if (ret < 0) {
 332                        bphy_err(drvr, "%s: failed to expand headroom\n",
 333                                 brcmf_ifname(ifp));
 334                        atomic_inc(&drvr->bus_if->stats.pktcow_failed);
 335                        goto done;
 336                }
 337        }
 338
 339        /* validate length for ether packet */
 340        if (skb->len < sizeof(*eh)) {
 341                ret = -EINVAL;
 342                dev_kfree_skb(skb);
 343                goto done;
 344        }
 345
 346        eh = (struct ethhdr *)(skb->data);
 347
 348        if (eh->h_proto == htons(ETH_P_PAE))
 349                atomic_inc(&ifp->pend_8021x_cnt);
 350
 351        /* determine the priority */
 352        if ((skb->priority == 0) || (skb->priority > 7))
 353                skb->priority = cfg80211_classify8021d(skb, NULL);
 354
 355        ret = brcmf_proto_tx_queue_data(drvr, ifp->ifidx, skb);
 356        if (ret < 0)
 357                brcmf_txfinalize(ifp, skb, false);
 358
 359done:
 360        if (ret) {
 361                ndev->stats.tx_dropped++;
 362        } else {
 363                ndev->stats.tx_packets++;
 364                ndev->stats.tx_bytes += skb->len;
 365        }
 366
 367        /* Return ok: we always eat the packet */
 368        return NETDEV_TX_OK;
 369}
 370
 371void brcmf_txflowblock_if(struct brcmf_if *ifp,
 372                          enum brcmf_netif_stop_reason reason, bool state)
 373{
 374        unsigned long flags;
 375
 376        if (!ifp || !ifp->ndev)
 377                return;
 378
 379        brcmf_dbg(TRACE, "enter: bsscfgidx=%d stop=0x%X reason=%d state=%d\n",
 380                  ifp->bsscfgidx, ifp->netif_stop, reason, state);
 381
 382        spin_lock_irqsave(&ifp->netif_stop_lock, flags);
 383        if (state) {
 384                if (!ifp->netif_stop)
 385                        netif_stop_queue(ifp->ndev);
 386                ifp->netif_stop |= reason;
 387        } else {
 388                ifp->netif_stop &= ~reason;
 389                if (!ifp->netif_stop)
 390                        netif_wake_queue(ifp->ndev);
 391        }
 392        spin_unlock_irqrestore(&ifp->netif_stop_lock, flags);
 393}
 394
 395void brcmf_netif_rx(struct brcmf_if *ifp, struct sk_buff *skb)
 396{
 397        /* Most of Broadcom's firmwares send 802.11f ADD frame every time a new
 398         * STA connects to the AP interface. This is an obsoleted standard most
 399         * users don't use, so don't pass these frames up unless requested.
 400         */
 401        if (!ifp->drvr->settings->iapp && brcmf_skb_is_iapp(skb)) {
 402                brcmu_pkt_buf_free_skb(skb);
 403                return;
 404        }
 405
 406        if (skb->pkt_type == PACKET_MULTICAST)
 407                ifp->ndev->stats.multicast++;
 408
 409        if (!(ifp->ndev->flags & IFF_UP)) {
 410                brcmu_pkt_buf_free_skb(skb);
 411                return;
 412        }
 413
 414        ifp->ndev->stats.rx_bytes += skb->len;
 415        ifp->ndev->stats.rx_packets++;
 416
 417        brcmf_dbg(DATA, "rx proto=0x%X\n", ntohs(skb->protocol));
 418        if (in_interrupt())
 419                netif_rx(skb);
 420        else
 421                /* If the receive is not processed inside an ISR,
 422                 * the softirqd must be woken explicitly to service
 423                 * the NET_RX_SOFTIRQ.  This is handled by netif_rx_ni().
 424                 */
 425                netif_rx_ni(skb);
 426}
 427
 428void brcmf_netif_mon_rx(struct brcmf_if *ifp, struct sk_buff *skb)
 429{
 430        if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FMT_RADIOTAP)) {
 431                /* Do nothing */
 432        } else if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FMT_HW_RX_HDR)) {
 433                struct wlc_d11rxhdr *wlc_rxhdr = (struct wlc_d11rxhdr *)skb->data;
 434                struct ieee80211_radiotap_header *radiotap;
 435                unsigned int offset;
 436                u16 RxStatus1;
 437
 438                RxStatus1 = le16_to_cpu(wlc_rxhdr->rxhdr.RxStatus1);
 439
 440                offset = sizeof(struct wlc_d11rxhdr);
 441                /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU
 442                 * subframes
 443                 */
 444                if (RxStatus1 & RXS_PBPRES)
 445                        offset += 2;
 446                offset += D11_PHY_HDR_LEN;
 447
 448                skb_pull(skb, offset);
 449
 450                /* TODO: use RX header to fill some radiotap data */
 451                radiotap = skb_push(skb, sizeof(*radiotap));
 452                memset(radiotap, 0, sizeof(*radiotap));
 453                radiotap->it_len = cpu_to_le16(sizeof(*radiotap));
 454
 455                /* TODO: 4 bytes with receive status? */
 456                skb->len -= 4;
 457        } else {
 458                struct ieee80211_radiotap_header *radiotap;
 459
 460                /* TODO: use RX status to fill some radiotap data */
 461                radiotap = skb_push(skb, sizeof(*radiotap));
 462                memset(radiotap, 0, sizeof(*radiotap));
 463                radiotap->it_len = cpu_to_le16(sizeof(*radiotap));
 464
 465                /* TODO: 4 bytes with receive status? */
 466                skb->len -= 4;
 467        }
 468
 469        skb->dev = ifp->ndev;
 470        skb_reset_mac_header(skb);
 471        skb->pkt_type = PACKET_OTHERHOST;
 472        skb->protocol = htons(ETH_P_802_2);
 473
 474        brcmf_netif_rx(ifp, skb);
 475}
 476
 477static int brcmf_rx_hdrpull(struct brcmf_pub *drvr, struct sk_buff *skb,
 478                            struct brcmf_if **ifp)
 479{
 480        int ret;
 481
 482        /* process and remove protocol-specific header */
 483        ret = brcmf_proto_hdrpull(drvr, true, skb, ifp);
 484
 485        if (ret || !(*ifp) || !(*ifp)->ndev) {
 486                if (ret != -ENODATA && *ifp)
 487                        (*ifp)->ndev->stats.rx_errors++;
 488                brcmu_pkt_buf_free_skb(skb);
 489                return -ENODATA;
 490        }
 491
 492        skb->protocol = eth_type_trans(skb, (*ifp)->ndev);
 493        return 0;
 494}
 495
 496void brcmf_rx_frame(struct device *dev, struct sk_buff *skb, bool handle_event)
 497{
 498        struct brcmf_if *ifp;
 499        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
 500        struct brcmf_pub *drvr = bus_if->drvr;
 501
 502        brcmf_dbg(DATA, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
 503
 504        if (brcmf_rx_hdrpull(drvr, skb, &ifp))
 505                return;
 506
 507        if (brcmf_proto_is_reorder_skb(skb)) {
 508                brcmf_proto_rxreorder(ifp, skb);
 509        } else {
 510                /* Process special event packets */
 511                if (handle_event)
 512                        brcmf_fweh_process_skb(ifp->drvr, skb,
 513                                               BCMILCP_SUBTYPE_VENDOR_LONG);
 514
 515                brcmf_netif_rx(ifp, skb);
 516        }
 517}
 518
 519void brcmf_rx_event(struct device *dev, struct sk_buff *skb)
 520{
 521        struct brcmf_if *ifp;
 522        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
 523        struct brcmf_pub *drvr = bus_if->drvr;
 524
 525        brcmf_dbg(EVENT, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
 526
 527        if (brcmf_rx_hdrpull(drvr, skb, &ifp))
 528                return;
 529
 530        brcmf_fweh_process_skb(ifp->drvr, skb, 0);
 531        brcmu_pkt_buf_free_skb(skb);
 532}
 533
 534void brcmf_txfinalize(struct brcmf_if *ifp, struct sk_buff *txp, bool success)
 535{
 536        struct ethhdr *eh;
 537        u16 type;
 538
 539        eh = (struct ethhdr *)(txp->data);
 540        type = ntohs(eh->h_proto);
 541
 542        if (type == ETH_P_PAE) {
 543                atomic_dec(&ifp->pend_8021x_cnt);
 544                if (waitqueue_active(&ifp->pend_8021x_wait))
 545                        wake_up(&ifp->pend_8021x_wait);
 546        }
 547
 548        if (!success)
 549                ifp->ndev->stats.tx_errors++;
 550
 551        brcmu_pkt_buf_free_skb(txp);
 552}
 553
 554static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
 555                                    struct ethtool_drvinfo *info)
 556{
 557        struct brcmf_if *ifp = netdev_priv(ndev);
 558        struct brcmf_pub *drvr = ifp->drvr;
 559        char drev[BRCMU_DOTREV_LEN] = "n/a";
 560
 561        if (drvr->revinfo.result == 0)
 562                brcmu_dotrev_str(drvr->revinfo.driverrev, drev);
 563        strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
 564        strlcpy(info->version, drev, sizeof(info->version));
 565        strlcpy(info->fw_version, drvr->fwver, sizeof(info->fw_version));
 566        strlcpy(info->bus_info, dev_name(drvr->bus_if->dev),
 567                sizeof(info->bus_info));
 568}
 569
 570static const struct ethtool_ops brcmf_ethtool_ops = {
 571        .get_drvinfo = brcmf_ethtool_get_drvinfo,
 572};
 573
 574static int brcmf_netdev_stop(struct net_device *ndev)
 575{
 576        struct brcmf_if *ifp = netdev_priv(ndev);
 577
 578        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
 579
 580        brcmf_cfg80211_down(ndev);
 581
 582        if (ifp->drvr->bus_if->state == BRCMF_BUS_UP)
 583                brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear", NULL, 0);
 584
 585        brcmf_net_setcarrier(ifp, false);
 586
 587        return 0;
 588}
 589
 590static int brcmf_netdev_open(struct net_device *ndev)
 591{
 592        struct brcmf_if *ifp = netdev_priv(ndev);
 593        struct brcmf_pub *drvr = ifp->drvr;
 594        struct brcmf_bus *bus_if = drvr->bus_if;
 595        u32 toe_ol;
 596
 597        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
 598
 599        /* If bus is not ready, can't continue */
 600        if (bus_if->state != BRCMF_BUS_UP) {
 601                bphy_err(drvr, "failed bus is not ready\n");
 602                return -EAGAIN;
 603        }
 604
 605        atomic_set(&ifp->pend_8021x_cnt, 0);
 606
 607        /* Get current TOE mode from dongle */
 608        if (brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_ol) >= 0
 609            && (toe_ol & TOE_TX_CSUM_OL) != 0)
 610                ndev->features |= NETIF_F_IP_CSUM;
 611        else
 612                ndev->features &= ~NETIF_F_IP_CSUM;
 613
 614        if (brcmf_cfg80211_up(ndev)) {
 615                bphy_err(drvr, "failed to bring up cfg80211\n");
 616                return -EIO;
 617        }
 618
 619        /* Clear, carrier, set when connected or AP mode. */
 620        netif_carrier_off(ndev);
 621        return 0;
 622}
 623
 624static const struct net_device_ops brcmf_netdev_ops_pri = {
 625        .ndo_open = brcmf_netdev_open,
 626        .ndo_stop = brcmf_netdev_stop,
 627        .ndo_start_xmit = brcmf_netdev_start_xmit,
 628        .ndo_set_mac_address = brcmf_netdev_set_mac_address,
 629        .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
 630};
 631
 632int brcmf_net_attach(struct brcmf_if *ifp, bool rtnl_locked)
 633{
 634        struct brcmf_pub *drvr = ifp->drvr;
 635        struct net_device *ndev;
 636        s32 err;
 637
 638        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
 639                  ifp->mac_addr);
 640        ndev = ifp->ndev;
 641
 642        /* set appropriate operations */
 643        ndev->netdev_ops = &brcmf_netdev_ops_pri;
 644
 645        ndev->needed_headroom += drvr->hdrlen;
 646        ndev->ethtool_ops = &brcmf_ethtool_ops;
 647
 648        /* set the mac address & netns */
 649        memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
 650        dev_net_set(ndev, wiphy_net(cfg_to_wiphy(drvr->config)));
 651
 652        INIT_WORK(&ifp->multicast_work, _brcmf_set_multicast_list);
 653        INIT_WORK(&ifp->ndoffload_work, _brcmf_update_ndtable);
 654
 655        if (rtnl_locked)
 656                err = register_netdevice(ndev);
 657        else
 658                err = register_netdev(ndev);
 659        if (err != 0) {
 660                bphy_err(drvr, "couldn't register the net device\n");
 661                goto fail;
 662        }
 663
 664        ndev->priv_destructor = brcmf_cfg80211_free_netdev;
 665        brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
 666        return 0;
 667
 668fail:
 669        drvr->iflist[ifp->bsscfgidx] = NULL;
 670        ndev->netdev_ops = NULL;
 671        return -EBADE;
 672}
 673
 674static void brcmf_net_detach(struct net_device *ndev, bool rtnl_locked)
 675{
 676        if (ndev->reg_state == NETREG_REGISTERED) {
 677                if (rtnl_locked)
 678                        unregister_netdevice(ndev);
 679                else
 680                        unregister_netdev(ndev);
 681        } else {
 682                brcmf_cfg80211_free_netdev(ndev);
 683                free_netdev(ndev);
 684        }
 685}
 686
 687void brcmf_net_setcarrier(struct brcmf_if *ifp, bool on)
 688{
 689        struct net_device *ndev;
 690
 691        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d carrier=%d\n", ifp->bsscfgidx,
 692                  on);
 693
 694        ndev = ifp->ndev;
 695        brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_DISCONNECTED, !on);
 696        if (on) {
 697                if (!netif_carrier_ok(ndev))
 698                        netif_carrier_on(ndev);
 699
 700        } else {
 701                if (netif_carrier_ok(ndev))
 702                        netif_carrier_off(ndev);
 703        }
 704}
 705
 706static int brcmf_net_p2p_open(struct net_device *ndev)
 707{
 708        brcmf_dbg(TRACE, "Enter\n");
 709
 710        return brcmf_cfg80211_up(ndev);
 711}
 712
 713static int brcmf_net_p2p_stop(struct net_device *ndev)
 714{
 715        brcmf_dbg(TRACE, "Enter\n");
 716
 717        return brcmf_cfg80211_down(ndev);
 718}
 719
 720static netdev_tx_t brcmf_net_p2p_start_xmit(struct sk_buff *skb,
 721                                            struct net_device *ndev)
 722{
 723        if (skb)
 724                dev_kfree_skb_any(skb);
 725
 726        return NETDEV_TX_OK;
 727}
 728
 729static const struct net_device_ops brcmf_netdev_ops_p2p = {
 730        .ndo_open = brcmf_net_p2p_open,
 731        .ndo_stop = brcmf_net_p2p_stop,
 732        .ndo_start_xmit = brcmf_net_p2p_start_xmit
 733};
 734
 735static int brcmf_net_p2p_attach(struct brcmf_if *ifp)
 736{
 737        struct brcmf_pub *drvr = ifp->drvr;
 738        struct net_device *ndev;
 739
 740        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
 741                  ifp->mac_addr);
 742        ndev = ifp->ndev;
 743
 744        ndev->netdev_ops = &brcmf_netdev_ops_p2p;
 745
 746        /* set the mac address */
 747        memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
 748
 749        if (register_netdev(ndev) != 0) {
 750                bphy_err(drvr, "couldn't register the p2p net device\n");
 751                goto fail;
 752        }
 753
 754        brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
 755
 756        return 0;
 757
 758fail:
 759        ifp->drvr->iflist[ifp->bsscfgidx] = NULL;
 760        ndev->netdev_ops = NULL;
 761        return -EBADE;
 762}
 763
 764struct brcmf_if *brcmf_add_if(struct brcmf_pub *drvr, s32 bsscfgidx, s32 ifidx,
 765                              bool is_p2pdev, const char *name, u8 *mac_addr)
 766{
 767        struct brcmf_if *ifp;
 768        struct net_device *ndev;
 769
 770        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx, ifidx);
 771
 772        ifp = drvr->iflist[bsscfgidx];
 773        /*
 774         * Delete the existing interface before overwriting it
 775         * in case we missed the BRCMF_E_IF_DEL event.
 776         */
 777        if (ifp) {
 778                if (ifidx) {
 779                        bphy_err(drvr, "ERROR: netdev:%s already exists\n",
 780                                 ifp->ndev->name);
 781                        netif_stop_queue(ifp->ndev);
 782                        brcmf_net_detach(ifp->ndev, false);
 783                        drvr->iflist[bsscfgidx] = NULL;
 784                } else {
 785                        brcmf_dbg(INFO, "netdev:%s ignore IF event\n",
 786                                  ifp->ndev->name);
 787                        return ERR_PTR(-EINVAL);
 788                }
 789        }
 790
 791        if (!drvr->settings->p2p_enable && is_p2pdev) {
 792                /* this is P2P_DEVICE interface */
 793                brcmf_dbg(INFO, "allocate non-netdev interface\n");
 794                ifp = kzalloc(sizeof(*ifp), GFP_KERNEL);
 795                if (!ifp)
 796                        return ERR_PTR(-ENOMEM);
 797        } else {
 798                brcmf_dbg(INFO, "allocate netdev interface\n");
 799                /* Allocate netdev, including space for private structure */
 800                ndev = alloc_netdev(sizeof(*ifp), is_p2pdev ? "p2p%d" : name,
 801                                    NET_NAME_UNKNOWN, ether_setup);
 802                if (!ndev)
 803                        return ERR_PTR(-ENOMEM);
 804
 805                ndev->needs_free_netdev = true;
 806                ifp = netdev_priv(ndev);
 807                ifp->ndev = ndev;
 808                /* store mapping ifidx to bsscfgidx */
 809                if (drvr->if2bss[ifidx] == BRCMF_BSSIDX_INVALID)
 810                        drvr->if2bss[ifidx] = bsscfgidx;
 811        }
 812
 813        ifp->drvr = drvr;
 814        drvr->iflist[bsscfgidx] = ifp;
 815        ifp->ifidx = ifidx;
 816        ifp->bsscfgidx = bsscfgidx;
 817
 818        init_waitqueue_head(&ifp->pend_8021x_wait);
 819        spin_lock_init(&ifp->netif_stop_lock);
 820
 821        if (mac_addr != NULL)
 822                memcpy(ifp->mac_addr, mac_addr, ETH_ALEN);
 823
 824        brcmf_dbg(TRACE, " ==== pid:%x, if:%s (%pM) created ===\n",
 825                  current->pid, name, ifp->mac_addr);
 826
 827        return ifp;
 828}
 829
 830static void brcmf_del_if(struct brcmf_pub *drvr, s32 bsscfgidx,
 831                         bool rtnl_locked)
 832{
 833        struct brcmf_if *ifp;
 834        int ifidx;
 835
 836        ifp = drvr->iflist[bsscfgidx];
 837        if (!ifp) {
 838                bphy_err(drvr, "Null interface, bsscfgidx=%d\n", bsscfgidx);
 839                return;
 840        }
 841        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx,
 842                  ifp->ifidx);
 843        ifidx = ifp->ifidx;
 844
 845        if (ifp->ndev) {
 846                if (bsscfgidx == 0) {
 847                        if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
 848                                rtnl_lock();
 849                                brcmf_netdev_stop(ifp->ndev);
 850                                rtnl_unlock();
 851                        }
 852                } else {
 853                        netif_stop_queue(ifp->ndev);
 854                }
 855
 856                if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
 857                        cancel_work_sync(&ifp->multicast_work);
 858                        cancel_work_sync(&ifp->ndoffload_work);
 859                }
 860                brcmf_net_detach(ifp->ndev, rtnl_locked);
 861        } else {
 862                /* Only p2p device interfaces which get dynamically created
 863                 * end up here. In this case the p2p module should be informed
 864                 * about the removal of the interface within the firmware. If
 865                 * not then p2p commands towards the firmware will cause some
 866                 * serious troublesome side effects. The p2p module will clean
 867                 * up the ifp if needed.
 868                 */
 869                brcmf_p2p_ifp_removed(ifp, rtnl_locked);
 870                kfree(ifp);
 871        }
 872
 873        drvr->iflist[bsscfgidx] = NULL;
 874        if (drvr->if2bss[ifidx] == bsscfgidx)
 875                drvr->if2bss[ifidx] = BRCMF_BSSIDX_INVALID;
 876}
 877
 878void brcmf_remove_interface(struct brcmf_if *ifp, bool rtnl_locked)
 879{
 880        if (!ifp || WARN_ON(ifp->drvr->iflist[ifp->bsscfgidx] != ifp))
 881                return;
 882        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp->bsscfgidx,
 883                  ifp->ifidx);
 884        brcmf_proto_del_if(ifp->drvr, ifp);
 885        brcmf_del_if(ifp->drvr, ifp->bsscfgidx, rtnl_locked);
 886}
 887
 888static int brcmf_psm_watchdog_notify(struct brcmf_if *ifp,
 889                                     const struct brcmf_event_msg *evtmsg,
 890                                     void *data)
 891{
 892        struct brcmf_pub *drvr = ifp->drvr;
 893        int err;
 894
 895        brcmf_dbg(TRACE, "enter: bsscfgidx=%d\n", ifp->bsscfgidx);
 896
 897        bphy_err(drvr, "PSM's watchdog has fired!\n");
 898
 899        err = brcmf_debug_create_memdump(ifp->drvr->bus_if, data,
 900                                         evtmsg->datalen);
 901        if (err)
 902                bphy_err(drvr, "Failed to get memory dump, %d\n", err);
 903
 904        return err;
 905}
 906
 907#ifdef CONFIG_INET
 908#define ARPOL_MAX_ENTRIES       8
 909static int brcmf_inetaddr_changed(struct notifier_block *nb,
 910                                  unsigned long action, void *data)
 911{
 912        struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
 913                                              inetaddr_notifier);
 914        struct in_ifaddr *ifa = data;
 915        struct net_device *ndev = ifa->ifa_dev->dev;
 916        struct brcmf_if *ifp;
 917        int idx, i, ret;
 918        u32 val;
 919        __be32 addr_table[ARPOL_MAX_ENTRIES] = {0};
 920
 921        /* Find out if the notification is meant for us */
 922        for (idx = 0; idx < BRCMF_MAX_IFS; idx++) {
 923                ifp = drvr->iflist[idx];
 924                if (ifp && ifp->ndev == ndev)
 925                        break;
 926                if (idx == BRCMF_MAX_IFS - 1)
 927                        return NOTIFY_DONE;
 928        }
 929
 930        /* check if arp offload is supported */
 931        ret = brcmf_fil_iovar_int_get(ifp, "arpoe", &val);
 932        if (ret)
 933                return NOTIFY_OK;
 934
 935        /* old version only support primary index */
 936        ret = brcmf_fil_iovar_int_get(ifp, "arp_version", &val);
 937        if (ret)
 938                val = 1;
 939        if (val == 1)
 940                ifp = drvr->iflist[0];
 941
 942        /* retrieve the table from firmware */
 943        ret = brcmf_fil_iovar_data_get(ifp, "arp_hostip", addr_table,
 944                                       sizeof(addr_table));
 945        if (ret) {
 946                bphy_err(drvr, "fail to get arp ip table err:%d\n", ret);
 947                return NOTIFY_OK;
 948        }
 949
 950        for (i = 0; i < ARPOL_MAX_ENTRIES; i++)
 951                if (ifa->ifa_address == addr_table[i])
 952                        break;
 953
 954        switch (action) {
 955        case NETDEV_UP:
 956                if (i == ARPOL_MAX_ENTRIES) {
 957                        brcmf_dbg(TRACE, "add %pI4 to arp table\n",
 958                                  &ifa->ifa_address);
 959                        /* set it directly */
 960                        ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
 961                                &ifa->ifa_address, sizeof(ifa->ifa_address));
 962                        if (ret)
 963                                bphy_err(drvr, "add arp ip err %d\n", ret);
 964                }
 965                break;
 966        case NETDEV_DOWN:
 967                if (i < ARPOL_MAX_ENTRIES) {
 968                        addr_table[i] = 0;
 969                        brcmf_dbg(TRACE, "remove %pI4 from arp table\n",
 970                                  &ifa->ifa_address);
 971                        /* clear the table in firmware */
 972                        ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear",
 973                                                       NULL, 0);
 974                        if (ret) {
 975                                bphy_err(drvr, "fail to clear arp ip table err:%d\n",
 976                                         ret);
 977                                return NOTIFY_OK;
 978                        }
 979                        for (i = 0; i < ARPOL_MAX_ENTRIES; i++) {
 980                                if (addr_table[i] == 0)
 981                                        continue;
 982                                ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
 983                                                               &addr_table[i],
 984                                                               sizeof(addr_table[i]));
 985                                if (ret)
 986                                        bphy_err(drvr, "add arp ip err %d\n",
 987                                                 ret);
 988                        }
 989                }
 990                break;
 991        default:
 992                break;
 993        }
 994
 995        return NOTIFY_OK;
 996}
 997#endif
 998
 999#if IS_ENABLED(CONFIG_IPV6)
1000static int brcmf_inet6addr_changed(struct notifier_block *nb,
1001                                   unsigned long action, void *data)
1002{
1003        struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
1004                                              inet6addr_notifier);
1005        struct inet6_ifaddr *ifa = data;
1006        struct brcmf_if *ifp;
1007        int i;
1008        struct in6_addr *table;
1009
1010        /* Only handle primary interface */
1011        ifp = drvr->iflist[0];
1012        if (!ifp)
1013                return NOTIFY_DONE;
1014        if (ifp->ndev != ifa->idev->dev)
1015                return NOTIFY_DONE;
1016
1017        table = ifp->ipv6_addr_tbl;
1018        for (i = 0; i < NDOL_MAX_ENTRIES; i++)
1019                if (ipv6_addr_equal(&ifa->addr, &table[i]))
1020                        break;
1021
1022        switch (action) {
1023        case NETDEV_UP:
1024                if (i == NDOL_MAX_ENTRIES) {
1025                        if (ifp->ipv6addr_idx < NDOL_MAX_ENTRIES) {
1026                                table[ifp->ipv6addr_idx++] = ifa->addr;
1027                        } else {
1028                                for (i = 0; i < NDOL_MAX_ENTRIES - 1; i++)
1029                                        table[i] = table[i + 1];
1030                                table[NDOL_MAX_ENTRIES - 1] = ifa->addr;
1031                        }
1032                }
1033                break;
1034        case NETDEV_DOWN:
1035                if (i < NDOL_MAX_ENTRIES) {
1036                        for (; i < ifp->ipv6addr_idx - 1; i++)
1037                                table[i] = table[i + 1];
1038                        memset(&table[i], 0, sizeof(table[i]));
1039                        ifp->ipv6addr_idx--;
1040                }
1041                break;
1042        default:
1043                break;
1044        }
1045
1046        schedule_work(&ifp->ndoffload_work);
1047
1048        return NOTIFY_OK;
1049}
1050#endif
1051
1052static int brcmf_revinfo_read(struct seq_file *s, void *data)
1053{
1054        struct brcmf_bus *bus_if = dev_get_drvdata(s->private);
1055        struct brcmf_rev_info *ri = &bus_if->drvr->revinfo;
1056        char drev[BRCMU_DOTREV_LEN];
1057        char brev[BRCMU_BOARDREV_LEN];
1058
1059        seq_printf(s, "vendorid: 0x%04x\n", ri->vendorid);
1060        seq_printf(s, "deviceid: 0x%04x\n", ri->deviceid);
1061        seq_printf(s, "radiorev: %s\n", brcmu_dotrev_str(ri->radiorev, drev));
1062        seq_printf(s, "chip: %s\n", ri->chipname);
1063        seq_printf(s, "chippkg: %u\n", ri->chippkg);
1064        seq_printf(s, "corerev: %u\n", ri->corerev);
1065        seq_printf(s, "boardid: 0x%04x\n", ri->boardid);
1066        seq_printf(s, "boardvendor: 0x%04x\n", ri->boardvendor);
1067        seq_printf(s, "boardrev: %s\n", brcmu_boardrev_str(ri->boardrev, brev));
1068        seq_printf(s, "driverrev: %s\n", brcmu_dotrev_str(ri->driverrev, drev));
1069        seq_printf(s, "ucoderev: %u\n", ri->ucoderev);
1070        seq_printf(s, "bus: %u\n", ri->bus);
1071        seq_printf(s, "phytype: %u\n", ri->phytype);
1072        seq_printf(s, "phyrev: %u\n", ri->phyrev);
1073        seq_printf(s, "anarev: %u\n", ri->anarev);
1074        seq_printf(s, "nvramrev: %08x\n", ri->nvramrev);
1075
1076        seq_printf(s, "clmver: %s\n", bus_if->drvr->clmver);
1077
1078        return 0;
1079}
1080
1081static void brcmf_core_bus_reset(struct work_struct *work)
1082{
1083        struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
1084                                              bus_reset);
1085
1086        brcmf_bus_reset(drvr->bus_if);
1087}
1088
1089static ssize_t bus_reset_write(struct file *file, const char __user *user_buf,
1090                               size_t count, loff_t *ppos)
1091{
1092        struct brcmf_pub *drvr = file->private_data;
1093        u8 value;
1094
1095        if (kstrtou8_from_user(user_buf, count, 0, &value))
1096                return -EINVAL;
1097
1098        if (value != 1)
1099                return -EINVAL;
1100
1101        schedule_work(&drvr->bus_reset);
1102
1103        return count;
1104}
1105
1106static const struct file_operations bus_reset_fops = {
1107        .open   = simple_open,
1108        .llseek = no_llseek,
1109        .write  = bus_reset_write,
1110};
1111
1112static int brcmf_bus_started(struct brcmf_pub *drvr, struct cfg80211_ops *ops)
1113{
1114        int ret = -1;
1115        struct brcmf_bus *bus_if = drvr->bus_if;
1116        struct brcmf_if *ifp;
1117        struct brcmf_if *p2p_ifp;
1118
1119        brcmf_dbg(TRACE, "\n");
1120
1121        /* add primary networking interface */
1122        ifp = brcmf_add_if(drvr, 0, 0, false, "wlan%d", NULL);
1123        if (IS_ERR(ifp))
1124                return PTR_ERR(ifp);
1125
1126        p2p_ifp = NULL;
1127
1128        /* signal bus ready */
1129        brcmf_bus_change_state(bus_if, BRCMF_BUS_UP);
1130
1131        /* do bus specific preinit here */
1132        ret = brcmf_bus_preinit(bus_if);
1133        if (ret < 0)
1134                goto fail;
1135
1136        /* Bus is ready, do any initialization */
1137        ret = brcmf_c_preinit_dcmds(ifp);
1138        if (ret < 0)
1139                goto fail;
1140
1141        brcmf_feat_attach(drvr);
1142
1143        ret = brcmf_proto_init_done(drvr);
1144        if (ret < 0)
1145                goto fail;
1146
1147        brcmf_proto_add_if(drvr, ifp);
1148
1149        drvr->config = brcmf_cfg80211_attach(drvr, ops,
1150                                             drvr->settings->p2p_enable);
1151        if (drvr->config == NULL) {
1152                ret = -ENOMEM;
1153                goto fail;
1154        }
1155
1156        ret = brcmf_net_attach(ifp, false);
1157
1158        if ((!ret) && (drvr->settings->p2p_enable)) {
1159                p2p_ifp = drvr->iflist[1];
1160                if (p2p_ifp)
1161                        ret = brcmf_net_p2p_attach(p2p_ifp);
1162        }
1163
1164        if (ret)
1165                goto fail;
1166
1167#ifdef CONFIG_INET
1168        drvr->inetaddr_notifier.notifier_call = brcmf_inetaddr_changed;
1169        ret = register_inetaddr_notifier(&drvr->inetaddr_notifier);
1170        if (ret)
1171                goto fail;
1172
1173#if IS_ENABLED(CONFIG_IPV6)
1174        drvr->inet6addr_notifier.notifier_call = brcmf_inet6addr_changed;
1175        ret = register_inet6addr_notifier(&drvr->inet6addr_notifier);
1176        if (ret) {
1177                unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1178                goto fail;
1179        }
1180#endif
1181#endif /* CONFIG_INET */
1182
1183        INIT_WORK(&drvr->bus_reset, brcmf_core_bus_reset);
1184
1185        /* populate debugfs */
1186        brcmf_debugfs_add_entry(drvr, "revinfo", brcmf_revinfo_read);
1187        debugfs_create_file("reset", 0600, brcmf_debugfs_get_devdir(drvr), drvr,
1188                            &bus_reset_fops);
1189        brcmf_feat_debugfs_create(drvr);
1190        brcmf_proto_debugfs_create(drvr);
1191        brcmf_bus_debugfs_create(bus_if);
1192
1193        return 0;
1194
1195fail:
1196        bphy_err(drvr, "failed: %d\n", ret);
1197        if (drvr->config) {
1198                brcmf_cfg80211_detach(drvr->config);
1199                drvr->config = NULL;
1200        }
1201        brcmf_net_detach(ifp->ndev, false);
1202        if (p2p_ifp)
1203                brcmf_net_detach(p2p_ifp->ndev, false);
1204        drvr->iflist[0] = NULL;
1205        drvr->iflist[1] = NULL;
1206        if (drvr->settings->ignore_probe_fail)
1207                ret = 0;
1208
1209        return ret;
1210}
1211
1212int brcmf_alloc(struct device *dev, struct brcmf_mp_device *settings)
1213{
1214        struct wiphy *wiphy;
1215        struct cfg80211_ops *ops;
1216        struct brcmf_pub *drvr = NULL;
1217
1218        brcmf_dbg(TRACE, "Enter\n");
1219
1220        ops = brcmf_cfg80211_get_ops(settings);
1221        if (!ops)
1222                return -ENOMEM;
1223
1224        wiphy = wiphy_new(ops, sizeof(*drvr));
1225        if (!wiphy) {
1226                kfree(ops);
1227                return -ENOMEM;
1228        }
1229
1230        set_wiphy_dev(wiphy, dev);
1231        drvr = wiphy_priv(wiphy);
1232        drvr->wiphy = wiphy;
1233        drvr->ops = ops;
1234        drvr->bus_if = dev_get_drvdata(dev);
1235        drvr->bus_if->drvr = drvr;
1236        drvr->settings = settings;
1237
1238        return 0;
1239}
1240
1241int brcmf_attach(struct device *dev)
1242{
1243        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1244        struct brcmf_pub *drvr = bus_if->drvr;
1245        int ret = 0;
1246        int i;
1247
1248        brcmf_dbg(TRACE, "Enter\n");
1249
1250        for (i = 0; i < ARRAY_SIZE(drvr->if2bss); i++)
1251                drvr->if2bss[i] = BRCMF_BSSIDX_INVALID;
1252
1253        mutex_init(&drvr->proto_block);
1254
1255        /* Link to bus module */
1256        drvr->hdrlen = 0;
1257
1258        /* Attach and link in the protocol */
1259        ret = brcmf_proto_attach(drvr);
1260        if (ret != 0) {
1261                bphy_err(drvr, "brcmf_prot_attach failed\n");
1262                goto fail;
1263        }
1264
1265        /* Attach to events important for core code */
1266        brcmf_fweh_register(drvr, BRCMF_E_PSM_WATCHDOG,
1267                            brcmf_psm_watchdog_notify);
1268
1269        /* attach firmware event handler */
1270        brcmf_fweh_attach(drvr);
1271
1272        ret = brcmf_bus_started(drvr, drvr->ops);
1273        if (ret != 0) {
1274                bphy_err(drvr, "dongle is not responding: err=%d\n", ret);
1275                goto fail;
1276        }
1277
1278        return 0;
1279
1280fail:
1281        brcmf_detach(dev);
1282
1283        return ret;
1284}
1285
1286void brcmf_bus_add_txhdrlen(struct device *dev, uint len)
1287{
1288        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1289        struct brcmf_pub *drvr = bus_if->drvr;
1290
1291        if (drvr) {
1292                drvr->hdrlen += len;
1293        }
1294}
1295
1296void brcmf_dev_reset(struct device *dev)
1297{
1298        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1299        struct brcmf_pub *drvr = bus_if->drvr;
1300
1301        if (drvr == NULL)
1302                return;
1303
1304        if (drvr->iflist[0])
1305                brcmf_fil_cmd_int_set(drvr->iflist[0], BRCMF_C_TERMINATED, 1);
1306}
1307
1308void brcmf_dev_coredump(struct device *dev)
1309{
1310        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1311
1312        if (brcmf_debug_create_memdump(bus_if, NULL, 0) < 0)
1313                brcmf_dbg(TRACE, "failed to create coredump\n");
1314}
1315
1316void brcmf_fw_crashed(struct device *dev)
1317{
1318        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1319        struct brcmf_pub *drvr = bus_if->drvr;
1320
1321        bphy_err(drvr, "Firmware has halted or crashed\n");
1322
1323        brcmf_dev_coredump(dev);
1324
1325        schedule_work(&drvr->bus_reset);
1326}
1327
1328void brcmf_detach(struct device *dev)
1329{
1330        s32 i;
1331        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1332        struct brcmf_pub *drvr = bus_if->drvr;
1333
1334        brcmf_dbg(TRACE, "Enter\n");
1335
1336        if (drvr == NULL)
1337                return;
1338
1339#ifdef CONFIG_INET
1340        unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1341#endif
1342
1343#if IS_ENABLED(CONFIG_IPV6)
1344        unregister_inet6addr_notifier(&drvr->inet6addr_notifier);
1345#endif
1346
1347        brcmf_bus_change_state(bus_if, BRCMF_BUS_DOWN);
1348        brcmf_bus_stop(drvr->bus_if);
1349
1350        brcmf_fweh_detach(drvr);
1351        brcmf_proto_detach(drvr);
1352
1353        if (drvr->mon_if) {
1354                brcmf_net_detach(drvr->mon_if->ndev, false);
1355                drvr->mon_if = NULL;
1356        }
1357
1358        /* make sure primary interface removed last */
1359        for (i = BRCMF_MAX_IFS - 1; i > -1; i--) {
1360                if (drvr->iflist[i])
1361                        brcmf_del_if(drvr, drvr->iflist[i]->bsscfgidx, false);
1362        }
1363
1364        if (drvr->config) {
1365                brcmf_p2p_detach(&drvr->config->p2p);
1366                brcmf_cfg80211_detach(drvr->config);
1367                drvr->config = NULL;
1368        }
1369}
1370
1371void brcmf_free(struct device *dev)
1372{
1373        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1374        struct brcmf_pub *drvr = bus_if->drvr;
1375
1376        if (!drvr)
1377                return;
1378
1379        bus_if->drvr = NULL;
1380
1381        kfree(drvr->ops);
1382
1383        wiphy_free(drvr->wiphy);
1384}
1385
1386s32 brcmf_iovar_data_set(struct device *dev, char *name, void *data, u32 len)
1387{
1388        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1389        struct brcmf_if *ifp = bus_if->drvr->iflist[0];
1390
1391        return brcmf_fil_iovar_data_set(ifp, name, data, len);
1392}
1393
1394static int brcmf_get_pend_8021x_cnt(struct brcmf_if *ifp)
1395{
1396        return atomic_read(&ifp->pend_8021x_cnt);
1397}
1398
1399int brcmf_netdev_wait_pend8021x(struct brcmf_if *ifp)
1400{
1401        struct brcmf_pub *drvr = ifp->drvr;
1402        int err;
1403
1404        err = wait_event_timeout(ifp->pend_8021x_wait,
1405                                 !brcmf_get_pend_8021x_cnt(ifp),
1406                                 MAX_WAIT_FOR_8021X_TX);
1407
1408        if (!err)
1409                bphy_err(drvr, "Timed out waiting for no pending 802.1x packets\n");
1410
1411        return !err;
1412}
1413
1414void brcmf_bus_change_state(struct brcmf_bus *bus, enum brcmf_bus_state state)
1415{
1416        struct brcmf_pub *drvr = bus->drvr;
1417        struct net_device *ndev;
1418        int ifidx;
1419
1420        brcmf_dbg(TRACE, "%d -> %d\n", bus->state, state);
1421
1422        if (!drvr) {
1423                brcmf_dbg(INFO, "ignoring transition, bus not attached yet\n");
1424                return;
1425        }
1426
1427        bus->state = state;
1428
1429        if (state == BRCMF_BUS_UP) {
1430                for (ifidx = 0; ifidx < BRCMF_MAX_IFS; ifidx++) {
1431                        if ((drvr->iflist[ifidx]) &&
1432                            (drvr->iflist[ifidx]->ndev)) {
1433                                ndev = drvr->iflist[ifidx]->ndev;
1434                                if (netif_queue_stopped(ndev))
1435                                        netif_wake_queue(ndev);
1436                        }
1437                }
1438        }
1439}
1440
1441static void brcmf_driver_register(struct work_struct *work)
1442{
1443#ifdef CONFIG_BRCMFMAC_SDIO
1444        brcmf_sdio_register();
1445#endif
1446#ifdef CONFIG_BRCMFMAC_USB
1447        brcmf_usb_register();
1448#endif
1449#ifdef CONFIG_BRCMFMAC_PCIE
1450        brcmf_pcie_register();
1451#endif
1452}
1453static DECLARE_WORK(brcmf_driver_work, brcmf_driver_register);
1454
1455int __init brcmf_core_init(void)
1456{
1457        if (!schedule_work(&brcmf_driver_work))
1458                return -EBUSY;
1459
1460        return 0;
1461}
1462
1463void __exit brcmf_core_exit(void)
1464{
1465        cancel_work_sync(&brcmf_driver_work);
1466
1467#ifdef CONFIG_BRCMFMAC_SDIO
1468        brcmf_sdio_exit();
1469#endif
1470#ifdef CONFIG_BRCMFMAC_USB
1471        brcmf_usb_exit();
1472#endif
1473#ifdef CONFIG_BRCMFMAC_PCIE
1474        brcmf_pcie_exit();
1475#endif
1476}
1477
1478