5b54cabdc8636d8c07944b8e5544a2084d0f24c8
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6655 / device_main.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: device_main.c
20  *
21  * Purpose: driver entry for initial, open, close, tx and rx.
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: Jan 8, 2003
26  *
27  * Functions:
28  *
29  *   vt6655_probe - module initial (insmod) driver entry
30  *   vt6655_remove - module remove entry
31  *   vt6655_init_info - device structure resource allocation function
32  *   device_free_info - device structure resource free function
33  *   device_get_pci_info - get allocated pci io/mem resource
34  *   device_print_info - print out resource
35  *   device_intr - interrupt handle function
36  *   device_rx_srv - rx service function
37  *   device_alloc_rx_buf - rx buffer pre-allocated function
38  *   device_alloc_frag_buf - rx fragement pre-allocated function
39  *   device_free_tx_buf - free tx buffer function
40  *   device_free_frag_buf- free de-fragement buffer
41  *   device_init_rd0_ring- initial rd dma0 ring
42  *   device_init_rd1_ring- initial rd dma1 ring
43  *   device_init_td0_ring- initial tx dma0 ring buffer
44  *   device_init_td1_ring- initial tx dma1 ring buffer
45  *   device_init_registers- initial MAC & BBP & RF internal registers.
46  *   device_init_rings- initial tx/rx ring buffer
47  *   device_init_defrag_cb- initial & allocate de-fragement buffer.
48  *   device_free_rings- free all allocated ring buffer
49  *   device_tx_srv- tx interrupt service function
50  *
51  * Revision History:
52  */
53 #undef __NO_VERSION__
54
55 #include <linux/file.h>
56 #include "device.h"
57 #include "card.h"
58 #include "channel.h"
59 #include "baseband.h"
60 #include "mac.h"
61 #include "tether.h"
62 #include "wmgr.h"
63 #include "wctl.h"
64 #include "power.h"
65 #include "wcmd.h"
66 #include "iocmd.h"
67 #include "tcrc.h"
68 #include "rxtx.h"
69 #include "wroute.h"
70 #include "bssdb.h"
71 #include "hostap.h"
72 #include "wpactl.h"
73 #include "dpc.h"
74 #include "datarate.h"
75 #include "rf.h"
76 #include "iowpa.h"
77 #include <linux/delay.h>
78 #include <linux/kthread.h>
79 #include <linux/slab.h>
80
81 /*---------------------  Static Definitions -------------------------*/
82 //
83 // Define module options
84 //
85 MODULE_AUTHOR("VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>");
86 MODULE_LICENSE("GPL");
87 MODULE_DESCRIPTION("VIA Networking Solomon-A/B/G Wireless LAN Adapter Driver");
88
89 #define DEVICE_PARAM(N, D)
90
91 #define RX_DESC_MIN0     16
92 #define RX_DESC_MAX0     128
93 #define RX_DESC_DEF0     32
94 DEVICE_PARAM(RxDescriptors0, "Number of receive descriptors0");
95
96 #define RX_DESC_MIN1     16
97 #define RX_DESC_MAX1     128
98 #define RX_DESC_DEF1     32
99 DEVICE_PARAM(RxDescriptors1, "Number of receive descriptors1");
100
101 #define TX_DESC_MIN0     16
102 #define TX_DESC_MAX0     128
103 #define TX_DESC_DEF0     32
104 DEVICE_PARAM(TxDescriptors0, "Number of transmit descriptors0");
105
106 #define TX_DESC_MIN1     16
107 #define TX_DESC_MAX1     128
108 #define TX_DESC_DEF1     64
109 DEVICE_PARAM(TxDescriptors1, "Number of transmit descriptors1");
110
111 #define IP_ALIG_DEF     0
112 /* IP_byte_align[] is used for IP header unsigned long byte aligned
113    0: indicate the IP header won't be unsigned long byte aligned.(Default) .
114    1: indicate the IP header will be unsigned long byte aligned.
115    In some environment, the IP header should be unsigned long byte aligned,
116    or the packet will be droped when we receive it. (eg: IPVS)
117 */
118 DEVICE_PARAM(IP_byte_align, "Enable IP header dword aligned");
119
120 #define INT_WORKS_DEF   20
121 #define INT_WORKS_MIN   10
122 #define INT_WORKS_MAX   64
123
124 DEVICE_PARAM(int_works, "Number of packets per interrupt services");
125
126 #define CHANNEL_MIN     1
127 #define CHANNEL_MAX     14
128 #define CHANNEL_DEF     6
129
130 DEVICE_PARAM(Channel, "Channel number");
131
132 /* PreambleType[] is the preamble length used for transmit.
133    0: indicate allows long preamble type
134    1: indicate allows short preamble type
135 */
136
137 #define PREAMBLE_TYPE_DEF     1
138
139 DEVICE_PARAM(PreambleType, "Preamble Type");
140
141 #define RTS_THRESH_MIN     512
142 #define RTS_THRESH_MAX     2347
143 #define RTS_THRESH_DEF     2347
144
145 DEVICE_PARAM(RTSThreshold, "RTS threshold");
146
147 #define FRAG_THRESH_MIN     256
148 #define FRAG_THRESH_MAX     2346
149 #define FRAG_THRESH_DEF     2346
150
151 DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
152
153 #define DATA_RATE_MIN     0
154 #define DATA_RATE_MAX     13
155 #define DATA_RATE_DEF     13
156 /* datarate[] index
157    0: indicate 1 Mbps   0x02
158    1: indicate 2 Mbps   0x04
159    2: indicate 5.5 Mbps 0x0B
160    3: indicate 11 Mbps  0x16
161    4: indicate 6 Mbps   0x0c
162    5: indicate 9 Mbps   0x12
163    6: indicate 12 Mbps  0x18
164    7: indicate 18 Mbps  0x24
165    8: indicate 24 Mbps  0x30
166    9: indicate 36 Mbps  0x48
167    10: indicate 48 Mbps  0x60
168    11: indicate 54 Mbps  0x6c
169    12: indicate 72 Mbps  0x90
170    13: indicate auto rate
171 */
172
173 DEVICE_PARAM(ConnectionRate, "Connection data rate");
174
175 #define OP_MODE_DEF     0
176
177 DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
178
179 /* OpMode[] is used for transmit.
180    0: indicate infrastruct mode used
181    1: indicate adhoc mode used
182    2: indicate AP mode used
183 */
184
185 /* PSMode[]
186    0: indicate disable power saving mode
187    1: indicate enable power saving mode
188 */
189
190 #define PS_MODE_DEF     0
191
192 DEVICE_PARAM(PSMode, "Power saving mode");
193
194 #define SHORT_RETRY_MIN     0
195 #define SHORT_RETRY_MAX     31
196 #define SHORT_RETRY_DEF     8
197
198 DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
199
200 #define LONG_RETRY_MIN     0
201 #define LONG_RETRY_MAX     15
202 #define LONG_RETRY_DEF     4
203
204 DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
205
206 /* BasebandType[] baseband type selected
207    0: indicate 802.11a type
208    1: indicate 802.11b type
209    2: indicate 802.11g type
210 */
211 #define BBP_TYPE_MIN     0
212 #define BBP_TYPE_MAX     2
213 #define BBP_TYPE_DEF     2
214
215 DEVICE_PARAM(BasebandType, "baseband type");
216
217 /* 80211hEnable[]
218    0: indicate disable 802.11h
219    1: indicate enable 802.11h
220 */
221
222 #define X80211h_MODE_DEF     0
223
224 DEVICE_PARAM(b80211hEnable, "802.11h mode");
225
226 /* 80211hEnable[]
227    0: indicate disable 802.11h
228    1: indicate enable 802.11h
229 */
230
231 #define DIVERSITY_ANT_DEF     0
232
233 DEVICE_PARAM(bDiversityANTEnable, "ANT diversity mode");
234
235 //
236 // Static vars definitions
237 //
238 static CHIP_INFO chip_info_table[] = {
239         { VT3253,       "VIA Networking Solomon-A/B/G Wireless LAN Adapter ",
240           256, 1,     DEVICE_FLAGS_IP_ALIGN|DEVICE_FLAGS_TX_ALIGN },
241         {0, NULL}
242 };
243
244 static const struct pci_device_id vt6655_pci_id_table[] = {
245         { PCI_VDEVICE(VIA, 0x3253), (kernel_ulong_t)chip_info_table},
246         { 0, }
247 };
248
249 /*---------------------  Static Functions  --------------------------*/
250
251 static int  vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent);
252 static void vt6655_init_info(struct pci_dev *pcid,
253                              struct vnt_private **ppDevice, PCHIP_INFO);
254 static void device_free_info(struct vnt_private *pDevice);
255 static bool device_get_pci_info(struct vnt_private *, struct pci_dev *pcid);
256 static void device_print_info(struct vnt_private *pDevice);
257 static void device_init_diversity_timer(struct vnt_private *pDevice);
258 static  irqreturn_t  device_intr(int irq,  void *dev_instance);
259
260 #ifdef CONFIG_PM
261 static int device_notify_reboot(struct notifier_block *, unsigned long event, void *ptr);
262 static struct notifier_block device_notifier = {
263         .notifier_call = device_notify_reboot,
264         .next = NULL,
265         .priority = 0,
266 };
267 #endif
268
269 static void device_init_rd0_ring(struct vnt_private *pDevice);
270 static void device_init_rd1_ring(struct vnt_private *pDevice);
271 static void device_init_defrag_cb(struct vnt_private *pDevice);
272 static void device_init_td0_ring(struct vnt_private *pDevice);
273 static void device_init_td1_ring(struct vnt_private *pDevice);
274
275 static int  device_rx_srv(struct vnt_private *pDevice, unsigned int uIdx);
276 static int  device_tx_srv(struct vnt_private *pDevice, unsigned int uIdx);
277 static bool device_alloc_rx_buf(struct vnt_private *pDevice, PSRxDesc pDesc);
278 static void device_init_registers(struct vnt_private *pDevice);
279 static void device_free_tx_buf(struct vnt_private *pDevice, PSTxDesc pDesc);
280 static void device_free_td0_ring(struct vnt_private *pDevice);
281 static void device_free_td1_ring(struct vnt_private *pDevice);
282 static void device_free_rd0_ring(struct vnt_private *pDevice);
283 static void device_free_rd1_ring(struct vnt_private *pDevice);
284 static void device_free_rings(struct vnt_private *pDevice);
285 static void device_free_frag_buf(struct vnt_private *pDevice);
286
287 /*---------------------  Export Variables  --------------------------*/
288
289 /*---------------------  Export Functions  --------------------------*/
290
291 static char *get_chip_name(int chip_id)
292 {
293         int i;
294
295         for (i = 0; chip_info_table[i].name != NULL; i++)
296                 if (chip_info_table[i].chip_id == chip_id)
297                         break;
298         return chip_info_table[i].name;
299 }
300
301 static void vt6655_remove(struct pci_dev *pcid)
302 {
303         struct vnt_private *pDevice = pci_get_drvdata(pcid);
304
305         if (pDevice == NULL)
306                 return;
307         device_free_info(pDevice);
308 }
309
310 static void device_get_options(struct vnt_private *pDevice)
311 {
312         POPTIONS pOpts = &(pDevice->sOpts);
313
314         pOpts->nRxDescs0 = RX_DESC_DEF0;
315         pOpts->nRxDescs1 = RX_DESC_DEF1;
316         pOpts->nTxDescs[0] = TX_DESC_DEF0;
317         pOpts->nTxDescs[1] = TX_DESC_DEF1;
318         pOpts->flags |= DEVICE_FLAGS_IP_ALIGN;
319         pOpts->int_works = INT_WORKS_DEF;
320         pOpts->rts_thresh = RTS_THRESH_DEF;
321         pOpts->frag_thresh = FRAG_THRESH_DEF;
322         pOpts->data_rate = DATA_RATE_DEF;
323         pOpts->channel_num = CHANNEL_DEF;
324
325         pOpts->flags |= DEVICE_FLAGS_PREAMBLE_TYPE;
326         pOpts->flags |= DEVICE_FLAGS_OP_MODE;
327         pOpts->short_retry = SHORT_RETRY_DEF;
328         pOpts->long_retry = LONG_RETRY_DEF;
329         pOpts->bbp_type = BBP_TYPE_DEF;
330         pOpts->flags |= DEVICE_FLAGS_80211h_MODE;
331         pOpts->flags |= DEVICE_FLAGS_DiversityANT;
332 }
333
334 static void
335 device_set_options(struct vnt_private *pDevice)
336 {
337         unsigned char abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
338         unsigned char abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
339         unsigned char abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
340
341         ether_addr_copy(pDevice->abyBroadcastAddr, abyBroadcastAddr);
342         ether_addr_copy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042);
343         ether_addr_copy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel);
344
345         pDevice->uChannel = pDevice->sOpts.channel_num;
346         pDevice->wRTSThreshold = pDevice->sOpts.rts_thresh;
347         pDevice->wFragmentationThreshold = pDevice->sOpts.frag_thresh;
348         pDevice->byShortRetryLimit = pDevice->sOpts.short_retry;
349         pDevice->byLongRetryLimit = pDevice->sOpts.long_retry;
350         pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
351         pDevice->byShortPreamble = (pDevice->sOpts.flags & DEVICE_FLAGS_PREAMBLE_TYPE) ? 1 : 0;
352         pDevice->byOpMode = (pDevice->sOpts.flags & DEVICE_FLAGS_OP_MODE) ? 1 : 0;
353         pDevice->ePSMode = (pDevice->sOpts.flags & DEVICE_FLAGS_PS_MODE) ? 1 : 0;
354         pDevice->b11hEnable = (pDevice->sOpts.flags & DEVICE_FLAGS_80211h_MODE) ? 1 : 0;
355         pDevice->bDiversityRegCtlON = (pDevice->sOpts.flags & DEVICE_FLAGS_DiversityANT) ? 1 : 0;
356         pDevice->uConnectionRate = pDevice->sOpts.data_rate;
357         if (pDevice->uConnectionRate < RATE_AUTO)
358                 pDevice->bFixRate = true;
359         pDevice->byBBType = pDevice->sOpts.bbp_type;
360         pDevice->byPacketType = (VIA_PKT_TYPE)pDevice->byBBType;
361         pDevice->byAutoFBCtrl = AUTO_FB_0;
362         pDevice->bUpdateBBVGA = true;
363         pDevice->byFOETuning = 0;
364         pDevice->byPreambleType = 0;
365
366         pr_debug(" uChannel= %d\n", (int)pDevice->uChannel);
367         pr_debug(" byOpMode= %d\n", (int)pDevice->byOpMode);
368         pr_debug(" ePSMode= %d\n", (int)pDevice->ePSMode);
369         pr_debug(" wRTSThreshold= %d\n", (int)pDevice->wRTSThreshold);
370         pr_debug(" byShortRetryLimit= %d\n", (int)pDevice->byShortRetryLimit);
371         pr_debug(" byLongRetryLimit= %d\n", (int)pDevice->byLongRetryLimit);
372         pr_debug(" byPreambleType= %d\n", (int)pDevice->byPreambleType);
373         pr_debug(" byShortPreamble= %d\n", (int)pDevice->byShortPreamble);
374         pr_debug(" uConnectionRate= %d\n", (int)pDevice->uConnectionRate);
375         pr_debug(" byBBType= %d\n", (int)pDevice->byBBType);
376         pr_debug(" pDevice->b11hEnable= %d\n", (int)pDevice->b11hEnable);
377         pr_debug(" pDevice->bDiversityRegCtlON= %d\n",
378                  (int)pDevice->bDiversityRegCtlON);
379 }
380
381 //
382 // Initialisation of MAC & BBP registers
383 //
384
385 static void device_init_registers(struct vnt_private *pDevice)
386 {
387         unsigned long flags;
388         unsigned int ii;
389         unsigned char byValue;
390         unsigned char byValue1;
391         unsigned char byCCKPwrdBm = 0;
392         unsigned char byOFDMPwrdBm = 0;
393
394         MACbShutdown(pDevice->PortOffset);
395         BBvSoftwareReset(pDevice->PortOffset);
396
397         /* Do MACbSoftwareReset in MACvInitialize */
398         MACbSoftwareReset(pDevice->PortOffset);
399
400         pDevice->bAES = false;
401
402         /* Only used in 11g type, sync with ERP IE */
403         pDevice->bProtectMode = false;
404
405         pDevice->bNonERPPresent = false;
406         pDevice->bBarkerPreambleMd = false;
407         pDevice->wCurrentRate = RATE_1M;
408         pDevice->byTopOFDMBasicRate = RATE_24M;
409         pDevice->byTopCCKBasicRate = RATE_1M;
410
411         /* Target to IF pin while programming to RF chip. */
412         pDevice->byRevId = 0;
413
414         /* init MAC */
415         MACvInitialize(pDevice->PortOffset);
416
417         /* Get Local ID */
418         VNSvInPortB(pDevice->PortOffset + MAC_REG_LOCALID, &pDevice->byLocalID);
419
420         spin_lock_irqsave(&pDevice->lock, flags);
421
422         SROMvReadAllContents(pDevice->PortOffset, pDevice->abyEEPROM);
423
424         spin_unlock_irqrestore(&pDevice->lock, flags);
425
426         /* Get Channel range */
427         pDevice->byMinChannel = 1;
428         pDevice->byMaxChannel = CB_MAX_CHANNEL;
429
430         /* Get Antena */
431         byValue = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ANTENNA);
432         if (byValue & EEP_ANTINV)
433                 pDevice->bTxRxAntInv = true;
434         else
435                 pDevice->bTxRxAntInv = false;
436
437         byValue &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
438         /* if not set default is All */
439         if (byValue == 0)
440                 byValue = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
441
442         pDevice->ulDiversityNValue = 100*260;
443         pDevice->ulDiversityMValue = 100*16;
444         pDevice->byTMax = 1;
445         pDevice->byTMax2 = 4;
446         pDevice->ulSQ3TH = 0;
447         pDevice->byTMax3 = 64;
448
449         if (byValue == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
450                 pDevice->byAntennaCount = 2;
451                 pDevice->byTxAntennaMode = ANT_B;
452                 pDevice->dwTxAntennaSel = 1;
453                 pDevice->dwRxAntennaSel = 1;
454
455                 if (pDevice->bTxRxAntInv)
456                         pDevice->byRxAntennaMode = ANT_A;
457                 else
458                         pDevice->byRxAntennaMode = ANT_B;
459
460                 byValue1 = SROMbyReadEmbedded(pDevice->PortOffset,
461                                               EEP_OFS_ANTENNA);
462
463                 if ((byValue1 & 0x08) == 0)
464                         pDevice->bDiversityEnable = false;
465                 else
466                         pDevice->bDiversityEnable = true;
467         } else  {
468                 pDevice->bDiversityEnable = false;
469                 pDevice->byAntennaCount = 1;
470                 pDevice->dwTxAntennaSel = 0;
471                 pDevice->dwRxAntennaSel = 0;
472
473                 if (byValue & EEP_ANTENNA_AUX) {
474                         pDevice->byTxAntennaMode = ANT_A;
475
476                         if (pDevice->bTxRxAntInv)
477                                 pDevice->byRxAntennaMode = ANT_B;
478                         else
479                                 pDevice->byRxAntennaMode = ANT_A;
480                 } else {
481                         pDevice->byTxAntennaMode = ANT_B;
482
483                         if (pDevice->bTxRxAntInv)
484                                 pDevice->byRxAntennaMode = ANT_A;
485                         else
486                                 pDevice->byRxAntennaMode = ANT_B;
487                 }
488         }
489
490         pr_debug("bDiversityEnable=[%d],NValue=[%d],MValue=[%d],TMax=[%d],TMax2=[%d]\n",
491                  pDevice->bDiversityEnable, (int)pDevice->ulDiversityNValue,
492                  (int)pDevice->ulDiversityMValue, pDevice->byTMax,
493                  pDevice->byTMax2);
494
495         /* zonetype initial */
496         pDevice->byOriginalZonetype = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
497
498         /* Get RFType */
499         pDevice->byRFType = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RFTYPE);
500
501         /* force change RevID for VT3253 emu */
502         if ((pDevice->byRFType & RF_EMU) != 0)
503                         pDevice->byRevId = 0x80;
504
505         pDevice->byRFType &= RF_MASK;
506         pr_debug("pDevice->byRFType = %x\n", pDevice->byRFType);
507
508         if (!pDevice->bZoneRegExist)
509                 pDevice->byZoneType = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
510
511         pr_debug("pDevice->byZoneType = %x\n", pDevice->byZoneType);
512
513         /* Init RF module */
514         RFbInit(pDevice);
515
516         /* Get Desire Power Value */
517         pDevice->byCurPwr = 0xFF;
518         pDevice->byCCKPwr = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_CCK);
519         pDevice->byOFDMPwrG = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_OFDMG);
520
521         /* Load power Table */
522         for (ii = 0; ii < CB_MAX_CHANNEL_24G; ii++) {
523                 pDevice->abyCCKPwrTbl[ii + 1] =
524                         SROMbyReadEmbedded(pDevice->PortOffset,
525                                            (unsigned char)(ii + EEP_OFS_CCK_PWR_TBL));
526                 if (pDevice->abyCCKPwrTbl[ii + 1] == 0)
527                         pDevice->abyCCKPwrTbl[ii+1] = pDevice->byCCKPwr;
528
529                 pDevice->abyOFDMPwrTbl[ii + 1] =
530                         SROMbyReadEmbedded(pDevice->PortOffset,
531                                            (unsigned char)(ii + EEP_OFS_OFDM_PWR_TBL));
532                 if (pDevice->abyOFDMPwrTbl[ii + 1] == 0)
533                         pDevice->abyOFDMPwrTbl[ii + 1] = pDevice->byOFDMPwrG;
534
535                 pDevice->abyCCKDefaultPwr[ii + 1] = byCCKPwrdBm;
536                 pDevice->abyOFDMDefaultPwr[ii + 1] = byOFDMPwrdBm;
537         }
538
539         /* recover 12,13 ,14channel for EUROPE by 11 channel */
540         for (ii = 11; ii < 14; ii++) {
541                 pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
542                 pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
543         }
544
545         /* Load OFDM A Power Table */
546         for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
547                 pDevice->abyOFDMPwrTbl[ii + CB_MAX_CHANNEL_24G + 1] =
548                         SROMbyReadEmbedded(pDevice->PortOffset,
549                                            (unsigned char)(ii + EEP_OFS_OFDMA_PWR_TBL));
550
551                 pDevice->abyOFDMDefaultPwr[ii + CB_MAX_CHANNEL_24G + 1] =
552                         SROMbyReadEmbedded(pDevice->PortOffset,
553                                            (unsigned char)(ii + EEP_OFS_OFDMA_PWR_dBm));
554         }
555
556         if (pDevice->byLocalID > REV_ID_VT3253_B1) {
557                 MACvSelectPage1(pDevice->PortOffset);
558
559                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_MSRCTL + 1,
560                              (MSRCTL1_TXPWR | MSRCTL1_CSAPAREN));
561
562                 MACvSelectPage0(pDevice->PortOffset);
563         }
564
565         /* use relative tx timeout and 802.11i D4 */
566         MACvWordRegBitsOn(pDevice->PortOffset,
567                           MAC_REG_CFG, (CFG_TKIPOPT | CFG_NOTXTIMEOUT));
568
569         /* set performance parameter by registry */
570         MACvSetShortRetryLimit(pDevice->PortOffset, pDevice->byShortRetryLimit);
571         MACvSetLongRetryLimit(pDevice->PortOffset, pDevice->byLongRetryLimit);
572
573         /* reset TSF counter */
574         VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
575         /* enable TSF counter */
576         VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
577
578         /* initialize BBP registers */
579         BBbVT3253Init(pDevice);
580
581         if (pDevice->bUpdateBBVGA) {
582                 pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
583                 pDevice->byBBVGANew = pDevice->byBBVGACurrent;
584                 BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
585         }
586
587         BBvSetRxAntennaMode(pDevice->PortOffset, pDevice->byRxAntennaMode);
588         BBvSetTxAntennaMode(pDevice->PortOffset, pDevice->byTxAntennaMode);
589
590         /* Set BB and packet type at the same time. */
591         /* Set Short Slot Time, xIFS, and RSPINF. */
592         if (pDevice->uConnectionRate == RATE_AUTO)
593                 pDevice->wCurrentRate = RATE_54M;
594         else
595                 pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
596
597         pDevice->bRadioOff = false;
598
599         pDevice->byRadioCtl = SROMbyReadEmbedded(pDevice->PortOffset,
600                                                  EEP_OFS_RADIOCTL);
601         pDevice->bHWRadioOff = false;
602
603         if (pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) {
604                 /* Get GPIO */
605                 MACvGPIOIn(pDevice->PortOffset, &pDevice->byGPIO);
606
607                 if (((pDevice->byGPIO & GPIO0_DATA) &&
608                      !(pDevice->byRadioCtl & EEP_RADIOCTL_INV)) ||
609                      (!(pDevice->byGPIO & GPIO0_DATA) &&
610                      (pDevice->byRadioCtl & EEP_RADIOCTL_INV)))
611                         pDevice->bHWRadioOff = true;
612         }
613
614         if (pDevice->bHWRadioOff || pDevice->bRadioControlOff)
615                 CARDbRadioPowerOff(pDevice);
616
617         /* get Permanent network address */
618         SROMvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
619         pr_debug("Network address = %pM\n", pDevice->abyCurrentNetAddr);
620
621         /* reset Tx pointer */
622         CARDvSafeResetRx(pDevice);
623         /* reset Rx pointer */
624         CARDvSafeResetTx(pDevice);
625
626         if (pDevice->byLocalID <= REV_ID_VT3253_A1)
627                 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_RCR, RCR_WPAERR);
628
629         /* Turn On Rx DMA */
630         MACvReceive0(pDevice->PortOffset);
631         MACvReceive1(pDevice->PortOffset);
632
633         /* start the adapter */
634         MACvStart(pDevice->PortOffset);
635 }
636
637 static void device_init_diversity_timer(struct vnt_private *pDevice)
638 {
639         init_timer(&pDevice->TimerSQ3Tmax1);
640         pDevice->TimerSQ3Tmax1.data = (unsigned long) pDevice;
641         pDevice->TimerSQ3Tmax1.function = (TimerFunction)TimerSQ3CallBack;
642         pDevice->TimerSQ3Tmax1.expires = RUN_AT(HZ);
643
644         init_timer(&pDevice->TimerSQ3Tmax2);
645         pDevice->TimerSQ3Tmax2.data = (unsigned long) pDevice;
646         pDevice->TimerSQ3Tmax2.function = (TimerFunction)TimerSQ3CallBack;
647         pDevice->TimerSQ3Tmax2.expires = RUN_AT(HZ);
648
649         init_timer(&pDevice->TimerSQ3Tmax3);
650         pDevice->TimerSQ3Tmax3.data = (unsigned long) pDevice;
651         pDevice->TimerSQ3Tmax3.function = (TimerFunction)TimerState1CallBack;
652         pDevice->TimerSQ3Tmax3.expires = RUN_AT(HZ);
653 }
654
655 static void device_print_info(struct vnt_private *pDevice)
656 {
657         dev_info(&pDevice->pcid->dev, "%s\n", get_chip_name(pDevice->chip_id));
658
659         dev_info(&pDevice->pcid->dev, "MAC=%pM IO=0x%lx Mem=0x%lx IRQ=%d\n",
660                  pDevice->abyCurrentNetAddr, (unsigned long)pDevice->ioaddr,
661                  (unsigned long)pDevice->PortOffset, pDevice->pcid->irq);
662 }
663
664 static void vt6655_init_info(struct pci_dev *pcid,
665                              struct vnt_private **ppDevice,
666                              PCHIP_INFO pChip_info)
667 {
668         memset(*ppDevice, 0, sizeof(**ppDevice));
669
670         (*ppDevice)->pcid = pcid;
671         (*ppDevice)->chip_id = pChip_info->chip_id;
672         (*ppDevice)->io_size = pChip_info->io_size;
673         (*ppDevice)->nTxQueues = pChip_info->nTxQueue;
674         (*ppDevice)->multicast_limit = 32;
675
676         spin_lock_init(&((*ppDevice)->lock));
677 }
678
679 static bool device_get_pci_info(struct vnt_private *pDevice,
680                                 struct pci_dev *pcid)
681 {
682         u16 pci_cmd;
683         u8  b;
684         unsigned int cis_addr;
685
686         pci_read_config_byte(pcid, PCI_REVISION_ID, &pDevice->byRevId);
687         pci_read_config_word(pcid, PCI_SUBSYSTEM_ID, &pDevice->SubSystemID);
688         pci_read_config_word(pcid, PCI_SUBSYSTEM_VENDOR_ID, &pDevice->SubVendorID);
689         pci_read_config_word(pcid, PCI_COMMAND, (u16 *)&(pci_cmd));
690
691         pci_set_master(pcid);
692
693         pDevice->memaddr = pci_resource_start(pcid, 0);
694         pDevice->ioaddr = pci_resource_start(pcid, 1);
695
696         cis_addr = pci_resource_start(pcid, 2);
697
698         pDevice->pcid = pcid;
699
700         pci_read_config_byte(pcid, PCI_COMMAND, &b);
701         pci_write_config_byte(pcid, PCI_COMMAND, (b|PCI_COMMAND_MASTER));
702
703         return true;
704 }
705
706 static void device_free_info(struct vnt_private *pDevice)
707 {
708         if (!pDevice)
709                 return;
710
711         if (pDevice->mac_hw)
712                 ieee80211_unregister_hw(pDevice->hw);
713
714         if (pDevice->PortOffset)
715                 iounmap(pDevice->PortOffset);
716
717         if (pDevice->pcid)
718                 pci_release_regions(pDevice->pcid);
719
720         if (pDevice->hw)
721                 ieee80211_free_hw(pDevice->hw);
722 }
723
724 static bool device_init_rings(struct vnt_private *pDevice)
725 {
726         void *vir_pool;
727
728         /*allocate all RD/TD rings a single pool*/
729         vir_pool = pci_zalloc_consistent(pDevice->pcid,
730                                          pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
731                                          pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
732                                          pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
733                                          pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
734                                          &pDevice->pool_dma);
735         if (vir_pool == NULL) {
736                 dev_err(&pDevice->pcid->dev, "allocate desc dma memory failed\n");
737                 return false;
738         }
739
740         pDevice->aRD0Ring = vir_pool;
741         pDevice->aRD1Ring = vir_pool +
742                 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
743
744         pDevice->rd0_pool_dma = pDevice->pool_dma;
745         pDevice->rd1_pool_dma = pDevice->rd0_pool_dma +
746                 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
747
748         pDevice->tx0_bufs = pci_zalloc_consistent(pDevice->pcid,
749                                                   pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
750                                                   pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
751                                                   CB_BEACON_BUF_SIZE +
752                                                   CB_MAX_BUF_SIZE,
753                                                   &pDevice->tx_bufs_dma0);
754         if (pDevice->tx0_bufs == NULL) {
755                 dev_err(&pDevice->pcid->dev, "allocate buf dma memory failed\n");
756
757                 pci_free_consistent(pDevice->pcid,
758                                     pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
759                                     pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
760                                     pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
761                                     pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
762                                     vir_pool, pDevice->pool_dma
763                         );
764                 return false;
765         }
766
767         pDevice->td0_pool_dma = pDevice->rd1_pool_dma +
768                 pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
769
770         pDevice->td1_pool_dma = pDevice->td0_pool_dma +
771                 pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
772
773         // vir_pool: pvoid type
774         pDevice->apTD0Rings = vir_pool
775                 + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
776                 + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
777
778         pDevice->apTD1Rings = vir_pool
779                 + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
780                 + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc)
781                 + pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
782
783         pDevice->tx1_bufs = pDevice->tx0_bufs +
784                 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
785
786         pDevice->tx_beacon_bufs = pDevice->tx1_bufs +
787                 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
788
789         pDevice->pbyTmpBuff = pDevice->tx_beacon_bufs +
790                 CB_BEACON_BUF_SIZE;
791
792         pDevice->tx_bufs_dma1 = pDevice->tx_bufs_dma0 +
793                 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
794
795         pDevice->tx_beacon_dma = pDevice->tx_bufs_dma1 +
796                 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
797
798         return true;
799 }
800
801 static void device_free_rings(struct vnt_private *pDevice)
802 {
803         pci_free_consistent(pDevice->pcid,
804                             pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
805                             pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
806                             pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
807                             pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
808                             ,
809                             pDevice->aRD0Ring, pDevice->pool_dma
810                 );
811
812         if (pDevice->tx0_bufs)
813                 pci_free_consistent(pDevice->pcid,
814                                     pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
815                                     pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
816                                     CB_BEACON_BUF_SIZE +
817                                     CB_MAX_BUF_SIZE,
818                                     pDevice->tx0_bufs, pDevice->tx_bufs_dma0
819                         );
820 }
821
822 static void device_init_rd0_ring(struct vnt_private *pDevice)
823 {
824         int i;
825         dma_addr_t      curr = pDevice->rd0_pool_dma;
826         PSRxDesc        pDesc;
827
828         /* Init the RD0 ring entries */
829         for (i = 0; i < pDevice->sOpts.nRxDescs0; i ++, curr += sizeof(SRxDesc)) {
830                 pDesc = &(pDevice->aRD0Ring[i]);
831                 pDesc->pRDInfo = alloc_rd_info();
832                 ASSERT(pDesc->pRDInfo);
833                 if (!device_alloc_rx_buf(pDevice, pDesc))
834                         dev_err(&pDevice->pcid->dev, "can not alloc rx bufs\n");
835
836                 pDesc->next = &(pDevice->aRD0Ring[(i+1) % pDevice->sOpts.nRxDescs0]);
837                 pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
838                 pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
839         }
840
841         if (i > 0)
842                 pDevice->aRD0Ring[i-1].next_desc = cpu_to_le32(pDevice->rd0_pool_dma);
843         pDevice->pCurrRD[0] = &(pDevice->aRD0Ring[0]);
844 }
845
846 static void device_init_rd1_ring(struct vnt_private *pDevice)
847 {
848         int i;
849         dma_addr_t      curr = pDevice->rd1_pool_dma;
850         PSRxDesc        pDesc;
851
852         /* Init the RD1 ring entries */
853         for (i = 0; i < pDevice->sOpts.nRxDescs1; i ++, curr += sizeof(SRxDesc)) {
854                 pDesc = &(pDevice->aRD1Ring[i]);
855                 pDesc->pRDInfo = alloc_rd_info();
856                 ASSERT(pDesc->pRDInfo);
857                 if (!device_alloc_rx_buf(pDevice, pDesc))
858                         dev_err(&pDevice->pcid->dev, "can not alloc rx bufs\n");
859
860                 pDesc->next = &(pDevice->aRD1Ring[(i+1) % pDevice->sOpts.nRxDescs1]);
861                 pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
862                 pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
863         }
864
865         if (i > 0)
866                 pDevice->aRD1Ring[i-1].next_desc = cpu_to_le32(pDevice->rd1_pool_dma);
867         pDevice->pCurrRD[1] = &(pDevice->aRD1Ring[0]);
868 }
869
870 static void device_init_defrag_cb(struct vnt_private *pDevice)
871 {
872         int i;
873         PSDeFragControlBlock pDeF;
874
875         /* Init the fragment ctl entries */
876         for (i = 0; i < CB_MAX_RX_FRAG; i++) {
877                 pDeF = &(pDevice->sRxDFCB[i]);
878                 if (!device_alloc_frag_buf(pDevice, pDeF))
879                         dev_err(&pDevice->pcid->dev, "can not alloc frag bufs\n");
880         }
881         pDevice->cbDFCB = CB_MAX_RX_FRAG;
882         pDevice->cbFreeDFCB = pDevice->cbDFCB;
883 }
884
885 static void device_free_rd0_ring(struct vnt_private *pDevice)
886 {
887         int i;
888
889         for (i = 0; i < pDevice->sOpts.nRxDescs0; i++) {
890                 PSRxDesc        pDesc = &(pDevice->aRD0Ring[i]);
891                 PDEVICE_RD_INFO  pRDInfo = pDesc->pRDInfo;
892
893                 pci_unmap_single(pDevice->pcid, pRDInfo->skb_dma,
894                                  pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
895
896                 dev_kfree_skb(pRDInfo->skb);
897
898                 kfree(pDesc->pRDInfo);
899         }
900 }
901
902 static void device_free_rd1_ring(struct vnt_private *pDevice)
903 {
904         int i;
905
906         for (i = 0; i < pDevice->sOpts.nRxDescs1; i++) {
907                 PSRxDesc        pDesc = &(pDevice->aRD1Ring[i]);
908                 PDEVICE_RD_INFO  pRDInfo = pDesc->pRDInfo;
909
910                 pci_unmap_single(pDevice->pcid, pRDInfo->skb_dma,
911                                  pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
912
913                 dev_kfree_skb(pRDInfo->skb);
914
915                 kfree(pDesc->pRDInfo);
916         }
917 }
918
919 static void device_free_frag_buf(struct vnt_private *pDevice)
920 {
921         PSDeFragControlBlock pDeF;
922         int i;
923
924         for (i = 0; i < CB_MAX_RX_FRAG; i++) {
925                 pDeF = &(pDevice->sRxDFCB[i]);
926
927                 if (pDeF->skb)
928                         dev_kfree_skb(pDeF->skb);
929
930         }
931 }
932
933 static void device_init_td0_ring(struct vnt_private *pDevice)
934 {
935         int i;
936         dma_addr_t  curr;
937         PSTxDesc        pDesc;
938
939         curr = pDevice->td0_pool_dma;
940         for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++, curr += sizeof(STxDesc)) {
941                 pDesc = &(pDevice->apTD0Rings[i]);
942                 pDesc->pTDInfo = alloc_td_info();
943                 ASSERT(pDesc->pTDInfo);
944                 if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
945                         pDesc->pTDInfo->buf = pDevice->tx0_bufs + (i)*PKT_BUF_SZ;
946                         pDesc->pTDInfo->buf_dma = pDevice->tx_bufs_dma0 + (i)*PKT_BUF_SZ;
947                 }
948                 pDesc->next = &(pDevice->apTD0Rings[(i+1) % pDevice->sOpts.nTxDescs[0]]);
949                 pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
950                 pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
951         }
952
953         if (i > 0)
954                 pDevice->apTD0Rings[i-1].next_desc = cpu_to_le32(pDevice->td0_pool_dma);
955         pDevice->apTailTD[0] = pDevice->apCurrTD[0] = &(pDevice->apTD0Rings[0]);
956 }
957
958 static void device_init_td1_ring(struct vnt_private *pDevice)
959 {
960         int i;
961         dma_addr_t  curr;
962         PSTxDesc    pDesc;
963
964         /* Init the TD ring entries */
965         curr = pDevice->td1_pool_dma;
966         for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++, curr += sizeof(STxDesc)) {
967                 pDesc = &(pDevice->apTD1Rings[i]);
968                 pDesc->pTDInfo = alloc_td_info();
969                 ASSERT(pDesc->pTDInfo);
970                 if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
971                         pDesc->pTDInfo->buf = pDevice->tx1_bufs + (i) * PKT_BUF_SZ;
972                         pDesc->pTDInfo->buf_dma = pDevice->tx_bufs_dma1 + (i) * PKT_BUF_SZ;
973                 }
974                 pDesc->next = &(pDevice->apTD1Rings[(i + 1) % pDevice->sOpts.nTxDescs[1]]);
975                 pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
976                 pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
977         }
978
979         if (i > 0)
980                 pDevice->apTD1Rings[i-1].next_desc = cpu_to_le32(pDevice->td1_pool_dma);
981         pDevice->apTailTD[1] = pDevice->apCurrTD[1] = &(pDevice->apTD1Rings[0]);
982 }
983
984 static void device_free_td0_ring(struct vnt_private *pDevice)
985 {
986         int i;
987
988         for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++) {
989                 PSTxDesc        pDesc = &(pDevice->apTD0Rings[i]);
990                 PDEVICE_TD_INFO  pTDInfo = pDesc->pTDInfo;
991
992                 if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
993                         pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma,
994                                          pTDInfo->skb->len, PCI_DMA_TODEVICE);
995
996                 if (pTDInfo->skb)
997                         dev_kfree_skb(pTDInfo->skb);
998
999                 kfree(pDesc->pTDInfo);
1000         }
1001 }
1002
1003 static void device_free_td1_ring(struct vnt_private *pDevice)
1004 {
1005         int i;
1006
1007         for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++) {
1008                 PSTxDesc        pDesc = &(pDevice->apTD1Rings[i]);
1009                 PDEVICE_TD_INFO  pTDInfo = pDesc->pTDInfo;
1010
1011                 if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
1012                         pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma,
1013                                          pTDInfo->skb->len, PCI_DMA_TODEVICE);
1014
1015                 if (pTDInfo->skb)
1016                         dev_kfree_skb(pTDInfo->skb);
1017
1018                 kfree(pDesc->pTDInfo);
1019         }
1020 }
1021
1022 /*-----------------------------------------------------------------*/
1023
1024 static int device_rx_srv(struct vnt_private *pDevice, unsigned int uIdx)
1025 {
1026         PSRxDesc    pRD;
1027         int works = 0;
1028
1029         for (pRD = pDevice->pCurrRD[uIdx];
1030              pRD->m_rd0RD0.f1Owner == OWNED_BY_HOST;
1031              pRD = pRD->next) {
1032                 if (works++ > 15)
1033                         break;
1034                 if (vnt_receive_frame(pDevice, pRD)) {
1035                         if (!device_alloc_rx_buf(pDevice, pRD)) {
1036                                 dev_err(&pDevice->pcid->dev,
1037                                         "can not allocate rx buf\n");
1038                                 break;
1039                         }
1040                 }
1041                 pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1042         }
1043
1044         pDevice->pCurrRD[uIdx] = pRD;
1045
1046         return works;
1047 }
1048
1049 static bool device_alloc_rx_buf(struct vnt_private *pDevice, PSRxDesc pRD)
1050 {
1051         PDEVICE_RD_INFO pRDInfo = pRD->pRDInfo;
1052
1053         pRDInfo->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1054         if (pRDInfo->skb == NULL)
1055                 return false;
1056         ASSERT(pRDInfo->skb);
1057
1058         pRDInfo->skb_dma =
1059                 pci_map_single(pDevice->pcid,
1060                                skb_put(pRDInfo->skb, skb_tailroom(pRDInfo->skb)),
1061                                pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1062
1063         *((unsigned int *)&(pRD->m_rd0RD0)) = 0; /* FIX cast */
1064
1065         pRD->m_rd0RD0.wResCount = cpu_to_le16(pDevice->rx_buf_sz);
1066         pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1067         pRD->m_rd1RD1.wReqCount = cpu_to_le16(pDevice->rx_buf_sz);
1068         pRD->buff_addr = cpu_to_le32(pRDInfo->skb_dma);
1069
1070         return true;
1071 }
1072
1073 bool device_alloc_frag_buf(struct vnt_private *pDevice,
1074                            PSDeFragControlBlock pDeF)
1075 {
1076         pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1077         if (pDeF->skb == NULL)
1078                 return false;
1079         ASSERT(pDeF->skb);
1080
1081         return true;
1082 }
1083
1084 static const u8 fallback_rate0[5][5] = {
1085         {RATE_18M, RATE_18M, RATE_12M, RATE_12M, RATE_12M},
1086         {RATE_24M, RATE_24M, RATE_18M, RATE_12M, RATE_12M},
1087         {RATE_36M, RATE_36M, RATE_24M, RATE_18M, RATE_18M},
1088         {RATE_48M, RATE_48M, RATE_36M, RATE_24M, RATE_24M},
1089         {RATE_54M, RATE_54M, RATE_48M, RATE_36M, RATE_36M}
1090 };
1091
1092 static const u8 fallback_rate1[5][5] = {
1093         {RATE_18M, RATE_18M, RATE_12M, RATE_6M, RATE_6M},
1094         {RATE_24M, RATE_24M, RATE_18M, RATE_6M, RATE_6M},
1095         {RATE_36M, RATE_36M, RATE_24M, RATE_12M, RATE_12M},
1096         {RATE_48M, RATE_48M, RATE_24M, RATE_12M, RATE_12M},
1097         {RATE_54M, RATE_54M, RATE_36M, RATE_18M, RATE_18M}
1098 };
1099
1100 static int vnt_int_report_rate(struct vnt_private *priv,
1101                                PDEVICE_TD_INFO context, u8 tsr0, u8 tsr1)
1102 {
1103         struct vnt_tx_fifo_head *fifo_head;
1104         struct ieee80211_tx_info *info;
1105         struct ieee80211_rate *rate;
1106         u16 fb_option;
1107         u8 tx_retry = (tsr0 & TSR0_NCR);
1108         s8 idx;
1109
1110         if (!context)
1111                 return -ENOMEM;
1112
1113         if (!context->skb)
1114                 return -EINVAL;
1115
1116         fifo_head = (struct vnt_tx_fifo_head *)context->buf;
1117         fb_option = (le16_to_cpu(fifo_head->fifo_ctl) &
1118                         (FIFOCTL_AUTO_FB_0 | FIFOCTL_AUTO_FB_1));
1119
1120         info = IEEE80211_SKB_CB(context->skb);
1121         idx = info->control.rates[0].idx;
1122
1123         if (fb_option && !(tsr1 & TSR1_TERR)) {
1124                 u8 tx_rate;
1125                 u8 retry = tx_retry;
1126
1127                 rate = ieee80211_get_tx_rate(priv->hw, info);
1128                 tx_rate = rate->hw_value - RATE_18M;
1129
1130                 if (retry > 4)
1131                         retry = 4;
1132
1133                 if (fb_option & FIFOCTL_AUTO_FB_0)
1134                         tx_rate = fallback_rate0[tx_rate][retry];
1135                 else if (fb_option & FIFOCTL_AUTO_FB_1)
1136                         tx_rate = fallback_rate1[tx_rate][retry];
1137
1138                 if (info->band == IEEE80211_BAND_5GHZ)
1139                         idx = tx_rate - RATE_6M;
1140                 else
1141                         idx = tx_rate;
1142         }
1143
1144         ieee80211_tx_info_clear_status(info);
1145
1146         info->status.rates[0].count = tx_retry;
1147
1148         if (!(tsr1 & TSR1_TERR)) {
1149                 info->status.rates[0].idx = idx;
1150                 info->flags |= IEEE80211_TX_STAT_ACK;
1151         }
1152
1153         return 0;
1154 }
1155
1156 static int device_tx_srv(struct vnt_private *pDevice, unsigned int uIdx)
1157 {
1158         PSTxDesc                 pTD;
1159         int                      works = 0;
1160         unsigned char byTsr0;
1161         unsigned char byTsr1;
1162
1163         for (pTD = pDevice->apTailTD[uIdx]; pDevice->iTDUsed[uIdx] > 0; pTD = pTD->next) {
1164                 if (pTD->m_td0TD0.f1Owner == OWNED_BY_NIC)
1165                         break;
1166                 if (works++ > 15)
1167                         break;
1168
1169                 byTsr0 = pTD->m_td0TD0.byTSR0;
1170                 byTsr1 = pTD->m_td0TD0.byTSR1;
1171
1172                 //Only the status of first TD in the chain is correct
1173                 if (pTD->m_td1TD1.byTCR & TCR_STP) {
1174                         if ((pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0) {
1175
1176                                 vnt_int_report_rate(pDevice, pTD->pTDInfo, byTsr0, byTsr1);
1177
1178                                 if (!(byTsr1 & TSR1_TERR)) {
1179                                         if (byTsr0 != 0) {
1180                                                 pr_debug(" Tx[%d] OK but has error. tsr1[%02X] tsr0[%02X]\n",
1181                                                          (int)uIdx, byTsr1,
1182                                                          byTsr0);
1183                                         }
1184                                 } else {
1185                                         pr_debug(" Tx[%d] dropped & tsr1[%02X] tsr0[%02X]\n",
1186                                                  (int)uIdx, byTsr1, byTsr0);
1187                                 }
1188                         }
1189
1190                         if (byTsr1 & TSR1_TERR) {
1191                                 if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1192                                         pr_debug(" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X]\n",
1193                                                  (int)uIdx, byTsr1, byTsr0);
1194                                 }
1195                         }
1196                         device_free_tx_buf(pDevice, pTD);
1197                         pDevice->iTDUsed[uIdx]--;
1198
1199                         /* Make sure queue is available */
1200                         if (AVAIL_TD(pDevice, uIdx))
1201                                 ieee80211_wake_queues(pDevice->hw);
1202                 }
1203         }
1204
1205         pDevice->apTailTD[uIdx] = pTD;
1206
1207         return works;
1208 }
1209
1210 static void device_error(struct vnt_private *pDevice, unsigned short status)
1211 {
1212         if (status & ISR_FETALERR) {
1213                 dev_err(&pDevice->pcid->dev, "Hardware fatal error\n");
1214
1215                 MACbShutdown(pDevice->PortOffset);
1216                 return;
1217         }
1218 }
1219
1220 static void device_free_tx_buf(struct vnt_private *pDevice, PSTxDesc pDesc)
1221 {
1222         PDEVICE_TD_INFO  pTDInfo = pDesc->pTDInfo;
1223         struct sk_buff *skb = pTDInfo->skb;
1224
1225         // pre-allocated buf_dma can't be unmapped.
1226         if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma)) {
1227                 pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma, skb->len,
1228                                  PCI_DMA_TODEVICE);
1229         }
1230
1231         if (pTDInfo->byFlags & TD_FLAGS_NETIF_SKB)
1232                 ieee80211_tx_status_irqsafe(pDevice->hw, skb);
1233         else
1234                 dev_kfree_skb_irq(skb);
1235
1236         pTDInfo->skb_dma = 0;
1237         pTDInfo->skb = NULL;
1238         pTDInfo->byFlags = 0;
1239 }
1240
1241 static  irqreturn_t  device_intr(int irq,  void *dev_instance)
1242 {
1243         struct vnt_private *pDevice = dev_instance;
1244         int             max_count = 0;
1245         unsigned long dwMIBCounter = 0;
1246         unsigned char byOrgPageSel = 0;
1247         int             handled = 0;
1248         int             ii = 0;
1249         unsigned long flags;
1250
1251         MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
1252
1253         if (pDevice->dwIsr == 0)
1254                 return IRQ_RETVAL(handled);
1255
1256         if (pDevice->dwIsr == 0xffffffff) {
1257                 pr_debug("dwIsr = 0xffff\n");
1258                 return IRQ_RETVAL(handled);
1259         }
1260
1261         handled = 1;
1262         MACvIntDisable(pDevice->PortOffset);
1263
1264         spin_lock_irqsave(&pDevice->lock, flags);
1265
1266         //Make sure current page is 0
1267         VNSvInPortB(pDevice->PortOffset + MAC_REG_PAGE1SEL, &byOrgPageSel);
1268         if (byOrgPageSel == 1)
1269                 MACvSelectPage0(pDevice->PortOffset);
1270         else
1271                 byOrgPageSel = 0;
1272
1273         MACvReadMIBCounter(pDevice->PortOffset, &dwMIBCounter);
1274         // TBD....
1275         // Must do this after doing rx/tx, cause ISR bit is slow
1276         // than RD/TD write back
1277         // update ISR counter
1278         STAvUpdate802_11Counter(&pDevice->s802_11Counter, &pDevice->scStatistic , dwMIBCounter);
1279         while (pDevice->dwIsr != 0) {
1280                 STAvUpdateIsrStatCounter(&pDevice->scStatistic, pDevice->dwIsr);
1281                 MACvWriteISR(pDevice->PortOffset, pDevice->dwIsr);
1282
1283                 if (pDevice->dwIsr & ISR_FETALERR) {
1284                         pr_debug(" ISR_FETALERR\n");
1285                         VNSvOutPortB(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, 0);
1286                         VNSvOutPortW(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPECTI);
1287                         device_error(pDevice, pDevice->dwIsr);
1288                 }
1289
1290                 if (pDevice->dwIsr & ISR_TBTT) {
1291                         if (pDevice->vif &&
1292                             pDevice->op_mode != NL80211_IFTYPE_ADHOC) {
1293                                 if (pDevice->bUpdateBBVGA &&
1294                                     !(pDevice->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) &&
1295                                     pDevice->vif->bss_conf.assoc &&
1296                                     pDevice->uCurrRSSI) {
1297                                         long            ldBm;
1298
1299                                         RFvRSSITodBm(pDevice, (unsigned char) pDevice->uCurrRSSI, &ldBm);
1300                                         for (ii = 0; ii < BB_VGA_LEVEL; ii++) {
1301                                                 if (ldBm < pDevice->ldBmThreshold[ii]) {
1302                                                         pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
1303                                                         break;
1304                                                 }
1305                                         }
1306                                         if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
1307                                                 pDevice->uBBVGADiffCount++;
1308                                                 if (pDevice->uBBVGADiffCount == 1) {
1309                                                         // first VGA diff gain
1310                                                         BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
1311                                                         pr_debug("First RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
1312                                                                  (int)ldBm,
1313                                                                  pDevice->byBBVGANew,
1314                                                                  pDevice->byBBVGACurrent,
1315                                                                  (int)pDevice->uBBVGADiffCount);
1316                                                 }
1317                                                 if (pDevice->uBBVGADiffCount >= BB_VGA_CHANGE_THRESHOLD) {
1318                                                         pr_debug("RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
1319                                                                  (int)ldBm,
1320                                                                  pDevice->byBBVGANew,
1321                                                                  pDevice->byBBVGACurrent,
1322                                                                  (int)pDevice->uBBVGADiffCount);
1323                                                         BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
1324                                                 }
1325                                         } else {
1326                                                 pDevice->uBBVGADiffCount = 1;
1327                                         }
1328                                 }
1329                         }
1330
1331                         pDevice->bBeaconSent = false;
1332                         if (pDevice->bEnablePSMode)
1333                                 PSbIsNextTBTTWakeUp((void *)pDevice);
1334
1335                         if ((pDevice->op_mode == NL80211_IFTYPE_AP ||
1336                             pDevice->op_mode == NL80211_IFTYPE_ADHOC) &&
1337                             pDevice->vif->bss_conf.enable_beacon) {
1338                                 MACvOneShotTimer1MicroSec(pDevice->PortOffset,
1339                                                           (pDevice->vif->bss_conf.beacon_int - MAKE_BEACON_RESERVED) << 10);
1340                         }
1341
1342                         /* TODO: adhoc PS mode */
1343
1344                 }
1345
1346                 if (pDevice->dwIsr & ISR_BNTX) {
1347                         if (pDevice->op_mode == NL80211_IFTYPE_ADHOC) {
1348                                 pDevice->bIsBeaconBufReadySet = false;
1349                                 pDevice->cbBeaconBufReadySetCnt = 0;
1350                         }
1351
1352                         pDevice->bBeaconSent = true;
1353                 }
1354
1355                 if (pDevice->dwIsr & ISR_RXDMA0)
1356                         max_count += device_rx_srv(pDevice, TYPE_RXDMA0);
1357
1358                 if (pDevice->dwIsr & ISR_RXDMA1)
1359                         max_count += device_rx_srv(pDevice, TYPE_RXDMA1);
1360
1361                 if (pDevice->dwIsr & ISR_TXDMA0)
1362                         max_count += device_tx_srv(pDevice, TYPE_TXDMA0);
1363
1364                 if (pDevice->dwIsr & ISR_AC0DMA)
1365                         max_count += device_tx_srv(pDevice, TYPE_AC0DMA);
1366
1367                 if (pDevice->dwIsr & ISR_SOFTTIMER1) {
1368                         if (pDevice->vif) {
1369                                 if (pDevice->vif->bss_conf.enable_beacon)
1370                                         vnt_beacon_make(pDevice, pDevice->vif);
1371                         }
1372                 }
1373
1374                 MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
1375
1376                 MACvReceive0(pDevice->PortOffset);
1377                 MACvReceive1(pDevice->PortOffset);
1378
1379                 if (max_count > pDevice->sOpts.int_works)
1380                         break;
1381         }
1382
1383         if (byOrgPageSel == 1)
1384                 MACvSelectPage1(pDevice->PortOffset);
1385
1386         spin_unlock_irqrestore(&pDevice->lock, flags);
1387
1388         MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
1389
1390         return IRQ_RETVAL(handled);
1391 }
1392
1393 static int vnt_tx_packet(struct vnt_private *priv, struct sk_buff *skb)
1394 {
1395         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1396         PSTxDesc head_td;
1397         u32 dma_idx = TYPE_AC0DMA;
1398         unsigned long flags;
1399
1400         spin_lock_irqsave(&priv->lock, flags);
1401
1402         if (!ieee80211_is_data(hdr->frame_control))
1403                 dma_idx = TYPE_TXDMA0;
1404
1405         if (AVAIL_TD(priv, dma_idx) < 1) {
1406                 spin_unlock_irqrestore(&priv->lock, flags);
1407                 return -ENOMEM;
1408         }
1409
1410         head_td = priv->apCurrTD[dma_idx];
1411
1412         head_td->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
1413
1414         head_td->pTDInfo->skb = skb;
1415
1416         priv->iTDUsed[dma_idx]++;
1417
1418         /* Take ownership */
1419         wmb();
1420         head_td->m_td0TD0.f1Owner = OWNED_BY_NIC;
1421
1422         /* get Next */
1423         wmb();
1424         priv->apCurrTD[dma_idx] = head_td->next;
1425
1426         spin_unlock_irqrestore(&priv->lock, flags);
1427
1428         vnt_generate_fifo_header(priv, dma_idx, head_td, skb);
1429
1430         if (MACbIsRegBitsOn(priv->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
1431                 MACbPSWakeup(priv->PortOffset);
1432
1433         spin_lock_irqsave(&priv->lock, flags);
1434
1435         priv->bPWBitOn = false;
1436
1437         head_td->pTDInfo->byFlags = TD_FLAGS_NETIF_SKB;
1438
1439         if (dma_idx == TYPE_AC0DMA)
1440                 MACvTransmitAC0(priv->PortOffset);
1441         else
1442                 MACvTransmit0(priv->PortOffset);
1443
1444         spin_unlock_irqrestore(&priv->lock, flags);
1445
1446         return 0;
1447 }
1448
1449 static void vnt_tx_80211(struct ieee80211_hw *hw,
1450                          struct ieee80211_tx_control *control,
1451                          struct sk_buff *skb)
1452 {
1453         struct vnt_private *priv = hw->priv;
1454
1455         ieee80211_stop_queues(hw);
1456
1457         if (vnt_tx_packet(priv, skb)) {
1458                 ieee80211_free_txskb(hw, skb);
1459
1460                 ieee80211_wake_queues(hw);
1461         }
1462 }
1463
1464 static int vnt_start(struct ieee80211_hw *hw)
1465 {
1466         struct vnt_private *priv = hw->priv;
1467         int ret;
1468
1469         priv->rx_buf_sz = PKT_BUF_SZ;
1470         if (!device_init_rings(priv))
1471                 return -ENOMEM;
1472
1473         ret = request_irq(priv->pcid->irq, &device_intr,
1474                           IRQF_SHARED, "vt6655", priv);
1475         if (ret) {
1476                 dev_dbg(&priv->pcid->dev, "failed to start irq\n");
1477                 return ret;
1478         }
1479
1480         dev_dbg(&priv->pcid->dev, "call device init rd0 ring\n");
1481         device_init_rd0_ring(priv);
1482         device_init_rd1_ring(priv);
1483         device_init_defrag_cb(priv);
1484         device_init_td0_ring(priv);
1485         device_init_td1_ring(priv);
1486
1487         device_init_registers(priv);
1488
1489         dev_dbg(&priv->pcid->dev, "call MACvIntEnable\n");
1490         MACvIntEnable(priv->PortOffset, IMR_MASK_VALUE);
1491
1492         ieee80211_wake_queues(hw);
1493
1494         return 0;
1495 }
1496
1497 static void vnt_stop(struct ieee80211_hw *hw)
1498 {
1499         struct vnt_private *priv = hw->priv;
1500
1501         ieee80211_stop_queues(hw);
1502
1503         MACbShutdown(priv->PortOffset);
1504         MACbSoftwareReset(priv->PortOffset);
1505         CARDbRadioPowerOff(priv);
1506
1507         device_free_td0_ring(priv);
1508         device_free_td1_ring(priv);
1509         device_free_rd0_ring(priv);
1510         device_free_rd1_ring(priv);
1511         device_free_frag_buf(priv);
1512         device_free_rings(priv);
1513
1514         free_irq(priv->pcid->irq, priv);
1515 }
1516
1517 static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1518 {
1519         struct vnt_private *priv = hw->priv;
1520
1521         priv->vif = vif;
1522
1523         switch (vif->type) {
1524         case NL80211_IFTYPE_STATION:
1525                 if (priv->bDiversityRegCtlON)
1526                         device_init_diversity_timer(priv);
1527                 break;
1528         case NL80211_IFTYPE_ADHOC:
1529                 MACvRegBitsOff(priv->PortOffset, MAC_REG_RCR, RCR_UNICAST);
1530
1531                 MACvRegBitsOn(priv->PortOffset, MAC_REG_HOSTCR, HOSTCR_ADHOC);
1532
1533                 break;
1534         case NL80211_IFTYPE_AP:
1535                 MACvRegBitsOff(priv->PortOffset, MAC_REG_RCR, RCR_UNICAST);
1536
1537                 MACvRegBitsOn(priv->PortOffset, MAC_REG_HOSTCR, HOSTCR_AP);
1538
1539                 break;
1540         default:
1541                 return -EOPNOTSUPP;
1542         }
1543
1544         priv->op_mode = vif->type;
1545
1546         return 0;
1547 }
1548
1549 static void vnt_remove_interface(struct ieee80211_hw *hw,
1550                                  struct ieee80211_vif *vif)
1551 {
1552         struct vnt_private *priv = hw->priv;
1553
1554         switch (vif->type) {
1555         case NL80211_IFTYPE_STATION:
1556                 if (priv->bDiversityRegCtlON) {
1557                         del_timer(&priv->TimerSQ3Tmax1);
1558                         del_timer(&priv->TimerSQ3Tmax2);
1559                         del_timer(&priv->TimerSQ3Tmax3);
1560                 }
1561                 break;
1562         case NL80211_IFTYPE_ADHOC:
1563                 MACvRegBitsOff(priv->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
1564                 MACvRegBitsOff(priv->PortOffset,
1565                                MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1566                 MACvRegBitsOff(priv->PortOffset, MAC_REG_HOSTCR, HOSTCR_ADHOC);
1567                 break;
1568         case NL80211_IFTYPE_AP:
1569                 MACvRegBitsOff(priv->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
1570                 MACvRegBitsOff(priv->PortOffset,
1571                                MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1572                 MACvRegBitsOff(priv->PortOffset, MAC_REG_HOSTCR, HOSTCR_AP);
1573                 break;
1574         default:
1575                 break;
1576         }
1577
1578         priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
1579 }
1580
1581
1582 static int vnt_config(struct ieee80211_hw *hw, u32 changed)
1583 {
1584         struct vnt_private *priv = hw->priv;
1585         struct ieee80211_conf *conf = &hw->conf;
1586         u8 bb_type;
1587
1588         if (changed & IEEE80211_CONF_CHANGE_PS) {
1589                 if (conf->flags & IEEE80211_CONF_PS)
1590                         PSvEnablePowerSaving(priv, conf->listen_interval);
1591                 else
1592                         PSvDisablePowerSaving(priv);
1593         }
1594
1595         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
1596             (conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
1597                 set_channel(priv, conf->chandef.chan->hw_value);
1598
1599                 if (conf->chandef.chan->band == IEEE80211_BAND_5GHZ)
1600                         bb_type = BB_TYPE_11A;
1601                 else
1602                         bb_type = BB_TYPE_11G;
1603
1604                 if (priv->byBBType != bb_type) {
1605                         priv->byBBType = bb_type;
1606
1607                         CARDbSetPhyParameter(priv,
1608                                              priv->byBBType, 0, 0, NULL, NULL);
1609                 }
1610         }
1611
1612         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1613                 if (priv->byBBType == BB_TYPE_11B)
1614                         priv->wCurrentRate = RATE_1M;
1615                 else
1616                         priv->wCurrentRate = RATE_54M;
1617
1618                 RFbSetPower(priv, priv->wCurrentRate,
1619                             conf->chandef.chan->hw_value);
1620         }
1621
1622         return 0;
1623 }
1624
1625 static void vnt_bss_info_changed(struct ieee80211_hw *hw,
1626                 struct ieee80211_vif *vif, struct ieee80211_bss_conf *conf,
1627                 u32 changed)
1628 {
1629         struct vnt_private *priv = hw->priv;
1630
1631         priv->current_aid = conf->aid;
1632
1633         if (changed & BSS_CHANGED_BSSID)
1634                 MACvWriteBSSIDAddress(priv->PortOffset, (u8 *)conf->bssid);
1635
1636         if (changed & BSS_CHANGED_BASIC_RATES) {
1637                 priv->basic_rates = conf->basic_rates;
1638
1639                 CARDvUpdateBasicTopRate(priv);
1640
1641                 dev_dbg(&priv->pcid->dev,
1642                         "basic rates %x\n", conf->basic_rates);
1643         }
1644
1645         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1646                 if (conf->use_short_preamble) {
1647                         MACvEnableBarkerPreambleMd(priv->PortOffset);
1648                         priv->byPreambleType = true;
1649                 } else {
1650                         MACvDisableBarkerPreambleMd(priv->PortOffset);
1651                         priv->byPreambleType = false;
1652                 }
1653         }
1654
1655         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1656                 if (conf->use_cts_prot)
1657                         MACvEnableProtectMD(priv->PortOffset);
1658                 else
1659                         MACvDisableProtectMD(priv->PortOffset);
1660         }
1661
1662         if (changed & BSS_CHANGED_ERP_SLOT) {
1663                 if (conf->use_short_slot)
1664                         priv->bShortSlotTime = true;
1665                 else
1666                         priv->bShortSlotTime = false;
1667
1668                 vUpdateIFS(priv);
1669                 CARDbSetPhyParameter(priv, priv->byBBType, 0, 0, NULL, NULL);
1670                 BBvSetVGAGainOffset(priv, priv->abyBBVGA[0]);
1671         }
1672
1673         if (changed & BSS_CHANGED_TXPOWER)
1674                 RFbSetPower(priv, priv->wCurrentRate,
1675                             conf->chandef.chan->hw_value);
1676
1677         if (changed & BSS_CHANGED_BEACON_ENABLED) {
1678                 dev_dbg(&priv->pcid->dev,
1679                         "Beacon enable %d\n", conf->enable_beacon);
1680
1681                 if (conf->enable_beacon) {
1682                         vnt_beacon_enable(priv, vif, conf);
1683
1684                         MACvRegBitsOn(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1685                 } else {
1686                         MACvRegBitsOff(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1687                 }
1688         }
1689
1690         if (changed & BSS_CHANGED_ASSOC && priv->op_mode != NL80211_IFTYPE_AP) {
1691                 if (conf->assoc) {
1692                         CARDbUpdateTSF(priv, conf->beacon_rate->hw_value,
1693                                        conf->sync_device_ts, conf->sync_tsf);
1694
1695                         CARDbSetBeaconPeriod(priv, conf->beacon_int);
1696
1697                         CARDvSetFirstNextTBTT(priv->PortOffset,
1698                                               conf->beacon_int);
1699                 } else {
1700                         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL,
1701                                      TFTCTL_TSFCNTRST);
1702                         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL,
1703                                      TFTCTL_TSFCNTREN);
1704                 }
1705         }
1706 }
1707
1708 static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
1709         struct netdev_hw_addr_list *mc_list)
1710 {
1711         struct vnt_private *priv = hw->priv;
1712         struct netdev_hw_addr *ha;
1713         u64 mc_filter = 0;
1714         u32 bit_nr = 0;
1715
1716         netdev_hw_addr_list_for_each(ha, mc_list) {
1717                 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1718
1719                 mc_filter |= 1ULL << (bit_nr & 0x3f);
1720         }
1721
1722         priv->mc_list_count = mc_list->count;
1723
1724         return mc_filter;
1725 }
1726
1727 static void vnt_configure(struct ieee80211_hw *hw,
1728         unsigned int changed_flags, unsigned int *total_flags, u64 multicast)
1729 {
1730         struct vnt_private *priv = hw->priv;
1731         u8 rx_mode = 0;
1732
1733         *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_PROMISC_IN_BSS |
1734                 FIF_BCN_PRBRESP_PROMISC;
1735
1736         VNSvInPortB(priv->PortOffset + MAC_REG_RCR, &rx_mode);
1737
1738         dev_dbg(&priv->pcid->dev, "rx mode in = %x\n", rx_mode);
1739
1740         if (changed_flags & FIF_PROMISC_IN_BSS) {
1741                 /* unconditionally log net taps */
1742                 if (*total_flags & FIF_PROMISC_IN_BSS)
1743                         rx_mode |= RCR_UNICAST;
1744                 else
1745                         rx_mode &= ~RCR_UNICAST;
1746         }
1747
1748         if (changed_flags & FIF_ALLMULTI) {
1749                 if (*total_flags & FIF_ALLMULTI) {
1750                         if (priv->mc_list_count > 2) {
1751                                 MACvSelectPage1(priv->PortOffset);
1752
1753                                 VNSvOutPortD(priv->PortOffset +
1754                                              MAC_REG_MAR0, 0xffffffff);
1755                                 VNSvOutPortD(priv->PortOffset +
1756                                             MAC_REG_MAR0 + 4, 0xffffffff);
1757
1758                                 MACvSelectPage0(priv->PortOffset);
1759                         } else {
1760                                 MACvSelectPage1(priv->PortOffset);
1761
1762                                 VNSvOutPortD(priv->PortOffset +
1763                                              MAC_REG_MAR0, (u32)multicast);
1764                                 VNSvOutPortD(priv->PortOffset +
1765                                              MAC_REG_MAR0 + 4,
1766                                              (u32)(multicast >> 32));
1767
1768                                 MACvSelectPage0(priv->PortOffset);
1769                         }
1770
1771                         rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
1772                 } else {
1773                         rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST);
1774                 }
1775         }
1776
1777         if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) {
1778                 rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
1779
1780                 if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC))
1781                         rx_mode &= ~RCR_BSSID;
1782                 else
1783                         rx_mode |= RCR_BSSID;
1784         }
1785
1786         VNSvOutPortB(priv->PortOffset + MAC_REG_RCR, rx_mode);
1787
1788         dev_dbg(&priv->pcid->dev, "rx mode out= %x\n", rx_mode);
1789 }
1790
1791 static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1792         struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1793                 struct ieee80211_key_conf *key)
1794 {
1795         struct vnt_private *priv = hw->priv;
1796
1797         switch (cmd) {
1798         case SET_KEY:
1799                 if (vnt_set_keys(hw, sta, vif, key))
1800                         return -EOPNOTSUPP;
1801                 break;
1802         case DISABLE_KEY:
1803                 if (test_bit(key->hw_key_idx, &priv->key_entry_inuse))
1804                         clear_bit(key->hw_key_idx, &priv->key_entry_inuse);
1805         default:
1806                 break;
1807         }
1808
1809         return 0;
1810 }
1811
1812 static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1813 {
1814         struct vnt_private *priv = hw->priv;
1815         u64 tsf;
1816
1817         CARDbGetCurrentTSF(priv->PortOffset, &tsf);
1818
1819         return tsf;
1820 }
1821
1822 static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1823                         u64 tsf)
1824 {
1825         struct vnt_private *priv = hw->priv;
1826
1827         CARDvUpdateNextTBTT(priv->PortOffset, tsf, vif->bss_conf.beacon_int);
1828 }
1829
1830 static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1831 {
1832         struct vnt_private *priv = hw->priv;
1833
1834         /* reset TSF counter */
1835         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
1836 }
1837
1838 static const struct ieee80211_ops vnt_mac_ops = {
1839         .tx                     = vnt_tx_80211,
1840         .start                  = vnt_start,
1841         .stop                   = vnt_stop,
1842         .add_interface          = vnt_add_interface,
1843         .remove_interface       = vnt_remove_interface,
1844         .config                 = vnt_config,
1845         .bss_info_changed       = vnt_bss_info_changed,
1846         .prepare_multicast      = vnt_prepare_multicast,
1847         .configure_filter       = vnt_configure,
1848         .set_key                = vnt_set_key,
1849         .get_tsf                = vnt_get_tsf,
1850         .set_tsf                = vnt_set_tsf,
1851         .reset_tsf              = vnt_reset_tsf,
1852 };
1853
1854 int vnt_init(struct vnt_private *priv)
1855 {
1856         SET_IEEE80211_PERM_ADDR(priv->hw, priv->abyCurrentNetAddr);
1857
1858         vnt_init_bands(priv);
1859
1860         if (ieee80211_register_hw(priv->hw))
1861                 return -ENODEV;
1862
1863         priv->mac_hw = true;
1864
1865         CARDbRadioPowerOff(priv);
1866
1867         return 0;
1868 }
1869
1870 static int
1871 vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent)
1872 {
1873         PCHIP_INFO  pChip_info = (PCHIP_INFO)ent->driver_data;
1874         struct vnt_private *priv;
1875         struct ieee80211_hw *hw;
1876         struct wiphy *wiphy;
1877         int         rc;
1878
1879         dev_notice(&pcid->dev,
1880                    "%s Ver. %s\n", DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
1881
1882         dev_notice(&pcid->dev,
1883                    "Copyright (c) 2003 VIA Networking Technologies, Inc.\n");
1884
1885         hw = ieee80211_alloc_hw(sizeof(*priv), &vnt_mac_ops);
1886         if (!hw) {
1887                 dev_err(&pcid->dev, "could not register ieee80211_hw\n");
1888                 return -ENOMEM;
1889         }
1890
1891         priv = hw->priv;
1892
1893         vt6655_init_info(pcid, &priv, pChip_info);
1894
1895         priv->hw = hw;
1896
1897         SET_IEEE80211_DEV(priv->hw, &pcid->dev);
1898
1899         if (pci_enable_device(pcid)) {
1900                 device_free_info(priv);
1901                 return -ENODEV;
1902         }
1903
1904         dev_dbg(&pcid->dev,
1905                 "Before get pci_info memaddr is %x\n", priv->memaddr);
1906
1907         if (!device_get_pci_info(priv, pcid)) {
1908                 dev_err(&pcid->dev, ": Failed to find PCI device.\n");
1909                 device_free_info(priv);
1910                 return -ENODEV;
1911         }
1912
1913 #ifdef  DEBUG
1914         dev_dbg(&pcid->dev,
1915                 "after get pci_info memaddr is %x, io addr is %x,io_size is %d\n",
1916                 priv->memaddr, priv->ioaddr, priv->io_size);
1917         {
1918                 int i;
1919                 u32 bar, len;
1920                 u32 address[] = {
1921                         PCI_BASE_ADDRESS_0,
1922                         PCI_BASE_ADDRESS_1,
1923                         PCI_BASE_ADDRESS_2,
1924                         PCI_BASE_ADDRESS_3,
1925                         PCI_BASE_ADDRESS_4,
1926                         PCI_BASE_ADDRESS_5,
1927                         0};
1928                 for (i = 0; address[i]; i++) {
1929                         pci_read_config_dword(pcid, address[i], &bar);
1930
1931                         dev_dbg(&pcid->dev, "bar %d is %x\n", i, bar);
1932
1933                         if (!bar) {
1934                                 dev_dbg(&pcid->dev,
1935                                         "bar %d not implemented\n", i);
1936                                 continue;
1937                         }
1938
1939                         if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
1940                                 /* This is IO */
1941
1942                                 len = bar & (PCI_BASE_ADDRESS_IO_MASK & 0xffff);
1943                                 len = len & ~(len - 1);
1944
1945                                 dev_dbg(&pcid->dev,
1946                                         "IO space:  len in IO %x, BAR %d\n",
1947                                         len, i);
1948                         } else {
1949                                 len = bar & 0xfffffff0;
1950                                 len = ~len + 1;
1951
1952                                 dev_dbg(&pcid->dev,
1953                                         "len in MEM %x, BAR %d\n", len, i);
1954                         }
1955                 }
1956         }
1957 #endif
1958
1959         priv->PortOffset = ioremap(priv->memaddr & PCI_BASE_ADDRESS_MEM_MASK,
1960                                    priv->io_size);
1961         if (!priv->PortOffset) {
1962                 dev_err(&pcid->dev, ": Failed to IO remapping ..\n");
1963                 device_free_info(priv);
1964                 return -ENODEV;
1965         }
1966
1967         rc = pci_request_regions(pcid, DEVICE_NAME);
1968         if (rc) {
1969                 dev_err(&pcid->dev, ": Failed to find PCI device\n");
1970                 device_free_info(priv);
1971                 return -ENODEV;
1972         }
1973
1974         /* do reset */
1975         if (!MACbSoftwareReset(priv->PortOffset)) {
1976                 dev_err(&pcid->dev, ": Failed to access MAC hardware..\n");
1977                 device_free_info(priv);
1978                 return -ENODEV;
1979         }
1980         /* initial to reload eeprom */
1981         MACvInitialize(priv->PortOffset);
1982         MACvReadEtherAddress(priv->PortOffset, priv->abyCurrentNetAddr);
1983
1984         device_get_options(priv);
1985         device_set_options(priv);
1986         /* Mask out the options cannot be set to the chip */
1987         priv->sOpts.flags &= pChip_info->flags;
1988
1989         /* Enable the chip specified capabilities */
1990         priv->flags = priv->sOpts.flags | (pChip_info->flags & 0xff000000UL);
1991
1992         wiphy = priv->hw->wiphy;
1993
1994         wiphy->frag_threshold = FRAG_THRESH_DEF;
1995         wiphy->rts_threshold = RTS_THRESH_DEF;
1996         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1997                 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);
1998
1999         priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
2000                 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
2001                 IEEE80211_HW_SIGNAL_DBM |
2002                 IEEE80211_HW_TIMING_BEACON_ONLY;
2003
2004         priv->hw->max_signal = 100;
2005
2006         if (vnt_init(priv))
2007                 return -ENODEV;
2008
2009         device_print_info(priv);
2010         pci_set_drvdata(pcid, priv);
2011
2012         return 0;
2013 }
2014
2015 /*------------------------------------------------------------------*/
2016
2017 #ifdef CONFIG_PM
2018 static int vt6655_suspend(struct pci_dev *pcid, pm_message_t state)
2019 {
2020         struct vnt_private *priv = pci_get_drvdata(pcid);
2021         unsigned long flags;
2022
2023         spin_lock_irqsave(&priv->lock, flags);
2024
2025         pci_save_state(pcid);
2026
2027         MACbShutdown(priv->PortOffset);
2028
2029         pci_disable_device(pcid);
2030         pci_set_power_state(pcid, pci_choose_state(pcid, state));
2031
2032         spin_unlock_irqrestore(&priv->lock, flags);
2033
2034         return 0;
2035 }
2036
2037 static int vt6655_resume(struct pci_dev *pcid)
2038 {
2039
2040         pci_set_power_state(pcid, PCI_D0);
2041         pci_enable_wake(pcid, PCI_D0, 0);
2042         pci_restore_state(pcid);
2043
2044         return 0;
2045 }
2046 #endif
2047
2048 MODULE_DEVICE_TABLE(pci, vt6655_pci_id_table);
2049
2050 static struct pci_driver device_driver = {
2051         .name = DEVICE_NAME,
2052         .id_table = vt6655_pci_id_table,
2053         .probe = vt6655_probe,
2054         .remove = vt6655_remove,
2055 #ifdef CONFIG_PM
2056         .suspend = vt6655_suspend,
2057         .resume = vt6655_resume,
2058 #endif
2059 };
2060
2061 static int __init vt6655_init_module(void)
2062 {
2063         int ret;
2064
2065         ret = pci_register_driver(&device_driver);
2066 #ifdef CONFIG_PM
2067         if (ret >= 0)
2068                 register_reboot_notifier(&device_notifier);
2069 #endif
2070
2071         return ret;
2072 }
2073
2074 static void __exit vt6655_cleanup_module(void)
2075 {
2076 #ifdef CONFIG_PM
2077         unregister_reboot_notifier(&device_notifier);
2078 #endif
2079         pci_unregister_driver(&device_driver);
2080 }
2081
2082 module_init(vt6655_init_module);
2083 module_exit(vt6655_cleanup_module);
2084
2085 #ifdef CONFIG_PM
2086 static int
2087 device_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
2088 {
2089         struct pci_dev *pdev = NULL;
2090
2091         switch (event) {
2092         case SYS_DOWN:
2093         case SYS_HALT:
2094         case SYS_POWER_OFF:
2095                 for_each_pci_dev(pdev) {
2096                         if (pci_dev_driver(pdev) == &device_driver) {
2097                                 if (pci_get_drvdata(pdev))
2098                                         vt6655_suspend(pdev, PMSG_HIBERNATE);
2099                         }
2100                 }
2101         }
2102         return NOTIFY_DONE;
2103 }
2104 #endif