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>
24 #include <net/ieee80211_radiotap.h>
26 #include <brcmu_utils.h>
27 #include <brcmu_wifi.h>
32 #include "fwil_types.h"
42 #define MAX_WAIT_FOR_8021X_TX msecs_to_jiffies(950)
44 #define BRCMF_BSSIDX_INVALID -1
46 char *brcmf_ifname(struct brcmf_if
*ifp
)
52 return ifp
->ndev
->name
;
57 struct brcmf_if
*brcmf_get_ifp(struct brcmf_pub
*drvr
, int ifidx
)
62 if (ifidx
< 0 || ifidx
>= BRCMF_MAX_IFS
) {
63 brcmf_err("ifidx %d out of range\n", ifidx
);
68 bsscfgidx
= drvr
->if2bss
[ifidx
];
70 ifp
= drvr
->iflist
[bsscfgidx
];
75 void brcmf_configure_arp_nd_offload(struct brcmf_if
*ifp
, bool enable
)
81 mode
= BRCMF_ARP_OL_AGENT
| BRCMF_ARP_OL_PEER_AUTO_REPLY
;
85 /* Try to set and enable ARP offload feature, this may fail, then it */
86 /* is simply not supported and err 0 will be returned */
87 err
= brcmf_fil_iovar_int_set(ifp
, "arp_ol", mode
);
89 brcmf_dbg(TRACE
, "failed to set ARP offload mode to 0x%x, err = %d\n",
92 err
= brcmf_fil_iovar_int_set(ifp
, "arpoe", enable
);
94 brcmf_dbg(TRACE
, "failed to configure (%d) ARP offload err = %d\n",
97 brcmf_dbg(TRACE
, "successfully configured (%d) ARP offload to 0x%x\n",
102 err
= brcmf_fil_iovar_int_set(ifp
, "ndoe", enable
);
104 brcmf_dbg(TRACE
, "failed to configure (%d) ND offload err = %d\n",
107 brcmf_dbg(TRACE
, "successfully configured (%d) ND offload to 0x%x\n",
112 static void _brcmf_set_multicast_list(struct work_struct
*work
)
114 struct brcmf_if
*ifp
;
115 struct net_device
*ndev
;
116 struct netdev_hw_addr
*ha
;
123 ifp
= container_of(work
, struct brcmf_if
, multicast_work
);
125 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
129 /* Determine initial value of allmulti flag */
130 cmd_value
= (ndev
->flags
& IFF_ALLMULTI
) ? true : false;
132 /* Send down the multicast list first. */
133 cnt
= netdev_mc_count(ndev
);
134 buflen
= sizeof(cnt
) + (cnt
* ETH_ALEN
);
135 buf
= kmalloc(buflen
, GFP_ATOMIC
);
140 cnt_le
= cpu_to_le32(cnt
);
141 memcpy(bufp
, &cnt_le
, sizeof(cnt_le
));
142 bufp
+= sizeof(cnt_le
);
144 netdev_for_each_mc_addr(ha
, ndev
) {
147 memcpy(bufp
, ha
->addr
, ETH_ALEN
);
152 err
= brcmf_fil_iovar_data_set(ifp
, "mcast_list", buf
, buflen
);
154 brcmf_err("Setting mcast_list failed, %d\n", err
);
155 cmd_value
= cnt
? true : cmd_value
;
161 * Now send the allmulti setting. This is based on the setting in the
162 * net_device flags, but might be modified above to be turned on if we
163 * were trying to set some addresses and dongle rejected it...
165 err
= brcmf_fil_iovar_int_set(ifp
, "allmulti", cmd_value
);
167 brcmf_err("Setting allmulti failed, %d\n", err
);
169 /*Finally, pick up the PROMISC flag */
170 cmd_value
= (ndev
->flags
& IFF_PROMISC
) ? true : false;
171 err
= brcmf_fil_cmd_int_set(ifp
, BRCMF_C_SET_PROMISC
, cmd_value
);
173 brcmf_err("Setting BRCMF_C_SET_PROMISC failed, %d\n",
175 brcmf_configure_arp_nd_offload(ifp
, !cmd_value
);
178 #if IS_ENABLED(CONFIG_IPV6)
179 static void _brcmf_update_ndtable(struct work_struct
*work
)
181 struct brcmf_if
*ifp
;
184 ifp
= container_of(work
, struct brcmf_if
, ndoffload_work
);
186 /* clear the table in firmware */
187 ret
= brcmf_fil_iovar_data_set(ifp
, "nd_hostip_clear", NULL
, 0);
189 brcmf_dbg(TRACE
, "fail to clear nd ip table err:%d\n", ret
);
193 for (i
= 0; i
< ifp
->ipv6addr_idx
; i
++) {
194 ret
= brcmf_fil_iovar_data_set(ifp
, "nd_hostip",
195 &ifp
->ipv6_addr_tbl
[i
],
196 sizeof(struct in6_addr
));
198 brcmf_err("add nd ip err %d\n", ret
);
202 static void _brcmf_update_ndtable(struct work_struct
*work
)
207 static int brcmf_netdev_set_mac_address(struct net_device
*ndev
, void *addr
)
209 struct brcmf_if
*ifp
= netdev_priv(ndev
);
210 struct sockaddr
*sa
= (struct sockaddr
*)addr
;
213 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
215 err
= brcmf_fil_iovar_data_set(ifp
, "cur_etheraddr", sa
->sa_data
,
218 brcmf_err("Setting cur_etheraddr failed, %d\n", err
);
220 brcmf_dbg(TRACE
, "updated to %pM\n", sa
->sa_data
);
221 memcpy(ifp
->mac_addr
, sa
->sa_data
, ETH_ALEN
);
222 memcpy(ifp
->ndev
->dev_addr
, ifp
->mac_addr
, ETH_ALEN
);
227 static void brcmf_netdev_set_multicast_list(struct net_device
*ndev
)
229 struct brcmf_if
*ifp
= netdev_priv(ndev
);
231 schedule_work(&ifp
->multicast_work
);
235 * brcmf_skb_is_iapp - checks if skb is an IAPP packet
239 static bool brcmf_skb_is_iapp(struct sk_buff
*skb
)
241 static const u8 iapp_l2_update_packet
[6] __aligned(2) = {
242 0x00, 0x01, 0xaf, 0x81, 0x01, 0x00,
244 unsigned char *eth_data
;
245 #if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
249 if (skb
->len
- skb
->mac_len
!= 6 ||
250 !is_multicast_ether_addr(eth_hdr(skb
)->h_dest
))
253 eth_data
= skb_mac_header(skb
) + ETH_HLEN
;
254 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
255 return !(((*(const u32
*)eth_data
) ^ (*(const u32
*)iapp_l2_update_packet
)) |
256 ((*(const u16
*)(eth_data
+ 4)) ^ (*(const u16
*)(iapp_l2_update_packet
+ 4))));
258 a
= (const u16
*)eth_data
;
259 b
= (const u16
*)iapp_l2_update_packet
;
261 return !((a
[0] ^ b
[0]) | (a
[1] ^ b
[1]) | (a
[2] ^ b
[2]));
265 static netdev_tx_t
brcmf_netdev_start_xmit(struct sk_buff
*skb
,
266 struct net_device
*ndev
)
269 struct brcmf_if
*ifp
= netdev_priv(ndev
);
270 struct brcmf_pub
*drvr
= ifp
->drvr
;
274 brcmf_dbg(DATA
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
276 /* Can the device send data? */
277 if (drvr
->bus_if
->state
!= BRCMF_BUS_UP
) {
278 brcmf_err("xmit rejected state=%d\n", drvr
->bus_if
->state
);
279 netif_stop_queue(ndev
);
285 /* Some recent Broadcom's firmwares disassociate STA when they receive
286 * an 802.11f ADD frame. This behavior can lead to a local DoS security
287 * issue. Attacker may trigger disassociation of any STA by sending a
288 * proper Ethernet frame to the wireless interface.
290 * Moreover this feature may break AP interfaces in some specific
291 * setups. This applies e.g. to the bridge with hairpin mode enabled and
292 * IFLA_BRPORT_MCAST_TO_UCAST set. IAPP packet generated by a firmware
293 * will get passed back to the wireless interface and cause immediate
294 * disassociation of a just-connected STA.
296 if (!drvr
->settings
->iapp
&& brcmf_skb_is_iapp(skb
)) {
302 /* Make sure there's enough writeable headroom */
303 if (skb_headroom(skb
) < drvr
->hdrlen
|| skb_header_cloned(skb
)) {
304 head_delta
= max_t(int, drvr
->hdrlen
- skb_headroom(skb
), 0);
306 brcmf_dbg(INFO
, "%s: insufficient headroom (%d)\n",
307 brcmf_ifname(ifp
), head_delta
);
308 atomic_inc(&drvr
->bus_if
->stats
.pktcowed
);
309 ret
= pskb_expand_head(skb
, ALIGN(head_delta
, NET_SKB_PAD
), 0,
312 brcmf_err("%s: failed to expand headroom\n",
314 atomic_inc(&drvr
->bus_if
->stats
.pktcow_failed
);
319 /* validate length for ether packet */
320 if (skb
->len
< sizeof(*eh
)) {
326 eh
= (struct ethhdr
*)(skb
->data
);
328 if (eh
->h_proto
== htons(ETH_P_PAE
))
329 atomic_inc(&ifp
->pend_8021x_cnt
);
331 /* determine the priority */
332 if ((skb
->priority
== 0) || (skb
->priority
> 7))
333 skb
->priority
= cfg80211_classify8021d(skb
, NULL
);
335 ret
= brcmf_proto_tx_queue_data(drvr
, ifp
->ifidx
, skb
);
337 brcmf_txfinalize(ifp
, skb
, false);
341 ndev
->stats
.tx_dropped
++;
343 ndev
->stats
.tx_packets
++;
344 ndev
->stats
.tx_bytes
+= skb
->len
;
347 /* Return ok: we always eat the packet */
351 void brcmf_txflowblock_if(struct brcmf_if
*ifp
,
352 enum brcmf_netif_stop_reason reason
, bool state
)
356 if (!ifp
|| !ifp
->ndev
)
359 brcmf_dbg(TRACE
, "enter: bsscfgidx=%d stop=0x%X reason=%d state=%d\n",
360 ifp
->bsscfgidx
, ifp
->netif_stop
, reason
, state
);
362 spin_lock_irqsave(&ifp
->netif_stop_lock
, flags
);
364 if (!ifp
->netif_stop
)
365 netif_stop_queue(ifp
->ndev
);
366 ifp
->netif_stop
|= reason
;
368 ifp
->netif_stop
&= ~reason
;
369 if (!ifp
->netif_stop
)
370 netif_wake_queue(ifp
->ndev
);
372 spin_unlock_irqrestore(&ifp
->netif_stop_lock
, flags
);
375 void brcmf_netif_rx(struct brcmf_if
*ifp
, struct sk_buff
*skb
)
377 /* Most of Broadcom's firmwares send 802.11f ADD frame every time a new
378 * STA connects to the AP interface. This is an obsoleted standard most
379 * users don't use, so don't pass these frames up unless requested.
381 if (!ifp
->drvr
->settings
->iapp
&& brcmf_skb_is_iapp(skb
)) {
382 brcmu_pkt_buf_free_skb(skb
);
386 if (skb
->pkt_type
== PACKET_MULTICAST
)
387 ifp
->ndev
->stats
.multicast
++;
389 if (!(ifp
->ndev
->flags
& IFF_UP
)) {
390 brcmu_pkt_buf_free_skb(skb
);
394 ifp
->ndev
->stats
.rx_bytes
+= skb
->len
;
395 ifp
->ndev
->stats
.rx_packets
++;
397 brcmf_dbg(DATA
, "rx proto=0x%X\n", ntohs(skb
->protocol
));
401 /* If the receive is not processed inside an ISR,
402 * the softirqd must be woken explicitly to service
403 * the NET_RX_SOFTIRQ. This is handled by netif_rx_ni().
408 void brcmf_netif_mon_rx(struct brcmf_if
*ifp
, struct sk_buff
*skb
)
410 if (brcmf_feat_is_enabled(ifp
, BRCMF_FEAT_MONITOR_FMT_RADIOTAP
)) {
413 struct ieee80211_radiotap_header
*radiotap
;
415 /* TODO: use RX status to fill some radiotap data */
416 radiotap
= skb_push(skb
, sizeof(*radiotap
));
417 memset(radiotap
, 0, sizeof(*radiotap
));
418 radiotap
->it_len
= cpu_to_le16(sizeof(*radiotap
));
420 /* TODO: 4 bytes with receive status? */
424 skb
->dev
= ifp
->ndev
;
425 skb_reset_mac_header(skb
);
426 skb
->pkt_type
= PACKET_OTHERHOST
;
427 skb
->protocol
= htons(ETH_P_802_2
);
429 brcmf_netif_rx(ifp
, skb
);
432 static int brcmf_rx_hdrpull(struct brcmf_pub
*drvr
, struct sk_buff
*skb
,
433 struct brcmf_if
**ifp
)
437 /* process and remove protocol-specific header */
438 ret
= brcmf_proto_hdrpull(drvr
, true, skb
, ifp
);
440 if (ret
|| !(*ifp
) || !(*ifp
)->ndev
) {
441 if (ret
!= -ENODATA
&& *ifp
)
442 (*ifp
)->ndev
->stats
.rx_errors
++;
443 brcmu_pkt_buf_free_skb(skb
);
447 skb
->protocol
= eth_type_trans(skb
, (*ifp
)->ndev
);
451 void brcmf_rx_frame(struct device
*dev
, struct sk_buff
*skb
, bool handle_event
)
453 struct brcmf_if
*ifp
;
454 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
455 struct brcmf_pub
*drvr
= bus_if
->drvr
;
457 brcmf_dbg(DATA
, "Enter: %s: rxp=%p\n", dev_name(dev
), skb
);
459 if (brcmf_rx_hdrpull(drvr
, skb
, &ifp
))
462 if (brcmf_proto_is_reorder_skb(skb
)) {
463 brcmf_proto_rxreorder(ifp
, skb
);
465 /* Process special event packets */
467 brcmf_fweh_process_skb(ifp
->drvr
, skb
,
468 BCMILCP_SUBTYPE_VENDOR_LONG
);
470 brcmf_netif_rx(ifp
, skb
);
474 void brcmf_rx_event(struct device
*dev
, struct sk_buff
*skb
)
476 struct brcmf_if
*ifp
;
477 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
478 struct brcmf_pub
*drvr
= bus_if
->drvr
;
480 brcmf_dbg(EVENT
, "Enter: %s: rxp=%p\n", dev_name(dev
), skb
);
482 if (brcmf_rx_hdrpull(drvr
, skb
, &ifp
))
485 brcmf_fweh_process_skb(ifp
->drvr
, skb
, 0);
486 brcmu_pkt_buf_free_skb(skb
);
489 void brcmf_txfinalize(struct brcmf_if
*ifp
, struct sk_buff
*txp
, bool success
)
494 eh
= (struct ethhdr
*)(txp
->data
);
495 type
= ntohs(eh
->h_proto
);
497 if (type
== ETH_P_PAE
) {
498 atomic_dec(&ifp
->pend_8021x_cnt
);
499 if (waitqueue_active(&ifp
->pend_8021x_wait
))
500 wake_up(&ifp
->pend_8021x_wait
);
504 ifp
->ndev
->stats
.tx_errors
++;
506 brcmu_pkt_buf_free_skb(txp
);
509 static void brcmf_ethtool_get_drvinfo(struct net_device
*ndev
,
510 struct ethtool_drvinfo
*info
)
512 struct brcmf_if
*ifp
= netdev_priv(ndev
);
513 struct brcmf_pub
*drvr
= ifp
->drvr
;
514 char drev
[BRCMU_DOTREV_LEN
] = "n/a";
516 if (drvr
->revinfo
.result
== 0)
517 brcmu_dotrev_str(drvr
->revinfo
.driverrev
, drev
);
518 strlcpy(info
->driver
, KBUILD_MODNAME
, sizeof(info
->driver
));
519 strlcpy(info
->version
, drev
, sizeof(info
->version
));
520 strlcpy(info
->fw_version
, drvr
->fwver
, sizeof(info
->fw_version
));
521 strlcpy(info
->bus_info
, dev_name(drvr
->bus_if
->dev
),
522 sizeof(info
->bus_info
));
525 static const struct ethtool_ops brcmf_ethtool_ops
= {
526 .get_drvinfo
= brcmf_ethtool_get_drvinfo
,
529 static int brcmf_netdev_stop(struct net_device
*ndev
)
531 struct brcmf_if
*ifp
= netdev_priv(ndev
);
533 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
535 brcmf_cfg80211_down(ndev
);
537 brcmf_fil_iovar_data_set(ifp
, "arp_hostip_clear", NULL
, 0);
539 brcmf_net_setcarrier(ifp
, false);
544 static int brcmf_netdev_open(struct net_device
*ndev
)
546 struct brcmf_if
*ifp
= netdev_priv(ndev
);
547 struct brcmf_pub
*drvr
= ifp
->drvr
;
548 struct brcmf_bus
*bus_if
= drvr
->bus_if
;
551 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d\n", ifp
->bsscfgidx
);
553 /* If bus is not ready, can't continue */
554 if (bus_if
->state
!= BRCMF_BUS_UP
) {
555 brcmf_err("failed bus is not ready\n");
559 atomic_set(&ifp
->pend_8021x_cnt
, 0);
561 /* Get current TOE mode from dongle */
562 if (brcmf_fil_iovar_int_get(ifp
, "toe_ol", &toe_ol
) >= 0
563 && (toe_ol
& TOE_TX_CSUM_OL
) != 0)
564 ndev
->features
|= NETIF_F_IP_CSUM
;
566 ndev
->features
&= ~NETIF_F_IP_CSUM
;
568 if (brcmf_cfg80211_up(ndev
)) {
569 brcmf_err("failed to bring up cfg80211\n");
573 /* Clear, carrier, set when connected or AP mode. */
574 netif_carrier_off(ndev
);
578 static const struct net_device_ops brcmf_netdev_ops_pri
= {
579 .ndo_open
= brcmf_netdev_open
,
580 .ndo_stop
= brcmf_netdev_stop
,
581 .ndo_start_xmit
= brcmf_netdev_start_xmit
,
582 .ndo_set_mac_address
= brcmf_netdev_set_mac_address
,
583 .ndo_set_rx_mode
= brcmf_netdev_set_multicast_list
586 int brcmf_net_attach(struct brcmf_if
*ifp
, bool rtnl_locked
)
588 struct brcmf_pub
*drvr
= ifp
->drvr
;
589 struct net_device
*ndev
;
592 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d mac=%pM\n", ifp
->bsscfgidx
,
596 /* set appropriate operations */
597 ndev
->netdev_ops
= &brcmf_netdev_ops_pri
;
599 ndev
->needed_headroom
+= drvr
->hdrlen
;
600 ndev
->ethtool_ops
= &brcmf_ethtool_ops
;
602 /* set the mac address & netns */
603 memcpy(ndev
->dev_addr
, ifp
->mac_addr
, ETH_ALEN
);
604 dev_net_set(ndev
, wiphy_net(cfg_to_wiphy(drvr
->config
)));
606 INIT_WORK(&ifp
->multicast_work
, _brcmf_set_multicast_list
);
607 INIT_WORK(&ifp
->ndoffload_work
, _brcmf_update_ndtable
);
610 err
= register_netdevice(ndev
);
612 err
= register_netdev(ndev
);
614 brcmf_err("couldn't register the net device\n");
618 ndev
->priv_destructor
= brcmf_cfg80211_free_netdev
;
619 brcmf_dbg(INFO
, "%s: Broadcom Dongle Host Driver\n", ndev
->name
);
623 drvr
->iflist
[ifp
->bsscfgidx
] = NULL
;
624 ndev
->netdev_ops
= NULL
;
628 static void brcmf_net_detach(struct net_device
*ndev
, bool rtnl_locked
)
630 if (ndev
->reg_state
== NETREG_REGISTERED
) {
632 unregister_netdevice(ndev
);
634 unregister_netdev(ndev
);
636 brcmf_cfg80211_free_netdev(ndev
);
641 void brcmf_net_setcarrier(struct brcmf_if
*ifp
, bool on
)
643 struct net_device
*ndev
;
645 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d carrier=%d\n", ifp
->bsscfgidx
,
649 brcmf_txflowblock_if(ifp
, BRCMF_NETIF_STOP_REASON_DISCONNECTED
, !on
);
651 if (!netif_carrier_ok(ndev
))
652 netif_carrier_on(ndev
);
655 if (netif_carrier_ok(ndev
))
656 netif_carrier_off(ndev
);
660 static int brcmf_net_p2p_open(struct net_device
*ndev
)
662 brcmf_dbg(TRACE
, "Enter\n");
664 return brcmf_cfg80211_up(ndev
);
667 static int brcmf_net_p2p_stop(struct net_device
*ndev
)
669 brcmf_dbg(TRACE
, "Enter\n");
671 return brcmf_cfg80211_down(ndev
);
674 static netdev_tx_t
brcmf_net_p2p_start_xmit(struct sk_buff
*skb
,
675 struct net_device
*ndev
)
678 dev_kfree_skb_any(skb
);
683 static const struct net_device_ops brcmf_netdev_ops_p2p
= {
684 .ndo_open
= brcmf_net_p2p_open
,
685 .ndo_stop
= brcmf_net_p2p_stop
,
686 .ndo_start_xmit
= brcmf_net_p2p_start_xmit
689 static int brcmf_net_p2p_attach(struct brcmf_if
*ifp
)
691 struct net_device
*ndev
;
693 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d mac=%pM\n", ifp
->bsscfgidx
,
697 ndev
->netdev_ops
= &brcmf_netdev_ops_p2p
;
699 /* set the mac address */
700 memcpy(ndev
->dev_addr
, ifp
->mac_addr
, ETH_ALEN
);
702 if (register_netdev(ndev
) != 0) {
703 brcmf_err("couldn't register the p2p net device\n");
707 brcmf_dbg(INFO
, "%s: Broadcom Dongle Host Driver\n", ndev
->name
);
712 ifp
->drvr
->iflist
[ifp
->bsscfgidx
] = NULL
;
713 ndev
->netdev_ops
= NULL
;
717 struct brcmf_if
*brcmf_add_if(struct brcmf_pub
*drvr
, s32 bsscfgidx
, s32 ifidx
,
718 bool is_p2pdev
, const char *name
, u8
*mac_addr
)
720 struct brcmf_if
*ifp
;
721 struct net_device
*ndev
;
723 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx
, ifidx
);
725 ifp
= drvr
->iflist
[bsscfgidx
];
727 * Delete the existing interface before overwriting it
728 * in case we missed the BRCMF_E_IF_DEL event.
732 brcmf_err("ERROR: netdev:%s already exists\n",
734 netif_stop_queue(ifp
->ndev
);
735 brcmf_net_detach(ifp
->ndev
, false);
736 drvr
->iflist
[bsscfgidx
] = NULL
;
738 brcmf_dbg(INFO
, "netdev:%s ignore IF event\n",
740 return ERR_PTR(-EINVAL
);
744 if (!drvr
->settings
->p2p_enable
&& is_p2pdev
) {
745 /* this is P2P_DEVICE interface */
746 brcmf_dbg(INFO
, "allocate non-netdev interface\n");
747 ifp
= kzalloc(sizeof(*ifp
), GFP_KERNEL
);
749 return ERR_PTR(-ENOMEM
);
751 brcmf_dbg(INFO
, "allocate netdev interface\n");
752 /* Allocate netdev, including space for private structure */
753 ndev
= alloc_netdev(sizeof(*ifp
), is_p2pdev
? "p2p%d" : name
,
754 NET_NAME_UNKNOWN
, ether_setup
);
756 return ERR_PTR(-ENOMEM
);
758 ndev
->needs_free_netdev
= true;
759 ifp
= netdev_priv(ndev
);
761 /* store mapping ifidx to bsscfgidx */
762 if (drvr
->if2bss
[ifidx
] == BRCMF_BSSIDX_INVALID
)
763 drvr
->if2bss
[ifidx
] = bsscfgidx
;
767 drvr
->iflist
[bsscfgidx
] = ifp
;
769 ifp
->bsscfgidx
= bsscfgidx
;
771 init_waitqueue_head(&ifp
->pend_8021x_wait
);
772 spin_lock_init(&ifp
->netif_stop_lock
);
774 if (mac_addr
!= NULL
)
775 memcpy(ifp
->mac_addr
, mac_addr
, ETH_ALEN
);
777 brcmf_dbg(TRACE
, " ==== pid:%x, if:%s (%pM) created ===\n",
778 current
->pid
, name
, ifp
->mac_addr
);
783 static void brcmf_del_if(struct brcmf_pub
*drvr
, s32 bsscfgidx
,
786 struct brcmf_if
*ifp
;
789 ifp
= drvr
->iflist
[bsscfgidx
];
791 brcmf_err("Null interface, bsscfgidx=%d\n", bsscfgidx
);
794 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx
,
799 if (bsscfgidx
== 0) {
800 if (ifp
->ndev
->netdev_ops
== &brcmf_netdev_ops_pri
) {
802 brcmf_netdev_stop(ifp
->ndev
);
806 netif_stop_queue(ifp
->ndev
);
809 if (ifp
->ndev
->netdev_ops
== &brcmf_netdev_ops_pri
) {
810 cancel_work_sync(&ifp
->multicast_work
);
811 cancel_work_sync(&ifp
->ndoffload_work
);
813 brcmf_net_detach(ifp
->ndev
, rtnl_locked
);
815 /* Only p2p device interfaces which get dynamically created
816 * end up here. In this case the p2p module should be informed
817 * about the removal of the interface within the firmware. If
818 * not then p2p commands towards the firmware will cause some
819 * serious troublesome side effects. The p2p module will clean
820 * up the ifp if needed.
822 brcmf_p2p_ifp_removed(ifp
, rtnl_locked
);
826 drvr
->iflist
[bsscfgidx
] = NULL
;
827 if (drvr
->if2bss
[ifidx
] == bsscfgidx
)
828 drvr
->if2bss
[ifidx
] = BRCMF_BSSIDX_INVALID
;
831 void brcmf_remove_interface(struct brcmf_if
*ifp
, bool rtnl_locked
)
833 if (!ifp
|| WARN_ON(ifp
->drvr
->iflist
[ifp
->bsscfgidx
] != ifp
))
835 brcmf_dbg(TRACE
, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp
->bsscfgidx
,
837 brcmf_proto_del_if(ifp
->drvr
, ifp
);
838 brcmf_del_if(ifp
->drvr
, ifp
->bsscfgidx
, rtnl_locked
);
841 static int brcmf_psm_watchdog_notify(struct brcmf_if
*ifp
,
842 const struct brcmf_event_msg
*evtmsg
,
847 brcmf_dbg(TRACE
, "enter: bsscfgidx=%d\n", ifp
->bsscfgidx
);
849 brcmf_err("PSM's watchdog has fired!\n");
851 err
= brcmf_debug_create_memdump(ifp
->drvr
->bus_if
, data
,
854 brcmf_err("Failed to get memory dump, %d\n", err
);
860 #define ARPOL_MAX_ENTRIES 8
861 static int brcmf_inetaddr_changed(struct notifier_block
*nb
,
862 unsigned long action
, void *data
)
864 struct brcmf_pub
*drvr
= container_of(nb
, struct brcmf_pub
,
866 struct in_ifaddr
*ifa
= data
;
867 struct net_device
*ndev
= ifa
->ifa_dev
->dev
;
868 struct brcmf_if
*ifp
;
871 __be32 addr_table
[ARPOL_MAX_ENTRIES
] = {0};
873 /* Find out if the notification is meant for us */
874 for (idx
= 0; idx
< BRCMF_MAX_IFS
; idx
++) {
875 ifp
= drvr
->iflist
[idx
];
876 if (ifp
&& ifp
->ndev
== ndev
)
878 if (idx
== BRCMF_MAX_IFS
- 1)
882 /* check if arp offload is supported */
883 ret
= brcmf_fil_iovar_int_get(ifp
, "arpoe", &val
);
887 /* old version only support primary index */
888 ret
= brcmf_fil_iovar_int_get(ifp
, "arp_version", &val
);
892 ifp
= drvr
->iflist
[0];
894 /* retrieve the table from firmware */
895 ret
= brcmf_fil_iovar_data_get(ifp
, "arp_hostip", addr_table
,
898 brcmf_err("fail to get arp ip table err:%d\n", ret
);
902 for (i
= 0; i
< ARPOL_MAX_ENTRIES
; i
++)
903 if (ifa
->ifa_address
== addr_table
[i
])
908 if (i
== ARPOL_MAX_ENTRIES
) {
909 brcmf_dbg(TRACE
, "add %pI4 to arp table\n",
911 /* set it directly */
912 ret
= brcmf_fil_iovar_data_set(ifp
, "arp_hostip",
913 &ifa
->ifa_address
, sizeof(ifa
->ifa_address
));
915 brcmf_err("add arp ip err %d\n", ret
);
919 if (i
< ARPOL_MAX_ENTRIES
) {
921 brcmf_dbg(TRACE
, "remove %pI4 from arp table\n",
923 /* clear the table in firmware */
924 ret
= brcmf_fil_iovar_data_set(ifp
, "arp_hostip_clear",
927 brcmf_err("fail to clear arp ip table err:%d\n",
931 for (i
= 0; i
< ARPOL_MAX_ENTRIES
; i
++) {
932 if (addr_table
[i
] == 0)
934 ret
= brcmf_fil_iovar_data_set(ifp
, "arp_hostip",
936 sizeof(addr_table
[i
]));
938 brcmf_err("add arp ip err %d\n",
951 #if IS_ENABLED(CONFIG_IPV6)
952 static int brcmf_inet6addr_changed(struct notifier_block
*nb
,
953 unsigned long action
, void *data
)
955 struct brcmf_pub
*drvr
= container_of(nb
, struct brcmf_pub
,
957 struct inet6_ifaddr
*ifa
= data
;
958 struct brcmf_if
*ifp
;
960 struct in6_addr
*table
;
962 /* Only handle primary interface */
963 ifp
= drvr
->iflist
[0];
966 if (ifp
->ndev
!= ifa
->idev
->dev
)
969 table
= ifp
->ipv6_addr_tbl
;
970 for (i
= 0; i
< NDOL_MAX_ENTRIES
; i
++)
971 if (ipv6_addr_equal(&ifa
->addr
, &table
[i
]))
976 if (i
== NDOL_MAX_ENTRIES
) {
977 if (ifp
->ipv6addr_idx
< NDOL_MAX_ENTRIES
) {
978 table
[ifp
->ipv6addr_idx
++] = ifa
->addr
;
980 for (i
= 0; i
< NDOL_MAX_ENTRIES
- 1; i
++)
981 table
[i
] = table
[i
+ 1];
982 table
[NDOL_MAX_ENTRIES
- 1] = ifa
->addr
;
987 if (i
< NDOL_MAX_ENTRIES
) {
988 for (; i
< ifp
->ipv6addr_idx
- 1; i
++)
989 table
[i
] = table
[i
+ 1];
990 memset(&table
[i
], 0, sizeof(table
[i
]));
998 schedule_work(&ifp
->ndoffload_work
);
1004 static int brcmf_revinfo_read(struct seq_file
*s
, void *data
)
1006 struct brcmf_bus
*bus_if
= dev_get_drvdata(s
->private);
1007 struct brcmf_rev_info
*ri
= &bus_if
->drvr
->revinfo
;
1008 char drev
[BRCMU_DOTREV_LEN
];
1009 char brev
[BRCMU_BOARDREV_LEN
];
1011 seq_printf(s
, "vendorid: 0x%04x\n", ri
->vendorid
);
1012 seq_printf(s
, "deviceid: 0x%04x\n", ri
->deviceid
);
1013 seq_printf(s
, "radiorev: %s\n", brcmu_dotrev_str(ri
->radiorev
, drev
));
1014 seq_printf(s
, "chip: %s\n", ri
->chipname
);
1015 seq_printf(s
, "chippkg: %u\n", ri
->chippkg
);
1016 seq_printf(s
, "corerev: %u\n", ri
->corerev
);
1017 seq_printf(s
, "boardid: 0x%04x\n", ri
->boardid
);
1018 seq_printf(s
, "boardvendor: 0x%04x\n", ri
->boardvendor
);
1019 seq_printf(s
, "boardrev: %s\n", brcmu_boardrev_str(ri
->boardrev
, brev
));
1020 seq_printf(s
, "driverrev: %s\n", brcmu_dotrev_str(ri
->driverrev
, drev
));
1021 seq_printf(s
, "ucoderev: %u\n", ri
->ucoderev
);
1022 seq_printf(s
, "bus: %u\n", ri
->bus
);
1023 seq_printf(s
, "phytype: %u\n", ri
->phytype
);
1024 seq_printf(s
, "phyrev: %u\n", ri
->phyrev
);
1025 seq_printf(s
, "anarev: %u\n", ri
->anarev
);
1026 seq_printf(s
, "nvramrev: %08x\n", ri
->nvramrev
);
1028 seq_printf(s
, "clmver: %s\n", bus_if
->drvr
->clmver
);
1033 static int brcmf_bus_started(struct brcmf_pub
*drvr
, struct cfg80211_ops
*ops
)
1036 struct brcmf_bus
*bus_if
= drvr
->bus_if
;
1037 struct brcmf_if
*ifp
;
1038 struct brcmf_if
*p2p_ifp
;
1040 brcmf_dbg(TRACE
, "\n");
1042 /* add primary networking interface */
1043 ifp
= brcmf_add_if(drvr
, 0, 0, false, "wlan%d", NULL
);
1045 return PTR_ERR(ifp
);
1049 /* signal bus ready */
1050 brcmf_bus_change_state(bus_if
, BRCMF_BUS_UP
);
1052 /* do bus specific preinit here */
1053 ret
= brcmf_bus_preinit(bus_if
);
1057 /* Bus is ready, do any initialization */
1058 ret
= brcmf_c_preinit_dcmds(ifp
);
1062 brcmf_feat_attach(drvr
);
1064 ret
= brcmf_proto_init_done(drvr
);
1068 brcmf_proto_add_if(drvr
, ifp
);
1070 drvr
->config
= brcmf_cfg80211_attach(drvr
, ops
,
1071 drvr
->settings
->p2p_enable
);
1072 if (drvr
->config
== NULL
) {
1077 ret
= brcmf_net_attach(ifp
, false);
1079 if ((!ret
) && (drvr
->settings
->p2p_enable
)) {
1080 p2p_ifp
= drvr
->iflist
[1];
1082 ret
= brcmf_net_p2p_attach(p2p_ifp
);
1089 drvr
->inetaddr_notifier
.notifier_call
= brcmf_inetaddr_changed
;
1090 ret
= register_inetaddr_notifier(&drvr
->inetaddr_notifier
);
1094 #if IS_ENABLED(CONFIG_IPV6)
1095 drvr
->inet6addr_notifier
.notifier_call
= brcmf_inet6addr_changed
;
1096 ret
= register_inet6addr_notifier(&drvr
->inet6addr_notifier
);
1098 unregister_inetaddr_notifier(&drvr
->inetaddr_notifier
);
1102 #endif /* CONFIG_INET */
1104 /* populate debugfs */
1105 brcmf_debugfs_add_entry(drvr
, "revinfo", brcmf_revinfo_read
);
1106 brcmf_feat_debugfs_create(drvr
);
1107 brcmf_proto_debugfs_create(drvr
);
1108 brcmf_bus_debugfs_create(bus_if
);
1113 brcmf_err("failed: %d\n", ret
);
1115 brcmf_cfg80211_detach(drvr
->config
);
1116 drvr
->config
= NULL
;
1118 brcmf_net_detach(ifp
->ndev
, false);
1120 brcmf_net_detach(p2p_ifp
->ndev
, false);
1121 drvr
->iflist
[0] = NULL
;
1122 drvr
->iflist
[1] = NULL
;
1123 if (drvr
->settings
->ignore_probe_fail
)
1129 int brcmf_attach(struct device
*dev
, struct brcmf_mp_device
*settings
)
1131 struct wiphy
*wiphy
;
1132 struct cfg80211_ops
*ops
;
1133 struct brcmf_pub
*drvr
= NULL
;
1137 brcmf_dbg(TRACE
, "Enter\n");
1139 ops
= brcmf_cfg80211_get_ops(settings
);
1143 wiphy
= wiphy_new(ops
, sizeof(*drvr
));
1147 set_wiphy_dev(wiphy
, dev
);
1148 drvr
= wiphy_priv(wiphy
);
1149 drvr
->wiphy
= wiphy
;
1151 for (i
= 0; i
< ARRAY_SIZE(drvr
->if2bss
); i
++)
1152 drvr
->if2bss
[i
] = BRCMF_BSSIDX_INVALID
;
1154 mutex_init(&drvr
->proto_block
);
1156 /* Link to bus module */
1158 drvr
->bus_if
= dev_get_drvdata(dev
);
1159 drvr
->bus_if
->drvr
= drvr
;
1160 drvr
->settings
= settings
;
1162 /* Attach and link in the protocol */
1163 ret
= brcmf_proto_attach(drvr
);
1165 brcmf_err("brcmf_prot_attach failed\n");
1169 /* Attach to events important for core code */
1170 brcmf_fweh_register(drvr
, BRCMF_E_PSM_WATCHDOG
,
1171 brcmf_psm_watchdog_notify
);
1173 /* attach firmware event handler */
1174 brcmf_fweh_attach(drvr
);
1176 ret
= brcmf_bus_started(drvr
, ops
);
1178 brcmf_err("dongle is not responding: err=%d\n", ret
);
1182 drvr
->config
->ops
= ops
;
1192 void brcmf_bus_add_txhdrlen(struct device
*dev
, uint len
)
1194 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1195 struct brcmf_pub
*drvr
= bus_if
->drvr
;
1198 drvr
->hdrlen
+= len
;
1202 void brcmf_dev_reset(struct device
*dev
)
1204 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1205 struct brcmf_pub
*drvr
= bus_if
->drvr
;
1210 if (drvr
->iflist
[0])
1211 brcmf_fil_cmd_int_set(drvr
->iflist
[0], BRCMF_C_TERMINATED
, 1);
1214 void brcmf_dev_coredump(struct device
*dev
)
1216 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1218 if (brcmf_debug_create_memdump(bus_if
, NULL
, 0) < 0)
1219 brcmf_dbg(TRACE
, "failed to create coredump\n");
1222 void brcmf_detach(struct device
*dev
)
1225 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1226 struct brcmf_pub
*drvr
= bus_if
->drvr
;
1228 brcmf_dbg(TRACE
, "Enter\n");
1234 unregister_inetaddr_notifier(&drvr
->inetaddr_notifier
);
1237 #if IS_ENABLED(CONFIG_IPV6)
1238 unregister_inet6addr_notifier(&drvr
->inet6addr_notifier
);
1241 /* stop firmware event handling */
1242 brcmf_fweh_detach(drvr
);
1244 brcmf_p2p_detach(&drvr
->config
->p2p
);
1246 brcmf_bus_change_state(bus_if
, BRCMF_BUS_DOWN
);
1248 brcmf_proto_detach_pre_delif(drvr
);
1251 brcmf_net_detach(drvr
->mon_if
->ndev
, false);
1252 drvr
->mon_if
= NULL
;
1255 /* make sure primary interface removed last */
1256 for (i
= BRCMF_MAX_IFS
-1; i
> -1; i
--)
1257 brcmf_remove_interface(drvr
->iflist
[i
], false);
1259 brcmf_cfg80211_detach(drvr
->config
);
1260 drvr
->config
= NULL
;
1262 brcmf_bus_stop(drvr
->bus_if
);
1264 brcmf_proto_detach_post_delif(drvr
);
1266 bus_if
->drvr
= NULL
;
1267 wiphy_free(drvr
->wiphy
);
1270 s32
brcmf_iovar_data_set(struct device
*dev
, char *name
, void *data
, u32 len
)
1272 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1273 struct brcmf_if
*ifp
= bus_if
->drvr
->iflist
[0];
1275 return brcmf_fil_iovar_data_set(ifp
, name
, data
, len
);
1278 static int brcmf_get_pend_8021x_cnt(struct brcmf_if
*ifp
)
1280 return atomic_read(&ifp
->pend_8021x_cnt
);
1283 int brcmf_netdev_wait_pend8021x(struct brcmf_if
*ifp
)
1287 err
= wait_event_timeout(ifp
->pend_8021x_wait
,
1288 !brcmf_get_pend_8021x_cnt(ifp
),
1289 MAX_WAIT_FOR_8021X_TX
);
1292 brcmf_err("Timed out waiting for no pending 802.1x packets\n");
1297 void brcmf_bus_change_state(struct brcmf_bus
*bus
, enum brcmf_bus_state state
)
1299 struct brcmf_pub
*drvr
= bus
->drvr
;
1300 struct net_device
*ndev
;
1303 brcmf_dbg(TRACE
, "%d -> %d\n", bus
->state
, state
);
1306 brcmf_dbg(INFO
, "ignoring transition, bus not attached yet\n");
1312 if (state
== BRCMF_BUS_UP
) {
1313 for (ifidx
= 0; ifidx
< BRCMF_MAX_IFS
; ifidx
++) {
1314 if ((drvr
->iflist
[ifidx
]) &&
1315 (drvr
->iflist
[ifidx
]->ndev
)) {
1316 ndev
= drvr
->iflist
[ifidx
]->ndev
;
1317 if (netif_queue_stopped(ndev
))
1318 netif_wake_queue(ndev
);
1324 static void brcmf_driver_register(struct work_struct
*work
)
1326 #ifdef CONFIG_BRCMFMAC_SDIO
1327 brcmf_sdio_register();
1329 #ifdef CONFIG_BRCMFMAC_USB
1330 brcmf_usb_register();
1332 #ifdef CONFIG_BRCMFMAC_PCIE
1333 brcmf_pcie_register();
1336 static DECLARE_WORK(brcmf_driver_work
, brcmf_driver_register
);
1338 int __init
brcmf_core_init(void)
1340 if (!schedule_work(&brcmf_driver_work
))
1346 void __exit
brcmf_core_exit(void)
1348 cancel_work_sync(&brcmf_driver_work
);
1350 #ifdef CONFIG_BRCMFMAC_SDIO
1353 #ifdef CONFIG_BRCMFMAC_USB
1356 #ifdef CONFIG_BRCMFMAC_PCIE