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