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