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