2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/kernel.h>
18 #include <linux/etherdevice.h>
19 #include <linux/module.h>
20 #include <linux/inetdevice.h>
21 #include <net/cfg80211.h>
22 #include <net/rtnetlink.h>
23 #include <net/addrconf.h>
25 #include <brcmu_utils.h>
26 #include <brcmu_wifi.h>
31 #include "fwil_types.h"
41 #define MAX_WAIT_FOR_8021X_TX msecs_to_jiffies(950)
43 #define BRCMF_BSSIDX_INVALID -1
45 char *brcmf_ifname(struct brcmf_if *ifp)
51 return ifp->ndev->name;
56 struct brcmf_if *brcmf_get_ifp(struct brcmf_pub *drvr, int ifidx)
61 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
62 brcmf_err("ifidx %d out of range\n", ifidx);
67 bsscfgidx = drvr->if2bss[ifidx];
69 ifp = drvr->iflist[bsscfgidx];
74 static void _brcmf_set_multicast_list(struct work_struct *work)
77 struct net_device *ndev;
78 struct netdev_hw_addr *ha;
85 ifp = container_of(work, struct brcmf_if, multicast_work);
87 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
91 /* Determine initial value of allmulti flag */
92 cmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
94 /* Send down the multicast list first. */
95 cnt = netdev_mc_count(ndev);
96 buflen = sizeof(cnt) + (cnt * ETH_ALEN);
97 buf = kmalloc(buflen, GFP_ATOMIC);
102 cnt_le = cpu_to_le32(cnt);
103 memcpy(bufp, &cnt_le, sizeof(cnt_le));
104 bufp += sizeof(cnt_le);
106 netdev_for_each_mc_addr(ha, ndev) {
109 memcpy(bufp, ha->addr, ETH_ALEN);
114 err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
116 brcmf_err("Setting mcast_list failed, %d\n", err);
117 cmd_value = cnt ? true : cmd_value;
123 * Now send the allmulti setting. This is based on the setting in the
124 * net_device flags, but might be modified above to be turned on if we
125 * were trying to set some addresses and dongle rejected it...
127 err = brcmf_fil_iovar_int_set(ifp, "allmulti", cmd_value);
129 brcmf_err("Setting allmulti failed, %d\n", err);
131 /*Finally, pick up the PROMISC flag */
132 cmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
133 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PROMISC, cmd_value);
135 brcmf_err("Setting BRCMF_C_SET_PROMISC failed, %d\n",
140 _brcmf_set_mac_address(struct work_struct *work)
142 struct brcmf_if *ifp;
145 ifp = container_of(work, struct brcmf_if, setmacaddr_work);
147 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
149 err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", ifp->mac_addr,
152 brcmf_err("Setting cur_etheraddr failed, %d\n", err);
154 brcmf_dbg(TRACE, "MAC address updated to %pM\n",
156 memcpy(ifp->ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
160 #if IS_ENABLED(CONFIG_IPV6)
161 static void _brcmf_update_ndtable(struct work_struct *work)
163 struct brcmf_if *ifp;
166 ifp = container_of(work, struct brcmf_if, ndoffload_work);
168 /* clear the table in firmware */
169 ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip_clear", NULL, 0);
171 brcmf_dbg(TRACE, "fail to clear nd ip table err:%d\n", ret);
175 for (i = 0; i < ifp->ipv6addr_idx; i++) {
176 ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip",
177 &ifp->ipv6_addr_tbl[i],
178 sizeof(struct in6_addr));
180 brcmf_err("add nd ip err %d\n", ret);
184 static void _brcmf_update_ndtable(struct work_struct *work)
189 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
191 struct brcmf_if *ifp = netdev_priv(ndev);
192 struct sockaddr *sa = (struct sockaddr *)addr;
194 memcpy(&ifp->mac_addr, sa->sa_data, ETH_ALEN);
195 schedule_work(&ifp->setmacaddr_work);
199 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
201 struct brcmf_if *ifp = netdev_priv(ndev);
203 schedule_work(&ifp->multicast_work);
206 static netdev_tx_t brcmf_netdev_start_xmit(struct sk_buff *skb,
207 struct net_device *ndev)
210 struct brcmf_if *ifp = netdev_priv(ndev);
211 struct brcmf_pub *drvr = ifp->drvr;
212 struct ethhdr *eh = (struct ethhdr *)(skb->data);
214 brcmf_dbg(DATA, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
216 /* Can the device send data? */
217 if (drvr->bus_if->state != BRCMF_BUS_UP) {
218 brcmf_err("xmit rejected state=%d\n", drvr->bus_if->state);
219 netif_stop_queue(ndev);
225 /* Make sure there's enough room for any header */
226 if (skb_headroom(skb) < drvr->hdrlen) {
227 struct sk_buff *skb2;
229 brcmf_dbg(INFO, "%s: insufficient headroom\n",
231 drvr->bus_if->tx_realloc++;
232 skb2 = skb_realloc_headroom(skb, drvr->hdrlen);
236 brcmf_err("%s: skb_realloc_headroom failed\n",
243 /* validate length for ether packet */
244 if (skb->len < sizeof(*eh)) {
250 if (eh->h_proto == htons(ETH_P_PAE))
251 atomic_inc(&ifp->pend_8021x_cnt);
253 ret = brcmf_fws_process_skb(ifp, skb);
257 ifp->stats.tx_dropped++;
259 ifp->stats.tx_packets++;
260 ifp->stats.tx_bytes += skb->len;
263 /* Return ok: we always eat the packet */
267 void brcmf_txflowblock_if(struct brcmf_if *ifp,
268 enum brcmf_netif_stop_reason reason, bool state)
272 if (!ifp || !ifp->ndev)
275 brcmf_dbg(TRACE, "enter: bsscfgidx=%d stop=0x%X reason=%d state=%d\n",
276 ifp->bsscfgidx, ifp->netif_stop, reason, state);
278 spin_lock_irqsave(&ifp->netif_stop_lock, flags);
280 if (!ifp->netif_stop)
281 netif_stop_queue(ifp->ndev);
282 ifp->netif_stop |= reason;
284 ifp->netif_stop &= ~reason;
285 if (!ifp->netif_stop)
286 netif_wake_queue(ifp->ndev);
288 spin_unlock_irqrestore(&ifp->netif_stop_lock, flags);
291 void brcmf_txflowblock(struct device *dev, bool state)
293 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
294 struct brcmf_pub *drvr = bus_if->drvr;
296 brcmf_dbg(TRACE, "Enter\n");
298 brcmf_fws_bus_blocked(drvr, state);
301 void brcmf_netif_rx(struct brcmf_if *ifp, struct sk_buff *skb)
303 if (skb->pkt_type == PACKET_MULTICAST)
304 ifp->stats.multicast++;
306 if (!(ifp->ndev->flags & IFF_UP)) {
307 brcmu_pkt_buf_free_skb(skb);
311 ifp->stats.rx_bytes += skb->len;
312 ifp->stats.rx_packets++;
314 brcmf_dbg(DATA, "rx proto=0x%X\n", ntohs(skb->protocol));
318 /* If the receive is not processed inside an ISR,
319 * the softirqd must be woken explicitly to service
320 * the NET_RX_SOFTIRQ. This is handled by netif_rx_ni().
325 static int brcmf_rx_hdrpull(struct brcmf_pub *drvr, struct sk_buff *skb,
326 struct brcmf_if **ifp)
330 /* process and remove protocol-specific header */
331 ret = brcmf_proto_hdrpull(drvr, true, skb, ifp);
333 if (ret || !(*ifp) || !(*ifp)->ndev) {
334 if (ret != -ENODATA && *ifp)
335 (*ifp)->stats.rx_errors++;
336 brcmu_pkt_buf_free_skb(skb);
340 skb->protocol = eth_type_trans(skb, (*ifp)->ndev);
344 void brcmf_rx_frame(struct device *dev, struct sk_buff *skb, bool handle_event)
346 struct brcmf_if *ifp;
347 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
348 struct brcmf_pub *drvr = bus_if->drvr;
350 brcmf_dbg(DATA, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
352 if (brcmf_rx_hdrpull(drvr, skb, &ifp))
355 if (brcmf_proto_is_reorder_skb(skb)) {
356 brcmf_proto_rxreorder(ifp, skb);
358 /* Process special event packets */
360 brcmf_fweh_process_skb(ifp->drvr, skb);
362 brcmf_netif_rx(ifp, skb);
366 void brcmf_rx_event(struct device *dev, struct sk_buff *skb)
368 struct brcmf_if *ifp;
369 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
370 struct brcmf_pub *drvr = bus_if->drvr;
372 brcmf_dbg(EVENT, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
374 if (brcmf_rx_hdrpull(drvr, skb, &ifp))
377 brcmf_fweh_process_skb(ifp->drvr, skb);
378 brcmu_pkt_buf_free_skb(skb);
381 void brcmf_txfinalize(struct brcmf_if *ifp, struct sk_buff *txp, bool success)
386 eh = (struct ethhdr *)(txp->data);
387 type = ntohs(eh->h_proto);
389 if (type == ETH_P_PAE) {
390 atomic_dec(&ifp->pend_8021x_cnt);
391 if (waitqueue_active(&ifp->pend_8021x_wait))
392 wake_up(&ifp->pend_8021x_wait);
396 ifp->stats.tx_errors++;
398 brcmu_pkt_buf_free_skb(txp);
401 void brcmf_txcomplete(struct device *dev, struct sk_buff *txp, bool success)
403 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
404 struct brcmf_pub *drvr = bus_if->drvr;
405 struct brcmf_if *ifp;
407 /* await txstatus signal for firmware if active */
408 if (brcmf_fws_fc_active(drvr->fws)) {
410 brcmf_fws_bustxfail(drvr->fws, txp);
412 if (brcmf_proto_hdrpull(drvr, false, txp, &ifp))
413 brcmu_pkt_buf_free_skb(txp);
415 brcmf_txfinalize(ifp, txp, success);
419 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
421 struct brcmf_if *ifp = netdev_priv(ndev);
423 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
428 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
429 struct ethtool_drvinfo *info)
431 struct brcmf_if *ifp = netdev_priv(ndev);
432 struct brcmf_pub *drvr = ifp->drvr;
433 char drev[BRCMU_DOTREV_LEN] = "n/a";
435 if (drvr->revinfo.result == 0)
436 brcmu_dotrev_str(drvr->revinfo.driverrev, drev);
437 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
438 strlcpy(info->version, drev, sizeof(info->version));
439 strlcpy(info->fw_version, drvr->fwver, sizeof(info->fw_version));
440 strlcpy(info->bus_info, dev_name(drvr->bus_if->dev),
441 sizeof(info->bus_info));
444 static const struct ethtool_ops brcmf_ethtool_ops = {
445 .get_drvinfo = brcmf_ethtool_get_drvinfo,
448 static int brcmf_netdev_stop(struct net_device *ndev)
450 struct brcmf_if *ifp = netdev_priv(ndev);
452 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
454 brcmf_cfg80211_down(ndev);
456 brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear", NULL, 0);
458 brcmf_net_setcarrier(ifp, false);
463 static int brcmf_netdev_open(struct net_device *ndev)
465 struct brcmf_if *ifp = netdev_priv(ndev);
466 struct brcmf_pub *drvr = ifp->drvr;
467 struct brcmf_bus *bus_if = drvr->bus_if;
470 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
472 /* If bus is not ready, can't continue */
473 if (bus_if->state != BRCMF_BUS_UP) {
474 brcmf_err("failed bus is not ready\n");
478 atomic_set(&ifp->pend_8021x_cnt, 0);
480 /* Get current TOE mode from dongle */
481 if (brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_ol) >= 0
482 && (toe_ol & TOE_TX_CSUM_OL) != 0)
483 ndev->features |= NETIF_F_IP_CSUM;
485 ndev->features &= ~NETIF_F_IP_CSUM;
487 if (brcmf_cfg80211_up(ndev)) {
488 brcmf_err("failed to bring up cfg80211\n");
492 /* Clear, carrier, set when connected or AP mode. */
493 netif_carrier_off(ndev);
497 static const struct net_device_ops brcmf_netdev_ops_pri = {
498 .ndo_open = brcmf_netdev_open,
499 .ndo_stop = brcmf_netdev_stop,
500 .ndo_get_stats = brcmf_netdev_get_stats,
501 .ndo_start_xmit = brcmf_netdev_start_xmit,
502 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
503 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
506 int brcmf_net_attach(struct brcmf_if *ifp, bool rtnl_locked)
508 struct brcmf_pub *drvr = ifp->drvr;
509 struct net_device *ndev;
512 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
516 /* set appropriate operations */
517 ndev->netdev_ops = &brcmf_netdev_ops_pri;
519 ndev->needed_headroom += drvr->hdrlen;
520 ndev->ethtool_ops = &brcmf_ethtool_ops;
522 drvr->rxsz = ndev->mtu + ndev->hard_header_len +
525 /* set the mac address */
526 memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
528 INIT_WORK(&ifp->setmacaddr_work, _brcmf_set_mac_address);
529 INIT_WORK(&ifp->multicast_work, _brcmf_set_multicast_list);
530 INIT_WORK(&ifp->ndoffload_work, _brcmf_update_ndtable);
533 err = register_netdevice(ndev);
535 err = register_netdev(ndev);
537 brcmf_err("couldn't register the net device\n");
541 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
545 drvr->iflist[ifp->bsscfgidx] = NULL;
546 ndev->netdev_ops = NULL;
551 static void brcmf_net_detach(struct net_device *ndev, bool rtnl_locked)
553 if (ndev->reg_state == NETREG_REGISTERED) {
555 unregister_netdevice(ndev);
557 unregister_netdev(ndev);
559 brcmf_cfg80211_free_netdev(ndev);
563 void brcmf_net_setcarrier(struct brcmf_if *ifp, bool on)
565 struct net_device *ndev;
567 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d carrier=%d\n", ifp->bsscfgidx,
571 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_DISCONNECTED, !on);
573 if (!netif_carrier_ok(ndev))
574 netif_carrier_on(ndev);
577 if (netif_carrier_ok(ndev))
578 netif_carrier_off(ndev);
582 static int brcmf_net_p2p_open(struct net_device *ndev)
584 brcmf_dbg(TRACE, "Enter\n");
586 return brcmf_cfg80211_up(ndev);
589 static int brcmf_net_p2p_stop(struct net_device *ndev)
591 brcmf_dbg(TRACE, "Enter\n");
593 return brcmf_cfg80211_down(ndev);
596 static netdev_tx_t brcmf_net_p2p_start_xmit(struct sk_buff *skb,
597 struct net_device *ndev)
600 dev_kfree_skb_any(skb);
605 static const struct net_device_ops brcmf_netdev_ops_p2p = {
606 .ndo_open = brcmf_net_p2p_open,
607 .ndo_stop = brcmf_net_p2p_stop,
608 .ndo_start_xmit = brcmf_net_p2p_start_xmit
611 static int brcmf_net_p2p_attach(struct brcmf_if *ifp)
613 struct net_device *ndev;
615 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
619 ndev->netdev_ops = &brcmf_netdev_ops_p2p;
621 /* set the mac address */
622 memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
624 if (register_netdev(ndev) != 0) {
625 brcmf_err("couldn't register the p2p net device\n");
629 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
634 ifp->drvr->iflist[ifp->bsscfgidx] = NULL;
635 ndev->netdev_ops = NULL;
640 struct brcmf_if *brcmf_add_if(struct brcmf_pub *drvr, s32 bsscfgidx, s32 ifidx,
641 bool is_p2pdev, const char *name, u8 *mac_addr)
643 struct brcmf_if *ifp;
644 struct net_device *ndev;
646 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx, ifidx);
648 ifp = drvr->iflist[bsscfgidx];
650 * Delete the existing interface before overwriting it
651 * in case we missed the BRCMF_E_IF_DEL event.
655 brcmf_err("ERROR: netdev:%s already exists\n",
657 netif_stop_queue(ifp->ndev);
658 brcmf_net_detach(ifp->ndev, false);
659 drvr->iflist[bsscfgidx] = NULL;
661 brcmf_dbg(INFO, "netdev:%s ignore IF event\n",
663 return ERR_PTR(-EINVAL);
667 if (!drvr->settings->p2p_enable && is_p2pdev) {
668 /* this is P2P_DEVICE interface */
669 brcmf_dbg(INFO, "allocate non-netdev interface\n");
670 ifp = kzalloc(sizeof(*ifp), GFP_KERNEL);
672 return ERR_PTR(-ENOMEM);
674 brcmf_dbg(INFO, "allocate netdev interface\n");
675 /* Allocate netdev, including space for private structure */
676 ndev = alloc_netdev(sizeof(*ifp), is_p2pdev ? "p2p%d" : name,
677 NET_NAME_UNKNOWN, ether_setup);
679 return ERR_PTR(-ENOMEM);
681 ndev->destructor = brcmf_cfg80211_free_netdev;
682 ifp = netdev_priv(ndev);
684 /* store mapping ifidx to bsscfgidx */
685 if (drvr->if2bss[ifidx] == BRCMF_BSSIDX_INVALID)
686 drvr->if2bss[ifidx] = bsscfgidx;
690 drvr->iflist[bsscfgidx] = ifp;
692 ifp->bsscfgidx = bsscfgidx;
694 init_waitqueue_head(&ifp->pend_8021x_wait);
695 spin_lock_init(&ifp->netif_stop_lock);
697 if (mac_addr != NULL)
698 memcpy(ifp->mac_addr, mac_addr, ETH_ALEN);
700 brcmf_dbg(TRACE, " ==== pid:%x, if:%s (%pM) created ===\n",
701 current->pid, name, ifp->mac_addr);
706 static void brcmf_del_if(struct brcmf_pub *drvr, s32 bsscfgidx,
709 struct brcmf_if *ifp;
711 ifp = drvr->iflist[bsscfgidx];
712 drvr->iflist[bsscfgidx] = NULL;
714 brcmf_err("Null interface, bsscfgidx=%d\n", bsscfgidx);
717 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx,
719 if (drvr->if2bss[ifp->ifidx] == bsscfgidx)
720 drvr->if2bss[ifp->ifidx] = BRCMF_BSSIDX_INVALID;
722 if (bsscfgidx == 0) {
723 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
725 brcmf_netdev_stop(ifp->ndev);
729 netif_stop_queue(ifp->ndev);
732 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
733 cancel_work_sync(&ifp->setmacaddr_work);
734 cancel_work_sync(&ifp->multicast_work);
735 cancel_work_sync(&ifp->ndoffload_work);
737 brcmf_net_detach(ifp->ndev, rtnl_locked);
739 /* Only p2p device interfaces which get dynamically created
740 * end up here. In this case the p2p module should be informed
741 * about the removal of the interface within the firmware. If
742 * not then p2p commands towards the firmware will cause some
743 * serious troublesome side effects. The p2p module will clean
744 * up the ifp if needed.
746 brcmf_p2p_ifp_removed(ifp);
751 void brcmf_remove_interface(struct brcmf_if *ifp, bool rtnl_locked)
753 if (!ifp || WARN_ON(ifp->drvr->iflist[ifp->bsscfgidx] != ifp))
755 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp->bsscfgidx,
757 brcmf_fws_del_interface(ifp);
758 brcmf_del_if(ifp->drvr, ifp->bsscfgidx, rtnl_locked);
762 #define ARPOL_MAX_ENTRIES 8
763 static int brcmf_inetaddr_changed(struct notifier_block *nb,
764 unsigned long action, void *data)
766 struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
768 struct in_ifaddr *ifa = data;
769 struct net_device *ndev = ifa->ifa_dev->dev;
770 struct brcmf_if *ifp;
773 __be32 addr_table[ARPOL_MAX_ENTRIES] = {0};
775 /* Find out if the notification is meant for us */
776 for (idx = 0; idx < BRCMF_MAX_IFS; idx++) {
777 ifp = drvr->iflist[idx];
778 if (ifp && ifp->ndev == ndev)
780 if (idx == BRCMF_MAX_IFS - 1)
784 /* check if arp offload is supported */
785 ret = brcmf_fil_iovar_int_get(ifp, "arpoe", &val);
789 /* old version only support primary index */
790 ret = brcmf_fil_iovar_int_get(ifp, "arp_version", &val);
794 ifp = drvr->iflist[0];
796 /* retrieve the table from firmware */
797 ret = brcmf_fil_iovar_data_get(ifp, "arp_hostip", addr_table,
800 brcmf_err("fail to get arp ip table err:%d\n", ret);
804 for (i = 0; i < ARPOL_MAX_ENTRIES; i++)
805 if (ifa->ifa_address == addr_table[i])
810 if (i == ARPOL_MAX_ENTRIES) {
811 brcmf_dbg(TRACE, "add %pI4 to arp table\n",
813 /* set it directly */
814 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
815 &ifa->ifa_address, sizeof(ifa->ifa_address));
817 brcmf_err("add arp ip err %d\n", ret);
821 if (i < ARPOL_MAX_ENTRIES) {
823 brcmf_dbg(TRACE, "remove %pI4 from arp table\n",
825 /* clear the table in firmware */
826 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear",
829 brcmf_err("fail to clear arp ip table err:%d\n",
833 for (i = 0; i < ARPOL_MAX_ENTRIES; i++) {
834 if (addr_table[i] == 0)
836 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
838 sizeof(addr_table[i]));
840 brcmf_err("add arp ip err %d\n",
853 #if IS_ENABLED(CONFIG_IPV6)
854 static int brcmf_inet6addr_changed(struct notifier_block *nb,
855 unsigned long action, void *data)
857 struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
859 struct inet6_ifaddr *ifa = data;
860 struct brcmf_if *ifp;
862 struct in6_addr *table;
864 /* Only handle primary interface */
865 ifp = drvr->iflist[0];
868 if (ifp->ndev != ifa->idev->dev)
871 table = ifp->ipv6_addr_tbl;
872 for (i = 0; i < NDOL_MAX_ENTRIES; i++)
873 if (ipv6_addr_equal(&ifa->addr, &table[i]))
878 if (i == NDOL_MAX_ENTRIES) {
879 if (ifp->ipv6addr_idx < NDOL_MAX_ENTRIES) {
880 table[ifp->ipv6addr_idx++] = ifa->addr;
882 for (i = 0; i < NDOL_MAX_ENTRIES - 1; i++)
883 table[i] = table[i + 1];
884 table[NDOL_MAX_ENTRIES - 1] = ifa->addr;
889 if (i < NDOL_MAX_ENTRIES)
890 for (; i < ifp->ipv6addr_idx; i++)
891 table[i] = table[i + 1];
897 schedule_work(&ifp->ndoffload_work);
903 int brcmf_attach(struct device *dev, struct brcmf_mp_device *settings)
905 struct brcmf_pub *drvr = NULL;
909 brcmf_dbg(TRACE, "Enter\n");
911 /* Allocate primary brcmf_info */
912 drvr = kzalloc(sizeof(struct brcmf_pub), GFP_ATOMIC);
916 for (i = 0; i < ARRAY_SIZE(drvr->if2bss); i++)
917 drvr->if2bss[i] = BRCMF_BSSIDX_INVALID;
919 mutex_init(&drvr->proto_block);
921 /* Link to bus module */
923 drvr->bus_if = dev_get_drvdata(dev);
924 drvr->bus_if->drvr = drvr;
925 drvr->settings = settings;
927 /* attach debug facilities */
928 brcmf_debug_attach(drvr);
930 /* Attach and link in the protocol */
931 ret = brcmf_proto_attach(drvr);
933 brcmf_err("brcmf_prot_attach failed\n");
937 /* attach firmware event handler */
938 brcmf_fweh_attach(drvr);
948 static int brcmf_revinfo_read(struct seq_file *s, void *data)
950 struct brcmf_bus *bus_if = dev_get_drvdata(s->private);
951 struct brcmf_rev_info *ri = &bus_if->drvr->revinfo;
952 char drev[BRCMU_DOTREV_LEN];
953 char brev[BRCMU_BOARDREV_LEN];
955 seq_printf(s, "vendorid: 0x%04x\n", ri->vendorid);
956 seq_printf(s, "deviceid: 0x%04x\n", ri->deviceid);
957 seq_printf(s, "radiorev: %s\n", brcmu_dotrev_str(ri->radiorev, drev));
958 seq_printf(s, "chipnum: %u (%x)\n", ri->chipnum, ri->chipnum);
959 seq_printf(s, "chiprev: %u\n", ri->chiprev);
960 seq_printf(s, "chippkg: %u\n", ri->chippkg);
961 seq_printf(s, "corerev: %u\n", ri->corerev);
962 seq_printf(s, "boardid: 0x%04x\n", ri->boardid);
963 seq_printf(s, "boardvendor: 0x%04x\n", ri->boardvendor);
964 seq_printf(s, "boardrev: %s\n", brcmu_boardrev_str(ri->boardrev, brev));
965 seq_printf(s, "driverrev: %s\n", brcmu_dotrev_str(ri->driverrev, drev));
966 seq_printf(s, "ucoderev: %u\n", ri->ucoderev);
967 seq_printf(s, "bus: %u\n", ri->bus);
968 seq_printf(s, "phytype: %u\n", ri->phytype);
969 seq_printf(s, "phyrev: %u\n", ri->phyrev);
970 seq_printf(s, "anarev: %u\n", ri->anarev);
971 seq_printf(s, "nvramrev: %08x\n", ri->nvramrev);
976 int brcmf_bus_start(struct device *dev)
979 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
980 struct brcmf_pub *drvr = bus_if->drvr;
981 struct brcmf_if *ifp;
982 struct brcmf_if *p2p_ifp;
984 brcmf_dbg(TRACE, "\n");
986 /* add primary networking interface */
987 ifp = brcmf_add_if(drvr, 0, 0, false, "wlan%d", NULL);
993 /* signal bus ready */
994 brcmf_bus_change_state(bus_if, BRCMF_BUS_UP);
996 /* Bus is ready, do any initialization */
997 ret = brcmf_c_preinit_dcmds(ifp);
1001 brcmf_debugfs_add_entry(drvr, "revinfo", brcmf_revinfo_read);
1003 /* assure we have chipid before feature attach */
1004 if (!bus_if->chip) {
1005 bus_if->chip = drvr->revinfo.chipnum;
1006 bus_if->chiprev = drvr->revinfo.chiprev;
1007 brcmf_dbg(INFO, "firmware revinfo: chip %x (%d) rev %d\n",
1008 bus_if->chip, bus_if->chip, bus_if->chiprev);
1010 brcmf_feat_attach(drvr);
1012 ret = brcmf_fws_init(drvr);
1016 brcmf_fws_add_interface(ifp);
1018 drvr->config = brcmf_cfg80211_attach(drvr, bus_if->dev,
1019 drvr->settings->p2p_enable);
1020 if (drvr->config == NULL) {
1025 ret = brcmf_net_attach(ifp, false);
1027 if ((!ret) && (drvr->settings->p2p_enable)) {
1028 p2p_ifp = drvr->iflist[1];
1030 ret = brcmf_net_p2p_attach(p2p_ifp);
1037 drvr->inetaddr_notifier.notifier_call = brcmf_inetaddr_changed;
1038 ret = register_inetaddr_notifier(&drvr->inetaddr_notifier);
1042 #if IS_ENABLED(CONFIG_IPV6)
1043 drvr->inet6addr_notifier.notifier_call = brcmf_inet6addr_changed;
1044 ret = register_inet6addr_notifier(&drvr->inet6addr_notifier);
1046 unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1050 #endif /* CONFIG_INET */
1055 brcmf_err("failed: %d\n", ret);
1057 brcmf_cfg80211_detach(drvr->config);
1058 drvr->config = NULL;
1061 brcmf_fws_del_interface(ifp);
1062 brcmf_fws_deinit(drvr);
1065 brcmf_net_detach(ifp->ndev, false);
1067 brcmf_net_detach(p2p_ifp->ndev, false);
1068 drvr->iflist[0] = NULL;
1069 drvr->iflist[1] = NULL;
1070 if (drvr->settings->ignore_probe_fail)
1076 void brcmf_bus_add_txhdrlen(struct device *dev, uint len)
1078 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1079 struct brcmf_pub *drvr = bus_if->drvr;
1082 drvr->hdrlen += len;
1086 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1088 brcmf_dbg(TRACE, "Enter\n");
1091 /* Stop the bus module */
1092 brcmf_bus_stop(drvr->bus_if);
1096 void brcmf_dev_reset(struct device *dev)
1098 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1099 struct brcmf_pub *drvr = bus_if->drvr;
1104 if (drvr->iflist[0])
1105 brcmf_fil_cmd_int_set(drvr->iflist[0], BRCMF_C_TERMINATED, 1);
1108 void brcmf_detach(struct device *dev)
1111 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1112 struct brcmf_pub *drvr = bus_if->drvr;
1114 brcmf_dbg(TRACE, "Enter\n");
1120 unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1123 #if IS_ENABLED(CONFIG_IPV6)
1124 unregister_inet6addr_notifier(&drvr->inet6addr_notifier);
1127 /* stop firmware event handling */
1128 brcmf_fweh_detach(drvr);
1130 brcmf_p2p_detach(&drvr->config->p2p);
1132 brcmf_bus_change_state(bus_if, BRCMF_BUS_DOWN);
1134 /* make sure primary interface removed last */
1135 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
1136 brcmf_remove_interface(drvr->iflist[i], false);
1138 brcmf_cfg80211_detach(drvr->config);
1140 brcmf_fws_deinit(drvr);
1142 brcmf_bus_detach(drvr);
1144 brcmf_proto_detach(drvr);
1146 brcmf_debug_detach(drvr);
1147 bus_if->drvr = NULL;
1151 s32 brcmf_iovar_data_set(struct device *dev, char *name, void *data, u32 len)
1153 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1154 struct brcmf_if *ifp = bus_if->drvr->iflist[0];
1156 return brcmf_fil_iovar_data_set(ifp, name, data, len);
1159 static int brcmf_get_pend_8021x_cnt(struct brcmf_if *ifp)
1161 return atomic_read(&ifp->pend_8021x_cnt);
1164 int brcmf_netdev_wait_pend8021x(struct brcmf_if *ifp)
1168 err = wait_event_timeout(ifp->pend_8021x_wait,
1169 !brcmf_get_pend_8021x_cnt(ifp),
1170 MAX_WAIT_FOR_8021X_TX);
1177 void brcmf_bus_change_state(struct brcmf_bus *bus, enum brcmf_bus_state state)
1179 struct brcmf_pub *drvr = bus->drvr;
1180 struct net_device *ndev;
1183 brcmf_dbg(TRACE, "%d -> %d\n", bus->state, state);
1186 if (state == BRCMF_BUS_UP) {
1187 for (ifidx = 0; ifidx < BRCMF_MAX_IFS; ifidx++) {
1188 if ((drvr->iflist[ifidx]) &&
1189 (drvr->iflist[ifidx]->ndev)) {
1190 ndev = drvr->iflist[ifidx]->ndev;
1191 if (netif_queue_stopped(ndev))
1192 netif_wake_queue(ndev);
1198 static void brcmf_driver_register(struct work_struct *work)
1200 #ifdef CONFIG_BRCMFMAC_SDIO
1201 brcmf_sdio_register();
1203 #ifdef CONFIG_BRCMFMAC_USB
1204 brcmf_usb_register();
1206 #ifdef CONFIG_BRCMFMAC_PCIE
1207 brcmf_pcie_register();
1210 static DECLARE_WORK(brcmf_driver_work, brcmf_driver_register);
1212 int __init brcmf_core_init(void)
1214 if (!schedule_work(&brcmf_driver_work))
1220 void __exit brcmf_core_exit(void)
1222 cancel_work_sync(&brcmf_driver_work);
1224 #ifdef CONFIG_BRCMFMAC_SDIO
1227 #ifdef CONFIG_BRCMFMAC_USB
1230 #ifdef CONFIG_BRCMFMAC_PCIE