1 /*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <linux/kernel.h>
18 #include <linux/etherdevice.h>
19 #include <linux/module.h>
20 #include <linux/inetdevice.h>
21 #include <net/cfg80211.h>
22 #include <net/rtnetlink.h>
23 #include <net/addrconf.h>
24 #include <net/ipv6.h>
25 #include <brcmu_utils.h>
26 #include <brcmu_wifi.h>
27
28 #include "core.h"
29 #include "bus.h"
30 #include "debug.h"
31 #include "fwil_types.h"
32 #include "p2p.h"
33 #include "cfg80211.h"
34 #include "fwil.h"
35 #include "fwsignal.h"
36 #include "feature.h"
37 #include "proto.h"
38 #include "pcie.h"
39 #include "common.h"
40
41 #define MAX_WAIT_FOR_8021X_TX msecs_to_jiffies(950)
42
43 #define BRCMF_BSSIDX_INVALID -1
44
brcmf_ifname(struct brcmf_if * ifp)45 char *brcmf_ifname(struct brcmf_if *ifp)
46 {
47 if (!ifp)
48 return "<if_null>";
49
50 if (ifp->ndev)
51 return ifp->ndev->name;
52
53 return "<if_none>";
54 }
55
brcmf_get_ifp(struct brcmf_pub * drvr,int ifidx)56 struct brcmf_if *brcmf_get_ifp(struct brcmf_pub *drvr, int ifidx)
57 {
58 struct brcmf_if *ifp;
59 s32 bsscfgidx;
60
61 if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
62 brcmf_err("ifidx %d out of range\n", ifidx);
63 return NULL;
64 }
65
66 ifp = NULL;
67 bsscfgidx = drvr->if2bss[ifidx];
68 if (bsscfgidx >= 0)
69 ifp = drvr->iflist[bsscfgidx];
70
71 return ifp;
72 }
73
_brcmf_set_multicast_list(struct work_struct * work)74 static void _brcmf_set_multicast_list(struct work_struct *work)
75 {
76 struct brcmf_if *ifp;
77 struct net_device *ndev;
78 struct netdev_hw_addr *ha;
79 u32 cmd_value, cnt;
80 __le32 cnt_le;
81 char *buf, *bufp;
82 u32 buflen;
83 s32 err;
84
85 ifp = container_of(work, struct brcmf_if, multicast_work);
86
87 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
88
89 ndev = ifp->ndev;
90
91 /* Determine initial value of allmulti flag */
92 cmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
93
94 /* Send down the multicast list first. */
95 cnt = netdev_mc_count(ndev);
96 buflen = sizeof(cnt) + (cnt * ETH_ALEN);
97 buf = kmalloc(buflen, GFP_ATOMIC);
98 if (!buf)
99 return;
100 bufp = buf;
101
102 cnt_le = cpu_to_le32(cnt);
103 memcpy(bufp, &cnt_le, sizeof(cnt_le));
104 bufp += sizeof(cnt_le);
105
106 netdev_for_each_mc_addr(ha, ndev) {
107 if (!cnt)
108 break;
109 memcpy(bufp, ha->addr, ETH_ALEN);
110 bufp += ETH_ALEN;
111 cnt--;
112 }
113
114 err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
115 if (err < 0) {
116 brcmf_err("Setting mcast_list failed, %d\n", err);
117 cmd_value = cnt ? true : cmd_value;
118 }
119
120 kfree(buf);
121
122 /*
123 * Now send the allmulti setting. This is based on the setting in the
124 * net_device flags, but might be modified above to be turned on if we
125 * were trying to set some addresses and dongle rejected it...
126 */
127 err = brcmf_fil_iovar_int_set(ifp, "allmulti", cmd_value);
128 if (err < 0)
129 brcmf_err("Setting allmulti failed, %d\n", err);
130
131 /*Finally, pick up the PROMISC flag */
132 cmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
133 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PROMISC, cmd_value);
134 if (err < 0)
135 brcmf_err("Setting BRCMF_C_SET_PROMISC failed, %d\n",
136 err);
137 }
138
139 #if IS_ENABLED(CONFIG_IPV6)
_brcmf_update_ndtable(struct work_struct * work)140 static void _brcmf_update_ndtable(struct work_struct *work)
141 {
142 struct brcmf_if *ifp;
143 int i, ret;
144
145 ifp = container_of(work, struct brcmf_if, ndoffload_work);
146
147 /* clear the table in firmware */
148 ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip_clear", NULL, 0);
149 if (ret) {
150 brcmf_dbg(TRACE, "fail to clear nd ip table err:%d\n", ret);
151 return;
152 }
153
154 for (i = 0; i < ifp->ipv6addr_idx; i++) {
155 ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip",
156 &ifp->ipv6_addr_tbl[i],
157 sizeof(struct in6_addr));
158 if (ret)
159 brcmf_err("add nd ip err %d\n", ret);
160 }
161 }
162 #else
_brcmf_update_ndtable(struct work_struct * work)163 static void _brcmf_update_ndtable(struct work_struct *work)
164 {
165 }
166 #endif
167
brcmf_netdev_set_mac_address(struct net_device * ndev,void * addr)168 static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
169 {
170 struct brcmf_if *ifp = netdev_priv(ndev);
171 struct sockaddr *sa = (struct sockaddr *)addr;
172 int err;
173
174 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
175
176 err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", sa->sa_data,
177 ETH_ALEN);
178 if (err < 0) {
179 brcmf_err("Setting cur_etheraddr failed, %d\n", err);
180 } else {
181 brcmf_dbg(TRACE, "updated to %pM\n", sa->sa_data);
182 memcpy(ifp->mac_addr, sa->sa_data, ETH_ALEN);
183 memcpy(ifp->ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
184 }
185 return err;
186 }
187
brcmf_netdev_set_multicast_list(struct net_device * ndev)188 static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
189 {
190 struct brcmf_if *ifp = netdev_priv(ndev);
191
192 schedule_work(&ifp->multicast_work);
193 }
194
brcmf_netdev_start_xmit(struct sk_buff * skb,struct net_device * ndev)195 static netdev_tx_t brcmf_netdev_start_xmit(struct sk_buff *skb,
196 struct net_device *ndev)
197 {
198 int ret;
199 struct brcmf_if *ifp = netdev_priv(ndev);
200 struct brcmf_pub *drvr = ifp->drvr;
201 struct ethhdr *eh;
202
203 brcmf_dbg(DATA, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
204
205 /* Can the device send data? */
206 if (drvr->bus_if->state != BRCMF_BUS_UP) {
207 brcmf_err("xmit rejected state=%d\n", drvr->bus_if->state);
208 netif_stop_queue(ndev);
209 dev_kfree_skb(skb);
210 ret = -ENODEV;
211 goto done;
212 }
213
214 /* Make sure there's enough writable headroom*/
215 ret = skb_cow_head(skb, drvr->hdrlen);
216 if (ret < 0) {
217 brcmf_err("%s: skb_cow_head failed\n",
218 brcmf_ifname(ifp));
219 dev_kfree_skb(skb);
220 goto done;
221 }
222
223 /* validate length for ether packet */
224 if (skb->len < sizeof(*eh)) {
225 ret = -EINVAL;
226 dev_kfree_skb(skb);
227 goto done;
228 }
229
230 eh = (struct ethhdr *)(skb->data);
231
232 if (eh->h_proto == htons(ETH_P_PAE))
233 atomic_inc(&ifp->pend_8021x_cnt);
234
235 ret = brcmf_fws_process_skb(ifp, skb);
236
237 done:
238 if (ret) {
239 ifp->stats.tx_dropped++;
240 } else {
241 ifp->stats.tx_packets++;
242 ifp->stats.tx_bytes += skb->len;
243 }
244
245 /* Return ok: we always eat the packet */
246 return NETDEV_TX_OK;
247 }
248
brcmf_txflowblock_if(struct brcmf_if * ifp,enum brcmf_netif_stop_reason reason,bool state)249 void brcmf_txflowblock_if(struct brcmf_if *ifp,
250 enum brcmf_netif_stop_reason reason, bool state)
251 {
252 unsigned long flags;
253
254 if (!ifp || !ifp->ndev)
255 return;
256
257 brcmf_dbg(TRACE, "enter: bsscfgidx=%d stop=0x%X reason=%d state=%d\n",
258 ifp->bsscfgidx, ifp->netif_stop, reason, state);
259
260 spin_lock_irqsave(&ifp->netif_stop_lock, flags);
261 if (state) {
262 if (!ifp->netif_stop)
263 netif_stop_queue(ifp->ndev);
264 ifp->netif_stop |= reason;
265 } else {
266 ifp->netif_stop &= ~reason;
267 if (!ifp->netif_stop)
268 netif_wake_queue(ifp->ndev);
269 }
270 spin_unlock_irqrestore(&ifp->netif_stop_lock, flags);
271 }
272
brcmf_txflowblock(struct device * dev,bool state)273 void brcmf_txflowblock(struct device *dev, bool state)
274 {
275 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
276 struct brcmf_pub *drvr = bus_if->drvr;
277
278 brcmf_dbg(TRACE, "Enter\n");
279
280 brcmf_fws_bus_blocked(drvr, state);
281 }
282
brcmf_netif_rx(struct brcmf_if * ifp,struct sk_buff * skb)283 void brcmf_netif_rx(struct brcmf_if *ifp, struct sk_buff *skb)
284 {
285 if (skb->pkt_type == PACKET_MULTICAST)
286 ifp->stats.multicast++;
287
288 if (!(ifp->ndev->flags & IFF_UP)) {
289 brcmu_pkt_buf_free_skb(skb);
290 return;
291 }
292
293 ifp->stats.rx_bytes += skb->len;
294 ifp->stats.rx_packets++;
295
296 brcmf_dbg(DATA, "rx proto=0x%X\n", ntohs(skb->protocol));
297 if (in_interrupt())
298 netif_rx(skb);
299 else
300 /* If the receive is not processed inside an ISR,
301 * the softirqd must be woken explicitly to service
302 * the NET_RX_SOFTIRQ. This is handled by netif_rx_ni().
303 */
304 netif_rx_ni(skb);
305 }
306
brcmf_rx_hdrpull(struct brcmf_pub * drvr,struct sk_buff * skb,struct brcmf_if ** ifp)307 static int brcmf_rx_hdrpull(struct brcmf_pub *drvr, struct sk_buff *skb,
308 struct brcmf_if **ifp)
309 {
310 int ret;
311
312 /* process and remove protocol-specific header */
313 ret = brcmf_proto_hdrpull(drvr, true, skb, ifp);
314
315 if (ret || !(*ifp) || !(*ifp)->ndev) {
316 if (ret != -ENODATA && *ifp)
317 (*ifp)->stats.rx_errors++;
318 brcmu_pkt_buf_free_skb(skb);
319 return -ENODATA;
320 }
321
322 skb->protocol = eth_type_trans(skb, (*ifp)->ndev);
323 return 0;
324 }
325
brcmf_rx_frame(struct device * dev,struct sk_buff * skb,bool handle_event)326 void brcmf_rx_frame(struct device *dev, struct sk_buff *skb, bool handle_event)
327 {
328 struct brcmf_if *ifp;
329 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
330 struct brcmf_pub *drvr = bus_if->drvr;
331
332 brcmf_dbg(DATA, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
333
334 if (brcmf_rx_hdrpull(drvr, skb, &ifp))
335 return;
336
337 if (brcmf_proto_is_reorder_skb(skb)) {
338 brcmf_proto_rxreorder(ifp, skb);
339 } else {
340 /* Process special event packets */
341 if (handle_event)
342 brcmf_fweh_process_skb(ifp->drvr, skb);
343
344 brcmf_netif_rx(ifp, skb);
345 }
346 }
347
brcmf_rx_event(struct device * dev,struct sk_buff * skb)348 void brcmf_rx_event(struct device *dev, struct sk_buff *skb)
349 {
350 struct brcmf_if *ifp;
351 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
352 struct brcmf_pub *drvr = bus_if->drvr;
353
354 brcmf_dbg(EVENT, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
355
356 if (brcmf_rx_hdrpull(drvr, skb, &ifp))
357 return;
358
359 brcmf_fweh_process_skb(ifp->drvr, skb);
360 brcmu_pkt_buf_free_skb(skb);
361 }
362
brcmf_txfinalize(struct brcmf_if * ifp,struct sk_buff * txp,bool success)363 void brcmf_txfinalize(struct brcmf_if *ifp, struct sk_buff *txp, bool success)
364 {
365 struct ethhdr *eh;
366 u16 type;
367
368 eh = (struct ethhdr *)(txp->data);
369 type = ntohs(eh->h_proto);
370
371 if (type == ETH_P_PAE) {
372 atomic_dec(&ifp->pend_8021x_cnt);
373 if (waitqueue_active(&ifp->pend_8021x_wait))
374 wake_up(&ifp->pend_8021x_wait);
375 }
376
377 if (!success)
378 ifp->stats.tx_errors++;
379
380 brcmu_pkt_buf_free_skb(txp);
381 }
382
brcmf_txcomplete(struct device * dev,struct sk_buff * txp,bool success)383 void brcmf_txcomplete(struct device *dev, struct sk_buff *txp, bool success)
384 {
385 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
386 struct brcmf_pub *drvr = bus_if->drvr;
387 struct brcmf_if *ifp;
388
389 /* await txstatus signal for firmware if active */
390 if (brcmf_fws_fc_active(drvr->fws)) {
391 if (!success)
392 brcmf_fws_bustxfail(drvr->fws, txp);
393 } else {
394 if (brcmf_proto_hdrpull(drvr, false, txp, &ifp))
395 brcmu_pkt_buf_free_skb(txp);
396 else
397 brcmf_txfinalize(ifp, txp, success);
398 }
399 }
400
brcmf_netdev_get_stats(struct net_device * ndev)401 static struct net_device_stats *brcmf_netdev_get_stats(struct net_device *ndev)
402 {
403 struct brcmf_if *ifp = netdev_priv(ndev);
404
405 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
406
407 return &ifp->stats;
408 }
409
brcmf_ethtool_get_drvinfo(struct net_device * ndev,struct ethtool_drvinfo * info)410 static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
411 struct ethtool_drvinfo *info)
412 {
413 struct brcmf_if *ifp = netdev_priv(ndev);
414 struct brcmf_pub *drvr = ifp->drvr;
415 char drev[BRCMU_DOTREV_LEN] = "n/a";
416
417 if (drvr->revinfo.result == 0)
418 brcmu_dotrev_str(drvr->revinfo.driverrev, drev);
419 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
420 strlcpy(info->version, drev, sizeof(info->version));
421 strlcpy(info->fw_version, drvr->fwver, sizeof(info->fw_version));
422 strlcpy(info->bus_info, dev_name(drvr->bus_if->dev),
423 sizeof(info->bus_info));
424 }
425
426 static const struct ethtool_ops brcmf_ethtool_ops = {
427 .get_drvinfo = brcmf_ethtool_get_drvinfo,
428 };
429
brcmf_netdev_stop(struct net_device * ndev)430 static int brcmf_netdev_stop(struct net_device *ndev)
431 {
432 struct brcmf_if *ifp = netdev_priv(ndev);
433
434 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
435
436 brcmf_cfg80211_down(ndev);
437
438 brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear", NULL, 0);
439
440 brcmf_net_setcarrier(ifp, false);
441
442 return 0;
443 }
444
brcmf_netdev_open(struct net_device * ndev)445 static int brcmf_netdev_open(struct net_device *ndev)
446 {
447 struct brcmf_if *ifp = netdev_priv(ndev);
448 struct brcmf_pub *drvr = ifp->drvr;
449 struct brcmf_bus *bus_if = drvr->bus_if;
450 u32 toe_ol;
451
452 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
453
454 /* If bus is not ready, can't continue */
455 if (bus_if->state != BRCMF_BUS_UP) {
456 brcmf_err("failed bus is not ready\n");
457 return -EAGAIN;
458 }
459
460 atomic_set(&ifp->pend_8021x_cnt, 0);
461
462 /* Get current TOE mode from dongle */
463 if (brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_ol) >= 0
464 && (toe_ol & TOE_TX_CSUM_OL) != 0)
465 ndev->features |= NETIF_F_IP_CSUM;
466 else
467 ndev->features &= ~NETIF_F_IP_CSUM;
468
469 if (brcmf_cfg80211_up(ndev)) {
470 brcmf_err("failed to bring up cfg80211\n");
471 return -EIO;
472 }
473
474 /* Clear, carrier, set when connected or AP mode. */
475 netif_carrier_off(ndev);
476 return 0;
477 }
478
479 static const struct net_device_ops brcmf_netdev_ops_pri = {
480 .ndo_open = brcmf_netdev_open,
481 .ndo_stop = brcmf_netdev_stop,
482 .ndo_get_stats = brcmf_netdev_get_stats,
483 .ndo_start_xmit = brcmf_netdev_start_xmit,
484 .ndo_set_mac_address = brcmf_netdev_set_mac_address,
485 .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
486 };
487
brcmf_net_attach(struct brcmf_if * ifp,bool rtnl_locked)488 int brcmf_net_attach(struct brcmf_if *ifp, bool rtnl_locked)
489 {
490 struct brcmf_pub *drvr = ifp->drvr;
491 struct net_device *ndev;
492 s32 err;
493
494 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
495 ifp->mac_addr);
496 ndev = ifp->ndev;
497
498 /* set appropriate operations */
499 ndev->netdev_ops = &brcmf_netdev_ops_pri;
500
501 ndev->needed_headroom += drvr->hdrlen;
502 ndev->ethtool_ops = &brcmf_ethtool_ops;
503
504 /* set the mac address */
505 memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
506
507 INIT_WORK(&ifp->multicast_work, _brcmf_set_multicast_list);
508 INIT_WORK(&ifp->ndoffload_work, _brcmf_update_ndtable);
509
510 if (rtnl_locked)
511 err = register_netdevice(ndev);
512 else
513 err = register_netdev(ndev);
514 if (err != 0) {
515 brcmf_err("couldn't register the net device\n");
516 goto fail;
517 }
518
519 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
520 return 0;
521
522 fail:
523 drvr->iflist[ifp->bsscfgidx] = NULL;
524 ndev->netdev_ops = NULL;
525 free_netdev(ndev);
526 return -EBADE;
527 }
528
brcmf_net_detach(struct net_device * ndev,bool rtnl_locked)529 static void brcmf_net_detach(struct net_device *ndev, bool rtnl_locked)
530 {
531 if (ndev->reg_state == NETREG_REGISTERED) {
532 if (rtnl_locked)
533 unregister_netdevice(ndev);
534 else
535 unregister_netdev(ndev);
536 } else {
537 brcmf_cfg80211_free_netdev(ndev);
538 }
539 }
540
brcmf_net_setcarrier(struct brcmf_if * ifp,bool on)541 void brcmf_net_setcarrier(struct brcmf_if *ifp, bool on)
542 {
543 struct net_device *ndev;
544
545 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d carrier=%d\n", ifp->bsscfgidx,
546 on);
547
548 ndev = ifp->ndev;
549 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_DISCONNECTED, !on);
550 if (on) {
551 if (!netif_carrier_ok(ndev))
552 netif_carrier_on(ndev);
553
554 } else {
555 if (netif_carrier_ok(ndev))
556 netif_carrier_off(ndev);
557 }
558 }
559
brcmf_net_p2p_open(struct net_device * ndev)560 static int brcmf_net_p2p_open(struct net_device *ndev)
561 {
562 brcmf_dbg(TRACE, "Enter\n");
563
564 return brcmf_cfg80211_up(ndev);
565 }
566
brcmf_net_p2p_stop(struct net_device * ndev)567 static int brcmf_net_p2p_stop(struct net_device *ndev)
568 {
569 brcmf_dbg(TRACE, "Enter\n");
570
571 return brcmf_cfg80211_down(ndev);
572 }
573
brcmf_net_p2p_start_xmit(struct sk_buff * skb,struct net_device * ndev)574 static netdev_tx_t brcmf_net_p2p_start_xmit(struct sk_buff *skb,
575 struct net_device *ndev)
576 {
577 if (skb)
578 dev_kfree_skb_any(skb);
579
580 return NETDEV_TX_OK;
581 }
582
583 static const struct net_device_ops brcmf_netdev_ops_p2p = {
584 .ndo_open = brcmf_net_p2p_open,
585 .ndo_stop = brcmf_net_p2p_stop,
586 .ndo_start_xmit = brcmf_net_p2p_start_xmit
587 };
588
brcmf_net_p2p_attach(struct brcmf_if * ifp)589 static int brcmf_net_p2p_attach(struct brcmf_if *ifp)
590 {
591 struct net_device *ndev;
592
593 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
594 ifp->mac_addr);
595 ndev = ifp->ndev;
596
597 ndev->netdev_ops = &brcmf_netdev_ops_p2p;
598
599 /* set the mac address */
600 memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
601
602 if (register_netdev(ndev) != 0) {
603 brcmf_err("couldn't register the p2p net device\n");
604 goto fail;
605 }
606
607 brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
608
609 return 0;
610
611 fail:
612 ifp->drvr->iflist[ifp->bsscfgidx] = NULL;
613 ndev->netdev_ops = NULL;
614 free_netdev(ndev);
615 return -EBADE;
616 }
617
brcmf_add_if(struct brcmf_pub * drvr,s32 bsscfgidx,s32 ifidx,bool is_p2pdev,const char * name,u8 * mac_addr)618 struct brcmf_if *brcmf_add_if(struct brcmf_pub *drvr, s32 bsscfgidx, s32 ifidx,
619 bool is_p2pdev, const char *name, u8 *mac_addr)
620 {
621 struct brcmf_if *ifp;
622 struct net_device *ndev;
623
624 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx, ifidx);
625
626 ifp = drvr->iflist[bsscfgidx];
627 /*
628 * Delete the existing interface before overwriting it
629 * in case we missed the BRCMF_E_IF_DEL event.
630 */
631 if (ifp) {
632 if (ifidx) {
633 brcmf_err("ERROR: netdev:%s already exists\n",
634 ifp->ndev->name);
635 netif_stop_queue(ifp->ndev);
636 brcmf_net_detach(ifp->ndev, false);
637 drvr->iflist[bsscfgidx] = NULL;
638 } else {
639 brcmf_dbg(INFO, "netdev:%s ignore IF event\n",
640 ifp->ndev->name);
641 return ERR_PTR(-EINVAL);
642 }
643 }
644
645 if (!drvr->settings->p2p_enable && is_p2pdev) {
646 /* this is P2P_DEVICE interface */
647 brcmf_dbg(INFO, "allocate non-netdev interface\n");
648 ifp = kzalloc(sizeof(*ifp), GFP_KERNEL);
649 if (!ifp)
650 return ERR_PTR(-ENOMEM);
651 } else {
652 brcmf_dbg(INFO, "allocate netdev interface\n");
653 /* Allocate netdev, including space for private structure */
654 ndev = alloc_netdev(sizeof(*ifp), is_p2pdev ? "p2p%d" : name,
655 NET_NAME_UNKNOWN, ether_setup);
656 if (!ndev)
657 return ERR_PTR(-ENOMEM);
658
659 ndev->destructor = brcmf_cfg80211_free_netdev;
660 ifp = netdev_priv(ndev);
661 ifp->ndev = ndev;
662 /* store mapping ifidx to bsscfgidx */
663 if (drvr->if2bss[ifidx] == BRCMF_BSSIDX_INVALID)
664 drvr->if2bss[ifidx] = bsscfgidx;
665 }
666
667 ifp->drvr = drvr;
668 drvr->iflist[bsscfgidx] = ifp;
669 ifp->ifidx = ifidx;
670 ifp->bsscfgidx = bsscfgidx;
671
672 init_waitqueue_head(&ifp->pend_8021x_wait);
673 spin_lock_init(&ifp->netif_stop_lock);
674
675 if (mac_addr != NULL)
676 memcpy(ifp->mac_addr, mac_addr, ETH_ALEN);
677
678 brcmf_dbg(TRACE, " ==== pid:%x, if:%s (%pM) created ===\n",
679 current->pid, name, ifp->mac_addr);
680
681 return ifp;
682 }
683
brcmf_del_if(struct brcmf_pub * drvr,s32 bsscfgidx,bool rtnl_locked)684 static void brcmf_del_if(struct brcmf_pub *drvr, s32 bsscfgidx,
685 bool rtnl_locked)
686 {
687 struct brcmf_if *ifp;
688
689 ifp = drvr->iflist[bsscfgidx];
690 drvr->iflist[bsscfgidx] = NULL;
691 if (!ifp) {
692 brcmf_err("Null interface, bsscfgidx=%d\n", bsscfgidx);
693 return;
694 }
695 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx,
696 ifp->ifidx);
697 if (drvr->if2bss[ifp->ifidx] == bsscfgidx)
698 drvr->if2bss[ifp->ifidx] = BRCMF_BSSIDX_INVALID;
699 if (ifp->ndev) {
700 if (bsscfgidx == 0) {
701 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
702 rtnl_lock();
703 brcmf_netdev_stop(ifp->ndev);
704 rtnl_unlock();
705 }
706 } else {
707 netif_stop_queue(ifp->ndev);
708 }
709
710 if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
711 cancel_work_sync(&ifp->multicast_work);
712 cancel_work_sync(&ifp->ndoffload_work);
713 }
714 brcmf_net_detach(ifp->ndev, rtnl_locked);
715 } else {
716 /* Only p2p device interfaces which get dynamically created
717 * end up here. In this case the p2p module should be informed
718 * about the removal of the interface within the firmware. If
719 * not then p2p commands towards the firmware will cause some
720 * serious troublesome side effects. The p2p module will clean
721 * up the ifp if needed.
722 */
723 brcmf_p2p_ifp_removed(ifp, rtnl_locked);
724 kfree(ifp);
725 }
726 }
727
brcmf_remove_interface(struct brcmf_if * ifp,bool rtnl_locked)728 void brcmf_remove_interface(struct brcmf_if *ifp, bool rtnl_locked)
729 {
730 if (!ifp || WARN_ON(ifp->drvr->iflist[ifp->bsscfgidx] != ifp))
731 return;
732 brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp->bsscfgidx,
733 ifp->ifidx);
734 brcmf_fws_del_interface(ifp);
735 brcmf_del_if(ifp->drvr, ifp->bsscfgidx, rtnl_locked);
736 }
737
738 #ifdef CONFIG_INET
739 #define ARPOL_MAX_ENTRIES 8
brcmf_inetaddr_changed(struct notifier_block * nb,unsigned long action,void * data)740 static int brcmf_inetaddr_changed(struct notifier_block *nb,
741 unsigned long action, void *data)
742 {
743 struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
744 inetaddr_notifier);
745 struct in_ifaddr *ifa = data;
746 struct net_device *ndev = ifa->ifa_dev->dev;
747 struct brcmf_if *ifp;
748 int idx, i, ret;
749 u32 val;
750 __be32 addr_table[ARPOL_MAX_ENTRIES] = {0};
751
752 /* Find out if the notification is meant for us */
753 for (idx = 0; idx < BRCMF_MAX_IFS; idx++) {
754 ifp = drvr->iflist[idx];
755 if (ifp && ifp->ndev == ndev)
756 break;
757 if (idx == BRCMF_MAX_IFS - 1)
758 return NOTIFY_DONE;
759 }
760
761 /* check if arp offload is supported */
762 ret = brcmf_fil_iovar_int_get(ifp, "arpoe", &val);
763 if (ret)
764 return NOTIFY_OK;
765
766 /* old version only support primary index */
767 ret = brcmf_fil_iovar_int_get(ifp, "arp_version", &val);
768 if (ret)
769 val = 1;
770 if (val == 1)
771 ifp = drvr->iflist[0];
772
773 /* retrieve the table from firmware */
774 ret = brcmf_fil_iovar_data_get(ifp, "arp_hostip", addr_table,
775 sizeof(addr_table));
776 if (ret) {
777 brcmf_err("fail to get arp ip table err:%d\n", ret);
778 return NOTIFY_OK;
779 }
780
781 for (i = 0; i < ARPOL_MAX_ENTRIES; i++)
782 if (ifa->ifa_address == addr_table[i])
783 break;
784
785 switch (action) {
786 case NETDEV_UP:
787 if (i == ARPOL_MAX_ENTRIES) {
788 brcmf_dbg(TRACE, "add %pI4 to arp table\n",
789 &ifa->ifa_address);
790 /* set it directly */
791 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
792 &ifa->ifa_address, sizeof(ifa->ifa_address));
793 if (ret)
794 brcmf_err("add arp ip err %d\n", ret);
795 }
796 break;
797 case NETDEV_DOWN:
798 if (i < ARPOL_MAX_ENTRIES) {
799 addr_table[i] = 0;
800 brcmf_dbg(TRACE, "remove %pI4 from arp table\n",
801 &ifa->ifa_address);
802 /* clear the table in firmware */
803 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear",
804 NULL, 0);
805 if (ret) {
806 brcmf_err("fail to clear arp ip table err:%d\n",
807 ret);
808 return NOTIFY_OK;
809 }
810 for (i = 0; i < ARPOL_MAX_ENTRIES; i++) {
811 if (addr_table[i] == 0)
812 continue;
813 ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
814 &addr_table[i],
815 sizeof(addr_table[i]));
816 if (ret)
817 brcmf_err("add arp ip err %d\n",
818 ret);
819 }
820 }
821 break;
822 default:
823 break;
824 }
825
826 return NOTIFY_OK;
827 }
828 #endif
829
830 #if IS_ENABLED(CONFIG_IPV6)
brcmf_inet6addr_changed(struct notifier_block * nb,unsigned long action,void * data)831 static int brcmf_inet6addr_changed(struct notifier_block *nb,
832 unsigned long action, void *data)
833 {
834 struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
835 inet6addr_notifier);
836 struct inet6_ifaddr *ifa = data;
837 struct brcmf_if *ifp;
838 int i;
839 struct in6_addr *table;
840
841 /* Only handle primary interface */
842 ifp = drvr->iflist[0];
843 if (!ifp)
844 return NOTIFY_DONE;
845 if (ifp->ndev != ifa->idev->dev)
846 return NOTIFY_DONE;
847
848 table = ifp->ipv6_addr_tbl;
849 for (i = 0; i < NDOL_MAX_ENTRIES; i++)
850 if (ipv6_addr_equal(&ifa->addr, &table[i]))
851 break;
852
853 switch (action) {
854 case NETDEV_UP:
855 if (i == NDOL_MAX_ENTRIES) {
856 if (ifp->ipv6addr_idx < NDOL_MAX_ENTRIES) {
857 table[ifp->ipv6addr_idx++] = ifa->addr;
858 } else {
859 for (i = 0; i < NDOL_MAX_ENTRIES - 1; i++)
860 table[i] = table[i + 1];
861 table[NDOL_MAX_ENTRIES - 1] = ifa->addr;
862 }
863 }
864 break;
865 case NETDEV_DOWN:
866 if (i < NDOL_MAX_ENTRIES) {
867 for (; i < ifp->ipv6addr_idx - 1; i++)
868 table[i] = table[i + 1];
869 memset(&table[i], 0, sizeof(table[i]));
870 ifp->ipv6addr_idx--;
871 }
872 break;
873 default:
874 break;
875 }
876
877 schedule_work(&ifp->ndoffload_work);
878
879 return NOTIFY_OK;
880 }
881 #endif
882
brcmf_attach(struct device * dev,struct brcmf_mp_device * settings)883 int brcmf_attach(struct device *dev, struct brcmf_mp_device *settings)
884 {
885 struct brcmf_pub *drvr = NULL;
886 int ret = 0;
887 int i;
888
889 brcmf_dbg(TRACE, "Enter\n");
890
891 /* Allocate primary brcmf_info */
892 drvr = kzalloc(sizeof(struct brcmf_pub), GFP_ATOMIC);
893 if (!drvr)
894 return -ENOMEM;
895
896 for (i = 0; i < ARRAY_SIZE(drvr->if2bss); i++)
897 drvr->if2bss[i] = BRCMF_BSSIDX_INVALID;
898
899 mutex_init(&drvr->proto_block);
900
901 /* Link to bus module */
902 drvr->hdrlen = 0;
903 drvr->bus_if = dev_get_drvdata(dev);
904 drvr->bus_if->drvr = drvr;
905 drvr->settings = settings;
906
907 /* attach debug facilities */
908 brcmf_debug_attach(drvr);
909
910 /* Attach and link in the protocol */
911 ret = brcmf_proto_attach(drvr);
912 if (ret != 0) {
913 brcmf_err("brcmf_prot_attach failed\n");
914 goto fail;
915 }
916
917 /* attach firmware event handler */
918 brcmf_fweh_attach(drvr);
919
920 return ret;
921
922 fail:
923 brcmf_detach(dev);
924
925 return ret;
926 }
927
brcmf_revinfo_read(struct seq_file * s,void * data)928 static int brcmf_revinfo_read(struct seq_file *s, void *data)
929 {
930 struct brcmf_bus *bus_if = dev_get_drvdata(s->private);
931 struct brcmf_rev_info *ri = &bus_if->drvr->revinfo;
932 char drev[BRCMU_DOTREV_LEN];
933 char brev[BRCMU_BOARDREV_LEN];
934
935 seq_printf(s, "vendorid: 0x%04x\n", ri->vendorid);
936 seq_printf(s, "deviceid: 0x%04x\n", ri->deviceid);
937 seq_printf(s, "radiorev: %s\n", brcmu_dotrev_str(ri->radiorev, drev));
938 seq_printf(s, "chipnum: %u (%x)\n", ri->chipnum, ri->chipnum);
939 seq_printf(s, "chiprev: %u\n", ri->chiprev);
940 seq_printf(s, "chippkg: %u\n", ri->chippkg);
941 seq_printf(s, "corerev: %u\n", ri->corerev);
942 seq_printf(s, "boardid: 0x%04x\n", ri->boardid);
943 seq_printf(s, "boardvendor: 0x%04x\n", ri->boardvendor);
944 seq_printf(s, "boardrev: %s\n", brcmu_boardrev_str(ri->boardrev, brev));
945 seq_printf(s, "driverrev: %s\n", brcmu_dotrev_str(ri->driverrev, drev));
946 seq_printf(s, "ucoderev: %u\n", ri->ucoderev);
947 seq_printf(s, "bus: %u\n", ri->bus);
948 seq_printf(s, "phytype: %u\n", ri->phytype);
949 seq_printf(s, "phyrev: %u\n", ri->phyrev);
950 seq_printf(s, "anarev: %u\n", ri->anarev);
951 seq_printf(s, "nvramrev: %08x\n", ri->nvramrev);
952
953 return 0;
954 }
955
brcmf_bus_start(struct device * dev)956 int brcmf_bus_start(struct device *dev)
957 {
958 int ret = -1;
959 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
960 struct brcmf_pub *drvr = bus_if->drvr;
961 struct brcmf_if *ifp;
962 struct brcmf_if *p2p_ifp;
963
964 brcmf_dbg(TRACE, "\n");
965
966 /* add primary networking interface */
967 ifp = brcmf_add_if(drvr, 0, 0, false, "wlan%d", NULL);
968 if (IS_ERR(ifp))
969 return PTR_ERR(ifp);
970
971 p2p_ifp = NULL;
972
973 /* signal bus ready */
974 brcmf_bus_change_state(bus_if, BRCMF_BUS_UP);
975
976 /* Bus is ready, do any initialization */
977 ret = brcmf_c_preinit_dcmds(ifp);
978 if (ret < 0)
979 goto fail;
980
981 brcmf_debugfs_add_entry(drvr, "revinfo", brcmf_revinfo_read);
982
983 /* assure we have chipid before feature attach */
984 if (!bus_if->chip) {
985 bus_if->chip = drvr->revinfo.chipnum;
986 bus_if->chiprev = drvr->revinfo.chiprev;
987 brcmf_dbg(INFO, "firmware revinfo: chip %x (%d) rev %d\n",
988 bus_if->chip, bus_if->chip, bus_if->chiprev);
989 }
990 brcmf_feat_attach(drvr);
991
992 ret = brcmf_fws_init(drvr);
993 if (ret < 0)
994 goto fail;
995
996 brcmf_fws_add_interface(ifp);
997
998 drvr->config = brcmf_cfg80211_attach(drvr, bus_if->dev,
999 drvr->settings->p2p_enable);
1000 if (drvr->config == NULL) {
1001 ret = -ENOMEM;
1002 goto fail;
1003 }
1004
1005 ret = brcmf_net_attach(ifp, false);
1006
1007 if ((!ret) && (drvr->settings->p2p_enable)) {
1008 p2p_ifp = drvr->iflist[1];
1009 if (p2p_ifp)
1010 ret = brcmf_net_p2p_attach(p2p_ifp);
1011 }
1012
1013 if (ret)
1014 goto fail;
1015
1016 #ifdef CONFIG_INET
1017 drvr->inetaddr_notifier.notifier_call = brcmf_inetaddr_changed;
1018 ret = register_inetaddr_notifier(&drvr->inetaddr_notifier);
1019 if (ret)
1020 goto fail;
1021
1022 #if IS_ENABLED(CONFIG_IPV6)
1023 drvr->inet6addr_notifier.notifier_call = brcmf_inet6addr_changed;
1024 ret = register_inet6addr_notifier(&drvr->inet6addr_notifier);
1025 if (ret) {
1026 unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1027 goto fail;
1028 }
1029 #endif
1030 #endif /* CONFIG_INET */
1031
1032 return 0;
1033
1034 fail:
1035 brcmf_err("failed: %d\n", ret);
1036 if (drvr->config) {
1037 brcmf_cfg80211_detach(drvr->config);
1038 drvr->config = NULL;
1039 }
1040 if (drvr->fws) {
1041 brcmf_fws_del_interface(ifp);
1042 brcmf_fws_deinit(drvr);
1043 }
1044 brcmf_net_detach(ifp->ndev, false);
1045 if (p2p_ifp)
1046 brcmf_net_detach(p2p_ifp->ndev, false);
1047 drvr->iflist[0] = NULL;
1048 drvr->iflist[1] = NULL;
1049 if (drvr->settings->ignore_probe_fail)
1050 ret = 0;
1051
1052 return ret;
1053 }
1054
brcmf_bus_add_txhdrlen(struct device * dev,uint len)1055 void brcmf_bus_add_txhdrlen(struct device *dev, uint len)
1056 {
1057 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1058 struct brcmf_pub *drvr = bus_if->drvr;
1059
1060 if (drvr) {
1061 drvr->hdrlen += len;
1062 }
1063 }
1064
brcmf_bus_detach(struct brcmf_pub * drvr)1065 static void brcmf_bus_detach(struct brcmf_pub *drvr)
1066 {
1067 brcmf_dbg(TRACE, "Enter\n");
1068
1069 if (drvr) {
1070 /* Stop the bus module */
1071 brcmf_bus_stop(drvr->bus_if);
1072 }
1073 }
1074
brcmf_dev_reset(struct device * dev)1075 void brcmf_dev_reset(struct device *dev)
1076 {
1077 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1078 struct brcmf_pub *drvr = bus_if->drvr;
1079
1080 if (drvr == NULL)
1081 return;
1082
1083 if (drvr->iflist[0])
1084 brcmf_fil_cmd_int_set(drvr->iflist[0], BRCMF_C_TERMINATED, 1);
1085 }
1086
brcmf_detach(struct device * dev)1087 void brcmf_detach(struct device *dev)
1088 {
1089 s32 i;
1090 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1091 struct brcmf_pub *drvr = bus_if->drvr;
1092
1093 brcmf_dbg(TRACE, "Enter\n");
1094
1095 if (drvr == NULL)
1096 return;
1097
1098 #ifdef CONFIG_INET
1099 unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
1100 #endif
1101
1102 #if IS_ENABLED(CONFIG_IPV6)
1103 unregister_inet6addr_notifier(&drvr->inet6addr_notifier);
1104 #endif
1105
1106 /* stop firmware event handling */
1107 brcmf_fweh_detach(drvr);
1108 if (drvr->config)
1109 brcmf_p2p_detach(&drvr->config->p2p);
1110
1111 brcmf_bus_change_state(bus_if, BRCMF_BUS_DOWN);
1112
1113 /* make sure primary interface removed last */
1114 for (i = BRCMF_MAX_IFS-1; i > -1; i--)
1115 brcmf_remove_interface(drvr->iflist[i], false);
1116
1117 brcmf_cfg80211_detach(drvr->config);
1118
1119 brcmf_fws_deinit(drvr);
1120
1121 brcmf_bus_detach(drvr);
1122
1123 brcmf_proto_detach(drvr);
1124
1125 brcmf_debug_detach(drvr);
1126 bus_if->drvr = NULL;
1127 kfree(drvr);
1128 }
1129
brcmf_iovar_data_set(struct device * dev,char * name,void * data,u32 len)1130 s32 brcmf_iovar_data_set(struct device *dev, char *name, void *data, u32 len)
1131 {
1132 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1133 struct brcmf_if *ifp = bus_if->drvr->iflist[0];
1134
1135 return brcmf_fil_iovar_data_set(ifp, name, data, len);
1136 }
1137
brcmf_get_pend_8021x_cnt(struct brcmf_if * ifp)1138 static int brcmf_get_pend_8021x_cnt(struct brcmf_if *ifp)
1139 {
1140 return atomic_read(&ifp->pend_8021x_cnt);
1141 }
1142
brcmf_netdev_wait_pend8021x(struct brcmf_if * ifp)1143 int brcmf_netdev_wait_pend8021x(struct brcmf_if *ifp)
1144 {
1145 int err;
1146
1147 err = wait_event_timeout(ifp->pend_8021x_wait,
1148 !brcmf_get_pend_8021x_cnt(ifp),
1149 MAX_WAIT_FOR_8021X_TX);
1150
1151 if (!err)
1152 brcmf_err("Timed out waiting for no pending 802.1x packets\n");
1153
1154 return !err;
1155 }
1156
brcmf_bus_change_state(struct brcmf_bus * bus,enum brcmf_bus_state state)1157 void brcmf_bus_change_state(struct brcmf_bus *bus, enum brcmf_bus_state state)
1158 {
1159 struct brcmf_pub *drvr = bus->drvr;
1160 struct net_device *ndev;
1161 int ifidx;
1162
1163 brcmf_dbg(TRACE, "%d -> %d\n", bus->state, state);
1164 bus->state = state;
1165
1166 if (state == BRCMF_BUS_UP) {
1167 for (ifidx = 0; ifidx < BRCMF_MAX_IFS; ifidx++) {
1168 if ((drvr->iflist[ifidx]) &&
1169 (drvr->iflist[ifidx]->ndev)) {
1170 ndev = drvr->iflist[ifidx]->ndev;
1171 if (netif_queue_stopped(ndev))
1172 netif_wake_queue(ndev);
1173 }
1174 }
1175 }
1176 }
1177
brcmf_driver_register(struct work_struct * work)1178 static void brcmf_driver_register(struct work_struct *work)
1179 {
1180 #ifdef CONFIG_BRCMFMAC_SDIO
1181 brcmf_sdio_register();
1182 #endif
1183 #ifdef CONFIG_BRCMFMAC_USB
1184 brcmf_usb_register();
1185 #endif
1186 #ifdef CONFIG_BRCMFMAC_PCIE
1187 brcmf_pcie_register();
1188 #endif
1189 }
1190 static DECLARE_WORK(brcmf_driver_work, brcmf_driver_register);
1191
brcmf_core_init(void)1192 int __init brcmf_core_init(void)
1193 {
1194 if (!schedule_work(&brcmf_driver_work))
1195 return -EBUSY;
1196
1197 return 0;
1198 }
1199
brcmf_core_exit(void)1200 void __exit brcmf_core_exit(void)
1201 {
1202 cancel_work_sync(&brcmf_driver_work);
1203
1204 #ifdef CONFIG_BRCMFMAC_SDIO
1205 brcmf_sdio_exit();
1206 #endif
1207 #ifdef CONFIG_BRCMFMAC_USB
1208 brcmf_usb_exit();
1209 #endif
1210 #ifdef CONFIG_BRCMFMAC_PCIE
1211 brcmf_pcie_exit();
1212 #endif
1213 }
1214
1215