1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2010 Broadcom Corporation
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/etherdevice.h>
8 #include <linux/module.h>
9 #include <linux/inetdevice.h>
10 #include <net/cfg80211.h>
11 #include <net/rtnetlink.h>
12 #include <net/addrconf.h>
13 #include <net/ieee80211_radiotap.h>
14 #include <net/ipv6.h>
15 #include <brcmu_utils.h>
16 #include <brcmu_wifi.h>
17 
18 #include "core.h"
19 #include "bus.h"
20 #include "debug.h"
21 #include "fwil_types.h"
22 #include "p2p.h"
23 #include "pno.h"
24 #include "cfg80211.h"
25 #include "fwil.h"
26 #include "feature.h"
27 #include "proto.h"
28 #include "pcie.h"
29 #include "common.h"
30 
31 #define MAX_WAIT_FOR_8021X_TX			msecs_to_jiffies(950)
32 
33 #define BRCMF_BSSIDX_INVALID			-1
34 
35 #define	RXS_PBPRES				BIT(2)
36 
37 #define	D11_PHY_HDR_LEN				6
38 
39 struct d11rxhdr_le {
40 	__le16 RxFrameSize;
41 	u16 PAD;
42 	__le16 PhyRxStatus_0;
43 	__le16 PhyRxStatus_1;
44 	__le16 PhyRxStatus_2;
45 	__le16 PhyRxStatus_3;
46 	__le16 PhyRxStatus_4;
47 	__le16 PhyRxStatus_5;
48 	__le16 RxStatus1;
49 	__le16 RxStatus2;
50 	__le16 RxTSFTime;
51 	__le16 RxChan;
52 	u8 unknown[12];
53 } __packed;
54 
55 struct wlc_d11rxhdr {
56 	struct d11rxhdr_le rxhdr;
57 	__le32 tsf_l;
58 	s8 rssi;
59 	s8 rxpwr0;
60 	s8 rxpwr1;
61 	s8 do_rssi_ma;
62 	s8 rxpwr[4];
63 } __packed;
64 
brcmf_ifname(struct brcmf_if * ifp)65 char *brcmf_ifname(struct brcmf_if *ifp)
66 {
67 	if (!ifp)
68 		return "<if_null>";
69 
70 	if (ifp->ndev)
71 		return ifp->ndev->name;
72 
73 	return "<if_none>";
74 }
75 
brcmf_get_ifp(struct brcmf_pub * drvr,int ifidx)76 struct brcmf_if *brcmf_get_ifp(struct brcmf_pub *drvr, int ifidx)
77 {
78 	struct brcmf_if *ifp;
79 	s32 bsscfgidx;
80 
81 	if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
82 		bphy_err(drvr, "ifidx %d out of range\n", ifidx);
83 		return NULL;
84 	}
85 
86 	ifp = NULL;
87 	bsscfgidx = drvr->if2bss[ifidx];
88 	if (bsscfgidx >= 0)
89 		ifp = drvr->iflist[bsscfgidx];
90 
91 	return ifp;
92 }
93 
brcmf_configure_arp_nd_offload(struct brcmf_if * ifp,bool enable)94 void brcmf_configure_arp_nd_offload(struct brcmf_if *ifp, bool enable)
95 {
96 	s32 err;
97 	u32 mode;
98 
99 	if (enable)
100 		mode = BRCMF_ARP_OL_AGENT | BRCMF_ARP_OL_PEER_AUTO_REPLY;
101 	else
102 		mode = 0;
103 
104 	/* Try to set and enable ARP offload feature, this may fail, then it  */
105 	/* is simply not supported and err 0 will be returned                 */
106 	err = brcmf_fil_iovar_int_set(ifp, "arp_ol", mode);
107 	if (err) {
108 		brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, err = %d\n",
109 			  mode, err);
110 	} else {
111 		err = brcmf_fil_iovar_int_set(ifp, "arpoe", enable);
112 		if (err) {
113 			brcmf_dbg(TRACE, "failed to configure (%d) ARP offload err = %d\n",
114 				  enable, err);
115 		} else {
116 			brcmf_dbg(TRACE, "successfully configured (%d) ARP offload to 0x%x\n",
117 				  enable, mode);
118 		}
119 	}
120 
121 	err = brcmf_fil_iovar_int_set(ifp, "ndoe", enable);
122 	if (err) {
123 		brcmf_dbg(TRACE, "failed to configure (%d) ND offload err = %d\n",
124 			  enable, err);
125 	} else {
126 		brcmf_dbg(TRACE, "successfully configured (%d) ND offload to 0x%x\n",
127 			  enable, mode);
128 	}
129 }
130 
_brcmf_set_multicast_list(struct work_struct * work)131 static void _brcmf_set_multicast_list(struct work_struct *work)
132 {
133 	struct brcmf_if *ifp = container_of(work, struct brcmf_if,
134 					    multicast_work);
135 	struct brcmf_pub *drvr = ifp->drvr;
136 	struct net_device *ndev;
137 	struct netdev_hw_addr *ha;
138 	u32 cmd_value, cnt;
139 	__le32 cnt_le;
140 	char *buf, *bufp;
141 	u32 buflen;
142 	s32 err;
143 
144 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
145 
146 	ndev = ifp->ndev;
147 
148 	/* Determine initial value of allmulti flag */
149 	cmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
150 
151 	/* Send down the multicast list first. */
152 	cnt = netdev_mc_count(ndev);
153 	buflen = sizeof(cnt) + (cnt * ETH_ALEN);
154 	buf = kmalloc(buflen, GFP_KERNEL);
155 	if (!buf)
156 		return;
157 	bufp = buf;
158 
159 	cnt_le = cpu_to_le32(cnt);
160 	memcpy(bufp, &cnt_le, sizeof(cnt_le));
161 	bufp += sizeof(cnt_le);
162 
163 	netdev_for_each_mc_addr(ha, ndev) {
164 		if (!cnt)
165 			break;
166 		memcpy(bufp, ha->addr, ETH_ALEN);
167 		bufp += ETH_ALEN;
168 		cnt--;
169 	}
170 
171 	err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
172 	if (err < 0) {
173 		bphy_err(drvr, "Setting mcast_list failed, %d\n", err);
174 		cmd_value = cnt ? true : cmd_value;
175 	}
176 
177 	kfree(buf);
178 
179 	/*
180 	 * Now send the allmulti setting.  This is based on the setting in the
181 	 * net_device flags, but might be modified above to be turned on if we
182 	 * were trying to set some addresses and dongle rejected it...
183 	 */
184 	err = brcmf_fil_iovar_int_set(ifp, "allmulti", cmd_value);
185 	if (err < 0)
186 		bphy_err(drvr, "Setting allmulti failed, %d\n", err);
187 
188 	/*Finally, pick up the PROMISC flag */
189 	cmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
190 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PROMISC, cmd_value);
191 	if (err < 0) {
192 		/* PROMISC unsupported by firmware of older chips */
193 		if (err == -EBADE)
194 			bphy_info_once(drvr, "BRCMF_C_SET_PROMISC unsupported\n");
195 		else
196 			bphy_err(drvr, "Setting BRCMF_C_SET_PROMISC failed, err=%d\n",
197 				 err);
198 	}
199 	brcmf_configure_arp_nd_offload(ifp, !cmd_value);
200 }
201 
202 #if IS_ENABLED(CONFIG_IPV6)
_brcmf_update_ndtable(struct work_struct * work)203 static void _brcmf_update_ndtable(struct work_struct *work)
204 {
205 	struct brcmf_if *ifp = container_of(work, struct brcmf_if,
206 					    ndoffload_work);
207 	struct brcmf_pub *drvr = ifp->drvr;
208 	int i, ret;
209 
210 	/* clear the table in firmware */
211 	ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip_clear", NULL, 0);
212 	if (ret) {
213 		brcmf_dbg(TRACE, "fail to clear nd ip table err:%d\n", ret);
214 		return;
215 	}
216 
217 	for (i = 0; i < ifp->ipv6addr_idx; i++) {
218 		ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip",
219 					       &ifp->ipv6_addr_tbl[i],
220 					       sizeof(struct in6_addr));
221 		if (ret)
222 			bphy_err(drvr, "add nd ip err %d\n", ret);
223 	}
224 }
225 #else
_brcmf_update_ndtable(struct work_struct * work)226 static void _brcmf_update_ndtable(struct work_struct *work)
227 {
228 }
229 #endif
230 
brcmf_netdev_set_mac_address(struct net_device * ndev,void * addr)231 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
232 {
233 	struct brcmf_if *ifp = netdev_priv(ndev);
234 	struct sockaddr *sa = (struct sockaddr *)addr;
235 	struct brcmf_pub *drvr = ifp->drvr;
236 	int err;
237 
238 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
239 
240 	err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", sa->sa_data,
241 				       ETH_ALEN);
242 	if (err < 0) {
243 		bphy_err(drvr, "Setting cur_etheraddr failed, %d\n", err);
244 	} else {
245 		brcmf_dbg(TRACE, "updated to %pM\n", sa->sa_data);
246 		memcpy(ifp->mac_addr, sa->sa_data, ETH_ALEN);
247 		eth_hw_addr_set(ifp->ndev, ifp->mac_addr);
248 	}
249 	return err;
250 }
251 
brcmf_netdev_set_multicast_list(struct net_device * ndev)252 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
253 {
254 	struct brcmf_if *ifp = netdev_priv(ndev);
255 
256 	schedule_work(&ifp->multicast_work);
257 }
258 
259 /**
260  * brcmf_skb_is_iapp - checks if skb is an IAPP packet
261  *
262  * @skb: skb to check
263  */
brcmf_skb_is_iapp(struct sk_buff * skb)264 static bool brcmf_skb_is_iapp(struct sk_buff *skb)
265 {
266 	static const u8 iapp_l2_update_packet[6] __aligned(2) = {
267 		0x00, 0x01, 0xaf, 0x81, 0x01, 0x00,
268 	};
269 	unsigned char *eth_data;
270 #if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
271 	const u16 *a, *b;
272 #endif
273 
274 	if (skb->len - skb->mac_len != 6 ||
275 	    !is_multicast_ether_addr(eth_hdr(skb)->h_dest))
276 		return false;
277 
278 	eth_data = skb_mac_header(skb) + ETH_HLEN;
279 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
280 	return !(((*(const u32 *)eth_data) ^ (*(const u32 *)iapp_l2_update_packet)) |
281 		 ((*(const u16 *)(eth_data + 4)) ^ (*(const u16 *)(iapp_l2_update_packet + 4))));
282 #else
283 	a = (const u16 *)eth_data;
284 	b = (const u16 *)iapp_l2_update_packet;
285 
286 	return !((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2]));
287 #endif
288 }
289 
brcmf_netdev_start_xmit(struct sk_buff * skb,struct net_device * ndev)290 static netdev_tx_t brcmf_netdev_start_xmit(struct sk_buff *skb,
291 					   struct net_device *ndev)
292 {
293 	int ret;
294 	struct brcmf_if *ifp = netdev_priv(ndev);
295 	struct brcmf_pub *drvr = ifp->drvr;
296 	struct ethhdr *eh;
297 	int head_delta;
298 
299 	brcmf_dbg(DATA, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
300 
301 	/* Can the device send data? */
302 	if (drvr->bus_if->state != BRCMF_BUS_UP) {
303 		bphy_err(drvr, "xmit rejected state=%d\n", drvr->bus_if->state);
304 		netif_stop_queue(ndev);
305 		dev_kfree_skb(skb);
306 		ret = -ENODEV;
307 		goto done;
308 	}
309 
310 	/* Some recent Broadcom's firmwares disassociate STA when they receive
311 	 * an 802.11f ADD frame. This behavior can lead to a local DoS security
312 	 * issue. Attacker may trigger disassociation of any STA by sending a
313 	 * proper Ethernet frame to the wireless interface.
314 	 *
315 	 * Moreover this feature may break AP interfaces in some specific
316 	 * setups. This applies e.g. to the bridge with hairpin mode enabled and
317 	 * IFLA_BRPORT_MCAST_TO_UCAST set. IAPP packet generated by a firmware
318 	 * will get passed back to the wireless interface and cause immediate
319 	 * disassociation of a just-connected STA.
320 	 */
321 	if (!drvr->settings->iapp && brcmf_skb_is_iapp(skb)) {
322 		dev_kfree_skb(skb);
323 		ret = -EINVAL;
324 		goto done;
325 	}
326 
327 	/* Make sure there's enough writeable headroom */
328 	if (skb_headroom(skb) < drvr->hdrlen || skb_header_cloned(skb)) {
329 		head_delta = max_t(int, drvr->hdrlen - skb_headroom(skb), 0);
330 
331 		brcmf_dbg(INFO, "%s: insufficient headroom (%d)\n",
332 			  brcmf_ifname(ifp), head_delta);
333 		atomic_inc(&drvr->bus_if->stats.pktcowed);
334 		ret = pskb_expand_head(skb, ALIGN(head_delta, NET_SKB_PAD), 0,
335 				       GFP_ATOMIC);
336 		if (ret < 0) {
337 			bphy_err(drvr, "%s: failed to expand headroom\n",
338 				 brcmf_ifname(ifp));
339 			atomic_inc(&drvr->bus_if->stats.pktcow_failed);
340 			goto done;
341 		}
342 	}
343 
344 	/* validate length for ether packet */
345 	if (skb->len < sizeof(*eh)) {
346 		ret = -EINVAL;
347 		dev_kfree_skb(skb);
348 		goto done;
349 	}
350 
351 	eh = (struct ethhdr *)(skb->data);
352 
353 	if (eh->h_proto == htons(ETH_P_PAE))
354 		atomic_inc(&ifp->pend_8021x_cnt);
355 
356 	/* determine the priority */
357 	if ((skb->priority == 0) || (skb->priority > 7))
358 		skb->priority = cfg80211_classify8021d(skb, NULL);
359 
360 	/* set pacing shift for packet aggregation */
361 	sk_pacing_shift_update(skb->sk, 8);
362 
363 	ret = brcmf_proto_tx_queue_data(drvr, ifp->ifidx, skb);
364 	if (ret < 0)
365 		brcmf_txfinalize(ifp, skb, false);
366 
367 done:
368 	if (ret) {
369 		ndev->stats.tx_dropped++;
370 	} else {
371 		ndev->stats.tx_packets++;
372 		ndev->stats.tx_bytes += skb->len;
373 	}
374 
375 	/* Return ok: we always eat the packet */
376 	return NETDEV_TX_OK;
377 }
378 
brcmf_txflowblock_if(struct brcmf_if * ifp,enum brcmf_netif_stop_reason reason,bool state)379 void brcmf_txflowblock_if(struct brcmf_if *ifp,
380 			  enum brcmf_netif_stop_reason reason, bool state)
381 {
382 	unsigned long flags;
383 
384 	if (!ifp || !ifp->ndev)
385 		return;
386 
387 	brcmf_dbg(TRACE, "enter: bsscfgidx=%d stop=0x%X reason=%d state=%d\n",
388 		  ifp->bsscfgidx, ifp->netif_stop, reason, state);
389 
390 	spin_lock_irqsave(&ifp->netif_stop_lock, flags);
391 	if (state) {
392 		if (!ifp->netif_stop)
393 			netif_stop_queue(ifp->ndev);
394 		ifp->netif_stop |= reason;
395 	} else {
396 		ifp->netif_stop &= ~reason;
397 		if (!ifp->netif_stop)
398 			netif_wake_queue(ifp->ndev);
399 	}
400 	spin_unlock_irqrestore(&ifp->netif_stop_lock, flags);
401 }
402 
brcmf_netif_rx(struct brcmf_if * ifp,struct sk_buff * skb,bool inirq)403 void brcmf_netif_rx(struct brcmf_if *ifp, struct sk_buff *skb, bool inirq)
404 {
405 	/* Most of Broadcom's firmwares send 802.11f ADD frame every time a new
406 	 * STA connects to the AP interface. This is an obsoleted standard most
407 	 * users don't use, so don't pass these frames up unless requested.
408 	 */
409 	if (!ifp->drvr->settings->iapp && brcmf_skb_is_iapp(skb)) {
410 		brcmu_pkt_buf_free_skb(skb);
411 		return;
412 	}
413 
414 	if (skb->pkt_type == PACKET_MULTICAST)
415 		ifp->ndev->stats.multicast++;
416 
417 	if (!(ifp->ndev->flags & IFF_UP)) {
418 		brcmu_pkt_buf_free_skb(skb);
419 		return;
420 	}
421 
422 	ifp->ndev->stats.rx_bytes += skb->len;
423 	ifp->ndev->stats.rx_packets++;
424 
425 	brcmf_dbg(DATA, "rx proto=0x%X\n", ntohs(skb->protocol));
426 	if (inirq) {
427 		netif_rx(skb);
428 	} else {
429 		/* If the receive is not processed inside an ISR,
430 		 * the softirqd must be woken explicitly to service
431 		 * the NET_RX_SOFTIRQ.  This is handled by netif_rx_ni().
432 		 */
433 		netif_rx_ni(skb);
434 	}
435 }
436 
brcmf_netif_mon_rx(struct brcmf_if * ifp,struct sk_buff * skb)437 void brcmf_netif_mon_rx(struct brcmf_if *ifp, struct sk_buff *skb)
438 {
439 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FMT_RADIOTAP)) {
440 		/* Do nothing */
441 	} else if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FMT_HW_RX_HDR)) {
442 		struct wlc_d11rxhdr *wlc_rxhdr = (struct wlc_d11rxhdr *)skb->data;
443 		struct ieee80211_radiotap_header *radiotap;
444 		unsigned int offset;
445 		u16 RxStatus1;
446 
447 		RxStatus1 = le16_to_cpu(wlc_rxhdr->rxhdr.RxStatus1);
448 
449 		offset = sizeof(struct wlc_d11rxhdr);
450 		/* MAC inserts 2 pad bytes for a4 headers or QoS or A-MSDU
451 		 * subframes
452 		 */
453 		if (RxStatus1 & RXS_PBPRES)
454 			offset += 2;
455 		offset += D11_PHY_HDR_LEN;
456 
457 		skb_pull(skb, offset);
458 
459 		/* TODO: use RX header to fill some radiotap data */
460 		radiotap = skb_push(skb, sizeof(*radiotap));
461 		memset(radiotap, 0, sizeof(*radiotap));
462 		radiotap->it_len = cpu_to_le16(sizeof(*radiotap));
463 
464 		/* TODO: 4 bytes with receive status? */
465 		skb->len -= 4;
466 	} else {
467 		struct ieee80211_radiotap_header *radiotap;
468 
469 		/* TODO: use RX status to fill some radiotap data */
470 		radiotap = skb_push(skb, sizeof(*radiotap));
471 		memset(radiotap, 0, sizeof(*radiotap));
472 		radiotap->it_len = cpu_to_le16(sizeof(*radiotap));
473 
474 		/* TODO: 4 bytes with receive status? */
475 		skb->len -= 4;
476 	}
477 
478 	skb->dev = ifp->ndev;
479 	skb_reset_mac_header(skb);
480 	skb->pkt_type = PACKET_OTHERHOST;
481 	skb->protocol = htons(ETH_P_802_2);
482 
483 	brcmf_netif_rx(ifp, skb, false);
484 }
485 
brcmf_rx_hdrpull(struct brcmf_pub * drvr,struct sk_buff * skb,struct brcmf_if ** ifp)486 static int brcmf_rx_hdrpull(struct brcmf_pub *drvr, struct sk_buff *skb,
487 			    struct brcmf_if **ifp)
488 {
489 	int ret;
490 
491 	/* process and remove protocol-specific header */
492 	ret = brcmf_proto_hdrpull(drvr, true, skb, ifp);
493 
494 	if (ret || !(*ifp) || !(*ifp)->ndev) {
495 		if (ret != -ENODATA && *ifp && (*ifp)->ndev)
496 			(*ifp)->ndev->stats.rx_errors++;
497 		brcmu_pkt_buf_free_skb(skb);
498 		return -ENODATA;
499 	}
500 
501 	skb->protocol = eth_type_trans(skb, (*ifp)->ndev);
502 	return 0;
503 }
504 
brcmf_rx_frame(struct device * dev,struct sk_buff * skb,bool handle_event,bool inirq)505 void brcmf_rx_frame(struct device *dev, struct sk_buff *skb, bool handle_event,
506 		    bool inirq)
507 {
508 	struct brcmf_if *ifp;
509 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
510 	struct brcmf_pub *drvr = bus_if->drvr;
511 
512 	brcmf_dbg(DATA, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
513 
514 	if (brcmf_rx_hdrpull(drvr, skb, &ifp))
515 		return;
516 
517 	if (brcmf_proto_is_reorder_skb(skb)) {
518 		brcmf_proto_rxreorder(ifp, skb, inirq);
519 	} else {
520 		/* Process special event packets */
521 		if (handle_event) {
522 			gfp_t gfp = inirq ? GFP_ATOMIC : GFP_KERNEL;
523 
524 			brcmf_fweh_process_skb(ifp->drvr, skb,
525 					       BCMILCP_SUBTYPE_VENDOR_LONG, gfp);
526 		}
527 		brcmf_netif_rx(ifp, skb, inirq);
528 	}
529 }
530 
brcmf_rx_event(struct device * dev,struct sk_buff * skb)531 void brcmf_rx_event(struct device *dev, struct sk_buff *skb)
532 {
533 	struct brcmf_if *ifp;
534 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
535 	struct brcmf_pub *drvr = bus_if->drvr;
536 
537 	brcmf_dbg(EVENT, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
538 
539 	if (brcmf_rx_hdrpull(drvr, skb, &ifp))
540 		return;
541 
542 	brcmf_fweh_process_skb(ifp->drvr, skb, 0, GFP_KERNEL);
543 	brcmu_pkt_buf_free_skb(skb);
544 }
545 
brcmf_txfinalize(struct brcmf_if * ifp,struct sk_buff * txp,bool success)546 void brcmf_txfinalize(struct brcmf_if *ifp, struct sk_buff *txp, bool success)
547 {
548 	struct ethhdr *eh;
549 	u16 type;
550 
551 	eh = (struct ethhdr *)(txp->data);
552 	type = ntohs(eh->h_proto);
553 
554 	if (type == ETH_P_PAE) {
555 		atomic_dec(&ifp->pend_8021x_cnt);
556 		if (waitqueue_active(&ifp->pend_8021x_wait))
557 			wake_up(&ifp->pend_8021x_wait);
558 	}
559 
560 	if (!success)
561 		ifp->ndev->stats.tx_errors++;
562 
563 	brcmu_pkt_buf_free_skb(txp);
564 }
565 
brcmf_ethtool_get_drvinfo(struct net_device * ndev,struct ethtool_drvinfo * info)566 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
567 				    struct ethtool_drvinfo *info)
568 {
569 	struct brcmf_if *ifp = netdev_priv(ndev);
570 	struct brcmf_pub *drvr = ifp->drvr;
571 	char drev[BRCMU_DOTREV_LEN] = "n/a";
572 
573 	if (drvr->revinfo.result == 0)
574 		brcmu_dotrev_str(drvr->revinfo.driverrev, drev);
575 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
576 	strlcpy(info->version, drev, sizeof(info->version));
577 	strlcpy(info->fw_version, drvr->fwver, sizeof(info->fw_version));
578 	strlcpy(info->bus_info, dev_name(drvr->bus_if->dev),
579 		sizeof(info->bus_info));
580 }
581 
582 static const struct ethtool_ops brcmf_ethtool_ops = {
583 	.get_drvinfo = brcmf_ethtool_get_drvinfo,
584 };
585 
brcmf_netdev_stop(struct net_device * ndev)586 static int brcmf_netdev_stop(struct net_device *ndev)
587 {
588 	struct brcmf_if *ifp = netdev_priv(ndev);
589 
590 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
591 
592 	brcmf_cfg80211_down(ndev);
593 
594 	brcmf_net_setcarrier(ifp, false);
595 
596 	return 0;
597 }
598 
brcmf_netdev_open(struct net_device * ndev)599 static int brcmf_netdev_open(struct net_device *ndev)
600 {
601 	struct brcmf_if *ifp = netdev_priv(ndev);
602 	struct brcmf_pub *drvr = ifp->drvr;
603 	struct brcmf_bus *bus_if = drvr->bus_if;
604 	u32 toe_ol;
605 
606 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
607 
608 	/* If bus is not ready, can't continue */
609 	if (bus_if->state != BRCMF_BUS_UP) {
610 		bphy_err(drvr, "failed bus is not ready\n");
611 		return -EAGAIN;
612 	}
613 
614 	atomic_set(&ifp->pend_8021x_cnt, 0);
615 
616 	/* Get current TOE mode from dongle */
617 	if (brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_ol) >= 0
618 	    && (toe_ol & TOE_TX_CSUM_OL) != 0)
619 		ndev->features |= NETIF_F_IP_CSUM;
620 	else
621 		ndev->features &= ~NETIF_F_IP_CSUM;
622 
623 	if (brcmf_cfg80211_up(ndev)) {
624 		bphy_err(drvr, "failed to bring up cfg80211\n");
625 		return -EIO;
626 	}
627 
628 	/* Clear, carrier, set when connected or AP mode. */
629 	netif_carrier_off(ndev);
630 	return 0;
631 }
632 
633 static const struct net_device_ops brcmf_netdev_ops_pri = {
634 	.ndo_open = brcmf_netdev_open,
635 	.ndo_stop = brcmf_netdev_stop,
636 	.ndo_start_xmit = brcmf_netdev_start_xmit,
637 	.ndo_set_mac_address = brcmf_netdev_set_mac_address,
638 	.ndo_set_rx_mode = brcmf_netdev_set_multicast_list
639 };
640 
brcmf_net_attach(struct brcmf_if * ifp,bool locked)641 int brcmf_net_attach(struct brcmf_if *ifp, bool locked)
642 {
643 	struct brcmf_pub *drvr = ifp->drvr;
644 	struct net_device *ndev;
645 	s32 err;
646 
647 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
648 		  ifp->mac_addr);
649 	ndev = ifp->ndev;
650 
651 	/* set appropriate operations */
652 	ndev->netdev_ops = &brcmf_netdev_ops_pri;
653 
654 	ndev->needed_headroom += drvr->hdrlen;
655 	ndev->ethtool_ops = &brcmf_ethtool_ops;
656 
657 	/* set the mac address & netns */
658 	eth_hw_addr_set(ndev, ifp->mac_addr);
659 	dev_net_set(ndev, wiphy_net(cfg_to_wiphy(drvr->config)));
660 
661 	INIT_WORK(&ifp->multicast_work, _brcmf_set_multicast_list);
662 	INIT_WORK(&ifp->ndoffload_work, _brcmf_update_ndtable);
663 
664 	if (locked)
665 		err = cfg80211_register_netdevice(ndev);
666 	else
667 		err = register_netdev(ndev);
668 	if (err != 0) {
669 		bphy_err(drvr, "couldn't register the net device\n");
670 		goto fail;
671 	}
672 
673 	netif_carrier_off(ndev);
674 
675 	ndev->priv_destructor = brcmf_cfg80211_free_netdev;
676 	brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
677 	return 0;
678 
679 fail:
680 	drvr->iflist[ifp->bsscfgidx] = NULL;
681 	ndev->netdev_ops = NULL;
682 	return -EBADE;
683 }
684 
brcmf_net_detach(struct net_device * ndev,bool locked)685 void brcmf_net_detach(struct net_device *ndev, bool locked)
686 {
687 	if (ndev->reg_state == NETREG_REGISTERED) {
688 		if (locked)
689 			cfg80211_unregister_netdevice(ndev);
690 		else
691 			unregister_netdev(ndev);
692 	} else {
693 		brcmf_cfg80211_free_netdev(ndev);
694 		free_netdev(ndev);
695 	}
696 }
697 
brcmf_net_mon_open(struct net_device * ndev)698 static int brcmf_net_mon_open(struct net_device *ndev)
699 {
700 	struct brcmf_if *ifp = netdev_priv(ndev);
701 	struct brcmf_pub *drvr = ifp->drvr;
702 	u32 monitor;
703 	int err;
704 
705 	brcmf_dbg(TRACE, "Enter\n");
706 
707 	err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_MONITOR, &monitor);
708 	if (err) {
709 		bphy_err(drvr, "BRCMF_C_GET_MONITOR error (%d)\n", err);
710 		return err;
711 	} else if (monitor) {
712 		bphy_err(drvr, "Monitor mode is already enabled\n");
713 		return -EEXIST;
714 	}
715 
716 	monitor = 3;
717 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_MONITOR, monitor);
718 	if (err)
719 		bphy_err(drvr, "BRCMF_C_SET_MONITOR error (%d)\n", err);
720 
721 	return err;
722 }
723 
brcmf_net_mon_stop(struct net_device * ndev)724 static int brcmf_net_mon_stop(struct net_device *ndev)
725 {
726 	struct brcmf_if *ifp = netdev_priv(ndev);
727 	struct brcmf_pub *drvr = ifp->drvr;
728 	u32 monitor;
729 	int err;
730 
731 	brcmf_dbg(TRACE, "Enter\n");
732 
733 	monitor = 0;
734 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_MONITOR, monitor);
735 	if (err)
736 		bphy_err(drvr, "BRCMF_C_SET_MONITOR error (%d)\n", err);
737 
738 	return err;
739 }
740 
brcmf_net_mon_start_xmit(struct sk_buff * skb,struct net_device * ndev)741 static netdev_tx_t brcmf_net_mon_start_xmit(struct sk_buff *skb,
742 					    struct net_device *ndev)
743 {
744 	dev_kfree_skb_any(skb);
745 
746 	return NETDEV_TX_OK;
747 }
748 
749 static const struct net_device_ops brcmf_netdev_ops_mon = {
750 	.ndo_open = brcmf_net_mon_open,
751 	.ndo_stop = brcmf_net_mon_stop,
752 	.ndo_start_xmit = brcmf_net_mon_start_xmit,
753 };
754 
brcmf_net_mon_attach(struct brcmf_if * ifp)755 int brcmf_net_mon_attach(struct brcmf_if *ifp)
756 {
757 	struct brcmf_pub *drvr = ifp->drvr;
758 	struct net_device *ndev;
759 	int err;
760 
761 	brcmf_dbg(TRACE, "Enter\n");
762 
763 	ndev = ifp->ndev;
764 	ndev->netdev_ops = &brcmf_netdev_ops_mon;
765 
766 	err = cfg80211_register_netdevice(ndev);
767 	if (err)
768 		bphy_err(drvr, "Failed to register %s device\n", ndev->name);
769 
770 	return err;
771 }
772 
brcmf_net_setcarrier(struct brcmf_if * ifp,bool on)773 void brcmf_net_setcarrier(struct brcmf_if *ifp, bool on)
774 {
775 	struct net_device *ndev;
776 
777 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d carrier=%d\n", ifp->bsscfgidx,
778 		  on);
779 
780 	ndev = ifp->ndev;
781 	brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_DISCONNECTED, !on);
782 	if (on) {
783 		if (!netif_carrier_ok(ndev))
784 			netif_carrier_on(ndev);
785 
786 	} else {
787 		if (netif_carrier_ok(ndev))
788 			netif_carrier_off(ndev);
789 	}
790 }
791 
brcmf_net_p2p_open(struct net_device * ndev)792 static int brcmf_net_p2p_open(struct net_device *ndev)
793 {
794 	brcmf_dbg(TRACE, "Enter\n");
795 
796 	return brcmf_cfg80211_up(ndev);
797 }
798 
brcmf_net_p2p_stop(struct net_device * ndev)799 static int brcmf_net_p2p_stop(struct net_device *ndev)
800 {
801 	brcmf_dbg(TRACE, "Enter\n");
802 
803 	return brcmf_cfg80211_down(ndev);
804 }
805 
brcmf_net_p2p_start_xmit(struct sk_buff * skb,struct net_device * ndev)806 static netdev_tx_t brcmf_net_p2p_start_xmit(struct sk_buff *skb,
807 					    struct net_device *ndev)
808 {
809 	if (skb)
810 		dev_kfree_skb_any(skb);
811 
812 	return NETDEV_TX_OK;
813 }
814 
815 static const struct net_device_ops brcmf_netdev_ops_p2p = {
816 	.ndo_open = brcmf_net_p2p_open,
817 	.ndo_stop = brcmf_net_p2p_stop,
818 	.ndo_start_xmit = brcmf_net_p2p_start_xmit
819 };
820 
brcmf_net_p2p_attach(struct brcmf_if * ifp)821 static int brcmf_net_p2p_attach(struct brcmf_if *ifp)
822 {
823 	struct brcmf_pub *drvr = ifp->drvr;
824 	struct net_device *ndev;
825 
826 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
827 		  ifp->mac_addr);
828 	ndev = ifp->ndev;
829 
830 	ndev->netdev_ops = &brcmf_netdev_ops_p2p;
831 
832 	/* set the mac address */
833 	eth_hw_addr_set(ndev, ifp->mac_addr);
834 
835 	if (register_netdev(ndev) != 0) {
836 		bphy_err(drvr, "couldn't register the p2p net device\n");
837 		goto fail;
838 	}
839 
840 	brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
841 
842 	return 0;
843 
844 fail:
845 	ifp->drvr->iflist[ifp->bsscfgidx] = NULL;
846 	ndev->netdev_ops = NULL;
847 	return -EBADE;
848 }
849 
brcmf_add_if(struct brcmf_pub * drvr,s32 bsscfgidx,s32 ifidx,bool is_p2pdev,const char * name,u8 * mac_addr)850 struct brcmf_if *brcmf_add_if(struct brcmf_pub *drvr, s32 bsscfgidx, s32 ifidx,
851 			      bool is_p2pdev, const char *name, u8 *mac_addr)
852 {
853 	struct brcmf_if *ifp;
854 	struct net_device *ndev;
855 
856 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx, ifidx);
857 
858 	ifp = drvr->iflist[bsscfgidx];
859 	/*
860 	 * Delete the existing interface before overwriting it
861 	 * in case we missed the BRCMF_E_IF_DEL event.
862 	 */
863 	if (ifp) {
864 		if (ifidx) {
865 			bphy_err(drvr, "ERROR: netdev:%s already exists\n",
866 				 ifp->ndev->name);
867 			netif_stop_queue(ifp->ndev);
868 			brcmf_net_detach(ifp->ndev, false);
869 			drvr->iflist[bsscfgidx] = NULL;
870 		} else {
871 			brcmf_dbg(INFO, "netdev:%s ignore IF event\n",
872 				  ifp->ndev->name);
873 			return ERR_PTR(-EINVAL);
874 		}
875 	}
876 
877 	if (!drvr->settings->p2p_enable && is_p2pdev) {
878 		/* this is P2P_DEVICE interface */
879 		brcmf_dbg(INFO, "allocate non-netdev interface\n");
880 		ifp = kzalloc(sizeof(*ifp), GFP_KERNEL);
881 		if (!ifp)
882 			return ERR_PTR(-ENOMEM);
883 	} else {
884 		brcmf_dbg(INFO, "allocate netdev interface\n");
885 		/* Allocate netdev, including space for private structure */
886 		ndev = alloc_netdev(sizeof(*ifp), is_p2pdev ? "p2p%d" : name,
887 				    NET_NAME_UNKNOWN, ether_setup);
888 		if (!ndev)
889 			return ERR_PTR(-ENOMEM);
890 
891 		ndev->needs_free_netdev = true;
892 		ifp = netdev_priv(ndev);
893 		ifp->ndev = ndev;
894 		/* store mapping ifidx to bsscfgidx */
895 		if (drvr->if2bss[ifidx] == BRCMF_BSSIDX_INVALID)
896 			drvr->if2bss[ifidx] = bsscfgidx;
897 	}
898 
899 	ifp->drvr = drvr;
900 	drvr->iflist[bsscfgidx] = ifp;
901 	ifp->ifidx = ifidx;
902 	ifp->bsscfgidx = bsscfgidx;
903 
904 	init_waitqueue_head(&ifp->pend_8021x_wait);
905 	spin_lock_init(&ifp->netif_stop_lock);
906 
907 	if (mac_addr != NULL)
908 		memcpy(ifp->mac_addr, mac_addr, ETH_ALEN);
909 
910 	brcmf_dbg(TRACE, " ==== pid:%x, if:%s (%pM) created ===\n",
911 		  current->pid, name, ifp->mac_addr);
912 
913 	return ifp;
914 }
915 
brcmf_del_if(struct brcmf_pub * drvr,s32 bsscfgidx,bool locked)916 static void brcmf_del_if(struct brcmf_pub *drvr, s32 bsscfgidx,
917 			 bool locked)
918 {
919 	struct brcmf_if *ifp;
920 	int ifidx;
921 
922 	ifp = drvr->iflist[bsscfgidx];
923 	if (!ifp) {
924 		bphy_err(drvr, "Null interface, bsscfgidx=%d\n", bsscfgidx);
925 		return;
926 	}
927 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx,
928 		  ifp->ifidx);
929 	ifidx = ifp->ifidx;
930 
931 	if (ifp->ndev) {
932 		if (bsscfgidx == 0) {
933 			if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
934 				rtnl_lock();
935 				brcmf_netdev_stop(ifp->ndev);
936 				rtnl_unlock();
937 			}
938 		} else {
939 			netif_stop_queue(ifp->ndev);
940 		}
941 
942 		if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
943 			cancel_work_sync(&ifp->multicast_work);
944 			cancel_work_sync(&ifp->ndoffload_work);
945 		}
946 		brcmf_net_detach(ifp->ndev, locked);
947 	} else {
948 		/* Only p2p device interfaces which get dynamically created
949 		 * end up here. In this case the p2p module should be informed
950 		 * about the removal of the interface within the firmware. If
951 		 * not then p2p commands towards the firmware will cause some
952 		 * serious troublesome side effects. The p2p module will clean
953 		 * up the ifp if needed.
954 		 */
955 		brcmf_p2p_ifp_removed(ifp, locked);
956 		kfree(ifp);
957 	}
958 
959 	drvr->iflist[bsscfgidx] = NULL;
960 	if (drvr->if2bss[ifidx] == bsscfgidx)
961 		drvr->if2bss[ifidx] = BRCMF_BSSIDX_INVALID;
962 }
963 
brcmf_remove_interface(struct brcmf_if * ifp,bool locked)964 void brcmf_remove_interface(struct brcmf_if *ifp, bool locked)
965 {
966 	if (!ifp || WARN_ON(ifp->drvr->iflist[ifp->bsscfgidx] != ifp))
967 		return;
968 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp->bsscfgidx,
969 		  ifp->ifidx);
970 	brcmf_proto_del_if(ifp->drvr, ifp);
971 	brcmf_del_if(ifp->drvr, ifp->bsscfgidx, locked);
972 }
973 
brcmf_psm_watchdog_notify(struct brcmf_if * ifp,const struct brcmf_event_msg * evtmsg,void * data)974 static int brcmf_psm_watchdog_notify(struct brcmf_if *ifp,
975 				     const struct brcmf_event_msg *evtmsg,
976 				     void *data)
977 {
978 	struct brcmf_pub *drvr = ifp->drvr;
979 	int err;
980 
981 	brcmf_dbg(TRACE, "enter: bsscfgidx=%d\n", ifp->bsscfgidx);
982 
983 	bphy_err(drvr, "PSM's watchdog has fired!\n");
984 
985 	err = brcmf_debug_create_memdump(ifp->drvr->bus_if, data,
986 					 evtmsg->datalen);
987 	if (err)
988 		bphy_err(drvr, "Failed to get memory dump, %d\n", err);
989 
990 	return err;
991 }
992 
993 #ifdef CONFIG_INET
994 #define ARPOL_MAX_ENTRIES	8
brcmf_inetaddr_changed(struct notifier_block * nb,unsigned long action,void * data)995 static int brcmf_inetaddr_changed(struct notifier_block *nb,
996 				  unsigned long action, void *data)
997 {
998 	struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
999 					      inetaddr_notifier);
1000 	struct in_ifaddr *ifa = data;
1001 	struct net_device *ndev = ifa->ifa_dev->dev;
1002 	struct brcmf_if *ifp;
1003 	int idx, i, ret;
1004 	u32 val;
1005 	__be32 addr_table[ARPOL_MAX_ENTRIES] = {0};
1006 
1007 	/* Find out if the notification is meant for us */
1008 	for (idx = 0; idx < BRCMF_MAX_IFS; idx++) {
1009 		ifp = drvr->iflist[idx];
1010 		if (ifp && ifp->ndev == ndev)
1011 			break;
1012 		if (idx == BRCMF_MAX_IFS - 1)
1013 			return NOTIFY_DONE;
1014 	}
1015 
1016 	/* check if arp offload is supported */
1017 	ret = brcmf_fil_iovar_int_get(ifp, "arpoe", &val);
1018 	if (ret)
1019 		return NOTIFY_OK;
1020 
1021 	/* old version only support primary index */
1022 	ret = brcmf_fil_iovar_int_get(ifp, "arp_version", &val);
1023 	if (ret)
1024 		val = 1;
1025 	if (val == 1)
1026 		ifp = drvr->iflist[0];
1027 
1028 	/* retrieve the table from firmware */
1029 	ret = brcmf_fil_iovar_data_get(ifp, "arp_hostip", addr_table,
1030 				       sizeof(addr_table));
1031 	if (ret) {
1032 		bphy_err(drvr, "fail to get arp ip table err:%d\n", ret);
1033 		return NOTIFY_OK;
1034 	}
1035 
1036 	for (i = 0; i < ARPOL_MAX_ENTRIES; i++)
1037 		if (ifa->ifa_address == addr_table[i])
1038 			break;
1039 
1040 	switch (action) {
1041 	case NETDEV_UP:
1042 		if (i == ARPOL_MAX_ENTRIES) {
1043 			brcmf_dbg(TRACE, "add %pI4 to arp table\n",
1044 				  &ifa->ifa_address);
1045 			/* set it directly */
1046 			ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
1047 				&ifa->ifa_address, sizeof(ifa->ifa_address));
1048 			if (ret)
1049 				bphy_err(drvr, "add arp ip err %d\n", ret);
1050 		}
1051 		break;
1052 	case NETDEV_DOWN:
1053 		if (i < ARPOL_MAX_ENTRIES) {
1054 			addr_table[i] = 0;
1055 			brcmf_dbg(TRACE, "remove %pI4 from arp table\n",
1056 				  &ifa->ifa_address);
1057 			/* clear the table in firmware */
1058 			ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear",
1059 						       NULL, 0);
1060 			if (ret) {
1061 				bphy_err(drvr, "fail to clear arp ip table err:%d\n",
1062 					 ret);
1063 				return NOTIFY_OK;
1064 			}
1065 			for (i = 0; i < ARPOL_MAX_ENTRIES; i++) {
1066 				if (addr_table[i] == 0)
1067 					continue;
1068 				ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
1069 							       &addr_table[i],
1070 							       sizeof(addr_table[i]));
1071 				if (ret)
1072 					bphy_err(drvr, "add arp ip err %d\n",
1073 						 ret);
1074 			}
1075 		}
1076 		break;
1077 	default:
1078 		break;
1079 	}
1080 
1081 	return NOTIFY_OK;
1082 }
1083 #endif
1084 
1085 #if IS_ENABLED(CONFIG_IPV6)
brcmf_inet6addr_changed(struct notifier_block * nb,unsigned long action,void * data)1086 static int brcmf_inet6addr_changed(struct notifier_block *nb,
1087 				   unsigned long action, void *data)
1088 {
1089 	struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
1090 					      inet6addr_notifier);
1091 	struct inet6_ifaddr *ifa = data;
1092 	struct brcmf_if *ifp;
1093 	int i;
1094 	struct in6_addr *table;
1095 
1096 	/* Only handle primary interface */
1097 	ifp = drvr->iflist[0];
1098 	if (!ifp)
1099 		return NOTIFY_DONE;
1100 	if (ifp->ndev != ifa->idev->dev)
1101 		return NOTIFY_DONE;
1102 
1103 	table = ifp->ipv6_addr_tbl;
1104 	for (i = 0; i < NDOL_MAX_ENTRIES; i++)
1105 		if (ipv6_addr_equal(&ifa->addr, &table[i]))
1106 			break;
1107 
1108 	switch (action) {
1109 	case NETDEV_UP:
1110 		if (i == NDOL_MAX_ENTRIES) {
1111 			if (ifp->ipv6addr_idx < NDOL_MAX_ENTRIES) {
1112 				table[ifp->ipv6addr_idx++] = ifa->addr;
1113 			} else {
1114 				for (i = 0; i < NDOL_MAX_ENTRIES - 1; i++)
1115 					table[i] = table[i + 1];
1116 				table[NDOL_MAX_ENTRIES - 1] = ifa->addr;
1117 			}
1118 		}
1119 		break;
1120 	case NETDEV_DOWN:
1121 		if (i < NDOL_MAX_ENTRIES) {
1122 			for (; i < ifp->ipv6addr_idx - 1; i++)
1123 				table[i] = table[i + 1];
1124 			memset(&table[i], 0, sizeof(table[i]));
1125 			ifp->ipv6addr_idx--;
1126 		}
1127 		break;
1128 	default:
1129 		break;
1130 	}
1131 
1132 	schedule_work(&ifp->ndoffload_work);
1133 
1134 	return NOTIFY_OK;
1135 }
1136 #endif
1137 
brcmf_revinfo_read(struct seq_file * s,void * data)1138 static int brcmf_revinfo_read(struct seq_file *s, void *data)
1139 {
1140 	struct brcmf_bus *bus_if = dev_get_drvdata(s->private);
1141 	struct brcmf_rev_info *ri = &bus_if->drvr->revinfo;
1142 	char drev[BRCMU_DOTREV_LEN];
1143 	char brev[BRCMU_BOARDREV_LEN];
1144 
1145 	seq_printf(s, "vendorid: 0x%04x\n", ri->vendorid);
1146 	seq_printf(s, "deviceid: 0x%04x\n", ri->deviceid);
1147 	seq_printf(s, "radiorev: %s\n", brcmu_dotrev_str(ri->radiorev, drev));
1148 	seq_printf(s, "chip: %s\n", ri->chipname);
1149 	seq_printf(s, "chippkg: %u\n", ri->chippkg);
1150 	seq_printf(s, "corerev: %u\n", ri->corerev);
1151 	seq_printf(s, "boardid: 0x%04x\n", ri->boardid);
1152 	seq_printf(s, "boardvendor: 0x%04x\n", ri->boardvendor);
1153 	seq_printf(s, "boardrev: %s\n", brcmu_boardrev_str(ri->boardrev, brev));
1154 	seq_printf(s, "driverrev: %s\n", brcmu_dotrev_str(ri->driverrev, drev));
1155 	seq_printf(s, "ucoderev: %u\n", ri->ucoderev);
1156 	seq_printf(s, "bus: %u\n", ri->bus);
1157 	seq_printf(s, "phytype: %u\n", ri->phytype);
1158 	seq_printf(s, "phyrev: %u\n", ri->phyrev);
1159 	seq_printf(s, "anarev: %u\n", ri->anarev);
1160 	seq_printf(s, "nvramrev: %08x\n", ri->nvramrev);
1161 
1162 	seq_printf(s, "clmver: %s\n", bus_if->drvr->clmver);
1163 
1164 	return 0;
1165 }
1166 
brcmf_core_bus_reset(struct work_struct * work)1167 static void brcmf_core_bus_reset(struct work_struct *work)
1168 {
1169 	struct brcmf_pub *drvr = container_of(work, struct brcmf_pub,
1170 					      bus_reset);
1171 
1172 	brcmf_bus_reset(drvr->bus_if);
1173 }
1174 
bus_reset_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)1175 static ssize_t bus_reset_write(struct file *file, const char __user *user_buf,
1176 			       size_t count, loff_t *ppos)
1177 {
1178 	struct brcmf_pub *drvr = file->private_data;
1179 	u8 value;
1180 
1181 	if (kstrtou8_from_user(user_buf, count, 0, &value))
1182 		return -EINVAL;
1183 
1184 	if (value != 1)
1185 		return -EINVAL;
1186 
1187 	schedule_work(&drvr->bus_reset);
1188 
1189 	return count;
1190 }
1191 
1192 static const struct file_operations bus_reset_fops = {
1193 	.open	= simple_open,
1194 	.llseek	= no_llseek,
1195 	.write	= bus_reset_write,
1196 };
1197 
brcmf_bus_started(struct brcmf_pub * drvr,struct cfg80211_ops * ops)1198 static int brcmf_bus_started(struct brcmf_pub *drvr, struct cfg80211_ops *ops)
1199 {
1200 	int ret = -1;
1201 	struct brcmf_bus *bus_if = drvr->bus_if;
1202 	struct brcmf_if *ifp;
1203 	struct brcmf_if *p2p_ifp;
1204 
1205 	brcmf_dbg(TRACE, "\n");
1206 
1207 	/* add primary networking interface */
1208 	ifp = brcmf_add_if(drvr, 0, 0, false, "wlan%d", NULL);
1209 	if (IS_ERR(ifp))
1210 		return PTR_ERR(ifp);
1211 
1212 	p2p_ifp = NULL;
1213 
1214 	/* signal bus ready */
1215 	brcmf_bus_change_state(bus_if, BRCMF_BUS_UP);
1216 
1217 	/* do bus specific preinit here */
1218 	ret = brcmf_bus_preinit(bus_if);
1219 	if (ret < 0)
1220 		goto fail;
1221 
1222 	/* Bus is ready, do any initialization */
1223 	ret = brcmf_c_preinit_dcmds(ifp);
1224 	if (ret < 0)
1225 		goto fail;
1226 
1227 	brcmf_feat_attach(drvr);
1228 
1229 	ret = brcmf_proto_init_done(drvr);
1230 	if (ret < 0)
1231 		goto fail;
1232 
1233 	brcmf_proto_add_if(drvr, ifp);
1234 
1235 	drvr->config = brcmf_cfg80211_attach(drvr, ops,
1236 					     drvr->settings->p2p_enable);
1237 	if (drvr->config == NULL) {
1238 		ret = -ENOMEM;
1239 		goto fail;
1240 	}
1241 
1242 	ret = brcmf_net_attach(ifp, false);
1243 
1244 	if ((!ret) && (drvr->settings->p2p_enable)) {
1245 		p2p_ifp = drvr->iflist[1];
1246 		if (p2p_ifp)
1247 			ret = brcmf_net_p2p_attach(p2p_ifp);
1248 	}
1249 
1250 	if (ret)
1251 		goto fail;
1252 
1253 #ifdef CONFIG_INET
1254 	drvr->inetaddr_notifier.notifier_call = brcmf_inetaddr_changed;
1255 	ret = register_inetaddr_notifier(&drvr->inetaddr_notifier);
1256 	if (ret)
1257 		goto fail;
1258 
1259 #if IS_ENABLED(CONFIG_IPV6)
1260 	drvr->inet6addr_notifier.notifier_call = brcmf_inet6addr_changed;
1261 	ret = register_inet6addr_notifier(&drvr->inet6addr_notifier);
1262 	if (ret) {
1263 		unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1264 		goto fail;
1265 	}
1266 #endif
1267 #endif /* CONFIG_INET */
1268 
1269 	INIT_WORK(&drvr->bus_reset, brcmf_core_bus_reset);
1270 
1271 	/* populate debugfs */
1272 	brcmf_debugfs_add_entry(drvr, "revinfo", brcmf_revinfo_read);
1273 	debugfs_create_file("reset", 0600, brcmf_debugfs_get_devdir(drvr), drvr,
1274 			    &bus_reset_fops);
1275 	brcmf_feat_debugfs_create(drvr);
1276 	brcmf_proto_debugfs_create(drvr);
1277 	brcmf_bus_debugfs_create(bus_if);
1278 
1279 	return 0;
1280 
1281 fail:
1282 	bphy_err(drvr, "failed: %d\n", ret);
1283 	if (drvr->config) {
1284 		brcmf_cfg80211_detach(drvr->config);
1285 		drvr->config = NULL;
1286 	}
1287 	brcmf_net_detach(ifp->ndev, false);
1288 	if (p2p_ifp)
1289 		brcmf_net_detach(p2p_ifp->ndev, false);
1290 	drvr->iflist[0] = NULL;
1291 	drvr->iflist[1] = NULL;
1292 	if (drvr->settings->ignore_probe_fail)
1293 		ret = 0;
1294 
1295 	return ret;
1296 }
1297 
brcmf_alloc(struct device * dev,struct brcmf_mp_device * settings)1298 int brcmf_alloc(struct device *dev, struct brcmf_mp_device *settings)
1299 {
1300 	struct wiphy *wiphy;
1301 	struct cfg80211_ops *ops;
1302 	struct brcmf_pub *drvr = NULL;
1303 
1304 	brcmf_dbg(TRACE, "Enter\n");
1305 
1306 	ops = brcmf_cfg80211_get_ops(settings);
1307 	if (!ops)
1308 		return -ENOMEM;
1309 
1310 	wiphy = wiphy_new(ops, sizeof(*drvr));
1311 	if (!wiphy) {
1312 		kfree(ops);
1313 		return -ENOMEM;
1314 	}
1315 
1316 	set_wiphy_dev(wiphy, dev);
1317 	drvr = wiphy_priv(wiphy);
1318 	drvr->wiphy = wiphy;
1319 	drvr->ops = ops;
1320 	drvr->bus_if = dev_get_drvdata(dev);
1321 	drvr->bus_if->drvr = drvr;
1322 	drvr->settings = settings;
1323 
1324 	return 0;
1325 }
1326 
brcmf_attach(struct device * dev)1327 int brcmf_attach(struct device *dev)
1328 {
1329 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1330 	struct brcmf_pub *drvr = bus_if->drvr;
1331 	int ret = 0;
1332 	int i;
1333 
1334 	brcmf_dbg(TRACE, "Enter\n");
1335 
1336 	for (i = 0; i < ARRAY_SIZE(drvr->if2bss); i++)
1337 		drvr->if2bss[i] = BRCMF_BSSIDX_INVALID;
1338 
1339 	mutex_init(&drvr->proto_block);
1340 
1341 	/* Link to bus module */
1342 	drvr->hdrlen = 0;
1343 
1344 	/* Attach and link in the protocol */
1345 	ret = brcmf_proto_attach(drvr);
1346 	if (ret != 0) {
1347 		bphy_err(drvr, "brcmf_prot_attach failed\n");
1348 		goto fail;
1349 	}
1350 
1351 	/* Attach to events important for core code */
1352 	brcmf_fweh_register(drvr, BRCMF_E_PSM_WATCHDOG,
1353 			    brcmf_psm_watchdog_notify);
1354 
1355 	/* attach firmware event handler */
1356 	brcmf_fweh_attach(drvr);
1357 
1358 	ret = brcmf_bus_started(drvr, drvr->ops);
1359 	if (ret != 0) {
1360 		bphy_err(drvr, "dongle is not responding: err=%d\n", ret);
1361 		goto fail;
1362 	}
1363 
1364 	return 0;
1365 
1366 fail:
1367 	brcmf_detach(dev);
1368 
1369 	return ret;
1370 }
1371 
brcmf_bus_add_txhdrlen(struct device * dev,uint len)1372 void brcmf_bus_add_txhdrlen(struct device *dev, uint len)
1373 {
1374 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1375 	struct brcmf_pub *drvr = bus_if->drvr;
1376 
1377 	if (drvr) {
1378 		drvr->hdrlen += len;
1379 	}
1380 }
1381 
brcmf_dev_reset(struct device * dev)1382 void brcmf_dev_reset(struct device *dev)
1383 {
1384 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1385 	struct brcmf_pub *drvr = bus_if->drvr;
1386 
1387 	if (drvr == NULL)
1388 		return;
1389 
1390 	if (drvr->iflist[0])
1391 		brcmf_fil_cmd_int_set(drvr->iflist[0], BRCMF_C_TERMINATED, 1);
1392 }
1393 
brcmf_dev_coredump(struct device * dev)1394 void brcmf_dev_coredump(struct device *dev)
1395 {
1396 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1397 
1398 	if (brcmf_debug_create_memdump(bus_if, NULL, 0) < 0)
1399 		brcmf_dbg(TRACE, "failed to create coredump\n");
1400 }
1401 
brcmf_fw_crashed(struct device * dev)1402 void brcmf_fw_crashed(struct device *dev)
1403 {
1404 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1405 	struct brcmf_pub *drvr = bus_if->drvr;
1406 
1407 	bphy_err(drvr, "Firmware has halted or crashed\n");
1408 
1409 	brcmf_dev_coredump(dev);
1410 
1411 	schedule_work(&drvr->bus_reset);
1412 }
1413 
brcmf_detach(struct device * dev)1414 void brcmf_detach(struct device *dev)
1415 {
1416 	s32 i;
1417 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1418 	struct brcmf_pub *drvr = bus_if->drvr;
1419 
1420 	brcmf_dbg(TRACE, "Enter\n");
1421 
1422 	if (drvr == NULL)
1423 		return;
1424 
1425 #ifdef CONFIG_INET
1426 	unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1427 #endif
1428 
1429 #if IS_ENABLED(CONFIG_IPV6)
1430 	unregister_inet6addr_notifier(&drvr->inet6addr_notifier);
1431 #endif
1432 
1433 	brcmf_bus_change_state(bus_if, BRCMF_BUS_DOWN);
1434 	/* make sure primary interface removed last */
1435 	for (i = BRCMF_MAX_IFS - 1; i > -1; i--) {
1436 		if (drvr->iflist[i])
1437 			brcmf_remove_interface(drvr->iflist[i], false);
1438 	}
1439 	brcmf_bus_stop(drvr->bus_if);
1440 
1441 	brcmf_fweh_detach(drvr);
1442 	brcmf_proto_detach(drvr);
1443 
1444 	if (drvr->mon_if) {
1445 		brcmf_net_detach(drvr->mon_if->ndev, false);
1446 		drvr->mon_if = NULL;
1447 	}
1448 
1449 	if (drvr->config) {
1450 		brcmf_p2p_detach(&drvr->config->p2p);
1451 		brcmf_cfg80211_detach(drvr->config);
1452 		drvr->config = NULL;
1453 	}
1454 }
1455 
brcmf_free(struct device * dev)1456 void brcmf_free(struct device *dev)
1457 {
1458 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1459 	struct brcmf_pub *drvr = bus_if->drvr;
1460 
1461 	if (!drvr)
1462 		return;
1463 
1464 	bus_if->drvr = NULL;
1465 
1466 	kfree(drvr->ops);
1467 
1468 	wiphy_free(drvr->wiphy);
1469 }
1470 
brcmf_iovar_data_set(struct device * dev,char * name,void * data,u32 len)1471 s32 brcmf_iovar_data_set(struct device *dev, char *name, void *data, u32 len)
1472 {
1473 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1474 	struct brcmf_if *ifp = bus_if->drvr->iflist[0];
1475 
1476 	return brcmf_fil_iovar_data_set(ifp, name, data, len);
1477 }
1478 
brcmf_get_pend_8021x_cnt(struct brcmf_if * ifp)1479 static int brcmf_get_pend_8021x_cnt(struct brcmf_if *ifp)
1480 {
1481 	return atomic_read(&ifp->pend_8021x_cnt);
1482 }
1483 
brcmf_netdev_wait_pend8021x(struct brcmf_if * ifp)1484 int brcmf_netdev_wait_pend8021x(struct brcmf_if *ifp)
1485 {
1486 	struct brcmf_pub *drvr = ifp->drvr;
1487 	int err;
1488 
1489 	err = wait_event_timeout(ifp->pend_8021x_wait,
1490 				 !brcmf_get_pend_8021x_cnt(ifp),
1491 				 MAX_WAIT_FOR_8021X_TX);
1492 
1493 	if (!err)
1494 		bphy_err(drvr, "Timed out waiting for no pending 802.1x packets\n");
1495 
1496 	return !err;
1497 }
1498 
brcmf_bus_change_state(struct brcmf_bus * bus,enum brcmf_bus_state state)1499 void brcmf_bus_change_state(struct brcmf_bus *bus, enum brcmf_bus_state state)
1500 {
1501 	struct brcmf_pub *drvr = bus->drvr;
1502 	struct net_device *ndev;
1503 	int ifidx;
1504 
1505 	brcmf_dbg(TRACE, "%d -> %d\n", bus->state, state);
1506 
1507 	if (!drvr) {
1508 		brcmf_dbg(INFO, "ignoring transition, bus not attached yet\n");
1509 		return;
1510 	}
1511 
1512 	bus->state = state;
1513 
1514 	if (state == BRCMF_BUS_UP) {
1515 		for (ifidx = 0; ifidx < BRCMF_MAX_IFS; ifidx++) {
1516 			if ((drvr->iflist[ifidx]) &&
1517 			    (drvr->iflist[ifidx]->ndev)) {
1518 				ndev = drvr->iflist[ifidx]->ndev;
1519 				if (netif_queue_stopped(ndev))
1520 					netif_wake_queue(ndev);
1521 			}
1522 		}
1523 	}
1524 }
1525 
brcmf_core_init(void)1526 int __init brcmf_core_init(void)
1527 {
1528 	int err;
1529 
1530 	err = brcmf_sdio_register();
1531 	if (err)
1532 		return err;
1533 
1534 	err = brcmf_usb_register();
1535 	if (err)
1536 		goto error_usb_register;
1537 
1538 	err = brcmf_pcie_register();
1539 	if (err)
1540 		goto error_pcie_register;
1541 	return 0;
1542 
1543 error_pcie_register:
1544 	brcmf_usb_exit();
1545 error_usb_register:
1546 	brcmf_sdio_exit();
1547 	return err;
1548 }
1549 
brcmf_core_exit(void)1550 void __exit brcmf_core_exit(void)
1551 {
1552 	brcmf_sdio_exit();
1553 	brcmf_usb_exit();
1554 	brcmf_pcie_exit();
1555 }
1556 
1557