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