Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / core.c
blob9d7b8834b85454fb0dabf23538598726ec3a7f88
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/ieee80211_radiotap.h>
25 #include <net/ipv6.h>
26 #include <brcmu_utils.h>
27 #include <brcmu_wifi.h>
29 #include "core.h"
30 #include "bus.h"
31 #include "debug.h"
32 #include "fwil_types.h"
33 #include "p2p.h"
34 #include "pno.h"
35 #include "cfg80211.h"
36 #include "fwil.h"
37 #include "feature.h"
38 #include "proto.h"
39 #include "pcie.h"
40 #include "common.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)
48 if (!ifp)
49 return "<if_null>";
51 if (ifp->ndev)
52 return ifp->ndev->name;
54 return "<if_none>";
57 struct brcmf_if *brcmf_get_ifp(struct brcmf_pub *drvr, int ifidx)
59 struct brcmf_if *ifp;
60 s32 bsscfgidx;
62 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
63 brcmf_err("ifidx %d out of range\n", ifidx);
64 return NULL;
67 ifp = NULL;
68 bsscfgidx = drvr->if2bss[ifidx];
69 if (bsscfgidx >= 0)
70 ifp = drvr->iflist[bsscfgidx];
72 return ifp;
75 void brcmf_configure_arp_nd_offload(struct brcmf_if *ifp, bool enable)
77 s32 err;
78 u32 mode;
80 if (enable)
81 mode = BRCMF_ARP_OL_AGENT | BRCMF_ARP_OL_PEER_AUTO_REPLY;
82 else
83 mode = 0;
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);
88 if (err) {
89 brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, err = %d\n",
90 mode, err);
91 } else {
92 err = brcmf_fil_iovar_int_set(ifp, "arpoe", enable);
93 if (err) {
94 brcmf_dbg(TRACE, "failed to configure (%d) ARP offload err = %d\n",
95 enable, err);
96 } else {
97 brcmf_dbg(TRACE, "successfully configured (%d) ARP offload to 0x%x\n",
98 enable, mode);
102 err = brcmf_fil_iovar_int_set(ifp, "ndoe", enable);
103 if (err) {
104 brcmf_dbg(TRACE, "failed to configure (%d) ND offload err = %d\n",
105 enable, err);
106 } else {
107 brcmf_dbg(TRACE, "successfully configured (%d) ND offload to 0x%x\n",
108 enable, mode);
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;
117 u32 cmd_value, cnt;
118 __le32 cnt_le;
119 char *buf, *bufp;
120 u32 buflen;
121 s32 err;
123 ifp = container_of(work, struct brcmf_if, multicast_work);
125 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
127 ndev = ifp->ndev;
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);
136 if (!buf)
137 return;
138 bufp = buf;
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) {
145 if (!cnt)
146 break;
147 memcpy(bufp, ha->addr, ETH_ALEN);
148 bufp += ETH_ALEN;
149 cnt--;
152 err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
153 if (err < 0) {
154 brcmf_err("Setting mcast_list failed, %d\n", err);
155 cmd_value = cnt ? true : cmd_value;
158 kfree(buf);
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);
166 if (err < 0)
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);
172 if (err < 0)
173 brcmf_err("Setting BRCMF_C_SET_PROMISC failed, %d\n",
174 err);
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;
182 int i, ret;
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);
188 if (ret) {
189 brcmf_dbg(TRACE, "fail to clear nd ip table err:%d\n", ret);
190 return;
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));
197 if (ret)
198 brcmf_err("add nd ip err %d\n", ret);
201 #else
202 static void _brcmf_update_ndtable(struct work_struct *work)
205 #endif
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;
211 int err;
213 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
215 err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", sa->sa_data,
216 ETH_ALEN);
217 if (err < 0) {
218 brcmf_err("Setting cur_etheraddr failed, %d\n", err);
219 } else {
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);
224 return err;
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
237 * @skb: skb to check
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)
246 const u16 *a, *b;
247 #endif
249 if (skb->len - skb->mac_len != 6 ||
250 !is_multicast_ether_addr(eth_hdr(skb)->h_dest))
251 return false;
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))));
257 #else
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]));
262 #endif
265 static netdev_tx_t brcmf_netdev_start_xmit(struct sk_buff *skb,
266 struct net_device *ndev)
268 int ret;
269 struct brcmf_if *ifp = netdev_priv(ndev);
270 struct brcmf_pub *drvr = ifp->drvr;
271 struct ethhdr *eh;
272 int head_delta;
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);
280 dev_kfree_skb(skb);
281 ret = -ENODEV;
282 goto done;
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)) {
297 dev_kfree_skb(skb);
298 ret = -EINVAL;
299 goto done;
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,
310 GFP_ATOMIC);
311 if (ret < 0) {
312 brcmf_err("%s: failed to expand headroom\n",
313 brcmf_ifname(ifp));
314 atomic_inc(&drvr->bus_if->stats.pktcow_failed);
315 goto done;
319 /* validate length for ether packet */
320 if (skb->len < sizeof(*eh)) {
321 ret = -EINVAL;
322 dev_kfree_skb(skb);
323 goto done;
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);
336 if (ret < 0)
337 brcmf_txfinalize(ifp, skb, false);
339 done:
340 if (ret) {
341 ndev->stats.tx_dropped++;
342 } else {
343 ndev->stats.tx_packets++;
344 ndev->stats.tx_bytes += skb->len;
347 /* Return ok: we always eat the packet */
348 return NETDEV_TX_OK;
351 void brcmf_txflowblock_if(struct brcmf_if *ifp,
352 enum brcmf_netif_stop_reason reason, bool state)
354 unsigned long flags;
356 if (!ifp || !ifp->ndev)
357 return;
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);
363 if (state) {
364 if (!ifp->netif_stop)
365 netif_stop_queue(ifp->ndev);
366 ifp->netif_stop |= reason;
367 } else {
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);
383 return;
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);
391 return;
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));
398 if (in_interrupt())
399 netif_rx(skb);
400 else
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().
405 netif_rx_ni(skb);
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)) {
411 /* Do nothing */
412 } else {
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? */
421 skb->len -= 4;
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)
435 int ret;
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);
444 return -ENODATA;
447 skb->protocol = eth_type_trans(skb, (*ifp)->ndev);
448 return 0;
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))
460 return;
462 if (brcmf_proto_is_reorder_skb(skb)) {
463 brcmf_proto_rxreorder(ifp, skb);
464 } else {
465 /* Process special event packets */
466 if (handle_event)
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))
483 return;
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)
491 struct ethhdr *eh;
492 u16 type;
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);
503 if (!success)
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);
541 return 0;
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;
549 u32 toe_ol;
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");
556 return -EAGAIN;
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;
565 else
566 ndev->features &= ~NETIF_F_IP_CSUM;
568 if (brcmf_cfg80211_up(ndev)) {
569 brcmf_err("failed to bring up cfg80211\n");
570 return -EIO;
573 /* Clear, carrier, set when connected or AP mode. */
574 netif_carrier_off(ndev);
575 return 0;
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;
590 s32 err;
592 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
593 ifp->mac_addr);
594 ndev = ifp->ndev;
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);
609 if (rtnl_locked)
610 err = register_netdevice(ndev);
611 else
612 err = register_netdev(ndev);
613 if (err != 0) {
614 brcmf_err("couldn't register the net device\n");
615 goto fail;
618 ndev->priv_destructor = brcmf_cfg80211_free_netdev;
619 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
620 return 0;
622 fail:
623 drvr->iflist[ifp->bsscfgidx] = NULL;
624 ndev->netdev_ops = NULL;
625 return -EBADE;
628 static void brcmf_net_detach(struct net_device *ndev, bool rtnl_locked)
630 if (ndev->reg_state == NETREG_REGISTERED) {
631 if (rtnl_locked)
632 unregister_netdevice(ndev);
633 else
634 unregister_netdev(ndev);
635 } else {
636 brcmf_cfg80211_free_netdev(ndev);
637 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,
646 on);
648 ndev = ifp->ndev;
649 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_DISCONNECTED, !on);
650 if (on) {
651 if (!netif_carrier_ok(ndev))
652 netif_carrier_on(ndev);
654 } else {
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)
677 if (skb)
678 dev_kfree_skb_any(skb);
680 return NETDEV_TX_OK;
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,
694 ifp->mac_addr);
695 ndev = ifp->ndev;
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");
704 goto fail;
707 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
709 return 0;
711 fail:
712 ifp->drvr->iflist[ifp->bsscfgidx] = NULL;
713 ndev->netdev_ops = NULL;
714 return -EBADE;
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.
730 if (ifp) {
731 if (ifidx) {
732 brcmf_err("ERROR: netdev:%s already exists\n",
733 ifp->ndev->name);
734 netif_stop_queue(ifp->ndev);
735 brcmf_net_detach(ifp->ndev, false);
736 drvr->iflist[bsscfgidx] = NULL;
737 } else {
738 brcmf_dbg(INFO, "netdev:%s ignore IF event\n",
739 ifp->ndev->name);
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);
748 if (!ifp)
749 return ERR_PTR(-ENOMEM);
750 } else {
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);
755 if (!ndev)
756 return ERR_PTR(-ENOMEM);
758 ndev->needs_free_netdev = true;
759 ifp = netdev_priv(ndev);
760 ifp->ndev = ndev;
761 /* store mapping ifidx to bsscfgidx */
762 if (drvr->if2bss[ifidx] == BRCMF_BSSIDX_INVALID)
763 drvr->if2bss[ifidx] = bsscfgidx;
766 ifp->drvr = drvr;
767 drvr->iflist[bsscfgidx] = ifp;
768 ifp->ifidx = ifidx;
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);
780 return ifp;
783 static void brcmf_del_if(struct brcmf_pub *drvr, s32 bsscfgidx,
784 bool rtnl_locked)
786 struct brcmf_if *ifp;
787 int ifidx;
789 ifp = drvr->iflist[bsscfgidx];
790 if (!ifp) {
791 brcmf_err("Null interface, bsscfgidx=%d\n", bsscfgidx);
792 return;
794 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx,
795 ifp->ifidx);
796 ifidx = ifp->ifidx;
798 if (ifp->ndev) {
799 if (bsscfgidx == 0) {
800 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
801 rtnl_lock();
802 brcmf_netdev_stop(ifp->ndev);
803 rtnl_unlock();
805 } else {
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);
814 } else {
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);
823 kfree(ifp);
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))
834 return;
835 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp->bsscfgidx,
836 ifp->ifidx);
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,
843 void *data)
845 int err;
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,
852 evtmsg->datalen);
853 if (err)
854 brcmf_err("Failed to get memory dump, %d\n", err);
856 return err;
859 #ifdef CONFIG_INET
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,
865 inetaddr_notifier);
866 struct in_ifaddr *ifa = data;
867 struct net_device *ndev = ifa->ifa_dev->dev;
868 struct brcmf_if *ifp;
869 int idx, i, ret;
870 u32 val;
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)
877 break;
878 if (idx == BRCMF_MAX_IFS - 1)
879 return NOTIFY_DONE;
882 /* check if arp offload is supported */
883 ret = brcmf_fil_iovar_int_get(ifp, "arpoe", &val);
884 if (ret)
885 return NOTIFY_OK;
887 /* old version only support primary index */
888 ret = brcmf_fil_iovar_int_get(ifp, "arp_version", &val);
889 if (ret)
890 val = 1;
891 if (val == 1)
892 ifp = drvr->iflist[0];
894 /* retrieve the table from firmware */
895 ret = brcmf_fil_iovar_data_get(ifp, "arp_hostip", addr_table,
896 sizeof(addr_table));
897 if (ret) {
898 brcmf_err("fail to get arp ip table err:%d\n", ret);
899 return NOTIFY_OK;
902 for (i = 0; i < ARPOL_MAX_ENTRIES; i++)
903 if (ifa->ifa_address == addr_table[i])
904 break;
906 switch (action) {
907 case NETDEV_UP:
908 if (i == ARPOL_MAX_ENTRIES) {
909 brcmf_dbg(TRACE, "add %pI4 to arp table\n",
910 &ifa->ifa_address);
911 /* set it directly */
912 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
913 &ifa->ifa_address, sizeof(ifa->ifa_address));
914 if (ret)
915 brcmf_err("add arp ip err %d\n", ret);
917 break;
918 case NETDEV_DOWN:
919 if (i < ARPOL_MAX_ENTRIES) {
920 addr_table[i] = 0;
921 brcmf_dbg(TRACE, "remove %pI4 from arp table\n",
922 &ifa->ifa_address);
923 /* clear the table in firmware */
924 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear",
925 NULL, 0);
926 if (ret) {
927 brcmf_err("fail to clear arp ip table err:%d\n",
928 ret);
929 return NOTIFY_OK;
931 for (i = 0; i < ARPOL_MAX_ENTRIES; i++) {
932 if (addr_table[i] == 0)
933 continue;
934 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
935 &addr_table[i],
936 sizeof(addr_table[i]));
937 if (ret)
938 brcmf_err("add arp ip err %d\n",
939 ret);
942 break;
943 default:
944 break;
947 return NOTIFY_OK;
949 #endif
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,
956 inet6addr_notifier);
957 struct inet6_ifaddr *ifa = data;
958 struct brcmf_if *ifp;
959 int i;
960 struct in6_addr *table;
962 /* Only handle primary interface */
963 ifp = drvr->iflist[0];
964 if (!ifp)
965 return NOTIFY_DONE;
966 if (ifp->ndev != ifa->idev->dev)
967 return NOTIFY_DONE;
969 table = ifp->ipv6_addr_tbl;
970 for (i = 0; i < NDOL_MAX_ENTRIES; i++)
971 if (ipv6_addr_equal(&ifa->addr, &table[i]))
972 break;
974 switch (action) {
975 case NETDEV_UP:
976 if (i == NDOL_MAX_ENTRIES) {
977 if (ifp->ipv6addr_idx < NDOL_MAX_ENTRIES) {
978 table[ifp->ipv6addr_idx++] = ifa->addr;
979 } else {
980 for (i = 0; i < NDOL_MAX_ENTRIES - 1; i++)
981 table[i] = table[i + 1];
982 table[NDOL_MAX_ENTRIES - 1] = ifa->addr;
985 break;
986 case NETDEV_DOWN:
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]));
991 ifp->ipv6addr_idx--;
993 break;
994 default:
995 break;
998 schedule_work(&ifp->ndoffload_work);
1000 return NOTIFY_OK;
1002 #endif
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);
1030 return 0;
1033 static int brcmf_bus_started(struct brcmf_pub *drvr, struct cfg80211_ops *ops)
1035 int ret = -1;
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);
1044 if (IS_ERR(ifp))
1045 return PTR_ERR(ifp);
1047 p2p_ifp = NULL;
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);
1054 if (ret < 0)
1055 goto fail;
1057 /* Bus is ready, do any initialization */
1058 ret = brcmf_c_preinit_dcmds(ifp);
1059 if (ret < 0)
1060 goto fail;
1062 brcmf_feat_attach(drvr);
1064 ret = brcmf_proto_init_done(drvr);
1065 if (ret < 0)
1066 goto fail;
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) {
1073 ret = -ENOMEM;
1074 goto fail;
1077 ret = brcmf_net_attach(ifp, false);
1079 if ((!ret) && (drvr->settings->p2p_enable)) {
1080 p2p_ifp = drvr->iflist[1];
1081 if (p2p_ifp)
1082 ret = brcmf_net_p2p_attach(p2p_ifp);
1085 if (ret)
1086 goto fail;
1088 #ifdef CONFIG_INET
1089 drvr->inetaddr_notifier.notifier_call = brcmf_inetaddr_changed;
1090 ret = register_inetaddr_notifier(&drvr->inetaddr_notifier);
1091 if (ret)
1092 goto fail;
1094 #if IS_ENABLED(CONFIG_IPV6)
1095 drvr->inet6addr_notifier.notifier_call = brcmf_inet6addr_changed;
1096 ret = register_inet6addr_notifier(&drvr->inet6addr_notifier);
1097 if (ret) {
1098 unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1099 goto fail;
1101 #endif
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);
1110 return 0;
1112 fail:
1113 brcmf_err("failed: %d\n", ret);
1114 if (drvr->config) {
1115 brcmf_cfg80211_detach(drvr->config);
1116 drvr->config = NULL;
1118 brcmf_net_detach(ifp->ndev, false);
1119 if (p2p_ifp)
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)
1124 ret = 0;
1126 return ret;
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;
1134 int ret = 0;
1135 int i;
1137 brcmf_dbg(TRACE, "Enter\n");
1139 ops = brcmf_cfg80211_get_ops(settings);
1140 if (!ops)
1141 return -ENOMEM;
1143 wiphy = wiphy_new(ops, sizeof(*drvr));
1144 if (!wiphy)
1145 return -ENOMEM;
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 */
1157 drvr->hdrlen = 0;
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);
1164 if (ret != 0) {
1165 brcmf_err("brcmf_prot_attach failed\n");
1166 goto fail;
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);
1177 if (ret != 0) {
1178 brcmf_err("dongle is not responding: err=%d\n", ret);
1179 goto fail;
1182 drvr->config->ops = ops;
1183 return 0;
1185 fail:
1186 brcmf_detach(dev);
1187 kfree(ops);
1189 return ret;
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;
1197 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;
1207 if (drvr == NULL)
1208 return;
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)
1224 s32 i;
1225 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1226 struct brcmf_pub *drvr = bus_if->drvr;
1228 brcmf_dbg(TRACE, "Enter\n");
1230 if (drvr == NULL)
1231 return;
1233 #ifdef CONFIG_INET
1234 unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1235 #endif
1237 #if IS_ENABLED(CONFIG_IPV6)
1238 unregister_inet6addr_notifier(&drvr->inet6addr_notifier);
1239 #endif
1241 /* stop firmware event handling */
1242 brcmf_fweh_detach(drvr);
1243 if (drvr->config)
1244 brcmf_p2p_detach(&drvr->config->p2p);
1246 brcmf_bus_change_state(bus_if, BRCMF_BUS_DOWN);
1248 brcmf_proto_detach_pre_delif(drvr);
1250 if (drvr->mon_if) {
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)
1285 int err;
1287 err = wait_event_timeout(ifp->pend_8021x_wait,
1288 !brcmf_get_pend_8021x_cnt(ifp),
1289 MAX_WAIT_FOR_8021X_TX);
1291 if (!err)
1292 brcmf_err("Timed out waiting for no pending 802.1x packets\n");
1294 return !err;
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;
1301 int ifidx;
1303 brcmf_dbg(TRACE, "%d -> %d\n", bus->state, state);
1305 if (!drvr) {
1306 brcmf_dbg(INFO, "ignoring transition, bus not attached yet\n");
1307 return;
1310 bus->state = state;
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();
1328 #endif
1329 #ifdef CONFIG_BRCMFMAC_USB
1330 brcmf_usb_register();
1331 #endif
1332 #ifdef CONFIG_BRCMFMAC_PCIE
1333 brcmf_pcie_register();
1334 #endif
1336 static DECLARE_WORK(brcmf_driver_work, brcmf_driver_register);
1338 int __init brcmf_core_init(void)
1340 if (!schedule_work(&brcmf_driver_work))
1341 return -EBUSY;
1343 return 0;
1346 void __exit brcmf_core_exit(void)
1348 cancel_work_sync(&brcmf_driver_work);
1350 #ifdef CONFIG_BRCMFMAC_SDIO
1351 brcmf_sdio_exit();
1352 #endif
1353 #ifdef CONFIG_BRCMFMAC_USB
1354 brcmf_usb_exit();
1355 #endif
1356 #ifdef CONFIG_BRCMFMAC_PCIE
1357 brcmf_pcie_exit();
1358 #endif