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        /* set pacing shift for packet aggregation */
 356        sk_pacing_shift_update(skb->sk, 8);
 357
 358        ret = brcmf_proto_tx_queue_data(drvr, ifp->ifidx, skb);
 359        if (ret < 0)
 360                brcmf_txfinalize(ifp, skb, false);
 361
 362done:
 363        if (ret) {
 364                ndev->stats.tx_dropped++;
 365        } else {
 366                ndev->stats.tx_packets++;
 367                ndev->stats.tx_bytes += skb->len;
 368        }
 369
 370        /* Return ok: we always eat the packet */
 371        return NETDEV_TX_OK;
 372}
 373
 374void brcmf_txflowblock_if(struct brcmf_if *ifp,
 375                          enum brcmf_netif_stop_reason reason, bool state)
 376{
 377        unsigned long flags;
 378
 379        if (!ifp || !ifp->ndev)
 380                return;
 381
 382        brcmf_dbg(TRACE, "enter: bsscfgidx=%d stop=0x%X reason=%d state=%d\n",
 383                  ifp->bsscfgidx, ifp->netif_stop, reason, state);
 384
 385        spin_lock_irqsave(&ifp->netif_stop_lock, flags);
 386        if (state) {
 387                if (!ifp->netif_stop)
 388                        netif_stop_queue(ifp->ndev);
 389                ifp->netif_stop |= reason;
 390        } else {
 391                ifp->netif_stop &= ~reason;
 392                if (!ifp->netif_stop)
 393                        netif_wake_queue(ifp->ndev);
 394        }
 395        spin_unlock_irqrestore(&ifp->netif_stop_lock, flags);
 396}
 397
 398void brcmf_netif_rx(struct brcmf_if *ifp, struct sk_buff *skb, bool inirq)
 399{
 400        /* Most of Broadcom's firmwares send 802.11f ADD frame every time a new
 401         * STA connects to the AP interface. This is an obsoleted standard most
 402         * users don't use, so don't pass these frames up unless requested.
 403         */
 404        if (!ifp->drvr->settings->iapp && brcmf_skb_is_iapp(skb)) {
 405                brcmu_pkt_buf_free_skb(skb);
 406                return;
 407        }
 408
 409        if (skb->pkt_type == PACKET_MULTICAST)
 410                ifp->ndev->stats.multicast++;
 411
 412        if (!(ifp->ndev->flags & IFF_UP)) {
 413                brcmu_pkt_buf_free_skb(skb);
 414                return;
 415        }
 416
 417        ifp->ndev->stats.rx_bytes += skb->len;
 418        ifp->ndev->stats.rx_packets++;
 419
 420        brcmf_dbg(DATA, "rx proto=0x%X\n", ntohs(skb->protocol));
 421        if (inirq) {
 422                netif_rx(skb);
 423        } else {
 424                /* If the receive is not processed inside an ISR,
 425                 * the softirqd must be woken explicitly to service
 426                 * the NET_RX_SOFTIRQ.  This is handled by netif_rx_ni().
 427                 */
 428                netif_rx_ni(skb);
 429        }
 430}
 431
 432void brcmf_netif_mon_rx(struct brcmf_if *ifp, struct sk_buff *skb)
 433{
 434        if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FMT_RADIOTAP)) {
 435                /* Do nothing */
 436        } else if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FMT_HW_RX_HDR)) {
 437                struct wlc_d11rxhdr *wlc_rxhdr = (struct wlc_d11rxhdr *)skb->data;
 438                struct ieee80211_radiotap_header *radiotap;
 439                unsigned int offset;
 440                u16 RxStatus1;
 441
 442                RxStatus1 = le16_to_cpu(wlc_rxhdr->rxhdr.RxStatus1);
 443
 444                offset = sizeof(struct wlc_d11rxhdr);
 445                /* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU
 446                 * subframes
 447                 */
 448                if (RxStatus1 & RXS_PBPRES)
 449                        offset += 2;
 450                offset += D11_PHY_HDR_LEN;
 451
 452                skb_pull(skb, offset);
 453
 454                /* TODO: use RX header to fill some radiotap data */
 455                radiotap = skb_push(skb, sizeof(*radiotap));
 456                memset(radiotap, 0, sizeof(*radiotap));
 457                radiotap->it_len = cpu_to_le16(sizeof(*radiotap));
 458
 459                /* TODO: 4 bytes with receive status? */
 460                skb->len -= 4;
 461        } else {
 462                struct ieee80211_radiotap_header *radiotap;
 463
 464                /* TODO: use RX status to fill some radiotap data */
 465                radiotap = skb_push(skb, sizeof(*radiotap));
 466                memset(radiotap, 0, sizeof(*radiotap));
 467                radiotap->it_len = cpu_to_le16(sizeof(*radiotap));
 468
 469                /* TODO: 4 bytes with receive status? */
 470                skb->len -= 4;
 471        }
 472
 473        skb->dev = ifp->ndev;
 474        skb_reset_mac_header(skb);
 475        skb->pkt_type = PACKET_OTHERHOST;
 476        skb->protocol = htons(ETH_P_802_2);
 477
 478        brcmf_netif_rx(ifp, skb, false);
 479}
 480
 481static int brcmf_rx_hdrpull(struct brcmf_pub *drvr, struct sk_buff *skb,
 482                            struct brcmf_if **ifp)
 483{
 484        int ret;
 485
 486        /* process and remove protocol-specific header */
 487        ret = brcmf_proto_hdrpull(drvr, true, skb, ifp);
 488
 489        if (ret || !(*ifp) || !(*ifp)->ndev) {
 490                if (ret != -ENODATA && *ifp && (*ifp)->ndev)
 491                        (*ifp)->ndev->stats.rx_errors++;
 492                brcmu_pkt_buf_free_skb(skb);
 493                return -ENODATA;
 494        }
 495
 496        skb->protocol = eth_type_trans(skb, (*ifp)->ndev);
 497        return 0;
 498}
 499
 500void brcmf_rx_frame(struct device *dev, struct sk_buff *skb, bool handle_event,
 501                    bool inirq)
 502{
 503        struct brcmf_if *ifp;
 504        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
 505        struct brcmf_pub *drvr = bus_if->drvr;
 506
 507        brcmf_dbg(DATA, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
 508
 509        if (brcmf_rx_hdrpull(drvr, skb, &ifp))
 510                return;
 511
 512        if (brcmf_proto_is_reorder_skb(skb)) {
 513                brcmf_proto_rxreorder(ifp, skb, inirq);
 514        } else {
 515                /* Process special event packets */
 516                if (handle_event) {
 517                        gfp_t gfp = inirq ? GFP_ATOMIC : GFP_KERNEL;
 518
 519                        brcmf_fweh_process_skb(ifp->drvr, skb,
 520                                               BCMILCP_SUBTYPE_VENDOR_LONG, gfp);
 521                }
 522                brcmf_netif_rx(ifp, skb, inirq);
 523        }
 524}
 525
 526void brcmf_rx_event(struct device *dev, struct sk_buff *skb)
 527{
 528        struct brcmf_if *ifp;
 529        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
 530        struct brcmf_pub *drvr = bus_if->drvr;
 531
 532        brcmf_dbg(EVENT, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
 533
 534        if (brcmf_rx_hdrpull(drvr, skb, &ifp))
 535                return;
 536
 537        brcmf_fweh_process_skb(ifp->drvr, skb, 0, GFP_KERNEL);
 538        brcmu_pkt_buf_free_skb(skb);
 539}
 540
 541void brcmf_txfinalize(struct brcmf_if *ifp, struct sk_buff *txp, bool success)
 542{
 543        struct ethhdr *eh;
 544        u16 type;
 545
 546        eh = (struct ethhdr *)(txp->data);
 547        type = ntohs(eh->h_proto);
 548
 549        if (type == ETH_P_PAE) {
 550                atomic_dec(&ifp->pend_8021x_cnt);
 551                if (waitqueue_active(&ifp->pend_8021x_wait))
 552                        wake_up(&ifp->pend_8021x_wait);
 553        }
 554
 555        if (!success)
 556                ifp->ndev->stats.tx_errors++;
 557
 558        brcmu_pkt_buf_free_skb(txp);
 559}
 560
 561static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
 562                                    struct ethtool_drvinfo *info)
 563{
 564        struct brcmf_if *ifp = netdev_priv(ndev);
 565        struct brcmf_pub *drvr = ifp->drvr;
 566        char drev[BRCMU_DOTREV_LEN] = "n/a";
 567
 568        if (drvr->revinfo.result == 0)
 569                brcmu_dotrev_str(drvr->revinfo.driverrev, drev);
 570        strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
 571        strlcpy(info->version, drev, sizeof(info->version));
 572        strlcpy(info->fw_version, drvr->fwver, sizeof(info->fw_version));
 573        strlcpy(info->bus_info, dev_name(drvr->bus_if->dev),
 574                sizeof(info->bus_info));
 575}
 576
 577static const struct ethtool_ops brcmf_ethtool_ops = {
 578        .get_drvinfo = brcmf_ethtool_get_drvinfo,
 579};
 580
 581static int brcmf_netdev_stop(struct net_device *ndev)
 582{
 583        struct brcmf_if *ifp = netdev_priv(ndev);
 584
 585        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
 586
 587        brcmf_cfg80211_down(ndev);
 588
 589        brcmf_net_setcarrier(ifp, false);
 590
 591        return 0;
 592}
 593
 594static int brcmf_netdev_open(struct net_device *ndev)
 595{
 596        struct brcmf_if *ifp = netdev_priv(ndev);
 597        struct brcmf_pub *drvr = ifp->drvr;
 598        struct brcmf_bus *bus_if = drvr->bus_if;
 599        u32 toe_ol;
 600
 601        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
 602
 603        /* If bus is not ready, can't continue */
 604        if (bus_if->state != BRCMF_BUS_UP) {
 605                bphy_err(drvr, "failed bus is not ready\n");
 606                return -EAGAIN;
 607        }
 608
 609        atomic_set(&ifp->pend_8021x_cnt, 0);
 610
 611        /* Get current TOE mode from dongle */
 612        if (brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_ol) >= 0
 613            && (toe_ol & TOE_TX_CSUM_OL) != 0)
 614                ndev->features |= NETIF_F_IP_CSUM;
 615        else
 616                ndev->features &= ~NETIF_F_IP_CSUM;
 617
 618        if (brcmf_cfg80211_up(ndev)) {
 619                bphy_err(drvr, "failed to bring up cfg80211\n");
 620                return -EIO;
 621        }
 622
 623        /* Clear, carrier, set when connected or AP mode. */
 624        netif_carrier_off(ndev);
 625        return 0;
 626}
 627
 628static const struct net_device_ops brcmf_netdev_ops_pri = {
 629        .ndo_open = brcmf_netdev_open,
 630        .ndo_stop = brcmf_netdev_stop,
 631        .ndo_start_xmit = brcmf_netdev_start_xmit,
 632        .ndo_set_mac_address = brcmf_netdev_set_mac_address,
 633        .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
 634};
 635
 636int brcmf_net_attach(struct brcmf_if *ifp, bool rtnl_locked)
 637{
 638        struct brcmf_pub *drvr = ifp->drvr;
 639        struct net_device *ndev;
 640        s32 err;
 641
 642        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
 643                  ifp->mac_addr);
 644        ndev = ifp->ndev;
 645
 646        /* set appropriate operations */
 647        ndev->netdev_ops = &brcmf_netdev_ops_pri;
 648
 649        ndev->needed_headroom += drvr->hdrlen;
 650        ndev->ethtool_ops = &brcmf_ethtool_ops;
 651
 652        /* set the mac address & netns */
 653        memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
 654        dev_net_set(ndev, wiphy_net(cfg_to_wiphy(drvr->config)));
 655
 656        INIT_WORK(&ifp->multicast_work, _brcmf_set_multicast_list);
 657        INIT_WORK(&ifp->ndoffload_work, _brcmf_update_ndtable);
 658
 659        if (rtnl_locked)
 660                err = register_netdevice(ndev);
 661        else
 662                err = register_netdev(ndev);
 663        if (err != 0) {
 664                bphy_err(drvr, "couldn't register the net device\n");
 665                goto fail;
 666        }
 667
 668        netif_carrier_off(ndev);
 669
 670        ndev->priv_destructor = brcmf_cfg80211_free_netdev;
 671        brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
 672        return 0;
 673
 674fail:
 675        drvr->iflist[ifp->bsscfgidx] = NULL;
 676        ndev->netdev_ops = NULL;
 677        return -EBADE;
 678}
 679
 680void brcmf_net_detach(struct net_device *ndev, bool rtnl_locked)
 681{
 682        if (ndev->reg_state == NETREG_REGISTERED) {
 683                if (rtnl_locked)
 684                        unregister_netdevice(ndev);
 685                else
 686                        unregister_netdev(ndev);
 687        } else {
 688                brcmf_cfg80211_free_netdev(ndev);
 689                free_netdev(ndev);
 690        }
 691}
 692
 693static int brcmf_net_mon_open(struct net_device *ndev)
 694{
 695        struct brcmf_if *ifp = netdev_priv(ndev);
 696        struct brcmf_pub *drvr = ifp->drvr;
 697        u32 monitor;
 698        int err;
 699
 700        brcmf_dbg(TRACE, "Enter\n");
 701
 702        err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_MONITOR, &monitor);
 703        if (err) {
 704                bphy_err(drvr, "BRCMF_C_GET_MONITOR error (%d)\n", err);
 705                return err;
 706        } else if (monitor) {
 707                bphy_err(drvr, "Monitor mode is already enabled\n");
 708                return -EEXIST;
 709        }
 710
 711        monitor = 3;
 712        err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_MONITOR, monitor);
 713        if (err)
 714                bphy_err(drvr, "BRCMF_C_SET_MONITOR error (%d)\n", err);
 715
 716        return err;
 717}
 718
 719static int brcmf_net_mon_stop(struct net_device *ndev)
 720{
 721        struct brcmf_if *ifp = netdev_priv(ndev);
 722        struct brcmf_pub *drvr = ifp->drvr;
 723        u32 monitor;
 724        int err;
 725
 726        brcmf_dbg(TRACE, "Enter\n");
 727
 728        monitor = 0;
 729        err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_MONITOR, monitor);
 730        if (err)
 731                bphy_err(drvr, "BRCMF_C_SET_MONITOR error (%d)\n", err);
 732
 733        return err;
 734}
 735
 736static netdev_tx_t brcmf_net_mon_start_xmit(struct sk_buff *skb,
 737                                            struct net_device *ndev)
 738{
 739        dev_kfree_skb_any(skb);
 740
 741        return NETDEV_TX_OK;
 742}
 743
 744static const struct net_device_ops brcmf_netdev_ops_mon = {
 745        .ndo_open = brcmf_net_mon_open,
 746        .ndo_stop = brcmf_net_mon_stop,
 747        .ndo_start_xmit = brcmf_net_mon_start_xmit,
 748};
 749
 750int brcmf_net_mon_attach(struct brcmf_if *ifp)
 751{
 752        struct brcmf_pub *drvr = ifp->drvr;
 753        struct net_device *ndev;
 754        int err;
 755
 756        brcmf_dbg(TRACE, "Enter\n");
 757
 758        ndev = ifp->ndev;
 759        ndev->netdev_ops = &brcmf_netdev_ops_mon;
 760
 761        err = register_netdevice(ndev);
 762        if (err)
 763                bphy_err(drvr, "Failed to register %s device\n", ndev->name);
 764
 765        return err;
 766}
 767
 768void brcmf_net_setcarrier(struct brcmf_if *ifp, bool on)
 769{
 770        struct net_device *ndev;
 771
 772        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d carrier=%d\n", ifp->bsscfgidx,
 773                  on);
 774
 775        ndev = ifp->ndev;
 776        brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_DISCONNECTED, !on);
 777        if (on) {
 778                if (!netif_carrier_ok(ndev))
 779                        netif_carrier_on(ndev);
 780
 781        } else {
 782                if (netif_carrier_ok(ndev))
 783                        netif_carrier_off(ndev);
 784        }
 785}
 786
 787static int brcmf_net_p2p_open(struct net_device *ndev)
 788{
 789        brcmf_dbg(TRACE, "Enter\n");
 790
 791        return brcmf_cfg80211_up(ndev);
 792}
 793
 794static int brcmf_net_p2p_stop(struct net_device *ndev)
 795{
 796        brcmf_dbg(TRACE, "Enter\n");
 797
 798        return brcmf_cfg80211_down(ndev);
 799}
 800
 801static netdev_tx_t brcmf_net_p2p_start_xmit(struct sk_buff *skb,
 802                                            struct net_device *ndev)
 803{
 804        if (skb)
 805                dev_kfree_skb_any(skb);
 806
 807        return NETDEV_TX_OK;
 808}
 809
 810static const struct net_device_ops brcmf_netdev_ops_p2p = {
 811        .ndo_open = brcmf_net_p2p_open,
 812        .ndo_stop = brcmf_net_p2p_stop,
 813        .ndo_start_xmit = brcmf_net_p2p_start_xmit
 814};
 815
 816static int brcmf_net_p2p_attach(struct brcmf_if *ifp)
 817{
 818        struct brcmf_pub *drvr = ifp->drvr;
 819        struct net_device *ndev;
 820
 821        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
 822                  ifp->mac_addr);
 823        ndev = ifp->ndev;
 824
 825        ndev->netdev_ops = &brcmf_netdev_ops_p2p;
 826
 827        /* set the mac address */
 828        memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
 829
 830        if (register_netdev(ndev) != 0) {
 831                bphy_err(drvr, "couldn't register the p2p net device\n");
 832                goto fail;
 833        }
 834
 835        brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
 836
 837        return 0;
 838
 839fail:
 840        ifp->drvr->iflist[ifp->bsscfgidx] = NULL;
 841        ndev->netdev_ops = NULL;
 842        return -EBADE;
 843}
 844
 845struct brcmf_if *brcmf_add_if(struct brcmf_pub *drvr, s32 bsscfgidx, s32 ifidx,
 846                              bool is_p2pdev, const char *name, u8 *mac_addr)
 847{
 848        struct brcmf_if *ifp;
 849        struct net_device *ndev;
 850
 851        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx, ifidx);
 852
 853        ifp = drvr->iflist[bsscfgidx];
 854        /*
 855         * Delete the existing interface before overwriting it
 856         * in case we missed the BRCMF_E_IF_DEL event.
 857         */
 858        if (ifp) {
 859                if (ifidx) {
 860                        bphy_err(drvr, "ERROR: netdev:%s already exists\n",
 861                                 ifp->ndev->name);
 862                        netif_stop_queue(ifp->ndev);
 863                        brcmf_net_detach(ifp->ndev, false);
 864                        drvr->iflist[bsscfgidx] = NULL;
 865                } else {
 866                        brcmf_dbg(INFO, "netdev:%s ignore IF event\n",
 867                                  ifp->ndev->name);
 868                        return ERR_PTR(-EINVAL);
 869                }
 870        }
 871
 872        if (!drvr->settings->p2p_enable && is_p2pdev) {
 873                /* this is P2P_DEVICE interface */
 874                brcmf_dbg(INFO, "allocate non-netdev interface\n");
 875                ifp = kzalloc(sizeof(*ifp), GFP_KERNEL);
 876                if (!ifp)
 877                        return ERR_PTR(-ENOMEM);
 878        } else {
 879                brcmf_dbg(INFO, "allocate netdev interface\n");
 880                /* Allocate netdev, including space for private structure */
 881                ndev = alloc_netdev(sizeof(*ifp), is_p2pdev ? "p2p%d" : name,
 882                                    NET_NAME_UNKNOWN, ether_setup);
 883                if (!ndev)
 884                        return ERR_PTR(-ENOMEM);
 885
 886                ndev->needs_free_netdev = true;
 887                ifp = netdev_priv(ndev);
 888                ifp->ndev = ndev;
 889                /* store mapping ifidx to bsscfgidx */
 890                if (drvr->if2bss[ifidx] == BRCMF_BSSIDX_INVALID)
 891                        drvr->if2bss[ifidx] = bsscfgidx;
 892        }
 893
 894        ifp->drvr = drvr;
 895        drvr->iflist[bsscfgidx] = ifp;
 896        ifp->ifidx = ifidx;
 897        ifp->bsscfgidx = bsscfgidx;
 898
 899        init_waitqueue_head(&ifp->pend_8021x_wait);
 900        spin_lock_init(&ifp->netif_stop_lock);
 901
 902        if (mac_addr != NULL)
 903                memcpy(ifp->mac_addr, mac_addr, ETH_ALEN);
 904
 905        brcmf_dbg(TRACE, " ==== pid:%x, if:%s (%pM) created ===\n",
 906                  current->pid, name, ifp->mac_addr);
 907
 908        return ifp;
 909}
 910
 911static void brcmf_del_if(struct brcmf_pub *drvr, s32 bsscfgidx,
 912                         bool rtnl_locked)
 913{
 914        struct brcmf_if *ifp;
 915        int ifidx;
 916
 917        ifp = drvr->iflist[bsscfgidx];
 918        if (!ifp) {
 919                bphy_err(drvr, "Null interface, bsscfgidx=%d\n", bsscfgidx);
 920                return;
 921        }
 922        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx,
 923                  ifp->ifidx);
 924        ifidx = ifp->ifidx;
 925
 926        if (ifp->ndev) {
 927                if (bsscfgidx == 0) {
 928                        if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
 929                                rtnl_lock();
 930                                brcmf_netdev_stop(ifp->ndev);
 931                                rtnl_unlock();
 932                        }
 933                } else {
 934                        netif_stop_queue(ifp->ndev);
 935                }
 936
 937                if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
 938                        cancel_work_sync(&ifp->multicast_work);
 939                        cancel_work_sync(&ifp->ndoffload_work);
 940                }
 941                brcmf_net_detach(ifp->ndev, rtnl_locked);
 942        } else {
 943                /* Only p2p device interfaces which get dynamically created
 944                 * end up here. In this case the p2p module should be informed
 945                 * about the removal of the interface within the firmware. If
 946                 * not then p2p commands towards the firmware will cause some
 947                 * serious troublesome side effects. The p2p module will clean
 948                 * up the ifp if needed.
 949                 */
 950                brcmf_p2p_ifp_removed(ifp, rtnl_locked);
 951                kfree(ifp);
 952        }
 953
 954        drvr->iflist[bsscfgidx] = NULL;
 955        if (drvr->if2bss[ifidx] == bsscfgidx)
 956                drvr->if2bss[ifidx] = BRCMF_BSSIDX_INVALID;
 957}
 958
 959void brcmf_remove_interface(struct brcmf_if *ifp, bool rtnl_locked)
 960{
 961        if (!ifp || WARN_ON(ifp->drvr->iflist[ifp->bsscfgidx] != ifp))
 962                return;
 963        brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp->bsscfgidx,
 964                  ifp->ifidx);
 965        brcmf_proto_del_if(ifp->drvr, ifp);
 966        brcmf_del_if(ifp->drvr, ifp->bsscfgidx, rtnl_locked);
 967}
 968
 969static int brcmf_psm_watchdog_notify(struct brcmf_if *ifp,
 970                                     const struct brcmf_event_msg *evtmsg,
 971                                     void *data)
 972{
 973        struct brcmf_pub *drvr = ifp->drvr;
 974        int err;
 975
 976        brcmf_dbg(TRACE, "enter: bsscfgidx=%d\n", ifp->bsscfgidx);
 977
 978        bphy_err(drvr, "PSM's watchdog has fired!\n");
 979
 980        err = brcmf_debug_create_memdump(ifp->drvr->bus_if, data,
 981                                         evtmsg->datalen);
 982        if (err)
 983                bphy_err(drvr, "Failed to get memory dump, %d\n", err);
 984
 985        return err;
 986}
 987
 988#ifdef CONFIG_INET
 989#define ARPOL_MAX_ENTRIES       8
 990static int brcmf_inetaddr_changed(struct notifier_block *nb,
 991                                  unsigned long action, void *data)
 992{
 993        struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
 994                                              inetaddr_notifier);
 995        struct in_ifaddr *ifa = data;
 996        struct net_device *ndev = ifa->ifa_dev->dev;
 997        struct brcmf_if *ifp;
 998        int idx, i, ret;
 999        u32 val;
