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