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