1000        __be32 addr_table[ARPOL_MAX_ENTRIES] = {0};
1001
1002        /* Find out if the notification is meant for us */
1003        for (idx = 0; idx < BRCMF_MAX_IFS; idx++) {
1004                ifp = drvr->iflist[idx];
1005                if (ifp && ifp->ndev == ndev)
1006                        break;
1007                if (idx == BRCMF_MAX_IFS - 1)
1008                        return NOTIFY_DONE;
1009        }
1010
1011        /* check if arp offload is supported */
1012        ret = brcmf_fil_iovar_int_get(ifp, "arpoe", &val);
1013        if (ret)
1014                return NOTIFY_OK;
1015
1016        /* old version only support primary index */
1017        ret = brcmf_fil_iovar_int_get(ifp, "arp_version", &val);
1018        if (ret)
1019                val = 1;
1020        if (val == 1)
1021                ifp = drvr->iflist[0];
1022
1023        /* retrieve the table from firmware */
1024        ret = brcmf_fil_iovar_data_get(ifp, "arp_hostip", addr_table,
1025                                       sizeof(addr_table));
1026        if (ret) {
1027                bphy_err(drvr, "fail to get arp ip table err:%d\n", ret);
1028                return NOTIFY_OK;
1029        }
1030
1031        for (i = 0; i < ARPOL_MAX_ENTRIES; i++)
1032                if (ifa->ifa_address == addr_table[i])
1033                        break;
1034
1035        switch (action) {
1036        case NETDEV_UP:
1037                if (i == ARPOL_MAX_ENTRIES) {
1038                        brcmf_dbg(TRACE, "add %pI4 to arp table\n",
1039                                  &ifa->ifa_address);
1040                        /* set it directly */
1041                        ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
1042                                &ifa->ifa_address, sizeof(ifa->ifa_address));
1043                        if (ret)
1044                                bphy_err(drvr, "add arp ip err %d\n", ret);
1045                }
1046                break;
1047        case NETDEV_DOWN:
1048                if (i < ARPOL_MAX_ENTRIES) {
1049                        addr_table[i] = 0;
1050                        brcmf_dbg(TRACE, "remove %pI4 from arp table\n",
1051                                  &ifa->ifa_address);
1052                        /* clear the table in firmware */
1053                        ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear",
1054                                                       NULL, 0);
1055                        if (ret) {
1056                                bphy_err(drvr, "fail to clear arp ip table err:%d\n",
1057                                         ret);
1058                                return NOTIFY_OK;
1059                        }
1060                        for (i = 0; i < ARPOL_MAX_ENTRIES; i++) {
1061                                if (addr_table[i] == 0)
1062                                        continue;
1063                                ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
1064                                                               &addr_table[i],
1065                                                               sizeof(addr_table[i]));
1066                                if (ret)
1067                                        bphy_err(drvr, "add arp ip err %d\n",
1068                                                 ret);
1069                        }
1070                }
1071                break;
1072        default:
1073                break;
1074        }
1075
1076        return NOTIFY_OK;
1077}
1078#endif
1079
1080#if IS_ENABLED(CONFIG_IPV6)
1081static int brcmf_inet6addr_changed(struct notifier_block *nb,
1082                                   unsigned long action, void *data)
1083{
1084        struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
1085                                              inet6addr_notifier);
1086        struct inet6_ifaddr *ifa = data;
1087        struct brcmf_if *ifp;
1088        int i;
1089        struct in6_addr *table;
1090
1091        /* Only handle primary interface */
1092        ifp = drvr->iflist[0];
1093        if (!ifp)
1094                return NOTIFY_DONE;
1095        if (ifp->ndev != ifa->idev->dev)
1096                return NOTIFY_DONE;
1097
1098        table = ifp->ipv6_addr_tbl;
1099        for (i = 0; i < NDOL_MAX_ENTRIES; i++)
1100                if (ipv6_addr_equal(&ifa->addr, &table[i]))
1101                        break;
1102
1103        switch (action) {
1104        case NETDEV_UP:
1105                if (i == NDOL_MAX_ENTRIES) {
1106                        if (ifp->ipv6addr_idx < NDOL_MAX_ENTRIES) {
1107                                table[ifp->ipv6addr_idx++] = ifa->addr;
1108                        } else {
1109                                for (i = 0; i < NDOL_MAX_ENTRIES - 1; i++)
1110                                        table[i] = table[i + 1];
1111                                table[NDOL_MAX_ENTRIES - 1] = ifa->addr;
1112                        }
1113                }
1114                break;
1115        case NETDEV_DOWN:
1116                if (i < NDOL_MAX_ENTRIES) {
1117                        for (; i < ifp->ipv6addr_idx - 1; i++)
1118                                table[i] = table[i + 1];
1119                        memset(&table[i], 0, sizeof(table[i]));
1120                        ifp->ipv6addr_idx--;
1121                }
1122                break;
1123        default:
1124                break;
1125        }
1126
1127        schedule_work(&ifp->ndoffload_work);
1128
1129        return NOTIFY_OK;
1130}
1131#endif
1132
1133static int brcmf_revinfo_read(struct seq_file *s, void *data)
1134{
1135        struct brcmf_bus *bus_if = dev_get_drvdata(s->private);
1136        struct brcmf_rev_info *ri = &bus_if->drvr->revinfo;
1137        char drev[BRCMU_DOTREV_LEN];
1138        char brev[BRCMU_BOARDREV_LEN];
1139
1140        seq_printf(s, "vendorid: 0x%04x\n", ri->vendorid);
1141        seq_printf(s, "deviceid: 0x%04x\n", ri->deviceid);
1142        seq_printf(s, "radiorev: %s\n", brcmu_dotrev_str(ri->radiorev, drev));
1143        seq_printf(s, "chip: %s\n", ri->chipname);
1144        seq_printf(s, "chippkg: %u\n", ri->chippkg);
1145        seq_printf(s, "corerev: %u\n", ri->corerev);
1146        seq_printf(s, "boardid: 0x%04x\n", ri->boardid);
1147        seq_printf(s, "boardvendor: 0x%04x\n", ri->boardvendor);
1148        seq_printf(s, "boardrev: %s\n", brcmu_boardrev_str(ri->boardrev, brev));
1149        seq_printf(s, "driverrev: %s\n", brcmu_dotrev_str(ri->driverrev, drev));
1150        seq_printf(s, "ucoderev: %u\n", ri->ucoderev);
1151        seq_printf(s, "bus: %u\n", ri->bus);
1152        seq_printf(s, "phytype: %u\n", ri->phytype);
1153        seq_printf(s, "phyrev: %u\n", ri->phyrev);
1154        seq_printf(s, "anarev: %u\n", ri->anarev);
1155        seq_printf(s, "nvramrev: %08x\n", ri->nvramrev);
1156
1157        seq_printf(s, "clmver: %s\n", bus_if->drvr->clmver);
1158
1159        return 0;
1160}
1161
1162static void brcmf_core_bus_reset(struct work_struct *work)
1163{
1164        struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
1165                                              bus_reset);
1166
1167        brcmf_bus_reset(drvr->bus_if);
1168}
1169
1170static ssize_t bus_reset_write(struct file *file, const char __user *user_buf,
1171                               size_t count, loff_t *ppos)
1172{
1173        struct brcmf_pub *drvr = file->private_data;
1174        u8 value;
1175
1176        if (kstrtou8_from_user(user_buf, count, 0, &value))
1177                return -EINVAL;
1178
1179        if (value != 1)
1180                return -EINVAL;
1181
1182        schedule_work(&drvr->bus_reset);
1183
1184        return count;
1185}
1186
1187static const struct file_operations bus_reset_fops = {
1188        .open   = simple_open,
1189        .llseek = no_llseek,
1190        .write  = bus_reset_write,
1191};
1192
1193static int brcmf_bus_started(struct brcmf_pub *drvr, struct cfg80211_ops *ops)
1194{
1195        int ret = -1;
1196        struct brcmf_bus *bus_if = drvr->bus_if;
1197        struct brcmf_if *ifp;
1198        struct brcmf_if *p2p_ifp;
1199
1200        brcmf_dbg(TRACE, "\n");
1201
1202        /* add primary networking interface */
1203        ifp = brcmf_add_if(drvr, 0, 0, false, "wlan%d", NULL);
1204        if (IS_ERR(ifp))
1205                return PTR_ERR(ifp);
1206
1207        p2p_ifp = NULL;
1208
1209        /* signal bus ready */
1210        brcmf_bus_change_state(bus_if, BRCMF_BUS_UP);
1211
1212        /* do bus specific preinit here */
1213        ret = brcmf_bus_preinit(bus_if);
1214        if (ret < 0)
1215                goto fail;
1216
1217        /* Bus is ready, do any initialization */
1218        ret = brcmf_c_preinit_dcmds(ifp);
1219        if (ret < 0)
1220                goto fail;
1221
1222        brcmf_feat_attach(drvr);
1223
1224        ret = brcmf_proto_init_done(drvr);
1225        if (ret < 0)
1226                goto fail;
1227
1228        brcmf_proto_add_if(drvr, ifp);
1229
1230        drvr->config = brcmf_cfg80211_attach(drvr, ops,
1231                                             drvr->settings->p2p_enable);
1232        if (drvr->config == NULL) {
1233                ret = -ENOMEM;
1234                goto fail;
1235        }
1236
1237        ret = brcmf_net_attach(ifp, false);
1238
1239        if ((!ret) && (drvr->settings->p2p_enable)) {
1240                p2p_ifp = drvr->iflist[1];
1241                if (p2p_ifp)
1242                        ret = brcmf_net_p2p_attach(p2p_ifp);
1243        }
1244
1245        if (ret)
1246                goto fail;
1247
1248#ifdef CONFIG_INET
1249        drvr->inetaddr_notifier.notifier_call = brcmf_inetaddr_changed;
1250        ret = register_inetaddr_notifier(&drvr->inetaddr_notifier);
1251        if (ret)
1252                goto fail;
1253
1254#if IS_ENABLED(CONFIG_IPV6)
1255        drvr->inet6addr_notifier.notifier_call = brcmf_inet6addr_changed;
1256        ret = register_inet6addr_notifier(&drvr->inet6addr_notifier);
1257        if (ret) {
1258                unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1259                goto fail;
1260        }
1261#endif
1262#endif /* CONFIG_INET */
1263
1264        INIT_WORK(&drvr->bus_reset, brcmf_core_bus_reset);
1265
1266        /* populate debugfs */
1267        brcmf_debugfs_add_entry(drvr, "revinfo", brcmf_revinfo_read);
1268        debugfs_create_file("reset", 0600, brcmf_debugfs_get_devdir(drvr), drvr,
1269                            &bus_reset_fops);
1270        brcmf_feat_debugfs_create(drvr);
1271        brcmf_proto_debugfs_create(drvr);
1272        brcmf_bus_debugfs_create(bus_if);
1273
1274        return 0;
1275
1276fail:
1277        bphy_err(drvr, "failed: %d\n", ret);
1278        if (drvr->config) {
1279                brcmf_cfg80211_detach(drvr->config);
1280                drvr->config = NULL;
1281        }
1282        brcmf_net_detach(ifp->ndev, false);
1283        if (p2p_ifp)
1284                brcmf_net_detach(p2p_ifp->ndev, false);
1285        drvr->iflist[0] = NULL;
1286        drvr->iflist[1] = NULL;
1287        if (drvr->settings->ignore_probe_fail)
1288                ret = 0;
1289
1290        return ret;
1291}
1292
1293int brcmf_alloc(struct device *dev, struct brcmf_mp_device *settings)
1294{
1295        struct wiphy *wiphy;
1296        struct cfg80211_ops *ops;
1297        struct brcmf_pub *drvr = NULL;
1298
1299        brcmf_dbg(TRACE, "Enter\n");
1300
1301        ops = brcmf_cfg80211_get_ops(settings);
1302        if (!ops)
1303                return -ENOMEM;
1304
1305        wiphy = wiphy_new(ops, sizeof(*drvr));
1306        if (!wiphy) {
1307                kfree(ops);
1308                return -ENOMEM;
1309        }
1310
1311        set_wiphy_dev(wiphy, dev);
1312        drvr = wiphy_priv(wiphy);
1313        drvr->wiphy = wiphy;
1314        drvr->ops = ops;
1315        drvr->bus_if = dev_get_drvdata(dev);
1316        drvr->bus_if->drvr = drvr;
1317        drvr->settings = settings;
1318
1319        return 0;
1320}
1321
1322int brcmf_attach(struct device *dev)
1323{
1324        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1325        struct brcmf_pub *drvr = bus_if->drvr;
1326        int ret = 0;
1327        int i;
1328
1329        brcmf_dbg(TRACE, "Enter\n");
1330
1331        for (i = 0; i < ARRAY_SIZE(drvr->if2bss); i++)
1332                drvr->if2bss[i] = BRCMF_BSSIDX_INVALID;
1333
1334        mutex_init(&drvr->proto_block);
1335
1336        /* Link to bus module */
1337        drvr->hdrlen = 0;
1338
1339        /* Attach and link in the protocol */
1340        ret = brcmf_proto_attach(drvr);
1341        if (ret != 0) {
1342                bphy_err(drvr, "brcmf_prot_attach failed\n");
1343                goto fail;
1344        }
1345
1346        /* Attach to events important for core code */
1347        brcmf_fweh_register(drvr, BRCMF_E_PSM_WATCHDOG,
1348                            brcmf_psm_watchdog_notify);
1349
1350        /* attach firmware event handler */
1351        brcmf_fweh_attach(drvr);
1352
1353        ret = brcmf_bus_started(drvr, drvr->ops);
1354        if (ret != 0) {
1355                bphy_err(drvr, "dongle is not responding: err=%d\n", ret);
1356                goto fail;
1357        }
1358
1359        return 0;
1360
1361fail:
1362        brcmf_detach(dev);
1363
1364        return ret;
1365}
1366
1367void brcmf_bus_add_txhdrlen(struct device *dev, uint len)
1368{
1369        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1370        struct brcmf_pub *drvr = bus_if->drvr;
1371
1372        if (drvr) {
1373                drvr->hdrlen += len;
1374        }
1375}
1376
1377void brcmf_dev_reset(struct device *dev)
1378{
1379        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1380        struct brcmf_pub *drvr = bus_if->drvr;
1381
1382        if (drvr == NULL)
1383                return;
1384
1385        if (drvr->iflist[0])
1386                brcmf_fil_cmd_int_set(drvr->iflist[0], BRCMF_C_TERMINATED, 1);
1387}
1388
1389void brcmf_dev_coredump(struct device *dev)
1390{
1391        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1392
1393        if (brcmf_debug_create_memdump(bus_if, NULL, 0) < 0)
1394                brcmf_dbg(TRACE, "failed to create coredump\n");
1395}
1396
1397void brcmf_fw_crashed(struct device *dev)
1398{
1399        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1400        struct brcmf_pub *drvr = bus_if->drvr;
1401
1402        bphy_err(drvr, "Firmware has halted or crashed\n");
1403
1404        brcmf_dev_coredump(dev);
1405
1406        schedule_work(&drvr->bus_reset);
1407}
1408
1409void brcmf_detach(struct device *dev)
1410{
1411        s32 i;
1412        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1413        struct brcmf_pub *drvr = bus_if->drvr;
1414
1415        brcmf_dbg(TRACE, "Enter\n");
1416
1417        if (drvr == NULL)
1418                return;
1419
1420#ifdef CONFIG_INET
1421        unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1422#endif
1423
1424#if IS_ENABLED(CONFIG_IPV6)
1425        unregister_inet6addr_notifier(&drvr->inet6addr_notifier);
1426#endif
1427
1428        brcmf_bus_change_state(bus_if, BRCMF_BUS_DOWN);
1429        /* make sure primary interface removed last */
1430        for (i = BRCMF_MAX_IFS - 1; i > -1; i--) {
1431                if (drvr->iflist[i])
1432                        brcmf_remove_interface(drvr->iflist[i], false);
1433        }
1434        brcmf_bus_stop(drvr->bus_if);
1435
1436        brcmf_fweh_detach(drvr);
1437        brcmf_proto_detach(drvr);
1438
1439        if (drvr->mon_if) {
1440                brcmf_net_detach(drvr->mon_if->ndev, false);
1441                drvr->mon_if = NULL;
1442        }
1443
1444        if (drvr->config) {
1445                brcmf_p2p_detach(&drvr->config->p2p);
1446                brcmf_cfg80211_detach(drvr->config);
1447                drvr->config = NULL;
1448        }
1449}
1450
1451void brcmf_free(struct device *dev)
1452{
1453        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1454        struct brcmf_pub *drvr = bus_if->drvr;
1455
1456        if (!drvr)
1457                return;
1458
1459        bus_if->drvr = NULL;
1460
1461        kfree(drvr->ops);
1462
1463        wiphy_free(drvr->wiphy);
1464}
1465
1466s32 brcmf_iovar_data_set(struct device *dev, char *name, void *data, u32 len)
1467{
1468        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1469        struct brcmf_if *ifp = bus_if->drvr->iflist[0];
1470
1471        return brcmf_fil_iovar_data_set(ifp, name, data, len);
1472}
1473
1474static int brcmf_get_pend_8021x_cnt(struct brcmf_if *ifp)
1475{
1476        return atomic_read(&ifp->pend_8021x_cnt);
1477}
1478
1479int brcmf_netdev_wait_pend8021x(struct brcmf_if *ifp)
1480{
1481        struct brcmf_pub *drvr = ifp->drvr;
1482        int err;
1483
1484        err = wait_event_timeout(ifp->pend_8021x_wait,
1485                                 !brcmf_get_pend_8021x_cnt(ifp),
1486                                 MAX_WAIT_FOR_8021X_TX);
1487
1488        if (!err)
1489                bphy_err(drvr, "Timed out waiting for no pending 802.1x packets\n");
1490
1491        return !err;
1492}
1493
1494void brcmf_bus_change_state(struct brcmf_bus *bus, enum brcmf_bus_state state)
1495{
1496        struct brcmf_pub *drvr = bus->drvr;
1497        struct net_device *ndev;
1498        int ifidx;
1499
1500        brcmf_dbg(TRACE, "%d -> %d\n", bus->state, state);
1501
1502        if (!drvr) {
1503                brcmf_dbg(INFO, "ignoring transition, bus not attached yet\n");
1504                return;
1505        }
1506
1507        bus->state = state;
1508
1509        if (state == BRCMF_BUS_UP) {
1510                for (ifidx = 0; ifidx < BRCMF_MAX_IFS; ifidx++) {
1511                        if ((drvr->iflist[ifidx]) &&
1512                            (drvr->iflist[ifidx]->ndev)) {
1513                                ndev = drvr->iflist[ifidx]->ndev;
1514                                if (netif_queue_stopped(ndev))
1515                                        netif_wake_queue(ndev);
1516                        }
1517                }
1518        }
1519}
1520
1521static void brcmf_driver_register(struct work_struct *work)
1522{
1523#ifdef CONFIG_BRCMFMAC_SDIO
1524        brcmf_sdio_register();
1525#endif
1526#ifdef CONFIG_BRCMFMAC_USB
1527        brcmf_usb_register();
1528#endif
1529#ifdef CONFIG_BRCMFMAC_PCIE
1530        brcmf_pcie_register();
1531#endif
1532}
1533static DECLARE_WORK(brcmf_driver_work, brcmf_driver_register);
1534
1535int __init brcmf_core_init(void)
1536{
1537        if (!schedule_work(&brcmf_driver_work))
1538                return -EBUSY;
1539
1540        return 0;
1541}
1542
1543void __exit brcmf_core_exit(void)
1544{
1545        cancel_work_sync(&brcmf_driver_work);
1546
1547#ifdef CONFIG_BRCMFMAC_SDIO
1548        brcmf_sdio_exit();
1549#endif
1550#ifdef CONFIG_BRCMFMAC_USB
1551        brcmf_usb_exit();
1552#endif
1553#ifdef CONFIG_BRCMFMAC_PCIE
1554        brcmf_pcie_exit();
1555#endif
1556}
1557
1558