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