Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / core.c
blob44b7774ebd025d960edc7b8e03321ae3cd08f4ee
1 /*
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/ipv6.h>
25 #include <brcmu_utils.h>
26 #include <brcmu_wifi.h>
28 #include "core.h"
29 #include "bus.h"
30 #include "debug.h"
31 #include "fwil_types.h"
32 #include "p2p.h"
33 #include "pno.h"
34 #include "cfg80211.h"
35 #include "fwil.h"
36 #include "feature.h"
37 #include "proto.h"
38 #include "pcie.h"
39 #include "common.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)
47 if (!ifp)
48 return "<if_null>";
50 if (ifp->ndev)
51 return ifp->ndev->name;
53 return "<if_none>";
56 struct brcmf_if *brcmf_get_ifp(struct brcmf_pub *drvr, int ifidx)
58 struct brcmf_if *ifp;
59 s32 bsscfgidx;
61 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
62 brcmf_err("ifidx %d out of range\n", ifidx);
63 return NULL;
66 ifp = NULL;
67 bsscfgidx = drvr->if2bss[ifidx];
68 if (bsscfgidx >= 0)
69 ifp = drvr->iflist[bsscfgidx];
71 return ifp;
74 void brcmf_configure_arp_nd_offload(struct brcmf_if *ifp, bool enable)
76 s32 err;
77 u32 mode;
79 if (enable)
80 mode = BRCMF_ARP_OL_AGENT | BRCMF_ARP_OL_PEER_AUTO_REPLY;
81 else
82 mode = 0;
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);
87 if (err) {
88 brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, err = %d\n",
89 mode, err);
90 } else {
91 err = brcmf_fil_iovar_int_set(ifp, "arpoe", enable);
92 if (err) {
93 brcmf_dbg(TRACE, "failed to configure (%d) ARP offload err = %d\n",
94 enable, err);
95 } else {
96 brcmf_dbg(TRACE, "successfully configured (%d) ARP offload to 0x%x\n",
97 enable, mode);
101 err = brcmf_fil_iovar_int_set(ifp, "ndoe", enable);
102 if (err) {
103 brcmf_dbg(TRACE, "failed to configure (%d) ND offload err = %d\n",
104 enable, err);
105 } else {
106 brcmf_dbg(TRACE, "successfully configured (%d) ND offload to 0x%x\n",
107 enable, mode);
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;
116 u32 cmd_value, cnt;
117 __le32 cnt_le;
118 char *buf, *bufp;
119 u32 buflen;
120 s32 err;
122 ifp = container_of(work, struct brcmf_if, multicast_work);
124 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
126 ndev = ifp->ndev;
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);
135 if (!buf)
136 return;
137 bufp = buf;
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) {
144 if (!cnt)
145 break;
146 memcpy(bufp, ha->addr, ETH_ALEN);
147 bufp += ETH_ALEN;
148 cnt--;
151 err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
152 if (err < 0) {
153 brcmf_err("Setting mcast_list failed, %d\n", err);
154 cmd_value = cnt ? true : cmd_value;
157 kfree(buf);
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);
165 if (err < 0)
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);
171 if (err < 0)
172 brcmf_err("Setting BRCMF_C_SET_PROMISC failed, %d\n",
173 err);
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;
181 int i, ret;
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);
187 if (ret) {
188 brcmf_dbg(TRACE, "fail to clear nd ip table err:%d\n", ret);
189 return;
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));
196 if (ret)
197 brcmf_err("add nd ip err %d\n", ret);
200 #else
201 static void _brcmf_update_ndtable(struct work_struct *work)
204 #endif
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;
210 int err;
212 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
214 err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", sa->sa_data,
215 ETH_ALEN);
216 if (err < 0) {
217 brcmf_err("Setting cur_etheraddr failed, %d\n", err);
218 } else {
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);
223 return err;
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
236 * @skb: skb to check
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)
245 const u16 *a, *b;
246 #endif
248 if (skb->len - skb->mac_len != 6 ||
249 !is_multicast_ether_addr(eth_hdr(skb)->h_dest))
250 return false;
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))));
256 #else
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]));
261 #endif
264 static netdev_tx_t brcmf_netdev_start_xmit(struct sk_buff *skb,
265 struct net_device *ndev)
267 int ret;
268 struct brcmf_if *ifp = netdev_priv(ndev);
269 struct brcmf_pub *drvr = ifp->drvr;
270 struct ethhdr *eh;
271 int head_delta;
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);
279 dev_kfree_skb(skb);
280 ret = -ENODEV;
281 goto done;
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)) {
296 dev_kfree_skb(skb);
297 ret = -EINVAL;
298 goto done;
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,
309 GFP_ATOMIC);
310 if (ret < 0) {
311 brcmf_err("%s: failed to expand headroom\n",
312 brcmf_ifname(ifp));
313 atomic_inc(&drvr->bus_if->stats.pktcow_failed);
314 goto done;
318 /* validate length for ether packet */
319 if (skb->len < sizeof(*eh)) {
320 ret = -EINVAL;
321 dev_kfree_skb(skb);
322 goto done;
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);
335 if (ret < 0)
336 brcmf_txfinalize(ifp, skb, false);
338 done:
339 if (ret) {
340 ndev->stats.tx_dropped++;
341 } else {
342 ndev->stats.tx_packets++;
343 ndev->stats.tx_bytes += skb->len;
346 /* Return ok: we always eat the packet */
347 return NETDEV_TX_OK;
350 void brcmf_txflowblock_if(struct brcmf_if *ifp,
351 enum brcmf_netif_stop_reason reason, bool state)
353 unsigned long flags;
355 if (!ifp || !ifp->ndev)
356 return;
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);
362 if (state) {
363 if (!ifp->netif_stop)
364 netif_stop_queue(ifp->ndev);
365 ifp->netif_stop |= reason;
366 } else {
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);
382 return;
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);
390 return;
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));
397 if (in_interrupt())
398 netif_rx(skb);
399 else
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().
404 netif_rx_ni(skb);
407 static int brcmf_rx_hdrpull(struct brcmf_pub *drvr, struct sk_buff *skb,
408 struct brcmf_if **ifp)
410 int ret;
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);
419 return -ENODATA;
422 skb->protocol = eth_type_trans(skb, (*ifp)->ndev);
423 return 0;
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))
435 return;
437 if (brcmf_proto_is_reorder_skb(skb)) {
438 brcmf_proto_rxreorder(ifp, skb);
439 } else {
440 /* Process special event packets */
441 if (handle_event)
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))
457 return;
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)
465 struct ethhdr *eh;
466 u16 type;
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);
477 if (!success)
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);
515 return 0;
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;
523 u32 toe_ol;
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");
530 return -EAGAIN;
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;
539 else
540 ndev->features &= ~NETIF_F_IP_CSUM;
542 if (brcmf_cfg80211_up(ndev)) {
543 brcmf_err("failed to bring up cfg80211\n");
544 return -EIO;
547 /* Clear, carrier, set when connected or AP mode. */
548 netif_carrier_off(ndev);
549 return 0;
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;
564 s32 err;
566 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
567 ifp->mac_addr);
568 ndev = ifp->ndev;
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);
583 if (rtnl_locked)
584 err = register_netdevice(ndev);
585 else
586 err = register_netdev(ndev);
587 if (err != 0) {
588 brcmf_err("couldn't register the net device\n");
589 goto fail;
592 ndev->priv_destructor = brcmf_cfg80211_free_netdev;
593 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
594 return 0;
596 fail:
597 drvr->iflist[ifp->bsscfgidx] = NULL;
598 ndev->netdev_ops = NULL;
599 return -EBADE;
602 static void brcmf_net_detach(struct net_device *ndev, bool rtnl_locked)
604 if (ndev->reg_state == NETREG_REGISTERED) {
605 if (rtnl_locked)
606 unregister_netdevice(ndev);
607 else
608 unregister_netdev(ndev);
609 } else {
610 brcmf_cfg80211_free_netdev(ndev);
611 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,
620 on);
622 ndev = ifp->ndev;
623 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_DISCONNECTED, !on);
624 if (on) {
625 if (!netif_carrier_ok(ndev))
626 netif_carrier_on(ndev);
628 } else {
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)
651 if (skb)
652 dev_kfree_skb_any(skb);
654 return NETDEV_TX_OK;
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,
668 ifp->mac_addr);
669 ndev = ifp->ndev;
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");
678 goto fail;
681 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
683 return 0;
685 fail:
686 ifp->drvr->iflist[ifp->bsscfgidx] = NULL;
687 ndev->netdev_ops = NULL;
688 return -EBADE;
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.
704 if (ifp) {
705 if (ifidx) {
706 brcmf_err("ERROR: netdev:%s already exists\n",
707 ifp->ndev->name);
708 netif_stop_queue(ifp->ndev);
709 brcmf_net_detach(ifp->ndev, false);
710 drvr->iflist[bsscfgidx] = NULL;
711 } else {
712 brcmf_dbg(INFO, "netdev:%s ignore IF event\n",
713 ifp->ndev->name);
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);
722 if (!ifp)
723 return ERR_PTR(-ENOMEM);
724 } else {
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);
729 if (!ndev)
730 return ERR_PTR(-ENOMEM);
732 ndev->needs_free_netdev = true;
733 ifp = netdev_priv(ndev);
734 ifp->ndev = ndev;
735 /* store mapping ifidx to bsscfgidx */
736 if (drvr->if2bss[ifidx] == BRCMF_BSSIDX_INVALID)
737 drvr->if2bss[ifidx] = bsscfgidx;
740 ifp->drvr = drvr;
741 drvr->iflist[bsscfgidx] = ifp;
742 ifp->ifidx = ifidx;
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);
754 return ifp;
757 static void brcmf_del_if(struct brcmf_pub *drvr, s32 bsscfgidx,
758 bool rtnl_locked)
760 struct brcmf_if *ifp;
762 ifp = drvr->iflist[bsscfgidx];
763 drvr->iflist[bsscfgidx] = NULL;
764 if (!ifp) {
765 brcmf_err("Null interface, bsscfgidx=%d\n", bsscfgidx);
766 return;
768 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx,
769 ifp->ifidx);
770 if (drvr->if2bss[ifp->ifidx] == bsscfgidx)
771 drvr->if2bss[ifp->ifidx] = BRCMF_BSSIDX_INVALID;
772 if (ifp->ndev) {
773 if (bsscfgidx == 0) {
774 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
775 rtnl_lock();
776 brcmf_netdev_stop(ifp->ndev);
777 rtnl_unlock();
779 } else {
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);
788 } else {
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);
797 kfree(ifp);
801 void brcmf_remove_interface(struct brcmf_if *ifp, bool rtnl_locked)
803 if (!ifp || WARN_ON(ifp->drvr->iflist[ifp->bsscfgidx] != ifp))
804 return;
805 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp->bsscfgidx,
806 ifp->ifidx);
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,
813 void *data)
815 int err;
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,
822 evtmsg->datalen);
823 if (err)
824 brcmf_err("Failed to get memory dump, %d\n", err);
826 return err;
829 #ifdef CONFIG_INET
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,
835 inetaddr_notifier);
836 struct in_ifaddr *ifa = data;
837 struct net_device *ndev = ifa->ifa_dev->dev;
838 struct brcmf_if *ifp;
839 int idx, i, ret;
840 u32 val;
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)
847 break;
848 if (idx == BRCMF_MAX_IFS - 1)
849 return NOTIFY_DONE;
852 /* check if arp offload is supported */
853 ret = brcmf_fil_iovar_int_get(ifp, "arpoe", &val);
854 if (ret)
855 return NOTIFY_OK;
857 /* old version only support primary index */
858 ret = brcmf_fil_iovar_int_get(ifp, "arp_version", &val);
859 if (ret)
860 val = 1;
861 if (val == 1)
862 ifp = drvr->iflist[0];
864 /* retrieve the table from firmware */
865 ret = brcmf_fil_iovar_data_get(ifp, "arp_hostip", addr_table,
866 sizeof(addr_table));
867 if (ret) {
868 brcmf_err("fail to get arp ip table err:%d\n", ret);
869 return NOTIFY_OK;
872 for (i = 0; i < ARPOL_MAX_ENTRIES; i++)
873 if (ifa->ifa_address == addr_table[i])
874 break;
876 switch (action) {
877 case NETDEV_UP:
878 if (i == ARPOL_MAX_ENTRIES) {
879 brcmf_dbg(TRACE, "add %pI4 to arp table\n",
880 &ifa->ifa_address);
881 /* set it directly */
882 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
883 &ifa->ifa_address, sizeof(ifa->ifa_address));
884 if (ret)
885 brcmf_err("add arp ip err %d\n", ret);
887 break;
888 case NETDEV_DOWN:
889 if (i < ARPOL_MAX_ENTRIES) {
890 addr_table[i] = 0;
891 brcmf_dbg(TRACE, "remove %pI4 from arp table\n",
892 &ifa->ifa_address);
893 /* clear the table in firmware */
894 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear",
895 NULL, 0);
896 if (ret) {
897 brcmf_err("fail to clear arp ip table err:%d\n",
898 ret);
899 return NOTIFY_OK;
901 for (i = 0; i < ARPOL_MAX_ENTRIES; i++) {
902 if (addr_table[i] == 0)
903 continue;
904 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
905 &addr_table[i],
906 sizeof(addr_table[i]));
907 if (ret)
908 brcmf_err("add arp ip err %d\n",
909 ret);
912 break;
913 default:
914 break;
917 return NOTIFY_OK;
919 #endif
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,
926 inet6addr_notifier);
927 struct inet6_ifaddr *ifa = data;
928 struct brcmf_if *ifp;
929 int i;
930 struct in6_addr *table;
932 /* Only handle primary interface */
933 ifp = drvr->iflist[0];
934 if (!ifp)
935 return NOTIFY_DONE;
936 if (ifp->ndev != ifa->idev->dev)
937 return NOTIFY_DONE;
939 table = ifp->ipv6_addr_tbl;
940 for (i = 0; i < NDOL_MAX_ENTRIES; i++)
941 if (ipv6_addr_equal(&ifa->addr, &table[i]))
942 break;
944 switch (action) {
945 case NETDEV_UP:
946 if (i == NDOL_MAX_ENTRIES) {
947 if (ifp->ipv6addr_idx < NDOL_MAX_ENTRIES) {
948 table[ifp->ipv6addr_idx++] = ifa->addr;
949 } else {
950 for (i = 0; i < NDOL_MAX_ENTRIES - 1; i++)
951 table[i] = table[i + 1];
952 table[NDOL_MAX_ENTRIES - 1] = ifa->addr;
955 break;
956 case NETDEV_DOWN:
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]));
961 ifp->ipv6addr_idx--;
963 break;
964 default:
965 break;
968 schedule_work(&ifp->ndoffload_work);
970 return NOTIFY_OK;
972 #endif
974 int brcmf_attach(struct device *dev, struct brcmf_mp_device *settings)
976 struct brcmf_pub *drvr = NULL;
977 int ret = 0;
978 int i;
980 brcmf_dbg(TRACE, "Enter\n");
982 /* Allocate primary brcmf_info */
983 drvr = kzalloc(sizeof(struct brcmf_pub), GFP_ATOMIC);
984 if (!drvr)
985 return -ENOMEM;
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 */
993 drvr->hdrlen = 0;
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);
1003 if (ret != 0) {
1004 brcmf_err("brcmf_prot_attach failed\n");
1005 goto fail;
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);
1015 return ret;
1017 fail:
1018 brcmf_detach(dev);
1020 return ret;
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);
1050 return 0;
1053 int brcmf_bus_started(struct device *dev)
1055 int ret = -1;
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);
1065 if (IS_ERR(ifp))
1066 return PTR_ERR(ifp);
1068 p2p_ifp = NULL;
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);
1075 if (ret < 0)
1076 goto fail;
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);
1090 if (ret < 0)
1091 goto fail;
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) {
1098 ret = -ENOMEM;
1099 goto fail;
1102 ret = brcmf_net_attach(ifp, false);
1104 if ((!ret) && (drvr->settings->p2p_enable)) {
1105 p2p_ifp = drvr->iflist[1];
1106 if (p2p_ifp)
1107 ret = brcmf_net_p2p_attach(p2p_ifp);
1110 if (ret)
1111 goto fail;
1113 #ifdef CONFIG_INET
1114 drvr->inetaddr_notifier.notifier_call = brcmf_inetaddr_changed;
1115 ret = register_inetaddr_notifier(&drvr->inetaddr_notifier);
1116 if (ret)
1117 goto fail;
1119 #if IS_ENABLED(CONFIG_IPV6)
1120 drvr->inet6addr_notifier.notifier_call = brcmf_inet6addr_changed;
1121 ret = register_inet6addr_notifier(&drvr->inet6addr_notifier);
1122 if (ret) {
1123 unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1124 goto fail;
1126 #endif
1127 #endif /* CONFIG_INET */
1129 return 0;
1131 fail:
1132 brcmf_err("failed: %d\n", ret);
1133 if (drvr->config) {
1134 brcmf_cfg80211_detach(drvr->config);
1135 drvr->config = NULL;
1137 brcmf_net_detach(ifp->ndev, false);
1138 if (p2p_ifp)
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)
1143 ret = 0;
1145 return ret;
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;
1153 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;
1163 if (drvr == NULL)
1164 return;
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)
1172 s32 i;
1173 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1174 struct brcmf_pub *drvr = bus_if->drvr;
1176 brcmf_dbg(TRACE, "Enter\n");
1178 if (drvr == NULL)
1179 return;
1181 #ifdef CONFIG_INET
1182 unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1183 #endif
1185 #if IS_ENABLED(CONFIG_IPV6)
1186 unregister_inet6addr_notifier(&drvr->inet6addr_notifier);
1187 #endif
1189 /* stop firmware event handling */
1190 brcmf_fweh_detach(drvr);
1191 if (drvr->config)
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;
1208 kfree(drvr);
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)
1226 int err;
1228 err = wait_event_timeout(ifp->pend_8021x_wait,
1229 !brcmf_get_pend_8021x_cnt(ifp),
1230 MAX_WAIT_FOR_8021X_TX);
1232 if (!err)
1233 brcmf_err("Timed out waiting for no pending 802.1x packets\n");
1235 return !err;
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;
1242 int ifidx;
1244 brcmf_dbg(TRACE, "%d -> %d\n", bus->state, state);
1245 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();
1263 #endif
1264 #ifdef CONFIG_BRCMFMAC_USB
1265 brcmf_usb_register();
1266 #endif
1267 #ifdef CONFIG_BRCMFMAC_PCIE
1268 brcmf_pcie_register();
1269 #endif
1271 static DECLARE_WORK(brcmf_driver_work, brcmf_driver_register);
1273 int __init brcmf_core_init(void)
1275 if (!schedule_work(&brcmf_driver_work))
1276 return -EBUSY;
1278 return 0;
1281 void __exit brcmf_core_exit(void)
1283 cancel_work_sync(&brcmf_driver_work);
1285 #ifdef CONFIG_BRCMFMAC_SDIO
1286 brcmf_sdio_exit();
1287 #endif
1288 #ifdef CONFIG_BRCMFMAC_USB
1289 brcmf_usb_exit();
1290 #endif
1291 #ifdef CONFIG_BRCMFMAC_PCIE
1292 brcmf_pcie_exit();
1293 #endif