wifi: renew patch drivers/net/wireless
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / combo_mt66xx / mt6628 / wlan / nic / nic.c
1 /*
2 ** $Id: //Department/DaVinci/BRANCHES/MT6620_WIFI_DRIVER_V2_3/nic/nic.c#2 $
3 */
4
5 /*! \file   nic.c
6     \brief  Functions that provide operation in NIC's (Network Interface Card) point of view.
7
8     This file includes functions which unite multiple hal(Hardware) operations
9     and also take the responsibility of Software Resource Management in order
10     to keep the synchronization with Hardware Manipulation.
11 */
12
13
14
15 /*
16 ** $Log: nic.c $
17  *
18  * 07 17 2012 yuche.tsai
19  * NULL
20  * Compile no error before trial run.
21  *
22  * 05 02 2012 terry.wu
23  * NULL
24  * Set the default value of AP StaRec index to "STA_REC_INDEX_NOT_FOUND" in update firmware bss command.
25  *
26  * 03 02 2012 terry.wu
27  * NULL
28  * Sync CFG80211 modification from branch 2,2.
29  *
30  * 11 28 2011 cp.wu
31  * [WCXRP00001125] [MT6620 Wi-Fi][Firmware] Strengthen Wi-Fi power off sequence to have a clearroom environment when returining to ROM code
32  * 1. Due to firmware now stops HIF DMA for powering off, do not try to receive any packet from firmware
33  * 2. Take use of prAdapter->fgIsEnterD3ReqIssued for tracking whether it is powering off or not
34  *
35  * 11 22 2011 tsaiyuan.hsu
36  * [WCXRP00001083] [MT6620 Wi-Fi][DRV]] dump debug counter or frames when debugging is triggered
37  * keep debug counter setting after wake up.
38  *
39  * 11 19 2011 yuche.tsai
40  * NULL
41  * Update RSSI for P2P.
42  *
43  * 11 18 2011 yuche.tsai
44  * NULL
45  * CONFIG P2P support RSSI query, default turned off.
46  *
47  * 11 07 2011 tsaiyuan.hsu
48  * [WCXRP00001083] [MT6620 Wi-Fi][DRV]] dump debug counter or frames when debugging is triggered
49  * add debug counters and periodically dump counters for debugging.
50  *
51  * 11 04 2011 cp.wu
52  * [WCXRP00001079] [MT5931][Driver] Release pending MMPDU only when BSS is being deactivated
53  * pre-check for NULL before calling MMPDU free function
54  *
55  * 11 03 2011 wh.su
56  * [WCXRP00001078] [MT6620 Wi-Fi][Driver] Adding the mediatek log improment support : XLOG
57  * change the DBGLOG for "\n" and "\r\n". LABEL to LOUD for XLOG
58  *
59  * 11 01 2011 chinglan.wang
60  * NULL
61  * Modify the Wi-Fi method of the flush TX queue when disconnect the AP.
62  * If disconnect the AP and flush all the data frame in the TX queue, WPS cannot do the 4-way handshake to connect to the AP..
63  *
64  * 10 11 2011 terry.wu
65  * NULL
66  * Rewrite Assert Dump Function for Portability.
67  *
68  * 09 20 2011 cm.chang
69  * [WCXRP00000997] [MT6620 Wi-Fi][Driver][FW] Handle change of BSS preamble type and slot time
70  * New CMD definition about RLM parameters
71  *
72  * 08 15 2011 cp.wu
73  * [WCXRP00000851] [MT6628 Wi-Fi][Driver] Add HIFSYS related definition to driver source tree
74  * reuse firmware download logic of MT6620 for MT6628.
75  *
76  * 08 03 2011 terry.wu
77  * [WCXRP00000899] [MT6620] [FW] Reply probe rsp in FW for hotspot mode
78  * Reply Probe Rsp in FW for Hotspot Mode.
79  *
80  *
81  *
82  * 08 03 2011 terry.wu
83  * [WCXRP00000899] [MT6620] [FW] Reply probe rsp in FW for hotspot mode
84  * Reply Probe Rsp in FW for Hotspot Mode.
85  *
86  *
87  * 08 03 2011 terry.wu
88  * [WCXRP00000899] [MT6620] [FW] Reply probe rsp in FW for hotspot mode
89  * Reply Probe Rsp in FW for Hotspot Mode.
90  *
91  * 08 03 2011 terry.wu
92  * [WCXRP00000899] [MT6620] [FW] Reply probe rsp in FW for hotspot mode
93  * Reply Probe Rsp in FW for Hotspot Mode.
94  *
95  * 08 02 2011 yuche.tsai
96  * [WCXRP00000896] [Volunteer Patch][WiFi Direct][Driver] GO with multiple client, TX deauth to a disconnecting device issue.
97  * Fix GO send deauth frame issue.
98  *
99  * 07 18 2011 chinghwa.yu
100  * [WCXRP00000063] Update BCM CoEx design and settings[WCXRP00000612] [MT6620 Wi-Fi] [FW] CSD update SWRDD algorithm
101  * Add CMD/Event for RDD and BWCS.
102  *
103  * 07 11 2011 wh.su
104  * [WCXRP00000849] [MT6620 Wi-Fi][Driver] Remove some of the WAPI define for make sure the value is initialize, for customer not enable WAPI
105  * For make sure wapi initial value is set.
106  *
107  * 06 27 2011 cp.wu
108  * [WCXRP00000815] [MT6620 Wi-Fi][Driver] allow single BSSID with multiple SSID settings to work around some tricky AP which use space character as hidden SSID
109  * 1. correct logic
110  * 2. replace only BSS-DESC which doesn't have a valid SSID.
111  *
112  * 06 27 2011 cp.wu
113  * [WCXRP00000815] [MT6620 Wi-Fi][Driver] allow single BSSID with multiple SSID settings to work around some tricky AP which use space character as hidden SSID
114  * allow to have a single BSSID with multiple SSID to be presented in scanning result
115  *
116  * 05 12 2011 puff.wen
117  * NULL
118  * FW Assert information dump to driver
119  *
120  * 04 18 2011 terry.wu
121  * [WCXRP00000660] [MT6620 Wi-Fi][Driver] Remove flag CFG_WIFI_DIRECT_MOVED
122  * Remove flag CFG_WIFI_DIRECT_MOVED.
123  *
124  * 04 15 2011 cp.wu
125  * [WCXRP00000651] [MT6620 Wi-Fi][Driver] Refine RSSI buffering mechanism
126  * ROLLBACK due to the special design is to workaround incorrect initial RCPI value coming from firmware domain.
127  *
128  * 04 14 2011 cm.chang
129  * [WCXRP00000634] [MT6620 Wi-Fi][Driver][FW] 2nd BSS will not support 40MHz bandwidth for concurrency
130  * .
131  *
132  * 04 14 2011 cp.wu
133  * [WCXRP00000598] [MT6620 Wi-Fi][Driver] Implementation of interface for communicating with user space process for RESET_START and RESET_END events
134  * 1. add code to put whole-chip resetting trigger when abnormal firmware assertion is detected
135  * 2. add dummy function for both Win32 and Linux part.
136  *
137  * 04 12 2011 cp.wu
138  * [WCXRP00000635] [MT6620 Wi-Fi][Driver] Clear pending security frames when QM clear pending data frames for dedicated network type
139  * clear pending security frames for dedicated network type when BSS is being deactivated/disconnected
140  *
141  * 04 12 2011 wh.su
142  * NULL
143  * enable the p2p check the cipher to set the bssInfo auth mode.
144  *
145  * 04 12 2011 wh.su
146  * NULL
147  * prepare the code for sync the auth mode and encryption status for P2P and BOW.
148  *
149  * 04 11 2011 yuche.tsai
150  * [WCXRP00000627] [Volunteer Patch][MT6620][Driver] Pending MMPUD of P2P Network may crash system issue.
151  * Fix kernel panic issue when MMPDU of P2P is pending in driver.
152  *
153  * 04 10 2011 george.huang
154  * [WCXRP00000621] [MT6620 Wi-Fi][Driver] Support P2P supplicant to set power mode
155  * Fix compiler issue.
156  *
157  * 04 08 2011 george.huang
158  * [WCXRP00000621] [MT6620 Wi-Fi][Driver] Support P2P supplicant to set power mode
159  * separate settings of P2P and AIS
160  *
161  * 04 08 2011 eddie.chen
162  * [WCXRP00000617] [MT6620 Wi-Fi][DRV/FW] Fix for sigma
163  * Fix for sigma
164  *
165  * 04 07 2011 cp.wu
166  * [WCXRP00000616] [MT6620 Wi-Fi][Driver] Free memory to pool and kernel in case any unexpected failure happend inside wlanAdapterStart
167  * .
168  *
169  * 04 07 2011 cp.wu
170  * [WCXRP00000616] [MT6620 Wi-Fi][Driver] Free memory to pool and kernel in case any unexpected failure happend inside wlanAdapterStart
171  * implementation of internal error handling of nicAllocateAdapterMemory.
172  *
173  * 03 31 2011 chinglan.wang
174  * [WCXRP00000613] [MT6620 Wi-Fi] [FW] [Driver] BssInfo can get the security mode which is WPA/WPA2/WAPI or not.
175  * .
176  *
177  * 03 21 2011 cp.wu
178  * [WCXRP00000540] [MT5931][Driver] Add eHPI8/eHPI16 support to Linux Glue Layer
179  * portability improvement
180  *
181  * 03 17 2011 cp.wu
182  * [WCXRP00000562] [MT6620 Wi-Fi][Driver] I/O buffer pre-allocation to avoid physically continuous memory shortage after system running for a long period
183  * use pre-allocated buffer for storing enhanced interrupt response as well
184  *
185  * 03 16 2011 cp.wu
186  * [WCXRP00000562] [MT6620 Wi-Fi][Driver] I/O buffer pre-allocation to avoid physically continuous memory shortage after system running for a long period
187  * 1. pre-allocate physical continuous buffer while module is being loaded
188  * 2. use pre-allocated physical continuous buffer for TX/RX DMA transfer
189  *
190  * The windows part remained the same as before, but added similiar APIs to hide the difference.
191  *
192  * 03 15 2011 cp.wu
193  * [WCXRP00000559] [MT6620 Wi-Fi][Driver] Combine TX/RX DMA buffers into a single one to reduce physically continuous memory consumption
194  * 1. deprecate CFG_HANDLE_IST_IN_SDIO_CALLBACK
195  * 2. Use common coalescing buffer for both TX/RX directions
196  *
197  *
198  * 03 10 2011 cm.chang
199  * [WCXRP00000358] [MT6620 Wi-Fi][Driver] Provide concurrent information for each module
200  * Add some functions to let AIS/Tethering or AIS/BOW be the same channel
201  *
202  * 03 07 2011 terry.wu
203  * [WCXRP00000521] [MT6620 Wi-Fi][Driver] Remove non-standard debug message
204  * Toggle non-standard debug messages to comments.
205  *
206  * 03 02 2011 cp.wu
207  * [WCXRP00000503] [MT6620 Wi-Fi][Driver] Take RCPI brought by association response as initial RSSI right after connection is built.
208  * use RCPI brought by ASSOC-RESP after connection is built as initial RCPI to avoid using a uninitialized MAC-RX RCPI.
209  *
210  * 02 08 2011 terry.wu
211  * [WCXRP00000412] [MT6620 Wi-Fi][FW/Driver] Dump firmware assert info at android kernel log
212  * Use kalPrint to print firmware assert info.
213  *
214  * 02 01 2011 terry.wu
215  * [WCXRP00000412] [MT6620 Wi-Fi][FW/Driver] Dump firmware assert info at android kernel log
216  * .
217  *
218  * 02 01 2011 cm.chang
219  * [WCXRP00000415] [MT6620 Wi-Fi][Driver] Check if any memory leakage happens when uninitializing in DGB mode
220  * .
221  *
222  * 01 31 2011 terry.wu
223  * [WCXRP00000412] [MT6620 Wi-Fi][FW/Driver] Dump firmware assert info at android kernel log
224  * Print firmware ASSERT info at Android kernel log, driver side
225  *
226  * 01 27 2011 tsaiyuan.hsu
227  * [WCXRP00000392] [MT6620 Wi-Fi][Driver] Add Roaming Support
228  * add roaming fsm
229  * 1. not support 11r, only use strength of signal to determine roaming.
230  * 2. not enable CFG_SUPPORT_ROAMING until completion of full test.
231  * 3. in 6620, adopt work-around to avoid sign extension problem of cck of hw
232  * 4. assume that change of link quality in smooth way.
233  *
234  * 01 26 2011 cm.chang
235  * [WCXRP00000395] [MT6620 Wi-Fi][Driver][FW] Search STA_REC with additional net type index argument
236  * Allocate system RAM if fixed message or mgmt buffer is not available
237  *
238  * 01 24 2011 cp.wu
239  * [WCXRP00000382] [MT6620 Wi-Fi][Driver] Track forwarding packet number with notifying tx thread for serving
240  * 1. add an extra counter for tracking pending forward frames.
241  * 2. notify TX service thread as well when there is pending forward frame
242  * 3. correct build errors leaded by introduction of Wi-Fi direct separation module
243  *
244  * 01 19 2011 cp.wu
245  * [WCXRP00000372] [MT6620 Wi-Fi][Driver] Check bus access failure inside nicProcessIST()
246  * check bus error and/or card removal when retrieving interrupt status from HAL
247  *
248  * 01 04 2011 cp.wu
249  * [WCXRP00000338] [MT6620 Wi-Fi][Driver] Separate kalMemAlloc into kmalloc and vmalloc implementations to ease physically continous memory demands
250  * 1) correct typo in scan.c
251  * 2) TX descriptors, RX descriptos and management buffer should use virtually continous buffer instead of physically contineous one
252  *
253  * 01 04 2011 cp.wu
254  * [WCXRP00000338] [MT6620 Wi-Fi][Driver] Separate kalMemAlloc into kmalloc and vmalloc implementations to ease physically continous memory demands
255  * separate kalMemAlloc() into virtually-continous and physically-continous type to ease slab system pressure
256  *
257  * 12 30 2010 cp.wu
258  * [WCXRP00000327] [MT6620 Wi-Fi][Driver] Improve HEC WHQA 6972 workaround coverage in driver side
259  * host driver not to set FW-own when there is still pending interrupts
260  *
261  * 12 17 2010 cp.wu
262  * [WCXRP00000270] [MT6620 Wi-Fi][Driver] Clear issues after concurrent networking support has been merged
263  * before BSS disconnection is indicated to firmware, all correlated peer should be cleared and freed
264  *
265  * 12 07 2010 cm.chang
266  * [WCXRP00000239] MT6620 Wi-Fi][Driver][FW] Merge concurrent branch back to maintrunk
267  * 1. BSSINFO include RLM parameter
268  * 2. free all sta records when network is disconnected
269  *
270  * 12 02 2010 eddie.chen
271  * [WCXRP00000218] [MT6620 Wi-Fi][Driver] Add auto rate window control in registry
272  * Add more control value but dont use it now.
273  *
274  * 11 30 2010 eddie.chen
275  * [WCXRP00000218] [MT6620 Wi-Fi][Driver] Add auto rate window control in registry
276  * Add auto rate check window in registry
277  *
278  * 11 10 2010 eddie.chen
279  * [WCXRP00000156] [MT6620][FW] Change Auto rate window to 64 and add throughput swcr
280  * Use autorate parameter 1 as phy rate mask.
281  *
282  * 11 08 2010 cp.wu
283  * [WCXRP00000166] [MT6620 Wi-Fi][Driver] use SDIO CMD52 for enabling/disabling interrupt to reduce transaction period
284  * change to use CMD52 for enabling/disabling interrupt to reduce SDIO transaction time
285  *
286  * 10 26 2010 eddie.chen
287  * [WCXRP00000134] [MT6620 Wi-Fi][Driver] Add a registry to enable auto rate for SQA test by using E1 EVB
288  * Add auto rate parameter in registry.
289  *
290  * 10 18 2010 cp.wu
291  * [WCXRP00000053] [MT6620 Wi-Fi][Driver] Reset incomplete and might leads to BSOD when entering RF test with AIS associated
292  * 1. remove redundant variables in STA_REC structure
293  * 2. add STA-REC uninitialization routine for clearing pending events
294  *
295  * 10 18 2010 cp.wu
296  * [WCXRP00000103] [MT6620 Wi-Fi][Driver] Driver crashed when using WZC to connect to AP#B with connection with AP#A
297  * reset ptrs when IEs are going to be dropped
298  *
299  * 10 12 2010 cp.wu
300  * [WCXRP00000084] [MT6620 Wi-Fi][Driver][FW] Add fixed rate support for distance test
301  * add HT (802.11n) fixed rate support.
302  *
303  * 10 08 2010 cp.wu
304  * [WCXRP00000084] [MT6620 Wi-Fi][Driver][FW] Add fixed rate support for distance test
305  * adding fixed rate support for distance test. (from registry setting)
306  *
307  * 10 07 2010 cp.wu
308  * [WCXRP00000083] [MT5931][Driver][FW] Add necessary logic for MT5931 first connection
309  * add firmware download for MT5931.
310  *
311  * 10 05 2010 cp.wu
312  * [WCXRP00000056] [MT6620 Wi-Fi][Driver] NVRAM implementation with Version Check
313  * load manufacture data when CFG_SUPPORT_NVRAM is set to 1
314  *
315  * 10 04 2010 cp.wu
316  * [WCXRP00000077] [MT6620 Wi-Fi][Driver][FW] Eliminate use of ENUM_NETWORK_TYPE_T and replaced by ENUM_NETWORK_TYPE_INDEX_T only
317  * remove ENUM_NETWORK_TYPE_T definitions
318  *
319  * 09 21 2010 cp.wu
320  * [WCXRP00000053] [MT6620 Wi-Fi][Driver] Reset incomplete and might leads to BSOD when entering RF test with AIS associated
321  * Do a complete reset with STA-REC null checking for RF test re-entry
322  *
323  * 09 08 2010 cp.wu
324  * NULL
325  * use static memory pool for storing IEs of scanning result.
326  *
327  * 09 06 2010 cp.wu
328  * NULL
329  * Androi/Linux: return current operating channel information
330  *
331  * 09 01 2010 cp.wu
332  * NULL
333  * HIFSYS Clock Source Workaround
334  *
335  * 08 26 2010 yuche.tsai
336  * NULL
337  * Fix someones coding error while enable WIFI_DIRECT.
338  *
339  * 08 25 2010 george.huang
340  * NULL
341  * update OID/ registry control path for PM related settings
342  *
343  * 08 24 2010 cm.chang
344  * NULL
345  * Support RLM initail channel of Ad-hoc, P2P and BOW
346  *
347  * 08 24 2010 chinghwa.yu
348  * NULL
349  * Update BOW for the 1st time.
350  *
351  * 08 23 2010 chinghwa.yu
352  * NULL
353  * Update for BOW.
354  *
355  * 08 20 2010 yuche.tsai
356  * NULL
357  * Add state change indication.
358  *
359  * 08 16 2010 yuche.tsai
360  * NULL
361  * Add support for P2P BSS update info.
362  *
363  * 08 12 2010 cp.wu
364  * NULL
365  * [removing debugging] not to dump beacon content.
366  *
367  * 08 12 2010 cp.wu
368  * NULL
369  * [AIS-FSM] honor registry setting for adhoc running mode. (A/B/G)
370  *
371  * 08 11 2010 cp.wu
372  * NULL
373  * 1) do not use in-stack variable for beacon updating. (for MAUI porting)
374  * 2) extending scanning result to 64 instead of 48
375  *
376  * 08 04 2010 yarco.yang
377  * NULL
378  * Add TX_AMPDU and ADDBA_REJECT command
379  *
380  * 08 03 2010 cp.wu
381  * NULL
382  * surpress compilation warning.
383  *
384  * 08 03 2010 cp.wu
385  * NULL
386  * Centralize mgmt/system service procedures into independent calls.
387  *
388  * 07 28 2010 cp.wu
389  * NULL
390  * 1) eliminate redundant variable eOPMode in prAdapter->rWlanInfo
391  * 2) change nicMediaStateChange() API prototype
392  *
393  * 07 28 2010 cp.wu
394  * NULL
395  * sync. CMD_BSS_INFO structure change to CMD-EVENT v0.15.
396  *
397  * 07 24 2010 wh.su
398  *
399  * .support the Wi-Fi RSN
400  *
401  * 07 23 2010 cp.wu
402  *
403  * FIX: structure of CMD_SET_BSS_INFO has been changed but no follow-ups are done.
404  *
405  * 07 22 2010 george.huang
406  *
407  * .
408  *
409  * 07 22 2010 george.huang
410  *
411  * Update fgIsQoS information in BSS INFO by CMD
412  *
413  * 07 19 2010 cp.wu
414  *
415  * [WPD00003833] [MT6620 and MT5931] Driver migration.
416  * Add Ad-Hoc support to AIS-FSM
417  *
418  * 07 14 2010 yarco.yang
419  *
420  * 1. Remove CFG_MQM_MIGRATION
421  * 2. Add CMD_UPDATE_WMM_PARMS command
422  *
423  * 07 08 2010 cp.wu
424  *
425  * [WPD00003833] [MT6620 and MT5931] Driver migration - move to new repository.
426  *
427  * 07 07 2010 cp.wu
428  * [WPD00003833][MT6620 and MT5931] Driver migration
429  * update prStaRecOfAP with BSS-INFO.
430  *
431  * 07 06 2010 george.huang
432  * [WPD00001556]Basic power managemenet function
433  * Update arguments for nicUpdateBeaconIETemplate()
434  *
435  * 07 06 2010 cp.wu
436  * [WPD00003833][MT6620 and MT5931] Driver migration
437  * STA-REC is maintained by CNM only.
438  *
439  * 07 05 2010 cp.wu
440  * [WPD00003833][MT6620 and MT5931] Driver migration
441  * 1) ignore RSN checking when RSN is not turned on.
442  * 2) set STA-REC deactivation callback as NULL
443  * 3) add variable initialization API based on PHY configuration
444  *
445  * 07 01 2010 cm.chang
446  * [WPD00003841][LITE Driver] Migrate RLM/CNM to host driver
447  * Support sync command of STA_REC
448  *
449  * 06 30 2010 cp.wu
450  * [WPD00003833][MT6620 and MT5931] Driver migration
451  * sync. with CMD/EVENT document ver0.07.
452  *
453  * 06 29 2010 cp.wu
454  * [WPD00003833][MT6620 and MT5931] Driver migration
455  * correct variable naming for 8-bit variable used in CMD_BEACON_TEMPLATE_UPDATE.
456  *
457  * 06 29 2010 cp.wu
458  * [WPD00003833][MT6620 and MT5931] Driver migration
459  * 1) sync to. CMD/EVENT document v0.03
460  * 2) simplify DTIM period parsing in scan.c only, bss.c no longer parses it again.
461  * 3) send command packet to indicate FW-PM after
462  *     a) 1st beacon is received after AIS has connected to an AP
463  *     b) IBSS-ALONE has been created
464  *     c) IBSS-MERGE has occured
465  *
466  * 06 25 2010 george.huang
467  * [WPD00001556]Basic power managemenet function
468  * Create beacon update path, with expose bssUpdateBeaconContent()
469  *
470  * 06 22 2010 cp.wu
471  * [WPD00003833][MT6620 and MT5931] Driver migration
472  * fill fgIsUapsdConnection when indicating BSS-CREATE with AIS-STA mode.
473  *
474  * 06 22 2010 cp.wu
475  * [WPD00003833][MT6620 and MT5931] Driver migration
476  * 1) add command warpper for STA-REC/BSS-INFO sync.
477  * 2) enhance command packet sending procedure for non-oid part
478  * 3) add command packet definitions for STA-REC/BSS-INFO sync.
479  *
480  * 06 21 2010 cp.wu
481  * [WPD00003833][MT6620 and MT5931] Driver migration
482  * implement TX_DONE callback path.
483  *
484  * 06 21 2010 wh.su
485  * [WPD00003840][MT6620 5931] Security migration
486  * remove duplicate variable for migration.
487  *
488  * 06 21 2010 cp.wu
489  * [WPD00003833][MT6620 and MT5931] Driver migration
490  * TX descriptors are now allocated once for reducing allocation overhead
491  *
492  * 06 18 2010 cm.chang
493  * [WPD00003841][LITE Driver] Migrate RLM/CNM to host driver
494  * Provide cnmMgtPktAlloc() and alloc/free function of msg/buf
495  *
496  * 06 11 2010 cp.wu
497  * [WPD00003833][MT6620 and MT5931] Driver migration
498  * 1) migrate assoc.c.
499  * 2) add ucTxSeqNum for tracking frames which needs TX-DONE awareness
500  * 3) add configuration options for CNM_MEM and RSN modules
501  * 4) add data path for management frames
502  * 5) eliminate rPacketInfo of MSDU_INFO_T
503  *
504  * 06 10 2010 cp.wu
505  * [WPD00003833][MT6620 and MT5931] Driver migration
506  * 1) eliminate CFG_CMD_EVENT_VERSION_0_9
507  * 2) when disconnected, indicate nic directly (no event is needed)
508  *
509  * 06 06 2010 kevin.huang
510  * [WPD00003832][MT6620 5931] Create driver base
511  * [MT6620 5931] Create driver base
512  *
513  * 04 26 2010 cp.wu
514  * [WPD00001943]Create WiFi test driver framework on WinXP
515  * 1) surpress compiler warning
516  * 2) when acqruing LP-own, keep writing WHLPCR whenever OWN is not acquired yet
517  *
518  * 04 23 2010 cp.wu
519  * [WPD00001943]Create WiFi test driver framework on WinXP
520  * surpress compiler warning
521  *
522  * 04 13 2010 cp.wu
523  * [WPD00003823][MT6620 Wi-Fi] Add Bluetooth-over-Wi-Fi support
524  * add framework for BT-over-Wi-Fi support.
525  *  *  *  *  *  *  *  *  *  *  *  *  * 1) prPendingCmdInfo is replaced by queue for multiple handler capability
526  *  *  *  *  *  *  *  *  *  *  *  *  * 2) command sequence number is now increased atomically
527  *  *  *  *  *  *  *  *  *  *  *  *  * 3) private data could be hold and taken use for other purpose
528  *
529  * 04 12 2010 cp.wu
530  * [WPD00001943]Create WiFi test driver framework on WinXP
531  * add channel frequency <-> number conversion
532  *
533  * 04 06 2010 cp.wu
534  * [WPD00001943]Create WiFi test driver framework on WinXP
535  * eliminate direct access for prGlueInfo->fgIsCardRemoved in non-glue layer
536  *
537  * 04 06 2010 cp.wu
538  * [WPD00003827][MT6620 Wi-Fi] Chariot fail and following ping fail, no pkt send from driver
539  * correct nicProcessIST_impl() for interrupt status brought up by RX enhanced response
540  *
541  * 03 19 2010 cp.wu
542  * [WPD00001943]Create WiFi test driver framework on WinXP
543  * 1) add ACPI D0/D3 state switching support
544  *  *  *  *  *  *  * 2) use more formal way to handle interrupt when the status is retrieved from enhanced RX response
545  *
546  * 03 17 2010 cp.wu
547  * [WPD00001943]Create WiFi test driver framework on WinXP
548  * always process TX interrupt first then RX interrupt.
549  *
550  * 02 25 2010 cp.wu
551  * [WPD00001943]Create WiFi test driver framework on WinXP
552  * correct behavior to prevent duplicated RX handling for RX0_DONE and RX1_DONE
553  *
554  * 02 23 2010 cp.wu
555  * [WPD00001943]Create WiFi test driver framework on WinXP
556  * add checksum offloading support.
557 **  \main\maintrunk.MT6620WiFiDriver_Prj\27 2009-12-16 18:03:43 GMT mtk02752
558 **  handling enhanced response which fields are fetched at different moments
559 **  \main\maintrunk.MT6620WiFiDriver_Prj\26 2009-12-15 17:00:29 GMT mtk02752
560 **  if RX enhanced response is used, D2H interrupt status should be coming from buffered result as well
561 **  \main\maintrunk.MT6620WiFiDriver_Prj\25 2009-12-15 12:01:55 GMT mtk02752
562 **  if TX_DONE bit is not set but WTSR0/WTSR1 is non-zero, then set TX_DONE bit due to time latency of interrupt status enhanced response
563 **  \main\maintrunk.MT6620WiFiDriver_Prj\24 2009-12-10 16:52:52 GMT mtk02752
564 **  code clean
565 **  \main\maintrunk.MT6620WiFiDriver_Prj\23 2009-11-24 20:51:01 GMT mtk02752
566 **  integrate with SD1 by invoking qmHandleMailboxRxMessage()
567 **  \main\maintrunk.MT6620WiFiDriver_Prj\22 2009-11-16 17:32:33 GMT mtk02752
568 **  prepare code for invoking rxHandleMailboxRxMessage()
569 **  \main\maintrunk.MT6620WiFiDriver_Prj\21 2009-11-11 10:36:08 GMT mtk01084
570 **  \main\maintrunk.MT6620WiFiDriver_Prj\20 2009-11-09 22:56:41 GMT mtk01084
571 **  modify HW access routines
572 **  \main\maintrunk.MT6620WiFiDriver_Prj\19 2009-10-30 18:17:20 GMT mtk01084
573 **  prevent warning
574 **  \main\maintrunk.MT6620WiFiDriver_Prj\18 2009-10-29 19:54:57 GMT mtk01084
575 **  init HIF
576 **  \main\maintrunk.MT6620WiFiDriver_Prj\17 2009-10-23 16:08:30 GMT mtk01084
577 **  \main\maintrunk.MT6620WiFiDriver_Prj\16 2009-10-13 21:59:12 GMT mtk01084
578 **  update for new HW design
579 **  \main\maintrunk.MT6620WiFiDriver_Prj\15 2009-09-09 17:26:15 GMT mtk01084
580 **  modify for CFG_TEST_WITH_MT5921
581 **  \main\maintrunk.MT6620WiFiDriver_Prj\14 2009-05-19 10:55:22 GMT mtk01461
582 **  Unmask the unused HISR
583 **  \main\maintrunk.MT6620WiFiDriver_Prj\13 2009-05-18 15:59:13 GMT mtk01084
584 **  remove debug purpose code
585 **  \main\maintrunk.MT6620WiFiDriver_Prj\12 2009-05-18 14:05:02 GMT mtk01084
586 **  update for WIFI ownback part on initial
587 **  \main\maintrunk.MT6620WiFiDriver_Prj\11 2009-05-04 21:32:57 GMT mtk01084
588 **  add temporarily code to set driver own on adapter initialization
589 **  \main\maintrunk.MT6620WiFiDriver_Prj\10 2009-04-28 10:35:41 GMT mtk01461
590 **  Add init of TX aggregation and fix RX STATUS is DW align for SDIO_STATUS_ENHANCE mode
591 **  \main\maintrunk.MT6620WiFiDriver_Prj\9 2009-04-24 21:12:10 GMT mtk01104
592 **  Add function nicRestoreSpiDefMode()
593 **  \main\maintrunk.MT6620WiFiDriver_Prj\8 2009-04-21 09:43:31 GMT mtk01461
594 **  Revise for MTK coding style - nicInitializeAdapter()
595 **  \main\maintrunk.MT6620WiFiDriver_Prj\7 2009-04-17 19:52:47 GMT mtk01461
596 **  Update allocate Adapter Memory for MGMT Memory pool
597 **  \main\maintrunk.MT6620WiFiDriver_Prj\6 2009-04-01 10:57:08 GMT mtk01461
598 **  Refine the order of release memory from pucRxCoalescingBufCached
599 **  \main\maintrunk.MT6620WiFiDriver_Prj\5 2009-03-19 18:32:57 GMT mtk01084
600 **  update for basic power management functions
601 **  \main\maintrunk.MT6620WiFiDriver_Prj\4 2009-03-18 21:00:14 GMT mtk01426
602 **  Add CFG_SDIO_RX_ENHANCE support
603 **  \main\maintrunk.MT6620WiFiDriver_Prj\3 2009-03-16 09:10:27 GMT mtk01461
604 **  Update TX PATH API
605 **  \main\maintrunk.MT6620WiFiDriver_Prj\2 2009-03-10 20:25:59 GMT mtk01426
606 **  Init for develop
607 **
608 */
609
610 /*******************************************************************************
611 *                         C O M P I L E R   F L A G S
612 ********************************************************************************
613 */
614
615 /*******************************************************************************
616 *                    E X T E R N A L   R E F E R E N C E S
617 ********************************************************************************
618 */
619 #include "precomp.h"
620
621
622 /*******************************************************************************
623 *                              C O N S T A N T S
624 ********************************************************************************
625 */
626 const UINT_8 aucPhyCfg2PhyTypeSet[PHY_CONFIG_NUM] =
627 {
628     PHY_TYPE_SET_802_11ABG,         /* PHY_CONFIG_802_11ABG */
629     PHY_TYPE_SET_802_11BG,          /* PHY_CONFIG_802_11BG */
630     PHY_TYPE_SET_802_11G,           /* PHY_CONFIG_802_11G */
631     PHY_TYPE_SET_802_11A,           /* PHY_CONFIG_802_11A */
632     PHY_TYPE_SET_802_11B,           /* PHY_CONFIG_802_11B */
633     PHY_TYPE_SET_802_11ABGN,        /* PHY_CONFIG_802_11ABGN */
634     PHY_TYPE_SET_802_11BGN,         /* PHY_CONFIG_802_11BGN */
635     PHY_TYPE_SET_802_11AN,          /* PHY_CONFIG_802_11AN */
636     PHY_TYPE_SET_802_11GN           /* PHY_CONFIG_802_11GN */
637 };
638
639 #if (MT6620_E1_ASIC_HIFSYS_WORKAROUND == 1)
640     #define REQ_GATING_ENABLE_H2D_INT   BIT(31)
641     #define REQ_GATING_DISABLE_H2D_INT  BIT(30)
642     #define ACK_GATING_ENABLE_D2H_INT   BIT(31)
643     #define ACK_GATING_DISABLE_D2H_INT  BIT(30)
644
645     #define GATING_CONTROL_POLL_LIMIT   64
646 #endif
647
648 /*******************************************************************************
649 *                             D A T A   T Y P E S
650 ********************************************************************************
651 */
652
653 /*******************************************************************************
654 *                            P U B L I C   D A T A
655 ********************************************************************************
656 */
657
658 static INT_EVENT_MAP_T arIntEventMapTable[] = {
659     {WHISR_ABNORMAL_INT,                        INT_EVENT_ABNORMAL},
660     {WHISR_D2H_SW_INT,                          INT_EVENT_SW_INT},
661     {WHISR_TX_DONE_INT,                         INT_EVENT_TX},
662     {(WHISR_RX0_DONE_INT | WHISR_RX1_DONE_INT), INT_EVENT_RX}
663 };
664
665 static const UINT_8 ucIntEventMapSize = (sizeof(arIntEventMapTable) / sizeof(INT_EVENT_MAP_T));
666
667 static IST_EVENT_FUNCTION apfnEventFuncTable[] = {
668     nicProcessAbnormalInterrupt,            /*!< INT_EVENT_ABNORMAL */
669     nicProcessSoftwareInterrupt,            /*!< INT_EVENT_SW_INT   */
670     nicProcessTxInterrupt,                  /*!< INT_EVENT_TX       */
671     nicProcessRxInterrupt,                  /*!< INT_EVENT_RX       */
672 };
673 /*******************************************************************************
674 *                           P R I V A T E   D A T A
675 ********************************************************************************
676 */
677
678 /*******************************************************************************
679 *                                 M A C R O S
680 ********************************************************************************
681 */
682 /*! This macro is used to reduce coding errors inside nicAllocateAdapterMemory()
683  * and also enhance the readability.
684  */
685 #define LOCAL_NIC_ALLOCATE_MEMORY(pucMem, u4Size, eMemType, pucComment) \
686         { \
687             DBGLOG(INIT, INFO, ("Allocating %ld bytes for %s.\n", u4Size, pucComment)); \
688             if ((pucMem = (PUINT_8)kalMemAlloc(u4Size, eMemType)) == (PUINT_8)NULL) { \
689                 DBGLOG(INIT, ERROR, ("Could not allocate %ld bytes for %s.\n", u4Size, pucComment)); \
690                 break; \
691             } \
692             ASSERT(((UINT_32)pucMem % 4) == 0); \
693             DBGLOG(INIT, INFO, ("Virtual Address = %08lx for %s.\n", (UINT_32)pucMem, pucComment)); \
694         }
695
696
697
698 /*******************************************************************************
699 *                   F U N C T I O N   D E C L A R A T I O N S
700 ********************************************************************************
701 */
702
703 /*******************************************************************************
704 *                              F U N C T I O N S
705 ********************************************************************************
706 */
707
708
709 /*----------------------------------------------------------------------------*/
710 /*!
711 * @brief This routine is responsible for the allocation of the data structures
712 *        inside the Adapter structure, include:
713 *        1. SW_RFB_Ts
714 *        2. Common coalescing buffer for TX PATH.
715 *
716 * @param prAdapter Pointer of Adapter Data Structure
717 *
718 * @retval WLAN_STATUS_SUCCESS - Has enough memory.
719 * @retval WLAN_STATUS_RESOURCES - Memory is not enough.
720 */
721 /*----------------------------------------------------------------------------*/
722 WLAN_STATUS
723 nicAllocateAdapterMemory (
724     IN P_ADAPTER_T prAdapter
725     )
726 {
727     WLAN_STATUS status = WLAN_STATUS_RESOURCES;
728     P_RX_CTRL_T prRxCtrl;
729     P_TX_CTRL_T prTxCtrl;
730
731     DEBUGFUNC("nicAllocateAdapterMemory");
732
733     ASSERT(prAdapter);
734     prRxCtrl = &prAdapter->rRxCtrl;
735     prTxCtrl = &prAdapter->rTxCtrl;
736
737     do {
738         //4 <0> Reset all Memory Handler
739     #if CFG_DBG_MGT_BUF
740         prAdapter->u4MemFreeDynamicCount = 0;
741         prAdapter->u4MemAllocDynamicCount = 0;
742     #endif
743         prAdapter->pucMgtBufCached = (PUINT_8)NULL;
744         prRxCtrl->pucRxCached = (PUINT_8)NULL;
745         prAdapter->prSDIOCtrl = (P_SDIO_CTRL_T)NULL;
746
747
748         //4 <1> Memory for Management Memory Pool and CMD_INFO_T
749         /* Allocate memory for the CMD_INFO_T and its MGMT memory pool. */
750         prAdapter->u4MgtBufCachedSize = MGT_BUFFER_SIZE;
751
752         LOCAL_NIC_ALLOCATE_MEMORY(prAdapter->pucMgtBufCached,
753                                   prAdapter->u4MgtBufCachedSize,
754                                   VIR_MEM_TYPE,
755                                   "COMMON MGMT MEMORY POOL");
756
757         //4 <2> Memory for RX Descriptor
758         /* Initialize the number of rx buffers we will have in our queue. */
759         /* <TODO> We may setup ucRxPacketDescriptors by GLUE Layer, and using
760          * this variable directly.
761          */
762         /* Allocate memory for the SW receive structures. */
763         prRxCtrl->u4RxCachedSize = CFG_RX_MAX_PKT_NUM * \
764                                    ALIGN_4(sizeof(SW_RFB_T));
765
766         LOCAL_NIC_ALLOCATE_MEMORY(prRxCtrl->pucRxCached,
767                                   prRxCtrl->u4RxCachedSize,
768                                   VIR_MEM_TYPE,
769                                   "SW_RFB_T");
770
771         //4 <3> Memory for TX DEscriptor
772         prTxCtrl->u4TxCachedSize = CFG_TX_MAX_PKT_NUM * \
773                                    ALIGN_4(sizeof(MSDU_INFO_T));
774
775         LOCAL_NIC_ALLOCATE_MEMORY(prTxCtrl->pucTxCached,
776                                   prTxCtrl->u4TxCachedSize,
777                                   VIR_MEM_TYPE,
778                                   "MSDU_INFO_T");
779
780         //4 <4> Memory for Common Coalescing Buffer
781 #if CFG_COALESCING_BUFFER_SIZE || CFG_SDIO_RX_AGG
782         prAdapter->pucCoalescingBufCached = (PUINT_8)NULL;
783
784         /* Allocate memory for the common coalescing buffer. */
785         prAdapter->u4CoalescingBufCachedSize = CFG_COALESCING_BUFFER_SIZE > CFG_RX_COALESCING_BUFFER_SIZE ?
786             CFG_COALESCING_BUFFER_SIZE : CFG_RX_COALESCING_BUFFER_SIZE;
787
788         prAdapter->pucCoalescingBufCached =
789             kalAllocateIOBuffer(prAdapter->u4CoalescingBufCachedSize);
790
791         if(prAdapter->pucCoalescingBufCached == NULL) {
792             DBGLOG(INIT, ERROR, ("Could not allocate %ld bytes for coalescing buffer.\n", prAdapter->u4CoalescingBufCachedSize));
793             break;
794         }
795 #endif /* CFG_COALESCING_BUFFER_SIZE */
796
797         //4 <5> Memory for enhanced interrupt response
798         prAdapter->prSDIOCtrl = (P_SDIO_CTRL_T)
799             kalAllocateIOBuffer(sizeof(ENHANCE_MODE_DATA_STRUCT_T));
800
801         if(prAdapter->prSDIOCtrl == NULL) {
802             DBGLOG(INIT, ERROR, ("Could not allocate %ld bytes for interrupt response.\n", sizeof(ENHANCE_MODE_DATA_STRUCT_T)));
803             break;
804         }
805
806         status = WLAN_STATUS_SUCCESS;
807
808     }
809     while(FALSE);
810
811     if(status != WLAN_STATUS_SUCCESS) {
812         nicReleaseAdapterMemory(prAdapter);
813     }
814
815     return status;
816
817 }   /* end of nicAllocateAdapterMemory() */
818
819
820 /*----------------------------------------------------------------------------*/
821 /*!
822 * @brief This routine is responsible for releasing the allocated memory by
823 *        nicAllocatedAdapterMemory().
824 *
825 * @param prAdapter Pointer of Adapter Data Structure
826 *
827 * @return (none)
828 */
829 /*----------------------------------------------------------------------------*/
830 VOID
831 nicReleaseAdapterMemory (
832     IN P_ADAPTER_T prAdapter
833     )
834 {
835     P_TX_CTRL_T prTxCtrl;
836     P_RX_CTRL_T prRxCtrl;
837
838     ASSERT(prAdapter);
839     prTxCtrl = &prAdapter->rTxCtrl;
840     prRxCtrl = &prAdapter->rRxCtrl;
841
842     //4 <5> Memory for enhanced interrupt response
843     if (prAdapter->prSDIOCtrl) {
844         kalReleaseIOBuffer((PVOID)prAdapter->prSDIOCtrl, sizeof(ENHANCE_MODE_DATA_STRUCT_T));
845         prAdapter->prSDIOCtrl = (P_SDIO_CTRL_T)NULL;
846     }
847
848     //4 <4> Memory for Common Coalescing Buffer
849 #if CFG_COALESCING_BUFFER_SIZE || CFG_SDIO_RX_AGG
850     if (prAdapter->pucCoalescingBufCached) {
851         kalReleaseIOBuffer((PVOID)prAdapter->pucCoalescingBufCached, prAdapter->u4CoalescingBufCachedSize);
852         prAdapter->pucCoalescingBufCached = (PUINT_8)NULL;
853     }
854 #endif /* CFG_COALESCING_BUFFER_SIZE */
855
856     //4 <3> Memory for TX Descriptor
857     if (prTxCtrl->pucTxCached) {
858         kalMemFree((PVOID)prTxCtrl->pucTxCached,
859                    VIR_MEM_TYPE,
860                    prTxCtrl->u4TxCachedSize);
861         prTxCtrl->pucTxCached = (PUINT_8)NULL;
862     }
863
864     //4 <2> Memory for RX Descriptor
865     if (prRxCtrl->pucRxCached) {
866         kalMemFree((PVOID)prRxCtrl->pucRxCached,
867                    VIR_MEM_TYPE,
868                    prRxCtrl->u4RxCachedSize);
869         prRxCtrl->pucRxCached = (PUINT_8)NULL;
870     }
871
872     //4 <1> Memory for Management Memory Pool
873     if (prAdapter->pucMgtBufCached) {
874         kalMemFree((PVOID)prAdapter->pucMgtBufCached,
875                    VIR_MEM_TYPE,
876                    prAdapter->u4MgtBufCachedSize);
877         prAdapter->pucMgtBufCached = (PUINT_8)NULL;
878     }
879
880 #if CFG_DBG_MGT_BUF
881     /* Check if all allocated memories are free */
882     ASSERT(prAdapter->u4MemFreeDynamicCount ==
883            prAdapter->u4MemAllocDynamicCount);
884 #endif
885
886     return;
887 }
888
889
890 /*----------------------------------------------------------------------------*/
891 /*!
892 * @brief disable global interrupt
893 *
894 * @param prAdapter pointer to the Adapter handler
895 *
896 * @return (none)
897 */
898 /*----------------------------------------------------------------------------*/
899 VOID
900 nicDisableInterrupt (
901     IN P_ADAPTER_T prAdapter
902     )
903 {
904     ASSERT(prAdapter);
905
906     HAL_BYTE_WR(prAdapter, MCR_WHLPCR, WHLPCR_INT_EN_CLR);
907
908     prAdapter->fgIsIntEnable = FALSE;
909 }
910
911 /*----------------------------------------------------------------------------*/
912 /*!
913 * @brief enable global interrupt
914 *
915 * @param prAdapter pointer to the Adapter handler
916 *
917 * @return (none)
918 */
919 /*----------------------------------------------------------------------------*/
920 VOID
921 nicEnableInterrupt (
922     IN P_ADAPTER_T prAdapter
923     )
924 {
925     BOOLEAN fgIsIntEnableCache;
926
927
928     ASSERT(prAdapter);
929     fgIsIntEnableCache = prAdapter->fgIsIntEnable;
930
931     prAdapter->fgIsIntEnable = TRUE; // NOTE(Kevin): It must be placed before MCR GINT write.
932
933     /* If need enable INT and also set LPOwn at the same time. */
934     if (prAdapter->fgIsIntEnableWithLPOwnSet) {
935         prAdapter->fgIsIntEnableWithLPOwnSet = FALSE; /* NOTE(Kevin): It's better to place it
936                                                        * before MCR GINT write.
937                                                        */
938         /* If INT was enabled, only set LPOwn */
939         if (fgIsIntEnableCache) {
940             HAL_MCR_WR(prAdapter, MCR_WHLPCR, WHLPCR_FW_OWN_REQ_SET);
941             prAdapter->fgIsFwOwn = TRUE;
942         }
943         /* If INT was not enabled, enable it and also set LPOwn now */
944         else {
945             HAL_MCR_WR(prAdapter, MCR_WHLPCR, WHLPCR_FW_OWN_REQ_SET |
946                                               WHLPCR_INT_EN_SET);
947             prAdapter->fgIsFwOwn = TRUE;
948         }
949     }
950     /* If INT was not enabled, enable it now */
951     else if (!fgIsIntEnableCache) {
952         HAL_BYTE_WR(prAdapter, MCR_WHLPCR, WHLPCR_INT_EN_SET);
953     }
954
955     return;
956 } /* end of nicEnableInterrupt() */
957
958
959 #if CFG_SDIO_INTR_ENHANCE
960 /*----------------------------------------------------------------------------*/
961 /*!
962 * @brief For SDIO enhance mode, set the max rx len and tx status
963 *
964 * @param prAdapter      a pointer to adapter private data structure.
965 *
966 * @return  - none
967 */
968 /*----------------------------------------------------------------------------*/
969 VOID
970 nicSDIOInit (
971     IN P_ADAPTER_T prAdapter
972     )
973 {
974     UINT_32 u4Value = 0;
975
976     ASSERT(prAdapter);
977
978     //4 <1> Check STATUS Buffer is DW alignment.
979     ASSERT( IS_ALIGN_4( (UINT_32)&prAdapter->prSDIOCtrl->u4WHISR ) );
980
981     //4 <2> Setup STATUS count.
982     {
983         HAL_MCR_RD(prAdapter, MCR_WHCR, &u4Value);
984
985         //4 <2.1> Setup the number of maximum RX length to be report
986         u4Value &= ~(WHCR_MAX_HIF_RX_LEN_NUM);
987         u4Value |= ((SDIO_MAXIMUM_RX_LEN_NUM << WHCR_OFFSET_MAX_HIF_RX_LEN_NUM));
988
989         //4 <2.2> Setup RX enhancement mode
990 #if CFG_SDIO_RX_ENHANCE
991         u4Value |= WHCR_RX_ENHANCE_MODE_EN;
992 #else
993         u4Value &= ~WHCR_RX_ENHANCE_MODE_EN;
994 #endif /* CFG_SDIO_RX_AGG */
995
996         HAL_MCR_WR(prAdapter, MCR_WHCR, u4Value);
997     }
998
999     return;
1000
1001 } /* end of nicSDIOInit() */
1002
1003 /*----------------------------------------------------------------------------*/
1004 /*!
1005 * @brief Read interrupt status from hardware
1006 *
1007 * @param prAdapter pointer to the Adapter handler
1008 * @param the interrupts
1009 *
1010 * @return N/A
1011 *
1012 */
1013 /*----------------------------------------------------------------------------*/
1014 VOID
1015 nicSDIOReadIntStatus (
1016     IN P_ADAPTER_T prAdapter,
1017     OUT PUINT_32   pu4IntStatus
1018     )
1019 {
1020     P_SDIO_CTRL_T prSDIOCtrl;
1021
1022     DEBUGFUNC("nicSDIOReadIntStatus");
1023
1024     ASSERT(prAdapter);
1025     ASSERT(pu4IntStatus);
1026
1027     prSDIOCtrl = prAdapter->prSDIOCtrl;
1028     ASSERT(prSDIOCtrl);
1029
1030     HAL_PORT_RD(prAdapter,
1031                 MCR_WHISR,
1032                 sizeof(ENHANCE_MODE_DATA_STRUCT_T),
1033                 (PUINT_8)prSDIOCtrl,
1034                 sizeof(ENHANCE_MODE_DATA_STRUCT_T));
1035
1036     if(kalIsCardRemoved(prAdapter->prGlueInfo) == TRUE
1037             || fgIsBusAccessFailed == TRUE) {
1038         *pu4IntStatus = 0;
1039         return;
1040     }
1041
1042     /* workaround */
1043     if((prSDIOCtrl->u4WHISR & WHISR_TX_DONE_INT) == 0 &&
1044             (prSDIOCtrl->rTxInfo.au4WTSR[0] | prSDIOCtrl->rTxInfo.au4WTSR[1])) {
1045         prSDIOCtrl->u4WHISR |= WHISR_TX_DONE_INT;
1046     }
1047
1048     if((prSDIOCtrl->u4WHISR & BIT(31)) == 0 &&
1049             HAL_GET_MAILBOX_READ_CLEAR(prAdapter) == TRUE &&
1050             (prSDIOCtrl->u4RcvMailbox0 != 0 || prSDIOCtrl->u4RcvMailbox1 != 0)) {
1051         prSDIOCtrl->u4WHISR |= BIT(31);
1052     }
1053
1054     *pu4IntStatus = prSDIOCtrl->u4WHISR;
1055
1056     return;
1057 } /* end of nicSDIOReadIntStatus() */
1058 #endif
1059
1060 /*----------------------------------------------------------------------------*/
1061 /*!
1062 * @brief The function used to read interrupt status and then invoking
1063 *        dispatching procedure for the appropriate functions
1064 *        corresponding to specific interrupt bits
1065 *
1066 * @param prAdapter pointer to the Adapter handler
1067 *
1068 * @retval WLAN_STATUS_SUCCESS
1069 * @retval WLAN_STATUS_ADAPTER_NOT_READY
1070 */
1071 /*----------------------------------------------------------------------------*/
1072 WLAN_STATUS
1073 nicProcessIST (
1074     IN P_ADAPTER_T prAdapter
1075     )
1076 {
1077     WLAN_STATUS u4Status = WLAN_STATUS_SUCCESS;
1078     UINT_32 u4IntStatus = 0;
1079     UINT_32 i;
1080
1081     DEBUGFUNC("nicProcessIST");
1082     //DBGLOG(INIT, LOUD, ("\n"));
1083
1084     ASSERT(prAdapter);
1085
1086     if (prAdapter->rAcpiState == ACPI_STATE_D3) {
1087         DBGLOG(REQ, WARN, ("Fail in set nicProcessIST! (Adapter not ready). ACPI=D%d, Radio=%d\n",
1088                     prAdapter->rAcpiState, prAdapter->fgIsRadioOff));
1089         return WLAN_STATUS_ADAPTER_NOT_READY;
1090     }
1091
1092 #if (MT6620_E1_ASIC_HIFSYS_WORKAROUND == 1)
1093     if(prAdapter->fgIsClockGatingEnabled == TRUE) {
1094         nicDisableClockGating(prAdapter);
1095     }
1096 #endif
1097
1098     for (i = 0; i < CFG_IST_LOOP_COUNT; i++) {
1099
1100 #if CFG_SDIO_INTR_ENHANCE
1101         nicSDIOReadIntStatus(prAdapter, &u4IntStatus);
1102 #else
1103         HAL_MCR_RD(prAdapter, MCR_WHISR, &u4IntStatus);
1104 #endif /* CFG_SDIO_INTR_ENHANCE */
1105
1106         //DBGLOG(INIT, TRACE, ("u4IntStatus: 0x%x\n", u4IntStatus));
1107
1108         if (u4IntStatus & ~(WHIER_DEFAULT | WHIER_FW_OWN_BACK_INT_EN)) {
1109             DBGLOG(INTR, WARN, ("Un-handled HISR %#x, HISR = %#x (HIER:0x%x)\n",
1110                 (u4IntStatus & ~WHIER_DEFAULT), u4IntStatus, WHIER_DEFAULT));
1111             u4IntStatus &= WHIER_DEFAULT;
1112         }
1113
1114         nicProcessIST_impl(prAdapter, u4IntStatus);
1115
1116         if(u4IntStatus == 0) {
1117             if(i == 0) {
1118                 u4Status = WLAN_STATUS_NOT_INDICATING;
1119             }
1120             break;
1121         }
1122     }
1123
1124 #if (MT6620_E1_ASIC_HIFSYS_WORKAROUND == 1)
1125     if(prAdapter->fgIsClockGatingEnabled == FALSE) {
1126         nicEnableClockGating(prAdapter);
1127     }
1128 #endif
1129
1130     return u4Status;
1131 } /* end of nicProcessIST() */
1132
1133
1134 /*----------------------------------------------------------------------------*/
1135 /*!
1136 * @brief The function used to dispatch the appropriate functions for specific
1137 *        interrupt bits
1138 *
1139 * @param prAdapter   pointer to the Adapter handler
1140 *        u4IntStatus interrupt status bits
1141 *
1142 * @retval WLAN_STATUS_SUCCESS
1143 * @retval WLAN_STATUS_ADAPTER_NOT_READY
1144 */
1145 /*----------------------------------------------------------------------------*/
1146 WLAN_STATUS
1147 nicProcessIST_impl (
1148     IN P_ADAPTER_T prAdapter,
1149     IN UINT_32 u4IntStatus
1150     )
1151 {
1152     UINT_32 u4IntCount = 0;
1153     P_INT_EVENT_MAP_T prIntEventMap = NULL;
1154
1155     ASSERT(prAdapter);
1156
1157     prAdapter->u4IntStatus = u4IntStatus;
1158
1159     /* Process each of the interrupt status consequently */
1160     prIntEventMap = &arIntEventMapTable[0];
1161     for (u4IntCount = 0; u4IntCount < ucIntEventMapSize; prIntEventMap++, u4IntCount++) {
1162         if (prIntEventMap->u4Int & prAdapter->u4IntStatus) {
1163             if(prIntEventMap->u4Event == INT_EVENT_RX &&
1164                     prAdapter->fgIsEnterD3ReqIssued == TRUE) {
1165                 // ignore 
1166             }
1167             else if (apfnEventFuncTable[prIntEventMap->u4Event] != NULL) {
1168                 apfnEventFuncTable[prIntEventMap->u4Event](prAdapter);
1169             }
1170             else {
1171                 DBGLOG(INTR , WARN,
1172                         ("Empty INTR handler! ISAR bit#: %ld, event:%d, func: 0x%x\n",
1173                          prIntEventMap->u4Int, prIntEventMap->u4Event, apfnEventFuncTable[prIntEventMap->u4Event]));
1174
1175                 ASSERT(0); // to trap any NULL interrupt handler
1176             }
1177             prAdapter->u4IntStatus &= ~prIntEventMap->u4Int;
1178         }
1179     }
1180
1181     return WLAN_STATUS_SUCCESS;
1182 } /* end of nicProcessIST_impl() */
1183
1184
1185 /*----------------------------------------------------------------------------*/
1186 /*!
1187 * @brief Verify the CHIP ID
1188 *
1189 * @param prAdapter      a pointer to adapter private data structure.
1190 *
1191 *
1192 * @retval TRUE          CHIP ID is the same as the setting compiled
1193 * @retval FALSE         CHIP ID is different from the setting compiled
1194 */
1195 /*----------------------------------------------------------------------------*/
1196 BOOL
1197 nicVerifyChipID (
1198     IN P_ADAPTER_T prAdapter
1199     )
1200 {
1201     UINT_32 u4CIR = 0;
1202
1203     ASSERT(prAdapter);
1204
1205     HAL_MCR_RD(prAdapter, MCR_WCIR, &u4CIR );
1206
1207     DBGLOG(INIT, TRACE,("Chip ID: 0x%x\n", u4CIR & WCIR_CHIP_ID));
1208     DBGLOG(INIT, TRACE,("Revision ID: 0x%x\n", ((u4CIR & WCIR_REVISION_ID) >> 16)));
1209
1210     if ((u4CIR & WCIR_CHIP_ID) != MTK_CHIP_REV) {
1211         return FALSE;
1212     }
1213
1214     prAdapter->ucRevID = (UINT_8)(((u4CIR & WCIR_REVISION_ID) >> 16) & 0xF) ;
1215
1216     return TRUE;
1217 }
1218
1219 /*----------------------------------------------------------------------------*/
1220 /*!
1221 * @brief Initialize the MCR to the appropriate init value, and verify the init
1222 *        value
1223 *
1224 * @param prAdapter      a pointer to adapter private data structure.
1225 *
1226 * @return -
1227 */
1228 /*----------------------------------------------------------------------------*/
1229 VOID
1230 nicMCRInit (
1231     IN P_ADAPTER_T prAdapter
1232     )
1233 {
1234
1235     ASSERT(prAdapter);
1236
1237     //4 <0> Initial value
1238 }
1239
1240 VOID
1241 nicHifInit (
1242     IN P_ADAPTER_T prAdapter
1243     )
1244 {
1245
1246     ASSERT(prAdapter);
1247 #if 0
1248     /* reset event */
1249     nicPutMailbox(prAdapter, 0, 0x52455345); // RESE
1250     nicPutMailbox(prAdapter, 1, 0x545F5746); // T_WF
1251     nicSetSwIntr(prAdapter, BIT(16));
1252 #endif
1253 }
1254
1255
1256 /*----------------------------------------------------------------------------*/
1257 /*!
1258 * @brief Initialize the Adapter soft variable
1259 *
1260 * @param prAdapter pointer to the Adapter handler
1261 *
1262 * @return (none)
1263 *
1264 */
1265 /*----------------------------------------------------------------------------*/
1266 WLAN_STATUS
1267 nicInitializeAdapter (
1268     IN P_ADAPTER_T prAdapter
1269     )
1270 {
1271     WLAN_STATUS u4Status = WLAN_STATUS_SUCCESS;
1272
1273     ASSERT(prAdapter);
1274
1275     prAdapter->fgIsIntEnableWithLPOwnSet = FALSE;
1276
1277     do {
1278         if (!nicVerifyChipID(prAdapter)) {
1279             u4Status = WLAN_STATUS_FAILURE;
1280             break;
1281         }
1282
1283         //4 <1> MCR init
1284         nicMCRInit(prAdapter);
1285
1286     #if CFG_SDIO_INTR_ENHANCE
1287         nicSDIOInit(prAdapter);
1288     #endif /* CFG_SDIO_INTR_ENHANCE */
1289
1290         HAL_MCR_WR(prAdapter, MCR_WHIER, WHIER_DEFAULT);
1291
1292
1293         //4 <2> init FW HIF
1294         nicHifInit(prAdapter);
1295     }
1296     while (FALSE);
1297
1298
1299     return u4Status;
1300 }
1301
1302
1303 #if defined(_HIF_SPI)
1304 /*----------------------------------------------------------------------------*/
1305 /*!
1306 * \brief Restore the SPI Mode Select to default mode,
1307 *        this is important while driver is unload, and this must be last mcr
1308 *        since the operation will let the hif use 8bit mode access
1309 *
1310 * \param[in] prAdapter      a pointer to adapter private data structure.
1311 * \param[in] eGPIO2_Mode    GPIO2 operation mode
1312 *
1313 * \return (none)
1314 */
1315 /*----------------------------------------------------------------------------*/
1316 void
1317 nicRestoreSpiDefMode (
1318     IN P_ADAPTER_T prAdapter
1319     )
1320 {
1321     ASSERT(prAdapter);
1322
1323     HAL_MCR_WR(prAdapter, MCR_WCSR, SPICSR_8BIT_MODE_DATA);
1324
1325 }
1326 #endif
1327
1328 /*----------------------------------------------------------------------------*/
1329 /*!
1330 * @brief Process rx interrupt. When the rx
1331 *        Interrupt is asserted, it means there are frames in queue.
1332 *
1333 * @param prAdapter      Pointer to the Adapter structure.
1334 *
1335 * @return (none)
1336 */
1337 /*----------------------------------------------------------------------------*/
1338 VOID
1339 nicProcessAbnormalInterrupt (
1340     IN  P_ADAPTER_T prAdapter
1341     )
1342 {
1343     UINT_32 u4Value;
1344
1345     HAL_MCR_RD(prAdapter, MCR_WASR, &u4Value);
1346     DBGLOG(REQ, WARN, ("MCR_WASR: 0x%x \n", u4Value));
1347 }
1348
1349 /*----------------------------------------------------------------------------*/
1350 /*!
1351 * @brief .
1352 *
1353 * @param prAdapter  Pointer to the Adapter structure.
1354 *
1355 * @return (none)
1356 */
1357 /*----------------------------------------------------------------------------*/
1358 VOID
1359 nicProcessFwOwnBackInterrupt(
1360     IN P_ADAPTER_T prAdapter
1361     )
1362 {
1363
1364     return;
1365 } /* end of nicProcessFwOwnBackInterrupt() */
1366
1367 /*----------------------------------------------------------------------------*/
1368 /*!
1369 * @brief .
1370 *
1371 * @param prAdapter  Pointer to the Adapter structure.
1372 *
1373 * @return (none)
1374 */
1375 /*----------------------------------------------------------------------------*/
1376 VOID
1377 nicProcessSoftwareInterrupt(
1378     IN P_ADAPTER_T prAdapter
1379     )
1380 {
1381     UINT_32 u4IntrBits;
1382
1383     ASSERT(prAdapter);
1384
1385     u4IntrBits = prAdapter->u4IntStatus & BITS(8, 31);
1386
1387     if((u4IntrBits & WHISR_D2H_SW_ASSERT_INFO_INT) != 0) {
1388         nicPrintFirmwareAssertInfo(prAdapter);
1389 #if CFG_CHIP_RESET_SUPPORT
1390         glSendResetRequest();
1391 #endif
1392     }
1393
1394 #if (MT6620_E1_ASIC_HIFSYS_WORKAROUND == 1)
1395     ASSERT((u4IntrBits & (ACK_GATING_ENABLE_D2H_INT | ACK_GATING_DISABLE_D2H_INT))
1396             != (ACK_GATING_ENABLE_D2H_INT | ACK_GATING_DISABLE_D2H_INT));
1397
1398     if(u4IntrBits & ACK_GATING_ENABLE_D2H_INT) {
1399         prAdapter->fgIsClockGatingEnabled = TRUE;
1400     }
1401
1402     if(u4IntrBits & ACK_GATING_DISABLE_D2H_INT) {
1403         prAdapter->fgIsClockGatingEnabled = FALSE;
1404
1405         // Indicate Service Thread for TX
1406         if(kalGetTxPendingCmdCount(prAdapter->prGlueInfo) > 0
1407                 || wlanGetTxPendingFrameCount(prAdapter) > 0) {
1408             kalSetEvent(prAdapter->prGlueInfo);
1409         }
1410     }
1411 #endif
1412
1413     DBGLOG(REQ, WARN, ("u4IntrBits: 0x%x \n", u4IntrBits));
1414
1415     return;
1416 } /* end of nicProcessSoftwareInterrupt() */
1417
1418 VOID
1419 nicPutMailbox (
1420     IN P_ADAPTER_T prAdapter,
1421     IN UINT_32 u4MailboxNum,
1422     IN UINT_32 u4Data
1423     )
1424 {
1425     if (u4MailboxNum == 0) {
1426         HAL_MCR_WR(prAdapter, MCR_H2DSM0R, u4Data);
1427     } else if (u4MailboxNum == 1) {
1428         HAL_MCR_WR(prAdapter, MCR_H2DSM1R, u4Data);
1429     } else {
1430         ASSERT(0);
1431     }
1432 }
1433
1434 VOID
1435 nicGetMailbox (
1436     IN P_ADAPTER_T prAdapter,
1437     IN UINT_32 u4MailboxNum,
1438     OUT PUINT_32 pu4Data
1439     )
1440 {
1441     if (u4MailboxNum == 0) {
1442         HAL_MCR_RD(prAdapter, MCR_D2HRM0R, pu4Data);
1443     } else if (u4MailboxNum == 1) {
1444         HAL_MCR_RD(prAdapter, MCR_D2HRM1R, pu4Data);
1445     } else {
1446         ASSERT(0);
1447     }
1448 }
1449
1450 VOID
1451 nicSetSwIntr (
1452     IN P_ADAPTER_T prAdapter,
1453     IN UINT_32 u4SwIntrBitmap
1454     )
1455 {
1456     /* NOTE:
1457      *  SW interrup in HW bit 16 is mapping to SW bit 0 (shift 16bit in HW transparancy)
1458      *  SW interrupt valid from b0~b15
1459      */
1460     ASSERT((u4SwIntrBitmap & BITS(0, 15)) == 0);
1461 //    DBGLOG(INIT, TRACE, ("u4SwIntrBitmap: 0x%08x\n", u4SwIntrBitmap));
1462
1463     HAL_MCR_WR(prAdapter, MCR_WSICR, u4SwIntrBitmap);
1464 }
1465
1466
1467 /*----------------------------------------------------------------------------*/
1468 /*!
1469 * @brief This procedure is used to dequeue from prAdapter->rPendingCmdQueue
1470 *        with specified sequential number
1471 *
1472 * @param    prAdapter   Pointer of ADAPTER_T
1473 *           ucSeqNum    Sequential Number
1474 *
1475 * @retval - P_CMD_INFO_T
1476 */
1477 /*----------------------------------------------------------------------------*/
1478 P_CMD_INFO_T
1479 nicGetPendingCmdInfo (
1480     IN P_ADAPTER_T prAdapter,
1481     IN UINT_8 ucSeqNum
1482     )
1483 {
1484     P_QUE_T prCmdQue;
1485     QUE_T rTempCmdQue;
1486     P_QUE_T prTempCmdQue = &rTempCmdQue;
1487     P_QUE_ENTRY_T prQueueEntry = (P_QUE_ENTRY_T)NULL;
1488     P_CMD_INFO_T prCmdInfo = (P_CMD_INFO_T)NULL;
1489
1490     GLUE_SPIN_LOCK_DECLARATION();
1491
1492     ASSERT(prAdapter);
1493
1494     KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_CMD_PENDING);
1495
1496     prCmdQue = &prAdapter->rPendingCmdQueue;
1497     QUEUE_MOVE_ALL(prTempCmdQue, prCmdQue);
1498
1499     QUEUE_REMOVE_HEAD(prTempCmdQue, prQueueEntry, P_QUE_ENTRY_T);
1500     while (prQueueEntry) {
1501         prCmdInfo = (P_CMD_INFO_T)prQueueEntry;
1502
1503         if(prCmdInfo->ucCmdSeqNum == ucSeqNum)
1504             break;
1505         else {
1506             QUEUE_INSERT_TAIL(prCmdQue, prQueueEntry);
1507
1508             prCmdInfo = NULL;
1509         }
1510
1511         QUEUE_REMOVE_HEAD(prTempCmdQue, prQueueEntry, P_QUE_ENTRY_T);
1512     }
1513     QUEUE_CONCATENATE_QUEUES(prCmdQue, prTempCmdQue);
1514
1515     KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_CMD_PENDING);
1516
1517     return prCmdInfo;
1518 }
1519
1520
1521 /*----------------------------------------------------------------------------*/
1522 /*!
1523 * @brief This procedure is used to dequeue from prAdapter->rTxCtrl.rTxMgmtTxingQueue
1524 *        with specified sequential number
1525 *
1526 * @param    prAdapter   Pointer of ADAPTER_T
1527 *           ucSeqNum    Sequential Number
1528 *
1529 * @retval - P_MSDU_INFO_T
1530 */
1531 /*----------------------------------------------------------------------------*/
1532 P_MSDU_INFO_T
1533 nicGetPendingTxMsduInfo (
1534     IN P_ADAPTER_T prAdapter,
1535     IN UINT_8 ucSeqNum
1536     )
1537 {
1538     P_QUE_T prTxingQue;
1539     QUE_T rTempQue;
1540     P_QUE_T prTempQue = &rTempQue;
1541     P_QUE_ENTRY_T prQueueEntry = (P_QUE_ENTRY_T)NULL;
1542     P_MSDU_INFO_T prMsduInfo = (P_MSDU_INFO_T)NULL;
1543
1544     GLUE_SPIN_LOCK_DECLARATION();
1545
1546     ASSERT(prAdapter);
1547
1548     KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TXING_MGMT_LIST);
1549
1550     prTxingQue = &(prAdapter->rTxCtrl.rTxMgmtTxingQueue);
1551     QUEUE_MOVE_ALL(prTempQue, prTxingQue);
1552
1553     QUEUE_REMOVE_HEAD(prTempQue, prQueueEntry, P_QUE_ENTRY_T);
1554     while (prQueueEntry) {
1555         prMsduInfo = (P_MSDU_INFO_T)prQueueEntry;
1556
1557         if(prMsduInfo->ucTxSeqNum == ucSeqNum)
1558             break;
1559         else {
1560             QUEUE_INSERT_TAIL(prTxingQue, prQueueEntry);
1561
1562             prMsduInfo = NULL;
1563         }
1564
1565         QUEUE_REMOVE_HEAD(prTempQue, prQueueEntry, P_QUE_ENTRY_T);
1566     }
1567     QUEUE_CONCATENATE_QUEUES(prTxingQue, prTempQue);
1568
1569     KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TXING_MGMT_LIST);
1570
1571     return prMsduInfo;
1572 }
1573
1574 P_MSDU_INFO_T
1575 nicGetPendingStaMMPDU (
1576     IN P_ADAPTER_T prAdapter,
1577     IN UINT_8 ucStaRecIdx
1578     )
1579 {
1580     P_MSDU_INFO_T prMsduInfoListHead = (P_MSDU_INFO_T)NULL;
1581     P_QUE_T prTxingQue = (P_QUE_T)NULL;
1582     QUE_T rTempQue;
1583     P_QUE_T prTempQue = &rTempQue;
1584     P_QUE_ENTRY_T prQueueEntry = (P_QUE_ENTRY_T)NULL;
1585     P_MSDU_INFO_T prMsduInfo = (P_MSDU_INFO_T)NULL;
1586
1587     GLUE_SPIN_LOCK_DECLARATION();
1588
1589     KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TXING_MGMT_LIST);
1590     do {
1591         if (prAdapter == NULL) {
1592
1593             ASSERT(FALSE);
1594             break;
1595         }
1596
1597         prTxingQue = &(prAdapter->rTxCtrl.rTxMgmtTxingQueue);
1598         QUEUE_MOVE_ALL(prTempQue, prTxingQue);
1599
1600         QUEUE_REMOVE_HEAD(prTempQue, prQueueEntry, P_QUE_ENTRY_T);
1601         while (prQueueEntry) {
1602             prMsduInfo = (P_MSDU_INFO_T)prQueueEntry;
1603
1604             if ((prMsduInfo->ucStaRecIndex == ucStaRecIdx) && (prMsduInfo->pfTxDoneHandler != NULL)) {
1605                 QM_TX_SET_NEXT_MSDU_INFO(prMsduInfo,
1606                                     prMsduInfoListHead);
1607                 prMsduInfoListHead = prMsduInfo;
1608             }
1609             else {
1610                 QUEUE_INSERT_TAIL(prTxingQue, prQueueEntry);
1611
1612                 prMsduInfo = NULL;
1613             }
1614
1615
1616             QUEUE_REMOVE_HEAD(prTempQue, prQueueEntry, P_QUE_ENTRY_T);
1617         }
1618
1619     } while (FALSE);
1620     KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TXING_MGMT_LIST);
1621
1622
1623     return prMsduInfoListHead;
1624 } /* nicGetPendingStaMMPDU */
1625
1626
1627 VOID
1628 nicFreePendingTxMsduInfoByNetwork (
1629     IN P_ADAPTER_T                  prAdapter,
1630     IN ENUM_NETWORK_TYPE_INDEX_T    eNetworkType
1631     )
1632 {
1633     P_QUE_T prTxingQue;
1634     QUE_T rTempQue;
1635     P_QUE_T prTempQue = &rTempQue;
1636     P_QUE_ENTRY_T prQueueEntry = (P_QUE_ENTRY_T)NULL;
1637     P_MSDU_INFO_T prMsduInfoListHead = (P_MSDU_INFO_T)NULL;
1638     P_MSDU_INFO_T prMsduInfoListTail = (P_MSDU_INFO_T)NULL;
1639     P_MSDU_INFO_T prMsduInfo = (P_MSDU_INFO_T)NULL;
1640
1641     GLUE_SPIN_LOCK_DECLARATION();
1642
1643     ASSERT(prAdapter);
1644
1645     KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TXING_MGMT_LIST);
1646
1647     prTxingQue = &(prAdapter->rTxCtrl.rTxMgmtTxingQueue);
1648     QUEUE_MOVE_ALL(prTempQue, prTxingQue);
1649
1650     QUEUE_REMOVE_HEAD(prTempQue, prQueueEntry, P_QUE_ENTRY_T);
1651     while (prQueueEntry) {
1652         prMsduInfo = (P_MSDU_INFO_T)prQueueEntry;
1653
1654         if((ENUM_NETWORK_TYPE_INDEX_T)(prMsduInfo->ucNetworkType) == eNetworkType) {
1655             if(prMsduInfoListHead == NULL) {
1656                 prMsduInfoListHead = prMsduInfoListTail = prMsduInfo;
1657             }
1658             else {
1659                 QM_TX_SET_NEXT_MSDU_INFO(prMsduInfoListTail, prMsduInfo);
1660                 prMsduInfoListTail = prMsduInfo;
1661             }
1662         }
1663         else {
1664             QUEUE_INSERT_TAIL(prTxingQue, prQueueEntry);
1665
1666             prMsduInfo = NULL;
1667         }
1668
1669         QUEUE_REMOVE_HEAD(prTempQue, prQueueEntry, P_QUE_ENTRY_T);
1670     }
1671     QUEUE_CONCATENATE_QUEUES(prTxingQue, prTempQue);
1672
1673     KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TXING_MGMT_LIST);
1674
1675     /* free */
1676     if(prMsduInfoListHead) {
1677         nicTxFreeMsduInfoPacket(prAdapter, prMsduInfoListHead);
1678     }
1679
1680     return;
1681
1682 } /* end of nicFreePendingTxMsduInfoByNetwork() */
1683
1684
1685
1686 /*----------------------------------------------------------------------------*/
1687 /*!
1688 * @brief This procedure is used to retrieve a CMD sequence number atomically
1689 *
1690 * @param    prAdapter   Pointer of ADAPTER_T
1691 *
1692 * @retval - UINT_8
1693 */
1694 /*----------------------------------------------------------------------------*/
1695 UINT_8
1696 nicIncreaseCmdSeqNum (
1697     IN P_ADAPTER_T prAdapter
1698     )
1699 {
1700     UINT_8 ucRetval;
1701
1702     KAL_SPIN_LOCK_DECLARATION();
1703
1704     ASSERT(prAdapter);
1705
1706     KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_CMD_SEQ_NUM);
1707
1708     prAdapter->ucCmdSeqNum++;
1709     ucRetval = prAdapter->ucCmdSeqNum;
1710
1711     KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_CMD_SEQ_NUM);
1712
1713     return ucRetval;
1714 }
1715
1716
1717 /*----------------------------------------------------------------------------*/
1718 /*!
1719 * @brief This procedure is used to retrieve a TX sequence number atomically
1720 *
1721 * @param    prAdapter   Pointer of ADAPTER_T
1722 *
1723 * @retval - UINT_8
1724 */
1725 /*----------------------------------------------------------------------------*/
1726 UINT_8
1727 nicIncreaseTxSeqNum (
1728     IN P_ADAPTER_T prAdapter
1729     )
1730 {
1731     UINT_8 ucRetval;
1732
1733     KAL_SPIN_LOCK_DECLARATION();
1734
1735     ASSERT(prAdapter);
1736
1737     KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TX_SEQ_NUM);
1738
1739     prAdapter->ucTxSeqNum++;
1740     ucRetval = prAdapter->ucTxSeqNum;
1741
1742     KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TX_SEQ_NUM);
1743
1744     return ucRetval;
1745 }
1746
1747
1748
1749 /*----------------------------------------------------------------------------*/
1750 /*!
1751 * @brief This utility function is used to handle
1752 *        media state change event
1753 *
1754 * @param
1755 *
1756 * @retval
1757 */
1758 /*----------------------------------------------------------------------------*/
1759 WLAN_STATUS
1760 nicMediaStateChange (
1761     IN P_ADAPTER_T                  prAdapter,
1762     IN ENUM_NETWORK_TYPE_INDEX_T    eNetworkType,
1763     IN P_EVENT_CONNECTION_STATUS    prConnectionStatus
1764     )
1765 {
1766     P_GLUE_INFO_T prGlueInfo;
1767
1768     ASSERT(prAdapter);
1769     prGlueInfo = prAdapter->prGlueInfo;
1770
1771     switch(eNetworkType) {
1772     case NETWORK_TYPE_AIS_INDEX:
1773         if (prConnectionStatus->ucMediaStatus == PARAM_MEDIA_STATE_DISCONNECTED) { // disconnected
1774             if(kalGetMediaStateIndicated(prGlueInfo) != PARAM_MEDIA_STATE_DISCONNECTED) {
1775
1776                 kalIndicateStatusAndComplete(prGlueInfo,
1777                         WLAN_STATUS_MEDIA_DISCONNECT,
1778                         NULL,
1779                         0);
1780
1781                 prAdapter->rWlanInfo.u4SysTime = kalGetTimeTick();
1782             }
1783
1784             /* reset buffered link quality information */
1785             prAdapter->fgIsLinkQualityValid = FALSE;
1786             prAdapter->fgIsLinkRateValid = FALSE;
1787         }
1788         else if(prConnectionStatus->ucMediaStatus == PARAM_MEDIA_STATE_CONNECTED) { // connected
1789             prAdapter->rWlanInfo.u4SysTime = kalGetTimeTick();
1790
1791             // fill information for association result
1792             prAdapter->rWlanInfo.rCurrBssId.rSsid.u4SsidLen
1793                 = prConnectionStatus->ucSsidLen;
1794             kalMemCopy(prAdapter->rWlanInfo.rCurrBssId.rSsid.aucSsid,
1795                     prConnectionStatus->aucSsid,
1796                     prConnectionStatus->ucSsidLen);
1797             kalMemCopy(prAdapter->rWlanInfo.rCurrBssId.arMacAddress,
1798                     prConnectionStatus->aucBssid,
1799                     MAC_ADDR_LEN);
1800             prAdapter->rWlanInfo.rCurrBssId.u4Privacy
1801                 = prConnectionStatus->ucEncryptStatus; // @FIXME
1802             prAdapter->rWlanInfo.rCurrBssId.rRssi
1803                 = 0; //@FIXME
1804             prAdapter->rWlanInfo.rCurrBssId.eNetworkTypeInUse
1805                 = PARAM_NETWORK_TYPE_AUTOMODE; //@FIXME
1806             prAdapter->rWlanInfo.rCurrBssId.rConfiguration.u4BeaconPeriod
1807                 = prConnectionStatus->u2BeaconPeriod;
1808             prAdapter->rWlanInfo.rCurrBssId.rConfiguration.u4ATIMWindow
1809                 = prConnectionStatus->u2ATIMWindow;
1810             prAdapter->rWlanInfo.rCurrBssId.rConfiguration.u4DSConfig
1811                 = prConnectionStatus->u4FreqInKHz;
1812             prAdapter->rWlanInfo.ucNetworkType
1813                 = prConnectionStatus->ucNetworkType;
1814             prAdapter->rWlanInfo.rCurrBssId.eOpMode
1815                 = (ENUM_PARAM_OP_MODE_T) prConnectionStatus->ucInfraMode;
1816
1817             // always indicate to OS according to MSDN (re-association/roaming)
1818             if(kalGetMediaStateIndicated(prGlueInfo) != PARAM_MEDIA_STATE_CONNECTED) {
1819                 kalIndicateStatusAndComplete(prGlueInfo,
1820                         WLAN_STATUS_MEDIA_CONNECT,
1821                         NULL,
1822                         0);
1823             }
1824             else {
1825                 /* connected -> connected : roaming ? */
1826                 kalIndicateStatusAndComplete(prGlueInfo,
1827                         WLAN_STATUS_ROAM_OUT_FIND_BEST,
1828                         NULL,
1829                         0);
1830             }
1831         }
1832         break;
1833
1834 #if CFG_ENABLE_BT_OVER_WIFI
1835     case NETWORK_TYPE_BOW_INDEX:
1836         break;
1837 #endif
1838
1839 #if CFG_ENABLE_WIFI_DIRECT
1840     case NETWORK_TYPE_P2P_INDEX:
1841         break;
1842 #endif
1843     default:
1844         ASSERT(0);
1845     }
1846
1847     return WLAN_STATUS_SUCCESS;
1848 } /* nicMediaStateChange */
1849
1850 /*----------------------------------------------------------------------------*/
1851 /*!
1852 * @brief This utility function is used to convert between
1853 *        frequency and channel number
1854 *
1855 * @param u4ChannelNum
1856 *
1857 * @retval - Frequency in unit of KHz, 0 for invalid channel number
1858 */
1859 /*----------------------------------------------------------------------------*/
1860 UINT_32
1861 nicChannelNum2Freq (
1862     UINT_32 u4ChannelNum
1863     )
1864 {
1865     UINT_32 u4ChannelInMHz;
1866
1867     if(u4ChannelNum >= 1 && u4ChannelNum <= 13) {
1868         u4ChannelInMHz = 2412 + (u4ChannelNum - 1) * 5;
1869     }
1870     else if(u4ChannelNum == 14) {
1871         u4ChannelInMHz = 2484;
1872     }
1873     else if(u4ChannelNum == 133) {
1874         u4ChannelInMHz = 3665; // 802.11y
1875     }
1876     else if(u4ChannelNum == 137) {
1877         u4ChannelInMHz = 3685; // 802.11y
1878     }
1879     else if(u4ChannelNum >= 34 && u4ChannelNum <= 165) {
1880         u4ChannelInMHz = 5000 + u4ChannelNum * 5;
1881     }
1882     else if(u4ChannelNum >= 183 && u4ChannelNum <= 196) {
1883         u4ChannelInMHz = 4000 + u4ChannelNum * 5;
1884     }
1885     else {
1886         u4ChannelInMHz = 0;
1887     }
1888
1889     return 1000 * u4ChannelInMHz;
1890 }
1891
1892
1893 /*----------------------------------------------------------------------------*/
1894 /*!
1895 * @brief This utility function is used to convert between
1896 *        frequency and channel number
1897 *
1898 * @param u4FreqInKHz
1899 *
1900 * @retval - Frequency Number, 0 for invalid freqency
1901 */
1902 /*----------------------------------------------------------------------------*/
1903 UINT_32
1904 nicFreq2ChannelNum (
1905     UINT_32 u4FreqInKHz
1906     )
1907 {
1908     switch(u4FreqInKHz) {
1909     case 2412000:
1910         return 1;
1911     case 2417000:
1912         return 2;
1913     case 2422000:
1914         return 3;
1915     case 2427000:
1916         return 4;
1917     case 2432000:
1918         return 5;
1919     case 2437000:
1920         return 6;
1921     case 2442000:
1922         return 7;
1923     case 2447000:
1924         return 8;
1925     case 2452000:
1926         return 9;
1927     case 2457000:
1928         return 10;
1929     case 2462000:
1930         return 11;
1931     case 2467000:
1932         return 12;
1933     case 2472000:
1934         return 13;
1935     case 2484000:
1936         return 14;
1937     case 3665000:
1938         return 133; // 802.11y
1939     case 3685000:
1940         return 137; // 802.11y
1941     case 4915000:
1942         return 183;
1943     case 4920000:
1944         return 184;
1945     case 4925000:
1946         return 185;
1947     case 4930000:
1948         return 186;
1949     case 4935000:
1950         return 187;
1951     case 4940000:
1952         return 188;
1953     case 4945000:
1954         return 189;
1955     case 4960000:
1956         return 192;
1957     case 4980000:
1958         return 196;
1959     case 5170000:
1960         return 34;
1961     case 5180000:
1962         return 36;
1963     case 5190000:
1964         return 38;
1965     case 5200000:
1966         return 40;
1967     case 5210000:
1968         return 42;
1969     case 5220000:
1970         return 44;
1971     case 5230000:
1972         return 46;
1973     case 5240000:
1974         return 48;
1975     case 5250000:
1976         return 50;
1977     case 5260000:
1978         return 52;
1979     case 5270000:
1980         return 54;
1981     case 5280000:
1982         return 56;
1983     case 5290000:
1984         return 58;
1985     case 5300000:
1986         return 60;
1987     case 5320000:
1988         return 64;
1989     case 5500000:
1990         return 100;
1991     case 5520000:
1992         return 104;
1993     case 5540000:
1994         return 108;
1995     case 5560000:
1996         return 112;
1997     case 5580000:
1998         return 116;
1999     case 5600000:
2000         return 120;
2001     case 5620000:
2002         return 124;
2003     case 5640000:
2004         return 128;
2005     case 5660000:
2006         return 132;
2007     case 5680000:
2008         return 136;
2009     case 5700000:
2010         return 140;
2011     case 5745000:
2012         return 149;
2013     case 5765000:
2014         return 153;
2015     case 5785000:
2016         return 157;
2017     case 5805000:
2018         return 161;
2019     case 5825000:
2020         return 165;
2021     default:
2022         return 0;
2023     }
2024 }
2025
2026
2027 /* firmware command wrapper */
2028 /* NETWORK (WIFISYS) */
2029 /*----------------------------------------------------------------------------*/
2030 /*!
2031 * @brief This utility function is used to activate WIFISYS for specified network
2032 *
2033 * @param prAdapter          Pointer of ADAPTER_T
2034 *        eNetworkTypeIdx    Index of network type
2035 *
2036 * @retval -
2037 */
2038 /*----------------------------------------------------------------------------*/
2039 WLAN_STATUS
2040 nicActivateNetwork(
2041     IN P_ADAPTER_T prAdapter,
2042     IN ENUM_NETWORK_TYPE_INDEX_T eNetworkTypeIdx
2043     )
2044 {
2045     CMD_BSS_ACTIVATE_CTRL   rCmdActivateCtrl;
2046     P_BSS_INFO_T            prBssInfo;
2047
2048     ASSERT(prAdapter);
2049     ASSERT(eNetworkTypeIdx < NETWORK_TYPE_INDEX_NUM);
2050
2051     rCmdActivateCtrl.ucNetTypeIndex     = (UINT_8)eNetworkTypeIdx;
2052     rCmdActivateCtrl.ucActive           = 1;
2053
2054     if (((UINT_8) eNetworkTypeIdx) < NETWORK_TYPE_INDEX_NUM) {
2055         prBssInfo = &prAdapter->rWifiVar.arBssInfo[eNetworkTypeIdx];
2056         prBssInfo->fg40mBwAllowed = FALSE;
2057         prBssInfo->fgAssoc40mBwAllowed = FALSE;
2058     }
2059
2060     return wlanSendSetQueryCmd(prAdapter,
2061             CMD_ID_BSS_ACTIVATE_CTRL,
2062             TRUE,
2063             FALSE,
2064             FALSE,
2065             NULL,
2066             NULL,
2067             sizeof(CMD_BSS_ACTIVATE_CTRL),
2068             (PUINT_8)&rCmdActivateCtrl,
2069             NULL,
2070             0);
2071 }
2072
2073
2074 /*----------------------------------------------------------------------------*/
2075 /*!
2076 * @brief This utility function is used to deactivate WIFISYS for specified network
2077 *
2078 * @param prAdapter          Pointer of ADAPTER_T
2079 *        eNetworkTypeIdx    Index of network type
2080 *
2081 * @retval -
2082 */
2083 /*----------------------------------------------------------------------------*/
2084 WLAN_STATUS
2085 nicDeactivateNetwork(
2086     IN P_ADAPTER_T prAdapter,
2087     IN ENUM_NETWORK_TYPE_INDEX_T eNetworkTypeIdx
2088     )
2089 {
2090     WLAN_STATUS u4Status;
2091     CMD_BSS_ACTIVATE_CTRL rCmdActivateCtrl;
2092
2093     ASSERT(prAdapter);
2094     ASSERT(eNetworkTypeIdx < NETWORK_TYPE_INDEX_NUM);
2095
2096     rCmdActivateCtrl.ucNetTypeIndex     = (UINT_8)eNetworkTypeIdx;
2097     rCmdActivateCtrl.ucActive           = 0;
2098
2099     u4Status = wlanSendSetQueryCmd(prAdapter,
2100             CMD_ID_BSS_ACTIVATE_CTRL,
2101             TRUE,
2102             FALSE,
2103             FALSE,
2104             NULL,
2105             NULL,
2106             sizeof(CMD_BSS_ACTIVATE_CTRL),
2107             (PUINT_8)&rCmdActivateCtrl,
2108             NULL,
2109             0);
2110
2111     /* free all correlated station records */
2112     cnmStaFreeAllStaByNetType(prAdapter, eNetworkTypeIdx, FALSE);
2113     qmFreeAllByNetType(prAdapter, eNetworkTypeIdx);
2114     nicFreePendingTxMsduInfoByNetwork(prAdapter, eNetworkTypeIdx);
2115     kalClearSecurityFramesByNetType(prAdapter->prGlueInfo, eNetworkTypeIdx);
2116
2117     return u4Status;
2118 }
2119
2120
2121 /* BSS-INFO */
2122 /*----------------------------------------------------------------------------*/
2123 /*!
2124 * @brief This utility function is used to sync bss info with firmware
2125 *        when a new BSS has been connected or disconnected
2126 *
2127 * @param prAdapter          Pointer of ADAPTER_T
2128 *        eNetworkTypeIdx    Index of BSS-INFO type
2129 *
2130 * @retval -
2131 */
2132 /*----------------------------------------------------------------------------*/
2133 WLAN_STATUS
2134 nicUpdateBss(
2135     IN P_ADAPTER_T prAdapter,
2136     IN ENUM_NETWORK_TYPE_INDEX_T eNetworkTypeIdx
2137     )
2138 {
2139     WLAN_STATUS u4Status;
2140     P_BSS_INFO_T prBssInfo;
2141     CMD_SET_BSS_INFO rCmdSetBssInfo;
2142
2143     ASSERT(prAdapter);
2144     ASSERT(eNetworkTypeIdx < NETWORK_TYPE_INDEX_NUM);
2145
2146     prBssInfo = &(prAdapter->rWifiVar.arBssInfo[eNetworkTypeIdx]);
2147
2148     kalMemZero(&rCmdSetBssInfo, sizeof(CMD_SET_BSS_INFO));
2149
2150     rCmdSetBssInfo.ucNetTypeIndex               = (UINT_8)eNetworkTypeIdx;
2151     rCmdSetBssInfo.ucConnectionState            = (UINT_8)prBssInfo->eConnectionState;
2152     rCmdSetBssInfo.ucCurrentOPMode              = (UINT_8)prBssInfo->eCurrentOPMode;
2153     rCmdSetBssInfo.ucSSIDLen                    = (UINT_8)prBssInfo->ucSSIDLen;
2154     kalMemCopy(rCmdSetBssInfo.aucSSID, prBssInfo->aucSSID, prBssInfo->ucSSIDLen);
2155     COPY_MAC_ADDR(rCmdSetBssInfo.aucBSSID, prBssInfo->aucBSSID);
2156     rCmdSetBssInfo.ucIsQBSS                     = (UINT_8)prBssInfo->fgIsQBSS;
2157     rCmdSetBssInfo.ucNonHTBasicPhyType          = prBssInfo->ucNonHTBasicPhyType;
2158     rCmdSetBssInfo.u2OperationalRateSet         = prBssInfo->u2OperationalRateSet;
2159     rCmdSetBssInfo.u2BSSBasicRateSet            = prBssInfo->u2BSSBasicRateSet;
2160     rCmdSetBssInfo.ucPhyTypeSet                 = prBssInfo->ucPhyTypeSet;
2161 #if CFG_ENABLE_WIFI_DIRECT
2162     if(prAdapter->fgIsP2PRegistered) {
2163         COPY_MAC_ADDR(rCmdSetBssInfo.aucOwnMac, prBssInfo->aucOwnMacAddr);
2164     }
2165 #endif
2166
2167     rlmFillSyncCmdParam(&rCmdSetBssInfo.rBssRlmParam, prBssInfo);
2168
2169     rCmdSetBssInfo.fgWapiMode                   = (UINT_8)FALSE;
2170
2171     if(rCmdSetBssInfo.ucNetTypeIndex == NETWORK_TYPE_AIS_INDEX) {
2172         P_CONNECTION_SETTINGS_T prConnSettings = &(prAdapter->rWifiVar.rConnSettings);
2173
2174         rCmdSetBssInfo.ucAuthMode               = (UINT_8)prConnSettings->eAuthMode;
2175         rCmdSetBssInfo.ucEncStatus              = (UINT_8)prConnSettings->eEncStatus;
2176         rCmdSetBssInfo.fgWapiMode               = (UINT_8)prConnSettings->fgWapiMode;
2177     }
2178 #if CFG_ENABLE_BT_OVER_WIFI
2179     else if(rCmdSetBssInfo.ucNetTypeIndex == NETWORK_TYPE_BOW_INDEX) {
2180         //P_CONNECTION_SETTINGS_T prConnSettings = &(prAdapter->rWifiVar.rConnSettings);
2181         rCmdSetBssInfo.ucAuthMode               = (UINT_8)AUTH_MODE_WPA2_PSK;
2182         rCmdSetBssInfo.ucEncStatus              = (UINT_8)ENUM_ENCRYPTION3_KEY_ABSENT;
2183     }
2184 #endif
2185     else {
2186 #if CFG_ENABLE_WIFI_DIRECT
2187         if(prAdapter->fgIsP2PRegistered) {
2188             if (kalP2PGetCipher(prAdapter->prGlueInfo)) {
2189                                 rCmdSetBssInfo.ucAuthMode               = (UINT_8)AUTH_MODE_WPA2_PSK;
2190                                 rCmdSetBssInfo.ucEncStatus              = (UINT_8)ENUM_ENCRYPTION3_KEY_ABSENT;
2191                         }
2192                         else {
2193                                 rCmdSetBssInfo.ucAuthMode               = (UINT_8)AUTH_MODE_OPEN;
2194                                 rCmdSetBssInfo.ucEncStatus              = (UINT_8)ENUM_ENCRYPTION_DISABLED;
2195                         }
2196                         /* Need the probe response to detect the PBC overlap */    
2197             rCmdSetBssInfo.fgIsApMode = p2pFuncIsAPMode(prAdapter->rWifiVar.prP2pFsmInfo);
2198             }
2199 #else
2200         rCmdSetBssInfo.ucAuthMode               = (UINT_8)AUTH_MODE_WPA2_PSK;
2201         rCmdSetBssInfo.ucEncStatus              = (UINT_8)ENUM_ENCRYPTION3_KEY_ABSENT;
2202 #endif
2203     }
2204
2205     if(eNetworkTypeIdx == NETWORK_TYPE_AIS_INDEX &&
2206             prBssInfo->eCurrentOPMode == OP_MODE_INFRASTRUCTURE &&
2207             prBssInfo->prStaRecOfAP != NULL) {
2208         rCmdSetBssInfo.ucStaRecIdxOfAP          = prBssInfo->prStaRecOfAP->ucIndex;
2209
2210         cnmAisInfraConnectNotify(prAdapter);
2211     }
2212 #if CFG_ENABLE_WIFI_DIRECT
2213     else if ((prAdapter->fgIsP2PRegistered) &&
2214             (eNetworkTypeIdx == NETWORK_TYPE_P2P_INDEX) &&
2215             (prBssInfo->eCurrentOPMode == OP_MODE_INFRASTRUCTURE) &&
2216             (prBssInfo->prStaRecOfAP != NULL)) {
2217         rCmdSetBssInfo.ucStaRecIdxOfAP = prBssInfo->prStaRecOfAP->ucIndex;
2218     }
2219 #endif
2220
2221 #if CFG_ENABLE_BT_OVER_WIFI
2222     else if (eNetworkTypeIdx == NETWORK_TYPE_BOW_INDEX &&
2223             prBssInfo->eCurrentOPMode == OP_MODE_BOW &&
2224             prBssInfo->prStaRecOfAP != NULL) {
2225         rCmdSetBssInfo.ucStaRecIdxOfAP = prBssInfo->prStaRecOfAP->ucIndex;
2226     }
2227 #endif
2228     else {
2229         rCmdSetBssInfo.ucStaRecIdxOfAP              = STA_REC_INDEX_NOT_FOUND;
2230     }
2231
2232     u4Status = wlanSendSetQueryCmd(prAdapter,
2233             CMD_ID_SET_BSS_INFO,
2234             TRUE,
2235             FALSE,
2236             FALSE,
2237             NULL,
2238             NULL,
2239             sizeof(CMD_SET_BSS_INFO),
2240             (PUINT_8)&rCmdSetBssInfo,
2241             NULL,
2242             0);
2243
2244     /* if BSS-INFO is going to be disconnected state, free all correlated station records */
2245     if(prBssInfo->eConnectionState == PARAM_MEDIA_STATE_DISCONNECTED) {
2246         /* clear client list */
2247         bssClearClientList(prAdapter, prBssInfo);
2248
2249         /* free all correlated station records */
2250         cnmStaFreeAllStaByNetType(prAdapter, eNetworkTypeIdx, FALSE);
2251         qmFreeAllByNetType(prAdapter, eNetworkTypeIdx);
2252         kalClearSecurityFramesByNetType(prAdapter->prGlueInfo, eNetworkTypeIdx);
2253     }
2254
2255     return u4Status;
2256 }
2257
2258
2259 /* BSS-INFO Indication (PM) */
2260 /*----------------------------------------------------------------------------*/
2261 /*!
2262 * @brief This utility function is used to indicate PM that
2263 *        a BSS has been created. (for AdHoc / P2P-GO)
2264 *
2265 * @param prAdapter          Pointer of ADAPTER_T
2266 *        eNetworkTypeIdx    Index of BSS-INFO
2267 *
2268 * @retval -
2269 */
2270 /*----------------------------------------------------------------------------*/
2271 WLAN_STATUS
2272 nicPmIndicateBssCreated(
2273     IN P_ADAPTER_T prAdapter,
2274     IN ENUM_NETWORK_TYPE_INDEX_T eNetworkTypeIdx
2275     )
2276 {
2277     P_BSS_INFO_T prBssInfo;
2278     CMD_INDICATE_PM_BSS_CREATED rCmdIndicatePmBssCreated;
2279
2280     ASSERT(prAdapter);
2281     ASSERT(eNetworkTypeIdx < NETWORK_TYPE_INDEX_NUM);
2282
2283     prBssInfo = &(prAdapter->rWifiVar.arBssInfo[eNetworkTypeIdx]);
2284
2285     rCmdIndicatePmBssCreated.ucNetTypeIndex     = (UINT_8)eNetworkTypeIdx;
2286     rCmdIndicatePmBssCreated.ucDtimPeriod       = prBssInfo->ucDTIMPeriod;
2287     rCmdIndicatePmBssCreated.u2BeaconInterval   = prBssInfo->u2BeaconInterval;
2288     rCmdIndicatePmBssCreated.u2AtimWindow       = prBssInfo->u2ATIMWindow;
2289
2290     return wlanSendSetQueryCmd(prAdapter,
2291             CMD_ID_INDICATE_PM_BSS_CREATED,
2292             TRUE,
2293             FALSE,
2294             FALSE,
2295             NULL,
2296             NULL,
2297             sizeof(CMD_INDICATE_PM_BSS_CREATED),
2298             (PUINT_8)&rCmdIndicatePmBssCreated,
2299             NULL,
2300             0);
2301 }
2302
2303
2304 /*----------------------------------------------------------------------------*/
2305 /*!
2306 * @brief This utility function is used to indicate PM that
2307 *        a BSS has been connected
2308 *
2309 * @param prAdapter          Pointer of ADAPTER_T
2310 *        eNetworkTypeIdx    Index of BSS-INFO
2311 *
2312 * @retval -
2313 */
2314 /*----------------------------------------------------------------------------*/
2315 WLAN_STATUS
2316 nicPmIndicateBssConnected(
2317     IN P_ADAPTER_T prAdapter,
2318     IN ENUM_NETWORK_TYPE_INDEX_T eNetworkTypeIdx
2319     )
2320 {
2321     P_BSS_INFO_T prBssInfo;
2322     CMD_INDICATE_PM_BSS_CONNECTED rCmdIndicatePmBssConnected;
2323
2324     ASSERT(prAdapter);
2325     ASSERT(eNetworkTypeIdx < NETWORK_TYPE_INDEX_NUM);
2326
2327     prBssInfo = &(prAdapter->rWifiVar.arBssInfo[eNetworkTypeIdx]);
2328
2329     rCmdIndicatePmBssConnected.ucNetTypeIndex   = (UINT_8)eNetworkTypeIdx;
2330     rCmdIndicatePmBssConnected.ucDtimPeriod     = prBssInfo->ucDTIMPeriod;
2331     rCmdIndicatePmBssConnected.u2AssocId        = prBssInfo->u2AssocId;
2332     rCmdIndicatePmBssConnected.u2BeaconInterval = prBssInfo->u2BeaconInterval;
2333     rCmdIndicatePmBssConnected.u2AtimWindow     = prBssInfo->u2ATIMWindow;
2334
2335     rCmdIndicatePmBssConnected.ucBmpDeliveryAC  = prBssInfo->rPmProfSetupInfo.ucBmpDeliveryAC;
2336     rCmdIndicatePmBssConnected.ucBmpTriggerAC   = prBssInfo->rPmProfSetupInfo.ucBmpTriggerAC;
2337
2338     //DBGPRINTF("nicPmIndicateBssConnected: ucBmpDeliveryAC:0x%x, ucBmpTriggerAC:0x%x",
2339             //rCmdIndicatePmBssConnected.ucBmpDeliveryAC,
2340             //rCmdIndicatePmBssConnected.ucBmpTriggerAC);
2341
2342     if ((eNetworkTypeIdx == NETWORK_TYPE_AIS_INDEX)
2343 #if CFG_ENABLE_WIFI_DIRECT
2344         || ((eNetworkTypeIdx == NETWORK_TYPE_P2P_INDEX) && (prAdapter->fgIsP2PRegistered))
2345 #endif
2346          ) {
2347         if(prBssInfo->eCurrentOPMode == OP_MODE_INFRASTRUCTURE) {
2348             rCmdIndicatePmBssConnected.fgIsUapsdConnection  = (UINT_8)prBssInfo->prStaRecOfAP->fgIsUapsdSupported;
2349         }
2350         else {
2351             rCmdIndicatePmBssConnected.fgIsUapsdConnection  = 0; //@FIXME
2352         }
2353     }
2354     else {
2355         rCmdIndicatePmBssConnected.fgIsUapsdConnection  = 0;
2356     }
2357
2358     return wlanSendSetQueryCmd(prAdapter,
2359             CMD_ID_INDICATE_PM_BSS_CONNECTED,
2360             TRUE,
2361             FALSE,
2362             FALSE,
2363             NULL,
2364             NULL,
2365             sizeof(CMD_INDICATE_PM_BSS_CONNECTED),
2366             (PUINT_8)&rCmdIndicatePmBssConnected,
2367             NULL,
2368             0);
2369 }
2370
2371
2372 /*----------------------------------------------------------------------------*/
2373 /*!
2374 * @brief This utility function is used to indicate PM that
2375 *        a BSS has been disconnected
2376 *
2377 * @param prAdapter          Pointer of ADAPTER_T
2378 *        eNetworkTypeIdx    Index of BSS-INFO
2379 *
2380 * @retval -
2381 */
2382 /*----------------------------------------------------------------------------*/
2383 WLAN_STATUS
2384 nicPmIndicateBssAbort(
2385     IN P_ADAPTER_T prAdapter,
2386     IN ENUM_NETWORK_TYPE_INDEX_T eNetworkTypeIdx
2387     )
2388 {
2389     CMD_INDICATE_PM_BSS_ABORT rCmdIndicatePmBssAbort;
2390
2391     ASSERT(prAdapter);
2392     ASSERT(eNetworkTypeIdx < NETWORK_TYPE_INDEX_NUM);
2393
2394     rCmdIndicatePmBssAbort.ucNetTypeIndex       = (UINT_8)eNetworkTypeIdx;
2395
2396     return wlanSendSetQueryCmd(prAdapter,
2397             CMD_ID_INDICATE_PM_BSS_ABORT,
2398             TRUE,
2399             FALSE,
2400             FALSE,
2401             NULL,
2402             NULL,
2403             sizeof(CMD_INDICATE_PM_BSS_ABORT),
2404             (PUINT_8)&rCmdIndicatePmBssAbort,
2405             NULL,
2406             0);
2407 }
2408
2409 WLAN_STATUS
2410 nicConfigPowerSaveProfile (
2411     IN  P_ADAPTER_T prAdapter,
2412     ENUM_NETWORK_TYPE_INDEX_T eNetTypeIndex,
2413     PARAM_POWER_MODE ePwrMode,
2414     BOOLEAN fgEnCmdEvent
2415     )
2416 {
2417     DEBUGFUNC("nicConfigPowerSaveProfile");
2418     DBGLOG(INIT, TRACE, ("eNetTypeIndex:%d, ePwrMode:%d, fgEnCmdEvent:%d\n",
2419                     eNetTypeIndex, ePwrMode, fgEnCmdEvent));
2420
2421     ASSERT(prAdapter);
2422
2423         if (eNetTypeIndex >= NETWORK_TYPE_INDEX_NUM) {
2424                 ASSERT(0);
2425                 return WLAN_STATUS_NOT_SUPPORTED;
2426         }
2427
2428 //    prAdapter->rWlanInfo.ePowerSaveMode.ucNetTypeIndex = eNetTypeIndex;
2429 //    prAdapter->rWlanInfo.ePowerSaveMode.ucPsProfile = (UINT_8)ePwrMode;
2430     prAdapter->rWlanInfo.arPowerSaveMode[eNetTypeIndex].ucNetTypeIndex = eNetTypeIndex;
2431     prAdapter->rWlanInfo.arPowerSaveMode[eNetTypeIndex].ucPsProfile = (UINT_8)ePwrMode;
2432
2433     return wlanSendSetQueryCmd(prAdapter,
2434             CMD_ID_POWER_SAVE_MODE,
2435             TRUE,
2436             FALSE,
2437             (fgEnCmdEvent ? TRUE : FALSE),
2438             (fgEnCmdEvent ? nicCmdEventSetCommon : NULL),
2439             (fgEnCmdEvent ? nicOidCmdTimeoutCommon : NULL),
2440             sizeof(CMD_PS_PROFILE_T),
2441             (PUINT_8)&(prAdapter->rWlanInfo.arPowerSaveMode[eNetTypeIndex]),
2442             NULL,
2443             sizeof(PARAM_POWER_MODE)
2444             );
2445
2446 } /* end of wlanoidSetAcpiDevicePowerStateMode() */
2447
2448 WLAN_STATUS
2449 nicEnterCtiaMode (
2450     IN  P_ADAPTER_T prAdapter,
2451     BOOLEAN fgEnterCtia,
2452     BOOLEAN fgEnCmdEvent
2453     )
2454 {
2455     CMD_SW_DBG_CTRL_T rCmdSwCtrl;
2456     CMD_ACCESS_REG rCmdAccessReg;
2457     WLAN_STATUS rWlanStatus;
2458
2459     DEBUGFUNC("nicEnterCtiaMode");
2460     DBGLOG(INIT, TRACE, ("nicEnterCtiaMode: %d\n", fgEnterCtia));
2461
2462     ASSERT(prAdapter);
2463
2464     rWlanStatus = WLAN_STATUS_SUCCESS;
2465
2466     if (fgEnterCtia) {
2467         // 1. Disable On-Lin Scan
2468         prAdapter->fgEnOnlineScan = FALSE;
2469
2470         // 3. Disable FIFO FULL no ack
2471         rCmdAccessReg.u4Address = 0x60140028;
2472         rCmdAccessReg.u4Data = 0x904;
2473         wlanSendSetQueryCmd(prAdapter,
2474             CMD_ID_ACCESS_REG,
2475             TRUE, //FALSE,
2476             FALSE, //TRUE,
2477             FALSE,
2478             NULL,
2479             NULL,
2480             sizeof(CMD_ACCESS_REG),
2481             (PUINT_8)&rCmdAccessReg,
2482             NULL,
2483             0
2484             );
2485
2486         // 4. Disable Roaming
2487         rCmdSwCtrl.u4Id = 0x90000204;
2488             rCmdSwCtrl.u4Data = 0x0;
2489             wlanSendSetQueryCmd(prAdapter,
2490                 CMD_ID_SW_DBG_CTRL,
2491                 TRUE,
2492                 FALSE,
2493                 FALSE,
2494                 NULL,
2495                 NULL,
2496                 sizeof(CMD_SW_DBG_CTRL_T),
2497                 (PUINT_8)&rCmdSwCtrl,
2498                 NULL,
2499                 0
2500             );
2501
2502         rCmdSwCtrl.u4Id = 0x90000200;
2503         rCmdSwCtrl.u4Data = 0x820000;
2504         wlanSendSetQueryCmd(prAdapter,
2505             CMD_ID_SW_DBG_CTRL,
2506             TRUE,
2507             FALSE,
2508             FALSE,
2509             NULL,
2510             NULL,
2511             sizeof(CMD_SW_DBG_CTRL_T),
2512             (PUINT_8)&rCmdSwCtrl,
2513             NULL,
2514             0
2515             );
2516
2517         // Disalbe auto tx power        
2518         rCmdSwCtrl.u4Id = 0xa0100003;
2519         rCmdSwCtrl.u4Data = 0x0;
2520         wlanSendSetQueryCmd(prAdapter,
2521              CMD_ID_SW_DBG_CTRL,
2522              TRUE,
2523              FALSE,
2524              FALSE,
2525              NULL,
2526              NULL,
2527              sizeof(CMD_SW_DBG_CTRL_T),
2528              (PUINT_8)&rCmdSwCtrl,
2529              NULL,
2530              0
2531              );
2532
2533         // 2. Keep at CAM mode
2534         {
2535             PARAM_POWER_MODE ePowerMode;
2536
2537             prAdapter->u4CtiaPowerMode = 0;
2538             prAdapter->fgEnCtiaPowerMode = TRUE;
2539
2540             ePowerMode = Param_PowerModeCAM;
2541             rWlanStatus = nicConfigPowerSaveProfile(
2542                    prAdapter,
2543                    NETWORK_TYPE_AIS_INDEX,
2544                    ePowerMode,
2545                    fgEnCmdEvent);
2546         }
2547
2548         // 5. Disable Beacon Timeout Detection
2549         prAdapter->fgDisBcnLostDetection = TRUE;    
2550     }
2551     else {
2552         // 1. Enaable On-Lin Scan
2553         prAdapter->fgEnOnlineScan = TRUE;
2554
2555         // 3. Enable FIFO FULL no ack
2556         rCmdAccessReg.u4Address = 0x60140028;
2557         rCmdAccessReg.u4Data = 0x905;
2558         wlanSendSetQueryCmd(prAdapter,
2559             CMD_ID_ACCESS_REG,
2560             TRUE, //FALSE,
2561             FALSE, //TRUE,
2562             FALSE,
2563             NULL,
2564             NULL,
2565             sizeof(CMD_ACCESS_REG),
2566             (PUINT_8)&rCmdAccessReg,
2567             NULL,
2568             0
2569         );
2570
2571         // 4. Enable Roaming
2572         rCmdSwCtrl.u4Id = 0x90000204;
2573         rCmdSwCtrl.u4Data = 0x1;
2574         wlanSendSetQueryCmd(prAdapter,
2575             CMD_ID_SW_DBG_CTRL,
2576             TRUE,
2577             FALSE,
2578             FALSE,
2579             NULL,
2580             NULL,
2581             sizeof(CMD_SW_DBG_CTRL_T),
2582             (PUINT_8)&rCmdSwCtrl,
2583             NULL,
2584             0
2585         );
2586
2587         rCmdSwCtrl.u4Id = 0x90000200;
2588         rCmdSwCtrl.u4Data = 0x820000;
2589         wlanSendSetQueryCmd(prAdapter,
2590             CMD_ID_SW_DBG_CTRL,
2591             TRUE,
2592             FALSE,
2593             FALSE,
2594             NULL,
2595             NULL,
2596             sizeof(CMD_SW_DBG_CTRL_T),
2597             (PUINT_8)&rCmdSwCtrl,
2598             NULL,
2599             0
2600         );
2601
2602         // Enable auto tx power
2603         //
2604
2605         rCmdSwCtrl.u4Id = 0xa0100003;
2606         rCmdSwCtrl.u4Data = 0x1;
2607         wlanSendSetQueryCmd(prAdapter,
2608             CMD_ID_SW_DBG_CTRL,
2609             TRUE,
2610             FALSE,
2611             FALSE,
2612             NULL,
2613             NULL,
2614             sizeof(CMD_SW_DBG_CTRL_T),
2615             (PUINT_8)&rCmdSwCtrl,
2616             NULL,
2617             0
2618         );
2619
2620
2621         // 2. Keep at Fast PS
2622         {
2623             PARAM_POWER_MODE ePowerMode;
2624
2625             prAdapter->u4CtiaPowerMode = 2;
2626             prAdapter->fgEnCtiaPowerMode = TRUE;
2627
2628             ePowerMode = Param_PowerModeFast_PSP;
2629             rWlanStatus = nicConfigPowerSaveProfile(
2630                 prAdapter,
2631                 NETWORK_TYPE_AIS_INDEX,
2632                 ePowerMode,
2633                 fgEnCmdEvent);
2634             }
2635
2636         // 5. Enable Beacon Timeout Detection
2637         prAdapter->fgDisBcnLostDetection = FALSE;
2638     
2639     }
2640     
2641     return rWlanStatus;
2642 } /* end of nicEnterCtiaMode() */
2643
2644 /*----------------------------------------------------------------------------*/
2645 /*!
2646 * @brief This utility function is used to indicate firmware domain
2647 *        for beacon generation parameters
2648 *
2649 * @param prAdapter          Pointer of ADAPTER_T
2650 *        eIeUpdMethod,      Update Method
2651 *        eNetTypeIndex      Index of Network
2652 *        u2Capability       Capability
2653 *        aucIe              Pointer to buffer of IEs
2654 *        u2IELen            Length of IEs
2655 *
2656 * @retval - WLAN_STATUS_SUCCESS
2657 *           WLAN_STATUS_FAILURE
2658 *           WLAN_STATUS_PENDING
2659 *           WLAN_STATUS_INVALID_DATA
2660 */
2661 /*----------------------------------------------------------------------------*/
2662 WLAN_STATUS
2663 nicUpdateBeaconIETemplate (
2664     IN P_ADAPTER_T prAdapter,
2665     IN ENUM_IE_UPD_METHOD_T eIeUpdMethod,
2666     IN ENUM_NETWORK_TYPE_INDEX_T eNetTypeIndex,
2667     IN UINT_16 u2Capability,
2668     IN PUINT_8 aucIe,
2669     IN UINT_16 u2IELen
2670     )
2671 {
2672     P_CMD_BEACON_TEMPLATE_UPDATE prCmdBcnUpdate;
2673     UINT_16 u2CmdBufLen = 0;
2674     P_GLUE_INFO_T prGlueInfo;
2675     P_CMD_INFO_T prCmdInfo;
2676     P_WIFI_CMD_T prWifiCmd;
2677     UINT_8 ucCmdSeqNum;
2678
2679     DEBUGFUNC("wlanUpdateBeaconIETemplate");
2680     DBGLOG(INIT, LOUD, ("\n"));
2681
2682         printk("nicUpdateBeaconIETemplate\n");
2683
2684     ASSERT(prAdapter);
2685     prGlueInfo = prAdapter->prGlueInfo;
2686
2687     if (u2IELen > MAX_IE_LENGTH) {
2688         return WLAN_STATUS_INVALID_DATA;
2689     }
2690
2691     if (eIeUpdMethod == IE_UPD_METHOD_UPDATE_RANDOM
2692             || eIeUpdMethod == IE_UPD_METHOD_UPDATE_ALL) {
2693         u2CmdBufLen = OFFSET_OF(CMD_BEACON_TEMPLATE_UPDATE, aucIE) + u2IELen;
2694     }
2695     else if (eIeUpdMethod == IE_UPD_METHOD_DELETE_ALL) {
2696         u2CmdBufLen = OFFSET_OF(CMD_BEACON_TEMPLATE_UPDATE, u2IELen);
2697     }
2698     else {
2699         ASSERT(0);
2700         return WLAN_STATUS_FAILURE;
2701     }
2702
2703     // prepare command info
2704     prCmdInfo = cmdBufAllocateCmdInfo(prAdapter, (CMD_HDR_SIZE + u2CmdBufLen));
2705     if (!prCmdInfo) {
2706         DBGLOG(INIT, ERROR, ("Allocate CMD_INFO_T ==> FAILED.\n"));
2707                 printk("Allocate CMD_INFO_T ==> FAILED.\n");
2708         return WLAN_STATUS_FAILURE;
2709     }
2710
2711     // increase command sequence number
2712     ucCmdSeqNum = nicIncreaseCmdSeqNum(prAdapter);
2713     DBGLOG(REQ, TRACE, ("ucCmdSeqNum =%d\n", ucCmdSeqNum));
2714
2715     // Setup common CMD Info Packet
2716     prCmdInfo->eCmdType = COMMAND_TYPE_NETWORK_IOCTL;
2717     prCmdInfo->eNetworkType = eNetTypeIndex;
2718     prCmdInfo->u2InfoBufLen = (UINT_16)(CMD_HDR_SIZE + u2CmdBufLen);
2719     prCmdInfo->pfCmdDoneHandler = NULL;     //@FIXME
2720     prCmdInfo->pfCmdTimeoutHandler = NULL;  //@FIXME
2721     prCmdInfo->fgIsOid = FALSE;
2722     prCmdInfo->ucCID = CMD_ID_UPDATE_BEACON_CONTENT;
2723     prCmdInfo->fgSetQuery = TRUE;
2724     prCmdInfo->fgNeedResp = FALSE;
2725     prCmdInfo->fgDriverDomainMCR = FALSE;
2726     prCmdInfo->ucCmdSeqNum = ucCmdSeqNum;
2727     prCmdInfo->u4SetInfoLen = u2CmdBufLen;
2728     prCmdInfo->pvInformationBuffer = NULL;
2729     prCmdInfo->u4InformationBufferLength = 0;
2730
2731     // Setup WIFI_CMD_T (no payload)
2732     prWifiCmd = (P_WIFI_CMD_T)(prCmdInfo->pucInfoBuffer);
2733     prWifiCmd->u2TxByteCount_UserPriority = prCmdInfo->u2InfoBufLen;
2734     prWifiCmd->ucCID = prCmdInfo->ucCID;
2735     prWifiCmd->ucSetQuery = prCmdInfo->fgSetQuery;
2736     prWifiCmd->ucSeqNum = prCmdInfo->ucCmdSeqNum;
2737
2738     prCmdBcnUpdate = (P_CMD_BEACON_TEMPLATE_UPDATE)(prWifiCmd->aucBuffer);
2739
2740     // fill beacon updating command
2741     prCmdBcnUpdate->ucUpdateMethod  = (UINT_8) eIeUpdMethod;
2742     prCmdBcnUpdate->ucNetTypeIndex  = (UINT_8) eNetTypeIndex;
2743     prCmdBcnUpdate->u2Capability    = u2Capability;
2744     prCmdBcnUpdate->u2IELen         = u2IELen;
2745     if(u2IELen > 0 ) {
2746         kalMemCopy(prCmdBcnUpdate->aucIE, aucIe, u2IELen);
2747     }
2748
2749     // insert into prCmdQueue
2750     kalEnqueueCommand(prGlueInfo, (P_QUE_ENTRY_T)prCmdInfo);
2751
2752     // wakeup txServiceThread later
2753     GLUE_SET_EVENT(prGlueInfo);
2754     return WLAN_STATUS_PENDING;
2755 }
2756
2757
2758 /*----------------------------------------------------------------------------*/
2759 /*!
2760 * @brief This utility function is used to initialization PHY related
2761 *        varaibles
2762 *
2763 * @param prAdapter  Pointer of ADAPTER_T
2764 *
2765 * @retval none
2766 */
2767 /*----------------------------------------------------------------------------*/
2768 VOID
2769 nicSetAvailablePhyTypeSet (
2770     IN P_ADAPTER_T prAdapter
2771     )
2772 {
2773     P_CONNECTION_SETTINGS_T prConnSettings;
2774
2775     ASSERT(prAdapter);
2776
2777     prConnSettings = &(prAdapter->rWifiVar.rConnSettings);
2778
2779     if (prConnSettings->eDesiredPhyConfig >= PHY_CONFIG_NUM) {
2780         ASSERT(0);
2781         return;
2782     }
2783
2784     prAdapter->rWifiVar.ucAvailablePhyTypeSet =
2785         aucPhyCfg2PhyTypeSet[prConnSettings->eDesiredPhyConfig];
2786
2787     if (prAdapter->rWifiVar.ucAvailablePhyTypeSet & PHY_TYPE_BIT_ERP) {
2788         prAdapter->rWifiVar.eNonHTBasicPhyType2G4 = PHY_TYPE_ERP_INDEX;
2789     }
2790     /* NOTE(Kevin): Because we don't have N only mode, TBD */
2791     else /* if (ucNonHTPhyTypeSet & PHY_TYPE_HR_DSSS_INDEX) */ {
2792         prAdapter->rWifiVar.eNonHTBasicPhyType2G4 = PHY_TYPE_HR_DSSS_INDEX;
2793     }
2794
2795     return;
2796 }
2797
2798
2799 /*----------------------------------------------------------------------------*/
2800 /*!
2801 * @brief This utility function is used to update WMM Parms
2802 *
2803 * @param prAdapter          Pointer of ADAPTER_T
2804 *        eNetworkTypeIdx    Index of BSS-INFO
2805 *
2806 * @retval -
2807 */
2808 /*----------------------------------------------------------------------------*/
2809 WLAN_STATUS
2810 nicQmUpdateWmmParms(
2811     IN P_ADAPTER_T prAdapter,
2812     IN ENUM_NETWORK_TYPE_INDEX_T eNetworkTypeIdx
2813     )
2814 {
2815     P_BSS_INFO_T prBssInfo;
2816     CMD_UPDATE_WMM_PARMS_T rCmdUpdateWmmParms;
2817
2818     ASSERT(prAdapter);
2819     ASSERT(eNetworkTypeIdx < NETWORK_TYPE_INDEX_NUM);
2820
2821     DBGLOG(QM, EVENT, ("sizeof(AC_QUE_PARMS_T): %d \n", sizeof(AC_QUE_PARMS_T)));
2822     DBGLOG(QM, EVENT, ("sizeof(CMD_UPDATE_WMM_PARMS): %d \n", sizeof(CMD_UPDATE_WMM_PARMS_T)));
2823     DBGLOG(QM, EVENT, ("sizeof(WIFI_CMD_T): %d \n", sizeof(WIFI_CMD_T)));
2824
2825     prBssInfo = &(prAdapter->rWifiVar.arBssInfo[eNetworkTypeIdx]);
2826     rCmdUpdateWmmParms.ucNetTypeIndex = (UINT_8)eNetworkTypeIdx;
2827     kalMemCopy(&rCmdUpdateWmmParms.arACQueParms[0], &prBssInfo->arACQueParms[0],
2828         (sizeof(AC_QUE_PARMS_T)*AC_NUM));
2829
2830     rCmdUpdateWmmParms.fgIsQBSS = prBssInfo->fgIsQBSS;
2831
2832     return wlanSendSetQueryCmd(prAdapter,
2833             CMD_ID_UPDATE_WMM_PARMS,
2834             TRUE,
2835             FALSE,
2836             FALSE,
2837             NULL,
2838             NULL,
2839             sizeof(CMD_UPDATE_WMM_PARMS_T),
2840             (PUINT_8)&rCmdUpdateWmmParms,
2841             NULL,
2842             0);
2843 }
2844
2845
2846 /*----------------------------------------------------------------------------*/
2847 /*!
2848 * @brief This utility function is used to update TX power gain corresponding to
2849 *        each band/modulation combination
2850 *
2851 * @param prAdapter          Pointer of ADAPTER_T
2852 *        prTxPwrParam       Pointer of TX power parameters
2853 *
2854 * @retval WLAN_STATUS_PENDING
2855 *         WLAN_STATUS_FAILURE
2856 */
2857 /*----------------------------------------------------------------------------*/
2858 WLAN_STATUS
2859 nicUpdateTxPower(
2860     IN P_ADAPTER_T      prAdapter,
2861     IN P_CMD_TX_PWR_T   prTxPwrParam
2862     )
2863 {
2864     DEBUGFUNC("nicUpdateTxPower");
2865
2866     ASSERT(prAdapter);
2867
2868     return wlanSendSetQueryCmd(prAdapter,
2869             CMD_ID_SET_TX_PWR,
2870             TRUE,
2871             FALSE,
2872             FALSE,
2873             NULL,
2874             NULL,
2875             sizeof(CMD_TX_PWR_T),
2876             (PUINT_8)prTxPwrParam,
2877             NULL,
2878             0);
2879 }
2880
2881 /*----------------------------------------------------------------------------*/
2882 /*!
2883 * @brief This utility function is used to set auto tx power parameter
2884 *
2885 * @param prAdapter          Pointer of ADAPTER_T
2886 *        prTxPwrParam       Pointer of Auto TX power parameters
2887 *
2888 * @retval WLAN_STATUS_PENDING
2889 *         WLAN_STATUS_FAILURE
2890 */
2891 /*----------------------------------------------------------------------------*/
2892 WLAN_STATUS
2893 nicSetAutoTxPower(
2894     IN P_ADAPTER_T      prAdapter,
2895     IN P_CMD_AUTO_POWER_PARAM_T   prAutoPwrParam
2896     )
2897 {
2898     DEBUGFUNC("nicSetAutoTxPower");
2899
2900     ASSERT(prAdapter);
2901
2902     return wlanSendSetQueryCmd(prAdapter,
2903             CMD_ID_SET_AUTOPWR_CTRL,
2904             TRUE,
2905             FALSE,
2906             FALSE,
2907             NULL,
2908             NULL,
2909             sizeof(CMD_AUTO_POWER_PARAM_T),
2910             (PUINT_8)prAutoPwrParam,
2911             NULL,
2912             0);
2913 }
2914
2915
2916 /*----------------------------------------------------------------------------*/
2917 /*!
2918 * @brief This utility function is used to update TX power gain corresponding to
2919 *        each band/modulation combination
2920 *
2921 * @param prAdapter          Pointer of ADAPTER_T
2922 *        prTxPwrParam       Pointer of TX power parameters
2923 *
2924 * @retval WLAN_STATUS_PENDING
2925 *         WLAN_STATUS_FAILURE
2926 */
2927 /*----------------------------------------------------------------------------*/
2928 WLAN_STATUS
2929 nicSetAutoTxPowerControl(
2930     IN P_ADAPTER_T      prAdapter,
2931     IN P_CMD_TX_PWR_T   prTxPwrParam
2932     )
2933 {
2934     DEBUGFUNC("nicUpdateTxPower");
2935
2936     ASSERT(prAdapter);
2937
2938     return wlanSendSetQueryCmd(prAdapter,
2939             CMD_ID_SET_TX_PWR,
2940             TRUE,
2941             FALSE,
2942             FALSE,
2943             NULL,
2944             NULL,
2945             sizeof(CMD_TX_PWR_T),
2946             (PUINT_8)prTxPwrParam,
2947             NULL,
2948             0);
2949 }
2950
2951
2952 /*----------------------------------------------------------------------------*/
2953 /*!
2954 * @brief This utility function is used to update power offset around 5GHz band
2955 *
2956 * @param prAdapter          Pointer of ADAPTER_T
2957 *        pr5GPwrOffset      Pointer of 5GHz power offset parameter
2958 *
2959 * @retval WLAN_STATUS_PENDING
2960 *         WLAN_STATUS_FAILURE
2961 */
2962 /*----------------------------------------------------------------------------*/
2963 WLAN_STATUS
2964 nicUpdate5GOffset(
2965     IN P_ADAPTER_T              prAdapter,
2966     IN P_CMD_5G_PWR_OFFSET_T    pr5GPwrOffset
2967     )
2968 {
2969     DEBUGFUNC("nicUpdate5GOffset");
2970
2971     ASSERT(prAdapter);
2972
2973     return wlanSendSetQueryCmd(prAdapter,
2974             CMD_ID_SET_5G_PWR_OFFSET,
2975             TRUE,
2976             FALSE,
2977             FALSE,
2978             NULL,
2979             NULL,
2980             sizeof(CMD_5G_PWR_OFFSET_T),
2981             (PUINT_8)pr5GPwrOffset,
2982             NULL,
2983             0);
2984 }
2985
2986
2987 /*----------------------------------------------------------------------------*/
2988 /*!
2989 * @brief This utility function is used to update DPD calibration result
2990 *
2991 * @param prAdapter          Pointer of ADAPTER_T
2992 *        pr5GPwrOffset      Pointer of parameter for DPD calibration result
2993 *
2994 * @retval WLAN_STATUS_PENDING
2995 *         WLAN_STATUS_FAILURE
2996 */
2997 /*----------------------------------------------------------------------------*/
2998 WLAN_STATUS
2999 nicUpdateDPD(
3000     IN P_ADAPTER_T          prAdapter,
3001     IN P_CMD_PWR_PARAM_T    prDpdCalResult
3002     )
3003 {
3004     DEBUGFUNC("nicUpdateDPD");
3005
3006     ASSERT(prAdapter);
3007
3008     return wlanSendSetQueryCmd(prAdapter,
3009             CMD_ID_SET_PWR_PARAM,
3010             TRUE,
3011             FALSE,
3012             FALSE,
3013             NULL,
3014             NULL,
3015             sizeof(CMD_PWR_PARAM_T),
3016             (PUINT_8)prDpdCalResult,
3017             NULL,
3018             0);
3019 }
3020
3021
3022 /*----------------------------------------------------------------------------*/
3023 /*!
3024 * @brief This utility function starts system service such as timer and
3025 *        memory pools
3026 *
3027 * @param prAdapter          Pointer of ADAPTER_T
3028 *
3029 * @retval none
3030 */
3031 /*----------------------------------------------------------------------------*/
3032 VOID
3033 nicInitSystemService (
3034     IN P_ADAPTER_T prAdapter
3035     )
3036 {
3037     ASSERT(prAdapter);
3038
3039     // <1> Initialize MGMT Memory pool and STA_REC
3040     cnmMemInit(prAdapter);
3041     cnmStaRecInit(prAdapter);
3042     cmdBufInitialize(prAdapter);
3043
3044     // <2> Mailbox Initialization
3045     mboxInitialize(prAdapter);
3046
3047     // <3> Timer Initialization
3048     cnmTimerInitialize(prAdapter);
3049
3050     return;
3051 }
3052
3053
3054 /*----------------------------------------------------------------------------*/
3055 /*!
3056 * @brief This utility function reset some specific system service,
3057 *        such as STA-REC
3058 *
3059 * @param prAdapter          Pointer of ADAPTER_T
3060 *
3061 * @retval none
3062 */
3063 /*----------------------------------------------------------------------------*/
3064 VOID
3065 nicResetSystemService (
3066     IN P_ADAPTER_T prAdapter
3067     )
3068 {
3069     ASSERT(prAdapter);
3070 }
3071
3072
3073 /*----------------------------------------------------------------------------*/
3074 /*!
3075 * @brief This utility function is used to update WMM Parms
3076 *
3077 * @param prAdapter          Pointer of ADAPTER_T
3078 *
3079 * @retval none
3080 */
3081 /*----------------------------------------------------------------------------*/
3082 VOID
3083 nicUninitSystemService (
3084     IN P_ADAPTER_T prAdapter
3085     )
3086 {
3087     ASSERT(prAdapter);
3088
3089     /* Timer Destruction */
3090     cnmTimerDestroy(prAdapter);
3091
3092     /* Mailbox Destruction */
3093     mboxDestroy(prAdapter);
3094
3095     return;
3096 }
3097
3098
3099 /*----------------------------------------------------------------------------*/
3100 /*!
3101 * @brief This utility function is used to update WMM Parms
3102 *
3103 * @param prAdapter          Pointer of ADAPTER_T
3104 *
3105 * @retval none
3106 */
3107 /*----------------------------------------------------------------------------*/
3108 VOID
3109 nicInitMGMT (
3110     IN P_ADAPTER_T prAdapter,
3111     IN P_REG_INFO_T prRegInfo
3112     )
3113 {
3114     ASSERT(prAdapter);
3115
3116     /* CNM Module - initialization */
3117     cnmInit(prAdapter);
3118
3119     /* RLM Module - initialization */
3120     rlmFsmEventInit(prAdapter);
3121
3122     /* SCN Module - initialization */
3123     scnInit(prAdapter);
3124
3125     /* AIS Module - intiailization */
3126     aisInitializeConnectionSettings(prAdapter, prRegInfo);
3127     aisFsmInit(prAdapter);
3128
3129 #if CFG_SUPPORT_ROAMING
3130     /* Roaming Module - intiailization */
3131     roamingFsmInit(prAdapter);
3132 #endif /* CFG_SUPPORT_ROAMING */
3133
3134 #if CFG_SUPPORT_SWCR
3135     swCrDebugInit(prAdapter);
3136 #endif /* CFG_SUPPORT_SWCR */
3137
3138     return;
3139 }
3140
3141
3142 /*----------------------------------------------------------------------------*/
3143 /*!
3144 * @brief This utility function is used to update WMM Parms
3145 *
3146 * @param prAdapter          Pointer of ADAPTER_T
3147 *
3148 * @retval none
3149 */
3150 /*----------------------------------------------------------------------------*/
3151 VOID
3152 nicUninitMGMT (
3153     IN P_ADAPTER_T prAdapter
3154     )
3155 {
3156     ASSERT(prAdapter);
3157
3158 #if CFG_SUPPORT_SWCR
3159     swCrDebugUninit(prAdapter);
3160 #endif /* CFG_SUPPORT_SWCR */
3161
3162 #if CFG_SUPPORT_ROAMING
3163     /* Roaming Module - unintiailization */
3164     roamingFsmUninit(prAdapter);
3165 #endif /* CFG_SUPPORT_ROAMING */
3166
3167     /* AIS Module - unintiailization */
3168     aisFsmUninit(prAdapter);
3169
3170     /* SCN Module - unintiailization */
3171     scnUninit(prAdapter);
3172
3173     /* RLM Module - uninitialization */
3174     rlmFsmEventUninit(prAdapter);
3175
3176     /* CNM Module - uninitialization */
3177     cnmUninit(prAdapter);
3178
3179     return;
3180 }
3181
3182
3183 #if (MT6620_E1_ASIC_HIFSYS_WORKAROUND == 1)
3184 /*----------------------------------------------------------------------------*/
3185 /*!
3186 * @brief This utility function is to inform firmware to enable MCU clock gating
3187 *
3188 * @param prAdapter          Pointer of ADAPTER_T
3189 *
3190 * @retval none
3191 */
3192 /*----------------------------------------------------------------------------*/
3193 WLAN_STATUS
3194 nicEnableClockGating (
3195     IN P_ADAPTER_T prAdapter
3196     )
3197 {
3198     UINT_32 i, u4WHISR = 0;
3199
3200     ASSERT(prAdapter);
3201
3202     if(prAdapter->fgIsClockGatingEnabled == TRUE) {
3203         return WLAN_STATUS_SUCCESS;
3204     }
3205     else {
3206         nicSetSwIntr(prAdapter, REQ_GATING_ENABLE_H2D_INT);
3207
3208         i = 0;
3209         while(i < GATING_CONTROL_POLL_LIMIT) {
3210             if(kalIsCardRemoved(prAdapter->prGlueInfo) == TRUE
3211                     || fgIsBusAccessFailed == TRUE) {
3212                 return WLAN_STATUS_FAILURE;
3213             }
3214
3215             HAL_READ_INTR_STATUS(prAdapter, sizeof(UINT_32), (PUINT_8)&u4WHISR);
3216
3217             if(u4WHISR & ACK_GATING_ENABLE_D2H_INT) {
3218                 prAdapter->fgIsClockGatingEnabled = TRUE;
3219                 return WLAN_STATUS_SUCCESS;
3220             }
3221         }
3222
3223         ASSERT(0);
3224         return WLAN_STATUS_PENDING;
3225     }
3226 }
3227
3228
3229 /*----------------------------------------------------------------------------*/
3230 /*!
3231 * @brief This utility function is to inform firmware to disable MCU clock gating
3232 *
3233 * @param prAdapter          Pointer of ADAPTER_T
3234 *
3235 * @retval none
3236 */
3237 /*----------------------------------------------------------------------------*/
3238 WLAN_STATUS
3239 nicDisableClockGating (
3240     IN P_ADAPTER_T prAdapter
3241     )
3242 {
3243     UINT_32 i, u4WHISR = 0;
3244
3245     ASSERT(prAdapter);
3246
3247     if(prAdapter->fgIsClockGatingEnabled == FALSE) {
3248         return WLAN_STATUS_SUCCESS;
3249     }
3250     else {
3251         nicSetSwIntr(prAdapter, REQ_GATING_DISABLE_H2D_INT);
3252
3253         i = 0;
3254         while(i < GATING_CONTROL_POLL_LIMIT) {
3255             if(kalIsCardRemoved(prAdapter->prGlueInfo) == TRUE
3256                     || fgIsBusAccessFailed == TRUE) {
3257                 return WLAN_STATUS_FAILURE;
3258             }
3259
3260             HAL_READ_INTR_STATUS(prAdapter, sizeof(UINT_32), (PUINT_8)&u4WHISR);
3261
3262             if(u4WHISR & ACK_GATING_DISABLE_D2H_INT) {
3263                 prAdapter->fgIsClockGatingEnabled = FALSE;
3264                 return WLAN_STATUS_SUCCESS;
3265             }
3266         }
3267
3268         ASSERT(0);
3269         return WLAN_STATUS_PENDING;
3270     }
3271 }
3272 #endif
3273
3274
3275 /*----------------------------------------------------------------------------*/
3276 /*!
3277 * @brief This function is invoked to buffer scan result
3278 *
3279 * @param prAdapter          Pointer to the Adapter structure.
3280 * @param rMacAddr           BSSID
3281 * @param prSsid             Pointer to SSID
3282 * @param u4Privacy          Privacy settings (0: Open / 1: WEP/WPA/WPA2 enabled)
3283 * @param rRssi              Received Strength (-10 ~ -200 dBm)
3284 * @param eNetworkType       Network Type (a/b/g)
3285 * @param prConfiguration    Network Parameter
3286 * @param eOpMode            Infra/Ad-Hoc
3287 * @param rSupportedRates    Supported basic rates
3288 * @param u2IELength         IE Length
3289 * @param pucIEBuf           Pointer to Information Elements(IEs)
3290 *
3291 * @return (none)
3292 */
3293 /*----------------------------------------------------------------------------*/
3294 VOID
3295 nicAddScanResult (
3296     IN P_ADAPTER_T                  prAdapter,
3297     IN PARAM_MAC_ADDRESS            rMacAddr,
3298     IN P_PARAM_SSID_T               prSsid,
3299     IN UINT_32                      u4Privacy,
3300     IN PARAM_RSSI                   rRssi,
3301     IN ENUM_PARAM_NETWORK_TYPE_T    eNetworkType,
3302     IN P_PARAM_802_11_CONFIG_T      prConfiguration,
3303     IN ENUM_PARAM_OP_MODE_T         eOpMode,
3304     IN PARAM_RATES_EX               rSupportedRates,
3305     IN UINT_16                      u2IELength,
3306     IN PUINT_8                      pucIEBuf
3307     )
3308 {
3309     BOOLEAN bReplace;
3310     UINT_32 i;
3311     UINT_32 u4IdxWeakest = 0;
3312     PARAM_RSSI rWeakestRssi;
3313     UINT_32 u4BufferSize;
3314
3315     ASSERT(prAdapter);
3316
3317     rWeakestRssi = (PARAM_RSSI)INT_MAX;
3318     u4BufferSize = sizeof(prAdapter->rWlanInfo.aucScanIEBuf) / sizeof(prAdapter->rWlanInfo.aucScanIEBuf[0]);
3319
3320     bReplace = FALSE;
3321
3322     // decide to replace or add
3323     for(i = 0 ; i < prAdapter->rWlanInfo.u4ScanResultNum ; i++) {
3324         // find weakest entry && not connected one
3325         if(UNEQUAL_MAC_ADDR(prAdapter->rWlanInfo.arScanResult[i].arMacAddress, prAdapter->rWlanInfo.rCurrBssId.arMacAddress)
3326                 && prAdapter->rWlanInfo.arScanResult[i].rRssi < rWeakestRssi) {
3327             u4IdxWeakest = i;
3328             rWeakestRssi = prAdapter->rWlanInfo.arScanResult[i].rRssi;
3329         }
3330
3331         if(prAdapter->rWlanInfo.arScanResult[i].eOpMode == eOpMode &&
3332                 EQUAL_MAC_ADDR(&(prAdapter->rWlanInfo.arScanResult[i].arMacAddress), rMacAddr) &&
3333                     (EQUAL_SSID(prAdapter->rWlanInfo.arScanResult[i].rSsid.aucSsid,
3334                             prAdapter->rWlanInfo.arScanResult[i].rSsid.u4SsidLen,
3335                             prSsid->aucSsid,
3336                             prSsid->u4SsidLen)
3337                      || prAdapter->rWlanInfo.arScanResult[i].rSsid.u4SsidLen == 0)) {
3338             // replace entry
3339             bReplace = TRUE;
3340
3341             // free IE buffer then zero
3342             nicFreeScanResultIE(prAdapter, i);
3343             kalMemZero(&(prAdapter->rWlanInfo.arScanResult[i]), OFFSET_OF(PARAM_BSSID_EX_T, aucIEs));
3344
3345             // then fill buffer
3346             prAdapter->rWlanInfo.arScanResult[i].u4Length =
3347                 OFFSET_OF(PARAM_BSSID_EX_T, aucIEs) + u2IELength;
3348             COPY_MAC_ADDR(prAdapter->rWlanInfo.arScanResult[i].arMacAddress, rMacAddr);
3349             COPY_SSID(prAdapter->rWlanInfo.arScanResult[i].rSsid.aucSsid,
3350                     prAdapter->rWlanInfo.arScanResult[i].rSsid.u4SsidLen,
3351                     prSsid->aucSsid,
3352                     prSsid->u4SsidLen);
3353             prAdapter->rWlanInfo.arScanResult[i].u4Privacy = u4Privacy;
3354             prAdapter->rWlanInfo.arScanResult[i].rRssi = rRssi;
3355             prAdapter->rWlanInfo.arScanResult[i].eNetworkTypeInUse = eNetworkType;
3356             kalMemCopy(&(prAdapter->rWlanInfo.arScanResult[i].rConfiguration),
3357                     prConfiguration,
3358                     sizeof(PARAM_802_11_CONFIG_T));
3359             prAdapter->rWlanInfo.arScanResult[i].eOpMode = eOpMode;
3360             kalMemCopy((prAdapter->rWlanInfo.arScanResult[i].rSupportedRates),
3361                     rSupportedRates,
3362                     sizeof(PARAM_RATES_EX));
3363             prAdapter->rWlanInfo.arScanResult[i].u4IELength = (UINT_32)u2IELength;
3364
3365             // IE - allocate buffer and update pointer
3366             if(u2IELength > 0) {
3367                 if(ALIGN_4(u2IELength) + prAdapter->rWlanInfo.u4ScanIEBufferUsage <= u4BufferSize) {
3368                     kalMemCopy(&(prAdapter->rWlanInfo.aucScanIEBuf[prAdapter->rWlanInfo.u4ScanIEBufferUsage]),
3369                             pucIEBuf,
3370                             u2IELength);
3371
3372                     prAdapter->rWlanInfo.apucScanResultIEs[i] =
3373                         &(prAdapter->rWlanInfo.aucScanIEBuf[prAdapter->rWlanInfo.u4ScanIEBufferUsage]);
3374
3375                     prAdapter->rWlanInfo.u4ScanIEBufferUsage += ALIGN_4(u2IELength);
3376                 }
3377                 else {
3378                     // buffer is not enough
3379                     prAdapter->rWlanInfo.arScanResult[i].u4Length -= u2IELength;
3380                     prAdapter->rWlanInfo.arScanResult[i].u4IELength = 0;
3381                     prAdapter->rWlanInfo.apucScanResultIEs[i] = NULL;
3382                 }
3383             }
3384             else {
3385                 prAdapter->rWlanInfo.apucScanResultIEs[i] = NULL;
3386             }
3387
3388             break;
3389         }
3390     }
3391
3392     if (bReplace == FALSE) {
3393         if (prAdapter->rWlanInfo.u4ScanResultNum < (CFG_MAX_NUM_BSS_LIST - 1)) {
3394             i = prAdapter->rWlanInfo.u4ScanResultNum;
3395
3396             // zero
3397             kalMemZero(&(prAdapter->rWlanInfo.arScanResult[i]),
3398                     OFFSET_OF(PARAM_BSSID_EX_T, aucIEs));
3399
3400             // then fill buffer
3401             prAdapter->rWlanInfo.arScanResult[i].u4Length =
3402                 OFFSET_OF(PARAM_BSSID_EX_T, aucIEs) + u2IELength;
3403             COPY_MAC_ADDR(prAdapter->rWlanInfo.arScanResult[i].arMacAddress, rMacAddr);
3404             COPY_SSID(prAdapter->rWlanInfo.arScanResult[i].rSsid.aucSsid,
3405                     prAdapter->rWlanInfo.arScanResult[i].rSsid.u4SsidLen,
3406                     prSsid->aucSsid,
3407                     prSsid->u4SsidLen);
3408             prAdapter->rWlanInfo.arScanResult[i].u4Privacy = u4Privacy;
3409             prAdapter->rWlanInfo.arScanResult[i].rRssi = rRssi;
3410             prAdapter->rWlanInfo.arScanResult[i].eNetworkTypeInUse = eNetworkType;
3411             kalMemCopy(&(prAdapter->rWlanInfo.arScanResult[i].rConfiguration),
3412                     prConfiguration,
3413                     sizeof(PARAM_802_11_CONFIG_T));
3414             prAdapter->rWlanInfo.arScanResult[i].eOpMode = eOpMode;
3415             kalMemCopy((prAdapter->rWlanInfo.arScanResult[i].rSupportedRates),
3416                     rSupportedRates,
3417                     sizeof(PARAM_RATES_EX));
3418             prAdapter->rWlanInfo.arScanResult[i].u4IELength = (UINT_32)u2IELength;
3419
3420             // IE - allocate buffer and update pointer
3421             if(u2IELength > 0) {
3422                 if(ALIGN_4(u2IELength) + prAdapter->rWlanInfo.u4ScanIEBufferUsage <= u4BufferSize) {
3423                     kalMemCopy(&(prAdapter->rWlanInfo.aucScanIEBuf[prAdapter->rWlanInfo.u4ScanIEBufferUsage]),
3424                             pucIEBuf,
3425                             u2IELength);
3426
3427                     prAdapter->rWlanInfo.apucScanResultIEs[i] =
3428                         &(prAdapter->rWlanInfo.aucScanIEBuf[prAdapter->rWlanInfo.u4ScanIEBufferUsage]);
3429
3430                     prAdapter->rWlanInfo.u4ScanIEBufferUsage += ALIGN_4(u2IELength);
3431                 }
3432                 else {
3433                     // buffer is not enough
3434                     prAdapter->rWlanInfo.arScanResult[i].u4Length -= u2IELength;
3435                     prAdapter->rWlanInfo.arScanResult[i].u4IELength = 0;
3436                     prAdapter->rWlanInfo.apucScanResultIEs[i] = NULL;
3437                 }
3438             }
3439             else {
3440                 prAdapter->rWlanInfo.apucScanResultIEs[i] = NULL;
3441             }
3442
3443             prAdapter->rWlanInfo.u4ScanResultNum++;
3444         }
3445         else if(rWeakestRssi != (PARAM_RSSI)INT_MAX) {
3446             // replace weakest one
3447             i = u4IdxWeakest;
3448
3449             // free IE buffer then zero
3450             nicFreeScanResultIE(prAdapter, i);
3451             kalMemZero(&(prAdapter->rWlanInfo.arScanResult[i]),
3452                     OFFSET_OF(PARAM_BSSID_EX_T, aucIEs));
3453
3454             // then fill buffer
3455             prAdapter->rWlanInfo.arScanResult[i].u4Length =
3456                 OFFSET_OF(PARAM_BSSID_EX_T, aucIEs) + u2IELength;
3457             COPY_MAC_ADDR(prAdapter->rWlanInfo.arScanResult[i].arMacAddress, rMacAddr);
3458             COPY_SSID(prAdapter->rWlanInfo.arScanResult[i].rSsid.aucSsid,
3459                     prAdapter->rWlanInfo.arScanResult[i].rSsid.u4SsidLen,
3460                     prSsid->aucSsid,
3461                     prSsid->u4SsidLen);
3462             prAdapter->rWlanInfo.arScanResult[i].u4Privacy = u4Privacy;
3463             prAdapter->rWlanInfo.arScanResult[i].rRssi = rRssi;
3464             prAdapter->rWlanInfo.arScanResult[i].eNetworkTypeInUse = eNetworkType;
3465             kalMemCopy(&(prAdapter->rWlanInfo.arScanResult[i].rConfiguration),
3466                     prConfiguration,
3467                     sizeof(PARAM_802_11_CONFIG_T));
3468             prAdapter->rWlanInfo.arScanResult[i].eOpMode = eOpMode;
3469             kalMemCopy((prAdapter->rWlanInfo.arScanResult[i].rSupportedRates),
3470                     rSupportedRates,
3471                     sizeof(PARAM_RATES_EX));
3472             prAdapter->rWlanInfo.arScanResult[i].u4IELength = (UINT_32)u2IELength;
3473
3474             if(u2IELength > 0) {
3475                 // IE - allocate buffer and update pointer
3476                 if(ALIGN_4(u2IELength) + prAdapter->rWlanInfo.u4ScanIEBufferUsage <= u4BufferSize) {
3477                     kalMemCopy(&(prAdapter->rWlanInfo.aucScanIEBuf[prAdapter->rWlanInfo.u4ScanIEBufferUsage]),
3478                             pucIEBuf,
3479                             u2IELength);
3480
3481                     prAdapter->rWlanInfo.apucScanResultIEs[i] =
3482                         &(prAdapter->rWlanInfo.aucScanIEBuf[prAdapter->rWlanInfo.u4ScanIEBufferUsage]);
3483
3484                     prAdapter->rWlanInfo.u4ScanIEBufferUsage += ALIGN_4(u2IELength);
3485                 }
3486                 else {
3487                     // buffer is not enough
3488                     prAdapter->rWlanInfo.arScanResult[i].u4Length -= u2IELength;
3489                     prAdapter->rWlanInfo.arScanResult[i].u4IELength = 0;
3490                     prAdapter->rWlanInfo.apucScanResultIEs[i] = NULL;
3491                 }
3492             }
3493             else {
3494                 prAdapter->rWlanInfo.apucScanResultIEs[i] = NULL;
3495             }
3496         }
3497     }
3498 }
3499
3500
3501 /*----------------------------------------------------------------------------*/
3502 /*!
3503 * @brief This function is invoked to free IE buffer for dedicated scan result
3504 *
3505 * @param prAdapter          Pointer to the Adapter structure.
3506 * @param u4Idx              Index of Scan Result
3507 *
3508 * @return (none)
3509 */
3510 /*----------------------------------------------------------------------------*/
3511 VOID
3512 nicFreeScanResultIE (
3513     IN P_ADAPTER_T  prAdapter,
3514     IN UINT_32      u4Idx
3515     )
3516 {
3517     UINT_32 i;
3518     PUINT_8 pucPivot, pucMovePivot;
3519     UINT_32 u4MoveSize, u4FreeSize, u4ReserveSize;
3520
3521     ASSERT(prAdapter);
3522     ASSERT(u4Idx < CFG_MAX_NUM_BSS_LIST);
3523
3524     if(prAdapter->rWlanInfo.arScanResult[u4Idx].u4IELength == 0
3525             || prAdapter->rWlanInfo.apucScanResultIEs[u4Idx] == NULL) {
3526         return;
3527     }
3528
3529     u4FreeSize = ALIGN_4(prAdapter->rWlanInfo.arScanResult[u4Idx].u4IELength);
3530
3531     pucPivot = prAdapter->rWlanInfo.apucScanResultIEs[u4Idx];
3532     pucMovePivot = (PUINT_8)((UINT_32)(prAdapter->rWlanInfo.apucScanResultIEs[u4Idx]) + u4FreeSize);
3533
3534     u4ReserveSize = ((UINT_32)pucPivot) - (UINT_32)(&(prAdapter->rWlanInfo.aucScanIEBuf[0]));
3535     u4MoveSize = prAdapter->rWlanInfo.u4ScanIEBufferUsage - u4ReserveSize - u4FreeSize;
3536
3537     // 1. rest of buffer to move forward
3538     kalMemCopy(pucPivot, pucMovePivot, u4MoveSize);
3539
3540     // 1.1 modify pointers
3541     for(i = 0 ; i < prAdapter->rWlanInfo.u4ScanResultNum ; i++) {
3542         if(i != u4Idx) {
3543             if(prAdapter->rWlanInfo.apucScanResultIEs[i] >= pucMovePivot) {
3544                 prAdapter->rWlanInfo.apucScanResultIEs[i] =
3545                     (PUINT_8)((UINT_32)(prAdapter->rWlanInfo.apucScanResultIEs[i]) - u4FreeSize);
3546             }
3547         }
3548     }
3549
3550     // 1.2 reset the freed one
3551     prAdapter->rWlanInfo.arScanResult[u4Idx].u4IELength = 0;
3552     prAdapter->rWlanInfo.apucScanResultIEs[i] = NULL;
3553
3554     // 2. reduce IE buffer usage
3555     prAdapter->rWlanInfo.u4ScanIEBufferUsage -= u4FreeSize;
3556
3557     return;
3558 }
3559
3560
3561 /*----------------------------------------------------------------------------*/
3562 /*!
3563 * @brief This function is to hack parameters for WLAN TABLE for
3564 *        fixed rate settings
3565 *
3566 * @param prAdapter          Pointer to the Adapter structure.
3567 * @param eRateSetting
3568 * @param pu2DesiredNonHTRateSet,
3569 * @param pu2BSSBasicRateSet,
3570 * @param pucMcsSet
3571 * @param pucSupMcs32
3572 * @param pu2HtCapInfo
3573 *
3574 * @return WLAN_STATUS_SUCCESS
3575 */
3576 /*----------------------------------------------------------------------------*/
3577 WLAN_STATUS
3578 nicUpdateRateParams (
3579     IN P_ADAPTER_T                  prAdapter,
3580     IN ENUM_REGISTRY_FIXED_RATE_T   eRateSetting,
3581     IN PUINT_8                      pucDesiredPhyTypeSet,
3582     IN PUINT_16                     pu2DesiredNonHTRateSet,
3583     IN PUINT_16                     pu2BSSBasicRateSet,
3584     IN PUINT_8                      pucMcsSet,
3585     IN PUINT_8                      pucSupMcs32,
3586     IN PUINT_16                     pu2HtCapInfo
3587     )
3588 {
3589     ASSERT(prAdapter);
3590     ASSERT(eRateSetting > FIXED_RATE_NONE && eRateSetting < FIXED_RATE_NUM);
3591
3592     switch(prAdapter->rWifiVar.eRateSetting) {
3593     case FIXED_RATE_1M:
3594         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HR_DSSS;
3595         *pu2DesiredNonHTRateSet = RATE_SET_BIT_1M;
3596         *pu2BSSBasicRateSet = RATE_SET_BIT_1M;
3597         *pucMcsSet = 0;
3598         *pucSupMcs32 = 0;
3599         *pu2HtCapInfo = 0;
3600         break;
3601
3602     case FIXED_RATE_2M:
3603         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HR_DSSS;
3604         *pu2DesiredNonHTRateSet = RATE_SET_BIT_2M;
3605         *pu2BSSBasicRateSet = RATE_SET_BIT_2M;
3606         *pucMcsSet = 0;
3607         *pucSupMcs32 = 0;
3608         *pu2HtCapInfo = 0;
3609         break;
3610
3611     case FIXED_RATE_5_5M:
3612         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HR_DSSS;
3613         *pu2DesiredNonHTRateSet = RATE_SET_BIT_5_5M;
3614         *pu2BSSBasicRateSet = RATE_SET_BIT_5_5M;
3615         *pucMcsSet = 0;
3616         *pucSupMcs32 = 0;
3617         *pu2HtCapInfo = 0;
3618         break;
3619
3620     case FIXED_RATE_11M:
3621         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HR_DSSS;
3622         *pu2DesiredNonHTRateSet = RATE_SET_BIT_11M;
3623         *pu2BSSBasicRateSet = RATE_SET_BIT_11M;
3624         *pucMcsSet = 0;
3625         *pucSupMcs32 = 0;
3626         *pu2HtCapInfo = 0;
3627         break;
3628
3629     case FIXED_RATE_6M:
3630         if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_ERP) {
3631             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_ERP;
3632         }
3633         else if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_OFDM) {
3634             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_OFDM;
3635         }
3636
3637         *pu2DesiredNonHTRateSet = RATE_SET_BIT_6M;
3638         *pu2BSSBasicRateSet = RATE_SET_BIT_6M;
3639         *pucMcsSet = 0;
3640         *pucSupMcs32 = 0;
3641         *pu2HtCapInfo = 0;
3642         break;
3643
3644     case FIXED_RATE_9M:
3645         if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_ERP) {
3646             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_ERP;
3647         }
3648         else if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_OFDM) {
3649             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_OFDM;
3650         }
3651
3652         *pu2DesiredNonHTRateSet = RATE_SET_BIT_9M;
3653         *pu2BSSBasicRateSet = RATE_SET_BIT_9M;
3654         *pucMcsSet = 0;
3655         *pucSupMcs32 = 0;
3656         *pu2HtCapInfo = 0;
3657         break;
3658
3659     case FIXED_RATE_12M:
3660         if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_ERP) {
3661             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_ERP;
3662         }
3663         else if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_OFDM) {
3664             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_OFDM;
3665         }
3666
3667         *pu2DesiredNonHTRateSet = RATE_SET_BIT_12M;
3668         *pu2BSSBasicRateSet = RATE_SET_BIT_12M;
3669         *pucMcsSet = 0;
3670         *pucSupMcs32 = 0;
3671         *pu2HtCapInfo = 0;
3672         break;
3673
3674     case FIXED_RATE_18M:
3675         if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_ERP) {
3676             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_ERP;
3677         }
3678         else if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_OFDM) {
3679             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_OFDM;
3680         }
3681
3682         *pu2DesiredNonHTRateSet = RATE_SET_BIT_18M;
3683         *pu2BSSBasicRateSet = RATE_SET_BIT_18M;
3684         *pucMcsSet = 0;
3685         *pucSupMcs32 = 0;
3686         *pu2HtCapInfo = 0;
3687         break;
3688
3689     case FIXED_RATE_24M:
3690         if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_ERP) {
3691             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_ERP;
3692         }
3693         else if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_OFDM) {
3694             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_OFDM;
3695         }
3696
3697         *pu2DesiredNonHTRateSet = RATE_SET_BIT_24M;
3698         *pu2BSSBasicRateSet = RATE_SET_BIT_24M;
3699         *pucMcsSet = 0;
3700         *pucSupMcs32 = 0;
3701         *pu2HtCapInfo = 0;
3702         break;
3703
3704     case FIXED_RATE_36M:
3705         if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_ERP) {
3706             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_ERP;
3707         }
3708         else if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_OFDM) {
3709             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_OFDM;
3710         }
3711
3712         *pu2DesiredNonHTRateSet = RATE_SET_BIT_36M;
3713         *pu2BSSBasicRateSet = RATE_SET_BIT_36M;
3714         *pucMcsSet = 0;
3715         *pucSupMcs32 = 0;
3716         *pu2HtCapInfo = 0;
3717         break;
3718
3719     case FIXED_RATE_48M:
3720         if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_ERP) {
3721             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_ERP;
3722         }
3723         else if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_OFDM) {
3724             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_OFDM;
3725         }
3726
3727         *pu2DesiredNonHTRateSet = RATE_SET_BIT_48M;
3728         *pu2BSSBasicRateSet = RATE_SET_BIT_48M;
3729         *pucMcsSet = 0;
3730         *pucSupMcs32 = 0;
3731         *pu2HtCapInfo = 0;
3732         break;
3733
3734     case FIXED_RATE_54M:
3735         if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_ERP) {
3736             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_ERP;
3737         }
3738         else if((*pucDesiredPhyTypeSet) | PHY_TYPE_BIT_OFDM) {
3739             *pucDesiredPhyTypeSet = PHY_TYPE_BIT_OFDM;
3740         }
3741
3742         *pu2DesiredNonHTRateSet = RATE_SET_BIT_54M;
3743         *pu2BSSBasicRateSet = RATE_SET_BIT_54M;
3744         *pucMcsSet = 0;
3745         *pucSupMcs32 = 0;
3746         *pu2HtCapInfo = 0;
3747         break;
3748
3749     case FIXED_RATE_MCS0_20M_800NS:
3750         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3751         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3752         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3753         *pucMcsSet = HT_RATE_MCS0_INDEX;
3754         *pucSupMcs32 = 0;
3755         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3756                 | HT_CAP_INFO_SHORT_GI_20M
3757                 | HT_CAP_INFO_SHORT_GI_40M
3758                 | HT_CAP_INFO_HT_GF);
3759         break;
3760
3761     case FIXED_RATE_MCS1_20M_800NS:
3762         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3763         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3764         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3765         *pucMcsSet = HT_RATE_MCS1_INDEX;
3766         *pucSupMcs32 = 0;
3767         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3768                 | HT_CAP_INFO_SHORT_GI_20M
3769                 | HT_CAP_INFO_SHORT_GI_40M
3770                 | HT_CAP_INFO_HT_GF);
3771         break;
3772
3773     case FIXED_RATE_MCS2_20M_800NS:
3774         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3775         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3776         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3777         *pucMcsSet = HT_RATE_MCS2_INDEX;
3778         *pucSupMcs32 = 0;
3779         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3780                 | HT_CAP_INFO_SHORT_GI_20M
3781                 | HT_CAP_INFO_SHORT_GI_40M
3782                 | HT_CAP_INFO_HT_GF);
3783         break;
3784
3785     case FIXED_RATE_MCS3_20M_800NS:
3786         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3787         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3788         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3789         *pucMcsSet = HT_RATE_MCS3_INDEX;
3790         *pucSupMcs32 = 0;
3791         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3792                 | HT_CAP_INFO_SHORT_GI_20M
3793                 | HT_CAP_INFO_SHORT_GI_40M
3794                 | HT_CAP_INFO_HT_GF);
3795         break;
3796
3797     case FIXED_RATE_MCS4_20M_800NS:
3798         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3799         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3800         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3801         *pucMcsSet = HT_RATE_MCS4_INDEX;
3802         *pucSupMcs32 = 0;
3803         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3804                 | HT_CAP_INFO_SHORT_GI_20M
3805                 | HT_CAP_INFO_SHORT_GI_40M
3806                 | HT_CAP_INFO_HT_GF);
3807         break;
3808
3809     case FIXED_RATE_MCS5_20M_800NS:
3810         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3811         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3812         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3813         *pucMcsSet = HT_RATE_MCS5_INDEX;
3814         *pucSupMcs32 = 0;
3815         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3816                 | HT_CAP_INFO_SHORT_GI_20M
3817                 | HT_CAP_INFO_SHORT_GI_40M
3818                 | HT_CAP_INFO_HT_GF);
3819         break;
3820
3821     case FIXED_RATE_MCS6_20M_800NS:
3822         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3823         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3824         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3825         *pucMcsSet = HT_RATE_MCS6_INDEX;
3826         *pucSupMcs32 = 0;
3827         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3828                 | HT_CAP_INFO_SHORT_GI_20M
3829                 | HT_CAP_INFO_SHORT_GI_40M
3830                 | HT_CAP_INFO_HT_GF);
3831         break;
3832
3833     case FIXED_RATE_MCS7_20M_800NS:
3834         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3835         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3836         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3837         *pucMcsSet = HT_RATE_MCS7_INDEX;
3838         *pucSupMcs32 = 0;
3839         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3840                 | HT_CAP_INFO_SHORT_GI_20M
3841                 | HT_CAP_INFO_SHORT_GI_40M
3842                 | HT_CAP_INFO_HT_GF);
3843         break;
3844
3845     case FIXED_RATE_MCS0_20M_400NS:
3846         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3847         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3848         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3849         *pucMcsSet = HT_RATE_MCS0_INDEX;
3850         *pucSupMcs32 = 0;
3851         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3852                 | HT_CAP_INFO_SHORT_GI_40M
3853                 | HT_CAP_INFO_HT_GF);
3854         (*pu2HtCapInfo) |= HT_CAP_INFO_SHORT_GI_20M;
3855         break;
3856
3857     case FIXED_RATE_MCS1_20M_400NS:
3858         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3859         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3860         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3861         *pucMcsSet = HT_RATE_MCS1_INDEX;
3862         *pucSupMcs32 = 0;
3863         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3864                 | HT_CAP_INFO_SHORT_GI_40M
3865                 | HT_CAP_INFO_HT_GF);
3866         (*pu2HtCapInfo) |= HT_CAP_INFO_SHORT_GI_20M;
3867         break;
3868
3869     case FIXED_RATE_MCS2_20M_400NS:
3870         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3871         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3872         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3873         *pucMcsSet = HT_RATE_MCS2_INDEX;
3874         *pucSupMcs32 = 0;
3875         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3876                 | HT_CAP_INFO_SHORT_GI_40M
3877                 | HT_CAP_INFO_HT_GF);
3878         (*pu2HtCapInfo) |= HT_CAP_INFO_SHORT_GI_20M;
3879         break;
3880
3881     case FIXED_RATE_MCS3_20M_400NS:
3882         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3883         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3884         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3885         *pucMcsSet = HT_RATE_MCS3_INDEX;
3886         *pucSupMcs32 = 0;
3887         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3888                 | HT_CAP_INFO_SHORT_GI_40M
3889                 | HT_CAP_INFO_HT_GF);
3890         (*pu2HtCapInfo) |= HT_CAP_INFO_SHORT_GI_20M;
3891         break;
3892
3893     case FIXED_RATE_MCS4_20M_400NS:
3894         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3895         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3896         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3897         *pucMcsSet = HT_RATE_MCS4_INDEX;
3898         *pucSupMcs32 = 0;
3899         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3900                 | HT_CAP_INFO_SHORT_GI_40M
3901                 | HT_CAP_INFO_HT_GF);
3902         (*pu2HtCapInfo) |= HT_CAP_INFO_SHORT_GI_20M;
3903         break;
3904
3905     case FIXED_RATE_MCS5_20M_400NS:
3906         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3907         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3908         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3909         *pucMcsSet = HT_RATE_MCS5_INDEX;
3910         *pucSupMcs32 = 0;
3911         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3912                 | HT_CAP_INFO_SHORT_GI_40M
3913                 | HT_CAP_INFO_HT_GF);
3914         (*pu2HtCapInfo) |= HT_CAP_INFO_SHORT_GI_20M;
3915         break;
3916
3917     case FIXED_RATE_MCS6_20M_400NS:
3918         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3919         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3920         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3921         *pucMcsSet = HT_RATE_MCS6_INDEX;
3922         *pucSupMcs32 = 0;
3923         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3924                 | HT_CAP_INFO_SHORT_GI_40M
3925                 | HT_CAP_INFO_HT_GF);
3926         (*pu2HtCapInfo) |= HT_CAP_INFO_SHORT_GI_20M;
3927         break;
3928
3929     case FIXED_RATE_MCS7_20M_400NS:
3930         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3931         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3932         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3933         *pucMcsSet = HT_RATE_MCS7_INDEX;
3934         *pucSupMcs32 = 0;
3935         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SUP_CHNL_WIDTH
3936                 | HT_CAP_INFO_SHORT_GI_40M
3937                 | HT_CAP_INFO_HT_GF);
3938         (*pu2HtCapInfo) |= HT_CAP_INFO_SHORT_GI_20M;
3939         break;
3940
3941     case FIXED_RATE_MCS0_40M_800NS:
3942         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3943         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3944         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3945         *pucMcsSet = HT_RATE_MCS0_INDEX;
3946         *pucSupMcs32 = 0;
3947         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
3948                 | HT_CAP_INFO_SHORT_GI_40M
3949                 | HT_CAP_INFO_HT_GF);
3950         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
3951         break;
3952
3953     case FIXED_RATE_MCS1_40M_800NS:
3954         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3955         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3956         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3957         *pucMcsSet = HT_RATE_MCS1_INDEX;
3958         *pucSupMcs32 = 0;
3959         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
3960                 | HT_CAP_INFO_SHORT_GI_40M
3961                 | HT_CAP_INFO_HT_GF);
3962         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
3963         break;
3964
3965     case FIXED_RATE_MCS2_40M_800NS:
3966         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3967         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3968         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3969         *pucMcsSet = HT_RATE_MCS2_INDEX;
3970         *pucSupMcs32 = 0;
3971         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
3972                 | HT_CAP_INFO_SHORT_GI_40M
3973                 | HT_CAP_INFO_HT_GF);
3974         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
3975         break;
3976
3977     case FIXED_RATE_MCS3_40M_800NS:
3978         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3979         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3980         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3981         *pucMcsSet = HT_RATE_MCS3_INDEX;
3982         *pucSupMcs32 = 0;
3983         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
3984                 | HT_CAP_INFO_SHORT_GI_40M
3985                 | HT_CAP_INFO_HT_GF);
3986         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
3987         break;
3988
3989     case FIXED_RATE_MCS4_40M_800NS:
3990         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
3991         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
3992         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
3993         *pucMcsSet = HT_RATE_MCS4_INDEX;
3994         *pucSupMcs32 = 0;
3995         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
3996                 | HT_CAP_INFO_SHORT_GI_40M
3997                 | HT_CAP_INFO_HT_GF);
3998         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
3999         break;
4000
4001     case FIXED_RATE_MCS5_40M_800NS:
4002         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4003         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4004         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4005         *pucMcsSet = HT_RATE_MCS5_INDEX;
4006         *pucSupMcs32 = 0;
4007         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4008                 | HT_CAP_INFO_SHORT_GI_40M
4009                 | HT_CAP_INFO_HT_GF);
4010         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
4011         break;
4012
4013     case FIXED_RATE_MCS6_40M_800NS:
4014         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4015         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4016         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4017         *pucMcsSet = HT_RATE_MCS6_INDEX;
4018         *pucSupMcs32 = 0;
4019         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4020                 | HT_CAP_INFO_SHORT_GI_40M
4021                 | HT_CAP_INFO_HT_GF);
4022         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
4023         break;
4024
4025     case FIXED_RATE_MCS7_40M_800NS:
4026         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4027         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4028         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4029         *pucMcsSet = HT_RATE_MCS7_INDEX;
4030         *pucSupMcs32 = 0;
4031         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4032                 | HT_CAP_INFO_SHORT_GI_40M
4033                 | HT_CAP_INFO_HT_GF);
4034         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
4035         break;
4036
4037     case FIXED_RATE_MCS32_800NS:
4038         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4039         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4040         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4041         *pucMcsSet = HT_RATE_MCS32_INDEX;
4042         *pucSupMcs32 = 1;
4043         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4044                 | HT_CAP_INFO_SHORT_GI_40M
4045                 | HT_CAP_INFO_HT_GF);
4046         (*pu2HtCapInfo) |= HT_CAP_INFO_SUP_CHNL_WIDTH;
4047         break;
4048
4049     case FIXED_RATE_MCS0_40M_400NS:
4050         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4051         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4052         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4053         *pucMcsSet = HT_RATE_MCS0_INDEX;
4054         *pucSupMcs32 = 0;
4055         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4056                 | HT_CAP_INFO_HT_GF);
4057         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4058                 | HT_CAP_INFO_SHORT_GI_40M);
4059         break;
4060
4061     case FIXED_RATE_MCS1_40M_400NS:
4062         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4063         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4064         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4065         *pucMcsSet = HT_RATE_MCS1_INDEX;
4066         *pucSupMcs32 = 0;
4067         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4068                 | HT_CAP_INFO_HT_GF);
4069         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4070                 | HT_CAP_INFO_SHORT_GI_40M);
4071         break;
4072
4073     case FIXED_RATE_MCS2_40M_400NS:
4074         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4075         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4076         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4077         *pucMcsSet = HT_RATE_MCS2_INDEX;
4078         *pucSupMcs32 = 0;
4079         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4080                 | HT_CAP_INFO_HT_GF);
4081         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4082                 | HT_CAP_INFO_SHORT_GI_40M);
4083         break;
4084
4085     case FIXED_RATE_MCS3_40M_400NS:
4086         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4087         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4088         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4089         *pucMcsSet = HT_RATE_MCS3_INDEX;
4090         *pucSupMcs32 = 0;
4091         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4092                 | HT_CAP_INFO_HT_GF);
4093         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4094                 | HT_CAP_INFO_SHORT_GI_40M);
4095         break;
4096
4097     case FIXED_RATE_MCS4_40M_400NS:
4098         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4099         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4100         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4101         *pucMcsSet = HT_RATE_MCS4_INDEX;
4102         *pucSupMcs32 = 0;
4103         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4104                 | HT_CAP_INFO_HT_GF);
4105         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4106                 | HT_CAP_INFO_SHORT_GI_40M);
4107         break;
4108
4109     case FIXED_RATE_MCS5_40M_400NS:
4110         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4111         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4112         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4113         *pucMcsSet = HT_RATE_MCS5_INDEX;
4114         *pucSupMcs32 = 0;
4115         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4116                 | HT_CAP_INFO_HT_GF);
4117         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4118                 | HT_CAP_INFO_SHORT_GI_40M);
4119         break;
4120
4121     case FIXED_RATE_MCS6_40M_400NS:
4122         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4123         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4124         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4125         *pucMcsSet = HT_RATE_MCS6_INDEX;
4126         *pucSupMcs32 = 0;
4127         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4128                 | HT_CAP_INFO_HT_GF);
4129         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4130                 | HT_CAP_INFO_SHORT_GI_40M);
4131         break;
4132
4133     case FIXED_RATE_MCS7_40M_400NS:
4134         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4135         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4136         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4137         *pucMcsSet = HT_RATE_MCS7_INDEX;
4138         *pucSupMcs32 = 0;
4139         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4140                 | HT_CAP_INFO_HT_GF);
4141         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4142                 | HT_CAP_INFO_SHORT_GI_40M);
4143         break;
4144
4145     case FIXED_RATE_MCS32_400NS:
4146         *pucDesiredPhyTypeSet = PHY_TYPE_BIT_HT;
4147         *pu2DesiredNonHTRateSet = RATE_SET_BIT_HT_PHY;
4148         *pu2BSSBasicRateSet = RATE_SET_BIT_HT_PHY;
4149         *pucMcsSet = HT_RATE_MCS32_INDEX;
4150         *pucSupMcs32 = 1;
4151         (*pu2HtCapInfo) &= ~(HT_CAP_INFO_SHORT_GI_20M
4152                 | HT_CAP_INFO_HT_GF);
4153         (*pu2HtCapInfo) |= (HT_CAP_INFO_SUP_CHNL_WIDTH
4154                 | HT_CAP_INFO_SHORT_GI_40M);
4155         break;
4156
4157     default:
4158         ASSERT(0);
4159     }
4160
4161     return WLAN_STATUS_SUCCESS;
4162 }
4163
4164 /*----------------------------------------------------------------------------*/
4165 /*!
4166 * @brief This utility function is used to write the register
4167 *
4168 * @param u4Address         Register address
4169 *        u4Value           the value to be written
4170 *
4171 * @retval WLAN_STATUS_SUCCESS
4172 *         WLAN_STATUS_FAILURE
4173 */
4174 /*----------------------------------------------------------------------------*/
4175
4176 WLAN_STATUS
4177 nicWriteMcr (
4178     IN P_ADAPTER_T  prAdapter,
4179     IN UINT_32   u4Address,
4180     IN  UINT_32  u4Value
4181     )
4182 {
4183     CMD_ACCESS_REG rCmdAccessReg;
4184
4185     rCmdAccessReg.u4Address = u4Address;
4186     rCmdAccessReg.u4Data = u4Value;
4187
4188     return wlanSendSetQueryCmd(prAdapter,
4189                 CMD_ID_ACCESS_REG,
4190                 TRUE,
4191                 FALSE,
4192                 FALSE,
4193                 NULL,
4194                 NULL,
4195                 sizeof(CMD_ACCESS_REG),
4196                 (PUINT_8)&rCmdAccessReg,
4197                 NULL,
4198                 0
4199                 );
4200
4201 }
4202
4203 /*----------------------------------------------------------------------------*/
4204 /*!
4205 * @brief This utility function is used to modify the auto rate parameters
4206 *
4207 * @param u4ArSysParam0  see description below
4208 *        u4ArSysParam1
4209 *        u4ArSysParam2
4210 *        u4ArSysParam3
4211 *
4212 *
4213 * @retval WLAN_STATUS_SUCCESS
4214 *         WLAN_STATUS_FAILURE
4215 *
4216 * @note
4217 *   ArSysParam0[0:3] -> auto rate version (0:disable 1:version1 2:version2)
4218 *   ArSysParam0[4:5]-> auto bw version (0:disable 1:version1 2:version2)
4219 *   ArSysParam0[6:7]-> auto gi version (0:disable 1:version1 2:version2)
4220 *   ArSysParam0[8:15]-> HT rate clear mask
4221 *   ArSysParam0[16:31]-> Legacy rate clear mask
4222 *   ArSysParam1[0:7]-> Auto Rate check weighting window
4223 *   ArSysParam1[8:15]-> Auto Rate v1 Force Rate down
4224 *   ArSysParam1[16:23]-> Auto Rate v1 PerH
4225 *   ArSysParam1[24:31]-> Auto Rate v1 PerL
4226 *
4227 *   Examples
4228 *   ArSysParam0 = 1,
4229 *   Enable auto rate version 1
4230 *
4231 *   ArSysParam0 = 983041,
4232 *   Enable auto rate version 1
4233 *   Remove CCK 1M, 2M, 5.5M, 11M
4234 *
4235 *   ArSysParam0 = 786433
4236 *   Enable auto rate version 1
4237 *   Remove CCK 5.5M 11M
4238 */
4239 /*----------------------------------------------------------------------------*/
4240
4241
4242 WLAN_STATUS
4243 nicRlmArUpdateParms(
4244     IN P_ADAPTER_T prAdapter,
4245     IN UINT_32 u4ArSysParam0,
4246     IN UINT_32 u4ArSysParam1,
4247     IN UINT_32 u4ArSysParam2,
4248     IN UINT_32 u4ArSysParam3
4249     )
4250 {
4251     UINT_8 ucArVer,ucAbwVer,ucAgiVer;
4252     UINT_16 u2HtClrMask;
4253     UINT_16 u2LegacyClrMask;
4254     UINT_8  ucArCheckWindow;
4255     UINT_8  ucArPerL;
4256     UINT_8  ucArPerH;
4257     UINT_8  ucArPerForceRateDownPer;
4258
4259     ucArVer = (UINT_8)(u4ArSysParam0 & BITS(0,3));
4260     ucAbwVer = (UINT_8)((u4ArSysParam0 & BITS(4,5)) >>4);
4261     ucAgiVer = (UINT_8)((u4ArSysParam0 & BITS(6,7)) >>6);
4262     u2HtClrMask = (UINT_16) ((u4ArSysParam0 & BITS(8,15)) >> 8);
4263     u2LegacyClrMask = (UINT_16) ((u4ArSysParam0 & BITS(16,31)) >> 16);
4264
4265 #if 0
4266     ucArCheckWindow = (UINT_8) (u4ArSysParam1 & BITS(0,7));
4267     ucArPerForceRateDownPer = (UINT_8) ((u4ArSysParam1 & BITS(8,15)>>8));
4268     ucArPerH = (UINT_8) ((u4ArSysParam1 & BITS(16,23)) >>16);
4269     ucArPerL = (UINT_8) ((u4ArSysParam1 & BITS(24,31)) >>24);
4270 #endif
4271
4272     ucArCheckWindow = (UINT_8) (u4ArSysParam1 & BITS(0,7));
4273     ucArPerForceRateDownPer = (UINT_8) (((u4ArSysParam1>>8) & BITS(0,7)));
4274     ucArPerH = (UINT_8) (((u4ArSysParam1>>16) & BITS(0,7)));
4275     ucArPerL = (UINT_8) (((u4ArSysParam1>>24) & BITS(0,7)));
4276
4277
4278     DBGLOG(INIT, INFO, ("ArParam %u %u %u %u\n", u4ArSysParam0, u4ArSysParam1, u4ArSysParam2, u4ArSysParam3));
4279     DBGLOG(INIT, INFO, ("ArVer %u AbwVer %u AgiVer %u\n", ucArVer, ucAbwVer, ucAgiVer));
4280     DBGLOG(INIT, INFO, ("HtMask %x LegacyMask %x\n", u2HtClrMask, u2LegacyClrMask));
4281     DBGLOG(INIT, INFO, ("CheckWin %u RateDownPer %u PerH %u PerL %u\n", ucArCheckWindow, ucArPerForceRateDownPer, ucArPerH, ucArPerL));
4282
4283 #define SWCR_DATA_ADDR(MOD,ADDR) (0x90000000+(MOD<<8)+(ADDR))
4284 #define SWCR_DATA_CMD(CATE,WRITE,INDEX,OPT0,OPT1) ( (CATE<<24) | (WRITE<<23) | (INDEX<<16) | (OPT0 <<8) | OPT1  )
4285 #define SWCR_DATA0 0x0
4286 #define SWCR_DATA1 0x4
4287 #define SWCR_DATA2 0x8
4288 #define SWCR_DATA3 0xC
4289 #define SWCR_DATA4 0x10
4290 #define SWCR_WRITE 1
4291 #define SWCR_READ 0
4292
4293     if(ucArVer > 0) {
4294        /* dummy = WiFi.WriteMCR(&h90000104, &h00000001) */
4295        /* dummy = WiFi.WriteMCR(&h90000100, &h00850000)*/
4296
4297        nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),1);
4298        nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,5,0,0));
4299     }
4300     else {
4301        nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),0);
4302        nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,5,0,0)) ;
4303     }
4304
4305     /* ucArVer 0: none 1:PER 2:Rcpi */
4306     nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),ucArVer);
4307     nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,7,0,0));
4308
4309     /* Candidate rate Ht mask */
4310     nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),u2HtClrMask);
4311     nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,0x1c,0,0));
4312
4313     /* Candidate rate legacy mask */
4314     nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),u2LegacyClrMask);
4315     nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,0x1d,0,0));
4316
4317 #if 0
4318     if(ucArCheckWindow!=0) {
4319         /* TX DONE MCS INDEX CHECK STA RATE DOWN TH */
4320         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),ucArCheckWindow);
4321         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,0x14,0,0));
4322         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),ucArCheckWindow);
4323         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,0xc,0,0));
4324     }
4325
4326     if(ucArPerForceRateDownPer !=0) {
4327         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),ucArPerForceRateDownPer);
4328         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,0x18,0,0));
4329     }
4330     if(ucArPerH !=0) {
4331         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),ucArPerH);
4332         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,0x1,0,0));
4333     }
4334     if(ucArPerL !=0) {
4335         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA1),ucArPerL);
4336         nicWriteMcr (prAdapter, SWCR_DATA_ADDR(1/*MOD*/,SWCR_DATA0),SWCR_DATA_CMD(0,SWCR_WRITE,0x2,0,0));
4337     }
4338 #endif
4339
4340
4341
4342     return WLAN_STATUS_SUCCESS;
4343 }
4344
4345 /*----------------------------------------------------------------------------*/
4346 /*!
4347 * @brief This utility function is used to enable roaming
4348 *
4349 * @param u4EnableRoaming
4350 *
4351 *
4352 * @retval WLAN_STATUS_SUCCESS
4353 *         WLAN_STATUS_FAILURE
4354 *
4355 * @note
4356 *   u4EnableRoaming -> Enable Romaing
4357 *
4358 */
4359 /*----------------------------------------------------------------------------*/
4360 WLAN_STATUS
4361 nicRoamingUpdateParams(
4362     IN P_ADAPTER_T prAdapter,
4363     IN UINT_32 u4EnableRoaming
4364     )
4365 {
4366     P_CONNECTION_SETTINGS_T prConnSettings;
4367
4368     prConnSettings = &(prAdapter->rWifiVar.rConnSettings);
4369     prConnSettings->fgIsEnableRoaming = ((u4EnableRoaming>0)?(TRUE):(FALSE));
4370
4371     return WLAN_STATUS_SUCCESS;
4372 }
4373
4374 /*----------------------------------------------------------------------------*/
4375 /*!
4376 * \brief dump firmware Assert message
4377 *
4378 * \param[in]
4379 *           prAdapter
4380 *
4381 * \return
4382 *           TRUE
4383 *           FALSE
4384 */
4385 /*----------------------------------------------------------------------------*/
4386 VOID
4387 nicPrintFirmwareAssertInfo(
4388     IN P_ADAPTER_T prAdapter
4389     )
4390 {
4391     UINT_32 u4MailBox0, u4MailBox1;
4392     UINT_32 line = 0;
4393     UINT_8 aucAssertFile[7];
4394     UINT_32 u4ChipId;
4395
4396 #if CFG_SDIO_INTR_ENHANCE
4397     u4MailBox0 = prAdapter->prSDIOCtrl->u4RcvMailbox0;
4398     u4MailBox1 = prAdapter->prSDIOCtrl->u4RcvMailbox1;
4399 #else
4400     nicGetMailbox(prAdapter, 0, &u4MailBox0);
4401     nicGetMailbox(prAdapter, 1, &u4MailBox1);
4402 #endif
4403
4404     line = u4MailBox0 & 0x0000FFFF;
4405
4406     u4MailBox0 = ((u4MailBox0 >> 16) & 0x0000FFFF);
4407
4408     kalMemCopy(&aucAssertFile[0], &u4MailBox0, 2);
4409     kalMemCopy(&aucAssertFile[2], &u4MailBox1, 4);
4410
4411     aucAssertFile[6] = '\0';
4412
4413 #if defined(MT6620)
4414     u4ChipId = 6620;
4415 #elif defined(MT5931)
4416     u4ChipId = 5931;
4417 #elif defined(MT6628)
4418     u4ChipId = 6628;
4419 #endif
4420
4421     kalPrint("\n[MT%ld][wifi][Firmware] Assert at \"%s\" #%ld\n\n",
4422         u4ChipId,
4423         aucAssertFile,
4424                 line);
4425
4426 }
4427
4428
4429 /*----------------------------------------------------------------------------*/
4430 /*!
4431 * @brief This function is called to update Link Quality information
4432 *
4433 * @param prAdapter      Pointer of Adapter Data Structure
4434 *        eNetTypeIdx
4435 *        prEventLinkQuality
4436 *        cRssi
4437 *        cLinkQuality
4438 *
4439 * @return none
4440 */
4441 /*----------------------------------------------------------------------------*/
4442 VOID
4443 nicUpdateLinkQuality(
4444     IN P_ADAPTER_T                  prAdapter,
4445     IN ENUM_NETWORK_TYPE_INDEX_T    eNetTypeIdx,
4446     IN P_EVENT_LINK_QUALITY         prEventLinkQuality
4447     )
4448 {
4449     ASSERT(prAdapter);
4450     ASSERT(eNetTypeIdx < NETWORK_TYPE_INDEX_NUM);
4451     ASSERT(prEventLinkQuality);
4452
4453     switch(eNetTypeIdx) {
4454     case NETWORK_TYPE_AIS_INDEX:
4455         if(prAdapter->rWifiVar.arBssInfo[eNetTypeIdx].eConnectionState == PARAM_MEDIA_STATE_CONNECTED) {
4456             /* check is to prevent RSSI to be updated by incorrect initial RSSI from hardware */
4457             /* buffer statistics for further query */
4458             if(prAdapter->fgIsLinkQualityValid == FALSE
4459                     || (kalGetTimeTick() - prAdapter->rLinkQualityUpdateTime) > CFG_LINK_QUALITY_VALID_PERIOD) {
4460                 nicUpdateRSSI(prAdapter, eNetTypeIdx, prEventLinkQuality->cRssi, prEventLinkQuality->cLinkQuality);
4461             }
4462
4463             if(prAdapter->fgIsLinkRateValid == FALSE
4464                     || (kalGetTimeTick() - prAdapter->rLinkRateUpdateTime) > CFG_LINK_QUALITY_VALID_PERIOD) {
4465                 nicUpdateLinkSpeed(prAdapter, eNetTypeIdx, prEventLinkQuality->u2LinkSpeed);
4466             }
4467         }
4468         break;
4469 #if CFG_ENABLE_WIFI_DIRECT && CFG_SUPPORT_P2P_RSSI_QUERY
4470     case NETWORK_TYPE_P2P_INDEX:
4471         if (prAdapter->fgIsP2pLinkQualityValid == FALSE
4472                 || (kalGetTimeTick() - prAdapter->rP2pLinkQualityUpdateTime) > CFG_LINK_QUALITY_VALID_PERIOD) {
4473             P_EVENT_LINK_QUALITY_EX prEventLQEx = (P_EVENT_LINK_QUALITY_EX)prEventLinkQuality;
4474
4475             nicUpdateRSSI(prAdapter, NETWORK_TYPE_P2P_INDEX, prEventLQEx->cRssiP2P, prEventLQEx->cLinkQualityP2P);
4476         }
4477         break;
4478 #endif
4479     default:
4480         break;
4481
4482     }
4483
4484     return;
4485 }
4486
4487
4488 /*----------------------------------------------------------------------------*/
4489 /*!
4490 * @brief This function is called to update RSSI and Link Quality information
4491 *
4492 * @param prAdapter      Pointer of Adapter Data Structure
4493 *        eNetTypeIdx
4494 *        cRssi
4495 *        cLinkQuality
4496 *
4497 * @return none
4498 */
4499 /*----------------------------------------------------------------------------*/
4500 VOID
4501 nicUpdateRSSI(
4502     IN P_ADAPTER_T                  prAdapter,
4503     IN ENUM_NETWORK_TYPE_INDEX_T    eNetTypeIdx,
4504     IN INT_8                        cRssi,
4505     IN INT_8                        cLinkQuality
4506     )
4507 {
4508     ASSERT(prAdapter);
4509     ASSERT(eNetTypeIdx < NETWORK_TYPE_INDEX_NUM);
4510
4511     switch(eNetTypeIdx) {
4512     case NETWORK_TYPE_AIS_INDEX:
4513         if(prAdapter->rWifiVar.arBssInfo[eNetTypeIdx].eConnectionState == PARAM_MEDIA_STATE_CONNECTED) {
4514             prAdapter->fgIsLinkQualityValid = TRUE;
4515             prAdapter->rLinkQualityUpdateTime = kalGetTimeTick();
4516
4517             prAdapter->rLinkQuality.cRssi = cRssi;
4518             prAdapter->rLinkQuality.cLinkQuality = cLinkQuality;
4519
4520             /* indicate to glue layer */
4521             kalUpdateRSSI(prAdapter->prGlueInfo,
4522                     KAL_NETWORK_TYPE_AIS_INDEX,
4523                     prAdapter->rLinkQuality.cRssi,
4524                     prAdapter->rLinkQuality.cLinkQuality);
4525         }
4526
4527         break;
4528 #if CFG_ENABLE_WIFI_DIRECT
4529 #if CFG_SUPPORT_P2P_RSSI_QUERY
4530     case NETWORK_TYPE_P2P_INDEX:
4531         prAdapter->fgIsP2pLinkQualityValid = TRUE;
4532         prAdapter->rP2pLinkQualityUpdateTime = kalGetTimeTick();
4533
4534         prAdapter->rP2pLinkQuality.cRssi= cRssi;
4535         prAdapter->rP2pLinkQuality.cLinkQuality= cLinkQuality;
4536
4537         kalUpdateRSSI(prAdapter->prGlueInfo,
4538                 KAL_NETWORK_TYPE_P2P_INDEX,
4539                 cRssi,
4540                 cLinkQuality);
4541         break;
4542 #endif
4543 #endif
4544     default:
4545         break;
4546
4547     }
4548
4549     return;
4550 }
4551
4552
4553 /*----------------------------------------------------------------------------*/
4554 /*!
4555 * @brief This function is called to update Link Quality information
4556 *
4557 * @param prAdapter      Pointer of Adapter Data Structure
4558 *        eNetTypeIdx
4559 *        prEventLinkQuality
4560 *        cRssi
4561 *        cLinkQuality
4562 *
4563 * @return none
4564 */
4565 /*----------------------------------------------------------------------------*/
4566 VOID
4567 nicUpdateLinkSpeed(
4568     IN P_ADAPTER_T                  prAdapter,
4569     IN ENUM_NETWORK_TYPE_INDEX_T    eNetTypeIdx,
4570     IN UINT_16                      u2LinkSpeed
4571     )
4572 {
4573     ASSERT(prAdapter);
4574     ASSERT(eNetTypeIdx < NETWORK_TYPE_INDEX_NUM);
4575
4576     switch(eNetTypeIdx) {
4577     case NETWORK_TYPE_AIS_INDEX:
4578         if(prAdapter->rWifiVar.arBssInfo[eNetTypeIdx].eConnectionState == PARAM_MEDIA_STATE_CONNECTED) {
4579             /* buffer statistics for further query */
4580             prAdapter->fgIsLinkRateValid = TRUE;
4581             prAdapter->rLinkRateUpdateTime = kalGetTimeTick();
4582
4583             prAdapter->rLinkQuality.u2LinkSpeed = u2LinkSpeed;
4584         }
4585         break;
4586
4587     default:
4588         break;
4589
4590     }
4591
4592     return;
4593 }
4594
4595 #if CFG_SUPPORT_RDD_TEST_MODE
4596 WLAN_STATUS
4597 nicUpdateRddTestMode(
4598     IN P_ADAPTER_T      prAdapter,
4599     IN P_CMD_RDD_CH_T   prRddChParam
4600     )
4601 {
4602     DEBUGFUNC("nicUpdateRddTestMode.\n");
4603
4604     ASSERT(prAdapter);
4605
4606 //    aisFsmScanRequest(prAdapter, NULL);
4607
4608     return wlanSendSetQueryCmd(prAdapter,
4609             CMD_ID_SET_RDD_CH,
4610             TRUE,
4611             FALSE,
4612             FALSE,
4613             NULL,
4614             NULL,
4615             sizeof(CMD_RDD_CH_T),
4616             (PUINT_8)prRddChParam,
4617             NULL,
4618             0);
4619 }
4620 #endif
4621