xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/rkwifi/bcmdhd_indep_power/dhd_linux.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Broadcom Dongle Host Driver (DHD), Linux-specific network interface
4  * Basically selected code segments from usb-cdc.c and usb-rndis.c
5  *
6  * Copyright (C) 1999-2017, Broadcom Corporation
7  *
8  *      Unless you and Broadcom execute a separate written software license
9  * agreement governing use of this software, this software is licensed to you
10  * under the terms of the GNU General Public License version 2 (the "GPL"),
11  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12  * following added to such license:
13  *
14  *      As a special exception, the copyright holders of this software give you
15  * permission to link this software with independent modules, and to copy and
16  * distribute the resulting executable under terms of your choice, provided that
17  * you also meet, for each linked independent module, the terms and conditions of
18  * the license of that module.  An independent module is a module which is not
19  * derived from this software.  The special exception does not apply to any
20  * modifications of the software.
21  *
22  *      Notwithstanding the above, under no circumstances may you combine this
23  * software in any way with any other Broadcom software provided under a license
24  * other than the GPL, without Broadcom's express prior written consent.
25  *
26  *
27  * <<Broadcom-WL-IPTag/Open:>>
28  *
29  * $Id: dhd_linux.c 710862 2017-07-14 07:43:59Z $
30  */
31 
32 #include <typedefs.h>
33 #include <linuxver.h>
34 #include <osl.h>
35 #ifdef SHOW_LOGTRACE
36 #include <linux/syscalls.h>
37 #include <event_log.h>
38 #endif /* SHOW_LOGTRACE */
39 
40 #include <linux/init.h>
41 #include <linux/kernel.h>
42 #include <linux/slab.h>
43 #include <linux/skbuff.h>
44 #include <linux/netdevice.h>
45 #include <linux/inetdevice.h>
46 #include <linux/rtnetlink.h>
47 #include <linux/etherdevice.h>
48 #include <linux/random.h>
49 #include <linux/spinlock.h>
50 #include <linux/ethtool.h>
51 #include <linux/fcntl.h>
52 #include <linux/fs.h>
53 #include <linux/ip.h>
54 #include <linux/reboot.h>
55 #include <linux/notifier.h>
56 #include <net/addrconf.h>
57 #ifdef ENABLE_ADAPTIVE_SCHED
58 #include <linux/cpufreq.h>
59 #endif /* ENABLE_ADAPTIVE_SCHED */
60 
61 #include <linux/uaccess.h>
62 #include <asm/unaligned.h>
63 
64 #include <epivers.h>
65 #include <bcmutils.h>
66 #include <bcmendian.h>
67 #include <bcmdevs.h>
68 
69 
70 #include <ethernet.h>
71 #include <bcmevent.h>
72 #include <vlan.h>
73 #include <802.3.h>
74 
75 #include <dngl_stats.h>
76 #include <dhd_linux_wq.h>
77 #include <dhd.h>
78 #include <dhd_linux.h>
79 #ifdef DHD_WET
80 #include <dhd_wet.h>
81 #endif /* DHD_WET */
82 #ifdef PCIE_FULL_DONGLE
83 #include <dhd_flowring.h>
84 #endif
85 #include <dhd_bus.h>
86 #include <dhd_proto.h>
87 #include <dhd_config.h>
88 #ifdef WL_ESCAN
89 #include <wl_escan.h>
90 #endif
91 #include <dhd_dbg.h>
92 #include <dhd_debug.h>
93 #ifdef CONFIG_HAS_WAKELOCK
94 #include <linux/wakelock.h>
95 #endif
96 #ifdef WL_CFG80211
97 #include <wl_cfg80211.h>
98 #endif
99 #ifdef PNO_SUPPORT
100 #include <dhd_pno.h>
101 #endif
102 #ifdef RTT_SUPPORT
103 #include <dhd_rtt.h>
104 #endif
105 #ifdef DHD_TIMESYNC
106 #include <dhd_timesync.h>
107 #endif /* DHD_TIMESYNC */
108 
109 #ifdef CSI_SUPPORT
110 #include <dhd_csi.h>
111 #endif /* CSI_SUPPORT */
112 
113 #ifdef CONFIG_COMPAT
114 #include <linux/compat.h>
115 #endif
116 
117 #if defined(CONFIG_SOC_EXYNOS8895)
118 #include <linux/exynos-pci-ctrl.h>
119 #endif /* CONFIG_SOC_EXYNOS8895 */
120 
121 #ifdef DHD_WMF
122 #include <dhd_wmf_linux.h>
123 #endif /* DHD_WMF */
124 
125 #ifdef DHD_L2_FILTER
126 #include <bcmicmp.h>
127 #include <bcm_l2_filter.h>
128 #include <dhd_l2_filter.h>
129 #endif /* DHD_L2_FILTER */
130 
131 #ifdef DHD_PSTA
132 #include <dhd_psta.h>
133 #endif /* DHD_PSTA */
134 
135 
136 #ifdef DHDTCPACK_SUPPRESS
137 #include <dhd_ip.h>
138 #endif /* DHDTCPACK_SUPPRESS */
139 #include <dhd_daemon.h>
140 #ifdef DHD_PKT_LOGGING
141 #include <dhd_pktlog.h>
142 #endif /* DHD_PKT_LOGGING */
143 #if defined(STAT_REPORT)
144 #include <wl_statreport.h>
145 #endif /* STAT_REPORT */
146 #ifdef DHD_DEBUG_PAGEALLOC
147 typedef void (*page_corrupt_cb_t)(void *handle, void *addr_corrupt, size_t len);
148 void dhd_page_corrupt_cb(void *handle, void *addr_corrupt, size_t len);
149 extern void register_page_corrupt_cb(page_corrupt_cb_t cb, void* handle);
150 #endif /* DHD_DEBUG_PAGEALLOC */
151 
152 static void dhd_tcp_dump(char *ifname, uint8 *pktdata, bool tx);
153 
154 #if defined(DHD_LB)
155 #if !defined(PCIE_FULL_DONGLE)
156 #error "DHD Loadbalancing only supported on PCIE_FULL_DONGLE"
157 #endif /* !PCIE_FULL_DONGLE */
158 #endif /* DHD_LB */
159 
160 #if defined(DHD_LB_RXP) || defined(DHD_LB_RXC) || defined(DHD_LB_TXC) || \
161 	defined(DHD_LB_STATS)
162 #if !defined(DHD_LB)
163 #error "DHD loadbalance derivatives are supported only if DHD_LB is defined"
164 #endif /* !DHD_LB */
165 #endif /* DHD_LB_RXP || DHD_LB_RXC || DHD_LB_TXC || DHD_LB_STATS */
166 
167 #if defined(DHD_LB)
168 /* Dynamic CPU selection for load balancing */
169 #include <linux/cpu.h>
170 #include <linux/cpumask.h>
171 #include <linux/notifier.h>
172 #include <linux/workqueue.h>
173 #include <asm/atomic.h>
174 
175 #if !defined(DHD_LB_PRIMARY_CPUS)
176 #define DHD_LB_PRIMARY_CPUS     0x0 /* Big CPU coreids mask */
177 #endif
178 #if !defined(DHD_LB_SECONDARY_CPUS)
179 #define DHD_LB_SECONDARY_CPUS   0xFE /* Little CPU coreids mask */
180 #endif
181 
182 #define HIST_BIN_SIZE	9
183 
184 static void dhd_rx_napi_dispatcher_fn(struct work_struct * work);
185 
186 #if defined(DHD_LB_TXP)
187 static void dhd_lb_tx_handler(unsigned long data);
188 static void dhd_tx_dispatcher_work(struct work_struct * work);
189 static void dhd_tx_dispatcher_fn(dhd_pub_t *dhdp);
190 static void dhd_lb_tx_dispatch(dhd_pub_t *dhdp);
191 
192 /* Pkttag not compatible with PROP_TXSTATUS or WLFC */
193 typedef struct dhd_tx_lb_pkttag_fr {
194 	struct net_device *net;
195 	int ifidx;
196 } dhd_tx_lb_pkttag_fr_t;
197 
198 #define DHD_LB_TX_PKTTAG_SET_NETDEV(tag, netdevp)	((tag)->net = netdevp)
199 #define DHD_LB_TX_PKTTAG_NETDEV(tag)			((tag)->net)
200 
201 #define DHD_LB_TX_PKTTAG_SET_IFIDX(tag, ifidx)	((tag)->ifidx = ifidx)
202 #define DHD_LB_TX_PKTTAG_IFIDX(tag)		((tag)->ifidx)
203 #endif /* DHD_LB_TXP */
204 #endif /* DHD_LB */
205 
206 #ifdef HOFFLOAD_MODULES
207 #include <linux/firmware.h>
208 #endif
209 
210 #ifdef WLMEDIA_HTSF
211 #include <linux/time.h>
212 #include <htsf.h>
213 
214 #define HTSF_MINLEN 200    /* min. packet length to timestamp */
215 #define HTSF_BUS_DELAY 150 /* assume a fix propagation in us  */
216 #define TSMAX  1000        /* max no. of timing record kept   */
217 #define NUMBIN 34
218 
219 static uint32 tsidx = 0;
220 static uint32 htsf_seqnum = 0;
221 uint32 tsfsync;
222 struct timeval tsync;
223 static uint32 tsport = 5010;
224 
225 typedef struct histo_ {
226 	uint32 bin[NUMBIN];
227 } histo_t;
228 
229 #if !ISPOWEROF2(DHD_SDALIGN)
230 #error DHD_SDALIGN is not a power of 2!
231 #endif
232 
233 static histo_t vi_d1, vi_d2, vi_d3, vi_d4;
234 #endif /* WLMEDIA_HTSF */
235 
236 #ifdef WL_MONITOR
237 #include <bcmmsgbuf.h>
238 #include <bcmwifi_monitor.h>
239 #endif
240 
241 #define htod32(i) (i)
242 #define htod16(i) (i)
243 #define dtoh32(i) (i)
244 #define dtoh16(i) (i)
245 #define htodchanspec(i) (i)
246 #define dtohchanspec(i) (i)
247 
248 #ifdef STBLINUX
249 #ifdef quote_str
250 #undef quote_str
251 #endif /* quote_str */
252 #ifdef to_str
253 #undef to_str
254 #endif /* quote_str */
255 #define to_str(s) #s
256 #define quote_str(s) to_str(s)
257 
258 static char *driver_target = "driver_target: "quote_str(BRCM_DRIVER_TARGET);
259 #endif /* STBLINUX */
260 
261 
262 
263 #if defined(SOFTAP)
264 extern bool ap_cfg_running;
265 extern bool ap_fw_loaded;
266 #endif
267 
268 extern void dhd_dump_eapol_4way_message(dhd_pub_t *dhd, int ifidx,
269 	char *dump_data, bool direction);
270 
271 #ifdef FIX_CPU_MIN_CLOCK
272 #include <linux/pm_qos.h>
273 #endif /* FIX_CPU_MIN_CLOCK */
274 
275 #ifdef SET_RANDOM_MAC_SOFTAP
276 #ifndef CONFIG_DHD_SET_RANDOM_MAC_VAL
277 #define CONFIG_DHD_SET_RANDOM_MAC_VAL	0x001A11
278 #endif
279 static u32 vendor_oui = CONFIG_DHD_SET_RANDOM_MAC_VAL;
280 #endif /* SET_RANDOM_MAC_SOFTAP */
281 
282 #ifdef ENABLE_ADAPTIVE_SCHED
283 #define DEFAULT_CPUFREQ_THRESH		1000000	/* threshold frequency : 1000000 = 1GHz */
284 #ifndef CUSTOM_CPUFREQ_THRESH
285 #define CUSTOM_CPUFREQ_THRESH	DEFAULT_CPUFREQ_THRESH
286 #endif /* CUSTOM_CPUFREQ_THRESH */
287 #endif /* ENABLE_ADAPTIVE_SCHED */
288 
289 /* enable HOSTIP cache update from the host side when an eth0:N is up */
290 #define AOE_IP_ALIAS_SUPPORT 1
291 
292 #ifdef BCM_FD_AGGR
293 #include <bcm_rpc.h>
294 #include <bcm_rpc_tp.h>
295 #endif
296 #ifdef PROP_TXSTATUS
297 #include <wlfc_proto.h>
298 #include <dhd_wlfc.h>
299 #endif
300 
301 #include <wl_android.h>
302 
303 /* Maximum STA per radio */
304 #define DHD_MAX_STA     32
305 
306 
307 
308 const uint8 wme_fifo2ac[] = { 0, 1, 2, 3, 1, 1 };
309 const uint8 prio2fifo[8] = { 1, 0, 0, 1, 2, 2, 3, 3 };
310 #define WME_PRIO2AC(prio)  wme_fifo2ac[prio2fifo[(prio)]]
311 
312 #ifdef ARP_OFFLOAD_SUPPORT
313 void aoe_update_host_ipv4_table(dhd_pub_t *dhd_pub, u32 ipa, bool add, int idx);
314 static int dhd_inetaddr_notifier_call(struct notifier_block *this,
315 	unsigned long event, void *ptr);
316 static struct notifier_block dhd_inetaddr_notifier = {
317 	.notifier_call = dhd_inetaddr_notifier_call
318 };
319 /* to make sure we won't register the same notifier twice, otherwise a loop is likely to be
320  * created in kernel notifier link list (with 'next' pointing to itself)
321  */
322 static bool dhd_inetaddr_notifier_registered = FALSE;
323 #endif /* ARP_OFFLOAD_SUPPORT */
324 
325 #if defined(CONFIG_IPV6) && defined(IPV6_NDO_SUPPORT)
326 int dhd_inet6addr_notifier_call(struct notifier_block *this,
327 	unsigned long event, void *ptr);
328 static struct notifier_block dhd_inet6addr_notifier = {
329 	.notifier_call = dhd_inet6addr_notifier_call
330 };
331 /* to make sure we won't register the same notifier twice, otherwise a loop is likely to be
332  * created in kernel notifier link list (with 'next' pointing to itself)
333  */
334 static bool dhd_inet6addr_notifier_registered = FALSE;
335 #endif /* CONFIG_IPV6 && IPV6_NDO_SUPPORT */
336 
337 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP)
338 #include <linux/suspend.h>
339 volatile bool dhd_mmc_suspend = FALSE;
340 DECLARE_WAIT_QUEUE_HEAD(dhd_dpc_wait);
341 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && defined(CONFIG_PM_SLEEP) */
342 
343 #if defined(OOB_INTR_ONLY) || defined(FORCE_WOWLAN)
344 extern void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable);
345 #endif
346 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
347 static void dhd_hang_process(void *dhd_info, void *event_data, u8 event);
348 #endif
349 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
350 MODULE_LICENSE("GPL and additional rights");
351 #endif /* LinuxVer */
352 
353 #if defined(MULTIPLE_SUPPLICANT)
354 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
355 DEFINE_MUTEX(_dhd_mutex_lock_);
356 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)) */
357 #endif
358 static int dhd_suspend_resume_helper(struct dhd_info *dhd, int val, int force);
359 
360 #ifdef CONFIG_BCM_DETECT_CONSECUTIVE_HANG
361 #define MAX_CONSECUTIVE_HANG_COUNTS 5
362 #endif /* CONFIG_BCM_DETECT_CONSECUTIVE_HANG */
363 
364 #include <dhd_bus.h>
365 
366 #ifdef DHD_ULP
367 #include <dhd_ulp.h>
368 #endif /* DHD_ULP */
369 
370 #ifdef BCM_FD_AGGR
371 #define DBUS_RX_BUFFER_SIZE_DHD(net)	(BCM_RPC_TP_DNGL_AGG_MAX_BYTE)
372 #else
373 #ifndef PROP_TXSTATUS
374 #define DBUS_RX_BUFFER_SIZE_DHD(net)	(net->mtu + net->hard_header_len + dhd->pub.hdrlen)
375 #else
376 #define DBUS_RX_BUFFER_SIZE_DHD(net)	(net->mtu + net->hard_header_len + dhd->pub.hdrlen + 128)
377 #endif
378 #endif /* BCM_FD_AGGR */
379 
380 #ifdef PROP_TXSTATUS
381 extern bool dhd_wlfc_skip_fc(void * dhdp, uint8 idx);
382 extern void dhd_wlfc_plat_init(void *dhd);
383 extern void dhd_wlfc_plat_deinit(void *dhd);
384 #endif /* PROP_TXSTATUS */
385 #ifdef USE_DYNAMIC_F2_BLKSIZE
386 extern uint sd_f2_blocksize;
387 extern int dhdsdio_func_blocksize(dhd_pub_t *dhd, int function_num, int block_size);
388 #endif /* USE_DYNAMIC_F2_BLKSIZE */
389 
390 #if LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 15)
391 const char *
print_tainted()392 print_tainted()
393 {
394 	return "";
395 }
396 #endif	/* LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 15) */
397 
398 /* Linux wireless extension support */
399 #if defined(WL_WIRELESS_EXT)
400 #include <wl_iw.h>
401 extern wl_iw_extra_params_t  g_wl_iw_params;
402 #endif /* defined(WL_WIRELESS_EXT) */
403 
404 #ifdef CONFIG_PARTIALSUSPEND_SLP
405 #include <linux/partialsuspend_slp.h>
406 #define CONFIG_HAS_EARLYSUSPEND
407 #define DHD_USE_EARLYSUSPEND
408 #define register_early_suspend		register_pre_suspend
409 #define unregister_early_suspend	unregister_pre_suspend
410 #define early_suspend				pre_suspend
411 #define EARLY_SUSPEND_LEVEL_BLANK_SCREEN		50
412 #else
413 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
414 #include <linux/earlysuspend.h>
415 #endif /* defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND) */
416 #endif /* CONFIG_PARTIALSUSPEND_SLP */
417 
418 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
419 #include <linux/nl80211.h>
420 #endif /* OEM_ANDROID && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) */
421 
422 #if defined(BCMPCIE)
423 extern int dhd_get_suspend_bcn_li_dtim(dhd_pub_t *dhd, int *dtim_period, int *bcn_interval);
424 #else
425 extern int dhd_get_suspend_bcn_li_dtim(dhd_pub_t *dhd);
426 #endif /* OEM_ANDROID && BCMPCIE */
427 
428 #ifdef PKT_FILTER_SUPPORT
429 extern void dhd_pktfilter_offload_set(dhd_pub_t * dhd, char *arg);
430 extern void dhd_pktfilter_offload_enable(dhd_pub_t * dhd, char *arg, int enable, int master_mode);
431 extern void dhd_pktfilter_offload_delete(dhd_pub_t *dhd, int id);
432 #endif
433 
434 #if defined(PKT_FILTER_SUPPORT) && defined(APF)
435 static int __dhd_apf_add_filter(struct net_device *ndev, uint32 filter_id,
436 	u8* program, uint32 program_len);
437 static int __dhd_apf_config_filter(struct net_device *ndev, uint32 filter_id,
438 	uint32 mode, uint32 enable);
439 static int __dhd_apf_delete_filter(struct net_device *ndev, uint32 filter_id);
440 #endif /* PKT_FILTER_SUPPORT && APF */
441 
442 
443 
argos_register_notifier_init(struct net_device * net)444 static INLINE int argos_register_notifier_init(struct net_device *net) { return 0;}
argos_register_notifier_deinit(void)445 static INLINE int argos_register_notifier_deinit(void) { return 0;}
446 
447 #if defined(BT_OVER_SDIO)
448 extern void wl_android_set_wifi_on_flag(bool enable);
449 #endif /* BT_OVER_SDIO */
450 
451 
452 #if defined(TRAFFIC_MGMT_DWM)
453 void traffic_mgmt_pkt_set_prio(dhd_pub_t *dhdp, void * pktbuf);
454 #endif
455 
456 #ifdef DHD_FW_COREDUMP
457 static void dhd_mem_dump(void *dhd_info, void *event_info, u8 event);
458 #endif /* DHD_FW_COREDUMP */
459 #ifdef DHD_LOG_DUMP
460 #define DLD_BUFFER_NUM  2
461 /* [0]: General, [1]: Special */
462 struct dhd_log_dump_buf g_dld_buf[DLD_BUFFER_NUM];
463 static const int dld_buf_size[] = {
464 	(1024 * 1024),	/* DHD_LOG_DUMP_BUFFER_SIZE */
465 	(8 * 1024)	/* DHD_LOG_DUMP_BUFFER_EX_SIZE */
466 };
467 static void dhd_log_dump_init(dhd_pub_t *dhd);
468 static void dhd_log_dump_deinit(dhd_pub_t *dhd);
469 static void dhd_log_dump(void *handle, void *event_info, u8 event);
470 void dhd_schedule_log_dump(dhd_pub_t *dhdp);
471 static int do_dhd_log_dump(dhd_pub_t *dhdp);
472 #endif /* DHD_LOG_DUMP */
473 
474 #ifdef DHD_DEBUG_UART
475 #include <linux/kmod.h>
476 #define DHD_DEBUG_UART_EXEC_PATH	"/system/bin/wldu"
477 static void dhd_debug_uart_exec_rd(void *handle, void *event_info, u8 event);
478 static void dhd_debug_uart_exec(dhd_pub_t *dhdp, char *cmd);
479 #endif	/* DHD_DEBUG_UART */
480 
481 static int dhd_reboot_callback(struct notifier_block *this, unsigned long code, void *unused);
482 static struct notifier_block dhd_reboot_notifier = {
483 	.notifier_call = dhd_reboot_callback,
484 	.priority = 1,
485 };
486 
487 #ifdef BCMPCIE
488 static int is_reboot = 0;
489 #endif /* BCMPCIE */
490 
491 #if defined(BT_OVER_SDIO)
492 #include "dhd_bt_interface.h"
493 dhd_pub_t	*g_dhd_pub = NULL;
494 #endif /* defined (BT_OVER_SDIO) */
495 
496 atomic_t exit_in_progress = ATOMIC_INIT(0);
497 
498 typedef struct dhd_if_event {
499 	struct list_head	list;
500 	wl_event_data_if_t	event;
501 	char			name[IFNAMSIZ+1];
502 	uint8			mac[ETHER_ADDR_LEN];
503 } dhd_if_event_t;
504 
505 /* Interface control information */
506 typedef struct dhd_if {
507 	struct dhd_info *info;			/* back pointer to dhd_info */
508 	/* OS/stack specifics */
509 	struct net_device *net;
510 	int				idx;			/* iface idx in dongle */
511 	uint			subunit;		/* subunit */
512 	uint8			mac_addr[ETHER_ADDR_LEN];	/* assigned MAC address */
513 	bool			set_macaddress;
514 	bool			set_multicast;
515 	uint8			bssidx;			/* bsscfg index for the interface */
516 	bool			attached;		/* Delayed attachment when unset */
517 	bool			txflowcontrol;	/* Per interface flow control indicator */
518 	char			name[IFNAMSIZ+1]; /* linux interface name */
519 	char			dngl_name[IFNAMSIZ+1]; /* corresponding dongle interface name */
520 	struct net_device_stats stats;
521 #ifdef DHD_WMF
522 	dhd_wmf_t		wmf;		/* per bsscfg wmf setting */
523 	bool	wmf_psta_disable;		/* enable/disable MC pkt to each mac
524 						 * of MC group behind PSTA
525 						 */
526 #endif /* DHD_WMF */
527 #ifdef PCIE_FULL_DONGLE
528 	struct list_head sta_list;		/* sll of associated stations */
529 #if !defined(BCM_GMAC3)
530 	spinlock_t	sta_list_lock;		/* lock for manipulating sll */
531 #endif /* ! BCM_GMAC3 */
532 #endif /* PCIE_FULL_DONGLE */
533 	uint32  ap_isolate;			/* ap-isolation settings */
534 #ifdef DHD_L2_FILTER
535 	bool parp_enable;
536 	bool parp_discard;
537 	bool parp_allnode;
538 	arp_table_t *phnd_arp_table;
539 	/* for Per BSS modification */
540 	bool dhcp_unicast;
541 	bool block_ping;
542 	bool grat_arp;
543 #endif /* DHD_L2_FILTER */
544 #ifdef DHD_MCAST_REGEN
545 	bool mcast_regen_bss_enable;
546 #endif
547 	bool rx_pkt_chainable;		/* set all rx packet to chainable config by default */
548 	cumm_ctr_t cumm_ctr;			/* cummulative queue length of child flowrings */
549 } dhd_if_t;
550 
551 #ifdef WLMEDIA_HTSF
552 typedef struct {
553 	uint32 low;
554 	uint32 high;
555 } tsf_t;
556 
557 typedef struct {
558 	uint32 last_cycle;
559 	uint32 last_sec;
560 	uint32 last_tsf;
561 	uint32 coef;     /* scaling factor */
562 	uint32 coefdec1; /* first decimal  */
563 	uint32 coefdec2; /* second decimal */
564 } htsf_t;
565 
566 typedef struct {
567 	uint32 t1;
568 	uint32 t2;
569 	uint32 t3;
570 	uint32 t4;
571 } tstamp_t;
572 
573 static tstamp_t ts[TSMAX];
574 static tstamp_t maxdelayts;
575 static uint32 maxdelay = 0, tspktcnt = 0, maxdelaypktno = 0;
576 
577 #endif  /* WLMEDIA_HTSF */
578 
579 struct ipv6_work_info_t {
580 	uint8			if_idx;
581 	char			ipv6_addr[IPV6_ADDR_LEN];
582 	unsigned long		event;
583 };
584 static void dhd_process_daemon_msg(struct sk_buff *skb);
585 static void dhd_destroy_to_notifier_skt(void);
586 static int dhd_create_to_notifier_skt(void);
587 static struct sock *nl_to_event_sk = NULL;
588 int sender_pid = 0;
589 
590 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
591 struct netlink_kernel_cfg g_cfg = {
592 	.groups = 1,
593 	.input = dhd_process_daemon_msg,
594 };
595 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)) */
596 
597 typedef struct dhd_dump {
598 	uint8 *buf;
599 	int bufsize;
600 } dhd_dump_t;
601 
602 
603 /* When Perimeter locks are deployed, any blocking calls must be preceeded
604  * with a PERIM UNLOCK and followed by a PERIM LOCK.
605  * Examples of blocking calls are: schedule_timeout(), down_interruptible(),
606  * wait_event_timeout().
607  */
608 
609 /* Local private structure (extension of pub) */
610 typedef struct dhd_info {
611 #if defined(WL_WIRELESS_EXT)
612 	wl_iw_t		iw;		/* wireless extensions state (must be first) */
613 #endif /* defined(WL_WIRELESS_EXT) */
614 	dhd_pub_t pub;
615 	dhd_if_t *iflist[DHD_MAX_IFS]; /* for supporting multiple interfaces */
616 
617 	wifi_adapter_info_t *adapter;			/* adapter information, interrupt, fw path etc. */
618 	char fw_path[PATH_MAX];		/* path to firmware image */
619 	char nv_path[PATH_MAX];		/* path to nvram vars file */
620 	char clm_path[PATH_MAX];		/* path to clm vars file */
621 	char conf_path[PATH_MAX];	/* path to config vars file */
622 #ifdef DHD_UCODE_DOWNLOAD
623 	char uc_path[PATH_MAX];	/* path to ucode image */
624 #endif /* DHD_UCODE_DOWNLOAD */
625 
626 	/* serialize dhd iovars */
627 	struct mutex dhd_iovar_mutex;
628 
629 	struct semaphore proto_sem;
630 #ifdef PROP_TXSTATUS
631 	spinlock_t	wlfc_spinlock;
632 
633 #ifdef BCMDBUS
634 	ulong		wlfc_lock_flags;
635 	ulong		wlfc_pub_lock_flags;
636 #endif /* BCMDBUS */
637 #endif /* PROP_TXSTATUS */
638 #ifdef WLMEDIA_HTSF
639 	htsf_t  htsf;
640 #endif
641 	wait_queue_head_t ioctl_resp_wait;
642 	wait_queue_head_t d3ack_wait;
643 	wait_queue_head_t dhd_bus_busy_state_wait;
644 	uint32	default_wd_interval;
645 
646 	timer_list_compat_t timer;
647 	bool wd_timer_valid;
648 #ifdef DHD_PCIE_RUNTIMEPM
649 	timer_list_compat_t rpm_timer;
650 	bool rpm_timer_valid;
651 	tsk_ctl_t	  thr_rpm_ctl;
652 #endif /* DHD_PCIE_RUNTIMEPM */
653 	struct tasklet_struct tasklet;
654 	spinlock_t	sdlock;
655 	spinlock_t	txqlock;
656 	spinlock_t	rxqlock;
657 	spinlock_t	dhd_lock;
658 #ifdef BCMDBUS
659 	ulong		txqlock_flags;
660 #else
661 
662 	struct semaphore sdsem;
663 	tsk_ctl_t	thr_dpc_ctl;
664 	tsk_ctl_t	thr_wdt_ctl;
665 #endif /* BCMDBUS */
666 
667 	tsk_ctl_t	thr_rxf_ctl;
668 	spinlock_t	rxf_lock;
669 	bool		rxthread_enabled;
670 
671 	/* Wakelocks */
672 #if defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
673 	struct wake_lock wl_wifi;   /* Wifi wakelock */
674 	struct wake_lock wl_rxwake; /* Wifi rx wakelock */
675 	struct wake_lock wl_ctrlwake; /* Wifi ctrl wakelock */
676 	struct wake_lock wl_wdwake; /* Wifi wd wakelock */
677 	struct wake_lock wl_evtwake; /* Wifi event wakelock */
678 	struct wake_lock wl_pmwake;   /* Wifi pm handler wakelock */
679 	struct wake_lock wl_txflwake; /* Wifi tx flow wakelock */
680 #ifdef BCMPCIE_OOB_HOST_WAKE
681 	struct wake_lock wl_intrwake; /* Host wakeup wakelock */
682 #endif /* BCMPCIE_OOB_HOST_WAKE */
683 #ifdef DHD_USE_SCAN_WAKELOCK
684 	struct wake_lock wl_scanwake;  /* Wifi scan wakelock */
685 #endif /* DHD_USE_SCAN_WAKELOCK */
686 #endif /* CONFIG_HAS_WAKELOCK && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27) */
687 
688 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
689 	/* net_device interface lock, prevent race conditions among net_dev interface
690 	 * calls and wifi_on or wifi_off
691 	 */
692 	struct mutex dhd_net_if_mutex;
693 	struct mutex dhd_suspend_mutex;
694 #if defined(PKT_FILTER_SUPPORT) && defined(APF)
695 	struct mutex dhd_apf_mutex;
696 #endif /* PKT_FILTER_SUPPORT && APF */
697 #endif
698 	spinlock_t wakelock_spinlock;
699 	spinlock_t wakelock_evt_spinlock;
700 	uint32 wakelock_counter;
701 	int wakelock_wd_counter;
702 	int wakelock_rx_timeout_enable;
703 	int wakelock_ctrl_timeout_enable;
704 	bool waive_wakelock;
705 	uint32 wakelock_before_waive;
706 
707 	/* Thread to issue ioctl for multicast */
708 	wait_queue_head_t ctrl_wait;
709 	atomic_t pend_8021x_cnt;
710 	dhd_attach_states_t dhd_state;
711 #ifdef SHOW_LOGTRACE
712 	dhd_event_log_t event_data;
713 #endif /* SHOW_LOGTRACE */
714 
715 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
716 	struct early_suspend early_suspend;
717 #endif /* CONFIG_HAS_EARLYSUSPEND && DHD_USE_EARLYSUSPEND */
718 
719 #ifdef ARP_OFFLOAD_SUPPORT
720 	u32 pend_ipaddr;
721 #endif /* ARP_OFFLOAD_SUPPORT */
722 #ifdef BCM_FD_AGGR
723 	void *rpc_th;
724 	void *rpc_osh;
725 	timer_list_compat_t rpcth_timer;
726 	bool rpcth_timer_active;
727 	uint8 fdaggr;
728 #endif
729 #ifdef DHDTCPACK_SUPPRESS
730 	spinlock_t	tcpack_lock;
731 #endif /* DHDTCPACK_SUPPRESS */
732 #ifdef FIX_CPU_MIN_CLOCK
733 	bool cpufreq_fix_status;
734 	struct mutex cpufreq_fix;
735 	struct pm_qos_request dhd_cpu_qos;
736 #ifdef FIX_BUS_MIN_CLOCK
737 	struct pm_qos_request dhd_bus_qos;
738 #endif /* FIX_BUS_MIN_CLOCK */
739 #endif /* FIX_CPU_MIN_CLOCK */
740 	void			*dhd_deferred_wq;
741 #ifdef DEBUG_CPU_FREQ
742 	struct notifier_block freq_trans;
743 	int __percpu *new_freq;
744 #endif
745 	unsigned int unit;
746 	struct notifier_block pm_notifier;
747 #ifdef DHD_PSTA
748 	uint32	psta_mode;	/* PSTA or PSR */
749 #endif /* DHD_PSTA */
750 #ifdef DHD_WET
751 	        uint32  wet_mode;
752 #endif /* DHD_WET */
753 #ifdef DHD_DEBUG
754 	dhd_dump_t *dump;
755 	timer_list_compat_t join_timer;
756 	u32 join_timeout_val;
757 	bool join_timer_active;
758 	uint scan_time_count;
759 	timer_list_compat_t scan_timer;
760 	bool scan_timer_active;
761 #endif
762 #if defined(DHD_LB)
763 	/* CPU Load Balance dynamic CPU selection */
764 
765 	/* Variable that tracks the currect CPUs available for candidacy */
766 	cpumask_var_t cpumask_curr_avail;
767 
768 	/* Primary and secondary CPU mask */
769 	cpumask_var_t cpumask_primary, cpumask_secondary; /* configuration */
770 	cpumask_var_t cpumask_primary_new, cpumask_secondary_new; /* temp */
771 
772 	struct notifier_block cpu_notifier;
773 
774 	/* Tasklet to handle Tx Completion packet freeing */
775 	struct tasklet_struct tx_compl_tasklet;
776 	atomic_t                   tx_compl_cpu;
777 
778 	/* Tasklet to handle RxBuf Post during Rx completion */
779 	struct tasklet_struct rx_compl_tasklet;
780 	atomic_t                   rx_compl_cpu;
781 
782 	/* Napi struct for handling rx packet sendup. Packets are removed from
783 	 * H2D RxCompl ring and placed into rx_pend_queue. rx_pend_queue is then
784 	 * appended to rx_napi_queue (w/ lock) and the rx_napi_struct is scheduled
785 	 * to run to rx_napi_cpu.
786 	 */
787 	struct sk_buff_head   rx_pend_queue  ____cacheline_aligned;
788 	struct sk_buff_head   rx_napi_queue  ____cacheline_aligned;
789 	struct napi_struct    rx_napi_struct ____cacheline_aligned;
790 	atomic_t                   rx_napi_cpu; /* cpu on which the napi is dispatched */
791 	struct net_device    *rx_napi_netdev; /* netdev of primary interface */
792 
793 	struct work_struct    rx_napi_dispatcher_work;
794 	struct work_struct	  tx_compl_dispatcher_work;
795 	struct work_struct    tx_dispatcher_work;
796 
797 	/* Number of times DPC Tasklet ran */
798 	uint32	dhd_dpc_cnt;
799 	/* Number of times NAPI processing got scheduled */
800 	uint32	napi_sched_cnt;
801 	/* Number of times NAPI processing ran on each available core */
802 	uint32	*napi_percpu_run_cnt;
803 	/* Number of times RX Completions got scheduled */
804 	uint32	rxc_sched_cnt;
805 	/* Number of times RX Completion ran on each available core */
806 	uint32	*rxc_percpu_run_cnt;
807 	/* Number of times TX Completions got scheduled */
808 	uint32	txc_sched_cnt;
809 	/* Number of times TX Completions ran on each available core */
810 	uint32	*txc_percpu_run_cnt;
811 	/* CPU status */
812 	/* Number of times each CPU came online */
813 	uint32	*cpu_online_cnt;
814 	/* Number of times each CPU went offline */
815 	uint32	*cpu_offline_cnt;
816 
817 	/* Number of times TX processing run on each core */
818 	uint32	*txp_percpu_run_cnt;
819 	/* Number of times TX start run on each core */
820 	uint32	*tx_start_percpu_run_cnt;
821 
822 	/* Tx load balancing */
823 
824 	/* TODO: Need to see if batch processing is really required in case of TX
825 	 * processing. In case of RX the Dongle can send a bunch of rx completions,
826 	 * hence we took a 3 queue approach
827 	 * enque - adds the skbs to rx_pend_queue
828 	 * dispatch - uses a lock and adds the list of skbs from pend queue to
829 	 *            napi queue
830 	 * napi processing - copies the pend_queue into a local queue and works
831 	 * on it.
832 	 * But for TX its going to be 1 skb at a time, so we are just thinking
833 	 * of using only one queue and use the lock supported skb queue functions
834 	 * to add and process it. If its in-efficient we'll re-visit the queue
835 	 * design.
836 	 */
837 
838 	/* When the NET_TX tries to send a TX packet put it into tx_pend_queue */
839 	/* struct sk_buff_head		tx_pend_queue  ____cacheline_aligned;  */
840 	/*
841 	 * From the Tasklet that actually sends out data
842 	 * copy the list tx_pend_queue into tx_active_queue. There by we need
843 	 * to spinlock to only perform the copy the rest of the code ie to
844 	 * construct the tx_pend_queue and the code to process tx_active_queue
845 	 * can be lockless. The concept is borrowed as is from RX processing
846 	 */
847 	/* struct sk_buff_head		tx_active_queue  ____cacheline_aligned; */
848 
849 	/* Control TXP in runtime, enable by default */
850 	atomic_t                lb_txp_active;
851 
852 	/*
853 	 * When the NET_TX tries to send a TX packet put it into tx_pend_queue
854 	 * For now, the processing tasklet will also direcly operate on this
855 	 * queue
856 	 */
857 	struct sk_buff_head	tx_pend_queue  ____cacheline_aligned;
858 
859 	/* cpu on which the DHD Tx is happenning */
860 	atomic_t		tx_cpu;
861 
862 	/* CPU on which the Network stack is calling the DHD's xmit function */
863 	atomic_t		net_tx_cpu;
864 
865 	/* Tasklet context from which the DHD's TX processing happens */
866 	struct tasklet_struct tx_tasklet;
867 
868 	/*
869 	 * Consumer Histogram - NAPI RX Packet processing
870 	 * -----------------------------------------------
871 	 * On Each CPU, when the NAPI RX Packet processing call back was invoked
872 	 * how many packets were processed is captured in this data structure.
873 	 * Now its difficult to capture the "exact" number of packets processed.
874 	 * So considering the packet counter to be a 32 bit one, we have a
875 	 * bucket with 8 bins (2^1, 2^2 ... 2^8). The "number" of packets
876 	 * processed is rounded off to the next power of 2 and put in the
877 	 * approriate "bin" the value in the bin gets incremented.
878 	 * For example, assume that in CPU 1 if NAPI Rx runs 3 times
879 	 * and the packet count processed is as follows (assume the bin counters are 0)
880 	 * iteration 1 - 10 (the bin counter 2^4 increments to 1)
881 	 * iteration 2 - 30 (the bin counter 2^5 increments to 1)
882 	 * iteration 3 - 15 (the bin counter 2^4 increments by 1 to become 2)
883 	 */
884 	uint32 *napi_rx_hist[HIST_BIN_SIZE];
885 	uint32 *txc_hist[HIST_BIN_SIZE];
886 	uint32 *rxc_hist[HIST_BIN_SIZE];
887 #endif /* DHD_LB */
888 
889 #ifdef SHOW_LOGTRACE
890 	struct work_struct	  event_log_dispatcher_work;
891 #endif /* SHOW_LOGTRACE */
892 
893 #if defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW)
894 #endif /* defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW) */
895 	struct kobject dhd_kobj;
896 #ifdef SHOW_LOGTRACE
897 	struct sk_buff_head   evt_trace_queue     ____cacheline_aligned;
898 #endif
899 	timer_list_compat_t timesync_timer;
900 #if defined(BT_OVER_SDIO)
901 	char btfw_path[PATH_MAX];
902 #endif /* defined (BT_OVER_SDIO) */
903 
904 #ifdef WL_MONITOR
905 	struct net_device *monitor_dev; /* monitor pseudo device */
906 	struct sk_buff *monitor_skb;
907 	uint	monitor_len;
908 	uint monitor_type;   /* monitor pseudo device */
909 	monitor_info_t *monitor_info;
910 #endif /* WL_MONITOR */
911 	uint32 shub_enable;
912 #if defined(BT_OVER_SDIO)
913 	struct mutex bus_user_lock; /* lock for sdio bus apis shared between WLAN & BT */
914 	int	bus_user_count; /* User counts of sdio bus shared between WLAN & BT */
915 #endif /* BT_OVER_SDIO */
916 #ifdef DHD_DEBUG_UART
917 	bool duart_execute;
918 #endif
919 #ifdef PCIE_INB_DW
920 	wait_queue_head_t ds_exit_wait;
921 #endif /* PCIE_INB_DW */
922 } dhd_info_t;
923 
924 #ifdef WL_MONITOR
925 #define MONPKT_EXTRA_LEN	48
926 #endif
927 
928 #define DHDIF_FWDER(dhdif)      FALSE
929 
930 #if defined(BT_OVER_SDIO)
931 /* Flag to indicate if driver is initialized */
932 uint dhd_driver_init_done = TRUE;
933 #else
934 /* Flag to indicate if driver is initialized */
935 uint dhd_driver_init_done = FALSE;
936 #endif
937 /* Flag to indicate if we should download firmware on driver load */
938 uint dhd_download_fw_on_driverload = TRUE;
939 
940 /* Definitions to provide path to the firmware and nvram
941  * example nvram_path[MOD_PARAM_PATHLEN]="/projects/wlan/nvram.txt"
942  */
943 char firmware_path[MOD_PARAM_PATHLEN];
944 char nvram_path[MOD_PARAM_PATHLEN];
945 char clm_path[MOD_PARAM_PATHLEN];
946 char config_path[MOD_PARAM_PATHLEN];
947 #ifdef DHD_UCODE_DOWNLOAD
948 char ucode_path[MOD_PARAM_PATHLEN];
949 #endif /* DHD_UCODE_DOWNLOAD */
950 
951 module_param_string(clm_path, clm_path, MOD_PARAM_PATHLEN, 0660);
952 
953 
954 /* backup buffer for firmware and nvram path */
955 char fw_bak_path[MOD_PARAM_PATHLEN];
956 char nv_bak_path[MOD_PARAM_PATHLEN];
957 
958 /* information string to keep firmware, chio, cheip version info visiable from log */
959 char info_string[MOD_PARAM_INFOLEN];
960 module_param_string(info_string, info_string, MOD_PARAM_INFOLEN, 0444);
961 int op_mode = 0;
962 int disable_proptx = 0;
963 module_param(op_mode, int, 0644);
964 extern int wl_control_wl_start(struct net_device *dev);
965 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && (defined(BCMLXSDMMC) || defined(BCMDBUS))
966 struct semaphore dhd_registration_sem;
967 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) */
968 
969 /* deferred handlers */
970 static void dhd_ifadd_event_handler(void *handle, void *event_info, u8 event);
971 static void dhd_ifdel_event_handler(void *handle, void *event_info, u8 event);
972 static void dhd_set_mac_addr_handler(void *handle, void *event_info, u8 event);
973 static void dhd_set_mcast_list_handler(void *handle, void *event_info, u8 event);
974 
975 #ifdef DHD_UPDATE_INTF_MAC
976 static void dhd_ifupdate_event_handler(void *handle, void *event_info, u8 event);
977 #endif /* DHD_UPDATE_INTF_MAC */
978 #if defined(CONFIG_IPV6) && defined(IPV6_NDO_SUPPORT)
979 static void dhd_inet6_work_handler(void *dhd_info, void *event_data, u8 event);
980 #endif /* CONFIG_IPV6 && IPV6_NDO_SUPPORT */
981 #ifdef WL_CFG80211
982 extern void dhd_netdev_free(struct net_device *ndev);
983 #endif /* WL_CFG80211 */
984 
985 #if (defined(DHD_WET) || defined(DHD_MCAST_REGEN) || defined(DHD_L2_FILTER))
986 /* update rx_pkt_chainable state of dhd interface */
987 static void dhd_update_rx_pkt_chainable_state(dhd_pub_t* dhdp, uint32 idx);
988 #endif /* DHD_WET || DHD_MCAST_REGEN || DHD_L2_FILTER */
989 
990 #ifdef HOFFLOAD_MODULES
991 char dhd_hmem_module_string[MOD_PARAM_SRLEN];
992 module_param_string(dhd_hmem_module_string, dhd_hmem_module_string, MOD_PARAM_SRLEN, 0660);
993 #endif
994 /* Error bits */
995 module_param(dhd_msg_level, int, 0);
996 #if defined(WL_WIRELESS_EXT)
997 module_param(iw_msg_level, int, 0);
998 #endif
999 #ifdef WL_CFG80211
1000 module_param(wl_dbg_level, int, 0);
1001 #endif
1002 module_param(android_msg_level, int, 0);
1003 module_param(config_msg_level, int, 0);
1004 
1005 #ifdef ARP_OFFLOAD_SUPPORT
1006 /* ARP offload enable */
1007 uint dhd_arp_enable = TRUE;
1008 module_param(dhd_arp_enable, uint, 0);
1009 
1010 /* ARP offload agent mode : Enable ARP Host Auto-Reply and ARP Peer Auto-Reply */
1011 
1012 #ifdef ENABLE_ARP_SNOOP_MODE
1013 uint dhd_arp_mode = ARP_OL_AGENT | ARP_OL_PEER_AUTO_REPLY | ARP_OL_SNOOP | ARP_OL_HOST_AUTO_REPLY;
1014 #else
1015 uint dhd_arp_mode = ARP_OL_AGENT | ARP_OL_PEER_AUTO_REPLY;
1016 #endif /* ENABLE_ARP_SNOOP_MODE */
1017 
1018 module_param(dhd_arp_mode, uint, 0);
1019 #endif /* ARP_OFFLOAD_SUPPORT */
1020 
1021 /* Disable Prop tx */
1022 module_param(disable_proptx, int, 0644);
1023 /* load firmware and/or nvram values from the filesystem */
1024 module_param_string(firmware_path, firmware_path, MOD_PARAM_PATHLEN, 0660);
1025 module_param_string(nvram_path, nvram_path, MOD_PARAM_PATHLEN, 0660);
1026 module_param_string(config_path, config_path, MOD_PARAM_PATHLEN, 0);
1027 #ifdef DHD_UCODE_DOWNLOAD
1028 module_param_string(ucode_path, ucode_path, MOD_PARAM_PATHLEN, 0660);
1029 #endif /* DHD_UCODE_DOWNLOAD */
1030 
1031 /* Watchdog interval */
1032 
1033 /* extend watchdog expiration to 2 seconds when DPC is running */
1034 #define WATCHDOG_EXTEND_INTERVAL (2000)
1035 
1036 uint dhd_watchdog_ms = CUSTOM_DHD_WATCHDOG_MS;
1037 module_param(dhd_watchdog_ms, uint, 0);
1038 
1039 #ifdef DHD_PCIE_RUNTIMEPM
1040 uint dhd_runtimepm_ms = CUSTOM_DHD_RUNTIME_MS;
1041 #endif /* DHD_PCIE_RUNTIMEPMT */
1042 #if defined(DHD_DEBUG)
1043 /* Console poll interval */
1044 uint dhd_console_ms = 0;
1045 module_param(dhd_console_ms, uint, 0644);
1046 #else
1047 uint dhd_console_ms = 0;
1048 #endif /* DHD_DEBUG */
1049 
1050 uint dhd_slpauto = TRUE;
1051 module_param(dhd_slpauto, uint, 0);
1052 
1053 #ifdef PKT_FILTER_SUPPORT
1054 /* Global Pkt filter enable control */
1055 uint dhd_pkt_filter_enable = TRUE;
1056 module_param(dhd_pkt_filter_enable, uint, 0);
1057 #endif
1058 
1059 /* Pkt filter init setup */
1060 uint dhd_pkt_filter_init = 0;
1061 module_param(dhd_pkt_filter_init, uint, 0);
1062 
1063 /* Pkt filter mode control */
1064 #ifdef GAN_LITE_NAT_KEEPALIVE_FILTER
1065 uint dhd_master_mode = FALSE;
1066 #else
1067 uint dhd_master_mode = FALSE;
1068 #endif /* GAN_LITE_NAT_KEEPALIVE_FILTER */
1069 module_param(dhd_master_mode, uint, 0);
1070 
1071 int dhd_watchdog_prio = 0;
1072 module_param(dhd_watchdog_prio, int, 0);
1073 
1074 /* DPC thread priority */
1075 int dhd_dpc_prio = CUSTOM_DPC_PRIO_SETTING;
1076 module_param(dhd_dpc_prio, int, 0);
1077 
1078 /* RX frame thread priority */
1079 int dhd_rxf_prio = CUSTOM_RXF_PRIO_SETTING;
1080 module_param(dhd_rxf_prio, int, 0);
1081 
1082 #if !defined(BCMDBUS)
1083 extern int dhd_dongle_ramsize;
1084 module_param(dhd_dongle_ramsize, int, 0);
1085 #endif /* !BCMDBUS */
1086 
1087 #ifdef WL_CFG80211
1088 int passive_channel_skip = 0;
1089 module_param(passive_channel_skip, int, (S_IRUSR|S_IWUSR));
1090 #endif /* WL_CFG80211 */
1091 
1092 /* Keep track of number of instances */
1093 static int dhd_found = 0;
1094 static int instance_base = 0; /* Starting instance number */
1095 module_param(instance_base, int, 0644);
1096 
1097 #if defined(DHD_LB_RXP) && defined(PCIE_FULL_DONGLE)
1098 static int dhd_napi_weight = 32;
1099 module_param(dhd_napi_weight, int, 0644);
1100 #endif /* DHD_LB_RXP && PCIE_FULL_DONGLE */
1101 
1102 #ifdef PCIE_FULL_DONGLE
1103 extern int h2d_max_txpost;
1104 module_param(h2d_max_txpost, int, 0644);
1105 #endif /* PCIE_FULL_DONGLE */
1106 
1107 #ifdef DHD_ARP_DUMP
1108 #include <linux/if_arp.h>
1109 static const char arp_types[][10] = {
1110 	"NA", "REQUEST", "RESPONSE"
1111 };
1112 static void dhd_arp_dump(char *ifname, uint8 *pktdata, bool tx);
1113 #endif /* DHD_ARP_DUMP */
1114 
1115 #ifdef DHD_DHCP_DUMP
1116 struct bootp_fmt {
1117 	struct iphdr ip_header;
1118 	struct udphdr udp_header;
1119 	uint8 op;
1120 	uint8 htype;
1121 	uint8 hlen;
1122 	uint8 hops;
1123 	uint32 transaction_id;
1124 	uint16 secs;
1125 	uint16 flags;
1126 	uint32 client_ip;
1127 	uint32 assigned_ip;
1128 	uint32 server_ip;
1129 	uint32 relay_ip;
1130 	uint8 hw_address[16];
1131 	uint8 server_name[64];
1132 	uint8 file_name[128];
1133 	uint8 options[312];
1134 };
1135 
1136 static const uint8 bootp_magic_cookie[4] = { 99, 130, 83, 99 };
1137 static const char dhcp_ops[][10] = {
1138 	"NA", "REQUEST", "REPLY"
1139 };
1140 static const char dhcp_types[][10] = {
1141 	"NA", "DISCOVER", "OFFER", "REQUEST", "DECLINE", "ACK", "NAK", "RELEASE", "INFORM"
1142 };
1143 static void dhd_dhcp_dump(char *ifname, uint8 *pktdata, bool tx);
1144 #endif /* DHD_DHCP_DUMP */
1145 
1146 #ifdef DHD_ICMP_DUMP
1147 #include <net/icmp.h>
1148 static void dhd_icmp_dump(char *ifname, uint8 *pktdata, bool tx);
1149 #endif /* DHD_ICMP_DUMP */
1150 
1151 /* Functions to manage sysfs interface for dhd */
1152 static int dhd_sysfs_init(dhd_info_t *dhd);
1153 static void dhd_sysfs_exit(dhd_info_t *dhd);
1154 
1155 #ifdef SHOW_LOGTRACE
1156 #if defined(CUSTOMER_HW4_DEBUG)
1157 static char *logstrs_path = PLATFORM_PATH"logstrs.bin";
1158 static char *st_str_file_path = PLATFORM_PATH"rtecdc.bin";
1159 static char *map_file_path = PLATFORM_PATH"rtecdc.map";
1160 static char *rom_st_str_file_path = PLATFORM_PATH"roml.bin";
1161 static char *rom_map_file_path = PLATFORM_PATH"roml.map";
1162 #elif defined(CUSTOMER_HW2)
1163 static char *logstrs_path = "/data/misc/wifi/logstrs.bin";
1164 static char *st_str_file_path = "/data/misc/wifi/rtecdc.bin";
1165 static char *map_file_path = "/data/misc/wifi/rtecdc.map";
1166 static char *rom_st_str_file_path = "/data/misc/wifi/roml.bin";
1167 static char *rom_map_file_path = "/data/misc/wifi/roml.map";
1168 #else
1169 static char *logstrs_path = "/installmedia/logstrs.bin";
1170 static char *st_str_file_path = "/installmedia/rtecdc.bin";
1171 static char *map_file_path = "/installmedia/rtecdc.map";
1172 static char *rom_st_str_file_path = "/installmedia/roml.bin";
1173 static char *rom_map_file_path = "/installmedia/roml.map";
1174 #endif /* CUSTOMER_HW4_DEBUG || CUSTOMER_HW2 */
1175 static char *ram_file_str = "rtecdc";
1176 static char *rom_file_str = "roml";
1177 
1178 module_param(logstrs_path, charp, S_IRUGO);
1179 module_param(st_str_file_path, charp, S_IRUGO);
1180 module_param(map_file_path, charp, S_IRUGO);
1181 module_param(rom_st_str_file_path, charp, S_IRUGO);
1182 module_param(rom_map_file_path, charp, S_IRUGO);
1183 
1184 static int dhd_init_logstrs_array(osl_t *osh, dhd_event_log_t *temp);
1185 static int dhd_read_map(osl_t *osh, char *fname, uint32 *ramstart, uint32 *rodata_start,
1186 	uint32 *rodata_end);
1187 static int dhd_init_static_strs_array(osl_t *osh, dhd_event_log_t *temp, char *str_file,
1188 	char *map_file);
1189 #endif /* SHOW_LOGTRACE */
1190 
1191 #if defined(DHD_LB)
1192 
1193 static void
dhd_lb_set_default_cpus(dhd_info_t * dhd)1194 dhd_lb_set_default_cpus(dhd_info_t *dhd)
1195 {
1196 	/* Default CPU allocation for the jobs */
1197 	atomic_set(&dhd->rx_napi_cpu, 1);
1198 	atomic_set(&dhd->rx_compl_cpu, 2);
1199 	atomic_set(&dhd->tx_compl_cpu, 2);
1200 	atomic_set(&dhd->tx_cpu, 2);
1201 	atomic_set(&dhd->net_tx_cpu, 0);
1202 }
1203 
1204 static void
dhd_cpumasks_deinit(dhd_info_t * dhd)1205 dhd_cpumasks_deinit(dhd_info_t *dhd)
1206 {
1207 	free_cpumask_var(dhd->cpumask_curr_avail);
1208 	free_cpumask_var(dhd->cpumask_primary);
1209 	free_cpumask_var(dhd->cpumask_primary_new);
1210 	free_cpumask_var(dhd->cpumask_secondary);
1211 	free_cpumask_var(dhd->cpumask_secondary_new);
1212 }
1213 
1214 static int
dhd_cpumasks_init(dhd_info_t * dhd)1215 dhd_cpumasks_init(dhd_info_t *dhd)
1216 {
1217 	int id;
1218 	uint32 cpus, num_cpus = num_possible_cpus();
1219 	int ret = 0;
1220 
1221 	DHD_ERROR(("%s CPU masks primary(big)=0x%x secondary(little)=0x%x\n", __FUNCTION__,
1222 		DHD_LB_PRIMARY_CPUS, DHD_LB_SECONDARY_CPUS));
1223 
1224 	if (!alloc_cpumask_var(&dhd->cpumask_curr_avail, GFP_KERNEL) ||
1225 	    !alloc_cpumask_var(&dhd->cpumask_primary, GFP_KERNEL) ||
1226 	    !alloc_cpumask_var(&dhd->cpumask_primary_new, GFP_KERNEL) ||
1227 	    !alloc_cpumask_var(&dhd->cpumask_secondary, GFP_KERNEL) ||
1228 	    !alloc_cpumask_var(&dhd->cpumask_secondary_new, GFP_KERNEL)) {
1229 		DHD_ERROR(("%s Failed to init cpumasks\n", __FUNCTION__));
1230 		ret = -ENOMEM;
1231 		goto fail;
1232 	}
1233 
1234 	cpumask_copy(dhd->cpumask_curr_avail, cpu_online_mask);
1235 	cpumask_clear(dhd->cpumask_primary);
1236 	cpumask_clear(dhd->cpumask_secondary);
1237 
1238 	if (num_cpus > 32) {
1239 		DHD_ERROR(("%s max cpus must be 32, %d too big\n", __FUNCTION__, num_cpus));
1240 		ASSERT(0);
1241 	}
1242 
1243 	cpus = DHD_LB_PRIMARY_CPUS;
1244 	for (id = 0; id < num_cpus; id++) {
1245 		if (isset(&cpus, id))
1246 			cpumask_set_cpu(id, dhd->cpumask_primary);
1247 	}
1248 
1249 	cpus = DHD_LB_SECONDARY_CPUS;
1250 	for (id = 0; id < num_cpus; id++) {
1251 		if (isset(&cpus, id))
1252 			cpumask_set_cpu(id, dhd->cpumask_secondary);
1253 	}
1254 
1255 	return ret;
1256 fail:
1257 	dhd_cpumasks_deinit(dhd);
1258 	return ret;
1259 }
1260 
1261 /*
1262  * The CPU Candidacy Algorithm
1263  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1264  * The available CPUs for selection are divided into two groups
1265  *  Primary Set - A CPU mask that carries the First Choice CPUs
1266  *  Secondary Set - A CPU mask that carries the Second Choice CPUs.
1267  *
1268  * There are two types of Job, that needs to be assigned to
1269  * the CPUs, from one of the above mentioned CPU group. The Jobs are
1270  * 1) Rx Packet Processing - napi_cpu
1271  * 2) Completion Processiong (Tx, RX) - compl_cpu
1272  *
1273  * To begin with both napi_cpu and compl_cpu are on CPU0. Whenever a CPU goes
1274  * on-line/off-line the CPU candidacy algorithm is triggerd. The candidacy
1275  * algo tries to pickup the first available non boot CPU (CPU0) for napi_cpu.
1276  * If there are more processors free, it assigns one to compl_cpu.
1277  * It also tries to ensure that both napi_cpu and compl_cpu are not on the same
1278  * CPU, as much as possible.
1279  *
1280  * By design, both Tx and Rx completion jobs are run on the same CPU core, as it
1281  * would allow Tx completion skb's to be released into a local free pool from
1282  * which the rx buffer posts could have been serviced. it is important to note
1283  * that a Tx packet may not have a large enough buffer for rx posting.
1284  */
dhd_select_cpu_candidacy(dhd_info_t * dhd)1285 void dhd_select_cpu_candidacy(dhd_info_t *dhd)
1286 {
1287 	uint32 primary_available_cpus; /* count of primary available cpus */
1288 	uint32 secondary_available_cpus; /* count of secondary available cpus */
1289 	uint32 napi_cpu = 0; /* cpu selected for napi rx processing */
1290 	uint32 compl_cpu = 0; /* cpu selected for completion jobs */
1291 	uint32 tx_cpu = 0; /* cpu selected for tx processing job */
1292 
1293 	cpumask_clear(dhd->cpumask_primary_new);
1294 	cpumask_clear(dhd->cpumask_secondary_new);
1295 
1296 	/*
1297 	 * Now select from the primary mask. Even if a Job is
1298 	 * already running on a CPU in secondary group, we still move
1299 	 * to primary CPU. So no conditional checks.
1300 	 */
1301 	cpumask_and(dhd->cpumask_primary_new, dhd->cpumask_primary,
1302 		dhd->cpumask_curr_avail);
1303 
1304 	cpumask_and(dhd->cpumask_secondary_new, dhd->cpumask_secondary,
1305 		dhd->cpumask_curr_avail);
1306 
1307 	primary_available_cpus = cpumask_weight(dhd->cpumask_primary_new);
1308 
1309 	if (primary_available_cpus > 0) {
1310 		napi_cpu = cpumask_first(dhd->cpumask_primary_new);
1311 
1312 		/* If no further CPU is available,
1313 		 * cpumask_next returns >= nr_cpu_ids
1314 		 */
1315 		tx_cpu = cpumask_next(napi_cpu, dhd->cpumask_primary_new);
1316 		if (tx_cpu >= nr_cpu_ids)
1317 			tx_cpu = 0;
1318 
1319 		/* In case there are no more CPUs, do completions & Tx in same CPU */
1320 		compl_cpu = cpumask_next(tx_cpu, dhd->cpumask_primary_new);
1321 		if (compl_cpu >= nr_cpu_ids)
1322 			compl_cpu = tx_cpu;
1323 	}
1324 
1325 	DHD_INFO(("%s After primary CPU check napi_cpu %d compl_cpu %d tx_cpu %d\n",
1326 		__FUNCTION__, napi_cpu, compl_cpu, tx_cpu));
1327 
1328 	/* -- Now check for the CPUs from the secondary mask -- */
1329 	secondary_available_cpus = cpumask_weight(dhd->cpumask_secondary_new);
1330 
1331 	DHD_INFO(("%s Available secondary cpus %d nr_cpu_ids %d\n",
1332 		__FUNCTION__, secondary_available_cpus, nr_cpu_ids));
1333 
1334 	if (secondary_available_cpus > 0) {
1335 		/* At this point if napi_cpu is unassigned it means no CPU
1336 		 * is online from Primary Group
1337 		 */
1338 		if (napi_cpu == 0) {
1339 			napi_cpu = cpumask_first(dhd->cpumask_secondary_new);
1340 			tx_cpu = cpumask_next(napi_cpu, dhd->cpumask_secondary_new);
1341 			compl_cpu = cpumask_next(tx_cpu, dhd->cpumask_secondary_new);
1342 		} else if (tx_cpu == 0) {
1343 			tx_cpu = cpumask_first(dhd->cpumask_secondary_new);
1344 			compl_cpu = cpumask_next(tx_cpu, dhd->cpumask_secondary_new);
1345 		} else if (compl_cpu == 0) {
1346 			compl_cpu = cpumask_first(dhd->cpumask_secondary_new);
1347 		}
1348 
1349 		/* If no CPU was available for tx processing, choose CPU 0 */
1350 		if (tx_cpu >= nr_cpu_ids)
1351 			tx_cpu = 0;
1352 
1353 		/* If no CPU was available for completion, choose CPU 0 */
1354 		if (compl_cpu >= nr_cpu_ids)
1355 			compl_cpu = 0;
1356 	}
1357 	if ((primary_available_cpus == 0) &&
1358 		(secondary_available_cpus == 0)) {
1359 		/* No CPUs available from primary or secondary mask */
1360 		napi_cpu = 1;
1361 		compl_cpu = 0;
1362 		tx_cpu = 2;
1363 	}
1364 
1365 	DHD_INFO(("%s After secondary CPU check napi_cpu %d compl_cpu %d tx_cpu %d\n",
1366 		__FUNCTION__, napi_cpu, compl_cpu, tx_cpu));
1367 
1368 	ASSERT(napi_cpu < nr_cpu_ids);
1369 	ASSERT(compl_cpu < nr_cpu_ids);
1370 	ASSERT(tx_cpu < nr_cpu_ids);
1371 
1372 	atomic_set(&dhd->rx_napi_cpu, napi_cpu);
1373 	atomic_set(&dhd->tx_compl_cpu, compl_cpu);
1374 	atomic_set(&dhd->rx_compl_cpu, compl_cpu);
1375 	atomic_set(&dhd->tx_cpu, tx_cpu);
1376 
1377 	return;
1378 }
1379 
1380 /*
1381  * Function to handle CPU Hotplug notifications.
1382  * One of the task it does is to trigger the CPU Candidacy algorithm
1383  * for load balancing.
1384  */
1385 int
dhd_cpu_callback(struct notifier_block * nfb,unsigned long action,void * hcpu)1386 dhd_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
1387 {
1388 	unsigned long int cpu = (unsigned long int)hcpu;
1389 
1390 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
1391 #pragma GCC diagnostic push
1392 #pragma GCC diagnostic ignored "-Wcast-qual"
1393 #endif
1394 	dhd_info_t *dhd = container_of(nfb, dhd_info_t, cpu_notifier);
1395 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
1396 #pragma GCC diagnostic pop
1397 #endif
1398 
1399 	if (!dhd || !(dhd->dhd_state & DHD_ATTACH_STATE_LB_ATTACH_DONE)) {
1400 		DHD_INFO(("%s(): LB data is not initialized yet.\n",
1401 			__FUNCTION__));
1402 		return NOTIFY_BAD;
1403 	}
1404 
1405 	switch (action)
1406 	{
1407 		case CPU_ONLINE:
1408 		case CPU_ONLINE_FROZEN:
1409 			DHD_LB_STATS_INCR(dhd->cpu_online_cnt[cpu]);
1410 			cpumask_set_cpu(cpu, dhd->cpumask_curr_avail);
1411 			dhd_select_cpu_candidacy(dhd);
1412 			break;
1413 
1414 		case CPU_DOWN_PREPARE:
1415 		case CPU_DOWN_PREPARE_FROZEN:
1416 			DHD_LB_STATS_INCR(dhd->cpu_offline_cnt[cpu]);
1417 			cpumask_clear_cpu(cpu, dhd->cpumask_curr_avail);
1418 			dhd_select_cpu_candidacy(dhd);
1419 			break;
1420 		default:
1421 			break;
1422 	}
1423 
1424 	return NOTIFY_OK;
1425 }
1426 
1427 #if defined(DHD_LB_STATS)
dhd_lb_stats_init(dhd_pub_t * dhdp)1428 void dhd_lb_stats_init(dhd_pub_t *dhdp)
1429 {
1430 	dhd_info_t *dhd;
1431 	int i, j, num_cpus = num_possible_cpus();
1432 	int alloc_size = sizeof(uint32) * num_cpus;
1433 
1434 	if (dhdp == NULL) {
1435 		DHD_ERROR(("%s(): Invalid argument dhd pubb pointer is NULL \n",
1436 			__FUNCTION__));
1437 		return;
1438 	}
1439 
1440 	dhd = dhdp->info;
1441 	if (dhd == NULL) {
1442 		DHD_ERROR(("%s(): DHD pointer is NULL \n", __FUNCTION__));
1443 		return;
1444 	}
1445 
1446 	DHD_LB_STATS_CLR(dhd->dhd_dpc_cnt);
1447 	DHD_LB_STATS_CLR(dhd->napi_sched_cnt);
1448 
1449 	dhd->napi_percpu_run_cnt = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1450 	if (!dhd->napi_percpu_run_cnt) {
1451 		DHD_ERROR(("%s(): napi_percpu_run_cnt malloc failed \n",
1452 			__FUNCTION__));
1453 		return;
1454 	}
1455 	for (i = 0; i < num_cpus; i++)
1456 		DHD_LB_STATS_CLR(dhd->napi_percpu_run_cnt[i]);
1457 
1458 	DHD_LB_STATS_CLR(dhd->rxc_sched_cnt);
1459 
1460 	dhd->rxc_percpu_run_cnt = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1461 	if (!dhd->rxc_percpu_run_cnt) {
1462 		DHD_ERROR(("%s(): rxc_percpu_run_cnt malloc failed \n",
1463 			__FUNCTION__));
1464 		return;
1465 	}
1466 	for (i = 0; i < num_cpus; i++)
1467 		DHD_LB_STATS_CLR(dhd->rxc_percpu_run_cnt[i]);
1468 
1469 	DHD_LB_STATS_CLR(dhd->txc_sched_cnt);
1470 
1471 	dhd->txc_percpu_run_cnt = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1472 	if (!dhd->txc_percpu_run_cnt) {
1473 		DHD_ERROR(("%s(): txc_percpu_run_cnt malloc failed \n",
1474 			__FUNCTION__));
1475 		return;
1476 	}
1477 	for (i = 0; i < num_cpus; i++)
1478 		DHD_LB_STATS_CLR(dhd->txc_percpu_run_cnt[i]);
1479 
1480 	dhd->cpu_online_cnt = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1481 	if (!dhd->cpu_online_cnt) {
1482 		DHD_ERROR(("%s(): cpu_online_cnt malloc failed \n",
1483 			__FUNCTION__));
1484 		return;
1485 	}
1486 	for (i = 0; i < num_cpus; i++)
1487 		DHD_LB_STATS_CLR(dhd->cpu_online_cnt[i]);
1488 
1489 	dhd->cpu_offline_cnt = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1490 	if (!dhd->cpu_offline_cnt) {
1491 		DHD_ERROR(("%s(): cpu_offline_cnt malloc failed \n",
1492 			__FUNCTION__));
1493 		return;
1494 	}
1495 	for (i = 0; i < num_cpus; i++)
1496 		DHD_LB_STATS_CLR(dhd->cpu_offline_cnt[i]);
1497 
1498 	dhd->txp_percpu_run_cnt = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1499 	if (!dhd->txp_percpu_run_cnt) {
1500 		DHD_ERROR(("%s(): txp_percpu_run_cnt malloc failed \n",
1501 			__FUNCTION__));
1502 		return;
1503 	}
1504 	for (i = 0; i < num_cpus; i++)
1505 		DHD_LB_STATS_CLR(dhd->txp_percpu_run_cnt[i]);
1506 
1507 	dhd->tx_start_percpu_run_cnt = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1508 	if (!dhd->tx_start_percpu_run_cnt) {
1509 		DHD_ERROR(("%s(): tx_start_percpu_run_cnt malloc failed \n",
1510 			__FUNCTION__));
1511 		return;
1512 	}
1513 	for (i = 0; i < num_cpus; i++)
1514 		DHD_LB_STATS_CLR(dhd->tx_start_percpu_run_cnt[i]);
1515 
1516 	for (j = 0; j < HIST_BIN_SIZE; j++) {
1517 		dhd->napi_rx_hist[j] = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1518 		if (!dhd->napi_rx_hist[j]) {
1519 			DHD_ERROR(("%s(): dhd->napi_rx_hist[%d] malloc failed \n",
1520 				__FUNCTION__, j));
1521 			return;
1522 		}
1523 		for (i = 0; i < num_cpus; i++) {
1524 			DHD_LB_STATS_CLR(dhd->napi_rx_hist[j][i]);
1525 		}
1526 	}
1527 #ifdef DHD_LB_TXC
1528 	for (j = 0; j < HIST_BIN_SIZE; j++) {
1529 		dhd->txc_hist[j] = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1530 		if (!dhd->txc_hist[j]) {
1531 			DHD_ERROR(("%s(): dhd->txc_hist[%d] malloc failed \n",
1532 			         __FUNCTION__, j));
1533 			return;
1534 		}
1535 		for (i = 0; i < num_cpus; i++) {
1536 			DHD_LB_STATS_CLR(dhd->txc_hist[j][i]);
1537 		}
1538 	}
1539 #endif /* DHD_LB_TXC */
1540 #ifdef DHD_LB_RXC
1541 	for (j = 0; j < HIST_BIN_SIZE; j++) {
1542 		dhd->rxc_hist[j] = (uint32 *)MALLOC(dhdp->osh, alloc_size);
1543 		if (!dhd->rxc_hist[j]) {
1544 			DHD_ERROR(("%s(): dhd->rxc_hist[%d] malloc failed \n",
1545 				__FUNCTION__, j));
1546 			return;
1547 		}
1548 		for (i = 0; i < num_cpus; i++) {
1549 			DHD_LB_STATS_CLR(dhd->rxc_hist[j][i]);
1550 		}
1551 	}
1552 #endif /* DHD_LB_RXC */
1553 	return;
1554 }
1555 
dhd_lb_stats_deinit(dhd_pub_t * dhdp)1556 void dhd_lb_stats_deinit(dhd_pub_t *dhdp)
1557 {
1558 	dhd_info_t *dhd;
1559 	int j, num_cpus = num_possible_cpus();
1560 	int alloc_size = sizeof(uint32) * num_cpus;
1561 
1562 	if (dhdp == NULL) {
1563 		DHD_ERROR(("%s(): Invalid argument dhd pubb pointer is NULL \n",
1564 			__FUNCTION__));
1565 		return;
1566 	}
1567 
1568 	dhd = dhdp->info;
1569 	if (dhd == NULL) {
1570 		DHD_ERROR(("%s(): DHD pointer is NULL \n", __FUNCTION__));
1571 		return;
1572 	}
1573 
1574 	if (dhd->napi_percpu_run_cnt) {
1575 		MFREE(dhdp->osh, dhd->napi_percpu_run_cnt, alloc_size);
1576 		dhd->napi_percpu_run_cnt = NULL;
1577 	}
1578 	if (dhd->rxc_percpu_run_cnt) {
1579 		MFREE(dhdp->osh, dhd->rxc_percpu_run_cnt, alloc_size);
1580 		dhd->rxc_percpu_run_cnt = NULL;
1581 	}
1582 	if (dhd->txc_percpu_run_cnt) {
1583 		MFREE(dhdp->osh, dhd->txc_percpu_run_cnt, alloc_size);
1584 		dhd->txc_percpu_run_cnt = NULL;
1585 	}
1586 	if (dhd->cpu_online_cnt) {
1587 		MFREE(dhdp->osh, dhd->cpu_online_cnt, alloc_size);
1588 		dhd->cpu_online_cnt = NULL;
1589 	}
1590 	if (dhd->cpu_offline_cnt) {
1591 		MFREE(dhdp->osh, dhd->cpu_offline_cnt, alloc_size);
1592 		dhd->cpu_offline_cnt = NULL;
1593 	}
1594 
1595 	if (dhd->txp_percpu_run_cnt) {
1596 		MFREE(dhdp->osh, dhd->txp_percpu_run_cnt, alloc_size);
1597 		dhd->txp_percpu_run_cnt = NULL;
1598 	}
1599 	if (dhd->tx_start_percpu_run_cnt) {
1600 		MFREE(dhdp->osh, dhd->tx_start_percpu_run_cnt, alloc_size);
1601 		dhd->tx_start_percpu_run_cnt = NULL;
1602 	}
1603 
1604 	for (j = 0; j < HIST_BIN_SIZE; j++) {
1605 		if (dhd->napi_rx_hist[j]) {
1606 			MFREE(dhdp->osh, dhd->napi_rx_hist[j], alloc_size);
1607 			dhd->napi_rx_hist[j] = NULL;
1608 		}
1609 #ifdef DHD_LB_TXC
1610 		if (dhd->txc_hist[j]) {
1611 			MFREE(dhdp->osh, dhd->txc_hist[j], alloc_size);
1612 			dhd->txc_hist[j] = NULL;
1613 		}
1614 #endif /* DHD_LB_TXC */
1615 #ifdef DHD_LB_RXC
1616 		if (dhd->rxc_hist[j]) {
1617 			MFREE(dhdp->osh, dhd->rxc_hist[j], alloc_size);
1618 			dhd->rxc_hist[j] = NULL;
1619 		}
1620 #endif /* DHD_LB_RXC */
1621 	}
1622 
1623 	return;
1624 }
1625 
dhd_lb_stats_dump_histo(struct bcmstrbuf * strbuf,uint32 ** hist)1626 static void dhd_lb_stats_dump_histo(
1627 	struct bcmstrbuf *strbuf, uint32 **hist)
1628 {
1629 	int i, j;
1630 	uint32 *per_cpu_total;
1631 	uint32 total = 0;
1632 	uint32 num_cpus = num_possible_cpus();
1633 
1634 	per_cpu_total = (uint32 *)kmalloc(sizeof(uint32) * num_cpus, GFP_ATOMIC);
1635 	if (!per_cpu_total) {
1636 		DHD_ERROR(("%s(): dhd->per_cpu_total malloc failed \n", __FUNCTION__));
1637 		return;
1638 	}
1639 	bzero(per_cpu_total, sizeof(uint32) * num_cpus);
1640 
1641 	bcm_bprintf(strbuf, "CPU: \t\t");
1642 	for (i = 0; i < num_cpus; i++)
1643 		bcm_bprintf(strbuf, "%d\t", i);
1644 	bcm_bprintf(strbuf, "\nBin\n");
1645 
1646 	for (i = 0; i < HIST_BIN_SIZE; i++) {
1647 		bcm_bprintf(strbuf, "%d:\t\t", 1<<i);
1648 		for (j = 0; j < num_cpus; j++) {
1649 			bcm_bprintf(strbuf, "%d\t", hist[i][j]);
1650 		}
1651 		bcm_bprintf(strbuf, "\n");
1652 	}
1653 	bcm_bprintf(strbuf, "Per CPU Total \t");
1654 	total = 0;
1655 	for (i = 0; i < num_cpus; i++) {
1656 		for (j = 0; j < HIST_BIN_SIZE; j++) {
1657 			per_cpu_total[i] += (hist[j][i] * (1<<j));
1658 		}
1659 		bcm_bprintf(strbuf, "%d\t", per_cpu_total[i]);
1660 		total += per_cpu_total[i];
1661 	}
1662 	bcm_bprintf(strbuf, "\nTotal\t\t%d \n", total);
1663 
1664 	kfree(per_cpu_total);
1665 	return;
1666 }
1667 
dhd_lb_stats_dump_cpu_array(struct bcmstrbuf * strbuf,uint32 * p)1668 static inline void dhd_lb_stats_dump_cpu_array(struct bcmstrbuf *strbuf, uint32 *p)
1669 {
1670 	int i, num_cpus = num_possible_cpus();
1671 
1672 	bcm_bprintf(strbuf, "CPU: \t");
1673 	for (i = 0; i < num_cpus; i++)
1674 		bcm_bprintf(strbuf, "%d\t", i);
1675 	bcm_bprintf(strbuf, "\n");
1676 
1677 	bcm_bprintf(strbuf, "Val: \t");
1678 	for (i = 0; i < num_cpus; i++)
1679 		bcm_bprintf(strbuf, "%u\t", *(p+i));
1680 	bcm_bprintf(strbuf, "\n");
1681 	return;
1682 }
1683 
dhd_lb_stats_dump(dhd_pub_t * dhdp,struct bcmstrbuf * strbuf)1684 void dhd_lb_stats_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
1685 {
1686 	dhd_info_t *dhd;
1687 
1688 	if (dhdp == NULL || strbuf == NULL) {
1689 		DHD_ERROR(("%s(): Invalid argument dhdp %p strbuf %p \n",
1690 			__FUNCTION__, dhdp, strbuf));
1691 		return;
1692 	}
1693 
1694 	dhd = dhdp->info;
1695 	if (dhd == NULL) {
1696 		DHD_ERROR(("%s(): DHD pointer is NULL \n", __FUNCTION__));
1697 		return;
1698 	}
1699 
1700 	bcm_bprintf(strbuf, "\ncpu_online_cnt:\n");
1701 	dhd_lb_stats_dump_cpu_array(strbuf, dhd->cpu_online_cnt);
1702 
1703 	bcm_bprintf(strbuf, "\ncpu_offline_cnt:\n");
1704 	dhd_lb_stats_dump_cpu_array(strbuf, dhd->cpu_offline_cnt);
1705 
1706 	bcm_bprintf(strbuf, "\nsched_cnt: dhd_dpc %u napi %u rxc %u txc %u\n",
1707 		dhd->dhd_dpc_cnt, dhd->napi_sched_cnt, dhd->rxc_sched_cnt,
1708 		dhd->txc_sched_cnt);
1709 
1710 #ifdef DHD_LB_RXP
1711 	bcm_bprintf(strbuf, "\nnapi_percpu_run_cnt:\n");
1712 	dhd_lb_stats_dump_cpu_array(strbuf, dhd->napi_percpu_run_cnt);
1713 	bcm_bprintf(strbuf, "\nNAPI Packets Received Histogram:\n");
1714 	dhd_lb_stats_dump_histo(strbuf, dhd->napi_rx_hist);
1715 #endif /* DHD_LB_RXP */
1716 
1717 #ifdef DHD_LB_RXC
1718 	bcm_bprintf(strbuf, "\nrxc_percpu_run_cnt:\n");
1719 	dhd_lb_stats_dump_cpu_array(strbuf, dhd->rxc_percpu_run_cnt);
1720 	bcm_bprintf(strbuf, "\nRX Completions (Buffer Post) Histogram:\n");
1721 	dhd_lb_stats_dump_histo(strbuf, dhd->rxc_hist);
1722 #endif /* DHD_LB_RXC */
1723 
1724 #ifdef DHD_LB_TXC
1725 	bcm_bprintf(strbuf, "\ntxc_percpu_run_cnt:\n");
1726 	dhd_lb_stats_dump_cpu_array(strbuf, dhd->txc_percpu_run_cnt);
1727 	bcm_bprintf(strbuf, "\nTX Completions (Buffer Free) Histogram:\n");
1728 	dhd_lb_stats_dump_histo(strbuf, dhd->txc_hist);
1729 #endif /* DHD_LB_TXC */
1730 
1731 #ifdef DHD_LB_TXP
1732 	bcm_bprintf(strbuf, "\ntxp_percpu_run_cnt:\n");
1733 	dhd_lb_stats_dump_cpu_array(strbuf, dhd->txp_percpu_run_cnt);
1734 
1735 	bcm_bprintf(strbuf, "\ntx_start_percpu_run_cnt:\n");
1736 	dhd_lb_stats_dump_cpu_array(strbuf, dhd->tx_start_percpu_run_cnt);
1737 #endif /* DHD_LB_TXP */
1738 
1739 	bcm_bprintf(strbuf, "\nCPU masks primary(big)=0x%x secondary(little)=0x%x\n",
1740 		DHD_LB_PRIMARY_CPUS, DHD_LB_SECONDARY_CPUS);
1741 
1742 	bcm_bprintf(strbuf, "napi_cpu %x tx_cpu %x\n",
1743 		atomic_read(&dhd->rx_napi_cpu), atomic_read(&dhd->tx_cpu));
1744 
1745 }
1746 
1747 /* Given a number 'n' returns 'm' that is next larger power of 2 after n */
next_larger_power2(uint32 num)1748 static inline uint32 next_larger_power2(uint32 num)
1749 {
1750 	num--;
1751 	num |= (num >> 1);
1752 	num |= (num >> 2);
1753 	num |= (num >> 4);
1754 	num |= (num >> 8);
1755 	num |= (num >> 16);
1756 
1757 	return (num + 1);
1758 }
1759 
dhd_lb_stats_update_histo(uint32 ** bin,uint32 count,uint32 cpu)1760 static void dhd_lb_stats_update_histo(uint32 **bin, uint32 count, uint32 cpu)
1761 {
1762 	uint32 bin_power;
1763 	uint32 *p;
1764 	bin_power = next_larger_power2(count);
1765 
1766 	switch (bin_power) {
1767 		case   1: p = bin[0] + cpu; break;
1768 		case   2: p = bin[1] + cpu; break;
1769 		case   4: p = bin[2] + cpu; break;
1770 		case   8: p = bin[3] + cpu; break;
1771 		case  16: p = bin[4] + cpu; break;
1772 		case  32: p = bin[5] + cpu; break;
1773 		case  64: p = bin[6] + cpu; break;
1774 		case 128: p = bin[7] + cpu; break;
1775 		default : p = bin[8] + cpu; break;
1776 	}
1777 
1778 	*p = *p + 1;
1779 	return;
1780 }
1781 
dhd_lb_stats_update_napi_histo(dhd_pub_t * dhdp,uint32 count)1782 extern void dhd_lb_stats_update_napi_histo(dhd_pub_t *dhdp, uint32 count)
1783 {
1784 	int cpu;
1785 	dhd_info_t *dhd = dhdp->info;
1786 
1787 	cpu = get_cpu();
1788 	put_cpu();
1789 	dhd_lb_stats_update_histo(dhd->napi_rx_hist, count, cpu);
1790 
1791 	return;
1792 }
1793 
dhd_lb_stats_update_txc_histo(dhd_pub_t * dhdp,uint32 count)1794 extern void dhd_lb_stats_update_txc_histo(dhd_pub_t *dhdp, uint32 count)
1795 {
1796 	int cpu;
1797 	dhd_info_t *dhd = dhdp->info;
1798 
1799 	cpu = get_cpu();
1800 	put_cpu();
1801 	dhd_lb_stats_update_histo(dhd->txc_hist, count, cpu);
1802 
1803 	return;
1804 }
1805 
dhd_lb_stats_update_rxc_histo(dhd_pub_t * dhdp,uint32 count)1806 extern void dhd_lb_stats_update_rxc_histo(dhd_pub_t *dhdp, uint32 count)
1807 {
1808 	int cpu;
1809 	dhd_info_t *dhd = dhdp->info;
1810 
1811 	cpu = get_cpu();
1812 	put_cpu();
1813 	dhd_lb_stats_update_histo(dhd->rxc_hist, count, cpu);
1814 
1815 	return;
1816 }
1817 
dhd_lb_stats_txc_percpu_cnt_incr(dhd_pub_t * dhdp)1818 extern void dhd_lb_stats_txc_percpu_cnt_incr(dhd_pub_t *dhdp)
1819 {
1820 	dhd_info_t *dhd = dhdp->info;
1821 	DHD_LB_STATS_PERCPU_ARR_INCR(dhd->txc_percpu_run_cnt);
1822 }
1823 
dhd_lb_stats_rxc_percpu_cnt_incr(dhd_pub_t * dhdp)1824 extern void dhd_lb_stats_rxc_percpu_cnt_incr(dhd_pub_t *dhdp)
1825 {
1826 	dhd_info_t *dhd = dhdp->info;
1827 	DHD_LB_STATS_PERCPU_ARR_INCR(dhd->rxc_percpu_run_cnt);
1828 }
1829 #endif /* DHD_LB_STATS */
1830 
1831 #endif /* DHD_LB */
1832 
1833 #if defined(DISABLE_FRAMEBURST_VSDB) && defined(USE_WFA_CERT_CONF)
1834 int g_frameburst = 1;
1835 #endif /* DISABLE_FRAMEBURST_VSDB && USE_WFA_CERT_CONF */
1836 
1837 static int dhd_get_pend_8021x_cnt(dhd_info_t *dhd);
1838 
1839 /* DHD Perimiter lock only used in router with bypass forwarding. */
1840 #define DHD_PERIM_RADIO_INIT()              do { /* noop */ } while (0)
1841 #define DHD_PERIM_LOCK_TRY(unit, flag)      do { /* noop */ } while (0)
1842 #define DHD_PERIM_UNLOCK_TRY(unit, flag)    do { /* noop */ } while (0)
1843 
1844 #ifdef PCIE_FULL_DONGLE
1845 #if defined(BCM_GMAC3)
1846 #define DHD_IF_STA_LIST_LOCK_INIT(ifp)      do { /* noop */ } while (0)
1847 #define DHD_IF_STA_LIST_LOCK(ifp, flags)    ({ BCM_REFERENCE(flags); })
1848 #define DHD_IF_STA_LIST_UNLOCK(ifp, flags)  ({ BCM_REFERENCE(flags); })
1849 
1850 #if defined(DHD_IGMP_UCQUERY) || defined(DHD_UCAST_UPNP)
1851 #define DHD_IF_WMF_UCFORWARD_LOCK(dhd, ifp, slist) ({ BCM_REFERENCE(slist); &(ifp)->sta_list; })
1852 #define DHD_IF_WMF_UCFORWARD_UNLOCK(dhd, slist) ({ BCM_REFERENCE(slist); })
1853 #endif /* DHD_IGMP_UCQUERY || DHD_UCAST_UPNP */
1854 
1855 #else /* ! BCM_GMAC3 */
1856 #define DHD_IF_STA_LIST_LOCK_INIT(ifp) spin_lock_init(&(ifp)->sta_list_lock)
1857 #define DHD_IF_STA_LIST_LOCK(ifp, flags) \
1858 	spin_lock_irqsave(&(ifp)->sta_list_lock, (flags))
1859 #define DHD_IF_STA_LIST_UNLOCK(ifp, flags) \
1860 	spin_unlock_irqrestore(&(ifp)->sta_list_lock, (flags))
1861 
1862 #if defined(DHD_IGMP_UCQUERY) || defined(DHD_UCAST_UPNP)
1863 static struct list_head * dhd_sta_list_snapshot(dhd_info_t *dhd, dhd_if_t *ifp,
1864 	struct list_head *snapshot_list);
1865 static void dhd_sta_list_snapshot_free(dhd_info_t *dhd, struct list_head *snapshot_list);
1866 #define DHD_IF_WMF_UCFORWARD_LOCK(dhd, ifp, slist) ({ dhd_sta_list_snapshot(dhd, ifp, slist); })
1867 #define DHD_IF_WMF_UCFORWARD_UNLOCK(dhd, slist) ({ dhd_sta_list_snapshot_free(dhd, slist); })
1868 #endif /* DHD_IGMP_UCQUERY || DHD_UCAST_UPNP */
1869 
1870 #endif /* ! BCM_GMAC3 */
1871 #endif /* PCIE_FULL_DONGLE */
1872 
1873 /* Control fw roaming */
1874 uint dhd_roam_disable = 0;
1875 
1876 #ifdef BCMDBGFS
1877 extern void dhd_dbgfs_init(dhd_pub_t *dhdp);
1878 extern void dhd_dbgfs_remove(void);
1879 #endif
1880 
1881 
1882 /* Control radio state */
1883 uint dhd_radio_up = 1;
1884 
1885 /* Network inteface name */
1886 char iface_name[IFNAMSIZ] = {'\0'};
1887 module_param_string(iface_name, iface_name, IFNAMSIZ, 0);
1888 
1889 /* The following are specific to the SDIO dongle */
1890 
1891 /* IOCTL response timeout */
1892 int dhd_ioctl_timeout_msec = IOCTL_RESP_TIMEOUT;
1893 
1894 /* DS Exit response timeout */
1895 int ds_exit_timeout_msec = DS_EXIT_TIMEOUT;
1896 
1897 /* Idle timeout for backplane clock */
1898 int dhd_idletime = DHD_IDLETIME_TICKS;
1899 module_param(dhd_idletime, int, 0);
1900 
1901 /* Use polling */
1902 uint dhd_poll = FALSE;
1903 module_param(dhd_poll, uint, 0);
1904 
1905 /* Use interrupts */
1906 uint dhd_intr = TRUE;
1907 module_param(dhd_intr, uint, 0);
1908 
1909 /* SDIO Drive Strength (in milliamps) */
1910 uint dhd_sdiod_drive_strength = 6;
1911 module_param(dhd_sdiod_drive_strength, uint, 0);
1912 
1913 #ifdef BCMSDIO
1914 /* Tx/Rx bounds */
1915 extern uint dhd_txbound;
1916 extern uint dhd_rxbound;
1917 module_param(dhd_txbound, uint, 0);
1918 module_param(dhd_rxbound, uint, 0);
1919 
1920 /* Deferred transmits */
1921 extern uint dhd_deferred_tx;
1922 module_param(dhd_deferred_tx, uint, 0);
1923 
1924 #endif /* BCMSDIO */
1925 
1926 
1927 #ifdef SDTEST
1928 /* Echo packet generator (pkts/s) */
1929 uint dhd_pktgen = 0;
1930 module_param(dhd_pktgen, uint, 0);
1931 
1932 /* Echo packet len (0 => sawtooth, max 2040) */
1933 uint dhd_pktgen_len = 0;
1934 module_param(dhd_pktgen_len, uint, 0);
1935 #endif /* SDTEST */
1936 
1937 
1938 
1939 #ifndef BCMDBUS
1940 /* Allow delayed firmware download for debug purpose */
1941 int allow_delay_fwdl = FALSE;
1942 module_param(allow_delay_fwdl, int, 0);
1943 #endif /* !BCMDBUS */
1944 
1945 extern char dhd_version[];
1946 extern char fw_version[];
1947 extern char clm_version[];
1948 
1949 int dhd_net_bus_devreset(struct net_device *dev, uint8 flag);
1950 static void dhd_net_if_lock_local(dhd_info_t *dhd);
1951 static void dhd_net_if_unlock_local(dhd_info_t *dhd);
1952 static void dhd_suspend_lock(dhd_pub_t *dhdp);
1953 static void dhd_suspend_unlock(dhd_pub_t *dhdp);
1954 
1955 #ifdef WLMEDIA_HTSF
1956 void htsf_update(dhd_info_t *dhd, void *data);
1957 tsf_t prev_tsf, cur_tsf;
1958 
1959 uint32 dhd_get_htsf(dhd_info_t *dhd, int ifidx);
1960 static int dhd_ioctl_htsf_get(dhd_info_t *dhd, int ifidx);
1961 static void dhd_dump_latency(void);
1962 static void dhd_htsf_addtxts(dhd_pub_t *dhdp, void *pktbuf);
1963 static void dhd_htsf_addrxts(dhd_pub_t *dhdp, void *pktbuf);
1964 static void dhd_dump_htsfhisto(histo_t *his, char *s);
1965 #endif /* WLMEDIA_HTSF */
1966 
1967 /* Monitor interface */
1968 int dhd_monitor_init(void *dhd_pub);
1969 int dhd_monitor_uninit(void);
1970 
1971 
1972 #if defined(WL_WIRELESS_EXT)
1973 struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev);
1974 #endif /* defined(WL_WIRELESS_EXT) */
1975 
1976 #ifndef BCMDBUS
1977 static void dhd_dpc(ulong data);
1978 #endif /* !BCMDBUS */
1979 /* forward decl */
1980 extern int dhd_wait_pend8021x(struct net_device *dev);
1981 void dhd_os_wd_timer_extend(void *bus, bool extend);
1982 
1983 #ifdef TOE
1984 #ifndef BDC
1985 #error TOE requires BDC
1986 #endif /* !BDC */
1987 static int dhd_toe_get(dhd_info_t *dhd, int idx, uint32 *toe_ol);
1988 static int dhd_toe_set(dhd_info_t *dhd, int idx, uint32 toe_ol);
1989 #endif /* TOE */
1990 
1991 static int dhd_wl_host_event(dhd_info_t *dhd, int ifidx, void *pktdata, uint16 pktlen,
1992 		wl_event_msg_t *event_ptr, void **data_ptr);
1993 
1994 #if defined(CONFIG_PM_SLEEP)
dhd_pm_callback(struct notifier_block * nfb,unsigned long action,void * ignored)1995 static int dhd_pm_callback(struct notifier_block *nfb, unsigned long action, void *ignored)
1996 {
1997 	int ret = NOTIFY_DONE;
1998 	bool suspend = FALSE;
1999 
2000 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2001 #pragma GCC diagnostic push
2002 #pragma GCC diagnostic ignored "-Wcast-qual"
2003 #endif
2004 	dhd_info_t *dhdinfo = (dhd_info_t*)container_of(nfb, struct dhd_info, pm_notifier);
2005 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2006 #pragma GCC diagnostic pop
2007 #endif
2008 
2009 	BCM_REFERENCE(dhdinfo);
2010 	BCM_REFERENCE(suspend);
2011 
2012 	switch (action) {
2013 	case PM_HIBERNATION_PREPARE:
2014 	case PM_SUSPEND_PREPARE:
2015 		suspend = TRUE;
2016 		break;
2017 
2018 	case PM_POST_HIBERNATION:
2019 	case PM_POST_SUSPEND:
2020 		suspend = FALSE;
2021 		break;
2022 	}
2023 
2024 	printf("%s: action=%ld, suspend=%d, suspend_mode=%d\n",
2025 		__FUNCTION__, action, suspend, dhdinfo->pub.conf->suspend_mode);
2026 	if (suspend) {
2027 		DHD_OS_WAKE_LOCK_WAIVE(&dhdinfo->pub);
2028 		if (dhdinfo->pub.conf->suspend_mode == PM_NOTIFIER)
2029 			dhd_suspend_resume_helper(dhdinfo, suspend, 0);
2030 #if defined(SUPPORT_P2P_GO_PS) && defined(PROP_TXSTATUS)
2031 		dhd_wlfc_suspend(&dhdinfo->pub);
2032 #endif /* defined(SUPPORT_P2P_GO_PS) && defined(PROP_TXSTATUS) */
2033 		if (dhdinfo->pub.conf->suspend_mode == PM_NOTIFIER)
2034 			dhd_conf_set_suspend_resume(&dhdinfo->pub, suspend);
2035 		DHD_OS_WAKE_LOCK_RESTORE(&dhdinfo->pub);
2036 	} else {
2037 		if (dhdinfo->pub.conf->suspend_mode == PM_NOTIFIER)
2038 			dhd_conf_set_suspend_resume(&dhdinfo->pub, suspend);
2039 #if defined(SUPPORT_P2P_GO_PS) && defined(PROP_TXSTATUS)
2040 		dhd_wlfc_resume(&dhdinfo->pub);
2041 #endif /* defined(SUPPORT_P2P_GO_PS) && defined(PROP_TXSTATUS) */
2042 		if (dhdinfo->pub.conf->suspend_mode == PM_NOTIFIER)
2043 			dhd_suspend_resume_helper(dhdinfo, suspend, 0);
2044 	}
2045 
2046 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && (LINUX_VERSION_CODE <= \
2047         KERNEL_VERSION(2, 6, 39))
2048 	dhd_mmc_suspend = suspend;
2049 	smp_mb();
2050 #endif
2051 
2052 	return ret;
2053 }
2054 
2055 /* to make sure we won't register the same notifier twice, otherwise a loop is likely to be
2056  * created in kernel notifier link list (with 'next' pointing to itself)
2057  */
2058 static bool dhd_pm_notifier_registered = FALSE;
2059 
2060 extern int register_pm_notifier(struct notifier_block *nb);
2061 extern int unregister_pm_notifier(struct notifier_block *nb);
2062 #endif /* CONFIG_PM_SLEEP */
2063 
2064 /* Request scheduling of the bus rx frame */
2065 static void dhd_sched_rxf(dhd_pub_t *dhdp, void *skb);
2066 static void dhd_os_rxflock(dhd_pub_t *pub);
2067 static void dhd_os_rxfunlock(dhd_pub_t *pub);
2068 
2069 /** priv_link is the link between netdev and the dhdif and dhd_info structs. */
2070 typedef struct dhd_dev_priv {
2071 	dhd_info_t * dhd; /* cached pointer to dhd_info in netdevice priv */
2072 	dhd_if_t   * ifp; /* cached pointer to dhd_if in netdevice priv */
2073 	int          ifidx; /* interface index */
2074 	void       * lkup;
2075 } dhd_dev_priv_t;
2076 
2077 #define DHD_DEV_PRIV_SIZE       (sizeof(dhd_dev_priv_t))
2078 #define DHD_DEV_PRIV(dev)       ((dhd_dev_priv_t *)DEV_PRIV(dev))
2079 #define DHD_DEV_INFO(dev)       (((dhd_dev_priv_t *)DEV_PRIV(dev))->dhd)
2080 #define DHD_DEV_IFP(dev)        (((dhd_dev_priv_t *)DEV_PRIV(dev))->ifp)
2081 #define DHD_DEV_IFIDX(dev)      (((dhd_dev_priv_t *)DEV_PRIV(dev))->ifidx)
2082 #define DHD_DEV_LKUP(dev)		(((dhd_dev_priv_t *)DEV_PRIV(dev))->lkup)
2083 
2084 #if defined(DHD_OF_SUPPORT)
2085 extern int dhd_wlan_init(void);
2086 #endif /* defined(DHD_OF_SUPPORT) */
2087 /** Clear the dhd net_device's private structure. */
2088 static inline void
dhd_dev_priv_clear(struct net_device * dev)2089 dhd_dev_priv_clear(struct net_device * dev)
2090 {
2091 	dhd_dev_priv_t * dev_priv;
2092 	ASSERT(dev != (struct net_device *)NULL);
2093 	dev_priv = DHD_DEV_PRIV(dev);
2094 	dev_priv->dhd = (dhd_info_t *)NULL;
2095 	dev_priv->ifp = (dhd_if_t *)NULL;
2096 	dev_priv->ifidx = DHD_BAD_IF;
2097 	dev_priv->lkup = (void *)NULL;
2098 }
2099 
2100 /** Setup the dhd net_device's private structure. */
2101 static inline void
dhd_dev_priv_save(struct net_device * dev,dhd_info_t * dhd,dhd_if_t * ifp,int ifidx)2102 dhd_dev_priv_save(struct net_device * dev, dhd_info_t * dhd, dhd_if_t * ifp,
2103                   int ifidx)
2104 {
2105 	dhd_dev_priv_t * dev_priv;
2106 	ASSERT(dev != (struct net_device *)NULL);
2107 	dev_priv = DHD_DEV_PRIV(dev);
2108 	dev_priv->dhd = dhd;
2109 	dev_priv->ifp = ifp;
2110 	dev_priv->ifidx = ifidx;
2111 }
2112 
2113 #ifdef PCIE_FULL_DONGLE
2114 
2115 /** Dummy objects are defined with state representing bad|down.
2116  * Performance gains from reducing branch conditionals, instruction parallelism,
2117  * dual issue, reducing load shadows, avail of larger pipelines.
2118  * Use DHD_XXX_NULL instead of (dhd_xxx_t *)NULL, whenever an object pointer
2119  * is accessed via the dhd_sta_t.
2120  */
2121 
2122 /* Dummy dhd_info object */
2123 dhd_info_t dhd_info_null = {
2124 #if defined(BCM_GMAC3)
2125 	.fwdh = FWDER_NULL,
2126 #endif
2127 	.pub = {
2128 	         .info = &dhd_info_null,
2129 #ifdef DHDTCPACK_SUPPRESS
2130 	         .tcpack_sup_mode = TCPACK_SUP_REPLACE,
2131 #endif /* DHDTCPACK_SUPPRESS */
2132 #if defined(TRAFFIC_MGMT_DWM)
2133 	         .dhd_tm_dwm_tbl = { .dhd_dwm_enabled = TRUE },
2134 #endif
2135 	         .up = FALSE,
2136 	         .busstate = DHD_BUS_DOWN
2137 	}
2138 };
2139 #define DHD_INFO_NULL (&dhd_info_null)
2140 #define DHD_PUB_NULL  (&dhd_info_null.pub)
2141 
2142 /* Dummy netdevice object */
2143 struct net_device dhd_net_dev_null = {
2144 	.reg_state = NETREG_UNREGISTERED
2145 };
2146 #define DHD_NET_DEV_NULL (&dhd_net_dev_null)
2147 
2148 /* Dummy dhd_if object */
2149 dhd_if_t dhd_if_null = {
2150 #if defined(BCM_GMAC3)
2151 	.fwdh = FWDER_NULL,
2152 #endif
2153 #ifdef WMF
2154 	.wmf = { .wmf_enable = TRUE },
2155 #endif
2156 	.info = DHD_INFO_NULL,
2157 	.net = DHD_NET_DEV_NULL,
2158 	.idx = DHD_BAD_IF
2159 };
2160 #define DHD_IF_NULL  (&dhd_if_null)
2161 
2162 #define DHD_STA_NULL ((dhd_sta_t *)NULL)
2163 
2164 /** Interface STA list management. */
2165 
2166 /** Fetch the dhd_if object, given the interface index in the dhd. */
2167 static inline dhd_if_t *dhd_get_ifp(dhd_pub_t *dhdp, uint32 ifidx);
2168 
2169 /** Alloc/Free a dhd_sta object from the dhd instances' sta_pool. */
2170 static void dhd_sta_free(dhd_pub_t *pub, dhd_sta_t *sta);
2171 static dhd_sta_t * dhd_sta_alloc(dhd_pub_t * dhdp);
2172 
2173 /* Delete a dhd_sta or flush all dhd_sta in an interface's sta_list. */
2174 static void dhd_if_del_sta_list(dhd_if_t * ifp);
2175 static void	dhd_if_flush_sta(dhd_if_t * ifp);
2176 
2177 /* Construct/Destruct a sta pool. */
2178 static int dhd_sta_pool_init(dhd_pub_t *dhdp, int max_sta);
2179 static void dhd_sta_pool_fini(dhd_pub_t *dhdp, int max_sta);
2180 /* Clear the pool of dhd_sta_t objects for built-in type driver */
2181 static void dhd_sta_pool_clear(dhd_pub_t *dhdp, int max_sta);
2182 
2183 
2184 /* Return interface pointer */
dhd_get_ifp(dhd_pub_t * dhdp,uint32 ifidx)2185 static inline dhd_if_t *dhd_get_ifp(dhd_pub_t *dhdp, uint32 ifidx)
2186 {
2187 	ASSERT(ifidx < DHD_MAX_IFS);
2188 
2189 	if (ifidx >= DHD_MAX_IFS)
2190 		return NULL;
2191 
2192 	return dhdp->info->iflist[ifidx];
2193 }
2194 
2195 /** Reset a dhd_sta object and free into the dhd pool. */
2196 static void
dhd_sta_free(dhd_pub_t * dhdp,dhd_sta_t * sta)2197 dhd_sta_free(dhd_pub_t * dhdp, dhd_sta_t * sta)
2198 {
2199 	int prio;
2200 
2201 	ASSERT((sta != DHD_STA_NULL) && (sta->idx != ID16_INVALID));
2202 
2203 	ASSERT((dhdp->staid_allocator != NULL) && (dhdp->sta_pool != NULL));
2204 
2205 	/*
2206 	 * Flush and free all packets in all flowring's queues belonging to sta.
2207 	 * Packets in flow ring will be flushed later.
2208 	 */
2209 	for (prio = 0; prio < (int)NUMPRIO; prio++) {
2210 		uint16 flowid = sta->flowid[prio];
2211 
2212 		if (flowid != FLOWID_INVALID) {
2213 			unsigned long flags;
2214 			flow_queue_t * queue = dhd_flow_queue(dhdp, flowid);
2215 			flow_ring_node_t * flow_ring_node;
2216 
2217 #ifdef DHDTCPACK_SUPPRESS
2218 			/* Clean tcp_ack_info_tbl in order to prevent access to flushed pkt,
2219 			 * when there is a newly coming packet from network stack.
2220 			 */
2221 			dhd_tcpack_info_tbl_clean(dhdp);
2222 #endif /* DHDTCPACK_SUPPRESS */
2223 
2224 			flow_ring_node = dhd_flow_ring_node(dhdp, flowid);
2225 			DHD_FLOWRING_LOCK(flow_ring_node->lock, flags);
2226 			flow_ring_node->status = FLOW_RING_STATUS_STA_FREEING;
2227 
2228 			if (!DHD_FLOW_QUEUE_EMPTY(queue)) {
2229 				void * pkt;
2230 				while ((pkt = dhd_flow_queue_dequeue(dhdp, queue)) != NULL) {
2231 					PKTFREE(dhdp->osh, pkt, TRUE);
2232 				}
2233 			}
2234 
2235 			DHD_FLOWRING_UNLOCK(flow_ring_node->lock, flags);
2236 			ASSERT(DHD_FLOW_QUEUE_EMPTY(queue));
2237 		}
2238 
2239 		sta->flowid[prio] = FLOWID_INVALID;
2240 	}
2241 
2242 	id16_map_free(dhdp->staid_allocator, sta->idx);
2243 	DHD_CUMM_CTR_INIT(&sta->cumm_ctr);
2244 	sta->ifp = DHD_IF_NULL; /* dummy dhd_if object */
2245 	sta->ifidx = DHD_BAD_IF;
2246 	bzero(sta->ea.octet, ETHER_ADDR_LEN);
2247 	INIT_LIST_HEAD(&sta->list);
2248 	sta->idx = ID16_INVALID; /* implying free */
2249 }
2250 
2251 /** Allocate a dhd_sta object from the dhd pool. */
2252 static dhd_sta_t *
dhd_sta_alloc(dhd_pub_t * dhdp)2253 dhd_sta_alloc(dhd_pub_t * dhdp)
2254 {
2255 	uint16 idx;
2256 	dhd_sta_t * sta;
2257 	dhd_sta_pool_t * sta_pool;
2258 
2259 	ASSERT((dhdp->staid_allocator != NULL) && (dhdp->sta_pool != NULL));
2260 
2261 	idx = id16_map_alloc(dhdp->staid_allocator);
2262 	if (idx == ID16_INVALID) {
2263 		DHD_ERROR(("%s: cannot get free staid\n", __FUNCTION__));
2264 		return DHD_STA_NULL;
2265 	}
2266 
2267 	sta_pool = (dhd_sta_pool_t *)(dhdp->sta_pool);
2268 	sta = &sta_pool[idx];
2269 
2270 	ASSERT((sta->idx == ID16_INVALID) &&
2271 	       (sta->ifp == DHD_IF_NULL) && (sta->ifidx == DHD_BAD_IF));
2272 
2273 	DHD_CUMM_CTR_INIT(&sta->cumm_ctr);
2274 
2275 	sta->idx = idx; /* implying allocated */
2276 
2277 	return sta;
2278 }
2279 
2280 /** Delete all STAs in an interface's STA list. */
2281 static void
dhd_if_del_sta_list(dhd_if_t * ifp)2282 dhd_if_del_sta_list(dhd_if_t *ifp)
2283 {
2284 	dhd_sta_t *sta, *next;
2285 	unsigned long flags;
2286 
2287 	DHD_IF_STA_LIST_LOCK(ifp, flags);
2288 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2289 #pragma GCC diagnostic push
2290 #pragma GCC diagnostic ignored "-Wcast-qual"
2291 #endif
2292 	list_for_each_entry_safe(sta, next, &ifp->sta_list, list) {
2293 #if defined(BCM_GMAC3)
2294 		if (ifp->fwdh) {
2295 			/* Remove sta from WOFA forwarder. */
2296 			fwder_deassoc(ifp->fwdh, (uint16 *)(sta->ea.octet), (uintptr_t)sta);
2297 		}
2298 #endif /* BCM_GMAC3 */
2299 		list_del(&sta->list);
2300 		dhd_sta_free(&ifp->info->pub, sta);
2301 	}
2302 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2303 #pragma GCC diagnostic pop
2304 #endif
2305 	DHD_IF_STA_LIST_UNLOCK(ifp, flags);
2306 
2307 	return;
2308 }
2309 
2310 /** Router/GMAC3: Flush all station entries in the forwarder's WOFA database. */
2311 static void
dhd_if_flush_sta(dhd_if_t * ifp)2312 dhd_if_flush_sta(dhd_if_t * ifp)
2313 {
2314 #if defined(BCM_GMAC3)
2315 
2316 	if (ifp && (ifp->fwdh != FWDER_NULL)) {
2317 		dhd_sta_t *sta, *next;
2318 		unsigned long flags;
2319 
2320 		DHD_IF_STA_LIST_LOCK(ifp, flags);
2321 
2322 		list_for_each_entry_safe(sta, next, &ifp->sta_list, list) {
2323 			/* Remove any sta entry from WOFA forwarder. */
2324 			fwder_flush(ifp->fwdh, (uintptr_t)sta);
2325 		}
2326 
2327 		DHD_IF_STA_LIST_UNLOCK(ifp, flags);
2328 	}
2329 #endif /* BCM_GMAC3 */
2330 }
2331 
2332 /** Construct a pool of dhd_sta_t objects to be used by interfaces. */
2333 static int
dhd_sta_pool_init(dhd_pub_t * dhdp,int max_sta)2334 dhd_sta_pool_init(dhd_pub_t *dhdp, int max_sta)
2335 {
2336 	int idx, prio, sta_pool_memsz;
2337 	dhd_sta_t * sta;
2338 	dhd_sta_pool_t * sta_pool;
2339 	void * staid_allocator;
2340 
2341 	ASSERT(dhdp != (dhd_pub_t *)NULL);
2342 	ASSERT((dhdp->staid_allocator == NULL) && (dhdp->sta_pool == NULL));
2343 
2344 	/* dhd_sta objects per radio are managed in a table. id#0 reserved. */
2345 	staid_allocator = id16_map_init(dhdp->osh, max_sta, 1);
2346 	if (staid_allocator == NULL) {
2347 		DHD_ERROR(("%s: sta id allocator init failure\n", __FUNCTION__));
2348 		return BCME_ERROR;
2349 	}
2350 
2351 	/* Pre allocate a pool of dhd_sta objects (one extra). */
2352 	sta_pool_memsz = ((max_sta + 1) * sizeof(dhd_sta_t)); /* skip idx 0 */
2353 	sta_pool = (dhd_sta_pool_t *)MALLOC(dhdp->osh, sta_pool_memsz);
2354 	if (sta_pool == NULL) {
2355 		DHD_ERROR(("%s: sta table alloc failure\n", __FUNCTION__));
2356 		id16_map_fini(dhdp->osh, staid_allocator);
2357 		return BCME_ERROR;
2358 	}
2359 
2360 	dhdp->sta_pool = sta_pool;
2361 	dhdp->staid_allocator = staid_allocator;
2362 
2363 	/* Initialize all sta(s) for the pre-allocated free pool. */
2364 	bzero((uchar *)sta_pool, sta_pool_memsz);
2365 	for (idx = max_sta; idx >= 1; idx--) { /* skip sta_pool[0] */
2366 		sta = &sta_pool[idx];
2367 		sta->idx = id16_map_alloc(staid_allocator);
2368 		ASSERT(sta->idx <= max_sta);
2369 	}
2370 	/* Now place them into the pre-allocated free pool. */
2371 	for (idx = 1; idx <= max_sta; idx++) {
2372 		sta = &sta_pool[idx];
2373 		for (prio = 0; prio < (int)NUMPRIO; prio++) {
2374 			sta->flowid[prio] = FLOWID_INVALID; /* Flow rings do not exist */
2375 		}
2376 		dhd_sta_free(dhdp, sta);
2377 	}
2378 
2379 	return BCME_OK;
2380 }
2381 
2382 /** Destruct the pool of dhd_sta_t objects.
2383  * Caller must ensure that no STA objects are currently associated with an if.
2384  */
2385 static void
dhd_sta_pool_fini(dhd_pub_t * dhdp,int max_sta)2386 dhd_sta_pool_fini(dhd_pub_t *dhdp, int max_sta)
2387 {
2388 	dhd_sta_pool_t * sta_pool = (dhd_sta_pool_t *)dhdp->sta_pool;
2389 
2390 	if (sta_pool) {
2391 		int idx;
2392 		int sta_pool_memsz = ((max_sta + 1) * sizeof(dhd_sta_t));
2393 		for (idx = 1; idx <= max_sta; idx++) {
2394 			ASSERT(sta_pool[idx].ifp == DHD_IF_NULL);
2395 			ASSERT(sta_pool[idx].idx == ID16_INVALID);
2396 		}
2397 		MFREE(dhdp->osh, dhdp->sta_pool, sta_pool_memsz);
2398 		dhdp->sta_pool = NULL;
2399 	}
2400 
2401 	id16_map_fini(dhdp->osh, dhdp->staid_allocator);
2402 	dhdp->staid_allocator = NULL;
2403 }
2404 
2405 /* Clear the pool of dhd_sta_t objects for built-in type driver */
2406 static void
dhd_sta_pool_clear(dhd_pub_t * dhdp,int max_sta)2407 dhd_sta_pool_clear(dhd_pub_t *dhdp, int max_sta)
2408 {
2409 	int idx, prio, sta_pool_memsz;
2410 	dhd_sta_t * sta;
2411 	dhd_sta_pool_t * sta_pool;
2412 	void *staid_allocator;
2413 
2414 	if (!dhdp) {
2415 		DHD_ERROR(("%s: dhdp is NULL\n", __FUNCTION__));
2416 		return;
2417 	}
2418 
2419 	sta_pool = (dhd_sta_pool_t *)dhdp->sta_pool;
2420 	staid_allocator = dhdp->staid_allocator;
2421 
2422 	if (!sta_pool) {
2423 		DHD_ERROR(("%s: sta_pool is NULL\n", __FUNCTION__));
2424 		return;
2425 	}
2426 
2427 	if (!staid_allocator) {
2428 		DHD_ERROR(("%s: staid_allocator is NULL\n", __FUNCTION__));
2429 		return;
2430 	}
2431 
2432 	/* clear free pool */
2433 	sta_pool_memsz = ((max_sta + 1) * sizeof(dhd_sta_t));
2434 	bzero((uchar *)sta_pool, sta_pool_memsz);
2435 
2436 	/* dhd_sta objects per radio are managed in a table. id#0 reserved. */
2437 	id16_map_clear(staid_allocator, max_sta, 1);
2438 
2439 	/* Initialize all sta(s) for the pre-allocated free pool. */
2440 	for (idx = max_sta; idx >= 1; idx--) { /* skip sta_pool[0] */
2441 		sta = &sta_pool[idx];
2442 		sta->idx = id16_map_alloc(staid_allocator);
2443 		ASSERT(sta->idx <= max_sta);
2444 	}
2445 	/* Now place them into the pre-allocated free pool. */
2446 	for (idx = 1; idx <= max_sta; idx++) {
2447 		sta = &sta_pool[idx];
2448 		for (prio = 0; prio < (int)NUMPRIO; prio++) {
2449 			sta->flowid[prio] = FLOWID_INVALID; /* Flow rings do not exist */
2450 		}
2451 		dhd_sta_free(dhdp, sta);
2452 	}
2453 }
2454 
2455 /** Find STA with MAC address ea in an interface's STA list. */
2456 dhd_sta_t *
dhd_find_sta(void * pub,int ifidx,void * ea)2457 dhd_find_sta(void *pub, int ifidx, void *ea)
2458 {
2459 	dhd_sta_t *sta;
2460 	dhd_if_t *ifp;
2461 	unsigned long flags;
2462 
2463 	ASSERT(ea != NULL);
2464 	ifp = dhd_get_ifp((dhd_pub_t *)pub, ifidx);
2465 	if (ifp == NULL)
2466 		return DHD_STA_NULL;
2467 
2468 	DHD_IF_STA_LIST_LOCK(ifp, flags);
2469 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2470 #pragma GCC diagnostic push
2471 #pragma GCC diagnostic ignored "-Wcast-qual"
2472 #endif
2473 	list_for_each_entry(sta, &ifp->sta_list, list) {
2474 		if (!memcmp(sta->ea.octet, ea, ETHER_ADDR_LEN)) {
2475 			DHD_INFO(("%s: found STA " MACDBG "\n",
2476 				__FUNCTION__, MAC2STRDBG((char *)ea)));
2477 			DHD_IF_STA_LIST_UNLOCK(ifp, flags);
2478 			return sta;
2479 		}
2480 	}
2481 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2482 #pragma GCC diagnostic pop
2483 #endif
2484 	DHD_IF_STA_LIST_UNLOCK(ifp, flags);
2485 
2486 	return DHD_STA_NULL;
2487 }
2488 
2489 /** Add STA into the interface's STA list. */
2490 dhd_sta_t *
dhd_add_sta(void * pub,int ifidx,void * ea)2491 dhd_add_sta(void *pub, int ifidx, void *ea)
2492 {
2493 	dhd_sta_t *sta;
2494 	dhd_if_t *ifp;
2495 	unsigned long flags;
2496 
2497 	ASSERT(ea != NULL);
2498 	ifp = dhd_get_ifp((dhd_pub_t *)pub, ifidx);
2499 	if (ifp == NULL)
2500 		return DHD_STA_NULL;
2501 
2502 	sta = dhd_sta_alloc((dhd_pub_t *)pub);
2503 	if (sta == DHD_STA_NULL) {
2504 		DHD_ERROR(("%s: Alloc failed\n", __FUNCTION__));
2505 		return DHD_STA_NULL;
2506 	}
2507 
2508 	memcpy(sta->ea.octet, ea, ETHER_ADDR_LEN);
2509 
2510 	/* link the sta and the dhd interface */
2511 	sta->ifp = ifp;
2512 	sta->ifidx = ifidx;
2513 #ifdef DHD_WMF
2514 	sta->psta_prim = NULL;
2515 #endif
2516 	INIT_LIST_HEAD(&sta->list);
2517 
2518 	DHD_IF_STA_LIST_LOCK(ifp, flags);
2519 
2520 	list_add_tail(&sta->list, &ifp->sta_list);
2521 
2522 #if defined(BCM_GMAC3)
2523 	if (ifp->fwdh) {
2524 		ASSERT(ISALIGNED(ea, 2));
2525 		/* Add sta to WOFA forwarder. */
2526 		fwder_reassoc(ifp->fwdh, (uint16 *)ea, (uintptr_t)sta);
2527 	}
2528 #endif /* BCM_GMAC3 */
2529 
2530 	DHD_IF_STA_LIST_UNLOCK(ifp, flags);
2531 
2532 	return sta;
2533 }
2534 
2535 /** Delete all STAs from the interface's STA list. */
2536 void
dhd_del_all_sta(void * pub,int ifidx)2537 dhd_del_all_sta(void *pub, int ifidx)
2538 {
2539 	dhd_sta_t *sta, *next;
2540 	dhd_if_t *ifp;
2541 	unsigned long flags;
2542 
2543 	ifp = dhd_get_ifp((dhd_pub_t *)pub, ifidx);
2544 	if (ifp == NULL)
2545 		return;
2546 
2547 	DHD_IF_STA_LIST_LOCK(ifp, flags);
2548 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2549 #pragma GCC diagnostic push
2550 #pragma GCC diagnostic ignored "-Wcast-qual"
2551 #endif
2552 	list_for_each_entry_safe(sta, next, &ifp->sta_list, list) {
2553 #if defined(BCM_GMAC3)
2554 		if (ifp->fwdh) { /* Found a sta, remove from WOFA forwarder. */
2555 			ASSERT(ISALIGNED(sta->ea.octet, 2));
2556 			fwder_deassoc(ifp->fwdh, (uint16 *)sta->ea.octet, (uintptr_t)sta);
2557 		}
2558 #endif /* BCM_GMAC3 */
2559 
2560 		list_del(&sta->list);
2561 		dhd_sta_free(&ifp->info->pub, sta);
2562 #ifdef DHD_L2_FILTER
2563 		if (ifp->parp_enable) {
2564 			/* clear Proxy ARP cache of specific Ethernet Address */
2565 			bcm_l2_filter_arp_table_update(((dhd_pub_t*)pub)->osh,
2566 					ifp->phnd_arp_table, FALSE,
2567 					sta->ea.octet, FALSE, ((dhd_pub_t*)pub)->tickcnt);
2568 		}
2569 #endif /* DHD_L2_FILTER */
2570 	}
2571 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2572 #pragma GCC diagnostic pop
2573 #endif
2574 	DHD_IF_STA_LIST_UNLOCK(ifp, flags);
2575 
2576 	return;
2577 }
2578 
2579 /** Delete STA from the interface's STA list. */
2580 void
dhd_del_sta(void * pub,int ifidx,void * ea)2581 dhd_del_sta(void *pub, int ifidx, void *ea)
2582 {
2583 	dhd_sta_t *sta, *next;
2584 	dhd_if_t *ifp;
2585 	unsigned long flags;
2586 	char macstr[ETHER_ADDR_STR_LEN];
2587 
2588 	ASSERT(ea != NULL);
2589 	ifp = dhd_get_ifp((dhd_pub_t *)pub, ifidx);
2590 	if (ifp == NULL)
2591 		return;
2592 
2593 	DHD_IF_STA_LIST_LOCK(ifp, flags);
2594 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2595 #pragma GCC diagnostic push
2596 #pragma GCC diagnostic ignored "-Wcast-qual"
2597 #endif
2598 	list_for_each_entry_safe(sta, next, &ifp->sta_list, list) {
2599 		if (!memcmp(sta->ea.octet, ea, ETHER_ADDR_LEN)) {
2600 #if defined(BCM_GMAC3)
2601 			if (ifp->fwdh) { /* Found a sta, remove from WOFA forwarder. */
2602 				ASSERT(ISALIGNED(ea, 2));
2603 				fwder_deassoc(ifp->fwdh, (uint16 *)ea, (uintptr_t)sta);
2604 			}
2605 #endif /* BCM_GMAC3 */
2606 			DHD_MAC_TO_STR(((char *)ea), macstr);
2607 			DHD_ERROR(("%s: Deleting STA  %s\n", __FUNCTION__, macstr));
2608 			list_del(&sta->list);
2609 			dhd_sta_free(&ifp->info->pub, sta);
2610 		}
2611 	}
2612 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2613 #pragma GCC diagnostic pop
2614 #endif
2615 	DHD_IF_STA_LIST_UNLOCK(ifp, flags);
2616 #ifdef DHD_L2_FILTER
2617 	if (ifp->parp_enable) {
2618 		/* clear Proxy ARP cache of specific Ethernet Address */
2619 		bcm_l2_filter_arp_table_update(((dhd_pub_t*)pub)->osh, ifp->phnd_arp_table, FALSE,
2620 			ea, FALSE, ((dhd_pub_t*)pub)->tickcnt);
2621 	}
2622 #endif /* DHD_L2_FILTER */
2623 	return;
2624 }
2625 
2626 /** Add STA if it doesn't exist. Not reentrant. */
2627 dhd_sta_t*
dhd_findadd_sta(void * pub,int ifidx,void * ea)2628 dhd_findadd_sta(void *pub, int ifidx, void *ea)
2629 {
2630 	dhd_sta_t *sta;
2631 
2632 	sta = dhd_find_sta(pub, ifidx, ea);
2633 
2634 	if (!sta) {
2635 		/* Add entry */
2636 		sta = dhd_add_sta(pub, ifidx, ea);
2637 	}
2638 
2639 	return sta;
2640 }
2641 
2642 #if defined(DHD_IGMP_UCQUERY) || defined(DHD_UCAST_UPNP)
2643 #if !defined(BCM_GMAC3)
2644 static struct list_head *
dhd_sta_list_snapshot(dhd_info_t * dhd,dhd_if_t * ifp,struct list_head * snapshot_list)2645 dhd_sta_list_snapshot(dhd_info_t *dhd, dhd_if_t *ifp, struct list_head *snapshot_list)
2646 {
2647 	unsigned long flags;
2648 	dhd_sta_t *sta, *snapshot;
2649 
2650 	INIT_LIST_HEAD(snapshot_list);
2651 
2652 	DHD_IF_STA_LIST_LOCK(ifp, flags);
2653 
2654 	list_for_each_entry(sta, &ifp->sta_list, list) {
2655 		/* allocate one and add to snapshot */
2656 		snapshot = (dhd_sta_t *)MALLOC(dhd->pub.osh, sizeof(dhd_sta_t));
2657 		if (snapshot == NULL) {
2658 			DHD_ERROR(("%s: Cannot allocate memory\n", __FUNCTION__));
2659 			continue;
2660 		}
2661 
2662 		memcpy(snapshot->ea.octet, sta->ea.octet, ETHER_ADDR_LEN);
2663 
2664 		INIT_LIST_HEAD(&snapshot->list);
2665 		list_add_tail(&snapshot->list, snapshot_list);
2666 	}
2667 
2668 	DHD_IF_STA_LIST_UNLOCK(ifp, flags);
2669 
2670 	return snapshot_list;
2671 }
2672 
2673 static void
dhd_sta_list_snapshot_free(dhd_info_t * dhd,struct list_head * snapshot_list)2674 dhd_sta_list_snapshot_free(dhd_info_t *dhd, struct list_head *snapshot_list)
2675 {
2676 	dhd_sta_t *sta, *next;
2677 
2678 	list_for_each_entry_safe(sta, next, snapshot_list, list) {
2679 		list_del(&sta->list);
2680 		MFREE(dhd->pub.osh, sta, sizeof(dhd_sta_t));
2681 	}
2682 }
2683 #endif /* !BCM_GMAC3 */
2684 #endif /* DHD_IGMP_UCQUERY || DHD_UCAST_UPNP */
2685 
2686 #else
dhd_if_flush_sta(dhd_if_t * ifp)2687 static inline void dhd_if_flush_sta(dhd_if_t * ifp) { }
dhd_if_del_sta_list(dhd_if_t * ifp)2688 static inline void dhd_if_del_sta_list(dhd_if_t *ifp) {}
dhd_sta_pool_init(dhd_pub_t * dhdp,int max_sta)2689 static inline int dhd_sta_pool_init(dhd_pub_t *dhdp, int max_sta) { return BCME_OK; }
dhd_sta_pool_fini(dhd_pub_t * dhdp,int max_sta)2690 static inline void dhd_sta_pool_fini(dhd_pub_t *dhdp, int max_sta) {}
dhd_sta_pool_clear(dhd_pub_t * dhdp,int max_sta)2691 static inline void dhd_sta_pool_clear(dhd_pub_t *dhdp, int max_sta) {}
dhd_findadd_sta(void * pub,int ifidx,void * ea)2692 dhd_sta_t *dhd_findadd_sta(void *pub, int ifidx, void *ea) { return NULL; }
dhd_find_sta(void * pub,int ifidx,void * ea)2693 dhd_sta_t *dhd_find_sta(void *pub, int ifidx, void *ea) { return NULL; }
dhd_del_sta(void * pub,int ifidx,void * ea)2694 void dhd_del_sta(void *pub, int ifidx, void *ea) {}
2695 #endif /* PCIE_FULL_DONGLE */
2696 
2697 
2698 
2699 #if defined(DHD_LB)
2700 
2701 #if defined(DHD_LB_TXC) || defined(DHD_LB_RXC) || defined(DHD_LB_TXP)
2702 /**
2703  * dhd_tasklet_schedule - Function that runs in IPI context of the destination
2704  * CPU and schedules a tasklet.
2705  * @tasklet: opaque pointer to the tasklet
2706  */
2707 INLINE void
dhd_tasklet_schedule(void * tasklet)2708 dhd_tasklet_schedule(void *tasklet)
2709 {
2710 	tasklet_schedule((struct tasklet_struct *)tasklet);
2711 }
2712 /**
2713  * dhd_tasklet_schedule_on - Executes the passed takslet in a given CPU
2714  * @tasklet: tasklet to be scheduled
2715  * @on_cpu: cpu core id
2716  *
2717  * If the requested cpu is online, then an IPI is sent to this cpu via the
2718  * smp_call_function_single with no wait and the tasklet_schedule function
2719  * will be invoked to schedule the specified tasklet on the requested CPU.
2720  */
2721 INLINE void
dhd_tasklet_schedule_on(struct tasklet_struct * tasklet,int on_cpu)2722 dhd_tasklet_schedule_on(struct tasklet_struct *tasklet, int on_cpu)
2723 {
2724 	const int wait = 0;
2725 	smp_call_function_single(on_cpu,
2726 		dhd_tasklet_schedule, (void *)tasklet, wait);
2727 }
2728 
2729 /**
2730  * dhd_work_schedule_on - Executes the passed work in a given CPU
2731  * @work: work to be scheduled
2732  * @on_cpu: cpu core id
2733  *
2734  * If the requested cpu is online, then an IPI is sent to this cpu via the
2735  * schedule_work_on and the work function
2736  * will be invoked to schedule the specified work on the requested CPU.
2737  */
2738 
2739 INLINE void
dhd_work_schedule_on(struct work_struct * work,int on_cpu)2740 dhd_work_schedule_on(struct work_struct *work, int on_cpu)
2741 {
2742 	schedule_work_on(on_cpu, work);
2743 }
2744 #endif /* DHD_LB_TXC || DHD_LB_RXC || DHD_LB_TXP */
2745 
2746 #if defined(DHD_LB_TXC)
2747 /**
2748  * dhd_lb_tx_compl_dispatch - load balance by dispatching the tx_compl_tasklet
2749  * on another cpu. The tx_compl_tasklet will take care of DMA unmapping and
2750  * freeing the packets placed in the tx_compl workq
2751  */
2752 void
dhd_lb_tx_compl_dispatch(dhd_pub_t * dhdp)2753 dhd_lb_tx_compl_dispatch(dhd_pub_t *dhdp)
2754 {
2755 	dhd_info_t *dhd = dhdp->info;
2756 	int curr_cpu, on_cpu;
2757 
2758 	if (dhd->rx_napi_netdev == NULL) {
2759 		DHD_ERROR(("%s: dhd->rx_napi_netdev is NULL\n", __FUNCTION__));
2760 		return;
2761 	}
2762 
2763 	DHD_LB_STATS_INCR(dhd->txc_sched_cnt);
2764 	/*
2765 	 * If the destination CPU is NOT online or is same as current CPU
2766 	 * no need to schedule the work
2767 	 */
2768 	curr_cpu = get_cpu();
2769 	put_cpu();
2770 
2771 	on_cpu = atomic_read(&dhd->tx_compl_cpu);
2772 
2773 	if ((on_cpu == curr_cpu) || (!cpu_online(on_cpu))) {
2774 		dhd_tasklet_schedule(&dhd->tx_compl_tasklet);
2775 	} else {
2776 		schedule_work(&dhd->tx_compl_dispatcher_work);
2777 	}
2778 }
2779 
dhd_tx_compl_dispatcher_fn(struct work_struct * work)2780 static void dhd_tx_compl_dispatcher_fn(struct work_struct * work)
2781 {
2782 	struct dhd_info *dhd =
2783 		container_of(work, struct dhd_info, tx_compl_dispatcher_work);
2784 	int cpu;
2785 
2786 	get_online_cpus();
2787 	cpu = atomic_read(&dhd->tx_compl_cpu);
2788 	if (!cpu_online(cpu))
2789 		dhd_tasklet_schedule(&dhd->tx_compl_tasklet);
2790 	else
2791 		dhd_tasklet_schedule_on(&dhd->tx_compl_tasklet, cpu);
2792 	put_online_cpus();
2793 }
2794 #endif /* DHD_LB_TXC */
2795 
2796 #if defined(DHD_LB_RXC)
2797 /**
2798  * dhd_lb_rx_compl_dispatch - load balance by dispatching the rx_compl_tasklet
2799  * on another cpu. The rx_compl_tasklet will take care of reposting rx buffers
2800  * in the H2D RxBuffer Post common ring, by using the recycled pktids that were
2801  * placed in the rx_compl workq.
2802  *
2803  * @dhdp: pointer to dhd_pub object
2804  */
2805 void
dhd_lb_rx_compl_dispatch(dhd_pub_t * dhdp)2806 dhd_lb_rx_compl_dispatch(dhd_pub_t *dhdp)
2807 {
2808 	dhd_info_t *dhd = dhdp->info;
2809 	int curr_cpu, on_cpu;
2810 
2811 	if (dhd->rx_napi_netdev == NULL) {
2812 		DHD_ERROR(("%s: dhd->rx_napi_netdev is NULL\n", __FUNCTION__));
2813 		return;
2814 	}
2815 
2816 	DHD_LB_STATS_INCR(dhd->rxc_sched_cnt);
2817 	/*
2818 	 * If the destination CPU is NOT online or is same as current CPU
2819 	 * no need to schedule the work
2820 	 */
2821 	curr_cpu = get_cpu();
2822 	put_cpu();
2823 	on_cpu = atomic_read(&dhd->rx_compl_cpu);
2824 
2825 	if ((on_cpu == curr_cpu) || (!cpu_online(on_cpu))) {
2826 		dhd_tasklet_schedule(&dhd->rx_compl_tasklet);
2827 	} else {
2828 		dhd_rx_compl_dispatcher_fn(dhdp);
2829 	}
2830 }
2831 
dhd_rx_compl_dispatcher_fn(dhd_pub_t * dhdp)2832 static void dhd_rx_compl_dispatcher_fn(dhd_pub_t *dhdp)
2833 {
2834 	struct dhd_info *dhd = dhdp->info;
2835 	int cpu;
2836 
2837 	preempt_disable();
2838 	cpu = atomic_read(&dhd->rx_compl_cpu);
2839 	if (!cpu_online(cpu))
2840 		dhd_tasklet_schedule(&dhd->rx_compl_tasklet);
2841 	else {
2842 		dhd_tasklet_schedule_on(&dhd->rx_compl_tasklet, cpu);
2843 	}
2844 	preempt_enable();
2845 }
2846 #endif /* DHD_LB_RXC */
2847 
2848 #if defined(DHD_LB_TXP)
dhd_tx_dispatcher_work(struct work_struct * work)2849 static void dhd_tx_dispatcher_work(struct work_struct * work)
2850 {
2851 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2852 #pragma GCC diagnostic push
2853 #pragma GCC diagnostic ignored "-Wcast-qual"
2854 #endif
2855 	struct dhd_info *dhd =
2856 		container_of(work, struct dhd_info, tx_dispatcher_work);
2857 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2858 #pragma GCC diagnostic pop
2859 #endif
2860 	dhd_tasklet_schedule(&dhd->tx_tasklet);
2861 }
2862 
dhd_tx_dispatcher_fn(dhd_pub_t * dhdp)2863 static void dhd_tx_dispatcher_fn(dhd_pub_t *dhdp)
2864 {
2865 	int cpu;
2866 	int net_tx_cpu;
2867 	dhd_info_t *dhd = dhdp->info;
2868 
2869 	preempt_disable();
2870 	cpu = atomic_read(&dhd->tx_cpu);
2871 	net_tx_cpu = atomic_read(&dhd->net_tx_cpu);
2872 
2873 	/*
2874 	 * Now if the NET_TX has pushed the packet in the same
2875 	 * CPU that is chosen for Tx processing, seperate it out
2876 	 * i.e run the TX processing tasklet in compl_cpu
2877 	 */
2878 	if (net_tx_cpu == cpu)
2879 		cpu = atomic_read(&dhd->tx_compl_cpu);
2880 
2881 	if (!cpu_online(cpu)) {
2882 		/*
2883 		 * Ooohh... but the Chosen CPU is not online,
2884 		 * Do the job in the current CPU itself.
2885 		 */
2886 		dhd_tasklet_schedule(&dhd->tx_tasklet);
2887 	} else {
2888 		/*
2889 		 * Schedule tx_dispatcher_work to on the cpu which
2890 		 * in turn will schedule tx_tasklet.
2891 		 */
2892 		dhd_work_schedule_on(&dhd->tx_dispatcher_work, cpu);
2893 	}
2894 	preempt_enable();
2895 }
2896 
2897 /**
2898  * dhd_lb_tx_dispatch - load balance by dispatching the tx_tasklet
2899  * on another cpu. The tx_tasklet will take care of actually putting
2900  * the skbs into appropriate flow ring and ringing H2D interrupt
2901  *
2902  * @dhdp: pointer to dhd_pub object
2903  */
2904 static void
dhd_lb_tx_dispatch(dhd_pub_t * dhdp)2905 dhd_lb_tx_dispatch(dhd_pub_t *dhdp)
2906 {
2907 	dhd_info_t *dhd = dhdp->info;
2908 	int curr_cpu;
2909 
2910 	curr_cpu = get_cpu();
2911 	put_cpu();
2912 
2913 	/* Record the CPU in which the TX request from Network stack came */
2914 	atomic_set(&dhd->net_tx_cpu, curr_cpu);
2915 
2916 	/* Schedule the work to dispatch ... */
2917 	dhd_tx_dispatcher_fn(dhdp);
2918 
2919 }
2920 #endif /* DHD_LB_TXP */
2921 
2922 #if defined(DHD_LB_RXP)
2923 /**
2924  * dhd_napi_poll - Load balance napi poll function to process received
2925  * packets and send up the network stack using netif_receive_skb()
2926  *
2927  * @napi: napi object in which context this poll function is invoked
2928  * @budget: number of packets to be processed.
2929  *
2930  * Fetch the dhd_info given the rx_napi_struct. Move all packets from the
2931  * rx_napi_queue into a local rx_process_queue (lock and queue move and unlock).
2932  * Dequeue each packet from head of rx_process_queue, fetch the ifid from the
2933  * packet tag and sendup.
2934  */
2935 static int
dhd_napi_poll(struct napi_struct * napi,int budget)2936 dhd_napi_poll(struct napi_struct *napi, int budget)
2937 {
2938 	int ifid;
2939 	const int pkt_count = 1;
2940 	const int chan = 0;
2941 	struct sk_buff * skb;
2942 	unsigned long flags;
2943 	struct dhd_info *dhd;
2944 	int processed = 0;
2945 	struct sk_buff_head rx_process_queue;
2946 
2947 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2948 #pragma GCC diagnostic push
2949 #pragma GCC diagnostic ignored "-Wcast-qual"
2950 #endif
2951 	dhd = container_of(napi, struct dhd_info, rx_napi_struct);
2952 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
2953 #pragma GCC diagnostic pop
2954 #endif
2955 
2956 	DHD_INFO(("%s napi_queue<%d> budget<%d>\n",
2957 		__FUNCTION__, skb_queue_len(&dhd->rx_napi_queue), budget));
2958 		__skb_queue_head_init(&rx_process_queue);
2959 
2960 	/* extract the entire rx_napi_queue into local rx_process_queue */
2961 	spin_lock_irqsave(&dhd->rx_napi_queue.lock, flags);
2962 	skb_queue_splice_tail_init(&dhd->rx_napi_queue, &rx_process_queue);
2963 	spin_unlock_irqrestore(&dhd->rx_napi_queue.lock, flags);
2964 
2965 	while ((skb = __skb_dequeue(&rx_process_queue)) != NULL) {
2966 		OSL_PREFETCH(skb->data);
2967 
2968 		ifid = DHD_PKTTAG_IFID((dhd_pkttag_fr_t *)PKTTAG(skb));
2969 
2970 		DHD_INFO(("%s dhd_rx_frame pkt<%p> ifid<%d>\n",
2971 			__FUNCTION__, skb, ifid));
2972 
2973 		dhd_rx_frame(&dhd->pub, ifid, skb, pkt_count, chan);
2974 		processed++;
2975 	}
2976 
2977 	DHD_LB_STATS_UPDATE_NAPI_HISTO(&dhd->pub, processed);
2978 
2979 	DHD_INFO(("%s processed %d\n", __FUNCTION__, processed));
2980 	napi_complete(napi);
2981 
2982 	return budget - 1;
2983 }
2984 
2985 /**
2986  * dhd_napi_schedule - Place the napi struct into the current cpus softnet napi
2987  * poll list. This function may be invoked via the smp_call_function_single
2988  * from a remote CPU.
2989  *
2990  * This function will essentially invoke __raise_softirq_irqoff(NET_RX_SOFTIRQ)
2991  * after the napi_struct is added to the softnet data's poll_list
2992  *
2993  * @info: pointer to a dhd_info struct
2994  */
2995 static void
dhd_napi_schedule(void * info)2996 dhd_napi_schedule(void *info)
2997 {
2998 	dhd_info_t *dhd = (dhd_info_t *)info;
2999 
3000 	DHD_INFO(("%s rx_napi_struct<%p> on cpu<%d>\n",
3001 		__FUNCTION__, &dhd->rx_napi_struct, atomic_read(&dhd->rx_napi_cpu)));
3002 
3003 	/* add napi_struct to softnet data poll list and raise NET_RX_SOFTIRQ */
3004 	if (napi_schedule_prep(&dhd->rx_napi_struct)) {
3005 		__napi_schedule(&dhd->rx_napi_struct);
3006 		DHD_LB_STATS_PERCPU_ARR_INCR(dhd->napi_percpu_run_cnt);
3007 	}
3008 
3009 	/*
3010 	 * If the rx_napi_struct was already running, then we let it complete
3011 	 * processing all its packets. The rx_napi_struct may only run on one
3012 	 * core at a time, to avoid out-of-order handling.
3013 	 */
3014 }
3015 
3016 /**
3017  * dhd_napi_schedule_on - API to schedule on a desired CPU core a NET_RX_SOFTIRQ
3018  * action after placing the dhd's rx_process napi object in the the remote CPU's
3019  * softnet data's poll_list.
3020  *
3021  * @dhd: dhd_info which has the rx_process napi object
3022  * @on_cpu: desired remote CPU id
3023  */
3024 static INLINE int
dhd_napi_schedule_on(dhd_info_t * dhd,int on_cpu)3025 dhd_napi_schedule_on(dhd_info_t *dhd, int on_cpu)
3026 {
3027 	int wait = 0; /* asynchronous IPI */
3028 	DHD_INFO(("%s dhd<%p> napi<%p> on_cpu<%d>\n",
3029 		__FUNCTION__, dhd, &dhd->rx_napi_struct, on_cpu));
3030 
3031 	if (smp_call_function_single(on_cpu, dhd_napi_schedule, dhd, wait)) {
3032 		DHD_ERROR(("%s smp_call_function_single on_cpu<%d> failed\n",
3033 			__FUNCTION__, on_cpu));
3034 	}
3035 
3036 	DHD_LB_STATS_INCR(dhd->napi_sched_cnt);
3037 
3038 	return 0;
3039 }
3040 
3041 /*
3042  * Call get_online_cpus/put_online_cpus around dhd_napi_schedule_on
3043  * Why should we do this?
3044  * The candidacy algorithm is run from the call back function
3045  * registered to CPU hotplug notifier. This call back happens from Worker
3046  * context. The dhd_napi_schedule_on is also from worker context.
3047  * Note that both of this can run on two different CPUs at the same time.
3048  * So we can possibly have a window where a given CPUn is being brought
3049  * down from CPUm while we try to run a function on CPUn.
3050  * To prevent this its better have the whole code to execute an SMP
3051  * function under get_online_cpus.
3052  * This function call ensures that hotplug mechanism does not kick-in
3053  * until we are done dealing with online CPUs
3054  * If the hotplug worker is already running, no worries because the
3055  * candidacy algo would then reflect the same in dhd->rx_napi_cpu.
3056  *
3057  * The below mentioned code structure is proposed in
3058  * https://www.kernel.org/doc/Documentation/cpu-hotplug.txt
3059  * for the question
3060  * Q: I need to ensure that a particular cpu is not removed when there is some
3061  *    work specific to this cpu is in progress
3062  *
3063  * According to the documentation calling get_online_cpus is NOT required, if
3064  * we are running from tasklet context. Since dhd_rx_napi_dispatcher_fn can
3065  * run from Work Queue context we have to call these functions
3066  */
dhd_rx_napi_dispatcher_fn(struct work_struct * work)3067 static void dhd_rx_napi_dispatcher_fn(struct work_struct * work)
3068 {
3069 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
3070 #pragma GCC diagnostic push
3071 #pragma GCC diagnostic ignored "-Wcast-qual"
3072 #endif
3073 	struct dhd_info *dhd =
3074 		container_of(work, struct dhd_info, rx_napi_dispatcher_work);
3075 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
3076 #pragma GCC diagnostic pop
3077 #endif
3078 	int cpu;
3079 
3080 	get_online_cpus();
3081 	cpu = atomic_read(&dhd->rx_napi_cpu);
3082 
3083 	if (!cpu_online(cpu))
3084 		dhd_napi_schedule(dhd);
3085 	else
3086 		dhd_napi_schedule_on(dhd, cpu);
3087 
3088 	put_online_cpus();
3089 }
3090 
3091 /**
3092  * dhd_lb_rx_napi_dispatch - load balance by dispatching the rx_napi_struct
3093  * to run on another CPU. The rx_napi_struct's poll function will retrieve all
3094  * the packets enqueued into the rx_napi_queue and sendup.
3095  * The producer's rx packet queue is appended to the rx_napi_queue before
3096  * dispatching the rx_napi_struct.
3097  */
3098 void
dhd_lb_rx_napi_dispatch(dhd_pub_t * dhdp)3099 dhd_lb_rx_napi_dispatch(dhd_pub_t *dhdp)
3100 {
3101 	unsigned long flags;
3102 	dhd_info_t *dhd = dhdp->info;
3103 	int curr_cpu;
3104 	int on_cpu;
3105 
3106 	if (dhd->rx_napi_netdev == NULL) {
3107 		DHD_ERROR(("%s: dhd->rx_napi_netdev is NULL\n", __FUNCTION__));
3108 		return;
3109 	}
3110 
3111 	DHD_INFO(("%s append napi_queue<%d> pend_queue<%d>\n", __FUNCTION__,
3112 		skb_queue_len(&dhd->rx_napi_queue), skb_queue_len(&dhd->rx_pend_queue)));
3113 
3114 	/* append the producer's queue of packets to the napi's rx process queue */
3115 	spin_lock_irqsave(&dhd->rx_napi_queue.lock, flags);
3116 	skb_queue_splice_tail_init(&dhd->rx_pend_queue, &dhd->rx_napi_queue);
3117 	spin_unlock_irqrestore(&dhd->rx_napi_queue.lock, flags);
3118 
3119 	/*
3120 	 * If the destination CPU is NOT online or is same as current CPU
3121 	 * no need to schedule the work
3122 	 */
3123 	curr_cpu = get_cpu();
3124 	put_cpu();
3125 
3126 	on_cpu = atomic_read(&dhd->rx_napi_cpu);
3127 	if ((on_cpu == curr_cpu) || (!cpu_online(on_cpu))) {
3128 		dhd_napi_schedule(dhd);
3129 	} else {
3130 		schedule_work(&dhd->rx_napi_dispatcher_work);
3131 	}
3132 }
3133 
3134 /**
3135  * dhd_lb_rx_pkt_enqueue - Enqueue the packet into the producer's queue
3136  */
3137 void
dhd_lb_rx_pkt_enqueue(dhd_pub_t * dhdp,void * pkt,int ifidx)3138 dhd_lb_rx_pkt_enqueue(dhd_pub_t *dhdp, void *pkt, int ifidx)
3139 {
3140 	dhd_info_t *dhd = dhdp->info;
3141 
3142 	DHD_INFO(("%s enqueue pkt<%p> ifidx<%d> pend_queue<%d>\n", __FUNCTION__,
3143 		pkt, ifidx, skb_queue_len(&dhd->rx_pend_queue)));
3144 	DHD_PKTTAG_SET_IFID((dhd_pkttag_fr_t *)PKTTAG(pkt), ifidx);
3145 	__skb_queue_tail(&dhd->rx_pend_queue, pkt);
3146 }
3147 #endif /* DHD_LB_RXP */
3148 
3149 #endif /* DHD_LB */
3150 
3151 
3152 /** Returns dhd iflist index corresponding the the bssidx provided by apps */
dhd_bssidx2idx(dhd_pub_t * dhdp,uint32 bssidx)3153 int dhd_bssidx2idx(dhd_pub_t *dhdp, uint32 bssidx)
3154 {
3155 	dhd_if_t *ifp;
3156 	dhd_info_t *dhd = dhdp->info;
3157 	int i;
3158 
3159 	ASSERT(bssidx < DHD_MAX_IFS);
3160 	ASSERT(dhdp);
3161 
3162 	for (i = 0; i < DHD_MAX_IFS; i++) {
3163 		ifp = dhd->iflist[i];
3164 		if (ifp && (ifp->bssidx == bssidx)) {
3165 			DHD_TRACE(("Index manipulated for %s from %d to %d\n",
3166 				ifp->name, bssidx, i));
3167 			break;
3168 		}
3169 	}
3170 	return i;
3171 }
3172 
dhd_rxf_enqueue(dhd_pub_t * dhdp,void * skb)3173 static inline int dhd_rxf_enqueue(dhd_pub_t *dhdp, void* skb)
3174 {
3175 	uint32 store_idx;
3176 	uint32 sent_idx;
3177 
3178 	if (!skb) {
3179 		DHD_ERROR(("dhd_rxf_enqueue: NULL skb!!!\n"));
3180 		return BCME_ERROR;
3181 	}
3182 
3183 	dhd_os_rxflock(dhdp);
3184 	store_idx = dhdp->store_idx;
3185 	sent_idx = dhdp->sent_idx;
3186 	if (dhdp->skbbuf[store_idx] != NULL) {
3187 		/* Make sure the previous packets are processed */
3188 		dhd_os_rxfunlock(dhdp);
3189 #ifdef RXF_DEQUEUE_ON_BUSY
3190 		DHD_TRACE(("dhd_rxf_enqueue: pktbuf not consumed %p, store idx %d sent idx %d\n",
3191 			skb, store_idx, sent_idx));
3192 		return BCME_BUSY;
3193 #else /* RXF_DEQUEUE_ON_BUSY */
3194 		DHD_ERROR(("dhd_rxf_enqueue: pktbuf not consumed %p, store idx %d sent idx %d\n",
3195 			skb, store_idx, sent_idx));
3196 		/* removed msleep here, should use wait_event_timeout if we
3197 		 * want to give rx frame thread a chance to run
3198 		 */
3199 #if defined(WAIT_DEQUEUE)
3200 		OSL_SLEEP(1);
3201 #endif
3202 		return BCME_ERROR;
3203 #endif /* RXF_DEQUEUE_ON_BUSY */
3204 	}
3205 	DHD_TRACE(("dhd_rxf_enqueue: Store SKB %p. idx %d -> %d\n",
3206 		skb, store_idx, (store_idx + 1) & (MAXSKBPEND - 1)));
3207 	dhdp->skbbuf[store_idx] = skb;
3208 	dhdp->store_idx = (store_idx + 1) & (MAXSKBPEND - 1);
3209 	dhd_os_rxfunlock(dhdp);
3210 
3211 	return BCME_OK;
3212 }
3213 
dhd_rxf_dequeue(dhd_pub_t * dhdp)3214 static inline void* dhd_rxf_dequeue(dhd_pub_t *dhdp)
3215 {
3216 	uint32 store_idx;
3217 	uint32 sent_idx;
3218 	void *skb;
3219 
3220 	dhd_os_rxflock(dhdp);
3221 
3222 	store_idx = dhdp->store_idx;
3223 	sent_idx = dhdp->sent_idx;
3224 	skb = dhdp->skbbuf[sent_idx];
3225 
3226 	if (skb == NULL) {
3227 		dhd_os_rxfunlock(dhdp);
3228 		DHD_ERROR(("dhd_rxf_dequeue: Dequeued packet is NULL, store idx %d sent idx %d\n",
3229 			store_idx, sent_idx));
3230 		return NULL;
3231 	}
3232 
3233 	dhdp->skbbuf[sent_idx] = NULL;
3234 	dhdp->sent_idx = (sent_idx + 1) & (MAXSKBPEND - 1);
3235 
3236 	DHD_TRACE(("dhd_rxf_dequeue: netif_rx_ni(%p), sent idx %d\n",
3237 		skb, sent_idx));
3238 
3239 	dhd_os_rxfunlock(dhdp);
3240 
3241 	return skb;
3242 }
3243 
dhd_process_cid_mac(dhd_pub_t * dhdp,bool prepost)3244 int dhd_process_cid_mac(dhd_pub_t *dhdp, bool prepost)
3245 {
3246 	if (prepost) { /* pre process */
3247 		dhd_read_cis(dhdp);
3248 		dhd_check_module_cid(dhdp);
3249 		dhd_check_module_mac(dhdp);
3250 		dhd_set_macaddr_from_file(dhdp);
3251 	} else { /* post process */
3252 		dhd_write_macaddr(&dhdp->mac);
3253 		dhd_clear_cis(dhdp);
3254 	}
3255 
3256 	return 0;
3257 }
3258 
3259 // terence 20160615: fix building error if ARP_OFFLOAD_SUPPORT removed
3260 #if defined(PKT_FILTER_SUPPORT)
3261 #if defined(ARP_OFFLOAD_SUPPORT) && !defined(GAN_LITE_NAT_KEEPALIVE_FILTER)
3262 static bool
_turn_on_arp_filter(dhd_pub_t * dhd,int op_mode_param)3263 _turn_on_arp_filter(dhd_pub_t *dhd, int op_mode_param)
3264 {
3265 	bool _apply = FALSE;
3266 	/* In case of IBSS mode, apply arp pkt filter */
3267 	if (op_mode_param & DHD_FLAG_IBSS_MODE) {
3268 		_apply = TRUE;
3269 		goto exit;
3270 	}
3271 	/* In case of P2P GO or GC, apply pkt filter to pass arp pkt to host */
3272 	if (op_mode_param & (DHD_FLAG_P2P_GC_MODE | DHD_FLAG_P2P_GO_MODE)) {
3273 		_apply = TRUE;
3274 		goto exit;
3275 	}
3276 
3277 exit:
3278 	return _apply;
3279 }
3280 #endif /* !GAN_LITE_NAT_KEEPALIVE_FILTER */
3281 
3282 void
dhd_set_packet_filter(dhd_pub_t * dhd)3283 dhd_set_packet_filter(dhd_pub_t *dhd)
3284 {
3285 	int i;
3286 
3287 	DHD_TRACE(("%s: enter\n", __FUNCTION__));
3288 	if (dhd_pkt_filter_enable) {
3289 		for (i = 0; i < dhd->pktfilter_count; i++) {
3290 			dhd_pktfilter_offload_set(dhd, dhd->pktfilter[i]);
3291 		}
3292 	}
3293 }
3294 
3295 void
dhd_enable_packet_filter(int value,dhd_pub_t * dhd)3296 dhd_enable_packet_filter(int value, dhd_pub_t *dhd)
3297 {
3298 	int i;
3299 
3300 	DHD_PRINT("%s: enter, value = %d\n", __FUNCTION__, value);
3301 	if ((dhd->op_mode & DHD_FLAG_HOSTAP_MODE) && value &&
3302 			!dhd_conf_get_insuspend(dhd, AP_FILTER_IN_SUSPEND)) {
3303 		DHD_ERROR(("%s: DHD_FLAG_HOSTAP_MODE\n", __FUNCTION__));
3304 		return;
3305 	}
3306 	/* 1 - Enable packet filter, only allow unicast packet to send up */
3307 	/* 0 - Disable packet filter */
3308 	if (dhd_pkt_filter_enable && (!value ||
3309 	    (dhd_support_sta_mode(dhd) && !dhd->dhcp_in_progress) ||
3310 	    dhd_conf_get_insuspend(dhd, AP_FILTER_IN_SUSPEND)))
3311 	{
3312 		for (i = 0; i < dhd->pktfilter_count; i++) {
3313 // terence 20160615: fix building error if ARP_OFFLOAD_SUPPORT removed
3314 #if defined(ARP_OFFLOAD_SUPPORT) && !defined(GAN_LITE_NAT_KEEPALIVE_FILTER)
3315 			if (value && (i == DHD_ARP_FILTER_NUM) &&
3316 				!_turn_on_arp_filter(dhd, dhd->op_mode)) {
3317 				DHD_TRACE(("Do not turn on ARP white list pkt filter:"
3318 					"val %d, cnt %d, op_mode 0x%x\n",
3319 					value, i, dhd->op_mode));
3320 				continue;
3321 			}
3322 #endif /* !GAN_LITE_NAT_KEEPALIVE_FILTER */
3323 			dhd_pktfilter_offload_enable(dhd, dhd->pktfilter[i],
3324 				value, dhd_master_mode);
3325 		}
3326 	}
3327 }
3328 
3329 int
dhd_packet_filter_add_remove(dhd_pub_t * dhdp,int add_remove,int num)3330 dhd_packet_filter_add_remove(dhd_pub_t *dhdp, int add_remove, int num)
3331 {
3332 	char *filterp = NULL;
3333 	int filter_id = 0;
3334 
3335 	switch (num) {
3336 		case DHD_BROADCAST_FILTER_NUM:
3337 			filterp = "101 0 0 0 0xFFFFFFFFFFFF 0xFFFFFFFFFFFF";
3338 			filter_id = 101;
3339 			break;
3340 		case DHD_MULTICAST4_FILTER_NUM:
3341 			filter_id = 102;
3342 			if (FW_SUPPORTED((dhdp), pf6)) {
3343 				if (dhdp->pktfilter[num] != NULL) {
3344 					dhd_pktfilter_offload_delete(dhdp, filter_id);
3345 					dhdp->pktfilter[num] = NULL;
3346 				}
3347 				if (!add_remove) {
3348 					filterp = DISCARD_IPV4_MCAST;
3349 					add_remove = 1;
3350 					break;
3351 				}
3352 			}
3353 			filterp = "102 0 0 0 0xFFFFFF 0x01005E";
3354 			break;
3355 		case DHD_MULTICAST6_FILTER_NUM:
3356 			filter_id = 103;
3357 			if (FW_SUPPORTED((dhdp), pf6)) {
3358 				if (dhdp->pktfilter[num] != NULL) {
3359 					dhd_pktfilter_offload_delete(dhdp, filter_id);
3360 					dhdp->pktfilter[num] = NULL;
3361 				}
3362 				if (!add_remove) {
3363 					filterp = DISCARD_IPV6_MCAST;
3364 					add_remove = 1;
3365 					break;
3366 				}
3367 			}
3368 			filterp = "103 0 0 0 0xFFFF 0x3333";
3369 			break;
3370 		case DHD_MDNS_FILTER_NUM:
3371 			filterp = "104 0 0 0 0xFFFFFFFFFFFF 0x01005E0000FB";
3372 			filter_id = 104;
3373 			break;
3374 		case DHD_ARP_FILTER_NUM:
3375 			filterp = "105 0 0 12 0xFFFF 0x0806";
3376 			filter_id = 105;
3377 			break;
3378 		case DHD_BROADCAST_ARP_FILTER_NUM:
3379 			filterp = "106 0 0 0 0xFFFFFFFFFFFF0000000000000806"
3380 				" 0xFFFFFFFFFFFF0000000000000806";
3381 			filter_id = 106;
3382 			break;
3383 		default:
3384 			return -EINVAL;
3385 	}
3386 
3387 	/* Add filter */
3388 	if (add_remove) {
3389 		dhdp->pktfilter[num] = filterp;
3390 		dhd_pktfilter_offload_set(dhdp, dhdp->pktfilter[num]);
3391 	} else { /* Delete filter */
3392 		if (dhdp->pktfilter[num]) {
3393 			dhd_pktfilter_offload_delete(dhdp, filter_id);
3394 			dhdp->pktfilter[num] = NULL;
3395 		}
3396 	}
3397 
3398 	return 0;
3399 }
3400 #endif /* PKT_FILTER_SUPPORT */
3401 
dhd_set_suspend(int value,dhd_pub_t * dhd)3402 static int dhd_set_suspend(int value, dhd_pub_t *dhd)
3403 {
3404 #ifndef SUPPORT_PM2_ONLY
3405 	int power_mode = PM_MAX;
3406 #endif /* SUPPORT_PM2_ONLY */
3407 #ifdef SUPPORT_SENSORHUB
3408 	shub_control_t shub_ctl;
3409 #endif /* SUPPORT_SENSORHUB */
3410 	/* wl_pkt_filter_enable_t	enable_parm; */
3411 	int bcn_li_dtim = 0; /* Default bcn_li_dtim in resume mode is 0 */
3412 	int ret = 0;
3413 #ifdef DHD_USE_EARLYSUSPEND
3414 #ifdef CUSTOM_BCN_TIMEOUT_IN_SUSPEND
3415 	int bcn_timeout = 0;
3416 #endif /* CUSTOM_BCN_TIMEOUT_IN_SUSPEND */
3417 #ifdef CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND
3418 	int roam_time_thresh = 0;   /* (ms) */
3419 #endif /* CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND */
3420 #ifndef ENABLE_FW_ROAM_SUSPEND
3421 	uint roamvar = 1;
3422 #endif /* ENABLE_FW_ROAM_SUSPEND */
3423 #ifdef ENABLE_BCN_LI_BCN_WAKEUP
3424 	int bcn_li_bcn;
3425 #endif /* ENABLE_BCN_LI_BCN_WAKEUP */
3426 	uint nd_ra_filter = 0;
3427 #endif /* DHD_USE_EARLYSUSPEND */
3428 #ifdef PASS_ALL_MCAST_PKTS
3429 	struct dhd_info *dhdinfo;
3430 	uint32 allmulti;
3431 	uint i;
3432 #endif /* PASS_ALL_MCAST_PKTS */
3433 #ifdef ENABLE_IPMCAST_FILTER
3434 	int ipmcast_l2filter;
3435 #endif /* ENABLE_IPMCAST_FILTER */
3436 #ifdef DYNAMIC_SWOOB_DURATION
3437 #ifndef CUSTOM_INTR_WIDTH
3438 #define CUSTOM_INTR_WIDTH 100
3439 	int intr_width = 0;
3440 #endif /* CUSTOM_INTR_WIDTH */
3441 #endif /* DYNAMIC_SWOOB_DURATION */
3442 
3443 #if defined(BCMPCIE)
3444 	int lpas = 0;
3445 	int dtim_period = 0;
3446 	int bcn_interval = 0;
3447 	int bcn_to_dly = 0;
3448 #ifndef CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND
3449 	int bcn_timeout = CUSTOM_BCN_TIMEOUT_SETTING;
3450 #else
3451 	bcn_timeout = CUSTOM_BCN_TIMEOUT_SETTING;
3452 #endif /* CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND */
3453 #endif /* OEM_ANDROID && BCMPCIE */
3454 
3455 	if (!dhd)
3456 		return -ENODEV;
3457 
3458 #ifdef PASS_ALL_MCAST_PKTS
3459 	dhdinfo = dhd->info;
3460 #endif /* PASS_ALL_MCAST_PKTS */
3461 
3462 	DHD_TRACE(("%s: enter, value = %d in_suspend=%d\n",
3463 		__FUNCTION__, value, dhd->in_suspend));
3464 
3465 	dhd_suspend_lock(dhd);
3466 
3467 #ifdef CUSTOM_SET_CPUCORE
3468 	DHD_TRACE(("%s set cpucore(suspend%d)\n", __FUNCTION__, value));
3469 	/* set specific cpucore */
3470 	dhd_set_cpucore(dhd, TRUE);
3471 #endif /* CUSTOM_SET_CPUCORE */
3472 	if (dhd->up) {
3473 		if (value && dhd->in_suspend) {
3474 #ifdef PKT_FILTER_SUPPORT
3475 			dhd->early_suspended = 1;
3476 #endif
3477 			/* Kernel suspended */
3478 			DHD_ERROR(("%s: force extra suspend setting\n", __FUNCTION__));
3479 
3480 #ifndef SUPPORT_PM2_ONLY
3481 			dhd_wl_ioctl_cmd(dhd, WLC_SET_PM, (char *)&power_mode,
3482 				sizeof(power_mode), TRUE, 0);
3483 #endif /* SUPPORT_PM2_ONLY */
3484 
3485 #ifdef PKT_FILTER_SUPPORT
3486 			/* Enable packet filter,
3487 			 * only allow unicast packet to send up
3488 			 */
3489 			dhd_enable_packet_filter(1, dhd);
3490 #ifdef APF
3491 			dhd_dev_apf_enable_filter(dhd_linux_get_primary_netdev(dhd));
3492 #endif /* APF */
3493 #endif /* PKT_FILTER_SUPPORT */
3494 
3495 #ifdef SUPPORT_SENSORHUB
3496 			shub_ctl.enable = 1;
3497 			shub_ctl.cmd = 0x000;
3498 			shub_ctl.op_mode = 1;
3499 			shub_ctl.interval = 0;
3500 			if (dhd->info->shub_enable == 1) {
3501 				ret = dhd_iovar(dhd, 0, "shub_msreq",
3502 					(char *)&shub_ctl, sizeof(shub_ctl), NULL, 0, TRUE);
3503 				if (ret < 0) {
3504 					DHD_ERROR(("%s SensorHub MS start: failed %d\n",
3505 						__FUNCTION__, ret));
3506 				}
3507 			}
3508 #endif /* SUPPORT_SENSORHUB */
3509 
3510 
3511 #ifdef PASS_ALL_MCAST_PKTS
3512 			allmulti = 0;
3513 			for (i = 0; i < DHD_MAX_IFS; i++) {
3514 				if (dhdinfo->iflist[i] && dhdinfo->iflist[i]->net)
3515 					dhd_iovar(dhd, i, "allmulti", (char *)&allmulti,
3516 							sizeof(allmulti), NULL, 0, TRUE);
3517 
3518 			}
3519 #endif /* PASS_ALL_MCAST_PKTS */
3520 
3521 			/* If DTIM skip is set up as default, force it to wake
3522 			 * each third DTIM for better power savings.  Note that
3523 			 * one side effect is a chance to miss BC/MC packet.
3524 			 */
3525 #ifdef WLTDLS
3526 			/* Do not set bcn_li_ditm on WFD mode */
3527 			if (dhd->tdls_mode) {
3528 				bcn_li_dtim = 0;
3529 			} else
3530 #endif /* WLTDLS */
3531 #if defined(BCMPCIE)
3532 			bcn_li_dtim = dhd_get_suspend_bcn_li_dtim(dhd, &dtim_period,
3533 				&bcn_interval);
3534 			dhd_iovar(dhd, 0, "bcn_li_dtim", (char *)&bcn_li_dtim,
3535 					sizeof(bcn_li_dtim), NULL, 0, TRUE);
3536 
3537 			if ((bcn_li_dtim * dtim_period * bcn_interval) >=
3538 				MIN_DTIM_FOR_ROAM_THRES_EXTEND) {
3539 				/*
3540 				 * Increase max roaming threshold from 2 secs to 8 secs
3541 				 * the real roam threshold is MIN(max_roam_threshold,
3542 				 * bcn_timeout/2)
3543 				 */
3544 				lpas = 1;
3545 				dhd_iovar(dhd, 0, "lpas", (char *)&lpas, sizeof(lpas), NULL,
3546 						0, TRUE);
3547 
3548 				bcn_to_dly = 1;
3549 				/*
3550 				 * if bcn_to_dly is 1, the real roam threshold is
3551 				 * MIN(max_roam_threshold, bcn_timeout -1);
3552 				 * notify link down event after roaming procedure complete
3553 				 * if we hit bcn_timeout while we are in roaming progress.
3554 				 */
3555 				dhd_iovar(dhd, 0, "bcn_to_dly", (char *)&bcn_to_dly,
3556 						sizeof(bcn_to_dly), NULL, 0, TRUE);
3557 				/* Increase beacon timeout to 6 secs or use bigger one */
3558 				bcn_timeout = max(bcn_timeout, BCN_TIMEOUT_IN_SUSPEND);
3559 				dhd_iovar(dhd, 0, "bcn_timeout", (char *)&bcn_timeout,
3560 						sizeof(bcn_timeout), NULL, 0, TRUE);
3561 			}
3562 #else
3563 			bcn_li_dtim = dhd_get_suspend_bcn_li_dtim(dhd);
3564 			if (dhd_iovar(dhd, 0, "bcn_li_dtim", (char *)&bcn_li_dtim,
3565 					sizeof(bcn_li_dtim), NULL, 0, TRUE) < 0)
3566 				DHD_ERROR(("%s: set dtim failed\n", __FUNCTION__));
3567 #endif /* OEM_ANDROID && BCMPCIE */
3568 
3569 #ifdef DHD_USE_EARLYSUSPEND
3570 #ifdef CUSTOM_BCN_TIMEOUT_IN_SUSPEND
3571 			bcn_timeout = CUSTOM_BCN_TIMEOUT_IN_SUSPEND;
3572 			dhd_iovar(dhd, 0, "bcn_timeout", (char *)&bcn_timeout,
3573 					sizeof(bcn_timeout), NULL, 0, TRUE);
3574 #endif /* CUSTOM_BCN_TIMEOUT_IN_SUSPEND */
3575 #ifdef CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND
3576 			roam_time_thresh = CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND;
3577 			dhd_iovar(dhd, 0, "roam_time_thresh", (char *)&roam_time_thresh,
3578 					sizeof(roam_time_thresh), NULL, 0, TRUE);
3579 #endif /* CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND */
3580 #ifndef ENABLE_FW_ROAM_SUSPEND
3581 			/* Disable firmware roaming during suspend */
3582 			dhd_iovar(dhd, 0, "roam_off", (char *)&roamvar, sizeof(roamvar),
3583 					NULL, 0, TRUE);
3584 #endif /* ENABLE_FW_ROAM_SUSPEND */
3585 #ifdef ENABLE_BCN_LI_BCN_WAKEUP
3586 			bcn_li_bcn = 0;
3587 			dhd_iovar(dhd, 0, "bcn_li_bcn", (char *)&bcn_li_bcn,
3588 					sizeof(bcn_li_bcn), NULL, 0, TRUE);
3589 #endif /* ENABLE_BCN_LI_BCN_WAKEUP */
3590 #ifdef NDO_CONFIG_SUPPORT
3591 			if (dhd->ndo_enable) {
3592 				if (!dhd->ndo_host_ip_overflow) {
3593 					/* enable ND offload on suspend */
3594 					ret = dhd_ndo_enable(dhd, 1);
3595 					if (ret < 0) {
3596 						DHD_ERROR(("%s: failed to enable NDO\n",
3597 							__FUNCTION__));
3598 					}
3599 				} else {
3600 					DHD_INFO(("%s: NDO disabled on suspend due to"
3601 							"HW capacity\n", __FUNCTION__));
3602 				}
3603 			}
3604 #endif /* NDO_CONFIG_SUPPORT */
3605 #ifndef APF
3606 			if (FW_SUPPORTED(dhd, ndoe))
3607 #else
3608 			if (FW_SUPPORTED(dhd, ndoe) && !FW_SUPPORTED(dhd, apf))
3609 #endif /* APF */
3610 			{
3611 				/* enable IPv6 RA filter in  firmware during suspend */
3612 				nd_ra_filter = 1;
3613 				ret = dhd_iovar(dhd, 0, "nd_ra_filter_enable",
3614 						(char *)&nd_ra_filter, sizeof(nd_ra_filter),
3615 						NULL, 0, TRUE);
3616 				if (ret < 0)
3617 					DHD_ERROR(("failed to set nd_ra_filter (%d)\n",
3618 						ret));
3619 			}
3620 			dhd_os_suppress_logging(dhd, TRUE);
3621 #ifdef ENABLE_IPMCAST_FILTER
3622 			ipmcast_l2filter = 1;
3623 			ret = dhd_iovar(dhd, 0, "ipmcast_l2filter",
3624 					(char *)&ipmcast_l2filter, sizeof(ipmcast_l2filter),
3625 					NULL, 0, TRUE);
3626 #endif /* ENABLE_IPMCAST_FILTER */
3627 #ifdef DYNAMIC_SWOOB_DURATION
3628 			intr_width = CUSTOM_INTR_WIDTH;
3629 			ret = dhd_iovar(dhd, 0, "bus:intr_width", (char *)&intr_width,
3630 					sizeof(intr_width), NULL, 0, TRUE);
3631 			if (ret < 0) {
3632 				DHD_ERROR(("failed to set intr_width (%d)\n", ret));
3633 			}
3634 #endif /* DYNAMIC_SWOOB_DURATION */
3635 #endif /* DHD_USE_EARLYSUSPEND */
3636 		} else {
3637 #ifdef PKT_FILTER_SUPPORT
3638 			dhd->early_suspended = 0;
3639 #endif
3640 			/* Kernel resumed  */
3641 			DHD_PRINT("%s: Remove extra suspend setting \n", __FUNCTION__);
3642 
3643 #ifdef SUPPORT_SENSORHUB
3644 			shub_ctl.enable = 1;
3645 			shub_ctl.cmd = 0x000;
3646 			shub_ctl.op_mode = 0;
3647 			shub_ctl.interval = 0;
3648 			if (dhd->info->shub_enable == 1) {
3649 				ret = dhd_iovar(dhd, 0, "shub_msreq",
3650 						(char *)&shub_ctl, sizeof(shub_ctl),
3651 						NULL, 0, TRUE);
3652 				if (ret < 0) {
3653 					DHD_ERROR(("%s SensorHub MS stop: failed %d\n",
3654 						__FUNCTION__, ret));
3655 				}
3656 			}
3657 #endif /* SUPPORT_SENSORHUB */
3658 
3659 #ifdef DYNAMIC_SWOOB_DURATION
3660 			intr_width = 0;
3661 			ret = dhd_iovar(dhd, 0, "bus:intr_width", (char *)&intr_width,
3662 					sizeof(intr_width), NULL, 0, TRUE);
3663 			if (ret < 0) {
3664 				DHD_ERROR(("failed to set intr_width (%d)\n", ret));
3665 			}
3666 #endif /* DYNAMIC_SWOOB_DURATION */
3667 #ifndef SUPPORT_PM2_ONLY
3668 			power_mode = PM_FAST;
3669 			dhd_wl_ioctl_cmd(dhd, WLC_SET_PM, (char *)&power_mode,
3670 				sizeof(power_mode), TRUE, 0);
3671 #endif /* SUPPORT_PM2_ONLY */
3672 #ifdef PKT_FILTER_SUPPORT
3673 			/* disable pkt filter */
3674 			dhd_enable_packet_filter(0, dhd);
3675 #ifdef APF
3676 			dhd_dev_apf_disable_filter(dhd_linux_get_primary_netdev(dhd));
3677 #endif /* APF */
3678 #endif /* PKT_FILTER_SUPPORT */
3679 #ifdef PASS_ALL_MCAST_PKTS
3680 			allmulti = 1;
3681 			for (i = 0; i < DHD_MAX_IFS; i++) {
3682 				if (dhdinfo->iflist[i] && dhdinfo->iflist[i]->net)
3683 					dhd_iovar(dhd, i, "allmulti", (char *)&allmulti,
3684 							sizeof(allmulti), NULL, 0, TRUE);
3685 			}
3686 #endif /* PASS_ALL_MCAST_PKTS */
3687 #if defined(BCMPCIE)
3688 			/* restore pre-suspend setting */
3689 			ret = dhd_iovar(dhd, 0, "bcn_li_dtim", (char *)&bcn_li_dtim,
3690 					sizeof(bcn_li_dtim), NULL, 0, TRUE);
3691 			if (ret < 0) {
3692 				DHD_ERROR(("%s:bcn_li_ditm fail:%d\n", __FUNCTION__, ret));
3693 			}
3694 
3695 			dhd_iovar(dhd, 0, "lpas", (char *)&lpas, sizeof(lpas), NULL, 0,
3696 					TRUE);
3697 
3698 			dhd_iovar(dhd, 0, "bcn_to_dly", (char *)&bcn_to_dly,
3699 					sizeof(bcn_to_dly), NULL, 0, TRUE);
3700 
3701 			dhd_iovar(dhd, 0, "bcn_timeout", (char *)&bcn_timeout,
3702 					sizeof(bcn_timeout), NULL, 0, TRUE);
3703 #else
3704 			/* restore pre-suspend setting for dtim_skip */
3705 			ret = dhd_iovar(dhd, 0, "bcn_li_dtim", (char *)&bcn_li_dtim,
3706 					sizeof(bcn_li_dtim), NULL, 0, TRUE);
3707 			if (ret < 0) {
3708 				DHD_ERROR(("%s:bcn_li_ditm fail:%d\n", __FUNCTION__, ret));
3709 			}
3710 #endif /* OEM_ANDROID && BCMPCIE */
3711 #ifdef DHD_USE_EARLYSUSPEND
3712 #ifdef CUSTOM_BCN_TIMEOUT_IN_SUSPEND
3713 			bcn_timeout = CUSTOM_BCN_TIMEOUT;
3714 			dhd_iovar(dhd, 0, "bcn_timeout", (char *)&bcn_timeout,
3715 					sizeof(bcn_timeout), NULL, 0, TRUE);
3716 #endif /* CUSTOM_BCN_TIMEOUT_IN_SUSPEND */
3717 #ifdef CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND
3718 			roam_time_thresh = 2000;
3719 			dhd_iovar(dhd, 0, "roam_time_thresh", (char *)&roam_time_thresh,
3720 					sizeof(roam_time_thresh), NULL, 0, TRUE);
3721 
3722 #endif /* CUSTOM_ROAM_TIME_THRESH_IN_SUSPEND */
3723 #ifndef ENABLE_FW_ROAM_SUSPEND
3724 			roamvar = dhd_roam_disable;
3725 			dhd_iovar(dhd, 0, "roam_off", (char *)&roamvar, sizeof(roamvar),
3726 					NULL, 0, TRUE);
3727 #endif /* ENABLE_FW_ROAM_SUSPEND */
3728 #ifdef ENABLE_BCN_LI_BCN_WAKEUP
3729 			bcn_li_bcn = 1;
3730 			dhd_iovar(dhd, 0, "bcn_li_bcn", (char *)&bcn_li_bcn,
3731 					sizeof(bcn_li_bcn), NULL, 0, TRUE);
3732 #endif /* ENABLE_BCN_LI_BCN_WAKEUP */
3733 #ifdef NDO_CONFIG_SUPPORT
3734 			if (dhd->ndo_enable) {
3735 				/* Disable ND offload on resume */
3736 				ret = dhd_ndo_enable(dhd, 0);
3737 				if (ret < 0) {
3738 					DHD_ERROR(("%s: failed to disable NDO\n",
3739 						__FUNCTION__));
3740 				}
3741 			}
3742 #endif /* NDO_CONFIG_SUPPORT */
3743 #ifndef APF
3744 			if (FW_SUPPORTED(dhd, ndoe))
3745 #else
3746 			if (FW_SUPPORTED(dhd, ndoe) && !FW_SUPPORTED(dhd, apf))
3747 #endif /* APF */
3748 			{
3749 				/* disable IPv6 RA filter in  firmware during suspend */
3750 				nd_ra_filter = 0;
3751 				ret = dhd_iovar(dhd, 0, "nd_ra_filter_enable",
3752 						(char *)&nd_ra_filter, sizeof(nd_ra_filter),
3753 						NULL, 0, TRUE);
3754 				if (ret < 0) {
3755 					DHD_ERROR(("failed to set nd_ra_filter (%d)\n",
3756 						ret));
3757 				}
3758 			}
3759 			dhd_os_suppress_logging(dhd, FALSE);
3760 #ifdef ENABLE_IPMCAST_FILTER
3761 			ipmcast_l2filter = 0;
3762 			ret = dhd_iovar(dhd, 0, "ipmcast_l2filter",
3763 					(char *)&ipmcast_l2filter, sizeof(ipmcast_l2filter),
3764 					NULL, 0, TRUE);
3765 #endif /* ENABLE_IPMCAST_FILTER */
3766 #endif /* DHD_USE_EARLYSUSPEND */
3767 		}
3768 	}
3769 	dhd_suspend_unlock(dhd);
3770 
3771 	return 0;
3772 }
3773 
dhd_suspend_resume_helper(struct dhd_info * dhd,int val,int force)3774 static int dhd_suspend_resume_helper(struct dhd_info *dhd, int val, int force)
3775 {
3776 	dhd_pub_t *dhdp = &dhd->pub;
3777 	int ret = 0;
3778 
3779 	DHD_OS_WAKE_LOCK(dhdp);
3780 	DHD_PERIM_LOCK(dhdp);
3781 
3782 	/* Set flag when early suspend was called */
3783 	dhdp->in_suspend = val;
3784 	if ((force || !dhdp->suspend_disable_flag) &&
3785 		(dhd_support_sta_mode(dhdp) || dhd_conf_get_insuspend(dhdp, ALL_IN_SUSPEND)))
3786 	{
3787 		ret = dhd_set_suspend(val, dhdp);
3788 	}
3789 
3790 	DHD_PERIM_UNLOCK(dhdp);
3791 	DHD_OS_WAKE_UNLOCK(dhdp);
3792 	return ret;
3793 }
3794 
3795 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
dhd_early_suspend(struct early_suspend * h)3796 static void dhd_early_suspend(struct early_suspend *h)
3797 {
3798 	struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
3799 	DHD_TRACE_HW4(("%s: enter\n", __FUNCTION__));
3800 
3801 	if (dhd && dhd->pub.conf->suspend_mode == EARLY_SUSPEND) {
3802 		dhd_suspend_resume_helper(dhd, 1, 0);
3803 		dhd_conf_set_suspend_resume(&dhd->pub, 1);
3804 	}
3805 }
3806 
dhd_late_resume(struct early_suspend * h)3807 static void dhd_late_resume(struct early_suspend *h)
3808 {
3809 	struct dhd_info *dhd = container_of(h, struct dhd_info, early_suspend);
3810 	DHD_TRACE_HW4(("%s: enter\n", __FUNCTION__));
3811 
3812 	if (dhd && dhd->pub.conf->suspend_mode == EARLY_SUSPEND) {
3813 		dhd_conf_set_suspend_resume(&dhd->pub, 0);
3814 		dhd_suspend_resume_helper(dhd, 0, 0);
3815 	}
3816 }
3817 #endif /* CONFIG_HAS_EARLYSUSPEND && DHD_USE_EARLYSUSPEND */
3818 
3819 /*
3820  * Generalized timeout mechanism.  Uses spin sleep with exponential back-off until
3821  * the sleep time reaches one jiffy, then switches over to task delay.  Usage:
3822  *
3823  *      dhd_timeout_start(&tmo, usec);
3824  *      while (!dhd_timeout_expired(&tmo))
3825  *              if (poll_something())
3826  *                      break;
3827  *      if (dhd_timeout_expired(&tmo))
3828  *              fatal();
3829  */
3830 
3831 void
dhd_timeout_start(dhd_timeout_t * tmo,uint usec)3832 dhd_timeout_start(dhd_timeout_t *tmo, uint usec)
3833 {
3834 	tmo->limit = usec;
3835 	tmo->increment = 0;
3836 	tmo->elapsed = 0;
3837 	tmo->tick = jiffies_to_usecs(1);
3838 }
3839 
3840 int
dhd_timeout_expired(dhd_timeout_t * tmo)3841 dhd_timeout_expired(dhd_timeout_t *tmo)
3842 {
3843 	/* Does nothing the first call */
3844 	if (tmo->increment == 0) {
3845 		tmo->increment = 1;
3846 		return 0;
3847 	}
3848 
3849 	if (tmo->elapsed >= tmo->limit)
3850 		return 1;
3851 
3852 	/* Add the delay that's about to take place */
3853 	tmo->elapsed += tmo->increment;
3854 
3855 	if ((!CAN_SLEEP()) || tmo->increment < tmo->tick) {
3856 		OSL_DELAY(tmo->increment);
3857 		tmo->increment *= 2;
3858 		if (tmo->increment > tmo->tick)
3859 			tmo->increment = tmo->tick;
3860 	} else {
3861 		wait_queue_head_t delay_wait;
3862 		DECLARE_WAITQUEUE(wait, current);
3863 		init_waitqueue_head(&delay_wait);
3864 		add_wait_queue(&delay_wait, &wait);
3865 		set_current_state(TASK_INTERRUPTIBLE);
3866 		(void)schedule_timeout(1);
3867 		remove_wait_queue(&delay_wait, &wait);
3868 		set_current_state(TASK_RUNNING);
3869 	}
3870 
3871 	return 0;
3872 }
3873 
3874 int
dhd_net2idx(dhd_info_t * dhd,struct net_device * net)3875 dhd_net2idx(dhd_info_t *dhd, struct net_device *net)
3876 {
3877 	int i = 0;
3878 
3879 	if (!dhd) {
3880 		DHD_ERROR(("%s : DHD_BAD_IF return\n", __FUNCTION__));
3881 		return DHD_BAD_IF;
3882 	}
3883 
3884 	while (i < DHD_MAX_IFS) {
3885 		if (dhd->iflist[i] && dhd->iflist[i]->net && (dhd->iflist[i]->net == net))
3886 			return i;
3887 		i++;
3888 	}
3889 
3890 	return DHD_BAD_IF;
3891 }
3892 
dhd_idx2net(void * pub,int ifidx)3893 struct net_device * dhd_idx2net(void *pub, int ifidx)
3894 {
3895 	struct dhd_pub *dhd_pub = (struct dhd_pub *)pub;
3896 	struct dhd_info *dhd_info;
3897 
3898 	if (!dhd_pub || ifidx < 0 || ifidx >= DHD_MAX_IFS)
3899 		return NULL;
3900 	dhd_info = dhd_pub->info;
3901 	if (dhd_info && dhd_info->iflist[ifidx])
3902 		return dhd_info->iflist[ifidx]->net;
3903 	return NULL;
3904 }
3905 
3906 int
dhd_ifname2idx(dhd_info_t * dhd,char * name)3907 dhd_ifname2idx(dhd_info_t *dhd, char *name)
3908 {
3909 	int i = DHD_MAX_IFS;
3910 
3911 	ASSERT(dhd);
3912 
3913 	if (name == NULL || *name == '\0')
3914 		return 0;
3915 
3916 	while (--i > 0)
3917 		if (dhd->iflist[i] && !strncmp(dhd->iflist[i]->dngl_name, name, IFNAMSIZ))
3918 				break;
3919 
3920 	DHD_TRACE(("%s: return idx %d for \"%s\"\n", __FUNCTION__, i, name));
3921 
3922 	return i;	/* default - the primary interface */
3923 }
3924 
3925 char *
dhd_ifname(dhd_pub_t * dhdp,int ifidx)3926 dhd_ifname(dhd_pub_t *dhdp, int ifidx)
3927 {
3928 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
3929 
3930 	ASSERT(dhd);
3931 
3932 	if (ifidx < 0 || ifidx >= DHD_MAX_IFS) {
3933 		DHD_ERROR(("%s: ifidx %d out of range\n", __FUNCTION__, ifidx));
3934 		return "<if_bad>";
3935 	}
3936 
3937 	if (dhd->iflist[ifidx] == NULL) {
3938 		DHD_ERROR(("%s: null i/f %d\n", __FUNCTION__, ifidx));
3939 		return "<if_null>";
3940 	}
3941 
3942 	if (dhd->iflist[ifidx]->net)
3943 		return dhd->iflist[ifidx]->net->name;
3944 
3945 	return "<if_none>";
3946 }
3947 
3948 uint8 *
dhd_bssidx2bssid(dhd_pub_t * dhdp,int idx)3949 dhd_bssidx2bssid(dhd_pub_t *dhdp, int idx)
3950 {
3951 	int i;
3952 	dhd_info_t *dhd = (dhd_info_t *)dhdp;
3953 
3954 	ASSERT(dhd);
3955 	for (i = 0; i < DHD_MAX_IFS; i++)
3956 	if (dhd->iflist[i] && dhd->iflist[i]->bssidx == idx)
3957 		return dhd->iflist[i]->mac_addr;
3958 
3959 	return NULL;
3960 }
3961 
3962 
3963 static void
_dhd_set_multicast_list(dhd_info_t * dhd,int ifidx)3964 _dhd_set_multicast_list(dhd_info_t *dhd, int ifidx)
3965 {
3966 	struct net_device *dev;
3967 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
3968 	struct netdev_hw_addr *ha;
3969 #else
3970 	struct dev_mc_list *mclist;
3971 #endif
3972 	uint32 allmulti, cnt;
3973 
3974 	wl_ioctl_t ioc;
3975 	char *buf, *bufp;
3976 	uint buflen;
3977 	int ret;
3978 
3979 	if (!dhd->iflist[ifidx]) {
3980 		DHD_ERROR(("%s : dhd->iflist[%d] was NULL\n", __FUNCTION__, ifidx));
3981 		return;
3982 	}
3983 	dev = dhd->iflist[ifidx]->net;
3984 	if (!dev)
3985 		return;
3986 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
3987 	netif_addr_lock_bh(dev);
3988 #endif /* LINUX >= 2.6.27 */
3989 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
3990 	cnt = netdev_mc_count(dev);
3991 #else
3992 	cnt = dev->mc_count;
3993 #endif /* LINUX >= 2.6.35 */
3994 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
3995 	netif_addr_unlock_bh(dev);
3996 #endif /* LINUX >= 2.6.27 */
3997 
3998 	/* Determine initial value of allmulti flag */
3999 	allmulti = (dev->flags & IFF_ALLMULTI) ? TRUE : FALSE;
4000 
4001 #ifdef PASS_ALL_MCAST_PKTS
4002 #ifdef PKT_FILTER_SUPPORT
4003 	if (!dhd->pub.early_suspended)
4004 #endif /* PKT_FILTER_SUPPORT */
4005 		allmulti = TRUE;
4006 #endif /* PASS_ALL_MCAST_PKTS */
4007 
4008 	/* Send down the multicast list first. */
4009 
4010 
4011 	buflen = sizeof("mcast_list") + sizeof(cnt) + (cnt * ETHER_ADDR_LEN);
4012 	if (!(bufp = buf = MALLOC(dhd->pub.osh, buflen))) {
4013 		DHD_ERROR(("%s: out of memory for mcast_list, cnt %d\n",
4014 		           dhd_ifname(&dhd->pub, ifidx), cnt));
4015 		return;
4016 	}
4017 
4018 	strncpy(bufp, "mcast_list", buflen - 1);
4019 	bufp[buflen - 1] = '\0';
4020 	bufp += strlen("mcast_list") + 1;
4021 
4022 	cnt = htol32(cnt);
4023 	memcpy(bufp, &cnt, sizeof(cnt));
4024 	bufp += sizeof(cnt);
4025 
4026 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
4027 	netif_addr_lock_bh(dev);
4028 #endif /* LINUX >= 2.6.27 */
4029 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
4030 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
4031 #pragma GCC diagnostic push
4032 #pragma GCC diagnostic ignored "-Wcast-qual"
4033 #endif
4034 	netdev_for_each_mc_addr(ha, dev) {
4035 		if (!cnt)
4036 			break;
4037 		memcpy(bufp, ha->addr, ETHER_ADDR_LEN);
4038 		bufp += ETHER_ADDR_LEN;
4039 		cnt--;
4040 	}
4041 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
4042 #pragma GCC diagnostic pop
4043 #endif
4044 #else /* LINUX < 2.6.35 */
4045 	for (mclist = dev->mc_list; (mclist && (cnt > 0));
4046 		cnt--, mclist = mclist->next) {
4047 		memcpy(bufp, (void *)mclist->dmi_addr, ETHER_ADDR_LEN);
4048 		bufp += ETHER_ADDR_LEN;
4049 	}
4050 #endif /* LINUX >= 2.6.35 */
4051 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
4052 	netif_addr_unlock_bh(dev);
4053 #endif /* LINUX >= 2.6.27 */
4054 
4055 	memset(&ioc, 0, sizeof(ioc));
4056 	ioc.cmd = WLC_SET_VAR;
4057 	ioc.buf = buf;
4058 	ioc.len = buflen;
4059 	ioc.set = TRUE;
4060 
4061 	ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
4062 	if (ret < 0) {
4063 		DHD_ERROR(("%s: set mcast_list failed, cnt %d\n",
4064 			dhd_ifname(&dhd->pub, ifidx), cnt));
4065 		allmulti = cnt ? TRUE : allmulti;
4066 	}
4067 
4068 	MFREE(dhd->pub.osh, buf, buflen);
4069 
4070 	/* Now send the allmulti setting.  This is based on the setting in the
4071 	 * net_device flags, but might be modified above to be turned on if we
4072 	 * were trying to set some addresses and dongle rejected it...
4073 	 */
4074 
4075 	allmulti = htol32(allmulti);
4076 	ret = dhd_iovar(&dhd->pub, ifidx, "allmulti", (char *)&allmulti,
4077 			sizeof(allmulti), NULL, 0, TRUE);
4078 	if (ret < 0) {
4079 		DHD_ERROR(("%s: set allmulti %d failed\n",
4080 		           dhd_ifname(&dhd->pub, ifidx), ltoh32(allmulti)));
4081 	}
4082 
4083 	/* Finally, pick up the PROMISC flag as well, like the NIC driver does */
4084 
4085 	allmulti = (dev->flags & IFF_PROMISC) ? TRUE : FALSE;
4086 
4087 	allmulti = htol32(allmulti);
4088 
4089 	memset(&ioc, 0, sizeof(ioc));
4090 	ioc.cmd = WLC_SET_PROMISC;
4091 	ioc.buf = &allmulti;
4092 	ioc.len = sizeof(allmulti);
4093 	ioc.set = TRUE;
4094 
4095 	ret = dhd_wl_ioctl(&dhd->pub, ifidx, &ioc, ioc.buf, ioc.len);
4096 	if (ret < 0) {
4097 		DHD_ERROR(("%s: set promisc %d failed\n",
4098 		           dhd_ifname(&dhd->pub, ifidx), ltoh32(allmulti)));
4099 	}
4100 }
4101 
4102 int
_dhd_set_mac_address(dhd_info_t * dhd,int ifidx,uint8 * addr)4103 _dhd_set_mac_address(dhd_info_t *dhd, int ifidx, uint8 *addr)
4104 {
4105 	int ret;
4106 
4107 	ret = dhd_iovar(&dhd->pub, ifidx, "cur_etheraddr", (char *)addr,
4108 			ETHER_ADDR_LEN, NULL, 0, TRUE);
4109 	if (ret < 0) {
4110 		DHD_ERROR(("%s: set cur_etheraddr failed\n", dhd_ifname(&dhd->pub, ifidx)));
4111 	} else {
4112 		memcpy(dhd->iflist[ifidx]->net->dev_addr, addr, ETHER_ADDR_LEN);
4113 		if (ifidx == 0)
4114 			memcpy(dhd->pub.mac.octet, addr, ETHER_ADDR_LEN);
4115 	}
4116 
4117 	return ret;
4118 }
4119 
4120 #ifdef DHD_WMF
dhd_update_psta_interface_for_sta(dhd_pub_t * dhdp,char * ifname,void * ea,void * event_data)4121 void dhd_update_psta_interface_for_sta(dhd_pub_t* dhdp, char* ifname, void* ea,
4122 		void* event_data)
4123 {
4124 	struct wl_psta_primary_intf_event *psta_prim_event =
4125 			(struct wl_psta_primary_intf_event*)event_data;
4126 	dhd_sta_t *psta_interface =  NULL;
4127 	dhd_sta_t *sta = NULL;
4128 	uint8 ifindex;
4129 	ASSERT(ifname);
4130 	ASSERT(psta_prim_event);
4131 	ASSERT(ea);
4132 
4133 	ifindex = (uint8)dhd_ifname2idx(dhdp->info, ifname);
4134 	sta = dhd_find_sta(dhdp, ifindex, ea);
4135 	if (sta != NULL) {
4136 		psta_interface = dhd_find_sta(dhdp, ifindex,
4137 				(void *)(psta_prim_event->prim_ea.octet));
4138 		if (psta_interface != NULL) {
4139 			sta->psta_prim = psta_interface;
4140 		}
4141 	}
4142 }
4143 
4144 /* Get wmf_psta_disable configuration configuration */
dhd_get_wmf_psta_disable(dhd_pub_t * dhdp,uint32 idx)4145 int dhd_get_wmf_psta_disable(dhd_pub_t *dhdp, uint32 idx)
4146 {
4147 	dhd_info_t *dhd = dhdp->info;
4148 	dhd_if_t *ifp;
4149 	ASSERT(idx < DHD_MAX_IFS);
4150 	ifp = dhd->iflist[idx];
4151 	return ifp->wmf_psta_disable;
4152 }
4153 
4154 /* Set wmf_psta_disable configuration configuration */
dhd_set_wmf_psta_disable(dhd_pub_t * dhdp,uint32 idx,int val)4155 int dhd_set_wmf_psta_disable(dhd_pub_t *dhdp, uint32 idx, int val)
4156 {
4157 	dhd_info_t *dhd = dhdp->info;
4158 	dhd_if_t *ifp;
4159 	ASSERT(idx < DHD_MAX_IFS);
4160 	ifp = dhd->iflist[idx];
4161 	ifp->wmf_psta_disable = val;
4162 	return 0;
4163 }
4164 #endif /* DHD_WMF */
4165 
4166 #ifdef DHD_PSTA
4167 /* Get psta/psr configuration configuration */
dhd_get_psta_mode(dhd_pub_t * dhdp)4168 int dhd_get_psta_mode(dhd_pub_t *dhdp)
4169 {
4170 	dhd_info_t *dhd = dhdp->info;
4171 	return (int)dhd->psta_mode;
4172 }
4173 /* Set psta/psr configuration configuration */
dhd_set_psta_mode(dhd_pub_t * dhdp,uint32 val)4174 int dhd_set_psta_mode(dhd_pub_t *dhdp, uint32 val)
4175 {
4176 	dhd_info_t *dhd = dhdp->info;
4177 	dhd->psta_mode = val;
4178 	return 0;
4179 }
4180 #endif /* DHD_PSTA */
4181 
4182 #if (defined(DHD_WET) || defined(DHD_MCAST_REGEN) || defined(DHD_L2_FILTER))
4183 static void
dhd_update_rx_pkt_chainable_state(dhd_pub_t * dhdp,uint32 idx)4184 dhd_update_rx_pkt_chainable_state(dhd_pub_t* dhdp, uint32 idx)
4185 {
4186 	dhd_info_t *dhd = dhdp->info;
4187 	dhd_if_t *ifp;
4188 
4189 	ASSERT(idx < DHD_MAX_IFS);
4190 
4191 	ifp = dhd->iflist[idx];
4192 
4193 	if (
4194 #ifdef DHD_L2_FILTER
4195 		(ifp->block_ping) ||
4196 #endif
4197 #ifdef DHD_WET
4198 		(dhd->wet_mode) ||
4199 #endif
4200 #ifdef DHD_MCAST_REGEN
4201 		(ifp->mcast_regen_bss_enable) ||
4202 #endif
4203 		FALSE) {
4204 		ifp->rx_pkt_chainable = FALSE;
4205 	}
4206 }
4207 #endif /* DHD_WET || DHD_MCAST_REGEN || DHD_L2_FILTER */
4208 
4209 #ifdef DHD_WET
4210 /* Get wet configuration configuration */
dhd_get_wet_mode(dhd_pub_t * dhdp)4211 int dhd_get_wet_mode(dhd_pub_t *dhdp)
4212 {
4213 	dhd_info_t *dhd = dhdp->info;
4214 	return (int)dhd->wet_mode;
4215 }
4216 
4217 /* Set wet configuration configuration */
dhd_set_wet_mode(dhd_pub_t * dhdp,uint32 val)4218 int dhd_set_wet_mode(dhd_pub_t *dhdp, uint32 val)
4219 {
4220 	dhd_info_t *dhd = dhdp->info;
4221 	dhd->wet_mode = val;
4222 	dhd_update_rx_pkt_chainable_state(dhdp, 0);
4223 	return 0;
4224 }
4225 #endif /* DHD_WET */
4226 
4227 #if defined(WL_CFG80211) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0))
dhd_role_to_nl80211_iftype(int32 role)4228 int32 dhd_role_to_nl80211_iftype(int32 role)
4229 {
4230 	switch (role) {
4231 	case WLC_E_IF_ROLE_STA:
4232 		return NL80211_IFTYPE_STATION;
4233 	case WLC_E_IF_ROLE_AP:
4234 		return NL80211_IFTYPE_AP;
4235 	case WLC_E_IF_ROLE_WDS:
4236 		return NL80211_IFTYPE_WDS;
4237 	case WLC_E_IF_ROLE_P2P_GO:
4238 		return NL80211_IFTYPE_P2P_GO;
4239 	case WLC_E_IF_ROLE_P2P_CLIENT:
4240 		return NL80211_IFTYPE_P2P_CLIENT;
4241 	case WLC_E_IF_ROLE_IBSS:
4242 	case WLC_E_IF_ROLE_NAN:
4243 		return NL80211_IFTYPE_ADHOC;
4244 	default:
4245 		return NL80211_IFTYPE_UNSPECIFIED;
4246 	}
4247 }
4248 #endif /* WL_CFG80211 && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0) */
4249 
4250 static void
dhd_ifadd_event_handler(void * handle,void * event_info,u8 event)4251 dhd_ifadd_event_handler(void *handle, void *event_info, u8 event)
4252 {
4253 	dhd_info_t *dhd = handle;
4254 	dhd_if_event_t *if_event = event_info;
4255 	struct net_device *ndev;
4256 	int ifidx, bssidx;
4257 	int ret;
4258 #if defined(WL_CFG80211) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0))
4259 	struct wl_if_event_info info;
4260 #endif /* WL_CFG80211 && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0) */
4261 
4262 	if (event != DHD_WQ_WORK_IF_ADD) {
4263 		DHD_ERROR(("%s: unexpected event \n", __FUNCTION__));
4264 		return;
4265 	}
4266 
4267 	if (!dhd) {
4268 		DHD_ERROR(("%s: dhd info not available \n", __FUNCTION__));
4269 		return;
4270 	}
4271 
4272 	if (!if_event) {
4273 		DHD_ERROR(("%s: event data is null \n", __FUNCTION__));
4274 		return;
4275 	}
4276 
4277 	dhd_net_if_lock_local(dhd);
4278 	DHD_OS_WAKE_LOCK(&dhd->pub);
4279 	DHD_PERIM_LOCK(&dhd->pub);
4280 
4281 	ifidx = if_event->event.ifidx;
4282 	bssidx = if_event->event.bssidx;
4283 	DHD_TRACE(("%s: registering if with ifidx %d\n", __FUNCTION__, ifidx));
4284 
4285 
4286 #if defined(WL_CFG80211) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0))
4287 	if (if_event->event.ifidx > 0) {
4288 		bzero(&info, sizeof(info));
4289 		info.ifidx = if_event->event.ifidx;
4290 		info.bssidx = if_event->event.bssidx;
4291 		info.role = if_event->event.role;
4292 		strncpy(info.name, if_event->name, IFNAMSIZ);
4293 		if (wl_cfg80211_post_ifcreate(dhd->pub.info->iflist[0]->net,
4294 			&info, if_event->mac, NULL, true) != NULL) {
4295 			/* Do the post interface create ops */
4296 			DHD_ERROR(("Post ifcreate ops done. Returning \n"));
4297 			goto done;
4298 		}
4299 	}
4300 #endif /* WL_CFG80211 && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0) */
4301 
4302 	/* This path is for non-android case */
4303 	/* The interface name in host and in event msg are same */
4304 	/* if name in event msg is used to create dongle if list on host */
4305 	ndev = dhd_allocate_if(&dhd->pub, ifidx, if_event->name,
4306 		if_event->mac, bssidx, TRUE, if_event->name);
4307 	if (!ndev) {
4308 		DHD_ERROR(("%s: net device alloc failed  \n", __FUNCTION__));
4309 		goto done;
4310 	}
4311 
4312 	DHD_PERIM_UNLOCK(&dhd->pub);
4313 	ret = dhd_register_if(&dhd->pub, ifidx, TRUE);
4314 	DHD_PERIM_LOCK(&dhd->pub);
4315 	if (ret != BCME_OK) {
4316 		DHD_ERROR(("%s: dhd_register_if failed\n", __FUNCTION__));
4317 		dhd_remove_if(&dhd->pub, ifidx, TRUE);
4318 		goto done;
4319 	}
4320 #ifndef PCIE_FULL_DONGLE
4321 	/* Turn on AP isolation in the firmware for interfaces operating in AP mode */
4322 	if (FW_SUPPORTED((&dhd->pub), ap) && (if_event->event.role != WLC_E_IF_ROLE_STA)) {
4323 		uint32 var_int =  1;
4324 		ret = dhd_iovar(&dhd->pub, ifidx, "ap_isolate", (char *)&var_int, sizeof(var_int),
4325 				NULL, 0, TRUE);
4326 		if (ret != BCME_OK) {
4327 			DHD_ERROR(("%s: Failed to set ap_isolate to dongle\n", __FUNCTION__));
4328 			dhd_remove_if(&dhd->pub, ifidx, TRUE);
4329 		}
4330 	}
4331 #endif /* PCIE_FULL_DONGLE */
4332 
4333 done:
4334 	MFREE(dhd->pub.osh, if_event, sizeof(dhd_if_event_t));
4335 
4336 	DHD_PERIM_UNLOCK(&dhd->pub);
4337 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
4338 	dhd_net_if_unlock_local(dhd);
4339 }
4340 
4341 static void
dhd_ifdel_event_handler(void * handle,void * event_info,u8 event)4342 dhd_ifdel_event_handler(void *handle, void *event_info, u8 event)
4343 {
4344 	dhd_info_t *dhd = handle;
4345 	int ifidx;
4346 	dhd_if_event_t *if_event = event_info;
4347 
4348 
4349 	if (event != DHD_WQ_WORK_IF_DEL) {
4350 		DHD_ERROR(("%s: unexpected event \n", __FUNCTION__));
4351 		return;
4352 	}
4353 
4354 	if (!dhd) {
4355 		DHD_ERROR(("%s: dhd info not available \n", __FUNCTION__));
4356 		return;
4357 	}
4358 
4359 	if (!if_event) {
4360 		DHD_ERROR(("%s: event data is null \n", __FUNCTION__));
4361 		return;
4362 	}
4363 
4364 	dhd_net_if_lock_local(dhd);
4365 	DHD_OS_WAKE_LOCK(&dhd->pub);
4366 	DHD_PERIM_LOCK(&dhd->pub);
4367 
4368 	ifidx = if_event->event.ifidx;
4369 	DHD_TRACE(("Removing interface with idx %d\n", ifidx));
4370 
4371 	DHD_PERIM_UNLOCK(&dhd->pub);
4372 #if defined(WL_CFG80211) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0))
4373 	if (if_event->event.ifidx > 0) {
4374 		/* Do the post interface del ops */
4375 		if (wl_cfg80211_post_ifdel(dhd->pub.info->iflist[ifidx]->net, true) == 0) {
4376 			DHD_TRACE(("Post ifdel ops done. Returning \n"));
4377 			DHD_PERIM_LOCK(&dhd->pub);
4378 			goto done;
4379 		}
4380 	}
4381 #endif /* WL_CFG80211 && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0) */
4382 
4383 	dhd_remove_if(&dhd->pub, ifidx, TRUE);
4384 	DHD_PERIM_LOCK(&dhd->pub);
4385 
4386 #if defined(WL_CFG80211) && (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0))
4387 done:
4388 #endif /* WL_CFG80211 && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0) */
4389 	MFREE(dhd->pub.osh, if_event, sizeof(dhd_if_event_t));
4390 
4391 	DHD_PERIM_UNLOCK(&dhd->pub);
4392 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
4393 	dhd_net_if_unlock_local(dhd);
4394 }
4395 
4396 #ifdef DHD_UPDATE_INTF_MAC
4397 static void
dhd_ifupdate_event_handler(void * handle,void * event_info,u8 event)4398 dhd_ifupdate_event_handler(void *handle, void *event_info, u8 event)
4399 {
4400 	dhd_info_t *dhd = handle;
4401 	int ifidx;
4402 	dhd_if_event_t *if_event = event_info;
4403 
4404 	if (event != DHD_WQ_WORK_IF_UPDATE) {
4405 		DHD_ERROR(("%s: unexpected event \n", __FUNCTION__));
4406 		return;
4407 	}
4408 
4409 	if (!dhd) {
4410 		DHD_ERROR(("%s: dhd info not available \n", __FUNCTION__));
4411 		return;
4412 	}
4413 
4414 	if (!if_event) {
4415 		DHD_ERROR(("%s: event data is null \n", __FUNCTION__));
4416 		return;
4417 	}
4418 
4419 	dhd_net_if_lock_local(dhd);
4420 	DHD_OS_WAKE_LOCK(&dhd->pub);
4421 
4422 	ifidx = if_event->event.ifidx;
4423 	DHD_TRACE(("%s: Update interface with idx %d\n", __FUNCTION__, ifidx));
4424 
4425 	dhd_op_if_update(&dhd->pub, ifidx);
4426 
4427 	MFREE(dhd->pub.osh, if_event, sizeof(dhd_if_event_t));
4428 
4429 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
4430 	dhd_net_if_unlock_local(dhd);
4431 }
4432 
dhd_op_if_update(dhd_pub_t * dhdpub,int ifidx)4433 int dhd_op_if_update(dhd_pub_t *dhdpub, int ifidx)
4434 {
4435 	dhd_info_t *    dhdinfo = NULL;
4436 	dhd_if_t   *    ifp = NULL;
4437 	int             ret = 0;
4438 	char            buf[128];
4439 
4440 	if ((NULL==dhdpub)||(NULL==dhdpub->info)) {
4441 		DHD_ERROR(("%s: *** DHD handler is NULL!\n", __FUNCTION__));
4442 		return -1;
4443 	} else {
4444 		dhdinfo = (dhd_info_t *)dhdpub->info;
4445 		ifp = dhdinfo->iflist[ifidx];
4446 		if (NULL==ifp) {
4447 		    DHD_ERROR(("%s: *** ifp handler is NULL!\n", __FUNCTION__));
4448 		    return -2;
4449 		}
4450 	}
4451 
4452 	DHD_TRACE(("%s: idx %d\n", __FUNCTION__, ifidx));
4453 	// Get MAC address
4454 	strcpy(buf, "cur_etheraddr");
4455 	ret = dhd_wl_ioctl_cmd(&dhdinfo->pub, WLC_GET_VAR, buf, sizeof(buf), FALSE, ifp->idx);
4456 	if (0>ret) {
4457 		DHD_ERROR(("Failed to upudate the MAC address for itf=%s, ret=%d\n", ifp->name, ret));
4458 		// avoid collision
4459 		dhdinfo->iflist[ifp->idx]->mac_addr[5] += 1;
4460 		// force locally administrate address
4461 		ETHER_SET_LOCALADDR(&dhdinfo->iflist[ifp->idx]->mac_addr);
4462 	} else {
4463 		DHD_EVENT(("Got mac for itf %s, idx %d, MAC=%02X:%02X:%02X:%02X:%02X:%02X\n",
4464 		           ifp->name, ifp->idx,
4465 		           (unsigned char)buf[0], (unsigned char)buf[1], (unsigned char)buf[2],
4466 		           (unsigned char)buf[3], (unsigned char)buf[4], (unsigned char)buf[5]));
4467 		memcpy(dhdinfo->iflist[ifp->idx]->mac_addr, buf, ETHER_ADDR_LEN);
4468 		if (dhdinfo->iflist[ifp->idx]->net) {
4469 		    memcpy(dhdinfo->iflist[ifp->idx]->net->dev_addr, buf, ETHER_ADDR_LEN);
4470 		}
4471 	}
4472 
4473 	return ret;
4474 }
4475 #endif /* DHD_UPDATE_INTF_MAC */
4476 
4477 static void
dhd_set_mac_addr_handler(void * handle,void * event_info,u8 event)4478 dhd_set_mac_addr_handler(void *handle, void *event_info, u8 event)
4479 {
4480 	dhd_info_t *dhd = handle;
4481 	dhd_if_t *ifp = event_info;
4482 
4483 	if (event != DHD_WQ_WORK_SET_MAC) {
4484 		DHD_ERROR(("%s: unexpected event \n", __FUNCTION__));
4485 	}
4486 
4487 	if (!dhd) {
4488 		DHD_ERROR(("%s: dhd info not available \n", __FUNCTION__));
4489 		return;
4490 	}
4491 
4492 	dhd_net_if_lock_local(dhd);
4493 	DHD_OS_WAKE_LOCK(&dhd->pub);
4494 	DHD_PERIM_LOCK(&dhd->pub);
4495 
4496 	// terence 20160907: fix for not able to set mac when wlan0 is down
4497 	if (ifp == NULL || !ifp->set_macaddress) {
4498 		goto done;
4499 	}
4500 	if (ifp == NULL || !dhd->pub.up) {
4501 		DHD_ERROR(("%s: interface info not available/down \n", __FUNCTION__));
4502 		goto done;
4503 	}
4504 
4505 	DHD_ERROR(("%s: MACID is overwritten\n", __FUNCTION__));
4506 	ifp->set_macaddress = FALSE;
4507 	if (_dhd_set_mac_address(dhd, ifp->idx, ifp->mac_addr) == 0)
4508 		DHD_INFO(("%s: MACID is overwritten\n",	__FUNCTION__));
4509 	else
4510 		DHD_ERROR(("%s: _dhd_set_mac_address() failed\n", __FUNCTION__));
4511 
4512 done:
4513 	DHD_PERIM_UNLOCK(&dhd->pub);
4514 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
4515 	dhd_net_if_unlock_local(dhd);
4516 }
4517 
4518 static void
dhd_set_mcast_list_handler(void * handle,void * event_info,u8 event)4519 dhd_set_mcast_list_handler(void *handle, void *event_info, u8 event)
4520 {
4521 	dhd_info_t *dhd = handle;
4522 	int ifidx = (int)((long int)event_info);
4523 	dhd_if_t *ifp = NULL;
4524 
4525 	if (event != DHD_WQ_WORK_SET_MCAST_LIST) {
4526 		DHD_ERROR(("%s: unexpected event \n", __FUNCTION__));
4527 		return;
4528 	}
4529 
4530 	if (!dhd) {
4531 		DHD_ERROR(("%s: dhd info not available \n", __FUNCTION__));
4532 		return;
4533 	}
4534 
4535 	dhd_net_if_lock_local(dhd);
4536 	DHD_OS_WAKE_LOCK(&dhd->pub);
4537 	DHD_PERIM_LOCK(&dhd->pub);
4538 
4539 	ifp = dhd->iflist[ifidx];
4540 
4541 	if (ifp == NULL || !dhd->pub.up) {
4542 		DHD_ERROR(("%s: interface info not available/down \n", __FUNCTION__));
4543 		goto done;
4544 	}
4545 
4546 	if (ifp == NULL || !dhd->pub.up) {
4547 		DHD_ERROR(("%s: interface info not available/down \n", __FUNCTION__));
4548 		goto done;
4549 	}
4550 
4551 	ifidx = ifp->idx;
4552 
4553 
4554 	_dhd_set_multicast_list(dhd, ifidx);
4555 	DHD_INFO(("%s: set multicast list for if %d\n", __FUNCTION__, ifidx));
4556 
4557 done:
4558 	DHD_PERIM_UNLOCK(&dhd->pub);
4559 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
4560 	dhd_net_if_unlock_local(dhd);
4561 }
4562 
4563 static int
dhd_set_mac_address(struct net_device * dev,void * addr)4564 dhd_set_mac_address(struct net_device *dev, void *addr)
4565 {
4566 	int ret = 0;
4567 
4568 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
4569 	struct sockaddr *sa = (struct sockaddr *)addr;
4570 	int ifidx;
4571 	dhd_if_t *dhdif;
4572 
4573 	ifidx = dhd_net2idx(dhd, dev);
4574 	if (ifidx == DHD_BAD_IF)
4575 		return -1;
4576 
4577 	dhdif = dhd->iflist[ifidx];
4578 
4579 	dhd_net_if_lock_local(dhd);
4580 	memcpy(dhdif->mac_addr, sa->sa_data, ETHER_ADDR_LEN);
4581 	dhdif->set_macaddress = TRUE;
4582 	dhd_net_if_unlock_local(dhd);
4583 	dhd_deferred_schedule_work(dhd->dhd_deferred_wq, (void *)dhdif, DHD_WQ_WORK_SET_MAC,
4584 		dhd_set_mac_addr_handler, DHD_WQ_WORK_PRIORITY_LOW);
4585 	return ret;
4586 }
4587 
4588 static void
dhd_set_multicast_list(struct net_device * dev)4589 dhd_set_multicast_list(struct net_device *dev)
4590 {
4591 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
4592 	int ifidx;
4593 
4594 	ifidx = dhd_net2idx(dhd, dev);
4595 	if (ifidx == DHD_BAD_IF)
4596 		return;
4597 
4598 	dhd->iflist[ifidx]->set_multicast = TRUE;
4599 	dhd_deferred_schedule_work(dhd->dhd_deferred_wq, (void *)((long int)ifidx),
4600 		DHD_WQ_WORK_SET_MCAST_LIST, dhd_set_mcast_list_handler, DHD_WQ_WORK_PRIORITY_LOW);
4601 
4602 	// terence 20160907: fix for not able to set mac when wlan0 is down
4603 	dhd_deferred_schedule_work(dhd->dhd_deferred_wq, (void *)dhd->iflist[ifidx],
4604 		DHD_WQ_WORK_SET_MAC, dhd_set_mac_addr_handler, DHD_WQ_WORK_PRIORITY_LOW);
4605 }
4606 
4607 #ifdef DHD_UCODE_DOWNLOAD
4608 /* Get ucode path */
4609 char *
dhd_get_ucode_path(dhd_pub_t * dhdp)4610 dhd_get_ucode_path(dhd_pub_t *dhdp)
4611 {
4612 	dhd_info_t *dhd = dhdp->info;
4613 	return dhd->uc_path;
4614 }
4615 #endif /* DHD_UCODE_DOWNLOAD */
4616 
4617 #ifdef PROP_TXSTATUS
4618 int
dhd_os_wlfc_block(dhd_pub_t * pub)4619 dhd_os_wlfc_block(dhd_pub_t *pub)
4620 {
4621 	dhd_info_t *di = (dhd_info_t *)(pub->info);
4622 	ASSERT(di != NULL);
4623 	/* terence 20161229: don't do spin lock if proptx not enabled */
4624 	if (disable_proptx)
4625 		return 1;
4626 #ifdef BCMDBUS
4627 	spin_lock_irqsave(&di->wlfc_spinlock, di->wlfc_lock_flags);
4628 #else
4629 	spin_lock_bh(&di->wlfc_spinlock);
4630 #endif /* BCMDBUS */
4631 	return 1;
4632 }
4633 
4634 int
dhd_os_wlfc_unblock(dhd_pub_t * pub)4635 dhd_os_wlfc_unblock(dhd_pub_t *pub)
4636 {
4637 	dhd_info_t *di = (dhd_info_t *)(pub->info);
4638 
4639 	ASSERT(di != NULL);
4640 	/* terence 20161229: don't do spin lock if proptx not enabled */
4641 	if (disable_proptx)
4642 		return 1;
4643 #ifdef BCMDBUS
4644 	spin_unlock_irqrestore(&di->wlfc_spinlock, di->wlfc_lock_flags);
4645 #else
4646 	spin_unlock_bh(&di->wlfc_spinlock);
4647 #endif /* BCMDBUS */
4648 	return 1;
4649 }
4650 
4651 #endif /* PROP_TXSTATUS */
4652 
4653 #if defined(DHD_RX_DUMP) || defined(DHD_TX_DUMP)
4654 typedef struct {
4655 	uint16 type;
4656 	const char *str;
4657 } PKTTYPE_INFO;
4658 
4659 static const PKTTYPE_INFO packet_type_info[] =
4660 {
4661 	{ ETHER_TYPE_IP, "IP" },
4662 	{ ETHER_TYPE_ARP, "ARP" },
4663 	{ ETHER_TYPE_BRCM, "BRCM" },
4664 	{ ETHER_TYPE_802_1X, "802.1X" },
4665 	{ ETHER_TYPE_WAI, "WAPI" },
4666 	{ 0, ""}
4667 };
4668 
_get_packet_type_str(uint16 type)4669 static const char *_get_packet_type_str(uint16 type)
4670 {
4671 	int i;
4672 	int n = sizeof(packet_type_info)/sizeof(packet_type_info[1]) - 1;
4673 
4674 	for (i = 0; i < n; i++) {
4675 		if (packet_type_info[i].type == type)
4676 			return packet_type_info[i].str;
4677 	}
4678 
4679 	return packet_type_info[n].str;
4680 }
4681 
4682 void
dhd_trx_dump(struct net_device * ndev,uint8 * dump_data,uint datalen,bool tx)4683 dhd_trx_dump(struct net_device *ndev, uint8 *dump_data, uint datalen, bool tx)
4684 {
4685 	uint16 protocol;
4686 	char *ifname;
4687 
4688 	protocol = (dump_data[12] << 8) | dump_data[13];
4689 	ifname = ndev ? ndev->name : "N/A";
4690 
4691 	if (protocol != ETHER_TYPE_BRCM) {
4692 		printk("[dhd-%s] %s DUMP - %s\n", ifname, tx?"Tx":"Rx",
4693 			_get_packet_type_str(protocol));
4694 #if defined(DHD_TX_FULL_DUMP) || defined(DHD_RX_FULL_DUMP)
4695 		prhex("Data", dump_data, datalen);
4696 #endif /* DHD_TX_FULL_DUMP || DHD_RX_FULL_DUMP */
4697 	}
4698 }
4699 #endif /* DHD_TX_DUMP || DHD_RX_DUMP */
4700 
4701 /*  This routine do not support Packet chain feature, Currently tested for
4702  *  proxy arp feature
4703  */
dhd_sendup(dhd_pub_t * dhdp,int ifidx,void * p)4704 int dhd_sendup(dhd_pub_t *dhdp, int ifidx, void *p)
4705 {
4706 	struct sk_buff *skb;
4707 	void *skbhead = NULL;
4708 	void *skbprev = NULL;
4709 	dhd_if_t *ifp;
4710 	ASSERT(!PKTISCHAINED(p));
4711 	skb = PKTTONATIVE(dhdp->osh, p);
4712 
4713 	ifp = dhdp->info->iflist[ifidx];
4714 	skb->dev = ifp->net;
4715 #if defined(BCM_GMAC3)
4716 	/* Forwarder capable interfaces use WOFA based forwarding */
4717 	if (ifp->fwdh) {
4718 		struct ether_header *eh = (struct ether_header *)PKTDATA(dhdp->osh, p);
4719 		uint16 * da = (uint16 *)(eh->ether_dhost);
4720 		uintptr_t wofa_data;
4721 		ASSERT(ISALIGNED(da, 2));
4722 
4723 		wofa_data = fwder_lookup(ifp->fwdh->mate, da, ifp->idx);
4724 		if (wofa_data == WOFA_DATA_INVALID) { /* Unknown MAC address */
4725 			if (fwder_transmit(ifp->fwdh, skb, 1, skb->dev) == FWDER_SUCCESS) {
4726 				return BCME_OK;
4727 			}
4728 		}
4729 		PKTFRMNATIVE(dhdp->osh, p);
4730 		PKTFREE(dhdp->osh, p, FALSE);
4731 		return BCME_OK;
4732 	}
4733 #endif /* BCM_GMAC3 */
4734 
4735 	skb->protocol = eth_type_trans(skb, skb->dev);
4736 
4737 	if (in_interrupt()) {
4738 		bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE,
4739 			__FUNCTION__, __LINE__);
4740 		netif_rx(skb);
4741 	} else {
4742 		if (dhdp->info->rxthread_enabled) {
4743 			if (!skbhead) {
4744 				skbhead = skb;
4745 			} else {
4746 				PKTSETNEXT(dhdp->osh, skbprev, skb);
4747 			}
4748 			skbprev = skb;
4749 		} else {
4750 			/* If the receive is not processed inside an ISR,
4751 			 * the softirqd must be woken explicitly to service
4752 			 * the NET_RX_SOFTIRQ.	In 2.6 kernels, this is handled
4753 			 * by netif_rx_ni(), but in earlier kernels, we need
4754 			 * to do it manually.
4755 			 */
4756 			bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE,
4757 				__FUNCTION__, __LINE__);
4758 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
4759 			netif_rx_ni(skb);
4760 #else
4761 			ulong flags;
4762 			netif_rx(skb);
4763 			local_irq_save(flags);
4764 			RAISE_RX_SOFTIRQ();
4765 			local_irq_restore(flags);
4766 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) */
4767 		}
4768 	}
4769 
4770 	if (dhdp->info->rxthread_enabled && skbhead)
4771 		dhd_sched_rxf(dhdp, skbhead);
4772 
4773 	return BCME_OK;
4774 }
4775 
4776 int BCMFASTPATH
__dhd_sendpkt(dhd_pub_t * dhdp,int ifidx,void * pktbuf)4777 __dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, void *pktbuf)
4778 {
4779 	int ret = BCME_OK;
4780 	dhd_info_t *dhd = (dhd_info_t *)(dhdp->info);
4781 	struct ether_header *eh = NULL;
4782 #if defined(DHD_L2_FILTER)
4783 	dhd_if_t *ifp = dhd_get_ifp(dhdp, ifidx);
4784 #endif
4785 
4786 	/* Reject if down */
4787 	if (!dhdp->up || (dhdp->busstate == DHD_BUS_DOWN)) {
4788 		/* free the packet here since the caller won't */
4789 		PKTCFREE(dhdp->osh, pktbuf, TRUE);
4790 		return -ENODEV;
4791 	}
4792 
4793 #ifdef PCIE_FULL_DONGLE
4794 	if (dhdp->busstate == DHD_BUS_SUSPEND) {
4795 		DHD_ERROR(("%s : pcie is still in suspend state!!\n", __FUNCTION__));
4796 		PKTCFREE(dhdp->osh, pktbuf, TRUE);
4797 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20))
4798 		return -ENODEV;
4799 #else
4800 		return NETDEV_TX_BUSY;
4801 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20) */
4802 	}
4803 #endif /* PCIE_FULL_DONGLE */
4804 
4805 #ifdef DHD_L2_FILTER
4806 	/* if dhcp_unicast is enabled, we need to convert the */
4807 	/* broadcast DHCP ACK/REPLY packets to Unicast. */
4808 	if (ifp->dhcp_unicast) {
4809 	    uint8* mac_addr;
4810 	    uint8* ehptr = NULL;
4811 	    int ret;
4812 	    ret = bcm_l2_filter_get_mac_addr_dhcp_pkt(dhdp->osh, pktbuf, ifidx, &mac_addr);
4813 	    if (ret == BCME_OK) {
4814 		/*  if given mac address having valid entry in sta list
4815 		 *  copy the given mac address, and return with BCME_OK
4816 		*/
4817 		if (dhd_find_sta(dhdp, ifidx, mac_addr)) {
4818 		    ehptr = PKTDATA(dhdp->osh, pktbuf);
4819 		    bcopy(mac_addr, ehptr + ETHER_DEST_OFFSET, ETHER_ADDR_LEN);
4820 		}
4821 	    }
4822 	}
4823 
4824 	if (ifp->grat_arp && DHD_IF_ROLE_AP(dhdp, ifidx)) {
4825 	    if (bcm_l2_filter_gratuitous_arp(dhdp->osh, pktbuf) == BCME_OK) {
4826 			PKTCFREE(dhdp->osh, pktbuf, TRUE);
4827 			return BCME_ERROR;
4828 	    }
4829 	}
4830 
4831 	if (ifp->parp_enable && DHD_IF_ROLE_AP(dhdp, ifidx)) {
4832 		ret = dhd_l2_filter_pkt_handle(dhdp, ifidx, pktbuf, TRUE);
4833 
4834 		/* Drop the packets if l2 filter has processed it already
4835 		 * otherwise continue with the normal path
4836 		 */
4837 		if (ret == BCME_OK) {
4838 			PKTCFREE(dhdp->osh, pktbuf, TRUE);
4839 			return BCME_ERROR;
4840 		}
4841 	}
4842 #endif /* DHD_L2_FILTER */
4843 	/* Update multicast statistic */
4844 	if (PKTLEN(dhdp->osh, pktbuf) >= ETHER_HDR_LEN) {
4845 		uint8 *pktdata = (uint8 *)PKTDATA(dhdp->osh, pktbuf);
4846 		eh = (struct ether_header *)pktdata;
4847 
4848 		if (ETHER_ISMULTI(eh->ether_dhost))
4849 			dhdp->tx_multicast++;
4850 		if (ntoh16(eh->ether_type) == ETHER_TYPE_802_1X) {
4851 #ifdef DHD_LOSSLESS_ROAMING
4852 			uint8 prio = (uint8)PKTPRIO(pktbuf);
4853 
4854 			/* back up 802.1x's priority */
4855 			dhdp->prio_8021x = prio;
4856 #endif /* DHD_LOSSLESS_ROAMING */
4857 			DBG_EVENT_LOG(dhdp, WIFI_EVENT_DRIVER_EAPOL_FRAME_TRANSMIT_REQUESTED);
4858 			atomic_inc(&dhd->pend_8021x_cnt);
4859 			dhd_dump_eapol_4way_message(dhdp, ifidx, pktdata, TRUE);
4860 		}
4861 
4862 		if (ntoh16(eh->ether_type) == ETHER_TYPE_IP) {
4863 #ifdef DHD_DHCP_DUMP
4864 			dhd_dhcp_dump(dhd_ifname(dhdp, ifidx), pktdata, TRUE);
4865 #endif /* DHD_DHCP_DUMP */
4866 #ifdef DHD_ICMP_DUMP
4867 			dhd_icmp_dump(dhd_ifname(dhdp, ifidx), pktdata, TRUE);
4868 #endif /* DHD_ICMP_DUMP */
4869 			dhd_tcp_dump(dhd_ifname(dhdp, ifidx), pktdata, TRUE);
4870 		}
4871 #ifdef DHD_ARP_DUMP
4872 		if (ntoh16(eh->ether_type) == ETHER_TYPE_ARP) {
4873 			dhd_arp_dump(dhd_ifname(dhdp, ifidx), pktdata, TRUE);
4874 		}
4875 #endif /* DHD_ARP_DUMP */
4876 	} else {
4877 			PKTCFREE(dhdp->osh, pktbuf, TRUE);
4878 			return BCME_ERROR;
4879 	}
4880 
4881 	{
4882 		/* Look into the packet and update the packet priority */
4883 #ifndef PKTPRIO_OVERRIDE
4884 		if (PKTPRIO(pktbuf) == 0)
4885 #endif /* !PKTPRIO_OVERRIDE */
4886 		{
4887 #if defined(QOS_MAP_SET)
4888 			pktsetprio_qms(pktbuf, wl_get_up_table(dhdp, ifidx), FALSE);
4889 #else
4890 			pktsetprio(pktbuf, FALSE);
4891 #endif /* QOS_MAP_SET */
4892 		}
4893 	}
4894 
4895 
4896 #if defined(TRAFFIC_MGMT_DWM)
4897 	traffic_mgmt_pkt_set_prio(dhdp, pktbuf);
4898 
4899 #ifdef BCM_GMAC3
4900 	DHD_PKT_SET_DATAOFF(pktbuf, 0);
4901 #endif /* BCM_GMAC3 */
4902 #endif
4903 
4904 #ifdef PCIE_FULL_DONGLE
4905 	/*
4906 	 * Lkup the per interface hash table, for a matching flowring. If one is not
4907 	 * available, allocate a unique flowid and add a flowring entry.
4908 	 * The found or newly created flowid is placed into the pktbuf's tag.
4909 	 */
4910 	ret = dhd_flowid_update(dhdp, ifidx, dhdp->flow_prio_map[(PKTPRIO(pktbuf))], pktbuf);
4911 	if (ret != BCME_OK) {
4912 		PKTCFREE(dhd->pub.osh, pktbuf, TRUE);
4913 		return ret;
4914 	}
4915 #endif
4916 
4917 #if defined(DHD_TX_DUMP)
4918 	dhd_trx_dump(dhd_idx2net(dhdp, ifidx), PKTDATA(dhdp->osh, pktbuf),
4919 		PKTLEN(dhdp->osh, pktbuf), TRUE);
4920 #endif
4921 	/* terence 20150901: Micky add to ajust the 802.1X priority */
4922 	/* Set the 802.1X packet with the highest priority 7 */
4923 	if (dhdp->conf->pktprio8021x >= 0)
4924 		pktset8021xprio(pktbuf, dhdp->conf->pktprio8021x);
4925 
4926 #ifdef PROP_TXSTATUS
4927 	if (dhd_wlfc_is_supported(dhdp)) {
4928 		/* store the interface ID */
4929 		DHD_PKTTAG_SETIF(PKTTAG(pktbuf), ifidx);
4930 
4931 		/* store destination MAC in the tag as well */
4932 		DHD_PKTTAG_SETDSTN(PKTTAG(pktbuf), eh->ether_dhost);
4933 
4934 		/* decide which FIFO this packet belongs to */
4935 		if (ETHER_ISMULTI(eh->ether_dhost))
4936 			/* one additional queue index (highest AC + 1) is used for bc/mc queue */
4937 			DHD_PKTTAG_SETFIFO(PKTTAG(pktbuf), AC_COUNT);
4938 		else
4939 			DHD_PKTTAG_SETFIFO(PKTTAG(pktbuf), WME_PRIO2AC(PKTPRIO(pktbuf)));
4940 	} else
4941 #endif /* PROP_TXSTATUS */
4942 	{
4943 		/* If the protocol uses a data header, apply it */
4944 		dhd_prot_hdrpush(dhdp, ifidx, pktbuf);
4945 	}
4946 
4947 	/* Use bus module to send data frame */
4948 #ifdef WLMEDIA_HTSF
4949 	dhd_htsf_addtxts(dhdp, pktbuf);
4950 #endif
4951 #ifdef PROP_TXSTATUS
4952 	{
4953 		if (dhd_wlfc_commit_packets(dhdp, (f_commitpkt_t)dhd_bus_txdata,
4954 			dhdp->bus, pktbuf, TRUE) == WLFC_UNSUPPORTED) {
4955 			/* non-proptxstatus way */
4956 #ifdef BCMPCIE
4957 			ret = dhd_bus_txdata(dhdp->bus, pktbuf, (uint8)ifidx);
4958 #else
4959 			ret = dhd_bus_txdata(dhdp->bus, pktbuf);
4960 #endif /* BCMPCIE */
4961 		}
4962 	}
4963 #else
4964 #ifdef BCMPCIE
4965 	ret = dhd_bus_txdata(dhdp->bus, pktbuf, (uint8)ifidx);
4966 #else
4967 	ret = dhd_bus_txdata(dhdp->bus, pktbuf);
4968 #endif /* BCMPCIE */
4969 #endif /* PROP_TXSTATUS */
4970 #ifdef BCMDBUS
4971 	if (ret)
4972 		PKTCFREE(dhdp->osh, pktbuf, TRUE);
4973 #endif /* BCMDBUS */
4974 
4975 	return ret;
4976 }
4977 
4978 int BCMFASTPATH
dhd_sendpkt(dhd_pub_t * dhdp,int ifidx,void * pktbuf)4979 dhd_sendpkt(dhd_pub_t *dhdp, int ifidx, void *pktbuf)
4980 {
4981 	int ret = 0;
4982 	unsigned long flags;
4983 
4984 	DHD_GENERAL_LOCK(dhdp, flags);
4985 	if (DHD_BUS_CHECK_DOWN_OR_DOWN_IN_PROGRESS(dhdp)) {
4986 		DHD_ERROR(("%s: returning as busstate=%d\n",
4987 			__FUNCTION__, dhdp->busstate));
4988 		DHD_GENERAL_UNLOCK(dhdp, flags);
4989 		PKTCFREE(dhdp->osh, pktbuf, TRUE);
4990 		return -ENODEV;
4991 	}
4992 	DHD_BUS_BUSY_SET_IN_SEND_PKT(dhdp);
4993 	DHD_GENERAL_UNLOCK(dhdp, flags);
4994 
4995 #ifdef DHD_PCIE_RUNTIMEPM
4996 	if (dhdpcie_runtime_bus_wake(dhdp, FALSE, __builtin_return_address(0))) {
4997 		DHD_ERROR(("%s : pcie is still in suspend state!!\n", __FUNCTION__));
4998 		PKTCFREE(dhdp->osh, pktbuf, TRUE);
4999 		ret = -EBUSY;
5000 		goto exit;
5001 	}
5002 #endif /* DHD_PCIE_RUNTIMEPM */
5003 
5004 	DHD_GENERAL_LOCK(dhdp, flags);
5005 	if (DHD_BUS_CHECK_SUSPEND_OR_SUSPEND_IN_PROGRESS(dhdp)) {
5006 		DHD_ERROR(("%s: bus is in suspend(%d) or suspending(0x%x) state!!\n",
5007 			__FUNCTION__, dhdp->busstate, dhdp->dhd_bus_busy_state));
5008 		DHD_BUS_BUSY_CLEAR_IN_SEND_PKT(dhdp);
5009 		dhd_os_busbusy_wake(dhdp);
5010 		DHD_GENERAL_UNLOCK(dhdp, flags);
5011 		PKTCFREE(dhdp->osh, pktbuf, TRUE);
5012 		return -ENODEV;
5013 	}
5014 	DHD_GENERAL_UNLOCK(dhdp, flags);
5015 
5016 	ret = __dhd_sendpkt(dhdp, ifidx, pktbuf);
5017 
5018 #ifdef DHD_PCIE_RUNTIMEPM
5019 exit:
5020 #endif
5021 	DHD_GENERAL_LOCK(dhdp, flags);
5022 	DHD_BUS_BUSY_CLEAR_IN_SEND_PKT(dhdp);
5023 	dhd_os_busbusy_wake(dhdp);
5024 	DHD_GENERAL_UNLOCK(dhdp, flags);
5025 	return ret;
5026 }
5027 
5028 #if defined(DHD_LB_TXP)
5029 
5030 int BCMFASTPATH
dhd_lb_sendpkt(dhd_info_t * dhd,struct net_device * net,int ifidx,void * skb)5031 dhd_lb_sendpkt(dhd_info_t *dhd, struct net_device *net,
5032 	int ifidx, void *skb)
5033 {
5034 	DHD_LB_STATS_PERCPU_ARR_INCR(dhd->tx_start_percpu_run_cnt);
5035 
5036 	/* If the feature is disabled run-time do TX from here */
5037 	if (atomic_read(&dhd->lb_txp_active) == 0) {
5038 		DHD_LB_STATS_PERCPU_ARR_INCR(dhd->txp_percpu_run_cnt);
5039 		 return __dhd_sendpkt(&dhd->pub, ifidx, skb);
5040 	}
5041 
5042 	/* Store the address of net device and interface index in the Packet tag */
5043 	DHD_LB_TX_PKTTAG_SET_NETDEV((dhd_tx_lb_pkttag_fr_t *)PKTTAG(skb), net);
5044 	DHD_LB_TX_PKTTAG_SET_IFIDX((dhd_tx_lb_pkttag_fr_t *)PKTTAG(skb), ifidx);
5045 
5046 	/* Enqueue the skb into tx_pend_queue */
5047 	skb_queue_tail(&dhd->tx_pend_queue, skb);
5048 
5049 	DHD_TRACE(("%s(): Added skb %p for netdev %p \r\n", __FUNCTION__, skb, net));
5050 
5051 	/* Dispatch the Tx job to be processed by the tx_tasklet */
5052 	dhd_lb_tx_dispatch(&dhd->pub);
5053 
5054 	return NETDEV_TX_OK;
5055 }
5056 #endif /* DHD_LB_TXP */
5057 
5058 int BCMFASTPATH
dhd_start_xmit(struct sk_buff * skb,struct net_device * net)5059 dhd_start_xmit(struct sk_buff *skb, struct net_device *net)
5060 {
5061 	int ret;
5062 	uint datalen;
5063 	void *pktbuf;
5064 	dhd_info_t *dhd = DHD_DEV_INFO(net);
5065 	dhd_if_t *ifp = NULL;
5066 	int ifidx;
5067 	unsigned long flags;
5068 #ifdef WLMEDIA_HTSF
5069 	uint8 htsfdlystat_sz = dhd->pub.htsfdlystat_sz;
5070 #else
5071 	uint8 htsfdlystat_sz = 0;
5072 #endif
5073 #ifdef DHD_WMF
5074 	struct ether_header *eh;
5075 	uint8 *iph;
5076 #endif /* DHD_WMF */
5077 
5078 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
5079 
5080 	if (dhd_query_bus_erros(&dhd->pub)) {
5081 		return -ENODEV;
5082 	}
5083 
5084 	DHD_GENERAL_LOCK(&dhd->pub, flags);
5085 	DHD_BUS_BUSY_SET_IN_TX(&dhd->pub);
5086 	DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5087 
5088 #ifdef DHD_PCIE_RUNTIMEPM
5089 	if (dhdpcie_runtime_bus_wake(&dhd->pub, FALSE, dhd_start_xmit)) {
5090 		/* In order to avoid pkt loss. Return NETDEV_TX_BUSY until run-time resumed. */
5091 		/* stop the network queue temporarily until resume done */
5092 		DHD_GENERAL_LOCK(&dhd->pub, flags);
5093 		if (!dhdpcie_is_resume_done(&dhd->pub)) {
5094 			dhd_bus_stop_queue(dhd->pub.bus);
5095 		}
5096 		DHD_BUS_BUSY_CLEAR_IN_TX(&dhd->pub);
5097 		dhd_os_busbusy_wake(&dhd->pub);
5098 		DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5099 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20))
5100 		return -ENODEV;
5101 #else
5102 		return NETDEV_TX_BUSY;
5103 #endif
5104 	}
5105 #endif /* DHD_PCIE_RUNTIMEPM */
5106 
5107 	DHD_GENERAL_LOCK(&dhd->pub, flags);
5108 #ifdef BCMPCIE
5109 	if (DHD_BUS_CHECK_SUSPEND_OR_SUSPEND_IN_PROGRESS(&dhd->pub)) {
5110 		DHD_ERROR(("%s: bus is in suspend(%d) or suspending(0x%x) state!!\n",
5111 			__FUNCTION__, dhd->pub.busstate, dhd->pub.dhd_bus_busy_state));
5112 		DHD_BUS_BUSY_CLEAR_IN_TX(&dhd->pub);
5113 #ifdef PCIE_FULL_DONGLE
5114 		/* Stop tx queues if suspend is in progress */
5115 		if (DHD_BUS_CHECK_ANY_SUSPEND_IN_PROGRESS(&dhd->pub)) {
5116 			dhd_bus_stop_queue(dhd->pub.bus);
5117 		}
5118 #endif /* PCIE_FULL_DONGLE */
5119 		dhd_os_busbusy_wake(&dhd->pub);
5120 		DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5121 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20))
5122 		return -ENODEV;
5123 #else
5124 		return NETDEV_TX_BUSY;
5125 #endif
5126 	}
5127 #else
5128 	if (DHD_BUS_CHECK_SUSPEND_OR_SUSPEND_IN_PROGRESS(&dhd->pub)) {
5129 		DHD_ERROR(("%s: bus is in suspend(%d) or suspending(0x%x) state!!\n",
5130 			__FUNCTION__, dhd->pub.busstate, dhd->pub.dhd_bus_busy_state));
5131 	}
5132 #endif
5133 
5134 	DHD_OS_WAKE_LOCK(&dhd->pub);
5135 	DHD_PERIM_LOCK_TRY(DHD_FWDER_UNIT(dhd), lock_taken);
5136 
5137 
5138 #if defined(DHD_HANG_SEND_UP_TEST)
5139 	if (dhd->pub.req_hang_type == HANG_REASON_BUS_DOWN) {
5140 		dhd->pub.busstate = DHD_BUS_DOWN;
5141 	}
5142 #endif /* DHD_HANG_SEND_UP_TEST */
5143 
5144 	/* Reject if down */
5145 	if (dhd->pub.hang_was_sent || DHD_BUS_CHECK_DOWN_OR_DOWN_IN_PROGRESS(&dhd->pub)) {
5146 		DHD_ERROR(("%s: xmit rejected pub.up=%d busstate=%d \n",
5147 			__FUNCTION__, dhd->pub.up, dhd->pub.busstate));
5148 		netif_stop_queue(net);
5149 		/* Send Event when bus down detected during data session */
5150 		if (dhd->pub.up && !dhd->pub.hang_was_sent && !DHD_BUS_CHECK_REMOVE(&dhd->pub)) {
5151 			DHD_ERROR(("%s: Event HANG sent up\n", __FUNCTION__));
5152 			dhd->pub.hang_reason = HANG_REASON_BUS_DOWN;
5153 			net_os_send_hang_message(net);
5154 		}
5155 		DHD_BUS_BUSY_CLEAR_IN_TX(&dhd->pub);
5156 		dhd_os_busbusy_wake(&dhd->pub);
5157 		DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5158 		DHD_PERIM_UNLOCK_TRY(DHD_FWDER_UNIT(dhd), lock_taken);
5159 		DHD_OS_WAKE_UNLOCK(&dhd->pub);
5160 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20))
5161 		return -ENODEV;
5162 #else
5163 		return NETDEV_TX_BUSY;
5164 #endif
5165 	}
5166 
5167 	ifp = DHD_DEV_IFP(net);
5168 	ifidx = DHD_DEV_IFIDX(net);
5169 	if (ifidx == DHD_BAD_IF) {
5170 		DHD_ERROR(("%s: bad ifidx %d\n", __FUNCTION__, ifidx));
5171 		netif_stop_queue(net);
5172 		DHD_BUS_BUSY_CLEAR_IN_TX(&dhd->pub);
5173 		dhd_os_busbusy_wake(&dhd->pub);
5174 		DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5175 		DHD_PERIM_UNLOCK_TRY(DHD_FWDER_UNIT(dhd), lock_taken);
5176 		DHD_OS_WAKE_UNLOCK(&dhd->pub);
5177 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20))
5178 		return -ENODEV;
5179 #else
5180 		return NETDEV_TX_BUSY;
5181 #endif
5182 	}
5183 
5184 	DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5185 
5186 	ASSERT(ifidx == dhd_net2idx(dhd, net));
5187 	ASSERT((ifp != NULL) && ((ifidx < DHD_MAX_IFS) && (ifp == dhd->iflist[ifidx])));
5188 
5189 	bcm_object_trace_opr(skb, BCM_OBJDBG_ADD_PKT, __FUNCTION__, __LINE__);
5190 
5191 	/* re-align socket buffer if "skb->data" is odd address */
5192 	if (((unsigned long)(skb->data)) & 0x1) {
5193 		unsigned char *data = skb->data;
5194 		uint32 length = skb->len;
5195 		PKTPUSH(dhd->pub.osh, skb, 1);
5196 		memmove(skb->data, data, length);
5197 		PKTSETLEN(dhd->pub.osh, skb, length);
5198 	}
5199 
5200 	datalen  = PKTLEN(dhd->pub.osh, skb);
5201 
5202 #ifdef HOST_TPUT_TEST
5203 	dhd_os_sdlock_txq(&dhd->pub);
5204 	dhd->pub.net_len += datalen;
5205 	dhd_os_sdunlock_txq(&dhd->pub);
5206 	if ((dhd->pub.conf->data_drop_mode == XMIT_DROP) &&
5207 			(PKTLEN(dhd->pub.osh, skb) > 500)) {
5208 		dev_kfree_skb(skb);
5209 		return NETDEV_TX_OK;
5210 	}
5211 #endif
5212 	/* Make sure there's enough room for any header */
5213 	if (skb_headroom(skb) < dhd->pub.hdrlen + htsfdlystat_sz) {
5214 		struct sk_buff *skb2;
5215 
5216 		DHD_INFO(("%s: insufficient headroom\n",
5217 		          dhd_ifname(&dhd->pub, ifidx)));
5218 		dhd->pub.tx_realloc++;
5219 
5220 		bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE, __FUNCTION__, __LINE__);
5221 		skb2 = skb_realloc_headroom(skb, dhd->pub.hdrlen + htsfdlystat_sz);
5222 
5223 		dev_kfree_skb(skb);
5224 		if ((skb = skb2) == NULL) {
5225 			DHD_ERROR(("%s: skb_realloc_headroom failed\n",
5226 			           dhd_ifname(&dhd->pub, ifidx)));
5227 			ret = -ENOMEM;
5228 			goto done;
5229 		}
5230 		bcm_object_trace_opr(skb, BCM_OBJDBG_ADD_PKT, __FUNCTION__, __LINE__);
5231 	}
5232 
5233 	/* move from dhdsdio_sendfromq(), try to orphan skb early */
5234 	if (dhd->pub.conf->orphan_move == 2)
5235 		PKTORPHAN(skb, dhd->pub.conf->tsq);
5236 	else if (dhd->pub.conf->orphan_move == 3)
5237 		skb_orphan(skb);
5238 
5239 	/* Convert to packet */
5240 	if (!(pktbuf = PKTFRMNATIVE(dhd->pub.osh, skb))) {
5241 		DHD_ERROR(("%s: PKTFRMNATIVE failed\n",
5242 		           dhd_ifname(&dhd->pub, ifidx)));
5243 		bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE, __FUNCTION__, __LINE__);
5244 		dev_kfree_skb_any(skb);
5245 		ret = -ENOMEM;
5246 		goto done;
5247 	}
5248 
5249 #if defined(WLMEDIA_HTSF)
5250 	if (htsfdlystat_sz && PKTLEN(dhd->pub.osh, pktbuf) >= ETHER_ADDR_LEN) {
5251 		uint8 *pktdata = (uint8 *)PKTDATA(dhd->pub.osh, pktbuf);
5252 		struct ether_header *eh = (struct ether_header *)pktdata;
5253 
5254 		if (!ETHER_ISMULTI(eh->ether_dhost) &&
5255 			(ntoh16(eh->ether_type) == ETHER_TYPE_IP)) {
5256 			eh->ether_type = hton16(ETHER_TYPE_BRCM_PKTDLYSTATS);
5257 		}
5258 	}
5259 #endif
5260 #ifdef DHD_WET
5261 	/* wet related packet proto manipulation should be done in DHD
5262 	   since dongle doesn't have complete payload
5263 	 */
5264 	if (WET_ENABLED(&dhd->pub) &&
5265 			(dhd_wet_send_proc(dhd->pub.wet_info, pktbuf, &pktbuf) < 0)) {
5266 		DHD_INFO(("%s:%s: wet send proc failed\n",
5267 				__FUNCTION__, dhd_ifname(&dhd->pub, ifidx)));
5268 		PKTFREE(dhd->pub.osh, pktbuf, FALSE);
5269 		ret =  -EFAULT;
5270 		goto done;
5271 	}
5272 #endif /* DHD_WET */
5273 
5274 #ifdef DHD_WMF
5275 	eh = (struct ether_header *)PKTDATA(dhd->pub.osh, pktbuf);
5276 	iph = (uint8 *)eh + ETHER_HDR_LEN;
5277 
5278 	/* WMF processing for multicast packets
5279 	 * Only IPv4 packets are handled
5280 	 */
5281 	if (ifp->wmf.wmf_enable && (ntoh16(eh->ether_type) == ETHER_TYPE_IP) &&
5282 		(IP_VER(iph) == IP_VER_4) && (ETHER_ISMULTI(eh->ether_dhost) ||
5283 		((IPV4_PROT(iph) == IP_PROT_IGMP) && dhd->pub.wmf_ucast_igmp))) {
5284 #if defined(DHD_IGMP_UCQUERY) || defined(DHD_UCAST_UPNP)
5285 		void *sdu_clone;
5286 		bool ucast_convert = FALSE;
5287 #ifdef DHD_UCAST_UPNP
5288 		uint32 dest_ip;
5289 
5290 		dest_ip = ntoh32(*((uint32 *)(iph + IPV4_DEST_IP_OFFSET)));
5291 		ucast_convert = dhd->pub.wmf_ucast_upnp && MCAST_ADDR_UPNP_SSDP(dest_ip);
5292 #endif /* DHD_UCAST_UPNP */
5293 #ifdef DHD_IGMP_UCQUERY
5294 		ucast_convert |= dhd->pub.wmf_ucast_igmp_query &&
5295 			(IPV4_PROT(iph) == IP_PROT_IGMP) &&
5296 			(*(iph + IPV4_HLEN(iph)) == IGMPV2_HOST_MEMBERSHIP_QUERY);
5297 #endif /* DHD_IGMP_UCQUERY */
5298 		if (ucast_convert) {
5299 			dhd_sta_t *sta;
5300 			unsigned long flags;
5301 			struct list_head snapshot_list;
5302 			struct list_head *wmf_ucforward_list;
5303 
5304 			ret = NETDEV_TX_OK;
5305 
5306 			/* For non BCM_GMAC3 platform we need a snapshot sta_list to
5307 			 * resolve double DHD_IF_STA_LIST_LOCK call deadlock issue.
5308 			 */
5309 			wmf_ucforward_list = DHD_IF_WMF_UCFORWARD_LOCK(dhd, ifp, &snapshot_list);
5310 
5311 			/* Convert upnp/igmp query to unicast for each assoc STA */
5312 			list_for_each_entry(sta, wmf_ucforward_list, list) {
5313 				/* Skip sending to proxy interfaces of proxySTA */
5314 				if (sta->psta_prim != NULL && !ifp->wmf_psta_disable) {
5315 					continue;
5316 				}
5317 				if ((sdu_clone = PKTDUP(dhd->pub.osh, pktbuf)) == NULL) {
5318 					ret = WMF_NOP;
5319 					break;
5320 				}
5321 				dhd_wmf_forward(ifp->wmf.wmfh, sdu_clone, 0, sta, 1);
5322 			}
5323 			DHD_IF_WMF_UCFORWARD_UNLOCK(dhd, wmf_ucforward_list);
5324 
5325 			DHD_GENERAL_LOCK(&dhd->pub, flags);
5326 			DHD_BUS_BUSY_CLEAR_IN_TX(&dhd->pub);
5327 			dhd_os_busbusy_wake(&dhd->pub);
5328 			DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5329 			DHD_PERIM_UNLOCK_TRY(DHD_FWDER_UNIT(dhd), lock_taken);
5330 			DHD_OS_WAKE_UNLOCK(&dhd->pub);
5331 
5332 			if (ret == NETDEV_TX_OK)
5333 				PKTFREE(dhd->pub.osh, pktbuf, TRUE);
5334 
5335 			return ret;
5336 		} else
5337 #endif /* defined(DHD_IGMP_UCQUERY) || defined(DHD_UCAST_UPNP) */
5338 		{
5339 			/* There will be no STA info if the packet is coming from LAN host
5340 			 * Pass as NULL
5341 			 */
5342 			ret = dhd_wmf_packets_handle(&dhd->pub, pktbuf, NULL, ifidx, 0);
5343 			switch (ret) {
5344 			case WMF_TAKEN:
5345 			case WMF_DROP:
5346 				/* Either taken by WMF or we should drop it.
5347 				 * Exiting send path
5348 				 */
5349 
5350 				DHD_GENERAL_LOCK(&dhd->pub, flags);
5351 				DHD_BUS_BUSY_CLEAR_IN_TX(&dhd->pub);
5352 				dhd_os_busbusy_wake(&dhd->pub);
5353 				DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5354 				DHD_PERIM_UNLOCK_TRY(DHD_FWDER_UNIT(dhd), lock_taken);
5355 				DHD_OS_WAKE_UNLOCK(&dhd->pub);
5356 				return NETDEV_TX_OK;
5357 			default:
5358 				/* Continue the transmit path */
5359 				break;
5360 			}
5361 		}
5362 	}
5363 #endif /* DHD_WMF */
5364 #ifdef DHD_PSTA
5365 	/* PSR related packet proto manipulation should be done in DHD
5366 	 * since dongle doesn't have complete payload
5367 	 */
5368 	if (PSR_ENABLED(&dhd->pub) && (dhd_psta_proc(&dhd->pub,
5369 		ifidx, &pktbuf, TRUE) < 0)) {
5370 			DHD_ERROR(("%s:%s: psta send proc failed\n", __FUNCTION__,
5371 				dhd_ifname(&dhd->pub, ifidx)));
5372 	}
5373 #endif /* DHD_PSTA */
5374 
5375 #ifdef DHDTCPACK_SUPPRESS
5376 	if (dhd->pub.tcpack_sup_mode == TCPACK_SUP_HOLD) {
5377 		/* If this packet has been hold or got freed, just return */
5378 		if (dhd_tcpack_hold(&dhd->pub, pktbuf, ifidx)) {
5379 			ret = 0;
5380 			goto done;
5381 		}
5382 	} else {
5383 		/* If this packet has replaced another packet and got freed, just return */
5384 		if (dhd_tcpack_suppress(&dhd->pub, pktbuf)) {
5385 			ret = 0;
5386 			goto done;
5387 		}
5388 	}
5389 #endif /* DHDTCPACK_SUPPRESS */
5390 
5391 	/*
5392 	 * If Load Balance is enabled queue the packet
5393 	 * else send directly from here.
5394 	 */
5395 #if defined(DHD_LB_TXP)
5396 	ret = dhd_lb_sendpkt(dhd, net, ifidx, pktbuf);
5397 #else
5398 	ret = __dhd_sendpkt(&dhd->pub, ifidx, pktbuf);
5399 #endif
5400 
5401 done:
5402 	if (ret) {
5403 		ifp->stats.tx_dropped++;
5404 		dhd->pub.tx_dropped++;
5405 	} else {
5406 #ifdef PROP_TXSTATUS
5407 		/* tx_packets counter can counted only when wlfc is disabled */
5408 		if (!dhd_wlfc_is_supported(&dhd->pub))
5409 #endif
5410 		{
5411 			dhd->pub.tx_packets++;
5412 			ifp->stats.tx_packets++;
5413 			ifp->stats.tx_bytes += datalen;
5414 		}
5415 	}
5416 
5417 
5418 	DHD_GENERAL_LOCK(&dhd->pub, flags);
5419 	DHD_BUS_BUSY_CLEAR_IN_TX(&dhd->pub);
5420 	dhd_os_busbusy_wake(&dhd->pub);
5421 	DHD_GENERAL_UNLOCK(&dhd->pub, flags);
5422 	DHD_PERIM_UNLOCK_TRY(DHD_FWDER_UNIT(dhd), lock_taken);
5423 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
5424 	/* Return ok: we always eat the packet */
5425 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20))
5426 	return 0;
5427 #else
5428 	return NETDEV_TX_OK;
5429 #endif
5430 }
5431 
5432 
5433 void
dhd_txflowcontrol(dhd_pub_t * dhdp,int ifidx,bool state)5434 dhd_txflowcontrol(dhd_pub_t *dhdp, int ifidx, bool state)
5435 {
5436 	struct net_device *net;
5437 	dhd_info_t *dhd = dhdp->info;
5438 	int i;
5439 
5440 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
5441 
5442 	ASSERT(dhd);
5443 
5444 #ifdef DHD_LOSSLESS_ROAMING
5445 	/* block flowcontrol during roaming */
5446 	if ((dhdp->dequeue_prec_map == 1 << PRIO_8021D_NC) && state == ON) {
5447 		return;
5448 	}
5449 #endif
5450 
5451 	if (ifidx == ALL_INTERFACES) {
5452 		/* Flow control on all active interfaces */
5453 		dhdp->txoff = state;
5454 		for (i = 0; i < DHD_MAX_IFS; i++) {
5455 			if (dhd->iflist[i]) {
5456 				net = dhd->iflist[i]->net;
5457 				if (state == ON)
5458 					netif_stop_queue(net);
5459 				else
5460 					netif_wake_queue(net);
5461 			}
5462 		}
5463 	} else {
5464 		if (dhd->iflist[ifidx]) {
5465 			net = dhd->iflist[ifidx]->net;
5466 			if (state == ON)
5467 				netif_stop_queue(net);
5468 			else
5469 				netif_wake_queue(net);
5470 		}
5471 	}
5472 }
5473 
5474 
5475 #ifdef DHD_WMF
5476 bool
dhd_is_rxthread_enabled(dhd_pub_t * dhdp)5477 dhd_is_rxthread_enabled(dhd_pub_t *dhdp)
5478 {
5479 	dhd_info_t *dhd = dhdp->info;
5480 
5481 	return dhd->rxthread_enabled;
5482 }
5483 #endif /* DHD_WMF */
5484 
5485 #ifdef DHD_MCAST_REGEN
5486 /*
5487  * Description: This function is called to do the reverse translation
5488  *
5489  * Input    eh - pointer to the ethernet header
5490  */
5491 int32
dhd_mcast_reverse_translation(struct ether_header * eh)5492 dhd_mcast_reverse_translation(struct ether_header *eh)
5493 {
5494 	uint8 *iph;
5495 	uint32 dest_ip;
5496 
5497 	iph = (uint8 *)eh + ETHER_HDR_LEN;
5498 	dest_ip = ntoh32(*((uint32 *)(iph + IPV4_DEST_IP_OFFSET)));
5499 
5500 	/* Only IP packets are handled */
5501 	if (eh->ether_type != hton16(ETHER_TYPE_IP))
5502 		return BCME_ERROR;
5503 
5504 	/* Non-IPv4 multicast packets are not handled */
5505 	if (IP_VER(iph) != IP_VER_4)
5506 		return BCME_ERROR;
5507 
5508 	/*
5509 	 * The packet has a multicast IP and unicast MAC. That means
5510 	 * we have to do the reverse translation
5511 	 */
5512 	if (IPV4_ISMULTI(dest_ip) && !ETHER_ISMULTI(&eh->ether_dhost)) {
5513 		ETHER_FILL_MCAST_ADDR_FROM_IP(eh->ether_dhost, dest_ip);
5514 		return BCME_OK;
5515 	}
5516 
5517 	return BCME_ERROR;
5518 }
5519 #endif /* MCAST_REGEN */
5520 
5521 #ifdef SHOW_LOGTRACE
5522 static int
dhd_event_logtrace_pkt_process(dhd_pub_t * dhdp,struct sk_buff * skb)5523 dhd_event_logtrace_pkt_process(dhd_pub_t *dhdp, struct sk_buff * skb)
5524 {
5525 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
5526 	int ret = BCME_OK;
5527 	uint datalen;
5528 	bcm_event_msg_u_t evu;
5529 	void *data = NULL;
5530 	void *pktdata = NULL;
5531 	bcm_event_t *pvt_data;
5532 	uint pktlen;
5533 
5534 	DHD_TRACE(("%s:Enter\n", __FUNCTION__));
5535 
5536 	/* In dhd_rx_frame, header is stripped using skb_pull
5537 	 * of size ETH_HLEN, so adjust pktlen accordingly
5538 	 */
5539 	pktlen = skb->len + ETH_HLEN;
5540 
5541 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
5542 	pktdata = (void *)skb_mac_header(skb);
5543 #else
5544 	pktdata = (void *)skb->mac.raw;
5545 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) */
5546 
5547 	ret = wl_host_event_get_data(pktdata, pktlen, &evu);
5548 
5549 	if (ret != BCME_OK) {
5550 		DHD_ERROR(("%s: wl_host_event_get_data err = %d\n",
5551 			__FUNCTION__, ret));
5552 		goto exit;
5553 	}
5554 
5555 	datalen = ntoh32(evu.event.datalen);
5556 
5557 	pvt_data = (bcm_event_t *)pktdata;
5558 	data = &pvt_data[1];
5559 
5560 	dhd_dbg_trace_evnt_handler(dhdp, data, &dhd->event_data, datalen);
5561 
5562 exit:
5563 	return ret;
5564 }
5565 
5566 static void
dhd_event_logtrace_process(struct work_struct * work)5567 dhd_event_logtrace_process(struct work_struct * work)
5568 {
5569 /* Ignore compiler warnings due to -Werror=cast-qual */
5570 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
5571 #pragma GCC diagnostic push
5572 #pragma GCC diagnostic ignored "-Wcast-qual"
5573 #endif
5574 	struct dhd_info *dhd =
5575 		container_of(work, struct dhd_info, event_log_dispatcher_work);
5576 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
5577 #pragma GCC diagnostic pop
5578 #endif
5579 
5580 	dhd_pub_t *dhdp;
5581 	struct sk_buff *skb;
5582 
5583 	if (!dhd) {
5584 		DHD_ERROR(("%s: dhd info not available \n", __FUNCTION__));
5585 		return;
5586 	}
5587 
5588 	dhdp = &dhd->pub;
5589 
5590 	if (!dhdp) {
5591 		DHD_ERROR(("%s: dhd pub is null \n", __FUNCTION__));
5592 		return;
5593 	}
5594 
5595 	DHD_TRACE(("%s:Enter\n", __FUNCTION__));
5596 
5597 	/* Run while(1) loop till all skbs are dequeued */
5598 	while ((skb = skb_dequeue(&dhd->evt_trace_queue)) != NULL) {
5599 #ifdef PCIE_FULL_DONGLE
5600 		int ifid;
5601 		ifid = DHD_PKTTAG_IFID((dhd_pkttag_fr_t *)PKTTAG(skb));
5602 		if (ifid == DHD_EVENT_IF) {
5603 			dhd_event_logtrace_infobuf_pkt_process(dhdp, skb, &dhd->event_data);
5604 			/* For sending skb to network layer, convert it to Native PKT
5605 			 * after that assign skb->dev with Primary interface n/w device
5606 			 * as for infobuf events, we are sending special DHD_EVENT_IF
5607 			 */
5608 #ifdef DHD_USE_STATIC_CTRLBUF
5609 			PKTFREE_STATIC(dhdp->osh, skb, FALSE);
5610 #else
5611 			PKTFREE(dhdp->osh, skb, FALSE);
5612 #endif /* DHD_USE_STATIC_CTRLBUF */
5613 			continue;
5614 		}
5615 		else {
5616 			dhd_event_logtrace_pkt_process(dhdp, skb);
5617 		}
5618 #else
5619 		dhd_event_logtrace_pkt_process(dhdp, skb);
5620 #endif /* PCIE_FULL_DONGLE */
5621 
5622 		/* Free skb buffer here if DHD_DONOT_FORWARD_BCMEVENT_AS_NETWORK_PKT
5623 		* macro is defined the Info Ring event and WLC_E_TRACE event is freed in DHD
5624 		* else it is always sent up to network layers.
5625 		*/
5626 #ifdef DHD_DONOT_FORWARD_BCMEVENT_AS_NETWORK_PKT
5627 #ifdef DHD_USE_STATIC_CTRLBUF
5628 		PKTFREE_STATIC(dhdp->osh, skb, FALSE);
5629 #else
5630 		PKTFREE(dhdp->osh, skb, FALSE);
5631 #endif /* DHD_USE_STATIC_CTRLBUF */
5632 #else /* !DHD_DONOT_FORWARD_BCMEVENT_AS_NETWORK_PKT */
5633 		/* Do not call netif_recieve_skb as this workqueue scheduler is not from NAPI
5634 		 * Also as we are not in INTR context, do not call netif_rx, instead call
5635 		 * netif_rx_ni (for kerenl >= 2.6) which  does netif_rx, disables irq, raise
5636 		 * NET_IF_RX softirq and enables interrupts back
5637 		 */
5638 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
5639 		netif_rx_ni(skb);
5640 #else
5641 		{
5642 			ulong flags;
5643 			netif_rx(skb);
5644 			local_irq_save(flags);
5645 			RAISE_RX_SOFTIRQ();
5646 			local_irq_restore(flags);
5647 		}
5648 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) */
5649 #endif /* DHD_DONOT_FORWARD_BCMEVENT_AS_NETWORK_PKT */
5650 	}
5651 }
5652 
5653 void
dhd_event_logtrace_enqueue(dhd_pub_t * dhdp,int ifidx,void * pktbuf)5654 dhd_event_logtrace_enqueue(dhd_pub_t *dhdp, int ifidx, void *pktbuf)
5655 {
5656 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
5657 
5658 #ifdef PCIE_FULL_DONGLE
5659 	/* Add ifidx in the PKTTAG */
5660 	DHD_PKTTAG_SET_IFID((dhd_pkttag_fr_t *)PKTTAG(pktbuf), ifidx);
5661 #endif /* PCIE_FULL_DONGLE */
5662 	skb_queue_tail(&dhd->evt_trace_queue, pktbuf);
5663 
5664 	schedule_work(&dhd->event_log_dispatcher_work);
5665 }
5666 
5667 void
dhd_event_logtrace_flush_queue(dhd_pub_t * dhdp)5668 dhd_event_logtrace_flush_queue(dhd_pub_t *dhdp)
5669 {
5670 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
5671 	struct sk_buff *skb;
5672 
5673 	while ((skb = skb_dequeue(&dhd->evt_trace_queue)) != NULL) {
5674 #ifdef DHD_USE_STATIC_CTRLBUF
5675 		PKTFREE_STATIC(dhdp->osh, skb, FALSE);
5676 #else
5677 		PKTFREE(dhdp->osh, skb, FALSE);
5678 #endif /* DHD_USE_STATIC_CTRLBUF */
5679 	}
5680 }
5681 #endif /* SHOW_LOGTRACE */
5682 
5683 /** Called when a frame is received by the dongle on interface 'ifidx' */
5684 void
dhd_rx_frame(dhd_pub_t * dhdp,int ifidx,void * pktbuf,int numpkt,uint8 chan)5685 dhd_rx_frame(dhd_pub_t *dhdp, int ifidx, void *pktbuf, int numpkt, uint8 chan)
5686 {
5687 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
5688 	struct sk_buff *skb;
5689 	uchar *eth;
5690 	uint len;
5691 	void *data, *pnext = NULL;
5692 	int i;
5693 	dhd_if_t *ifp;
5694 	wl_event_msg_t event;
5695 	int tout_rx = 0;
5696 	int tout_ctrl = 0;
5697 	void *skbhead = NULL;
5698 	void *skbprev = NULL;
5699 	uint16 protocol;
5700 	unsigned char *dump_data;
5701 #ifdef DHD_MCAST_REGEN
5702 	uint8 interface_role;
5703 	if_flow_lkup_t *if_flow_lkup;
5704 	unsigned long flags;
5705 #endif
5706 #ifdef DHD_WAKE_STATUS
5707 	int pkt_wake = 0;
5708 	wake_counts_t *wcp = NULL;
5709 #endif /* DHD_WAKE_STATUS */
5710 
5711 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
5712 
5713 	for (i = 0; pktbuf && i < numpkt; i++, pktbuf = pnext) {
5714 		struct ether_header *eh;
5715 
5716 		pnext = PKTNEXT(dhdp->osh, pktbuf);
5717 		PKTSETNEXT(dhdp->osh, pktbuf, NULL);
5718 
5719 		/* info ring "debug" data, which is not a 802.3 frame, is sent/hacked with a
5720 		 * special ifidx of DHD_EVENT_IF.  This is just internal to dhd to get the data from
5721 		 * dhd_msgbuf.c:dhd_prot_infobuf_cmplt_process() to here (dhd_rx_frame).
5722 		 */
5723 		if (ifidx == DHD_EVENT_IF) {
5724 			/* Event msg printing is called from dhd_rx_frame which is in Tasklet
5725 			 * context in case of PCIe FD, in case of other bus this will be from
5726 			 * DPC context. If we get bunch of events from Dongle then printing all
5727 			 * of them from Tasklet/DPC context that too in data path is costly.
5728 			 * Also in the new Dongle SW(4359, 4355 onwards) console prints too come as
5729 			 * events with type WLC_E_TRACE.
5730 			 * We'll print this console logs from the WorkQueue context by enqueing SKB
5731 			 * here and Dequeuing will be done in WorkQueue and will be freed only if
5732 			 * DHD_DONOT_FORWARD_BCMEVENT_AS_NETWORK_PKT is defined
5733 			 */
5734 #ifdef SHOW_LOGTRACE
5735 			dhd_event_logtrace_enqueue(dhdp, ifidx, pktbuf);
5736 #else /* !SHOW_LOGTRACE */
5737 		/* If SHOW_LOGTRACE not defined and ifidx is DHD_EVENT_IF,
5738 		 * free the PKT here itself
5739 		 */
5740 #ifdef DHD_USE_STATIC_CTRLBUF
5741 		PKTFREE_STATIC(dhdp->osh, pktbuf, FALSE);
5742 #else
5743 		PKTFREE(dhdp->osh, pktbuf, FALSE);
5744 #endif /* DHD_USE_STATIC_CTRLBUF */
5745 #endif /* SHOW_LOGTRACE */
5746 			continue;
5747 		}
5748 #ifdef DHD_WAKE_STATUS
5749 #ifdef BCMDBUS
5750 		wcp = NULL;
5751 #else
5752 		pkt_wake = dhd_bus_get_bus_wake(dhdp);
5753 		wcp = dhd_bus_get_wakecount(dhdp);
5754 #endif /* BCMDBUS */
5755 		if (wcp == NULL) {
5756 			/* If wakeinfo count buffer is null do not update wake count values */
5757 			pkt_wake = 0;
5758 		}
5759 #endif /* DHD_WAKE_STATUS */
5760 
5761 		ifp = dhd->iflist[ifidx];
5762 		if (ifp == NULL) {
5763 			DHD_ERROR(("%s: ifp is NULL. drop packet\n",
5764 				__FUNCTION__));
5765 			PKTCFREE(dhdp->osh, pktbuf, FALSE);
5766 			continue;
5767 		}
5768 
5769 		eh = (struct ether_header *)PKTDATA(dhdp->osh, pktbuf);
5770 
5771 		/* Dropping only data packets before registering net device to avoid kernel panic */
5772 #ifndef PROP_TXSTATUS_VSDB
5773 		if ((!ifp->net || ifp->net->reg_state != NETREG_REGISTERED) &&
5774 			(ntoh16(eh->ether_type) != ETHER_TYPE_BRCM))
5775 #else
5776 		if ((!ifp->net || ifp->net->reg_state != NETREG_REGISTERED || !dhd->pub.up) &&
5777 			(ntoh16(eh->ether_type) != ETHER_TYPE_BRCM))
5778 #endif /* PROP_TXSTATUS_VSDB */
5779 		{
5780 			DHD_PRINT("%s: net device is NOT registered yet. drop packet\n",
5781 			__FUNCTION__);
5782 			PKTCFREE(dhdp->osh, pktbuf, FALSE);
5783 			continue;
5784 		}
5785 
5786 #ifdef PROP_TXSTATUS
5787 		if (dhd_wlfc_is_header_only_pkt(dhdp, pktbuf)) {
5788 			/* WLFC may send header only packet when
5789 			there is an urgent message but no packet to
5790 			piggy-back on
5791 			*/
5792 			PKTCFREE(dhdp->osh, pktbuf, FALSE);
5793 			continue;
5794 		}
5795 #endif
5796 #ifdef DHD_L2_FILTER
5797 		/* If block_ping is enabled drop the ping packet */
5798 		if (ifp->block_ping) {
5799 			if (bcm_l2_filter_block_ping(dhdp->osh, pktbuf) == BCME_OK) {
5800 				PKTCFREE(dhdp->osh, pktbuf, FALSE);
5801 				continue;
5802 			}
5803 		}
5804 		if (ifp->grat_arp && DHD_IF_ROLE_STA(dhdp, ifidx)) {
5805 		    if (bcm_l2_filter_gratuitous_arp(dhdp->osh, pktbuf) == BCME_OK) {
5806 				PKTCFREE(dhdp->osh, pktbuf, FALSE);
5807 				continue;
5808 		    }
5809 		}
5810 		if (ifp->parp_enable && DHD_IF_ROLE_AP(dhdp, ifidx)) {
5811 			int ret = dhd_l2_filter_pkt_handle(dhdp, ifidx, pktbuf, FALSE);
5812 
5813 			/* Drop the packets if l2 filter has processed it already
5814 			 * otherwise continue with the normal path
5815 			 */
5816 			if (ret == BCME_OK) {
5817 				PKTCFREE(dhdp->osh, pktbuf, TRUE);
5818 				continue;
5819 			}
5820 		}
5821 #endif /* DHD_L2_FILTER */
5822 
5823 #ifdef DHD_MCAST_REGEN
5824 		DHD_FLOWID_LOCK(dhdp->flowid_lock, flags);
5825 		if_flow_lkup = (if_flow_lkup_t *)dhdp->if_flow_lkup;
5826 		ASSERT(if_flow_lkup);
5827 
5828 		interface_role = if_flow_lkup[ifidx].role;
5829 		DHD_FLOWID_UNLOCK(dhdp->flowid_lock, flags);
5830 
5831 		if (ifp->mcast_regen_bss_enable && (interface_role != WLC_E_IF_ROLE_WDS) &&
5832 				!DHD_IF_ROLE_AP(dhdp, ifidx) &&
5833 				ETHER_ISUCAST(eh->ether_dhost)) {
5834 			if (dhd_mcast_reverse_translation(eh) ==  BCME_OK) {
5835 #ifdef DHD_PSTA
5836 				/* Change bsscfg to primary bsscfg for unicast-multicast packets */
5837 				if ((dhd_get_psta_mode(dhdp) == DHD_MODE_PSTA) ||
5838 						(dhd_get_psta_mode(dhdp) == DHD_MODE_PSR)) {
5839 					if (ifidx != 0) {
5840 						/* Let the primary in PSTA interface handle this
5841 						 * frame after unicast to Multicast conversion
5842 						 */
5843 						ifp = dhd_get_ifp(dhdp, 0);
5844 						ASSERT(ifp);
5845 					}
5846 				}
5847 			}
5848 #endif /* PSTA */
5849 		}
5850 #endif /* MCAST_REGEN */
5851 
5852 #ifdef DHD_WMF
5853 		/* WMF processing for multicast packets */
5854 		if (ifp->wmf.wmf_enable && (ETHER_ISMULTI(eh->ether_dhost))) {
5855 			dhd_sta_t *sta;
5856 			int ret;
5857 
5858 			sta = dhd_find_sta(dhdp, ifidx, (void *)eh->ether_shost);
5859 			ret = dhd_wmf_packets_handle(dhdp, pktbuf, sta, ifidx, 1);
5860 			switch (ret) {
5861 				case WMF_TAKEN:
5862 					/* The packet is taken by WMF. Continue to next iteration */
5863 					continue;
5864 				case WMF_DROP:
5865 					/* Packet DROP decision by WMF. Toss it */
5866 					DHD_ERROR(("%s: WMF decides to drop packet\n",
5867 						__FUNCTION__));
5868 					PKTCFREE(dhdp->osh, pktbuf, FALSE);
5869 					continue;
5870 				default:
5871 					/* Continue the transmit path */
5872 					break;
5873 			}
5874 		}
5875 #endif /* DHD_WMF */
5876 
5877 #ifdef DHDTCPACK_SUPPRESS
5878 		dhd_tcpdata_info_get(dhdp, pktbuf);
5879 #endif
5880 		skb = PKTTONATIVE(dhdp->osh, pktbuf);
5881 
5882 		ASSERT(ifp);
5883 		skb->dev = ifp->net;
5884 #ifdef DHD_WET
5885 		/* wet related packet proto manipulation should be done in DHD
5886 		 * since dongle doesn't have complete payload
5887 		 */
5888 		if (WET_ENABLED(&dhd->pub) && (dhd_wet_recv_proc(dhd->pub.wet_info,
5889 				pktbuf) < 0)) {
5890 			DHD_INFO(("%s:%s: wet recv proc failed\n",
5891 				__FUNCTION__, dhd_ifname(dhdp, ifidx)));
5892 		}
5893 #endif /* DHD_WET */
5894 
5895 #ifdef DHD_PSTA
5896 		if (PSR_ENABLED(dhdp) && (dhd_psta_proc(dhdp, ifidx, &pktbuf, FALSE) < 0)) {
5897 				DHD_ERROR(("%s:%s: psta recv proc failed\n", __FUNCTION__,
5898 					dhd_ifname(dhdp, ifidx)));
5899 		}
5900 #endif /* DHD_PSTA */
5901 
5902 #ifdef PCIE_FULL_DONGLE
5903 		if ((DHD_IF_ROLE_AP(dhdp, ifidx) || DHD_IF_ROLE_P2PGO(dhdp, ifidx)) &&
5904 			(!ifp->ap_isolate)) {
5905 			eh = (struct ether_header *)PKTDATA(dhdp->osh, pktbuf);
5906 			if (ETHER_ISUCAST(eh->ether_dhost)) {
5907 				if (dhd_find_sta(dhdp, ifidx, (void *)eh->ether_dhost)) {
5908 					dhd_sendpkt(dhdp, ifidx, pktbuf);
5909 					continue;
5910 				}
5911 			} else {
5912 				void *npktbuf = PKTDUP(dhdp->osh, pktbuf);
5913 				if (npktbuf)
5914 					dhd_sendpkt(dhdp, ifidx, npktbuf);
5915 			}
5916 		}
5917 #endif /* PCIE_FULL_DONGLE */
5918 
5919 		/* Get the protocol, maintain skb around eth_type_trans()
5920 		 * The main reason for this hack is for the limitation of
5921 		 * Linux 2.4 where 'eth_type_trans' uses the 'net->hard_header_len'
5922 		 * to perform skb_pull inside vs ETH_HLEN. Since to avoid
5923 		 * coping of the packet coming from the network stack to add
5924 		 * BDC, Hardware header etc, during network interface registration
5925 		 * we set the 'net->hard_header_len' to ETH_HLEN + extra space required
5926 		 * for BDC, Hardware header etc. and not just the ETH_HLEN
5927 		 */
5928 		eth = skb->data;
5929 		len = skb->len;
5930 
5931 		dump_data = skb->data;
5932 
5933 		protocol = (skb->data[12] << 8) | skb->data[13];
5934 		if (protocol == ETHER_TYPE_802_1X) {
5935 			DBG_EVENT_LOG(dhdp, WIFI_EVENT_DRIVER_EAPOL_FRAME_RECEIVED);
5936 			dhd_dump_eapol_4way_message(dhdp, ifidx, dump_data, FALSE);
5937 		}
5938 
5939 		if (protocol != ETHER_TYPE_BRCM && protocol == ETHER_TYPE_IP) {
5940 #ifdef DHD_DHCP_DUMP
5941 			dhd_dhcp_dump(dhd_ifname(dhdp, ifidx), dump_data, FALSE);
5942 #endif /* DHD_DHCP_DUMP */
5943 #ifdef DHD_ICMP_DUMP
5944 			dhd_icmp_dump(dhd_ifname(dhdp, ifidx), dump_data, FALSE);
5945 #endif /* DHD_ICMP_DUMP */
5946 			dhd_tcp_dump(dhd_ifname(dhdp, ifidx), dump_data, FALSE);
5947 		}
5948 #ifdef DHD_ARP_DUMP
5949 		if (ntoh16(eh->ether_type) == ETHER_TYPE_ARP) {
5950 			dhd_arp_dump(dhd_ifname(dhdp, ifidx), dump_data, FALSE);
5951 		}
5952 #endif /* DHD_ARP_DUMP */
5953 #ifdef DHD_RX_DUMP
5954 		dhd_trx_dump(dhd_idx2net(dhdp, ifidx), dump_data, skb->len, FALSE);
5955 #endif /* DHD_RX_DUMP */
5956 #if defined(DHD_WAKE_STATUS) && defined(DHD_WAKEPKT_DUMP)
5957 		if (pkt_wake) {
5958 			prhex("[wakepkt_dump]", (char*)dump_data, MIN(len, 32));
5959 		}
5960 #endif /* DHD_WAKE_STATUS && DHD_WAKEPKT_DUMP */
5961 
5962 		skb->protocol = eth_type_trans(skb, skb->dev);
5963 
5964 		if (skb->pkt_type == PACKET_MULTICAST) {
5965 			dhd->pub.rx_multicast++;
5966 			ifp->stats.multicast++;
5967 		}
5968 
5969 		skb->data = eth;
5970 		skb->len = len;
5971 
5972 #ifdef WLMEDIA_HTSF
5973 		dhd_htsf_addrxts(dhdp, pktbuf);
5974 #endif
5975 #ifdef DBG_PKT_MON
5976 		DHD_DBG_PKT_MON_RX(dhdp, skb);
5977 #endif /* DBG_PKT_MON */
5978 #ifdef DHD_PKT_LOGGING
5979 		DHD_PKTLOG_RX(dhdp, skb);
5980 #endif /* DHD_PKT_LOGGING */
5981 		/* Strip header, count, deliver upward */
5982 		skb_pull(skb, ETH_HLEN);
5983 
5984 		/* Process special event packets and then discard them */
5985 		memset(&event, 0, sizeof(event));
5986 
5987 		if (ntoh16(skb->protocol) == ETHER_TYPE_BRCM) {
5988 			bcm_event_msg_u_t evu;
5989 			int ret_event;
5990 			int event_type;
5991 
5992 			ret_event = wl_host_event_get_data(
5993 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
5994 			skb_mac_header(skb),
5995 #else
5996 			skb->mac.raw,
5997 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) */
5998 			len, &evu);
5999 
6000 			if (ret_event != BCME_OK) {
6001 				DHD_ERROR(("%s: wl_host_event_get_data err = %d\n",
6002 					__FUNCTION__, ret_event));
6003 #ifdef DHD_USE_STATIC_CTRLBUF
6004 				PKTFREE_STATIC(dhdp->osh, pktbuf, FALSE);
6005 #else
6006 				PKTFREE(dhdp->osh, pktbuf, FALSE);
6007 #endif
6008 				continue;
6009 			}
6010 
6011 			memcpy(&event, &evu.event, sizeof(wl_event_msg_t));
6012 			event_type = ntoh32_ua((void *)&event.event_type);
6013 #ifdef SHOW_LOGTRACE
6014 			/* Event msg printing is called from dhd_rx_frame which is in Tasklet
6015 			 * context in case of PCIe FD, in case of other bus this will be from
6016 			 * DPC context. If we get bunch of events from Dongle then printing all
6017 			 * of them from Tasklet/DPC context that too in data path is costly.
6018 			 * Also in the new Dongle SW(4359, 4355 onwards) console prints too come as
6019 			 * events with type WLC_E_TRACE.
6020 			 * We'll print this console logs from the WorkQueue context by enqueing SKB
6021 			 * here and Dequeuing will be done in WorkQueue and will be freed only if
6022 			 * DHD_DONOT_FORWARD_BCMEVENT_AS_NETWORK_PKT is defined
6023 			 */
6024 			if (event_type == WLC_E_TRACE) {
6025 				DHD_EVENT(("%s: WLC_E_TRACE\n", __FUNCTION__));
6026 				dhd_event_logtrace_enqueue(dhdp, ifidx, pktbuf);
6027 				continue;
6028 			}
6029 #endif /* SHOW_LOGTRACE */
6030 
6031 			ret_event = dhd_wl_host_event(dhd, ifidx,
6032 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
6033 			skb_mac_header(skb),
6034 #else
6035 			skb->mac.raw,
6036 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22) */
6037 			len, &event, &data);
6038 
6039 			wl_event_to_host_order(&event);
6040 			if (!tout_ctrl)
6041 				tout_ctrl = DHD_PACKET_TIMEOUT_MS;
6042 
6043 #if defined(PNO_SUPPORT)
6044 			if (event_type == WLC_E_PFN_NET_FOUND) {
6045 				/* enforce custom wake lock to garantee that Kernel not suspended */
6046 				tout_ctrl = CUSTOM_PNO_EVENT_LOCK_xTIME * DHD_PACKET_TIMEOUT_MS;
6047 			}
6048 #endif /* PNO_SUPPORT */
6049 			if (numpkt != 1) {
6050 				DHD_TRACE(("%s: Got BRCM event packet in a chained packet.\n",
6051 				__FUNCTION__));
6052 			}
6053 
6054 #ifdef DHD_WAKE_STATUS
6055 			if (unlikely(pkt_wake)) {
6056 #ifdef DHD_WAKE_EVENT_STATUS
6057 				if (event.event_type < WLC_E_LAST) {
6058 					wcp->rc_event[event.event_type]++;
6059 					wcp->rcwake++;
6060 					pkt_wake = 0;
6061 				}
6062 #endif /* DHD_WAKE_EVENT_STATUS */
6063 			}
6064 #endif /* DHD_WAKE_STATUS */
6065 
6066 			/* For delete virtual interface event, wl_host_event returns positive
6067 			 * i/f index, do not proceed. just free the pkt.
6068 			 */
6069 			if ((event_type == WLC_E_IF) && (ret_event > 0)) {
6070 				DHD_ERROR(("%s: interface is deleted. Free event packet\n",
6071 				__FUNCTION__));
6072 #ifdef DHD_USE_STATIC_CTRLBUF
6073 				PKTFREE_STATIC(dhdp->osh, pktbuf, FALSE);
6074 #else
6075 				PKTFREE(dhdp->osh, pktbuf, FALSE);
6076 #endif
6077 				continue;
6078 			}
6079 
6080 #if defined(DHD_DONOT_FORWARD_BCMEVENT_AS_NETWORK_PKT) && !defined(SENDPROB)
6081 #ifdef DHD_USE_STATIC_CTRLBUF
6082 			PKTFREE_STATIC(dhdp->osh, pktbuf, FALSE);
6083 #else
6084 			PKTFREE(dhdp->osh, pktbuf, FALSE);
6085 #endif /* DHD_USE_STATIC_CTRLBUF */
6086 			continue;
6087 #else
6088 #ifdef SENDPROB
6089 			if (!dhdp->recv_probereq || (event.event_type != WLC_E_PROBREQ_MSG)) {
6090 #ifdef DHD_USE_STATIC_CTRLBUF
6091 				PKTFREE_STATIC(dhdp->osh, pktbuf, FALSE);
6092 #else
6093 				PKTFREE(dhdp->osh, pktbuf, FALSE);
6094 #endif /* DHD_USE_STATIC_CTRLBUF */
6095 				continue;
6096 			}
6097 #endif
6098 			/*
6099 			 * For the event packets, there is a possibility
6100 			 * of ifidx getting modifed.Thus update the ifp
6101 			 * once again.
6102 			 */
6103 			ASSERT(ifidx < DHD_MAX_IFS && dhd->iflist[ifidx]);
6104 			ifp = dhd->iflist[ifidx];
6105 #ifndef PROP_TXSTATUS_VSDB
6106 			if (!(ifp && ifp->net && (ifp->net->reg_state == NETREG_REGISTERED)))
6107 #else
6108 			if (!(ifp && ifp->net && (ifp->net->reg_state == NETREG_REGISTERED) &&
6109 				dhd->pub.up))
6110 #endif /* PROP_TXSTATUS_VSDB */
6111 			{
6112 				DHD_PRINT("%s: net device is NOT registered. drop event packet\n",
6113 				__FUNCTION__);
6114 #ifdef DHD_USE_STATIC_CTRLBUF
6115 				PKTFREE_STATIC(dhdp->osh, pktbuf, FALSE);
6116 #else
6117 				PKTFREE(dhdp->osh, pktbuf, FALSE);
6118 #endif
6119 				continue;
6120 			}
6121 #endif /* DHD_DONOT_FORWARD_BCMEVENT_AS_NETWORK_PKT */
6122 		} else {
6123 			tout_rx = DHD_PACKET_TIMEOUT_MS;
6124 
6125 #ifdef PROP_TXSTATUS
6126 			dhd_wlfc_save_rxpath_ac_time(dhdp, (uint8)PKTPRIO(skb));
6127 #endif /* PROP_TXSTATUS */
6128 
6129 #ifdef DHD_WAKE_STATUS
6130 			if (unlikely(pkt_wake)) {
6131 				wcp->rxwake++;
6132 #ifdef DHD_WAKE_RX_STATUS
6133 #define ETHER_ICMP6_HEADER	20
6134 #define ETHER_IPV6_SADDR (ETHER_ICMP6_HEADER + 2)
6135 #define ETHER_IPV6_DAADR (ETHER_IPV6_SADDR + IPV6_ADDR_LEN)
6136 #define ETHER_ICMPV6_TYPE (ETHER_IPV6_DAADR + IPV6_ADDR_LEN)
6137 
6138 				if (ntoh16(skb->protocol) == ETHER_TYPE_ARP) /* ARP */
6139 					wcp->rx_arp++;
6140 				if (dump_data[0] == 0xFF) { /* Broadcast */
6141 					wcp->rx_bcast++;
6142 				} else if (dump_data[0] & 0x01) { /* Multicast */
6143 					wcp->rx_mcast++;
6144 					if (ntoh16(skb->protocol) == ETHER_TYPE_IPV6) {
6145 					    wcp->rx_multi_ipv6++;
6146 					    if ((skb->len > ETHER_ICMP6_HEADER) &&
6147 					        (dump_data[ETHER_ICMP6_HEADER] == IPPROTO_ICMPV6)) {
6148 					        wcp->rx_icmpv6++;
6149 					        if (skb->len > ETHER_ICMPV6_TYPE) {
6150 					            switch (dump_data[ETHER_ICMPV6_TYPE]) {
6151 					            case NDISC_ROUTER_ADVERTISEMENT:
6152 					                wcp->rx_icmpv6_ra++;
6153 					                break;
6154 					            case NDISC_NEIGHBOUR_ADVERTISEMENT:
6155 					                wcp->rx_icmpv6_na++;
6156 					                break;
6157 					            case NDISC_NEIGHBOUR_SOLICITATION:
6158 					                wcp->rx_icmpv6_ns++;
6159 					                break;
6160 					            }
6161 					        }
6162 					    }
6163 					} else if (dump_data[2] == 0x5E) {
6164 						wcp->rx_multi_ipv4++;
6165 					} else {
6166 						wcp->rx_multi_other++;
6167 					}
6168 				} else { /* Unicast */
6169 					wcp->rx_ucast++;
6170 				}
6171 #undef ETHER_ICMP6_HEADER
6172 #undef ETHER_IPV6_SADDR
6173 #undef ETHER_IPV6_DAADR
6174 #undef ETHER_ICMPV6_TYPE
6175 #endif /* DHD_WAKE_RX_STATUS */
6176 				pkt_wake = 0;
6177 			}
6178 #endif /* DHD_WAKE_STATUS */
6179 		}
6180 
6181 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
6182 		if (ifp->net)
6183 			ifp->net->last_rx = jiffies;
6184 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) */
6185 
6186 		if (ntoh16(skb->protocol) != ETHER_TYPE_BRCM) {
6187 			dhdp->dstats.rx_bytes += skb->len;
6188 			dhdp->rx_packets++; /* Local count */
6189 			ifp->stats.rx_bytes += skb->len;
6190 			ifp->stats.rx_packets++;
6191 		}
6192 
6193 		if (in_interrupt()) {
6194 			bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE,
6195 				__FUNCTION__, __LINE__);
6196 			DHD_PERIM_UNLOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
6197 #if defined(DHD_LB_RXP)
6198 			netif_receive_skb(skb);
6199 #else /* !defined(DHD_LB_RXP) */
6200 			netif_rx(skb);
6201 #endif /* !defined(DHD_LB_RXP) */
6202 			DHD_PERIM_LOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
6203 		} else {
6204 			if (dhd->rxthread_enabled) {
6205 				if (!skbhead)
6206 					skbhead = skb;
6207 				else
6208 					PKTSETNEXT(dhdp->osh, skbprev, skb);
6209 				skbprev = skb;
6210 			} else {
6211 
6212 				/* If the receive is not processed inside an ISR,
6213 				 * the softirqd must be woken explicitly to service
6214 				 * the NET_RX_SOFTIRQ.	In 2.6 kernels, this is handled
6215 				 * by netif_rx_ni(), but in earlier kernels, we need
6216 				 * to do it manually.
6217 				 */
6218 				bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE,
6219 					__FUNCTION__, __LINE__);
6220 
6221 #if defined(DHD_LB_RXP)
6222 				DHD_PERIM_UNLOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
6223 				netif_receive_skb(skb);
6224 				DHD_PERIM_LOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
6225 #else /* !defined(DHD_LB_RXP) */
6226 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
6227 				DHD_PERIM_UNLOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
6228 				netif_rx_ni(skb);
6229 				DHD_PERIM_LOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
6230 #else
6231 				ulong flags;
6232 				DHD_PERIM_UNLOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
6233 				netif_rx(skb);
6234 				DHD_PERIM_LOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
6235 				local_irq_save(flags);
6236 				RAISE_RX_SOFTIRQ();
6237 				local_irq_restore(flags);
6238 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) */
6239 #endif /* !defined(DHD_LB_RXP) */
6240 			}
6241 		}
6242 	}
6243 
6244 	if (dhd->rxthread_enabled && skbhead)
6245 		dhd_sched_rxf(dhdp, skbhead);
6246 
6247 	DHD_OS_WAKE_LOCK_RX_TIMEOUT_ENABLE(dhdp, tout_rx);
6248 	DHD_OS_WAKE_LOCK_CTRL_TIMEOUT_ENABLE(dhdp, tout_ctrl);
6249 }
6250 
6251 void
dhd_event(struct dhd_info * dhd,char * evpkt,int evlen,int ifidx)6252 dhd_event(struct dhd_info *dhd, char *evpkt, int evlen, int ifidx)
6253 {
6254 	/* Linux version has nothing to do */
6255 	return;
6256 }
6257 
6258 void
dhd_txcomplete(dhd_pub_t * dhdp,void * txp,bool success)6259 dhd_txcomplete(dhd_pub_t *dhdp, void *txp, bool success)
6260 {
6261 	dhd_info_t *dhd = (dhd_info_t *)(dhdp->info);
6262 	struct ether_header *eh;
6263 	uint16 type;
6264 
6265 	dhd_prot_hdrpull(dhdp, NULL, txp, NULL, NULL);
6266 
6267 
6268 	eh = (struct ether_header *)PKTDATA(dhdp->osh, txp);
6269 	type  = ntoh16(eh->ether_type);
6270 
6271 	if ((type == ETHER_TYPE_802_1X) && (dhd_get_pend_8021x_cnt(dhd) > 0)) {
6272 		atomic_dec(&dhd->pend_8021x_cnt);
6273 	}
6274 
6275 #ifdef PROP_TXSTATUS
6276 	if (dhdp->wlfc_state && (dhdp->proptxstatus_mode != WLFC_FCMODE_NONE)) {
6277 		dhd_if_t *ifp = dhd->iflist[DHD_PKTTAG_IF(PKTTAG(txp))];
6278 		uint datalen  = PKTLEN(dhd->pub.osh, txp);
6279 		if (ifp != NULL) {
6280 			if (success) {
6281 				dhd->pub.tx_packets++;
6282 				ifp->stats.tx_packets++;
6283 				ifp->stats.tx_bytes += datalen;
6284 			} else {
6285 				ifp->stats.tx_dropped++;
6286 			}
6287 		}
6288 	}
6289 #endif
6290 }
6291 
6292 static struct net_device_stats *
dhd_get_stats(struct net_device * net)6293 dhd_get_stats(struct net_device *net)
6294 {
6295 	dhd_info_t *dhd = DHD_DEV_INFO(net);
6296 	dhd_if_t *ifp;
6297 	int ifidx;
6298 
6299 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
6300 
6301 	if (!dhd) {
6302 		DHD_ERROR(("%s : dhd is NULL\n", __FUNCTION__));
6303 		goto error;
6304 	}
6305 
6306 	ifidx = dhd_net2idx(dhd, net);
6307 	if (ifidx == DHD_BAD_IF) {
6308 		DHD_ERROR(("%s: BAD_IF\n", __FUNCTION__));
6309 		goto error;
6310 	}
6311 
6312 	ifp = dhd->iflist[ifidx];
6313 
6314 	if (!ifp) {
6315 		ASSERT(ifp);
6316 		DHD_ERROR(("%s: ifp is NULL\n", __FUNCTION__));
6317 		goto error;
6318 	}
6319 
6320 	if (dhd->pub.up) {
6321 		/* Use the protocol to get dongle stats */
6322 		dhd_prot_dstats(&dhd->pub);
6323 	}
6324 	return &ifp->stats;
6325 
6326 error:
6327 	memset(&net->stats, 0, sizeof(net->stats));
6328 	return &net->stats;
6329 }
6330 
6331 #ifndef BCMDBUS
6332 static int
dhd_watchdog_thread(void * data)6333 dhd_watchdog_thread(void *data)
6334 {
6335 	tsk_ctl_t *tsk = (tsk_ctl_t *)data;
6336 	dhd_info_t *dhd = (dhd_info_t *)tsk->parent;
6337 	/* This thread doesn't need any user-level access,
6338 	 * so get rid of all our resources
6339 	 */
6340 	if (dhd_watchdog_prio > 0) {
6341 		struct sched_param param;
6342 		param.sched_priority = (dhd_watchdog_prio < MAX_RT_PRIO)?
6343 			dhd_watchdog_prio:(MAX_RT_PRIO-1);
6344 		setScheduler(current, SCHED_FIFO, &param);
6345 	}
6346 
6347 	while (1) {
6348 		if (down_interruptible (&tsk->sema) == 0) {
6349 			unsigned long flags;
6350 			unsigned long jiffies_at_start = jiffies;
6351 			unsigned long time_lapse;
6352 			DHD_OS_WD_WAKE_LOCK(&dhd->pub);
6353 
6354 			SMP_RD_BARRIER_DEPENDS();
6355 			if (tsk->terminated) {
6356 				break;
6357 			}
6358 
6359 			if (dhd->pub.dongle_reset == FALSE) {
6360 				DHD_TIMER(("%s:\n", __FUNCTION__));
6361 				dhd_bus_watchdog(&dhd->pub);
6362 
6363 #ifdef DHD_TIMESYNC
6364 				/* Call the timesync module watchdog */
6365 				dhd_timesync_watchdog(&dhd->pub);
6366 #endif /* DHD_TIMESYNC */
6367 
6368 				DHD_GENERAL_LOCK(&dhd->pub, flags);
6369 				/* Count the tick for reference */
6370 				dhd->pub.tickcnt++;
6371 #ifdef DHD_L2_FILTER
6372 				dhd_l2_filter_watchdog(&dhd->pub);
6373 #endif /* DHD_L2_FILTER */
6374 				time_lapse = jiffies - jiffies_at_start;
6375 
6376 				/* Reschedule the watchdog */
6377 				if (dhd->wd_timer_valid) {
6378 					mod_timer(&dhd->timer,
6379 					    jiffies +
6380 					    msecs_to_jiffies(dhd_watchdog_ms) -
6381 					    min(msecs_to_jiffies(dhd_watchdog_ms), time_lapse));
6382 				}
6383 				DHD_GENERAL_UNLOCK(&dhd->pub, flags);
6384 			}
6385 			DHD_OS_WD_WAKE_UNLOCK(&dhd->pub);
6386 		} else {
6387 			break;
6388 		}
6389 	}
6390 
6391 	complete_and_exit(&tsk->completed, 0);
6392 }
6393 
dhd_watchdog(ulong data)6394 static void dhd_watchdog(ulong data)
6395 {
6396 	dhd_info_t *dhd = (dhd_info_t *)data;
6397 	unsigned long flags;
6398 
6399 	if (dhd->pub.dongle_reset) {
6400 		return;
6401 	}
6402 
6403 	if (dhd->thr_wdt_ctl.thr_pid >= 0) {
6404 		up(&dhd->thr_wdt_ctl.sema);
6405 		return;
6406 	}
6407 
6408 	DHD_OS_WD_WAKE_LOCK(&dhd->pub);
6409 	/* Call the bus module watchdog */
6410 	dhd_bus_watchdog(&dhd->pub);
6411 
6412 #ifdef DHD_TIMESYNC
6413 	/* Call the timesync module watchdog */
6414 	dhd_timesync_watchdog(&dhd->pub);
6415 #endif /* DHD_TIMESYNC */
6416 
6417 	DHD_GENERAL_LOCK(&dhd->pub, flags);
6418 	/* Count the tick for reference */
6419 	dhd->pub.tickcnt++;
6420 
6421 #ifdef DHD_L2_FILTER
6422 	dhd_l2_filter_watchdog(&dhd->pub);
6423 #endif /* DHD_L2_FILTER */
6424 	/* Reschedule the watchdog */
6425 	if (dhd->wd_timer_valid)
6426 		mod_timer(&dhd->timer, jiffies + msecs_to_jiffies(dhd_watchdog_ms));
6427 	DHD_GENERAL_UNLOCK(&dhd->pub, flags);
6428 	DHD_OS_WD_WAKE_UNLOCK(&dhd->pub);
6429 }
6430 
6431 #ifdef DHD_PCIE_RUNTIMEPM
6432 static int
dhd_rpm_state_thread(void * data)6433 dhd_rpm_state_thread(void *data)
6434 {
6435 	tsk_ctl_t *tsk = (tsk_ctl_t *)data;
6436 	dhd_info_t *dhd = (dhd_info_t *)tsk->parent;
6437 
6438 	while (1) {
6439 		if (down_interruptible (&tsk->sema) == 0) {
6440 			unsigned long flags;
6441 			unsigned long jiffies_at_start = jiffies;
6442 			unsigned long time_lapse;
6443 
6444 			SMP_RD_BARRIER_DEPENDS();
6445 			if (tsk->terminated) {
6446 				break;
6447 			}
6448 
6449 			if (dhd->pub.dongle_reset == FALSE) {
6450 				DHD_TIMER(("%s:\n", __FUNCTION__));
6451 				if (dhd->pub.up) {
6452 					dhd_runtimepm_state(&dhd->pub);
6453 				}
6454 
6455 				DHD_GENERAL_LOCK(&dhd->pub, flags);
6456 				time_lapse = jiffies - jiffies_at_start;
6457 
6458 				/* Reschedule the watchdog */
6459 				if (dhd->rpm_timer_valid) {
6460 					mod_timer(&dhd->rpm_timer,
6461 						jiffies +
6462 						msecs_to_jiffies(dhd_runtimepm_ms) -
6463 						min(msecs_to_jiffies(dhd_runtimepm_ms),
6464 							time_lapse));
6465 				}
6466 				DHD_GENERAL_UNLOCK(&dhd->pub, flags);
6467 			}
6468 		} else {
6469 			break;
6470 		}
6471 	}
6472 
6473 	complete_and_exit(&tsk->completed, 0);
6474 }
6475 
dhd_runtimepm(ulong data)6476 static void dhd_runtimepm(ulong data)
6477 {
6478 	dhd_info_t *dhd = (dhd_info_t *)data;
6479 
6480 	if (dhd->pub.dongle_reset) {
6481 		return;
6482 	}
6483 
6484 	if (dhd->thr_rpm_ctl.thr_pid >= 0) {
6485 		up(&dhd->thr_rpm_ctl.sema);
6486 		return;
6487 	}
6488 }
6489 
dhd_runtime_pm_disable(dhd_pub_t * dhdp)6490 void dhd_runtime_pm_disable(dhd_pub_t *dhdp)
6491 {
6492 	dhd_os_runtimepm_timer(dhdp, 0);
6493 	dhdpcie_runtime_bus_wake(dhdp, TRUE, __builtin_return_address(0));
6494 	DHD_ERROR(("DHD Runtime PM Disabled \n"));
6495 }
6496 
dhd_runtime_pm_enable(dhd_pub_t * dhdp)6497 void dhd_runtime_pm_enable(dhd_pub_t *dhdp)
6498 {
6499 	if (dhd_get_idletime(dhdp)) {
6500 		dhd_os_runtimepm_timer(dhdp, dhd_runtimepm_ms);
6501 		DHD_ERROR(("DHD Runtime PM Enabled \n"));
6502 	}
6503 }
6504 
6505 #endif /* DHD_PCIE_RUNTIMEPM */
6506 
6507 
6508 #ifdef ENABLE_ADAPTIVE_SCHED
6509 static void
dhd_sched_policy(int prio)6510 dhd_sched_policy(int prio)
6511 {
6512 	struct sched_param param;
6513 	if (cpufreq_quick_get(0) <= CUSTOM_CPUFREQ_THRESH) {
6514 		param.sched_priority = 0;
6515 		setScheduler(current, SCHED_NORMAL, &param);
6516 	} else {
6517 		if (get_scheduler_policy(current) != SCHED_FIFO) {
6518 			param.sched_priority = (prio < MAX_RT_PRIO)? prio : (MAX_RT_PRIO-1);
6519 			setScheduler(current, SCHED_FIFO, &param);
6520 		}
6521 	}
6522 }
6523 #endif /* ENABLE_ADAPTIVE_SCHED */
6524 #ifdef DEBUG_CPU_FREQ
dhd_cpufreq_notifier(struct notifier_block * nb,unsigned long val,void * data)6525 static int dhd_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
6526 {
6527 	dhd_info_t *dhd = container_of(nb, struct dhd_info, freq_trans);
6528 	struct cpufreq_freqs *freq = data;
6529 	if (dhd) {
6530 		if (!dhd->new_freq)
6531 			goto exit;
6532 		if (val == CPUFREQ_POSTCHANGE) {
6533 			DHD_ERROR(("cpu freq is changed to %u kHZ on CPU %d\n",
6534 				freq->new, freq->cpu));
6535 			*per_cpu_ptr(dhd->new_freq, freq->cpu) = freq->new;
6536 		}
6537 	}
6538 exit:
6539 	return 0;
6540 }
6541 #endif /* DEBUG_CPU_FREQ */
6542 
6543 static int
dhd_dpc_thread(void * data)6544 dhd_dpc_thread(void *data)
6545 {
6546 	tsk_ctl_t *tsk = (tsk_ctl_t *)data;
6547 	dhd_info_t *dhd = (dhd_info_t *)tsk->parent;
6548 
6549 	/* This thread doesn't need any user-level access,
6550 	 * so get rid of all our resources
6551 	 */
6552 	if (dhd_dpc_prio > 0)
6553 	{
6554 		struct sched_param param;
6555 		param.sched_priority = (dhd_dpc_prio < MAX_RT_PRIO)?dhd_dpc_prio:(MAX_RT_PRIO-1);
6556 		setScheduler(current, SCHED_FIFO, &param);
6557 	}
6558 
6559 #ifdef CUSTOM_DPC_CPUCORE
6560 	set_cpus_allowed_ptr(current, cpumask_of(CUSTOM_DPC_CPUCORE));
6561 #endif
6562 #ifdef CUSTOM_SET_CPUCORE
6563 	dhd->pub.current_dpc = current;
6564 #endif /* CUSTOM_SET_CPUCORE */
6565 	/* Run until signal received */
6566 	while (1) {
6567 		if (dhd->pub.conf->dpc_cpucore >= 0) {
6568 			printf("%s: set dpc_cpucore %d\n", __FUNCTION__, dhd->pub.conf->dpc_cpucore);
6569 			set_cpus_allowed_ptr(current, cpumask_of(dhd->pub.conf->dpc_cpucore));
6570 			dhd->pub.conf->dpc_cpucore = -1;
6571 		}
6572 		if (!binary_sema_down(tsk)) {
6573 #ifdef ENABLE_ADAPTIVE_SCHED
6574 			dhd_sched_policy(dhd_dpc_prio);
6575 #endif /* ENABLE_ADAPTIVE_SCHED */
6576 			SMP_RD_BARRIER_DEPENDS();
6577 			if (tsk->terminated) {
6578 				break;
6579 			}
6580 
6581 			/* Call bus dpc unless it indicated down (then clean stop) */
6582 			if (dhd->pub.busstate != DHD_BUS_DOWN) {
6583 #ifdef DEBUG_DPC_THREAD_WATCHDOG
6584 				int resched_cnt = 0;
6585 #endif /* DEBUG_DPC_THREAD_WATCHDOG */
6586 				dhd_os_wd_timer_extend(&dhd->pub, TRUE);
6587 				while (dhd_bus_dpc(dhd->pub.bus)) {
6588 					/* process all data */
6589 #ifdef DEBUG_DPC_THREAD_WATCHDOG
6590 					resched_cnt++;
6591 					if (resched_cnt > MAX_RESCHED_CNT) {
6592 						DHD_INFO(("%s Calling msleep to"
6593 							"let other processes run. \n",
6594 							__FUNCTION__));
6595 						dhd->pub.dhd_bug_on = true;
6596 						resched_cnt = 0;
6597 						OSL_SLEEP(1);
6598 					}
6599 #endif /* DEBUG_DPC_THREAD_WATCHDOG */
6600 				}
6601 				dhd_os_wd_timer_extend(&dhd->pub, FALSE);
6602 				DHD_OS_WAKE_UNLOCK(&dhd->pub);
6603 			} else {
6604 				if (dhd->pub.up)
6605 					dhd_bus_stop(dhd->pub.bus, TRUE);
6606 				DHD_OS_WAKE_UNLOCK(&dhd->pub);
6607 			}
6608 		} else {
6609 			break;
6610 		}
6611 	}
6612 	complete_and_exit(&tsk->completed, 0);
6613 }
6614 
6615 static int
dhd_rxf_thread(void * data)6616 dhd_rxf_thread(void *data)
6617 {
6618 	tsk_ctl_t *tsk = (tsk_ctl_t *)data;
6619 	dhd_info_t *dhd = (dhd_info_t *)tsk->parent;
6620 #if defined(WAIT_DEQUEUE)
6621 #define RXF_WATCHDOG_TIME 250 /* BARK_TIME(1000) /  */
6622 	ulong watchdogTime = OSL_SYSUPTIME(); /* msec */
6623 #endif
6624 	dhd_pub_t *pub = &dhd->pub;
6625 
6626 	/* This thread doesn't need any user-level access,
6627 	 * so get rid of all our resources
6628 	 */
6629 	if (dhd_rxf_prio > 0)
6630 	{
6631 		struct sched_param param;
6632 		param.sched_priority = (dhd_rxf_prio < MAX_RT_PRIO)?dhd_rxf_prio:(MAX_RT_PRIO-1);
6633 		setScheduler(current, SCHED_FIFO, &param);
6634 	}
6635 
6636 #ifdef CUSTOM_SET_CPUCORE
6637 	dhd->pub.current_rxf = current;
6638 #endif /* CUSTOM_SET_CPUCORE */
6639 	/* Run until signal received */
6640 	while (1) {
6641 		if (dhd->pub.conf->rxf_cpucore >= 0) {
6642 			printf("%s: set rxf_cpucore %d\n", __FUNCTION__, dhd->pub.conf->rxf_cpucore);
6643 			set_cpus_allowed_ptr(current, cpumask_of(dhd->pub.conf->rxf_cpucore));
6644 			dhd->pub.conf->rxf_cpucore = -1;
6645 		}
6646 		if (down_interruptible(&tsk->sema) == 0) {
6647 			void *skb;
6648 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
6649 			ulong flags;
6650 #endif
6651 #ifdef ENABLE_ADAPTIVE_SCHED
6652 			dhd_sched_policy(dhd_rxf_prio);
6653 #endif /* ENABLE_ADAPTIVE_SCHED */
6654 
6655 			SMP_RD_BARRIER_DEPENDS();
6656 
6657 			if (tsk->terminated) {
6658 				break;
6659 			}
6660 			skb = dhd_rxf_dequeue(pub);
6661 
6662 			if (skb == NULL) {
6663 				continue;
6664 			}
6665 			while (skb) {
6666 				void *skbnext = PKTNEXT(pub->osh, skb);
6667 				PKTSETNEXT(pub->osh, skb, NULL);
6668 				bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE,
6669 					__FUNCTION__, __LINE__);
6670 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
6671 				netif_rx_ni(skb);
6672 #else
6673 				netif_rx(skb);
6674 				local_irq_save(flags);
6675 				RAISE_RX_SOFTIRQ();
6676 				local_irq_restore(flags);
6677 
6678 #endif
6679 				skb = skbnext;
6680 			}
6681 #if defined(WAIT_DEQUEUE)
6682 			if (OSL_SYSUPTIME() - watchdogTime > RXF_WATCHDOG_TIME) {
6683 				OSL_SLEEP(1);
6684 				watchdogTime = OSL_SYSUPTIME();
6685 			}
6686 #endif
6687 
6688 			DHD_OS_WAKE_UNLOCK(pub);
6689 		} else {
6690 			break;
6691 		}
6692 	}
6693 	complete_and_exit(&tsk->completed, 0);
6694 }
6695 
6696 #ifdef BCMPCIE
dhd_dpc_enable(dhd_pub_t * dhdp)6697 void dhd_dpc_enable(dhd_pub_t *dhdp)
6698 {
6699 #if defined(DHD_LB_RXP) || defined(DHD_LB_TXP)
6700 	dhd_info_t *dhd;
6701 
6702 	if (!dhdp || !dhdp->info)
6703 		return;
6704 	dhd = dhdp->info;
6705 #endif /* DHD_LB_RXP || DHD_LB_TXP */
6706 
6707 #ifdef DHD_LB_RXP
6708 	__skb_queue_head_init(&dhd->rx_pend_queue);
6709 #endif /* DHD_LB_RXP */
6710 
6711 #ifdef DHD_LB_TXP
6712 	skb_queue_head_init(&dhd->tx_pend_queue);
6713 #endif /* DHD_LB_TXP */
6714 }
6715 #endif /* BCMPCIE */
6716 
6717 #ifdef BCMPCIE
6718 void
dhd_dpc_kill(dhd_pub_t * dhdp)6719 dhd_dpc_kill(dhd_pub_t *dhdp)
6720 {
6721 	dhd_info_t *dhd;
6722 
6723 	if (!dhdp) {
6724 		return;
6725 	}
6726 
6727 	dhd = dhdp->info;
6728 
6729 	if (!dhd) {
6730 		return;
6731 	}
6732 
6733 	if (dhd->thr_dpc_ctl.thr_pid < 0) {
6734 		tasklet_kill(&dhd->tasklet);
6735 		DHD_ERROR(("%s: tasklet disabled\n", __FUNCTION__));
6736 	}
6737 
6738 #ifdef DHD_LB
6739 #ifdef DHD_LB_RXP
6740 	cancel_work_sync(&dhd->rx_napi_dispatcher_work);
6741 	__skb_queue_purge(&dhd->rx_pend_queue);
6742 #endif /* DHD_LB_RXP */
6743 #ifdef DHD_LB_TXP
6744 	cancel_work_sync(&dhd->tx_dispatcher_work);
6745 	skb_queue_purge(&dhd->tx_pend_queue);
6746 #endif /* DHD_LB_TXP */
6747 
6748 	/* Kill the Load Balancing Tasklets */
6749 #if defined(DHD_LB_TXC)
6750 	tasklet_kill(&dhd->tx_compl_tasklet);
6751 #endif /* DHD_LB_TXC */
6752 #if defined(DHD_LB_RXC)
6753 	tasklet_kill(&dhd->rx_compl_tasklet);
6754 #endif /* DHD_LB_RXC */
6755 #if defined(DHD_LB_TXP)
6756 	tasklet_kill(&dhd->tx_tasklet);
6757 #endif /* DHD_LB_TXP */
6758 #endif /* DHD_LB */
6759 }
6760 
6761 void
dhd_dpc_tasklet_kill(dhd_pub_t * dhdp)6762 dhd_dpc_tasklet_kill(dhd_pub_t *dhdp)
6763 {
6764 	dhd_info_t *dhd;
6765 
6766 	if (!dhdp) {
6767 		return;
6768 	}
6769 
6770 	dhd = dhdp->info;
6771 
6772 	if (!dhd) {
6773 		return;
6774 	}
6775 
6776 	if (dhd->thr_dpc_ctl.thr_pid < 0) {
6777 		tasklet_kill(&dhd->tasklet);
6778 	}
6779 }
6780 #endif /* BCMPCIE */
6781 
6782 static void
dhd_dpc(ulong data)6783 dhd_dpc(ulong data)
6784 {
6785 	dhd_info_t *dhd;
6786 
6787 	dhd = (dhd_info_t *)data;
6788 
6789 	/* this (tasklet) can be scheduled in dhd_sched_dpc[dhd_linux.c]
6790 	 * down below , wake lock is set,
6791 	 * the tasklet is initialized in dhd_attach()
6792 	 */
6793 	/* Call bus dpc unless it indicated down (then clean stop) */
6794 	if (dhd->pub.busstate != DHD_BUS_DOWN) {
6795 #if defined(DHD_LB_STATS) && defined(PCIE_FULL_DONGLE)
6796 		DHD_LB_STATS_INCR(dhd->dhd_dpc_cnt);
6797 #endif /* DHD_LB_STATS && PCIE_FULL_DONGLE */
6798 		if (dhd_bus_dpc(dhd->pub.bus)) {
6799 			tasklet_schedule(&dhd->tasklet);
6800 		}
6801 	} else {
6802 		dhd_bus_stop(dhd->pub.bus, TRUE);
6803 	}
6804 }
6805 
6806 void
dhd_sched_dpc(dhd_pub_t * dhdp)6807 dhd_sched_dpc(dhd_pub_t *dhdp)
6808 {
6809 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
6810 
6811 	if (dhd->thr_dpc_ctl.thr_pid >= 0) {
6812 		DHD_OS_WAKE_LOCK(dhdp);
6813 		/* If the semaphore does not get up,
6814 		* wake unlock should be done here
6815 		*/
6816 		if (!binary_sema_up(&dhd->thr_dpc_ctl)) {
6817 			DHD_OS_WAKE_UNLOCK(dhdp);
6818 		}
6819 		return;
6820 	} else {
6821 		tasklet_schedule(&dhd->tasklet);
6822 	}
6823 }
6824 #endif /* BCMDBUS */
6825 
6826 static void
dhd_sched_rxf(dhd_pub_t * dhdp,void * skb)6827 dhd_sched_rxf(dhd_pub_t *dhdp, void *skb)
6828 {
6829 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
6830 #ifdef RXF_DEQUEUE_ON_BUSY
6831 	int ret = BCME_OK;
6832 	int retry = 2;
6833 #endif /* RXF_DEQUEUE_ON_BUSY */
6834 
6835 	DHD_OS_WAKE_LOCK(dhdp);
6836 
6837 	DHD_TRACE(("dhd_sched_rxf: Enter\n"));
6838 #ifdef RXF_DEQUEUE_ON_BUSY
6839 	do {
6840 		ret = dhd_rxf_enqueue(dhdp, skb);
6841 		if (ret == BCME_OK || ret == BCME_ERROR)
6842 			break;
6843 		else
6844 			OSL_SLEEP(50); /* waiting for dequeueing */
6845 	} while (retry-- > 0);
6846 
6847 	if (retry <= 0 && ret == BCME_BUSY) {
6848 		void *skbp = skb;
6849 
6850 		while (skbp) {
6851 			void *skbnext = PKTNEXT(dhdp->osh, skbp);
6852 			PKTSETNEXT(dhdp->osh, skbp, NULL);
6853 			bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE,
6854 				__FUNCTION__, __LINE__);
6855 			netif_rx_ni(skbp);
6856 			skbp = skbnext;
6857 		}
6858 		DHD_ERROR(("send skb to kernel backlog without rxf_thread\n"));
6859 	} else {
6860 		if (dhd->thr_rxf_ctl.thr_pid >= 0) {
6861 			up(&dhd->thr_rxf_ctl.sema);
6862 		}
6863 	}
6864 #else /* RXF_DEQUEUE_ON_BUSY */
6865 	do {
6866 		if (dhd_rxf_enqueue(dhdp, skb) == BCME_OK)
6867 			break;
6868 	} while (1);
6869 	if (dhd->thr_rxf_ctl.thr_pid >= 0) {
6870 		up(&dhd->thr_rxf_ctl.sema);
6871 	}
6872 	return;
6873 #endif /* RXF_DEQUEUE_ON_BUSY */
6874 }
6875 
6876 #if defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW)
6877 #endif /* defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW) */
6878 
6879 #ifdef TOE
6880 /* Retrieve current toe component enables, which are kept as a bitmap in toe_ol iovar */
6881 static int
dhd_toe_get(dhd_info_t * dhd,int ifidx,uint32 * toe_ol)6882 dhd_toe_get(dhd_info_t *dhd, int ifidx, uint32 *toe_ol)
6883 {
6884 	char buf[32];
6885 	int ret;
6886 
6887 	ret = dhd_iovar(&dhd->pub, ifidx, "toe_ol", NULL, 0, (char *)&buf, sizeof(buf), FALSE);
6888 
6889 	if (ret < 0) {
6890 		if (ret == -EIO) {
6891 			DHD_ERROR(("%s: toe not supported by device\n", dhd_ifname(&dhd->pub,
6892 				ifidx)));
6893 			return -EOPNOTSUPP;
6894 		}
6895 
6896 		DHD_INFO(("%s: could not get toe_ol: ret=%d\n", dhd_ifname(&dhd->pub, ifidx), ret));
6897 		return ret;
6898 	}
6899 
6900 	memcpy(toe_ol, buf, sizeof(uint32));
6901 	return 0;
6902 }
6903 
6904 /* Set current toe component enables in toe_ol iovar, and set toe global enable iovar */
6905 static int
dhd_toe_set(dhd_info_t * dhd,int ifidx,uint32 toe_ol)6906 dhd_toe_set(dhd_info_t *dhd, int ifidx, uint32 toe_ol)
6907 {
6908 	int toe, ret;
6909 
6910 	/* Set toe_ol as requested */
6911 	ret = dhd_iovar(&dhd->pub, ifidx, "toe_ol", (char *)&toe_ol, sizeof(toe_ol), NULL, 0, TRUE);
6912 	if (ret < 0) {
6913 		DHD_ERROR(("%s: could not set toe_ol: ret=%d\n",
6914 			dhd_ifname(&dhd->pub, ifidx), ret));
6915 		return ret;
6916 	}
6917 
6918 	/* Enable toe globally only if any components are enabled. */
6919 	toe = (toe_ol != 0);
6920 	ret = dhd_iovar(&dhd->pub, ifidx, "toe", (char *)&toe, sizeof(toe), NULL, 0, TRUE);
6921 	if (ret < 0) {
6922 		DHD_ERROR(("%s: could not set toe: ret=%d\n", dhd_ifname(&dhd->pub, ifidx), ret));
6923 		return ret;
6924 	}
6925 
6926 	return 0;
6927 }
6928 #endif /* TOE */
6929 
6930 #if defined(WL_CFG80211) && defined(NUM_SCB_MAX_PROBE)
dhd_set_scb_probe(dhd_pub_t * dhd)6931 void dhd_set_scb_probe(dhd_pub_t *dhd)
6932 {
6933 	wl_scb_probe_t scb_probe;
6934 	int ret;
6935 
6936 	if (dhd->op_mode & DHD_FLAG_HOSTAP_MODE) {
6937 		return;
6938 	}
6939 
6940 	ret = dhd_iovar(dhd, 0, "scb_probe", NULL, 0,
6941 			(char *)&scb_probe, sizeof(scb_probe), FALSE);
6942 	if (ret < 0) {
6943 		DHD_ERROR(("%s: GET max_scb_probe failed\n", __FUNCTION__));
6944 	}
6945 
6946 	scb_probe.scb_max_probe = NUM_SCB_MAX_PROBE;
6947 
6948 	ret = dhd_iovar(dhd, 0, "scb_probe", (char *)&scb_probe, sizeof(scb_probe),
6949 			NULL, 0, TRUE);
6950 	if (ret < 0) {
6951 		DHD_ERROR(("%s: max_scb_probe setting failed\n", __FUNCTION__));
6952 		return;
6953 	}
6954 }
6955 #endif /* WL_CFG80211 && NUM_SCB_MAX_PROBE */
6956 
6957 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
6958 static void
dhd_ethtool_get_drvinfo(struct net_device * net,struct ethtool_drvinfo * info)6959 dhd_ethtool_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
6960 {
6961 	dhd_info_t *dhd = DHD_DEV_INFO(net);
6962 
6963 	snprintf(info->driver, sizeof(info->driver), "wl");
6964 	snprintf(info->version, sizeof(info->version), "%lu", dhd->pub.drv_version);
6965 }
6966 
6967 struct ethtool_ops dhd_ethtool_ops = {
6968 	.get_drvinfo = dhd_ethtool_get_drvinfo
6969 };
6970 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) */
6971 
6972 
6973 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 2)
6974 static int
dhd_ethtool(dhd_info_t * dhd,void * uaddr)6975 dhd_ethtool(dhd_info_t *dhd, void *uaddr)
6976 {
6977 	struct ethtool_drvinfo info;
6978 	char drvname[sizeof(info.driver)];
6979 	uint32 cmd;
6980 #ifdef TOE
6981 	struct ethtool_value edata;
6982 	uint32 toe_cmpnt, csum_dir;
6983 	int ret;
6984 #endif
6985 
6986 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
6987 
6988 	/* all ethtool calls start with a cmd word */
6989 	if (copy_from_user(&cmd, uaddr, sizeof (uint32)))
6990 		return -EFAULT;
6991 
6992 	switch (cmd) {
6993 	case ETHTOOL_GDRVINFO:
6994 		/* Copy out any request driver name */
6995 		if (copy_from_user(&info, uaddr, sizeof(info)))
6996 			return -EFAULT;
6997 		strncpy(drvname, info.driver, sizeof(info.driver));
6998 		drvname[sizeof(info.driver)-1] = '\0';
6999 
7000 		/* clear struct for return */
7001 		memset(&info, 0, sizeof(info));
7002 		info.cmd = cmd;
7003 
7004 		/* if dhd requested, identify ourselves */
7005 		if (strcmp(drvname, "?dhd") == 0) {
7006 			snprintf(info.driver, sizeof(info.driver), "dhd");
7007 			strncpy(info.version, EPI_VERSION_STR, sizeof(info.version) - 1);
7008 			info.version[sizeof(info.version) - 1] = '\0';
7009 		}
7010 
7011 		/* otherwise, require dongle to be up */
7012 		else if (!dhd->pub.up) {
7013 			DHD_ERROR(("%s: dongle is not up\n", __FUNCTION__));
7014 			return -ENODEV;
7015 		}
7016 
7017 		/* finally, report dongle driver type */
7018 		else if (dhd->pub.iswl)
7019 			snprintf(info.driver, sizeof(info.driver), "wl");
7020 		else
7021 			snprintf(info.driver, sizeof(info.driver), "xx");
7022 
7023 		snprintf(info.version, sizeof(info.version), "%lu", dhd->pub.drv_version);
7024 		if (copy_to_user(uaddr, &info, sizeof(info)))
7025 			return -EFAULT;
7026 		DHD_CTL(("%s: given %*s, returning %s\n", __FUNCTION__,
7027 		         (int)sizeof(drvname), drvname, info.driver));
7028 		break;
7029 
7030 #ifdef TOE
7031 	/* Get toe offload components from dongle */
7032 	case ETHTOOL_GRXCSUM:
7033 	case ETHTOOL_GTXCSUM:
7034 		if ((ret = dhd_toe_get(dhd, 0, &toe_cmpnt)) < 0)
7035 			return ret;
7036 
7037 		csum_dir = (cmd == ETHTOOL_GTXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
7038 
7039 		edata.cmd = cmd;
7040 		edata.data = (toe_cmpnt & csum_dir) ? 1 : 0;
7041 
7042 		if (copy_to_user(uaddr, &edata, sizeof(edata)))
7043 			return -EFAULT;
7044 		break;
7045 
7046 	/* Set toe offload components in dongle */
7047 	case ETHTOOL_SRXCSUM:
7048 	case ETHTOOL_STXCSUM:
7049 		if (copy_from_user(&edata, uaddr, sizeof(edata)))
7050 			return -EFAULT;
7051 
7052 		/* Read the current settings, update and write back */
7053 		if ((ret = dhd_toe_get(dhd, 0, &toe_cmpnt)) < 0)
7054 			return ret;
7055 
7056 		csum_dir = (cmd == ETHTOOL_STXCSUM) ? TOE_TX_CSUM_OL : TOE_RX_CSUM_OL;
7057 
7058 		if (edata.data != 0)
7059 			toe_cmpnt |= csum_dir;
7060 		else
7061 			toe_cmpnt &= ~csum_dir;
7062 
7063 		if ((ret = dhd_toe_set(dhd, 0, toe_cmpnt)) < 0)
7064 			return ret;
7065 
7066 		/* If setting TX checksum mode, tell Linux the new mode */
7067 		if (cmd == ETHTOOL_STXCSUM) {
7068 			if (edata.data)
7069 				dhd->iflist[0]->net->features |= NETIF_F_IP_CSUM;
7070 			else
7071 				dhd->iflist[0]->net->features &= ~NETIF_F_IP_CSUM;
7072 		}
7073 
7074 		break;
7075 #endif /* TOE */
7076 
7077 	default:
7078 		return -EOPNOTSUPP;
7079 	}
7080 
7081 	return 0;
7082 }
7083 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 2) */
7084 
dhd_check_hang(struct net_device * net,dhd_pub_t * dhdp,int error)7085 static bool dhd_check_hang(struct net_device *net, dhd_pub_t *dhdp, int error)
7086 {
7087 	if (!dhdp) {
7088 		DHD_ERROR(("%s: dhdp is NULL\n", __FUNCTION__));
7089 		return FALSE;
7090 	}
7091 
7092 	if (!dhdp->up)
7093 		return FALSE;
7094 
7095 #if !defined(BCMPCIE) && !defined(BCMDBUS)
7096 	if (dhdp->info->thr_dpc_ctl.thr_pid < 0) {
7097 		DHD_ERROR(("%s : skipped due to negative pid - unloading?\n", __FUNCTION__));
7098 		return FALSE;
7099 	}
7100 #endif /* !BCMPCIE && !BCMDBUS */
7101 
7102 	if ((error == -ETIMEDOUT) || (error == -EREMOTEIO) ||
7103 		((dhdp->busstate == DHD_BUS_DOWN) && (!dhdp->dongle_reset))) {
7104 #ifdef BCMPCIE
7105 		DHD_ERROR(("%s: Event HANG send up due to  re=%d te=%d d3acke=%d e=%d s=%d\n",
7106 			__FUNCTION__, dhdp->rxcnt_timeout, dhdp->txcnt_timeout,
7107 			dhdp->d3ackcnt_timeout, error, dhdp->busstate));
7108 #else
7109 		DHD_ERROR(("%s: Event HANG send up due to  re=%d te=%d e=%d s=%d\n", __FUNCTION__,
7110 			dhdp->rxcnt_timeout, dhdp->txcnt_timeout, error, dhdp->busstate));
7111 #endif /* BCMPCIE */
7112 		if (dhdp->hang_reason == 0) {
7113 			if (dhdp->dongle_trap_occured) {
7114 				dhdp->hang_reason = HANG_REASON_DONGLE_TRAP;
7115 #ifdef BCMPCIE
7116 			} else if (dhdp->d3ackcnt_timeout) {
7117 				dhdp->hang_reason = HANG_REASON_D3_ACK_TIMEOUT;
7118 #endif /* BCMPCIE */
7119 			} else {
7120 				dhdp->hang_reason = HANG_REASON_IOCTL_RESP_TIMEOUT;
7121 			}
7122 		}
7123 		printf("%s\n", info_string);
7124 		net_os_send_hang_message(net);
7125 		return TRUE;
7126 	}
7127 	return FALSE;
7128 }
7129 
7130 #ifdef WL_MONITOR
7131 bool
dhd_monitor_enabled(dhd_pub_t * dhd,int ifidx)7132 dhd_monitor_enabled(dhd_pub_t *dhd, int ifidx)
7133 {
7134 	return (dhd->info->monitor_type != 0);
7135 }
7136 
7137 void
dhd_rx_mon_pkt(dhd_pub_t * dhdp,host_rxbuf_cmpl_t * msg,void * pkt,int ifidx)7138 dhd_rx_mon_pkt(dhd_pub_t *dhdp, host_rxbuf_cmpl_t* msg, void *pkt, int ifidx)
7139 {
7140 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
7141 #ifdef HOST_RADIOTAP_CONV
7142 	uint16 len = 0, offset = 0;
7143 	monitor_pkt_info_t pkt_info;
7144 	memcpy(&pkt_info.marker, &msg->marker, sizeof(msg->marker));
7145 	memcpy(&pkt_info.ts, &msg->ts, sizeof(monitor_pkt_ts_t));
7146 
7147 	if (!dhd->monitor_skb) {
7148 		if ((dhd->monitor_skb = dev_alloc_skb(MAX_MON_PKT_SIZE)) == NULL)
7149 			return;
7150 	}
7151 
7152 	len = bcmwifi_monitor(dhd->monitor_info, &pkt_info, PKTDATA(dhdp->osh, pkt),
7153 		PKTLEN(dhdp->osh, pkt), PKTDATA(dhdp->osh, dhd->monitor_skb), &offset);
7154 
7155 	if (dhd->monitor_type && dhd->monitor_dev)
7156 		dhd->monitor_skb->dev = dhd->monitor_dev;
7157 	else {
7158 		PKTFREE(dhdp->osh, pkt, FALSE);
7159 		dev_kfree_skb(dhd->monitor_skb);
7160 		return;
7161 	}
7162 
7163 	PKTFREE(dhdp->osh, pkt, FALSE);
7164 
7165 	if (!len) {
7166 		return;
7167 	}
7168 
7169 	skb_put(dhd->monitor_skb, len);
7170 	skb_pull(dhd->monitor_skb, offset);
7171 
7172 	dhd->monitor_skb->protocol = eth_type_trans(dhd->monitor_skb, dhd->monitor_skb->dev);
7173 #else
7174 	uint8 amsdu_flag = (msg->flags & BCMPCIE_PKT_FLAGS_MONITOR_MASK) >>
7175 		BCMPCIE_PKT_FLAGS_MONITOR_SHIFT;
7176 	switch (amsdu_flag) {
7177 		case BCMPCIE_PKT_FLAGS_MONITOR_NO_AMSDU:
7178 		default:
7179 			if (!dhd->monitor_skb) {
7180 				if ((dhd->monitor_skb = PKTTONATIVE(dhdp->osh, pkt)) == NULL)
7181 					return;
7182 			}
7183 
7184 			if (dhd->monitor_type && dhd->monitor_dev)
7185 				dhd->monitor_skb->dev = dhd->monitor_dev;
7186 			else {
7187 				PKTFREE(dhdp->osh, pkt, FALSE);
7188 				dhd->monitor_skb = NULL;
7189 				return;
7190 			}
7191 
7192 			dhd->monitor_skb->protocol =
7193 				eth_type_trans(dhd->monitor_skb, dhd->monitor_skb->dev);
7194 			dhd->monitor_len = 0;
7195 			break;
7196 		case BCMPCIE_PKT_FLAGS_MONITOR_FIRST_PKT:
7197 			if (!dhd->monitor_skb) {
7198 				if ((dhd->monitor_skb = dev_alloc_skb(MAX_MON_PKT_SIZE)) == NULL)
7199 					return;
7200 				dhd->monitor_len = 0;
7201 			}
7202 			if (dhd->monitor_type && dhd->monitor_dev)
7203 				dhd->monitor_skb->dev = dhd->monitor_dev;
7204 			else {
7205 				PKTFREE(dhdp->osh, pkt, FALSE);
7206 				dev_kfree_skb(dhd->monitor_skb);
7207 				return;
7208 			}
7209 			memcpy(PKTDATA(dhdp->osh, dhd->monitor_skb),
7210 				PKTDATA(dhdp->osh, pkt), PKTLEN(dhdp->osh, pkt));
7211 
7212 			dhd->monitor_len = PKTLEN(dhdp->osh, pkt);
7213 			PKTFREE(dhdp->osh, pkt, FALSE);
7214 			return;
7215 		case BCMPCIE_PKT_FLAGS_MONITOR_INTER_PKT:
7216 			memcpy(PKTDATA(dhdp->osh, dhd->monitor_skb) + dhd->monitor_len,
7217 				PKTDATA(dhdp->osh, pkt), PKTLEN(dhdp->osh, pkt));
7218 			dhd->monitor_len += PKTLEN(dhdp->osh, pkt);
7219 
7220 			PKTFREE(dhdp->osh, pkt, FALSE);
7221 			return;
7222 		case BCMPCIE_PKT_FLAGS_MONITOR_LAST_PKT:
7223 			memcpy(PKTDATA(dhdp->osh, dhd->monitor_skb) + dhd->monitor_len,
7224 				PKTDATA(dhdp->osh, pkt), PKTLEN(dhdp->osh, pkt));
7225 			dhd->monitor_len += PKTLEN(dhdp->osh, pkt);
7226 
7227 			PKTFREE(dhdp->osh, pkt, FALSE);
7228 			skb_put(dhd->monitor_skb, dhd->monitor_len);
7229 			dhd->monitor_skb->protocol =
7230 				eth_type_trans(dhd->monitor_skb, dhd->monitor_skb->dev);
7231 			dhd->monitor_len = 0;
7232 			break;
7233 	}
7234 
7235 #endif /* HOST_RADIOTAP_CONV */
7236 	if (in_interrupt()) {
7237 		bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE,
7238 			__FUNCTION__, __LINE__);
7239 		DHD_PERIM_UNLOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
7240 		netif_rx(dhd->monitor_skb);
7241 		DHD_PERIM_LOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
7242 	} else {
7243 		/* If the receive is not processed inside an ISR,
7244 		 * the softirqd must be woken explicitly to service
7245 		 * the NET_RX_SOFTIRQ.	In 2.6 kernels, this is handled
7246 		 * by netif_rx_ni(), but in earlier kernels, we need
7247 		 * to do it manually.
7248 		 */
7249 		bcm_object_trace_opr(dhd->monitor_skb, BCM_OBJDBG_REMOVE,
7250 			__FUNCTION__, __LINE__);
7251 
7252 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
7253 		DHD_PERIM_UNLOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
7254 		netif_rx_ni(dhd->monitor_skb);
7255 		DHD_PERIM_LOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
7256 #else
7257 		ulong flags;
7258 		DHD_PERIM_UNLOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
7259 		netif_rx(dhd->monitor_skb);
7260 		DHD_PERIM_LOCK_ALL((dhd->fwder_unit % FWDER_MAX_UNIT));
7261 		local_irq_save(flags);
7262 		RAISE_RX_SOFTIRQ();
7263 		local_irq_restore(flags);
7264 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) */
7265 	}
7266 
7267 	dhd->monitor_skb = NULL;
7268 }
7269 
7270 typedef struct dhd_mon_dev_priv {
7271 	struct net_device_stats stats;
7272 } dhd_mon_dev_priv_t;
7273 
7274 #define DHD_MON_DEV_PRIV_SIZE		(sizeof(dhd_mon_dev_priv_t))
7275 #define DHD_MON_DEV_PRIV(dev)		((dhd_mon_dev_priv_t *)DEV_PRIV(dev))
7276 #define DHD_MON_DEV_STATS(dev)		(((dhd_mon_dev_priv_t *)DEV_PRIV(dev))->stats)
7277 
7278 static int
dhd_monitor_start(struct sk_buff * skb,struct net_device * dev)7279 dhd_monitor_start(struct sk_buff *skb, struct net_device *dev)
7280 {
7281 	PKTFREE(NULL, skb, FALSE);
7282 	return 0;
7283 }
7284 
7285 static int
dhd_monitor_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)7286 dhd_monitor_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7287 {
7288 	return 0;
7289 }
7290 
7291 static struct net_device_stats*
dhd_monitor_get_stats(struct net_device * dev)7292 dhd_monitor_get_stats(struct net_device *dev)
7293 {
7294 	return &DHD_MON_DEV_STATS(dev);
7295 }
7296 
7297 static const struct net_device_ops netdev_monitor_ops =
7298 {
7299 	.ndo_start_xmit = dhd_monitor_start,
7300 	.ndo_get_stats = dhd_monitor_get_stats,
7301 	.ndo_do_ioctl = dhd_monitor_ioctl
7302 };
7303 
7304 static void
dhd_add_monitor_if(void * handle,void * event_info,u8 event)7305 dhd_add_monitor_if(void *handle, void *event_info, u8 event)
7306 {
7307 	dhd_info_t *dhd = handle;
7308 	struct net_device *dev;
7309 	char *devname;
7310 
7311 	if (event != DHD_WQ_WORK_IF_ADD) {
7312 		DHD_ERROR(("%s: unexpected event \n", __FUNCTION__));
7313 		return;
7314 	}
7315 
7316 	if (!dhd) {
7317 		DHD_ERROR(("%s: dhd info not available \n", __FUNCTION__));
7318 		return;
7319 	}
7320 
7321 	dev = alloc_etherdev(DHD_MON_DEV_PRIV_SIZE);
7322 	if (!dev) {
7323 		DHD_ERROR(("%s: alloc wlif failed\n", __FUNCTION__));
7324 		return;
7325 	}
7326 
7327 	devname = "radiotap";
7328 
7329 	snprintf(dev->name, sizeof(dev->name), "%s%u", devname, dhd->unit);
7330 
7331 #ifndef ARPHRD_IEEE80211_PRISM  /* From Linux 2.4.18 */
7332 #define ARPHRD_IEEE80211_PRISM 802
7333 #endif
7334 
7335 #ifndef ARPHRD_IEEE80211_RADIOTAP
7336 #define ARPHRD_IEEE80211_RADIOTAP	803 /* IEEE 802.11 + radiotap header */
7337 #endif /* ARPHRD_IEEE80211_RADIOTAP */
7338 
7339 	dev->type = ARPHRD_IEEE80211_RADIOTAP;
7340 
7341 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
7342 	dev->hard_start_xmit = dhd_monitor_start;
7343 	dev->do_ioctl = dhd_monitor_ioctl;
7344 	dev->get_stats = dhd_monitor_get_stats;
7345 #else
7346 	dev->netdev_ops = &netdev_monitor_ops;
7347 #endif
7348 
7349 	if (register_netdev(dev)) {
7350 		DHD_ERROR(("%s, register_netdev failed for %s\n",
7351 			__FUNCTION__, dev->name));
7352 		free_netdev(dev);
7353 	}
7354 
7355 	bcmwifi_monitor_create(&dhd->monitor_info);
7356 	dhd->monitor_dev = dev;
7357 }
7358 
7359 static void
dhd_del_monitor_if(void * handle,void * event_info,u8 event)7360 dhd_del_monitor_if(void *handle, void *event_info, u8 event)
7361 {
7362 	dhd_info_t *dhd = handle;
7363 
7364 	if (event != DHD_WQ_WORK_IF_DEL) {
7365 		DHD_ERROR(("%s: unexpected event \n", __FUNCTION__));
7366 		return;
7367 	}
7368 
7369 	if (!dhd) {
7370 		DHD_ERROR(("%s: dhd info not available \n", __FUNCTION__));
7371 		return;
7372 	}
7373 
7374 	if (dhd->monitor_dev) {
7375 		unregister_netdev(dhd->monitor_dev);
7376 
7377 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24))
7378 		MFREE(dhd->osh, dhd->monitor_dev->priv, DHD_MON_DEV_PRIV_SIZE);
7379 		MFREE(dhd->osh, dhd->monitor_dev, sizeof(struct net_device));
7380 #else
7381 		free_netdev(dhd->monitor_dev);
7382 #endif /* 2.6.24 */
7383 
7384 		dhd->monitor_dev = NULL;
7385 	}
7386 
7387 	if (dhd->monitor_info) {
7388 		bcmwifi_monitor_delete(dhd->monitor_info);
7389 		dhd->monitor_info = NULL;
7390 	}
7391 }
7392 
7393 static void
dhd_set_monitor(dhd_pub_t * dhd,int ifidx,int val)7394 dhd_set_monitor(dhd_pub_t *dhd, int ifidx, int val)
7395 {
7396 	dhd_info_t *info = dhd->info;
7397 
7398 	DHD_TRACE(("%s: val %d\n", __FUNCTION__, val));
7399 	if ((val && info->monitor_dev) || (!val && !info->monitor_dev)) {
7400 		DHD_ERROR(("%s: Mismatched params, return\n", __FUNCTION__));
7401 		return;
7402 	}
7403 
7404 	/* Delete monitor */
7405 	if (!val) {
7406 		info->monitor_type = val;
7407 		dhd_deferred_schedule_work(info->dhd_deferred_wq, NULL, DHD_WQ_WORK_IF_DEL,
7408 			dhd_del_monitor_if, DHD_WQ_WORK_PRIORITY_LOW);
7409 		return;
7410 	}
7411 
7412 	/* Add monitor */
7413 	info->monitor_type = val;
7414 	dhd_deferred_schedule_work(info->dhd_deferred_wq, NULL, DHD_WQ_WORK_IF_ADD,
7415 		dhd_add_monitor_if, DHD_WQ_WORK_PRIORITY_LOW);
7416 }
7417 #endif /* WL_MONITOR */
7418 
dhd_ioctl_process(dhd_pub_t * pub,int ifidx,dhd_ioctl_t * ioc,void * data_buf)7419 int dhd_ioctl_process(dhd_pub_t *pub, int ifidx, dhd_ioctl_t *ioc, void *data_buf)
7420 {
7421 	int bcmerror = BCME_OK;
7422 	int buflen = 0;
7423 	struct net_device *net;
7424 
7425 #ifdef REPORT_FATAL_TIMEOUTS
7426 	if (ioc->cmd == WLC_SET_WPA_AUTH) {
7427 		int wpa_auth;
7428 
7429 		wpa_auth = *((int *)ioc->buf);
7430 		DHD_INFO(("wpa_auth:%d\n", wpa_auth));
7431 		if (wpa_auth != WPA_AUTH_DISABLED) {
7432 			/* If AP is with security then enable WLC_E_PSK_SUP event checking */
7433 			dhd_set_join_error(pub, WLC_WPA_MASK);
7434 		} else {
7435 			/* If AP is with open then disable WLC_E_PSK_SUP event checking */
7436 			dhd_clear_join_error(pub, WLC_WPA_MASK);
7437 		}
7438 	}
7439 
7440 	if (ioc->cmd == WLC_SET_AUTH) {
7441 		int auth;
7442 		auth = *((int *)ioc->buf);
7443 		DHD_INFO(("Auth:%d\n", auth));
7444 
7445 		if (auth != WL_AUTH_OPEN_SYSTEM) {
7446 			/* If AP is with security then enable WLC_E_PSK_SUP event checking */
7447 			dhd_set_join_error(pub, WLC_WPA_MASK);
7448 		} else {
7449 			/* If AP is with open then disable WLC_E_PSK_SUP event checking */
7450 			dhd_clear_join_error(pub, WLC_WPA_MASK);
7451 		}
7452 	}
7453 #endif /* REPORT_FATAL_TIMEOUTS */
7454 	net = dhd_idx2net(pub, ifidx);
7455 	if (!net) {
7456 		bcmerror = BCME_BADARG;
7457 		goto done;
7458 	}
7459 
7460 	/* check for local dhd ioctl and handle it */
7461 	if (ioc->driver == DHD_IOCTL_MAGIC) {
7462 		/* This is a DHD IOVAR, truncate buflen to DHD_IOCTL_MAXLEN */
7463 		if (data_buf)
7464 			buflen = MIN(ioc->len, DHD_IOCTL_MAXLEN);
7465 		bcmerror = dhd_ioctl((void *)pub, ioc, data_buf, buflen);
7466 		if (bcmerror)
7467 			pub->bcmerror = bcmerror;
7468 		goto done;
7469 	}
7470 
7471 	/* This is a WL IOVAR, truncate buflen to WLC_IOCTL_MAXLEN */
7472 	if (data_buf)
7473 		buflen = MIN(ioc->len, WLC_IOCTL_MAXLEN);
7474 
7475 #ifndef BCMDBUS
7476 	/* send to dongle (must be up, and wl). */
7477 	if (pub->busstate == DHD_BUS_DOWN || pub->busstate == DHD_BUS_LOAD) {
7478 		if ((!pub->dongle_trap_occured) && allow_delay_fwdl) {
7479 			int ret;
7480 			if (atomic_read(&exit_in_progress)) {
7481 				DHD_ERROR(("%s module exit in progress\n", __func__));
7482 				bcmerror = BCME_DONGLE_DOWN;
7483 				goto done;
7484 			}
7485 			ret = dhd_bus_start(pub);
7486 			if (ret != 0) {
7487 				DHD_ERROR(("%s: failed with code %d\n", __FUNCTION__, ret));
7488 				bcmerror = BCME_DONGLE_DOWN;
7489 				goto done;
7490 			}
7491 		} else {
7492 			bcmerror = BCME_DONGLE_DOWN;
7493 			goto done;
7494 		}
7495 	}
7496 
7497 	if (!pub->iswl) {
7498 		bcmerror = BCME_DONGLE_DOWN;
7499 		goto done;
7500 	}
7501 #endif /* !BCMDBUS */
7502 
7503 	/*
7504 	 * Flush the TX queue if required for proper message serialization:
7505 	 * Intercept WLC_SET_KEY IOCTL - serialize M4 send and set key IOCTL to
7506 	 * prevent M4 encryption and
7507 	 * intercept WLC_DISASSOC IOCTL - serialize WPS-DONE and WLC_DISASSOC IOCTL to
7508 	 * prevent disassoc frame being sent before WPS-DONE frame.
7509 	 */
7510 	if (ioc->cmd == WLC_SET_KEY ||
7511 	    (ioc->cmd == WLC_SET_VAR && data_buf != NULL &&
7512 	     strncmp("wsec_key", data_buf, 9) == 0) ||
7513 	    (ioc->cmd == WLC_SET_VAR && data_buf != NULL &&
7514 	     strncmp("bsscfg:wsec_key", data_buf, 15) == 0) ||
7515 	    ioc->cmd == WLC_DISASSOC)
7516 		dhd_wait_pend8021x(net);
7517 
7518 #ifdef WLMEDIA_HTSF
7519 	if (data_buf) {
7520 		/*  short cut wl ioctl calls here  */
7521 		if (strcmp("htsf", data_buf) == 0) {
7522 			dhd_ioctl_htsf_get(dhd, 0);
7523 			return BCME_OK;
7524 		}
7525 
7526 		if (strcmp("htsflate", data_buf) == 0) {
7527 			if (ioc->set) {
7528 				memset(ts, 0, sizeof(tstamp_t)*TSMAX);
7529 				memset(&maxdelayts, 0, sizeof(tstamp_t));
7530 				maxdelay = 0;
7531 				tspktcnt = 0;
7532 				maxdelaypktno = 0;
7533 				memset(&vi_d1.bin, 0, sizeof(uint32)*NUMBIN);
7534 				memset(&vi_d2.bin, 0, sizeof(uint32)*NUMBIN);
7535 				memset(&vi_d3.bin, 0, sizeof(uint32)*NUMBIN);
7536 				memset(&vi_d4.bin, 0, sizeof(uint32)*NUMBIN);
7537 			} else {
7538 				dhd_dump_latency();
7539 			}
7540 			return BCME_OK;
7541 		}
7542 		if (strcmp("htsfclear", data_buf) == 0) {
7543 			memset(&vi_d1.bin, 0, sizeof(uint32)*NUMBIN);
7544 			memset(&vi_d2.bin, 0, sizeof(uint32)*NUMBIN);
7545 			memset(&vi_d3.bin, 0, sizeof(uint32)*NUMBIN);
7546 			memset(&vi_d4.bin, 0, sizeof(uint32)*NUMBIN);
7547 			htsf_seqnum = 0;
7548 			return BCME_OK;
7549 		}
7550 		if (strcmp("htsfhis", data_buf) == 0) {
7551 			dhd_dump_htsfhisto(&vi_d1, "H to D");
7552 			dhd_dump_htsfhisto(&vi_d2, "D to D");
7553 			dhd_dump_htsfhisto(&vi_d3, "D to H");
7554 			dhd_dump_htsfhisto(&vi_d4, "H to H");
7555 			return BCME_OK;
7556 		}
7557 		if (strcmp("tsport", data_buf) == 0) {
7558 			if (ioc->set) {
7559 				memcpy(&tsport, data_buf + 7, 4);
7560 			} else {
7561 				DHD_ERROR(("current timestamp port: %d \n", tsport));
7562 			}
7563 			return BCME_OK;
7564 		}
7565 	}
7566 #endif /* WLMEDIA_HTSF */
7567 
7568 	if ((ioc->cmd == WLC_SET_VAR || ioc->cmd == WLC_GET_VAR) &&
7569 		data_buf != NULL && strncmp("rpc_", data_buf, 4) == 0) {
7570 #ifdef BCM_FD_AGGR
7571 		bcmerror = dhd_fdaggr_ioctl(pub, ifidx, (wl_ioctl_t *)ioc, data_buf, buflen);
7572 #else
7573 		bcmerror = BCME_UNSUPPORTED;
7574 #endif
7575 		goto done;
7576 	}
7577 	bcmerror = dhd_wl_ioctl(pub, ifidx, (wl_ioctl_t *)ioc, data_buf, buflen);
7578 
7579 #ifdef WL_MONITOR
7580 	/* Intercept monitor ioctl here, add/del monitor if */
7581 	if (bcmerror == BCME_OK && ioc->cmd == WLC_SET_MONITOR) {
7582 		dhd_set_monitor(pub, ifidx, *(int32*)data_buf);
7583 	}
7584 #endif
7585 
7586 #ifdef REPORT_FATAL_TIMEOUTS
7587 	if (ioc->cmd == WLC_SCAN && bcmerror == 0) {
7588 		dhd_start_scan_timer(pub);
7589 	}
7590 	if (ioc->cmd == WLC_SET_SSID && bcmerror == 0) {
7591 		dhd_start_join_timer(pub);
7592 	}
7593 #endif  /* REPORT_FATAL_TIMEOUTS */
7594 
7595 done:
7596 	dhd_check_hang(net, pub, bcmerror);
7597 
7598 	return bcmerror;
7599 }
7600 
7601 static int
dhd_ioctl_entry(struct net_device * net,struct ifreq * ifr,int cmd)7602 dhd_ioctl_entry(struct net_device *net, struct ifreq *ifr, int cmd)
7603 {
7604 	dhd_info_t *dhd = DHD_DEV_INFO(net);
7605 	dhd_ioctl_t ioc;
7606 	int bcmerror = 0;
7607 	int ifidx;
7608 	int ret;
7609 	void *local_buf = NULL;
7610 	void __user *ioc_buf_user = NULL;
7611 	u16 buflen = 0;
7612 
7613 	if (atomic_read(&exit_in_progress)) {
7614 		DHD_ERROR(("%s module exit in progress\n", __func__));
7615 		bcmerror = BCME_DONGLE_DOWN;
7616 		return OSL_ERROR(bcmerror);
7617 	}
7618 
7619 	DHD_OS_WAKE_LOCK(&dhd->pub);
7620 	DHD_PERIM_LOCK(&dhd->pub);
7621 
7622 	/* Interface up check for built-in type */
7623 	if (!dhd_download_fw_on_driverload && dhd->pub.up == FALSE) {
7624 		DHD_ERROR(("%s: Interface is down \n", __FUNCTION__));
7625 		DHD_PERIM_UNLOCK(&dhd->pub);
7626 		DHD_OS_WAKE_UNLOCK(&dhd->pub);
7627 		return OSL_ERROR(BCME_NOTUP);
7628 	}
7629 
7630 	ifidx = dhd_net2idx(dhd, net);
7631 	DHD_TRACE(("%s: ifidx %d, cmd 0x%04x\n", __FUNCTION__, ifidx, cmd));
7632 
7633 	if (ifidx == DHD_BAD_IF) {
7634 		DHD_ERROR(("%s: BAD IF\n", __FUNCTION__));
7635 		DHD_PERIM_UNLOCK(&dhd->pub);
7636 		DHD_OS_WAKE_UNLOCK(&dhd->pub);
7637 		return -1;
7638 	}
7639 
7640 #if defined(WL_WIRELESS_EXT)
7641 	/* linux wireless extensions */
7642 	if ((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST)) {
7643 		/* may recurse, do NOT lock */
7644 		ret = wl_iw_ioctl(net, ifr, cmd);
7645 		DHD_PERIM_UNLOCK(&dhd->pub);
7646 		DHD_OS_WAKE_UNLOCK(&dhd->pub);
7647 		return ret;
7648 	}
7649 #endif /* defined(WL_WIRELESS_EXT) */
7650 
7651 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 2)
7652 	if (cmd == SIOCETHTOOL) {
7653 		ret = dhd_ethtool(dhd, (void*)ifr->ifr_data);
7654 		DHD_PERIM_UNLOCK(&dhd->pub);
7655 		DHD_OS_WAKE_UNLOCK(&dhd->pub);
7656 		return ret;
7657 	}
7658 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 2) */
7659 
7660 	if (cmd == SIOCDEVPRIVATE+1) {
7661 		ret = wl_android_priv_cmd(net, ifr, cmd);
7662 		dhd_check_hang(net, &dhd->pub, ret);
7663 		DHD_PERIM_UNLOCK(&dhd->pub);
7664 		DHD_OS_WAKE_UNLOCK(&dhd->pub);
7665 		return ret;
7666 	}
7667 
7668 	if (cmd != SIOCDEVPRIVATE) {
7669 		DHD_PERIM_UNLOCK(&dhd->pub);
7670 		DHD_OS_WAKE_UNLOCK(&dhd->pub);
7671 		return -EOPNOTSUPP;
7672 	}
7673 
7674 	memset(&ioc, 0, sizeof(ioc));
7675 
7676 #ifdef CONFIG_COMPAT
7677 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0))
7678 	if (in_compat_syscall())
7679 #else
7680 	if (is_compat_task())
7681 #endif
7682 	{
7683 		compat_wl_ioctl_t compat_ioc;
7684 		if (copy_from_user(&compat_ioc, ifr->ifr_data, sizeof(compat_wl_ioctl_t))) {
7685 			bcmerror = BCME_BADADDR;
7686 			goto done;
7687 		}
7688 		ioc.cmd = compat_ioc.cmd;
7689 		if (ioc.cmd & WLC_SPEC_FLAG) {
7690 			memset(&ioc, 0, sizeof(ioc));
7691 			/* Copy the ioc control structure part of ioctl request */
7692 			if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
7693 				bcmerror = BCME_BADADDR;
7694 				goto done;
7695 			}
7696 			ioc.cmd &= ~WLC_SPEC_FLAG; /* Clear the FLAG */
7697 
7698 			/* To differentiate between wl and dhd read 4 more byes */
7699 			if ((copy_from_user(&ioc.driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
7700 				sizeof(uint)) != 0)) {
7701 				bcmerror = BCME_BADADDR;
7702 				goto done;
7703 			}
7704 
7705 		} else { /* ioc.cmd & WLC_SPEC_FLAG */
7706 			ioc.buf = compat_ptr(compat_ioc.buf);
7707 			ioc.len = compat_ioc.len;
7708 			ioc.set = compat_ioc.set;
7709 			ioc.used = compat_ioc.used;
7710 			ioc.needed = compat_ioc.needed;
7711 			/* To differentiate between wl and dhd read 4 more byes */
7712 			if ((copy_from_user(&ioc.driver, (char *)ifr->ifr_data + sizeof(compat_wl_ioctl_t),
7713 				sizeof(uint)) != 0)) {
7714 				bcmerror = BCME_BADADDR;
7715 				goto done;
7716 			}
7717 		} /* ioc.cmd & WLC_SPEC_FLAG */
7718 	} else
7719 #endif /* CONFIG_COMPAT */
7720 	{
7721 		/* Copy the ioc control structure part of ioctl request */
7722 		if (copy_from_user(&ioc, ifr->ifr_data, sizeof(wl_ioctl_t))) {
7723 			bcmerror = BCME_BADADDR;
7724 			goto done;
7725 		}
7726 #ifdef CONFIG_COMPAT
7727 		ioc.cmd &= ~WLC_SPEC_FLAG; /* make sure it was clear when it isn't a compat task*/
7728 #endif
7729 
7730 		/* To differentiate between wl and dhd read 4 more byes */
7731 		if ((copy_from_user(&ioc.driver, (char *)ifr->ifr_data + sizeof(wl_ioctl_t),
7732 			sizeof(uint)) != 0)) {
7733 			bcmerror = BCME_BADADDR;
7734 			goto done;
7735 		}
7736 	}
7737 
7738 #ifndef CONFIG_VTS_SUPPORT
7739 	if (!capable(CAP_NET_ADMIN)) {
7740 		bcmerror = BCME_EPERM;
7741 		goto done;
7742 	}
7743 #endif
7744 
7745 	/* Take backup of ioc.buf and restore later */
7746 	ioc_buf_user = ioc.buf;
7747 
7748 	if (ioc.len > 0) {
7749 		buflen = MIN(ioc.len, DHD_IOCTL_MAXLEN);
7750 		if (!(local_buf = MALLOC(dhd->pub.osh, buflen+1))) {
7751 			bcmerror = BCME_NOMEM;
7752 			goto done;
7753 		}
7754 
7755 		DHD_PERIM_UNLOCK(&dhd->pub);
7756 		if (copy_from_user(local_buf, ioc.buf, buflen)) {
7757 			DHD_PERIM_LOCK(&dhd->pub);
7758 			bcmerror = BCME_BADADDR;
7759 			goto done;
7760 		}
7761 		DHD_PERIM_LOCK(&dhd->pub);
7762 
7763 		*((char *)local_buf + buflen) = '\0';
7764 
7765 		/* For some platforms accessing userspace memory
7766 		 * of ioc.buf is causing kernel panic, so to avoid that
7767 		 * make ioc.buf pointing to kernel space memory local_buf
7768 		 */
7769 		ioc.buf = local_buf;
7770 	}
7771 
7772 	/* Skip all the non DHD iovars (wl iovars) after f/w hang */
7773 	if (ioc.driver != DHD_IOCTL_MAGIC && dhd->pub.hang_was_sent) {
7774 		DHD_TRACE(("%s: HANG was sent up earlier\n", __FUNCTION__));
7775 		DHD_OS_WAKE_LOCK_CTRL_TIMEOUT_ENABLE(&dhd->pub, DHD_EVENT_TIMEOUT_MS);
7776 		bcmerror = BCME_DONGLE_DOWN;
7777 		goto done;
7778 	}
7779 
7780 	bcmerror = dhd_ioctl_process(&dhd->pub, ifidx, &ioc, local_buf);
7781 
7782 	/* Restore back userspace pointer to ioc.buf */
7783 	ioc.buf = ioc_buf_user;
7784 
7785 	if (!bcmerror && buflen && local_buf && ioc.buf) {
7786 		DHD_PERIM_UNLOCK(&dhd->pub);
7787 		if (copy_to_user(ioc.buf, local_buf, buflen))
7788 			bcmerror = -EFAULT;
7789 		DHD_PERIM_LOCK(&dhd->pub);
7790 	}
7791 
7792 done:
7793 	if (local_buf)
7794 		MFREE(dhd->pub.osh, local_buf, buflen+1);
7795 
7796 	DHD_PERIM_UNLOCK(&dhd->pub);
7797 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
7798 
7799 	return OSL_ERROR(bcmerror);
7800 }
7801 
7802 
7803 #ifdef FIX_CPU_MIN_CLOCK
dhd_init_cpufreq_fix(dhd_info_t * dhd)7804 static int dhd_init_cpufreq_fix(dhd_info_t *dhd)
7805 {
7806 	if (dhd) {
7807 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
7808 		mutex_init(&dhd->cpufreq_fix);
7809 #endif
7810 		dhd->cpufreq_fix_status = FALSE;
7811 	}
7812 	return 0;
7813 }
7814 
dhd_fix_cpu_freq(dhd_info_t * dhd)7815 static void dhd_fix_cpu_freq(dhd_info_t *dhd)
7816 {
7817 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
7818 	mutex_lock(&dhd->cpufreq_fix);
7819 #endif
7820 	if (dhd && !dhd->cpufreq_fix_status) {
7821 		pm_qos_add_request(&dhd->dhd_cpu_qos, PM_QOS_CPU_FREQ_MIN, 300000);
7822 #ifdef FIX_BUS_MIN_CLOCK
7823 		pm_qos_add_request(&dhd->dhd_bus_qos, PM_QOS_BUS_THROUGHPUT, 400000);
7824 #endif /* FIX_BUS_MIN_CLOCK */
7825 		DHD_ERROR(("pm_qos_add_requests called\n"));
7826 
7827 		dhd->cpufreq_fix_status = TRUE;
7828 	}
7829 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
7830 	mutex_unlock(&dhd->cpufreq_fix);
7831 #endif
7832 }
7833 
dhd_rollback_cpu_freq(dhd_info_t * dhd)7834 static void dhd_rollback_cpu_freq(dhd_info_t *dhd)
7835 {
7836 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
7837 	mutex_lock(&dhd ->cpufreq_fix);
7838 #endif
7839 	if (dhd && dhd->cpufreq_fix_status != TRUE) {
7840 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
7841 		mutex_unlock(&dhd->cpufreq_fix);
7842 #endif
7843 		return;
7844 	}
7845 
7846 	pm_qos_remove_request(&dhd->dhd_cpu_qos);
7847 #ifdef FIX_BUS_MIN_CLOCK
7848 	pm_qos_remove_request(&dhd->dhd_bus_qos);
7849 #endif /* FIX_BUS_MIN_CLOCK */
7850 	DHD_ERROR(("pm_qos_add_requests called\n"));
7851 
7852 	dhd->cpufreq_fix_status = FALSE;
7853 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
7854 	mutex_unlock(&dhd->cpufreq_fix);
7855 #endif
7856 }
7857 #endif /* FIX_CPU_MIN_CLOCK */
7858 
7859 #if defined(BT_OVER_SDIO)
7860 
7861 void
dhdsdio_bus_usr_cnt_inc(dhd_pub_t * dhdp)7862 dhdsdio_bus_usr_cnt_inc(dhd_pub_t *dhdp)
7863 {
7864 	dhdp->info->bus_user_count++;
7865 }
7866 
7867 void
dhdsdio_bus_usr_cnt_dec(dhd_pub_t * dhdp)7868 dhdsdio_bus_usr_cnt_dec(dhd_pub_t *dhdp)
7869 {
7870 	dhdp->info->bus_user_count--;
7871 }
7872 
7873 /* Return values:
7874  * Success: Returns 0
7875  * Failure: Returns -1 or errono code
7876  */
7877 int
dhd_bus_get(wlan_bt_handle_t handle,bus_owner_t owner)7878 dhd_bus_get(wlan_bt_handle_t handle, bus_owner_t owner)
7879 {
7880 	dhd_pub_t *dhdp = (dhd_pub_t *)handle;
7881 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
7882 	int ret = 0;
7883 
7884 	mutex_lock(&dhd->bus_user_lock);
7885 	++dhd->bus_user_count;
7886 	if (dhd->bus_user_count < 0) {
7887 		DHD_ERROR(("%s(): bus_user_count is negative, which is invalid\n", __FUNCTION__));
7888 		ret = -1;
7889 		goto exit;
7890 	}
7891 
7892 	if (dhd->bus_user_count == 1) {
7893 
7894 		dhd->pub.hang_was_sent = 0;
7895 
7896 		/* First user, turn on WL_REG, start the bus */
7897 		DHD_ERROR(("%s(): First user Turn On WL_REG & start the bus", __FUNCTION__));
7898 
7899 		if (!wifi_platform_set_power(dhd->adapter, TRUE, WIFI_TURNON_DELAY)) {
7900 			/* Enable F1 */
7901 			ret = dhd_bus_resume(dhdp, 0);
7902 			if (ret) {
7903 				DHD_ERROR(("%s(): Failed to enable F1, err=%d\n",
7904 					__FUNCTION__, ret));
7905 				goto exit;
7906 			}
7907 		}
7908 
7909 		dhd_update_fw_nv_path(dhd);
7910 		/* update firmware and nvram path to sdio bus */
7911 		dhd_bus_update_fw_nv_path(dhd->pub.bus,
7912 			dhd->fw_path, dhd->nv_path);
7913 		/* download the firmware, Enable F2 */
7914 		/* TODO: Should be done only in case of FW switch */
7915 		ret = dhd_bus_devreset(dhdp, FALSE);
7916 		dhd_bus_resume(dhdp, 1);
7917 		if (!ret) {
7918 			if (dhd_sync_with_dongle(&dhd->pub) < 0) {
7919 				DHD_ERROR(("%s(): Sync with dongle failed!!\n", __FUNCTION__));
7920 				ret = -EFAULT;
7921 			}
7922 		} else {
7923 			DHD_ERROR(("%s(): Failed to download, err=%d\n", __FUNCTION__, ret));
7924 		}
7925 	} else {
7926 		DHD_ERROR(("%s(): BUS is already acquired, just increase the count %d \r\n",
7927 			__FUNCTION__, dhd->bus_user_count));
7928 	}
7929 exit:
7930 	mutex_unlock(&dhd->bus_user_lock);
7931 	return ret;
7932 }
7933 EXPORT_SYMBOL(dhd_bus_get);
7934 
7935 /* Return values:
7936  * Success: Returns 0
7937  * Failure: Returns -1 or errono code
7938  */
7939 int
dhd_bus_put(wlan_bt_handle_t handle,bus_owner_t owner)7940 dhd_bus_put(wlan_bt_handle_t handle, bus_owner_t owner)
7941 {
7942 	dhd_pub_t *dhdp = (dhd_pub_t *)handle;
7943 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
7944 	int ret = 0;
7945 	BCM_REFERENCE(owner);
7946 
7947 	mutex_lock(&dhd->bus_user_lock);
7948 	--dhd->bus_user_count;
7949 	if (dhd->bus_user_count < 0) {
7950 		DHD_ERROR(("%s(): bus_user_count is negative, which is invalid\n", __FUNCTION__));
7951 		dhd->bus_user_count = 0;
7952 		ret = -1;
7953 		goto exit;
7954 	}
7955 
7956 	if (dhd->bus_user_count == 0) {
7957 		/* Last user, stop the bus and turn Off WL_REG */
7958 		DHD_ERROR(("%s(): There are no owners left Trunf Off WL_REG & stop the bus \r\n",
7959 			__FUNCTION__));
7960 #ifdef PROP_TXSTATUS
7961 		if (dhd->pub.wlfc_enabled) {
7962 			dhd_wlfc_deinit(&dhd->pub);
7963 		}
7964 #endif /* PROP_TXSTATUS */
7965 #ifdef PNO_SUPPORT
7966 		if (dhd->pub.pno_state) {
7967 			dhd_pno_deinit(&dhd->pub);
7968 		}
7969 #endif /* PNO_SUPPORT */
7970 #ifdef RTT_SUPPORT
7971 		if (dhd->pub.rtt_state) {
7972 			dhd_rtt_deinit(&dhd->pub);
7973 		}
7974 #endif /* RTT_SUPPORT */
7975 		ret = dhd_bus_devreset(dhdp, TRUE);
7976 		if (!ret) {
7977 			dhd_bus_suspend(dhdp);
7978 			wifi_platform_set_power(dhd->adapter, FALSE, WIFI_TURNOFF_DELAY);
7979 		}
7980 	} else {
7981 		DHD_ERROR(("%s(): Other owners using bus, decrease the count %d \r\n",
7982 			__FUNCTION__, dhd->bus_user_count));
7983 	}
7984 exit:
7985 	mutex_unlock(&dhd->bus_user_lock);
7986 	return ret;
7987 }
7988 EXPORT_SYMBOL(dhd_bus_put);
7989 
7990 int
dhd_net_bus_get(struct net_device * dev)7991 dhd_net_bus_get(struct net_device *dev)
7992 {
7993 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
7994 	return dhd_bus_get(&dhd->pub, WLAN_MODULE);
7995 }
7996 
7997 int
dhd_net_bus_put(struct net_device * dev)7998 dhd_net_bus_put(struct net_device *dev)
7999 {
8000 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
8001 	return dhd_bus_put(&dhd->pub, WLAN_MODULE);
8002 }
8003 
8004 /*
8005  * Function to enable the Bus Clock
8006  * Returns BCME_OK on success and BCME_xxx on failure
8007  *
8008  * This function is not callable from non-sleepable context
8009  */
dhd_bus_clk_enable(wlan_bt_handle_t handle,bus_owner_t owner)8010 int dhd_bus_clk_enable(wlan_bt_handle_t handle, bus_owner_t owner)
8011 {
8012 	dhd_pub_t *dhdp = (dhd_pub_t *)handle;
8013 
8014 	int ret;
8015 
8016 	dhd_os_sdlock(dhdp);
8017 	/*
8018 	 * The second argument is TRUE, that means, we expect
8019 	 * the function to "wait" until the clocks are really
8020 	 * available
8021 	 */
8022 	ret = __dhdsdio_clk_enable(dhdp->bus, owner, TRUE);
8023 	dhd_os_sdunlock(dhdp);
8024 
8025 	return ret;
8026 }
8027 EXPORT_SYMBOL(dhd_bus_clk_enable);
8028 
8029 /*
8030  * Function to disable the Bus Clock
8031  * Returns BCME_OK on success and BCME_xxx on failure
8032  *
8033  * This function is not callable from non-sleepable context
8034  */
dhd_bus_clk_disable(wlan_bt_handle_t handle,bus_owner_t owner)8035 int dhd_bus_clk_disable(wlan_bt_handle_t handle, bus_owner_t owner)
8036 {
8037 	dhd_pub_t *dhdp = (dhd_pub_t *)handle;
8038 
8039 	int ret;
8040 
8041 	dhd_os_sdlock(dhdp);
8042 	/*
8043 	 * The second argument is TRUE, that means, we expect
8044 	 * the function to "wait" until the clocks are really
8045 	 * disabled
8046 	 */
8047 	ret = __dhdsdio_clk_disable(dhdp->bus, owner, TRUE);
8048 	dhd_os_sdunlock(dhdp);
8049 
8050 	return ret;
8051 }
8052 EXPORT_SYMBOL(dhd_bus_clk_disable);
8053 
8054 /*
8055  * Function to reset bt_use_count counter to zero.
8056  *
8057  * This function is not callable from non-sleepable context
8058  */
dhd_bus_reset_bt_use_count(wlan_bt_handle_t handle)8059 void dhd_bus_reset_bt_use_count(wlan_bt_handle_t handle)
8060 {
8061 	dhd_pub_t *dhdp = (dhd_pub_t *)handle;
8062 
8063 	/* take the lock and reset bt use count */
8064 	dhd_os_sdlock(dhdp);
8065 	dhdsdio_reset_bt_use_count(dhdp->bus);
8066 	dhd_os_sdunlock(dhdp);
8067 }
8068 EXPORT_SYMBOL(dhd_bus_reset_bt_use_count);
8069 
8070 #endif /* BT_OVER_SDIO */
8071 
8072 #define MAX_TRY_CNT             5 /* Number of tries to disable deepsleep */
dhd_deepsleep(dhd_info_t * dhd,int flag)8073 int dhd_deepsleep(dhd_info_t *dhd, int flag)
8074 {
8075 	char iovbuf[20];
8076 	uint powervar = 0;
8077 	dhd_pub_t *dhdp;
8078 	int cnt = 0;
8079 	int ret = 0;
8080 
8081 	dhdp = &dhd->pub;
8082 
8083 	switch (flag) {
8084 		case 1 :  /* Deepsleep on */
8085 			DHD_ERROR(("dhd_deepsleep: ON\n"));
8086 			/* give some time to sysioc_work before deepsleep */
8087 			OSL_SLEEP(200);
8088 #ifdef PKT_FILTER_SUPPORT
8089 			/* disable pkt filter */
8090 			dhd_enable_packet_filter(0, dhdp);
8091 #endif /* PKT_FILTER_SUPPORT */
8092 			/* Disable MPC */
8093 			powervar = 0;
8094 			memset(iovbuf, 0, sizeof(iovbuf));
8095 			bcm_mkiovar("mpc", (char *)&powervar, 4, iovbuf, sizeof(iovbuf));
8096 			dhd_wl_ioctl_cmd(dhdp, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
8097 
8098 			/* Enable Deepsleep */
8099 			powervar = 1;
8100 			memset(iovbuf, 0, sizeof(iovbuf));
8101 			bcm_mkiovar("deepsleep", (char *)&powervar, 4, iovbuf, sizeof(iovbuf));
8102 			dhd_wl_ioctl_cmd(dhdp, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
8103 			break;
8104 
8105 		case 0: /* Deepsleep Off */
8106 			DHD_ERROR(("dhd_deepsleep: OFF\n"));
8107 
8108 			/* Disable Deepsleep */
8109 			for (cnt = 0; cnt < MAX_TRY_CNT; cnt++) {
8110 				powervar = 0;
8111 				memset(iovbuf, 0, sizeof(iovbuf));
8112 				bcm_mkiovar("deepsleep", (char *)&powervar, 4,
8113 					iovbuf, sizeof(iovbuf));
8114 				dhd_wl_ioctl_cmd(dhdp, WLC_SET_VAR, iovbuf,
8115 					sizeof(iovbuf), TRUE, 0);
8116 
8117 				memset(iovbuf, 0, sizeof(iovbuf));
8118 				bcm_mkiovar("deepsleep", (char *)&powervar, 4,
8119 					iovbuf, sizeof(iovbuf));
8120 				if ((ret = dhd_wl_ioctl_cmd(dhdp, WLC_GET_VAR, iovbuf,
8121 					sizeof(iovbuf),	FALSE, 0)) < 0) {
8122 					DHD_ERROR(("the error of dhd deepsleep status"
8123 						" ret value :%d\n", ret));
8124 				} else {
8125 					if (!(*(int *)iovbuf)) {
8126 						DHD_ERROR(("deepsleep mode is 0,"
8127 							" count: %d\n", cnt));
8128 						break;
8129 					}
8130 				}
8131 			}
8132 
8133 			/* Enable MPC */
8134 			powervar = 1;
8135 			memset(iovbuf, 0, sizeof(iovbuf));
8136 			bcm_mkiovar("mpc", (char *)&powervar, 4, iovbuf, sizeof(iovbuf));
8137 			dhd_wl_ioctl_cmd(dhdp, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
8138 			break;
8139 	}
8140 
8141 	return 0;
8142 }
8143 
8144 static int
dhd_stop(struct net_device * net)8145 dhd_stop(struct net_device *net)
8146 {
8147 	int ifidx = 0;
8148 #ifdef WL_CFG80211
8149 	unsigned long flags = 0;
8150 #endif /* WL_CFG80211 */
8151 	dhd_info_t *dhd = DHD_DEV_INFO(net);
8152 	DHD_OS_WAKE_LOCK(&dhd->pub);
8153 	DHD_PERIM_LOCK(&dhd->pub);
8154 	DHD_PRINT("%s: Enter %p\n", __FUNCTION__, net);
8155 	dhd->pub.rxcnt_timeout = 0;
8156 	dhd->pub.txcnt_timeout = 0;
8157 
8158 #ifdef BCMPCIE
8159 	dhd->pub.d3ackcnt_timeout = 0;
8160 #endif /* BCMPCIE */
8161 
8162 	if (dhd->pub.up == 0) {
8163 		goto exit;
8164 	}
8165 #ifdef DHD_LOAD_CHIPALIVE
8166 	if (dhd->pub.conf->suspended) {
8167 		return 0;
8168 	}
8169 #endif
8170 #if defined(DHD_HANG_SEND_UP_TEST)
8171 	if (dhd->pub.req_hang_type) {
8172 		DHD_ERROR(("%s, Clear HANG test request 0x%x\n",
8173 			__FUNCTION__, dhd->pub.req_hang_type));
8174 		dhd->pub.req_hang_type = 0;
8175 	}
8176 #endif /* DHD_HANG_SEND_UP_TEST */
8177 
8178 	dhd_if_flush_sta(DHD_DEV_IFP(net));
8179 
8180 	/* Disable Runtime PM before interface down */
8181 	DHD_DISABLE_RUNTIME_PM(&dhd->pub);
8182 
8183 #ifdef FIX_CPU_MIN_CLOCK
8184 	if (dhd_get_fw_mode(dhd) == DHD_FLAG_HOSTAP_MODE)
8185 		dhd_rollback_cpu_freq(dhd);
8186 #endif /* FIX_CPU_MIN_CLOCK */
8187 
8188 	ifidx = dhd_net2idx(dhd, net);
8189 	BCM_REFERENCE(ifidx);
8190 
8191 	/* Set state and stop OS transmissions */
8192 	netif_stop_queue(net);
8193 #ifdef WL_CFG80211
8194 	spin_lock_irqsave(&dhd->pub.up_lock, flags);
8195 	dhd->pub.up = 0;
8196 	spin_unlock_irqrestore(&dhd->pub.up_lock, flags);
8197 #else
8198 	dhd->pub.up = 0;
8199 #endif /* WL_CFG80211 */
8200 
8201 #ifdef WL_CFG80211
8202 	if (ifidx == 0) {
8203 		dhd_if_t *ifp;
8204 		wl_cfg80211_down(net);
8205 
8206 		ifp = dhd->iflist[0];
8207 		ASSERT(ifp && ifp->net);
8208 		/*
8209 		 * For CFG80211: Clean up all the left over virtual interfaces
8210 		 * when the primary Interface is brought down. [ifconfig wlan0 down]
8211 		 */
8212 		if (!dhd_download_fw_on_driverload) {
8213 			if ((dhd->dhd_state & DHD_ATTACH_STATE_ADD_IF) &&
8214 				(dhd->dhd_state & DHD_ATTACH_STATE_CFG80211)) {
8215 				int i;
8216 #ifdef WL_CFG80211_P2P_DEV_IF
8217 				wl_cfg80211_del_p2p_wdev(net);
8218 #endif /* WL_CFG80211_P2P_DEV_IF */
8219 
8220 				dhd_net_if_lock_local(dhd);
8221 				for (i = 1; i < DHD_MAX_IFS; i++)
8222 					dhd_remove_if(&dhd->pub, i, FALSE);
8223 
8224 				if (ifp && ifp->net) {
8225 					dhd_if_del_sta_list(ifp);
8226 				}
8227 #ifdef ARP_OFFLOAD_SUPPORT
8228 				if (dhd_inetaddr_notifier_registered) {
8229 					dhd_inetaddr_notifier_registered = FALSE;
8230 					unregister_inetaddr_notifier(&dhd_inetaddr_notifier);
8231 				}
8232 #endif /* ARP_OFFLOAD_SUPPORT */
8233 #if defined(CONFIG_IPV6) && defined(IPV6_NDO_SUPPORT)
8234 				if (dhd_inet6addr_notifier_registered) {
8235 					dhd_inet6addr_notifier_registered = FALSE;
8236 					unregister_inet6addr_notifier(&dhd_inet6addr_notifier);
8237 				}
8238 #endif /* CONFIG_IPV6 && IPV6_NDO_SUPPORT */
8239 				dhd_net_if_unlock_local(dhd);
8240 			}
8241 #if 0
8242 			// terence 20161024: remove this to prevent dev_close() get stuck in dhd_hang_process
8243 			cancel_work_sync(dhd->dhd_deferred_wq);
8244 #endif
8245 
8246 #ifdef SHOW_LOGTRACE
8247 			/* Wait till event_log_dispatcher_work finishes */
8248 			cancel_work_sync(&dhd->event_log_dispatcher_work);
8249 #endif /* SHOW_LOGTRACE */
8250 
8251 #if defined(DHD_LB_RXP)
8252 			__skb_queue_purge(&dhd->rx_pend_queue);
8253 #endif /* DHD_LB_RXP */
8254 
8255 #if defined(DHD_LB_TXP)
8256 			skb_queue_purge(&dhd->tx_pend_queue);
8257 #endif /* DHD_LB_TXP */
8258 		}
8259 
8260 		argos_register_notifier_deinit();
8261 #ifdef DHDTCPACK_SUPPRESS
8262 		dhd_tcpack_suppress_set(&dhd->pub, TCPACK_SUP_OFF);
8263 #endif /* DHDTCPACK_SUPPRESS */
8264 #if defined(DHD_LB_RXP)
8265 		if (ifp->net == dhd->rx_napi_netdev) {
8266 			DHD_INFO(("%s napi<%p> disabled ifp->net<%p,%s>\n",
8267 				__FUNCTION__, &dhd->rx_napi_struct, net, net->name));
8268 			skb_queue_purge(&dhd->rx_napi_queue);
8269 			napi_disable(&dhd->rx_napi_struct);
8270 			netif_napi_del(&dhd->rx_napi_struct);
8271 			dhd->rx_napi_netdev = NULL;
8272 		}
8273 #endif /* DHD_LB_RXP */
8274 	}
8275 #endif /* WL_CFG80211 */
8276 
8277 	DHD_SSSR_DUMP_DEINIT(&dhd->pub);
8278 
8279 #ifdef PROP_TXSTATUS
8280 	dhd_wlfc_cleanup(&dhd->pub, NULL, 0);
8281 #endif
8282 #ifdef SHOW_LOGTRACE
8283 	if (!dhd_download_fw_on_driverload) {
8284 		/* Release the skbs from queue for WLC_E_TRACE event */
8285 		dhd_event_logtrace_flush_queue(&dhd->pub);
8286 		if (dhd->dhd_state & DHD_ATTACH_LOGTRACE_INIT) {
8287 			if (dhd->event_data.fmts) {
8288 				MFREE(dhd->pub.osh, dhd->event_data.fmts,
8289 					dhd->event_data.fmts_size);
8290 				dhd->event_data.fmts = NULL;
8291 			}
8292 			if (dhd->event_data.raw_fmts) {
8293 				MFREE(dhd->pub.osh, dhd->event_data.raw_fmts,
8294 					dhd->event_data.raw_fmts_size);
8295 				dhd->event_data.raw_fmts = NULL;
8296 			}
8297 			if (dhd->event_data.raw_sstr) {
8298 				MFREE(dhd->pub.osh, dhd->event_data.raw_sstr,
8299 					dhd->event_data.raw_sstr_size);
8300 				dhd->event_data.raw_sstr = NULL;
8301 			}
8302 			if (dhd->event_data.rom_raw_sstr) {
8303 				MFREE(dhd->pub.osh, dhd->event_data.rom_raw_sstr,
8304 					dhd->event_data.rom_raw_sstr_size);
8305 				dhd->event_data.rom_raw_sstr = NULL;
8306 			}
8307 			dhd->dhd_state &= ~DHD_ATTACH_LOGTRACE_INIT;
8308 		}
8309 	}
8310 #endif /* SHOW_LOGTRACE */
8311 #ifdef APF
8312 	dhd_dev_apf_delete_filter(net);
8313 #endif /* APF */
8314 
8315 	/* Stop the protocol module */
8316 	dhd_prot_stop(&dhd->pub);
8317 
8318 	OLD_MOD_DEC_USE_COUNT;
8319 exit:
8320 #if defined(WL_WIRELESS_EXT)
8321 	if (ifidx == 0) {
8322 		wl_iw_down(net, &dhd->pub);
8323 	}
8324 #endif /* defined(WL_WIRELESS_EXT) */
8325 #ifdef WL_ESCAN
8326 	if (ifidx == 0) {
8327 		wl_escan_down(net, &dhd->pub);
8328 	}
8329 #endif /* WL_ESCAN */
8330 	if (ifidx == 0 && !dhd_download_fw_on_driverload) {
8331 #if defined(BT_OVER_SDIO)
8332 		dhd_bus_put(&dhd->pub, WLAN_MODULE);
8333 		wl_android_set_wifi_on_flag(FALSE);
8334 #else
8335 		wl_android_wifi_off(net, TRUE);
8336 #if defined(WL_EXT_IAPSTA) || defined(USE_IW) || defined(WL_ESCAN)
8337 #ifdef WL_EXT_IAPSTA
8338 		wl_ext_iapsta_dettach_netdev(net, ifidx);
8339 #endif /* WL_EXT_IAPSTA */
8340 #ifdef WL_ESCAN
8341 			wl_escan_event_dettach(net, &dhd->pub);
8342 #endif /* WL_ESCAN */
8343 			wl_ext_event_dettach_netdev(net, ifidx);
8344 #endif /* WL_EXT_IAPSTA || USE_IW || WL_ESCAN */
8345 	} else {
8346 		if (dhd->pub.conf->deepsleep)
8347 			dhd_deepsleep(dhd, 1);
8348 #endif /* BT_OVER_SDIO */
8349 	}
8350 	dhd->pub.hang_was_sent = 0;
8351 
8352 	/* Clear country spec for for built-in type driver */
8353 	if (!dhd_download_fw_on_driverload) {
8354 		dhd->pub.dhd_cspec.country_abbrev[0] = 0x00;
8355 		dhd->pub.dhd_cspec.rev = 0;
8356 		dhd->pub.dhd_cspec.ccode[0] = 0x00;
8357 	}
8358 
8359 #ifdef BCMDBGFS
8360 	dhd_dbgfs_remove();
8361 #endif
8362 
8363 	DHD_PERIM_UNLOCK(&dhd->pub);
8364 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
8365 
8366 	/* Destroy wakelock */
8367 	if (!dhd_download_fw_on_driverload &&
8368 		(dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
8369 		DHD_OS_WAKE_LOCK_DESTROY(dhd);
8370 		dhd->dhd_state &= ~DHD_ATTACH_STATE_WAKELOCKS_INIT;
8371 	}
8372 	DHD_PRINT("%s: Exit\n", __FUNCTION__);
8373 
8374 	return 0;
8375 }
8376 
8377 #if defined(WL_CFG80211) && defined(USE_INITIAL_SHORT_DWELL_TIME)
8378 extern bool g_first_broadcast_scan;
8379 #endif
8380 
8381 #ifdef WL11U
dhd_interworking_enable(dhd_pub_t * dhd)8382 static int dhd_interworking_enable(dhd_pub_t *dhd)
8383 {
8384 	uint32 enable = true;
8385 	int ret = BCME_OK;
8386 
8387 	ret = dhd_iovar(dhd, 0, "interworking", (char *)&enable, sizeof(enable), NULL, 0, TRUE);
8388 	if (ret < 0) {
8389 		DHD_ERROR(("%s: enableing interworking failed, ret=%d\n", __FUNCTION__, ret));
8390 	}
8391 
8392 	return ret;
8393 }
8394 #endif /* WL11u */
8395 
8396 static int
dhd_open(struct net_device * net)8397 dhd_open(struct net_device *net)
8398 {
8399 	dhd_info_t *dhd = DHD_DEV_INFO(net);
8400 #ifdef TOE
8401 	uint32 toe_ol;
8402 #endif
8403 #ifdef BCM_FD_AGGR
8404 	char iovbuf[WLC_IOCTL_SMLEN];
8405 	dbus_config_t config;
8406 	uint32 agglimit = 0;
8407 	uint32 rpc_agg = BCM_RPC_TP_DNGL_AGG_DPC; /* host aggr not enabled yet */
8408 #endif /* BCM_FD_AGGR */
8409 	int ifidx;
8410 	int32 ret = 0;
8411 #if defined(OOB_INTR_ONLY)
8412 	uint32 bus_type = -1;
8413 	uint32 bus_num = -1;
8414 	uint32 slot_num = -1;
8415 	wifi_adapter_info_t *adapter = NULL;
8416 #endif
8417 #if (defined(WL_EXT_IAPSTA) && defined(ISAM_PREINIT)) || defined(DHD_LOAD_CHIPALIVE)
8418 	int bytes_written = 0;
8419 	int retry = 0;
8420 #endif
8421 
8422 	if (!dhd_download_fw_on_driverload) {
8423 		if (!dhd_driver_init_done) {
8424 			DHD_ERROR(("%s: WLAN driver is not initialized\n", __FUNCTION__));
8425 			return -1;
8426 		}
8427 	}
8428 
8429 	printf("%s: Enter %p\n", __FUNCTION__, net);
8430 	DHD_MUTEX_LOCK();
8431 	/* Init wakelock */
8432 	if (!dhd_download_fw_on_driverload) {
8433 		if (!(dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
8434 			DHD_OS_WAKE_LOCK_INIT(dhd);
8435 			dhd->dhd_state |= DHD_ATTACH_STATE_WAKELOCKS_INIT;
8436 		}
8437 #ifdef SHOW_LOGTRACE
8438 		skb_queue_head_init(&dhd->evt_trace_queue);
8439 
8440 		if (!(dhd->dhd_state & DHD_ATTACH_LOGTRACE_INIT)) {
8441 			ret = dhd_init_logstrs_array(dhd->pub.osh, &dhd->event_data);
8442 			if (ret == BCME_OK) {
8443 				dhd_init_static_strs_array(dhd->pub.osh, &dhd->event_data,
8444 					st_str_file_path, map_file_path);
8445 				dhd_init_static_strs_array(dhd->pub.osh, &dhd->event_data,
8446 					rom_st_str_file_path, rom_map_file_path);
8447 				dhd->dhd_state |= DHD_ATTACH_LOGTRACE_INIT;
8448 			}
8449 		}
8450 #endif /* SHOW_LOGTRACE */
8451 	}
8452 
8453 #if defined(PREVENT_REOPEN_DURING_HANG)
8454 	/* WAR : to prevent calling dhd_open abnormally in quick succession after hang event */
8455 	if (dhd->pub.hang_was_sent == 1) {
8456 		DHD_ERROR(("%s: HANG was sent up earlier\n", __FUNCTION__));
8457 		/* Force to bring down WLAN interface in case dhd_stop() is not called
8458 		 * from the upper layer when HANG event is triggered.
8459 		 */
8460 		if (!dhd_download_fw_on_driverload && dhd->pub.up == 1) {
8461 			DHD_ERROR(("%s: WLAN interface is not brought down\n", __FUNCTION__));
8462 			dhd_stop(net);
8463 		} else {
8464 			DHD_MUTEX_UNLOCK();
8465 			return -1;
8466 		}
8467 	}
8468 #endif /* PREVENT_REOPEN_DURING_HANG */
8469 
8470 
8471 	DHD_OS_WAKE_LOCK(&dhd->pub);
8472 	DHD_PERIM_LOCK(&dhd->pub);
8473 	dhd->pub.dongle_trap_occured = 0;
8474 	dhd->pub.hang_was_sent = 0;
8475 	dhd->pub.hang_reason = 0;
8476 	dhd->pub.iovar_timeout_occured = 0;
8477 #ifdef PCIE_FULL_DONGLE
8478 	dhd->pub.d3ack_timeout_occured = 0;
8479 #endif /* PCIE_FULL_DONGLE */
8480 
8481 #ifdef DHD_LOSSLESS_ROAMING
8482 	dhd->pub.dequeue_prec_map = ALLPRIO;
8483 #endif
8484 #if 0
8485 	/*
8486 	 * Force start if ifconfig_up gets called before START command
8487 	 *  We keep WEXT's wl_control_wl_start to provide backward compatibility
8488 	 *  This should be removed in the future
8489 	 */
8490 	ret = wl_control_wl_start(net);
8491 	if (ret != 0) {
8492 		DHD_ERROR(("%s: failed with code %d\n", __FUNCTION__, ret));
8493 		ret = -1;
8494 		goto exit;
8495 	}
8496 #endif
8497 
8498 	ifidx = dhd_net2idx(dhd, net);
8499 	DHD_TRACE(("%s: ifidx %d\n", __FUNCTION__, ifidx));
8500 
8501 	if (ifidx < 0) {
8502 		DHD_ERROR(("%s: Error: called with invalid IF\n", __FUNCTION__));
8503 		ret = -1;
8504 		goto exit;
8505 	}
8506 
8507 	if (!dhd->iflist[ifidx]) {
8508 		DHD_ERROR(("%s: Error: called when IF already deleted\n", __FUNCTION__));
8509 		ret = -1;
8510 		goto exit;
8511 	}
8512 
8513 	if (ifidx == 0) {
8514 		atomic_set(&dhd->pend_8021x_cnt, 0);
8515 		if (!dhd_download_fw_on_driverload) {
8516 			DHD_ERROR(("\n%s\n", dhd_version));
8517 #if defined(WL_EXT_IAPSTA) || defined(USE_IW) || defined(WL_ESCAN)
8518 			wl_ext_event_attach_netdev(net, ifidx, dhd->iflist[ifidx]->bssidx);
8519 #ifdef WL_ESCAN
8520 			wl_escan_event_attach(net, &dhd->pub);
8521 #endif /* WL_ESCAN */
8522 #ifdef WL_EXT_IAPSTA
8523 			wl_ext_iapsta_attach_netdev(net, ifidx, dhd->iflist[ifidx]->bssidx);
8524 #endif /* WL_EXT_IAPSTA */
8525 #endif /* WL_EXT_IAPSTA || USE_IW || WL_ESCAN */
8526 #if defined(USE_INITIAL_SHORT_DWELL_TIME)
8527 			g_first_broadcast_scan = TRUE;
8528 #endif
8529 #ifdef DHD_LOAD_CHIPALIVE
8530 fail_retry:
8531 #endif
8532 #if defined(BT_OVER_SDIO)
8533 			ret = dhd_bus_get(&dhd->pub, WLAN_MODULE);
8534 			wl_android_set_wifi_on_flag(TRUE);
8535 #else
8536 			ret = wl_android_wifi_on(net);
8537 #endif /* BT_OVER_SDIO */
8538 			if (ret != 0) {
8539 #ifdef DHD_LOAD_CHIPALIVE
8540 				retry++;
8541 				if (dhd_chip_alive) {
8542 					DHD_ERROR(("Turn off dhd_chip_alive, retry=%d\n", retry));
8543 					dhd_chip_alive = 0;
8544 				}
8545 				if (retry < 3)
8546 					goto fail_retry;
8547 #endif
8548 				DHD_ERROR(("%s : wl_android_wifi_on failed (%d)\n",
8549 					__FUNCTION__, ret));
8550 				ret = -1;
8551 				goto exit;
8552 			}
8553 		}
8554 #ifdef FIX_CPU_MIN_CLOCK
8555 		if (dhd_get_fw_mode(dhd) == DHD_FLAG_HOSTAP_MODE) {
8556 			dhd_init_cpufreq_fix(dhd);
8557 			dhd_fix_cpu_freq(dhd);
8558 		}
8559 #endif /* FIX_CPU_MIN_CLOCK */
8560 #if defined(OOB_INTR_ONLY)
8561 		if (dhd->pub.conf->dpc_cpucore >= 0) {
8562 			dhd_bus_get_ids(dhd->pub.bus, &bus_type, &bus_num, &slot_num);
8563 			adapter = dhd_wifi_platform_get_adapter(bus_type, bus_num, slot_num);
8564 			if (adapter) {
8565 				printf("%s: set irq affinity hit %d\n", __FUNCTION__, dhd->pub.conf->dpc_cpucore);
8566 				irq_set_affinity_hint(adapter->irq_num, cpumask_of(dhd->pub.conf->dpc_cpucore));
8567 			}
8568 		}
8569 #endif
8570 
8571 		if (dhd->pub.busstate != DHD_BUS_DATA) {
8572 #ifdef BCMDBUS
8573 			dhd_set_path(&dhd->pub);
8574 			DHD_MUTEX_UNLOCK();
8575 			wait_event_interruptible_timeout(dhd->adapter->status_event,
8576 				wifi_get_adapter_status(dhd->adapter, WIFI_STATUS_FW_READY),
8577 				msecs_to_jiffies(DHD_FW_READY_TIMEOUT));
8578 			DHD_MUTEX_LOCK();
8579 			if ((ret = dbus_up(dhd->pub.bus)) != 0) {
8580 				DHD_ERROR(("%s: failed to dbus_up with code %d\n", __FUNCTION__, ret));
8581 				goto exit;
8582 			} else {
8583 				dhd->pub.busstate = DHD_BUS_DATA;
8584 			}
8585 			if ((ret = dhd_sync_with_dongle(&dhd->pub)) < 0) {
8586 				DHD_ERROR(("%s: failed with code %d\n", __FUNCTION__, ret));
8587 				goto exit;
8588 			}
8589 #else
8590 			/* try to bring up bus */
8591 			DHD_PERIM_UNLOCK(&dhd->pub);
8592 			ret = dhd_bus_start(&dhd->pub);
8593 			DHD_PERIM_LOCK(&dhd->pub);
8594 			if (ret) {
8595 				DHD_ERROR(("%s: failed with code %d\n", __FUNCTION__, ret));
8596 				ret = -1;
8597 				goto exit;
8598 			}
8599 #endif /* !BCMDBUS */
8600 
8601 		}
8602 #ifdef WL_EXT_IAPSTA
8603 		wl_ext_iapsta_attach_name(net, ifidx);
8604 #endif
8605 		if (dhd_download_fw_on_driverload) {
8606 			if (dhd->pub.conf->deepsleep)
8607 				dhd_deepsleep(dhd, 0);
8608 		}
8609 
8610 #ifdef BCM_FD_AGGR
8611 		config.config_id = DBUS_CONFIG_ID_AGGR_LIMIT;
8612 
8613 
8614 		memset(iovbuf, 0, sizeof(iovbuf));
8615 		bcm_mkiovar("rpc_dngl_agglimit", (char *)&agglimit, 4,
8616 			iovbuf, sizeof(iovbuf));
8617 
8618 		if (!dhd_wl_ioctl_cmd(&dhd->pub, WLC_GET_VAR, iovbuf, sizeof(iovbuf), FALSE, 0)) {
8619 			agglimit = *(uint32 *)iovbuf;
8620 			config.aggr_param.maxrxsf = agglimit >> BCM_RPC_TP_AGG_SF_SHIFT;
8621 			config.aggr_param.maxrxsize = agglimit & BCM_RPC_TP_AGG_BYTES_MASK;
8622 			DHD_ERROR(("rpc_dngl_agglimit %x : sf_limit %d bytes_limit %d\n",
8623 				agglimit, config.aggr_param.maxrxsf, config.aggr_param.maxrxsize));
8624 			if (bcm_rpc_tp_set_config(dhd->pub.info->rpc_th, &config)) {
8625 				DHD_ERROR(("set tx/rx queue size and buffersize failed\n"));
8626 			}
8627 		} else {
8628 			DHD_ERROR(("get rpc_dngl_agglimit failed\n"));
8629 			rpc_agg &= ~BCM_RPC_TP_DNGL_AGG_DPC;
8630 		}
8631 
8632 		/* Set aggregation for TX */
8633 		bcm_rpc_tp_agg_set(dhd->pub.info->rpc_th, BCM_RPC_TP_HOST_AGG_MASK,
8634 			rpc_agg & BCM_RPC_TP_HOST_AGG_MASK);
8635 
8636 		/* Set aggregation for RX */
8637 		memset(iovbuf, 0, sizeof(iovbuf));
8638 		bcm_mkiovar("rpc_agg", (char *)&rpc_agg, sizeof(rpc_agg), iovbuf, sizeof(iovbuf));
8639 		if (!dhd_wl_ioctl_cmd(&dhd->pub, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0)) {
8640 			dhd->pub.info->fdaggr = 0;
8641 			if (rpc_agg & BCM_RPC_TP_HOST_AGG_MASK)
8642 				dhd->pub.info->fdaggr |= BCM_FDAGGR_H2D_ENABLED;
8643 			if (rpc_agg & BCM_RPC_TP_DNGL_AGG_MASK)
8644 				dhd->pub.info->fdaggr |= BCM_FDAGGR_D2H_ENABLED;
8645 		} else {
8646 			DHD_ERROR(("%s(): Setting RX aggregation failed %d\n", __FUNCTION__, ret));
8647 		}
8648 #endif /* BCM_FD_AGGR */
8649 
8650 #ifdef BT_OVER_SDIO
8651 		if (dhd->pub.is_bt_recovery_required) {
8652 			DHD_ERROR(("%s: Send Hang Notification 2 to BT\n", __FUNCTION__));
8653 			bcmsdh_btsdio_process_dhd_hang_notification(TRUE);
8654 		}
8655 		dhd->pub.is_bt_recovery_required = FALSE;
8656 #endif
8657 
8658 		/* dhd_sync_with_dongle has been called in dhd_bus_start or wl_android_wifi_on */
8659 		memcpy(net->dev_addr, dhd->pub.mac.octet, ETHER_ADDR_LEN);
8660 
8661 #ifdef TOE
8662 		/* Get current TOE mode from dongle */
8663 		if (dhd_toe_get(dhd, ifidx, &toe_ol) >= 0 && (toe_ol & TOE_TX_CSUM_OL) != 0) {
8664 			dhd->iflist[ifidx]->net->features |= NETIF_F_IP_CSUM;
8665 		} else {
8666 			dhd->iflist[ifidx]->net->features &= ~NETIF_F_IP_CSUM;
8667 		}
8668 #endif /* TOE */
8669 
8670 #if defined(DHD_LB_RXP)
8671 		__skb_queue_head_init(&dhd->rx_pend_queue);
8672 		if (dhd->rx_napi_netdev == NULL) {
8673 			dhd->rx_napi_netdev = dhd->iflist[ifidx]->net;
8674 			memset(&dhd->rx_napi_struct, 0, sizeof(struct napi_struct));
8675 			netif_napi_add(dhd->rx_napi_netdev, &dhd->rx_napi_struct,
8676 				dhd_napi_poll, dhd_napi_weight);
8677 			DHD_INFO(("%s napi<%p> enabled ifp->net<%p,%s>\n",
8678 				__FUNCTION__, &dhd->rx_napi_struct, net, net->name));
8679 			napi_enable(&dhd->rx_napi_struct);
8680 			DHD_INFO(("%s load balance init rx_napi_struct\n", __FUNCTION__));
8681 			skb_queue_head_init(&dhd->rx_napi_queue);
8682 		} /* rx_napi_netdev == NULL */
8683 #endif /* DHD_LB_RXP */
8684 
8685 #if defined(DHD_LB_TXP)
8686 		/* Use the variant that uses locks */
8687 		skb_queue_head_init(&dhd->tx_pend_queue);
8688 #endif /* DHD_LB_TXP */
8689 
8690 #if defined(WL_CFG80211)
8691 		if (unlikely(wl_cfg80211_up(net))) {
8692 			DHD_ERROR(("%s: failed to bring up cfg80211\n", __FUNCTION__));
8693 			ret = -1;
8694 			goto exit;
8695 		}
8696 		if (!dhd_download_fw_on_driverload) {
8697 #ifdef ARP_OFFLOAD_SUPPORT
8698 			dhd->pend_ipaddr = 0;
8699 			if (!dhd_inetaddr_notifier_registered) {
8700 				dhd_inetaddr_notifier_registered = TRUE;
8701 				register_inetaddr_notifier(&dhd_inetaddr_notifier);
8702 			}
8703 #endif /* ARP_OFFLOAD_SUPPORT */
8704 #if defined(CONFIG_IPV6) && defined(IPV6_NDO_SUPPORT)
8705 			if (!dhd_inet6addr_notifier_registered) {
8706 				dhd_inet6addr_notifier_registered = TRUE;
8707 				register_inet6addr_notifier(&dhd_inet6addr_notifier);
8708 			}
8709 #endif /* CONFIG_IPV6 && IPV6_NDO_SUPPORT */
8710 		}
8711 
8712 		argos_register_notifier_init(net);
8713 #if defined(NUM_SCB_MAX_PROBE)
8714 		dhd_set_scb_probe(&dhd->pub);
8715 #endif /* NUM_SCB_MAX_PROBE */
8716 #endif /* WL_CFG80211 */
8717 #if defined(WL_WIRELESS_EXT)
8718 		if (unlikely(wl_iw_up(net, &dhd->pub))) {
8719 			DHD_ERROR(("%s: failed to bring up wext\n", __FUNCTION__));
8720 			ret = -1;
8721 			goto exit;
8722 		}
8723 #endif
8724 #ifdef WL_ESCAN
8725 		if (unlikely(wl_escan_up(net, &dhd->pub))) {
8726 			DHD_ERROR(("%s: failed to bring up escan\n", __FUNCTION__));
8727 			ret = -1;
8728 			goto exit;
8729 		}
8730 #endif /* WL_ESCAN */
8731 	}
8732 
8733 	/* Allow transmit calls */
8734 	netif_start_queue(net);
8735 	dhd->pub.up = 1;
8736 	if (ifidx == 0) {
8737 #ifdef DHD_LOAD_CHIPALIVE
8738 		if (dhd_chip_alive) {
8739 			wl_android_ext_priv_cmd(net, "setsuspendmode 0", 0, &bytes_written);
8740 		} else {
8741 #endif
8742 #if defined(ISAM_PREINIT)
8743 		if (!dhd_download_fw_on_driverload) {
8744 			if (dhd->pub.conf) {
8745 				wl_android_ext_priv_cmd(net, dhd->pub.conf->isam_init, 0, &bytes_written);
8746 				wl_android_ext_priv_cmd(net, dhd->pub.conf->isam_config, 0, &bytes_written);
8747 				wl_android_ext_priv_cmd(net, dhd->pub.conf->isam_enable, 0, &bytes_written);
8748 			}
8749 		}
8750 #endif
8751 #ifdef DHD_LOAD_CHIPALIVE
8752 		}
8753 #endif
8754 	}
8755 
8756 	OLD_MOD_INC_USE_COUNT;
8757 
8758 #ifdef BCMDBGFS
8759 	dhd_dbgfs_init(&dhd->pub);
8760 #endif
8761 
8762 exit:
8763 #ifdef ENABLE_INSMOD_NO_FW_LOAD
8764 	if (dhd_chip_alive) {
8765 		dhd_download_fw_on_driverload = FALSE;
8766 		dhd_driver_init_done = TRUE;
8767 	}
8768 #endif
8769 	if (ret) {
8770 		dhd_stop(net);
8771 	}
8772 
8773 	DHD_PERIM_UNLOCK(&dhd->pub);
8774 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
8775 	DHD_MUTEX_UNLOCK();
8776 
8777 	printf("%s: Exit ret=%d\n", __FUNCTION__, ret);
8778 	return ret;
8779 }
8780 
dhd_do_driver_init(struct net_device * net)8781 int dhd_do_driver_init(struct net_device *net)
8782 {
8783 	dhd_info_t *dhd = NULL;
8784 
8785 	if (!net) {
8786 		DHD_ERROR(("Primary Interface not initialized \n"));
8787 		return -EINVAL;
8788 	}
8789 
8790 	DHD_MUTEX_IS_LOCK_RETURN();
8791 
8792 	/*  && defined(OEM_ANDROID) && defined(BCMSDIO) */
8793 	dhd = DHD_DEV_INFO(net);
8794 
8795 	/* If driver is already initialized, do nothing
8796 	 */
8797 	if (dhd->pub.busstate == DHD_BUS_DATA) {
8798 		DHD_TRACE(("Driver already Inititalized. Nothing to do"));
8799 		return 0;
8800 	}
8801 
8802 	if (dhd_open(net) < 0) {
8803 		DHD_ERROR(("Driver Init Failed \n"));
8804 		return -1;
8805 	}
8806 
8807 	return 0;
8808 }
8809 
8810 int
dhd_event_ifadd(dhd_info_t * dhdinfo,wl_event_data_if_t * ifevent,char * name,uint8 * mac)8811 dhd_event_ifadd(dhd_info_t *dhdinfo, wl_event_data_if_t *ifevent, char *name, uint8 *mac)
8812 {
8813 
8814 #ifdef WL_CFG80211
8815 		if (wl_cfg80211_notify_ifadd(dhd_linux_get_primary_netdev(&dhdinfo->pub),
8816 			ifevent->ifidx, name, mac, ifevent->bssidx) == BCME_OK)
8817 		return BCME_OK;
8818 #endif
8819 
8820 	/* handle IF event caused by wl commands, SoftAP, WEXT and
8821 	 * anything else. This has to be done asynchronously otherwise
8822 	 * DPC will be blocked (and iovars will timeout as DPC has no chance
8823 	 * to read the response back)
8824 	 */
8825 	if (ifevent->ifidx > 0) {
8826 		dhd_if_event_t *if_event = MALLOC(dhdinfo->pub.osh, sizeof(dhd_if_event_t));
8827 		if (if_event == NULL) {
8828 			DHD_ERROR(("dhd_event_ifadd: Failed MALLOC, malloced %d bytes",
8829 				MALLOCED(dhdinfo->pub.osh)));
8830 			return BCME_NOMEM;
8831 		}
8832 
8833 		memcpy(&if_event->event, ifevent, sizeof(if_event->event));
8834 		memcpy(if_event->mac, mac, ETHER_ADDR_LEN);
8835 		strncpy(if_event->name, name, IFNAMSIZ);
8836 		if_event->name[IFNAMSIZ - 1] = '\0';
8837 		dhd_deferred_schedule_work(dhdinfo->dhd_deferred_wq, (void *)if_event,
8838 			DHD_WQ_WORK_IF_ADD, dhd_ifadd_event_handler, DHD_WQ_WORK_PRIORITY_LOW);
8839 	}
8840 
8841 	return BCME_OK;
8842 }
8843 
8844 int
dhd_event_ifdel(dhd_info_t * dhdinfo,wl_event_data_if_t * ifevent,char * name,uint8 * mac)8845 dhd_event_ifdel(dhd_info_t *dhdinfo, wl_event_data_if_t *ifevent, char *name, uint8 *mac)
8846 {
8847 	dhd_if_event_t *if_event;
8848 
8849 #ifdef WL_CFG80211
8850 		if (wl_cfg80211_notify_ifdel(dhd_linux_get_primary_netdev(&dhdinfo->pub),
8851 			ifevent->ifidx, name, mac, ifevent->bssidx) == BCME_OK)
8852 		return BCME_OK;
8853 #endif /* WL_CFG80211 */
8854 
8855 	/* handle IF event caused by wl commands, SoftAP, WEXT and
8856 	 * anything else
8857 	 */
8858 	if_event = MALLOC(dhdinfo->pub.osh, sizeof(dhd_if_event_t));
8859 	if (if_event == NULL) {
8860 		DHD_ERROR(("dhd_event_ifdel: malloc failed for if_event, malloced %d bytes",
8861 			MALLOCED(dhdinfo->pub.osh)));
8862 		return BCME_NOMEM;
8863 	}
8864 	memcpy(&if_event->event, ifevent, sizeof(if_event->event));
8865 	memcpy(if_event->mac, mac, ETHER_ADDR_LEN);
8866 	strncpy(if_event->name, name, IFNAMSIZ);
8867 	if_event->name[IFNAMSIZ - 1] = '\0';
8868 	dhd_deferred_schedule_work(dhdinfo->dhd_deferred_wq, (void *)if_event, DHD_WQ_WORK_IF_DEL,
8869 		dhd_ifdel_event_handler, DHD_WQ_WORK_PRIORITY_LOW);
8870 
8871 	return BCME_OK;
8872 }
8873 
8874 int
dhd_event_ifchange(dhd_info_t * dhdinfo,wl_event_data_if_t * ifevent,char * name,uint8 * mac)8875 dhd_event_ifchange(dhd_info_t *dhdinfo, wl_event_data_if_t *ifevent, char *name, uint8 *mac)
8876 {
8877 #ifdef DHD_UPDATE_INTF_MAC
8878 	dhd_if_event_t *if_event;
8879 #endif /* DHD_UPDATE_INTF_MAC */
8880 
8881 #ifdef WL_CFG80211
8882 	wl_cfg80211_notify_ifchange(dhd_linux_get_primary_netdev(&dhdinfo->pub),
8883 		ifevent->ifidx, name, mac, ifevent->bssidx);
8884 #endif /* WL_CFG80211 */
8885 
8886 #ifdef DHD_UPDATE_INTF_MAC
8887 	/* handle IF event caused by wl commands, SoftAP, WEXT, MBSS and
8888 	 * anything else
8889 	 */
8890 	if_event = MALLOC(dhdinfo->pub.osh, sizeof(dhd_if_event_t));
8891 	if (if_event == NULL) {
8892 		DHD_ERROR(("dhd_event_ifdel: malloc failed for if_event, malloced %d bytes",
8893 			MALLOCED(dhdinfo->pub.osh)));
8894 		return BCME_NOMEM;
8895 	}
8896 	memcpy(&if_event->event, ifevent, sizeof(if_event->event));
8897 	// construct a change event
8898 	if_event->event.ifidx = dhd_ifname2idx(dhdinfo, name);
8899 	if_event->event.opcode = WLC_E_IF_CHANGE;
8900 	memcpy(if_event->mac, mac, ETHER_ADDR_LEN);
8901 	strncpy(if_event->name, name, IFNAMSIZ);
8902 	if_event->name[IFNAMSIZ - 1] = '\0';
8903 	dhd_deferred_schedule_work(dhdinfo->dhd_deferred_wq, (void *)if_event, DHD_WQ_WORK_IF_UPDATE,
8904 		dhd_ifupdate_event_handler, DHD_WQ_WORK_PRIORITY_LOW);
8905 #endif /* DHD_UPDATE_INTF_MAC */
8906 
8907 	return BCME_OK;
8908 }
8909 
8910 /* unregister and free the existing net_device interface (if any) in iflist and
8911  * allocate a new one. the slot is reused. this function does NOT register the
8912  * new interface to linux kernel. dhd_register_if does the job
8913  */
8914 struct net_device*
dhd_allocate_if(dhd_pub_t * dhdpub,int ifidx,const char * name,uint8 * mac,uint8 bssidx,bool need_rtnl_lock,const char * dngl_name)8915 dhd_allocate_if(dhd_pub_t *dhdpub, int ifidx, const char *name,
8916 	uint8 *mac, uint8 bssidx, bool need_rtnl_lock, const char *dngl_name)
8917 {
8918 	dhd_info_t *dhdinfo = (dhd_info_t *)dhdpub->info;
8919 	dhd_if_t *ifp;
8920 
8921 	ASSERT(dhdinfo && (ifidx < DHD_MAX_IFS));
8922 	ifp = dhdinfo->iflist[ifidx];
8923 
8924 	if (ifp != NULL) {
8925 		if (ifp->net != NULL) {
8926 			DHD_ERROR(("%s: free existing IF %s ifidx:%d \n",
8927 				__FUNCTION__, ifp->net->name, ifidx));
8928 
8929 			if (ifidx == 0) {
8930 				/* For primary ifidx (0), there shouldn't be
8931 				 * any netdev present already.
8932 				 */
8933 				DHD_ERROR(("Primary ifidx populated already\n"));
8934 				ASSERT(0);
8935 				return NULL;
8936 			}
8937 
8938 			dhd_dev_priv_clear(ifp->net); /* clear net_device private */
8939 
8940 			/* in unregister_netdev case, the interface gets freed by net->destructor
8941 			 * (which is set to free_netdev)
8942 			 */
8943 			if (ifp->net->reg_state == NETREG_UNINITIALIZED) {
8944 				free_netdev(ifp->net);
8945 			} else {
8946 				netif_stop_queue(ifp->net);
8947 				if (need_rtnl_lock)
8948 					unregister_netdev(ifp->net);
8949 				else
8950 					unregister_netdevice(ifp->net);
8951 			}
8952 			ifp->net = NULL;
8953 		}
8954 	} else {
8955 		ifp = MALLOC(dhdinfo->pub.osh, sizeof(dhd_if_t));
8956 		if (ifp == NULL) {
8957 			DHD_ERROR(("%s: OOM - dhd_if_t(%zu)\n", __FUNCTION__, sizeof(dhd_if_t)));
8958 			return NULL;
8959 		}
8960 	}
8961 
8962 	memset(ifp, 0, sizeof(dhd_if_t));
8963 	ifp->info = dhdinfo;
8964 	ifp->idx = ifidx;
8965 	ifp->bssidx = bssidx;
8966 #ifdef DHD_MCAST_REGEN
8967 	ifp->mcast_regen_bss_enable = FALSE;
8968 #endif
8969 	/* set to TRUE rx_pkt_chainable at alloc time */
8970 	ifp->rx_pkt_chainable = TRUE;
8971 
8972 	if (mac != NULL)
8973 		memcpy(&ifp->mac_addr, mac, ETHER_ADDR_LEN);
8974 
8975 	/* Allocate etherdev, including space for private structure */
8976 	ifp->net = alloc_etherdev(DHD_DEV_PRIV_SIZE);
8977 	if (ifp->net == NULL) {
8978 		DHD_ERROR(("%s: OOM - alloc_etherdev(%zu)\n", __FUNCTION__, sizeof(dhdinfo)));
8979 		goto fail;
8980 	}
8981 
8982 	/* Setup the dhd interface's netdevice private structure. */
8983 	dhd_dev_priv_save(ifp->net, dhdinfo, ifp, ifidx);
8984 
8985 	if (name && name[0]) {
8986 		strncpy(ifp->net->name, name, IFNAMSIZ);
8987 		ifp->net->name[IFNAMSIZ - 1] = '\0';
8988 	}
8989 
8990 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 9))
8991 #define IFP_NET_DESTRUCTOR	ifp->net->priv_destructor
8992 #else
8993 #define IFP_NET_DESTRUCTOR	ifp->net->destructor
8994 #endif // endif
8995 
8996 #ifdef WL_CFG80211
8997 	if (ifidx == 0) {
8998 		IFP_NET_DESTRUCTOR = free_netdev;
8999 	} else {
9000 		IFP_NET_DESTRUCTOR = dhd_netdev_free;
9001 	}
9002 #else
9003 	IFP_NET_DESTRUCTOR = free_netdev;
9004 #endif /* WL_CFG80211 */
9005 	strncpy(ifp->name, ifp->net->name, IFNAMSIZ);
9006 	ifp->name[IFNAMSIZ - 1] = '\0';
9007 	dhdinfo->iflist[ifidx] = ifp;
9008 
9009 /* initialize the dongle provided if name */
9010 	if (dngl_name)
9011 		strncpy(ifp->dngl_name, dngl_name, IFNAMSIZ);
9012 	else if (name)
9013 		strncpy(ifp->dngl_name, name, IFNAMSIZ);
9014 
9015 #ifdef PCIE_FULL_DONGLE
9016 	/* Initialize STA info list */
9017 	INIT_LIST_HEAD(&ifp->sta_list);
9018 	DHD_IF_STA_LIST_LOCK_INIT(ifp);
9019 #endif /* PCIE_FULL_DONGLE */
9020 
9021 #ifdef DHD_L2_FILTER
9022 	ifp->phnd_arp_table = init_l2_filter_arp_table(dhdpub->osh);
9023 	ifp->parp_allnode = TRUE;
9024 #endif /* DHD_L2_FILTER */
9025 
9026 
9027 	DHD_CUMM_CTR_INIT(&ifp->cumm_ctr);
9028 
9029 	return ifp->net;
9030 
9031 fail:
9032 	if (ifp != NULL) {
9033 		if (ifp->net != NULL) {
9034 #if defined(DHD_LB_RXP) && defined(PCIE_FULL_DONGLE)
9035 			if (ifp->net == dhdinfo->rx_napi_netdev) {
9036 				napi_disable(&dhdinfo->rx_napi_struct);
9037 				netif_napi_del(&dhdinfo->rx_napi_struct);
9038 				skb_queue_purge(&dhdinfo->rx_napi_queue);
9039 				dhdinfo->rx_napi_netdev = NULL;
9040 			}
9041 #endif /* DHD_LB_RXP && PCIE_FULL_DONGLE */
9042 			dhd_dev_priv_clear(ifp->net);
9043 			free_netdev(ifp->net);
9044 			ifp->net = NULL;
9045 		}
9046 		MFREE(dhdinfo->pub.osh, ifp, sizeof(*ifp));
9047 		ifp = NULL;
9048 	}
9049 	dhdinfo->iflist[ifidx] = NULL;
9050 	return NULL;
9051 }
9052 
9053 /* unregister and free the the net_device interface associated with the indexed
9054  * slot, also free the slot memory and set the slot pointer to NULL
9055  */
9056 int
dhd_remove_if(dhd_pub_t * dhdpub,int ifidx,bool need_rtnl_lock)9057 dhd_remove_if(dhd_pub_t *dhdpub, int ifidx, bool need_rtnl_lock)
9058 {
9059 	dhd_info_t *dhdinfo = (dhd_info_t *)dhdpub->info;
9060 	dhd_if_t *ifp;
9061 #ifdef PCIE_FULL_DONGLE
9062 	if_flow_lkup_t *if_flow_lkup = (if_flow_lkup_t *)dhdpub->if_flow_lkup;
9063 #endif /* PCIE_FULL_DONGLE */
9064 
9065 	ifp = dhdinfo->iflist[ifidx];
9066 
9067 	if (ifp != NULL) {
9068 		if (ifp->net != NULL) {
9069 			DHD_ERROR(("deleting interface '%s' idx %d\n", ifp->net->name, ifp->idx));
9070 
9071 			dhdinfo->iflist[ifidx] = NULL;
9072 			/* in unregister_netdev case, the interface gets freed by net->destructor
9073 			 * (which is set to free_netdev)
9074 			 */
9075 			if (ifp->net->reg_state == NETREG_UNINITIALIZED) {
9076 				free_netdev(ifp->net);
9077 			} else {
9078 				netif_tx_disable(ifp->net);
9079 
9080 
9081 
9082 #if defined(SET_RPS_CPUS)
9083 				custom_rps_map_clear(ifp->net->_rx);
9084 #endif /* SET_RPS_CPUS */
9085 				if (need_rtnl_lock)
9086 					unregister_netdev(ifp->net);
9087 				else
9088 					unregister_netdevice(ifp->net);
9089 #if defined(WL_EXT_IAPSTA) || defined(USE_IW) || defined(WL_ESCAN)
9090 #ifdef WL_EXT_IAPSTA
9091 				wl_ext_iapsta_dettach_netdev(ifp->net, ifidx);
9092 #endif /* WL_EXT_IAPSTA */
9093 #ifdef WL_ESCAN
9094 				wl_escan_event_dettach(ifp->net, dhdpub);
9095 #endif /* WL_ESCAN */
9096 				wl_ext_event_dettach_netdev(ifp->net, ifidx);
9097 #endif /* WL_EXT_IAPSTA || USE_IW || WL_ESCAN */
9098 			}
9099 			ifp->net = NULL;
9100 		}
9101 #ifdef DHD_WMF
9102 		dhd_wmf_cleanup(dhdpub, ifidx);
9103 #endif /* DHD_WMF */
9104 #ifdef DHD_L2_FILTER
9105 		bcm_l2_filter_arp_table_update(dhdpub->osh, ifp->phnd_arp_table, TRUE,
9106 			NULL, FALSE, dhdpub->tickcnt);
9107 		deinit_l2_filter_arp_table(dhdpub->osh, ifp->phnd_arp_table);
9108 		ifp->phnd_arp_table = NULL;
9109 #endif /* DHD_L2_FILTER */
9110 
9111 
9112 		dhd_if_del_sta_list(ifp);
9113 #ifdef PCIE_FULL_DONGLE
9114 		/* Delete flowrings of WDS interface */
9115 		if (if_flow_lkup[ifidx].role == WLC_E_IF_ROLE_WDS) {
9116 			dhd_flow_rings_delete(dhdpub, ifidx);
9117 		}
9118 #endif /* PCIE_FULL_DONGLE */
9119 		DHD_CUMM_CTR_INIT(&ifp->cumm_ctr);
9120 
9121 		MFREE(dhdinfo->pub.osh, ifp, sizeof(*ifp));
9122 		ifp = NULL;
9123 	}
9124 
9125 	return BCME_OK;
9126 }
9127 
9128 
9129 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
9130 static struct net_device_ops dhd_ops_pri = {
9131 	.ndo_open = dhd_open,
9132 	.ndo_stop = dhd_stop,
9133 	.ndo_get_stats = dhd_get_stats,
9134 	.ndo_do_ioctl = dhd_ioctl_entry,
9135 	.ndo_start_xmit = dhd_start_xmit,
9136 	.ndo_set_mac_address = dhd_set_mac_address,
9137 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
9138 	.ndo_set_rx_mode = dhd_set_multicast_list,
9139 #else
9140 	.ndo_set_multicast_list = dhd_set_multicast_list,
9141 #endif
9142 };
9143 
9144 static struct net_device_ops dhd_ops_virt = {
9145 	.ndo_get_stats = dhd_get_stats,
9146 	.ndo_do_ioctl = dhd_ioctl_entry,
9147 	.ndo_start_xmit = dhd_start_xmit,
9148 	.ndo_set_mac_address = dhd_set_mac_address,
9149 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
9150 	.ndo_set_rx_mode = dhd_set_multicast_list,
9151 #else
9152 	.ndo_set_multicast_list = dhd_set_multicast_list,
9153 #endif
9154 };
9155 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) */
9156 
9157 #ifdef DEBUGGER
9158 extern void debugger_init(void *bus_handle);
9159 #endif
9160 
9161 
9162 #ifdef SHOW_LOGTRACE
9163 int
dhd_os_read_file(void * file,char * buf,uint32 size)9164 dhd_os_read_file(void *file, char *buf, uint32 size)
9165 {
9166 	struct file *filep = (struct file *)file;
9167 
9168 	if (!file || !buf)
9169 		return -1;
9170 
9171 	return vfs_read(filep, buf, size, &filep->f_pos);
9172 }
9173 
9174 int
dhd_os_seek_file(void * file,int64 offset)9175 dhd_os_seek_file(void *file, int64 offset)
9176 {
9177 	struct file *filep = (struct file *)file;
9178 	if (!file)
9179 		return -1;
9180 
9181 	/* offset can be -ve */
9182 	filep->f_pos = filep->f_pos + offset;
9183 
9184 	return 0;
9185 }
9186 
9187 static int
dhd_init_logstrs_array(osl_t * osh,dhd_event_log_t * temp)9188 dhd_init_logstrs_array(osl_t *osh, dhd_event_log_t *temp)
9189 {
9190 	struct file *filep = NULL;
9191 	struct kstat stat;
9192 	mm_segment_t fs;
9193 	char *raw_fmts =  NULL;
9194 	int logstrs_size = 0;
9195 	int error = 0;
9196 
9197 	fs = get_fs();
9198 	set_fs(KERNEL_DS);
9199 
9200 	filep = filp_open(logstrs_path, O_RDONLY, 0);
9201 
9202 	if (IS_ERR(filep)) {
9203 		DHD_ERROR(("%s: Failed to open the file %s \n", __FUNCTION__, logstrs_path));
9204 		goto fail;
9205 	}
9206 	error = vfs_stat(logstrs_path, &stat);
9207 	if (error) {
9208 		DHD_ERROR(("%s: Failed to stat file %s \n", __FUNCTION__, logstrs_path));
9209 		goto fail;
9210 	}
9211 	logstrs_size = (int) stat.size;
9212 
9213 	if (logstrs_size == 0) {
9214 		DHD_ERROR(("%s: return as logstrs_size is 0\n", __FUNCTION__));
9215 		goto fail1;
9216 	}
9217 
9218 	raw_fmts = MALLOC(osh, logstrs_size);
9219 	if (raw_fmts == NULL) {
9220 		DHD_ERROR(("%s: Failed to allocate memory \n", __FUNCTION__));
9221 		goto fail;
9222 	}
9223 	if (vfs_read(filep, raw_fmts, logstrs_size, &filep->f_pos) !=	logstrs_size) {
9224 		DHD_ERROR(("%s: Failed to read file %s\n", __FUNCTION__, logstrs_path));
9225 		goto fail;
9226 	}
9227 
9228 	if (dhd_parse_logstrs_file(osh, raw_fmts, logstrs_size, temp)
9229 				== BCME_OK) {
9230 		filp_close(filep, NULL);
9231 		set_fs(fs);
9232 		return BCME_OK;
9233 	}
9234 
9235 fail:
9236 	if (raw_fmts) {
9237 		MFREE(osh, raw_fmts, logstrs_size);
9238 		raw_fmts = NULL;
9239 	}
9240 
9241 fail1:
9242 	if (!IS_ERR(filep))
9243 		filp_close(filep, NULL);
9244 
9245 	set_fs(fs);
9246 	temp->fmts = NULL;
9247 	return BCME_ERROR;
9248 }
9249 
9250 static int
dhd_read_map(osl_t * osh,char * fname,uint32 * ramstart,uint32 * rodata_start,uint32 * rodata_end)9251 dhd_read_map(osl_t *osh, char *fname, uint32 *ramstart, uint32 *rodata_start,
9252 		uint32 *rodata_end)
9253 {
9254 	struct file *filep = NULL;
9255 	mm_segment_t fs;
9256 	int err = BCME_ERROR;
9257 
9258 	if (fname == NULL) {
9259 		DHD_ERROR(("%s: ERROR fname is NULL \n", __FUNCTION__));
9260 		return BCME_ERROR;
9261 	}
9262 
9263 	fs = get_fs();
9264 	set_fs(KERNEL_DS);
9265 
9266 	filep = filp_open(fname, O_RDONLY, 0);
9267 	if (IS_ERR(filep)) {
9268 		DHD_ERROR(("%s: Failed to open %s \n",  __FUNCTION__, fname));
9269 		goto fail;
9270 	}
9271 
9272 	if ((err = dhd_parse_map_file(osh, filep, ramstart,
9273 			rodata_start, rodata_end)) < 0)
9274 		goto fail;
9275 
9276 fail:
9277 	if (!IS_ERR(filep))
9278 		filp_close(filep, NULL);
9279 
9280 	set_fs(fs);
9281 
9282 	return err;
9283 }
9284 
9285 static int
dhd_init_static_strs_array(osl_t * osh,dhd_event_log_t * temp,char * str_file,char * map_file)9286 dhd_init_static_strs_array(osl_t *osh, dhd_event_log_t *temp, char *str_file, char *map_file)
9287 {
9288 	struct file *filep = NULL;
9289 	mm_segment_t fs;
9290 	char *raw_fmts =  NULL;
9291 	uint32 logstrs_size = 0;
9292 
9293 	int error = 0;
9294 	uint32 ramstart = 0;
9295 	uint32 rodata_start = 0;
9296 	uint32 rodata_end = 0;
9297 	uint32 logfilebase = 0;
9298 
9299 	error = dhd_read_map(osh, map_file, &ramstart, &rodata_start, &rodata_end);
9300 	if (error != BCME_OK) {
9301 		DHD_ERROR(("readmap Error!! \n"));
9302 		/* don't do event log parsing in actual case */
9303 		if (strstr(str_file, ram_file_str) != NULL) {
9304 			temp->raw_sstr = NULL;
9305 		} else if (strstr(str_file, rom_file_str) != NULL) {
9306 			temp->rom_raw_sstr = NULL;
9307 		}
9308 		return error;
9309 	}
9310 	DHD_ERROR(("ramstart: 0x%x, rodata_start: 0x%x, rodata_end:0x%x\n",
9311 		ramstart, rodata_start, rodata_end));
9312 
9313 	fs = get_fs();
9314 	set_fs(KERNEL_DS);
9315 
9316 	filep = filp_open(str_file, O_RDONLY, 0);
9317 	if (IS_ERR(filep)) {
9318 		DHD_ERROR(("%s: Failed to open the file %s \n",  __FUNCTION__, str_file));
9319 		goto fail;
9320 	}
9321 
9322 	/* Full file size is huge. Just read required part */
9323 	logstrs_size = rodata_end - rodata_start;
9324 
9325 	if (logstrs_size == 0) {
9326 		DHD_ERROR(("%s: return as logstrs_size is 0\n", __FUNCTION__));
9327 		goto fail1;
9328 	}
9329 
9330 	raw_fmts = MALLOC(osh, logstrs_size);
9331 	if (raw_fmts == NULL) {
9332 		DHD_ERROR(("%s: Failed to allocate raw_fmts memory \n", __FUNCTION__));
9333 		goto fail;
9334 	}
9335 
9336 	logfilebase = rodata_start - ramstart;
9337 
9338 	error = generic_file_llseek(filep, logfilebase, SEEK_SET);
9339 	if (error < 0) {
9340 		DHD_ERROR(("%s: %s llseek failed %d \n", __FUNCTION__, str_file, error));
9341 		goto fail;
9342 	}
9343 
9344 	error = vfs_read(filep, raw_fmts, logstrs_size, (&filep->f_pos));
9345 	if (error != logstrs_size) {
9346 		DHD_ERROR(("%s: %s read failed %d \n", __FUNCTION__, str_file, error));
9347 		goto fail;
9348 	}
9349 
9350 	if (strstr(str_file, ram_file_str) != NULL) {
9351 		temp->raw_sstr = raw_fmts;
9352 		temp->raw_sstr_size = logstrs_size;
9353 		temp->ramstart = ramstart;
9354 		temp->rodata_start = rodata_start;
9355 		temp->rodata_end = rodata_end;
9356 	} else if (strstr(str_file, rom_file_str) != NULL) {
9357 		temp->rom_raw_sstr = raw_fmts;
9358 		temp->rom_raw_sstr_size = logstrs_size;
9359 		temp->rom_ramstart = ramstart;
9360 		temp->rom_rodata_start = rodata_start;
9361 		temp->rom_rodata_end = rodata_end;
9362 	}
9363 
9364 	filp_close(filep, NULL);
9365 	set_fs(fs);
9366 
9367 	return BCME_OK;
9368 
9369 fail:
9370 	if (raw_fmts) {
9371 		MFREE(osh, raw_fmts, logstrs_size);
9372 		raw_fmts = NULL;
9373 	}
9374 
9375 fail1:
9376 	if (!IS_ERR(filep))
9377 		filp_close(filep, NULL);
9378 
9379 	set_fs(fs);
9380 
9381 	if (strstr(str_file, ram_file_str) != NULL) {
9382 		temp->raw_sstr = NULL;
9383 	} else if (strstr(str_file, rom_file_str) != NULL) {
9384 		temp->rom_raw_sstr = NULL;
9385 	}
9386 
9387 	return error;
9388 }
9389 
9390 #endif /* SHOW_LOGTRACE */
9391 
9392 #ifdef BCMDBUS
9393 uint
dhd_get_rxsz(dhd_pub_t * pub)9394 dhd_get_rxsz(dhd_pub_t *pub)
9395 {
9396 	struct net_device *net = NULL;
9397 	dhd_info_t *dhd = NULL;
9398 	uint rxsz;
9399 
9400 	/* Assign rxsz for dbus_attach */
9401 	dhd = pub->info;
9402 	net = dhd->iflist[0]->net;
9403 	net->hard_header_len = ETH_HLEN + pub->hdrlen;
9404 	rxsz = DBUS_RX_BUFFER_SIZE_DHD(net);
9405 
9406 	return rxsz;
9407 }
9408 
9409 void
dhd_set_path(dhd_pub_t * pub)9410 dhd_set_path(dhd_pub_t *pub)
9411 {
9412 	dhd_info_t *dhd = NULL;
9413 
9414 	dhd = pub->info;
9415 
9416 	/* try to download image and nvram to the dongle */
9417 	if	(dhd_update_fw_nv_path(dhd) && dhd->pub.bus) {
9418 		DHD_INFO(("%s: fw %s, nv %s, conf %s\n",
9419 			__FUNCTION__, dhd->fw_path, dhd->nv_path, dhd->conf_path));
9420 		dhd_bus_update_fw_nv_path(dhd->pub.bus,
9421 				dhd->fw_path, dhd->nv_path, dhd->clm_path, dhd->conf_path);
9422 	}
9423 }
9424 #endif
9425 
9426 dhd_pub_t *
dhd_attach(osl_t * osh,struct dhd_bus * bus,uint bus_hdrlen,void * data)9427 dhd_attach(osl_t *osh, struct dhd_bus *bus, uint bus_hdrlen
9428 #ifdef BCMDBUS
9429 	, void *data
9430 #endif
9431 )
9432 {
9433 	dhd_info_t *dhd = NULL;
9434 	struct net_device *net = NULL;
9435 	char if_name[IFNAMSIZ] = {'\0'};
9436 #ifdef SHOW_LOGTRACE
9437 	int ret;
9438 #endif /* SHOW_LOGTRACE */
9439 #if defined(BCMSDIO) || defined(BCMPCIE)
9440 	uint32 bus_type = -1;
9441 	uint32 bus_num = -1;
9442 	uint32 slot_num = -1;
9443 	wifi_adapter_info_t *adapter = NULL;
9444 #elif defined(BCMDBUS)
9445 	wifi_adapter_info_t *adapter = data;
9446 #endif
9447 #ifdef GET_CUSTOM_MAC_ENABLE
9448 	char hw_ether[62];
9449 #endif /* GET_CUSTOM_MAC_ENABLE */
9450 
9451 	dhd_attach_states_t dhd_state = DHD_ATTACH_STATE_INIT;
9452 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
9453 
9454 #ifdef STBLINUX
9455 	DHD_ERROR(("%s\n", driver_target));
9456 #endif /* STBLINUX */
9457 	/* will implement get_ids for DBUS later */
9458 #if defined(BCMSDIO)
9459 	dhd_bus_get_ids(bus, &bus_type, &bus_num, &slot_num);
9460 #endif
9461 #if defined(BCMSDIO) || defined(BCMPCIE)
9462 	adapter = dhd_wifi_platform_get_adapter(bus_type, bus_num, slot_num);
9463 #endif
9464 
9465 	/* Allocate primary dhd_info */
9466 	dhd = wifi_platform_prealloc(adapter, DHD_PREALLOC_DHD_INFO, sizeof(dhd_info_t));
9467 	if (dhd == NULL) {
9468 		dhd = MALLOC(osh, sizeof(dhd_info_t));
9469 		if (dhd == NULL) {
9470 			DHD_ERROR(("%s: OOM - alloc dhd_info\n", __FUNCTION__));
9471 			goto dhd_null_flag;
9472 		}
9473 	}
9474 	memset(dhd, 0, sizeof(dhd_info_t));
9475 	dhd_state |= DHD_ATTACH_STATE_DHD_ALLOC;
9476 
9477 	dhd->unit = dhd_found + instance_base; /* do not increment dhd_found, yet */
9478 
9479 	dhd->pub.osh = osh;
9480 #ifdef DUMP_IOCTL_IOV_LIST
9481 	dll_init(&(dhd->pub.dump_iovlist_head));
9482 #endif /* DUMP_IOCTL_IOV_LIST */
9483 	dhd->adapter = adapter;
9484 	dhd->pub.adapter = (void *)adapter;
9485 #ifdef DHD_DEBUG
9486 	dll_init(&(dhd->pub.mw_list_head));
9487 #endif /* DHD_DEBUG */
9488 #ifdef BT_OVER_SDIO
9489 	dhd->pub.is_bt_recovery_required = FALSE;
9490 	mutex_init(&dhd->bus_user_lock);
9491 #endif /* BT_OVER_SDIO */
9492 
9493 #ifdef GET_CUSTOM_MAC_ENABLE
9494 	wifi_platform_get_mac_addr(dhd->adapter, hw_ether);
9495 	bcopy(hw_ether, dhd->pub.mac.octet, sizeof(struct ether_addr));
9496 #endif /* GET_CUSTOM_MAC_ENABLE */
9497 #ifdef CUSTOM_FORCE_NODFS_FLAG
9498 	dhd->pub.dhd_cflags |= WLAN_PLAT_NODFS_FLAG;
9499 	dhd->pub.force_country_change = TRUE;
9500 #endif /* CUSTOM_FORCE_NODFS_FLAG */
9501 #ifdef CUSTOM_COUNTRY_CODE
9502 	get_customized_country_code(dhd->adapter,
9503 		dhd->pub.dhd_cspec.country_abbrev, &dhd->pub.dhd_cspec,
9504 		dhd->pub.dhd_cflags);
9505 #endif /* CUSTOM_COUNTRY_CODE */
9506 #ifndef BCMDBUS
9507 	dhd->thr_dpc_ctl.thr_pid = DHD_PID_KT_TL_INVALID;
9508 	dhd->thr_wdt_ctl.thr_pid = DHD_PID_KT_INVALID;
9509 #ifdef DHD_WET
9510 	dhd->pub.wet_info = dhd_get_wet_info(&dhd->pub);
9511 #endif /* DHD_WET */
9512 	/* Initialize thread based operation and lock */
9513 	sema_init(&dhd->sdsem, 1);
9514 #endif /* !BCMDBUS */
9515 
9516 	/* Link to info module */
9517 	dhd->pub.info = dhd;
9518 
9519 
9520 	/* Link to bus module */
9521 	dhd->pub.bus = bus;
9522 	dhd->pub.hdrlen = bus_hdrlen;
9523 
9524 	/* dhd_conf must be attached after linking dhd to dhd->pub.info,
9525 	 * because dhd_detech will check .info is NULL or not.
9526 	*/
9527 	if (dhd_conf_attach(&dhd->pub) != 0) {
9528 		DHD_ERROR(("dhd_conf_attach failed\n"));
9529 		goto fail;
9530 	}
9531 #ifndef BCMDBUS
9532 	dhd_conf_reset(&dhd->pub);
9533 	dhd_conf_set_chiprev(&dhd->pub, dhd_bus_chip(bus), dhd_bus_chiprev(bus));
9534 	dhd_conf_preinit(&dhd->pub);
9535 #endif /* !BCMDBUS */
9536 
9537 	/* Some DHD modules (e.g. cfg80211) configures operation mode based on firmware name.
9538 	 * This is indeed a hack but we have to make it work properly before we have a better
9539 	 * solution
9540 	 */
9541 	dhd_update_fw_nv_path(dhd);
9542 
9543 	/* Set network interface name if it was provided as module parameter */
9544 	if (iface_name[0]) {
9545 		int len;
9546 		char ch;
9547 		strncpy(if_name, iface_name, IFNAMSIZ);
9548 		if_name[IFNAMSIZ - 1] = 0;
9549 		len = strlen(if_name);
9550 		ch = if_name[len - 1];
9551 		if ((ch > '9' || ch < '0') && (len < IFNAMSIZ - 2))
9552 			strncat(if_name, "%d", 2);
9553 	}
9554 
9555 	/* Passing NULL to dngl_name to ensure host gets if_name in dngl_name member */
9556 	net = dhd_allocate_if(&dhd->pub, 0, if_name, NULL, 0, TRUE, NULL);
9557 	if (net == NULL) {
9558 		goto fail;
9559 	}
9560 
9561 
9562 	dhd_state |= DHD_ATTACH_STATE_ADD_IF;
9563 #ifdef DHD_L2_FILTER
9564 	/* initialize the l2_filter_cnt */
9565 	dhd->pub.l2_filter_cnt = 0;
9566 #endif
9567 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
9568 	net->open = NULL;
9569 #else
9570 	net->netdev_ops = NULL;
9571 #endif
9572 
9573 	mutex_init(&dhd->dhd_iovar_mutex);
9574 	sema_init(&dhd->proto_sem, 1);
9575 #ifdef DHD_ULP
9576 	if (!(dhd_ulp_init(osh, &dhd->pub)))
9577 		goto fail;
9578 #endif /* DHD_ULP */
9579 
9580 #if defined(DHD_HANG_SEND_UP_TEST)
9581 	dhd->pub.req_hang_type = 0;
9582 #endif /* DHD_HANG_SEND_UP_TEST */
9583 
9584 #ifdef PROP_TXSTATUS
9585 	spin_lock_init(&dhd->wlfc_spinlock);
9586 
9587 	dhd->pub.skip_fc = dhd_wlfc_skip_fc;
9588 	dhd->pub.plat_init = dhd_wlfc_plat_init;
9589 	dhd->pub.plat_deinit = dhd_wlfc_plat_deinit;
9590 
9591 #ifdef DHD_WLFC_THREAD
9592 	init_waitqueue_head(&dhd->pub.wlfc_wqhead);
9593 	dhd->pub.wlfc_thread = kthread_create(dhd_wlfc_transfer_packets, &dhd->pub, "wlfc-thread");
9594 	if (IS_ERR(dhd->pub.wlfc_thread)) {
9595 		DHD_ERROR(("create wlfc thread failed\n"));
9596 		goto fail;
9597 	} else {
9598 		wake_up_process(dhd->pub.wlfc_thread);
9599 	}
9600 #endif /* DHD_WLFC_THREAD */
9601 #endif /* PROP_TXSTATUS */
9602 
9603 	/* Initialize other structure content */
9604 	init_waitqueue_head(&dhd->ioctl_resp_wait);
9605 	init_waitqueue_head(&dhd->d3ack_wait);
9606 #ifdef PCIE_INB_DW
9607 	init_waitqueue_head(&dhd->ds_exit_wait);
9608 #endif /* PCIE_INB_DW */
9609 	init_waitqueue_head(&dhd->ctrl_wait);
9610 	init_waitqueue_head(&dhd->dhd_bus_busy_state_wait);
9611 	dhd->pub.dhd_bus_busy_state = 0;
9612 
9613 	/* Initialize the spinlocks */
9614 	spin_lock_init(&dhd->sdlock);
9615 	spin_lock_init(&dhd->txqlock);
9616 	spin_lock_init(&dhd->rxqlock);
9617 	spin_lock_init(&dhd->dhd_lock);
9618 	spin_lock_init(&dhd->rxf_lock);
9619 #ifdef WLTDLS
9620 	spin_lock_init(&dhd->pub.tdls_lock);
9621 #endif /* WLTDLS */
9622 #if defined(RXFRAME_THREAD)
9623 	dhd->rxthread_enabled = TRUE;
9624 #endif /* defined(RXFRAME_THREAD) */
9625 
9626 #ifdef DHDTCPACK_SUPPRESS
9627 	spin_lock_init(&dhd->tcpack_lock);
9628 #endif /* DHDTCPACK_SUPPRESS */
9629 
9630 	/* Initialize Wakelock stuff */
9631 	spin_lock_init(&dhd->wakelock_spinlock);
9632 	spin_lock_init(&dhd->wakelock_evt_spinlock);
9633 	DHD_OS_WAKE_LOCK_INIT(dhd);
9634 	dhd->wakelock_counter = 0;
9635 #ifdef CONFIG_HAS_WAKELOCK
9636 	// terence 20161023: can not destroy wl_wifi when wlan down, it will happen null pointer in dhd_ioctl_entry
9637 	wake_lock_init(&dhd->wl_wifi, WAKE_LOCK_SUSPEND, "wlan_wake");
9638 	wake_lock_init(&dhd->wl_wdwake, WAKE_LOCK_SUSPEND, "wlan_wd_wake");
9639 #endif /* CONFIG_HAS_WAKELOCK */
9640 
9641 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
9642 	mutex_init(&dhd->dhd_net_if_mutex);
9643 	mutex_init(&dhd->dhd_suspend_mutex);
9644 #if defined(PKT_FILTER_SUPPORT) && defined(APF)
9645 	mutex_init(&dhd->dhd_apf_mutex);
9646 #endif /* PKT_FILTER_SUPPORT && APF */
9647 #endif
9648 	dhd_state |= DHD_ATTACH_STATE_WAKELOCKS_INIT;
9649 
9650 	/* Attach and link in the protocol */
9651 	if (dhd_prot_attach(&dhd->pub) != 0) {
9652 		DHD_ERROR(("dhd_prot_attach failed\n"));
9653 		goto fail;
9654 	}
9655 	dhd_state |= DHD_ATTACH_STATE_PROT_ATTACH;
9656 
9657 #ifdef DHD_TIMESYNC
9658 	/* attach the timesync module */
9659 	if (dhd_timesync_attach(&dhd->pub) != 0) {
9660 		DHD_ERROR(("dhd_timesync_attach failed\n"));
9661 		goto fail;
9662 	}
9663 	dhd_state |= DHD_ATTACH_TIMESYNC_ATTACH_DONE;
9664 #endif /* DHD_TIMESYNC */
9665 
9666 #ifdef WL_CFG80211
9667 	spin_lock_init(&dhd->pub.up_lock);
9668 	/* Attach and link in the cfg80211 */
9669 	if (unlikely(wl_cfg80211_attach(net, &dhd->pub))) {
9670 		DHD_ERROR(("wl_cfg80211_attach failed\n"));
9671 		goto fail;
9672 	}
9673 
9674 	dhd_monitor_init(&dhd->pub);
9675 	dhd_state |= DHD_ATTACH_STATE_CFG80211;
9676 #endif
9677 #ifdef DHD_LOG_DUMP
9678 	dhd_log_dump_init(&dhd->pub);
9679 #endif /* DHD_LOG_DUMP */
9680 #if defined(WL_EXT_IAPSTA) || defined(USE_IW) || defined(WL_ESCAN)
9681 	if (wl_ext_event_attach(net, &dhd->pub) != 0) {
9682 		DHD_ERROR(("wl_ext_event_attach failed\n"));
9683 		goto fail;
9684 	}
9685 #ifdef WL_ESCAN
9686 	/* Attach and link in the escan */
9687 	if (wl_escan_attach(net, &dhd->pub) != 0) {
9688 		DHD_ERROR(("wl_escan_attach failed\n"));
9689 		goto fail;
9690 	}
9691 #endif /* WL_ESCAN */
9692 #ifdef WL_EXT_IAPSTA
9693 	if (wl_ext_iapsta_attach(&dhd->pub) != 0) {
9694 		DHD_ERROR(("wl_ext_iapsta_attach failed\n"));
9695 		goto fail;
9696 	}
9697 #endif /* WL_EXT_IAPSTA */
9698 #endif /* WL_EXT_IAPSTA || USE_IW || WL_ESCAN */
9699 #if defined(WL_WIRELESS_EXT)
9700 	/* Attach and link in the iw */
9701 	if (wl_iw_attach(net, &dhd->pub) != 0) {
9702 		DHD_ERROR(("wl_iw_attach failed\n"));
9703 		goto fail;
9704 	}
9705 	dhd_state |= DHD_ATTACH_STATE_WL_ATTACH;
9706 #endif /* defined(WL_WIRELESS_EXT) */
9707 
9708 #ifdef SHOW_LOGTRACE
9709 	ret = dhd_init_logstrs_array(osh, &dhd->event_data);
9710 	if (ret == BCME_OK) {
9711 		dhd_init_static_strs_array(osh, &dhd->event_data, st_str_file_path, map_file_path);
9712 		dhd_init_static_strs_array(osh, &dhd->event_data, rom_st_str_file_path,
9713 			rom_map_file_path);
9714 		dhd_state |= DHD_ATTACH_LOGTRACE_INIT;
9715 	}
9716 #endif /* SHOW_LOGTRACE */
9717 
9718 #ifdef DEBUGABILITY
9719 	/* attach debug if support */
9720 	if (dhd_os_dbg_attach(&dhd->pub)) {
9721 		DHD_ERROR(("%s debug module attach failed\n", __FUNCTION__));
9722 		goto fail;
9723 	}
9724 
9725 #ifdef DBG_PKT_MON
9726 	dhd->pub.dbg->pkt_mon_lock = dhd_os_spin_lock_init(dhd->pub.osh);
9727 #ifdef DBG_PKT_MON_INIT_DEFAULT
9728 	dhd_os_dbg_attach_pkt_monitor(&dhd->pub);
9729 #endif /* DBG_PKT_MON_INIT_DEFAULT */
9730 #endif /* DBG_PKT_MON */
9731 #endif /* DEBUGABILITY */
9732 #ifdef DHD_PKT_LOGGING
9733 	dhd_os_attach_pktlog(&dhd->pub);
9734 #endif /* DHD_PKT_LOGGING */
9735 
9736 	if (dhd_sta_pool_init(&dhd->pub, DHD_MAX_STA) != BCME_OK) {
9737 		DHD_ERROR(("%s: Initializing %u sta\n", __FUNCTION__, DHD_MAX_STA));
9738 		goto fail;
9739 	}
9740 
9741 
9742 
9743 #ifndef BCMDBUS
9744 	/* Set up the watchdog timer */
9745 	init_timer_compat(&dhd->timer, dhd_watchdog, dhd);
9746 	dhd->default_wd_interval = dhd_watchdog_ms;
9747 
9748 	if (dhd_watchdog_prio >= 0) {
9749 		/* Initialize watchdog thread */
9750 		PROC_START(dhd_watchdog_thread, dhd, &dhd->thr_wdt_ctl, 0, "dhd_watchdog_thread");
9751 		if (dhd->thr_wdt_ctl.thr_pid < 0) {
9752 			goto fail;
9753 		}
9754 
9755 	} else {
9756 		dhd->thr_wdt_ctl.thr_pid = -1;
9757 	}
9758 
9759 #ifdef DHD_PCIE_RUNTIMEPM
9760 	/* Setup up the runtime PM Idlecount timer */
9761 	init_timer_compat(&dhd->rpm_timer, dhd_runtimepm, dhd);
9762 	dhd->rpm_timer_valid = FALSE;
9763 
9764 	dhd->thr_rpm_ctl.thr_pid = DHD_PID_KT_INVALID;
9765 	PROC_START(dhd_rpm_state_thread, dhd, &dhd->thr_rpm_ctl, 0, "dhd_rpm_state_thread");
9766 	if (dhd->thr_rpm_ctl.thr_pid < 0) {
9767 		goto fail;
9768 	}
9769 #endif /* DHD_PCIE_RUNTIMEPM */
9770 
9771 #ifdef DEBUGGER
9772 	debugger_init((void *) bus);
9773 #endif
9774 
9775 	/* Set up the bottom half handler */
9776 	if (dhd_dpc_prio >= 0) {
9777 		/* Initialize DPC thread */
9778 		PROC_START(dhd_dpc_thread, dhd, &dhd->thr_dpc_ctl, 0, "dhd_dpc");
9779 		if (dhd->thr_dpc_ctl.thr_pid < 0) {
9780 			goto fail;
9781 		}
9782 	} else {
9783 		/*  use tasklet for dpc */
9784 		tasklet_init(&dhd->tasklet, dhd_dpc, (ulong)dhd);
9785 		dhd->thr_dpc_ctl.thr_pid = -1;
9786 	}
9787 
9788 	if (dhd->rxthread_enabled) {
9789 		bzero(&dhd->pub.skbbuf[0], sizeof(void *) * MAXSKBPEND);
9790 		/* Initialize RXF thread */
9791 		PROC_START(dhd_rxf_thread, dhd, &dhd->thr_rxf_ctl, 0, "dhd_rxf");
9792 		if (dhd->thr_rxf_ctl.thr_pid < 0) {
9793 			goto fail;
9794 		}
9795 	}
9796 #endif /* !BCMDBUS */
9797 #ifdef SHOW_LOGTRACE
9798 	skb_queue_head_init(&dhd->evt_trace_queue);
9799 #endif /* SHOW_LOGTRACE */
9800 
9801 	dhd_state |= DHD_ATTACH_STATE_THREADS_CREATED;
9802 
9803 #if defined(CONFIG_PM_SLEEP)
9804 	if (!dhd_pm_notifier_registered) {
9805 		dhd_pm_notifier_registered = TRUE;
9806 		dhd->pm_notifier.notifier_call = dhd_pm_callback;
9807 		dhd->pm_notifier.priority = 10;
9808 		register_pm_notifier(&dhd->pm_notifier);
9809 	}
9810 
9811 #endif /* CONFIG_PM_SLEEP */
9812 
9813 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
9814 	dhd->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 20;
9815 	dhd->early_suspend.suspend = dhd_early_suspend;
9816 	dhd->early_suspend.resume = dhd_late_resume;
9817 	register_early_suspend(&dhd->early_suspend);
9818 	dhd_state |= DHD_ATTACH_STATE_EARLYSUSPEND_DONE;
9819 #endif /* CONFIG_HAS_EARLYSUSPEND && DHD_USE_EARLYSUSPEND */
9820 
9821 #ifdef ARP_OFFLOAD_SUPPORT
9822 	dhd->pend_ipaddr = 0;
9823 	if (!dhd_inetaddr_notifier_registered) {
9824 		dhd_inetaddr_notifier_registered = TRUE;
9825 		register_inetaddr_notifier(&dhd_inetaddr_notifier);
9826 	}
9827 #endif /* ARP_OFFLOAD_SUPPORT */
9828 
9829 #if defined(CONFIG_IPV6) && defined(IPV6_NDO_SUPPORT)
9830 	if (!dhd_inet6addr_notifier_registered) {
9831 		dhd_inet6addr_notifier_registered = TRUE;
9832 		register_inet6addr_notifier(&dhd_inet6addr_notifier);
9833 	}
9834 #endif /* CONFIG_IPV6 && IPV6_NDO_SUPPORT */
9835 	dhd->dhd_deferred_wq = dhd_deferred_work_init((void *)dhd);
9836 #ifdef DEBUG_CPU_FREQ
9837 	dhd->new_freq = alloc_percpu(int);
9838 	dhd->freq_trans.notifier_call = dhd_cpufreq_notifier;
9839 	cpufreq_register_notifier(&dhd->freq_trans, CPUFREQ_TRANSITION_NOTIFIER);
9840 #endif
9841 #ifdef DHDTCPACK_SUPPRESS
9842 	dhd_tcpack_suppress_set(&dhd->pub, TCPACK_SUP_DEFAULT);
9843 #endif /* DHDTCPACK_SUPPRESS */
9844 
9845 #if defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW)
9846 #endif /* defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW) */
9847 
9848 
9849 #ifdef DHD_DEBUG_PAGEALLOC
9850 	register_page_corrupt_cb(dhd_page_corrupt_cb, &dhd->pub);
9851 #endif /* DHD_DEBUG_PAGEALLOC */
9852 
9853 #if defined(DHD_LB)
9854 
9855 	dhd_lb_set_default_cpus(dhd);
9856 
9857 	/* Initialize the CPU Masks */
9858 	if (dhd_cpumasks_init(dhd) == 0) {
9859 		/* Now we have the current CPU maps, run through candidacy */
9860 		dhd_select_cpu_candidacy(dhd);
9861 		/*
9862 		* If we are able to initialize CPU masks, lets register to the
9863 		* CPU Hotplug framework to change the CPU for each job dynamically
9864 		* using candidacy algorithm.
9865 		*/
9866 		dhd->cpu_notifier.notifier_call = dhd_cpu_callback;
9867 		register_hotcpu_notifier(&dhd->cpu_notifier); /* Register a callback */
9868 	} else {
9869 		/*
9870 		* We are unable to initialize CPU masks, so candidacy algorithm
9871 		* won't run, but still Load Balancing will be honoured based
9872 		* on the CPUs allocated for a given job statically during init
9873 		*/
9874 		dhd->cpu_notifier.notifier_call = NULL;
9875 		DHD_ERROR(("%s():dhd_cpumasks_init failed CPUs for JOB would be static\n",
9876 			__FUNCTION__));
9877 	}
9878 
9879 #ifdef DHD_LB_TXP
9880 #ifdef DHD_LB_TXP_DEFAULT_ENAB
9881 	/* Trun ON the feature by default */
9882 	atomic_set(&dhd->lb_txp_active, 1);
9883 #else
9884 	/* Trun OFF the feature by default */
9885 	atomic_set(&dhd->lb_txp_active, 0);
9886 #endif /* DHD_LB_TXP_DEFAULT_ENAB */
9887 #endif /* DHD_LB_TXP */
9888 
9889 	DHD_LB_STATS_INIT(&dhd->pub);
9890 
9891 	/* Initialize the Load Balancing Tasklets and Napi object */
9892 #if defined(DHD_LB_TXC)
9893 	tasklet_init(&dhd->tx_compl_tasklet,
9894 		dhd_lb_tx_compl_handler, (ulong)(&dhd->pub));
9895 	INIT_WORK(&dhd->tx_compl_dispatcher_work, dhd_tx_compl_dispatcher_fn);
9896 	DHD_INFO(("%s load balance init tx_compl_tasklet\n", __FUNCTION__));
9897 #endif /* DHD_LB_TXC */
9898 
9899 #if defined(DHD_LB_RXC)
9900 	tasklet_init(&dhd->rx_compl_tasklet,
9901 		dhd_lb_rx_compl_handler, (ulong)(&dhd->pub));
9902 	DHD_INFO(("%s load balance init rx_compl_tasklet\n", __FUNCTION__));
9903 #endif /* DHD_LB_RXC */
9904 
9905 #if defined(DHD_LB_RXP)
9906 	__skb_queue_head_init(&dhd->rx_pend_queue);
9907 	skb_queue_head_init(&dhd->rx_napi_queue);
9908 	/* Initialize the work that dispatches NAPI job to a given core */
9909 	INIT_WORK(&dhd->rx_napi_dispatcher_work, dhd_rx_napi_dispatcher_fn);
9910 	DHD_INFO(("%s load balance init rx_napi_queue\n", __FUNCTION__));
9911 #endif /* DHD_LB_RXP */
9912 
9913 #if defined(DHD_LB_TXP)
9914 	INIT_WORK(&dhd->tx_dispatcher_work, dhd_tx_dispatcher_work);
9915 	skb_queue_head_init(&dhd->tx_pend_queue);
9916 	/* Initialize the work that dispatches TX job to a given core */
9917 	tasklet_init(&dhd->tx_tasklet,
9918 		dhd_lb_tx_handler, (ulong)(dhd));
9919 	DHD_INFO(("%s load balance init tx_pend_queue\n", __FUNCTION__));
9920 #endif /* DHD_LB_TXP */
9921 
9922 	dhd_state |= DHD_ATTACH_STATE_LB_ATTACH_DONE;
9923 #endif /* DHD_LB */
9924 
9925 #ifdef SHOW_LOGTRACE
9926 	INIT_WORK(&dhd->event_log_dispatcher_work, dhd_event_logtrace_process);
9927 #endif /* SHOW_LOGTRACE */
9928 
9929 	DHD_SSSR_MEMPOOL_INIT(&dhd->pub);
9930 
9931 #ifdef REPORT_FATAL_TIMEOUTS
9932 	init_dhd_timeouts(&dhd->pub);
9933 #endif /* REPORT_FATAL_TIMEOUTS */
9934 #ifdef BCMPCIE
9935 	dhd->pub.extended_trap_data = MALLOCZ(osh, BCMPCIE_EXT_TRAP_DATA_MAXLEN);
9936 	if (dhd->pub.extended_trap_data == NULL) {
9937 		DHD_ERROR(("%s: Failed to alloc extended_trap_data\n", __FUNCTION__));
9938 	}
9939 #endif /* BCMPCIE */
9940 
9941 	(void)dhd_sysfs_init(dhd);
9942 
9943 	dhd_state |= DHD_ATTACH_STATE_DONE;
9944 	dhd->dhd_state = dhd_state;
9945 
9946 	dhd_found++;
9947 
9948 #ifdef CSI_SUPPORT
9949 	dhd_csi_init(&dhd->pub);
9950 #endif /* CSI_SUPPORT */
9951 
9952 	return &dhd->pub;
9953 
9954 fail:
9955 	if (dhd_state >= DHD_ATTACH_STATE_DHD_ALLOC) {
9956 		DHD_TRACE(("%s: Calling dhd_detach dhd_state 0x%x &dhd->pub %p\n",
9957 			__FUNCTION__, dhd_state, &dhd->pub));
9958 		dhd->dhd_state = dhd_state;
9959 		dhd_detach(&dhd->pub);
9960 		dhd_free(&dhd->pub);
9961 	}
9962 dhd_null_flag:
9963 	return NULL;
9964 }
9965 
dhd_get_fw_mode(dhd_info_t * dhdinfo)9966 int dhd_get_fw_mode(dhd_info_t *dhdinfo)
9967 {
9968 	if (strstr(dhdinfo->fw_path, "_apsta") != NULL)
9969 		return DHD_FLAG_HOSTAP_MODE;
9970 	if (strstr(dhdinfo->fw_path, "_p2p") != NULL)
9971 		return DHD_FLAG_P2P_MODE;
9972 	if (strstr(dhdinfo->fw_path, "_ibss") != NULL)
9973 		return DHD_FLAG_IBSS_MODE;
9974 	if (strstr(dhdinfo->fw_path, "_mfg") != NULL)
9975 		return DHD_FLAG_MFG_MODE;
9976 
9977 	return DHD_FLAG_STA_MODE;
9978 }
9979 
dhd_bus_get_fw_mode(dhd_pub_t * dhdp)9980 int dhd_bus_get_fw_mode(dhd_pub_t *dhdp)
9981 {
9982 	return dhd_get_fw_mode(dhdp->info);
9983 }
9984 
9985 extern int rkwifi_set_firmware(char *fw, char *nvram);
dhd_update_fw_nv_path(dhd_info_t * dhdinfo)9986 bool dhd_update_fw_nv_path(dhd_info_t *dhdinfo)
9987 {
9988 	int fw_len;
9989 	int nv_len;
9990 	int clm_len;
9991 	int conf_len;
9992 	const char *fw = NULL;
9993 	const char *nv = NULL;
9994 	const char *clm = NULL;
9995 	const char *conf = NULL;
9996 #ifdef DHD_UCODE_DOWNLOAD
9997 	int uc_len;
9998 	const char *uc = NULL;
9999 #endif /* DHD_UCODE_DOWNLOAD */
10000 	char firmware[100] = {0};
10001 	char nvram[100] = {0};
10002 	wifi_adapter_info_t *adapter = dhdinfo->adapter;
10003 	int fw_path_len = sizeof(dhdinfo->fw_path);
10004 	int nv_path_len = sizeof(dhdinfo->nv_path);
10005 
10006 
10007 	/* Update firmware and nvram path. The path may be from adapter info or module parameter
10008 	 * The path from adapter info is used for initialization only (as it won't change).
10009 	 *
10010 	 * The firmware_path/nvram_path module parameter may be changed by the system at run
10011 	 * time. When it changes we need to copy it to dhdinfo->fw_path. Also Android private
10012 	 * command may change dhdinfo->fw_path. As such we need to clear the path info in
10013 	 * module parameter after it is copied. We won't update the path until the module parameter
10014 	 * is changed again (first character is not '\0')
10015 	 */
10016 
10017 	/* set default firmware and nvram path for built-in type driver */
10018 //	if (!dhd_download_fw_on_driverload) {
10019 		rkwifi_set_firmware(firmware, nvram);
10020 #ifdef CONFIG_BCMDHD_FW_PATH
10021 		fw = CONFIG_BCMDHD_FW_PATH;
10022 #else
10023 		fw = firmware;
10024 #endif /* CONFIG_BCMDHD_FW_PATH */
10025 #ifdef CONFIG_BCMDHD_NVRAM_PATH
10026 		nv = CONFIG_BCMDHD_NVRAM_PATH;
10027 #else
10028 		nv = nvram;
10029 #endif /* CONFIG_BCMDHD_NVRAM_PATH */
10030 //	}
10031 
10032 	/* check if we need to initialize the path */
10033 	if (dhdinfo->fw_path[0] == '\0') {
10034 		if (adapter && adapter->fw_path && adapter->fw_path[0] != '\0')
10035 			fw = adapter->fw_path;
10036 
10037 	}
10038 	if (dhdinfo->nv_path[0] == '\0') {
10039 		if (adapter && adapter->nv_path && adapter->nv_path[0] != '\0')
10040 			nv = adapter->nv_path;
10041 	}
10042 	if (dhdinfo->clm_path[0] == '\0') {
10043 		if (adapter && adapter->clm_path && adapter->clm_path[0] != '\0')
10044 			clm = adapter->clm_path;
10045 	}
10046 	if (dhdinfo->conf_path[0] == '\0') {
10047 		if (adapter && adapter->conf_path && adapter->conf_path[0] != '\0')
10048 			conf = adapter->conf_path;
10049 	}
10050 
10051 	/* Use module parameter if it is valid, EVEN IF the path has not been initialized
10052 	 *
10053 	 * TODO: need a solution for multi-chip, can't use the same firmware for all chips
10054 	 */
10055 	if (firmware_path[0] != '\0')
10056 		fw = firmware_path;
10057 	if (nvram_path[0] != '\0')
10058 		nv = nvram_path;
10059 	if (clm_path[0] != '\0')
10060 		clm = clm_path;
10061 	if (config_path[0] != '\0')
10062 		conf = config_path;
10063 #ifdef DHD_UCODE_DOWNLOAD
10064 	if (ucode_path[0] != '\0')
10065 		uc = ucode_path;
10066 #endif /* DHD_UCODE_DOWNLOAD */
10067 
10068 	if (fw && fw[0] != '\0') {
10069 		fw_len = strlen(fw);
10070 		if (fw_len >= fw_path_len) {
10071 			DHD_ERROR(("fw path len exceeds max len of dhdinfo->fw_path\n"));
10072 			return FALSE;
10073 		}
10074 		strncpy(dhdinfo->fw_path, fw, fw_path_len);
10075 		if (dhdinfo->fw_path[fw_len-1] == '\n')
10076 		       dhdinfo->fw_path[fw_len-1] = '\0';
10077 	}
10078 	if (nv && nv[0] != '\0') {
10079 		nv_len = strlen(nv);
10080 		if (nv_len >= nv_path_len) {
10081 			DHD_ERROR(("nvram path len exceeds max len of dhdinfo->nv_path\n"));
10082 			return FALSE;
10083 		}
10084 		memset(dhdinfo->nv_path, 0, nv_path_len);
10085 		strncpy(dhdinfo->nv_path, nv, nv_path_len);
10086 #ifdef DHD_USE_SINGLE_NVRAM_FILE
10087 		/* Remove "_net" or "_mfg" tag from current nvram path */
10088 		{
10089 			char *nvram_tag = "nvram_";
10090 			char *ext_tag = ".txt";
10091 			char *sp_nvram = strnstr(dhdinfo->nv_path, nvram_tag, nv_path_len);
10092 			bool valid_buf = sp_nvram && ((uint32)(sp_nvram + strlen(nvram_tag) +
10093 				strlen(ext_tag) - dhdinfo->nv_path) <= nv_path_len);
10094 			if (valid_buf) {
10095 				char *sp = sp_nvram + strlen(nvram_tag) - 1;
10096 				uint32 padding_size = (uint32)(dhdinfo->nv_path +
10097 					nv_path_len - sp);
10098 				memset(sp, 0, padding_size);
10099 				strncat(dhdinfo->nv_path, ext_tag, strlen(ext_tag));
10100 				nv_len = strlen(dhdinfo->nv_path);
10101 				DHD_INFO(("%s: new nvram path = %s\n",
10102 					__FUNCTION__, dhdinfo->nv_path));
10103 			} else if (sp_nvram) {
10104 				DHD_ERROR(("%s: buffer space for nvram path is not enough\n",
10105 					__FUNCTION__));
10106 				return FALSE;
10107 			} else {
10108 				DHD_ERROR(("%s: Couldn't find the nvram tag. current"
10109 					" nvram path = %s\n", __FUNCTION__, dhdinfo->nv_path));
10110 			}
10111 		}
10112 #endif /* DHD_USE_SINGLE_NVRAM_FILE */
10113 		if (dhdinfo->nv_path[nv_len-1] == '\n')
10114 		       dhdinfo->nv_path[nv_len-1] = '\0';
10115 	}
10116 	if (clm && clm[0] != '\0') {
10117 		clm_len = strlen(clm);
10118 		if (clm_len >= sizeof(dhdinfo->clm_path)) {
10119 			DHD_ERROR(("clm path len exceeds max len of dhdinfo->clm_path\n"));
10120 			return FALSE;
10121 		}
10122 		strncpy(dhdinfo->clm_path, clm, sizeof(dhdinfo->clm_path));
10123 		if (dhdinfo->clm_path[clm_len-1] == '\n')
10124 		       dhdinfo->clm_path[clm_len-1] = '\0';
10125 	}
10126 	if (conf && conf[0] != '\0') {
10127 		conf_len = strlen(conf);
10128 		if (conf_len >= sizeof(dhdinfo->conf_path)) {
10129 			DHD_ERROR(("config path len exceeds max len of dhdinfo->conf_path\n"));
10130 			return FALSE;
10131 		}
10132 		strncpy(dhdinfo->conf_path, conf, sizeof(dhdinfo->conf_path));
10133 		if (dhdinfo->conf_path[conf_len-1] == '\n')
10134 		       dhdinfo->conf_path[conf_len-1] = '\0';
10135 	}
10136 #ifdef DHD_UCODE_DOWNLOAD
10137 	if (uc && uc[0] != '\0') {
10138 		uc_len = strlen(uc);
10139 		if (uc_len >= sizeof(dhdinfo->uc_path)) {
10140 			DHD_ERROR(("uc path len exceeds max len of dhdinfo->uc_path\n"));
10141 			return FALSE;
10142 		}
10143 		strncpy(dhdinfo->uc_path, uc, sizeof(dhdinfo->uc_path));
10144 		if (dhdinfo->uc_path[uc_len-1] == '\n')
10145 		       dhdinfo->uc_path[uc_len-1] = '\0';
10146 	}
10147 #endif /* DHD_UCODE_DOWNLOAD */
10148 
10149 #if 0
10150 	/* clear the path in module parameter */
10151 	if (dhd_download_fw_on_driverload) {
10152 		firmware_path[0] = '\0';
10153 		nvram_path[0] = '\0';
10154 		clm_path[0] = '\0';
10155 		config_path[0] = '\0';
10156 	}
10157 #endif
10158 #ifdef DHD_UCODE_DOWNLOAD
10159 	ucode_path[0] = '\0';
10160 	DHD_ERROR(("ucode path: %s\n", dhdinfo->uc_path));
10161 #endif /* DHD_UCODE_DOWNLOAD */
10162 
10163 #ifndef BCMEMBEDIMAGE
10164 	/* fw_path and nv_path are not mandatory for BCMEMBEDIMAGE */
10165 	if (dhdinfo->fw_path[0] == '\0') {
10166 		DHD_ERROR(("firmware path not found\n"));
10167 		return FALSE;
10168 	}
10169 	if (dhdinfo->nv_path[0] == '\0') {
10170 		DHD_ERROR(("nvram path not found\n"));
10171 		return FALSE;
10172 	}
10173 #endif /* BCMEMBEDIMAGE */
10174 
10175 	return TRUE;
10176 }
10177 
10178 #if defined(BT_OVER_SDIO)
dhd_update_btfw_path(dhd_info_t * dhdinfo,char * btfw_path)10179 extern bool dhd_update_btfw_path(dhd_info_t *dhdinfo, char* btfw_path)
10180 {
10181 	int fw_len;
10182 	const char *fw = NULL;
10183 	wifi_adapter_info_t *adapter = dhdinfo->adapter;
10184 
10185 
10186 	/* Update bt firmware path. The path may be from adapter info or module parameter
10187 	 * The path from adapter info is used for initialization only (as it won't change).
10188 	 *
10189 	 * The btfw_path module parameter may be changed by the system at run
10190 	 * time. When it changes we need to copy it to dhdinfo->btfw_path. Also Android private
10191 	 * command may change dhdinfo->btfw_path. As such we need to clear the path info in
10192 	 * module parameter after it is copied. We won't update the path until the module parameter
10193 	 * is changed again (first character is not '\0')
10194 	 */
10195 
10196 	/* set default firmware and nvram path for built-in type driver */
10197 	if (!dhd_download_fw_on_driverload) {
10198 #ifdef CONFIG_BCMDHD_BTFW_PATH
10199 		fw = CONFIG_BCMDHD_BTFW_PATH;
10200 #endif /* CONFIG_BCMDHD_FW_PATH */
10201 	}
10202 
10203 	/* check if we need to initialize the path */
10204 	if (dhdinfo->btfw_path[0] == '\0') {
10205 		if (adapter && adapter->btfw_path && adapter->btfw_path[0] != '\0')
10206 			fw = adapter->btfw_path;
10207 	}
10208 
10209 	/* Use module parameter if it is valid, EVEN IF the path has not been initialized
10210 	 */
10211 	if (btfw_path[0] != '\0')
10212 		fw = btfw_path;
10213 
10214 	if (fw && fw[0] != '\0') {
10215 		fw_len = strlen(fw);
10216 		if (fw_len >= sizeof(dhdinfo->btfw_path)) {
10217 			DHD_ERROR(("fw path len exceeds max len of dhdinfo->btfw_path\n"));
10218 			return FALSE;
10219 		}
10220 		strncpy(dhdinfo->btfw_path, fw, sizeof(dhdinfo->btfw_path));
10221 		if (dhdinfo->btfw_path[fw_len-1] == '\n')
10222 		       dhdinfo->btfw_path[fw_len-1] = '\0';
10223 	}
10224 
10225 	/* clear the path in module parameter */
10226 	btfw_path[0] = '\0';
10227 
10228 	if (dhdinfo->btfw_path[0] == '\0') {
10229 		DHD_ERROR(("bt firmware path not found\n"));
10230 		return FALSE;
10231 	}
10232 
10233 	return TRUE;
10234 }
10235 #endif /* defined (BT_OVER_SDIO) */
10236 
10237 
10238 #ifdef CUSTOMER_HW4_DEBUG
dhd_validate_chipid(dhd_pub_t * dhdp)10239 bool dhd_validate_chipid(dhd_pub_t *dhdp)
10240 {
10241 	uint chipid = dhd_bus_chip_id(dhdp);
10242 	uint config_chipid;
10243 
10244 #ifdef BCM4361_CHIP
10245 	config_chipid = BCM4361_CHIP_ID;
10246 #elif defined(BCM4359_CHIP)
10247 	config_chipid = BCM4359_CHIP_ID;
10248 #elif defined(BCM4358_CHIP)
10249 	config_chipid = BCM4358_CHIP_ID;
10250 #elif defined(BCM4354_CHIP)
10251 	config_chipid = BCM4354_CHIP_ID;
10252 #elif defined(BCM4339_CHIP)
10253 	config_chipid = BCM4339_CHIP_ID;
10254 #elif defined(BCM43349_CHIP)
10255 	config_chipid = BCM43349_CHIP_ID;
10256 #elif defined(BCM4335_CHIP)
10257 	config_chipid = BCM4335_CHIP_ID;
10258 #elif defined(BCM43241_CHIP)
10259 	config_chipid = BCM4324_CHIP_ID;
10260 #elif defined(BCM4330_CHIP)
10261 	config_chipid = BCM4330_CHIP_ID;
10262 #elif defined(BCM43430_CHIP)
10263 	config_chipid = BCM43430_CHIP_ID;
10264 #elif defined(BCM43018_CHIP)
10265 	config_chipid = BCM43018_CHIP_ID;
10266 #elif defined(BCM43455_CHIP)
10267 	config_chipid = BCM4345_CHIP_ID;
10268 #elif defined(BCM4334W_CHIP)
10269 	config_chipid = BCM43342_CHIP_ID;
10270 #elif defined(BCM43454_CHIP)
10271 	config_chipid = BCM43454_CHIP_ID;
10272 #elif defined(BCM43012_CHIP_)
10273 	config_chipid = BCM43012_CHIP_ID;
10274 #else
10275 	DHD_ERROR(("%s: Unknown chip id, if you use new chipset,"
10276 		" please add CONFIG_BCMXXXX into the Kernel and"
10277 		" BCMXXXX_CHIP definition into the DHD driver\n",
10278 		__FUNCTION__));
10279 	config_chipid = 0;
10280 
10281 	return FALSE;
10282 #endif /* BCM4354_CHIP */
10283 
10284 #ifdef SUPPORT_MULTIPLE_CHIP_4345X
10285 	if (config_chipid == BCM43454_CHIP_ID || config_chipid == BCM4345_CHIP_ID) {
10286 		return TRUE;
10287 	}
10288 #endif /* SUPPORT_MULTIPLE_CHIP_4345X */
10289 #if defined(BCM4359_CHIP)
10290 	if (chipid == BCM4355_CHIP_ID && config_chipid == BCM4359_CHIP_ID) {
10291 		return TRUE;
10292 	}
10293 #endif /* BCM4359_CHIP */
10294 #if defined(BCM4361_CHIP)
10295 	if (chipid == BCM4347_CHIP_ID && config_chipid == BCM4361_CHIP_ID) {
10296 		return TRUE;
10297 	}
10298 #endif /* BCM4361_CHIP */
10299 
10300 	return config_chipid == chipid;
10301 }
10302 #endif /* CUSTOMER_HW4_DEBUG */
10303 
10304 #if defined(BT_OVER_SDIO)
dhd_bt_get_pub_hndl(void)10305 wlan_bt_handle_t dhd_bt_get_pub_hndl(void)
10306 {
10307 	DHD_ERROR(("%s: g_dhd_pub %p\n", __FUNCTION__, g_dhd_pub));
10308 	/* assuming that dhd_pub_t type pointer is available from a global variable */
10309 	return (wlan_bt_handle_t) g_dhd_pub;
10310 } EXPORT_SYMBOL(dhd_bt_get_pub_hndl);
10311 
dhd_download_btfw(wlan_bt_handle_t handle,char * btfw_path)10312 int dhd_download_btfw(wlan_bt_handle_t handle, char* btfw_path)
10313 {
10314 	int ret = -1;
10315 	dhd_pub_t *dhdp = (dhd_pub_t *)handle;
10316 	dhd_info_t *dhd = (dhd_info_t*)dhdp->info;
10317 
10318 
10319 	/* Download BT firmware image to the dongle */
10320 	if (dhd->pub.busstate == DHD_BUS_DATA && dhd_update_btfw_path(dhd, btfw_path)) {
10321 		DHD_INFO(("%s: download btfw from: %s\n", __FUNCTION__, dhd->btfw_path));
10322 		ret = dhd_bus_download_btfw(dhd->pub.bus, dhd->pub.osh, dhd->btfw_path);
10323 		if (ret < 0) {
10324 			DHD_ERROR(("%s: failed to download btfw from: %s\n",
10325 				__FUNCTION__, dhd->btfw_path));
10326 			return ret;
10327 		}
10328 	}
10329 	return ret;
10330 } EXPORT_SYMBOL(dhd_download_btfw);
10331 #endif /* defined (BT_OVER_SDIO) */
10332 
10333 #ifndef BCMDBUS
10334 int
dhd_bus_start(dhd_pub_t * dhdp)10335 dhd_bus_start(dhd_pub_t *dhdp)
10336 {
10337 	int ret = -1;
10338 	dhd_info_t *dhd = (dhd_info_t*)dhdp->info;
10339 	unsigned long flags;
10340 
10341 #if defined(DHD_DEBUG) && defined(BCMSDIO)
10342 	int fw_download_start = 0, fw_download_end = 0, f2_sync_start = 0, f2_sync_end = 0;
10343 #endif /* DHD_DEBUG && BCMSDIO */
10344 	ASSERT(dhd);
10345 
10346 	DHD_TRACE(("Enter %s:\n", __FUNCTION__));
10347 
10348 	DHD_PERIM_LOCK(dhdp);
10349 #ifdef HOFFLOAD_MODULES
10350 	dhd_linux_get_modfw_address(dhdp);
10351 #endif
10352 	/* try to download image and nvram to the dongle */
10353 	if  (dhd->pub.busstate == DHD_BUS_DOWN && dhd_update_fw_nv_path(dhd)) {
10354 		/* Indicate FW Download has not yet done */
10355 		dhd->pub.fw_download_done = FALSE;
10356 		DHD_INFO(("%s download fw %s, nv %s, conf %s\n",
10357 			__FUNCTION__, dhd->fw_path, dhd->nv_path, dhd->conf_path));
10358 #if defined(DHD_DEBUG) && defined(BCMSDIO)
10359 		fw_download_start = OSL_SYSUPTIME();
10360 #endif /* DHD_DEBUG && BCMSDIO */
10361 		ret = dhd_bus_download_firmware(dhd->pub.bus, dhd->pub.osh,
10362 			dhd->fw_path, dhd->nv_path, dhd->clm_path, dhd->conf_path);
10363 #if defined(DHD_DEBUG) && defined(BCMSDIO)
10364 		fw_download_end = OSL_SYSUPTIME();
10365 #endif /* DHD_DEBUG && BCMSDIO */
10366 		if (ret < 0) {
10367 			DHD_ERROR(("%s: failed to download firmware %s\n",
10368 				__FUNCTION__, dhd->fw_path));
10369 			DHD_PERIM_UNLOCK(dhdp);
10370 			return ret;
10371 		}
10372 		/* Indicate FW Download has succeeded */
10373 		dhd->pub.fw_download_done = TRUE;
10374 	}
10375 	if (dhd->pub.busstate != DHD_BUS_LOAD) {
10376 		DHD_PERIM_UNLOCK(dhdp);
10377 		return -ENETDOWN;
10378 	}
10379 
10380 #ifdef BCMSDIO
10381 	dhd_os_sdlock(dhdp);
10382 #endif /* BCMSDIO */
10383 
10384 	/* Start the watchdog timer */
10385 	dhd->pub.tickcnt = 0;
10386 	dhd_os_wd_timer(&dhd->pub, dhd_watchdog_ms);
10387 
10388 	/* Bring up the bus */
10389 	if ((ret = dhd_bus_init(&dhd->pub, FALSE)) != 0) {
10390 
10391 		DHD_ERROR(("%s, dhd_bus_init failed %d\n", __FUNCTION__, ret));
10392 #ifdef BCMSDIO
10393 		dhd_os_sdunlock(dhdp);
10394 #endif /* BCMSDIO */
10395 		DHD_PERIM_UNLOCK(dhdp);
10396 		return ret;
10397 	}
10398 
10399 	DHD_ENABLE_RUNTIME_PM(&dhd->pub);
10400 
10401 #ifdef DHD_ULP
10402 	dhd_ulp_set_ulp_state(dhdp, DHD_ULP_DISABLED);
10403 #endif /* DHD_ULP */
10404 #if defined(OOB_INTR_ONLY) || defined(BCMPCIE_OOB_HOST_WAKE)
10405 	/* Host registration for OOB interrupt */
10406 	if (dhd_bus_oob_intr_register(dhdp)) {
10407 		/* deactivate timer and wait for the handler to finish */
10408 #if !defined(BCMPCIE_OOB_HOST_WAKE)
10409 		DHD_GENERAL_LOCK(&dhd->pub, flags);
10410 		dhd->wd_timer_valid = FALSE;
10411 		DHD_GENERAL_UNLOCK(&dhd->pub, flags);
10412 		del_timer_sync(&dhd->timer);
10413 
10414 #endif /* !BCMPCIE_OOB_HOST_WAKE */
10415 		DHD_DISABLE_RUNTIME_PM(&dhd->pub);
10416 		DHD_PERIM_UNLOCK(dhdp);
10417 		DHD_ERROR(("%s Host failed to register for OOB\n", __FUNCTION__));
10418 		return -ENODEV;
10419 	}
10420 
10421 #if defined(BCMPCIE_OOB_HOST_WAKE)
10422 	dhd_bus_oob_intr_set(dhdp, TRUE);
10423 #else
10424 	/* Enable oob at firmware */
10425 	dhd_enable_oob_intr(dhd->pub.bus, TRUE);
10426 #endif /* BCMPCIE_OOB_HOST_WAKE */
10427 #elif defined(FORCE_WOWLAN)
10428 	/* Enable oob at firmware */
10429 	dhd_enable_oob_intr(dhd->pub.bus, TRUE);
10430 #endif
10431 #ifdef PCIE_FULL_DONGLE
10432 	{
10433 		/* max_h2d_rings includes H2D common rings */
10434 		uint32 max_h2d_rings = dhd_bus_max_h2d_queues(dhd->pub.bus);
10435 
10436 		DHD_ERROR(("%s: Initializing %u h2drings\n", __FUNCTION__,
10437 			max_h2d_rings));
10438 		if ((ret = dhd_flow_rings_init(&dhd->pub, max_h2d_rings)) != BCME_OK) {
10439 #ifdef BCMSDIO
10440 			dhd_os_sdunlock(dhdp);
10441 #endif /* BCMSDIO */
10442 			DHD_PERIM_UNLOCK(dhdp);
10443 			return ret;
10444 		}
10445 	}
10446 #endif /* PCIE_FULL_DONGLE */
10447 
10448 	/* Do protocol initialization necessary for IOCTL/IOVAR */
10449 	ret = dhd_prot_init(&dhd->pub);
10450 	if (unlikely(ret) != BCME_OK) {
10451 		DHD_PERIM_UNLOCK(dhdp);
10452 		DHD_OS_WD_WAKE_UNLOCK(&dhd->pub);
10453 		return ret;
10454 	}
10455 
10456 	/* If bus is not ready, can't come up */
10457 	if (dhd->pub.busstate != DHD_BUS_DATA) {
10458 		DHD_GENERAL_LOCK(&dhd->pub, flags);
10459 		dhd->wd_timer_valid = FALSE;
10460 		DHD_GENERAL_UNLOCK(&dhd->pub, flags);
10461 		del_timer_sync(&dhd->timer);
10462 		DHD_ERROR(("%s failed bus is not ready\n", __FUNCTION__));
10463 		DHD_DISABLE_RUNTIME_PM(&dhd->pub);
10464 #ifdef BCMSDIO
10465 		dhd_os_sdunlock(dhdp);
10466 #endif /* BCMSDIO */
10467 		DHD_PERIM_UNLOCK(dhdp);
10468 		return -ENODEV;
10469 	}
10470 
10471 #ifdef BCMSDIO
10472 	dhd_os_sdunlock(dhdp);
10473 #endif /* BCMSDIO */
10474 
10475 	/* Bus is ready, query any dongle information */
10476 #if defined(DHD_DEBUG) && defined(BCMSDIO)
10477 	f2_sync_start = OSL_SYSUPTIME();
10478 #endif /* DHD_DEBUG && BCMSDIO */
10479 	if ((ret = dhd_sync_with_dongle(&dhd->pub)) < 0) {
10480 		DHD_GENERAL_LOCK(&dhd->pub, flags);
10481 		dhd->wd_timer_valid = FALSE;
10482 		DHD_GENERAL_UNLOCK(&dhd->pub, flags);
10483 		del_timer_sync(&dhd->timer);
10484 		DHD_ERROR(("%s failed to sync with dongle\n", __FUNCTION__));
10485 		DHD_OS_WD_WAKE_UNLOCK(&dhd->pub);
10486 		DHD_PERIM_UNLOCK(dhdp);
10487 		return ret;
10488 	}
10489 #if defined(CONFIG_SOC_EXYNOS8895)
10490 	DHD_ERROR(("%s: Enable L1ss EP side\n", __FUNCTION__));
10491 	exynos_pcie_l1ss_ctrl(1, PCIE_L1SS_CTRL_WIFI);
10492 #endif /* CONFIG_SOC_EXYNOS8895 */
10493 
10494 #if defined(DHD_DEBUG) && defined(BCMSDIO)
10495 	f2_sync_end = OSL_SYSUPTIME();
10496 	DHD_PRINT("Time taken for FW download and F2 ready is: %d msec\n",
10497 			(fw_download_end - fw_download_start) + (f2_sync_end - f2_sync_start));
10498 #endif /* DHD_DEBUG && BCMSDIO */
10499 
10500 #ifdef ARP_OFFLOAD_SUPPORT
10501 	if (dhd->pend_ipaddr) {
10502 #ifdef AOE_IP_ALIAS_SUPPORT
10503 		aoe_update_host_ipv4_table(&dhd->pub, dhd->pend_ipaddr, TRUE, 0);
10504 #endif /* AOE_IP_ALIAS_SUPPORT */
10505 		dhd->pend_ipaddr = 0;
10506 	}
10507 #endif /* ARP_OFFLOAD_SUPPORT */
10508 
10509 #if defined(TRAFFIC_MGMT_DWM)
10510 	bzero(&dhd->pub.dhd_tm_dwm_tbl, sizeof(dhd_trf_mgmt_dwm_tbl_t));
10511 #endif
10512 	DHD_PERIM_UNLOCK(dhdp);
10513 	return 0;
10514 }
10515 #endif /* !BCMDBUS */
10516 
10517 #ifdef WLTDLS
_dhd_tdls_enable(dhd_pub_t * dhd,bool tdls_on,bool auto_on,struct ether_addr * mac)10518 int _dhd_tdls_enable(dhd_pub_t *dhd, bool tdls_on, bool auto_on, struct ether_addr *mac)
10519 {
10520 	uint32 tdls = tdls_on;
10521 	int ret = 0;
10522 	uint32 tdls_auto_op = 0;
10523 	uint32 tdls_idle_time = CUSTOM_TDLS_IDLE_MODE_SETTING;
10524 	int32 tdls_rssi_high = CUSTOM_TDLS_RSSI_THRESHOLD_HIGH;
10525 	int32 tdls_rssi_low = CUSTOM_TDLS_RSSI_THRESHOLD_LOW;
10526 	BCM_REFERENCE(mac);
10527 	if (!FW_SUPPORTED(dhd, tdls))
10528 		return BCME_ERROR;
10529 
10530 	if (dhd->tdls_enable == tdls_on)
10531 		goto auto_mode;
10532 	ret = dhd_iovar(dhd, 0, "tdls_enable", (char *)&tdls, sizeof(tdls), NULL, 0, TRUE);
10533 	if (ret < 0) {
10534 		DHD_ERROR(("%s: tdls %d failed %d\n", __FUNCTION__, tdls, ret));
10535 		goto exit;
10536 	}
10537 	dhd->tdls_enable = tdls_on;
10538 auto_mode:
10539 
10540 	tdls_auto_op = auto_on;
10541 	ret = dhd_iovar(dhd, 0, "tdls_auto_op", (char *)&tdls_auto_op, sizeof(tdls_auto_op), NULL,
10542 			0, TRUE);
10543 	if (ret < 0) {
10544 		DHD_ERROR(("%s: tdls_auto_op failed %d\n", __FUNCTION__, ret));
10545 		goto exit;
10546 	}
10547 
10548 	if (tdls_auto_op) {
10549 		ret = dhd_iovar(dhd, 0, "tdls_idle_time", (char *)&tdls_idle_time,
10550 				sizeof(tdls_idle_time), NULL, 0, TRUE);
10551 		if (ret < 0) {
10552 			DHD_ERROR(("%s: tdls_idle_time failed %d\n", __FUNCTION__, ret));
10553 			goto exit;
10554 		}
10555 		ret = dhd_iovar(dhd, 0, "tdls_rssi_high", (char *)&tdls_rssi_high,
10556 				sizeof(tdls_rssi_high), NULL, 0, TRUE);
10557 		if (ret < 0) {
10558 			DHD_ERROR(("%s: tdls_rssi_high failed %d\n", __FUNCTION__, ret));
10559 			goto exit;
10560 		}
10561 		ret = dhd_iovar(dhd, 0, "tdls_rssi_low", (char *)&tdls_rssi_low,
10562 				sizeof(tdls_rssi_low), NULL, 0, TRUE);
10563 		if (ret < 0) {
10564 			DHD_ERROR(("%s: tdls_rssi_low failed %d\n", __FUNCTION__, ret));
10565 			goto exit;
10566 		}
10567 	}
10568 
10569 exit:
10570 	return ret;
10571 }
10572 
dhd_tdls_enable(struct net_device * dev,bool tdls_on,bool auto_on,struct ether_addr * mac)10573 int dhd_tdls_enable(struct net_device *dev, bool tdls_on, bool auto_on, struct ether_addr *mac)
10574 {
10575 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
10576 	int ret = 0;
10577 	if (dhd)
10578 		ret = _dhd_tdls_enable(&dhd->pub, tdls_on, auto_on, mac);
10579 	else
10580 		ret = BCME_ERROR;
10581 	return ret;
10582 }
10583 
10584 int
dhd_tdls_set_mode(dhd_pub_t * dhd,bool wfd_mode)10585 dhd_tdls_set_mode(dhd_pub_t *dhd, bool wfd_mode)
10586 {
10587 	int ret = 0;
10588 	bool auto_on = false;
10589 	uint32 mode =  wfd_mode;
10590 
10591 #ifdef ENABLE_TDLS_AUTO_MODE
10592 	if (wfd_mode) {
10593 		auto_on = false;
10594 	} else {
10595 		auto_on = true;
10596 	}
10597 #else
10598 	auto_on = false;
10599 #endif /* ENABLE_TDLS_AUTO_MODE */
10600 	ret = _dhd_tdls_enable(dhd, false, auto_on, NULL);
10601 	if (ret < 0) {
10602 		DHD_ERROR(("Disable tdls_auto_op failed. %d\n", ret));
10603 		return ret;
10604 	}
10605 
10606 	ret = dhd_iovar(dhd, 0, "tdls_wfd_mode", (char *)&mode, sizeof(mode), NULL, 0, TRUE);
10607 	if ((ret < 0) && (ret != BCME_UNSUPPORTED)) {
10608 		DHD_ERROR(("%s: tdls_wfd_mode faile_wfd_mode %d\n", __FUNCTION__, ret));
10609 		return ret;
10610 	}
10611 
10612 	ret = _dhd_tdls_enable(dhd, true, auto_on, NULL);
10613 	if (ret < 0) {
10614 		DHD_ERROR(("enable tdls_auto_op failed. %d\n", ret));
10615 		return ret;
10616 	}
10617 
10618 	dhd->tdls_mode = mode;
10619 	return ret;
10620 }
10621 #ifdef PCIE_FULL_DONGLE
dhd_tdls_update_peer_info(dhd_pub_t * dhdp,wl_event_msg_t * event)10622 int dhd_tdls_update_peer_info(dhd_pub_t *dhdp, wl_event_msg_t *event)
10623 {
10624 	dhd_pub_t *dhd_pub = dhdp;
10625 	tdls_peer_node_t *cur = dhd_pub->peer_tbl.node;
10626 	tdls_peer_node_t *new = NULL, *prev = NULL;
10627 	int ifindex = dhd_ifname2idx(dhd_pub->info, event->ifname);
10628 	uint8 *da = (uint8 *)&event->addr.octet[0];
10629 	bool connect = FALSE;
10630 	uint32 reason = ntoh32(event->reason);
10631 	unsigned long flags;
10632 
10633 	if (reason == WLC_E_TDLS_PEER_CONNECTED)
10634 		connect = TRUE;
10635 	else if (reason == WLC_E_TDLS_PEER_DISCONNECTED)
10636 		connect = FALSE;
10637 	else
10638 	{
10639 		DHD_ERROR(("%s: TDLS Event reason is unknown\n", __FUNCTION__));
10640 		return BCME_ERROR;
10641 	}
10642 	if (ifindex == DHD_BAD_IF)
10643 		return BCME_ERROR;
10644 
10645 	if (connect) {
10646 		while (cur != NULL) {
10647 			if (!memcmp(da, cur->addr, ETHER_ADDR_LEN)) {
10648 				DHD_ERROR(("%s: TDLS Peer exist already %d\n",
10649 					__FUNCTION__, __LINE__));
10650 				return BCME_ERROR;
10651 			}
10652 			cur = cur->next;
10653 		}
10654 
10655 		new = MALLOC(dhd_pub->osh, sizeof(tdls_peer_node_t));
10656 		if (new == NULL) {
10657 			DHD_ERROR(("%s: Failed to allocate memory\n", __FUNCTION__));
10658 			return BCME_ERROR;
10659 		}
10660 		memcpy(new->addr, da, ETHER_ADDR_LEN);
10661 		DHD_TDLS_LOCK(&dhdp->tdls_lock, flags);
10662 		new->next = dhd_pub->peer_tbl.node;
10663 		dhd_pub->peer_tbl.node = new;
10664 		dhd_pub->peer_tbl.tdls_peer_count++;
10665 		DHD_TDLS_UNLOCK(&dhdp->tdls_lock, flags);
10666 
10667 	} else {
10668 		while (cur != NULL) {
10669 			if (!memcmp(da, cur->addr, ETHER_ADDR_LEN)) {
10670 				dhd_flow_rings_delete_for_peer(dhd_pub, (uint8)ifindex, da);
10671 				DHD_TDLS_LOCK(&dhdp->tdls_lock, flags);
10672 				if (prev)
10673 					prev->next = cur->next;
10674 				else
10675 					dhd_pub->peer_tbl.node = cur->next;
10676 				MFREE(dhd_pub->osh, cur, sizeof(tdls_peer_node_t));
10677 				dhd_pub->peer_tbl.tdls_peer_count--;
10678 				DHD_TDLS_UNLOCK(&dhdp->tdls_lock, flags);
10679 				return BCME_OK;
10680 			}
10681 			prev = cur;
10682 			cur = cur->next;
10683 		}
10684 		DHD_ERROR(("%s: TDLS Peer Entry Not found\n", __FUNCTION__));
10685 	}
10686 	return BCME_OK;
10687 }
10688 #endif /* PCIE_FULL_DONGLE */
10689 #endif
10690 
dhd_is_concurrent_mode(dhd_pub_t * dhd)10691 bool dhd_is_concurrent_mode(dhd_pub_t *dhd)
10692 {
10693 	if (!dhd)
10694 		return FALSE;
10695 
10696 	if (dhd->op_mode & DHD_FLAG_CONCURR_MULTI_CHAN_MODE)
10697 		return TRUE;
10698 	else if ((dhd->op_mode & DHD_FLAG_CONCURR_SINGLE_CHAN_MODE) ==
10699 		DHD_FLAG_CONCURR_SINGLE_CHAN_MODE)
10700 		return TRUE;
10701 	else
10702 		return FALSE;
10703 }
10704 #if !defined(AP) && defined(WLP2P)
10705 /* From Android JerryBean release, the concurrent mode is enabled by default and the firmware
10706  * name would be fw_bcmdhd.bin. So we need to determine whether P2P is enabled in the STA
10707  * firmware and accordingly enable concurrent mode (Apply P2P settings). SoftAP firmware
10708  * would still be named as fw_bcmdhd_apsta.
10709  */
10710 uint32
dhd_get_concurrent_capabilites(dhd_pub_t * dhd)10711 dhd_get_concurrent_capabilites(dhd_pub_t *dhd)
10712 {
10713 	int32 ret = 0;
10714 	char buf[WLC_IOCTL_SMLEN];
10715 	bool mchan_supported = FALSE;
10716 	/* if dhd->op_mode is already set for HOSTAP and Manufacturing
10717 	 * test mode, that means we only will use the mode as it is
10718 	 */
10719 	if (dhd->op_mode & (DHD_FLAG_HOSTAP_MODE | DHD_FLAG_MFG_MODE))
10720 		return 0;
10721 	if (FW_SUPPORTED(dhd, vsdb)) {
10722 		mchan_supported = TRUE;
10723 	}
10724 	if (!FW_SUPPORTED(dhd, p2p)) {
10725 		DHD_TRACE(("Chip does not support p2p\n"));
10726 		return 0;
10727 	} else {
10728 		/* Chip supports p2p but ensure that p2p is really implemented in firmware or not */
10729 		memset(buf, 0, sizeof(buf));
10730 		ret = dhd_iovar(dhd, 0, "p2p", NULL, 0, (char *)&buf,
10731 				sizeof(buf), FALSE);
10732 		if (ret < 0) {
10733 			DHD_ERROR(("%s: Get P2P failed (error=%d)\n", __FUNCTION__, ret));
10734 			return 0;
10735 		} else {
10736 			if (buf[0] == 1) {
10737 				/* By default, chip supports single chan concurrency,
10738 				* now lets check for mchan
10739 				*/
10740 				ret = DHD_FLAG_CONCURR_SINGLE_CHAN_MODE;
10741 				if (mchan_supported)
10742 					ret |= DHD_FLAG_CONCURR_MULTI_CHAN_MODE;
10743 				if (FW_SUPPORTED(dhd, rsdb)) {
10744 					ret |= DHD_FLAG_RSDB_MODE;
10745 				}
10746 #ifdef WL_SUPPORT_MULTIP2P
10747 				if (FW_SUPPORTED(dhd, mp2p)) {
10748 					ret |= DHD_FLAG_MP2P_MODE;
10749 				}
10750 #endif /* WL_SUPPORT_MULTIP2P */
10751 #if defined(WL_ENABLE_P2P_IF) || defined(WL_CFG80211_P2P_DEV_IF)
10752 				return ret;
10753 #else
10754 				return 0;
10755 #endif /* WL_ENABLE_P2P_IF || WL_CFG80211_P2P_DEV_IF */
10756 			}
10757 		}
10758 	}
10759 	return 0;
10760 }
10761 #endif
10762 
10763 #ifdef SUPPORT_AP_POWERSAVE
10764 #define RXCHAIN_PWRSAVE_PPS			10
10765 #define RXCHAIN_PWRSAVE_QUIET_TIME		10
10766 #define RXCHAIN_PWRSAVE_STAS_ASSOC_CHECK	0
dhd_set_ap_powersave(dhd_pub_t * dhdp,int ifidx,int enable)10767 int dhd_set_ap_powersave(dhd_pub_t *dhdp, int ifidx, int enable)
10768 {
10769 	int32 pps = RXCHAIN_PWRSAVE_PPS;
10770 	int32 quiet_time = RXCHAIN_PWRSAVE_QUIET_TIME;
10771 	int32 stas_assoc_check = RXCHAIN_PWRSAVE_STAS_ASSOC_CHECK;
10772 	int ret;
10773 
10774 	if (enable) {
10775 		ret = dhd_iovar(dhdp, 0, "rxchain_pwrsave_enable", (char *)&enable, sizeof(enable),
10776 				NULL, 0, TRUE);
10777 		if (ret != BCME_OK) {
10778 			DHD_ERROR(("Failed to enable AP power save\n"));
10779 		}
10780 		ret = dhd_iovar(dhdp, 0, "rxchain_pwrsave_pps", (char *)&pps, sizeof(pps), NULL, 0,
10781 				TRUE);
10782 		if (ret != BCME_OK) {
10783 			DHD_ERROR(("Failed to set pps\n"));
10784 		}
10785 		ret = dhd_iovar(dhdp, 0, "rxchain_pwrsave_quiet_time", (char *)&quiet_time,
10786 				sizeof(quiet_time), NULL, 0, TRUE);
10787 		if (ret != BCME_OK) {
10788 			DHD_ERROR(("Failed to set quiet time\n"));
10789 		}
10790 		ret = dhd_iovar(dhdp, 0, "rxchain_pwrsave_stas_assoc_check",
10791 				(char *)&stas_assoc_check, sizeof(stas_assoc_check), NULL, 0, TRUE);
10792 		if (ret != BCME_OK) {
10793 			DHD_ERROR(("Failed to set stas assoc check\n"));
10794 		}
10795 	} else {
10796 		ret = dhd_iovar(dhdp, 0, "rxchain_pwrsave_enable", (char *)&enable, sizeof(enable),
10797 				NULL, 0, TRUE);
10798 		if (ret != BCME_OK) {
10799 			DHD_ERROR(("Failed to disable AP power save\n"));
10800 		}
10801 	}
10802 
10803 	return 0;
10804 }
10805 #endif /* SUPPORT_AP_POWERSAVE */
10806 
10807 
10808 
10809 
10810 #if defined(WLADPS) || defined(WLADPS_PRIVATE_CMD)
10811 int
dhd_enable_adps(dhd_pub_t * dhd,uint8 on)10812 dhd_enable_adps(dhd_pub_t *dhd, uint8 on)
10813 {
10814 	int i;
10815 	int len;
10816 	int ret = BCME_OK;
10817 
10818 	bcm_iov_buf_t *iov_buf = NULL;
10819 	wl_adps_params_v1_t *data = NULL;
10820 	char buf[WL_EVENTING_MASK_LEN + 12];	/* Room for "event_msgs" + '\0' + bitvec  */
10821 
10822 	len = OFFSETOF(bcm_iov_buf_t, data) + sizeof(*data);
10823 	iov_buf = kmalloc(len, GFP_KERNEL);
10824 	if (iov_buf == NULL) {
10825 		DHD_ERROR(("%s - failed to allocate %d bytes for iov_buf\n", __FUNCTION__, len));
10826 		ret = BCME_NOMEM;
10827 		goto exit;
10828 	}
10829 
10830 	iov_buf->version = WL_ADPS_IOV_VER;
10831 	iov_buf->len = sizeof(*data);
10832 	iov_buf->id = WL_ADPS_IOV_MODE;
10833 
10834 	data = (wl_adps_params_v1_t *)iov_buf->data;
10835 	data->version = ADPS_SUB_IOV_VERSION_1;
10836 	data->length = sizeof(*data);
10837 	data->mode = on;
10838 
10839 	for (i = 1; i <= MAX_BANDS; i++) {
10840 		data->band = i;
10841 		bcm_mkiovar("adps", (char *)iov_buf, len, buf, sizeof(buf));
10842 		if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, sizeof(buf), TRUE, 0)) < 0) {
10843 			if (ret == BCME_UNSUPPORTED) {
10844 				DHD_ERROR(("%s adps is not supported\n", __FUNCTION__));
10845 				ret = BCME_OK;
10846 				goto exit;
10847 			}
10848 			else {
10849 				DHD_ERROR(("%s fail to set adps %s for band %d (%d)\n",
10850 					__FUNCTION__, on ? "On" : "Off", i, ret));
10851 				goto exit;
10852 			}
10853 		}
10854 	}
10855 
10856 exit:
10857 	if (iov_buf) {
10858 		kfree(iov_buf);
10859 	}
10860 	return ret;
10861 }
10862 #endif /* WLADPS || WLADPS_PRIVATE_CMD */
10863 
10864 int
dhd_preinit_ioctls(dhd_pub_t * dhd)10865 dhd_preinit_ioctls(dhd_pub_t *dhd)
10866 {
10867 	int ret = 0;
10868 	char eventmask[WL_EVENTING_MASK_LEN];
10869 	char iovbuf[WL_EVENTING_MASK_LEN + 12];	/*  Room for "event_msgs" + '\0' + bitvec  */
10870 	uint32 buf_key_b4_m4 = 1;
10871 	uint8 msglen;
10872 	eventmsgs_ext_t *eventmask_msg = NULL;
10873 	char* iov_buf = NULL;
10874 	int ret2 = 0;
10875 	uint32 wnm_cap = 0;
10876 #if defined(CUSTOM_AMPDU_BA_WSIZE)
10877 	uint32 ampdu_ba_wsize = 0;
10878 #endif
10879 #if defined(CUSTOM_AMPDU_MPDU)
10880 	int32 ampdu_mpdu = 0;
10881 #endif
10882 #if defined(CUSTOM_AMPDU_RELEASE)
10883 	int32 ampdu_release = 0;
10884 #endif
10885 #if defined(CUSTOM_AMSDU_AGGSF)
10886 	int32 amsdu_aggsf = 0;
10887 #endif
10888 	shub_control_t shub_ctl;
10889 
10890 #if defined(BCMSDIO) || defined(BCMDBUS)
10891 #ifdef PROP_TXSTATUS
10892 	int wlfc_enable = TRUE;
10893 #ifndef DISABLE_11N
10894 	uint32 hostreorder = 1;
10895 	uint wl_down = 1;
10896 #endif /* DISABLE_11N */
10897 #endif /* PROP_TXSTATUS */
10898 #endif /* BCMSDIO || BCMDBUS */
10899 #ifndef PCIE_FULL_DONGLE
10900 	uint32 wl_ap_isolate;
10901 #endif /* PCIE_FULL_DONGLE */
10902 	uint32 frameburst = CUSTOM_FRAMEBURST_SET;
10903 	uint wnm_bsstrans_resp = 0;
10904 #ifdef SUPPORT_SET_CAC
10905 	uint32 cac = 1;
10906 #endif /* SUPPORT_SET_CAC */
10907 #ifdef DHD_ENABLE_LPC
10908 	uint32 lpc = 1;
10909 #endif /* DHD_ENABLE_LPC */
10910 	uint power_mode = PM_FAST;
10911 #if defined(BCMSDIO)
10912 	uint32 dongle_align = DHD_SDALIGN;
10913 	uint32 glom = CUSTOM_GLOM_SETTING;
10914 #endif /* defined(BCMSDIO) */
10915 #if defined(CUSTOMER_HW2) && defined(USE_WL_CREDALL)
10916 	uint32 credall = 1;
10917 #endif
10918 	uint bcn_timeout = CUSTOM_BCN_TIMEOUT;
10919 	uint scancache_enab = TRUE;
10920 #ifdef ENABLE_BCN_LI_BCN_WAKEUP
10921 	uint32 bcn_li_bcn = 1;
10922 #endif /* ENABLE_BCN_LI_BCN_WAKEUP */
10923 	uint retry_max = CUSTOM_ASSOC_RETRY_MAX;
10924 #if defined(ARP_OFFLOAD_SUPPORT)
10925 	int arpoe = 1;
10926 #endif
10927 	int scan_assoc_time = DHD_SCAN_ASSOC_ACTIVE_TIME;
10928 	int scan_unassoc_time = DHD_SCAN_UNASSOC_ACTIVE_TIME;
10929 	int scan_passive_time = DHD_SCAN_PASSIVE_TIME;
10930 	char buf[WLC_IOCTL_SMLEN];
10931 	char *ptr;
10932 	uint32 listen_interval = CUSTOM_LISTEN_INTERVAL; /* Default Listen Interval in Beacons */
10933 #if defined(DHD_8021X_DUMP) && defined(SHOW_LOGTRACE)
10934 	wl_el_tag_params_t *el_tag = NULL;
10935 #endif /* DHD_8021X_DUMP */
10936 #ifdef ROAM_ENABLE
10937 	uint roamvar = 0;
10938 	int roam_trigger[2] = {CUSTOM_ROAM_TRIGGER_SETTING, WLC_BAND_ALL};
10939 	int roam_scan_period[2] = {10, WLC_BAND_ALL};
10940 	int roam_delta[2] = {CUSTOM_ROAM_DELTA_SETTING, WLC_BAND_ALL};
10941 #ifdef FULL_ROAMING_SCAN_PERIOD_60_SEC
10942 	int roam_fullscan_period = 60;
10943 #else /* FULL_ROAMING_SCAN_PERIOD_60_SEC */
10944 	int roam_fullscan_period = 120;
10945 #endif /* FULL_ROAMING_SCAN_PERIOD_60_SEC */
10946 #ifdef DISABLE_BCNLOSS_ROAM
10947 	uint roam_bcnloss_off = 1;
10948 #endif /* DISABLE_BCNLOSS_ROAM */
10949 #else
10950 #ifdef DISABLE_BUILTIN_ROAM
10951 	uint roamvar = 1;
10952 #endif /* DISABLE_BUILTIN_ROAM */
10953 #endif /* ROAM_ENABLE */
10954 
10955 #if defined(SOFTAP)
10956 	uint dtim = 1;
10957 #endif
10958 #if (defined(AP) && !defined(WLP2P)) || (!defined(AP) && defined(WL_CFG80211))
10959 	struct ether_addr p2p_ea;
10960 #endif
10961 #ifdef SOFTAP_UAPSD_OFF
10962 	uint32 wme_apsd = 0;
10963 #endif /* SOFTAP_UAPSD_OFF */
10964 #if (defined(AP) || defined(WLP2P)) && !defined(SOFTAP_AND_GC)
10965 	uint32 apsta = 1; /* Enable APSTA mode */
10966 #elif defined(SOFTAP_AND_GC)
10967 	uint32 apsta = 0;
10968 	int ap_mode = 1;
10969 #endif /* (defined(AP) || defined(WLP2P)) && !defined(SOFTAP_AND_GC) */
10970 #ifdef GET_CUSTOM_MAC_ENABLE
10971 	struct ether_addr ea_addr;
10972 	char hw_ether[62];
10973 #endif /* GET_CUSTOM_MAC_ENABLE */
10974 
10975 #ifdef DISABLE_11N
10976 	uint32 nmode = 0;
10977 #endif /* DISABLE_11N */
10978 
10979 #ifdef USE_WL_TXBF
10980 	uint32 txbf = 1;
10981 #endif /* USE_WL_TXBF */
10982 #ifdef DISABLE_TXBFR
10983 	uint32 txbf_bfr_cap = 0;
10984 #endif /* DISABLE_TXBFR */
10985 #if defined(PROP_TXSTATUS)
10986 #ifdef USE_WFA_CERT_CONF
10987 	uint32 proptx = 0;
10988 #endif /* USE_WFA_CERT_CONF */
10989 #endif /* PROP_TXSTATUS */
10990 #if defined(SUPPORT_5G_1024QAM_VHT)
10991 	uint32 vht_features = 0; /* init to 0, will be set based on each support */
10992 #endif
10993 #ifdef DISABLE_11N_PROPRIETARY_RATES
10994 	uint32 ht_features = 0;
10995 #endif /* DISABLE_11N_PROPRIETARY_RATES */
10996 #ifdef CUSTOM_PSPRETEND_THR
10997 	uint32 pspretend_thr = CUSTOM_PSPRETEND_THR;
10998 #endif
10999 #ifdef CUSTOM_EVENT_PM_WAKE
11000 	uint32 pm_awake_thresh = CUSTOM_EVENT_PM_WAKE;
11001 #endif	/* CUSTOM_EVENT_PM_WAKE */
11002 	uint32 rsdb_mode = 0;
11003 #ifdef ENABLE_TEMP_THROTTLING
11004 	wl_temp_control_t temp_control;
11005 #endif /* ENABLE_TEMP_THROTTLING */
11006 #ifdef DISABLE_PRUNED_SCAN
11007 	uint32 scan_features = 0;
11008 #endif /* DISABLE_PRUNED_SCAN */
11009 #ifdef PKT_FILTER_SUPPORT
11010 	dhd_pkt_filter_enable = TRUE;
11011 #ifdef APF
11012 	dhd->apf_set = FALSE;
11013 #endif /* APF */
11014 #endif /* PKT_FILTER_SUPPORT */
11015 #ifdef WLTDLS
11016 	dhd->tdls_enable = FALSE;
11017 	dhd_tdls_set_mode(dhd, false);
11018 #endif /* WLTDLS */
11019 	dhd->suspend_bcn_li_dtim = CUSTOM_SUSPEND_BCN_LI_DTIM;
11020 #ifdef ENABLE_MAX_DTIM_IN_SUSPEND
11021 	dhd->max_dtim_enable = TRUE;
11022 #else
11023 	dhd->max_dtim_enable = FALSE;
11024 #endif /* ENABLE_MAX_DTIM_IN_SUSPEND */
11025 #ifdef CUSTOM_SET_OCLOFF
11026 	dhd->ocl_off = FALSE;
11027 #endif /* CUSTOM_SET_OCLOFF */
11028 	DHD_TRACE(("Enter %s\n", __FUNCTION__));
11029 
11030 #ifdef DHDTCPACK_SUPPRESS
11031 	dhd_tcpack_suppress_set(dhd, dhd->conf->tcpack_sup_mode);
11032 #endif
11033 	dhd->op_mode = 0;
11034 
11035 #if defined(CUSTOM_COUNTRY_CODE) && defined(CUSTOMER_HW2)
11036 	/* clear AP flags */
11037 	dhd->dhd_cflags &= ~WLAN_PLAT_AP_FLAG;
11038 #endif /* CUSTOM_COUNTRY_CODE && CUSTOMER_HW2 */
11039 
11040 #ifdef CUSTOMER_HW4_DEBUG
11041 	if (!dhd_validate_chipid(dhd)) {
11042 		DHD_ERROR(("%s: CONFIG_BCMXXX and CHIP ID(%x) is mismatched\n",
11043 			__FUNCTION__, dhd_bus_chip_id(dhd)));
11044 #ifndef SUPPORT_MULTIPLE_CHIPS
11045 		ret = BCME_BADARG;
11046 		goto done;
11047 #endif /* !SUPPORT_MULTIPLE_CHIPS */
11048 	}
11049 #endif /* CUSTOMER_HW4_DEBUG */
11050 	if ((!op_mode && dhd_get_fw_mode(dhd->info) == DHD_FLAG_MFG_MODE) ||
11051 		(op_mode == DHD_FLAG_MFG_MODE)) {
11052 		dhd->op_mode = DHD_FLAG_MFG_MODE;
11053 #ifdef DHD_PCIE_RUNTIMEPM
11054 		/* Disable RuntimePM in mfg mode */
11055 		DHD_DISABLE_RUNTIME_PM(dhd);
11056 		DHD_ERROR(("%s : Disable RuntimePM in Manufactring Firmware\n", __FUNCTION__));
11057 #endif /* DHD_PCIE_RUNTIME_PM */
11058 		/* Check and adjust IOCTL response timeout for Manufactring firmware */
11059 		dhd_os_set_ioctl_resp_timeout(MFG_IOCTL_RESP_TIMEOUT);
11060 		DHD_ERROR(("%s : Set IOCTL response time for Manufactring Firmware\n",
11061 			__FUNCTION__));
11062 	} else {
11063 		dhd_os_set_ioctl_resp_timeout(IOCTL_RESP_TIMEOUT);
11064 		DHD_INFO(("%s : Set IOCTL response time.\n", __FUNCTION__));
11065 	}
11066 #ifdef GET_CUSTOM_MAC_ENABLE
11067 	memset(hw_ether, 0, sizeof(hw_ether));
11068 	ret = wifi_platform_get_mac_addr(dhd->info->adapter, hw_ether);
11069 #ifdef GET_CUSTOM_MAC_FROM_CONFIG
11070 	if (!memcmp(&ether_null, &dhd->conf->hw_ether, ETHER_ADDR_LEN)) {
11071 		ret = 0;
11072 	} else
11073 #endif
11074 	if (!ret) {
11075 		memset(buf, 0, sizeof(buf));
11076 #ifdef GET_CUSTOM_MAC_FROM_CONFIG
11077 		memcpy(hw_ether, &dhd->conf->hw_ether, sizeof(dhd->conf->hw_ether));
11078 #endif
11079 		bcopy(hw_ether, ea_addr.octet, sizeof(struct ether_addr));
11080 		bcm_mkiovar("cur_etheraddr", (void *)&ea_addr, ETHER_ADDR_LEN, buf, sizeof(buf));
11081 		ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, sizeof(buf), TRUE, 0);
11082 		if (ret < 0) {
11083 			memset(buf, 0, sizeof(buf));
11084 			bcm_mkiovar("hw_ether", hw_ether, sizeof(hw_ether), buf, sizeof(buf));
11085 			ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, buf, sizeof(buf), TRUE, 0);
11086 			if (ret) {
11087 				DHD_ERROR(("%s: can't set MAC address MAC="MACDBG", error=%d\n",
11088 					__FUNCTION__, MAC2STRDBG(hw_ether), ret));
11089 				prhex("MACPAD", &hw_ether[ETHER_ADDR_LEN], sizeof(hw_ether)-ETHER_ADDR_LEN);
11090 				ret = BCME_NOTUP;
11091 				goto done;
11092 			}
11093 		}
11094 	} else {
11095 		DHD_ERROR(("%s: can't get custom MAC address, ret=%d\n", __FUNCTION__, ret));
11096 		ret = BCME_NOTUP;
11097 		goto done;
11098 	}
11099 #endif /* GET_CUSTOM_MAC_ENABLE */
11100 	/* Get the default device MAC address directly from firmware */
11101 	memset(buf, 0, sizeof(buf));
11102 	bcm_mkiovar("cur_etheraddr", 0, 0, buf, sizeof(buf));
11103 	if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_VAR, buf, sizeof(buf),
11104 		FALSE, 0)) < 0) {
11105 		DHD_ERROR(("%s: can't get MAC address , error=%d\n", __FUNCTION__, ret));
11106 		ret = BCME_NOTUP;
11107 		goto done;
11108 	}
11109 	/* Update public MAC address after reading from Firmware */
11110 	memcpy(dhd->mac.octet, buf, ETHER_ADDR_LEN);
11111 
11112 	if ((ret = dhd_apply_default_clm(dhd, dhd->clm_path)) < 0) {
11113 		DHD_ERROR(("%s: CLM set failed. Abort initialization.\n", __FUNCTION__));
11114 		goto done;
11115 	}
11116 
11117 	/* get a capabilities from firmware */
11118 	{
11119 		uint32 cap_buf_size = sizeof(dhd->fw_capabilities);
11120 		memset(dhd->fw_capabilities, 0, cap_buf_size);
11121 		ret = dhd_iovar(dhd, 0, "cap", NULL, 0, dhd->fw_capabilities, (cap_buf_size - 1),
11122 				FALSE);
11123 		if (ret < 0) {
11124 			DHD_ERROR(("%s: Get Capability failed (error=%d)\n",
11125 				__FUNCTION__, ret));
11126 			return 0;
11127 		}
11128 
11129 		memmove(&dhd->fw_capabilities[1], dhd->fw_capabilities, (cap_buf_size - 1));
11130 		dhd->fw_capabilities[0] = ' ';
11131 		dhd->fw_capabilities[cap_buf_size - 2] = ' ';
11132 		dhd->fw_capabilities[cap_buf_size - 1] = '\0';
11133 	}
11134 
11135 	if ((!op_mode && dhd_get_fw_mode(dhd->info) == DHD_FLAG_HOSTAP_MODE) ||
11136 		(op_mode == DHD_FLAG_HOSTAP_MODE)) {
11137 #ifdef SET_RANDOM_MAC_SOFTAP
11138 		uint rand_mac;
11139 #endif /* SET_RANDOM_MAC_SOFTAP */
11140 		dhd->op_mode = DHD_FLAG_HOSTAP_MODE;
11141 #if defined(ARP_OFFLOAD_SUPPORT)
11142 			arpoe = 0;
11143 #endif
11144 #ifdef PKT_FILTER_SUPPORT
11145 		if (dhd_conf_get_insuspend(dhd, AP_FILTER_IN_SUSPEND))
11146 			dhd_pkt_filter_enable = TRUE;
11147 		else
11148 			dhd_pkt_filter_enable = FALSE;
11149 #endif
11150 #ifdef SET_RANDOM_MAC_SOFTAP
11151 		SRANDOM32((uint)jiffies);
11152 		rand_mac = RANDOM32();
11153 		iovbuf[0] = (unsigned char)(vendor_oui >> 16) | 0x02;	/* local admin bit */
11154 		iovbuf[1] = (unsigned char)(vendor_oui >> 8);
11155 		iovbuf[2] = (unsigned char)vendor_oui;
11156 		iovbuf[3] = (unsigned char)(rand_mac & 0x0F) | 0xF0;
11157 		iovbuf[4] = (unsigned char)(rand_mac >> 8);
11158 		iovbuf[5] = (unsigned char)(rand_mac >> 16);
11159 
11160 		ret = dhd_iovar(dhd, 0, "cur_etheraddr", (char *)&iovbuf, ETHER_ADDR_LEN, NULL, 0,
11161 				TRUE);
11162 		if (ret < 0) {
11163 			DHD_ERROR(("%s: can't set MAC address , error=%d\n", __FUNCTION__, ret));
11164 		} else
11165 			memcpy(dhd->mac.octet, iovbuf, ETHER_ADDR_LEN);
11166 #endif /* SET_RANDOM_MAC_SOFTAP */
11167 #ifdef USE_DYNAMIC_F2_BLKSIZE
11168 		dhdsdio_func_blocksize(dhd, 2, DYNAMIC_F2_BLKSIZE_FOR_NONLEGACY);
11169 #endif /* USE_DYNAMIC_F2_BLKSIZE */
11170 #ifdef SUPPORT_AP_POWERSAVE
11171 		dhd_set_ap_powersave(dhd, 0, TRUE);
11172 #endif /* SUPPORT_AP_POWERSAVE */
11173 #ifdef SOFTAP_UAPSD_OFF
11174 		ret = dhd_iovar(dhd, 0, "wme_apsd", (char *)&wme_apsd, sizeof(wme_apsd), NULL, 0,
11175 				TRUE);
11176 		if (ret < 0) {
11177 			DHD_ERROR(("%s: set wme_apsd 0 fail (error=%d)\n",
11178 				__FUNCTION__, ret));
11179 		}
11180 #endif /* SOFTAP_UAPSD_OFF */
11181 #if defined(CUSTOM_COUNTRY_CODE) && defined(CUSTOMER_HW2)
11182 		/* set AP flag for specific country code of SOFTAP */
11183 		dhd->dhd_cflags |= WLAN_PLAT_AP_FLAG | WLAN_PLAT_NODFS_FLAG;
11184 #endif /* CUSTOM_COUNTRY_CODE && CUSTOMER_HW2 */
11185 	} else if ((!op_mode && dhd_get_fw_mode(dhd->info) == DHD_FLAG_MFG_MODE) ||
11186 		(op_mode == DHD_FLAG_MFG_MODE)) {
11187 #if defined(ARP_OFFLOAD_SUPPORT)
11188 		arpoe = 0;
11189 #endif /* ARP_OFFLOAD_SUPPORT */
11190 #ifdef PKT_FILTER_SUPPORT
11191 		dhd_pkt_filter_enable = FALSE;
11192 #endif /* PKT_FILTER_SUPPORT */
11193 		dhd->op_mode = DHD_FLAG_MFG_MODE;
11194 #ifdef USE_DYNAMIC_F2_BLKSIZE
11195 		dhdsdio_func_blocksize(dhd, 2, DYNAMIC_F2_BLKSIZE_FOR_NONLEGACY);
11196 #endif /* USE_DYNAMIC_F2_BLKSIZE */
11197 		if (FW_SUPPORTED(dhd, rsdb)) {
11198 			rsdb_mode = 0;
11199 			ret = dhd_iovar(dhd, 0, "rsdb_mode", (char *)&rsdb_mode, sizeof(rsdb_mode),
11200 					NULL, 0, TRUE);
11201 			if (ret < 0) {
11202 				DHD_ERROR(("%s Disable rsdb_mode is failed ret= %d\n",
11203 					__FUNCTION__, ret));
11204 			}
11205 		}
11206 	} else {
11207 		uint32 concurrent_mode = 0;
11208 		if ((!op_mode && dhd_get_fw_mode(dhd->info) == DHD_FLAG_P2P_MODE) ||
11209 			(op_mode == DHD_FLAG_P2P_MODE)) {
11210 #if defined(ARP_OFFLOAD_SUPPORT)
11211 			arpoe = 0;
11212 #endif
11213 #ifdef PKT_FILTER_SUPPORT
11214 			dhd_pkt_filter_enable = FALSE;
11215 #endif
11216 			dhd->op_mode = DHD_FLAG_P2P_MODE;
11217 		} else if ((!op_mode && dhd_get_fw_mode(dhd->info) == DHD_FLAG_IBSS_MODE) ||
11218 			(op_mode == DHD_FLAG_IBSS_MODE)) {
11219 			dhd->op_mode = DHD_FLAG_IBSS_MODE;
11220 		} else
11221 			dhd->op_mode = DHD_FLAG_STA_MODE;
11222 #if !defined(AP) && defined(WLP2P)
11223 		if (dhd->op_mode != DHD_FLAG_IBSS_MODE &&
11224 			(concurrent_mode = dhd_get_concurrent_capabilites(dhd))) {
11225 #if defined(ARP_OFFLOAD_SUPPORT)
11226 			arpoe = 1;
11227 #endif
11228 			dhd->op_mode |= concurrent_mode;
11229 		}
11230 
11231 		/* Check if we are enabling p2p */
11232 		if (dhd->op_mode & DHD_FLAG_P2P_MODE) {
11233 			ret = dhd_iovar(dhd, 0, "apsta", (char *)&apsta, sizeof(apsta), NULL, 0,
11234 					TRUE);
11235 			if (ret < 0)
11236 				DHD_ERROR(("%s APSTA for P2P failed ret= %d\n", __FUNCTION__, ret));
11237 
11238 #if defined(SOFTAP_AND_GC)
11239 		if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_AP,
11240 			(char *)&ap_mode, sizeof(ap_mode), TRUE, 0)) < 0) {
11241 				DHD_ERROR(("%s WLC_SET_AP failed %d\n", __FUNCTION__, ret));
11242 		}
11243 #endif
11244 			memcpy(&p2p_ea, &dhd->mac, ETHER_ADDR_LEN);
11245 			ETHER_SET_LOCALADDR(&p2p_ea);
11246 			ret = dhd_iovar(dhd, 0, "p2p_da_override", (char *)&p2p_ea, sizeof(p2p_ea),
11247 					NULL, 0, TRUE);
11248 			if (ret < 0)
11249 				DHD_ERROR(("%s p2p_da_override ret= %d\n", __FUNCTION__, ret));
11250 			else
11251 				DHD_INFO(("dhd_preinit_ioctls: p2p_da_override succeeded\n"));
11252 		}
11253 #else
11254 	(void)concurrent_mode;
11255 #endif
11256 	}
11257 
11258 #if defined(RSDB_MODE_FROM_FILE)
11259 	(void)dhd_rsdb_mode_from_file(dhd);
11260 #endif
11261 
11262 #ifdef DISABLE_PRUNED_SCAN
11263 	if (FW_SUPPORTED(dhd, rsdb)) {
11264 		ret = dhd_iovar(dhd, 0, "scan_features", (char *)&scan_features,
11265 				sizeof(scan_features), iovbuf, sizeof(iovbuf), FALSE);
11266 		if (ret < 0) {
11267 			DHD_ERROR(("%s get scan_features is failed ret=%d\n",
11268 				__FUNCTION__, ret));
11269 		} else {
11270 			memcpy(&scan_features, iovbuf, 4);
11271 			scan_features &= ~RSDB_SCAN_DOWNGRADED_CH_PRUNE_ROAM;
11272 			ret = dhd_iovar(dhd, 0, "scan_features", (char *)&scan_features,
11273 					sizeof(scan_features), NULL, 0, TRUE);
11274 			if (ret < 0) {
11275 				DHD_ERROR(("%s set scan_features is failed ret=%d\n",
11276 					__FUNCTION__, ret));
11277 			}
11278 		}
11279 	}
11280 #endif /* DISABLE_PRUNED_SCAN */
11281 
11282 	DHD_ERROR(("Firmware up: op_mode=0x%04x, MAC="MACDBG"\n",
11283 		dhd->op_mode, MAC2STRDBG(dhd->mac.octet)));
11284 #ifdef CUSTOMER_HW2
11285 #if defined(DHD_BLOB_EXISTENCE_CHECK)
11286 	if (!dhd->pub.is_blob)
11287 #endif /* DHD_BLOB_EXISTENCE_CHECK */
11288 	{
11289 		/* get a ccode and revision for the country code */
11290 #if defined(CUSTOM_COUNTRY_CODE)
11291 		get_customized_country_code(dhd->info->adapter, dhd->dhd_cspec.country_abbrev,
11292 			&dhd->dhd_cspec, dhd->dhd_cflags);
11293 #else
11294 		get_customized_country_code(dhd->info->adapter, dhd->dhd_cspec.country_abbrev,
11295 			&dhd->dhd_cspec);
11296 #endif /* CUSTOM_COUNTRY_CODE */
11297 	}
11298 #endif /* CUSTOMER_HW2 */
11299 
11300 #if defined(RXFRAME_THREAD) && defined(RXTHREAD_ONLYSTA)
11301 	if (dhd->op_mode == DHD_FLAG_HOSTAP_MODE)
11302 		dhd->info->rxthread_enabled = FALSE;
11303 	else
11304 		dhd->info->rxthread_enabled = TRUE;
11305 #endif
11306 	/* Set Country code  */
11307 	if (dhd->dhd_cspec.ccode[0] != 0) {
11308 		ret = dhd_iovar(dhd, 0, "country", (char *)&dhd->dhd_cspec, sizeof(wl_country_t),
11309 				NULL, 0, TRUE);
11310 		if (ret < 0)
11311 			DHD_ERROR(("%s: country code setting failed\n", __FUNCTION__));
11312 	}
11313 
11314 
11315 	/* Set Listen Interval */
11316 	ret = dhd_iovar(dhd, 0, "assoc_listen", (char *)&listen_interval, sizeof(listen_interval),
11317 			NULL, 0, TRUE);
11318 	if (ret < 0)
11319 		DHD_ERROR(("%s assoc_listen failed %d\n", __FUNCTION__, ret));
11320 
11321 #if defined(ROAM_ENABLE) || defined(DISABLE_BUILTIN_ROAM)
11322 #ifdef USE_WFA_CERT_CONF
11323 	if (sec_get_param_wfa_cert(dhd, SET_PARAM_ROAMOFF, &roamvar) == BCME_OK) {
11324 		DHD_ERROR(("%s: read roam_off param =%d\n", __FUNCTION__, roamvar));
11325 	}
11326 #endif /* USE_WFA_CERT_CONF */
11327 	/* Disable built-in roaming to allowed ext supplicant to take care of roaming */
11328 	dhd_iovar(dhd, 0, "roam_off", (char *)&roamvar, sizeof(roamvar), NULL, 0, TRUE);
11329 #endif /* ROAM_ENABLE || DISABLE_BUILTIN_ROAM */
11330 #if defined(ROAM_ENABLE)
11331 #ifdef DISABLE_BCNLOSS_ROAM
11332 	dhd_iovar(dhd, 0, "roam_bcnloss_off", (char *)&roam_bcnloss_off, sizeof(roam_bcnloss_off),
11333 			NULL, 0, TRUE);
11334 #endif /* DISABLE_BCNLOSS_ROAM */
11335 	if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_ROAM_TRIGGER, roam_trigger,
11336 		sizeof(roam_trigger), TRUE, 0)) < 0)
11337 		DHD_ERROR(("%s: roam trigger set failed %d\n", __FUNCTION__, ret));
11338 	if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_ROAM_SCAN_PERIOD, roam_scan_period,
11339 		sizeof(roam_scan_period), TRUE, 0)) < 0)
11340 		DHD_ERROR(("%s: roam scan period set failed %d\n", __FUNCTION__, ret));
11341 	if ((dhd_wl_ioctl_cmd(dhd, WLC_SET_ROAM_DELTA, roam_delta,
11342 		sizeof(roam_delta), TRUE, 0)) < 0)
11343 		DHD_ERROR(("%s: roam delta set failed %d\n", __FUNCTION__, ret));
11344 	ret = dhd_iovar(dhd, 0, "fullroamperiod", (char *)&roam_fullscan_period,
11345 			sizeof(roam_fullscan_period), NULL, 0, TRUE);
11346 	if (ret < 0)
11347 		DHD_ERROR(("%s: roam fullscan period set failed %d\n", __FUNCTION__, ret));
11348 #endif /* ROAM_ENABLE */
11349 
11350 #ifdef CUSTOM_EVENT_PM_WAKE
11351 	ret = dhd_iovar(dhd, 0, "const_awake_thresh", (char *)&pm_awake_thresh,
11352 			sizeof(pm_awake_thresh), NULL, 0, TRUE);
11353 	if (ret < 0) {
11354 		DHD_ERROR(("%s set const_awake_thresh failed %d\n", __FUNCTION__, ret));
11355 	}
11356 #endif	/* CUSTOM_EVENT_PM_WAKE */
11357 #ifdef WLTDLS
11358 #ifdef ENABLE_TDLS_AUTO_MODE
11359 	/* by default TDLS on and auto mode on */
11360 	_dhd_tdls_enable(dhd, true, true, NULL);
11361 #else
11362 	/* by default TDLS on and auto mode off */
11363 	_dhd_tdls_enable(dhd, true, false, NULL);
11364 #endif /* ENABLE_TDLS_AUTO_MODE */
11365 #endif /* WLTDLS */
11366 
11367 #ifdef DHD_ENABLE_LPC
11368 	/* Set lpc 1 */
11369 	ret = dhd_iovar(dhd, 0, "lpc", (char *)&lpc, sizeof(lpc), NULL, 0, TRUE);
11370 	if (ret < 0) {
11371 		DHD_ERROR(("%s Set lpc failed  %d\n", __FUNCTION__, ret));
11372 
11373 		if (ret == BCME_NOTDOWN) {
11374 			uint wl_down = 1;
11375 			ret = dhd_wl_ioctl_cmd(dhd, WLC_DOWN,
11376 				(char *)&wl_down, sizeof(wl_down), TRUE, 0);
11377 			DHD_ERROR(("%s lpc fail WL_DOWN : %d, lpc = %d\n", __FUNCTION__, ret, lpc));
11378 
11379 			ret = dhd_iovar(dhd, 0, "lpc", (char *)&lpc, sizeof(lpc), NULL, 0, TRUE);
11380 			DHD_ERROR(("%s Set lpc ret --> %d\n", __FUNCTION__, ret));
11381 		}
11382 	}
11383 #endif /* DHD_ENABLE_LPC */
11384 
11385 #ifdef WLADPS
11386 #ifdef WLADPS_SEAK_AP_WAR
11387 	dhd->disabled_adps = FALSE;
11388 #endif /* WLADPS_SEAK_AP_WAR */
11389 	if (dhd->op_mode & DHD_FLAG_STA_MODE) {
11390 #ifdef ADPS_MODE_FROM_FILE
11391 		dhd_adps_mode_from_file(dhd);
11392 #else
11393 		if ((ret = dhd_enable_adps(dhd, ADPS_ENABLE)) != BCME_OK) {
11394 			DHD_ERROR(("%s dhd_enable_adps failed %d\n",
11395 					__FUNCTION__, ret));
11396 		}
11397 #endif /* ADPS_MODE_FROM_FILE */
11398 	}
11399 #endif /* WLADPS */
11400 
11401 	/* Set PowerSave mode */
11402 	(void) dhd_wl_ioctl_cmd(dhd, WLC_SET_PM, (char *)&power_mode, sizeof(power_mode), TRUE, 0);
11403 
11404 #if defined(BCMSDIO)
11405 	/* Match Host and Dongle rx alignment */
11406 	dhd_iovar(dhd, 0, "bus:txglomalign", (char *)&dongle_align, sizeof(dongle_align),
11407 			NULL, 0, TRUE);
11408 
11409 #if defined(CUSTOMER_HW2) && defined(USE_WL_CREDALL)
11410 	/* enable credall to reduce the chance of no bus credit happened. */
11411 	dhd_iovar(dhd, 0, "bus:credall", (char *)&credall, sizeof(credall), NULL, 0, TRUE);
11412 #endif
11413 
11414 #ifdef USE_WFA_CERT_CONF
11415 	if (sec_get_param_wfa_cert(dhd, SET_PARAM_BUS_TXGLOM_MODE, &glom) == BCME_OK) {
11416 		DHD_ERROR(("%s, read txglom param =%d\n", __FUNCTION__, glom));
11417 	}
11418 #endif /* USE_WFA_CERT_CONF */
11419 	if (glom != DEFAULT_GLOM_VALUE) {
11420 		DHD_INFO(("%s set glom=0x%X\n", __FUNCTION__, glom));
11421 		dhd_iovar(dhd, 0, "bus:txglom", (char *)&glom, sizeof(glom), NULL, 0, TRUE);
11422 	}
11423 #endif /* defined(BCMSDIO) */
11424 
11425 	/* Setup timeout if Beacons are lost and roam is off to report link down */
11426 	dhd_iovar(dhd, 0, "bcn_timeout", (char *)&bcn_timeout, sizeof(bcn_timeout), NULL, 0, TRUE);
11427 
11428 	/* Setup assoc_retry_max count to reconnect target AP in dongle */
11429 	dhd_iovar(dhd, 0, "assoc_retry_max", (char *)&retry_max, sizeof(retry_max), NULL, 0, TRUE);
11430 
11431 #if defined(AP) && !defined(WLP2P)
11432 	dhd_iovar(dhd, 0, "apsta", (char *)&apsta, sizeof(apsta), NULL, 0, TRUE);
11433 
11434 #endif /* defined(AP) && !defined(WLP2P) */
11435 
11436 #ifdef MIMO_ANT_SETTING
11437 	dhd_sel_ant_from_file(dhd);
11438 #endif /* MIMO_ANT_SETTING */
11439 
11440 #if defined(SOFTAP)
11441 	if (ap_fw_loaded == TRUE) {
11442 		dhd_wl_ioctl_cmd(dhd, WLC_SET_DTIMPRD, (char *)&dtim, sizeof(dtim), TRUE, 0);
11443 	}
11444 #endif
11445 
11446 #if defined(KEEP_ALIVE)
11447 	{
11448 	/* Set Keep Alive : be sure to use FW with -keepalive */
11449 	int res;
11450 
11451 #if defined(SOFTAP)
11452 	if (ap_fw_loaded == FALSE)
11453 #endif
11454 		if (!(dhd->op_mode &
11455 			(DHD_FLAG_HOSTAP_MODE | DHD_FLAG_MFG_MODE))) {
11456 			if ((res = dhd_keep_alive_onoff(dhd)) < 0)
11457 				DHD_ERROR(("%s set keeplive failed %d\n",
11458 				__FUNCTION__, res));
11459 		}
11460 	}
11461 #endif /* defined(KEEP_ALIVE) */
11462 
11463 #ifdef USE_WL_TXBF
11464 	ret = dhd_iovar(dhd, 0, "txbf", (char *)&txbf, sizeof(txbf), NULL, 0, TRUE);
11465 	if (ret < 0)
11466 		DHD_ERROR(("%s Set txbf failed  %d\n", __FUNCTION__, ret));
11467 
11468 #endif /* USE_WL_TXBF */
11469 
11470 	ret = dhd_iovar(dhd, 0, "scancache", (char *)&scancache_enab, sizeof(scancache_enab), NULL,
11471 			0, TRUE);
11472 	if (ret < 0) {
11473 		DHD_ERROR(("%s Set scancache failed %d\n", __FUNCTION__, ret));
11474 	}
11475 
11476 #ifdef DISABLE_TXBFR
11477 	ret = dhd_iovar(dhd, 0, "txbf_bfr_cap", (char *)&txbf_bfr_cap, sizeof(txbf_bfr_cap), NULL,
11478 			0, TRUE);
11479 	if (ret < 0) {
11480 		DHD_ERROR(("%s Clear txbf_bfr_cap failed  %d\n", __FUNCTION__, ret));
11481 	}
11482 #endif /* DISABLE_TXBFR */
11483 
11484 #ifdef USE_WFA_CERT_CONF
11485 #ifdef USE_WL_FRAMEBURST
11486 	 if (sec_get_param_wfa_cert(dhd, SET_PARAM_FRAMEBURST, &frameburst) == BCME_OK) {
11487 		DHD_ERROR(("%s, read frameburst param=%d\n", __FUNCTION__, frameburst));
11488 	 }
11489 #endif /* USE_WL_FRAMEBURST */
11490 #ifdef DISABLE_FRAMEBURST_VSDB
11491 	 g_frameburst = frameburst;
11492 #endif /* DISABLE_FRAMEBURST_VSDB */
11493 #endif /* USE_WFA_CERT_CONF */
11494 #ifdef DISABLE_WL_FRAMEBURST_SOFTAP
11495 	/* Disable Framebursting for SofAP */
11496 	if (dhd->op_mode & DHD_FLAG_HOSTAP_MODE) {
11497 		frameburst = 0;
11498 	}
11499 #endif /* DISABLE_WL_FRAMEBURST_SOFTAP */
11500 	/* Set frameburst to value */
11501 	if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_FAKEFRAG, (char *)&frameburst,
11502 		sizeof(frameburst), TRUE, 0)) < 0) {
11503 		DHD_INFO(("%s frameburst not supported  %d\n", __FUNCTION__, ret));
11504 	}
11505 
11506 	iov_buf = (char*)kmalloc(WLC_IOCTL_SMLEN, GFP_KERNEL);
11507 	if (iov_buf == NULL) {
11508 		DHD_ERROR(("failed to allocate %d bytes for iov_buf\n", WLC_IOCTL_SMLEN));
11509 		ret = BCME_NOMEM;
11510 		goto done;
11511 	}
11512 
11513 
11514 #if defined(CUSTOM_AMPDU_BA_WSIZE)
11515 	/* Set ampdu ba wsize to 64 or 16 */
11516 #ifdef CUSTOM_AMPDU_BA_WSIZE
11517 	ampdu_ba_wsize = CUSTOM_AMPDU_BA_WSIZE;
11518 #endif
11519 	if (ampdu_ba_wsize != 0) {
11520 		ret = dhd_iovar(dhd, 0, "ampdu_ba_wsize", (char *)&ampdu_ba_wsize,
11521 				sizeof(ampdu_ba_wsize), NULL, 0, TRUE);
11522 		if (ret < 0) {
11523 			DHD_ERROR(("%s Set ampdu_ba_wsize to %d failed  %d\n",
11524 				__FUNCTION__, ampdu_ba_wsize, ret));
11525 		}
11526 	}
11527 #endif
11528 
11529 #ifdef ENABLE_TEMP_THROTTLING
11530 	if (dhd->op_mode & DHD_FLAG_STA_MODE) {
11531 		memset(&temp_control, 0, sizeof(temp_control));
11532 		temp_control.enable = 1;
11533 		temp_control.control_bit = TEMP_THROTTLE_CONTROL_BIT;
11534 		ret = dhd_iovar(dhd, 0, "temp_throttle_control", (char *)&temp_control,
11535 				sizeof(temp_control), NULL, 0, TRUE);
11536 		if (ret < 0) {
11537 			DHD_ERROR(("%s Set temp_throttle_control to %d failed \n",
11538 				__FUNCTION__, ret));
11539 		}
11540 	}
11541 #endif /* ENABLE_TEMP_THROTTLING */
11542 
11543 #if defined(CUSTOM_AMPDU_MPDU)
11544 	ampdu_mpdu = CUSTOM_AMPDU_MPDU;
11545 	if (ampdu_mpdu != 0 && (ampdu_mpdu <= ampdu_ba_wsize)) {
11546 		ret = dhd_iovar(dhd, 0, "ampdu_mpdu", (char *)&ampdu_mpdu, sizeof(ampdu_mpdu),
11547 				NULL, 0, TRUE);
11548 		if (ret < 0) {
11549 			DHD_ERROR(("%s Set ampdu_mpdu to %d failed  %d\n",
11550 				__FUNCTION__, CUSTOM_AMPDU_MPDU, ret));
11551 		}
11552 	}
11553 #endif /* CUSTOM_AMPDU_MPDU */
11554 
11555 #if defined(CUSTOM_AMPDU_RELEASE)
11556 	ampdu_release = CUSTOM_AMPDU_RELEASE;
11557 	if (ampdu_release != 0 && (ampdu_release <= ampdu_ba_wsize)) {
11558 		ret = dhd_iovar(dhd, 0, "ampdu_release", (char *)&ampdu_release,
11559 				sizeof(ampdu_release), NULL, 0, TRUE);
11560 		if (ret < 0) {
11561 			DHD_ERROR(("%s Set ampdu_release to %d failed  %d\n",
11562 				__FUNCTION__, CUSTOM_AMPDU_RELEASE, ret));
11563 		}
11564 	}
11565 #endif /* CUSTOM_AMPDU_RELEASE */
11566 
11567 #if defined(CUSTOM_AMSDU_AGGSF)
11568 	amsdu_aggsf = CUSTOM_AMSDU_AGGSF;
11569 	if (amsdu_aggsf != 0) {
11570 		ret = dhd_iovar(dhd, 0, "amsdu_aggsf", (char *)&amsdu_aggsf, sizeof(amsdu_aggsf),
11571 				NULL, 0, TRUE);
11572 		if (ret < 0) {
11573 			DHD_ERROR(("%s Set amsdu_aggsf to %d failed %d\n",
11574 				__FUNCTION__, CUSTOM_AMSDU_AGGSF, ret));
11575 		}
11576 	}
11577 #endif /* CUSTOM_AMSDU_AGGSF */
11578 
11579 #if defined(SUPPORT_5G_1024QAM_VHT)
11580 #ifdef SUPPORT_5G_1024QAM_VHT
11581 	if (dhd_get_chipid(dhd) == BCM4361_CHIP_ID) {
11582 		vht_features |= 0x6; /* 5G 1024 QAM support */
11583 	}
11584 #endif /* SUPPORT_5G_1024QAM_VHT */
11585 	if (vht_features) {
11586 		ret = dhd_iovar(dhd, 0, "vht_features", (char *)&vht_features, sizeof(vht_features),
11587 				NULL, 0, TRUE);
11588 		if (ret < 0) {
11589 			DHD_ERROR(("%s vht_features set failed %d\n", __FUNCTION__, ret));
11590 
11591 			if (ret == BCME_NOTDOWN) {
11592 				uint wl_down = 1;
11593 				ret = dhd_wl_ioctl_cmd(dhd, WLC_DOWN,
11594 					(char *)&wl_down, sizeof(wl_down), TRUE, 0);
11595 				DHD_ERROR(("%s vht_features fail WL_DOWN : %d,"
11596 					" vht_features = 0x%x\n",
11597 					__FUNCTION__, ret, vht_features));
11598 
11599 				ret = dhd_iovar(dhd, 0, "vht_features", (char *)&vht_features,
11600 					sizeof(vht_features), NULL, 0, TRUE);
11601 				DHD_ERROR(("%s vht_features set. ret --> %d\n", __FUNCTION__, ret));
11602 			}
11603 		}
11604 	}
11605 #endif
11606 #ifdef DISABLE_11N_PROPRIETARY_RATES
11607 	ret = dhd_iovar(dhd, 0, "ht_features", (char *)&ht_features, sizeof(ht_features), NULL, 0,
11608 			TRUE);
11609 	if (ret < 0) {
11610 		DHD_ERROR(("%s ht_features set failed %d\n", __FUNCTION__, ret));
11611 	}
11612 #endif /* DISABLE_11N_PROPRIETARY_RATES */
11613 #ifdef CUSTOM_PSPRETEND_THR
11614 	/* Turn off MPC in AP mode */
11615 	ret = dhd_iovar(dhd, 0, "pspretend_threshold", (char *)&pspretend_thr,
11616 			sizeof(pspretend_thr), NULL, 0, TRUE);
11617 	if (ret < 0) {
11618 		DHD_ERROR(("%s pspretend_threshold for HostAPD failed  %d\n",
11619 			__FUNCTION__, ret));
11620 	}
11621 #endif
11622 
11623 	ret = dhd_iovar(dhd, 0, "buf_key_b4_m4", (char *)&buf_key_b4_m4, sizeof(buf_key_b4_m4),
11624 			NULL, 0, TRUE);
11625 	if (ret < 0) {
11626 		DHD_ERROR(("%s buf_key_b4_m4 set failed %d\n", __FUNCTION__, ret));
11627 	}
11628 #ifdef SUPPORT_SET_CAC
11629 	bcm_mkiovar("cac", (char *)&cac, sizeof(cac), iovbuf, sizeof(iovbuf));
11630 	if ((ret = dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0)) < 0) {
11631 		DHD_ERROR(("%s Failed to set cac to %d, %d\n", __FUNCTION__, cac, ret));
11632 	}
11633 #endif /* SUPPORT_SET_CAC */
11634 #ifdef DHD_ULP
11635 	/* Get the required details from dongle during preinit ioctl */
11636 	dhd_ulp_preinit(dhd);
11637 #endif /* DHD_ULP */
11638 
11639 	/* Read event_msgs mask */
11640 	ret = dhd_iovar(dhd, 0, "event_msgs", eventmask, WL_EVENTING_MASK_LEN, iovbuf,
11641 			sizeof(iovbuf), FALSE);
11642 	if (ret < 0) {
11643 		DHD_ERROR(("%s read Event mask failed %d\n", __FUNCTION__, ret));
11644 		goto done;
11645 	}
11646 	bcopy(iovbuf, eventmask, WL_EVENTING_MASK_LEN);
11647 
11648 	/* Setup event_msgs */
11649 	setbit(eventmask, WLC_E_SET_SSID);
11650 	setbit(eventmask, WLC_E_PRUNE);
11651 	setbit(eventmask, WLC_E_AUTH);
11652 	setbit(eventmask, WLC_E_AUTH_IND);
11653 	setbit(eventmask, WLC_E_ASSOC);
11654 	setbit(eventmask, WLC_E_REASSOC);
11655 	setbit(eventmask, WLC_E_REASSOC_IND);
11656 	if (!(dhd->op_mode & DHD_FLAG_IBSS_MODE))
11657 		setbit(eventmask, WLC_E_DEAUTH);
11658 	setbit(eventmask, WLC_E_DEAUTH_IND);
11659 	setbit(eventmask, WLC_E_DISASSOC_IND);
11660 	setbit(eventmask, WLC_E_DISASSOC);
11661 	setbit(eventmask, WLC_E_JOIN);
11662 	setbit(eventmask, WLC_E_BSSID);
11663 	setbit(eventmask, WLC_E_START);
11664 	setbit(eventmask, WLC_E_ASSOC_IND);
11665 #ifdef DHD_LOAD_CHIPALIVE
11666 	setbit(eventmask, WLC_E_OVERLAY_REQ);
11667 #else
11668 	setbit(eventmask, WLC_E_PSK_SUP);
11669 #endif
11670 	setbit(eventmask, WLC_E_LINK);
11671 	setbit(eventmask, WLC_E_MIC_ERROR);
11672 	setbit(eventmask, WLC_E_ASSOC_REQ_IE);
11673 	setbit(eventmask, WLC_E_ASSOC_RESP_IE);
11674 #ifdef LIMIT_BORROW
11675 	setbit(eventmask, WLC_E_ALLOW_CREDIT_BORROW);
11676 #endif
11677 #ifndef WL_CFG80211
11678 	setbit(eventmask, WLC_E_PMKID_CACHE);
11679 //	setbit(eventmask, WLC_E_TXFAIL); // terence 20181106: remove unnecessary event
11680 #endif
11681 	setbit(eventmask, WLC_E_JOIN_START);
11682 //	setbit(eventmask, WLC_E_SCAN_COMPLETE); // terence 20150628: remove redundant event
11683 #ifdef DHD_DEBUG
11684 	setbit(eventmask, WLC_E_SCAN_CONFIRM_IND);
11685 #endif
11686 #ifdef WLMEDIA_HTSF
11687 	setbit(eventmask, WLC_E_HTSFSYNC);
11688 #endif /* WLMEDIA_HTSF */
11689 #ifdef PNO_SUPPORT
11690 	setbit(eventmask, WLC_E_PFN_NET_FOUND);
11691 	setbit(eventmask, WLC_E_PFN_BEST_BATCHING);
11692 	setbit(eventmask, WLC_E_PFN_BSSID_NET_FOUND);
11693 	setbit(eventmask, WLC_E_PFN_BSSID_NET_LOST);
11694 #endif /* PNO_SUPPORT */
11695 	/* enable dongle roaming event */
11696 	setbit(eventmask, WLC_E_ROAM);
11697 #ifdef WLTDLS
11698 	setbit(eventmask, WLC_E_TDLS_PEER_EVENT);
11699 #endif /* WLTDLS */
11700 #ifdef WL_ESCAN
11701 	setbit(eventmask, WLC_E_ESCAN_RESULT);
11702 #endif /* WL_ESCAN */
11703 #ifdef CSI_SUPPORT
11704 	setbit(eventmask, WLC_E_CSI);
11705 #endif /* CSI_SUPPORT */
11706 #ifdef RTT_SUPPORT
11707 	setbit(eventmask, WLC_E_PROXD);
11708 #endif /* RTT_SUPPORT */
11709 #ifdef WL_CFG80211
11710 	setbit(eventmask, WLC_E_ESCAN_RESULT);
11711 	setbit(eventmask, WLC_E_AP_STARTED);
11712 	setbit(eventmask, WLC_E_ACTION_FRAME_RX);
11713 	if (dhd->op_mode & DHD_FLAG_P2P_MODE) {
11714 		setbit(eventmask, WLC_E_P2P_DISC_LISTEN_COMPLETE);
11715 	}
11716 #endif /* WL_CFG80211 */
11717 
11718 #if defined(SHOW_LOGTRACE) && defined(LOGTRACE_FROM_FILE)
11719 	if (dhd_logtrace_from_file(dhd)) {
11720 		setbit(eventmask, WLC_E_TRACE);
11721 	} else {
11722 		clrbit(eventmask, WLC_E_TRACE);
11723 	}
11724 #elif defined(SHOW_LOGTRACE)
11725 	setbit(eventmask, WLC_E_TRACE);
11726 #else
11727 	clrbit(eventmask, WLC_E_TRACE);
11728 #endif /* defined(SHOW_LOGTRACE) && defined(LOGTRACE_FROM_FILE) */
11729 
11730 	setbit(eventmask, WLC_E_CSA_COMPLETE_IND);
11731 #ifdef DHD_WMF
11732 	setbit(eventmask, WLC_E_PSTA_PRIMARY_INTF_IND);
11733 #endif
11734 #ifdef CUSTOM_EVENT_PM_WAKE
11735 	setbit(eventmask, WLC_E_EXCESS_PM_WAKE_EVENT);
11736 #endif	/* CUSTOM_EVENT_PM_WAKE */
11737 #ifdef DHD_LOSSLESS_ROAMING
11738 	setbit(eventmask, WLC_E_ROAM_PREP);
11739 #endif
11740 #if defined(PCIE_FULL_DONGLE) && defined(DHD_LOSSLESS_ROAMING)
11741 	dhd_update_flow_prio_map(dhd, DHD_FLOW_PRIO_LLR_MAP);
11742 #endif /* defined(PCIE_FULL_DONGLE) && defined(DHD_LOSSLESS_ROAMING) */
11743 
11744 #if defined(BCMPCIE) && defined(EAPOL_PKT_PRIO)
11745 	dhd_update_flow_prio_map(dhd, DHD_FLOW_PRIO_LLR_MAP);
11746 #endif /* defined(BCMPCIE) && defined(EAPOL_PKT_PRIO) */
11747 
11748 	/* Write updated Event mask */
11749 	ret = dhd_iovar(dhd, 0, "event_msgs", eventmask, WL_EVENTING_MASK_LEN, NULL, 0, TRUE);
11750 	if (ret < 0) {
11751 		DHD_ERROR(("%s Set Event mask failed %d\n", __FUNCTION__, ret));
11752 		goto done;
11753 	}
11754 
11755 	/* make up event mask ext message iovar for event larger than 128 */
11756 	msglen = ROUNDUP(WLC_E_LAST, NBBY)/NBBY + EVENTMSGS_EXT_STRUCT_SIZE;
11757 	eventmask_msg = (eventmsgs_ext_t*)kmalloc(msglen, GFP_KERNEL);
11758 	if (eventmask_msg == NULL) {
11759 		DHD_ERROR(("failed to allocate %d bytes for event_msg_ext\n", msglen));
11760 		ret = BCME_NOMEM;
11761 		goto done;
11762 	}
11763 	bzero(eventmask_msg, msglen);
11764 	eventmask_msg->ver = EVENTMSGS_VER;
11765 	eventmask_msg->len = ROUNDUP(WLC_E_LAST, NBBY)/NBBY;
11766 
11767 	/* Read event_msgs_ext mask */
11768 	ret2 = dhd_iovar(dhd, 0, "event_msgs_ext", (char *)eventmask_msg, msglen, iov_buf,
11769 			WLC_IOCTL_SMLEN, FALSE);
11770 
11771 	if (ret2 == 0) { /* event_msgs_ext must be supported */
11772 		bcopy(iov_buf, eventmask_msg, msglen);
11773 #ifdef RSSI_MONITOR_SUPPORT
11774 		setbit(eventmask_msg->mask, WLC_E_RSSI_LQM);
11775 #endif /* RSSI_MONITOR_SUPPORT */
11776 #ifdef GSCAN_SUPPORT
11777 		setbit(eventmask_msg->mask, WLC_E_PFN_GSCAN_FULL_RESULT);
11778 		setbit(eventmask_msg->mask, WLC_E_PFN_SCAN_COMPLETE);
11779 		setbit(eventmask_msg->mask, WLC_E_PFN_SSID_EXT);
11780 		setbit(eventmask_msg->mask, WLC_E_ROAM_EXP_EVENT);
11781 #endif /* GSCAN_SUPPORT */
11782 		setbit(eventmask_msg->mask, WLC_E_RSSI_LQM);
11783 #ifdef BT_WIFI_HANDOVER
11784 		setbit(eventmask_msg->mask, WLC_E_BT_WIFI_HANDOVER_REQ);
11785 #endif /* BT_WIFI_HANDOVER */
11786 #ifdef DBG_PKT_MON
11787 		setbit(eventmask_msg->mask, WLC_E_ROAM_PREP);
11788 #endif /* DBG_PKT_MON */
11789 #ifdef DHD_ULP
11790 		setbit(eventmask_msg->mask, WLC_E_ULP);
11791 #endif
11792 #ifdef ENABLE_TEMP_THROTTLING
11793 		setbit(eventmask_msg->mask, WLC_E_TEMP_THROTTLE);
11794 #endif /* ENABLE_TEMP_THROTTLING */
11795 #ifdef WL_CLIENT_SAE
11796 		setbit(eventmask_msg->mask, WLC_E_JOIN_START);
11797 #endif /* WL_CLIENT_SAE */
11798 
11799 		/* Write updated Event mask */
11800 		eventmask_msg->ver = EVENTMSGS_VER;
11801 		eventmask_msg->command = EVENTMSGS_SET_MASK;
11802 		eventmask_msg->len = ROUNDUP(WLC_E_LAST, NBBY)/NBBY;
11803 		ret = dhd_iovar(dhd, 0, "event_msgs_ext", (char *)eventmask_msg, msglen, NULL, 0,
11804 				TRUE);
11805 		if (ret < 0) {
11806 			DHD_ERROR(("%s write event mask ext failed %d\n", __FUNCTION__, ret));
11807 			goto done;
11808 		}
11809 	} else if (ret2 == BCME_UNSUPPORTED || ret2 == BCME_VERSION) {
11810 		/* Skip for BCME_UNSUPPORTED or BCME_VERSION */
11811 		DHD_ERROR(("%s event_msgs_ext not support or version mismatch %d\n",
11812 			__FUNCTION__, ret2));
11813 	} else {
11814 		DHD_ERROR(("%s read event mask ext failed %d\n", __FUNCTION__, ret2));
11815 		ret = ret2;
11816 		goto done;
11817 	}
11818 
11819 #if defined(DHD_8021X_DUMP) && defined(SHOW_LOGTRACE)
11820 	/* Enabling event log trace for EAP events */
11821 	el_tag = (wl_el_tag_params_t *)kmalloc(sizeof(wl_el_tag_params_t), GFP_KERNEL);
11822 	if (el_tag == NULL) {
11823 		DHD_ERROR(("failed to allocate %d bytes for event_msg_ext\n",
11824 				(int)sizeof(wl_el_tag_params_t)));
11825 		ret = BCME_NOMEM;
11826 		goto done;
11827 	}
11828 	el_tag->tag = EVENT_LOG_TAG_4WAYHANDSHAKE;
11829 	el_tag->set = 1;
11830 	el_tag->flags = EVENT_LOG_TAG_FLAG_LOG;
11831 	bcm_mkiovar("event_log_tag_control", (char *)el_tag,
11832 			sizeof(*el_tag), iovbuf, sizeof(iovbuf));
11833 	dhd_wl_ioctl_cmd(dhd, WLC_SET_VAR, iovbuf, sizeof(iovbuf), TRUE, 0);
11834 #endif /* DHD_8021X_DUMP */
11835 
11836 	dhd_wl_ioctl_cmd(dhd, WLC_SET_SCAN_CHANNEL_TIME, (char *)&scan_assoc_time,
11837 		sizeof(scan_assoc_time), TRUE, 0);
11838 	dhd_wl_ioctl_cmd(dhd, WLC_SET_SCAN_UNASSOC_TIME, (char *)&scan_unassoc_time,
11839 		sizeof(scan_unassoc_time), TRUE, 0);
11840 	dhd_wl_ioctl_cmd(dhd, WLC_SET_SCAN_PASSIVE_TIME, (char *)&scan_passive_time,
11841 		sizeof(scan_passive_time), TRUE, 0);
11842 
11843 #ifdef ARP_OFFLOAD_SUPPORT
11844 	/* Set and enable ARP offload feature for STA only  */
11845 #if defined(SOFTAP)
11846 	if (arpoe && !ap_fw_loaded)
11847 #else
11848 	if (arpoe)
11849 #endif
11850 	{
11851 		dhd_arp_offload_enable(dhd, TRUE);
11852 		dhd_arp_offload_set(dhd, dhd_arp_mode);
11853 	} else {
11854 		dhd_arp_offload_enable(dhd, FALSE);
11855 		dhd_arp_offload_set(dhd, 0);
11856 	}
11857 	dhd_arp_enable = arpoe;
11858 #endif /* ARP_OFFLOAD_SUPPORT */
11859 
11860 #ifdef PKT_FILTER_SUPPORT
11861 	/* Setup default defintions for pktfilter , enable in suspend */
11862 	if (dhd_master_mode) {
11863 		dhd->pktfilter_count = 6;
11864 		dhd->pktfilter[DHD_BROADCAST_FILTER_NUM] = NULL;
11865 		if (!FW_SUPPORTED(dhd, pf6)) {
11866 			dhd->pktfilter[DHD_MULTICAST4_FILTER_NUM] = NULL;
11867 			dhd->pktfilter[DHD_MULTICAST6_FILTER_NUM] = NULL;
11868 		} else {
11869 			/* Immediately pkt filter TYPE 6 Discard IPv4/IPv6 Multicast Packet */
11870 			dhd->pktfilter[DHD_MULTICAST4_FILTER_NUM] = DISCARD_IPV4_MCAST;
11871 			dhd->pktfilter[DHD_MULTICAST6_FILTER_NUM] = DISCARD_IPV6_MCAST;
11872 		}
11873 		/* apply APP pktfilter */
11874 		dhd->pktfilter[DHD_ARP_FILTER_NUM] = "105 0 0 12 0xFFFF 0x0806";
11875 
11876 		/* Setup filter to allow only unicast */
11877 		dhd->pktfilter[DHD_UNICAST_FILTER_NUM] = "100 0 0 0 0x01 0x00";
11878 
11879 		/* Add filter to pass multicastDNS packet and NOT filter out as Broadcast */
11880 		dhd->pktfilter[DHD_MDNS_FILTER_NUM] = NULL;
11881 
11882 		dhd->pktfilter[DHD_BROADCAST_ARP_FILTER_NUM] = NULL;
11883 		if (FW_SUPPORTED(dhd, pf6)) {
11884 			/* Immediately pkt filter TYPE 6 Dicard Broadcast IP packet */
11885 			dhd->pktfilter[DHD_IP4BCAST_DROP_FILTER_NUM] =
11886 				"107 1 6 IP4_H:16 0xf0 !0xe0 IP4_H:19 0xff 0xff";
11887 			dhd->pktfilter_count = 8;
11888 		}
11889 
11890 #ifdef GAN_LITE_NAT_KEEPALIVE_FILTER
11891 		dhd->pktfilter_count = 4;
11892 		/* Setup filter to block broadcast and NAT Keepalive packets */
11893 		/* discard all broadcast packets */
11894 		dhd->pktfilter[DHD_UNICAST_FILTER_NUM] = "100 0 0 0 0xffffff 0xffffff";
11895 		/* discard NAT Keepalive packets */
11896 		dhd->pktfilter[DHD_BROADCAST_FILTER_NUM] = "102 0 0 36 0xffffffff 0x11940009";
11897 		/* discard NAT Keepalive packets */
11898 		dhd->pktfilter[DHD_MULTICAST4_FILTER_NUM] = "104 0 0 38 0xffffffff 0x11940009";
11899 		dhd->pktfilter[DHD_MULTICAST6_FILTER_NUM] = NULL;
11900 #endif /* GAN_LITE_NAT_KEEPALIVE_FILTER */
11901 	} else
11902 		dhd_conf_discard_pkt_filter(dhd);
11903 	dhd_conf_add_pkt_filter(dhd);
11904 
11905 #if defined(SOFTAP)
11906 	if (ap_fw_loaded) {
11907 		dhd_enable_packet_filter(0, dhd);
11908 	}
11909 #endif /* defined(SOFTAP) */
11910 	dhd_set_packet_filter(dhd);
11911 #endif /* PKT_FILTER_SUPPORT */
11912 #ifdef DISABLE_11N
11913 	ret = dhd_iovar(dhd, 0, "nmode", (char *)&nmode, sizeof(nmode), NULL, 0, TRUE);
11914 	if (ret < 0)
11915 		DHD_ERROR(("%s wl nmode 0 failed %d\n", __FUNCTION__, ret));
11916 #endif /* DISABLE_11N */
11917 
11918 #ifdef ENABLE_BCN_LI_BCN_WAKEUP
11919 	dhd_iovar(dhd, 0, "bcn_li_bcn", (char *)&bcn_li_bcn, sizeof(bcn_li_bcn), NULL, 0, TRUE);
11920 #endif /* ENABLE_BCN_LI_BCN_WAKEUP */
11921 	/* query for 'clmver' to get clm version info from firmware */
11922 	memset(buf, 0, sizeof(buf));
11923 	ret = dhd_iovar(dhd, 0, "clmver", NULL, 0, buf, sizeof(buf), FALSE);
11924 	if (ret < 0)
11925 		DHD_ERROR(("%s clmver failed %d\n", __FUNCTION__, ret));
11926 	else {
11927 		char *ver_temp_buf = NULL, *ver_date_buf = NULL;
11928 		int len;
11929 
11930 		if ((ver_temp_buf = bcmstrstr(buf, "Data:")) == NULL) {
11931 			DHD_ERROR(("Couldn't find \"Data:\"\n"));
11932 		} else {
11933 			ver_date_buf = bcmstrstr(buf, "Creation:");
11934 			ptr = (ver_temp_buf + strlen("Data:"));
11935 			if ((ver_temp_buf = bcmstrtok(&ptr, "\n", 0)) == NULL) {
11936 				DHD_ERROR(("Couldn't find New line character\n"));
11937 			} else {
11938 				memset(clm_version, 0, CLM_VER_STR_LEN);
11939 				len = snprintf(clm_version, CLM_VER_STR_LEN - 1, "%s", ver_temp_buf);
11940 				if (ver_date_buf) {
11941 					ptr = (ver_date_buf + strlen("Creation:"));
11942 					ver_date_buf = bcmstrtok(&ptr, "\n", 0);
11943 					if (ver_date_buf)
11944 						snprintf(clm_version+len, CLM_VER_STR_LEN-1-len,
11945 							" (%s)", ver_date_buf);
11946 				}
11947 				DHD_INFO(("CLM version = %s\n", clm_version));
11948 			}
11949 		}
11950 	}
11951 
11952 	/* query for 'ver' to get version info from firmware */
11953 	memset(buf, 0, sizeof(buf));
11954 	ptr = buf;
11955 	ret = dhd_iovar(dhd, 0, "ver", NULL, 0, (char *)&buf, sizeof(buf), FALSE);
11956 	if (ret < 0)
11957 		DHD_ERROR(("%s failed %d\n", __FUNCTION__, ret));
11958 	else {
11959 		bcmstrtok(&ptr, "\n", 0);
11960 		strncpy(fw_version, buf, FW_VER_STR_LEN);
11961 		fw_version[FW_VER_STR_LEN-1] = '\0';
11962 		dhd_set_version_info(dhd, buf);
11963 #ifdef WRITE_WLANINFO
11964 		sec_save_wlinfo(buf, EPI_VERSION_STR, dhd->info->nv_path, clm_version);
11965 #endif /* WRITE_WLANINFO */
11966 	}
11967 #ifdef GEN_SOFTAP_INFO_FILE
11968 	sec_save_softap_info();
11969 #endif /* GEN_SOFTAP_INFO_FILE */
11970 
11971 #if defined(BCMSDIO)
11972 	dhd_txglom_enable(dhd, dhd->conf->bus_rxglom);
11973 #endif /* defined(BCMSDIO) */
11974 
11975 #if defined(BCMSDIO) || defined(BCMDBUS)
11976 #ifdef PROP_TXSTATUS
11977 	if (disable_proptx ||
11978 #ifdef PROP_TXSTATUS_VSDB
11979 		/* enable WLFC only if the firmware is VSDB when it is in STA mode */
11980 		(dhd->op_mode != DHD_FLAG_HOSTAP_MODE &&
11981 		 dhd->op_mode != DHD_FLAG_IBSS_MODE) ||
11982 #endif /* PROP_TXSTATUS_VSDB */
11983 		FALSE) {
11984 		wlfc_enable = FALSE;
11985 	}
11986 	ret = dhd_conf_get_disable_proptx(dhd);
11987 	if (ret == 0){
11988 		disable_proptx = 0;
11989 		wlfc_enable = TRUE;
11990 	} else if (ret >= 1) {
11991 		disable_proptx = 1;
11992 		wlfc_enable = FALSE;
11993 		/* terence 20161229: we should set ampdu_hostreorder=0 when disable_proptx=1 */
11994 		hostreorder = 0;
11995 	}
11996 
11997 #if defined(PROP_TXSTATUS)
11998 #ifdef USE_WFA_CERT_CONF
11999 	if (sec_get_param_wfa_cert(dhd, SET_PARAM_PROPTX, &proptx) == BCME_OK) {
12000 		DHD_ERROR(("%s , read proptx param=%d\n", __FUNCTION__, proptx));
12001 		wlfc_enable = proptx;
12002 	}
12003 #endif /* USE_WFA_CERT_CONF */
12004 #endif /* PROP_TXSTATUS */
12005 
12006 #ifndef DISABLE_11N
12007 	ret = dhd_wl_ioctl_cmd(dhd, WLC_DOWN, (char *)&wl_down, sizeof(wl_down), TRUE, 0);
12008 	ret2 = dhd_iovar(dhd, 0, "ampdu_hostreorder", (char *)&hostreorder, sizeof(hostreorder),
12009 			NULL, 0, TRUE);
12010 	if (ret2 < 0) {
12011 		DHD_ERROR(("%s wl ampdu_hostreorder failed %d\n", __FUNCTION__, ret2));
12012 		if (ret2 != BCME_UNSUPPORTED)
12013 			ret = ret2;
12014 
12015 		if (ret == BCME_NOTDOWN) {
12016 			uint wl_down = 1;
12017 			ret2 = dhd_wl_ioctl_cmd(dhd, WLC_DOWN, (char *)&wl_down,
12018 				sizeof(wl_down), TRUE, 0);
12019 			DHD_ERROR(("%s ampdu_hostreorder fail WL_DOWN : %d, hostreorder :%d\n",
12020 				__FUNCTION__, ret2, hostreorder));
12021 
12022 			ret2 = dhd_iovar(dhd, 0, "ampdu_hostreorder", (char *)&hostreorder,
12023 					sizeof(hostreorder), NULL, 0, TRUE);
12024 			DHD_ERROR(("%s wl ampdu_hostreorder. ret --> %d\n", __FUNCTION__, ret2));
12025 			if (ret2 != BCME_UNSUPPORTED)
12026 					ret = ret2;
12027 		}
12028 		if (ret2 != BCME_OK)
12029 			hostreorder = 0;
12030 	}
12031 #endif /* DISABLE_11N */
12032 
12033 
12034 	if (wlfc_enable) {
12035 		dhd_wlfc_init(dhd);
12036 		/* terence 20161229: enable ampdu_hostreorder if tlv enabled */
12037 		dhd_conf_set_intiovar(dhd, WLC_SET_VAR, "ampdu_hostreorder", 1, 0, TRUE);
12038 	}
12039 #ifndef DISABLE_11N
12040 	else if (hostreorder)
12041 		dhd_wlfc_hostreorder_init(dhd);
12042 #endif /* DISABLE_11N */
12043 #else
12044 	/* terence 20161229: disable ampdu_hostreorder if PROP_TXSTATUS not defined */
12045 	DHD_PRINT("%s: not define PROP_TXSTATUS\n", __FUNCTION__);
12046 	dhd_conf_set_intiovar(dhd, WLC_SET_VAR, "ampdu_hostreorder", 0, 0, TRUE);
12047 #endif /* PROP_TXSTATUS */
12048 #endif /* BCMSDIO || BCMDBUS */
12049 #ifndef PCIE_FULL_DONGLE
12050 	/* For FD we need all the packets at DHD to handle intra-BSS forwarding */
12051 	if (FW_SUPPORTED(dhd, ap)) {
12052 		wl_ap_isolate = AP_ISOLATE_SENDUP_ALL;
12053 		ret = dhd_iovar(dhd, 0, "ap_isolate", (char *)&wl_ap_isolate, sizeof(wl_ap_isolate),
12054 				NULL, 0, TRUE);
12055 		if (ret < 0)
12056 			DHD_ERROR(("%s failed %d\n", __FUNCTION__, ret));
12057 	}
12058 #endif /* PCIE_FULL_DONGLE */
12059 #ifdef PNO_SUPPORT
12060 	if (!dhd->pno_state) {
12061 		dhd_pno_init(dhd);
12062 	}
12063 #endif
12064 #ifdef RTT_SUPPORT
12065 	if (!dhd->rtt_state) {
12066 		ret = dhd_rtt_init(dhd);
12067 		if (ret < 0) {
12068 			DHD_ERROR(("%s failed to initialize RTT\n", __FUNCTION__));
12069 		}
12070 	}
12071 #endif
12072 #ifdef WL11U
12073 	dhd_interworking_enable(dhd);
12074 #endif /* WL11U */
12075 
12076 #ifdef SUPPORT_SENSORHUB
12077 	DHD_ERROR(("%s: SensorHub enabled %d\n",
12078 			__FUNCTION__, dhd->info->shub_enable));
12079 	ret2 = dhd_iovar(dhd, 0, "shub", NULL, 0,
12080 			(char *)&shub_ctl, sizeof(shub_ctl), FALSE);
12081 	if (ret2 < 0) {
12082 		DHD_ERROR(("%s failed to get shub hub enable information %d\n",
12083 			__FUNCTION__, ret2));
12084 		dhd->info->shub_enable = 0;
12085 	} else {
12086 		dhd->info->shub_enable = shub_ctl.enable;
12087 		DHD_ERROR(("%s: checking sensorhub enable %d\n",
12088 			__FUNCTION__, dhd->info->shub_enable));
12089 	}
12090 #else
12091 	DHD_ERROR(("%s: SensorHub diabled %d\n",
12092 			__FUNCTION__, dhd->info->shub_enable));
12093 	dhd->info->shub_enable = FALSE;
12094 	shub_ctl.enable = FALSE;
12095 	ret2 = dhd_iovar(dhd, 0, "shub", (char *)&shub_ctl, sizeof(shub_ctl),
12096 			NULL, 0, TRUE);
12097 	if (ret2 < 0) {
12098 		DHD_ERROR(("%s failed to set ShubHub disable\n",
12099 			__FUNCTION__));
12100 	}
12101 #endif /* SUPPORT_SENSORHUB */
12102 
12103 
12104 #ifdef NDO_CONFIG_SUPPORT
12105 	dhd->ndo_enable = FALSE;
12106 	dhd->ndo_host_ip_overflow = FALSE;
12107 	dhd->ndo_max_host_ip = NDO_MAX_HOST_IP_ENTRIES;
12108 #endif /* NDO_CONFIG_SUPPORT */
12109 
12110 	/* ND offload version supported */
12111 	dhd->ndo_version = dhd_ndo_get_version(dhd);
12112 	if (dhd->ndo_version > 0) {
12113 		DHD_INFO(("%s: ndo version %d\n", __FUNCTION__, dhd->ndo_version));
12114 
12115 #ifdef NDO_CONFIG_SUPPORT
12116 		/* enable Unsolicited NA filter */
12117 		ret = dhd_ndo_unsolicited_na_filter_enable(dhd, 1);
12118 		if (ret < 0) {
12119 			DHD_ERROR(("%s failed to enable Unsolicited NA filter\n", __FUNCTION__));
12120 		}
12121 #endif /* NDO_CONFIG_SUPPORT */
12122 	}
12123 
12124 	/* check dongle supports wbtext or not */
12125 	dhd->wbtext_support = FALSE;
12126 	if (dhd_wl_ioctl_get_intiovar(dhd, "wnm_bsstrans_resp", &wnm_bsstrans_resp,
12127 			WLC_GET_VAR, FALSE, 0) != BCME_OK) {
12128 		DHD_ERROR(("failed to get wnm_bsstrans_resp\n"));
12129 	}
12130 	if (wnm_bsstrans_resp == WL_BSSTRANS_POLICY_PRODUCT_WBTEXT) {
12131 		dhd->wbtext_support = TRUE;
12132 	}
12133 #ifndef WBTEXT
12134 	/* driver can turn off wbtext feature through makefile */
12135 	if (dhd->wbtext_support) {
12136 		if (dhd_wl_ioctl_set_intiovar(dhd, "wnm_bsstrans_resp",
12137 				WL_BSSTRANS_POLICY_ROAM_ALWAYS,
12138 				WLC_SET_VAR, FALSE, 0) != BCME_OK) {
12139 			DHD_ERROR(("failed to disable WBTEXT\n"));
12140 		}
12141 	}
12142 #endif /* !WBTEXT */
12143 
12144 	/* WNM capabilities */
12145 	wnm_cap = 0
12146 #ifdef WL11U
12147 		| WL_WNM_BSSTRANS | WL_WNM_NOTIF
12148 #endif
12149 #ifdef WBTEXT
12150 		| WL_WNM_BSSTRANS | WL_WNM_MAXIDLE
12151 #endif
12152 		;
12153 	if (dhd_iovar(dhd, 0, "wnm", (char *)&wnm_cap, sizeof(wnm_cap), NULL, 0, TRUE) < 0) {
12154 		DHD_ERROR(("failed to set WNM capabilities\n"));
12155 	}
12156 
12157 	dhd_conf_postinit_ioctls(dhd);
12158 done:
12159 
12160 	if (eventmask_msg)
12161 		kfree(eventmask_msg);
12162 	if (iov_buf)
12163 		kfree(iov_buf);
12164 #if defined(DHD_8021X_DUMP) && defined(SHOW_LOGTRACE)
12165 	if (el_tag)
12166 		kfree(el_tag);
12167 #endif /* DHD_8021X_DUMP */
12168 	return ret;
12169 }
12170 
12171 #ifdef DHD_LOAD_CHIPALIVE
12172 void
dhd_alive_preinit(dhd_pub_t * dhd)12173 dhd_alive_preinit(dhd_pub_t *dhd)
12174 {
12175 	/* We should set bus_rxglom first, so DON'T set/get any fw cmd in this function,  */
12176 
12177 	DHD_TRACE(("Enter %s\n", __FUNCTION__));
12178 
12179 #ifdef WL_EXT_IAPSTA
12180 	if (op_mode == 0) {
12181 		wl_ext_iapsta_alive_preinit(dhd->info->iflist[0]->net);
12182 	}
12183 #endif
12184 
12185 	dhd_txglom_enable_host(dhd, dhd->conf->bus_rxglom);
12186 }
12187 
12188 void
dhd_alive_postinit(dhd_pub_t * dhd)12189 dhd_alive_postinit(dhd_pub_t *dhd)
12190 {
12191 	int ret = 0, ret2 = 0;
12192 	char buf[WLC_IOCTL_SMLEN];
12193 	char *ptr;
12194 #ifdef WLTDLS
12195 	uint32 tdls;
12196 #endif /* WLTDLS */
12197 #if defined(ARP_OFFLOAD_SUPPORT)
12198 	int arpoe = 0;
12199 	uint32 version;
12200 #endif
12201 	shub_control_t shub_ctl;
12202 	uint wnm_bsstrans_resp = 0;
12203 
12204 	/* Don't change any fw setting here, only get fw seettings to sync for host side */
12205 
12206 	DHD_TRACE(("Enter %s\n", __FUNCTION__));
12207 
12208 	if (op_mode == 0) {
12209 #ifdef WL_EXT_IAPSTA
12210 		op_mode = wl_ext_iapsta_alive_postinit(dhd->info->iflist[0]->net);
12211 #else
12212 		op_mode = DHD_FLAG_STA_MODE;
12213 #endif
12214 	}
12215 	DHD_PRINT("%s: set op_mode %d\n", __FUNCTION__, op_mode);
12216 
12217 	dhd->op_mode = op_mode;
12218 
12219 	if (dhd->op_mode == DHD_FLAG_MFG_MODE) {
12220 		/* Check and adjust IOCTL response timeout for Manufactring firmware */
12221 		dhd_os_set_ioctl_resp_timeout(MFG_IOCTL_RESP_TIMEOUT);
12222 		DHD_ERROR(("%s : Set IOCTL response time for Manufactring Firmware\n",
12223 			__FUNCTION__));
12224 	} else {
12225 		dhd_os_set_ioctl_resp_timeout(IOCTL_RESP_TIMEOUT);
12226 		DHD_INFO(("%s : Set IOCTL response time.\n", __FUNCTION__));
12227 	}
12228 
12229 	dhd->suspend_bcn_li_dtim = CUSTOM_SUSPEND_BCN_LI_DTIM;
12230 
12231 #ifdef DHDTCPACK_SUPPRESS
12232 	DHD_PRINT("%s: Set tcpack_sup_mode %d\n", __FUNCTION__, dhd->conf->tcpack_sup_mode);
12233 	dhd_tcpack_suppress_set(dhd, dhd->conf->tcpack_sup_mode);
12234 #endif
12235 
12236 	if (dhd->op_mode == DHD_FLAG_HOSTAP_MODE) {
12237 #ifdef PKT_FILTER_SUPPORT
12238 		if (dhd_conf_get_insuspend(dhd, AP_FILTER_IN_SUSPEND))
12239 			dhd_pkt_filter_enable = TRUE;
12240 		else
12241 			dhd_pkt_filter_enable = FALSE;
12242 #endif
12243 	}
12244 
12245 #ifdef ARP_OFFLOAD_SUPPORT
12246 	dhd_conf_get_iovar(dhd, 0, WLC_GET_VAR, "arpoe", (char *)&arpoe, sizeof(arpoe));
12247 	if (arpoe) {
12248 		ret = dhd_conf_get_iovar(dhd, 0, WLC_GET_VAR, "arp_version",
12249 			(char *)&version, sizeof(version));
12250 		if (ret) {
12251 			dhd->arp_version = 1;
12252 		} else {
12253 			DHD_INFO(("%s: ARP Version= %x\n", __FUNCTION__, version));
12254 			dhd->arp_version = version;
12255 		}
12256 	}
12257 	dhd_arp_enable = arpoe;
12258 #endif /* ARP_OFFLOAD_SUPPORT */
12259 
12260 	/* Get the default device MAC address directly from firmware */
12261 	dhd_conf_get_iovar(dhd, 0, WLC_GET_VAR, "cur_etheraddr",
12262 		(char *)dhd->mac.octet, ETHER_ADDR_LEN);
12263 
12264 	/* get a capabilities from firmware */
12265 	{
12266 		uint32 cap_buf_size = sizeof(dhd->fw_capabilities);
12267 		memset(dhd->fw_capabilities, 0, cap_buf_size);
12268 		ret = dhd_iovar(dhd, 0, "cap", NULL, 0, dhd->fw_capabilities, (cap_buf_size - 1),
12269 				FALSE);
12270 		if (ret < 0) {
12271 			DHD_ERROR(("%s: Get Capability failed (error=%d)\n",
12272 				__FUNCTION__, ret));
12273 		}
12274 		else {
12275 			memmove(&dhd->fw_capabilities[1], dhd->fw_capabilities, (cap_buf_size - 1));
12276 			dhd->fw_capabilities[0] = ' ';
12277 			dhd->fw_capabilities[cap_buf_size - 2] = ' ';
12278 			dhd->fw_capabilities[cap_buf_size - 1] = '\0';
12279 		}
12280 	}
12281 
12282 	dhd_conf_get_country(dhd, &dhd->dhd_cspec);
12283 
12284 #ifdef WLTDLS
12285 	if (FW_SUPPORTED(dhd, tdls)) {
12286 		ret= dhd_conf_get_iovar(dhd, 0, WLC_GET_VAR, "tdls_enable", (char *)&tdls,
12287 			sizeof(tdls));
12288 		if (ret)
12289 			dhd->tdls_enable = FALSE;
12290 		else
12291 			dhd->tdls_enable = TRUE;
12292 	}
12293 #endif /* WLTDLS */
12294 
12295 #ifdef PKT_FILTER_SUPPORT
12296 	/* Setup default defintions for pktfilter , enable in suspend */
12297 	if (dhd_master_mode) {
12298 		dhd->pktfilter_count = 6;
12299 		dhd->pktfilter[DHD_BROADCAST_FILTER_NUM] = NULL;
12300 		if (!FW_SUPPORTED(dhd, pf6)) {
12301 			dhd->pktfilter[DHD_MULTICAST4_FILTER_NUM] = NULL;
12302 			dhd->pktfilter[DHD_MULTICAST6_FILTER_NUM] = NULL;
12303 		} else {
12304 			/* Immediately pkt filter TYPE 6 Discard IPv4/IPv6 Multicast Packet */
12305 			dhd->pktfilter[DHD_MULTICAST4_FILTER_NUM] = DISCARD_IPV4_MCAST;
12306 			dhd->pktfilter[DHD_MULTICAST6_FILTER_NUM] = DISCARD_IPV6_MCAST;
12307 		}
12308 		/* apply APP pktfilter */
12309 		dhd->pktfilter[DHD_ARP_FILTER_NUM] = "105 0 0 12 0xFFFF 0x0806";
12310 
12311 		/* Setup filter to allow only unicast */
12312 		dhd->pktfilter[DHD_UNICAST_FILTER_NUM] = "100 0 0 0 0x01 0x00";
12313 
12314 		/* Add filter to pass multicastDNS packet and NOT filter out as Broadcast */
12315 		dhd->pktfilter[DHD_MDNS_FILTER_NUM] = NULL;
12316 
12317 		dhd->pktfilter[DHD_BROADCAST_ARP_FILTER_NUM] = NULL;
12318 		if (FW_SUPPORTED(dhd, pf6)) {
12319 			/* Immediately pkt filter TYPE 6 Dicard Broadcast IP packet */
12320 			dhd->pktfilter[DHD_IP4BCAST_DROP_FILTER_NUM] =
12321 				"107 1 6 IP4_H:16 0xf0 !0xe0 IP4_H:19 0xff 0xff";
12322 			dhd->pktfilter_count = 8;
12323 		}
12324 
12325 #ifdef GAN_LITE_NAT_KEEPALIVE_FILTER
12326 		dhd->pktfilter_count = 4;
12327 		/* Setup filter to block broadcast and NAT Keepalive packets */
12328 		/* discard all broadcast packets */
12329 		dhd->pktfilter[DHD_UNICAST_FILTER_NUM] = "100 0 0 0 0xffffff 0xffffff";
12330 		/* discard NAT Keepalive packets */
12331 		dhd->pktfilter[DHD_BROADCAST_FILTER_NUM] = "102 0 0 36 0xffffffff 0x11940009";
12332 		/* discard NAT Keepalive packets */
12333 		dhd->pktfilter[DHD_MULTICAST4_FILTER_NUM] = "104 0 0 38 0xffffffff 0x11940009";
12334 		dhd->pktfilter[DHD_MULTICAST6_FILTER_NUM] = NULL;
12335 #endif /* GAN_LITE_NAT_KEEPALIVE_FILTER */
12336 	} else
12337 		dhd_conf_discard_pkt_filter(dhd);
12338 	dhd_conf_add_pkt_filter(dhd);
12339 #endif /* PKT_FILTER_SUPPORT */
12340 
12341 	/* query for 'ver' to get version info from firmware */
12342 	memset(buf, 0, sizeof(buf));
12343 	ptr = buf;
12344 	dhd_conf_get_iovar(dhd, 0, WLC_GET_VAR, "ver", (char *)buf, sizeof(buf));
12345 	if (!ret) {
12346 		bcmstrtok(&ptr, "\n", 0);
12347 		/* Print fw version info */
12348 		dhd_set_version_info(dhd, buf);
12349 	}
12350 
12351 #ifdef PROP_TXSTATUS
12352 	/* 1. In AP mode, we have to issue "dhd -i wlan0 proptx 0" before host shut down
12353 	  *    to avoid MAC_OPEN/MAC_CLOSE from fw when there is client in the "wl assoclist".
12354 	  *    So we have to enable proptxstatus again when we back from chip alive.
12355 	  * 2. Otherwise, call dhd_chipalive_wlfc_init to check fw have init wlfc already or not,
12356 	  *    if initialized, issue proptx_credit_map to trigger WLC_E_FIFO_CREDIT_MAP
12357 	  *    and WLC_E_BCMC_CREDIT_SUPPORT to sync for dhd driver.
12358 	  */
12359 	if (op_mode & DHD_FLAG_HOSTAP_MODE && dhd->conf->disable_proptx!=1) {
12360 		dhd_wlfc_init(dhd);
12361 	} else {
12362 		ret = dhd_chipalive_wlfc_init(dhd);
12363 		if (!ret) {
12364 			dhd_conf_get_iovar(dhd, 0, WLC_GET_VAR, "proptx_credit_map",
12365 				(char *)buf, sizeof(buf));
12366 		}
12367 	}
12368 #endif
12369 
12370 #ifdef PNO_SUPPORT
12371 	if (!dhd->pno_state) {
12372 		dhd_pno_init(dhd);
12373 	}
12374 #endif
12375 #ifdef RTT_SUPPORT
12376 	if (!dhd->rtt_state) {
12377 		ret = dhd_rtt_init(dhd);
12378 		if (ret < 0) {
12379 			DHD_ERROR(("%s failed to initialize RTT\n", __FUNCTION__));
12380 		}
12381 	}
12382 #endif
12383 
12384 #ifdef SUPPORT_SENSORHUB
12385 	DHD_ERROR(("%s: SensorHub enabled %d\n",
12386 			__FUNCTION__, dhd->info->shub_enable));
12387 	ret2 = dhd_iovar(dhd, 0, "shub", NULL, 0,
12388 			(char *)&shub_ctl, sizeof(shub_ctl), FALSE);
12389 	if (ret2 < 0) {
12390 		DHD_ERROR(("%s failed to get shub hub enable information %d\n",
12391 			__FUNCTION__, ret2));
12392 		dhd->info->shub_enable = 0;
12393 	} else {
12394 		dhd->info->shub_enable = shub_ctl.enable;
12395 		DHD_ERROR(("%s: checking sensorhub enable %d\n",
12396 			__FUNCTION__, dhd->info->shub_enable));
12397 	}
12398 #else
12399 	DHD_PRINT("%s: SensorHub diabled %d\n",
12400 			__FUNCTION__, dhd->info->shub_enable);
12401 	dhd->info->shub_enable = FALSE;
12402 	shub_ctl.enable = FALSE;
12403 	ret2 = dhd_iovar(dhd, 0, "shub", (char *)&shub_ctl, sizeof(shub_ctl),
12404 			NULL, 0, TRUE);
12405 	if (ret2 < 0) {
12406 		DHD_PRINT("%s failed to set ShubHub disable\n",
12407 			__FUNCTION__);
12408 	}
12409 #endif /* SUPPORT_SENSORHUB */
12410 
12411 #ifdef NDO_CONFIG_SUPPORT
12412 	dhd->ndo_enable = FALSE;
12413 	dhd->ndo_host_ip_overflow = FALSE;
12414 	dhd->ndo_max_host_ip = NDO_MAX_HOST_IP_ENTRIES;
12415 #endif /* NDO_CONFIG_SUPPORT */
12416 
12417 	/* ND offload version supported */
12418 	dhd->ndo_version = dhd_ndo_get_version(dhd);
12419 	if (dhd->ndo_version > 0) {
12420 		DHD_INFO(("%s: ndo version %d\n", __FUNCTION__, dhd->ndo_version));
12421 
12422 #ifdef NDO_CONFIG_SUPPORT
12423 		/* enable Unsolicited NA filter */
12424 		ret = dhd_ndo_unsolicited_na_filter_enable(dhd, 1);
12425 		if (ret < 0) {
12426 			DHD_ERROR(("%s failed to enable Unsolicited NA filter\n", __FUNCTION__));
12427 		}
12428 #endif /* NDO_CONFIG_SUPPORT */
12429 	}
12430 
12431 	/* check dongle supports wbtext or not */
12432 	dhd->wbtext_support = FALSE;
12433 	if (dhd_wl_ioctl_get_intiovar(dhd, "wnm_bsstrans_resp", &wnm_bsstrans_resp,
12434 			WLC_GET_VAR, FALSE, 0) != BCME_OK) {
12435 		DHD_ERROR(("failed to get wnm_bsstrans_resp\n"));
12436 	}
12437 	if (wnm_bsstrans_resp == WL_BSSTRANS_POLICY_PRODUCT_WBTEXT) {
12438 		dhd->wbtext_support = TRUE;
12439 	}
12440 
12441 }
12442 #endif
12443 
12444 int
dhd_iovar(dhd_pub_t * pub,int ifidx,char * name,char * param_buf,uint param_len,char * res_buf,uint res_len,int set)12445 dhd_iovar(dhd_pub_t *pub, int ifidx, char *name, char *param_buf, uint param_len, char *res_buf,
12446 		uint res_len, int set)
12447 {
12448 	char *buf = NULL;
12449 	int input_len;
12450 	wl_ioctl_t ioc;
12451 	int ret;
12452 
12453 	if (res_len > WLC_IOCTL_MAXLEN || param_len > WLC_IOCTL_MAXLEN)
12454 		return BCME_BADARG;
12455 
12456 	input_len = strlen(name) + 1 + param_len;
12457 	if (input_len > WLC_IOCTL_MAXLEN)
12458 		return BCME_BADARG;
12459 
12460 	buf = NULL;
12461 	if (set) {
12462 		if (res_buf || res_len != 0) {
12463 			DHD_ERROR(("%s: SET wrong arguemnet\n", __FUNCTION__));
12464 			ret = BCME_BADARG;
12465 			goto exit;
12466 		}
12467 		buf = kzalloc(input_len, GFP_KERNEL);
12468 		if (!buf) {
12469 			DHD_ERROR(("%s: mem alloc failed\n", __FUNCTION__));
12470 			ret = BCME_NOMEM;
12471 			goto exit;
12472 		}
12473 		ret = bcm_mkiovar(name, param_buf, param_len, buf, input_len);
12474 		if (!ret) {
12475 			ret = BCME_NOMEM;
12476 			goto exit;
12477 		}
12478 
12479 		ioc.cmd = WLC_SET_VAR;
12480 		ioc.buf = buf;
12481 		ioc.len = input_len;
12482 		ioc.set = set;
12483 
12484 		ret = dhd_wl_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
12485 	} else {
12486 		if (!res_buf || !res_len) {
12487 			DHD_ERROR(("%s: GET failed. resp_buf NULL or length 0.\n", __FUNCTION__));
12488 			ret = BCME_BADARG;
12489 			goto exit;
12490 		}
12491 
12492 		if (res_len < input_len) {
12493 			DHD_INFO(("%s: res_len(%d) < input_len(%d)\n", __FUNCTION__,
12494 					res_len, input_len));
12495 			buf = kzalloc(input_len, GFP_KERNEL);
12496 			if (!buf) {
12497 				DHD_ERROR(("%s: mem alloc failed\n", __FUNCTION__));
12498 				ret = BCME_NOMEM;
12499 				goto exit;
12500 			}
12501 			ret = bcm_mkiovar(name, param_buf, param_len, buf, input_len);
12502 			if (!ret) {
12503 				ret = BCME_NOMEM;
12504 				goto exit;
12505 			}
12506 
12507 			ioc.cmd = WLC_GET_VAR;
12508 			ioc.buf = buf;
12509 			ioc.len = input_len;
12510 			ioc.set = set;
12511 
12512 			ret = dhd_wl_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
12513 
12514 			if (ret == BCME_OK) {
12515 				memcpy(res_buf, buf, res_len);
12516 			}
12517 		} else {
12518 			memset(res_buf, 0, res_len);
12519 			ret = bcm_mkiovar(name, param_buf, param_len, res_buf, res_len);
12520 			if (!ret) {
12521 				ret = BCME_NOMEM;
12522 				goto exit;
12523 			}
12524 
12525 			ioc.cmd = WLC_GET_VAR;
12526 			ioc.buf = res_buf;
12527 			ioc.len = res_len;
12528 			ioc.set = set;
12529 
12530 			ret = dhd_wl_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
12531 		}
12532 	}
12533 exit:
12534 	kfree(buf);
12535 	return ret;
12536 }
12537 
12538 int
dhd_getiovar(dhd_pub_t * pub,int ifidx,char * name,char * cmd_buf,uint cmd_len,char ** resptr,uint resp_len)12539 dhd_getiovar(dhd_pub_t *pub, int ifidx, char *name, char *cmd_buf,
12540 	uint cmd_len, char **resptr, uint resp_len)
12541 {
12542 	int len = resp_len;
12543 	int ret;
12544 	char *buf = *resptr;
12545 	wl_ioctl_t ioc;
12546 	if (resp_len > WLC_IOCTL_MAXLEN)
12547 		return BCME_BADARG;
12548 
12549 	memset(buf, 0, resp_len);
12550 
12551 	ret = bcm_mkiovar(name, cmd_buf, cmd_len, buf, len);
12552 	if (ret == 0) {
12553 		return BCME_BUFTOOSHORT;
12554 	}
12555 
12556 	memset(&ioc, 0, sizeof(ioc));
12557 
12558 	ioc.cmd = WLC_GET_VAR;
12559 	ioc.buf = buf;
12560 	ioc.len = len;
12561 	ioc.set = 0;
12562 
12563 	ret = dhd_wl_ioctl(pub, ifidx, &ioc, ioc.buf, ioc.len);
12564 
12565 	return ret;
12566 }
12567 
12568 
dhd_change_mtu(dhd_pub_t * dhdp,int new_mtu,int ifidx)12569 int dhd_change_mtu(dhd_pub_t *dhdp, int new_mtu, int ifidx)
12570 {
12571 	struct dhd_info *dhd = dhdp->info;
12572 	struct net_device *dev = NULL;
12573 
12574 	ASSERT(dhd && dhd->iflist[ifidx]);
12575 	dev = dhd->iflist[ifidx]->net;
12576 	ASSERT(dev);
12577 
12578 	if (netif_running(dev)) {
12579 		DHD_ERROR(("%s: Must be down to change its MTU\n", dev->name));
12580 		return BCME_NOTDOWN;
12581 	}
12582 
12583 #define DHD_MIN_MTU 1500
12584 #define DHD_MAX_MTU 1752
12585 
12586 	if ((new_mtu < DHD_MIN_MTU) || (new_mtu > DHD_MAX_MTU)) {
12587 		DHD_ERROR(("%s: MTU size %d is invalid.\n", __FUNCTION__, new_mtu));
12588 		return BCME_BADARG;
12589 	}
12590 
12591 	dev->mtu = new_mtu;
12592 	return 0;
12593 }
12594 
12595 #ifdef ARP_OFFLOAD_SUPPORT
12596 /* add or remove AOE host ip(s) (up to 8 IPs on the interface)  */
12597 void
aoe_update_host_ipv4_table(dhd_pub_t * dhd_pub,u32 ipa,bool add,int idx)12598 aoe_update_host_ipv4_table(dhd_pub_t *dhd_pub, u32 ipa, bool add, int idx)
12599 {
12600 	u32 ipv4_buf[MAX_IPV4_ENTRIES]; /* temp save for AOE host_ip table */
12601 	int i;
12602 	int ret;
12603 
12604 	bzero(ipv4_buf, sizeof(ipv4_buf));
12605 
12606 	/* display what we've got */
12607 	ret = dhd_arp_get_arp_hostip_table(dhd_pub, ipv4_buf, sizeof(ipv4_buf), idx);
12608 	DHD_ARPOE(("%s: hostip table read from Dongle:\n", __FUNCTION__));
12609 #ifdef AOE_DBG
12610 	dhd_print_buf(ipv4_buf, 32, 4); /* max 8 IPs 4b each */
12611 #endif
12612 	/* now we saved hoste_ip table, clr it in the dongle AOE */
12613 	dhd_aoe_hostip_clr(dhd_pub, idx);
12614 
12615 	if (ret) {
12616 		DHD_ERROR(("%s failed\n", __FUNCTION__));
12617 		return;
12618 	}
12619 
12620 	for (i = 0; i < MAX_IPV4_ENTRIES; i++) {
12621 		if (add && (ipv4_buf[i] == 0)) {
12622 				ipv4_buf[i] = ipa;
12623 				add = FALSE; /* added ipa to local table  */
12624 				DHD_ARPOE(("%s: Saved new IP in temp arp_hostip[%d]\n",
12625 				__FUNCTION__, i));
12626 		} else if (ipv4_buf[i] == ipa) {
12627 			ipv4_buf[i]	= 0;
12628 			DHD_ARPOE(("%s: removed IP:%x from temp table %d\n",
12629 				__FUNCTION__, ipa, i));
12630 		}
12631 
12632 		if (ipv4_buf[i] != 0) {
12633 			/* add back host_ip entries from our local cache */
12634 			dhd_arp_offload_add_ip(dhd_pub, ipv4_buf[i], idx);
12635 			DHD_ARPOE(("%s: added IP:%x to dongle arp_hostip[%d]\n\n",
12636 				__FUNCTION__, ipv4_buf[i], i));
12637 		}
12638 	}
12639 #ifdef AOE_DBG
12640 	/* see the resulting hostip table */
12641 	dhd_arp_get_arp_hostip_table(dhd_pub, ipv4_buf, sizeof(ipv4_buf), idx);
12642 	DHD_ARPOE(("%s: read back arp_hostip table:\n", __FUNCTION__));
12643 	dhd_print_buf(ipv4_buf, 32, 4); /* max 8 IPs 4b each */
12644 #endif
12645 }
12646 
12647 /*
12648  * Notification mechanism from kernel to our driver. This function is called by the Linux kernel
12649  * whenever there is an event related to an IP address.
12650  * ptr : kernel provided pointer to IP address that has changed
12651  */
dhd_inetaddr_notifier_call(struct notifier_block * this,unsigned long event,void * ptr)12652 static int dhd_inetaddr_notifier_call(struct notifier_block *this,
12653 	unsigned long event,
12654 	void *ptr)
12655 {
12656 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
12657 
12658 	dhd_info_t *dhd;
12659 	dhd_pub_t *dhd_pub;
12660 	int idx;
12661 
12662 	if (!dhd_arp_enable)
12663 		return NOTIFY_DONE;
12664 	if (!ifa || !(ifa->ifa_dev->dev))
12665 		return NOTIFY_DONE;
12666 
12667 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
12668 	/* Filter notifications meant for non Broadcom devices */
12669 	if ((ifa->ifa_dev->dev->netdev_ops != &dhd_ops_pri) &&
12670 	    (ifa->ifa_dev->dev->netdev_ops != &dhd_ops_virt)) {
12671 #if defined(WL_ENABLE_P2P_IF)
12672 		if (!wl_cfgp2p_is_ifops(ifa->ifa_dev->dev->netdev_ops))
12673 #endif /* WL_ENABLE_P2P_IF */
12674 			return NOTIFY_DONE;
12675 	}
12676 #endif /* LINUX_VERSION_CODE */
12677 
12678 	dhd = DHD_DEV_INFO(ifa->ifa_dev->dev);
12679 	if (!dhd)
12680 		return NOTIFY_DONE;
12681 
12682 	dhd_pub = &dhd->pub;
12683 
12684 	if (dhd_pub->arp_version == 1) {
12685 		idx = 0;
12686 	} else {
12687 		for (idx = 0; idx < DHD_MAX_IFS; idx++) {
12688 			if (dhd->iflist[idx] && dhd->iflist[idx]->net == ifa->ifa_dev->dev)
12689 			break;
12690 		}
12691 		if (idx < DHD_MAX_IFS)
12692 			DHD_TRACE(("ifidx : %p %s %d\n", dhd->iflist[idx]->net,
12693 				dhd->iflist[idx]->name, dhd->iflist[idx]->idx));
12694 		else {
12695 			DHD_ERROR(("Cannot find ifidx for(%s) set to 0\n", ifa->ifa_label));
12696 			idx = 0;
12697 		}
12698 	}
12699 
12700 	switch (event) {
12701 		case NETDEV_UP:
12702 			DHD_ARPOE(("%s: [%s] Up IP: 0x%x\n",
12703 				__FUNCTION__, ifa->ifa_label, ifa->ifa_address));
12704 
12705 			if (dhd->pub.busstate != DHD_BUS_DATA) {
12706 				DHD_ERROR(("%s: bus not ready, exit\n", __FUNCTION__));
12707 				if (dhd->pend_ipaddr) {
12708 					DHD_ERROR(("%s: overwrite pending ipaddr: 0x%x\n",
12709 						__FUNCTION__, dhd->pend_ipaddr));
12710 				}
12711 				dhd->pend_ipaddr = ifa->ifa_address;
12712 				break;
12713 			}
12714 
12715 #ifdef AOE_IP_ALIAS_SUPPORT
12716 			DHD_ARPOE(("%s:add aliased IP to AOE hostip cache\n",
12717 				__FUNCTION__));
12718 			aoe_update_host_ipv4_table(dhd_pub, ifa->ifa_address, TRUE, idx);
12719 #endif /* AOE_IP_ALIAS_SUPPORT */
12720 			dhd_conf_set_garp(dhd_pub, idx, ifa->ifa_address, TRUE);
12721 			break;
12722 
12723 		case NETDEV_DOWN:
12724 			DHD_ARPOE(("%s: [%s] Down IP: 0x%x\n",
12725 				__FUNCTION__, ifa->ifa_label, ifa->ifa_address));
12726 			dhd->pend_ipaddr = 0;
12727 #ifdef AOE_IP_ALIAS_SUPPORT
12728 			DHD_ARPOE(("%s:interface is down, AOE clr all for this if\n",
12729 				__FUNCTION__));
12730 			if ((dhd_pub->op_mode & DHD_FLAG_HOSTAP_MODE) ||
12731 				(ifa->ifa_dev->dev != dhd_linux_get_primary_netdev(dhd_pub))) {
12732 				aoe_update_host_ipv4_table(dhd_pub, ifa->ifa_address, FALSE, idx);
12733 			} else
12734 #endif /* AOE_IP_ALIAS_SUPPORT */
12735 			{
12736 				dhd_aoe_hostip_clr(&dhd->pub, idx);
12737 				dhd_aoe_arp_clr(&dhd->pub, idx);
12738 			}
12739 			dhd_conf_set_garp(dhd_pub, idx, ifa->ifa_address, FALSE);
12740 			break;
12741 
12742 		default:
12743 			DHD_ARPOE(("%s: do noting for [%s] Event: %lu\n",
12744 				__func__, ifa->ifa_label, event));
12745 			break;
12746 	}
12747 	return NOTIFY_DONE;
12748 }
12749 #endif /* ARP_OFFLOAD_SUPPORT */
12750 
12751 #if defined(CONFIG_IPV6) && defined(IPV6_NDO_SUPPORT)
12752 /* Neighbor Discovery Offload: defered handler */
12753 static void
dhd_inet6_work_handler(void * dhd_info,void * event_data,u8 event)12754 dhd_inet6_work_handler(void *dhd_info, void *event_data, u8 event)
12755 {
12756 	struct ipv6_work_info_t *ndo_work = (struct ipv6_work_info_t *)event_data;
12757 	dhd_info_t *dhd = (dhd_info_t *)dhd_info;
12758 	dhd_pub_t *dhdp;
12759 	int ret;
12760 
12761 	if (!dhd) {
12762 		DHD_ERROR(("%s: invalid dhd_info\n", __FUNCTION__));
12763 		goto done;
12764 	}
12765 	dhdp = &dhd->pub;
12766 
12767 	if (event != DHD_WQ_WORK_IPV6_NDO) {
12768 		DHD_ERROR(("%s: unexpected event\n", __FUNCTION__));
12769 		goto done;
12770 	}
12771 
12772 	if (!ndo_work) {
12773 		DHD_ERROR(("%s: ipv6 work info is not initialized\n", __FUNCTION__));
12774 		return;
12775 	}
12776 
12777 	switch (ndo_work->event) {
12778 		case NETDEV_UP:
12779 #ifndef NDO_CONFIG_SUPPORT
12780 			DHD_TRACE(("%s: Enable NDO \n ", __FUNCTION__));
12781 			ret = dhd_ndo_enable(dhdp, TRUE);
12782 			if (ret < 0) {
12783 				DHD_ERROR(("%s: Enabling NDO Failed %d\n", __FUNCTION__, ret));
12784 			}
12785 #endif /* !NDO_CONFIG_SUPPORT */
12786 			DHD_TRACE(("%s: Add a host ip for NDO\n", __FUNCTION__));
12787 			if (dhdp->ndo_version > 0) {
12788 				/* inet6 addr notifier called only for unicast address */
12789 				ret = dhd_ndo_add_ip_with_type(dhdp, &ndo_work->ipv6_addr[0],
12790 					WL_ND_IPV6_ADDR_TYPE_UNICAST, ndo_work->if_idx);
12791 			} else {
12792 				ret = dhd_ndo_add_ip(dhdp, &ndo_work->ipv6_addr[0],
12793 						ndo_work->if_idx);
12794 			}
12795 			if (ret < 0) {
12796 				DHD_ERROR(("%s: Adding a host ip for NDO failed %d\n",
12797 					__FUNCTION__, ret));
12798 			}
12799 			break;
12800 		case NETDEV_DOWN:
12801 			if (dhdp->ndo_version > 0) {
12802 				DHD_TRACE(("%s: Remove a host ip for NDO\n", __FUNCTION__));
12803 				ret = dhd_ndo_remove_ip_by_addr(dhdp,
12804 					&ndo_work->ipv6_addr[0], ndo_work->if_idx);
12805 			} else {
12806 				DHD_TRACE(("%s: Clear host ip table for NDO \n", __FUNCTION__));
12807 				ret = dhd_ndo_remove_ip(dhdp, ndo_work->if_idx);
12808 			}
12809 			if (ret < 0) {
12810 				DHD_ERROR(("%s: Removing host ip for NDO failed %d\n",
12811 					__FUNCTION__, ret));
12812 				goto done;
12813 			}
12814 #ifdef NDO_CONFIG_SUPPORT
12815 			if (dhdp->ndo_host_ip_overflow) {
12816 				ret = dhd_dev_ndo_update_inet6addr(
12817 					dhd_idx2net(dhdp, ndo_work->if_idx));
12818 				if ((ret < 0) && (ret != BCME_NORESOURCE)) {
12819 					DHD_ERROR(("%s: Updating host ip for NDO failed %d\n",
12820 						__FUNCTION__, ret));
12821 					goto done;
12822 				}
12823 			}
12824 #else /* !NDO_CONFIG_SUPPORT */
12825 			DHD_TRACE(("%s: Disable NDO\n ", __FUNCTION__));
12826 			ret = dhd_ndo_enable(dhdp, FALSE);
12827 			if (ret < 0) {
12828 				DHD_ERROR(("%s: disabling NDO Failed %d\n", __FUNCTION__, ret));
12829 				goto done;
12830 			}
12831 #endif /* NDO_CONFIG_SUPPORT */
12832 			break;
12833 
12834 		default:
12835 			DHD_ERROR(("%s: unknown notifier event \n", __FUNCTION__));
12836 			break;
12837 	}
12838 done:
12839 	/* free ndo_work. alloced while scheduling the work */
12840 	if (ndo_work) {
12841 		kfree(ndo_work);
12842 	}
12843 
12844 	return;
12845 }
12846 
12847 /*
12848  * Neighbor Discovery Offload: Called when an interface
12849  * is assigned with ipv6 address.
12850  * Handles only primary interface
12851  */
dhd_inet6addr_notifier_call(struct notifier_block * this,unsigned long event,void * ptr)12852 int dhd_inet6addr_notifier_call(struct notifier_block *this, unsigned long event, void *ptr)
12853 {
12854 	dhd_info_t *dhd;
12855 	dhd_pub_t *dhdp;
12856 	struct inet6_ifaddr *inet6_ifa = ptr;
12857 	struct ipv6_work_info_t *ndo_info;
12858 	int idx;
12859 
12860 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
12861 	/* Filter notifications meant for non Broadcom devices */
12862 	if (inet6_ifa->idev->dev->netdev_ops != &dhd_ops_pri) {
12863 			return NOTIFY_DONE;
12864 	}
12865 #endif /* LINUX_VERSION_CODE */
12866 
12867 	dhd = DHD_DEV_INFO(inet6_ifa->idev->dev);
12868 	if (!dhd) {
12869 		return NOTIFY_DONE;
12870 	}
12871 	dhdp = &dhd->pub;
12872 
12873 	/* Supports only primary interface */
12874 	idx = dhd_net2idx(dhd, inet6_ifa->idev->dev);
12875 	if (idx != 0) {
12876 		return NOTIFY_DONE;
12877 	}
12878 
12879 	/* FW capability */
12880 	if (!FW_SUPPORTED(dhdp, ndoe)) {
12881 		return NOTIFY_DONE;
12882 	}
12883 
12884 	ndo_info = (struct ipv6_work_info_t *)kzalloc(sizeof(struct ipv6_work_info_t), GFP_ATOMIC);
12885 	if (!ndo_info) {
12886 		DHD_ERROR(("%s: ipv6 work alloc failed\n", __FUNCTION__));
12887 		return NOTIFY_DONE;
12888 	}
12889 
12890 	/* fill up ndo_info */
12891 	ndo_info->event = event;
12892 	ndo_info->if_idx = idx;
12893 	memcpy(ndo_info->ipv6_addr, &inet6_ifa->addr, IPV6_ADDR_LEN);
12894 
12895 	/* defer the work to thread as it may block kernel */
12896 	dhd_deferred_schedule_work(dhd->dhd_deferred_wq, (void *)ndo_info, DHD_WQ_WORK_IPV6_NDO,
12897 		dhd_inet6_work_handler, DHD_WQ_WORK_PRIORITY_LOW);
12898 	return NOTIFY_DONE;
12899 }
12900 #endif /* CONFIG_IPV6 && IPV6_NDO_SUPPORT */
12901 
12902 int
dhd_register_if(dhd_pub_t * dhdp,int ifidx,bool need_rtnl_lock)12903 dhd_register_if(dhd_pub_t *dhdp, int ifidx, bool need_rtnl_lock)
12904 {
12905 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
12906 	dhd_if_t *ifp;
12907 	struct net_device *net = NULL;
12908 	int err = 0;
12909 	uint8 temp_addr[ETHER_ADDR_LEN] = { 0x00, 0x90, 0x4c, 0x11, 0x22, 0x33 };
12910 
12911 	DHD_TRACE(("%s: ifidx %d\n", __FUNCTION__, ifidx));
12912 
12913 	if (dhd == NULL || dhd->iflist[ifidx] == NULL) {
12914 		DHD_ERROR(("%s: Invalid Interface\n", __FUNCTION__));
12915 		return BCME_ERROR;
12916 	}
12917 
12918 	ASSERT(dhd && dhd->iflist[ifidx]);
12919 	ifp = dhd->iflist[ifidx];
12920 	net = ifp->net;
12921 	ASSERT(net && (ifp->idx == ifidx));
12922 
12923 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
12924 	ASSERT(!net->open);
12925 	net->get_stats = dhd_get_stats;
12926 	net->do_ioctl = dhd_ioctl_entry;
12927 	net->hard_start_xmit = dhd_start_xmit;
12928 	net->set_mac_address = dhd_set_mac_address;
12929 	net->set_multicast_list = dhd_set_multicast_list;
12930 	net->open = net->stop = NULL;
12931 #else
12932 	ASSERT(!net->netdev_ops);
12933 	net->netdev_ops = &dhd_ops_virt;
12934 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31) */
12935 
12936 	/* Ok, link into the network layer... */
12937 	if (ifidx == 0) {
12938 		/*
12939 		 * device functions for the primary interface only
12940 		 */
12941 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
12942 		net->open = dhd_open;
12943 		net->stop = dhd_stop;
12944 #else
12945 		net->netdev_ops = &dhd_ops_pri;
12946 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31) */
12947 		if (!ETHER_ISNULLADDR(dhd->pub.mac.octet))
12948 			memcpy(temp_addr, dhd->pub.mac.octet, ETHER_ADDR_LEN);
12949 	} else {
12950 		/*
12951 		 * We have to use the primary MAC for virtual interfaces
12952 		 */
12953 		memcpy(temp_addr, ifp->mac_addr, ETHER_ADDR_LEN);
12954 		/*
12955 		 * Android sets the locally administered bit to indicate that this is a
12956 		 * portable hotspot.  This will not work in simultaneous AP/STA mode,
12957 		 * nor with P2P.  Need to set the Donlge's MAC address, and then use that.
12958 		 */
12959 		if (!memcmp(temp_addr, dhd->iflist[0]->mac_addr,
12960 			ETHER_ADDR_LEN)) {
12961 			DHD_ERROR(("%s interface [%s]: set locally administered bit in MAC\n",
12962 			__func__, net->name));
12963 			temp_addr[0] |= 0x02;
12964 		}
12965 	}
12966 
12967 	net->hard_header_len = ETH_HLEN + dhd->pub.hdrlen;
12968 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
12969 	net->ethtool_ops = &dhd_ethtool_ops;
12970 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24) */
12971 
12972 #if defined(WL_WIRELESS_EXT)
12973 #if WIRELESS_EXT < 19
12974 	net->get_wireless_stats = dhd_get_wireless_stats;
12975 #endif /* WIRELESS_EXT < 19 */
12976 #if WIRELESS_EXT > 12
12977 	net->wireless_handlers = &wl_iw_handler_def;
12978 #endif /* WIRELESS_EXT > 12 */
12979 #endif /* defined(WL_WIRELESS_EXT) */
12980 
12981 	dhd->pub.rxsz = DBUS_RX_BUFFER_SIZE_DHD(net);
12982 
12983 #ifdef WLMESH
12984 	if (ifidx >= 2 && dhdp->conf->fw_type == FW_TYPE_MESH) {
12985 		temp_addr[4] ^= 0x80;
12986 		temp_addr[4] += ifidx;
12987 		temp_addr[5] += ifidx;
12988 	}
12989 #endif
12990 	memcpy(net->dev_addr, temp_addr, ETHER_ADDR_LEN);
12991 
12992 	if (ifidx == 0)
12993 		DHD_PRINT("%s\n", dhd_version);
12994 	else {
12995 #ifdef WL_EXT_IAPSTA
12996 		wl_ext_iapsta_update_net_device(net, ifidx);
12997 #endif /* WL_EXT_IAPSTA */
12998 		if (_dhd_set_mac_address(dhd, ifidx, net->dev_addr) == 0)
12999 			DHD_INFO(("%s: MACID is overwritten\n", __FUNCTION__));
13000 		else
13001 			DHD_ERROR(("%s: _dhd_set_mac_address() failed\n", __FUNCTION__));
13002 	}
13003 
13004 	if (need_rtnl_lock)
13005 		err = register_netdev(net);
13006 	else
13007 		err = register_netdevice(net);
13008 
13009 	if (err != 0) {
13010 		DHD_ERROR(("couldn't register the net device [%s], err %d\n", net->name, err));
13011 		goto fail;
13012 	}
13013 #if defined(WL_EXT_IAPSTA) || defined(USE_IW) || defined(WL_ESCAN)
13014 	wl_ext_event_attach_netdev(net, ifidx, ifp->bssidx);
13015 #ifdef WL_ESCAN
13016 	wl_escan_event_attach(net, dhdp);
13017 #endif /* WL_ESCAN */
13018 #ifdef WL_EXT_IAPSTA
13019 	wl_ext_iapsta_attach_netdev(net, ifidx, ifp->bssidx);
13020 	wl_ext_iapsta_attach_name(net, ifidx);
13021 #endif /* WL_EXT_IAPSTA */
13022 #endif /* WL_EXT_IAPSTA || USE_IW || WL_ESCAN */
13023 
13024 
13025 
13026 	DHD_PRINT("Register interface [%s]  MAC: "MACDBG"\n\n", net->name,
13027 #if defined(CUSTOMER_HW4_DEBUG)
13028 		MAC2STRDBG(dhd->pub.mac.octet));
13029 #else
13030 		MAC2STRDBG(net->dev_addr));
13031 #endif /* CUSTOMER_HW4_DEBUG */
13032 
13033 #if defined(SOFTAP) && defined(WL_WIRELESS_EXT) && !defined(WL_CFG80211)
13034 //		wl_iw_iscan_set_scan_broadcast_prep(net, 1);
13035 #endif
13036 
13037 #if (defined(BCMPCIE) || (defined(BCMLXSDMMC) && (LINUX_VERSION_CODE >= \
13038 	KERNEL_VERSION(2, 6, 27))) || defined(BCMDBUS))
13039 	if (ifidx == 0) {
13040 #if defined(BCMLXSDMMC) && !defined(DHD_PRELOAD)
13041 		up(&dhd_registration_sem);
13042 #endif /* BCMLXSDMMC */
13043 		if (!dhd_download_fw_on_driverload) {
13044 #ifdef WL_CFG80211
13045 			wl_terminate_event_handler(net);
13046 #endif /* WL_CFG80211 */
13047 #if defined(DHD_LB_RXP)
13048 			__skb_queue_purge(&dhd->rx_pend_queue);
13049 #endif /* DHD_LB_RXP */
13050 
13051 #if defined(DHD_LB_TXP)
13052 			skb_queue_purge(&dhd->tx_pend_queue);
13053 #endif /* DHD_LB_TXP */
13054 
13055 #ifdef SHOW_LOGTRACE
13056 			/* Release the skbs from queue for WLC_E_TRACE event */
13057 			dhd_event_logtrace_flush_queue(dhdp);
13058 #endif /* SHOW_LOGTRACE */
13059 
13060 #ifdef DHDTCPACK_SUPPRESS
13061 			dhd_tcpack_suppress_set(dhdp, TCPACK_SUP_OFF);
13062 #endif /* DHDTCPACK_SUPPRESS */
13063 			dhd_net_bus_devreset(net, TRUE);
13064 #ifdef BCMLXSDMMC
13065 			dhd_net_bus_suspend(net);
13066 #endif /* BCMLXSDMMC */
13067 #ifdef DHD_LOAD_CHIPALIVE
13068 			if (!dhd_chip_alive)
13069 #endif
13070 			wifi_platform_set_power(dhdp->info->adapter, FALSE, WIFI_TURNOFF_DELAY);
13071 #if defined(BT_OVER_SDIO)
13072 			dhd->bus_user_count--;
13073 #endif /* BT_OVER_SDIO */
13074 		}
13075 #if defined(WL_WIRELESS_EXT)
13076 		wl_iw_down(net, &dhd->pub);
13077 #endif /* defined(WL_WIRELESS_EXT) */
13078 	}
13079 #endif /* OEM_ANDROID && (BCMPCIE || (BCMLXSDMMC && KERNEL_VERSION >= 2.6.27)) */
13080 	return 0;
13081 
13082 fail:
13083 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)
13084 	net->open = NULL;
13085 #else
13086 	net->netdev_ops = NULL;
13087 #endif
13088 	return err;
13089 }
13090 
13091 void
dhd_bus_detach(dhd_pub_t * dhdp)13092 dhd_bus_detach(dhd_pub_t *dhdp)
13093 {
13094 	dhd_info_t *dhd;
13095 
13096 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
13097 
13098 	if (dhdp) {
13099 		dhd = (dhd_info_t *)dhdp->info;
13100 		if (dhd) {
13101 
13102 			/*
13103 			 * In case of Android cfg80211 driver, the bus is down in dhd_stop,
13104 			 *  calling stop again will cuase SD read/write errors.
13105 			 */
13106 			if (dhd->pub.busstate != DHD_BUS_DOWN && dhd_download_fw_on_driverload) {
13107 				/* Stop the protocol module */
13108 				dhd_prot_stop(&dhd->pub);
13109 
13110 				/* Stop the bus module */
13111 #ifdef BCMDBUS
13112 				/* Force Dongle terminated */
13113 				if (dhd_wl_ioctl_cmd(dhdp, WLC_TERMINATED, NULL, 0, TRUE, 0) < 0)
13114 					DHD_ERROR(("%s Setting WLC_TERMINATED failed\n",
13115 						__FUNCTION__));
13116 				dbus_stop(dhd->pub.bus);
13117 				dhd->pub.busstate = DHD_BUS_DOWN;
13118 #else
13119 				dhd_bus_stop(dhd->pub.bus, TRUE);
13120 #endif /* BCMDBUS */
13121 			}
13122 
13123 #if defined(OOB_INTR_ONLY) || defined(BCMPCIE_OOB_HOST_WAKE)
13124 			dhd_bus_oob_intr_unregister(dhdp);
13125 #endif
13126 		}
13127 	}
13128 }
13129 
13130 
dhd_detach(dhd_pub_t * dhdp)13131 void dhd_detach(dhd_pub_t *dhdp)
13132 {
13133 	dhd_info_t *dhd;
13134 	unsigned long flags;
13135 	int timer_valid = FALSE;
13136 	struct net_device *dev;
13137 #ifdef WL_CFG80211
13138 	struct bcm_cfg80211 *cfg = NULL;
13139 #endif
13140 #ifdef HOFFLOAD_MODULES
13141 	struct module_metadata *hmem = NULL;
13142 #endif
13143 	if (!dhdp)
13144 		return;
13145 
13146 	dhd = (dhd_info_t *)dhdp->info;
13147 	if (!dhd)
13148 		return;
13149 
13150 	dev = dhd->iflist[0]->net;
13151 
13152 	if (dev) {
13153 		rtnl_lock();
13154 		if (dev->flags & IFF_UP) {
13155 			/* If IFF_UP is still up, it indicates that
13156 			 * "ifconfig wlan0 down" hasn't been called.
13157 			 * So invoke dev_close explicitly here to
13158 			 * bring down the interface.
13159 			 */
13160 			DHD_TRACE(("IFF_UP flag is up. Enforcing dev_close from detach \n"));
13161 			dev_close(dev);
13162 		}
13163 		rtnl_unlock();
13164 	}
13165 
13166 	DHD_TRACE(("%s: Enter state 0x%x\n", __FUNCTION__, dhd->dhd_state));
13167 
13168 	dhd->pub.up = 0;
13169 	if (!(dhd->dhd_state & DHD_ATTACH_STATE_DONE)) {
13170 		/* Give sufficient time for threads to start running in case
13171 		 * dhd_attach() has failed
13172 		 */
13173 		OSL_SLEEP(100);
13174 	}
13175 #ifdef DHD_WET
13176 	dhd_free_wet_info(&dhd->pub, dhd->pub.wet_info);
13177 #endif /* DHD_WET */
13178 #if defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW)
13179 #endif /* defined(BCM_DNGL_EMBEDIMAGE) || defined(BCM_REQUEST_FW) */
13180 
13181 #ifdef PROP_TXSTATUS
13182 #ifdef DHD_WLFC_THREAD
13183 	if (dhd->pub.wlfc_thread) {
13184 		kthread_stop(dhd->pub.wlfc_thread);
13185 		dhdp->wlfc_thread_go = TRUE;
13186 		wake_up_interruptible(&dhdp->wlfc_wqhead);
13187 	}
13188 	dhd->pub.wlfc_thread = NULL;
13189 #endif /* DHD_WLFC_THREAD */
13190 #endif /* PROP_TXSTATUS */
13191 
13192 #ifdef DHD_TIMESYNC
13193 	if (dhd->dhd_state & DHD_ATTACH_TIMESYNC_ATTACH_DONE) {
13194 		dhd_timesync_detach(dhdp);
13195 	}
13196 #endif /* DHD_TIMESYNC */
13197 #ifdef WL_CFG80211
13198 	if (dev) {
13199 		wl_cfg80211_down(dev);
13200 	}
13201 #endif /* WL_CFG80211 */
13202 
13203 	if (dhd->dhd_state & DHD_ATTACH_STATE_PROT_ATTACH) {
13204 		dhd_bus_detach(dhdp);
13205 #ifdef BCMPCIE
13206 		if (is_reboot == SYS_RESTART) {
13207 			extern bcmdhd_wifi_platdata_t *dhd_wifi_platdata;
13208 			if (dhd_wifi_platdata && !dhdp->dongle_reset) {
13209 				dhdpcie_bus_clock_stop(dhdp->bus);
13210 				wifi_platform_set_power(dhd_wifi_platdata->adapters,
13211 					FALSE, WIFI_TURNOFF_DELAY);
13212 			}
13213 		}
13214 #endif /* BCMPCIE */
13215 #ifndef PCIE_FULL_DONGLE
13216 		if (dhdp->prot)
13217 			dhd_prot_detach(dhdp);
13218 #endif /* !PCIE_FULL_DONGLE */
13219 	}
13220 
13221 #ifdef ARP_OFFLOAD_SUPPORT
13222 	if (dhd_inetaddr_notifier_registered) {
13223 		dhd_inetaddr_notifier_registered = FALSE;
13224 		unregister_inetaddr_notifier(&dhd_inetaddr_notifier);
13225 	}
13226 #endif /* ARP_OFFLOAD_SUPPORT */
13227 #if defined(CONFIG_IPV6) && defined(IPV6_NDO_SUPPORT)
13228 	if (dhd_inet6addr_notifier_registered) {
13229 		dhd_inet6addr_notifier_registered = FALSE;
13230 		unregister_inet6addr_notifier(&dhd_inet6addr_notifier);
13231 	}
13232 #endif /* CONFIG_IPV6 && IPV6_NDO_SUPPORT */
13233 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
13234 	if (dhd->dhd_state & DHD_ATTACH_STATE_EARLYSUSPEND_DONE) {
13235 		if (dhd->early_suspend.suspend)
13236 			unregister_early_suspend(&dhd->early_suspend);
13237 	}
13238 #endif /* CONFIG_HAS_EARLYSUSPEND && DHD_USE_EARLYSUSPEND */
13239 
13240 #if defined(WL_WIRELESS_EXT)
13241 	if (dhd->dhd_state & DHD_ATTACH_STATE_WL_ATTACH) {
13242 		/* Detatch and unlink in the iw */
13243 		wl_iw_detach(dev, dhdp);
13244 	}
13245 #endif /* defined(WL_WIRELESS_EXT) */
13246 #if defined(WL_EXT_IAPSTA) || defined(USE_IW) || defined(WL_ESCAN)
13247 #ifdef WL_EXT_IAPSTA
13248 	wl_ext_iapsta_dettach(dhdp);
13249 #endif /* WL_EXT_IAPSTA */
13250 #ifdef WL_ESCAN
13251 	wl_escan_detach(dev, dhdp);
13252 #endif /* WL_ESCAN */
13253 	wl_ext_event_dettach(dhdp);
13254 #endif /* WL_EXT_IAPSTA || USE_IW || WL_ESCAN */
13255 
13256 #ifdef DHD_ULP
13257 	dhd_ulp_deinit(dhd->pub.osh, dhdp);
13258 #endif /* DHD_ULP */
13259 
13260 	/* delete all interfaces, start with virtual  */
13261 	if (dhd->dhd_state & DHD_ATTACH_STATE_ADD_IF) {
13262 		int i = 1;
13263 		dhd_if_t *ifp;
13264 
13265 		/* Cleanup virtual interfaces */
13266 		dhd_net_if_lock_local(dhd);
13267 		for (i = 1; i < DHD_MAX_IFS; i++) {
13268 			if (dhd->iflist[i]) {
13269 				dhd_remove_if(&dhd->pub, i, TRUE);
13270 			}
13271 		}
13272 		dhd_net_if_unlock_local(dhd);
13273 
13274 		/*  delete primary interface 0 */
13275 		ifp = dhd->iflist[0];
13276 		ASSERT(ifp);
13277 		ASSERT(ifp->net);
13278 		if (ifp && ifp->net) {
13279 #ifdef WL_CFG80211
13280 			cfg = wl_get_cfg(ifp->net);
13281 #endif
13282 			/* in unregister_netdev case, the interface gets freed by net->destructor
13283 			 * (which is set to free_netdev)
13284 			 */
13285 			if (ifp->net->reg_state == NETREG_UNINITIALIZED) {
13286 				free_netdev(ifp->net);
13287 			} else {
13288 				argos_register_notifier_deinit();
13289 #ifdef SET_RPS_CPUS
13290 				custom_rps_map_clear(ifp->net->_rx);
13291 #endif /* SET_RPS_CPUS */
13292 				netif_tx_disable(ifp->net);
13293 				unregister_netdev(ifp->net);
13294 			}
13295 #ifdef PCIE_FULL_DONGLE
13296 			ifp->net = DHD_NET_DEV_NULL;
13297 #else
13298 			ifp->net = NULL;
13299 #endif /* PCIE_FULL_DONGLE */
13300 
13301 #ifdef DHD_WMF
13302 			dhd_wmf_cleanup(dhdp, 0);
13303 #endif /* DHD_WMF */
13304 #ifdef DHD_L2_FILTER
13305 			bcm_l2_filter_arp_table_update(dhdp->osh, ifp->phnd_arp_table, TRUE,
13306 				NULL, FALSE, dhdp->tickcnt);
13307 			deinit_l2_filter_arp_table(dhdp->osh, ifp->phnd_arp_table);
13308 			ifp->phnd_arp_table = NULL;
13309 #endif /* DHD_L2_FILTER */
13310 
13311 
13312 			dhd_if_del_sta_list(ifp);
13313 
13314 			MFREE(dhd->pub.osh, ifp, sizeof(*ifp));
13315 			dhd->iflist[0] = NULL;
13316 		}
13317 	}
13318 
13319 	/* Clear the watchdog timer */
13320 	DHD_GENERAL_LOCK(&dhd->pub, flags);
13321 	timer_valid = dhd->wd_timer_valid;
13322 	dhd->wd_timer_valid = FALSE;
13323 	DHD_GENERAL_UNLOCK(&dhd->pub, flags);
13324 	if (timer_valid)
13325 		del_timer_sync(&dhd->timer);
13326 	DHD_DISABLE_RUNTIME_PM(&dhd->pub);
13327 
13328 #ifdef BCMDBUS
13329 	tasklet_kill(&dhd->tasklet);
13330 #else
13331 	if (dhd->dhd_state & DHD_ATTACH_STATE_THREADS_CREATED) {
13332 #ifdef DHD_PCIE_RUNTIMEPM
13333 		if (dhd->thr_rpm_ctl.thr_pid >= 0) {
13334 			PROC_STOP(&dhd->thr_rpm_ctl);
13335 		}
13336 #endif /* DHD_PCIE_RUNTIMEPM */
13337 		if (dhd->thr_wdt_ctl.thr_pid >= 0) {
13338 			PROC_STOP(&dhd->thr_wdt_ctl);
13339 		}
13340 
13341 		if (dhd->rxthread_enabled && dhd->thr_rxf_ctl.thr_pid >= 0) {
13342 			PROC_STOP(&dhd->thr_rxf_ctl);
13343 		}
13344 
13345 		if (dhd->thr_dpc_ctl.thr_pid >= 0) {
13346 			PROC_STOP(&dhd->thr_dpc_ctl);
13347 		} else
13348 		{
13349 			tasklet_kill(&dhd->tasklet);
13350 		}
13351 	}
13352 #endif /* BCMDBUS */
13353 
13354 #ifdef DHD_LB
13355 	if (dhd->dhd_state & DHD_ATTACH_STATE_LB_ATTACH_DONE) {
13356 		/* Clear the flag first to avoid calling the cpu notifier */
13357 		dhd->dhd_state &= ~DHD_ATTACH_STATE_LB_ATTACH_DONE;
13358 
13359 		/* Kill the Load Balancing Tasklets */
13360 #ifdef DHD_LB_RXP
13361 		cancel_work_sync(&dhd->rx_napi_dispatcher_work);
13362 		__skb_queue_purge(&dhd->rx_pend_queue);
13363 #endif /* DHD_LB_RXP */
13364 #ifdef DHD_LB_TXP
13365 		cancel_work_sync(&dhd->tx_dispatcher_work);
13366 		tasklet_kill(&dhd->tx_tasklet);
13367 		__skb_queue_purge(&dhd->tx_pend_queue);
13368 #endif /* DHD_LB_TXP */
13369 #ifdef DHD_LB_TXC
13370 		cancel_work_sync(&dhd->tx_compl_dispatcher_work);
13371 		tasklet_kill(&dhd->tx_compl_tasklet);
13372 #endif /* DHD_LB_TXC */
13373 #ifdef DHD_LB_RXC
13374 		tasklet_kill(&dhd->rx_compl_tasklet);
13375 #endif /* DHD_LB_RXC */
13376 
13377 		if (dhd->cpu_notifier.notifier_call != NULL) {
13378 			unregister_cpu_notifier(&dhd->cpu_notifier);
13379 		}
13380 		dhd_cpumasks_deinit(dhd);
13381 		DHD_LB_STATS_DEINIT(&dhd->pub);
13382 	}
13383 #endif /* DHD_LB */
13384 
13385 #ifdef CSI_SUPPORT
13386 	dhd_csi_deinit(dhdp);
13387 #endif /* CSI_SUPPORT */
13388 
13389 	DHD_SSSR_MEMPOOL_DEINIT(&dhd->pub);
13390 
13391 #ifdef DHD_LOG_DUMP
13392 	dhd_log_dump_deinit(&dhd->pub);
13393 #endif /* DHD_LOG_DUMP */
13394 #ifdef WL_CFG80211
13395 	if (dhd->dhd_state & DHD_ATTACH_STATE_CFG80211) {
13396 		if (!cfg) {
13397 			DHD_ERROR(("cfg NULL!\n"));
13398 			ASSERT(0);
13399 		} else {
13400 			wl_cfg80211_detach(cfg);
13401 			dhd_monitor_uninit();
13402 		}
13403 	}
13404 #endif
13405 
13406 #ifdef DEBUGABILITY
13407 	if (dhdp->dbg) {
13408 #ifdef DBG_PKT_MON
13409 		dhd_os_dbg_detach_pkt_monitor(dhdp);
13410 		dhd_os_spin_lock_deinit(dhd->pub.osh, dhd->pub.dbg->pkt_mon_lock);
13411 #endif /* DBG_PKT_MON */
13412 		dhd_os_dbg_detach(dhdp);
13413 	}
13414 #endif /* DEBUGABILITY */
13415 #ifdef SHOW_LOGTRACE
13416 #ifdef DHD_PKT_LOGGING
13417 	dhd_os_detach_pktlog(dhdp);
13418 #endif /* DHD_PKT_LOGGING */
13419 	/* Release the skbs from queue for WLC_E_TRACE event */
13420 	dhd_event_logtrace_flush_queue(dhdp);
13421 
13422 	if (dhd->dhd_state & DHD_ATTACH_LOGTRACE_INIT) {
13423 		if (dhd->event_data.fmts) {
13424 			MFREE(dhd->pub.osh, dhd->event_data.fmts,
13425 					dhd->event_data.fmts_size);
13426 			dhd->event_data.fmts = NULL;
13427 		}
13428 		if (dhd->event_data.raw_fmts) {
13429 			MFREE(dhd->pub.osh, dhd->event_data.raw_fmts,
13430 					dhd->event_data.raw_fmts_size);
13431 			dhd->event_data.raw_fmts = NULL;
13432 		}
13433 		if (dhd->event_data.raw_sstr) {
13434 			MFREE(dhd->pub.osh, dhd->event_data.raw_sstr,
13435 					dhd->event_data.raw_sstr_size);
13436 			dhd->event_data.raw_sstr = NULL;
13437 		}
13438 		if (dhd->event_data.rom_raw_sstr) {
13439 			MFREE(dhd->pub.osh, dhd->event_data.rom_raw_sstr,
13440 					dhd->event_data.rom_raw_sstr_size);
13441 			dhd->event_data.rom_raw_sstr = NULL;
13442 		}
13443 		dhd->dhd_state &= ~DHD_ATTACH_LOGTRACE_INIT;
13444 	}
13445 #endif /* SHOW_LOGTRACE */
13446 #ifdef BCMPCIE
13447 	if (dhdp->extended_trap_data)
13448 	{
13449 		MFREE(dhdp->osh, dhdp->extended_trap_data, BCMPCIE_EXT_TRAP_DATA_MAXLEN);
13450 		dhdp->extended_trap_data = NULL;
13451 	}
13452 #endif /* BCMPCIE */
13453 #ifdef PNO_SUPPORT
13454 	if (dhdp->pno_state)
13455 		dhd_pno_deinit(dhdp);
13456 #endif
13457 #ifdef RTT_SUPPORT
13458 	if (dhdp->rtt_state) {
13459 		dhd_rtt_deinit(dhdp);
13460 	}
13461 #endif
13462 #if defined(CONFIG_PM_SLEEP)
13463 	if (dhd_pm_notifier_registered) {
13464 		unregister_pm_notifier(&dhd->pm_notifier);
13465 		dhd_pm_notifier_registered = FALSE;
13466 	}
13467 #endif /* CONFIG_PM_SLEEP */
13468 
13469 #ifdef DEBUG_CPU_FREQ
13470 		if (dhd->new_freq)
13471 			free_percpu(dhd->new_freq);
13472 		dhd->new_freq = NULL;
13473 		cpufreq_unregister_notifier(&dhd->freq_trans, CPUFREQ_TRANSITION_NOTIFIER);
13474 #endif
13475 	DHD_TRACE(("wd wakelock count:%d\n", dhd->wakelock_wd_counter));
13476 #ifdef CONFIG_HAS_WAKELOCK
13477 	dhd->wakelock_wd_counter = 0;
13478 	wake_lock_destroy(&dhd->wl_wdwake);
13479 	// terence 20161023: can not destroy wl_wifi when wlan down, it will happen null pointer in dhd_ioctl_entry
13480 	wake_lock_destroy(&dhd->wl_wifi);
13481 #endif /* CONFIG_HAS_WAKELOCK */
13482 	if (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT) {
13483 		DHD_OS_WAKE_LOCK_DESTROY(dhd);
13484 	}
13485 
13486 
13487 
13488 #ifdef DHDTCPACK_SUPPRESS
13489 	/* This will free all MEM allocated for TCPACK SUPPRESS */
13490 	dhd_tcpack_suppress_set(&dhd->pub, TCPACK_SUP_OFF);
13491 #endif /* DHDTCPACK_SUPPRESS */
13492 
13493 #ifdef PCIE_FULL_DONGLE
13494 	dhd_flow_rings_deinit(dhdp);
13495 	if (dhdp->prot)
13496 		dhd_prot_detach(dhdp);
13497 #endif
13498 
13499 #if defined(WLTDLS) && defined(PCIE_FULL_DONGLE)
13500 		dhd_free_tdls_peer_list(dhdp);
13501 #endif
13502 
13503 #ifdef HOFFLOAD_MODULES
13504 	hmem = &dhdp->hmem;
13505 	dhd_free_module_memory(dhdp->bus, hmem);
13506 #endif /* HOFFLOAD_MODULES */
13507 #if defined(BT_OVER_SDIO)
13508 	mutex_destroy(&dhd->bus_user_lock);
13509 #endif /* BT_OVER_SDIO */
13510 #ifdef DUMP_IOCTL_IOV_LIST
13511 	dhd_iov_li_delete(dhdp, &(dhdp->dump_iovlist_head));
13512 #endif /* DUMP_IOCTL_IOV_LIST */
13513 #ifdef DHD_DEBUG
13514 	/* memory waste feature list initilization */
13515 	dhd_mw_list_delete(dhdp, &(dhdp->mw_list_head));
13516 #endif /* DHD_DEBUG */
13517 #ifdef WL_MONITOR
13518 	dhd_del_monitor_if(dhd, NULL, DHD_WQ_WORK_IF_DEL);
13519 #endif /* WL_MONITOR */
13520 
13521 	/* Prefer adding de-init code above this comment unless necessary.
13522 	 * The idea is to cancel work queue, sysfs and flags at the end.
13523 	 */
13524 	dhd_deferred_work_deinit(dhd->dhd_deferred_wq);
13525 	dhd->dhd_deferred_wq = NULL;
13526 
13527 #ifdef SHOW_LOGTRACE
13528 	/* Wait till event_log_dispatcher_work finishes */
13529 	cancel_work_sync(&dhd->event_log_dispatcher_work);
13530 #endif /* SHOW_LOGTRACE */
13531 
13532 	dhd_sysfs_exit(dhd);
13533 	dhd->pub.fw_download_done = FALSE;
13534 	dhd_conf_detach(dhdp);
13535 }
13536 
13537 
13538 void
dhd_free(dhd_pub_t * dhdp)13539 dhd_free(dhd_pub_t *dhdp)
13540 {
13541 	dhd_info_t *dhd;
13542 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
13543 
13544 	if (dhdp) {
13545 		int i;
13546 		for (i = 0; i < ARRAYSIZE(dhdp->reorder_bufs); i++) {
13547 			if (dhdp->reorder_bufs[i]) {
13548 				reorder_info_t *ptr;
13549 				uint32 buf_size = sizeof(struct reorder_info);
13550 
13551 				ptr = dhdp->reorder_bufs[i];
13552 
13553 				buf_size += ((ptr->max_idx + 1) * sizeof(void*));
13554 				DHD_REORDER(("free flow id buf %d, maxidx is %d, buf_size %d\n",
13555 					i, ptr->max_idx, buf_size));
13556 
13557 				MFREE(dhdp->osh, dhdp->reorder_bufs[i], buf_size);
13558 				dhdp->reorder_bufs[i] = NULL;
13559 			}
13560 		}
13561 
13562 		dhd_sta_pool_fini(dhdp, DHD_MAX_STA);
13563 
13564 		dhd = (dhd_info_t *)dhdp->info;
13565 		if (dhdp->soc_ram) {
13566 #if defined(CONFIG_DHD_USE_STATIC_BUF) && defined(DHD_USE_STATIC_MEMDUMP)
13567 			DHD_OS_PREFREE(dhdp, dhdp->soc_ram, dhdp->soc_ram_length);
13568 #else
13569 			MFREE(dhdp->osh, dhdp->soc_ram, dhdp->soc_ram_length);
13570 #endif /* CONFIG_DHD_USE_STATIC_BUF && DHD_USE_STATIC_MEMDUMP */
13571 			dhdp->soc_ram = NULL;
13572 		}
13573 #ifdef CACHE_FW_IMAGES
13574 		if (dhdp->cached_fw) {
13575 			MFREE(dhdp->osh, dhdp->cached_fw, dhdp->bus->ramsize);
13576 			dhdp->cached_fw = NULL;
13577 		}
13578 
13579 		if (dhdp->cached_nvram) {
13580 			MFREE(dhdp->osh, dhdp->cached_nvram, MAX_NVRAMBUF_SIZE);
13581 			dhdp->cached_nvram = NULL;
13582 		}
13583 #endif
13584 		if (dhd) {
13585 #ifdef REPORT_FATAL_TIMEOUTS
13586 			deinit_dhd_timeouts(&dhd->pub);
13587 #endif /* REPORT_FATAL_TIMEOUTS */
13588 
13589 			/* If pointer is allocated by dhd_os_prealloc then avoid MFREE */
13590 			if (dhd != (dhd_info_t *)dhd_os_prealloc(dhdp,
13591 					DHD_PREALLOC_DHD_INFO, 0, FALSE))
13592 				MFREE(dhd->pub.osh, dhd, sizeof(*dhd));
13593 			dhd = NULL;
13594 		}
13595 	}
13596 }
13597 
13598 void
dhd_clear(dhd_pub_t * dhdp)13599 dhd_clear(dhd_pub_t *dhdp)
13600 {
13601 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
13602 
13603 	if (dhdp) {
13604 		int i;
13605 #ifdef DHDTCPACK_SUPPRESS
13606 		/* Clean up timer/data structure for any remaining/pending packet or timer. */
13607 		dhd_tcpack_info_tbl_clean(dhdp);
13608 #endif /* DHDTCPACK_SUPPRESS */
13609 		for (i = 0; i < ARRAYSIZE(dhdp->reorder_bufs); i++) {
13610 			if (dhdp->reorder_bufs[i]) {
13611 				reorder_info_t *ptr;
13612 				uint32 buf_size = sizeof(struct reorder_info);
13613 
13614 				ptr = dhdp->reorder_bufs[i];
13615 
13616 				buf_size += ((ptr->max_idx + 1) * sizeof(void*));
13617 				DHD_REORDER(("free flow id buf %d, maxidx is %d, buf_size %d\n",
13618 					i, ptr->max_idx, buf_size));
13619 
13620 				MFREE(dhdp->osh, dhdp->reorder_bufs[i], buf_size);
13621 				dhdp->reorder_bufs[i] = NULL;
13622 			}
13623 		}
13624 
13625 		dhd_sta_pool_clear(dhdp, DHD_MAX_STA);
13626 
13627 		if (dhdp->soc_ram) {
13628 #if defined(CONFIG_DHD_USE_STATIC_BUF) && defined(DHD_USE_STATIC_MEMDUMP)
13629 			DHD_OS_PREFREE(dhdp, dhdp->soc_ram, dhdp->soc_ram_length);
13630 #else
13631 			MFREE(dhdp->osh, dhdp->soc_ram, dhdp->soc_ram_length);
13632 #endif /* CONFIG_DHD_USE_STATIC_BUF && DHD_USE_STATIC_MEMDUMP */
13633 			dhdp->soc_ram = NULL;
13634 		}
13635 	}
13636 }
13637 
13638 static void
dhd_module_cleanup(void)13639 dhd_module_cleanup(void)
13640 {
13641 	printf("%s: Enter\n", __FUNCTION__);
13642 
13643 	dhd_bus_unregister();
13644 
13645 	wl_android_exit();
13646 
13647 	dhd_wifi_platform_unregister_drv();
13648 	printf("%s: Exit\n", __FUNCTION__);
13649 }
13650 
13651 static void
dhd_module_exit(void)13652 dhd_module_exit(void)
13653 {
13654 	atomic_set(&exit_in_progress, 1);
13655 	dhd_module_cleanup();
13656 	unregister_reboot_notifier(&dhd_reboot_notifier);
13657 	dhd_destroy_to_notifier_skt();
13658 }
13659 
13660 static int
dhd_module_init(void)13661 dhd_module_init(void)
13662 {
13663 	int err;
13664 	int retry = 0;
13665 
13666 	printf("%s: in %s\n", __FUNCTION__, dhd_version);
13667 
13668 	DHD_PERIM_RADIO_INIT();
13669 
13670 
13671 	if (firmware_path[0] != '\0') {
13672 		strncpy(fw_bak_path, firmware_path, MOD_PARAM_PATHLEN);
13673 		fw_bak_path[MOD_PARAM_PATHLEN-1] = '\0';
13674 	}
13675 
13676 	if (nvram_path[0] != '\0') {
13677 		strncpy(nv_bak_path, nvram_path, MOD_PARAM_PATHLEN);
13678 		nv_bak_path[MOD_PARAM_PATHLEN-1] = '\0';
13679 	}
13680 
13681 	do {
13682 		err = dhd_wifi_platform_register_drv();
13683 		if (!err) {
13684 			register_reboot_notifier(&dhd_reboot_notifier);
13685 			break;
13686 		} else {
13687 			DHD_ERROR(("%s: Failed to load the driver, try cnt %d\n",
13688 				__FUNCTION__, retry));
13689 			strncpy(firmware_path, fw_bak_path, MOD_PARAM_PATHLEN);
13690 			firmware_path[MOD_PARAM_PATHLEN-1] = '\0';
13691 			strncpy(nvram_path, nv_bak_path, MOD_PARAM_PATHLEN);
13692 			nvram_path[MOD_PARAM_PATHLEN-1] = '\0';
13693 		}
13694 	} while (retry--);
13695 
13696 	dhd_create_to_notifier_skt();
13697 
13698 	if (err) {
13699 		DHD_ERROR(("%s: Failed to load driver max retry reached**\n", __FUNCTION__));
13700 	} else {
13701 		if (!dhd_download_fw_on_driverload) {
13702 			dhd_driver_init_done = TRUE;
13703 		}
13704 	}
13705 
13706 	printf("%s: Exit err=%d\n", __FUNCTION__, err);
13707 	return err;
13708 }
13709 
13710 static int
dhd_reboot_callback(struct notifier_block * this,unsigned long code,void * unused)13711 dhd_reboot_callback(struct notifier_block *this, unsigned long code, void *unused)
13712 {
13713 	DHD_TRACE(("%s: code = %ld\n", __FUNCTION__, code));
13714 	if (code == SYS_RESTART) {
13715 #ifdef BCMPCIE
13716 		is_reboot = code;
13717 #endif /* BCMPCIE */
13718 	}
13719 	return NOTIFY_DONE;
13720 }
13721 
13722 //rk
13723 /* The debugfs functions are optimized away when CONFIG_DEBUG_FS isn't set. */
13724 //static char tcp_keepalive_param[200];
13725 static char sabuf[20]="", dabuf[20]="";
13726 static char seabuf[ETHER_ADDR_STR_LEN]="";
13727 static char deabuf[ETHER_ADDR_STR_LEN]="";
13728 static uint16 source, dest, window, ip_id;
13729 static uint32 seq = 0, seq_ack = 0, tcp_option_len = 0, tcp_data_len = 0, tsval = 0, tsecr = 0;
13730 #include <linux/hrtimer.h>
13731 #include <linux/time.h>
13732 #include <net/tcp.h>
13733 #include <linux/proc_fs.h>
13734 
tcp_param_show(struct seq_file * s,void * data)13735 static int tcp_param_show(struct seq_file *s, void *data)
13736 {
13737 	seq_printf(s, "dhd_priv wl tcpka_conn_add 1 %s %s %s 1 %d %d 1 1 1 1 1 2 0xc000\n",
13738 			deabuf, sabuf, dabuf, source, dest);
13739 
13740 	return 0;
13741 }
13742 
13743 #ifndef CONFIG_PROC_FS
13744 DEFINE_SHOW_ATTRIBUTE(tcp_param);
13745 #endif
13746 
rk_bcm_add_tcp_keepalive_debugfs(void)13747 static void rk_bcm_add_tcp_keepalive_debugfs(void)
13748 {
13749 #ifdef CONFIG_PROC_FS
13750 	proc_create_single("tcp_params", 0, NULL, tcp_param_show);
13751 #else
13752 	struct dentry *root;
13753 
13754 	root = debugfs_create_dir("tcp_keepalive_param", NULL);
13755 	if (IS_ERR(root))
13756 		/* Don't complain -- debugfs just isn't enabled */
13757 		return;
13758 	if (!root)
13759 		/* Complain -- debugfs is enabled, but it failed to
13760 		 * create the directory. */
13761 		goto err_root;
13762 
13763 	if (!debugfs_create_file("tcp_param", S_IRUSR, root, NULL, &tcp_param_fops))
13764 		goto err_node;
13765 
13766 	return;
13767 
13768 err_node:
13769 	debugfs_remove_recursive(root);
13770 err_root:
13771 	pr_err("failed to initialize keepalive debugfs\n");
13772 #endif
13773 }
13774 
wifi_init_thread(void * data)13775 static int wifi_init_thread(void *data)
13776 {
13777 	rk_bcm_add_tcp_keepalive_debugfs();
13778 	dhd_module_init();
13779 
13780 	return 0;
13781 }
13782 
rockchip_wifi_init_module_rkwifi(void)13783 int rockchip_wifi_init_module_rkwifi(void)
13784 {
13785 	struct task_struct *kthread = NULL;
13786 
13787 	kthread = kthread_run(wifi_init_thread, NULL, "wifi_init_thread");
13788 	if (IS_ERR(kthread))
13789 		pr_err("create wifi_init_thread failed.\n");
13790 
13791 	return 0;
13792 }
13793 
rockchip_wifi_exit_module_rkwifi(void)13794 void rockchip_wifi_exit_module_rkwifi(void)
13795 {
13796 	dhd_module_exit();
13797 }
13798 #ifdef CONFIG_WIFI_BUILD_MODULE
13799 module_init(rockchip_wifi_init_module_rkwifi);
13800 module_exit(rockchip_wifi_exit_module_rkwifi);
13801 #else
13802 #ifdef CONFIG_WIFI_LOAD_DRIVER_WHEN_KERNEL_BOOTUP
13803 late_initcall(rockchip_wifi_init_module_rkwifi);
13804 module_exit(rockchip_wifi_exit_module_rkwifi);
13805 #else
13806 module_init(rockchip_wifi_init_module_rkwifi);
13807 module_exit(rockchip_wifi_exit_module_rkwifi);
13808 #endif
13809 #endif
13810 #if 0
13811 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
13812 #if defined(CONFIG_DEFERRED_INITCALLS) && !defined(EXYNOS_PCIE_MODULE_PATCH)
13813 #if defined(CONFIG_MACH_UNIVERSAL7420) || defined(CONFIG_SOC_EXYNOS8890) || \
13814 	defined(CONFIG_ARCH_MSM8996) || defined(CONFIG_SOC_EXYNOS8895) || \
13815 	defined(CONFIG_ARCH_MSM8998)
13816 deferred_module_init_sync(dhd_module_init);
13817 #else
13818 deferred_module_init(dhd_module_init);
13819 #endif /* CONFIG_MACH_UNIVERSAL7420 || CONFIG_SOC_EXYNOS8890 ||
13820 	* CONFIG_ARCH_MSM8996 || CONFIG_SOC_EXYNOS8895 || CONFIG_ARCH_MSM8998
13821 	*/
13822 #elif defined(USE_LATE_INITCALL_SYNC)
13823 late_initcall_sync(dhd_module_init);
13824 #else
13825 late_initcall(dhd_module_init);
13826 #endif /* USE_LATE_INITCALL_SYNC */
13827 #else
13828 module_init(dhd_module_init);
13829 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) */
13830 
13831 module_exit(dhd_module_exit);
13832 
13833 #endif
13834 /*
13835  * OS specific functions required to implement DHD driver in OS independent way
13836  */
13837 int
dhd_os_proto_block(dhd_pub_t * pub)13838 dhd_os_proto_block(dhd_pub_t *pub)
13839 {
13840 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
13841 
13842 	if (dhd) {
13843 		DHD_PERIM_UNLOCK(pub);
13844 
13845 		down(&dhd->proto_sem);
13846 
13847 		DHD_PERIM_LOCK(pub);
13848 		return 1;
13849 	}
13850 
13851 	return 0;
13852 }
13853 
13854 int
dhd_os_proto_unblock(dhd_pub_t * pub)13855 dhd_os_proto_unblock(dhd_pub_t *pub)
13856 {
13857 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
13858 
13859 	if (dhd) {
13860 		up(&dhd->proto_sem);
13861 		return 1;
13862 	}
13863 
13864 	return 0;
13865 }
13866 
13867 void
dhd_os_dhdiovar_lock(dhd_pub_t * pub)13868 dhd_os_dhdiovar_lock(dhd_pub_t *pub)
13869 {
13870 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
13871 
13872 	if (dhd) {
13873 		mutex_lock(&dhd->dhd_iovar_mutex);
13874 	}
13875 }
13876 
13877 void
dhd_os_dhdiovar_unlock(dhd_pub_t * pub)13878 dhd_os_dhdiovar_unlock(dhd_pub_t *pub)
13879 {
13880 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
13881 
13882 	if (dhd) {
13883 		mutex_unlock(&dhd->dhd_iovar_mutex);
13884 	}
13885 }
13886 
13887 unsigned int
dhd_os_get_ioctl_resp_timeout(void)13888 dhd_os_get_ioctl_resp_timeout(void)
13889 {
13890 	return ((unsigned int)dhd_ioctl_timeout_msec);
13891 }
13892 
13893 void
dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec)13894 dhd_os_set_ioctl_resp_timeout(unsigned int timeout_msec)
13895 {
13896 	dhd_ioctl_timeout_msec = (int)timeout_msec;
13897 }
13898 
13899 int
dhd_os_ioctl_resp_wait(dhd_pub_t * pub,uint * condition)13900 dhd_os_ioctl_resp_wait(dhd_pub_t *pub, uint *condition)
13901 {
13902 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
13903 	int timeout;
13904 
13905 	/* Convert timeout in millsecond to jiffies */
13906 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
13907 	timeout = msecs_to_jiffies(dhd_ioctl_timeout_msec);
13908 #else
13909 	timeout = dhd_ioctl_timeout_msec * HZ / 1000;
13910 #endif
13911 
13912 	DHD_PERIM_UNLOCK(pub);
13913 
13914 	timeout = wait_event_timeout(dhd->ioctl_resp_wait, (*condition), timeout);
13915 
13916 	DHD_PERIM_LOCK(pub);
13917 
13918 	return timeout;
13919 }
13920 
13921 int
dhd_os_ioctl_resp_wake(dhd_pub_t * pub)13922 dhd_os_ioctl_resp_wake(dhd_pub_t *pub)
13923 {
13924 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
13925 
13926 	wake_up(&dhd->ioctl_resp_wait);
13927 	return 0;
13928 }
13929 
13930 int
dhd_os_d3ack_wait(dhd_pub_t * pub,uint * condition)13931 dhd_os_d3ack_wait(dhd_pub_t *pub, uint *condition)
13932 {
13933 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
13934 	int timeout;
13935 
13936 	/* Convert timeout in millsecond to jiffies */
13937 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
13938 	timeout = msecs_to_jiffies(dhd_ioctl_timeout_msec);
13939 #else
13940 	timeout = dhd_ioctl_timeout_msec * HZ / 1000;
13941 #endif
13942 
13943 	DHD_PERIM_UNLOCK(pub);
13944 
13945 	timeout = wait_event_timeout(dhd->d3ack_wait, (*condition), timeout);
13946 
13947 	DHD_PERIM_LOCK(pub);
13948 
13949 	return timeout;
13950 }
13951 
13952 #ifdef PCIE_INB_DW
13953 int
dhd_os_ds_exit_wait(dhd_pub_t * pub,uint * condition)13954 dhd_os_ds_exit_wait(dhd_pub_t *pub, uint *condition)
13955 {
13956 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
13957 	int timeout;
13958 
13959 	/* Convert timeout in millsecond to jiffies */
13960 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
13961 	timeout = msecs_to_jiffies(ds_exit_timeout_msec);
13962 #else
13963 	timeout = ds_exit_timeout_msec * HZ / 1000;
13964 #endif
13965 
13966 	DHD_PERIM_UNLOCK(pub);
13967 
13968 	timeout = wait_event_timeout(dhd->ds_exit_wait, (*condition), timeout);
13969 
13970 	DHD_PERIM_LOCK(pub);
13971 
13972 	return timeout;
13973 }
13974 
13975 int
dhd_os_ds_exit_wake(dhd_pub_t * pub)13976 dhd_os_ds_exit_wake(dhd_pub_t *pub)
13977 {
13978 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
13979 
13980 	wake_up(&dhd->ds_exit_wait);
13981 	return 0;
13982 }
13983 
13984 #endif /* PCIE_INB_DW */
13985 
13986 int
dhd_os_d3ack_wake(dhd_pub_t * pub)13987 dhd_os_d3ack_wake(dhd_pub_t *pub)
13988 {
13989 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
13990 
13991 	wake_up(&dhd->d3ack_wait);
13992 	return 0;
13993 }
13994 
13995 int
dhd_os_busbusy_wait_negation(dhd_pub_t * pub,uint * condition)13996 dhd_os_busbusy_wait_negation(dhd_pub_t *pub, uint *condition)
13997 {
13998 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
13999 	int timeout;
14000 
14001 	/* Wait for bus usage contexts to gracefully exit within some timeout value
14002 	 * Set time out to little higher than dhd_ioctl_timeout_msec,
14003 	 * so that IOCTL timeout should not get affected.
14004 	 */
14005 	/* Convert timeout in millsecond to jiffies */
14006 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
14007 	timeout = msecs_to_jiffies(DHD_BUS_BUSY_TIMEOUT);
14008 #else
14009 	timeout = DHD_BUS_BUSY_TIMEOUT * HZ / 1000;
14010 #endif
14011 
14012 	timeout = wait_event_timeout(dhd->dhd_bus_busy_state_wait, !(*condition), timeout);
14013 
14014 	return timeout;
14015 }
14016 
14017 /*
14018  * Wait until the condition *var == condition is met.
14019  * Returns 0 if the @condition evaluated to false after the timeout elapsed
14020  * Returns 1 if the @condition evaluated to true
14021  */
14022 int
dhd_os_busbusy_wait_condition(dhd_pub_t * pub,uint * var,uint condition)14023 dhd_os_busbusy_wait_condition(dhd_pub_t *pub, uint *var, uint condition)
14024 {
14025 	dhd_info_t * dhd = (dhd_info_t *)(pub->info);
14026 	int timeout;
14027 
14028 	/* Convert timeout in millsecond to jiffies */
14029 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
14030 	timeout = msecs_to_jiffies(DHD_BUS_BUSY_TIMEOUT);
14031 #else
14032 	timeout = DHD_BUS_BUSY_TIMEOUT * HZ / 1000;
14033 #endif
14034 
14035 	timeout = wait_event_timeout(dhd->dhd_bus_busy_state_wait, (*var == condition), timeout);
14036 
14037 	return timeout;
14038 }
14039 
14040 
14041 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36))
14042 /* Fix compilation error for FC11 */
14043 INLINE
14044 #endif
14045 int
dhd_os_busbusy_wake(dhd_pub_t * pub)14046 dhd_os_busbusy_wake(dhd_pub_t *pub)
14047 {
14048 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
14049 	/* Call wmb() to make sure before waking up the other event value gets updated */
14050 	OSL_SMP_WMB();
14051 	wake_up(&dhd->dhd_bus_busy_state_wait);
14052 	return 0;
14053 }
14054 
14055 void
dhd_os_wd_timer_extend(void * bus,bool extend)14056 dhd_os_wd_timer_extend(void *bus, bool extend)
14057 {
14058 #ifndef BCMDBUS
14059 	dhd_pub_t *pub = bus;
14060 	dhd_info_t *dhd = (dhd_info_t *)pub->info;
14061 
14062 	if (extend)
14063 		dhd_os_wd_timer(bus, WATCHDOG_EXTEND_INTERVAL);
14064 	else
14065 		dhd_os_wd_timer(bus, dhd->default_wd_interval);
14066 #endif /* !BCMDBUS */
14067 }
14068 
14069 
14070 void
dhd_os_wd_timer(void * bus,uint wdtick)14071 dhd_os_wd_timer(void *bus, uint wdtick)
14072 {
14073 #ifndef BCMDBUS
14074 	dhd_pub_t *pub = bus;
14075 	dhd_info_t *dhd = (dhd_info_t *)pub->info;
14076 	unsigned long flags;
14077 
14078 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
14079 
14080 	if (!dhd) {
14081 		DHD_ERROR(("%s: dhd NULL\n", __FUNCTION__));
14082 		return;
14083 	}
14084 
14085 	DHD_GENERAL_LOCK(pub, flags);
14086 
14087 	/* don't start the wd until fw is loaded */
14088 	if (pub->busstate == DHD_BUS_DOWN) {
14089 		DHD_GENERAL_UNLOCK(pub, flags);
14090 		return;
14091 	}
14092 
14093 	/* Totally stop the timer */
14094 	if (!wdtick && dhd->wd_timer_valid == TRUE) {
14095 		dhd->wd_timer_valid = FALSE;
14096 		DHD_GENERAL_UNLOCK(pub, flags);
14097 		del_timer_sync(&dhd->timer);
14098 		return;
14099 	}
14100 
14101 	if (wdtick) {
14102 		dhd_watchdog_ms = (uint)wdtick;
14103 		/* Re arm the timer, at last watchdog period */
14104 		mod_timer(&dhd->timer, jiffies + msecs_to_jiffies(dhd_watchdog_ms));
14105 		dhd->wd_timer_valid = TRUE;
14106 	}
14107 	DHD_GENERAL_UNLOCK(pub, flags);
14108 #endif /* !BCMDBUS */
14109 }
14110 
14111 #ifdef DHD_PCIE_RUNTIMEPM
14112 void
dhd_os_runtimepm_timer(void * bus,uint tick)14113 dhd_os_runtimepm_timer(void *bus, uint tick)
14114 {
14115 	dhd_pub_t *pub = bus;
14116 	dhd_info_t *dhd = (dhd_info_t *)pub->info;
14117 	unsigned long flags;
14118 
14119 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
14120 
14121 	if (!dhd) {
14122 		DHD_ERROR(("%s: dhd is NULL\n", __FUNCTION__));
14123 		return;
14124 	}
14125 
14126 	DHD_GENERAL_LOCK(pub, flags);
14127 
14128 	/* don't start the RPM until fw is loaded */
14129 	if (DHD_BUS_CHECK_DOWN_OR_DOWN_IN_PROGRESS(pub)) {
14130 		DHD_GENERAL_UNLOCK(pub, flags);
14131 		return;
14132 	}
14133 
14134 	/* If tick is non-zero, the request is to start the timer */
14135 	if (tick) {
14136 		/* Start the timer only if its not already running */
14137 		if (dhd->rpm_timer_valid == FALSE) {
14138 			mod_timer(&dhd->rpm_timer, jiffies + msecs_to_jiffies(dhd_runtimepm_ms));
14139 			dhd->rpm_timer_valid = TRUE;
14140 		}
14141 	} else {
14142 		/* tick is zero, we have to stop the timer */
14143 		/* Stop the timer only if its running, otherwise we don't have to do anything */
14144 		if (dhd->rpm_timer_valid == TRUE) {
14145 			dhd->rpm_timer_valid = FALSE;
14146 			DHD_GENERAL_UNLOCK(pub, flags);
14147 			del_timer_sync(&dhd->rpm_timer);
14148 			/* we have already released the lock, so just go to exit */
14149 			goto exit;
14150 		}
14151 	}
14152 
14153 	DHD_GENERAL_UNLOCK(pub, flags);
14154 exit:
14155 	return;
14156 
14157 }
14158 
14159 #endif /* DHD_PCIE_RUNTIMEPM */
14160 
14161 void *
dhd_os_open_image(char * filename)14162 dhd_os_open_image(char *filename)
14163 {
14164 	struct file *fp;
14165 	int size;
14166 
14167 	fp = filp_open(filename, O_RDONLY, 0);
14168 	/*
14169 	 * 2.6.11 (FC4) supports filp_open() but later revs don't?
14170 	 * Alternative:
14171 	 * fp = open_namei(AT_FDCWD, filename, O_RD, 0);
14172 	 * ???
14173 	 */
14174 	 if (IS_ERR(fp)) {
14175 		 fp = NULL;
14176 		 goto err;
14177 	 }
14178 
14179 	 if (!S_ISREG(file_inode(fp)->i_mode)) {
14180 		 DHD_ERROR(("%s: %s is not regular file\n", __FUNCTION__, filename));
14181 		 fp = NULL;
14182 		 goto err;
14183 	 }
14184 
14185 	 size = i_size_read(file_inode(fp));
14186 	 if (size <= 0) {
14187 		 DHD_ERROR(("%s: %s file size invalid %d\n", __FUNCTION__, filename, size));
14188 		 fp = NULL;
14189 		 goto err;
14190 	 }
14191 
14192 	 DHD_PRINT("%s: %s (%d bytes) open success\n", __FUNCTION__, filename, size);
14193 
14194 err:
14195 	 return fp;
14196 }
14197 
14198 int
dhd_os_get_image_block(char * buf,int len,void * image)14199 dhd_os_get_image_block(char *buf, int len, void *image)
14200 {
14201 	struct file *fp = (struct file *)image;
14202 	int rdlen;
14203 	int size;
14204 
14205 	if (!image) {
14206 		return 0;
14207 	}
14208 
14209 	size = i_size_read(file_inode(fp));
14210 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
14211 	rdlen = kernel_read(fp, buf, MIN(len, size), &fp->f_pos);
14212 #else
14213 	rdlen = kernel_read(fp, fp->f_pos, buf, MIN(len, size));
14214 #endif
14215 
14216 	if (len >= size && size != rdlen) {
14217 		return -EIO;
14218 	}
14219 
14220 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
14221 	if (rdlen > 0) {
14222 		fp->f_pos += rdlen;
14223 	}
14224 #endif
14225 
14226 	return rdlen;
14227 }
14228 
14229 int
dhd_os_get_image_size(void * image)14230 dhd_os_get_image_size(void *image)
14231 {
14232 	struct file *fp = (struct file *)image;
14233 	int size;
14234 	if (!image) {
14235 		return 0;
14236 	}
14237 
14238 	size = i_size_read(file_inode(fp));
14239 
14240 	return size;
14241 }
14242 
14243 #if defined(BT_OVER_SDIO)
14244 int
dhd_os_gets_image(dhd_pub_t * pub,char * str,int len,void * image)14245 dhd_os_gets_image(dhd_pub_t *pub, char *str, int len, void *image)
14246 {
14247 	struct file *fp = (struct file *)image;
14248 	int rd_len;
14249 	uint str_len = 0;
14250 	char *str_end = NULL;
14251 
14252 	if (!image)
14253 		return 0;
14254 
14255 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
14256 	rd_len = kernel_read(fp, str, len, &fp->f_pos);
14257 #else
14258 	rd_len = kernel_read(fp, fp->f_pos, str, len);
14259 #endif
14260 	str_end = strnchr(str, len, '\n');
14261 	if (str_end == NULL) {
14262 		goto err;
14263 	}
14264 	str_len = (uint)(str_end - str);
14265 
14266 	/* Advance file pointer past the string length */
14267 	fp->f_pos += str_len + 1;
14268 	bzero(str_end, rd_len - str_len);
14269 
14270 err:
14271 	return str_len;
14272 }
14273 #endif /* defined (BT_OVER_SDIO) */
14274 
14275 
14276 void
dhd_os_close_image(void * image)14277 dhd_os_close_image(void *image)
14278 {
14279 	if (image)
14280 		filp_close((struct file *)image, NULL);
14281 }
14282 
14283 void
dhd_os_sdlock(dhd_pub_t * pub)14284 dhd_os_sdlock(dhd_pub_t *pub)
14285 {
14286 	dhd_info_t *dhd;
14287 
14288 	dhd = (dhd_info_t *)(pub->info);
14289 
14290 #ifdef BCMDBUS
14291 	spin_lock_bh(&dhd->sdlock);
14292 #else
14293 	if (dhd_dpc_prio >= 0)
14294 		down(&dhd->sdsem);
14295 	else
14296 		spin_lock_bh(&dhd->sdlock);
14297 #endif /* !BCMDBUS */
14298 }
14299 
14300 void
dhd_os_sdunlock(dhd_pub_t * pub)14301 dhd_os_sdunlock(dhd_pub_t *pub)
14302 {
14303 	dhd_info_t *dhd;
14304 
14305 	dhd = (dhd_info_t *)(pub->info);
14306 
14307 #ifdef BCMDBUS
14308 	spin_unlock_bh(&dhd->sdlock);
14309 #else
14310 	if (dhd_dpc_prio >= 0)
14311 		up(&dhd->sdsem);
14312 	else
14313 		spin_unlock_bh(&dhd->sdlock);
14314 #endif /* !BCMDBUS */
14315 }
14316 
14317 void
dhd_os_sdlock_txq(dhd_pub_t * pub)14318 dhd_os_sdlock_txq(dhd_pub_t *pub)
14319 {
14320 	dhd_info_t *dhd;
14321 
14322 	dhd = (dhd_info_t *)(pub->info);
14323 #ifdef BCMDBUS
14324 	spin_lock_irqsave(&dhd->txqlock, dhd->txqlock_flags);
14325 #else
14326 	spin_lock_bh(&dhd->txqlock);
14327 #endif /* BCMDBUS */
14328 }
14329 
14330 void
dhd_os_sdunlock_txq(dhd_pub_t * pub)14331 dhd_os_sdunlock_txq(dhd_pub_t *pub)
14332 {
14333 	dhd_info_t *dhd;
14334 
14335 	dhd = (dhd_info_t *)(pub->info);
14336 #ifdef BCMDBUS
14337 	spin_unlock_irqrestore(&dhd->txqlock, dhd->txqlock_flags);
14338 #else
14339 	spin_unlock_bh(&dhd->txqlock);
14340 #endif /* BCMDBUS */
14341 }
14342 
14343 void
dhd_os_sdlock_rxq(dhd_pub_t * pub)14344 dhd_os_sdlock_rxq(dhd_pub_t *pub)
14345 {
14346 #if 0
14347 	dhd_info_t *dhd;
14348 
14349 	dhd = (dhd_info_t *)(pub->info);
14350 	spin_lock_bh(&dhd->rxqlock);
14351 #endif
14352 }
14353 
14354 void
dhd_os_sdunlock_rxq(dhd_pub_t * pub)14355 dhd_os_sdunlock_rxq(dhd_pub_t *pub)
14356 {
14357 #if 0
14358 	dhd_info_t *dhd;
14359 
14360 	dhd = (dhd_info_t *)(pub->info);
14361 	spin_unlock_bh(&dhd->rxqlock);
14362 #endif
14363 }
14364 
14365 static void
dhd_os_rxflock(dhd_pub_t * pub)14366 dhd_os_rxflock(dhd_pub_t *pub)
14367 {
14368 	dhd_info_t *dhd;
14369 
14370 	dhd = (dhd_info_t *)(pub->info);
14371 	spin_lock_bh(&dhd->rxf_lock);
14372 
14373 }
14374 
14375 static void
dhd_os_rxfunlock(dhd_pub_t * pub)14376 dhd_os_rxfunlock(dhd_pub_t *pub)
14377 {
14378 	dhd_info_t *dhd;
14379 
14380 	dhd = (dhd_info_t *)(pub->info);
14381 	spin_unlock_bh(&dhd->rxf_lock);
14382 }
14383 
14384 #ifdef DHDTCPACK_SUPPRESS
14385 unsigned long
dhd_os_tcpacklock(dhd_pub_t * pub)14386 dhd_os_tcpacklock(dhd_pub_t *pub)
14387 {
14388 	dhd_info_t *dhd;
14389 	unsigned long flags = 0;
14390 
14391 	dhd = (dhd_info_t *)(pub->info);
14392 
14393 	if (dhd) {
14394 #ifdef BCMSDIO
14395 		spin_lock_bh(&dhd->tcpack_lock);
14396 #else
14397 		spin_lock_irqsave(&dhd->tcpack_lock, flags);
14398 #endif /* BCMSDIO */
14399 	}
14400 
14401 	return flags;
14402 }
14403 
14404 void
dhd_os_tcpackunlock(dhd_pub_t * pub,unsigned long flags)14405 dhd_os_tcpackunlock(dhd_pub_t *pub, unsigned long flags)
14406 {
14407 	dhd_info_t *dhd;
14408 
14409 #ifdef BCMSDIO
14410 	BCM_REFERENCE(flags);
14411 #endif /* BCMSDIO */
14412 
14413 	dhd = (dhd_info_t *)(pub->info);
14414 
14415 	if (dhd) {
14416 #ifdef BCMSDIO
14417 		spin_unlock_bh(&dhd->tcpack_lock);
14418 #else
14419 		spin_unlock_irqrestore(&dhd->tcpack_lock, flags);
14420 #endif /* BCMSDIO */
14421 	}
14422 }
14423 #endif /* DHDTCPACK_SUPPRESS */
14424 
dhd_os_prealloc(dhd_pub_t * dhdpub,int section,uint size,bool kmalloc_if_fail)14425 uint8* dhd_os_prealloc(dhd_pub_t *dhdpub, int section, uint size, bool kmalloc_if_fail)
14426 {
14427 	uint8* buf;
14428 	gfp_t flags = CAN_SLEEP() ? GFP_KERNEL: GFP_ATOMIC;
14429 
14430 	buf = (uint8*)wifi_platform_prealloc(dhdpub->info->adapter, section, size);
14431 	if (buf == NULL && kmalloc_if_fail)
14432 		buf = kmalloc(size, flags);
14433 
14434 	return buf;
14435 }
14436 
dhd_os_prefree(dhd_pub_t * dhdpub,void * addr,uint size)14437 void dhd_os_prefree(dhd_pub_t *dhdpub, void *addr, uint size)
14438 {
14439 }
14440 
14441 #if defined(WL_WIRELESS_EXT)
14442 struct iw_statistics *
dhd_get_wireless_stats(struct net_device * dev)14443 dhd_get_wireless_stats(struct net_device *dev)
14444 {
14445 	int res = 0;
14446 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14447 
14448 	if (!dhd->pub.up) {
14449 		return NULL;
14450 	}
14451 
14452 	res = wl_iw_get_wireless_stats(dev, &dhd->iw.wstats);
14453 
14454 	if (res == 0)
14455 		return &dhd->iw.wstats;
14456 	else
14457 		return NULL;
14458 }
14459 #endif /* defined(WL_WIRELESS_EXT) */
14460 
14461 static int
dhd_wl_host_event(dhd_info_t * dhd,int ifidx,void * pktdata,uint16 pktlen,wl_event_msg_t * event,void ** data)14462 dhd_wl_host_event(dhd_info_t *dhd, int ifidx, void *pktdata, uint16 pktlen,
14463 	wl_event_msg_t *event, void **data)
14464 {
14465 	int bcmerror = 0;
14466 #ifdef WL_CFG80211
14467 	unsigned long flags = 0;
14468 #endif /* WL_CFG80211 */
14469 	ASSERT(dhd != NULL);
14470 
14471 #ifdef SHOW_LOGTRACE
14472 	bcmerror = wl_process_host_event(&dhd->pub, &ifidx, pktdata, pktlen, event, data,
14473 		&dhd->event_data);
14474 #else
14475 	bcmerror = wl_process_host_event(&dhd->pub, &ifidx, pktdata, pktlen, event, data,
14476 		NULL);
14477 #endif /* SHOW_LOGTRACE */
14478 
14479 	if (bcmerror != BCME_OK)
14480 		return (bcmerror);
14481 
14482 #if defined(WL_EXT_IAPSTA) || defined(USE_IW)
14483 	wl_ext_event_send(dhd->pub.event_params, event, *data);
14484 #endif
14485 
14486 #ifdef WL_CFG80211
14487 	ASSERT(dhd->iflist[ifidx] != NULL);
14488 	ASSERT(dhd->iflist[ifidx]->net != NULL);
14489 	if (dhd->iflist[ifidx]->net) {
14490 		spin_lock_irqsave(&dhd->pub.up_lock, flags);
14491 		if (dhd->pub.up) {
14492 			wl_cfg80211_event(dhd->iflist[ifidx]->net, event, *data);
14493 		}
14494 		spin_unlock_irqrestore(&dhd->pub.up_lock, flags);
14495 	}
14496 #endif /* defined(WL_CFG80211) */
14497 
14498 	return (bcmerror);
14499 }
14500 
14501 /* send up locally generated event */
14502 void
dhd_sendup_event(dhd_pub_t * dhdp,wl_event_msg_t * event,void * data)14503 dhd_sendup_event(dhd_pub_t *dhdp, wl_event_msg_t *event, void *data)
14504 {
14505 	/* Just return from here */
14506 	return;
14507 }
14508 
14509 #ifdef LOG_INTO_TCPDUMP
14510 void
dhd_sendup_log(dhd_pub_t * dhdp,void * data,int data_len)14511 dhd_sendup_log(dhd_pub_t *dhdp, void *data, int data_len)
14512 {
14513 	struct sk_buff *p, *skb;
14514 	uint32 pktlen;
14515 	int len;
14516 	dhd_if_t *ifp;
14517 	dhd_info_t *dhd;
14518 	uchar *skb_data;
14519 	int ifidx = 0;
14520 	struct ether_header eth;
14521 
14522 	pktlen = sizeof(eth) + data_len;
14523 	dhd = dhdp->info;
14524 
14525 	if ((p = PKTGET(dhdp->osh, pktlen, FALSE))) {
14526 		ASSERT(ISALIGNED((uintptr)PKTDATA(dhdp->osh, p), sizeof(uint32)));
14527 
14528 		bcopy(&dhdp->mac, &eth.ether_dhost, ETHER_ADDR_LEN);
14529 		bcopy(&dhdp->mac, &eth.ether_shost, ETHER_ADDR_LEN);
14530 		ETHER_TOGGLE_LOCALADDR(&eth.ether_shost);
14531 		eth.ether_type = hton16(ETHER_TYPE_BRCM);
14532 
14533 		bcopy((void *)&eth, PKTDATA(dhdp->osh, p), sizeof(eth));
14534 		bcopy(data, PKTDATA(dhdp->osh, p) + sizeof(eth), data_len);
14535 		skb = PKTTONATIVE(dhdp->osh, p);
14536 		skb_data = skb->data;
14537 		len = skb->len;
14538 
14539 		ifidx = dhd_ifname2idx(dhd, "wlan0");
14540 		ifp = dhd->iflist[ifidx];
14541 		if (ifp == NULL)
14542 			 ifp = dhd->iflist[0];
14543 
14544 		ASSERT(ifp);
14545 		skb->dev = ifp->net;
14546 		skb->protocol = eth_type_trans(skb, skb->dev);
14547 		skb->data = skb_data;
14548 		skb->len = len;
14549 
14550 		/* Strip header, count, deliver upward */
14551 		skb_pull(skb, ETH_HLEN);
14552 
14553 		bcm_object_trace_opr(skb, BCM_OBJDBG_REMOVE,
14554 			__FUNCTION__, __LINE__);
14555 		/* Send the packet */
14556 		if (in_interrupt()) {
14557 			netif_rx(skb);
14558 		} else {
14559 			netif_rx_ni(skb);
14560 		}
14561 	} else {
14562 		/* Could not allocate a sk_buf */
14563 		DHD_ERROR(("%s: unable to alloc sk_buf\n", __FUNCTION__));
14564 	}
14565 }
14566 #endif /* LOG_INTO_TCPDUMP */
14567 
dhd_wait_for_event(dhd_pub_t * dhd,bool * lockvar)14568 void dhd_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
14569 {
14570 #if defined(BCMSDIO) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
14571 	struct dhd_info *dhdinfo =  dhd->info;
14572 
14573 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
14574 	int timeout = msecs_to_jiffies(IOCTL_RESP_TIMEOUT);
14575 #else
14576 	int timeout = (IOCTL_RESP_TIMEOUT / 1000) * HZ;
14577 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) */
14578 
14579 	dhd_os_sdunlock(dhd);
14580 	wait_event_timeout(dhdinfo->ctrl_wait, (*lockvar == FALSE), timeout);
14581 	dhd_os_sdlock(dhd);
14582 #endif /* defined(BCMSDIO) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)) */
14583 	return;
14584 }
14585 
dhd_wait_event_wakeup(dhd_pub_t * dhd)14586 void dhd_wait_event_wakeup(dhd_pub_t *dhd)
14587 {
14588 #if defined(BCMSDIO) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
14589 	struct dhd_info *dhdinfo =  dhd->info;
14590 	if (waitqueue_active(&dhdinfo->ctrl_wait))
14591 		wake_up(&dhdinfo->ctrl_wait);
14592 #endif
14593 	return;
14594 }
14595 
14596 #if defined(BCMSDIO) || defined(BCMPCIE) || defined(BCMDBUS)
14597 int
dhd_net_bus_devreset(struct net_device * dev,uint8 flag)14598 dhd_net_bus_devreset(struct net_device *dev, uint8 flag)
14599 {
14600 	int ret;
14601 
14602 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14603 
14604 	if (flag == TRUE) {
14605 		/* Issue wl down command before resetting the chip */
14606 		if (dhd_wl_ioctl_cmd(&dhd->pub, WLC_DOWN, NULL, 0, TRUE, 0) < 0) {
14607 			DHD_TRACE(("%s: wl down failed\n", __FUNCTION__));
14608 		}
14609 #ifdef PROP_TXSTATUS
14610 		if (dhd->pub.wlfc_enabled) {
14611 			dhd_wlfc_deinit(&dhd->pub);
14612 		}
14613 #endif /* PROP_TXSTATUS */
14614 #ifdef PNO_SUPPORT
14615 		if (dhd->pub.pno_state) {
14616 			dhd_pno_deinit(&dhd->pub);
14617 		}
14618 #endif
14619 #ifdef RTT_SUPPORT
14620 		if (dhd->pub.rtt_state) {
14621 			dhd_rtt_deinit(&dhd->pub);
14622 		}
14623 #endif /* RTT_SUPPORT */
14624 
14625 #if defined(DBG_PKT_MON) && !defined(DBG_PKT_MON_INIT_DEFAULT)
14626 		dhd_os_dbg_detach_pkt_monitor(&dhd->pub);
14627 #endif /* DBG_PKT_MON */
14628 	}
14629 
14630 #ifdef BCMSDIO
14631 	if (!flag) {
14632 		dhd_update_fw_nv_path(dhd);
14633 		/* update firmware and nvram path to sdio bus */
14634 		dhd_bus_update_fw_nv_path(dhd->pub.bus,
14635 			dhd->fw_path, dhd->nv_path, dhd->clm_path, dhd->conf_path);
14636 	}
14637 #endif /* BCMSDIO */
14638 
14639 	ret = dhd_bus_devreset(&dhd->pub, flag);
14640 	if (ret) {
14641 		DHD_ERROR(("%s: dhd_bus_devreset: %d\n", __FUNCTION__, ret));
14642 		return ret;
14643 	}
14644 
14645 	return ret;
14646 }
14647 
14648 #ifdef BCMSDIO
14649 int
dhd_net_bus_suspend(struct net_device * dev)14650 dhd_net_bus_suspend(struct net_device *dev)
14651 {
14652 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14653 	return dhd_bus_suspend(&dhd->pub);
14654 }
14655 
14656 int
dhd_net_bus_resume(struct net_device * dev,uint8 stage)14657 dhd_net_bus_resume(struct net_device *dev, uint8 stage)
14658 {
14659 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14660 	return dhd_bus_resume(&dhd->pub, stage);
14661 }
14662 
14663 #endif /* BCMSDIO */
14664 #endif /* BCMSDIO || BCMPCIE || BCMDBUS */
14665 
net_os_set_suspend_disable(struct net_device * dev,int val)14666 int net_os_set_suspend_disable(struct net_device *dev, int val)
14667 {
14668 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14669 	int ret = 0;
14670 
14671 	if (dhd) {
14672 		ret = dhd->pub.suspend_disable_flag;
14673 		dhd->pub.suspend_disable_flag = val;
14674 	}
14675 	return ret;
14676 }
14677 
net_os_set_suspend(struct net_device * dev,int val,int force)14678 int net_os_set_suspend(struct net_device *dev, int val, int force)
14679 {
14680 	int ret = 0;
14681 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14682 
14683 	if (dhd && dhd->pub.conf->suspend_mode == EARLY_SUSPEND) {
14684 		if (!val)
14685 			dhd_conf_set_suspend_resume(&dhd->pub, val);
14686 #ifdef CONFIG_MACH_UNIVERSAL7420
14687 #endif /* CONFIG_MACH_UNIVERSAL7420 */
14688 #if defined(CONFIG_HAS_EARLYSUSPEND) && defined(DHD_USE_EARLYSUSPEND)
14689 		ret = dhd_set_suspend(val, &dhd->pub);
14690 #else
14691 		ret = dhd_suspend_resume_helper(dhd, val, force);
14692 #endif
14693 #ifdef WL_CFG80211
14694 		wl_cfg80211_update_power_mode(dev);
14695 #endif
14696 		if (val)
14697 			dhd_conf_set_suspend_resume(&dhd->pub, val);
14698 	}
14699 	return ret;
14700 }
14701 
net_os_set_suspend_bcn_li_dtim(struct net_device * dev,int val)14702 int net_os_set_suspend_bcn_li_dtim(struct net_device *dev, int val)
14703 {
14704 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14705 
14706 	if (dhd)
14707 		dhd->pub.suspend_bcn_li_dtim = val;
14708 
14709 	return 0;
14710 }
14711 
net_os_set_max_dtim_enable(struct net_device * dev,int val)14712 int net_os_set_max_dtim_enable(struct net_device *dev, int val)
14713 {
14714 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14715 
14716 	if (dhd) {
14717 		DHD_ERROR(("%s: use MAX bcn_li_dtim in suspend %s\n",
14718 			__FUNCTION__, (val ? "Enable" : "Disable")));
14719 		if (val) {
14720 			dhd->pub.max_dtim_enable = TRUE;
14721 		} else {
14722 			dhd->pub.max_dtim_enable = FALSE;
14723 		}
14724 	} else {
14725 		return -1;
14726 	}
14727 
14728 	return 0;
14729 }
14730 
14731 #ifdef PKT_FILTER_SUPPORT
net_os_rxfilter_add_remove(struct net_device * dev,int add_remove,int num)14732 int net_os_rxfilter_add_remove(struct net_device *dev, int add_remove, int num)
14733 {
14734 	int ret = 0;
14735 
14736 #ifndef GAN_LITE_NAT_KEEPALIVE_FILTER
14737 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14738 
14739 	if (!dhd_master_mode)
14740 		add_remove = !add_remove;
14741 	DHD_ERROR(("%s: add_remove = %d, num = %d\n", __FUNCTION__, add_remove, num));
14742 	if (!dhd || (num == DHD_UNICAST_FILTER_NUM)) {
14743 		return 0;
14744 	}
14745 
14746 
14747 	if (num >= dhd->pub.pktfilter_count) {
14748 		return -EINVAL;
14749 	}
14750 
14751 	ret = dhd_packet_filter_add_remove(&dhd->pub, add_remove, num);
14752 #endif /* !GAN_LITE_NAT_KEEPALIVE_FILTER */
14753 
14754 	return ret;
14755 }
14756 
dhd_os_enable_packet_filter(dhd_pub_t * dhdp,int val)14757 int dhd_os_enable_packet_filter(dhd_pub_t *dhdp, int val)
14758 
14759 {
14760 	int ret = 0;
14761 
14762 	/* Packet filtering is set only if we still in early-suspend and
14763 	 * we need either to turn it ON or turn it OFF
14764 	 * We can always turn it OFF in case of early-suspend, but we turn it
14765 	 * back ON only if suspend_disable_flag was not set
14766 	*/
14767 	if (dhdp && dhdp->up) {
14768 		if (dhdp->in_suspend) {
14769 			if (!val || (val && !dhdp->suspend_disable_flag))
14770 				dhd_enable_packet_filter(val, dhdp);
14771 		}
14772 	}
14773 	return ret;
14774 }
14775 
14776 /* function to enable/disable packet for Network device */
net_os_enable_packet_filter(struct net_device * dev,int val)14777 int net_os_enable_packet_filter(struct net_device *dev, int val)
14778 {
14779 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14780 
14781 	DHD_ERROR(("%s: val = %d\n", __FUNCTION__, val));
14782 	return dhd_os_enable_packet_filter(&dhd->pub, val);
14783 }
14784 #endif /* PKT_FILTER_SUPPORT */
14785 
14786 int
dhd_dev_init_ioctl(struct net_device * dev)14787 dhd_dev_init_ioctl(struct net_device *dev)
14788 {
14789 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14790 	int ret;
14791 
14792 	if ((ret = dhd_sync_with_dongle(&dhd->pub)) < 0)
14793 		goto done;
14794 
14795 done:
14796 	return ret;
14797 }
14798 
14799 int
dhd_dev_get_feature_set(struct net_device * dev)14800 dhd_dev_get_feature_set(struct net_device *dev)
14801 {
14802 	dhd_info_t *ptr = *(dhd_info_t **)netdev_priv(dev);
14803 	dhd_pub_t *dhd = (&ptr->pub);
14804 	int feature_set = 0;
14805 
14806 	if (FW_SUPPORTED(dhd, sta))
14807 		feature_set |= WIFI_FEATURE_INFRA;
14808 	if (FW_SUPPORTED(dhd, dualband))
14809 		feature_set |= WIFI_FEATURE_INFRA_5G;
14810 	if (FW_SUPPORTED(dhd, p2p))
14811 		feature_set |= WIFI_FEATURE_P2P;
14812 	if (dhd->op_mode & DHD_FLAG_HOSTAP_MODE)
14813 		feature_set |= WIFI_FEATURE_SOFT_AP;
14814 	if (FW_SUPPORTED(dhd, tdls))
14815 		feature_set |= WIFI_FEATURE_TDLS;
14816 	if (FW_SUPPORTED(dhd, vsdb))
14817 		feature_set |= WIFI_FEATURE_TDLS_OFFCHANNEL;
14818 	if (FW_SUPPORTED(dhd, nan)) {
14819 		feature_set |= WIFI_FEATURE_NAN;
14820 		/* NAN is essentail for d2d rtt */
14821 		if (FW_SUPPORTED(dhd, rttd2d))
14822 			feature_set |= WIFI_FEATURE_D2D_RTT;
14823 	}
14824 #ifdef RTT_SUPPORT
14825 	if (dhd->rtt_supported) {
14826 		feature_set |= WIFI_FEATURE_D2D_RTT;
14827 		feature_set |= WIFI_FEATURE_D2AP_RTT;
14828 	}
14829 #endif /* RTT_SUPPORT */
14830 #ifdef LINKSTAT_SUPPORT
14831 	feature_set |= WIFI_FEATURE_LINKSTAT;
14832 #endif /* LINKSTAT_SUPPORT */
14833 
14834 #ifdef PNO_SUPPORT
14835 	if (dhd_is_pno_supported(dhd)) {
14836 		feature_set |= WIFI_FEATURE_PNO;
14837 #ifdef GSCAN_SUPPORT
14838 		/* terence 20171115: remove to get GTS PASS
14839 		 * com.google.android.gts.wifi.WifiHostTest#testWifiScannerBatchTimestamp
14840 		 */
14841 //		feature_set |= WIFI_FEATURE_GSCAN;
14842 //		feature_set |= WIFI_FEATURE_HAL_EPNO;
14843 #endif /* GSCAN_SUPPORT */
14844 	}
14845 #endif /* PNO_SUPPORT */
14846 #ifdef RSSI_MONITOR_SUPPORT
14847 	if (FW_SUPPORTED(dhd, rssi_mon)) {
14848 		feature_set |= WIFI_FEATURE_RSSI_MONITOR;
14849 	}
14850 #endif /* RSSI_MONITOR_SUPPORT */
14851 #ifdef WL11U
14852 	feature_set |= WIFI_FEATURE_HOTSPOT;
14853 #endif /* WL11U */
14854 #ifdef NDO_CONFIG_SUPPORT
14855 	feature_set |= WIFI_FEATURE_CONFIG_NDO;
14856 #endif /* NDO_CONFIG_SUPPORT */
14857 #ifdef KEEP_ALIVE
14858 	feature_set |= WIFI_FEATURE_MKEEP_ALIVE;
14859 #endif /* KEEP_ALIVE */
14860 
14861 	return feature_set;
14862 }
14863 
14864 int
dhd_dev_get_feature_set_matrix(struct net_device * dev,int num)14865 dhd_dev_get_feature_set_matrix(struct net_device *dev, int num)
14866 {
14867 	int feature_set_full;
14868 	int ret = 0;
14869 
14870 	feature_set_full = dhd_dev_get_feature_set(dev);
14871 
14872 	/* Common feature set for all interface */
14873 	ret = (feature_set_full & WIFI_FEATURE_INFRA) |
14874 		(feature_set_full & WIFI_FEATURE_INFRA_5G) |
14875 		(feature_set_full & WIFI_FEATURE_D2D_RTT) |
14876 		(feature_set_full & WIFI_FEATURE_D2AP_RTT) |
14877 		(feature_set_full & WIFI_FEATURE_RSSI_MONITOR) |
14878 		(feature_set_full & WIFI_FEATURE_EPR);
14879 
14880 	/* Specific feature group for each interface */
14881 	switch (num) {
14882 	case 0:
14883 		ret |= (feature_set_full & WIFI_FEATURE_P2P) |
14884 			/* Not supported yet */
14885 			/* (feature_set_full & WIFI_FEATURE_NAN) | */
14886 			(feature_set_full & WIFI_FEATURE_TDLS) |
14887 			(feature_set_full & WIFI_FEATURE_PNO) |
14888 			(feature_set_full & WIFI_FEATURE_HAL_EPNO) |
14889 			(feature_set_full & WIFI_FEATURE_BATCH_SCAN) |
14890 			(feature_set_full & WIFI_FEATURE_GSCAN) |
14891 			(feature_set_full & WIFI_FEATURE_HOTSPOT) |
14892 			(feature_set_full & WIFI_FEATURE_ADDITIONAL_STA);
14893 		break;
14894 
14895 	case 1:
14896 		ret |= (feature_set_full & WIFI_FEATURE_P2P);
14897 		/* Not yet verified NAN with P2P */
14898 		/* (feature_set_full & WIFI_FEATURE_NAN) | */
14899 		break;
14900 
14901 	case 2:
14902 		ret |= (feature_set_full & WIFI_FEATURE_NAN) |
14903 			(feature_set_full & WIFI_FEATURE_TDLS) |
14904 			(feature_set_full & WIFI_FEATURE_TDLS_OFFCHANNEL);
14905 		break;
14906 
14907 	default:
14908 		ret = WIFI_FEATURE_INVALID;
14909 		DHD_ERROR(("%s: Out of index(%d) for get feature set\n", __FUNCTION__, num));
14910 		break;
14911 	}
14912 
14913 	return ret;
14914 }
14915 
14916 #ifdef CUSTOM_FORCE_NODFS_FLAG
14917 int
dhd_dev_set_nodfs(struct net_device * dev,u32 nodfs)14918 dhd_dev_set_nodfs(struct net_device *dev, u32 nodfs)
14919 {
14920 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
14921 
14922 	if (nodfs)
14923 		dhd->pub.dhd_cflags |= WLAN_PLAT_NODFS_FLAG;
14924 	else
14925 		dhd->pub.dhd_cflags &= ~WLAN_PLAT_NODFS_FLAG;
14926 	dhd->pub.force_country_change = TRUE;
14927 	return 0;
14928 }
14929 #endif /* CUSTOM_FORCE_NODFS_FLAG */
14930 
14931 #ifdef NDO_CONFIG_SUPPORT
14932 int
dhd_dev_ndo_cfg(struct net_device * dev,u8 enable)14933 dhd_dev_ndo_cfg(struct net_device *dev, u8 enable)
14934 {
14935 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
14936 	dhd_pub_t *dhdp = &dhd->pub;
14937 	int ret = 0;
14938 
14939 	if (enable) {
14940 		/* enable ND offload feature (will be enabled in FW on suspend) */
14941 		dhdp->ndo_enable = TRUE;
14942 
14943 		/* Update changes of anycast address & DAD failed address */
14944 		ret = dhd_dev_ndo_update_inet6addr(dev);
14945 		if ((ret < 0) && (ret != BCME_NORESOURCE)) {
14946 			DHD_ERROR(("%s: failed to update host ip addr: %d\n", __FUNCTION__, ret));
14947 			return ret;
14948 		}
14949 	} else {
14950 		/* disable ND offload feature */
14951 		dhdp->ndo_enable = FALSE;
14952 
14953 		/* disable ND offload in FW */
14954 		ret = dhd_ndo_enable(dhdp, 0);
14955 		if (ret < 0) {
14956 			DHD_ERROR(("%s: failed to disable NDO: %d\n", __FUNCTION__, ret));
14957 		}
14958 	}
14959 	return ret;
14960 }
14961 
14962 /* #pragma used as a WAR to fix build failure,
14963 * ignore dropping of 'const' qualifier in 'list_entry' macro
14964 * this pragma disables the warning only for the following function
14965 */
14966 #pragma GCC diagnostic push
14967 #pragma GCC diagnostic ignored "-Wcast-qual"
14968 
14969 static int
dhd_dev_ndo_get_valid_inet6addr_count(struct inet6_dev * inet6)14970 dhd_dev_ndo_get_valid_inet6addr_count(struct inet6_dev *inet6)
14971 {
14972 	struct inet6_ifaddr *ifa;
14973 	struct ifacaddr6 *acaddr = NULL;
14974 	int addr_count = 0;
14975 
14976 	/* lock */
14977 	read_lock_bh(&inet6->lock);
14978 
14979 	/* Count valid unicast address */
14980 	list_for_each_entry(ifa, &inet6->addr_list, if_list) {
14981 		if ((ifa->flags & IFA_F_DADFAILED) == 0) {
14982 			addr_count++;
14983 		}
14984 	}
14985 
14986 	/* Count anycast address */
14987 	acaddr = inet6->ac_list;
14988 	while (acaddr) {
14989 		addr_count++;
14990 		acaddr = acaddr->aca_next;
14991 	}
14992 
14993 	/* unlock */
14994 	read_unlock_bh(&inet6->lock);
14995 
14996 	return addr_count;
14997 }
14998 
14999 int
dhd_dev_ndo_update_inet6addr(struct net_device * dev)15000 dhd_dev_ndo_update_inet6addr(struct net_device *dev)
15001 {
15002 	dhd_info_t *dhd;
15003 	dhd_pub_t *dhdp;
15004 	struct inet6_dev *inet6;
15005 	struct inet6_ifaddr *ifa;
15006 	struct ifacaddr6 *acaddr = NULL;
15007 	struct in6_addr *ipv6_addr = NULL;
15008 	int cnt, i;
15009 	int ret = BCME_OK;
15010 
15011 	/*
15012 	 * this function evaulates host ip address in struct inet6_dev
15013 	 * unicast addr in inet6_dev->addr_list
15014 	 * anycast addr in inet6_dev->ac_list
15015 	 * while evaluating inet6_dev, read_lock_bh() is required to prevent
15016 	 * access on null(freed) pointer.
15017 	 */
15018 
15019 	if (dev) {
15020 		inet6 = dev->ip6_ptr;
15021 		if (!inet6) {
15022 			DHD_ERROR(("%s: Invalid inet6_dev\n", __FUNCTION__));
15023 			return BCME_ERROR;
15024 		}
15025 
15026 		dhd = DHD_DEV_INFO(dev);
15027 		if (!dhd) {
15028 			DHD_ERROR(("%s: Invalid dhd_info\n", __FUNCTION__));
15029 			return BCME_ERROR;
15030 		}
15031 		dhdp = &dhd->pub;
15032 
15033 		if (dhd_net2idx(dhd, dev) != 0) {
15034 			DHD_ERROR(("%s: Not primary interface\n", __FUNCTION__));
15035 			return BCME_ERROR;
15036 		}
15037 	} else {
15038 		DHD_ERROR(("%s: Invalid net_device\n", __FUNCTION__));
15039 		return BCME_ERROR;
15040 	}
15041 
15042 	/* Check host IP overflow */
15043 	cnt = dhd_dev_ndo_get_valid_inet6addr_count(inet6);
15044 	if (cnt > dhdp->ndo_max_host_ip) {
15045 		if (!dhdp->ndo_host_ip_overflow) {
15046 			dhdp->ndo_host_ip_overflow = TRUE;
15047 			/* Disable ND offload in FW */
15048 			DHD_INFO(("%s: Host IP overflow, disable NDO\n", __FUNCTION__));
15049 			ret = dhd_ndo_enable(dhdp, 0);
15050 		}
15051 
15052 		return ret;
15053 	}
15054 
15055 	/*
15056 	 * Allocate ipv6 addr buffer to store addresses to be added/removed.
15057 	 * driver need to lock inet6_dev while accessing structure. but, driver
15058 	 * cannot use ioctl while inet6_dev locked since it requires scheduling
15059 	 * hence, copy addresses to the buffer and do ioctl after unlock.
15060 	 */
15061 	ipv6_addr = (struct in6_addr *)MALLOC(dhdp->osh,
15062 		sizeof(struct in6_addr) * dhdp->ndo_max_host_ip);
15063 	if (!ipv6_addr) {
15064 		DHD_ERROR(("%s: failed to alloc ipv6 addr buffer\n", __FUNCTION__));
15065 		return BCME_NOMEM;
15066 	}
15067 
15068 	/* Find DAD failed unicast address to be removed */
15069 	cnt = 0;
15070 	read_lock_bh(&inet6->lock);
15071 	list_for_each_entry(ifa, &inet6->addr_list, if_list) {
15072 		/* DAD failed unicast address */
15073 		if ((ifa->flags & IFA_F_DADFAILED) &&
15074 			(cnt < dhdp->ndo_max_host_ip)) {
15075 				memcpy(&ipv6_addr[cnt], &ifa->addr, sizeof(struct in6_addr));
15076 				cnt++;
15077 		}
15078 	}
15079 	read_unlock_bh(&inet6->lock);
15080 
15081 	/* Remove DAD failed unicast address */
15082 	for (i = 0; i < cnt; i++) {
15083 		DHD_INFO(("%s: Remove DAD failed addr\n", __FUNCTION__));
15084 		ret = dhd_ndo_remove_ip_by_addr(dhdp, (char *)&ipv6_addr[i], 0);
15085 		if (ret < 0) {
15086 			goto done;
15087 		}
15088 	}
15089 
15090 	/* Remove all anycast address */
15091 	ret = dhd_ndo_remove_ip_by_type(dhdp, WL_ND_IPV6_ADDR_TYPE_ANYCAST, 0);
15092 	if (ret < 0) {
15093 		goto done;
15094 	}
15095 
15096 	/*
15097 	 * if ND offload was disabled due to host ip overflow,
15098 	 * attempt to add valid unicast address.
15099 	 */
15100 	if (dhdp->ndo_host_ip_overflow) {
15101 		/* Find valid unicast address */
15102 		cnt = 0;
15103 		read_lock_bh(&inet6->lock);
15104 		list_for_each_entry(ifa, &inet6->addr_list, if_list) {
15105 			/* valid unicast address */
15106 			if (!(ifa->flags & IFA_F_DADFAILED) &&
15107 				(cnt < dhdp->ndo_max_host_ip)) {
15108 					memcpy(&ipv6_addr[cnt], &ifa->addr,
15109 						sizeof(struct in6_addr));
15110 					cnt++;
15111 			}
15112 		}
15113 		read_unlock_bh(&inet6->lock);
15114 
15115 		/* Add valid unicast address */
15116 		for (i = 0; i < cnt; i++) {
15117 			ret = dhd_ndo_add_ip_with_type(dhdp,
15118 				(char *)&ipv6_addr[i], WL_ND_IPV6_ADDR_TYPE_UNICAST, 0);
15119 			if (ret < 0) {
15120 				goto done;
15121 			}
15122 		}
15123 	}
15124 
15125 	/* Find anycast address */
15126 	cnt = 0;
15127 	read_lock_bh(&inet6->lock);
15128 	acaddr = inet6->ac_list;
15129 	while (acaddr) {
15130 		if (cnt < dhdp->ndo_max_host_ip) {
15131 			memcpy(&ipv6_addr[cnt], &acaddr->aca_addr, sizeof(struct in6_addr));
15132 			cnt++;
15133 		}
15134 		acaddr = acaddr->aca_next;
15135 	}
15136 	read_unlock_bh(&inet6->lock);
15137 
15138 	/* Add anycast address */
15139 	for (i = 0; i < cnt; i++) {
15140 		ret = dhd_ndo_add_ip_with_type(dhdp,
15141 			(char *)&ipv6_addr[i], WL_ND_IPV6_ADDR_TYPE_ANYCAST, 0);
15142 		if (ret < 0) {
15143 			goto done;
15144 		}
15145 	}
15146 
15147 	/* Now All host IP addr were added successfully */
15148 	if (dhdp->ndo_host_ip_overflow) {
15149 		dhdp->ndo_host_ip_overflow = FALSE;
15150 		if (dhdp->in_suspend) {
15151 			/* drvier is in (early) suspend state, need to enable ND offload in FW */
15152 			DHD_INFO(("%s: enable NDO\n", __FUNCTION__));
15153 			ret = dhd_ndo_enable(dhdp, 1);
15154 		}
15155 	}
15156 
15157 done:
15158 	if (ipv6_addr) {
15159 		MFREE(dhdp->osh, ipv6_addr, sizeof(struct in6_addr) * dhdp->ndo_max_host_ip);
15160 	}
15161 
15162 	return ret;
15163 }
15164 #pragma GCC diagnostic pop
15165 
15166 #endif /* NDO_CONFIG_SUPPORT */
15167 
15168 #ifdef PNO_SUPPORT
15169 /* Linux wrapper to call common dhd_pno_stop_for_ssid */
15170 int
dhd_dev_pno_stop_for_ssid(struct net_device * dev)15171 dhd_dev_pno_stop_for_ssid(struct net_device *dev)
15172 {
15173 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15174 
15175 	return (dhd_pno_stop_for_ssid(&dhd->pub));
15176 }
15177 
15178 /* Linux wrapper to call common dhd_pno_set_for_ssid */
15179 int
dhd_dev_pno_set_for_ssid(struct net_device * dev,wlc_ssid_ext_t * ssids_local,int nssid,uint16 scan_fr,int pno_repeat,int pno_freq_expo_max,uint16 * channel_list,int nchan)15180 dhd_dev_pno_set_for_ssid(struct net_device *dev, wlc_ssid_ext_t* ssids_local, int nssid,
15181 	uint16  scan_fr, int pno_repeat, int pno_freq_expo_max, uint16 *channel_list, int nchan)
15182 {
15183 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15184 
15185 	return (dhd_pno_set_for_ssid(&dhd->pub, ssids_local, nssid, scan_fr,
15186 		pno_repeat, pno_freq_expo_max, channel_list, nchan));
15187 }
15188 
15189 /* Linux wrapper to call common dhd_pno_enable */
15190 int
dhd_dev_pno_enable(struct net_device * dev,int enable)15191 dhd_dev_pno_enable(struct net_device *dev, int enable)
15192 {
15193 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15194 
15195 	return (dhd_pno_enable(&dhd->pub, enable));
15196 }
15197 
15198 /* Linux wrapper to call common dhd_pno_set_for_hotlist */
15199 int
dhd_dev_pno_set_for_hotlist(struct net_device * dev,wl_pfn_bssid_t * p_pfn_bssid,struct dhd_pno_hotlist_params * hotlist_params)15200 dhd_dev_pno_set_for_hotlist(struct net_device *dev, wl_pfn_bssid_t *p_pfn_bssid,
15201 	struct dhd_pno_hotlist_params *hotlist_params)
15202 {
15203 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15204 	return (dhd_pno_set_for_hotlist(&dhd->pub, p_pfn_bssid, hotlist_params));
15205 }
15206 /* Linux wrapper to call common dhd_dev_pno_stop_for_batch */
15207 int
dhd_dev_pno_stop_for_batch(struct net_device * dev)15208 dhd_dev_pno_stop_for_batch(struct net_device *dev)
15209 {
15210 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15211 	return (dhd_pno_stop_for_batch(&dhd->pub));
15212 }
15213 
15214 /* Linux wrapper to call common dhd_dev_pno_set_for_batch */
15215 int
dhd_dev_pno_set_for_batch(struct net_device * dev,struct dhd_pno_batch_params * batch_params)15216 dhd_dev_pno_set_for_batch(struct net_device *dev, struct dhd_pno_batch_params *batch_params)
15217 {
15218 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15219 	return (dhd_pno_set_for_batch(&dhd->pub, batch_params));
15220 }
15221 
15222 /* Linux wrapper to call common dhd_dev_pno_get_for_batch */
15223 int
dhd_dev_pno_get_for_batch(struct net_device * dev,char * buf,int bufsize)15224 dhd_dev_pno_get_for_batch(struct net_device *dev, char *buf, int bufsize)
15225 {
15226 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15227 	return (dhd_pno_get_for_batch(&dhd->pub, buf, bufsize, PNO_STATUS_NORMAL));
15228 }
15229 #endif /* PNO_SUPPORT */
15230 
15231 #if defined(PNO_SUPPORT)
15232 #ifdef GSCAN_SUPPORT
15233 bool
dhd_dev_is_legacy_pno_enabled(struct net_device * dev)15234 dhd_dev_is_legacy_pno_enabled(struct net_device *dev)
15235 {
15236 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15237 
15238 	return (dhd_is_legacy_pno_enabled(&dhd->pub));
15239 }
15240 
15241 int
dhd_dev_set_epno(struct net_device * dev)15242 dhd_dev_set_epno(struct net_device *dev)
15243 {
15244 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15245 	if (!dhd) {
15246 		return BCME_ERROR;
15247 	}
15248 	return dhd_pno_set_epno(&dhd->pub);
15249 }
15250 int
dhd_dev_flush_fw_epno(struct net_device * dev)15251 dhd_dev_flush_fw_epno(struct net_device *dev)
15252 {
15253 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
15254 	if (!dhd) {
15255 		return BCME_ERROR;
15256 	}
15257 	return dhd_pno_flush_fw_epno(&dhd->pub);
15258 }
15259 
15260 /* Linux wrapper to call common dhd_pno_set_cfg_gscan */
15261 int
dhd_dev_pno_set_cfg_gscan(struct net_device * dev,dhd_pno_gscan_cmd_cfg_t type,void * buf,bool flush)15262 dhd_dev_pno_set_cfg_gscan(struct net_device *dev, dhd_pno_gscan_cmd_cfg_t type,
15263  void *buf, bool flush)
15264 {
15265 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15266 
15267 	return (dhd_pno_set_cfg_gscan(&dhd->pub, type, buf, flush));
15268 }
15269 
15270 /* Linux wrapper to call common dhd_wait_batch_results_complete */
15271 int
dhd_dev_wait_batch_results_complete(struct net_device * dev)15272 dhd_dev_wait_batch_results_complete(struct net_device *dev)
15273 {
15274 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15275 
15276 	return (dhd_wait_batch_results_complete(&dhd->pub));
15277 }
15278 
15279 /* Linux wrapper to call common dhd_pno_lock_batch_results */
15280 int
dhd_dev_pno_lock_access_batch_results(struct net_device * dev)15281 dhd_dev_pno_lock_access_batch_results(struct net_device *dev)
15282 {
15283 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15284 
15285 	return (dhd_pno_lock_batch_results(&dhd->pub));
15286 }
15287 /* Linux wrapper to call common dhd_pno_unlock_batch_results */
15288 void
dhd_dev_pno_unlock_access_batch_results(struct net_device * dev)15289 dhd_dev_pno_unlock_access_batch_results(struct net_device *dev)
15290 {
15291 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15292 
15293 	return (dhd_pno_unlock_batch_results(&dhd->pub));
15294 }
15295 
15296 /* Linux wrapper to call common dhd_pno_initiate_gscan_request */
15297 int
dhd_dev_pno_run_gscan(struct net_device * dev,bool run,bool flush)15298 dhd_dev_pno_run_gscan(struct net_device *dev, bool run, bool flush)
15299 {
15300 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15301 
15302 	return (dhd_pno_initiate_gscan_request(&dhd->pub, run, flush));
15303 }
15304 
15305 /* Linux wrapper to call common dhd_pno_enable_full_scan_result */
15306 int
dhd_dev_pno_enable_full_scan_result(struct net_device * dev,bool real_time_flag)15307 dhd_dev_pno_enable_full_scan_result(struct net_device *dev, bool real_time_flag)
15308 {
15309 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15310 
15311 	return (dhd_pno_enable_full_scan_result(&dhd->pub, real_time_flag));
15312 }
15313 
15314 /* Linux wrapper to call common dhd_handle_hotlist_scan_evt */
15315 void *
dhd_dev_hotlist_scan_event(struct net_device * dev,const void * data,int * send_evt_bytes,hotlist_type_t type)15316 dhd_dev_hotlist_scan_event(struct net_device *dev,
15317       const void  *data, int *send_evt_bytes, hotlist_type_t type)
15318 {
15319 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15320 
15321 	return (dhd_handle_hotlist_scan_evt(&dhd->pub, data, send_evt_bytes, type));
15322 }
15323 
15324 /* Linux wrapper to call common dhd_process_full_gscan_result */
15325 void *
dhd_dev_process_full_gscan_result(struct net_device * dev,const void * data,uint32 len,int * send_evt_bytes)15326 dhd_dev_process_full_gscan_result(struct net_device *dev,
15327 const void  *data, uint32 len, int *send_evt_bytes)
15328 {
15329 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15330 
15331 	return (dhd_process_full_gscan_result(&dhd->pub, data, len, send_evt_bytes));
15332 }
15333 
15334 void
dhd_dev_gscan_hotlist_cache_cleanup(struct net_device * dev,hotlist_type_t type)15335 dhd_dev_gscan_hotlist_cache_cleanup(struct net_device *dev, hotlist_type_t type)
15336 {
15337 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15338 
15339 	dhd_gscan_hotlist_cache_cleanup(&dhd->pub, type);
15340 
15341 	return;
15342 }
15343 
15344 int
dhd_dev_gscan_batch_cache_cleanup(struct net_device * dev)15345 dhd_dev_gscan_batch_cache_cleanup(struct net_device *dev)
15346 {
15347 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15348 
15349 	return (dhd_gscan_batch_cache_cleanup(&dhd->pub));
15350 }
15351 
15352 /* Linux wrapper to call common dhd_retreive_batch_scan_results */
15353 int
dhd_dev_retrieve_batch_scan(struct net_device * dev)15354 dhd_dev_retrieve_batch_scan(struct net_device *dev)
15355 {
15356 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15357 
15358 	return (dhd_retreive_batch_scan_results(&dhd->pub));
15359 }
15360 
15361 /* Linux wrapper to call common dhd_pno_process_epno_result */
dhd_dev_process_epno_result(struct net_device * dev,const void * data,uint32 event,int * send_evt_bytes)15362 void * dhd_dev_process_epno_result(struct net_device *dev,
15363 	const void  *data, uint32 event, int *send_evt_bytes)
15364 {
15365 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15366 
15367 	return (dhd_pno_process_epno_result(&dhd->pub, data, event, send_evt_bytes));
15368 }
15369 
15370 int
dhd_dev_set_lazy_roam_cfg(struct net_device * dev,wlc_roam_exp_params_t * roam_param)15371 dhd_dev_set_lazy_roam_cfg(struct net_device *dev,
15372              wlc_roam_exp_params_t *roam_param)
15373 {
15374 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15375 	wl_roam_exp_cfg_t roam_exp_cfg;
15376 	int err;
15377 
15378 	if (!roam_param) {
15379 		return BCME_BADARG;
15380 	}
15381 
15382 	DHD_ERROR(("a_band_boost_thr %d a_band_penalty_thr %d\n",
15383 	      roam_param->a_band_boost_threshold, roam_param->a_band_penalty_threshold));
15384 	DHD_ERROR(("a_band_boost_factor %d a_band_penalty_factor %d cur_bssid_boost %d\n",
15385 	      roam_param->a_band_boost_factor, roam_param->a_band_penalty_factor,
15386 	      roam_param->cur_bssid_boost));
15387 	DHD_ERROR(("alert_roam_trigger_thr %d a_band_max_boost %d\n",
15388 	      roam_param->alert_roam_trigger_threshold, roam_param->a_band_max_boost));
15389 
15390 	memcpy(&roam_exp_cfg.params, roam_param, sizeof(*roam_param));
15391 	roam_exp_cfg.version = ROAM_EXP_CFG_VERSION;
15392 	roam_exp_cfg.flags = ROAM_EXP_CFG_PRESENT;
15393 	if (dhd->pub.lazy_roam_enable) {
15394 		roam_exp_cfg.flags |= ROAM_EXP_ENABLE_FLAG;
15395 	}
15396 	err = dhd_iovar(&dhd->pub, 0, "roam_exp_params",
15397 			(char *)&roam_exp_cfg, sizeof(roam_exp_cfg), NULL, 0,
15398 			TRUE);
15399 	if (err < 0) {
15400 		DHD_ERROR(("%s : Failed to execute roam_exp_params %d\n", __FUNCTION__, err));
15401 	}
15402 	return err;
15403 }
15404 
15405 int
dhd_dev_lazy_roam_enable(struct net_device * dev,uint32 enable)15406 dhd_dev_lazy_roam_enable(struct net_device *dev, uint32 enable)
15407 {
15408 	int err;
15409 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15410 	wl_roam_exp_cfg_t roam_exp_cfg;
15411 
15412 	memset(&roam_exp_cfg, 0, sizeof(roam_exp_cfg));
15413 	roam_exp_cfg.version = ROAM_EXP_CFG_VERSION;
15414 	if (enable) {
15415 		roam_exp_cfg.flags = ROAM_EXP_ENABLE_FLAG;
15416 	}
15417 
15418 	err = dhd_iovar(&dhd->pub, 0, "roam_exp_params",
15419 			(char *)&roam_exp_cfg, sizeof(roam_exp_cfg), NULL, 0,
15420 			TRUE);
15421 	if (err < 0) {
15422 		DHD_ERROR(("%s : Failed to execute roam_exp_params %d\n", __FUNCTION__, err));
15423 	} else {
15424 		dhd->pub.lazy_roam_enable = (enable != 0);
15425 	}
15426 	return err;
15427 }
15428 
15429 int
dhd_dev_set_lazy_roam_bssid_pref(struct net_device * dev,wl_bssid_pref_cfg_t * bssid_pref,uint32 flush)15430 dhd_dev_set_lazy_roam_bssid_pref(struct net_device *dev,
15431        wl_bssid_pref_cfg_t *bssid_pref, uint32 flush)
15432 {
15433 	int err;
15434 	int len;
15435 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15436 
15437 	bssid_pref->version = BSSID_PREF_LIST_VERSION;
15438 	/* By default programming bssid pref flushes out old values */
15439 	bssid_pref->flags = (flush && !bssid_pref->count) ? ROAM_EXP_CLEAR_BSSID_PREF: 0;
15440 	len = sizeof(wl_bssid_pref_cfg_t);
15441 	len += (bssid_pref->count - 1) * sizeof(wl_bssid_pref_list_t);
15442 	err = dhd_iovar(&(dhd->pub), 0, "roam_exp_bssid_pref", (char *)bssid_pref,
15443 		len, NULL, 0, TRUE);
15444 	if (err != BCME_OK) {
15445 		DHD_ERROR(("%s : Failed to execute roam_exp_bssid_pref %d\n", __FUNCTION__, err));
15446 	}
15447 	return err;
15448 }
15449 
15450 int
dhd_dev_set_blacklist_bssid(struct net_device * dev,maclist_t * blacklist,uint32 len,uint32 flush)15451 dhd_dev_set_blacklist_bssid(struct net_device *dev, maclist_t *blacklist,
15452     uint32 len, uint32 flush)
15453 {
15454 	int err;
15455 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15456 	int macmode;
15457 
15458 	if (blacklist) {
15459 		err = dhd_wl_ioctl_cmd(&(dhd->pub), WLC_SET_MACLIST, (char *)blacklist,
15460 				len, TRUE, 0);
15461 		if (err != BCME_OK) {
15462 			DHD_ERROR(("%s : WLC_SET_MACLIST failed %d\n", __FUNCTION__, err));
15463 			return err;
15464 		}
15465 	}
15466 	/* By default programming blacklist flushes out old values */
15467 	macmode = (flush && !blacklist) ? WLC_MACMODE_DISABLED : WLC_MACMODE_DENY;
15468 	err = dhd_wl_ioctl_cmd(&(dhd->pub), WLC_SET_MACMODE, (char *)&macmode,
15469 	              sizeof(macmode), TRUE, 0);
15470 	if (err != BCME_OK) {
15471 		DHD_ERROR(("%s : WLC_SET_MACMODE failed %d\n", __FUNCTION__, err));
15472 	}
15473 	return err;
15474 }
15475 
15476 int
dhd_dev_set_whitelist_ssid(struct net_device * dev,wl_ssid_whitelist_t * ssid_whitelist,uint32 len,uint32 flush)15477 dhd_dev_set_whitelist_ssid(struct net_device *dev, wl_ssid_whitelist_t *ssid_whitelist,
15478     uint32 len, uint32 flush)
15479 {
15480 	int err;
15481 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15482 	wl_ssid_whitelist_t whitelist_ssid_flush;
15483 
15484 	if (!ssid_whitelist) {
15485 		if (flush) {
15486 			ssid_whitelist = &whitelist_ssid_flush;
15487 			ssid_whitelist->ssid_count = 0;
15488 		} else {
15489 			DHD_ERROR(("%s : Nothing to do here\n", __FUNCTION__));
15490 			return BCME_BADARG;
15491 		}
15492 	}
15493 	ssid_whitelist->version = SSID_WHITELIST_VERSION;
15494 	ssid_whitelist->flags = flush ? ROAM_EXP_CLEAR_SSID_WHITELIST : 0;
15495 	err = dhd_iovar(&(dhd->pub), 0, "roam_exp_ssid_whitelist", (char *)ssid_whitelist,
15496 			len, NULL, 0, TRUE);
15497 	if (err != BCME_OK) {
15498 		DHD_ERROR(("%s : Failed to execute roam_exp_bssid_pref %d\n", __FUNCTION__, err));
15499 	}
15500 	return err;
15501 }
15502 #endif /* GSCAN_SUPPORT */
15503 
15504 #if defined(GSCAN_SUPPORT) || defined(DHD_GET_VALID_CHANNELS)
15505 /* Linux wrapper to call common dhd_pno_get_gscan */
15506 void *
dhd_dev_pno_get_gscan(struct net_device * dev,dhd_pno_gscan_cmd_cfg_t type,void * info,uint32 * len)15507 dhd_dev_pno_get_gscan(struct net_device *dev, dhd_pno_gscan_cmd_cfg_t type,
15508 	void *info, uint32 *len)
15509 {
15510 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15511 
15512 	return (dhd_pno_get_gscan(&dhd->pub, type, info, len));
15513 }
15514 #endif /* GSCAN_SUPPORT || DHD_GET_VALID_CHANNELS */
15515 #endif
15516 
15517 #ifdef  RSSI_MONITOR_SUPPORT
15518 int
dhd_dev_set_rssi_monitor_cfg(struct net_device * dev,int start,int8 max_rssi,int8 min_rssi)15519 dhd_dev_set_rssi_monitor_cfg(struct net_device *dev, int start,
15520              int8 max_rssi, int8 min_rssi)
15521 {
15522 	int err;
15523 	wl_rssi_monitor_cfg_t rssi_monitor;
15524 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15525 
15526 	rssi_monitor.version = RSSI_MONITOR_VERSION;
15527 	rssi_monitor.max_rssi = max_rssi;
15528 	rssi_monitor.min_rssi = min_rssi;
15529 	rssi_monitor.flags = start ? 0: RSSI_MONITOR_STOP;
15530 	err = dhd_iovar(&(dhd->pub), 0, "rssi_monitor", (char *)&rssi_monitor,
15531 		sizeof(rssi_monitor), NULL, 0, TRUE);
15532 	if (err < 0 && err != BCME_UNSUPPORTED) {
15533 		DHD_ERROR(("%s : Failed to execute rssi_monitor %d\n", __FUNCTION__, err));
15534 	}
15535 	return err;
15536 }
15537 #endif /* RSSI_MONITOR_SUPPORT */
15538 
15539 #ifdef DHDTCPACK_SUPPRESS
dhd_dev_set_tcpack_sup_mode_cfg(struct net_device * dev,uint8 enable)15540 int dhd_dev_set_tcpack_sup_mode_cfg(struct net_device *dev, uint8 enable)
15541 {
15542 	int err;
15543 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15544 
15545 	err = dhd_tcpack_suppress_set(&(dhd->pub), enable);
15546 	if (err != BCME_OK) {
15547 		DHD_ERROR(("%s : Failed to execute rssi_monitor %d\n", __FUNCTION__, err));
15548 	}
15549 	return err;
15550 }
15551 #endif /* DHDTCPACK_SUPPRESS */
15552 
15553 int
dhd_dev_cfg_rand_mac_oui(struct net_device * dev,uint8 * oui)15554 dhd_dev_cfg_rand_mac_oui(struct net_device *dev, uint8 *oui)
15555 {
15556 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15557 	dhd_pub_t *dhdp = &dhd->pub;
15558 
15559 	if (!dhdp || !oui) {
15560 		DHD_ERROR(("NULL POINTER : %s\n",
15561 			__FUNCTION__));
15562 		return BCME_ERROR;
15563 	}
15564 	if (ETHER_ISMULTI(oui)) {
15565 		DHD_ERROR(("Expected unicast OUI\n"));
15566 		return BCME_ERROR;
15567 	} else {
15568 		uint8 *rand_mac_oui = dhdp->rand_mac_oui;
15569 		memcpy(rand_mac_oui, oui, DOT11_OUI_LEN);
15570 		DHD_ERROR(("Random MAC OUI to be used - %02x:%02x:%02x\n", rand_mac_oui[0],
15571 		    rand_mac_oui[1], rand_mac_oui[2]));
15572 	}
15573 	return BCME_OK;
15574 }
15575 
15576 int
dhd_set_rand_mac_oui(dhd_pub_t * dhd)15577 dhd_set_rand_mac_oui(dhd_pub_t *dhd)
15578 {
15579 	int err;
15580 	wl_pfn_macaddr_cfg_t wl_cfg;
15581 	uint8 *rand_mac_oui = dhd->rand_mac_oui;
15582 
15583 	memset(&wl_cfg.macaddr, 0, ETHER_ADDR_LEN);
15584 	memcpy(&wl_cfg.macaddr, rand_mac_oui, DOT11_OUI_LEN);
15585 	wl_cfg.version = WL_PFN_MACADDR_CFG_VER;
15586 	if (ETHER_ISNULLADDR(&wl_cfg.macaddr)) {
15587 		wl_cfg.flags = 0;
15588 	} else {
15589 		wl_cfg.flags = (WL_PFN_MAC_OUI_ONLY_MASK | WL_PFN_SET_MAC_UNASSOC_MASK);
15590 	}
15591 
15592 	DHD_ERROR(("Setting rand mac oui to FW - %02x:%02x:%02x\n", rand_mac_oui[0],
15593 		rand_mac_oui[1], rand_mac_oui[2]));
15594 
15595 	err = dhd_iovar(dhd, 0, "pfn_macaddr", (char *)&wl_cfg, sizeof(wl_cfg), NULL, 0, TRUE);
15596 	if (err < 0) {
15597 		DHD_ERROR(("%s : failed to execute pfn_macaddr %d\n", __FUNCTION__, err));
15598 	}
15599 	return err;
15600 }
15601 
15602 #ifdef RTT_SUPPORT
15603 #ifdef WL_CFG80211
15604 /* Linux wrapper to call common dhd_pno_set_cfg_gscan */
15605 int
dhd_dev_rtt_set_cfg(struct net_device * dev,void * buf)15606 dhd_dev_rtt_set_cfg(struct net_device *dev, void *buf)
15607 {
15608 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15609 
15610 	return (dhd_rtt_set_cfg(&dhd->pub, buf));
15611 }
15612 
15613 int
dhd_dev_rtt_cancel_cfg(struct net_device * dev,struct ether_addr * mac_list,int mac_cnt)15614 dhd_dev_rtt_cancel_cfg(struct net_device *dev, struct ether_addr *mac_list, int mac_cnt)
15615 {
15616 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15617 
15618 	return (dhd_rtt_stop(&dhd->pub, mac_list, mac_cnt));
15619 }
15620 
15621 int
dhd_dev_rtt_register_noti_callback(struct net_device * dev,void * ctx,dhd_rtt_compl_noti_fn noti_fn)15622 dhd_dev_rtt_register_noti_callback(struct net_device *dev, void *ctx, dhd_rtt_compl_noti_fn noti_fn)
15623 {
15624 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15625 
15626 	return (dhd_rtt_register_noti_callback(&dhd->pub, ctx, noti_fn));
15627 }
15628 
15629 int
dhd_dev_rtt_unregister_noti_callback(struct net_device * dev,dhd_rtt_compl_noti_fn noti_fn)15630 dhd_dev_rtt_unregister_noti_callback(struct net_device *dev, dhd_rtt_compl_noti_fn noti_fn)
15631 {
15632 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15633 
15634 	return (dhd_rtt_unregister_noti_callback(&dhd->pub, noti_fn));
15635 }
15636 
15637 int
dhd_dev_rtt_capability(struct net_device * dev,rtt_capabilities_t * capa)15638 dhd_dev_rtt_capability(struct net_device *dev, rtt_capabilities_t *capa)
15639 {
15640 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15641 
15642 	return (dhd_rtt_capability(&dhd->pub, capa));
15643 }
15644 
15645 int
dhd_dev_rtt_avail_channel(struct net_device * dev,wifi_channel_info * channel_info)15646 dhd_dev_rtt_avail_channel(struct net_device *dev, wifi_channel_info *channel_info)
15647 {
15648 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15649 	return (dhd_rtt_avail_channel(&dhd->pub, channel_info));
15650 }
15651 
15652 int
dhd_dev_rtt_enable_responder(struct net_device * dev,wifi_channel_info * channel_info)15653 dhd_dev_rtt_enable_responder(struct net_device *dev, wifi_channel_info *channel_info)
15654 {
15655 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15656 	return (dhd_rtt_enable_responder(&dhd->pub, channel_info));
15657 }
15658 
dhd_dev_rtt_cancel_responder(struct net_device * dev)15659 int dhd_dev_rtt_cancel_responder(struct net_device *dev)
15660 {
15661 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
15662 	return (dhd_rtt_cancel_responder(&dhd->pub));
15663 }
15664 #endif /* WL_CFG80211 */
15665 #endif /* RTT_SUPPORT */
15666 
15667 #ifdef KEEP_ALIVE
15668 #define KA_TEMP_BUF_SIZE 512
15669 #define KA_FRAME_SIZE 300
15670 
15671 int
dhd_dev_start_mkeep_alive(dhd_pub_t * dhd_pub,uint8 mkeep_alive_id,uint8 * ip_pkt,uint16 ip_pkt_len,uint8 * src_mac,uint8 * dst_mac,uint32 period_msec)15672 dhd_dev_start_mkeep_alive(dhd_pub_t *dhd_pub, uint8 mkeep_alive_id, uint8 *ip_pkt,
15673 	uint16 ip_pkt_len, uint8* src_mac, uint8* dst_mac, uint32 period_msec)
15674 {
15675 	const int		ETHERTYPE_LEN = 2;
15676 	char			*pbuf = NULL;
15677 	const char		*str;
15678 	wl_mkeep_alive_pkt_t	mkeep_alive_pkt;
15679 	wl_mkeep_alive_pkt_t	*mkeep_alive_pktp = NULL;
15680 	int			buf_len = 0;
15681 	int			str_len = 0;
15682 	int			res = BCME_ERROR;
15683 	int			len_bytes = 0;
15684 	int			i = 0;
15685 
15686 	/* ether frame to have both max IP pkt (256 bytes) and ether header */
15687 	char			*pmac_frame = NULL;
15688 	char			*pmac_frame_begin = NULL;
15689 
15690 	/*
15691 	 * The mkeep_alive packet is for STA interface only; if the bss is configured as AP,
15692 	 * dongle shall reject a mkeep_alive request.
15693 	 */
15694 	if (!dhd_support_sta_mode(dhd_pub))
15695 		return res;
15696 
15697 	DHD_TRACE(("%s execution\n", __FUNCTION__));
15698 
15699 	if ((pbuf = kzalloc(KA_TEMP_BUF_SIZE, GFP_KERNEL)) == NULL) {
15700 		DHD_ERROR(("failed to allocate buf with size %d\n", KA_TEMP_BUF_SIZE));
15701 		res = BCME_NOMEM;
15702 		return res;
15703 	}
15704 
15705 	if ((pmac_frame = kzalloc(KA_FRAME_SIZE, GFP_KERNEL)) == NULL) {
15706 		DHD_ERROR(("failed to allocate mac_frame with size %d\n", KA_FRAME_SIZE));
15707 		res = BCME_NOMEM;
15708 		goto exit;
15709 	}
15710 	pmac_frame_begin = pmac_frame;
15711 
15712 	/*
15713 	 * Get current mkeep-alive status.
15714 	 */
15715 	res = dhd_iovar(dhd_pub, 0, "mkeep_alive", &mkeep_alive_id, sizeof(mkeep_alive_id), pbuf,
15716 			KA_TEMP_BUF_SIZE, FALSE);
15717 	if (res < 0) {
15718 		DHD_ERROR(("%s: Get mkeep_alive failed (error=%d)\n", __FUNCTION__, res));
15719 		goto exit;
15720 	} else {
15721 		/* Check available ID whether it is occupied */
15722 		mkeep_alive_pktp = (wl_mkeep_alive_pkt_t *) pbuf;
15723 		if (dtoh32(mkeep_alive_pktp->period_msec != 0)) {
15724 			DHD_ERROR(("%s: Get mkeep_alive failed, ID %u is in use.\n",
15725 				__FUNCTION__, mkeep_alive_id));
15726 
15727 			/* Current occupied ID info */
15728 			DHD_ERROR(("%s: mkeep_alive\n", __FUNCTION__));
15729 			DHD_ERROR(("   Id    : %d\n"
15730 				"   Period: %d msec\n"
15731 				"   Length: %d\n"
15732 				"   Packet: 0x",
15733 				mkeep_alive_pktp->keep_alive_id,
15734 				dtoh32(mkeep_alive_pktp->period_msec),
15735 				dtoh16(mkeep_alive_pktp->len_bytes)));
15736 
15737 			for (i = 0; i < mkeep_alive_pktp->len_bytes; i++) {
15738 				DHD_ERROR(("%02x", mkeep_alive_pktp->data[i]));
15739 			}
15740 			DHD_ERROR(("\n"));
15741 
15742 			res = BCME_NOTFOUND;
15743 			goto exit;
15744 		}
15745 	}
15746 
15747 	/* Request the specified ID */
15748 	memset(&mkeep_alive_pkt, 0, sizeof(wl_mkeep_alive_pkt_t));
15749 	memset(pbuf, 0, KA_TEMP_BUF_SIZE);
15750 	str = "mkeep_alive";
15751 	str_len = strlen(str);
15752 	strncpy(pbuf, str, str_len);
15753 	pbuf[str_len] = '\0';
15754 
15755 	mkeep_alive_pktp = (wl_mkeep_alive_pkt_t *) (pbuf + str_len + 1);
15756 	mkeep_alive_pkt.period_msec = htod32(period_msec);
15757 	buf_len = str_len + 1;
15758 	mkeep_alive_pkt.version = htod16(WL_MKEEP_ALIVE_VERSION);
15759 	mkeep_alive_pkt.length = htod16(WL_MKEEP_ALIVE_FIXED_LEN);
15760 
15761 	/* ID assigned */
15762 	mkeep_alive_pkt.keep_alive_id = mkeep_alive_id;
15763 
15764 	buf_len += WL_MKEEP_ALIVE_FIXED_LEN;
15765 
15766 	/*
15767 	 * Build up Ethernet Frame
15768 	 */
15769 
15770 	/* Mapping dest mac addr */
15771 	memcpy(pmac_frame, dst_mac, ETHER_ADDR_LEN);
15772 	pmac_frame += ETHER_ADDR_LEN;
15773 
15774 	/* Mapping src mac addr */
15775 	memcpy(pmac_frame, src_mac, ETHER_ADDR_LEN);
15776 	pmac_frame += ETHER_ADDR_LEN;
15777 
15778 	/* Mapping Ethernet type (ETHERTYPE_IP: 0x0800) */
15779 	*(pmac_frame++) = 0x08;
15780 	*(pmac_frame++) = 0x00;
15781 
15782 	/* Mapping IP pkt */
15783 	memcpy(pmac_frame, ip_pkt, ip_pkt_len);
15784 	pmac_frame += ip_pkt_len;
15785 
15786 	/*
15787 	 * Length of ether frame (assume to be all hexa bytes)
15788 	 *     = src mac + dst mac + ether type + ip pkt len
15789 	 */
15790 	len_bytes = ETHER_ADDR_LEN*2 + ETHERTYPE_LEN + ip_pkt_len;
15791 	memcpy(mkeep_alive_pktp->data, pmac_frame_begin, len_bytes);
15792 	buf_len += len_bytes;
15793 	mkeep_alive_pkt.len_bytes = htod16(len_bytes);
15794 
15795 	/*
15796 	 * Keep-alive attributes are set in local variable (mkeep_alive_pkt), and
15797 	 * then memcpy'ed into buffer (mkeep_alive_pktp) since there is no
15798 	 * guarantee that the buffer is properly aligned.
15799 	 */
15800 	memcpy((char *)mkeep_alive_pktp, &mkeep_alive_pkt, WL_MKEEP_ALIVE_FIXED_LEN);
15801 
15802 	res = dhd_wl_ioctl_cmd(dhd_pub, WLC_SET_VAR, pbuf, buf_len, TRUE, 0);
15803 exit:
15804 	kfree(pmac_frame_begin);
15805 	kfree(pbuf);
15806 	return res;
15807 }
15808 
15809 int
dhd_dev_stop_mkeep_alive(dhd_pub_t * dhd_pub,uint8 mkeep_alive_id)15810 dhd_dev_stop_mkeep_alive(dhd_pub_t *dhd_pub, uint8 mkeep_alive_id)
15811 {
15812 	char			*pbuf;
15813 	wl_mkeep_alive_pkt_t	mkeep_alive_pkt;
15814 	wl_mkeep_alive_pkt_t	*mkeep_alive_pktp;
15815 	int			res = BCME_ERROR;
15816 	int			i;
15817 
15818 	/*
15819 	 * The mkeep_alive packet is for STA interface only; if the bss is configured as AP,
15820 	 * dongle shall reject a mkeep_alive request.
15821 	 */
15822 	if (!dhd_support_sta_mode(dhd_pub))
15823 		return res;
15824 
15825 	DHD_TRACE(("%s execution\n", __FUNCTION__));
15826 
15827 	/*
15828 	 * Get current mkeep-alive status. Skip ID 0 which is being used for NULL pkt.
15829 	 */
15830 	if ((pbuf = kmalloc(KA_TEMP_BUF_SIZE, GFP_KERNEL)) == NULL) {
15831 		DHD_ERROR(("failed to allocate buf with size %d\n", KA_TEMP_BUF_SIZE));
15832 		return res;
15833 	}
15834 
15835 	res = dhd_iovar(dhd_pub, 0, "mkeep_alive", &mkeep_alive_id,
15836 			sizeof(mkeep_alive_id), pbuf, KA_TEMP_BUF_SIZE, FALSE);
15837 	if (res < 0) {
15838 		DHD_ERROR(("%s: Get mkeep_alive failed (error=%d)\n", __FUNCTION__, res));
15839 		goto exit;
15840 	} else {
15841 		/* Check occupied ID */
15842 		mkeep_alive_pktp = (wl_mkeep_alive_pkt_t *) pbuf;
15843 		DHD_INFO(("%s: mkeep_alive\n", __FUNCTION__));
15844 		DHD_INFO(("   Id    : %d\n"
15845 			"   Period: %d msec\n"
15846 			"   Length: %d\n"
15847 			"   Packet: 0x",
15848 			mkeep_alive_pktp->keep_alive_id,
15849 			dtoh32(mkeep_alive_pktp->period_msec),
15850 			dtoh16(mkeep_alive_pktp->len_bytes)));
15851 
15852 		for (i = 0; i < mkeep_alive_pktp->len_bytes; i++) {
15853 			DHD_INFO(("%02x", mkeep_alive_pktp->data[i]));
15854 		}
15855 		DHD_INFO(("\n"));
15856 	}
15857 
15858 	/* Make it stop if available */
15859 	if (dtoh32(mkeep_alive_pktp->period_msec != 0)) {
15860 		DHD_INFO(("stop mkeep_alive on ID %d\n", mkeep_alive_id));
15861 		memset(&mkeep_alive_pkt, 0, sizeof(wl_mkeep_alive_pkt_t));
15862 
15863 		mkeep_alive_pkt.period_msec = 0;
15864 		mkeep_alive_pkt.version = htod16(WL_MKEEP_ALIVE_VERSION);
15865 		mkeep_alive_pkt.length = htod16(WL_MKEEP_ALIVE_FIXED_LEN);
15866 		mkeep_alive_pkt.keep_alive_id = mkeep_alive_id;
15867 
15868 		res = dhd_iovar(dhd_pub, 0, "mkeep_alive",
15869 				(char *)&mkeep_alive_pkt,
15870 				WL_MKEEP_ALIVE_FIXED_LEN, NULL, 0, TRUE);
15871 	} else {
15872 		DHD_ERROR(("%s: ID %u does not exist.\n", __FUNCTION__, mkeep_alive_id));
15873 		res = BCME_NOTFOUND;
15874 	}
15875 exit:
15876 	kfree(pbuf);
15877 	return res;
15878 }
15879 #endif /* KEEP_ALIVE */
15880 
15881 #if defined(PKT_FILTER_SUPPORT) && defined(APF)
_dhd_apf_lock_local(dhd_info_t * dhd)15882 static void _dhd_apf_lock_local(dhd_info_t *dhd)
15883 {
15884 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
15885 	if (dhd) {
15886 		mutex_lock(&dhd->dhd_apf_mutex);
15887 	}
15888 #endif
15889 }
15890 
_dhd_apf_unlock_local(dhd_info_t * dhd)15891 static void _dhd_apf_unlock_local(dhd_info_t *dhd)
15892 {
15893 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
15894 	if (dhd) {
15895 		mutex_unlock(&dhd->dhd_apf_mutex);
15896 	}
15897 #endif
15898 }
15899 
15900 static int
__dhd_apf_add_filter(struct net_device * ndev,uint32 filter_id,u8 * program,uint32 program_len)15901 __dhd_apf_add_filter(struct net_device *ndev, uint32 filter_id,
15902 	u8* program, uint32 program_len)
15903 {
15904 	dhd_info_t *dhd = DHD_DEV_INFO(ndev);
15905 	dhd_pub_t *dhdp = &dhd->pub;
15906 	wl_pkt_filter_t * pkt_filterp;
15907 	wl_apf_program_t *apf_program;
15908 	char *buf;
15909 	u32 cmd_len, buf_len;
15910 	int ifidx, ret;
15911 	gfp_t kflags;
15912 	char cmd[] = "pkt_filter_add";
15913 
15914 	ifidx = dhd_net2idx(dhd, ndev);
15915 	if (ifidx == DHD_BAD_IF) {
15916 		DHD_ERROR(("%s: bad ifidx\n", __FUNCTION__));
15917 		return -ENODEV;
15918 	}
15919 
15920 	cmd_len = sizeof(cmd);
15921 
15922 	/* Check if the program_len is more than the expected len
15923 	 * and if the program is NULL return from here.
15924 	 */
15925 	if ((program_len > WL_APF_PROGRAM_MAX_SIZE) || (program == NULL)) {
15926 		DHD_ERROR(("%s Invalid program_len: %d, program: %pK\n",
15927 				__FUNCTION__, program_len, program));
15928 		return -EINVAL;
15929 	}
15930 	buf_len = cmd_len + WL_PKT_FILTER_FIXED_LEN +
15931 		WL_APF_PROGRAM_FIXED_LEN + program_len;
15932 
15933 	kflags = in_atomic() ? GFP_ATOMIC : GFP_KERNEL;
15934 	buf = kzalloc(buf_len, kflags);
15935 	if (unlikely(!buf)) {
15936 		DHD_ERROR(("%s: MALLOC failure, %d bytes\n", __FUNCTION__, buf_len));
15937 		return -ENOMEM;
15938 	}
15939 
15940 	memcpy(buf, cmd, cmd_len);
15941 
15942 	pkt_filterp = (wl_pkt_filter_t *) (buf + cmd_len);
15943 	pkt_filterp->id = htod32(filter_id);
15944 	pkt_filterp->negate_match = htod32(FALSE);
15945 	pkt_filterp->type = htod32(WL_PKT_FILTER_TYPE_APF_MATCH);
15946 
15947 	apf_program = &pkt_filterp->u.apf_program;
15948 	apf_program->version = htod16(WL_APF_INTERNAL_VERSION);
15949 	apf_program->instr_len = htod16(program_len);
15950 	memcpy(apf_program->instrs, program, program_len);
15951 
15952 	ret = dhd_wl_ioctl_cmd(dhdp, WLC_SET_VAR, buf, buf_len, TRUE, ifidx);
15953 	if (unlikely(ret)) {
15954 		DHD_ERROR(("%s: failed to add APF filter, id=%d, ret=%d\n",
15955 			__FUNCTION__, filter_id, ret));
15956 	}
15957 
15958 	if (buf) {
15959 		kfree(buf);
15960 	}
15961 	return ret;
15962 }
15963 
15964 static int
__dhd_apf_config_filter(struct net_device * ndev,uint32 filter_id,uint32 mode,uint32 enable)15965 __dhd_apf_config_filter(struct net_device *ndev, uint32 filter_id,
15966 	uint32 mode, uint32 enable)
15967 {
15968 	dhd_info_t *dhd = DHD_DEV_INFO(ndev);
15969 	dhd_pub_t *dhdp = &dhd->pub;
15970 	wl_pkt_filter_enable_t * pkt_filterp;
15971 	char *buf;
15972 	u32 cmd_len, buf_len;
15973 	int ifidx, ret;
15974 	gfp_t kflags;
15975 	char cmd[] = "pkt_filter_enable";
15976 
15977 	ifidx = dhd_net2idx(dhd, ndev);
15978 	if (ifidx == DHD_BAD_IF) {
15979 		DHD_ERROR(("%s: bad ifidx\n", __FUNCTION__));
15980 		return -ENODEV;
15981 	}
15982 
15983 	cmd_len = sizeof(cmd);
15984 	buf_len = cmd_len + sizeof(*pkt_filterp);
15985 
15986 	kflags = in_atomic() ? GFP_ATOMIC : GFP_KERNEL;
15987 	buf = kzalloc(buf_len, kflags);
15988 	if (unlikely(!buf)) {
15989 		DHD_ERROR(("%s: MALLOC failure, %d bytes\n", __FUNCTION__, buf_len));
15990 		return -ENOMEM;
15991 	}
15992 
15993 	memcpy(buf, cmd, cmd_len);
15994 
15995 	pkt_filterp = (wl_pkt_filter_enable_t *) (buf + cmd_len);
15996 	pkt_filterp->id = htod32(filter_id);
15997 	pkt_filterp->enable = htod32(enable);
15998 
15999 	ret = dhd_wl_ioctl_cmd(dhdp, WLC_SET_VAR, buf, buf_len, TRUE, ifidx);
16000 	if (unlikely(ret)) {
16001 		DHD_ERROR(("%s: failed to enable APF filter, id=%d, ret=%d\n",
16002 			__FUNCTION__, filter_id, ret));
16003 		goto exit;
16004 	}
16005 
16006 	ret = dhd_wl_ioctl_set_intiovar(dhdp, "pkt_filter_mode", dhd_master_mode,
16007 		WLC_SET_VAR, TRUE, ifidx);
16008 	if (unlikely(ret)) {
16009 		DHD_ERROR(("%s: failed to set APF filter mode, id=%d, ret=%d\n",
16010 			__FUNCTION__, filter_id, ret));
16011 	}
16012 
16013 exit:
16014 	if (buf) {
16015 		kfree(buf);
16016 	}
16017 	return ret;
16018 }
16019 
16020 static int
__dhd_apf_delete_filter(struct net_device * ndev,uint32 filter_id)16021 __dhd_apf_delete_filter(struct net_device *ndev, uint32 filter_id)
16022 {
16023 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(ndev);
16024 	dhd_pub_t *dhdp = &dhd->pub;
16025 	int ifidx, ret;
16026 
16027 	ifidx = dhd_net2idx(dhd, ndev);
16028 	if (ifidx == DHD_BAD_IF) {
16029 		DHD_ERROR(("%s: bad ifidx\n", __FUNCTION__));
16030 		return -ENODEV;
16031 	}
16032 
16033 	ret = dhd_wl_ioctl_set_intiovar(dhdp, "pkt_filter_delete",
16034 		htod32(filter_id), WLC_SET_VAR, TRUE, ifidx);
16035 	if (unlikely(ret)) {
16036 		DHD_ERROR(("%s: failed to delete APF filter, id=%d, ret=%d\n",
16037 			__FUNCTION__, filter_id, ret));
16038 	}
16039 
16040 	return ret;
16041 }
16042 
dhd_apf_lock(struct net_device * dev)16043 void dhd_apf_lock(struct net_device *dev)
16044 {
16045 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16046 	_dhd_apf_lock_local(dhd);
16047 }
16048 
dhd_apf_unlock(struct net_device * dev)16049 void dhd_apf_unlock(struct net_device *dev)
16050 {
16051 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16052 	_dhd_apf_unlock_local(dhd);
16053 }
16054 
16055 int
dhd_dev_apf_get_version(struct net_device * ndev,uint32 * version)16056 dhd_dev_apf_get_version(struct net_device *ndev, uint32 *version)
16057 {
16058 	dhd_info_t *dhd = DHD_DEV_INFO(ndev);
16059 	dhd_pub_t *dhdp = &dhd->pub;
16060 	int ifidx, ret;
16061 
16062 	if (!FW_SUPPORTED(dhdp, apf)) {
16063 		DHD_ERROR(("%s: firmware doesn't support APF\n", __FUNCTION__));
16064 
16065 		/*
16066 		 * Notify Android framework that APF is not supported by setting
16067 		 * version as zero.
16068 		 */
16069 		*version = 0;
16070 		return BCME_OK;
16071 	}
16072 
16073 	ifidx = dhd_net2idx(dhd, ndev);
16074 	if (ifidx == DHD_BAD_IF) {
16075 		DHD_ERROR(("%s: bad ifidx\n", __FUNCTION__));
16076 		return -ENODEV;
16077 	}
16078 
16079 	ret = dhd_wl_ioctl_get_intiovar(dhdp, "apf_ver", version,
16080 		WLC_GET_VAR, FALSE, ifidx);
16081 	if (unlikely(ret)) {
16082 		DHD_ERROR(("%s: failed to get APF version, ret=%d\n",
16083 			__FUNCTION__, ret));
16084 	}
16085 
16086 	return ret;
16087 }
16088 
16089 int
dhd_dev_apf_get_max_len(struct net_device * ndev,uint32 * max_len)16090 dhd_dev_apf_get_max_len(struct net_device *ndev, uint32 *max_len)
16091 {
16092 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(ndev);
16093 	dhd_pub_t *dhdp = &dhd->pub;
16094 	int ifidx, ret;
16095 
16096 	if (!FW_SUPPORTED(dhdp, apf)) {
16097 		DHD_ERROR(("%s: firmware doesn't support APF\n", __FUNCTION__));
16098 		*max_len = 0;
16099 		return BCME_OK;
16100 	}
16101 
16102 	ifidx = dhd_net2idx(dhd, ndev);
16103 	if (ifidx == DHD_BAD_IF) {
16104 		DHD_ERROR(("%s bad ifidx\n", __FUNCTION__));
16105 		return -ENODEV;
16106 	}
16107 
16108 	ret = dhd_wl_ioctl_get_intiovar(dhdp, "apf_size_limit", max_len,
16109 		WLC_GET_VAR, FALSE, ifidx);
16110 	if (unlikely(ret)) {
16111 		DHD_ERROR(("%s: failed to get APF size limit, ret=%d\n",
16112 			__FUNCTION__, ret));
16113 	}
16114 
16115 	return ret;
16116 }
16117 
16118 int
dhd_dev_apf_add_filter(struct net_device * ndev,u8 * program,uint32 program_len)16119 dhd_dev_apf_add_filter(struct net_device *ndev, u8* program,
16120 	uint32 program_len)
16121 {
16122 	dhd_info_t *dhd = DHD_DEV_INFO(ndev);
16123 	dhd_pub_t *dhdp = &dhd->pub;
16124 	int ret;
16125 
16126 	DHD_APF_LOCK(ndev);
16127 
16128 	/* delete, if filter already exists */
16129 	if (dhdp->apf_set) {
16130 		ret = __dhd_apf_delete_filter(ndev, PKT_FILTER_APF_ID);
16131 		if (unlikely(ret)) {
16132 			goto exit;
16133 		}
16134 		dhdp->apf_set = FALSE;
16135 	}
16136 
16137 	ret = __dhd_apf_add_filter(ndev, PKT_FILTER_APF_ID, program, program_len);
16138 	if (ret) {
16139 		goto exit;
16140 	}
16141 	dhdp->apf_set = TRUE;
16142 
16143 	if (dhdp->in_suspend && dhdp->apf_set && !(dhdp->op_mode & DHD_FLAG_HOSTAP_MODE)) {
16144 		/* Driver is still in (early) suspend state, enable APF filter back */
16145 		ret = __dhd_apf_config_filter(ndev, PKT_FILTER_APF_ID,
16146 			PKT_FILTER_MODE_FORWARD_ON_MATCH, TRUE);
16147 	}
16148 exit:
16149 	DHD_APF_UNLOCK(ndev);
16150 
16151 	return ret;
16152 }
16153 
16154 int
dhd_dev_apf_enable_filter(struct net_device * ndev)16155 dhd_dev_apf_enable_filter(struct net_device *ndev)
16156 {
16157 	dhd_info_t *dhd = DHD_DEV_INFO(ndev);
16158 	dhd_pub_t *dhdp = &dhd->pub;
16159 	int ret = 0;
16160 
16161 	DHD_APF_LOCK(ndev);
16162 
16163 	if (dhdp->apf_set && !(dhdp->op_mode & DHD_FLAG_HOSTAP_MODE)) {
16164 		ret = __dhd_apf_config_filter(ndev, PKT_FILTER_APF_ID,
16165 			PKT_FILTER_MODE_FORWARD_ON_MATCH, TRUE);
16166 	}
16167 
16168 	DHD_APF_UNLOCK(ndev);
16169 
16170 	return ret;
16171 }
16172 
16173 int
dhd_dev_apf_disable_filter(struct net_device * ndev)16174 dhd_dev_apf_disable_filter(struct net_device *ndev)
16175 {
16176 	dhd_info_t *dhd = DHD_DEV_INFO(ndev);
16177 	dhd_pub_t *dhdp = &dhd->pub;
16178 	int ret = 0;
16179 
16180 	DHD_APF_LOCK(ndev);
16181 
16182 	if (dhdp->apf_set) {
16183 		ret = __dhd_apf_config_filter(ndev, PKT_FILTER_APF_ID,
16184 			PKT_FILTER_MODE_FORWARD_ON_MATCH, FALSE);
16185 	}
16186 
16187 	DHD_APF_UNLOCK(ndev);
16188 
16189 	return ret;
16190 }
16191 
16192 int
dhd_dev_apf_delete_filter(struct net_device * ndev)16193 dhd_dev_apf_delete_filter(struct net_device *ndev)
16194 {
16195 	dhd_info_t *dhd = DHD_DEV_INFO(ndev);
16196 	dhd_pub_t *dhdp = &dhd->pub;
16197 	int ret = 0;
16198 
16199 	DHD_APF_LOCK(ndev);
16200 
16201 	if (dhdp->apf_set) {
16202 		ret = __dhd_apf_delete_filter(ndev, PKT_FILTER_APF_ID);
16203 		if (!ret) {
16204 			dhdp->apf_set = FALSE;
16205 		}
16206 	}
16207 
16208 	DHD_APF_UNLOCK(ndev);
16209 
16210 	return ret;
16211 }
16212 #endif /* PKT_FILTER_SUPPORT && APF */
16213 
16214 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
dhd_hang_process(void * dhd_info,void * event_info,u8 event)16215 static void dhd_hang_process(void *dhd_info, void *event_info, u8 event)
16216 {
16217 	dhd_info_t *dhd;
16218 	struct net_device *dev;
16219 
16220 	dhd = (dhd_info_t *)dhd_info;
16221 	if (!dhd || !dhd->iflist[0])
16222 		return;
16223 	dev = dhd->iflist[0]->net;
16224 
16225 	if (dev) {
16226 		/*
16227 		 * For HW2, dev_close need to be done to recover
16228 		 * from upper layer after hang. For Interposer skip
16229 		 * dev_close so that dhd iovars can be used to take
16230 		 * socramdump after crash, also skip for HW4 as
16231 		 * handling of hang event is different
16232 		 */
16233 #if !defined(CUSTOMER_HW2_INTERPOSER)
16234 		rtnl_lock();
16235 		dev_close(dev);
16236 		rtnl_unlock();
16237 #endif
16238 #if defined(WL_WIRELESS_EXT)
16239 		wl_iw_send_priv_event(dev, "HANG");
16240 #endif
16241 #if defined(WL_CFG80211)
16242 		wl_cfg80211_hang(dev, WLAN_REASON_UNSPECIFIED);
16243 #endif
16244 	}
16245 }
16246 
16247 #ifdef EXYNOS_PCIE_LINKDOWN_RECOVERY
16248 extern dhd_pub_t *link_recovery;
dhd_host_recover_link(void)16249 void dhd_host_recover_link(void)
16250 {
16251 	DHD_ERROR(("****** %s ******\n", __FUNCTION__));
16252 	link_recovery->hang_reason = HANG_REASON_PCIE_LINK_DOWN;
16253 	dhd_bus_set_linkdown(link_recovery, TRUE);
16254 	dhd_os_send_hang_message(link_recovery);
16255 }
16256 EXPORT_SYMBOL(dhd_host_recover_link);
16257 #endif /* EXYNOS_PCIE_LINKDOWN_RECOVERY */
16258 
dhd_os_send_hang_message(dhd_pub_t * dhdp)16259 int dhd_os_send_hang_message(dhd_pub_t *dhdp)
16260 {
16261 	int ret = 0;
16262 	if (dhdp) {
16263 #if defined(DHD_HANG_SEND_UP_TEST)
16264 		if (dhdp->req_hang_type) {
16265 			DHD_ERROR(("%s, Clear HANG test request 0x%x\n",
16266 				__FUNCTION__, dhdp->req_hang_type));
16267 			dhdp->req_hang_type = 0;
16268 		}
16269 #endif /* DHD_HANG_SEND_UP_TEST */
16270 
16271 		if (!dhdp->hang_was_sent) {
16272 #if defined(CONFIG_BCM_DETECT_CONSECUTIVE_HANG)
16273 			dhdp->hang_counts++;
16274 			if (dhdp->hang_counts >= MAX_CONSECUTIVE_HANG_COUNTS) {
16275 				DHD_ERROR(("%s, Consecutive hang from Dongle :%u\n",
16276 					__func__, dhdp->hang_counts));
16277 				BUG_ON(1);
16278 			}
16279 #endif /* CONFIG_BCM_DETECT_CONSECUTIVE_HANG */
16280 #ifdef DHD_DEBUG_UART
16281 			/* If PCIe lane has broken, execute the debug uart application
16282 			 * to gether a ramdump data from dongle via uart
16283 			 */
16284 			if (!dhdp->info->duart_execute) {
16285 				dhd_deferred_schedule_work(dhdp->info->dhd_deferred_wq,
16286 					(void *)dhdp, DHD_WQ_WORK_DEBUG_UART_DUMP,
16287 					dhd_debug_uart_exec_rd, DHD_WQ_WORK_PRIORITY_HIGH);
16288 			}
16289 #endif	/* DHD_DEBUG_UART */
16290 			dhdp->hang_was_sent = 1;
16291 #ifdef BT_OVER_SDIO
16292 			dhdp->is_bt_recovery_required = TRUE;
16293 #endif
16294 			dhd_deferred_schedule_work(dhdp->info->dhd_deferred_wq, (void *)dhdp,
16295 				DHD_WQ_WORK_HANG_MSG, dhd_hang_process, DHD_WQ_WORK_PRIORITY_HIGH);
16296 			DHD_ERROR(("%s: Event HANG send up due to  re=%d te=%d s=%d\n", __FUNCTION__,
16297 				dhdp->rxcnt_timeout, dhdp->txcnt_timeout, dhdp->busstate));
16298 		}
16299 	}
16300 	return ret;
16301 }
16302 
net_os_send_hang_message(struct net_device * dev)16303 int net_os_send_hang_message(struct net_device *dev)
16304 {
16305 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16306 	int ret = 0;
16307 
16308 	if (dhd) {
16309 		/* Report FW problem when enabled */
16310 		if (dhd->pub.hang_report) {
16311 #ifdef BT_OVER_SDIO
16312 			if (netif_running(dev)) {
16313 #endif /* BT_OVER_SDIO */
16314 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
16315 				ret = dhd_os_send_hang_message(&dhd->pub);
16316 #else
16317 				ret = wl_cfg80211_hang(dev, WLAN_REASON_UNSPECIFIED);
16318 #endif
16319 #ifdef BT_OVER_SDIO
16320 			}
16321 			DHD_ERROR(("%s: HANG -> Reset BT\n", __FUNCTION__));
16322 			bcmsdh_btsdio_process_dhd_hang_notification(!netif_running(dev));
16323 #endif /* BT_OVER_SDIO */
16324 		} else {
16325 			DHD_ERROR(("%s: FW HANG ignored (for testing purpose) and not sent up\n",
16326 				__FUNCTION__));
16327 		}
16328 	}
16329 	return ret;
16330 }
16331 
net_os_send_hang_message_reason(struct net_device * dev,const char * string_num)16332 int net_os_send_hang_message_reason(struct net_device *dev, const char *string_num)
16333 {
16334 	dhd_info_t *dhd = NULL;
16335 	dhd_pub_t *dhdp = NULL;
16336 	int reason;
16337 
16338 	dhd = DHD_DEV_INFO(dev);
16339 	if (dhd) {
16340 		dhdp = &dhd->pub;
16341 	}
16342 
16343 	if (!dhd || !dhdp) {
16344 		return 0;
16345 	}
16346 
16347 	reason = bcm_strtoul(string_num, NULL, 0);
16348 	DHD_INFO(("%s: Enter, reason=0x%x\n", __FUNCTION__, reason));
16349 
16350 	if ((reason <= HANG_REASON_MASK) || (reason >= HANG_REASON_MAX)) {
16351 		reason = 0;
16352 	}
16353 
16354 	dhdp->hang_reason = reason;
16355 
16356 	return net_os_send_hang_message(dev);
16357 }
16358 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27) && OEM_ANDROID */
16359 
16360 
dhd_net_wifi_platform_set_power(struct net_device * dev,bool on,unsigned long delay_msec)16361 int dhd_net_wifi_platform_set_power(struct net_device *dev, bool on, unsigned long delay_msec)
16362 {
16363 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16364 	return wifi_platform_set_power(dhd->adapter, on, delay_msec);
16365 }
16366 
dhd_force_country_change(struct net_device * dev)16367 bool dhd_force_country_change(struct net_device *dev)
16368 {
16369 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16370 
16371 	if (dhd && dhd->pub.up)
16372 		return dhd->pub.force_country_change;
16373 	return FALSE;
16374 }
16375 
dhd_get_customized_country_code(struct net_device * dev,char * country_iso_code,wl_country_t * cspec)16376 void dhd_get_customized_country_code(struct net_device *dev, char *country_iso_code,
16377 	wl_country_t *cspec)
16378 {
16379 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16380 #if defined(DHD_BLOB_EXISTENCE_CHECK)
16381 	if (!dhd->pub.is_blob)
16382 #endif /* DHD_BLOB_EXISTENCE_CHECK */
16383 	{
16384 #if defined(CUSTOM_COUNTRY_CODE)
16385 		get_customized_country_code(dhd->adapter, country_iso_code, cspec,
16386 			dhd->pub.dhd_cflags);
16387 #else
16388 		get_customized_country_code(dhd->adapter, country_iso_code, cspec);
16389 #endif /* CUSTOM_COUNTRY_CODE */
16390 	}
16391 
16392 	BCM_REFERENCE(dhd);
16393 }
16394 
dhd_bus_country_set(struct net_device * dev,wl_country_t * cspec,bool notify)16395 void dhd_bus_country_set(struct net_device *dev, wl_country_t *cspec, bool notify)
16396 {
16397 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16398 #ifdef WL_CFG80211
16399 	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
16400 #endif
16401 
16402 	if (dhd && dhd->pub.up) {
16403 		memcpy(&dhd->pub.dhd_cspec, cspec, sizeof(wl_country_t));
16404 #ifdef WL_CFG80211
16405 		wl_update_wiphybands(cfg, notify);
16406 #endif
16407 	}
16408 }
16409 
dhd_bus_band_set(struct net_device * dev,uint band)16410 void dhd_bus_band_set(struct net_device *dev, uint band)
16411 {
16412 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16413 #ifdef WL_CFG80211
16414 	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
16415 #endif
16416 	if (dhd && dhd->pub.up) {
16417 #ifdef WL_CFG80211
16418 		wl_update_wiphybands(cfg, true);
16419 #endif
16420 	}
16421 }
16422 
dhd_net_set_fw_path(struct net_device * dev,char * fw)16423 int dhd_net_set_fw_path(struct net_device *dev, char *fw)
16424 {
16425 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16426 
16427 	if (!fw || fw[0] == '\0')
16428 		return -EINVAL;
16429 
16430 	strncpy(dhd->fw_path, fw, sizeof(dhd->fw_path) - 1);
16431 	dhd->fw_path[sizeof(dhd->fw_path)-1] = '\0';
16432 
16433 #if defined(SOFTAP)
16434 	if (strstr(fw, "apsta") != NULL) {
16435 		DHD_INFO(("GOT APSTA FIRMWARE\n"));
16436 		ap_fw_loaded = TRUE;
16437 	} else {
16438 		DHD_INFO(("GOT STA FIRMWARE\n"));
16439 		ap_fw_loaded = FALSE;
16440 	}
16441 #endif
16442 	return 0;
16443 }
16444 
dhd_net_if_lock(struct net_device * dev)16445 void dhd_net_if_lock(struct net_device *dev)
16446 {
16447 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16448 	dhd_net_if_lock_local(dhd);
16449 }
16450 
dhd_net_if_unlock(struct net_device * dev)16451 void dhd_net_if_unlock(struct net_device *dev)
16452 {
16453 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16454 	dhd_net_if_unlock_local(dhd);
16455 }
16456 
dhd_net_if_lock_local(dhd_info_t * dhd)16457 static void dhd_net_if_lock_local(dhd_info_t *dhd)
16458 {
16459 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
16460 	if (dhd)
16461 		mutex_lock(&dhd->dhd_net_if_mutex);
16462 #endif
16463 }
16464 
dhd_net_if_unlock_local(dhd_info_t * dhd)16465 static void dhd_net_if_unlock_local(dhd_info_t *dhd)
16466 {
16467 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
16468 	if (dhd)
16469 		mutex_unlock(&dhd->dhd_net_if_mutex);
16470 #endif
16471 }
16472 
dhd_suspend_lock(dhd_pub_t * pub)16473 static void dhd_suspend_lock(dhd_pub_t *pub)
16474 {
16475 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
16476 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
16477 	if (dhd)
16478 		mutex_lock(&dhd->dhd_suspend_mutex);
16479 #endif
16480 }
16481 
dhd_suspend_unlock(dhd_pub_t * pub)16482 static void dhd_suspend_unlock(dhd_pub_t *pub)
16483 {
16484 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25))
16485 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
16486 	if (dhd)
16487 		mutex_unlock(&dhd->dhd_suspend_mutex);
16488 #endif
16489 }
16490 
dhd_os_general_spin_lock(dhd_pub_t * pub)16491 unsigned long dhd_os_general_spin_lock(dhd_pub_t *pub)
16492 {
16493 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
16494 	unsigned long flags = 0;
16495 
16496 	if (dhd)
16497 		spin_lock_irqsave(&dhd->dhd_lock, flags);
16498 
16499 	return flags;
16500 }
16501 
dhd_os_general_spin_unlock(dhd_pub_t * pub,unsigned long flags)16502 void dhd_os_general_spin_unlock(dhd_pub_t *pub, unsigned long flags)
16503 {
16504 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
16505 
16506 	if (dhd)
16507 		spin_unlock_irqrestore(&dhd->dhd_lock, flags);
16508 }
16509 
16510 /* Linux specific multipurpose spinlock API */
16511 void *
dhd_os_spin_lock_init(osl_t * osh)16512 dhd_os_spin_lock_init(osl_t *osh)
16513 {
16514 	/* Adding 4 bytes since the sizeof(spinlock_t) could be 0 */
16515 	/* if CONFIG_SMP and CONFIG_DEBUG_SPINLOCK are not defined */
16516 	/* and this results in kernel asserts in internal builds */
16517 	spinlock_t * lock = MALLOC(osh, sizeof(spinlock_t) + 4);
16518 	if (lock)
16519 		spin_lock_init(lock);
16520 	return ((void *)lock);
16521 }
16522 void
dhd_os_spin_lock_deinit(osl_t * osh,void * lock)16523 dhd_os_spin_lock_deinit(osl_t *osh, void *lock)
16524 {
16525 	if (lock)
16526 		MFREE(osh, lock, sizeof(spinlock_t) + 4);
16527 }
16528 unsigned long
dhd_os_spin_lock(void * lock)16529 dhd_os_spin_lock(void *lock)
16530 {
16531 	unsigned long flags = 0;
16532 
16533 	if (lock)
16534 		spin_lock_irqsave((spinlock_t *)lock, flags);
16535 
16536 	return flags;
16537 }
16538 void
dhd_os_spin_unlock(void * lock,unsigned long flags)16539 dhd_os_spin_unlock(void *lock, unsigned long flags)
16540 {
16541 	if (lock)
16542 		spin_unlock_irqrestore((spinlock_t *)lock, flags);
16543 }
16544 
16545 static int
dhd_get_pend_8021x_cnt(dhd_info_t * dhd)16546 dhd_get_pend_8021x_cnt(dhd_info_t *dhd)
16547 {
16548 	return (atomic_read(&dhd->pend_8021x_cnt));
16549 }
16550 
16551 #define MAX_WAIT_FOR_8021X_TX	100
16552 
16553 int
dhd_wait_pend8021x(struct net_device * dev)16554 dhd_wait_pend8021x(struct net_device *dev)
16555 {
16556 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16557 	int timeout = msecs_to_jiffies(10);
16558 	int ntimes = MAX_WAIT_FOR_8021X_TX;
16559 	int pend = dhd_get_pend_8021x_cnt(dhd);
16560 
16561 	while (ntimes && pend) {
16562 		if (pend) {
16563 			set_current_state(TASK_INTERRUPTIBLE);
16564 			DHD_PERIM_UNLOCK(&dhd->pub);
16565 			schedule_timeout(timeout);
16566 			DHD_PERIM_LOCK(&dhd->pub);
16567 			set_current_state(TASK_RUNNING);
16568 			ntimes--;
16569 		}
16570 		pend = dhd_get_pend_8021x_cnt(dhd);
16571 	}
16572 	if (ntimes == 0)
16573 	{
16574 		atomic_set(&dhd->pend_8021x_cnt, 0);
16575 		WL_MSG(dev->name, "TIMEOUT\n");
16576 	}
16577 	return pend;
16578 }
16579 
16580 #if defined(DHD_DEBUG)
write_file(const char * file_name,uint32 flags,uint8 * buf,int size)16581 int write_file(const char * file_name, uint32 flags, uint8 *buf, int size)
16582 {
16583 	int ret = 0;
16584 	struct file *fp = NULL;
16585 	mm_segment_t old_fs;
16586 	loff_t pos = 0;
16587 	/* change to KERNEL_DS address limit */
16588 	old_fs = get_fs();
16589 	set_fs(KERNEL_DS);
16590 
16591 	/* open file to write */
16592 	fp = filp_open(file_name, flags, 0664);
16593 	if (IS_ERR(fp)) {
16594 		DHD_ERROR(("open file error, err = %ld\n", PTR_ERR(fp)));
16595 		ret = -1;
16596 		goto exit;
16597 	}
16598 
16599 	/* Write buf to file */
16600 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
16601 	ret = kernel_write(fp, buf, size, &pos);
16602 #else
16603 	ret = vfs_write(fp, buf, size, &pos);
16604 #endif
16605 	if (ret < 0) {
16606 		DHD_ERROR(("write file error, err = %d\n", ret));
16607 		goto exit;
16608 	}
16609 
16610 	/* Sync file from filesystem to physical media */
16611 	ret = vfs_fsync(fp, 0);
16612 	if (ret < 0) {
16613 		DHD_ERROR(("sync file error, error = %d\n", ret));
16614 		goto exit;
16615 	}
16616 	ret = BCME_OK;
16617 
16618 exit:
16619 	/* close file before return */
16620 	if (!IS_ERR(fp))
16621 		filp_close(fp, current->files);
16622 
16623 	/* restore previous address limit */
16624 	set_fs(old_fs);
16625 
16626 	return ret;
16627 }
16628 #endif
16629 
16630 #ifdef DHD_DEBUG
16631 static void
dhd_convert_memdump_type_to_str(uint32 type,char * buf)16632 dhd_convert_memdump_type_to_str(uint32 type, char *buf)
16633 {
16634 	char *type_str = NULL;
16635 
16636 	switch (type) {
16637 		case DUMP_TYPE_RESUMED_ON_TIMEOUT:
16638 			type_str = "resumed_on_timeout";
16639 			break;
16640 		case DUMP_TYPE_D3_ACK_TIMEOUT:
16641 			type_str = "D3_ACK_timeout";
16642 			break;
16643 		case DUMP_TYPE_DONGLE_TRAP:
16644 			type_str = "Dongle_Trap";
16645 			break;
16646 		case DUMP_TYPE_MEMORY_CORRUPTION:
16647 			type_str = "Memory_Corruption";
16648 			break;
16649 		case DUMP_TYPE_PKTID_AUDIT_FAILURE:
16650 			type_str = "PKTID_AUDIT_Fail";
16651 			break;
16652 		case DUMP_TYPE_PKTID_INVALID:
16653 			type_str = "PKTID_INVALID";
16654 			break;
16655 		case DUMP_TYPE_SCAN_TIMEOUT:
16656 			type_str = "SCAN_timeout";
16657 			break;
16658 		case DUMP_TYPE_JOIN_TIMEOUT:
16659 			type_str = "JOIN_timeout";
16660 			break;
16661 		case DUMP_TYPE_SCAN_BUSY:
16662 			type_str = "SCAN_Busy";
16663 			break;
16664 		case DUMP_TYPE_BY_SYSDUMP:
16665 			type_str = "BY_SYSDUMP";
16666 			break;
16667 		case DUMP_TYPE_BY_LIVELOCK:
16668 			type_str = "BY_LIVELOCK";
16669 			break;
16670 		case DUMP_TYPE_AP_LINKUP_FAILURE:
16671 			type_str = "BY_AP_LINK_FAILURE";
16672 			break;
16673 		case DUMP_TYPE_AP_ABNORMAL_ACCESS:
16674 			type_str = "INVALID_ACCESS";
16675 			break;
16676 		case DUMP_TYPE_CFG_VENDOR_TRIGGERED:
16677 			type_str = "CFG_VENDOR_TRIGGERED";
16678 			break;
16679 		case DUMP_TYPE_RESUMED_ON_TIMEOUT_RX:
16680 			type_str = "ERROR_RX_TIMED_OUT";
16681 			break;
16682 		case DUMP_TYPE_RESUMED_ON_TIMEOUT_TX:
16683 			type_str = "ERROR_TX_TIMED_OUT";
16684 			break;
16685 		case DUMP_TYPE_RESUMED_ON_INVALID_RING_RDWR:
16686 			type_str = "BY_INVALID_RING_RDWR";
16687 			break;
16688 		case DUMP_TYPE_DONGLE_HOST_EVENT:
16689 			type_str = "BY_DONGLE_HOST_EVENT";
16690 			break;
16691 		case DUMP_TYPE_TRANS_ID_MISMATCH:
16692 			type_str = "BY_TRANS_ID_MISMATCH";
16693 			break;
16694 		case DUMP_TYPE_HANG_ON_IFACE_OP_FAIL:
16695 			type_str = "HANG_IFACE_OP_FAIL";
16696 			break;
16697 #ifdef SUPPORT_LINKDOWN_RECOVERY
16698 		case DUMP_TYPE_READ_SHM_FAIL:
16699 			type_str = "READ_SHM_FAIL";
16700 			break;
16701 #endif /* SUPPORT_LINKDOWN_RECOVERY */
16702 		default:
16703 			type_str = "Unknown_type";
16704 			break;
16705 	}
16706 
16707 	strncpy(buf, type_str, strlen(type_str));
16708 	buf[strlen(type_str)] = 0;
16709 }
16710 
16711 int
write_dump_to_file(dhd_pub_t * dhd,uint8 * buf,int size,char * fname)16712 write_dump_to_file(dhd_pub_t *dhd, uint8 *buf, int size, char *fname)
16713 {
16714 	int ret = 0;
16715 	char memdump_path[128];
16716 	char memdump_type[32];
16717 	struct osl_timespec curtime;
16718 	uint32 file_mode;
16719 
16720 	/* Init file name */
16721 	memset(memdump_path, 0, sizeof(memdump_path));
16722 	memset(memdump_type, 0, sizeof(memdump_type));
16723 	osl_do_gettimeofday(&curtime);
16724 	dhd_convert_memdump_type_to_str(dhd->memdump_type, memdump_type);
16725 #ifdef CUSTOMER_HW4_DEBUG
16726 	snprintf(memdump_path, sizeof(memdump_path), "%s%s_%s_%ld.%ld",
16727 		DHD_COMMON_DUMP_PATH, fname, memdump_type,
16728 		(unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
16729 	file_mode = O_CREAT | O_WRONLY | O_SYNC;
16730 #elif defined(CUSTOMER_HW2)
16731 	snprintf(memdump_path, sizeof(memdump_path), "%s%s_%s_%ld.%ld",
16732 		"/data/misc/wifi/", fname, memdump_type,
16733 		(unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
16734 	file_mode = O_CREAT | O_WRONLY | O_SYNC;
16735 #elif (defined(BOARD_PANDA) || defined(__ARM_ARCH_7A__))
16736 	snprintf(memdump_path, sizeof(memdump_path), "%s%s_%s_%ld.%ld",
16737 		"/data/misc/wifi/", fname, memdump_type,
16738 		(unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
16739 	file_mode = O_CREAT | O_WRONLY;
16740 #else
16741 	snprintf(memdump_path, sizeof(memdump_path), "%s%s_%s_%ld.%ld",
16742 		"/installmedia/", fname, memdump_type,
16743 		(unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
16744 	/* Extra flags O_DIRECT and O_SYNC are required for Brix Android, as we are
16745 	 * calling BUG_ON immediately after collecting the socram dump.
16746 	 * So the file write operation should directly write the contents into the
16747 	 * file instead of caching it. O_TRUNC flag ensures that file will be re-written
16748 	 * instead of appending.
16749 	 */
16750 	file_mode = O_CREAT | O_WRONLY | O_SYNC;
16751 	{
16752 		struct file *fp = filp_open(memdump_path, file_mode, 0664);
16753 		/* Check if it is live Brix image having /installmedia, else use /data */
16754 		if (IS_ERR(fp)) {
16755 			DHD_ERROR(("open file %s, try /data/\n", memdump_path));
16756 			snprintf(memdump_path, sizeof(memdump_path), "%s%s_%s_%ld.%ld",
16757 				"/data/", fname, memdump_type,
16758 				(unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
16759 		} else {
16760 			filp_close(fp, NULL);
16761 		}
16762 	}
16763 #endif /* CUSTOMER_HW4_DEBUG */
16764 
16765 	/* print SOCRAM dump file path */
16766 	DHD_ERROR(("%s: file_path = %s\n", __FUNCTION__, memdump_path));
16767 
16768 	/* Write file */
16769 	ret = write_file(memdump_path, file_mode, buf, size);
16770 
16771 	return ret;
16772 }
16773 #endif /* DHD_DEBUG */
16774 
dhd_os_wake_lock_timeout(dhd_pub_t * pub)16775 int dhd_os_wake_lock_timeout(dhd_pub_t *pub)
16776 {
16777 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
16778 	unsigned long flags;
16779 	int ret = 0;
16780 
16781 	if (dhd && (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
16782 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
16783 		ret = dhd->wakelock_rx_timeout_enable > dhd->wakelock_ctrl_timeout_enable ?
16784 			dhd->wakelock_rx_timeout_enable : dhd->wakelock_ctrl_timeout_enable;
16785 #ifdef CONFIG_HAS_WAKELOCK
16786 		if (dhd->wakelock_rx_timeout_enable)
16787 			wake_lock_timeout(&dhd->wl_rxwake,
16788 				msecs_to_jiffies(dhd->wakelock_rx_timeout_enable));
16789 		if (dhd->wakelock_ctrl_timeout_enable)
16790 			wake_lock_timeout(&dhd->wl_ctrlwake,
16791 				msecs_to_jiffies(dhd->wakelock_ctrl_timeout_enable));
16792 #endif
16793 		dhd->wakelock_rx_timeout_enable = 0;
16794 		dhd->wakelock_ctrl_timeout_enable = 0;
16795 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
16796 	}
16797 	return ret;
16798 }
16799 
net_os_wake_lock_timeout(struct net_device * dev)16800 int net_os_wake_lock_timeout(struct net_device *dev)
16801 {
16802 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16803 	int ret = 0;
16804 
16805 	if (dhd)
16806 		ret = dhd_os_wake_lock_timeout(&dhd->pub);
16807 	return ret;
16808 }
16809 
dhd_os_wake_lock_rx_timeout_enable(dhd_pub_t * pub,int val)16810 int dhd_os_wake_lock_rx_timeout_enable(dhd_pub_t *pub, int val)
16811 {
16812 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
16813 	unsigned long flags;
16814 
16815 	if (dhd && (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
16816 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
16817 		if (val > dhd->wakelock_rx_timeout_enable)
16818 			dhd->wakelock_rx_timeout_enable = val;
16819 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
16820 	}
16821 	return 0;
16822 }
16823 
dhd_os_wake_lock_ctrl_timeout_enable(dhd_pub_t * pub,int val)16824 int dhd_os_wake_lock_ctrl_timeout_enable(dhd_pub_t *pub, int val)
16825 {
16826 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
16827 	unsigned long flags;
16828 
16829 	if (dhd && (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
16830 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
16831 		if (val > dhd->wakelock_ctrl_timeout_enable)
16832 			dhd->wakelock_ctrl_timeout_enable = val;
16833 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
16834 	}
16835 	return 0;
16836 }
16837 
dhd_os_wake_lock_ctrl_timeout_cancel(dhd_pub_t * pub)16838 int dhd_os_wake_lock_ctrl_timeout_cancel(dhd_pub_t *pub)
16839 {
16840 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
16841 	unsigned long flags;
16842 
16843 	if (dhd && (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
16844 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
16845 		dhd->wakelock_ctrl_timeout_enable = 0;
16846 #ifdef CONFIG_HAS_WAKELOCK
16847 		if (wake_lock_active(&dhd->wl_ctrlwake))
16848 			wake_unlock(&dhd->wl_ctrlwake);
16849 #endif
16850 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
16851 	}
16852 	return 0;
16853 }
16854 
net_os_wake_lock_rx_timeout_enable(struct net_device * dev,int val)16855 int net_os_wake_lock_rx_timeout_enable(struct net_device *dev, int val)
16856 {
16857 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16858 	int ret = 0;
16859 
16860 	if (dhd)
16861 		ret = dhd_os_wake_lock_rx_timeout_enable(&dhd->pub, val);
16862 	return ret;
16863 }
16864 
net_os_wake_lock_ctrl_timeout_enable(struct net_device * dev,int val)16865 int net_os_wake_lock_ctrl_timeout_enable(struct net_device *dev, int val)
16866 {
16867 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
16868 	int ret = 0;
16869 
16870 	if (dhd)
16871 		ret = dhd_os_wake_lock_ctrl_timeout_enable(&dhd->pub, val);
16872 	return ret;
16873 }
16874 
16875 
16876 #if defined(DHD_TRACE_WAKE_LOCK)
16877 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
16878 #include <linux/hashtable.h>
16879 #else
16880 #include <linux/hash.h>
16881 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0) */
16882 
16883 
16884 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
16885 /* Define 2^5 = 32 bucket size hash table */
16886 DEFINE_HASHTABLE(wklock_history, 5);
16887 #else
16888 /* Define 2^5 = 32 bucket size hash table */
16889 struct hlist_head wklock_history[32] = { [0 ... 31] = HLIST_HEAD_INIT };
16890 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0) */
16891 
16892 int trace_wklock_onoff = 1;
16893 typedef enum dhd_wklock_type {
16894 	DHD_WAKE_LOCK,
16895 	DHD_WAKE_UNLOCK,
16896 	DHD_WAIVE_LOCK,
16897 	DHD_RESTORE_LOCK
16898 } dhd_wklock_t;
16899 
16900 struct wk_trace_record {
16901 	unsigned long addr;	            /* Address of the instruction */
16902 	dhd_wklock_t lock_type;         /* lock_type */
16903 	unsigned long long counter;		/* counter information */
16904 	struct hlist_node wklock_node;  /* hash node */
16905 };
16906 
find_wklock_entry(unsigned long addr)16907 static struct wk_trace_record *find_wklock_entry(unsigned long addr)
16908 {
16909 	struct wk_trace_record *wklock_info;
16910 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
16911 	hash_for_each_possible(wklock_history, wklock_info, wklock_node, addr)
16912 #else
16913 	struct hlist_node *entry;
16914 	int index = hash_long(addr, ilog2(ARRAY_SIZE(wklock_history)));
16915 	hlist_for_each_entry(wklock_info, entry, &wklock_history[index], wklock_node)
16916 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0) */
16917 	{
16918 		if (wklock_info->addr == addr) {
16919 			return wklock_info;
16920 		}
16921 	}
16922 	return NULL;
16923 }
16924 
16925 
16926 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
16927 #define HASH_ADD(hashtable, node, key) \
16928 	do { \
16929 		hash_add(hashtable, node, key); \
16930 	} while (0);
16931 #else
16932 #define HASH_ADD(hashtable, node, key) \
16933 	do { \
16934 		int index = hash_long(key, ilog2(ARRAY_SIZE(hashtable))); \
16935 		hlist_add_head(node, &hashtable[index]); \
16936 	} while (0);
16937 #endif /* KERNEL_VER < KERNEL_VERSION(3, 7, 0) */
16938 
16939 #define STORE_WKLOCK_RECORD(wklock_type) \
16940 	do { \
16941 		struct wk_trace_record *wklock_info = NULL; \
16942 		unsigned long func_addr = (unsigned long)__builtin_return_address(0); \
16943 		wklock_info = find_wklock_entry(func_addr); \
16944 		if (wklock_info) { \
16945 			if (wklock_type == DHD_WAIVE_LOCK || wklock_type == DHD_RESTORE_LOCK) { \
16946 				wklock_info->counter = dhd->wakelock_counter; \
16947 			} else { \
16948 				wklock_info->counter++; \
16949 			} \
16950 		} else { \
16951 			wklock_info = kzalloc(sizeof(*wklock_info), GFP_ATOMIC); \
16952 			if (!wklock_info) {\
16953 				printk("Can't allocate wk_trace_record \n"); \
16954 			} else { \
16955 				wklock_info->addr = func_addr; \
16956 				wklock_info->lock_type = wklock_type; \
16957 				if (wklock_type == DHD_WAIVE_LOCK || \
16958 						wklock_type == DHD_RESTORE_LOCK) { \
16959 					wklock_info->counter = dhd->wakelock_counter; \
16960 				} else { \
16961 					wklock_info->counter++; \
16962 				} \
16963 				HASH_ADD(wklock_history, &wklock_info->wklock_node, func_addr); \
16964 			} \
16965 		} \
16966 	} while (0);
16967 
dhd_wk_lock_rec_dump(void)16968 static inline void dhd_wk_lock_rec_dump(void)
16969 {
16970 	int bkt;
16971 	struct wk_trace_record *wklock_info;
16972 
16973 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
16974 	hash_for_each(wklock_history, bkt, wklock_info, wklock_node)
16975 #else
16976 	struct hlist_node *entry = NULL;
16977 	int max_index = ARRAY_SIZE(wklock_history);
16978 	for (bkt = 0; bkt < max_index; bkt++)
16979 		hlist_for_each_entry(wklock_info, entry, &wklock_history[bkt], wklock_node)
16980 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0) */
16981 		{
16982 			switch (wklock_info->lock_type) {
16983 				case DHD_WAKE_LOCK:
16984 					printk("wakelock lock : %pS  lock_counter : %llu \n",
16985 						(void *)wklock_info->addr, wklock_info->counter);
16986 					break;
16987 				case DHD_WAKE_UNLOCK:
16988 					printk("wakelock unlock : %pS, unlock_counter : %llu \n",
16989 						(void *)wklock_info->addr, wklock_info->counter);
16990 					break;
16991 				case DHD_WAIVE_LOCK:
16992 					printk("wakelock waive : %pS  before_waive : %llu \n",
16993 						(void *)wklock_info->addr, wklock_info->counter);
16994 					break;
16995 				case DHD_RESTORE_LOCK:
16996 					printk("wakelock restore : %pS, after_waive : %llu \n",
16997 						(void *)wklock_info->addr, wklock_info->counter);
16998 					break;
16999 			}
17000 		}
17001 }
17002 
dhd_wk_lock_trace_init(struct dhd_info * dhd)17003 static void dhd_wk_lock_trace_init(struct dhd_info *dhd)
17004 {
17005 	unsigned long flags;
17006 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0))
17007 	int i;
17008 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0) */
17009 
17010 	spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17011 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
17012 	hash_init(wklock_history);
17013 #else
17014 	for (i = 0; i < ARRAY_SIZE(wklock_history); i++)
17015 		INIT_HLIST_HEAD(&wklock_history[i]);
17016 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0) */
17017 	spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17018 }
17019 
dhd_wk_lock_trace_deinit(struct dhd_info * dhd)17020 static void dhd_wk_lock_trace_deinit(struct dhd_info *dhd)
17021 {
17022 	int bkt;
17023 	struct wk_trace_record *wklock_info;
17024 	struct hlist_node *tmp;
17025 	unsigned long flags;
17026 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0))
17027 	struct hlist_node *entry = NULL;
17028 	int max_index = ARRAY_SIZE(wklock_history);
17029 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0) */
17030 
17031 	spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17032 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
17033 	hash_for_each_safe(wklock_history, bkt, tmp, wklock_info, wklock_node)
17034 #else
17035 	for (bkt = 0; bkt < max_index; bkt++)
17036 		hlist_for_each_entry_safe(wklock_info, entry, tmp,
17037 			&wklock_history[bkt], wklock_node)
17038 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0)) */
17039 		{
17040 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
17041 			hash_del(&wklock_info->wklock_node);
17042 #else
17043 			hlist_del_init(&wklock_info->wklock_node);
17044 #endif /* KERNEL_VER >= KERNEL_VERSION(3, 7, 0)) */
17045 			kfree(wklock_info);
17046 		}
17047 	spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17048 }
17049 
dhd_wk_lock_stats_dump(dhd_pub_t * dhdp)17050 void dhd_wk_lock_stats_dump(dhd_pub_t *dhdp)
17051 {
17052 	dhd_info_t *dhd = (dhd_info_t *)(dhdp->info);
17053 	unsigned long flags;
17054 
17055 	printk(KERN_ERR"DHD Printing wl_wake Lock/Unlock Record \r\n");
17056 	spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17057 	dhd_wk_lock_rec_dump();
17058 	spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17059 
17060 }
17061 #else
17062 #define STORE_WKLOCK_RECORD(wklock_type)
17063 #endif /* ! DHD_TRACE_WAKE_LOCK */
17064 
dhd_os_wake_lock(dhd_pub_t * pub)17065 int dhd_os_wake_lock(dhd_pub_t *pub)
17066 {
17067 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17068 	unsigned long flags;
17069 	int ret = 0;
17070 
17071 	if (dhd && (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
17072 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17073 		if (dhd->wakelock_counter == 0 && !dhd->waive_wakelock) {
17074 #ifdef CONFIG_HAS_WAKELOCK
17075 			wake_lock(&dhd->wl_wifi);
17076 #elif defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
17077 			dhd_bus_dev_pm_stay_awake(pub);
17078 #endif
17079 		}
17080 #ifdef DHD_TRACE_WAKE_LOCK
17081 		if (trace_wklock_onoff) {
17082 			STORE_WKLOCK_RECORD(DHD_WAKE_LOCK);
17083 		}
17084 #endif /* DHD_TRACE_WAKE_LOCK */
17085 		dhd->wakelock_counter++;
17086 		ret = dhd->wakelock_counter;
17087 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17088 	}
17089 
17090 	return ret;
17091 }
17092 
dhd_event_wake_lock(dhd_pub_t * pub)17093 void dhd_event_wake_lock(dhd_pub_t *pub)
17094 {
17095 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17096 
17097 	if (dhd) {
17098 #ifdef CONFIG_HAS_WAKELOCK
17099 		wake_lock(&dhd->wl_evtwake);
17100 #elif defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
17101 		dhd_bus_dev_pm_stay_awake(pub);
17102 #endif
17103 	}
17104 }
17105 
17106 void
dhd_pm_wake_lock_timeout(dhd_pub_t * pub,int val)17107 dhd_pm_wake_lock_timeout(dhd_pub_t *pub, int val)
17108 {
17109 #ifdef CONFIG_HAS_WAKELOCK
17110 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17111 
17112 	if (dhd) {
17113 		wake_lock_timeout(&dhd->wl_pmwake, msecs_to_jiffies(val));
17114 	}
17115 #endif /* CONFIG_HAS_WAKE_LOCK */
17116 }
17117 
17118 void
dhd_txfl_wake_lock_timeout(dhd_pub_t * pub,int val)17119 dhd_txfl_wake_lock_timeout(dhd_pub_t *pub, int val)
17120 {
17121 #ifdef CONFIG_HAS_WAKELOCK
17122 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17123 
17124 	if (dhd) {
17125 		wake_lock_timeout(&dhd->wl_txflwake, msecs_to_jiffies(val));
17126 	}
17127 #endif /* CONFIG_HAS_WAKE_LOCK */
17128 }
17129 
net_os_wake_lock(struct net_device * dev)17130 int net_os_wake_lock(struct net_device *dev)
17131 {
17132 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
17133 	int ret = 0;
17134 
17135 	if (dhd)
17136 		ret = dhd_os_wake_lock(&dhd->pub);
17137 	return ret;
17138 }
17139 
dhd_os_wake_unlock(dhd_pub_t * pub)17140 int dhd_os_wake_unlock(dhd_pub_t *pub)
17141 {
17142 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17143 	unsigned long flags;
17144 	int ret = 0;
17145 
17146 	dhd_os_wake_lock_timeout(pub);
17147 	if (dhd && (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
17148 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17149 
17150 		if (dhd->wakelock_counter > 0) {
17151 			dhd->wakelock_counter--;
17152 #ifdef DHD_TRACE_WAKE_LOCK
17153 			if (trace_wklock_onoff) {
17154 				STORE_WKLOCK_RECORD(DHD_WAKE_UNLOCK);
17155 			}
17156 #endif /* DHD_TRACE_WAKE_LOCK */
17157 			if (dhd->wakelock_counter == 0 && !dhd->waive_wakelock) {
17158 #ifdef CONFIG_HAS_WAKELOCK
17159 				wake_unlock(&dhd->wl_wifi);
17160 #elif defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
17161 				dhd_bus_dev_pm_relax(pub);
17162 #endif
17163 			}
17164 			ret = dhd->wakelock_counter;
17165 		}
17166 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17167 	}
17168 	return ret;
17169 }
17170 
dhd_event_wake_unlock(dhd_pub_t * pub)17171 void dhd_event_wake_unlock(dhd_pub_t *pub)
17172 {
17173 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17174 
17175 	if (dhd) {
17176 #ifdef CONFIG_HAS_WAKELOCK
17177 		wake_unlock(&dhd->wl_evtwake);
17178 #elif defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
17179 		dhd_bus_dev_pm_relax(pub);
17180 #endif
17181 	}
17182 }
17183 
dhd_pm_wake_unlock(dhd_pub_t * pub)17184 void dhd_pm_wake_unlock(dhd_pub_t *pub)
17185 {
17186 #ifdef CONFIG_HAS_WAKELOCK
17187 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17188 
17189 	if (dhd) {
17190 		/* if wl_pmwake is active, unlock it */
17191 		if (wake_lock_active(&dhd->wl_pmwake)) {
17192 			wake_unlock(&dhd->wl_pmwake);
17193 		}
17194 	}
17195 #endif /* CONFIG_HAS_WAKELOCK */
17196 }
17197 
dhd_txfl_wake_unlock(dhd_pub_t * pub)17198 void dhd_txfl_wake_unlock(dhd_pub_t *pub)
17199 {
17200 #ifdef CONFIG_HAS_WAKELOCK
17201 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17202 
17203 	if (dhd) {
17204 		/* if wl_txflwake is active, unlock it */
17205 		if (wake_lock_active(&dhd->wl_txflwake)) {
17206 			wake_unlock(&dhd->wl_txflwake);
17207 		}
17208 	}
17209 #endif /* CONFIG_HAS_WAKELOCK */
17210 }
17211 
dhd_os_check_wakelock(dhd_pub_t * pub)17212 int dhd_os_check_wakelock(dhd_pub_t *pub)
17213 {
17214 #if defined(CONFIG_HAS_WAKELOCK) || (defined(BCMSDIO) && (LINUX_VERSION_CODE > \
17215 	KERNEL_VERSION(2, 6, 36)))
17216 	dhd_info_t *dhd;
17217 
17218 	if (!pub)
17219 		return 0;
17220 	dhd = (dhd_info_t *)(pub->info);
17221 #endif /* CONFIG_HAS_WAKELOCK || BCMSDIO */
17222 
17223 #ifdef CONFIG_HAS_WAKELOCK
17224 	/* Indicate to the SD Host to avoid going to suspend if internal locks are up */
17225 	if (dhd && (wake_lock_active(&dhd->wl_wifi) ||
17226 		(wake_lock_active(&dhd->wl_wdwake))))
17227 		return 1;
17228 #elif defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
17229 	if (dhd && (dhd->wakelock_counter > 0) && dhd_bus_dev_pm_enabled(pub))
17230 		return 1;
17231 #endif
17232 	return 0;
17233 }
17234 
17235 int
dhd_os_check_wakelock_all(dhd_pub_t * pub)17236 dhd_os_check_wakelock_all(dhd_pub_t *pub)
17237 {
17238 #if defined(CONFIG_HAS_WAKELOCK) || (defined(BCMSDIO) && (LINUX_VERSION_CODE > \
17239 	KERNEL_VERSION(2, 6, 36)))
17240 #if defined(CONFIG_HAS_WAKELOCK)
17241 	int l1, l2, l3, l4, l7, l8, l9;
17242 	int l5 = 0, l6 = 0;
17243 	int c, lock_active;
17244 #endif /* CONFIG_HAS_WAKELOCK */
17245 	dhd_info_t *dhd;
17246 
17247 	if (!pub) {
17248 		return 0;
17249 	}
17250 	dhd = (dhd_info_t *)(pub->info);
17251 	if (!dhd) {
17252 		return 0;
17253 	}
17254 #endif /* CONFIG_HAS_WAKELOCK || BCMSDIO */
17255 
17256 #ifdef CONFIG_HAS_WAKELOCK
17257 	c = dhd->wakelock_counter;
17258 	l1 = wake_lock_active(&dhd->wl_wifi);
17259 	l2 = wake_lock_active(&dhd->wl_wdwake);
17260 	l3 = wake_lock_active(&dhd->wl_rxwake);
17261 	l4 = wake_lock_active(&dhd->wl_ctrlwake);
17262 	l7 = wake_lock_active(&dhd->wl_evtwake);
17263 #ifdef BCMPCIE_OOB_HOST_WAKE
17264 	l5 = wake_lock_active(&dhd->wl_intrwake);
17265 #endif /* BCMPCIE_OOB_HOST_WAKE */
17266 #ifdef DHD_USE_SCAN_WAKELOCK
17267 	l6 = wake_lock_active(&dhd->wl_scanwake);
17268 #endif /* DHD_USE_SCAN_WAKELOCK */
17269 	l8 = wake_lock_active(&dhd->wl_pmwake);
17270 	l9 = wake_lock_active(&dhd->wl_txflwake);
17271 	lock_active = (l1 || l2 || l3 || l4 || l5 || l6 || l7 || l8 || l9);
17272 
17273 	/* Indicate to the Host to avoid going to suspend if internal locks are up */
17274 	if (lock_active) {
17275 		DHD_ERROR(("%s wakelock c-%d wl-%d wd-%d rx-%d "
17276 			"ctl-%d intr-%d scan-%d evt-%d, pm-%d, txfl-%d\n",
17277 			__FUNCTION__, c, l1, l2, l3, l4, l5, l6, l7, l8, l9));
17278 		return 1;
17279 	}
17280 #elif defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
17281 	if (dhd && (dhd->wakelock_counter > 0) && dhd_bus_dev_pm_enabled(pub)) {
17282 		return 1;
17283 	}
17284 #endif /* defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36)) */
17285 	return 0;
17286 }
17287 
net_os_wake_unlock(struct net_device * dev)17288 int net_os_wake_unlock(struct net_device *dev)
17289 {
17290 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
17291 	int ret = 0;
17292 
17293 	if (dhd)
17294 		ret = dhd_os_wake_unlock(&dhd->pub);
17295 	return ret;
17296 }
17297 
dhd_os_wd_wake_lock(dhd_pub_t * pub)17298 int dhd_os_wd_wake_lock(dhd_pub_t *pub)
17299 {
17300 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17301 	unsigned long flags;
17302 	int ret = 0;
17303 
17304 	if (dhd) {
17305 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17306 #ifdef CONFIG_HAS_WAKELOCK
17307 		/* if wakelock_wd_counter was never used : lock it at once */
17308 		if (!dhd->wakelock_wd_counter)
17309 			wake_lock(&dhd->wl_wdwake);
17310 #endif
17311 		dhd->wakelock_wd_counter++;
17312 		ret = dhd->wakelock_wd_counter;
17313 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17314 	}
17315 	return ret;
17316 }
17317 
dhd_os_wd_wake_unlock(dhd_pub_t * pub)17318 int dhd_os_wd_wake_unlock(dhd_pub_t *pub)
17319 {
17320 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17321 	unsigned long flags;
17322 	int ret = 0;
17323 
17324 	if (dhd) {
17325 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17326 		if (dhd->wakelock_wd_counter) {
17327 			dhd->wakelock_wd_counter = 0;
17328 #ifdef CONFIG_HAS_WAKELOCK
17329 			wake_unlock(&dhd->wl_wdwake);
17330 #endif
17331 		}
17332 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17333 	}
17334 	return ret;
17335 }
17336 
17337 #ifdef BCMPCIE_OOB_HOST_WAKE
17338 void
dhd_os_oob_irq_wake_lock_timeout(dhd_pub_t * pub,int val)17339 dhd_os_oob_irq_wake_lock_timeout(dhd_pub_t *pub, int val)
17340 {
17341 #ifdef CONFIG_HAS_WAKELOCK
17342 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17343 
17344 	if (dhd) {
17345 		wake_lock_timeout(&dhd->wl_intrwake, msecs_to_jiffies(val));
17346 	}
17347 #endif /* CONFIG_HAS_WAKELOCK */
17348 }
17349 
17350 void
dhd_os_oob_irq_wake_unlock(dhd_pub_t * pub)17351 dhd_os_oob_irq_wake_unlock(dhd_pub_t *pub)
17352 {
17353 #ifdef CONFIG_HAS_WAKELOCK
17354 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17355 
17356 	if (dhd) {
17357 		/* if wl_intrwake is active, unlock it */
17358 		if (wake_lock_active(&dhd->wl_intrwake)) {
17359 			wake_unlock(&dhd->wl_intrwake);
17360 		}
17361 	}
17362 #endif /* CONFIG_HAS_WAKELOCK */
17363 }
17364 #endif /* BCMPCIE_OOB_HOST_WAKE */
17365 
17366 #ifdef DHD_USE_SCAN_WAKELOCK
17367 void
dhd_os_scan_wake_lock_timeout(dhd_pub_t * pub,int val)17368 dhd_os_scan_wake_lock_timeout(dhd_pub_t *pub, int val)
17369 {
17370 #ifdef CONFIG_HAS_WAKELOCK
17371 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17372 
17373 	if (dhd) {
17374 		wake_lock_timeout(&dhd->wl_scanwake, msecs_to_jiffies(val));
17375 	}
17376 #endif /* CONFIG_HAS_WAKELOCK */
17377 }
17378 
17379 void
dhd_os_scan_wake_unlock(dhd_pub_t * pub)17380 dhd_os_scan_wake_unlock(dhd_pub_t *pub)
17381 {
17382 #ifdef CONFIG_HAS_WAKELOCK
17383 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17384 
17385 	if (dhd) {
17386 		/* if wl_scanwake is active, unlock it */
17387 		if (wake_lock_active(&dhd->wl_scanwake)) {
17388 			wake_unlock(&dhd->wl_scanwake);
17389 		}
17390 	}
17391 #endif /* CONFIG_HAS_WAKELOCK */
17392 }
17393 #endif /* DHD_USE_SCAN_WAKELOCK */
17394 
17395 /* waive wakelocks for operations such as IOVARs in suspend function, must be closed
17396  * by a paired function call to dhd_wakelock_restore. returns current wakelock counter
17397  */
dhd_os_wake_lock_waive(dhd_pub_t * pub)17398 int dhd_os_wake_lock_waive(dhd_pub_t *pub)
17399 {
17400 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17401 	unsigned long flags;
17402 	int ret = 0;
17403 
17404 	if (dhd && (dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT)) {
17405 		spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17406 
17407 		/* dhd_wakelock_waive/dhd_wakelock_restore must be paired */
17408 		if (dhd->waive_wakelock == FALSE) {
17409 #ifdef DHD_TRACE_WAKE_LOCK
17410 			if (trace_wklock_onoff) {
17411 				STORE_WKLOCK_RECORD(DHD_WAIVE_LOCK);
17412 			}
17413 #endif /* DHD_TRACE_WAKE_LOCK */
17414 			/* record current lock status */
17415 			dhd->wakelock_before_waive = dhd->wakelock_counter;
17416 			dhd->waive_wakelock = TRUE;
17417 		}
17418 		ret = dhd->wakelock_wd_counter;
17419 		spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17420 	}
17421 	return ret;
17422 }
17423 
dhd_os_wake_lock_restore(dhd_pub_t * pub)17424 int dhd_os_wake_lock_restore(dhd_pub_t *pub)
17425 {
17426 	dhd_info_t *dhd = (dhd_info_t *)(pub->info);
17427 	unsigned long flags;
17428 	int ret = 0;
17429 
17430 	if (!dhd)
17431 		return 0;
17432 	if ((dhd->dhd_state & DHD_ATTACH_STATE_WAKELOCKS_INIT) == 0)
17433 		return 0;
17434 
17435 	spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
17436 
17437 	/* dhd_wakelock_waive/dhd_wakelock_restore must be paired */
17438 	if (!dhd->waive_wakelock)
17439 		goto exit;
17440 
17441 	dhd->waive_wakelock = FALSE;
17442 	/* if somebody else acquires wakelock between dhd_wakelock_waive/dhd_wakelock_restore,
17443 	 * we need to make it up by calling wake_lock or pm_stay_awake. or if somebody releases
17444 	 * the lock in between, do the same by calling wake_unlock or pm_relax
17445 	 */
17446 #ifdef DHD_TRACE_WAKE_LOCK
17447 	if (trace_wklock_onoff) {
17448 		STORE_WKLOCK_RECORD(DHD_RESTORE_LOCK);
17449 	}
17450 #endif /* DHD_TRACE_WAKE_LOCK */
17451 
17452 	if (dhd->wakelock_before_waive == 0 && dhd->wakelock_counter > 0) {
17453 #ifdef CONFIG_HAS_WAKELOCK
17454 		wake_lock(&dhd->wl_wifi);
17455 #elif defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
17456 		dhd_bus_dev_pm_stay_awake(&dhd->pub);
17457 #endif
17458 	} else if (dhd->wakelock_before_waive > 0 && dhd->wakelock_counter == 0) {
17459 #ifdef CONFIG_HAS_WAKELOCK
17460 		wake_unlock(&dhd->wl_wifi);
17461 #elif defined(BCMSDIO) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
17462 		dhd_bus_dev_pm_relax(&dhd->pub);
17463 #endif
17464 	}
17465 	dhd->wakelock_before_waive = 0;
17466 exit:
17467 	ret = dhd->wakelock_wd_counter;
17468 	spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
17469 	return ret;
17470 }
17471 
dhd_os_wake_lock_init(struct dhd_info * dhd)17472 void dhd_os_wake_lock_init(struct dhd_info *dhd)
17473 {
17474 	DHD_TRACE(("%s: initialize wake_lock_counters\n", __FUNCTION__));
17475 	dhd->wakelock_counter = 0;
17476 	dhd->wakelock_rx_timeout_enable = 0;
17477 	dhd->wakelock_ctrl_timeout_enable = 0;
17478 #ifdef CONFIG_HAS_WAKELOCK
17479 	// terence 20161023: can not destroy wl_wifi when wlan down, it will happen null pointer in dhd_ioctl_entry
17480 	wake_lock_init(&dhd->wl_rxwake, WAKE_LOCK_SUSPEND, "wlan_rx_wake");
17481 	wake_lock_init(&dhd->wl_ctrlwake, WAKE_LOCK_SUSPEND, "wlan_ctrl_wake");
17482 	wake_lock_init(&dhd->wl_evtwake, WAKE_LOCK_SUSPEND, "wlan_evt_wake");
17483 	wake_lock_init(&dhd->wl_pmwake, WAKE_LOCK_SUSPEND, "wlan_pm_wake");
17484 	wake_lock_init(&dhd->wl_txflwake, WAKE_LOCK_SUSPEND, "wlan_txfl_wake");
17485 #ifdef BCMPCIE_OOB_HOST_WAKE
17486 	wake_lock_init(&dhd->wl_intrwake, WAKE_LOCK_SUSPEND, "wlan_oob_irq_wake");
17487 #endif /* BCMPCIE_OOB_HOST_WAKE */
17488 #ifdef DHD_USE_SCAN_WAKELOCK
17489 	wake_lock_init(&dhd->wl_scanwake, WAKE_LOCK_SUSPEND, "wlan_scan_wake");
17490 #endif /* DHD_USE_SCAN_WAKELOCK */
17491 #endif /* CONFIG_HAS_WAKELOCK */
17492 #ifdef DHD_TRACE_WAKE_LOCK
17493 	dhd_wk_lock_trace_init(dhd);
17494 #endif /* DHD_TRACE_WAKE_LOCK */
17495 }
17496 
dhd_os_wake_lock_destroy(struct dhd_info * dhd)17497 void dhd_os_wake_lock_destroy(struct dhd_info *dhd)
17498 {
17499 	DHD_TRACE(("%s: deinit wake_lock_counters\n", __FUNCTION__));
17500 #ifdef CONFIG_HAS_WAKELOCK
17501 	dhd->wakelock_counter = 0;
17502 	dhd->wakelock_rx_timeout_enable = 0;
17503 	dhd->wakelock_ctrl_timeout_enable = 0;
17504 	// terence 20161023: can not destroy wl_wifi when wlan down, it will happen null pointer in dhd_ioctl_entry
17505 	wake_lock_destroy(&dhd->wl_rxwake);
17506 	wake_lock_destroy(&dhd->wl_ctrlwake);
17507 	wake_lock_destroy(&dhd->wl_evtwake);
17508 	wake_lock_destroy(&dhd->wl_pmwake);
17509 	wake_lock_destroy(&dhd->wl_txflwake);
17510 #ifdef BCMPCIE_OOB_HOST_WAKE
17511 	wake_lock_destroy(&dhd->wl_intrwake);
17512 #endif /* BCMPCIE_OOB_HOST_WAKE */
17513 #ifdef DHD_USE_SCAN_WAKELOCK
17514 	wake_lock_destroy(&dhd->wl_scanwake);
17515 #endif /* DHD_USE_SCAN_WAKELOCK */
17516 #ifdef DHD_TRACE_WAKE_LOCK
17517 	dhd_wk_lock_trace_deinit(dhd);
17518 #endif /* DHD_TRACE_WAKE_LOCK */
17519 #endif /* CONFIG_HAS_WAKELOCK */
17520 }
17521 
dhd_os_check_if_up(dhd_pub_t * pub)17522 bool dhd_os_check_if_up(dhd_pub_t *pub)
17523 {
17524 	if (!pub)
17525 		return FALSE;
17526 	return pub->up;
17527 }
17528 
17529 /* function to collect firmware, chip id and chip version info */
dhd_set_version_info(dhd_pub_t * dhdp,char * fw)17530 void dhd_set_version_info(dhd_pub_t *dhdp, char *fw)
17531 {
17532 	int i;
17533 
17534 	i = snprintf(info_string, sizeof(info_string),
17535 		"  Driver: %s\n  Firmware: %s\n  CLM: %s ", EPI_VERSION_STR, fw, clm_version);
17536 	DHD_PRINT("%s\n", info_string);
17537 
17538 	if (!dhdp)
17539 		return;
17540 
17541 	i = snprintf(&info_string[i], sizeof(info_string) - i,
17542 		"\n  Chip: %x Rev %x", dhd_conf_get_chip(dhdp),
17543 		dhd_conf_get_chiprev(dhdp));
17544 }
17545 
dhd_ioctl_entry_local(struct net_device * net,wl_ioctl_t * ioc,int cmd)17546 int dhd_ioctl_entry_local(struct net_device *net, wl_ioctl_t *ioc, int cmd)
17547 {
17548 	int ifidx;
17549 	int ret = 0;
17550 	dhd_info_t *dhd = NULL;
17551 
17552 	if (!net || !DEV_PRIV(net)) {
17553 		DHD_ERROR(("%s invalid parameter\n", __FUNCTION__));
17554 		return -EINVAL;
17555 	}
17556 
17557 	dhd = DHD_DEV_INFO(net);
17558 	if (!dhd)
17559 		return -EINVAL;
17560 
17561 	ifidx = dhd_net2idx(dhd, net);
17562 	if (ifidx == DHD_BAD_IF) {
17563 		DHD_ERROR(("%s bad ifidx\n", __FUNCTION__));
17564 		return -ENODEV;
17565 	}
17566 
17567 	DHD_OS_WAKE_LOCK(&dhd->pub);
17568 	DHD_PERIM_LOCK(&dhd->pub);
17569 
17570 	ret = dhd_wl_ioctl(&dhd->pub, ifidx, ioc, ioc->buf, ioc->len);
17571 	dhd_check_hang(net, &dhd->pub, ret);
17572 
17573 	DHD_PERIM_UNLOCK(&dhd->pub);
17574 	DHD_OS_WAKE_UNLOCK(&dhd->pub);
17575 
17576 	return ret;
17577 }
17578 
dhd_os_check_hang(dhd_pub_t * dhdp,int ifidx,int ret)17579 bool dhd_os_check_hang(dhd_pub_t *dhdp, int ifidx, int ret)
17580 {
17581 	struct net_device *net;
17582 
17583 	net = dhd_idx2net(dhdp, ifidx);
17584 	if (!net) {
17585 		DHD_ERROR(("%s : Invalid index : %d\n", __FUNCTION__, ifidx));
17586 		return -EINVAL;
17587 	}
17588 
17589 	return dhd_check_hang(net, dhdp, ret);
17590 }
17591 
17592 /* Return instance */
dhd_get_instance(dhd_pub_t * dhdp)17593 int dhd_get_instance(dhd_pub_t *dhdp)
17594 {
17595 	return dhdp->info->unit;
17596 }
17597 
17598 
17599 #ifdef PROP_TXSTATUS
17600 
dhd_wlfc_plat_init(void * dhd)17601 void dhd_wlfc_plat_init(void *dhd)
17602 {
17603 #ifdef USE_DYNAMIC_F2_BLKSIZE
17604 	dhdsdio_func_blocksize((dhd_pub_t *)dhd, 2, DYNAMIC_F2_BLKSIZE_FOR_NONLEGACY);
17605 #endif /* USE_DYNAMIC_F2_BLKSIZE */
17606 	return;
17607 }
17608 
dhd_wlfc_plat_deinit(void * dhd)17609 void dhd_wlfc_plat_deinit(void *dhd)
17610 {
17611 #ifdef USE_DYNAMIC_F2_BLKSIZE
17612 	dhdsdio_func_blocksize((dhd_pub_t *)dhd, 2, sd_f2_blocksize);
17613 #endif /* USE_DYNAMIC_F2_BLKSIZE */
17614 	return;
17615 }
17616 
dhd_wlfc_skip_fc(void * dhdp,uint8 idx)17617 bool dhd_wlfc_skip_fc(void * dhdp, uint8 idx)
17618 {
17619 #ifdef SKIP_WLFC_ON_CONCURRENT
17620 
17621 #ifdef WL_CFG80211
17622 	struct net_device * net =  dhd_idx2net((dhd_pub_t *)dhdp, idx);
17623 	if (net)
17624 	/* enable flow control in vsdb mode */
17625 	return !(wl_cfg80211_is_concurrent_mode(net));
17626 #else
17627 	return TRUE; /* skip flow control */
17628 #endif /* WL_CFG80211 */
17629 
17630 #else
17631 	return FALSE;
17632 #endif /* SKIP_WLFC_ON_CONCURRENT */
17633 	return FALSE;
17634 }
17635 #endif /* PROP_TXSTATUS */
17636 
17637 #ifdef BCMDBGFS
17638 #include <linux/debugfs.h>
17639 
17640 typedef struct dhd_dbgfs {
17641 	struct dentry	*debugfs_dir;
17642 	struct dentry	*debugfs_mem;
17643 	dhd_pub_t	*dhdp;
17644 	uint32		size;
17645 } dhd_dbgfs_t;
17646 
17647 dhd_dbgfs_t g_dbgfs;
17648 
17649 extern uint32 dhd_readregl(void *bp, uint32 addr);
17650 extern uint32 dhd_writeregl(void *bp, uint32 addr, uint32 data);
17651 
17652 static int
dhd_dbg_state_open(struct inode * inode,struct file * file)17653 dhd_dbg_state_open(struct inode *inode, struct file *file)
17654 {
17655 	file->private_data = inode->i_private;
17656 	return 0;
17657 }
17658 
17659 static ssize_t
dhd_dbg_state_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)17660 dhd_dbg_state_read(struct file *file, char __user *ubuf,
17661                        size_t count, loff_t *ppos)
17662 {
17663 	ssize_t rval;
17664 	uint32 tmp;
17665 	loff_t pos = *ppos;
17666 	size_t ret;
17667 
17668 	if (pos < 0)
17669 		return -EINVAL;
17670 	if (pos >= g_dbgfs.size || !count)
17671 		return 0;
17672 	if (count > g_dbgfs.size - pos)
17673 		count = g_dbgfs.size - pos;
17674 
17675 	/* Basically enforce aligned 4 byte reads. It's up to the user to work out the details */
17676 	tmp = dhd_readregl(g_dbgfs.dhdp->bus, file->f_pos & (~3));
17677 
17678 	ret = copy_to_user(ubuf, &tmp, 4);
17679 	if (ret == count)
17680 		return -EFAULT;
17681 
17682 	count -= ret;
17683 	*ppos = pos + count;
17684 	rval = count;
17685 
17686 	return rval;
17687 }
17688 
17689 
17690 static ssize_t
dhd_debugfs_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)17691 dhd_debugfs_write(struct file *file, const char __user *ubuf, size_t count, loff_t *ppos)
17692 {
17693 	loff_t pos = *ppos;
17694 	size_t ret;
17695 	uint32 buf;
17696 
17697 	if (pos < 0)
17698 		return -EINVAL;
17699 	if (pos >= g_dbgfs.size || !count)
17700 		return 0;
17701 	if (count > g_dbgfs.size - pos)
17702 		count = g_dbgfs.size - pos;
17703 
17704 	ret = copy_from_user(&buf, ubuf, sizeof(uint32));
17705 	if (ret == count)
17706 		return -EFAULT;
17707 
17708 	/* Basically enforce aligned 4 byte writes. It's up to the user to work out the details */
17709 	dhd_writeregl(g_dbgfs.dhdp->bus, file->f_pos & (~3), buf);
17710 
17711 	return count;
17712 }
17713 
17714 
17715 loff_t
dhd_debugfs_lseek(struct file * file,loff_t off,int whence)17716 dhd_debugfs_lseek(struct file *file, loff_t off, int whence)
17717 {
17718 	loff_t pos = -1;
17719 
17720 	switch (whence) {
17721 		case 0:
17722 			pos = off;
17723 			break;
17724 		case 1:
17725 			pos = file->f_pos + off;
17726 			break;
17727 		case 2:
17728 			pos = g_dbgfs.size - off;
17729 	}
17730 	return (pos < 0 || pos > g_dbgfs.size) ? -EINVAL : (file->f_pos = pos);
17731 }
17732 
17733 static const struct file_operations dhd_dbg_state_ops = {
17734 	.read   = dhd_dbg_state_read,
17735 	.write	= dhd_debugfs_write,
17736 	.open   = dhd_dbg_state_open,
17737 	.llseek	= dhd_debugfs_lseek
17738 };
17739 
dhd_dbgfs_create(void)17740 static void dhd_dbgfs_create(void)
17741 {
17742 	if (g_dbgfs.debugfs_dir) {
17743 		g_dbgfs.debugfs_mem = debugfs_create_file("mem", 0644, g_dbgfs.debugfs_dir,
17744 			NULL, &dhd_dbg_state_ops);
17745 	}
17746 }
17747 
dhd_dbgfs_init(dhd_pub_t * dhdp)17748 void dhd_dbgfs_init(dhd_pub_t *dhdp)
17749 {
17750 	g_dbgfs.dhdp = dhdp;
17751 	g_dbgfs.size = 0x20000000; /* Allow access to various cores regs */
17752 
17753 	g_dbgfs.debugfs_dir = debugfs_create_dir("dhd", 0);
17754 	if (IS_ERR(g_dbgfs.debugfs_dir)) {
17755 		g_dbgfs.debugfs_dir = NULL;
17756 		return;
17757 	}
17758 
17759 	dhd_dbgfs_create();
17760 
17761 	return;
17762 }
17763 
dhd_dbgfs_remove(void)17764 void dhd_dbgfs_remove(void)
17765 {
17766 	debugfs_remove(g_dbgfs.debugfs_mem);
17767 	debugfs_remove(g_dbgfs.debugfs_dir);
17768 
17769 	bzero((unsigned char *) &g_dbgfs, sizeof(g_dbgfs));
17770 }
17771 #endif /* BCMDBGFS */
17772 
17773 #ifdef WLMEDIA_HTSF
17774 
17775 static
dhd_htsf_addtxts(dhd_pub_t * dhdp,void * pktbuf)17776 void dhd_htsf_addtxts(dhd_pub_t *dhdp, void *pktbuf)
17777 {
17778 	dhd_info_t *dhd = (dhd_info_t *)(dhdp->info);
17779 	struct sk_buff *skb;
17780 	uint32 htsf = 0;
17781 	uint16 dport = 0, oldmagic = 0xACAC;
17782 	char *p1;
17783 	htsfts_t ts;
17784 
17785 	/*  timestamp packet  */
17786 
17787 	p1 = (char*) PKTDATA(dhdp->osh, pktbuf);
17788 
17789 	if (PKTLEN(dhdp->osh, pktbuf) > HTSF_MINLEN) {
17790 /*		memcpy(&proto, p1+26, 4);  	*/
17791 		memcpy(&dport, p1+40, 2);
17792 /* 	proto = ((ntoh32(proto))>> 16) & 0xFF;  */
17793 		dport = ntoh16(dport);
17794 	}
17795 
17796 	/* timestamp only if  icmp or udb iperf with port 5555 */
17797 /*	if (proto == 17 && dport == tsport) { */
17798 	if (dport >= tsport && dport <= tsport + 20) {
17799 
17800 		skb = (struct sk_buff *) pktbuf;
17801 
17802 		htsf = dhd_get_htsf(dhd, 0);
17803 		memset(skb->data + 44, 0, 2); /* clear checksum */
17804 		memcpy(skb->data+82, &oldmagic, 2);
17805 		memcpy(skb->data+84, &htsf, 4);
17806 
17807 		memset(&ts, 0, sizeof(htsfts_t));
17808 		ts.magic  = HTSFMAGIC;
17809 		ts.prio   = PKTPRIO(pktbuf);
17810 		ts.seqnum = htsf_seqnum++;
17811 		ts.c10    = get_cycles();
17812 		ts.t10    = htsf;
17813 		ts.endmagic = HTSFENDMAGIC;
17814 
17815 		memcpy(skb->data + HTSF_HOSTOFFSET, &ts, sizeof(ts));
17816 	}
17817 }
17818 
dhd_dump_htsfhisto(histo_t * his,char * s)17819 static void dhd_dump_htsfhisto(histo_t *his, char *s)
17820 {
17821 	int pktcnt = 0, curval = 0, i;
17822 	for (i = 0; i < (NUMBIN-2); i++) {
17823 		curval += 500;
17824 		printf("%d ",  his->bin[i]);
17825 		pktcnt += his->bin[i];
17826 	}
17827 	printf(" max: %d TotPkt: %d neg: %d [%s]\n", his->bin[NUMBIN-2], pktcnt,
17828 		his->bin[NUMBIN-1], s);
17829 }
17830 
17831 static
sorttobin(int value,histo_t * histo)17832 void sorttobin(int value, histo_t *histo)
17833 {
17834 	int i, binval = 0;
17835 
17836 	if (value < 0) {
17837 		histo->bin[NUMBIN-1]++;
17838 		return;
17839 	}
17840 	if (value > histo->bin[NUMBIN-2])  /* store the max value  */
17841 		histo->bin[NUMBIN-2] = value;
17842 
17843 	for (i = 0; i < (NUMBIN-2); i++) {
17844 		binval += 500; /* 500m s bins */
17845 		if (value <= binval) {
17846 			histo->bin[i]++;
17847 			return;
17848 		}
17849 	}
17850 	histo->bin[NUMBIN-3]++;
17851 }
17852 
17853 static
dhd_htsf_addrxts(dhd_pub_t * dhdp,void * pktbuf)17854 void dhd_htsf_addrxts(dhd_pub_t *dhdp, void *pktbuf)
17855 {
17856 	dhd_info_t *dhd = (dhd_info_t *)dhdp->info;
17857 	struct sk_buff *skb;
17858 	char *p1;
17859 	uint16 old_magic;
17860 	int d1, d2, d3, end2end;
17861 	htsfts_t *htsf_ts;
17862 	uint32 htsf;
17863 
17864 	skb = PKTTONATIVE(dhdp->osh, pktbuf);
17865 	p1 = (char*)PKTDATA(dhdp->osh, pktbuf);
17866 
17867 	if (PKTLEN(osh, pktbuf) > HTSF_MINLEN) {
17868 		memcpy(&old_magic, p1+78, 2);
17869 		htsf_ts = (htsfts_t*) (p1 + HTSF_HOSTOFFSET - 4);
17870 	} else {
17871 		return;
17872 	}
17873 
17874 	if (htsf_ts->magic == HTSFMAGIC) {
17875 		htsf_ts->tE0 = dhd_get_htsf(dhd, 0);
17876 		htsf_ts->cE0 = get_cycles();
17877 	}
17878 
17879 	if (old_magic == 0xACAC) {
17880 
17881 		tspktcnt++;
17882 		htsf = dhd_get_htsf(dhd, 0);
17883 		memcpy(skb->data+92, &htsf, sizeof(uint32));
17884 
17885 		memcpy(&ts[tsidx].t1, skb->data+80, 16);
17886 
17887 		d1 = ts[tsidx].t2 - ts[tsidx].t1;
17888 		d2 = ts[tsidx].t3 - ts[tsidx].t2;
17889 		d3 = ts[tsidx].t4 - ts[tsidx].t3;
17890 		end2end = ts[tsidx].t4 - ts[tsidx].t1;
17891 
17892 		sorttobin(d1, &vi_d1);
17893 		sorttobin(d2, &vi_d2);
17894 		sorttobin(d3, &vi_d3);
17895 		sorttobin(end2end, &vi_d4);
17896 
17897 		if (end2end > 0 && end2end >  maxdelay) {
17898 			maxdelay = end2end;
17899 			maxdelaypktno = tspktcnt;
17900 			memcpy(&maxdelayts, &ts[tsidx], 16);
17901 		}
17902 		if (++tsidx >= TSMAX)
17903 			tsidx = 0;
17904 	}
17905 }
17906 
dhd_get_htsf(dhd_info_t * dhd,int ifidx)17907 uint32 dhd_get_htsf(dhd_info_t *dhd, int ifidx)
17908 {
17909 	uint32 htsf = 0, cur_cycle, delta, delta_us;
17910 	uint32    factor, baseval, baseval2;
17911 	cycles_t t;
17912 
17913 	t = get_cycles();
17914 	cur_cycle = t;
17915 
17916 	if (cur_cycle >  dhd->htsf.last_cycle)
17917 		delta = cur_cycle -  dhd->htsf.last_cycle;
17918 	else {
17919 		delta = cur_cycle + (0xFFFFFFFF -  dhd->htsf.last_cycle);
17920 	}
17921 
17922 	delta = delta >> 4;
17923 
17924 	if (dhd->htsf.coef) {
17925 		/* times ten to get the first digit */
17926 	        factor = (dhd->htsf.coef*10 + dhd->htsf.coefdec1);
17927 		baseval  = (delta*10)/factor;
17928 		baseval2 = (delta*10)/(factor+1);
17929 		delta_us  = (baseval -  (((baseval - baseval2) * dhd->htsf.coefdec2)) / 10);
17930 		htsf = (delta_us << 4) +  dhd->htsf.last_tsf + HTSF_BUS_DELAY;
17931 	} else {
17932 		DHD_ERROR(("-------dhd->htsf.coef = 0 -------\n"));
17933 	}
17934 
17935 	return htsf;
17936 }
17937 
dhd_dump_latency(void)17938 static void dhd_dump_latency(void)
17939 {
17940 	int i, max = 0;
17941 	int d1, d2, d3, d4, d5;
17942 
17943 	printf("T1       T2       T3       T4           d1  d2   t4-t1     i    \n");
17944 	for (i = 0; i < TSMAX; i++) {
17945 		d1 = ts[i].t2 - ts[i].t1;
17946 		d2 = ts[i].t3 - ts[i].t2;
17947 		d3 = ts[i].t4 - ts[i].t3;
17948 		d4 = ts[i].t4 - ts[i].t1;
17949 		d5 = ts[max].t4-ts[max].t1;
17950 		if (d4 > d5 && d4 > 0)  {
17951 			max = i;
17952 		}
17953 		printf("%08X %08X %08X %08X \t%d %d %d   %d i=%d\n",
17954 			ts[i].t1, ts[i].t2, ts[i].t3, ts[i].t4,
17955 			d1, d2, d3, d4, i);
17956 	}
17957 
17958 	printf("current idx = %d \n", tsidx);
17959 
17960 	printf("Highest latency %d pkt no.%d total=%d\n", maxdelay, maxdelaypktno, tspktcnt);
17961 	printf("%08X %08X %08X %08X \t%d %d %d   %d\n",
17962 	maxdelayts.t1, maxdelayts.t2, maxdelayts.t3, maxdelayts.t4,
17963 	maxdelayts.t2 - maxdelayts.t1,
17964 	maxdelayts.t3 - maxdelayts.t2,
17965 	maxdelayts.t4 - maxdelayts.t3,
17966 	maxdelayts.t4 - maxdelayts.t1);
17967 }
17968 
17969 
17970 static int
dhd_ioctl_htsf_get(dhd_info_t * dhd,int ifidx)17971 dhd_ioctl_htsf_get(dhd_info_t *dhd, int ifidx)
17972 {
17973 	char buf[32];
17974 	int ret;
17975 	uint32 s1, s2;
17976 
17977 	struct tsf {
17978 		uint32 low;
17979 		uint32 high;
17980 	} tsf_buf;
17981 
17982 	memset(&tsf_buf, 0, sizeof(tsf_buf));
17983 
17984 	s1 = dhd_get_htsf(dhd, 0);
17985 	ret = dhd_iovar(&dhd->pub, ifidx, "tsf", NULL, 0, buf, sizeof(buf), FALSE);
17986 	if (ret < 0) {
17987 		if (ret == -EIO) {
17988 			DHD_ERROR(("%s: tsf is not supported by device\n",
17989 				dhd_ifname(&dhd->pub, ifidx)));
17990 			return -EOPNOTSUPP;
17991 		}
17992 		return ret;
17993 	}
17994 	s2 = dhd_get_htsf(dhd, 0);
17995 
17996 	memcpy(&tsf_buf, buf, sizeof(tsf_buf));
17997 	printf(" TSF_h=%04X lo=%08X Calc:htsf=%08X, coef=%d.%d%d delta=%d ",
17998 		tsf_buf.high, tsf_buf.low, s2, dhd->htsf.coef, dhd->htsf.coefdec1,
17999 		dhd->htsf.coefdec2, s2-tsf_buf.low);
18000 	printf("lasttsf=%08X lastcycle=%08X\n", dhd->htsf.last_tsf, dhd->htsf.last_cycle);
18001 	return 0;
18002 }
18003 
htsf_update(dhd_info_t * dhd,void * data)18004 void htsf_update(dhd_info_t *dhd, void *data)
18005 {
18006 	static ulong  cur_cycle = 0, prev_cycle = 0;
18007 	uint32 htsf, tsf_delta = 0;
18008 	uint32 hfactor = 0, cyc_delta, dec1 = 0, dec2, dec3, tmp;
18009 	ulong b, a;
18010 	cycles_t t;
18011 
18012 	/* cycles_t in inlcude/mips/timex.h */
18013 
18014 	t = get_cycles();
18015 
18016 	prev_cycle = cur_cycle;
18017 	cur_cycle = t;
18018 
18019 	if (cur_cycle > prev_cycle)
18020 		cyc_delta = cur_cycle - prev_cycle;
18021 	else {
18022 		b = cur_cycle;
18023 		a = prev_cycle;
18024 		cyc_delta = cur_cycle + (0xFFFFFFFF - prev_cycle);
18025 	}
18026 
18027 	if (data == NULL)
18028 		printf(" tsf update ata point er is null \n");
18029 
18030 	memcpy(&prev_tsf, &cur_tsf, sizeof(tsf_t));
18031 	memcpy(&cur_tsf, data, sizeof(tsf_t));
18032 
18033 	if (cur_tsf.low == 0) {
18034 		DHD_INFO((" ---- 0 TSF, do not update, return\n"));
18035 		return;
18036 	}
18037 
18038 	if (cur_tsf.low > prev_tsf.low)
18039 		tsf_delta = (cur_tsf.low - prev_tsf.low);
18040 	else {
18041 		DHD_INFO((" ---- tsf low is smaller cur_tsf= %08X, prev_tsf=%08X, \n",
18042 		 cur_tsf.low, prev_tsf.low));
18043 		if (cur_tsf.high > prev_tsf.high) {
18044 			tsf_delta = cur_tsf.low + (0xFFFFFFFF - prev_tsf.low);
18045 			DHD_INFO((" ---- Wrap around tsf coutner  adjusted TSF=%08X\n", tsf_delta));
18046 		} else {
18047 			return; /* do not update */
18048 		}
18049 	}
18050 
18051 	if (tsf_delta)  {
18052 		hfactor = cyc_delta / tsf_delta;
18053 		tmp  = 	(cyc_delta - (hfactor * tsf_delta))*10;
18054 		dec1 =  tmp/tsf_delta;
18055 		dec2 =  ((tmp - dec1*tsf_delta)*10) / tsf_delta;
18056 		tmp  = 	(tmp   - (dec1*tsf_delta))*10;
18057 		dec3 =  ((tmp - dec2*tsf_delta)*10) / tsf_delta;
18058 
18059 		if (dec3 > 4) {
18060 			if (dec2 == 9) {
18061 				dec2 = 0;
18062 				if (dec1 == 9) {
18063 					dec1 = 0;
18064 					hfactor++;
18065 				} else {
18066 					dec1++;
18067 				}
18068 			} else {
18069 				dec2++;
18070 			}
18071 		}
18072 	}
18073 
18074 	if (hfactor) {
18075 		htsf = ((cyc_delta * 10)  / (hfactor*10+dec1)) + prev_tsf.low;
18076 		dhd->htsf.coef = hfactor;
18077 		dhd->htsf.last_cycle = cur_cycle;
18078 		dhd->htsf.last_tsf = cur_tsf.low;
18079 		dhd->htsf.coefdec1 = dec1;
18080 		dhd->htsf.coefdec2 = dec2;
18081 	} else {
18082 		htsf = prev_tsf.low;
18083 	}
18084 }
18085 
18086 #endif /* WLMEDIA_HTSF */
18087 
18088 #ifdef CUSTOM_SET_CPUCORE
dhd_set_cpucore(dhd_pub_t * dhd,int set)18089 void dhd_set_cpucore(dhd_pub_t *dhd, int set)
18090 {
18091 	int e_dpc = 0, e_rxf = 0, retry_set = 0;
18092 
18093 	if (!(dhd->chan_isvht80)) {
18094 		DHD_ERROR(("%s: chan_status(%d) cpucore!!!\n", __FUNCTION__, dhd->chan_isvht80));
18095 		return;
18096 	}
18097 
18098 	if (DPC_CPUCORE) {
18099 		do {
18100 			if (set == TRUE) {
18101 				e_dpc = set_cpus_allowed_ptr(dhd->current_dpc,
18102 					cpumask_of(DPC_CPUCORE));
18103 			} else {
18104 				e_dpc = set_cpus_allowed_ptr(dhd->current_dpc,
18105 					cpumask_of(PRIMARY_CPUCORE));
18106 			}
18107 			if (retry_set++ > MAX_RETRY_SET_CPUCORE) {
18108 				DHD_ERROR(("%s: dpc(%d) invalid cpu!\n", __FUNCTION__, e_dpc));
18109 				return;
18110 			}
18111 			if (e_dpc < 0)
18112 				OSL_SLEEP(1);
18113 		} while (e_dpc < 0);
18114 	}
18115 	if (RXF_CPUCORE) {
18116 		do {
18117 			if (set == TRUE) {
18118 				e_rxf = set_cpus_allowed_ptr(dhd->current_rxf,
18119 					cpumask_of(RXF_CPUCORE));
18120 			} else {
18121 				e_rxf = set_cpus_allowed_ptr(dhd->current_rxf,
18122 					cpumask_of(PRIMARY_CPUCORE));
18123 			}
18124 			if (retry_set++ > MAX_RETRY_SET_CPUCORE) {
18125 				DHD_ERROR(("%s: rxf(%d) invalid cpu!\n", __FUNCTION__, e_rxf));
18126 				return;
18127 			}
18128 			if (e_rxf < 0)
18129 				OSL_SLEEP(1);
18130 		} while (e_rxf < 0);
18131 	}
18132 #ifdef DHD_OF_SUPPORT
18133 	interrupt_set_cpucore(set, DPC_CPUCORE, PRIMARY_CPUCORE);
18134 #endif /* DHD_OF_SUPPORT */
18135 	DHD_TRACE(("%s: set(%d) cpucore success!\n", __FUNCTION__, set));
18136 
18137 	return;
18138 }
18139 #endif /* CUSTOM_SET_CPUCORE */
18140 
18141 #ifdef DHD_MCAST_REGEN
18142 /* Get interface specific ap_isolate configuration */
dhd_get_mcast_regen_bss_enable(dhd_pub_t * dhdp,uint32 idx)18143 int dhd_get_mcast_regen_bss_enable(dhd_pub_t *dhdp, uint32 idx)
18144 {
18145 	dhd_info_t *dhd = dhdp->info;
18146 	dhd_if_t *ifp;
18147 
18148 	ASSERT(idx < DHD_MAX_IFS);
18149 
18150 	ifp = dhd->iflist[idx];
18151 
18152 	return ifp->mcast_regen_bss_enable;
18153 }
18154 
18155 /* Set interface specific mcast_regen configuration */
dhd_set_mcast_regen_bss_enable(dhd_pub_t * dhdp,uint32 idx,int val)18156 int dhd_set_mcast_regen_bss_enable(dhd_pub_t *dhdp, uint32 idx, int val)
18157 {
18158 	dhd_info_t *dhd = dhdp->info;
18159 	dhd_if_t *ifp;
18160 
18161 	ASSERT(idx < DHD_MAX_IFS);
18162 
18163 	ifp = dhd->iflist[idx];
18164 
18165 	ifp->mcast_regen_bss_enable = val;
18166 
18167 	/* Disable rx_pkt_chain feature for interface, if mcast_regen feature
18168 	 * is enabled
18169 	 */
18170 	dhd_update_rx_pkt_chainable_state(dhdp, idx);
18171 	return BCME_OK;
18172 }
18173 #endif	/* DHD_MCAST_REGEN */
18174 
18175 /* Get interface specific ap_isolate configuration */
dhd_get_ap_isolate(dhd_pub_t * dhdp,uint32 idx)18176 int dhd_get_ap_isolate(dhd_pub_t *dhdp, uint32 idx)
18177 {
18178 	dhd_info_t *dhd = dhdp->info;
18179 	dhd_if_t *ifp;
18180 
18181 	ASSERT(idx < DHD_MAX_IFS);
18182 
18183 	ifp = dhd->iflist[idx];
18184 
18185 	return ifp->ap_isolate;
18186 }
18187 
18188 /* Set interface specific ap_isolate configuration */
dhd_set_ap_isolate(dhd_pub_t * dhdp,uint32 idx,int val)18189 int dhd_set_ap_isolate(dhd_pub_t *dhdp, uint32 idx, int val)
18190 {
18191 	dhd_info_t *dhd = dhdp->info;
18192 	dhd_if_t *ifp;
18193 
18194 	ASSERT(idx < DHD_MAX_IFS);
18195 
18196 	ifp = dhd->iflist[idx];
18197 
18198 	if (ifp)
18199 		ifp->ap_isolate = val;
18200 
18201 	return 0;
18202 }
18203 
18204 #ifdef DHD_FW_COREDUMP
18205 #if defined(CONFIG_X86)
18206 #define MEMDUMPINFO_LIVE "/installmedia/.memdump.info"
18207 #define MEMDUMPINFO_INST "/data/.memdump.info"
18208 #endif /* CONFIG_X86 && OEM_ANDROID */
18209 
18210 #ifdef CUSTOMER_HW4_DEBUG
18211 #define MEMDUMPINFO PLATFORM_PATH".memdump.info"
18212 #elif defined(CUSTOMER_HW2)
18213 #define MEMDUMPINFO "/data/misc/wifi/.memdump.info"
18214 #elif (defined(BOARD_PANDA) || defined(__ARM_ARCH_7A__))
18215 #define MEMDUMPINFO "/data/misc/wifi/.memdump.info"
18216 #else
18217 #define MEMDUMPINFO "/data/misc/wifi/.memdump.info"
18218 #endif /* CUSTOMER_HW4_DEBUG */
18219 
dhd_get_memdump_info(dhd_pub_t * dhd)18220 void dhd_get_memdump_info(dhd_pub_t *dhd)
18221 {
18222 	struct file *fp = NULL;
18223 	uint32 mem_val = DUMP_MEMFILE_MAX;
18224 	int ret = 0;
18225 	char *filepath = MEMDUMPINFO;
18226 
18227 	/* Read memdump info from the file */
18228 	fp = filp_open(filepath, O_RDONLY, 0);
18229 	if (IS_ERR(fp)) {
18230 		DHD_PRINT("%s: File [%s] doesn't exist\n", __FUNCTION__, filepath);
18231 #if defined(CONFIG_X86)
18232 		/* Check if it is Live Brix Image */
18233 		if (strcmp(filepath, MEMDUMPINFO_LIVE) != 0) {
18234 			goto done;
18235 		}
18236 		/* Try if it is Installed Brix Image */
18237 		filepath = MEMDUMPINFO_INST;
18238 		DHD_ERROR(("%s: Try File [%s]\n", __FUNCTION__, filepath));
18239 		fp = filp_open(filepath, O_RDONLY, 0);
18240 		if (IS_ERR(fp)) {
18241 			DHD_ERROR(("%s: File [%s] doesn't exist\n", __FUNCTION__, filepath));
18242 			goto done;
18243 		}
18244 #else /* Non Brix Android platform */
18245 		goto done;
18246 #endif /* CONFIG_X86 && OEM_ANDROID */
18247 	}
18248 
18249 	/* Handle success case */
18250 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
18251 	ret = kernel_read(fp, (char *)&mem_val, 4, NULL);
18252 #else
18253 	ret = kernel_read(fp, 0, (char *)&mem_val, 4);
18254 #endif
18255 	if (ret < 0) {
18256 		DHD_ERROR(("%s: File read error, ret=%d\n", __FUNCTION__, ret));
18257 		filp_close(fp, NULL);
18258 		goto done;
18259 	}
18260 
18261 	mem_val = bcm_atoi((char *)&mem_val);
18262 
18263 	filp_close(fp, NULL);
18264 
18265 #ifdef DHD_INIT_DEFAULT_MEMDUMP
18266 	if (mem_val == 0 || mem_val == DUMP_MEMFILE_MAX)
18267 		mem_val = DUMP_MEMFILE_BUGON;
18268 #endif /* DHD_INIT_DEFAULT_MEMDUMP */
18269 
18270 done:
18271 #ifdef CUSTOMER_HW4_DEBUG
18272 	dhd->memdump_enabled = (mem_val < DUMP_MEMFILE_MAX) ? mem_val : DUMP_DISABLED;
18273 #else
18274 	dhd->memdump_enabled = (mem_val < DUMP_MEMFILE_MAX) ? mem_val : DUMP_MEMFILE;
18275 #endif /* CUSTOMER_HW4_DEBUG */
18276 
18277 	DHD_PRINT("%s: MEMDUMP ENABLED = %d\n", __FUNCTION__, dhd->memdump_enabled);
18278 }
18279 
dhd_schedule_memdump(dhd_pub_t * dhdp,uint8 * buf,uint32 size)18280 void dhd_schedule_memdump(dhd_pub_t *dhdp, uint8 *buf, uint32 size)
18281 {
18282 	dhd_dump_t *dump = NULL;
18283 	dump = (dhd_dump_t *)MALLOC(dhdp->osh, sizeof(dhd_dump_t));
18284 	if (dump == NULL) {
18285 		DHD_ERROR(("%s: dhd dump memory allocation failed\n", __FUNCTION__));
18286 		return;
18287 	}
18288 	dump->buf = buf;
18289 	dump->bufsize = size;
18290 
18291 #if defined(CONFIG_ARM64)
18292 	DHD_ERROR(("%s: buf(va)=%llx, buf(pa)=%llx, bufsize=%d\n", __FUNCTION__,
18293 		(uint64)buf, (uint64)__virt_to_phys((ulong)buf), size));
18294 #elif defined(__ARM_ARCH_7A__)
18295 	DHD_ERROR(("%s: buf(va)=%x, buf(pa)=%x, bufsize=%d\n", __FUNCTION__,
18296 		(uint32)buf, (uint32)__virt_to_phys((ulong)buf), size));
18297 #endif /* __ARM_ARCH_7A__ */
18298 	if (dhdp->memdump_enabled == DUMP_MEMONLY) {
18299 		BUG_ON(1);
18300 	}
18301 
18302 #ifdef DHD_LOG_DUMP
18303 	if (dhdp->memdump_type != DUMP_TYPE_BY_SYSDUMP) {
18304 		dhd_schedule_log_dump(dhdp);
18305 	}
18306 #endif /* DHD_LOG_DUMP */
18307 	dhd_deferred_schedule_work(dhdp->info->dhd_deferred_wq, (void *)dump,
18308 		DHD_WQ_WORK_SOC_RAM_DUMP, dhd_mem_dump, DHD_WQ_WORK_PRIORITY_HIGH);
18309 }
18310 
18311 static void
dhd_mem_dump(void * handle,void * event_info,u8 event)18312 dhd_mem_dump(void *handle, void *event_info, u8 event)
18313 {
18314 	dhd_info_t *dhd = handle;
18315 	dhd_dump_t *dump = event_info;
18316 
18317 	if (!dhd) {
18318 		DHD_ERROR(("%s: dhd is NULL\n", __FUNCTION__));
18319 		return;
18320 	}
18321 
18322 	if (!dump) {
18323 		DHD_ERROR(("%s: dump is NULL\n", __FUNCTION__));
18324 		return;
18325 	}
18326 
18327 	if (write_dump_to_file(&dhd->pub, dump->buf, dump->bufsize, "mem_dump")) {
18328 		DHD_ERROR(("%s: writing SoC_RAM dump to the file failed\n", __FUNCTION__));
18329 		dhd->pub.memdump_success = FALSE;
18330 	}
18331 
18332 	if (dhd->pub.memdump_enabled == DUMP_MEMFILE_BUGON &&
18333 #ifdef DHD_LOG_DUMP
18334 		dhd->pub.memdump_type != DUMP_TYPE_BY_SYSDUMP &&
18335 #endif /* DHD_LOG_DUMP */
18336 #ifdef DHD_DEBUG_UART
18337 		dhd->pub.memdump_success == TRUE &&
18338 #endif	/* DHD_DEBUG_UART */
18339 		dhd->pub.memdump_type != DUMP_TYPE_CFG_VENDOR_TRIGGERED) {
18340 
18341 #ifdef SHOW_LOGTRACE
18342 		/* Wait till event_log_dispatcher_work finishes */
18343 		cancel_work_sync(&dhd->event_log_dispatcher_work);
18344 #endif /* SHOW_LOGTRACE */
18345 
18346 		BUG_ON(1);
18347 	}
18348 	MFREE(dhd->pub.osh, dump, sizeof(dhd_dump_t));
18349 }
18350 #endif /* DHD_FW_COREDUMP */
18351 
18352 #ifdef DHD_SSSR_DUMP
18353 
18354 static void
dhd_sssr_dump(void * handle,void * event_info,u8 event)18355 dhd_sssr_dump(void *handle, void *event_info, u8 event)
18356 {
18357 	dhd_info_t *dhd = handle;
18358 	dhd_pub_t *dhdp;
18359 	int i;
18360 	char before_sr_dump[128];
18361 	char after_sr_dump[128];
18362 
18363 	if (!dhd) {
18364 		DHD_ERROR(("%s: dhd is NULL\n", __FUNCTION__));
18365 		return;
18366 	}
18367 
18368 	dhdp = &dhd->pub;
18369 
18370 	for (i = 0; i < MAX_NUM_D11CORES; i++) {
18371 		/* Init file name */
18372 		memset(before_sr_dump, 0, sizeof(before_sr_dump));
18373 		memset(after_sr_dump, 0, sizeof(after_sr_dump));
18374 
18375 		snprintf(before_sr_dump, sizeof(before_sr_dump), "%s_%d_%s",
18376 			"sssr_core", i, "before_SR");
18377 		snprintf(after_sr_dump, sizeof(after_sr_dump), "%s_%d_%s",
18378 			"sssr_core", i, "after_SR");
18379 
18380 		if (dhdp->sssr_d11_before[i] && dhdp->sssr_d11_outofreset[i]) {
18381 			if (write_dump_to_file(dhdp, (uint8 *)dhdp->sssr_d11_before[i],
18382 				dhdp->sssr_reg_info.mac_regs[i].sr_size, before_sr_dump)) {
18383 				DHD_ERROR(("%s: writing SSSR MAIN dump before to the file failed\n",
18384 					__FUNCTION__));
18385 			}
18386 		}
18387 		if (dhdp->sssr_d11_after[i] && dhdp->sssr_d11_outofreset[i]) {
18388 			if (write_dump_to_file(dhdp, (uint8 *)dhdp->sssr_d11_after[i],
18389 				dhdp->sssr_reg_info.mac_regs[i].sr_size, after_sr_dump)) {
18390 				DHD_ERROR(("%s: writing SSSR AUX dump after to the file failed\n",
18391 					__FUNCTION__));
18392 			}
18393 		}
18394 	}
18395 
18396 	if (dhdp->sssr_vasip_buf_before) {
18397 		if (write_dump_to_file(dhdp, (uint8 *)dhdp->sssr_vasip_buf_before,
18398 			dhdp->sssr_reg_info.vasip_regs.vasip_sr_size, "sssr_vasip_before_SR")) {
18399 			DHD_ERROR(("%s: writing SSSR VASIP dump before to the file failed\n",
18400 				__FUNCTION__));
18401 		}
18402 	}
18403 
18404 	if (dhdp->sssr_vasip_buf_after) {
18405 		if (write_dump_to_file(dhdp, (uint8 *)dhdp->sssr_vasip_buf_after,
18406 			dhdp->sssr_reg_info.vasip_regs.vasip_sr_size, "sssr_vasip_after_SR")) {
18407 			DHD_ERROR(("%s: writing SSSR VASIP dump after to the file failed\n",
18408 				__FUNCTION__));
18409 		}
18410 	}
18411 
18412 }
18413 
18414 void
dhd_schedule_sssr_dump(dhd_pub_t * dhdp)18415 dhd_schedule_sssr_dump(dhd_pub_t *dhdp)
18416 {
18417 	dhd_deferred_schedule_work(dhdp->info->dhd_deferred_wq, NULL,
18418 		DHD_WQ_WORK_SSSR_DUMP, dhd_sssr_dump, DHD_WQ_WORK_PRIORITY_HIGH);
18419 }
18420 #endif /* DHD_SSSR_DUMP */
18421 
18422 #ifdef DHD_LOG_DUMP
18423 static void
dhd_log_dump(void * handle,void * event_info,u8 event)18424 dhd_log_dump(void *handle, void *event_info, u8 event)
18425 {
18426 	dhd_info_t *dhd = handle;
18427 
18428 	if (!dhd) {
18429 		DHD_ERROR(("%s: dhd is NULL\n", __FUNCTION__));
18430 		return;
18431 	}
18432 
18433 	if (do_dhd_log_dump(&dhd->pub)) {
18434 		DHD_ERROR(("%s: writing debug dump to the file failed\n", __FUNCTION__));
18435 		return;
18436 	}
18437 }
18438 
dhd_schedule_log_dump(dhd_pub_t * dhdp)18439 void dhd_schedule_log_dump(dhd_pub_t *dhdp)
18440 {
18441 	dhd_deferred_schedule_work(dhdp->info->dhd_deferred_wq,
18442 		(void*)NULL, DHD_WQ_WORK_DHD_LOG_DUMP,
18443 		dhd_log_dump, DHD_WQ_WORK_PRIORITY_HIGH);
18444 }
18445 
18446 static int
do_dhd_log_dump(dhd_pub_t * dhdp)18447 do_dhd_log_dump(dhd_pub_t *dhdp)
18448 {
18449 	int ret = 0, i = 0;
18450 	struct file *fp = NULL;
18451 	mm_segment_t old_fs;
18452 	loff_t pos = 0;
18453 	unsigned int wr_size = 0;
18454 	char dump_path[128];
18455 	struct osl_timespec curtime;
18456 	uint32 file_mode;
18457 	unsigned long flags = 0;
18458 	struct dhd_log_dump_buf *dld_buf = &g_dld_buf[0];
18459 
18460 	const char *pre_strs =
18461 		"-------------------- General log ---------------------------\n";
18462 
18463 	const char *post_strs =
18464 		"-------------------- Specific log --------------------------\n";
18465 
18466 	if (!dhdp) {
18467 		return -1;
18468 	}
18469 
18470 	DHD_ERROR(("DHD version: %s\n", dhd_version));
18471 	DHD_ERROR(("F/W version: %s\n", fw_version));
18472 
18473 	/* change to KERNEL_DS address limit */
18474 	old_fs = get_fs();
18475 	set_fs(KERNEL_DS);
18476 
18477 	/* Init file name */
18478 	memset(dump_path, 0, sizeof(dump_path));
18479 	osl_do_gettimeofday(&curtime);
18480 	snprintf(dump_path, sizeof(dump_path), "%s_%ld.%ld",
18481 		DHD_COMMON_DUMP_PATH "debug_dump",
18482 		(unsigned long)curtime.tv_sec, (unsigned long)curtime.tv_usec);
18483 	file_mode = O_CREAT | O_WRONLY | O_SYNC;
18484 
18485 	DHD_ERROR(("debug_dump_path = %s\n", dump_path));
18486 	fp = filp_open(dump_path, file_mode, 0664);
18487 	if (IS_ERR(fp)) {
18488 		ret = PTR_ERR(fp);
18489 		DHD_ERROR(("open file error, err = %d\n", ret));
18490 		goto exit;
18491 	}
18492 
18493 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
18494 	ret = kernel_write(fp, pre_strs, strlen(pre_strs), &pos);
18495 #else
18496 	ret = vfs_write(fp, pre_strs, strlen(pre_strs), &pos);
18497 #endif
18498 	if (ret < 0) {
18499 		DHD_ERROR(("write file error, err = %d\n", ret));
18500 		goto exit;
18501 	}
18502 
18503 	do {
18504 		unsigned int buf_size = (unsigned int)(dld_buf->max -
18505 			(unsigned long)dld_buf->buffer);
18506 		if (dld_buf->wraparound) {
18507 			wr_size = buf_size;
18508 		} else {
18509 			if (!dld_buf->buffer[0]) { /* print log if buf is empty. */
18510 				DHD_ERROR_EX(("Buffer is empty. No event/log.\n"));
18511 			}
18512 			wr_size = (unsigned int)(dld_buf->present - dld_buf->front);
18513 		}
18514 
18515 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
18516 		ret = kernel_write(fp, dld_buf->buffer, wr_size, &pos);
18517 #else
18518 		ret = vfs_write(fp, dld_buf->buffer, wr_size, &pos);
18519 #endif
18520 		if (ret < 0) {
18521 			DHD_ERROR(("write file error, err = %d\n", ret));
18522 			goto exit;
18523 		}
18524 
18525 		/* re-init dhd_log_dump_buf structure */
18526 		spin_lock_irqsave(&dld_buf->lock, flags);
18527 		dld_buf->wraparound = 0;
18528 		dld_buf->present = dld_buf->front;
18529 		dld_buf->remain = buf_size;
18530 		bzero(dld_buf->buffer, buf_size);
18531 		spin_unlock_irqrestore(&dld_buf->lock, flags);
18532 		ret = BCME_OK;
18533 
18534 		if (++i < DLD_BUFFER_NUM) {
18535 			dld_buf = &g_dld_buf[i];
18536 		} else {
18537 			break;
18538 		}
18539 
18540 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
18541 		ret = kernel_write(fp, post_strs, strlen(post_strs), &pos);
18542 #else
18543 		ret = vfs_write(fp, post_strs, strlen(post_strs), &pos);
18544 #endif
18545 		if (ret < 0) {
18546 			DHD_ERROR(("write file error, err = %d\n", ret));
18547 			goto exit;
18548 		}
18549 	} while (1);
18550 
18551 exit:
18552 #if defined(STAT_REPORT)
18553 	if (!IS_ERR(fp) && ret >= 0) {
18554 		wl_stat_report_file_save(dhdp, fp);
18555 	}
18556 #endif /* STAT_REPORT */
18557 
18558 	if (!IS_ERR(fp)) {
18559 		filp_close(fp, NULL);
18560 	}
18561 	set_fs(old_fs);
18562 
18563 	return ret;
18564 }
18565 #endif /* DHD_LOG_DUMP */
18566 
18567 
18568 #ifdef BCMASSERT_LOG
18569 #ifdef CUSTOMER_HW4_DEBUG
18570 #define ASSERTINFO PLATFORM_PATH".assert.info"
18571 #elif defined(CUSTOMER_HW2)
18572 #define ASSERTINFO "/data/misc/wifi/.assert.info"
18573 #else
18574 #define ASSERTINFO "/installmedia/.assert.info"
18575 #endif /* CUSTOMER_HW4_DEBUG */
dhd_get_assert_info(dhd_pub_t * dhd)18576 void dhd_get_assert_info(dhd_pub_t *dhd)
18577 {
18578 	struct file *fp = NULL;
18579 	char *filepath = ASSERTINFO;
18580 	int mem_val = -1;
18581 
18582 	/*
18583 	 * Read assert info from the file
18584 	 * 0: Trigger Kernel crash by panic()
18585 	 * 1: Print out the logs and don't trigger Kernel panic. (default)
18586 	 * 2: Trigger Kernel crash by BUG()
18587 	 * File doesn't exist: Keep default value (1).
18588 	 */
18589 	fp = filp_open(filepath, O_RDONLY, 0);
18590 	if (IS_ERR(fp)) {
18591 		DHD_ERROR(("%s: File [%s] doesn't exist\n", __FUNCTION__, filepath));
18592 	} else {
18593 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
18594 		ssize_t ret = kernel_read(fp, (char *)&mem_val, 4, NULL);
18595 #else
18596 		int ret = kernel_read(fp, 0, (char *)&mem_val, 4);
18597 #endif
18598 		if (ret < 0) {
18599 			DHD_ERROR(("%s: File read error, ret=%d\n", __FUNCTION__, ret));
18600 		} else {
18601 			mem_val = bcm_atoi((char *)&mem_val);
18602 			DHD_ERROR(("%s: ASSERT ENABLED = %d\n", __FUNCTION__, mem_val));
18603 		}
18604 		filp_close(fp, NULL);
18605 	}
18606 #ifdef CUSTOMER_HW4_DEBUG
18607 		/* By default. set to 1, No Kernel Panic */
18608 		g_assert_type = (mem_val >= 0) ? mem_val : 1;
18609 #else
18610 		/* By default. set to 0, Kernel Panic */
18611 		g_assert_type = (mem_val >= 0) ? mem_val : 0;
18612 #endif
18613 }
18614 #endif /* BCMASSERT_LOG */
18615 
18616 /*
18617  * This call is to get the memdump size so that,
18618  * halutil can alloc that much buffer in user space.
18619  */
18620 int
dhd_os_socram_dump(struct net_device * dev,uint32 * dump_size)18621 dhd_os_socram_dump(struct net_device *dev, uint32 *dump_size)
18622 {
18623 	int ret = BCME_OK;
18624 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
18625 	dhd_pub_t *dhdp = &dhd->pub;
18626 
18627 	if (dhdp->busstate == DHD_BUS_DOWN) {
18628 		DHD_ERROR(("%s: bus is down\n", __FUNCTION__));
18629 		return BCME_ERROR;
18630 	}
18631 
18632 	if (DHD_BUS_CHECK_SUSPEND_OR_SUSPEND_IN_PROGRESS(dhdp)) {
18633 		DHD_ERROR(("%s: bus is in suspend(%d) or suspending(0x%x) state, so skip\n",
18634 			__FUNCTION__, dhdp->busstate, dhdp->dhd_bus_busy_state));
18635 		return BCME_ERROR;
18636 	}
18637 
18638 	ret = dhd_common_socram_dump(dhdp);
18639 	if (ret == BCME_OK) {
18640 		*dump_size = dhdp->soc_ram_length;
18641 	}
18642 	return ret;
18643 }
18644 
18645 /*
18646  * This is to get the actual memdup after getting the memdump size
18647  */
18648 int
dhd_os_get_socram_dump(struct net_device * dev,char ** buf,uint32 * size)18649 dhd_os_get_socram_dump(struct net_device *dev, char **buf, uint32 *size)
18650 {
18651 	int ret = BCME_OK;
18652 	int orig_len = 0;
18653 	dhd_info_t *dhd = *(dhd_info_t **)netdev_priv(dev);
18654 	dhd_pub_t *dhdp = &dhd->pub;
18655 	if (buf == NULL)
18656 		return BCME_ERROR;
18657 	orig_len = *size;
18658 	if (dhdp->soc_ram) {
18659 		if (orig_len >= dhdp->soc_ram_length) {
18660 			memcpy(*buf, dhdp->soc_ram, dhdp->soc_ram_length);
18661 			/* reset the storage of dump */
18662 			memset(dhdp->soc_ram, 0, dhdp->soc_ram_length);
18663 			*size = dhdp->soc_ram_length;
18664 		} else {
18665 			ret = BCME_BUFTOOSHORT;
18666 			DHD_ERROR(("The length of the buffer is too short"
18667 				" to save the memory dump with %d\n", dhdp->soc_ram_length));
18668 		}
18669 	} else {
18670 		DHD_ERROR(("socram_dump is not ready to get\n"));
18671 		ret = BCME_NOTREADY;
18672 	}
18673 	return ret;
18674 }
18675 
18676 int
dhd_os_get_version(struct net_device * dev,bool dhd_ver,char ** buf,uint32 size)18677 dhd_os_get_version(struct net_device *dev, bool dhd_ver, char **buf, uint32 size)
18678 {
18679 	char *fw_str;
18680 
18681 	if (size == 0)
18682 		return BCME_BADARG;
18683 
18684 	fw_str = strstr(info_string, "Firmware: ");
18685 	if (fw_str == NULL) {
18686 		return BCME_ERROR;
18687 	}
18688 
18689 	memset(*buf, 0, size);
18690 	if (dhd_ver) {
18691 		strncpy(*buf, dhd_version, size - 1);
18692 	} else {
18693 		strncpy(*buf, fw_str, size - 1);
18694 	}
18695 	return BCME_OK;
18696 }
18697 
18698 #ifdef DHD_WMF
18699 /* Returns interface specific WMF configuration */
dhd_wmf_conf(dhd_pub_t * dhdp,uint32 idx)18700 dhd_wmf_t* dhd_wmf_conf(dhd_pub_t *dhdp, uint32 idx)
18701 {
18702 	dhd_info_t *dhd = dhdp->info;
18703 	dhd_if_t *ifp;
18704 
18705 	ASSERT(idx < DHD_MAX_IFS);
18706 
18707 	ifp = dhd->iflist[idx];
18708 	return &ifp->wmf;
18709 }
18710 #endif /* DHD_WMF */
18711 
18712 #if defined(TRAFFIC_MGMT_DWM)
traffic_mgmt_pkt_set_prio(dhd_pub_t * dhdp,void * pktbuf)18713 void traffic_mgmt_pkt_set_prio(dhd_pub_t *dhdp, void * pktbuf)
18714 {
18715 	struct ether_header *eh;
18716 	struct ethervlan_header *evh;
18717 	uint8 *pktdata, *ip_body;
18718 	uint8  dwm_filter;
18719 	uint8 tos_tc = 0;
18720 	uint8 dscp   = 0;
18721 	pktdata = (uint8 *)PKTDATA(dhdp->osh, pktbuf);
18722 	eh = (struct ether_header *) pktdata;
18723 	ip_body = NULL;
18724 
18725 	if (dhdp->dhd_tm_dwm_tbl.dhd_dwm_enabled) {
18726 		if (eh->ether_type == hton16(ETHER_TYPE_8021Q)) {
18727 			evh = (struct ethervlan_header *)eh;
18728 			if ((evh->ether_type == hton16(ETHER_TYPE_IP)) ||
18729 				(evh->ether_type == hton16(ETHER_TYPE_IPV6))) {
18730 				ip_body = pktdata + sizeof(struct ethervlan_header);
18731 			}
18732 		} else if ((eh->ether_type == hton16(ETHER_TYPE_IP)) ||
18733 			(eh->ether_type == hton16(ETHER_TYPE_IPV6))) {
18734 			ip_body = pktdata + sizeof(struct ether_header);
18735 		}
18736 		if (ip_body) {
18737 			tos_tc = IP_TOS46(ip_body);
18738 			dscp = tos_tc >> IPV4_TOS_DSCP_SHIFT;
18739 		}
18740 
18741 		if (dscp < DHD_DWM_TBL_SIZE) {
18742 			dwm_filter = dhdp->dhd_tm_dwm_tbl.dhd_dwm_tbl[dscp];
18743 			if (DHD_TRF_MGMT_DWM_IS_FILTER_SET(dwm_filter)) {
18744 				PKTSETPRIO(pktbuf, DHD_TRF_MGMT_DWM_PRIO(dwm_filter));
18745 			}
18746 		}
18747 	}
18748 }
18749 #endif
18750 
dhd_sta_associated(dhd_pub_t * dhdp,uint32 bssidx,uint8 * mac)18751 bool dhd_sta_associated(dhd_pub_t *dhdp, uint32 bssidx, uint8 *mac)
18752 {
18753 	return dhd_find_sta(dhdp, bssidx, mac) ? TRUE : FALSE;
18754 }
18755 
18756 #ifdef DHD_L2_FILTER
18757 arp_table_t*
dhd_get_ifp_arp_table_handle(dhd_pub_t * dhdp,uint32 bssidx)18758 dhd_get_ifp_arp_table_handle(dhd_pub_t *dhdp, uint32 bssidx)
18759 {
18760 	dhd_info_t *dhd = dhdp->info;
18761 	dhd_if_t *ifp;
18762 
18763 	ASSERT(bssidx < DHD_MAX_IFS);
18764 
18765 	ifp = dhd->iflist[bssidx];
18766 	return ifp->phnd_arp_table;
18767 }
18768 
dhd_get_parp_status(dhd_pub_t * dhdp,uint32 idx)18769 int dhd_get_parp_status(dhd_pub_t *dhdp, uint32 idx)
18770 {
18771 	dhd_info_t *dhd = dhdp->info;
18772 	dhd_if_t *ifp;
18773 
18774 	ASSERT(idx < DHD_MAX_IFS);
18775 
18776 	ifp = dhd->iflist[idx];
18777 
18778 	if (ifp)
18779 		return ifp->parp_enable;
18780 	else
18781 		return FALSE;
18782 }
18783 
18784 /* Set interface specific proxy arp configuration */
dhd_set_parp_status(dhd_pub_t * dhdp,uint32 idx,int val)18785 int dhd_set_parp_status(dhd_pub_t *dhdp, uint32 idx, int val)
18786 {
18787 	dhd_info_t *dhd = dhdp->info;
18788 	dhd_if_t *ifp;
18789 	ASSERT(idx < DHD_MAX_IFS);
18790 	ifp = dhd->iflist[idx];
18791 
18792 	if (!ifp)
18793 	    return BCME_ERROR;
18794 
18795 	/* At present all 3 variables are being
18796 	 * handled at once
18797 	 */
18798 	ifp->parp_enable = val;
18799 	ifp->parp_discard = val;
18800 	ifp->parp_allnode = val;
18801 
18802 	/* Flush ARP entries when disabled */
18803 	if (val == FALSE) {
18804 		bcm_l2_filter_arp_table_update(dhdp->osh, ifp->phnd_arp_table, TRUE, NULL,
18805 			FALSE, dhdp->tickcnt);
18806 	}
18807 	return BCME_OK;
18808 }
18809 
dhd_parp_discard_is_enabled(dhd_pub_t * dhdp,uint32 idx)18810 bool dhd_parp_discard_is_enabled(dhd_pub_t *dhdp, uint32 idx)
18811 {
18812 	dhd_info_t *dhd = dhdp->info;
18813 	dhd_if_t *ifp;
18814 
18815 	ASSERT(idx < DHD_MAX_IFS);
18816 
18817 	ifp = dhd->iflist[idx];
18818 
18819 	ASSERT(ifp);
18820 	return ifp->parp_discard;
18821 }
18822 
18823 bool
dhd_parp_allnode_is_enabled(dhd_pub_t * dhdp,uint32 idx)18824 dhd_parp_allnode_is_enabled(dhd_pub_t *dhdp, uint32 idx)
18825 {
18826 	dhd_info_t *dhd = dhdp->info;
18827 	dhd_if_t *ifp;
18828 
18829 	ASSERT(idx < DHD_MAX_IFS);
18830 
18831 	ifp = dhd->iflist[idx];
18832 
18833 	ASSERT(ifp);
18834 
18835 	return ifp->parp_allnode;
18836 }
18837 
dhd_get_dhcp_unicast_status(dhd_pub_t * dhdp,uint32 idx)18838 int dhd_get_dhcp_unicast_status(dhd_pub_t *dhdp, uint32 idx)
18839 {
18840 	dhd_info_t *dhd = dhdp->info;
18841 	dhd_if_t *ifp;
18842 
18843 	ASSERT(idx < DHD_MAX_IFS);
18844 
18845 	ifp = dhd->iflist[idx];
18846 
18847 	ASSERT(ifp);
18848 
18849 	return ifp->dhcp_unicast;
18850 }
18851 
dhd_set_dhcp_unicast_status(dhd_pub_t * dhdp,uint32 idx,int val)18852 int dhd_set_dhcp_unicast_status(dhd_pub_t *dhdp, uint32 idx, int val)
18853 {
18854 	dhd_info_t *dhd = dhdp->info;
18855 	dhd_if_t *ifp;
18856 	ASSERT(idx < DHD_MAX_IFS);
18857 	ifp = dhd->iflist[idx];
18858 
18859 	ASSERT(ifp);
18860 
18861 	ifp->dhcp_unicast = val;
18862 	return BCME_OK;
18863 }
18864 
dhd_get_block_ping_status(dhd_pub_t * dhdp,uint32 idx)18865 int dhd_get_block_ping_status(dhd_pub_t *dhdp, uint32 idx)
18866 {
18867 	dhd_info_t *dhd = dhdp->info;
18868 	dhd_if_t *ifp;
18869 
18870 	ASSERT(idx < DHD_MAX_IFS);
18871 
18872 	ifp = dhd->iflist[idx];
18873 
18874 	ASSERT(ifp);
18875 
18876 	return ifp->block_ping;
18877 }
18878 
dhd_set_block_ping_status(dhd_pub_t * dhdp,uint32 idx,int val)18879 int dhd_set_block_ping_status(dhd_pub_t *dhdp, uint32 idx, int val)
18880 {
18881 	dhd_info_t *dhd = dhdp->info;
18882 	dhd_if_t *ifp;
18883 	ASSERT(idx < DHD_MAX_IFS);
18884 	ifp = dhd->iflist[idx];
18885 
18886 	ASSERT(ifp);
18887 
18888 	ifp->block_ping = val;
18889 	/* Disable rx_pkt_chain feature for interface if block_ping option is
18890 	 * enabled
18891 	 */
18892 	dhd_update_rx_pkt_chainable_state(dhdp, idx);
18893 	return BCME_OK;
18894 }
18895 
dhd_get_grat_arp_status(dhd_pub_t * dhdp,uint32 idx)18896 int dhd_get_grat_arp_status(dhd_pub_t *dhdp, uint32 idx)
18897 {
18898 	dhd_info_t *dhd = dhdp->info;
18899 	dhd_if_t *ifp;
18900 
18901 	ASSERT(idx < DHD_MAX_IFS);
18902 
18903 	ifp = dhd->iflist[idx];
18904 
18905 	ASSERT(ifp);
18906 
18907 	return ifp->grat_arp;
18908 }
18909 
dhd_set_grat_arp_status(dhd_pub_t * dhdp,uint32 idx,int val)18910 int dhd_set_grat_arp_status(dhd_pub_t *dhdp, uint32 idx, int val)
18911 {
18912 	dhd_info_t *dhd = dhdp->info;
18913 	dhd_if_t *ifp;
18914 	ASSERT(idx < DHD_MAX_IFS);
18915 	ifp = dhd->iflist[idx];
18916 
18917 	ASSERT(ifp);
18918 
18919 	ifp->grat_arp = val;
18920 
18921 	return BCME_OK;
18922 }
18923 #endif /* DHD_L2_FILTER */
18924 
18925 
18926 #if defined(SET_RPS_CPUS)
dhd_rps_cpus_enable(struct net_device * net,int enable)18927 int dhd_rps_cpus_enable(struct net_device *net, int enable)
18928 {
18929 	dhd_info_t *dhd = DHD_DEV_INFO(net);
18930 	dhd_if_t *ifp;
18931 	int ifidx;
18932 	char * RPS_CPU_SETBUF;
18933 
18934 	ifidx = dhd_net2idx(dhd, net);
18935 	if (ifidx == DHD_BAD_IF) {
18936 		DHD_ERROR(("%s bad ifidx\n", __FUNCTION__));
18937 		return -ENODEV;
18938 	}
18939 
18940 	if (ifidx == PRIMARY_INF) {
18941 		if (dhd->pub.op_mode == DHD_FLAG_IBSS_MODE) {
18942 			DHD_INFO(("%s : set for IBSS.\n", __FUNCTION__));
18943 			RPS_CPU_SETBUF = RPS_CPUS_MASK_IBSS;
18944 		} else {
18945 			DHD_INFO(("%s : set for BSS.\n", __FUNCTION__));
18946 			RPS_CPU_SETBUF = RPS_CPUS_MASK;
18947 		}
18948 	} else if (ifidx == VIRTUAL_INF) {
18949 		DHD_INFO(("%s : set for P2P.\n", __FUNCTION__));
18950 		RPS_CPU_SETBUF = RPS_CPUS_MASK_P2P;
18951 	} else {
18952 		DHD_ERROR(("%s : Invalid index : %d.\n", __FUNCTION__, ifidx));
18953 		return -EINVAL;
18954 	}
18955 
18956 	ifp = dhd->iflist[ifidx];
18957 	if (ifp) {
18958 		if (enable) {
18959 			DHD_INFO(("%s : set rps_cpus as [%s]\n", __FUNCTION__, RPS_CPU_SETBUF));
18960 			custom_rps_map_set(ifp->net->_rx, RPS_CPU_SETBUF, strlen(RPS_CPU_SETBUF));
18961 		} else {
18962 			custom_rps_map_clear(ifp->net->_rx);
18963 		}
18964 	} else {
18965 		DHD_ERROR(("%s : ifp is NULL!!\n", __FUNCTION__));
18966 		return -ENODEV;
18967 	}
18968 	return BCME_OK;
18969 }
18970 
custom_rps_map_set(struct netdev_rx_queue * queue,char * buf,size_t len)18971 int custom_rps_map_set(struct netdev_rx_queue *queue, char *buf, size_t len)
18972 {
18973 	struct rps_map *old_map, *map;
18974 	cpumask_var_t mask;
18975 	int err, cpu, i;
18976 	static DEFINE_SPINLOCK(rps_map_lock);
18977 
18978 	DHD_INFO(("%s : Entered.\n", __FUNCTION__));
18979 
18980 	if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
18981 		DHD_ERROR(("%s : alloc_cpumask_var fail.\n", __FUNCTION__));
18982 		return -ENOMEM;
18983 	}
18984 
18985 	err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
18986 	if (err) {
18987 		free_cpumask_var(mask);
18988 		DHD_ERROR(("%s : bitmap_parse fail.\n", __FUNCTION__));
18989 		return err;
18990 	}
18991 
18992 	map = kzalloc(max_t(unsigned int,
18993 		RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
18994 		GFP_KERNEL);
18995 	if (!map) {
18996 		free_cpumask_var(mask);
18997 		DHD_ERROR(("%s : map malloc fail.\n", __FUNCTION__));
18998 		return -ENOMEM;
18999 	}
19000 
19001 	i = 0;
19002 	for_each_cpu(cpu, mask) {
19003 		map->cpus[i++] = cpu;
19004 	}
19005 
19006 	if (i) {
19007 		map->len = i;
19008 	} else {
19009 		kfree(map);
19010 		map = NULL;
19011 		free_cpumask_var(mask);
19012 		DHD_ERROR(("%s : mapping cpu fail.\n", __FUNCTION__));
19013 		return -1;
19014 	}
19015 
19016 	spin_lock(&rps_map_lock);
19017 	old_map = rcu_dereference_protected(queue->rps_map,
19018 		lockdep_is_held(&rps_map_lock));
19019 	rcu_assign_pointer(queue->rps_map, map);
19020 	spin_unlock(&rps_map_lock);
19021 
19022 	if (map) {
19023 		static_key_slow_inc(&rps_needed);
19024 	}
19025 	if (old_map) {
19026 		kfree_rcu(old_map, rcu);
19027 		static_key_slow_dec(&rps_needed);
19028 	}
19029 	free_cpumask_var(mask);
19030 
19031 	DHD_INFO(("%s : Done. mapping cpu nummber : %d\n", __FUNCTION__, map->len));
19032 	return map->len;
19033 }
19034 
custom_rps_map_clear(struct netdev_rx_queue * queue)19035 void custom_rps_map_clear(struct netdev_rx_queue *queue)
19036 {
19037 	struct rps_map *map;
19038 
19039 	DHD_INFO(("%s : Entered.\n", __FUNCTION__));
19040 
19041 	map = rcu_dereference_protected(queue->rps_map, 1);
19042 	if (map) {
19043 		RCU_INIT_POINTER(queue->rps_map, NULL);
19044 		kfree_rcu(map, rcu);
19045 		DHD_INFO(("%s : rps_cpus map clear.\n", __FUNCTION__));
19046 	}
19047 }
19048 #endif
19049 
19050 
19051 
19052 #ifdef DHD_DEBUG_PAGEALLOC
19053 
19054 void
dhd_page_corrupt_cb(void * handle,void * addr_corrupt,size_t len)19055 dhd_page_corrupt_cb(void *handle, void *addr_corrupt, size_t len)
19056 {
19057 	dhd_pub_t *dhdp = (dhd_pub_t *)handle;
19058 
19059 	DHD_ERROR(("%s: Got dhd_page_corrupt_cb 0x%p %d\n",
19060 		__FUNCTION__, addr_corrupt, (uint32)len));
19061 
19062 	DHD_OS_WAKE_LOCK(dhdp);
19063 	prhex("Page Corruption:", addr_corrupt, len);
19064 	dhd_dump_to_kernelog(dhdp);
19065 #if defined(BCMPCIE) && defined(DHD_FW_COREDUMP)
19066 	/* Load the dongle side dump to host memory and then BUG_ON() */
19067 	dhdp->memdump_enabled = DUMP_MEMONLY;
19068 	dhdp->memdump_type = DUMP_TYPE_MEMORY_CORRUPTION;
19069 	dhd_bus_mem_dump(dhdp);
19070 #endif /* BCMPCIE && DHD_FW_COREDUMP */
19071 	DHD_OS_WAKE_UNLOCK(dhdp);
19072 }
19073 EXPORT_SYMBOL(dhd_page_corrupt_cb);
19074 #endif /* DHD_DEBUG_PAGEALLOC */
19075 
19076 #if defined(BCMPCIE) && defined(DHD_PKTID_AUDIT_ENABLED)
19077 void
dhd_pktid_error_handler(dhd_pub_t * dhdp)19078 dhd_pktid_error_handler(dhd_pub_t *dhdp)
19079 {
19080 	DHD_ERROR(("%s: Got Pkt Id Audit failure \n", __FUNCTION__));
19081 	DHD_OS_WAKE_LOCK(dhdp);
19082 	dhd_dump_to_kernelog(dhdp);
19083 #ifdef DHD_FW_COREDUMP
19084 	/* Load the dongle side dump to host memory */
19085 	if (dhdp->memdump_enabled == DUMP_DISABLED) {
19086 		dhdp->memdump_enabled = DUMP_MEMFILE;
19087 	}
19088 	dhdp->memdump_type = DUMP_TYPE_PKTID_AUDIT_FAILURE;
19089 	dhd_bus_mem_dump(dhdp);
19090 #endif /* DHD_FW_COREDUMP */
19091 	dhdp->hang_reason = HANG_REASON_PCIE_PKTID_ERROR;
19092 	dhd_os_check_hang(dhdp, 0, -EREMOTEIO);
19093 	DHD_OS_WAKE_UNLOCK(dhdp);
19094 }
19095 #endif /* BCMPCIE && DHD_PKTID_AUDIT_ENABLED */
19096 
19097 struct net_device *
dhd_linux_get_primary_netdev(dhd_pub_t * dhdp)19098 dhd_linux_get_primary_netdev(dhd_pub_t *dhdp)
19099 {
19100 	dhd_info_t *dhd = dhdp->info;
19101 
19102 	if (dhd->iflist[0] && dhd->iflist[0]->net)
19103 		return dhd->iflist[0]->net;
19104 	else
19105 		return NULL;
19106 }
19107 
19108 #ifdef DHD_ARP_DUMP
19109 #define ARP_PRINT(str) \
19110 	do { \
19111 		printk("[dhd-%s] " str " [%s] : %s(%s) %s %s(%s)\n", \
19112 			ifname, tx?"TX":"RX", \
19113 			tx?sabuf:dabuf, tx?seabuf:deabuf, \
19114 			tx?"->":"<-", tx?dabuf:sabuf, tx?deabuf:seabuf); \
19115 	} while (0)
19116 
19117 #define ARP_PRINT_OTHER(str) \
19118 	do { \
19119 		printk("[dhd-%s] " str " [%s] : %s(%s) %s %s(%s) op_code=%d\n", \
19120 			ifname, tx?"TX":"RX", \
19121 			tx?sabuf:dabuf, tx?seabuf:deabuf, \
19122 			tx?"->":"<-", tx?dabuf:sabuf, tx?deabuf:seabuf, opcode); \
19123 	} while (0)
19124 
19125 static void
dhd_arp_dump(char * ifname,uint8 * pktdata,bool tx)19126 dhd_arp_dump(char *ifname, uint8 *pktdata, bool tx)
19127 {
19128 	uint8 *pkt = (uint8 *)&pktdata[ETHER_HDR_LEN];
19129 	struct bcmarp *arph = (struct bcmarp *)pkt;
19130 	uint16 opcode;
19131 	char sabuf[20]="", dabuf[20]="";
19132 	char seabuf[ETHER_ADDR_STR_LEN]="";
19133 	char deabuf[ETHER_ADDR_STR_LEN]="";
19134 
19135 	if (!(dump_msg_level & DUMP_ARP_VAL))
19136 		return;
19137 
19138 	/* validation check */
19139 	if (arph->htype != hton16(HTYPE_ETHERNET) ||
19140 		arph->hlen != ETHER_ADDR_LEN ||
19141 		arph->plen != 4) {
19142 		return;
19143 	}
19144 
19145 	opcode = ntoh16(arph->oper);
19146 	bcm_ip_ntoa((struct ipv4_addr *)arph->src_ip, sabuf);
19147 	bcm_ip_ntoa((struct ipv4_addr *)arph->dst_ip, dabuf);
19148 	bcm_ether_ntoa((struct ether_addr *)arph->dst_eth, deabuf);
19149 	bcm_ether_ntoa((struct ether_addr *)arph->src_eth, seabuf);
19150 	if (opcode == ARP_OPC_REQUEST) {
19151 		ARP_PRINT("ARP REQUEST ");
19152 	} else if (opcode == ARP_OPC_REPLY) {
19153 		ARP_PRINT("ARP RESPONSE");
19154 	} else {
19155 		ARP_PRINT_OTHER("ARP OTHER");
19156 	}
19157 }
19158 #endif /* DHD_ARP_DUMP */
19159 
19160 struct tcp_fmt {
19161 	struct iphdr ip_header;
19162 	struct tcphdr tcp_header;
19163 };
19164 
19165 #define KP_PORT 443
19166 
19167 static void
dhd_tcp_dump(char * ifname,uint8 * pktdata,bool tx)19168 dhd_tcp_dump(char *ifname, uint8 *pktdata, bool tx)
19169 {
19170 	struct tcp_fmt *b = (struct tcp_fmt *) &pktdata[ETHER_HDR_LEN];
19171 	struct iphdr *h = &b->ip_header;
19172 	uint32 ip_saddr, ip_daddr, c, max_c, opt, tsval_t, tsecr_t;
19173 	unsigned char *opts;
19174 
19175 	/* check IP header */
19176 	if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_TCP) {
19177 		return;
19178 	}
19179 
19180 	if ((ntohs(b->tcp_header.source) != KP_PORT) && (ntohs(b->tcp_header.dest) != KP_PORT))
19181 		return;
19182 
19183 	if (tx) {
19184 		ip_saddr = h->saddr;
19185 		ip_daddr = h->daddr;
19186 		ip_id = ntohs(h->id);
19187 		bcm_ip_ntoa((struct ipv4_addr *)&ip_saddr, sabuf);
19188 		bcm_ip_ntoa((struct ipv4_addr *)&ip_daddr, dabuf);
19189 		bcm_ether_ntoa((struct ether_addr *)pktdata, deabuf);
19190 		bcm_ether_ntoa((struct ether_addr *)(pktdata+6), seabuf);
19191 		source = ntohs(b->tcp_header.source);
19192 		dest = ntohs(b->tcp_header.dest);
19193 		seq = ntohl(b->tcp_header.seq);
19194 		seq_ack = ntohl(b->tcp_header.ack_seq);
19195 		window = ntohs(b->tcp_header.window);
19196 		tcp_data_len = ntohs(h->tot_len) - 20 - (b->tcp_header.doff * 4);
19197 	}
19198 
19199 	tcp_option_len = (b->tcp_header.doff * 4) - 20;
19200 	if (tcp_option_len) {
19201 		max_c = tcp_option_len;
19202 		opts = &pktdata[ETHER_HDR_LEN + 40];
19203 		for (c = 0; c < max_c; ) {
19204 			opt = opts[c];
19205 			switch (opt) {
19206 			case 0x00:
19207 				/* End of options. */
19208 				pr_debug("rk tcp_parseopt: EOL\n");
19209 				return;
19210 			case 0x01:
19211 				/* NOP option. */
19212 				++c;
19213 				pr_debug("rk tcp_parseopt: NOP\n");
19214 				break;
19215 			case 0x02:
19216 				pr_debug("rk tcp_parseopt: MSS\n");
19217 				if (opts[c + 1] != 0x04 || c + 0x04 > max_c) {
19218 					/* Bad length */
19219 					pr_err("rk tcp_parseopt: bad length\n");
19220 					return;
19221 				}
19222 				/* Advance to next option */
19223 				c += 0x04;
19224 				break;
19225 			case 0x08:
19226 				pr_debug("rk tcp_parseopt: TS\n");
19227 				if (opts[c + 1] != 0x0A || c + 0x0A > max_c) {
19228 				  /* Bad length */
19229 				  pr_err("rk tcp_parseopt: bad length\n");
19230 				  return;
19231 				}
19232 				/* TCP timestamp option with valid length */
19233 				tsval_t = (opts[c+2]) | (opts[c+3] << 8) |
19234 				  (opts[c+4] << 16) | (opts[c+5] << 24);
19235 				tsecr_t = (opts[c+6]) | (opts[c+7] << 8) |
19236 				  (opts[c+8] << 16) | (opts[c+9] << 24);
19237 
19238 				if (tx) {
19239 					tsval = ntohl(tsval_t);
19240 					tsecr = ntohl(tsecr_t);
19241 				} else {
19242 					tsecr = ntohl(tsval_t);
19243 					tsval = ntohl(tsecr_t);
19244 				}
19245 				/* Advance to next option */
19246 				c += 0x0A;
19247 				break;
19248 			default:
19249 				pr_debug("rk tcp_parseopt: other\n");
19250 				if (opts[c + 1] == 0) {
19251 				  pr_err("rk tcp_parseopt: bad length\n");
19252 				  /* If the length field is zero, the options are malformed
19253 					 and we don't process them further. */
19254 				  return;
19255 				}
19256 				/* All other options have a length field, so that we easily
19257 				   can skip past them. */
19258 				c += opts[c + 1];
19259 			}
19260 		}
19261 	}
19262 
19263 	if (tx)
19264 		pr_debug("[%s-%s] %s %s %s %s %d, %d, %d, %8u, %8u, %d, %d, %d, %8u, %8u\n", ifname, tx ? "tx" : "rx",
19265 					sabuf, dabuf, seabuf, deabuf, ip_id, source, dest, seq, seq_ack, window,
19266 					tcp_option_len, tcp_data_len, tsval, tsecr);
19267 }
19268 
19269 #ifdef DHD_DHCP_DUMP
19270 #define DHCP_PRINT(str) \
19271 	do { \
19272 		printk("[dhd-%s] " str " %8s, %8s [%s] : %s(%s) %s %s(%s)\n", \
19273 			ifname, dhcp_types[dhcp_type], dhcp_ops[b->op], \
19274 			tx?"TX":"RX", \
19275 			tx?sabuf:dabuf, tx?seabuf:deabuf, \
19276 			tx?"->":"<-", tx?dabuf:sabuf, tx?deabuf:seabuf); \
19277 	} while (0)
19278 static void
dhd_dhcp_dump(char * ifname,uint8 * pktdata,bool tx)19279 dhd_dhcp_dump(char *ifname, uint8 *pktdata, bool tx)
19280 {
19281 	struct bootp_fmt *b = (struct bootp_fmt *) &pktdata[ETHER_HDR_LEN];
19282 	struct iphdr *h = &b->ip_header;
19283 	uint8 *ptr, *opt, *end = (uint8 *) b + ntohs(b->ip_header.tot_len);
19284 	int dhcp_type = 0, len, opt_len;
19285 	uint32 ip_saddr, ip_daddr;
19286 	char sabuf[20]="", dabuf[20]="";
19287 	char seabuf[ETHER_ADDR_STR_LEN]="";
19288 	char deabuf[ETHER_ADDR_STR_LEN]="";
19289 
19290 	if (!(dump_msg_level & DUMP_DHCP_VAL))
19291 		return;
19292 
19293 	/* check IP header */
19294 	if (h->ihl != 5 || h->version != 4 || h->protocol != IPPROTO_UDP) {
19295 		return;
19296 	}
19297 
19298 	/* check UDP port for bootp (67, 68) */
19299 	if (b->udp_header.source != htons(67) && b->udp_header.source != htons(68) &&
19300 			b->udp_header.dest != htons(67) && b->udp_header.dest != htons(68)) {
19301 		return;
19302 	}
19303 
19304 	/* check header length */
19305 	if (ntohs(h->tot_len) < ntohs(b->udp_header.len) + sizeof(struct iphdr)) {
19306 		return;
19307 	}
19308 	ip_saddr = h->saddr;
19309 	ip_daddr = h->daddr;
19310 	bcm_ip_ntoa((struct ipv4_addr *)&ip_saddr, sabuf);
19311 	bcm_ip_ntoa((struct ipv4_addr *)&ip_daddr, dabuf);
19312 	bcm_ether_ntoa((struct ether_addr *)pktdata, deabuf);
19313 	bcm_ether_ntoa((struct ether_addr *)(pktdata+6), seabuf);
19314 
19315 	len = ntohs(b->udp_header.len) - sizeof(struct udphdr);
19316 	opt_len = len
19317 		- (sizeof(*b) - sizeof(struct iphdr) - sizeof(struct udphdr) - sizeof(b->options));
19318 
19319 	/* parse bootp options */
19320 	if (opt_len >= 4 && !memcmp(b->options, bootp_magic_cookie, 4)) {
19321 		ptr = &b->options[4];
19322 		while (ptr < end && *ptr != 0xff) {
19323 			opt = ptr++;
19324 			if (*opt == 0) {
19325 				continue;
19326 			}
19327 			ptr += *ptr + 1;
19328 			if (ptr >= end) {
19329 				break;
19330 			}
19331 			/* 53 is dhcp type */
19332 			if (*opt == 53) {
19333 				if (opt[1]) {
19334 					dhcp_type = opt[2];
19335 					DHCP_PRINT("DHCP");
19336 					break;
19337 				}
19338 			}
19339 		}
19340 	}
19341 }
19342 #endif /* DHD_DHCP_DUMP */
19343 
19344 #ifdef DHD_ICMP_DUMP
19345 #define ICMP_TYPE_ECHO_REQUEST	8	/* ICMP type echo request */
19346 #define ICMP_TYPE_ECHO_REPLY		0	/* ICMP type echo reply */
19347 #define ICMP_TYPE_DEST_UNREACH		3
19348 #define ICMP_ECHO_SEQ_OFFSET		6
19349 #define ICMP_ECHO_SEQ(h) (*(uint16 *)((uint8 *)(h) + (ICMP_ECHO_SEQ_OFFSET)))
19350 #define ICMP_PING_PRINT(str) \
19351 	do { \
19352 		printk("[dhd-%s] " str " [%2s] : %s(%s) %s %s(%s) SEQNUM=%d\n", \
19353 			ifname, tx?"TX":"RX", tx?sabuf:dabuf, tx?seabuf:deabuf, \
19354 			tx?"->":"<-", tx?dabuf:sabuf, tx?deabuf:seabuf, seqnum); \
19355 	} while (0)
19356 #define ICMP_PRINT(str) \
19357 	do { \
19358 		printk("[dhd-%s] " str " [%2s] : %s(%s) %s %s(%s)\n", \
19359 			ifname, tx?"TX":"RX", tx?sabuf:dabuf, tx?seabuf:deabuf, \
19360 			tx?"->":"<-", tx?dabuf:sabuf, tx?deabuf:seabuf); \
19361 	} while (0)
19362 static void
dhd_icmp_dump(char * ifname,uint8 * pktdata,bool tx)19363 dhd_icmp_dump(char *ifname, uint8 *pktdata, bool tx)
19364 {
19365 	uint8 *pkt = (uint8 *)&pktdata[ETHER_HDR_LEN];
19366 	struct iphdr *iph = (struct iphdr *)pkt;
19367 	struct icmphdr *icmph;
19368 	uint32 ip_saddr, ip_daddr;
19369 	char sabuf[20]="", dabuf[20]="";
19370 	char seabuf[ETHER_ADDR_STR_LEN]="";
19371 	char deabuf[ETHER_ADDR_STR_LEN]="";
19372 	uint16 seqnum, type, code;
19373 
19374 	if (!(dump_msg_level & DUMP_ICMP_VAL))
19375 		return;
19376 
19377 	/* check IP header */
19378 	if (iph->ihl != 5 || iph->version != 4 || iph->protocol != IP_PROT_ICMP) {
19379 		return;
19380 	}
19381 
19382 	icmph = (struct icmphdr *)((uint8 *)pkt + sizeof(struct iphdr));
19383 	seqnum = 0;
19384 	type = icmph->type;
19385 	code = icmph->code;
19386 	ip_saddr = iph->saddr;
19387 	ip_daddr = iph->daddr;
19388 	bcm_ip_ntoa((struct ipv4_addr *)&ip_saddr, sabuf);
19389 	bcm_ip_ntoa((struct ipv4_addr *)&ip_daddr, dabuf);
19390 	bcm_ether_ntoa((struct ether_addr *)pktdata, deabuf);
19391 	bcm_ether_ntoa((struct ether_addr *)(pktdata+6), seabuf);
19392 	if (type == ICMP_TYPE_ECHO_REQUEST) {
19393 		seqnum = ntoh16(ICMP_ECHO_SEQ(icmph));
19394 		ICMP_PING_PRINT("PING REQUEST");
19395 	} else if (type == ICMP_TYPE_ECHO_REPLY) {
19396 		seqnum = ntoh16(ICMP_ECHO_SEQ(icmph));
19397 		ICMP_PING_PRINT("PING REPLY  ");
19398 	} else if (type == ICMP_TYPE_DEST_UNREACH) {
19399 		ICMP_PRINT("ICMP DEST UNREACH");
19400 	} else {
19401 		ICMP_PRINT("ICMP OTHER");
19402 	}
19403 }
19404 #endif /* DHD_ICMP_DUMP */
19405 
19406 #ifdef SHOW_LOGTRACE
19407 void
dhd_get_read_buf_ptr(dhd_pub_t * dhd_pub,trace_buf_info_t * trace_buf_info)19408 dhd_get_read_buf_ptr(dhd_pub_t *dhd_pub, trace_buf_info_t *trace_buf_info)
19409 {
19410 	dhd_dbg_ring_status_t ring_status;
19411 	uint32 rlen;
19412 
19413 	rlen = dhd_dbg_ring_pull_single(dhd_pub, FW_VERBOSE_RING_ID, trace_buf_info->buf,
19414 		TRACE_LOG_BUF_MAX_SIZE, TRUE);
19415 	trace_buf_info->size = rlen;
19416 	trace_buf_info->availability = NEXT_BUF_NOT_AVAIL;
19417 	if (rlen == 0) {
19418 		trace_buf_info->availability = BUF_NOT_AVAILABLE;
19419 		return;
19420 	}
19421 	dhd_dbg_get_ring_status(dhd_pub, FW_VERBOSE_RING_ID, &ring_status);
19422 	if (ring_status.written_bytes != ring_status.read_bytes) {
19423 		trace_buf_info->availability = NEXT_BUF_AVAIL;
19424 	}
19425 }
19426 #endif /* SHOW_LOGTRACE */
19427 
19428 bool
dhd_fw_download_status(dhd_pub_t * dhd_pub)19429 dhd_fw_download_status(dhd_pub_t * dhd_pub)
19430 {
19431 	return dhd_pub->fw_download_done;
19432 }
19433 
19434 int
dhd_create_to_notifier_skt(void)19435 dhd_create_to_notifier_skt(void)
19436 {
19437 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
19438 	/* Kernel 3.7 onwards this API accepts only 3 arguments. */
19439 	/* Kernel version 3.6 is a special case which accepts 4 arguments */
19440 	nl_to_event_sk = netlink_kernel_create(&init_net, BCM_NL_USER, &g_cfg);
19441 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
19442 	/* Kernel version 3.5 and below use this old API format */
19443 	nl_to_event_sk = netlink_kernel_create(&init_net, BCM_NL_USER, 0,
19444 			dhd_process_daemon_msg, NULL, THIS_MODULE);
19445 #else
19446 	nl_to_event_sk = netlink_kernel_create(&init_net, BCM_NL_USER, THIS_MODULE, &g_cfg);
19447 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)) */
19448 	if (!nl_to_event_sk)
19449 	{
19450 		printf("Error creating socket.\n");
19451 		return -1;
19452 	}
19453 	DHD_INFO(("nl_to socket created successfully...\n"));
19454 	return 0;
19455 }
19456 
19457 void
dhd_destroy_to_notifier_skt(void)19458 dhd_destroy_to_notifier_skt(void)
19459 {
19460 	DHD_INFO(("Destroying nl_to socket\n"));
19461 	if (nl_to_event_sk) {
19462 		netlink_kernel_release(nl_to_event_sk);
19463 	}
19464 }
19465 
19466 static void
dhd_recv_msg_from_daemon(struct sk_buff * skb)19467 dhd_recv_msg_from_daemon(struct sk_buff *skb)
19468 {
19469 	struct nlmsghdr *nlh;
19470 	bcm_to_info_t *cmd;
19471 
19472 	nlh = (struct nlmsghdr *)skb->data;
19473 	cmd = (bcm_to_info_t *)nlmsg_data(nlh);
19474 	if ((cmd->magic == BCM_TO_MAGIC) && (cmd->reason == REASON_DAEMON_STARTED)) {
19475 		sender_pid = ((struct nlmsghdr *)(skb->data))->nlmsg_pid;
19476 		DHD_INFO(("DHD Daemon Started\n"));
19477 	}
19478 }
19479 
19480 int
dhd_send_msg_to_daemon(struct sk_buff * skb,void * data,int size)19481 dhd_send_msg_to_daemon(struct sk_buff *skb, void *data, int size)
19482 {
19483 	struct nlmsghdr *nlh;
19484 	struct sk_buff *skb_out;
19485 
19486 	if (!nl_to_event_sk) {
19487 		DHD_INFO(("No socket available\n"));
19488 		return -1;
19489 	}
19490 
19491 	BCM_REFERENCE(skb);
19492 	if (sender_pid == 0) {
19493 		DHD_INFO(("Invalid PID 0\n"));
19494 		return -1;
19495 	}
19496 
19497 	if ((skb_out = nlmsg_new(size, 0)) == NULL) {
19498 		DHD_ERROR(("%s: skb alloc failed\n", __FUNCTION__));
19499 		return -1;
19500 	}
19501 	nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, size, 0);
19502 	NETLINK_CB(skb_out).dst_group = 0; /* Unicast */
19503 	memcpy(nlmsg_data(nlh), (char *)data, size);
19504 
19505 	if ((nlmsg_unicast(nl_to_event_sk, skb_out, sender_pid)) < 0) {
19506 		DHD_INFO(("Error sending message\n"));
19507 	}
19508 	return 0;
19509 }
19510 
19511 
19512 static void
dhd_process_daemon_msg(struct sk_buff * skb)19513 dhd_process_daemon_msg(struct sk_buff *skb)
19514 {
19515 	bcm_to_info_t to_info;
19516 
19517 	to_info.magic = BCM_TO_MAGIC;
19518 	to_info.reason = REASON_DAEMON_STARTED;
19519 	to_info.trap = NO_TRAP;
19520 
19521 	dhd_recv_msg_from_daemon(skb);
19522 	dhd_send_msg_to_daemon(skb, &to_info, sizeof(to_info));
19523 }
19524 
19525 #ifdef REPORT_FATAL_TIMEOUTS
19526 static void
dhd_send_trap_to_fw(dhd_pub_t * pub,int reason,int trap)19527 dhd_send_trap_to_fw(dhd_pub_t * pub, int reason, int trap)
19528 {
19529 	bcm_to_info_t to_info;
19530 
19531 	to_info.magic = BCM_TO_MAGIC;
19532 	to_info.reason = reason;
19533 	to_info.trap = trap;
19534 
19535 	DHD_ERROR(("Sending Event reason:%d trap:%d\n", reason, trap));
19536 	dhd_send_msg_to_daemon(NULL, (void *)&to_info, sizeof(bcm_to_info_t));
19537 }
19538 
19539 void
dhd_send_trap_to_fw_for_timeout(dhd_pub_t * pub,timeout_reasons_t reason)19540 dhd_send_trap_to_fw_for_timeout(dhd_pub_t * pub, timeout_reasons_t reason)
19541 {
19542 	int to_reason;
19543 	int trap = NO_TRAP;
19544 	switch (reason) {
19545 	        case DHD_REASON_COMMAND_TO:
19546 	                to_reason = REASON_COMMAND_TO;
19547 	                trap = DO_TRAP;
19548 	                break;
19549 	        case DHD_REASON_JOIN_TO:
19550 	                to_reason = REASON_JOIN_TO;
19551 	                break;
19552 	        case DHD_REASON_SCAN_TO:
19553 	                to_reason = REASON_SCAN_TO;
19554 	                break;
19555 	        case DHD_REASON_OQS_TO:
19556 	                to_reason = REASON_OQS_TO;
19557 	                trap = DO_TRAP;
19558 	                break;
19559 	        default:
19560 	                to_reason = REASON_UNKOWN;
19561 	}
19562 	dhd_send_trap_to_fw(pub, to_reason, trap);
19563 }
19564 #endif /* REPORT_FATAL_TIMEOUTS */
19565 
19566 #ifdef DHD_LOG_DUMP
19567 void
dhd_log_dump_init(dhd_pub_t * dhd)19568 dhd_log_dump_init(dhd_pub_t *dhd)
19569 {
19570 	struct dhd_log_dump_buf *dld_buf;
19571 	int i = 0;
19572 #if defined(CONFIG_DHD_USE_STATIC_BUF) && defined(DHD_USE_STATIC_MEMDUMP)
19573 	int prealloc_idx = DHD_PREALLOC_DHD_LOG_DUMP_BUF;
19574 #endif /* CONFIG_DHD_USE_STATIC_BUF && DHD_USE_STATIC_MEMDUMP */
19575 
19576 	for (i = 0; i < DLD_BUFFER_NUM; i++) {
19577 		dld_buf = &g_dld_buf[i];
19578 		spin_lock_init(&dld_buf->lock);
19579 #if defined(CONFIG_DHD_USE_STATIC_BUF) && defined(DHD_USE_STATIC_MEMDUMP)
19580 		dld_buf->buffer = DHD_OS_PREALLOC(dhd, prealloc_idx++, dld_buf_size[i]);
19581 #else
19582 		dld_buf->buffer = kmalloc(dld_buf_size[i], GFP_KERNEL);
19583 #endif /* CONFIG_DHD_USE_STATIC_BUF && DHD_USE_STATIC_MEMDUMP */
19584 
19585 		if (!dld_buf->buffer) {
19586 			dld_buf->buffer = kmalloc(dld_buf_size[i], GFP_KERNEL);
19587 			DHD_ERROR(("Try to allocate memory using kmalloc().\n"));
19588 
19589 			if (!dld_buf->buffer) {
19590 				DHD_ERROR(("Failed to allocate memory for dld_buf[%d].\n", i));
19591 				goto fail;
19592 			}
19593 		}
19594 
19595 		dld_buf->wraparound = 0;
19596 		dld_buf->max = (unsigned long)dld_buf->buffer + dld_buf_size[i];
19597 		dld_buf->present = dld_buf->front = dld_buf->buffer;
19598 		dld_buf->remain = dld_buf_size[i];
19599 		dld_buf->enable = 1;
19600 	}
19601 	return;
19602 
19603 fail:
19604 	for (i = 0; i < DLD_BUFFER_NUM; i++) {
19605 		if (dld_buf[i].buffer) {
19606 			kfree(dld_buf[i].buffer);
19607 		}
19608 	}
19609 }
19610 
19611 void
dhd_log_dump_deinit(dhd_pub_t * dhd)19612 dhd_log_dump_deinit(dhd_pub_t *dhd)
19613 {
19614 	struct dhd_log_dump_buf *dld_buf;
19615 	int i = 0;
19616 
19617 	for (i = 0; i < DLD_BUFFER_NUM; i++) {
19618 		dld_buf = &g_dld_buf[i];
19619 		dld_buf->enable = 0;
19620 #if defined(CONFIG_DHD_USE_STATIC_BUF) && defined(DHD_USE_STATIC_MEMDUMP)
19621 		DHD_OS_PREFREE(dhd, dld_buf->buffer, dld_buf_size[i]);
19622 #else
19623 		kfree(dld_buf->buffer);
19624 #endif /* CONFIG_DHD_USE_STATIC_BUF && DHD_USE_STATIC_MEMDUMP */
19625 	}
19626 }
19627 
19628 void
dhd_log_dump_write(int type,const char * fmt,...)19629 dhd_log_dump_write(int type, const char *fmt, ...)
19630 {
19631 	int len = 0;
19632 	char tmp_buf[DHD_LOG_DUMP_MAX_TEMP_BUFFER_SIZE] = {0, };
19633 	va_list args;
19634 	unsigned long flags = 0;
19635 	struct dhd_log_dump_buf *dld_buf = NULL;
19636 
19637 	switch (type)
19638 	{
19639 		case DLD_BUF_TYPE_GENERAL:
19640 			dld_buf = &g_dld_buf[type];
19641 			break;
19642 		case DLD_BUF_TYPE_SPECIAL:
19643 			dld_buf = &g_dld_buf[type];
19644 			break;
19645 		default:
19646 			DHD_ERROR(("%s: Unknown DHD_LOG_DUMP_BUF_TYPE(%d).\n",
19647 				__FUNCTION__, type));
19648 			return;
19649 	}
19650 
19651 	if (dld_buf->enable != 1) {
19652 		return;
19653 	}
19654 
19655 	va_start(args, fmt);
19656 
19657 	len = vsnprintf(tmp_buf, DHD_LOG_DUMP_MAX_TEMP_BUFFER_SIZE, fmt, args);
19658 	/* Non ANSI C99 compliant returns -1,
19659 	 * ANSI compliant return len >= DHD_LOG_DUMP_MAX_TEMP_BUFFER_SIZE
19660 	 */
19661 	if (len < 0) {
19662 		return;
19663 	}
19664 
19665 	if (len >= DHD_LOG_DUMP_MAX_TEMP_BUFFER_SIZE) {
19666 		len = DHD_LOG_DUMP_MAX_TEMP_BUFFER_SIZE - 1;
19667 		tmp_buf[len] = '\0';
19668 	}
19669 
19670 	/* make a critical section to eliminate race conditions */
19671 	spin_lock_irqsave(&dld_buf->lock, flags);
19672 	if (dld_buf->remain < len) {
19673 		dld_buf->wraparound = 1;
19674 		dld_buf->present = dld_buf->front;
19675 		dld_buf->remain = dld_buf_size[type];
19676 	}
19677 
19678 	strncpy(dld_buf->present, tmp_buf, len);
19679 	dld_buf->remain -= len;
19680 	dld_buf->present += len;
19681 	spin_unlock_irqrestore(&dld_buf->lock, flags);
19682 
19683 	/* double check invalid memory operation */
19684 	ASSERT((unsigned long)dld_buf->present <= dld_buf->max);
19685 	va_end(args);
19686 }
19687 
19688 char*
dhd_log_dump_get_timestamp(void)19689 dhd_log_dump_get_timestamp(void)
19690 {
19691 	static char buf[16];
19692 	u64 ts_nsec;
19693 	unsigned long rem_nsec;
19694 
19695 	ts_nsec = local_clock();
19696 	rem_nsec = do_div(ts_nsec, 1000000000);
19697 	snprintf(buf, sizeof(buf), "%5lu.%06lu",
19698 		(unsigned long)ts_nsec, rem_nsec / 1000);
19699 
19700 	return buf;
19701 }
19702 #endif /* DHD_LOG_DUMP */
19703 
19704 int
dhd_write_file(const char * filepath,char * buf,int buf_len)19705 dhd_write_file(const char *filepath, char *buf, int buf_len)
19706 {
19707 	struct file *fp = NULL;
19708 	mm_segment_t old_fs;
19709 	int ret = 0;
19710 
19711 	/* change to KERNEL_DS address limit */
19712 	old_fs = get_fs();
19713 	set_fs(KERNEL_DS);
19714 
19715 	/* File is always created. */
19716 	fp = filp_open(filepath, O_RDWR | O_CREAT, 0664);
19717 	if (IS_ERR(fp)) {
19718 		DHD_ERROR(("%s: Couldn't open file '%s' err %ld\n",
19719 			__FUNCTION__, filepath, PTR_ERR(fp)));
19720 		ret = BCME_ERROR;
19721 	} else {
19722 		if (fp->f_mode & FMODE_WRITE) {
19723 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
19724 			ret = kernel_write(fp, buf, buf_len, &fp->f_pos);
19725 #else
19726 			ret = vfs_write(fp, buf, buf_len, &fp->f_pos);
19727 #endif
19728 			if (ret < 0) {
19729 				DHD_ERROR(("%s: Couldn't write file '%s'\n",
19730 					__FUNCTION__, filepath));
19731 				ret = BCME_ERROR;
19732 			} else {
19733 				ret = BCME_OK;
19734 			}
19735 		}
19736 		filp_close(fp, NULL);
19737 	}
19738 
19739 	/* restore previous address limit */
19740 	set_fs(old_fs);
19741 
19742 	return ret;
19743 }
19744 
19745 int
dhd_read_file(const char * filepath,char * buf,int buf_len)19746 dhd_read_file(const char *filepath, char *buf, int buf_len)
19747 {
19748 	struct file *fp = NULL;
19749 	mm_segment_t old_fs;
19750 	int ret;
19751 
19752 	/* change to KERNEL_DS address limit */
19753 	old_fs = get_fs();
19754 	set_fs(KERNEL_DS);
19755 
19756 	fp = filp_open(filepath, O_RDONLY, 0);
19757 	if (IS_ERR(fp)) {
19758 		set_fs(old_fs);
19759 		DHD_ERROR(("%s: File %s doesn't exist\n", __FUNCTION__, filepath));
19760 		return BCME_ERROR;
19761 	}
19762 
19763 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
19764 	ret = kernel_read(fp, buf, buf_len, NULL);
19765 #else
19766 	ret = kernel_read(fp, 0, buf, buf_len);
19767 #endif
19768 	filp_close(fp, NULL);
19769 
19770 	/* restore previous address limit */
19771 	set_fs(old_fs);
19772 
19773 	/* Return the number of bytes read */
19774 	if (ret > 0) {
19775 		/* Success to read */
19776 		ret = 0;
19777 	} else {
19778 		DHD_ERROR(("%s: Couldn't read the file %s, ret=%d\n",
19779 			__FUNCTION__, filepath, ret));
19780 		ret = BCME_ERROR;
19781 	}
19782 
19783 	return ret;
19784 }
19785 
19786 int
dhd_write_file_and_check(const char * filepath,char * buf,int buf_len)19787 dhd_write_file_and_check(const char *filepath, char *buf, int buf_len)
19788 {
19789 	int ret;
19790 
19791 	ret = dhd_write_file(filepath, buf, buf_len);
19792 	if (ret < 0) {
19793 		return ret;
19794 	}
19795 
19796 	/* Read the file again and check if the file size is not zero */
19797 	memset(buf, 0, buf_len);
19798 	ret = dhd_read_file(filepath, buf, buf_len);
19799 
19800 	return ret;
19801 }
19802 
19803 #ifdef DHD_LB_TXP
19804 #define DHD_LB_TXBOUND	64
19805 /*
19806  * Function that performs the TX processing on a given CPU
19807  */
19808 bool
dhd_lb_tx_process(dhd_info_t * dhd)19809 dhd_lb_tx_process(dhd_info_t *dhd)
19810 {
19811 	struct sk_buff *skb;
19812 	int cnt = 0;
19813 	struct net_device *net;
19814 	int ifidx;
19815 	bool resched = FALSE;
19816 
19817 	DHD_TRACE(("%s(): TX Processing \r\n", __FUNCTION__));
19818 	if (dhd == NULL) {
19819 		DHD_ERROR((" Null pointer DHD \r\n"));
19820 		return resched;
19821 	}
19822 
19823 	DHD_LB_STATS_PERCPU_ARR_INCR(dhd->txp_percpu_run_cnt);
19824 
19825 	/* Base Loop to perform the actual Tx */
19826 	do {
19827 		skb = skb_dequeue(&dhd->tx_pend_queue);
19828 		if (skb == NULL) {
19829 			DHD_TRACE(("Dequeued a Null Packet \r\n"));
19830 			break;
19831 		}
19832 		cnt++;
19833 
19834 		net =  DHD_LB_TX_PKTTAG_NETDEV((dhd_tx_lb_pkttag_fr_t *)PKTTAG(skb));
19835 		ifidx = DHD_LB_TX_PKTTAG_IFIDX((dhd_tx_lb_pkttag_fr_t *)PKTTAG(skb));
19836 
19837 		BCM_REFERENCE(net);
19838 		DHD_TRACE(("Processing skb %p for net %p index %d \r\n", skb,
19839 			net, ifidx));
19840 
19841 		__dhd_sendpkt(&dhd->pub, ifidx, skb);
19842 
19843 		if (cnt >= DHD_LB_TXBOUND) {
19844 			resched = TRUE;
19845 			break;
19846 		}
19847 
19848 	} while (1);
19849 
19850 	DHD_INFO(("%s(): Processed %d packets \r\n", __FUNCTION__, cnt));
19851 
19852 	return resched;
19853 }
19854 
19855 void
dhd_lb_tx_handler(unsigned long data)19856 dhd_lb_tx_handler(unsigned long data)
19857 {
19858 	dhd_info_t *dhd = (dhd_info_t *)data;
19859 
19860 	if (dhd_lb_tx_process(dhd)) {
19861 		dhd_tasklet_schedule(&dhd->tx_tasklet);
19862 	}
19863 }
19864 
19865 #endif /* DHD_LB_TXP */
19866 
19867 /* ----------------------------------------------------------------------------
19868  * Infrastructure code for sysfs interface support for DHD
19869  *
19870  * What is sysfs interface?
19871  * https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt
19872  *
19873  * Why sysfs interface?
19874  * This is the Linux standard way of changing/configuring Run Time parameters
19875  * for a driver. We can use this interface to control "linux" specific driver
19876  * parameters.
19877  *
19878  * -----------------------------------------------------------------------------
19879  */
19880 
19881 #include <linux/sysfs.h>
19882 #include <linux/kobject.h>
19883 
19884 #if defined(DHD_TRACE_WAKE_LOCK)
19885 
19886 /* Function to show the history buffer */
19887 static ssize_t
show_wklock_trace(struct dhd_info * dev,char * buf)19888 show_wklock_trace(struct dhd_info *dev, char *buf)
19889 {
19890 	ssize_t ret = 0;
19891 	dhd_info_t *dhd = (dhd_info_t *)dev;
19892 
19893 	buf[ret] = '\n';
19894 	buf[ret+1] = 0;
19895 
19896 	dhd_wk_lock_stats_dump(&dhd->pub);
19897 	return ret+1;
19898 }
19899 
19900 /* Function to enable/disable wakelock trace */
19901 static ssize_t
wklock_trace_onoff(struct dhd_info * dev,const char * buf,size_t count)19902 wklock_trace_onoff(struct dhd_info *dev, const char *buf, size_t count)
19903 {
19904 	unsigned long onoff;
19905 	unsigned long flags;
19906 	dhd_info_t *dhd = (dhd_info_t *)dev;
19907 
19908 	onoff = bcm_strtoul(buf, NULL, 10);
19909 	if (onoff != 0 && onoff != 1) {
19910 		return -EINVAL;
19911 	}
19912 
19913 	spin_lock_irqsave(&dhd->wakelock_spinlock, flags);
19914 	trace_wklock_onoff = onoff;
19915 	spin_unlock_irqrestore(&dhd->wakelock_spinlock, flags);
19916 	if (trace_wklock_onoff) {
19917 		printk("ENABLE WAKLOCK TRACE\n");
19918 	} else {
19919 		printk("DISABLE WAKELOCK TRACE\n");
19920 	}
19921 
19922 	return (ssize_t)(onoff+1);
19923 }
19924 #endif /* DHD_TRACE_WAKE_LOCK */
19925 
19926 #if defined(DHD_LB_TXP)
19927 static ssize_t
show_lbtxp(struct dhd_info * dev,char * buf)19928 show_lbtxp(struct dhd_info *dev, char *buf)
19929 {
19930 	ssize_t ret = 0;
19931 	unsigned long onoff;
19932 	dhd_info_t *dhd = (dhd_info_t *)dev;
19933 
19934 	onoff = atomic_read(&dhd->lb_txp_active);
19935 	ret = scnprintf(buf, PAGE_SIZE - 1, "%lu \n",
19936 		onoff);
19937 	return ret;
19938 }
19939 
19940 static ssize_t
lbtxp_onoff(struct dhd_info * dev,const char * buf,size_t count)19941 lbtxp_onoff(struct dhd_info *dev, const char *buf, size_t count)
19942 {
19943 	unsigned long onoff;
19944 	dhd_info_t *dhd = (dhd_info_t *)dev;
19945 	int i;
19946 
19947 	onoff = bcm_strtoul(buf, NULL, 10);
19948 
19949 	sscanf(buf, "%lu", &onoff);
19950 	if (onoff != 0 && onoff != 1) {
19951 		return -EINVAL;
19952 	}
19953 	atomic_set(&dhd->lb_txp_active, onoff);
19954 
19955 	/* Since the scheme is changed clear the counters */
19956 	for (i = 0; i < NR_CPUS; i++) {
19957 		DHD_LB_STATS_CLR(dhd->txp_percpu_run_cnt[i]);
19958 		DHD_LB_STATS_CLR(dhd->tx_start_percpu_run_cnt[i]);
19959 	}
19960 
19961 	return count;
19962 }
19963 
19964 #endif /* DHD_LB_TXP */
19965 /*
19966  * Generic Attribute Structure for DHD.
19967  * If we have to add a new sysfs entry under /sys/bcm-dhd/, we have
19968  * to instantiate an object of type dhd_attr,  populate it with
19969  * the required show/store functions (ex:- dhd_attr_cpumask_primary)
19970  * and add the object to default_attrs[] array, that gets registered
19971  * to the kobject of dhd (named bcm-dhd).
19972  */
19973 
19974 struct dhd_attr {
19975 	struct attribute attr;
19976 	ssize_t(*show)(struct dhd_info *, char *);
19977 	ssize_t(*store)(struct dhd_info *, const char *, size_t count);
19978 };
19979 
19980 #if defined(DHD_TRACE_WAKE_LOCK)
19981 static struct dhd_attr dhd_attr_wklock =
19982 	__ATTR(wklock_trace, 0660, show_wklock_trace, wklock_trace_onoff);
19983 #endif /* defined(DHD_TRACE_WAKE_LOCK */
19984 
19985 #if defined(DHD_LB_TXP)
19986 static struct dhd_attr dhd_attr_lbtxp =
19987 	__ATTR(lbtxp, 0660, show_lbtxp, lbtxp_onoff);
19988 #endif /* DHD_LB_TXP */
19989 
19990 /* Attribute object that gets registered with "bcm-dhd" kobject tree */
19991 static struct attribute *default_attrs[] = {
19992 #if defined(DHD_TRACE_WAKE_LOCK)
19993 	&dhd_attr_wklock.attr,
19994 #endif /* DHD_TRACE_WAKE_LOCK */
19995 #if defined(DHD_LB_TXP)
19996 	&dhd_attr_lbtxp.attr,
19997 #endif /* DHD_LB_TXP */
19998 	NULL
19999 };
20000 
20001 #define to_dhd(k) container_of(k, struct dhd_info, dhd_kobj)
20002 #define to_attr(a) container_of(a, struct dhd_attr, attr)
20003 
20004 /*
20005  * bcm-dhd kobject show function, the "attr" attribute specifices to which
20006  * node under "bcm-dhd" the show function is called.
20007  */
dhd_show(struct kobject * kobj,struct attribute * attr,char * buf)20008 static ssize_t dhd_show(struct kobject *kobj, struct attribute *attr, char *buf)
20009 {
20010 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
20011 #pragma GCC diagnostic push
20012 #pragma GCC diagnostic ignored "-Wcast-qual"
20013 #endif
20014 	dhd_info_t *dhd = to_dhd(kobj);
20015 	struct dhd_attr *d_attr = to_attr(attr);
20016 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
20017 #pragma GCC diagnostic pop
20018 #endif
20019 	int ret;
20020 
20021 	if (d_attr->show)
20022 		ret = d_attr->show(dhd, buf);
20023 	else
20024 		ret = -EIO;
20025 
20026 	return ret;
20027 }
20028 
20029 /*
20030  * bcm-dhd kobject show function, the "attr" attribute specifices to which
20031  * node under "bcm-dhd" the store function is called.
20032  */
dhd_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)20033 static ssize_t dhd_store(struct kobject *kobj, struct attribute *attr,
20034 	const char *buf, size_t count)
20035 {
20036 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
20037 #pragma GCC diagnostic push
20038 #pragma GCC diagnostic ignored "-Wcast-qual"
20039 #endif
20040 	dhd_info_t *dhd = to_dhd(kobj);
20041 	struct dhd_attr *d_attr = to_attr(attr);
20042 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
20043 #pragma GCC diagnostic pop
20044 #endif
20045 	int ret;
20046 
20047 	if (d_attr->store)
20048 		ret = d_attr->store(dhd, buf, count);
20049 	else
20050 		ret = -EIO;
20051 
20052 	return ret;
20053 
20054 }
20055 
20056 static struct sysfs_ops dhd_sysfs_ops = {
20057 	.show = dhd_show,
20058 	.store = dhd_store,
20059 };
20060 
20061 static struct kobj_type dhd_ktype = {
20062 	.sysfs_ops = &dhd_sysfs_ops,
20063 	.default_attrs = default_attrs,
20064 };
20065 
20066 #ifdef CSI_SUPPORT
20067 /* Function to show current ccode */
read_csi_data(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)20068 static ssize_t read_csi_data(struct file *filp, struct kobject *kobj,
20069 	struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
20070 {
20071 	dhd_info_t *dhd = to_dhd(kobj);
20072 	int n = 0;
20073 
20074 	n = dhd_csi_dump_list(&dhd->pub, buf);
20075 	DHD_INFO(("Dump data to file, size %d\n", n));
20076 	dhd_csi_clean_list(&dhd->pub);
20077 
20078 	return n;
20079 }
20080 
20081 static struct bin_attribute dhd_attr_csi = {
20082 	.attr = { .name = "csi",
20083 		  .mode = 0660, },
20084 	.size = MAX_CSI_FILESZ,
20085 	.read = read_csi_data,
20086 };
20087 #endif /* CSI_SUPPORT */
20088 
20089 /* Create a kobject and attach to sysfs interface */
dhd_sysfs_init(dhd_info_t * dhd)20090 static int dhd_sysfs_init(dhd_info_t *dhd)
20091 {
20092 	int ret = -1;
20093 
20094 	if (dhd == NULL) {
20095 		DHD_ERROR(("%s(): dhd is NULL \r\n", __FUNCTION__));
20096 		return ret;
20097 	}
20098 
20099 	/* Initialize the kobject */
20100 	ret = kobject_init_and_add(&dhd->dhd_kobj, &dhd_ktype, NULL, "bcm-dhd");
20101 	if (ret) {
20102 		kobject_put(&dhd->dhd_kobj);
20103 		DHD_ERROR(("%s(): Unable to allocate kobject \r\n", __FUNCTION__));
20104 		return ret;
20105 	}
20106 
20107 #ifdef CSI_SUPPORT
20108 	ret = sysfs_create_bin_file(&dhd->dhd_kobj, &dhd_attr_csi);
20109 	if (ret) {
20110 		DHD_ERROR(("%s: can't create %s\n", __FUNCTION__, dhd_attr_csi.attr.name));
20111 		kobject_put(&dhd->dhd_kobj);
20112 		return ret;
20113 	}
20114 #endif /* CSI_SUPPORT */
20115 
20116 	/*
20117 	 * We are always responsible for sending the uevent that the kobject
20118 	 * was added to the system.
20119 	 */
20120 	kobject_uevent(&dhd->dhd_kobj, KOBJ_ADD);
20121 
20122 	return ret;
20123 }
20124 
20125 /* Done with the kobject and detach the sysfs interface */
dhd_sysfs_exit(dhd_info_t * dhd)20126 static void dhd_sysfs_exit(dhd_info_t *dhd)
20127 {
20128 	if (dhd == NULL) {
20129 		DHD_ERROR(("%s(): dhd is NULL \r\n", __FUNCTION__));
20130 		return;
20131 	}
20132 
20133 	/* Releae the kobject */
20134 	if (dhd->dhd_kobj.state_initialized)
20135 		kobject_put(&dhd->dhd_kobj);
20136 }
20137 
20138 #ifdef DHD_DEBUG_UART
20139 bool
dhd_debug_uart_is_running(struct net_device * dev)20140 dhd_debug_uart_is_running(struct net_device *dev)
20141 {
20142 	dhd_info_t *dhd = DHD_DEV_INFO(dev);
20143 
20144 	if (dhd->duart_execute) {
20145 		return TRUE;
20146 	}
20147 
20148 	return FALSE;
20149 }
20150 
20151 static void
dhd_debug_uart_exec_rd(void * handle,void * event_info,u8 event)20152 dhd_debug_uart_exec_rd(void *handle, void *event_info, u8 event)
20153 {
20154 	dhd_pub_t *dhdp = handle;
20155 	dhd_debug_uart_exec(dhdp, "rd");
20156 }
20157 
20158 static void
dhd_debug_uart_exec(dhd_pub_t * dhdp,char * cmd)20159 dhd_debug_uart_exec(dhd_pub_t *dhdp, char *cmd)
20160 {
20161 	int ret;
20162 
20163 	char *argv[] = {DHD_DEBUG_UART_EXEC_PATH, cmd, NULL};
20164 	char *envp[] = {"HOME=/", "TERM=linux", "PATH=/sbin:/system/bin", NULL};
20165 
20166 #ifdef DHD_FW_COREDUMP
20167 	if (dhdp->memdump_enabled == DUMP_MEMFILE_BUGON)
20168 #endif
20169 	{
20170 		if (dhdp->hang_reason == HANG_REASON_PCIE_LINK_DOWN ||
20171 #ifdef DHD_FW_COREDUMP
20172 			dhdp->memdump_success == FALSE ||
20173 #endif
20174 			FALSE) {
20175 			dhdp->info->duart_execute = TRUE;
20176 			DHD_ERROR(("DHD: %s - execute %s %s\n",
20177 				__FUNCTION__, DHD_DEBUG_UART_EXEC_PATH, cmd));
20178 			ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
20179 			DHD_ERROR(("DHD: %s - %s %s ret = %d\n",
20180 				__FUNCTION__, DHD_DEBUG_UART_EXEC_PATH, cmd, ret));
20181 			dhdp->info->duart_execute = FALSE;
20182 
20183 #ifdef DHD_LOG_DUMP
20184 			if (dhdp->memdump_type != DUMP_TYPE_BY_SYSDUMP)
20185 #endif
20186 			{
20187 				BUG_ON(1);
20188 			}
20189 		}
20190 	}
20191 }
20192 #endif	/* DHD_DEBUG_UART */
20193 
20194 #if defined(DHD_BLOB_EXISTENCE_CHECK)
20195 void
dhd_set_blob_support(dhd_pub_t * dhdp,char * fw_path)20196 dhd_set_blob_support(dhd_pub_t *dhdp, char *fw_path)
20197 {
20198 	struct file *fp;
20199 	char *filepath = CONFIG_BCMDHD_CLM_PATH;
20200 
20201 	fp = filp_open(filepath, O_RDONLY, 0);
20202 	if (IS_ERR(fp)) {
20203 		DHD_ERROR(("%s: ----- blob file dosen't exist -----\n", __FUNCTION__));
20204 		dhdp->is_blob = FALSE;
20205 	} else {
20206 		DHD_ERROR(("%s: ----- blob file exist -----\n", __FUNCTION__));
20207 		dhdp->is_blob = TRUE;
20208 #if defined(CONCATE_BLOB)
20209 		strncat(fw_path, "_blob", strlen("_blob"));
20210 #else
20211 		BCM_REFERENCE(fw_path);
20212 #endif /* SKIP_CONCATE_BLOB */
20213 		filp_close(fp, NULL);
20214 	}
20215 }
20216 #endif /* DHD_BLOB_EXISTENCE_CHECK */
20217 
20218 #if defined(PCIE_FULL_DONGLE)
20219 /** test / loopback */
20220 void
dmaxfer_free_dmaaddr_handler(void * handle,void * event_info,u8 event)20221 dmaxfer_free_dmaaddr_handler(void *handle, void *event_info, u8 event)
20222 {
20223 	dmaxref_mem_map_t *dmmap = (dmaxref_mem_map_t *)event_info;
20224 	dhd_info_t *dhd_info = (dhd_info_t *)handle;
20225 	dhd_pub_t *dhdp = &dhd_info->pub;
20226 
20227 	if (event != DHD_WQ_WORK_DMA_LB_MEM_REL) {
20228 		DHD_ERROR(("%s: unexpected event \n", __FUNCTION__));
20229 		return;
20230 	}
20231 
20232 	if ((dhd_info == NULL) || (dhdp == NULL)) {
20233 		DHD_ERROR(("%s: invalid dhd_info\n", __FUNCTION__));
20234 		return;
20235 	}
20236 
20237 	if (dmmap == NULL) {
20238 		DHD_ERROR(("%s: dmmap is null\n", __FUNCTION__));
20239 		return;
20240 	}
20241 	dmaxfer_free_prev_dmaaddr(dhdp, dmmap);
20242 }
20243 
20244 
20245 void
dhd_schedule_dmaxfer_free(dhd_pub_t * dhdp,dmaxref_mem_map_t * dmmap)20246 dhd_schedule_dmaxfer_free(dhd_pub_t *dhdp, dmaxref_mem_map_t *dmmap)
20247 {
20248 	dhd_info_t *dhd_info = dhdp->info;
20249 
20250 	dhd_deferred_schedule_work(dhd_info->dhd_deferred_wq, (void *)dmmap,
20251 		DHD_WQ_WORK_DMA_LB_MEM_REL, dmaxfer_free_dmaaddr_handler, DHD_WQ_WORK_PRIORITY_LOW);
20252 }
20253 #endif /* PCIE_FULL_DONGLE */
20254 /* ---------------------------- End of sysfs implementation ------------------------------------- */
20255 #ifdef HOFFLOAD_MODULES
20256 void
dhd_linux_get_modfw_address(dhd_pub_t * dhd)20257 dhd_linux_get_modfw_address(dhd_pub_t *dhd)
20258 {
20259 	const char* module_name = NULL;
20260 	const struct firmware *module_fw;
20261 	struct module_metadata *hmem = &dhd->hmem;
20262 
20263 	if (dhd_hmem_module_string[0] != '\0') {
20264 		module_name = dhd_hmem_module_string;
20265 	} else {
20266 		DHD_ERROR(("%s No module image name specified\n", __FUNCTION__));
20267 		return;
20268 	}
20269 	if (request_firmware(&module_fw, module_name, dhd_bus_to_dev(dhd->bus))) {
20270 		DHD_ERROR(("modules.img not available\n"));
20271 		return;
20272 	}
20273 	if (!dhd_alloc_module_memory(dhd->bus, module_fw->size, hmem)) {
20274 		release_firmware(module_fw);
20275 		return;
20276 	}
20277 	memcpy(hmem->data, module_fw->data, module_fw->size);
20278 	release_firmware(module_fw);
20279 }
20280 #endif /* HOFFLOAD_MODULES */
20281 
20282 #ifdef SET_PCIE_IRQ_CPU_CORE
20283 void
dhd_set_irq_cpucore(dhd_pub_t * dhdp,int set)20284 dhd_set_irq_cpucore(dhd_pub_t *dhdp, int set)
20285 {
20286 	unsigned int irq;
20287 	if (!dhdp) {
20288 		DHD_ERROR(("%s : dhd is NULL\n", __FUNCTION__));
20289 		return;
20290 	}
20291 
20292 	if (!dhdp->bus) {
20293 		DHD_ERROR(("%s : dhd->bus is NULL\n", __FUNCTION__));
20294 		return;
20295 	}
20296 
20297 	if (dhdpcie_get_pcieirq(dhdp->bus, &irq)) {
20298 		return;
20299 	}
20300 
20301 	set_irq_cpucore(irq, set);
20302 }
20303 #endif /* SET_PCIE_IRQ_CPU_CORE */
20304 
20305 #if defined(DHD_HANG_SEND_UP_TEST)
20306 void
dhd_make_hang_with_reason(struct net_device * dev,const char * string_num)20307 dhd_make_hang_with_reason(struct net_device *dev, const char *string_num)
20308 {
20309 	dhd_info_t *dhd = NULL;
20310 	dhd_pub_t *dhdp = NULL;
20311 	uint reason = HANG_REASON_MAX;
20312 	char buf[WLC_IOCTL_SMLEN] = {0, };
20313 	uint32 fw_test_code = 0;
20314 	dhd = DHD_DEV_INFO(dev);
20315 
20316 	if (dhd) {
20317 		dhdp = &dhd->pub;
20318 	}
20319 
20320 	if (!dhd || !dhdp) {
20321 		return;
20322 	}
20323 
20324 	reason = (uint) bcm_strtoul(string_num, NULL, 0);
20325 	DHD_ERROR(("Enter %s, reason=0x%x\n", __FUNCTION__,  reason));
20326 
20327 	if (reason == 0) {
20328 		if (dhdp->req_hang_type) {
20329 			DHD_ERROR(("%s, Clear HANG test request 0x%x\n",
20330 				__FUNCTION__, dhdp->req_hang_type));
20331 			dhdp->req_hang_type = 0;
20332 			return;
20333 		} else {
20334 			DHD_ERROR(("%s, No requested HANG test\n", __FUNCTION__));
20335 			return;
20336 		}
20337 	} else if ((reason <= HANG_REASON_MASK) || (reason >= HANG_REASON_MAX)) {
20338 		DHD_ERROR(("Invalid HANG request, reason 0x%x\n", reason));
20339 		return;
20340 	}
20341 
20342 	if (dhdp->req_hang_type != 0) {
20343 		DHD_ERROR(("Already HANG requested for test\n"));
20344 		return;
20345 	}
20346 
20347 	switch (reason) {
20348 		case HANG_REASON_IOCTL_RESP_TIMEOUT:
20349 			DHD_ERROR(("Make HANG!!!: IOCTL response timeout(0x%x)\n", reason));
20350 			dhdp->req_hang_type = reason;
20351 			fw_test_code = 102; /* resumed on timeour */
20352 			bcm_mkiovar("bus:disconnect", (void *)&fw_test_code, 4, buf, sizeof(buf));
20353 			dhd_wl_ioctl_cmd(dhdp, WLC_SET_VAR, buf, sizeof(buf), TRUE, 0);
20354 			break;
20355 		case HANG_REASON_DONGLE_TRAP:
20356 			DHD_ERROR(("Make HANG!!!: Dongle trap (0x%x)\n", reason));
20357 			dhdp->req_hang_type = reason;
20358 			fw_test_code = 99; /* dongle trap */
20359 			bcm_mkiovar("bus:disconnect", (void *)&fw_test_code, 4, buf, sizeof(buf));
20360 			dhd_wl_ioctl_cmd(dhdp, WLC_SET_VAR, buf, sizeof(buf), TRUE, 0);
20361 			break;
20362 		case HANG_REASON_D3_ACK_TIMEOUT:
20363 			DHD_ERROR(("Make HANG!!!: D3 ACK timeout (0x%x)\n", reason));
20364 			dhdp->req_hang_type = reason;
20365 			break;
20366 		case HANG_REASON_BUS_DOWN:
20367 			DHD_ERROR(("Make HANG!!!: BUS down(0x%x)\n", reason));
20368 			dhdp->req_hang_type = reason;
20369 			break;
20370 		case HANG_REASON_PCIE_LINK_DOWN:
20371 		case HANG_REASON_MSGBUF_LIVELOCK:
20372 			dhdp->req_hang_type = 0;
20373 			DHD_ERROR(("Does not support requested HANG(0x%x)\n", reason));
20374 			break;
20375 		case HANG_REASON_IFACE_OP_FAILURE:
20376 			DHD_ERROR(("Make HANG!!!: P2P inrerface delete failure(0x%x)\n", reason));
20377 			dhdp->req_hang_type = reason;
20378 			break;
20379 		case HANG_REASON_HT_AVAIL_ERROR:
20380 			dhdp->req_hang_type = 0;
20381 			DHD_ERROR(("PCIe does not support requested HANG(0x%x)\n", reason));
20382 			break;
20383 		case HANG_REASON_PCIE_RC_LINK_UP_FAIL:
20384 			DHD_ERROR(("Make HANG!!!:Link Up(0x%x)\n", reason));
20385 			dhdp->req_hang_type = reason;
20386 			break;
20387 		default:
20388 			dhdp->req_hang_type = 0;
20389 			DHD_ERROR(("Unknown HANG request (0x%x)\n", reason));
20390 			break;
20391 	}
20392 }
20393 #endif /* DHD_HANG_SEND_UP_TEST */
20394 #ifdef DHD_WAKE_STATUS
20395 wake_counts_t*
dhd_get_wakecount(dhd_pub_t * dhdp)20396 dhd_get_wakecount(dhd_pub_t *dhdp)
20397 {
20398 #ifdef BCMDBUS
20399 	return NULL;
20400 #else
20401 	return dhd_bus_get_wakecount(dhdp);
20402 #endif /* BCMDBUS */
20403 }
20404 #endif /* DHD_WAKE_STATUS */
20405 
20406 #ifdef BCM_ASLR_HEAP
20407 uint32
dhd_get_random_number(void)20408 dhd_get_random_number(void)
20409 {
20410 	uint32 rand = 0;
20411 	get_random_bytes_arch(&rand, sizeof(rand));
20412 	return rand;
20413 }
20414 #endif /* BCM_ASLR_HEAP */
20415 
20416 #ifdef DHD_PKT_LOGGING
20417 void
dhd_pktlog_dump(void * handle,void * event_info,u8 event)20418 dhd_pktlog_dump(void *handle, void *event_info, u8 event)
20419 {
20420 	dhd_info_t *dhd = handle;
20421 
20422 	if (!dhd) {
20423 		DHD_ERROR(("%s: dhd is NULL\n", __FUNCTION__));
20424 		return;
20425 	}
20426 
20427 	if (dhd_pktlog_write_file(&dhd->pub)) {
20428 		DHD_ERROR(("%s: writing pktlog dump to the file failed\n", __FUNCTION__));
20429 		return;
20430 	}
20431 }
20432 
20433 void
dhd_schedule_pktlog_dump(dhd_pub_t * dhdp)20434 dhd_schedule_pktlog_dump(dhd_pub_t *dhdp)
20435 {
20436 	dhd_deferred_schedule_work(dhdp->info->dhd_deferred_wq,
20437 			(void*)NULL, DHD_WQ_WORK_PKTLOG_DUMP,
20438 			dhd_pktlog_dump, DHD_WQ_WORK_PRIORITY_HIGH);
20439 }
20440 #endif /* DHD_PKT_LOGGING */
20441 
dhd_get_pub(struct net_device * dev)20442 void *dhd_get_pub(struct net_device *dev)
20443 {
20444 	dhd_info_t *dhdinfo = *(dhd_info_t **)netdev_priv(dev);
20445 	if (dhdinfo)
20446 		return (void *)&dhdinfo->pub;
20447 	else {
20448 		printf("%s: null dhdinfo\n", __FUNCTION__);
20449 		return NULL;
20450 	}
20451 }
20452 
dhd_os_wd_timer_enabled(void * bus)20453 bool dhd_os_wd_timer_enabled(void *bus)
20454 {
20455 	dhd_pub_t *pub = bus;
20456 	dhd_info_t *dhd = (dhd_info_t *)pub->info;
20457 
20458 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
20459 	if (!dhd) {
20460 		DHD_ERROR(("%s: dhd NULL\n", __FUNCTION__));
20461 		return FALSE;
20462 	}
20463 	return dhd->wd_timer_valid;
20464 }
20465