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 void brcmf_configure_arp_nd_offload(struct brcmf_if
*ifp
, bool enable
)
80 mode
= BRCMF_ARP_OL_AGENT
| BRCMF_ARP_OL_PEER_AUTO_REPLY
;
84 /* Try to set and enable ARP offload feature, this may fail, then it */
85 /* is simply not supported and err 0 will be returned */
86 err
= brcmf_fil_iovar_int_set(ifp
, "arp_ol", mode
);
88 brcmf_dbg(TRACE
, "failed to set ARP offload mode to 0x%x, err = %d\n",
91 err
= brcmf_fil_iovar_int_set(ifp
, "arpoe", enable
);
93 brcmf_dbg(TRACE
, "failed to configure (%d) ARP offload err = %d\n",
96 brcmf_dbg(TRACE
, "successfully configured (%d) ARP offload to 0x%x\n",
101 err
= brcmf_fil_iovar_int_set(ifp
, "ndoe", enable
);
103 brcmf_dbg(TRACE
, "failed to configure (%d) ND offload err = %d\n",
106 brcmf_dbg(TRACE
, "successfully configured (%d) ND offload to 0x%x\n",
111 static void _brcmf_set_multicast_list(struct work_struct
*work
)
113 struct brcmf_if
*ifp
;
114 struct net_device
*ndev
;
115 struct netdev_hw_addr
*ha
;
122 ifp
= container_of(work
, struct brcmf_if
, multicast_work
);
124 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
128 /* Determine initial value of allmulti flag */
129 cmd_value
= (ndev
->flags
& IFF_ALLMULTI
) ? true : false;
131 /* Send down the multicast list first. */
132 cnt
= netdev_mc_count(ndev
);
133 buflen
= sizeof(cnt
) + (cnt
* ETH_ALEN
);
134 buf
= kmalloc(buflen
, GFP_ATOMIC
);
139 cnt_le
= cpu_to_le32(cnt
);
140 memcpy(bufp
, &cnt_le
, sizeof(cnt_le
));
141 bufp
+= sizeof(cnt_le
);
143 netdev_for_each_mc_addr(ha
, ndev
) {
146 memcpy(bufp
, ha
->addr
, ETH_ALEN
);
151 err
= brcmf_fil_iovar_data_set(ifp
, "mcast_list", buf
, buflen
);
153 brcmf_err("Setting mcast_list failed, %d\n", err
);
154 cmd_value
= cnt
? true : cmd_value
;
160 * Now send the allmulti setting. This is based on the setting in the
161 * net_device flags, but might be modified above to be turned on if we
162 * were trying to set some addresses and dongle rejected it...
164 err
= brcmf_fil_iovar_int_set(ifp
, "allmulti", cmd_value
);
166 brcmf_err("Setting allmulti failed, %d\n", err
);
168 /*Finally, pick up the PROMISC flag */
169 cmd_value
= (ndev
->flags
& IFF_PROMISC
) ? true : false;
170 err
= brcmf_fil_cmd_int_set(ifp
, BRCMF_C_SET_PROMISC
, cmd_value
);
172 brcmf_err("Setting BRCMF_C_SET_PROMISC failed, %d\n",
174 brcmf_configure_arp_nd_offload(ifp
, !cmd_value
);
177 #if IS_ENABLED(CONFIG_IPV6)
178 static void _brcmf_update_ndtable(struct work_struct
*work
)
180 struct brcmf_if
*ifp
;
183 ifp
= container_of(work
, struct brcmf_if
, ndoffload_work
);
185 /* clear the table in firmware */
186 ret
= brcmf_fil_iovar_data_set(ifp
, "nd_hostip_clear", NULL
, 0);
188 brcmf_dbg(TRACE
, "fail to clear nd ip table err:%d\n", ret
);
192 for (i
= 0; i
< ifp
->ipv6addr_idx
; i
++) {
193 ret
= brcmf_fil_iovar_data_set(ifp
, "nd_hostip",
194 &ifp
->ipv6_addr_tbl
[i
],
195 sizeof(struct in6_addr
));
197 brcmf_err("add nd ip err %d\n", ret
);
201 static void _brcmf_update_ndtable(struct work_struct
*work
)
206 static int brcmf_netdev_set_mac_address(struct net_device
*ndev
, void *addr
)
208 struct brcmf_if
*ifp
= netdev_priv(ndev
);
209 struct sockaddr
*sa
= (struct sockaddr
*)addr
;
212 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
214 err
= brcmf_fil_iovar_data_set(ifp
, "cur_etheraddr", sa
->sa_data
,
217 brcmf_err("Setting cur_etheraddr failed, %d\n", err
);
219 brcmf_dbg(TRACE
, "updated to %pM\n", sa
->sa_data
);
220 memcpy(ifp
->mac_addr
, sa
->sa_data
, ETH_ALEN
);
221 memcpy(ifp
->ndev
->dev_addr
, ifp
->mac_addr
, ETH_ALEN
);
226 static void brcmf_netdev_set_multicast_list(struct net_device
*ndev
)
228 struct brcmf_if
*ifp
= netdev_priv(ndev
);
230 schedule_work(&ifp
->multicast_work
);
234 * brcmf_skb_is_iapp - checks if skb is an IAPP packet
238 static bool brcmf_skb_is_iapp(struct sk_buff
*skb
)
240 static const u8 iapp_l2_update_packet
[6] __aligned(2) = {
241 0x00, 0x01, 0xaf, 0x81, 0x01, 0x00,
243 unsigned char *eth_data
;
244 #if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
248 if (skb
->len
- skb
->mac_len
!= 6 ||
249 !is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
252 eth_data
= skb_mac_header(skb
) + ETH_HLEN
;
253 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
254 return !(((*(const u32
*)eth_data
) ^ (*(const u32
*)iapp_l2_update_packet
)) |
255 ((*(const u16
*)(eth_data
+ 4)) ^ (*(const u16
*)(iapp_l2_update_packet
+ 4))));
257 a
= (const u16
*)eth_data
;
258 b
= (const u16
*)iapp_l2_update_packet
;
260 return !((a
[0] ^ b
[0]) | (a
[1] ^ b
[1]) | (a
[2] ^ b
[2]));
264 static netdev_tx_t
brcmf_netdev_start_xmit(struct sk_buff
*skb
,
265 struct net_device
*ndev
)
268 struct brcmf_if
*ifp
= netdev_priv(ndev
);
269 struct brcmf_pub
*drvr
= ifp
->drvr
;
273 brcmf_dbg(DATA
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
275 /* Can the device send data? */
276 if (drvr
->bus_if
->state
!= BRCMF_BUS_UP
) {
277 brcmf_err("xmit rejected state=%d\n", drvr
->bus_if
->state
);
278 netif_stop_queue(ndev
);
284 /* Some recent Broadcom's firmwares disassociate STA when they receive
285 * an 802.11f ADD frame. This behavior can lead to a local DoS security
286 * issue. Attacker may trigger disassociation of any STA by sending a
287 * proper Ethernet frame to the wireless interface.
289 * Moreover this feature may break AP interfaces in some specific
290 * setups. This applies e.g. to the bridge with hairpin mode enabled and
291 * IFLA_BRPORT_MCAST_TO_UCAST set. IAPP packet generated by a firmware
292 * will get passed back to the wireless interface and cause immediate
293 * disassociation of a just-connected STA.
295 if (!drvr
->settings
->iapp
&& brcmf_skb_is_iapp(skb
)) {
301 /* Make sure there's enough writeable headroom */
302 if (skb_headroom(skb
) < drvr
->hdrlen
|| skb_header_cloned(skb
)) {
303 head_delta
= max_t(int, drvr
->hdrlen
- skb_headroom(skb
), 0);
305 brcmf_dbg(INFO
, "%s: insufficient headroom (%d)\n",
306 brcmf_ifname(ifp
), head_delta
);
307 atomic_inc(&drvr
->bus_if
->stats
.pktcowed
);
308 ret
= pskb_expand_head(skb
, ALIGN(head_delta
, NET_SKB_PAD
), 0,
311 brcmf_err("%s: failed to expand headroom\n",
313 atomic_inc(&drvr
->bus_if
->stats
.pktcow_failed
);
318 /* validate length for ether packet */
319 if (skb
->len
< sizeof(*eh
)) {
325 eh
= (struct ethhdr
*)(skb
->data
);
327 if (eh
->h_proto
== htons(ETH_P_PAE
))
328 atomic_inc(&ifp
->pend_8021x_cnt
);
330 /* determine the priority */
331 if ((skb
->priority
== 0) || (skb
->priority
> 7))
332 skb
->priority
= cfg80211_classify8021d(skb
, NULL
);
334 ret
= brcmf_proto_tx_queue_data(drvr
, ifp
->ifidx
, skb
);
336 brcmf_txfinalize(ifp
, skb
, false);
340 ndev
->stats
.tx_dropped
++;
342 ndev
->stats
.tx_packets
++;
343 ndev
->stats
.tx_bytes
+= skb
->len
;
346 /* Return ok: we always eat the packet */
350 void brcmf_txflowblock_if(struct brcmf_if
*ifp
,
351 enum brcmf_netif_stop_reason reason
, bool state
)
355 if (!ifp
|| !ifp
->ndev
)
358 brcmf_dbg(TRACE
, "enter: bsscfgidx=%d stop=0x%X reason=%d state=%d\n",
359 ifp
->bsscfgidx
, ifp
->netif_stop
, reason
, state
);
361 spin_lock_irqsave(&ifp
->netif_stop_lock
, flags
);
363 if (!ifp
->netif_stop
)
364 netif_stop_queue(ifp
->ndev
);
365 ifp
->netif_stop
|= reason
;
367 ifp
->netif_stop
&= ~reason
;
368 if (!ifp
->netif_stop
)
369 netif_wake_queue(ifp
->ndev
);
371 spin_unlock_irqrestore(&ifp
->netif_stop_lock
, flags
);
374 void brcmf_netif_rx(struct brcmf_if
*ifp
, struct sk_buff
*skb
)
376 /* Most of Broadcom's firmwares send 802.11f ADD frame every time a new
377 * STA connects to the AP interface. This is an obsoleted standard most
378 * users don't use, so don't pass these frames up unless requested.
380 if (!ifp
->drvr
->settings
->iapp
&& brcmf_skb_is_iapp(skb
)) {
381 brcmu_pkt_buf_free_skb(skb
);
385 if (skb
->pkt_type
== PACKET_MULTICAST
)
386 ifp
->ndev
->stats
.multicast
++;
388 if (!(ifp
->ndev
->flags
& IFF_UP
)) {
389 brcmu_pkt_buf_free_skb(skb
);
393 ifp
->ndev
->stats
.rx_bytes
+= skb
->len
;
394 ifp
->ndev
->stats
.rx_packets
++;
396 brcmf_dbg(DATA
, "rx proto=0x%X\n", ntohs(skb
->protocol
));
400 /* If the receive is not processed inside an ISR,
401 * the softirqd must be woken explicitly to service
402 * the NET_RX_SOFTIRQ. This is handled by netif_rx_ni().
407 static int brcmf_rx_hdrpull(struct brcmf_pub
*drvr
, struct sk_buff
*skb
,
408 struct brcmf_if
**ifp
)
412 /* process and remove protocol-specific header */
413 ret
= brcmf_proto_hdrpull(drvr
, true, skb
, ifp
);
415 if (ret
|| !(*ifp
) || !(*ifp
)->ndev
) {
416 if (ret
!= -ENODATA
&& *ifp
)
417 (*ifp
)->ndev
->stats
.rx_errors
++;
418 brcmu_pkt_buf_free_skb(skb
);
422 skb
->protocol
= eth_type_trans(skb
, (*ifp
)->ndev
);
426 void brcmf_rx_frame(struct device
*dev
, struct sk_buff
*skb
, bool handle_event
)
428 struct brcmf_if
*ifp
;
429 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
430 struct brcmf_pub
*drvr
= bus_if
->drvr
;
432 brcmf_dbg(DATA
, "Enter: %s: rxp=%p\n", dev_name(dev
), skb
);
434 if (brcmf_rx_hdrpull(drvr
, skb
, &ifp
))
437 if (brcmf_proto_is_reorder_skb(skb
)) {
438 brcmf_proto_rxreorder(ifp
, skb
);
440 /* Process special event packets */
442 brcmf_fweh_process_skb(ifp
->drvr
, skb
);
444 brcmf_netif_rx(ifp
, skb
);
448 void brcmf_rx_event(struct device
*dev
, struct sk_buff
*skb
)
450 struct brcmf_if
*ifp
;
451 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
452 struct brcmf_pub
*drvr
= bus_if
->drvr
;
454 brcmf_dbg(EVENT
, "Enter: %s: rxp=%p\n", dev_name(dev
), skb
);
456 if (brcmf_rx_hdrpull(drvr
, skb
, &ifp
))
459 brcmf_fweh_process_skb(ifp
->drvr
, skb
);
460 brcmu_pkt_buf_free_skb(skb
);
463 void brcmf_txfinalize(struct brcmf_if
*ifp
, struct sk_buff
*txp
, bool success
)
468 eh
= (struct ethhdr
*)(txp
->data
);
469 type
= ntohs(eh
->h_proto
);
471 if (type
== ETH_P_PAE
) {
472 atomic_dec(&ifp
->pend_8021x_cnt
);
473 if (waitqueue_active(&ifp
->pend_8021x_wait
))
474 wake_up(&ifp
->pend_8021x_wait
);
478 ifp
->ndev
->stats
.tx_errors
++;
480 brcmu_pkt_buf_free_skb(txp
);
483 static void brcmf_ethtool_get_drvinfo(struct net_device
*ndev
,
484 struct ethtool_drvinfo
*info
)
486 struct brcmf_if
*ifp
= netdev_priv(ndev
);
487 struct brcmf_pub
*drvr
= ifp
->drvr
;
488 char drev
[BRCMU_DOTREV_LEN
] = "n/a";
490 if (drvr
->revinfo
.result
== 0)
491 brcmu_dotrev_str(drvr
->revinfo
.driverrev
, drev
);
492 strlcpy(info
->driver
, KBUILD_MODNAME
, sizeof(info
->driver
));
493 strlcpy(info
->version
, drev
, sizeof(info
->version
));
494 strlcpy(info
->fw_version
, drvr
->fwver
, sizeof(info
->fw_version
));
495 strlcpy(info
->bus_info
, dev_name(drvr
->bus_if
->dev
),
496 sizeof(info
->bus_info
));
499 static const struct ethtool_ops brcmf_ethtool_ops
= {
500 .get_drvinfo
= brcmf_ethtool_get_drvinfo
,
503 static int brcmf_netdev_stop(struct net_device
*ndev
)
505 struct brcmf_if
*ifp
= netdev_priv(ndev
);
507 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
509 brcmf_cfg80211_down(ndev
);
511 brcmf_fil_iovar_data_set(ifp
, "arp_hostip_clear", NULL
, 0);
513 brcmf_net_setcarrier(ifp
, false);
518 static int brcmf_netdev_open(struct net_device
*ndev
)
520 struct brcmf_if
*ifp
= netdev_priv(ndev
);
521 struct brcmf_pub
*drvr
= ifp
->drvr
;
522 struct brcmf_bus
*bus_if
= drvr
->bus_if
;
525 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
527 /* If bus is not ready, can't continue */
528 if (bus_if
->state
!= BRCMF_BUS_UP
) {
529 brcmf_err("failed bus is not ready\n");
533 atomic_set(&ifp
->pend_8021x_cnt
, 0);
535 /* Get current TOE mode from dongle */
536 if (brcmf_fil_iovar_int_get(ifp
, "toe_ol", &toe_ol
) >= 0
537 && (toe_ol
& TOE_TX_CSUM_OL
) != 0)
538 ndev
->features
|= NETIF_F_IP_CSUM
;
540 ndev
->features
&= ~NETIF_F_IP_CSUM
;
542 if (brcmf_cfg80211_up(ndev
)) {
543 brcmf_err("failed to bring up cfg80211\n");
547 /* Clear, carrier, set when connected or AP mode. */
548 netif_carrier_off(ndev
);
552 static const struct net_device_ops brcmf_netdev_ops_pri
= {
553 .ndo_open
= brcmf_netdev_open
,
554 .ndo_stop
= brcmf_netdev_stop
,
555 .ndo_start_xmit
= brcmf_netdev_start_xmit
,
556 .ndo_set_mac_address
= brcmf_netdev_set_mac_address
,
557 .ndo_set_rx_mode
= brcmf_netdev_set_multicast_list
560 int brcmf_net_attach(struct brcmf_if
*ifp
, bool rtnl_locked
)
562 struct brcmf_pub
*drvr
= ifp
->drvr
;
563 struct net_device
*ndev
;
566 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d mac=%pM\n", ifp
->bsscfgidx
,
570 /* set appropriate operations */
571 ndev
->netdev_ops
= &brcmf_netdev_ops_pri
;
573 ndev
->needed_headroom
+= drvr
->hdrlen
;
574 ndev
->ethtool_ops
= &brcmf_ethtool_ops
;
576 /* set the mac address & netns */
577 memcpy(ndev
->dev_addr
, ifp
->mac_addr
, ETH_ALEN
);
578 dev_net_set(ndev
, wiphy_net(cfg_to_wiphy(drvr
->config
)));
580 INIT_WORK(&ifp
->multicast_work
, _brcmf_set_multicast_list
);
581 INIT_WORK(&ifp
->ndoffload_work
, _brcmf_update_ndtable
);
584 err
= register_netdevice(ndev
);
586 err
= register_netdev(ndev
);
588 brcmf_err("couldn't register the net device\n");
592 ndev
->priv_destructor
= brcmf_cfg80211_free_netdev
;
593 brcmf_dbg(INFO
, "%s: Broadcom Dongle Host Driver\n", ndev
->name
);
597 drvr
->iflist
[ifp
->bsscfgidx
] = NULL
;
598 ndev
->netdev_ops
= NULL
;
602 static void brcmf_net_detach(struct net_device
*ndev
, bool rtnl_locked
)
604 if (ndev
->reg_state
== NETREG_REGISTERED
) {
606 unregister_netdevice(ndev
);
608 unregister_netdev(ndev
);
610 brcmf_cfg80211_free_netdev(ndev
);
615 void brcmf_net_setcarrier(struct brcmf_if
*ifp
, bool on
)
617 struct net_device
*ndev
;
619 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d carrier=%d\n", ifp
->bsscfgidx
,
623 brcmf_txflowblock_if(ifp
, BRCMF_NETIF_STOP_REASON_DISCONNECTED
, !on
);
625 if (!netif_carrier_ok(ndev
))
626 netif_carrier_on(ndev
);
629 if (netif_carrier_ok(ndev
))
630 netif_carrier_off(ndev
);
634 static int brcmf_net_p2p_open(struct net_device
*ndev
)
636 brcmf_dbg(TRACE
, "Enter\n");
638 return brcmf_cfg80211_up(ndev
);
641 static int brcmf_net_p2p_stop(struct net_device
*ndev
)
643 brcmf_dbg(TRACE
, "Enter\n");
645 return brcmf_cfg80211_down(ndev
);
648 static netdev_tx_t
brcmf_net_p2p_start_xmit(struct sk_buff
*skb
,
649 struct net_device
*ndev
)
652 dev_kfree_skb_any(skb
);
657 static const struct net_device_ops brcmf_netdev_ops_p2p
= {
658 .ndo_open
= brcmf_net_p2p_open
,
659 .ndo_stop
= brcmf_net_p2p_stop
,
660 .ndo_start_xmit
= brcmf_net_p2p_start_xmit
663 static int brcmf_net_p2p_attach(struct brcmf_if
*ifp
)
665 struct net_device
*ndev
;
667 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d mac=%pM\n", ifp
->bsscfgidx
,
671 ndev
->netdev_ops
= &brcmf_netdev_ops_p2p
;
673 /* set the mac address */
674 memcpy(ndev
->dev_addr
, ifp
->mac_addr
, ETH_ALEN
);
676 if (register_netdev(ndev
) != 0) {
677 brcmf_err("couldn't register the p2p net device\n");
681 brcmf_dbg(INFO
, "%s: Broadcom Dongle Host Driver\n", ndev
->name
);
686 ifp
->drvr
->iflist
[ifp
->bsscfgidx
] = NULL
;
687 ndev
->netdev_ops
= NULL
;
691 struct brcmf_if
*brcmf_add_if(struct brcmf_pub
*drvr
, s32 bsscfgidx
, s32 ifidx
,
692 bool is_p2pdev
, const char *name
, u8
*mac_addr
)
694 struct brcmf_if
*ifp
;
695 struct net_device
*ndev
;
697 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx
, ifidx
);
699 ifp
= drvr
->iflist
[bsscfgidx
];
701 * Delete the existing interface before overwriting it
702 * in case we missed the BRCMF_E_IF_DEL event.
706 brcmf_err("ERROR: netdev:%s already exists\n",
708 netif_stop_queue(ifp
->ndev
);
709 brcmf_net_detach(ifp
->ndev
, false);
710 drvr
->iflist
[bsscfgidx
] = NULL
;
712 brcmf_dbg(INFO
, "netdev:%s ignore IF event\n",
714 return ERR_PTR(-EINVAL
);
718 if (!drvr
->settings
->p2p_enable
&& is_p2pdev
) {
719 /* this is P2P_DEVICE interface */
720 brcmf_dbg(INFO
, "allocate non-netdev interface\n");
721 ifp
= kzalloc(sizeof(*ifp
), GFP_KERNEL
);
723 return ERR_PTR(-ENOMEM
);
725 brcmf_dbg(INFO
, "allocate netdev interface\n");
726 /* Allocate netdev, including space for private structure */
727 ndev
= alloc_netdev(sizeof(*ifp
), is_p2pdev
? "p2p%d" : name
,
728 NET_NAME_UNKNOWN
, ether_setup
);
730 return ERR_PTR(-ENOMEM
);
732 ndev
->needs_free_netdev
= true;
733 ifp
= netdev_priv(ndev
);
735 /* store mapping ifidx to bsscfgidx */
736 if (drvr
->if2bss
[ifidx
] == BRCMF_BSSIDX_INVALID
)
737 drvr
->if2bss
[ifidx
] = bsscfgidx
;
741 drvr
->iflist
[bsscfgidx
] = ifp
;
743 ifp
->bsscfgidx
= bsscfgidx
;
745 init_waitqueue_head(&ifp
->pend_8021x_wait
);
746 spin_lock_init(&ifp
->netif_stop_lock
);
748 if (mac_addr
!= NULL
)
749 memcpy(ifp
->mac_addr
, mac_addr
, ETH_ALEN
);
751 brcmf_dbg(TRACE
, " ==== pid:%x, if:%s (%pM) created ===\n",
752 current
->pid
, name
, ifp
->mac_addr
);
757 static void brcmf_del_if(struct brcmf_pub
*drvr
, s32 bsscfgidx
,
760 struct brcmf_if
*ifp
;
762 ifp
= drvr
->iflist
[bsscfgidx
];
763 drvr
->iflist
[bsscfgidx
] = NULL
;
765 brcmf_err("Null interface, bsscfgidx=%d\n", bsscfgidx
);
768 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx
,
770 if (drvr
->if2bss
[ifp
->ifidx
] == bsscfgidx
)
771 drvr
->if2bss
[ifp
->ifidx
] = BRCMF_BSSIDX_INVALID
;
773 if (bsscfgidx
== 0) {
774 if (ifp
->ndev
->netdev_ops
== &brcmf_netdev_ops_pri
) {
776 brcmf_netdev_stop(ifp
->ndev
);
780 netif_stop_queue(ifp
->ndev
);
783 if (ifp
->ndev
->netdev_ops
== &brcmf_netdev_ops_pri
) {
784 cancel_work_sync(&ifp
->multicast_work
);
785 cancel_work_sync(&ifp
->ndoffload_work
);
787 brcmf_net_detach(ifp
->ndev
, rtnl_locked
);
789 /* Only p2p device interfaces which get dynamically created
790 * end up here. In this case the p2p module should be informed
791 * about the removal of the interface within the firmware. If
792 * not then p2p commands towards the firmware will cause some
793 * serious troublesome side effects. The p2p module will clean
794 * up the ifp if needed.
796 brcmf_p2p_ifp_removed(ifp
, rtnl_locked
);
801 void brcmf_remove_interface(struct brcmf_if
*ifp
, bool rtnl_locked
)
803 if (!ifp
|| WARN_ON(ifp
->drvr
->iflist
[ifp
->bsscfgidx
] != ifp
))
805 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp
->bsscfgidx
,
807 brcmf_proto_del_if(ifp
->drvr
, ifp
);
808 brcmf_del_if(ifp
->drvr
, ifp
->bsscfgidx
, rtnl_locked
);
811 static int brcmf_psm_watchdog_notify(struct brcmf_if
*ifp
,
812 const struct brcmf_event_msg
*evtmsg
,
817 brcmf_dbg(TRACE
, "enter: bsscfgidx=%d\n", ifp
->bsscfgidx
);
819 brcmf_err("PSM's watchdog has fired!\n");
821 err
= brcmf_debug_create_memdump(ifp
->drvr
->bus_if
, data
,
824 brcmf_err("Failed to get memory dump, %d\n", err
);
830 #define ARPOL_MAX_ENTRIES 8
831 static int brcmf_inetaddr_changed(struct notifier_block
*nb
,
832 unsigned long action
, void *data
)
834 struct brcmf_pub
*drvr
= container_of(nb
, struct brcmf_pub
,
836 struct in_ifaddr
*ifa
= data
;
837 struct net_device
*ndev
= ifa
->ifa_dev
->dev
;
838 struct brcmf_if
*ifp
;
841 __be32 addr_table
[ARPOL_MAX_ENTRIES
] = {0};
843 /* Find out if the notification is meant for us */
844 for (idx
= 0; idx
< BRCMF_MAX_IFS
; idx
++) {
845 ifp
= drvr
->iflist
[idx
];
846 if (ifp
&& ifp
->ndev
== ndev
)
848 if (idx
== BRCMF_MAX_IFS
- 1)
852 /* check if arp offload is supported */
853 ret
= brcmf_fil_iovar_int_get(ifp
, "arpoe", &val
);
857 /* old version only support primary index */
858 ret
= brcmf_fil_iovar_int_get(ifp
, "arp_version", &val
);
862 ifp
= drvr
->iflist
[0];
864 /* retrieve the table from firmware */
865 ret
= brcmf_fil_iovar_data_get(ifp
, "arp_hostip", addr_table
,
868 brcmf_err("fail to get arp ip table err:%d\n", ret
);
872 for (i
= 0; i
< ARPOL_MAX_ENTRIES
; i
++)
873 if (ifa
->ifa_address
== addr_table
[i
])
878 if (i
== ARPOL_MAX_ENTRIES
) {
879 brcmf_dbg(TRACE
, "add %pI4 to arp table\n",
881 /* set it directly */
882 ret
= brcmf_fil_iovar_data_set(ifp
, "arp_hostip",
883 &ifa
->ifa_address
, sizeof(ifa
->ifa_address
));
885 brcmf_err("add arp ip err %d\n", ret
);
889 if (i
< ARPOL_MAX_ENTRIES
) {
891 brcmf_dbg(TRACE
, "remove %pI4 from arp table\n",
893 /* clear the table in firmware */
894 ret
= brcmf_fil_iovar_data_set(ifp
, "arp_hostip_clear",
897 brcmf_err("fail to clear arp ip table err:%d\n",
901 for (i
= 0; i
< ARPOL_MAX_ENTRIES
; i
++) {
902 if (addr_table
[i
] == 0)
904 ret
= brcmf_fil_iovar_data_set(ifp
, "arp_hostip",
906 sizeof(addr_table
[i
]));
908 brcmf_err("add arp ip err %d\n",
921 #if IS_ENABLED(CONFIG_IPV6)
922 static int brcmf_inet6addr_changed(struct notifier_block
*nb
,
923 unsigned long action
, void *data
)
925 struct brcmf_pub
*drvr
= container_of(nb
, struct brcmf_pub
,
927 struct inet6_ifaddr
*ifa
= data
;
928 struct brcmf_if
*ifp
;
930 struct in6_addr
*table
;
932 /* Only handle primary interface */
933 ifp
= drvr
->iflist
[0];
936 if (ifp
->ndev
!= ifa
->idev
->dev
)
939 table
= ifp
->ipv6_addr_tbl
;
940 for (i
= 0; i
< NDOL_MAX_ENTRIES
; i
++)
941 if (ipv6_addr_equal(&ifa
->addr
, &table
[i
]))
946 if (i
== NDOL_MAX_ENTRIES
) {
947 if (ifp
->ipv6addr_idx
< NDOL_MAX_ENTRIES
) {
948 table
[ifp
->ipv6addr_idx
++] = ifa
->addr
;
950 for (i
= 0; i
< NDOL_MAX_ENTRIES
- 1; i
++)
951 table
[i
] = table
[i
+ 1];
952 table
[NDOL_MAX_ENTRIES
- 1] = ifa
->addr
;
957 if (i
< NDOL_MAX_ENTRIES
) {
958 for (; i
< ifp
->ipv6addr_idx
- 1; i
++)
959 table
[i
] = table
[i
+ 1];
960 memset(&table
[i
], 0, sizeof(table
[i
]));
968 schedule_work(&ifp
->ndoffload_work
);
974 int brcmf_attach(struct device
*dev
, struct brcmf_mp_device
*settings
)
976 struct brcmf_pub
*drvr
= NULL
;
980 brcmf_dbg(TRACE
, "Enter\n");
982 /* Allocate primary brcmf_info */
983 drvr
= kzalloc(sizeof(struct brcmf_pub
), GFP_ATOMIC
);
987 for (i
= 0; i
< ARRAY_SIZE(drvr
->if2bss
); i
++)
988 drvr
->if2bss
[i
] = BRCMF_BSSIDX_INVALID
;
990 mutex_init(&drvr
->proto_block
);
992 /* Link to bus module */
994 drvr
->bus_if
= dev_get_drvdata(dev
);
995 drvr
->bus_if
->drvr
= drvr
;
996 drvr
->settings
= settings
;
998 /* attach debug facilities */
999 brcmf_debug_attach(drvr
);
1001 /* Attach and link in the protocol */
1002 ret
= brcmf_proto_attach(drvr
);
1004 brcmf_err("brcmf_prot_attach failed\n");
1008 /* Attach to events important for core code */
1009 brcmf_fweh_register(drvr
, BRCMF_E_PSM_WATCHDOG
,
1010 brcmf_psm_watchdog_notify
);
1012 /* attach firmware event handler */
1013 brcmf_fweh_attach(drvr
);
1023 static int brcmf_revinfo_read(struct seq_file
*s
, void *data
)
1025 struct brcmf_bus
*bus_if
= dev_get_drvdata(s
->private);
1026 struct brcmf_rev_info
*ri
= &bus_if
->drvr
->revinfo
;
1027 char drev
[BRCMU_DOTREV_LEN
];
1028 char brev
[BRCMU_BOARDREV_LEN
];
1030 seq_printf(s
, "vendorid: 0x%04x\n", ri
->vendorid
);
1031 seq_printf(s
, "deviceid: 0x%04x\n", ri
->deviceid
);
1032 seq_printf(s
, "radiorev: %s\n", brcmu_dotrev_str(ri
->radiorev
, drev
));
1033 seq_printf(s
, "chipnum: %u (%x)\n", ri
->chipnum
, ri
->chipnum
);
1034 seq_printf(s
, "chiprev: %u\n", ri
->chiprev
);
1035 seq_printf(s
, "chippkg: %u\n", ri
->chippkg
);
1036 seq_printf(s
, "corerev: %u\n", ri
->corerev
);
1037 seq_printf(s
, "boardid: 0x%04x\n", ri
->boardid
);
1038 seq_printf(s
, "boardvendor: 0x%04x\n", ri
->boardvendor
);
1039 seq_printf(s
, "boardrev: %s\n", brcmu_boardrev_str(ri
->boardrev
, brev
));
1040 seq_printf(s
, "driverrev: %s\n", brcmu_dotrev_str(ri
->driverrev
, drev
));
1041 seq_printf(s
, "ucoderev: %u\n", ri
->ucoderev
);
1042 seq_printf(s
, "bus: %u\n", ri
->bus
);
1043 seq_printf(s
, "phytype: %u\n", ri
->phytype
);
1044 seq_printf(s
, "phyrev: %u\n", ri
->phyrev
);
1045 seq_printf(s
, "anarev: %u\n", ri
->anarev
);
1046 seq_printf(s
, "nvramrev: %08x\n", ri
->nvramrev
);
1048 seq_printf(s
, "clmver: %s\n", bus_if
->drvr
->clmver
);
1053 int brcmf_bus_started(struct device
*dev
)
1056 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1057 struct brcmf_pub
*drvr
= bus_if
->drvr
;
1058 struct brcmf_if
*ifp
;
1059 struct brcmf_if
*p2p_ifp
;
1061 brcmf_dbg(TRACE
, "\n");
1063 /* add primary networking interface */
1064 ifp
= brcmf_add_if(drvr
, 0, 0, false, "wlan%d", NULL
);
1066 return PTR_ERR(ifp
);
1070 /* signal bus ready */
1071 brcmf_bus_change_state(bus_if
, BRCMF_BUS_UP
);
1073 /* Bus is ready, do any initialization */
1074 ret
= brcmf_c_preinit_dcmds(ifp
);
1078 brcmf_debugfs_add_entry(drvr
, "revinfo", brcmf_revinfo_read
);
1080 /* assure we have chipid before feature attach */
1081 if (!bus_if
->chip
) {
1082 bus_if
->chip
= drvr
->revinfo
.chipnum
;
1083 bus_if
->chiprev
= drvr
->revinfo
.chiprev
;
1084 brcmf_dbg(INFO
, "firmware revinfo: chip %x (%d) rev %d\n",
1085 bus_if
->chip
, bus_if
->chip
, bus_if
->chiprev
);
1087 brcmf_feat_attach(drvr
);
1089 ret
= brcmf_proto_init_done(drvr
);
1093 brcmf_proto_add_if(drvr
, ifp
);
1095 drvr
->config
= brcmf_cfg80211_attach(drvr
, bus_if
->dev
,
1096 drvr
->settings
->p2p_enable
);
1097 if (drvr
->config
== NULL
) {
1102 ret
= brcmf_net_attach(ifp
, false);
1104 if ((!ret
) && (drvr
->settings
->p2p_enable
)) {
1105 p2p_ifp
= drvr
->iflist
[1];
1107 ret
= brcmf_net_p2p_attach(p2p_ifp
);
1114 drvr
->inetaddr_notifier
.notifier_call
= brcmf_inetaddr_changed
;
1115 ret
= register_inetaddr_notifier(&drvr
->inetaddr_notifier
);
1119 #if IS_ENABLED(CONFIG_IPV6)
1120 drvr
->inet6addr_notifier
.notifier_call
= brcmf_inet6addr_changed
;
1121 ret
= register_inet6addr_notifier(&drvr
->inet6addr_notifier
);
1123 unregister_inetaddr_notifier(&drvr
->inetaddr_notifier
);
1127 #endif /* CONFIG_INET */
1132 brcmf_err("failed: %d\n", ret
);
1134 brcmf_cfg80211_detach(drvr
->config
);
1135 drvr
->config
= NULL
;
1137 brcmf_net_detach(ifp
->ndev
, false);
1139 brcmf_net_detach(p2p_ifp
->ndev
, false);
1140 drvr
->iflist
[0] = NULL
;
1141 drvr
->iflist
[1] = NULL
;
1142 if (drvr
->settings
->ignore_probe_fail
)
1148 void brcmf_bus_add_txhdrlen(struct device
*dev
, uint len
)
1150 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1151 struct brcmf_pub
*drvr
= bus_if
->drvr
;
1154 drvr
->hdrlen
+= len
;
1158 void brcmf_dev_reset(struct device
*dev
)
1160 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1161 struct brcmf_pub
*drvr
= bus_if
->drvr
;
1166 if (drvr
->iflist
[0])
1167 brcmf_fil_cmd_int_set(drvr
->iflist
[0], BRCMF_C_TERMINATED
, 1);
1170 void brcmf_detach(struct device
*dev
)
1173 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1174 struct brcmf_pub
*drvr
= bus_if
->drvr
;
1176 brcmf_dbg(TRACE
, "Enter\n");
1182 unregister_inetaddr_notifier(&drvr
->inetaddr_notifier
);
1185 #if IS_ENABLED(CONFIG_IPV6)
1186 unregister_inet6addr_notifier(&drvr
->inet6addr_notifier
);
1189 /* stop firmware event handling */
1190 brcmf_fweh_detach(drvr
);
1192 brcmf_p2p_detach(&drvr
->config
->p2p
);
1194 brcmf_bus_change_state(bus_if
, BRCMF_BUS_DOWN
);
1196 /* make sure primary interface removed last */
1197 for (i
= BRCMF_MAX_IFS
-1; i
> -1; i
--)
1198 brcmf_remove_interface(drvr
->iflist
[i
], false);
1200 brcmf_cfg80211_detach(drvr
->config
);
1202 brcmf_bus_stop(drvr
->bus_if
);
1204 brcmf_proto_detach(drvr
);
1206 brcmf_debug_detach(drvr
);
1207 bus_if
->drvr
= NULL
;
1211 s32
brcmf_iovar_data_set(struct device
*dev
, char *name
, void *data
, u32 len
)
1213 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1214 struct brcmf_if
*ifp
= bus_if
->drvr
->iflist
[0];
1216 return brcmf_fil_iovar_data_set(ifp
, name
, data
, len
);
1219 static int brcmf_get_pend_8021x_cnt(struct brcmf_if
*ifp
)
1221 return atomic_read(&ifp
->pend_8021x_cnt
);
1224 int brcmf_netdev_wait_pend8021x(struct brcmf_if
*ifp
)
1228 err
= wait_event_timeout(ifp
->pend_8021x_wait
,
1229 !brcmf_get_pend_8021x_cnt(ifp
),
1230 MAX_WAIT_FOR_8021X_TX
);
1233 brcmf_err("Timed out waiting for no pending 802.1x packets\n");
1238 void brcmf_bus_change_state(struct brcmf_bus
*bus
, enum brcmf_bus_state state
)
1240 struct brcmf_pub
*drvr
= bus
->drvr
;
1241 struct net_device
*ndev
;
1244 brcmf_dbg(TRACE
, "%d -> %d\n", bus
->state
, state
);
1247 if (state
== BRCMF_BUS_UP
) {
1248 for (ifidx
= 0; ifidx
< BRCMF_MAX_IFS
; ifidx
++) {
1249 if ((drvr
->iflist
[ifidx
]) &&
1250 (drvr
->iflist
[ifidx
]->ndev
)) {
1251 ndev
= drvr
->iflist
[ifidx
]->ndev
;
1252 if (netif_queue_stopped(ndev
))
1253 netif_wake_queue(ndev
);
1259 static void brcmf_driver_register(struct work_struct
*work
)
1261 #ifdef CONFIG_BRCMFMAC_SDIO
1262 brcmf_sdio_register();
1264 #ifdef CONFIG_BRCMFMAC_USB
1265 brcmf_usb_register();
1267 #ifdef CONFIG_BRCMFMAC_PCIE
1268 brcmf_pcie_register();
1271 static DECLARE_WORK(brcmf_driver_work
, brcmf_driver_register
);
1273 int __init
brcmf_core_init(void)
1275 if (!schedule_work(&brcmf_driver_work
))
1281 void __exit
brcmf_core_exit(void)
1283 cancel_work_sync(&brcmf_driver_work
);
1285 #ifdef CONFIG_BRCMFMAC_SDIO
1288 #ifdef CONFIG_BRCMFMAC_USB
1291 #ifdef CONFIG_BRCMFMAC_PCIE