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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/sched.h>
22 #include <linux/netdevice.h>
23 #include <asm/unaligned.h>
24 #include <defs.h>
25 #include <brcmu_wifi.h>
26 #include <brcmu_utils.h>
27 #include "dhd.h"
28 #include "dhd_bus.h"
29 #include "dhd_proto.h"
30 #include "dhd_dbg.h"
31 
32 #define BRCM_OUI			"\x00\x10\x18"
33 #define DOT11_OUI_LEN			3
34 #define BCMILCP_BCM_SUBTYPE_EVENT	1
35 #define PKTFILTER_BUF_SIZE		2048
36 #define BRCMF_ARPOL_MODE		0xb	/* agent|snoop|peer_autoreply */
37 
38 #define MSGTRACE_VERSION	1
39 
40 #define BRCMF_PKT_FILTER_FIXED_LEN	offsetof(struct brcmf_pkt_filter_le, u)
41 #define BRCMF_PKT_FILTER_PATTERN_FIXED_LEN	\
42 	offsetof(struct brcmf_pkt_filter_pattern_le, mask_and_pattern)
43 
44 #ifdef DEBUG
45 static const char brcmf_version[] =
46 	"Dongle Host Driver, version " BRCMF_VERSION_STR "\nCompiled on "
47 	__DATE__ " at " __TIME__;
48 #else
49 static const char brcmf_version[] =
50 	"Dongle Host Driver, version " BRCMF_VERSION_STR;
51 #endif
52 
53 /* Message trace header */
54 struct msgtrace_hdr {
55 	u8 version;
56 	u8 spare;
57 	__be16 len;		/* Len of the trace */
58 	__be32 seqnum;		/* Sequence number of message. Useful
59 				 * if the messsage has been lost
60 				 * because of DMA error or a bus reset
61 				 * (ex: SDIO Func2)
62 				 */
63 	__be32 discarded_bytes;	/* Number of discarded bytes because of
64 				 trace overflow  */
65 	__be32 discarded_printf;	/* Number of discarded printf
66 				 because of trace overflow */
67 } __packed;
68 
69 
70 uint
brcmf_c_mkiovar(char * name,char * data,uint datalen,char * buf,uint buflen)71 brcmf_c_mkiovar(char *name, char *data, uint datalen, char *buf, uint buflen)
72 {
73 	uint len;
74 
75 	len = strlen(name) + 1;
76 
77 	if ((len + datalen) > buflen)
78 		return 0;
79 
80 	strncpy(buf, name, buflen);
81 
82 	/* append data onto the end of the name string */
83 	memcpy(&buf[len], data, datalen);
84 	len += datalen;
85 
86 	return len;
87 }
88 
brcmf_c_prec_enq(struct device * dev,struct pktq * q,struct sk_buff * pkt,int prec)89 bool brcmf_c_prec_enq(struct device *dev, struct pktq *q,
90 		      struct sk_buff *pkt, int prec)
91 {
92 	struct sk_buff *p;
93 	int eprec = -1;		/* precedence to evict from */
94 	bool discard_oldest;
95 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
96 	struct brcmf_pub *drvr = bus_if->drvr;
97 
98 	/* Fast case, precedence queue is not full and we are also not
99 	 * exceeding total queue length
100 	 */
101 	if (!pktq_pfull(q, prec) && !pktq_full(q)) {
102 		brcmu_pktq_penq(q, prec, pkt);
103 		return true;
104 	}
105 
106 	/* Determine precedence from which to evict packet, if any */
107 	if (pktq_pfull(q, prec))
108 		eprec = prec;
109 	else if (pktq_full(q)) {
110 		p = brcmu_pktq_peek_tail(q, &eprec);
111 		if (eprec > prec)
112 			return false;
113 	}
114 
115 	/* Evict if needed */
116 	if (eprec >= 0) {
117 		/* Detect queueing to unconfigured precedence */
118 		discard_oldest = ac_bitmap_tst(drvr->wme_dp, eprec);
119 		if (eprec == prec && !discard_oldest)
120 			return false;	/* refuse newer (incoming) packet */
121 		/* Evict packet according to discard policy */
122 		p = discard_oldest ? brcmu_pktq_pdeq(q, eprec) :
123 			brcmu_pktq_pdeq_tail(q, eprec);
124 		if (p == NULL)
125 			brcmf_dbg(ERROR, "brcmu_pktq_penq() failed, oldest %d\n",
126 				  discard_oldest);
127 
128 		brcmu_pkt_buf_free_skb(p);
129 	}
130 
131 	/* Enqueue */
132 	p = brcmu_pktq_penq(q, prec, pkt);
133 	if (p == NULL)
134 		brcmf_dbg(ERROR, "brcmu_pktq_penq() failed\n");
135 
136 	return p != NULL;
137 }
138 
139 #ifdef DEBUG
140 static void
brcmf_c_show_host_event(struct brcmf_event_msg * event,void * event_data)141 brcmf_c_show_host_event(struct brcmf_event_msg *event, void *event_data)
142 {
143 	uint i, status, reason;
144 	bool group = false, flush_txq = false, link = false;
145 	char *auth_str, *event_name;
146 	unsigned char *buf;
147 	char err_msg[256], eabuf[ETHER_ADDR_STR_LEN];
148 	static struct {
149 		uint event;
150 		char *event_name;
151 	} event_names[] = {
152 		{
153 		BRCMF_E_SET_SSID, "SET_SSID"}, {
154 		BRCMF_E_JOIN, "JOIN"}, {
155 		BRCMF_E_START, "START"}, {
156 		BRCMF_E_AUTH, "AUTH"}, {
157 		BRCMF_E_AUTH_IND, "AUTH_IND"}, {
158 		BRCMF_E_DEAUTH, "DEAUTH"}, {
159 		BRCMF_E_DEAUTH_IND, "DEAUTH_IND"}, {
160 		BRCMF_E_ASSOC, "ASSOC"}, {
161 		BRCMF_E_ASSOC_IND, "ASSOC_IND"}, {
162 		BRCMF_E_REASSOC, "REASSOC"}, {
163 		BRCMF_E_REASSOC_IND, "REASSOC_IND"}, {
164 		BRCMF_E_DISASSOC, "DISASSOC"}, {
165 		BRCMF_E_DISASSOC_IND, "DISASSOC_IND"}, {
166 		BRCMF_E_QUIET_START, "START_QUIET"}, {
167 		BRCMF_E_QUIET_END, "END_QUIET"}, {
168 		BRCMF_E_BEACON_RX, "BEACON_RX"}, {
169 		BRCMF_E_LINK, "LINK"}, {
170 		BRCMF_E_MIC_ERROR, "MIC_ERROR"}, {
171 		BRCMF_E_NDIS_LINK, "NDIS_LINK"}, {
172 		BRCMF_E_ROAM, "ROAM"}, {
173 		BRCMF_E_TXFAIL, "TXFAIL"}, {
174 		BRCMF_E_PMKID_CACHE, "PMKID_CACHE"}, {
175 		BRCMF_E_RETROGRADE_TSF, "RETROGRADE_TSF"}, {
176 		BRCMF_E_PRUNE, "PRUNE"}, {
177 		BRCMF_E_AUTOAUTH, "AUTOAUTH"}, {
178 		BRCMF_E_EAPOL_MSG, "EAPOL_MSG"}, {
179 		BRCMF_E_SCAN_COMPLETE, "SCAN_COMPLETE"}, {
180 		BRCMF_E_ADDTS_IND, "ADDTS_IND"}, {
181 		BRCMF_E_DELTS_IND, "DELTS_IND"}, {
182 		BRCMF_E_BCNSENT_IND, "BCNSENT_IND"}, {
183 		BRCMF_E_BCNRX_MSG, "BCNRX_MSG"}, {
184 		BRCMF_E_BCNLOST_MSG, "BCNLOST_MSG"}, {
185 		BRCMF_E_ROAM_PREP, "ROAM_PREP"}, {
186 		BRCMF_E_PFN_NET_FOUND, "PNO_NET_FOUND"}, {
187 		BRCMF_E_PFN_NET_LOST, "PNO_NET_LOST"}, {
188 		BRCMF_E_RESET_COMPLETE, "RESET_COMPLETE"}, {
189 		BRCMF_E_JOIN_START, "JOIN_START"}, {
190 		BRCMF_E_ROAM_START, "ROAM_START"}, {
191 		BRCMF_E_ASSOC_START, "ASSOC_START"}, {
192 		BRCMF_E_IBSS_ASSOC, "IBSS_ASSOC"}, {
193 		BRCMF_E_RADIO, "RADIO"}, {
194 		BRCMF_E_PSM_WATCHDOG, "PSM_WATCHDOG"}, {
195 		BRCMF_E_PROBREQ_MSG, "PROBREQ_MSG"}, {
196 		BRCMF_E_SCAN_CONFIRM_IND, "SCAN_CONFIRM_IND"}, {
197 		BRCMF_E_PSK_SUP, "PSK_SUP"}, {
198 		BRCMF_E_COUNTRY_CODE_CHANGED, "COUNTRY_CODE_CHANGED"}, {
199 		BRCMF_E_EXCEEDED_MEDIUM_TIME, "EXCEEDED_MEDIUM_TIME"}, {
200 		BRCMF_E_ICV_ERROR, "ICV_ERROR"}, {
201 		BRCMF_E_UNICAST_DECODE_ERROR, "UNICAST_DECODE_ERROR"}, {
202 		BRCMF_E_MULTICAST_DECODE_ERROR, "MULTICAST_DECODE_ERROR"}, {
203 		BRCMF_E_TRACE, "TRACE"}, {
204 		BRCMF_E_ACTION_FRAME, "ACTION FRAME"}, {
205 		BRCMF_E_ACTION_FRAME_COMPLETE, "ACTION FRAME TX COMPLETE"}, {
206 		BRCMF_E_IF, "IF"}, {
207 		BRCMF_E_RSSI, "RSSI"}, {
208 		BRCMF_E_PFN_SCAN_COMPLETE, "SCAN_COMPLETE"}
209 	};
210 	uint event_type, flags, auth_type, datalen;
211 	static u32 seqnum_prev;
212 	struct msgtrace_hdr hdr;
213 	u32 nblost;
214 	char *s, *p;
215 
216 	event_type = be32_to_cpu(event->event_type);
217 	flags = be16_to_cpu(event->flags);
218 	status = be32_to_cpu(event->status);
219 	reason = be32_to_cpu(event->reason);
220 	auth_type = be32_to_cpu(event->auth_type);
221 	datalen = be32_to_cpu(event->datalen);
222 	/* debug dump of event messages */
223 	sprintf(eabuf, "%pM", event->addr);
224 
225 	event_name = "UNKNOWN";
226 	for (i = 0; i < ARRAY_SIZE(event_names); i++) {
227 		if (event_names[i].event == event_type)
228 			event_name = event_names[i].event_name;
229 	}
230 
231 	brcmf_dbg(EVENT, "EVENT: %s, event ID = %d\n", event_name, event_type);
232 	brcmf_dbg(EVENT, "flags 0x%04x, status %d, reason %d, auth_type %d MAC %s\n",
233 		  flags, status, reason, auth_type, eabuf);
234 
235 	if (flags & BRCMF_EVENT_MSG_LINK)
236 		link = true;
237 	if (flags & BRCMF_EVENT_MSG_GROUP)
238 		group = true;
239 	if (flags & BRCMF_EVENT_MSG_FLUSHTXQ)
240 		flush_txq = true;
241 
242 	switch (event_type) {
243 	case BRCMF_E_START:
244 	case BRCMF_E_DEAUTH:
245 	case BRCMF_E_DISASSOC:
246 		brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s\n", event_name, eabuf);
247 		break;
248 
249 	case BRCMF_E_ASSOC_IND:
250 	case BRCMF_E_REASSOC_IND:
251 		brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s\n", event_name, eabuf);
252 		break;
253 
254 	case BRCMF_E_ASSOC:
255 	case BRCMF_E_REASSOC:
256 		if (status == BRCMF_E_STATUS_SUCCESS)
257 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, SUCCESS\n",
258 				  event_name, eabuf);
259 		else if (status == BRCMF_E_STATUS_TIMEOUT)
260 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, TIMEOUT\n",
261 				  event_name, eabuf);
262 		else if (status == BRCMF_E_STATUS_FAIL)
263 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, FAILURE, reason %d\n",
264 				  event_name, eabuf, (int)reason);
265 		else
266 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, unexpected status %d\n",
267 				  event_name, eabuf, (int)status);
268 		break;
269 
270 	case BRCMF_E_DEAUTH_IND:
271 	case BRCMF_E_DISASSOC_IND:
272 		brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, reason %d\n",
273 			  event_name, eabuf, (int)reason);
274 		break;
275 
276 	case BRCMF_E_AUTH:
277 	case BRCMF_E_AUTH_IND:
278 		if (auth_type == WLAN_AUTH_OPEN)
279 			auth_str = "Open System";
280 		else if (auth_type == WLAN_AUTH_SHARED_KEY)
281 			auth_str = "Shared Key";
282 		else {
283 			sprintf(err_msg, "AUTH unknown: %d", (int)auth_type);
284 			auth_str = err_msg;
285 		}
286 		if (event_type == BRCMF_E_AUTH_IND)
287 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, %s\n",
288 				  event_name, eabuf, auth_str);
289 		else if (status == BRCMF_E_STATUS_SUCCESS)
290 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, %s, SUCCESS\n",
291 				  event_name, eabuf, auth_str);
292 		else if (status == BRCMF_E_STATUS_TIMEOUT)
293 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, %s, TIMEOUT\n",
294 				  event_name, eabuf, auth_str);
295 		else if (status == BRCMF_E_STATUS_FAIL) {
296 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, %s, FAILURE, reason %d\n",
297 				  event_name, eabuf, auth_str, (int)reason);
298 		}
299 
300 		break;
301 
302 	case BRCMF_E_JOIN:
303 	case BRCMF_E_ROAM:
304 	case BRCMF_E_SET_SSID:
305 		if (status == BRCMF_E_STATUS_SUCCESS)
306 			brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s\n",
307 				  event_name, eabuf);
308 		else if (status == BRCMF_E_STATUS_FAIL)
309 			brcmf_dbg(EVENT, "MACEVENT: %s, failed\n", event_name);
310 		else if (status == BRCMF_E_STATUS_NO_NETWORKS)
311 			brcmf_dbg(EVENT, "MACEVENT: %s, no networks found\n",
312 				  event_name);
313 		else
314 			brcmf_dbg(EVENT, "MACEVENT: %s, unexpected status %d\n",
315 				  event_name, (int)status);
316 		break;
317 
318 	case BRCMF_E_BEACON_RX:
319 		if (status == BRCMF_E_STATUS_SUCCESS)
320 			brcmf_dbg(EVENT, "MACEVENT: %s, SUCCESS\n", event_name);
321 		else if (status == BRCMF_E_STATUS_FAIL)
322 			brcmf_dbg(EVENT, "MACEVENT: %s, FAIL\n", event_name);
323 		else
324 			brcmf_dbg(EVENT, "MACEVENT: %s, status %d\n",
325 				  event_name, status);
326 		break;
327 
328 	case BRCMF_E_LINK:
329 		brcmf_dbg(EVENT, "MACEVENT: %s %s\n",
330 			  event_name, link ? "UP" : "DOWN");
331 		break;
332 
333 	case BRCMF_E_MIC_ERROR:
334 		brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, Group %d, Flush %d\n",
335 			  event_name, eabuf, group, flush_txq);
336 		break;
337 
338 	case BRCMF_E_ICV_ERROR:
339 	case BRCMF_E_UNICAST_DECODE_ERROR:
340 	case BRCMF_E_MULTICAST_DECODE_ERROR:
341 		brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s\n", event_name, eabuf);
342 		break;
343 
344 	case BRCMF_E_TXFAIL:
345 		brcmf_dbg(EVENT, "MACEVENT: %s, RA %s\n", event_name, eabuf);
346 		break;
347 
348 	case BRCMF_E_SCAN_COMPLETE:
349 	case BRCMF_E_PMKID_CACHE:
350 		brcmf_dbg(EVENT, "MACEVENT: %s\n", event_name);
351 		break;
352 
353 	case BRCMF_E_PFN_NET_FOUND:
354 	case BRCMF_E_PFN_NET_LOST:
355 	case BRCMF_E_PFN_SCAN_COMPLETE:
356 		brcmf_dbg(EVENT, "PNOEVENT: %s\n", event_name);
357 		break;
358 
359 	case BRCMF_E_PSK_SUP:
360 	case BRCMF_E_PRUNE:
361 		brcmf_dbg(EVENT, "MACEVENT: %s, status %d, reason %d\n",
362 			  event_name, (int)status, (int)reason);
363 		break;
364 
365 	case BRCMF_E_TRACE:
366 		buf = (unsigned char *) event_data;
367 		memcpy(&hdr, buf, sizeof(struct msgtrace_hdr));
368 
369 		if (hdr.version != MSGTRACE_VERSION) {
370 			brcmf_dbg(ERROR,
371 				  "MACEVENT: %s [unsupported version --> brcmf"
372 				  " version:%d dongle version:%d]\n",
373 				  event_name, MSGTRACE_VERSION, hdr.version);
374 			/* Reset datalen to avoid display below */
375 			datalen = 0;
376 			break;
377 		}
378 
379 		/* There are 2 bytes available at the end of data */
380 		*(buf + sizeof(struct msgtrace_hdr)
381 			 + be16_to_cpu(hdr.len)) = '\0';
382 
383 		if (be32_to_cpu(hdr.discarded_bytes)
384 		    || be32_to_cpu(hdr.discarded_printf))
385 			brcmf_dbg(ERROR,
386 				  "WLC_E_TRACE: [Discarded traces in dongle -->"
387 				  " discarded_bytes %d discarded_printf %d]\n",
388 				  be32_to_cpu(hdr.discarded_bytes),
389 				  be32_to_cpu(hdr.discarded_printf));
390 
391 		nblost = be32_to_cpu(hdr.seqnum) - seqnum_prev - 1;
392 		if (nblost > 0)
393 			brcmf_dbg(ERROR, "WLC_E_TRACE: [Event lost --> seqnum "
394 				  " %d nblost %d\n", be32_to_cpu(hdr.seqnum),
395 				  nblost);
396 		seqnum_prev = be32_to_cpu(hdr.seqnum);
397 
398 		/* Display the trace buffer. Advance from \n to \n to
399 		 * avoid display big
400 		 * printf (issue with Linux printk )
401 		 */
402 		p = (char *)&buf[sizeof(struct msgtrace_hdr)];
403 		while ((s = strstr(p, "\n")) != NULL) {
404 			*s = '\0';
405 			pr_debug("%s\n", p);
406 			p = s + 1;
407 		}
408 		pr_debug("%s\n", p);
409 
410 		/* Reset datalen to avoid display below */
411 		datalen = 0;
412 		break;
413 
414 	case BRCMF_E_RSSI:
415 		brcmf_dbg(EVENT, "MACEVENT: %s %d\n",
416 			  event_name, be32_to_cpu(*((__be32 *)event_data)));
417 		break;
418 
419 	default:
420 		brcmf_dbg(EVENT,
421 			  "MACEVENT: %s %d, MAC %s, status %d, reason %d, "
422 			  "auth %d\n", event_name, event_type, eabuf,
423 			  (int)status, (int)reason, (int)auth_type);
424 		break;
425 	}
426 
427 	/* show any appended data */
428 	if (datalen) {
429 		buf = (unsigned char *) event_data;
430 		brcmf_dbg(EVENT, " data (%d) : ", datalen);
431 		for (i = 0; i < datalen; i++)
432 			brcmf_dbg(EVENT, " 0x%02x ", *buf++);
433 		brcmf_dbg(EVENT, "\n");
434 	}
435 }
436 #endif				/* DEBUG */
437 
438 int
brcmf_c_host_event(struct brcmf_pub * drvr,int * ifidx,void * pktdata,struct brcmf_event_msg * event,void ** data_ptr)439 brcmf_c_host_event(struct brcmf_pub *drvr, int *ifidx, void *pktdata,
440 		   struct brcmf_event_msg *event, void **data_ptr)
441 {
442 	/* check whether packet is a BRCM event pkt */
443 	struct brcmf_event *pvt_data = (struct brcmf_event *) pktdata;
444 	struct brcmf_if_event *ifevent;
445 	char *event_data;
446 	u32 type, status;
447 	u16 flags;
448 	int evlen;
449 
450 	if (memcmp(BRCM_OUI, &pvt_data->hdr.oui[0], DOT11_OUI_LEN)) {
451 		brcmf_dbg(ERROR, "mismatched OUI, bailing\n");
452 		return -EBADE;
453 	}
454 
455 	/* BRCM event pkt may be unaligned - use xxx_ua to load user_subtype. */
456 	if (get_unaligned_be16(&pvt_data->hdr.usr_subtype) !=
457 	    BCMILCP_BCM_SUBTYPE_EVENT) {
458 		brcmf_dbg(ERROR, "mismatched subtype, bailing\n");
459 		return -EBADE;
460 	}
461 
462 	*data_ptr = &pvt_data[1];
463 	event_data = *data_ptr;
464 
465 	/* memcpy since BRCM event pkt may be unaligned. */
466 	memcpy(event, &pvt_data->msg, sizeof(struct brcmf_event_msg));
467 
468 	type = get_unaligned_be32(&event->event_type);
469 	flags = get_unaligned_be16(&event->flags);
470 	status = get_unaligned_be32(&event->status);
471 	evlen = get_unaligned_be32(&event->datalen) +
472 		sizeof(struct brcmf_event);
473 
474 	switch (type) {
475 	case BRCMF_E_IF:
476 		ifevent = (struct brcmf_if_event *) event_data;
477 		brcmf_dbg(TRACE, "if event\n");
478 
479 		if (ifevent->ifidx > 0 && ifevent->ifidx < BRCMF_MAX_IFS) {
480 			if (ifevent->action == BRCMF_E_IF_ADD)
481 				brcmf_add_if(drvr->dev, ifevent->ifidx,
482 					     event->ifname,
483 					     pvt_data->eth.h_dest);
484 			else
485 				brcmf_del_if(drvr, ifevent->ifidx);
486 		} else {
487 			brcmf_dbg(ERROR, "Invalid ifidx %d for %s\n",
488 				  ifevent->ifidx, event->ifname);
489 		}
490 
491 		/* send up the if event: btamp user needs it */
492 		*ifidx = brcmf_ifname2idx(drvr, event->ifname);
493 		break;
494 
495 		/* These are what external supplicant/authenticator wants */
496 	case BRCMF_E_LINK:
497 	case BRCMF_E_ASSOC_IND:
498 	case BRCMF_E_REASSOC_IND:
499 	case BRCMF_E_DISASSOC_IND:
500 	case BRCMF_E_MIC_ERROR:
501 	default:
502 		/* Fall through: this should get _everything_  */
503 
504 		*ifidx = brcmf_ifname2idx(drvr, event->ifname);
505 		brcmf_dbg(TRACE, "MAC event %d, flags %x, status %x\n",
506 			  type, flags, status);
507 
508 		/* put it back to BRCMF_E_NDIS_LINK */
509 		if (type == BRCMF_E_NDIS_LINK) {
510 			u32 temp1;
511 			__be32 temp2;
512 
513 			temp1 = get_unaligned_be32(&event->event_type);
514 			brcmf_dbg(TRACE, "Converted to WLC_E_LINK type %d\n",
515 				  temp1);
516 
517 			temp2 = cpu_to_be32(BRCMF_E_NDIS_LINK);
518 			memcpy((void *)(&pvt_data->msg.event_type), &temp2,
519 			       sizeof(pvt_data->msg.event_type));
520 		}
521 		break;
522 	}
523 
524 #ifdef DEBUG
525 	brcmf_c_show_host_event(event, event_data);
526 #endif				/* DEBUG */
527 
528 	return 0;
529 }
530 
531 /* Convert user's input in hex pattern to byte-size mask */
brcmf_c_pattern_atoh(char * src,char * dst)532 static int brcmf_c_pattern_atoh(char *src, char *dst)
533 {
534 	int i;
535 	if (strncmp(src, "0x", 2) != 0 && strncmp(src, "0X", 2) != 0) {
536 		brcmf_dbg(ERROR, "Mask invalid format. Needs to start with 0x\n");
537 		return -EINVAL;
538 	}
539 	src = src + 2;		/* Skip past 0x */
540 	if (strlen(src) % 2 != 0) {
541 		brcmf_dbg(ERROR, "Mask invalid format. Length must be even.\n");
542 		return -EINVAL;
543 	}
544 	for (i = 0; *src != '\0'; i++) {
545 		unsigned long res;
546 		char num[3];
547 		strncpy(num, src, 2);
548 		num[2] = '\0';
549 		if (kstrtoul(num, 16, &res))
550 			return -EINVAL;
551 		dst[i] = (u8)res;
552 		src += 2;
553 	}
554 	return i;
555 }
556 
557 void
brcmf_c_pktfilter_offload_enable(struct brcmf_pub * drvr,char * arg,int enable,int master_mode)558 brcmf_c_pktfilter_offload_enable(struct brcmf_pub *drvr, char *arg, int enable,
559 			     int master_mode)
560 {
561 	unsigned long res;
562 	char *argv[8];
563 	int i = 0;
564 	const char *str;
565 	int buf_len;
566 	int str_len;
567 	char *arg_save = NULL, *arg_org = NULL;
568 	int rc;
569 	char buf[128];
570 	struct brcmf_pkt_filter_enable_le enable_parm;
571 	struct brcmf_pkt_filter_enable_le *pkt_filterp;
572 	__le32 mmode_le;
573 
574 	arg_save = kmalloc(strlen(arg) + 1, GFP_ATOMIC);
575 	if (!arg_save)
576 		goto fail;
577 
578 	arg_org = arg_save;
579 	memcpy(arg_save, arg, strlen(arg) + 1);
580 
581 	argv[i] = strsep(&arg_save, " ");
582 
583 	i = 0;
584 	if (NULL == argv[i]) {
585 		brcmf_dbg(ERROR, "No args provided\n");
586 		goto fail;
587 	}
588 
589 	str = "pkt_filter_enable";
590 	str_len = strlen(str);
591 	strncpy(buf, str, str_len);
592 	buf[str_len] = '\0';
593 	buf_len = str_len + 1;
594 
595 	pkt_filterp = (struct brcmf_pkt_filter_enable_le *) (buf + str_len + 1);
596 
597 	/* Parse packet filter id. */
598 	enable_parm.id = 0;
599 	if (!kstrtoul(argv[i], 0, &res))
600 		enable_parm.id = cpu_to_le32((u32)res);
601 
602 	/* Parse enable/disable value. */
603 	enable_parm.enable = cpu_to_le32(enable);
604 
605 	buf_len += sizeof(enable_parm);
606 	memcpy((char *)pkt_filterp, &enable_parm, sizeof(enable_parm));
607 
608 	/* Enable/disable the specified filter. */
609 	rc = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, buf, buf_len);
610 	rc = rc >= 0 ? 0 : rc;
611 	if (rc)
612 		brcmf_dbg(TRACE, "failed to add pktfilter %s, retcode = %d\n",
613 			  arg, rc);
614 	else
615 		brcmf_dbg(TRACE, "successfully added pktfilter %s\n", arg);
616 
617 	/* Contorl the master mode */
618 	mmode_le = cpu_to_le32(master_mode);
619 	brcmf_c_mkiovar("pkt_filter_mode", (char *)&mmode_le, 4, buf,
620 		    sizeof(buf));
621 	rc = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, buf,
622 				       sizeof(buf));
623 	rc = rc >= 0 ? 0 : rc;
624 	if (rc)
625 		brcmf_dbg(TRACE, "failed to add pktfilter %s, retcode = %d\n",
626 			  arg, rc);
627 
628 fail:
629 	kfree(arg_org);
630 }
631 
brcmf_c_pktfilter_offload_set(struct brcmf_pub * drvr,char * arg)632 void brcmf_c_pktfilter_offload_set(struct brcmf_pub *drvr, char *arg)
633 {
634 	const char *str;
635 	struct brcmf_pkt_filter_le pkt_filter;
636 	struct brcmf_pkt_filter_le *pkt_filterp;
637 	unsigned long res;
638 	int buf_len;
639 	int str_len;
640 	int rc;
641 	u32 mask_size;
642 	u32 pattern_size;
643 	char *argv[8], *buf = NULL;
644 	int i = 0;
645 	char *arg_save = NULL, *arg_org = NULL;
646 
647 	arg_save = kstrdup(arg, GFP_ATOMIC);
648 	if (!arg_save)
649 		goto fail;
650 
651 	arg_org = arg_save;
652 
653 	buf = kmalloc(PKTFILTER_BUF_SIZE, GFP_ATOMIC);
654 	if (!buf)
655 		goto fail;
656 
657 	argv[i] = strsep(&arg_save, " ");
658 	while (argv[i++])
659 		argv[i] = strsep(&arg_save, " ");
660 
661 	i = 0;
662 	if (NULL == argv[i]) {
663 		brcmf_dbg(ERROR, "No args provided\n");
664 		goto fail;
665 	}
666 
667 	str = "pkt_filter_add";
668 	strcpy(buf, str);
669 	str_len = strlen(str);
670 	buf_len = str_len + 1;
671 
672 	pkt_filterp = (struct brcmf_pkt_filter_le *) (buf + str_len + 1);
673 
674 	/* Parse packet filter id. */
675 	pkt_filter.id = 0;
676 	if (!kstrtoul(argv[i], 0, &res))
677 		pkt_filter.id = cpu_to_le32((u32)res);
678 
679 	if (NULL == argv[++i]) {
680 		brcmf_dbg(ERROR, "Polarity not provided\n");
681 		goto fail;
682 	}
683 
684 	/* Parse filter polarity. */
685 	pkt_filter.negate_match = 0;
686 	if (!kstrtoul(argv[i], 0, &res))
687 		pkt_filter.negate_match = cpu_to_le32((u32)res);
688 
689 	if (NULL == argv[++i]) {
690 		brcmf_dbg(ERROR, "Filter type not provided\n");
691 		goto fail;
692 	}
693 
694 	/* Parse filter type. */
695 	pkt_filter.type = 0;
696 	if (!kstrtoul(argv[i], 0, &res))
697 		pkt_filter.type = cpu_to_le32((u32)res);
698 
699 	if (NULL == argv[++i]) {
700 		brcmf_dbg(ERROR, "Offset not provided\n");
701 		goto fail;
702 	}
703 
704 	/* Parse pattern filter offset. */
705 	pkt_filter.u.pattern.offset = 0;
706 	if (!kstrtoul(argv[i], 0, &res))
707 		pkt_filter.u.pattern.offset = cpu_to_le32((u32)res);
708 
709 	if (NULL == argv[++i]) {
710 		brcmf_dbg(ERROR, "Bitmask not provided\n");
711 		goto fail;
712 	}
713 
714 	/* Parse pattern filter mask. */
715 	mask_size =
716 	    brcmf_c_pattern_atoh
717 		   (argv[i], (char *)pkt_filterp->u.pattern.mask_and_pattern);
718 
719 	if (NULL == argv[++i]) {
720 		brcmf_dbg(ERROR, "Pattern not provided\n");
721 		goto fail;
722 	}
723 
724 	/* Parse pattern filter pattern. */
725 	pattern_size =
726 	    brcmf_c_pattern_atoh(argv[i],
727 				   (char *)&pkt_filterp->u.pattern.
728 				   mask_and_pattern[mask_size]);
729 
730 	if (mask_size != pattern_size) {
731 		brcmf_dbg(ERROR, "Mask and pattern not the same size\n");
732 		goto fail;
733 	}
734 
735 	pkt_filter.u.pattern.size_bytes = cpu_to_le32(mask_size);
736 	buf_len += BRCMF_PKT_FILTER_FIXED_LEN;
737 	buf_len += (BRCMF_PKT_FILTER_PATTERN_FIXED_LEN + 2 * mask_size);
738 
739 	/* Keep-alive attributes are set in local
740 	 * variable (keep_alive_pkt), and
741 	 ** then memcpy'ed into buffer (keep_alive_pktp) since there is no
742 	 ** guarantee that the buffer is properly aligned.
743 	 */
744 	memcpy((char *)pkt_filterp,
745 	       &pkt_filter,
746 	       BRCMF_PKT_FILTER_FIXED_LEN + BRCMF_PKT_FILTER_PATTERN_FIXED_LEN);
747 
748 	rc = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, buf, buf_len);
749 	rc = rc >= 0 ? 0 : rc;
750 
751 	if (rc)
752 		brcmf_dbg(TRACE, "failed to add pktfilter %s, retcode = %d\n",
753 			  arg, rc);
754 	else
755 		brcmf_dbg(TRACE, "successfully added pktfilter %s\n", arg);
756 
757 fail:
758 	kfree(arg_org);
759 
760 	kfree(buf);
761 }
762 
brcmf_c_arp_offload_set(struct brcmf_pub * drvr,int arp_mode)763 static void brcmf_c_arp_offload_set(struct brcmf_pub *drvr, int arp_mode)
764 {
765 	char iovbuf[32];
766 	int retcode;
767 	__le32 arp_mode_le;
768 
769 	arp_mode_le = cpu_to_le32(arp_mode);
770 	brcmf_c_mkiovar("arp_ol", (char *)&arp_mode_le, 4, iovbuf,
771 			sizeof(iovbuf));
772 	retcode = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR,
773 				   iovbuf, sizeof(iovbuf));
774 	retcode = retcode >= 0 ? 0 : retcode;
775 	if (retcode)
776 		brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, retcode = %d\n",
777 			  arp_mode, retcode);
778 	else
779 		brcmf_dbg(TRACE, "successfully set ARP offload mode to 0x%x\n",
780 			  arp_mode);
781 }
782 
brcmf_c_arp_offload_enable(struct brcmf_pub * drvr,int arp_enable)783 static void brcmf_c_arp_offload_enable(struct brcmf_pub *drvr, int arp_enable)
784 {
785 	char iovbuf[32];
786 	int retcode;
787 	__le32 arp_enable_le;
788 
789 	arp_enable_le = cpu_to_le32(arp_enable);
790 
791 	brcmf_c_mkiovar("arpoe", (char *)&arp_enable_le, 4,
792 			iovbuf, sizeof(iovbuf));
793 	retcode = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR,
794 				   iovbuf, sizeof(iovbuf));
795 	retcode = retcode >= 0 ? 0 : retcode;
796 	if (retcode)
797 		brcmf_dbg(TRACE, "failed to enable ARP offload to %d, retcode = %d\n",
798 			  arp_enable, retcode);
799 	else
800 		brcmf_dbg(TRACE, "successfully enabled ARP offload to %d\n",
801 			  arp_enable);
802 }
803 
brcmf_c_preinit_dcmds(struct brcmf_pub * drvr)804 int brcmf_c_preinit_dcmds(struct brcmf_pub *drvr)
805 {
806 	char iovbuf[BRCMF_EVENTING_MASK_LEN + 12];	/*  Room for
807 				 "event_msgs" + '\0' + bitvec  */
808 	uint up = 0;
809 	char buf[128], *ptr;
810 	u32 dongle_align = drvr->bus_if->align;
811 	u32 glom = 0;
812 	__le32 roaming_le = cpu_to_le32(1);
813 	__le32 bcn_timeout_le = cpu_to_le32(3);
814 	__le32 scan_assoc_time_le = cpu_to_le32(40);
815 	__le32 scan_unassoc_time_le = cpu_to_le32(40);
816 	int i;
817 
818 	mutex_lock(&drvr->proto_block);
819 
820 	/* Set Country code */
821 	if (drvr->country_code[0] != 0) {
822 		if (brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_COUNTRY,
823 					      drvr->country_code,
824 					      sizeof(drvr->country_code)) < 0)
825 			brcmf_dbg(ERROR, "country code setting failed\n");
826 	}
827 
828 	/* query for 'ver' to get version info from firmware */
829 	memset(buf, 0, sizeof(buf));
830 	ptr = buf;
831 	brcmf_c_mkiovar("ver", NULL, 0, buf, sizeof(buf));
832 	brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, buf, sizeof(buf));
833 	strsep(&ptr, "\n");
834 	/* Print fw version info */
835 	brcmf_dbg(ERROR, "Firmware version = %s\n", buf);
836 
837 	/* Match Host and Dongle rx alignment */
838 	brcmf_c_mkiovar("bus:txglomalign", (char *)&dongle_align, 4, iovbuf,
839 		    sizeof(iovbuf));
840 	brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
841 				  sizeof(iovbuf));
842 
843 	/* disable glom option per default */
844 	brcmf_c_mkiovar("bus:txglom", (char *)&glom, 4, iovbuf, sizeof(iovbuf));
845 	brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
846 				  sizeof(iovbuf));
847 
848 	/* Setup timeout if Beacons are lost and roam is off to report
849 		 link down */
850 	brcmf_c_mkiovar("bcn_timeout", (char *)&bcn_timeout_le, 4, iovbuf,
851 		    sizeof(iovbuf));
852 	brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
853 				  sizeof(iovbuf));
854 
855 	/* Enable/Disable build-in roaming to allowed ext supplicant to take
856 		 of romaing */
857 	brcmf_c_mkiovar("roam_off", (char *)&roaming_le, 4,
858 		      iovbuf, sizeof(iovbuf));
859 	brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
860 				  sizeof(iovbuf));
861 
862 	/* Force STA UP */
863 	brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_UP, (char *)&up, sizeof(up));
864 
865 	/* Setup event_msgs */
866 	brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
867 		      iovbuf, sizeof(iovbuf));
868 	brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
869 				  sizeof(iovbuf));
870 
871 	brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_SCAN_CHANNEL_TIME,
872 		 (char *)&scan_assoc_time_le, sizeof(scan_assoc_time_le));
873 	brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_SCAN_UNASSOC_TIME,
874 		 (char *)&scan_unassoc_time_le, sizeof(scan_unassoc_time_le));
875 
876 	/* Set and enable ARP offload feature */
877 	brcmf_c_arp_offload_set(drvr, BRCMF_ARPOL_MODE);
878 	brcmf_c_arp_offload_enable(drvr, true);
879 
880 	/* Set up pkt filter */
881 	for (i = 0; i < drvr->pktfilter_count; i++) {
882 		brcmf_c_pktfilter_offload_set(drvr, drvr->pktfilter[i]);
883 		brcmf_c_pktfilter_offload_enable(drvr, drvr->pktfilter[i],
884 						 0, true);
885 	}
886 
887 	mutex_unlock(&drvr->proto_block);
888 
889 	return 0;
890 }
891